OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_PARSER_H_ | 5 #ifndef V8_PARSER_H_ |
6 #define V8_PARSER_H_ | 6 #define V8_PARSER_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/ast.h" | 9 #include "src/ast.h" |
10 #include "src/compiler.h" // For CachedDataMode | 10 #include "src/compiler.h" // TODO(titzer): remove this include dependency |
11 #include "src/pending-compilation-error-handler.h" | 11 #include "src/pending-compilation-error-handler.h" |
12 #include "src/preparse-data.h" | 12 #include "src/preparse-data.h" |
13 #include "src/preparse-data-format.h" | 13 #include "src/preparse-data-format.h" |
14 #include "src/preparser.h" | 14 #include "src/preparser.h" |
15 #include "src/scopes.h" | 15 #include "src/scopes.h" |
16 | 16 |
17 namespace v8 { | 17 namespace v8 { |
| 18 |
18 class ScriptCompiler; | 19 class ScriptCompiler; |
19 | 20 |
20 namespace internal { | 21 namespace internal { |
21 | 22 |
22 class CompilationInfo; | |
23 class ParserLog; | |
24 class PositionStack; | |
25 class Target; | 23 class Target; |
26 | 24 |
| 25 // A container for the inputs, configuration options, and outputs of parsing. |
| 26 class ParseInfo { |
| 27 public: |
| 28 explicit ParseInfo(Zone* zone) { |
| 29 memset(this, 0, sizeof(ParseInfo)); |
| 30 zone_ = zone; |
| 31 } |
| 32 |
| 33 ~ParseInfo() { |
| 34 if (ast_value_factory_owned()) { |
| 35 delete ast_value_factory_; |
| 36 set_ast_value_factory_owned(false); |
| 37 } |
| 38 ast_value_factory_ = nullptr; |
| 39 } |
| 40 |
| 41 ParseInfo* InitializeFromJSFunction(Handle<JSFunction> function); |
| 42 ParseInfo* InitializeFromSharedFunctionInfo( |
| 43 Handle<SharedFunctionInfo> shared); |
| 44 ParseInfo* InitializeFromScript(Handle<Script> script); |
| 45 |
| 46 Zone* zone() { return zone_; } |
| 47 |
| 48 // Convenience accessor methods for flags. |
| 49 #define FLAG_ACCESSOR(flag, getter, setter) \ |
| 50 bool getter() const { return GetFlag(flag); } \ |
| 51 void setter() { SetFlag(flag); } \ |
| 52 void setter(bool val) { SetFlag(flag, val); } |
| 53 |
| 54 FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel) |
| 55 FLAG_ACCESSOR(kLazy, is_lazy, set_lazy) |
| 56 FLAG_ACCESSOR(kEval, is_eval, set_eval) |
| 57 FLAG_ACCESSOR(kGlobal, is_global, set_global) |
| 58 FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode) |
| 59 FLAG_ACCESSOR(kStrongMode, is_strong_mode, set_strong_mode) |
| 60 FLAG_ACCESSOR(kNative, is_native, set_native) |
| 61 FLAG_ACCESSOR(kModule, is_module, set_module) |
| 62 FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing) |
| 63 FLAG_ACCESSOR(kThisHasUses, this_has_uses, set_this_has_uses) |
| 64 FLAG_ACCESSOR(kAstValueFactoryOwned, ast_value_factory_owned, |
| 65 set_ast_value_factory_owned) |
| 66 |
| 67 #undef FLAG_ACCESSOR |
| 68 |
| 69 void set_parse_restriction(ParseRestriction restriction) { |
| 70 SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION); |
| 71 } |
| 72 |
| 73 ParseRestriction parse_restriction() const { |
| 74 return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL |
| 75 : NO_PARSE_RESTRICTION; |
| 76 } |
| 77 |
| 78 ScriptCompiler::ExternalSourceStream* source_stream() { |
| 79 return source_stream_; |
| 80 } |
| 81 void set_source_stream(ScriptCompiler::ExternalSourceStream* source_stream) { |
| 82 source_stream_ = source_stream; |
| 83 } |
| 84 |
| 85 ScriptCompiler::StreamedSource::Encoding source_stream_encoding() { |
| 86 return source_stream_encoding_; |
| 87 } |
| 88 void set_source_stream_encoding( |
| 89 ScriptCompiler::StreamedSource::Encoding source_stream_encoding) { |
| 90 source_stream_encoding_ = source_stream_encoding; |
| 91 } |
| 92 |
| 93 v8::Extension* extension() { return extension_; } |
| 94 void set_extension(v8::Extension* extension) { extension_ = extension; } |
| 95 |
| 96 ScriptData** cached_data() { return cached_data_; } |
| 97 void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; } |
| 98 |
| 99 ScriptCompiler::CompileOptions compile_options() { return compile_options_; } |
| 100 void set_compile_options(ScriptCompiler::CompileOptions compile_options) { |
| 101 compile_options_ = compile_options; |
| 102 } |
| 103 |
| 104 Scope* script_scope() { return script_scope_; } |
| 105 void set_script_scope(Scope* script_scope) { script_scope_ = script_scope; } |
| 106 |
| 107 AstValueFactory* ast_value_factory() { return ast_value_factory_; } |
| 108 void set_ast_value_factory(AstValueFactory* ast_value_factory) { |
| 109 ast_value_factory_ = ast_value_factory; |
| 110 } |
| 111 |
| 112 FunctionLiteral* function() { // TODO(titzer): temporary name adapter |
| 113 return literal_; |
| 114 } |
| 115 FunctionLiteral* literal() { return literal_; } |
| 116 void set_literal(FunctionLiteral* literal) { literal_ = literal; } |
| 117 |
| 118 Scope* scope() { return scope_; } |
| 119 void set_scope(Scope* scope) { scope_ = scope; } |
| 120 |
| 121 UnicodeCache* unicode_cache() { return unicode_cache_; } |
| 122 void set_unicode_cache(UnicodeCache* unicode_cache) { |
| 123 unicode_cache_ = unicode_cache; |
| 124 } |
| 125 |
| 126 uintptr_t stack_limit() { return stack_limit_; } |
| 127 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } |
| 128 |
| 129 uint32_t hash_seed() { return hash_seed_; } |
| 130 void set_hash_seed(uint32_t hash_seed) { hash_seed_ = hash_seed; } |
| 131 |
| 132 //-------------------------------------------------------------------------- |
| 133 // TODO(titzer): these should not be part of ParseInfo. |
| 134 //-------------------------------------------------------------------------- |
| 135 Isolate* isolate() { return isolate_; } |
| 136 Handle<JSFunction> closure() { return closure_; } |
| 137 Handle<SharedFunctionInfo> shared_info() { return shared_; } |
| 138 Handle<Script> script() { return script_; } |
| 139 Handle<Context> context() { return context_; } |
| 140 void clear_script() { script_ = Handle<Script>::null(); } |
| 141 void set_isolate(Isolate* isolate) { isolate_ = isolate; } |
| 142 void set_context(Handle<Context> context) { context_ = context; } |
| 143 void set_script(Handle<Script> script) { script_ = script; } |
| 144 //-------------------------------------------------------------------------- |
| 145 |
| 146 LanguageMode language_mode() { |
| 147 return construct_language_mode(is_strict_mode(), is_strong_mode()); |
| 148 } |
| 149 void set_language_mode(LanguageMode language_mode) { |
| 150 STATIC_ASSERT(LANGUAGE_END == 3); |
| 151 set_strict_mode(language_mode & STRICT_BIT); |
| 152 set_strong_mode(language_mode & STRONG_BIT); |
| 153 } |
| 154 |
| 155 void ReopenHandlesInNewHandleScope() { |
| 156 closure_ = Handle<JSFunction>(*closure_); |
| 157 shared_ = Handle<SharedFunctionInfo>(*shared_); |
| 158 script_ = Handle<Script>(*script_); |
| 159 context_ = Handle<Context>(*context_); |
| 160 } |
| 161 |
| 162 private: |
| 163 // Various configuration flags for parsing. |
| 164 enum Flag { |
| 165 // ---------- Input flags --------------------------- |
| 166 kToplevel = 1 << 0, |
| 167 kLazy = 1 << 1, |
| 168 kEval = 1 << 2, |
| 169 kGlobal = 1 << 3, |
| 170 kStrictMode = 1 << 4, |
| 171 kStrongMode = 1 << 5, |
| 172 kNative = 1 << 6, |
| 173 kParseRestriction = 1 << 7, |
| 174 kModule = 1 << 8, |
| 175 kAllowLazyParsing = 1 << 9, |
| 176 // ---------- Output flags -------------------------- |
| 177 kThisHasUses = 1 << 10, |
| 178 kAstValueFactoryOwned = 1 << 11 |
| 179 }; |
| 180 |
| 181 //------------- Inputs to parsing and scope analysis ----------------------- |
| 182 Zone* zone_; |
| 183 unsigned flags_; |
| 184 ScriptCompiler::ExternalSourceStream* source_stream_; |
| 185 ScriptCompiler::StreamedSource::Encoding source_stream_encoding_; |
| 186 v8::Extension* extension_; |
| 187 ScriptCompiler::CompileOptions compile_options_; |
| 188 Scope* script_scope_; |
| 189 UnicodeCache* unicode_cache_; |
| 190 uintptr_t stack_limit_; |
| 191 uint32_t hash_seed_; |
| 192 |
| 193 // TODO(titzer): Move handles and isolate out of ParseInfo. |
| 194 Isolate* isolate_; |
| 195 Handle<JSFunction> closure_; |
| 196 Handle<SharedFunctionInfo> shared_; |
| 197 Handle<Script> script_; |
| 198 Handle<Context> context_; |
| 199 |
| 200 //----------- Inputs+Outputs of parsing and scope analysis ----------------- |
| 201 ScriptData** cached_data_; // used if available, populated if requested. |
| 202 AstValueFactory* ast_value_factory_; // used if available, otherwise new. |
| 203 |
| 204 //----------- Outputs of parsing and scope analysis ------------------------ |
| 205 FunctionLiteral* literal_; // produced by full parser. |
| 206 Scope* scope_; // produced by scope analysis. |
| 207 |
| 208 void SetFlag(Flag f) { flags_ |= f; } |
| 209 void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; } |
| 210 bool GetFlag(Flag f) const { return (flags_ & f) != 0; } |
| 211 |
| 212 void set_shared_info(Handle<SharedFunctionInfo> shared) { shared_ = shared; } |
| 213 void set_closure(Handle<JSFunction> closure) { closure_ = closure; } |
| 214 }; |
27 | 215 |
28 class FunctionEntry BASE_EMBEDDED { | 216 class FunctionEntry BASE_EMBEDDED { |
29 public: | 217 public: |
30 enum { | 218 enum { |
31 kStartPositionIndex, | 219 kStartPositionIndex, |
32 kEndPositionIndex, | 220 kEndPositionIndex, |
33 kLiteralCountIndex, | 221 kLiteralCountIndex, |
34 kPropertyCountIndex, | 222 kPropertyCountIndex, |
35 kLanguageModeIndex, | 223 kLanguageModeIndex, |
36 kUsesSuperPropertyIndex, | 224 kUsesSuperPropertyIndex, |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 return tag != NULL; | 817 return tag != NULL; |
630 } | 818 } |
631 | 819 |
632 private: | 820 private: |
633 Parser* parser_; | 821 Parser* parser_; |
634 }; | 822 }; |
635 | 823 |
636 | 824 |
637 class Parser : public ParserBase<ParserTraits> { | 825 class Parser : public ParserBase<ParserTraits> { |
638 public: | 826 public: |
639 Parser(CompilationInfo* info, uintptr_t stack_limit, uint32_t hash_seed, | 827 explicit Parser(ParseInfo* info); |
640 UnicodeCache* unicode_cache); | |
641 ~Parser() { | 828 ~Parser() { |
642 delete reusable_preparser_; | 829 delete reusable_preparser_; |
643 reusable_preparser_ = NULL; | 830 reusable_preparser_ = NULL; |
644 delete cached_parse_data_; | 831 delete cached_parse_data_; |
645 cached_parse_data_ = NULL; | 832 cached_parse_data_ = NULL; |
646 } | 833 } |
647 | 834 |
648 // Parses the source code represented by the compilation info and sets its | 835 // Parses the source code represented by the compilation info and sets its |
649 // function literal. Returns false (and deallocates any allocated AST | 836 // function literal. Returns false (and deallocates any allocated AST |
650 // nodes) if parsing failed. | 837 // nodes) if parsing failed. |
651 static bool ParseStatic(CompilationInfo* info, bool allow_lazy = false); | 838 static bool ParseStatic(ParseInfo* info); |
652 bool Parse(CompilationInfo* info); | 839 bool Parse(ParseInfo* info); |
653 void ParseOnBackground(CompilationInfo* info); | 840 void ParseOnBackground(ParseInfo* info); |
654 | 841 |
655 // Handle errors detected during parsing, move statistics to Isolate, | 842 // Handle errors detected during parsing, move statistics to Isolate, |
656 // internalize strings (move them to the heap). | 843 // internalize strings (move them to the heap). |
657 void Internalize(Isolate* isolate, Handle<Script> script, bool error); | 844 void Internalize(Isolate* isolate, Handle<Script> script, bool error); |
658 void HandleSourceURLComments(Isolate* isolate, Handle<Script> script); | 845 void HandleSourceURLComments(Isolate* isolate, Handle<Script> script); |
659 | 846 |
660 private: | 847 private: |
661 friend class ParserTraits; | 848 friend class ParserTraits; |
662 | 849 |
663 // Limit the allowed number of local variables in a function. The hard limit | 850 // Limit the allowed number of local variables in a function. The hard limit |
664 // is that offsets computed by FullCodeGenerator::StackOperand and similar | 851 // is that offsets computed by FullCodeGenerator::StackOperand and similar |
665 // functions are ints, and they should not overflow. In addition, accessing | 852 // functions are ints, and they should not overflow. In addition, accessing |
666 // local variables creates user-controlled constants in the generated code, | 853 // local variables creates user-controlled constants in the generated code, |
667 // and we don't want too much user-controlled memory inside the code (this was | 854 // and we don't want too much user-controlled memory inside the code (this was |
668 // the reason why this limit was introduced in the first place; see | 855 // the reason why this limit was introduced in the first place; see |
669 // https://codereview.chromium.org/7003030/ ). | 856 // https://codereview.chromium.org/7003030/ ). |
670 static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 | 857 static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 |
671 | 858 |
672 // Returns NULL if parsing failed. | 859 // Returns NULL if parsing failed. |
673 FunctionLiteral* ParseProgram(Isolate* isolate, CompilationInfo* info); | 860 FunctionLiteral* ParseProgram(Isolate* isolate, ParseInfo* info); |
674 | 861 |
675 FunctionLiteral* ParseLazy(Isolate* isolate, CompilationInfo* info); | 862 FunctionLiteral* ParseLazy(Isolate* isolate, ParseInfo* info); |
676 FunctionLiteral* ParseLazy(Isolate* isolate, CompilationInfo* info, | 863 FunctionLiteral* ParseLazy(Isolate* isolate, ParseInfo* info, |
677 Utf16CharacterStream* source); | 864 Utf16CharacterStream* source); |
678 | 865 |
679 // Called by ParseProgram after setting up the scanner. | 866 // Called by ParseProgram after setting up the scanner. |
680 FunctionLiteral* DoParseProgram(CompilationInfo* info, Scope** scope, | 867 FunctionLiteral* DoParseProgram(ParseInfo* info, Scope** scope, |
681 Scope** ad_hoc_eval_scope); | 868 Scope** ad_hoc_eval_scope); |
682 | 869 |
683 void SetCachedData(CompilationInfo* info); | 870 void SetCachedData(ParseInfo* info); |
684 | 871 |
685 bool inside_with() const { return scope_->inside_with(); } | 872 bool inside_with() const { return scope_->inside_with(); } |
686 ScriptCompiler::CompileOptions compile_options() const { | 873 ScriptCompiler::CompileOptions compile_options() const { |
687 return compile_options_; | 874 return compile_options_; |
688 } | 875 } |
689 bool consume_cached_parse_data() const { | 876 bool consume_cached_parse_data() const { |
690 return compile_options_ == ScriptCompiler::kConsumeParserCache && | 877 return compile_options_ == ScriptCompiler::kConsumeParserCache && |
691 cached_parse_data_ != NULL; | 878 cached_parse_data_ != NULL; |
692 } | 879 } |
693 bool produce_cached_parse_data() const { | 880 bool produce_cached_parse_data() const { |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
941 } | 1128 } |
942 | 1129 |
943 | 1130 |
944 Expression* ParserTraits::CloseTemplateLiteral(TemplateLiteralState* state, | 1131 Expression* ParserTraits::CloseTemplateLiteral(TemplateLiteralState* state, |
945 int start, Expression* tag) { | 1132 int start, Expression* tag) { |
946 return parser_->CloseTemplateLiteral(state, start, tag); | 1133 return parser_->CloseTemplateLiteral(state, start, tag); |
947 } | 1134 } |
948 } } // namespace v8::internal | 1135 } } // namespace v8::internal |
949 | 1136 |
950 #endif // V8_PARSER_H_ | 1137 #endif // V8_PARSER_H_ |
OLD | NEW |