V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
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_AST_H_
6 #define V8_AST_AST_H_
7 
8 #include <memory>
9 
10 #include "src/ast/ast-value-factory.h"
11 #include "src/ast/modules.h"
12 #include "src/ast/variables.h"
13 #include "src/bailout-reason.h"
14 #include "src/globals.h"
15 #include "src/heap/factory.h"
16 #include "src/isolate.h"
17 #include "src/label.h"
18 #include "src/objects/literal-objects.h"
19 #include "src/objects/smi.h"
20 #include "src/parsing/token.h"
21 #include "src/runtime/runtime.h"
22 
23 namespace v8 {
24 namespace internal {
25 
26 // The abstract syntax tree is an intermediate, light-weight
27 // representation of the parsed JavaScript code suitable for
28 // compilation to native code.
29 
30 // Nodes are allocated in a separate zone, which allows faster
31 // allocation and constant-time deallocation of the entire syntax
32 // tree.
33 
34 
35 // ----------------------------------------------------------------------------
36 // Nodes of the abstract syntax tree. Only concrete classes are
37 // enumerated here.
38 
39 #define DECLARATION_NODE_LIST(V) \
40  V(VariableDeclaration) \
41  V(FunctionDeclaration)
42 
43 #define ITERATION_NODE_LIST(V) \
44  V(DoWhileStatement) \
45  V(WhileStatement) \
46  V(ForStatement) \
47  V(ForInStatement) \
48  V(ForOfStatement)
49 
50 #define BREAKABLE_NODE_LIST(V) \
51  V(Block) \
52  V(SwitchStatement)
53 
54 #define STATEMENT_NODE_LIST(V) \
55  ITERATION_NODE_LIST(V) \
56  BREAKABLE_NODE_LIST(V) \
57  V(ExpressionStatement) \
58  V(EmptyStatement) \
59  V(SloppyBlockFunctionStatement) \
60  V(IfStatement) \
61  V(ContinueStatement) \
62  V(BreakStatement) \
63  V(ReturnStatement) \
64  V(WithStatement) \
65  V(TryCatchStatement) \
66  V(TryFinallyStatement) \
67  V(DebuggerStatement) \
68  V(InitializeClassMembersStatement)
69 
70 #define LITERAL_NODE_LIST(V) \
71  V(RegExpLiteral) \
72  V(ObjectLiteral) \
73  V(ArrayLiteral)
74 
75 #define EXPRESSION_NODE_LIST(V) \
76  LITERAL_NODE_LIST(V) \
77  V(Assignment) \
78  V(Await) \
79  V(BinaryOperation) \
80  V(NaryOperation) \
81  V(Call) \
82  V(CallNew) \
83  V(CallRuntime) \
84  V(ClassLiteral) \
85  V(CompareOperation) \
86  V(CompoundAssignment) \
87  V(Conditional) \
88  V(CountOperation) \
89  V(DoExpression) \
90  V(EmptyParentheses) \
91  V(FunctionLiteral) \
92  V(GetIterator) \
93  V(GetTemplateObject) \
94  V(ImportCallExpression) \
95  V(Literal) \
96  V(NativeFunctionLiteral) \
97  V(Property) \
98  V(ResolvedProperty) \
99  V(RewritableExpression) \
100  V(Spread) \
101  V(StoreInArrayLiteral) \
102  V(SuperCallReference) \
103  V(SuperPropertyReference) \
104  V(TemplateLiteral) \
105  V(ThisFunction) \
106  V(Throw) \
107  V(UnaryOperation) \
108  V(VariableProxy) \
109  V(Yield) \
110  V(YieldStar)
111 
112 #define FAILURE_NODE_LIST(V) V(FailureExpression)
113 
114 #define AST_NODE_LIST(V) \
115  DECLARATION_NODE_LIST(V) \
116  STATEMENT_NODE_LIST(V) \
117  EXPRESSION_NODE_LIST(V)
118 
119 // Forward declarations
120 class AstNode;
121 class AstNodeFactory;
122 class Declaration;
123 class BreakableStatement;
124 class Expression;
125 class IterationStatement;
126 class MaterializedLiteral;
127 class NestedVariableDeclaration;
128 class ProducedPreParsedScopeData;
129 class Statement;
130 
131 #define DEF_FORWARD_DECLARATION(type) class type;
132 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
133 FAILURE_NODE_LIST(DEF_FORWARD_DECLARATION)
134 #undef DEF_FORWARD_DECLARATION
135 
136 class AstNode: public ZoneObject {
137  public:
138 #define DECLARE_TYPE_ENUM(type) k##type,
139  enum NodeType : uint8_t {
140  AST_NODE_LIST(DECLARE_TYPE_ENUM) /* , */
141  FAILURE_NODE_LIST(DECLARE_TYPE_ENUM)
142  };
143 #undef DECLARE_TYPE_ENUM
144 
145  void* operator new(size_t size, Zone* zone) { return zone->New(size); }
146 
147  NodeType node_type() const { return NodeTypeField::decode(bit_field_); }
148  int position() const { return position_; }
149 
150 #ifdef DEBUG
151  void Print();
152  void Print(Isolate* isolate);
153 #endif // DEBUG
154 
155  // Type testing & conversion functions overridden by concrete subclasses.
156 #define DECLARE_NODE_FUNCTIONS(type) \
157  V8_INLINE bool Is##type() const; \
158  V8_INLINE type* As##type(); \
159  V8_INLINE const type* As##type() const;
160  AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
161  FAILURE_NODE_LIST(DECLARE_NODE_FUNCTIONS)
162 #undef DECLARE_NODE_FUNCTIONS
163 
164  BreakableStatement* AsBreakableStatement();
165  IterationStatement* AsIterationStatement();
166  MaterializedLiteral* AsMaterializedLiteral();
167 
168  private:
169  // Hidden to prevent accidental usage. It would have to load the
170  // current zone from the TLS.
171  void* operator new(size_t size);
172 
173  int position_;
174  class NodeTypeField : public BitField<NodeType, 0, 6> {};
175 
176  protected:
177  uint32_t bit_field_;
178  static const uint8_t kNextBitFieldIndex = NodeTypeField::kNext;
179 
180  AstNode(int position, NodeType type)
181  : position_(position), bit_field_(NodeTypeField::encode(type)) {}
182 };
183 
184 
185 class Statement : public AstNode {
186  public:
187  bool IsJump() const;
188 
189  protected:
190  Statement(int position, NodeType type) : AstNode(position, type) {}
191 
192  static const uint8_t kNextBitFieldIndex = AstNode::kNextBitFieldIndex;
193 };
194 
195 
196 class Expression : public AstNode {
197  public:
198  enum Context {
199  // Not assigned a context yet, or else will not be visited during
200  // code generation.
201  kUninitialized,
202  // Evaluated for its side effects.
203  kEffect,
204  // Evaluated for its value (and side effects).
205  kValue,
206  // Evaluated for control flow (and side effects).
207  kTest
208  };
209 
210  // True iff the expression is a valid reference expression.
211  bool IsValidReferenceExpression() const;
212 
213  // Helpers for ToBoolean conversion.
214  bool ToBooleanIsTrue() const;
215  bool ToBooleanIsFalse() const;
216 
217  // Symbols that cannot be parsed as array indices are considered property
218  // names. We do not treat symbols that can be array indexes as property
219  // names because [] for string objects is handled only by keyed ICs.
220  bool IsPropertyName() const;
221 
222  // True iff the expression is a class or function expression without
223  // a syntactic name.
224  bool IsAnonymousFunctionDefinition() const;
225 
226  // True iff the expression is a concise method definition.
227  bool IsConciseMethodDefinition() const;
228 
229  // True iff the expression is an accessor function definition.
230  bool IsAccessorFunctionDefinition() const;
231 
232  // True iff the expression is a literal represented as a smi.
233  bool IsSmiLiteral() const;
234 
235  // True iff the expression is a literal represented as a number.
236  bool IsNumberLiteral() const;
237 
238  // True iff the expression is a string literal.
239  bool IsStringLiteral() const;
240 
241  // True iff the expression is the null literal.
242  bool IsNullLiteral() const;
243 
244  // True iff the expression is the hole literal.
245  bool IsTheHoleLiteral() const;
246 
247  // True if we can prove that the expression is the undefined literal. Note
248  // that this also checks for loads of the global "undefined" variable.
249  bool IsUndefinedLiteral() const;
250 
251  bool IsCompileTimeValue();
252 
253  bool IsPattern() {
254  STATIC_ASSERT(kObjectLiteral + 1 == kArrayLiteral);
255  return IsInRange(node_type(), kObjectLiteral, kArrayLiteral);
256  }
257 
258  bool is_parenthesized() const {
259  return IsParenthesizedField::decode(bit_field_);
260  }
261 
262  void mark_parenthesized() {
263  bit_field_ = IsParenthesizedField::update(bit_field_, true);
264  }
265 
266  private:
267  class IsParenthesizedField
268  : public BitField<bool, AstNode::kNextBitFieldIndex, 1> {};
269 
270  protected:
271  Expression(int pos, NodeType type) : AstNode(pos, type) {
272  DCHECK(!is_parenthesized());
273  }
274 
275  static const uint8_t kNextBitFieldIndex = IsParenthesizedField::kNext;
276 };
277 
279  private:
280  friend class AstNodeFactory;
281  FailureExpression() : Expression(kNoSourcePosition, kFailureExpression) {}
282 };
283 
284 // V8's notion of BreakableStatement does not correspond to the notion of
285 // BreakableStatement in ECMAScript. In V8, the idea is that a
286 // BreakableStatement is a statement that can be the target of a break
287 // statement. The BreakableStatement AST node carries a list of labels, any of
288 // which can be used as an argument to the break statement in order to target
289 // it.
290 //
291 // Since we don't want to attach a list of labels to all kinds of statements, we
292 // only declare switchs, loops, and blocks as BreakableStatements. This means
293 // that we implement breaks targeting other statement forms as breaks targeting
294 // a substatement thereof. For instance, in "foo: if (b) { f(); break foo; }" we
295 // pretend that foo is the label of the inner block. That's okay because one
296 // can't observe the difference.
297 //
298 // This optimization makes it harder to detect invalid continue labels, see the
299 // need for own_labels in IterationStatement.
300 //
302  public:
303  enum BreakableType {
304  TARGET_FOR_ANONYMOUS,
305  TARGET_FOR_NAMED_ONLY
306  };
307 
308  // A list of all labels declared on the path up to the previous
309  // BreakableStatement (if any).
310  //
311  // Example: "l1: for (;;) l2: l3: { l4: if (b) l5: { s } }"
312  // labels() of the ForStatement will be l1.
313  // labels() of the Block { l4: ... } will be l2, l3.
314  // labels() of the Block { s } will be l4, l5.
315  ZonePtrList<const AstRawString>* labels() const;
316 
317  // Testers.
318  bool is_target_for_anonymous() const {
319  return BreakableTypeField::decode(bit_field_) == TARGET_FOR_ANONYMOUS;
320  }
321 
322  private:
323  class BreakableTypeField
324  : public BitField<BreakableType, Statement::kNextBitFieldIndex, 1> {};
325 
326  protected:
327  BreakableStatement(BreakableType breakable_type, int position, NodeType type)
328  : Statement(position, type) {
329  bit_field_ |= BreakableTypeField::encode(breakable_type);
330  }
331 
332  static const uint8_t kNextBitFieldIndex = BreakableTypeField::kNext;
333 };
334 
335 class Block : public BreakableStatement {
336  public:
337  ZonePtrList<Statement>* statements() { return &statements_; }
338  bool ignore_completion_value() const {
339  return IgnoreCompletionField::decode(bit_field_);
340  }
341 
342  inline ZonePtrList<const AstRawString>* labels() const;
343 
344  bool IsJump() const {
345  return !statements_.is_empty() && statements_.last()->IsJump() &&
346  labels() == nullptr; // Good enough as an approximation...
347  }
348 
349  Scope* scope() const { return scope_; }
350  void set_scope(Scope* scope) { scope_ = scope; }
351 
352  void InitializeStatements(const ScopedPtrList<Statement>& statements,
353  Zone* zone) {
354  DCHECK_EQ(0, statements_.length());
355  statements.CopyTo(&statements_, zone);
356  }
357 
358  private:
359  friend class AstNodeFactory;
360 
361  ZonePtrList<Statement> statements_;
362  Scope* scope_;
363 
364  class IgnoreCompletionField
365  : public BitField<bool, BreakableStatement::kNextBitFieldIndex, 1> {};
366  class IsLabeledField
367  : public BitField<bool, IgnoreCompletionField::kNext, 1> {};
368 
369  protected:
370  Block(Zone* zone, ZonePtrList<const AstRawString>* labels, int capacity,
371  bool ignore_completion_value)
372  : BreakableStatement(TARGET_FOR_NAMED_ONLY, kNoSourcePosition, kBlock),
373  statements_(capacity, zone),
374  scope_(nullptr) {
375  bit_field_ |= IgnoreCompletionField::encode(ignore_completion_value) |
376  IsLabeledField::encode(labels != nullptr);
377  }
378 
379  Block(ZonePtrList<const AstRawString>* labels, bool ignore_completion_value)
380  : Block(nullptr, labels, 0, ignore_completion_value) {}
381 };
382 
383 class LabeledBlock final : public Block {
384  private:
385  friend class AstNodeFactory;
386  friend class Block;
387 
389  int capacity, bool ignore_completion_value)
390  : Block(zone, labels, capacity, ignore_completion_value),
391  labels_(labels) {
392  DCHECK_NOT_NULL(labels);
393  DCHECK_GT(labels->length(), 0);
394  }
395 
397  bool ignore_completion_value)
398  : LabeledBlock(nullptr, labels, 0, ignore_completion_value) {}
399 
401 };
402 
403 inline ZonePtrList<const AstRawString>* Block::labels() const {
404  if (IsLabeledField::decode(bit_field_)) {
405  return static_cast<const LabeledBlock*>(this)->labels_;
406  }
407  return nullptr;
408 }
409 
410 class DoExpression final : public Expression {
411  public:
412  Block* block() { return block_; }
413  VariableProxy* result() { return result_; }
414 
415  private:
416  friend class AstNodeFactory;
417 
418  DoExpression(Block* block, VariableProxy* result, int pos)
419  : Expression(pos, kDoExpression), block_(block), result_(result) {
420  DCHECK_NOT_NULL(block_);
421  DCHECK_NOT_NULL(result_);
422  }
423 
424  Block* block_;
425  VariableProxy* result_;
426 };
427 
428 
429 class Declaration : public AstNode {
430  public:
432 
433  VariableProxy* proxy() const { return proxy_; }
434 
435  protected:
436  Declaration(VariableProxy* proxy, int pos, NodeType type)
437  : AstNode(pos, type), proxy_(proxy), next_(nullptr) {}
438 
439  private:
440  VariableProxy* proxy_;
441  // Declarations list threaded through the declarations.
442  Declaration** next() { return &next_; }
443  Declaration* next_;
444  friend List;
446 };
447 
449  public:
450  inline NestedVariableDeclaration* AsNested();
451 
452  private:
453  friend class AstNodeFactory;
454 
455  class IsNestedField
456  : public BitField<bool, Declaration::kNextBitFieldIndex, 1> {};
457 
458  protected:
459  VariableDeclaration(VariableProxy* proxy, int pos, bool is_nested = false)
460  : Declaration(proxy, pos, kVariableDeclaration) {
461  bit_field_ = IsNestedField::update(bit_field_, is_nested);
462  }
463 
464  static const uint8_t kNextBitFieldIndex = IsNestedField::kNext;
465 };
466 
467 // For var declarations that appear in a block scope.
468 // Only distinguished from VariableDeclaration during Scope analysis,
469 // so it doesn't get its own NodeType.
471  public:
472  Scope* scope() const { return scope_; }
473 
474  private:
475  friend class AstNodeFactory;
476 
477  NestedVariableDeclaration(VariableProxy* proxy, Scope* scope, int pos)
478  : VariableDeclaration(proxy, pos, true), scope_(scope) {}
479 
480  // Nested scope from which the declaration originated.
481  Scope* scope_;
482 };
483 
484 inline NestedVariableDeclaration* VariableDeclaration::AsNested() {
485  return IsNestedField::decode(bit_field_)
486  ? static_cast<NestedVariableDeclaration*>(this)
487  : nullptr;
488 }
489 
490 class FunctionDeclaration final : public Declaration {
491  public:
492  FunctionLiteral* fun() const { return fun_; }
493  bool declares_sloppy_block_function() const {
494  return DeclaresSloppyBlockFunction::decode(bit_field_);
495  }
496 
497  private:
498  friend class AstNodeFactory;
499 
500  class DeclaresSloppyBlockFunction
501  : public BitField<bool, Declaration::kNextBitFieldIndex, 1> {};
502 
504  bool declares_sloppy_block_function, int pos)
505  : Declaration(proxy, pos, kFunctionDeclaration), fun_(fun) {
506  bit_field_ = DeclaresSloppyBlockFunction::update(
507  bit_field_, declares_sloppy_block_function);
508  }
509 
510  FunctionLiteral* fun_;
511 
512  static const uint8_t kNextBitFieldIndex = DeclaresSloppyBlockFunction::kNext;
513 };
514 
515 
517  public:
518  Statement* body() const { return body_; }
519  void set_body(Statement* s) { body_ = s; }
520 
521  ZonePtrList<const AstRawString>* labels() const { return labels_; }
522 
523  // A list of all labels that the iteration statement is directly prefixed
524  // with, i.e. all the labels that a continue statement in the body can use to
525  // continue this iteration statement. This is always a subset of {labels}.
526  //
527  // Example: "l1: { l2: if (b) l3: l4: for (;;) s }"
528  // labels() of the Block will be l1.
529  // labels() of the ForStatement will be l2, l3, l4.
530  // own_labels() of the ForStatement will be l3, l4.
531  ZonePtrList<const AstRawString>* own_labels() const { return own_labels_; }
532 
533  protected:
535  ZonePtrList<const AstRawString>* own_labels, int pos,
536  NodeType type)
537  : BreakableStatement(TARGET_FOR_ANONYMOUS, pos, type),
538  labels_(labels),
539  own_labels_(own_labels),
540  body_(nullptr) {}
541  void Initialize(Statement* body) { body_ = body; }
542 
543  static const uint8_t kNextBitFieldIndex =
544  BreakableStatement::kNextBitFieldIndex;
545 
546  private:
548  ZonePtrList<const AstRawString>* own_labels_;
549  Statement* body_;
550 };
551 
552 
553 class DoWhileStatement final : public IterationStatement {
554  public:
555  void Initialize(Expression* cond, Statement* body) {
556  IterationStatement::Initialize(body);
557  cond_ = cond;
558  }
559 
560  Expression* cond() const { return cond_; }
561 
562  private:
563  friend class AstNodeFactory;
564 
566  ZonePtrList<const AstRawString>* own_labels, int pos)
567  : IterationStatement(labels, own_labels, pos, kDoWhileStatement),
568  cond_(nullptr) {}
569 
570  Expression* cond_;
571 };
572 
573 
574 class WhileStatement final : public IterationStatement {
575  public:
576  void Initialize(Expression* cond, Statement* body) {
577  IterationStatement::Initialize(body);
578  cond_ = cond;
579  }
580 
581  Expression* cond() const { return cond_; }
582 
583  private:
584  friend class AstNodeFactory;
585 
587  ZonePtrList<const AstRawString>* own_labels, int pos)
588  : IterationStatement(labels, own_labels, pos, kWhileStatement),
589  cond_(nullptr) {}
590 
591  Expression* cond_;
592 };
593 
594 
595 class ForStatement final : public IterationStatement {
596  public:
597  void Initialize(Statement* init, Expression* cond, Statement* next,
598  Statement* body) {
599  IterationStatement::Initialize(body);
600  init_ = init;
601  cond_ = cond;
602  next_ = next;
603  }
604 
605  Statement* init() const { return init_; }
606  Expression* cond() const { return cond_; }
607  Statement* next() const { return next_; }
608 
609  private:
610  friend class AstNodeFactory;
611 
613  ZonePtrList<const AstRawString>* own_labels, int pos)
614  : IterationStatement(labels, own_labels, pos, kForStatement),
615  init_(nullptr),
616  cond_(nullptr),
617  next_(nullptr) {}
618 
619  Statement* init_;
620  Expression* cond_;
621  Statement* next_;
622 };
623 
624 
626  public:
627  enum VisitMode {
628  ENUMERATE, // for (each in subject) body;
629  ITERATE // for (each of subject) body;
630  };
631 
632  using IterationStatement::Initialize;
633 
634  static const char* VisitModeString(VisitMode mode) {
635  return mode == ITERATE ? "for-of" : "for-in";
636  }
637 
638  protected:
640  ZonePtrList<const AstRawString>* own_labels, int pos,
641  NodeType type)
642  : IterationStatement(labels, own_labels, pos, type) {}
643 };
644 
645 
646 class ForInStatement final : public ForEachStatement {
647  public:
648  void Initialize(Expression* each, Expression* subject, Statement* body) {
649  ForEachStatement::Initialize(body);
650  each_ = each;
651  subject_ = subject;
652  }
653 
654  Expression* enumerable() const {
655  return subject();
656  }
657 
658  Expression* each() const { return each_; }
659  Expression* subject() const { return subject_; }
660 
661  enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
662  ForInType for_in_type() const { return ForInTypeField::decode(bit_field_); }
663 
664  private:
665  friend class AstNodeFactory;
666 
668  ZonePtrList<const AstRawString>* own_labels, int pos)
669  : ForEachStatement(labels, own_labels, pos, kForInStatement),
670  each_(nullptr),
671  subject_(nullptr) {
672  bit_field_ = ForInTypeField::update(bit_field_, SLOW_FOR_IN);
673  }
674 
675  Expression* each_;
676  Expression* subject_;
677 
678  class ForInTypeField
679  : public BitField<ForInType, ForEachStatement::kNextBitFieldIndex, 1> {};
680 };
681 
682 
683 class ForOfStatement final : public ForEachStatement {
684  public:
685  void Initialize(Statement* body, Variable* iterator,
686  Expression* assign_iterator, Expression* assign_next,
687  Expression* next_result, Expression* result_done,
688  Expression* assign_each) {
689  ForEachStatement::Initialize(body);
690  iterator_ = iterator;
691  assign_iterator_ = assign_iterator;
692  assign_next_ = assign_next;
693  next_result_ = next_result;
694  result_done_ = result_done;
695  assign_each_ = assign_each;
696  }
697 
698  Variable* iterator() const {
699  return iterator_;
700  }
701 
702  // iterator = subject[Symbol.iterator]()
703  Expression* assign_iterator() const {
704  return assign_iterator_;
705  }
706 
707  // iteratorRecord.next = iterator.next
708  Expression* assign_next() const { return assign_next_; }
709 
710  // result = iterator.next() // with type check
711  Expression* next_result() const {
712  return next_result_;
713  }
714 
715  // result.done
716  Expression* result_done() const {
717  return result_done_;
718  }
719 
720  // each = result.value
721  Expression* assign_each() const {
722  return assign_each_;
723  }
724 
725  void set_assign_iterator(Expression* e) { assign_iterator_ = e; }
726  void set_assign_next(Expression* e) { assign_next_ = e; }
727  void set_next_result(Expression* e) { next_result_ = e; }
728  void set_result_done(Expression* e) { result_done_ = e; }
729  void set_assign_each(Expression* e) { assign_each_ = e; }
730 
731  private:
732  friend class AstNodeFactory;
733 
735  ZonePtrList<const AstRawString>* own_labels, int pos)
736  : ForEachStatement(labels, own_labels, pos, kForOfStatement),
737  iterator_(nullptr),
738  assign_iterator_(nullptr),
739  next_result_(nullptr),
740  result_done_(nullptr),
741  assign_each_(nullptr) {}
742 
743  Variable* iterator_;
744  Expression* assign_iterator_;
745  Expression* assign_next_;
746  Expression* next_result_;
747  Expression* result_done_;
748  Expression* assign_each_;
749 };
750 
751 
752 class ExpressionStatement final : public Statement {
753  public:
754  void set_expression(Expression* e) { expression_ = e; }
755  Expression* expression() const { return expression_; }
756  bool IsJump() const { return expression_->IsThrow(); }
757 
758  private:
759  friend class AstNodeFactory;
760 
761  ExpressionStatement(Expression* expression, int pos)
762  : Statement(pos, kExpressionStatement), expression_(expression) {}
763 
764  Expression* expression_;
765 };
766 
767 
768 class JumpStatement : public Statement {
769  public:
770  bool IsJump() const { return true; }
771 
772  protected:
773  JumpStatement(int pos, NodeType type) : Statement(pos, type) {}
774 };
775 
776 
777 class ContinueStatement final : public JumpStatement {
778  public:
779  IterationStatement* target() const { return target_; }
780 
781  private:
782  friend class AstNodeFactory;
783 
784  ContinueStatement(IterationStatement* target, int pos)
785  : JumpStatement(pos, kContinueStatement), target_(target) {}
786 
787  IterationStatement* target_;
788 };
789 
790 
791 class BreakStatement final : public JumpStatement {
792  public:
793  BreakableStatement* target() const { return target_; }
794 
795  private:
796  friend class AstNodeFactory;
797 
798  BreakStatement(BreakableStatement* target, int pos)
799  : JumpStatement(pos, kBreakStatement), target_(target) {}
800 
801  BreakableStatement* target_;
802 };
803 
804 
805 class ReturnStatement final : public JumpStatement {
806  public:
807  enum Type { kNormal, kAsyncReturn };
808  Expression* expression() const { return expression_; }
809 
810  Type type() const { return TypeField::decode(bit_field_); }
811  bool is_async_return() const { return type() == kAsyncReturn; }
812 
813  int end_position() const { return end_position_; }
814 
815  private:
816  friend class AstNodeFactory;
817 
818  ReturnStatement(Expression* expression, Type type, int pos, int end_position)
819  : JumpStatement(pos, kReturnStatement),
820  expression_(expression),
821  end_position_(end_position) {
822  bit_field_ |= TypeField::encode(type);
823  }
824 
825  Expression* expression_;
826  int end_position_;
827 
828  class TypeField
829  : public BitField<Type, JumpStatement::kNextBitFieldIndex, 1> {};
830 };
831 
832 
833 class WithStatement final : public Statement {
834  public:
835  Scope* scope() { return scope_; }
836  Expression* expression() const { return expression_; }
837  Statement* statement() const { return statement_; }
838  void set_statement(Statement* s) { statement_ = s; }
839 
840  private:
841  friend class AstNodeFactory;
842 
843  WithStatement(Scope* scope, Expression* expression, Statement* statement,
844  int pos)
845  : Statement(pos, kWithStatement),
846  scope_(scope),
847  expression_(expression),
848  statement_(statement) {}
849 
850  Scope* scope_;
851  Expression* expression_;
852  Statement* statement_;
853 };
854 
855 class CaseClause final : public ZoneObject {
856  public:
857  bool is_default() const { return label_ == nullptr; }
858  Expression* label() const {
859  DCHECK(!is_default());
860  return label_;
861  }
862  ZonePtrList<Statement>* statements() { return &statements_; }
863 
864  private:
865  friend class AstNodeFactory;
866 
867  CaseClause(Zone* zone, Expression* label,
868  const ScopedPtrList<Statement>& statements);
869 
870  Expression* label_;
871  ZonePtrList<Statement> statements_;
872 };
873 
874 
875 class SwitchStatement final : public BreakableStatement {
876  public:
877  ZonePtrList<const AstRawString>* labels() const { return labels_; }
878 
879  Expression* tag() const { return tag_; }
880  void set_tag(Expression* t) { tag_ = t; }
881 
882  ZonePtrList<CaseClause>* cases() { return &cases_; }
883 
884  private:
885  friend class AstNodeFactory;
886 
888  Expression* tag, int pos)
889  : BreakableStatement(TARGET_FOR_ANONYMOUS, pos, kSwitchStatement),
890  labels_(labels),
891  tag_(tag),
892  cases_(4, zone) {}
893 
895  Expression* tag_;
897 };
898 
899 
900 // If-statements always have non-null references to their then- and
901 // else-parts. When parsing if-statements with no explicit else-part,
902 // the parser implicitly creates an empty statement. Use the
903 // HasThenStatement() and HasElseStatement() functions to check if a
904 // given if-statement has a then- or an else-part containing code.
905 class IfStatement final : public Statement {
906  public:
907  bool HasThenStatement() const { return !then_statement_->IsEmptyStatement(); }
908  bool HasElseStatement() const { return !else_statement_->IsEmptyStatement(); }
909 
910  Expression* condition() const { return condition_; }
911  Statement* then_statement() const { return then_statement_; }
912  Statement* else_statement() const { return else_statement_; }
913 
914  void set_then_statement(Statement* s) { then_statement_ = s; }
915  void set_else_statement(Statement* s) { else_statement_ = s; }
916 
917  bool IsJump() const {
918  return HasThenStatement() && then_statement()->IsJump()
919  && HasElseStatement() && else_statement()->IsJump();
920  }
921 
922  private:
923  friend class AstNodeFactory;
924 
925  IfStatement(Expression* condition, Statement* then_statement,
926  Statement* else_statement, int pos)
927  : Statement(pos, kIfStatement),
928  condition_(condition),
929  then_statement_(then_statement),
930  else_statement_(else_statement) {}
931 
932  Expression* condition_;
933  Statement* then_statement_;
934  Statement* else_statement_;
935 };
936 
937 
938 class TryStatement : public Statement {
939  public:
940  Block* try_block() const { return try_block_; }
941  void set_try_block(Block* b) { try_block_ = b; }
942 
943  protected:
944  TryStatement(Block* try_block, int pos, NodeType type)
945  : Statement(pos, type), try_block_(try_block) {}
946 
947  private:
948  Block* try_block_;
949 };
950 
951 
952 class TryCatchStatement final : public TryStatement {
953  public:
954  Scope* scope() { return scope_; }
955  Block* catch_block() const { return catch_block_; }
956  void set_catch_block(Block* b) { catch_block_ = b; }
957 
958  // Prediction of whether exceptions thrown into the handler for this try block
959  // will be caught.
960  //
961  // BytecodeGenerator tracks the state of catch prediction, which can change
962  // with each TryCatchStatement encountered. The tracked catch prediction is
963  // later compiled into the code's handler table. The runtime uses this
964  // information to implement a feature that notifies the debugger when an
965  // uncaught exception is thrown, _before_ the exception propagates to the top.
966  //
967  // If this try/catch statement is meant to rethrow (HandlerTable::UNCAUGHT),
968  // the catch prediction value is set to the same value as the surrounding
969  // catch prediction.
970  //
971  // Since it's generally undecidable whether an exception will be caught, our
972  // prediction is only an approximation.
973  // ---------------------------------------------------------------------------
974  inline HandlerTable::CatchPrediction GetCatchPrediction(
975  HandlerTable::CatchPrediction outer_catch_prediction) const {
976  if (catch_prediction_ == HandlerTable::UNCAUGHT) {
977  return outer_catch_prediction;
978  }
979  return catch_prediction_;
980  }
981 
982  // Indicates whether or not code should be generated to clear the pending
983  // exception. The pending exception is cleared for cases where the exception
984  // is not guaranteed to be rethrown, indicated by the value
985  // HandlerTable::UNCAUGHT. If both the current and surrounding catch handler's
986  // are predicted uncaught, the exception is not cleared.
987  //
988  // If this handler is not going to simply rethrow the exception, this method
989  // indicates that the isolate's pending exception message should be cleared
990  // before executing the catch_block.
991  // In the normal use case, this flag is always on because the message object
992  // is not needed anymore when entering the catch block and should not be
993  // kept alive.
994  // The use case where the flag is off is when the catch block is guaranteed
995  // to rethrow the caught exception (using %ReThrow), which reuses the
996  // pending message instead of generating a new one.
997  // (When the catch block doesn't rethrow but is guaranteed to perform an
998  // ordinary throw, not clearing the old message is safe but not very
999  // useful.)
1000  inline bool ShouldClearPendingException(
1001  HandlerTable::CatchPrediction outer_catch_prediction) const {
1002  return catch_prediction_ != HandlerTable::UNCAUGHT ||
1003  outer_catch_prediction != HandlerTable::UNCAUGHT;
1004  }
1005 
1006  private:
1007  friend class AstNodeFactory;
1008 
1009  TryCatchStatement(Block* try_block, Scope* scope, Block* catch_block,
1010  HandlerTable::CatchPrediction catch_prediction, int pos)
1011  : TryStatement(try_block, pos, kTryCatchStatement),
1012  scope_(scope),
1013  catch_block_(catch_block),
1014  catch_prediction_(catch_prediction) {}
1015 
1016  Scope* scope_;
1017  Block* catch_block_;
1018  HandlerTable::CatchPrediction catch_prediction_;
1019 };
1020 
1021 
1022 class TryFinallyStatement final : public TryStatement {
1023  public:
1024  Block* finally_block() const { return finally_block_; }
1025  void set_finally_block(Block* b) { finally_block_ = b; }
1026 
1027  private:
1028  friend class AstNodeFactory;
1029 
1030  TryFinallyStatement(Block* try_block, Block* finally_block, int pos)
1031  : TryStatement(try_block, pos, kTryFinallyStatement),
1032  finally_block_(finally_block) {}
1033 
1034  Block* finally_block_;
1035 };
1036 
1037 
1038 class DebuggerStatement final : public Statement {
1039  private:
1040  friend class AstNodeFactory;
1041 
1042  explicit DebuggerStatement(int pos) : Statement(pos, kDebuggerStatement) {}
1043 };
1044 
1045 
1046 class EmptyStatement final : public Statement {
1047  private:
1048  friend class AstNodeFactory;
1049  EmptyStatement() : Statement(kNoSourcePosition, kEmptyStatement) {}
1050 };
1051 
1052 
1053 // Delegates to another statement, which may be overwritten.
1054 // This was introduced to implement ES2015 Annex B3.3 for conditionally making
1055 // sloppy-mode block-scoped functions have a var binding, which is changed
1056 // from one statement to another during parsing.
1058  public:
1059  Statement* statement() const { return statement_; }
1060  void set_statement(Statement* statement) { statement_ = statement; }
1061 
1062  private:
1063  friend class AstNodeFactory;
1064 
1065  explicit SloppyBlockFunctionStatement(Statement* statement)
1066  : Statement(kNoSourcePosition, kSloppyBlockFunctionStatement),
1067  statement_(statement) {}
1068 
1069  Statement* statement_;
1070 };
1071 
1072 
1073 class Literal final : public Expression {
1074  public:
1075  enum Type {
1076  kSmi,
1077  kHeapNumber,
1078  kBigInt,
1079  kString,
1080  kSymbol,
1081  kBoolean,
1082  kUndefined,
1083  kNull,
1084  kTheHole,
1085  };
1086 
1087  Type type() const { return TypeField::decode(bit_field_); }
1088 
1089  // Returns true if literal represents a property name (i.e. cannot be parsed
1090  // as array indices).
1091  bool IsPropertyName() const;
1092 
1093  // Returns true if literal represents an array index.
1094  // Note, that in general the following statement is not true:
1095  // key->IsPropertyName() != key->AsArrayIndex(...)
1096  // but for non-computed LiteralProperty properties the following is true:
1097  // property->key()->IsPropertyName() != property->key()->AsArrayIndex(...)
1098  bool AsArrayIndex(uint32_t* index) const;
1099 
1100  const AstRawString* AsRawPropertyName() {
1101  DCHECK(IsPropertyName());
1102  return string_;
1103  }
1104 
1105  Smi AsSmiLiteral() const {
1106  DCHECK_EQ(kSmi, type());
1107  return Smi::FromInt(smi_);
1108  }
1109 
1110  // Returns true if literal represents a Number.
1111  bool IsNumber() const { return type() == kHeapNumber || type() == kSmi; }
1112  double AsNumber() const {
1113  DCHECK(IsNumber());
1114  switch (type()) {
1115  case kSmi:
1116  return smi_;
1117  case kHeapNumber:
1118  return number_;
1119  default:
1120  UNREACHABLE();
1121  }
1122  }
1123 
1124  AstBigInt AsBigInt() const {
1125  DCHECK_EQ(type(), kBigInt);
1126  return bigint_;
1127  }
1128 
1129  bool IsString() const { return type() == kString; }
1130  const AstRawString* AsRawString() {
1131  DCHECK_EQ(type(), kString);
1132  return string_;
1133  }
1134 
1135  AstSymbol AsSymbol() {
1136  DCHECK_EQ(type(), kSymbol);
1137  return symbol_;
1138  }
1139 
1140  V8_EXPORT_PRIVATE bool ToBooleanIsTrue() const;
1141  bool ToBooleanIsFalse() const { return !ToBooleanIsTrue(); }
1142 
1143  bool ToUint32(uint32_t* value) const;
1144 
1145  // Returns an appropriate Object representing this Literal, allocating
1146  // a heap object if needed.
1147  Handle<Object> BuildValue(Isolate* isolate) const;
1148 
1149  // Support for using Literal as a HashMap key. NOTE: Currently, this works
1150  // only for string and number literals!
1151  uint32_t Hash();
1152  static bool Match(void* literal1, void* literal2);
1153 
1154  private:
1155  friend class AstNodeFactory;
1156 
1157  class TypeField : public BitField<Type, Expression::kNextBitFieldIndex, 4> {};
1158 
1159  Literal(int smi, int position) : Expression(position, kLiteral), smi_(smi) {
1160  bit_field_ = TypeField::update(bit_field_, kSmi);
1161  }
1162 
1163  Literal(double number, int position)
1164  : Expression(position, kLiteral), number_(number) {
1165  bit_field_ = TypeField::update(bit_field_, kHeapNumber);
1166  }
1167 
1168  Literal(AstBigInt bigint, int position)
1169  : Expression(position, kLiteral), bigint_(bigint) {
1170  bit_field_ = TypeField::update(bit_field_, kBigInt);
1171  }
1172 
1173  Literal(const AstRawString* string, int position)
1174  : Expression(position, kLiteral), string_(string) {
1175  bit_field_ = TypeField::update(bit_field_, kString);
1176  }
1177 
1178  Literal(AstSymbol symbol, int position)
1179  : Expression(position, kLiteral), symbol_(symbol) {
1180  bit_field_ = TypeField::update(bit_field_, kSymbol);
1181  }
1182 
1183  Literal(bool boolean, int position)
1184  : Expression(position, kLiteral), boolean_(boolean) {
1185  bit_field_ = TypeField::update(bit_field_, kBoolean);
1186  }
1187 
1188  Literal(Type type, int position) : Expression(position, kLiteral) {
1189  DCHECK(type == kNull || type == kUndefined || type == kTheHole);
1190  bit_field_ = TypeField::update(bit_field_, type);
1191  }
1192 
1193  union {
1194  const AstRawString* string_;
1195  int smi_;
1196  double number_;
1197  AstSymbol symbol_;
1198  AstBigInt bigint_;
1199  bool boolean_;
1200  };
1201 };
1202 
1203 // Base class for literals that need space in the type feedback vector.
1205  public:
1206  // A Materializedliteral is simple if the values consist of only
1207  // constants and simple object and array literals.
1208  bool IsSimple() const;
1209 
1210  protected:
1211  MaterializedLiteral(int pos, NodeType type) : Expression(pos, type) {}
1212 
1213  friend class CompileTimeValue;
1214  friend class ArrayLiteral;
1215  friend class ObjectLiteral;
1216 
1217  // Populate the depth field and any flags the literal has, returns the depth.
1218  int InitDepthAndFlags();
1219 
1220  bool NeedsInitialAllocationSite();
1221 
1222  // Populate the constant properties/elements fixed array.
1223  void BuildConstants(Isolate* isolate);
1224 
1225  // If the expression is a literal, return the literal value;
1226  // if the expression is a materialized literal and is_simple
1227  // then return an Array or Object Boilerplate Description
1228  // Otherwise, return undefined literal as the placeholder
1229  // in the object literal boilerplate.
1230  Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1231 };
1232 
1233 // Node for capturing a regexp literal.
1234 class RegExpLiteral final : public MaterializedLiteral {
1235  public:
1236  Handle<String> pattern() const { return pattern_->string(); }
1237  const AstRawString* raw_pattern() const { return pattern_; }
1238  int flags() const { return flags_; }
1239 
1240  private:
1241  friend class AstNodeFactory;
1242 
1243  RegExpLiteral(const AstRawString* pattern, int flags, int pos)
1244  : MaterializedLiteral(pos, kRegExpLiteral),
1245  flags_(flags),
1246  pattern_(pattern) {}
1247 
1248  int const flags_;
1249  const AstRawString* const pattern_;
1250 };
1251 
1252 // Base class for Array and Object literals, providing common code for handling
1253 // nested subliterals.
1255  public:
1256  enum Flags {
1257  kNoFlags = 0,
1258  kIsShallow = 1,
1259  kDisableMementos = 1 << 1,
1260  kNeedsInitialAllocationSite = 1 << 2,
1261  };
1262 
1263  bool is_initialized() const { return 0 < depth_; }
1264  int depth() const {
1265  DCHECK(is_initialized());
1266  return depth_;
1267  }
1268 
1269  bool is_shallow() const { return depth() == 1; }
1270  bool needs_initial_allocation_site() const {
1271  return NeedsInitialAllocationSiteField::decode(bit_field_);
1272  }
1273 
1274  int ComputeFlags(bool disable_mementos = false) const {
1275  int flags = kNoFlags;
1276  if (is_shallow()) flags |= kIsShallow;
1277  if (disable_mementos) flags |= kDisableMementos;
1278  if (needs_initial_allocation_site()) flags |= kNeedsInitialAllocationSite;
1279  return flags;
1280  }
1281 
1282  // An AggregateLiteral is simple if the values consist of only
1283  // constants and simple object and array literals.
1284  bool is_simple() const { return IsSimpleField::decode(bit_field_); }
1285 
1286  private:
1287  int depth_ : 31;
1288  class NeedsInitialAllocationSiteField
1289  : public BitField<bool, MaterializedLiteral::kNextBitFieldIndex, 1> {};
1290  class IsSimpleField
1291  : public BitField<bool, NeedsInitialAllocationSiteField::kNext, 1> {};
1292 
1293  protected:
1294  friend class AstNodeFactory;
1295  AggregateLiteral(int pos, NodeType type)
1296  : MaterializedLiteral(pos, type), depth_(0) {
1297  bit_field_ |= NeedsInitialAllocationSiteField::encode(false) |
1298  IsSimpleField::encode(false);
1299  }
1300 
1301  void set_is_simple(bool is_simple) {
1302  bit_field_ = IsSimpleField::update(bit_field_, is_simple);
1303  }
1304 
1305  void set_depth(int depth) {
1306  DCHECK(!is_initialized());
1307  depth_ = depth;
1308  }
1309 
1310  void set_needs_initial_allocation_site(bool required) {
1311  bit_field_ = NeedsInitialAllocationSiteField::update(bit_field_, required);
1312  }
1313 
1314  static const uint8_t kNextBitFieldIndex = IsSimpleField::kNext;
1315 };
1316 
1317 // Common supertype for ObjectLiteralProperty and ClassLiteralProperty
1318 class LiteralProperty : public ZoneObject {
1319  public:
1320  Expression* key() const { return key_and_is_computed_name_.GetPointer(); }
1321  Expression* value() const { return value_; }
1322 
1323  bool is_computed_name() const {
1324  return key_and_is_computed_name_.GetPayload();
1325  }
1326  bool NeedsSetFunctionName() const;
1327 
1328  protected:
1329  LiteralProperty(Expression* key, Expression* value, bool is_computed_name)
1330  : key_and_is_computed_name_(key, is_computed_name), value_(value) {}
1331 
1332  PointerWithPayload<Expression, bool, 1> key_and_is_computed_name_;
1333  Expression* value_;
1334 };
1335 
1336 // Property is used for passing information
1337 // about an object literal's properties from the parser
1338 // to the code generator.
1340  public:
1341  enum Kind : uint8_t {
1342  CONSTANT, // Property with constant value (compile time).
1343  COMPUTED, // Property with computed value (execution time).
1344  MATERIALIZED_LITERAL, // Property value is a materialized literal.
1345  GETTER,
1346  SETTER, // Property is an accessor function.
1347  PROTOTYPE, // Property is __proto__.
1348  SPREAD
1349  };
1350 
1351  Kind kind() const { return kind_; }
1352 
1353  bool IsCompileTimeValue() const;
1354 
1355  void set_emit_store(bool emit_store);
1356  bool emit_store() const;
1357 
1358  bool IsNullPrototype() const {
1359  return IsPrototype() && value()->IsNullLiteral();
1360  }
1361  bool IsPrototype() const { return kind() == PROTOTYPE; }
1362 
1363  private:
1364  friend class AstNodeFactory;
1365 
1366  ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1367  bool is_computed_name);
1368  ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1369  Expression* value, bool is_computed_name);
1370 
1371  Kind kind_;
1372  bool emit_store_;
1373 };
1374 
1375 
1376 // An object literal has a boilerplate object that is used
1377 // for minimizing the work when constructing it at runtime.
1378 class ObjectLiteral final : public AggregateLiteral {
1379  public:
1381 
1382  Handle<ObjectBoilerplateDescription> boilerplate_description() const {
1383  DCHECK(!boilerplate_description_.is_null());
1384  return boilerplate_description_;
1385  }
1386  int properties_count() const { return boilerplate_properties_; }
1387  const ZonePtrList<Property>* properties() const { return &properties_; }
1388  bool has_elements() const { return HasElementsField::decode(bit_field_); }
1389  bool has_rest_property() const {
1390  return HasRestPropertyField::decode(bit_field_);
1391  }
1392  bool fast_elements() const { return FastElementsField::decode(bit_field_); }
1393  bool has_null_prototype() const {
1394  return HasNullPrototypeField::decode(bit_field_);
1395  }
1396 
1397  bool is_empty() const {
1398  DCHECK(is_initialized());
1399  return !has_elements() && properties_count() == 0 &&
1400  properties()->length() == 0;
1401  }
1402 
1403  bool IsEmptyObjectLiteral() const {
1404  return is_empty() && !has_null_prototype();
1405  }
1406 
1407  // Populate the depth field and flags, returns the depth.
1408  int InitDepthAndFlags();
1409 
1410  // Get the boilerplate description, populating it if necessary.
1411  Handle<ObjectBoilerplateDescription> GetOrBuildBoilerplateDescription(
1412  Isolate* isolate) {
1413  if (boilerplate_description_.is_null()) {
1414  BuildBoilerplateDescription(isolate);
1415  }
1416  return boilerplate_description();
1417  }
1418 
1419  // Populate the boilerplate description.
1420  void BuildBoilerplateDescription(Isolate* isolate);
1421 
1422  // Mark all computed expressions that are bound to a key that
1423  // is shadowed by a later occurrence of the same key. For the
1424  // marked expressions, no store code is emitted.
1425  void CalculateEmitStore(Zone* zone);
1426 
1427  // Determines whether the {CreateShallowObjectLiteratal} builtin can be used.
1428  bool IsFastCloningSupported() const;
1429 
1430  // Assemble bitfield of flags for the CreateObjectLiteral helper.
1431  int ComputeFlags(bool disable_mementos = false) const {
1432  int flags = AggregateLiteral::ComputeFlags(disable_mementos);
1433  if (fast_elements()) flags |= kFastElements;
1434  if (has_null_prototype()) flags |= kHasNullPrototype;
1435  return flags;
1436  }
1437 
1438  int EncodeLiteralType() {
1439  int flags = kNoFlags;
1440  if (fast_elements()) flags |= kFastElements;
1441  if (has_null_prototype()) flags |= kHasNullPrototype;
1442  return flags;
1443  }
1444 
1445  enum Flags {
1446  kFastElements = 1 << 3,
1447  kHasNullPrototype = 1 << 4,
1448  };
1449  STATIC_ASSERT(
1450  static_cast<int>(AggregateLiteral::kNeedsInitialAllocationSite) <
1451  static_cast<int>(kFastElements));
1452 
1453  struct Accessors: public ZoneObject {
1454  Accessors() : getter(nullptr), setter(nullptr) {}
1455  ObjectLiteralProperty* getter;
1456  ObjectLiteralProperty* setter;
1457  };
1458 
1459  private:
1460  friend class AstNodeFactory;
1461 
1462  ObjectLiteral(Zone* zone, const ScopedPtrList<Property>& properties,
1463  uint32_t boilerplate_properties, int pos,
1464  bool has_rest_property)
1465  : AggregateLiteral(pos, kObjectLiteral),
1466  boilerplate_properties_(boilerplate_properties),
1467  properties_(0, nullptr) {
1468  bit_field_ |= HasElementsField::encode(false) |
1469  HasRestPropertyField::encode(has_rest_property) |
1470  FastElementsField::encode(false) |
1471  HasNullPrototypeField::encode(false);
1472  properties.CopyTo(&properties_, zone);
1473  }
1474 
1475  void InitFlagsForPendingNullPrototype(int i);
1476 
1477  void set_has_elements(bool has_elements) {
1478  bit_field_ = HasElementsField::update(bit_field_, has_elements);
1479  }
1480  void set_fast_elements(bool fast_elements) {
1481  bit_field_ = FastElementsField::update(bit_field_, fast_elements);
1482  }
1483  void set_has_null_protoype(bool has_null_prototype) {
1484  bit_field_ = HasNullPrototypeField::update(bit_field_, has_null_prototype);
1485  }
1486 
1487  uint32_t boilerplate_properties_;
1488  Handle<ObjectBoilerplateDescription> boilerplate_description_;
1489  ZoneList<Property*> properties_;
1490 
1491  class HasElementsField
1492  : public BitField<bool, AggregateLiteral::kNextBitFieldIndex, 1> {};
1493  class HasRestPropertyField
1494  : public BitField<bool, HasElementsField::kNext, 1> {};
1495  class FastElementsField
1496  : public BitField<bool, HasRestPropertyField::kNext, 1> {};
1497  class HasNullPrototypeField
1498  : public BitField<bool, FastElementsField::kNext, 1> {};
1499 };
1500 
1501 
1502 // A map from property names to getter/setter pairs allocated in the zone.
1504  : public base::TemplateHashMap<Literal, ObjectLiteral::Accessors,
1505  bool (*)(void*, void*),
1506  ZoneAllocationPolicy> {
1507  public:
1508  explicit AccessorTable(Zone* zone)
1510  bool (*)(void*, void*), ZoneAllocationPolicy>(
1511  Literal::Match, ZoneAllocationPolicy(zone)),
1512  zone_(zone) {}
1513 
1514  Iterator lookup(Literal* literal) {
1515  Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
1516  if (it->second == nullptr) {
1517  it->second = new (zone_) ObjectLiteral::Accessors();
1518  }
1519  return it;
1520  }
1521 
1522  private:
1523  Zone* zone_;
1524 };
1525 
1526 
1527 // An array literal has a literals object that is used
1528 // for minimizing the work when constructing it at runtime.
1529 class ArrayLiteral final : public AggregateLiteral {
1530  public:
1531  Handle<ArrayBoilerplateDescription> boilerplate_description() const {
1532  return boilerplate_description_;
1533  }
1534 
1535  const ZonePtrList<Expression>* values() const { return &values_; }
1536 
1537  int first_spread_index() const { return first_spread_index_; }
1538 
1539  // Populate the depth field and flags, returns the depth.
1540  int InitDepthAndFlags();
1541 
1542  // Get the boilerplate description, populating it if necessary.
1543  Handle<ArrayBoilerplateDescription> GetOrBuildBoilerplateDescription(
1544  Isolate* isolate) {
1545  if (boilerplate_description_.is_null()) {
1546  BuildBoilerplateDescription(isolate);
1547  }
1548  return boilerplate_description();
1549  }
1550 
1551  // Populate the boilerplate description.
1552  void BuildBoilerplateDescription(Isolate* isolate);
1553 
1554  // Determines whether the {CreateShallowArrayLiteral} builtin can be used.
1555  bool IsFastCloningSupported() const;
1556 
1557  // Assemble bitfield of flags for the CreateArrayLiteral helper.
1558  int ComputeFlags(bool disable_mementos = false) const {
1559  return AggregateLiteral::ComputeFlags(disable_mementos);
1560  }
1561 
1562  private:
1563  friend class AstNodeFactory;
1564 
1565  ArrayLiteral(Zone* zone, const ScopedPtrList<Expression>& values,
1566  int first_spread_index, int pos)
1567  : AggregateLiteral(pos, kArrayLiteral),
1568  first_spread_index_(first_spread_index),
1569  values_(0, nullptr) {
1570  values.CopyTo(&values_, zone);
1571  }
1572 
1573  int first_spread_index_;
1574  Handle<ArrayBoilerplateDescription> boilerplate_description_;
1575  ZonePtrList<Expression> values_;
1576 };
1577 
1578 enum class HoleCheckMode { kRequired, kElided };
1579 
1580 class VariableProxy final : public Expression {
1581  public:
1582  bool IsValidReferenceExpression() const {
1583  return !is_this() && !is_new_target();
1584  }
1585 
1586  Handle<String> name() const { return raw_name()->string(); }
1587  const AstRawString* raw_name() const {
1588  return is_resolved() ? var_->raw_name() : raw_name_;
1589  }
1590 
1591  Variable* var() const {
1592  DCHECK(is_resolved());
1593  return var_;
1594  }
1595  void set_var(Variable* v) {
1596  DCHECK(!is_resolved());
1597  DCHECK_NOT_NULL(v);
1598  var_ = v;
1599  }
1600 
1601  bool is_this() const { return IsThisField::decode(bit_field_); }
1602 
1603  bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1604  void set_is_assigned() {
1605  bit_field_ = IsAssignedField::update(bit_field_, true);
1606  if (is_resolved()) {
1607  var()->set_maybe_assigned();
1608  }
1609  }
1610 
1611  bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1612  void set_is_resolved() {
1613  bit_field_ = IsResolvedField::update(bit_field_, true);
1614  }
1615 
1616  bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1617  void set_is_new_target() {
1618  bit_field_ = IsNewTargetField::update(bit_field_, true);
1619  }
1620 
1621  HoleCheckMode hole_check_mode() const {
1622  HoleCheckMode mode = HoleCheckModeField::decode(bit_field_);
1623  DCHECK_IMPLIES(mode == HoleCheckMode::kRequired,
1624  var()->binding_needs_init() ||
1625  var()->local_if_not_shadowed()->binding_needs_init());
1626  return mode;
1627  }
1628  void set_needs_hole_check() {
1629  bit_field_ =
1630  HoleCheckModeField::update(bit_field_, HoleCheckMode::kRequired);
1631  }
1632 
1633  bool is_private_name() const { return IsPrivateName::decode(bit_field_); }
1634  void set_is_private_name() {
1635  bit_field_ = IsPrivateName::update(bit_field_, true);
1636  }
1637 
1638  // Bind this proxy to the variable var.
1639  void BindTo(Variable* var);
1640 
1641  V8_INLINE VariableProxy* next_unresolved() { return next_unresolved_; }
1642  V8_INLINE bool is_removed_from_unresolved() const {
1643  return IsRemovedFromUnresolvedField::decode(bit_field_);
1644  }
1645 
1646  void mark_removed_from_unresolved() {
1647  bit_field_ = IsRemovedFromUnresolvedField::update(bit_field_, true);
1648  }
1649 
1650  // Provides an access type for the ThreadedList used by the PreParsers
1651  // expressions, lists, and formal parameters.
1652  struct PreParserNext {
1653  static VariableProxy** next(VariableProxy* t) {
1654  return t->pre_parser_expr_next();
1655  }
1656 
1657  static VariableProxy** start(VariableProxy** head) { return head; }
1658  };
1659 
1660  // Provides an access type for the ThreadedList used by the PreParsers
1661  // expressions, lists, and formal parameters.
1663  static VariableProxy** filter(VariableProxy** t) {
1664  VariableProxy** n = t;
1665  // Skip over possibly removed values.
1666  while (*n != nullptr && (*n)->is_removed_from_unresolved()) {
1667  n = (*n)->next();
1668  }
1669  return n;
1670  }
1671 
1672  static VariableProxy** start(VariableProxy** head) { return filter(head); }
1673 
1674  static VariableProxy** next(VariableProxy* t) { return filter(t->next()); }
1675  };
1676 
1677  private:
1678  friend class AstNodeFactory;
1679 
1680  VariableProxy(Variable* var, int start_position);
1681 
1682  VariableProxy(const AstRawString* name, VariableKind variable_kind,
1683  int start_position)
1684  : Expression(start_position, kVariableProxy),
1685  raw_name_(name),
1686  next_unresolved_(nullptr),
1687  pre_parser_expr_next_(nullptr) {
1688  bit_field_ |= IsThisField::encode(variable_kind == THIS_VARIABLE) |
1689  IsAssignedField::encode(false) |
1690  IsResolvedField::encode(false) |
1691  IsRemovedFromUnresolvedField::encode(false) |
1692  HoleCheckModeField::encode(HoleCheckMode::kElided) |
1693  IsPrivateName::encode(false);
1694  }
1695 
1696  explicit VariableProxy(const VariableProxy* copy_from);
1697 
1698  class IsThisField : public BitField<bool, Expression::kNextBitFieldIndex, 1> {
1699  };
1700  class IsAssignedField : public BitField<bool, IsThisField::kNext, 1> {};
1701  class IsResolvedField : public BitField<bool, IsAssignedField::kNext, 1> {};
1702  class IsRemovedFromUnresolvedField
1703  : public BitField<bool, IsResolvedField::kNext, 1> {};
1704  class IsNewTargetField
1705  : public BitField<bool, IsRemovedFromUnresolvedField::kNext, 1> {};
1706  class HoleCheckModeField
1707  : public BitField<HoleCheckMode, IsNewTargetField::kNext, 1> {};
1708  class IsPrivateName : public BitField<bool, HoleCheckModeField::kNext, 1> {};
1709 
1710  union {
1711  const AstRawString* raw_name_; // if !is_resolved_
1712  Variable* var_; // if is_resolved_
1713  };
1714 
1715  V8_INLINE VariableProxy** next() { return &next_unresolved_; }
1716  VariableProxy* next_unresolved_;
1717 
1718  VariableProxy** pre_parser_expr_next() { return &pre_parser_expr_next_; }
1719  VariableProxy* pre_parser_expr_next_;
1720 
1721  friend base::ThreadedListTraits<VariableProxy>;
1722 };
1723 
1724 // Left-hand side can only be a property, a global or a (parameter or local)
1725 // slot.
1726 enum LhsKind {
1727  VARIABLE,
1728  NAMED_PROPERTY,
1729  KEYED_PROPERTY,
1730  NAMED_SUPER_PROPERTY,
1731  KEYED_SUPER_PROPERTY
1732 };
1733 
1734 class Property final : public Expression {
1735  public:
1736  bool IsValidReferenceExpression() const { return true; }
1737 
1738  Expression* obj() const { return obj_; }
1739  Expression* key() const { return key_; }
1740 
1741  bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1742 
1743  // Returns the properties assign type.
1744  static LhsKind GetAssignType(Property* property) {
1745  if (property == nullptr) return VARIABLE;
1746  bool super_access = property->IsSuperAccess();
1747  return (property->key()->IsPropertyName())
1748  ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1749  : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1750  }
1751 
1752  private:
1753  friend class AstNodeFactory;
1754 
1755  Property(Expression* obj, Expression* key, int pos)
1756  : Expression(pos, kProperty), obj_(obj), key_(key) {
1757  }
1758 
1759  Expression* obj_;
1760  Expression* key_;
1761 };
1762 
1763 // ResolvedProperty pairs a receiver field with a value field. It allows Call
1764 // to support arbitrary receivers while still taking advantage of TypeFeedback.
1765 class ResolvedProperty final : public Expression {
1766  public:
1767  VariableProxy* object() const { return object_; }
1768  VariableProxy* property() const { return property_; }
1769 
1770  void set_object(VariableProxy* e) { object_ = e; }
1771  void set_property(VariableProxy* e) { property_ = e; }
1772 
1773  private:
1774  friend class AstNodeFactory;
1775 
1776  ResolvedProperty(VariableProxy* obj, VariableProxy* property, int pos)
1777  : Expression(pos, kResolvedProperty), object_(obj), property_(property) {}
1778 
1779  VariableProxy* object_;
1780  VariableProxy* property_;
1781 };
1782 
1783 class Call final : public Expression {
1784  public:
1785  Expression* expression() const { return expression_; }
1786  const ZonePtrList<Expression>* arguments() const { return &arguments_; }
1787 
1788  bool is_possibly_eval() const {
1789  return IsPossiblyEvalField::decode(bit_field_);
1790  }
1791 
1792  bool is_tagged_template() const {
1793  return IsTaggedTemplateField::decode(bit_field_);
1794  }
1795 
1796  bool only_last_arg_is_spread() {
1797  return !arguments_.is_empty() && arguments_.last()->IsSpread();
1798  }
1799 
1800  enum CallType {
1801  GLOBAL_CALL,
1802  WITH_CALL,
1803  NAMED_PROPERTY_CALL,
1804  KEYED_PROPERTY_CALL,
1805  NAMED_SUPER_PROPERTY_CALL,
1806  KEYED_SUPER_PROPERTY_CALL,
1807  SUPER_CALL,
1808  RESOLVED_PROPERTY_CALL,
1809  OTHER_CALL
1810  };
1811 
1812  enum PossiblyEval {
1813  IS_POSSIBLY_EVAL,
1814  NOT_EVAL,
1815  };
1816 
1817  // Helpers to determine how to handle the call.
1818  CallType GetCallType() const;
1819 
1820  enum class TaggedTemplateTag { kTrue };
1821 
1822  private:
1823  friend class AstNodeFactory;
1824 
1825  Call(Zone* zone, Expression* expression,
1826  const ScopedPtrList<Expression>& arguments, int pos,
1827  PossiblyEval possibly_eval)
1828  : Expression(pos, kCall),
1829  expression_(expression),
1830  arguments_(0, nullptr) {
1831  bit_field_ |=
1832  IsPossiblyEvalField::encode(possibly_eval == IS_POSSIBLY_EVAL) |
1833  IsTaggedTemplateField::encode(false);
1834  arguments.CopyTo(&arguments_, zone);
1835  }
1836 
1837  Call(Zone* zone, Expression* expression,
1838  const ScopedPtrList<Expression>& arguments, int pos,
1839  TaggedTemplateTag tag)
1840  : Expression(pos, kCall),
1841  expression_(expression),
1842  arguments_(0, nullptr) {
1843  bit_field_ |= IsPossiblyEvalField::encode(false) |
1844  IsTaggedTemplateField::encode(true);
1845  arguments.CopyTo(&arguments_, zone);
1846  }
1847 
1848  class IsPossiblyEvalField
1849  : public BitField<bool, Expression::kNextBitFieldIndex, 1> {};
1850  class IsTaggedTemplateField
1851  : public BitField<bool, IsPossiblyEvalField::kNext, 1> {};
1852 
1853  Expression* expression_;
1854  ZonePtrList<Expression> arguments_;
1855 };
1856 
1857 
1858 class CallNew final : public Expression {
1859  public:
1860  Expression* expression() const { return expression_; }
1861  const ZonePtrList<Expression>* arguments() const { return &arguments_; }
1862 
1863  bool only_last_arg_is_spread() {
1864  return !arguments_.is_empty() && arguments_.last()->IsSpread();
1865  }
1866 
1867  private:
1868  friend class AstNodeFactory;
1869 
1870  CallNew(Zone* zone, Expression* expression,
1871  const ScopedPtrList<Expression>& arguments, int pos)
1872  : Expression(pos, kCallNew),
1873  expression_(expression),
1874  arguments_(0, nullptr) {
1875  arguments.CopyTo(&arguments_, zone);
1876  }
1877 
1878  Expression* expression_;
1879  ZonePtrList<Expression> arguments_;
1880 };
1881 
1882 // The CallRuntime class does not represent any official JavaScript
1883 // language construct. Instead it is used to call a C or JS function
1884 // with a set of arguments. This is used from the builtins that are
1885 // implemented in JavaScript.
1886 class CallRuntime final : public Expression {
1887  public:
1888  const ZonePtrList<Expression>* arguments() const { return &arguments_; }
1889  bool is_jsruntime() const { return function_ == nullptr; }
1890 
1891  int context_index() const {
1892  DCHECK(is_jsruntime());
1893  return context_index_;
1894  }
1895  const Runtime::Function* function() const {
1896  DCHECK(!is_jsruntime());
1897  return function_;
1898  }
1899 
1900  const char* debug_name();
1901 
1902  private:
1903  friend class AstNodeFactory;
1904 
1905  CallRuntime(Zone* zone, const Runtime::Function* function,
1906  const ScopedPtrList<Expression>& arguments, int pos)
1907  : Expression(pos, kCallRuntime),
1908  function_(function),
1909  arguments_(0, nullptr) {
1910  arguments.CopyTo(&arguments_, zone);
1911  }
1912  CallRuntime(Zone* zone, int context_index,
1913  const ScopedPtrList<Expression>& arguments, int pos)
1914  : Expression(pos, kCallRuntime),
1915  context_index_(context_index),
1916  function_(nullptr),
1917  arguments_(0, nullptr) {
1918  arguments.CopyTo(&arguments_, zone);
1919  }
1920 
1921  int context_index_;
1922  const Runtime::Function* function_;
1923  ZonePtrList<Expression> arguments_;
1924 };
1925 
1926 
1927 class UnaryOperation final : public Expression {
1928  public:
1929  Token::Value op() const { return OperatorField::decode(bit_field_); }
1930  Expression* expression() const { return expression_; }
1931 
1932  private:
1933  friend class AstNodeFactory;
1934 
1935  UnaryOperation(Token::Value op, Expression* expression, int pos)
1936  : Expression(pos, kUnaryOperation), expression_(expression) {
1937  bit_field_ |= OperatorField::encode(op);
1938  DCHECK(Token::IsUnaryOp(op));
1939  }
1940 
1941  Expression* expression_;
1942 
1943  class OperatorField
1944  : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
1945 };
1946 
1947 
1948 class BinaryOperation final : public Expression {
1949  public:
1950  Token::Value op() const { return OperatorField::decode(bit_field_); }
1951  Expression* left() const { return left_; }
1952  Expression* right() const { return right_; }
1953 
1954  // Returns true if one side is a Smi literal, returning the other side's
1955  // sub-expression in |subexpr| and the literal Smi in |literal|.
1956  bool IsSmiLiteralOperation(Expression** subexpr, Smi* literal);
1957 
1958  private:
1959  friend class AstNodeFactory;
1960 
1961  BinaryOperation(Token::Value op, Expression* left, Expression* right, int pos)
1962  : Expression(pos, kBinaryOperation), left_(left), right_(right) {
1963  bit_field_ |= OperatorField::encode(op);
1964  DCHECK(Token::IsBinaryOp(op));
1965  }
1966 
1967  Expression* left_;
1968  Expression* right_;
1969 
1970  class OperatorField
1971  : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
1972 };
1973 
1974 class NaryOperation final : public Expression {
1975  public:
1976  Token::Value op() const { return OperatorField::decode(bit_field_); }
1977  Expression* first() const { return first_; }
1978  Expression* subsequent(size_t index) const {
1979  return subsequent_[index].expression;
1980  }
1981 
1982  size_t subsequent_length() const { return subsequent_.size(); }
1983  int subsequent_op_position(size_t index) const {
1984  return subsequent_[index].op_position;
1985  }
1986 
1987  void AddSubsequent(Expression* expr, int pos) {
1988  subsequent_.emplace_back(expr, pos);
1989  }
1990 
1991  private:
1992  friend class AstNodeFactory;
1993 
1994  NaryOperation(Zone* zone, Token::Value op, Expression* first,
1995  size_t initial_subsequent_size)
1996  : Expression(first->position(), kNaryOperation),
1997  first_(first),
1998  subsequent_(zone) {
1999  bit_field_ |= OperatorField::encode(op);
2000  DCHECK(Token::IsBinaryOp(op));
2001  DCHECK_NE(op, Token::EXP);
2002  subsequent_.reserve(initial_subsequent_size);
2003  }
2004 
2005  // Nary operations store the first (lhs) child expression inline, and the
2006  // child expressions (rhs of each op) are stored out-of-line, along with
2007  // their operation's position. Note that the Nary operation expression's
2008  // position has no meaning.
2009  //
2010  // So an nary add:
2011  //
2012  // expr + expr + expr + ...
2013  //
2014  // is stored as:
2015  //
2016  // (expr) [(+ expr), (+ expr), ...]
2017  // '-.--' '-----------.-----------'
2018  // first subsequent entry list
2019 
2020  Expression* first_;
2021 
2022  struct NaryOperationEntry {
2023  Expression* expression;
2024  int op_position;
2025  NaryOperationEntry(Expression* e, int pos)
2026  : expression(e), op_position(pos) {}
2027  };
2028  ZoneVector<NaryOperationEntry> subsequent_;
2029 
2030  class OperatorField
2031  : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
2032 };
2033 
2034 class CountOperation final : public Expression {
2035  public:
2036  bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2037  bool is_postfix() const { return !is_prefix(); }
2038 
2039  Token::Value op() const { return TokenField::decode(bit_field_); }
2040 
2041  Expression* expression() const { return expression_; }
2042 
2043  private:
2044  friend class AstNodeFactory;
2045 
2046  CountOperation(Token::Value op, bool is_prefix, Expression* expr, int pos)
2047  : Expression(pos, kCountOperation), expression_(expr) {
2048  bit_field_ |= IsPrefixField::encode(is_prefix) | TokenField::encode(op);
2049  }
2050 
2051  class IsPrefixField
2052  : public BitField<bool, Expression::kNextBitFieldIndex, 1> {};
2053  class TokenField : public BitField<Token::Value, IsPrefixField::kNext, 7> {};
2054 
2055  Expression* expression_;
2056 };
2057 
2058 
2059 class CompareOperation final : public Expression {
2060  public:
2061  Token::Value op() const { return OperatorField::decode(bit_field_); }
2062  Expression* left() const { return left_; }
2063  Expression* right() const { return right_; }
2064 
2065  // Match special cases.
2066  bool IsLiteralCompareTypeof(Expression** expr, Literal** literal);
2067  bool IsLiteralCompareUndefined(Expression** expr);
2068  bool IsLiteralCompareNull(Expression** expr);
2069 
2070  private:
2071  friend class AstNodeFactory;
2072 
2073  CompareOperation(Token::Value op, Expression* left, Expression* right,
2074  int pos)
2075  : Expression(pos, kCompareOperation), left_(left), right_(right) {
2076  bit_field_ |= OperatorField::encode(op);
2077  DCHECK(Token::IsCompareOp(op));
2078  }
2079 
2080  Expression* left_;
2081  Expression* right_;
2082 
2083  class OperatorField
2084  : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
2085 };
2086 
2087 
2088 class Spread final : public Expression {
2089  public:
2090  Expression* expression() const { return expression_; }
2091 
2092  int expression_position() const { return expr_pos_; }
2093 
2094  private:
2095  friend class AstNodeFactory;
2096 
2097  Spread(Expression* expression, int pos, int expr_pos)
2098  : Expression(pos, kSpread),
2099  expr_pos_(expr_pos),
2100  expression_(expression) {}
2101 
2102  int expr_pos_;
2103  Expression* expression_;
2104 };
2105 
2106 // The StoreInArrayLiteral node corresponds to the StaInArrayLiteral bytecode.
2107 // It is used in the rewriting of destructuring assignments that contain an
2108 // array rest pattern.
2109 class StoreInArrayLiteral final : public Expression {
2110  public:
2111  Expression* array() const { return array_; }
2112  Expression* index() const { return index_; }
2113  Expression* value() const { return value_; }
2114 
2115  private:
2116  friend class AstNodeFactory;
2117 
2118  StoreInArrayLiteral(Expression* array, Expression* index, Expression* value,
2119  int position)
2120  : Expression(position, kStoreInArrayLiteral),
2121  array_(array),
2122  index_(index),
2123  value_(value) {}
2124 
2125  Expression* array_;
2126  Expression* index_;
2127  Expression* value_;
2128 };
2129 
2130 class Conditional final : public Expression {
2131  public:
2132  Expression* condition() const { return condition_; }
2133  Expression* then_expression() const { return then_expression_; }
2134  Expression* else_expression() const { return else_expression_; }
2135 
2136  private:
2137  friend class AstNodeFactory;
2138 
2139  Conditional(Expression* condition, Expression* then_expression,
2140  Expression* else_expression, int position)
2141  : Expression(position, kConditional),
2142  condition_(condition),
2143  then_expression_(then_expression),
2144  else_expression_(else_expression) {}
2145 
2146  Expression* condition_;
2147  Expression* then_expression_;
2148  Expression* else_expression_;
2149 };
2150 
2151 class Assignment : public Expression {
2152  public:
2153  Token::Value op() const { return TokenField::decode(bit_field_); }
2154  Expression* target() const { return target_; }
2155  Expression* value() const { return value_; }
2156 
2157  // The assignment was generated as part of block-scoped sloppy-mode
2158  // function hoisting, see
2159  // ES#sec-block-level-function-declarations-web-legacy-compatibility-semantics
2160  LookupHoistingMode lookup_hoisting_mode() const {
2161  return static_cast<LookupHoistingMode>(
2162  LookupHoistingModeField::decode(bit_field_));
2163  }
2164  void set_lookup_hoisting_mode(LookupHoistingMode mode) {
2165  bit_field_ =
2166  LookupHoistingModeField::update(bit_field_, static_cast<bool>(mode));
2167  }
2168 
2169  protected:
2170  Assignment(NodeType type, Token::Value op, Expression* target,
2171  Expression* value, int pos);
2172 
2173  private:
2174  friend class AstNodeFactory;
2175 
2176  class TokenField
2177  : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
2178  class LookupHoistingModeField : public BitField<bool, TokenField::kNext, 1> {
2179  };
2180 
2181  Expression* target_;
2182  Expression* value_;
2183 };
2184 
2185 class CompoundAssignment final : public Assignment {
2186  public:
2187  BinaryOperation* binary_operation() const { return binary_operation_; }
2188 
2189  private:
2190  friend class AstNodeFactory;
2191 
2192  CompoundAssignment(Token::Value op, Expression* target, Expression* value,
2193  int pos, BinaryOperation* binary_operation)
2194  : Assignment(kCompoundAssignment, op, target, value, pos),
2195  binary_operation_(binary_operation) {}
2196 
2197  BinaryOperation* binary_operation_;
2198 };
2199 
2200 // The RewritableExpression class is a wrapper for AST nodes that wait
2201 // for some potential rewriting. However, even if such nodes are indeed
2202 // rewritten, the RewritableExpression wrapper nodes will survive in the
2203 // final AST and should be just ignored, i.e., they should be treated as
2204 // equivalent to the wrapped nodes. For this reason and to simplify later
2205 // phases, RewritableExpressions are considered as exceptions of AST nodes
2206 // in the following sense:
2207 //
2208 // 1. IsRewritableExpression and AsRewritableExpression behave as usual.
2209 // 2. All other Is* and As* methods are practically delegated to the
2210 // wrapped node, i.e. IsArrayLiteral() will return true iff the
2211 // wrapped node is an array literal.
2212 //
2213 // Furthermore, an invariant that should be respected is that the wrapped
2214 // node is not a RewritableExpression.
2215 class RewritableExpression final : public Expression {
2216  public:
2217  Expression* expression() const { return expr_; }
2218  bool is_rewritten() const { return IsRewrittenField::decode(bit_field_); }
2219  void set_rewritten() {
2220  bit_field_ = IsRewrittenField::update(bit_field_, true);
2221  }
2222 
2223  void Rewrite(Expression* new_expression) {
2224  DCHECK(!is_rewritten());
2225  DCHECK_NOT_NULL(new_expression);
2226  DCHECK(!new_expression->IsRewritableExpression());
2227  expr_ = new_expression;
2228  set_rewritten();
2229  }
2230 
2231  Scope* scope() const { return scope_; }
2232  void set_scope(Scope* scope) { scope_ = scope; }
2233 
2234  private:
2235  friend class AstNodeFactory;
2236 
2237  RewritableExpression(Expression* expression, Scope* scope)
2238  : Expression(expression->position(), kRewritableExpression),
2239  expr_(expression),
2240  scope_(scope) {
2241  bit_field_ |= IsRewrittenField::encode(false);
2242  DCHECK(!expression->IsRewritableExpression());
2243  }
2244 
2245  Expression* expr_;
2246  Scope* scope_;
2247 
2248  class IsRewrittenField
2249  : public BitField<bool, Expression::kNextBitFieldIndex, 1> {};
2250 };
2251 
2252 // There are several types of Suspend node:
2253 //
2254 // Yield
2255 // YieldStar
2256 // Await
2257 //
2258 // Our Yield is different from the JS yield in that it "returns" its argument as
2259 // is, without wrapping it in an iterator result object. Such wrapping, if
2260 // desired, must be done beforehand (see the parser).
2261 class Suspend : public Expression {
2262  public:
2263  // With {kNoControl}, the {Suspend} behaves like yield, except that it never
2264  // throws and never causes the current generator to return. This is used to
2265  // desugar yield*.
2266  // TODO(caitp): remove once yield* desugaring for async generators is handled
2267  // in BytecodeGenerator.
2268  enum OnAbruptResume { kOnExceptionThrow, kNoControl };
2269 
2270  Expression* expression() const { return expression_; }
2271  OnAbruptResume on_abrupt_resume() const {
2272  return OnAbruptResumeField::decode(bit_field_);
2273  }
2274 
2275  private:
2276  friend class AstNodeFactory;
2277  friend class Yield;
2278  friend class YieldStar;
2279  friend class Await;
2280 
2281  Suspend(NodeType node_type, Expression* expression, int pos,
2282  OnAbruptResume on_abrupt_resume)
2283  : Expression(pos, node_type), expression_(expression) {
2284  bit_field_ |= OnAbruptResumeField::encode(on_abrupt_resume);
2285  }
2286 
2287  Expression* expression_;
2288 
2289  class OnAbruptResumeField
2290  : public BitField<OnAbruptResume, Expression::kNextBitFieldIndex, 1> {};
2291 };
2292 
2293 class Yield final : public Suspend {
2294  private:
2295  friend class AstNodeFactory;
2296  Yield(Expression* expression, int pos, OnAbruptResume on_abrupt_resume)
2297  : Suspend(kYield, expression, pos, on_abrupt_resume) {}
2298 };
2299 
2300 class YieldStar final : public Suspend {
2301  private:
2302  friend class AstNodeFactory;
2303  YieldStar(Expression* expression, int pos)
2304  : Suspend(kYieldStar, expression, pos,
2305  Suspend::OnAbruptResume::kNoControl) {}
2306 };
2307 
2308 class Await final : public Suspend {
2309  private:
2310  friend class AstNodeFactory;
2311 
2312  Await(Expression* expression, int pos)
2313  : Suspend(kAwait, expression, pos, Suspend::kOnExceptionThrow) {}
2314 };
2315 
2316 class Throw final : public Expression {
2317  public:
2318  Expression* exception() const { return exception_; }
2319 
2320  private:
2321  friend class AstNodeFactory;
2322 
2323  Throw(Expression* exception, int pos)
2324  : Expression(pos, kThrow), exception_(exception) {}
2325 
2326  Expression* exception_;
2327 };
2328 
2329 
2330 class FunctionLiteral final : public Expression {
2331  public:
2332  enum FunctionType {
2333  kAnonymousExpression,
2334  kNamedExpression,
2335  kDeclaration,
2336  kAccessorOrMethod,
2337  kWrapped,
2338  };
2339 
2340  enum IdType { kIdTypeInvalid = -1, kIdTypeTopLevel = 0 };
2341 
2342  enum ParameterFlag : uint8_t {
2343  kNoDuplicateParameters,
2344  kHasDuplicateParameters
2345  };
2346  enum EagerCompileHint : uint8_t { kShouldEagerCompile, kShouldLazyCompile };
2347 
2348  // Empty handle means that the function does not have a shared name (i.e.
2349  // the name will be set dynamically after creation of the function closure).
2350  MaybeHandle<String> name() const {
2351  return raw_name_ ? raw_name_->string() : MaybeHandle<String>();
2352  }
2353  Handle<String> name(Isolate* isolate) const;
2354  bool has_shared_name() const { return raw_name_ != nullptr; }
2355  const AstConsString* raw_name() const { return raw_name_; }
2356  void set_raw_name(const AstConsString* name) { raw_name_ = name; }
2357  DeclarationScope* scope() const { return scope_; }
2358  ZonePtrList<Statement>* body() { return &body_; }
2359  void set_function_token_position(int pos) { function_token_position_ = pos; }
2360  int function_token_position() const { return function_token_position_; }
2361  int start_position() const;
2362  int end_position() const;
2363  bool is_declaration() const { return function_type() == kDeclaration; }
2364  bool is_named_expression() const {
2365  return function_type() == kNamedExpression;
2366  }
2367  bool is_anonymous_expression() const {
2368  return function_type() == kAnonymousExpression;
2369  }
2370 
2371  void mark_as_iife() { bit_field_ = IIFEBit::update(bit_field_, true); }
2372  bool is_iife() const { return IIFEBit::decode(bit_field_); }
2373  bool is_toplevel() const {
2374  return function_literal_id() == FunctionLiteral::kIdTypeTopLevel;
2375  }
2376  bool is_wrapped() const { return function_type() == kWrapped; }
2377  LanguageMode language_mode() const;
2378 
2379  static bool NeedsHomeObject(Expression* expr);
2380 
2381  int expected_property_count() {
2382  // Not valid for lazy functions.
2383  DCHECK(ShouldEagerCompile());
2384  return expected_property_count_;
2385  }
2386  int parameter_count() { return parameter_count_; }
2387  int function_length() { return function_length_; }
2388 
2389  bool AllowsLazyCompilation();
2390 
2391  bool CanSuspend() {
2392  if (suspend_count() > 0) {
2393  DCHECK(IsResumableFunction(kind()));
2394  return true;
2395  }
2396  return false;
2397  }
2398 
2399  // Returns either name or inferred name as a cstring.
2400  std::unique_ptr<char[]> GetDebugName() const;
2401 
2402  Handle<String> inferred_name() const {
2403  if (!inferred_name_.is_null()) {
2404  DCHECK_NULL(raw_inferred_name_);
2405  return inferred_name_;
2406  }
2407  if (raw_inferred_name_ != nullptr) {
2408  return raw_inferred_name_->string();
2409  }
2410  UNREACHABLE();
2411  }
2412  const AstConsString* raw_inferred_name() { return raw_inferred_name_; }
2413 
2414  // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2415  void set_inferred_name(Handle<String> inferred_name);
2416  void set_raw_inferred_name(const AstConsString* raw_inferred_name);
2417 
2418  bool pretenure() const { return Pretenure::decode(bit_field_); }
2419  void set_pretenure() { bit_field_ = Pretenure::update(bit_field_, true); }
2420 
2421  bool has_duplicate_parameters() const {
2422  // Not valid for lazy functions.
2423  DCHECK(ShouldEagerCompile());
2424  return HasDuplicateParameters::decode(bit_field_);
2425  }
2426 
2427  // This is used as a heuristic on when to eagerly compile a function
2428  // literal. We consider the following constructs as hints that the
2429  // function will be called immediately:
2430  // - (function() { ... })();
2431  // - var x = function() { ... }();
2432  bool ShouldEagerCompile() const;
2433  V8_EXPORT_PRIVATE void SetShouldEagerCompile();
2434 
2435  FunctionType function_type() const {
2436  return FunctionTypeBits::decode(bit_field_);
2437  }
2438  FunctionKind kind() const;
2439 
2440  bool dont_optimize() {
2441  return dont_optimize_reason() != BailoutReason::kNoReason;
2442  }
2443  BailoutReason dont_optimize_reason() {
2444  return DontOptimizeReasonField::decode(bit_field_);
2445  }
2446  void set_dont_optimize_reason(BailoutReason reason) {
2447  bit_field_ = DontOptimizeReasonField::update(bit_field_, reason);
2448  }
2449 
2450  bool IsAnonymousFunctionDefinition() const {
2451  return is_anonymous_expression();
2452  }
2453 
2454  int suspend_count() { return suspend_count_; }
2455  void set_suspend_count(int suspend_count) { suspend_count_ = suspend_count; }
2456 
2457  int return_position() {
2458  return std::max(
2459  start_position(),
2460  end_position() - (HasBracesField::decode(bit_field_) ? 1 : 0));
2461  }
2462 
2463  int function_literal_id() const { return function_literal_id_; }
2464  void set_function_literal_id(int function_literal_id) {
2465  function_literal_id_ = function_literal_id;
2466  }
2467 
2468  void set_requires_instance_members_initializer(bool value) {
2469  bit_field_ = RequiresInstanceMembersInitializer::update(bit_field_, value);
2470  }
2471  bool requires_instance_members_initializer() const {
2472  return RequiresInstanceMembersInitializer::decode(bit_field_);
2473  }
2474 
2475  ProducedPreParsedScopeData* produced_preparsed_scope_data() const {
2476  return produced_preparsed_scope_data_;
2477  }
2478 
2479  private:
2480  friend class AstNodeFactory;
2481 
2483  Zone* zone, const AstRawString* name, AstValueFactory* ast_value_factory,
2484  DeclarationScope* scope, const ScopedPtrList<Statement>& body,
2485  int expected_property_count, int parameter_count, int function_length,
2486  FunctionType function_type, ParameterFlag has_duplicate_parameters,
2487  EagerCompileHint eager_compile_hint, int position, bool has_braces,
2488  int function_literal_id,
2489  ProducedPreParsedScopeData* produced_preparsed_scope_data = nullptr)
2490  : Expression(position, kFunctionLiteral),
2491  expected_property_count_(expected_property_count),
2492  parameter_count_(parameter_count),
2493  function_length_(function_length),
2494  function_token_position_(kNoSourcePosition),
2495  suspend_count_(0),
2496  function_literal_id_(function_literal_id),
2497  raw_name_(name ? ast_value_factory->NewConsString(name) : nullptr),
2498  scope_(scope),
2499  body_(0, nullptr),
2500  raw_inferred_name_(ast_value_factory->empty_cons_string()),
2501  produced_preparsed_scope_data_(produced_preparsed_scope_data) {
2502  bit_field_ |= FunctionTypeBits::encode(function_type) |
2503  Pretenure::encode(false) |
2504  HasDuplicateParameters::encode(has_duplicate_parameters ==
2505  kHasDuplicateParameters) |
2506  DontOptimizeReasonField::encode(BailoutReason::kNoReason) |
2507  RequiresInstanceMembersInitializer::encode(false) |
2508  HasBracesField::encode(has_braces) | IIFEBit::encode(false);
2509  if (eager_compile_hint == kShouldEagerCompile) SetShouldEagerCompile();
2510  body.CopyTo(&body_, zone);
2511  }
2512 
2513  class FunctionTypeBits
2514  : public BitField<FunctionType, Expression::kNextBitFieldIndex, 3> {};
2515  class Pretenure : public BitField<bool, FunctionTypeBits::kNext, 1> {};
2516  class HasDuplicateParameters : public BitField<bool, Pretenure::kNext, 1> {};
2517  class DontOptimizeReasonField
2518  : public BitField<BailoutReason, HasDuplicateParameters::kNext, 8> {};
2519  class RequiresInstanceMembersInitializer
2520  : public BitField<bool, DontOptimizeReasonField::kNext, 1> {};
2521  class HasBracesField
2522  : public BitField<bool, RequiresInstanceMembersInitializer::kNext, 1> {};
2523  class IIFEBit : public BitField<bool, HasBracesField::kNext, 1> {};
2524 
2525  int expected_property_count_;
2526  int parameter_count_;
2527  int function_length_;
2528  int function_token_position_;
2529  int suspend_count_;
2530  int function_literal_id_;
2531 
2532  const AstConsString* raw_name_;
2533  DeclarationScope* scope_;
2534  ZonePtrList<Statement> body_;
2535  const AstConsString* raw_inferred_name_;
2536  Handle<String> inferred_name_;
2537  ProducedPreParsedScopeData* produced_preparsed_scope_data_;
2538 };
2539 
2540 // Property is used for passing information
2541 // about a class literal's properties from the parser to the code generator.
2543  public:
2544  enum Kind : uint8_t { METHOD, GETTER, SETTER, FIELD };
2545 
2546  Kind kind() const { return kind_; }
2547 
2548  bool is_static() const { return is_static_; }
2549 
2550  bool is_private() const { return is_private_; }
2551 
2552  void set_computed_name_var(Variable* var) {
2553  DCHECK_EQ(FIELD, kind());
2554  DCHECK(!is_private());
2555  private_or_computed_name_var_ = var;
2556  }
2557 
2558  Variable* computed_name_var() const {
2559  DCHECK_EQ(FIELD, kind());
2560  DCHECK(!is_private());
2561  return private_or_computed_name_var_;
2562  }
2563 
2564  void set_private_name_var(Variable* var) {
2565  DCHECK_EQ(FIELD, kind());
2566  DCHECK(is_private());
2567  private_or_computed_name_var_ = var;
2568  }
2569  Variable* private_name_var() const {
2570  DCHECK_EQ(FIELD, kind());
2571  DCHECK(is_private());
2572  return private_or_computed_name_var_;
2573  }
2574 
2575  private:
2576  friend class AstNodeFactory;
2577 
2578  ClassLiteralProperty(Expression* key, Expression* value, Kind kind,
2579  bool is_static, bool is_computed_name, bool is_private);
2580 
2581  Kind kind_;
2582  bool is_static_;
2583  bool is_private_;
2584  Variable* private_or_computed_name_var_;
2585 };
2586 
2588  public:
2590 
2591  ZonePtrList<Property>* fields() const { return fields_; }
2592 
2593  private:
2594  friend class AstNodeFactory;
2595 
2597  : Statement(pos, kInitializeClassMembersStatement), fields_(fields) {}
2598 
2599  ZonePtrList<Property>* fields_;
2600 };
2601 
2602 class ClassLiteral final : public Expression {
2603  public:
2605 
2606  Scope* scope() const { return scope_; }
2607  Variable* class_variable() const { return class_variable_; }
2608  Expression* extends() const { return extends_; }
2609  FunctionLiteral* constructor() const { return constructor_; }
2610  ZonePtrList<Property>* properties() const { return properties_; }
2611  int start_position() const { return position(); }
2612  int end_position() const { return end_position_; }
2613  bool has_name_static_property() const {
2614  return HasNameStaticProperty::decode(bit_field_);
2615  }
2616  bool has_static_computed_names() const {
2617  return HasStaticComputedNames::decode(bit_field_);
2618  }
2619 
2620  bool is_anonymous_expression() const {
2621  return IsAnonymousExpression::decode(bit_field_);
2622  }
2623  bool IsAnonymousFunctionDefinition() const {
2624  return is_anonymous_expression();
2625  }
2626 
2627  FunctionLiteral* static_fields_initializer() const {
2628  return static_fields_initializer_;
2629  }
2630 
2631  FunctionLiteral* instance_members_initializer_function() const {
2632  return instance_members_initializer_function_;
2633  }
2634 
2635  private:
2636  friend class AstNodeFactory;
2637 
2638  ClassLiteral(Scope* scope, Variable* class_variable, Expression* extends,
2639  FunctionLiteral* constructor, ZonePtrList<Property>* properties,
2640  FunctionLiteral* static_fields_initializer,
2641  FunctionLiteral* instance_members_initializer_function,
2642  int start_position, int end_position,
2643  bool has_name_static_property, bool has_static_computed_names,
2644  bool is_anonymous)
2645  : Expression(start_position, kClassLiteral),
2646  end_position_(end_position),
2647  scope_(scope),
2648  class_variable_(class_variable),
2649  extends_(extends),
2650  constructor_(constructor),
2651  properties_(properties),
2652  static_fields_initializer_(static_fields_initializer),
2653  instance_members_initializer_function_(
2654  instance_members_initializer_function) {
2655  bit_field_ |= HasNameStaticProperty::encode(has_name_static_property) |
2656  HasStaticComputedNames::encode(has_static_computed_names) |
2657  IsAnonymousExpression::encode(is_anonymous);
2658  }
2659 
2660  int end_position_;
2661  Scope* scope_;
2662  Variable* class_variable_;
2663  Expression* extends_;
2664  FunctionLiteral* constructor_;
2665  ZonePtrList<Property>* properties_;
2666  FunctionLiteral* static_fields_initializer_;
2667  FunctionLiteral* instance_members_initializer_function_;
2668  class HasNameStaticProperty
2669  : public BitField<bool, Expression::kNextBitFieldIndex, 1> {};
2670  class HasStaticComputedNames
2671  : public BitField<bool, HasNameStaticProperty::kNext, 1> {};
2672  class IsAnonymousExpression
2673  : public BitField<bool, HasStaticComputedNames::kNext, 1> {};
2674 };
2675 
2676 
2677 class NativeFunctionLiteral final : public Expression {
2678  public:
2679  Handle<String> name() const { return name_->string(); }
2680  const AstRawString* raw_name() const { return name_; }
2681  v8::Extension* extension() const { return extension_; }
2682 
2683  private:
2684  friend class AstNodeFactory;
2685 
2686  NativeFunctionLiteral(const AstRawString* name, v8::Extension* extension,
2687  int pos)
2688  : Expression(pos, kNativeFunctionLiteral),
2689  name_(name),
2690  extension_(extension) {}
2691 
2692  const AstRawString* name_;
2693  v8::Extension* extension_;
2694 };
2695 
2696 
2697 class ThisFunction final : public Expression {
2698  private:
2699  friend class AstNodeFactory;
2700  explicit ThisFunction(int pos) : Expression(pos, kThisFunction) {}
2701 };
2702 
2703 
2704 class SuperPropertyReference final : public Expression {
2705  public:
2706  VariableProxy* this_var() const { return this_var_; }
2707  Expression* home_object() const { return home_object_; }
2708 
2709  private:
2710  friend class AstNodeFactory;
2711 
2712  SuperPropertyReference(VariableProxy* this_var, Expression* home_object,
2713  int pos)
2714  : Expression(pos, kSuperPropertyReference),
2715  this_var_(this_var),
2716  home_object_(home_object) {
2717  DCHECK(this_var->is_this());
2718  DCHECK(home_object->IsProperty());
2719  }
2720 
2721  VariableProxy* this_var_;
2722  Expression* home_object_;
2723 };
2724 
2725 
2726 class SuperCallReference final : public Expression {
2727  public:
2728  VariableProxy* this_var() const { return this_var_; }
2729  VariableProxy* new_target_var() const { return new_target_var_; }
2730  VariableProxy* this_function_var() const { return this_function_var_; }
2731 
2732  private:
2733  friend class AstNodeFactory;
2734 
2735  SuperCallReference(VariableProxy* this_var, VariableProxy* new_target_var,
2736  VariableProxy* this_function_var, int pos)
2737  : Expression(pos, kSuperCallReference),
2738  this_var_(this_var),
2739  new_target_var_(new_target_var),
2740  this_function_var_(this_function_var) {
2741  DCHECK(this_var->is_this());
2742  DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2743  DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2744  }
2745 
2746  VariableProxy* this_var_;
2747  VariableProxy* new_target_var_;
2748  VariableProxy* this_function_var_;
2749 };
2750 
2751 // This AST Node is used to represent a dynamic import call --
2752 // import(argument).
2753 class ImportCallExpression final : public Expression {
2754  public:
2755  Expression* argument() const { return argument_; }
2756 
2757  private:
2758  friend class AstNodeFactory;
2759 
2760  ImportCallExpression(Expression* argument, int pos)
2761  : Expression(pos, kImportCallExpression), argument_(argument) {}
2762 
2763  Expression* argument_;
2764 };
2765 
2766 // This class is produced when parsing the () in arrow functions without any
2767 // arguments and is not actually a valid expression.
2768 class EmptyParentheses final : public Expression {
2769  private:
2770  friend class AstNodeFactory;
2771 
2772  explicit EmptyParentheses(int pos) : Expression(pos, kEmptyParentheses) {
2773  mark_parenthesized();
2774  }
2775 };
2776 
2777 // Represents the spec operation `GetIterator()`
2778 // (defined at https://tc39.github.io/ecma262/#sec-getiterator). Ignition
2779 // desugars this into a LoadIC / JSLoadNamed, CallIC, and a type-check to
2780 // validate return value of the Symbol.iterator() call.
2781 enum class IteratorType { kNormal, kAsync };
2782 class GetIterator final : public Expression {
2783  public:
2784  IteratorType hint() const { return hint_; }
2785 
2786  Expression* iterable() const { return iterable_; }
2787 
2788  Expression* iterable_for_call_printer() const {
2789  return destructured_iterable_ != nullptr ? destructured_iterable_
2790  : iterable_;
2791  }
2792 
2793  private:
2794  friend class AstNodeFactory;
2795 
2796  GetIterator(Expression* iterable, Expression* destructured_iterable,
2797  IteratorType hint, int pos)
2798  : Expression(pos, kGetIterator),
2799  hint_(hint),
2800  iterable_(iterable),
2801  destructured_iterable_(destructured_iterable) {}
2802 
2803  GetIterator(Expression* iterable, IteratorType hint, int pos)
2804  : Expression(pos, kGetIterator),
2805  hint_(hint),
2806  iterable_(iterable),
2807  destructured_iterable_(nullptr) {}
2808 
2809  IteratorType hint_;
2810  Expression* iterable_;
2811 
2812  // iterable_ is the variable proxy, while destructured_iterable_ points to
2813  // the raw value stored in the variable proxy. This is only used for
2814  // pretty printing error messages.
2815  Expression* destructured_iterable_;
2816 };
2817 
2818 // Represents the spec operation `GetTemplateObject(templateLiteral)`
2819 // (defined at https://tc39.github.io/ecma262/#sec-gettemplateobject).
2820 class GetTemplateObject final : public Expression {
2821  public:
2822  const ZonePtrList<const AstRawString>* cooked_strings() const {
2823  return cooked_strings_;
2824  }
2825  const ZonePtrList<const AstRawString>* raw_strings() const {
2826  return raw_strings_;
2827  }
2828 
2829  Handle<TemplateObjectDescription> GetOrBuildDescription(Isolate* isolate);
2830 
2831  private:
2832  friend class AstNodeFactory;
2833 
2834  GetTemplateObject(const ZonePtrList<const AstRawString>* cooked_strings,
2835  const ZonePtrList<const AstRawString>* raw_strings, int pos)
2836  : Expression(pos, kGetTemplateObject),
2837  cooked_strings_(cooked_strings),
2838  raw_strings_(raw_strings) {}
2839 
2840  const ZonePtrList<const AstRawString>* cooked_strings_;
2841  const ZonePtrList<const AstRawString>* raw_strings_;
2842 };
2843 
2844 class TemplateLiteral final : public Expression {
2845  public:
2846  const ZonePtrList<const AstRawString>* string_parts() const {
2847  return string_parts_;
2848  }
2849  const ZonePtrList<Expression>* substitutions() const {
2850  return substitutions_;
2851  }
2852 
2853  private:
2854  friend class AstNodeFactory;
2856  const ZonePtrList<Expression>* substitutions, int pos)
2857  : Expression(pos, kTemplateLiteral),
2858  string_parts_(parts),
2859  substitutions_(substitutions) {}
2860 
2861  const ZonePtrList<const AstRawString>* string_parts_;
2862  const ZonePtrList<Expression>* substitutions_;
2863 };
2864 
2865 // ----------------------------------------------------------------------------
2866 // Basic visitor
2867 // Sub-class should parametrize AstVisitor with itself, e.g.:
2868 // class SpecificVisitor : public AstVisitor<SpecificVisitor> { ... }
2869 
2870 template <class Subclass>
2871 class AstVisitor {
2872  public:
2873  void Visit(AstNode* node) { impl()->Visit(node); }
2874 
2875  void VisitDeclarations(Declaration::List* declarations) {
2876  for (Declaration* decl : *declarations) Visit(decl);
2877  }
2878 
2879  void VisitStatements(const ZonePtrList<Statement>* statements) {
2880  for (int i = 0; i < statements->length(); i++) {
2881  Statement* stmt = statements->at(i);
2882  Visit(stmt);
2883  if (stmt->IsJump()) break;
2884  }
2885  }
2886 
2887  void VisitExpressions(const ZonePtrList<Expression>* expressions) {
2888  for (int i = 0; i < expressions->length(); i++) {
2889  // The variable statement visiting code may pass null expressions
2890  // to this code. Maybe this should be handled by introducing an
2891  // undefined expression or literal? Revisit this code if this
2892  // changes.
2893  Expression* expression = expressions->at(i);
2894  if (expression != nullptr) Visit(expression);
2895  }
2896  }
2897 
2898  protected:
2899  Subclass* impl() { return static_cast<Subclass*>(this); }
2900 };
2901 
2902 #define GENERATE_VISIT_CASE(NodeType) \
2903  case AstNode::k##NodeType: \
2904  return this->impl()->Visit##NodeType(static_cast<NodeType*>(node));
2905 
2906 #define GENERATE_FAILURE_CASE(NodeType) \
2907  case AstNode::k##NodeType: \
2908  UNREACHABLE();
2909 
2910 #define GENERATE_AST_VISITOR_SWITCH() \
2911  switch (node->node_type()) { \
2912  AST_NODE_LIST(GENERATE_VISIT_CASE) \
2913  FAILURE_NODE_LIST(GENERATE_FAILURE_CASE) \
2914  }
2915 
2916 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
2917  public: \
2918  void VisitNoStackOverflowCheck(AstNode* node) { \
2919  GENERATE_AST_VISITOR_SWITCH() \
2920  } \
2921  \
2922  void Visit(AstNode* node) { \
2923  if (CheckStackOverflow()) return; \
2924  VisitNoStackOverflowCheck(node); \
2925  } \
2926  \
2927  void SetStackOverflow() { stack_overflow_ = true; } \
2928  void ClearStackOverflow() { stack_overflow_ = false; } \
2929  bool HasStackOverflow() const { return stack_overflow_; } \
2930  \
2931  bool CheckStackOverflow() { \
2932  if (stack_overflow_) return true; \
2933  if (GetCurrentStackPosition() < stack_limit_) { \
2934  stack_overflow_ = true; \
2935  return true; \
2936  } \
2937  return false; \
2938  } \
2939  \
2940  private: \
2941  void InitializeAstVisitor(Isolate* isolate) { \
2942  stack_limit_ = isolate->stack_guard()->real_climit(); \
2943  stack_overflow_ = false; \
2944  } \
2945  \
2946  void InitializeAstVisitor(uintptr_t stack_limit) { \
2947  stack_limit_ = stack_limit; \
2948  stack_overflow_ = false; \
2949  } \
2950  \
2951  uintptr_t stack_limit_; \
2952  bool stack_overflow_
2953 
2954 #define DEFINE_AST_VISITOR_MEMBERS_WITHOUT_STACKOVERFLOW() \
2955  public: \
2956  void Visit(AstNode* node) { GENERATE_AST_VISITOR_SWITCH() } \
2957  \
2958  private:
2959 
2960 // ----------------------------------------------------------------------------
2961 // AstNode factory
2962 
2963 class AstNodeFactory final {
2964  public:
2965  AstNodeFactory(AstValueFactory* ast_value_factory, Zone* zone)
2966  : zone_(zone),
2967  ast_value_factory_(ast_value_factory),
2968  empty_statement_(new (zone) class EmptyStatement()),
2969  failure_expression_(new (zone) class FailureExpression()) {}
2970 
2971  AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
2972 
2973  VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy, int pos) {
2974  return new (zone_) VariableDeclaration(proxy, pos);
2975  }
2976 
2977  NestedVariableDeclaration* NewNestedVariableDeclaration(VariableProxy* proxy,
2978  Scope* scope,
2979  int pos) {
2980  return new (zone_) NestedVariableDeclaration(proxy, scope, pos);
2981  }
2982 
2983  FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
2984  FunctionLiteral* fun,
2985  bool is_sloppy_block_function,
2986  int pos) {
2987  return new (zone_)
2988  FunctionDeclaration(proxy, fun, is_sloppy_block_function, pos);
2989  }
2990 
2991  Block* NewBlock(int capacity, bool ignore_completion_value) {
2992  return new (zone_) Block(zone_, nullptr, capacity, ignore_completion_value);
2993  }
2994 
2995  Block* NewBlock(bool ignore_completion_value,
2997  return labels != nullptr
2998  ? new (zone_) LabeledBlock(labels, ignore_completion_value)
2999  : new (zone_) Block(labels, ignore_completion_value);
3000  }
3001 
3002  Block* NewBlock(bool ignore_completion_value,
3003  const ScopedPtrList<Statement>& statements) {
3004  Block* result = NewBlock(ignore_completion_value, nullptr);
3005  result->InitializeStatements(statements, zone_);
3006  return result;
3007  }
3008 
3009 #define STATEMENT_WITH_LABELS(NodeType) \
3010  NodeType* New##NodeType(ZonePtrList<const AstRawString>* labels, \
3011  ZonePtrList<const AstRawString>* own_labels, \
3012  int pos) { \
3013  return new (zone_) NodeType(labels, own_labels, pos); \
3014  }
3015  STATEMENT_WITH_LABELS(DoWhileStatement)
3016  STATEMENT_WITH_LABELS(WhileStatement)
3017  STATEMENT_WITH_LABELS(ForStatement)
3018 #undef STATEMENT_WITH_LABELS
3019 
3020  SwitchStatement* NewSwitchStatement(ZonePtrList<const AstRawString>* labels,
3021  Expression* tag, int pos) {
3022  return new (zone_) SwitchStatement(zone_, labels, tag, pos);
3023  }
3024 
3025  ForEachStatement* NewForEachStatement(
3026  ForEachStatement::VisitMode visit_mode,
3028  ZonePtrList<const AstRawString>* own_labels, int pos) {
3029  switch (visit_mode) {
3030  case ForEachStatement::ENUMERATE: {
3031  return new (zone_) ForInStatement(labels, own_labels, pos);
3032  }
3033  case ForEachStatement::ITERATE: {
3034  return new (zone_) ForOfStatement(labels, own_labels, pos);
3035  }
3036  }
3037  UNREACHABLE();
3038  }
3039 
3040  ForOfStatement* NewForOfStatement(ZonePtrList<const AstRawString>* labels,
3041  ZonePtrList<const AstRawString>* own_labels,
3042  int pos) {
3043  return new (zone_) ForOfStatement(labels, own_labels, pos);
3044  }
3045 
3046  ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3047  return new (zone_) ExpressionStatement(expression, pos);
3048  }
3049 
3050  ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3051  return new (zone_) ContinueStatement(target, pos);
3052  }
3053 
3054  BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3055  return new (zone_) BreakStatement(target, pos);
3056  }
3057 
3058  ReturnStatement* NewReturnStatement(Expression* expression, int pos,
3059  int end_position = kNoSourcePosition) {
3060  return new (zone_) ReturnStatement(expression, ReturnStatement::kNormal,
3061  pos, end_position);
3062  }
3063 
3064  ReturnStatement* NewAsyncReturnStatement(
3065  Expression* expression, int pos, int end_position = kNoSourcePosition) {
3066  return new (zone_) ReturnStatement(
3067  expression, ReturnStatement::kAsyncReturn, pos, end_position);
3068  }
3069 
3070  WithStatement* NewWithStatement(Scope* scope,
3071  Expression* expression,
3072  Statement* statement,
3073  int pos) {
3074  return new (zone_) WithStatement(scope, expression, statement, pos);
3075  }
3076 
3077  IfStatement* NewIfStatement(Expression* condition, Statement* then_statement,
3078  Statement* else_statement, int pos) {
3079  return new (zone_)
3080  IfStatement(condition, then_statement, else_statement, pos);
3081  }
3082 
3083  TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3084  Block* catch_block, int pos) {
3085  return new (zone_) TryCatchStatement(try_block, scope, catch_block,
3086  HandlerTable::CAUGHT, pos);
3087  }
3088 
3089  TryCatchStatement* NewTryCatchStatementForReThrow(Block* try_block,
3090  Scope* scope,
3091  Block* catch_block,
3092  int pos) {
3093  return new (zone_) TryCatchStatement(try_block, scope, catch_block,
3094  HandlerTable::UNCAUGHT, pos);
3095  }
3096 
3097  TryCatchStatement* NewTryCatchStatementForDesugaring(Block* try_block,
3098  Scope* scope,
3099  Block* catch_block,
3100  int pos) {
3101  return new (zone_) TryCatchStatement(try_block, scope, catch_block,
3102  HandlerTable::DESUGARING, pos);
3103  }
3104 
3105  TryCatchStatement* NewTryCatchStatementForAsyncAwait(Block* try_block,
3106  Scope* scope,
3107  Block* catch_block,
3108  int pos) {
3109  return new (zone_) TryCatchStatement(try_block, scope, catch_block,
3110  HandlerTable::ASYNC_AWAIT, pos);
3111  }
3112 
3113  TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3114  Block* finally_block, int pos) {
3115  return new (zone_) TryFinallyStatement(try_block, finally_block, pos);
3116  }
3117 
3118  DebuggerStatement* NewDebuggerStatement(int pos) {
3119  return new (zone_) DebuggerStatement(pos);
3120  }
3121 
3122  class EmptyStatement* EmptyStatement() {
3123  return empty_statement_;
3124  }
3125 
3127  return failure_expression_;
3128  }
3129 
3130  SloppyBlockFunctionStatement* NewSloppyBlockFunctionStatement() {
3131  return new (zone_) SloppyBlockFunctionStatement(EmptyStatement());
3132  }
3133 
3134  CaseClause* NewCaseClause(Expression* label,
3135  const ScopedPtrList<Statement>& statements) {
3136  return new (zone_) CaseClause(zone_, label, statements);
3137  }
3138 
3139  Literal* NewStringLiteral(const AstRawString* string, int pos) {
3140  DCHECK_NOT_NULL(string);
3141  return new (zone_) Literal(string, pos);
3142  }
3143 
3144  // A JavaScript symbol (ECMA-262 edition 6).
3145  Literal* NewSymbolLiteral(AstSymbol symbol, int pos) {
3146  return new (zone_) Literal(symbol, pos);
3147  }
3148 
3149  Literal* NewNumberLiteral(double number, int pos);
3150 
3151  Literal* NewSmiLiteral(int number, int pos) {
3152  return new (zone_) Literal(number, pos);
3153  }
3154 
3155  Literal* NewBigIntLiteral(AstBigInt bigint, int pos) {
3156  return new (zone_) Literal(bigint, pos);
3157  }
3158 
3159  Literal* NewBooleanLiteral(bool b, int pos) {
3160  return new (zone_) Literal(b, pos);
3161  }
3162 
3163  Literal* NewNullLiteral(int pos) {
3164  return new (zone_) Literal(Literal::kNull, pos);
3165  }
3166 
3167  Literal* NewUndefinedLiteral(int pos) {
3168  return new (zone_) Literal(Literal::kUndefined, pos);
3169  }
3170 
3171  Literal* NewTheHoleLiteral() {
3172  return new (zone_) Literal(Literal::kTheHole, kNoSourcePosition);
3173  }
3174 
3175  ObjectLiteral* NewObjectLiteral(
3176  const ScopedPtrList<ObjectLiteral::Property>& properties,
3177  uint32_t boilerplate_properties, int pos, bool has_rest_property) {
3178  return new (zone_) ObjectLiteral(zone_, properties, boilerplate_properties,
3179  pos, has_rest_property);
3180  }
3181 
3182  ObjectLiteral::Property* NewObjectLiteralProperty(
3183  Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3184  bool is_computed_name) {
3185  return new (zone_)
3186  ObjectLiteral::Property(key, value, kind, is_computed_name);
3187  }
3188 
3189  ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3190  Expression* value,
3191  bool is_computed_name) {
3192  return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3193  is_computed_name);
3194  }
3195 
3196  RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern, int flags,
3197  int pos) {
3198  return new (zone_) RegExpLiteral(pattern, flags, pos);
3199  }
3200 
3201  ArrayLiteral* NewArrayLiteral(const ScopedPtrList<Expression>& values,
3202  int pos) {
3203  return new (zone_) ArrayLiteral(zone_, values, -1, pos);
3204  }
3205 
3206  ArrayLiteral* NewArrayLiteral(const ScopedPtrList<Expression>& values,
3207  int first_spread_index, int pos) {
3208  return new (zone_) ArrayLiteral(zone_, values, first_spread_index, pos);
3209  }
3210 
3211  VariableProxy* NewVariableProxy(Variable* var,
3212  int start_position = kNoSourcePosition) {
3213  return new (zone_) VariableProxy(var, start_position);
3214  }
3215 
3216  VariableProxy* NewVariableProxy(const AstRawString* name,
3217  VariableKind variable_kind,
3218  int start_position = kNoSourcePosition) {
3219  DCHECK_NOT_NULL(name);
3220  return new (zone_) VariableProxy(name, variable_kind, start_position);
3221  }
3222 
3223  // Recreates the VariableProxy in this Zone.
3224  VariableProxy* CopyVariableProxy(VariableProxy* proxy) {
3225  return new (zone_) VariableProxy(proxy);
3226  }
3227 
3228  Variable* CopyVariable(Variable* variable) {
3229  return new (zone_) Variable(variable);
3230  }
3231 
3232  Property* NewProperty(Expression* obj, Expression* key, int pos) {
3233  return new (zone_) Property(obj, key, pos);
3234  }
3235 
3236  ResolvedProperty* NewResolvedProperty(VariableProxy* obj,
3237  VariableProxy* property,
3238  int pos = kNoSourcePosition) {
3239  return new (zone_) ResolvedProperty(obj, property, pos);
3240  }
3241 
3242  Call* NewCall(Expression* expression,
3243  const ScopedPtrList<Expression>& arguments, int pos,
3244  Call::PossiblyEval possibly_eval = Call::NOT_EVAL) {
3245  return new (zone_) Call(zone_, expression, arguments, pos, possibly_eval);
3246  }
3247 
3248  Call* NewTaggedTemplate(Expression* expression,
3249  const ScopedPtrList<Expression>& arguments, int pos) {
3250  return new (zone_)
3251  Call(zone_, expression, arguments, pos, Call::TaggedTemplateTag::kTrue);
3252  }
3253 
3254  CallNew* NewCallNew(Expression* expression,
3255  const ScopedPtrList<Expression>& arguments, int pos) {
3256  return new (zone_) CallNew(zone_, expression, arguments, pos);
3257  }
3258 
3259  CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3260  const ScopedPtrList<Expression>& arguments,
3261  int pos) {
3262  return new (zone_)
3263  CallRuntime(zone_, Runtime::FunctionForId(id), arguments, pos);
3264  }
3265 
3266  CallRuntime* NewCallRuntime(const Runtime::Function* function,
3267  const ScopedPtrList<Expression>& arguments,
3268  int pos) {
3269  return new (zone_) CallRuntime(zone_, function, arguments, pos);
3270  }
3271 
3272  CallRuntime* NewCallRuntime(int context_index,
3273  const ScopedPtrList<Expression>& arguments,
3274  int pos) {
3275  return new (zone_) CallRuntime(zone_, context_index, arguments, pos);
3276  }
3277 
3278  UnaryOperation* NewUnaryOperation(Token::Value op,
3279  Expression* expression,
3280  int pos) {
3281  return new (zone_) UnaryOperation(op, expression, pos);
3282  }
3283 
3284  BinaryOperation* NewBinaryOperation(Token::Value op,
3285  Expression* left,
3286  Expression* right,
3287  int pos) {
3288  return new (zone_) BinaryOperation(op, left, right, pos);
3289  }
3290 
3291  NaryOperation* NewNaryOperation(Token::Value op, Expression* first,
3292  size_t initial_subsequent_size) {
3293  return new (zone_) NaryOperation(zone_, op, first, initial_subsequent_size);
3294  }
3295 
3296  CountOperation* NewCountOperation(Token::Value op,
3297  bool is_prefix,
3298  Expression* expr,
3299  int pos) {
3300  return new (zone_) CountOperation(op, is_prefix, expr, pos);
3301  }
3302 
3303  CompareOperation* NewCompareOperation(Token::Value op,
3304  Expression* left,
3305  Expression* right,
3306  int pos) {
3307  return new (zone_) CompareOperation(op, left, right, pos);
3308  }
3309 
3310  Spread* NewSpread(Expression* expression, int pos, int expr_pos) {
3311  return new (zone_) Spread(expression, pos, expr_pos);
3312  }
3313 
3314  StoreInArrayLiteral* NewStoreInArrayLiteral(Expression* array,
3315  Expression* index,
3316  Expression* value, int pos) {
3317  return new (zone_) StoreInArrayLiteral(array, index, value, pos);
3318  }
3319 
3320  Conditional* NewConditional(Expression* condition,
3321  Expression* then_expression,
3322  Expression* else_expression,
3323  int position) {
3324  return new (zone_)
3325  Conditional(condition, then_expression, else_expression, position);
3326  }
3327 
3328  RewritableExpression* NewRewritableExpression(Expression* expression,
3329  Scope* scope) {
3330  DCHECK_NOT_NULL(expression);
3331  return new (zone_) RewritableExpression(expression, scope);
3332  }
3333 
3334  Assignment* NewAssignment(Token::Value op,
3335  Expression* target,
3336  Expression* value,
3337  int pos) {
3338  DCHECK(Token::IsAssignmentOp(op));
3339 
3340  if (op != Token::INIT && target->IsVariableProxy()) {
3341  target->AsVariableProxy()->set_is_assigned();
3342  }
3343 
3344  if (op == Token::ASSIGN || op == Token::INIT) {
3345  return new (zone_)
3346  Assignment(AstNode::kAssignment, op, target, value, pos);
3347  } else {
3348  return new (zone_) CompoundAssignment(
3349  op, target, value, pos,
3350  NewBinaryOperation(Token::BinaryOpForAssignment(op), target, value,
3351  pos + 1));
3352  }
3353  }
3354 
3355  Suspend* NewYield(Expression* expression, int pos,
3356  Suspend::OnAbruptResume on_abrupt_resume) {
3357  if (!expression) expression = NewUndefinedLiteral(pos);
3358  return new (zone_) Yield(expression, pos, on_abrupt_resume);
3359  }
3360 
3361  YieldStar* NewYieldStar(Expression* expression, int pos) {
3362  return new (zone_) YieldStar(expression, pos);
3363  }
3364 
3365  Await* NewAwait(Expression* expression, int pos) {
3366  if (!expression) expression = NewUndefinedLiteral(pos);
3367  return new (zone_) Await(expression, pos);
3368  }
3369 
3370  Throw* NewThrow(Expression* exception, int pos) {
3371  return new (zone_) Throw(exception, pos);
3372  }
3373 
3374  FunctionLiteral* NewFunctionLiteral(
3375  const AstRawString* name, DeclarationScope* scope,
3376  const ScopedPtrList<Statement>& body, int expected_property_count,
3377  int parameter_count, int function_length,
3378  FunctionLiteral::ParameterFlag has_duplicate_parameters,
3379  FunctionLiteral::FunctionType function_type,
3380  FunctionLiteral::EagerCompileHint eager_compile_hint, int position,
3381  bool has_braces, int function_literal_id,
3382  ProducedPreParsedScopeData* produced_preparsed_scope_data = nullptr) {
3383  return new (zone_) FunctionLiteral(
3384  zone_, name, ast_value_factory_, scope, body, expected_property_count,
3385  parameter_count, function_length, function_type,
3386  has_duplicate_parameters, eager_compile_hint, position, has_braces,
3387  function_literal_id, produced_preparsed_scope_data);
3388  }
3389 
3390  // Creates a FunctionLiteral representing a top-level script, the
3391  // result of an eval (top-level or otherwise), or the result of calling
3392  // the Function constructor.
3393  FunctionLiteral* NewScriptOrEvalFunctionLiteral(
3394  DeclarationScope* scope, const ScopedPtrList<Statement>& body,
3395  int expected_property_count, int parameter_count) {
3396  return new (zone_) FunctionLiteral(
3397  zone_, ast_value_factory_->empty_string(), ast_value_factory_, scope,
3398  body, expected_property_count, parameter_count, parameter_count,
3399  FunctionLiteral::kAnonymousExpression,
3400  FunctionLiteral::kNoDuplicateParameters,
3401  FunctionLiteral::kShouldLazyCompile, 0, /* has_braces */ false,
3402  FunctionLiteral::kIdTypeTopLevel);
3403  }
3404 
3405  ClassLiteral::Property* NewClassLiteralProperty(
3406  Expression* key, Expression* value, ClassLiteralProperty::Kind kind,
3407  bool is_static, bool is_computed_name, bool is_private) {
3408  return new (zone_) ClassLiteral::Property(key, value, kind, is_static,
3409  is_computed_name, is_private);
3410  }
3411 
3412  ClassLiteral* NewClassLiteral(
3413  Scope* scope, Variable* variable, Expression* extends,
3414  FunctionLiteral* constructor,
3416  FunctionLiteral* static_fields_initializer,
3417  FunctionLiteral* instance_members_initializer_function,
3418  int start_position, int end_position, bool has_name_static_property,
3419  bool has_static_computed_names, bool is_anonymous) {
3420  return new (zone_) ClassLiteral(
3421  scope, variable, extends, constructor, properties,
3422  static_fields_initializer, instance_members_initializer_function,
3423  start_position, end_position, has_name_static_property,
3424  has_static_computed_names, is_anonymous);
3425  }
3426 
3427  NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3428  v8::Extension* extension,
3429  int pos) {
3430  return new (zone_) NativeFunctionLiteral(name, extension, pos);
3431  }
3432 
3433  DoExpression* NewDoExpression(Block* block, Variable* result_var, int pos) {
3434  VariableProxy* result = NewVariableProxy(result_var, pos);
3435  return new (zone_) DoExpression(block, result, pos);
3436  }
3437 
3438  ThisFunction* NewThisFunction(int pos) {
3439  return new (zone_) ThisFunction(pos);
3440  }
3441 
3442  SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3443  Expression* home_object,
3444  int pos) {
3445  return new (zone_) SuperPropertyReference(this_var, home_object, pos);
3446  }
3447 
3448  SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3449  VariableProxy* new_target_var,
3450  VariableProxy* this_function_var,
3451  int pos) {
3452  return new (zone_)
3453  SuperCallReference(this_var, new_target_var, this_function_var, pos);
3454  }
3455 
3456  EmptyParentheses* NewEmptyParentheses(int pos) {
3457  return new (zone_) EmptyParentheses(pos);
3458  }
3459 
3460  GetIterator* NewGetIterator(Expression* iterable,
3461  Expression* destructured_iterable,
3462  IteratorType hint, int pos) {
3463  return new (zone_) GetIterator(iterable, destructured_iterable, hint, pos);
3464  }
3465 
3466  GetIterator* NewGetIterator(Expression* iterable, IteratorType hint,
3467  int pos) {
3468  return new (zone_) GetIterator(iterable, hint, pos);
3469  }
3470 
3471  GetTemplateObject* NewGetTemplateObject(
3472  const ZonePtrList<const AstRawString>* cooked_strings,
3473  const ZonePtrList<const AstRawString>* raw_strings, int pos) {
3474  return new (zone_) GetTemplateObject(cooked_strings, raw_strings, pos);
3475  }
3476 
3477  TemplateLiteral* NewTemplateLiteral(
3478  const ZonePtrList<const AstRawString>* string_parts,
3479  const ZonePtrList<Expression>* substitutions, int pos) {
3480  return new (zone_) TemplateLiteral(string_parts, substitutions, pos);
3481  }
3482 
3483  ImportCallExpression* NewImportCallExpression(Expression* args, int pos) {
3484  return new (zone_) ImportCallExpression(args, pos);
3485  }
3486 
3487  InitializeClassMembersStatement* NewInitializeClassMembersStatement(
3488  ZonePtrList<ClassLiteral::Property>* args, int pos) {
3489  return new (zone_) InitializeClassMembersStatement(args, pos);
3490  }
3491 
3492  Zone* zone() const { return zone_; }
3493 
3494  private:
3495  // This zone may be deallocated upon returning from parsing a function body
3496  // which we can guarantee is not going to be compiled or have its AST
3497  // inspected.
3498  // See ParseFunctionLiteral in parser.cc for preconditions.
3499  Zone* zone_;
3500  AstValueFactory* ast_value_factory_;
3501  class EmptyStatement* empty_statement_;
3502  class FailureExpression* failure_expression_;
3503 };
3504 
3505 
3506 // Type testing & conversion functions overridden by concrete subclasses.
3507 // Inline functions for AstNode.
3508 
3509 #define DECLARE_NODE_FUNCTIONS(type) \
3510  bool AstNode::Is##type() const { \
3511  NodeType mine = node_type(); \
3512  if (mine == AstNode::kRewritableExpression && \
3513  AstNode::k##type == AstNode::kAssignment) \
3514  mine = reinterpret_cast<const RewritableExpression*>(this) \
3515  ->expression() \
3516  ->node_type(); \
3517  return mine == AstNode::k##type; \
3518  } \
3519  type* AstNode::As##type() { \
3520  NodeType mine = node_type(); \
3521  AstNode* result = this; \
3522  if (mine == AstNode::kRewritableExpression && \
3523  AstNode::k##type == AstNode::kAssignment) { \
3524  result = \
3525  reinterpret_cast<const RewritableExpression*>(this)->expression(); \
3526  mine = result->node_type(); \
3527  } \
3528  return mine == AstNode::k##type ? reinterpret_cast<type*>(result) \
3529  : nullptr; \
3530  } \
3531  const type* AstNode::As##type() const { \
3532  NodeType mine = node_type(); \
3533  const AstNode* result = this; \
3534  if (mine == AstNode::kRewritableExpression && \
3535  AstNode::k##type != AstNode::kRewritableExpression) { \
3536  result = \
3537  reinterpret_cast<const RewritableExpression*>(this)->expression(); \
3538  mine = result->node_type(); \
3539  } \
3540  return mine == AstNode::k##type ? reinterpret_cast<const type*>(result) \
3541  : nullptr; \
3542  }
3543 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3544 FAILURE_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3545 #undef DECLARE_NODE_FUNCTIONS
3546 
3547 } // namespace internal
3548 } // namespace v8
3549 
3550 #endif // V8_AST_AST_H_
Definition: libplatform.h:13