5 #include "src/parsing/parse-info.h" 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/heap/heap-inl.h" 14 #include "src/objects-inl.h" 15 #include "src/objects/scope-info.h" 16 #include "src/zone/zone.h" 21 ParseInfo::ParseInfo(AccountingAllocator* zone_allocator)
22 : zone_(base::make_unique<Zone>(zone_allocator, ZONE_NAME)),
25 script_scope_(nullptr),
28 function_kind_(FunctionKind::kNormalFunction),
32 parameters_end_pos_(kNoSourcePosition),
33 function_literal_id_(FunctionLiteral::kIdTypeInvalid),
34 max_function_literal_id_(FunctionLiteral::kIdTypeInvalid),
35 character_stream_(nullptr),
36 ast_value_factory_(nullptr),
37 ast_string_constants_(nullptr),
38 function_name_(nullptr),
39 runtime_call_stats_(nullptr),
40 source_range_map_(nullptr),
43 ParseInfo::ParseInfo(Isolate* isolate, AccountingAllocator* zone_allocator)
44 : ParseInfo(zone_allocator) {
45 set_hash_seed(isolate->heap()->HashSeed());
46 set_stack_limit(isolate->stack_guard()->real_climit());
47 set_runtime_call_stats(isolate->counters()->runtime_call_stats());
48 set_logger(isolate->logger());
49 set_ast_string_constants(isolate->ast_string_constants());
50 if (isolate->is_block_code_coverage()) set_block_coverage_enabled();
51 if (isolate->is_collecting_type_profile()) set_collect_type_profile();
52 if (isolate->compiler_dispatcher()->IsEnabled()) {
53 parallel_tasks_.reset(
new ParallelTasks(isolate->compiler_dispatcher()));
57 ParseInfo::ParseInfo(Isolate* isolate)
58 : ParseInfo(isolate, isolate->allocator()) {
59 script_id_ = isolate->heap()->NextScriptId();
60 LOG(isolate, ScriptEvent(Logger::ScriptEventType::kReserveId, script_id_));
64 void ParseInfo::SetFunctionInfo(T
function) {
65 set_is_named_expression(function->is_named_expression());
66 set_language_mode(function->language_mode());
67 set_function_kind(function->kind());
68 set_declaration(function->is_declaration());
69 set_requires_instance_members_initializer(
70 function->requires_instance_members_initializer());
71 set_toplevel(function->is_toplevel());
72 set_wrapped_as_function(function->is_wrapped());
75 ParseInfo::ParseInfo(Isolate* isolate, Handle<SharedFunctionInfo> shared)
76 : ParseInfo(isolate, isolate->allocator()) {
80 DCHECK_IMPLIES(is_toplevel(), !Script::cast(shared->script())->is_wrapped());
82 set_allow_lazy_parsing(
true);
83 set_asm_wasm_broken(shared->is_asm_wasm_broken());
85 set_start_position(shared->StartPosition());
86 set_end_position(shared->EndPosition());
87 function_literal_id_ = shared->FunctionLiteralId(isolate);
88 SetFunctionInfo(shared);
90 Handle<Script> script(Script::cast(shared->script()), isolate);
93 if (shared->HasOuterScopeInfo()) {
94 set_outer_scope_info(handle(shared->GetOuterScopeInfo(), isolate));
100 set_collect_type_profile(
101 isolate->is_collecting_type_profile() &&
102 (shared->HasFeedbackMetadata()
103 ? shared->feedback_metadata()->HasTypeProfileSlot()
104 : script->IsUserJavaScript()));
107 ParseInfo::ParseInfo(Isolate* isolate, Handle<Script> script)
108 : ParseInfo(isolate, isolate->allocator()) {
109 SetScriptForToplevelCompile(isolate, script);
110 set_collect_type_profile(isolate->is_collecting_type_profile() &&
111 script->IsUserJavaScript());
115 std::unique_ptr<ParseInfo> ParseInfo::FromParent(
116 const ParseInfo* outer_parse_info, AccountingAllocator* zone_allocator,
117 const FunctionLiteral* literal,
const AstRawString* function_name) {
118 std::unique_ptr<ParseInfo> result =
119 base::make_unique<ParseInfo>(zone_allocator);
122 result->flags_ = outer_parse_info->flags_;
123 result->script_id_ = outer_parse_info->script_id_;
124 result->set_logger(outer_parse_info->logger());
125 result->set_ast_string_constants(outer_parse_info->ast_string_constants());
126 result->set_hash_seed(outer_parse_info->hash_seed());
128 DCHECK_EQ(outer_parse_info->parameters_end_pos(), kNoSourcePosition);
129 DCHECK_NULL(outer_parse_info->extension());
130 DCHECK(outer_parse_info->maybe_outer_scope_info().is_null());
134 const AstRawString* cloned_function_name =
135 result->GetOrCreateAstValueFactory()->CloneFromOtherFactory(
139 DCHECK(!literal->is_toplevel());
140 result->set_function_name(cloned_function_name);
141 result->set_start_position(literal->start_position());
142 result->set_end_position(literal->end_position());
143 result->set_function_literal_id(literal->function_literal_id());
144 result->SetFunctionInfo(literal);
149 ParseInfo::~ParseInfo() =
default;
151 DeclarationScope* ParseInfo::scope()
const {
return literal()->scope(); }
153 Handle<Script> ParseInfo::CreateScript(Isolate* isolate, Handle<String> source,
154 ScriptOriginOptions origin_options,
155 NativesFlag natives) {
157 Handle<Script> script;
158 if (script_id_ == -1) {
159 script = isolate->factory()->NewScript(source);
161 script = isolate->factory()->NewScriptWithId(source, script_id_);
163 if (isolate->NeedsSourcePositionsForProfiling()) {
164 Script::InitLineEnds(script);
168 script->set_type(Script::TYPE_NATIVE);
171 script->set_type(Script::TYPE_EXTENSION);
174 script->set_type(Script::TYPE_INSPECTOR);
176 case NOT_NATIVES_CODE:
179 script->set_origin_options(origin_options);
181 SetScriptForToplevelCompile(isolate, script);
185 AstValueFactory* ParseInfo::GetOrCreateAstValueFactory() {
186 if (!ast_value_factory_.get()) {
187 ast_value_factory_.reset(
188 new AstValueFactory(zone(), ast_string_constants(), hash_seed()));
190 return ast_value_factory();
193 void ParseInfo::AllocateSourceRangeMap() {
194 DCHECK(block_coverage_enabled());
195 set_source_range_map(
new (zone()) SourceRangeMap(zone()));
198 void ParseInfo::ResetCharacterStream() { character_stream_.reset(); }
200 void ParseInfo::set_character_stream(
201 std::unique_ptr<Utf16CharacterStream> character_stream) {
202 DCHECK_NULL(character_stream_);
203 character_stream_.swap(character_stream);
206 void ParseInfo::SetScriptForToplevelCompile(Isolate* isolate,
207 Handle<Script> script) {
209 set_allow_lazy_parsing();
211 set_collect_type_profile(isolate->is_collecting_type_profile() &&
212 script->IsUserJavaScript());
213 set_wrapped_as_function(script->is_wrapped());
216 void ParseInfo::set_script(Handle<Script> script) {
218 DCHECK(script_id_ == -1 || script_id_ == script->id());
219 script_id_ = script->id();
221 set_native(script->type() == Script::TYPE_NATIVE);
222 set_eval(script->compilation_type() == Script::COMPILATION_TYPE_EVAL);
223 set_module(script->origin_options().IsModule());
224 DCHECK(!(is_eval() && is_module()));
226 if (block_coverage_enabled() && script->IsUserJavaScript()) {
227 AllocateSourceRangeMap();
231 void ParseInfo::ParallelTasks::Enqueue(ParseInfo* outer_parse_info,
232 const AstRawString* function_name,
233 FunctionLiteral* literal) {
234 base::Optional<CompilerDispatcher::JobId> job_id =
235 dispatcher_->Enqueue(outer_parse_info, function_name, literal);
237 enqueued_jobs_.emplace_front(std::make_pair(literal, *job_id));