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 : #include "src/parsing/parse-info.h"
6 :
7 : #include "src/ast/ast-source-ranges.h"
8 : #include "src/ast/ast-value-factory.h"
9 : #include "src/ast/ast.h"
10 : #include "src/base/template-utils.h"
11 : #include "src/compiler-dispatcher/compiler-dispatcher.h"
12 : #include "src/counters.h"
13 : #include "src/hash-seed-inl.h"
14 : #include "src/heap/heap-inl.h"
15 : #include "src/log.h"
16 : #include "src/objects-inl.h"
17 : #include "src/objects/scope-info.h"
18 : #include "src/zone/zone.h"
19 :
20 : namespace v8 {
21 : namespace internal {
22 :
23 2437702 : ParseInfo::ParseInfo(AccountingAllocator* zone_allocator)
24 : : zone_(base::make_unique<Zone>(zone_allocator, ZONE_NAME)),
25 : flags_(0),
26 : extension_(nullptr),
27 : script_scope_(nullptr),
28 : stack_limit_(0),
29 : hash_seed_(0),
30 : function_kind_(FunctionKind::kNormalFunction),
31 : script_id_(-1),
32 : start_position_(0),
33 : end_position_(0),
34 : parameters_end_pos_(kNoSourcePosition),
35 : function_literal_id_(FunctionLiteral::kIdTypeInvalid),
36 : max_function_literal_id_(FunctionLiteral::kIdTypeInvalid),
37 : character_stream_(nullptr),
38 : ast_value_factory_(nullptr),
39 : ast_string_constants_(nullptr),
40 : function_name_(nullptr),
41 : runtime_call_stats_(nullptr),
42 : source_range_map_(nullptr),
43 7313110 : literal_(nullptr) {}
44 :
45 9750599 : ParseInfo::ParseInfo(Isolate* isolate, AccountingAllocator* zone_allocator)
46 2437623 : : ParseInfo(zone_allocator) {
47 2437624 : set_hash_seed(HashSeed(isolate));
48 2437626 : set_stack_limit(isolate->stack_guard()->real_climit());
49 2437626 : set_runtime_call_stats(isolate->counters()->runtime_call_stats());
50 : set_logger(isolate->logger());
51 : set_ast_string_constants(isolate->ast_string_constants());
52 2437646 : set_collect_source_positions(!FLAG_enable_lazy_source_positions ||
53 20 : isolate->NeedsDetailedOptimizedCodeLineInfo());
54 2437626 : if (!isolate->is_best_effort_code_coverage()) set_coverage_enabled();
55 2437626 : if (isolate->is_block_code_coverage()) set_block_coverage_enabled();
56 2437626 : if (isolate->is_collecting_type_profile()) set_collect_type_profile();
57 2437626 : if (isolate->compiler_dispatcher()->IsEnabled()) {
58 98 : parallel_tasks_.reset(new ParallelTasks(isolate->compiler_dispatcher()));
59 : }
60 2437627 : set_might_always_opt(FLAG_always_opt || FLAG_prepare_always_opt);
61 2437627 : set_allow_lazy_compile(FLAG_lazy);
62 2437627 : set_allow_natives_syntax(FLAG_allow_natives_syntax);
63 2437627 : set_allow_harmony_public_fields(FLAG_harmony_public_fields);
64 2437627 : set_allow_harmony_static_fields(FLAG_harmony_static_fields);
65 2437627 : set_allow_harmony_dynamic_import(FLAG_harmony_dynamic_import);
66 2437627 : set_allow_harmony_import_meta(FLAG_harmony_import_meta);
67 2437627 : set_allow_harmony_numeric_separator(FLAG_harmony_numeric_separator);
68 2437627 : set_allow_harmony_private_fields(FLAG_harmony_private_fields);
69 2437627 : set_allow_harmony_private_methods(FLAG_harmony_private_methods);
70 2437627 : }
71 :
72 2242708 : ParseInfo::ParseInfo(Isolate* isolate)
73 1121354 : : ParseInfo(isolate, isolate->allocator()) {
74 1121354 : script_id_ = isolate->heap()->NextScriptId();
75 1121354 : LOG(isolate, ScriptEvent(Logger::ScriptEventType::kReserveId, script_id_));
76 1121354 : }
77 :
78 : template <typename T>
79 708630 : void ParseInfo::SetFunctionInfo(T function) {
80 1416781 : set_is_named_expression(function->is_named_expression());
81 1416859 : set_language_mode(function->language_mode());
82 80 : set_function_kind(function->kind());
83 1416782 : set_declaration(function->is_declaration());
84 1416782 : set_requires_instance_members_initializer(
85 : function->requires_instance_members_initializer());
86 1416782 : set_toplevel(function->is_toplevel());
87 : set_is_oneshot_iife(function->is_oneshot_iife());
88 1416781 : set_wrapped_as_function(function->is_wrapped());
89 708471 : }
90 :
91 708388 : ParseInfo::ParseInfo(Isolate* isolate, Handle<SharedFunctionInfo> shared)
92 708388 : : ParseInfo(isolate, isolate->allocator()) {
93 : // Do not support re-parsing top-level function of a wrapped script.
94 : // TODO(yangguo): consider whether we need a top-level function in a
95 : // wrapped script at all.
96 : DCHECK_IMPLIES(is_toplevel(), !Script::cast(shared->script())->is_wrapped());
97 :
98 : set_allow_lazy_parsing(true);
99 1416779 : set_asm_wasm_broken(shared->is_asm_wasm_broken());
100 :
101 1416779 : set_start_position(shared->StartPosition());
102 1416780 : set_end_position(shared->EndPosition());
103 708391 : function_literal_id_ = shared->FunctionLiteralId(isolate);
104 708391 : SetFunctionInfo(shared);
105 :
106 1416781 : Handle<Script> script(Script::cast(shared->script()), isolate);
107 708390 : set_script(script);
108 :
109 708391 : if (shared->HasOuterScopeInfo()) {
110 866582 : set_outer_scope_info(handle(shared->GetOuterScopeInfo(), isolate));
111 : }
112 :
113 : // CollectTypeProfile uses its own feedback slots. If we have existing
114 : // FeedbackMetadata, we can only collect type profile if the feedback vector
115 : // has the appropriate slots.
116 : set_collect_type_profile(
117 708501 : isolate->is_collecting_type_profile() &&
118 708501 : (shared->HasFeedbackMetadata()
119 708389 : ? shared->feedback_metadata()->HasTypeProfileSlot()
120 1416778 : : script->IsUserJavaScript()));
121 708389 : }
122 :
123 607882 : ParseInfo::ParseInfo(Isolate* isolate, Handle<Script> script)
124 607882 : : ParseInfo(isolate, isolate->allocator()) {
125 607882 : SetScriptForToplevelCompile(isolate, script);
126 607882 : set_collect_type_profile(isolate->is_collecting_type_profile() &&
127 607882 : script->IsUserJavaScript());
128 607882 : }
129 :
130 : // static
131 80 : std::unique_ptr<ParseInfo> ParseInfo::FromParent(
132 240 : const ParseInfo* outer_parse_info, AccountingAllocator* zone_allocator,
133 80 : const FunctionLiteral* literal, const AstRawString* function_name) {
134 : std::unique_ptr<ParseInfo> result =
135 80 : base::make_unique<ParseInfo>(zone_allocator);
136 :
137 : // Replicate shared state of the outer_parse_info.
138 80 : result->flags_ = outer_parse_info->flags_;
139 80 : result->script_id_ = outer_parse_info->script_id_;
140 : result->set_logger(outer_parse_info->logger());
141 : result->set_ast_string_constants(outer_parse_info->ast_string_constants());
142 : result->set_hash_seed(outer_parse_info->hash_seed());
143 :
144 : DCHECK_EQ(outer_parse_info->parameters_end_pos(), kNoSourcePosition);
145 : DCHECK_NULL(outer_parse_info->extension());
146 : DCHECK(outer_parse_info->maybe_outer_scope_info().is_null());
147 :
148 : // Clone the function_name AstRawString into the ParseInfo's own
149 : // AstValueFactory.
150 : const AstRawString* cloned_function_name =
151 : result->GetOrCreateAstValueFactory()->CloneFromOtherFactory(
152 80 : function_name);
153 :
154 : // Setup function specific details.
155 : DCHECK(!literal->is_toplevel());
156 : result->set_function_name(cloned_function_name);
157 80 : result->set_start_position(literal->start_position());
158 80 : result->set_end_position(literal->end_position());
159 : result->set_function_literal_id(literal->function_literal_id());
160 80 : result->SetFunctionInfo(literal);
161 :
162 80 : return result;
163 : }
164 :
165 : ParseInfo::~ParseInfo() = default;
166 :
167 2420671 : DeclarationScope* ParseInfo::scope() const { return literal()->scope(); }
168 :
169 1108494 : Handle<Script> ParseInfo::CreateScript(Isolate* isolate, Handle<String> source,
170 : ScriptOriginOptions origin_options,
171 : NativesFlag natives) {
172 : // Create a script object describing the script to be compiled.
173 : Handle<Script> script;
174 1108494 : if (script_id_ == -1) {
175 0 : script = isolate->factory()->NewScript(source);
176 : } else {
177 1108494 : script = isolate->factory()->NewScriptWithId(source, script_id_);
178 : }
179 1108493 : if (isolate->NeedsSourcePositionsForProfiling()) {
180 25995 : Script::InitLineEnds(script);
181 : }
182 1108494 : switch (natives) {
183 : case NATIVES_CODE:
184 : script->set_type(Script::TYPE_NATIVE);
185 0 : break;
186 : case EXTENSION_CODE:
187 : script->set_type(Script::TYPE_EXTENSION);
188 3320 : break;
189 : case INSPECTOR_CODE:
190 : script->set_type(Script::TYPE_INSPECTOR);
191 261 : break;
192 : case NOT_NATIVES_CODE:
193 : break;
194 : }
195 1108494 : script->set_origin_options(origin_options);
196 :
197 1108494 : SetScriptForToplevelCompile(isolate, script);
198 1108492 : return script;
199 : }
200 :
201 4875379 : AstValueFactory* ParseInfo::GetOrCreateAstValueFactory() {
202 2437727 : if (!ast_value_factory_.get()) {
203 : ast_value_factory_.reset(
204 2437652 : new AstValueFactory(zone(), ast_string_constants(), hash_seed()));
205 : }
206 2437737 : return ast_value_factory();
207 : }
208 :
209 668 : void ParseInfo::AllocateSourceRangeMap() {
210 : DCHECK(block_coverage_enabled());
211 : set_source_range_map(new (zone()) SourceRangeMap(zone()));
212 668 : }
213 :
214 8007178 : void ParseInfo::ResetCharacterStream() { character_stream_.reset(); }
215 :
216 2437661 : void ParseInfo::set_character_stream(
217 : std::unique_ptr<Utf16CharacterStream> character_stream) {
218 : DCHECK_NULL(character_stream_);
219 : character_stream_.swap(character_stream);
220 2437661 : }
221 :
222 1716376 : void ParseInfo::SetScriptForToplevelCompile(Isolate* isolate,
223 : Handle<Script> script) {
224 1716376 : set_script(script);
225 : set_allow_lazy_parsing();
226 : set_toplevel();
227 1716388 : set_collect_type_profile(isolate->is_collecting_type_profile() &&
228 1716388 : script->IsUserJavaScript());
229 3432750 : set_wrapped_as_function(script->is_wrapped());
230 1716374 : }
231 :
232 2424835 : void ParseInfo::set_script(Handle<Script> script) {
233 2424835 : script_ = script;
234 : DCHECK(script_id_ == -1 || script_id_ == script->id());
235 2424835 : script_id_ = script->id();
236 :
237 : set_native(script->type() == Script::TYPE_NATIVE);
238 4849670 : set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
239 4849671 : set_module(script->origin_options().IsModule());
240 : DCHECK(!(is_eval() && is_module()));
241 :
242 2425504 : if (block_coverage_enabled() && script->IsUserJavaScript()) {
243 668 : AllocateSourceRangeMap();
244 : }
245 2424836 : }
246 :
247 55 : void ParseInfo::ParallelTasks::Enqueue(ParseInfo* outer_parse_info,
248 : const AstRawString* function_name,
249 : FunctionLiteral* literal) {
250 : base::Optional<CompilerDispatcher::JobId> job_id =
251 55 : dispatcher_->Enqueue(outer_parse_info, function_name, literal);
252 55 : if (job_id) {
253 110 : enqueued_jobs_.emplace_front(std::make_pair(literal, *job_id));
254 : }
255 55 : }
256 :
257 : } // namespace internal
258 178779 : } // namespace v8
|