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) {
204 return zone->New(static_cast<int>(size));
207 explicit AstNode(int position): position_(position) {}
208 virtual ~AstNode() {}
210 virtual void Accept(AstVisitor* v) = 0;
211 virtual NodeType node_type() const = 0;
212 int position() const { return position_; }
214 // Type testing & conversion functions overridden by concrete subclasses.
215 #define DECLARE_NODE_FUNCTIONS(type) \
216 bool Is##type() const { return node_type() == AstNode::k##type; } \
218 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
220 const type* As##type() const { \
221 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
223 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
224 #undef DECLARE_NODE_FUNCTIONS
226 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
227 virtual IterationStatement* AsIterationStatement() { return NULL; }
228 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
230 // The interface for feedback slots, with default no-op implementations for
231 // node types which don't actually have this. Note that this is conceptually
232 // not really nice, but multiple inheritance would introduce yet another
233 // vtable entry per node, something we don't want for space reasons.
234 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
236 return FeedbackVectorRequirements(0, 0);
238 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
239 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) {
242 // Each ICSlot stores a kind of IC which the participating node should know.
243 virtual Code::Kind FeedbackICSlotKind(int index) {
245 return Code::NUMBER_OF_KINDS;
249 // Hidden to prevent accidental usage. It would have to load the
250 // current zone from the TLS.
251 void* operator new(size_t size);
253 friend class CaseClause; // Generates AST IDs.
259 class Statement : public AstNode {
261 explicit Statement(Zone* zone, int position) : AstNode(position) {}
263 bool IsEmpty() { return AsEmptyStatement() != NULL; }
264 virtual bool IsJump() const { return false; }
268 class SmallMapList FINAL {
271 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
273 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
274 void Clear() { list_.Clear(); }
275 void Sort() { list_.Sort(); }
277 bool is_empty() const { return list_.is_empty(); }
278 int length() const { return list_.length(); }
280 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
281 if (!Map::TryUpdate(map).ToHandle(&map)) return;
282 for (int i = 0; i < length(); ++i) {
283 if (at(i).is_identical_to(map)) return;
288 void FilterForPossibleTransitions(Map* root_map) {
289 for (int i = list_.length() - 1; i >= 0; i--) {
290 if (at(i)->FindRootMap() != root_map) {
291 list_.RemoveElement(list_.at(i));
296 void Add(Handle<Map> handle, Zone* zone) {
297 list_.Add(handle.location(), zone);
300 Handle<Map> at(int i) const {
301 return Handle<Map>(list_.at(i));
304 Handle<Map> first() const { return at(0); }
305 Handle<Map> last() const { return at(length() - 1); }
308 // The list stores pointers to Map*, that is Map**, so it's GC safe.
309 SmallPointerList<Map*> list_;
311 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
315 class Expression : public AstNode {
318 // Not assigned a context yet, or else will not be visited during
321 // Evaluated for its side effects.
323 // Evaluated for its value (and side effects).
325 // Evaluated for control flow (and side effects).
329 virtual bool IsValidReferenceExpression() const { return false; }
331 // Helpers for ToBoolean conversion.
332 virtual bool ToBooleanIsTrue() const { return false; }
333 virtual bool ToBooleanIsFalse() const { return false; }
335 // Symbols that cannot be parsed as array indices are considered property
336 // names. We do not treat symbols that can be array indexes as property
337 // names because [] for string objects is handled only by keyed ICs.
338 virtual bool IsPropertyName() const { return false; }
340 // True iff the expression is a literal represented as a smi.
341 bool IsSmiLiteral() const;
343 // True iff the expression is a string literal.
344 bool IsStringLiteral() const;
346 // True iff the expression is the null literal.
347 bool IsNullLiteral() const;
349 // True if we can prove that the expression is the undefined literal.
350 bool IsUndefinedLiteral(Isolate* isolate) const;
352 // Expression type bounds
353 Bounds bounds() const { return bounds_; }
354 void set_bounds(Bounds bounds) { bounds_ = bounds; }
356 // Whether the expression is parenthesized
357 bool is_parenthesized() const {
358 return IsParenthesizedField::decode(bit_field_);
360 bool is_multi_parenthesized() const {
361 return IsMultiParenthesizedField::decode(bit_field_);
363 void increase_parenthesization_level() {
365 IsMultiParenthesizedField::update(bit_field_, is_parenthesized());
366 bit_field_ = IsParenthesizedField::update(bit_field_, true);
369 // Type feedback information for assignments and properties.
370 virtual bool IsMonomorphic() {
374 virtual SmallMapList* GetReceiverTypes() {
378 virtual KeyedAccessStoreMode GetStoreMode() const {
380 return STANDARD_STORE;
382 virtual IcCheckType GetKeyType() const {
387 // TODO(rossberg): this should move to its own AST node eventually.
388 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
389 byte to_boolean_types() const {
390 return ToBooleanTypesField::decode(bit_field_);
393 void set_base_id(int id) { base_id_ = id; }
394 static int num_ids() { return parent_num_ids() + 2; }
395 BailoutId id() const { return BailoutId(local_id(0)); }
396 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
399 Expression(Zone* zone, int pos)
401 base_id_(BailoutId::None().ToInt()),
402 bounds_(Bounds::Unbounded(zone)),
404 static int parent_num_ids() { return 0; }
405 void set_to_boolean_types(byte types) {
406 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
409 int base_id() const {
410 DCHECK(!BailoutId(base_id_).IsNone());
415 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
419 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
420 class IsParenthesizedField : public BitField16<bool, 8, 1> {};
421 class IsMultiParenthesizedField : public BitField16<bool, 9, 1> {};
423 // Ends with 16-bit field; deriving classes in turn begin with
424 // 16-bit fields for optimum packing efficiency.
428 class BreakableStatement : public Statement {
431 TARGET_FOR_ANONYMOUS,
432 TARGET_FOR_NAMED_ONLY
435 // The labels associated with this statement. May be NULL;
436 // if it is != NULL, guaranteed to contain at least one entry.
437 ZoneList<const AstRawString*>* labels() const { return labels_; }
439 // Type testing & conversion.
440 BreakableStatement* AsBreakableStatement() FINAL { return this; }
443 Label* break_target() { return &break_target_; }
446 bool is_target_for_anonymous() const {
447 return breakable_type_ == TARGET_FOR_ANONYMOUS;
450 void set_base_id(int id) { base_id_ = id; }
451 static int num_ids() { return parent_num_ids() + 2; }
452 BailoutId EntryId() const { return BailoutId(local_id(0)); }
453 BailoutId ExitId() const { return BailoutId(local_id(1)); }
456 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
457 BreakableType breakable_type, int position)
458 : Statement(zone, position),
460 breakable_type_(breakable_type),
461 base_id_(BailoutId::None().ToInt()) {
462 DCHECK(labels == NULL || labels->length() > 0);
464 static int parent_num_ids() { return 0; }
466 int base_id() const {
467 DCHECK(!BailoutId(base_id_).IsNone());
472 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
474 ZoneList<const AstRawString*>* labels_;
475 BreakableType breakable_type_;
481 class Block FINAL : public BreakableStatement {
483 DECLARE_NODE_TYPE(Block)
485 void AddStatement(Statement* statement, Zone* zone) {
486 statements_.Add(statement, zone);
489 ZoneList<Statement*>* statements() { return &statements_; }
490 bool is_initializer_block() const { return is_initializer_block_; }
492 static int num_ids() { return parent_num_ids() + 1; }
493 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
495 bool IsJump() const OVERRIDE {
496 return !statements_.is_empty() && statements_.last()->IsJump()
497 && labels() == NULL; // Good enough as an approximation...
500 Scope* scope() const { return scope_; }
501 void set_scope(Scope* scope) { scope_ = scope; }
504 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
505 bool is_initializer_block, int pos)
506 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
507 statements_(capacity, zone),
508 is_initializer_block_(is_initializer_block),
510 static int parent_num_ids() { return BreakableStatement::num_ids(); }
513 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
515 ZoneList<Statement*> statements_;
516 bool is_initializer_block_;
521 class Declaration : public AstNode {
523 VariableProxy* proxy() const { return proxy_; }
524 VariableMode mode() const { return mode_; }
525 Scope* scope() const { return scope_; }
526 virtual InitializationFlag initialization() const = 0;
527 virtual bool IsInlineable() const;
530 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
532 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
533 DCHECK(IsDeclaredVariableMode(mode));
538 VariableProxy* proxy_;
540 // Nested scope from which the declaration originated.
545 class VariableDeclaration FINAL : public Declaration {
547 DECLARE_NODE_TYPE(VariableDeclaration)
549 InitializationFlag initialization() const OVERRIDE {
550 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
554 VariableDeclaration(Zone* zone,
555 VariableProxy* proxy,
559 : Declaration(zone, proxy, mode, scope, pos) {
564 class FunctionDeclaration FINAL : public Declaration {
566 DECLARE_NODE_TYPE(FunctionDeclaration)
568 FunctionLiteral* fun() const { return fun_; }
569 InitializationFlag initialization() const OVERRIDE {
570 return kCreatedInitialized;
572 bool IsInlineable() const OVERRIDE;
575 FunctionDeclaration(Zone* zone,
576 VariableProxy* proxy,
578 FunctionLiteral* fun,
581 : Declaration(zone, proxy, mode, scope, pos),
583 // At the moment there are no "const functions" in JavaScript...
584 DCHECK(mode == VAR || mode == LET);
589 FunctionLiteral* fun_;
593 class ModuleDeclaration FINAL : public Declaration {
595 DECLARE_NODE_TYPE(ModuleDeclaration)
597 Module* module() const { return module_; }
598 InitializationFlag initialization() const OVERRIDE {
599 return kCreatedInitialized;
603 ModuleDeclaration(Zone* zone,
604 VariableProxy* proxy,
608 : Declaration(zone, proxy, MODULE, scope, pos),
617 class ImportDeclaration FINAL : public Declaration {
619 DECLARE_NODE_TYPE(ImportDeclaration)
621 Module* module() const { return module_; }
622 InitializationFlag initialization() const OVERRIDE {
623 return kCreatedInitialized;
627 ImportDeclaration(Zone* zone,
628 VariableProxy* proxy,
632 : Declaration(zone, proxy, LET, scope, pos),
641 class ExportDeclaration FINAL : public Declaration {
643 DECLARE_NODE_TYPE(ExportDeclaration)
645 InitializationFlag initialization() const OVERRIDE {
646 return kCreatedInitialized;
650 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
651 : Declaration(zone, proxy, LET, scope, pos) {}
655 class Module : public AstNode {
657 Interface* interface() const { return interface_; }
658 Block* body() const { return body_; }
661 Module(Zone* zone, int pos)
663 interface_(Interface::NewModule(zone)),
665 Module(Zone* zone, Interface* interface, int pos, Block* body = NULL)
667 interface_(interface),
671 Interface* interface_;
676 class ModuleLiteral FINAL : public Module {
678 DECLARE_NODE_TYPE(ModuleLiteral)
681 ModuleLiteral(Zone* zone, Block* body, Interface* interface, int pos)
682 : Module(zone, interface, pos, body) {}
686 class ModuleVariable FINAL : public Module {
688 DECLARE_NODE_TYPE(ModuleVariable)
690 VariableProxy* proxy() const { return proxy_; }
693 inline ModuleVariable(Zone* zone, VariableProxy* proxy, int pos);
696 VariableProxy* proxy_;
700 class ModulePath FINAL : public Module {
702 DECLARE_NODE_TYPE(ModulePath)
704 Module* module() const { return module_; }
705 Handle<String> name() const { return name_->string(); }
708 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
709 : Module(zone, pos), module_(module), name_(name) {}
713 const AstRawString* name_;
717 class ModuleUrl FINAL : public Module {
719 DECLARE_NODE_TYPE(ModuleUrl)
721 Handle<String> url() const { return url_; }
724 ModuleUrl(Zone* zone, Handle<String> url, int pos)
725 : Module(zone, pos), url_(url) {
733 class ModuleStatement FINAL : public Statement {
735 DECLARE_NODE_TYPE(ModuleStatement)
737 VariableProxy* proxy() const { return proxy_; }
738 Block* body() const { return body_; }
741 ModuleStatement(Zone* zone, VariableProxy* proxy, Block* body, int pos)
742 : Statement(zone, pos),
748 VariableProxy* proxy_;
753 class IterationStatement : public BreakableStatement {
755 // Type testing & conversion.
756 IterationStatement* AsIterationStatement() FINAL { return this; }
758 Statement* body() const { return body_; }
760 static int num_ids() { return parent_num_ids() + 1; }
761 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
762 virtual BailoutId ContinueId() const = 0;
763 virtual BailoutId StackCheckId() const = 0;
766 Label* continue_target() { return &continue_target_; }
769 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
770 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
772 static int parent_num_ids() { return BreakableStatement::num_ids(); }
773 void Initialize(Statement* body) { body_ = body; }
776 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
779 Label continue_target_;
783 class DoWhileStatement FINAL : public IterationStatement {
785 DECLARE_NODE_TYPE(DoWhileStatement)
787 void Initialize(Expression* cond, Statement* body) {
788 IterationStatement::Initialize(body);
792 Expression* cond() const { return cond_; }
794 static int num_ids() { return parent_num_ids() + 2; }
795 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
796 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
797 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
800 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
801 : IterationStatement(zone, labels, pos), cond_(NULL) {}
802 static int parent_num_ids() { return IterationStatement::num_ids(); }
805 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
811 class WhileStatement FINAL : public IterationStatement {
813 DECLARE_NODE_TYPE(WhileStatement)
815 void Initialize(Expression* cond, Statement* body) {
816 IterationStatement::Initialize(body);
820 Expression* cond() const { return cond_; }
822 static int num_ids() { return parent_num_ids() + 1; }
823 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
824 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
825 BailoutId BodyId() const { return BailoutId(local_id(0)); }
828 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
829 : IterationStatement(zone, labels, pos), cond_(NULL) {}
830 static int parent_num_ids() { return IterationStatement::num_ids(); }
833 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
839 class ForStatement FINAL : public IterationStatement {
841 DECLARE_NODE_TYPE(ForStatement)
843 void Initialize(Statement* init,
847 IterationStatement::Initialize(body);
853 Statement* init() const { return init_; }
854 Expression* cond() const { return cond_; }
855 Statement* next() const { return next_; }
857 static int num_ids() { return parent_num_ids() + 2; }
858 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
859 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
860 BailoutId BodyId() const { return BailoutId(local_id(1)); }
863 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
864 : IterationStatement(zone, labels, pos),
868 static int parent_num_ids() { return IterationStatement::num_ids(); }
871 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
879 class ForEachStatement : public IterationStatement {
882 ENUMERATE, // for (each in subject) body;
883 ITERATE // for (each of subject) body;
886 void Initialize(Expression* each, Expression* subject, Statement* body) {
887 IterationStatement::Initialize(body);
892 Expression* each() const { return each_; }
893 Expression* subject() const { return subject_; }
896 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
897 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
901 Expression* subject_;
905 class ForInStatement FINAL : public ForEachStatement {
907 DECLARE_NODE_TYPE(ForInStatement)
909 Expression* enumerable() const {
913 // Type feedback information.
914 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
915 Isolate* isolate) OVERRIDE {
916 return FeedbackVectorRequirements(1, 0);
918 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
919 for_in_feedback_slot_ = slot;
922 FeedbackVectorSlot ForInFeedbackSlot() {
923 DCHECK(!for_in_feedback_slot_.IsInvalid());
924 return for_in_feedback_slot_;
927 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
928 ForInType for_in_type() const { return for_in_type_; }
929 void set_for_in_type(ForInType type) { for_in_type_ = type; }
931 static int num_ids() { return parent_num_ids() + 5; }
932 BailoutId BodyId() const { return BailoutId(local_id(0)); }
933 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
934 BailoutId EnumId() const { return BailoutId(local_id(2)); }
935 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
936 BailoutId AssignmentId() const { return BailoutId(local_id(4)); }
937 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
938 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
941 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
942 : ForEachStatement(zone, labels, pos),
943 for_in_type_(SLOW_FOR_IN),
944 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
945 static int parent_num_ids() { return ForEachStatement::num_ids(); }
948 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
950 ForInType for_in_type_;
951 FeedbackVectorSlot for_in_feedback_slot_;
955 class ForOfStatement FINAL : public ForEachStatement {
957 DECLARE_NODE_TYPE(ForOfStatement)
959 void Initialize(Expression* each,
962 Expression* assign_iterator,
963 Expression* next_result,
964 Expression* result_done,
965 Expression* assign_each) {
966 ForEachStatement::Initialize(each, subject, body);
967 assign_iterator_ = assign_iterator;
968 next_result_ = next_result;
969 result_done_ = result_done;
970 assign_each_ = assign_each;
973 Expression* iterable() const {
977 // var iterator = subject[Symbol.iterator]();
978 Expression* assign_iterator() const {
979 return assign_iterator_;
982 // var result = iterator.next();
983 Expression* next_result() const {
988 Expression* result_done() const {
992 // each = result.value
993 Expression* assign_each() const {
997 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
998 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
1000 static int num_ids() { return parent_num_ids() + 1; }
1001 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
1004 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1005 : ForEachStatement(zone, labels, pos),
1006 assign_iterator_(NULL),
1009 assign_each_(NULL) {}
1010 static int parent_num_ids() { return ForEachStatement::num_ids(); }
1013 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1015 Expression* assign_iterator_;
1016 Expression* next_result_;
1017 Expression* result_done_;
1018 Expression* assign_each_;
1022 class ExpressionStatement FINAL : public Statement {
1024 DECLARE_NODE_TYPE(ExpressionStatement)
1026 void set_expression(Expression* e) { expression_ = e; }
1027 Expression* expression() const { return expression_; }
1028 bool IsJump() const OVERRIDE { return expression_->IsThrow(); }
1031 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1032 : Statement(zone, pos), expression_(expression) { }
1035 Expression* expression_;
1039 class JumpStatement : public Statement {
1041 bool IsJump() const FINAL { return true; }
1044 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1048 class ContinueStatement FINAL : public JumpStatement {
1050 DECLARE_NODE_TYPE(ContinueStatement)
1052 IterationStatement* target() const { return target_; }
1055 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1056 : JumpStatement(zone, pos), target_(target) { }
1059 IterationStatement* target_;
1063 class BreakStatement FINAL : public JumpStatement {
1065 DECLARE_NODE_TYPE(BreakStatement)
1067 BreakableStatement* target() const { return target_; }
1070 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1071 : JumpStatement(zone, pos), target_(target) { }
1074 BreakableStatement* target_;
1078 class ReturnStatement FINAL : public JumpStatement {
1080 DECLARE_NODE_TYPE(ReturnStatement)
1082 Expression* expression() const { return expression_; }
1085 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1086 : JumpStatement(zone, pos), expression_(expression) { }
1089 Expression* expression_;
1093 class WithStatement FINAL : public Statement {
1095 DECLARE_NODE_TYPE(WithStatement)
1097 Scope* scope() { return scope_; }
1098 Expression* expression() const { return expression_; }
1099 Statement* statement() const { return statement_; }
1101 void set_base_id(int id) { base_id_ = id; }
1102 static int num_ids() { return parent_num_ids() + 1; }
1103 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1106 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1107 Statement* statement, int pos)
1108 : Statement(zone, pos),
1110 expression_(expression),
1111 statement_(statement),
1112 base_id_(BailoutId::None().ToInt()) {}
1113 static int parent_num_ids() { return 0; }
1115 int base_id() const {
1116 DCHECK(!BailoutId(base_id_).IsNone());
1121 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1124 Expression* expression_;
1125 Statement* statement_;
1130 class CaseClause FINAL : public Expression {
1132 DECLARE_NODE_TYPE(CaseClause)
1134 bool is_default() const { return label_ == NULL; }
1135 Expression* label() const {
1136 CHECK(!is_default());
1139 Label* body_target() { return &body_target_; }
1140 ZoneList<Statement*>* statements() const { return statements_; }
1142 static int num_ids() { return parent_num_ids() + 2; }
1143 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1144 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1146 Type* compare_type() { return compare_type_; }
1147 void set_compare_type(Type* type) { compare_type_ = type; }
1150 static int parent_num_ids() { return Expression::num_ids(); }
1153 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1155 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1159 ZoneList<Statement*>* statements_;
1160 Type* compare_type_;
1164 class SwitchStatement FINAL : public BreakableStatement {
1166 DECLARE_NODE_TYPE(SwitchStatement)
1168 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1173 Expression* tag() const { return tag_; }
1174 ZoneList<CaseClause*>* cases() const { return cases_; }
1177 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1178 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1184 ZoneList<CaseClause*>* cases_;
1188 // If-statements always have non-null references to their then- and
1189 // else-parts. When parsing if-statements with no explicit else-part,
1190 // the parser implicitly creates an empty statement. Use the
1191 // HasThenStatement() and HasElseStatement() functions to check if a
1192 // given if-statement has a then- or an else-part containing code.
1193 class IfStatement FINAL : public Statement {
1195 DECLARE_NODE_TYPE(IfStatement)
1197 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1198 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1200 Expression* condition() const { return condition_; }
1201 Statement* then_statement() const { return then_statement_; }
1202 Statement* else_statement() const { return else_statement_; }
1204 bool IsJump() const OVERRIDE {
1205 return HasThenStatement() && then_statement()->IsJump()
1206 && HasElseStatement() && else_statement()->IsJump();
1209 void set_base_id(int id) { base_id_ = id; }
1210 static int num_ids() { return parent_num_ids() + 3; }
1211 BailoutId IfId() const { return BailoutId(local_id(0)); }
1212 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1213 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1216 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1217 Statement* else_statement, int pos)
1218 : Statement(zone, pos),
1219 condition_(condition),
1220 then_statement_(then_statement),
1221 else_statement_(else_statement),
1222 base_id_(BailoutId::None().ToInt()) {}
1223 static int parent_num_ids() { return 0; }
1225 int base_id() const {
1226 DCHECK(!BailoutId(base_id_).IsNone());
1231 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1233 Expression* condition_;
1234 Statement* then_statement_;
1235 Statement* else_statement_;
1240 class TryStatement : public Statement {
1242 int index() const { return index_; }
1243 Block* try_block() const { return try_block_; }
1246 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1247 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1250 // Unique (per-function) index of this handler. This is not an AST ID.
1257 class TryCatchStatement FINAL : public TryStatement {
1259 DECLARE_NODE_TYPE(TryCatchStatement)
1261 Scope* scope() { return scope_; }
1262 Variable* variable() { return variable_; }
1263 Block* catch_block() const { return catch_block_; }
1266 TryCatchStatement(Zone* zone,
1273 : TryStatement(zone, index, try_block, pos),
1275 variable_(variable),
1276 catch_block_(catch_block) {
1281 Variable* variable_;
1282 Block* catch_block_;
1286 class TryFinallyStatement FINAL : public TryStatement {
1288 DECLARE_NODE_TYPE(TryFinallyStatement)
1290 Block* finally_block() const { return finally_block_; }
1293 TryFinallyStatement(
1294 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1295 : TryStatement(zone, index, try_block, pos),
1296 finally_block_(finally_block) { }
1299 Block* finally_block_;
1303 class DebuggerStatement FINAL : public Statement {
1305 DECLARE_NODE_TYPE(DebuggerStatement)
1307 void set_base_id(int id) { base_id_ = id; }
1308 static int num_ids() { return parent_num_ids() + 1; }
1309 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1312 explicit DebuggerStatement(Zone* zone, int pos)
1313 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1314 static int parent_num_ids() { return 0; }
1316 int base_id() const {
1317 DCHECK(!BailoutId(base_id_).IsNone());
1322 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1328 class EmptyStatement FINAL : public Statement {
1330 DECLARE_NODE_TYPE(EmptyStatement)
1333 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1337 class Literal FINAL : public Expression {
1339 DECLARE_NODE_TYPE(Literal)
1341 bool IsPropertyName() const OVERRIDE { return value_->IsPropertyName(); }
1343 Handle<String> AsPropertyName() {
1344 DCHECK(IsPropertyName());
1345 return Handle<String>::cast(value());
1348 const AstRawString* AsRawPropertyName() {
1349 DCHECK(IsPropertyName());
1350 return value_->AsString();
1353 bool ToBooleanIsTrue() const OVERRIDE { return value()->BooleanValue(); }
1354 bool ToBooleanIsFalse() const OVERRIDE { return !value()->BooleanValue(); }
1356 Handle<Object> value() const { return value_->value(); }
1357 const AstValue* raw_value() const { return value_; }
1359 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1360 // only for string and number literals!
1362 static bool Match(void* literal1, void* literal2);
1364 static int num_ids() { return parent_num_ids() + 1; }
1365 TypeFeedbackId LiteralFeedbackId() const {
1366 return TypeFeedbackId(local_id(0));
1370 Literal(Zone* zone, const AstValue* value, int position)
1371 : Expression(zone, position), value_(value) {}
1372 static int parent_num_ids() { return Expression::num_ids(); }
1375 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1377 const AstValue* value_;
1381 // Base class for literals that needs space in the corresponding JSFunction.
1382 class MaterializedLiteral : public Expression {
1384 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1386 int literal_index() { return literal_index_; }
1389 // only callable after initialization.
1390 DCHECK(depth_ >= 1);
1395 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1396 : Expression(zone, pos),
1397 literal_index_(literal_index),
1401 // A materialized literal is simple if the values consist of only
1402 // constants and simple object and array literals.
1403 bool is_simple() const { return is_simple_; }
1404 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1405 friend class CompileTimeValue;
1407 void set_depth(int depth) {
1412 // Populate the constant properties/elements fixed array.
1413 void BuildConstants(Isolate* isolate);
1414 friend class ArrayLiteral;
1415 friend class ObjectLiteral;
1417 // If the expression is a literal, return the literal value;
1418 // if the expression is a materialized literal and is simple return a
1419 // compile time value as encoded by CompileTimeValue::GetValue().
1420 // Otherwise, return undefined literal as the placeholder
1421 // in the object literal boilerplate.
1422 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1431 // Property is used for passing information
1432 // about an object literal's properties from the parser
1433 // to the code generator.
1434 class ObjectLiteralProperty FINAL : public ZoneObject {
1437 CONSTANT, // Property with constant value (compile time).
1438 COMPUTED, // Property with computed value (execution time).
1439 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1440 GETTER, SETTER, // Property is an accessor function.
1441 PROTOTYPE // Property is __proto__.
1444 Expression* key() { return key_; }
1445 Expression* value() { return value_; }
1446 Kind kind() { return kind_; }
1448 // Type feedback information.
1449 void RecordTypeFeedback(TypeFeedbackOracle* oracle);
1450 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1451 Handle<Map> GetReceiverType() { return receiver_type_; }
1453 bool IsCompileTimeValue();
1455 void set_emit_store(bool emit_store);
1458 bool is_static() const { return is_static_; }
1459 bool is_computed_name() const { return is_computed_name_; }
1462 friend class AstNodeFactory;
1464 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1465 bool is_static, bool is_computed_name);
1466 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1467 Expression* value, bool is_static,
1468 bool is_computed_name);
1476 bool is_computed_name_;
1477 Handle<Map> receiver_type_;
1481 // An object literal has a boilerplate object that is used
1482 // for minimizing the work when constructing it at runtime.
1483 class ObjectLiteral FINAL : public MaterializedLiteral {
1485 typedef ObjectLiteralProperty Property;
1487 DECLARE_NODE_TYPE(ObjectLiteral)
1489 Handle<FixedArray> constant_properties() const {
1490 return constant_properties_;
1492 ZoneList<Property*>* properties() const { return properties_; }
1493 bool fast_elements() const { return fast_elements_; }
1494 bool may_store_doubles() const { return may_store_doubles_; }
1495 bool has_function() const { return has_function_; }
1497 // Decide if a property should be in the object boilerplate.
1498 static bool IsBoilerplateProperty(Property* property);
1500 // Populate the constant properties fixed array.
1501 void BuildConstantProperties(Isolate* isolate);
1503 // Mark all computed expressions that are bound to a key that
1504 // is shadowed by a later occurrence of the same key. For the
1505 // marked expressions, no store code is emitted.
1506 void CalculateEmitStore(Zone* zone);
1508 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1509 int ComputeFlags() const {
1510 int flags = fast_elements() ? kFastElements : kNoFlags;
1511 flags |= has_function() ? kHasFunction : kNoFlags;
1518 kHasFunction = 1 << 1
1521 struct Accessors: public ZoneObject {
1522 Accessors() : getter(NULL), setter(NULL) {}
1527 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1529 // Return an AST id for a property that is used in simulate instructions.
1530 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1532 // Unlike other AST nodes, this number of bailout IDs allocated for an
1533 // ObjectLiteral can vary, so num_ids() is not a static method.
1534 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1537 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1538 int boilerplate_properties, bool has_function, int pos)
1539 : MaterializedLiteral(zone, literal_index, pos),
1540 properties_(properties),
1541 boilerplate_properties_(boilerplate_properties),
1542 fast_elements_(false),
1543 may_store_doubles_(false),
1544 has_function_(has_function) {}
1545 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1548 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1549 Handle<FixedArray> constant_properties_;
1550 ZoneList<Property*>* properties_;
1551 int boilerplate_properties_;
1552 bool fast_elements_;
1553 bool may_store_doubles_;
1558 // Node for capturing a regexp literal.
1559 class RegExpLiteral FINAL : public MaterializedLiteral {
1561 DECLARE_NODE_TYPE(RegExpLiteral)
1563 Handle<String> pattern() const { return pattern_->string(); }
1564 Handle<String> flags() const { return flags_->string(); }
1567 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1568 const AstRawString* flags, int literal_index, int pos)
1569 : MaterializedLiteral(zone, literal_index, pos),
1576 const AstRawString* pattern_;
1577 const AstRawString* flags_;
1581 // An array literal has a literals object that is used
1582 // for minimizing the work when constructing it at runtime.
1583 class ArrayLiteral FINAL : public MaterializedLiteral {
1585 DECLARE_NODE_TYPE(ArrayLiteral)
1587 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1588 ZoneList<Expression*>* values() const { return values_; }
1590 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1592 // Return an AST id for an element that is used in simulate instructions.
1593 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1595 // Unlike other AST nodes, this number of bailout IDs allocated for an
1596 // ArrayLiteral can vary, so num_ids() is not a static method.
1597 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1599 // Populate the constant elements fixed array.
1600 void BuildConstantElements(Isolate* isolate);
1602 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1603 int ComputeFlags() const {
1604 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1605 flags |= ArrayLiteral::kDisableMementos;
1611 kShallowElements = 1,
1612 kDisableMementos = 1 << 1
1616 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1618 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1619 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1622 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1624 Handle<FixedArray> constant_elements_;
1625 ZoneList<Expression*>* values_;
1629 class VariableProxy FINAL : public Expression {
1631 DECLARE_NODE_TYPE(VariableProxy)
1633 bool IsValidReferenceExpression() const OVERRIDE {
1634 return !is_resolved() || var()->IsValidReference();
1637 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1639 Handle<String> name() const { return raw_name()->string(); }
1640 const AstRawString* raw_name() const {
1641 return is_resolved() ? var_->raw_name() : raw_name_;
1644 Variable* var() const {
1645 DCHECK(is_resolved());
1648 void set_var(Variable* v) {
1649 DCHECK(!is_resolved());
1654 bool is_this() const { return IsThisField::decode(bit_field_); }
1656 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1657 void set_is_assigned() {
1658 bit_field_ = IsAssignedField::update(bit_field_, true);
1661 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1662 void set_is_resolved() {
1663 bit_field_ = IsResolvedField::update(bit_field_, true);
1666 Interface* interface() const { return interface_; }
1668 // Bind this proxy to the variable var. Interfaces must match.
1669 void BindTo(Variable* var);
1671 bool UsesVariableFeedbackSlot() const {
1672 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1675 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1676 Isolate* isolate) OVERRIDE {
1677 return FeedbackVectorRequirements(0, UsesVariableFeedbackSlot() ? 1 : 0);
1680 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1681 variable_feedback_slot_ = slot;
1683 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1684 FeedbackVectorICSlot VariableFeedbackSlot() {
1685 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1686 return variable_feedback_slot_;
1690 VariableProxy(Zone* zone, Variable* var, int position);
1692 VariableProxy(Zone* zone, const AstRawString* name, bool is_this,
1693 Interface* interface, int position);
1695 class IsThisField : public BitField8<bool, 0, 1> {};
1696 class IsAssignedField : public BitField8<bool, 1, 1> {};
1697 class IsResolvedField : public BitField8<bool, 2, 1> {};
1699 // Start with 16-bit (or smaller) field, which should get packed together
1700 // with Expression's trailing 16-bit field.
1702 FeedbackVectorICSlot variable_feedback_slot_;
1704 const AstRawString* raw_name_; // if !is_resolved_
1705 Variable* var_; // if is_resolved_
1707 Interface* interface_;
1711 class Property FINAL : public Expression {
1713 DECLARE_NODE_TYPE(Property)
1715 bool IsValidReferenceExpression() const OVERRIDE { return true; }
1717 Expression* obj() const { return obj_; }
1718 Expression* key() const { return key_; }
1720 static int num_ids() { return parent_num_ids() + 2; }
1721 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1722 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1724 bool IsStringAccess() const {
1725 return IsStringAccessField::decode(bit_field_);
1728 // Type feedback information.
1729 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
1730 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
1731 KeyedAccessStoreMode GetStoreMode() const OVERRIDE { return STANDARD_STORE; }
1732 IcCheckType GetKeyType() const OVERRIDE {
1733 return KeyTypeField::decode(bit_field_);
1735 bool IsUninitialized() const {
1736 return !is_for_call() && HasNoTypeInformation();
1738 bool HasNoTypeInformation() const {
1739 return IsUninitializedField::decode(bit_field_);
1741 void set_is_uninitialized(bool b) {
1742 bit_field_ = IsUninitializedField::update(bit_field_, b);
1744 void set_is_string_access(bool b) {
1745 bit_field_ = IsStringAccessField::update(bit_field_, b);
1747 void set_key_type(IcCheckType key_type) {
1748 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1750 void mark_for_call() {
1751 bit_field_ = IsForCallField::update(bit_field_, true);
1753 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1755 bool IsSuperAccess() {
1756 return obj()->IsSuperReference();
1759 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1760 Isolate* isolate) OVERRIDE {
1761 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1763 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1764 property_feedback_slot_ = slot;
1766 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1767 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1770 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1771 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1772 return property_feedback_slot_;
1776 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1777 : Expression(zone, pos),
1778 bit_field_(IsForCallField::encode(false) |
1779 IsUninitializedField::encode(false) |
1780 IsStringAccessField::encode(false)),
1781 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1784 static int parent_num_ids() { return Expression::num_ids(); }
1787 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1789 class IsForCallField : public BitField8<bool, 0, 1> {};
1790 class IsUninitializedField : public BitField8<bool, 1, 1> {};
1791 class IsStringAccessField : public BitField8<bool, 2, 1> {};
1792 class KeyTypeField : public BitField8<IcCheckType, 3, 1> {};
1794 FeedbackVectorICSlot property_feedback_slot_;
1797 SmallMapList receiver_types_;
1801 class Call FINAL : public Expression {
1803 DECLARE_NODE_TYPE(Call)
1805 Expression* expression() const { return expression_; }
1806 ZoneList<Expression*>* arguments() const { return arguments_; }
1808 // Type feedback information.
1809 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1810 Isolate* isolate) OVERRIDE;
1811 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1812 ic_slot_or_slot_ = slot.ToInt();
1814 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1815 ic_slot_or_slot_ = slot.ToInt();
1817 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1819 FeedbackVectorSlot CallFeedbackSlot() const {
1820 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1821 return FeedbackVectorSlot(ic_slot_or_slot_);
1824 FeedbackVectorICSlot CallFeedbackICSlot() const {
1825 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1826 return FeedbackVectorICSlot(ic_slot_or_slot_);
1829 SmallMapList* GetReceiverTypes() OVERRIDE {
1830 if (expression()->IsProperty()) {
1831 return expression()->AsProperty()->GetReceiverTypes();
1836 bool IsMonomorphic() OVERRIDE {
1837 if (expression()->IsProperty()) {
1838 return expression()->AsProperty()->IsMonomorphic();
1840 return !target_.is_null();
1843 bool global_call() const {
1844 VariableProxy* proxy = expression_->AsVariableProxy();
1845 return proxy != NULL && proxy->var()->IsUnallocated();
1848 bool known_global_function() const {
1849 return global_call() && !target_.is_null();
1852 Handle<JSFunction> target() { return target_; }
1854 Handle<Cell> cell() { return cell_; }
1856 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1858 void set_target(Handle<JSFunction> target) { target_ = target; }
1859 void set_allocation_site(Handle<AllocationSite> site) {
1860 allocation_site_ = site;
1862 bool ComputeGlobalTarget(Handle<GlobalObject> global, LookupIterator* it);
1864 static int num_ids() { return parent_num_ids() + 2; }
1865 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1866 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1868 bool is_uninitialized() const {
1869 return IsUninitializedField::decode(bit_field_);
1871 void set_is_uninitialized(bool b) {
1872 bit_field_ = IsUninitializedField::update(bit_field_, b);
1884 // Helpers to determine how to handle the call.
1885 CallType GetCallType(Isolate* isolate) const;
1886 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1887 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1890 // Used to assert that the FullCodeGenerator records the return site.
1891 bool return_is_recorded_;
1895 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1897 : Expression(zone, pos),
1898 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1899 expression_(expression),
1900 arguments_(arguments),
1901 bit_field_(IsUninitializedField::encode(false)) {
1902 if (expression->IsProperty()) {
1903 expression->AsProperty()->mark_for_call();
1906 static int parent_num_ids() { return Expression::num_ids(); }
1909 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1911 // We store this as an integer because we don't know if we have a slot or
1912 // an ic slot until scoping time.
1913 int ic_slot_or_slot_;
1914 Expression* expression_;
1915 ZoneList<Expression*>* arguments_;
1916 Handle<JSFunction> target_;
1918 Handle<AllocationSite> allocation_site_;
1919 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1924 class CallNew FINAL : public Expression {
1926 DECLARE_NODE_TYPE(CallNew)
1928 Expression* expression() const { return expression_; }
1929 ZoneList<Expression*>* arguments() const { return arguments_; }
1931 // Type feedback information.
1932 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1933 Isolate* isolate) OVERRIDE {
1934 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1936 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1937 callnew_feedback_slot_ = slot;
1940 FeedbackVectorSlot CallNewFeedbackSlot() {
1941 DCHECK(!callnew_feedback_slot_.IsInvalid());
1942 return callnew_feedback_slot_;
1944 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1945 DCHECK(FLAG_pretenuring_call_new);
1946 return CallNewFeedbackSlot().next();
1949 void RecordTypeFeedback(TypeFeedbackOracle* oracle);
1950 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1951 Handle<JSFunction> target() const { return target_; }
1952 Handle<AllocationSite> allocation_site() const {
1953 return allocation_site_;
1956 static int num_ids() { return parent_num_ids() + 1; }
1957 static int feedback_slots() { return 1; }
1958 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1961 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1963 : Expression(zone, pos),
1964 expression_(expression),
1965 arguments_(arguments),
1966 is_monomorphic_(false),
1967 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1969 static int parent_num_ids() { return Expression::num_ids(); }
1972 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1974 Expression* expression_;
1975 ZoneList<Expression*>* arguments_;
1976 bool is_monomorphic_;
1977 Handle<JSFunction> target_;
1978 Handle<AllocationSite> allocation_site_;
1979 FeedbackVectorSlot callnew_feedback_slot_;
1983 // The CallRuntime class does not represent any official JavaScript
1984 // language construct. Instead it is used to call a C or JS function
1985 // with a set of arguments. This is used from the builtins that are
1986 // implemented in JavaScript (see "v8natives.js").
1987 class CallRuntime FINAL : public Expression {
1989 DECLARE_NODE_TYPE(CallRuntime)
1991 Handle<String> name() const { return raw_name_->string(); }
1992 const AstRawString* raw_name() const { return raw_name_; }
1993 const Runtime::Function* function() const { return function_; }
1994 ZoneList<Expression*>* arguments() const { return arguments_; }
1995 bool is_jsruntime() const { return function_ == NULL; }
1997 // Type feedback information.
1998 bool HasCallRuntimeFeedbackSlot() const {
1999 return FLAG_vector_ics && is_jsruntime();
2001 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2002 Isolate* isolate) OVERRIDE {
2003 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2005 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2006 callruntime_feedback_slot_ = slot;
2008 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2010 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2011 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2012 !callruntime_feedback_slot_.IsInvalid());
2013 return callruntime_feedback_slot_;
2016 static int num_ids() { return parent_num_ids() + 1; }
2017 TypeFeedbackId CallRuntimeFeedbackId() const {
2018 return TypeFeedbackId(local_id(0));
2022 CallRuntime(Zone* zone, const AstRawString* name,
2023 const Runtime::Function* function,
2024 ZoneList<Expression*>* arguments, int pos)
2025 : Expression(zone, pos),
2027 function_(function),
2028 arguments_(arguments),
2029 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2030 static int parent_num_ids() { return Expression::num_ids(); }
2033 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2035 const AstRawString* raw_name_;
2036 const Runtime::Function* function_;
2037 ZoneList<Expression*>* arguments_;
2038 FeedbackVectorICSlot callruntime_feedback_slot_;
2042 class UnaryOperation FINAL : public Expression {
2044 DECLARE_NODE_TYPE(UnaryOperation)
2046 Token::Value op() const { return op_; }
2047 Expression* expression() const { return expression_; }
2049 // For unary not (Token::NOT), the AST ids where true and false will
2050 // actually be materialized, respectively.
2051 static int num_ids() { return parent_num_ids() + 2; }
2052 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2053 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2055 virtual void RecordToBooleanTypeFeedback(
2056 TypeFeedbackOracle* oracle) OVERRIDE;
2059 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2060 : Expression(zone, pos), op_(op), expression_(expression) {
2061 DCHECK(Token::IsUnaryOp(op));
2063 static int parent_num_ids() { return Expression::num_ids(); }
2066 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2069 Expression* expression_;
2073 class BinaryOperation FINAL : public Expression {
2075 DECLARE_NODE_TYPE(BinaryOperation)
2077 Token::Value op() const { return static_cast<Token::Value>(op_); }
2078 Expression* left() const { return left_; }
2079 Expression* right() const { return right_; }
2080 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2081 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2082 allocation_site_ = allocation_site;
2085 // The short-circuit logical operations need an AST ID for their
2086 // right-hand subexpression.
2087 static int num_ids() { return parent_num_ids() + 2; }
2088 BailoutId RightId() const { return BailoutId(local_id(0)); }
2090 TypeFeedbackId BinaryOperationFeedbackId() const {
2091 return TypeFeedbackId(local_id(1));
2093 Maybe<int> fixed_right_arg() const {
2094 return has_fixed_right_arg_ ? Maybe<int>(fixed_right_arg_value_)
2097 void set_fixed_right_arg(Maybe<int> arg) {
2098 has_fixed_right_arg_ = arg.has_value;
2099 if (arg.has_value) fixed_right_arg_value_ = arg.value;
2102 virtual void RecordToBooleanTypeFeedback(
2103 TypeFeedbackOracle* oracle) OVERRIDE;
2106 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2107 Expression* right, int pos)
2108 : Expression(zone, pos),
2109 op_(static_cast<byte>(op)),
2110 has_fixed_right_arg_(false),
2111 fixed_right_arg_value_(0),
2114 DCHECK(Token::IsBinaryOp(op));
2116 static int parent_num_ids() { return Expression::num_ids(); }
2119 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2121 const byte op_; // actually Token::Value
2122 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2123 // type for the RHS. Currenty it's actually a Maybe<int>
2124 bool has_fixed_right_arg_;
2125 int fixed_right_arg_value_;
2128 Handle<AllocationSite> allocation_site_;
2132 class CountOperation FINAL : public Expression {
2134 DECLARE_NODE_TYPE(CountOperation)
2136 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2137 bool is_postfix() const { return !is_prefix(); }
2139 Token::Value op() const { return TokenField::decode(bit_field_); }
2140 Token::Value binary_op() {
2141 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2144 Expression* expression() const { return expression_; }
2146 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2147 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2148 IcCheckType GetKeyType() const OVERRIDE {
2149 return KeyTypeField::decode(bit_field_);
2151 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2152 return StoreModeField::decode(bit_field_);
2154 Type* type() const { return type_; }
2155 void set_key_type(IcCheckType type) {
2156 bit_field_ = KeyTypeField::update(bit_field_, type);
2158 void set_store_mode(KeyedAccessStoreMode mode) {
2159 bit_field_ = StoreModeField::update(bit_field_, mode);
2161 void set_type(Type* type) { type_ = type; }
2163 static int num_ids() { return parent_num_ids() + 4; }
2164 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2165 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2166 TypeFeedbackId CountBinOpFeedbackId() const {
2167 return TypeFeedbackId(local_id(2));
2169 TypeFeedbackId CountStoreFeedbackId() const {
2170 return TypeFeedbackId(local_id(3));
2174 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2176 : Expression(zone, pos),
2177 bit_field_(IsPrefixField::encode(is_prefix) |
2178 KeyTypeField::encode(ELEMENT) |
2179 StoreModeField::encode(STANDARD_STORE) |
2180 TokenField::encode(op)),
2182 expression_(expr) {}
2183 static int parent_num_ids() { return Expression::num_ids(); }
2186 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2188 class IsPrefixField : public BitField16<bool, 0, 1> {};
2189 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2190 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2191 class TokenField : public BitField16<Token::Value, 6, 8> {};
2193 // Starts with 16-bit field, which should get packed together with
2194 // Expression's trailing 16-bit field.
2195 uint16_t bit_field_;
2197 Expression* expression_;
2198 SmallMapList receiver_types_;
2202 class CompareOperation FINAL : public Expression {
2204 DECLARE_NODE_TYPE(CompareOperation)
2206 Token::Value op() const { return op_; }
2207 Expression* left() const { return left_; }
2208 Expression* right() const { return right_; }
2210 // Type feedback information.
2211 static int num_ids() { return parent_num_ids() + 1; }
2212 TypeFeedbackId CompareOperationFeedbackId() const {
2213 return TypeFeedbackId(local_id(0));
2215 Type* combined_type() const { return combined_type_; }
2216 void set_combined_type(Type* type) { combined_type_ = type; }
2218 // Match special cases.
2219 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2220 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2221 bool IsLiteralCompareNull(Expression** expr);
2224 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2225 Expression* right, int pos)
2226 : Expression(zone, pos),
2230 combined_type_(Type::None(zone)) {
2231 DCHECK(Token::IsCompareOp(op));
2233 static int parent_num_ids() { return Expression::num_ids(); }
2236 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2242 Type* combined_type_;
2246 class Conditional FINAL : public Expression {
2248 DECLARE_NODE_TYPE(Conditional)
2250 Expression* condition() const { return condition_; }
2251 Expression* then_expression() const { return then_expression_; }
2252 Expression* else_expression() const { return else_expression_; }
2254 static int num_ids() { return parent_num_ids() + 2; }
2255 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2256 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2259 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2260 Expression* else_expression, int position)
2261 : Expression(zone, position),
2262 condition_(condition),
2263 then_expression_(then_expression),
2264 else_expression_(else_expression) {}
2265 static int parent_num_ids() { return Expression::num_ids(); }
2268 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2270 Expression* condition_;
2271 Expression* then_expression_;
2272 Expression* else_expression_;
2276 class Assignment FINAL : public Expression {
2278 DECLARE_NODE_TYPE(Assignment)
2280 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2282 Token::Value binary_op() const;
2284 Token::Value op() const { return TokenField::decode(bit_field_); }
2285 Expression* target() const { return target_; }
2286 Expression* value() const { return value_; }
2287 BinaryOperation* binary_operation() const { return binary_operation_; }
2289 // This check relies on the definition order of token in token.h.
2290 bool is_compound() const { return op() > Token::ASSIGN; }
2292 static int num_ids() { return parent_num_ids() + 2; }
2293 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2295 // Type feedback information.
2296 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2297 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2298 bool IsUninitialized() const {
2299 return IsUninitializedField::decode(bit_field_);
2301 bool HasNoTypeInformation() {
2302 return IsUninitializedField::decode(bit_field_);
2304 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2305 IcCheckType GetKeyType() const OVERRIDE {
2306 return KeyTypeField::decode(bit_field_);
2308 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2309 return StoreModeField::decode(bit_field_);
2311 void set_is_uninitialized(bool b) {
2312 bit_field_ = IsUninitializedField::update(bit_field_, b);
2314 void set_key_type(IcCheckType key_type) {
2315 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2317 void set_store_mode(KeyedAccessStoreMode mode) {
2318 bit_field_ = StoreModeField::update(bit_field_, mode);
2322 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2324 static int parent_num_ids() { return Expression::num_ids(); }
2327 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2329 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2330 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2331 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2332 class TokenField : public BitField16<Token::Value, 6, 8> {};
2334 // Starts with 16-bit field, which should get packed together with
2335 // Expression's trailing 16-bit field.
2336 uint16_t bit_field_;
2337 Expression* target_;
2339 BinaryOperation* binary_operation_;
2340 SmallMapList receiver_types_;
2344 class Yield FINAL : public Expression {
2346 DECLARE_NODE_TYPE(Yield)
2349 kInitial, // The initial yield that returns the unboxed generator object.
2350 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2351 kDelegating, // A yield*.
2352 kFinal // A return: { value: EXPRESSION, done: true }
2355 Expression* generator_object() const { return generator_object_; }
2356 Expression* expression() const { return expression_; }
2357 Kind yield_kind() const { return yield_kind_; }
2359 // Delegating yield surrounds the "yield" in a "try/catch". This index
2360 // locates the catch handler in the handler table, and is equivalent to
2361 // TryCatchStatement::index().
2363 DCHECK_EQ(kDelegating, yield_kind());
2366 void set_index(int index) {
2367 DCHECK_EQ(kDelegating, yield_kind());
2371 // Type feedback information.
2372 bool HasFeedbackSlots() const {
2373 return FLAG_vector_ics && (yield_kind() == kDelegating);
2375 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2376 Isolate* isolate) OVERRIDE {
2377 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2379 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2380 yield_first_feedback_slot_ = slot;
2382 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2383 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2386 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2387 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2388 return yield_first_feedback_slot_;
2391 FeedbackVectorICSlot DoneFeedbackSlot() {
2392 return KeyedLoadFeedbackSlot().next();
2395 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2398 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2399 Kind yield_kind, int pos)
2400 : Expression(zone, pos),
2401 generator_object_(generator_object),
2402 expression_(expression),
2403 yield_kind_(yield_kind),
2405 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2408 Expression* generator_object_;
2409 Expression* expression_;
2412 FeedbackVectorICSlot yield_first_feedback_slot_;
2416 class Throw FINAL : public Expression {
2418 DECLARE_NODE_TYPE(Throw)
2420 Expression* exception() const { return exception_; }
2423 Throw(Zone* zone, Expression* exception, int pos)
2424 : Expression(zone, pos), exception_(exception) {}
2427 Expression* exception_;
2431 class FunctionLiteral FINAL : public Expression {
2434 ANONYMOUS_EXPRESSION,
2439 enum ParameterFlag {
2440 kNoDuplicateParameters = 0,
2441 kHasDuplicateParameters = 1
2444 enum IsFunctionFlag {
2449 enum IsParenthesizedFlag {
2454 enum ArityRestriction {
2460 DECLARE_NODE_TYPE(FunctionLiteral)
2462 Handle<String> name() const { return raw_name_->string(); }
2463 const AstRawString* raw_name() const { return raw_name_; }
2464 Scope* scope() const { return scope_; }
2465 ZoneList<Statement*>* body() const { return body_; }
2466 void set_function_token_position(int pos) { function_token_position_ = pos; }
2467 int function_token_position() const { return function_token_position_; }
2468 int start_position() const;
2469 int end_position() const;
2470 int SourceSize() const { return end_position() - start_position(); }
2471 bool is_expression() const { return IsExpression::decode(bitfield_); }
2472 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2473 LanguageMode language_mode() const;
2474 bool uses_super_property() const;
2475 bool uses_super_constructor_call() const;
2477 static bool NeedsHomeObject(Expression* literal) {
2478 return literal != NULL && literal->IsFunctionLiteral() &&
2479 literal->AsFunctionLiteral()->uses_super_property();
2482 int materialized_literal_count() { return materialized_literal_count_; }
2483 int expected_property_count() { return expected_property_count_; }
2484 int handler_count() { return handler_count_; }
2485 int parameter_count() { return parameter_count_; }
2487 bool AllowsLazyCompilation();
2488 bool AllowsLazyCompilationWithoutContext();
2490 void InitializeSharedInfo(Handle<Code> code);
2492 Handle<String> debug_name() const {
2493 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2494 return raw_name_->string();
2496 return inferred_name();
2499 Handle<String> inferred_name() const {
2500 if (!inferred_name_.is_null()) {
2501 DCHECK(raw_inferred_name_ == NULL);
2502 return inferred_name_;
2504 if (raw_inferred_name_ != NULL) {
2505 return raw_inferred_name_->string();
2508 return Handle<String>();
2511 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2512 void set_inferred_name(Handle<String> inferred_name) {
2513 DCHECK(!inferred_name.is_null());
2514 inferred_name_ = inferred_name;
2515 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2516 raw_inferred_name_ = NULL;
2519 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2520 DCHECK(raw_inferred_name != NULL);
2521 raw_inferred_name_ = raw_inferred_name;
2522 DCHECK(inferred_name_.is_null());
2523 inferred_name_ = Handle<String>();
2526 // shared_info may be null if it's not cached in full code.
2527 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2529 bool pretenure() { return Pretenure::decode(bitfield_); }
2530 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2532 bool has_duplicate_parameters() {
2533 return HasDuplicateParameters::decode(bitfield_);
2536 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2538 // This is used as a heuristic on when to eagerly compile a function
2539 // literal. We consider the following constructs as hints that the
2540 // function will be called immediately:
2541 // - (function() { ... })();
2542 // - var x = function() { ... }();
2543 bool is_parenthesized() {
2544 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2546 void set_parenthesized() {
2547 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2550 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2552 int ast_node_count() { return ast_properties_.node_count(); }
2553 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2554 void set_ast_properties(AstProperties* ast_properties) {
2555 ast_properties_ = *ast_properties;
2557 const FeedbackVectorSpec& feedback_vector_spec() const {
2558 return ast_properties_.get_spec();
2560 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2561 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2562 void set_dont_optimize_reason(BailoutReason reason) {
2563 dont_optimize_reason_ = reason;
2567 FunctionLiteral(Zone* zone, const AstRawString* name,
2568 AstValueFactory* ast_value_factory, Scope* scope,
2569 ZoneList<Statement*>* body, int materialized_literal_count,
2570 int expected_property_count, int handler_count,
2571 int parameter_count, FunctionType function_type,
2572 ParameterFlag has_duplicate_parameters,
2573 IsFunctionFlag is_function,
2574 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2576 : Expression(zone, position),
2580 raw_inferred_name_(ast_value_factory->empty_string()),
2581 dont_optimize_reason_(kNoReason),
2582 materialized_literal_count_(materialized_literal_count),
2583 expected_property_count_(expected_property_count),
2584 handler_count_(handler_count),
2585 parameter_count_(parameter_count),
2586 function_token_position_(RelocInfo::kNoPosition) {
2587 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2588 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2589 Pretenure::encode(false) |
2590 HasDuplicateParameters::encode(has_duplicate_parameters) |
2591 IsFunction::encode(is_function) |
2592 IsParenthesized::encode(is_parenthesized) |
2593 FunctionKindBits::encode(kind);
2594 DCHECK(IsValidFunctionKind(kind));
2598 const AstRawString* raw_name_;
2599 Handle<String> name_;
2600 Handle<SharedFunctionInfo> shared_info_;
2602 ZoneList<Statement*>* body_;
2603 const AstString* raw_inferred_name_;
2604 Handle<String> inferred_name_;
2605 AstProperties ast_properties_;
2606 BailoutReason dont_optimize_reason_;
2608 int materialized_literal_count_;
2609 int expected_property_count_;
2611 int parameter_count_;
2612 int function_token_position_;
2615 class IsExpression : public BitField<bool, 0, 1> {};
2616 class IsAnonymous : public BitField<bool, 1, 1> {};
2617 class Pretenure : public BitField<bool, 2, 1> {};
2618 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2619 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2620 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2621 class FunctionKindBits : public BitField<FunctionKind, 6, 6> {};
2625 class ClassLiteral FINAL : public Expression {
2627 typedef ObjectLiteralProperty Property;
2629 DECLARE_NODE_TYPE(ClassLiteral)
2631 Handle<String> name() const { return raw_name_->string(); }
2632 const AstRawString* raw_name() const { return raw_name_; }
2633 Scope* scope() const { return scope_; }
2634 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2635 Expression* extends() const { return extends_; }
2636 FunctionLiteral* constructor() const { return constructor_; }
2637 ZoneList<Property*>* properties() const { return properties_; }
2638 int start_position() const { return position(); }
2639 int end_position() const { return end_position_; }
2641 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2642 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2643 BailoutId ExitId() { return BailoutId(local_id(2)); }
2645 // Return an AST id for a property that is used in simulate instructions.
2646 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2648 // Unlike other AST nodes, this number of bailout IDs allocated for an
2649 // ClassLiteral can vary, so num_ids() is not a static method.
2650 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2653 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2654 VariableProxy* class_variable_proxy, Expression* extends,
2655 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2656 int start_position, int end_position)
2657 : Expression(zone, start_position),
2660 class_variable_proxy_(class_variable_proxy),
2662 constructor_(constructor),
2663 properties_(properties),
2664 end_position_(end_position) {}
2665 static int parent_num_ids() { return Expression::num_ids(); }
2668 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2670 const AstRawString* raw_name_;
2672 VariableProxy* class_variable_proxy_;
2673 Expression* extends_;
2674 FunctionLiteral* constructor_;
2675 ZoneList<Property*>* properties_;
2680 class NativeFunctionLiteral FINAL : public Expression {
2682 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2684 Handle<String> name() const { return name_->string(); }
2685 v8::Extension* extension() const { return extension_; }
2688 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2689 v8::Extension* extension, int pos)
2690 : Expression(zone, pos), name_(name), extension_(extension) {}
2693 const AstRawString* name_;
2694 v8::Extension* extension_;
2698 class ThisFunction FINAL : public Expression {
2700 DECLARE_NODE_TYPE(ThisFunction)
2703 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2707 class SuperReference FINAL : public Expression {
2709 DECLARE_NODE_TYPE(SuperReference)
2711 VariableProxy* this_var() const { return this_var_; }
2713 static int num_ids() { return parent_num_ids() + 1; }
2714 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2716 // Type feedback information.
2717 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2718 Isolate* isolate) OVERRIDE {
2719 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2721 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2722 homeobject_feedback_slot_ = slot;
2724 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2726 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2727 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2728 return homeobject_feedback_slot_;
2732 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2733 : Expression(zone, pos),
2734 this_var_(this_var),
2735 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2736 DCHECK(this_var->is_this());
2738 static int parent_num_ids() { return Expression::num_ids(); }
2741 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2743 VariableProxy* this_var_;
2744 FeedbackVectorICSlot homeobject_feedback_slot_;
2748 #undef DECLARE_NODE_TYPE
2751 // ----------------------------------------------------------------------------
2752 // Regular expressions
2755 class RegExpVisitor BASE_EMBEDDED {
2757 virtual ~RegExpVisitor() { }
2758 #define MAKE_CASE(Name) \
2759 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2760 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2765 class RegExpTree : public ZoneObject {
2767 static const int kInfinity = kMaxInt;
2768 virtual ~RegExpTree() {}
2769 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2770 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2771 RegExpNode* on_success) = 0;
2772 virtual bool IsTextElement() { return false; }
2773 virtual bool IsAnchoredAtStart() { return false; }
2774 virtual bool IsAnchoredAtEnd() { return false; }
2775 virtual int min_match() = 0;
2776 virtual int max_match() = 0;
2777 // Returns the interval of registers used for captures within this
2779 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2780 virtual void AppendToText(RegExpText* text, Zone* zone);
2781 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2782 #define MAKE_ASTYPE(Name) \
2783 virtual RegExp##Name* As##Name(); \
2784 virtual bool Is##Name();
2785 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2790 class RegExpDisjunction FINAL : public RegExpTree {
2792 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2793 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2794 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2795 RegExpNode* on_success) OVERRIDE;
2796 RegExpDisjunction* AsDisjunction() OVERRIDE;
2797 Interval CaptureRegisters() OVERRIDE;
2798 bool IsDisjunction() OVERRIDE;
2799 bool IsAnchoredAtStart() OVERRIDE;
2800 bool IsAnchoredAtEnd() OVERRIDE;
2801 int min_match() OVERRIDE { return min_match_; }
2802 int max_match() OVERRIDE { return max_match_; }
2803 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2805 ZoneList<RegExpTree*>* alternatives_;
2811 class RegExpAlternative FINAL : public RegExpTree {
2813 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2814 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2815 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2816 RegExpNode* on_success) OVERRIDE;
2817 RegExpAlternative* AsAlternative() OVERRIDE;
2818 Interval CaptureRegisters() OVERRIDE;
2819 bool IsAlternative() OVERRIDE;
2820 bool IsAnchoredAtStart() OVERRIDE;
2821 bool IsAnchoredAtEnd() OVERRIDE;
2822 int min_match() OVERRIDE { return min_match_; }
2823 int max_match() OVERRIDE { return max_match_; }
2824 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2826 ZoneList<RegExpTree*>* nodes_;
2832 class RegExpAssertion FINAL : public RegExpTree {
2834 enum AssertionType {
2842 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2843 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2844 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2845 RegExpNode* on_success) OVERRIDE;
2846 RegExpAssertion* AsAssertion() OVERRIDE;
2847 bool IsAssertion() OVERRIDE;
2848 bool IsAnchoredAtStart() OVERRIDE;
2849 bool IsAnchoredAtEnd() OVERRIDE;
2850 int min_match() OVERRIDE { return 0; }
2851 int max_match() OVERRIDE { return 0; }
2852 AssertionType assertion_type() { return assertion_type_; }
2854 AssertionType assertion_type_;
2858 class CharacterSet FINAL BASE_EMBEDDED {
2860 explicit CharacterSet(uc16 standard_set_type)
2862 standard_set_type_(standard_set_type) {}
2863 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2865 standard_set_type_(0) {}
2866 ZoneList<CharacterRange>* ranges(Zone* zone);
2867 uc16 standard_set_type() { return standard_set_type_; }
2868 void set_standard_set_type(uc16 special_set_type) {
2869 standard_set_type_ = special_set_type;
2871 bool is_standard() { return standard_set_type_ != 0; }
2872 void Canonicalize();
2874 ZoneList<CharacterRange>* ranges_;
2875 // If non-zero, the value represents a standard set (e.g., all whitespace
2876 // characters) without having to expand the ranges.
2877 uc16 standard_set_type_;
2881 class RegExpCharacterClass FINAL : public RegExpTree {
2883 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2885 is_negated_(is_negated) { }
2886 explicit RegExpCharacterClass(uc16 type)
2888 is_negated_(false) { }
2889 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2890 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2891 RegExpNode* on_success) OVERRIDE;
2892 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2893 bool IsCharacterClass() OVERRIDE;
2894 bool IsTextElement() OVERRIDE { return true; }
2895 int min_match() OVERRIDE { return 1; }
2896 int max_match() OVERRIDE { return 1; }
2897 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2898 CharacterSet character_set() { return set_; }
2899 // TODO(lrn): Remove need for complex version if is_standard that
2900 // recognizes a mangled standard set and just do { return set_.is_special(); }
2901 bool is_standard(Zone* zone);
2902 // Returns a value representing the standard character set if is_standard()
2904 // Currently used values are:
2905 // s : unicode whitespace
2906 // S : unicode non-whitespace
2907 // w : ASCII word character (digit, letter, underscore)
2908 // W : non-ASCII word character
2910 // D : non-ASCII digit
2911 // . : non-unicode non-newline
2912 // * : All characters
2913 uc16 standard_type() { return set_.standard_set_type(); }
2914 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2915 bool is_negated() { return is_negated_; }
2923 class RegExpAtom FINAL : public RegExpTree {
2925 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2926 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2927 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2928 RegExpNode* on_success) OVERRIDE;
2929 RegExpAtom* AsAtom() OVERRIDE;
2930 bool IsAtom() OVERRIDE;
2931 bool IsTextElement() OVERRIDE { return true; }
2932 int min_match() OVERRIDE { return data_.length(); }
2933 int max_match() OVERRIDE { return data_.length(); }
2934 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2935 Vector<const uc16> data() { return data_; }
2936 int length() { return data_.length(); }
2938 Vector<const uc16> data_;
2942 class RegExpText FINAL : public RegExpTree {
2944 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2945 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2946 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2947 RegExpNode* on_success) OVERRIDE;
2948 RegExpText* AsText() OVERRIDE;
2949 bool IsText() OVERRIDE;
2950 bool IsTextElement() OVERRIDE { return true; }
2951 int min_match() OVERRIDE { return length_; }
2952 int max_match() OVERRIDE { return length_; }
2953 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2954 void AddElement(TextElement elm, Zone* zone) {
2955 elements_.Add(elm, zone);
2956 length_ += elm.length();
2958 ZoneList<TextElement>* elements() { return &elements_; }
2960 ZoneList<TextElement> elements_;
2965 class RegExpQuantifier FINAL : public RegExpTree {
2967 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
2968 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
2972 min_match_(min * body->min_match()),
2973 quantifier_type_(type) {
2974 if (max > 0 && body->max_match() > kInfinity / max) {
2975 max_match_ = kInfinity;
2977 max_match_ = max * body->max_match();
2980 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2981 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2982 RegExpNode* on_success) OVERRIDE;
2983 static RegExpNode* ToNode(int min,
2987 RegExpCompiler* compiler,
2988 RegExpNode* on_success,
2989 bool not_at_start = false);
2990 RegExpQuantifier* AsQuantifier() OVERRIDE;
2991 Interval CaptureRegisters() OVERRIDE;
2992 bool IsQuantifier() OVERRIDE;
2993 int min_match() OVERRIDE { return min_match_; }
2994 int max_match() OVERRIDE { return max_match_; }
2995 int min() { return min_; }
2996 int max() { return max_; }
2997 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
2998 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
2999 bool is_greedy() { return quantifier_type_ == GREEDY; }
3000 RegExpTree* body() { return body_; }
3008 QuantifierType quantifier_type_;
3012 class RegExpCapture FINAL : public RegExpTree {
3014 explicit RegExpCapture(RegExpTree* body, int index)
3015 : body_(body), index_(index) { }
3016 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3017 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3018 RegExpNode* on_success) OVERRIDE;
3019 static RegExpNode* ToNode(RegExpTree* body,
3021 RegExpCompiler* compiler,
3022 RegExpNode* on_success);
3023 RegExpCapture* AsCapture() OVERRIDE;
3024 bool IsAnchoredAtStart() OVERRIDE;
3025 bool IsAnchoredAtEnd() OVERRIDE;
3026 Interval CaptureRegisters() OVERRIDE;
3027 bool IsCapture() OVERRIDE;
3028 int min_match() OVERRIDE { return body_->min_match(); }
3029 int max_match() OVERRIDE { return body_->max_match(); }
3030 RegExpTree* body() { return body_; }
3031 int index() { return index_; }
3032 static int StartRegister(int index) { return index * 2; }
3033 static int EndRegister(int index) { return index * 2 + 1; }
3041 class RegExpLookahead FINAL : public RegExpTree {
3043 RegExpLookahead(RegExpTree* body,
3048 is_positive_(is_positive),
3049 capture_count_(capture_count),
3050 capture_from_(capture_from) { }
3052 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3053 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3054 RegExpNode* on_success) OVERRIDE;
3055 RegExpLookahead* AsLookahead() OVERRIDE;
3056 Interval CaptureRegisters() OVERRIDE;
3057 bool IsLookahead() OVERRIDE;
3058 bool IsAnchoredAtStart() OVERRIDE;
3059 int min_match() OVERRIDE { return 0; }
3060 int max_match() OVERRIDE { return 0; }
3061 RegExpTree* body() { return body_; }
3062 bool is_positive() { return is_positive_; }
3063 int capture_count() { return capture_count_; }
3064 int capture_from() { return capture_from_; }
3074 class RegExpBackReference FINAL : public RegExpTree {
3076 explicit RegExpBackReference(RegExpCapture* capture)
3077 : capture_(capture) { }
3078 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3079 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3080 RegExpNode* on_success) OVERRIDE;
3081 RegExpBackReference* AsBackReference() OVERRIDE;
3082 bool IsBackReference() OVERRIDE;
3083 int min_match() OVERRIDE { return 0; }
3084 int max_match() OVERRIDE { return capture_->max_match(); }
3085 int index() { return capture_->index(); }
3086 RegExpCapture* capture() { return capture_; }
3088 RegExpCapture* capture_;
3092 class RegExpEmpty FINAL : public RegExpTree {
3095 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3096 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3097 RegExpNode* on_success) OVERRIDE;
3098 RegExpEmpty* AsEmpty() OVERRIDE;
3099 bool IsEmpty() OVERRIDE;
3100 int min_match() OVERRIDE { return 0; }
3101 int max_match() OVERRIDE { return 0; }
3105 // ----------------------------------------------------------------------------
3106 // Out-of-line inline constructors (to side-step cyclic dependencies).
3108 inline ModuleVariable::ModuleVariable(Zone* zone, VariableProxy* proxy, int pos)
3109 : Module(zone, proxy->interface(), pos),
3114 // ----------------------------------------------------------------------------
3116 // - leaf node visitors are abstract.
3118 class AstVisitor BASE_EMBEDDED {
3121 virtual ~AstVisitor() {}
3123 // Stack overflow check and dynamic dispatch.
3124 virtual void Visit(AstNode* node) = 0;
3126 // Iteration left-to-right.
3127 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3128 virtual void VisitStatements(ZoneList<Statement*>* statements);
3129 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3131 // Individual AST nodes.
3132 #define DEF_VISIT(type) \
3133 virtual void Visit##type(type* node) = 0;
3134 AST_NODE_LIST(DEF_VISIT)
3139 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3141 void Visit(AstNode* node) FINAL { \
3142 if (!CheckStackOverflow()) node->Accept(this); \
3145 void SetStackOverflow() { stack_overflow_ = true; } \
3146 void ClearStackOverflow() { stack_overflow_ = false; } \
3147 bool HasStackOverflow() const { return stack_overflow_; } \
3149 bool CheckStackOverflow() { \
3150 if (stack_overflow_) return true; \
3151 StackLimitCheck check(isolate_); \
3152 if (!check.HasOverflowed()) return false; \
3153 stack_overflow_ = true; \
3158 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3159 isolate_ = isolate; \
3161 stack_overflow_ = false; \
3163 Zone* zone() { return zone_; } \
3164 Isolate* isolate() { return isolate_; } \
3166 Isolate* isolate_; \
3168 bool stack_overflow_
3171 // ----------------------------------------------------------------------------
3174 class AstNodeFactory FINAL BASE_EMBEDDED {
3176 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3177 : zone_(ast_value_factory->zone()),
3178 ast_value_factory_(ast_value_factory) {}
3180 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3184 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3187 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3189 FunctionLiteral* fun,
3192 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3195 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3199 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3202 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3206 return new (zone_) ImportDeclaration(zone_, proxy, module, scope, pos);
3209 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3212 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3215 ModuleLiteral* NewModuleLiteral(Block* body, Interface* interface, int pos) {
3216 return new (zone_) ModuleLiteral(zone_, body, interface, pos);
3219 ModuleVariable* NewModuleVariable(VariableProxy* proxy, int pos) {
3220 return new (zone_) ModuleVariable(zone_, proxy, pos);
3223 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3224 return new (zone_) ModulePath(zone_, origin, name, pos);
3227 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3228 return new (zone_) ModuleUrl(zone_, url, pos);
3231 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3233 bool is_initializer_block,
3236 Block(zone_, labels, capacity, is_initializer_block, pos);
3239 #define STATEMENT_WITH_LABELS(NodeType) \
3240 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3241 return new (zone_) NodeType(zone_, labels, pos); \
3243 STATEMENT_WITH_LABELS(DoWhileStatement)
3244 STATEMENT_WITH_LABELS(WhileStatement)
3245 STATEMENT_WITH_LABELS(ForStatement)
3246 STATEMENT_WITH_LABELS(SwitchStatement)
3247 #undef STATEMENT_WITH_LABELS
3249 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3250 ZoneList<const AstRawString*>* labels,
3252 switch (visit_mode) {
3253 case ForEachStatement::ENUMERATE: {
3254 return new (zone_) ForInStatement(zone_, labels, pos);
3256 case ForEachStatement::ITERATE: {
3257 return new (zone_) ForOfStatement(zone_, labels, pos);
3264 ModuleStatement* NewModuleStatement(
3265 VariableProxy* proxy, Block* body, int pos) {
3266 return new (zone_) ModuleStatement(zone_, proxy, body, pos);
3269 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3270 return new (zone_) ExpressionStatement(zone_, expression, pos);
3273 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3274 return new (zone_) ContinueStatement(zone_, target, pos);
3277 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3278 return new (zone_) BreakStatement(zone_, target, pos);
3281 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3282 return new (zone_) ReturnStatement(zone_, expression, pos);
3285 WithStatement* NewWithStatement(Scope* scope,
3286 Expression* expression,
3287 Statement* statement,
3289 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3292 IfStatement* NewIfStatement(Expression* condition,
3293 Statement* then_statement,
3294 Statement* else_statement,
3297 IfStatement(zone_, condition, then_statement, else_statement, pos);
3300 TryCatchStatement* NewTryCatchStatement(int index,
3306 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3307 variable, catch_block, pos);
3310 TryFinallyStatement* NewTryFinallyStatement(int index,
3312 Block* finally_block,
3315 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3318 DebuggerStatement* NewDebuggerStatement(int pos) {
3319 return new (zone_) DebuggerStatement(zone_, pos);
3322 EmptyStatement* NewEmptyStatement(int pos) {
3323 return new(zone_) EmptyStatement(zone_, pos);
3326 CaseClause* NewCaseClause(
3327 Expression* label, ZoneList<Statement*>* statements, int pos) {
3328 return new (zone_) CaseClause(zone_, label, statements, pos);
3331 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3333 Literal(zone_, ast_value_factory_->NewString(string), pos);
3336 // A JavaScript symbol (ECMA-262 edition 6).
3337 Literal* NewSymbolLiteral(const char* name, int pos) {
3338 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3341 Literal* NewNumberLiteral(double number, int pos) {
3343 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3346 Literal* NewSmiLiteral(int number, int pos) {
3347 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3350 Literal* NewBooleanLiteral(bool b, int pos) {
3351 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3354 Literal* NewNullLiteral(int pos) {
3355 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3358 Literal* NewUndefinedLiteral(int pos) {
3359 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3362 Literal* NewTheHoleLiteral(int pos) {
3363 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3366 ObjectLiteral* NewObjectLiteral(
3367 ZoneList<ObjectLiteral::Property*>* properties,
3369 int boilerplate_properties,
3372 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3373 boilerplate_properties, has_function, pos);
3376 ObjectLiteral::Property* NewObjectLiteralProperty(
3377 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3378 bool is_static, bool is_computed_name) {
3380 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3383 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3386 bool is_computed_name) {
3387 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3388 is_static, is_computed_name);
3391 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3392 const AstRawString* flags,
3395 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3398 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3401 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3404 VariableProxy* NewVariableProxy(Variable* var,
3405 int pos = RelocInfo::kNoPosition) {
3406 return new (zone_) VariableProxy(zone_, var, pos);
3409 VariableProxy* NewVariableProxy(const AstRawString* name,
3411 Interface* interface = Interface::NewValue(),
3412 int position = RelocInfo::kNoPosition) {
3413 return new (zone_) VariableProxy(zone_, name, is_this, interface, position);
3416 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3417 return new (zone_) Property(zone_, obj, key, pos);
3420 Call* NewCall(Expression* expression,
3421 ZoneList<Expression*>* arguments,
3423 return new (zone_) Call(zone_, expression, arguments, pos);
3426 CallNew* NewCallNew(Expression* expression,
3427 ZoneList<Expression*>* arguments,
3429 return new (zone_) CallNew(zone_, expression, arguments, pos);
3432 CallRuntime* NewCallRuntime(const AstRawString* name,
3433 const Runtime::Function* function,
3434 ZoneList<Expression*>* arguments,
3436 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3439 UnaryOperation* NewUnaryOperation(Token::Value op,
3440 Expression* expression,
3442 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3445 BinaryOperation* NewBinaryOperation(Token::Value op,
3449 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3452 CountOperation* NewCountOperation(Token::Value op,
3456 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3459 CompareOperation* NewCompareOperation(Token::Value op,
3463 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3466 Conditional* NewConditional(Expression* condition,
3467 Expression* then_expression,
3468 Expression* else_expression,
3470 return new (zone_) Conditional(zone_, condition, then_expression,
3471 else_expression, position);
3474 Assignment* NewAssignment(Token::Value op,
3478 DCHECK(Token::IsAssignmentOp(op));
3479 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3480 if (assign->is_compound()) {
3481 DCHECK(Token::IsAssignmentOp(op));
3482 assign->binary_operation_ =
3483 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3488 Yield* NewYield(Expression *generator_object,
3489 Expression* expression,
3490 Yield::Kind yield_kind,
3492 if (!expression) expression = NewUndefinedLiteral(pos);
3494 Yield(zone_, generator_object, expression, yield_kind, pos);
3497 Throw* NewThrow(Expression* exception, int pos) {
3498 return new (zone_) Throw(zone_, exception, pos);
3501 FunctionLiteral* NewFunctionLiteral(
3502 const AstRawString* name, AstValueFactory* ast_value_factory,
3503 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3504 int expected_property_count, int handler_count, int parameter_count,
3505 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3506 FunctionLiteral::FunctionType function_type,
3507 FunctionLiteral::IsFunctionFlag is_function,
3508 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3510 return new (zone_) FunctionLiteral(
3511 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3512 expected_property_count, handler_count, parameter_count, function_type,
3513 has_duplicate_parameters, is_function, is_parenthesized, kind,
3517 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3518 VariableProxy* proxy, Expression* extends,
3519 FunctionLiteral* constructor,
3520 ZoneList<ObjectLiteral::Property*>* properties,
3521 int start_position, int end_position) {
3523 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3524 properties, start_position, end_position);
3527 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3528 v8::Extension* extension,
3530 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3533 ThisFunction* NewThisFunction(int pos) {
3534 return new (zone_) ThisFunction(zone_, pos);
3537 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3538 return new (zone_) SuperReference(zone_, this_var, pos);
3543 AstValueFactory* ast_value_factory_;
3547 } } // namespace v8::internal