Chromium Code Reviews| Index: src/parser.h |
| diff --git a/src/parser.h b/src/parser.h |
| index 8f13916fde82bfafbc3dddb720afda4a8a27b046..eb4a82d68ad7b0f177988ba8e48ff0981673cfc0 100644 |
| --- a/src/parser.h |
| +++ b/src/parser.h |
| @@ -19,11 +19,234 @@ class ScriptCompiler; |
| namespace internal { |
| -class CompilationInfo; |
| class ParserLog; |
| class PositionStack; |
| class Target; |
| +// TODO(titzer): move this to preparse-data.h? |
| +class ScriptData { |
|
marja
2015/03/04 09:24:40
Can you put the added classes (ScriptData and Pars
titzer
2015/03/04 22:39:35
ScriptData probably should live somewhere else, bu
titzer
2015/03/05 20:18:30
I've moved ScriptData to preparse-data.h
|
| + public: |
| + ScriptData(const byte* data, int length); |
| + ~ScriptData() { |
| + if (owns_data_) DeleteArray(data_); |
| + } |
| + |
| + const byte* data() const { return data_; } |
| + int length() const { return length_; } |
| + bool rejected() const { return rejected_; } |
| + |
| + void Reject() { rejected_ = true; } |
| + |
| + void AcquireDataOwnership() { |
| + DCHECK(!owns_data_); |
| + owns_data_ = true; |
| + } |
| + |
| + void ReleaseDataOwnership() { |
| + DCHECK(owns_data_); |
| + owns_data_ = false; |
| + } |
| + |
| + private: |
| + bool owns_data_ : 1; |
| + bool rejected_ : 1; |
| + const byte* data_; |
| + int length_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ScriptData); |
| +}; |
| + |
| +// Collects the data necessary to parse a function as well as the outcome |
| +// of parsing, which includes an AST. |
| +class ParseInfo { |
| + public: |
| + explicit ParseInfo(Zone* zone) { |
| + memset(this, 0, sizeof(ParseInfo)); |
| + zone_ = zone; |
| + } |
| + |
| + ~ParseInfo() { DeleteAstValueFactory(); } |
| + |
| + void InitializeFromJSFunction(Handle<JSFunction> function); |
| + void InitializeFromSharedFunctionInfo(Handle<SharedFunctionInfo> shared); |
| + void InitializeFromScript(Handle<Script> script); |
| + |
| + // Various configuration flags for parsing. |
| + enum Flag { |
|
rossberg
2015/03/04 10:25:12
Can we make the enum private?
titzer
2015/03/04 22:39:35
Done.
|
| + // ---------- Input flags --------------------------- |
| + kToplevel = 1 << 0, |
| + kLazy = 1 << 1, |
| + kEval = 1 << 2, |
| + kGlobal = 1 << 3, |
| + kStrictMode = 1 << 4, |
| + kStrongMode = 1 << 5, |
| + kNative = 1 << 6, |
| + kParseRestriction = 1 << 7, |
| + kModule = 1 << 8, |
| + kAllowLazyParsing = 1 << 9, |
| + // ---------- Output flags -------------------------- |
| + kThisHasUses = 1 << 10, |
| + kAstValueFactoryOwned = 1 << 11 |
| + }; |
| + |
| +// 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(kLazy, is_lazy, set_lazy) |
| + FLAG_ACCESSOR(kEval, is_eval, set_eval) |
| + FLAG_ACCESSOR(kGlobal, is_global, set_global) |
| + FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode) |
| + FLAG_ACCESSOR(kStrongMode, is_strong_mode, set_strong_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(kThisHasUses, this_has_uses, set_this_has_uses) |
| + FLAG_ACCESSOR(kAstValueFactoryOwned, ast_value_factory_owned, |
| + set_ast_value_factory_owned) |
| + |
| + 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; |
| + } |
| + |
| +#undef FLAG_ACCESSOR |
| + |
| + Zone* zone() { return zone_; } |
| + |
| + ScriptCompiler::ExternalSourceStream* source_stream() { |
| + return source_stream_; |
| + } |
| + void set_source_stream(ScriptCompiler::ExternalSourceStream* source_stream) { |
| + source_stream_ = source_stream; |
| + } |
| + |
| + ScriptCompiler::StreamedSource::Encoding source_stream_encoding() { |
| + return source_stream_encoding_; |
| + } |
| + void set_source_stream_encoding( |
| + ScriptCompiler::StreamedSource::Encoding source_stream_encoding) { |
| + source_stream_encoding_ = source_stream_encoding; |
| + } |
| + |
| + v8::Extension* extension() { return extension_; } |
| + void set_extension(v8::Extension* extension) { extension_ = extension; } |
| + |
| + ScriptData** cached_data() { return cached_data_; } |
| + void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; } |
| + |
| + ScriptCompiler::CompileOptions compile_options() { return compile_options_; } |
| + void set_compile_options(ScriptCompiler::CompileOptions compile_options) { |
| + compile_options_ = compile_options; |
| + } |
| + |
| + Scope* script_scope() { return script_scope_; } |
| + void set_script_scope(Scope* script_scope) { script_scope_ = script_scope; } |
| + |
| + AstValueFactory* ast_value_factory() { return ast_value_factory_; } |
| + void set_ast_value_factory(AstValueFactory* ast_value_factory) { |
| + ast_value_factory_ = ast_value_factory; |
| + } |
| + |
| + FunctionLiteral* function() { // TODO(titzer): temporary name adapter |
| + return literal_; |
| + } |
| + FunctionLiteral* literal() { return literal_; } |
| + void set_literal(FunctionLiteral* literal) { literal_ = literal; } |
| + |
| + Scope* scope() { return scope_; } |
| + void set_scope(Scope* scope) { scope_ = scope; } |
| + |
| + UnicodeCache* unicode_cache() { return unicode_cache_; } |
| + void set_unicode_cache(UnicodeCache* unicode_cache) { |
| + unicode_cache_ = unicode_cache; |
| + } |
| + |
| + uintptr_t stack_limit() { return stack_limit_; } |
| + void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } |
| + |
| + uint32_t hash_seed() { return hash_seed_; } |
| + void set_hash_seed(uint32_t hash_seed) { hash_seed_ = hash_seed; } |
| + |
| + //-------------------------------------------------------------------------- |
| + // TODO(titzer): these should not be part of ParseInfo. |
| + //-------------------------------------------------------------------------- |
| + Isolate* isolate() { return isolate_; } |
| + Handle<JSFunction> closure() { return closure_; } |
| + Handle<SharedFunctionInfo> shared_info() { return shared_; } |
| + Handle<Script> script() { return script_; } |
| + Handle<Context> context() { return context_; } |
| + void clear_script() { script_ = Handle<Script>::null(); } |
| + void set_context(Handle<Context> context) { context_ = context; } |
| + void set_script(Handle<Script> script) { script_ = script; } |
| + //-------------------------------------------------------------------------- |
| + |
| + LanguageMode language_mode() { |
| + return construct_language_mode(is_strict_mode(), is_strong_mode()); |
| + } |
| + void set_language_mode(LanguageMode language_mode) { |
| + STATIC_ASSERT(LANGUAGE_END == 3); |
| + set_strict_mode(language_mode & STRICT_BIT); |
| + set_strong_mode(language_mode & STRONG_BIT); |
| + } |
| + |
| + void DeleteAstValueFactory() { |
| + if (ast_value_factory_owned()) { |
| + delete ast_value_factory_; |
| + set_ast_value_factory_owned(false); |
| + } |
| + ast_value_factory_ = nullptr; |
| + } |
| + |
| + void ReopenHandlesInNewHandleScope() { |
| + closure_ = Handle<JSFunction>(*closure_); |
| + shared_ = Handle<SharedFunctionInfo>(*shared_); |
| + script_ = Handle<Script>(*script_); |
| + context_ = Handle<Context>(*context_); |
| + } |
| + |
| + private: |
| + //------------- Inputs to parsing and scope analysis ----------------------- |
|
rossberg
2015/03/04 10:25:12
Nit: Would be even more useful to have these secti
|
| + Zone* zone_; |
| + unsigned flags_; |
| + ScriptCompiler::ExternalSourceStream* source_stream_; |
| + ScriptCompiler::StreamedSource::Encoding source_stream_encoding_; |
| + v8::Extension* extension_; |
| + ScriptCompiler::CompileOptions compile_options_; |
| + Scope* script_scope_; |
| + UnicodeCache* unicode_cache_; |
| + uintptr_t stack_limit_; |
| + uint32_t hash_seed_; |
| + |
| + // TODO(titzer): these should *not* be part of ParseInfo. Refactor Parser. |
|
marja
2015/03/04 09:24:40
Yes! :)
titzer
2015/03/05 20:18:30
I'm not sure exactly where these will land, but I
|
| + Isolate* isolate_; |
| + Handle<JSFunction> closure_; |
| + Handle<SharedFunctionInfo> shared_; |
| + Handle<Script> script_; |
| + Handle<Context> context_; |
| + |
| + //----------- Inputs+Outputs of parsing and scope analysis ----------------- |
| + ScriptData** cached_data_; // used if available, populated if requested. |
| + AstValueFactory* ast_value_factory_; // used if available, otherwise new. |
| + |
| + //----------- Outputs of parsing and scope analysis ------------------------ |
| + FunctionLiteral* literal_; // produced by full parser. |
| + Scope* scope_; // produced by scope analysis. |
| + |
| + 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; } |
| + |
| + void set_shared_info(Handle<SharedFunctionInfo> shared) { shared_ = shared; } |
| + void set_closure(Handle<JSFunction> closure) { closure_ = closure; } |
| +}; |
| class FunctionEntry BASE_EMBEDDED { |
| public: |
| @@ -636,8 +859,7 @@ class ParserTraits { |
| class Parser : public ParserBase<ParserTraits> { |
| public: |
| - Parser(CompilationInfo* info, uintptr_t stack_limit, uint32_t hash_seed, |
| - UnicodeCache* unicode_cache); |
| + explicit Parser(ParseInfo* info); |
| ~Parser() { |
| delete reusable_preparser_; |
| reusable_preparser_ = NULL; |
| @@ -648,9 +870,9 @@ class Parser : public ParserBase<ParserTraits> { |
| // Parses the source code represented by the compilation info and sets its |
| // function literal. Returns false (and deallocates any allocated AST |
| // nodes) if parsing failed. |
| - static bool ParseStatic(CompilationInfo* info, bool allow_lazy = false); |
| - bool Parse(CompilationInfo* info); |
| - void ParseOnBackground(CompilationInfo* info); |
| + static bool ParseStatic(ParseInfo* info, bool allow_lazy = false); |
| + bool Parse(ParseInfo* info); |
| + void ParseOnBackground(ParseInfo* info); |
| // Handle errors detected during parsing, move statistics to Isolate, |
| // internalize strings (move them to the heap). |
| @@ -670,17 +892,17 @@ class Parser : public ParserBase<ParserTraits> { |
| static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 |
| // Returns NULL if parsing failed. |
| - FunctionLiteral* ParseProgram(Isolate* isolate, CompilationInfo* info); |
| + FunctionLiteral* ParseProgram(Isolate* isolate, ParseInfo* info); |
| - FunctionLiteral* ParseLazy(Isolate* isolate, CompilationInfo* info); |
| - FunctionLiteral* ParseLazy(Isolate* isolate, CompilationInfo* info, |
| + FunctionLiteral* ParseLazy(Isolate* isolate, ParseInfo* info); |
| + FunctionLiteral* ParseLazy(Isolate* isolate, ParseInfo* info, |
| Utf16CharacterStream* source); |
| // Called by ParseProgram after setting up the scanner. |
| - FunctionLiteral* DoParseProgram(CompilationInfo* info, Scope** scope, |
| + FunctionLiteral* DoParseProgram(ParseInfo* info, Scope** scope, |
| Scope** ad_hoc_eval_scope); |
| - void SetCachedData(CompilationInfo* info); |
| + void SetCachedData(ParseInfo* info); |
| bool inside_with() const { return scope_->inside_with(); } |
| ScriptCompiler::CompileOptions compile_options() const { |