11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/parsing/parsing.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include <memory>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "src/ast/ast.h"
101cb0ef41Sopenharmony_ci#include "src/base/v8-fallthrough.h"
111cb0ef41Sopenharmony_ci#include "src/execution/vm-state-inl.h"
121cb0ef41Sopenharmony_ci#include "src/handles/maybe-handles.h"
131cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
141cb0ef41Sopenharmony_ci#include "src/parsing/parse-info.h"
151cb0ef41Sopenharmony_ci#include "src/parsing/parser.h"
161cb0ef41Sopenharmony_ci#include "src/parsing/rewriter.h"
171cb0ef41Sopenharmony_ci#include "src/parsing/scanner-character-streams.h"
181cb0ef41Sopenharmony_ci#include "src/zone/zone-list-inl.h"  // crbug.com/v8/8816
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cinamespace v8 {
211cb0ef41Sopenharmony_cinamespace internal {
221cb0ef41Sopenharmony_cinamespace parsing {
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cinamespace {
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_civoid MaybeReportStatistics(ParseInfo* info, Handle<Script> script,
271cb0ef41Sopenharmony_ci                           Isolate* isolate, Parser* parser,
281cb0ef41Sopenharmony_ci                           ReportStatisticsMode mode) {
291cb0ef41Sopenharmony_ci  switch (mode) {
301cb0ef41Sopenharmony_ci    case ReportStatisticsMode::kYes:
311cb0ef41Sopenharmony_ci      parser->UpdateStatistics(isolate, script);
321cb0ef41Sopenharmony_ci      break;
331cb0ef41Sopenharmony_ci    case ReportStatisticsMode::kNo:
341cb0ef41Sopenharmony_ci      break;
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci}  // namespace
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cibool ParseProgram(ParseInfo* info, Handle<Script> script,
411cb0ef41Sopenharmony_ci                  MaybeHandle<ScopeInfo> maybe_outer_scope_info,
421cb0ef41Sopenharmony_ci                  Isolate* isolate, ReportStatisticsMode mode) {
431cb0ef41Sopenharmony_ci  DCHECK(info->flags().is_toplevel());
441cb0ef41Sopenharmony_ci  DCHECK_NULL(info->literal());
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  VMState<PARSER> state(isolate);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  // Create a character stream for the parser.
491cb0ef41Sopenharmony_ci  Handle<String> source(String::cast(script->source()), isolate);
501cb0ef41Sopenharmony_ci  isolate->counters()->total_parse_size()->Increment(source->length());
511cb0ef41Sopenharmony_ci  std::unique_ptr<Utf16CharacterStream> stream(
521cb0ef41Sopenharmony_ci      ScannerStream::For(isolate, source));
531cb0ef41Sopenharmony_ci  info->set_character_stream(std::move(stream));
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  Parser parser(isolate->main_thread_local_isolate(), info, script);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  // Ok to use Isolate here; this function is only called in the main thread.
581cb0ef41Sopenharmony_ci  DCHECK(parser.parsing_on_main_thread_);
591cb0ef41Sopenharmony_ci  parser.ParseProgram(isolate, script, info, maybe_outer_scope_info);
601cb0ef41Sopenharmony_ci  MaybeReportStatistics(info, script, isolate, &parser, mode);
611cb0ef41Sopenharmony_ci  return info->literal() != nullptr;
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cibool ParseProgram(ParseInfo* info, Handle<Script> script, Isolate* isolate,
651cb0ef41Sopenharmony_ci                  ReportStatisticsMode mode) {
661cb0ef41Sopenharmony_ci  return ParseProgram(info, script, kNullMaybeHandle, isolate, mode);
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cibool ParseFunction(ParseInfo* info, Handle<SharedFunctionInfo> shared_info,
701cb0ef41Sopenharmony_ci                   Isolate* isolate, ReportStatisticsMode mode) {
711cb0ef41Sopenharmony_ci  DCHECK(!info->flags().is_toplevel());
721cb0ef41Sopenharmony_ci  DCHECK(!shared_info.is_null());
731cb0ef41Sopenharmony_ci  DCHECK_NULL(info->literal());
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  VMState<PARSER> state(isolate);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  // Create a character stream for the parser.
781cb0ef41Sopenharmony_ci  Handle<Script> script(Script::cast(shared_info->script()), isolate);
791cb0ef41Sopenharmony_ci  Handle<String> source(String::cast(script->source()), isolate);
801cb0ef41Sopenharmony_ci  isolate->counters()->total_parse_size()->Increment(source->length());
811cb0ef41Sopenharmony_ci  std::unique_ptr<Utf16CharacterStream> stream(
821cb0ef41Sopenharmony_ci      ScannerStream::For(isolate, source, shared_info->StartPosition(),
831cb0ef41Sopenharmony_ci                         shared_info->EndPosition()));
841cb0ef41Sopenharmony_ci  info->set_character_stream(std::move(stream));
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  Parser parser(isolate->main_thread_local_isolate(), info, script);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  // Ok to use Isolate here; this function is only called in the main thread.
891cb0ef41Sopenharmony_ci  DCHECK(parser.parsing_on_main_thread_);
901cb0ef41Sopenharmony_ci  parser.ParseFunction(isolate, info, shared_info);
911cb0ef41Sopenharmony_ci  MaybeReportStatistics(info, script, isolate, &parser, mode);
921cb0ef41Sopenharmony_ci  return info->literal() != nullptr;
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_cibool ParseAny(ParseInfo* info, Handle<SharedFunctionInfo> shared_info,
961cb0ef41Sopenharmony_ci              Isolate* isolate, ReportStatisticsMode mode) {
971cb0ef41Sopenharmony_ci  DCHECK(!shared_info.is_null());
981cb0ef41Sopenharmony_ci  if (info->flags().is_toplevel()) {
991cb0ef41Sopenharmony_ci    MaybeHandle<ScopeInfo> maybe_outer_scope_info;
1001cb0ef41Sopenharmony_ci    if (shared_info->HasOuterScopeInfo()) {
1011cb0ef41Sopenharmony_ci      maybe_outer_scope_info =
1021cb0ef41Sopenharmony_ci          handle(shared_info->GetOuterScopeInfo(), isolate);
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci    return ParseProgram(info,
1051cb0ef41Sopenharmony_ci                        handle(Script::cast(shared_info->script()), isolate),
1061cb0ef41Sopenharmony_ci                        maybe_outer_scope_info, isolate, mode);
1071cb0ef41Sopenharmony_ci  }
1081cb0ef41Sopenharmony_ci  return ParseFunction(info, shared_info, isolate, mode);
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci}  // namespace parsing
1121cb0ef41Sopenharmony_ci}  // namespace internal
1131cb0ef41Sopenharmony_ci}  // namespace v8
114