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