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