[destructuring] Adapting PatternRewriter to work in C-style for-statements.
[platform/upstream/v8.git] / src / ast.h
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.
4
5 #ifndef V8_AST_H_
6 #define V8_AST_H_
7
8 #include "src/v8.h"
9
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"
25
26 namespace v8 {
27 namespace internal {
28
29 // The abstract syntax tree is an intermediate, light-weight
30 // representation of the parsed JavaScript code suitable for
31 // compilation to native code.
32
33 // Nodes are allocated in a separate zone, which allows faster
34 // allocation and constant-time deallocation of the entire syntax
35 // tree.
36
37
38 // ----------------------------------------------------------------------------
39 // Nodes of the abstract syntax tree. Only concrete classes are
40 // enumerated here.
41
42 #define DECLARATION_NODE_LIST(V) \
43   V(VariableDeclaration)         \
44   V(FunctionDeclaration)         \
45   V(ImportDeclaration)           \
46   V(ExportDeclaration)
47
48 #define STATEMENT_NODE_LIST(V)                  \
49   V(Block)                                      \
50   V(ExpressionStatement)                        \
51   V(EmptyStatement)                             \
52   V(IfStatement)                                \
53   V(ContinueStatement)                          \
54   V(BreakStatement)                             \
55   V(ReturnStatement)                            \
56   V(WithStatement)                              \
57   V(SwitchStatement)                            \
58   V(DoWhileStatement)                           \
59   V(WhileStatement)                             \
60   V(ForStatement)                               \
61   V(ForInStatement)                             \
62   V(ForOfStatement)                             \
63   V(TryCatchStatement)                          \
64   V(TryFinallyStatement)                        \
65   V(DebuggerStatement)
66
67 #define EXPRESSION_NODE_LIST(V) \
68   V(FunctionLiteral)            \
69   V(ClassLiteral)               \
70   V(NativeFunctionLiteral)      \
71   V(Conditional)                \
72   V(VariableProxy)              \
73   V(Literal)                    \
74   V(RegExpLiteral)              \
75   V(ObjectLiteral)              \
76   V(ArrayLiteral)               \
77   V(Assignment)                 \
78   V(Yield)                      \
79   V(Throw)                      \
80   V(Property)                   \
81   V(Call)                       \
82   V(CallNew)                    \
83   V(CallRuntime)                \
84   V(UnaryOperation)             \
85   V(CountOperation)             \
86   V(BinaryOperation)            \
87   V(CompareOperation)           \
88   V(Spread)                     \
89   V(ThisFunction)               \
90   V(SuperReference)             \
91   V(CaseClause)
92
93 #define AST_NODE_LIST(V)                        \
94   DECLARATION_NODE_LIST(V)                      \
95   STATEMENT_NODE_LIST(V)                        \
96   EXPRESSION_NODE_LIST(V)
97
98 // Forward declarations
99 class AstNodeFactory;
100 class AstVisitor;
101 class Declaration;
102 class Module;
103 class BreakableStatement;
104 class Expression;
105 class IterationStatement;
106 class MaterializedLiteral;
107 class Statement;
108 class TypeFeedbackOracle;
109
110 class RegExpAlternative;
111 class RegExpAssertion;
112 class RegExpAtom;
113 class RegExpBackReference;
114 class RegExpCapture;
115 class RegExpCharacterClass;
116 class RegExpCompiler;
117 class RegExpDisjunction;
118 class RegExpEmpty;
119 class RegExpLookahead;
120 class RegExpQuantifier;
121 class RegExpText;
122
123 #define DEF_FORWARD_DECLARATION(type) class type;
124 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
125 #undef DEF_FORWARD_DECLARATION
126
127
128 // Typedef only introduced to avoid unreadable code.
129 // Please do appreciate the required space in "> >".
130 typedef ZoneList<Handle<String> > ZoneStringList;
131 typedef ZoneList<Handle<Object> > ZoneObjectList;
132
133
134 #define DECLARE_NODE_TYPE(type)                                          \
135   void Accept(AstVisitor* v) override;                                   \
136   AstNode::NodeType node_type() const final { return AstNode::k##type; } \
137   friend class AstNodeFactory;
138
139
140 enum AstPropertiesFlag {
141   kDontSelfOptimize,
142   kDontSoftInline,
143   kDontCache
144 };
145
146
147 class FeedbackVectorRequirements {
148  public:
149   FeedbackVectorRequirements(int slots, int ic_slots)
150       : slots_(slots), ic_slots_(ic_slots) {}
151
152   int slots() const { return slots_; }
153   int ic_slots() const { return ic_slots_; }
154
155  private:
156   int slots_;
157   int ic_slots_;
158 };
159
160
161 class VariableICSlotPair final {
162  public:
163   VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
164       : variable_(variable), slot_(slot) {}
165   VariableICSlotPair()
166       : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
167
168   Variable* variable() const { return variable_; }
169   FeedbackVectorICSlot slot() const { return slot_; }
170
171  private:
172   Variable* variable_;
173   FeedbackVectorICSlot slot_;
174 };
175
176
177 typedef List<VariableICSlotPair> ICSlotCache;
178
179
180 class AstProperties final BASE_EMBEDDED {
181  public:
182   class Flags : public EnumSet<AstPropertiesFlag, int> {};
183
184   explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
185
186   Flags* flags() { return &flags_; }
187   int node_count() { return node_count_; }
188   void add_node_count(int count) { node_count_ += count; }
189
190   int slots() const { return spec_.slots(); }
191   void increase_slots(int count) { spec_.increase_slots(count); }
192
193   int ic_slots() const { return spec_.ic_slots(); }
194   void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
195   void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
196   const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
197
198  private:
199   Flags flags_;
200   int node_count_;
201   ZoneFeedbackVectorSpec spec_;
202 };
203
204
205 class AstNode: public ZoneObject {
206  public:
207 #define DECLARE_TYPE_ENUM(type) k##type,
208   enum NodeType {
209     AST_NODE_LIST(DECLARE_TYPE_ENUM)
210     kInvalid = -1
211   };
212 #undef DECLARE_TYPE_ENUM
213
214   void* operator new(size_t size, Zone* zone) { return zone->New(size); }
215
216   explicit AstNode(int position): position_(position) {}
217   virtual ~AstNode() {}
218
219   virtual void Accept(AstVisitor* v) = 0;
220   virtual NodeType node_type() const = 0;
221   int position() const { return position_; }
222
223   // Type testing & conversion functions overridden by concrete subclasses.
224 #define DECLARE_NODE_FUNCTIONS(type) \
225   bool Is##type() const { return node_type() == AstNode::k##type; } \
226   type* As##type() { \
227     return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
228   } \
229   const type* As##type() const { \
230     return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
231   }
232   AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
233 #undef DECLARE_NODE_FUNCTIONS
234
235   virtual BreakableStatement* AsBreakableStatement() { return NULL; }
236   virtual IterationStatement* AsIterationStatement() { return NULL; }
237   virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
238
239   // The interface for feedback slots, with default no-op implementations for
240   // node types which don't actually have this. Note that this is conceptually
241   // not really nice, but multiple inheritance would introduce yet another
242   // vtable entry per node, something we don't want for space reasons.
243   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
244       Isolate* isolate, const ICSlotCache* cache) {
245     return FeedbackVectorRequirements(0, 0);
246   }
247   virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
248   virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
249                                       ICSlotCache* cache) {
250     UNREACHABLE();
251   }
252   // Each ICSlot stores a kind of IC which the participating node should know.
253   virtual Code::Kind FeedbackICSlotKind(int index) {
254     UNREACHABLE();
255     return Code::NUMBER_OF_KINDS;
256   }
257
258  private:
259   // Hidden to prevent accidental usage. It would have to load the
260   // current zone from the TLS.
261   void* operator new(size_t size);
262
263   friend class CaseClause;  // Generates AST IDs.
264
265   int position_;
266 };
267
268
269 class Statement : public AstNode {
270  public:
271   explicit Statement(Zone* zone, int position) : AstNode(position) {}
272
273   bool IsEmpty() { return AsEmptyStatement() != NULL; }
274   virtual bool IsJump() const { return false; }
275 };
276
277
278 class SmallMapList final {
279  public:
280   SmallMapList() {}
281   SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
282
283   void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
284   void Clear() { list_.Clear(); }
285   void Sort() { list_.Sort(); }
286
287   bool is_empty() const { return list_.is_empty(); }
288   int length() const { return list_.length(); }
289
290   void AddMapIfMissing(Handle<Map> map, Zone* zone) {
291     if (!Map::TryUpdate(map).ToHandle(&map)) return;
292     for (int i = 0; i < length(); ++i) {
293       if (at(i).is_identical_to(map)) return;
294     }
295     Add(map, zone);
296   }
297
298   void FilterForPossibleTransitions(Map* root_map) {
299     for (int i = list_.length() - 1; i >= 0; i--) {
300       if (at(i)->FindRootMap() != root_map) {
301         list_.RemoveElement(list_.at(i));
302       }
303     }
304   }
305
306   void Add(Handle<Map> handle, Zone* zone) {
307     list_.Add(handle.location(), zone);
308   }
309
310   Handle<Map> at(int i) const {
311     return Handle<Map>(list_.at(i));
312   }
313
314   Handle<Map> first() const { return at(0); }
315   Handle<Map> last() const { return at(length() - 1); }
316
317  private:
318   // The list stores pointers to Map*, that is Map**, so it's GC safe.
319   SmallPointerList<Map*> list_;
320
321   DISALLOW_COPY_AND_ASSIGN(SmallMapList);
322 };
323
324
325 class Expression : public AstNode {
326  public:
327   enum Context {
328     // Not assigned a context yet, or else will not be visited during
329     // code generation.
330     kUninitialized,
331     // Evaluated for its side effects.
332     kEffect,
333     // Evaluated for its value (and side effects).
334     kValue,
335     // Evaluated for control flow (and side effects).
336     kTest
337   };
338
339   virtual bool IsValidReferenceExpression() const { return false; }
340
341   // Helpers for ToBoolean conversion.
342   virtual bool ToBooleanIsTrue() const { return false; }
343   virtual bool ToBooleanIsFalse() const { return false; }
344
345   // Symbols that cannot be parsed as array indices are considered property
346   // names.  We do not treat symbols that can be array indexes as property
347   // names because [] for string objects is handled only by keyed ICs.
348   virtual bool IsPropertyName() const { return false; }
349
350   // True iff the expression is a literal represented as a smi.
351   bool IsSmiLiteral() const;
352
353   // True iff the expression is a string literal.
354   bool IsStringLiteral() const;
355
356   // True iff the expression is the null literal.
357   bool IsNullLiteral() const;
358
359   // True if we can prove that the expression is the undefined literal.
360   bool IsUndefinedLiteral(Isolate* isolate) const;
361
362   // Expression type bounds
363   Bounds bounds() const { return bounds_; }
364   void set_bounds(Bounds bounds) { bounds_ = bounds; }
365
366   // Type feedback information for assignments and properties.
367   virtual bool IsMonomorphic() {
368     UNREACHABLE();
369     return false;
370   }
371   virtual SmallMapList* GetReceiverTypes() {
372     UNREACHABLE();
373     return NULL;
374   }
375   virtual KeyedAccessStoreMode GetStoreMode() const {
376     UNREACHABLE();
377     return STANDARD_STORE;
378   }
379   virtual IcCheckType GetKeyType() const {
380     UNREACHABLE();
381     return ELEMENT;
382   }
383
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_);
388   }
389
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)); }
394
395  protected:
396   Expression(Zone* zone, int pos)
397       : AstNode(pos),
398         base_id_(BailoutId::None().ToInt()),
399         bounds_(Bounds::Unbounded(zone)),
400         bit_field_(0) {}
401   static int parent_num_ids() { return 0; }
402   void set_to_boolean_types(byte types) {
403     bit_field_ = ToBooleanTypesField::update(bit_field_, types);
404   }
405
406   int base_id() const {
407     DCHECK(!BailoutId(base_id_).IsNone());
408     return base_id_;
409   }
410
411  private:
412   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
413
414   int base_id_;
415   Bounds bounds_;
416   class ToBooleanTypesField : public BitField16<byte, 0, 8> {};
417   uint16_t bit_field_;
418   // Ends with 16-bit field; deriving classes in turn begin with
419   // 16-bit fields for optimum packing efficiency.
420 };
421
422
423 class BreakableStatement : public Statement {
424  public:
425   enum BreakableType {
426     TARGET_FOR_ANONYMOUS,
427     TARGET_FOR_NAMED_ONLY
428   };
429
430   // The labels associated with this statement. May be NULL;
431   // if it is != NULL, guaranteed to contain at least one entry.
432   ZoneList<const AstRawString*>* labels() const { return labels_; }
433
434   // Type testing & conversion.
435   BreakableStatement* AsBreakableStatement() final { return this; }
436
437   // Code generation
438   Label* break_target() { return &break_target_; }
439
440   // Testers.
441   bool is_target_for_anonymous() const {
442     return breakable_type_ == TARGET_FOR_ANONYMOUS;
443   }
444
445   void set_base_id(int id) { base_id_ = id; }
446   static int num_ids() { return parent_num_ids() + 2; }
447   BailoutId EntryId() const { return BailoutId(local_id(0)); }
448   BailoutId ExitId() const { return BailoutId(local_id(1)); }
449
450  protected:
451   BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
452                      BreakableType breakable_type, int position)
453       : Statement(zone, position),
454         labels_(labels),
455         breakable_type_(breakable_type),
456         base_id_(BailoutId::None().ToInt()) {
457     DCHECK(labels == NULL || labels->length() > 0);
458   }
459   static int parent_num_ids() { return 0; }
460
461   int base_id() const {
462     DCHECK(!BailoutId(base_id_).IsNone());
463     return base_id_;
464   }
465
466  private:
467   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
468
469   ZoneList<const AstRawString*>* labels_;
470   BreakableType breakable_type_;
471   Label break_target_;
472   int base_id_;
473 };
474
475
476 class Block final : public BreakableStatement {
477  public:
478   DECLARE_NODE_TYPE(Block)
479
480   void AddStatement(Statement* statement, Zone* zone) {
481     statements_.Add(statement, zone);
482   }
483
484   ZoneList<Statement*>* statements() { return &statements_; }
485   bool is_initializer_block() const { return is_initializer_block_; }
486
487   static int num_ids() { return parent_num_ids() + 1; }
488   BailoutId DeclsId() const { return BailoutId(local_id(0)); }
489
490   bool IsJump() const override {
491     return !statements_.is_empty() && statements_.last()->IsJump()
492         && labels() == NULL;  // Good enough as an approximation...
493   }
494
495   Scope* scope() const { return scope_; }
496   void set_scope(Scope* scope) { scope_ = scope; }
497
498  protected:
499   Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
500         bool is_initializer_block, int pos)
501       : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
502         statements_(capacity, zone),
503         is_initializer_block_(is_initializer_block),
504         scope_(NULL) {}
505   static int parent_num_ids() { return BreakableStatement::num_ids(); }
506
507  private:
508   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
509
510   ZoneList<Statement*> statements_;
511   bool is_initializer_block_;
512   Scope* scope_;
513 };
514
515
516 class Declaration : public AstNode {
517  public:
518   VariableProxy* proxy() const { return proxy_; }
519   VariableMode mode() const { return mode_; }
520   Scope* scope() const { return scope_; }
521   virtual InitializationFlag initialization() const = 0;
522   virtual bool IsInlineable() const;
523
524  protected:
525   Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
526               int pos)
527       : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
528     DCHECK(IsDeclaredVariableMode(mode));
529   }
530
531  private:
532   VariableMode mode_;
533   VariableProxy* proxy_;
534
535   // Nested scope from which the declaration originated.
536   Scope* scope_;
537 };
538
539
540 class VariableDeclaration final : public Declaration {
541  public:
542   DECLARE_NODE_TYPE(VariableDeclaration)
543
544   InitializationFlag initialization() const override {
545     return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
546   }
547
548   bool is_class_declaration() const { return is_class_declaration_; }
549
550   // VariableDeclarations can be grouped into consecutive declaration
551   // groups. Each VariableDeclaration is associated with the start position of
552   // the group it belongs to. The positions are used for strong mode scope
553   // checks for classes and functions.
554   int declaration_group_start() const { return declaration_group_start_; }
555
556  protected:
557   VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
558                       Scope* scope, int pos, bool is_class_declaration = false,
559                       int declaration_group_start = -1)
560       : Declaration(zone, proxy, mode, scope, pos),
561         is_class_declaration_(is_class_declaration),
562         declaration_group_start_(declaration_group_start) {}
563
564   bool is_class_declaration_;
565   int declaration_group_start_;
566 };
567
568
569 class FunctionDeclaration final : public Declaration {
570  public:
571   DECLARE_NODE_TYPE(FunctionDeclaration)
572
573   FunctionLiteral* fun() const { return fun_; }
574   InitializationFlag initialization() const override {
575     return kCreatedInitialized;
576   }
577   bool IsInlineable() const override;
578
579  protected:
580   FunctionDeclaration(Zone* zone,
581                       VariableProxy* proxy,
582                       VariableMode mode,
583                       FunctionLiteral* fun,
584                       Scope* scope,
585                       int pos)
586       : Declaration(zone, proxy, mode, scope, pos),
587         fun_(fun) {
588     DCHECK(mode == VAR || mode == LET || mode == CONST);
589     DCHECK(fun != NULL);
590   }
591
592  private:
593   FunctionLiteral* fun_;
594 };
595
596
597 class ImportDeclaration final : public Declaration {
598  public:
599   DECLARE_NODE_TYPE(ImportDeclaration)
600
601   const AstRawString* import_name() const { return import_name_; }
602   const AstRawString* module_specifier() const { return module_specifier_; }
603   void set_module_specifier(const AstRawString* module_specifier) {
604     DCHECK(module_specifier_ == NULL);
605     module_specifier_ = module_specifier;
606   }
607   InitializationFlag initialization() const override {
608     return kNeedsInitialization;
609   }
610
611  protected:
612   ImportDeclaration(Zone* zone, VariableProxy* proxy,
613                     const AstRawString* import_name,
614                     const AstRawString* module_specifier, Scope* scope, int pos)
615       : Declaration(zone, proxy, IMPORT, scope, pos),
616         import_name_(import_name),
617         module_specifier_(module_specifier) {}
618
619  private:
620   const AstRawString* import_name_;
621   const AstRawString* module_specifier_;
622 };
623
624
625 class ExportDeclaration final : public Declaration {
626  public:
627   DECLARE_NODE_TYPE(ExportDeclaration)
628
629   InitializationFlag initialization() const override {
630     return kCreatedInitialized;
631   }
632
633  protected:
634   ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
635       : Declaration(zone, proxy, LET, scope, pos) {}
636 };
637
638
639 class Module : public AstNode {
640  public:
641   ModuleDescriptor* descriptor() const { return descriptor_; }
642   Block* body() const { return body_; }
643
644  protected:
645   Module(Zone* zone, int pos)
646       : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
647   Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
648       : AstNode(pos), descriptor_(descriptor), body_(body) {}
649
650  private:
651   ModuleDescriptor* descriptor_;
652   Block* body_;
653 };
654
655
656 class IterationStatement : public BreakableStatement {
657  public:
658   // Type testing & conversion.
659   IterationStatement* AsIterationStatement() final { return this; }
660
661   Statement* body() const { return body_; }
662
663   static int num_ids() { return parent_num_ids() + 1; }
664   BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
665   virtual BailoutId ContinueId() const = 0;
666   virtual BailoutId StackCheckId() const = 0;
667
668   // Code generation
669   Label* continue_target()  { return &continue_target_; }
670
671  protected:
672   IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
673       : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
674         body_(NULL) {}
675   static int parent_num_ids() { return BreakableStatement::num_ids(); }
676   void Initialize(Statement* body) { body_ = body; }
677
678  private:
679   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
680
681   Statement* body_;
682   Label continue_target_;
683 };
684
685
686 class DoWhileStatement final : public IterationStatement {
687  public:
688   DECLARE_NODE_TYPE(DoWhileStatement)
689
690   void Initialize(Expression* cond, Statement* body) {
691     IterationStatement::Initialize(body);
692     cond_ = cond;
693   }
694
695   Expression* cond() const { return cond_; }
696
697   static int num_ids() { return parent_num_ids() + 2; }
698   BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
699   BailoutId StackCheckId() const override { return BackEdgeId(); }
700   BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
701
702  protected:
703   DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
704       : IterationStatement(zone, labels, pos), cond_(NULL) {}
705   static int parent_num_ids() { return IterationStatement::num_ids(); }
706
707  private:
708   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
709
710   Expression* cond_;
711 };
712
713
714 class WhileStatement final : public IterationStatement {
715  public:
716   DECLARE_NODE_TYPE(WhileStatement)
717
718   void Initialize(Expression* cond, Statement* body) {
719     IterationStatement::Initialize(body);
720     cond_ = cond;
721   }
722
723   Expression* cond() const { return cond_; }
724
725   static int num_ids() { return parent_num_ids() + 1; }
726   BailoutId ContinueId() const override { return EntryId(); }
727   BailoutId StackCheckId() const override { return BodyId(); }
728   BailoutId BodyId() const { return BailoutId(local_id(0)); }
729
730  protected:
731   WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
732       : IterationStatement(zone, labels, pos), cond_(NULL) {}
733   static int parent_num_ids() { return IterationStatement::num_ids(); }
734
735  private:
736   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
737
738   Expression* cond_;
739 };
740
741
742 class ForStatement final : public IterationStatement {
743  public:
744   DECLARE_NODE_TYPE(ForStatement)
745
746   void Initialize(Statement* init,
747                   Expression* cond,
748                   Statement* next,
749                   Statement* body) {
750     IterationStatement::Initialize(body);
751     init_ = init;
752     cond_ = cond;
753     next_ = next;
754   }
755
756   Statement* init() const { return init_; }
757   Expression* cond() const { return cond_; }
758   Statement* next() const { return next_; }
759
760   static int num_ids() { return parent_num_ids() + 2; }
761   BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
762   BailoutId StackCheckId() const override { return BodyId(); }
763   BailoutId BodyId() const { return BailoutId(local_id(1)); }
764
765  protected:
766   ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
767       : IterationStatement(zone, labels, pos),
768         init_(NULL),
769         cond_(NULL),
770         next_(NULL) {}
771   static int parent_num_ids() { return IterationStatement::num_ids(); }
772
773  private:
774   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
775
776   Statement* init_;
777   Expression* cond_;
778   Statement* next_;
779 };
780
781
782 class ForEachStatement : public IterationStatement {
783  public:
784   enum VisitMode {
785     ENUMERATE,   // for (each in subject) body;
786     ITERATE      // for (each of subject) body;
787   };
788
789   void Initialize(Expression* each, Expression* subject, Statement* body) {
790     IterationStatement::Initialize(body);
791     each_ = each;
792     subject_ = subject;
793   }
794
795   Expression* each() const { return each_; }
796   Expression* subject() const { return subject_; }
797
798  protected:
799   ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
800       : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
801
802  private:
803   Expression* each_;
804   Expression* subject_;
805 };
806
807
808 class ForInStatement final : public ForEachStatement {
809  public:
810   DECLARE_NODE_TYPE(ForInStatement)
811
812   Expression* enumerable() const {
813     return subject();
814   }
815
816   // Type feedback information.
817   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
818       Isolate* isolate, const ICSlotCache* cache) override {
819     return FeedbackVectorRequirements(1, 0);
820   }
821   void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
822     for_in_feedback_slot_ = slot;
823   }
824
825   FeedbackVectorSlot ForInFeedbackSlot() {
826     DCHECK(!for_in_feedback_slot_.IsInvalid());
827     return for_in_feedback_slot_;
828   }
829
830   enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
831   ForInType for_in_type() const { return for_in_type_; }
832   void set_for_in_type(ForInType type) { for_in_type_ = type; }
833
834   static int num_ids() { return parent_num_ids() + 6; }
835   BailoutId BodyId() const { return BailoutId(local_id(0)); }
836   BailoutId PrepareId() const { return BailoutId(local_id(1)); }
837   BailoutId EnumId() const { return BailoutId(local_id(2)); }
838   BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
839   BailoutId FilterId() const { return BailoutId(local_id(4)); }
840   BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
841   BailoutId ContinueId() const override { return EntryId(); }
842   BailoutId StackCheckId() const override { return BodyId(); }
843
844  protected:
845   ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
846       : ForEachStatement(zone, labels, pos),
847         for_in_type_(SLOW_FOR_IN),
848         for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
849   static int parent_num_ids() { return ForEachStatement::num_ids(); }
850
851  private:
852   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
853
854   ForInType for_in_type_;
855   FeedbackVectorSlot for_in_feedback_slot_;
856 };
857
858
859 class ForOfStatement final : public ForEachStatement {
860  public:
861   DECLARE_NODE_TYPE(ForOfStatement)
862
863   void Initialize(Expression* each,
864                   Expression* subject,
865                   Statement* body,
866                   Expression* assign_iterator,
867                   Expression* next_result,
868                   Expression* result_done,
869                   Expression* assign_each) {
870     ForEachStatement::Initialize(each, subject, body);
871     assign_iterator_ = assign_iterator;
872     next_result_ = next_result;
873     result_done_ = result_done;
874     assign_each_ = assign_each;
875   }
876
877   Expression* iterable() const {
878     return subject();
879   }
880
881   // iterator = subject[Symbol.iterator]()
882   Expression* assign_iterator() const {
883     return assign_iterator_;
884   }
885
886   // result = iterator.next()  // with type check
887   Expression* next_result() const {
888     return next_result_;
889   }
890
891   // result.done
892   Expression* result_done() const {
893     return result_done_;
894   }
895
896   // each = result.value
897   Expression* assign_each() const {
898     return assign_each_;
899   }
900
901   BailoutId ContinueId() const override { return EntryId(); }
902   BailoutId StackCheckId() const override { return BackEdgeId(); }
903
904   static int num_ids() { return parent_num_ids() + 1; }
905   BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
906
907  protected:
908   ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
909       : ForEachStatement(zone, labels, pos),
910         assign_iterator_(NULL),
911         next_result_(NULL),
912         result_done_(NULL),
913         assign_each_(NULL) {}
914   static int parent_num_ids() { return ForEachStatement::num_ids(); }
915
916  private:
917   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
918
919   Expression* assign_iterator_;
920   Expression* next_result_;
921   Expression* result_done_;
922   Expression* assign_each_;
923 };
924
925
926 class ExpressionStatement final : public Statement {
927  public:
928   DECLARE_NODE_TYPE(ExpressionStatement)
929
930   void set_expression(Expression* e) { expression_ = e; }
931   Expression* expression() const { return expression_; }
932   bool IsJump() const override { return expression_->IsThrow(); }
933
934  protected:
935   ExpressionStatement(Zone* zone, Expression* expression, int pos)
936       : Statement(zone, pos), expression_(expression) { }
937
938  private:
939   Expression* expression_;
940 };
941
942
943 class JumpStatement : public Statement {
944  public:
945   bool IsJump() const final { return true; }
946
947  protected:
948   explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
949 };
950
951
952 class ContinueStatement final : public JumpStatement {
953  public:
954   DECLARE_NODE_TYPE(ContinueStatement)
955
956   IterationStatement* target() const { return target_; }
957
958  protected:
959   explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
960       : JumpStatement(zone, pos), target_(target) { }
961
962  private:
963   IterationStatement* target_;
964 };
965
966
967 class BreakStatement final : public JumpStatement {
968  public:
969   DECLARE_NODE_TYPE(BreakStatement)
970
971   BreakableStatement* target() const { return target_; }
972
973  protected:
974   explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
975       : JumpStatement(zone, pos), target_(target) { }
976
977  private:
978   BreakableStatement* target_;
979 };
980
981
982 class ReturnStatement final : public JumpStatement {
983  public:
984   DECLARE_NODE_TYPE(ReturnStatement)
985
986   Expression* expression() const { return expression_; }
987
988  protected:
989   explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
990       : JumpStatement(zone, pos), expression_(expression) { }
991
992  private:
993   Expression* expression_;
994 };
995
996
997 class WithStatement final : public Statement {
998  public:
999   DECLARE_NODE_TYPE(WithStatement)
1000
1001   Scope* scope() { return scope_; }
1002   Expression* expression() const { return expression_; }
1003   Statement* statement() const { return statement_; }
1004
1005   void set_base_id(int id) { base_id_ = id; }
1006   static int num_ids() { return parent_num_ids() + 1; }
1007   BailoutId EntryId() const { return BailoutId(local_id(0)); }
1008
1009  protected:
1010   WithStatement(Zone* zone, Scope* scope, Expression* expression,
1011                 Statement* statement, int pos)
1012       : Statement(zone, pos),
1013         scope_(scope),
1014         expression_(expression),
1015         statement_(statement),
1016         base_id_(BailoutId::None().ToInt()) {}
1017   static int parent_num_ids() { return 0; }
1018
1019   int base_id() const {
1020     DCHECK(!BailoutId(base_id_).IsNone());
1021     return base_id_;
1022   }
1023
1024  private:
1025   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1026
1027   Scope* scope_;
1028   Expression* expression_;
1029   Statement* statement_;
1030   int base_id_;
1031 };
1032
1033
1034 class CaseClause final : public Expression {
1035  public:
1036   DECLARE_NODE_TYPE(CaseClause)
1037
1038   bool is_default() const { return label_ == NULL; }
1039   Expression* label() const {
1040     CHECK(!is_default());
1041     return label_;
1042   }
1043   Label* body_target() { return &body_target_; }
1044   ZoneList<Statement*>* statements() const { return statements_; }
1045
1046   static int num_ids() { return parent_num_ids() + 2; }
1047   BailoutId EntryId() const { return BailoutId(local_id(0)); }
1048   TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1049
1050   Type* compare_type() { return compare_type_; }
1051   void set_compare_type(Type* type) { compare_type_ = type; }
1052
1053  protected:
1054   static int parent_num_ids() { return Expression::num_ids(); }
1055
1056  private:
1057   CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1058              int pos);
1059   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1060
1061   Expression* label_;
1062   Label body_target_;
1063   ZoneList<Statement*>* statements_;
1064   Type* compare_type_;
1065 };
1066
1067
1068 class SwitchStatement final : public BreakableStatement {
1069  public:
1070   DECLARE_NODE_TYPE(SwitchStatement)
1071
1072   void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1073     tag_ = tag;
1074     cases_ = cases;
1075   }
1076
1077   Expression* tag() const { return tag_; }
1078   ZoneList<CaseClause*>* cases() const { return cases_; }
1079
1080  protected:
1081   SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1082       : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1083         tag_(NULL),
1084         cases_(NULL) {}
1085
1086  private:
1087   Expression* tag_;
1088   ZoneList<CaseClause*>* cases_;
1089 };
1090
1091
1092 // If-statements always have non-null references to their then- and
1093 // else-parts. When parsing if-statements with no explicit else-part,
1094 // the parser implicitly creates an empty statement. Use the
1095 // HasThenStatement() and HasElseStatement() functions to check if a
1096 // given if-statement has a then- or an else-part containing code.
1097 class IfStatement final : public Statement {
1098  public:
1099   DECLARE_NODE_TYPE(IfStatement)
1100
1101   bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1102   bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1103
1104   Expression* condition() const { return condition_; }
1105   Statement* then_statement() const { return then_statement_; }
1106   Statement* else_statement() const { return else_statement_; }
1107
1108   bool IsJump() const override {
1109     return HasThenStatement() && then_statement()->IsJump()
1110         && HasElseStatement() && else_statement()->IsJump();
1111   }
1112
1113   void set_base_id(int id) { base_id_ = id; }
1114   static int num_ids() { return parent_num_ids() + 3; }
1115   BailoutId IfId() const { return BailoutId(local_id(0)); }
1116   BailoutId ThenId() const { return BailoutId(local_id(1)); }
1117   BailoutId ElseId() const { return BailoutId(local_id(2)); }
1118
1119  protected:
1120   IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1121               Statement* else_statement, int pos)
1122       : Statement(zone, pos),
1123         condition_(condition),
1124         then_statement_(then_statement),
1125         else_statement_(else_statement),
1126         base_id_(BailoutId::None().ToInt()) {}
1127   static int parent_num_ids() { return 0; }
1128
1129   int base_id() const {
1130     DCHECK(!BailoutId(base_id_).IsNone());
1131     return base_id_;
1132   }
1133
1134  private:
1135   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1136
1137   Expression* condition_;
1138   Statement* then_statement_;
1139   Statement* else_statement_;
1140   int base_id_;
1141 };
1142
1143
1144 class TryStatement : public Statement {
1145  public:
1146   int index() const { return index_; }
1147   Block* try_block() const { return try_block_; }
1148
1149  protected:
1150   TryStatement(Zone* zone, int index, Block* try_block, int pos)
1151       : Statement(zone, pos), index_(index), try_block_(try_block) {}
1152
1153  private:
1154   // Unique (per-function) index of this handler.  This is not an AST ID.
1155   int index_;
1156
1157   Block* try_block_;
1158 };
1159
1160
1161 class TryCatchStatement final : public TryStatement {
1162  public:
1163   DECLARE_NODE_TYPE(TryCatchStatement)
1164
1165   Scope* scope() { return scope_; }
1166   Variable* variable() { return variable_; }
1167   Block* catch_block() const { return catch_block_; }
1168
1169  protected:
1170   TryCatchStatement(Zone* zone,
1171                     int index,
1172                     Block* try_block,
1173                     Scope* scope,
1174                     Variable* variable,
1175                     Block* catch_block,
1176                     int pos)
1177       : TryStatement(zone, index, try_block, pos),
1178         scope_(scope),
1179         variable_(variable),
1180         catch_block_(catch_block) {
1181   }
1182
1183  private:
1184   Scope* scope_;
1185   Variable* variable_;
1186   Block* catch_block_;
1187 };
1188
1189
1190 class TryFinallyStatement final : public TryStatement {
1191  public:
1192   DECLARE_NODE_TYPE(TryFinallyStatement)
1193
1194   Block* finally_block() const { return finally_block_; }
1195
1196  protected:
1197   TryFinallyStatement(
1198       Zone* zone, int index, Block* try_block, Block* finally_block, int pos)
1199       : TryStatement(zone, index, try_block, pos),
1200         finally_block_(finally_block) { }
1201
1202  private:
1203   Block* finally_block_;
1204 };
1205
1206
1207 class DebuggerStatement final : public Statement {
1208  public:
1209   DECLARE_NODE_TYPE(DebuggerStatement)
1210
1211   void set_base_id(int id) { base_id_ = id; }
1212   static int num_ids() { return parent_num_ids() + 1; }
1213   BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1214
1215  protected:
1216   explicit DebuggerStatement(Zone* zone, int pos)
1217       : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1218   static int parent_num_ids() { return 0; }
1219
1220   int base_id() const {
1221     DCHECK(!BailoutId(base_id_).IsNone());
1222     return base_id_;
1223   }
1224
1225  private:
1226   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1227
1228   int base_id_;
1229 };
1230
1231
1232 class EmptyStatement final : public Statement {
1233  public:
1234   DECLARE_NODE_TYPE(EmptyStatement)
1235
1236  protected:
1237   explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1238 };
1239
1240
1241 class Literal final : public Expression {
1242  public:
1243   DECLARE_NODE_TYPE(Literal)
1244
1245   bool IsPropertyName() const override { return value_->IsPropertyName(); }
1246
1247   Handle<String> AsPropertyName() {
1248     DCHECK(IsPropertyName());
1249     return Handle<String>::cast(value());
1250   }
1251
1252   const AstRawString* AsRawPropertyName() {
1253     DCHECK(IsPropertyName());
1254     return value_->AsString();
1255   }
1256
1257   bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1258   bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1259
1260   Handle<Object> value() const { return value_->value(); }
1261   const AstValue* raw_value() const { return value_; }
1262
1263   // Support for using Literal as a HashMap key. NOTE: Currently, this works
1264   // only for string and number literals!
1265   uint32_t Hash();
1266   static bool Match(void* literal1, void* literal2);
1267
1268   static int num_ids() { return parent_num_ids() + 1; }
1269   TypeFeedbackId LiteralFeedbackId() const {
1270     return TypeFeedbackId(local_id(0));
1271   }
1272
1273  protected:
1274   Literal(Zone* zone, const AstValue* value, int position)
1275       : Expression(zone, position), value_(value) {}
1276   static int parent_num_ids() { return Expression::num_ids(); }
1277
1278  private:
1279   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1280
1281   const AstValue* value_;
1282 };
1283
1284
1285 // Base class for literals that needs space in the corresponding JSFunction.
1286 class MaterializedLiteral : public Expression {
1287  public:
1288   virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1289
1290   int literal_index() { return literal_index_; }
1291
1292   int depth() const {
1293     // only callable after initialization.
1294     DCHECK(depth_ >= 1);
1295     return depth_;
1296   }
1297
1298  protected:
1299   MaterializedLiteral(Zone* zone, int literal_index, int pos)
1300       : Expression(zone, pos),
1301         literal_index_(literal_index),
1302         is_simple_(false),
1303         depth_(0) {}
1304
1305   // A materialized literal is simple if the values consist of only
1306   // constants and simple object and array literals.
1307   bool is_simple() const { return is_simple_; }
1308   void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1309   friend class CompileTimeValue;
1310
1311   void set_depth(int depth) {
1312     DCHECK(depth >= 1);
1313     depth_ = depth;
1314   }
1315
1316   // Populate the constant properties/elements fixed array.
1317   void BuildConstants(Isolate* isolate);
1318   friend class ArrayLiteral;
1319   friend class ObjectLiteral;
1320
1321   // If the expression is a literal, return the literal value;
1322   // if the expression is a materialized literal and is simple return a
1323   // compile time value as encoded by CompileTimeValue::GetValue().
1324   // Otherwise, return undefined literal as the placeholder
1325   // in the object literal boilerplate.
1326   Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1327
1328  private:
1329   int literal_index_;
1330   bool is_simple_;
1331   int depth_;
1332 };
1333
1334
1335 // Property is used for passing information
1336 // about an object literal's properties from the parser
1337 // to the code generator.
1338 class ObjectLiteralProperty final : public ZoneObject {
1339  public:
1340   enum Kind {
1341     CONSTANT,              // Property with constant value (compile time).
1342     COMPUTED,              // Property with computed value (execution time).
1343     MATERIALIZED_LITERAL,  // Property value is a materialized literal.
1344     GETTER, SETTER,        // Property is an accessor function.
1345     PROTOTYPE              // Property is __proto__.
1346   };
1347
1348   Expression* key() { return key_; }
1349   Expression* value() { return value_; }
1350   Kind kind() { return kind_; }
1351
1352   // Type feedback information.
1353   bool IsMonomorphic() { return !receiver_type_.is_null(); }
1354   Handle<Map> GetReceiverType() { return receiver_type_; }
1355
1356   bool IsCompileTimeValue();
1357
1358   void set_emit_store(bool emit_store);
1359   bool emit_store();
1360
1361   bool is_static() const { return is_static_; }
1362   bool is_computed_name() const { return is_computed_name_; }
1363
1364   void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1365
1366  protected:
1367   friend class AstNodeFactory;
1368
1369   ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1370                         bool is_static, bool is_computed_name);
1371   ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1372                         Expression* value, bool is_static,
1373                         bool is_computed_name);
1374
1375  private:
1376   Expression* key_;
1377   Expression* value_;
1378   Kind kind_;
1379   bool emit_store_;
1380   bool is_static_;
1381   bool is_computed_name_;
1382   Handle<Map> receiver_type_;
1383 };
1384
1385
1386 // An object literal has a boilerplate object that is used
1387 // for minimizing the work when constructing it at runtime.
1388 class ObjectLiteral final : public MaterializedLiteral {
1389  public:
1390   typedef ObjectLiteralProperty Property;
1391
1392   DECLARE_NODE_TYPE(ObjectLiteral)
1393
1394   Handle<FixedArray> constant_properties() const {
1395     return constant_properties_;
1396   }
1397   int properties_count() const { return constant_properties_->length() / 2; }
1398   ZoneList<Property*>* properties() const { return properties_; }
1399   bool fast_elements() const { return fast_elements_; }
1400   bool may_store_doubles() const { return may_store_doubles_; }
1401   bool has_function() const { return has_function_; }
1402   bool has_elements() const { return has_elements_; }
1403
1404   // Decide if a property should be in the object boilerplate.
1405   static bool IsBoilerplateProperty(Property* property);
1406
1407   // Populate the constant properties fixed array.
1408   void BuildConstantProperties(Isolate* isolate);
1409
1410   // Mark all computed expressions that are bound to a key that
1411   // is shadowed by a later occurrence of the same key. For the
1412   // marked expressions, no store code is emitted.
1413   void CalculateEmitStore(Zone* zone);
1414
1415   // Assemble bitfield of flags for the CreateObjectLiteral helper.
1416   int ComputeFlags(bool disable_mementos = false) const {
1417     int flags = fast_elements() ? kFastElements : kNoFlags;
1418     flags |= has_function() ? kHasFunction : kNoFlags;
1419     if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1420       flags |= kShallowProperties;
1421     }
1422     if (disable_mementos) {
1423       flags |= kDisableMementos;
1424     }
1425     return flags;
1426   }
1427
1428   enum Flags {
1429     kNoFlags = 0,
1430     kFastElements = 1,
1431     kHasFunction = 1 << 1,
1432     kShallowProperties = 1 << 2,
1433     kDisableMementos = 1 << 3
1434   };
1435
1436   struct Accessors: public ZoneObject {
1437     Accessors() : getter(NULL), setter(NULL) {}
1438     Expression* getter;
1439     Expression* setter;
1440   };
1441
1442   BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1443
1444   // Return an AST id for a property that is used in simulate instructions.
1445   BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1446
1447   // Unlike other AST nodes, this number of bailout IDs allocated for an
1448   // ObjectLiteral can vary, so num_ids() is not a static method.
1449   int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1450
1451  protected:
1452   ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1453                 int boilerplate_properties, bool has_function, int pos)
1454       : MaterializedLiteral(zone, literal_index, pos),
1455         properties_(properties),
1456         boilerplate_properties_(boilerplate_properties),
1457         fast_elements_(false),
1458         has_elements_(false),
1459         may_store_doubles_(false),
1460         has_function_(has_function) {}
1461   static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1462
1463  private:
1464   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1465   Handle<FixedArray> constant_properties_;
1466   ZoneList<Property*>* properties_;
1467   int boilerplate_properties_;
1468   bool fast_elements_;
1469   bool has_elements_;
1470   bool may_store_doubles_;
1471   bool has_function_;
1472 };
1473
1474
1475 // Node for capturing a regexp literal.
1476 class RegExpLiteral final : public MaterializedLiteral {
1477  public:
1478   DECLARE_NODE_TYPE(RegExpLiteral)
1479
1480   Handle<String> pattern() const { return pattern_->string(); }
1481   Handle<String> flags() const { return flags_->string(); }
1482
1483  protected:
1484   RegExpLiteral(Zone* zone, const AstRawString* pattern,
1485                 const AstRawString* flags, int literal_index, int pos)
1486       : MaterializedLiteral(zone, literal_index, pos),
1487         pattern_(pattern),
1488         flags_(flags) {
1489     set_depth(1);
1490   }
1491
1492  private:
1493   const AstRawString* pattern_;
1494   const AstRawString* flags_;
1495 };
1496
1497
1498 // An array literal has a literals object that is used
1499 // for minimizing the work when constructing it at runtime.
1500 class ArrayLiteral final : public MaterializedLiteral {
1501  public:
1502   DECLARE_NODE_TYPE(ArrayLiteral)
1503
1504   Handle<FixedArray> constant_elements() const { return constant_elements_; }
1505   ElementsKind constant_elements_kind() const {
1506     DCHECK_EQ(2, constant_elements_->length());
1507     return static_cast<ElementsKind>(
1508         Smi::cast(constant_elements_->get(0))->value());
1509   }
1510
1511   ZoneList<Expression*>* values() const { return values_; }
1512
1513   BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1514
1515   // Return an AST id for an element that is used in simulate instructions.
1516   BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1517
1518   // Unlike other AST nodes, this number of bailout IDs allocated for an
1519   // ArrayLiteral can vary, so num_ids() is not a static method.
1520   int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1521
1522   // Populate the constant elements fixed array.
1523   void BuildConstantElements(Isolate* isolate);
1524
1525   // Assemble bitfield of flags for the CreateArrayLiteral helper.
1526   int ComputeFlags(bool disable_mementos = false) const {
1527     int flags = depth() == 1 ? kShallowElements : kNoFlags;
1528     if (disable_mementos) {
1529       flags |= kDisableMementos;
1530     }
1531     return flags;
1532   }
1533
1534   enum Flags {
1535     kNoFlags = 0,
1536     kShallowElements = 1,
1537     kDisableMementos = 1 << 1
1538   };
1539
1540  protected:
1541   ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1542                int pos)
1543       : MaterializedLiteral(zone, literal_index, pos), values_(values) {}
1544   static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1545
1546  private:
1547   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1548
1549   Handle<FixedArray> constant_elements_;
1550   ZoneList<Expression*>* values_;
1551 };
1552
1553
1554 class VariableProxy final : public Expression {
1555  public:
1556   DECLARE_NODE_TYPE(VariableProxy)
1557
1558   bool IsValidReferenceExpression() const override { return !is_this(); }
1559
1560   bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1561
1562   Handle<String> name() const { return raw_name()->string(); }
1563   const AstRawString* raw_name() const {
1564     return is_resolved() ? var_->raw_name() : raw_name_;
1565   }
1566
1567   Variable* var() const {
1568     DCHECK(is_resolved());
1569     return var_;
1570   }
1571   void set_var(Variable* v) {
1572     DCHECK(!is_resolved());
1573     DCHECK_NOT_NULL(v);
1574     var_ = v;
1575   }
1576
1577   bool is_this() const { return IsThisField::decode(bit_field_); }
1578
1579   bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1580   void set_is_assigned() {
1581     bit_field_ = IsAssignedField::update(bit_field_, true);
1582   }
1583
1584   bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1585   void set_is_resolved() {
1586     bit_field_ = IsResolvedField::update(bit_field_, true);
1587   }
1588
1589   int end_position() const { return end_position_; }
1590
1591   // Bind this proxy to the variable var.
1592   void BindTo(Variable* var);
1593
1594   bool UsesVariableFeedbackSlot() const {
1595     return FLAG_vector_ics && (var()->IsUnallocated() || var()->IsLookupSlot());
1596   }
1597
1598   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1599       Isolate* isolate, const ICSlotCache* cache) override;
1600
1601   void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1602                               ICSlotCache* cache) override;
1603   Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1604   FeedbackVectorICSlot VariableFeedbackSlot() {
1605     DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1606     return variable_feedback_slot_;
1607   }
1608
1609   static int num_ids() { return parent_num_ids() + 1; }
1610   BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1611
1612  protected:
1613   VariableProxy(Zone* zone, Variable* var, int start_position,
1614                 int end_position);
1615
1616   VariableProxy(Zone* zone, const AstRawString* name,
1617                 Variable::Kind variable_kind, int start_position,
1618                 int end_position);
1619   static int parent_num_ids() { return Expression::num_ids(); }
1620   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1621
1622   class IsThisField : public BitField8<bool, 0, 1> {};
1623   class IsAssignedField : public BitField8<bool, 1, 1> {};
1624   class IsResolvedField : public BitField8<bool, 2, 1> {};
1625
1626   // Start with 16-bit (or smaller) field, which should get packed together
1627   // with Expression's trailing 16-bit field.
1628   uint8_t bit_field_;
1629   FeedbackVectorICSlot variable_feedback_slot_;
1630   union {
1631     const AstRawString* raw_name_;  // if !is_resolved_
1632     Variable* var_;                 // if is_resolved_
1633   };
1634   // Position is stored in the AstNode superclass, but VariableProxy needs to
1635   // know its end position too (for error messages). It cannot be inferred from
1636   // the variable name length because it can contain escapes.
1637   int end_position_;
1638 };
1639
1640
1641 class Property final : public Expression {
1642  public:
1643   DECLARE_NODE_TYPE(Property)
1644
1645   bool IsValidReferenceExpression() const override { return true; }
1646
1647   Expression* obj() const { return obj_; }
1648   Expression* key() const { return key_; }
1649
1650   static int num_ids() { return parent_num_ids() + 2; }
1651   BailoutId LoadId() const { return BailoutId(local_id(0)); }
1652   TypeFeedbackId PropertyFeedbackId() { return TypeFeedbackId(local_id(1)); }
1653
1654   bool IsStringAccess() const {
1655     return IsStringAccessField::decode(bit_field_);
1656   }
1657
1658   // Type feedback information.
1659   bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1660   SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1661   KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1662   IcCheckType GetKeyType() const override {
1663     return KeyTypeField::decode(bit_field_);
1664   }
1665   bool IsUninitialized() const {
1666     return !is_for_call() && HasNoTypeInformation();
1667   }
1668   bool HasNoTypeInformation() const {
1669     return GetInlineCacheState() == UNINITIALIZED;
1670   }
1671   InlineCacheState GetInlineCacheState() const {
1672     return InlineCacheStateField::decode(bit_field_);
1673   }
1674   void set_is_string_access(bool b) {
1675     bit_field_ = IsStringAccessField::update(bit_field_, b);
1676   }
1677   void set_key_type(IcCheckType key_type) {
1678     bit_field_ = KeyTypeField::update(bit_field_, key_type);
1679   }
1680   void set_inline_cache_state(InlineCacheState state) {
1681     bit_field_ = InlineCacheStateField::update(bit_field_, state);
1682   }
1683   void mark_for_call() {
1684     bit_field_ = IsForCallField::update(bit_field_, true);
1685   }
1686   bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1687
1688   bool IsSuperAccess() {
1689     return obj()->IsSuperReference();
1690   }
1691
1692   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1693       Isolate* isolate, const ICSlotCache* cache) override {
1694     return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
1695   }
1696   void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1697                               ICSlotCache* cache) override {
1698     property_feedback_slot_ = slot;
1699   }
1700   Code::Kind FeedbackICSlotKind(int index) override {
1701     return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1702   }
1703
1704   FeedbackVectorICSlot PropertyFeedbackSlot() const {
1705     DCHECK(!FLAG_vector_ics || !property_feedback_slot_.IsInvalid());
1706     return property_feedback_slot_;
1707   }
1708
1709  protected:
1710   Property(Zone* zone, Expression* obj, Expression* key, int pos)
1711       : Expression(zone, pos),
1712         bit_field_(IsForCallField::encode(false) |
1713                    IsStringAccessField::encode(false) |
1714                    InlineCacheStateField::encode(UNINITIALIZED)),
1715         property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1716         obj_(obj),
1717         key_(key) {}
1718   static int parent_num_ids() { return Expression::num_ids(); }
1719
1720  private:
1721   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1722
1723   class IsForCallField : public BitField8<bool, 0, 1> {};
1724   class IsStringAccessField : public BitField8<bool, 1, 1> {};
1725   class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1726   class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1727   uint8_t bit_field_;
1728   FeedbackVectorICSlot property_feedback_slot_;
1729   Expression* obj_;
1730   Expression* key_;
1731   SmallMapList receiver_types_;
1732 };
1733
1734
1735 class Call final : public Expression {
1736  public:
1737   DECLARE_NODE_TYPE(Call)
1738
1739   Expression* expression() const { return expression_; }
1740   ZoneList<Expression*>* arguments() const { return arguments_; }
1741
1742   // Type feedback information.
1743   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1744       Isolate* isolate, const ICSlotCache* cache) override;
1745   void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1746                               ICSlotCache* cache) override {
1747     ic_slot_or_slot_ = slot.ToInt();
1748   }
1749   void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1750     ic_slot_or_slot_ = slot.ToInt();
1751   }
1752   Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1753
1754   FeedbackVectorSlot CallFeedbackSlot() const {
1755     DCHECK(ic_slot_or_slot_ != FeedbackVectorSlot::Invalid().ToInt());
1756     return FeedbackVectorSlot(ic_slot_or_slot_);
1757   }
1758
1759   FeedbackVectorICSlot CallFeedbackICSlot() const {
1760     DCHECK(ic_slot_or_slot_ != FeedbackVectorICSlot::Invalid().ToInt());
1761     return FeedbackVectorICSlot(ic_slot_or_slot_);
1762   }
1763
1764   SmallMapList* GetReceiverTypes() override {
1765     if (expression()->IsProperty()) {
1766       return expression()->AsProperty()->GetReceiverTypes();
1767     }
1768     return NULL;
1769   }
1770
1771   bool IsMonomorphic() override {
1772     if (expression()->IsProperty()) {
1773       return expression()->AsProperty()->IsMonomorphic();
1774     }
1775     return !target_.is_null();
1776   }
1777
1778   bool global_call() const {
1779     VariableProxy* proxy = expression_->AsVariableProxy();
1780     return proxy != NULL && proxy->var()->IsUnallocated();
1781   }
1782
1783   bool known_global_function() const {
1784     return global_call() && !target_.is_null();
1785   }
1786
1787   Handle<JSFunction> target() { return target_; }
1788
1789   Handle<AllocationSite> allocation_site() { return allocation_site_; }
1790
1791   void SetKnownGlobalTarget(Handle<JSFunction> target) {
1792     target_ = target;
1793     set_is_uninitialized(false);
1794   }
1795   void set_target(Handle<JSFunction> target) { target_ = target; }
1796   void set_allocation_site(Handle<AllocationSite> site) {
1797     allocation_site_ = site;
1798   }
1799
1800   static int num_ids() { return parent_num_ids() + 2; }
1801   BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1802   BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1803
1804   bool is_uninitialized() const {
1805     return IsUninitializedField::decode(bit_field_);
1806   }
1807   void set_is_uninitialized(bool b) {
1808     bit_field_ = IsUninitializedField::update(bit_field_, b);
1809   }
1810
1811   enum CallType {
1812     POSSIBLY_EVAL_CALL,
1813     GLOBAL_CALL,
1814     LOOKUP_SLOT_CALL,
1815     PROPERTY_CALL,
1816     SUPER_CALL,
1817     OTHER_CALL
1818   };
1819
1820   // Helpers to determine how to handle the call.
1821   CallType GetCallType(Isolate* isolate) const;
1822   bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1823   bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1824
1825 #ifdef DEBUG
1826   // Used to assert that the FullCodeGenerator records the return site.
1827   bool return_is_recorded_;
1828 #endif
1829
1830  protected:
1831   Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1832        int pos)
1833       : Expression(zone, pos),
1834         ic_slot_or_slot_(FeedbackVectorICSlot::Invalid().ToInt()),
1835         expression_(expression),
1836         arguments_(arguments),
1837         bit_field_(IsUninitializedField::encode(false)) {
1838     if (expression->IsProperty()) {
1839       expression->AsProperty()->mark_for_call();
1840     }
1841   }
1842   static int parent_num_ids() { return Expression::num_ids(); }
1843
1844  private:
1845   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1846
1847   // We store this as an integer because we don't know if we have a slot or
1848   // an ic slot until scoping time.
1849   int ic_slot_or_slot_;
1850   Expression* expression_;
1851   ZoneList<Expression*>* arguments_;
1852   Handle<JSFunction> target_;
1853   Handle<AllocationSite> allocation_site_;
1854   class IsUninitializedField : public BitField8<bool, 0, 1> {};
1855   uint8_t bit_field_;
1856 };
1857
1858
1859 class CallNew final : public Expression {
1860  public:
1861   DECLARE_NODE_TYPE(CallNew)
1862
1863   Expression* expression() const { return expression_; }
1864   ZoneList<Expression*>* arguments() const { return arguments_; }
1865
1866   // Type feedback information.
1867   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1868       Isolate* isolate, const ICSlotCache* cache) override {
1869     return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1870   }
1871   void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1872     callnew_feedback_slot_ = slot;
1873   }
1874
1875   FeedbackVectorSlot CallNewFeedbackSlot() {
1876     DCHECK(!callnew_feedback_slot_.IsInvalid());
1877     return callnew_feedback_slot_;
1878   }
1879   FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1880     DCHECK(FLAG_pretenuring_call_new);
1881     return CallNewFeedbackSlot().next();
1882   }
1883
1884   bool IsMonomorphic() override { return is_monomorphic_; }
1885   Handle<JSFunction> target() const { return target_; }
1886   Handle<AllocationSite> allocation_site() const {
1887     return allocation_site_;
1888   }
1889
1890   static int num_ids() { return parent_num_ids() + 1; }
1891   static int feedback_slots() { return 1; }
1892   BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1893
1894   void set_allocation_site(Handle<AllocationSite> site) {
1895     allocation_site_ = site;
1896   }
1897   void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1898   void set_target(Handle<JSFunction> target) { target_ = target; }
1899   void SetKnownGlobalTarget(Handle<JSFunction> target) {
1900     target_ = target;
1901     is_monomorphic_ = true;
1902   }
1903
1904  protected:
1905   CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1906           int pos)
1907       : Expression(zone, pos),
1908         expression_(expression),
1909         arguments_(arguments),
1910         is_monomorphic_(false),
1911         callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1912
1913   static int parent_num_ids() { return Expression::num_ids(); }
1914
1915  private:
1916   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1917
1918   Expression* expression_;
1919   ZoneList<Expression*>* arguments_;
1920   bool is_monomorphic_;
1921   Handle<JSFunction> target_;
1922   Handle<AllocationSite> allocation_site_;
1923   FeedbackVectorSlot callnew_feedback_slot_;
1924 };
1925
1926
1927 // The CallRuntime class does not represent any official JavaScript
1928 // language construct. Instead it is used to call a C or JS function
1929 // with a set of arguments. This is used from the builtins that are
1930 // implemented in JavaScript (see "v8natives.js").
1931 class CallRuntime final : public Expression {
1932  public:
1933   DECLARE_NODE_TYPE(CallRuntime)
1934
1935   Handle<String> name() const { return raw_name_->string(); }
1936   const AstRawString* raw_name() const { return raw_name_; }
1937   const Runtime::Function* function() const { return function_; }
1938   ZoneList<Expression*>* arguments() const { return arguments_; }
1939   bool is_jsruntime() const { return function_ == NULL; }
1940
1941   // Type feedback information.
1942   bool HasCallRuntimeFeedbackSlot() const {
1943     return FLAG_vector_ics && is_jsruntime();
1944   }
1945   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1946       Isolate* isolate, const ICSlotCache* cache) override {
1947     return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
1948   }
1949   void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1950                               ICSlotCache* cache) override {
1951     callruntime_feedback_slot_ = slot;
1952   }
1953   Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1954
1955   FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
1956     DCHECK(!HasCallRuntimeFeedbackSlot() ||
1957            !callruntime_feedback_slot_.IsInvalid());
1958     return callruntime_feedback_slot_;
1959   }
1960
1961   static int num_ids() { return parent_num_ids() + 1; }
1962   TypeFeedbackId CallRuntimeFeedbackId() const {
1963     return TypeFeedbackId(local_id(0));
1964   }
1965
1966  protected:
1967   CallRuntime(Zone* zone, const AstRawString* name,
1968               const Runtime::Function* function,
1969               ZoneList<Expression*>* arguments, int pos)
1970       : Expression(zone, pos),
1971         raw_name_(name),
1972         function_(function),
1973         arguments_(arguments),
1974         callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
1975   static int parent_num_ids() { return Expression::num_ids(); }
1976
1977  private:
1978   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1979
1980   const AstRawString* raw_name_;
1981   const Runtime::Function* function_;
1982   ZoneList<Expression*>* arguments_;
1983   FeedbackVectorICSlot callruntime_feedback_slot_;
1984 };
1985
1986
1987 class UnaryOperation final : public Expression {
1988  public:
1989   DECLARE_NODE_TYPE(UnaryOperation)
1990
1991   Token::Value op() const { return op_; }
1992   Expression* expression() const { return expression_; }
1993
1994   // For unary not (Token::NOT), the AST ids where true and false will
1995   // actually be materialized, respectively.
1996   static int num_ids() { return parent_num_ids() + 2; }
1997   BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
1998   BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
1999
2000   virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2001
2002  protected:
2003   UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2004       : Expression(zone, pos), op_(op), expression_(expression) {
2005     DCHECK(Token::IsUnaryOp(op));
2006   }
2007   static int parent_num_ids() { return Expression::num_ids(); }
2008
2009  private:
2010   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2011
2012   Token::Value op_;
2013   Expression* expression_;
2014 };
2015
2016
2017 class BinaryOperation final : public Expression {
2018  public:
2019   DECLARE_NODE_TYPE(BinaryOperation)
2020
2021   Token::Value op() const { return static_cast<Token::Value>(op_); }
2022   Expression* left() const { return left_; }
2023   Expression* right() const { return right_; }
2024   Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2025   void set_allocation_site(Handle<AllocationSite> allocation_site) {
2026     allocation_site_ = allocation_site;
2027   }
2028
2029   // The short-circuit logical operations need an AST ID for their
2030   // right-hand subexpression.
2031   static int num_ids() { return parent_num_ids() + 2; }
2032   BailoutId RightId() const { return BailoutId(local_id(0)); }
2033
2034   TypeFeedbackId BinaryOperationFeedbackId() const {
2035     return TypeFeedbackId(local_id(1));
2036   }
2037   Maybe<int> fixed_right_arg() const {
2038     return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2039   }
2040   void set_fixed_right_arg(Maybe<int> arg) {
2041     has_fixed_right_arg_ = arg.IsJust();
2042     if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2043   }
2044
2045   virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2046
2047  protected:
2048   BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2049                   Expression* right, int pos)
2050       : Expression(zone, pos),
2051         op_(static_cast<byte>(op)),
2052         has_fixed_right_arg_(false),
2053         fixed_right_arg_value_(0),
2054         left_(left),
2055         right_(right) {
2056     DCHECK(Token::IsBinaryOp(op));
2057   }
2058   static int parent_num_ids() { return Expression::num_ids(); }
2059
2060  private:
2061   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2062
2063   const byte op_;  // actually Token::Value
2064   // TODO(rossberg): the fixed arg should probably be represented as a Constant
2065   // type for the RHS. Currenty it's actually a Maybe<int>
2066   bool has_fixed_right_arg_;
2067   int fixed_right_arg_value_;
2068   Expression* left_;
2069   Expression* right_;
2070   Handle<AllocationSite> allocation_site_;
2071 };
2072
2073
2074 class CountOperation final : public Expression {
2075  public:
2076   DECLARE_NODE_TYPE(CountOperation)
2077
2078   bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2079   bool is_postfix() const { return !is_prefix(); }
2080
2081   Token::Value op() const { return TokenField::decode(bit_field_); }
2082   Token::Value binary_op() {
2083     return (op() == Token::INC) ? Token::ADD : Token::SUB;
2084   }
2085
2086   Expression* expression() const { return expression_; }
2087
2088   bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2089   SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2090   IcCheckType GetKeyType() const override {
2091     return KeyTypeField::decode(bit_field_);
2092   }
2093   KeyedAccessStoreMode GetStoreMode() const override {
2094     return StoreModeField::decode(bit_field_);
2095   }
2096   Type* type() const { return type_; }
2097   void set_key_type(IcCheckType type) {
2098     bit_field_ = KeyTypeField::update(bit_field_, type);
2099   }
2100   void set_store_mode(KeyedAccessStoreMode mode) {
2101     bit_field_ = StoreModeField::update(bit_field_, mode);
2102   }
2103   void set_type(Type* type) { type_ = type; }
2104
2105   static int num_ids() { return parent_num_ids() + 4; }
2106   BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2107   BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2108   TypeFeedbackId CountBinOpFeedbackId() const {
2109     return TypeFeedbackId(local_id(2));
2110   }
2111   TypeFeedbackId CountStoreFeedbackId() const {
2112     return TypeFeedbackId(local_id(3));
2113   }
2114
2115  protected:
2116   CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2117                  int pos)
2118       : Expression(zone, pos),
2119         bit_field_(IsPrefixField::encode(is_prefix) |
2120                    KeyTypeField::encode(ELEMENT) |
2121                    StoreModeField::encode(STANDARD_STORE) |
2122                    TokenField::encode(op)),
2123         type_(NULL),
2124         expression_(expr) {}
2125   static int parent_num_ids() { return Expression::num_ids(); }
2126
2127  private:
2128   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2129
2130   class IsPrefixField : public BitField16<bool, 0, 1> {};
2131   class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2132   class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2133   class TokenField : public BitField16<Token::Value, 6, 8> {};
2134
2135   // Starts with 16-bit field, which should get packed together with
2136   // Expression's trailing 16-bit field.
2137   uint16_t bit_field_;
2138   Type* type_;
2139   Expression* expression_;
2140   SmallMapList receiver_types_;
2141 };
2142
2143
2144 class CompareOperation final : public Expression {
2145  public:
2146   DECLARE_NODE_TYPE(CompareOperation)
2147
2148   Token::Value op() const { return op_; }
2149   Expression* left() const { return left_; }
2150   Expression* right() const { return right_; }
2151
2152   // Type feedback information.
2153   static int num_ids() { return parent_num_ids() + 1; }
2154   TypeFeedbackId CompareOperationFeedbackId() const {
2155     return TypeFeedbackId(local_id(0));
2156   }
2157   Type* combined_type() const { return combined_type_; }
2158   void set_combined_type(Type* type) { combined_type_ = type; }
2159
2160   // Match special cases.
2161   bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2162   bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2163   bool IsLiteralCompareNull(Expression** expr);
2164
2165  protected:
2166   CompareOperation(Zone* zone, Token::Value op, Expression* left,
2167                    Expression* right, int pos)
2168       : Expression(zone, pos),
2169         op_(op),
2170         left_(left),
2171         right_(right),
2172         combined_type_(Type::None(zone)) {
2173     DCHECK(Token::IsCompareOp(op));
2174   }
2175   static int parent_num_ids() { return Expression::num_ids(); }
2176
2177  private:
2178   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2179
2180   Token::Value op_;
2181   Expression* left_;
2182   Expression* right_;
2183
2184   Type* combined_type_;
2185 };
2186
2187
2188 class Spread final : public Expression {
2189  public:
2190   DECLARE_NODE_TYPE(Spread)
2191
2192   Expression* expression() const { return expression_; }
2193
2194   static int num_ids() { return parent_num_ids(); }
2195
2196  protected:
2197   Spread(Zone* zone, Expression* expression, int pos)
2198       : Expression(zone, pos), expression_(expression) {}
2199   static int parent_num_ids() { return Expression::num_ids(); }
2200
2201  private:
2202   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2203
2204   Expression* expression_;
2205 };
2206
2207
2208 class Conditional final : public Expression {
2209  public:
2210   DECLARE_NODE_TYPE(Conditional)
2211
2212   Expression* condition() const { return condition_; }
2213   Expression* then_expression() const { return then_expression_; }
2214   Expression* else_expression() const { return else_expression_; }
2215
2216   static int num_ids() { return parent_num_ids() + 2; }
2217   BailoutId ThenId() const { return BailoutId(local_id(0)); }
2218   BailoutId ElseId() const { return BailoutId(local_id(1)); }
2219
2220  protected:
2221   Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2222               Expression* else_expression, int position)
2223       : Expression(zone, position),
2224         condition_(condition),
2225         then_expression_(then_expression),
2226         else_expression_(else_expression) {}
2227   static int parent_num_ids() { return Expression::num_ids(); }
2228
2229  private:
2230   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2231
2232   Expression* condition_;
2233   Expression* then_expression_;
2234   Expression* else_expression_;
2235 };
2236
2237
2238 class Assignment final : public Expression {
2239  public:
2240   DECLARE_NODE_TYPE(Assignment)
2241
2242   Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2243
2244   Token::Value binary_op() const;
2245
2246   Token::Value op() const { return TokenField::decode(bit_field_); }
2247   Expression* target() const { return target_; }
2248   Expression* value() const { return value_; }
2249   BinaryOperation* binary_operation() const { return binary_operation_; }
2250
2251   // This check relies on the definition order of token in token.h.
2252   bool is_compound() const { return op() > Token::ASSIGN; }
2253
2254   static int num_ids() { return parent_num_ids() + 2; }
2255   BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2256
2257   // Type feedback information.
2258   TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2259   bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2260   bool IsUninitialized() const {
2261     return IsUninitializedField::decode(bit_field_);
2262   }
2263   bool HasNoTypeInformation() {
2264     return IsUninitializedField::decode(bit_field_);
2265   }
2266   SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2267   IcCheckType GetKeyType() const override {
2268     return KeyTypeField::decode(bit_field_);
2269   }
2270   KeyedAccessStoreMode GetStoreMode() const override {
2271     return StoreModeField::decode(bit_field_);
2272   }
2273   void set_is_uninitialized(bool b) {
2274     bit_field_ = IsUninitializedField::update(bit_field_, b);
2275   }
2276   void set_key_type(IcCheckType key_type) {
2277     bit_field_ = KeyTypeField::update(bit_field_, key_type);
2278   }
2279   void set_store_mode(KeyedAccessStoreMode mode) {
2280     bit_field_ = StoreModeField::update(bit_field_, mode);
2281   }
2282
2283  protected:
2284   Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2285              int pos);
2286   static int parent_num_ids() { return Expression::num_ids(); }
2287
2288  private:
2289   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2290
2291   class IsUninitializedField : public BitField16<bool, 0, 1> {};
2292   class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2293   class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2294   class TokenField : public BitField16<Token::Value, 6, 8> {};
2295
2296   // Starts with 16-bit field, which should get packed together with
2297   // Expression's trailing 16-bit field.
2298   uint16_t bit_field_;
2299   Expression* target_;
2300   Expression* value_;
2301   BinaryOperation* binary_operation_;
2302   SmallMapList receiver_types_;
2303 };
2304
2305
2306 class Yield final : public Expression {
2307  public:
2308   DECLARE_NODE_TYPE(Yield)
2309
2310   enum Kind {
2311     kInitial,  // The initial yield that returns the unboxed generator object.
2312     kSuspend,  // A normal yield: { value: EXPRESSION, done: false }
2313     kDelegating,  // A yield*.
2314     kFinal        // A return: { value: EXPRESSION, done: true }
2315   };
2316
2317   Expression* generator_object() const { return generator_object_; }
2318   Expression* expression() const { return expression_; }
2319   Kind yield_kind() const { return yield_kind_; }
2320
2321   // Delegating yield surrounds the "yield" in a "try/catch".  This index
2322   // locates the catch handler in the handler table, and is equivalent to
2323   // TryCatchStatement::index().
2324   int index() const {
2325     DCHECK_EQ(kDelegating, yield_kind());
2326     return index_;
2327   }
2328   void set_index(int index) {
2329     DCHECK_EQ(kDelegating, yield_kind());
2330     index_ = index;
2331   }
2332
2333   // Type feedback information.
2334   bool HasFeedbackSlots() const {
2335     return FLAG_vector_ics && (yield_kind() == kDelegating);
2336   }
2337   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2338       Isolate* isolate, const ICSlotCache* cache) override {
2339     return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2340   }
2341   void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2342                               ICSlotCache* cache) override {
2343     yield_first_feedback_slot_ = slot;
2344   }
2345   Code::Kind FeedbackICSlotKind(int index) override {
2346     return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2347   }
2348
2349   FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2350     DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2351     return yield_first_feedback_slot_;
2352   }
2353
2354   FeedbackVectorICSlot DoneFeedbackSlot() {
2355     return KeyedLoadFeedbackSlot().next();
2356   }
2357
2358   FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2359
2360  protected:
2361   Yield(Zone* zone, Expression* generator_object, Expression* expression,
2362         Kind yield_kind, int pos)
2363       : Expression(zone, pos),
2364         generator_object_(generator_object),
2365         expression_(expression),
2366         yield_kind_(yield_kind),
2367         index_(-1),
2368         yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2369
2370  private:
2371   Expression* generator_object_;
2372   Expression* expression_;
2373   Kind yield_kind_;
2374   int index_;
2375   FeedbackVectorICSlot yield_first_feedback_slot_;
2376 };
2377
2378
2379 class Throw final : public Expression {
2380  public:
2381   DECLARE_NODE_TYPE(Throw)
2382
2383   Expression* exception() const { return exception_; }
2384
2385  protected:
2386   Throw(Zone* zone, Expression* exception, int pos)
2387       : Expression(zone, pos), exception_(exception) {}
2388
2389  private:
2390   Expression* exception_;
2391 };
2392
2393
2394 class FunctionLiteral final : public Expression {
2395  public:
2396   enum FunctionType {
2397     ANONYMOUS_EXPRESSION,
2398     NAMED_EXPRESSION,
2399     DECLARATION
2400   };
2401
2402   enum ParameterFlag {
2403     kNoDuplicateParameters = 0,
2404     kHasDuplicateParameters = 1
2405   };
2406
2407   enum IsFunctionFlag {
2408     kGlobalOrEval,
2409     kIsFunction
2410   };
2411
2412   enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2413
2414   enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2415
2416   enum ArityRestriction {
2417     NORMAL_ARITY,
2418     GETTER_ARITY,
2419     SETTER_ARITY
2420   };
2421
2422   DECLARE_NODE_TYPE(FunctionLiteral)
2423
2424   Handle<String> name() const { return raw_name_->string(); }
2425   const AstRawString* raw_name() const { return raw_name_; }
2426   Scope* scope() const { return scope_; }
2427   ZoneList<Statement*>* body() const { return body_; }
2428   void set_function_token_position(int pos) { function_token_position_ = pos; }
2429   int function_token_position() const { return function_token_position_; }
2430   int start_position() const;
2431   int end_position() const;
2432   int SourceSize() const { return end_position() - start_position(); }
2433   bool is_expression() const { return IsExpression::decode(bitfield_); }
2434   bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2435   LanguageMode language_mode() const;
2436   bool uses_super_property() const;
2437
2438   static bool NeedsHomeObject(Expression* literal) {
2439     return literal != NULL && literal->IsFunctionLiteral() &&
2440            literal->AsFunctionLiteral()->uses_super_property();
2441   }
2442
2443   int materialized_literal_count() { return materialized_literal_count_; }
2444   int expected_property_count() { return expected_property_count_; }
2445   int handler_count() { return handler_count_; }
2446   int parameter_count() { return parameter_count_; }
2447
2448   bool AllowsLazyCompilation();
2449   bool AllowsLazyCompilationWithoutContext();
2450
2451   void InitializeSharedInfo(Handle<Code> code);
2452
2453   Handle<String> debug_name() const {
2454     if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2455       return raw_name_->string();
2456     }
2457     return inferred_name();
2458   }
2459
2460   Handle<String> inferred_name() const {
2461     if (!inferred_name_.is_null()) {
2462       DCHECK(raw_inferred_name_ == NULL);
2463       return inferred_name_;
2464     }
2465     if (raw_inferred_name_ != NULL) {
2466       return raw_inferred_name_->string();
2467     }
2468     UNREACHABLE();
2469     return Handle<String>();
2470   }
2471
2472   // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2473   void set_inferred_name(Handle<String> inferred_name) {
2474     DCHECK(!inferred_name.is_null());
2475     inferred_name_ = inferred_name;
2476     DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2477     raw_inferred_name_ = NULL;
2478   }
2479
2480   void set_raw_inferred_name(const AstString* raw_inferred_name) {
2481     DCHECK(raw_inferred_name != NULL);
2482     raw_inferred_name_ = raw_inferred_name;
2483     DCHECK(inferred_name_.is_null());
2484     inferred_name_ = Handle<String>();
2485   }
2486
2487   // shared_info may be null if it's not cached in full code.
2488   Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2489
2490   bool pretenure() { return Pretenure::decode(bitfield_); }
2491   void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2492
2493   bool has_duplicate_parameters() {
2494     return HasDuplicateParameters::decode(bitfield_);
2495   }
2496
2497   bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2498
2499   // This is used as a heuristic on when to eagerly compile a function
2500   // literal. We consider the following constructs as hints that the
2501   // function will be called immediately:
2502   // - (function() { ... })();
2503   // - var x = function() { ... }();
2504   bool should_eager_compile() const {
2505     return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2506   }
2507   void set_should_eager_compile() {
2508     bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2509   }
2510
2511   // A hint that we expect this function to be called (exactly) once,
2512   // i.e. we suspect it's an initialization function.
2513   bool should_be_used_once_hint() const {
2514     return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2515   }
2516   void set_should_be_used_once_hint() {
2517     bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2518   }
2519
2520   FunctionKind kind() { return FunctionKindBits::decode(bitfield_); }
2521
2522   int ast_node_count() { return ast_properties_.node_count(); }
2523   AstProperties::Flags* flags() { return ast_properties_.flags(); }
2524   void set_ast_properties(AstProperties* ast_properties) {
2525     ast_properties_ = *ast_properties;
2526   }
2527   const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2528     return ast_properties_.get_spec();
2529   }
2530   bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2531   BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2532   void set_dont_optimize_reason(BailoutReason reason) {
2533     dont_optimize_reason_ = reason;
2534   }
2535
2536  protected:
2537   FunctionLiteral(Zone* zone, const AstRawString* name,
2538                   AstValueFactory* ast_value_factory, Scope* scope,
2539                   ZoneList<Statement*>* body, int materialized_literal_count,
2540                   int expected_property_count, int handler_count,
2541                   int parameter_count, FunctionType function_type,
2542                   ParameterFlag has_duplicate_parameters,
2543                   IsFunctionFlag is_function,
2544                   EagerCompileHint eager_compile_hint, FunctionKind kind,
2545                   int position)
2546       : Expression(zone, position),
2547         raw_name_(name),
2548         scope_(scope),
2549         body_(body),
2550         raw_inferred_name_(ast_value_factory->empty_string()),
2551         ast_properties_(zone),
2552         dont_optimize_reason_(kNoReason),
2553         materialized_literal_count_(materialized_literal_count),
2554         expected_property_count_(expected_property_count),
2555         handler_count_(handler_count),
2556         parameter_count_(parameter_count),
2557         function_token_position_(RelocInfo::kNoPosition) {
2558     bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2559                 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2560                 Pretenure::encode(false) |
2561                 HasDuplicateParameters::encode(has_duplicate_parameters) |
2562                 IsFunction::encode(is_function) |
2563                 EagerCompileHintBit::encode(eager_compile_hint) |
2564                 FunctionKindBits::encode(kind) |
2565                 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2566     DCHECK(IsValidFunctionKind(kind));
2567   }
2568
2569  private:
2570   const AstRawString* raw_name_;
2571   Handle<String> name_;
2572   Handle<SharedFunctionInfo> shared_info_;
2573   Scope* scope_;
2574   ZoneList<Statement*>* body_;
2575   const AstString* raw_inferred_name_;
2576   Handle<String> inferred_name_;
2577   AstProperties ast_properties_;
2578   BailoutReason dont_optimize_reason_;
2579
2580   int materialized_literal_count_;
2581   int expected_property_count_;
2582   int handler_count_;
2583   int parameter_count_;
2584   int function_token_position_;
2585
2586   unsigned bitfield_;
2587   class IsExpression : public BitField<bool, 0, 1> {};
2588   class IsAnonymous : public BitField<bool, 1, 1> {};
2589   class Pretenure : public BitField<bool, 2, 1> {};
2590   class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2591   class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2592   class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2593   class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2594   class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2595   };
2596 };
2597
2598
2599 class ClassLiteral final : public Expression {
2600  public:
2601   typedef ObjectLiteralProperty Property;
2602
2603   DECLARE_NODE_TYPE(ClassLiteral)
2604
2605   Handle<String> name() const { return raw_name_->string(); }
2606   const AstRawString* raw_name() const { return raw_name_; }
2607   Scope* scope() const { return scope_; }
2608   VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2609   Expression* extends() const { return extends_; }
2610   FunctionLiteral* constructor() const { return constructor_; }
2611   ZoneList<Property*>* properties() const { return properties_; }
2612   int start_position() const { return position(); }
2613   int end_position() const { return end_position_; }
2614
2615   BailoutId EntryId() const { return BailoutId(local_id(0)); }
2616   BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2617   BailoutId ExitId() { return BailoutId(local_id(2)); }
2618   BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2619
2620   // Return an AST id for a property that is used in simulate instructions.
2621   BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2622
2623   // Unlike other AST nodes, this number of bailout IDs allocated for an
2624   // ClassLiteral can vary, so num_ids() is not a static method.
2625   int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2626
2627  protected:
2628   ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2629                VariableProxy* class_variable_proxy, Expression* extends,
2630                FunctionLiteral* constructor, ZoneList<Property*>* properties,
2631                int start_position, int end_position)
2632       : Expression(zone, start_position),
2633         raw_name_(name),
2634         scope_(scope),
2635         class_variable_proxy_(class_variable_proxy),
2636         extends_(extends),
2637         constructor_(constructor),
2638         properties_(properties),
2639         end_position_(end_position) {}
2640   static int parent_num_ids() { return Expression::num_ids(); }
2641
2642  private:
2643   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2644
2645   const AstRawString* raw_name_;
2646   Scope* scope_;
2647   VariableProxy* class_variable_proxy_;
2648   Expression* extends_;
2649   FunctionLiteral* constructor_;
2650   ZoneList<Property*>* properties_;
2651   int end_position_;
2652 };
2653
2654
2655 class NativeFunctionLiteral final : public Expression {
2656  public:
2657   DECLARE_NODE_TYPE(NativeFunctionLiteral)
2658
2659   Handle<String> name() const { return name_->string(); }
2660   v8::Extension* extension() const { return extension_; }
2661
2662  protected:
2663   NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2664                         v8::Extension* extension, int pos)
2665       : Expression(zone, pos), name_(name), extension_(extension) {}
2666
2667  private:
2668   const AstRawString* name_;
2669   v8::Extension* extension_;
2670 };
2671
2672
2673 class ThisFunction final : public Expression {
2674  public:
2675   DECLARE_NODE_TYPE(ThisFunction)
2676
2677  protected:
2678   ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2679 };
2680
2681
2682 class SuperReference final : public Expression {
2683  public:
2684   DECLARE_NODE_TYPE(SuperReference)
2685
2686   VariableProxy* this_var() const { return this_var_; }
2687
2688   static int num_ids() { return parent_num_ids() + 1; }
2689   TypeFeedbackId HomeObjectFeedbackId() { return TypeFeedbackId(local_id(0)); }
2690
2691   // Type feedback information.
2692   virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2693       Isolate* isolate, const ICSlotCache* cache) override {
2694     return FeedbackVectorRequirements(0, FLAG_vector_ics ? 1 : 0);
2695   }
2696   void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2697                               ICSlotCache* cache) override {
2698     homeobject_feedback_slot_ = slot;
2699   }
2700   Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2701
2702   FeedbackVectorICSlot HomeObjectFeedbackSlot() {
2703     DCHECK(!FLAG_vector_ics || !homeobject_feedback_slot_.IsInvalid());
2704     return homeobject_feedback_slot_;
2705   }
2706
2707  protected:
2708   SuperReference(Zone* zone, VariableProxy* this_var, int pos)
2709       : Expression(zone, pos),
2710         this_var_(this_var),
2711         homeobject_feedback_slot_(FeedbackVectorICSlot::Invalid()) {
2712     DCHECK(this_var->is_this());
2713   }
2714   static int parent_num_ids() { return Expression::num_ids(); }
2715
2716  private:
2717   int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2718
2719   VariableProxy* this_var_;
2720   FeedbackVectorICSlot homeobject_feedback_slot_;
2721 };
2722
2723
2724 #undef DECLARE_NODE_TYPE
2725
2726
2727 // ----------------------------------------------------------------------------
2728 // Regular expressions
2729
2730
2731 class RegExpVisitor BASE_EMBEDDED {
2732  public:
2733   virtual ~RegExpVisitor() { }
2734 #define MAKE_CASE(Name)                                              \
2735   virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2736   FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2737 #undef MAKE_CASE
2738 };
2739
2740
2741 class RegExpTree : public ZoneObject {
2742  public:
2743   static const int kInfinity = kMaxInt;
2744   virtual ~RegExpTree() {}
2745   virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2746   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2747                              RegExpNode* on_success) = 0;
2748   virtual bool IsTextElement() { return false; }
2749   virtual bool IsAnchoredAtStart() { return false; }
2750   virtual bool IsAnchoredAtEnd() { return false; }
2751   virtual int min_match() = 0;
2752   virtual int max_match() = 0;
2753   // Returns the interval of registers used for captures within this
2754   // expression.
2755   virtual Interval CaptureRegisters() { return Interval::Empty(); }
2756   virtual void AppendToText(RegExpText* text, Zone* zone);
2757   std::ostream& Print(std::ostream& os, Zone* zone);  // NOLINT
2758 #define MAKE_ASTYPE(Name)                                                  \
2759   virtual RegExp##Name* As##Name();                                        \
2760   virtual bool Is##Name();
2761   FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2762 #undef MAKE_ASTYPE
2763 };
2764
2765
2766 class RegExpDisjunction final : public RegExpTree {
2767  public:
2768   explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2769   void* Accept(RegExpVisitor* visitor, void* data) override;
2770   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2771                              RegExpNode* on_success) override;
2772   RegExpDisjunction* AsDisjunction() override;
2773   Interval CaptureRegisters() override;
2774   bool IsDisjunction() override;
2775   bool IsAnchoredAtStart() override;
2776   bool IsAnchoredAtEnd() override;
2777   int min_match() override { return min_match_; }
2778   int max_match() override { return max_match_; }
2779   ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2780  private:
2781   ZoneList<RegExpTree*>* alternatives_;
2782   int min_match_;
2783   int max_match_;
2784 };
2785
2786
2787 class RegExpAlternative final : public RegExpTree {
2788  public:
2789   explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2790   void* Accept(RegExpVisitor* visitor, void* data) override;
2791   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2792                              RegExpNode* on_success) override;
2793   RegExpAlternative* AsAlternative() override;
2794   Interval CaptureRegisters() override;
2795   bool IsAlternative() override;
2796   bool IsAnchoredAtStart() override;
2797   bool IsAnchoredAtEnd() override;
2798   int min_match() override { return min_match_; }
2799   int max_match() override { return max_match_; }
2800   ZoneList<RegExpTree*>* nodes() { return nodes_; }
2801  private:
2802   ZoneList<RegExpTree*>* nodes_;
2803   int min_match_;
2804   int max_match_;
2805 };
2806
2807
2808 class RegExpAssertion final : public RegExpTree {
2809  public:
2810   enum AssertionType {
2811     START_OF_LINE,
2812     START_OF_INPUT,
2813     END_OF_LINE,
2814     END_OF_INPUT,
2815     BOUNDARY,
2816     NON_BOUNDARY
2817   };
2818   explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2819   void* Accept(RegExpVisitor* visitor, void* data) override;
2820   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2821                              RegExpNode* on_success) override;
2822   RegExpAssertion* AsAssertion() override;
2823   bool IsAssertion() override;
2824   bool IsAnchoredAtStart() override;
2825   bool IsAnchoredAtEnd() override;
2826   int min_match() override { return 0; }
2827   int max_match() override { return 0; }
2828   AssertionType assertion_type() { return assertion_type_; }
2829  private:
2830   AssertionType assertion_type_;
2831 };
2832
2833
2834 class CharacterSet final BASE_EMBEDDED {
2835  public:
2836   explicit CharacterSet(uc16 standard_set_type)
2837       : ranges_(NULL),
2838         standard_set_type_(standard_set_type) {}
2839   explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2840       : ranges_(ranges),
2841         standard_set_type_(0) {}
2842   ZoneList<CharacterRange>* ranges(Zone* zone);
2843   uc16 standard_set_type() { return standard_set_type_; }
2844   void set_standard_set_type(uc16 special_set_type) {
2845     standard_set_type_ = special_set_type;
2846   }
2847   bool is_standard() { return standard_set_type_ != 0; }
2848   void Canonicalize();
2849  private:
2850   ZoneList<CharacterRange>* ranges_;
2851   // If non-zero, the value represents a standard set (e.g., all whitespace
2852   // characters) without having to expand the ranges.
2853   uc16 standard_set_type_;
2854 };
2855
2856
2857 class RegExpCharacterClass final : public RegExpTree {
2858  public:
2859   RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2860       : set_(ranges),
2861         is_negated_(is_negated) { }
2862   explicit RegExpCharacterClass(uc16 type)
2863       : set_(type),
2864         is_negated_(false) { }
2865   void* Accept(RegExpVisitor* visitor, void* data) override;
2866   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2867                              RegExpNode* on_success) override;
2868   RegExpCharacterClass* AsCharacterClass() override;
2869   bool IsCharacterClass() override;
2870   bool IsTextElement() override { return true; }
2871   int min_match() override { return 1; }
2872   int max_match() override { return 1; }
2873   void AppendToText(RegExpText* text, Zone* zone) override;
2874   CharacterSet character_set() { return set_; }
2875   // TODO(lrn): Remove need for complex version if is_standard that
2876   // recognizes a mangled standard set and just do { return set_.is_special(); }
2877   bool is_standard(Zone* zone);
2878   // Returns a value representing the standard character set if is_standard()
2879   // returns true.
2880   // Currently used values are:
2881   // s : unicode whitespace
2882   // S : unicode non-whitespace
2883   // w : ASCII word character (digit, letter, underscore)
2884   // W : non-ASCII word character
2885   // d : ASCII digit
2886   // D : non-ASCII digit
2887   // . : non-unicode non-newline
2888   // * : All characters
2889   uc16 standard_type() { return set_.standard_set_type(); }
2890   ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2891   bool is_negated() { return is_negated_; }
2892
2893  private:
2894   CharacterSet set_;
2895   bool is_negated_;
2896 };
2897
2898
2899 class RegExpAtom final : public RegExpTree {
2900  public:
2901   explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
2902   void* Accept(RegExpVisitor* visitor, void* data) override;
2903   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2904                              RegExpNode* on_success) override;
2905   RegExpAtom* AsAtom() override;
2906   bool IsAtom() override;
2907   bool IsTextElement() override { return true; }
2908   int min_match() override { return data_.length(); }
2909   int max_match() override { return data_.length(); }
2910   void AppendToText(RegExpText* text, Zone* zone) override;
2911   Vector<const uc16> data() { return data_; }
2912   int length() { return data_.length(); }
2913  private:
2914   Vector<const uc16> data_;
2915 };
2916
2917
2918 class RegExpText final : public RegExpTree {
2919  public:
2920   explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
2921   void* Accept(RegExpVisitor* visitor, void* data) override;
2922   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2923                              RegExpNode* on_success) override;
2924   RegExpText* AsText() override;
2925   bool IsText() override;
2926   bool IsTextElement() override { return true; }
2927   int min_match() override { return length_; }
2928   int max_match() override { return length_; }
2929   void AppendToText(RegExpText* text, Zone* zone) override;
2930   void AddElement(TextElement elm, Zone* zone)  {
2931     elements_.Add(elm, zone);
2932     length_ += elm.length();
2933   }
2934   ZoneList<TextElement>* elements() { return &elements_; }
2935  private:
2936   ZoneList<TextElement> elements_;
2937   int length_;
2938 };
2939
2940
2941 class RegExpQuantifier final : public RegExpTree {
2942  public:
2943   enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
2944   RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
2945       : body_(body),
2946         min_(min),
2947         max_(max),
2948         min_match_(min * body->min_match()),
2949         quantifier_type_(type) {
2950     if (max > 0 && body->max_match() > kInfinity / max) {
2951       max_match_ = kInfinity;
2952     } else {
2953       max_match_ = max * body->max_match();
2954     }
2955   }
2956   void* Accept(RegExpVisitor* visitor, void* data) override;
2957   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2958                              RegExpNode* on_success) override;
2959   static RegExpNode* ToNode(int min,
2960                             int max,
2961                             bool is_greedy,
2962                             RegExpTree* body,
2963                             RegExpCompiler* compiler,
2964                             RegExpNode* on_success,
2965                             bool not_at_start = false);
2966   RegExpQuantifier* AsQuantifier() override;
2967   Interval CaptureRegisters() override;
2968   bool IsQuantifier() override;
2969   int min_match() override { return min_match_; }
2970   int max_match() override { return max_match_; }
2971   int min() { return min_; }
2972   int max() { return max_; }
2973   bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
2974   bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
2975   bool is_greedy() { return quantifier_type_ == GREEDY; }
2976   RegExpTree* body() { return body_; }
2977
2978  private:
2979   RegExpTree* body_;
2980   int min_;
2981   int max_;
2982   int min_match_;
2983   int max_match_;
2984   QuantifierType quantifier_type_;
2985 };
2986
2987
2988 class RegExpCapture final : public RegExpTree {
2989  public:
2990   explicit RegExpCapture(RegExpTree* body, int index)
2991       : body_(body), index_(index) { }
2992   void* Accept(RegExpVisitor* visitor, void* data) override;
2993   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2994                              RegExpNode* on_success) override;
2995   static RegExpNode* ToNode(RegExpTree* body,
2996                             int index,
2997                             RegExpCompiler* compiler,
2998                             RegExpNode* on_success);
2999   RegExpCapture* AsCapture() override;
3000   bool IsAnchoredAtStart() override;
3001   bool IsAnchoredAtEnd() override;
3002   Interval CaptureRegisters() override;
3003   bool IsCapture() override;
3004   int min_match() override { return body_->min_match(); }
3005   int max_match() override { return body_->max_match(); }
3006   RegExpTree* body() { return body_; }
3007   int index() { return index_; }
3008   static int StartRegister(int index) { return index * 2; }
3009   static int EndRegister(int index) { return index * 2 + 1; }
3010
3011  private:
3012   RegExpTree* body_;
3013   int index_;
3014 };
3015
3016
3017 class RegExpLookahead final : public RegExpTree {
3018  public:
3019   RegExpLookahead(RegExpTree* body,
3020                   bool is_positive,
3021                   int capture_count,
3022                   int capture_from)
3023       : body_(body),
3024         is_positive_(is_positive),
3025         capture_count_(capture_count),
3026         capture_from_(capture_from) { }
3027
3028   void* Accept(RegExpVisitor* visitor, void* data) override;
3029   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3030                              RegExpNode* on_success) override;
3031   RegExpLookahead* AsLookahead() override;
3032   Interval CaptureRegisters() override;
3033   bool IsLookahead() override;
3034   bool IsAnchoredAtStart() override;
3035   int min_match() override { return 0; }
3036   int max_match() override { return 0; }
3037   RegExpTree* body() { return body_; }
3038   bool is_positive() { return is_positive_; }
3039   int capture_count() { return capture_count_; }
3040   int capture_from() { return capture_from_; }
3041
3042  private:
3043   RegExpTree* body_;
3044   bool is_positive_;
3045   int capture_count_;
3046   int capture_from_;
3047 };
3048
3049
3050 class RegExpBackReference final : public RegExpTree {
3051  public:
3052   explicit RegExpBackReference(RegExpCapture* capture)
3053       : capture_(capture) { }
3054   void* Accept(RegExpVisitor* visitor, void* data) override;
3055   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3056                              RegExpNode* on_success) override;
3057   RegExpBackReference* AsBackReference() override;
3058   bool IsBackReference() override;
3059   int min_match() override { return 0; }
3060   int max_match() override { return capture_->max_match(); }
3061   int index() { return capture_->index(); }
3062   RegExpCapture* capture() { return capture_; }
3063  private:
3064   RegExpCapture* capture_;
3065 };
3066
3067
3068 class RegExpEmpty final : public RegExpTree {
3069  public:
3070   RegExpEmpty() { }
3071   void* Accept(RegExpVisitor* visitor, void* data) override;
3072   virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3073                              RegExpNode* on_success) override;
3074   RegExpEmpty* AsEmpty() override;
3075   bool IsEmpty() override;
3076   int min_match() override { return 0; }
3077   int max_match() override { return 0; }
3078 };
3079
3080
3081 // ----------------------------------------------------------------------------
3082 // Basic visitor
3083 // - leaf node visitors are abstract.
3084
3085 class AstVisitor BASE_EMBEDDED {
3086  public:
3087   AstVisitor() {}
3088   virtual ~AstVisitor() {}
3089
3090   // Stack overflow check and dynamic dispatch.
3091   virtual void Visit(AstNode* node) = 0;
3092
3093   // Iteration left-to-right.
3094   virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3095   virtual void VisitStatements(ZoneList<Statement*>* statements);
3096   virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3097
3098   // Individual AST nodes.
3099 #define DEF_VISIT(type)                         \
3100   virtual void Visit##type(type* node) = 0;
3101   AST_NODE_LIST(DEF_VISIT)
3102 #undef DEF_VISIT
3103 };
3104
3105
3106 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS()               \
3107  public:                                                    \
3108   void Visit(AstNode* node) final {                         \
3109     if (!CheckStackOverflow()) node->Accept(this);          \
3110   }                                                         \
3111                                                             \
3112   void SetStackOverflow() { stack_overflow_ = true; }       \
3113   void ClearStackOverflow() { stack_overflow_ = false; }    \
3114   bool HasStackOverflow() const { return stack_overflow_; } \
3115                                                             \
3116   bool CheckStackOverflow() {                               \
3117     if (stack_overflow_) return true;                       \
3118     StackLimitCheck check(isolate_);                        \
3119     if (!check.HasOverflowed()) return false;               \
3120     stack_overflow_ = true;                                 \
3121     return true;                                            \
3122   }                                                         \
3123                                                             \
3124  private:                                                   \
3125   void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3126     isolate_ = isolate;                                     \
3127     zone_ = zone;                                           \
3128     stack_overflow_ = false;                                \
3129   }                                                         \
3130   Zone* zone() { return zone_; }                            \
3131   Isolate* isolate() { return isolate_; }                   \
3132                                                             \
3133   Isolate* isolate_;                                        \
3134   Zone* zone_;                                              \
3135   bool stack_overflow_
3136
3137
3138 // ----------------------------------------------------------------------------
3139 // AstNode factory
3140
3141 class AstNodeFactory final BASE_EMBEDDED {
3142  public:
3143   explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3144       : zone_(ast_value_factory->zone()),
3145         ast_value_factory_(ast_value_factory) {}
3146
3147   VariableDeclaration* NewVariableDeclaration(
3148       VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3149       bool is_class_declaration = false, int declaration_group_start = -1) {
3150     return new (zone_)
3151         VariableDeclaration(zone_, proxy, mode, scope, pos,
3152                             is_class_declaration, declaration_group_start);
3153   }
3154
3155   FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3156                                               VariableMode mode,
3157                                               FunctionLiteral* fun,
3158                                               Scope* scope,
3159                                               int pos) {
3160     return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3161   }
3162
3163   ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3164                                           const AstRawString* import_name,
3165                                           const AstRawString* module_specifier,
3166                                           Scope* scope, int pos) {
3167     return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3168                                          module_specifier, scope, pos);
3169   }
3170
3171   ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3172                                           Scope* scope,
3173                                           int pos) {
3174     return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3175   }
3176
3177   Block* NewBlock(ZoneList<const AstRawString*>* labels,
3178                   int capacity,
3179                   bool is_initializer_block,
3180                   int pos) {
3181     return new (zone_)
3182         Block(zone_, labels, capacity, is_initializer_block, pos);
3183   }
3184
3185 #define STATEMENT_WITH_LABELS(NodeType)                                     \
3186   NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3187     return new (zone_) NodeType(zone_, labels, pos);                        \
3188   }
3189   STATEMENT_WITH_LABELS(DoWhileStatement)
3190   STATEMENT_WITH_LABELS(WhileStatement)
3191   STATEMENT_WITH_LABELS(ForStatement)
3192   STATEMENT_WITH_LABELS(SwitchStatement)
3193 #undef STATEMENT_WITH_LABELS
3194
3195   ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3196                                         ZoneList<const AstRawString*>* labels,
3197                                         int pos) {
3198     switch (visit_mode) {
3199       case ForEachStatement::ENUMERATE: {
3200         return new (zone_) ForInStatement(zone_, labels, pos);
3201       }
3202       case ForEachStatement::ITERATE: {
3203         return new (zone_) ForOfStatement(zone_, labels, pos);
3204       }
3205     }
3206     UNREACHABLE();
3207     return NULL;
3208   }
3209
3210   ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3211     return new (zone_) ExpressionStatement(zone_, expression, pos);
3212   }
3213
3214   ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3215     return new (zone_) ContinueStatement(zone_, target, pos);
3216   }
3217
3218   BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3219     return new (zone_) BreakStatement(zone_, target, pos);
3220   }
3221
3222   ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3223     return new (zone_) ReturnStatement(zone_, expression, pos);
3224   }
3225
3226   WithStatement* NewWithStatement(Scope* scope,
3227                                   Expression* expression,
3228                                   Statement* statement,
3229                                   int pos) {
3230     return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3231   }
3232
3233   IfStatement* NewIfStatement(Expression* condition,
3234                               Statement* then_statement,
3235                               Statement* else_statement,
3236                               int pos) {
3237     return new (zone_)
3238         IfStatement(zone_, condition, then_statement, else_statement, pos);
3239   }
3240
3241   TryCatchStatement* NewTryCatchStatement(int index,
3242                                           Block* try_block,
3243                                           Scope* scope,
3244                                           Variable* variable,
3245                                           Block* catch_block,
3246                                           int pos) {
3247     return new (zone_) TryCatchStatement(zone_, index, try_block, scope,
3248                                          variable, catch_block, pos);
3249   }
3250
3251   TryFinallyStatement* NewTryFinallyStatement(int index,
3252                                               Block* try_block,
3253                                               Block* finally_block,
3254                                               int pos) {
3255     return new (zone_)
3256         TryFinallyStatement(zone_, index, try_block, finally_block, pos);
3257   }
3258
3259   DebuggerStatement* NewDebuggerStatement(int pos) {
3260     return new (zone_) DebuggerStatement(zone_, pos);
3261   }
3262
3263   EmptyStatement* NewEmptyStatement(int pos) {
3264     return new(zone_) EmptyStatement(zone_, pos);
3265   }
3266
3267   CaseClause* NewCaseClause(
3268       Expression* label, ZoneList<Statement*>* statements, int pos) {
3269     return new (zone_) CaseClause(zone_, label, statements, pos);
3270   }
3271
3272   Literal* NewStringLiteral(const AstRawString* string, int pos) {
3273     return new (zone_)
3274         Literal(zone_, ast_value_factory_->NewString(string), pos);
3275   }
3276
3277   // A JavaScript symbol (ECMA-262 edition 6).
3278   Literal* NewSymbolLiteral(const char* name, int pos) {
3279     return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3280   }
3281
3282   Literal* NewNumberLiteral(double number, int pos) {
3283     return new (zone_)
3284         Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3285   }
3286
3287   Literal* NewSmiLiteral(int number, int pos) {
3288     return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3289   }
3290
3291   Literal* NewBooleanLiteral(bool b, int pos) {
3292     return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3293   }
3294
3295   Literal* NewNullLiteral(int pos) {
3296     return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3297   }
3298
3299   Literal* NewUndefinedLiteral(int pos) {
3300     return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3301   }
3302
3303   Literal* NewTheHoleLiteral(int pos) {
3304     return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3305   }
3306
3307   ObjectLiteral* NewObjectLiteral(
3308       ZoneList<ObjectLiteral::Property*>* properties,
3309       int literal_index,
3310       int boilerplate_properties,
3311       bool has_function,
3312       int pos) {
3313     return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3314                                      boilerplate_properties, has_function, pos);
3315   }
3316
3317   ObjectLiteral::Property* NewObjectLiteralProperty(
3318       Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3319       bool is_static, bool is_computed_name) {
3320     return new (zone_)
3321         ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3322   }
3323
3324   ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3325                                                     Expression* value,
3326                                                     bool is_static,
3327                                                     bool is_computed_name) {
3328     return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3329                                                is_static, is_computed_name);
3330   }
3331
3332   RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3333                                   const AstRawString* flags,
3334                                   int literal_index,
3335                                   int pos) {
3336     return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index, pos);
3337   }
3338
3339   ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3340                                 int literal_index,
3341                                 int pos) {
3342     return new (zone_) ArrayLiteral(zone_, values, literal_index, pos);
3343   }
3344
3345   VariableProxy* NewVariableProxy(Variable* var,
3346                                   int start_position = RelocInfo::kNoPosition,
3347                                   int end_position = RelocInfo::kNoPosition) {
3348     return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3349   }
3350
3351   VariableProxy* NewVariableProxy(const AstRawString* name,
3352                                   Variable::Kind variable_kind,
3353                                   int start_position = RelocInfo::kNoPosition,
3354                                   int end_position = RelocInfo::kNoPosition) {
3355     DCHECK_NOT_NULL(name);
3356     return new (zone_)
3357         VariableProxy(zone_, name, variable_kind, start_position, end_position);
3358   }
3359
3360   Property* NewProperty(Expression* obj, Expression* key, int pos) {
3361     return new (zone_) Property(zone_, obj, key, pos);
3362   }
3363
3364   Call* NewCall(Expression* expression,
3365                 ZoneList<Expression*>* arguments,
3366                 int pos) {
3367     return new (zone_) Call(zone_, expression, arguments, pos);
3368   }
3369
3370   CallNew* NewCallNew(Expression* expression,
3371                       ZoneList<Expression*>* arguments,
3372                       int pos) {
3373     return new (zone_) CallNew(zone_, expression, arguments, pos);
3374   }
3375
3376   CallRuntime* NewCallRuntime(const AstRawString* name,
3377                               const Runtime::Function* function,
3378                               ZoneList<Expression*>* arguments,
3379                               int pos) {
3380     return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3381   }
3382
3383   UnaryOperation* NewUnaryOperation(Token::Value op,
3384                                     Expression* expression,
3385                                     int pos) {
3386     return new (zone_) UnaryOperation(zone_, op, expression, pos);
3387   }
3388
3389   BinaryOperation* NewBinaryOperation(Token::Value op,
3390                                       Expression* left,
3391                                       Expression* right,
3392                                       int pos) {
3393     return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3394   }
3395
3396   CountOperation* NewCountOperation(Token::Value op,
3397                                     bool is_prefix,
3398                                     Expression* expr,
3399                                     int pos) {
3400     return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3401   }
3402
3403   CompareOperation* NewCompareOperation(Token::Value op,
3404                                         Expression* left,
3405                                         Expression* right,
3406                                         int pos) {
3407     return new (zone_) CompareOperation(zone_, op, left, right, pos);
3408   }
3409
3410   Spread* NewSpread(Expression* expression, int pos) {
3411     return new (zone_) Spread(zone_, expression, pos);
3412   }
3413
3414   Conditional* NewConditional(Expression* condition,
3415                               Expression* then_expression,
3416                               Expression* else_expression,
3417                               int position) {
3418     return new (zone_) Conditional(zone_, condition, then_expression,
3419                                    else_expression, position);
3420   }
3421
3422   Assignment* NewAssignment(Token::Value op,
3423                             Expression* target,
3424                             Expression* value,
3425                             int pos) {
3426     DCHECK(Token::IsAssignmentOp(op));
3427     Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3428     if (assign->is_compound()) {
3429       DCHECK(Token::IsAssignmentOp(op));
3430       assign->binary_operation_ =
3431           NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3432     }
3433     return assign;
3434   }
3435
3436   Yield* NewYield(Expression *generator_object,
3437                   Expression* expression,
3438                   Yield::Kind yield_kind,
3439                   int pos) {
3440     if (!expression) expression = NewUndefinedLiteral(pos);
3441     return new (zone_)
3442         Yield(zone_, generator_object, expression, yield_kind, pos);
3443   }
3444
3445   Throw* NewThrow(Expression* exception, int pos) {
3446     return new (zone_) Throw(zone_, exception, pos);
3447   }
3448
3449   FunctionLiteral* NewFunctionLiteral(
3450       const AstRawString* name, AstValueFactory* ast_value_factory,
3451       Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3452       int expected_property_count, int handler_count, int parameter_count,
3453       FunctionLiteral::ParameterFlag has_duplicate_parameters,
3454       FunctionLiteral::FunctionType function_type,
3455       FunctionLiteral::IsFunctionFlag is_function,
3456       FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3457       int position) {
3458     return new (zone_) FunctionLiteral(
3459         zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3460         expected_property_count, handler_count, parameter_count, function_type,
3461         has_duplicate_parameters, is_function, eager_compile_hint, kind,
3462         position);
3463   }
3464
3465   ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3466                                 VariableProxy* proxy, Expression* extends,
3467                                 FunctionLiteral* constructor,
3468                                 ZoneList<ObjectLiteral::Property*>* properties,
3469                                 int start_position, int end_position) {
3470     return new (zone_)
3471         ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3472                      properties, start_position, end_position);
3473   }
3474
3475   NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3476                                                   v8::Extension* extension,
3477                                                   int pos) {
3478     return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3479   }
3480
3481   ThisFunction* NewThisFunction(int pos) {
3482     return new (zone_) ThisFunction(zone_, pos);
3483   }
3484
3485   SuperReference* NewSuperReference(VariableProxy* this_var, int pos) {
3486     return new (zone_) SuperReference(zone_, this_var, pos);
3487   }
3488
3489  private:
3490   Zone* zone_;
3491   AstValueFactory* ast_value_factory_;
3492 };
3493
3494
3495 } }  // namespace v8::internal
3496
3497 #endif  // V8_AST_H_