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_PARSING_PARSER_H_ | 5 #ifndef V8_PARSING_PARSER_H_ |
6 #define V8_PARSING_PARSER_H_ | 6 #define V8_PARSING_PARSER_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/ast/ast.h" | 9 #include "src/ast/ast.h" |
10 #include "src/ast/scopes.h" | 10 #include "src/ast/scopes.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 void set_extension(v8::Extension* extension) { extension_ = extension; } | 97 void set_extension(v8::Extension* extension) { extension_ = extension; } |
98 | 98 |
99 ScriptData** cached_data() { return cached_data_; } | 99 ScriptData** cached_data() { return cached_data_; } |
100 void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; } | 100 void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; } |
101 | 101 |
102 ScriptCompiler::CompileOptions compile_options() { return compile_options_; } | 102 ScriptCompiler::CompileOptions compile_options() { return compile_options_; } |
103 void set_compile_options(ScriptCompiler::CompileOptions compile_options) { | 103 void set_compile_options(ScriptCompiler::CompileOptions compile_options) { |
104 compile_options_ = compile_options; | 104 compile_options_ = compile_options; |
105 } | 105 } |
106 | 106 |
107 Scope* script_scope() { return script_scope_; } | 107 DeclarationScope* script_scope() { return script_scope_; } |
108 void set_script_scope(Scope* script_scope) { script_scope_ = script_scope; } | 108 void set_script_scope(DeclarationScope* script_scope) { |
| 109 script_scope_ = script_scope; |
| 110 } |
109 | 111 |
110 AstValueFactory* ast_value_factory() { return ast_value_factory_; } | 112 AstValueFactory* ast_value_factory() { return ast_value_factory_; } |
111 void set_ast_value_factory(AstValueFactory* ast_value_factory) { | 113 void set_ast_value_factory(AstValueFactory* ast_value_factory) { |
112 ast_value_factory_ = ast_value_factory; | 114 ast_value_factory_ = ast_value_factory; |
113 } | 115 } |
114 | 116 |
115 FunctionLiteral* literal() { return literal_; } | 117 FunctionLiteral* literal() { return literal_; } |
116 void set_literal(FunctionLiteral* literal) { literal_ = literal; } | 118 void set_literal(FunctionLiteral* literal) { literal_ = literal; } |
117 | 119 |
118 Scope* scope() { return scope_; } | 120 DeclarationScope* scope() { return scope_; } |
119 void set_scope(Scope* scope) { scope_ = scope; } | 121 void set_scope(DeclarationScope* scope) { scope_ = scope; } |
120 | 122 |
121 UnicodeCache* unicode_cache() { return unicode_cache_; } | 123 UnicodeCache* unicode_cache() { return unicode_cache_; } |
122 void set_unicode_cache(UnicodeCache* unicode_cache) { | 124 void set_unicode_cache(UnicodeCache* unicode_cache) { |
123 unicode_cache_ = unicode_cache; | 125 unicode_cache_ = unicode_cache; |
124 } | 126 } |
125 | 127 |
126 uintptr_t stack_limit() { return stack_limit_; } | 128 uintptr_t stack_limit() { return stack_limit_; } |
127 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } | 129 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } |
128 | 130 |
129 uint32_t hash_seed() { return hash_seed_; } | 131 uint32_t hash_seed() { return hash_seed_; } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 }; | 203 }; |
202 | 204 |
203 //------------- Inputs to parsing and scope analysis ----------------------- | 205 //------------- Inputs to parsing and scope analysis ----------------------- |
204 Zone* zone_; | 206 Zone* zone_; |
205 unsigned flags_; | 207 unsigned flags_; |
206 ScriptCompiler::ExternalSourceStream* source_stream_; | 208 ScriptCompiler::ExternalSourceStream* source_stream_; |
207 ScriptCompiler::StreamedSource::Encoding source_stream_encoding_; | 209 ScriptCompiler::StreamedSource::Encoding source_stream_encoding_; |
208 Utf16CharacterStream* character_stream_; | 210 Utf16CharacterStream* character_stream_; |
209 v8::Extension* extension_; | 211 v8::Extension* extension_; |
210 ScriptCompiler::CompileOptions compile_options_; | 212 ScriptCompiler::CompileOptions compile_options_; |
211 Scope* script_scope_; | 213 DeclarationScope* script_scope_; |
212 UnicodeCache* unicode_cache_; | 214 UnicodeCache* unicode_cache_; |
213 uintptr_t stack_limit_; | 215 uintptr_t stack_limit_; |
214 uint32_t hash_seed_; | 216 uint32_t hash_seed_; |
215 int compiler_hints_; | 217 int compiler_hints_; |
216 int start_position_; | 218 int start_position_; |
217 int end_position_; | 219 int end_position_; |
218 | 220 |
219 // TODO(titzer): Move handles and isolate out of ParseInfo. | 221 // TODO(titzer): Move handles and isolate out of ParseInfo. |
220 Isolate* isolate_; | 222 Isolate* isolate_; |
221 Handle<SharedFunctionInfo> shared_; | 223 Handle<SharedFunctionInfo> shared_; |
222 Handle<Script> script_; | 224 Handle<Script> script_; |
223 Handle<Context> context_; | 225 Handle<Context> context_; |
224 | 226 |
225 //----------- Inputs+Outputs of parsing and scope analysis ----------------- | 227 //----------- Inputs+Outputs of parsing and scope analysis ----------------- |
226 ScriptData** cached_data_; // used if available, populated if requested. | 228 ScriptData** cached_data_; // used if available, populated if requested. |
227 AstValueFactory* ast_value_factory_; // used if available, otherwise new. | 229 AstValueFactory* ast_value_factory_; // used if available, otherwise new. |
228 | 230 |
229 //----------- Outputs of parsing and scope analysis ------------------------ | 231 //----------- Outputs of parsing and scope analysis ------------------------ |
230 FunctionLiteral* literal_; // produced by full parser. | 232 FunctionLiteral* literal_; // produced by full parser. |
231 Scope* scope_; // produced by scope analysis. | 233 DeclarationScope* scope_; // produced by scope analysis. |
232 | 234 |
233 void SetFlag(Flag f) { flags_ |= f; } | 235 void SetFlag(Flag f) { flags_ |= f; } |
234 void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; } | 236 void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; } |
235 bool GetFlag(Flag f) const { return (flags_ & f) != 0; } | 237 bool GetFlag(Flag f) const { return (flags_ & f) != 0; } |
236 }; | 238 }; |
237 | 239 |
238 class FunctionEntry BASE_EMBEDDED { | 240 class FunctionEntry BASE_EMBEDDED { |
239 public: | 241 public: |
240 enum { | 242 enum { |
241 kStartPositionIndex, | 243 kStartPositionIndex, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 const AstRawString* name; | 336 const AstRawString* name; |
335 Expression* pattern; | 337 Expression* pattern; |
336 Expression* initializer; | 338 Expression* initializer; |
337 int initializer_end_position; | 339 int initializer_end_position; |
338 bool is_rest; | 340 bool is_rest; |
339 bool is_simple() const { | 341 bool is_simple() const { |
340 return pattern->IsVariableProxy() && initializer == nullptr && !is_rest; | 342 return pattern->IsVariableProxy() && initializer == nullptr && !is_rest; |
341 } | 343 } |
342 }; | 344 }; |
343 | 345 |
344 explicit ParserFormalParameters(Scope* scope) | 346 explicit ParserFormalParameters(DeclarationScope* scope) |
345 : FormalParametersBase(scope), params(4, scope->zone()) {} | 347 : FormalParametersBase(scope), params(4, scope->zone()) {} |
346 ZoneList<Parameter> params; | 348 ZoneList<Parameter> params; |
347 | 349 |
348 int Arity() const { return params.length(); } | 350 int Arity() const { return params.length(); } |
349 const Parameter& at(int i) const { return params[i]; } | 351 const Parameter& at(int i) const { return params[i]; } |
350 }; | 352 }; |
351 | 353 |
352 | 354 |
353 class ParserTraits { | 355 class ParserTraits { |
354 public: | 356 public: |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 | 572 |
571 V8_INLINE void AddParameterInitializationBlock( | 573 V8_INLINE void AddParameterInitializationBlock( |
572 const ParserFormalParameters& parameters, | 574 const ParserFormalParameters& parameters, |
573 ZoneList<v8::internal::Statement*>* body, bool is_async, bool* ok); | 575 ZoneList<v8::internal::Statement*>* body, bool is_async, bool* ok); |
574 | 576 |
575 void ParseAsyncArrowSingleExpressionBody( | 577 void ParseAsyncArrowSingleExpressionBody( |
576 ZoneList<Statement*>* body, bool accept_IN, | 578 ZoneList<Statement*>* body, bool accept_IN, |
577 Type::ExpressionClassifier* classifier, int pos, bool* ok); | 579 Type::ExpressionClassifier* classifier, int pos, bool* ok); |
578 | 580 |
579 V8_INLINE Scope* NewScope(ScopeType scope_type); | 581 V8_INLINE Scope* NewScope(ScopeType scope_type); |
580 V8_INLINE Scope* NewFunctionScope(FunctionKind kind); | 582 V8_INLINE DeclarationScope* NewFunctionScope(FunctionKind kind); |
581 V8_INLINE Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type); | 583 V8_INLINE Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type); |
582 | 584 |
583 V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters, | 585 V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters, |
584 Expression* pattern, | 586 Expression* pattern, |
585 Expression* initializer, | 587 Expression* initializer, |
586 int initializer_end_position, bool is_rest); | 588 int initializer_end_position, bool is_rest); |
587 V8_INLINE void DeclareFormalParameter( | 589 V8_INLINE void DeclareFormalParameter( |
588 Scope* scope, const ParserFormalParameters::Parameter& parameter, | 590 DeclarationScope* scope, |
| 591 const ParserFormalParameters::Parameter& parameter, |
589 Type::ExpressionClassifier* classifier); | 592 Type::ExpressionClassifier* classifier); |
590 void ParseArrowFunctionFormalParameters(ParserFormalParameters* parameters, | 593 void ParseArrowFunctionFormalParameters(ParserFormalParameters* parameters, |
591 Expression* params, int end_pos, | 594 Expression* params, int end_pos, |
592 bool* ok); | 595 bool* ok); |
593 void ParseArrowFunctionFormalParameterList( | 596 void ParseArrowFunctionFormalParameterList( |
594 ParserFormalParameters* parameters, Expression* params, | 597 ParserFormalParameters* parameters, Expression* params, |
595 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 598 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, |
596 const Scope::Snapshot& scope_snapshot, bool* ok); | 599 const Scope::Snapshot& scope_snapshot, bool* ok); |
597 | 600 |
598 V8_INLINE Expression* ParseAsyncFunctionExpression(bool* ok); | 601 V8_INLINE Expression* ParseAsyncFunctionExpression(bool* ok); |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 // The var declarations are hoisted to the function scope, but originate from | 1061 // The var declarations are hoisted to the function scope, but originate from |
1059 // a scope where the name has also been let bound or the var declaration is | 1062 // a scope where the name has also been let bound or the var declaration is |
1060 // hoisted over such a scope. | 1063 // hoisted over such a scope. |
1061 void CheckConflictingVarDeclarations(Scope* scope, bool* ok); | 1064 void CheckConflictingVarDeclarations(Scope* scope, bool* ok); |
1062 | 1065 |
1063 // Insert initializer statements for var-bindings shadowing parameter bindings | 1066 // Insert initializer statements for var-bindings shadowing parameter bindings |
1064 // from a non-simple parameter list. | 1067 // from a non-simple parameter list. |
1065 void InsertShadowingVarBindingInitializers(Block* block); | 1068 void InsertShadowingVarBindingInitializers(Block* block); |
1066 | 1069 |
1067 // Implement sloppy block-scoped functions, ES2015 Annex B 3.3 | 1070 // Implement sloppy block-scoped functions, ES2015 Annex B 3.3 |
1068 void InsertSloppyBlockFunctionVarBindings(Scope* scope, | 1071 void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope, |
1069 Scope* complex_params_scope, | 1072 Scope* complex_params_scope, |
1070 bool* ok); | 1073 bool* ok); |
1071 | 1074 |
1072 // Parser support | 1075 // Parser support |
1073 VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode); | 1076 VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode); |
1074 Variable* Declare(Declaration* declaration, | 1077 Variable* Declare(Declaration* declaration, |
1075 DeclarationDescriptor::Kind declaration_kind, bool resolve, | 1078 DeclarationDescriptor::Kind declaration_kind, bool resolve, |
1076 bool* ok, Scope* declaration_scope = nullptr); | 1079 bool* ok, Scope* declaration_scope = nullptr); |
1077 void DeclareImport(const AstRawString* local_name, int pos, bool* ok); | 1080 void DeclareImport(const AstRawString* local_name, int pos, bool* ok); |
1078 | 1081 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1180 } | 1183 } |
1181 | 1184 |
1182 Scope* ParserTraits::NewScopeWithParent(Scope* parent, ScopeType scope_type) { | 1185 Scope* ParserTraits::NewScopeWithParent(Scope* parent, ScopeType scope_type) { |
1183 return parser_->NewScopeWithParent(parent, scope_type); | 1186 return parser_->NewScopeWithParent(parent, scope_type); |
1184 } | 1187 } |
1185 | 1188 |
1186 Scope* ParserTraits::NewScope(ScopeType scope_type) { | 1189 Scope* ParserTraits::NewScope(ScopeType scope_type) { |
1187 return parser_->NewScope(scope_type); | 1190 return parser_->NewScope(scope_type); |
1188 } | 1191 } |
1189 | 1192 |
1190 Scope* ParserTraits::NewFunctionScope(FunctionKind kind) { | 1193 DeclarationScope* ParserTraits::NewFunctionScope(FunctionKind kind) { |
1191 return parser_->NewFunctionScope(kind); | 1194 return parser_->NewFunctionScope(kind); |
1192 } | 1195 } |
1193 | 1196 |
1194 const AstRawString* ParserTraits::EmptyIdentifierString() { | 1197 const AstRawString* ParserTraits::EmptyIdentifierString() { |
1195 return parser_->ast_value_factory()->empty_string(); | 1198 return parser_->ast_value_factory()->empty_string(); |
1196 } | 1199 } |
1197 | 1200 |
1198 | 1201 |
1199 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count, | 1202 void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count, |
1200 int* expected_property_count, bool* ok, | 1203 int* expected_property_count, bool* ok, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1296 bool is_simple = pattern->IsVariableProxy() && initializer == nullptr; | 1299 bool is_simple = pattern->IsVariableProxy() && initializer == nullptr; |
1297 const AstRawString* name = is_simple | 1300 const AstRawString* name = is_simple |
1298 ? pattern->AsVariableProxy()->raw_name() | 1301 ? pattern->AsVariableProxy()->raw_name() |
1299 : parser_->ast_value_factory()->empty_string(); | 1302 : parser_->ast_value_factory()->empty_string(); |
1300 parameters->params.Add( | 1303 parameters->params.Add( |
1301 ParserFormalParameters::Parameter(name, pattern, initializer, | 1304 ParserFormalParameters::Parameter(name, pattern, initializer, |
1302 initializer_end_position, is_rest), | 1305 initializer_end_position, is_rest), |
1303 parameters->scope->zone()); | 1306 parameters->scope->zone()); |
1304 } | 1307 } |
1305 | 1308 |
1306 | |
1307 void ParserTraits::DeclareFormalParameter( | 1309 void ParserTraits::DeclareFormalParameter( |
1308 Scope* scope, const ParserFormalParameters::Parameter& parameter, | 1310 DeclarationScope* scope, const ParserFormalParameters::Parameter& parameter, |
1309 Type::ExpressionClassifier* classifier) { | 1311 Type::ExpressionClassifier* classifier) { |
1310 bool is_duplicate = false; | 1312 bool is_duplicate = false; |
1311 bool is_simple = classifier->is_simple_parameter_list(); | 1313 bool is_simple = classifier->is_simple_parameter_list(); |
1312 auto name = is_simple || parameter.is_rest | 1314 auto name = is_simple || parameter.is_rest |
1313 ? parameter.name | 1315 ? parameter.name |
1314 : parser_->ast_value_factory()->empty_string(); | 1316 : parser_->ast_value_factory()->empty_string(); |
1315 auto mode = is_simple || parameter.is_rest ? VAR : TEMPORARY; | 1317 auto mode = is_simple || parameter.is_rest ? VAR : TEMPORARY; |
1316 if (!is_simple) scope->SetHasNonSimpleParameters(); | 1318 if (!is_simple) scope->SetHasNonSimpleParameters(); |
1317 bool is_optional = parameter.initializer != nullptr; | 1319 bool is_optional = parameter.initializer != nullptr; |
1318 Variable* var = | 1320 Variable* var = |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1354 | 1356 |
1355 DoExpression* ParserTraits::ParseDoExpression(bool* ok) { | 1357 DoExpression* ParserTraits::ParseDoExpression(bool* ok) { |
1356 return parser_->ParseDoExpression(ok); | 1358 return parser_->ParseDoExpression(ok); |
1357 } | 1359 } |
1358 | 1360 |
1359 | 1361 |
1360 } // namespace internal | 1362 } // namespace internal |
1361 } // namespace v8 | 1363 } // namespace v8 |
1362 | 1364 |
1363 #endif // V8_PARSING_PARSER_H_ | 1365 #endif // V8_PARSING_PARSER_H_ |
OLD | NEW |