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) \
99 #define AST_NODE_LIST(V) \
100 DECLARATION_NODE_LIST(V) \
101 MODULE_NODE_LIST(V) \
102 STATEMENT_NODE_LIST(V) \
103 EXPRESSION_NODE_LIST(V)
105 // Forward declarations
106 class AstNodeFactory;
110 class BreakableStatement;
112 class IterationStatement;
113 class MaterializedLiteral;
115 class TypeFeedbackOracle;
117 class RegExpAlternative;
118 class RegExpAssertion;
120 class RegExpBackReference;
122 class RegExpCharacterClass;
123 class RegExpCompiler;
124 class RegExpDisjunction;
126 class RegExpLookahead;
127 class RegExpQuantifier;
130 #define DEF_FORWARD_DECLARATION(type) class type;
131 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
132 #undef DEF_FORWARD_DECLARATION
135 // Typedef only introduced to avoid unreadable code.
136 // Please do appreciate the required space in "> >".
137 typedef ZoneList<Handle<String> > ZoneStringList;
138 typedef ZoneList<Handle<Object> > ZoneObjectList;
141 #define DECLARE_NODE_TYPE(type) \
142 void Accept(AstVisitor* v) OVERRIDE; \
143 AstNode::NodeType node_type() const FINAL { return AstNode::k##type; } \
144 friend class AstNodeFactory;
147 enum AstPropertiesFlag {
154 class FeedbackVectorRequirements {
156 FeedbackVectorRequirements(int slots, int ic_slots)
157 : slots_(slots), ic_slots_(ic_slots) {}
159 int slots() const { return slots_; }
160 int ic_slots() const { return ic_slots_; }
168 class VariableICSlotPair FINAL {
170 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
171 : variable_(variable), slot_(slot) {}
173 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
175 Variable* variable() const { return variable_; }
176 FeedbackVectorICSlot slot() const { return slot_; }
180 FeedbackVectorICSlot slot_;
184 typedef List<VariableICSlotPair> ICSlotCache;
187 class AstProperties FINAL BASE_EMBEDDED {
189 class Flags : public EnumSet<AstPropertiesFlag, int> {};
191 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
193 Flags* flags() { return &flags_; }
194 int node_count() { return node_count_; }
195 void add_node_count(int count) { node_count_ += count; }
197 int slots() const { return spec_.slots(); }
198 void increase_slots(int count) { spec_.increase_slots(count); }
200 int ic_slots() const { return spec_.ic_slots(); }
201 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
202 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
203 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
208 ZoneFeedbackVectorSpec spec_;
212 class AstNode: public ZoneObject {
214 #define DECLARE_TYPE_ENUM(type) k##type,
216 AST_NODE_LIST(DECLARE_TYPE_ENUM)
219 #undef DECLARE_TYPE_ENUM
221 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
223 explicit AstNode(int position): position_(position) {}
224 virtual ~AstNode() {}
226 virtual void Accept(AstVisitor* v) = 0;
227 virtual NodeType node_type() const = 0;
228 int position() const { return position_; }
230 // Type testing & conversion functions overridden by concrete subclasses.
231 #define DECLARE_NODE_FUNCTIONS(type) \
232 bool Is##type() const { return node_type() == AstNode::k##type; } \
234 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
236 const type* As##type() const { \
237 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
239 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
240 #undef DECLARE_NODE_FUNCTIONS
242 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
243 virtual IterationStatement* AsIterationStatement() { return NULL; }
244 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
246 // The interface for feedback slots, with default no-op implementations for
247 // node types which don't actually have this. Note that this is conceptually
248 // not really nice, but multiple inheritance would introduce yet another
249 // vtable entry per node, something we don't want for space reasons.
250 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
251 Isolate* isolate, const ICSlotCache* cache) {
252 return FeedbackVectorRequirements(0, 0);
254 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
255 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
256 ICSlotCache* cache) {
259 // Each ICSlot stores a kind of IC which the participating node should know.
260 virtual Code::Kind FeedbackICSlotKind(int index) {
262 return Code::NUMBER_OF_KINDS;
266 // Hidden to prevent accidental usage. It would have to load the
267 // current zone from the TLS.
268 void* operator new(size_t size);
270 friend class CaseClause; // Generates AST IDs.
276 class Statement : public AstNode {
278 explicit Statement(Zone* zone, int position) : AstNode(position) {}
280 bool IsEmpty() { return AsEmptyStatement() != NULL; }
281 virtual bool IsJump() const { return false; }
285 class SmallMapList FINAL {
288 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
290 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
291 void Clear() { list_.Clear(); }
292 void Sort() { list_.Sort(); }
294 bool is_empty() const { return list_.is_empty(); }
295 int length() const { return list_.length(); }
297 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
298 if (!Map::TryUpdate(map).ToHandle(&map)) return;
299 for (int i = 0; i < length(); ++i) {
300 if (at(i).is_identical_to(map)) return;
305 void FilterForPossibleTransitions(Map* root_map) {
306 for (int i = list_.length() - 1; i >= 0; i--) {
307 if (at(i)->FindRootMap() != root_map) {
308 list_.RemoveElement(list_.at(i));
313 void Add(Handle<Map> handle, Zone* zone) {
314 list_.Add(handle.location(), zone);
317 Handle<Map> at(int i) const {
318 return Handle<Map>(list_.at(i));
321 Handle<Map> first() const { return at(0); }
322 Handle<Map> last() const { return at(length() - 1); }
325 // The list stores pointers to Map*, that is Map**, so it's GC safe.
326 SmallPointerList<Map*> list_;
328 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
332 class Expression : public AstNode {
335 // Not assigned a context yet, or else will not be visited during
338 // Evaluated for its side effects.
340 // Evaluated for its value (and side effects).
342 // Evaluated for control flow (and side effects).
346 virtual bool IsValidReferenceExpression() const { return false; }
348 // Helpers for ToBoolean conversion.
349 virtual bool ToBooleanIsTrue() const { return false; }
350 virtual bool ToBooleanIsFalse() const { return false; }
352 // Symbols that cannot be parsed as array indices are considered property
353 // names. We do not treat symbols that can be array indexes as property
354 // names because [] for string objects is handled only by keyed ICs.
355 virtual bool IsPropertyName() const { return false; }
357 // True iff the expression is a literal represented as a smi.
358 bool IsSmiLiteral() const;
360 // True iff the expression is a string literal.
361 bool IsStringLiteral() const;
363 // True iff the expression is the null literal.
364 bool IsNullLiteral() const;
366 // True if we can prove that the expression is the undefined literal.
367 bool IsUndefinedLiteral(Isolate* isolate) const;
369 // Expression type bounds
370 Bounds bounds() const { return bounds_; }
371 void set_bounds(Bounds bounds) { bounds_ = bounds; }
373 // Whether the expression is parenthesized
374 bool is_parenthesized() const {
375 return IsParenthesizedField::decode(bit_field_);
377 bool is_multi_parenthesized() const {
378 return IsMultiParenthesizedField::decode(bit_field_);
380 void increase_parenthesization_level() {
382 IsMultiParenthesizedField::update(bit_field_, is_parenthesized());
383 bit_field_ = IsParenthesizedField::update(bit_field_, true);
386 // Type feedback information for assignments and properties.
387 virtual bool IsMonomorphic() {
391 virtual SmallMapList* GetReceiverTypes() {
395 virtual KeyedAccessStoreMode GetStoreMode() const {
397 return STANDARD_STORE;
399 virtual IcCheckType GetKeyType() const {
404 // TODO(rossberg): this should move to its own AST node eventually.
405 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
406 byte to_boolean_types() const {
407 return ToBooleanTypesField::decode(bit_field_);
410 void set_base_id(int id) { base_id_ = id; }
411 static int num_ids() { return parent_num_ids() + 2; }
412 BailoutId id() const { return BailoutId(local_id(0)); }
413 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
416 Expression(Zone* zone, int pos)
418 base_id_(BailoutId::None().ToInt()),
419 bounds_(Bounds::Unbounded(zone)),
421 static int parent_num_ids() { return 0; }
422 void set_to_boolean_types(byte types) {
423 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
426 int base_id() const {
427 DCHECK(!BailoutId(base_id_).IsNone());
432 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
436 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
437 class IsParenthesizedField : public BitField16<bool, 8, 1> {};
438 class IsMultiParenthesizedField : public BitField16<bool, 9, 1> {};
440 // Ends with 16-bit field; deriving classes in turn begin with
441 // 16-bit fields for optimum packing efficiency.
445 class BreakableStatement : public Statement {
448 TARGET_FOR_ANONYMOUS,
449 TARGET_FOR_NAMED_ONLY
452 // The labels associated with this statement. May be NULL;
453 // if it is != NULL, guaranteed to contain at least one entry.
454 ZoneList<const AstRawString*>* labels() const { return labels_; }
456 // Type testing & conversion.
457 BreakableStatement* AsBreakableStatement() FINAL { return this; }
460 Label* break_target() { return &break_target_; }
463 bool is_target_for_anonymous() const {
464 return breakable_type_ == TARGET_FOR_ANONYMOUS;
467 void set_base_id(int id) { base_id_ = id; }
468 static int num_ids() { return parent_num_ids() + 2; }
469 BailoutId EntryId() const { return BailoutId(local_id(0)); }
470 BailoutId ExitId() const { return BailoutId(local_id(1)); }
473 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
474 BreakableType breakable_type, int position)
475 : Statement(zone, position),
477 breakable_type_(breakable_type),
478 base_id_(BailoutId::None().ToInt()) {
479 DCHECK(labels == NULL || labels->length() > 0);
481 static int parent_num_ids() { return 0; }
483 int base_id() const {
484 DCHECK(!BailoutId(base_id_).IsNone());
489 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
491 ZoneList<const AstRawString*>* labels_;
492 BreakableType breakable_type_;
498 class Block FINAL : public BreakableStatement {
500 DECLARE_NODE_TYPE(Block)
502 void AddStatement(Statement* statement, Zone* zone) {
503 statements_.Add(statement, zone);
506 ZoneList<Statement*>* statements() { return &statements_; }
507 bool is_initializer_block() const { return is_initializer_block_; }
509 static int num_ids() { return parent_num_ids() + 1; }
510 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
512 bool IsJump() const OVERRIDE {
513 return !statements_.is_empty() && statements_.last()->IsJump()
514 && labels() == NULL; // Good enough as an approximation...
517 Scope* scope() const { return scope_; }
518 void set_scope(Scope* scope) { scope_ = scope; }
521 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
522 bool is_initializer_block, int pos)
523 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
524 statements_(capacity, zone),
525 is_initializer_block_(is_initializer_block),
527 static int parent_num_ids() { return BreakableStatement::num_ids(); }
530 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
532 ZoneList<Statement*> statements_;
533 bool is_initializer_block_;
538 class Declaration : public AstNode {
540 VariableProxy* proxy() const { return proxy_; }
541 VariableMode mode() const { return mode_; }
542 Scope* scope() const { return scope_; }
543 virtual InitializationFlag initialization() const = 0;
544 virtual bool IsInlineable() const;
547 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
549 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
550 DCHECK(IsDeclaredVariableMode(mode));
555 VariableProxy* proxy_;
557 // Nested scope from which the declaration originated.
562 class VariableDeclaration FINAL : public Declaration {
564 DECLARE_NODE_TYPE(VariableDeclaration)
566 InitializationFlag initialization() const OVERRIDE {
567 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
571 VariableDeclaration(Zone* zone,
572 VariableProxy* proxy,
576 : Declaration(zone, proxy, mode, scope, pos) {
581 class FunctionDeclaration FINAL : public Declaration {
583 DECLARE_NODE_TYPE(FunctionDeclaration)
585 FunctionLiteral* fun() const { return fun_; }
586 InitializationFlag initialization() const OVERRIDE {
587 return kCreatedInitialized;
589 bool IsInlineable() const OVERRIDE;
592 FunctionDeclaration(Zone* zone,
593 VariableProxy* proxy,
595 FunctionLiteral* fun,
598 : Declaration(zone, proxy, mode, scope, pos),
600 DCHECK(mode == VAR || mode == LET || mode == CONST);
605 FunctionLiteral* fun_;
609 class ModuleDeclaration FINAL : public Declaration {
611 DECLARE_NODE_TYPE(ModuleDeclaration)
613 Module* module() const { return module_; }
614 InitializationFlag initialization() const OVERRIDE {
615 return kCreatedInitialized;
619 ModuleDeclaration(Zone* zone, VariableProxy* proxy, Module* module,
620 Scope* scope, int pos)
621 : Declaration(zone, proxy, CONST, scope, pos), module_(module) {}
628 class ImportDeclaration FINAL : public Declaration {
630 DECLARE_NODE_TYPE(ImportDeclaration)
632 const AstRawString* import_name() const { return import_name_; }
633 const AstRawString* module_specifier() const { return module_specifier_; }
634 void set_module_specifier(const AstRawString* module_specifier) {
635 DCHECK(module_specifier_ == NULL);
636 module_specifier_ = module_specifier;
638 InitializationFlag initialization() const OVERRIDE {
639 return kNeedsInitialization;
643 ImportDeclaration(Zone* zone, VariableProxy* proxy,
644 const AstRawString* import_name,
645 const AstRawString* module_specifier, Scope* scope, int pos)
646 : Declaration(zone, proxy, IMPORT, scope, pos),
647 import_name_(import_name),
648 module_specifier_(module_specifier) {}
651 const AstRawString* import_name_;
652 const AstRawString* module_specifier_;
656 class ExportDeclaration FINAL : public Declaration {
658 DECLARE_NODE_TYPE(ExportDeclaration)
660 InitializationFlag initialization() const OVERRIDE {
661 return kCreatedInitialized;
665 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
666 : Declaration(zone, proxy, LET, scope, pos) {}
670 class Module : public AstNode {
672 ModuleDescriptor* descriptor() const { return descriptor_; }
673 Block* body() const { return body_; }
676 Module(Zone* zone, int pos)
677 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
678 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
679 : AstNode(pos), descriptor_(descriptor), body_(body) {}
682 ModuleDescriptor* descriptor_;
687 class ModuleLiteral FINAL : public Module {
689 DECLARE_NODE_TYPE(ModuleLiteral)
692 ModuleLiteral(Zone* zone, Block* body, ModuleDescriptor* descriptor, int pos)
693 : Module(zone, descriptor, pos, body) {}
697 class ModulePath FINAL : public Module {
699 DECLARE_NODE_TYPE(ModulePath)
701 Module* module() const { return module_; }
702 Handle<String> name() const { return name_->string(); }
705 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
706 : Module(zone, pos), module_(module), name_(name) {}
710 const AstRawString* name_;
714 class ModuleUrl FINAL : public Module {
716 DECLARE_NODE_TYPE(ModuleUrl)
718 Handle<String> url() const { return url_; }
721 ModuleUrl(Zone* zone, Handle<String> url, int pos)
722 : Module(zone, pos), url_(url) {
730 class ModuleStatement FINAL : public Statement {
732 DECLARE_NODE_TYPE(ModuleStatement)
734 Block* body() const { return body_; }
737 ModuleStatement(Zone* zone, Block* body, int pos)
738 : Statement(zone, pos), body_(body) {}
745 class IterationStatement : public BreakableStatement {
747 // Type testing & conversion.
748 IterationStatement* AsIterationStatement() FINAL { return this; }
750 Statement* body() const { return body_; }
752 static int num_ids() { return parent_num_ids() + 1; }
753 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
754 virtual BailoutId ContinueId() const = 0;
755 virtual BailoutId StackCheckId() const = 0;
758 Label* continue_target() { return &continue_target_; }
761 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
762 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
764 static int parent_num_ids() { return BreakableStatement::num_ids(); }
765 void Initialize(Statement* body) { body_ = body; }
768 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
771 Label continue_target_;
775 class DoWhileStatement FINAL : public IterationStatement {
777 DECLARE_NODE_TYPE(DoWhileStatement)
779 void Initialize(Expression* cond, Statement* body) {
780 IterationStatement::Initialize(body);
784 Expression* cond() const { return cond_; }
786 static int num_ids() { return parent_num_ids() + 2; }
787 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
788 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
789 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
792 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
793 : IterationStatement(zone, labels, pos), cond_(NULL) {}
794 static int parent_num_ids() { return IterationStatement::num_ids(); }
797 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
803 class WhileStatement FINAL : public IterationStatement {
805 DECLARE_NODE_TYPE(WhileStatement)
807 void Initialize(Expression* cond, Statement* body) {
808 IterationStatement::Initialize(body);
812 Expression* cond() const { return cond_; }
814 static int num_ids() { return parent_num_ids() + 1; }
815 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
816 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
817 BailoutId BodyId() const { return BailoutId(local_id(0)); }
820 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
821 : IterationStatement(zone, labels, pos), cond_(NULL) {}
822 static int parent_num_ids() { return IterationStatement::num_ids(); }
825 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
831 class ForStatement FINAL : public IterationStatement {
833 DECLARE_NODE_TYPE(ForStatement)
835 void Initialize(Statement* init,
839 IterationStatement::Initialize(body);
845 Statement* init() const { return init_; }
846 Expression* cond() const { return cond_; }
847 Statement* next() const { return next_; }
849 static int num_ids() { return parent_num_ids() + 2; }
850 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
851 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
852 BailoutId BodyId() const { return BailoutId(local_id(1)); }
855 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
856 : IterationStatement(zone, labels, pos),
860 static int parent_num_ids() { return IterationStatement::num_ids(); }
863 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
871 class ForEachStatement : public IterationStatement {
874 ENUMERATE, // for (each in subject) body;
875 ITERATE // for (each of subject) body;
878 void Initialize(Expression* each, Expression* subject, Statement* body) {
879 IterationStatement::Initialize(body);
884 Expression* each() const { return each_; }
885 Expression* subject() const { return subject_; }
888 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
889 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
893 Expression* subject_;
897 class ForInStatement FINAL : public ForEachStatement {
899 DECLARE_NODE_TYPE(ForInStatement)
901 Expression* enumerable() const {
905 // Type feedback information.
906 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
907 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
908 return FeedbackVectorRequirements(1, 0);
910 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
911 for_in_feedback_slot_ = slot;
914 FeedbackVectorSlot ForInFeedbackSlot() {
915 DCHECK(!for_in_feedback_slot_.IsInvalid());
916 return for_in_feedback_slot_;
919 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
920 ForInType for_in_type() const { return for_in_type_; }
921 void set_for_in_type(ForInType type) { for_in_type_ = type; }
923 static int num_ids() { return parent_num_ids() + 5; }
924 BailoutId BodyId() const { return BailoutId(local_id(0)); }
925 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
926 BailoutId EnumId() const { return BailoutId(local_id(2)); }
927 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
928 BailoutId AssignmentId() const { return BailoutId(local_id(4)); }
929 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
930 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
933 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
934 : ForEachStatement(zone, labels, pos),
935 for_in_type_(SLOW_FOR_IN),
936 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
937 static int parent_num_ids() { return ForEachStatement::num_ids(); }
940 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
942 ForInType for_in_type_;
943 FeedbackVectorSlot for_in_feedback_slot_;
947 class ForOfStatement FINAL : public ForEachStatement {
949 DECLARE_NODE_TYPE(ForOfStatement)
951 void Initialize(Expression* each,
954 Expression* assign_iterator,
955 Expression* next_result,
956 Expression* result_done,
957 Expression* assign_each) {
958 ForEachStatement::Initialize(each, subject, body);
959 assign_iterator_ = assign_iterator;
960 next_result_ = next_result;
961 result_done_ = result_done;
962 assign_each_ = assign_each;
965 Expression* iterable() const {
969 // iterator = subject[Symbol.iterator]()
970 Expression* assign_iterator() const {
971 return assign_iterator_;
974 // result = iterator.next() // with type check
975 Expression* next_result() const {
980 Expression* result_done() const {
984 // each = result.value
985 Expression* assign_each() const {
989 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
990 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
992 static int num_ids() { return parent_num_ids() + 1; }
993 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
996 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
997 : ForEachStatement(zone, labels, pos),
998 assign_iterator_(NULL),
1001 assign_each_(NULL) {}
1002 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1005 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1007 Expression* assign_iterator_;
1008 Expression* next_result_;
1009 Expression* result_done_;
1010 Expression* assign_each_;
1014 class ExpressionStatement FINAL : public Statement {
1016 DECLARE_NODE_TYPE(ExpressionStatement)
1018 void set_expression(Expression* e) { expression_ = e; }
1019 Expression* expression() const { return expression_; }
1020 bool IsJump() const OVERRIDE { return expression_->IsThrow(); }
1023 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1024 : Statement(zone, pos), expression_(expression) { }
1027 Expression* expression_;
1031 class JumpStatement : public Statement {
1033 bool IsJump() const FINAL { return true; }
1036 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1040 class ContinueStatement FINAL : public JumpStatement {
1042 DECLARE_NODE_TYPE(ContinueStatement)
1044 IterationStatement* target() const { return target_; }
1047 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1048 : JumpStatement(zone, pos), target_(target) { }
1051 IterationStatement* target_;
1055 class BreakStatement FINAL : public JumpStatement {
1057 DECLARE_NODE_TYPE(BreakStatement)
1059 BreakableStatement* target() const { return target_; }
1062 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1063 : JumpStatement(zone, pos), target_(target) { }
1066 BreakableStatement* target_;
1070 class ReturnStatement FINAL : public JumpStatement {
1072 DECLARE_NODE_TYPE(ReturnStatement)
1074 Expression* expression() const { return expression_; }
1077 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1078 : JumpStatement(zone, pos), expression_(expression) { }
1081 Expression* expression_;
1085 class WithStatement FINAL : public Statement {
1087 DECLARE_NODE_TYPE(WithStatement)
1089 Scope* scope() { return scope_; }
1090 Expression* expression() const { return expression_; }
1091 Statement* statement() const { return statement_; }
1093 void set_base_id(int id) { base_id_ = id; }
1094 static int num_ids() { return parent_num_ids() + 1; }
1095 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1098 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1099 Statement* statement, int pos)
1100 : Statement(zone, pos),
1102 expression_(expression),
1103 statement_(statement),
1104 base_id_(BailoutId::None().ToInt()) {}
1105 static int parent_num_ids() { return 0; }
1107 int base_id() const {
1108 DCHECK(!BailoutId(base_id_).IsNone());
1113 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1116 Expression* expression_;
1117 Statement* statement_;
1122 class CaseClause FINAL : public Expression {
1124 DECLARE_NODE_TYPE(CaseClause)
1126 bool is_default() const { return label_ == NULL; }
1127 Expression* label() const {
1128 CHECK(!is_default());
1131 Label* body_target() { return &body_target_; }
1132 ZoneList<Statement*>* statements() const { return statements_; }
1134 static int num_ids() { return parent_num_ids() + 2; }
1135 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1136 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1138 Type* compare_type() { return compare_type_; }
1139 void set_compare_type(Type* type) { compare_type_ = type; }
1142 static int parent_num_ids() { return Expression::num_ids(); }
1145 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1147 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1151 ZoneList<Statement*>* statements_;
1152 Type* compare_type_;
1156 class SwitchStatement FINAL : public BreakableStatement {
1158 DECLARE_NODE_TYPE(SwitchStatement)
1160 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1165 Expression* tag() const { return tag_; }
1166 ZoneList<CaseClause*>* cases() const { return cases_; }
1169 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1170 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1176 ZoneList<CaseClause*>* cases_;
1180 // If-statements always have non-null references to their then- and
1181 // else-parts. When parsing if-statements with no explicit else-part,
1182 // the parser implicitly creates an empty statement. Use the
1183 // HasThenStatement() and HasElseStatement() functions to check if a
1184 // given if-statement has a then- or an else-part containing code.
1185 class IfStatement FINAL : public Statement {
1187 DECLARE_NODE_TYPE(IfStatement)
1189 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1190 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1192 Expression* condition() const { return condition_; }
1193 Statement* then_statement() const { return then_statement_; }
1194 Statement* else_statement() const { return else_statement_; }
1196 bool IsJump() const OVERRIDE {
1197 return HasThenStatement() && then_statement()->IsJump()
1198 && HasElseStatement() && else_statement()->IsJump();
1201 void set_base_id(int id) { base_id_ = id; }
1202 static int num_ids() { return parent_num_ids() + 3; }
1203 BailoutId IfId() const { return BailoutId(local_id(0)); }
1204 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1205 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1208 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1209 Statement* else_statement, int pos)
1210 : Statement(zone, pos),
1211 condition_(condition),
1212 then_statement_(then_statement),
1213 else_statement_(else_statement),
1214 base_id_(BailoutId::None().ToInt()) {}
1215 static int parent_num_ids() { return 0; }
1217 int base_id() const {
1218 DCHECK(!BailoutId(base_id_).IsNone());
1223 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1225 Expression* condition_;
1226 Statement* then_statement_;
1227 Statement* else_statement_;
1232 class TryStatement : public Statement {
1234 int index() const { return index_; }
1235 Block* try_block() const { return try_block_; }
1238 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1239 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1242 // Unique (per-function) index of this handler. This is not an AST ID.
1249 class TryCatchStatement FINAL : public TryStatement {
1251 DECLARE_NODE_TYPE(TryCatchStatement)
1253 Scope* scope() { return scope_; }
1254 Variable* variable() { return variable_; }
1255 Block* catch_block() const { return catch_block_; }
1258 TryCatchStatement(Zone* zone,
1265 : TryStatement(zone, index, try_block, pos),
1267 variable_(variable),
1268 catch_block_(catch_block) {
1273 Variable* variable_;
1274 Block* catch_block_;
1278 class TryFinallyStatement FINAL : public TryStatement {
1280 DECLARE_NODE_TYPE(TryFinallyStatement)
1282 Block* finally_block() const { return finally_block_; }
1285 TryFinallyStatement(
1286 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1287 : TryStatement(zone, index, try_block, pos),
1288 finally_block_(finally_block) { }
1291 Block* finally_block_;
1295 class DebuggerStatement FINAL : public Statement {
1297 DECLARE_NODE_TYPE(DebuggerStatement)
1299 void set_base_id(int id) { base_id_ = id; }
1300 static int num_ids() { return parent_num_ids() + 1; }
1301 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1304 explicit DebuggerStatement(Zone* zone, int pos)
1305 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1306 static int parent_num_ids() { return 0; }
1308 int base_id() const {
1309 DCHECK(!BailoutId(base_id_).IsNone());
1314 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1320 class EmptyStatement FINAL : public Statement {
1322 DECLARE_NODE_TYPE(EmptyStatement)
1325 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1329 class Literal FINAL : public Expression {
1331 DECLARE_NODE_TYPE(Literal)
1333 bool IsPropertyName() const OVERRIDE { return value_->IsPropertyName(); }
1335 Handle<String> AsPropertyName() {
1336 DCHECK(IsPropertyName());
1337 return Handle<String>::cast(value());
1340 const AstRawString* AsRawPropertyName() {
1341 DCHECK(IsPropertyName());
1342 return value_->AsString();
1345 bool ToBooleanIsTrue() const OVERRIDE { return value()->BooleanValue(); }
1346 bool ToBooleanIsFalse() const OVERRIDE { return !value()->BooleanValue(); }
1348 Handle<Object> value() const { return value_->value(); }
1349 const AstValue* raw_value() const { return value_; }
1351 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1352 // only for string and number literals!
1354 static bool Match(void* literal1, void* literal2);
1356 static int num_ids() { return parent_num_ids() + 1; }
1357 TypeFeedbackId LiteralFeedbackId() const {
1358 return TypeFeedbackId(local_id(0));
1362 Literal(Zone* zone, const AstValue* value, int position)
1363 : Expression(zone, position), value_(value) {}
1364 static int parent_num_ids() { return Expression::num_ids(); }
1367 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1369 const AstValue* value_;
1373 // Base class for literals that needs space in the corresponding JSFunction.
1374 class MaterializedLiteral : public Expression {
1376 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1378 int literal_index() { return literal_index_; }
1381 // only callable after initialization.
1382 DCHECK(depth_ >= 1);
1387 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1388 : Expression(zone, pos),
1389 literal_index_(literal_index),
1393 // A materialized literal is simple if the values consist of only
1394 // constants and simple object and array literals.
1395 bool is_simple() const { return is_simple_; }
1396 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1397 friend class CompileTimeValue;
1399 void set_depth(int depth) {
1404 // Populate the constant properties/elements fixed array.
1405 void BuildConstants(Isolate* isolate);
1406 friend class ArrayLiteral;
1407 friend class ObjectLiteral;
1409 // If the expression is a literal, return the literal value;
1410 // if the expression is a materialized literal and is simple return a
1411 // compile time value as encoded by CompileTimeValue::GetValue().
1412 // Otherwise, return undefined literal as the placeholder
1413 // in the object literal boilerplate.
1414 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1423 // Property is used for passing information
1424 // about an object literal's properties from the parser
1425 // to the code generator.
1426 class ObjectLiteralProperty FINAL : public ZoneObject {
1429 CONSTANT, // Property with constant value (compile time).
1430 COMPUTED, // Property with computed value (execution time).
1431 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1432 GETTER, SETTER, // Property is an accessor function.
1433 PROTOTYPE // Property is __proto__.
1436 Expression* key() { return key_; }
1437 Expression* value() { return value_; }
1438 Kind kind() { return kind_; }
1440 // Type feedback information.
1441 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1442 Handle<Map> GetReceiverType() { return receiver_type_; }
1444 bool IsCompileTimeValue();
1446 void set_emit_store(bool emit_store);
1449 bool is_static() const { return is_static_; }
1450 bool is_computed_name() const { return is_computed_name_; }
1452 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1455 friend class AstNodeFactory;
1457 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1458 bool is_static, bool is_computed_name);
1459 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1460 Expression* value, bool is_static,
1461 bool is_computed_name);
1469 bool is_computed_name_;
1470 Handle<Map> receiver_type_;
1474 // An object literal has a boilerplate object that is used
1475 // for minimizing the work when constructing it at runtime.
1476 class ObjectLiteral FINAL : public MaterializedLiteral {
1478 typedef ObjectLiteralProperty Property;
1480 DECLARE_NODE_TYPE(ObjectLiteral)
1482 Handle<FixedArray> constant_properties() const {
1483 return constant_properties_;
1485 ZoneList<Property*>* properties() const { return properties_; }
1486 bool fast_elements() const { return fast_elements_; }
1487 bool may_store_doubles() const { return may_store_doubles_; }
1488 bool has_function() const { return has_function_; }
1490 // Decide if a property should be in the object boilerplate.
1491 static bool IsBoilerplateProperty(Property* property);
1493 // Populate the constant properties fixed array.
1494 void BuildConstantProperties(Isolate* isolate);
1496 // Mark all computed expressions that are bound to a key that
1497 // is shadowed by a later occurrence of the same key. For the
1498 // marked expressions, no store code is emitted.
1499 void CalculateEmitStore(Zone* zone);
1501 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1502 int ComputeFlags() const {
1503 int flags = fast_elements() ? kFastElements : kNoFlags;
1504 flags |= has_function() ? kHasFunction : kNoFlags;
1511 kHasFunction = 1 << 1
1514 struct Accessors: public ZoneObject {
1515 Accessors() : getter(NULL), setter(NULL) {}
1520 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1522 // Return an AST id for a property that is used in simulate instructions.
1523 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1525 // Unlike other AST nodes, this number of bailout IDs allocated for an
1526 // ObjectLiteral can vary, so num_ids() is not a static method.
1527 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1530 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1531 int boilerplate_properties, bool has_function, int pos)
1532 : MaterializedLiteral(zone, literal_index, pos),
1533 properties_(properties),
1534 boilerplate_properties_(boilerplate_properties),
1535 fast_elements_(false),
1536 may_store_doubles_(false),
1537 has_function_(has_function) {}
1538 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1541 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1542 Handle<FixedArray> constant_properties_;
1543 ZoneList<Property*>* properties_;
1544 int boilerplate_properties_;
1545 bool fast_elements_;
1546 bool may_store_doubles_;
1551 // Node for capturing a regexp literal.
1552 class RegExpLiteral FINAL : public MaterializedLiteral {
1554 DECLARE_NODE_TYPE(RegExpLiteral)
1556 Handle<String> pattern() const { return pattern_->string(); }
1557 Handle<String> flags() const { return flags_->string(); }
1560 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1561 const AstRawString* flags, int literal_index, int pos)
1562 : MaterializedLiteral(zone, literal_index, pos),
1569 const AstRawString* pattern_;
1570 const AstRawString* flags_;
1574 // An array literal has a literals object that is used
1575 // for minimizing the work when constructing it at runtime.
1576 class ArrayLiteral FINAL : public MaterializedLiteral {
1578 DECLARE_NODE_TYPE(ArrayLiteral)
1580 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1581 ZoneList<Expression*>* values() const { return values_; }
1583 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1585 // Return an AST id for an element that is used in simulate instructions.
1586 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1588 // Unlike other AST nodes, this number of bailout IDs allocated for an
1589 // ArrayLiteral can vary, so num_ids() is not a static method.
1590 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1592 // Populate the constant elements fixed array.
1593 void BuildConstantElements(Isolate* isolate);
1595 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1596 int ComputeFlags() const {
1597 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1598 flags |= ArrayLiteral::kDisableMementos;
1604 kShallowElements = 1,
1605 kDisableMementos = 1 << 1
1609 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1611 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1612 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1615 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1617 Handle<FixedArray> constant_elements_;
1618 ZoneList<Expression*>* values_;
1622 class VariableProxy FINAL : public Expression {
1624 DECLARE_NODE_TYPE(VariableProxy)
1626 bool IsValidReferenceExpression() const OVERRIDE {
1627 return !is_resolved() || var()->IsValidReference();
1630 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1632 Handle<String> name() const { return raw_name()->string(); }
1633 const AstRawString* raw_name() const {
1634 return is_resolved() ? var_->raw_name() : raw_name_;
1637 Variable* var() const {
1638 DCHECK(is_resolved());
1641 void set_var(Variable* v) {
1642 DCHECK(!is_resolved());
1647 bool is_this() const { return IsThisField::decode(bit_field_); }
1649 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1650 void set_is_assigned() {
1651 bit_field_ = IsAssignedField::update(bit_field_, true);
1654 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1655 void set_is_resolved() {
1656 bit_field_ = IsResolvedField::update(bit_field_, true);
1659 int end_position() const { return end_position_; }
1661 // Bind this proxy to the variable var.
1662 void BindTo(Variable* var);
1664 bool UsesVariableFeedbackSlot() const {
1665 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1668 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1669 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1671 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1672 ICSlotCache* cache) OVERRIDE;
1673 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1674 FeedbackVectorICSlot VariableFeedbackSlot() {
1675 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1676 return variable_feedback_slot_;
1680 VariableProxy(Zone* zone, Variable* var, int start_position,
1683 VariableProxy(Zone* zone, const AstRawString* name, bool is_this,
1684 int start_position, int end_position);
1686 class IsThisField : public BitField8<bool, 0, 1> {};
1687 class IsAssignedField : public BitField8<bool, 1, 1> {};
1688 class IsResolvedField : public BitField8<bool, 2, 1> {};
1690 // Start with 16-bit (or smaller) field, which should get packed together
1691 // with Expression's trailing 16-bit field.
1693 FeedbackVectorICSlot variable_feedback_slot_;
1695 const AstRawString* raw_name_; // if !is_resolved_
1696 Variable* var_; // if is_resolved_
1698 // Position is stored in the AstNode superclass, but VariableProxy needs to
1699 // know its end position too (for error messages). It cannot be inferred from
1700 // the variable name length because it can contain escapes.
1705 class Property FINAL : public Expression {
1707 DECLARE_NODE_TYPE(Property)
1709 bool IsValidReferenceExpression() const OVERRIDE { return true; }
1711 Expression* obj() const { return obj_; }
1712 Expression* key() const { return key_; }
1714 static int num_ids() { return parent_num_ids() + 2; }
1715 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1716 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1718 bool IsStringAccess() const {
1719 return IsStringAccessField::decode(bit_field_);
1722 // Type feedback information.
1723 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
1724 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
1725 KeyedAccessStoreMode GetStoreMode() const OVERRIDE { return STANDARD_STORE; }
1726 IcCheckType GetKeyType() const OVERRIDE {
1727 return KeyTypeField::decode(bit_field_);
1729 bool IsUninitialized() const {
1730 return !is_for_call() && HasNoTypeInformation();
1732 bool HasNoTypeInformation() const {
1733 return IsUninitializedField::decode(bit_field_);
1735 void set_is_uninitialized(bool b) {
1736 bit_field_ = IsUninitializedField::update(bit_field_, b);
1738 void set_is_string_access(bool b) {
1739 bit_field_ = IsStringAccessField::update(bit_field_, b);
1741 void set_key_type(IcCheckType key_type) {
1742 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1744 void mark_for_call() {
1745 bit_field_ = IsForCallField::update(bit_field_, true);
1747 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1749 bool IsSuperAccess() {
1750 return obj()->IsSuperReference();
1753 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1754 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1755 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1757 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1758 ICSlotCache* cache) OVERRIDE {
1759 property_feedback_slot_ = slot;
1761 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1762 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1765 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1766 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1767 return property_feedback_slot_;
1771 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1772 : Expression(zone, pos),
1773 bit_field_(IsForCallField::encode(false) |
1774 IsUninitializedField::encode(false) |
1775 IsStringAccessField::encode(false)),
1776 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1779 static int parent_num_ids() { return Expression::num_ids(); }
1782 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1784 class IsForCallField : public BitField8<bool, 0, 1> {};
1785 class IsUninitializedField : public BitField8<bool, 1, 1> {};
1786 class IsStringAccessField : public BitField8<bool, 2, 1> {};
1787 class KeyTypeField : public BitField8<IcCheckType, 3, 1> {};
1789 FeedbackVectorICSlot property_feedback_slot_;
1792 SmallMapList receiver_types_;
1796 class Call FINAL : public Expression {
1798 DECLARE_NODE_TYPE(Call)
1800 Expression* expression() const { return expression_; }
1801 ZoneList<Expression*>* arguments() const { return arguments_; }
1803 // Type feedback information.
1804 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1805 Isolate* isolate, const ICSlotCache* cache) OVERRIDE;
1806 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1807 ICSlotCache* cache) OVERRIDE {
1808 ic_slot_or_slot_ = slot.ToInt();
1810 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1811 ic_slot_or_slot_ = slot.ToInt();
1813 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1815 FeedbackVectorSlot CallFeedbackSlot() const {
1816 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1817 return FeedbackVectorSlot(ic_slot_or_slot_);
1820 FeedbackVectorICSlot CallFeedbackICSlot() const {
1821 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1822 return FeedbackVectorICSlot(ic_slot_or_slot_);
1825 SmallMapList* GetReceiverTypes() OVERRIDE {
1826 if (expression()->IsProperty()) {
1827 return expression()->AsProperty()->GetReceiverTypes();
1832 bool IsMonomorphic() OVERRIDE {
1833 if (expression()->IsProperty()) {
1834 return expression()->AsProperty()->IsMonomorphic();
1836 return !target_.is_null();
1839 bool global_call() const {
1840 VariableProxy* proxy = expression_->AsVariableProxy();
1841 return proxy != NULL && proxy->var()->IsUnallocated();
1844 bool known_global_function() const {
1845 return global_call() && !target_.is_null();
1848 Handle<JSFunction> target() { return target_; }
1850 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1852 void set_target(Handle<JSFunction> target) { target_ = target; }
1853 void set_allocation_site(Handle<AllocationSite> site) {
1854 allocation_site_ = site;
1856 bool ComputeGlobalTarget(Handle<GlobalObject> global, LookupIterator* it);
1858 static int num_ids() { return parent_num_ids() + 2; }
1859 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1860 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1862 bool is_uninitialized() const {
1863 return IsUninitializedField::decode(bit_field_);
1865 void set_is_uninitialized(bool b) {
1866 bit_field_ = IsUninitializedField::update(bit_field_, b);
1878 // Helpers to determine how to handle the call.
1879 CallType GetCallType(Isolate* isolate) const;
1880 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1881 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1884 // Used to assert that the FullCodeGenerator records the return site.
1885 bool return_is_recorded_;
1889 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1891 : Expression(zone, pos),
1892 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1893 expression_(expression),
1894 arguments_(arguments),
1895 bit_field_(IsUninitializedField::encode(false)) {
1896 if (expression->IsProperty()) {
1897 expression->AsProperty()->mark_for_call();
1900 static int parent_num_ids() { return Expression::num_ids(); }
1903 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1905 // We store this as an integer because we don't know if we have a slot or
1906 // an ic slot until scoping time.
1907 int ic_slot_or_slot_;
1908 Expression* expression_;
1909 ZoneList<Expression*>* arguments_;
1910 Handle<JSFunction> target_;
1911 Handle<AllocationSite> allocation_site_;
1912 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1917 class CallNew FINAL : public Expression {
1919 DECLARE_NODE_TYPE(CallNew)
1921 Expression* expression() const { return expression_; }
1922 ZoneList<Expression*>* arguments() const { return arguments_; }
1924 // Type feedback information.
1925 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1926 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
1927 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1929 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1930 callnew_feedback_slot_ = slot;
1933 FeedbackVectorSlot CallNewFeedbackSlot() {
1934 DCHECK(!callnew_feedback_slot_.IsInvalid());
1935 return callnew_feedback_slot_;
1937 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1938 DCHECK(FLAG_pretenuring_call_new);
1939 return CallNewFeedbackSlot().next();
1942 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1943 Handle<JSFunction> target() const { return target_; }
1944 Handle<AllocationSite> allocation_site() const {
1945 return allocation_site_;
1948 static int num_ids() { return parent_num_ids() + 1; }
1949 static int feedback_slots() { return 1; }
1950 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1952 void set_allocation_site(Handle<AllocationSite> site) {
1953 allocation_site_ = site;
1955 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1956 void set_target(Handle<JSFunction> target) { target_ = target; }
1959 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1961 : Expression(zone, pos),
1962 expression_(expression),
1963 arguments_(arguments),
1964 is_monomorphic_(false),
1965 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1967 static int parent_num_ids() { return Expression::num_ids(); }
1970 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1972 Expression* expression_;
1973 ZoneList<Expression*>* arguments_;
1974 bool is_monomorphic_;
1975 Handle<JSFunction> target_;
1976 Handle<AllocationSite> allocation_site_;
1977 FeedbackVectorSlot callnew_feedback_slot_;
1981 // The CallRuntime class does not represent any official JavaScript
1982 // language construct. Instead it is used to call a C or JS function
1983 // with a set of arguments. This is used from the builtins that are
1984 // implemented in JavaScript (see "v8natives.js").
1985 class CallRuntime FINAL : public Expression {
1987 DECLARE_NODE_TYPE(CallRuntime)
1989 Handle<String> name() const { return raw_name_->string(); }
1990 const AstRawString* raw_name() const { return raw_name_; }
1991 const Runtime::Function* function() const { return function_; }
1992 ZoneList<Expression*>* arguments() const { return arguments_; }
1993 bool is_jsruntime() const { return function_ == NULL; }
1995 // Type feedback information.
1996 bool HasCallRuntimeFeedbackSlot() const {
1997 return FLAG_vector_ics && is_jsruntime();
1999 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2000 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2001 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2003 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2004 ICSlotCache* cache) OVERRIDE {
2005 callruntime_feedback_slot_ = slot;
2007 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2009 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2010 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2011 !callruntime_feedback_slot_.IsInvalid());
2012 return callruntime_feedback_slot_;
2015 static int num_ids() { return parent_num_ids() + 1; }
2016 TypeFeedbackId CallRuntimeFeedbackId() const {
2017 return TypeFeedbackId(local_id(0));
2021 CallRuntime(Zone* zone, const AstRawString* name,
2022 const Runtime::Function* function,
2023 ZoneList<Expression*>* arguments, int pos)
2024 : Expression(zone, pos),
2026 function_(function),
2027 arguments_(arguments),
2028 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2029 static int parent_num_ids() { return Expression::num_ids(); }
2032 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2034 const AstRawString* raw_name_;
2035 const Runtime::Function* function_;
2036 ZoneList<Expression*>* arguments_;
2037 FeedbackVectorICSlot callruntime_feedback_slot_;
2041 class UnaryOperation FINAL : public Expression {
2043 DECLARE_NODE_TYPE(UnaryOperation)
2045 Token::Value op() const { return op_; }
2046 Expression* expression() const { return expression_; }
2048 // For unary not (Token::NOT), the AST ids where true and false will
2049 // actually be materialized, respectively.
2050 static int num_ids() { return parent_num_ids() + 2; }
2051 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2052 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2054 virtual void RecordToBooleanTypeFeedback(
2055 TypeFeedbackOracle* oracle) OVERRIDE;
2058 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2059 : Expression(zone, pos), op_(op), expression_(expression) {
2060 DCHECK(Token::IsUnaryOp(op));
2062 static int parent_num_ids() { return Expression::num_ids(); }
2065 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2068 Expression* expression_;
2072 class BinaryOperation FINAL : public Expression {
2074 DECLARE_NODE_TYPE(BinaryOperation)
2076 Token::Value op() const { return static_cast<Token::Value>(op_); }
2077 Expression* left() const { return left_; }
2078 Expression* right() const { return right_; }
2079 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2080 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2081 allocation_site_ = allocation_site;
2084 // The short-circuit logical operations need an AST ID for their
2085 // right-hand subexpression.
2086 static int num_ids() { return parent_num_ids() + 2; }
2087 BailoutId RightId() const { return BailoutId(local_id(0)); }
2089 TypeFeedbackId BinaryOperationFeedbackId() const {
2090 return TypeFeedbackId(local_id(1));
2092 Maybe<int> fixed_right_arg() const {
2093 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2095 void set_fixed_right_arg(Maybe<int> arg) {
2096 has_fixed_right_arg_ = arg.IsJust();
2097 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2100 virtual void RecordToBooleanTypeFeedback(
2101 TypeFeedbackOracle* oracle) OVERRIDE;
2104 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2105 Expression* right, int pos)
2106 : Expression(zone, pos),
2107 op_(static_cast<byte>(op)),
2108 has_fixed_right_arg_(false),
2109 fixed_right_arg_value_(0),
2112 DCHECK(Token::IsBinaryOp(op));
2114 static int parent_num_ids() { return Expression::num_ids(); }
2117 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2119 const byte op_; // actually Token::Value
2120 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2121 // type for the RHS. Currenty it's actually a Maybe<int>
2122 bool has_fixed_right_arg_;
2123 int fixed_right_arg_value_;
2126 Handle<AllocationSite> allocation_site_;
2130 class CountOperation FINAL : public Expression {
2132 DECLARE_NODE_TYPE(CountOperation)
2134 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2135 bool is_postfix() const { return !is_prefix(); }
2137 Token::Value op() const { return TokenField::decode(bit_field_); }
2138 Token::Value binary_op() {
2139 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2142 Expression* expression() const { return expression_; }
2144 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2145 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2146 IcCheckType GetKeyType() const OVERRIDE {
2147 return KeyTypeField::decode(bit_field_);
2149 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2150 return StoreModeField::decode(bit_field_);
2152 Type* type() const { return type_; }
2153 void set_key_type(IcCheckType type) {
2154 bit_field_ = KeyTypeField::update(bit_field_, type);
2156 void set_store_mode(KeyedAccessStoreMode mode) {
2157 bit_field_ = StoreModeField::update(bit_field_, mode);
2159 void set_type(Type* type) { type_ = type; }
2161 static int num_ids() { return parent_num_ids() + 4; }
2162 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2163 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2164 TypeFeedbackId CountBinOpFeedbackId() const {
2165 return TypeFeedbackId(local_id(2));
2167 TypeFeedbackId CountStoreFeedbackId() const {
2168 return TypeFeedbackId(local_id(3));
2172 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2174 : Expression(zone, pos),
2175 bit_field_(IsPrefixField::encode(is_prefix) |
2176 KeyTypeField::encode(ELEMENT) |
2177 StoreModeField::encode(STANDARD_STORE) |
2178 TokenField::encode(op)),
2180 expression_(expr) {}
2181 static int parent_num_ids() { return Expression::num_ids(); }
2184 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2186 class IsPrefixField : public BitField16<bool, 0, 1> {};
2187 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2188 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2189 class TokenField : public BitField16<Token::Value, 6, 8> {};
2191 // Starts with 16-bit field, which should get packed together with
2192 // Expression's trailing 16-bit field.
2193 uint16_t bit_field_;
2195 Expression* expression_;
2196 SmallMapList receiver_types_;
2200 class CompareOperation FINAL : public Expression {
2202 DECLARE_NODE_TYPE(CompareOperation)
2204 Token::Value op() const { return op_; }
2205 Expression* left() const { return left_; }
2206 Expression* right() const { return right_; }
2208 // Type feedback information.
2209 static int num_ids() { return parent_num_ids() + 1; }
2210 TypeFeedbackId CompareOperationFeedbackId() const {
2211 return TypeFeedbackId(local_id(0));
2213 Type* combined_type() const { return combined_type_; }
2214 void set_combined_type(Type* type) { combined_type_ = type; }
2216 // Match special cases.
2217 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2218 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2219 bool IsLiteralCompareNull(Expression** expr);
2222 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2223 Expression* right, int pos)
2224 : Expression(zone, pos),
2228 combined_type_(Type::None(zone)) {
2229 DCHECK(Token::IsCompareOp(op));
2231 static int parent_num_ids() { return Expression::num_ids(); }
2234 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2240 Type* combined_type_;
2244 class Conditional FINAL : public Expression {
2246 DECLARE_NODE_TYPE(Conditional)
2248 Expression* condition() const { return condition_; }
2249 Expression* then_expression() const { return then_expression_; }
2250 Expression* else_expression() const { return else_expression_; }
2252 static int num_ids() { return parent_num_ids() + 2; }
2253 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2254 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2257 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2258 Expression* else_expression, int position)
2259 : Expression(zone, position),
2260 condition_(condition),
2261 then_expression_(then_expression),
2262 else_expression_(else_expression) {}
2263 static int parent_num_ids() { return Expression::num_ids(); }
2266 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2268 Expression* condition_;
2269 Expression* then_expression_;
2270 Expression* else_expression_;
2274 class Assignment FINAL : public Expression {
2276 DECLARE_NODE_TYPE(Assignment)
2278 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2280 Token::Value binary_op() const;
2282 Token::Value op() const { return TokenField::decode(bit_field_); }
2283 Expression* target() const { return target_; }
2284 Expression* value() const { return value_; }
2285 BinaryOperation* binary_operation() const { return binary_operation_; }
2287 // This check relies on the definition order of token in token.h.
2288 bool is_compound() const { return op() > Token::ASSIGN; }
2290 static int num_ids() { return parent_num_ids() + 2; }
2291 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2293 // Type feedback information.
2294 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2295 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2296 bool IsUninitialized() const {
2297 return IsUninitializedField::decode(bit_field_);
2299 bool HasNoTypeInformation() {
2300 return IsUninitializedField::decode(bit_field_);
2302 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2303 IcCheckType GetKeyType() const OVERRIDE {
2304 return KeyTypeField::decode(bit_field_);
2306 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2307 return StoreModeField::decode(bit_field_);
2309 void set_is_uninitialized(bool b) {
2310 bit_field_ = IsUninitializedField::update(bit_field_, b);
2312 void set_key_type(IcCheckType key_type) {
2313 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2315 void set_store_mode(KeyedAccessStoreMode mode) {
2316 bit_field_ = StoreModeField::update(bit_field_, mode);
2320 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2322 static int parent_num_ids() { return Expression::num_ids(); }
2325 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2327 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2328 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2329 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2330 class TokenField : public BitField16<Token::Value, 6, 8> {};
2332 // Starts with 16-bit field, which should get packed together with
2333 // Expression's trailing 16-bit field.
2334 uint16_t bit_field_;
2335 Expression* target_;
2337 BinaryOperation* binary_operation_;
2338 SmallMapList receiver_types_;
2342 class Yield FINAL : public Expression {
2344 DECLARE_NODE_TYPE(Yield)
2347 kInitial, // The initial yield that returns the unboxed generator object.
2348 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2349 kDelegating, // A yield*.
2350 kFinal // A return: { value: EXPRESSION, done: true }
2353 Expression* generator_object() const { return generator_object_; }
2354 Expression* expression() const { return expression_; }
2355 Kind yield_kind() const { return yield_kind_; }
2357 // Delegating yield surrounds the "yield" in a "try/catch". This index
2358 // locates the catch handler in the handler table, and is equivalent to
2359 // TryCatchStatement::index().
2361 DCHECK_EQ(kDelegating, yield_kind());
2364 void set_index(int index) {
2365 DCHECK_EQ(kDelegating, yield_kind());
2369 // Type feedback information.
2370 bool HasFeedbackSlots() const {
2371 return FLAG_vector_ics && (yield_kind() == kDelegating);
2373 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2374 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2375 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2377 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2378 ICSlotCache* cache) OVERRIDE {
2379 yield_first_feedback_slot_ = slot;
2381 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2382 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2385 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2386 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2387 return yield_first_feedback_slot_;
2390 FeedbackVectorICSlot DoneFeedbackSlot() {
2391 return KeyedLoadFeedbackSlot().next();
2394 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2397 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2398 Kind yield_kind, int pos)
2399 : Expression(zone, pos),
2400 generator_object_(generator_object),
2401 expression_(expression),
2402 yield_kind_(yield_kind),
2404 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2407 Expression* generator_object_;
2408 Expression* expression_;
2411 FeedbackVectorICSlot yield_first_feedback_slot_;
2415 class Throw FINAL : public Expression {
2417 DECLARE_NODE_TYPE(Throw)
2419 Expression* exception() const { return exception_; }
2422 Throw(Zone* zone, Expression* exception, int pos)
2423 : Expression(zone, pos), exception_(exception) {}
2426 Expression* exception_;
2430 class FunctionLiteral FINAL : public Expression {
2433 ANONYMOUS_EXPRESSION,
2438 enum ParameterFlag {
2439 kNoDuplicateParameters = 0,
2440 kHasDuplicateParameters = 1
2443 enum IsFunctionFlag {
2448 enum IsParenthesizedFlag {
2453 enum ArityRestriction {
2459 DECLARE_NODE_TYPE(FunctionLiteral)
2461 Handle<String> name() const { return raw_name_->string(); }
2462 const AstRawString* raw_name() const { return raw_name_; }
2463 Scope* scope() const { return scope_; }
2464 ZoneList<Statement*>* body() const { return body_; }
2465 void set_function_token_position(int pos) { function_token_position_ = pos; }
2466 int function_token_position() const { return function_token_position_; }
2467 int start_position() const;
2468 int end_position() const;
2469 int SourceSize() const { return end_position() - start_position(); }
2470 bool is_expression() const { return IsExpression::decode(bitfield_); }
2471 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2472 LanguageMode language_mode() const;
2473 bool uses_super_property() const;
2475 static bool NeedsHomeObject(Expression* literal) {
2476 return literal != NULL && literal->IsFunctionLiteral() &&
2477 literal->AsFunctionLiteral()->uses_super_property();
2480 int materialized_literal_count() { return materialized_literal_count_; }
2481 int expected_property_count() { return expected_property_count_; }
2482 int handler_count() { return handler_count_; }
2483 int parameter_count() { return parameter_count_; }
2485 bool AllowsLazyCompilation();
2486 bool AllowsLazyCompilationWithoutContext();
2488 void InitializeSharedInfo(Handle<Code> code);
2490 Handle<String> debug_name() const {
2491 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2492 return raw_name_->string();
2494 return inferred_name();
2497 Handle<String> inferred_name() const {
2498 if (!inferred_name_.is_null()) {
2499 DCHECK(raw_inferred_name_ == NULL);
2500 return inferred_name_;
2502 if (raw_inferred_name_ != NULL) {
2503 return raw_inferred_name_->string();
2506 return Handle<String>();
2509 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2510 void set_inferred_name(Handle<String> inferred_name) {
2511 DCHECK(!inferred_name.is_null());
2512 inferred_name_ = inferred_name;
2513 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2514 raw_inferred_name_ = NULL;
2517 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2518 DCHECK(raw_inferred_name != NULL);
2519 raw_inferred_name_ = raw_inferred_name;
2520 DCHECK(inferred_name_.is_null());
2521 inferred_name_ = Handle<String>();
2524 // shared_info may be null if it's not cached in full code.
2525 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2527 bool pretenure() { return Pretenure::decode(bitfield_); }
2528 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2530 bool has_duplicate_parameters() {
2531 return HasDuplicateParameters::decode(bitfield_);
2534 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2536 // This is used as a heuristic on when to eagerly compile a function
2537 // literal. We consider the following constructs as hints that the
2538 // function will be called immediately:
2539 // - (function() { ... })();
2540 // - var x = function() { ... }();
2541 bool is_parenthesized() {
2542 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2544 void set_parenthesized() {
2545 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2548 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2550 int ast_node_count() { return ast_properties_.node_count(); }
2551 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2552 void set_ast_properties(AstProperties* ast_properties) {
2553 ast_properties_ = *ast_properties;
2555 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2556 return ast_properties_.get_spec();
2558 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2559 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2560 void set_dont_optimize_reason(BailoutReason reason) {
2561 dont_optimize_reason_ = reason;
2565 FunctionLiteral(Zone* zone, const AstRawString* name,
2566 AstValueFactory* ast_value_factory, Scope* scope,
2567 ZoneList<Statement*>* body, int materialized_literal_count,
2568 int expected_property_count, int handler_count,
2569 int parameter_count, FunctionType function_type,
2570 ParameterFlag has_duplicate_parameters,
2571 IsFunctionFlag is_function,
2572 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2574 : Expression(zone, position),
2578 raw_inferred_name_(ast_value_factory->empty_string()),
2579 ast_properties_(zone),
2580 dont_optimize_reason_(kNoReason),
2581 materialized_literal_count_(materialized_literal_count),
2582 expected_property_count_(expected_property_count),
2583 handler_count_(handler_count),
2584 parameter_count_(parameter_count),
2585 function_token_position_(RelocInfo::kNoPosition) {
2586 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2587 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2588 Pretenure::encode(false) |
2589 HasDuplicateParameters::encode(has_duplicate_parameters) |
2590 IsFunction::encode(is_function) |
2591 IsParenthesized::encode(is_parenthesized) |
2592 FunctionKindBits::encode(kind);
2593 DCHECK(IsValidFunctionKind(kind));
2597 const AstRawString* raw_name_;
2598 Handle<String> name_;
2599 Handle<SharedFunctionInfo> shared_info_;
2601 ZoneList<Statement*>* body_;
2602 const AstString* raw_inferred_name_;
2603 Handle<String> inferred_name_;
2604 AstProperties ast_properties_;
2605 BailoutReason dont_optimize_reason_;
2607 int materialized_literal_count_;
2608 int expected_property_count_;
2610 int parameter_count_;
2611 int function_token_position_;
2614 class IsExpression : public BitField<bool, 0, 1> {};
2615 class IsAnonymous : public BitField<bool, 1, 1> {};
2616 class Pretenure : public BitField<bool, 2, 1> {};
2617 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2618 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2619 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2620 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2624 class ClassLiteral FINAL : public Expression {
2626 typedef ObjectLiteralProperty Property;
2628 DECLARE_NODE_TYPE(ClassLiteral)
2630 Handle<String> name() const { return raw_name_->string(); }
2631 const AstRawString* raw_name() const { return raw_name_; }
2632 Scope* scope() const { return scope_; }
2633 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2634 Expression* extends() const { return extends_; }
2635 FunctionLiteral* constructor() const { return constructor_; }
2636 ZoneList<Property*>* properties() const { return properties_; }
2637 int start_position() const { return position(); }
2638 int end_position() const { return end_position_; }
2640 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2641 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2642 BailoutId ExitId() { return BailoutId(local_id(2)); }
2644 // Return an AST id for a property that is used in simulate instructions.
2645 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2647 // Unlike other AST nodes, this number of bailout IDs allocated for an
2648 // ClassLiteral can vary, so num_ids() is not a static method.
2649 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2652 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2653 VariableProxy* class_variable_proxy, Expression* extends,
2654 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2655 int start_position, int end_position)
2656 : Expression(zone, start_position),
2659 class_variable_proxy_(class_variable_proxy),
2661 constructor_(constructor),
2662 properties_(properties),
2663 end_position_(end_position) {}
2664 static int parent_num_ids() { return Expression::num_ids(); }
2667 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2669 const AstRawString* raw_name_;
2671 VariableProxy* class_variable_proxy_;
2672 Expression* extends_;
2673 FunctionLiteral* constructor_;
2674 ZoneList<Property*>* properties_;
2679 class NativeFunctionLiteral FINAL : public Expression {
2681 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2683 Handle<String> name() const { return name_->string(); }
2684 v8::Extension* extension() const { return extension_; }
2687 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2688 v8::Extension* extension, int pos)
2689 : Expression(zone, pos), name_(name), extension_(extension) {}
2692 const AstRawString* name_;
2693 v8::Extension* extension_;
2697 class ThisFunction FINAL : public Expression {
2699 DECLARE_NODE_TYPE(ThisFunction)
2702 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2706 class SuperReference FINAL : public Expression {
2708 DECLARE_NODE_TYPE(SuperReference)
2710 VariableProxy* this_var() const { return this_var_; }
2712 static int num_ids() { return parent_num_ids() + 1; }
2713 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2715 // Type feedback information.
2716 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2717 Isolate* isolate, const ICSlotCache* cache) OVERRIDE {
2718 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2720 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2721 ICSlotCache* cache) OVERRIDE {
2722 homeobject_feedback_slot_ = slot;
2724 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2726 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2727 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2728 return homeobject_feedback_slot_;
2732 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2733 : Expression(zone, pos),
2734 this_var_(this_var),
2735 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2736 DCHECK(this_var->is_this());
2738 static int parent_num_ids() { return Expression::num_ids(); }
2741 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2743 VariableProxy* this_var_;
2744 FeedbackVectorICSlot homeobject_feedback_slot_;
2748 #undef DECLARE_NODE_TYPE
2751 // ----------------------------------------------------------------------------
2752 // Regular expressions
2755 class RegExpVisitor BASE_EMBEDDED {
2757 virtual ~RegExpVisitor() { }
2758 #define MAKE_CASE(Name) \
2759 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2760 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2765 class RegExpTree : public ZoneObject {
2767 static const int kInfinity = kMaxInt;
2768 virtual ~RegExpTree() {}
2769 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2770 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2771 RegExpNode* on_success) = 0;
2772 virtual bool IsTextElement() { return false; }
2773 virtual bool IsAnchoredAtStart() { return false; }
2774 virtual bool IsAnchoredAtEnd() { return false; }
2775 virtual int min_match() = 0;
2776 virtual int max_match() = 0;
2777 // Returns the interval of registers used for captures within this
2779 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2780 virtual void AppendToText(RegExpText* text, Zone* zone);
2781 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2782 #define MAKE_ASTYPE(Name) \
2783 virtual RegExp##Name* As##Name(); \
2784 virtual bool Is##Name();
2785 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2790 class RegExpDisjunction FINAL : public RegExpTree {
2792 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2793 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2794 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2795 RegExpNode* on_success) OVERRIDE;
2796 RegExpDisjunction* AsDisjunction() OVERRIDE;
2797 Interval CaptureRegisters() OVERRIDE;
2798 bool IsDisjunction() OVERRIDE;
2799 bool IsAnchoredAtStart() OVERRIDE;
2800 bool IsAnchoredAtEnd() OVERRIDE;
2801 int min_match() OVERRIDE { return min_match_; }
2802 int max_match() OVERRIDE { return max_match_; }
2803 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2805 ZoneList<RegExpTree*>* alternatives_;
2811 class RegExpAlternative FINAL : public RegExpTree {
2813 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2814 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2815 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2816 RegExpNode* on_success) OVERRIDE;
2817 RegExpAlternative* AsAlternative() OVERRIDE;
2818 Interval CaptureRegisters() OVERRIDE;
2819 bool IsAlternative() OVERRIDE;
2820 bool IsAnchoredAtStart() OVERRIDE;
2821 bool IsAnchoredAtEnd() OVERRIDE;
2822 int min_match() OVERRIDE { return min_match_; }
2823 int max_match() OVERRIDE { return max_match_; }
2824 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2826 ZoneList<RegExpTree*>* nodes_;
2832 class RegExpAssertion FINAL : public RegExpTree {
2834 enum AssertionType {
2842 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2843 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2844 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2845 RegExpNode* on_success) OVERRIDE;
2846 RegExpAssertion* AsAssertion() OVERRIDE;
2847 bool IsAssertion() OVERRIDE;
2848 bool IsAnchoredAtStart() OVERRIDE;
2849 bool IsAnchoredAtEnd() OVERRIDE;
2850 int min_match() OVERRIDE { return 0; }
2851 int max_match() OVERRIDE { return 0; }
2852 AssertionType assertion_type() { return assertion_type_; }
2854 AssertionType assertion_type_;
2858 class CharacterSet FINAL BASE_EMBEDDED {
2860 explicit CharacterSet(uc16 standard_set_type)
2862 standard_set_type_(standard_set_type) {}
2863 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2865 standard_set_type_(0) {}
2866 ZoneList<CharacterRange>* ranges(Zone* zone);
2867 uc16 standard_set_type() { return standard_set_type_; }
2868 void set_standard_set_type(uc16 special_set_type) {
2869 standard_set_type_ = special_set_type;
2871 bool is_standard() { return standard_set_type_ != 0; }
2872 void Canonicalize();
2874 ZoneList<CharacterRange>* ranges_;
2875 // If non-zero, the value represents a standard set (e.g., all whitespace
2876 // characters) without having to expand the ranges.
2877 uc16 standard_set_type_;
2881 class RegExpCharacterClass FINAL : public RegExpTree {
2883 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2885 is_negated_(is_negated) { }
2886 explicit RegExpCharacterClass(uc16 type)
2888 is_negated_(false) { }
2889 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2890 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2891 RegExpNode* on_success) OVERRIDE;
2892 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2893 bool IsCharacterClass() OVERRIDE;
2894 bool IsTextElement() OVERRIDE { return true; }
2895 int min_match() OVERRIDE { return 1; }
2896 int max_match() OVERRIDE { return 1; }
2897 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2898 CharacterSet character_set() { return set_; }
2899 // TODO(lrn): Remove need for complex version if is_standard that
2900 // recognizes a mangled standard set and just do { return set_.is_special(); }
2901 bool is_standard(Zone* zone);
2902 // Returns a value representing the standard character set if is_standard()
2904 // Currently used values are:
2905 // s : unicode whitespace
2906 // S : unicode non-whitespace
2907 // w : ASCII word character (digit, letter, underscore)
2908 // W : non-ASCII word character
2910 // D : non-ASCII digit
2911 // . : non-unicode non-newline
2912 // * : All characters
2913 uc16 standard_type() { return set_.standard_set_type(); }
2914 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2915 bool is_negated() { return is_negated_; }
2923 class RegExpAtom FINAL : public RegExpTree {
2925 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2926 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2927 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2928 RegExpNode* on_success) OVERRIDE;
2929 RegExpAtom* AsAtom() OVERRIDE;
2930 bool IsAtom() OVERRIDE;
2931 bool IsTextElement() OVERRIDE { return true; }
2932 int min_match() OVERRIDE { return data_.length(); }
2933 int max_match() OVERRIDE { return data_.length(); }
2934 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2935 Vector<const uc16> data() { return data_; }
2936 int length() { return data_.length(); }
2938 Vector<const uc16> data_;
2942 class RegExpText FINAL : public RegExpTree {
2944 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2945 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2946 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2947 RegExpNode* on_success) OVERRIDE;
2948 RegExpText* AsText() OVERRIDE;
2949 bool IsText() OVERRIDE;
2950 bool IsTextElement() OVERRIDE { return true; }
2951 int min_match() OVERRIDE { return length_; }
2952 int max_match() OVERRIDE { return length_; }
2953 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2954 void AddElement(TextElement elm, Zone* zone) {
2955 elements_.Add(elm, zone);
2956 length_ += elm.length();
2958 ZoneList<TextElement>* elements() { return &elements_; }
2960 ZoneList<TextElement> elements_;
2965 class RegExpQuantifier FINAL : public RegExpTree {
2967 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
2968 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
2972 min_match_(min * body->min_match()),
2973 quantifier_type_(type) {
2974 if (max > 0 && body->max_match() > kInfinity / max) {
2975 max_match_ = kInfinity;
2977 max_match_ = max * body->max_match();
2980 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2981 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2982 RegExpNode* on_success) OVERRIDE;
2983 static RegExpNode* ToNode(int min,
2987 RegExpCompiler* compiler,
2988 RegExpNode* on_success,
2989 bool not_at_start = false);
2990 RegExpQuantifier* AsQuantifier() OVERRIDE;
2991 Interval CaptureRegisters() OVERRIDE;
2992 bool IsQuantifier() OVERRIDE;
2993 int min_match() OVERRIDE { return min_match_; }
2994 int max_match() OVERRIDE { return max_match_; }
2995 int min() { return min_; }
2996 int max() { return max_; }
2997 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
2998 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
2999 bool is_greedy() { return quantifier_type_ == GREEDY; }
3000 RegExpTree* body() { return body_; }
3008 QuantifierType quantifier_type_;
3012 class RegExpCapture FINAL : public RegExpTree {
3014 explicit RegExpCapture(RegExpTree* body, int index)
3015 : body_(body), index_(index) { }
3016 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3017 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3018 RegExpNode* on_success) OVERRIDE;
3019 static RegExpNode* ToNode(RegExpTree* body,
3021 RegExpCompiler* compiler,
3022 RegExpNode* on_success);
3023 RegExpCapture* AsCapture() OVERRIDE;
3024 bool IsAnchoredAtStart() OVERRIDE;
3025 bool IsAnchoredAtEnd() OVERRIDE;
3026 Interval CaptureRegisters() OVERRIDE;
3027 bool IsCapture() OVERRIDE;
3028 int min_match() OVERRIDE { return body_->min_match(); }
3029 int max_match() OVERRIDE { return body_->max_match(); }
3030 RegExpTree* body() { return body_; }
3031 int index() { return index_; }
3032 static int StartRegister(int index) { return index * 2; }
3033 static int EndRegister(int index) { return index * 2 + 1; }
3041 class RegExpLookahead FINAL : public RegExpTree {
3043 RegExpLookahead(RegExpTree* body,
3048 is_positive_(is_positive),
3049 capture_count_(capture_count),
3050 capture_from_(capture_from) { }
3052 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3053 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3054 RegExpNode* on_success) OVERRIDE;
3055 RegExpLookahead* AsLookahead() OVERRIDE;
3056 Interval CaptureRegisters() OVERRIDE;
3057 bool IsLookahead() OVERRIDE;
3058 bool IsAnchoredAtStart() OVERRIDE;
3059 int min_match() OVERRIDE { return 0; }
3060 int max_match() OVERRIDE { return 0; }
3061 RegExpTree* body() { return body_; }
3062 bool is_positive() { return is_positive_; }
3063 int capture_count() { return capture_count_; }
3064 int capture_from() { return capture_from_; }
3074 class RegExpBackReference FINAL : public RegExpTree {
3076 explicit RegExpBackReference(RegExpCapture* capture)
3077 : capture_(capture) { }
3078 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3079 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3080 RegExpNode* on_success) OVERRIDE;
3081 RegExpBackReference* AsBackReference() OVERRIDE;
3082 bool IsBackReference() OVERRIDE;
3083 int min_match() OVERRIDE { return 0; }
3084 int max_match() OVERRIDE { return capture_->max_match(); }
3085 int index() { return capture_->index(); }
3086 RegExpCapture* capture() { return capture_; }
3088 RegExpCapture* capture_;
3092 class RegExpEmpty FINAL : public RegExpTree {
3095 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3096 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3097 RegExpNode* on_success) OVERRIDE;
3098 RegExpEmpty* AsEmpty() OVERRIDE;
3099 bool IsEmpty() OVERRIDE;
3100 int min_match() OVERRIDE { return 0; }
3101 int max_match() OVERRIDE { return 0; }
3105 // ----------------------------------------------------------------------------
3107 // - leaf node visitors are abstract.
3109 class AstVisitor BASE_EMBEDDED {
3112 virtual ~AstVisitor() {}
3114 // Stack overflow check and dynamic dispatch.
3115 virtual void Visit(AstNode* node) = 0;
3117 // Iteration left-to-right.
3118 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3119 virtual void VisitStatements(ZoneList<Statement*>* statements);
3120 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3122 // Individual AST nodes.
3123 #define DEF_VISIT(type) \
3124 virtual void Visit##type(type* node) = 0;
3125 AST_NODE_LIST(DEF_VISIT)
3130 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3132 void Visit(AstNode* node) FINAL { \
3133 if (!CheckStackOverflow()) node->Accept(this); \
3136 void SetStackOverflow() { stack_overflow_ = true; } \
3137 void ClearStackOverflow() { stack_overflow_ = false; } \
3138 bool HasStackOverflow() const { return stack_overflow_; } \
3140 bool CheckStackOverflow() { \
3141 if (stack_overflow_) return true; \
3142 StackLimitCheck check(isolate_); \
3143 if (!check.HasOverflowed()) return false; \
3144 stack_overflow_ = true; \
3149 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3150 isolate_ = isolate; \
3152 stack_overflow_ = false; \
3154 Zone* zone() { return zone_; } \
3155 Isolate* isolate() { return isolate_; } \
3157 Isolate* isolate_; \
3159 bool stack_overflow_
3162 // ----------------------------------------------------------------------------
3165 class AstNodeFactory FINAL BASE_EMBEDDED {
3167 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3168 : zone_(ast_value_factory->zone()),
3169 ast_value_factory_(ast_value_factory) {}
3171 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3175 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3178 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3180 FunctionLiteral* fun,
3183 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3186 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3190 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3193 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3194 const AstRawString* import_name,
3195 const AstRawString* module_specifier,
3196 Scope* scope, int pos) {
3197 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3198 module_specifier, scope, pos);
3201 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3204 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3207 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3209 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3212 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3213 return new (zone_) ModulePath(zone_, origin, name, pos);
3216 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3217 return new (zone_) ModuleUrl(zone_, url, pos);
3220 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3222 bool is_initializer_block,
3225 Block(zone_, labels, capacity, is_initializer_block, pos);
3228 #define STATEMENT_WITH_LABELS(NodeType) \
3229 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3230 return new (zone_) NodeType(zone_, labels, pos); \
3232 STATEMENT_WITH_LABELS(DoWhileStatement)
3233 STATEMENT_WITH_LABELS(WhileStatement)
3234 STATEMENT_WITH_LABELS(ForStatement)
3235 STATEMENT_WITH_LABELS(SwitchStatement)
3236 #undef STATEMENT_WITH_LABELS
3238 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3239 ZoneList<const AstRawString*>* labels,
3241 switch (visit_mode) {
3242 case ForEachStatement::ENUMERATE: {
3243 return new (zone_) ForInStatement(zone_, labels, pos);
3245 case ForEachStatement::ITERATE: {
3246 return new (zone_) ForOfStatement(zone_, labels, pos);
3253 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3254 return new (zone_) ModuleStatement(zone_, body, pos);
3257 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3258 return new (zone_) ExpressionStatement(zone_, expression, pos);
3261 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3262 return new (zone_) ContinueStatement(zone_, target, pos);
3265 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3266 return new (zone_) BreakStatement(zone_, target, pos);
3269 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3270 return new (zone_) ReturnStatement(zone_, expression, pos);
3273 WithStatement* NewWithStatement(Scope* scope,
3274 Expression* expression,
3275 Statement* statement,
3277 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3280 IfStatement* NewIfStatement(Expression* condition,
3281 Statement* then_statement,
3282 Statement* else_statement,
3285 IfStatement(zone_, condition, then_statement, else_statement, pos);
3288 TryCatchStatement* NewTryCatchStatement(int index,
3294 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3295 variable, catch_block, pos);
3298 TryFinallyStatement* NewTryFinallyStatement(int index,
3300 Block* finally_block,
3303 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3306 DebuggerStatement* NewDebuggerStatement(int pos) {
3307 return new (zone_) DebuggerStatement(zone_, pos);
3310 EmptyStatement* NewEmptyStatement(int pos) {
3311 return new(zone_) EmptyStatement(zone_, pos);
3314 CaseClause* NewCaseClause(
3315 Expression* label, ZoneList<Statement*>* statements, int pos) {
3316 return new (zone_) CaseClause(zone_, label, statements, pos);
3319 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3321 Literal(zone_, ast_value_factory_->NewString(string), pos);
3324 // A JavaScript symbol (ECMA-262 edition 6).
3325 Literal* NewSymbolLiteral(const char* name, int pos) {
3326 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3329 Literal* NewNumberLiteral(double number, int pos) {
3331 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3334 Literal* NewSmiLiteral(int number, int pos) {
3335 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3338 Literal* NewBooleanLiteral(bool b, int pos) {
3339 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3342 Literal* NewNullLiteral(int pos) {
3343 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3346 Literal* NewUndefinedLiteral(int pos) {
3347 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3350 Literal* NewTheHoleLiteral(int pos) {
3351 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3354 ObjectLiteral* NewObjectLiteral(
3355 ZoneList<ObjectLiteral::Property*>* properties,
3357 int boilerplate_properties,
3360 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3361 boilerplate_properties, has_function, pos);
3364 ObjectLiteral::Property* NewObjectLiteralProperty(
3365 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3366 bool is_static, bool is_computed_name) {
3368 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3371 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3374 bool is_computed_name) {
3375 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3376 is_static, is_computed_name);
3379 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3380 const AstRawString* flags,
3383 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3386 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3389 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3392 VariableProxy* NewVariableProxy(Variable* var,
3393 int start_position = RelocInfo::kNoPosition,
3394 int end_position = RelocInfo::kNoPosition) {
3395 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3398 VariableProxy* NewVariableProxy(const AstRawString* name, bool is_this,
3399 int start_position = RelocInfo::kNoPosition,
3400 int end_position = RelocInfo::kNoPosition) {
3402 VariableProxy(zone_, name, is_this, start_position, end_position);
3405 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3406 return new (zone_) Property(zone_, obj, key, pos);
3409 Call* NewCall(Expression* expression,
3410 ZoneList<Expression*>* arguments,
3412 return new (zone_) Call(zone_, expression, arguments, pos);
3415 CallNew* NewCallNew(Expression* expression,
3416 ZoneList<Expression*>* arguments,
3418 return new (zone_) CallNew(zone_, expression, arguments, pos);
3421 CallRuntime* NewCallRuntime(const AstRawString* name,
3422 const Runtime::Function* function,
3423 ZoneList<Expression*>* arguments,
3425 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3428 UnaryOperation* NewUnaryOperation(Token::Value op,
3429 Expression* expression,
3431 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3434 BinaryOperation* NewBinaryOperation(Token::Value op,
3438 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3441 CountOperation* NewCountOperation(Token::Value op,
3445 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3448 CompareOperation* NewCompareOperation(Token::Value op,
3452 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3455 Conditional* NewConditional(Expression* condition,
3456 Expression* then_expression,
3457 Expression* else_expression,
3459 return new (zone_) Conditional(zone_, condition, then_expression,
3460 else_expression, position);
3463 Assignment* NewAssignment(Token::Value op,
3467 DCHECK(Token::IsAssignmentOp(op));
3468 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3469 if (assign->is_compound()) {
3470 DCHECK(Token::IsAssignmentOp(op));
3471 assign->binary_operation_ =
3472 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3477 Yield* NewYield(Expression *generator_object,
3478 Expression* expression,
3479 Yield::Kind yield_kind,
3481 if (!expression) expression = NewUndefinedLiteral(pos);
3483 Yield(zone_, generator_object, expression, yield_kind, pos);
3486 Throw* NewThrow(Expression* exception, int pos) {
3487 return new (zone_) Throw(zone_, exception, pos);
3490 FunctionLiteral* NewFunctionLiteral(
3491 const AstRawString* name, AstValueFactory* ast_value_factory,
3492 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3493 int expected_property_count, int handler_count, int parameter_count,
3494 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3495 FunctionLiteral::FunctionType function_type,
3496 FunctionLiteral::IsFunctionFlag is_function,
3497 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3499 return new (zone_) FunctionLiteral(
3500 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3501 expected_property_count, handler_count, parameter_count, function_type,
3502 has_duplicate_parameters, is_function, is_parenthesized, kind,
3506 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3507 VariableProxy* proxy, Expression* extends,
3508 FunctionLiteral* constructor,
3509 ZoneList<ObjectLiteral::Property*>* properties,
3510 int start_position, int end_position) {
3512 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3513 properties, start_position, end_position);
3516 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3517 v8::Extension* extension,
3519 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3522 ThisFunction* NewThisFunction(int pos) {
3523 return new (zone_) ThisFunction(zone_, pos);
3526 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3527 return new (zone_) SuperReference(zone_, this_var, pos);
3532 AstValueFactory* ast_value_factory_;
3536 } } // namespace v8::internal