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/parsing/preparsed-scope-data.h"
16 :
17 : namespace v8 {
18 :
19 : class Extension;
20 :
21 : namespace internal {
22 :
23 : class AccountingAllocator;
24 : class AstRawString;
25 : class AstStringConstants;
26 : class AstValueFactory;
27 : class DeclarationScope;
28 : class FunctionLiteral;
29 : class RuntimeCallStats;
30 : class ScriptData;
31 : class SourceRangeMap;
32 : class UnicodeCache;
33 : class Utf16CharacterStream;
34 : class Zone;
35 :
36 : // A container for the inputs, configuration options, and outputs of parsing.
37 : class V8_EXPORT_PRIVATE ParseInfo {
38 : public:
39 : explicit ParseInfo(AccountingAllocator* zone_allocator);
40 : ParseInfo(Handle<Script> script);
41 : ParseInfo(Handle<SharedFunctionInfo> shared);
42 :
43 : ~ParseInfo();
44 :
45 : void InitFromIsolate(Isolate* isolate);
46 :
47 : static ParseInfo* AllocateWithoutScript(Handle<SharedFunctionInfo> shared);
48 :
49 : // Either returns the ast-value-factory associcated with this ParseInfo, or
50 : // creates and returns a new factory if none exists.
51 : AstValueFactory* GetOrCreateAstValueFactory();
52 :
53 10400791 : Zone* zone() const { return zone_.get(); }
54 :
55 : // Sets this parse info to share the same zone as |other|
56 : void ShareZone(ParseInfo* other);
57 :
58 : // Sets this parse info to share the same ast value factory as |other|
59 : void ShareAstValueFactory(ParseInfo* other);
60 :
61 : // Convenience accessor methods for flags.
62 : #define FLAG_ACCESSOR(flag, getter, setter) \
63 : bool getter() const { return GetFlag(flag); } \
64 : void setter() { SetFlag(flag); } \
65 : void setter(bool val) { SetFlag(flag, val); }
66 :
67 2273891 : FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel)
68 2347659 : FLAG_ACCESSOR(kEager, is_eager, set_eager)
69 7963452 : FLAG_ACCESSOR(kEval, is_eval, set_eval)
70 2460255 : FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode)
71 4121505 : FLAG_ACCESSOR(kNative, is_native, set_native)
72 4026461 : FLAG_ACCESSOR(kModule, is_module, set_module)
73 2347658 : FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing)
74 365964 : FLAG_ACCESSOR(kIsNamedExpression, is_named_expression,
75 : set_is_named_expression)
76 2477376 : FLAG_ACCESSOR(kSerializing, will_serialize, set_will_serialize)
77 : FLAG_ACCESSOR(kLazyCompile, lazy_compile, set_lazy_compile)
78 2477376 : FLAG_ACCESSOR(kCollectTypeProfile, collect_type_profile,
79 : set_collect_type_profile)
80 2134847 : FLAG_ACCESSOR(kIsAsmWasmBroken, is_asm_wasm_broken, set_asm_wasm_broken)
81 : FLAG_ACCESSOR(kBlockCoverageEnabled, block_coverage_enabled,
82 : set_block_coverage_enabled)
83 : #undef FLAG_ACCESSOR
84 :
85 : void set_parse_restriction(ParseRestriction restriction) {
86 : SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION);
87 : }
88 :
89 1362477 : ParseRestriction parse_restriction() const {
90 : return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL
91 1362477 : : NO_PARSE_RESTRICTION;
92 : }
93 :
94 : Utf16CharacterStream* character_stream() const {
95 : return character_stream_.get();
96 : }
97 : void set_character_stream(
98 : std::unique_ptr<Utf16CharacterStream> character_stream);
99 : void ResetCharacterStream();
100 :
101 : v8::Extension* extension() const { return extension_; }
102 145038 : void set_extension(v8::Extension* extension) { extension_ = extension; }
103 :
104 : ScriptData** cached_data() const { return cached_data_; }
105 610 : void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; }
106 :
107 : ConsumedPreParsedScopeData* consumed_preparsed_scope_data() {
108 : return &consumed_preparsed_scope_data_;
109 : }
110 :
111 : ScriptCompiler::CompileOptions compile_options() const {
112 : return compile_options_;
113 : }
114 : void set_compile_options(ScriptCompiler::CompileOptions compile_options) {
115 145178 : compile_options_ = compile_options;
116 : }
117 :
118 : DeclarationScope* script_scope() const { return script_scope_; }
119 : void set_script_scope(DeclarationScope* script_scope) {
120 3260776 : script_scope_ = script_scope;
121 : }
122 :
123 : AstValueFactory* ast_value_factory() const {
124 : DCHECK(ast_value_factory_.get());
125 9745066 : return ast_value_factory_.get();
126 : }
127 :
128 : const AstRawString* function_name() const { return function_name_; }
129 : void set_function_name(const AstRawString* function_name) {
130 669553 : function_name_ = function_name;
131 : }
132 :
133 : FunctionLiteral* literal() const { return literal_; }
134 2348007 : void set_literal(FunctionLiteral* literal) { literal_ = literal; }
135 :
136 : DeclarationScope* scope() const;
137 :
138 : UnicodeCache* unicode_cache() const { return unicode_cache_; }
139 : void set_unicode_cache(UnicodeCache* unicode_cache) {
140 2806759 : unicode_cache_ = unicode_cache;
141 : }
142 :
143 : uintptr_t stack_limit() const { return stack_limit_; }
144 2806593 : void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }
145 :
146 : uint32_t hash_seed() const { return hash_seed_; }
147 2806622 : void set_hash_seed(uint32_t hash_seed) { hash_seed_ = hash_seed; }
148 :
149 : int compiler_hints() const { return compiler_hints_; }
150 : void set_compiler_hints(int compiler_hints) {
151 1130015 : compiler_hints_ = compiler_hints;
152 : }
153 :
154 : int start_position() const { return start_position_; }
155 : void set_start_position(int start_position) {
156 1130015 : start_position_ = start_position;
157 : }
158 :
159 : int end_position() const { return end_position_; }
160 1130015 : void set_end_position(int end_position) { end_position_ = end_position; }
161 :
162 : int parameters_end_pos() const { return parameters_end_pos_; }
163 : void set_parameters_end_pos(int parameters_end_pos) {
164 971748 : parameters_end_pos_ = parameters_end_pos;
165 : }
166 :
167 : int function_literal_id() const { return function_literal_id_; }
168 : void set_function_literal_id(int function_literal_id) {
169 29 : function_literal_id_ = function_literal_id;
170 : }
171 :
172 : int max_function_literal_id() const { return max_function_literal_id_; }
173 : void set_max_function_literal_id(int max_function_literal_id) {
174 1678456 : max_function_literal_id_ = max_function_literal_id;
175 : }
176 :
177 : const AstStringConstants* ast_string_constants() const {
178 : return ast_string_constants_;
179 : }
180 : void set_ast_string_constants(
181 : const AstStringConstants* ast_string_constants) {
182 2806593 : ast_string_constants_ = ast_string_constants;
183 : }
184 :
185 : RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
186 : void set_runtime_call_stats(RuntimeCallStats* runtime_call_stats) {
187 2806725 : runtime_call_stats_ = runtime_call_stats;
188 : }
189 :
190 : void AllocateSourceRangeMap();
191 : SourceRangeMap* source_range_map() const { return source_range_map_; }
192 : void set_source_range_map(SourceRangeMap* source_range_map) {
193 624 : source_range_map_ = source_range_map;
194 : }
195 :
196 : // Getters for individual compiler hints.
197 : bool is_declaration() const;
198 : FunctionKind function_kind() const;
199 :
200 : //--------------------------------------------------------------------------
201 : // TODO(titzer): these should not be part of ParseInfo.
202 : //--------------------------------------------------------------------------
203 : Handle<Script> script() const { return script_; }
204 : MaybeHandle<ScopeInfo> maybe_outer_scope_info() const {
205 : return maybe_outer_scope_info_;
206 : }
207 : void clear_script() { script_ = Handle<Script>::null(); }
208 : void set_outer_scope_info(Handle<ScopeInfo> outer_scope_info) {
209 1181431 : maybe_outer_scope_info_ = outer_scope_info;
210 : }
211 2806587 : void set_script(Handle<Script> script) { script_ = script; }
212 : //--------------------------------------------------------------------------
213 :
214 : LanguageMode language_mode() const {
215 : return construct_language_mode(is_strict_mode());
216 : }
217 : void set_language_mode(LanguageMode language_mode) {
218 : STATIC_ASSERT(LanguageModeSize == 2);
219 : set_strict_mode(is_strict(language_mode));
220 : }
221 :
222 439074 : void ReopenHandlesInNewHandleScope() {
223 439074 : if (!script_.is_null()) {
224 439074 : script_ = Handle<Script>(*script_);
225 : }
226 : Handle<ScopeInfo> outer_scope_info;
227 439074 : if (maybe_outer_scope_info_.ToHandle(&outer_scope_info)) {
228 169501 : maybe_outer_scope_info_ = Handle<ScopeInfo>(*outer_scope_info);
229 : }
230 439074 : }
231 :
232 : void UpdateStatisticsAfterBackgroundParse(Isolate* isolate);
233 :
234 : private:
235 : // Various configuration flags for parsing.
236 : enum Flag {
237 : // ---------- Input flags ---------------------------
238 : kToplevel = 1 << 0,
239 : kEager = 1 << 1,
240 : kEval = 1 << 2,
241 : kStrictMode = 1 << 3,
242 : kNative = 1 << 4,
243 : kParseRestriction = 1 << 5,
244 : kModule = 1 << 6,
245 : kAllowLazyParsing = 1 << 7,
246 : kIsNamedExpression = 1 << 8,
247 : kSerializing = 1 << 9,
248 : kLazyCompile = 1 << 10,
249 : kCollectTypeProfile = 1 << 11,
250 : kBlockCoverageEnabled = 1 << 12,
251 : kIsAsmWasmBroken = 1 << 13,
252 : };
253 :
254 : //------------- Inputs to parsing and scope analysis -----------------------
255 : std::shared_ptr<Zone> zone_;
256 : unsigned flags_;
257 : v8::Extension* extension_;
258 : ScriptCompiler::CompileOptions compile_options_;
259 : DeclarationScope* script_scope_;
260 : UnicodeCache* unicode_cache_;
261 : uintptr_t stack_limit_;
262 : uint32_t hash_seed_;
263 : int compiler_hints_;
264 : int start_position_;
265 : int end_position_;
266 : int parameters_end_pos_;
267 : int function_literal_id_;
268 : int max_function_literal_id_;
269 :
270 : // TODO(titzer): Move handles out of ParseInfo.
271 : Handle<Script> script_;
272 : MaybeHandle<ScopeInfo> maybe_outer_scope_info_;
273 :
274 : //----------- Inputs+Outputs of parsing and scope analysis -----------------
275 : std::unique_ptr<Utf16CharacterStream> character_stream_;
276 : ScriptData** cached_data_; // used if available, populated if requested.
277 : ConsumedPreParsedScopeData consumed_preparsed_scope_data_;
278 : std::shared_ptr<AstValueFactory> ast_value_factory_;
279 : const class AstStringConstants* ast_string_constants_;
280 : const AstRawString* function_name_;
281 : RuntimeCallStats* runtime_call_stats_;
282 : SourceRangeMap* source_range_map_; // Used when block coverage is enabled.
283 :
284 : //----------- Output of parsing and scope analysis ------------------------
285 : FunctionLiteral* literal_;
286 : std::shared_ptr<DeferredHandles> deferred_handles_;
287 :
288 3253666 : void SetFlag(Flag f) { flags_ |= f; }
289 18170491 : void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; }
290 39826722 : bool GetFlag(Flag f) const { return (flags_ & f) != 0; }
291 : };
292 :
293 : } // namespace internal
294 : } // namespace v8
295 :
296 : #endif // V8_PARSING_PARSE_INFO_H_
|