parse-info.h 12.9 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_PARSING_PARSE_INFO_H_
#define V8_PARSING_PARSE_INFO_H_

8
#include <map>
9
#include <memory>
10
#include <vector>
11

12
#include "src/base/bit-field.h"
13
#include "src/base/export-template.h"
14
#include "src/base/logging.h"
15
#include "src/common/globals.h"
16
#include "src/handles/handles.h"
17
#include "src/objects/function-kind.h"
18
#include "src/objects/function-syntax-kind.h"
19
#include "src/objects/script.h"
20
#include "src/parsing/pending-compilation-error-handler.h"
21
#include "src/parsing/preparse-data.h"
22 23 24 25 26 27 28

namespace v8 {

class Extension;

namespace internal {

29
class AccountingAllocator;
30
class AstRawString;
31
class AstStringConstants;
32
class AstValueFactory;
33
class LazyCompileDispatcher;
34 35
class DeclarationScope;
class FunctionLiteral;
36
class RuntimeCallStats;
37
class Logger;
38
class SourceRangeMap;
39
class Utf16CharacterStream;
40 41
class Zone;

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// The flags for a parse + unoptimized compile operation.
#define FLAG_FIELDS(V, _)                                \
  V(is_toplevel, bool, 1, _)                             \
  V(is_eager, bool, 1, _)                                \
  V(is_eval, bool, 1, _)                                 \
  V(outer_language_mode, LanguageMode, 1, _)             \
  V(parse_restriction, ParseRestriction, 1, _)           \
  V(is_module, bool, 1, _)                               \
  V(allow_lazy_parsing, bool, 1, _)                      \
  V(is_lazy_compile, bool, 1, _)                         \
  V(collect_type_profile, bool, 1, _)                    \
  V(coverage_enabled, bool, 1, _)                        \
  V(block_coverage_enabled, bool, 1, _)                  \
  V(is_asm_wasm_broken, bool, 1, _)                      \
  V(class_scope_has_private_brand, bool, 1, _)           \
  V(requires_instance_members_initializer, bool, 1, _)   \
  V(has_static_private_methods_or_accessors, bool, 1, _) \
  V(might_always_opt, bool, 1, _)                        \
  V(allow_natives_syntax, bool, 1, _)                    \
  V(allow_lazy_compile, bool, 1, _)                      \
  V(collect_source_positions, bool, 1, _)                \
  V(allow_harmony_top_level_await, bool, 1, _)           \
64
  V(is_repl_mode, bool, 1, _)
65 66 67 68

class V8_EXPORT_PRIVATE UnoptimizedCompileFlags {
 public:
  // Set-up flags for a toplevel compilation.
69 70 71 72 73
  static UnoptimizedCompileFlags ForToplevelCompile(Isolate* isolate,
                                                    bool is_user_javascript,
                                                    LanguageMode language_mode,
                                                    REPLMode repl_mode,
                                                    ScriptType type, bool lazy);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  // Set-up flags for a compiling a particular function (either a lazy compile
  // or a recompile).
  static UnoptimizedCompileFlags ForFunctionCompile(Isolate* isolate,
                                                    SharedFunctionInfo shared);

  // Set-up flags for a full compilation of a given script.
  static UnoptimizedCompileFlags ForScriptCompile(Isolate* isolate,
                                                  Script script);

  // Set-up flags for a parallel toplevel function compilation, based on the
  // flags of an existing toplevel compilation.
  static UnoptimizedCompileFlags ForToplevelFunction(
      const UnoptimizedCompileFlags toplevel_flags,
      const FunctionLiteral* literal);

  // Create flags for a test.
  static UnoptimizedCompileFlags ForTest(Isolate* isolate);

#define FLAG_GET_SET(NAME, TYPE, SIZE, _)                       \
  TYPE NAME() const { return BitFields::NAME::decode(flags_); } \
  UnoptimizedCompileFlags& set_##NAME(TYPE value) {             \
    flags_ = BitFields::NAME::update(flags_, value);            \
    return *this;                                               \
  }

  FLAG_FIELDS(FLAG_GET_SET, _)

  int script_id() const { return script_id_; }
  UnoptimizedCompileFlags& set_script_id(int value) {
    script_id_ = value;
    return *this;
  }

  FunctionKind function_kind() const { return function_kind_; }
  UnoptimizedCompileFlags& set_function_kind(FunctionKind value) {
    function_kind_ = value;
    return *this;
  }

  FunctionSyntaxKind function_syntax_kind() const {
    return function_syntax_kind_;
  }
  UnoptimizedCompileFlags& set_function_syntax_kind(FunctionSyntaxKind value) {
    function_syntax_kind_ = value;
    return *this;
  }

 private:
  struct BitFields {
    DEFINE_BIT_FIELDS(FLAG_FIELDS)
  };

  UnoptimizedCompileFlags(Isolate* isolate, int script_id);

  // Set function info flags based on those in either FunctionLiteral or
  // SharedFunctionInfo |function|
  template <typename T>
  void SetFlagsFromFunction(T function);
  void SetFlagsForToplevelCompile(bool is_collecting_type_profile,
                                  bool is_user_javascript,
                                  LanguageMode language_mode,
136 137
                                  REPLMode repl_mode, ScriptType type,
                                  bool lazy);
138 139 140 141 142 143 144 145 146
  void SetFlagsForFunctionFromScript(Script script);

  uint32_t flags_;
  int script_id_;
  FunctionKind function_kind_;
  FunctionSyntaxKind function_syntax_kind_;
};

#undef FLAG_FIELDS
147 148 149 150 151 152 153 154 155 156
class ParseInfo;

// The mutable state for a parse + unoptimized compile operation.
class V8_EXPORT_PRIVATE UnoptimizedCompileState {
 public:
  explicit UnoptimizedCompileState(Isolate*);
  UnoptimizedCompileState(const UnoptimizedCompileState& other) V8_NOEXCEPT;

  class ParallelTasks {
   public:
157 158
    explicit ParallelTasks(LazyCompileDispatcher* lazy_compile_dispatcher)
        : dispatcher_(lazy_compile_dispatcher) {
159 160 161 162 163 164 165 166 167 168 169 170
      DCHECK_NOT_NULL(dispatcher_);
    }

    void Enqueue(ParseInfo* outer_parse_info, const AstRawString* function_name,
                 FunctionLiteral* literal);

    using EnqueuedJobsIterator =
        std::forward_list<std::pair<FunctionLiteral*, uintptr_t>>::iterator;

    EnqueuedJobsIterator begin() { return enqueued_jobs_.begin(); }
    EnqueuedJobsIterator end() { return enqueued_jobs_.end(); }

171
    LazyCompileDispatcher* dispatcher() { return dispatcher_; }
172 173

   private:
174
    LazyCompileDispatcher* dispatcher_;
175 176 177 178 179 180 181 182 183 184 185 186
    std::forward_list<std::pair<FunctionLiteral*, uintptr_t>> enqueued_jobs_;
  };

  uint64_t hash_seed() const { return hash_seed_; }
  AccountingAllocator* allocator() const { return allocator_; }
  const AstStringConstants* ast_string_constants() const {
    return ast_string_constants_;
  }
  Logger* logger() const { return logger_; }
  PendingCompilationErrorHandler* pending_error_handler() {
    return &pending_error_handler_;
  }
187 188 189
  const PendingCompilationErrorHandler* pending_error_handler() const {
    return &pending_error_handler_;
  }
190 191 192 193 194 195 196 197 198 199
  ParallelTasks* parallel_tasks() const { return parallel_tasks_.get(); }

 private:
  uint64_t hash_seed_;
  AccountingAllocator* allocator_;
  const AstStringConstants* ast_string_constants_;
  PendingCompilationErrorHandler pending_error_handler_;
  Logger* logger_;
  std::unique_ptr<ParallelTasks> parallel_tasks_;
};
200

201
// A container for the inputs, configuration options, and outputs of parsing.
202
class V8_EXPORT_PRIVATE ParseInfo {
203
 public:
204 205
  ParseInfo(Isolate* isolate, const UnoptimizedCompileFlags flags,
            UnoptimizedCompileState* state);
206

207 208
  // Creates a new parse info based on parent top-level |outer_parse_info| for
  // function |literal|.
209 210 211
  static std::unique_ptr<ParseInfo> ForToplevelFunction(
      const UnoptimizedCompileFlags flags,
      UnoptimizedCompileState* compile_state, const FunctionLiteral* literal,
212
      const AstRawString* function_name);
213

214
  ~ParseInfo();
215

216
  template <typename IsolateT>
217
  EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
218
  Handle<Script> CreateScript(IsolateT* isolate, Handle<String> source,
219
                              MaybeHandle<FixedArray> maybe_wrapped_arguments,
220 221
                              ScriptOriginOptions origin_options,
                              NativesFlag natives = NOT_NATIVES_CODE);
222

223 224 225 226
  // Either returns the ast-value-factory associcated with this ParseInfo, or
  // creates and returns a new factory if none exists.
  AstValueFactory* GetOrCreateAstValueFactory();

227 228
  Zone* zone() const { return zone_.get(); }

229
  const UnoptimizedCompileFlags& flags() const { return flags_; }
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  // Getters for state.
  uint64_t hash_seed() const { return state_->hash_seed(); }
  AccountingAllocator* allocator() const { return state_->allocator(); }
  const AstStringConstants* ast_string_constants() const {
    return state_->ast_string_constants();
  }
  Logger* logger() const { return state_->logger(); }
  PendingCompilationErrorHandler* pending_error_handler() {
    return state_->pending_error_handler();
  }
  UnoptimizedCompileState::ParallelTasks* parallel_tasks() const {
    return state_->parallel_tasks();
  }
  const UnoptimizedCompileState* state() const { return state_; }

