| 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 935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 946 const AstRawString* raw_name, | 946 const AstRawString* raw_name, |
| 947 Utf16CharacterStream* source) { | 947 Utf16CharacterStream* source) { |
| 948 scanner_.Initialize(source); | 948 scanner_.Initialize(source); |
| 949 DCHECK_NULL(scope_state_); | 949 DCHECK_NULL(scope_state_); |
| 950 DCHECK_NULL(target_stack_); | 950 DCHECK_NULL(target_stack_); |
| 951 | 951 |
| 952 DCHECK(ast_value_factory()); | 952 DCHECK(ast_value_factory()); |
| 953 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); | 953 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); |
| 954 fni_->PushEnclosingName(raw_name); | 954 fni_->PushEnclosingName(raw_name); |
| 955 | 955 |
| 956 ParsingModeScope parsing_mode(this, PARSE_EAGERLY); | 956 ParsingModeScope mode(this, PARSE_EAGERLY); |
| 957 | 957 |
| 958 // Place holder for the result. | 958 // Place holder for the result. |
| 959 FunctionLiteral* result = nullptr; | 959 FunctionLiteral* result = nullptr; |
| 960 | 960 |
| 961 { | 961 { |
| 962 // Parse the function literal. | 962 // Parse the function literal. |
| 963 Scope* outer = original_scope_; | 963 Scope* outer = original_scope_; |
| 964 DeclarationScope* outer_function = outer->GetClosureScope(); | 964 DeclarationScope* outer_function = outer->GetClosureScope(); |
| 965 DCHECK(outer); | 965 DCHECK(outer); |
| 966 FunctionState function_state(&function_state_, &scope_state_, | 966 FunctionState function_state(&function_state_, &scope_state_, |
| (...skipping 1650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2617 // })(); | 2617 // })(); |
| 2618 | 2618 |
| 2619 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume | 2619 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume |
| 2620 // parenthesis before the function means that it will be called | 2620 // parenthesis before the function means that it will be called |
| 2621 // immediately). bar can be parsed lazily, but we need to parse it in a mode | 2621 // immediately). bar can be parsed lazily, but we need to parse it in a mode |
| 2622 // that tracks unresolved variables. | 2622 // that tracks unresolved variables. |
| 2623 DCHECK_IMPLIES(mode() == PARSE_LAZILY, FLAG_lazy); | 2623 DCHECK_IMPLIES(mode() == PARSE_LAZILY, FLAG_lazy); |
| 2624 DCHECK_IMPLIES(mode() == PARSE_LAZILY, allow_lazy()); | 2624 DCHECK_IMPLIES(mode() == PARSE_LAZILY, allow_lazy()); |
| 2625 DCHECK_IMPLIES(mode() == PARSE_LAZILY, extension_ == nullptr); | 2625 DCHECK_IMPLIES(mode() == PARSE_LAZILY, extension_ == nullptr); |
| 2626 | 2626 |
| 2627 bool can_preparse = mode() == PARSE_LAZILY && |
| 2628 eager_compile_hint == FunctionLiteral::kShouldLazyCompile; |
| 2629 |
| 2627 bool is_lazy_top_level_function = | 2630 bool is_lazy_top_level_function = |
| 2628 mode() == PARSE_LAZILY && | 2631 can_preparse && scope()->AllowsLazyParsingWithoutUnresolvedVariables(); |
| 2629 eager_compile_hint == FunctionLiteral::kShouldLazyCompile && | |
| 2630 scope()->AllowsLazyParsingWithoutUnresolvedVariables(); | |
| 2631 | 2632 |
| 2632 // Determine whether we can still lazy parse the inner function. | 2633 // Determine whether we can still lazy parse the inner function. |
| 2633 // The preconditions are: | 2634 // The preconditions are: |
| 2634 // - Lazy compilation has to be enabled. | 2635 // - Lazy compilation has to be enabled. |
| 2635 // - Neither V8 natives nor native function declarations can be allowed, | 2636 // - Neither V8 natives nor native function declarations can be allowed, |
| 2636 // since parsing one would retroactively force the function to be | 2637 // since parsing one would retroactively force the function to be |
| 2637 // eagerly compiled. | 2638 // eagerly compiled. |
| 2638 // - The invoker of this parser can't depend on the AST being eagerly | 2639 // - The invoker of this parser can't depend on the AST being eagerly |
| 2639 // built (either because the function is about to be compiled, or | 2640 // built (either because the function is about to be compiled, or |
| 2640 // because the AST is going to be inspected for some reason). | 2641 // because the AST is going to be inspected for some reason). |
| 2641 // - Because of the above, we can't be attempting to parse a | 2642 // - Because of the above, we can't be attempting to parse a |
| 2642 // FunctionExpression; even without enclosing parentheses it might be | 2643 // FunctionExpression; even without enclosing parentheses it might be |
| 2643 // immediately invoked. | 2644 // immediately invoked. |
| 2644 // - The function literal shouldn't be hinted to eagerly compile. | 2645 // - The function literal shouldn't be hinted to eagerly compile. |
| 2645 // - For asm.js functions the body needs to be available when module | 2646 // - For asm.js functions the body needs to be available when module |
| 2646 // validation is active, because we examine the entire module at once. | 2647 // validation is active, because we examine the entire module at once. |
| 2647 | 2648 |
| 2648 // Inner functions will be parsed using a temporary Zone. After parsing, we | 2649 // Inner functions will be parsed using a temporary Zone. After parsing, we |
| 2649 // will migrate unresolved variable into a Scope in the main Zone. | 2650 // will migrate unresolved variable into a Scope in the main Zone. |
| 2650 // TODO(marja): Refactor parsing modes: simplify this. | 2651 // TODO(marja): Refactor parsing modes: simplify this. |
| 2651 bool use_temp_zone = | 2652 bool use_temp_zone = |
| 2652 allow_lazy() && function_type == FunctionLiteral::kDeclaration && | 2653 can_preparse && !(FLAG_validate_asm && scope()->IsAsmModule()); |
| 2653 eager_compile_hint != FunctionLiteral::kShouldEagerCompile && | |
| 2654 !(FLAG_validate_asm && scope()->IsAsmModule()); | |
| 2655 bool is_lazy_inner_function = | 2654 bool is_lazy_inner_function = |
| 2656 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function; | 2655 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function; |
| 2657 | 2656 |
| 2658 // This Scope lives in the main zone. We'll migrate data into that zone later. | 2657 // This Scope lives in the main zone. We'll migrate data into that zone later. |
| 2659 DeclarationScope* scope = NewFunctionScope(kind); | 2658 DeclarationScope* scope = NewFunctionScope(kind); |
| 2660 SetLanguageMode(scope, language_mode); | 2659 SetLanguageMode(scope, language_mode); |
| 2661 | 2660 |
| 2662 ZoneList<Statement*>* body = nullptr; | 2661 ZoneList<Statement*>* body = nullptr; |
| 2663 int arity = -1; | 2662 int arity = -1; |
| 2664 int materialized_literal_count = -1; | 2663 int materialized_literal_count = -1; |
| (...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3143 // caused by calling the .throw method on a generator suspended at the | 3142 // caused by calling the .throw method on a generator suspended at the |
| 3144 // initial yield (i.e. right after generator instantiation). | 3143 // initial yield (i.e. right after generator instantiation). |
| 3145 return factory()->NewYield(get_proxy, assignment, scope()->start_position(), | 3144 return factory()->NewYield(get_proxy, assignment, scope()->start_position(), |
| 3146 Yield::kOnExceptionThrow); | 3145 Yield::kOnExceptionThrow); |
| 3147 } | 3146 } |
| 3148 | 3147 |
| 3149 ZoneList<Statement*>* Parser::ParseEagerFunctionBody( | 3148 ZoneList<Statement*>* Parser::ParseEagerFunctionBody( |
| 3150 const AstRawString* function_name, int pos, | 3149 const AstRawString* function_name, int pos, |
| 3151 const ParserFormalParameters& parameters, FunctionKind kind, | 3150 const ParserFormalParameters& parameters, FunctionKind kind, |
| 3152 FunctionLiteral::FunctionType function_type, bool* ok) { | 3151 FunctionLiteral::FunctionType function_type, bool* ok) { |
| 3153 // Everything inside an eagerly parsed function will be parsed eagerly (see | 3152 ParsingModeScope mode(this, allow_lazy() ? PARSE_LAZILY : PARSE_EAGERLY); |
| 3154 // comment above). Lazy inner functions are handled separately and they won't | |
| 3155 // require the mode to be PARSE_LAZILY (see ParseFunctionLiteral). | |
| 3156 // TODO(marja): Refactor parsing modes: remove this. | |
| 3157 ParsingModeScope parsing_mode(this, PARSE_EAGERLY); | |
| 3158 ZoneList<Statement*>* result = new(zone()) ZoneList<Statement*>(8, zone()); | 3153 ZoneList<Statement*>* result = new(zone()) ZoneList<Statement*>(8, zone()); |
| 3159 | 3154 |
| 3160 static const int kFunctionNameAssignmentIndex = 0; | 3155 static const int kFunctionNameAssignmentIndex = 0; |
| 3161 if (function_type == FunctionLiteral::kNamedExpression) { | 3156 if (function_type == FunctionLiteral::kNamedExpression) { |
| 3162 DCHECK(function_name != NULL); | 3157 DCHECK(function_name != NULL); |
| 3163 // If we have a named function expression, we add a local variable | 3158 // If we have a named function expression, we add a local variable |
| 3164 // declaration to the body of the function with the name of the | 3159 // declaration to the body of the function with the name of the |
| 3165 // function and let it refer to the function itself (closure). | 3160 // function and let it refer to the function itself (closure). |
| 3166 // Not having parsed the function body, the language mode may still change, | 3161 // Not having parsed the function body, the language mode may still change, |
| 3167 // so we reserve a spot and create the actual const assignment later. | 3162 // so we reserve a spot and create the actual const assignment later. |
| (...skipping 2294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5462 | 5457 |
| 5463 return final_loop; | 5458 return final_loop; |
| 5464 } | 5459 } |
| 5465 | 5460 |
| 5466 #undef CHECK_OK | 5461 #undef CHECK_OK |
| 5467 #undef CHECK_OK_VOID | 5462 #undef CHECK_OK_VOID |
| 5468 #undef CHECK_FAILED | 5463 #undef CHECK_FAILED |
| 5469 | 5464 |
| 5470 } // namespace internal | 5465 } // namespace internal |
| 5471 } // namespace v8 | 5466 } // namespace v8 |
| OLD | NEW |