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 GetInlineCacheState() == UNINITIALIZED;
1751 InlineCacheState GetInlineCacheState() const {
1752 return InlineCacheStateField::decode(bit_field_);
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 set_inline_cache_state(InlineCacheState state) {
1761 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1763 void mark_for_call() {
1764 bit_field_ = IsForCallField::update(bit_field_, true);
1766 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1768 bool IsSuperAccess() {
1769 return obj()->IsSuperReference();
1772 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1773 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1774 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1776 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1777 ICSlotCache* cache) OVERRIDE {
1778 property_feedback_slot_ = slot;
1780 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1781 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1784 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1785 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1786 return property_feedback_slot_;
1790 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1791 : Expression(zone, pos),
1792 bit_field_(IsForCallField::encode(false) |
1793 IsStringAccessField::encode(false) |
1794 InlineCacheStateField::encode(UNINITIALIZED)),
1795 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1798 static int parent_num_ids() { return Expression::num_ids(); }
1801 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1803 class IsForCallField : public BitField8<bool, 0, 1> {};
1804 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1805 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1806 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1808 FeedbackVectorICSlot property_feedback_slot_;
1811 SmallMapList receiver_types_;
1815 class Call FINAL : public Expression {
1817 DECLARE_NODE_TYPE(Call)
1819 Expression* expression() const { return expression_; }
1820 ZoneList<Expression*>* arguments() const { return arguments_; }
1822 // Type feedback information.
1823 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1824 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1825 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1826 ICSlotCache* cache) OVERRIDE {
1827 ic_slot_or_slot_ = slot.ToInt();
1829 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1830 ic_slot_or_slot_ = slot.ToInt();
1832 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1834 FeedbackVectorSlot CallFeedbackSlot() const {
1835 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1836 return FeedbackVectorSlot(ic_slot_or_slot_);
1839 FeedbackVectorICSlot CallFeedbackICSlot() const {
1840 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1841 return FeedbackVectorICSlot(ic_slot_or_slot_);
1844 SmallMapList* GetReceiverTypes() OVERRIDE {
1845 if (expression()->IsProperty()) {
1846 return expression()->AsProperty()->GetReceiverTypes();
1851 bool IsMonomorphic() OVERRIDE {
1852 if (expression()->IsProperty()) {
1853 return expression()->AsProperty()->IsMonomorphic();
1855 return !target_.is_null();
1858 bool global_call() const {
1859 VariableProxy* proxy = expression_->AsVariableProxy();
1860 return proxy != NULL && proxy->var()->IsUnallocated();
1863 bool known_global_function() const {
1864 return global_call() && !target_.is_null();
1867 Handle<JSFunction> target() { return target_; }
1869 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1871 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1873 set_is_uninitialized(false);
1875 void set_target(Handle<JSFunction> target) { target_ = target; }
1876 void set_allocation_site(Handle<AllocationSite> site) {
1877 allocation_site_ = site;
1880 static int num_ids() { return parent_num_ids() + 2; }
1881 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1882 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1884 bool is_uninitialized() const {
1885 return IsUninitializedField::decode(bit_field_);
1887 void set_is_uninitialized(bool b) {
1888 bit_field_ = IsUninitializedField::update(bit_field_, b);
1900 // Helpers to determine how to handle the call.
1901 CallType GetCallType(Isolate* isolate) const;
1902 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1903 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1906 // Used to assert that the FullCodeGenerator records the return site.
1907 bool return_is_recorded_;
1911 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1913 : Expression(zone, pos),
1914 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1915 expression_(expression),
1916 arguments_(arguments),
1917 bit_field_(IsUninitializedField::encode(false)) {
1918 if (expression->IsProperty()) {
1919 expression->AsProperty()->mark_for_call();
1922 static int parent_num_ids() { return Expression::num_ids(); }
1925 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1927 // We store this as an integer because we don't know if we have a slot or
1928 // an ic slot until scoping time.
1929 int ic_slot_or_slot_;
1930 Expression* expression_;
1931 ZoneList<Expression*>* arguments_;
1932 Handle<JSFunction> target_;
1933 Handle<AllocationSite> allocation_site_;
1934 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1939 class CallNew FINAL : public Expression {
1941 DECLARE_NODE_TYPE(CallNew)
1943 Expression* expression() const { return expression_; }
1944 ZoneList<Expression*>* arguments() const { return arguments_; }
1946 // Type feedback information.
1947 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1948 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1949 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1951 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1952 callnew_feedback_slot_ = slot;
1955 FeedbackVectorSlot CallNewFeedbackSlot() {
1956 DCHECK(!callnew_feedback_slot_.IsInvalid());
1957 return callnew_feedback_slot_;
1959 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1960 DCHECK(FLAG_pretenuring_call_new);
1961 return CallNewFeedbackSlot().next();
1964 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1965 Handle<JSFunction> target() const { return target_; }
1966 Handle<AllocationSite> allocation_site() const {
1967 return allocation_site_;
1970 static int num_ids() { return parent_num_ids() + 1; }
1971 static int feedback_slots() { return 1; }
1972 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1974 void set_allocation_site(Handle<AllocationSite> site) {
1975 allocation_site_ = site;
1977 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1978 void set_target(Handle<JSFunction> target) { target_ = target; }
1979 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1981 is_monomorphic_ = true;
1985 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1987 : Expression(zone, pos),
1988 expression_(expression),
1989 arguments_(arguments),
1990 is_monomorphic_(false),
1991 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1993 static int parent_num_ids() { return Expression::num_ids(); }
1996 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1998 Expression* expression_;
1999 ZoneList<Expression*>* arguments_;
2000 bool is_monomorphic_;
2001 Handle<JSFunction> target_;
2002 Handle<AllocationSite> allocation_site_;
2003 FeedbackVectorSlot callnew_feedback_slot_;
2007 // The CallRuntime class does not represent any official JavaScript
2008 // language construct. Instead it is used to call a C or JS function
2009 // with a set of arguments. This is used from the builtins that are
2010 // implemented in JavaScript (see "v8natives.js").
2011 class CallRuntime FINAL : public Expression {
2013 DECLARE_NODE_TYPE(CallRuntime)
2015 Handle<String> name() const { return raw_name_->string(); }
2016 const AstRawString* raw_name() const { return raw_name_; }
2017 const Runtime::Function* function() const { return function_; }
2018 ZoneList<Expression*>* arguments() const { return arguments_; }
2019 bool is_jsruntime() const { return function_ == NULL; }
2021 // Type feedback information.
2022 bool HasCallRuntimeFeedbackSlot() const {
2023 return FLAG_vector_ics && is_jsruntime();
2025 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2026 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2027 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2029 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2030 ICSlotCache* cache) OVERRIDE {
2031 callruntime_feedback_slot_ = slot;
2033 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2035 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2036 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2037 !callruntime_feedback_slot_.IsInvalid());
2038 return callruntime_feedback_slot_;
2041 static int num_ids() { return parent_num_ids() + 1; }
2042 TypeFeedbackId CallRuntimeFeedbackId() const {
2043 return TypeFeedbackId(local_id(0));
2047 CallRuntime(Zone* zone, const AstRawString* name,
2048 const Runtime::Function* function,
2049 ZoneList<Expression*>* arguments, int pos)
2050 : Expression(zone, pos),
2052 function_(function),
2053 arguments_(arguments),
2054 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2055 static int parent_num_ids() { return Expression::num_ids(); }
2058 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2060 const AstRawString* raw_name_;
2061 const Runtime::Function* function_;
2062 ZoneList<Expression*>* arguments_;
2063 FeedbackVectorICSlot callruntime_feedback_slot_;
2067 class UnaryOperation FINAL : public Expression {
2069 DECLARE_NODE_TYPE(UnaryOperation)
2071 Token::Value op() const { return op_; }
2072 Expression* expression() const { return expression_; }
2074 // For unary not (Token::NOT), the AST ids where true and false will
2075 // actually be materialized, respectively.
2076 static int num_ids() { return parent_num_ids() + 2; }
2077 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2078 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2080 virtual void RecordToBooleanTypeFeedback(
2081 TypeFeedbackOracle* oracle) OVERRIDE;
2084 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2085 : Expression(zone, pos), op_(op), expression_(expression) {
2086 DCHECK(Token::IsUnaryOp(op));
2088 static int parent_num_ids() { return Expression::num_ids(); }
2091 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2094 Expression* expression_;
2098 class BinaryOperation FINAL : public Expression {
2100 DECLARE_NODE_TYPE(BinaryOperation)
2102 Token::Value op() const { return static_cast<Token::Value>(op_); }
2103 Expression* left() const { return left_; }
2104 Expression* right() const { return right_; }
2105 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2106 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2107 allocation_site_ = allocation_site;
2110 // The short-circuit logical operations need an AST ID for their
2111 // right-hand subexpression.
2112 static int num_ids() { return parent_num_ids() + 2; }
2113 BailoutId RightId() const { return BailoutId(local_id(0)); }
2115 TypeFeedbackId BinaryOperationFeedbackId() const {
2116 return TypeFeedbackId(local_id(1));
2118 Maybe<int> fixed_right_arg() const {
2119 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2121 void set_fixed_right_arg(Maybe<int> arg) {
2122 has_fixed_right_arg_ = arg.IsJust();
2123 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2126 virtual void RecordToBooleanTypeFeedback(
2127 TypeFeedbackOracle* oracle) OVERRIDE;
2130 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2131 Expression* right, int pos)
2132 : Expression(zone, pos),
2133 op_(static_cast<byte>(op)),
2134 has_fixed_right_arg_(false),
2135 fixed_right_arg_value_(0),
2138 DCHECK(Token::IsBinaryOp(op));
2140 static int parent_num_ids() { return Expression::num_ids(); }
2143 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2145 const byte op_; // actually Token::Value
2146 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2147 // type for the RHS. Currenty it's actually a Maybe<int>
2148 bool has_fixed_right_arg_;
2149 int fixed_right_arg_value_;
2152 Handle<AllocationSite> allocation_site_;
2156 class CountOperation FINAL : public Expression {
2158 DECLARE_NODE_TYPE(CountOperation)
2160 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2161 bool is_postfix() const { return !is_prefix(); }
2163 Token::Value op() const { return TokenField::decode(bit_field_); }
2164 Token::Value binary_op() {
2165 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2168 Expression* expression() const { return expression_; }
2170 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2171 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2172 IcCheckType GetKeyType() const OVERRIDE {
2173 return KeyTypeField::decode(bit_field_);
2175 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2176 return StoreModeField::decode(bit_field_);
2178 Type* type() const { return type_; }
2179 void set_key_type(IcCheckType type) {
2180 bit_field_ = KeyTypeField::update(bit_field_, type);
2182 void set_store_mode(KeyedAccessStoreMode mode) {
2183 bit_field_ = StoreModeField::update(bit_field_, mode);
2185 void set_type(Type* type) { type_ = type; }
2187 static int num_ids() { return parent_num_ids() + 4; }
2188 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2189 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2190 TypeFeedbackId CountBinOpFeedbackId() const {
2191 return TypeFeedbackId(local_id(2));
2193 TypeFeedbackId CountStoreFeedbackId() const {
2194 return TypeFeedbackId(local_id(3));
2198 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2200 : Expression(zone, pos),
2201 bit_field_(IsPrefixField::encode(is_prefix) |
2202 KeyTypeField::encode(ELEMENT) |
2203 StoreModeField::encode(STANDARD_STORE) |
2204 TokenField::encode(op)),
2206 expression_(expr) {}
2207 static int parent_num_ids() { return Expression::num_ids(); }
2210 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2212 class IsPrefixField : public BitField16<bool, 0, 1> {};
2213 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2214 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2215 class TokenField : public BitField16<Token::Value, 6, 8> {};
2217 // Starts with 16-bit field, which should get packed together with
2218 // Expression's trailing 16-bit field.
2219 uint16_t bit_field_;
2221 Expression* expression_;
2222 SmallMapList receiver_types_;
2226 class CompareOperation FINAL : public Expression {
2228 DECLARE_NODE_TYPE(CompareOperation)
2230 Token::Value op() const { return op_; }
2231 Expression* left() const { return left_; }
2232 Expression* right() const { return right_; }
2234 // Type feedback information.
2235 static int num_ids() { return parent_num_ids() + 1; }
2236 TypeFeedbackId CompareOperationFeedbackId() const {
2237 return TypeFeedbackId(local_id(0));
2239 Type* combined_type() const { return combined_type_; }
2240 void set_combined_type(Type* type) { combined_type_ = type; }
2242 // Match special cases.
2243 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2244 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2245 bool IsLiteralCompareNull(Expression** expr);
2248 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2249 Expression* right, int pos)
2250 : Expression(zone, pos),
2254 combined_type_(Type::None(zone)) {
2255 DCHECK(Token::IsCompareOp(op));
2257 static int parent_num_ids() { return Expression::num_ids(); }
2260 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2266 Type* combined_type_;
2270 class Spread FINAL : public Expression {
2272 DECLARE_NODE_TYPE(Spread)
2274 Expression* expression() const { return expression_; }
2276 static int num_ids() { return parent_num_ids(); }
2279 Spread(Zone* zone, Expression* expression, int pos)
2280 : Expression(zone, pos), expression_(expression) {}
2281 static int parent_num_ids() { return Expression::num_ids(); }
2284 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2286 Expression* expression_;
2290 class Conditional FINAL : public Expression {
2292 DECLARE_NODE_TYPE(Conditional)
2294 Expression* condition() const { return condition_; }
2295 Expression* then_expression() const { return then_expression_; }
2296 Expression* else_expression() const { return else_expression_; }
2298 static int num_ids() { return parent_num_ids() + 2; }
2299 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2300 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2303 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2304 Expression* else_expression, int position)
2305 : Expression(zone, position),
2306 condition_(condition),
2307 then_expression_(then_expression),
2308 else_expression_(else_expression) {}
2309 static int parent_num_ids() { return Expression::num_ids(); }
2312 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2314 Expression* condition_;
2315 Expression* then_expression_;
2316 Expression* else_expression_;
2320 class Assignment FINAL : public Expression {
2322 DECLARE_NODE_TYPE(Assignment)
2324 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2326 Token::Value binary_op() const;
2328 Token::Value op() const { return TokenField::decode(bit_field_); }
2329 Expression* target() const { return target_; }
2330 Expression* value() const { return value_; }
2331 BinaryOperation* binary_operation() const { return binary_operation_; }
2333 // This check relies on the definition order of token in token.h.
2334 bool is_compound() const { return op() > Token::ASSIGN; }
2336 static int num_ids() { return parent_num_ids() + 2; }
2337 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2339 // Type feedback information.
2340 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2341 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2342 bool IsUninitialized() const {
2343 return IsUninitializedField::decode(bit_field_);
2345 bool HasNoTypeInformation() {
2346 return IsUninitializedField::decode(bit_field_);
2348 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2349 IcCheckType GetKeyType() const OVERRIDE {
2350 return KeyTypeField::decode(bit_field_);
2352 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2353 return StoreModeField::decode(bit_field_);
2355 void set_is_uninitialized(bool b) {
2356 bit_field_ = IsUninitializedField::update(bit_field_, b);
2358 void set_key_type(IcCheckType key_type) {
2359 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2361 void set_store_mode(KeyedAccessStoreMode mode) {
2362 bit_field_ = StoreModeField::update(bit_field_, mode);
2366 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2368 static int parent_num_ids() { return Expression::num_ids(); }
2371 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2373 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2374 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2375 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2376 class TokenField : public BitField16<Token::Value, 6, 8> {};
2378 // Starts with 16-bit field, which should get packed together with
2379 // Expression's trailing 16-bit field.
2380 uint16_t bit_field_;
2381 Expression* target_;
2383 BinaryOperation* binary_operation_;
2384 SmallMapList receiver_types_;
2388 class Yield FINAL : public Expression {
2390 DECLARE_NODE_TYPE(Yield)
2393 kInitial, // The initial yield that returns the unboxed generator object.
2394 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2395 kDelegating, // A yield*.
2396 kFinal // A return: { value: EXPRESSION, done: true }
2399 Expression* generator_object() const { return generator_object_; }
2400 Expression* expression() const { return expression_; }
2401 Kind yield_kind() const { return yield_kind_; }
2403 // Delegating yield surrounds the "yield" in a "try/catch". This index
2404 // locates the catch handler in the handler table, and is equivalent to
2405 // TryCatchStatement::index().
2407 DCHECK_EQ(kDelegating, yield_kind());
2410 void set_index(int index) {
2411 DCHECK_EQ(kDelegating, yield_kind());
2415 // Type feedback information.
2416 bool HasFeedbackSlots() const {
2417 return FLAG_vector_ics && (yield_kind() == kDelegating);
2419 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2420 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2421 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2423 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2424 ICSlotCache* cache) OVERRIDE {
2425 yield_first_feedback_slot_ = slot;
2427 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2428 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2431 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2432 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2433 return yield_first_feedback_slot_;
2436 FeedbackVectorICSlot DoneFeedbackSlot() {
2437 return KeyedLoadFeedbackSlot().next();
2440 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2443 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2444 Kind yield_kind, int pos)
2445 : Expression(zone, pos),
2446 generator_object_(generator_object),
2447 expression_(expression),
2448 yield_kind_(yield_kind),
2450 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2453 Expression* generator_object_;
2454 Expression* expression_;
2457 FeedbackVectorICSlot yield_first_feedback_slot_;
2461 class Throw FINAL : public Expression {
2463 DECLARE_NODE_TYPE(Throw)
2465 Expression* exception() const { return exception_; }
2468 Throw(Zone* zone, Expression* exception, int pos)
2469 : Expression(zone, pos), exception_(exception) {}
2472 Expression* exception_;
2476 class FunctionLiteral FINAL : public Expression {
2479 ANONYMOUS_EXPRESSION,
2484 enum ParameterFlag {
2485 kNoDuplicateParameters = 0,
2486 kHasDuplicateParameters = 1
2489 enum IsFunctionFlag {
2494 enum IsParenthesizedFlag {
2499 enum ArityRestriction {
2505 DECLARE_NODE_TYPE(FunctionLiteral)
2507 Handle<String> name() const { return raw_name_->string(); }
2508 const AstRawString* raw_name() const { return raw_name_; }
2509 Scope* scope() const { return scope_; }
2510 ZoneList<Statement*>* body() const { return body_; }
2511 void set_function_token_position(int pos) { function_token_position_ = pos; }
2512 int function_token_position() const { return function_token_position_; }
2513 int start_position() const;
2514 int end_position() const;
2515 int SourceSize() const { return end_position() - start_position(); }
2516 bool is_expression() const { return IsExpression::decode(bitfield_); }
2517 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2518 LanguageMode language_mode() const;
2519 bool uses_super_property() const;
2521 static bool NeedsHomeObject(Expression* literal) {
2522 return literal != NULL && literal->IsFunctionLiteral() &&
2523 literal->AsFunctionLiteral()->uses_super_property();
2526 int materialized_literal_count() { return materialized_literal_count_; }
2527 int expected_property_count() { return expected_property_count_; }
2528 int handler_count() { return handler_count_; }
2529 int parameter_count() { return parameter_count_; }
2531 bool AllowsLazyCompilation();
2532 bool AllowsLazyCompilationWithoutContext();
2534 void InitializeSharedInfo(Handle<Code> code);
2536 Handle<String> debug_name() const {
2537 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2538 return raw_name_->string();
2540 return inferred_name();
2543 Handle<String> inferred_name() const {
2544 if (!inferred_name_.is_null()) {
2545 DCHECK(raw_inferred_name_ == NULL);
2546 return inferred_name_;
2548 if (raw_inferred_name_ != NULL) {
2549 return raw_inferred_name_->string();
2552 return Handle<String>();
2555 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2556 void set_inferred_name(Handle<String> inferred_name) {
2557 DCHECK(!inferred_name.is_null());
2558 inferred_name_ = inferred_name;
2559 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2560 raw_inferred_name_ = NULL;
2563 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2564 DCHECK(raw_inferred_name != NULL);
2565 raw_inferred_name_ = raw_inferred_name;
2566 DCHECK(inferred_name_.is_null());
2567 inferred_name_ = Handle<String>();
2570 // shared_info may be null if it's not cached in full code.
2571 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2573 bool pretenure() { return Pretenure::decode(bitfield_); }
2574 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2576 bool has_duplicate_parameters() {
2577 return HasDuplicateParameters::decode(bitfield_);
2580 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2582 // This is used as a heuristic on when to eagerly compile a function
2583 // literal. We consider the following constructs as hints that the
2584 // function will be called immediately:
2585 // - (function() { ... })();
2586 // - var x = function() { ... }();
2587 bool is_parenthesized() {
2588 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2590 void set_parenthesized() {
2591 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2594 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2596 int ast_node_count() { return ast_properties_.node_count(); }
2597 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2598 void set_ast_properties(AstProperties* ast_properties) {
2599 ast_properties_ = *ast_properties;
2601 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2602 return ast_properties_.get_spec();
2604 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2605 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2606 void set_dont_optimize_reason(BailoutReason reason) {
2607 dont_optimize_reason_ = reason;
2611 FunctionLiteral(Zone* zone, const AstRawString* name,
2612 AstValueFactory* ast_value_factory, Scope* scope,
2613 ZoneList<Statement*>* body, int materialized_literal_count,
2614 int expected_property_count, int handler_count,
2615 int parameter_count, FunctionType function_type,
2616 ParameterFlag has_duplicate_parameters,
2617 IsFunctionFlag is_function,
2618 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2620 : Expression(zone, position),
2624 raw_inferred_name_(ast_value_factory->empty_string()),
2625 ast_properties_(zone),
2626 dont_optimize_reason_(kNoReason),
2627 materialized_literal_count_(materialized_literal_count),
2628 expected_property_count_(expected_property_count),
2629 handler_count_(handler_count),
2630 parameter_count_(parameter_count),
2631 function_token_position_(RelocInfo::kNoPosition) {
2632 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2633 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2634 Pretenure::encode(false) |
2635 HasDuplicateParameters::encode(has_duplicate_parameters) |
2636 IsFunction::encode(is_function) |
2637 IsParenthesized::encode(is_parenthesized) |
2638 FunctionKindBits::encode(kind);
2639 DCHECK(IsValidFunctionKind(kind));
2643 const AstRawString* raw_name_;
2644 Handle<String> name_;
2645 Handle<SharedFunctionInfo> shared_info_;
2647 ZoneList<Statement*>* body_;
2648 const AstString* raw_inferred_name_;
2649 Handle<String> inferred_name_;
2650 AstProperties ast_properties_;
2651 BailoutReason dont_optimize_reason_;
2653 int materialized_literal_count_;
2654 int expected_property_count_;
2656 int parameter_count_;
2657 int function_token_position_;
2660 class IsExpression : public BitField<bool, 0, 1> {};
2661 class IsAnonymous : public BitField<bool, 1, 1> {};
2662 class Pretenure : public BitField<bool, 2, 1> {};
2663 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2664 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2665 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2666 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2670 class ClassLiteral FINAL : public Expression {
2672 typedef ObjectLiteralProperty Property;
2674 DECLARE_NODE_TYPE(ClassLiteral)
2676 Handle<String> name() const { return raw_name_->string(); }
2677 const AstRawString* raw_name() const { return raw_name_; }
2678 Scope* scope() const { return scope_; }
2679 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2680 Expression* extends() const { return extends_; }
2681 FunctionLiteral* constructor() const { return constructor_; }
2682 ZoneList<Property*>* properties() const { return properties_; }
2683 int start_position() const { return position(); }
2684 int end_position() const { return end_position_; }
2686 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2687 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2688 BailoutId ExitId() { return BailoutId(local_id(2)); }
2690 // Return an AST id for a property that is used in simulate instructions.
2691 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2693 // Unlike other AST nodes, this number of bailout IDs allocated for an
2694 // ClassLiteral can vary, so num_ids() is not a static method.
2695 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2698 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2699 VariableProxy* class_variable_proxy, Expression* extends,
2700 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2701 int start_position, int end_position)
2702 : Expression(zone, start_position),
2705 class_variable_proxy_(class_variable_proxy),
2707 constructor_(constructor),
2708 properties_(properties),
2709 end_position_(end_position) {}
2710 static int parent_num_ids() { return Expression::num_ids(); }
2713 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2715 const AstRawString* raw_name_;
2717 VariableProxy* class_variable_proxy_;
2718 Expression* extends_;
2719 FunctionLiteral* constructor_;
2720 ZoneList<Property*>* properties_;
2725 class NativeFunctionLiteral FINAL : public Expression {
2727 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2729 Handle<String> name() const { return name_->string(); }
2730 v8::Extension* extension() const { return extension_; }
2733 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2734 v8::Extension* extension, int pos)
2735 : Expression(zone, pos), name_(name), extension_(extension) {}
2738 const AstRawString* name_;
2739 v8::Extension* extension_;
2743 class ThisFunction FINAL : public Expression {
2745 DECLARE_NODE_TYPE(ThisFunction)
2748 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2752 class SuperReference FINAL : public Expression {
2754 DECLARE_NODE_TYPE(SuperReference)
2756 VariableProxy* this_var() const { return this_var_; }
2758 static int num_ids() { return parent_num_ids() + 1; }
2759 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2761 // Type feedback information.
2762 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2763 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2764 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2766 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2767 ICSlotCache* cache) OVERRIDE {
2768 homeobject_feedback_slot_ = slot;
2770 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2772 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2773 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2774 return homeobject_feedback_slot_;
2778 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2779 : Expression(zone, pos),
2780 this_var_(this_var),
2781 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2782 DCHECK(this_var->is_this());
2784 static int parent_num_ids() { return Expression::num_ids(); }
2787 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2789 VariableProxy* this_var_;
2790 FeedbackVectorICSlot homeobject_feedback_slot_;
2794 #undef DECLARE_NODE_TYPE
2797 // ----------------------------------------------------------------------------
2798 // Regular expressions
2801 class RegExpVisitor BASE_EMBEDDED {
2803 virtual ~RegExpVisitor() { }
2804 #define MAKE_CASE(Name) \
2805 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2806 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2811 class RegExpTree : public ZoneObject {
2813 static const int kInfinity = kMaxInt;
2814 virtual ~RegExpTree() {}
2815 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2816 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2817 RegExpNode* on_success) = 0;
2818 virtual bool IsTextElement() { return false; }
2819 virtual bool IsAnchoredAtStart() { return false; }
2820 virtual bool IsAnchoredAtEnd() { return false; }
2821 virtual int min_match() = 0;
2822 virtual int max_match() = 0;
2823 // Returns the interval of registers used for captures within this
2825 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2826 virtual void AppendToText(RegExpText* text, Zone* zone);
2827 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2828 #define MAKE_ASTYPE(Name) \
2829 virtual RegExp##Name* As##Name(); \
2830 virtual bool Is##Name();
2831 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2836 class RegExpDisjunction FINAL : public RegExpTree {
2838 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2839 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2840 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2841 RegExpNode* on_success) OVERRIDE;
2842 RegExpDisjunction* AsDisjunction() OVERRIDE;
2843 Interval CaptureRegisters() OVERRIDE;
2844 bool IsDisjunction() OVERRIDE;
2845 bool IsAnchoredAtStart() OVERRIDE;
2846 bool IsAnchoredAtEnd() OVERRIDE;
2847 int min_match() OVERRIDE { return min_match_; }
2848 int max_match() OVERRIDE { return max_match_; }
2849 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2851 ZoneList<RegExpTree*>* alternatives_;
2857 class RegExpAlternative FINAL : public RegExpTree {
2859 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2860 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2861 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2862 RegExpNode* on_success) OVERRIDE;
2863 RegExpAlternative* AsAlternative() OVERRIDE;
2864 Interval CaptureRegisters() OVERRIDE;
2865 bool IsAlternative() OVERRIDE;
2866 bool IsAnchoredAtStart() OVERRIDE;
2867 bool IsAnchoredAtEnd() OVERRIDE;
2868 int min_match() OVERRIDE { return min_match_; }
2869 int max_match() OVERRIDE { return max_match_; }
2870 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2872 ZoneList<RegExpTree*>* nodes_;
2878 class RegExpAssertion FINAL : public RegExpTree {
2880 enum AssertionType {
2888 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2889 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2890 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2891 RegExpNode* on_success) OVERRIDE;
2892 RegExpAssertion* AsAssertion() OVERRIDE;
2893 bool IsAssertion() OVERRIDE;
2894 bool IsAnchoredAtStart() OVERRIDE;
2895 bool IsAnchoredAtEnd() OVERRIDE;
2896 int min_match() OVERRIDE { return 0; }
2897 int max_match() OVERRIDE { return 0; }
2898 AssertionType assertion_type() { return assertion_type_; }
2900 AssertionType assertion_type_;
2904 class CharacterSet FINAL BASE_EMBEDDED {
2906 explicit CharacterSet(uc16 standard_set_type)
2908 standard_set_type_(standard_set_type) {}
2909 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2911 standard_set_type_(0) {}
2912 ZoneList<CharacterRange>* ranges(Zone* zone);
2913 uc16 standard_set_type() { return standard_set_type_; }
2914 void set_standard_set_type(uc16 special_set_type) {
2915 standard_set_type_ = special_set_type;
2917 bool is_standard() { return standard_set_type_ != 0; }
2918 void Canonicalize();
2920 ZoneList<CharacterRange>* ranges_;
2921 // If non-zero, the value represents a standard set (e.g., all whitespace
2922 // characters) without having to expand the ranges.
2923 uc16 standard_set_type_;
2927 class RegExpCharacterClass FINAL : public RegExpTree {
2929 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2931 is_negated_(is_negated) { }
2932 explicit RegExpCharacterClass(uc16 type)
2934 is_negated_(false) { }
2935 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2936 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2937 RegExpNode* on_success) OVERRIDE;
2938 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2939 bool IsCharacterClass() OVERRIDE;
2940 bool IsTextElement() OVERRIDE { return true; }
2941 int min_match() OVERRIDE { return 1; }
2942 int max_match() OVERRIDE { return 1; }
2943 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2944 CharacterSet character_set() { return set_; }
2945 // TODO(lrn): Remove need for complex version if is_standard that
2946 // recognizes a mangled standard set and just do { return set_.is_special(); }
2947 bool is_standard(Zone* zone);
2948 // Returns a value representing the standard character set if is_standard()
2950 // Currently used values are:
2951 // s : unicode whitespace
2952 // S : unicode non-whitespace
2953 // w : ASCII word character (digit, letter, underscore)
2954 // W : non-ASCII word character
2956 // D : non-ASCII digit
2957 // . : non-unicode non-newline
2958 // * : All characters
2959 uc16 standard_type() { return set_.standard_set_type(); }
2960 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2961 bool is_negated() { return is_negated_; }
2969 class RegExpAtom FINAL : public RegExpTree {
2971 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2972 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2973 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2974 RegExpNode* on_success) OVERRIDE;
2975 RegExpAtom* AsAtom() OVERRIDE;
2976 bool IsAtom() OVERRIDE;
2977 bool IsTextElement() OVERRIDE { return true; }
2978 int min_match() OVERRIDE { return data_.length(); }
2979 int max_match() OVERRIDE { return data_.length(); }
2980 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2981 Vector<const uc16> data() { return data_; }
2982 int length() { return data_.length(); }
2984 Vector<const uc16> data_;
2988 class RegExpText FINAL : public RegExpTree {
2990 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2991 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2992 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2993 RegExpNode* on_success) OVERRIDE;
2994 RegExpText* AsText() OVERRIDE;
2995 bool IsText() OVERRIDE;
2996 bool IsTextElement() OVERRIDE { return true; }
2997 int min_match() OVERRIDE { return length_; }
2998 int max_match() OVERRIDE { return length_; }
2999 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
3000 void AddElement(TextElement elm, Zone* zone) {
3001 elements_.Add(elm, zone);
3002 length_ += elm.length();
3004 ZoneList<TextElement>* elements() { return &elements_; }
3006 ZoneList<TextElement> elements_;
3011 class RegExpQuantifier FINAL : public RegExpTree {
3013 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3014 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3018 min_match_(min * body->min_match()),
3019 quantifier_type_(type) {
3020 if (max > 0 && body->max_match() > kInfinity / max) {
3021 max_match_ = kInfinity;
3023 max_match_ = max * body->max_match();
3026 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3027 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3028 RegExpNode* on_success) OVERRIDE;
3029 static RegExpNode* ToNode(int min,
3033 RegExpCompiler* compiler,
3034 RegExpNode* on_success,
3035 bool not_at_start = false);
3036 RegExpQuantifier* AsQuantifier() OVERRIDE;
3037 Interval CaptureRegisters() OVERRIDE;
3038 bool IsQuantifier() OVERRIDE;
3039 int min_match() OVERRIDE { return min_match_; }
3040 int max_match() OVERRIDE { return max_match_; }
3041 int min() { return min_; }
3042 int max() { return max_; }
3043 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3044 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3045 bool is_greedy() { return quantifier_type_ == GREEDY; }
3046 RegExpTree* body() { return body_; }
3054 QuantifierType quantifier_type_;
3058 class RegExpCapture FINAL : public RegExpTree {
3060 explicit RegExpCapture(RegExpTree* body, int index)
3061 : body_(body), index_(index) { }
3062 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3063 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3064 RegExpNode* on_success) OVERRIDE;
3065 static RegExpNode* ToNode(RegExpTree* body,
3067 RegExpCompiler* compiler,
3068 RegExpNode* on_success);
3069 RegExpCapture* AsCapture() OVERRIDE;
3070 bool IsAnchoredAtStart() OVERRIDE;
3071 bool IsAnchoredAtEnd() OVERRIDE;
3072 Interval CaptureRegisters() OVERRIDE;
3073 bool IsCapture() OVERRIDE;
3074 int min_match() OVERRIDE { return body_->min_match(); }
3075 int max_match() OVERRIDE { return body_->max_match(); }
3076 RegExpTree* body() { return body_; }
3077 int index() { return index_; }
3078 static int StartRegister(int index) { return index * 2; }
3079 static int EndRegister(int index) { return index * 2 + 1; }
3087 class RegExpLookahead FINAL : public RegExpTree {
3089 RegExpLookahead(RegExpTree* body,
3094 is_positive_(is_positive),
3095 capture_count_(capture_count),
3096 capture_from_(capture_from) { }
3098 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3099 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3100 RegExpNode* on_success) OVERRIDE;
3101 RegExpLookahead* AsLookahead() OVERRIDE;
3102 Interval CaptureRegisters() OVERRIDE;
3103 bool IsLookahead() OVERRIDE;
3104 bool IsAnchoredAtStart() OVERRIDE;
3105 int min_match() OVERRIDE { return 0; }
3106 int max_match() OVERRIDE { return 0; }
3107 RegExpTree* body() { return body_; }
3108 bool is_positive() { return is_positive_; }
3109 int capture_count() { return capture_count_; }
3110 int capture_from() { return capture_from_; }
3120 class RegExpBackReference FINAL : public RegExpTree {
3122 explicit RegExpBackReference(RegExpCapture* capture)
3123 : capture_(capture) { }
3124 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3125 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3126 RegExpNode* on_success) OVERRIDE;
3127 RegExpBackReference* AsBackReference() OVERRIDE;
3128 bool IsBackReference() OVERRIDE;
3129 int min_match() OVERRIDE { return 0; }
3130 int max_match() OVERRIDE { return capture_->max_match(); }
3131 int index() { return capture_->index(); }
3132 RegExpCapture* capture() { return capture_; }
3134 RegExpCapture* capture_;
3138 class RegExpEmpty FINAL : public RegExpTree {
3141 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3142 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3143 RegExpNode* on_success) OVERRIDE;
3144 RegExpEmpty* AsEmpty() OVERRIDE;
3145 bool IsEmpty() OVERRIDE;
3146 int min_match() OVERRIDE { return 0; }
3147 int max_match() OVERRIDE { return 0; }
3151 // ----------------------------------------------------------------------------
3153 // - leaf node visitors are abstract.
3155 class AstVisitor BASE_EMBEDDED {
3158 virtual ~AstVisitor() {}
3160 // Stack overflow check and dynamic dispatch.
3161 virtual void Visit(AstNode* node) = 0;
3163 // Iteration left-to-right.
3164 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3165 virtual void VisitStatements(ZoneList<Statement*>* statements);
3166 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3168 // Individual AST nodes.
3169 #define DEF_VISIT(type) \
3170 virtual void Visit##type(type* node) = 0;
3171 AST_NODE_LIST(DEF_VISIT)
3176 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3178 void Visit(AstNode* node) FINAL { \
3179 if (!CheckStackOverflow()) node->Accept(this); \
3182 void SetStackOverflow() { stack_overflow_ = true; } \
3183 void ClearStackOverflow() { stack_overflow_ = false; } \
3184 bool HasStackOverflow() const { return stack_overflow_; } \
3186 bool CheckStackOverflow() { \
3187 if (stack_overflow_) return true; \
3188 StackLimitCheck check(isolate_); \
3189 if (!check.HasOverflowed()) return false; \
3190 stack_overflow_ = true; \
3195 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3196 isolate_ = isolate; \
3198 stack_overflow_ = false; \
3200 Zone* zone() { return zone_; } \
3201 Isolate* isolate() { return isolate_; } \
3203 Isolate* isolate_; \
3205 bool stack_overflow_
3208 // ----------------------------------------------------------------------------
3211 class AstNodeFactory FINAL BASE_EMBEDDED {
3213 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3214 : zone_(ast_value_factory->zone()),
3215 ast_value_factory_(ast_value_factory) {}
3217 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3221 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3224 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3226 FunctionLiteral* fun,
3229 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3232 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3236 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3239 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3240 const AstRawString* import_name,
3241 const AstRawString* module_specifier,
3242 Scope* scope, int pos) {
3243 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3244 module_specifier, scope, pos);
3247 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3250 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3253 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3255 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3258 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3259 return new (zone_) ModulePath(zone_, origin, name, pos);
3262 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3263 return new (zone_) ModuleUrl(zone_, url, pos);
3266 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3268 bool is_initializer_block,
3271 Block(zone_, labels, capacity, is_initializer_block, pos);
3274 #define STATEMENT_WITH_LABELS(NodeType) \
3275 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3276 return new (zone_) NodeType(zone_, labels, pos); \
3278 STATEMENT_WITH_LABELS(DoWhileStatement)
3279 STATEMENT_WITH_LABELS(WhileStatement)
3280 STATEMENT_WITH_LABELS(ForStatement)
3281 STATEMENT_WITH_LABELS(SwitchStatement)
3282 #undef STATEMENT_WITH_LABELS
3284 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3285 ZoneList<const AstRawString*>* labels,
3287 switch (visit_mode) {
3288 case ForEachStatement::ENUMERATE: {
3289 return new (zone_) ForInStatement(zone_, labels, pos);
3291 case ForEachStatement::ITERATE: {
3292 return new (zone_) ForOfStatement(zone_, labels, pos);
3299 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3300 return new (zone_) ModuleStatement(zone_, body, pos);
3303 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3304 return new (zone_) ExpressionStatement(zone_, expression, pos);
3307 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3308 return new (zone_) ContinueStatement(zone_, target, pos);
3311 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3312 return new (zone_) BreakStatement(zone_, target, pos);
3315 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3316 return new (zone_) ReturnStatement(zone_, expression, pos);
3319 WithStatement* NewWithStatement(Scope* scope,
3320 Expression* expression,
3321 Statement* statement,
3323 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3326 IfStatement* NewIfStatement(Expression* condition,
3327 Statement* then_statement,
3328 Statement* else_statement,
3331 IfStatement(zone_, condition, then_statement, else_statement, pos);
3334 TryCatchStatement* NewTryCatchStatement(int index,
3340 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3341 variable, catch_block, pos);
3344 TryFinallyStatement* NewTryFinallyStatement(int index,
3346 Block* finally_block,
3349 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3352 DebuggerStatement* NewDebuggerStatement(int pos) {
3353 return new (zone_) DebuggerStatement(zone_, pos);
3356 EmptyStatement* NewEmptyStatement(int pos) {
3357 return new(zone_) EmptyStatement(zone_, pos);
3360 CaseClause* NewCaseClause(
3361 Expression* label, ZoneList<Statement*>* statements, int pos) {
3362 return new (zone_) CaseClause(zone_, label, statements, pos);
3365 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3367 Literal(zone_, ast_value_factory_->NewString(string), pos);
3370 // A JavaScript symbol (ECMA-262 edition 6).
3371 Literal* NewSymbolLiteral(const char* name, int pos) {
3372 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3375 Literal* NewNumberLiteral(double number, int pos) {
3377 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3380 Literal* NewSmiLiteral(int number, int pos) {
3381 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3384 Literal* NewBooleanLiteral(bool b, int pos) {
3385 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3388 Literal* NewNullLiteral(int pos) {
3389 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3392 Literal* NewUndefinedLiteral(int pos) {
3393 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3396 Literal* NewTheHoleLiteral(int pos) {
3397 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3400 ObjectLiteral* NewObjectLiteral(
3401 ZoneList<ObjectLiteral::Property*>* properties,
3403 int boilerplate_properties,
3406 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3407 boilerplate_properties, has_function, pos);
3410 ObjectLiteral::Property* NewObjectLiteralProperty(
3411 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3412 bool is_static, bool is_computed_name) {
3414 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3417 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3420 bool is_computed_name) {
3421 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3422 is_static, is_computed_name);
3425 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3426 const AstRawString* flags,
3429 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3432 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3435 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3438 VariableProxy* NewVariableProxy(Variable* var,
3439 int start_position = RelocInfo::kNoPosition,
3440 int end_position = RelocInfo::kNoPosition) {
3441 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3444 VariableProxy* NewVariableProxy(const AstRawString* name,
3445 Variable::Kind variable_kind,
3446 int start_position = RelocInfo::kNoPosition,
3447 int end_position = RelocInfo::kNoPosition) {
3449 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3452 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3453 return new (zone_) Property(zone_, obj, key, pos);
3456 Call* NewCall(Expression* expression,
3457 ZoneList<Expression*>* arguments,
3459 return new (zone_) Call(zone_, expression, arguments, pos);
3462 CallNew* NewCallNew(Expression* expression,
3463 ZoneList<Expression*>* arguments,
3465 return new (zone_) CallNew(zone_, expression, arguments, pos);
3468 CallRuntime* NewCallRuntime(const AstRawString* name,
3469 const Runtime::Function* function,
3470 ZoneList<Expression*>* arguments,
3472 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3475 UnaryOperation* NewUnaryOperation(Token::Value op,
3476 Expression* expression,
3478 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3481 BinaryOperation* NewBinaryOperation(Token::Value op,
3485 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3488 CountOperation* NewCountOperation(Token::Value op,
3492 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3495 CompareOperation* NewCompareOperation(Token::Value op,
3499 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3502 Spread* NewSpread(Expression* expression, int pos) {
3503 return new (zone_) Spread(zone_, expression, pos);
3506 Conditional* NewConditional(Expression* condition,
3507 Expression* then_expression,
3508 Expression* else_expression,
3510 return new (zone_) Conditional(zone_, condition, then_expression,
3511 else_expression, position);
3514 Assignment* NewAssignment(Token::Value op,
3518 DCHECK(Token::IsAssignmentOp(op));
3519 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3520 if (assign->is_compound()) {
3521 DCHECK(Token::IsAssignmentOp(op));
3522 assign->binary_operation_ =
3523 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3528 Yield* NewYield(Expression *generator_object,
3529 Expression* expression,
3530 Yield::Kind yield_kind,
3532 if (!expression) expression = NewUndefinedLiteral(pos);
3534 Yield(zone_, generator_object, expression, yield_kind, pos);
3537 Throw* NewThrow(Expression* exception, int pos) {
3538 return new (zone_) Throw(zone_, exception, pos);
3541 FunctionLiteral* NewFunctionLiteral(
3542 const AstRawString* name, AstValueFactory* ast_value_factory,
3543 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3544 int expected_property_count, int handler_count, int parameter_count,
3545 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3546 FunctionLiteral::FunctionType function_type,
3547 FunctionLiteral::IsFunctionFlag is_function,
3548 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3550 return new (zone_) FunctionLiteral(
3551 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3552 expected_property_count, handler_count, parameter_count, function_type,
3553 has_duplicate_parameters, is_function, is_parenthesized, kind,
3557 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3558 VariableProxy* proxy, Expression* extends,
3559 FunctionLiteral* constructor,
3560 ZoneList<ObjectLiteral::Property*>* properties,
3561 int start_position, int end_position) {
3563 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3564 properties, start_position, end_position);
3567 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3568 v8::Extension* extension,
3570 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3573 ThisFunction* NewThisFunction(int pos) {
3574 return new (zone_) ThisFunction(zone_, pos);
3577 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3578 return new (zone_) SuperReference(zone_, this_var, pos);
3583 AstValueFactory* ast_value_factory_;
3587 } } // namespace v8::internal