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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 273 |
274 materialized_literal_count = function_state.materialized_literal_count(); | 274 materialized_literal_count = function_state.materialized_literal_count(); |
275 expected_property_count = function_state.expected_property_count(); | 275 expected_property_count = function_state.expected_property_count(); |
276 } | 276 } |
277 | 277 |
278 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 278 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
279 name, function_scope, body, materialized_literal_count, | 279 name, function_scope, body, materialized_literal_count, |
280 expected_property_count, parameter_count, parameter_count, | 280 expected_property_count, parameter_count, parameter_count, |
281 FunctionLiteral::kNoDuplicateParameters, | 281 FunctionLiteral::kNoDuplicateParameters, |
282 FunctionLiteral::kAnonymousExpression, default_eager_compile_hint(), pos, | 282 FunctionLiteral::kAnonymousExpression, default_eager_compile_hint(), pos, |
283 true); | 283 true, GetNextFunctionLiteralNum()); |
284 | 284 |
285 function_literal->set_requires_class_field_init(requires_class_field_init); | 285 function_literal->set_requires_class_field_init(requires_class_field_init); |
286 | 286 |
287 return function_literal; | 287 return function_literal; |
288 } | 288 } |
289 | 289 |
290 // ---------------------------------------------------------------------------- | 290 // ---------------------------------------------------------------------------- |
291 // The CHECK_OK macro is a convenient macro to enforce error | 291 // The CHECK_OK macro is a convenient macro to enforce error |
292 // handling for functions that may fail (by returning !*ok). | 292 // handling for functions that may fail (by returning !*ok). |
293 // | 293 // |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 | 722 |
723 | 723 |
724 FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) { | 724 FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) { |
725 // Note that this function can be called from the main thread or from a | 725 // Note that this function can be called from the main thread or from a |
726 // background thread. We should not access anything Isolate / heap dependent | 726 // background thread. We should not access anything Isolate / heap dependent |
727 // via ParseInfo, and also not pass it forward. | 727 // via ParseInfo, and also not pass it forward. |
728 DCHECK_NULL(scope_state_); | 728 DCHECK_NULL(scope_state_); |
729 DCHECK_NULL(target_stack_); | 729 DCHECK_NULL(target_stack_); |
730 | 730 |
731 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY); | 731 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY); |
| 732 ResetFunctionLiteralNum(); |
| 733 DCHECK(info->function_literal_num() == 0 || |
| 734 info->function_literal_num() == -1); |
732 | 735 |
733 FunctionLiteral* result = NULL; | 736 FunctionLiteral* result = NULL; |
734 { | 737 { |
735 Scope* outer = original_scope_; | 738 Scope* outer = original_scope_; |
736 DCHECK_NOT_NULL(outer); | 739 DCHECK_NOT_NULL(outer); |
737 parsing_module_ = info->is_module(); | 740 parsing_module_ = info->is_module(); |
738 if (info->is_eval()) { | 741 if (info->is_eval()) { |
739 outer = NewEvalScope(outer); | 742 outer = NewEvalScope(outer); |
740 } else if (parsing_module_) { | 743 } else if (parsing_module_) { |
741 DCHECK_EQ(outer, info->script_scope()); | 744 DCHECK_EQ(outer, info->script_scope()); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 const AstRawString* raw_name, | 885 const AstRawString* raw_name, |
883 Utf16CharacterStream* source) { | 886 Utf16CharacterStream* source) { |
884 scanner_.Initialize(source); | 887 scanner_.Initialize(source); |
885 DCHECK_NULL(scope_state_); | 888 DCHECK_NULL(scope_state_); |
886 DCHECK_NULL(target_stack_); | 889 DCHECK_NULL(target_stack_); |
887 | 890 |
888 DCHECK(ast_value_factory()); | 891 DCHECK(ast_value_factory()); |
889 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); | 892 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); |
890 fni_->PushEnclosingName(raw_name); | 893 fni_->PushEnclosingName(raw_name); |
891 | 894 |
| 895 ResetFunctionLiteralNum(); |
| 896 DCHECK_LT(0, info->function_literal_num()); |
| 897 SkipFunctionLiterals(info->function_literal_num() - 1); |
| 898 |
892 ParsingModeScope parsing_mode(this, PARSE_EAGERLY); | 899 ParsingModeScope parsing_mode(this, PARSE_EAGERLY); |
893 | 900 |
894 // Place holder for the result. | 901 // Place holder for the result. |
895 FunctionLiteral* result = nullptr; | 902 FunctionLiteral* result = nullptr; |
896 | 903 |
897 { | 904 { |
898 // Parse the function literal. | 905 // Parse the function literal. |
899 Scope* outer = original_scope_; | 906 Scope* outer = original_scope_; |
900 DeclarationScope* outer_function = outer->GetClosureScope(); | 907 DeclarationScope* outer_function = outer->GetClosureScope(); |
901 DCHECK(outer); | 908 DCHECK(outer); |
(...skipping 12 matching lines...) Expand all Loading... |
914 if (!Check(Token::ASYNC)) { | 921 if (!Check(Token::ASYNC)) { |
915 CHECK(stack_overflow()); | 922 CHECK(stack_overflow()); |
916 return nullptr; | 923 return nullptr; |
917 } | 924 } |
918 if (!(peek_any_identifier() || peek() == Token::LPAREN)) { | 925 if (!(peek_any_identifier() || peek() == Token::LPAREN)) { |
919 CHECK(stack_overflow()); | 926 CHECK(stack_overflow()); |
920 return nullptr; | 927 return nullptr; |
921 } | 928 } |
922 } | 929 } |
923 | 930 |
| 931 int function_literal_num = GetNextFunctionLiteralNum(); |
| 932 |
924 // TODO(adamk): We should construct this scope from the ScopeInfo. | 933 // TODO(adamk): We should construct this scope from the ScopeInfo. |
925 DeclarationScope* scope = NewFunctionScope(kind); | 934 DeclarationScope* scope = NewFunctionScope(kind); |
926 | 935 |
927 // These two bits only need to be explicitly set because we're | 936 // These two bits only need to be explicitly set because we're |
928 // not passing the ScopeInfo to the Scope constructor. | 937 // not passing the ScopeInfo to the Scope constructor. |
929 // TODO(adamk): Remove these calls once the above NewScope call | 938 // TODO(adamk): Remove these calls once the above NewScope call |
930 // passes the ScopeInfo. | 939 // passes the ScopeInfo. |
931 if (info->calls_eval()) { | 940 if (info->calls_eval()) { |
932 scope->RecordEvalCall(); | 941 scope->RecordEvalCall(); |
933 } | 942 } |
(...skipping 16 matching lines...) Expand all Loading... |
950 // BindingIdentifier | 959 // BindingIdentifier |
951 ParseFormalParameter(&formals, &ok); | 960 ParseFormalParameter(&formals, &ok); |
952 if (ok) DeclareFormalParameter(formals.scope, formals.at(0)); | 961 if (ok) DeclareFormalParameter(formals.scope, formals.at(0)); |
953 } | 962 } |
954 } | 963 } |
955 | 964 |
956 if (ok) { | 965 if (ok) { |
957 checkpoint.Restore(&formals.materialized_literals_count); | 966 checkpoint.Restore(&formals.materialized_literals_count); |
958 // Pass `accept_IN=true` to ParseArrowFunctionLiteral --- This should | 967 // Pass `accept_IN=true` to ParseArrowFunctionLiteral --- This should |
959 // not be observable, or else the preparser would have failed. | 968 // not be observable, or else the preparser would have failed. |
960 Expression* expression = ParseArrowFunctionLiteral(true, formals, &ok); | 969 Expression* expression = |
| 970 ParseArrowFunctionLiteral(true, formals, function_literal_num, &ok); |
961 if (ok) { | 971 if (ok) { |
962 // Scanning must end at the same position that was recorded | 972 // Scanning must end at the same position that was recorded |
963 // previously. If not, parsing has been interrupted due to a stack | 973 // previously. If not, parsing has been interrupted due to a stack |
964 // overflow, at which point the partially parsed arrow function | 974 // overflow, at which point the partially parsed arrow function |
965 // concise body happens to be a valid expression. This is a problem | 975 // concise body happens to be a valid expression. This is a problem |
966 // only for arrow functions with single expression bodies, since there | 976 // only for arrow functions with single expression bodies, since there |
967 // is no end token such as "}" for normal functions. | 977 // is no end token such as "}" for normal functions. |
968 if (scanner()->location().end_pos == info->end_position()) { | 978 if (scanner()->location().end_pos == info->end_position()) { |
969 // The pre-parser saw an arrow function here, so the full parser | 979 // The pre-parser saw an arrow function here, so the full parser |
970 // must produce a FunctionLiteral. | 980 // must produce a FunctionLiteral. |
(...skipping 1647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2618 scope->SetScopeName(function_name); | 2628 scope->SetScopeName(function_name); |
2619 #endif | 2629 #endif |
2620 | 2630 |
2621 ZoneList<Statement*>* body = nullptr; | 2631 ZoneList<Statement*>* body = nullptr; |
2622 int materialized_literal_count = -1; | 2632 int materialized_literal_count = -1; |
2623 int expected_property_count = -1; | 2633 int expected_property_count = -1; |
2624 bool should_be_used_once_hint = false; | 2634 bool should_be_used_once_hint = false; |
2625 int num_parameters = -1; | 2635 int num_parameters = -1; |
2626 int function_length = -1; | 2636 int function_length = -1; |
2627 bool has_duplicate_parameters = false; | 2637 bool has_duplicate_parameters = false; |
| 2638 int function_literal_num = GetNextFunctionLiteralNum(); |
2628 | 2639 |
2629 Expect(Token::LPAREN, CHECK_OK); | 2640 Expect(Token::LPAREN, CHECK_OK); |
2630 scope->set_start_position(scanner()->location().beg_pos); | 2641 scope->set_start_position(scanner()->location().beg_pos); |
2631 | 2642 |
2632 { | 2643 { |
2633 // Temporary zones can nest. When we migrate free variables (see below), we | 2644 // Temporary zones can nest. When we migrate free variables (see below), we |
2634 // need to recreate them in the previous Zone. | 2645 // need to recreate them in the previous Zone. |
2635 AstNodeFactory previous_zone_ast_node_factory(ast_value_factory()); | 2646 AstNodeFactory previous_zone_ast_node_factory(ast_value_factory()); |
2636 previous_zone_ast_node_factory.set_zone(zone()); | 2647 previous_zone_ast_node_factory.set_zone(zone()); |
2637 | 2648 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2724 } // DiscardableZoneScope goes out of scope. | 2735 } // DiscardableZoneScope goes out of scope. |
2725 | 2736 |
2726 FunctionLiteral::ParameterFlag duplicate_parameters = | 2737 FunctionLiteral::ParameterFlag duplicate_parameters = |
2727 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters | 2738 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters |
2728 : FunctionLiteral::kNoDuplicateParameters; | 2739 : FunctionLiteral::kNoDuplicateParameters; |
2729 | 2740 |
2730 // Note that the FunctionLiteral needs to be created in the main Zone again. | 2741 // Note that the FunctionLiteral needs to be created in the main Zone again. |
2731 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 2742 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
2732 function_name, scope, body, materialized_literal_count, | 2743 function_name, scope, body, materialized_literal_count, |
2733 expected_property_count, num_parameters, function_length, | 2744 expected_property_count, num_parameters, function_length, |
2734 duplicate_parameters, function_type, eager_compile_hint, pos, true); | 2745 duplicate_parameters, function_type, eager_compile_hint, pos, true, |
| 2746 function_literal_num); |
2735 function_literal->set_function_token_position(function_token_pos); | 2747 function_literal->set_function_token_position(function_token_pos); |
2736 if (should_be_used_once_hint) | 2748 if (should_be_used_once_hint) |
2737 function_literal->set_should_be_used_once_hint(); | 2749 function_literal->set_should_be_used_once_hint(); |
2738 | 2750 |
2739 if (should_infer_name) { | 2751 if (should_infer_name) { |
2740 DCHECK_NOT_NULL(fni_); | 2752 DCHECK_NOT_NULL(fni_); |
2741 fni_->AddFunction(function_literal); | 2753 fni_->AddFunction(function_literal); |
2742 } | 2754 } |
2743 return function_literal; | 2755 return function_literal; |
2744 } | 2756 } |
(...skipping 27 matching lines...) Expand all Loading... |
2772 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); | 2784 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); |
2773 *num_parameters = entry.num_parameters(); | 2785 *num_parameters = entry.num_parameters(); |
2774 *function_length = entry.function_length(); | 2786 *function_length = entry.function_length(); |
2775 *has_duplicate_parameters = entry.has_duplicate_parameters(); | 2787 *has_duplicate_parameters = entry.has_duplicate_parameters(); |
2776 *materialized_literal_count = entry.literal_count(); | 2788 *materialized_literal_count = entry.literal_count(); |
2777 *expected_property_count = entry.property_count(); | 2789 *expected_property_count = entry.property_count(); |
2778 SetLanguageMode(function_scope, entry.language_mode()); | 2790 SetLanguageMode(function_scope, entry.language_mode()); |
2779 if (entry.uses_super_property()) | 2791 if (entry.uses_super_property()) |
2780 function_scope->RecordSuperPropertyUsage(); | 2792 function_scope->RecordSuperPropertyUsage(); |
2781 if (entry.calls_eval()) function_scope->RecordEvalCall(); | 2793 if (entry.calls_eval()) function_scope->RecordEvalCall(); |
| 2794 SkipFunctionLiterals(entry.num_inner_functions()); |
2782 return kLazyParsingComplete; | 2795 return kLazyParsingComplete; |
2783 } | 2796 } |
2784 cached_parse_data_->Reject(); | 2797 cached_parse_data_->Reject(); |
2785 } | 2798 } |
2786 | 2799 |
2787 // With no cached data, we partially parse the function, without building an | 2800 // With no cached data, we partially parse the function, without building an |
2788 // AST. This gathers the data needed to build a lazy function. | 2801 // AST. This gathers the data needed to build a lazy function. |
2789 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.PreParse"); | 2802 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.PreParse"); |
2790 | 2803 |
2791 if (reusable_preparser_ == NULL) { | 2804 if (reusable_preparser_ == NULL) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2824 PreParserLogger* logger = reusable_preparser_->logger(); | 2837 PreParserLogger* logger = reusable_preparser_->logger(); |
2825 function_scope->set_end_position(logger->end()); | 2838 function_scope->set_end_position(logger->end()); |
2826 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); | 2839 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); |
2827 total_preparse_skipped_ += | 2840 total_preparse_skipped_ += |
2828 function_scope->end_position() - function_scope->start_position(); | 2841 function_scope->end_position() - function_scope->start_position(); |
2829 *num_parameters = logger->num_parameters(); | 2842 *num_parameters = logger->num_parameters(); |
2830 *function_length = logger->function_length(); | 2843 *function_length = logger->function_length(); |
2831 *has_duplicate_parameters = logger->has_duplicate_parameters(); | 2844 *has_duplicate_parameters = logger->has_duplicate_parameters(); |
2832 *materialized_literal_count = logger->literals(); | 2845 *materialized_literal_count = logger->literals(); |
2833 *expected_property_count = logger->properties(); | 2846 *expected_property_count = logger->properties(); |
| 2847 SkipFunctionLiterals(logger->num_inner_functions()); |
2834 if (!is_inner_function && produce_cached_parse_data()) { | 2848 if (!is_inner_function && produce_cached_parse_data()) { |
2835 DCHECK(log_); | 2849 DCHECK(log_); |
2836 log_->LogFunction( | 2850 log_->LogFunction( |
2837 function_scope->start_position(), function_scope->end_position(), | 2851 function_scope->start_position(), function_scope->end_position(), |
2838 *num_parameters, *function_length, *has_duplicate_parameters, | 2852 *num_parameters, *function_length, *has_duplicate_parameters, |
2839 *materialized_literal_count, *expected_property_count, language_mode(), | 2853 *materialized_literal_count, *expected_property_count, language_mode(), |
2840 function_scope->uses_super_property(), function_scope->calls_eval()); | 2854 function_scope->uses_super_property(), function_scope->calls_eval(), |
| 2855 logger->num_inner_functions()); |
2841 } | 2856 } |
2842 return kLazyParsingComplete; | 2857 return kLazyParsingComplete; |
2843 } | 2858 } |
2844 | 2859 |
2845 | 2860 |
2846 Statement* Parser::BuildAssertIsCoercible(Variable* var) { | 2861 Statement* Parser::BuildAssertIsCoercible(Variable* var) { |
2847 // if (var === null || var === undefined) | 2862 // if (var === null || var === undefined) |
2848 // throw /* type error kNonCoercible) */; | 2863 // throw /* type error kNonCoercible) */; |
2849 | 2864 |
2850 Expression* condition = factory()->NewBinaryOperation( | 2865 Expression* condition = factory()->NewBinaryOperation( |
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3409 body->Add(factory()->NewReturnStatement(ThisExpression(kNoSourcePosition), | 3424 body->Add(factory()->NewReturnStatement(ThisExpression(kNoSourcePosition), |
3410 kNoSourcePosition), | 3425 kNoSourcePosition), |
3411 zone()); | 3426 zone()); |
3412 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 3427 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
3413 ast_value_factory()->empty_string(), initializer_scope, body, | 3428 ast_value_factory()->empty_string(), initializer_scope, body, |
3414 initializer_state.materialized_literal_count(), | 3429 initializer_state.materialized_literal_count(), |
3415 initializer_state.expected_property_count(), 0, count, | 3430 initializer_state.expected_property_count(), 0, count, |
3416 FunctionLiteral::kNoDuplicateParameters, | 3431 FunctionLiteral::kNoDuplicateParameters, |
3417 FunctionLiteral::kAnonymousExpression, | 3432 FunctionLiteral::kAnonymousExpression, |
3418 FunctionLiteral::kShouldLazyCompile, initializer_scope->start_position(), | 3433 FunctionLiteral::kShouldLazyCompile, initializer_scope->start_position(), |
3419 true); | 3434 true, GetNextFunctionLiteralNum()); |
3420 function_literal->set_is_class_field_initializer(true); | 3435 function_literal->set_is_class_field_initializer(true); |
3421 return function_literal; | 3436 return function_literal; |
3422 } | 3437 } |
3423 | 3438 |
3424 FunctionLiteral* Parser::InsertClassFieldInitializer( | 3439 FunctionLiteral* Parser::InsertClassFieldInitializer( |
3425 FunctionLiteral* constructor) { | 3440 FunctionLiteral* constructor) { |
3426 Statement* call_initializer = factory()->NewExpressionStatement( | 3441 Statement* call_initializer = factory()->NewExpressionStatement( |
3427 CallClassFieldInitializer( | 3442 CallClassFieldInitializer( |
3428 constructor->scope(), | 3443 constructor->scope(), |
3429 constructor->scope()->NewUnresolved( | 3444 constructor->scope()->NewUnresolved( |
(...skipping 23 matching lines...) Expand all Loading... |
3453 } | 3468 } |
3454 | 3469 |
3455 // This method declares a property of the given class. It updates the | 3470 // This method declares a property of the given class. It updates the |
3456 // following fields of class_info, as appropriate: | 3471 // following fields of class_info, as appropriate: |
3457 // - constructor | 3472 // - constructor |
3458 // - static_initializer_var | 3473 // - static_initializer_var |
3459 // - instance_field_initializers | 3474 // - instance_field_initializers |
3460 // - properties | 3475 // - properties |
3461 void Parser::DeclareClassProperty(const AstRawString* class_name, | 3476 void Parser::DeclareClassProperty(const AstRawString* class_name, |
3462 ClassLiteralProperty* property, | 3477 ClassLiteralProperty* property, |
3463 ClassInfo* class_info, bool* ok) { | 3478 ClassLiteralProperty::Kind kind, |
| 3479 bool is_static, ClassInfo* class_info, |
| 3480 bool* ok) { |
3464 if (class_info->has_seen_constructor && class_info->constructor == nullptr) { | 3481 if (class_info->has_seen_constructor && class_info->constructor == nullptr) { |
3465 class_info->constructor = GetPropertyValue(property)->AsFunctionLiteral(); | 3482 class_info->constructor = GetPropertyValue(property)->AsFunctionLiteral(); |
3466 DCHECK_NOT_NULL(class_info->constructor); | 3483 DCHECK_NOT_NULL(class_info->constructor); |
3467 class_info->constructor->set_raw_name( | 3484 class_info->constructor->set_raw_name( |
3468 class_name != nullptr ? class_name | 3485 class_name != nullptr ? class_name |
3469 : ast_value_factory()->empty_string()); | 3486 : ast_value_factory()->empty_string()); |
3470 return; | 3487 return; |
3471 } | 3488 } |
3472 | 3489 |
3473 if (property->kind() == ClassLiteralProperty::FIELD) { | 3490 if (property->kind() == ClassLiteralProperty::FIELD) { |
(...skipping 1963 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5437 | 5454 |
5438 return final_loop; | 5455 return final_loop; |
5439 } | 5456 } |
5440 | 5457 |
5441 #undef CHECK_OK | 5458 #undef CHECK_OK |
5442 #undef CHECK_OK_VOID | 5459 #undef CHECK_OK_VOID |
5443 #undef CHECK_FAILED | 5460 #undef CHECK_FAILED |
5444 | 5461 |
5445 } // namespace internal | 5462 } // namespace internal |
5446 } // namespace v8 | 5463 } // namespace v8 |
OLD | NEW |