parse-info.h 10.9 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_PARSING_PARSE_INFO_H_
#define V8_PARSING_PARSE_INFO_H_

8 9
#include <memory>

10 11 12
#include "include/v8.h"
#include "src/globals.h"
#include "src/handles.h"
13
#include "src/parsing/preparsed-scope-data.h"
14 15 16 17 18 19 20

namespace v8 {

class Extension;

namespace internal {

21
class AccountingAllocator;
22
class AstRawString;
23
class AstStringConstants;
24 25
class AstValueFactory;
class DeclarationScope;
26
class DeferredHandles;
27
class FunctionLiteral;
28
class RuntimeCallStats;
29 30 31 32 33 34 35
class ScriptData;
class SharedFunctionInfo;
class UnicodeCache;
class Utf16CharacterStream;
class Zone;

// A container for the inputs, configuration options, and outputs of parsing.
36
class V8_EXPORT_PRIVATE ParseInfo {
37
 public:
38 39 40 41 42 43
  explicit ParseInfo(AccountingAllocator* zone_allocator);
  ParseInfo(Handle<Script> script);
  ParseInfo(Handle<SharedFunctionInfo> shared);

  // TODO(rmcilroy): Remove once Hydrogen no longer needs this.
  ParseInfo(Handle<SharedFunctionInfo> shared, std::shared_ptr<Zone> zone);
44 45 46

  ~ParseInfo();

47 48
  void InitFromIsolate(Isolate* isolate);

49 50
  static ParseInfo* AllocateWithoutScript(Handle<SharedFunctionInfo> shared);

51 52 53
  Zone* zone() const { return zone_.get(); }

  std::shared_ptr<Zone> zone_shared() const { return zone_; }
54

55 56 57 58 59 60
  void set_deferred_handles(std::shared_ptr<DeferredHandles> deferred_handles);
  void set_deferred_handles(DeferredHandles* deferred_handles);
  std::shared_ptr<DeferredHandles> deferred_handles() const {
    return deferred_handles_;
  }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
// Convenience accessor methods for flags.
#define FLAG_ACCESSOR(flag, getter, setter)     \
  bool getter() const { return GetFlag(flag); } \
  void setter() { SetFlag(flag); }              \
  void setter(bool val) { SetFlag(flag, val); }

  FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel)
  FLAG_ACCESSOR(kEval, is_eval, set_eval)
  FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode)
  FLAG_ACCESSOR(kNative, is_native, set_native)
  FLAG_ACCESSOR(kModule, is_module, set_module)
  FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing)
  FLAG_ACCESSOR(kAstValueFactoryOwned, ast_value_factory_owned,
                set_ast_value_factory_owned)
  FLAG_ACCESSOR(kIsNamedExpression, is_named_expression,
                set_is_named_expression)
  FLAG_ACCESSOR(kCallsEval, calls_eval, set_calls_eval)
78 79
  FLAG_ACCESSOR(kDebug, is_debug, set_is_debug)
  FLAG_ACCESSOR(kSerializing, will_serialize, set_will_serialize)
80
  FLAG_ACCESSOR(kScopeInfoIsEmpty, scope_info_is_empty, set_scope_info_is_empty)
81 82
  FLAG_ACCESSOR(kTailCallEliminationEnabled, is_tail_call_elimination_enabled,
                set_tail_call_elimination_enabled)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

#undef FLAG_ACCESSOR

  void set_parse_restriction(ParseRestriction restriction) {
    SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION);
  }

  ParseRestriction parse_restriction() const {
    return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL
                                      : NO_PARSE_RESTRICTION;
  }

  ScriptCompiler::ExternalSourceStream* source_stream() const {
    return source_stream_;
  }
  void set_source_stream(ScriptCompiler::ExternalSourceStream* source_stream) {
    source_stream_ = source_stream;
  }

  ScriptCompiler::StreamedSource::Encoding source_stream_encoding() const {
    return source_stream_encoding_;
  }
  void set_source_stream_encoding(
      ScriptCompiler::StreamedSource::Encoding source_stream_encoding) {
    source_stream_encoding_ = source_stream_encoding;
  }

  Utf16CharacterStream* character_stream() const { return character_stream_; }
  void set_character_stream(Utf16CharacterStream* character_stream) {
    character_stream_ = character_stream;
  }

  v8::Extension* extension() const { return extension_; }
  void set_extension(v8::Extension* extension) { extension_ = extension; }

  ScriptData** cached_data() const { return cached_data_; }
  void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; }

