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_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_H |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 ast_value_factory_(ast_value_factory), | 183 ast_value_factory_(ast_value_factory), |
184 ast_node_factory_(ast_value_factory), | 184 ast_node_factory_(ast_value_factory), |
185 log_(log), | 185 log_(log), |
186 mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly. | 186 mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly. |
187 parsing_module_(false), | 187 parsing_module_(false), |
188 stack_limit_(stack_limit), | 188 stack_limit_(stack_limit), |
189 zone_(zone), | 189 zone_(zone), |
190 classifier_(nullptr), | 190 classifier_(nullptr), |
191 scanner_(scanner), | 191 scanner_(scanner), |
192 stack_overflow_(false), | 192 stack_overflow_(false), |
| 193 default_eager_compile_hint_(FunctionLiteral::kShouldLazyCompile), |
193 allow_lazy_(false), | 194 allow_lazy_(false), |
194 allow_natives_(false), | 195 allow_natives_(false), |
195 allow_tailcalls_(false), | 196 allow_tailcalls_(false), |
196 allow_harmony_restrictive_declarations_(false), | 197 allow_harmony_restrictive_declarations_(false), |
197 allow_harmony_do_expressions_(false), | 198 allow_harmony_do_expressions_(false), |
198 allow_harmony_for_in_(false), | 199 allow_harmony_for_in_(false), |
199 allow_harmony_function_sent_(false), | 200 allow_harmony_function_sent_(false), |
200 allow_harmony_async_await_(false), | 201 allow_harmony_async_await_(false), |
201 allow_harmony_restrictive_generators_(false), | 202 allow_harmony_restrictive_generators_(false), |
202 allow_harmony_trailing_commas_(false), | 203 allow_harmony_trailing_commas_(false), |
(...skipping 14 matching lines...) Expand all Loading... |
217 ALLOW_ACCESSORS(harmony_restrictive_generators); | 218 ALLOW_ACCESSORS(harmony_restrictive_generators); |
218 ALLOW_ACCESSORS(harmony_trailing_commas); | 219 ALLOW_ACCESSORS(harmony_trailing_commas); |
219 ALLOW_ACCESSORS(harmony_class_fields); | 220 ALLOW_ACCESSORS(harmony_class_fields); |
220 | 221 |
221 #undef ALLOW_ACCESSORS | 222 #undef ALLOW_ACCESSORS |
222 | 223 |
223 uintptr_t stack_limit() const { return stack_limit_; } | 224 uintptr_t stack_limit() const { return stack_limit_; } |
224 | 225 |
225 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } | 226 void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } |
226 | 227 |
| 228 void set_default_eager_compile_hint( |
| 229 FunctionLiteral::EagerCompileHint eager_compile_hint) { |
| 230 default_eager_compile_hint_ = eager_compile_hint; |
| 231 } |
| 232 |
| 233 FunctionLiteral::EagerCompileHint default_eager_compile_hint() const { |
| 234 return default_eager_compile_hint_; |
| 235 } |
| 236 |
227 Zone* zone() const { return zone_; } | 237 Zone* zone() const { return zone_; } |
228 | 238 |
229 protected: | 239 protected: |
230 friend class v8::internal::ExpressionClassifier<ParserTypes<Impl>>; | 240 friend class v8::internal::ExpressionClassifier<ParserTypes<Impl>>; |
231 | 241 |
232 // clang-format off | |
233 enum AllowRestrictedIdentifiers { | 242 enum AllowRestrictedIdentifiers { |
234 kAllowRestrictedIdentifiers, | 243 kAllowRestrictedIdentifiers, |
235 kDontAllowRestrictedIdentifiers | 244 kDontAllowRestrictedIdentifiers |
236 }; | 245 }; |
237 | 246 |
238 enum Mode { | 247 enum Mode { PARSE_LAZILY, PARSE_EAGERLY }; |
239 PARSE_LAZILY, | |
240 PARSE_EAGERLY | |
241 }; | |
242 | 248 |
243 enum LazyParsingResult { | 249 enum LazyParsingResult { kLazyParsingComplete, kLazyParsingAborted }; |
244 kLazyParsingComplete, | |
245 kLazyParsingAborted | |
246 }; | |
247 | 250 |
248 enum VariableDeclarationContext { | 251 enum VariableDeclarationContext { |
249 kStatementListItem, | 252 kStatementListItem, |
250 kStatement, | 253 kStatement, |
251 kForStatement | 254 kForStatement |
252 }; | 255 }; |
253 | 256 |
254 enum class FunctionBodyType { | 257 enum class FunctionBodyType { kNormal, kSingleExpression }; |
255 kNormal, | |
256 kSingleExpression | |
257 }; | |
258 // clang-format on | |
259 | 258 |
260 class Checkpoint; | 259 class Checkpoint; |
261 class ClassLiteralChecker; | 260 class ClassLiteralChecker; |
262 class ObjectLiteralChecker; | 261 class ObjectLiteralChecker; |
263 | 262 |
264 // --------------------------------------------------------------------------- | 263 // --------------------------------------------------------------------------- |
265 // ScopeState and its subclasses implement the parser's scope stack. | 264 // ScopeState and its subclasses implement the parser's scope stack. |
266 // ScopeState keeps track of the current scope, and the outer ScopeState. The | 265 // ScopeState keeps track of the current scope, and the outer ScopeState. The |
267 // parser's scope_state_ points to the top ScopeState. ScopeState's | 266 // parser's scope_state_ points to the top ScopeState. ScopeState's |
268 // constructor push on the scope stack and the destructors pop. BlockState and | 267 // constructor push on the scope stack and the destructors pop. BlockState and |
(...skipping 1168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1437 | 1436 |
1438 // Parser base's private field members. | 1437 // Parser base's private field members. |
1439 | 1438 |
1440 private: | 1439 private: |
1441 Zone* zone_; | 1440 Zone* zone_; |
1442 ExpressionClassifier* classifier_; | 1441 ExpressionClassifier* classifier_; |
1443 | 1442 |
1444 Scanner* scanner_; | 1443 Scanner* scanner_; |
1445 bool stack_overflow_; | 1444 bool stack_overflow_; |
1446 | 1445 |
| 1446 FunctionLiteral::EagerCompileHint default_eager_compile_hint_; |
| 1447 |
1447 bool allow_lazy_; | 1448 bool allow_lazy_; |
1448 bool allow_natives_; | 1449 bool allow_natives_; |
1449 bool allow_tailcalls_; | 1450 bool allow_tailcalls_; |
1450 bool allow_harmony_restrictive_declarations_; | 1451 bool allow_harmony_restrictive_declarations_; |
1451 bool allow_harmony_do_expressions_; | 1452 bool allow_harmony_do_expressions_; |
1452 bool allow_harmony_for_in_; | 1453 bool allow_harmony_for_in_; |
1453 bool allow_harmony_function_sent_; | 1454 bool allow_harmony_function_sent_; |
1454 bool allow_harmony_async_await_; | 1455 bool allow_harmony_async_await_; |
1455 bool allow_harmony_restrictive_generators_; | 1456 bool allow_harmony_restrictive_generators_; |
1456 bool allow_harmony_trailing_commas_; | 1457 bool allow_harmony_trailing_commas_; |
(...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2281 value = factory()->NewUndefinedLiteral(kNoSourcePosition); | 2282 value = factory()->NewUndefinedLiteral(kNoSourcePosition); |
2282 } | 2283 } |
2283 initializer_scope->set_end_position(scanner()->location().end_pos); | 2284 initializer_scope->set_end_position(scanner()->location().end_pos); |
2284 typename Types::StatementList body = impl()->NewStatementList(1); | 2285 typename Types::StatementList body = impl()->NewStatementList(1); |
2285 body->Add(factory()->NewReturnStatement(value, kNoSourcePosition), zone()); | 2286 body->Add(factory()->NewReturnStatement(value, kNoSourcePosition), zone()); |
2286 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( | 2287 FunctionLiteralT function_literal = factory()->NewFunctionLiteral( |
2287 impl()->EmptyIdentifierString(), initializer_scope, body, | 2288 impl()->EmptyIdentifierString(), initializer_scope, body, |
2288 initializer_state.materialized_literal_count(), | 2289 initializer_state.materialized_literal_count(), |
2289 initializer_state.expected_property_count(), 0, | 2290 initializer_state.expected_property_count(), 0, |
2290 FunctionLiteral::kNoDuplicateParameters, | 2291 FunctionLiteral::kNoDuplicateParameters, |
2291 FunctionLiteral::kAnonymousExpression, | 2292 FunctionLiteral::kAnonymousExpression, default_eager_compile_hint_, |
2292 FunctionLiteral::kShouldLazyCompile, initializer_scope->start_position()); | 2293 initializer_scope->start_position()); |
2293 function_literal->set_is_class_field_initializer(true); | 2294 function_literal->set_is_class_field_initializer(true); |
2294 return function_literal; | 2295 return function_literal; |
2295 } | 2296 } |
2296 | 2297 |
2297 template <typename Impl> | 2298 template <typename Impl> |
2298 typename ParserBase<Impl>::ObjectLiteralPropertyT | 2299 typename ParserBase<Impl>::ObjectLiteralPropertyT |
2299 ParserBase<Impl>::ParseObjectPropertyDefinition(ObjectLiteralChecker* checker, | 2300 ParserBase<Impl>::ParseObjectPropertyDefinition(ObjectLiteralChecker* checker, |
2300 bool* is_computed_name, | 2301 bool* is_computed_name, |
2301 bool* ok) { | 2302 bool* ok) { |
2302 bool is_get = false; | 2303 bool is_get = false; |
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3076 // For other kinds of calls we record position of the parenthesis as | 3077 // For other kinds of calls we record position of the parenthesis as |
3077 // position of the call. Note that this is extremely important for | 3078 // position of the call. Note that this is extremely important for |
3078 // expressions of the form function(){...}() for which call position | 3079 // expressions of the form function(){...}() for which call position |
3079 // should not point to the closing brace otherwise it will intersect | 3080 // should not point to the closing brace otherwise it will intersect |
3080 // with positions recorded for function literal and confuse debugger. | 3081 // with positions recorded for function literal and confuse debugger. |
3081 pos = peek_position(); | 3082 pos = peek_position(); |
3082 // Also the trailing parenthesis are a hint that the function will | 3083 // Also the trailing parenthesis are a hint that the function will |
3083 // be called immediately. If we happen to have parsed a preceding | 3084 // be called immediately. If we happen to have parsed a preceding |
3084 // function literal eagerly, we can also compile it eagerly. | 3085 // function literal eagerly, we can also compile it eagerly. |
3085 if (result->IsFunctionLiteral() && mode() == PARSE_EAGERLY) { | 3086 if (result->IsFunctionLiteral() && mode() == PARSE_EAGERLY) { |
3086 result->AsFunctionLiteral()->set_should_eager_compile(); | 3087 result->AsFunctionLiteral()->SetShouldEagerCompile(); |
3087 } | 3088 } |
3088 } | 3089 } |
3089 Scanner::Location spread_pos; | 3090 Scanner::Location spread_pos; |
3090 ExpressionListT args; | 3091 ExpressionListT args; |
3091 if (V8_UNLIKELY(is_async && impl()->IsIdentifier(result))) { | 3092 if (V8_UNLIKELY(is_async && impl()->IsIdentifier(result))) { |
3092 ExpressionClassifier async_classifier(this); | 3093 ExpressionClassifier async_classifier(this); |
3093 args = ParseArguments(&spread_pos, true, CHECK_OK); | 3094 args = ParseArguments(&spread_pos, true, CHECK_OK); |
3094 if (peek() == Token::ARROW) { | 3095 if (peek() == Token::ARROW) { |
3095 if (fni_) { | 3096 if (fni_) { |
3096 fni_->RemoveAsyncKeywordFromEnd(); | 3097 fni_->RemoveAsyncKeywordFromEnd(); |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3408 BindingPatternUnexpectedToken(); | 3409 BindingPatternUnexpectedToken(); |
3409 ArrowFormalParametersUnexpectedToken(); | 3410 ArrowFormalParametersUnexpectedToken(); |
3410 int pos; | 3411 int pos; |
3411 if (scanner()->current_token() == Token::IDENTIFIER) { | 3412 if (scanner()->current_token() == Token::IDENTIFIER) { |
3412 pos = position(); | 3413 pos = position(); |
3413 } else { | 3414 } else { |
3414 pos = peek_position(); | 3415 pos = peek_position(); |
3415 if (expression->IsFunctionLiteral() && mode() == PARSE_EAGERLY) { | 3416 if (expression->IsFunctionLiteral() && mode() == PARSE_EAGERLY) { |
3416 // If the tag function looks like an IIFE, set_parenthesized() to | 3417 // If the tag function looks like an IIFE, set_parenthesized() to |
3417 // force eager compilation. | 3418 // force eager compilation. |
3418 expression->AsFunctionLiteral()->set_should_eager_compile(); | 3419 expression->AsFunctionLiteral()->SetShouldEagerCompile(); |
3419 } | 3420 } |
3420 } | 3421 } |
3421 expression = ParseTemplateLiteral(expression, pos, CHECK_OK); | 3422 expression = ParseTemplateLiteral(expression, pos, CHECK_OK); |
3422 break; | 3423 break; |
3423 } | 3424 } |
3424 case Token::ILLEGAL: { | 3425 case Token::ILLEGAL: { |
3425 ReportUnexpectedTokenAt(scanner()->peek_location(), Token::ILLEGAL); | 3426 ReportUnexpectedTokenAt(scanner()->peek_location(), Token::ILLEGAL); |
3426 *ok = false; | 3427 *ok = false; |
3427 return impl()->EmptyExpression(); | 3428 return impl()->EmptyExpression(); |
3428 } | 3429 } |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3913 return impl()->EmptyExpression(); | 3914 return impl()->EmptyExpression(); |
3914 } | 3915 } |
3915 | 3916 |
3916 StatementListT body = impl()->NullStatementList(); | 3917 StatementListT body = impl()->NullStatementList(); |
3917 int num_parameters = formal_parameters.scope->num_parameters(); | 3918 int num_parameters = formal_parameters.scope->num_parameters(); |
3918 int materialized_literal_count = -1; | 3919 int materialized_literal_count = -1; |
3919 int expected_property_count = -1; | 3920 int expected_property_count = -1; |
3920 | 3921 |
3921 FunctionKind kind = formal_parameters.scope->function_kind(); | 3922 FunctionKind kind = formal_parameters.scope->function_kind(); |
3922 FunctionLiteral::EagerCompileHint eager_compile_hint = | 3923 FunctionLiteral::EagerCompileHint eager_compile_hint = |
3923 FunctionLiteral::kShouldLazyCompile; | 3924 default_eager_compile_hint_; |
3924 bool should_be_used_once_hint = false; | 3925 bool should_be_used_once_hint = false; |
3925 { | 3926 { |
3926 FunctionState function_state(&function_state_, &scope_state_, | 3927 FunctionState function_state(&function_state_, &scope_state_, |
3927 formal_parameters.scope); | 3928 formal_parameters.scope); |
3928 | 3929 |
3929 function_state.SkipMaterializedLiterals( | 3930 function_state.SkipMaterializedLiterals( |
3930 formal_parameters.materialized_literals_count); | 3931 formal_parameters.materialized_literals_count); |
3931 | 3932 |
3932 impl()->ReindexLiterals(formal_parameters); | 3933 impl()->ReindexLiterals(formal_parameters); |
3933 | 3934 |
(...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4685 template <typename Impl> | 4686 template <typename Impl> |
4686 typename ParserBase<Impl>::StatementT | 4687 typename ParserBase<Impl>::StatementT |
4687 ParserBase<Impl>::ParseExpressionOrLabelledStatement( | 4688 ParserBase<Impl>::ParseExpressionOrLabelledStatement( |
4688 ZoneList<const AstRawString*>* labels, | 4689 ZoneList<const AstRawString*>* labels, |
4689 AllowLabelledFunctionStatement allow_function, bool* ok) { | 4690 AllowLabelledFunctionStatement allow_function, bool* ok) { |
4690 // ExpressionStatement | LabelledStatement :: | 4691 // ExpressionStatement | LabelledStatement :: |
4691 // Expression ';' | 4692 // Expression ';' |
4692 // Identifier ':' Statement | 4693 // Identifier ':' Statement |
4693 // | 4694 // |
4694 // ExpressionStatement[Yield] : | 4695 // ExpressionStatement[Yield] : |
4695 // [lookahead ∉ {{, function, class, let [}] Expression[In, ?Yield] ; | 4696 // [lookahead notin {{, function, class, let [}] Expression[In, ?Yield] ; |
4696 | 4697 |
4697 int pos = peek_position(); | 4698 int pos = peek_position(); |
4698 | 4699 |
4699 switch (peek()) { | 4700 switch (peek()) { |
4700 case Token::FUNCTION: | 4701 case Token::FUNCTION: |
4701 case Token::LBRACE: | 4702 case Token::LBRACE: |
4702 UNREACHABLE(); // Always handled by the callers. | 4703 UNREACHABLE(); // Always handled by the callers. |
4703 case Token::CLASS: | 4704 case Token::CLASS: |
4704 ReportUnexpectedToken(Next()); | 4705 ReportUnexpectedToken(Next()); |
4705 *ok = false; | 4706 *ok = false; |
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5454 has_seen_constructor_ = true; | 5455 has_seen_constructor_ = true; |
5455 return; | 5456 return; |
5456 } | 5457 } |
5457 } | 5458 } |
5458 | 5459 |
5459 | 5460 |
5460 } // namespace internal | 5461 } // namespace internal |
5461 } // namespace v8 | 5462 } // namespace v8 |
5462 | 5463 |
5463 #endif // V8_PARSING_PARSER_BASE_H | 5464 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |