V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
parse-info.h
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_PARSING_PARSE_INFO_H_
6 #define V8_PARSING_PARSE_INFO_H_
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include "include/v8.h"
13 #include "src/globals.h"
14 #include "src/handles.h"
15 #include "src/objects/script.h"
16 #include "src/parsing/preparsed-scope-data.h"
17 #include "src/pending-compilation-error-handler.h"
18 
19 namespace v8 {
20 
21 class Extension;
22 
23 namespace internal {
24 
25 class AccountingAllocator;
26 class AstRawString;
27 class AstStringConstants;
28 class AstValueFactory;
29 class CompilerDispatcher;
30 class DeclarationScope;
31 class FunctionLiteral;
32 class RuntimeCallStats;
33 class Logger;
34 class SourceRangeMap;
35 class Utf16CharacterStream;
36 class Zone;
37 
38 // A container for the inputs, configuration options, and outputs of parsing.
39 class V8_EXPORT_PRIVATE ParseInfo {
40  public:
41  explicit ParseInfo(AccountingAllocator* zone_allocator);
42  explicit ParseInfo(Isolate*);
43  ParseInfo(Isolate*, AccountingAllocator* zone_allocator);
44  ParseInfo(Isolate* isolate, Handle<Script> script);
46 
47  // Creates a new parse info based on parent top-level |outer_parse_info| for
48  // function |literal|.
49  static std::unique_ptr<ParseInfo> FromParent(
50  const ParseInfo* outer_parse_info, AccountingAllocator* zone_allocator,
51  const FunctionLiteral* literal, const AstRawString* function_name);
52 
53  ~ParseInfo();
54 
55  Handle<Script> CreateScript(Isolate* isolate, Handle<String> source,
56  ScriptOriginOptions origin_options,
57  NativesFlag natives = NOT_NATIVES_CODE);
58 
59  // Either returns the ast-value-factory associcated with this ParseInfo, or
60  // creates and returns a new factory if none exists.
61  AstValueFactory* GetOrCreateAstValueFactory();
62 
63  Zone* zone() const { return zone_.get(); }
64 
65 // Convenience accessor methods for flags.
66 #define FLAG_ACCESSOR(flag, getter, setter) \
67  bool getter() const { return GetFlag(flag); } \
68  void setter() { SetFlag(flag); } \
69  void setter(bool val) { SetFlag(flag, val); }
70 
71  FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel)
72  FLAG_ACCESSOR(kEager, is_eager, set_eager)
73  FLAG_ACCESSOR(kEval, is_eval, set_eval)
74  FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode)
75  FLAG_ACCESSOR(kNative, is_native, set_native)
76  FLAG_ACCESSOR(kModule, is_module, set_module)
77  FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing)
78  FLAG_ACCESSOR(kIsNamedExpression, is_named_expression,
79  set_is_named_expression)
80  FLAG_ACCESSOR(kLazyCompile, lazy_compile, set_lazy_compile)
81  FLAG_ACCESSOR(kCollectTypeProfile, collect_type_profile,
82  set_collect_type_profile)
83  FLAG_ACCESSOR(kIsAsmWasmBroken, is_asm_wasm_broken, set_asm_wasm_broken)
84  FLAG_ACCESSOR(kBlockCoverageEnabled, block_coverage_enabled,
85  set_block_coverage_enabled)
86  FLAG_ACCESSOR(kOnBackgroundThread, on_background_thread,
87  set_on_background_thread)
88  FLAG_ACCESSOR(kWrappedAsFunction, is_wrapped_as_function,
89  set_wrapped_as_function)
90  FLAG_ACCESSOR(kAllowEvalCache, allow_eval_cache, set_allow_eval_cache)
91  FLAG_ACCESSOR(kIsDeclaration, is_declaration, set_declaration)
92  FLAG_ACCESSOR(kRequiresInstanceMembersInitializer,
93  requires_instance_members_initializer,
94  set_requires_instance_members_initializer);
95 #undef FLAG_ACCESSOR
96 
97  void set_parse_restriction(ParseRestriction restriction) {
98  SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION);
99  }
100 
101  ParseRestriction parse_restriction() const {
102  return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL
103  : NO_PARSE_RESTRICTION;
104  }
105 
106  Utf16CharacterStream* character_stream() const {
107  return character_stream_.get();
108  }
109  void set_character_stream(
110  std::unique_ptr<Utf16CharacterStream> character_stream);
111  void ResetCharacterStream();
112 
113  v8::Extension* extension() const { return extension_; }
114  void set_extension(v8::Extension* extension) { extension_ = extension; }
115 
116  void set_consumed_preparsed_scope_data(
117  std::unique_ptr<ConsumedPreParsedScopeData> data) {
118  consumed_preparsed_scope_data_.swap(data);
119  }
120  ConsumedPreParsedScopeData* consumed_preparsed_scope_data() {
121  return consumed_preparsed_scope_data_.get();
122  }
123 
124  DeclarationScope* script_scope() const { return script_scope_; }
125  void set_script_scope(DeclarationScope* script_scope) {
126  script_scope_ = script_scope;
127  }
128 
129  AstValueFactory* ast_value_factory() const {
130  DCHECK(ast_value_factory_.get());
131  return ast_value_factory_.get();
132  }
133 
134  const AstRawString* function_name() const { return function_name_; }
135  void set_function_name(const AstRawString* function_name) {
136  function_name_ = function_name;
137  }
138 
139  FunctionLiteral* literal() const { return literal_; }
140  void set_literal(FunctionLiteral* literal) { literal_ = literal; }
141 
142  DeclarationScope* scope() const;
143 
144  uintptr_t stack_limit() const { return stack_limit_; }
145  void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }
146 
147  uint64_t hash_seed() const { return hash_seed_; }
148  void set_hash_seed(uint64_t hash_seed) { hash_seed_ = hash_seed; }
149 
150  int start_position() const { return start_position_; }
151  void set_start_position(int start_position) {
152  start_position_ = start_position;
153  }
154 
155  int end_position() const { return end_position_; }
156  void set_end_position(int end_position) { end_position_ = end_position; }
157 
158  int parameters_end_pos() const { return parameters_end_pos_; }
159  void set_parameters_end_pos(int parameters_end_pos) {
160  parameters_end_pos_ = parameters_end_pos;
161  }
162 
163  int function_literal_id() const { return function_literal_id_; }
164  void set_function_literal_id(int function_literal_id) {
165  function_literal_id_ = function_literal_id;
166  }
167 
168  FunctionKind function_kind() const { return function_kind_; }
169  void set_function_kind(FunctionKind function_kind) {
170  function_kind_ = function_kind;
171  }
172 
173  int max_function_literal_id() const { return max_function_literal_id_; }
174  void set_max_function_literal_id(int max_function_literal_id) {
175  max_function_literal_id_ = max_function_literal_id;
176  }
177 
178  const AstStringConstants* ast_string_constants() const {
179  return ast_string_constants_;
180  }
181  void set_ast_string_constants(
182  const AstStringConstants* ast_string_constants) {
183  ast_string_constants_ = ast_string_constants;
184  }
185 
186  RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
187  void set_runtime_call_stats(RuntimeCallStats* runtime_call_stats) {
188  runtime_call_stats_ = runtime_call_stats;
189  }
190  Logger* logger() const { return logger_; }
191  void set_logger(Logger* logger) { logger_ = logger; }
192 
193  void AllocateSourceRangeMap();
194  SourceRangeMap* source_range_map() const { return source_range_map_; }
195  void set_source_range_map(SourceRangeMap* source_range_map) {
196  source_range_map_ = source_range_map;
197  }
198 
199  PendingCompilationErrorHandler* pending_error_handler() {
200  return &pending_error_handler_;
201  }
202 
204  public:
205  explicit ParallelTasks(CompilerDispatcher* compiler_dispatcher)
206  : dispatcher_(compiler_dispatcher) {
207  DCHECK(dispatcher_);
208  }
209 
210  void Enqueue(ParseInfo* outer_parse_info, const AstRawString* function_name,
211  FunctionLiteral* literal);
212 
213  typedef std::forward_list<std::pair<FunctionLiteral*, uintptr_t>>::iterator
214  EnqueuedJobsIterator;
215 
216  EnqueuedJobsIterator begin() { return enqueued_jobs_.begin(); }
217  EnqueuedJobsIterator end() { return enqueued_jobs_.end(); }
218 
219  CompilerDispatcher* dispatcher() { return dispatcher_; }
220 
221  private:
222  CompilerDispatcher* dispatcher_;
223  std::forward_list<std::pair<FunctionLiteral*, uintptr_t>> enqueued_jobs_;
224  };
225 
226  ParallelTasks* parallel_tasks() { return parallel_tasks_.get(); }
227 
228  //--------------------------------------------------------------------------
229  // TODO(titzer): these should not be part of ParseInfo.
230  //--------------------------------------------------------------------------
231  Handle<Script> script() const { return script_; }
232  void set_script(Handle<Script> script);
233 
234  MaybeHandle<ScopeInfo> maybe_outer_scope_info() const {
235  return maybe_outer_scope_info_;
236  }
237  void set_outer_scope_info(Handle<ScopeInfo> outer_scope_info) {
238  maybe_outer_scope_info_ = outer_scope_info;
239  }
240 
241  int script_id() const { return script_id_; }
242  //--------------------------------------------------------------------------
243 
244  LanguageMode language_mode() const {
245  return construct_language_mode(is_strict_mode());
246  }
247  void set_language_mode(LanguageMode language_mode) {
248  STATIC_ASSERT(LanguageModeSize == 2);
249  set_strict_mode(is_strict(language_mode));
250  }
251 
252  private:
253  void SetScriptForToplevelCompile(Isolate* isolate, Handle<Script> script);
254 
255  // Set function info flags based on those in either FunctionLiteral or
256  // SharedFunctionInfo |function|
257  template <typename T>
258  void SetFunctionInfo(T function);
259 
260  // Various configuration flags for parsing.
261  enum Flag {
262  // ---------- Input flags ---------------------------
263  kToplevel = 1 << 0,
264  kEager = 1 << 1,
265  kEval = 1 << 2,
266  kStrictMode = 1 << 3,
267  kNative = 1 << 4,
268  kParseRestriction = 1 << 5,
269  kModule = 1 << 6,
270  kAllowLazyParsing = 1 << 7,
271  kIsNamedExpression = 1 << 8,
272  kLazyCompile = 1 << 9,
273  kCollectTypeProfile = 1 << 10,
274  kBlockCoverageEnabled = 1 << 11,
275  kIsAsmWasmBroken = 1 << 12,
276  kOnBackgroundThread = 1 << 13,
277  kWrappedAsFunction = 1 << 14, // Implicitly wrapped as function.
278  kAllowEvalCache = 1 << 15,
279  kIsDeclaration = 1 << 16,
280  kRequiresInstanceMembersInitializer = 1 << 17,
281  };
282 
283  //------------- Inputs to parsing and scope analysis -----------------------
284  std::unique_ptr<Zone> zone_;
285  unsigned flags_;
286  v8::Extension* extension_;
287  DeclarationScope* script_scope_;
288  uintptr_t stack_limit_;
289  uint64_t hash_seed_;
290  FunctionKind function_kind_;
291  int script_id_;
292  int start_position_;
293  int end_position_;
294  int parameters_end_pos_;
295  int function_literal_id_;
296  int max_function_literal_id_;
297 
298  // TODO(titzer): Move handles out of ParseInfo.
299  Handle<Script> script_;
300  MaybeHandle<ScopeInfo> maybe_outer_scope_info_;
301 
302  //----------- Inputs+Outputs of parsing and scope analysis -----------------
303  std::unique_ptr<Utf16CharacterStream> character_stream_;
304  std::unique_ptr<ConsumedPreParsedScopeData> consumed_preparsed_scope_data_;
305  std::unique_ptr<AstValueFactory> ast_value_factory_;
306  const class AstStringConstants* ast_string_constants_;
307  const AstRawString* function_name_;
308  RuntimeCallStats* runtime_call_stats_;
309  Logger* logger_;
310  SourceRangeMap* source_range_map_; // Used when block coverage is enabled.
311  std::unique_ptr<ParallelTasks> parallel_tasks_;
312 
313  //----------- Output of parsing and scope analysis ------------------------
314  FunctionLiteral* literal_;
315  PendingCompilationErrorHandler pending_error_handler_;
316 
317  void SetFlag(Flag f) { flags_ |= f; }
318  void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; }
319  bool GetFlag(Flag f) const { return (flags_ & f) != 0; }
320 };
321 
322 } // namespace internal
323 } // namespace v8
324 
325 #endif // V8_PARSING_PARSE_INFO_H_
Definition: v8.h:85
Definition: libplatform.h:13