  // Accessors for per-thread state.
  uintptr_t stack_limit() const { return stack_limit_; }
  RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
  void SetPerThreadState(uintptr_t stack_limit,
                         RuntimeCallStats* runtime_call_stats) {
    stack_limit_ = stack_limit;
    runtime_call_stats_ = runtime_call_stats;
  }

255 256 257
  // Accessor methods for output flags.
  bool allow_eval_cache() const { return allow_eval_cache_; }
  void set_allow_eval_cache(bool value) { allow_eval_cache_ = value; }
258 259

#if V8_ENABLE_WEBASSEMBLY
260 261
  bool contains_asm_module() const { return contains_asm_module_; }
  void set_contains_asm_module(bool value) { contains_asm_module_ = value; }
262 263
#endif  // V8_ENABLE_WEBASSEMBLY

264 265
  LanguageMode language_mode() const { return language_mode_; }
  void set_language_mode(LanguageMode value) { language_mode_ = value; }
266

267 268 269 270 271
  Utf16CharacterStream* character_stream() const {
    return character_stream_.get();
  }
  void set_character_stream(
      std::unique_ptr<Utf16CharacterStream> character_stream);
272
  void ResetCharacterStream();
273 274 275 276

  v8::Extension* extension() const { return extension_; }
  void set_extension(v8::Extension* extension) { extension_ = extension; }

277 278
  void set_consumed_preparse_data(std::unique_ptr<ConsumedPreparseData> data) {
    consumed_preparse_data_.swap(data);
279
  }
280 281
  ConsumedPreparseData* consumed_preparse_data() {
    return consumed_preparse_data_.get();
282
  }
283

284 285 286 287 288
  DeclarationScope* script_scope() const { return script_scope_; }
  void set_script_scope(DeclarationScope* script_scope) {
    script_scope_ = script_scope;
  }

289 290 291
  AstValueFactory* ast_value_factory() const {
    DCHECK(ast_value_factory_.get());
    return ast_value_factory_.get();
292 293 294 295 296 297 298 299 300 301 302 303
  }

  const AstRawString* function_name() const { return function_name_; }
  void set_function_name(const AstRawString* function_name) {
    function_name_ = function_name;
  }

  FunctionLiteral* literal() const { return literal_; }
  void set_literal(FunctionLiteral* literal) { literal_ = literal; }

  DeclarationScope* scope() const;

304 305 306 307 308
  int parameters_end_pos() const { return parameters_end_pos_; }
  void set_parameters_end_pos(int parameters_end_pos) {
    parameters_end_pos_ = parameters_end_pos;
  }

309
  bool is_wrapped_as_function() const {
310
    return flags().function_syntax_kind() == FunctionSyntaxKind::kWrapped;
311 312
  }

313 314 315 316 317
  int max_function_literal_id() const { return max_function_literal_id_; }
  void set_max_function_literal_id(int max_function_literal_id) {
    max_function_literal_id_ = max_function_literal_id;
  }

318
  void AllocateSourceRangeMap();
319 320 321 322 323
  SourceRangeMap* source_range_map() const { return source_range_map_; }
  void set_source_range_map(SourceRangeMap* source_range_map) {
    source_range_map_ = source_range_map;
  }

324
  void CheckFlagsForFunctionFromScript(Script script);
325

326
 private:
327 328
  ParseInfo(const UnoptimizedCompileFlags flags,
            UnoptimizedCompileState* state);
329

330 331
  void CheckFlagsForToplevelCompileFromScript(Script script,
                                              bool is_collecting_type_profile);
332

333
  //------------- Inputs to parsing and scope analysis -----------------------
334
  const UnoptimizedCompileFlags flags_;
335 336
  UnoptimizedCompileState* state_;

337
  std::unique_ptr<Zone> zone_;
338 339 340
  v8::Extension* extension_;
  DeclarationScope* script_scope_;
  uintptr_t stack_limit_;
341
  int parameters_end_pos_;
342
  int max_function_literal_id_;
343 344

  //----------- Inputs+Outputs of parsing and scope analysis -----------------
345
  std::unique_ptr<Utf16CharacterStream> character_stream_;
346
  std::unique_ptr<ConsumedPreparseData> consumed_preparse_data_;
347
  std::unique_ptr<AstValueFactory> ast_value_factory_;
348
  const AstRawString* function_name_;
349
  RuntimeCallStats* runtime_call_stats_;
350
  SourceRangeMap* source_range_map_;  // Used when block coverage is enabled.
351 352 353

  //----------- Output of parsing and scope analysis ------------------------
  FunctionLiteral* literal_;
354
  bool allow_eval_cache_ : 1;
355
#if V8_ENABLE_WEBASSEMBLY
356
  bool contains_asm_module_ : 1;
357
#endif  // V8_ENABLE_WEBASSEMBLY
358
  LanguageMode language_mode_ : 1;
359 360 361 362 363 364
};

}  // namespace internal
}  // namespace v8

#endif  // V8_PARSING_PARSE_INFO_H_