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 <memory>
9 :
10 : #include "include/v8.h"
11 : #include "src/globals.h"
12 : #include "src/handles.h"
13 : #include "src/parsing/preparsed-scope-data.h"
14 :
15 : namespace v8 {
16 :
17 : class Extension;
18 :
19 : namespace internal {
20 :
21 : class AccountingAllocator;
22 : class AstRawString;
23 : class AstStringConstants;
24 : class AstValueFactory;
25 : class DeclarationScope;
26 : class DeferredHandles;
27 : class FunctionLiteral;
28 : class RuntimeCallStats;
29 : class ScriptData;
30 : class SharedFunctionInfo;
31 : class UnicodeCache;
32 : class Utf16CharacterStream;
33 : class Zone;
34 :
35 : // A container for the inputs, configuration options, and outputs of parsing.
36 : class V8_EXPORT_PRIVATE ParseInfo {
37 : public:
38 : explicit ParseInfo(AccountingAllocator* zone_allocator);
39 : ParseInfo(Handle<Script> script);
40 : ParseInfo(Handle<SharedFunctionInfo> shared);
41 :
42 : // TODO(rmcilroy): Remove once Hydrogen no longer needs this.
43 : ParseInfo(Handle<SharedFunctionInfo> shared, std::shared_ptr<Zone> zone);
44 :
45 : ~ParseInfo();
46 :
47 : void InitFromIsolate(Isolate* isolate);
48 :
49 : static ParseInfo* AllocateWithoutScript(Handle<SharedFunctionInfo> shared);
50 :
51 12595492 : Zone* zone() const { return zone_.get(); }
52 :
53 : std::shared_ptr<Zone> zone_shared() const { return zone_; }
54 :
55 : void set_deferred_handles(std::shared_ptr<DeferredHandles> deferred_handles);
56 : void set_deferred_handles(DeferredHandles* deferred_handles);
57 : std::shared_ptr<DeferredHandles> deferred_handles() const {
58 : return deferred_handles_;
59 : }
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 2797384 : FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel)
68 6578689 : FLAG_ACCESSOR(kEval, is_eval, set_eval)
69 3832234 : FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode)
70 6020816 : FLAG_ACCESSOR(kNative, is_native, set_native)
71 2263486 : FLAG_ACCESSOR(kModule, is_module, set_module)
72 3570982 : FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing)
73 4842031 : FLAG_ACCESSOR(kAstValueFactoryOwned, ast_value_factory_owned,
74 : set_ast_value_factory_owned)
75 655180 : FLAG_ACCESSOR(kIsNamedExpression, is_named_expression,
76 : set_is_named_expression)
77 24471673 : FLAG_ACCESSOR(kDebug, is_debug, set_is_debug)
78 231778 : FLAG_ACCESSOR(kSerializing, will_serialize, set_will_serialize)
79 : FLAG_ACCESSOR(kTailCallEliminationEnabled, is_tail_call_elimination_enabled,
80 : set_tail_call_elimination_enabled)
81 :
82 : #undef FLAG_ACCESSOR
83 :
84 : void set_parse_restriction(ParseRestriction restriction) {
85 : SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION);
86 : }
87 :
88 1883852 : ParseRestriction parse_restriction() const {
89 : return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL
90 1883852 : : NO_PARSE_RESTRICTION;
91 : }
92 :
93 : ScriptCompiler::ExternalSourceStream* source_stream() const {
94 : return source_stream_;
95 : }
96 : void set_source_stream(ScriptCompiler::ExternalSourceStream* source_stream) {
97 132 : source_stream_ = source_stream;
98 : }
99 :
100 : ScriptCompiler::StreamedSource::Encoding source_stream_encoding() const {
101 : return source_stream_encoding_;
102 : }
103 : void set_source_stream_encoding(
104 : ScriptCompiler::StreamedSource::Encoding source_stream_encoding) {
105 132 : source_stream_encoding_ = source_stream_encoding;
106 : }
107 :
108 : Utf16CharacterStream* character_stream() const { return character_stream_; }
109 : void set_character_stream(Utf16CharacterStream* character_stream) {
110 58 : character_stream_ = character_stream;
111 : }
112 :
113 : v8::Extension* extension() const { return extension_; }
114 207535 : void set_extension(v8::Extension* extension) { extension_ = extension; }
115 :
116 : ScriptData** cached_data() const { return cached_data_; }
117 728 : void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; }
118 :
119 : PreParsedScopeData* preparsed_scope_data() { return &preparsed_scope_data_; }
120 :
121 : ScriptCompiler::CompileOptions compile_options() const {
122 : return compile_options_;
123 : }
124 : void set_compile_options(ScriptCompiler::CompileOptions compile_options) {
125 207667 : compile_options_ = compile_options;
126 : }
127 :
128 : DeclarationScope* script_scope() const { return script_scope_; }
129 : void set_script_scope(DeclarationScope* script_scope) {
130 5109147 : script_scope_ = script_scope;
131 : }
132 :
133 : DeclarationScope* asm_function_scope() const { return asm_function_scope_; }
134 : void set_asm_function_scope(DeclarationScope* scope) {
135 11262 : asm_function_scope_ = scope;
136 : }
137 :
138 : AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
139 : void set_ast_value_factory(AstValueFactory* ast_value_factory) {
140 4265784 : ast_value_factory_ = ast_value_factory;
141 : }
142 :
143 : const AstRawString* function_name() const { return function_name_; }
144 : void set_function_name(const AstRawString* function_name) {
145 1304246 : function_name_ = function_name;
146 : }
147 :
148 : FunctionLiteral* literal() const { return literal_; }
149 4269876 : void set_literal(FunctionLiteral* literal) { literal_ = literal; }
150 :
151 : DeclarationScope* scope() const;
152 :
153 : UnicodeCache* unicode_cache() const { return unicode_cache_; }
154 : void set_unicode_cache(UnicodeCache* unicode_cache) {
155 4842248 : unicode_cache_ = unicode_cache;
156 : }
157 :
158 : uintptr_t stack_limit() const { return stack_limit_; }
159 : void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }
160 :
161 : uint32_t hash_seed() const { return hash_seed_; }
162 4842089 : void set_hash_seed(uint32_t hash_seed) { hash_seed_ = hash_seed; }
163 :
164 : int compiler_hints() const { return compiler_hints_; }
165 : void set_compiler_hints(int compiler_hints) {
166 1989931 : compiler_hints_ = compiler_hints;
167 : }
168 :
169 : int start_position() const { return start_position_; }
170 : void set_start_position(int start_position) {
171 1989931 : start_position_ = start_position;
172 : }
173 :
174 : int end_position() const { return end_position_; }
175 1989931 : void set_end_position(int end_position) { end_position_ = end_position; }
176 :
177 : int parameters_end_pos() const { return parameters_end_pos_; }
178 : void set_parameters_end_pos(int parameters_end_pos) {
179 1294323 : parameters_end_pos_ = parameters_end_pos;
180 : }
181 :
182 : int function_literal_id() const { return function_literal_id_; }
183 : void set_function_literal_id(int function_literal_id) {
184 2054773 : function_literal_id_ = function_literal_id;
185 : }
186 :
187 : int max_function_literal_id() const { return max_function_literal_id_; }
188 : void set_max_function_literal_id(int max_function_literal_id) {
189 2263486 : max_function_literal_id_ = max_function_literal_id;
190 : }
191 :
192 : const AstStringConstants* ast_string_constants() const {
193 : return ast_string_constants_;
194 : }
195 : void set_ast_string_constants(
196 : const AstStringConstants* ast_string_constants) {
197 4842061 : ast_string_constants_ = ast_string_constants;
198 : }
199 :
200 : RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
201 : void set_runtime_call_stats(RuntimeCallStats* runtime_call_stats) {
202 4842058 : runtime_call_stats_ = runtime_call_stats;
203 : }
204 :
205 : // Getters for individual compiler hints.
206 : bool is_declaration() const;
207 : FunctionKind function_kind() const;
208 :
209 : //--------------------------------------------------------------------------
210 : // TODO(titzer): these should not be part of ParseInfo.
211 : //--------------------------------------------------------------------------
212 : Handle<SharedFunctionInfo> shared_info() const { return shared_; }
213 : Handle<Script> script() const { return script_; }
214 : MaybeHandle<ScopeInfo> maybe_outer_scope_info() const {
215 6444150 : return maybe_outer_scope_info_;
216 : }
217 : void clear_script() { script_ = Handle<Script>::null(); }
218 4044669 : void set_shared_info(Handle<SharedFunctionInfo> shared) { shared_ = shared; }
219 : void set_outer_scope_info(Handle<ScopeInfo> outer_scope_info) {
220 2009251 : maybe_outer_scope_info_ = outer_scope_info;
221 : }
222 4842056 : void set_script(Handle<Script> script) { script_ = script; }
223 : //--------------------------------------------------------------------------
224 :
225 : LanguageMode language_mode() const {
226 : return construct_language_mode(is_strict_mode());
227 : }
228 : void set_language_mode(LanguageMode language_mode) {
229 : STATIC_ASSERT(LANGUAGE_END == 2);
230 : set_strict_mode(is_strict(language_mode));
231 : }
232 :
233 753104 : void ReopenHandlesInNewHandleScope() {
234 753104 : if (!script_.is_null()) {
235 753104 : script_ = Handle<Script>(*script_);
236 : }
237 753104 : if (!shared_.is_null()) {
238 752951 : shared_ = Handle<SharedFunctionInfo>(*shared_);
239 : }
240 : Handle<ScopeInfo> outer_scope_info;
241 753104 : if (maybe_outer_scope_info_.ToHandle(&outer_scope_info)) {
242 392312 : maybe_outer_scope_info_ = Handle<ScopeInfo>(*outer_scope_info);
243 : }
244 753104 : }
245 :
246 : #ifdef DEBUG
247 : bool script_is_native() const;
248 : #endif // DEBUG
249 :
250 : private:
251 : // Various configuration flags for parsing.
252 : enum Flag {
253 : // ---------- Input flags ---------------------------
254 : kToplevel = 1 << 0,
255 : kLazy = 1 << 1,
256 : kEval = 1 << 2,
257 : kStrictMode = 1 << 3,
258 : kNative = 1 << 4,
259 : kParseRestriction = 1 << 5,
260 : kModule = 1 << 6,
261 : kAllowLazyParsing = 1 << 7,
262 : kIsNamedExpression = 1 << 8,
263 : kDebug = 1 << 9,
264 : kSerializing = 1 << 10,
265 : kTailCallEliminationEnabled = 1 << 11,
266 : kAstValueFactoryOwned = 1 << 12,
267 : };
268 :
269 : //------------- Inputs to parsing and scope analysis -----------------------
270 : std::shared_ptr<Zone> zone_;
271 : unsigned flags_;
272 : ScriptCompiler::ExternalSourceStream* source_stream_;
273 : ScriptCompiler::StreamedSource::Encoding source_stream_encoding_;
274 : Utf16CharacterStream* character_stream_;
275 : v8::Extension* extension_;
276 : ScriptCompiler::CompileOptions compile_options_;
277 : DeclarationScope* script_scope_;
278 : DeclarationScope* asm_function_scope_;
279 : UnicodeCache* unicode_cache_;
280 : uintptr_t stack_limit_;
281 : uint32_t hash_seed_;
282 : int compiler_hints_;
283 : int start_position_;
284 : int end_position_;
285 : int parameters_end_pos_;
286 : int function_literal_id_;
287 : int max_function_literal_id_;
288 :
289 : // TODO(titzer): Move handles out of ParseInfo.
290 : Handle<SharedFunctionInfo> shared_;
291 : Handle<Script> script_;
292 : MaybeHandle<ScopeInfo> maybe_outer_scope_info_;
293 :
294 : //----------- Inputs+Outputs of parsing and scope analysis -----------------
295 : ScriptData** cached_data_; // used if available, populated if requested.
296 : PreParsedScopeData preparsed_scope_data_;
297 : AstValueFactory* ast_value_factory_; // used if available, otherwise new.
298 : const class AstStringConstants* ast_string_constants_;
299 : const AstRawString* function_name_;
300 : RuntimeCallStats* runtime_call_stats_;
301 :
302 : //----------- Output of parsing and scope analysis ------------------------
303 : FunctionLiteral* literal_;
304 : std::shared_ptr<DeferredHandles> deferred_handles_;
305 :
306 7595442 : void SetFlag(Flag f) { flags_ |= f; }
307 33089263 : void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; }
308 62941383 : bool GetFlag(Flag f) const { return (flags_ & f) != 0; }
309 : };
310 :
311 : } // namespace internal
312 : } // namespace v8
313 :
314 : #endif // V8_PARSING_PARSE_INFO_H_
|