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 "include/v8.h" 9 #include "src/globals.h" 10 #include "src/handles.h" 11 12 namespace v8 { 13 14 class Extension; 15 16 namespace internal { 17 18 class AstRawString; 19 class AstValueFactory; 20 class DeclarationScope; 21 class FunctionLiteral; 22 class ScriptData; 23 class SharedFunctionInfo; 24 class UnicodeCache; 25 class Utf16CharacterStream; 26 class Zone; 27 28 // A container for the inputs, configuration options, and outputs of parsing. 29 class V8_EXPORT_PRIVATE ParseInfo { 30 public: 31 explicit ParseInfo(Zone* zone); 32 ParseInfo(Zone* zone, Handle<Script> script); 33 ParseInfo(Zone* zone, Handle<SharedFunctionInfo> shared); 34 35 ~ParseInfo(); 36 37 Zone* zone() const { return zone_; } 38 39 // Convenience accessor methods for flags. 40 #define FLAG_ACCESSOR(flag, getter, setter) \ 41 bool getter() const { return GetFlag(flag); } \ 42 void setter() { SetFlag(flag); } \ 43 void setter(bool val) { SetFlag(flag, val); } 44 45 FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel) 46 FLAG_ACCESSOR(kEval, is_eval, set_eval) 47 FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode) 48 FLAG_ACCESSOR(kNative, is_native, set_native) 49 FLAG_ACCESSOR(kModule, is_module, set_module) 50 FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing) 51 FLAG_ACCESSOR(kAstValueFactoryOwned, ast_value_factory_owned, 52 set_ast_value_factory_owned) 53 FLAG_ACCESSOR(kIsNamedExpression, is_named_expression, 54 set_is_named_expression) 55 FLAG_ACCESSOR(kCallsEval, calls_eval, set_calls_eval) 56 FLAG_ACCESSOR(kDebug, is_debug, set_is_debug) 57 FLAG_ACCESSOR(kSerializing, will_serialize, set_will_serialize) 58 59 #undef FLAG_ACCESSOR 60 61 void set_parse_restriction(ParseRestriction restriction) { 62 SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION); 63 } 64 65 ParseRestriction parse_restriction() const { 66 return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL 67 : NO_PARSE_RESTRICTION; 68 } 69 70 ScriptCompiler::ExternalSourceStream* source_stream() const { 71 return source_stream_; 72 } 73 void set_source_stream(ScriptCompiler::ExternalSourceStream* source_stream) { 74 source_stream_ = source_stream; 75 } 76 77 ScriptCompiler::StreamedSource::Encoding source_stream_encoding() const { 78 return source_stream_encoding_; 79 } 80 void set_source_stream_encoding( 81 ScriptCompiler::StreamedSource::Encoding source_stream_encoding) { 82 source_stream_encoding_ = source_stream_encoding; 83 } 84 85 Utf16CharacterStream* character_stream() const { return character_stream_; } 86 void set_character_stream(Utf16CharacterStream* character_stream) { 87 character_stream_ = character_stream; 88 } 89 90 v8::Extension* extension() const { return extension_; } 91 void set_extension(v8::Extension* extension) { extension_ = extension; } 92 93 ScriptData** cached_data() const { return cached_data_; } 94 void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; } 95 96 ScriptCompiler::CompileOptions compile_options() const { 97 return compile_options_; 98 } 99 void set_compile_options(ScriptCompiler::CompileOptions compile_options) { 100 if (compile_options == ScriptCompiler::kConsumeParserCache) { 101 set_allow_lazy_parsing(); 102 } 103 compile_options_ = compile_options; 104 } 105 106 DeclarationScope* script_scope() const { return script_scope_; } 107 void set_script_scope(DeclarationScope* script_scope) { 108 script_scope_ = script_scope; 109 } 110 111 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } 112 void set_ast_value_factory(AstValueFactory* ast_value_factory) { 113 ast_value_factory_ = ast_value_factory; 114 } 115 116 const AstRawString* function_name() const { return function_name_; } 117 void set_function_name(const AstRawString* function_name) { 118 function_name_ = function_name; 119 } 120 121 FunctionLiteral* literal() const { return literal_; } 122 void set_literal(FunctionLiteral* literal) { literal_ = literal; } 123 124 DeclarationScope* scope() const; 125 126 UnicodeCache* unicode_cache() const { return unicode_cache_; } 127 void set_unicode_cache(UnicodeCache* unicode_cache) { 128 unicode_cache_ = unicode_cache; 129 } 130 131 uintptr_t stack_limit() const { return stack_limit_; } 132 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } 133 134 uint32_t hash_seed() const { return hash_seed_; } 135 void set_hash_seed(uint32_t hash_seed) { hash_seed_ = hash_seed; } 136 137 int compiler_hints() const { return compiler_hints_; } 138 void set_compiler_hints(int compiler_hints) { 139 compiler_hints_ = compiler_hints; 140 } 141 142 int start_position() const { return start_position_; } 143 void set_start_position(int start_position) { 144 start_position_ = start_position; 145 } 146 147 int end_position() const { return end_position_; } 148 void set_end_position(int end_position) { end_position_ = end_position; } 149 150 // Getters for individual compiler hints. 151 bool is_declaration() const; 152 bool requires_class_field_init() const; 153 bool is_class_field_initializer() const; 154 FunctionKind function_kind() const; 155 156 //-------------------------------------------------------------------------- 157 // TODO(titzer): these should not be part of ParseInfo. 158 //-------------------------------------------------------------------------- 159 Isolate* isolate() const { return isolate_; } 160 Handle<SharedFunctionInfo> shared_info() const { return shared_; } 161 Handle<Script> script() const { return script_; } 162 MaybeHandle<ScopeInfo> maybe_outer_scope_info() const { 163 return maybe_outer_scope_info_; 164 } 165 void clear_script() { script_ = Handle<Script>::null(); } 166 void set_isolate(Isolate* isolate) { isolate_ = isolate; } 167 void set_shared_info(Handle<SharedFunctionInfo> shared) { shared_ = shared; } 168 void set_outer_scope_info(Handle<ScopeInfo> outer_scope_info) { 169 maybe_outer_scope_info_ = outer_scope_info; 170 } 171 void set_script(Handle<Script> script) { script_ = script; } 172 //-------------------------------------------------------------------------- 173 174 LanguageMode language_mode() const { 175 return construct_language_mode(is_strict_mode()); 176 } 177 void set_language_mode(LanguageMode language_mode) { 178 STATIC_ASSERT(LANGUAGE_END == 2); 179 set_strict_mode(is_strict(language_mode)); 180 } 181 182 void ReopenHandlesInNewHandleScope() { 183 shared_ = Handle<SharedFunctionInfo>(*shared_); 184 script_ = Handle<Script>(*script_); 185 Handle<ScopeInfo> outer_scope_info; 186 if (maybe_outer_scope_info_.ToHandle(&outer_scope_info)) { 187 maybe_outer_scope_info_ = Handle<ScopeInfo>(*outer_scope_info); 188 } 189 } 190 191 #ifdef DEBUG 192 bool script_is_native() const; 193 #endif // DEBUG 194 195 private: 196 // Various configuration flags for parsing. 197 enum Flag { 198 // ---------- Input flags --------------------------- 199 kToplevel = 1 << 0, 200 kLazy = 1 << 1, 201 kEval = 1 << 2, 202 kStrictMode = 1 << 3, 203 kNative = 1 << 4, 204 kParseRestriction = 1 << 5, 205 kModule = 1 << 6, 206 kAllowLazyParsing = 1 << 7, 207 kIsNamedExpression = 1 << 8, 208 kCallsEval = 1 << 9, 209 kDebug = 1 << 10, 210 kSerializing = 1 << 11, 211 // ---------- Output flags -------------------------- 212 kAstValueFactoryOwned = 1 << 12 213 }; 214 215 //------------- Inputs to parsing and scope analysis ----------------------- 216 Zone* zone_; 217 unsigned flags_; 218 ScriptCompiler::ExternalSourceStream* source_stream_; 219 ScriptCompiler::StreamedSource::Encoding source_stream_encoding_; 220 Utf16CharacterStream* character_stream_; 221 v8::Extension* extension_; 222 ScriptCompiler::CompileOptions compile_options_; 223 DeclarationScope* script_scope_; 224 UnicodeCache* unicode_cache_; 225 uintptr_t stack_limit_; 226 uint32_t hash_seed_; 227 int compiler_hints_; 228 int start_position_; 229 int end_position_; 230 231 // TODO(titzer): Move handles and isolate out of ParseInfo. 232 Isolate* isolate_; 233 Handle<SharedFunctionInfo> shared_; 234 Handle<Script> script_; 235 MaybeHandle<ScopeInfo> maybe_outer_scope_info_; 236 237 //----------- Inputs+Outputs of parsing and scope analysis ----------------- 238 ScriptData** cached_data_; // used if available, populated if requested. 239 AstValueFactory* ast_value_factory_; // used if available, otherwise new. 240 const AstRawString* function_name_; 241 242 //----------- Output of parsing and scope analysis ------------------------ 243 FunctionLiteral* literal_; 244 245 void SetFlag(Flag f) { flags_ |= f; } 246 void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; } 247 bool GetFlag(Flag f) const { return (flags_ & f) != 0; } 248 }; 249 250 } // namespace internal 251 } // namespace v8 252 253 #endif // V8_PARSING_PARSE_INFO_H_ 254