Home | History | Annotate | Download | only in parsing
      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 #include "src/pending-compilation-error-handler.h"
     17 
     18 namespace v8 {
     19 
     20 class Extension;
     21 
     22 namespace internal {
     23 
     24 class AccountingAllocator;
     25 class AstRawString;
     26 class AstStringConstants;
     27 class AstValueFactory;
     28 class DeclarationScope;
     29 class FunctionLiteral;
     30 class RuntimeCallStats;
     31 class Logger;
     32 class SourceRangeMap;
     33 class UnicodeCache;
     34 class Utf16CharacterStream;
     35 class Zone;
     36 
     37 // A container for the inputs, configuration options, and outputs of parsing.
     38 class V8_EXPORT_PRIVATE ParseInfo {
     39  public:
     40   ParseInfo(Isolate*);
     41   ParseInfo(Isolate*, AccountingAllocator* zone_allocator);
     42   ParseInfo(Isolate* isolate, Handle<Script> script);
     43   ParseInfo(Isolate* isolate, Handle<SharedFunctionInfo> shared);
     44 
     45   ~ParseInfo();
     46 
     47   Handle<Script> CreateScript(Isolate* isolate, Handle<String> source,
     48                               ScriptOriginOptions origin_options,
     49                               NativesFlag natives = NOT_NATIVES_CODE);
     50 
     51   // Either returns the ast-value-factory associcated with this ParseInfo, or
     52   // creates and returns a new factory if none exists.
     53   AstValueFactory* GetOrCreateAstValueFactory();
     54 
     55   Zone* zone() const { return zone_.get(); }
     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 
     63   FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel)
     64   FLAG_ACCESSOR(kEager, is_eager, set_eager)
     65   FLAG_ACCESSOR(kEval, is_eval, set_eval)
     66   FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode)
     67   FLAG_ACCESSOR(kNative, is_native, set_native)
     68   FLAG_ACCESSOR(kModule, is_module, set_module)
     69   FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing)
     70   FLAG_ACCESSOR(kIsNamedExpression, is_named_expression,
     71                 set_is_named_expression)
     72   FLAG_ACCESSOR(kLazyCompile, lazy_compile, set_lazy_compile)
     73   FLAG_ACCESSOR(kCollectTypeProfile, collect_type_profile,
     74                 set_collect_type_profile)
     75   FLAG_ACCESSOR(kIsAsmWasmBroken, is_asm_wasm_broken, set_asm_wasm_broken)
     76   FLAG_ACCESSOR(kBlockCoverageEnabled, block_coverage_enabled,
     77                 set_block_coverage_enabled)
     78   FLAG_ACCESSOR(kOnBackgroundThread, on_background_thread,
     79                 set_on_background_thread)
     80   FLAG_ACCESSOR(kWrappedAsFunction, is_wrapped_as_function,
     81                 set_wrapped_as_function)
     82   FLAG_ACCESSOR(kAllowEvalCache, allow_eval_cache, set_allow_eval_cache)
     83   FLAG_ACCESSOR(kIsDeclaration, is_declaration, set_declaration)
     84   FLAG_ACCESSOR(kRequiresInstanceFieldsInitializer,
     85                 requires_instance_fields_initializer,
     86                 set_requires_instance_fields_initializer);
     87 #undef FLAG_ACCESSOR
     88 
     89   void set_parse_restriction(ParseRestriction restriction) {
     90     SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION);
     91   }
     92 
     93   ParseRestriction parse_restriction() const {
     94     return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL
     95                                       : NO_PARSE_RESTRICTION;
     96   }
     97 
     98   Utf16CharacterStream* character_stream() const {
     99     return character_stream_.get();
    100   }
    101   void set_character_stream(
    102       std::unique_ptr<Utf16CharacterStream> character_stream);
    103   void ResetCharacterStream();
    104 
    105   v8::Extension* extension() const { return extension_; }
    106   void set_extension(v8::Extension* extension) { extension_ = extension; }
    107 
    108 
    109   ConsumedPreParsedScopeData* consumed_preparsed_scope_data() {
    110     return &consumed_preparsed_scope_data_;
    111   }
    112 
    113   DeclarationScope* script_scope() const { return script_scope_; }
    114   void set_script_scope(DeclarationScope* script_scope) {
    115     script_scope_ = script_scope;
    116   }
    117 
    118   AstValueFactory* ast_value_factory() const {
    119     DCHECK(ast_value_factory_.get());
    120     return ast_value_factory_.get();
    121   }
    122 
    123   const AstRawString* function_name() const { return function_name_; }
    124   void set_function_name(const AstRawString* function_name) {
    125     function_name_ = function_name;
    126   }
    127 
    128   FunctionLiteral* literal() const { return literal_; }
    129   void set_literal(FunctionLiteral* literal) { literal_ = literal; }
    130 
    131   DeclarationScope* scope() const;
    132 
    133   UnicodeCache* unicode_cache() const { return unicode_cache_; }
    134   void set_unicode_cache(UnicodeCache* unicode_cache) {
    135     unicode_cache_ = unicode_cache;
    136   }
    137 
    138   uintptr_t stack_limit() const { return stack_limit_; }
    139   void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; }
    140 
    141   uint64_t hash_seed() const { return hash_seed_; }
    142   void set_hash_seed(uint64_t hash_seed) { hash_seed_ = hash_seed; }
    143 
    144   int start_position() const { return start_position_; }
    145   void set_start_position(int start_position) {
    146     start_position_ = start_position;
    147   }
    148 
    149   int end_position() const { return end_position_; }
    150   void set_end_position(int end_position) { end_position_ = end_position; }
    151 
    152   int parameters_end_pos() const { return parameters_end_pos_; }
    153   void set_parameters_end_pos(int parameters_end_pos) {
    154     parameters_end_pos_ = parameters_end_pos;
    155   }
    156 
    157   int function_literal_id() const { return function_literal_id_; }
    158   void set_function_literal_id(int function_literal_id) {
    159     function_literal_id_ = function_literal_id;
    160   }
    161 
    162   FunctionKind function_kind() const { return function_kind_; }
    163   void set_function_kind(FunctionKind function_kind) {
    164     function_kind_ = function_kind;
    165   }
    166 
    167   int max_function_literal_id() const { return max_function_literal_id_; }
    168   void set_max_function_literal_id(int max_function_literal_id) {
    169     max_function_literal_id_ = max_function_literal_id;
    170   }
    171 
    172   const AstStringConstants* ast_string_constants() const {
    173     return ast_string_constants_;
    174   }
    175   void set_ast_string_constants(
    176       const AstStringConstants* ast_string_constants) {
    177     ast_string_constants_ = ast_string_constants;
    178   }
    179 
    180   RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
    181   void set_runtime_call_stats(RuntimeCallStats* runtime_call_stats) {
    182     runtime_call_stats_ = runtime_call_stats;
    183   }
    184   Logger* logger() const { return logger_; }
    185   void set_logger(Logger* logger) { logger_ = logger; }
    186 
    187   void AllocateSourceRangeMap();
    188   SourceRangeMap* source_range_map() const { return source_range_map_; }
    189   void set_source_range_map(SourceRangeMap* source_range_map) {
    190     source_range_map_ = source_range_map;
    191   }
    192 
    193   PendingCompilationErrorHandler* pending_error_handler() {
    194     return &pending_error_handler_;
    195   }
    196 
    197   //--------------------------------------------------------------------------
    198   // TODO(titzer): these should not be part of ParseInfo.
    199   //--------------------------------------------------------------------------
    200   Handle<Script> script() const { return script_; }
    201   MaybeHandle<ScopeInfo> maybe_outer_scope_info() const {
    202     return maybe_outer_scope_info_;
    203   }
    204   void set_outer_scope_info(Handle<ScopeInfo> outer_scope_info) {
    205     maybe_outer_scope_info_ = outer_scope_info;
    206   }
    207 
    208   int script_id() const { return script_id_; }
    209   //--------------------------------------------------------------------------
    210 
    211   LanguageMode language_mode() const {
    212     return construct_language_mode(is_strict_mode());
    213   }
    214   void set_language_mode(LanguageMode language_mode) {
    215     STATIC_ASSERT(LanguageModeSize == 2);
    216     set_strict_mode(is_strict(language_mode));
    217   }
    218 
    219   void EmitBackgroundParseStatisticsOnBackgroundThread();
    220   void UpdateBackgroundParseStatisticsOnMainThread(Isolate* isolate);
    221 
    222  private:
    223   void SetScriptForToplevelCompile(Isolate* isolate, Handle<Script> script);
    224   void set_script(Handle<Script> script);
    225 
    226   // Various configuration flags for parsing.
    227   enum Flag {
    228     // ---------- Input flags ---------------------------
    229     kToplevel = 1 << 0,
    230     kEager = 1 << 1,
    231     kEval = 1 << 2,
    232     kStrictMode = 1 << 3,
    233     kNative = 1 << 4,
    234     kParseRestriction = 1 << 5,
    235     kModule = 1 << 6,
    236     kAllowLazyParsing = 1 << 7,
    237     kIsNamedExpression = 1 << 8,
    238     kLazyCompile = 1 << 9,
    239     kCollectTypeProfile = 1 << 10,
    240     kBlockCoverageEnabled = 1 << 11,
    241     kIsAsmWasmBroken = 1 << 12,
    242     kOnBackgroundThread = 1 << 13,
    243     kWrappedAsFunction = 1 << 14,  // Implicitly wrapped as function.
    244     kAllowEvalCache = 1 << 15,
    245     kIsDeclaration = 1 << 16,
    246     kRequiresInstanceFieldsInitializer = 1 << 17,
    247   };
    248 
    249   //------------- Inputs to parsing and scope analysis -----------------------
    250   std::unique_ptr<Zone> zone_;
    251   unsigned flags_;
    252   v8::Extension* extension_;
    253   DeclarationScope* script_scope_;
    254   UnicodeCache* unicode_cache_;
    255   uintptr_t stack_limit_;
    256   uint64_t hash_seed_;
    257   FunctionKind function_kind_;
    258   int script_id_;
    259   int start_position_;
    260   int end_position_;
    261   int parameters_end_pos_;
    262   int function_literal_id_;
    263   int max_function_literal_id_;
    264 
    265   // TODO(titzer): Move handles out of ParseInfo.
    266   Handle<Script> script_;
    267   MaybeHandle<ScopeInfo> maybe_outer_scope_info_;
    268 
    269   //----------- Inputs+Outputs of parsing and scope analysis -----------------
    270   std::unique_ptr<Utf16CharacterStream> character_stream_;
    271   ConsumedPreParsedScopeData consumed_preparsed_scope_data_;
    272   std::unique_ptr<AstValueFactory> ast_value_factory_;
    273   const class AstStringConstants* ast_string_constants_;
    274   const AstRawString* function_name_;
    275   RuntimeCallStats* runtime_call_stats_;
    276   Logger* logger_;
    277   SourceRangeMap* source_range_map_;  // Used when block coverage is enabled.
    278 
    279   //----------- Output of parsing and scope analysis ------------------------
    280   FunctionLiteral* literal_;
    281   PendingCompilationErrorHandler pending_error_handler_;
    282 
    283   void SetFlag(Flag f) { flags_ |= f; }
    284   void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; }
    285   bool GetFlag(Flag f) const { return (flags_ & f) != 0; }
    286 };
    287 
    288 }  // namespace internal
    289 }  // namespace v8
    290 
    291 #endif  // V8_PARSING_PARSE_INFO_H_
    292