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 #include "src/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/ast/ast-expression-rewriter.h" | 10 #include "src/ast/ast-expression-rewriter.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 return factory()->NewDoExpression(block, var_tmp, kNoSourcePosition); | 214 return factory()->NewDoExpression(block, var_tmp, kNoSourcePosition); |
215 } | 215 } |
216 | 216 |
217 FunctionLiteral* Parser::DefaultConstructor(const AstRawString* name, | 217 FunctionLiteral* Parser::DefaultConstructor(const AstRawString* name, |
218 bool call_super, | 218 bool call_super, |
219 bool requires_class_field_init, | 219 bool requires_class_field_init, |
220 int pos, int end_pos, | 220 int pos, int end_pos, |
221 LanguageMode language_mode) { | 221 LanguageMode language_mode) { |
222 int materialized_literal_count = -1; | 222 int materialized_literal_count = -1; |
223 int expected_property_count = -1; | 223 int expected_property_count = -1; |
224 int parameter_count = 0; | 224 const int parameter_count = 0; |
225 if (name == nullptr) name = ast_value_factory()->empty_string(); | 225 if (name == nullptr) name = ast_value_factory()->empty_string(); |
226 | 226 |
227 FunctionKind kind = call_super ? FunctionKind::kDefaultSubclassConstructor | 227 FunctionKind kind = call_super ? FunctionKind::kDefaultSubclassConstructor |
228 : FunctionKind::kDefaultBaseConstructor; | 228 : FunctionKind::kDefaultBaseConstructor; |
229 DeclarationScope* function_scope = NewFunctionScope(kind); | 229 DeclarationScope* function_scope = NewFunctionScope(kind); |
230 SetLanguageMode(function_scope, | 230 SetLanguageMode(function_scope, |
231 static_cast<LanguageMode>(language_mode | STRICT)); | 231 static_cast<LanguageMode>(language_mode | STRICT)); |
232 // Set start and end position to the same value | 232 // Set start and end position to the same value |
233 function_scope->set_start_position(pos); | 233 function_scope->set_start_position(pos); |
234 function_scope->set_end_position(pos); | 234 function_scope->set_end_position(pos); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 277 } |
278 body->Add(factory()->NewReturnStatement(call, pos), zone()); | 278 body->Add(factory()->NewReturnStatement(call, pos), zone()); |
279 } | 279 } |
280 | 280 |
281 materialized_literal_count = function_state.materialized_literal_count(); | 281 materialized_literal_count = function_state.materialized_literal_count(); |
282 expected_property_count = function_state.expected_property_count(); | 282 expected_property_count = function_state.expected_property_count(); |
283 } | 283 } |
284 | 284 |
285 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 285 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
286 name, function_scope, body, materialized_literal_count, | 286 name, function_scope, body, materialized_literal_count, |
287 expected_property_count, parameter_count, | 287 expected_property_count, parameter_count, parameter_count, |
288 FunctionLiteral::kNoDuplicateParameters, | 288 FunctionLiteral::kNoDuplicateParameters, |
289 FunctionLiteral::kAnonymousExpression, default_eager_compile_hint(), pos); | 289 FunctionLiteral::kAnonymousExpression, default_eager_compile_hint(), pos); |
290 | 290 |
291 function_literal->set_requires_class_field_init(requires_class_field_init); | 291 function_literal->set_requires_class_field_init(requires_class_field_init); |
292 | 292 |
293 return function_literal; | 293 return function_literal; |
294 } | 294 } |
295 | 295 |
296 | 296 |
297 // ---------------------------------------------------------------------------- | 297 // ---------------------------------------------------------------------------- |
(...skipping 2198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2496 | 2496 |
2497 void Parser::DeclareArrowFunctionFormalParameters( | 2497 void Parser::DeclareArrowFunctionFormalParameters( |
2498 ParserFormalParameters* parameters, Expression* expr, | 2498 ParserFormalParameters* parameters, Expression* expr, |
2499 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 2499 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, |
2500 bool* ok) { | 2500 bool* ok) { |
2501 if (expr->IsEmptyParentheses()) return; | 2501 if (expr->IsEmptyParentheses()) return; |
2502 | 2502 |
2503 AddArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, | 2503 AddArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, |
2504 CHECK_OK_VOID); | 2504 CHECK_OK_VOID); |
2505 | 2505 |
2506 if (parameters->Arity() > Code::kMaxArguments) { | 2506 if (parameters->arity > Code::kMaxArguments) { |
2507 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); | 2507 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); |
2508 *ok = false; | 2508 *ok = false; |
2509 return; | 2509 return; |
2510 } | 2510 } |
2511 | 2511 |
2512 ExpressionClassifier classifier(this); | 2512 ExpressionClassifier classifier(this); |
2513 if (!parameters->is_simple) { | 2513 if (!parameters->is_simple) { |
2514 this->classifier()->RecordNonSimpleParameter(); | 2514 this->classifier()->RecordNonSimpleParameter(); |
2515 } | 2515 } |
2516 for (int i = 0; i < parameters->Arity(); ++i) { | 2516 for (int i = 0; i < parameters->arity; ++i) { |
2517 auto parameter = parameters->at(i); | 2517 auto parameter = parameters->at(i); |
2518 DeclareFormalParameter(parameters->scope, parameter); | 2518 DeclareFormalParameter(parameters->scope, parameter); |
2519 if (!this->classifier() | 2519 if (!this->classifier() |
2520 ->is_valid_formal_parameter_list_without_duplicates() && | 2520 ->is_valid_formal_parameter_list_without_duplicates() && |
2521 !duplicate_loc->IsValid()) { | 2521 !duplicate_loc->IsValid()) { |
2522 *duplicate_loc = | 2522 *duplicate_loc = |
2523 this->classifier()->duplicate_formal_parameter_error().location; | 2523 this->classifier()->duplicate_formal_parameter_error().location; |
2524 } | 2524 } |
2525 } | 2525 } |
2526 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters()); | 2526 DCHECK_EQ(parameters->is_simple, parameters->scope->has_simple_parameters()); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2656 eager_compile_hint == FunctionLiteral::kShouldLazyCompile)) && | 2656 eager_compile_hint == FunctionLiteral::kShouldLazyCompile)) && |
2657 !(FLAG_validate_asm && scope()->IsAsmModule()); | 2657 !(FLAG_validate_asm && scope()->IsAsmModule()); |
2658 bool is_lazy_inner_function = | 2658 bool is_lazy_inner_function = |
2659 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function; | 2659 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function; |
2660 | 2660 |
2661 // This Scope lives in the main zone. We'll migrate data into that zone later. | 2661 // This Scope lives in the main zone. We'll migrate data into that zone later. |
2662 DeclarationScope* scope = NewFunctionScope(kind); | 2662 DeclarationScope* scope = NewFunctionScope(kind); |
2663 SetLanguageMode(scope, language_mode); | 2663 SetLanguageMode(scope, language_mode); |
2664 | 2664 |
2665 ZoneList<Statement*>* body = nullptr; | 2665 ZoneList<Statement*>* body = nullptr; |
2666 int arity = -1; | |
2667 int materialized_literal_count = -1; | 2666 int materialized_literal_count = -1; |
2668 int expected_property_count = -1; | 2667 int expected_property_count = -1; |
2669 DuplicateFinder duplicate_finder(scanner()->unicode_cache()); | 2668 DuplicateFinder duplicate_finder(scanner()->unicode_cache()); |
2670 bool should_be_used_once_hint = false; | 2669 bool should_be_used_once_hint = false; |
2671 bool has_duplicate_parameters; | 2670 bool has_duplicate_parameters; |
2672 | 2671 |
2673 FunctionState function_state(&function_state_, &scope_state_, scope); | 2672 FunctionState function_state(&function_state_, &scope_state_, scope); |
2674 #ifdef DEBUG | 2673 #ifdef DEBUG |
2675 scope->SetScopeName(function_name); | 2674 scope->SetScopeName(function_name); |
2676 #endif | 2675 #endif |
2677 | 2676 |
2678 ExpressionClassifier formals_classifier(this, &duplicate_finder); | 2677 ExpressionClassifier formals_classifier(this, &duplicate_finder); |
2679 | 2678 |
2680 if (is_generator) PrepareGeneratorVariables(&function_state); | 2679 if (is_generator) PrepareGeneratorVariables(&function_state); |
2681 | 2680 |
2682 Expect(Token::LPAREN, CHECK_OK); | 2681 Expect(Token::LPAREN, CHECK_OK); |
2683 int start_position = scanner()->location().beg_pos; | 2682 int start_position = scanner()->location().beg_pos; |
2684 this->scope()->set_start_position(start_position); | 2683 this->scope()->set_start_position(start_position); |
2685 ParserFormalParameters formals(scope); | 2684 ParserFormalParameters formals(scope); |
2686 ParseFormalParameterList(&formals, CHECK_OK); | 2685 ParseFormalParameterList(&formals, CHECK_OK); |
2687 arity = formals.Arity(); | |
2688 Expect(Token::RPAREN, CHECK_OK); | 2686 Expect(Token::RPAREN, CHECK_OK); |
2689 int formals_end_position = scanner()->location().end_pos; | 2687 int formals_end_position = scanner()->location().end_pos; |
2690 | 2688 |
2691 CheckArityRestrictions(arity, kind, formals.has_rest, start_position, | 2689 CheckArityRestrictions(formals.arity, kind, formals.has_rest, start_position, |
2692 formals_end_position, CHECK_OK); | 2690 formals_end_position, CHECK_OK); |
2693 Expect(Token::LBRACE, CHECK_OK); | 2691 Expect(Token::LBRACE, CHECK_OK); |
2694 // Don't include the rest parameter into the function's formal parameter | |
2695 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, | |
2696 // which says whether we need to create an arguments adaptor frame). | |
2697 if (formals.has_rest) arity--; | |
2698 | 2692 |
2699 { | 2693 { |
2700 // Temporary zones can nest. When we migrate free variables (see below), we | 2694 // Temporary zones can nest. When we migrate free variables (see below), we |
2701 // need to recreate them in the previous Zone. | 2695 // need to recreate them in the previous Zone. |
2702 AstNodeFactory previous_zone_ast_node_factory(ast_value_factory()); | 2696 AstNodeFactory previous_zone_ast_node_factory(ast_value_factory()); |
2703 previous_zone_ast_node_factory.set_zone(zone()); | 2697 previous_zone_ast_node_factory.set_zone(zone()); |
2704 | 2698 |
2705 // Open a new zone scope, which sets our AstNodeFactory to allocate in the | 2699 // Open a new zone scope, which sets our AstNodeFactory to allocate in the |
2706 // new temporary zone if the preconditions are satisfied, and ensures that | 2700 // new temporary zone if the preconditions are satisfied, and ensures that |
2707 // the previous zone is always restored after parsing the body. To be able | 2701 // the previous zone is always restored after parsing the body. To be able |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2788 !classifier()->is_valid_formal_parameter_list_without_duplicates(); | 2782 !classifier()->is_valid_formal_parameter_list_without_duplicates(); |
2789 } // DiscardableZoneScope goes out of scope. | 2783 } // DiscardableZoneScope goes out of scope. |
2790 | 2784 |
2791 FunctionLiteral::ParameterFlag duplicate_parameters = | 2785 FunctionLiteral::ParameterFlag duplicate_parameters = |
2792 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters | 2786 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters |
2793 : FunctionLiteral::kNoDuplicateParameters; | 2787 : FunctionLiteral::kNoDuplicateParameters; |
2794 | 2788 |
2795 // Note that the FunctionLiteral needs to be created in the main Zone again. | 2789 // Note that the FunctionLiteral needs to be created in the main Zone again. |
2796 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 2790 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
2797 function_name, scope, body, materialized_literal_count, | 2791 function_name, scope, body, materialized_literal_count, |
2798 expected_property_count, arity, duplicate_parameters, function_type, | 2792 expected_property_count, formals.num_parameters(), |
| 2793 formals.function_length, duplicate_parameters, function_type, |
2799 eager_compile_hint, pos); | 2794 eager_compile_hint, pos); |
2800 function_literal->set_function_token_position(function_token_pos); | 2795 function_literal->set_function_token_position(function_token_pos); |
2801 if (should_be_used_once_hint) | 2796 if (should_be_used_once_hint) |
2802 function_literal->set_should_be_used_once_hint(); | 2797 function_literal->set_should_be_used_once_hint(); |
2803 | 2798 |
2804 if (should_infer_name) { | 2799 if (should_infer_name) { |
2805 DCHECK_NOT_NULL(fni_); | 2800 DCHECK_NOT_NULL(fni_); |
2806 fni_->AddFunction(function_literal); | 2801 fni_->AddFunction(function_literal); |
2807 } | 2802 } |
2808 return function_literal; | 2803 return function_literal; |
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3437 kNoSourcePosition), | 3432 kNoSourcePosition), |
3438 kNoSourcePosition), | 3433 kNoSourcePosition), |
3439 zone()); | 3434 zone()); |
3440 } | 3435 } |
3441 body->Add(factory()->NewReturnStatement(ThisExpression(kNoSourcePosition), | 3436 body->Add(factory()->NewReturnStatement(ThisExpression(kNoSourcePosition), |
3442 kNoSourcePosition), | 3437 kNoSourcePosition), |
3443 zone()); | 3438 zone()); |
3444 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 3439 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
3445 ast_value_factory()->empty_string(), initializer_scope, body, | 3440 ast_value_factory()->empty_string(), initializer_scope, body, |
3446 initializer_state.materialized_literal_count(), | 3441 initializer_state.materialized_literal_count(), |
3447 initializer_state.expected_property_count(), 0, | 3442 initializer_state.expected_property_count(), 0, count, |
3448 FunctionLiteral::kNoDuplicateParameters, | 3443 FunctionLiteral::kNoDuplicateParameters, |
3449 FunctionLiteral::kAnonymousExpression, | 3444 FunctionLiteral::kAnonymousExpression, |
3450 FunctionLiteral::kShouldLazyCompile, initializer_scope->start_position()); | 3445 FunctionLiteral::kShouldLazyCompile, initializer_scope->start_position()); |
3451 function_literal->set_is_class_field_initializer(true); | 3446 function_literal->set_is_class_field_initializer(true); |
3452 function_literal->scope()->set_arity(count); | |
3453 return function_literal; | 3447 return function_literal; |
3454 } | 3448 } |
3455 | 3449 |
3456 FunctionLiteral* Parser::InsertClassFieldInitializer( | 3450 FunctionLiteral* Parser::InsertClassFieldInitializer( |
3457 FunctionLiteral* constructor) { | 3451 FunctionLiteral* constructor) { |
3458 Statement* call_initializer = factory()->NewExpressionStatement( | 3452 Statement* call_initializer = factory()->NewExpressionStatement( |
3459 CallClassFieldInitializer( | 3453 CallClassFieldInitializer( |
3460 constructor->scope(), | 3454 constructor->scope(), |
3461 constructor->scope()->NewUnresolved( | 3455 constructor->scope()->NewUnresolved( |
3462 factory(), ast_value_factory()->this_string(), kNoSourcePosition, | 3456 factory(), ast_value_factory()->this_string(), kNoSourcePosition, |
(...skipping 1992 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5455 | 5449 |
5456 return final_loop; | 5450 return final_loop; |
5457 } | 5451 } |
5458 | 5452 |
5459 #undef CHECK_OK | 5453 #undef CHECK_OK |
5460 #undef CHECK_OK_VOID | 5454 #undef CHECK_OK_VOID |
5461 #undef CHECK_FAILED | 5455 #undef CHECK_FAILED |
5462 | 5456 |
5463 } // namespace internal | 5457 } // namespace internal |
5464 } // namespace v8 | 5458 } // namespace v8 |
OLD | NEW |