Line data Source code
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/preparse-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 4882259 : 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);
45 : ParseInfo(Isolate* isolate, Handle<SharedFunctionInfo> shared);
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 2851824 : FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel)
72 : FLAG_ACCESSOR(kEager, is_eager, set_eager)
73 10110418 : FLAG_ACCESSOR(kEval, is_eval, set_eval)
74 734217 : FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode)
75 2114364 : FLAG_ACCESSOR(kNative, is_native, set_native)
76 4882098 : 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 2114364 : FLAG_ACCESSOR(kCollectTypeProfile, collect_type_profile,
82 : set_collect_type_profile)
83 2112809 : FLAG_ACCESSOR(kIsAsmWasmBroken, is_asm_wasm_broken, set_asm_wasm_broken)
84 2440880 : FLAG_ACCESSOR(kContainsAsmModule, contains_asm_module,
85 : set_contains_asm_module)
86 13051 : FLAG_ACCESSOR(kBlockCoverageEnabled, block_coverage_enabled,
87 : set_block_coverage_enabled)
88 7320350 : FLAG_ACCESSOR(kOnBackgroundThread, on_background_thread,
89 : set_on_background_thread)
90 2918235 : FLAG_ACCESSOR(kWrappedAsFunction, is_wrapped_as_function,
91 : set_wrapped_as_function)
92 849930 : FLAG_ACCESSOR(kAllowEvalCache, allow_eval_cache, set_allow_eval_cache)
93 : FLAG_ACCESSOR(kIsDeclaration, is_declaration, set_declaration)
94 712316 : FLAG_ACCESSOR(kRequiresInstanceMembersInitializer,
95 : requires_instance_members_initializer,
96 : set_requires_instance_members_initializer);
97 2114364 : FLAG_ACCESSOR(kMightAlwaysOpt, might_always_opt, set_might_always_opt)
98 2441005 : FLAG_ACCESSOR(kAllowNativeSyntax, allow_natives_syntax,
99 : set_allow_natives_syntax)
100 4882010 : FLAG_ACCESSOR(kAllowLazyCompile, allow_lazy_compile, set_allow_lazy_compile)
101 : FLAG_ACCESSOR(kAllowNativeSyntax, allow_native_syntax,
102 : set_allow_native_syntax);
103 2441005 : FLAG_ACCESSOR(kAllowHarmonyPublicFields, allow_harmony_public_fields,
104 : set_allow_harmony_public_fields);
105 2441005 : FLAG_ACCESSOR(kAllowHarmonyStaticFields, allow_harmony_static_fields,
106 : set_allow_harmony_static_fields);
107 2441005 : FLAG_ACCESSOR(kAllowHarmonyDynamicImport, allow_harmony_dynamic_import,
108 : set_allow_harmony_dynamic_import);
109 2441005 : FLAG_ACCESSOR(kAllowHarmonyImportMeta, allow_harmony_import_meta,
110 : set_allow_harmony_import_meta);
111 2441005 : FLAG_ACCESSOR(kAllowHarmonyNumericSeparator, allow_harmony_numeric_separator,
112 : set_allow_harmony_numeric_separator);
113 2441005 : FLAG_ACCESSOR(kAllowHarmonyPrivateFields, allow_harmony_private_fields,
114 : set_allow_harmony_private_fields);
115 2441005 : FLAG_ACCESSOR(kAllowHarmonyPrivateMethods, allow_harmony_private_methods,
116 : set_allow_harmony_private_methods);
117 712316 : FLAG_ACCESSOR(kIsIIFE, is_iife, set_is_iife);
118 : #undef FLAG_ACCESSOR
119 :
120 : void set_parse_restriction(ParseRestriction restriction) {
121 : SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION);
122 : }
123 :
124 1723478 : ParseRestriction parse_restriction() const {
125 : return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL
126 1723478 : : NO_PARSE_RESTRICTION;
127 : }
128 :
129 : Utf16CharacterStream* character_stream() const {
130 : return character_stream_.get();
131 : }
132 : void set_character_stream(
133 : std::unique_ptr<Utf16CharacterStream> character_stream);
134 : void ResetCharacterStream();
135 :
136 : v8::Extension* extension() const { return extension_; }
137 157585 : void set_extension(v8::Extension* extension) { extension_ = extension; }
138 :
139 : void set_consumed_preparse_data(std::unique_ptr<ConsumedPreparseData> data) {
140 : consumed_preparse_data_.swap(data);
141 : }
142 : ConsumedPreparseData* consumed_preparse_data() {
143 : return consumed_preparse_data_.get();
144 : }
145 :
146 : DeclarationScope* script_scope() const { return script_scope_; }
147 : void set_script_scope(DeclarationScope* script_scope) {
148 2441037 : script_scope_ = script_scope;
149 : }
150 :
151 207 : AstValueFactory* ast_value_factory() const {
152 : DCHECK(ast_value_factory_.get());
153 207 : return ast_value_factory_.get();
154 : }
155 :
156 : const AstRawString* function_name() const { return function_name_; }
157 : void set_function_name(const AstRawString* function_name) {
158 717505 : function_name_ = function_name;
159 : }
160 :
161 207 : FunctionLiteral* literal() const { return literal_; }
162 2441040 : void set_literal(FunctionLiteral* literal) { literal_ = literal; }
163 :
164 : DeclarationScope* scope() const;
165 :
166 : uintptr_t stack_limit() const { return stack_limit_; }
167 2467297 : void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }
168 :
169 : uint64_t hash_seed() const { return hash_seed_; }
170 2441133 : void set_hash_seed(uint64_t hash_seed) { hash_seed_ = hash_seed; }
171 :
172 : int start_position() const { return start_position_; }
173 : void set_start_position(int start_position) {
174 719384 : start_position_ = start_position;
175 : }
176 :
177 : int end_position() const { return end_position_; }
178 719388 : void set_end_position(int end_position) { end_position_ = end_position; }
179 :
180 : int parameters_end_pos() const { return parameters_end_pos_; }
181 : void set_parameters_end_pos(int parameters_end_pos) {
182 953755 : parameters_end_pos_ = parameters_end_pos;
183 : }
184 :
185 : int function_literal_id() const { return function_literal_id_; }
186 : void set_function_literal_id(int function_literal_id) {
187 80 : function_literal_id_ = function_literal_id;
188 : }
189 :
190 : FunctionKind function_kind() const { return function_kind_; }
191 : void set_function_kind(FunctionKind function_kind) {
192 719392 : function_kind_ = function_kind;
193 : }
194 :
195 : int max_function_literal_id() const { return max_function_literal_id_; }
196 : void set_max_function_literal_id(int max_function_literal_id) {
197 1723524 : max_function_literal_id_ = max_function_literal_id;
198 : }
199 :
200 : const AstStringConstants* ast_string_constants() const {
201 : return ast_string_constants_;
202 : }
203 : void set_ast_string_constants(
204 : const AstStringConstants* ast_string_constants) {
205 2441133 : ast_string_constants_ = ast_string_constants;
206 : }
207 :
208 : RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
209 : void set_runtime_call_stats(RuntimeCallStats* runtime_call_stats) {
210 2467297 : runtime_call_stats_ = runtime_call_stats;
211 : }
212 : Logger* logger() const { return logger_; }
213 2441133 : void set_logger(Logger* logger) { logger_ = logger; }
214 :
215 : void AllocateSourceRangeMap();
216 : SourceRangeMap* source_range_map() const { return source_range_map_; }
217 : void set_source_range_map(SourceRangeMap* source_range_map) {
218 815 : source_range_map_ = source_range_map;
219 : }
220 :
221 : PendingCompilationErrorHandler* pending_error_handler() {
222 : return &pending_error_handler_;
223 : }
224 :
225 : class ParallelTasks {
226 : public:
227 : explicit ParallelTasks(CompilerDispatcher* compiler_dispatcher)
228 98 : : dispatcher_(compiler_dispatcher) {
229 : DCHECK(dispatcher_);
230 : }
231 :
232 : void Enqueue(ParseInfo* outer_parse_info, const AstRawString* function_name,
233 : FunctionLiteral* literal);
234 :
235 : typedef std::forward_list<std::pair<FunctionLiteral*, uintptr_t>>::iterator
236 : EnqueuedJobsIterator;
237 :
238 : EnqueuedJobsIterator begin() { return enqueued_jobs_.begin(); }
239 : EnqueuedJobsIterator end() { return enqueued_jobs_.end(); }
240 :
241 : CompilerDispatcher* dispatcher() { return dispatcher_; }
242 :
243 : private:
244 : CompilerDispatcher* dispatcher_;
245 : std::forward_list<std::pair<FunctionLiteral*, uintptr_t>> enqueued_jobs_;
246 : };
247 :
248 : ParallelTasks* parallel_tasks() { return parallel_tasks_.get(); }
249 :
250 : //--------------------------------------------------------------------------
251 : // TODO(titzer): these should not be part of ParseInfo.
252 : //--------------------------------------------------------------------------
253 : Handle<Script> script() const { return script_; }
254 : void set_script(Handle<Script> script);
255 :
256 : MaybeHandle<ScopeInfo> maybe_outer_scope_info() const {
257 : return maybe_outer_scope_info_;
258 : }
259 : void set_outer_scope_info(Handle<ScopeInfo> outer_scope_info) {
260 1011008 : maybe_outer_scope_info_ = outer_scope_info;
261 : }
262 :
263 : int script_id() const { return script_id_; }
264 : //--------------------------------------------------------------------------
265 :
266 : LanguageMode language_mode() const {
267 : return construct_language_mode(is_strict_mode());
268 : }
269 : void set_language_mode(LanguageMode language_mode) {
270 : STATIC_ASSERT(LanguageModeSize == 2);
271 : set_strict_mode(is_strict(language_mode));
272 : }
273 :
274 : private:
275 : void SetScriptForToplevelCompile(Isolate* isolate, Handle<Script> script);
276 :
277 : // Set function info flags based on those in either FunctionLiteral or
278 : // SharedFunctionInfo |function|
279 : template <typename T>
280 : void SetFunctionInfo(T function);
281 :
282 : // Various configuration flags for parsing.
283 : enum Flag {
284 : // ---------- Input flags ---------------------------
285 : kToplevel = 1 << 0,
286 : kEager = 1 << 1,
287 : kEval = 1 << 2,
288 : kStrictMode = 1 << 3,
289 : kNative = 1 << 4,
290 : kParseRestriction = 1 << 5,
291 : kModule = 1 << 6,
292 : kAllowLazyParsing = 1 << 7,
293 : kIsNamedExpression = 1 << 8,
294 : kLazyCompile = 1 << 9,
295 : kCollectTypeProfile = 1 << 10,
296 : kBlockCoverageEnabled = 1 << 11,
297 : kIsAsmWasmBroken = 1 << 12,
298 : kOnBackgroundThread = 1 << 13,
299 : kWrappedAsFunction = 1 << 14, // Implicitly wrapped as function.
300 : kAllowEvalCache = 1 << 15,
301 : kIsDeclaration = 1 << 16,
302 : kRequiresInstanceMembersInitializer = 1 << 17,
303 : kContainsAsmModule = 1 << 18,
304 : kMightAlwaysOpt = 1 << 19,
305 : kAllowLazyCompile = 1 << 20,
306 : kAllowNativeSyntax = 1 << 21,
307 : kAllowHarmonyPublicFields = 1 << 22,
308 : kAllowHarmonyStaticFields = 1 << 23,
309 : kAllowHarmonyDynamicImport = 1 << 24,
310 : kAllowHarmonyImportMeta = 1 << 25,
311 : kAllowHarmonyNumericSeparator = 1 << 26,
312 : kAllowHarmonyPrivateFields = 1 << 27,
313 : kAllowHarmonyPrivateMethods = 1 << 28,
314 : kIsIIFE = 1 << 29
315 : };
316 :
317 : //------------- Inputs to parsing and scope analysis -----------------------
318 : std::unique_ptr<Zone> zone_;
319 : unsigned flags_;
320 : v8::Extension* extension_;
321 : DeclarationScope* script_scope_;
322 : uintptr_t stack_limit_;
323 : uint64_t hash_seed_;
324 : FunctionKind function_kind_;
325 : int script_id_;
326 : int start_position_;
327 : int end_position_;
328 : int parameters_end_pos_;
329 : int function_literal_id_;
330 : int max_function_literal_id_;
331 :
332 : // TODO(titzer): Move handles out of ParseInfo.
333 : Handle<Script> script_;
334 : MaybeHandle<ScopeInfo> maybe_outer_scope_info_;
335 :
336 : //----------- Inputs+Outputs of parsing and scope analysis -----------------
337 : std::unique_ptr<Utf16CharacterStream> character_stream_;
338 : std::unique_ptr<ConsumedPreparseData> consumed_preparse_data_;
339 : std::unique_ptr<AstValueFactory> ast_value_factory_;
340 : const class AstStringConstants* ast_string_constants_;
341 : const AstRawString* function_name_;
342 : RuntimeCallStats* runtime_call_stats_;
343 : Logger* logger_;
344 : SourceRangeMap* source_range_map_; // Used when block coverage is enabled.
345 : std::unique_ptr<ParallelTasks> parallel_tasks_;
346 :
347 : //----------- Output of parsing and scope analysis ------------------------
348 : FunctionLiteral* literal_;
349 : PendingCompilationErrorHandler pending_error_handler_;
350 :
351 3328621 : void SetFlag(Flag f) { flags_ |= f; }
352 47011466 : void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; }
353 84259185 : bool GetFlag(Flag f) const { return (flags_ & f) != 0; }
354 : };
355 :
356 : } // namespace internal
357 : } // namespace v8
358 :
359 : #endif // V8_PARSING_PARSE_INFO_H_
|