1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/assembler.h"
11 #include "src/ast-value-factory.h"
12 #include "src/bailout-reason.h"
13 #include "src/factory.h"
14 #include "src/isolate.h"
15 #include "src/jsregexp.h"
16 #include "src/list-inl.h"
17 #include "src/modules.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/smart-pointers.h"
21 #include "src/token.h"
22 #include "src/types.h"
23 #include "src/utils.h"
24 #include "src/variables.h"
29 // The abstract syntax tree is an intermediate, light-weight
30 // representation of the parsed JavaScript code suitable for
31 // compilation to native code.
33 // Nodes are allocated in a separate zone, which allows faster
34 // allocation and constant-time deallocation of the entire syntax
38 // ----------------------------------------------------------------------------
39 // Nodes of the abstract syntax tree. Only concrete classes are
42 #define DECLARATION_NODE_LIST(V) \
43 V(VariableDeclaration) \
44 V(FunctionDeclaration) \
45 V(ModuleDeclaration) \
46 V(ImportDeclaration) \
49 #define MODULE_NODE_LIST(V) \
54 #define STATEMENT_NODE_LIST(V) \
57 V(ExpressionStatement) \
60 V(ContinueStatement) \
70 V(TryCatchStatement) \
71 V(TryFinallyStatement) \
74 #define EXPRESSION_NODE_LIST(V) \
77 V(NativeFunctionLiteral) \
100 #define AST_NODE_LIST(V) \
101 DECLARATION_NODE_LIST(V) \
102 MODULE_NODE_LIST(V) \
103 STATEMENT_NODE_LIST(V) \
104 EXPRESSION_NODE_LIST(V)
106 // Forward declarations
107 class AstNodeFactory;
111 class BreakableStatement;
113 class IterationStatement;
114 class MaterializedLiteral;
116 class TypeFeedbackOracle;
118 class RegExpAlternative;
119 class RegExpAssertion;
121 class RegExpBackReference;
123 class RegExpCharacterClass;
124 class RegExpCompiler;
125 class RegExpDisjunction;
127 class RegExpLookahead;
128 class RegExpQuantifier;
131 #define DEF_FORWARD_DECLARATION(type) class type;
132 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
133 #undef DEF_FORWARD_DECLARATION
136 // Typedef only introduced to avoid unreadable code.
137 // Please do appreciate the required space in "> >".
138 typedef ZoneList<Handle<String> > ZoneStringList;
139 typedef ZoneList<Handle<Object> > ZoneObjectList;
142 #define DECLARE_NODE_TYPE(type) \
143 void Accept(AstVisitor* v) OVERRIDE; \
144 AstNode::NodeType node_type() const FINAL { return AstNode::k##type; } \
145 friend class AstNodeFactory;
148 enum AstPropertiesFlag {
155 class FeedbackVectorRequirements {
157 FeedbackVectorRequirements(int slots, int ic_slots)
158 : slots_(slots), ic_slots_(ic_slots) {}
160 int slots() const { return slots_; }
161 int ic_slots() const { return ic_slots_; }
169 class VariableICSlotPair FINAL {
171 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
172 : variable_(variable), slot_(slot) {}
174 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
176 Variable* variable() const { return variable_; }
177 FeedbackVectorICSlot slot() const { return slot_; }
181 FeedbackVectorICSlot slot_;
185 typedef List<VariableICSlotPair> ICSlotCache;
188 class AstProperties FINAL BASE_EMBEDDED {
190 class Flags : public EnumSet<AstPropertiesFlag, int> {};
192 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
194 Flags* flags() { return &flags_; }
195 int node_count() { return node_count_; }
196 void add_node_count(int count) { node_count_ += count; }
198 int slots() const { return spec_.slots(); }
199 void increase_slots(int count) { spec_.increase_slots(count); }
201 int ic_slots() const { return spec_.ic_slots(); }
202 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
203 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
204 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
209 ZoneFeedbackVectorSpec spec_;
213 class AstNode: public ZoneObject {
215 #define DECLARE_TYPE_ENUM(type) k##type,
217 AST_NODE_LIST(DECLARE_TYPE_ENUM)
220 #undef DECLARE_TYPE_ENUM
222 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
224 explicit AstNode(int position): position_(position) {}
225 virtual ~AstNode() {}
227 virtual void Accept(AstVisitor* v) = 0;
228 virtual NodeType node_type() const = 0;
229 int position() const { return position_; }
231 // Type testing & conversion functions overridden by concrete subclasses.
232 #define DECLARE_NODE_FUNCTIONS(type) \
233 bool Is##type() const { return node_type() == AstNode::k##type; } \
235 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
237 const type* As##type() const { \
238 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
240 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
241 #undef DECLARE_NODE_FUNCTIONS
243 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
244 virtual IterationStatement* AsIterationStatement() { return NULL; }
245 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
247 // The interface for feedback slots, with default no-op implementations for
248 // node types which don't actually have this. Note that this is conceptually
249 // not really nice, but multiple inheritance would introduce yet another
250 // vtable entry per node, something we don't want for space reasons.
251 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
252 Isolate* isolate, const ICSlotCache* cache) {
253 return FeedbackVectorRequirements(0, 0);
255 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
256 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
257 ICSlotCache* cache) {
260 // Each ICSlot stores a kind of IC which the participating node should know.
261 virtual Code::Kind FeedbackICSlotKind(int index) {
263 return Code::NUMBER_OF_KINDS;
267 // Hidden to prevent accidental usage. It would have to load the
268 // current zone from the TLS.
269 void* operator new(size_t size);
271 friend class CaseClause; // Generates AST IDs.
277 class Statement : public AstNode {
279 explicit Statement(Zone* zone, int position) : AstNode(position) {}
281 bool IsEmpty() { return AsEmptyStatement() != NULL; }
282 virtual bool IsJump() const { return false; }
286 class SmallMapList FINAL {
289 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
291 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
292 void Clear() { list_.Clear(); }
293 void Sort() { list_.Sort(); }
295 bool is_empty() const { return list_.is_empty(); }
296 int length() const { return list_.length(); }
298 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
299 if (!Map::TryUpdate(map).ToHandle(&map)) return;
300 for (int i = 0; i < length(); ++i) {
301 if (at(i).is_identical_to(map)) return;
306 void FilterForPossibleTransitions(Map* root_map) {
307 for (int i = list_.length() - 1; i >= 0; i--) {
308 if (at(i)->FindRootMap() != root_map) {
309 list_.RemoveElement(list_.at(i));
314 void Add(Handle<Map> handle, Zone* zone) {
315 list_.Add(handle.location(), zone);
318 Handle<Map> at(int i) const {
319 return Handle<Map>(list_.at(i));
322 Handle<Map> first() const { return at(0); }
323 Handle<Map> last() const { return at(length() - 1); }
326 // The list stores pointers to Map*, that is Map**, so it's GC safe.
327 SmallPointerList<Map*> list_;
329 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
333 class Expression : public AstNode {
336 // Not assigned a context yet, or else will not be visited during
339 // Evaluated for its side effects.
341 // Evaluated for its value (and side effects).
343 // Evaluated for control flow (and side effects).
347 virtual bool IsValidReferenceExpression() const { return false; }
349 // Helpers for ToBoolean conversion.
350 virtual bool ToBooleanIsTrue() const { return false; }
351 virtual bool ToBooleanIsFalse() const { return false; }
353 // Symbols that cannot be parsed as array indices are considered property
354 // names. We do not treat symbols that can be array indexes as property
355 // names because [] for string objects is handled only by keyed ICs.
356 virtual bool IsPropertyName() const { return false; }
358 // True iff the expression is a literal represented as a smi.
359 bool IsSmiLiteral() const;
361 // True iff the expression is a string literal.
362 bool IsStringLiteral() const;
364 // True iff the expression is the null literal.
365 bool IsNullLiteral() const;
367 // True if we can prove that the expression is the undefined literal.
368 bool IsUndefinedLiteral(Isolate* isolate) const;
370 // Expression type bounds
371 Bounds bounds() const { return bounds_; }
372 void set_bounds(Bounds bounds) { bounds_ = bounds; }
374 // Whether the expression is parenthesized
375 bool is_parenthesized() const {
376 return IsParenthesizedField::decode(bit_field_);
378 bool is_multi_parenthesized() const {
379 return IsMultiParenthesizedField::decode(bit_field_);
381 void increase_parenthesization_level() {
383 IsMultiParenthesizedField::update(bit_field_, is_parenthesized());
384 bit_field_ = IsParenthesizedField::update(bit_field_, true);
387 // Type feedback information for assignments and properties.
388 virtual bool IsMonomorphic() {
392 virtual SmallMapList* GetReceiverTypes() {
396 virtual KeyedAccessStoreMode GetStoreMode() const {
398 return STANDARD_STORE;
400 virtual IcCheckType GetKeyType() const {
405 // TODO(rossberg): this should move to its own AST node eventually.
406 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
407 byte to_boolean_types() const {
408 return ToBooleanTypesField::decode(bit_field_);
411 void set_base_id(int id) { base_id_ = id; }
412 static int num_ids() { return parent_num_ids() + 2; }
413 BailoutId id() const { return BailoutId(local_id(0)); }
414 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
417 Expression(Zone* zone, int pos)
419 base_id_(BailoutId::None().ToInt()),
420 bounds_(Bounds::Unbounded(zone)),
422 static int parent_num_ids() { return 0; }
423 void set_to_boolean_types(byte types) {
424 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
427 int base_id() const {
428 DCHECK(!BailoutId(base_id_).IsNone());
433 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
437 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
438 class IsParenthesizedField : public BitField16<bool, 8, 1> {};
439 class IsMultiParenthesizedField : public BitField16<bool, 9, 1> {};
441 // Ends with 16-bit field; deriving classes in turn begin with
442 // 16-bit fields for optimum packing efficiency.
446 class BreakableStatement : public Statement {
449 TARGET_FOR_ANONYMOUS,
450 TARGET_FOR_NAMED_ONLY
453 // The labels associated with this statement. May be NULL;
454 // if it is != NULL, guaranteed to contain at least one entry.
455 ZoneList<const AstRawString*>* labels() const { return labels_; }
457 // Type testing & conversion.
458 BreakableStatement* AsBreakableStatement() FINAL { return this; }
461 Label* break_target() { return &break_target_; }
464 bool is_target_for_anonymous() const {
465 return breakable_type_ == TARGET_FOR_ANONYMOUS;
468 void set_base_id(int id) { base_id_ = id; }
469 static int num_ids() { return parent_num_ids() + 2; }
470 BailoutId EntryId() const { return BailoutId(local_id(0)); }
471 BailoutId ExitId() const { return BailoutId(local_id(1)); }
474 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
475 BreakableType breakable_type, int position)
476 : Statement(zone, position),
478 breakable_type_(breakable_type),
479 base_id_(BailoutId::None().ToInt()) {
480 DCHECK(labels == NULL || labels->length() > 0);
482 static int parent_num_ids() { return 0; }
484 int base_id() const {
485 DCHECK(!BailoutId(base_id_).IsNone());
490 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
492 ZoneList<const AstRawString*>* labels_;
493 BreakableType breakable_type_;
499 class Block FINAL : public BreakableStatement {
501 DECLARE_NODE_TYPE(Block)
503 void AddStatement(Statement* statement, Zone* zone) {
504 statements_.Add(statement, zone);
507 ZoneList<Statement*>* statements() { return &statements_; }
508 bool is_initializer_block() const { return is_initializer_block_; }
510 static int num_ids() { return parent_num_ids() + 1; }
511 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
513 bool IsJump() const OVERRIDE {
514 return !statements_.is_empty() && statements_.last()->IsJump()
515 && labels() == NULL; // Good enough as an approximation...
518 Scope* scope() const { return scope_; }
519 void set_scope(Scope* scope) { scope_ = scope; }
522 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
523 bool is_initializer_block, int pos)
524 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
525 statements_(capacity, zone),
526 is_initializer_block_(is_initializer_block),
528 static int parent_num_ids() { return BreakableStatement::num_ids(); }
531 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
533 ZoneList<Statement*> statements_;
534 bool is_initializer_block_;
539 class Declaration : public AstNode {
541 VariableProxy* proxy() const { return proxy_; }
542 VariableMode mode() const { return mode_; }
543 Scope* scope() const { return scope_; }
544 virtual InitializationFlag initialization() const = 0;
545 virtual bool IsInlineable() const;
548 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
550 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
551 DCHECK(IsDeclaredVariableMode(mode));
556 VariableProxy* proxy_;
558 // Nested scope from which the declaration originated.
563 class VariableDeclaration FINAL : public Declaration {
565 DECLARE_NODE_TYPE(VariableDeclaration)
567 InitializationFlag initialization() const OVERRIDE {
568 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
571 bool is_class_declaration() const { return is_class_declaration_; }
574 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
575 Scope* scope, int pos, bool is_class_declaration = false)
576 : Declaration(zone, proxy, mode, scope, pos),
577 is_class_declaration_(is_class_declaration) {}
579 bool is_class_declaration_;
583 class FunctionDeclaration FINAL : public Declaration {
585 DECLARE_NODE_TYPE(FunctionDeclaration)
587 FunctionLiteral* fun() const { return fun_; }
588 InitializationFlag initialization() const OVERRIDE {
589 return kCreatedInitialized;
591 bool IsInlineable() const OVERRIDE;
594 FunctionDeclaration(Zone* zone,
595 VariableProxy* proxy,
597 FunctionLiteral* fun,
600 : Declaration(zone, proxy, mode, scope, pos),
602 DCHECK(mode == VAR || mode == LET || mode == CONST);
607 FunctionLiteral* fun_;
611 class ModuleDeclaration FINAL : public Declaration {
613 DECLARE_NODE_TYPE(ModuleDeclaration)
615 Module* module() const { return module_; }
616 InitializationFlag initialization() const OVERRIDE {
617 return kCreatedInitialized;
621 ModuleDeclaration(Zone* zone, VariableProxy* proxy, Module* module,
622 Scope* scope, int pos)
623 : Declaration(zone, proxy, CONST, scope, pos), module_(module) {}
630 class ImportDeclaration FINAL : public Declaration {
632 DECLARE_NODE_TYPE(ImportDeclaration)
634 const AstRawString* import_name() const { return import_name_; }
635 const AstRawString* module_specifier() const { return module_specifier_; }
636 void set_module_specifier(const AstRawString* module_specifier) {
637 DCHECK(module_specifier_ == NULL);
638 module_specifier_ = module_specifier;
640 InitializationFlag initialization() const OVERRIDE {
641 return kNeedsInitialization;
645 ImportDeclaration(Zone* zone, VariableProxy* proxy,
646 const AstRawString* import_name,
647 const AstRawString* module_specifier, Scope* scope, int pos)
648 : Declaration(zone, proxy, IMPORT, scope, pos),
649 import_name_(import_name),
650 module_specifier_(module_specifier) {}
653 const AstRawString* import_name_;
654 const AstRawString* module_specifier_;
658 class ExportDeclaration FINAL : public Declaration {
660 DECLARE_NODE_TYPE(ExportDeclaration)
662 InitializationFlag initialization() const OVERRIDE {
663 return kCreatedInitialized;
667 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
668 : Declaration(zone, proxy, LET, scope, pos) {}
672 class Module : public AstNode {
674 ModuleDescriptor* descriptor() const { return descriptor_; }
675 Block* body() const { return body_; }
678 Module(Zone* zone, int pos)
679 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
680 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
681 : AstNode(pos), descriptor_(descriptor), body_(body) {}
684 ModuleDescriptor* descriptor_;
689 class ModuleLiteral FINAL : public Module {
691 DECLARE_NODE_TYPE(ModuleLiteral)
694 ModuleLiteral(Zone* zone, Block* body, ModuleDescriptor* descriptor, int pos)
695 : Module(zone, descriptor, pos, body) {}
699 class ModulePath FINAL : public Module {
701 DECLARE_NODE_TYPE(ModulePath)
703 Module* module() const { return module_; }
704 Handle<String> name() const { return name_->string(); }
707 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
708 : Module(zone, pos), module_(module), name_(name) {}
712 const AstRawString* name_;
716 class ModuleUrl FINAL : public Module {
718 DECLARE_NODE_TYPE(ModuleUrl)
720 Handle<String> url() const { return url_; }
723 ModuleUrl(Zone* zone, Handle<String> url, int pos)
724 : Module(zone, pos), url_(url) {
732 class ModuleStatement FINAL : public Statement {
734 DECLARE_NODE_TYPE(ModuleStatement)
736 Block* body() const { return body_; }
739 ModuleStatement(Zone* zone, Block* body, int pos)
740 : Statement(zone, pos), body_(body) {}
747 class IterationStatement : public BreakableStatement {
749 // Type testing & conversion.
750 IterationStatement* AsIterationStatement() FINAL { return this; }
752 Statement* body() const { return body_; }
754 static int num_ids() { return parent_num_ids() + 1; }
755 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
756 virtual BailoutId ContinueId() const = 0;
757 virtual BailoutId StackCheckId() const = 0;
760 Label* continue_target() { return &continue_target_; }
763 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
764 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
766 static int parent_num_ids() { return BreakableStatement::num_ids(); }
767 void Initialize(Statement* body) { body_ = body; }
770 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
773 Label continue_target_;
777 class DoWhileStatement FINAL : public IterationStatement {
779 DECLARE_NODE_TYPE(DoWhileStatement)
781 void Initialize(Expression* cond, Statement* body) {
782 IterationStatement::Initialize(body);
786 Expression* cond() const { return cond_; }
788 static int num_ids() { return parent_num_ids() + 2; }
789 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
790 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
791 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
794 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
795 : IterationStatement(zone, labels, pos), cond_(NULL) {}
796 static int parent_num_ids() { return IterationStatement::num_ids(); }
799 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
805 class WhileStatement FINAL : public IterationStatement {
807 DECLARE_NODE_TYPE(WhileStatement)
809 void Initialize(Expression* cond, Statement* body) {
810 IterationStatement::Initialize(body);
814 Expression* cond() const { return cond_; }
816 static int num_ids() { return parent_num_ids() + 1; }
817 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
818 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
819 BailoutId BodyId() const { return BailoutId(local_id(0)); }
822 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
823 : IterationStatement(zone, labels, pos), cond_(NULL) {}
824 static int parent_num_ids() { return IterationStatement::num_ids(); }
827 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
833 class ForStatement FINAL : public IterationStatement {
835 DECLARE_NODE_TYPE(ForStatement)
837 void Initialize(Statement* init,
841 IterationStatement::Initialize(body);
847 Statement* init() const { return init_; }
848 Expression* cond() const { return cond_; }
849 Statement* next() const { return next_; }
851 static int num_ids() { return parent_num_ids() + 2; }
852 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
853 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
854 BailoutId BodyId() const { return BailoutId(local_id(1)); }
857 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
858 : IterationStatement(zone, labels, pos),
862 static int parent_num_ids() { return IterationStatement::num_ids(); }
865 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
873 class ForEachStatement : public IterationStatement {
876 ENUMERATE, // for (each in subject) body;
877 ITERATE // for (each of subject) body;
880 void Initialize(Expression* each, Expression* subject, Statement* body) {
881 IterationStatement::Initialize(body);
886 Expression* each() const { return each_; }
887 Expression* subject() const { return subject_; }
890 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
891 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
895 Expression* subject_;
899 class ForInStatement FINAL : public ForEachStatement {
901 DECLARE_NODE_TYPE(ForInStatement)
903 Expression* enumerable() const {
907 // Type feedback information.
908 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
909 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
910 return FeedbackVectorRequirements(1, 0);
912 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
913 for_in_feedback_slot_ = slot;
916 FeedbackVectorSlot ForInFeedbackSlot() {
917 DCHECK(!for_in_feedback_slot_.IsInvalid());
918 return for_in_feedback_slot_;
921 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
922 ForInType for_in_type() const { return for_in_type_; }
923 void set_for_in_type(ForInType type) { for_in_type_ = type; }
925 static int num_ids() { return parent_num_ids() + 6; }
926 BailoutId BodyId() const { return BailoutId(local_id(0)); }
927 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
928 BailoutId EnumId() const { return BailoutId(local_id(2)); }
929 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
930 BailoutId FilterId() const { return BailoutId(local_id(4)); }
931 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
932 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
933 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
936 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
937 : ForEachStatement(zone, labels, pos),
938 for_in_type_(SLOW_FOR_IN),
939 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
940 static int parent_num_ids() { return ForEachStatement::num_ids(); }
943 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
945 ForInType for_in_type_;
946 FeedbackVectorSlot for_in_feedback_slot_;
950 class ForOfStatement FINAL : public ForEachStatement {
952 DECLARE_NODE_TYPE(ForOfStatement)
954 void Initialize(Expression* each,
957 Expression* assign_iterator,
958 Expression* next_result,
959 Expression* result_done,
960 Expression* assign_each) {
961 ForEachStatement::Initialize(each, subject, body);
962 assign_iterator_ = assign_iterator;
963 next_result_ = next_result;
964 result_done_ = result_done;
965 assign_each_ = assign_each;
968 Expression* iterable() const {
972 // iterator = subject[Symbol.iterator]()
973 Expression* assign_iterator() const {
974 return assign_iterator_;
977 // result = iterator.next() // with type check
978 Expression* next_result() const {
983 Expression* result_done() const {
987 // each = result.value
988 Expression* assign_each() const {
992 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
993 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
995 static int num_ids() { return parent_num_ids() + 1; }
996 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
999 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1000 : ForEachStatement(zone, labels, pos),
1001 assign_iterator_(NULL),
1004 assign_each_(NULL) {}
1005 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1008 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1010 Expression* assign_iterator_;
1011 Expression* next_result_;
1012 Expression* result_done_;
1013 Expression* assign_each_;
1017 class ExpressionStatement FINAL : public Statement {
1019 DECLARE_NODE_TYPE(ExpressionStatement)
1021 void set_expression(Expression* e) { expression_ = e; }
1022 Expression* expression() const { return expression_; }
1023 bool IsJump() const OVERRIDE { return expression_->IsThrow(); }
1026 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1027 : Statement(zone, pos), expression_(expression) { }
1030 Expression* expression_;
1034 class JumpStatement : public Statement {
1036 bool IsJump() const FINAL { return true; }
1039 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1043 class ContinueStatement FINAL : public JumpStatement {
1045 DECLARE_NODE_TYPE(ContinueStatement)
1047 IterationStatement* target() const { return target_; }
1050 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1051 : JumpStatement(zone, pos), target_(target) { }
1054 IterationStatement* target_;
1058 class BreakStatement FINAL : public JumpStatement {
1060 DECLARE_NODE_TYPE(BreakStatement)
1062 BreakableStatement* target() const { return target_; }
1065 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1066 : JumpStatement(zone, pos), target_(target) { }
1069 BreakableStatement* target_;
1073 class ReturnStatement FINAL : public JumpStatement {
1075 DECLARE_NODE_TYPE(ReturnStatement)
1077 Expression* expression() const { return expression_; }
1080 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1081 : JumpStatement(zone, pos), expression_(expression) { }
1084 Expression* expression_;
1088 class WithStatement FINAL : public Statement {
1090 DECLARE_NODE_TYPE(WithStatement)
1092 Scope* scope() { return scope_; }
1093 Expression* expression() const { return expression_; }
1094 Statement* statement() const { return statement_; }
1096 void set_base_id(int id) { base_id_ = id; }
1097 static int num_ids() { return parent_num_ids() + 1; }
1098 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1101 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1102 Statement* statement, int pos)
1103 : Statement(zone, pos),
1105 expression_(expression),
1106 statement_(statement),
1107 base_id_(BailoutId::None().ToInt()) {}
1108 static int parent_num_ids() { return 0; }
1110 int base_id() const {
1111 DCHECK(!BailoutId(base_id_).IsNone());
1116 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1119 Expression* expression_;
1120 Statement* statement_;
1125 class CaseClause FINAL : public Expression {
1127 DECLARE_NODE_TYPE(CaseClause)
1129 bool is_default() const { return label_ == NULL; }
1130 Expression* label() const {
1131 CHECK(!is_default());
1134 Label* body_target() { return &body_target_; }
1135 ZoneList<Statement*>* statements() const { return statements_; }
1137 static int num_ids() { return parent_num_ids() + 2; }
1138 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1139 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1141 Type* compare_type() { return compare_type_; }
1142 void set_compare_type(Type* type) { compare_type_ = type; }
1145 static int parent_num_ids() { return Expression::num_ids(); }
1148 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1150 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1154 ZoneList<Statement*>* statements_;
1155 Type* compare_type_;
1159 class SwitchStatement FINAL : public BreakableStatement {
1161 DECLARE_NODE_TYPE(SwitchStatement)
1163 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1168 Expression* tag() const { return tag_; }
1169 ZoneList<CaseClause*>* cases() const { return cases_; }
1172 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1173 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1179 ZoneList<CaseClause*>* cases_;
1183 // If-statements always have non-null references to their then- and
1184 // else-parts. When parsing if-statements with no explicit else-part,
1185 // the parser implicitly creates an empty statement. Use the
1186 // HasThenStatement() and HasElseStatement() functions to check if a
1187 // given if-statement has a then- or an else-part containing code.
1188 class IfStatement FINAL : public Statement {
1190 DECLARE_NODE_TYPE(IfStatement)
1192 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1193 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1195 Expression* condition() const { return condition_; }
1196 Statement* then_statement() const { return then_statement_; }
1197 Statement* else_statement() const { return else_statement_; }
1199 bool IsJump() const OVERRIDE {
1200 return HasThenStatement() && then_statement()->IsJump()
1201 && HasElseStatement() && else_statement()->IsJump();
1204 void set_base_id(int id) { base_id_ = id; }
1205 static int num_ids() { return parent_num_ids() + 3; }
1206 BailoutId IfId() const { return BailoutId(local_id(0)); }
1207 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1208 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1211 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1212 Statement* else_statement, int pos)
1213 : Statement(zone, pos),
1214 condition_(condition),
1215 then_statement_(then_statement),
1216 else_statement_(else_statement),
1217 base_id_(BailoutId::None().ToInt()) {}
1218 static int parent_num_ids() { return 0; }
1220 int base_id() const {
1221 DCHECK(!BailoutId(base_id_).IsNone());
1226 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1228 Expression* condition_;
1229 Statement* then_statement_;
1230 Statement* else_statement_;
1235 class TryStatement : public Statement {
1237 int index() const { return index_; }
1238 Block* try_block() const { return try_block_; }
1241 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1242 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1245 // Unique (per-function) index of this handler. This is not an AST ID.
1252 class TryCatchStatement FINAL : public TryStatement {
1254 DECLARE_NODE_TYPE(TryCatchStatement)
1256 Scope* scope() { return scope_; }
1257 Variable* variable() { return variable_; }
1258 Block* catch_block() const { return catch_block_; }
1261 TryCatchStatement(Zone* zone,
1268 : TryStatement(zone, index, try_block, pos),
1270 variable_(variable),
1271 catch_block_(catch_block) {
1276 Variable* variable_;
1277 Block* catch_block_;
1281 class TryFinallyStatement FINAL : public TryStatement {
1283 DECLARE_NODE_TYPE(TryFinallyStatement)
1285 Block* finally_block() const { return finally_block_; }
1288 TryFinallyStatement(
1289 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1290 : TryStatement(zone, index, try_block, pos),
1291 finally_block_(finally_block) { }
1294 Block* finally_block_;
1298 class DebuggerStatement FINAL : public Statement {
1300 DECLARE_NODE_TYPE(DebuggerStatement)
1302 void set_base_id(int id) { base_id_ = id; }
1303 static int num_ids() { return parent_num_ids() + 1; }
1304 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1307 explicit DebuggerStatement(Zone* zone, int pos)
1308 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1309 static int parent_num_ids() { return 0; }
1311 int base_id() const {
1312 DCHECK(!BailoutId(base_id_).IsNone());
1317 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1323 class EmptyStatement FINAL : public Statement {
1325 DECLARE_NODE_TYPE(EmptyStatement)
1328 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1332 class Literal FINAL : public Expression {
1334 DECLARE_NODE_TYPE(Literal)
1336 bool IsPropertyName() const OVERRIDE { return value_->IsPropertyName(); }
1338 Handle<String> AsPropertyName() {
1339 DCHECK(IsPropertyName());
1340 return Handle<String>::cast(value());
1343 const AstRawString* AsRawPropertyName() {
1344 DCHECK(IsPropertyName());
1345 return value_->AsString();
1348 bool ToBooleanIsTrue() const OVERRIDE { return value()->BooleanValue(); }
1349 bool ToBooleanIsFalse() const OVERRIDE { return !value()->BooleanValue(); }
1351 Handle<Object> value() const { return value_->value(); }
1352 const AstValue* raw_value() const { return value_; }
1354 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1355 // only for string and number literals!
1357 static bool Match(void* literal1, void* literal2);
1359 static int num_ids() { return parent_num_ids() + 1; }
1360 TypeFeedbackId LiteralFeedbackId() const {
1361 return TypeFeedbackId(local_id(0));
1365 Literal(Zone* zone, const AstValue* value, int position)
1366 : Expression(zone, position), value_(value) {}
1367 static int parent_num_ids() { return Expression::num_ids(); }
1370 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1372 const AstValue* value_;
1376 // Base class for literals that needs space in the corresponding JSFunction.
1377 class MaterializedLiteral : public Expression {
1379 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1381 int literal_index() { return literal_index_; }
1384 // only callable after initialization.
1385 DCHECK(depth_ >= 1);
1390 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1391 : Expression(zone, pos),
1392 literal_index_(literal_index),
1396 // A materialized literal is simple if the values consist of only
1397 // constants and simple object and array literals.
1398 bool is_simple() const { return is_simple_; }
1399 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1400 friend class CompileTimeValue;
1402 void set_depth(int depth) {
1407 // Populate the constant properties/elements fixed array.
1408 void BuildConstants(Isolate* isolate);
1409 friend class ArrayLiteral;
1410 friend class ObjectLiteral;
1412 // If the expression is a literal, return the literal value;
1413 // if the expression is a materialized literal and is simple return a
1414 // compile time value as encoded by CompileTimeValue::GetValue().
1415 // Otherwise, return undefined literal as the placeholder
1416 // in the object literal boilerplate.
1417 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1426 // Property is used for passing information
1427 // about an object literal's properties from the parser
1428 // to the code generator.
1429 class ObjectLiteralProperty FINAL : public ZoneObject {
1432 CONSTANT, // Property with constant value (compile time).
1433 COMPUTED, // Property with computed value (execution time).
1434 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1435 GETTER, SETTER, // Property is an accessor function.
1436 PROTOTYPE // Property is __proto__.
1439 Expression* key() { return key_; }
1440 Expression* value() { return value_; }
1441 Kind kind() { return kind_; }
1443 // Type feedback information.
1444 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1445 Handle<Map> GetReceiverType() { return receiver_type_; }
1447 bool IsCompileTimeValue();
1449 void set_emit_store(bool emit_store);
1452 bool is_static() const { return is_static_; }
1453 bool is_computed_name() const { return is_computed_name_; }
1455 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1458 friend class AstNodeFactory;
1460 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1461 bool is_static, bool is_computed_name);
1462 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1463 Expression* value, bool is_static,
1464 bool is_computed_name);
1472 bool is_computed_name_;
1473 Handle<Map> receiver_type_;
1477 // An object literal has a boilerplate object that is used
1478 // for minimizing the work when constructing it at runtime.
1479 class ObjectLiteral FINAL : public MaterializedLiteral {
1481 typedef ObjectLiteralProperty Property;
1483 DECLARE_NODE_TYPE(ObjectLiteral)
1485 Handle<FixedArray> constant_properties() const {
1486 return constant_properties_;
1488 int properties_count() const { return constant_properties_->length() / 2; }
1489 ZoneList<Property*>* properties() const { return properties_; }
1490 bool fast_elements() const { return fast_elements_; }
1491 bool may_store_doubles() const { return may_store_doubles_; }
1492 bool has_function() const { return has_function_; }
1493 bool has_elements() const { return has_elements_; }
1495 // Decide if a property should be in the object boilerplate.
1496 static bool IsBoilerplateProperty(Property* property);
1498 // Populate the constant properties fixed array.
1499 void BuildConstantProperties(Isolate* isolate);
1501 // Mark all computed expressions that are bound to a key that
1502 // is shadowed by a later occurrence of the same key. For the
1503 // marked expressions, no store code is emitted.
1504 void CalculateEmitStore(Zone* zone);
1506 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1507 int ComputeFlags(bool disable_mementos = false) const {
1508 int flags = fast_elements() ? kFastElements : kNoFlags;
1509 flags |= has_function() ? kHasFunction : kNoFlags;
1510 if (disable_mementos) {
1511 flags |= kDisableMementos;
1519 kHasFunction = 1 << 1,
1520 kDisableMementos = 1 << 2
1523 struct Accessors: public ZoneObject {
1524 Accessors() : getter(NULL), setter(NULL) {}
1529 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1531 // Return an AST id for a property that is used in simulate instructions.
1532 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1534 // Unlike other AST nodes, this number of bailout IDs allocated for an
1535 // ObjectLiteral can vary, so num_ids() is not a static method.
1536 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1539 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1540 int boilerplate_properties, bool has_function, int pos)
1541 : MaterializedLiteral(zone, literal_index, pos),
1542 properties_(properties),
1543 boilerplate_properties_(boilerplate_properties),
1544 fast_elements_(false),
1545 has_elements_(false),
1546 may_store_doubles_(false),
1547 has_function_(has_function) {}
1548 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1551 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1552 Handle<FixedArray> constant_properties_;
1553 ZoneList<Property*>* properties_;
1554 int boilerplate_properties_;
1555 bool fast_elements_;
1557 bool may_store_doubles_;
1562 // Node for capturing a regexp literal.
1563 class RegExpLiteral FINAL : public MaterializedLiteral {
1565 DECLARE_NODE_TYPE(RegExpLiteral)
1567 Handle<String> pattern() const { return pattern_->string(); }
1568 Handle<String> flags() const { return flags_->string(); }
1571 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1572 const AstRawString* flags, int literal_index, int pos)
1573 : MaterializedLiteral(zone, literal_index, pos),
1580 const AstRawString* pattern_;
1581 const AstRawString* flags_;
1585 // An array literal has a literals object that is used
1586 // for minimizing the work when constructing it at runtime.
1587 class ArrayLiteral FINAL : public MaterializedLiteral {
1589 DECLARE_NODE_TYPE(ArrayLiteral)
1591 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1592 ElementsKind constant_elements_kind() const {
1593 DCHECK_EQ(2, constant_elements_->length());
1594 return static_cast<ElementsKind>(
1595 Smi::cast(constant_elements_->get(0))->value());
1598 ZoneList<Expression*>* values() const { return values_; }
1600 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1602 // Return an AST id for an element that is used in simulate instructions.
1603 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1605 // Unlike other AST nodes, this number of bailout IDs allocated for an
1606 // ArrayLiteral can vary, so num_ids() is not a static method.
1607 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1609 // Populate the constant elements fixed array.
1610 void BuildConstantElements(Isolate* isolate);
1612 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1613 int ComputeFlags(bool disable_mementos = false) const {
1614 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1615 if (disable_mementos) {
1616 flags |= kDisableMementos;
1623 kShallowElements = 1,
1624 kDisableMementos = 1 << 1
1628 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1630 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1631 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1634 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1636 Handle<FixedArray> constant_elements_;
1637 ZoneList<Expression*>* values_;
1641 class VariableProxy FINAL : public Expression {
1643 DECLARE_NODE_TYPE(VariableProxy)
1645 bool IsValidReferenceExpression() const OVERRIDE { return !is_this(); }
1647 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1649 Handle<String> name() const { return raw_name()->string(); }
1650 const AstRawString* raw_name() const {
1651 return is_resolved() ? var_->raw_name() : raw_name_;
1654 Variable* var() const {
1655 DCHECK(is_resolved());
1658 void set_var(Variable* v) {
1659 DCHECK(!is_resolved());
1664 bool is_this() const { return IsThisField::decode(bit_field_); }
1666 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1667 void set_is_assigned() {
1668 bit_field_ = IsAssignedField::update(bit_field_, true);
1671 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1672 void set_is_resolved() {
1673 bit_field_ = IsResolvedField::update(bit_field_, true);
1676 int end_position() const { return end_position_; }
1678 // Bind this proxy to the variable var.
1679 void BindTo(Variable* var);
1681 bool UsesVariableFeedbackSlot() const {
1682 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1685 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1686 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1688 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1689 ICSlotCache* cache) OVERRIDE;
1690 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1691 FeedbackVectorICSlot VariableFeedbackSlot() {
1692 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1693 return variable_feedback_slot_;
1697 VariableProxy(Zone* zone, Variable* var, int start_position,
1700 VariableProxy(Zone* zone, const AstRawString* name,
1701 Variable::Kind variable_kind, int start_position,
1704 class IsThisField : public BitField8<bool, 0, 1> {};
1705 class IsAssignedField : public BitField8<bool, 1, 1> {};
1706 class IsResolvedField : public BitField8<bool, 2, 1> {};
1708 // Start with 16-bit (or smaller) field, which should get packed together
1709 // with Expression's trailing 16-bit field.
1711 FeedbackVectorICSlot variable_feedback_slot_;
1713 const AstRawString* raw_name_; // if !is_resolved_
1714 Variable* var_; // if is_resolved_
1716 // Position is stored in the AstNode superclass, but VariableProxy needs to
1717 // know its end position too (for error messages). It cannot be inferred from
1718 // the variable name length because it can contain escapes.
1723 class Property FINAL : public Expression {
1725 DECLARE_NODE_TYPE(Property)
1727 bool IsValidReferenceExpression() const OVERRIDE { return true; }
1729 Expression* obj() const { return obj_; }
1730 Expression* key() const { return key_; }
1732 static int num_ids() { return parent_num_ids() + 2; }
1733 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1734 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1736 bool IsStringAccess() const {
1737 return IsStringAccessField::decode(bit_field_);
1740 // Type feedback information.
1741 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
1742 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
1743 KeyedAccessStoreMode GetStoreMode() const OVERRIDE { return STANDARD_STORE; }
1744 IcCheckType GetKeyType() const OVERRIDE {
1745 return KeyTypeField::decode(bit_field_);
1747 bool IsUninitialized() const {
1748 return !is_for_call() && HasNoTypeInformation();
1750 bool HasNoTypeInformation() const {
1751 return GetInlineCacheState() == UNINITIALIZED;
1753 InlineCacheState GetInlineCacheState() const {
1754 return InlineCacheStateField::decode(bit_field_);
1756 void set_is_string_access(bool b) {
1757 bit_field_ = IsStringAccessField::update(bit_field_, b);
1759 void set_key_type(IcCheckType key_type) {
1760 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1762 void set_inline_cache_state(InlineCacheState state) {
1763 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1765 void mark_for_call() {
1766 bit_field_ = IsForCallField::update(bit_field_, true);
1768 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1770 bool IsSuperAccess() {
1771 return obj()->IsSuperReference();
1774 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1775 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1776 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1778 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1779 ICSlotCache* cache) OVERRIDE {
1780 property_feedback_slot_ = slot;
1782 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1783 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1786 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1787 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1788 return property_feedback_slot_;
1792 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1793 : Expression(zone, pos),
1794 bit_field_(IsForCallField::encode(false) |
1795 IsStringAccessField::encode(false) |
1796 InlineCacheStateField::encode(UNINITIALIZED)),
1797 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1800 static int parent_num_ids() { return Expression::num_ids(); }
1803 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1805 class IsForCallField : public BitField8<bool, 0, 1> {};
1806 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1807 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1808 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1810 FeedbackVectorICSlot property_feedback_slot_;
1813 SmallMapList receiver_types_;
1817 class Call FINAL : public Expression {
1819 DECLARE_NODE_TYPE(Call)
1821 Expression* expression() const { return expression_; }
1822 ZoneList<Expression*>* arguments() const { return arguments_; }
1824 // Type feedback information.
1825 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1826 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1827 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1828 ICSlotCache* cache) OVERRIDE {
1829 ic_slot_or_slot_ = slot.ToInt();
1831 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1832 ic_slot_or_slot_ = slot.ToInt();
1834 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1836 FeedbackVectorSlot CallFeedbackSlot() const {
1837 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1838 return FeedbackVectorSlot(ic_slot_or_slot_);
1841 FeedbackVectorICSlot CallFeedbackICSlot() const {
1842 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1843 return FeedbackVectorICSlot(ic_slot_or_slot_);
1846 SmallMapList* GetReceiverTypes() OVERRIDE {
1847 if (expression()->IsProperty()) {
1848 return expression()->AsProperty()->GetReceiverTypes();
1853 bool IsMonomorphic() OVERRIDE {
1854 if (expression()->IsProperty()) {
1855 return expression()->AsProperty()->IsMonomorphic();
1857 return !target_.is_null();
1860 bool global_call() const {
1861 VariableProxy* proxy = expression_->AsVariableProxy();
1862 return proxy != NULL && proxy->var()->IsUnallocated();
1865 bool known_global_function() const {
1866 return global_call() && !target_.is_null();
1869 Handle<JSFunction> target() { return target_; }
1871 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1873 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1875 set_is_uninitialized(false);
1877 void set_target(Handle<JSFunction> target) { target_ = target; }
1878 void set_allocation_site(Handle<AllocationSite> site) {
1879 allocation_site_ = site;
1882 static int num_ids() { return parent_num_ids() + 2; }
1883 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1884 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1886 bool is_uninitialized() const {
1887 return IsUninitializedField::decode(bit_field_);
1889 void set_is_uninitialized(bool b) {
1890 bit_field_ = IsUninitializedField::update(bit_field_, b);
1902 // Helpers to determine how to handle the call.
1903 CallType GetCallType(Isolate* isolate) const;
1904 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1905 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1908 // Used to assert that the FullCodeGenerator records the return site.
1909 bool return_is_recorded_;
1913 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1915 : Expression(zone, pos),
1916 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1917 expression_(expression),
1918 arguments_(arguments),
1919 bit_field_(IsUninitializedField::encode(false)) {
1920 if (expression->IsProperty()) {
1921 expression->AsProperty()->mark_for_call();
1924 static int parent_num_ids() { return Expression::num_ids(); }
1927 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1929 // We store this as an integer because we don't know if we have a slot or
1930 // an ic slot until scoping time.
1931 int ic_slot_or_slot_;
1932 Expression* expression_;
1933 ZoneList<Expression*>* arguments_;
1934 Handle<JSFunction> target_;
1935 Handle<AllocationSite> allocation_site_;
1936 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1941 class CallNew FINAL : public Expression {
1943 DECLARE_NODE_TYPE(CallNew)
1945 Expression* expression() const { return expression_; }
1946 ZoneList<Expression*>* arguments() const { return arguments_; }
1948 // Type feedback information.
1949 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1950 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1951 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1953 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1954 callnew_feedback_slot_ = slot;
1957 FeedbackVectorSlot CallNewFeedbackSlot() {
1958 DCHECK(!callnew_feedback_slot_.IsInvalid());
1959 return callnew_feedback_slot_;
1961 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1962 DCHECK(FLAG_pretenuring_call_new);
1963 return CallNewFeedbackSlot().next();
1966 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1967 Handle<JSFunction> target() const { return target_; }
1968 Handle<AllocationSite> allocation_site() const {
1969 return allocation_site_;
1972 static int num_ids() { return parent_num_ids() + 1; }
1973 static int feedback_slots() { return 1; }
1974 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1976 void set_allocation_site(Handle<AllocationSite> site) {
1977 allocation_site_ = site;
1979 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1980 void set_target(Handle<JSFunction> target) { target_ = target; }
1981 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1983 is_monomorphic_ = true;
1987 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1989 : Expression(zone, pos),
1990 expression_(expression),
1991 arguments_(arguments),
1992 is_monomorphic_(false),
1993 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1995 static int parent_num_ids() { return Expression::num_ids(); }
1998 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2000 Expression* expression_;
2001 ZoneList<Expression*>* arguments_;
2002 bool is_monomorphic_;
2003 Handle<JSFunction> target_;
2004 Handle<AllocationSite> allocation_site_;
2005 FeedbackVectorSlot callnew_feedback_slot_;
2009 // The CallRuntime class does not represent any official JavaScript
2010 // language construct. Instead it is used to call a C or JS function
2011 // with a set of arguments. This is used from the builtins that are
2012 // implemented in JavaScript (see "v8natives.js").
2013 class CallRuntime FINAL : public Expression {
2015 DECLARE_NODE_TYPE(CallRuntime)
2017 Handle<String> name() const { return raw_name_->string(); }
2018 const AstRawString* raw_name() const { return raw_name_; }
2019 const Runtime::Function* function() const { return function_; }
2020 ZoneList<Expression*>* arguments() const { return arguments_; }
2021 bool is_jsruntime() const { return function_ == NULL; }
2023 // Type feedback information.
2024 bool HasCallRuntimeFeedbackSlot() const {
2025 return FLAG_vector_ics && is_jsruntime();
2027 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2028 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2029 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2031 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2032 ICSlotCache* cache) OVERRIDE {
2033 callruntime_feedback_slot_ = slot;
2035 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2037 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2038 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2039 !callruntime_feedback_slot_.IsInvalid());
2040 return callruntime_feedback_slot_;
2043 static int num_ids() { return parent_num_ids() + 1; }
2044 TypeFeedbackId CallRuntimeFeedbackId() const {
2045 return TypeFeedbackId(local_id(0));
2049 CallRuntime(Zone* zone, const AstRawString* name,
2050 const Runtime::Function* function,
2051 ZoneList<Expression*>* arguments, int pos)
2052 : Expression(zone, pos),
2054 function_(function),
2055 arguments_(arguments),
2056 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2057 static int parent_num_ids() { return Expression::num_ids(); }
2060 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2062 const AstRawString* raw_name_;
2063 const Runtime::Function* function_;
2064 ZoneList<Expression*>* arguments_;
2065 FeedbackVectorICSlot callruntime_feedback_slot_;
2069 class UnaryOperation FINAL : public Expression {
2071 DECLARE_NODE_TYPE(UnaryOperation)
2073 Token::Value op() const { return op_; }
2074 Expression* expression() const { return expression_; }
2076 // For unary not (Token::NOT), the AST ids where true and false will
2077 // actually be materialized, respectively.
2078 static int num_ids() { return parent_num_ids() + 2; }
2079 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2080 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2082 virtual void RecordToBooleanTypeFeedback(
2083 TypeFeedbackOracle* oracle) OVERRIDE;
2086 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2087 : Expression(zone, pos), op_(op), expression_(expression) {
2088 DCHECK(Token::IsUnaryOp(op));
2090 static int parent_num_ids() { return Expression::num_ids(); }
2093 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2096 Expression* expression_;
2100 class BinaryOperation FINAL : public Expression {
2102 DECLARE_NODE_TYPE(BinaryOperation)
2104 Token::Value op() const { return static_cast<Token::Value>(op_); }
2105 Expression* left() const { return left_; }
2106 Expression* right() const { return right_; }
2107 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2108 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2109 allocation_site_ = allocation_site;
2112 // The short-circuit logical operations need an AST ID for their
2113 // right-hand subexpression.
2114 static int num_ids() { return parent_num_ids() + 2; }
2115 BailoutId RightId() const { return BailoutId(local_id(0)); }
2117 TypeFeedbackId BinaryOperationFeedbackId() const {
2118 return TypeFeedbackId(local_id(1));
2120 Maybe<int> fixed_right_arg() const {
2121 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2123 void set_fixed_right_arg(Maybe<int> arg) {
2124 has_fixed_right_arg_ = arg.IsJust();
2125 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2128 virtual void RecordToBooleanTypeFeedback(
2129 TypeFeedbackOracle* oracle) OVERRIDE;
2132 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2133 Expression* right, int pos)
2134 : Expression(zone, pos),
2135 op_(static_cast<byte>(op)),
2136 has_fixed_right_arg_(false),
2137 fixed_right_arg_value_(0),
2140 DCHECK(Token::IsBinaryOp(op));
2142 static int parent_num_ids() { return Expression::num_ids(); }
2145 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2147 const byte op_; // actually Token::Value
2148 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2149 // type for the RHS. Currenty it's actually a Maybe<int>
2150 bool has_fixed_right_arg_;
2151 int fixed_right_arg_value_;
2154 Handle<AllocationSite> allocation_site_;
2158 class CountOperation FINAL : public Expression {
2160 DECLARE_NODE_TYPE(CountOperation)
2162 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2163 bool is_postfix() const { return !is_prefix(); }
2165 Token::Value op() const { return TokenField::decode(bit_field_); }
2166 Token::Value binary_op() {
2167 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2170 Expression* expression() const { return expression_; }
2172 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2173 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2174 IcCheckType GetKeyType() const OVERRIDE {
2175 return KeyTypeField::decode(bit_field_);
2177 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2178 return StoreModeField::decode(bit_field_);
2180 Type* type() const { return type_; }
2181 void set_key_type(IcCheckType type) {
2182 bit_field_ = KeyTypeField::update(bit_field_, type);
2184 void set_store_mode(KeyedAccessStoreMode mode) {
2185 bit_field_ = StoreModeField::update(bit_field_, mode);
2187 void set_type(Type* type) { type_ = type; }
2189 static int num_ids() { return parent_num_ids() + 4; }
2190 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2191 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2192 TypeFeedbackId CountBinOpFeedbackId() const {
2193 return TypeFeedbackId(local_id(2));
2195 TypeFeedbackId CountStoreFeedbackId() const {
2196 return TypeFeedbackId(local_id(3));
2200 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2202 : Expression(zone, pos),
2203 bit_field_(IsPrefixField::encode(is_prefix) |
2204 KeyTypeField::encode(ELEMENT) |
2205 StoreModeField::encode(STANDARD_STORE) |
2206 TokenField::encode(op)),
2208 expression_(expr) {}
2209 static int parent_num_ids() { return Expression::num_ids(); }
2212 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2214 class IsPrefixField : public BitField16<bool, 0, 1> {};
2215 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2216 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2217 class TokenField : public BitField16<Token::Value, 6, 8> {};
2219 // Starts with 16-bit field, which should get packed together with
2220 // Expression's trailing 16-bit field.
2221 uint16_t bit_field_;
2223 Expression* expression_;
2224 SmallMapList receiver_types_;
2228 class CompareOperation FINAL : public Expression {
2230 DECLARE_NODE_TYPE(CompareOperation)
2232 Token::Value op() const { return op_; }
2233 Expression* left() const { return left_; }
2234 Expression* right() const { return right_; }
2236 // Type feedback information.
2237 static int num_ids() { return parent_num_ids() + 1; }
2238 TypeFeedbackId CompareOperationFeedbackId() const {
2239 return TypeFeedbackId(local_id(0));
2241 Type* combined_type() const { return combined_type_; }
2242 void set_combined_type(Type* type) { combined_type_ = type; }
2244 // Match special cases.
2245 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2246 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2247 bool IsLiteralCompareNull(Expression** expr);
2250 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2251 Expression* right, int pos)
2252 : Expression(zone, pos),
2256 combined_type_(Type::None(zone)) {
2257 DCHECK(Token::IsCompareOp(op));
2259 static int parent_num_ids() { return Expression::num_ids(); }
2262 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2268 Type* combined_type_;
2272 class Spread FINAL : public Expression {
2274 DECLARE_NODE_TYPE(Spread)
2276 Expression* expression() const { return expression_; }
2278 static int num_ids() { return parent_num_ids(); }
2281 Spread(Zone* zone, Expression* expression, int pos)
2282 : Expression(zone, pos), expression_(expression) {}
2283 static int parent_num_ids() { return Expression::num_ids(); }
2286 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2288 Expression* expression_;
2292 class Conditional FINAL : public Expression {
2294 DECLARE_NODE_TYPE(Conditional)
2296 Expression* condition() const { return condition_; }
2297 Expression* then_expression() const { return then_expression_; }
2298 Expression* else_expression() const { return else_expression_; }
2300 static int num_ids() { return parent_num_ids() + 2; }
2301 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2302 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2305 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2306 Expression* else_expression, int position)
2307 : Expression(zone, position),
2308 condition_(condition),
2309 then_expression_(then_expression),
2310 else_expression_(else_expression) {}
2311 static int parent_num_ids() { return Expression::num_ids(); }
2314 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2316 Expression* condition_;
2317 Expression* then_expression_;
2318 Expression* else_expression_;
2322 class Assignment FINAL : public Expression {
2324 DECLARE_NODE_TYPE(Assignment)
2326 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2328 Token::Value binary_op() const;
2330 Token::Value op() const { return TokenField::decode(bit_field_); }
2331 Expression* target() const { return target_; }
2332 Expression* value() const { return value_; }
2333 BinaryOperation* binary_operation() const { return binary_operation_; }
2335 // This check relies on the definition order of token in token.h.
2336 bool is_compound() const { return op() > Token::ASSIGN; }
2338 static int num_ids() { return parent_num_ids() + 2; }
2339 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2341 // Type feedback information.
2342 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2343 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2344 bool IsUninitialized() const {
2345 return IsUninitializedField::decode(bit_field_);
2347 bool HasNoTypeInformation() {
2348 return IsUninitializedField::decode(bit_field_);
2350 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2351 IcCheckType GetKeyType() const OVERRIDE {
2352 return KeyTypeField::decode(bit_field_);
2354 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2355 return StoreModeField::decode(bit_field_);
2357 void set_is_uninitialized(bool b) {
2358 bit_field_ = IsUninitializedField::update(bit_field_, b);
2360 void set_key_type(IcCheckType key_type) {
2361 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2363 void set_store_mode(KeyedAccessStoreMode mode) {
2364 bit_field_ = StoreModeField::update(bit_field_, mode);
2368 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2370 static int parent_num_ids() { return Expression::num_ids(); }
2373 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2375 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2376 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2377 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2378 class TokenField : public BitField16<Token::Value, 6, 8> {};
2380 // Starts with 16-bit field, which should get packed together with
2381 // Expression's trailing 16-bit field.
2382 uint16_t bit_field_;
2383 Expression* target_;
2385 BinaryOperation* binary_operation_;
2386 SmallMapList receiver_types_;
2390 class Yield FINAL : public Expression {
2392 DECLARE_NODE_TYPE(Yield)
2395 kInitial, // The initial yield that returns the unboxed generator object.
2396 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2397 kDelegating, // A yield*.
2398 kFinal // A return: { value: EXPRESSION, done: true }
2401 Expression* generator_object() const { return generator_object_; }
2402 Expression* expression() const { return expression_; }
2403 Kind yield_kind() const { return yield_kind_; }
2405 // Delegating yield surrounds the "yield" in a "try/catch". This index
2406 // locates the catch handler in the handler table, and is equivalent to
2407 // TryCatchStatement::index().
2409 DCHECK_EQ(kDelegating, yield_kind());
2412 void set_index(int index) {
2413 DCHECK_EQ(kDelegating, yield_kind());
2417 // Type feedback information.
2418 bool HasFeedbackSlots() const {
2419 return FLAG_vector_ics && (yield_kind() == kDelegating);
2421 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2422 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2423 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2425 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2426 ICSlotCache* cache) OVERRIDE {
2427 yield_first_feedback_slot_ = slot;
2429 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2430 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2433 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2434 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2435 return yield_first_feedback_slot_;
2438 FeedbackVectorICSlot DoneFeedbackSlot() {
2439 return KeyedLoadFeedbackSlot().next();
2442 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2445 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2446 Kind yield_kind, int pos)
2447 : Expression(zone, pos),
2448 generator_object_(generator_object),
2449 expression_(expression),
2450 yield_kind_(yield_kind),
2452 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2455 Expression* generator_object_;
2456 Expression* expression_;
2459 FeedbackVectorICSlot yield_first_feedback_slot_;
2463 class Throw FINAL : public Expression {
2465 DECLARE_NODE_TYPE(Throw)
2467 Expression* exception() const { return exception_; }
2470 Throw(Zone* zone, Expression* exception, int pos)
2471 : Expression(zone, pos), exception_(exception) {}
2474 Expression* exception_;
2478 class FunctionLiteral FINAL : public Expression {
2481 ANONYMOUS_EXPRESSION,
2486 enum ParameterFlag {
2487 kNoDuplicateParameters = 0,
2488 kHasDuplicateParameters = 1
2491 enum IsFunctionFlag {
2496 enum IsParenthesizedFlag {
2501 enum ArityRestriction {
2507 DECLARE_NODE_TYPE(FunctionLiteral)
2509 Handle<String> name() const { return raw_name_->string(); }
2510 const AstRawString* raw_name() const { return raw_name_; }
2511 Scope* scope() const { return scope_; }
2512 ZoneList<Statement*>* body() const { return body_; }
2513 void set_function_token_position(int pos) { function_token_position_ = pos; }
2514 int function_token_position() const { return function_token_position_; }
2515 int start_position() const;
2516 int end_position() const;
2517 int SourceSize() const { return end_position() - start_position(); }
2518 bool is_expression() const { return IsExpression::decode(bitfield_); }
2519 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2520 LanguageMode language_mode() const;
2521 bool uses_super_property() const;
2523 static bool NeedsHomeObject(Expression* literal) {
2524 return literal != NULL && literal->IsFunctionLiteral() &&
2525 literal->AsFunctionLiteral()->uses_super_property();
2528 int materialized_literal_count() { return materialized_literal_count_; }
2529 int expected_property_count() { return expected_property_count_; }
2530 int handler_count() { return handler_count_; }
2531 int parameter_count() { return parameter_count_; }
2533 bool AllowsLazyCompilation();
2534 bool AllowsLazyCompilationWithoutContext();
2536 void InitializeSharedInfo(Handle<Code> code);
2538 Handle<String> debug_name() const {
2539 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2540 return raw_name_->string();
2542 return inferred_name();
2545 Handle<String> inferred_name() const {
2546 if (!inferred_name_.is_null()) {
2547 DCHECK(raw_inferred_name_ == NULL);
2548 return inferred_name_;
2550 if (raw_inferred_name_ != NULL) {
2551 return raw_inferred_name_->string();
2554 return Handle<String>();
2557 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2558 void set_inferred_name(Handle<String> inferred_name) {
2559 DCHECK(!inferred_name.is_null());
2560 inferred_name_ = inferred_name;
2561 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2562 raw_inferred_name_ = NULL;
2565 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2566 DCHECK(raw_inferred_name != NULL);
2567 raw_inferred_name_ = raw_inferred_name;
2568 DCHECK(inferred_name_.is_null());
2569 inferred_name_ = Handle<String>();
2572 // shared_info may be null if it's not cached in full code.
2573 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2575 bool pretenure() { return Pretenure::decode(bitfield_); }
2576 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2578 bool has_duplicate_parameters() {
2579 return HasDuplicateParameters::decode(bitfield_);
2582 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2584 // This is used as a heuristic on when to eagerly compile a function
2585 // literal. We consider the following constructs as hints that the
2586 // function will be called immediately:
2587 // - (function() { ... })();
2588 // - var x = function() { ... }();
2589 bool is_parenthesized() {
2590 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2592 void set_parenthesized() {
2593 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2596 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2598 int ast_node_count() { return ast_properties_.node_count(); }
2599 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2600 void set_ast_properties(AstProperties* ast_properties) {
2601 ast_properties_ = *ast_properties;
2603 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2604 return ast_properties_.get_spec();
2606 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2607 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2608 void set_dont_optimize_reason(BailoutReason reason) {
2609 dont_optimize_reason_ = reason;
2613 FunctionLiteral(Zone* zone, const AstRawString* name,
2614 AstValueFactory* ast_value_factory, Scope* scope,
2615 ZoneList<Statement*>* body, int materialized_literal_count,
2616 int expected_property_count, int handler_count,
2617 int parameter_count, FunctionType function_type,
2618 ParameterFlag has_duplicate_parameters,
2619 IsFunctionFlag is_function,
2620 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2622 : Expression(zone, position),
2626 raw_inferred_name_(ast_value_factory->empty_string()),
2627 ast_properties_(zone),
2628 dont_optimize_reason_(kNoReason),
2629 materialized_literal_count_(materialized_literal_count),
2630 expected_property_count_(expected_property_count),
2631 handler_count_(handler_count),
2632 parameter_count_(parameter_count),
2633 function_token_position_(RelocInfo::kNoPosition) {
2634 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2635 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2636 Pretenure::encode(false) |
2637 HasDuplicateParameters::encode(has_duplicate_parameters) |
2638 IsFunction::encode(is_function) |
2639 IsParenthesized::encode(is_parenthesized) |
2640 FunctionKindBits::encode(kind);
2641 DCHECK(IsValidFunctionKind(kind));
2645 const AstRawString* raw_name_;
2646 Handle<String> name_;
2647 Handle<SharedFunctionInfo> shared_info_;
2649 ZoneList<Statement*>* body_;
2650 const AstString* raw_inferred_name_;
2651 Handle<String> inferred_name_;
2652 AstProperties ast_properties_;
2653 BailoutReason dont_optimize_reason_;
2655 int materialized_literal_count_;
2656 int expected_property_count_;
2658 int parameter_count_;
2659 int function_token_position_;
2662 class IsExpression : public BitField<bool, 0, 1> {};
2663 class IsAnonymous : public BitField<bool, 1, 1> {};
2664 class Pretenure : public BitField<bool, 2, 1> {};
2665 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2666 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2667 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2668 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2672 class ClassLiteral FINAL : public Expression {
2674 typedef ObjectLiteralProperty Property;
2676 DECLARE_NODE_TYPE(ClassLiteral)
2678 Handle<String> name() const { return raw_name_->string(); }
2679 const AstRawString* raw_name() const { return raw_name_; }
2680 Scope* scope() const { return scope_; }
2681 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2682 Expression* extends() const { return extends_; }
2683 FunctionLiteral* constructor() const { return constructor_; }
2684 ZoneList<Property*>* properties() const { return properties_; }
2685 int start_position() const { return position(); }
2686 int end_position() const { return end_position_; }
2688 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2689 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2690 BailoutId ExitId() { return BailoutId(local_id(2)); }
2692 // Return an AST id for a property that is used in simulate instructions.
2693 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2695 // Unlike other AST nodes, this number of bailout IDs allocated for an
2696 // ClassLiteral can vary, so num_ids() is not a static method.
2697 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2700 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2701 VariableProxy* class_variable_proxy, Expression* extends,
2702 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2703 int start_position, int end_position)
2704 : Expression(zone, start_position),
2707 class_variable_proxy_(class_variable_proxy),
2709 constructor_(constructor),
2710 properties_(properties),
2711 end_position_(end_position) {}
2712 static int parent_num_ids() { return Expression::num_ids(); }
2715 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2717 const AstRawString* raw_name_;
2719 VariableProxy* class_variable_proxy_;
2720 Expression* extends_;
2721 FunctionLiteral* constructor_;
2722 ZoneList<Property*>* properties_;
2727 class NativeFunctionLiteral FINAL : public Expression {
2729 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2731 Handle<String> name() const { return name_->string(); }
2732 v8::Extension* extension() const { return extension_; }
2735 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2736 v8::Extension* extension, int pos)
2737 : Expression(zone, pos), name_(name), extension_(extension) {}
2740 const AstRawString* name_;
2741 v8::Extension* extension_;
2745 class ThisFunction FINAL : public Expression {
2747 DECLARE_NODE_TYPE(ThisFunction)
2750 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2754 class SuperReference FINAL : public Expression {
2756 DECLARE_NODE_TYPE(SuperReference)
2758 VariableProxy* this_var() const { return this_var_; }
2760 static int num_ids() { return parent_num_ids() + 1; }
2761 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2763 // Type feedback information.
2764 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2765 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2766 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2768 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2769 ICSlotCache* cache) OVERRIDE {
2770 homeobject_feedback_slot_ = slot;
2772 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2774 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2775 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2776 return homeobject_feedback_slot_;
2780 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2781 : Expression(zone, pos),
2782 this_var_(this_var),
2783 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2784 DCHECK(this_var->is_this());
2786 static int parent_num_ids() { return Expression::num_ids(); }
2789 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2791 VariableProxy* this_var_;
2792 FeedbackVectorICSlot homeobject_feedback_slot_;
2796 #undef DECLARE_NODE_TYPE
2799 // ----------------------------------------------------------------------------
2800 // Regular expressions
2803 class RegExpVisitor BASE_EMBEDDED {
2805 virtual ~RegExpVisitor() { }
2806 #define MAKE_CASE(Name) \
2807 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2808 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2813 class RegExpTree : public ZoneObject {
2815 static const int kInfinity = kMaxInt;
2816 virtual ~RegExpTree() {}
2817 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2818 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2819 RegExpNode* on_success) = 0;
2820 virtual bool IsTextElement() { return false; }
2821 virtual bool IsAnchoredAtStart() { return false; }
2822 virtual bool IsAnchoredAtEnd() { return false; }
2823 virtual int min_match() = 0;
2824 virtual int max_match() = 0;
2825 // Returns the interval of registers used for captures within this
2827 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2828 virtual void AppendToText(RegExpText* text, Zone* zone);
2829 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2830 #define MAKE_ASTYPE(Name) \
2831 virtual RegExp##Name* As##Name(); \
2832 virtual bool Is##Name();
2833 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2838 class RegExpDisjunction FINAL : public RegExpTree {
2840 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2841 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2842 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2843 RegExpNode* on_success) OVERRIDE;
2844 RegExpDisjunction* AsDisjunction() OVERRIDE;
2845 Interval CaptureRegisters() OVERRIDE;
2846 bool IsDisjunction() OVERRIDE;
2847 bool IsAnchoredAtStart() OVERRIDE;
2848 bool IsAnchoredAtEnd() OVERRIDE;
2849 int min_match() OVERRIDE { return min_match_; }
2850 int max_match() OVERRIDE { return max_match_; }
2851 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2853 ZoneList<RegExpTree*>* alternatives_;
2859 class RegExpAlternative FINAL : public RegExpTree {
2861 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2862 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2863 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2864 RegExpNode* on_success) OVERRIDE;
2865 RegExpAlternative* AsAlternative() OVERRIDE;
2866 Interval CaptureRegisters() OVERRIDE;
2867 bool IsAlternative() OVERRIDE;
2868 bool IsAnchoredAtStart() OVERRIDE;
2869 bool IsAnchoredAtEnd() OVERRIDE;
2870 int min_match() OVERRIDE { return min_match_; }
2871 int max_match() OVERRIDE { return max_match_; }
2872 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2874 ZoneList<RegExpTree*>* nodes_;
2880 class RegExpAssertion FINAL : public RegExpTree {
2882 enum AssertionType {
2890 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2891 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2892 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2893 RegExpNode* on_success) OVERRIDE;
2894 RegExpAssertion* AsAssertion() OVERRIDE;
2895 bool IsAssertion() OVERRIDE;
2896 bool IsAnchoredAtStart() OVERRIDE;
2897 bool IsAnchoredAtEnd() OVERRIDE;
2898 int min_match() OVERRIDE { return 0; }
2899 int max_match() OVERRIDE { return 0; }
2900 AssertionType assertion_type() { return assertion_type_; }
2902 AssertionType assertion_type_;
2906 class CharacterSet FINAL BASE_EMBEDDED {
2908 explicit CharacterSet(uc16 standard_set_type)
2910 standard_set_type_(standard_set_type) {}
2911 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2913 standard_set_type_(0) {}
2914 ZoneList<CharacterRange>* ranges(Zone* zone);
2915 uc16 standard_set_type() { return standard_set_type_; }
2916 void set_standard_set_type(uc16 special_set_type) {
2917 standard_set_type_ = special_set_type;
2919 bool is_standard() { return standard_set_type_ != 0; }
2920 void Canonicalize();
2922 ZoneList<CharacterRange>* ranges_;
2923 // If non-zero, the value represents a standard set (e.g., all whitespace
2924 // characters) without having to expand the ranges.
2925 uc16 standard_set_type_;
2929 class RegExpCharacterClass FINAL : public RegExpTree {
2931 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2933 is_negated_(is_negated) { }
2934 explicit RegExpCharacterClass(uc16 type)
2936 is_negated_(false) { }
2937 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2938 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2939 RegExpNode* on_success) OVERRIDE;
2940 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2941 bool IsCharacterClass() OVERRIDE;
2942 bool IsTextElement() OVERRIDE { return true; }
2943 int min_match() OVERRIDE { return 1; }
2944 int max_match() OVERRIDE { return 1; }
2945 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2946 CharacterSet character_set() { return set_; }
2947 // TODO(lrn): Remove need for complex version if is_standard that
2948 // recognizes a mangled standard set and just do { return set_.is_special(); }
2949 bool is_standard(Zone* zone);
2950 // Returns a value representing the standard character set if is_standard()
2952 // Currently used values are:
2953 // s : unicode whitespace
2954 // S : unicode non-whitespace
2955 // w : ASCII word character (digit, letter, underscore)
2956 // W : non-ASCII word character
2958 // D : non-ASCII digit
2959 // . : non-unicode non-newline
2960 // * : All characters
2961 uc16 standard_type() { return set_.standard_set_type(); }
2962 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2963 bool is_negated() { return is_negated_; }
2971 class RegExpAtom FINAL : public RegExpTree {
2973 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2974 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2975 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2976 RegExpNode* on_success) OVERRIDE;
2977 RegExpAtom* AsAtom() OVERRIDE;
2978 bool IsAtom() OVERRIDE;
2979 bool IsTextElement() OVERRIDE { return true; }
2980 int min_match() OVERRIDE { return data_.length(); }
2981 int max_match() OVERRIDE { return data_.length(); }
2982 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2983 Vector<const uc16> data() { return data_; }
2984 int length() { return data_.length(); }
2986 Vector<const uc16> data_;
2990 class RegExpText FINAL : public RegExpTree {
2992 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2993 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2994 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2995 RegExpNode* on_success) OVERRIDE;
2996 RegExpText* AsText() OVERRIDE;
2997 bool IsText() OVERRIDE;
2998 bool IsTextElement() OVERRIDE { return true; }
2999 int min_match() OVERRIDE { return length_; }
3000 int max_match() OVERRIDE { return length_; }
3001 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
3002 void AddElement(TextElement elm, Zone* zone) {
3003 elements_.Add(elm, zone);
3004 length_ += elm.length();
3006 ZoneList<TextElement>* elements() { return &elements_; }
3008 ZoneList<TextElement> elements_;
3013 class RegExpQuantifier FINAL : public RegExpTree {
3015 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3016 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3020 min_match_(min * body->min_match()),
3021 quantifier_type_(type) {
3022 if (max > 0 && body->max_match() > kInfinity / max) {
3023 max_match_ = kInfinity;
3025 max_match_ = max * body->max_match();
3028 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3029 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3030 RegExpNode* on_success) OVERRIDE;
3031 static RegExpNode* ToNode(int min,
3035 RegExpCompiler* compiler,
3036 RegExpNode* on_success,
3037 bool not_at_start = false);
3038 RegExpQuantifier* AsQuantifier() OVERRIDE;
3039 Interval CaptureRegisters() OVERRIDE;
3040 bool IsQuantifier() OVERRIDE;
3041 int min_match() OVERRIDE { return min_match_; }
3042 int max_match() OVERRIDE { return max_match_; }
3043 int min() { return min_; }
3044 int max() { return max_; }
3045 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3046 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3047 bool is_greedy() { return quantifier_type_ == GREEDY; }
3048 RegExpTree* body() { return body_; }
3056 QuantifierType quantifier_type_;
3060 class RegExpCapture FINAL : public RegExpTree {
3062 explicit RegExpCapture(RegExpTree* body, int index)
3063 : body_(body), index_(index) { }
3064 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3065 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3066 RegExpNode* on_success) OVERRIDE;
3067 static RegExpNode* ToNode(RegExpTree* body,
3069 RegExpCompiler* compiler,
3070 RegExpNode* on_success);
3071 RegExpCapture* AsCapture() OVERRIDE;
3072 bool IsAnchoredAtStart() OVERRIDE;
3073 bool IsAnchoredAtEnd() OVERRIDE;
3074 Interval CaptureRegisters() OVERRIDE;
3075 bool IsCapture() OVERRIDE;
3076 int min_match() OVERRIDE { return body_->min_match(); }
3077 int max_match() OVERRIDE { return body_->max_match(); }
3078 RegExpTree* body() { return body_; }
3079 int index() { return index_; }
3080 static int StartRegister(int index) { return index * 2; }
3081 static int EndRegister(int index) { return index * 2 + 1; }
3089 class RegExpLookahead FINAL : public RegExpTree {
3091 RegExpLookahead(RegExpTree* body,
3096 is_positive_(is_positive),
3097 capture_count_(capture_count),
3098 capture_from_(capture_from) { }
3100 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3101 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3102 RegExpNode* on_success) OVERRIDE;
3103 RegExpLookahead* AsLookahead() OVERRIDE;
3104 Interval CaptureRegisters() OVERRIDE;
3105 bool IsLookahead() OVERRIDE;
3106 bool IsAnchoredAtStart() OVERRIDE;
3107 int min_match() OVERRIDE { return 0; }
3108 int max_match() OVERRIDE { return 0; }
3109 RegExpTree* body() { return body_; }
3110 bool is_positive() { return is_positive_; }
3111 int capture_count() { return capture_count_; }
3112 int capture_from() { return capture_from_; }
3122 class RegExpBackReference FINAL : public RegExpTree {
3124 explicit RegExpBackReference(RegExpCapture* capture)
3125 : capture_(capture) { }
3126 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3127 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3128 RegExpNode* on_success) OVERRIDE;
3129 RegExpBackReference* AsBackReference() OVERRIDE;
3130 bool IsBackReference() OVERRIDE;
3131 int min_match() OVERRIDE { return 0; }
3132 int max_match() OVERRIDE { return capture_->max_match(); }
3133 int index() { return capture_->index(); }
3134 RegExpCapture* capture() { return capture_; }
3136 RegExpCapture* capture_;
3140 class RegExpEmpty FINAL : public RegExpTree {
3143 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3144 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3145 RegExpNode* on_success) OVERRIDE;
3146 RegExpEmpty* AsEmpty() OVERRIDE;
3147 bool IsEmpty() OVERRIDE;
3148 int min_match() OVERRIDE { return 0; }
3149 int max_match() OVERRIDE { return 0; }
3153 // ----------------------------------------------------------------------------
3155 // - leaf node visitors are abstract.
3157 class AstVisitor BASE_EMBEDDED {
3160 virtual ~AstVisitor() {}
3162 // Stack overflow check and dynamic dispatch.
3163 virtual void Visit(AstNode* node) = 0;
3165 // Iteration left-to-right.
3166 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3167 virtual void VisitStatements(ZoneList<Statement*>* statements);
3168 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3170 // Individual AST nodes.
3171 #define DEF_VISIT(type) \
3172 virtual void Visit##type(type* node) = 0;
3173 AST_NODE_LIST(DEF_VISIT)
3178 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3180 void Visit(AstNode* node) FINAL { \
3181 if (!CheckStackOverflow()) node->Accept(this); \
3184 void SetStackOverflow() { stack_overflow_ = true; } \
3185 void ClearStackOverflow() { stack_overflow_ = false; } \
3186 bool HasStackOverflow() const { return stack_overflow_; } \
3188 bool CheckStackOverflow() { \
3189 if (stack_overflow_) return true; \
3190 StackLimitCheck check(isolate_); \
3191 if (!check.HasOverflowed()) return false; \
3192 stack_overflow_ = true; \
3197 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3198 isolate_ = isolate; \
3200 stack_overflow_ = false; \
3202 Zone* zone() { return zone_; } \
3203 Isolate* isolate() { return isolate_; } \
3205 Isolate* isolate_; \
3207 bool stack_overflow_
3210 // ----------------------------------------------------------------------------
3213 class AstNodeFactory FINAL BASE_EMBEDDED {
3215 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3216 : zone_(ast_value_factory->zone()),
3217 ast_value_factory_(ast_value_factory) {}
3219 VariableDeclaration* NewVariableDeclaration(
3220 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3221 bool is_class_declaration = false) {
3222 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos,
3223 is_class_declaration);
3226 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3228 FunctionLiteral* fun,
3231 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3234 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3238 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3241 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3242 const AstRawString* import_name,
3243 const AstRawString* module_specifier,
3244 Scope* scope, int pos) {
3245 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3246 module_specifier, scope, pos);
3249 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3252 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3255 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3257 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3260 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3261 return new (zone_) ModulePath(zone_, origin, name, pos);
3264 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3265 return new (zone_) ModuleUrl(zone_, url, pos);
3268 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3270 bool is_initializer_block,
3273 Block(zone_, labels, capacity, is_initializer_block, pos);
3276 #define STATEMENT_WITH_LABELS(NodeType) \
3277 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3278 return new (zone_) NodeType(zone_, labels, pos); \
3280 STATEMENT_WITH_LABELS(DoWhileStatement)
3281 STATEMENT_WITH_LABELS(WhileStatement)
3282 STATEMENT_WITH_LABELS(ForStatement)
3283 STATEMENT_WITH_LABELS(SwitchStatement)
3284 #undef STATEMENT_WITH_LABELS
3286 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3287 ZoneList<const AstRawString*>* labels,
3289 switch (visit_mode) {
3290 case ForEachStatement::ENUMERATE: {
3291 return new (zone_) ForInStatement(zone_, labels, pos);
3293 case ForEachStatement::ITERATE: {
3294 return new (zone_) ForOfStatement(zone_, labels, pos);
3301 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3302 return new (zone_) ModuleStatement(zone_, body, pos);
3305 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3306 return new (zone_) ExpressionStatement(zone_, expression, pos);
3309 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3310 return new (zone_) ContinueStatement(zone_, target, pos);
3313 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3314 return new (zone_) BreakStatement(zone_, target, pos);
3317 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3318 return new (zone_) ReturnStatement(zone_, expression, pos);
3321 WithStatement* NewWithStatement(Scope* scope,
3322 Expression* expression,
3323 Statement* statement,
3325 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3328 IfStatement* NewIfStatement(Expression* condition,
3329 Statement* then_statement,
3330 Statement* else_statement,
3333 IfStatement(zone_, condition, then_statement, else_statement, pos);
3336 TryCatchStatement* NewTryCatchStatement(int index,
3342 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3343 variable, catch_block, pos);
3346 TryFinallyStatement* NewTryFinallyStatement(int index,
3348 Block* finally_block,
3351 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3354 DebuggerStatement* NewDebuggerStatement(int pos) {
3355 return new (zone_) DebuggerStatement(zone_, pos);
3358 EmptyStatement* NewEmptyStatement(int pos) {
3359 return new(zone_) EmptyStatement(zone_, pos);
3362 CaseClause* NewCaseClause(
3363 Expression* label, ZoneList<Statement*>* statements, int pos) {
3364 return new (zone_) CaseClause(zone_, label, statements, pos);
3367 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3369 Literal(zone_, ast_value_factory_->NewString(string), pos);
3372 // A JavaScript symbol (ECMA-262 edition 6).
3373 Literal* NewSymbolLiteral(const char* name, int pos) {
3374 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3377 Literal* NewNumberLiteral(double number, int pos) {
3379 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3382 Literal* NewSmiLiteral(int number, int pos) {
3383 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3386 Literal* NewBooleanLiteral(bool b, int pos) {
3387 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3390 Literal* NewNullLiteral(int pos) {
3391 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3394 Literal* NewUndefinedLiteral(int pos) {
3395 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3398 Literal* NewTheHoleLiteral(int pos) {
3399 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3402 ObjectLiteral* NewObjectLiteral(
3403 ZoneList<ObjectLiteral::Property*>* properties,
3405 int boilerplate_properties,
3408 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3409 boilerplate_properties, has_function, pos);
3412 ObjectLiteral::Property* NewObjectLiteralProperty(
3413 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3414 bool is_static, bool is_computed_name) {
3416 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3419 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3422 bool is_computed_name) {
3423 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3424 is_static, is_computed_name);
3427 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3428 const AstRawString* flags,
3431 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3434 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3437 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3440 VariableProxy* NewVariableProxy(Variable* var,
3441 int start_position = RelocInfo::kNoPosition,
3442 int end_position = RelocInfo::kNoPosition) {
3443 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3446 VariableProxy* NewVariableProxy(const AstRawString* name,
3447 Variable::Kind variable_kind,
3448 int start_position = RelocInfo::kNoPosition,
3449 int end_position = RelocInfo::kNoPosition) {
3451 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3454 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3455 return new (zone_) Property(zone_, obj, key, pos);
3458 Call* NewCall(Expression* expression,
3459 ZoneList<Expression*>* arguments,
3461 return new (zone_) Call(zone_, expression, arguments, pos);
3464 CallNew* NewCallNew(Expression* expression,
3465 ZoneList<Expression*>* arguments,
3467 return new (zone_) CallNew(zone_, expression, arguments, pos);
3470 CallRuntime* NewCallRuntime(const AstRawString* name,
3471 const Runtime::Function* function,
3472 ZoneList<Expression*>* arguments,
3474 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3477 UnaryOperation* NewUnaryOperation(Token::Value op,
3478 Expression* expression,
3480 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3483 BinaryOperation* NewBinaryOperation(Token::Value op,
3487 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3490 CountOperation* NewCountOperation(Token::Value op,
3494 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3497 CompareOperation* NewCompareOperation(Token::Value op,
3501 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3504 Spread* NewSpread(Expression* expression, int pos) {
3505 return new (zone_) Spread(zone_, expression, pos);
3508 Conditional* NewConditional(Expression* condition,
3509 Expression* then_expression,
3510 Expression* else_expression,
3512 return new (zone_) Conditional(zone_, condition, then_expression,
3513 else_expression, position);
3516 Assignment* NewAssignment(Token::Value op,
3520 DCHECK(Token::IsAssignmentOp(op));
3521 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3522 if (assign->is_compound()) {
3523 DCHECK(Token::IsAssignmentOp(op));
3524 assign->binary_operation_ =
3525 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3530 Yield* NewYield(Expression *generator_object,
3531 Expression* expression,
3532 Yield::Kind yield_kind,
3534 if (!expression) expression = NewUndefinedLiteral(pos);
3536 Yield(zone_, generator_object, expression, yield_kind, pos);
3539 Throw* NewThrow(Expression* exception, int pos) {
3540 return new (zone_) Throw(zone_, exception, pos);
3543 FunctionLiteral* NewFunctionLiteral(
3544 const AstRawString* name, AstValueFactory* ast_value_factory,
3545 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3546 int expected_property_count, int handler_count, int parameter_count,
3547 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3548 FunctionLiteral::FunctionType function_type,
3549 FunctionLiteral::IsFunctionFlag is_function,
3550 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3552 return new (zone_) FunctionLiteral(
3553 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3554 expected_property_count, handler_count, parameter_count, function_type,
3555 has_duplicate_parameters, is_function, is_parenthesized, kind,
3559 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3560 VariableProxy* proxy, Expression* extends,
3561 FunctionLiteral* constructor,
3562 ZoneList<ObjectLiteral::Property*>* properties,
3563 int start_position, int end_position) {
3565 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3566 properties, start_position, end_position);
3569 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3570 v8::Extension* extension,
3572 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3575 ThisFunction* NewThisFunction(int pos) {
3576 return new (zone_) ThisFunction(zone_, pos);
3579 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3580 return new (zone_) SuperReference(zone_, this_var, pos);
3585 AstValueFactory* ast_value_factory_;
3589 } } // namespace v8::internal