121 122
  PreParsedScopeData* preparsed_scope_data() { return &preparsed_scope_data_; }

123 124 125 126 127 128 129 130 131 132 133 134
  ScriptCompiler::CompileOptions compile_options() const {
    return compile_options_;
  }
  void set_compile_options(ScriptCompiler::CompileOptions compile_options) {
    compile_options_ = compile_options;
  }

  DeclarationScope* script_scope() const { return script_scope_; }
  void set_script_scope(DeclarationScope* script_scope) {
    script_scope_ = script_scope;
  }

135 136 137 138 139
  DeclarationScope* asm_function_scope() const { return asm_function_scope_; }
  void set_asm_function_scope(DeclarationScope* scope) {
    asm_function_scope_ = scope;
  }

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
  void set_ast_value_factory(AstValueFactory* ast_value_factory) {
    ast_value_factory_ = ast_value_factory;
  }

  const AstRawString* function_name() const { return function_name_; }
  void set_function_name(const AstRawString* function_name) {
    function_name_ = function_name;
  }

  FunctionLiteral* literal() const { return literal_; }
  void set_literal(FunctionLiteral* literal) { literal_ = literal; }

  DeclarationScope* scope() const;

  UnicodeCache* unicode_cache() const { return unicode_cache_; }
  void set_unicode_cache(UnicodeCache* unicode_cache) {
    unicode_cache_ = unicode_cache;
  }

  uintptr_t stack_limit() const { return stack_limit_; }
  void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }

  uint32_t hash_seed() const { return hash_seed_; }
  void set_hash_seed(uint32_t hash_seed) { hash_seed_ = hash_seed; }

  int compiler_hints() const { return compiler_hints_; }
  void set_compiler_hints(int compiler_hints) {
    compiler_hints_ = compiler_hints;
  }

  int start_position() const { return start_position_; }
  void set_start_position(int start_position) {
    start_position_ = start_position;
  }

  int end_position() const { return end_position_; }
  void set_end_position(int end_position) { end_position_ = end_position; }

179 180 181 182 183
  int parameters_end_pos() const { return parameters_end_pos_; }
  void set_parameters_end_pos(int parameters_end_pos) {
    parameters_end_pos_ = parameters_end_pos;
  }

184 185 186 187 188
  int function_literal_id() const { return function_literal_id_; }
  void set_function_literal_id(int function_literal_id) {
    function_literal_id_ = function_literal_id;
  }

189 190 191 192 193
  int max_function_literal_id() const { return max_function_literal_id_; }
  void set_max_function_literal_id(int max_function_literal_id) {
    max_function_literal_id_ = max_function_literal_id;
  }

194 195 196 197 198 199 200 201 202 203 204 205 206
  const AstStringConstants* ast_string_constants() const {
    return ast_string_constants_;
  }
  void set_ast_string_constants(
      const AstStringConstants* ast_string_constants) {
    ast_string_constants_ = ast_string_constants;
  }

  RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
  void set_runtime_call_stats(RuntimeCallStats* runtime_call_stats) {
    runtime_call_stats_ = runtime_call_stats;
  }

207 208 209 210 211 212 213 214 215 216
  // Getters for individual compiler hints.
  bool is_declaration() const;
  FunctionKind function_kind() const;

  //--------------------------------------------------------------------------
  // TODO(titzer): these should not be part of ParseInfo.
  //--------------------------------------------------------------------------
  Isolate* isolate() const { return isolate_; }
  Handle<SharedFunctionInfo> shared_info() const { return shared_; }
  Handle<Script> script() const { return script_; }
217 218 219
  MaybeHandle<ScopeInfo> maybe_outer_scope_info() const {
    return maybe_outer_scope_info_;
  }
220
  void clear_script() { script_ = Handle<Script>::null(); }
221 222 223 224 225 226
  void set_isolate(Isolate* isolate) {
    if (isolate) {
      InitFromIsolate(isolate);
    }
    isolate_ = isolate;
  }
227
  void set_shared_info(Handle<SharedFunctionInfo> shared) { shared_ = shared; }
