1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/assembler.h"
11 #include "src/ast-value-factory.h"
12 #include "src/bailout-reason.h"
13 #include "src/factory.h"
14 #include "src/isolate.h"
15 #include "src/jsregexp.h"
16 #include "src/list-inl.h"
17 #include "src/modules.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/smart-pointers.h"
21 #include "src/token.h"
22 #include "src/types.h"
23 #include "src/utils.h"
24 #include "src/variables.h"
29 // The abstract syntax tree is an intermediate, light-weight
30 // representation of the parsed JavaScript code suitable for
31 // compilation to native code.
33 // Nodes are allocated in a separate zone, which allows faster
34 // allocation and constant-time deallocation of the entire syntax
38 // ----------------------------------------------------------------------------
39 // Nodes of the abstract syntax tree. Only concrete classes are
42 #define DECLARATION_NODE_LIST(V) \
43 V(VariableDeclaration) \
44 V(FunctionDeclaration) \
45 V(ModuleDeclaration) \
46 V(ImportDeclaration) \
49 #define MODULE_NODE_LIST(V) \
54 #define STATEMENT_NODE_LIST(V) \
57 V(ExpressionStatement) \
60 V(ContinueStatement) \
70 V(TryCatchStatement) \
71 V(TryFinallyStatement) \
74 #define EXPRESSION_NODE_LIST(V) \
77 V(NativeFunctionLiteral) \
99 #define AST_NODE_LIST(V) \
100 DECLARATION_NODE_LIST(V) \
101 MODULE_NODE_LIST(V) \
102 STATEMENT_NODE_LIST(V) \
103 EXPRESSION_NODE_LIST(V)
105 // Forward declarations
106 class AstNodeFactory;
110 class BreakableStatement;
112 class IterationStatement;
113 class MaterializedLiteral;
115 class TypeFeedbackOracle;
117 class RegExpAlternative;
118 class RegExpAssertion;
120 class RegExpBackReference;
122 class RegExpCharacterClass;
123 class RegExpCompiler;
124 class RegExpDisjunction;
126 class RegExpLookahead;
127 class RegExpQuantifier;
130 #define DEF_FORWARD_DECLARATION(type) class type;
131 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
132 #undef DEF_FORWARD_DECLARATION
135 // Typedef only introduced to avoid unreadable code.
136 // Please do appreciate the required space in "> >".
137 typedef ZoneList<Handle<String> > ZoneStringList;
138 typedef ZoneList<Handle<Object> > ZoneObjectList;
141 #define DECLARE_NODE_TYPE(type) \
142 void Accept(AstVisitor* v) OVERRIDE; \
143 AstNode::NodeType node_type() const FINAL { return AstNode::k##type; } \
144 friend class AstNodeFactory;
147 enum AstPropertiesFlag {
154 class FeedbackVectorRequirements {
156 FeedbackVectorRequirements(int slots, int ic_slots)
157 : slots_(slots), ic_slots_(ic_slots) {}
159 int slots() const { return slots_; }
160 int ic_slots() const { return ic_slots_; }
168 class AstProperties FINAL BASE_EMBEDDED {
170 class Flags : public EnumSet<AstPropertiesFlag, int> {};
172 AstProperties() : node_count_(0) {}
174 Flags* flags() { return &flags_; }
175 int node_count() { return node_count_; }
176 void add_node_count(int count) { node_count_ += count; }
178 int slots() const { return spec_.slots(); }
179 void increase_slots(int count) { spec_.increase_slots(count); }
181 int ic_slots() const { return spec_.ic_slots(); }
182 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
183 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
184 const FeedbackVectorSpec& get_spec() const { return spec_; }
189 FeedbackVectorSpec spec_;
193 class AstNode: public ZoneObject {
195 #define DECLARE_TYPE_ENUM(type) k##type,
197 AST_NODE_LIST(DECLARE_TYPE_ENUM)
200 #undef DECLARE_TYPE_ENUM
202 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
204 explicit AstNode(int position): position_(position) {}
205 virtual ~AstNode() {}
207 virtual void Accept(AstVisitor* v) = 0;
208 virtual NodeType node_type() const = 0;
209 int position() const { return position_; }
211 // Type testing & conversion functions overridden by concrete subclasses.
212 #define DECLARE_NODE_FUNCTIONS(type) \
213 bool Is##type() const { return node_type() == AstNode::k##type; } \
215 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
217 const type* As##type() const { \
218 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
220 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
221 #undef DECLARE_NODE_FUNCTIONS
223 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
224 virtual IterationStatement* AsIterationStatement() { return NULL; }
225 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
227 // The interface for feedback slots, with default no-op implementations for
228 // node types which don't actually have this. Note that this is conceptually
229 // not really nice, but multiple inheritance would introduce yet another
230 // vtable entry per node, something we don't want for space reasons.
231 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
233 return FeedbackVectorRequirements(0, 0);
235 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
236 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) {
239 // Each ICSlot stores a kind of IC which the participating node should know.
240 virtual Code::Kind FeedbackICSlotKind(int index) {
242 return Code::NUMBER_OF_KINDS;
246 // Hidden to prevent accidental usage. It would have to load the
247 // current zone from the TLS.
248 void* operator new(size_t size);
250 friend class CaseClause; // Generates AST IDs.
256 class Statement : public AstNode {
258 explicit Statement(Zone* zone, int position) : AstNode(position) {}
260 bool IsEmpty() { return AsEmptyStatement() != NULL; }
261 virtual bool IsJump() const { return false; }
265 class SmallMapList FINAL {
268 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
270 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
271 void Clear() { list_.Clear(); }
272 void Sort() { list_.Sort(); }
274 bool is_empty() const { return list_.is_empty(); }
275 int length() const { return list_.length(); }
277 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
278 if (!Map::TryUpdate(map).ToHandle(&map)) return;
279 for (int i = 0; i < length(); ++i) {
280 if (at(i).is_identical_to(map)) return;
285 void FilterForPossibleTransitions(Map* root_map) {
286 for (int i = list_.length() - 1; i >= 0; i--) {
287 if (at(i)->FindRootMap() != root_map) {
288 list_.RemoveElement(list_.at(i));
293 void Add(Handle<Map> handle, Zone* zone) {
294 list_.Add(handle.location(), zone);
297 Handle<Map> at(int i) const {
298 return Handle<Map>(list_.at(i));
301 Handle<Map> first() const { return at(0); }
302 Handle<Map> last() const { return at(length() - 1); }
305 // The list stores pointers to Map*, that is Map**, so it's GC safe.
306 SmallPointerList<Map*> list_;
308 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
312 class Expression : public AstNode {
315 // Not assigned a context yet, or else will not be visited during
318 // Evaluated for its side effects.
320 // Evaluated for its value (and side effects).
322 // Evaluated for control flow (and side effects).
326 virtual bool IsValidReferenceExpression() const { return false; }
328 // Helpers for ToBoolean conversion.
329 virtual bool ToBooleanIsTrue() const { return false; }
330 virtual bool ToBooleanIsFalse() const { return false; }
332 // Symbols that cannot be parsed as array indices are considered property
333 // names. We do not treat symbols that can be array indexes as property
334 // names because [] for string objects is handled only by keyed ICs.
335 virtual bool IsPropertyName() const { return false; }
337 // True iff the expression is a literal represented as a smi.
338 bool IsSmiLiteral() const;
340 // True iff the expression is a string literal.
341 bool IsStringLiteral() const;
343 // True iff the expression is the null literal.
344 bool IsNullLiteral() const;
346 // True if we can prove that the expression is the undefined literal.
347 bool IsUndefinedLiteral(Isolate* isolate) const;
349 // Expression type bounds
350 Bounds bounds() const { return bounds_; }
351 void set_bounds(Bounds bounds) { bounds_ = bounds; }
353 // Whether the expression is parenthesized
354 bool is_parenthesized() const {
355 return IsParenthesizedField::decode(bit_field_);
357 bool is_multi_parenthesized() const {
358 return IsMultiParenthesizedField::decode(bit_field_);
360 void increase_parenthesization_level() {
362 IsMultiParenthesizedField::update(bit_field_, is_parenthesized());
363 bit_field_ = IsParenthesizedField::update(bit_field_, true);
366 // Type feedback information for assignments and properties.
367 virtual bool IsMonomorphic() {
371 virtual SmallMapList* GetReceiverTypes() {
375 virtual KeyedAccessStoreMode GetStoreMode() const {
377 return STANDARD_STORE;
379 virtual IcCheckType GetKeyType() const {
384 // TODO(rossberg): this should move to its own AST node eventually.
385 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
386 byte to_boolean_types() const {
387 return ToBooleanTypesField::decode(bit_field_);
390 void set_base_id(int id) { base_id_ = id; }
391 static int num_ids() { return parent_num_ids() + 2; }
392 BailoutId id() const { return BailoutId(local_id(0)); }
393 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
396 Expression(Zone* zone, int pos)
398 base_id_(BailoutId::None().ToInt()),
399 bounds_(Bounds::Unbounded(zone)),
401 static int parent_num_ids() { return 0; }
402 void set_to_boolean_types(byte types) {
403 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
406 int base_id() const {
407 DCHECK(!BailoutId(base_id_).IsNone());
412 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
416 class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
417 class IsParenthesizedField : public BitField16<bool, 8, 1> {};
418 class IsMultiParenthesizedField : public BitField16<bool, 9, 1> {};
420 // Ends with 16-bit field; deriving classes in turn begin with
421 // 16-bit fields for optimum packing efficiency.
425 class BreakableStatement : public Statement {
428 TARGET_FOR_ANONYMOUS,
429 TARGET_FOR_NAMED_ONLY
432 // The labels associated with this statement. May be NULL;
433 // if it is != NULL, guaranteed to contain at least one entry.
434 ZoneList<const AstRawString*>* labels() const { return labels_; }
436 // Type testing & conversion.
437 BreakableStatement* AsBreakableStatement() FINAL { return this; }
440 Label* break_target() { return &break_target_; }
443 bool is_target_for_anonymous() const {
444 return breakable_type_ == TARGET_FOR_ANONYMOUS;
447 void set_base_id(int id) { base_id_ = id; }
448 static int num_ids() { return parent_num_ids() + 2; }
449 BailoutId EntryId() const { return BailoutId(local_id(0)); }
450 BailoutId ExitId() const { return BailoutId(local_id(1)); }
453 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
454 BreakableType breakable_type, int position)
455 : Statement(zone, position),
457 breakable_type_(breakable_type),
458 base_id_(BailoutId::None().ToInt()) {
459 DCHECK(labels == NULL || labels->length() > 0);
461 static int parent_num_ids() { return 0; }
463 int base_id() const {
464 DCHECK(!BailoutId(base_id_).IsNone());
469 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
471 ZoneList<const AstRawString*>* labels_;
472 BreakableType breakable_type_;
478 class Block FINAL : public BreakableStatement {
480 DECLARE_NODE_TYPE(Block)
482 void AddStatement(Statement* statement, Zone* zone) {
483 statements_.Add(statement, zone);
486 ZoneList<Statement*>* statements() { return &statements_; }
487 bool is_initializer_block() const { return is_initializer_block_; }
489 static int num_ids() { return parent_num_ids() + 1; }
490 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
492 bool IsJump() const OVERRIDE {
493 return !statements_.is_empty() && statements_.last()->IsJump()
494 && labels() == NULL; // Good enough as an approximation...
497 Scope* scope() const { return scope_; }
498 void set_scope(Scope* scope) { scope_ = scope; }
501 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
502 bool is_initializer_block, int pos)
503 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
504 statements_(capacity, zone),
505 is_initializer_block_(is_initializer_block),
507 static int parent_num_ids() { return BreakableStatement::num_ids(); }
510 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
512 ZoneList<Statement*> statements_;
513 bool is_initializer_block_;
518 class Declaration : public AstNode {
520 VariableProxy* proxy() const { return proxy_; }
521 VariableMode mode() const { return mode_; }
522 Scope* scope() const { return scope_; }
523 virtual InitializationFlag initialization() const = 0;
524 virtual bool IsInlineable() const;
527 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
529 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
530 DCHECK(IsDeclaredVariableMode(mode));
535 VariableProxy* proxy_;
537 // Nested scope from which the declaration originated.
542 class VariableDeclaration FINAL : public Declaration {
544 DECLARE_NODE_TYPE(VariableDeclaration)
546 InitializationFlag initialization() const OVERRIDE {
547 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
551 VariableDeclaration(Zone* zone,
552 VariableProxy* proxy,
556 : Declaration(zone, proxy, mode, scope, pos) {
561 class FunctionDeclaration FINAL : public Declaration {
563 DECLARE_NODE_TYPE(FunctionDeclaration)
565 FunctionLiteral* fun() const { return fun_; }
566 InitializationFlag initialization() const OVERRIDE {
567 return kCreatedInitialized;
569 bool IsInlineable() const OVERRIDE;
572 FunctionDeclaration(Zone* zone,
573 VariableProxy* proxy,
575 FunctionLiteral* fun,
578 : Declaration(zone, proxy, mode, scope, pos),
580 DCHECK(mode == VAR || mode == LET || mode == CONST);
585 FunctionLiteral* fun_;
589 class ModuleDeclaration FINAL : public Declaration {
591 DECLARE_NODE_TYPE(ModuleDeclaration)
593 Module* module() const { return module_; }
594 InitializationFlag initialization() const OVERRIDE {
595 return kCreatedInitialized;
599 ModuleDeclaration(Zone* zone, VariableProxy* proxy, Module* module,
600 Scope* scope, int pos)
601 : Declaration(zone, proxy, CONST, scope, pos), module_(module) {}
608 class ImportDeclaration FINAL : public Declaration {
610 DECLARE_NODE_TYPE(ImportDeclaration)
612 const AstRawString* import_name() const { return import_name_; }
613 const AstRawString* module_specifier() const { return module_specifier_; }
614 void set_module_specifier(const AstRawString* module_specifier) {
615 DCHECK(module_specifier_ == NULL);
616 module_specifier_ = module_specifier;
618 InitializationFlag initialization() const OVERRIDE {
619 return kNeedsInitialization;
623 ImportDeclaration(Zone* zone, VariableProxy* proxy,
624 const AstRawString* import_name,
625 const AstRawString* module_specifier, Scope* scope, int pos)
626 : Declaration(zone, proxy, IMPORT, scope, pos),
627 import_name_(import_name),
628 module_specifier_(module_specifier) {}
631 const AstRawString* import_name_;
632 const AstRawString* module_specifier_;
636 class ExportDeclaration FINAL : public Declaration {
638 DECLARE_NODE_TYPE(ExportDeclaration)
640 InitializationFlag initialization() const OVERRIDE {
641 return kCreatedInitialized;
645 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
646 : Declaration(zone, proxy, LET, scope, pos) {}
650 class Module : public AstNode {
652 ModuleDescriptor* descriptor() const { return descriptor_; }
653 Block* body() const { return body_; }
656 Module(Zone* zone, int pos)
657 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
658 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
659 : AstNode(pos), descriptor_(descriptor), body_(body) {}
662 ModuleDescriptor* descriptor_;
667 class ModuleLiteral FINAL : public Module {
669 DECLARE_NODE_TYPE(ModuleLiteral)
672 ModuleLiteral(Zone* zone, Block* body, ModuleDescriptor* descriptor, int pos)
673 : Module(zone, descriptor, pos, body) {}
677 class ModulePath FINAL : public Module {
679 DECLARE_NODE_TYPE(ModulePath)
681 Module* module() const { return module_; }
682 Handle<String> name() const { return name_->string(); }
685 ModulePath(Zone* zone, Module* module, const AstRawString* name, int pos)
686 : Module(zone, pos), module_(module), name_(name) {}
690 const AstRawString* name_;
694 class ModuleUrl FINAL : public Module {
696 DECLARE_NODE_TYPE(ModuleUrl)
698 Handle<String> url() const { return url_; }
701 ModuleUrl(Zone* zone, Handle<String> url, int pos)
702 : Module(zone, pos), url_(url) {
710 class ModuleStatement FINAL : public Statement {
712 DECLARE_NODE_TYPE(ModuleStatement)
714 Block* body() const { return body_; }
717 ModuleStatement(Zone* zone, Block* body, int pos)
718 : Statement(zone, pos), body_(body) {}
725 class IterationStatement : public BreakableStatement {
727 // Type testing & conversion.
728 IterationStatement* AsIterationStatement() FINAL { return this; }
730 Statement* body() const { return body_; }
732 static int num_ids() { return parent_num_ids() + 1; }
733 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
734 virtual BailoutId ContinueId() const = 0;
735 virtual BailoutId StackCheckId() const = 0;
738 Label* continue_target() { return &continue_target_; }
741 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
742 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
744 static int parent_num_ids() { return BreakableStatement::num_ids(); }
745 void Initialize(Statement* body) { body_ = body; }
748 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
751 Label continue_target_;
755 class DoWhileStatement FINAL : public IterationStatement {
757 DECLARE_NODE_TYPE(DoWhileStatement)
759 void Initialize(Expression* cond, Statement* body) {
760 IterationStatement::Initialize(body);
764 Expression* cond() const { return cond_; }
766 static int num_ids() { return parent_num_ids() + 2; }
767 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
768 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
769 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
772 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
773 : IterationStatement(zone, labels, pos), cond_(NULL) {}
774 static int parent_num_ids() { return IterationStatement::num_ids(); }
777 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
783 class WhileStatement FINAL : public IterationStatement {
785 DECLARE_NODE_TYPE(WhileStatement)
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() + 1; }
795 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
796 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
797 BailoutId BodyId() const { return BailoutId(local_id(0)); }
800 WhileStatement(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 ForStatement FINAL : public IterationStatement {
813 DECLARE_NODE_TYPE(ForStatement)
815 void Initialize(Statement* init,
819 IterationStatement::Initialize(body);
825 Statement* init() const { return init_; }
826 Expression* cond() const { return cond_; }
827 Statement* next() const { return next_; }
829 static int num_ids() { return parent_num_ids() + 2; }
830 BailoutId ContinueId() const OVERRIDE { return BailoutId(local_id(0)); }
831 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
832 BailoutId BodyId() const { return BailoutId(local_id(1)); }
835 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
836 : IterationStatement(zone, labels, pos),
840 static int parent_num_ids() { return IterationStatement::num_ids(); }
843 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
851 class ForEachStatement : public IterationStatement {
854 ENUMERATE, // for (each in subject) body;
855 ITERATE // for (each of subject) body;
858 void Initialize(Expression* each, Expression* subject, Statement* body) {
859 IterationStatement::Initialize(body);
864 Expression* each() const { return each_; }
865 Expression* subject() const { return subject_; }
868 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
869 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
873 Expression* subject_;
877 class ForInStatement FINAL : public ForEachStatement {
879 DECLARE_NODE_TYPE(ForInStatement)
881 Expression* enumerable() const {
885 // Type feedback information.
886 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
887 Isolate* isolate) OVERRIDE {
888 return FeedbackVectorRequirements(1, 0);
890 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
891 for_in_feedback_slot_ = slot;
894 FeedbackVectorSlot ForInFeedbackSlot() {
895 DCHECK(!for_in_feedback_slot_.IsInvalid());
896 return for_in_feedback_slot_;
899 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
900 ForInType for_in_type() const { return for_in_type_; }
901 void set_for_in_type(ForInType type) { for_in_type_ = type; }
903 static int num_ids() { return parent_num_ids() + 5; }
904 BailoutId BodyId() const { return BailoutId(local_id(0)); }
905 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
906 BailoutId EnumId() const { return BailoutId(local_id(2)); }
907 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
908 BailoutId AssignmentId() const { return BailoutId(local_id(4)); }
909 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
910 BailoutId StackCheckId() const OVERRIDE { return BodyId(); }
913 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
914 : ForEachStatement(zone, labels, pos),
915 for_in_type_(SLOW_FOR_IN),
916 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
917 static int parent_num_ids() { return ForEachStatement::num_ids(); }
920 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
922 ForInType for_in_type_;
923 FeedbackVectorSlot for_in_feedback_slot_;
927 class ForOfStatement FINAL : public ForEachStatement {
929 DECLARE_NODE_TYPE(ForOfStatement)
931 void Initialize(Expression* each,
934 Expression* assign_iterator,
935 Expression* next_result,
936 Expression* result_done,
937 Expression* assign_each) {
938 ForEachStatement::Initialize(each, subject, body);
939 assign_iterator_ = assign_iterator;
940 next_result_ = next_result;
941 result_done_ = result_done;
942 assign_each_ = assign_each;
945 Expression* iterable() const {
949 // iterator = subject[Symbol.iterator]()
950 Expression* assign_iterator() const {
951 return assign_iterator_;
954 // result = iterator.next() // with type check
955 Expression* next_result() const {
960 Expression* result_done() const {
964 // each = result.value
965 Expression* assign_each() const {
969 BailoutId ContinueId() const OVERRIDE { return EntryId(); }
970 BailoutId StackCheckId() const OVERRIDE { return BackEdgeId(); }
972 static int num_ids() { return parent_num_ids() + 1; }
973 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
976 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
977 : ForEachStatement(zone, labels, pos),
978 assign_iterator_(NULL),
981 assign_each_(NULL) {}
982 static int parent_num_ids() { return ForEachStatement::num_ids(); }
985 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
987 Expression* assign_iterator_;
988 Expression* next_result_;
989 Expression* result_done_;
990 Expression* assign_each_;
994 class ExpressionStatement FINAL : public Statement {
996 DECLARE_NODE_TYPE(ExpressionStatement)
998 void set_expression(Expression* e) { expression_ = e; }
999 Expression* expression() const { return expression_; }
1000 bool IsJump() const OVERRIDE { return expression_->IsThrow(); }
1003 ExpressionStatement(Zone* zone, Expression* expression, int pos)
1004 : Statement(zone, pos), expression_(expression) { }
1007 Expression* expression_;
1011 class JumpStatement : public Statement {
1013 bool IsJump() const FINAL { return true; }
1016 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
1020 class ContinueStatement FINAL : public JumpStatement {
1022 DECLARE_NODE_TYPE(ContinueStatement)
1024 IterationStatement* target() const { return target_; }
1027 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
1028 : JumpStatement(zone, pos), target_(target) { }
1031 IterationStatement* target_;
1035 class BreakStatement FINAL : public JumpStatement {
1037 DECLARE_NODE_TYPE(BreakStatement)
1039 BreakableStatement* target() const { return target_; }
1042 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1043 : JumpStatement(zone, pos), target_(target) { }
1046 BreakableStatement* target_;
1050 class ReturnStatement FINAL : public JumpStatement {
1052 DECLARE_NODE_TYPE(ReturnStatement)
1054 Expression* expression() const { return expression_; }
1057 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1058 : JumpStatement(zone, pos), expression_(expression) { }
1061 Expression* expression_;
1065 class WithStatement FINAL : public Statement {
1067 DECLARE_NODE_TYPE(WithStatement)
1069 Scope* scope() { return scope_; }
1070 Expression* expression() const { return expression_; }
1071 Statement* statement() const { return statement_; }
1073 void set_base_id(int id) { base_id_ = id; }
1074 static int num_ids() { return parent_num_ids() + 1; }
1075 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1078 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1079 Statement* statement, int pos)
1080 : Statement(zone, pos),
1082 expression_(expression),
1083 statement_(statement),
1084 base_id_(BailoutId::None().ToInt()) {}
1085 static int parent_num_ids() { return 0; }
1087 int base_id() const {
1088 DCHECK(!BailoutId(base_id_).IsNone());
1093 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1096 Expression* expression_;
1097 Statement* statement_;
1102 class CaseClause FINAL : public Expression {
1104 DECLARE_NODE_TYPE(CaseClause)
1106 bool is_default() const { return label_ == NULL; }
1107 Expression* label() const {
1108 CHECK(!is_default());
1111 Label* body_target() { return &body_target_; }
1112 ZoneList<Statement*>* statements() const { return statements_; }
1114 static int num_ids() { return parent_num_ids() + 2; }
1115 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1116 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1118 Type* compare_type() { return compare_type_; }
1119 void set_compare_type(Type* type) { compare_type_ = type; }
1122 static int parent_num_ids() { return Expression::num_ids(); }
1125 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1127 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1131 ZoneList<Statement*>* statements_;
1132 Type* compare_type_;
1136 class SwitchStatement FINAL : public BreakableStatement {
1138 DECLARE_NODE_TYPE(SwitchStatement)
1140 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1145 Expression* tag() const { return tag_; }
1146 ZoneList<CaseClause*>* cases() const { return cases_; }
1149 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1150 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1156 ZoneList<CaseClause*>* cases_;
1160 // If-statements always have non-null references to their then- and
1161 // else-parts. When parsing if-statements with no explicit else-part,
1162 // the parser implicitly creates an empty statement. Use the
1163 // HasThenStatement() and HasElseStatement() functions to check if a
1164 // given if-statement has a then- or an else-part containing code.
1165 class IfStatement FINAL : public Statement {
1167 DECLARE_NODE_TYPE(IfStatement)
1169 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1170 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1172 Expression* condition() const { return condition_; }
1173 Statement* then_statement() const { return then_statement_; }
1174 Statement* else_statement() const { return else_statement_; }
1176 bool IsJump() const OVERRIDE {
1177 return HasThenStatement() && then_statement()->IsJump()
1178 && HasElseStatement() && else_statement()->IsJump();
1181 void set_base_id(int id) { base_id_ = id; }
1182 static int num_ids() { return parent_num_ids() + 3; }
1183 BailoutId IfId() const { return BailoutId(local_id(0)); }
1184 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1185 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1188 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1189 Statement* else_statement, int pos)
1190 : Statement(zone, pos),
1191 condition_(condition),
1192 then_statement_(then_statement),
1193 else_statement_(else_statement),
1194 base_id_(BailoutId::None().ToInt()) {}
1195 static int parent_num_ids() { return 0; }
1197 int base_id() const {
1198 DCHECK(!BailoutId(base_id_).IsNone());
1203 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1205 Expression* condition_;
1206 Statement* then_statement_;
1207 Statement* else_statement_;
1212 class TryStatement : public Statement {
1214 int index() const { return index_; }
1215 Block* try_block() const { return try_block_; }
1218 TryStatement(Zone* zone, int index, Block* try_block, int pos)
1219 : Statement(zone, pos), index_(index), try_block_(try_block) {}
1222 // Unique (per-function) index of this handler. This is not an AST ID.
1229 class TryCatchStatement FINAL : public TryStatement {
1231 DECLARE_NODE_TYPE(TryCatchStatement)
1233 Scope* scope() { return scope_; }
1234 Variable* variable() { return variable_; }
1235 Block* catch_block() const { return catch_block_; }
1238 TryCatchStatement(Zone* zone,
1245 : TryStatement(zone, index, try_block, pos),
1247 variable_(variable),
1248 catch_block_(catch_block) {
1253 Variable* variable_;
1254 Block* catch_block_;
1258 class TryFinallyStatement FINAL : public TryStatement {
1260 DECLARE_NODE_TYPE(TryFinallyStatement)
1262 Block* finally_block() const { return finally_block_; }
1265 TryFinallyStatement(
1266 Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1267 : TryStatement(zone, index, try_block, pos),
1268 finally_block_(finally_block) { }
1271 Block* finally_block_;
1275 class DebuggerStatement FINAL : public Statement {
1277 DECLARE_NODE_TYPE(DebuggerStatement)
1279 void set_base_id(int id) { base_id_ = id; }
1280 static int num_ids() { return parent_num_ids() + 1; }
1281 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1284 explicit DebuggerStatement(Zone* zone, int pos)
1285 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1286 static int parent_num_ids() { return 0; }
1288 int base_id() const {
1289 DCHECK(!BailoutId(base_id_).IsNone());
1294 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1300 class EmptyStatement FINAL : public Statement {
1302 DECLARE_NODE_TYPE(EmptyStatement)
1305 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1309 class Literal FINAL : public Expression {
1311 DECLARE_NODE_TYPE(Literal)
1313 bool IsPropertyName() const OVERRIDE { return value_->IsPropertyName(); }
1315 Handle<String> AsPropertyName() {
1316 DCHECK(IsPropertyName());
1317 return Handle<String>::cast(value());
1320 const AstRawString* AsRawPropertyName() {
1321 DCHECK(IsPropertyName());
1322 return value_->AsString();
1325 bool ToBooleanIsTrue() const OVERRIDE { return value()->BooleanValue(); }
1326 bool ToBooleanIsFalse() const OVERRIDE { return !value()->BooleanValue(); }
1328 Handle<Object> value() const { return value_->value(); }
1329 const AstValue* raw_value() const { return value_; }
1331 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1332 // only for string and number literals!
1334 static bool Match(void* literal1, void* literal2);
1336 static int num_ids() { return parent_num_ids() + 1; }
1337 TypeFeedbackId LiteralFeedbackId() const {
1338 return TypeFeedbackId(local_id(0));
1342 Literal(Zone* zone, const AstValue* value, int position)
1343 : Expression(zone, position), value_(value) {}
1344 static int parent_num_ids() { return Expression::num_ids(); }
1347 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1349 const AstValue* value_;
1353 // Base class for literals that needs space in the corresponding JSFunction.
1354 class MaterializedLiteral : public Expression {
1356 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1358 int literal_index() { return literal_index_; }
1361 // only callable after initialization.
1362 DCHECK(depth_ >= 1);
1367 MaterializedLiteral(Zone* zone, int literal_index, int pos)
1368 : Expression(zone, pos),
1369 literal_index_(literal_index),
1373 // A materialized literal is simple if the values consist of only
1374 // constants and simple object and array literals.
1375 bool is_simple() const { return is_simple_; }
1376 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1377 friend class CompileTimeValue;
1379 void set_depth(int depth) {
1384 // Populate the constant properties/elements fixed array.
1385 void BuildConstants(Isolate* isolate);
1386 friend class ArrayLiteral;
1387 friend class ObjectLiteral;
1389 // If the expression is a literal, return the literal value;
1390 // if the expression is a materialized literal and is simple return a
1391 // compile time value as encoded by CompileTimeValue::GetValue().
1392 // Otherwise, return undefined literal as the placeholder
1393 // in the object literal boilerplate.
1394 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1403 // Property is used for passing information
1404 // about an object literal's properties from the parser
1405 // to the code generator.
1406 class ObjectLiteralProperty FINAL : public ZoneObject {
1409 CONSTANT, // Property with constant value (compile time).
1410 COMPUTED, // Property with computed value (execution time).
1411 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1412 GETTER, SETTER, // Property is an accessor function.
1413 PROTOTYPE // Property is __proto__.
1416 Expression* key() { return key_; }
1417 Expression* value() { return value_; }
1418 Kind kind() { return kind_; }
1420 // Type feedback information.
1421 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1422 Handle<Map> GetReceiverType() { return receiver_type_; }
1424 bool IsCompileTimeValue();
1426 void set_emit_store(bool emit_store);
1429 bool is_static() const { return is_static_; }
1430 bool is_computed_name() const { return is_computed_name_; }
1432 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1435 friend class AstNodeFactory;
1437 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1438 bool is_static, bool is_computed_name);
1439 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1440 Expression* value, bool is_static,
1441 bool is_computed_name);
1449 bool is_computed_name_;
1450 Handle<Map> receiver_type_;
1454 // An object literal has a boilerplate object that is used
1455 // for minimizing the work when constructing it at runtime.
1456 class ObjectLiteral FINAL : public MaterializedLiteral {
1458 typedef ObjectLiteralProperty Property;
1460 DECLARE_NODE_TYPE(ObjectLiteral)
1462 Handle<FixedArray> constant_properties() const {
1463 return constant_properties_;
1465 ZoneList<Property*>* properties() const { return properties_; }
1466 bool fast_elements() const { return fast_elements_; }
1467 bool may_store_doubles() const { return may_store_doubles_; }
1468 bool has_function() const { return has_function_; }
1470 // Decide if a property should be in the object boilerplate.
1471 static bool IsBoilerplateProperty(Property* property);
1473 // Populate the constant properties fixed array.
1474 void BuildConstantProperties(Isolate* isolate);
1476 // Mark all computed expressions that are bound to a key that
1477 // is shadowed by a later occurrence of the same key. For the
1478 // marked expressions, no store code is emitted.
1479 void CalculateEmitStore(Zone* zone);
1481 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1482 int ComputeFlags() const {
1483 int flags = fast_elements() ? kFastElements : kNoFlags;
1484 flags |= has_function() ? kHasFunction : kNoFlags;
1491 kHasFunction = 1 << 1
1494 struct Accessors: public ZoneObject {
1495 Accessors() : getter(NULL), setter(NULL) {}
1500 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1502 // Return an AST id for a property that is used in simulate instructions.
1503 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1505 // Unlike other AST nodes, this number of bailout IDs allocated for an
1506 // ObjectLiteral can vary, so num_ids() is not a static method.
1507 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1510 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1511 int boilerplate_properties, bool has_function, int pos)
1512 : MaterializedLiteral(zone, literal_index, pos),
1513 properties_(properties),
1514 boilerplate_properties_(boilerplate_properties),
1515 fast_elements_(false),
1516 may_store_doubles_(false),
1517 has_function_(has_function) {}
1518 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1521 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1522 Handle<FixedArray> constant_properties_;
1523 ZoneList<Property*>* properties_;
1524 int boilerplate_properties_;
1525 bool fast_elements_;
1526 bool may_store_doubles_;
1531 // Node for capturing a regexp literal.
1532 class RegExpLiteral FINAL : public MaterializedLiteral {
1534 DECLARE_NODE_TYPE(RegExpLiteral)
1536 Handle<String> pattern() const { return pattern_->string(); }
1537 Handle<String> flags() const { return flags_->string(); }
1540 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1541 const AstRawString* flags, int literal_index, int pos)
1542 : MaterializedLiteral(zone, literal_index, pos),
1549 const AstRawString* pattern_;
1550 const AstRawString* flags_;
1554 // An array literal has a literals object that is used
1555 // for minimizing the work when constructing it at runtime.
1556 class ArrayLiteral FINAL : public MaterializedLiteral {
1558 DECLARE_NODE_TYPE(ArrayLiteral)
1560 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1561 ZoneList<Expression*>* values() const { return values_; }
1563 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1565 // Return an AST id for an element that is used in simulate instructions.
1566 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1568 // Unlike other AST nodes, this number of bailout IDs allocated for an
1569 // ArrayLiteral can vary, so num_ids() is not a static method.
1570 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1572 // Populate the constant elements fixed array.
1573 void BuildConstantElements(Isolate* isolate);
1575 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1576 int ComputeFlags() const {
1577 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1578 flags |= ArrayLiteral::kDisableMementos;
1584 kShallowElements = 1,
1585 kDisableMementos = 1 << 1
1589 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1591 : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1592 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1595 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1597 Handle<FixedArray> constant_elements_;
1598 ZoneList<Expression*>* values_;
1602 class VariableProxy FINAL : public Expression {
1604 DECLARE_NODE_TYPE(VariableProxy)
1606 bool IsValidReferenceExpression() const OVERRIDE {
1607 return !is_resolved() || var()->IsValidReference();
1610 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1612 Handle<String> name() const { return raw_name()->string(); }
1613 const AstRawString* raw_name() const {
1614 return is_resolved() ? var_->raw_name() : raw_name_;
1617 Variable* var() const {
1618 DCHECK(is_resolved());
1621 void set_var(Variable* v) {
1622 DCHECK(!is_resolved());
1627 bool is_this() const { return IsThisField::decode(bit_field_); }
1629 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1630 void set_is_assigned() {
1631 bit_field_ = IsAssignedField::update(bit_field_, true);
1634 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1635 void set_is_resolved() {
1636 bit_field_ = IsResolvedField::update(bit_field_, true);
1639 int end_position() const { return end_position_; }
1641 // Bind this proxy to the variable var.
1642 void BindTo(Variable* var);
1644 bool UsesVariableFeedbackSlot() const {
1645 return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1648 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1649 Isolate* isolate) OVERRIDE {
1650 return FeedbackVectorRequirements(0, UsesVariableFeedbackSlot() ? 1 : 0);
1653 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1654 variable_feedback_slot_ = slot;
1656 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1657 FeedbackVectorICSlot VariableFeedbackSlot() {
1658 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1659 return variable_feedback_slot_;
1663 VariableProxy(Zone* zone, Variable* var, int start_position,
1666 VariableProxy(Zone* zone, const AstRawString* name, bool is_this,
1667 int start_position, int end_position);
1669 class IsThisField : public BitField8<bool, 0, 1> {};
1670 class IsAssignedField : public BitField8<bool, 1, 1> {};
1671 class IsResolvedField : public BitField8<bool, 2, 1> {};
1673 // Start with 16-bit (or smaller) field, which should get packed together
1674 // with Expression's trailing 16-bit field.
1676 FeedbackVectorICSlot variable_feedback_slot_;
1678 const AstRawString* raw_name_; // if !is_resolved_
1679 Variable* var_; // if is_resolved_
1681 // Position is stored in the AstNode superclass, but VariableProxy needs to
1682 // know its end position too (for error messages). It cannot be inferred from
1683 // the variable name length because it can contain escapes.
1688 class Property FINAL : public Expression {
1690 DECLARE_NODE_TYPE(Property)
1692 bool IsValidReferenceExpression() const OVERRIDE { return true; }
1694 Expression* obj() const { return obj_; }
1695 Expression* key() const { return key_; }
1697 static int num_ids() { return parent_num_ids() + 2; }
1698 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1699 TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1701 bool IsStringAccess() const {
1702 return IsStringAccessField::decode(bit_field_);
1705 // Type feedback information.
1706 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
1707 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
1708 KeyedAccessStoreMode GetStoreMode() const OVERRIDE { return STANDARD_STORE; }
1709 IcCheckType GetKeyType() const OVERRIDE {
1710 return KeyTypeField::decode(bit_field_);
1712 bool IsUninitialized() const {
1713 return !is_for_call() && HasNoTypeInformation();
1715 bool HasNoTypeInformation() const {
1716 return IsUninitializedField::decode(bit_field_);
1718 void set_is_uninitialized(bool b) {
1719 bit_field_ = IsUninitializedField::update(bit_field_, b);
1721 void set_is_string_access(bool b) {
1722 bit_field_ = IsStringAccessField::update(bit_field_, b);
1724 void set_key_type(IcCheckType key_type) {
1725 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1727 void mark_for_call() {
1728 bit_field_ = IsForCallField::update(bit_field_, true);
1730 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1732 bool IsSuperAccess() {
1733 return obj()->IsSuperReference();
1736 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1737 Isolate* isolate) OVERRIDE {
1738 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1740 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1741 property_feedback_slot_ = slot;
1743 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
1744 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1747 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1748 DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1749 return property_feedback_slot_;
1753 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1754 : Expression(zone, pos),
1755 bit_field_(IsForCallField::encode(false) |
1756 IsUninitializedField::encode(false) |
1757 IsStringAccessField::encode(false)),
1758 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1761 static int parent_num_ids() { return Expression::num_ids(); }
1764 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1766 class IsForCallField : public BitField8<bool, 0, 1> {};
1767 class IsUninitializedField : public BitField8<bool, 1, 1> {};
1768 class IsStringAccessField : public BitField8<bool, 2, 1> {};
1769 class KeyTypeField : public BitField8<IcCheckType, 3, 1> {};
1771 FeedbackVectorICSlot property_feedback_slot_;
1774 SmallMapList receiver_types_;
1778 class Call FINAL : public Expression {
1780 DECLARE_NODE_TYPE(Call)
1782 Expression* expression() const { return expression_; }
1783 ZoneList<Expression*>* arguments() const { return arguments_; }
1785 // Type feedback information.
1786 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1787 Isolate* isolate) OVERRIDE;
1788 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1789 ic_slot_or_slot_ = slot.ToInt();
1791 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1792 ic_slot_or_slot_ = slot.ToInt();
1794 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::CALL_IC; }
1796 FeedbackVectorSlot CallFeedbackSlot() const {
1797 DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1798 return FeedbackVectorSlot(ic_slot_or_slot_);
1801 FeedbackVectorICSlot CallFeedbackICSlot() const {
1802 DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1803 return FeedbackVectorICSlot(ic_slot_or_slot_);
1806 SmallMapList* GetReceiverTypes() OVERRIDE {
1807 if (expression()->IsProperty()) {
1808 return expression()->AsProperty()->GetReceiverTypes();
1813 bool IsMonomorphic() OVERRIDE {
1814 if (expression()->IsProperty()) {
1815 return expression()->AsProperty()->IsMonomorphic();
1817 return !target_.is_null();
1820 bool global_call() const {
1821 VariableProxy* proxy = expression_->AsVariableProxy();
1822 return proxy != NULL && proxy->var()->IsUnallocated();
1825 bool known_global_function() const {
1826 return global_call() && !target_.is_null();
1829 Handle<JSFunction> target() { return target_; }
1831 Handle<Cell> cell() { return cell_; }
1833 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1835 void set_target(Handle<JSFunction> target) { target_ = target; }
1836 void set_allocation_site(Handle<AllocationSite> site) {
1837 allocation_site_ = site;
1839 bool ComputeGlobalTarget(Handle<GlobalObject> global, LookupIterator* it);
1841 static int num_ids() { return parent_num_ids() + 2; }
1842 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1843 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1845 bool is_uninitialized() const {
1846 return IsUninitializedField::decode(bit_field_);
1848 void set_is_uninitialized(bool b) {
1849 bit_field_ = IsUninitializedField::update(bit_field_, b);
1861 // Helpers to determine how to handle the call.
1862 CallType GetCallType(Isolate* isolate) const;
1863 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1864 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1867 // Used to assert that the FullCodeGenerator records the return site.
1868 bool return_is_recorded_;
1872 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1874 : Expression(zone, pos),
1875 ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1876 expression_(expression),
1877 arguments_(arguments),
1878 bit_field_(IsUninitializedField::encode(false)) {
1879 if (expression->IsProperty()) {
1880 expression->AsProperty()->mark_for_call();
1883 static int parent_num_ids() { return Expression::num_ids(); }
1886 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1888 // We store this as an integer because we don't know if we have a slot or
1889 // an ic slot until scoping time.
1890 int ic_slot_or_slot_;
1891 Expression* expression_;
1892 ZoneList<Expression*>* arguments_;
1893 Handle<JSFunction> target_;
1895 Handle<AllocationSite> allocation_site_;
1896 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1901 class CallNew FINAL : public Expression {
1903 DECLARE_NODE_TYPE(CallNew)
1905 Expression* expression() const { return expression_; }
1906 ZoneList<Expression*>* arguments() const { return arguments_; }
1908 // Type feedback information.
1909 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1910 Isolate* isolate) OVERRIDE {
1911 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1913 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) OVERRIDE {
1914 callnew_feedback_slot_ = slot;
1917 FeedbackVectorSlot CallNewFeedbackSlot() {
1918 DCHECK(!callnew_feedback_slot_.IsInvalid());
1919 return callnew_feedback_slot_;
1921 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1922 DCHECK(FLAG_pretenuring_call_new);
1923 return CallNewFeedbackSlot().next();
1926 bool IsMonomorphic() OVERRIDE { return is_monomorphic_; }
1927 Handle<JSFunction> target() const { return target_; }
1928 Handle<AllocationSite> allocation_site() const {
1929 return allocation_site_;
1932 static int num_ids() { return parent_num_ids() + 1; }
1933 static int feedback_slots() { return 1; }
1934 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1936 void set_allocation_site(Handle<AllocationSite> site) {
1937 allocation_site_ = site;
1939 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1940 void set_target(Handle<JSFunction> target) { target_ = target; }
1943 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1945 : Expression(zone, pos),
1946 expression_(expression),
1947 arguments_(arguments),
1948 is_monomorphic_(false),
1949 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1951 static int parent_num_ids() { return Expression::num_ids(); }
1954 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1956 Expression* expression_;
1957 ZoneList<Expression*>* arguments_;
1958 bool is_monomorphic_;
1959 Handle<JSFunction> target_;
1960 Handle<AllocationSite> allocation_site_;
1961 FeedbackVectorSlot callnew_feedback_slot_;
1965 // The CallRuntime class does not represent any official JavaScript
1966 // language construct. Instead it is used to call a C or JS function
1967 // with a set of arguments. This is used from the builtins that are
1968 // implemented in JavaScript (see "v8natives.js").
1969 class CallRuntime FINAL : public Expression {
1971 DECLARE_NODE_TYPE(CallRuntime)
1973 Handle<String> name() const { return raw_name_->string(); }
1974 const AstRawString* raw_name() const { return raw_name_; }
1975 const Runtime::Function* function() const { return function_; }
1976 ZoneList<Expression*>* arguments() const { return arguments_; }
1977 bool is_jsruntime() const { return function_ == NULL; }
1979 // Type feedback information.
1980 bool HasCallRuntimeFeedbackSlot() const {
1981 return FLAG_vector_ics && is_jsruntime();
1983 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1984 Isolate* isolate) OVERRIDE {
1985 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
1987 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
1988 callruntime_feedback_slot_ = slot;
1990 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
1992 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
1993 DCHECK(!HasCallRuntimeFeedbackSlot() ||
1994 !callruntime_feedback_slot_.IsInvalid());
1995 return callruntime_feedback_slot_;
1998 static int num_ids() { return parent_num_ids() + 1; }
1999 TypeFeedbackId CallRuntimeFeedbackId() const {
2000 return TypeFeedbackId(local_id(0));
2004 CallRuntime(Zone* zone, const AstRawString* name,
2005 const Runtime::Function* function,
2006 ZoneList<Expression*>* arguments, int pos)
2007 : Expression(zone, pos),
2009 function_(function),
2010 arguments_(arguments),
2011 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2012 static int parent_num_ids() { return Expression::num_ids(); }
2015 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2017 const AstRawString* raw_name_;
2018 const Runtime::Function* function_;
2019 ZoneList<Expression*>* arguments_;
2020 FeedbackVectorICSlot callruntime_feedback_slot_;
2024 class UnaryOperation FINAL : public Expression {
2026 DECLARE_NODE_TYPE(UnaryOperation)
2028 Token::Value op() const { return op_; }
2029 Expression* expression() const { return expression_; }
2031 // For unary not (Token::NOT), the AST ids where true and false will
2032 // actually be materialized, respectively.
2033 static int num_ids() { return parent_num_ids() + 2; }
2034 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2035 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2037 virtual void RecordToBooleanTypeFeedback(
2038 TypeFeedbackOracle* oracle) OVERRIDE;
2041 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2042 : Expression(zone, pos), op_(op), expression_(expression) {
2043 DCHECK(Token::IsUnaryOp(op));
2045 static int parent_num_ids() { return Expression::num_ids(); }
2048 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2051 Expression* expression_;
2055 class BinaryOperation FINAL : public Expression {
2057 DECLARE_NODE_TYPE(BinaryOperation)
2059 Token::Value op() const { return static_cast<Token::Value>(op_); }
2060 Expression* left() const { return left_; }
2061 Expression* right() const { return right_; }
2062 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2063 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2064 allocation_site_ = allocation_site;
2067 // The short-circuit logical operations need an AST ID for their
2068 // right-hand subexpression.
2069 static int num_ids() { return parent_num_ids() + 2; }
2070 BailoutId RightId() const { return BailoutId(local_id(0)); }
2072 TypeFeedbackId BinaryOperationFeedbackId() const {
2073 return TypeFeedbackId(local_id(1));
2075 Maybe<int> fixed_right_arg() const {
2076 return has_fixed_right_arg_ ? Maybe<int>(fixed_right_arg_value_)
2079 void set_fixed_right_arg(Maybe<int> arg) {
2080 has_fixed_right_arg_ = arg.has_value;
2081 if (arg.has_value) fixed_right_arg_value_ = arg.value;
2084 virtual void RecordToBooleanTypeFeedback(
2085 TypeFeedbackOracle* oracle) OVERRIDE;
2088 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2089 Expression* right, int pos)
2090 : Expression(zone, pos),
2091 op_(static_cast<byte>(op)),
2092 has_fixed_right_arg_(false),
2093 fixed_right_arg_value_(0),
2096 DCHECK(Token::IsBinaryOp(op));
2098 static int parent_num_ids() { return Expression::num_ids(); }
2101 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2103 const byte op_; // actually Token::Value
2104 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2105 // type for the RHS. Currenty it's actually a Maybe<int>
2106 bool has_fixed_right_arg_;
2107 int fixed_right_arg_value_;
2110 Handle<AllocationSite> allocation_site_;
2114 class CountOperation FINAL : public Expression {
2116 DECLARE_NODE_TYPE(CountOperation)
2118 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2119 bool is_postfix() const { return !is_prefix(); }
2121 Token::Value op() const { return TokenField::decode(bit_field_); }
2122 Token::Value binary_op() {
2123 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2126 Expression* expression() const { return expression_; }
2128 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2129 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2130 IcCheckType GetKeyType() const OVERRIDE {
2131 return KeyTypeField::decode(bit_field_);
2133 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2134 return StoreModeField::decode(bit_field_);
2136 Type* type() const { return type_; }
2137 void set_key_type(IcCheckType type) {
2138 bit_field_ = KeyTypeField::update(bit_field_, type);
2140 void set_store_mode(KeyedAccessStoreMode mode) {
2141 bit_field_ = StoreModeField::update(bit_field_, mode);
2143 void set_type(Type* type) { type_ = type; }
2145 static int num_ids() { return parent_num_ids() + 4; }
2146 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2147 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2148 TypeFeedbackId CountBinOpFeedbackId() const {
2149 return TypeFeedbackId(local_id(2));
2151 TypeFeedbackId CountStoreFeedbackId() const {
2152 return TypeFeedbackId(local_id(3));
2156 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2158 : Expression(zone, pos),
2159 bit_field_(IsPrefixField::encode(is_prefix) |
2160 KeyTypeField::encode(ELEMENT) |
2161 StoreModeField::encode(STANDARD_STORE) |
2162 TokenField::encode(op)),
2164 expression_(expr) {}
2165 static int parent_num_ids() { return Expression::num_ids(); }
2168 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2170 class IsPrefixField : public BitField16<bool, 0, 1> {};
2171 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2172 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2173 class TokenField : public BitField16<Token::Value, 6, 8> {};
2175 // Starts with 16-bit field, which should get packed together with
2176 // Expression's trailing 16-bit field.
2177 uint16_t bit_field_;
2179 Expression* expression_;
2180 SmallMapList receiver_types_;
2184 class CompareOperation FINAL : public Expression {
2186 DECLARE_NODE_TYPE(CompareOperation)
2188 Token::Value op() const { return op_; }
2189 Expression* left() const { return left_; }
2190 Expression* right() const { return right_; }
2192 // Type feedback information.
2193 static int num_ids() { return parent_num_ids() + 1; }
2194 TypeFeedbackId CompareOperationFeedbackId() const {
2195 return TypeFeedbackId(local_id(0));
2197 Type* combined_type() const { return combined_type_; }
2198 void set_combined_type(Type* type) { combined_type_ = type; }
2200 // Match special cases.
2201 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2202 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2203 bool IsLiteralCompareNull(Expression** expr);
2206 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2207 Expression* right, int pos)
2208 : Expression(zone, pos),
2212 combined_type_(Type::None(zone)) {
2213 DCHECK(Token::IsCompareOp(op));
2215 static int parent_num_ids() { return Expression::num_ids(); }
2218 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2224 Type* combined_type_;
2228 class Conditional FINAL : public Expression {
2230 DECLARE_NODE_TYPE(Conditional)
2232 Expression* condition() const { return condition_; }
2233 Expression* then_expression() const { return then_expression_; }
2234 Expression* else_expression() const { return else_expression_; }
2236 static int num_ids() { return parent_num_ids() + 2; }
2237 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2238 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2241 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2242 Expression* else_expression, int position)
2243 : Expression(zone, position),
2244 condition_(condition),
2245 then_expression_(then_expression),
2246 else_expression_(else_expression) {}
2247 static int parent_num_ids() { return Expression::num_ids(); }
2250 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2252 Expression* condition_;
2253 Expression* then_expression_;
2254 Expression* else_expression_;
2258 class Assignment FINAL : public Expression {
2260 DECLARE_NODE_TYPE(Assignment)
2262 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2264 Token::Value binary_op() const;
2266 Token::Value op() const { return TokenField::decode(bit_field_); }
2267 Expression* target() const { return target_; }
2268 Expression* value() const { return value_; }
2269 BinaryOperation* binary_operation() const { return binary_operation_; }
2271 // This check relies on the definition order of token in token.h.
2272 bool is_compound() const { return op() > Token::ASSIGN; }
2274 static int num_ids() { return parent_num_ids() + 2; }
2275 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2277 // Type feedback information.
2278 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2279 bool IsMonomorphic() OVERRIDE { return receiver_types_.length() == 1; }
2280 bool IsUninitialized() const {
2281 return IsUninitializedField::decode(bit_field_);
2283 bool HasNoTypeInformation() {
2284 return IsUninitializedField::decode(bit_field_);
2286 SmallMapList* GetReceiverTypes() OVERRIDE { return &receiver_types_; }
2287 IcCheckType GetKeyType() const OVERRIDE {
2288 return KeyTypeField::decode(bit_field_);
2290 KeyedAccessStoreMode GetStoreMode() const OVERRIDE {
2291 return StoreModeField::decode(bit_field_);
2293 void set_is_uninitialized(bool b) {
2294 bit_field_ = IsUninitializedField::update(bit_field_, b);
2296 void set_key_type(IcCheckType key_type) {
2297 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2299 void set_store_mode(KeyedAccessStoreMode mode) {
2300 bit_field_ = StoreModeField::update(bit_field_, mode);
2304 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2306 static int parent_num_ids() { return Expression::num_ids(); }
2309 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2311 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2312 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2313 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2314 class TokenField : public BitField16<Token::Value, 6, 8> {};
2316 // Starts with 16-bit field, which should get packed together with
2317 // Expression's trailing 16-bit field.
2318 uint16_t bit_field_;
2319 Expression* target_;
2321 BinaryOperation* binary_operation_;
2322 SmallMapList receiver_types_;
2326 class Yield FINAL : public Expression {
2328 DECLARE_NODE_TYPE(Yield)
2331 kInitial, // The initial yield that returns the unboxed generator object.
2332 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2333 kDelegating, // A yield*.
2334 kFinal // A return: { value: EXPRESSION, done: true }
2337 Expression* generator_object() const { return generator_object_; }
2338 Expression* expression() const { return expression_; }
2339 Kind yield_kind() const { return yield_kind_; }
2341 // Delegating yield surrounds the "yield" in a "try/catch". This index
2342 // locates the catch handler in the handler table, and is equivalent to
2343 // TryCatchStatement::index().
2345 DCHECK_EQ(kDelegating, yield_kind());
2348 void set_index(int index) {
2349 DCHECK_EQ(kDelegating, yield_kind());
2353 // Type feedback information.
2354 bool HasFeedbackSlots() const {
2355 return FLAG_vector_ics && (yield_kind() == kDelegating);
2357 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2358 Isolate* isolate) OVERRIDE {
2359 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2361 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2362 yield_first_feedback_slot_ = slot;
2364 Code::Kind FeedbackICSlotKind(int index) OVERRIDE {
2365 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2368 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2369 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2370 return yield_first_feedback_slot_;
2373 FeedbackVectorICSlot DoneFeedbackSlot() {
2374 return KeyedLoadFeedbackSlot().next();
2377 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2380 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2381 Kind yield_kind, int pos)
2382 : Expression(zone, pos),
2383 generator_object_(generator_object),
2384 expression_(expression),
2385 yield_kind_(yield_kind),
2387 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2390 Expression* generator_object_;
2391 Expression* expression_;
2394 FeedbackVectorICSlot yield_first_feedback_slot_;
2398 class Throw FINAL : public Expression {
2400 DECLARE_NODE_TYPE(Throw)
2402 Expression* exception() const { return exception_; }
2405 Throw(Zone* zone, Expression* exception, int pos)
2406 : Expression(zone, pos), exception_(exception) {}
2409 Expression* exception_;
2413 class FunctionLiteral FINAL : public Expression {
2416 ANONYMOUS_EXPRESSION,
2421 enum ParameterFlag {
2422 kNoDuplicateParameters = 0,
2423 kHasDuplicateParameters = 1
2426 enum IsFunctionFlag {
2431 enum IsParenthesizedFlag {
2436 enum ArityRestriction {
2442 DECLARE_NODE_TYPE(FunctionLiteral)
2444 Handle<String> name() const { return raw_name_->string(); }
2445 const AstRawString* raw_name() const { return raw_name_; }
2446 Scope* scope() const { return scope_; }
2447 ZoneList<Statement*>* body() const { return body_; }
2448 void set_function_token_position(int pos) { function_token_position_ = pos; }
2449 int function_token_position() const { return function_token_position_; }
2450 int start_position() const;
2451 int end_position() const;
2452 int SourceSize() const { return end_position() - start_position(); }
2453 bool is_expression() const { return IsExpression::decode(bitfield_); }
2454 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2455 LanguageMode language_mode() const;
2456 bool uses_super_property() const;
2458 static bool NeedsHomeObject(Expression* literal) {
2459 return literal != NULL && literal->IsFunctionLiteral() &&
2460 literal->AsFunctionLiteral()->uses_super_property();
2463 int materialized_literal_count() { return materialized_literal_count_; }
2464 int expected_property_count() { return expected_property_count_; }
2465 int handler_count() { return handler_count_; }
2466 int parameter_count() { return parameter_count_; }
2468 bool AllowsLazyCompilation();
2469 bool AllowsLazyCompilationWithoutContext();
2471 void InitializeSharedInfo(Handle<Code> code);
2473 Handle<String> debug_name() const {
2474 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2475 return raw_name_->string();
2477 return inferred_name();
2480 Handle<String> inferred_name() const {
2481 if (!inferred_name_.is_null()) {
2482 DCHECK(raw_inferred_name_ == NULL);
2483 return inferred_name_;
2485 if (raw_inferred_name_ != NULL) {
2486 return raw_inferred_name_->string();
2489 return Handle<String>();
2492 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2493 void set_inferred_name(Handle<String> inferred_name) {
2494 DCHECK(!inferred_name.is_null());
2495 inferred_name_ = inferred_name;
2496 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2497 raw_inferred_name_ = NULL;
2500 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2501 DCHECK(raw_inferred_name != NULL);
2502 raw_inferred_name_ = raw_inferred_name;
2503 DCHECK(inferred_name_.is_null());
2504 inferred_name_ = Handle<String>();
2507 // shared_info may be null if it's not cached in full code.
2508 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2510 bool pretenure() { return Pretenure::decode(bitfield_); }
2511 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2513 bool has_duplicate_parameters() {
2514 return HasDuplicateParameters::decode(bitfield_);
2517 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2519 // This is used as a heuristic on when to eagerly compile a function
2520 // literal. We consider the following constructs as hints that the
2521 // function will be called immediately:
2522 // - (function() { ... })();
2523 // - var x = function() { ... }();
2524 bool is_parenthesized() {
2525 return IsParenthesized::decode(bitfield_) == kIsParenthesized;
2527 void set_parenthesized() {
2528 bitfield_ = IsParenthesized::update(bitfield_, kIsParenthesized);
2531 FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2533 int ast_node_count() { return ast_properties_.node_count(); }
2534 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2535 void set_ast_properties(AstProperties* ast_properties) {
2536 ast_properties_ = *ast_properties;
2538 const FeedbackVectorSpec& feedback_vector_spec() const {
2539 return ast_properties_.get_spec();
2541 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2542 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2543 void set_dont_optimize_reason(BailoutReason reason) {
2544 dont_optimize_reason_ = reason;
2548 FunctionLiteral(Zone* zone, const AstRawString* name,
2549 AstValueFactory* ast_value_factory, Scope* scope,
2550 ZoneList<Statement*>* body, int materialized_literal_count,
2551 int expected_property_count, int handler_count,
2552 int parameter_count, FunctionType function_type,
2553 ParameterFlag has_duplicate_parameters,
2554 IsFunctionFlag is_function,
2555 IsParenthesizedFlag is_parenthesized, FunctionKind kind,
2557 : Expression(zone, position),
2561 raw_inferred_name_(ast_value_factory->empty_string()),
2562 dont_optimize_reason_(kNoReason),
2563 materialized_literal_count_(materialized_literal_count),
2564 expected_property_count_(expected_property_count),
2565 handler_count_(handler_count),
2566 parameter_count_(parameter_count),
2567 function_token_position_(RelocInfo::kNoPosition) {
2568 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2569 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2570 Pretenure::encode(false) |
2571 HasDuplicateParameters::encode(has_duplicate_parameters) |
2572 IsFunction::encode(is_function) |
2573 IsParenthesized::encode(is_parenthesized) |
2574 FunctionKindBits::encode(kind);
2575 DCHECK(IsValidFunctionKind(kind));
2579 const AstRawString* raw_name_;
2580 Handle<String> name_;
2581 Handle<SharedFunctionInfo> shared_info_;
2583 ZoneList<Statement*>* body_;
2584 const AstString* raw_inferred_name_;
2585 Handle<String> inferred_name_;
2586 AstProperties ast_properties_;
2587 BailoutReason dont_optimize_reason_;
2589 int materialized_literal_count_;
2590 int expected_property_count_;
2592 int parameter_count_;
2593 int function_token_position_;
2596 class IsExpression : public BitField<bool, 0, 1> {};
2597 class IsAnonymous : public BitField<bool, 1, 1> {};
2598 class Pretenure : public BitField<bool, 2, 1> {};
2599 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2600 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2601 class IsParenthesized : public BitField<IsParenthesizedFlag, 5, 1> {};
2602 class FunctionKindBits : public BitField<FunctionKind, 6, 7> {};
2606 class ClassLiteral FINAL : public Expression {
2608 typedef ObjectLiteralProperty Property;
2610 DECLARE_NODE_TYPE(ClassLiteral)
2612 Handle<String> name() const { return raw_name_->string(); }
2613 const AstRawString* raw_name() const { return raw_name_; }
2614 Scope* scope() const { return scope_; }
2615 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2616 Expression* extends() const { return extends_; }
2617 FunctionLiteral* constructor() const { return constructor_; }
2618 ZoneList<Property*>* properties() const { return properties_; }
2619 int start_position() const { return position(); }
2620 int end_position() const { return end_position_; }
2622 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2623 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2624 BailoutId ExitId() { return BailoutId(local_id(2)); }
2626 // Return an AST id for a property that is used in simulate instructions.
2627 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 3)); }
2629 // Unlike other AST nodes, this number of bailout IDs allocated for an
2630 // ClassLiteral can vary, so num_ids() is not a static method.
2631 int num_ids() const { return parent_num_ids() + 3 + properties()->length(); }
2634 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2635 VariableProxy* class_variable_proxy, Expression* extends,
2636 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2637 int start_position, int end_position)
2638 : Expression(zone, start_position),
2641 class_variable_proxy_(class_variable_proxy),
2643 constructor_(constructor),
2644 properties_(properties),
2645 end_position_(end_position) {}
2646 static int parent_num_ids() { return Expression::num_ids(); }
2649 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2651 const AstRawString* raw_name_;
2653 VariableProxy* class_variable_proxy_;
2654 Expression* extends_;
2655 FunctionLiteral* constructor_;
2656 ZoneList<Property*>* properties_;
2661 class NativeFunctionLiteral FINAL : public Expression {
2663 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2665 Handle<String> name() const { return name_->string(); }
2666 v8::Extension* extension() const { return extension_; }
2669 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2670 v8::Extension* extension, int pos)
2671 : Expression(zone, pos), name_(name), extension_(extension) {}
2674 const AstRawString* name_;
2675 v8::Extension* extension_;
2679 class ThisFunction FINAL : public Expression {
2681 DECLARE_NODE_TYPE(ThisFunction)
2684 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2688 class SuperReference FINAL : public Expression {
2690 DECLARE_NODE_TYPE(SuperReference)
2692 VariableProxy* this_var() const { return this_var_; }
2694 static int num_ids() { return parent_num_ids() + 1; }
2695 TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2697 // Type feedback information.
2698 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2699 Isolate* isolate) OVERRIDE {
2700 return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2702 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot) OVERRIDE {
2703 homeobject_feedback_slot_ = slot;
2705 Code::Kind FeedbackICSlotKind(int index) OVERRIDE { return Code::LOAD_IC; }
2707 FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2708 DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2709 return homeobject_feedback_slot_;
2713 SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2714 : Expression(zone, pos),
2715 this_var_(this_var),
2716 homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2717 DCHECK(this_var->is_this());
2719 static int parent_num_ids() { return Expression::num_ids(); }
2722 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2724 VariableProxy* this_var_;
2725 FeedbackVectorICSlot homeobject_feedback_slot_;
2729 #undef DECLARE_NODE_TYPE
2732 // ----------------------------------------------------------------------------
2733 // Regular expressions
2736 class RegExpVisitor BASE_EMBEDDED {
2738 virtual ~RegExpVisitor() { }
2739 #define MAKE_CASE(Name) \
2740 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2741 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2746 class RegExpTree : public ZoneObject {
2748 static const int kInfinity = kMaxInt;
2749 virtual ~RegExpTree() {}
2750 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2751 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2752 RegExpNode* on_success) = 0;
2753 virtual bool IsTextElement() { return false; }
2754 virtual bool IsAnchoredAtStart() { return false; }
2755 virtual bool IsAnchoredAtEnd() { return false; }
2756 virtual int min_match() = 0;
2757 virtual int max_match() = 0;
2758 // Returns the interval of registers used for captures within this
2760 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2761 virtual void AppendToText(RegExpText* text, Zone* zone);
2762 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2763 #define MAKE_ASTYPE(Name) \
2764 virtual RegExp##Name* As##Name(); \
2765 virtual bool Is##Name();
2766 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2771 class RegExpDisjunction FINAL : public RegExpTree {
2773 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2774 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2775 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2776 RegExpNode* on_success) OVERRIDE;
2777 RegExpDisjunction* AsDisjunction() OVERRIDE;
2778 Interval CaptureRegisters() OVERRIDE;
2779 bool IsDisjunction() OVERRIDE;
2780 bool IsAnchoredAtStart() OVERRIDE;
2781 bool IsAnchoredAtEnd() OVERRIDE;
2782 int min_match() OVERRIDE { return min_match_; }
2783 int max_match() OVERRIDE { return max_match_; }
2784 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2786 ZoneList<RegExpTree*>* alternatives_;
2792 class RegExpAlternative FINAL : public RegExpTree {
2794 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2795 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2796 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2797 RegExpNode* on_success) OVERRIDE;
2798 RegExpAlternative* AsAlternative() OVERRIDE;
2799 Interval CaptureRegisters() OVERRIDE;
2800 bool IsAlternative() OVERRIDE;
2801 bool IsAnchoredAtStart() OVERRIDE;
2802 bool IsAnchoredAtEnd() OVERRIDE;
2803 int min_match() OVERRIDE { return min_match_; }
2804 int max_match() OVERRIDE { return max_match_; }
2805 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2807 ZoneList<RegExpTree*>* nodes_;
2813 class RegExpAssertion FINAL : public RegExpTree {
2815 enum AssertionType {
2823 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2824 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2825 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2826 RegExpNode* on_success) OVERRIDE;
2827 RegExpAssertion* AsAssertion() OVERRIDE;
2828 bool IsAssertion() OVERRIDE;
2829 bool IsAnchoredAtStart() OVERRIDE;
2830 bool IsAnchoredAtEnd() OVERRIDE;
2831 int min_match() OVERRIDE { return 0; }
2832 int max_match() OVERRIDE { return 0; }
2833 AssertionType assertion_type() { return assertion_type_; }
2835 AssertionType assertion_type_;
2839 class CharacterSet FINAL BASE_EMBEDDED {
2841 explicit CharacterSet(uc16 standard_set_type)
2843 standard_set_type_(standard_set_type) {}
2844 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2846 standard_set_type_(0) {}
2847 ZoneList<CharacterRange>* ranges(Zone* zone);
2848 uc16 standard_set_type() { return standard_set_type_; }
2849 void set_standard_set_type(uc16 special_set_type) {
2850 standard_set_type_ = special_set_type;
2852 bool is_standard() { return standard_set_type_ != 0; }
2853 void Canonicalize();
2855 ZoneList<CharacterRange>* ranges_;
2856 // If non-zero, the value represents a standard set (e.g., all whitespace
2857 // characters) without having to expand the ranges.
2858 uc16 standard_set_type_;
2862 class RegExpCharacterClass FINAL : public RegExpTree {
2864 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2866 is_negated_(is_negated) { }
2867 explicit RegExpCharacterClass(uc16 type)
2869 is_negated_(false) { }
2870 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2871 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2872 RegExpNode* on_success) OVERRIDE;
2873 RegExpCharacterClass* AsCharacterClass() OVERRIDE;
2874 bool IsCharacterClass() OVERRIDE;
2875 bool IsTextElement() OVERRIDE { return true; }
2876 int min_match() OVERRIDE { return 1; }
2877 int max_match() OVERRIDE { return 1; }
2878 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2879 CharacterSet character_set() { return set_; }
2880 // TODO(lrn): Remove need for complex version if is_standard that
2881 // recognizes a mangled standard set and just do { return set_.is_special(); }
2882 bool is_standard(Zone* zone);
2883 // Returns a value representing the standard character set if is_standard()
2885 // Currently used values are:
2886 // s : unicode whitespace
2887 // S : unicode non-whitespace
2888 // w : ASCII word character (digit, letter, underscore)
2889 // W : non-ASCII word character
2891 // D : non-ASCII digit
2892 // . : non-unicode non-newline
2893 // * : All characters
2894 uc16 standard_type() { return set_.standard_set_type(); }
2895 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2896 bool is_negated() { return is_negated_; }
2904 class RegExpAtom FINAL : public RegExpTree {
2906 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2907 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2908 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2909 RegExpNode* on_success) OVERRIDE;
2910 RegExpAtom* AsAtom() OVERRIDE;
2911 bool IsAtom() OVERRIDE;
2912 bool IsTextElement() OVERRIDE { return true; }
2913 int min_match() OVERRIDE { return data_.length(); }
2914 int max_match() OVERRIDE { return data_.length(); }
2915 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2916 Vector<const uc16> data() { return data_; }
2917 int length() { return data_.length(); }
2919 Vector<const uc16> data_;
2923 class RegExpText FINAL : public RegExpTree {
2925 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2926 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2927 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2928 RegExpNode* on_success) OVERRIDE;
2929 RegExpText* AsText() OVERRIDE;
2930 bool IsText() OVERRIDE;
2931 bool IsTextElement() OVERRIDE { return true; }
2932 int min_match() OVERRIDE { return length_; }
2933 int max_match() OVERRIDE { return length_; }
2934 void AppendToText(RegExpText* text, Zone* zone) OVERRIDE;
2935 void AddElement(TextElement elm, Zone* zone) {
2936 elements_.Add(elm, zone);
2937 length_ += elm.length();
2939 ZoneList<TextElement>* elements() { return &elements_; }
2941 ZoneList<TextElement> elements_;
2946 class RegExpQuantifier FINAL : public RegExpTree {
2948 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
2949 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
2953 min_match_(min * body->min_match()),
2954 quantifier_type_(type) {
2955 if (max > 0 && body->max_match() > kInfinity / max) {
2956 max_match_ = kInfinity;
2958 max_match_ = max * body->max_match();
2961 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2962 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2963 RegExpNode* on_success) OVERRIDE;
2964 static RegExpNode* ToNode(int min,
2968 RegExpCompiler* compiler,
2969 RegExpNode* on_success,
2970 bool not_at_start = false);
2971 RegExpQuantifier* AsQuantifier() OVERRIDE;
2972 Interval CaptureRegisters() OVERRIDE;
2973 bool IsQuantifier() OVERRIDE;
2974 int min_match() OVERRIDE { return min_match_; }
2975 int max_match() OVERRIDE { return max_match_; }
2976 int min() { return min_; }
2977 int max() { return max_; }
2978 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
2979 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
2980 bool is_greedy() { return quantifier_type_ == GREEDY; }
2981 RegExpTree* body() { return body_; }
2989 QuantifierType quantifier_type_;
2993 class RegExpCapture FINAL : public RegExpTree {
2995 explicit RegExpCapture(RegExpTree* body, int index)
2996 : body_(body), index_(index) { }
2997 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
2998 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2999 RegExpNode* on_success) OVERRIDE;
3000 static RegExpNode* ToNode(RegExpTree* body,
3002 RegExpCompiler* compiler,
3003 RegExpNode* on_success);
3004 RegExpCapture* AsCapture() OVERRIDE;
3005 bool IsAnchoredAtStart() OVERRIDE;
3006 bool IsAnchoredAtEnd() OVERRIDE;
3007 Interval CaptureRegisters() OVERRIDE;
3008 bool IsCapture() OVERRIDE;
3009 int min_match() OVERRIDE { return body_->min_match(); }
3010 int max_match() OVERRIDE { return body_->max_match(); }
3011 RegExpTree* body() { return body_; }
3012 int index() { return index_; }
3013 static int StartRegister(int index) { return index * 2; }
3014 static int EndRegister(int index) { return index * 2 + 1; }
3022 class RegExpLookahead FINAL : public RegExpTree {
3024 RegExpLookahead(RegExpTree* body,
3029 is_positive_(is_positive),
3030 capture_count_(capture_count),
3031 capture_from_(capture_from) { }
3033 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3034 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3035 RegExpNode* on_success) OVERRIDE;
3036 RegExpLookahead* AsLookahead() OVERRIDE;
3037 Interval CaptureRegisters() OVERRIDE;
3038 bool IsLookahead() OVERRIDE;
3039 bool IsAnchoredAtStart() OVERRIDE;
3040 int min_match() OVERRIDE { return 0; }
3041 int max_match() OVERRIDE { return 0; }
3042 RegExpTree* body() { return body_; }
3043 bool is_positive() { return is_positive_; }
3044 int capture_count() { return capture_count_; }
3045 int capture_from() { return capture_from_; }
3055 class RegExpBackReference FINAL : public RegExpTree {
3057 explicit RegExpBackReference(RegExpCapture* capture)
3058 : capture_(capture) { }
3059 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3060 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3061 RegExpNode* on_success) OVERRIDE;
3062 RegExpBackReference* AsBackReference() OVERRIDE;
3063 bool IsBackReference() OVERRIDE;
3064 int min_match() OVERRIDE { return 0; }
3065 int max_match() OVERRIDE { return capture_->max_match(); }
3066 int index() { return capture_->index(); }
3067 RegExpCapture* capture() { return capture_; }
3069 RegExpCapture* capture_;
3073 class RegExpEmpty FINAL : public RegExpTree {
3076 void* Accept(RegExpVisitor* visitor, void* data) OVERRIDE;
3077 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3078 RegExpNode* on_success) OVERRIDE;
3079 RegExpEmpty* AsEmpty() OVERRIDE;
3080 bool IsEmpty() OVERRIDE;
3081 int min_match() OVERRIDE { return 0; }
3082 int max_match() OVERRIDE { return 0; }
3086 // ----------------------------------------------------------------------------
3088 // - leaf node visitors are abstract.
3090 class AstVisitor BASE_EMBEDDED {
3093 virtual ~AstVisitor() {}
3095 // Stack overflow check and dynamic dispatch.
3096 virtual void Visit(AstNode* node) = 0;
3098 // Iteration left-to-right.
3099 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3100 virtual void VisitStatements(ZoneList<Statement*>* statements);
3101 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3103 // Individual AST nodes.
3104 #define DEF_VISIT(type) \
3105 virtual void Visit##type(type* node) = 0;
3106 AST_NODE_LIST(DEF_VISIT)
3111 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3113 void Visit(AstNode* node) FINAL { \
3114 if (!CheckStackOverflow()) node->Accept(this); \
3117 void SetStackOverflow() { stack_overflow_ = true; } \
3118 void ClearStackOverflow() { stack_overflow_ = false; } \
3119 bool HasStackOverflow() const { return stack_overflow_; } \
3121 bool CheckStackOverflow() { \
3122 if (stack_overflow_) return true; \
3123 StackLimitCheck check(isolate_); \
3124 if (!check.HasOverflowed()) return false; \
3125 stack_overflow_ = true; \
3130 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3131 isolate_ = isolate; \
3133 stack_overflow_ = false; \
3135 Zone* zone() { return zone_; } \
3136 Isolate* isolate() { return isolate_; } \
3138 Isolate* isolate_; \
3140 bool stack_overflow_
3143 // ----------------------------------------------------------------------------
3146 class AstNodeFactory FINAL BASE_EMBEDDED {
3148 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3149 : zone_(ast_value_factory->zone()),
3150 ast_value_factory_(ast_value_factory) {}
3152 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3156 return new (zone_) VariableDeclaration(zone_, proxy, mode, scope, pos);
3159 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3161 FunctionLiteral* fun,
3164 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3167 ModuleDeclaration* NewModuleDeclaration(VariableProxy* proxy,
3171 return new (zone_) ModuleDeclaration(zone_, proxy, module, scope, pos);
3174 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3175 const AstRawString* import_name,
3176 const AstRawString* module_specifier,
3177 Scope* scope, int pos) {
3178 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3179 module_specifier, scope, pos);
3182 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3185 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3188 ModuleLiteral* NewModuleLiteral(Block* body, ModuleDescriptor* descriptor,
3190 return new (zone_) ModuleLiteral(zone_, body, descriptor, pos);
3193 ModulePath* NewModulePath(Module* origin, const AstRawString* name, int pos) {
3194 return new (zone_) ModulePath(zone_, origin, name, pos);
3197 ModuleUrl* NewModuleUrl(Handle<String> url, int pos) {
3198 return new (zone_) ModuleUrl(zone_, url, pos);
3201 Block* NewBlock(ZoneList<const AstRawString*>* labels,
3203 bool is_initializer_block,
3206 Block(zone_, labels, capacity, is_initializer_block, pos);
3209 #define STATEMENT_WITH_LABELS(NodeType) \
3210 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3211 return new (zone_) NodeType(zone_, labels, pos); \
3213 STATEMENT_WITH_LABELS(DoWhileStatement)
3214 STATEMENT_WITH_LABELS(WhileStatement)
3215 STATEMENT_WITH_LABELS(ForStatement)
3216 STATEMENT_WITH_LABELS(SwitchStatement)
3217 #undef STATEMENT_WITH_LABELS
3219 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3220 ZoneList<const AstRawString*>* labels,
3222 switch (visit_mode) {
3223 case ForEachStatement::ENUMERATE: {
3224 return new (zone_) ForInStatement(zone_, labels, pos);
3226 case ForEachStatement::ITERATE: {
3227 return new (zone_) ForOfStatement(zone_, labels, pos);
3234 ModuleStatement* NewModuleStatement(Block* body, int pos) {
3235 return new (zone_) ModuleStatement(zone_, body, pos);
3238 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3239 return new (zone_) ExpressionStatement(zone_, expression, pos);
3242 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3243 return new (zone_) ContinueStatement(zone_, target, pos);
3246 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3247 return new (zone_) BreakStatement(zone_, target, pos);
3250 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3251 return new (zone_) ReturnStatement(zone_, expression, pos);
3254 WithStatement* NewWithStatement(Scope* scope,
3255 Expression* expression,
3256 Statement* statement,
3258 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3261 IfStatement* NewIfStatement(Expression* condition,
3262 Statement* then_statement,
3263 Statement* else_statement,
3266 IfStatement(zone_, condition, then_statement, else_statement, pos);
3269 TryCatchStatement* NewTryCatchStatement(int index,
3275 return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3276 variable, catch_block, pos);
3279 TryFinallyStatement* NewTryFinallyStatement(int index,
3281 Block* finally_block,
3284 TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3287 DebuggerStatement* NewDebuggerStatement(int pos) {
3288 return new (zone_) DebuggerStatement(zone_, pos);
3291 EmptyStatement* NewEmptyStatement(int pos) {
3292 return new(zone_) EmptyStatement(zone_, pos);
3295 CaseClause* NewCaseClause(
3296 Expression* label, ZoneList<Statement*>* statements, int pos) {
3297 return new (zone_) CaseClause(zone_, label, statements, pos);
3300 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3302 Literal(zone_, ast_value_factory_->NewString(string), pos);
3305 // A JavaScript symbol (ECMA-262 edition 6).
3306 Literal* NewSymbolLiteral(const char* name, int pos) {
3307 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3310 Literal* NewNumberLiteral(double number, int pos) {
3312 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3315 Literal* NewSmiLiteral(int number, int pos) {
3316 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3319 Literal* NewBooleanLiteral(bool b, int pos) {
3320 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3323 Literal* NewNullLiteral(int pos) {
3324 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3327 Literal* NewUndefinedLiteral(int pos) {
3328 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3331 Literal* NewTheHoleLiteral(int pos) {
3332 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3335 ObjectLiteral* NewObjectLiteral(
3336 ZoneList<ObjectLiteral::Property*>* properties,
3338 int boilerplate_properties,
3341 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3342 boilerplate_properties, has_function, pos);
3345 ObjectLiteral::Property* NewObjectLiteralProperty(
3346 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3347 bool is_static, bool is_computed_name) {
3349 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3352 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3355 bool is_computed_name) {
3356 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3357 is_static, is_computed_name);
3360 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3361 const AstRawString* flags,
3364 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3367 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3370 return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3373 VariableProxy* NewVariableProxy(Variable* var,
3374 int start_position = RelocInfo::kNoPosition,
3375 int end_position = RelocInfo::kNoPosition) {
3376 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3379 VariableProxy* NewVariableProxy(const AstRawString* name, bool is_this,
3380 int start_position = RelocInfo::kNoPosition,
3381 int end_position = RelocInfo::kNoPosition) {
3383 VariableProxy(zone_, name, is_this, start_position, end_position);
3386 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3387 return new (zone_) Property(zone_, obj, key, pos);
3390 Call* NewCall(Expression* expression,
3391 ZoneList<Expression*>* arguments,
3393 return new (zone_) Call(zone_, expression, arguments, pos);
3396 CallNew* NewCallNew(Expression* expression,
3397 ZoneList<Expression*>* arguments,
3399 return new (zone_) CallNew(zone_, expression, arguments, pos);
3402 CallRuntime* NewCallRuntime(const AstRawString* name,
3403 const Runtime::Function* function,
3404 ZoneList<Expression*>* arguments,
3406 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3409 UnaryOperation* NewUnaryOperation(Token::Value op,
3410 Expression* expression,
3412 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3415 BinaryOperation* NewBinaryOperation(Token::Value op,
3419 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3422 CountOperation* NewCountOperation(Token::Value op,
3426 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3429 CompareOperation* NewCompareOperation(Token::Value op,
3433 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3436 Conditional* NewConditional(Expression* condition,
3437 Expression* then_expression,
3438 Expression* else_expression,
3440 return new (zone_) Conditional(zone_, condition, then_expression,
3441 else_expression, position);
3444 Assignment* NewAssignment(Token::Value op,
3448 DCHECK(Token::IsAssignmentOp(op));
3449 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3450 if (assign->is_compound()) {
3451 DCHECK(Token::IsAssignmentOp(op));
3452 assign->binary_operation_ =
3453 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3458 Yield* NewYield(Expression *generator_object,
3459 Expression* expression,
3460 Yield::Kind yield_kind,
3462 if (!expression) expression = NewUndefinedLiteral(pos);
3464 Yield(zone_, generator_object, expression, yield_kind, pos);
3467 Throw* NewThrow(Expression* exception, int pos) {
3468 return new (zone_) Throw(zone_, exception, pos);
3471 FunctionLiteral* NewFunctionLiteral(
3472 const AstRawString* name, AstValueFactory* ast_value_factory,
3473 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3474 int expected_property_count, int handler_count, int parameter_count,
3475 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3476 FunctionLiteral::FunctionType function_type,
3477 FunctionLiteral::IsFunctionFlag is_function,
3478 FunctionLiteral::IsParenthesizedFlag is_parenthesized, FunctionKind kind,
3480 return new (zone_) FunctionLiteral(
3481 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3482 expected_property_count, handler_count, parameter_count, function_type,
3483 has_duplicate_parameters, is_function, is_parenthesized, kind,
3487 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3488 VariableProxy* proxy, Expression* extends,
3489 FunctionLiteral* constructor,
3490 ZoneList<ObjectLiteral::Property*>* properties,
3491 int start_position, int end_position) {
3493 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3494 properties, start_position, end_position);
3497 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3498 v8::Extension* extension,
3500 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3503 ThisFunction* NewThisFunction(int pos) {
3504 return new (zone_) ThisFunction(zone_, pos);
3507 SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3508 return new (zone_) SuperReference(zone_, this_var, pos);
3513 AstValueFactory* ast_value_factory_;
3517 } } // namespace v8::internal