V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
parser-base.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_PARSING_PARSER_BASE_H_
6 #define V8_PARSING_PARSER_BASE_H_
7 
8 #include <stdint.h>
9 #include <vector>
10 
11 #include "src/ast/ast-source-ranges.h"
12 #include "src/ast/ast.h"
13 #include "src/ast/scopes.h"
14 #include "src/bailout-reason.h"
15 #include "src/base/flags.h"
16 #include "src/base/hashmap.h"
17 #include "src/base/v8-fallthrough.h"
18 #include "src/counters.h"
19 #include "src/globals.h"
20 #include "src/log.h"
21 #include "src/message-template.h"
22 #include "src/parsing/expression-classifier.h"
23 #include "src/parsing/func-name-inferrer.h"
24 #include "src/parsing/scanner.h"
25 #include "src/parsing/token.h"
26 #include "src/pointer-with-payload.h"
27 #include "src/zone/zone-chunk-list.h"
28 
29 namespace v8 {
30 namespace internal {
31 
32 enum FunctionNameValidity {
33  kFunctionNameIsStrictReserved,
34  kSkipFunctionNameCheck,
35  kFunctionNameValidityUnknown
36 };
37 
38 enum AllowLabelledFunctionStatement {
39  kAllowLabelledFunctionStatement,
40  kDisallowLabelledFunctionStatement,
41 };
42 
43 enum class ParseFunctionFlag : uint8_t {
44  kIsNormal = 0,
45  kIsGenerator = 1 << 0,
46  kIsAsync = 1 << 1
47 };
48 
49 typedef base::Flags<ParseFunctionFlag> ParseFunctionFlags;
50 
52  explicit FormalParametersBase(DeclarationScope* scope) : scope(scope) {}
53 
54  int num_parameters() const {
55  // Don't include the rest parameter into the function's formal parameter
56  // count (esp. the SharedFunctionInfo::internal_formal_parameter_count,
57  // which says whether we need to create an arguments adaptor frame).
58  return arity - has_rest;
59  }
60 
61  void UpdateArityAndFunctionLength(bool is_optional, bool is_rest) {
62  if (!is_optional && !is_rest && function_length == arity) {
63  ++function_length;
64  }
65  ++arity;
66  }
67 
68  DeclarationScope* scope;
69  bool has_rest = false;
70  bool is_simple = true;
71  int function_length = 0;
72  int arity = 0;
73 };
74 
75 // Stack-allocated scope to collect source ranges from the parser.
76 class SourceRangeScope final {
77  public:
78  SourceRangeScope(const Scanner* scanner, SourceRange* range)
79  : scanner_(scanner), range_(range) {
80  range_->start = scanner->peek_location().beg_pos;
81  DCHECK_NE(range_->start, kNoSourcePosition);
82  DCHECK_EQ(range_->end, kNoSourcePosition);
83  }
84 
85  ~SourceRangeScope() {
86  DCHECK_EQ(kNoSourcePosition, range_->end);
87  range_->end = scanner_->location().end_pos;
88  DCHECK_NE(range_->end, kNoSourcePosition);
89  }
90 
91  private:
92  const Scanner* scanner_;
93  SourceRange* range_;
94 
95  DISALLOW_IMPLICIT_CONSTRUCTORS(SourceRangeScope);
96 };
97 
98 // ----------------------------------------------------------------------------
99 // The RETURN_IF_PARSE_ERROR macro is a convenient macro to enforce error
100 // handling for functions that may fail (by returning if there was an parser
101 // error).
102 //
103 // Usage:
104 // foo = ParseFoo(); // may fail
105 // RETURN_IF_PARSE_ERROR
106 //
107 // SAFE_USE(foo);
108 
109 #define RETURN_IF_PARSE_ERROR \
110  if (has_error()) return impl()->NullStatement();
111 
112 // Common base class template shared between parser and pre-parser.
113 // The Impl parameter is the actual class of the parser/pre-parser,
114 // following the Curiously Recurring Template Pattern (CRTP).
115 // The structure of the parser objects is roughly the following:
116 //
117 // // A structure template containing type definitions, needed to
118 // // avoid a cyclic dependency.
119 // template <typename Impl>
120 // struct ParserTypes;
121 //
122 // // The parser base object, which should just implement pure
123 // // parser behavior. The Impl parameter is the actual derived
124 // // class (according to CRTP), which implements impure parser
125 // // behavior.
126 // template <typename Impl>
127 // class ParserBase { ... };
128 //
129 // // And then, for each parser variant (e.g., parser, preparser, etc):
130 // class Parser;
131 //
132 // template <>
133 // class ParserTypes<Parser> { ... };
134 //
135 // class Parser : public ParserBase<Parser> { ... };
136 //
137 // The parser base object implements pure parsing, according to the
138 // language grammar. Different parser implementations may exhibit
139 // different parser-driven behavior that is not considered as pure
140 // parsing, e.g., early error detection and reporting, AST generation, etc.
141 
142 // The ParserTypes structure encapsulates the differences in the
143 // types used in parsing methods. E.g., Parser methods use Expression*
144 // and PreParser methods use PreParserExpression. For any given parser
145 // implementation class Impl, it is expected to contain the following typedefs:
146 //
147 // template <>
148 // struct ParserTypes<Impl> {
149 // // Synonyms for ParserBase<Impl> and Impl, respectively.
150 // typedef Base;
151 // typedef Impl;
152 // // Return types for traversing functions.
153 // typedef Identifier;
154 // typedef Expression;
155 // typedef FunctionLiteral;
156 // typedef ObjectLiteralProperty;
157 // typedef ClassLiteralProperty;
158 // typedef ExpressionList;
159 // typedef ObjectPropertyList;
160 // typedef ClassPropertyList;
161 // typedef FormalParameters;
162 // typedef Statement;
163 // typedef StatementList;
164 // typedef Block;
165 // typedef BreakableStatement;
166 // typedef ForStatement;
167 // typedef IterationStatement;
168 // // For constructing objects returned by the traversing functions.
169 // typedef Factory;
170 // // For other implementation-specific tasks.
171 // typedef Target;
172 // typedef TargetScope;
173 // };
174 
175 template <typename Impl>
176 struct ParserTypes;
177 
178 enum class ParsePropertyKind : uint8_t {
179  kAccessorGetter,
180  kAccessorSetter,
181  kValue,
182  kShorthand,
183  kAssign,
184  kMethod,
185  kClassField,
186  kShorthandOrClassField,
187  kSpread,
188  kNotSet
189 };
190 
191 template <typename Impl>
192 class ParserBase {
193  public:
194  // Shorten type names defined by ParserTypes<Impl>.
195  typedef ParserTypes<Impl> Types;
198 
199  // Return types for traversing functions.
200  typedef typename Types::Block BlockT;
201  typedef typename Types::BreakableStatement BreakableStatementT;
202  typedef typename Types::ClassLiteralProperty ClassLiteralPropertyT;
203  typedef typename Types::ClassPropertyList ClassPropertyListT;
204  typedef typename Types::Expression ExpressionT;
205  typedef typename Types::ExpressionList ExpressionListT;
206  typedef typename Types::FormalParameters FormalParametersT;
207  typedef typename Types::ForStatement ForStatementT;
208  typedef typename Types::FunctionLiteral FunctionLiteralT;
209  typedef typename Types::Identifier IdentifierT;
210  typedef typename Types::IterationStatement IterationStatementT;
211  typedef typename Types::ObjectLiteralProperty ObjectLiteralPropertyT;
212  typedef typename Types::ObjectPropertyList ObjectPropertyListT;
213  typedef typename Types::RewritableExpression RewritableExpressionT;
214  typedef typename Types::Statement StatementT;
215  typedef typename Types::StatementList StatementListT;
216  typedef typename Types::Suspend SuspendExpressionT;
217  // For constructing objects returned by the traversing functions.
218  typedef typename Types::Factory FactoryT;
219  // Other implementation-specific tasks.
220  typedef typename Types::FuncNameInferrer FuncNameInferrer;
221  typedef typename Types::FuncNameInferrer::State FuncNameInferrerState;
222  typedef typename Types::SourceRange SourceRange;
223  typedef typename Types::SourceRangeScope SourceRangeScope;
224  typedef typename Types::Target TargetT;
225  typedef typename Types::TargetScope TargetScopeT;
226 
227  // All implementation-specific methods must be called through this.
228  Impl* impl() { return static_cast<Impl*>(this); }
229  const Impl* impl() const { return static_cast<const Impl*>(this); }
230 
231  ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
232  v8::Extension* extension, AstValueFactory* ast_value_factory,
233  PendingCompilationErrorHandler* pending_error_handler,
234  RuntimeCallStats* runtime_call_stats, Logger* logger,
235  int script_id, bool parsing_module, bool parsing_on_main_thread)
236  : scope_(nullptr),
237  original_scope_(nullptr),
238  function_state_(nullptr),
239  extension_(extension),
240  fni_(ast_value_factory),
241  ast_value_factory_(ast_value_factory),
242  ast_node_factory_(ast_value_factory, zone),
243  runtime_call_stats_(runtime_call_stats),
244  logger_(logger),
245  parsing_on_main_thread_(parsing_on_main_thread),
246  parsing_module_(parsing_module),
247  stack_limit_(stack_limit),
248  pending_error_handler_(pending_error_handler),
249  zone_(zone),
250  classifier_(nullptr),
251  scanner_(scanner),
252  function_literal_id_(0),
253  script_id_(script_id),
254  default_eager_compile_hint_(FunctionLiteral::kShouldLazyCompile),
255  allow_natives_(false),
256  allow_harmony_public_fields_(false),
257  allow_harmony_static_fields_(false),
258  allow_harmony_dynamic_import_(false),
259  allow_harmony_import_meta_(false),
260  allow_harmony_private_fields_(false),
261  allow_harmony_private_methods_(false),
262  allow_eval_cache_(true) {
263  pointer_buffer_.reserve(128);
264  }
265 
266 #define ALLOW_ACCESSORS(name) \
267  bool allow_##name() const { return allow_##name##_; } \
268  void set_allow_##name(bool allow) { allow_##name##_ = allow; }
269 
270  void set_rewritable_length(int i) { rewritable_length_ = i; }
271 
272  ALLOW_ACCESSORS(natives);
273  ALLOW_ACCESSORS(harmony_public_fields);
274  ALLOW_ACCESSORS(harmony_static_fields);
275  ALLOW_ACCESSORS(harmony_dynamic_import);
276  ALLOW_ACCESSORS(harmony_import_meta);
277  ALLOW_ACCESSORS(harmony_private_methods);
278  ALLOW_ACCESSORS(eval_cache);
279 
280 #undef ALLOW_ACCESSORS
281 
282  V8_INLINE bool has_error() const { return scanner()->has_parser_error(); }
283  bool allow_harmony_numeric_separator() const {
284  return scanner()->allow_harmony_numeric_separator();
285  }
286  void set_allow_harmony_numeric_separator(bool allow) {
287  scanner()->set_allow_harmony_numeric_separator(allow);
288  }
289 
290  bool allow_harmony_private_fields() const {
291  return scanner()->allow_harmony_private_fields();
292  }
293  void set_allow_harmony_private_fields(bool allow) {
294  scanner()->set_allow_harmony_private_fields(allow);
295  }
296 
297  uintptr_t stack_limit() const { return stack_limit_; }
298 
299  void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }
300 
301  void set_default_eager_compile_hint(
302  FunctionLiteral::EagerCompileHint eager_compile_hint) {
303  default_eager_compile_hint_ = eager_compile_hint;
304  }
305 
306  FunctionLiteral::EagerCompileHint default_eager_compile_hint() const {
307  return default_eager_compile_hint_;
308  }
309 
310  int GetNextFunctionLiteralId() { return ++function_literal_id_; }
311  int GetLastFunctionLiteralId() const { return function_literal_id_; }
312 
313  void SkipFunctionLiterals(int delta) { function_literal_id_ += delta; }
314 
315  void ResetFunctionLiteralId() { function_literal_id_ = 0; }
316 
317  // The Zone where the parsing outputs are stored.
318  Zone* main_zone() const { return ast_value_factory()->zone(); }
319 
320  // The current Zone, which might be the main zone or a temporary Zone.
321  Zone* zone() const { return zone_; }
322 
323  protected:
325 
326  enum AllowRestrictedIdentifiers {
327  kAllowRestrictedIdentifiers,
328  kDontAllowRestrictedIdentifiers
329  };
330 
331  enum LazyParsingResult { kLazyParsingComplete, kLazyParsingAborted };
332 
333  enum VariableDeclarationContext {
334  kStatementListItem,
335  kStatement,
336  kForStatement
337  };
338 
339  class ClassLiteralChecker;
340 
341  // ---------------------------------------------------------------------------
342  // BlockState and FunctionState implement the parser's scope stack.
343  // The parser's current scope is in scope_. BlockState and FunctionState
344  // constructors push on the scope stack and the destructors pop. They are also
345  // used to hold the parser's per-funcion state.
346  class BlockState {
347  public:
348  BlockState(Scope** scope_stack, Scope* scope)
349  : scope_stack_(scope_stack), outer_scope_(*scope_stack) {
350  *scope_stack_ = scope;
351  }
352 
353  BlockState(Zone* zone, Scope** scope_stack)
354  : BlockState(scope_stack,
355  new (zone) Scope(zone, *scope_stack, BLOCK_SCOPE)) {}
356 
357  ~BlockState() { *scope_stack_ = outer_scope_; }
358 
359  private:
360  Scope** const scope_stack_;
361  Scope* const outer_scope_;
362  };
363 
364  class FunctionState final : public BlockState {
365  public:
366  FunctionState(FunctionState** function_state_stack, Scope** scope_stack,
367  DeclarationScope* scope);
368  ~FunctionState();
369 
370  DeclarationScope* scope() const { return scope_->AsDeclarationScope(); }
371 
372  void AddProperty() { expected_property_count_++; }
373  int expected_property_count() { return expected_property_count_; }
374 
375  void DisableOptimization(BailoutReason reason) {
376  dont_optimize_reason_ = reason;
377  }
378  BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
379 
380  void AddSuspend() { suspend_count_++; }
381  int suspend_count() const { return suspend_count_; }
382  bool CanSuspend() const { return suspend_count_ > 0; }
383 
384  FunctionKind kind() const { return scope()->function_kind(); }
385 
386  void RewindDestructuringAssignments(int pos) {
387  destructuring_assignments_to_rewrite_.Rewind(pos);
388  }
389 
390  void AdoptDestructuringAssignmentsFromParentState(int pos) {
391  const auto& outer_assignments =
392  outer_function_state_->destructuring_assignments_to_rewrite_;
393  DCHECK_GE(outer_assignments.size(), pos);
394  auto it = outer_assignments.begin();
395  it.Advance(pos);
396  for (; it != outer_assignments.end(); ++it) {
397  auto expr = *it;
398  expr->set_scope(scope_);
399  destructuring_assignments_to_rewrite_.push_back(expr);
400  }
401  outer_function_state_->RewindDestructuringAssignments(pos);
402  }
403 
405  destructuring_assignments_to_rewrite() const {
406  return destructuring_assignments_to_rewrite_;
407  }
408 
409  ZoneList<typename ExpressionClassifier::Error>* GetReportedErrorList() {
410  return &reported_errors_;
411  }
412 
413  bool next_function_is_likely_called() const {
414  return next_function_is_likely_called_;
415  }
416 
417  bool previous_function_was_likely_called() const {
418  return previous_function_was_likely_called_;
419  }
420 
421  void set_next_function_is_likely_called() {
422  next_function_is_likely_called_ = true;
423  }
424 
425  void RecordFunctionOrEvalCall() { contains_function_or_eval_ = true; }
426  bool contains_function_or_eval() const {
427  return contains_function_or_eval_;
428  }
429 
431  public:
433  : state_and_prev_value_(state, state->contains_function_or_eval_) {
434  state->contains_function_or_eval_ = false;
435  }
437  bool found = state_and_prev_value_->contains_function_or_eval_;
438  if (!found) {
439  state_and_prev_value_->contains_function_or_eval_ =
440  state_and_prev_value_.GetPayload();
441  }
442  }
443 
444  private:
445  PointerWithPayload<FunctionState, bool, 1> state_and_prev_value_;
446  };
447 
448  private:
449  void AddDestructuringAssignment(RewritableExpressionT expr) {
450  destructuring_assignments_to_rewrite_.push_back(expr);
451  }
452 
453  // Properties count estimation.
454  int expected_property_count_;
455 
456  // How many suspends are needed for this function.
457  int suspend_count_;
458 
459  FunctionState** function_state_stack_;
460  FunctionState* outer_function_state_;
461  DeclarationScope* scope_;
462 
463  ZoneChunkList<RewritableExpressionT> destructuring_assignments_to_rewrite_;
464 
466 
467  // A reason, if any, why this function should not be optimized.
468  BailoutReason dont_optimize_reason_;
469 
470  // Record whether the next (=== immediately following) function literal is
471  // preceded by a parenthesis / exclamation mark. Also record the previous
472  // state.
473  // These are managed by the FunctionState constructor; the caller may only
474  // call set_next_function_is_likely_called.
475  bool next_function_is_likely_called_;
476  bool previous_function_was_likely_called_;
477 
478  // Track if a function or eval occurs within this FunctionState
479  bool contains_function_or_eval_;
480 
481  friend Impl;
482  };
483 
485  enum Kind { NORMAL, PARAMETER, FOR_EACH };
486  Scope* scope;
487  VariableMode mode;
488  int declaration_pos;
489  int initialization_pos;
490  Kind declaration_kind;
491  };
492 
494  struct Declaration {
495  Declaration(ExpressionT pattern, int initializer_position,
496  ExpressionT initializer)
497  : pattern(pattern),
498  initializer_position(initializer_position),
499  initializer(initializer) {}
500 
501  ExpressionT pattern;
502  int initializer_position;
503  int value_beg_position = kNoSourcePosition;
504  ExpressionT initializer;
505  };
506 
508  : first_initializer_loc(Scanner::Location::invalid()),
509  bindings_loc(Scanner::Location::invalid()) {}
510 
511  DeclarationDescriptor descriptor;
512  std::vector<Declaration> declarations;
513  Scanner::Location first_initializer_loc;
514  Scanner::Location bindings_loc;
515  };
516 
517  struct CatchInfo {
518  public:
519  explicit CatchInfo(ParserBase* parser)
520  : name(parser->impl()->NullIdentifier()),
521  pattern(parser->impl()->NullExpression()),
522  scope(nullptr),
523  init_block(parser->impl()->NullStatement()),
524  inner_block(parser->impl()->NullStatement()),
525  bound_names(1, parser->zone()) {}
526  IdentifierT name;
527  ExpressionT pattern;
528  Scope* scope;
529  BlockT init_block;
530  BlockT inner_block;
532  };
533 
534  struct ForInfo {
535  public:
536  explicit ForInfo(ParserBase* parser)
537  : bound_names(1, parser->zone()),
538  mode(ForEachStatement::ENUMERATE),
539  position(kNoSourcePosition),
540  parsing_result() {}
542  ForEachStatement::VisitMode mode;
543  int position;
544  DeclarationParsingResult parsing_result;
545  };
546 
547  struct ClassInfo {
548  public:
549  explicit ClassInfo(ParserBase* parser)
550  : variable(nullptr),
551  extends(parser->impl()->NullExpression()),
552  properties(parser->impl()->NewClassPropertyList(4)),
553  static_fields(parser->impl()->NewClassPropertyList(4)),
554  instance_fields(parser->impl()->NewClassPropertyList(4)),
555  constructor(parser->impl()->NullExpression()),
556  has_seen_constructor(false),
557  has_name_static_property(false),
558  has_static_computed_names(false),
559  has_static_class_fields(false),
560  has_instance_members(false),
561  is_anonymous(false),
562  static_fields_scope(nullptr),
563  instance_members_scope(nullptr),
564  computed_field_count(0) {}
565  Variable* variable;
566  ExpressionT extends;
567  ClassPropertyListT properties;
568  ClassPropertyListT static_fields;
569  ClassPropertyListT instance_fields;
570  FunctionLiteralT constructor;
571 
572  bool has_seen_constructor;
573  bool has_name_static_property;
574  bool has_static_computed_names;
575  bool has_static_class_fields;
576  bool has_instance_members;
577  bool is_anonymous;
578  DeclarationScope* static_fields_scope;
579  DeclarationScope* instance_members_scope;
580  int computed_field_count;
581  };
582 
583  enum class PropertyPosition { kObjectLiteral, kClassLiteral };
585  public:
586  explicit ParsePropertyInfo(ParserBase* parser)
587  : name(parser->impl()->NullIdentifier()),
588  position(PropertyPosition::kClassLiteral),
589  function_flags(ParseFunctionFlag::kIsNormal),
590  kind(ParsePropertyKind::kNotSet),
591  is_computed_name(false),
592  is_private(false),
593  is_static(false),
594  is_rest(false) {}
595 
596  bool ParsePropertyKindFromToken(Token::Value token) {
597  // This returns true, setting the property kind, iff the given token is
598  // one which must occur after a property name, indicating that the
599  // previous token was in fact a name and not a modifier (like the "get" in
600  // "get x").
601  switch (token) {
602  case Token::COLON:
603  kind = ParsePropertyKind::kValue;
604  return true;
605  case Token::COMMA:
606  kind = ParsePropertyKind::kShorthand;
607  return true;
608  case Token::RBRACE:
609  kind = ParsePropertyKind::kShorthandOrClassField;
610  return true;
611  case Token::ASSIGN:
612  kind = ParsePropertyKind::kAssign;
613  return true;
614  case Token::LPAREN:
615  kind = ParsePropertyKind::kMethod;
616  return true;
617  case Token::MUL:
618  case Token::SEMICOLON:
619  kind = ParsePropertyKind::kClassField;
620  return true;
621  default:
622  break;
623  }
624  return false;
625  }
626 
627  IdentifierT name;
628  PropertyPosition position;
629  ParseFunctionFlags function_flags;
630  ParsePropertyKind kind;
631  bool is_computed_name;
632  bool is_private;
633  bool is_static;
634  bool is_rest;
635  };
636 
637  ClassLiteralProperty::Kind ClassPropertyKindFor(ParsePropertyKind kind) {
638  switch (kind) {
639  case ParsePropertyKind::kAccessorGetter:
640  return ClassLiteralProperty::GETTER;
641  case ParsePropertyKind::kAccessorSetter:
642  return ClassLiteralProperty::SETTER;
643  case ParsePropertyKind::kMethod:
644  return ClassLiteralProperty::METHOD;
645  case ParsePropertyKind::kClassField:
646  return ClassLiteralProperty::FIELD;
647  default:
648  // Only returns for deterministic kinds
649  UNREACHABLE();
650  }
651  }
652 
653  const AstRawString* ClassFieldVariableName(AstValueFactory* ast_value_factory,
654  int index) {
655  std::string name = ".class-field-" + std::to_string(index);
656  return ast_value_factory->GetOneByteString(name.c_str());
657  }
658 
659  DeclarationScope* NewScriptScope() const {
660  return new (zone()) DeclarationScope(zone(), ast_value_factory());
661  }
662 
663  DeclarationScope* NewVarblockScope() const {
664  return new (zone()) DeclarationScope(zone(), scope(), BLOCK_SCOPE);
665  }
666 
667  ModuleScope* NewModuleScope(DeclarationScope* parent) const {
668  return new (zone()) ModuleScope(parent, ast_value_factory());
669  }
670 
671  DeclarationScope* NewEvalScope(Scope* parent) const {
672  return new (zone()) DeclarationScope(zone(), parent, EVAL_SCOPE);
673  }
674 
675  Scope* NewScope(ScopeType scope_type) const {
676  return NewScopeWithParent(scope(), scope_type);
677  }
678 
679  // This constructor should only be used when absolutely necessary. Most scopes
680  // should automatically use scope() as parent, and be fine with
681  // NewScope(ScopeType) above.
682  Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type) const {
683  // Must always use the specific constructors for the blacklisted scope
684  // types.
685  DCHECK_NE(FUNCTION_SCOPE, scope_type);
686  DCHECK_NE(SCRIPT_SCOPE, scope_type);
687  DCHECK_NE(MODULE_SCOPE, scope_type);
688  DCHECK_NOT_NULL(parent);
689  return new (zone()) Scope(zone(), parent, scope_type);
690  }
691 
692  // Creates a function scope that always allocates in zone(). The function
693  // scope itself is either allocated in zone() or in target_zone if one is
694  // passed in.
695  DeclarationScope* NewFunctionScope(FunctionKind kind,
696  Zone* parse_zone = nullptr) const {
697  DCHECK(ast_value_factory());
698  if (parse_zone == nullptr) parse_zone = zone();
699  DeclarationScope* result = new (zone())
700  DeclarationScope(parse_zone, scope(), FUNCTION_SCOPE, kind);
701 
702  // Record presence of an inner function scope
703  function_state_->RecordFunctionOrEvalCall();
704 
705  // TODO(verwaest): Move into the DeclarationScope constructor.
706  if (!IsArrowFunction(kind)) {
707  result->DeclareDefaultFunctionVariables(ast_value_factory());
708  }
709  return result;
710  }
711 
712  V8_INLINE DeclarationScope* GetDeclarationScope() const {
713  return scope()->GetDeclarationScope();
714  }
715  V8_INLINE DeclarationScope* GetClosureScope() const {
716  return scope()->GetClosureScope();
717  }
718 
719  Scanner* scanner() const { return scanner_; }
720  AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
721  int position() const { return scanner_->location().beg_pos; }
722  int peek_position() const { return scanner_->peek_location().beg_pos; }
723  int end_position() const { return scanner_->location().end_pos; }
724  int peek_end_position() const { return scanner_->peek_location().end_pos; }
725  bool stack_overflow() const {
726  return pending_error_handler()->stack_overflow();
727  }
728  void set_stack_overflow() {
729  scanner_->set_parser_error();
730  pending_error_handler()->set_stack_overflow();
731  }
732  void CheckStackOverflow() {
733  // Any further calls to Next or peek will return the illegal token.
734  if (GetCurrentStackPosition() < stack_limit_) set_stack_overflow();
735  }
736  int script_id() { return script_id_; }
737  void set_script_id(int id) { script_id_ = id; }
738 
739  V8_INLINE Token::Value peek() { return scanner()->peek(); }
740 
741  // Returns the position past the following semicolon (if it exists), and the
742  // position past the end of the current token otherwise.
743  int PositionAfterSemicolon() {
744  return (peek() == Token::SEMICOLON) ? peek_end_position() : end_position();
745  }
746 
747  V8_INLINE Token::Value PeekAhead() { return scanner()->PeekAhead(); }
748 
749  V8_INLINE Token::Value Next() { return scanner()->Next(); }
750 
751  V8_INLINE void Consume(Token::Value token) {
752  Token::Value next = scanner()->Next();
753  USE(next);
754  USE(token);
755  DCHECK_IMPLIES(!has_error(), next == token);
756  }
757 
758  V8_INLINE bool Check(Token::Value token) {
759  Token::Value next = scanner()->peek();
760  if (next == token) {
761  Consume(next);
762  return true;
763  }
764  return false;
765  }
766 
767  void Expect(Token::Value token) {
768  Token::Value next = Next();
769  if (V8_UNLIKELY(next != token)) {
770  ReportUnexpectedToken(next);
771  }
772  }
773 
774  void ExpectSemicolon() {
775  // Check for automatic semicolon insertion according to
776  // the rules given in ECMA-262, section 7.9, page 21.
777  Token::Value tok = peek();
778  if (V8_LIKELY(tok == Token::SEMICOLON)) {
779  Next();
780  return;
781  }
782  if (V8_LIKELY(scanner()->HasLineTerminatorBeforeNext() ||
783  Token::IsAutoSemicolon(tok))) {
784  return;
785  }
786 
787  if (scanner()->current_token() == Token::AWAIT && !is_async_function()) {
788  ReportMessageAt(scanner()->location(),
789  MessageTemplate::kAwaitNotInAsyncFunction, kSyntaxError);
790  return;
791  }
792 
793  ReportUnexpectedToken(Next());
794  }
795 
796  bool peek_any_identifier() { return Token::IsAnyIdentifier(peek()); }
797 
798  bool PeekContextualKeyword(const AstRawString* name) {
799  return peek() == Token::IDENTIFIER &&
800  scanner()->NextSymbol(ast_value_factory()) == name;
801  }
802 
803  bool CheckContextualKeyword(const AstRawString* name) {
804  if (PeekContextualKeyword(name)) {
805  Consume(Token::IDENTIFIER);
806  return true;
807  }
808  return false;
809  }
810 
811  void ExpectMetaProperty(const AstRawString* property_name,
812  const char* full_name, int pos);
813 
814  void ExpectContextualKeyword(const AstRawString* name) {
815  Expect(Token::IDENTIFIER);
816  if (V8_UNLIKELY(scanner()->CurrentSymbol(ast_value_factory()) != name)) {
817  ReportUnexpectedToken(scanner()->current_token());
818  }
819  }
820 
821  bool CheckInOrOf(ForEachStatement::VisitMode* visit_mode) {
822  if (Check(Token::IN)) {
823  *visit_mode = ForEachStatement::ENUMERATE;
824  return true;
825  } else if (CheckContextualKeyword(ast_value_factory()->of_string())) {
826  *visit_mode = ForEachStatement::ITERATE;
827  return true;
828  }
829  return false;
830  }
831 
832  bool PeekInOrOf() {
833  return peek() == Token::IN ||
834  PeekContextualKeyword(ast_value_factory()->of_string());
835  }
836 
837  // Checks whether an octal literal was last seen between beg_pos and end_pos.
838  // Only called for strict mode strings.
839  void CheckStrictOctalLiteral(int beg_pos, int end_pos) {
840  Scanner::Location octal = scanner()->octal_position();
841  if (octal.IsValid() && beg_pos <= octal.beg_pos &&
842  octal.end_pos <= end_pos) {
843  MessageTemplate message = scanner()->octal_message();
844  DCHECK_NE(message, MessageTemplate::kNone);
845  impl()->ReportMessageAt(octal, message);
846  scanner()->clear_octal_position();
847  if (message == MessageTemplate::kStrictDecimalWithLeadingZero) {
848  impl()->CountUsage(v8::Isolate::kDecimalWithLeadingZeroInStrictMode);
849  }
850  }
851  }
852 
853  // Checks if an octal literal or an invalid hex or unicode escape sequence
854  // appears in the current template literal token. In the presence of such,
855  // either returns false or reports an error, depending on should_throw.
856  // Otherwise returns true.
857  inline bool CheckTemplateEscapes(bool should_throw) {
858  DCHECK(Token::IsTemplate(scanner()->current_token()));
859  if (!scanner()->has_invalid_template_escape()) return true;
860 
861  // Handle error case(s)
862  if (should_throw) {
863  impl()->ReportMessageAt(scanner()->invalid_template_escape_location(),
864  scanner()->invalid_template_escape_message());
865  }
866  scanner()->clear_invalid_template_escape_message();
867  return should_throw;
868  }
869 
870  void CheckDestructuringElement(ExpressionT element, int beg_pos, int end_pos);
871  void CheckArrowFormalParameter(ExpressionT formal);
872 
873  // Checking the name of a function literal. This has to be done after parsing
874  // the function, since the function can declare itself strict.
875  void CheckFunctionName(LanguageMode language_mode, IdentifierT function_name,
876  FunctionNameValidity function_name_validity,
877  const Scanner::Location& function_name_loc) {
878  if (impl()->IsNull(function_name)) return;
879  if (function_name_validity == kSkipFunctionNameCheck) return;
880  // The function name needs to be checked in strict mode.
881  if (is_sloppy(language_mode)) return;
882 
883  if (impl()->IsEvalOrArguments(function_name)) {
884  impl()->ReportMessageAt(function_name_loc,
885  MessageTemplate::kStrictEvalArguments);
886  return;
887  }
888  if (function_name_validity == kFunctionNameIsStrictReserved) {
889  impl()->ReportMessageAt(function_name_loc,
890  MessageTemplate::kUnexpectedStrictReserved);
891  return;
892  }
893  }
894 
895  typename Types::Factory* factory() { return &ast_node_factory_; }
896 
897  DeclarationScope* GetReceiverScope() const {
898  return scope()->GetReceiverScope();
899  }
900  LanguageMode language_mode() { return scope()->language_mode(); }
901  void RaiseLanguageMode(LanguageMode mode) {
902  LanguageMode old = scope()->language_mode();
903  impl()->SetLanguageMode(scope(), old > mode ? old : mode);
904  }
905  bool is_generator() const {
906  return IsGeneratorFunction(function_state_->kind());
907  }
908  bool is_async_function() const {
909  return IsAsyncFunction(function_state_->kind());
910  }
911  bool is_async_generator() const {
912  return IsAsyncGeneratorFunction(function_state_->kind());
913  }
914  bool is_resumable() const {
915  return IsResumableFunction(function_state_->kind());
916  }
917 
918  const PendingCompilationErrorHandler* pending_error_handler() const {
919  return pending_error_handler_;
920  }
921  PendingCompilationErrorHandler* pending_error_handler() {
922  return pending_error_handler_;
923  }
924 
925  // Report syntax errors.
926  V8_NOINLINE void ReportMessage(MessageTemplate message) {
927  Scanner::Location source_location = scanner()->location();
928  impl()->ReportMessageAt(source_location, message,
929  static_cast<const char*>(nullptr), kSyntaxError);
930  }
931 
932  template <typename T>
933  V8_NOINLINE void ReportMessage(MessageTemplate message, T arg,
934  ParseErrorType error_type = kSyntaxError) {
935  Scanner::Location source_location = scanner()->location();
936  impl()->ReportMessageAt(source_location, message, arg, error_type);
937  }
938 
939  V8_NOINLINE void ReportMessageAt(Scanner::Location location,
940  MessageTemplate message,
941  ParseErrorType error_type) {
942  impl()->ReportMessageAt(location, message,
943  static_cast<const char*>(nullptr), error_type);
944  }
945 
946  V8_NOINLINE void ReportUnexpectedToken(Token::Value token);
947  V8_NOINLINE void ReportUnexpectedTokenAt(
948  Scanner::Location location, Token::Value token,
949  MessageTemplate message = MessageTemplate::kUnexpectedToken);
950 
951  V8_NOINLINE void ReportClassifierError(
952  const typename ExpressionClassifier::Error& error) {
953  if (classifier()->does_error_reporting()) {
954  impl()->ReportMessageAt(error.location, error.message(), error.arg);
955  } else {
956  impl()->ReportUnidentifiableError();
957  }
958  }
959 
960  void ValidateExpression() {
961  if (!classifier()->is_valid_expression()) {
962  ReportClassifierError(classifier()->expression_error());
963  }
964  }
965 
966  void ValidateFormalParameterInitializer() {
967  if (!classifier()->is_valid_formal_parameter_initializer()) {
968  ReportClassifierError(classifier()->formal_parameter_initializer_error());
969  }
970  }
971 
972  void ValidateBindingPattern() {
973  if (!classifier()->is_valid_binding_pattern()) {
974  ReportClassifierError(classifier()->binding_pattern_error());
975  }
976  ValidatePattern();
977  }
978 
979  void ValidatePattern() {
980  if (!classifier()->is_valid_pattern()) {
981  ReportClassifierError(classifier()->pattern_error());
982  }
983  }
984 
985  void ValidatePattern(ExpressionT expression) {
986  if (expression->is_parenthesized()) {
987  impl()->ReportMessageAt(
988  Scanner::Location(expression->position(), end_position()),
989  MessageTemplate::kInvalidDestructuringTarget);
990  }
991  ValidatePattern();
992  }
993 
994  void ValidateFormalParameters(LanguageMode language_mode,
995  const FormalParametersT& parameters,
996  bool allow_duplicates) {
997  if (!allow_duplicates && parameters.has_duplicate()) {
998  if (classifier()->does_error_reporting()) {
999  impl()->ReportMessageAt(parameters.duplicate_location(),
1000  MessageTemplate::kParamDupe);
1001  } else {
1002  impl()->ReportUnidentifiableError();
1003  }
1004  } else if (is_strict(language_mode) &&
1005  !classifier()->is_valid_strict_mode_formal_parameters()) {
1006  ReportClassifierError(classifier()->strict_mode_formal_parameter_error());
1007  }
1008  }
1009 
1010  void ValidateArrowFormalParameters(ExpressionT expression) {
1011  ValidateBindingPattern();
1012  if (!expression->is_parenthesized() && !impl()->IsIdentifier(expression)) {
1013  // Non-parenthesized destructuring param.
1014  impl()->ReportMessageAt(
1015  Scanner::Location(expression->position(), position()),
1016  MessageTemplate::kMalformedArrowFunParamList);
1017  }
1018  DCHECK_IMPLIES(IsAsyncFunction(next_arrow_function_kind_),
1019  classifier()->is_valid_async_arrow_formal_parameters());
1020  }
1021 
1022  void ValidateLetPattern() {
1023  if (!classifier()->is_valid_let_pattern()) {
1024  ReportClassifierError(classifier()->let_pattern_error());
1025  }
1026  }
1027 
1028  // Parses an identifier that is valid for the current scope, in particular it
1029  // fails on strict mode future reserved keywords in a strict scope. If
1030  // allow_eval_or_arguments is kAllowEvalOrArguments, we allow "eval" or
1031  // "arguments" as identifier even in strict mode (this is needed in cases like
1032  // "var foo = eval;").
1033  IdentifierT ParseIdentifier(AllowRestrictedIdentifiers);
1034  V8_INLINE IdentifierT ParseAndClassifyIdentifier();
1035  // Parses an identifier or a strict mode future reserved word, and indicate
1036  // whether it is strict mode future reserved. Allows passing in function_kind
1037  // for the case of parsing the identifier in a function expression, where the
1038  // relevant "function_kind" bit is of the function being parsed, not the
1039  // containing function.
1040  IdentifierT ParseIdentifierOrStrictReservedWord(FunctionKind function_kind,
1041  bool* is_strict_reserved,
1042  bool* is_await);
1043  IdentifierT ParseIdentifierOrStrictReservedWord(bool* is_strict_reserved,
1044  bool* is_await) {
1045  return ParseIdentifierOrStrictReservedWord(function_state_->kind(),
1046  is_strict_reserved, is_await);
1047  }
1048 
1049  V8_INLINE IdentifierT ParseIdentifierName();
1050 
1051  ExpressionT ParseIdentifierNameOrPrivateName();
1052 
1053  ExpressionT ParseRegExpLiteral();
1054 
1055  ExpressionT ParseBindingPattern();
1056  ExpressionT ParsePrimaryExpression();
1057 
1058  // Use when parsing an expression that is known to not be a pattern or part
1059  // of a pattern.
1060  V8_INLINE ExpressionT ParseExpression();
1061 
1062  // This method does not wrap the parsing of the expression inside a
1063  // new expression classifier; it uses the top-level classifier instead.
1064  // It should be used whenever we're parsing something with the "cover"
1065  // grammar that recognizes both patterns and non-patterns (which roughly
1066  // corresponds to what's inside the parentheses generated by the symbol
1067  // "CoverParenthesizedExpressionAndArrowParameterList" in the ES 2017
1068  // specification).
1069  ExpressionT ParseExpressionCoverGrammar();
1070  ExpressionT ParseArrowFormalsWithRest(ExpressionListT* list);
1071 
1072  ExpressionT ParseArrayLiteral();
1073 
1074  inline static bool IsAccessor(ParsePropertyKind kind) {
1075  return IsInRange(kind, ParsePropertyKind::kAccessorGetter,
1076  ParsePropertyKind::kAccessorSetter);
1077  }
1078 
1079  ExpressionT ParsePropertyName(ParsePropertyInfo* prop_info);
1080  ExpressionT ParseObjectLiteral();
1081  ClassLiteralPropertyT ParseClassPropertyDefinition(
1082  ClassInfo* class_info, ParsePropertyInfo* prop_info, bool has_extends);
1083  void CheckClassFieldName(IdentifierT name, bool is_static);
1084  void CheckClassMethodName(IdentifierT name, ParsePropertyKind type,
1085  ParseFunctionFlags flags, bool is_static,
1086  bool* has_seen_constructor);
1087  ExpressionT ParseMemberInitializer(ClassInfo* class_info, int beg_pos,
1088  bool is_static);
1089  ObjectLiteralPropertyT ParseObjectPropertyDefinition(
1090  ParsePropertyInfo* prop_info, bool* has_seen_proto);
1091  void ParseArguments(ExpressionListT* args, bool* has_spread,
1092  bool maybe_arrow);
1093  void ParseArguments(ExpressionListT* args, bool* has_spread) {
1094  ParseArguments(args, has_spread, false);
1095  }
1096 
1097  ExpressionT ParseAssignmentExpression();
1098  ExpressionT ParseYieldExpression();
1099  V8_INLINE ExpressionT ParseConditionalExpression();
1100  ExpressionT ParseConditionalContinuation(ExpressionT expression, int pos);
1101  ExpressionT ParseBinaryContinuation(ExpressionT x, int prec, int prec1);
1102  V8_INLINE ExpressionT ParseBinaryExpression(int prec);
1103  ExpressionT ParseUnaryOrPrefixExpression();
1104  ExpressionT ParseAwaitExpression();
1105  V8_INLINE ExpressionT ParseUnaryExpression();
1106  V8_INLINE ExpressionT ParsePostfixExpression();
1107  V8_INLINE ExpressionT ParseLeftHandSideExpression();
1108  ExpressionT ParseLeftHandSideContinuation(ExpressionT expression);
1109  ExpressionT ParseMemberWithPresentNewPrefixesExpression();
1110  V8_INLINE ExpressionT ParseMemberWithNewPrefixesExpression();
1111  ExpressionT ParseFunctionExpression();
1112  V8_INLINE ExpressionT ParseMemberExpression();
1113  V8_INLINE ExpressionT
1114  ParseMemberExpressionContinuation(ExpressionT expression) {
1115  if (!Token::IsMember(peek())) return expression;
1116  return DoParseMemberExpressionContinuation(expression);
1117  }
1118  ExpressionT DoParseMemberExpressionContinuation(ExpressionT expression);
1119 
1120  ExpressionT ParseArrowFunctionLiteral(const FormalParametersT& parameters);
1121  void ParseAsyncFunctionBody(Scope* scope, StatementListT* body);
1122  ExpressionT ParseAsyncFunctionLiteral();
1123  ExpressionT ParseClassLiteral(IdentifierT name,
1124  Scanner::Location class_name_location,
1125  bool name_is_strict_reserved,
1126  int class_token_pos);
1127  ExpressionT ParseTemplateLiteral(ExpressionT tag, int start, bool tagged);
1128  ExpressionT ParseSuperExpression(bool is_new);
1129  ExpressionT ParseImportExpressions();
1130  ExpressionT ParseNewTargetExpression();
1131 
1132  V8_INLINE void ParseFormalParameter(FormalParametersT* parameters);
1133  void ParseFormalParameterList(FormalParametersT* parameters);
1134  void CheckArityRestrictions(int param_count, FunctionKind function_type,
1135  bool has_rest, int formals_start_pos,
1136  int formals_end_pos);
1137 
1138  BlockT ParseVariableDeclarations(VariableDeclarationContext var_context,
1139  DeclarationParsingResult* parsing_result,
1140  ZonePtrList<const AstRawString>* names);
1141  StatementT ParseAsyncFunctionDeclaration(
1142  ZonePtrList<const AstRawString>* names, bool default_export);
1143  StatementT ParseFunctionDeclaration();
1144  StatementT ParseHoistableDeclaration(ZonePtrList<const AstRawString>* names,
1145  bool default_export);
1146  StatementT ParseHoistableDeclaration(int pos, ParseFunctionFlags flags,
1147  ZonePtrList<const AstRawString>* names,
1148  bool default_export);
1149  StatementT ParseClassDeclaration(ZonePtrList<const AstRawString>* names,
1150  bool default_export);
1151  StatementT ParseNativeDeclaration();
1152 
1153  // Whether we're parsing a single-expression arrow function or something else.
1154  enum class FunctionBodyType { kExpression, kBlock };
1155  // Consumes the ending }.
1156  void ParseFunctionBody(StatementListT* result, IdentifierT function_name,
1157  int pos, const FormalParametersT& parameters,
1158  FunctionKind kind,
1159  FunctionLiteral::FunctionType function_type,
1160  FunctionBodyType body_type);
1161 
1162  // Under some circumstances, we allow preparsing to abort if the preparsed
1163  // function is "long and trivial", and fully parse instead. Our current
1164  // definition of "long and trivial" is:
1165  // - over kLazyParseTrialLimit statements
1166  // - all starting with an identifier (i.e., no if, for, while, etc.)
1167  static const int kLazyParseTrialLimit = 200;
1168 
1169  // TODO(nikolaos, marja): The first argument should not really be passed
1170  // by value. The method is expected to add the parsed statements to the
1171  // list. This works because in the case of the parser, StatementListT is
1172  // a pointer whereas the preparser does not really modify the body.
1173  V8_INLINE void ParseStatementList(StatementListT* body,
1174  Token::Value end_token) {
1175  LazyParsingResult result = ParseStatementList(body, end_token, false);
1176  USE(result);
1177  DCHECK_EQ(result, kLazyParsingComplete);
1178  }
1179  V8_INLINE LazyParsingResult ParseStatementList(StatementListT* body,
1180  Token::Value end_token,
1181  bool may_abort);
1182  StatementT ParseStatementListItem();
1183 
1184  StatementT ParseStatement(ZonePtrList<const AstRawString>* labels,
1185  ZonePtrList<const AstRawString>* own_labels) {
1186  return ParseStatement(labels, own_labels,
1187  kDisallowLabelledFunctionStatement);
1188  }
1189  StatementT ParseStatement(ZonePtrList<const AstRawString>* labels,
1190  ZonePtrList<const AstRawString>* own_labels,
1191  AllowLabelledFunctionStatement allow_function);
1192  BlockT ParseBlock(ZonePtrList<const AstRawString>* labels);
1193 
1194  // Parse a SubStatement in strict mode, or with an extra block scope in
1195  // sloppy mode to handle
1196  // ES#sec-functiondeclarations-in-ifstatement-statement-clauses
1197  StatementT ParseScopedStatement(ZonePtrList<const AstRawString>* labels);
1198 
1199  StatementT ParseVariableStatement(VariableDeclarationContext var_context,
1200  ZonePtrList<const AstRawString>* names);
1201 
1202  // Magical syntax support.
1203  ExpressionT ParseV8Intrinsic();
1204 
1205  StatementT ParseDebuggerStatement();
1206 
1207  StatementT ParseExpressionOrLabelledStatement(
1208  ZonePtrList<const AstRawString>* labels,
1209  ZonePtrList<const AstRawString>* own_labels,
1210  AllowLabelledFunctionStatement allow_function);
1211  StatementT ParseIfStatement(ZonePtrList<const AstRawString>* labels);
1212  StatementT ParseContinueStatement();
1213  StatementT ParseBreakStatement(ZonePtrList<const AstRawString>* labels);
1214  StatementT ParseReturnStatement();
1215  StatementT ParseWithStatement(ZonePtrList<const AstRawString>* labels);
1216  StatementT ParseDoWhileStatement(ZonePtrList<const AstRawString>* labels,
1217  ZonePtrList<const AstRawString>* own_labels);
1218  StatementT ParseWhileStatement(ZonePtrList<const AstRawString>* labels,
1219  ZonePtrList<const AstRawString>* own_labels);
1220  StatementT ParseThrowStatement();
1221  StatementT ParseSwitchStatement(ZonePtrList<const AstRawString>* labels);
1222  V8_INLINE StatementT ParseTryStatement();
1223  StatementT ParseForStatement(ZonePtrList<const AstRawString>* labels,
1224  ZonePtrList<const AstRawString>* own_labels);
1225  StatementT ParseForEachStatementWithDeclarations(
1226  int stmt_pos, ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
1227  ZonePtrList<const AstRawString>* own_labels, Scope* inner_block_scope);
1228  StatementT ParseForEachStatementWithoutDeclarations(
1229  int stmt_pos, ExpressionT expression, int lhs_beg_pos, int lhs_end_pos,
1230  ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
1231  ZonePtrList<const AstRawString>* own_labels);
1232 
1233  // Parse a C-style for loop: 'for (<init>; <cond>; <next>) { ... }'
1234  // "for (<init>;" is assumed to have been parser already.
1235  ForStatementT ParseStandardForLoop(
1236  int stmt_pos, ZonePtrList<const AstRawString>* labels,
1237  ZonePtrList<const AstRawString>* own_labels, ExpressionT* cond,
1238  StatementT* next, StatementT* body);
1239  // Same as the above, but handles those cases where <init> is a
1240  // lexical variable declaration.
1241  StatementT ParseStandardForLoopWithLexicalDeclarations(
1242  int stmt_pos, StatementT init, ForInfo* for_info,
1243  ZonePtrList<const AstRawString>* labels,
1244  ZonePtrList<const AstRawString>* own_labels);
1245  StatementT ParseForAwaitStatement(
1246  ZonePtrList<const AstRawString>* labels,
1247  ZonePtrList<const AstRawString>* own_labels);
1248 
1249  bool IsNextLetKeyword();
1250 
1251  // Checks if the expression is a valid reference expression (e.g., on the
1252  // left-hand side of assignments). Although ruled out by ECMA as early errors,
1253  // we allow calls for web compatibility and rewrite them to a runtime throw.
1254  V8_INLINE ExpressionT
1255  RewriteInvalidReferenceExpression(ExpressionT expression, int beg_pos,
1256  int end_pos, MessageTemplate message);
1257  ExpressionT RewriteInvalidReferenceExpression(ExpressionT expression,
1258  int beg_pos, int end_pos,
1259  MessageTemplate message,
1260  ParseErrorType type);
1261 
1262  bool IsValidReferenceExpression(ExpressionT expression);
1263 
1264  bool IsAssignableIdentifier(ExpressionT expression) {
1265  if (!impl()->IsIdentifier(expression)) return false;
1266  if (is_strict(language_mode()) &&
1267  impl()->IsEvalOrArguments(impl()->AsIdentifier(expression))) {
1268  return false;
1269  }
1270  return true;
1271  }
1272 
1273  // Due to hoisting, the value of a 'var'-declared variable may actually change
1274  // even if the code contains only the "initial" assignment, namely when that
1275  // assignment occurs inside a loop. For example:
1276  //
1277  // let i = 10;
1278  // do { var x = i } while (i--):
1279  //
1280  // As a simple and very conservative approximation of this, we explicitly mark
1281  // as maybe-assigned any non-lexical variable whose initializing "declaration"
1282  // does not syntactically occur in the function scope. (In the example above,
1283  // it occurs in a block scope.)
1284  //
1285  // Note that non-lexical variables include temporaries, which may also get
1286  // assigned inside a loop due to the various rewritings that the parser
1287  // performs.
1288  //
1289  // This also handles marking of loop variables in for-in and for-of loops,
1290  // as determined by declaration_kind.
1291  //
1292  static void MarkLoopVariableAsAssigned(
1293  Scope* scope, Variable* var,
1294  typename DeclarationDescriptor::Kind declaration_kind);
1295 
1296  FunctionKind FunctionKindForImpl(bool is_method, ParseFunctionFlags flags) {
1297  static const FunctionKind kFunctionKinds[][2][2] = {
1298  {
1299  // is_method=false
1300  {// is_generator=false
1301  FunctionKind::kNormalFunction, FunctionKind::kAsyncFunction},
1302  {// is_generator=true
1303  FunctionKind::kGeneratorFunction,
1304  FunctionKind::kAsyncGeneratorFunction},
1305  },
1306  {
1307  // is_method=true
1308  {// is_generator=false
1309  FunctionKind::kConciseMethod, FunctionKind::kAsyncConciseMethod},
1310  {// is_generator=true
1311  FunctionKind::kConciseGeneratorMethod,
1312  FunctionKind::kAsyncConciseGeneratorMethod},
1313  }};
1314  return kFunctionKinds[is_method]
1315  [(flags & ParseFunctionFlag::kIsGenerator) != 0]
1316  [(flags & ParseFunctionFlag::kIsAsync) != 0];
1317  }
1318 
1319  inline FunctionKind FunctionKindFor(ParseFunctionFlags flags) {
1320  const bool kIsMethod = false;
1321  return FunctionKindForImpl(kIsMethod, flags);
1322  }
1323 
1324  inline FunctionKind MethodKindFor(ParseFunctionFlags flags) {
1325  const bool kIsMethod = true;
1326  return FunctionKindForImpl(kIsMethod, flags);
1327  }
1328 
1329  // Keep track of eval() calls since they disable all local variable
1330  // optimizations. This checks if expression is an eval call, and if yes,
1331  // forwards the information to scope.
1332  Call::PossiblyEval CheckPossibleEvalCall(ExpressionT expression,
1333  Scope* scope) {
1334  if (impl()->IsIdentifier(expression) &&
1335  impl()->IsEval(impl()->AsIdentifier(expression))) {
1336  scope->RecordInnerScopeEvalCall();
1337  function_state_->RecordFunctionOrEvalCall();
1338  if (is_sloppy(scope->language_mode())) {
1339  // For sloppy scopes we also have to record the call at function level,
1340  // in case it includes declarations that will be hoisted.
1341  scope->GetDeclarationScope()->RecordEvalCall();
1342  }
1343 
1344  // This call is only necessary to track evals that may be
1345  // inside arrow function parameter lists. In that case,
1346  // Scope::Snapshot::Reparent will move this bit down into
1347  // the arrow function's scope.
1348  scope->RecordEvalCall();
1349 
1350  return Call::IS_POSSIBLY_EVAL;
1351  }
1352  return Call::NOT_EVAL;
1353  }
1354 
1355  // Convenience method which determines the type of return statement to emit
1356  // depending on the current function type.
1357  inline StatementT BuildReturnStatement(ExpressionT expr, int pos,
1358  int end_pos = kNoSourcePosition) {
1359  if (impl()->IsNull(expr)) {
1360  expr = factory()->NewUndefinedLiteral(kNoSourcePosition);
1361  } else if (is_async_generator()) {
1362  // In async generators, if there is an explicit operand to the return
1363  // statement, await the operand.
1364  expr = factory()->NewAwait(expr, kNoSourcePosition);
1365  function_state_->AddSuspend();
1366  }
1367  if (is_async_function()) {
1368  return factory()->NewAsyncReturnStatement(expr, pos, end_pos);
1369  }
1370  return factory()->NewReturnStatement(expr, pos, end_pos);
1371  }
1372 
1373  ModuleDescriptor* module() const {
1374  return scope()->AsModuleScope()->module();
1375  }
1376  Scope* scope() const { return scope_; }
1377 
1378  // Stack of expression classifiers.
1379  // The top of the stack is always pointed to by classifier().
1380  V8_INLINE ExpressionClassifier* classifier() const {
1381  DCHECK_NOT_NULL(classifier_);
1382  return classifier_;
1383  }
1384 
1385  // Accumulates the classifier that is on top of the stack (inner) to
1386  // the one that is right below (outer) and pops the inner.
1387  V8_INLINE void Accumulate(unsigned productions) {
1388  DCHECK_NOT_NULL(classifier_);
1389  ExpressionClassifier* previous = classifier_->previous();
1390  DCHECK_NOT_NULL(previous);
1391  previous->Accumulate(classifier_, productions);
1392  classifier_ = previous;
1393  }
1394 
1395  class AcceptINScope final {
1396  public:
1397  AcceptINScope(ParserBase* parser, bool accept_IN)
1398  : parser_(parser), previous_accept_IN_(parser->accept_IN_) {
1399  parser_->accept_IN_ = accept_IN;
1400  }
1401 
1402  ~AcceptINScope() { parser_->accept_IN_ = previous_accept_IN_; }
1403 
1404  private:
1405  ParserBase* parser_;
1406  bool previous_accept_IN_;
1407  };
1408 
1409  // Accumulate errors that can be arbitrarily deep in an expression.
1410  // These correspond to the ECMAScript spec's 'Contains' operation
1411  // on productions. This includes:
1412  //
1413  // - YieldExpression is disallowed in arrow parameters in a generator.
1414  // - AwaitExpression is disallowed in arrow parameters in an async function.
1415  // - AwaitExpression is disallowed in async arrow parameters.
1416  //
1417  V8_INLINE void AccumulateFormalParameterContainmentErrors() {
1418  Accumulate(ExpressionClassifier::FormalParameterInitializerProduction |
1419  ExpressionClassifier::AsyncArrowFormalParametersProduction);
1420  }
1421 
1422  std::vector<void*>* pointer_buffer() { return &pointer_buffer_; }
1423 
1424  // Parser base's protected field members.
1425 
1426  Scope* scope_; // Scope stack.
1427  Scope* original_scope_; // The top scope for the current parsing item.
1428  FunctionState* function_state_; // Function state stack.
1429  v8::Extension* extension_;
1430  FuncNameInferrer fni_;
1431  AstValueFactory* ast_value_factory_; // Not owned.
1432  typename Types::Factory ast_node_factory_;
1433  RuntimeCallStats* runtime_call_stats_;
1434  internal::Logger* logger_;
1435  bool parsing_on_main_thread_;
1436  const bool parsing_module_;
1437  uintptr_t stack_limit_;
1438  PendingCompilationErrorHandler* pending_error_handler_;
1439 
1440  // Parser base's private field members.
1441 
1442  private:
1443  Zone* zone_;
1444  ExpressionClassifier* classifier_;
1445 
1446  std::vector<void*> pointer_buffer_;
1447 
1448  Scanner* scanner_;
1449 
1450  Scope::Snapshot scope_snapshot_;
1451  // `rewritable_length_`: length of the destructuring_assignments_to_rewrite()
1452  // queue in the parent function state, prior to parsing of formal parameters.
1453  // If the arrow function is lazy, any items added during formal parameter
1454  // parsing are removed from the queue.
1455  int rewritable_length_ = -1;
1456 
1457  int function_literal_id_;
1458  int script_id_;
1459 
1460  FunctionLiteral::EagerCompileHint default_eager_compile_hint_;
1461  FunctionKind next_arrow_function_kind_ = FunctionKind::kArrowFunction;
1462 
1463  bool accept_IN_ = true;
1464 
1465  bool allow_natives_;
1466  bool allow_harmony_public_fields_;
1467  bool allow_harmony_static_fields_;
1468  bool allow_harmony_dynamic_import_;
1469  bool allow_harmony_import_meta_;
1470  bool allow_harmony_private_fields_;
1471  bool allow_harmony_private_methods_;
1472  bool allow_eval_cache_;
1473 };
1474 
1475 template <typename Impl>
1476 ParserBase<Impl>::FunctionState::FunctionState(
1477  FunctionState** function_state_stack, Scope** scope_stack,
1478  DeclarationScope* scope)
1479  : BlockState(scope_stack, scope),
1480  expected_property_count_(0),
1481  suspend_count_(0),
1482  function_state_stack_(function_state_stack),
1483  outer_function_state_(*function_state_stack),
1484  scope_(scope),
1485  destructuring_assignments_to_rewrite_(scope->zone()),
1486  reported_errors_(16, scope->zone()),
1487  dont_optimize_reason_(BailoutReason::kNoReason),
1488  next_function_is_likely_called_(false),
1489  previous_function_was_likely_called_(false),
1490  contains_function_or_eval_(false) {
1491  *function_state_stack = this;
1492  if (outer_function_state_) {
1493  outer_function_state_->previous_function_was_likely_called_ =
1494  outer_function_state_->next_function_is_likely_called_;
1495  outer_function_state_->next_function_is_likely_called_ = false;
1496  }
1497 }
1498 
1499 template <typename Impl>
1500 ParserBase<Impl>::FunctionState::~FunctionState() {
1501  *function_state_stack_ = outer_function_state_;
1502 }
1503 
1504 template <typename Impl>
1505 void ParserBase<Impl>::ReportUnexpectedToken(Token::Value token) {
1506  return ReportUnexpectedTokenAt(scanner_->location(), token);
1507 }
1508 
1509 template <typename Impl>
1510 void ParserBase<Impl>::ReportUnexpectedTokenAt(
1511  Scanner::Location source_location, Token::Value token,
1512  MessageTemplate message) {
1513  const char* arg = nullptr;
1514  impl()->GetUnexpectedTokenMessage(token, &message, &source_location, &arg);
1515  if (Impl::IsPreParser()) {
1516  impl()->ReportUnidentifiableError();
1517  } else {
1518  impl()->ReportMessageAt(source_location, message, arg);
1519  }
1520 }
1521 
1522 template <typename Impl>
1523 typename ParserBase<Impl>::IdentifierT ParserBase<Impl>::ParseIdentifier(
1524  AllowRestrictedIdentifiers allow_restricted_identifiers) {
1525  ExpressionClassifier classifier(this);
1526  auto result = ParseAndClassifyIdentifier();
1527 
1528  if (allow_restricted_identifiers == kDontAllowRestrictedIdentifiers &&
1529  is_strict(language_mode()) && impl()->IsEvalOrArguments(result)) {
1530  impl()->ReportMessageAt(scanner()->location(),
1531  MessageTemplate::kStrictEvalArguments);
1532  }
1533 
1534  return result;
1535 }
1536 
1537 template <typename Impl>
1538 typename ParserBase<Impl>::IdentifierT
1539 ParserBase<Impl>::ParseAndClassifyIdentifier() {
1540  Token::Value next = Next();
1541  STATIC_ASSERT(Token::IDENTIFIER + 1 == Token::ASYNC);
1542  if (IsInRange(next, Token::IDENTIFIER, Token::ASYNC)) {
1543  IdentifierT name = impl()->GetSymbol();
1544 
1545  // When this function is used to read a formal parameter, we don't always
1546  // know whether the function is going to be strict or sloppy. Indeed for
1547  // arrow functions we don't always know that the identifier we are reading
1548  // is actually a formal parameter. Therefore besides the errors that we
1549  // must detect because we know we're in strict mode, we also record any
1550  // error that we might make in the future once we know the language mode.
1551  if (V8_UNLIKELY(impl()->IsEvalOrArguments(name))) {
1552  if (impl()->IsArguments(name) && scope()->ShouldBanArguments()) {
1553  ReportMessage(MessageTemplate::kArgumentsDisallowedInInitializer);
1554  return impl()->EmptyIdentifierString();
1555  }
1556 
1557  classifier()->RecordStrictModeFormalParameterError(
1558  scanner()->location(), MessageTemplate::kStrictEvalArguments);
1559  }
1560 
1561  return name;
1562  } else if (next == Token::AWAIT && !parsing_module_ && !is_async_function()) {
1563  classifier()->RecordAsyncArrowFormalParametersError(
1564  scanner()->location(), MessageTemplate::kAwaitBindingIdentifier);
1565  return impl()->GetSymbol();
1566  } else if (is_sloppy(language_mode()) &&
1567  (Token::IsStrictReservedWord(next) ||
1568  (next == Token::YIELD && !is_generator()))) {
1569  IdentifierT name = impl()->GetSymbol();
1570  classifier()->RecordStrictModeFormalParameterError(
1571  scanner()->location(), MessageTemplate::kUnexpectedStrictReserved);
1572  if (impl()->IdentifierEquals(name, ast_value_factory()->let_string())) {
1573  classifier()->RecordLetPatternError(
1574  scanner()->location(), MessageTemplate::kLetInLexicalBinding);
1575  }
1576  return name;
1577  } else {
1578  ReportUnexpectedToken(next);
1579  return impl()->EmptyIdentifierString();
1580  }
1581 }
1582 
1583 template <class Impl>
1584 typename ParserBase<Impl>::IdentifierT
1585 ParserBase<Impl>::ParseIdentifierOrStrictReservedWord(
1586  FunctionKind function_kind, bool* is_strict_reserved, bool* is_await) {
1587  Token::Value next = Next();
1588  if (next == Token::IDENTIFIER || (next == Token::AWAIT && !parsing_module_ &&
1589  !IsAsyncFunction(function_kind)) ||
1590  next == Token::ASYNC) {
1591  *is_strict_reserved = false;
1592  *is_await = next == Token::AWAIT;
1593  } else if (Token::IsStrictReservedWord(next) ||
1594  (next == Token::YIELD && !IsGeneratorFunction(function_kind))) {
1595  *is_strict_reserved = true;
1596  } else {
1597  ReportUnexpectedToken(next);
1598  return impl()->EmptyIdentifierString();
1599  }
1600 
1601  return impl()->GetSymbol();
1602 }
1603 
1604 template <typename Impl>
1605 typename ParserBase<Impl>::IdentifierT ParserBase<Impl>::ParseIdentifierName() {
1606  Token::Value next = Next();
1607  if (!Token::IsAnyIdentifier(next) && next != Token::ESCAPED_KEYWORD &&
1608  !Token::IsKeyword(next)) {
1609  ReportUnexpectedToken(next);
1610  return impl()->EmptyIdentifierString();
1611  }
1612 
1613  return impl()->GetSymbol();
1614 }
1615 
1616 template <typename Impl>
1617 typename ParserBase<Impl>::ExpressionT
1618 ParserBase<Impl>::ParseIdentifierNameOrPrivateName() {
1619  int pos = position();
1620  IdentifierT name;
1621  ExpressionT key;
1622  if (allow_harmony_private_fields() && Check(Token::PRIVATE_NAME)) {
1623  name = impl()->GetSymbol();
1624  auto key_proxy =
1625  impl()->ExpressionFromIdentifier(name, pos, InferName::kNo);
1626  key_proxy->set_is_private_name();
1627  key = key_proxy;
1628  } else {
1629  name = ParseIdentifierName();
1630  key = factory()->NewStringLiteral(name, pos);
1631  }
1632  impl()->PushLiteralName(name);
1633  return key;
1634 }
1635 
1636 template <typename Impl>
1637 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseRegExpLiteral() {
1638  int pos = peek_position();
1639  if (!scanner()->ScanRegExpPattern()) {
1640  Next();
1641  ReportMessage(MessageTemplate::kUnterminatedRegExp);
1642  return impl()->FailureExpression();
1643  }
1644 
1645  IdentifierT js_pattern = impl()->GetNextSymbol();
1646  Maybe<RegExp::Flags> flags = scanner()->ScanRegExpFlags();
1647  if (flags.IsNothing()) {
1648  Next();
1649  ReportMessage(MessageTemplate::kMalformedRegExpFlags);
1650  return impl()->FailureExpression();
1651  }
1652  int js_flags = flags.FromJust();
1653  Next();
1654  return factory()->NewRegExpLiteral(js_pattern, js_flags, pos);
1655 }
1656 
1657 template <typename Impl>
1658 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseBindingPattern() {
1659  // Pattern ::
1660  // Identifier
1661  // ArrayLiteral
1662  // ObjectLiteral
1663 
1664  int beg_pos = peek_position();
1665  Token::Value token = peek();
1666  ExpressionT result;
1667 
1668  if (Token::IsAnyIdentifier(token)) {
1669  IdentifierT name = ParseAndClassifyIdentifier();
1670  if (V8_UNLIKELY(is_strict(language_mode()) &&
1671  impl()->IsEvalOrArguments(name))) {
1672  impl()->ReportMessageAt(scanner()->location(),
1673  MessageTemplate::kStrictEvalArguments);
1674  return impl()->FailureExpression();
1675  }
1676  return impl()->ExpressionFromIdentifier(name, beg_pos);
1677  }
1678 
1679  CheckStackOverflow();
1680  classifier()->RecordNonSimpleParameter();
1681 
1682  if (token == Token::LBRACK) {
1683  result = ParseArrayLiteral();
1684  } else if (token == Token::LBRACE) {
1685  result = ParseObjectLiteral();
1686  } else {
1687  ReportUnexpectedToken(Next());
1688  return impl()->FailureExpression();
1689  }
1690 
1691  ValidateBindingPattern();
1692  return result;
1693 }
1694 
1695 template <typename Impl>
1696 typename ParserBase<Impl>::ExpressionT
1697 ParserBase<Impl>::ParsePrimaryExpression() {
1698  CheckStackOverflow();
1699 
1700  // PrimaryExpression ::
1701  // 'this'
1702  // 'null'
1703  // 'true'
1704  // 'false'
1705  // Identifier
1706  // Number
1707  // String
1708  // ArrayLiteral
1709  // ObjectLiteral
1710  // RegExpLiteral
1711  // ClassLiteral
1712  // '(' Expression ')'
1713  // TemplateLiteral
1714  // do Block
1715  // AsyncFunctionLiteral
1716 
1717  int beg_pos = peek_position();
1718  Token::Value token = peek();
1719 
1720  if (IsInRange(token, Token::IDENTIFIER,
1721  Token::ESCAPED_STRICT_RESERVED_WORD)) {
1722  // Using eval or arguments in this context is OK even in strict mode.
1723  IdentifierT name = ParseAndClassifyIdentifier();
1724  InferName infer = InferName::kYes;
1725  if (V8_UNLIKELY(impl()->IsAsync(name) &&
1726  !scanner()->HasLineTerminatorBeforeNext())) {
1727  if (peek() == Token::FUNCTION) {
1728  return ParseAsyncFunctionLiteral();
1729  }
1730  // async Identifier => AsyncConciseBody
1731  if (peek_any_identifier() && PeekAhead() == Token::ARROW) {
1732  name = ParseAndClassifyIdentifier();
1733 
1734  if (!classifier()->is_valid_async_arrow_formal_parameters()) {
1735  ReportClassifierError(
1736  classifier()->async_arrow_formal_parameters_error());
1737  return impl()->FailureExpression();
1738  }
1739 
1740  next_arrow_function_kind_ = FunctionKind::kAsyncArrowFunction;
1741  infer = InferName::kNo;
1742  }
1743  }
1744 
1745  if (peek() == Token::ARROW) {
1746  scope_snapshot_ = std::move(Scope::Snapshot(scope()));
1747  rewritable_length_ = static_cast<int>(
1748  function_state_->destructuring_assignments_to_rewrite().size());
1749  }
1750  return impl()->ExpressionFromIdentifier(name, beg_pos, infer);
1751  }
1752  DCHECK_IMPLIES(Token::IsAnyIdentifier(token), token == Token::ENUM);
1753 
1754  if (Token::IsLiteral(token)) {
1755  return impl()->ExpressionFromLiteral(Next(), beg_pos);
1756  }
1757 
1758  switch (token) {
1759  case Token::THIS: {
1760  Consume(Token::THIS);
1761  return impl()->ThisExpression(beg_pos);
1762  }
1763 
1764  case Token::ASSIGN_DIV:
1765  case Token::DIV:
1766  return ParseRegExpLiteral();
1767 
1768  case Token::LBRACK:
1769  return ParseArrayLiteral();
1770 
1771  case Token::LBRACE:
1772  return ParseObjectLiteral();
1773 
1774  case Token::LPAREN: {
1775  Consume(Token::LPAREN);
1776  Scope::Snapshot scope_snapshot(scope());
1777  int rewritable_length = static_cast<int>(
1778  function_state_->destructuring_assignments_to_rewrite().size());
1779  if (Check(Token::RPAREN)) {
1780  // ()=>x. The continuation that consumes the => is in
1781  // ParseAssignmentExpression.
1782  if (peek() != Token::ARROW) ReportUnexpectedToken(Token::RPAREN);
1783  scope_snapshot_ = std::move(scope_snapshot);
1784  rewritable_length_ = rewritable_length;
1785  return factory()->NewEmptyParentheses(beg_pos);
1786  }
1787  // Heuristically try to detect immediately called functions before
1788  // seeing the call parentheses.
1789  if (peek() == Token::FUNCTION ||
1790  (peek() == Token::ASYNC && PeekAhead() == Token::FUNCTION)) {
1791  function_state_->set_next_function_is_likely_called();
1792  }
1793  AcceptINScope scope(this, true);
1794  ExpressionT expr = ParseExpressionCoverGrammar();
1795  expr->mark_parenthesized();
1796  Expect(Token::RPAREN);
1797 
1798  if (peek() == Token::ARROW) {
1799  scope_snapshot_ = std::move(scope_snapshot);
1800  rewritable_length_ = rewritable_length;
1801  }
1802 
1803  return expr;
1804  }
1805 
1806  case Token::CLASS: {
1807  Consume(Token::CLASS);
1808  int class_token_pos = position();
1809  IdentifierT name = impl()->NullIdentifier();
1810  bool is_strict_reserved_name = false;
1811  Scanner::Location class_name_location = Scanner::Location::invalid();
1812  if (peek_any_identifier()) {
1813  bool is_await = false;
1814  name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
1815  &is_await);
1816  class_name_location = scanner()->location();
1817  if (is_await) {
1818  classifier()->RecordAsyncArrowFormalParametersError(
1819  scanner()->location(), MessageTemplate::kAwaitBindingIdentifier);
1820  }
1821  }
1822  return ParseClassLiteral(name, class_name_location,
1823  is_strict_reserved_name, class_token_pos);
1824  }
1825 
1826  case Token::TEMPLATE_SPAN:
1827  case Token::TEMPLATE_TAIL:
1828  return ParseTemplateLiteral(impl()->NullExpression(), beg_pos, false);
1829 
1830  case Token::MOD:
1831  if (allow_natives() || extension_ != nullptr) {
1832  return ParseV8Intrinsic();
1833  }
1834  break;
1835 
1836  default:
1837  break;
1838  }
1839 
1840  ReportUnexpectedToken(Next());
1841  return impl()->FailureExpression();
1842 }
1843 
1844 template <typename Impl>
1845 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseExpression() {
1846  ExpressionClassifier classifier(this);
1847  AcceptINScope scope(this, true);
1848  ExpressionT result = ParseExpressionCoverGrammar();
1849  ValidateExpression();
1850  return result;
1851 }
1852 
1853 template <typename Impl>
1854 typename ParserBase<Impl>::ExpressionT
1855 ParserBase<Impl>::ParseExpressionCoverGrammar() {
1856  // Expression ::
1857  // AssignmentExpression
1858  // Expression ',' AssignmentExpression
1859 
1860  ExpressionListT list(pointer_buffer());
1861  ExpressionT expression;
1862  while (true) {
1863  if (V8_UNLIKELY(peek() == Token::ELLIPSIS)) {
1864  return ParseArrowFormalsWithRest(&list);
1865  }
1866 
1867  expression = ParseAssignmentExpression();
1868  CheckArrowFormalParameter(expression);
1869  list.Add(expression);
1870 
1871  if (!Check(Token::COMMA)) break;
1872 
1873  if (peek() == Token::RPAREN && PeekAhead() == Token::ARROW) {
1874  // a trailing comma is allowed at the end of an arrow parameter list
1875  break;
1876  }
1877 
1878  // Pass on the 'set_next_function_is_likely_called' flag if we have
1879  // several function literals separated by comma.
1880  if (peek() == Token::FUNCTION &&
1881  function_state_->previous_function_was_likely_called()) {
1882  function_state_->set_next_function_is_likely_called();
1883  }
1884  }
1885 
1886  // Return the single element if the list is empty. We need to do this because
1887  // callers of this function care about the type of the result if there was
1888  // only a single assignment expression. The preparser would lose this
1889  // information otherwise.
1890  if (list.length() == 1) return expression;
1891  return impl()->ExpressionListToExpression(list);
1892 }
1893 
1894 template <typename Impl>
1895 typename ParserBase<Impl>::ExpressionT
1896 ParserBase<Impl>::ParseArrowFormalsWithRest(
1897  typename ParserBase<Impl>::ExpressionListT* list) {
1898  Consume(Token::ELLIPSIS);
1899 
1900  Scanner::Location ellipsis = scanner()->location();
1901  int pattern_pos = peek_position();
1902  ExpressionT pattern = ParseBindingPattern();
1903 
1904  classifier()->RecordNonSimpleParameter();
1905 
1906  if (V8_UNLIKELY(peek() == Token::ASSIGN)) {
1907  ReportMessage(MessageTemplate::kRestDefaultInitializer);
1908  return impl()->FailureExpression();
1909  }
1910 
1911  ExpressionT spread =
1912  factory()->NewSpread(pattern, ellipsis.beg_pos, pattern_pos);
1913  if (V8_UNLIKELY(peek() == Token::COMMA)) {
1914  ReportMessage(MessageTemplate::kParamAfterRest);
1915  return impl()->FailureExpression();
1916  }
1917 
1918  // 'x, y, ...z' in CoverParenthesizedExpressionAndArrowParameterList only
1919  // as the formal parameters of'(x, y, ...z) => foo', and is not itself a
1920  // valid expression.
1921  if (peek() != Token::RPAREN || PeekAhead() != Token::ARROW) {
1922  ReportUnexpectedTokenAt(ellipsis, Token::ELLIPSIS);
1923  return impl()->FailureExpression();
1924  }
1925 
1926  list->Add(spread);
1927  return impl()->ExpressionListToExpression(*list);
1928 }
1929 
1930 template <typename Impl>
1931 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseArrayLiteral() {
1932  // ArrayLiteral ::
1933  // '[' Expression? (',' Expression?)* ']'
1934 
1935  int pos = peek_position();
1936  ExpressionListT values(pointer_buffer());
1937  int first_spread_index = -1;
1938  Consume(Token::LBRACK);
1939  while (!Check(Token::RBRACK)) {
1940  ExpressionT elem;
1941  if (peek() == Token::COMMA) {
1942  elem = factory()->NewTheHoleLiteral();
1943  } else if (Check(Token::ELLIPSIS)) {
1944  int start_pos = position();
1945  int expr_pos = peek_position();
1946  AcceptINScope scope(this, true);
1947  ExpressionT argument = ParseAssignmentExpression();
1948  elem = factory()->NewSpread(argument, start_pos, expr_pos);
1949 
1950  if (first_spread_index < 0) {
1951  first_spread_index = values.length();
1952  }
1953 
1954  if (argument->IsAssignment()) {
1955  classifier()->RecordPatternError(
1956  Scanner::Location(start_pos, end_position()),
1957  MessageTemplate::kInvalidDestructuringTarget);
1958  } else {
1959  CheckDestructuringElement(argument, start_pos, end_position());
1960  }
1961 
1962  if (peek() == Token::COMMA) {
1963  classifier()->RecordPatternError(
1964  Scanner::Location(start_pos, end_position()),
1965  MessageTemplate::kElementAfterRest);
1966  }
1967  } else {
1968  int beg_pos = peek_position();
1969  AcceptINScope scope(this, true);
1970  elem = ParseAssignmentExpression();
1971  CheckDestructuringElement(elem, beg_pos, end_position());
1972  }
1973  values.Add(elem);
1974  if (peek() != Token::RBRACK) {
1975  Expect(Token::COMMA);
1976  if (elem->IsFailureExpression()) return elem;
1977  }
1978  }
1979 
1980  return factory()->NewArrayLiteral(values, first_spread_index, pos);
1981 }
1982 
1983 template <class Impl>
1984 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParsePropertyName(
1985  ParsePropertyInfo* prop_info) {
1986  DCHECK_EQ(prop_info->kind, ParsePropertyKind::kNotSet);
1987  DCHECK_EQ(prop_info->function_flags, ParseFunctionFlag::kIsNormal);
1988  DCHECK(!prop_info->is_computed_name);
1989 
1990  if (Check(Token::ASYNC)) {
1991  Token::Value token = peek();
1992  if ((token != Token::MUL && prop_info->ParsePropertyKindFromToken(token)) ||
1993  scanner()->HasLineTerminatorBeforeNext()) {
1994  prop_info->name = impl()->GetSymbol();
1995  impl()->PushLiteralName(prop_info->name);
1996  return factory()->NewStringLiteral(prop_info->name, position());
1997  }
1998  prop_info->function_flags = ParseFunctionFlag::kIsAsync;
1999  prop_info->kind = ParsePropertyKind::kMethod;
2000  }
2001 
2002  if (Check(Token::MUL)) {
2003  prop_info->function_flags |= ParseFunctionFlag::kIsGenerator;
2004  prop_info->kind = ParsePropertyKind::kMethod;
2005  }
2006 
2007  if (prop_info->kind == ParsePropertyKind::kNotSet &&
2008  Check(Token::IDENTIFIER)) {
2009  IdentifierT symbol = impl()->GetSymbol();
2010  if (!prop_info->ParsePropertyKindFromToken(peek())) {
2011  if (impl()->IdentifierEquals(symbol, ast_value_factory()->get_string())) {
2012  prop_info->kind = ParsePropertyKind::kAccessorGetter;
2013  } else if (impl()->IdentifierEquals(symbol,
2014  ast_value_factory()->set_string())) {
2015  prop_info->kind = ParsePropertyKind::kAccessorSetter;
2016  }
2017  }
2018  if (!IsAccessor(prop_info->kind)) {
2019  prop_info->name = symbol;
2020  impl()->PushLiteralName(prop_info->name);
2021  return factory()->NewStringLiteral(prop_info->name, position());
2022  }
2023  }
2024 
2025  int pos = peek_position();
2026 
2027  // For non computed property names we normalize the name a bit:
2028  //
2029  // "12" -> 12
2030  // 12.3 -> "12.3"
2031  // 12.30 -> "12.3"
2032  // identifier -> "identifier"
2033  //
2034  // This is important because we use the property name as a key in a hash
2035  // table when we compute constant properties.
2036  bool is_array_index;
2037  uint32_t index;
2038  switch (peek()) {
2039  case Token::PRIVATE_NAME:
2040  prop_info->is_private = true;
2041  is_array_index = false;
2042  Consume(Token::PRIVATE_NAME);
2043  if (prop_info->kind == ParsePropertyKind::kNotSet) {
2044  prop_info->ParsePropertyKindFromToken(peek());
2045  }
2046  prop_info->name = impl()->GetSymbol();
2047  if (prop_info->position == PropertyPosition::kObjectLiteral ||
2048  prop_info->is_static ||
2049  (!allow_harmony_private_methods() &&
2050  (IsAccessor(prop_info->kind) ||
2051  prop_info->kind == ParsePropertyKind::kMethod))) {
2052  ReportUnexpectedToken(Next());
2053  return impl()->FailureExpression();
2054  }
2055  break;
2056 
2057  case Token::STRING:
2058  Consume(Token::STRING);
2059  prop_info->name = impl()->GetSymbol();
2060  is_array_index = impl()->IsArrayIndex(prop_info->name, &index);
2061  break;
2062 
2063  case Token::SMI:
2064  Consume(Token::SMI);
2065  index = scanner()->smi_value();
2066  is_array_index = true;
2067  // Token::SMI were scanned from their canonical representation.
2068  prop_info->name = impl()->GetSymbol();
2069  break;
2070 
2071  case Token::NUMBER: {
2072  Consume(Token::NUMBER);
2073  prop_info->name = impl()->GetNumberAsSymbol();
2074  is_array_index = impl()->IsArrayIndex(prop_info->name, &index);
2075  break;
2076  }
2077  case Token::LBRACK: {
2078  prop_info->name = impl()->NullIdentifier();
2079  prop_info->is_computed_name = true;
2080  Consume(Token::LBRACK);
2081  ExpressionClassifier computed_name_classifier(this);
2082  AcceptINScope scope(this, true);
2083  ExpressionT expression = ParseAssignmentExpression();
2084  ValidateExpression();
2085  AccumulateFormalParameterContainmentErrors();
2086  Expect(Token::RBRACK);
2087  if (prop_info->kind == ParsePropertyKind::kNotSet) {
2088  prop_info->ParsePropertyKindFromToken(peek());
2089  }
2090  return expression;
2091  }
2092 
2093  case Token::ELLIPSIS:
2094  if (prop_info->kind == ParsePropertyKind::kNotSet) {
2095  prop_info->name = impl()->NullIdentifier();
2096  Consume(Token::ELLIPSIS);
2097  AcceptINScope scope(this, true);
2098  ExpressionT expression = ParseAssignmentExpression();
2099  prop_info->kind = ParsePropertyKind::kSpread;
2100 
2101  CheckDestructuringElement(expression, expression->position(),
2102  end_position());
2103  if (!IsValidReferenceExpression(expression)) {
2104  classifier()->RecordBindingPatternError(
2105  Scanner::Location(expression->position(), end_position()),
2106  MessageTemplate::kInvalidRestBindingPattern);
2107  classifier()->RecordPatternError(
2108  Scanner::Location(expression->position(), end_position()),
2109  MessageTemplate::kInvalidRestAssignmentPattern);
2110  }
2111 
2112  if (peek() != Token::RBRACE) {
2113  classifier()->RecordPatternError(scanner()->location(),
2114  MessageTemplate::kElementAfterRest);
2115  }
2116  return expression;
2117  }
2118  V8_FALLTHROUGH;
2119 
2120  default:
2121  prop_info->name = ParseIdentifierName();
2122  is_array_index = false;
2123  break;
2124  }
2125 
2126  if (prop_info->kind == ParsePropertyKind::kNotSet) {
2127  prop_info->ParsePropertyKindFromToken(peek());
2128  }
2129  impl()->PushLiteralName(prop_info->name);
2130  return is_array_index ? factory()->NewNumberLiteral(index, pos)
2131  : factory()->NewStringLiteral(prop_info->name, pos);
2132 }
2133 
2134 template <typename Impl>
2135 typename ParserBase<Impl>::ClassLiteralPropertyT
2136 ParserBase<Impl>::ParseClassPropertyDefinition(ClassInfo* class_info,
2137  ParsePropertyInfo* prop_info,
2138  bool has_extends) {
2139  DCHECK_NOT_NULL(class_info);
2140  DCHECK_EQ(prop_info->position, PropertyPosition::kClassLiteral);
2141 
2142  Token::Value name_token = peek();
2143  DCHECK_IMPLIES(name_token == Token::PRIVATE_NAME,
2144  allow_harmony_private_fields());
2145 
2146  int property_beg_pos = scanner()->peek_location().beg_pos;
2147  int name_token_position = property_beg_pos;
2148  ExpressionT name_expression;
2149  if (name_token == Token::STATIC) {
2150  Consume(Token::STATIC);
2151  name_token_position = scanner()->peek_location().beg_pos;
2152  if (peek() == Token::LPAREN) {
2153  prop_info->kind = ParsePropertyKind::kMethod;
2154  // TODO(bakkot) specialize on 'static'
2155  prop_info->name = impl()->GetSymbol();
2156  name_expression =
2157  factory()->NewStringLiteral(prop_info->name, position());
2158  } else if (peek() == Token::ASSIGN || peek() == Token::SEMICOLON ||
2159  peek() == Token::RBRACE) {
2160  // TODO(bakkot) specialize on 'static'
2161  prop_info->name = impl()->GetSymbol();
2162  name_expression =
2163  factory()->NewStringLiteral(prop_info->name, position());
2164  } else if (peek() == Token::PRIVATE_NAME) {
2165  // TODO(gsathya): Make a better error message for this.
2166  ReportUnexpectedToken(Next());
2167  return impl()->NullLiteralProperty();
2168  } else {
2169  prop_info->is_static = true;
2170  name_expression = ParsePropertyName(prop_info);
2171  }
2172  } else {
2173  name_expression = ParsePropertyName(prop_info);
2174  }
2175 
2176  if (!class_info->has_name_static_property && prop_info->is_static &&
2177  impl()->IsName(prop_info->name)) {
2178  class_info->has_name_static_property = true;
2179  }
2180 
2181  switch (prop_info->kind) {
2182  case ParsePropertyKind::kAssign:
2183  case ParsePropertyKind::kClassField:
2184  case ParsePropertyKind::kShorthandOrClassField:
2185  case ParsePropertyKind::kNotSet: // This case is a name followed by a name
2186  // or other property. Here we have to
2187  // assume that's an uninitialized field
2188  // followed by a linebreak followed by a
2189  // property, with ASI adding the
2190  // semicolon. If not, there will be a
2191  // syntax error after parsing the first
2192  // name as an uninitialized field.
2193  if (allow_harmony_public_fields() || allow_harmony_private_fields()) {
2194  prop_info->kind = ParsePropertyKind::kClassField;
2195  prop_info->is_private = name_token == Token::PRIVATE_NAME;
2196  if (prop_info->is_static && !allow_harmony_static_fields()) {
2197  ReportUnexpectedToken(Next());
2198  return impl()->NullLiteralProperty();
2199  }
2200  if (!prop_info->is_computed_name) {
2201  CheckClassFieldName(prop_info->name, prop_info->is_static);
2202  }
2203  ExpressionT initializer = ParseMemberInitializer(
2204  class_info, property_beg_pos, prop_info->is_static);
2205  ExpectSemicolon();
2206  ClassLiteralPropertyT result = factory()->NewClassLiteralProperty(
2207  name_expression, initializer, ClassLiteralProperty::FIELD,
2208  prop_info->is_static, prop_info->is_computed_name,
2209  prop_info->is_private);
2210  impl()->SetFunctionNameFromPropertyName(result, prop_info->name);
2211  return result;
2212 
2213  } else {
2214  ReportUnexpectedToken(Next());
2215  return impl()->NullLiteralProperty();
2216  }
2217 
2218  case ParsePropertyKind::kMethod: {
2219  // MethodDefinition
2220  // PropertyName '(' StrictFormalParameters ')' '{' FunctionBody '}'
2221  // '*' PropertyName '(' StrictFormalParameters ')' '{' FunctionBody '}'
2222  // async PropertyName '(' StrictFormalParameters ')'
2223  // '{' FunctionBody '}'
2224  // async '*' PropertyName '(' StrictFormalParameters ')'
2225  // '{' FunctionBody '}'
2226 
2227  if (!prop_info->is_computed_name) {
2228  CheckClassMethodName(prop_info->name, ParsePropertyKind::kMethod,
2229  prop_info->function_flags, prop_info->is_static,
2230  &class_info->has_seen_constructor);
2231  }
2232 
2233  FunctionKind kind = MethodKindFor(prop_info->function_flags);
2234 
2235  if (!prop_info->is_static && impl()->IsConstructor(prop_info->name)) {
2236  class_info->has_seen_constructor = true;
2237  kind = has_extends ? FunctionKind::kDerivedConstructor
2238  : FunctionKind::kBaseConstructor;
2239  }
2240 
2241  ExpressionT value = impl()->ParseFunctionLiteral(
2242  prop_info->name, scanner()->location(), kSkipFunctionNameCheck, kind,
2243  name_token_position, FunctionLiteral::kAccessorOrMethod,
2244  language_mode(), nullptr);
2245 
2246  ClassLiteralPropertyT result = factory()->NewClassLiteralProperty(
2247  name_expression, value, ClassLiteralProperty::METHOD,
2248  prop_info->is_static, prop_info->is_computed_name,
2249  prop_info->is_private);
2250  impl()->SetFunctionNameFromPropertyName(result, prop_info->name);
2251  return result;
2252  }
2253 
2254  case ParsePropertyKind::kAccessorGetter:
2255  case ParsePropertyKind::kAccessorSetter: {
2256  DCHECK_EQ(prop_info->function_flags, ParseFunctionFlag::kIsNormal);
2257  bool is_get = prop_info->kind == ParsePropertyKind::kAccessorGetter;
2258 
2259  if (!prop_info->is_computed_name) {
2260  CheckClassMethodName(prop_info->name, prop_info->kind,
2261  ParseFunctionFlag::kIsNormal, prop_info->is_static,
2262  &class_info->has_seen_constructor);
2263  // Make sure the name expression is a string since we need a Name for
2264  // Runtime_DefineAccessorPropertyUnchecked and since we can determine
2265  // this statically we can skip the extra runtime check.
2266  name_expression = factory()->NewStringLiteral(
2267  prop_info->name, name_expression->position());
2268  }
2269 
2270  FunctionKind kind = is_get ? FunctionKind::kGetterFunction
2271  : FunctionKind::kSetterFunction;
2272 
2273  FunctionLiteralT value = impl()->ParseFunctionLiteral(
2274  prop_info->name, scanner()->location(), kSkipFunctionNameCheck, kind,
2275  name_token_position, FunctionLiteral::kAccessorOrMethod,
2276  language_mode(), nullptr);
2277 
2278  ClassLiteralProperty::Kind property_kind =
2279  is_get ? ClassLiteralProperty::GETTER : ClassLiteralProperty::SETTER;
2280  ClassLiteralPropertyT result = factory()->NewClassLiteralProperty(
2281  name_expression, value, property_kind, prop_info->is_static,
2282  prop_info->is_computed_name, prop_info->is_private);
2283  const AstRawString* prefix =
2284  is_get ? ast_value_factory()->get_space_string()
2285  : ast_value_factory()->set_space_string();
2286  impl()->SetFunctionNameFromPropertyName(result, prop_info->name, prefix);
2287  return result;
2288  }
2289  case ParsePropertyKind::kValue:
2290  case ParsePropertyKind::kShorthand:
2291  case ParsePropertyKind::kSpread:
2292  ReportUnexpectedTokenAt(
2293  Scanner::Location(name_token_position, name_expression->position()),
2294  name_token);
2295  return impl()->NullLiteralProperty();
2296  }
2297  UNREACHABLE();
2298 }
2299 
2300 template <typename Impl>
2301 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseMemberInitializer(
2302  ClassInfo* class_info, int beg_pos, bool is_static) {
2303  DeclarationScope* initializer_scope =
2304  is_static ? class_info->static_fields_scope
2305  : class_info->instance_members_scope;
2306 
2307  if (initializer_scope == nullptr) {
2308  initializer_scope =
2309  NewFunctionScope(FunctionKind::kClassMembersInitializerFunction);
2310  // TODO(gsathya): Make scopes be non contiguous.
2311  initializer_scope->set_start_position(beg_pos);
2312  initializer_scope->SetLanguageMode(LanguageMode::kStrict);
2313  }
2314 
2315  ExpressionT initializer;
2316  if (Check(Token::ASSIGN)) {
2317  FunctionState initializer_state(&function_state_, &scope_,
2318  initializer_scope);
2319  ExpressionClassifier expression_classifier(this);
2320 
2321  AcceptINScope scope(this, true);
2322  initializer = ParseAssignmentExpression();
2323  ValidateExpression();
2324 
2325  // TODO(gsathya): In the future, this could be changed to be
2326  // called once for all the class field initializers, instead of
2327  // rewriting after each class field initializer, improving
2328  // performance.
2329  impl()->RewriteDestructuringAssignments();
2330  } else {
2331  initializer = factory()->NewUndefinedLiteral(kNoSourcePosition);
2332  }
2333 
2334  initializer_scope->set_end_position(end_position());
2335  if (is_static) {
2336  class_info->static_fields_scope = initializer_scope;
2337  class_info->has_static_class_fields = true;
2338  } else {
2339  class_info->instance_members_scope = initializer_scope;
2340  class_info->has_instance_members = true;
2341  }
2342 
2343  return initializer;
2344 }
2345 
2346 template <typename Impl>
2347 typename ParserBase<Impl>::ObjectLiteralPropertyT
2348 ParserBase<Impl>::ParseObjectPropertyDefinition(ParsePropertyInfo* prop_info,
2349  bool* has_seen_proto) {
2350  DCHECK_EQ(prop_info->position, PropertyPosition::kObjectLiteral);
2351  Token::Value name_token = peek();
2352  Scanner::Location next_loc = scanner()->peek_location();
2353 
2354  ExpressionT name_expression = ParsePropertyName(prop_info);
2355  IdentifierT name = prop_info->name;
2356  ParseFunctionFlags function_flags = prop_info->function_flags;
2357  ParsePropertyKind kind = prop_info->kind;
2358 
2359  switch (prop_info->kind) {
2360  case ParsePropertyKind::kSpread:
2361  DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2362  DCHECK(!prop_info->is_computed_name);
2363  DCHECK_EQ(Token::ELLIPSIS, name_token);
2364 
2365  prop_info->is_computed_name = true;
2366  prop_info->is_rest = true;
2367 
2368  return factory()->NewObjectLiteralProperty(
2369  factory()->NewTheHoleLiteral(), name_expression,
2370  ObjectLiteralProperty::SPREAD, true);
2371 
2372  case ParsePropertyKind::kValue: {
2373  DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2374 
2375  if (!prop_info->is_computed_name &&
2376  impl()->IdentifierEquals(name, ast_value_factory()->proto_string())) {
2377  if (*has_seen_proto) {
2378  classifier()->RecordExpressionError(scanner()->location(),
2379  MessageTemplate::kDuplicateProto);
2380  }
2381  *has_seen_proto = true;
2382  }
2383  Consume(Token::COLON);
2384  int beg_pos = peek_position();
2385  AcceptINScope scope(this, true);
2386  ExpressionT value = ParseAssignmentExpression();
2387  CheckDestructuringElement(value, beg_pos, end_position());
2388 
2389  ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2390  name_expression, value, prop_info->is_computed_name);
2391  impl()->SetFunctionNameFromPropertyName(result, name);
2392  return result;
2393  }
2394 
2395  case ParsePropertyKind::kAssign:
2396  case ParsePropertyKind::kShorthandOrClassField:
2397  case ParsePropertyKind::kShorthand: {
2398  // PropertyDefinition
2399  // IdentifierReference
2400  // CoverInitializedName
2401  //
2402  // CoverInitializedName
2403  // IdentifierReference Initializer?
2404  DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2405 
2406  if (!Token::IsIdentifier(name_token, language_mode(),
2407  this->is_generator(),
2408  parsing_module_ || is_async_function())) {
2409  ReportUnexpectedToken(Next());
2410  return impl()->NullLiteralProperty();
2411  }
2412 
2413  DCHECK(!prop_info->is_computed_name);
2414 
2415  if (name_token == Token::LET) {
2416  classifier()->RecordLetPatternError(
2417  scanner()->location(), MessageTemplate::kLetInLexicalBinding);
2418  }
2419  if (name_token == Token::AWAIT) {
2420  DCHECK(!is_async_function());
2421  classifier()->RecordAsyncArrowFormalParametersError(
2422  next_loc, MessageTemplate::kAwaitBindingIdentifier);
2423  }
2424  ExpressionT lhs =
2425  impl()->ExpressionFromIdentifier(name, next_loc.beg_pos);
2426  if (!IsAssignableIdentifier(lhs)) {
2427  classifier()->RecordPatternError(next_loc,
2428  MessageTemplate::kStrictEvalArguments);
2429  }
2430 
2431  ExpressionT value;
2432  if (peek() == Token::ASSIGN) {
2433  Consume(Token::ASSIGN);
2434  ExpressionClassifier rhs_classifier(this);
2435  AcceptINScope scope(this, true);
2436  ExpressionT rhs = ParseAssignmentExpression();
2437  ValidateExpression();
2438  AccumulateFormalParameterContainmentErrors();
2439  value = factory()->NewAssignment(Token::ASSIGN, lhs, rhs,
2440  kNoSourcePosition);
2441  classifier()->RecordExpressionError(
2442  Scanner::Location(next_loc.beg_pos, end_position()),
2443  MessageTemplate::kInvalidCoverInitializedName);
2444 
2445  impl()->SetFunctionNameFromIdentifierRef(rhs, lhs);
2446  } else {
2447  value = lhs;
2448  }
2449 
2450  ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2451  name_expression, value, ObjectLiteralProperty::COMPUTED, false);
2452  impl()->SetFunctionNameFromPropertyName(result, name);
2453  return result;
2454  }
2455 
2456  case ParsePropertyKind::kMethod: {
2457  // MethodDefinition
2458  // PropertyName '(' StrictFormalParameters ')' '{' FunctionBody '}'
2459  // '*' PropertyName '(' StrictFormalParameters ')' '{' FunctionBody '}'
2460 
2461  classifier()->RecordPatternError(
2462  Scanner::Location(next_loc.beg_pos, end_position()),
2463  MessageTemplate::kInvalidDestructuringTarget);
2464 
2465  FunctionKind kind = MethodKindFor(function_flags);
2466 
2467  ExpressionT value = impl()->ParseFunctionLiteral(
2468  name, scanner()->location(), kSkipFunctionNameCheck, kind,
2469  next_loc.beg_pos, FunctionLiteral::kAccessorOrMethod, language_mode(),
2470  nullptr);
2471 
2472  ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2473  name_expression, value, ObjectLiteralProperty::COMPUTED,
2474  prop_info->is_computed_name);
2475  impl()->SetFunctionNameFromPropertyName(result, name);
2476  return result;
2477  }
2478 
2479  case ParsePropertyKind::kAccessorGetter:
2480  case ParsePropertyKind::kAccessorSetter: {
2481  DCHECK_EQ(function_flags, ParseFunctionFlag::kIsNormal);
2482  bool is_get = kind == ParsePropertyKind::kAccessorGetter;
2483 
2484  classifier()->RecordPatternError(
2485  Scanner::Location(next_loc.beg_pos, end_position()),
2486  MessageTemplate::kInvalidDestructuringTarget);
2487 
2488  if (!prop_info->is_computed_name) {
2489  // Make sure the name expression is a string since we need a Name for
2490  // Runtime_DefineAccessorPropertyUnchecked and since we can determine
2491  // this statically we can skip the extra runtime check.
2492  name_expression =
2493  factory()->NewStringLiteral(name, name_expression->position());
2494  }
2495 
2496  FunctionKind kind = is_get ? FunctionKind::kGetterFunction
2497  : FunctionKind::kSetterFunction;
2498 
2499  FunctionLiteralT value = impl()->ParseFunctionLiteral(
2500  name, scanner()->location(), kSkipFunctionNameCheck, kind,
2501  next_loc.beg_pos, FunctionLiteral::kAccessorOrMethod, language_mode(),
2502  nullptr);
2503 
2504  ObjectLiteralPropertyT result = factory()->NewObjectLiteralProperty(
2505  name_expression, value,
2506  is_get ? ObjectLiteralProperty::GETTER
2507  : ObjectLiteralProperty::SETTER,
2508  prop_info->is_computed_name);
2509  const AstRawString* prefix =
2510  is_get ? ast_value_factory()->get_space_string()
2511  : ast_value_factory()->set_space_string();
2512  impl()->SetFunctionNameFromPropertyName(result, name, prefix);
2513  return result;
2514  }
2515 
2516  case ParsePropertyKind::kClassField:
2517  case ParsePropertyKind::kNotSet:
2518  ReportUnexpectedToken(Next());
2519  return impl()->NullLiteralProperty();
2520  }
2521  UNREACHABLE();
2522 }
2523 
2524 template <typename Impl>
2525 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseObjectLiteral() {
2526  // ObjectLiteral ::
2527  // '{' (PropertyDefinition (',' PropertyDefinition)* ','? )? '}'
2528 
2529  int pos = peek_position();
2530  ObjectPropertyListT properties(pointer_buffer());
2531  int number_of_boilerplate_properties = 0;
2532 
2533  bool has_computed_names = false;
2534  bool has_rest_property = false;
2535  bool has_seen_proto = false;
2536 
2537  Consume(Token::LBRACE);
2538 
2539  while (!Check(Token::RBRACE)) {
2540  FuncNameInferrerState fni_state(&fni_);
2541 
2542  ParsePropertyInfo prop_info(this);
2543  prop_info.position = PropertyPosition::kObjectLiteral;
2544  ObjectLiteralPropertyT property =
2545  ParseObjectPropertyDefinition(&prop_info, &has_seen_proto);
2546  if (impl()->IsNull(property)) return impl()->FailureExpression();
2547 
2548  if (prop_info.is_computed_name) {
2549  has_computed_names = true;
2550  }
2551 
2552  if (prop_info.is_rest) {
2553  has_rest_property = true;
2554  }
2555 
2556  if (impl()->IsBoilerplateProperty(property) && !has_computed_names) {
2557  // Count CONSTANT or COMPUTED properties to maintain the enumeration
2558  // order.
2559  number_of_boilerplate_properties++;
2560  }
2561 
2562  properties.Add(property);
2563 
2564  if (peek() != Token::RBRACE) {
2565  Expect(Token::COMMA);
2566  }
2567 
2568  fni_.Infer();
2569  }
2570 
2571  // In pattern rewriter, we rewrite rest property to call out to a
2572  // runtime function passing all the other properties as arguments to
2573  // this runtime function. Here, we make sure that the number of
2574  // properties is less than number of arguments allowed for a runtime
2575  // call.
2576  if (has_rest_property && properties.length() > Code::kMaxArguments) {
2577  this->classifier()->RecordPatternError(Scanner::Location(pos, position()),
2578  MessageTemplate::kTooManyArguments);
2579  }
2580 
2581  return impl()->InitializeObjectLiteral(factory()->NewObjectLiteral(
2582  properties, number_of_boilerplate_properties, pos, has_rest_property));
2583 }
2584 
2585 template <typename Impl>
2586 void ParserBase<Impl>::ParseArguments(
2587  typename ParserBase<Impl>::ExpressionListT* args, bool* has_spread,
2588  bool maybe_arrow) {
2589  // Arguments ::
2590  // '(' (AssignmentExpression)*[','] ')'
2591 
2592  *has_spread = false;
2593  Consume(Token::LPAREN);
2594 
2595  while (peek() != Token::RPAREN) {
2596  int start_pos = peek_position();
2597  bool is_spread = Check(Token::ELLIPSIS);
2598  int expr_pos = peek_position();
2599 
2600  AcceptINScope scope(this, true);
2601  ExpressionT argument = ParseAssignmentExpression();
2602 
2603  if (V8_UNLIKELY(maybe_arrow)) {
2604  CheckArrowFormalParameter(argument);
2605  if (is_spread) {
2606  classifier()->RecordNonSimpleParameter();
2607  if (argument->IsAssignment()) {
2608  classifier()->RecordAsyncArrowFormalParametersError(
2609  scanner()->location(), MessageTemplate::kRestDefaultInitializer);
2610  }
2611  if (peek() == Token::COMMA) {
2612  classifier()->RecordAsyncArrowFormalParametersError(
2613  scanner()->peek_location(), MessageTemplate::kParamAfterRest);
2614  }
2615  }
2616  }
2617  if (is_spread) {
2618  *has_spread = true;
2619  argument = factory()->NewSpread(argument, start_pos, expr_pos);
2620  }
2621  args->Add(argument);
2622  if (!Check(Token::COMMA)) break;
2623  }
2624 
2625  if (args->length() > Code::kMaxArguments) {
2626  ReportMessage(MessageTemplate::kTooManyArguments);
2627  return;
2628  }
2629 
2630  Scanner::Location location = scanner_->location();
2631  if (!Check(Token::RPAREN)) {
2632  impl()->ReportMessageAt(location, MessageTemplate::kUnterminatedArgList);
2633  }
2634 }
2635 
2636 // Precedence = 2
2637 template <typename Impl>
2638 typename ParserBase<Impl>::ExpressionT
2639 ParserBase<Impl>::ParseAssignmentExpression() {
2640  // AssignmentExpression ::
2641  // ConditionalExpression
2642  // ArrowFunction
2643  // YieldExpression
2644  // LeftHandSideExpression AssignmentOperator AssignmentExpression
2645  int lhs_beg_pos = peek_position();
2646 
2647  if (peek() == Token::YIELD && is_generator()) {
2648  return ParseYieldExpression();
2649  }
2650 
2651  FuncNameInferrerState fni_state(&fni_);
2652  ExpressionClassifier arrow_formals_classifier(this);
2653 
2654  DCHECK_IMPLIES(!has_error(), -1 == rewritable_length_);
2655  DCHECK_IMPLIES(!has_error(), scope_snapshot_.IsCleared());
2656  DCHECK_IMPLIES(!has_error(),
2657  FunctionKind::kArrowFunction == next_arrow_function_kind_);
2658  ExpressionT expression = ParseConditionalExpression();
2659 
2660  Token::Value op = peek();
2661 
2662  if (!Token::IsArrowOrAssignmentOp(op)) {
2663  if (expression->IsProperty()) {
2664  Accumulate(~ExpressionClassifier::PatternProduction);
2665  } else {
2666  Accumulate(ExpressionClassifier::AllProductions);
2667  }
2668  return expression;
2669  }
2670 
2671  // Arrow functions.
2672  if (V8_UNLIKELY(op == Token::ARROW)) {
2673  ValidateArrowFormalParameters(expression);
2674  Scanner::Location loc(lhs_beg_pos, end_position());
2675  DeclarationScope* scope = NewFunctionScope(next_arrow_function_kind_);
2676 
2677  // Reset to default.
2678  next_arrow_function_kind_ = FunctionKind::kArrowFunction;
2679 
2680  if (has_error()) return impl()->FailureExpression();
2681  // Because the arrow's parameters were parsed in the outer scope,
2682  // we need to fix up the scope chain appropriately.
2683  scope_snapshot_.Reparent(scope);
2684 
2685  FormalParametersT parameters(scope);
2686  if (!classifier()->is_simple_parameter_list()) {
2687  scope->SetHasNonSimpleParameters();
2688  parameters.is_simple = false;
2689  }
2690 
2691  scope->set_start_position(lhs_beg_pos);
2692  impl()->DeclareArrowFunctionFormalParameters(&parameters, expression, loc);
2693 
2694  expression = ParseArrowFunctionLiteral(parameters);
2695  Accumulate(ExpressionClassifier::AsyncArrowFormalParametersProduction);
2696 
2697  fni_.Infer();
2698 
2699  return expression;
2700  }
2701 
2702  // Destructuring assignmment.
2703  if (V8_UNLIKELY(expression->IsPattern() && op == Token::ASSIGN)) {
2704  ValidatePattern(expression);
2705 
2706  // This is definitely not an expression so don't accumulate
2707  // expression-related errors.
2708  Accumulate(~ExpressionClassifier::ExpressionProduction);
2709  impl()->MarkPatternAsAssigned(expression);
2710 
2711  Consume(op);
2712  int pos = position();
2713 
2714  ExpressionClassifier rhs_classifier(this);
2715  ExpressionT right = ParseAssignmentExpression();
2716  ValidateExpression();
2717  AccumulateFormalParameterContainmentErrors();
2718  ExpressionT result = factory()->NewAssignment(op, expression, right, pos);
2719 
2720  auto rewritable = factory()->NewRewritableExpression(result, scope());
2721  impl()->QueueDestructuringAssignmentForRewriting(rewritable);
2722  return rewritable;
2723  }
2724 
2725  if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
2726  expression = RewriteInvalidReferenceExpression(
2727  expression, lhs_beg_pos, end_position(),
2728  MessageTemplate::kInvalidLhsInAssignment);
2729  }
2730  impl()->MarkExpressionAsAssigned(expression);
2731 
2732  Consume(op);
2733  Scanner::Location op_location = scanner()->location();
2734 
2735  ExpressionT right = ParseAssignmentExpression();
2736  // This is definitely not an assignment pattern, so don't accumulate
2737  // assignment pattern-related errors.
2738  ValidateExpression();
2739  AccumulateFormalParameterContainmentErrors();
2740 
2741  if (op == Token::ASSIGN) {
2742  // We try to estimate the set of properties set by constructors. We define a
2743  // new property whenever there is an assignment to a property of 'this'. We
2744  // should probably only add properties if we haven't seen them before.
2745  // Otherwise we'll probably overestimate the number of properties.
2746  if (impl()->IsThisProperty(expression)) function_state_->AddProperty();
2747 
2748  impl()->CheckAssigningFunctionLiteralToProperty(expression, right);
2749 
2750  // Check if the right hand side is a call to avoid inferring a
2751  // name if we're dealing with "a = function(){...}();"-like
2752  // expression.
2753  if (right->IsCall() || right->IsCallNew()) {
2754  fni_.RemoveLastFunction();
2755  } else {
2756  fni_.Infer();
2757  }
2758 
2759  impl()->SetFunctionNameFromIdentifierRef(right, expression);
2760 
2761  if (expression->IsProperty()) {
2762  classifier()->RecordBindingPatternError(
2763  Scanner::Location(expression->position(), end_position()),
2764  MessageTemplate::kInvalidPropertyBindingPattern);
2765  }
2766  } else {
2767  classifier()->RecordPatternError(
2768  op_location, MessageTemplate::kUnexpectedToken, Token::String(op));
2769  fni_.RemoveLastFunction();
2770  }
2771 
2772  return factory()->NewAssignment(op, expression, right, op_location.beg_pos);
2773 }
2774 
2775 template <typename Impl>
2776 typename ParserBase<Impl>::ExpressionT
2777 ParserBase<Impl>::ParseYieldExpression() {
2778  // YieldExpression ::
2779  // 'yield' ([no line terminator] '*'? AssignmentExpression)?
2780  int pos = peek_position();
2781  classifier()->RecordFormalParameterInitializerError(
2782  scanner()->peek_location(), MessageTemplate::kYieldInParameter);
2783  Consume(Token::YIELD);
2784 
2785  CheckStackOverflow();
2786 
2787  // The following initialization is necessary.
2788  ExpressionT expression = impl()->NullExpression();
2789  bool delegating = false; // yield*
2790  if (!scanner()->HasLineTerminatorBeforeNext()) {
2791  if (Check(Token::MUL)) delegating = true;
2792  switch (peek()) {
2793  case Token::EOS:
2794  case Token::SEMICOLON:
2795  case Token::RBRACE:
2796  case Token::RBRACK:
2797  case Token::RPAREN:
2798  case Token::COLON:
2799  case Token::COMMA:
2800  case Token::IN:
2801  // The above set of tokens is the complete set of tokens that can appear
2802  // after an AssignmentExpression, and none of them can start an
2803  // AssignmentExpression. This allows us to avoid looking for an RHS for
2804  // a regular yield, given only one look-ahead token.
2805  if (!delegating) break;
2806  // Delegating yields require an RHS; fall through.
2807  V8_FALLTHROUGH;
2808  default:
2809  expression = ParseAssignmentExpression();
2810  break;
2811  }
2812  }
2813 
2814  if (delegating) {
2815  ExpressionT yieldstar = factory()->NewYieldStar(expression, pos);
2816  impl()->RecordSuspendSourceRange(yieldstar, PositionAfterSemicolon());
2817  function_state_->AddSuspend();
2818  if (IsAsyncGeneratorFunction(function_state_->kind())) {
2819  // iterator_close and delegated_iterator_output suspend ids.
2820  function_state_->AddSuspend();
2821  function_state_->AddSuspend();
2822  }
2823  return yieldstar;
2824  }
2825 
2826  // Hackily disambiguate o from o.next and o [Symbol.iterator]().
2827  // TODO(verwaest): Come up with a better solution.
2828  ExpressionT yield =
2829  factory()->NewYield(expression, pos, Suspend::kOnExceptionThrow);
2830  impl()->RecordSuspendSourceRange(yield, PositionAfterSemicolon());
2831  function_state_->AddSuspend();
2832  return yield;
2833 }
2834 
2835 // Precedence = 3
2836 template <typename Impl>
2837 typename ParserBase<Impl>::ExpressionT
2838 ParserBase<Impl>::ParseConditionalExpression() {
2839  // ConditionalExpression ::
2840  // LogicalOrExpression
2841  // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression
2842 
2843  int pos = peek_position();
2844  // We start using the binary expression parser for prec >= 4 only!
2845  ExpressionT expression = ParseBinaryExpression(4);
2846  return peek() == Token::CONDITIONAL
2847  ? ParseConditionalContinuation(expression, pos)
2848  : expression;
2849 }
2850 
2851 template <typename Impl>
2852 typename ParserBase<Impl>::ExpressionT
2853 ParserBase<Impl>::ParseConditionalContinuation(ExpressionT expression,
2854  int pos) {
2855  SourceRange then_range, else_range;
2856 
2857  ExpressionT left;
2858  {
2859  SourceRangeScope range_scope(scanner(), &then_range);
2860  Consume(Token::CONDITIONAL);
2861  // In parsing the first assignment expression in conditional
2862  // expressions we always accept the 'in' keyword; see ECMA-262,
2863  // section 11.12, page 58.
2864  AcceptINScope scope(this, true);
2865  left = ParseAssignmentExpression();
2866  }
2867  ExpressionT right;
2868  {
2869  SourceRangeScope range_scope(scanner(), &else_range);
2870  Expect(Token::COLON);
2871  right = ParseAssignmentExpression();
2872  }
2873  ExpressionT expr = factory()->NewConditional(expression, left, right, pos);
2874  impl()->RecordConditionalSourceRange(expr, then_range, else_range);
2875  return expr;
2876 }
2877 
2878 // Precedence >= 4
2879 template <typename Impl>
2880 typename ParserBase<Impl>::ExpressionT
2881 ParserBase<Impl>::ParseBinaryContinuation(ExpressionT x, int prec, int prec1) {
2882  do {
2883  // prec1 >= 4
2884  while (Token::Precedence(peek(), accept_IN_) == prec1) {
2885  SourceRange right_range;
2886  int pos = peek_position();
2887  ExpressionT y;
2888  Token::Value op;
2889  {
2890  SourceRangeScope right_range_scope(scanner(), &right_range);
2891  op = Next();
2892 
2893  const bool is_right_associative = op == Token::EXP;
2894  const int next_prec = is_right_associative ? prec1 : prec1 + 1;
2895  y = ParseBinaryExpression(next_prec);
2896  }
2897 
2898  // For now we distinguish between comparisons and other binary
2899  // operations. (We could combine the two and get rid of this
2900  // code and AST node eventually.)
2901  if (Token::IsCompareOp(op)) {
2902  // We have a comparison.
2903  Token::Value cmp = op;
2904  switch (op) {
2905  case Token::NE: cmp = Token::EQ; break;
2906  case Token::NE_STRICT: cmp = Token::EQ_STRICT; break;
2907  default: break;
2908  }
2909  x = factory()->NewCompareOperation(cmp, x, y, pos);
2910  if (cmp != op) {
2911  // The comparison was negated - add a NOT.
2912  x = factory()->NewUnaryOperation(Token::NOT, x, pos);
2913  }
2914  } else if (!impl()->ShortcutNumericLiteralBinaryExpression(&x, y, op,
2915  pos) &&
2916  !impl()->CollapseNaryExpression(&x, y, op, pos, right_range)) {
2917  // We have a "normal" binary operation.
2918  x = factory()->NewBinaryOperation(op, x, y, pos);
2919  if (op == Token::OR || op == Token::AND) {
2920  impl()->RecordBinaryOperationSourceRange(x, right_range);
2921  }
2922  }
2923  }
2924  --prec1;
2925  } while (prec1 >= prec);
2926 
2927  return x;
2928 }
2929 
2930 // Precedence >= 4
2931 template <typename Impl>
2932 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseBinaryExpression(
2933  int prec) {
2934  DCHECK_GE(prec, 4);
2935  ExpressionT x = ParseUnaryExpression();
2936  int prec1 = Token::Precedence(peek(), accept_IN_);
2937  if (prec1 >= prec) {
2938  return ParseBinaryContinuation(x, prec, prec1);
2939  }
2940  return x;
2941 }
2942 
2943 template <typename Impl>
2944 typename ParserBase<Impl>::ExpressionT
2945 ParserBase<Impl>::ParseUnaryOrPrefixExpression() {
2946  Token::Value op = Next();
2947  int pos = position();
2948 
2949  // Assume "! function ..." indicates the function is likely to be called.
2950  if (op == Token::NOT && peek() == Token::FUNCTION) {
2951  function_state_->set_next_function_is_likely_called();
2952  }
2953 
2954  CheckStackOverflow();
2955 
2956  ExpressionT expression = ParseUnaryExpression();
2957 
2958  if (Token::IsUnaryOp(op)) {
2959  if (op == Token::DELETE) {
2960  if (impl()->IsIdentifier(expression) && is_strict(language_mode())) {
2961  // "delete identifier" is a syntax error in strict mode.
2962  ReportMessage(MessageTemplate::kStrictDelete);
2963  return impl()->FailureExpression();
2964  }
2965 
2966  if (impl()->IsPropertyWithPrivateFieldKey(expression)) {
2967  ReportMessage(MessageTemplate::kDeletePrivateField);
2968  return impl()->FailureExpression();
2969  }
2970  }
2971 
2972  if (peek() == Token::EXP) {
2973  ReportUnexpectedToken(Next());
2974  return impl()->FailureExpression();
2975  }
2976 
2977  // Allow the parser's implementation to rewrite the expression.
2978  return impl()->BuildUnaryExpression(expression, op, pos);
2979  }
2980 
2981  DCHECK(Token::IsCountOp(op));
2982 
2983  if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
2984  expression = RewriteInvalidReferenceExpression(
2985  expression, expression->position(), end_position(),
2986  MessageTemplate::kInvalidLhsInPrefixOp);
2987  }
2988  impl()->MarkExpressionAsAssigned(expression);
2989 
2990  return factory()->NewCountOperation(op, true /* prefix */, expression,
2991  position());
2992 }
2993 
2994 template <typename Impl>
2995 typename ParserBase<Impl>::ExpressionT
2996 ParserBase<Impl>::ParseAwaitExpression() {
2997  classifier()->RecordFormalParameterInitializerError(
2998  scanner()->peek_location(),
2999  MessageTemplate::kAwaitExpressionFormalParameter);
3000  int await_pos = peek_position();
3001  Consume(Token::AWAIT);
3002 
3003  CheckStackOverflow();
3004 
3005  ExpressionT value = ParseUnaryExpression();
3006 
3007  ExpressionT expr = factory()->NewAwait(value, await_pos);
3008  function_state_->AddSuspend();
3009  impl()->RecordSuspendSourceRange(expr, PositionAfterSemicolon());
3010  return expr;
3011 }
3012 
3013 template <typename Impl>
3014 typename ParserBase<Impl>::ExpressionT
3015 ParserBase<Impl>::ParseUnaryExpression() {
3016  // UnaryExpression ::
3017  // PostfixExpression
3018  // 'delete' UnaryExpression
3019  // 'void' UnaryExpression
3020  // 'typeof' UnaryExpression
3021  // '++' UnaryExpression
3022  // '--' UnaryExpression
3023  // '+' UnaryExpression
3024  // '-' UnaryExpression
3025  // '~' UnaryExpression
3026  // '!' UnaryExpression
3027  // [+Await] AwaitExpression[?Yield]
3028 
3029  Token::Value op = peek();
3030  if (Token::IsUnaryOrCountOp(op)) return ParseUnaryOrPrefixExpression();
3031  if (is_async_function() && op == Token::AWAIT) {
3032  return ParseAwaitExpression();
3033  }
3034  return ParsePostfixExpression();
3035 }
3036 
3037 template <typename Impl>
3038 typename ParserBase<Impl>::ExpressionT
3039 ParserBase<Impl>::ParsePostfixExpression() {
3040  // PostfixExpression ::
3041  // LeftHandSideExpression ('++' | '--')?
3042 
3043  int lhs_beg_pos = peek_position();
3044  ExpressionT expression = ParseLeftHandSideExpression();
3045  if (!scanner()->HasLineTerminatorBeforeNext() && Token::IsCountOp(peek())) {
3046  if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
3047  expression = RewriteInvalidReferenceExpression(
3048  expression, lhs_beg_pos, end_position(),
3049  MessageTemplate::kInvalidLhsInPostfixOp);
3050  }
3051  impl()->MarkExpressionAsAssigned(expression);
3052 
3053  Token::Value next = Next();
3054  expression =
3055  factory()->NewCountOperation(next,
3056  false /* postfix */,
3057  expression,
3058  position());
3059  }
3060  return expression;
3061 }
3062 
3063 template <typename Impl>
3064 typename ParserBase<Impl>::ExpressionT
3065 ParserBase<Impl>::ParseLeftHandSideExpression() {
3066  // LeftHandSideExpression ::
3067  // (NewExpression | MemberExpression) ...
3068 
3069  ExpressionT result = ParseMemberWithNewPrefixesExpression();
3070  if (!Token::IsPropertyOrCall(peek())) return result;
3071  return ParseLeftHandSideContinuation(result);
3072 }
3073 
3074 template <typename Impl>
3075 typename ParserBase<Impl>::ExpressionT
3076 ParserBase<Impl>::ParseLeftHandSideContinuation(ExpressionT result) {
3077  DCHECK(Token::IsPropertyOrCall(peek()));
3078 
3079  if (V8_UNLIKELY(peek() == Token::LPAREN && impl()->IsIdentifier(result) &&
3080  scanner()->current_token() == Token::ASYNC &&
3081  !scanner()->HasLineTerminatorBeforeNext())) {
3082  DCHECK(impl()->IsAsync(impl()->AsIdentifier(result)));
3083  int pos = position();
3084 
3085  Scope::Snapshot scope_snapshot(scope());
3086  int rewritable_length = static_cast<int>(
3087  function_state_->destructuring_assignments_to_rewrite().size());
3088 
3089  ExpressionListT args(pointer_buffer());
3090  bool has_spread;
3091  ParseArguments(&args, &has_spread, true);
3092  if (V8_LIKELY(peek() == Token::ARROW)) {
3093  fni_.RemoveAsyncKeywordFromEnd();
3094  if (!classifier()->is_valid_async_arrow_formal_parameters()) {
3095  ReportClassifierError(
3096  classifier()->async_arrow_formal_parameters_error());
3097  return impl()->FailureExpression();
3098  }
3099  next_arrow_function_kind_ = FunctionKind::kAsyncArrowFunction;
3100  scope_snapshot_ = std::move(scope_snapshot);
3101  rewritable_length_ = rewritable_length;
3102  // async () => ...
3103  if (!args.length()) return factory()->NewEmptyParentheses(pos);
3104  // async ( Arguments ) => ...
3105  ExpressionT result = impl()->ExpressionListToExpression(args);
3106  result->mark_parenthesized();
3107  return result;
3108  }
3109 
3110  if (has_spread) {
3111  result = impl()->SpreadCall(result, args, pos, Call::NOT_EVAL);
3112  } else {
3113  result = factory()->NewCall(result, args, pos, Call::NOT_EVAL);
3114  }
3115 
3116  fni_.RemoveLastFunction();
3117  if (!Token::IsPropertyOrCall(peek())) return result;
3118  }
3119 
3120  do {
3121  switch (peek()) {
3122  /* Property */
3123  case Token::LBRACK: {
3124  Consume(Token::LBRACK);
3125  int pos = position();
3126  AcceptINScope scope(this, true);
3127  ExpressionT index = ParseExpressionCoverGrammar();
3128  result = factory()->NewProperty(result, index, pos);
3129  Expect(Token::RBRACK);
3130  break;
3131  }
3132 
3133  /* Property */
3134  case Token::PERIOD: {
3135  Consume(Token::PERIOD);
3136  int pos = position();
3137  ExpressionT key = ParseIdentifierNameOrPrivateName();
3138  result = factory()->NewProperty(result, key, pos);
3139  break;
3140  }
3141 
3142  /* Call */
3143  case Token::LPAREN: {
3144  int pos;
3145  if (Token::IsCallable(scanner()->current_token())) {
3146  // For call of an identifier we want to report position of
3147  // the identifier as position of the call in the stack trace.
3148  pos = position();
3149  } else {
3150  // For other kinds of calls we record position of the parenthesis as
3151  // position of the call. Note that this is extremely important for
3152  // expressions of the form function(){...}() for which call position
3153  // should not point to the closing brace otherwise it will intersect
3154  // with positions recorded for function literal and confuse debugger.
3155  pos = peek_position();
3156  // Also the trailing parenthesis are a hint that the function will
3157  // be called immediately. If we happen to have parsed a preceding
3158  // function literal eagerly, we can also compile it eagerly.
3159  if (result->IsFunctionLiteral()) {
3160  result->AsFunctionLiteral()->SetShouldEagerCompile();
3161  result->AsFunctionLiteral()->mark_as_iife();
3162  }
3163  }
3164  bool has_spread;
3165  ExpressionListT args(pointer_buffer());
3166  ParseArguments(&args, &has_spread);
3167 
3168  // Keep track of eval() calls since they disable all local variable
3169  // optimizations.
3170  // The calls that need special treatment are the
3171  // direct eval calls. These calls are all of the form eval(...), with
3172  // no explicit receiver.
3173  // These calls are marked as potentially direct eval calls. Whether
3174  // they are actually direct calls to eval is determined at run time.
3175  Call::PossiblyEval is_possibly_eval =
3176  CheckPossibleEvalCall(result, scope());
3177 
3178  if (has_spread) {
3179  result = impl()->SpreadCall(result, args, pos, is_possibly_eval);
3180  } else {
3181  result = factory()->NewCall(result, args, pos, is_possibly_eval);
3182  }
3183 
3184  fni_.RemoveLastFunction();
3185  break;
3186  }
3187 
3188  /* Call */
3189  default:
3190  DCHECK(Token::IsTemplate(peek()));
3191  result = ParseTemplateLiteral(result, position(), true);
3192  break;
3193  }
3194  } while (Token::IsPropertyOrCall(peek()));
3195  return result;
3196 }
3197 
3198 template <typename Impl>
3199 typename ParserBase<Impl>::ExpressionT
3200 ParserBase<Impl>::ParseMemberWithPresentNewPrefixesExpression() {
3201  // NewExpression ::
3202  // ('new')+ MemberExpression
3203  //
3204  // NewTarget ::
3205  // 'new' '.' 'target'
3206 
3207  // The grammar for new expressions is pretty warped. We can have several 'new'
3208  // keywords following each other, and then a MemberExpression. When we see '('
3209  // after the MemberExpression, it's associated with the rightmost unassociated
3210  // 'new' to create a NewExpression with arguments. However, a NewExpression
3211  // can also occur without arguments.
3212 
3213  // Examples of new expression:
3214  // new foo.bar().baz means (new (foo.bar)()).baz
3215  // new foo()() means (new foo())()
3216  // new new foo()() means (new (new foo())())
3217  // new new foo means new (new foo)
3218  // new new foo() means new (new foo())
3219  // new new foo().bar().baz means (new (new foo()).bar()).baz
3220  Consume(Token::NEW);
3221  int new_pos = position();
3222  ExpressionT result;
3223 
3224  CheckStackOverflow();
3225 
3226  if (peek() == Token::SUPER) {
3227  const bool is_new = true;
3228  result = ParseSuperExpression(is_new);
3229  } else if (allow_harmony_dynamic_import() && peek() == Token::IMPORT &&
3230  (!allow_harmony_import_meta() || PeekAhead() == Token::LPAREN)) {
3231  impl()->ReportMessageAt(scanner()->peek_location(),
3232  MessageTemplate::kImportCallNotNewExpression);
3233  return impl()->FailureExpression();
3234  } else if (peek() == Token::PERIOD) {
3235  result = ParseNewTargetExpression();
3236  return ParseMemberExpressionContinuation(result);
3237  } else {
3238  result = ParseMemberWithNewPrefixesExpression();
3239  }
3240  if (peek() == Token::LPAREN) {
3241  // NewExpression with arguments.
3242  {
3243  ExpressionListT args(pointer_buffer());
3244  bool has_spread;
3245  ParseArguments(&args, &has_spread);
3246 
3247  if (has_spread) {
3248  result = impl()->SpreadCallNew(result, args, new_pos);
3249  } else {
3250  result = factory()->NewCallNew(result, args, new_pos);
3251  }
3252  }
3253  // The expression can still continue with . or [ after the arguments.
3254  return ParseMemberExpressionContinuation(result);
3255  }
3256  // NewExpression without arguments.
3257  ExpressionListT args(pointer_buffer());
3258  return factory()->NewCallNew(result, args, new_pos);
3259 }
3260 
3261 template <typename Impl>
3262 typename ParserBase<Impl>::ExpressionT
3263 ParserBase<Impl>::ParseMemberWithNewPrefixesExpression() {
3264  return peek() == Token::NEW ? ParseMemberWithPresentNewPrefixesExpression()
3265  : ParseMemberExpression();
3266 }
3267 
3268 template <typename Impl>
3269 typename ParserBase<Impl>::ExpressionT
3270 ParserBase<Impl>::ParseFunctionExpression() {
3271  Consume(Token::FUNCTION);
3272  int function_token_position = position();
3273 
3274  FunctionKind function_kind = Check(Token::MUL)
3275  ? FunctionKind::kGeneratorFunction
3276  : FunctionKind::kNormalFunction;
3277  IdentifierT name = impl()->NullIdentifier();
3278  bool is_strict_reserved_name = false;
3279  Scanner::Location function_name_location = Scanner::Location::invalid();
3280  FunctionLiteral::FunctionType function_type =
3281  FunctionLiteral::kAnonymousExpression;
3282  if (impl()->ParsingDynamicFunctionDeclaration()) {
3283  // We don't want dynamic functions to actually declare their name
3284  // "anonymous". We just want that name in the toString().
3285  Consume(Token::IDENTIFIER);
3286  DCHECK_IMPLIES(!has_error(),
3287  scanner()->CurrentSymbol(ast_value_factory()) ==
3288  ast_value_factory()->anonymous_string());
3289  } else if (peek_any_identifier()) {
3290  bool is_await = false;
3291  name = ParseIdentifierOrStrictReservedWord(
3292  function_kind, &is_strict_reserved_name, &is_await);
3293  function_name_location = scanner()->location();
3294  function_type = FunctionLiteral::kNamedExpression;
3295  }
3296  FunctionLiteralT result = impl()->ParseFunctionLiteral(
3297  name, function_name_location,
3298  is_strict_reserved_name ? kFunctionNameIsStrictReserved
3299  : kFunctionNameValidityUnknown,
3300  function_kind, function_token_position, function_type, language_mode(),
3301  nullptr);
3302  // TODO(verwaest): FailureFunctionLiteral?
3303  if (impl()->IsNull(result)) return impl()->FailureExpression();
3304  return result;
3305 }
3306 
3307 template <typename Impl>
3308 typename ParserBase<Impl>::ExpressionT
3309 ParserBase<Impl>::ParseMemberExpression() {
3310  // MemberExpression ::
3311  // (PrimaryExpression | FunctionLiteral | ClassLiteral)
3312  // ('[' Expression ']' | '.' Identifier | Arguments | TemplateLiteral)*
3313  //
3314  // CallExpression ::
3315  // (SuperCall | ImportCall)
3316  // ('[' Expression ']' | '.' Identifier | Arguments | TemplateLiteral)*
3317  //
3318  // The '[' Expression ']' and '.' Identifier parts are parsed by
3319  // ParseMemberExpressionContinuation, and the Arguments part is parsed by the
3320  // caller.
3321 
3322  // Parse the initial primary or function expression.
3323  ExpressionT result;
3324  if (peek() == Token::FUNCTION) {
3325  result = ParseFunctionExpression();
3326  } else if (peek() == Token::SUPER) {
3327  const bool is_new = false;
3328  result = ParseSuperExpression(is_new);
3329  } else if (allow_harmony_dynamic_import() && peek() == Token::IMPORT) {
3330  result = ParseImportExpressions();
3331  } else {
3332  result = ParsePrimaryExpression();
3333  }
3334 
3335  return ParseMemberExpressionContinuation(result);
3336 }
3337 
3338 template <typename Impl>
3339 typename ParserBase<Impl>::ExpressionT
3340 ParserBase<Impl>::ParseImportExpressions() {
3341  DCHECK(allow_harmony_dynamic_import());
3342 
3343  Consume(Token::IMPORT);
3344  int pos = position();
3345  if (allow_harmony_import_meta() && peek() == Token::PERIOD) {
3346  ExpectMetaProperty(ast_value_factory()->meta_string(), "import.meta", pos);
3347  if (!parsing_module_) {
3348  impl()->ReportMessageAt(scanner()->location(),
3349  MessageTemplate::kImportMetaOutsideModule);
3350  return impl()->FailureExpression();
3351  }
3352 
3353  return impl()->ImportMetaExpression(pos);
3354  }
3355  Expect(Token::LPAREN);
3356  if (peek() == Token::RPAREN) {
3357  impl()->ReportMessageAt(scanner()->location(),
3358  MessageTemplate::kImportMissingSpecifier);
3359  return impl()->FailureExpression();
3360  }
3361  AcceptINScope scope(this, true);
3362  ExpressionT arg = ParseAssignmentExpression();
3363  Expect(Token::RPAREN);
3364 
3365  return factory()->NewImportCallExpression(arg, pos);
3366 }
3367 
3368 template <typename Impl>
3369 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseSuperExpression(
3370  bool is_new) {
3371  Consume(Token::SUPER);
3372  int pos = position();
3373 
3374  DeclarationScope* scope = GetReceiverScope();
3375  FunctionKind kind = scope->function_kind();
3376  if (IsConciseMethod(kind) || IsAccessorFunction(kind) ||
3377  IsClassConstructor(kind)) {
3378  if (Token::IsProperty(peek())) {
3379  scope->RecordSuperPropertyUsage();
3380  return impl()->NewSuperPropertyReference(pos);
3381  }
3382  // new super() is never allowed.
3383  // super() is only allowed in derived constructor
3384  if (!is_new && peek() == Token::LPAREN && IsDerivedConstructor(kind)) {
3385  // TODO(rossberg): This might not be the correct FunctionState for the
3386  // method here.
3387  return impl()->NewSuperCallReference(pos);
3388  }
3389  }
3390 
3391  impl()->ReportMessageAt(scanner()->location(),
3392  MessageTemplate::kUnexpectedSuper);
3393  return impl()->FailureExpression();
3394 }
3395 
3396 template <typename Impl>
3397 void ParserBase<Impl>::ExpectMetaProperty(const AstRawString* property_name,
3398  const char* full_name, int pos) {
3399  Consume(Token::PERIOD);
3400  ExpectContextualKeyword(property_name);
3401  if (V8_UNLIKELY(scanner()->literal_contains_escapes())) {
3402  impl()->ReportMessageAt(Scanner::Location(pos, end_position()),
3403  MessageTemplate::kInvalidEscapedMetaProperty,
3404  full_name);
3405  }
3406 }
3407 
3408 template <typename Impl>
3409 typename ParserBase<Impl>::ExpressionT
3410 ParserBase<Impl>::ParseNewTargetExpression() {
3411  int pos = position();
3412  ExpectMetaProperty(ast_value_factory()->target_string(), "new.target", pos);
3413 
3414  if (!GetReceiverScope()->is_function_scope()) {
3415  impl()->ReportMessageAt(scanner()->location(),
3416  MessageTemplate::kUnexpectedNewTarget);
3417  return impl()->FailureExpression();
3418  }
3419 
3420  return impl()->NewTargetExpression(pos);
3421 }
3422 
3423 template <typename Impl>
3424 typename ParserBase<Impl>::ExpressionT
3425 ParserBase<Impl>::DoParseMemberExpressionContinuation(ExpressionT expression) {
3426  DCHECK(Token::IsMember(peek()));
3427  // Parses this part of MemberExpression:
3428  // ('[' Expression ']' | '.' Identifier | TemplateLiteral)*
3429  do {
3430  switch (peek()) {
3431  case Token::LBRACK: {
3432  Consume(Token::LBRACK);
3433  int pos = position();
3434  AcceptINScope scope(this, true);
3435  ExpressionT index = ParseExpressionCoverGrammar();
3436  expression = factory()->NewProperty(expression, index, pos);
3437  impl()->PushPropertyName(index);
3438  Expect(Token::RBRACK);
3439  break;
3440  }
3441  case Token::PERIOD: {
3442  Consume(Token::PERIOD);
3443  int pos = peek_position();
3444  ExpressionT key = ParseIdentifierNameOrPrivateName();
3445  expression = factory()->NewProperty(expression, key, pos);
3446  break;
3447  }
3448  default: {
3449  DCHECK(Token::IsTemplate(peek()));
3450  int pos;
3451  if (scanner()->current_token() == Token::IDENTIFIER) {
3452  pos = position();
3453  } else {
3454  pos = peek_position();
3455  if (expression->IsFunctionLiteral()) {
3456  // If the tag function looks like an IIFE, set_parenthesized() to
3457  // force eager compilation.
3458  expression->AsFunctionLiteral()->SetShouldEagerCompile();
3459  }
3460  }
3461  expression = ParseTemplateLiteral(expression, pos, true);
3462  break;
3463  }
3464  }
3465  } while (Token::IsMember(peek()));
3466  return expression;
3467 }
3468 
3469 template <typename Impl>
3470 void ParserBase<Impl>::ParseFormalParameter(FormalParametersT* parameters) {
3471  // FormalParameter[Yield,GeneratorParameter] :
3472  // BindingElement[?Yield, ?GeneratorParameter]
3473  bool is_rest = parameters->has_rest;
3474 
3475  FuncNameInferrerState fni_state(&fni_);
3476  ExpressionT pattern = ParseBindingPattern();
3477  if (!impl()->IsIdentifier(pattern)) {
3478  parameters->is_simple = false;
3479  }
3480 
3481  ExpressionT initializer = impl()->NullExpression();
3482  if (Check(Token::ASSIGN)) {
3483  if (is_rest) {
3484  ReportMessage(MessageTemplate::kRestDefaultInitializer);
3485  return;
3486  }
3487  {
3488  ExpressionClassifier init_classifier(this);
3489  AcceptINScope scope(this, true);
3490  initializer = ParseAssignmentExpression();
3491  ValidateExpression();
3492  parameters->is_simple = false;
3493  Accumulate(ExpressionClassifier::FormalParameterInitializerProduction);
3494  }
3495  classifier()->RecordNonSimpleParameter();
3496  impl()->SetFunctionNameFromIdentifierRef(initializer, pattern);
3497  }
3498 
3499  impl()->AddFormalParameter(parameters, pattern, initializer, end_position(),
3500  is_rest);
3501 }
3502 
3503 template <typename Impl>
3504 void ParserBase<Impl>::ParseFormalParameterList(FormalParametersT* parameters) {
3505  // FormalParameters[Yield] :
3506  // [empty]
3507  // FunctionRestParameter[?Yield]
3508  // FormalParameterList[?Yield]
3509  // FormalParameterList[?Yield] ,
3510  // FormalParameterList[?Yield] , FunctionRestParameter[?Yield]
3511  //
3512  // FormalParameterList[Yield] :
3513  // FormalParameter[?Yield]
3514  // FormalParameterList[?Yield] , FormalParameter[?Yield]
3515 
3516  DCHECK_EQ(0, parameters->arity);
3517 
3518  if (peek() != Token::RPAREN) {
3519  while (true) {
3520  // Add one since we're going to be adding a parameter.
3521  if (parameters->arity + 1 > Code::kMaxArguments) {
3522  ReportMessage(MessageTemplate::kTooManyParameters);
3523  return;
3524  }
3525  parameters->has_rest = Check(Token::ELLIPSIS);
3526  ParseFormalParameter(parameters);
3527 
3528  if (parameters->has_rest) {
3529  parameters->is_simple = false;
3530  classifier()->RecordNonSimpleParameter();
3531  if (peek() == Token::COMMA) {
3532  impl()->ReportMessageAt(scanner()->peek_location(),
3533  MessageTemplate::kParamAfterRest);
3534  return;
3535  }
3536  break;
3537  }
3538  if (!Check(Token::COMMA)) break;
3539  if (peek() == Token::RPAREN) {
3540  // allow the trailing comma
3541  break;
3542  }
3543  }
3544  }
3545 
3546  impl()->DeclareFormalParameters(parameters);
3547 }
3548 
3549 template <typename Impl>
3550 typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseVariableDeclarations(
3551  VariableDeclarationContext var_context,
3552  DeclarationParsingResult* parsing_result,
3553  ZonePtrList<const AstRawString>* names) {
3554  // VariableDeclarations ::
3555  // ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[',']
3556  //
3557  // ES6:
3558  // FIXME(marja, nikolaos): Add an up-to-date comment about ES6 variable
3559  // declaration syntax.
3560 
3561  DCHECK_NOT_NULL(parsing_result);
3562  parsing_result->descriptor.declaration_kind = DeclarationDescriptor::NORMAL;
3563  parsing_result->descriptor.declaration_pos = peek_position();
3564  parsing_result->descriptor.initialization_pos = peek_position();
3565 
3566  BlockT init_block = impl()->NullStatement();
3567  if (var_context != kForStatement) {
3568  init_block = factory()->NewBlock(1, true);
3569  }
3570 
3571  switch (peek()) {
3572  case Token::VAR:
3573  parsing_result->descriptor.mode = VariableMode::kVar;
3574  Consume(Token::VAR);
3575  break;
3576  case Token::CONST:
3577  Consume(Token::CONST);
3578  DCHECK_NE(var_context, kStatement);
3579  parsing_result->descriptor.mode = VariableMode::kConst;
3580  break;
3581  case Token::LET:
3582  Consume(Token::LET);
3583  DCHECK_NE(var_context, kStatement);
3584  parsing_result->descriptor.mode = VariableMode::kLet;
3585  break;
3586  default:
3587  UNREACHABLE(); // by current callers
3588  break;
3589  }
3590 
3591  parsing_result->descriptor.scope = scope();
3592 
3593  int bindings_start = peek_position();
3594  do {
3595  // Parse binding pattern.
3596  FuncNameInferrerState fni_state(&fni_);
3597 
3598  ExpressionT pattern = impl()->NullExpression();
3599  int decl_pos = peek_position();
3600  {
3601  ExpressionClassifier pattern_classifier(this);
3602  pattern = ParseBindingPattern();
3603 
3604  if (IsLexicalVariableMode(parsing_result->descriptor.mode)) {
3605  ValidateLetPattern();
3606  }
3607  }
3608  Scanner::Location variable_loc = scanner()->location();
3609 
3610  bool single_name = impl()->IsIdentifier(pattern);
3611  if (single_name) {
3612  impl()->PushVariableName(impl()->AsIdentifier(pattern));
3613  }
3614 
3615  ExpressionT value = impl()->NullExpression();
3616  int initializer_position = kNoSourcePosition;
3617  int value_beg_position = kNoSourcePosition;
3618  if (Check(Token::ASSIGN)) {
3619  value_beg_position = peek_position();
3620 
3621  ExpressionClassifier classifier(this);
3622  AcceptINScope scope(this, var_context != kForStatement);
3623  value = ParseAssignmentExpression();
3624  ValidateExpression();
3625  variable_loc.end_pos = end_position();
3626 
3627  if (!parsing_result->first_initializer_loc.IsValid()) {
3628  parsing_result->first_initializer_loc = variable_loc;
3629  }
3630 
3631  // Don't infer if it is "a = function(){...}();"-like expression.
3632  if (single_name) {
3633  if (!value->IsCall() && !value->IsCallNew()) {
3634  fni_.Infer();
3635  } else {
3636  fni_.RemoveLastFunction();
3637  }
3638  }
3639 
3640  impl()->SetFunctionNameFromIdentifierRef(value, pattern);
3641 
3642  // End position of the initializer is after the assignment expression.
3643  initializer_position = end_position();
3644  } else {
3645  if (var_context != kForStatement || !PeekInOrOf()) {
3646  // ES6 'const' and binding patterns require initializers.
3647  if (parsing_result->descriptor.mode == VariableMode::kConst ||
3648  !impl()->IsIdentifier(pattern)) {
3649  impl()->ReportMessageAt(
3650  Scanner::Location(decl_pos, end_position()),
3651  MessageTemplate::kDeclarationMissingInitializer,
3652  !impl()->IsIdentifier(pattern) ? "destructuring" : "const");
3653  return impl()->NullStatement();
3654  }
3655  // 'let x' initializes 'x' to undefined.
3656  if (parsing_result->descriptor.mode == VariableMode::kLet) {
3657  value = factory()->NewUndefinedLiteral(position());
3658  }
3659  }
3660 
3661  // End position of the initializer is after the variable.
3662  initializer_position = position();
3663  }
3664 
3665  typename DeclarationParsingResult::Declaration decl(
3666  pattern, initializer_position, value);
3667  decl.value_beg_position = value_beg_position;
3668  if (var_context == kForStatement) {
3669  // Save the declaration for further handling in ParseForStatement.
3670  parsing_result->declarations.push_back(decl);
3671  } else {
3672  // Immediately declare the variable otherwise. This avoids O(N^2)
3673  // behavior (where N is the number of variables in a single
3674  // declaration) in the PatternRewriter having to do with removing
3675  // and adding VariableProxies to the Scope (see bug 4699).
3676  impl()->DeclareAndInitializeVariables(
3677  init_block, &parsing_result->descriptor, &decl, names);
3678  }
3679  } while (Check(Token::COMMA));
3680 
3681  parsing_result->bindings_loc =
3682  Scanner::Location(bindings_start, end_position());
3683 
3684  return init_block;
3685 }
3686 
3687 template <typename Impl>
3688 typename ParserBase<Impl>::StatementT
3689 ParserBase<Impl>::ParseFunctionDeclaration() {
3690  Consume(Token::FUNCTION);
3691 
3692  int pos = position();
3693  ParseFunctionFlags flags = ParseFunctionFlag::kIsNormal;
3694  if (Check(Token::MUL)) {
3695  impl()->ReportMessageAt(
3696  scanner()->location(),
3697  MessageTemplate::kGeneratorInSingleStatementContext);
3698  return impl()->NullStatement();
3699  }
3700  return ParseHoistableDeclaration(pos, flags, nullptr, false);
3701 }
3702 
3703 template <typename Impl>
3704 typename ParserBase<Impl>::StatementT
3705 ParserBase<Impl>::ParseHoistableDeclaration(
3706  ZonePtrList<const AstRawString>* names, bool default_export) {
3707  Consume(Token::FUNCTION);
3708 
3709  int pos = position();
3710  ParseFunctionFlags flags = ParseFunctionFlag::kIsNormal;
3711  if (Check(Token::MUL)) {
3712  flags |= ParseFunctionFlag::kIsGenerator;
3713  }
3714  return ParseHoistableDeclaration(pos, flags, names, default_export);
3715 }
3716 
3717 template <typename Impl>
3718 typename ParserBase<Impl>::StatementT
3719 ParserBase<Impl>::ParseHoistableDeclaration(
3720  int pos, ParseFunctionFlags flags, ZonePtrList<const AstRawString>* names,
3721  bool default_export) {
3722  CheckStackOverflow();
3723 
3724  // FunctionDeclaration ::
3725  // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
3726  // 'function' '(' FormalParameters ')' '{' FunctionBody '}'
3727  // GeneratorDeclaration ::
3728  // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
3729  // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}'
3730  //
3731  // The anonymous forms are allowed iff [default_export] is true.
3732  //
3733  // 'function' and '*' (if present) have been consumed by the caller.
3734 
3735  DCHECK_IMPLIES((flags & ParseFunctionFlag::kIsAsync) != 0,
3736  (flags & ParseFunctionFlag::kIsGenerator) == 0);
3737 
3738  if ((flags & ParseFunctionFlag::kIsAsync) != 0 && Check(Token::MUL)) {
3739  // Async generator
3740  flags |= ParseFunctionFlag::kIsGenerator;
3741  }
3742 
3743  IdentifierT name;
3744  FunctionNameValidity name_validity;
3745  IdentifierT variable_name;
3746  if (default_export && peek() == Token::LPAREN) {
3747  impl()->GetDefaultStrings(&name, &variable_name);
3748  name_validity = kSkipFunctionNameCheck;
3749  } else {
3750  bool is_strict_reserved = false;
3751  bool is_await = false;
3752  name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, &is_await);
3753  name_validity = is_strict_reserved ? kFunctionNameIsStrictReserved
3754  : kFunctionNameValidityUnknown;
3755  variable_name = name;
3756  }
3757 
3758  FuncNameInferrerState fni_state(&fni_);
3759  impl()->PushEnclosingName(name);
3760 
3761  FunctionKind kind = FunctionKindFor(flags);
3762 
3763  FunctionLiteralT function = impl()->ParseFunctionLiteral(
3764  name, scanner()->location(), name_validity, kind, pos,
3765  FunctionLiteral::kDeclaration, language_mode(), nullptr);
3766 
3767  // In ES6, a function behaves as a lexical binding, except in
3768  // a script scope, or the initial scope of eval or another function.
3769  VariableMode mode =
3770  (!scope()->is_declaration_scope() || scope()->is_module_scope())
3771  ? VariableMode::kLet
3772  : VariableMode::kVar;
3773  // Async functions don't undergo sloppy mode block scoped hoisting, and don't
3774  // allow duplicates in a block. Both are represented by the
3775  // sloppy_block_function_map. Don't add them to the map for async functions.
3776  // Generators are also supposed to be prohibited; currently doing this behind
3777  // a flag and UseCounting violations to assess web compatibility.
3778  bool is_sloppy_block_function = is_sloppy(language_mode()) &&
3779  !scope()->is_declaration_scope() &&
3780  flags == ParseFunctionFlag::kIsNormal;
3781 
3782  return impl()->DeclareFunction(variable_name, function, mode, pos,
3783  is_sloppy_block_function, names);
3784 }
3785 
3786 template <typename Impl>
3787 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseClassDeclaration(
3788  ZonePtrList<const AstRawString>* names, bool default_export) {
3789  // ClassDeclaration ::
3790  // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}'
3791  // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}'
3792  //
3793  // The anonymous form is allowed iff [default_export] is true.
3794  //
3795  // 'class' is expected to be consumed by the caller.
3796  //
3797  // A ClassDeclaration
3798  //
3799  // class C { ... }
3800  //
3801  // has the same semantics as:
3802  //
3803  // let C = class C { ... };
3804  //
3805  // so rewrite it as such.
3806 
3807  int class_token_pos = position();
3808  IdentifierT name = impl()->NullIdentifier();
3809  bool is_strict_reserved = false;
3810  IdentifierT variable_name = impl()->NullIdentifier();
3811  if (default_export && (peek() == Token::EXTENDS || peek() == Token::LBRACE)) {
3812  impl()->GetDefaultStrings(&name, &variable_name);
3813  } else {
3814  bool is_await = false;
3815  name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, &is_await);
3816  variable_name = name;
3817  }
3818 
3819  ExpressionClassifier no_classifier(this);
3820  ExpressionT value = ParseClassLiteral(name, scanner()->location(),
3821  is_strict_reserved, class_token_pos);
3822  ValidateExpression();
3823  int end_pos = position();
3824  return impl()->DeclareClass(variable_name, value, names, class_token_pos,
3825  end_pos);
3826 }
3827 
3828 // Language extension which is only enabled for source files loaded
3829 // through the API's extension mechanism. A native function
3830 // declaration is resolved by looking up the function through a
3831 // callback provided by the extension.
3832 template <typename Impl>
3833 typename ParserBase<Impl>::StatementT
3834 ParserBase<Impl>::ParseNativeDeclaration() {
3835  function_state_->DisableOptimization(BailoutReason::kNativeFunctionLiteral);
3836 
3837  int pos = peek_position();
3838  Consume(Token::FUNCTION);
3839  // Allow "eval" or "arguments" for backward compatibility.
3840  IdentifierT name = ParseIdentifier(kAllowRestrictedIdentifiers);
3841  Expect(Token::LPAREN);
3842  if (peek() != Token::RPAREN) {
3843  do {
3844  ParseIdentifier(kAllowRestrictedIdentifiers);
3845  } while (Check(Token::COMMA));
3846  }
3847  Expect(Token::RPAREN);
3848  Expect(Token::SEMICOLON);
3849  return impl()->DeclareNative(name, pos);
3850 }
3851 
3852 template <typename Impl>
3853 typename ParserBase<Impl>::StatementT
3854 ParserBase<Impl>::ParseAsyncFunctionDeclaration(
3855  ZonePtrList<const AstRawString>* names, bool default_export) {
3856  // AsyncFunctionDeclaration ::
3857  // async [no LineTerminator here] function BindingIdentifier[Await]
3858  // ( FormalParameters[Await] ) { AsyncFunctionBody }
3859  DCHECK_EQ(scanner()->current_token(), Token::ASYNC);
3860  int pos = position();
3861  DCHECK(!scanner()->HasLineTerminatorBeforeNext());
3862  Consume(Token::FUNCTION);
3863  ParseFunctionFlags flags = ParseFunctionFlag::kIsAsync;
3864  return ParseHoistableDeclaration(pos, flags, names, default_export);
3865 }
3866 
3867 template <typename Impl>
3868 void ParserBase<Impl>::ParseFunctionBody(
3869  typename ParserBase<Impl>::StatementListT* body, IdentifierT function_name,
3870  int pos, const FormalParametersT& parameters, FunctionKind kind,
3871  FunctionLiteral::FunctionType function_type, FunctionBodyType body_type) {
3872  DeclarationScope* function_scope = scope()->AsDeclarationScope();
3873  DeclarationScope* inner_scope = function_scope;
3874 
3875  if (!parameters.is_simple) {
3876  inner_scope = NewVarblockScope();
3877  inner_scope->set_start_position(scanner()->location().beg_pos);
3878  }
3879 
3880  {
3881  BlockState block_state(&scope_, inner_scope);
3882 
3883  if (IsResumableFunction(kind)) impl()->PrepareGeneratorVariables();
3884 
3885  if (body_type == FunctionBodyType::kExpression) {
3886  ExpressionClassifier classifier(this);
3887  ExpressionT expression = ParseAssignmentExpression();
3888  ValidateExpression();
3889 
3890  if (IsAsyncFunction(kind)) {
3891  BlockT block = factory()->NewBlock(1, true);
3892  impl()->RewriteAsyncFunctionBody(body, block, expression);
3893  } else {
3894  body->Add(BuildReturnStatement(expression, expression->position()));
3895  }
3896  } else {
3897  DCHECK(accept_IN_);
3898  DCHECK_EQ(FunctionBodyType::kBlock, body_type);
3899  // If we are parsing the source as if it is wrapped in a function, the
3900  // source ends without a closing brace.
3901  Token::Value closing_token = function_type == FunctionLiteral::kWrapped
3902  ? Token::EOS
3903  : Token::RBRACE;
3904 
3905  if (IsAsyncGeneratorFunction(kind)) {
3906  impl()->ParseAndRewriteAsyncGeneratorFunctionBody(pos, kind, body);
3907  } else if (IsGeneratorFunction(kind)) {
3908  impl()->ParseAndRewriteGeneratorFunctionBody(pos, kind, body);
3909  } else if (IsAsyncFunction(kind)) {
3910  ParseAsyncFunctionBody(inner_scope, body);
3911  } else {
3912  ParseStatementList(body, closing_token);
3913  }
3914 
3915  if (IsDerivedConstructor(kind)) {
3916  body->Add(factory()->NewReturnStatement(impl()->ThisExpression(),
3917  kNoSourcePosition));
3918  }
3919  Expect(closing_token);
3920  }
3921  }
3922 
3923  scope()->set_end_position(end_position());
3924 
3925  bool allow_duplicate_parameters = false;
3926 
3927  if (parameters.is_simple) {
3928  DCHECK_EQ(inner_scope, function_scope);
3929  if (is_sloppy(function_scope->language_mode())) {
3930  impl()->InsertSloppyBlockFunctionVarBindings(function_scope);
3931  }
3932  allow_duplicate_parameters = is_sloppy(function_scope->language_mode()) &&
3933  !IsConciseMethod(kind) &&
3934  !IsArrowFunction(kind);
3935  } else {
3936  BlockT inner_block = factory()->NewBlock(true, *body);
3937  inner_block->set_scope(inner_scope);
3938  body->Rewind();
3939  DCHECK_NOT_NULL(inner_scope);
3940  DCHECK_EQ(function_scope, scope());
3941  DCHECK_EQ(function_scope, inner_scope->outer_scope());
3942  impl()->SetLanguageMode(function_scope, inner_scope->language_mode());
3943  // TODO(verwaest): Disable DCHECKs in failure mode?
3944  if (has_error()) return;
3945  BlockT init_block = impl()->BuildParameterInitializationBlock(parameters);
3946 
3947  if (is_sloppy(inner_scope->language_mode())) {
3948  impl()->InsertSloppyBlockFunctionVarBindings(inner_scope);
3949  }
3950 
3951  // TODO(littledan): Merge the two rejection blocks into one
3952  if (IsAsyncFunction(kind) && !IsAsyncGeneratorFunction(kind)) {
3953  init_block = impl()->BuildRejectPromiseOnException(init_block);
3954  }
3955 
3956  inner_scope->set_end_position(end_position());
3957  if (inner_scope->FinalizeBlockScope() != nullptr) {
3958  impl()->CheckConflictingVarDeclarations(inner_scope);
3959  impl()->InsertShadowingVarBindingInitializers(inner_block);
3960  } else {
3961  inner_block->set_scope(nullptr);
3962  }
3963  inner_scope = nullptr;
3964 
3965  body->Add(init_block);
3966  body->Add(inner_block);
3967  }
3968 
3969  ValidateFormalParameters(language_mode(), parameters,
3970  allow_duplicate_parameters);
3971 
3972  if (!IsArrowFunction(kind)) {
3973  // Declare arguments after parsing the function since lexical 'arguments'
3974  // masks the arguments object. Declare arguments before declaring the
3975  // function var since the arguments object masks 'function arguments'.
3976  function_scope->DeclareArguments(ast_value_factory());
3977  }
3978 
3979  impl()->DeclareFunctionNameVar(function_name, function_type, function_scope);
3980 }
3981 
3982 template <typename Impl>
3983 void ParserBase<Impl>::CheckArityRestrictions(int param_count,
3984  FunctionKind function_kind,
3985  bool has_rest,
3986  int formals_start_pos,
3987  int formals_end_pos) {
3988  if (IsGetterFunction(function_kind)) {
3989  if (param_count != 0) {
3990  impl()->ReportMessageAt(
3991  Scanner::Location(formals_start_pos, formals_end_pos),
3992  MessageTemplate::kBadGetterArity);
3993  }
3994  } else if (IsSetterFunction(function_kind)) {
3995  if (param_count != 1) {
3996  impl()->ReportMessageAt(
3997  Scanner::Location(formals_start_pos, formals_end_pos),
3998  MessageTemplate::kBadSetterArity);
3999  }
4000  if (has_rest) {
4001  impl()->ReportMessageAt(
4002  Scanner::Location(formals_start_pos, formals_end_pos),
4003  MessageTemplate::kBadSetterRestParameter);
4004  }
4005  }
4006 }
4007 
4008 template <typename Impl>
4009 bool ParserBase<Impl>::IsNextLetKeyword() {
4010  DCHECK_EQ(Token::LET, peek());
4011  Token::Value next_next = PeekAhead();
4012  switch (next_next) {
4013  case Token::LBRACE:
4014  case Token::LBRACK:
4015  case Token::IDENTIFIER:
4016  case Token::STATIC:
4017  case Token::LET: // `let let;` is disallowed by static semantics, but the
4018  // token must be first interpreted as a keyword in order
4019  // for those semantics to apply. This ensures that ASI is
4020  // not honored when a LineTerminator separates the
4021  // tokens.
4022  case Token::YIELD:
4023  case Token::AWAIT:
4024  case Token::ASYNC:
4025  return true;
4026  case Token::FUTURE_STRICT_RESERVED_WORD:
4027  return is_sloppy(language_mode());
4028  default:
4029  return false;
4030  }
4031 }
4032 
4033 template <typename Impl>
4034 typename ParserBase<Impl>::ExpressionT
4035 ParserBase<Impl>::ParseArrowFunctionLiteral(
4036  const FormalParametersT& formal_parameters) {
4037  const RuntimeCallCounterId counters[2][2] = {
4038  {RuntimeCallCounterId::kParseBackgroundArrowFunctionLiteral,
4039  RuntimeCallCounterId::kParseArrowFunctionLiteral},
4040  {RuntimeCallCounterId::kPreParseBackgroundArrowFunctionLiteral,
4041  RuntimeCallCounterId::kPreParseArrowFunctionLiteral}};
4042  RuntimeCallTimerScope runtime_timer(
4043  runtime_call_stats_,
4044  counters[Impl::IsPreParser()][parsing_on_main_thread_]);
4045  base::ElapsedTimer timer;
4046  if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
4047 
4048  DCHECK_IMPLIES(!has_error(), peek() == Token::ARROW);
4049  if (scanner_->HasLineTerminatorBeforeNext()) {
4050  // ASI inserts `;` after arrow parameters if a line terminator is found.
4051  // `=> ...` is never a valid expression, so report as syntax error.
4052  // If next token is not `=>`, it's a syntax error anyways.
4053  ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW);
4054  return impl()->FailureExpression();
4055  }
4056 
4057  int expected_property_count = -1;
4058  int suspend_count = 0;
4059  int function_literal_id = GetNextFunctionLiteralId();
4060 
4061  FunctionKind kind = formal_parameters.scope->function_kind();
4062  FunctionLiteral::EagerCompileHint eager_compile_hint =
4063  default_eager_compile_hint_;
4064  bool can_preparse = impl()->parse_lazily() &&
4065  eager_compile_hint == FunctionLiteral::kShouldLazyCompile;
4066  // TODO(marja): consider lazy-parsing inner arrow functions too. is_this
4067  // handling in Scope::ResolveVariable needs to change.
4068  bool is_lazy_top_level_function =
4069  can_preparse && impl()->AllowsLazyParsingWithoutUnresolvedVariables();
4070  bool has_braces = true;
4071  ProducedPreParsedScopeData* produced_preparsed_scope_data = nullptr;
4072  StatementListT body(pointer_buffer());
4073  {
4074  FunctionState function_state(&function_state_, &scope_,
4075  formal_parameters.scope);
4076 
4077  DCHECK_IMPLIES(!has_error(), -1 != rewritable_length_);
4078  // Move any queued destructuring assignments which appeared
4079  // in this function's parameter list into its own function_state.
4080  function_state.AdoptDestructuringAssignmentsFromParentState(
4081  rewritable_length_);
4082  rewritable_length_ = -1;
4083 
4084  Consume(Token::ARROW);
4085 
4086  if (peek() == Token::LBRACE) {
4087  // Multiple statement body
4088  DCHECK_EQ(scope(), formal_parameters.scope);
4089  if (is_lazy_top_level_function) {
4090  // FIXME(marja): Arrow function parameters will be parsed even if the
4091  // body is preparsed; move relevant parts of parameter handling to
4092  // simulate consistent parameter handling.
4093 
4094  // For arrow functions, we don't need to retrieve data about function
4095  // parameters.
4096  int dummy_num_parameters = -1;
4097  DCHECK_NE(kind & FunctionKind::kArrowFunction, 0);
4098  FunctionLiteral::EagerCompileHint hint;
4099  bool did_preparse_successfully = impl()->SkipFunction(
4100  nullptr, kind, FunctionLiteral::kAnonymousExpression,
4101  formal_parameters.scope, &dummy_num_parameters,
4102  &produced_preparsed_scope_data, false, &hint);
4103 
4104  // Validate parameter names. We can do this only after preparsing the
4105  // function, since the function can declare itself strict.
4106  ValidateFormalParameters(language_mode(), formal_parameters, false);
4107 
4108  DCHECK_NULL(produced_preparsed_scope_data);
4109 
4110  if (did_preparse_successfully) {
4111  // Discard any queued destructuring assignments which appeared
4112  // in this function's parameter list, and which were adopted
4113  // into this function state, above.
4114  function_state.RewindDestructuringAssignments(0);
4115  } else {
4116  // In case we did not sucessfully preparse the function because of an
4117  // unidentified error we do a full reparse to return the error.
4118  Consume(Token::LBRACE);
4119  AcceptINScope scope(this, true);
4120  ParseFunctionBody(&body, impl()->NullIdentifier(), kNoSourcePosition,
4121  formal_parameters, kind,
4122  FunctionLiteral::kAnonymousExpression,
4123  FunctionBodyType::kBlock);
4124  CHECK(has_error());
4125  return impl()->FailureExpression();
4126  }
4127  } else {
4128  Consume(Token::LBRACE);
4129  AcceptINScope scope(this, true);
4130  ParseFunctionBody(&body, impl()->NullIdentifier(), kNoSourcePosition,
4131  formal_parameters, kind,
4132  FunctionLiteral::kAnonymousExpression,
4133  FunctionBodyType::kBlock);
4134  expected_property_count = function_state.expected_property_count();
4135  }
4136  } else {
4137  // Single-expression body
4138  has_braces = false;
4139  ParseFunctionBody(&body, impl()->NullIdentifier(), kNoSourcePosition,
4140  formal_parameters, kind,
4141  FunctionLiteral::kAnonymousExpression,
4142  FunctionBodyType::kExpression);
4143  expected_property_count = function_state.expected_property_count();
4144  }
4145 
4146  formal_parameters.scope->set_end_position(end_position());
4147 
4148  // Validate strict mode.
4149  if (is_strict(language_mode())) {
4150  CheckStrictOctalLiteral(formal_parameters.scope->start_position(),
4151  end_position());
4152  }
4153  impl()->CheckConflictingVarDeclarations(formal_parameters.scope);
4154 
4155  impl()->RewriteDestructuringAssignments();
4156  suspend_count = function_state.suspend_count();
4157  }
4158 
4159  FunctionLiteralT function_literal = factory()->NewFunctionLiteral(
4160  impl()->EmptyIdentifierString(), formal_parameters.scope, body,
4161  expected_property_count, formal_parameters.num_parameters(),
4162  formal_parameters.function_length,
4163  FunctionLiteral::kNoDuplicateParameters,
4164  FunctionLiteral::kAnonymousExpression, eager_compile_hint,
4165  formal_parameters.scope->start_position(), has_braces,
4166  function_literal_id, produced_preparsed_scope_data);
4167 
4168  function_literal->set_suspend_count(suspend_count);
4169  function_literal->set_function_token_position(
4170  formal_parameters.scope->start_position());
4171 
4172  impl()->AddFunctionForNameInference(function_literal);
4173 
4174  if (V8_UNLIKELY((FLAG_log_function_events))) {
4175  Scope* scope = formal_parameters.scope;
4176  double ms = timer.Elapsed().InMillisecondsF();
4177  const char* event_name =
4178  is_lazy_top_level_function ? "preparse-no-resolution" : "parse";
4179  const char* name = "arrow function";
4180  logger_->FunctionEvent(event_name, script_id(), ms, scope->start_position(),
4181  scope->end_position(), name, strlen(name));
4182  }
4183 
4184  return function_literal;
4185 }
4186 
4187 template <typename Impl>
4188 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseClassLiteral(
4189  IdentifierT name, Scanner::Location class_name_location,
4190  bool name_is_strict_reserved, int class_token_pos) {
4191  bool is_anonymous = impl()->IsNull(name);
4192 
4193  // All parts of a ClassDeclaration and ClassExpression are strict code.
4194  if (!is_anonymous) {
4195  if (name_is_strict_reserved) {
4196  impl()->ReportMessageAt(class_name_location,
4197  MessageTemplate::kUnexpectedStrictReserved);
4198  return impl()->FailureExpression();
4199  }
4200  if (impl()->IsEvalOrArguments(name)) {
4201  impl()->ReportMessageAt(class_name_location,
4202  MessageTemplate::kStrictEvalArguments);
4203  return impl()->FailureExpression();
4204  }
4205  }
4206 
4207  Scope* block_scope = NewScope(BLOCK_SCOPE);
4208  BlockState block_state(&scope_, block_scope);
4209  RaiseLanguageMode(LanguageMode::kStrict);
4210 
4211  ClassInfo class_info(this);
4212  class_info.is_anonymous = is_anonymous;
4213  impl()->DeclareClassVariable(name, &class_info, class_token_pos);
4214 
4215  scope()->set_start_position(end_position());
4216  if (Check(Token::EXTENDS)) {
4217  FuncNameInferrerState fni_state(&fni_);
4218  class_info.extends = ParseLeftHandSideExpression();
4219  ValidateExpression();
4220  }
4221 
4222  Expect(Token::LBRACE);
4223 
4224  const bool has_extends = !impl()->IsNull(class_info.extends);
4225  while (peek() != Token::RBRACE) {
4226  if (Check(Token::SEMICOLON)) continue;
4227  FuncNameInferrerState fni_state(&fni_);
4228  // If we haven't seen the constructor yet, it potentially is the next
4229  // property.
4230  bool is_constructor = !class_info.has_seen_constructor;
4231  ParsePropertyInfo prop_info(this);
4232  prop_info.position = PropertyPosition::kClassLiteral;
4233  ClassLiteralPropertyT property =
4234  ParseClassPropertyDefinition(&class_info, &prop_info, has_extends);
4235 
4236  if (has_error()) return impl()->FailureExpression();
4237 
4238  ClassLiteralProperty::Kind property_kind =
4239  ClassPropertyKindFor(prop_info.kind);
4240  if (!class_info.has_static_computed_names && prop_info.is_static &&
4241  prop_info.is_computed_name) {
4242  class_info.has_static_computed_names = true;
4243  }
4244  if (prop_info.is_computed_name && !prop_info.is_private &&
4245  property_kind == ClassLiteralProperty::FIELD) {
4246  class_info.computed_field_count++;
4247  }
4248  is_constructor &= class_info.has_seen_constructor;
4249 
4250  impl()->DeclareClassProperty(name, property, prop_info.name, property_kind,
4251  prop_info.is_static, is_constructor,
4252  prop_info.is_computed_name,
4253  prop_info.is_private, &class_info);
4254  impl()->InferFunctionName();
4255  }
4256 
4257  Expect(Token::RBRACE);
4258  int end_pos = end_position();
4259  block_scope->set_end_position(end_pos);
4260  return impl()->RewriteClassLiteral(block_scope, name, &class_info,
4261  class_token_pos, end_pos);
4262 }
4263 
4264 template <typename Impl>
4265 void ParserBase<Impl>::ParseAsyncFunctionBody(Scope* scope,
4266  StatementListT* body) {
4267  BlockT block = impl()->NullStatement();
4268  {
4269  StatementListT statements(pointer_buffer());
4270  ParseStatementList(&statements, Token::RBRACE);
4271  block = factory()->NewBlock(true, statements);
4272  }
4273  impl()->RewriteAsyncFunctionBody(
4274  body, block, factory()->NewUndefinedLiteral(kNoSourcePosition));
4275  scope->set_end_position(end_position());
4276 }
4277 
4278 template <typename Impl>
4279 typename ParserBase<Impl>::ExpressionT
4280 ParserBase<Impl>::ParseAsyncFunctionLiteral() {
4281  // AsyncFunctionLiteral ::
4282  // async [no LineTerminator here] function ( FormalParameters[Await] )
4283  // { AsyncFunctionBody }
4284  //
4285  // async [no LineTerminator here] function BindingIdentifier[Await]
4286  // ( FormalParameters[Await] ) { AsyncFunctionBody }
4287  DCHECK_EQ(scanner()->current_token(), Token::ASYNC);
4288  int pos = position();
4289  Consume(Token::FUNCTION);
4290  bool is_strict_reserved = false;
4291  IdentifierT name = impl()->NullIdentifier();
4292  FunctionLiteral::FunctionType type = FunctionLiteral::kAnonymousExpression;
4293 
4294  ParseFunctionFlags flags = ParseFunctionFlag::kIsAsync;
4295  if (Check(Token::MUL)) flags |= ParseFunctionFlag::kIsGenerator;
4296  const FunctionKind kind = FunctionKindFor(flags);
4297 
4298  if (impl()->ParsingDynamicFunctionDeclaration()) {
4299  // We don't want dynamic functions to actually declare their name
4300  // "anonymous". We just want that name in the toString().
4301 
4302  // Consuming token we did not peek yet, which could lead to a ILLEGAL token
4303  // in the case of a stackoverflow.
4304  Consume(Token::IDENTIFIER);
4305  DCHECK_IMPLIES(!has_error(),
4306  scanner()->CurrentSymbol(ast_value_factory()) ==
4307  ast_value_factory()->anonymous_string());
4308  } else if (peek_any_identifier()) {
4309  type = FunctionLiteral::kNamedExpression;
4310  bool is_await = false;
4311  name = ParseIdentifierOrStrictReservedWord(kind, &is_strict_reserved,
4312  &is_await);
4313  // If the function name is "await", ParseIdentifierOrStrictReservedWord
4314  // recognized the error.
4315  DCHECK(!is_await);
4316  }
4317  FunctionLiteralT result = impl()->ParseFunctionLiteral(
4318  name, scanner()->location(),
4319  is_strict_reserved ? kFunctionNameIsStrictReserved
4320  : kFunctionNameValidityUnknown,
4321  kind, pos, type, language_mode(), nullptr);
4322  if (impl()->IsNull(result)) return impl()->FailureExpression();
4323  return result;
4324 }
4325 
4326 template <typename Impl>
4327 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseTemplateLiteral(
4328  ExpressionT tag, int start, bool tagged) {
4329  // A TemplateLiteral is made up of 0 or more TEMPLATE_SPAN tokens (literal
4330  // text followed by a substitution expression), finalized by a single
4331  // TEMPLATE_TAIL.
4332  //
4333  // In terms of draft language, TEMPLATE_SPAN may be either the TemplateHead or
4334  // TemplateMiddle productions, while TEMPLATE_TAIL is either TemplateTail, or
4335  // NoSubstitutionTemplate.
4336  //
4337  // When parsing a TemplateLiteral, we must have scanned either an initial
4338  // TEMPLATE_SPAN, or a TEMPLATE_TAIL.
4339  DCHECK(peek() == Token::TEMPLATE_SPAN || peek() == Token::TEMPLATE_TAIL);
4340 
4341  if (tagged) {
4342  // TaggedTemplate expressions prevent the eval compilation cache from being
4343  // used. This flag is only used if an eval is being parsed.
4344  set_allow_eval_cache(false);
4345  }
4346 
4347  bool forbid_illegal_escapes = !tagged;
4348 
4349  // If we reach a TEMPLATE_TAIL first, we are parsing a NoSubstitutionTemplate.
4350  // In this case we may simply consume the token and build a template with a
4351  // single TEMPLATE_SPAN and no expressions.
4352  if (peek() == Token::TEMPLATE_TAIL) {
4353  Consume(Token::TEMPLATE_TAIL);
4354  int pos = position();
4355  typename Impl::TemplateLiteralState ts = impl()->OpenTemplateLiteral(pos);
4356  bool is_valid = CheckTemplateEscapes(forbid_illegal_escapes);
4357  impl()->AddTemplateSpan(&ts, is_valid, true);
4358  return impl()->CloseTemplateLiteral(&ts, start, tag);
4359  }
4360 
4361  Consume(Token::TEMPLATE_SPAN);
4362  int pos = position();
4363  typename Impl::TemplateLiteralState ts = impl()->OpenTemplateLiteral(pos);
4364  bool is_valid = CheckTemplateEscapes(forbid_illegal_escapes);
4365  impl()->AddTemplateSpan(&ts, is_valid, false);
4366  Token::Value next;
4367 
4368  // If we open with a TEMPLATE_SPAN, we must scan the subsequent expression,
4369  // and repeat if the following token is a TEMPLATE_SPAN as well (in this
4370  // case, representing a TemplateMiddle).
4371 
4372  do {
4373  next = peek();
4374 
4375  int expr_pos = peek_position();
4376  AcceptINScope scope(this, true);
4377  ExpressionT expression = ParseExpressionCoverGrammar();
4378  impl()->AddTemplateExpression(&ts, expression);
4379 
4380  if (peek() != Token::RBRACE) {
4381  impl()->ReportMessageAt(Scanner::Location(expr_pos, peek_position()),
4382  MessageTemplate::kUnterminatedTemplateExpr);
4383  return impl()->FailureExpression();
4384  }
4385 
4386  // If we didn't die parsing that expression, our next token should be a
4387  // TEMPLATE_SPAN or TEMPLATE_TAIL.
4388  next = scanner()->ScanTemplateContinuation();
4389  Next();
4390  pos = position();
4391 
4392  bool is_valid = CheckTemplateEscapes(forbid_illegal_escapes);
4393  impl()->AddTemplateSpan(&ts, is_valid, next == Token::TEMPLATE_TAIL);
4394  } while (next == Token::TEMPLATE_SPAN);
4395 
4396  DCHECK_IMPLIES(!has_error(), next == Token::TEMPLATE_TAIL);
4397  // Once we've reached a TEMPLATE_TAIL, we can close the TemplateLiteral.
4398  return impl()->CloseTemplateLiteral(&ts, start, tag);
4399 }
4400 
4401 template <typename Impl>
4402 typename ParserBase<Impl>::ExpressionT
4403 ParserBase<Impl>::RewriteInvalidReferenceExpression(ExpressionT expression,
4404  int beg_pos, int end_pos,
4405  MessageTemplate message) {
4406  return RewriteInvalidReferenceExpression(expression, beg_pos, end_pos,
4407  message, kReferenceError);
4408 }
4409 
4410 template <typename Impl>
4411 typename ParserBase<Impl>::ExpressionT
4412 ParserBase<Impl>::RewriteInvalidReferenceExpression(ExpressionT expression,
4413  int beg_pos, int end_pos,
4414  MessageTemplate message,
4415  ParseErrorType type) {
4416  DCHECK(!IsValidReferenceExpression(expression));
4417  if (impl()->IsIdentifier(expression)) {
4418  DCHECK(is_strict(language_mode()));
4419  DCHECK(impl()->IsEvalOrArguments(impl()->AsIdentifier(expression)));
4420 
4421  ReportMessageAt(Scanner::Location(beg_pos, end_pos),
4422  MessageTemplate::kStrictEvalArguments, kSyntaxError);
4423  return impl()->FailureExpression();
4424  }
4425  if (expression->IsCall() && !expression->AsCall()->is_tagged_template()) {
4426  // If it is a call, make it a runtime error for legacy web compatibility.
4427  // Bug: https://bugs.chromium.org/p/v8/issues/detail?id=4480
4428  // Rewrite `expr' to `expr[throw ReferenceError]'.
4429  impl()->CountUsage(
4430  is_strict(language_mode())
4431  ? v8::Isolate::kAssigmentExpressionLHSIsCallInStrict
4432  : v8::Isolate::kAssigmentExpressionLHSIsCallInSloppy);
4433  ExpressionT error = impl()->NewThrowReferenceError(message, beg_pos);
4434  return factory()->NewProperty(expression, error, beg_pos);
4435  }
4436  ReportMessageAt(Scanner::Location(beg_pos, end_pos), message, type);
4437  return impl()->FailureExpression();
4438 }
4439 
4440 template <typename Impl>
4441 void ParserBase<Impl>::CheckArrowFormalParameter(ExpressionT formal) {
4442  if (formal->is_parenthesized() ||
4443  !(impl()->IsIdentifier(formal) || formal->IsPattern() ||
4444  formal->IsAssignment())) {
4445  classifier()->RecordBindingPatternError(
4446  Scanner::Location(formal->position(), end_position()),
4447  MessageTemplate::kInvalidDestructuringTarget);
4448  } else if (!impl()->IsIdentifier(formal)) {
4449  classifier()->RecordNonSimpleParameter();
4450  }
4451 }
4452 
4453 template <typename Impl>
4454 bool ParserBase<Impl>::IsValidReferenceExpression(ExpressionT expression) {
4455  return IsAssignableIdentifier(expression) ||
4456  (expression->IsProperty() && classifier()->is_valid_expression());
4457 }
4458 
4459 template <typename Impl>
4460 void ParserBase<Impl>::CheckDestructuringElement(ExpressionT expression,
4461  int begin, int end) {
4462  if (IsValidReferenceExpression(expression)) {
4463  // Parenthesized identifiers and property references are allowed as part of
4464  // a larger assignment pattern, even though parenthesized patterns
4465  // themselves are not allowed, e.g., "[(x)] = []". Only accumulate
4466  // assignment pattern errors if the parsed expression is more complex.
4467  if (expression->IsProperty()) {
4468  classifier()->RecordBindingPatternError(
4469  Scanner::Location(begin, end),
4470  MessageTemplate::kInvalidPropertyBindingPattern);
4471  }
4472  return;
4473  }
4474  if (expression->is_parenthesized() ||
4475  (!expression->IsPattern() && !expression->IsAssignment())) {
4476  classifier()->RecordPatternError(
4477  Scanner::Location(begin, end),
4478  MessageTemplate::kInvalidDestructuringTarget);
4479  }
4480 }
4481 
4482 template <typename Impl>
4483 typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseV8Intrinsic() {
4484  // CallRuntime ::
4485  // '%' Identifier Arguments
4486 
4487  int pos = peek_position();
4488  Consume(Token::MOD);
4489  // Allow "eval" or "arguments" for backward compatibility.
4490  IdentifierT name = ParseIdentifier(kAllowRestrictedIdentifiers);
4491  if (peek() != Token::LPAREN) {
4492  impl()->ReportUnexpectedToken(peek());
4493  return impl()->FailureExpression();
4494  }
4495  bool has_spread;
4496  ExpressionListT args(pointer_buffer());
4497  ParseArguments(&args, &has_spread);
4498 
4499  if (has_spread) {
4500  ReportMessageAt(Scanner::Location(pos, position()),
4501  MessageTemplate::kIntrinsicWithSpread, kSyntaxError);
4502  return impl()->FailureExpression();
4503  }
4504 
4505  return impl()->NewV8Intrinsic(name, args, pos);
4506 }
4507 
4508 template <typename Impl>
4509 typename ParserBase<Impl>::LazyParsingResult
4510 ParserBase<Impl>::ParseStatementList(StatementListT* body,
4511  Token::Value end_token, bool may_abort) {
4512  // StatementList ::
4513  // (StatementListItem)* <end_token>
4514  DCHECK_NOT_NULL(body);
4515 
4516  while (peek() == Token::STRING) {
4517  bool use_strict = false;
4518  bool use_asm = false;
4519 
4520  Scanner::Location token_loc = scanner()->peek_location();
4521 
4522  if (scanner()->NextLiteralEquals("use strict")) {
4523  use_strict = true;
4524  } else if (scanner()->NextLiteralEquals("use asm")) {
4525  use_asm = true;
4526  }
4527 
4528  StatementT stat = ParseStatementListItem();
4529  if (impl()->IsNull(stat)) return kLazyParsingComplete;
4530 
4531  body->Add(stat);
4532  may_abort = false;
4533 
4534  if (!impl()->IsStringLiteral(stat)) break;
4535 
4536  if (use_strict) {
4537  // Directive "use strict" (ES5 14.1).
4538  RaiseLanguageMode(LanguageMode::kStrict);
4539  if (!scope()->HasSimpleParameters()) {
4540  // TC39 deemed "use strict" directives to be an error when occurring
4541  // in the body of a function with non-simple parameter list, on
4542  // 29/7/2015. https://goo.gl/ueA7Ln
4543  impl()->ReportMessageAt(token_loc,
4544  MessageTemplate::kIllegalLanguageModeDirective,
4545  "use strict");
4546  return kLazyParsingComplete;
4547  }
4548  } else if (use_asm) {
4549  // Directive "use asm".
4550  impl()->SetAsmModule();
4551  } else {
4552  // Possibly an unknown directive.
4553  // Should not change mode, but will increment usage counters
4554  // as appropriate. Ditto usages below.
4555  RaiseLanguageMode(LanguageMode::kSloppy);
4556  }
4557  }
4558 
4559  // Allocate a target stack to use for this set of source elements. This way,
4560  // all scripts and functions get their own target stack thus avoiding illegal
4561  // breaks and continues across functions.
4562  TargetScopeT target_scope(this);
4563  int count_statements = 0;
4564 
4565  if (may_abort) {
4566  while (peek() == Token::IDENTIFIER) {
4567  StatementT stat = ParseStatementListItem();
4568  // If we're allowed to abort, we will do so when we see a "long and
4569  // trivial" function. Our current definition of "long and trivial" is:
4570  // - over kLazyParseTrialLimit statements
4571  // - all starting with an identifier (i.e., no if, for, while, etc.)
4572  if (++count_statements > kLazyParseTrialLimit) return kLazyParsingAborted;
4573  body->Add(stat);
4574  }
4575  }
4576 
4577  while (peek() != end_token) {
4578  StatementT stat = ParseStatementListItem();
4579  if (impl()->IsNull(stat)) return kLazyParsingComplete;
4580  if (stat->IsEmptyStatement()) continue;
4581  body->Add(stat);
4582  }
4583 
4584  return kLazyParsingComplete;
4585 }
4586 
4587 template <typename Impl>
4588 typename ParserBase<Impl>::StatementT
4589 ParserBase<Impl>::ParseStatementListItem() {
4590  // ECMA 262 6th Edition
4591  // StatementListItem[Yield, Return] :
4592  // Statement[?Yield, ?Return]
4593  // Declaration[?Yield]
4594  //
4595  // Declaration[Yield] :
4596  // HoistableDeclaration[?Yield]
4597  // ClassDeclaration[?Yield]
4598  // LexicalDeclaration[In, ?Yield]
4599  //
4600  // HoistableDeclaration[Yield, Default] :
4601  // FunctionDeclaration[?Yield, ?Default]
4602  // GeneratorDeclaration[?Yield, ?Default]
4603  //
4604  // LexicalDeclaration[In, Yield] :
4605  // LetOrConst BindingList[?In, ?Yield] ;
4606 
4607  switch (peek()) {
4608  case Token::FUNCTION:
4609  return ParseHoistableDeclaration(nullptr, false);
4610  case Token::CLASS:
4611  Consume(Token::CLASS);
4612  return ParseClassDeclaration(nullptr, false);
4613  case Token::VAR:
4614  case Token::CONST:
4615  return ParseVariableStatement(kStatementListItem, nullptr);
4616  case Token::LET:
4617  if (IsNextLetKeyword()) {
4618  return ParseVariableStatement(kStatementListItem, nullptr);
4619  }
4620  break;
4621  case Token::ASYNC:
4622  if (PeekAhead() == Token::FUNCTION &&
4623  !scanner()->HasLineTerminatorAfterNext()) {
4624  Consume(Token::ASYNC);
4625  return ParseAsyncFunctionDeclaration(nullptr, false);
4626  }
4627  break;
4628  default:
4629  break;
4630  }
4631  return ParseStatement(nullptr, nullptr, kAllowLabelledFunctionStatement);
4632 }
4633 
4634 template <typename Impl>
4635 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseStatement(
4636  ZonePtrList<const AstRawString>* labels,
4637  ZonePtrList<const AstRawString>* own_labels,
4638  AllowLabelledFunctionStatement allow_function) {
4639  // Statement ::
4640  // Block
4641  // VariableStatement
4642  // EmptyStatement
4643  // ExpressionStatement
4644  // IfStatement
4645  // IterationStatement
4646  // ContinueStatement
4647  // BreakStatement
4648  // ReturnStatement
4649  // WithStatement
4650  // LabelledStatement
4651  // SwitchStatement
4652  // ThrowStatement
4653  // TryStatement
4654  // DebuggerStatement
4655 
4656  // {own_labels} is always a subset of {labels}.
4657  DCHECK_IMPLIES(labels == nullptr, own_labels == nullptr);
4658 
4659  // Note: Since labels can only be used by 'break' and 'continue'
4660  // statements, which themselves are only valid within blocks,
4661  // iterations or 'switch' statements (i.e., BreakableStatements),
4662  // labels can be simply ignored in all other cases; except for
4663  // trivial labeled break statements 'label: break label' which is
4664  // parsed into an empty statement.
4665  switch (peek()) {
4666  case Token::LBRACE:
4667  return ParseBlock(labels);
4668  case Token::SEMICOLON:
4669  Next();
4670  return factory()->EmptyStatement();
4671  case Token::IF:
4672  return ParseIfStatement(labels);
4673  case Token::DO:
4674  return ParseDoWhileStatement(labels, own_labels);
4675  case Token::WHILE:
4676  return ParseWhileStatement(labels, own_labels);
4677  case Token::FOR:
4678  if (V8_UNLIKELY(is_async_function() && PeekAhead() == Token::AWAIT)) {
4679  return ParseForAwaitStatement(labels, own_labels);
4680  }
4681  return ParseForStatement(labels, own_labels);
4682  case Token::CONTINUE:
4683  return ParseContinueStatement();
4684  case Token::BREAK:
4685  return ParseBreakStatement(labels);
4686  case Token::RETURN:
4687  return ParseReturnStatement();
4688  case Token::THROW:
4689  return ParseThrowStatement();
4690  case Token::TRY: {
4691  // It is somewhat complicated to have labels on try-statements.
4692  // When breaking out of a try-finally statement, one must take
4693  // great care not to treat it as a fall-through. It is much easier
4694  // just to wrap the entire try-statement in a statement block and
4695  // put the labels there.
4696  if (labels == nullptr) return ParseTryStatement();
4697  StatementListT statements(pointer_buffer());
4698  BlockT result = factory()->NewBlock(false, labels);
4699  TargetT target(this, result);
4700  StatementT statement = ParseTryStatement();
4701  statements.Add(statement);
4702  result->InitializeStatements(statements, zone());
4703  return result;
4704  }
4705  case Token::WITH:
4706  return ParseWithStatement(labels);
4707  case Token::SWITCH:
4708  return ParseSwitchStatement(labels);
4709  case Token::FUNCTION:
4710  // FunctionDeclaration only allowed as a StatementListItem, not in
4711  // an arbitrary Statement position. Exceptions such as
4712  // ES#sec-functiondeclarations-in-ifstatement-statement-clauses
4713  // are handled by calling ParseScopedStatement rather than
4714  // ParseStatement directly.
4715  impl()->ReportMessageAt(scanner()->peek_location(),
4716  is_strict(language_mode())
4717  ? MessageTemplate::kStrictFunction
4718  : MessageTemplate::kSloppyFunction);
4719  return impl()->NullStatement();
4720  case Token::DEBUGGER:
4721  return ParseDebuggerStatement();
4722  case Token::VAR:
4723  return ParseVariableStatement(kStatement, nullptr);
4724  case Token::ASYNC:
4725  if (!scanner()->HasLineTerminatorAfterNext() &&
4726  PeekAhead() == Token::FUNCTION) {
4727  impl()->ReportMessageAt(
4728  scanner()->peek_location(),
4729  MessageTemplate::kAsyncFunctionInSingleStatementContext);
4730  return impl()->NullStatement();
4731  }
4732  V8_FALLTHROUGH;
4733  default:
4734  return ParseExpressionOrLabelledStatement(labels, own_labels,
4735  allow_function);
4736  }
4737 }
4738 
4739 template <typename Impl>
4740 typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseBlock(
4741  ZonePtrList<const AstRawString>* labels) {
4742  // Block ::
4743  // '{' StatementList '}'
4744 
4745  BlockT body = factory()->NewBlock(false, labels);
4746  StatementListT statements(pointer_buffer());
4747 
4748  // Parse the statements and collect escaping labels.
4749  Expect(Token::LBRACE);
4750 
4751  CheckStackOverflow();
4752 
4753  {
4754  BlockState block_state(zone(), &scope_);
4755  scope()->set_start_position(scanner()->location().beg_pos);
4756  TargetT target(this, body);
4757 
4758  while (peek() != Token::RBRACE) {
4759  StatementT stat = ParseStatementListItem();
4760  if (impl()->IsNull(stat)) return body;
4761  if (stat->IsEmptyStatement()) continue;
4762  statements.Add(stat);
4763  }
4764 
4765  Expect(Token::RBRACE);
4766  int end_pos = end_position();
4767  scope()->set_end_position(end_pos);
4768 
4769  impl()->RecordBlockSourceRange(body, end_pos);
4770  body->set_scope(scope()->FinalizeBlockScope());
4771  }
4772 
4773  body->InitializeStatements(statements, zone_);
4774  return body;
4775 }
4776 
4777 template <typename Impl>
4778 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseScopedStatement(
4779  ZonePtrList<const AstRawString>* labels) {
4780  if (is_strict(language_mode()) || peek() != Token::FUNCTION) {
4781  return ParseStatement(labels, nullptr);
4782  } else {
4783  // Make a block around the statement for a lexical binding
4784  // is introduced by a FunctionDeclaration.
4785  BlockState block_state(zone(), &scope_);
4786  scope()->set_start_position(scanner()->location().beg_pos);
4787  BlockT block = factory()->NewBlock(1, false);
4788  StatementT body = ParseFunctionDeclaration();
4789  block->statements()->Add(body, zone());
4790  scope()->set_end_position(end_position());
4791  block->set_scope(scope()->FinalizeBlockScope());
4792  return block;
4793  }
4794 }
4795 
4796 template <typename Impl>
4797 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseVariableStatement(
4798  VariableDeclarationContext var_context,
4799  ZonePtrList<const AstRawString>* names) {
4800  // VariableStatement ::
4801  // VariableDeclarations ';'
4802 
4803  // The scope of a var declared variable anywhere inside a function
4804  // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can
4805  // transform a source-level var declaration into a (Function) Scope
4806  // declaration, and rewrite the source-level initialization into an assignment
4807  // statement. We use a block to collect multiple assignments.
4808  //
4809  // We mark the block as initializer block because we don't want the
4810  // rewriter to add a '.result' assignment to such a block (to get compliant
4811  // behavior for code such as print(eval('var x = 7')), and for cosmetic
4812  // reasons when pretty-printing. Also, unless an assignment (initialization)
4813  // is inside an initializer block, it is ignored.
4814 
4815  DeclarationParsingResult parsing_result;
4816  StatementT result =
4817  ParseVariableDeclarations(var_context, &parsing_result, names);
4818  ExpectSemicolon();
4819  return result;
4820 }
4821 
4822 template <typename Impl>
4823 typename ParserBase<Impl>::StatementT
4824 ParserBase<Impl>::ParseDebuggerStatement() {
4825  // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
4826  // contexts this is used as a statement which invokes the debugger as i a
4827  // break point is present.
4828  // DebuggerStatement ::
4829  // 'debugger' ';'
4830 
4831  int pos = peek_position();
4832  Consume(Token::DEBUGGER);
4833  ExpectSemicolon();
4834  return factory()->NewDebuggerStatement(pos);
4835 }
4836 
4837 template <typename Impl>
4838 typename ParserBase<Impl>::StatementT
4839 ParserBase<Impl>::ParseExpressionOrLabelledStatement(
4840  ZonePtrList<const AstRawString>* labels,
4841  ZonePtrList<const AstRawString>* own_labels,
4842  AllowLabelledFunctionStatement allow_function) {
4843  // ExpressionStatement | LabelledStatement ::
4844  // Expression ';'
4845  // Identifier ':' Statement
4846  //
4847  // ExpressionStatement[Yield] :
4848  // [lookahead notin {{, function, class, let [}] Expression[In, ?Yield] ;
4849 
4850  int pos = peek_position();
4851 
4852  switch (peek()) {
4853  case Token::FUNCTION:
4854  case Token::LBRACE:
4855  UNREACHABLE(); // Always handled by the callers.
4856  case Token::CLASS:
4857  ReportUnexpectedToken(Next());
4858  return impl()->NullStatement();
4859  case Token::LET: {
4860  Token::Value next_next = PeekAhead();
4861  // "let" followed by either "[", "{" or an identifier means a lexical
4862  // declaration, which should not appear here.
4863  // However, ASI may insert a line break before an identifier or a brace.
4864  if (next_next != Token::LBRACK &&
4865  ((next_next != Token::LBRACE && next_next != Token::IDENTIFIER) ||
4866  scanner_->HasLineTerminatorAfterNext())) {
4867  break;
4868  }
4869  impl()->ReportMessageAt(scanner()->peek_location(),
4870  MessageTemplate::kUnexpectedLexicalDeclaration);
4871  return impl()->NullStatement();
4872  }
4873  default:
4874  break;
4875  }
4876 
4877  bool starts_with_identifier = peek_any_identifier();
4878  ExpressionT expr = ParseExpression();
4879  if (peek() == Token::COLON && starts_with_identifier &&
4880  impl()->IsIdentifier(expr)) {
4881  // The whole expression was a single identifier, and not, e.g.,
4882  // something starting with an identifier or a parenthesized identifier.
4883  impl()->DeclareLabel(&labels, &own_labels,
4884  impl()->AsIdentifierExpression(expr));
4885  Consume(Token::COLON);
4886  // ES#sec-labelled-function-declarations Labelled Function Declarations
4887  if (peek() == Token::FUNCTION && is_sloppy(language_mode()) &&
4888  allow_function == kAllowLabelledFunctionStatement) {
4889  return ParseFunctionDeclaration();
4890  }
4891  return ParseStatement(labels, own_labels, allow_function);
4892  }
4893 
4894  // If we have an extension, we allow a native function declaration.
4895  // A native function declaration starts with "native function" with
4896  // no line-terminator between the two words.
4897  if (extension_ != nullptr && peek() == Token::FUNCTION &&
4898  !scanner()->HasLineTerminatorBeforeNext() && impl()->IsNative(expr) &&
4899  !scanner()->literal_contains_escapes()) {
4900  return ParseNativeDeclaration();
4901  }
4902 
4903  // Parsed expression statement, followed by semicolon.
4904  ExpectSemicolon();
4905  if (expr->IsFailureExpression()) return impl()->NullStatement();
4906  return factory()->NewExpressionStatement(expr, pos);
4907 }
4908 
4909 template <typename Impl>
4910 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseIfStatement(
4911  ZonePtrList<const AstRawString>* labels) {
4912  // IfStatement ::
4913  // 'if' '(' Expression ')' Statement ('else' Statement)?
4914 
4915  int pos = peek_position();
4916  Consume(Token::IF);
4917  Expect(Token::LPAREN);
4918  ExpressionT condition = ParseExpression();
4919  Expect(Token::RPAREN);
4920 
4921  SourceRange then_range, else_range;
4922  StatementT then_statement = impl()->NullStatement();
4923  {
4924  SourceRangeScope range_scope(scanner(), &then_range);
4925  then_statement = ParseScopedStatement(labels);
4926  }
4927 
4928  StatementT else_statement = impl()->NullStatement();
4929  if (Check(Token::ELSE)) {
4930  else_statement = ParseScopedStatement(labels);
4931  else_range = SourceRange::ContinuationOf(then_range, end_position());
4932  } else {
4933  else_statement = factory()->EmptyStatement();
4934  }
4935  StatementT stmt =
4936  factory()->NewIfStatement(condition, then_statement, else_statement, pos);
4937  impl()->RecordIfStatementSourceRange(stmt, then_range, else_range);
4938  return stmt;
4939 }
4940 
4941 template <typename Impl>
4942 typename ParserBase<Impl>::StatementT
4943 ParserBase<Impl>::ParseContinueStatement() {
4944  // ContinueStatement ::
4945  // 'continue' Identifier? ';'
4946 
4947  int pos = peek_position();
4948  Consume(Token::CONTINUE);
4949  IdentifierT label = impl()->NullIdentifier();
4950  Token::Value tok = peek();
4951  if (!scanner()->HasLineTerminatorBeforeNext() &&
4952  !Token::IsAutoSemicolon(tok)) {
4953  // ECMA allows "eval" or "arguments" as labels even in strict mode.
4954  label = ParseIdentifier(kAllowRestrictedIdentifiers);
4955  }
4956  IterationStatementT target = impl()->LookupContinueTarget(label);
4957  if (impl()->IsNull(target)) {
4958  // Illegal continue statement.
4959  MessageTemplate message = MessageTemplate::kIllegalContinue;
4960  BreakableStatementT breakable_target = impl()->LookupBreakTarget(label);
4961  if (impl()->IsNull(label)) {
4962  message = MessageTemplate::kNoIterationStatement;
4963  } else if (impl()->IsNull(breakable_target)) {
4964  message = MessageTemplate::kUnknownLabel;
4965  }
4966  ReportMessage(message, label);
4967  return impl()->NullStatement();
4968  }
4969  ExpectSemicolon();
4970  StatementT stmt = factory()->NewContinueStatement(target, pos);
4971  impl()->RecordJumpStatementSourceRange(stmt, end_position());
4972  return stmt;
4973 }
4974 
4975 template <typename Impl>
4976 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseBreakStatement(
4977  ZonePtrList<const AstRawString>* labels) {
4978  // BreakStatement ::
4979  // 'break' Identifier? ';'
4980 
4981  int pos = peek_position();
4982  Consume(Token::BREAK);
4983  IdentifierT label = impl()->NullIdentifier();
4984  Token::Value tok = peek();
4985  if (!scanner()->HasLineTerminatorBeforeNext() &&
4986  !Token::IsAutoSemicolon(tok)) {
4987  // ECMA allows "eval" or "arguments" as labels even in strict mode.
4988  label = ParseIdentifier(kAllowRestrictedIdentifiers);
4989  }
4990  // Parse labeled break statements that target themselves into
4991  // empty statements, e.g. 'l1: l2: l3: break l2;'
4992  if (!impl()->IsNull(label) && impl()->ContainsLabel(labels, label)) {
4993  ExpectSemicolon();
4994  return factory()->EmptyStatement();
4995  }
4996  BreakableStatementT target = impl()->LookupBreakTarget(label);
4997  if (impl()->IsNull(target)) {
4998  // Illegal break statement.
4999  MessageTemplate message = MessageTemplate::kIllegalBreak;
5000  if (!impl()->IsNull(label)) {
5001  message = MessageTemplate::kUnknownLabel;
5002  }
5003  ReportMessage(message, label);
5004  return impl()->NullStatement();
5005  }
5006  ExpectSemicolon();
5007  StatementT stmt = factory()->NewBreakStatement(target, pos);
5008  impl()->RecordJumpStatementSourceRange(stmt, end_position());
5009  return stmt;
5010 }
5011 
5012 template <typename Impl>
5013 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseReturnStatement() {
5014  // ReturnStatement ::
5015  // 'return' [no line terminator] Expression? ';'
5016 
5017  // Consume the return token. It is necessary to do that before
5018  // reporting any errors on it, because of the way errors are
5019  // reported (underlining).
5020  Consume(Token::RETURN);
5021  Scanner::Location loc = scanner()->location();
5022 
5023  switch (GetDeclarationScope()->scope_type()) {
5024  case SCRIPT_SCOPE:
5025  case EVAL_SCOPE:
5026  case MODULE_SCOPE:
5027  impl()->ReportMessageAt(loc, MessageTemplate::kIllegalReturn);
5028  return impl()->NullStatement();
5029  default:
5030  break;
5031  }
5032 
5033  Token::Value tok = peek();
5034  ExpressionT return_value = impl()->NullExpression();
5035  if (scanner()->HasLineTerminatorBeforeNext() || Token::IsAutoSemicolon(tok)) {
5036  if (IsDerivedConstructor(function_state_->kind())) {
5037  return_value = impl()->ThisExpression(loc.beg_pos);
5038  }
5039  } else {
5040  return_value = ParseExpression();
5041  }
5042  ExpectSemicolon();
5043 
5044  return_value = impl()->RewriteReturn(return_value, loc.beg_pos);
5045  int continuation_pos = end_position();
5046  StatementT stmt =
5047  BuildReturnStatement(return_value, loc.beg_pos, continuation_pos);
5048  impl()->RecordJumpStatementSourceRange(stmt, end_position());
5049  return stmt;
5050 }
5051 
5052 template <typename Impl>
5053 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseWithStatement(
5054  ZonePtrList<const AstRawString>* labels) {
5055  // WithStatement ::
5056  // 'with' '(' Expression ')' Statement
5057 
5058  Consume(Token::WITH);
5059  int pos = position();
5060 
5061  if (is_strict(language_mode())) {
5062  ReportMessage(MessageTemplate::kStrictWith);
5063  return impl()->NullStatement();
5064  }
5065 
5066  Expect(Token::LPAREN);
5067  ExpressionT expr = ParseExpression();
5068  Expect(Token::RPAREN);
5069 
5070  Scope* with_scope = NewScope(WITH_SCOPE);
5071  StatementT body = impl()->NullStatement();
5072  {
5073  BlockState block_state(&scope_, with_scope);
5074  with_scope->set_start_position(scanner()->peek_location().beg_pos);
5075  body = ParseStatement(labels, nullptr);
5076  with_scope->set_end_position(end_position());
5077  }
5078  return factory()->NewWithStatement(with_scope, expr, body, pos);
5079 }
5080 
5081 template <typename Impl>
5082 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseDoWhileStatement(
5083  ZonePtrList<const AstRawString>* labels,
5084  ZonePtrList<const AstRawString>* own_labels) {
5085  // DoStatement ::
5086  // 'do' Statement 'while' '(' Expression ')' ';'
5087  auto loop =
5088  factory()->NewDoWhileStatement(labels, own_labels, peek_position());
5089  TargetT target(this, loop);
5090 
5091  SourceRange body_range;
5092  StatementT body = impl()->NullStatement();
5093 
5094  Consume(Token::DO);
5095 
5096  CheckStackOverflow();
5097  {
5098  SourceRangeScope range_scope(scanner(), &body_range);
5099  body = ParseStatement(nullptr, nullptr);
5100  }
5101  Expect(Token::WHILE);
5102  Expect(Token::LPAREN);
5103 
5104  ExpressionT cond = ParseExpression();
5105  Expect(Token::RPAREN);
5106 
5107  // Allow do-statements to be terminated with and without
5108  // semi-colons. This allows code such as 'do;while(0)return' to
5109  // parse, which would not be the case if we had used the
5110  // ExpectSemicolon() functionality here.
5111  Check(Token::SEMICOLON);
5112 
5113  loop->Initialize(cond, body);
5114  impl()->RecordIterationStatementSourceRange(loop, body_range);
5115 
5116  return loop;
5117 }
5118 
5119 template <typename Impl>
5120 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseWhileStatement(
5121  ZonePtrList<const AstRawString>* labels,
5122  ZonePtrList<const AstRawString>* own_labels) {
5123  // WhileStatement ::
5124  // 'while' '(' Expression ')' Statement
5125 
5126  auto loop = factory()->NewWhileStatement(labels, own_labels, peek_position());
5127  TargetT target(this, loop);
5128 
5129  SourceRange body_range;
5130  StatementT body = impl()->NullStatement();
5131 
5132  Consume(Token::WHILE);
5133  Expect(Token::LPAREN);
5134  ExpressionT cond = ParseExpression();
5135  Expect(Token::RPAREN);
5136  {
5137  SourceRangeScope range_scope(scanner(), &body_range);
5138  body = ParseStatement(nullptr, nullptr);
5139  }
5140 
5141  loop->Initialize(cond, body);
5142  impl()->RecordIterationStatementSourceRange(loop, body_range);
5143 
5144  return loop;
5145 }
5146 
5147 template <typename Impl>
5148 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseThrowStatement() {
5149  // ThrowStatement ::
5150  // 'throw' Expression ';'
5151 
5152  Consume(Token::THROW);
5153  int pos = position();
5154  if (scanner()->HasLineTerminatorBeforeNext()) {
5155  ReportMessage(MessageTemplate::kNewlineAfterThrow);
5156  return impl()->NullStatement();
5157  }
5158  ExpressionT exception = ParseExpression();
5159  ExpectSemicolon();
5160 
5161  StatementT stmt = impl()->NewThrowStatement(exception, pos);
5162  impl()->RecordThrowSourceRange(stmt, end_position());
5163 
5164  return stmt;
5165 }
5166 
5167 template <typename Impl>
5168 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseSwitchStatement(
5169  ZonePtrList<const AstRawString>* labels) {
5170  // SwitchStatement ::
5171  // 'switch' '(' Expression ')' '{' CaseClause* '}'
5172  // CaseClause ::
5173  // 'case' Expression ':' StatementList
5174  // 'default' ':' StatementList
5175 
5176  int switch_pos = peek_position();
5177 
5178  Consume(Token::SWITCH);
5179  Expect(Token::LPAREN);
5180  ExpressionT tag = ParseExpression();
5181  Expect(Token::RPAREN);
5182 
5183  auto switch_statement =
5184  factory()->NewSwitchStatement(labels, tag, switch_pos);
5185 
5186  {
5187  BlockState cases_block_state(zone(), &scope_);
5188  scope()->set_start_position(switch_pos);
5189  scope()->SetNonlinear();
5190  TargetT target(this, switch_statement);
5191 
5192  bool default_seen = false;
5193  Expect(Token::LBRACE);
5194  while (peek() != Token::RBRACE) {
5195  // An empty label indicates the default case.
5196  ExpressionT label = impl()->NullExpression();
5197  StatementListT statements(pointer_buffer());
5198  SourceRange clause_range;
5199  {
5200  SourceRangeScope range_scope(scanner(), &clause_range);
5201  if (Check(Token::CASE)) {
5202  label = ParseExpression();
5203  } else {
5204  Expect(Token::DEFAULT);
5205  if (default_seen) {
5206  ReportMessage(MessageTemplate::kMultipleDefaultsInSwitch);
5207  return impl()->NullStatement();
5208  }
5209  default_seen = true;
5210  }
5211  Expect(Token::COLON);
5212  while (peek() != Token::CASE && peek() != Token::DEFAULT &&
5213  peek() != Token::RBRACE) {
5214  StatementT stat = ParseStatementListItem();
5215  if (impl()->IsNull(stat)) return stat;
5216  if (stat->IsEmptyStatement()) continue;
5217  statements.Add(stat);
5218  }
5219  }
5220  auto clause = factory()->NewCaseClause(label, statements);
5221  impl()->RecordCaseClauseSourceRange(clause, clause_range);
5222  switch_statement->cases()->Add(clause, zone());
5223  }
5224  Expect(Token::RBRACE);
5225 
5226  int end_pos = end_position();
5227  scope()->set_end_position(end_pos);
5228  impl()->RecordSwitchStatementSourceRange(switch_statement, end_pos);
5229  Scope* switch_scope = scope()->FinalizeBlockScope();
5230  if (switch_scope != nullptr) {
5231  return impl()->RewriteSwitchStatement(switch_statement, switch_scope);
5232  }
5233  return switch_statement;
5234  }
5235 }
5236 
5237 template <typename Impl>
5238 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseTryStatement() {
5239  // TryStatement ::
5240  // 'try' Block Catch
5241  // 'try' Block Finally
5242  // 'try' Block Catch Finally
5243  //
5244  // Catch ::
5245  // 'catch' '(' Identifier ')' Block
5246  //
5247  // Finally ::
5248  // 'finally' Block
5249 
5250  Consume(Token::TRY);
5251  int pos = position();
5252 
5253  BlockT try_block = ParseBlock(nullptr);
5254 
5255  CatchInfo catch_info(this);
5256 
5257  if (peek() != Token::CATCH && peek() != Token::FINALLY) {
5258  ReportMessage(MessageTemplate::kNoCatchOrFinally);
5259  return impl()->NullStatement();
5260  }
5261 
5262  SourceRange catch_range, finally_range;
5263 
5264  BlockT catch_block = impl()->NullStatement();
5265  {
5266  SourceRangeScope catch_range_scope(scanner(), &catch_range);
5267  if (Check(Token::CATCH)) {
5268  bool has_binding;
5269  has_binding = Check(Token::LPAREN);
5270 
5271  if (has_binding) {
5272  catch_info.scope = NewScope(CATCH_SCOPE);
5273  catch_info.scope->set_start_position(scanner()->location().beg_pos);
5274 
5275  {
5276  BlockState catch_block_state(&scope_, catch_info.scope);
5277  StatementListT catch_statements(pointer_buffer());
5278 
5279  // Create a block scope to hold any lexical declarations created
5280  // as part of destructuring the catch parameter.
5281  {
5282  BlockState catch_variable_block_state(zone(), &scope_);
5283  scope()->set_start_position(scanner()->location().beg_pos);
5284 
5285  // This does not simply call ParsePrimaryExpression to avoid
5286  // ExpressionFromIdentifier from being called in the first
5287  // branch, which would introduce an unresolved symbol and mess
5288  // with arrow function names.
5289  if (peek_any_identifier()) {
5290  catch_info.name =
5291  ParseIdentifier(kDontAllowRestrictedIdentifiers);
5292  } else {
5293  ExpressionClassifier pattern_classifier(this);
5294  catch_info.pattern = ParseBindingPattern();
5295  }
5296 
5297  Expect(Token::RPAREN);
5298  RETURN_IF_PARSE_ERROR;
5299  impl()->RewriteCatchPattern(&catch_info);
5300  if (!impl()->IsNull(catch_info.init_block)) {
5301  catch_statements.Add(catch_info.init_block);
5302  }
5303 
5304  catch_info.inner_block = ParseBlock(nullptr);
5305  catch_statements.Add(catch_info.inner_block);
5306  RETURN_IF_PARSE_ERROR;
5307  impl()->ValidateCatchBlock(catch_info);
5308  scope()->set_end_position(end_position());
5309  catch_block = factory()->NewBlock(false, catch_statements);
5310  catch_block->set_scope(scope()->FinalizeBlockScope());
5311  }
5312  }
5313 
5314  catch_info.scope->set_end_position(end_position());
5315  } else {
5316  catch_block = ParseBlock(nullptr);
5317  }
5318  }
5319  }
5320 
5321  BlockT finally_block = impl()->NullStatement();
5322  DCHECK(has_error() || peek() == Token::FINALLY ||
5323  !impl()->IsNull(catch_block));
5324  {
5325  SourceRangeScope range_scope(scanner(), &finally_range);
5326  if (Check(Token::FINALLY)) {
5327  finally_block = ParseBlock(nullptr);
5328  }
5329  }
5330 
5331  RETURN_IF_PARSE_ERROR;
5332  return impl()->RewriteTryStatement(try_block, catch_block, catch_range,
5333  finally_block, finally_range, catch_info,
5334  pos);
5335 }
5336 
5337 template <typename Impl>
5338 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseForStatement(
5339  ZonePtrList<const AstRawString>* labels,
5340  ZonePtrList<const AstRawString>* own_labels) {
5341  // Either a standard for loop
5342  // for (<init>; <cond>; <next>) { ... }
5343  // or a for-each loop
5344  // for (<each> of|in <iterable>) { ... }
5345  //
5346  // We parse a declaration/expression after the 'for (' and then read the first
5347  // expression/declaration before we know if this is a for or a for-each.
5348 
5349  int stmt_pos = peek_position();
5350  ForInfo for_info(this);
5351 
5352  Consume(Token::FOR);
5353  Expect(Token::LPAREN);
5354 
5355  if (peek() == Token::CONST || (peek() == Token::LET && IsNextLetKeyword())) {
5356  // The initializer contains lexical declarations,
5357  // so create an in-between scope.
5358  BlockState for_state(zone(), &scope_);
5359  scope()->set_start_position(scanner()->location().beg_pos);
5360 
5361  // Also record whether inner functions or evals are found inside
5362  // this loop, as this information is used to simplify the desugaring
5363  // if none are found.
5364  typename FunctionState::FunctionOrEvalRecordingScope recording_scope(
5365  function_state_);
5366 
5367  // Create an inner block scope which will be the parent scope of scopes
5368  // possibly created by ParseVariableDeclarations.
5369  Scope* inner_block_scope = NewScope(BLOCK_SCOPE);
5370  {
5371  BlockState inner_state(&scope_, inner_block_scope);
5372  ParseVariableDeclarations(kForStatement, &for_info.parsing_result,
5373  nullptr);
5374  }
5375  DCHECK(IsLexicalVariableMode(for_info.parsing_result.descriptor.mode));
5376  for_info.position = scanner()->location().beg_pos;
5377 
5378  if (CheckInOrOf(&for_info.mode)) {
5379  scope()->set_is_hidden();
5380  return ParseForEachStatementWithDeclarations(
5381  stmt_pos, &for_info, labels, own_labels, inner_block_scope);
5382  }
5383 
5384  Expect(Token::SEMICOLON);
5385 
5386  StatementT init = impl()->BuildInitializationBlock(&for_info.parsing_result,
5387  &for_info.bound_names);
5388 
5389  Scope* finalized = inner_block_scope->FinalizeBlockScope();
5390  // No variable declarations will have been created in inner_block_scope.
5391  DCHECK_NULL(finalized);
5392  USE(finalized);
5393  return ParseStandardForLoopWithLexicalDeclarations(
5394  stmt_pos, init, &for_info, labels, own_labels);
5395  }
5396 
5397  StatementT init = impl()->NullStatement();
5398  if (peek() == Token::VAR) {
5399  ParseVariableDeclarations(kForStatement, &for_info.parsing_result, nullptr);
5400  DCHECK_EQ(for_info.parsing_result.descriptor.mode, VariableMode::kVar);
5401  for_info.position = scanner()->location().beg_pos;
5402 
5403  if (CheckInOrOf(&for_info.mode)) {
5404  return ParseForEachStatementWithDeclarations(stmt_pos, &for_info, labels,
5405  own_labels, nullptr);
5406  }
5407 
5408  init = impl()->BuildInitializationBlock(&for_info.parsing_result, nullptr);
5409  } else if (peek() != Token::SEMICOLON) {
5410  // The initializer does not contain declarations.
5411  int lhs_beg_pos = peek_position();
5412  ExpressionClassifier classifier(this);
5413  ExpressionT expression;
5414  {
5415  AcceptINScope scope(this, false);
5416  expression = ParseExpressionCoverGrammar();
5417  }
5418  int lhs_end_pos = end_position();
5419 
5420  bool is_for_each = CheckInOrOf(&for_info.mode);
5421  bool is_destructuring = is_for_each && expression->IsPattern();
5422 
5423  if (is_destructuring) {
5424  ValidatePattern(expression);
5425  } else {
5426  ValidateExpression();
5427  }
5428 
5429  if (is_for_each) {
5430  return ParseForEachStatementWithoutDeclarations(
5431  stmt_pos, expression, lhs_beg_pos, lhs_end_pos, &for_info, labels,
5432  own_labels);
5433  }
5434  // Initializer is just an expression.
5435  init = factory()->NewExpressionStatement(expression, lhs_beg_pos);
5436  }
5437 
5438  Expect(Token::SEMICOLON);
5439 
5440  // Standard 'for' loop, we have parsed the initializer at this point.
5441  ExpressionT cond = impl()->NullExpression();
5442  StatementT next = impl()->NullStatement();
5443  StatementT body = impl()->NullStatement();
5444  ForStatementT loop =
5445  ParseStandardForLoop(stmt_pos, labels, own_labels, &cond, &next, &body);
5446  RETURN_IF_PARSE_ERROR;
5447  loop->Initialize(init, cond, next, body);
5448  return loop;
5449 }
5450 
5451 template <typename Impl>
5452 typename ParserBase<Impl>::StatementT
5453 ParserBase<Impl>::ParseForEachStatementWithDeclarations(
5454  int stmt_pos, ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
5455  ZonePtrList<const AstRawString>* own_labels, Scope* inner_block_scope) {
5456  // Just one declaration followed by in/of.
5457  if (for_info->parsing_result.declarations.size() != 1) {
5458  impl()->ReportMessageAt(for_info->parsing_result.bindings_loc,
5459  MessageTemplate::kForInOfLoopMultiBindings,
5460  ForEachStatement::VisitModeString(for_info->mode));
5461  return impl()->NullStatement();
5462  }
5463  if (for_info->parsing_result.first_initializer_loc.IsValid() &&
5464  (is_strict(language_mode()) ||
5465  for_info->mode == ForEachStatement::ITERATE ||
5466  IsLexicalVariableMode(for_info->parsing_result.descriptor.mode) ||
5467  !impl()->IsIdentifier(
5468  for_info->parsing_result.declarations[0].pattern))) {
5469  impl()->ReportMessageAt(for_info->parsing_result.first_initializer_loc,
5470  MessageTemplate::kForInOfLoopInitializer,
5471  ForEachStatement::VisitModeString(for_info->mode));
5472  return impl()->NullStatement();
5473  }
5474 
5475  // Reset the declaration_kind to ensure proper processing during declaration.
5476  for_info->parsing_result.descriptor.declaration_kind =
5477  DeclarationDescriptor::FOR_EACH;
5478 
5479  BlockT init_block = impl()->RewriteForVarInLegacy(*for_info);
5480 
5481  auto loop = factory()->NewForEachStatement(for_info->mode, labels, own_labels,
5482  stmt_pos);
5483  TargetT target(this, loop);
5484 
5485  ExpressionT enumerable = impl()->NullExpression();
5486  if (for_info->mode == ForEachStatement::ITERATE) {
5487  ExpressionClassifier classifier(this);
5488  AcceptINScope scope(this, true);
5489  enumerable = ParseAssignmentExpression();
5490  ValidateExpression();
5491  } else {
5492  enumerable = ParseExpression();
5493  }
5494 
5495  Expect(Token::RPAREN);
5496 
5497  Scope* for_scope = nullptr;
5498  if (inner_block_scope != nullptr) {
5499  for_scope = inner_block_scope->outer_scope();
5500  DCHECK_EQ(for_scope, scope());
5501  inner_block_scope->set_start_position(scanner()->location().beg_pos);
5502  }
5503 
5504  ExpressionT each_variable = impl()->NullExpression();
5505  BlockT body_block = impl()->NullStatement();
5506  {
5507  BlockState block_state(
5508  &scope_, inner_block_scope != nullptr ? inner_block_scope : scope_);
5509 
5510  SourceRange body_range;
5511  StatementT body = impl()->NullStatement();
5512  {
5513  SourceRangeScope range_scope(scanner(), &body_range);
5514  body = ParseStatement(nullptr, nullptr);
5515  }
5516  impl()->RecordIterationStatementSourceRange(loop, body_range);
5517 
5518  impl()->DesugarBindingInForEachStatement(for_info, &body_block,
5519  &each_variable);
5520  body_block->statements()->Add(body, zone());
5521 
5522  if (inner_block_scope != nullptr) {
5523  inner_block_scope->set_end_position(end_position());
5524  body_block->set_scope(inner_block_scope->FinalizeBlockScope());
5525  }
5526  }
5527 
5528  StatementT final_loop = impl()->InitializeForEachStatement(
5529  loop, each_variable, enumerable, body_block);
5530 
5531  init_block = impl()->CreateForEachStatementTDZ(init_block, *for_info);
5532 
5533  if (for_scope != nullptr) {
5534  for_scope->set_end_position(end_position());
5535  for_scope = for_scope->FinalizeBlockScope();
5536  }
5537 
5538  // Parsed for-in loop w/ variable declarations.
5539  if (!impl()->IsNull(init_block)) {
5540  init_block->statements()->Add(final_loop, zone());
5541  init_block->set_scope(for_scope);
5542  return init_block;
5543  }
5544 
5545  DCHECK_NULL(for_scope);
5546  return final_loop;
5547 }
5548 
5549 template <typename Impl>
5550 typename ParserBase<Impl>::StatementT
5551 ParserBase<Impl>::ParseForEachStatementWithoutDeclarations(
5552  int stmt_pos, ExpressionT expression, int lhs_beg_pos, int lhs_end_pos,
5553  ForInfo* for_info, ZonePtrList<const AstRawString>* labels,
5554  ZonePtrList<const AstRawString>* own_labels) {
5555  // Initializer is reference followed by in/of.
5556  if (expression->IsPattern()) {
5557  if (expression->is_parenthesized()) {
5558  impl()->ReportMessageAt(
5559  Scanner::Location(expression->position(), end_position()),
5560  MessageTemplate::kInvalidDestructuringTarget);
5561  }
5562  } else if (V8_UNLIKELY(!IsValidReferenceExpression(expression))) {
5563  expression = RewriteInvalidReferenceExpression(
5564  expression, lhs_beg_pos, lhs_end_pos, MessageTemplate::kInvalidLhsInFor,
5565  kSyntaxError);
5566  }
5567 
5568  auto loop = factory()->NewForEachStatement(for_info->mode, labels, own_labels,
5569  stmt_pos);
5570  TargetT target(this, loop);
5571 
5572  ExpressionT enumerable = impl()->NullExpression();
5573  if (for_info->mode == ForEachStatement::ITERATE) {
5574  ExpressionClassifier classifier(this);
5575  AcceptINScope scope(this, true);
5576  enumerable = ParseAssignmentExpression();
5577  ValidateExpression();
5578  } else {
5579  enumerable = ParseExpression();
5580  }
5581 
5582  Expect(Token::RPAREN);
5583 
5584  StatementT body = impl()->NullStatement();
5585  SourceRange body_range;
5586  {
5587  SourceRangeScope range_scope(scanner(), &body_range);
5588  body = ParseStatement(nullptr, nullptr);
5589  }
5590  impl()->RecordIterationStatementSourceRange(loop, body_range);
5591  RETURN_IF_PARSE_ERROR;
5592  return impl()->InitializeForEachStatement(loop, expression, enumerable, body);
5593 }
5594 
5595 template <typename Impl>
5596 typename ParserBase<Impl>::StatementT
5597 ParserBase<Impl>::ParseStandardForLoopWithLexicalDeclarations(
5598  int stmt_pos, StatementT init, ForInfo* for_info,
5599  ZonePtrList<const AstRawString>* labels,
5600  ZonePtrList<const AstRawString>* own_labels) {
5601  // The condition and the next statement of the for loop must be parsed
5602  // in a new scope.
5603  Scope* inner_scope = NewScope(BLOCK_SCOPE);
5604  ForStatementT loop = impl()->NullStatement();
5605  ExpressionT cond = impl()->NullExpression();
5606  StatementT next = impl()->NullStatement();
5607  StatementT body = impl()->NullStatement();
5608  {
5609  BlockState block_state(&scope_, inner_scope);
5610  scope()->set_start_position(scanner()->location().beg_pos);
5611  loop =
5612  ParseStandardForLoop(stmt_pos, labels, own_labels, &cond, &next, &body);
5613  RETURN_IF_PARSE_ERROR;
5614  scope()->set_end_position(end_position());
5615  }
5616 
5617  scope()->set_end_position(end_position());
5618  if (for_info->bound_names.length() > 0 &&
5619  function_state_->contains_function_or_eval()) {
5620  scope()->set_is_hidden();
5621  return impl()->DesugarLexicalBindingsInForStatement(
5622  loop, init, cond, next, body, inner_scope, *for_info);
5623  } else {
5624  inner_scope = inner_scope->FinalizeBlockScope();
5625  DCHECK_NULL(inner_scope);
5626  USE(inner_scope);
5627  }
5628 
5629  Scope* for_scope = scope()->FinalizeBlockScope();
5630  if (for_scope != nullptr) {
5631  // Rewrite a for statement of the form
5632  // for (const x = i; c; n) b
5633  //
5634  // into
5635  //
5636  // {
5637  // const x = i;
5638  // for (; c; n) b
5639  // }
5640  //
5641  DCHECK(!impl()->IsNull(init));
5642  BlockT block = factory()->NewBlock(2, false);
5643  block->statements()->Add(init, zone());
5644  block->statements()->Add(loop, zone());
5645  block->set_scope(for_scope);
5646  loop->Initialize(impl()->NullStatement(), cond, next, body);
5647  return block;
5648  }
5649 
5650  loop->Initialize(init, cond, next, body);
5651  return loop;
5652 }
5653 
5654 template <typename Impl>
5655 typename ParserBase<Impl>::ForStatementT ParserBase<Impl>::ParseStandardForLoop(
5656  int stmt_pos, ZonePtrList<const AstRawString>* labels,
5657  ZonePtrList<const AstRawString>* own_labels, ExpressionT* cond,
5658  StatementT* next, StatementT* body) {
5659  ForStatementT loop = factory()->NewForStatement(labels, own_labels, stmt_pos);
5660  TargetT target(this, loop);
5661 
5662  if (peek() != Token::SEMICOLON) {
5663  *cond = ParseExpression();
5664  }
5665  Expect(Token::SEMICOLON);
5666 
5667  if (peek() != Token::RPAREN) {
5668  ExpressionT exp = ParseExpression();
5669  *next = factory()->NewExpressionStatement(exp, exp->position());
5670  }
5671  Expect(Token::RPAREN);
5672 
5673  SourceRange body_range;
5674  {
5675  SourceRangeScope range_scope(scanner(), &body_range);
5676  *body = ParseStatement(nullptr, nullptr);
5677  }
5678  impl()->RecordIterationStatementSourceRange(loop, body_range);
5679 
5680  return loop;
5681 }
5682 
5683 template <typename Impl>
5684 void ParserBase<Impl>::MarkLoopVariableAsAssigned(
5685  Scope* scope, Variable* var,
5686  typename DeclarationDescriptor::Kind declaration_kind) {
5687  if (!IsLexicalVariableMode(var->mode()) &&
5688  (!scope->is_function_scope() ||
5689  declaration_kind == DeclarationDescriptor::FOR_EACH)) {
5690  var->set_maybe_assigned();
5691  }
5692 }
5693 
5694 template <typename Impl>
5695 typename ParserBase<Impl>::StatementT ParserBase<Impl>::ParseForAwaitStatement(
5696  ZonePtrList<const AstRawString>* labels,
5697  ZonePtrList<const AstRawString>* own_labels) {
5698  // for await '(' ForDeclaration of AssignmentExpression ')'
5699  DCHECK(is_async_function());
5700 
5701  int stmt_pos = peek_position();
5702 
5703  ForInfo for_info(this);
5704  for_info.mode = ForEachStatement::ITERATE;
5705 
5706  // Create an in-between scope for let-bound iteration variables.
5707  BlockState for_state(zone(), &scope_);
5708  Expect(Token::FOR);
5709  Expect(Token::AWAIT);
5710  Expect(Token::LPAREN);
5711  scope()->set_start_position(scanner()->location().beg_pos);
5712  scope()->set_is_hidden();
5713 
5714  auto loop = factory()->NewForOfStatement(labels, own_labels, stmt_pos);
5715  TargetT target(this, loop);
5716 
5717  ExpressionT each_variable = impl()->NullExpression();
5718 
5719  bool has_declarations = false;
5720  Scope* inner_block_scope = NewScope(BLOCK_SCOPE);
5721 
5722  if (peek() == Token::VAR || peek() == Token::CONST ||
5723  (peek() == Token::LET && IsNextLetKeyword())) {
5724  // The initializer contains declarations
5725  // 'for' 'await' '(' ForDeclaration 'of' AssignmentExpression ')'
5726  // Statement
5727  // 'for' 'await' '(' 'var' ForBinding 'of' AssignmentExpression ')'
5728  // Statement
5729  has_declarations = true;
5730 
5731  {
5732  BlockState inner_state(&scope_, inner_block_scope);
5733  ParseVariableDeclarations(kForStatement, &for_info.parsing_result,
5734  nullptr);
5735  }
5736  for_info.position = scanner()->location().beg_pos;
5737 
5738  // Only a single declaration is allowed in for-await-of loops
5739  if (for_info.parsing_result.declarations.size() != 1) {
5740  impl()->ReportMessageAt(for_info.parsing_result.bindings_loc,
5741  MessageTemplate::kForInOfLoopMultiBindings,
5742  "for-await-of");
5743  return impl()->NullStatement();
5744  }
5745 
5746  // for-await-of's declarations do not permit initializers.
5747  if (for_info.parsing_result.first_initializer_loc.IsValid()) {
5748  impl()->ReportMessageAt(for_info.parsing_result.first_initializer_loc,
5749  MessageTemplate::kForInOfLoopInitializer,
5750  "for-await-of");
5751  return impl()->NullStatement();
5752  }
5753  } else {
5754  // The initializer does not contain declarations.
5755  // 'for' 'await' '(' LeftHandSideExpression 'of' AssignmentExpression ')'
5756  // Statement
5757  int lhs_beg_pos = peek_position();
5758  BlockState inner_state(&scope_, inner_block_scope);
5759  ExpressionClassifier classifier(this);
5760  ExpressionT lhs = each_variable = ParseLeftHandSideExpression();
5761  int lhs_end_pos = end_position();
5762 
5763  if (lhs->IsPattern()) {
5764  ValidatePattern(lhs);
5765  } else {
5766  ValidateExpression();
5767  if (V8_UNLIKELY(!IsValidReferenceExpression(lhs))) {
5768  each_variable = RewriteInvalidReferenceExpression(
5769  lhs, lhs_beg_pos, lhs_end_pos, MessageTemplate::kInvalidLhsInFor,
5770  kSyntaxError);
5771  }
5772  }
5773  }
5774 
5775  ExpectContextualKeyword(ast_value_factory()->of_string());
5776  int each_keyword_pos = scanner()->location().beg_pos;
5777 
5778  const bool kAllowIn = true;
5779  ExpressionT iterable = impl()->NullExpression();
5780 
5781  {
5782  ExpressionClassifier classifier(this);
5783  AcceptINScope scope(this, kAllowIn);
5784  iterable = ParseAssignmentExpression();
5785  ValidateExpression();
5786  }
5787 
5788  Expect(Token::RPAREN);
5789 
5790  StatementT body = impl()->NullStatement();
5791  {
5792  BlockState block_state(&scope_, inner_block_scope);
5793  scope()->set_start_position(scanner()->location().beg_pos);
5794 
5795  SourceRange body_range;
5796  {
5797  SourceRangeScope range_scope(scanner(), &body_range);
5798  body = ParseStatement(nullptr, nullptr);
5799  scope()->set_end_position(end_position());
5800  }
5801  impl()->RecordIterationStatementSourceRange(loop, body_range);
5802 
5803  if (has_declarations) {
5804  BlockT body_block = impl()->NullStatement();
5805  impl()->DesugarBindingInForEachStatement(&for_info, &body_block,
5806  &each_variable);
5807  body_block->statements()->Add(body, zone());
5808  body_block->set_scope(scope()->FinalizeBlockScope());
5809  body = body_block;
5810  } else {
5811  Scope* block_scope = scope()->FinalizeBlockScope();
5812  DCHECK_NULL(block_scope);
5813  USE(block_scope);
5814  }
5815  }
5816  const bool finalize = true;
5817  StatementT final_loop = impl()->InitializeForOfStatement(
5818  loop, each_variable, iterable, body, finalize, IteratorType::kAsync,
5819  each_keyword_pos);
5820 
5821  if (!has_declarations) {
5822  Scope* for_scope = scope()->FinalizeBlockScope();
5823  DCHECK_NULL(for_scope);
5824  USE(for_scope);
5825  return final_loop;
5826  }
5827 
5828  BlockT init_block =
5829  impl()->CreateForEachStatementTDZ(impl()->NullStatement(), for_info);
5830 
5831  scope()->set_end_position(end_position());
5832  Scope* for_scope = scope()->FinalizeBlockScope();
5833  // Parsed for-in loop w/ variable declarations.
5834  if (!impl()->IsNull(init_block)) {
5835  init_block->statements()->Add(final_loop, zone());
5836  init_block->set_scope(for_scope);
5837  return init_block;
5838  }
5839  DCHECK_NULL(for_scope);
5840  return final_loop;
5841 }
5842 
5843 template <typename Impl>
5844 void ParserBase<Impl>::CheckClassMethodName(IdentifierT name,
5845  ParsePropertyKind type,
5846  ParseFunctionFlags flags,
5847  bool is_static,
5848  bool* has_seen_constructor) {
5849  DCHECK(type == ParsePropertyKind::kMethod || IsAccessor(type));
5850 
5851  AstValueFactory* avf = ast_value_factory();
5852 
5853  if (is_static) {
5854  if (impl()->IdentifierEquals(name, avf->prototype_string())) {
5855  ReportMessage(MessageTemplate::kStaticPrototype);
5856  return;
5857  }
5858  } else if (impl()->IdentifierEquals(name,
5859  avf->private_constructor_string())) {
5860  ReportMessage(MessageTemplate::kConstructorIsPrivate);
5861  return;
5862  } else if (impl()->IdentifierEquals(name, avf->constructor_string())) {
5863  if (flags != ParseFunctionFlag::kIsNormal || IsAccessor(type)) {
5864  MessageTemplate msg = (flags & ParseFunctionFlag::kIsGenerator) != 0
5865  ? MessageTemplate::kConstructorIsGenerator
5866  : (flags & ParseFunctionFlag::kIsAsync) != 0
5867  ? MessageTemplate::kConstructorIsAsync
5868  : MessageTemplate::kConstructorIsAccessor;
5869  ReportMessage(msg);
5870  return;
5871  }
5872  if (*has_seen_constructor) {
5873  ReportMessage(MessageTemplate::kDuplicateConstructor);
5874  return;
5875  }
5876  *has_seen_constructor = true;
5877  return;
5878  }
5879 }
5880 
5881 template <typename Impl>
5882 void ParserBase<Impl>::CheckClassFieldName(IdentifierT name, bool is_static) {
5883  AstValueFactory* avf = ast_value_factory();
5884  if (is_static && impl()->IdentifierEquals(name, avf->prototype_string())) {
5885  ReportMessage(MessageTemplate::kStaticPrototype);
5886  return;
5887  }
5888 
5889  if (impl()->IdentifierEquals(name, avf->constructor_string()) ||
5890  impl()->IdentifierEquals(name, avf->private_constructor_string())) {
5891  ReportMessage(MessageTemplate::kConstructorClassField);
5892  return;
5893  }
5894 }
5895 
5896 #undef RETURN_IF_PARSE_ERROR
5897 
5898 } // namespace internal
5899 } // namespace v8
5900 
5901 #endif // V8_PARSING_PARSER_BASE_H_
Definition: libplatform.h:13