V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
parser.cc
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 #include "src/parsing/parser.h"
6 
7 #include <algorithm>
8 #include <memory>
9 
10 #include "src/ast/ast-function-literal-id-reindexer.h"
11 #include "src/ast/ast-traversal-visitor.h"
12 #include "src/ast/ast.h"
13 #include "src/bailout-reason.h"
14 #include "src/base/platform/platform.h"
15 #include "src/char-predicates-inl.h"
16 #include "src/compiler-dispatcher/compiler-dispatcher.h"
17 #include "src/conversions-inl.h"
18 #include "src/log.h"
19 #include "src/message-template.h"
20 #include "src/objects/scope-info.h"
21 #include "src/parsing/expression-scope-reparenter.h"
22 #include "src/parsing/parse-info.h"
23 #include "src/parsing/rewriter.h"
24 #include "src/runtime/runtime.h"
25 #include "src/string-stream.h"
26 #include "src/tracing/trace-event.h"
27 
28 namespace v8 {
29 namespace internal {
30 
31 FunctionLiteral* Parser::DefaultConstructor(const AstRawString* name,
32  bool call_super, int pos,
33  int end_pos) {
34  int expected_property_count = -1;
35  const int parameter_count = 0;
36 
37  FunctionKind kind = call_super ? FunctionKind::kDefaultDerivedConstructor
38  : FunctionKind::kDefaultBaseConstructor;
39  DeclarationScope* function_scope = NewFunctionScope(kind);
40  SetLanguageMode(function_scope, LanguageMode::kStrict);
41  // Set start and end position to the same value
42  function_scope->set_start_position(pos);
43  function_scope->set_end_position(pos);
44  ScopedPtrList<Statement> body(pointer_buffer());
45 
46  {
47  FunctionState function_state(&function_state_, &scope_, function_scope);
48 
49  if (call_super) {
50  // Create a SuperCallReference and handle in BytecodeGenerator.
51  auto constructor_args_name = ast_value_factory()->empty_string();
52  bool is_rest = true;
53  bool is_optional = false;
54  Variable* constructor_args = function_scope->DeclareParameter(
55  constructor_args_name, VariableMode::kTemporary, is_optional, is_rest,
56  ast_value_factory(), pos);
57 
58  Expression* call;
59  {
60  ScopedPtrList<Expression> args(pointer_buffer());
61  Spread* spread_args = factory()->NewSpread(
62  factory()->NewVariableProxy(constructor_args), pos, pos);
63 
64  args.Add(spread_args);
65  Expression* super_call_ref = NewSuperCallReference(pos);
66  call = factory()->NewCall(super_call_ref, args, pos);
67  }
68  body.Add(factory()->NewReturnStatement(call, pos));
69  }
70 
71  expected_property_count = function_state.expected_property_count();
72  }
73 
74  FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
75  name, function_scope, body, expected_property_count, parameter_count,
76  parameter_count, FunctionLiteral::kNoDuplicateParameters,
77  FunctionLiteral::kAnonymousExpression, default_eager_compile_hint(), pos,
78  true, GetNextFunctionLiteralId());
79  return function_literal;
80 }
81 
82 void Parser::GetUnexpectedTokenMessage(Token::Value token,
83  MessageTemplate* message,
84  Scanner::Location* location,
85  const char** arg) {
86  switch (token) {
87  case Token::EOS:
88  *message = MessageTemplate::kUnexpectedEOS;
89  break;
90  case Token::SMI:
91  case Token::NUMBER:
92  case Token::BIGINT:
93  *message = MessageTemplate::kUnexpectedTokenNumber;
94  break;
95  case Token::STRING:
96  *message = MessageTemplate::kUnexpectedTokenString;
97  break;
98  case Token::PRIVATE_NAME:
99  case Token::IDENTIFIER:
100  *message = MessageTemplate::kUnexpectedTokenIdentifier;
101  break;
102  case Token::AWAIT:
103  case Token::ENUM:
104  *message = MessageTemplate::kUnexpectedReserved;
105  break;
106  case Token::LET:
107  case Token::STATIC:
108  case Token::YIELD:
109  case Token::FUTURE_STRICT_RESERVED_WORD:
110  *message = is_strict(language_mode())
111  ? MessageTemplate::kUnexpectedStrictReserved
112  : MessageTemplate::kUnexpectedTokenIdentifier;
113  break;
114  case Token::TEMPLATE_SPAN:
115  case Token::TEMPLATE_TAIL:
116  *message = MessageTemplate::kUnexpectedTemplateString;
117  break;
118  case Token::ESCAPED_STRICT_RESERVED_WORD:
119  case Token::ESCAPED_KEYWORD:
120  *message = MessageTemplate::kInvalidEscapedReservedWord;
121  break;
122  case Token::ILLEGAL:
123  if (scanner()->has_error()) {
124  *message = scanner()->error();
125  *location = scanner()->error_location();
126  } else {
127  *message = MessageTemplate::kInvalidOrUnexpectedToken;
128  }
129  break;
130  case Token::REGEXP_LITERAL:
131  *message = MessageTemplate::kUnexpectedTokenRegExp;
132  break;
133  default:
134  const char* name = Token::String(token);
135  DCHECK_NOT_NULL(name);
136  *arg = name;
137  break;
138  }
139 }
140 
141 // ----------------------------------------------------------------------------
142 // Implementation of Parser
143 
144 bool Parser::ShortcutNumericLiteralBinaryExpression(Expression** x,
145  Expression* y,
146  Token::Value op, int pos) {
147  if ((*x)->IsNumberLiteral() && y->IsNumberLiteral()) {
148  double x_val = (*x)->AsLiteral()->AsNumber();
149  double y_val = y->AsLiteral()->AsNumber();
150  switch (op) {
151  case Token::ADD:
152  *x = factory()->NewNumberLiteral(x_val + y_val, pos);
153  return true;
154  case Token::SUB:
155  *x = factory()->NewNumberLiteral(x_val - y_val, pos);
156  return true;
157  case Token::MUL:
158  *x = factory()->NewNumberLiteral(x_val * y_val, pos);
159  return true;
160  case Token::DIV:
161  *x = factory()->NewNumberLiteral(x_val / y_val, pos);
162  return true;
163  case Token::BIT_OR: {
164  int value = DoubleToInt32(x_val) | DoubleToInt32(y_val);
165  *x = factory()->NewNumberLiteral(value, pos);
166  return true;
167  }
168  case Token::BIT_AND: {
169  int value = DoubleToInt32(x_val) & DoubleToInt32(y_val);
170  *x = factory()->NewNumberLiteral(value, pos);
171  return true;
172  }
173  case Token::BIT_XOR: {
174  int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val);
175  *x = factory()->NewNumberLiteral(value, pos);
176  return true;
177  }
178  case Token::SHL: {
179  int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1F);
180  *x = factory()->NewNumberLiteral(value, pos);
181  return true;
182  }
183  case Token::SHR: {
184  uint32_t shift = DoubleToInt32(y_val) & 0x1F;
185  uint32_t value = DoubleToUint32(x_val) >> shift;
186  *x = factory()->NewNumberLiteral(value, pos);
187  return true;
188  }
189  case Token::SAR: {
190  uint32_t shift = DoubleToInt32(y_val) & 0x1F;
191  int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
192  *x = factory()->NewNumberLiteral(value, pos);
193  return true;
194  }
195  case Token::EXP: {
196  double value = Pow(x_val, y_val);
197  int int_value = static_cast<int>(value);
198  *x = factory()->NewNumberLiteral(
199  int_value == value && value != -0.0 ? int_value : value, pos);
200  return true;
201  }
202  default:
203  break;
204  }
205  }
206  return false;
207 }
208 
209 bool Parser::CollapseNaryExpression(Expression** x, Expression* y,
210  Token::Value op, int pos,
211  const SourceRange& range) {
212  // Filter out unsupported ops.
213  if (!Token::IsBinaryOp(op) || op == Token::EXP) return false;
214 
215  // Convert *x into an nary operation with the given op, returning false if
216  // this is not possible.
217  NaryOperation* nary = nullptr;
218  if ((*x)->IsBinaryOperation()) {
219  BinaryOperation* binop = (*x)->AsBinaryOperation();
220  if (binop->op() != op) return false;
221 
222  nary = factory()->NewNaryOperation(op, binop->left(), 2);
223  nary->AddSubsequent(binop->right(), binop->position());
224  ConvertBinaryToNaryOperationSourceRange(binop, nary);
225  *x = nary;
226  } else if ((*x)->IsNaryOperation()) {
227  nary = (*x)->AsNaryOperation();
228  if (nary->op() != op) return false;
229  } else {
230  return false;
231  }
232 
233  // Append our current expression to the nary operation.
234  // TODO(leszeks): Do some literal collapsing here if we're appending Smi or
235  // String literals.
236  nary->AddSubsequent(y, pos);
237  AppendNaryOperationSourceRange(nary, range);
238 
239  return true;
240 }
241 
242 Expression* Parser::BuildUnaryExpression(Expression* expression,
243  Token::Value op, int pos) {
244  DCHECK_NOT_NULL(expression);
245  const Literal* literal = expression->AsLiteral();
246  if (literal != nullptr) {
247  if (op == Token::NOT) {
248  // Convert the literal to a boolean condition and negate it.
249  return factory()->NewBooleanLiteral(literal->ToBooleanIsFalse(), pos);
250  } else if (literal->IsNumberLiteral()) {
251  // Compute some expressions involving only number literals.
252  double value = literal->AsNumber();
253  switch (op) {
254  case Token::ADD:
255  return expression;
256  case Token::SUB:
257  return factory()->NewNumberLiteral(-value, pos);
258  case Token::BIT_NOT:
259  return factory()->NewNumberLiteral(~DoubleToInt32(value), pos);
260  default:
261  break;
262  }
263  }
264  }
265  return factory()->NewUnaryOperation(op, expression, pos);
266 }
267 
268 Expression* Parser::NewThrowError(Runtime::FunctionId id,
269  MessageTemplate message,
270  const AstRawString* arg, int pos) {
271  ScopedPtrList<Expression> args(pointer_buffer());
272  args.Add(factory()->NewSmiLiteral(static_cast<int>(message), pos));
273  args.Add(factory()->NewStringLiteral(arg, pos));
274  CallRuntime* call_constructor = factory()->NewCallRuntime(id, args, pos);
275  return factory()->NewThrow(call_constructor, pos);
276 }
277 
278 Expression* Parser::NewSuperPropertyReference(int pos) {
279  // this_function[home_object_symbol]
280  VariableProxy* this_function_proxy =
281  NewUnresolved(ast_value_factory()->this_function_string(), pos);
282  Expression* home_object_symbol_literal = factory()->NewSymbolLiteral(
283  AstSymbol::kHomeObjectSymbol, kNoSourcePosition);
284  Expression* home_object = factory()->NewProperty(
285  this_function_proxy, home_object_symbol_literal, pos);
286  return factory()->NewSuperPropertyReference(
287  ThisExpression(pos)->AsVariableProxy(), home_object, pos);
288 }
289 
290 Expression* Parser::NewSuperCallReference(int pos) {
291  VariableProxy* new_target_proxy =
292  NewUnresolved(ast_value_factory()->new_target_string(), pos);
293  VariableProxy* this_function_proxy =
294  NewUnresolved(ast_value_factory()->this_function_string(), pos);
295  return factory()->NewSuperCallReference(
296  ThisExpression(pos)->AsVariableProxy(), new_target_proxy,
297  this_function_proxy, pos);
298 }
299 
300 Expression* Parser::NewTargetExpression(int pos) {
301  auto proxy = NewUnresolved(ast_value_factory()->new_target_string(), pos);
302  proxy->set_is_new_target();
303  return proxy;
304 }
305 
306 Expression* Parser::ImportMetaExpression(int pos) {
307  ScopedPtrList<Expression> args(pointer_buffer());
308  return factory()->NewCallRuntime(Runtime::kInlineGetImportMetaObject, args,
309  pos);
310 }
311 
312 Expression* Parser::ExpressionFromLiteral(Token::Value token, int pos) {
313  switch (token) {
314  case Token::NULL_LITERAL:
315  return factory()->NewNullLiteral(pos);
316  case Token::TRUE_LITERAL:
317  return factory()->NewBooleanLiteral(true, pos);
318  case Token::FALSE_LITERAL:
319  return factory()->NewBooleanLiteral(false, pos);
320  case Token::SMI: {
321  uint32_t value = scanner()->smi_value();
322  return factory()->NewSmiLiteral(value, pos);
323  }
324  case Token::NUMBER: {
325  double value = scanner()->DoubleValue();
326  return factory()->NewNumberLiteral(value, pos);
327  }
328  case Token::BIGINT:
329  return factory()->NewBigIntLiteral(
330  AstBigInt(scanner()->CurrentLiteralAsCString(zone())), pos);
331  case Token::STRING: {
332  const AstRawString* symbol = GetSymbol();
333  fni_.PushLiteralName(symbol);
334  return factory()->NewStringLiteral(symbol, pos);
335  }
336  default:
337  DCHECK(false);
338  }
339  return FailureExpression();
340 }
341 
342 Expression* Parser::NewV8Intrinsic(const AstRawString* name,
343  const ScopedPtrList<Expression>& args,
344  int pos) {
345  if (extension_ != nullptr) {
346  // The extension structures are only accessible while parsing the
347  // very first time, not when reparsing because of lazy compilation.
348  GetClosureScope()->ForceEagerCompilation();
349  }
350 
351  DCHECK(name->is_one_byte());
352  const Runtime::Function* function =
353  Runtime::FunctionForName(name->raw_data(), name->length());
354 
355  if (function != nullptr) {
356  // Check for possible name clash.
357  DCHECK_EQ(Context::kNotFound,
358  Context::IntrinsicIndexForName(name->raw_data(), name->length()));
359 
360  // Check that the expected number of arguments are being passed.
361  if (function->nargs != -1 && function->nargs != args.length()) {
362  ReportMessage(MessageTemplate::kRuntimeWrongNumArgs);
363  return FailureExpression();
364  }
365 
366  return factory()->NewCallRuntime(function, args, pos);
367  }
368 
369  int context_index =
370  Context::IntrinsicIndexForName(name->raw_data(), name->length());
371 
372  // Check that the function is defined.
373  if (context_index == Context::kNotFound) {
374  ReportMessage(MessageTemplate::kNotDefined, name);
375  return FailureExpression();
376  }
377 
378  return factory()->NewCallRuntime(context_index, args, pos);
379 }
380 
381 Parser::Parser(ParseInfo* info)
382  : ParserBase<Parser>(info->zone(), &scanner_, info->stack_limit(),
383  info->extension(), info->GetOrCreateAstValueFactory(),
384  info->pending_error_handler(),
385  info->runtime_call_stats(), info->logger(),
386  info->script().is_null() ? -1 : info->script()->id(),
387  info->is_module(), true),
388  info_(info),
389  scanner_(info->character_stream(), info->is_module()),
390  preparser_zone_(info->zone()->allocator(), ZONE_NAME),
391  reusable_preparser_(nullptr),
392  mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly.
393  source_range_map_(info->source_range_map()),
394  target_stack_(nullptr),
395  total_preparse_skipped_(0),
396  consumed_preparsed_scope_data_(info->consumed_preparsed_scope_data()),
397  parameters_end_pos_(info->parameters_end_pos()) {
398  // Even though we were passed ParseInfo, we should not store it in
399  // Parser - this makes sure that Isolate is not accidentally accessed via
400  // ParseInfo during background parsing.
401  DCHECK_NOT_NULL(info->character_stream());
402  // Determine if functions can be lazily compiled. This is necessary to
403  // allow some of our builtin JS files to be lazily compiled. These
404  // builtins cannot be handled lazily by the parser, since we have to know
405  // if a function uses the special natives syntax, which is something the
406  // parser records.
407  // If the debugger requests compilation for break points, we cannot be
408  // aggressive about lazy compilation, because it might trigger compilation
409  // of functions without an outer context when setting a breakpoint through
410  // Debug::FindSharedFunctionInfoInScript
411  // We also compile eagerly for kProduceExhaustiveCodeCache.
412  bool can_compile_lazily = FLAG_lazy && !info->is_eager();
413 
414  set_default_eager_compile_hint(can_compile_lazily
415  ? FunctionLiteral::kShouldLazyCompile
416  : FunctionLiteral::kShouldEagerCompile);
417  allow_lazy_ = FLAG_lazy && info->allow_lazy_parsing() && !info->is_native() &&
418  info->extension() == nullptr && can_compile_lazily;
419  set_allow_natives(FLAG_allow_natives_syntax || info->is_native());
420  set_allow_harmony_public_fields(FLAG_harmony_public_fields);
421  set_allow_harmony_static_fields(FLAG_harmony_static_fields);
422  set_allow_harmony_dynamic_import(FLAG_harmony_dynamic_import);
423  set_allow_harmony_import_meta(FLAG_harmony_import_meta);
424  set_allow_harmony_numeric_separator(FLAG_harmony_numeric_separator);
425  set_allow_harmony_private_fields(FLAG_harmony_private_fields);
426  set_allow_harmony_private_methods(FLAG_harmony_private_methods);
427  for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
428  ++feature) {
429  use_counts_[feature] = 0;
430  }
431 }
432 
433 void Parser::InitializeEmptyScopeChain(ParseInfo* info) {
434  DCHECK_NULL(original_scope_);
435  DCHECK_NULL(info->script_scope());
436  DeclarationScope* script_scope = NewScriptScope();
437  info->set_script_scope(script_scope);
438  original_scope_ = script_scope;
439 }
440 
441 void Parser::DeserializeScopeChain(
442  Isolate* isolate, ParseInfo* info,
443  MaybeHandle<ScopeInfo> maybe_outer_scope_info) {
444  InitializeEmptyScopeChain(info);
445  Handle<ScopeInfo> outer_scope_info;
446  if (maybe_outer_scope_info.ToHandle(&outer_scope_info)) {
447  DCHECK(ThreadId::Current().Equals(isolate->thread_id()));
448  original_scope_ = Scope::DeserializeScopeChain(
449  isolate, zone(), *outer_scope_info, info->script_scope(),
450  ast_value_factory(), Scope::DeserializationMode::kScopesOnly);
451  }
452 }
453 
454 namespace {
455 
456 void MaybeResetCharacterStream(ParseInfo* info, FunctionLiteral* literal) {
457  // Don't reset the character stream if there is an asm.js module since it will
458  // be used again by the asm-parser.
459  if (!FLAG_stress_validate_asm &&
460  (literal == nullptr || !literal->scope()->ContainsAsmModule())) {
461  info->ResetCharacterStream();
462  }
463 }
464 
465 } // namespace
466 
467 FunctionLiteral* Parser::ParseProgram(Isolate* isolate, ParseInfo* info) {
468  // TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
469  // see comment for HistogramTimerScope class.
470 
471  // It's OK to use the Isolate & counters here, since this function is only
472  // called in the main thread.
473  DCHECK(parsing_on_main_thread_);
474  RuntimeCallTimerScope runtime_timer(
475  runtime_call_stats_, info->is_eval()
476  ? RuntimeCallCounterId::kParseEval
477  : RuntimeCallCounterId::kParseProgram);
478  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.ParseProgram");
479  base::ElapsedTimer timer;
480  if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
481 
482  // Initialize parser state.
483  DeserializeScopeChain(isolate, info, info->maybe_outer_scope_info());
484 
485  scanner_.Initialize();
486  FunctionLiteral* result = DoParseProgram(isolate, info);
487  MaybeResetCharacterStream(info, result);
488 
489  HandleSourceURLComments(isolate, info->script());
490 
491  if (V8_UNLIKELY(FLAG_log_function_events) && result != nullptr) {
492  double ms = timer.Elapsed().InMillisecondsF();
493  const char* event_name = "parse-eval";
494  Script* script = *info->script();
495  int start = -1;
496  int end = -1;
497  if (!info->is_eval()) {
498  event_name = "parse-script";
499  start = 0;
500  end = String::cast(script->source())->length();
501  }
502  LOG(isolate,
503  FunctionEvent(event_name, script->id(), ms, start, end, "", 0));
504  }
505  return result;
506 }
507 
508 FunctionLiteral* Parser::DoParseProgram(Isolate* isolate, ParseInfo* info) {
509  // Note that this function can be called from the main thread or from a
510  // background thread. We should not access anything Isolate / heap dependent
511  // via ParseInfo, and also not pass it forward. If not on the main thread
512  // isolate will be nullptr.
513  DCHECK_EQ(parsing_on_main_thread_, isolate != nullptr);
514  DCHECK_NULL(scope_);
515  DCHECK_NULL(target_stack_);
516 
517  ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY);
518  ResetFunctionLiteralId();
519  DCHECK(info->function_literal_id() == FunctionLiteral::kIdTypeTopLevel ||
520  info->function_literal_id() == FunctionLiteral::kIdTypeInvalid);
521 
522  FunctionLiteral* result = nullptr;
523  {
524  Scope* outer = original_scope_;
525  DCHECK_NOT_NULL(outer);
526  if (info->is_eval()) {
527  outer = NewEvalScope(outer);
528  } else if (parsing_module_) {
529  DCHECK_EQ(outer, info->script_scope());
530  outer = NewModuleScope(info->script_scope());
531  }
532 
533  DeclarationScope* scope = outer->AsDeclarationScope();
534  scope->set_start_position(0);
535 
536  FunctionState function_state(&function_state_, &scope_, scope);
537  ScopedPtrList<Statement> body(pointer_buffer());
538  int beg_pos = scanner()->location().beg_pos;
539  if (parsing_module_) {
540  DCHECK(info->is_module());
541  // Declare the special module parameter.
542  auto name = ast_value_factory()->empty_string();
543  bool is_rest = false;
544  bool is_optional = false;
545  auto var = scope->DeclareParameter(name, VariableMode::kVar, is_optional,
546  is_rest, ast_value_factory(), beg_pos);
547  var->AllocateTo(VariableLocation::PARAMETER, 0);
548 
549  PrepareGeneratorVariables();
550  Expression* initial_yield =
551  BuildInitialYield(kNoSourcePosition, kGeneratorFunction);
552  body.Add(
553  factory()->NewExpressionStatement(initial_yield, kNoSourcePosition));
554 
555  ParseModuleItemList(&body);
556  if (!module()->Validate(this->scope()->AsModuleScope(),
557  pending_error_handler(), zone())) {
558  scanner()->set_parser_error();
559  }
560  } else if (info->is_wrapped_as_function()) {
561  ParseWrapped(isolate, info, &body, scope, zone());
562  } else {
563  // Don't count the mode in the use counters--give the program a chance
564  // to enable script-wide strict mode below.
565  this->scope()->SetLanguageMode(info->language_mode());
566  ParseStatementList(&body, Token::EOS);
567  }
568 
569  // The parser will peek but not consume EOS. Our scope logically goes all
570  // the way to the EOS, though.
571  scope->set_end_position(peek_position());
572 
573  if (is_strict(language_mode())) {
574  CheckStrictOctalLiteral(beg_pos, end_position());
575  }
576  if (is_sloppy(language_mode())) {
577  // TODO(littledan): Function bindings on the global object that modify
578  // pre-existing bindings should be made writable, enumerable and
579  // nonconfigurable if possible, whereas this code will leave attributes
580  // unchanged if the property already exists.
581  InsertSloppyBlockFunctionVarBindings(scope);
582  }
583  CheckConflictingVarDeclarations(scope);
584 
585  if (info->parse_restriction() == ONLY_SINGLE_FUNCTION_LITERAL) {
586  if (body.length() != 1 || !body.at(0)->IsExpressionStatement() ||
587  !body.at(0)
588  ->AsExpressionStatement()
589  ->expression()
590  ->IsFunctionLiteral()) {
591  ReportMessage(MessageTemplate::kSingleFunctionLiteral);
592  }
593  }
594 
595  RewriteDestructuringAssignments();
596  int parameter_count = parsing_module_ ? 1 : 0;
597  result = factory()->NewScriptOrEvalFunctionLiteral(
598  scope, body, function_state.expected_property_count(), parameter_count);
599  result->set_suspend_count(function_state.suspend_count());
600  }
601 
602  info->set_max_function_literal_id(GetLastFunctionLiteralId());
603 
604  // Make sure the target stack is empty.
605  DCHECK_NULL(target_stack_);
606 
607  if (has_error()) return nullptr;
608  return result;
609 }
610 
611 ZonePtrList<const AstRawString>* Parser::PrepareWrappedArguments(
612  Isolate* isolate, ParseInfo* info, Zone* zone) {
613  DCHECK(parsing_on_main_thread_);
614  DCHECK_NOT_NULL(isolate);
615  Handle<FixedArray> arguments(info->script()->wrapped_arguments(), isolate);
616  int arguments_length = arguments->length();
617  ZonePtrList<const AstRawString>* arguments_for_wrapped_function =
618  new (zone) ZonePtrList<const AstRawString>(arguments_length, zone);
619  for (int i = 0; i < arguments_length; i++) {
620  const AstRawString* argument_string = ast_value_factory()->GetString(
621  Handle<String>(String::cast(arguments->get(i)), isolate));
622  arguments_for_wrapped_function->Add(argument_string, zone);
623  }
624  return arguments_for_wrapped_function;
625 }
626 
627 void Parser::ParseWrapped(Isolate* isolate, ParseInfo* info,
628  ScopedPtrList<Statement>* body,
629  DeclarationScope* outer_scope, Zone* zone) {
630  DCHECK_EQ(parsing_on_main_thread_, isolate != nullptr);
631  DCHECK(info->is_wrapped_as_function());
632  ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
633 
634  // Set function and block state for the outer eval scope.
635  DCHECK(outer_scope->is_eval_scope());
636  FunctionState function_state(&function_state_, &scope_, outer_scope);
637 
638  const AstRawString* function_name = nullptr;
639  Scanner::Location location(0, 0);
640 
641  ZonePtrList<const AstRawString>* arguments_for_wrapped_function =
642  PrepareWrappedArguments(isolate, info, zone);
643 
644  FunctionLiteral* function_literal = ParseFunctionLiteral(
645  function_name, location, kSkipFunctionNameCheck, kNormalFunction,
646  kNoSourcePosition, FunctionLiteral::kWrapped, LanguageMode::kSloppy,
647  arguments_for_wrapped_function);
648 
649  Statement* return_statement = factory()->NewReturnStatement(
650  function_literal, kNoSourcePosition, kNoSourcePosition);
651  body->Add(return_statement);
652 }
653 
654 FunctionLiteral* Parser::ParseFunction(Isolate* isolate, ParseInfo* info,
655  Handle<SharedFunctionInfo> shared_info) {
656  // It's OK to use the Isolate & counters here, since this function is only
657  // called in the main thread.
658  DCHECK(parsing_on_main_thread_);
659  RuntimeCallTimerScope runtime_timer(runtime_call_stats_,
660  RuntimeCallCounterId::kParseFunction);
661  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.ParseFunction");
662  base::ElapsedTimer timer;
663  if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
664 
665  DeserializeScopeChain(isolate, info, info->maybe_outer_scope_info());
666  DCHECK_EQ(factory()->zone(), info->zone());
667 
668  // Initialize parser state.
669  Handle<String> name(shared_info->Name(), isolate);
670  info->set_function_name(ast_value_factory()->GetString(name));
671  scanner_.Initialize();
672 
673  FunctionLiteral* result =
674  DoParseFunction(isolate, info, info->function_name());
675  MaybeResetCharacterStream(info, result);
676  if (result != nullptr) {
677  Handle<String> inferred_name(shared_info->inferred_name(), isolate);
678  result->set_inferred_name(inferred_name);
679  }
680 
681  if (V8_UNLIKELY(FLAG_log_function_events) && result != nullptr) {
682  double ms = timer.Elapsed().InMillisecondsF();
683  // We need to make sure that the debug-name is available.
684  ast_value_factory()->Internalize(isolate);
685  DeclarationScope* function_scope = result->scope();
686  std::unique_ptr<char[]> function_name = result->GetDebugName();
687  LOG(isolate,
688  FunctionEvent("parse-function", info->script()->id(), ms,
689  function_scope->start_position(),
690  function_scope->end_position(), function_name.get(),
691  strlen(function_name.get())));
692  }
693  return result;
694 }
695 
696 static FunctionLiteral::FunctionType ComputeFunctionType(ParseInfo* info) {
697  if (info->is_wrapped_as_function()) {
698  return FunctionLiteral::kWrapped;
699  } else if (info->is_declaration()) {
700  return FunctionLiteral::kDeclaration;
701  } else if (info->is_named_expression()) {
702  return FunctionLiteral::kNamedExpression;
703  } else if (IsConciseMethod(info->function_kind()) ||
704  IsAccessorFunction(info->function_kind())) {
705  return FunctionLiteral::kAccessorOrMethod;
706  }
707  return FunctionLiteral::kAnonymousExpression;
708 }
709 
710 FunctionLiteral* Parser::DoParseFunction(Isolate* isolate, ParseInfo* info,
711  const AstRawString* raw_name) {
712  DCHECK_EQ(parsing_on_main_thread_, isolate != nullptr);
713  DCHECK_NOT_NULL(raw_name);
714  DCHECK_NULL(scope_);
715  DCHECK_NULL(target_stack_);
716 
717  DCHECK(ast_value_factory());
718  fni_.PushEnclosingName(raw_name);
719 
720  ResetFunctionLiteralId();
721  DCHECK_LT(0, info->function_literal_id());
722  SkipFunctionLiterals(info->function_literal_id() - 1);
723 
724  ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
725 
726  // Place holder for the result.
727  FunctionLiteral* result = nullptr;
728 
729  {
730  // Parse the function literal.
731  Scope* outer = original_scope_;
732  DeclarationScope* outer_function = outer->GetClosureScope();
733  DCHECK(outer);
734  FunctionState function_state(&function_state_, &scope_, outer_function);
735  BlockState block_state(&scope_, outer);
736  DCHECK(is_sloppy(outer->language_mode()) ||
737  is_strict(info->language_mode()));
738  FunctionLiteral::FunctionType function_type = ComputeFunctionType(info);
739  FunctionKind kind = info->function_kind();
740 
741  if (IsArrowFunction(kind)) {
742  if (IsAsyncFunction(kind)) {
743  DCHECK(!scanner()->HasLineTerminatorAfterNext());
744  if (!Check(Token::ASYNC)) {
745  CHECK(stack_overflow());
746  return nullptr;
747  }
748  if (!(peek_any_identifier() || peek() == Token::LPAREN)) {
749  CHECK(stack_overflow());
750  return nullptr;
751  }
752  }
753 
754  // TODO(adamk): We should construct this scope from the ScopeInfo.
755  DeclarationScope* scope = NewFunctionScope(kind);
756 
757  // This bit only needs to be explicitly set because we're
758  // not passing the ScopeInfo to the Scope constructor.
759  SetLanguageMode(scope, info->language_mode());
760 
761  scope->set_start_position(info->start_position());
762  ExpressionClassifier formals_classifier(this);
763  ParserFormalParameters formals(scope);
764  // The outer FunctionState should not contain destructuring assignments.
765  DCHECK_EQ(0,
766  function_state.destructuring_assignments_to_rewrite().size());
767  {
768  // Parsing patterns as variable reference expression creates
769  // NewUnresolved references in current scope. Enter arrow function
770  // scope for formal parameter parsing.
771  BlockState block_state(&scope_, scope);
772  if (Check(Token::LPAREN)) {
773  // '(' StrictFormalParameters ')'
774  ParseFormalParameterList(&formals);
775  Expect(Token::RPAREN);
776  } else {
777  // BindingIdentifier
778  ParseFormalParameter(&formals);
779  DeclareFormalParameters(&formals);
780  }
781  }
782 
783  if (GetLastFunctionLiteralId() != info->function_literal_id() - 1) {
784  if (has_error()) return nullptr;
785  // If there were FunctionLiterals in the parameters, we need to
786  // renumber them to shift down so the next function literal id for
787  // the arrow function is the one requested.
788  AstFunctionLiteralIdReindexer reindexer(
789  stack_limit_,
790  (info->function_literal_id() - 1) - GetLastFunctionLiteralId());
791  for (auto p : formals.params) {
792  if (p->pattern != nullptr) reindexer.Reindex(p->pattern);
793  if (p->initializer() != nullptr) {
794  reindexer.Reindex(p->initializer());
795  }
796  }
797  ResetFunctionLiteralId();
798  SkipFunctionLiterals(info->function_literal_id() - 1);
799  }
800 
801  set_rewritable_length(0);
802  Expression* expression = ParseArrowFunctionLiteral(formals);
803  // Scanning must end at the same position that was recorded
804  // previously. If not, parsing has been interrupted due to a stack
805  // overflow, at which point the partially parsed arrow function
806  // concise body happens to be a valid expression. This is a problem
807  // only for arrow functions with single expression bodies, since there
808  // is no end token such as "}" for normal functions.
809  if (scanner()->location().end_pos == info->end_position()) {
810  // The pre-parser saw an arrow function here, so the full parser
811  // must produce a FunctionLiteral.
812  DCHECK(expression->IsFunctionLiteral());
813  result = expression->AsFunctionLiteral();
814  }
815  } else if (IsDefaultConstructor(kind)) {
816  DCHECK_EQ(scope(), outer);
817  result = DefaultConstructor(raw_name, IsDerivedConstructor(kind),
818  info->start_position(), info->end_position());
819  } else {
820  ZonePtrList<const AstRawString>* arguments_for_wrapped_function =
821  info->is_wrapped_as_function()
822  ? PrepareWrappedArguments(isolate, info, zone())
823  : nullptr;
824  result = ParseFunctionLiteral(
825  raw_name, Scanner::Location::invalid(), kSkipFunctionNameCheck, kind,
826  kNoSourcePosition, function_type, info->language_mode(),
827  arguments_for_wrapped_function);
828  }
829 
830  if (has_error()) return nullptr;
831  result->set_requires_instance_members_initializer(
832  info->requires_instance_members_initializer());
833  }
834 
835  // Make sure the target stack is empty.
836  DCHECK_NULL(target_stack_);
837  DCHECK_IMPLIES(result,
838  info->function_literal_id() == result->function_literal_id());
839  return result;
840 }
841 
842 Statement* Parser::ParseModuleItem() {
843  // ecma262/#prod-ModuleItem
844  // ModuleItem :
845  // ImportDeclaration
846  // ExportDeclaration
847  // StatementListItem
848 
849  Token::Value next = peek();
850 
851  if (next == Token::EXPORT) {
852  return ParseExportDeclaration();
853  }
854 
855  if (next == Token::IMPORT) {
856  // We must be careful not to parse a dynamic import expression as an import
857  // declaration. Same for import.meta expressions.
858  Token::Value peek_ahead = PeekAhead();
859  if ((!allow_harmony_dynamic_import() || peek_ahead != Token::LPAREN) &&
860  (!allow_harmony_import_meta() || peek_ahead != Token::PERIOD)) {
861  ParseImportDeclaration();
862  return factory()->EmptyStatement();
863  }
864  }
865 
866  return ParseStatementListItem();
867 }
868 
869 void Parser::ParseModuleItemList(ScopedPtrList<Statement>* body) {
870  // ecma262/#prod-Module
871  // Module :
872  // ModuleBody?
873  //
874  // ecma262/#prod-ModuleItemList
875  // ModuleBody :
876  // ModuleItem*
877 
878  DCHECK(scope()->is_module_scope());
879  while (peek() != Token::EOS) {
880  Statement* stat = ParseModuleItem();
881  if (stat == nullptr) return;
882  if (stat->IsEmptyStatement()) continue;
883  body->Add(stat);
884  }
885 }
886 
887 const AstRawString* Parser::ParseModuleSpecifier() {
888  // ModuleSpecifier :
889  // StringLiteral
890 
891  Expect(Token::STRING);
892  return GetSymbol();
893 }
894 
895 ZoneChunkList<Parser::ExportClauseData>* Parser::ParseExportClause(
896  Scanner::Location* reserved_loc) {
897  // ExportClause :
898  // '{' '}'
899  // '{' ExportsList '}'
900  // '{' ExportsList ',' '}'
901  //
902  // ExportsList :
903  // ExportSpecifier
904  // ExportsList ',' ExportSpecifier
905  //
906  // ExportSpecifier :
907  // IdentifierName
908  // IdentifierName 'as' IdentifierName
909  ZoneChunkList<ExportClauseData>* export_data =
910  new (zone()) ZoneChunkList<ExportClauseData>(zone());
911 
912  Expect(Token::LBRACE);
913 
914  Token::Value name_tok;
915  while ((name_tok = peek()) != Token::RBRACE) {
916  // Keep track of the first reserved word encountered in case our
917  // caller needs to report an error.
918  if (!reserved_loc->IsValid() &&
919  !Token::IsIdentifier(name_tok, LanguageMode::kStrict, false,
920  parsing_module_)) {
921  *reserved_loc = scanner()->location();
922  }
923  const AstRawString* local_name = ParseIdentifierName();
924  const AstRawString* export_name = nullptr;
925  Scanner::Location location = scanner()->location();
926  if (CheckContextualKeyword(ast_value_factory()->as_string())) {
927  export_name = ParseIdentifierName();
928  // Set the location to the whole "a as b" string, so that it makes sense
929  // both for errors due to "a" and for errors due to "b".
930  location.end_pos = scanner()->location().end_pos;
931  }
932  if (export_name == nullptr) {
933  export_name = local_name;
934  }
935  export_data->push_back({export_name, local_name, location});
936  if (peek() == Token::RBRACE) break;
937  if (V8_UNLIKELY(!Check(Token::COMMA))) {
938  ReportUnexpectedToken(Next());
939  break;
940  }
941  }
942 
943  Expect(Token::RBRACE);
944  return export_data;
945 }
946 
947 ZonePtrList<const Parser::NamedImport>* Parser::ParseNamedImports(int pos) {
948  // NamedImports :
949  // '{' '}'
950  // '{' ImportsList '}'
951  // '{' ImportsList ',' '}'
952  //
953  // ImportsList :
954  // ImportSpecifier
955  // ImportsList ',' ImportSpecifier
956  //
957  // ImportSpecifier :
958  // BindingIdentifier
959  // IdentifierName 'as' BindingIdentifier
960 
961  Expect(Token::LBRACE);
962 
963  auto result = new (zone()) ZonePtrList<const NamedImport>(1, zone());
964  while (peek() != Token::RBRACE) {
965  const AstRawString* import_name = ParseIdentifierName();
966  const AstRawString* local_name = import_name;
967  Scanner::Location location = scanner()->location();
968  // In the presence of 'as', the left-side of the 'as' can
969  // be any IdentifierName. But without 'as', it must be a valid
970  // BindingIdentifier.
971  if (CheckContextualKeyword(ast_value_factory()->as_string())) {
972  local_name = ParseIdentifierName();
973  }
974  if (!Token::IsIdentifier(scanner()->current_token(), LanguageMode::kStrict,
975  false, parsing_module_)) {
976  ReportMessage(MessageTemplate::kUnexpectedReserved);
977  return nullptr;
978  } else if (IsEvalOrArguments(local_name)) {
979  ReportMessage(MessageTemplate::kStrictEvalArguments);
980  return nullptr;
981  }
982 
983  DeclareVariable(local_name, VariableMode::kConst, kNeedsInitialization,
984  position());
985 
986  NamedImport* import =
987  new (zone()) NamedImport(import_name, local_name, location);
988  result->Add(import, zone());
989 
990  if (peek() == Token::RBRACE) break;
991  Expect(Token::COMMA);
992  }
993 
994  Expect(Token::RBRACE);
995  return result;
996 }
997 
998 void Parser::ParseImportDeclaration() {
999  // ImportDeclaration :
1000  // 'import' ImportClause 'from' ModuleSpecifier ';'
1001  // 'import' ModuleSpecifier ';'
1002  //
1003  // ImportClause :
1004  // ImportedDefaultBinding
1005  // NameSpaceImport
1006  // NamedImports
1007  // ImportedDefaultBinding ',' NameSpaceImport
1008  // ImportedDefaultBinding ',' NamedImports
1009  //
1010  // NameSpaceImport :
1011  // '*' 'as' ImportedBinding
1012 
1013  int pos = peek_position();
1014  Expect(Token::IMPORT);
1015 
1016  Token::Value tok = peek();
1017 
1018  // 'import' ModuleSpecifier ';'
1019  if (tok == Token::STRING) {
1020  Scanner::Location specifier_loc = scanner()->peek_location();
1021  const AstRawString* module_specifier = ParseModuleSpecifier();
1022  ExpectSemicolon();
1023  module()->AddEmptyImport(module_specifier, specifier_loc);
1024  return;
1025  }
1026 
1027  // Parse ImportedDefaultBinding if present.
1028  const AstRawString* import_default_binding = nullptr;
1029  Scanner::Location import_default_binding_loc;
1030  if (tok != Token::MUL && tok != Token::LBRACE) {
1031  import_default_binding = ParseIdentifier(kDontAllowRestrictedIdentifiers);
1032  import_default_binding_loc = scanner()->location();
1033  DeclareVariable(import_default_binding, VariableMode::kConst,
1034  kNeedsInitialization, pos);
1035  }
1036 
1037  // Parse NameSpaceImport or NamedImports if present.
1038  const AstRawString* module_namespace_binding = nullptr;
1039  Scanner::Location module_namespace_binding_loc;
1040  const ZonePtrList<const NamedImport>* named_imports = nullptr;
1041  if (import_default_binding == nullptr || Check(Token::COMMA)) {
1042  switch (peek()) {
1043  case Token::MUL: {
1044  Consume(Token::MUL);
1045  ExpectContextualKeyword(ast_value_factory()->as_string());
1046  module_namespace_binding =
1047  ParseIdentifier(kDontAllowRestrictedIdentifiers);
1048  module_namespace_binding_loc = scanner()->location();
1049  DeclareVariable(module_namespace_binding, VariableMode::kConst,
1050  kCreatedInitialized, pos);
1051  break;
1052  }
1053 
1054  case Token::LBRACE:
1055  named_imports = ParseNamedImports(pos);
1056  break;
1057 
1058  default:
1059  ReportUnexpectedToken(scanner()->current_token());
1060  return;
1061  }
1062  }
1063 
1064  ExpectContextualKeyword(ast_value_factory()->from_string());
1065  Scanner::Location specifier_loc = scanner()->peek_location();
1066  const AstRawString* module_specifier = ParseModuleSpecifier();
1067  ExpectSemicolon();
1068 
1069  // Now that we have all the information, we can make the appropriate
1070  // declarations.
1071 
1072  // TODO(neis): Would prefer to call DeclareVariable for each case below rather
1073  // than above and in ParseNamedImports, but then a possible error message
1074  // would point to the wrong location. Maybe have a DeclareAt version of
1075  // Declare that takes a location?
1076 
1077  if (module_namespace_binding != nullptr) {
1078  module()->AddStarImport(module_namespace_binding, module_specifier,
1079  module_namespace_binding_loc, specifier_loc,
1080  zone());
1081  }
1082 
1083  if (import_default_binding != nullptr) {
1084  module()->AddImport(ast_value_factory()->default_string(),
1085  import_default_binding, module_specifier,
1086  import_default_binding_loc, specifier_loc, zone());
1087  }
1088 
1089  if (named_imports != nullptr) {
1090  if (named_imports->length() == 0) {
1091  module()->AddEmptyImport(module_specifier, specifier_loc);
1092  } else {
1093  for (int i = 0; i < named_imports->length(); ++i) {
1094  const NamedImport* import = named_imports->at(i);
1095  module()->AddImport(import->import_name, import->local_name,
1096  module_specifier, import->location, specifier_loc,
1097  zone());
1098  }
1099  }
1100  }
1101 }
1102 
1103 Statement* Parser::ParseExportDefault() {
1104  // Supports the following productions, starting after the 'default' token:
1105  // 'export' 'default' HoistableDeclaration
1106  // 'export' 'default' ClassDeclaration
1107  // 'export' 'default' AssignmentExpression[In] ';'
1108 
1109  Expect(Token::DEFAULT);
1110  Scanner::Location default_loc = scanner()->location();
1111 
1112  ZonePtrList<const AstRawString> local_names(1, zone());
1113  Statement* result = nullptr;
1114  switch (peek()) {
1115  case Token::FUNCTION:
1116  result = ParseHoistableDeclaration(&local_names, true);
1117  break;
1118 
1119  case Token::CLASS:
1120  Consume(Token::CLASS);
1121  result = ParseClassDeclaration(&local_names, true);
1122  break;
1123 
1124  case Token::ASYNC:
1125  if (PeekAhead() == Token::FUNCTION &&
1126  !scanner()->HasLineTerminatorAfterNext()) {
1127  Consume(Token::ASYNC);
1128  result = ParseAsyncFunctionDeclaration(&local_names, true);
1129  break;
1130  }
1131  V8_FALLTHROUGH;
1132 
1133  default: {
1134  int pos = position();
1135  ExpressionClassifier classifier(this);
1136  AcceptINScope scope(this, true);
1137  Expression* value = ParseAssignmentExpression();
1138  ValidateExpression();
1139  SetFunctionName(value, ast_value_factory()->default_string());
1140 
1141  const AstRawString* local_name =
1142  ast_value_factory()->star_default_star_string();
1143  local_names.Add(local_name, zone());
1144 
1145  // It's fine to declare this as VariableMode::kConst because the user has
1146  // no way of writing to it.
1147  Declaration* decl =
1148  DeclareVariable(local_name, VariableMode::kConst, pos);
1149  decl->proxy()->var()->set_initializer_position(position());
1150 
1151  Assignment* assignment = factory()->NewAssignment(
1152  Token::INIT, decl->proxy(), value, kNoSourcePosition);
1153  result = IgnoreCompletion(
1154  factory()->NewExpressionStatement(assignment, kNoSourcePosition));
1155 
1156  ExpectSemicolon();
1157  break;
1158  }
1159  }
1160 
1161  if (result != nullptr) {
1162  DCHECK_EQ(local_names.length(), 1);
1163  module()->AddExport(local_names.first(),
1164  ast_value_factory()->default_string(), default_loc,
1165  zone());
1166  }
1167 
1168  return result;
1169 }
1170 
1171 const AstRawString* Parser::NextInternalNamespaceExportName() {
1172  const char* prefix = ".ns-export";
1173  std::string s(prefix);
1174  s.append(std::to_string(number_of_named_namespace_exports_++));
1175  return ast_value_factory()->GetOneByteString(s.c_str());
1176 }
1177 
1178 void Parser::ParseExportStar() {
1179  int pos = position();
1180  Consume(Token::MUL);
1181 
1182  if (!FLAG_harmony_namespace_exports ||
1183  !PeekContextualKeyword(ast_value_factory()->as_string())) {
1184  // 'export' '*' 'from' ModuleSpecifier ';'
1185  Scanner::Location loc = scanner()->location();
1186  ExpectContextualKeyword(ast_value_factory()->from_string());
1187  Scanner::Location specifier_loc = scanner()->peek_location();
1188  const AstRawString* module_specifier = ParseModuleSpecifier();
1189  ExpectSemicolon();
1190  module()->AddStarExport(module_specifier, loc, specifier_loc, zone());
1191  return;
1192  }
1193  if (!FLAG_harmony_namespace_exports) return;
1194 
1195  // 'export' '*' 'as' IdentifierName 'from' ModuleSpecifier ';'
1196  //
1197  // Desugaring:
1198  // export * as x from "...";
1199  // ~>
1200  // import * as .x from "..."; export {.x as x};
1201 
1202  ExpectContextualKeyword(ast_value_factory()->as_string());
1203  const AstRawString* export_name = ParseIdentifierName();
1204  Scanner::Location export_name_loc = scanner()->location();
1205  const AstRawString* local_name = NextInternalNamespaceExportName();
1206  Scanner::Location local_name_loc = Scanner::Location::invalid();
1207  DeclareVariable(local_name, VariableMode::kConst, kCreatedInitialized, pos);
1208 
1209  ExpectContextualKeyword(ast_value_factory()->from_string());
1210  Scanner::Location specifier_loc = scanner()->peek_location();
1211  const AstRawString* module_specifier = ParseModuleSpecifier();
1212  ExpectSemicolon();
1213 
1214  module()->AddStarImport(local_name, module_specifier, local_name_loc,
1215  specifier_loc, zone());
1216  module()->AddExport(local_name, export_name, export_name_loc, zone());
1217 }
1218 
1219 Statement* Parser::ParseExportDeclaration() {
1220  // ExportDeclaration:
1221  // 'export' '*' 'from' ModuleSpecifier ';'
1222  // 'export' '*' 'as' IdentifierName 'from' ModuleSpecifier ';'
1223  // 'export' ExportClause ('from' ModuleSpecifier)? ';'
1224  // 'export' VariableStatement
1225  // 'export' Declaration
1226  // 'export' 'default' ... (handled in ParseExportDefault)
1227 
1228  Expect(Token::EXPORT);
1229  Statement* result = nullptr;
1230  ZonePtrList<const AstRawString> names(1, zone());
1231  Scanner::Location loc = scanner()->peek_location();
1232  switch (peek()) {
1233  case Token::DEFAULT:
1234  return ParseExportDefault();
1235 
1236  case Token::MUL:
1237  ParseExportStar();
1238  return factory()->EmptyStatement();
1239 
1240  case Token::LBRACE: {
1241  // There are two cases here:
1242  //
1243  // 'export' ExportClause ';'
1244  // and
1245  // 'export' ExportClause FromClause ';'
1246  //
1247  // In the first case, the exported identifiers in ExportClause must
1248  // not be reserved words, while in the latter they may be. We
1249  // pass in a location that gets filled with the first reserved word
1250  // encountered, and then throw a SyntaxError if we are in the
1251  // non-FromClause case.
1252  Scanner::Location reserved_loc = Scanner::Location::invalid();
1253  ZoneChunkList<ExportClauseData>* export_data =
1254  ParseExportClause(&reserved_loc);
1255  const AstRawString* module_specifier = nullptr;
1256  Scanner::Location specifier_loc;
1257  if (CheckContextualKeyword(ast_value_factory()->from_string())) {
1258  specifier_loc = scanner()->peek_location();
1259  module_specifier = ParseModuleSpecifier();
1260  } else if (reserved_loc.IsValid()) {
1261  // No FromClause, so reserved words are invalid in ExportClause.
1262  ReportMessageAt(reserved_loc, MessageTemplate::kUnexpectedReserved);
1263  return nullptr;
1264  }
1265  ExpectSemicolon();
1266  if (module_specifier == nullptr) {
1267  for (const ExportClauseData& data : *export_data) {
1268  module()->AddExport(data.local_name, data.export_name, data.location,
1269  zone());
1270  }
1271  } else if (export_data->is_empty()) {
1272  module()->AddEmptyImport(module_specifier, specifier_loc);
1273  } else {
1274  for (const ExportClauseData& data : *export_data) {
1275  module()->AddExport(data.local_name, data.export_name,
1276  module_specifier, data.location, specifier_loc,
1277  zone());
1278  }
1279  }
1280  return factory()->EmptyStatement();
1281  }
1282 
1283  case Token::FUNCTION:
1284  result = ParseHoistableDeclaration(&names, false);
1285  break;
1286 
1287  case Token::CLASS:
1288  Consume(Token::CLASS);
1289  result = ParseClassDeclaration(&names, false);
1290  break;
1291 
1292  case Token::VAR:
1293  case Token::LET:
1294  case Token::CONST:
1295  result = ParseVariableStatement(kStatementListItem, &names);
1296  break;
1297 
1298  case Token::ASYNC:
1299  Consume(Token::ASYNC);
1300  if (peek() == Token::FUNCTION &&
1301  !scanner()->HasLineTerminatorBeforeNext()) {
1302  result = ParseAsyncFunctionDeclaration(&names, false);
1303  break;
1304  }
1305  V8_FALLTHROUGH;
1306 
1307  default:
1308  ReportUnexpectedToken(scanner()->current_token());
1309  return nullptr;
1310  }
1311  loc.end_pos = scanner()->location().end_pos;
1312 
1313  ModuleDescriptor* descriptor = module();
1314  for (int i = 0; i < names.length(); ++i) {
1315  descriptor->AddExport(names[i], names[i], loc, zone());
1316  }
1317 
1318  return result;
1319 }
1320 
1321 VariableProxy* Parser::NewUnresolved(const AstRawString* name, int begin_pos,
1322  VariableKind kind) {
1323  return scope()->NewUnresolved(factory(), name, begin_pos, kind);
1324 }
1325 
1326 VariableProxy* Parser::NewUnresolved(const AstRawString* name) {
1327  return scope()->NewUnresolved(factory(), name, scanner()->location().beg_pos);
1328 }
1329 
1330 Declaration* Parser::DeclareVariable(const AstRawString* name,
1331  VariableMode mode, int pos) {
1332  return DeclareVariable(name, mode, Variable::DefaultInitializationFlag(mode),
1333  pos);
1334 }
1335 
1336 Declaration* Parser::DeclareVariable(const AstRawString* name,
1337  VariableMode mode, InitializationFlag init,
1338  int pos) {
1339  DCHECK_NOT_NULL(name);
1340  VariableProxy* proxy = factory()->NewVariableProxy(
1341  name, NORMAL_VARIABLE, scanner()->location().beg_pos);
1342  Declaration* declaration;
1343  if (mode == VariableMode::kVar && !scope()->is_declaration_scope()) {
1344  DCHECK(scope()->is_block_scope() || scope()->is_with_scope());
1345  declaration = factory()->NewNestedVariableDeclaration(proxy, scope(), pos);
1346  } else {
1347  declaration = factory()->NewVariableDeclaration(proxy, pos);
1348  }
1349  Declare(declaration, DeclarationDescriptor::NORMAL, mode, init, nullptr,
1350  scanner()->location().end_pos);
1351  return declaration;
1352 }
1353 
1354 Variable* Parser::Declare(Declaration* declaration,
1355  DeclarationDescriptor::Kind declaration_kind,
1356  VariableMode mode, InitializationFlag init,
1357  Scope* scope, int var_end_pos) {
1358  bool local_ok = true;
1359  if (scope == nullptr) {
1360  scope = this->scope();
1361  }
1362  bool sloppy_mode_block_scope_function_redefinition = false;
1363  Variable* variable = scope->DeclareVariable(
1364  declaration, mode, init, &sloppy_mode_block_scope_function_redefinition,
1365  &local_ok);
1366  if (!local_ok) {
1367  // If we only have the start position of a proxy, we can't highlight the
1368  // whole variable name. Pretend its length is 1 so that we highlight at
1369  // least the first character.
1370  Scanner::Location loc(declaration->proxy()->position(),
1371  var_end_pos != kNoSourcePosition
1372  ? var_end_pos
1373  : declaration->proxy()->position() + 1);
1374  if (declaration_kind == DeclarationDescriptor::PARAMETER) {
1375  ReportMessageAt(loc, MessageTemplate::kParamDupe);
1376  } else {
1377  ReportMessageAt(loc, MessageTemplate::kVarRedeclaration,
1378  declaration->proxy()->raw_name());
1379  }
1380  } else if (sloppy_mode_block_scope_function_redefinition) {
1381  ++use_counts_[v8::Isolate::kSloppyModeBlockScopedFunctionRedefinition];
1382  }
1383  return variable;
1384 }
1385 
1386 Block* Parser::BuildInitializationBlock(
1387  DeclarationParsingResult* parsing_result,
1388  ZonePtrList<const AstRawString>* names) {
1389  Block* result = factory()->NewBlock(1, true);
1390  for (const auto& declaration : parsing_result->declarations) {
1391  DeclareAndInitializeVariables(result, &(parsing_result->descriptor),
1392  &declaration, names);
1393  }
1394  return result;
1395 }
1396 
1397 Statement* Parser::DeclareFunction(const AstRawString* variable_name,
1398  FunctionLiteral* function, VariableMode mode,
1399  int pos, bool is_sloppy_block_function,
1400  ZonePtrList<const AstRawString>* names) {
1401  VariableProxy* proxy =
1402  factory()->NewVariableProxy(variable_name, NORMAL_VARIABLE, pos);
1403  Declaration* declaration = factory()->NewFunctionDeclaration(
1404  proxy, function, is_sloppy_block_function, pos);
1405  Declare(declaration, DeclarationDescriptor::NORMAL, mode,
1406  kCreatedInitialized);
1407  if (names) names->Add(variable_name, zone());
1408  if (is_sloppy_block_function) {
1409  SloppyBlockFunctionStatement* statement =
1410  factory()->NewSloppyBlockFunctionStatement();
1411  GetDeclarationScope()->DeclareSloppyBlockFunction(variable_name, scope(),
1412  statement);
1413  return statement;
1414  }
1415  return factory()->EmptyStatement();
1416 }
1417 
1418 Statement* Parser::DeclareClass(const AstRawString* variable_name,
1419  Expression* value,
1420  ZonePtrList<const AstRawString>* names,
1421  int class_token_pos, int end_pos) {
1422  Declaration* decl =
1423  DeclareVariable(variable_name, VariableMode::kLet, class_token_pos);
1424  decl->proxy()->var()->set_initializer_position(end_pos);
1425  if (names) names->Add(variable_name, zone());
1426 
1427  Assignment* assignment = factory()->NewAssignment(Token::INIT, decl->proxy(),
1428  value, class_token_pos);
1429  return IgnoreCompletion(
1430  factory()->NewExpressionStatement(assignment, kNoSourcePosition));
1431 }
1432 
1433 Statement* Parser::DeclareNative(const AstRawString* name, int pos) {
1434  // Make sure that the function containing the native declaration
1435  // isn't lazily compiled. The extension structures are only
1436  // accessible while parsing the first time not when reparsing
1437  // because of lazy compilation.
1438  GetClosureScope()->ForceEagerCompilation();
1439 
1440  // TODO(1240846): It's weird that native function declarations are
1441  // introduced dynamically when we meet their declarations, whereas
1442  // other functions are set up when entering the surrounding scope.
1443  Declaration* decl = DeclareVariable(name, VariableMode::kVar, pos);
1444  NativeFunctionLiteral* lit =
1445  factory()->NewNativeFunctionLiteral(name, extension_, kNoSourcePosition);
1446  return factory()->NewExpressionStatement(
1447  factory()->NewAssignment(Token::INIT, decl->proxy(), lit,
1448  kNoSourcePosition),
1449  pos);
1450 }
1451 
1452 void Parser::DeclareLabel(ZonePtrList<const AstRawString>** labels,
1453  ZonePtrList<const AstRawString>** own_labels,
1454  VariableProxy* var) {
1455  DCHECK(IsIdentifier(var));
1456  const AstRawString* label = var->raw_name();
1457 
1458  // TODO(1240780): We don't check for redeclaration of labels
1459  // during preparsing since keeping track of the set of active
1460  // labels requires nontrivial changes to the way scopes are
1461  // structured. However, these are probably changes we want to
1462  // make later anyway so we should go back and fix this then.
1463  if (ContainsLabel(*labels, label) || TargetStackContainsLabel(label)) {
1464  ReportMessage(MessageTemplate::kLabelRedeclaration, label);
1465  return;
1466  }
1467 
1468  // Add {label} to both {labels} and {own_labels}.
1469  if (*labels == nullptr) {
1470  DCHECK_NULL(*own_labels);
1471  *labels = new (zone()) ZonePtrList<const AstRawString>(1, zone());
1472  *own_labels = new (zone()) ZonePtrList<const AstRawString>(1, zone());
1473  } else {
1474  if (*own_labels == nullptr) {
1475  *own_labels = new (zone()) ZonePtrList<const AstRawString>(1, zone());
1476  }
1477  }
1478  (*labels)->Add(label, zone());
1479  (*own_labels)->Add(label, zone());
1480 
1481  // Remove the "ghost" variable that turned out to be a label
1482  // from the top scope. This way, we don't try to resolve it
1483  // during the scope processing.
1484  scope()->DeleteUnresolved(var);
1485 }
1486 
1487 bool Parser::ContainsLabel(ZonePtrList<const AstRawString>* labels,
1488  const AstRawString* label) {
1489  DCHECK_NOT_NULL(label);
1490  if (labels != nullptr) {
1491  for (int i = labels->length(); i-- > 0;) {
1492  if (labels->at(i) == label) return true;
1493  }
1494  }
1495  return false;
1496 }
1497 
1498 Block* Parser::IgnoreCompletion(Statement* statement) {
1499  Block* block = factory()->NewBlock(1, true);
1500  block->statements()->Add(statement, zone());
1501  return block;
1502 }
1503 
1504 Expression* Parser::RewriteReturn(Expression* return_value, int pos) {
1505  if (IsDerivedConstructor(function_state_->kind())) {
1506  // For subclass constructors we need to return this in case of undefined;
1507  // other primitive values trigger an exception in the ConstructStub.
1508  //
1509  // return expr;
1510  //
1511  // Is rewritten as:
1512  //
1513  // return (temp = expr) === undefined ? this : temp;
1514 
1515  // temp = expr
1516  Variable* temp = NewTemporary(ast_value_factory()->empty_string());
1517  Assignment* assign = factory()->NewAssignment(
1518  Token::ASSIGN, factory()->NewVariableProxy(temp), return_value, pos);
1519 
1520  // temp === undefined
1521  Expression* is_undefined = factory()->NewCompareOperation(
1522  Token::EQ_STRICT, assign,
1523  factory()->NewUndefinedLiteral(kNoSourcePosition), pos);
1524 
1525  // is_undefined ? this : temp
1526  return_value =
1527  factory()->NewConditional(is_undefined, ThisExpression(pos),
1528  factory()->NewVariableProxy(temp), pos);
1529  }
1530  return return_value;
1531 }
1532 
1533 Statement* Parser::RewriteSwitchStatement(SwitchStatement* switch_statement,
1534  Scope* scope) {
1535  // In order to get the CaseClauses to execute in their own lexical scope,
1536  // but without requiring downstream code to have special scope handling
1537  // code for switch statements, desugar into blocks as follows:
1538  // { // To group the statements--harmless to evaluate Expression in scope
1539  // .tag_variable = Expression;
1540  // { // To give CaseClauses a scope
1541  // switch (.tag_variable) { CaseClause* }
1542  // }
1543  // }
1544  DCHECK_NOT_NULL(scope);
1545  DCHECK(scope->is_block_scope());
1546  DCHECK_GE(switch_statement->position(), scope->start_position());
1547  DCHECK_LT(switch_statement->position(), scope->end_position());
1548 
1549  Block* switch_block = factory()->NewBlock(2, false);
1550 
1551  Expression* tag = switch_statement->tag();
1552  Variable* tag_variable =
1553  NewTemporary(ast_value_factory()->dot_switch_tag_string());
1554  Assignment* tag_assign = factory()->NewAssignment(
1555  Token::ASSIGN, factory()->NewVariableProxy(tag_variable), tag,
1556  tag->position());
1557  // Wrap with IgnoreCompletion so the tag isn't returned as the completion
1558  // value, in case the switch statements don't have a value.
1559  Statement* tag_statement = IgnoreCompletion(
1560  factory()->NewExpressionStatement(tag_assign, kNoSourcePosition));
1561  switch_block->statements()->Add(tag_statement, zone());
1562 
1563  switch_statement->set_tag(factory()->NewVariableProxy(tag_variable));
1564  Block* cases_block = factory()->NewBlock(1, false);
1565  cases_block->statements()->Add(switch_statement, zone());
1566  cases_block->set_scope(scope);
1567  switch_block->statements()->Add(cases_block, zone());
1568  return switch_block;
1569 }
1570 
1571 void Parser::RewriteCatchPattern(CatchInfo* catch_info) {
1572  if (catch_info->name == nullptr) {
1573  DCHECK_NOT_NULL(catch_info->pattern);
1574  catch_info->name = ast_value_factory()->dot_catch_string();
1575  }
1576  Variable* catch_variable =
1577  catch_info->scope->DeclareLocal(catch_info->name, VariableMode::kVar);
1578  if (catch_info->pattern != nullptr) {
1579  DeclarationDescriptor descriptor;
1580  descriptor.declaration_kind = DeclarationDescriptor::NORMAL;
1581  descriptor.scope = scope();
1582  descriptor.mode = VariableMode::kLet;
1583  descriptor.declaration_pos = catch_info->pattern->position();
1584  descriptor.initialization_pos = catch_info->pattern->position();
1585 
1586  // Initializer position for variables declared by the pattern.
1587  const int initializer_position = position();
1588 
1589  DeclarationParsingResult::Declaration decl(
1590  catch_info->pattern, initializer_position,
1591  factory()->NewVariableProxy(catch_variable));
1592 
1593  catch_info->init_block = factory()->NewBlock(8, true);
1594  DeclareAndInitializeVariables(catch_info->init_block, &descriptor, &decl,
1595  &catch_info->bound_names);
1596  } else {
1597  catch_info->bound_names.Add(catch_info->name, zone());
1598  }
1599 }
1600 
1601 void Parser::ValidateCatchBlock(const CatchInfo& catch_info) {
1602  // Check for `catch(e) { let e; }` and similar errors.
1603  Scope* inner_block_scope = catch_info.inner_block->scope();
1604  if (inner_block_scope != nullptr) {
1605  Declaration* decl = inner_block_scope->CheckLexDeclarationsConflictingWith(
1606  catch_info.bound_names);
1607  if (decl != nullptr) {
1608  const AstRawString* name = decl->proxy()->raw_name();
1609  int position = decl->proxy()->position();
1610  Scanner::Location location =
1611  position == kNoSourcePosition
1612  ? Scanner::Location::invalid()
1613  : Scanner::Location(position, position + 1);
1614  ReportMessageAt(location, MessageTemplate::kVarRedeclaration, name);
1615  }
1616  }
1617 }
1618 
1619 Statement* Parser::RewriteTryStatement(Block* try_block, Block* catch_block,
1620  const SourceRange& catch_range,
1621  Block* finally_block,
1622  const SourceRange& finally_range,
1623  const CatchInfo& catch_info, int pos) {
1624  // Simplify the AST nodes by converting:
1625  // 'try B0 catch B1 finally B2'
1626  // to:
1627  // 'try { try B0 catch B1 } finally B2'
1628 
1629  if (catch_block != nullptr && finally_block != nullptr) {
1630  // If we have both, create an inner try/catch.
1631  TryCatchStatement* statement;
1632  statement = factory()->NewTryCatchStatement(try_block, catch_info.scope,
1633  catch_block, kNoSourcePosition);
1634  RecordTryCatchStatementSourceRange(statement, catch_range);
1635 
1636  try_block = factory()->NewBlock(1, false);
1637  try_block->statements()->Add(statement, zone());
1638  catch_block = nullptr; // Clear to indicate it's been handled.
1639  }
1640 
1641  if (catch_block != nullptr) {
1642  DCHECK_NULL(finally_block);
1643  TryCatchStatement* stmt = factory()->NewTryCatchStatement(
1644  try_block, catch_info.scope, catch_block, pos);
1645  RecordTryCatchStatementSourceRange(stmt, catch_range);
1646  return stmt;
1647  } else {
1648  DCHECK_NOT_NULL(finally_block);
1649  TryFinallyStatement* stmt =
1650  factory()->NewTryFinallyStatement(try_block, finally_block, pos);
1651  RecordTryFinallyStatementSourceRange(stmt, finally_range);
1652  return stmt;
1653  }
1654 }
1655 
1656 void Parser::ParseAndRewriteGeneratorFunctionBody(
1657  int pos, FunctionKind kind, ScopedPtrList<Statement>* body) {
1658  // For ES6 Generators, we just prepend the initial yield.
1659  Expression* initial_yield = BuildInitialYield(pos, kind);
1660  body->Add(
1661  factory()->NewExpressionStatement(initial_yield, kNoSourcePosition));
1662  ParseStatementList(body, Token::RBRACE);
1663 }
1664 
1665 void Parser::ParseAndRewriteAsyncGeneratorFunctionBody(
1666  int pos, FunctionKind kind, ScopedPtrList<Statement>* body) {
1667  // For ES2017 Async Generators, we produce:
1668  //
1669  // try {
1670  // InitialYield;
1671  // ...body...;
1672  // return undefined; // See comment below
1673  // } catch (.catch) {
1674  // %AsyncGeneratorReject(generator, .catch);
1675  // } finally {
1676  // %_GeneratorClose(generator);
1677  // }
1678  //
1679  // - InitialYield yields the actual generator object.
1680  // - Any return statement inside the body will have its argument wrapped
1681  // in an iterator result object with a "done" property set to `true`.
1682  // - If the generator terminates for whatever reason, we must close it.
1683  // Hence the finally clause.
1684  // - BytecodeGenerator performs special handling for ReturnStatements in
1685  // async generator functions, resolving the appropriate Promise with an
1686  // "done" iterator result object containing a Promise-unwrapped value.
1687  DCHECK(IsAsyncGeneratorFunction(kind));
1688 
1689  Block* try_block;
1690  {
1691  ScopedPtrList<Statement> statements(pointer_buffer());
1692  Expression* initial_yield = BuildInitialYield(pos, kind);
1693  statements.Add(
1694  factory()->NewExpressionStatement(initial_yield, kNoSourcePosition));
1695  ParseStatementList(&statements, Token::RBRACE);
1696 
1697  // Don't create iterator result for async generators, as the resume methods
1698  // will create it.
1699  // TODO(leszeks): This will create another suspend point, which is
1700  // unnecessary if there is already an unconditional return in the body.
1701  Statement* final_return = BuildReturnStatement(
1702  factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition);
1703  statements.Add(final_return);
1704 
1705  try_block = factory()->NewBlock(false, statements);
1706  }
1707 
1708  // For AsyncGenerators, a top-level catch block will reject the Promise.
1709  Scope* catch_scope = NewHiddenCatchScope();
1710 
1711  Block* catch_block;
1712  {
1713  ScopedPtrList<Expression> reject_args(pointer_buffer());
1714  reject_args.Add(factory()->NewVariableProxy(
1715  function_state_->scope()->generator_object_var()));
1716  reject_args.Add(factory()->NewVariableProxy(catch_scope->catch_variable()));
1717 
1718  Expression* reject_call = factory()->NewCallRuntime(
1719  Runtime::kInlineAsyncGeneratorReject, reject_args, kNoSourcePosition);
1720  catch_block = IgnoreCompletion(
1721  factory()->NewReturnStatement(reject_call, kNoSourcePosition));
1722  }
1723 
1724  {
1725  ScopedPtrList<Statement> statements(pointer_buffer());
1726  TryStatement* try_catch = factory()->NewTryCatchStatementForAsyncAwait(
1727  try_block, catch_scope, catch_block, kNoSourcePosition);
1728  statements.Add(try_catch);
1729  try_block = factory()->NewBlock(false, statements);
1730  }
1731 
1732  Expression* close_call;
1733  {
1734  ScopedPtrList<Expression> close_args(pointer_buffer());
1735  VariableProxy* call_proxy = factory()->NewVariableProxy(
1736  function_state_->scope()->generator_object_var());
1737  close_args.Add(call_proxy);
1738  close_call = factory()->NewCallRuntime(Runtime::kInlineGeneratorClose,
1739  close_args, kNoSourcePosition);
1740  }
1741 
1742  Block* finally_block;
1743  {
1744  ScopedPtrList<Statement> statements(pointer_buffer());
1745  statements.Add(
1746  factory()->NewExpressionStatement(close_call, kNoSourcePosition));
1747  finally_block = factory()->NewBlock(false, statements);
1748  }
1749 
1750  body->Add(factory()->NewTryFinallyStatement(try_block, finally_block,
1751  kNoSourcePosition));
1752 }
1753 
1754 void Parser::DeclareFunctionNameVar(const AstRawString* function_name,
1755  FunctionLiteral::FunctionType function_type,
1756  DeclarationScope* function_scope) {
1757  if (function_type == FunctionLiteral::kNamedExpression &&
1758  function_scope->LookupLocal(function_name) == nullptr) {
1759  DCHECK_EQ(function_scope, scope());
1760  function_scope->DeclareFunctionVar(function_name);
1761  }
1762 }
1763 
1764 // [if (IteratorType == kNormal)]
1765 // !%_IsJSReceiver(result = iterator.next()) &&
1766 // %ThrowIteratorResultNotAnObject(result)
1767 // [else if (IteratorType == kAsync)]
1768 // !%_IsJSReceiver(result = Await(iterator.next())) &&
1769 // %ThrowIteratorResultNotAnObject(result)
1770 // [endif]
1771 Expression* Parser::BuildIteratorNextResult(VariableProxy* iterator,
1772  VariableProxy* next,
1773  Variable* result, IteratorType type,
1774  int pos) {
1775  Expression* next_property = factory()->NewResolvedProperty(iterator, next);
1776  ScopedPtrList<Expression> next_arguments(pointer_buffer());
1777  Expression* next_call =
1778  factory()->NewCall(next_property, next_arguments, kNoSourcePosition);
1779  if (type == IteratorType::kAsync) {
1780  function_state_->AddSuspend();
1781  next_call = factory()->NewAwait(next_call, pos);
1782  }
1783  Expression* result_proxy = factory()->NewVariableProxy(result);
1784  Expression* left =
1785  factory()->NewAssignment(Token::ASSIGN, result_proxy, next_call, pos);
1786 
1787  // %_IsJSReceiver(...)
1788  ScopedPtrList<Expression> is_spec_object_args(pointer_buffer());
1789  is_spec_object_args.Add(left);
1790  Expression* is_spec_object_call = factory()->NewCallRuntime(
1791  Runtime::kInlineIsJSReceiver, is_spec_object_args, pos);
1792 
1793  // %ThrowIteratorResultNotAnObject(result)
1794  Expression* result_proxy_again = factory()->NewVariableProxy(result);
1795  ScopedPtrList<Expression> throw_arguments(pointer_buffer());
1796  throw_arguments.Add(result_proxy_again);
1797  Expression* throw_call = factory()->NewCallRuntime(
1798  Runtime::kThrowIteratorResultNotAnObject, throw_arguments, pos);
1799 
1800  return factory()->NewBinaryOperation(
1801  Token::AND,
1802  factory()->NewUnaryOperation(Token::NOT, is_spec_object_call, pos),
1803  throw_call, pos);
1804 }
1805 
1806 Statement* Parser::InitializeForEachStatement(ForEachStatement* stmt,
1807  Expression* each,
1808  Expression* subject,
1809  Statement* body) {
1810  ForOfStatement* for_of = stmt->AsForOfStatement();
1811  if (for_of != nullptr) {
1812  const bool finalize = true;
1813  return InitializeForOfStatement(for_of, each, subject, body, finalize,
1814  IteratorType::kNormal, each->position());
1815  } else {
1816  if (each->IsPattern()) {
1817  Variable* temp = NewTemporary(ast_value_factory()->empty_string());
1818  VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
1819  Expression* assign_each =
1820  RewriteDestructuringAssignment(factory()->NewAssignment(
1821  Token::ASSIGN, each, temp_proxy, kNoSourcePosition));
1822  auto block = factory()->NewBlock(2, false);
1823  block->statements()->Add(
1824  factory()->NewExpressionStatement(assign_each, kNoSourcePosition),
1825  zone());
1826  block->statements()->Add(body, zone());
1827  body = block;
1828  each = factory()->NewVariableProxy(temp);
1829  }
1830  MarkExpressionAsAssigned(each);
1831  stmt->AsForInStatement()->Initialize(each, subject, body);
1832  }
1833  return stmt;
1834 }
1835 
1836 // Special case for legacy for
1837 //
1838 // for (var x = initializer in enumerable) body
1839 //
1840 // An initialization block of the form
1841 //
1842 // {
1843 // x = initializer;
1844 // }
1845 //
1846 // is returned in this case. It has reserved space for two statements,
1847 // so that (later on during parsing), the equivalent of
1848 //
1849 // for (x in enumerable) body
1850 //
1851 // is added as a second statement to it.
1852 Block* Parser::RewriteForVarInLegacy(const ForInfo& for_info) {
1853  const DeclarationParsingResult::Declaration& decl =
1854  for_info.parsing_result.declarations[0];
1855  if (!IsLexicalVariableMode(for_info.parsing_result.descriptor.mode) &&
1856  decl.pattern->IsVariableProxy() && decl.initializer != nullptr) {
1857  ++use_counts_[v8::Isolate::kForInInitializer];
1858  const AstRawString* name = decl.pattern->AsVariableProxy()->raw_name();
1859  VariableProxy* single_var = NewUnresolved(name);
1860  Block* init_block = factory()->NewBlock(2, true);
1861  init_block->statements()->Add(
1862  factory()->NewExpressionStatement(
1863  factory()->NewAssignment(Token::ASSIGN, single_var,
1864  decl.initializer, kNoSourcePosition),
1865  kNoSourcePosition),
1866  zone());
1867  return init_block;
1868  }
1869  return nullptr;
1870 }
1871 
1872 // Rewrite a for-in/of statement of the form
1873 //
1874 // for (let/const/var x in/of e) b
1875 //
1876 // into
1877 //
1878 // {
1879 // var temp;
1880 // for (temp in/of e) {
1881 // let/const/var x = temp;
1882 // b;
1883 // }
1884 // let x; // for TDZ
1885 // }
1886 void Parser::DesugarBindingInForEachStatement(ForInfo* for_info,
1887  Block** body_block,
1888  Expression** each_variable) {
1889  DCHECK_EQ(1, for_info->parsing_result.declarations.size());
1890  DeclarationParsingResult::Declaration& decl =
1891  for_info->parsing_result.declarations[0];
1892  Variable* temp = NewTemporary(ast_value_factory()->dot_for_string());
1893  auto each_initialization_block = factory()->NewBlock(1, true);
1894  {
1895  auto descriptor = for_info->parsing_result.descriptor;
1896  descriptor.declaration_pos = kNoSourcePosition;
1897  descriptor.initialization_pos = kNoSourcePosition;
1898  descriptor.scope = scope();
1899  decl.initializer = factory()->NewVariableProxy(temp);
1900 
1901  bool is_for_var_of =
1902  for_info->mode == ForEachStatement::ITERATE &&
1903  for_info->parsing_result.descriptor.mode == VariableMode::kVar;
1904  bool collect_names =
1905  IsLexicalVariableMode(for_info->parsing_result.descriptor.mode) ||
1906  is_for_var_of;
1907 
1908  DeclareAndInitializeVariables(
1909  each_initialization_block, &descriptor, &decl,
1910  collect_names ? &for_info->bound_names : nullptr);
1911 
1912  // Annex B.3.5 prohibits the form
1913  // `try {} catch(e) { for (var e of {}); }`
1914  // So if we are parsing a statement like `for (var ... of ...)`
1915  // we need to walk up the scope chain and look for catch scopes
1916  // which have a simple binding, then compare their binding against
1917  // all of the names declared in the init of the for-of we're
1918  // parsing.
1919  if (is_for_var_of) {
1920  Scope* catch_scope = scope();
1921  while (catch_scope != nullptr && !catch_scope->is_declaration_scope()) {
1922  if (catch_scope->is_catch_scope()) {
1923  auto name = catch_scope->catch_variable()->raw_name();
1924  // If it's a simple binding and the name is declared in the for loop.
1925  if (name != ast_value_factory()->dot_catch_string() &&
1926  for_info->bound_names.Contains(name)) {
1927  ReportMessageAt(for_info->parsing_result.bindings_loc,
1928  MessageTemplate::kVarRedeclaration, name);
1929  }
1930  }
1931  catch_scope = catch_scope->outer_scope();
1932  }
1933  }
1934  }
1935 
1936  *body_block = factory()->NewBlock(3, false);
1937  (*body_block)->statements()->Add(each_initialization_block, zone());
1938  *each_variable = factory()->NewVariableProxy(temp, for_info->position);
1939 }
1940 
1941 // Create a TDZ for any lexically-bound names in for in/of statements.
1942 Block* Parser::CreateForEachStatementTDZ(Block* init_block,
1943  const ForInfo& for_info) {
1944  if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) {
1945  DCHECK_NULL(init_block);
1946 
1947  init_block = factory()->NewBlock(1, false);
1948 
1949  for (int i = 0; i < for_info.bound_names.length(); ++i) {
1950  // TODO(adamk): This needs to be some sort of special
1951  // INTERNAL variable that's invisible to the debugger
1952  // but visible to everything else.
1953  Declaration* tdz_decl = DeclareVariable(
1954  for_info.bound_names[i], VariableMode::kLet, kNoSourcePosition);
1955  tdz_decl->proxy()->var()->set_initializer_position(position());
1956  }
1957  }
1958  return init_block;
1959 }
1960 
1961 Statement* Parser::InitializeForOfStatement(
1962  ForOfStatement* for_of, Expression* each, Expression* iterable,
1963  Statement* body, bool finalize, IteratorType type, int next_result_pos) {
1964  // Create the auxiliary expressions needed for iterating over the iterable,
1965  // and initialize the given ForOfStatement with them.
1966  // If finalize is true, also instrument the loop with code that performs the
1967  // proper ES6 iterator finalization. In that case, the result is not
1968  // immediately a ForOfStatement.
1969  const int nopos = kNoSourcePosition;
1970  auto avfactory = ast_value_factory();
1971 
1972  Variable* iterator = NewTemporary(avfactory->dot_iterator_string());
1973  Variable* next = NewTemporary(avfactory->empty_string());
1974  Variable* result = NewTemporary(avfactory->dot_result_string());
1975  Variable* completion = NewTemporary(avfactory->empty_string());
1976 
1977  // iterator = GetIterator(iterable, type)
1978  Expression* assign_iterator;
1979  {
1980  assign_iterator = factory()->NewAssignment(
1981  Token::ASSIGN, factory()->NewVariableProxy(iterator),
1982  factory()->NewGetIterator(iterable, type, iterable->position()),
1983  iterable->position());
1984  }
1985 
1986  Expression* assign_next;
1987  {
1988  assign_next = factory()->NewAssignment(
1989  Token::ASSIGN, factory()->NewVariableProxy(next),
1990  factory()->NewProperty(factory()->NewVariableProxy(iterator),
1991  factory()->NewStringLiteral(
1992  avfactory->next_string(), kNoSourcePosition),
1993  kNoSourcePosition),
1994  kNoSourcePosition);
1995  }
1996 
1997  // [if (IteratorType == kNormal)]
1998  // !%_IsJSReceiver(result = iterator.next()) &&
1999  // %ThrowIteratorResultNotAnObject(result)
2000  // [else if (IteratorType == kAsync)]
2001  // !%_IsJSReceiver(result = Await(iterator.next())) &&
2002  // %ThrowIteratorResultNotAnObject(result)
2003  // [endif]
2004  Expression* next_result;
2005  {
2006  VariableProxy* iterator_proxy = factory()->NewVariableProxy(iterator);
2007  VariableProxy* next_proxy = factory()->NewVariableProxy(next);
2008  next_result = BuildIteratorNextResult(iterator_proxy, next_proxy, result,
2009  type, next_result_pos);
2010  }
2011 
2012  // result.done
2013  Expression* result_done;
2014  {
2015  Expression* done_literal = factory()->NewStringLiteral(
2016  ast_value_factory()->done_string(), kNoSourcePosition);
2017  Expression* result_proxy = factory()->NewVariableProxy(result);
2018  result_done =
2019  factory()->NewProperty(result_proxy, done_literal, kNoSourcePosition);
2020  }
2021 
2022  // result.value
2023  Expression* result_value;
2024  {
2025  Expression* value_literal =
2026  factory()->NewStringLiteral(avfactory->value_string(), nopos);
2027  Expression* result_proxy = factory()->NewVariableProxy(result);
2028  result_value = factory()->NewProperty(result_proxy, value_literal, nopos);
2029  }
2030 
2031  // {{tmp = #result_value, completion = kAbruptCompletion, tmp}}
2032  // Expression* result_value (gets overwritten)
2033  if (finalize) {
2034  Variable* tmp = NewTemporary(avfactory->empty_string());
2035  Expression* save_result = factory()->NewAssignment(
2036  Token::ASSIGN, factory()->NewVariableProxy(tmp), result_value, nopos);
2037 
2038  Expression* set_completion_abrupt = factory()->NewAssignment(
2039  Token::ASSIGN, factory()->NewVariableProxy(completion),
2040  factory()->NewSmiLiteral(Parser::kAbruptCompletion, nopos), nopos);
2041 
2042  result_value = factory()->NewBinaryOperation(Token::COMMA, save_result,
2043  set_completion_abrupt, nopos);
2044  result_value = factory()->NewBinaryOperation(
2045  Token::COMMA, result_value, factory()->NewVariableProxy(tmp), nopos);
2046  }
2047 
2048  // each = #result_value;
2049  Expression* assign_each;
2050  {
2051  assign_each =
2052  factory()->NewAssignment(Token::ASSIGN, each, result_value, nopos);
2053  if (each->IsPattern()) {
2054  assign_each = RewriteDestructuringAssignment(assign_each->AsAssignment());
2055  }
2056  }
2057 
2058  // {{completion = kNormalCompletion;}}
2059  Statement* set_completion_normal;
2060  if (finalize) {
2061  Expression* proxy = factory()->NewVariableProxy(completion);
2062  Expression* assignment = factory()->NewAssignment(
2063  Token::ASSIGN, proxy,
2064  factory()->NewSmiLiteral(Parser::kNormalCompletion, nopos), nopos);
2065 
2066  set_completion_normal =
2067  IgnoreCompletion(factory()->NewExpressionStatement(assignment, nopos));
2068  }
2069 
2070  // { #loop-body; #set_completion_normal }
2071  // Statement* body (gets overwritten)
2072  if (finalize) {
2073  Block* block = factory()->NewBlock(2, false);
2074  block->statements()->Add(body, zone());
2075  block->statements()->Add(set_completion_normal, zone());
2076  body = block;
2077  }
2078 
2079  for_of->Initialize(body, iterator, assign_iterator, assign_next, next_result,
2080  result_done, assign_each);
2081  return finalize ? FinalizeForOfStatement(for_of, completion, type, nopos)
2082  : for_of;
2083 }
2084 
2085 Statement* Parser::DesugarLexicalBindingsInForStatement(
2086  ForStatement* loop, Statement* init, Expression* cond, Statement* next,
2087  Statement* body, Scope* inner_scope, const ForInfo& for_info) {
2088  // ES6 13.7.4.8 specifies that on each loop iteration the let variables are
2089  // copied into a new environment. Moreover, the "next" statement must be
2090  // evaluated not in the environment of the just completed iteration but in
2091  // that of the upcoming one. We achieve this with the following desugaring.
2092  // Extra care is needed to preserve the completion value of the original loop.
2093  //
2094  // We are given a for statement of the form
2095  //
2096  // labels: for (let/const x = i; cond; next) body
2097  //
2098  // and rewrite it as follows. Here we write {{ ... }} for init-blocks, ie.,
2099  // blocks whose ignore_completion_value_ flag is set.
2100  //
2101  // {
2102  // let/const x = i;
2103  // temp_x = x;
2104  // first = 1;
2105  // undefined;
2106  // outer: for (;;) {
2107  // let/const x = temp_x;
2108  // {{ if (first == 1) {
2109  // first = 0;
2110  // } else {
2111  // next;
2112  // }
2113  // flag = 1;
2114  // if (!cond) break;
2115  // }}
2116  // labels: for (; flag == 1; flag = 0, temp_x = x) {
2117  // body
2118  // }
2119  // {{ if (flag == 1) // Body used break.
2120  // break;
2121  // }}
2122  // }
2123  // }
2124 
2125  DCHECK_GT(for_info.bound_names.length(), 0);
2126  ZonePtrList<Variable> temps(for_info.bound_names.length(), zone());
2127 
2128  Block* outer_block =
2129  factory()->NewBlock(for_info.bound_names.length() + 4, false);
2130 
2131  // Add statement: let/const x = i.
2132  outer_block->statements()->Add(init, zone());
2133 
2134  const AstRawString* temp_name = ast_value_factory()->dot_for_string();
2135 
2136  // For each lexical variable x:
2137  // make statement: temp_x = x.
2138  for (int i = 0; i < for_info.bound_names.length(); i++) {
2139  VariableProxy* proxy = NewUnresolved(for_info.bound_names[i]);
2140  Variable* temp = NewTemporary(temp_name);
2141  VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
2142  Assignment* assignment = factory()->NewAssignment(Token::ASSIGN, temp_proxy,
2143  proxy, kNoSourcePosition);
2144  Statement* assignment_statement =
2145  factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2146  outer_block->statements()->Add(assignment_statement, zone());
2147  temps.Add(temp, zone());
2148  }
2149 
2150  Variable* first = nullptr;
2151  // Make statement: first = 1.
2152  if (next) {
2153  first = NewTemporary(temp_name);
2154  VariableProxy* first_proxy = factory()->NewVariableProxy(first);
2155  Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2156  Assignment* assignment = factory()->NewAssignment(
2157  Token::ASSIGN, first_proxy, const1, kNoSourcePosition);
2158  Statement* assignment_statement =
2159  factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2160  outer_block->statements()->Add(assignment_statement, zone());
2161  }
2162 
2163  // make statement: undefined;
2164  outer_block->statements()->Add(
2165  factory()->NewExpressionStatement(
2166  factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition),
2167  zone());
2168 
2169  // Make statement: outer: for (;;)
2170  // Note that we don't actually create the label, or set this loop up as an
2171  // explicit break target, instead handing it directly to those nodes that
2172  // need to know about it. This should be safe because we don't run any code
2173  // in this function that looks up break targets.
2174  ForStatement* outer_loop =
2175  factory()->NewForStatement(nullptr, nullptr, kNoSourcePosition);
2176  outer_block->statements()->Add(outer_loop, zone());
2177  outer_block->set_scope(scope());
2178 
2179  Block* inner_block = factory()->NewBlock(3, false);
2180  {
2181  BlockState block_state(&scope_, inner_scope);
2182 
2183  Block* ignore_completion_block =
2184  factory()->NewBlock(for_info.bound_names.length() + 3, true);
2185  ZonePtrList<Variable> inner_vars(for_info.bound_names.length(), zone());
2186  // For each let variable x:
2187  // make statement: let/const x = temp_x.
2188  for (int i = 0; i < for_info.bound_names.length(); i++) {
2189  Declaration* decl = DeclareVariable(
2190  for_info.bound_names[i], for_info.parsing_result.descriptor.mode,
2191  kNoSourcePosition);
2192  inner_vars.Add(decl->proxy()->var(), zone());
2193  VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
2194  Assignment* assignment = factory()->NewAssignment(
2195  Token::INIT, decl->proxy(), temp_proxy, kNoSourcePosition);
2196  Statement* assignment_statement =
2197  factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2198  int declaration_pos = for_info.parsing_result.descriptor.declaration_pos;
2199  DCHECK_NE(declaration_pos, kNoSourcePosition);
2200  decl->proxy()->var()->set_initializer_position(declaration_pos);
2201  ignore_completion_block->statements()->Add(assignment_statement, zone());
2202  }
2203 
2204  // Make statement: if (first == 1) { first = 0; } else { next; }
2205  if (next) {
2206  DCHECK(first);
2207  Expression* compare = nullptr;
2208  // Make compare expression: first == 1.
2209  {
2210  Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2211  VariableProxy* first_proxy = factory()->NewVariableProxy(first);
2212  compare = factory()->NewCompareOperation(Token::EQ, first_proxy, const1,
2213  kNoSourcePosition);
2214  }
2215  Statement* clear_first = nullptr;
2216  // Make statement: first = 0.
2217  {
2218  VariableProxy* first_proxy = factory()->NewVariableProxy(first);
2219  Expression* const0 = factory()->NewSmiLiteral(0, kNoSourcePosition);
2220  Assignment* assignment = factory()->NewAssignment(
2221  Token::ASSIGN, first_proxy, const0, kNoSourcePosition);
2222  clear_first =
2223  factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2224  }
2225  Statement* clear_first_or_next = factory()->NewIfStatement(
2226  compare, clear_first, next, kNoSourcePosition);
2227  ignore_completion_block->statements()->Add(clear_first_or_next, zone());
2228  }
2229 
2230  Variable* flag = NewTemporary(temp_name);
2231  // Make statement: flag = 1.
2232  {
2233  VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2234  Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2235  Assignment* assignment = factory()->NewAssignment(
2236  Token::ASSIGN, flag_proxy, const1, kNoSourcePosition);
2237  Statement* assignment_statement =
2238  factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2239  ignore_completion_block->statements()->Add(assignment_statement, zone());
2240  }
2241 
2242  // Make statement: if (!cond) break.
2243  if (cond) {
2244  Statement* stop =
2245  factory()->NewBreakStatement(outer_loop, kNoSourcePosition);
2246  Statement* noop = factory()->EmptyStatement();
2247  ignore_completion_block->statements()->Add(
2248  factory()->NewIfStatement(cond, noop, stop, cond->position()),
2249  zone());
2250  }
2251 
2252  inner_block->statements()->Add(ignore_completion_block, zone());
2253  // Make cond expression for main loop: flag == 1.
2254  Expression* flag_cond = nullptr;
2255  {
2256  Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2257  VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2258  flag_cond = factory()->NewCompareOperation(Token::EQ, flag_proxy, const1,
2259  kNoSourcePosition);
2260  }
2261 
2262  // Create chain of expressions "flag = 0, temp_x = x, ..."
2263  Statement* compound_next_statement = nullptr;
2264  {
2265  Expression* compound_next = nullptr;
2266  // Make expression: flag = 0.
2267  {
2268  VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2269  Expression* const0 = factory()->NewSmiLiteral(0, kNoSourcePosition);
2270  compound_next = factory()->NewAssignment(Token::ASSIGN, flag_proxy,
2271  const0, kNoSourcePosition);
2272  }
2273 
2274  // Make the comma-separated list of temp_x = x assignments.
2275  int inner_var_proxy_pos = scanner()->location().beg_pos;
2276  for (int i = 0; i < for_info.bound_names.length(); i++) {
2277  VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
2278  VariableProxy* proxy =
2279  factory()->NewVariableProxy(inner_vars.at(i), inner_var_proxy_pos);
2280  Assignment* assignment = factory()->NewAssignment(
2281  Token::ASSIGN, temp_proxy, proxy, kNoSourcePosition);
2282  compound_next = factory()->NewBinaryOperation(
2283  Token::COMMA, compound_next, assignment, kNoSourcePosition);
2284  }
2285 
2286  compound_next_statement =
2287  factory()->NewExpressionStatement(compound_next, kNoSourcePosition);
2288  }
2289 
2290  // Make statement: labels: for (; flag == 1; flag = 0, temp_x = x)
2291  // Note that we re-use the original loop node, which retains its labels
2292  // and ensures that any break or continue statements in body point to
2293  // the right place.
2294  loop->Initialize(nullptr, flag_cond, compound_next_statement, body);
2295  inner_block->statements()->Add(loop, zone());
2296 
2297  // Make statement: {{if (flag == 1) break;}}
2298  {
2299  Expression* compare = nullptr;
2300  // Make compare expresion: flag == 1.
2301  {
2302  Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2303  VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2304  compare = factory()->NewCompareOperation(Token::EQ, flag_proxy, const1,
2305  kNoSourcePosition);
2306  }
2307  Statement* stop =
2308  factory()->NewBreakStatement(outer_loop, kNoSourcePosition);
2309  Statement* empty = factory()->EmptyStatement();
2310  Statement* if_flag_break =
2311  factory()->NewIfStatement(compare, stop, empty, kNoSourcePosition);
2312  inner_block->statements()->Add(IgnoreCompletion(if_flag_break), zone());
2313  }
2314 
2315  inner_block->set_scope(inner_scope);
2316  }
2317 
2318  outer_loop->Initialize(nullptr, nullptr, nullptr, inner_block);
2319 
2320  return outer_block;
2321 }
2322 
2323 void Parser::AddArrowFunctionFormalParameters(
2324  ParserFormalParameters* parameters, Expression* expr, int end_pos) {
2325  // ArrowFunctionFormals ::
2326  // Nary(Token::COMMA, VariableProxy*, Tail)
2327  // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail)
2328  // Tail
2329  // NonTailArrowFunctionFormals ::
2330  // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy)
2331  // VariableProxy
2332  // Tail ::
2333  // VariableProxy
2334  // Spread(VariableProxy)
2335  //
2336  // We need to visit the parameters in left-to-right order
2337  //
2338 
2339  // For the Nary case, we simply visit the parameters in a loop.
2340  if (expr->IsNaryOperation()) {
2341  NaryOperation* nary = expr->AsNaryOperation();
2342  // The classifier has already run, so we know that the expression is a valid
2343  // arrow function formals production.
2344  DCHECK_EQ(nary->op(), Token::COMMA);
2345  // Each op position is the end position of the *previous* expr, with the
2346  // second (i.e. first "subsequent") op position being the end position of
2347  // the first child expression.
2348  Expression* next = nary->first();
2349  for (size_t i = 0; i < nary->subsequent_length(); ++i) {
2350  AddArrowFunctionFormalParameters(parameters, next,
2351  nary->subsequent_op_position(i));
2352  next = nary->subsequent(i);
2353  }
2354  AddArrowFunctionFormalParameters(parameters, next, end_pos);
2355  return;
2356  }
2357 
2358  // For the binary case, we recurse on the left-hand side of binary comma
2359  // expressions.
2360  if (expr->IsBinaryOperation()) {
2361  BinaryOperation* binop = expr->AsBinaryOperation();
2362  // The classifier has already run, so we know that the expression is a valid
2363  // arrow function formals production.
2364  DCHECK_EQ(binop->op(), Token::COMMA);
2365  Expression* left = binop->left();
2366  Expression* right = binop->right();
2367  int comma_pos = binop->position();
2368  AddArrowFunctionFormalParameters(parameters, left, comma_pos);
2369  // LHS of comma expression should be unparenthesized.
2370  expr = right;
2371  }
2372 
2373  // Only the right-most expression may be a rest parameter.
2374  DCHECK(!parameters->has_rest);
2375 
2376  bool is_rest = expr->IsSpread();
2377  if (is_rest) {
2378  expr = expr->AsSpread()->expression();
2379  parameters->has_rest = true;
2380  }
2381  DCHECK_IMPLIES(parameters->is_simple, !is_rest);
2382  DCHECK_IMPLIES(parameters->is_simple, expr->IsVariableProxy());
2383 
2384  Expression* initializer = nullptr;
2385  if (expr->IsAssignment()) {
2386  if (expr->IsRewritableExpression()) {
2387  // This expression was parsed as a possible destructuring assignment.
2388  // Mark it as already-rewritten to avoid an unnecessary visit later.
2389  expr->AsRewritableExpression()->set_rewritten();
2390  }
2391  Assignment* assignment = expr->AsAssignment();
2392  DCHECK(!assignment->IsCompoundAssignment());
2393  initializer = assignment->value();
2394  expr = assignment->target();
2395  }
2396 
2397  AddFormalParameter(parameters, expr, initializer,
2398  end_pos, is_rest);
2399 }
2400 
2401 void Parser::DeclareArrowFunctionFormalParameters(
2402  ParserFormalParameters* parameters, Expression* expr,
2403  const Scanner::Location& params_loc) {
2404  if (expr->IsEmptyParentheses() || has_error()) return;
2405 
2406  AddArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos);
2407 
2408  if (parameters->arity > Code::kMaxArguments) {
2409  ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
2410  return;
2411  }
2412 
2413  DeclareFormalParameters(parameters);
2414  DCHECK_IMPLIES(parameters->is_simple,
2415  parameters->scope->has_simple_parameters());
2416 }
2417 
2418 void Parser::PrepareGeneratorVariables() {
2419  // Calling a generator returns a generator object. That object is stored
2420  // in a temporary variable, a definition that is used by "yield"
2421  // expressions.
2422  function_state_->scope()->DeclareGeneratorObjectVar(
2423  ast_value_factory()->dot_generator_object_string());
2424 }
2425 
2426 FunctionLiteral* Parser::ParseFunctionLiteral(
2427  const AstRawString* function_name, Scanner::Location function_name_location,
2428  FunctionNameValidity function_name_validity, FunctionKind kind,
2429  int function_token_pos, FunctionLiteral::FunctionType function_type,
2430  LanguageMode language_mode,
2431  ZonePtrList<const AstRawString>* arguments_for_wrapped_function) {
2432  // Function ::
2433  // '(' FormalParameterList? ')' '{' FunctionBody '}'
2434  //
2435  // Getter ::
2436  // '(' ')' '{' FunctionBody '}'
2437  //
2438  // Setter ::
2439  // '(' PropertySetParameterList ')' '{' FunctionBody '}'
2440 
2441  bool is_wrapped = function_type == FunctionLiteral::kWrapped;
2442  DCHECK_EQ(is_wrapped, arguments_for_wrapped_function != nullptr);
2443 
2444  int pos = function_token_pos == kNoSourcePosition ? peek_position()
2445  : function_token_pos;
2446  DCHECK_NE(kNoSourcePosition, pos);
2447 
2448  // Anonymous functions were passed either the empty symbol or a null
2449  // handle as the function name. Remember if we were passed a non-empty
2450  // handle to decide whether to invoke function name inference.
2451  bool should_infer_name = function_name == nullptr;
2452 
2453  // We want a non-null handle as the function name by default. We will handle
2454  // the "function does not have a shared name" case later.
2455  if (should_infer_name) {
2456  function_name = ast_value_factory()->empty_string();
2457  }
2458 
2459  FunctionLiteral::EagerCompileHint eager_compile_hint =
2460  function_state_->next_function_is_likely_called() || is_wrapped
2461  ? FunctionLiteral::kShouldEagerCompile
2462  : default_eager_compile_hint();
2463 
2464  // Determine if the function can be parsed lazily. Lazy parsing is
2465  // different from lazy compilation; we need to parse more eagerly than we
2466  // compile.
2467 
2468  // We can only parse lazily if we also compile lazily. The heuristics for lazy
2469  // compilation are:
2470  // - It must not have been prohibited by the caller to Parse (some callers
2471  // need a full AST).
2472  // - The outer scope must allow lazy compilation of inner functions.
2473  // - The function mustn't be a function expression with an open parenthesis
2474  // before; we consider that a hint that the function will be called
2475  // immediately, and it would be a waste of time to make it lazily
2476  // compiled.
2477  // These are all things we can know at this point, without looking at the
2478  // function itself.
2479 
2480  // We separate between lazy parsing top level functions and lazy parsing inner
2481  // functions, because the latter needs to do more work. In particular, we need
2482  // to track unresolved variables to distinguish between these cases:
2483  // (function foo() {
2484  // bar = function() { return 1; }
2485  // })();
2486  // and
2487  // (function foo() {
2488  // var a = 1;
2489  // bar = function() { return a; }
2490  // })();
2491 
2492  // Now foo will be parsed eagerly and compiled eagerly (optimization: assume
2493  // parenthesis before the function means that it will be called
2494  // immediately). bar can be parsed lazily, but we need to parse it in a mode
2495  // that tracks unresolved variables.
2496  DCHECK_IMPLIES(parse_lazily(), FLAG_lazy);
2497  DCHECK_IMPLIES(parse_lazily(), has_error() || allow_lazy_);
2498  DCHECK_IMPLIES(parse_lazily(), extension_ == nullptr);
2499 
2500  const bool is_lazy =
2501  eager_compile_hint == FunctionLiteral::kShouldLazyCompile;
2502  const bool is_top_level = AllowsLazyParsingWithoutUnresolvedVariables();
2503  const bool is_eager_top_level_function = !is_lazy && is_top_level;
2504  const bool is_lazy_top_level_function = is_lazy && is_top_level;
2505  const bool is_lazy_inner_function = is_lazy && !is_top_level;
2506 
2507  RuntimeCallTimerScope runtime_timer(
2508  runtime_call_stats_,
2509  parsing_on_main_thread_
2510  ? RuntimeCallCounterId::kParseFunctionLiteral
2511  : RuntimeCallCounterId::kParseBackgroundFunctionLiteral);
2512  base::ElapsedTimer timer;
2513  if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
2514 
2515  // Determine whether we can still lazy parse the inner function.
2516  // The preconditions are:
2517  // - Lazy compilation has to be enabled.
2518  // - Neither V8 natives nor native function declarations can be allowed,
2519  // since parsing one would retroactively force the function to be
2520  // eagerly compiled.
2521  // - The invoker of this parser can't depend on the AST being eagerly
2522  // built (either because the function is about to be compiled, or
2523  // because the AST is going to be inspected for some reason).
2524  // - Because of the above, we can't be attempting to parse a
2525  // FunctionExpression; even without enclosing parentheses it might be
2526  // immediately invoked.
2527  // - The function literal shouldn't be hinted to eagerly compile.
2528 
2529  // Inner functions will be parsed using a temporary Zone. After parsing, we
2530  // will migrate unresolved variable into a Scope in the main Zone.
2531 
2532  const bool should_preparse_inner = parse_lazily() && is_lazy_inner_function;
2533 
2534  // If parallel compile tasks are enabled, and the function is an eager
2535  // top level function, then we can pre-parse the function and parse / compile
2536  // in a parallel task on a worker thread.
2537  bool should_post_parallel_task =
2538  parse_lazily() && is_eager_top_level_function &&
2539  FLAG_parallel_compile_tasks && info()->parallel_tasks() &&
2540  scanner()->stream()->can_be_cloned_for_parallel_access();
2541 
2542  // This may be modified later to reflect preparsing decision taken
2543  bool should_preparse = (parse_lazily() && is_lazy_top_level_function) ||
2544  should_preparse_inner || should_post_parallel_task;
2545 
2546  ScopedPtrList<Statement> body(pointer_buffer());
2547  int expected_property_count = -1;
2548  int suspend_count = -1;
2549  int num_parameters = -1;
2550  int function_length = -1;
2551  bool has_duplicate_parameters = false;
2552  int function_literal_id = GetNextFunctionLiteralId();
2553  ProducedPreParsedScopeData* produced_preparsed_scope_data = nullptr;
2554 
2555  // This Scope lives in the main zone. We'll migrate data into that zone later.
2556  Zone* parse_zone = should_preparse ? &preparser_zone_ : zone();
2557  DeclarationScope* scope = NewFunctionScope(kind, parse_zone);
2558  SetLanguageMode(scope, language_mode);
2559 #ifdef DEBUG
2560  scope->SetScopeName(function_name);
2561 #endif
2562 
2563  if (!is_wrapped && V8_UNLIKELY(!Check(Token::LPAREN))) {
2564  ReportUnexpectedToken(Next());
2565  return nullptr;
2566  }
2567  scope->set_start_position(position());
2568 
2569  // Eager or lazy parse? If is_lazy_top_level_function, we'll parse
2570  // lazily. We'll call SkipFunction, which may decide to
2571  // abort lazy parsing if it suspects that wasn't a good idea. If so (in
2572  // which case the parser is expected to have backtracked), or if we didn't
2573  // try to lazy parse in the first place, we'll have to parse eagerly.
2574  bool did_preparse_successfully =
2575  should_preparse &&
2576  SkipFunction(function_name, kind, function_type, scope, &num_parameters,
2577  &produced_preparsed_scope_data, is_lazy_top_level_function,
2578  &eager_compile_hint);
2579  if (!did_preparse_successfully) {
2580  should_post_parallel_task = false;
2581  ParseFunction(&body, function_name, pos, kind, function_type, scope,
2582  &num_parameters, &function_length, &has_duplicate_parameters,
2583  &expected_property_count, &suspend_count,
2584  arguments_for_wrapped_function);
2585  }
2586 
2587  if (V8_UNLIKELY(FLAG_log_function_events)) {
2588  double ms = timer.Elapsed().InMillisecondsF();
2589  const char* event_name =
2590  should_preparse
2591  ? (is_top_level ? "preparse-no-resolution" : "preparse-resolution")
2592  : "full-parse";
2593  logger_->FunctionEvent(
2594  event_name, script_id(), ms, scope->start_position(),
2595  scope->end_position(),
2596  reinterpret_cast<const char*>(function_name->raw_data()),
2597  function_name->byte_length());
2598  }
2599  if (V8_UNLIKELY(FLAG_runtime_stats) && did_preparse_successfully) {
2600  const RuntimeCallCounterId counters[2] = {
2601  RuntimeCallCounterId::kPreParseBackgroundWithVariableResolution,
2602  RuntimeCallCounterId::kPreParseWithVariableResolution};
2603  if (runtime_call_stats_) {
2604  runtime_call_stats_->CorrectCurrentCounterId(
2605  counters[parsing_on_main_thread_]);
2606  }
2607  }
2608 
2609  // Validate function name. We can do this only after parsing the function,
2610  // since the function can declare itself strict.
2611  language_mode = scope->language_mode();
2612  CheckFunctionName(language_mode, function_name, function_name_validity,
2613  function_name_location);
2614 
2615  if (is_strict(language_mode)) {
2616  CheckStrictOctalLiteral(scope->start_position(), scope->end_position());
2617  }
2618  CheckConflictingVarDeclarations(scope);
2619 
2620  FunctionLiteral::ParameterFlag duplicate_parameters =
2621  has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
2622  : FunctionLiteral::kNoDuplicateParameters;
2623 
2624  // Note that the FunctionLiteral needs to be created in the main Zone again.
2625  FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
2626  function_name, scope, body, expected_property_count, num_parameters,
2627  function_length, duplicate_parameters, function_type, eager_compile_hint,
2628  pos, true, function_literal_id, produced_preparsed_scope_data);
2629  function_literal->set_function_token_position(function_token_pos);
2630  function_literal->set_suspend_count(suspend_count);
2631 
2632  if (should_post_parallel_task) {
2633  // Start a parallel parse / compile task on the compiler dispatcher.
2634  info()->parallel_tasks()->Enqueue(info(), function_name, function_literal);
2635  }
2636 
2637  if (should_infer_name) {
2638  fni_.AddFunction(function_literal);
2639  }
2640  return function_literal;
2641 }
2642 
2643 bool Parser::SkipFunction(
2644  const AstRawString* function_name, FunctionKind kind,
2645  FunctionLiteral::FunctionType function_type,
2646  DeclarationScope* function_scope, int* num_parameters,
2647  ProducedPreParsedScopeData** produced_preparsed_scope_data, bool may_abort,
2648  FunctionLiteral::EagerCompileHint* hint) {
2649  FunctionState function_state(&function_state_, &scope_, function_scope);
2650  function_scope->set_zone(&preparser_zone_);
2651 
2652  DCHECK_NE(kNoSourcePosition, function_scope->start_position());
2653  DCHECK_EQ(kNoSourcePosition, parameters_end_pos_);
2654 
2655  DCHECK_IMPLIES(IsArrowFunction(kind),
2656  scanner()->current_token() == Token::ARROW);
2657 
2658  // FIXME(marja): There are 2 ways to skip functions now. Unify them.
2659  if (consumed_preparsed_scope_data_) {
2660  int end_position;
2661  LanguageMode language_mode;
2662  int num_inner_functions;
2663  bool uses_super_property;
2664  if (stack_overflow()) {
2665  return true;
2666  }
2667  *produced_preparsed_scope_data =
2668  consumed_preparsed_scope_data_->GetDataForSkippableFunction(
2669  main_zone(), function_scope->start_position(), &end_position,
2670  num_parameters, &num_inner_functions, &uses_super_property,
2671  &language_mode);
2672 
2673  function_scope->outer_scope()->SetMustUsePreParsedScopeData();
2674  function_scope->set_is_skipped_function(true);
2675  function_scope->set_end_position(end_position);
2676  scanner()->SeekForward(end_position - 1);
2677  Expect(Token::RBRACE);
2678  SetLanguageMode(function_scope, language_mode);
2679  if (uses_super_property) {
2680  function_scope->RecordSuperPropertyUsage();
2681  }
2682  SkipFunctionLiterals(num_inner_functions);
2683  function_scope->ResetAfterPreparsing(ast_value_factory_, false);
2684  return true;
2685  }
2686 
2687  Scanner::BookmarkScope bookmark(scanner());
2688  bookmark.Set();
2689 
2690  // With no cached data, we partially parse the function, without building an
2691  // AST. This gathers the data needed to build a lazy function.
2692  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.PreParse");
2693 
2694  PreParser::PreParseResult result = reusable_preparser()->PreParseFunction(
2695  function_name, kind, function_type, function_scope, may_abort,
2696  use_counts_, produced_preparsed_scope_data, this->script_id());
2697 
2698  // Return immediately if pre-parser decided to abort parsing.
2699  if (result == PreParser::kPreParseAbort) {
2700  bookmark.Apply();
2701  function_scope->ResetAfterPreparsing(ast_value_factory(), true);
2702  *hint = FunctionLiteral::kShouldEagerCompile;
2703  return false;
2704  }
2705 
2706  if (result == PreParser::kPreParseStackOverflow) {
2707  // Propagate stack overflow.
2708  set_stack_overflow();
2709  } else if (pending_error_handler()->has_error_unidentifiable_by_preparser()) {
2710  // Make sure we don't re-preparse inner functions of the aborted function.
2711  // The error might be in an inner function.
2712  allow_lazy_ = false;
2713  mode_ = PARSE_EAGERLY;
2714  DCHECK(!pending_error_handler()->stack_overflow());
2715  // If we encounter an error that the preparser can not identify we reset to
2716  // the state before preparsing. The caller may then fully parse the function
2717  // to identify the actual error.
2718  bookmark.Apply();
2719  function_scope->ResetAfterPreparsing(ast_value_factory(), true);
2720  pending_error_handler()->clear_unidentifiable_error();
2721  return false;
2722  } else if (pending_error_handler()->has_pending_error()) {
2723  DCHECK(!pending_error_handler()->stack_overflow());
2724  DCHECK(has_error());
2725  } else {
2726  DCHECK(!pending_error_handler()->stack_overflow());
2727  set_allow_eval_cache(reusable_preparser()->allow_eval_cache());
2728 
2729  PreParserLogger* logger = reusable_preparser()->logger();
2730  function_scope->set_end_position(logger->end());
2731  Expect(Token::RBRACE);
2732  total_preparse_skipped_ +=
2733  function_scope->end_position() - function_scope->start_position();
2734  *num_parameters = logger->num_parameters();
2735  SkipFunctionLiterals(logger->num_inner_functions());
2736  function_scope->AnalyzePartially(factory());
2737  }
2738 
2739  return true;
2740 }
2741 
2742 Statement* Parser::BuildAssertIsCoercible(Variable* var,
2743  ObjectLiteral* pattern) {
2744  // if (var === null || var === undefined)
2745  // throw /* type error kNonCoercible) */;
2746  auto source_position = pattern->position();
2747  const AstRawString* property = ast_value_factory()->empty_string();
2748  MessageTemplate msg = MessageTemplate::kNonCoercible;
2749  for (ObjectLiteralProperty* literal_property : *pattern->properties()) {
2750  Expression* key = literal_property->key();
2751  if (key->IsPropertyName()) {
2752  property = key->AsLiteral()->AsRawPropertyName();
2753  msg = MessageTemplate::kNonCoercibleWithProperty;
2754  source_position = key->position();
2755  break;
2756  }
2757  }
2758 
2759  Expression* condition = factory()->NewBinaryOperation(
2760  Token::OR,
2761  factory()->NewCompareOperation(
2762  Token::EQ_STRICT, factory()->NewVariableProxy(var),
2763  factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition),
2764  factory()->NewCompareOperation(
2765  Token::EQ_STRICT, factory()->NewVariableProxy(var),
2766  factory()->NewNullLiteral(kNoSourcePosition), kNoSourcePosition),
2767  kNoSourcePosition);
2768  Expression* throw_type_error =
2769  NewThrowTypeError(msg, property, source_position);
2770  IfStatement* if_statement = factory()->NewIfStatement(
2771  condition,
2772  factory()->NewExpressionStatement(throw_type_error, kNoSourcePosition),
2773  factory()->EmptyStatement(), kNoSourcePosition);
2774  return if_statement;
2775 }
2776 
2778  : public AstTraversalVisitor<InitializerRewriter> {
2779  public:
2780  InitializerRewriter(uintptr_t stack_limit, Expression* root, Parser* parser)
2781  : AstTraversalVisitor(stack_limit, root), parser_(parser) {}
2782 
2783  private:
2784  // This is required so that the overriden Visit* methods can be
2785  // called by the base class (template).
2787 
2788  // Just rewrite destructuring assignments wrapped in RewritableExpressions.
2789  void VisitRewritableExpression(RewritableExpression* to_rewrite) {
2790  if (to_rewrite->is_rewritten()) return;
2791  parser_->RewriteDestructuringAssignment(to_rewrite);
2792  AstTraversalVisitor::VisitRewritableExpression(to_rewrite);
2793  }
2794 
2795  // Code in function literals does not need to be eagerly rewritten, it will be
2796  // rewritten when scheduled.
2797  void VisitFunctionLiteral(FunctionLiteral* expr) {}
2798 
2799  Parser* parser_;
2800 };
2801 
2802 void Parser::RewriteParameterInitializer(Expression* expr) {
2803  if (has_error()) return;
2804  InitializerRewriter rewriter(stack_limit_, expr, this);
2805  rewriter.Run();
2806 }
2807 
2808 Block* Parser::BuildParameterInitializationBlock(
2809  const ParserFormalParameters& parameters) {
2810  DCHECK(!parameters.is_simple);
2811  DCHECK(scope()->is_function_scope());
2812  DCHECK_EQ(scope(), parameters.scope);
2813  Block* init_block = factory()->NewBlock(parameters.num_parameters(), true);
2814  int index = 0;
2815  for (auto parameter : parameters.params) {
2816  DeclarationDescriptor descriptor;
2817  descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
2818  descriptor.scope = scope();
2819  descriptor.mode = VariableMode::kLet;
2820  descriptor.declaration_pos = parameter->pattern->position();
2821  // The position that will be used by the AssignmentExpression
2822  // which copies from the temp parameter to the pattern.
2823  //
2824  // TODO(adamk): Should this be kNoSourcePosition, since
2825  // it's just copying from a temp var to the real param var?
2826  descriptor.initialization_pos = parameter->pattern->position();
2827  Expression* initial_value =
2828  factory()->NewVariableProxy(parameters.scope->parameter(index));
2829  if (parameter->initializer() != nullptr) {
2830  // IS_UNDEFINED($param) ? initializer : $param
2831 
2832  // Ensure initializer is rewritten
2833  RewriteParameterInitializer(parameter->initializer());
2834 
2835  auto condition = factory()->NewCompareOperation(
2836  Token::EQ_STRICT,
2837  factory()->NewVariableProxy(parameters.scope->parameter(index)),
2838  factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition);
2839  initial_value =
2840  factory()->NewConditional(condition, parameter->initializer(),
2841  initial_value, kNoSourcePosition);
2842  descriptor.initialization_pos = parameter->initializer()->position();
2843  }
2844 
2845  Scope* param_scope = scope();
2846  Block* param_block = init_block;
2847  if (!parameter->is_simple() &&
2848  scope()->AsDeclarationScope()->calls_sloppy_eval()) {
2849  param_scope = NewVarblockScope();
2850  param_scope->set_start_position(descriptor.initialization_pos);
2851  param_scope->set_end_position(parameter->initializer_end_position);
2852  param_scope->RecordEvalCall();
2853  param_block = factory()->NewBlock(8, true);
2854  param_block->set_scope(param_scope);
2855  // Pass the appropriate scope in so that PatternRewriter can appropriately
2856  // rewrite inner initializers of the pattern to param_scope
2857  descriptor.scope = param_scope;
2858  // Rewrite the outer initializer to point to param_scope
2859  ReparentExpressionScope(stack_limit(), initial_value, param_scope);
2860  }
2861 
2862  BlockState block_state(&scope_, param_scope);
2863  DeclarationParsingResult::Declaration decl(
2864  parameter->pattern, parameter->initializer_end_position, initial_value);
2865  DeclareAndInitializeVariables(param_block, &descriptor, &decl, nullptr);
2866 
2867  if (param_block != init_block) {
2868  param_scope = param_scope->FinalizeBlockScope();
2869  if (param_scope != nullptr) {
2870  CheckConflictingVarDeclarations(param_scope);
2871  }
2872  init_block->statements()->Add(param_block, zone());
2873  }
2874  ++index;
2875  }
2876  return init_block;
2877 }
2878 
2879 Scope* Parser::NewHiddenCatchScope() {
2880  Scope* catch_scope = NewScopeWithParent(scope(), CATCH_SCOPE);
2881  catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(),
2882  VariableMode::kVar);
2883  catch_scope->set_is_hidden();
2884  return catch_scope;
2885 }
2886 
2887 Block* Parser::BuildRejectPromiseOnException(Block* inner_block) {
2888  // try {
2889  // <inner_block>
2890  // } catch (.catch) {
2891  // return %_AsyncFunctionReject(.generator_object, .catch, can_suspend);
2892  // }
2893  Block* result = factory()->NewBlock(1, true);
2894 
2895  // catch (.catch) {
2896  // return %_AsyncFunctionReject(.generator_object, .catch, can_suspend)
2897  // }
2898  Scope* catch_scope = NewHiddenCatchScope();
2899 
2900  Expression* reject_promise;
2901  {
2902  ScopedPtrList<Expression> args(pointer_buffer());
2903  args.Add(factory()->NewVariableProxy(
2904  function_state_->scope()->generator_object_var()));
2905  args.Add(factory()->NewVariableProxy(catch_scope->catch_variable()));
2906  args.Add(factory()->NewBooleanLiteral(function_state_->CanSuspend(),
2907  kNoSourcePosition));
2908  reject_promise = factory()->NewCallRuntime(
2909  Runtime::kInlineAsyncFunctionReject, args, kNoSourcePosition);
2910  }
2911  Block* catch_block = IgnoreCompletion(
2912  factory()->NewReturnStatement(reject_promise, kNoSourcePosition));
2913 
2914  TryStatement* try_catch_statement =
2915  factory()->NewTryCatchStatementForAsyncAwait(
2916  inner_block, catch_scope, catch_block, kNoSourcePosition);
2917  result->statements()->Add(try_catch_statement, zone());
2918  return result;
2919 }
2920 
2921 Expression* Parser::BuildInitialYield(int pos, FunctionKind kind) {
2922  Expression* yield_result = factory()->NewVariableProxy(
2923  function_state_->scope()->generator_object_var());
2924  // The position of the yield is important for reporting the exception
2925  // caused by calling the .throw method on a generator suspended at the
2926  // initial yield (i.e. right after generator instantiation).
2927  function_state_->AddSuspend();
2928  return factory()->NewYield(yield_result, scope()->start_position(),
2929  Suspend::kOnExceptionThrow);
2930 }
2931 
2932 void Parser::ParseFunction(
2933  ScopedPtrList<Statement>* body, const AstRawString* function_name, int pos,
2934  FunctionKind kind, FunctionLiteral::FunctionType function_type,
2935  DeclarationScope* function_scope, int* num_parameters, int* function_length,
2936  bool* has_duplicate_parameters, int* expected_property_count,
2937  int* suspend_count,
2938  ZonePtrList<const AstRawString>* arguments_for_wrapped_function) {
2939  ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY);
2940 
2941  FunctionState function_state(&function_state_, &scope_, function_scope);
2942 
2943  bool is_wrapped = function_type == FunctionLiteral::kWrapped;
2944 
2945  ExpressionClassifier formals_classifier(this);
2946 
2947  int expected_parameters_end_pos = parameters_end_pos_;
2948  if (expected_parameters_end_pos != kNoSourcePosition) {
2949  // This is the first function encountered in a CreateDynamicFunction eval.
2950  parameters_end_pos_ = kNoSourcePosition;
2951  // The function name should have been ignored, giving us the empty string
2952  // here.
2953  DCHECK_EQ(function_name, ast_value_factory()->empty_string());
2954  }
2955 
2956  ParserFormalParameters formals(function_scope);
2957 
2958  if (is_wrapped) {
2959  // For a function implicitly wrapped in function header and footer, the
2960  // function arguments are provided separately to the source, and are
2961  // declared directly here.
2962  int arguments_length = arguments_for_wrapped_function->length();
2963  for (int i = 0; i < arguments_length; i++) {
2964  const bool is_rest = false;
2965  Expression* argument = ExpressionFromIdentifier(
2966  arguments_for_wrapped_function->at(i), kNoSourcePosition);
2967  AddFormalParameter(&formals, argument, NullExpression(),
2968  kNoSourcePosition, is_rest);
2969  }
2970  DCHECK_EQ(arguments_length, formals.num_parameters());
2971  DeclareFormalParameters(&formals);
2972  } else {
2973  // For a regular function, the function arguments are parsed from source.
2974  DCHECK_NULL(arguments_for_wrapped_function);
2975  ParseFormalParameterList(&formals);
2976  if (expected_parameters_end_pos != kNoSourcePosition) {
2977  // Check for '(' or ')' shenanigans in the parameter string for dynamic
2978  // functions.
2979  int position = peek_position();
2980  if (position < expected_parameters_end_pos) {
2981  ReportMessageAt(Scanner::Location(position, position + 1),
2982  MessageTemplate::kArgStringTerminatesParametersEarly);
2983  return;
2984  } else if (position > expected_parameters_end_pos) {
2985  ReportMessageAt(Scanner::Location(expected_parameters_end_pos - 2,
2986  expected_parameters_end_pos),
2987  MessageTemplate::kUnexpectedEndOfArgString);
2988  return;
2989  }
2990  }
2991  Expect(Token::RPAREN);
2992  int formals_end_position = scanner()->location().end_pos;
2993 
2994  CheckArityRestrictions(formals.arity, kind, formals.has_rest,
2995  function_scope->start_position(),
2996  formals_end_position);
2997  Expect(Token::LBRACE);
2998  }
2999  *num_parameters = formals.num_parameters();
3000  *function_length = formals.function_length;
3001 
3002  AcceptINScope scope(this, true);
3003  ParseFunctionBody(body, function_name, pos, formals, kind, function_type,
3004  FunctionBodyType::kBlock);
3005 
3006  RewriteDestructuringAssignments();
3007 
3008  *has_duplicate_parameters = formals.has_duplicate();
3009 
3010  *expected_property_count = function_state.expected_property_count();
3011  *suspend_count = function_state.suspend_count();
3012 }
3013 
3014 void Parser::DeclareClassVariable(const AstRawString* name,
3015  ClassInfo* class_info, int class_token_pos) {
3016 #ifdef DEBUG
3017  scope()->SetScopeName(name);
3018 #endif
3019 
3020  if (name != nullptr) {
3021  VariableProxy* proxy = factory()->NewVariableProxy(name, NORMAL_VARIABLE);
3022  Declaration* declaration =
3023  factory()->NewVariableDeclaration(proxy, class_token_pos);
3024  class_info->variable = Declare(
3025  declaration, DeclarationDescriptor::NORMAL, VariableMode::kConst,
3026  Variable::DefaultInitializationFlag(VariableMode::kConst));
3027  }
3028 }
3029 
3030 // TODO(gsathya): Ideally, this should just bypass scope analysis and
3031 // allocate a slot directly on the context. We should just store this
3032 // index in the AST, instead of storing the variable.
3033 Variable* Parser::CreateSyntheticContextVariable(const AstRawString* name) {
3034  VariableProxy* proxy = factory()->NewVariableProxy(name, NORMAL_VARIABLE);
3035  Declaration* declaration =
3036  factory()->NewVariableDeclaration(proxy, kNoSourcePosition);
3037  Variable* var =
3038  Declare(declaration, DeclarationDescriptor::NORMAL, VariableMode::kConst,
3039  Variable::DefaultInitializationFlag(VariableMode::kConst));
3040  var->ForceContextAllocation();
3041  return var;
3042 }
3043 
3044 // This method declares a property of the given class. It updates the
3045 // following fields of class_info, as appropriate:
3046 // - constructor
3047 // - properties
3048 void Parser::DeclareClassProperty(const AstRawString* class_name,
3049  ClassLiteralProperty* property,
3050  const AstRawString* property_name,
3051  ClassLiteralProperty::Kind kind,
3052  bool is_static, bool is_constructor,
3053  bool is_computed_name, bool is_private,
3054  ClassInfo* class_info) {
3055  if (is_constructor) {
3056  DCHECK(!class_info->constructor);
3057  class_info->constructor = property->value()->AsFunctionLiteral();
3058  DCHECK_NOT_NULL(class_info->constructor);
3059  class_info->constructor->set_raw_name(
3060  class_name != nullptr ? ast_value_factory()->NewConsString(class_name)
3061  : nullptr);
3062  return;
3063  }
3064 
3065  if (kind != ClassLiteralProperty::FIELD) {
3066  class_info->properties->Add(property, zone());
3067  return;
3068  }
3069 
3070  DCHECK(allow_harmony_public_fields() || allow_harmony_private_fields());
3071 
3072  if (is_static) {
3073  DCHECK(allow_harmony_static_fields());
3074  DCHECK_EQ(kind, ClassLiteralProperty::FIELD);
3075  DCHECK(!is_private);
3076  class_info->static_fields->Add(property, zone());
3077  } else {
3078  class_info->instance_fields->Add(property, zone());
3079  }
3080 
3081  if (is_computed_name) {
3082  DCHECK_EQ(kind, ClassLiteralProperty::FIELD);
3083  DCHECK(!is_private);
3084  // We create a synthetic variable name here so that scope
3085  // analysis doesn't dedupe the vars.
3086  Variable* computed_name_var =
3087  CreateSyntheticContextVariable(ClassFieldVariableName(
3088  ast_value_factory(), class_info->computed_field_count));
3089  property->set_computed_name_var(computed_name_var);
3090  class_info->properties->Add(property, zone());
3091  }
3092 
3093  if (kind == ClassLiteralProperty::FIELD && is_private) {
3094  Variable* private_name_var = CreateSyntheticContextVariable(property_name);
3095  property->set_private_name_var(private_name_var);
3096  class_info->properties->Add(property, zone());
3097  }
3098 }
3099 
3100 FunctionLiteral* Parser::CreateInitializerFunction(
3101  const char* name, DeclarationScope* scope,
3102  ZonePtrList<ClassLiteral::Property>* fields) {
3103  DCHECK_EQ(scope->function_kind(),
3104  FunctionKind::kClassMembersInitializerFunction);
3105  // function() { .. class fields initializer .. }
3106  ScopedPtrList<Statement> statements(pointer_buffer());
3107  InitializeClassMembersStatement* static_fields =
3108  factory()->NewInitializeClassMembersStatement(fields, kNoSourcePosition);
3109  statements.Add(static_fields);
3110  return factory()->NewFunctionLiteral(
3111  ast_value_factory()->GetOneByteString(name), scope, statements, 0, 0, 0,
3112  FunctionLiteral::kNoDuplicateParameters,
3113  FunctionLiteral::kAnonymousExpression,
3114  FunctionLiteral::kShouldEagerCompile, scope->start_position(), false,
3115  GetNextFunctionLiteralId());
3116 }
3117 
3118 // This method generates a ClassLiteral AST node.
3119 // It uses the following fields of class_info:
3120 // - constructor (if missing, it updates it with a default constructor)
3121 // - proxy
3122 // - extends
3123 // - properties
3124 // - has_name_static_property
3125 // - has_static_computed_names
3126 Expression* Parser::RewriteClassLiteral(Scope* block_scope,
3127  const AstRawString* name,
3128  ClassInfo* class_info, int pos,
3129  int end_pos) {
3130  DCHECK_NOT_NULL(block_scope);
3131  DCHECK_EQ(block_scope->scope_type(), BLOCK_SCOPE);
3132  DCHECK_EQ(block_scope->language_mode(), LanguageMode::kStrict);
3133 
3134  bool has_extends = class_info->extends != nullptr;
3135  bool has_default_constructor = class_info->constructor == nullptr;
3136  if (has_default_constructor) {
3137  class_info->constructor =
3138  DefaultConstructor(name, has_extends, pos, end_pos);
3139  }
3140 
3141  if (name != nullptr) {
3142  DCHECK_NOT_NULL(class_info->variable);
3143  class_info->variable->set_initializer_position(end_pos);
3144  }
3145 
3146  FunctionLiteral* static_fields_initializer = nullptr;
3147  if (class_info->has_static_class_fields) {
3148  static_fields_initializer = CreateInitializerFunction(
3149  "<static_fields_initializer>", class_info->static_fields_scope,
3150  class_info->static_fields);
3151  }
3152 
3153  FunctionLiteral* instance_members_initializer_function = nullptr;
3154  if (class_info->has_instance_members) {
3155  instance_members_initializer_function = CreateInitializerFunction(
3156  "<instance_members_initializer>", class_info->instance_members_scope,
3157  class_info->instance_fields);
3158  class_info->constructor->set_requires_instance_members_initializer(true);
3159  }
3160 
3161  ClassLiteral* class_literal = factory()->NewClassLiteral(
3162  block_scope, class_info->variable, class_info->extends,
3163  class_info->constructor, class_info->properties,
3164  static_fields_initializer, instance_members_initializer_function, pos,
3165  end_pos, class_info->has_name_static_property,
3166  class_info->has_static_computed_names, class_info->is_anonymous);
3167 
3168  AddFunctionForNameInference(class_info->constructor);
3169  return class_literal;
3170 }
3171 
3172 void Parser::CheckConflictingVarDeclarations(Scope* scope) {
3173  if (has_error()) return;
3174  Declaration* decl = scope->CheckConflictingVarDeclarations();
3175  if (decl != nullptr) {
3176  // In ES6, conflicting variable bindings are early errors.
3177  const AstRawString* name = decl->proxy()->raw_name();
3178  int position = decl->proxy()->position();
3179  Scanner::Location location =
3180  position == kNoSourcePosition
3181  ? Scanner::Location::invalid()
3182  : Scanner::Location(position, position + 1);
3183  ReportMessageAt(location, MessageTemplate::kVarRedeclaration, name);
3184  }
3185 }
3186 
3187 bool Parser::IsPropertyWithPrivateFieldKey(Expression* expression) {
3188  if (!expression->IsProperty()) return false;
3189  Property* property = expression->AsProperty();
3190 
3191  if (!property->key()->IsVariableProxy()) return false;
3192  VariableProxy* key = property->key()->AsVariableProxy();
3193 
3194  return key->is_private_name();
3195 }
3196 
3197 void Parser::InsertShadowingVarBindingInitializers(Block* inner_block) {
3198  // For each var-binding that shadows a parameter, insert an assignment
3199  // initializing the variable with the parameter.
3200  Scope* inner_scope = inner_block->scope();
3201  DCHECK(inner_scope->is_declaration_scope());
3202  Scope* function_scope = inner_scope->outer_scope();
3203  DCHECK(function_scope->is_function_scope());
3204  BlockState block_state(&scope_, inner_scope);
3205  for (Declaration* decl : *inner_scope->declarations()) {
3206  if (decl->proxy()->var()->mode() != VariableMode::kVar ||
3207  !decl->IsVariableDeclaration()) {
3208  continue;
3209  }
3210  const AstRawString* name = decl->proxy()->raw_name();
3211  Variable* parameter = function_scope->LookupLocal(name);
3212  if (parameter == nullptr) continue;
3213  VariableProxy* to = NewUnresolved(name);
3214  VariableProxy* from = factory()->NewVariableProxy(parameter);
3215  Expression* assignment =
3216  factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition);
3217  Statement* statement =
3218  factory()->NewExpressionStatement(assignment, kNoSourcePosition);
3219  inner_block->statements()->InsertAt(0, statement, zone());
3220  }
3221 }
3222 
3223 void Parser::InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) {
3224  // For the outermost eval scope, we cannot hoist during parsing: let
3225  // declarations in the surrounding scope may prevent hoisting, but the
3226  // information is unaccessible during parsing. In this case, we hoist later in
3227  // DeclarationScope::Analyze.
3228  if (scope->is_eval_scope() && scope->outer_scope() == original_scope_) {
3229  return;
3230  }
3231  scope->HoistSloppyBlockFunctions(factory());
3232 }
3233 
3234 // ----------------------------------------------------------------------------
3235 // Parser support
3236 
3237 bool Parser::TargetStackContainsLabel(const AstRawString* label) {
3238  for (ParserTarget* t = target_stack_; t != nullptr; t = t->previous()) {
3239  if (ContainsLabel(t->statement()->labels(), label)) return true;
3240  }
3241  return false;
3242 }
3243 
3244 BreakableStatement* Parser::LookupBreakTarget(const AstRawString* label) {
3245  bool anonymous = label == nullptr;
3246  for (ParserTarget* t = target_stack_; t != nullptr; t = t->previous()) {
3247  BreakableStatement* stat = t->statement();
3248  if ((anonymous && stat->is_target_for_anonymous()) ||
3249  (!anonymous && ContainsLabel(stat->labels(), label))) {
3250  return stat;
3251  }
3252  }
3253  return nullptr;
3254 }
3255 
3256 IterationStatement* Parser::LookupContinueTarget(const AstRawString* label) {
3257  bool anonymous = label == nullptr;
3258  for (ParserTarget* t = target_stack_; t != nullptr; t = t->previous()) {
3259  IterationStatement* stat = t->statement()->AsIterationStatement();
3260  if (stat == nullptr) continue;
3261 
3262  DCHECK(stat->is_target_for_anonymous());
3263  if (anonymous || ContainsLabel(stat->own_labels(), label)) {
3264  return stat;
3265  }
3266  if (ContainsLabel(stat->labels(), label)) break;
3267  }
3268  return nullptr;
3269 }
3270 
3271 void Parser::HandleSourceURLComments(Isolate* isolate, Handle<Script> script) {
3272  Handle<String> source_url = scanner_.SourceUrl(isolate);
3273  if (!source_url.is_null()) {
3274  script->set_source_url(*source_url);
3275  }
3276  Handle<String> source_mapping_url = scanner_.SourceMappingUrl(isolate);
3277  if (!source_mapping_url.is_null()) {
3278  script->set_source_mapping_url(*source_mapping_url);
3279  }
3280 }
3281 
3282 void Parser::UpdateStatistics(Isolate* isolate, Handle<Script> script) {
3283  // Move statistics to Isolate.
3284  for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
3285  ++feature) {
3286  if (use_counts_[feature] > 0) {
3287  isolate->CountUsage(v8::Isolate::UseCounterFeature(feature));
3288  }
3289  }
3290  if (scanner_.FoundHtmlComment()) {
3291  isolate->CountUsage(v8::Isolate::kHtmlComment);
3292  if (script->line_offset() == 0 && script->column_offset() == 0) {
3293  isolate->CountUsage(v8::Isolate::kHtmlCommentInExternalScript);
3294  }
3295  }
3296  isolate->counters()->total_preparse_skipped()->Increment(
3297  total_preparse_skipped_);
3298 }
3299 
3300 void Parser::ParseOnBackground(ParseInfo* info) {
3301  RuntimeCallTimerScope runtimeTimer(
3302  runtime_call_stats_, RuntimeCallCounterId::kParseBackgroundProgram);
3303  parsing_on_main_thread_ = false;
3304  set_script_id(info->script_id());
3305 
3306  DCHECK_NULL(info->literal());
3307  FunctionLiteral* result = nullptr;
3308 
3309  scanner_.Initialize();
3310  DCHECK(info->maybe_outer_scope_info().is_null());
3311 
3312  DCHECK(original_scope_);
3313 
3314  // When streaming, we don't know the length of the source until we have parsed
3315  // it. The raw data can be UTF-8, so we wouldn't know the source length until
3316  // we have decoded it anyway even if we knew the raw data length (which we
3317  // don't). We work around this by storing all the scopes which need their end
3318  // position set at the end of the script (the top scope and possible eval
3319  // scopes) and set their end position after we know the script length.
3320  if (info->is_toplevel()) {
3321  result = DoParseProgram(/* isolate = */ nullptr, info);
3322  } else {
3323  result =
3324  DoParseFunction(/* isolate = */ nullptr, info, info->function_name());
3325  }
3326  MaybeResetCharacterStream(info, result);
3327 
3328  info->set_literal(result);
3329 
3330  // We cannot internalize on a background thread; a foreground task will take
3331  // care of calling AstValueFactory::Internalize just before compilation.
3332 }
3333 
3334 Parser::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) {
3335  return new (zone()) TemplateLiteral(zone(), pos);
3336 }
3337 
3338 void Parser::AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
3339  bool tail) {
3340  int end = scanner()->location().end_pos - (tail ? 1 : 2);
3341  const AstRawString* raw = scanner()->CurrentRawSymbol(ast_value_factory());
3342  if (should_cook) {
3343  const AstRawString* cooked = scanner()->CurrentSymbol(ast_value_factory());
3344  (*state)->AddTemplateSpan(cooked, raw, end, zone());
3345  } else {
3346  (*state)->AddTemplateSpan(nullptr, raw, end, zone());
3347  }
3348 }
3349 
3350 
3351 void Parser::AddTemplateExpression(TemplateLiteralState* state,
3352  Expression* expression) {
3353  (*state)->AddExpression(expression, zone());
3354 }
3355 
3356 
3357 Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state, int start,
3358  Expression* tag) {
3359  TemplateLiteral* lit = *state;
3360  int pos = lit->position();
3361  const ZonePtrList<const AstRawString>* cooked_strings = lit->cooked();
3362  const ZonePtrList<const AstRawString>* raw_strings = lit->raw();
3363  const ZonePtrList<Expression>* expressions = lit->expressions();
3364  DCHECK_EQ(cooked_strings->length(), raw_strings->length());
3365  DCHECK_EQ(cooked_strings->length(), expressions->length() + 1);
3366 
3367  if (!tag) {
3368  if (cooked_strings->length() == 1) {
3369  return factory()->NewStringLiteral(cooked_strings->first(), pos);
3370  }
3371  return factory()->NewTemplateLiteral(cooked_strings, expressions, pos);
3372  } else {
3373  // GetTemplateObject
3374  Expression* template_object =
3375  factory()->NewGetTemplateObject(cooked_strings, raw_strings, pos);
3376 
3377  // Call TagFn
3378  ScopedPtrList<Expression> call_args(pointer_buffer());
3379  call_args.Add(template_object);
3380  call_args.AddAll(*expressions);
3381  return factory()->NewTaggedTemplate(tag, call_args, pos);
3382  }
3383 }
3384 
3385 namespace {
3386 
3387 bool OnlyLastArgIsSpread(const ScopedPtrList<Expression>& args) {
3388  for (int i = 0; i < args.length() - 1; i++) {
3389  if (args.at(i)->IsSpread()) {
3390  return false;
3391  }
3392  }
3393  return args.at(args.length() - 1)->IsSpread();
3394 }
3395 
3396 } // namespace
3397 
3398 ArrayLiteral* Parser::ArrayLiteralFromListWithSpread(
3399  const ScopedPtrList<Expression>& list) {
3400  // If there's only a single spread argument, a fast path using CallWithSpread
3401  // is taken.
3402  DCHECK_LT(1, list.length());
3403 
3404  // The arguments of the spread call become a single ArrayLiteral.
3405  int first_spread = 0;
3406  for (; first_spread < list.length() && !list.at(first_spread)->IsSpread();
3407  ++first_spread) {
3408  }
3409 
3410  DCHECK_LT(first_spread, list.length());
3411  return factory()->NewArrayLiteral(list, first_spread, kNoSourcePosition);
3412 }
3413 
3414 Expression* Parser::SpreadCall(Expression* function,
3415  const ScopedPtrList<Expression>& args_list,
3416  int pos, Call::PossiblyEval is_possibly_eval) {
3417  // Handle this case in BytecodeGenerator.
3418  if (OnlyLastArgIsSpread(args_list) || function->IsSuperCallReference()) {
3419  return factory()->NewCall(function, args_list, pos);
3420  }
3421 
3422  ScopedPtrList<Expression> args(pointer_buffer());
3423  if (function->IsProperty()) {
3424  // Method calls
3425  if (function->AsProperty()->IsSuperAccess()) {
3426  Expression* home = ThisExpression(kNoSourcePosition);
3427  args.Add(function);
3428  args.Add(home);
3429  } else {
3430  Variable* temp = NewTemporary(ast_value_factory()->empty_string());
3431  VariableProxy* obj = factory()->NewVariableProxy(temp);
3432  Assignment* assign_obj = factory()->NewAssignment(
3433  Token::ASSIGN, obj, function->AsProperty()->obj(), kNoSourcePosition);
3434  function = factory()->NewProperty(
3435  assign_obj, function->AsProperty()->key(), kNoSourcePosition);
3436  args.Add(function);
3437  obj = factory()->NewVariableProxy(temp);
3438  args.Add(obj);
3439  }
3440  } else {
3441  // Non-method calls
3442  args.Add(function);
3443  args.Add(factory()->NewUndefinedLiteral(kNoSourcePosition));
3444  }
3445  args.Add(ArrayLiteralFromListWithSpread(args_list));
3446  return factory()->NewCallRuntime(Context::REFLECT_APPLY_INDEX, args, pos);
3447 }
3448 
3449 Expression* Parser::SpreadCallNew(Expression* function,
3450  const ScopedPtrList<Expression>& args_list,
3451  int pos) {
3452  if (OnlyLastArgIsSpread(args_list)) {
3453  // Handle in BytecodeGenerator.
3454  return factory()->NewCallNew(function, args_list, pos);
3455  }
3456  ScopedPtrList<Expression> args(pointer_buffer());
3457  args.Add(function);
3458  args.Add(ArrayLiteralFromListWithSpread(args_list));
3459 
3460  return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos);
3461 }
3462 
3463 
3464 void Parser::SetLanguageMode(Scope* scope, LanguageMode mode) {
3465  v8::Isolate::UseCounterFeature feature;
3466  if (is_sloppy(mode))
3467  feature = v8::Isolate::kSloppyMode;
3468  else if (is_strict(mode))
3469  feature = v8::Isolate::kStrictMode;
3470  else
3471  UNREACHABLE();
3472  ++use_counts_[feature];
3473  scope->SetLanguageMode(mode);
3474 }
3475 
3476 void Parser::SetAsmModule() {
3477  // Store the usage count; The actual use counter on the isolate is
3478  // incremented after parsing is done.
3479  ++use_counts_[v8::Isolate::kUseAsm];
3480  DCHECK(scope()->is_declaration_scope());
3481  scope()->AsDeclarationScope()->set_asm_module();
3482 }
3483 
3484 Expression* Parser::ExpressionListToExpression(
3485  const ScopedPtrList<Expression>& args) {
3486  Expression* expr = args.at(0);
3487  if (args.length() == 1) return expr;
3488  if (args.length() == 2) {
3489  return factory()->NewBinaryOperation(Token::COMMA, expr, args.at(1),
3490  args.at(1)->position());
3491  }
3492  NaryOperation* result =
3493  factory()->NewNaryOperation(Token::COMMA, expr, args.length() - 1);
3494  for (int i = 1; i < args.length(); i++) {
3495  result->AddSubsequent(args.at(i), args.at(i)->position());
3496  }
3497  return result;
3498 }
3499 
3500 // This method completes the desugaring of the body of async_function.
3501 void Parser::RewriteAsyncFunctionBody(ScopedPtrList<Statement>* body,
3502  Block* block, Expression* return_value) {
3503  // function async_function() {
3504  // .generator_object = %_AsyncFunctionEnter();
3505  // BuildRejectPromiseOnException({
3506  // ... block ...
3507  // return %_AsyncFunctionResolve(.generator_object, expr);
3508  // })
3509  // }
3510 
3511  block->statements()->Add(factory()->NewAsyncReturnStatement(
3512  return_value, return_value->position()),
3513  zone());
3514  block = BuildRejectPromiseOnException(block);
3515  body->Add(block);
3516 }
3517 
3518 void Parser::RewriteDestructuringAssignments() {
3519  const auto& assignments =
3520  function_state_->destructuring_assignments_to_rewrite();
3521  auto it = assignments.rbegin();
3522  for (; it != assignments.rend(); ++it) {
3523  // Rewrite list in reverse, so that nested assignment patterns are rewritten
3524  // correctly.
3525  RewritableExpression* to_rewrite = *it;
3526  DCHECK_NOT_NULL(to_rewrite);
3527  if (!to_rewrite->is_rewritten()) {
3528  // Since this function is called at the end of parsing the program,
3529  // pair.scope may already have been removed by FinalizeBlockScope in the
3530  // meantime.
3531  Scope* scope = to_rewrite->scope()->GetUnremovedScope();
3532  // Scope at the time of the rewriting and the original parsing
3533  // should be in the same function.
3534  DCHECK(scope->GetClosureScope() == scope_->GetClosureScope());
3535  BlockState block_state(&scope_, scope);
3536  RewriteDestructuringAssignment(to_rewrite);
3537  }
3538  }
3539 }
3540 
3541 void Parser::QueueDestructuringAssignmentForRewriting(
3542  RewritableExpression* expr) {
3543  function_state_->AddDestructuringAssignment(expr);
3544 }
3545 
3546 void Parser::SetFunctionNameFromPropertyName(LiteralProperty* property,
3547  const AstRawString* name,
3548  const AstRawString* prefix) {
3549  if (has_error()) return;
3550  // Ensure that the function we are going to create has shared name iff
3551  // we are not going to set it later.
3552  if (property->NeedsSetFunctionName()) {
3553  name = nullptr;
3554  prefix = nullptr;
3555  } else {
3556  // If the property value is an anonymous function or an anonymous class or
3557  // a concise method or an accessor function which doesn't require the name
3558  // to be set then the shared name must be provided.
3559  DCHECK_IMPLIES(property->value()->IsAnonymousFunctionDefinition() ||
3560  property->value()->IsConciseMethodDefinition() ||
3561  property->value()->IsAccessorFunctionDefinition(),
3562  name != nullptr);
3563  }
3564 
3565  Expression* value = property->value();
3566  SetFunctionName(value, name, prefix);
3567 }
3568 
3569 void Parser::SetFunctionNameFromPropertyName(ObjectLiteralProperty* property,
3570  const AstRawString* name,
3571  const AstRawString* prefix) {
3572  // Ignore "__proto__" as a name when it's being used to set the [[Prototype]]
3573  // of an object literal.
3574  // See ES #sec-__proto__-property-names-in-object-initializers.
3575  if (property->IsPrototype() || has_error()) return;
3576 
3577  DCHECK(!property->value()->IsAnonymousFunctionDefinition() ||
3578  property->kind() == ObjectLiteralProperty::COMPUTED);
3579 
3580  SetFunctionNameFromPropertyName(static_cast<LiteralProperty*>(property), name,
3581  prefix);
3582 }
3583 
3584 void Parser::SetFunctionNameFromIdentifierRef(Expression* value,
3585  Expression* identifier) {
3586  if (!identifier->IsVariableProxy()) return;
3587  SetFunctionName(value, identifier->AsVariableProxy()->raw_name());
3588 }
3589 
3590 void Parser::SetFunctionName(Expression* value, const AstRawString* name,
3591  const AstRawString* prefix) {
3592  if (!value->IsAnonymousFunctionDefinition() &&
3593  !value->IsConciseMethodDefinition() &&
3594  !value->IsAccessorFunctionDefinition()) {
3595  return;
3596  }
3597  auto function = value->AsFunctionLiteral();
3598  if (value->IsClassLiteral()) {
3599  function = value->AsClassLiteral()->constructor();
3600  }
3601  if (function != nullptr) {
3602  AstConsString* cons_name = nullptr;
3603  if (name != nullptr) {
3604  if (prefix != nullptr) {
3605  cons_name = ast_value_factory()->NewConsString(prefix, name);
3606  } else {
3607  cons_name = ast_value_factory()->NewConsString(name);
3608  }
3609  } else {
3610  DCHECK_NULL(prefix);
3611  }
3612  function->set_raw_name(cons_name);
3613  }
3614 }
3615 
3616 Statement* Parser::CheckCallable(Variable* var, Expression* error, int pos) {
3617  const int nopos = kNoSourcePosition;
3618  Statement* validate_var;
3619  {
3620  Expression* type_of = factory()->NewUnaryOperation(
3621  Token::TYPEOF, factory()->NewVariableProxy(var), nopos);
3622  Expression* function_literal = factory()->NewStringLiteral(
3623  ast_value_factory()->function_string(), nopos);
3624  Expression* condition = factory()->NewCompareOperation(
3625  Token::EQ_STRICT, type_of, function_literal, nopos);
3626 
3627  Statement* throw_call = factory()->NewExpressionStatement(error, pos);
3628 
3629  validate_var = factory()->NewIfStatement(
3630  condition, factory()->EmptyStatement(), throw_call, nopos);
3631  }
3632  return validate_var;
3633 }
3634 
3635 void Parser::BuildIteratorClose(ZonePtrList<Statement>* statements,
3636  Variable* iterator, Variable* input,
3637  Variable* var_output, IteratorType type) {
3638  //
3639  // This function adds four statements to [statements], corresponding to the
3640  // following code:
3641  //
3642  // let iteratorReturn = iterator.return;
3643  // if (IS_NULL_OR_UNDEFINED(iteratorReturn) {
3644  // return {value: input, done: true};
3645  // }
3646  // output = %_Call(iteratorReturn, iterator, input);
3647  // if (!IS_RECEIVER(output)) %ThrowIterResultNotAnObject(output);
3648  //
3649 
3650  const int nopos = kNoSourcePosition;
3651 
3652  // let iteratorReturn = iterator.return;
3653  Variable* var_return = var_output; // Reusing the output variable.
3654  Statement* get_return;
3655  {
3656  Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
3657  Expression* literal = factory()->NewStringLiteral(
3658  ast_value_factory()->return_string(), nopos);
3659  Expression* property =
3660  factory()->NewProperty(iterator_proxy, literal, nopos);
3661  Expression* return_proxy = factory()->NewVariableProxy(var_return);
3662  Expression* assignment =
3663  factory()->NewAssignment(Token::ASSIGN, return_proxy, property, nopos);
3664  get_return = factory()->NewExpressionStatement(assignment, nopos);
3665  }
3666 
3667  // if (IS_NULL_OR_UNDEFINED(iteratorReturn) {
3668  // return {value: input, done: true};
3669  // }
3670  Statement* check_return;
3671  {
3672  Expression* condition = factory()->NewCompareOperation(
3673  Token::EQ, factory()->NewVariableProxy(var_return),
3674  factory()->NewNullLiteral(nopos), nopos);
3675 
3676  Expression* value = factory()->NewVariableProxy(input);
3677 
3678  Statement* return_input = BuildReturnStatement(value, nopos);
3679 
3680  check_return = factory()->NewIfStatement(
3681  condition, return_input, factory()->EmptyStatement(), nopos);
3682  }
3683 
3684  // output = %_Call(iteratorReturn, iterator, input);
3685  Statement* call_return;
3686  {
3687  ScopedPtrList<Expression> args(pointer_buffer());
3688  args.Add(factory()->NewVariableProxy(var_return));
3689  args.Add(factory()->NewVariableProxy(iterator));
3690  args.Add(factory()->NewVariableProxy(input));
3691 
3692  Expression* call =
3693  factory()->NewCallRuntime(Runtime::kInlineCall, args, nopos);
3694  if (type == IteratorType::kAsync) {
3695  function_state_->AddSuspend();
3696  call = factory()->NewAwait(call, nopos);
3697  }
3698  Expression* output_proxy = factory()->NewVariableProxy(var_output);
3699  Expression* assignment =
3700  factory()->NewAssignment(Token::ASSIGN, output_proxy, call, nopos);
3701  call_return = factory()->NewExpressionStatement(assignment, nopos);
3702  }
3703 
3704  // if (!IS_RECEIVER(output)) %ThrowIteratorResultNotAnObject(output);
3705  Statement* validate_output;
3706  {
3707  Expression* is_receiver_call;
3708  {
3709  ScopedPtrList<Expression> args(pointer_buffer());
3710  args.Add(factory()->NewVariableProxy(var_output));
3711  is_receiver_call =
3712  factory()->NewCallRuntime(Runtime::kInlineIsJSReceiver, args, nopos);
3713  }
3714 
3715  Statement* throw_call;
3716  {
3717  ScopedPtrList<Expression> args(pointer_buffer());
3718  args.Add(factory()->NewVariableProxy(var_output));
3719  Expression* call = factory()->NewCallRuntime(
3720  Runtime::kThrowIteratorResultNotAnObject, args, nopos);
3721  throw_call = factory()->NewExpressionStatement(call, nopos);
3722  }
3723 
3724  validate_output = factory()->NewIfStatement(
3725  is_receiver_call, factory()->EmptyStatement(), throw_call, nopos);
3726  }
3727 
3728  statements->Add(get_return, zone());
3729  statements->Add(check_return, zone());
3730  statements->Add(call_return, zone());
3731  statements->Add(validate_output, zone());
3732 }
3733 
3734 void Parser::FinalizeIteratorUse(Variable* completion, Expression* condition,
3735  Variable* iter, Block* iterator_use,
3736  Block* target, IteratorType type) {
3737  //
3738  // This function adds two statements to [target], corresponding to the
3739  // following code:
3740  //
3741  // completion = kNormalCompletion;
3742  // try {
3743  // try {
3744  // iterator_use
3745  // } catch(e) {
3746  // if (completion === kAbruptCompletion) completion = kThrowCompletion;
3747  // %ReThrow(e);
3748  // }
3749  // } finally {
3750  // if (condition) {
3751  // #BuildIteratorCloseForCompletion(iter, completion)
3752  // }
3753  // }
3754  //
3755 
3756  const int nopos = kNoSourcePosition;
3757 
3758  // completion = kNormalCompletion;
3759  Statement* initialize_completion;
3760  {
3761  Expression* proxy = factory()->NewVariableProxy(completion);
3762  Expression* assignment = factory()->NewAssignment(
3763  Token::ASSIGN, proxy,
3764  factory()->NewSmiLiteral(Parser::kNormalCompletion, nopos), nopos);
3765  initialize_completion =
3766  factory()->NewExpressionStatement(assignment, nopos);
3767  }
3768 
3769  // if (completion === kAbruptCompletion) completion = kThrowCompletion;
3770  Statement* set_completion_throw;
3771  {
3772  Expression* condition = factory()->NewCompareOperation(
3773  Token::EQ_STRICT, factory()->NewVariableProxy(completion),
3774  factory()->NewSmiLiteral(Parser::kAbruptCompletion, nopos), nopos);
3775 
3776  Expression* proxy = factory()->NewVariableProxy(completion);
3777  Expression* assignment = factory()->NewAssignment(
3778  Token::ASSIGN, proxy,
3779  factory()->NewSmiLiteral(Parser::kThrowCompletion, nopos), nopos);
3780  Statement* statement = factory()->NewExpressionStatement(assignment, nopos);
3781  set_completion_throw = factory()->NewIfStatement(
3782  condition, statement, factory()->EmptyStatement(), nopos);
3783  }
3784 
3785  // if (condition) {
3786  // #BuildIteratorCloseForCompletion(iter, completion)
3787  // }
3788  Block* maybe_close;
3789  {
3790  Block* block = factory()->NewBlock(2, true);
3791  Expression* proxy = factory()->NewVariableProxy(completion);
3792  BuildIteratorCloseForCompletion(block->statements(), iter, proxy, type);
3793  DCHECK_EQ(block->statements()->length(), 2);
3794 
3795  maybe_close = IgnoreCompletion(factory()->NewIfStatement(
3796  condition, block, factory()->EmptyStatement(), nopos));
3797  }
3798 
3799  // try { #try_block }
3800  // catch(e) {
3801  // #set_completion_throw;
3802  // %ReThrow(e);
3803  // }
3804  Statement* try_catch;
3805  {
3806  Scope* catch_scope = NewHiddenCatchScope();
3807 
3808  Statement* rethrow;
3809  // We use %ReThrow rather than the ordinary throw because we want to
3810  // preserve the original exception message. This is also why we create a
3811  // TryCatchStatementForReThrow below (which does not clear the pending
3812  // message), rather than a TryCatchStatement.
3813  {
3814  ScopedPtrList<Expression> args(pointer_buffer());
3815  args.Add(factory()->NewVariableProxy(catch_scope->catch_variable()));
3816  rethrow = factory()->NewExpressionStatement(
3817  factory()->NewCallRuntime(Runtime::kReThrow, args, nopos), nopos);
3818  }
3819 
3820  Block* catch_block = factory()->NewBlock(2, false);
3821  catch_block->statements()->Add(set_completion_throw, zone());
3822  catch_block->statements()->Add(rethrow, zone());
3823 
3824  try_catch = factory()->NewTryCatchStatementForReThrow(
3825  iterator_use, catch_scope, catch_block, nopos);
3826  }
3827 
3828  // try { #try_catch } finally { #maybe_close }
3829  Statement* try_finally;
3830  {
3831  Block* try_block = factory()->NewBlock(1, false);
3832  try_block->statements()->Add(try_catch, zone());
3833 
3834  try_finally =
3835  factory()->NewTryFinallyStatement(try_block, maybe_close, nopos);
3836  }
3837 
3838  target->statements()->Add(initialize_completion, zone());
3839  target->statements()->Add(try_finally, zone());
3840 }
3841 
3842 void Parser::BuildIteratorCloseForCompletion(ZonePtrList<Statement>* statements,
3843  Variable* iterator,
3844  Expression* completion,
3845  IteratorType type) {
3846  //
3847  // This function adds two statements to [statements], corresponding to the
3848  // following code:
3849  //
3850  // let iteratorReturn = iterator.return;
3851  // if (!IS_NULL_OR_UNDEFINED(iteratorReturn)) {
3852  // if (completion === kThrowCompletion) {
3853  // if (!IS_CALLABLE(iteratorReturn)) {
3854  // throw MakeTypeError(kReturnMethodNotCallable);
3855  // }
3856  // [if (IteratorType == kAsync)]
3857  // try { Await(%_Call(iteratorReturn, iterator) } catch (_) { }
3858  // [else]
3859  // try { %_Call(iteratorReturn, iterator) } catch (_) { }
3860  // [endif]
3861  // } else {
3862  // [if (IteratorType == kAsync)]
3863  // let output = Await(%_Call(iteratorReturn, iterator));
3864  // [else]
3865  // let output = %_Call(iteratorReturn, iterator);
3866  // [endif]
3867  // if (!IS_RECEIVER(output)) {
3868  // %ThrowIterResultNotAnObject(output);
3869  // }
3870  // }
3871  // }
3872  //
3873 
3874  const int nopos = kNoSourcePosition;
3875  // let iteratorReturn = iterator.return;
3876  Variable* var_return = NewTemporary(ast_value_factory()->empty_string());
3877  Statement* get_return;
3878  {
3879  Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
3880  Expression* literal = factory()->NewStringLiteral(
3881  ast_value_factory()->return_string(), nopos);
3882  Expression* property =
3883  factory()->NewProperty(iterator_proxy, literal, nopos);
3884  Expression* return_proxy = factory()->NewVariableProxy(var_return);
3885  Expression* assignment =
3886  factory()->NewAssignment(Token::ASSIGN, return_proxy, property, nopos);
3887  get_return = factory()->NewExpressionStatement(assignment, nopos);
3888  }
3889 
3890  // if (!IS_CALLABLE(iteratorReturn)) {
3891  // throw MakeTypeError(kReturnMethodNotCallable);
3892  // }
3893  Statement* check_return_callable;
3894  {
3895  Expression* throw_expr =
3896  NewThrowTypeError(MessageTemplate::kReturnMethodNotCallable,
3897  ast_value_factory()->empty_string(), nopos);
3898  check_return_callable = CheckCallable(var_return, throw_expr, nopos);
3899  }
3900 
3901  // try { %_Call(iteratorReturn, iterator) } catch (_) { }
3902  Statement* try_call_return;
3903  {
3904  ScopedPtrList<Expression> args(pointer_buffer());
3905  args.Add(factory()->NewVariableProxy(var_return));
3906  args.Add(factory()->NewVariableProxy(iterator));
3907 
3908  Expression* call =
3909  factory()->NewCallRuntime(Runtime::kInlineCall, args, nopos);
3910 
3911  if (type == IteratorType::kAsync) {
3912  function_state_->AddSuspend();
3913  call = factory()->NewAwait(call, nopos);
3914  }
3915 
3916  Block* try_block = factory()->NewBlock(1, false);
3917  try_block->statements()->Add(factory()->NewExpressionStatement(call, nopos),
3918  zone());
3919 
3920  Block* catch_block = factory()->NewBlock(0, false);
3921  try_call_return =
3922  factory()->NewTryCatchStatement(try_block, nullptr, catch_block, nopos);
3923  }
3924 
3925  // let output = %_Call(iteratorReturn, iterator);
3926  // if (!IS_RECEIVER(output)) {
3927  // %ThrowIteratorResultNotAnObject(output);
3928  // }
3929  Block* validate_return;
3930  {
3931  Variable* var_output = NewTemporary(ast_value_factory()->empty_string());
3932  Statement* call_return;
3933  {
3934  ScopedPtrList<Expression> args(pointer_buffer());
3935  args.Add(factory()->NewVariableProxy(var_return));
3936  args.Add(factory()->NewVariableProxy(iterator));
3937  Expression* call =
3938  factory()->NewCallRuntime(Runtime::kInlineCall, args, nopos);
3939  if (type == IteratorType::kAsync) {
3940  function_state_->AddSuspend();
3941  call = factory()->NewAwait(call, nopos);
3942  }
3943 
3944  Expression* output_proxy = factory()->NewVariableProxy(var_output);
3945  Expression* assignment =
3946  factory()->NewAssignment(Token::ASSIGN, output_proxy, call, nopos);
3947  call_return = factory()->NewExpressionStatement(assignment, nopos);
3948  }
3949 
3950  Expression* is_receiver_call;
3951  {
3952  ScopedPtrList<Expression> args(pointer_buffer());
3953  args.Add(factory()->NewVariableProxy(var_output));
3954  is_receiver_call =
3955  factory()->NewCallRuntime(Runtime::kInlineIsJSReceiver, args, nopos);
3956  }
3957 
3958  Statement* throw_call;
3959  {
3960  ScopedPtrList<Expression> args(pointer_buffer());
3961  args.Add(factory()->NewVariableProxy(var_output));
3962  Expression* call = factory()->NewCallRuntime(
3963  Runtime::kThrowIteratorResultNotAnObject, args, nopos);
3964  throw_call = factory()->NewExpressionStatement(call, nopos);
3965  }
3966 
3967  Statement* check_return = factory()->NewIfStatement(
3968  is_receiver_call, factory()->EmptyStatement(), throw_call, nopos);
3969 
3970  validate_return = factory()->NewBlock(2, false);
3971  validate_return->statements()->Add(call_return, zone());
3972  validate_return->statements()->Add(check_return, zone());
3973  }
3974 
3975  // if (completion === kThrowCompletion) {
3976  // #check_return_callable;
3977  // #try_call_return;
3978  // } else {
3979  // #validate_return;
3980  // }
3981  Statement* call_return_carefully;
3982  {
3983  Expression* condition = factory()->NewCompareOperation(
3984  Token::EQ_STRICT, completion,
3985  factory()->NewSmiLiteral(Parser::kThrowCompletion, nopos), nopos);
3986 
3987  Block* then_block = factory()->NewBlock(2, false);
3988  then_block->statements()->Add(check_return_callable, zone());
3989  then_block->statements()->Add(try_call_return, zone());
3990 
3991  call_return_carefully = factory()->NewIfStatement(condition, then_block,
3992  validate_return, nopos);
3993  }
3994 
3995  // if (!IS_NULL_OR_UNDEFINED(iteratorReturn)) { ... }
3996  Statement* maybe_call_return;
3997  {
3998  Expression* condition = factory()->NewCompareOperation(
3999  Token::EQ, factory()->NewVariableProxy(var_return),
4000  factory()->NewNullLiteral(nopos), nopos);
4001 
4002  maybe_call_return = factory()->NewIfStatement(
4003  condition, factory()->EmptyStatement(), call_return_carefully, nopos);
4004  }
4005 
4006  statements->Add(get_return, zone());
4007  statements->Add(maybe_call_return, zone());
4008 }
4009 
4010 Statement* Parser::FinalizeForOfStatement(ForOfStatement* loop,
4011  Variable* var_completion,
4012  IteratorType type, int pos) {
4013  //
4014  // This function replaces the loop with the following wrapping:
4015  //
4016  // completion = kNormalCompletion;
4017  // try {
4018  // try {
4019  // #loop;
4020  // } catch(e) {
4021  // if (completion === kAbruptCompletion) completion = kThrowCompletion;
4022  // %ReThrow(e);
4023  // }
4024  // } finally {
4025  // if (!(completion === kNormalCompletion)) {
4026  // #BuildIteratorCloseForCompletion(#iterator, completion)
4027  // }
4028  // }
4029  //
4030  // Note that the loop's body and its assign_each already contain appropriate
4031  // assignments to completion (see InitializeForOfStatement).
4032  //
4033 
4034  const int nopos = kNoSourcePosition;
4035 
4036  // !(completion === kNormalCompletion)
4037  Expression* closing_condition;
4038  {
4039  Expression* cmp = factory()->NewCompareOperation(
4040  Token::EQ_STRICT, factory()->NewVariableProxy(var_completion),
4041  factory()->NewSmiLiteral(Parser::kNormalCompletion, nopos), nopos);
4042  closing_condition = factory()->NewUnaryOperation(Token::NOT, cmp, nopos);
4043  }
4044 
4045  Block* final_loop = factory()->NewBlock(2, false);
4046  {
4047  Block* try_block = factory()->NewBlock(1, false);
4048  try_block->statements()->Add(loop, zone());
4049 
4050  FinalizeIteratorUse(var_completion, closing_condition, loop->iterator(),
4051  try_block, final_loop, type);
4052  }
4053 
4054  return final_loop;
4055 }
4056 
4057 } // namespace internal
4058 } // namespace v8
Definition: libplatform.h:13