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/interface.h"
15 #include "src/isolate.h"
16 #include "src/jsregexp.h"
17 #include "src/list-inl.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) \
55 #define STATEMENT_NODE_LIST(V) \
58 V(ExpressionStatement) \
61 V(ContinueStatement) \
71 V(TryCatchStatement) \
72 V(TryFinallyStatement) \
75 #define EXPRESSION_NODE_LIST(V) \
78 V(NativeFunctionLiteral) \
100 #define AST_NODE_LIST(V) \
101 DECLARATION_NODE_LIST(V) \
102 MODULE_NODE_LIST(V) \
103 STATEMENT_NODE_LIST(V) \
104 EXPRESSION_NODE_LIST(V)
106 // Forward declarations
107 class AstNodeFactory;
111 class BreakableStatement;
113 class IterationStatement;
114 class MaterializedLiteral;
116 class TypeFeedbackOracle;
118 class RegExpAlternative;
119 class RegExpAssertion;
121 class RegExpBackReference;
123 class RegExpCharacterClass;
124 class RegExpCompiler;
125 class RegExpDisjunction;
127 class RegExpLookahead;
128 class RegExpQuantifier;
131 #define DEF_FORWARD_DECLARATION(type) class type;
132 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
133 #undef DEF_FORWARD_DECLARATION
136 // Typedef only introduced to avoid unreadable code.
137 // Please do appreciate the required space in "> >".
138 typedef ZoneList<Handle<String> > ZoneStringList;
139 typedef ZoneList<Handle<Object> > ZoneObjectList;
142 #define DECLARE_NODE_TYPE(type) \
143 void Accept(AstVisitor* v) OVERRIDE; \
144 AstNode::NodeType node_type() const FINAL { return AstNode::k##type; } \
145 friend class AstNodeFactory;
148 enum AstPropertiesFlag {
155 class FeedbackVectorRequirements {
157 FeedbackVectorRequirements(int slots, int ic_slots)
158 : slots_(slots), ic_slots_(ic_slots) {}
160 int slots() const { return slots_; }
161 int ic_slots() const { return ic_slots_; }
169 class AstProperties FINAL BASE_EMBEDDED {
171 class Flags : public EnumSet<AstPropertiesFlag, int> {};
173 AstProperties() : node_count_(0) {}
175 Flags* flags() { return &flags_; }
176 int node_count() { return node_count_; }
177 void add_node_count(int count) { node_count_ += count; }
179 int slots() const { return spec_.slots(); }
180 void increase_slots(int count) { spec_.increase_slots(count); }
182 int ic_slots() const { return spec_.ic_slots(); }
183 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
184 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
185 const FeedbackVectorSpec& get_spec() const { return spec_; }
190 FeedbackVectorSpec spec_;
194 class AstNode: public ZoneObject {
196 #define DECLARE_TYPE_ENUM(type) k##type,
198 AST_NODE_LIST(DECLARE_TYPE_ENUM)
201 #undef DECLARE_TYPE_ENUM
203 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
205 explicit AstNode(int position): position_(position) {}
206 virtual ~AstNode() {}
208 virtual void Accept(AstVisitor* v) = 0;
209 virtual NodeType node_type() const = 0;
210 int position() const { return position_; }
212 // Type testing & conversion functions overridden by concrete subclasses.
213 #define DECLARE_NODE_FUNCTIONS(type) \
214 bool Is##type() const { return node_type() == AstNode::k##type; } \
216 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
218 const type* As##type() const { \
219 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
221 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
222 #undef DECLARE_NODE_FUNCTIONS
224 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
225 virtual IterationStatement* AsIterationStatement() { return NULL; }
226 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
228 // The interface for feedback slots, with default no-op implementations for
229 // node types which don't actually have this. Note that this is conceptually
230 // not really nice, but multiple inheritance would introduce yet another
231 // vtable entry per node, something we don't want for space reasons.
232 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
234 return FeedbackVectorRequirements(0, 0);
236 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
237 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) {
240 // Each ICSlot stores a kind of IC which the participating node should know.
241 virtual Code::Kind FeedbackICSlotKind(int index) {
243 return Code::NUMBER_OF_KINDS;
247 // Hidden to prevent accidental usage. It would have to load the
248 // current zone from the TLS.
249 void* operator new(size_t size);
251 friend class CaseClause; // Generates AST IDs.
257 class Statement : public AstNode {
259 explicit Statement(Zone* zone, int position) : AstNode(position) {}
261 bool IsEmpty() { return AsEmptyStatement() != NULL; }
262 virtual bool IsJump() const { return false; }
266 class SmallMapList FINAL {
269 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
271 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
272 void Clear() { list_.Clear(); }
273 void Sort() { list_.Sort(); }
275 bool is_empty() const { return list_.is_empty(); }
276 int length() const { return list_.length(); }
278 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
279 if (!Map::TryUpdate(map).ToHandle(&map)) return;
280 for (int i = 0; i < length(); ++i) {
281 if (at(i).is_identical_to(map)) return;
286 void FilterForPossibleTransitions(Map* root_map) {
287 for (int i = list_.length() - 1; i >= 0; i--) {
288 if (at(i)->FindRootMap() != root_map) {
289 list_.RemoveElement(list_.at(i));
294 void Add(Handle<Map> handle, Zone* zone) {
295 list_.Add(handle.location(), zone);
298 Handle<Map> at(int i) const {
299 return Handle<Map>(list_.at(i));
302 Handle<Map> first() const { return at(0); }
303 Handle<Map> last() const { return at(length() - 1); }
306 // The list stores pointers to Map*, that is Map**, so it's GC safe.
307 SmallPointerList<Map*> list_;
309 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
313 class Expression : public AstNode {
316 // Not assigned a context yet, or else will not be visited during
319 // Evaluated for its side effects.
321 // Evaluated for its value (and side effects).
323 // Evaluated for control flow (and side effects).
327 virtual bool IsValidReferenceExpression() const { return false; }
329 // Helpers for ToBoolean conversion.
330 virtual bool ToBooleanIsTrue() const { return false; }
331 virtual bool ToBooleanIsFalse() const { return false; }
333 // Symbols that cannot be parsed as array indices are considered property
334 // names. We do not treat symbols that can be array indexes as property
335 // names because [] for string objects is handled only by keyed ICs.
336 virtual bool IsPropertyName() const { return false; }
338 // True iff the expression is a literal represented as a smi.
339 bool IsSmiLiteral() const;
341 // True iff the expression is a string literal.
342 bool IsStringLiteral() const;
344 // True iff the expression is the null literal.
345 bool IsNullLiteral() const;
347 // True if we can prove that the expression is the undefined literal.
348 bool IsUndefinedLiteral(Isolate* isolate) const;
350 // Expression type bounds
351 Bounds bounds() const { return bounds_; }
352 void set_bounds(Bounds bounds) { bounds_ = bounds; }
354 // Whether the expression is parenthesized
355 bool is_parenthesized() const {
356 return IsParenthesizedField::decode(bit_field_);
358 bool is_multi_parenthesized() const {
359 return IsMultiParenthesizedField::decode(bit_field_);
361 void increase_parenthesization_level() {
363 IsMultiParenthesizedField::update(bit_field_, is_parenthesized());
364 bit_field_ = IsParenthesizedField::update(bit_field_, true);
367 // Type feedback information for assignments and properties.
368 virtual bool IsMonomorphic() {
372 virtual SmallMapList* GetReceiverTypes() {
376 virtual KeyedAccessStoreMode GetStoreMode() const {
378 return STANDARD_STORE;
380 virtual IcCheckType GetKeyType() const {
385 // TODO(rossberg): this should move to its own AST node eventually.
386 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
387 byte to_boolean_types() const {
388 return ToBooleanTypesField::decode(bit_field_);
391 void set_base_id(int id) { base_id_ = id; }
392 static int num_ids() { return parent_num_ids() + 2; }
393 BailoutId id() const { return BailoutId(local_id(0)); }
394 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
397 Expression(Zone* zone, int pos)
399 base_id_(BailoutId::None().ToInt()),
400 bounds_(Bounds::Unbounded(zone)),
402 static int parent_num_ids() { return 0; }
403 void set_to_boolean_types(byte types) {
404 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
407 int base_id() const {
408 DCHECK(!BailoutId(base_id_).IsNone());
413 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
417 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
418 class IsParenthesizedField : public BitField16<bool, 8, 1> {};
419 class IsMultiParenthesizedField : public BitField16<bool, 9, 1> {};
421 // Ends with 16-bit field; deriving classes in turn begin with
422 // 16-bit fields for optimum packing efficiency.
426 class BreakableStatement : public Statement {
429 TARGET_FOR_ANONYMOUS,
430 TARGET_FOR_NAMED_ONLY
433 // The labels associated with this statement. May be NULL;
434 // if it is != NULL, guaranteed to contain at least one entry.
435 ZoneList<const AstRawString*>* labels() const { return labels_; }
437 // Type testing & conversion.
438 BreakableStatement* AsBreakableStatement() FINAL { return this; }
441 Label* break_target() { return &break_target_; }
444 bool is_target_for_anonymous() const {
445 return breakable_type_ == TARGET_FOR_ANONYMOUS;
448 void set_base_id(int id) { base_id_ = id; }
449 static int num_ids() { return parent_num_ids() + 2; }
450 BailoutId EntryId() const { return BailoutId(local_id(0)); }
451 BailoutId ExitId() const { return BailoutId(local_id(1)); }
454 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
455 BreakableType breakable_type, int position)
456 : Statement(zone, position),
458 breakable_type_(breakable_type),
459 base_id_(BailoutId::None().ToInt()) {
460 DCHECK(labels == NULL || labels->length() > 0);
462 static int parent_num_ids() { return 0; }
464 int base_id() const {
465 DCHECK(!BailoutId(base_id_).IsNone());
470 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
472 ZoneList<const AstRawString*>* labels_;
473 BreakableType breakable_type_;
479 class Block FINAL : public BreakableStatement {
481 DECLARE_NODE_TYPE(Block)
483 void AddStatement(Statement* statement, Zone* zone) {
484 statements_.Add(statement, zone);
487 ZoneList<Statement*>* statements() { return &statements_; }
488 bool is_initializer_block() const { return is_initializer_block_; }
490 static int num_ids() { return parent_num_ids() + 1; }
491 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
493 bool IsJump() const OVERRIDE {
494 return !statements_.is_empty() && statements_.last()->IsJump()
495 && labels() == NULL; // Good enough as an approximation...
498 Scope* scope() const { return scope_; }
499 void set_scope(Scope* scope) { scope_ = scope; }
502 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
503 bool is_initializer_block, int pos)
504 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
505 statements_(capacity, zone),
506 is_initializer_block_(is_initializer_block),
508 static int parent_num_ids() { return BreakableStatement::num_ids(); }
511 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
513 ZoneList<Statement*> statements_;
514 bool is_initializer_block_;
519 class Declaration : public AstNode {
521 VariableProxy* proxy() const { return proxy_; }
522 VariableMode mode() const { return mode_; }
523 Scope* scope() const { return scope_; }
524 virtual InitializationFlag initialization() const = 0;
525 virtual bool IsInlineable() const;
528 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
530 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
531 DCHECK(IsDeclaredVariableMode(mode));
536 VariableProxy* proxy_;
538 // Nested scope from which the declaration originated.
543 class VariableDeclaration FINAL : public Declaration {
545 DECLARE_NODE_TYPE(VariableDeclaration)
547 InitializationFlag initialization() const OVERRIDE {
548 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
552 VariableDeclaration(Zone* zone,
553 VariableProxy* proxy,
557 : Declaration(zone, proxy, mode, scope, pos) {
562 class FunctionDeclaration FINAL : public Declaration {
564 DECLARE_NODE_TYPE(FunctionDeclaration)
566 FunctionLiteral* fun() const { return fun_; }
567 InitializationFlag initialization() const OVERRIDE {
568 return kCreatedInitialized;
570 bool IsInlineable() const OVERRIDE;
573 FunctionDeclaration(Zone* zone,
574 VariableProxy* proxy,
576 FunctionLiteral* fun,
579 : Declaration(zone, proxy, mode, scope, pos),
581 // At the moment there are no "const functions" in JavaScript...
582 DCHECK(mode == VAR || mode == LET);
587 FunctionLiteral* fun_;
591 class ModuleDeclaration FINAL : public Declaration {
593 DECLARE_NODE_TYPE(ModuleDeclaration)
595 Module* module() const { return module_; }
596 InitializationFlag initialization() const OVERRIDE {
597 return kCreatedInitialized;
601 ModuleDeclaration(Zone* zone,
602 VariableProxy* proxy,
606 : Declaration(zone, proxy, MODULE, scope, pos),
615 class ImportDeclaration FINAL : public Declaration {
617 DECLARE_NODE_TYPE(ImportDeclaration)
619 Module* module() const { return module_; }
620 InitializationFlag initialization() const OVERRIDE {
621 return kCreatedInitialized;
625 ImportDeclaration(Zone* zone,
626 VariableProxy* proxy,
630 : Declaration(zone, proxy, LET, scope, pos),
639 class ExportDeclaration FINAL : public Declaration {
641 DECLARE_NODE_TYPE(ExportDeclaration)
643 InitializationFlag initialization() const OVERRIDE {
644 return kCreatedInitialized;
648 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
649 : Declaration(zone, proxy, LET, scope, pos) {}
653 class Module : public AstNode {
655 Interface* interface() const { return interface_; }
656 Block* body() const { return body_; }
659 Module(Zone* zone, int pos)
661 interface_(Interface::NewModule(zone)),
663 Module(Zone* zone, Interface* interface, int pos, Block* body = NULL)
665 interface_(interface),
669 Interface* interface_;
674 class ModuleLiteral FINAL : public Module {
676 DECLARE_NODE_TYPE(ModuleLiteral)
679 ModuleLiteral(Zone* zone, Block* body, Interface* interface, int pos)
680 : Module(zone, interface, pos, body) {}
684 class ModuleVariable FINAL : public Module {
686 DECLARE_NODE_TYPE(ModuleVariable)
688 VariableProxy* proxy() const { return proxy_; }
691 inline ModuleVariable(Zone* zone, VariableProxy* proxy, int pos);
694 VariableProxy* proxy_;
698 class ModulePath FINAL : public Module {
700 DECLARE_NODE_TYPE(ModulePath)
702 Module* module() const { return module_; }
703 Handle<String> name() const { return name_->string(); }
706 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
707 : Module(zone, pos), module_(module), name_(name) {}
711 const AstRawString* name_;
715 class ModuleUrl FINAL : public Module {
717 DECLARE_NODE_TYPE(ModuleUrl)
719 Handle<String> url() const { return url_; }
722 ModuleUrl(Zone* zone, Handle<String> url, int pos)
723 : Module(zone, pos), url_(url) {
731 class ModuleStatement FINAL : public Statement {
733 DECLARE_NODE_TYPE(ModuleStatement)
735 VariableProxy* proxy() const { return proxy_; }
736 Block* body() const { return body_; }
739 ModuleStatement(Zone* zone, VariableProxy* proxy, Block* body, int pos)
740 : Statement(zone, pos),
746 VariableProxy* proxy_;
751 class IterationStatement : public BreakableStatement {
753 // Type testing & conversion.
754 IterationStatement* AsIterationStatement() FINAL { return this; }
756 Statement* body() const { return body_; }
758 static int num_ids() { return parent_num_ids() + 1; }
759 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
760 virtual BailoutId ContinueId() const = 0;
761 virtual BailoutId StackCheckId() const = 0;
764 Label* continue_target() { return &continue_target_; }
767 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
768 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
770 static int parent_num_ids() { return BreakableStatement::num_ids(); }
771 void Initialize(Statement* body) { body_ = body; }
774 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
777 Label continue_target_;
781 class DoWhileStatement FINAL : public IterationStatement {
783 DECLARE_NODE_TYPE(DoWhileStatement)
785 void Initialize(Expression* cond, Statement* body) {
786 IterationStatement::Initialize(body);
790 Expression* cond() const { return cond_; }
792 static int num_ids() { return parent_num_ids() + 2; }
793 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
794 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
795 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
798 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
799 : IterationStatement(zone, labels, pos), cond_(NULL) {}
800 static int parent_num_ids() { return IterationStatement::num_ids(); }
803 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
809 class WhileStatement FINAL : public IterationStatement {
811 DECLARE_NODE_TYPE(WhileStatement)
813 void Initialize(Expression* cond, Statement* body) {
814 IterationStatement::Initialize(body);
818 Expression* cond() const { return cond_; }
820 static int num_ids() { return parent_num_ids() + 1; }
821 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
822 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
823 BailoutId BodyId() const { return BailoutId(local_id(0)); }
826 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
827 : IterationStatement(zone, labels, pos), cond_(NULL) {}
828 static int parent_num_ids() { return IterationStatement::num_ids(); }
831 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
837 class ForStatement FINAL : public IterationStatement {
839 DECLARE_NODE_TYPE(ForStatement)
841 void Initialize(Statement* init,
845 IterationStatement::Initialize(body);
851 Statement* init() const { return init_; }
852 Expression* cond() const { return cond_; }
853 Statement* next() const { return next_; }
855 static int num_ids() { return parent_num_ids() + 2; }
856 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
857 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
858 BailoutId BodyId() const { return BailoutId(local_id(1)); }
861 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
862 : IterationStatement(zone, labels, pos),
866 static int parent_num_ids() { return IterationStatement::num_ids(); }
869 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
877 class ForEachStatement : public IterationStatement {
880 ENUMERATE, // for (each in subject) body;
881 ITERATE // for (each of subject) body;
884 void Initialize(Expression* each, Expression* subject, Statement* body) {
885 IterationStatement::Initialize(body);
890 Expression* each() const { return each_; }
891 Expression* subject() const { return subject_; }
894 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
895 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
899 Expression* subject_;
903 class ForInStatement FINAL : public ForEachStatement {
905 DECLARE_NODE_TYPE(ForInStatement)
907 Expression* enumerable() const {
911 // Type feedback information.
912 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
913 Isolate* isolate) OVERRIDE {
914 return FeedbackVectorRequirements(1, 0);
916 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
917 for_in_feedback_slot_ = slot;
920 FeedbackVectorSlot ForInFeedbackSlot() {
921 DCHECK(!for_in_feedback_slot_.IsInvalid());
922 return for_in_feedback_slot_;
925 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
926 ForInType for_in_type() const { return for_in_type_; }
927 void set_for_in_type(ForInType type) { for_in_type_ = type; }
929 static int num_ids() { return parent_num_ids() + 5; }
930 BailoutId BodyId() const { return BailoutId(local_id(0)); }
931 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
932 BailoutId EnumId() const { return BailoutId(local_id(2)); }
933 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
934 BailoutId AssignmentId() const { return BailoutId(local_id(4)); }
935 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
936 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
939 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
940 : ForEachStatement(zone, labels, pos),
941 for_in_type_(SLOW_FOR_IN),
942 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
943 static int parent_num_ids() { return ForEachStatement::num_ids(); }
946 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
948 ForInType for_in_type_;
949 FeedbackVectorSlot for_in_feedback_slot_;
953 class ForOfStatement FINAL : public ForEachStatement {
955 DECLARE_NODE_TYPE(ForOfStatement)
957 void Initialize(Expression* each,
960 Expression* assign_iterator,
961 Expression* next_result,
962 Expression* result_done,
963 Expression* assign_each) {
964 ForEachStatement::Initialize(each, subject, body);
965 assign_iterator_ = assign_iterator;
966 next_result_ = next_result;
967 result_done_ = result_done;
968 assign_each_ = assign_each;
971 Expression* iterable() const {
975 // var iterator = subject[Symbol.iterator]();
976 Expression* assign_iterator() const {
977 return assign_iterator_;
980 // var result = iterator.next();
981 Expression* next_result() const {
986 Expression* result_done() const {
990 // each = result.value
991 Expression* assign_each() const {
995 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
996 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
998 static int num_ids() { return parent_num_ids() + 1; }
999 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
1002 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1003 : ForEachStatement(zone, labels, pos),
1004 assign_iterator_(NULL),
1007 assign_each_(NULL) {}
1008 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1011 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1013 Expression* assign_iterator_;
1014 Expression* next_result_;
1015 Expression* result_done_;
1016 Expression* assign_each_;
1020 class ExpressionStatement FINAL : public Statement {
1022 DECLARE_NODE_TYPE(ExpressionStatement)
1024 void set_expression(Expression* e) { expression_ = e; }
1025 Expression* expression() const { return expression_; }
1026 bool IsJump() const OVERRIDE { return expression_->IsThrow(); }
1029 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1030 : Statement(zone, pos), expression_(expression) { }
1033 Expression* expression_;
1037 class JumpStatement : public Statement {
1039 bool IsJump() const FINAL { return true; }
1042 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1046 class ContinueStatement FINAL : public JumpStatement {
1048 DECLARE_NODE_TYPE(ContinueStatement)
1050 IterationStatement* target() const { return target_; }
1053 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1054 : JumpStatement(zone, pos), target_(target) { }
1057 IterationStatement* target_;
1061 class BreakStatement FINAL : public JumpStatement {
1063 DECLARE_NODE_TYPE(BreakStatement)
1065 BreakableStatement* target() const { return target_; }
1068 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1069 : JumpStatement(zone, pos), target_(target) { }
1072 BreakableStatement* target_;
1076 class ReturnStatement FINAL : public JumpStatement {
1078 DECLARE_NODE_TYPE(ReturnStatement)
1080 Expression* expression() const { return expression_; }
1083 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1084 : JumpStatement(zone, pos), expression_(expression) { }
1087 Expression* expression_;
1091 class WithStatement FINAL : public Statement {
1093 DECLARE_NODE_TYPE(WithStatement)
1095 Scope* scope() { return scope_; }
1096 Expression* expression() const { return expression_; }
1097 Statement* statement() const { return statement_; }
1099 void set_base_id(int id) { base_id_ = id; }
1100 static int num_ids() { return parent_num_ids() + 1; }
1101 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1104 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1105 Statement* statement, int pos)
1106 : Statement(zone, pos),
1108 expression_(expression),
1109 statement_(statement),
1110 base_id_(BailoutId::None().ToInt()) {}
1111 static int parent_num_ids() { return 0; }
1113 int base_id() const {
1114 DCHECK(!BailoutId(base_id_).IsNone());
1119 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1122 Expression* expression_;
1123 Statement* statement_;
1128 class CaseClause FINAL : public Expression {
1130 DECLARE_NODE_TYPE(CaseClause)
1132 bool is_default() const { return label_ == NULL; }
1133 Expression* label() const {
1134 CHECK(!is_default());
1137 Label* body_target() { return &body_target_; }
1138 ZoneList<Statement*>* statements() const { return statements_; }
1140 static int num_ids() { return parent_num_ids() + 2; }
1141 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1142 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1144 Type* compare_type() { return compare_type_; }
1145 void set_compare_type(Type* type) { compare_type_ = type; }
1148 static int parent_num_ids() { return Expression::num_ids(); }
1151 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1153 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1157 ZoneList<Statement*>* statements_;
1158 Type* compare_type_;
1162 class SwitchStatement FINAL : public BreakableStatement {
1164 DECLARE_NODE_TYPE(SwitchStatement)
1166 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1171 Expression* tag() const { return tag_; }
1172 ZoneList<CaseClause*>* cases() const { return cases_; }
1175 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1176 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1182 ZoneList<CaseClause*>* cases_;
1186 // If-statements always have non-null references to their then- and
1187 // else-parts. When parsing if-statements with no explicit else-part,
1188 // the parser implicitly creates an empty statement. Use the
1189 // HasThenStatement() and HasElseStatement() functions to check if a
1190 // given if-statement has a then- or an else-part containing code.
1191 class IfStatement FINAL : public Statement {
1193 DECLARE_NODE_TYPE(IfStatement)
1195 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1196 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1198 Expression* condition() const { return condition_; }
1199 Statement* then_statement() const { return then_statement_; }
1200 Statement* else_statement() const { return else_statement_; }
1202 bool IsJump() const OVERRIDE {
1203 return HasThenStatement() && then_statement()->IsJump()
1204 && HasElseStatement() && else_statement()->IsJump();
1207 void set_base_id(int id) { base_id_ = id; }
1208 static int num_ids() { return parent_num_ids() + 3; }
1209 BailoutId IfId() const { return BailoutId(local_id(0)); }
1210 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1211 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1214 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1215 Statement* else_statement, int pos)
1216 : Statement(zone, pos),
1217 condition_(condition),
1218 then_statement_(then_statement),
1219 else_statement_(else_statement),
1220 base_id_(BailoutId::None().ToInt()) {}
1221 static int parent_num_ids() { return 0; }
1223 int base_id() const {
1224 DCHECK(!BailoutId(base_id_).IsNone());
1229 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1231 Expression* condition_;
1232 Statement* then_statement_;
1233 Statement* else_statement_;
1238 class TryStatement : public Statement {
1240 int index() const { return index_; }
1241 Block* try_block() const { return try_block_; }
1244 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1245 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1248 // Unique (per-function) index of this handler. This is not an AST ID.
1255 class TryCatchStatement FINAL : public TryStatement {
1257 DECLARE_NODE_TYPE(TryCatchStatement)
1259 Scope* scope() { return scope_; }
1260 Variable* variable() { return variable_; }
1261 Block* catch_block() const { return catch_block_; }
1264 TryCatchStatement(Zone* zone,
1271 : TryStatement(zone, index, try_block, pos),
1273 variable_(variable),
1274 catch_block_(catch_block) {
1279 Variable* variable_;
1280 Block* catch_block_;
1284 class TryFinallyStatement FINAL : public TryStatement {
1286 DECLARE_NODE_TYPE(TryFinallyStatement)
1288 Block* finally_block() const { return finally_block_; }
1291 TryFinallyStatement(
1292 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1293 : TryStatement(zone, index, try_block, pos),
1294 finally_block_(finally_block) { }
1297 Block* finally_block_;
1301 class DebuggerStatement FINAL : public Statement {
1303 DECLARE_NODE_TYPE(DebuggerStatement)
1305 void set_base_id(int id) { base_id_ = id; }
1306 static int num_ids() { return parent_num_ids() + 1; }
1307 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1310 explicit DebuggerStatement(Zone* zone, int pos)
1311 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1312 static int parent_num_ids() { return 0; }
1314 int base_id() const {
1315 DCHECK(!BailoutId(base_id_).IsNone());
1320 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1326 class EmptyStatement FINAL : public Statement {
1328 DECLARE_NODE_TYPE(EmptyStatement)
1331 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1335 class Literal FINAL : public Expression {
1337 DECLARE_NODE_TYPE(Literal)
1339 bool IsPropertyName() const OVERRIDE { return value_->IsPropertyName(); }
1341 Handle<String> AsPropertyName() {
1342 DCHECK(IsPropertyName());
1343 return Handle<String>::cast(value());
1346 const AstRawString* AsRawPropertyName() {
1347 DCHECK(IsPropertyName());
1348 return value_->AsString();
1351 bool ToBooleanIsTrue() const OVERRIDE { return value()->BooleanValue(); }
1352 bool ToBooleanIsFalse() const OVERRIDE { return !value()->BooleanValue(); }
1354 Handle<Object> value() const { return value_->value(); }
1355 const AstValue* raw_value() const { return value_; }
1357 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1358 // only for string and number literals!
1360 static bool Match(void* literal1, void* literal2);
1362 static int num_ids() { return parent_num_ids() + 1; }
1363 TypeFeedbackId LiteralFeedbackId() const {
1364 return TypeFeedbackId(local_id(0));
1368 Literal(Zone* zone, const AstValue* value, int position)
1369 : Expression(zone, position), value_(value) {}
1370 static int parent_num_ids() { return Expression::num_ids(); }
1373 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1375 const AstValue* value_;
1379 // Base class for literals that needs space in the corresponding JSFunction.
1380 class MaterializedLiteral : public Expression {
1382 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1384 int literal_index() { return literal_index_; }
1387 // only callable after initialization.
1388 DCHECK(depth_ >= 1);
1393 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1394 : Expression(zone, pos),
1395 literal_index_(literal_index),
1399 // A materialized literal is simple if the values consist of only
1400 // constants and simple object and array literals.
1401 bool is_simple() const { return is_simple_; }
1402 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1403 friend class CompileTimeValue;
1405 void set_depth(int depth) {
1410 // Populate the constant properties/elements fixed array.
1411 void BuildConstants(Isolate* isolate);
1412 friend class ArrayLiteral;
1413 friend class ObjectLiteral;
1415 // If the expression is a literal, return the literal value;
1416 // if the expression is a materialized literal and is simple return a
1417 // compile time value as encoded by CompileTimeValue::GetValue().
1418 // Otherwise, return undefined literal as the placeholder
1419 // in the object literal boilerplate.
1420 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1429 // Property is used for passing information
1430 // about an object literal's properties from the parser
1431 // to the code generator.
1432 class ObjectLiteralProperty FINAL : public ZoneObject {
1435 CONSTANT, // Property with constant value (compile time).
1436 COMPUTED, // Property with computed value (execution time).
1437 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1438 GETTER, SETTER, // Property is an accessor function.
1439 PROTOTYPE // Property is __proto__.
1442 Expression* key() { return key_; }
1443 Expression* value() { return value_; }
1444 Kind kind() { return kind_; }
1446 // Type feedback information.
1447 void RecordTypeFeedback(TypeFeedbackOracle* oracle);
1448 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1449 Handle<Map> GetReceiverType() { return receiver_type_; }
1451 bool IsCompileTimeValue();
1453 void set_emit_store(bool emit_store);
1456 bool is_static() const { return is_static_; }
1457 bool is_computed_name() const { return is_computed_name_; }
1460 friend class AstNodeFactory;
1462 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1463 bool is_static, bool is_computed_name);
1464 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1465 Expression* value, bool is_static,
1466 bool is_computed_name);
1474 bool is_computed_name_;
1475 Handle<Map> receiver_type_;
1479 // An object literal has a boilerplate object that is used
1480 // for minimizing the work when constructing it at runtime.
1481 class ObjectLiteral FINAL : public MaterializedLiteral {
1483 typedef ObjectLiteralProperty Property;
1485 DECLARE_NODE_TYPE(ObjectLiteral)
1487 Handle<FixedArray> constant_properties() const {
1488 return constant_properties_;
1490 ZoneList<Property*>* properties() const { return properties_; }
1491 bool fast_elements() const { return fast_elements_; }
1492 bool may_store_doubles() const { return may_store_doubles_; }
1493 bool has_function() const { return has_function_; }
1495 // Decide if a property should be in the object boilerplate.
1496 static bool IsBoilerplateProperty(Property* property);
1498 // Populate the constant properties fixed array.
1499 void BuildConstantProperties(Isolate* isolate);
1501 // Mark all computed expressions that are bound to a key that
1502 // is shadowed by a later occurrence of the same key. For the
1503 // marked expressions, no store code is emitted.
1504 void CalculateEmitStore(Zone* zone);
1506 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1507 int ComputeFlags() const {
1508 int flags = fast_elements() ? kFastElements : kNoFlags;
1509 flags |= has_function() ? kHasFunction : kNoFlags;
1516 kHasFunction = 1 << 1
1519 struct Accessors: public ZoneObject {
1520 Accessors() : getter(NULL), setter(NULL) {}
1525 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1527 // Return an AST id for a property that is used in simulate instructions.
1528 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1530 // Unlike other AST nodes, this number of bailout IDs allocated for an
1531 // ObjectLiteral can vary, so num_ids() is not a static method.
1532 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1535 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1536 int boilerplate_properties, bool has_function, int pos)
1537 : MaterializedLiteral(zone, literal_index, pos),
1538 properties_(properties),
1539 boilerplate_properties_(boilerplate_properties),
1540 fast_elements_(false),
1541 may_store_doubles_(false),
1542 has_function_(has_function) {}
1543 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1546 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1547 Handle<FixedArray> constant_properties_;
1548 ZoneList<Property*>* properties_;
1549 int boilerplate_properties_;
1550 bool fast_elements_;
1551 bool may_store_doubles_;
1556 // Node for capturing a regexp literal.
1557 class RegExpLiteral FINAL : public MaterializedLiteral {
1559 DECLARE_NODE_TYPE(RegExpLiteral)
1561 Handle<String> pattern() const { return pattern_->string(); }
1562 Handle<String> flags() const { return flags_->string(); }
1565 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1566 const AstRawString* flags, int literal_index, int pos)
1567 : MaterializedLiteral(zone, literal_index, pos),
1574 const AstRawString* pattern_;
1575 const AstRawString* flags_;
1579 // An array literal has a literals object that is used
1580 // for minimizing the work when constructing it at runtime.
1581 class ArrayLiteral FINAL : public MaterializedLiteral {
1583 DECLARE_NODE_TYPE(ArrayLiteral)
1585 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1586 ZoneList<Expression*>* values() const { return values_; }
1588 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1590 // Return an AST id for an element that is used in simulate instructions.
1591 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1593 // Unlike other AST nodes, this number of bailout IDs allocated for an
1594 // ArrayLiteral can vary, so num_ids() is not a static method.
1595 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1597 // Populate the constant elements fixed array.
1598 void BuildConstantElements(Isolate* isolate);
1600 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1601 int ComputeFlags() const {
1602 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1603 flags |= ArrayLiteral::kDisableMementos;
1609 kShallowElements = 1,
1610 kDisableMementos = 1 << 1
1614 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1616 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1617 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1620 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1622 Handle<FixedArray> constant_elements_;
1623 ZoneList<Expression*>* values_;
1627 class VariableProxy FINAL : public Expression {
1629 DECLARE_NODE_TYPE(VariableProxy)
1631 bool IsValidReferenceExpression() const OVERRIDE {
1632 return !is_resolved() || var()->IsValidReference();
1635 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1637 Handle<String> name() const { return raw_name()->string(); }
1638 const AstRawString* raw_name() const {
1639 return is_resolved() ? var_->raw_name() : raw_name_;
1642 Variable* var() const {
1643 DCHECK(is_resolved());
1646 void set_var(Variable* v) {
1647 DCHECK(!is_resolved());
1652 bool is_this() const { return IsThisField::decode(bit_field_); }
1654 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1655 void set_is_assigned() {
1656 bit_field_ = IsAssignedField::update(bit_field_, true);
1659 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1660 void set_is_resolved() {
1661 bit_field_ = IsResolvedField::update(bit_field_, true);
1664 Interface* interface() const { return interface_; }
1666 // Bind this proxy to the variable var. Interfaces must match.
1667 void BindTo(Variable* var);
1669 bool UsesVariableFeedbackSlot() const {
1670 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1673 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1674 Isolate* isolate) OVERRIDE {
1675 return FeedbackVectorRequirements(0, UsesVariableFeedbackSlot() ? 1 : 0);
1678 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1679 variable_feedback_slot_ = slot;
1681 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1682 FeedbackVectorICSlot VariableFeedbackSlot() {
1683 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1684 return variable_feedback_slot_;
1688 VariableProxy(Zone* zone, Variable* var, int position);
1690 VariableProxy(Zone* zone, const AstRawString* name, bool is_this,
1691 Interface* interface, int position);
1693 class IsThisField : public BitField8<bool, 0, 1> {};
1694 class IsAssignedField : public BitField8<bool, 1, 1> {};
1695 class IsResolvedField : public BitField8<bool, 2, 1> {};
1697 // Start with 16-bit (or smaller) field, which should get packed together
1698 // with Expression's trailing 16-bit field.
1700 FeedbackVectorICSlot variable_feedback_slot_;
1702 const AstRawString* raw_name_; // if !is_resolved_
1703 Variable* var_; // if is_resolved_
1705 Interface* interface_;
1709 class Property FINAL : public Expression {
1711 DECLARE_NODE_TYPE(Property)
1713 bool IsValidReferenceExpression() const OVERRIDE { return true; }
1715 Expression* obj() const { return obj_; }
1716 Expression* key() const { return key_; }
1718 static int num_ids() { return parent_num_ids() + 2; }
1719 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1720 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1722 bool IsStringAccess() const {
1723 return IsStringAccessField::decode(bit_field_);
1726 // Type feedback information.
1727 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
1728 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
1729 KeyedAccessStoreMode GetStoreMode() const OVERRIDE { return STANDARD_STORE; }
1730 IcCheckType GetKeyType() const OVERRIDE {
1731 return KeyTypeField::decode(bit_field_);
1733 bool IsUninitialized() const {
1734 return !is_for_call() && HasNoTypeInformation();
1736 bool HasNoTypeInformation() const {
1737 return IsUninitializedField::decode(bit_field_);
1739 void set_is_uninitialized(bool b) {
1740 bit_field_ = IsUninitializedField::update(bit_field_, b);
1742 void set_is_string_access(bool b) {
1743 bit_field_ = IsStringAccessField::update(bit_field_, b);
1745 void set_key_type(IcCheckType key_type) {
1746 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1748 void mark_for_call() {
1749 bit_field_ = IsForCallField::update(bit_field_, true);
1751 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1753 bool IsSuperAccess() {
1754 return obj()->IsSuperReference();
1757 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1758 Isolate* isolate) OVERRIDE {
1759 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1761 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1762 property_feedback_slot_ = slot;
1764 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1765 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1768 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1769 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1770 return property_feedback_slot_;
1774 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1775 : Expression(zone, pos),
1776 bit_field_(IsForCallField::encode(false) |
1777 IsUninitializedField::encode(false) |
1778 IsStringAccessField::encode(false)),
1779 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1782 static int parent_num_ids() { return Expression::num_ids(); }
1785 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1787 class IsForCallField : public BitField8<bool, 0, 1> {};
1788 class IsUninitializedField : public BitField8<bool, 1, 1> {};
1789 class IsStringAccessField : public BitField8<bool, 2, 1> {};
1790 class KeyTypeField : public BitField8<IcCheckType, 3, 1> {};
1792 FeedbackVectorICSlot property_feedback_slot_;
1795 SmallMapList receiver_types_;
1799 class Call FINAL : public Expression {
1801 DECLARE_NODE_TYPE(Call)
1803 Expression* expression() const { return expression_; }
1804 ZoneList<Expression*>* arguments() const { return arguments_; }
1806 // Type feedback information.
1807 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1808 Isolate* isolate) OVERRIDE;
1809 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1810 ic_slot_or_slot_ = slot.ToInt();
1812 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1813 ic_slot_or_slot_ = slot.ToInt();
1815 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1817 FeedbackVectorSlot CallFeedbackSlot() const {
1818 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1819 return FeedbackVectorSlot(ic_slot_or_slot_);
1822 FeedbackVectorICSlot CallFeedbackICSlot() const {
1823 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1824 return FeedbackVectorICSlot(ic_slot_or_slot_);
1827 SmallMapList* GetReceiverTypes() OVERRIDE {
1828 if (expression()->IsProperty()) {
1829 return expression()->AsProperty()->GetReceiverTypes();
1834 bool IsMonomorphic() OVERRIDE {
1835 if (expression()->IsProperty()) {
1836 return expression()->AsProperty()->IsMonomorphic();
1838 return !target_.is_null();
1841 bool global_call() const {
1842 VariableProxy* proxy = expression_->AsVariableProxy();
1843 return proxy != NULL && proxy->var()->IsUnallocated();
1846 bool known_global_function() const {
1847 return global_call() && !target_.is_null();
1850 Handle<JSFunction> target() { return target_; }
1852 Handle<Cell> cell() { return cell_; }
1854 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1856 void set_target(Handle<JSFunction> target) { target_ = target; }
1857 void set_allocation_site(Handle<AllocationSite> site) {
1858 allocation_site_ = site;
1860 bool ComputeGlobalTarget(Handle<GlobalObject> global, LookupIterator* it);
1862 static int num_ids() { return parent_num_ids() + 2; }
1863 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1864 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1866 bool is_uninitialized() const {
1867 return IsUninitializedField::decode(bit_field_);
1869 void set_is_uninitialized(bool b) {
1870 bit_field_ = IsUninitializedField::update(bit_field_, b);
1882 // Helpers to determine how to handle the call.
1883 CallType GetCallType(Isolate* isolate) const;
1884 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1885 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1888 // Used to assert that the FullCodeGenerator records the return site.
1889 bool return_is_recorded_;
1893 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1895 : Expression(zone, pos),
1896 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1897 expression_(expression),
1898 arguments_(arguments),
1899 bit_field_(IsUninitializedField::encode(false)) {
1900 if (expression->IsProperty()) {
1901 expression->AsProperty()->mark_for_call();
1904 static int parent_num_ids() { return Expression::num_ids(); }
1907 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1909 // We store this as an integer because we don't know if we have a slot or
1910 // an ic slot until scoping time.
1911 int ic_slot_or_slot_;
1912 Expression* expression_;
1913 ZoneList<Expression*>* arguments_;
1914 Handle<JSFunction> target_;
1916 Handle<AllocationSite> allocation_site_;
1917 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1922 class CallNew FINAL : public Expression {
1924 DECLARE_NODE_TYPE(CallNew)
1926 Expression* expression() const { return expression_; }
1927 ZoneList<Expression*>* arguments() const { return arguments_; }
1929 // Type feedback information.
1930 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1931 Isolate* isolate) OVERRIDE {
1932 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1934 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1935 callnew_feedback_slot_ = slot;
1938 FeedbackVectorSlot CallNewFeedbackSlot() {
1939 DCHECK(!callnew_feedback_slot_.IsInvalid());
1940 return callnew_feedback_slot_;
1942 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1943 DCHECK(FLAG_pretenuring_call_new);
1944 return CallNewFeedbackSlot().next();
1947 void RecordTypeFeedback(TypeFeedbackOracle* oracle);
1948 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1949 Handle<JSFunction> target() const { return target_; }
1950 Handle<AllocationSite> allocation_site() const {
1951 return allocation_site_;
1954 static int num_ids() { return parent_num_ids() + 1; }
1955 static int feedback_slots() { return 1; }
1956 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
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) OVERRIDE {
2001 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2003 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2004 callruntime_feedback_slot_ = slot;
2006 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2008 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2009 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2010 !callruntime_feedback_slot_.IsInvalid());
2011 return callruntime_feedback_slot_;
2014 static int num_ids() { return parent_num_ids() + 1; }
2015 TypeFeedbackId CallRuntimeFeedbackId() const {
2016 return TypeFeedbackId(local_id(0));
2020 CallRuntime(Zone* zone, const AstRawString* name,
2021 const Runtime::Function* function,
2022 ZoneList<Expression*>* arguments, int pos)
2023 : Expression(zone, pos),
2025 function_(function),
2026 arguments_(arguments),
2027 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2028 static int parent_num_ids() { return Expression::num_ids(); }
2031 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2033 const AstRawString* raw_name_;
2034 const Runtime::Function* function_;
2035 ZoneList<Expression*>* arguments_;
2036 FeedbackVectorICSlot callruntime_feedback_slot_;
2040 class UnaryOperation FINAL : public Expression {
2042 DECLARE_NODE_TYPE(UnaryOperation)
2044 Token::Value op() const { return op_; }
2045 Expression* expression() const { return expression_; }
2047 // For unary not (Token::NOT), the AST ids where true and false will
2048 // actually be materialized, respectively.
2049 static int num_ids() { return parent_num_ids() + 2; }
2050 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2051 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2053 virtual void RecordToBooleanTypeFeedback(
2054 TypeFeedbackOracle* oracle) OVERRIDE;
2057 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2058 : Expression(zone, pos), op_(op), expression_(expression) {
2059 DCHECK(Token::IsUnaryOp(op));
2061 static int parent_num_ids() { return Expression::num_ids(); }
2064 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2067 Expression* expression_;
2071 class BinaryOperation FINAL : public Expression {
2073 DECLARE_NODE_TYPE(BinaryOperation)
2075 Token::Value op() const { return static_cast<Token::Value>(op_); }
2076 Expression* left() const { return left_; }
2077 Expression* right() const { return right_; }
2078 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2079 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2080 allocation_site_ = allocation_site;
2083 // The short-circuit logical operations need an AST ID for their
2084 // right-hand subexpression.
2085 static int num_ids() { return parent_num_ids() + 2; }
2086 BailoutId RightId() const { return BailoutId(local_id(0)); }
2088 TypeFeedbackId BinaryOperationFeedbackId() const {
2089 return TypeFeedbackId(local_id(1));
2091 Maybe<int> fixed_right_arg() const {
2092 return has_fixed_right_arg_ ? Maybe<int>(fixed_right_arg_value_)
2095 void set_fixed_right_arg(Maybe<int> arg) {
2096 has_fixed_right_arg_ = arg.has_value;
2097 if (arg.has_value) fixed_right_arg_value_ = arg.value;
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) OVERRIDE {
2375 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2377 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2378 yield_first_feedback_slot_ = slot;
2380 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2381 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2384 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2385 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2386 return yield_first_feedback_slot_;
2389 FeedbackVectorICSlot DoneFeedbackSlot() {
2390 return KeyedLoadFeedbackSlot().next();
2393 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2396 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2397 Kind yield_kind, int pos)
2398 : Expression(zone, pos),
2399 generator_object_(generator_object),
2400 expression_(expression),
2401 yield_kind_(yield_kind),
2403 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2406 Expression* generator_object_;
2407 Expression* expression_;
2410 FeedbackVectorICSlot yield_first_feedback_slot_;
2414 class Throw FINAL : public Expression {
2416 DECLARE_NODE_TYPE(Throw)
2418 Expression* exception() const { return exception_; }
2421 Throw(Zone* zone, Expression* exception, int pos)
2422 : Expression(zone, pos), exception_(exception) {}
2425 Expression* exception_;
2429 class FunctionLiteral FINAL : public Expression {
2432 ANONYMOUS_EXPRESSION,
2437 enum ParameterFlag {
2438 kNoDuplicateParameters = 0,
2439 kHasDuplicateParameters = 1
2442 enum IsFunctionFlag {
2447 enum IsParenthesizedFlag {
2452 enum ArityRestriction {
2458 DECLARE_NODE_TYPE(FunctionLiteral)
2460 Handle<String> name() const { return raw_name_->string(); }
2461 const AstRawString* raw_name() const { return raw_name_; }
2462 Scope* scope() const { return scope_; }
2463 ZoneList<Statement*>* body() const { return body_; }
2464 void set_function_token_position(int pos) { function_token_position_ = pos; }
2465 int function_token_position() const { return function_token_position_; }
2466 int start_position() const;
2467 int end_position() const;
2468 int SourceSize() const { return end_position() - start_position(); }
2469 bool is_expression() const { return IsExpression::decode(bitfield_); }
2470 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2471 LanguageMode language_mode() const;
2472 bool uses_super_property() const;
2473 bool uses_super_constructor_call() 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 FeedbackVectorSpec& 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 dont_optimize_reason_(kNoReason),
2580 materialized_literal_count_(materialized_literal_count),
2581 expected_property_count_(expected_property_count),
2582 handler_count_(handler_count),
2583 parameter_count_(parameter_count),
2584 function_token_position_(RelocInfo::kNoPosition) {
2585 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2586 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2587 Pretenure::encode(false) |
2588 HasDuplicateParameters::encode(has_duplicate_parameters) |
2589 IsFunction::encode(is_function) |
2590 IsParenthesized::encode(is_parenthesized) |
2591 FunctionKindBits::encode(kind);
2592 DCHECK(IsValidFunctionKind(kind));
2596 const AstRawString* raw_name_;
2597 Handle<String> name_;
2598 Handle<SharedFunctionInfo> shared_info_;
2600 ZoneList<Statement*>* body_;
2601 const AstString* raw_inferred_name_;
2602 Handle<String> inferred_name_;
2603 AstProperties ast_properties_;
2604 BailoutReason dont_optimize_reason_;
2606 int materialized_literal_count_;
2607 int expected_property_count_;
2609 int parameter_count_;
2610 int function_token_position_;
2613 class IsExpression : public BitField<bool, 0, 1> {};
2614 class IsAnonymous : public BitField<bool, 1, 1> {};
2615 class Pretenure : public BitField<bool, 2, 1> {};
2616 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2617 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2618 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2619 class FunctionKindBits : public BitField<FunctionKind, 6, 7> {};
2623 class ClassLiteral FINAL : public Expression {
2625 typedef ObjectLiteralProperty Property;
2627 DECLARE_NODE_TYPE(ClassLiteral)
2629 Handle<String> name() const { return raw_name_->string(); }
2630 const AstRawString* raw_name() const { return raw_name_; }
2631 Scope* scope() const { return scope_; }
2632 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2633 Expression* extends() const { return extends_; }
2634 FunctionLiteral* constructor() const { return constructor_; }
2635 ZoneList<Property*>* properties() const { return properties_; }
2636 int start_position() const { return position(); }
2637 int end_position() const { return end_position_; }
2639 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2640 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2641 BailoutId ExitId() { return BailoutId(local_id(2)); }
2643 // Return an AST id for a property that is used in simulate instructions.
2644 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2646 // Unlike other AST nodes, this number of bailout IDs allocated for an
2647 // ClassLiteral can vary, so num_ids() is not a static method.
2648 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2651 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2652 VariableProxy* class_variable_proxy, Expression* extends,
2653 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2654 int start_position, int end_position)
2655 : Expression(zone, start_position),
2658 class_variable_proxy_(class_variable_proxy),
2660 constructor_(constructor),
2661 properties_(properties),
2662 end_position_(end_position) {}
2663 static int parent_num_ids() { return Expression::num_ids(); }
2666 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2668 const AstRawString* raw_name_;
2670 VariableProxy* class_variable_proxy_;
2671 Expression* extends_;
2672 FunctionLiteral* constructor_;
2673 ZoneList<Property*>* properties_;
2678 class NativeFunctionLiteral FINAL : public Expression {
2680 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2682 Handle<String> name() const { return name_->string(); }
2683 v8::Extension* extension() const { return extension_; }
2686 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2687 v8::Extension* extension, int pos)
2688 : Expression(zone, pos), name_(name), extension_(extension) {}
2691 const AstRawString* name_;
2692 v8::Extension* extension_;
2696 class ThisFunction FINAL : public Expression {
2698 DECLARE_NODE_TYPE(ThisFunction)
2701 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2705 class SuperReference FINAL : public Expression {
2707 DECLARE_NODE_TYPE(SuperReference)
2709 VariableProxy* this_var() const { return this_var_; }
2711 static int num_ids() { return parent_num_ids() + 1; }
2712 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2714 // Type feedback information.
2715 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2716 Isolate* isolate) OVERRIDE {
2717 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2719 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2720 homeobject_feedback_slot_ = slot;
2722 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2724 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2725 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2726 return homeobject_feedback_slot_;
2730 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2731 : Expression(zone, pos),
2732 this_var_(this_var),
2733 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2734 DCHECK(this_var->is_this());
2736 static int parent_num_ids() { return Expression::num_ids(); }
2739 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2741 VariableProxy* this_var_;
2742 FeedbackVectorICSlot homeobject_feedback_slot_;
2746 #undef DECLARE_NODE_TYPE
2749 // ----------------------------------------------------------------------------
2750 // Regular expressions
2753 class RegExpVisitor BASE_EMBEDDED {
2755 virtual ~RegExpVisitor() { }
2756 #define MAKE_CASE(Name) \
2757 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2758 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2763 class RegExpTree : public ZoneObject {
2765 static const int kInfinity = kMaxInt;
2766 virtual ~RegExpTree() {}
2767 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2768 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2769 RegExpNode* on_success) = 0;
2770 virtual bool IsTextElement() { return false; }
2771 virtual bool IsAnchoredAtStart() { return false; }
2772 virtual bool IsAnchoredAtEnd() { return false; }
2773 virtual int min_match() = 0;
2774 virtual int max_match() = 0;
2775 // Returns the interval of registers used for captures within this
2777 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2778 virtual void AppendToText(RegExpText* text, Zone* zone);
2779 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2780 #define MAKE_ASTYPE(Name) \
2781 virtual RegExp##Name* As##Name(); \
2782 virtual bool Is##Name();
2783 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2788 class RegExpDisjunction FINAL : public RegExpTree {
2790 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2791 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2792 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2793 RegExpNode* on_success) OVERRIDE;
2794 RegExpDisjunction* AsDisjunction() OVERRIDE;
2795 Interval CaptureRegisters() OVERRIDE;
2796 bool IsDisjunction() OVERRIDE;
2797 bool IsAnchoredAtStart() OVERRIDE;
2798 bool IsAnchoredAtEnd() OVERRIDE;
2799 int min_match() OVERRIDE { return min_match_; }
2800 int max_match() OVERRIDE { return max_match_; }
2801 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2803 ZoneList<RegExpTree*>* alternatives_;
2809 class RegExpAlternative FINAL : public RegExpTree {
2811 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2812 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2813 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2814 RegExpNode* on_success) OVERRIDE;
2815 RegExpAlternative* AsAlternative() OVERRIDE;
2816 Interval CaptureRegisters() OVERRIDE;
2817 bool IsAlternative() OVERRIDE;
2818 bool IsAnchoredAtStart() OVERRIDE;
2819 bool IsAnchoredAtEnd() OVERRIDE;
2820 int min_match() OVERRIDE { return min_match_; }
2821 int max_match() OVERRIDE { return max_match_; }
2822 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2824 ZoneList<RegExpTree*>* nodes_;
2830 class RegExpAssertion FINAL : public RegExpTree {
2832 enum AssertionType {
2840 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2841 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2842 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2843 RegExpNode* on_success) OVERRIDE;
2844 RegExpAssertion* AsAssertion() OVERRIDE;
2845 bool IsAssertion() OVERRIDE;
2846 bool IsAnchoredAtStart() OVERRIDE;
2847 bool IsAnchoredAtEnd() OVERRIDE;
2848 int min_match() OVERRIDE { return 0; }
2849 int max_match() OVERRIDE { return 0; }
2850 AssertionType assertion_type() { return assertion_type_; }
2852 AssertionType assertion_type_;
2856 class CharacterSet FINAL BASE_EMBEDDED {
2858 explicit CharacterSet(uc16 standard_set_type)
2860 standard_set_type_(standard_set_type) {}
2861 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2863 standard_set_type_(0) {}
2864 ZoneList<CharacterRange>* ranges(Zone* zone);
2865 uc16 standard_set_type() { return standard_set_type_; }
2866 void set_standard_set_type(uc16 special_set_type) {
2867 standard_set_type_ = special_set_type;
2869 bool is_standard() { return standard_set_type_ != 0; }
2870 void Canonicalize();
2872 ZoneList<CharacterRange>* ranges_;
2873 // If non-zero, the value represents a standard set (e.g., all whitespace
2874 // characters) without having to expand the ranges.
2875 uc16 standard_set_type_;
2879 class RegExpCharacterClass FINAL : public RegExpTree {
2881 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2883 is_negated_(is_negated) { }
2884 explicit RegExpCharacterClass(uc16 type)
2886 is_negated_(false) { }
2887 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2888 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2889 RegExpNode* on_success) OVERRIDE;
2890 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2891 bool IsCharacterClass() OVERRIDE;
2892 bool IsTextElement() OVERRIDE { return true; }
2893 int min_match() OVERRIDE { return 1; }
2894 int max_match() OVERRIDE { return 1; }
2895 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2896 CharacterSet character_set() { return set_; }
2897 // TODO(lrn): Remove need for complex version if is_standard that
2898 // recognizes a mangled standard set and just do { return set_.is_special(); }
2899 bool is_standard(Zone* zone);
2900 // Returns a value representing the standard character set if is_standard()
2902 // Currently used values are:
2903 // s : unicode whitespace
2904 // S : unicode non-whitespace
2905 // w : ASCII word character (digit, letter, underscore)
2906 // W : non-ASCII word character
2908 // D : non-ASCII digit
2909 // . : non-unicode non-newline
2910 // * : All characters
2911 uc16 standard_type() { return set_.standard_set_type(); }
2912 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2913 bool is_negated() { return is_negated_; }
2921 class RegExpAtom FINAL : public RegExpTree {
2923 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2924 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2925 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2926 RegExpNode* on_success) OVERRIDE;
2927 RegExpAtom* AsAtom() OVERRIDE;
2928 bool IsAtom() OVERRIDE;
2929 bool IsTextElement() OVERRIDE { return true; }
2930 int min_match() OVERRIDE { return data_.length(); }
2931 int max_match() OVERRIDE { return data_.length(); }
2932 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2933 Vector<const uc16> data() { return data_; }
2934 int length() { return data_.length(); }
2936 Vector<const uc16> data_;
2940 class RegExpText FINAL : public RegExpTree {
2942 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2943 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2944 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2945 RegExpNode* on_success) OVERRIDE;
2946 RegExpText* AsText() OVERRIDE;
2947 bool IsText() OVERRIDE;
2948 bool IsTextElement() OVERRIDE { return true; }
2949 int min_match() OVERRIDE { return length_; }
2950 int max_match() OVERRIDE { return length_; }
2951 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2952 void AddElement(TextElement elm, Zone* zone) {
2953 elements_.Add(elm, zone);
2954 length_ += elm.length();
2956 ZoneList<TextElement>* elements() { return &elements_; }
2958 ZoneList<TextElement> elements_;
2963 class RegExpQuantifier FINAL : public RegExpTree {
2965 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
2966 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
2970 min_match_(min * body->min_match()),
2971 quantifier_type_(type) {
2972 if (max > 0 && body->max_match() > kInfinity / max) {
2973 max_match_ = kInfinity;
2975 max_match_ = max * body->max_match();
2978 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2979 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2980 RegExpNode* on_success) OVERRIDE;
2981 static RegExpNode* ToNode(int min,
2985 RegExpCompiler* compiler,
2986 RegExpNode* on_success,
2987 bool not_at_start = false);
2988 RegExpQuantifier* AsQuantifier() OVERRIDE;
2989 Interval CaptureRegisters() OVERRIDE;
2990 bool IsQuantifier() OVERRIDE;
2991 int min_match() OVERRIDE { return min_match_; }
2992 int max_match() OVERRIDE { return max_match_; }
2993 int min() { return min_; }
2994 int max() { return max_; }
2995 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
2996 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
2997 bool is_greedy() { return quantifier_type_ == GREEDY; }
2998 RegExpTree* body() { return body_; }
3006 QuantifierType quantifier_type_;
3010 class RegExpCapture FINAL : public RegExpTree {
3012 explicit RegExpCapture(RegExpTree* body, int index)
3013 : body_(body), index_(index) { }
3014 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3015 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3016 RegExpNode* on_success) OVERRIDE;
3017 static RegExpNode* ToNode(RegExpTree* body,
3019 RegExpCompiler* compiler,
3020 RegExpNode* on_success);
3021 RegExpCapture* AsCapture() OVERRIDE;
3022 bool IsAnchoredAtStart() OVERRIDE;
3023 bool IsAnchoredAtEnd() OVERRIDE;
3024 Interval CaptureRegisters() OVERRIDE;
3025 bool IsCapture() OVERRIDE;
3026 int min_match() OVERRIDE { return body_->min_match(); }
3027 int max_match() OVERRIDE { return body_->max_match(); }
3028 RegExpTree* body() { return body_; }
3029 int index() { return index_; }
3030 static int StartRegister(int index) { return index * 2; }
3031 static int EndRegister(int index) { return index * 2 + 1; }
3039 class RegExpLookahead FINAL : public RegExpTree {
3041 RegExpLookahead(RegExpTree* body,
3046 is_positive_(is_positive),
3047 capture_count_(capture_count),
3048 capture_from_(capture_from) { }
3050 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3051 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3052 RegExpNode* on_success) OVERRIDE;
3053 RegExpLookahead* AsLookahead() OVERRIDE;
3054 Interval CaptureRegisters() OVERRIDE;
3055 bool IsLookahead() OVERRIDE;
3056 bool IsAnchoredAtStart() OVERRIDE;
3057 int min_match() OVERRIDE { return 0; }
3058 int max_match() OVERRIDE { return 0; }
3059 RegExpTree* body() { return body_; }
3060 bool is_positive() { return is_positive_; }
3061 int capture_count() { return capture_count_; }
3062 int capture_from() { return capture_from_; }
3072 class RegExpBackReference FINAL : public RegExpTree {
3074 explicit RegExpBackReference(RegExpCapture* capture)
3075 : capture_(capture) { }
3076 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3077 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3078 RegExpNode* on_success) OVERRIDE;
3079 RegExpBackReference* AsBackReference() OVERRIDE;
3080 bool IsBackReference() OVERRIDE;
3081 int min_match() OVERRIDE { return 0; }
3082 int max_match() OVERRIDE { return capture_->max_match(); }
3083 int index() { return capture_->index(); }
3084 RegExpCapture* capture() { return capture_; }
3086 RegExpCapture* capture_;
3090 class RegExpEmpty FINAL : public RegExpTree {
3093 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3094 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3095 RegExpNode* on_success) OVERRIDE;
3096 RegExpEmpty* AsEmpty() OVERRIDE;
3097 bool IsEmpty() OVERRIDE;
3098 int min_match() OVERRIDE { return 0; }
3099 int max_match() OVERRIDE { return 0; }
3103 // ----------------------------------------------------------------------------
3104 // Out-of-line inline constructors (to side-step cyclic dependencies).
3106 inline ModuleVariable::ModuleVariable(Zone* zone, VariableProxy* proxy, int pos)
3107 : Module(zone, proxy->interface(), pos),
3112 // ----------------------------------------------------------------------------
3114 // - leaf node visitors are abstract.
3116 class AstVisitor BASE_EMBEDDED {
3119 virtual ~AstVisitor() {}
3121 // Stack overflow check and dynamic dispatch.
3122 virtual void Visit(AstNode* node) = 0;
3124 // Iteration left-to-right.
3125 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3126 virtual void VisitStatements(ZoneList<Statement*>* statements);
3127 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3129 // Individual AST nodes.
3130 #define DEF_VISIT(type) \
3131 virtual void Visit##type(type* node) = 0;
3132 AST_NODE_LIST(DEF_VISIT)
3137 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3139 void Visit(AstNode* node) FINAL { \
3140 if (!CheckStackOverflow()) node->Accept(this); \
3143 void SetStackOverflow() { stack_overflow_ = true; } \
3144 void ClearStackOverflow() { stack_overflow_ = false; } \
3145 bool HasStackOverflow() const { return stack_overflow_; } \
3147 bool CheckStackOverflow() { \
3148 if (stack_overflow_) return true; \
3149 StackLimitCheck check(isolate_); \
3150 if (!check.HasOverflowed()) return false; \
3151 stack_overflow_ = true; \
3156 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3157 isolate_ = isolate; \
3159 stack_overflow_ = false; \
3161 Zone* zone() { return zone_; } \
3162 Isolate* isolate() { return isolate_; } \
3164 Isolate* isolate_; \
3166 bool stack_overflow_
3169 // ----------------------------------------------------------------------------
3172 class AstNodeFactory FINAL BASE_EMBEDDED {
3174 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3175 : zone_(ast_value_factory->zone()),
3176 ast_value_factory_(ast_value_factory) {}
3178 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3182 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3185 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3187 FunctionLiteral* fun,
3190 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3193 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3197 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3200 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3204 return new (zone_) ImportDeclaration(zone_, proxy, module, scope, pos);
3207 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3210 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3213 ModuleLiteral* NewModuleLiteral(Block* body, Interface* interface, int pos) {
3214 return new (zone_) ModuleLiteral(zone_, body, interface, pos);
3217 ModuleVariable* NewModuleVariable(VariableProxy* proxy, int pos) {
3218 return new (zone_) ModuleVariable(zone_, proxy, pos);
3221 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3222 return new (zone_) ModulePath(zone_, origin, name, pos);
3225 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3226 return new (zone_) ModuleUrl(zone_, url, pos);
3229 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3231 bool is_initializer_block,
3234 Block(zone_, labels, capacity, is_initializer_block, pos);
3237 #define STATEMENT_WITH_LABELS(NodeType) \
3238 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3239 return new (zone_) NodeType(zone_, labels, pos); \
3241 STATEMENT_WITH_LABELS(DoWhileStatement)
3242 STATEMENT_WITH_LABELS(WhileStatement)
3243 STATEMENT_WITH_LABELS(ForStatement)
3244 STATEMENT_WITH_LABELS(SwitchStatement)
3245 #undef STATEMENT_WITH_LABELS
3247 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3248 ZoneList<const AstRawString*>* labels,
3250 switch (visit_mode) {
3251 case ForEachStatement::ENUMERATE: {
3252 return new (zone_) ForInStatement(zone_, labels, pos);
3254 case ForEachStatement::ITERATE: {
3255 return new (zone_) ForOfStatement(zone_, labels, pos);
3262 ModuleStatement* NewModuleStatement(
3263 VariableProxy* proxy, Block* body, int pos) {
3264 return new (zone_) ModuleStatement(zone_, proxy, body, pos);
3267 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3268 return new (zone_) ExpressionStatement(zone_, expression, pos);
3271 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3272 return new (zone_) ContinueStatement(zone_, target, pos);
3275 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3276 return new (zone_) BreakStatement(zone_, target, pos);
3279 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3280 return new (zone_) ReturnStatement(zone_, expression, pos);
3283 WithStatement* NewWithStatement(Scope* scope,
3284 Expression* expression,
3285 Statement* statement,
3287 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3290 IfStatement* NewIfStatement(Expression* condition,
3291 Statement* then_statement,
3292 Statement* else_statement,
3295 IfStatement(zone_, condition, then_statement, else_statement, pos);
3298 TryCatchStatement* NewTryCatchStatement(int index,
3304 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3305 variable, catch_block, pos);
3308 TryFinallyStatement* NewTryFinallyStatement(int index,
3310 Block* finally_block,
3313 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3316 DebuggerStatement* NewDebuggerStatement(int pos) {
3317 return new (zone_) DebuggerStatement(zone_, pos);
3320 EmptyStatement* NewEmptyStatement(int pos) {
3321 return new(zone_) EmptyStatement(zone_, pos);
3324 CaseClause* NewCaseClause(
3325 Expression* label, ZoneList<Statement*>* statements, int pos) {
3326 return new (zone_) CaseClause(zone_, label, statements, pos);
3329 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3331 Literal(zone_, ast_value_factory_->NewString(string), pos);
3334 // A JavaScript symbol (ECMA-262 edition 6).
3335 Literal* NewSymbolLiteral(const char* name, int pos) {
3336 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3339 Literal* NewNumberLiteral(double number, int pos) {
3341 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3344 Literal* NewSmiLiteral(int number, int pos) {
3345 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3348 Literal* NewBooleanLiteral(bool b, int pos) {
3349 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3352 Literal* NewNullLiteral(int pos) {
3353 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3356 Literal* NewUndefinedLiteral(int pos) {
3357 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3360 Literal* NewTheHoleLiteral(int pos) {
3361 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3364 ObjectLiteral* NewObjectLiteral(
3365 ZoneList<ObjectLiteral::Property*>* properties,
3367 int boilerplate_properties,
3370 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3371 boilerplate_properties, has_function, pos);
3374 ObjectLiteral::Property* NewObjectLiteralProperty(
3375 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3376 bool is_static, bool is_computed_name) {
3378 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3381 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3384 bool is_computed_name) {
3385 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3386 is_static, is_computed_name);
3389 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3390 const AstRawString* flags,
3393 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3396 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3399 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3402 VariableProxy* NewVariableProxy(Variable* var,
3403 int pos = RelocInfo::kNoPosition) {
3404 return new (zone_) VariableProxy(zone_, var, pos);
3407 VariableProxy* NewVariableProxy(const AstRawString* name,
3409 Interface* interface = Interface::NewValue(),
3410 int position = RelocInfo::kNoPosition) {
3411 return new (zone_) VariableProxy(zone_, name, is_this, interface, position);
3414 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3415 return new (zone_) Property(zone_, obj, key, pos);
3418 Call* NewCall(Expression* expression,
3419 ZoneList<Expression*>* arguments,
3421 return new (zone_) Call(zone_, expression, arguments, pos);
3424 CallNew* NewCallNew(Expression* expression,
3425 ZoneList<Expression*>* arguments,
3427 return new (zone_) CallNew(zone_, expression, arguments, pos);
3430 CallRuntime* NewCallRuntime(const AstRawString* name,
3431 const Runtime::Function* function,
3432 ZoneList<Expression*>* arguments,
3434 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3437 UnaryOperation* NewUnaryOperation(Token::Value op,
3438 Expression* expression,
3440 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3443 BinaryOperation* NewBinaryOperation(Token::Value op,
3447 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3450 CountOperation* NewCountOperation(Token::Value op,
3454 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3457 CompareOperation* NewCompareOperation(Token::Value op,
3461 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3464 Conditional* NewConditional(Expression* condition,
3465 Expression* then_expression,
3466 Expression* else_expression,
3468 return new (zone_) Conditional(zone_, condition, then_expression,
3469 else_expression, position);
3472 Assignment* NewAssignment(Token::Value op,
3476 DCHECK(Token::IsAssignmentOp(op));
3477 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3478 if (assign->is_compound()) {
3479 DCHECK(Token::IsAssignmentOp(op));
3480 assign->binary_operation_ =
3481 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3486 Yield* NewYield(Expression *generator_object,
3487 Expression* expression,
3488 Yield::Kind yield_kind,
3490 if (!expression) expression = NewUndefinedLiteral(pos);
3492 Yield(zone_, generator_object, expression, yield_kind, pos);
3495 Throw* NewThrow(Expression* exception, int pos) {
3496 return new (zone_) Throw(zone_, exception, pos);
3499 FunctionLiteral* NewFunctionLiteral(
3500 const AstRawString* name, AstValueFactory* ast_value_factory,
3501 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3502 int expected_property_count, int handler_count, int parameter_count,
3503 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3504 FunctionLiteral::FunctionType function_type,
3505 FunctionLiteral::IsFunctionFlag is_function,
3506 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3508 return new (zone_) FunctionLiteral(
3509 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3510 expected_property_count, handler_count, parameter_count, function_type,
3511 has_duplicate_parameters, is_function, is_parenthesized, kind,
3515 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3516 VariableProxy* proxy, Expression* extends,
3517 FunctionLiteral* constructor,
3518 ZoneList<ObjectLiteral::Property*>* properties,
3519 int start_position, int end_position) {
3521 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3522 properties, start_position, end_position);
3525 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3526 v8::Extension* extension,
3528 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3531 ThisFunction* NewThisFunction(int pos) {
3532 return new (zone_) ThisFunction(zone_, pos);
3535 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3536 return new (zone_) SuperReference(zone_, this_var, pos);
3541 AstValueFactory* ast_value_factory_;
3545 } } // namespace v8::internal