228 229 230
  void set_outer_scope_info(Handle<ScopeInfo> outer_scope_info) {
    maybe_outer_scope_info_ = outer_scope_info;
  }
231 232 233 234 235 236 237 238 239 240 241 242
  void set_script(Handle<Script> script) { script_ = script; }
  //--------------------------------------------------------------------------

  LanguageMode language_mode() const {
    return construct_language_mode(is_strict_mode());
  }
  void set_language_mode(LanguageMode language_mode) {
    STATIC_ASSERT(LANGUAGE_END == 2);
    set_strict_mode(is_strict(language_mode));
  }

  void ReopenHandlesInNewHandleScope() {
243 244 245 246 247 248
    if (!script_.is_null()) {
      script_ = Handle<Script>(*script_);
    }
    if (!shared_.is_null()) {
      shared_ = Handle<SharedFunctionInfo>(*shared_);
    }
249 250 251 252
    Handle<ScopeInfo> outer_scope_info;
    if (maybe_outer_scope_info_.ToHandle(&outer_scope_info)) {
      maybe_outer_scope_info_ = Handle<ScopeInfo>(*outer_scope_info);
    }
253 254 255 256 257 258 259 260 261 262 263 264 265
  }

#ifdef DEBUG
  bool script_is_native() const;
#endif  // DEBUG

 private:
  // Various configuration flags for parsing.
  enum Flag {
    // ---------- Input flags ---------------------------
    kToplevel = 1 << 0,
    kLazy = 1 << 1,
    kEval = 1 << 2,
266 267 268 269 270 271 272
    kStrictMode = 1 << 3,
    kNative = 1 << 4,
    kParseRestriction = 1 << 5,
    kModule = 1 << 6,
    kAllowLazyParsing = 1 << 7,
    kIsNamedExpression = 1 << 8,
    kCallsEval = 1 << 9,
273 274
    kDebug = 1 << 10,
    kSerializing = 1 << 11,
275
    kScopeInfoIsEmpty = 1 << 12,
276 277
    kTailCallEliminationEnabled = 1 << 13,
    kAstValueFactoryOwned = 1 << 14,
278 279 280
  };

  //------------- Inputs to parsing and scope analysis -----------------------
281
  std::shared_ptr<Zone> zone_;
282 283 284 285 286 287 288
  unsigned flags_;
  ScriptCompiler::ExternalSourceStream* source_stream_;
  ScriptCompiler::StreamedSource::Encoding source_stream_encoding_;
  Utf16CharacterStream* character_stream_;
  v8::Extension* extension_;
  ScriptCompiler::CompileOptions compile_options_;
  DeclarationScope* script_scope_;
289
  DeclarationScope* asm_function_scope_;
290 291 292 293 294 295
  UnicodeCache* unicode_cache_;
  uintptr_t stack_limit_;
  uint32_t hash_seed_;
  int compiler_hints_;
  int start_position_;
  int end_position_;
296
  int parameters_end_pos_;
297
  int function_literal_id_;
298
  int max_function_literal_id_;
299 300 301 302 303

  // TODO(titzer): Move handles and isolate out of ParseInfo.
  Isolate* isolate_;
  Handle<SharedFunctionInfo> shared_;
  Handle<Script> script_;
304
  MaybeHandle<ScopeInfo> maybe_outer_scope_info_;
305 306 307

  //----------- Inputs+Outputs of parsing and scope analysis -----------------
  ScriptData** cached_data_;  // used if available, populated if requested.
308
  PreParsedScopeData preparsed_scope_data_;
309
  AstValueFactory* ast_value_factory_;  // used if available, otherwise new.
310
  const class AstStringConstants* ast_string_constants_;
311
  const AstRawString* function_name_;
312
  RuntimeCallStats* runtime_call_stats_;
313 314 315

  //----------- Output of parsing and scope analysis ------------------------
  FunctionLiteral* literal_;
316
  std::shared_ptr<DeferredHandles> deferred_handles_;
317 318 319 320 321 322 323 324 325 326

  void SetFlag(Flag f) { flags_ |= f; }
  void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; }
  bool GetFlag(Flag f) const { return (flags_ & f) != 0; }
};

}  // namespace internal
}  // namespace v8

#endif  // V8_PARSING_PARSE_INFO_H_