Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: src/parsing/parser.cc

Issue 2654423004: [async-functions] support await expressions in finally statements (Closed)
Patch Set: make -Wunused-variable bots happy maybe Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 409
410 Expression* Parser::NewTargetExpression(int pos) { 410 Expression* Parser::NewTargetExpression(int pos) {
411 auto proxy = NewUnresolved(ast_value_factory()->new_target_string(), pos); 411 auto proxy = NewUnresolved(ast_value_factory()->new_target_string(), pos);
412 proxy->set_is_new_target(); 412 proxy->set_is_new_target();
413 return proxy; 413 return proxy;
414 } 414 }
415 415
416 Expression* Parser::FunctionSentExpression(int pos) { 416 Expression* Parser::FunctionSentExpression(int pos) {
417 // We desugar function.sent into %_GeneratorGetInputOrDebugPos(generator). 417 // We desugar function.sent into %_GeneratorGetInputOrDebugPos(generator).
418 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone()); 418 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone());
419 VariableProxy* generator = 419 InternalVariable* generator =
420 factory()->NewVariableProxy(function_state_->generator_object_variable()); 420 factory()->NewInternalVariable(InternalVariable::kGeneratorObject);
421 args->Add(generator, zone()); 421 args->Add(generator, zone());
422 return factory()->NewCallRuntime(Runtime::kInlineGeneratorGetInputOrDebugPos, 422 return factory()->NewCallRuntime(Runtime::kInlineGeneratorGetInputOrDebugPos,
423 args, pos); 423 args, pos);
424 } 424 }
425 425
426 Literal* Parser::ExpressionFromLiteral(Token::Value token, int pos) { 426 Literal* Parser::ExpressionFromLiteral(Token::Value token, int pos) {
427 switch (token) { 427 switch (token) {
428 case Token::NULL_LITERAL: 428 case Token::NULL_LITERAL:
429 return factory()->NewNullLiteral(pos); 429 return factory()->NewNullLiteral(pos);
430 case Token::TRUE_LITERAL: 430 case Token::TRUE_LITERAL:
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 // Declare the special module parameter. 691 // Declare the special module parameter.
692 auto name = ast_value_factory()->empty_string(); 692 auto name = ast_value_factory()->empty_string();
693 bool is_duplicate; 693 bool is_duplicate;
694 bool is_rest = false; 694 bool is_rest = false;
695 bool is_optional = false; 695 bool is_optional = false;
696 auto var = scope->DeclareParameter(name, VAR, is_optional, is_rest, 696 auto var = scope->DeclareParameter(name, VAR, is_optional, is_rest,
697 &is_duplicate, ast_value_factory()); 697 &is_duplicate, ast_value_factory());
698 DCHECK(!is_duplicate); 698 DCHECK(!is_duplicate);
699 var->AllocateTo(VariableLocation::PARAMETER, 0); 699 var->AllocateTo(VariableLocation::PARAMETER, 0);
700 700
701 PrepareGeneratorVariables(); 701 scope->ForceContextAllocation();
702 Expression* initial_yield =
703 BuildInitialYield(kNoSourcePosition, kGeneratorFunction);
704 body->Add(
705 factory()->NewExpressionStatement(initial_yield, kNoSourcePosition),
706 zone());
707
708 ParseModuleItemList(body, &ok); 702 ParseModuleItemList(body, &ok);
709 ok = ok && 703 ok = ok &&
710 module()->Validate(this->scope()->AsModuleScope(), 704 module()->Validate(this->scope()->AsModuleScope(),
711 &pending_error_handler_, zone()); 705 &pending_error_handler_, zone());
712 } else { 706 } else {
713 // Don't count the mode in the use counters--give the program a chance 707 // Don't count the mode in the use counters--give the program a chance
714 // to enable script-wide strict mode below. 708 // to enable script-wide strict mode below.
715 this->scope()->SetLanguageMode(info->language_mode()); 709 this->scope()->SetLanguageMode(info->language_mode());
716 ParseStatementList(body, Token::EOS, &ok); 710 ParseStatementList(body, Token::EOS, &ok);
717 } 711 }
(...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after
1600 Expression* is_undefined = factory()->NewCompareOperation( 1594 Expression* is_undefined = factory()->NewCompareOperation(
1601 Token::EQ_STRICT, assign, 1595 Token::EQ_STRICT, assign,
1602 factory()->NewUndefinedLiteral(kNoSourcePosition), pos); 1596 factory()->NewUndefinedLiteral(kNoSourcePosition), pos);
1603 1597
1604 // is_undefined ? this : is_object_conditional 1598 // is_undefined ? this : is_object_conditional
1605 return_value = factory()->NewConditional(is_undefined, ThisExpression(pos), 1599 return_value = factory()->NewConditional(is_undefined, ThisExpression(pos),
1606 is_object_conditional, pos); 1600 is_object_conditional, pos);
1607 } 1601 }
1608 if (is_generator()) { 1602 if (is_generator()) {
1609 return_value = BuildIteratorResult(return_value, true); 1603 return_value = BuildIteratorResult(return_value, true);
1610 } else if (is_async_function()) {
1611 return_value = BuildResolvePromise(return_value, return_value->position());
1612 } 1604 }
1613 return return_value; 1605 return return_value;
1614 } 1606 }
1615 1607
1616 Expression* Parser::RewriteDoExpression(Block* body, int pos, bool* ok) { 1608 Expression* Parser::RewriteDoExpression(Block* body, int pos, bool* ok) {
1617 Variable* result = NewTemporary(ast_value_factory()->dot_result_string()); 1609 Variable* result = NewTemporary(ast_value_factory()->dot_result_string());
1618 DoExpression* expr = factory()->NewDoExpression(body, result, pos); 1610 DoExpression* expr = factory()->NewDoExpression(body, result, pos);
1619 if (!Rewriter::Rewrite(this, GetClosureScope(), expr, ast_value_factory())) { 1611 if (!Rewriter::Rewrite(this, GetClosureScope(), expr, ast_value_factory())) {
1620 *ok = false; 1612 *ok = false;
1621 return nullptr; 1613 return nullptr;
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1748 DCHECK_NOT_NULL(catch_info.scope); 1740 DCHECK_NOT_NULL(catch_info.scope);
1749 DCHECK_NOT_NULL(catch_info.variable); 1741 DCHECK_NOT_NULL(catch_info.variable);
1750 return factory()->NewTryCatchStatement( 1742 return factory()->NewTryCatchStatement(
1751 try_block, catch_info.scope, catch_info.variable, catch_block, pos); 1743 try_block, catch_info.scope, catch_info.variable, catch_block, pos);
1752 } else { 1744 } else {
1753 DCHECK_NOT_NULL(finally_block); 1745 DCHECK_NOT_NULL(finally_block);
1754 return factory()->NewTryFinallyStatement(try_block, finally_block, pos); 1746 return factory()->NewTryFinallyStatement(try_block, finally_block, pos);
1755 } 1747 }
1756 } 1748 }
1757 1749
1758 void Parser::ParseAndRewriteGeneratorFunctionBody(int pos, FunctionKind kind, 1750 void Parser::CreateFunctionNameVariable(
1759 ZoneList<Statement*>* body,
1760 bool* ok) {
1761 // We produce:
1762 //
1763 // try { InitialYield; ...body...; return {value: undefined, done: true} }
1764 // finally { %_GeneratorClose(generator) }
1765 //
1766 // - InitialYield yields the actual generator object.
1767 // - Any return statement inside the body will have its argument wrapped
1768 // in a "done" iterator result object.
1769 // - If the generator terminates for whatever reason, we must close it.
1770 // Hence the finally clause.
1771
1772 Block* try_block = factory()->NewBlock(nullptr, 3, false, kNoSourcePosition);
1773 Expression* initial_yield = BuildInitialYield(pos, kind);
1774 try_block->statements()->Add(
1775 factory()->NewExpressionStatement(initial_yield, kNoSourcePosition),
1776 zone());
1777 ParseStatementList(try_block->statements(), Token::RBRACE, ok);
1778 if (!*ok) return;
1779
1780 Statement* final_return = factory()->NewReturnStatement(
1781 BuildIteratorResult(nullptr, true), kNoSourcePosition);
1782 try_block->statements()->Add(final_return, zone());
1783
1784 Block* finally_block =
1785 factory()->NewBlock(nullptr, 1, false, kNoSourcePosition);
1786 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone());
1787 VariableProxy* call_proxy =
1788 factory()->NewVariableProxy(function_state_->generator_object_variable());
1789 args->Add(call_proxy, zone());
1790 Expression* call = factory()->NewCallRuntime(Runtime::kInlineGeneratorClose,
1791 args, kNoSourcePosition);
1792 finally_block->statements()->Add(
1793 factory()->NewExpressionStatement(call, kNoSourcePosition), zone());
1794
1795 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block,
1796 kNoSourcePosition),
1797 zone());
1798 }
1799
1800 void Parser::CreateFunctionNameAssignment(
1801 const AstRawString* function_name, int pos, 1751 const AstRawString* function_name, int pos,
1802 FunctionLiteral::FunctionType function_type, 1752 FunctionLiteral::FunctionType function_type,
1803 DeclarationScope* function_scope, ZoneList<Statement*>* result, int index) { 1753 DeclarationScope* function_scope) {
1804 if (function_type == FunctionLiteral::kNamedExpression) { 1754 if (function_type == FunctionLiteral::kNamedExpression) {
1805 StatementT statement = factory()->NewEmptyStatement(kNoSourcePosition); 1755 StatementT statement = factory()->NewEmptyStatement(kNoSourcePosition);
1806 if (function_scope->LookupLocal(function_name) == nullptr) { 1756 if (function_scope->LookupLocal(function_name) == nullptr) {
1807 // Now that we know the language mode, we can create the const assignment 1757 // Now that we know the language mode, we can create the const assignment
1808 // in the previously reserved spot. 1758 // in the previously reserved spot.
1809 DCHECK_EQ(function_scope, scope()); 1759 DCHECK_EQ(function_scope, scope());
1810 Variable* fvar = function_scope->DeclareFunctionVar(function_name); 1760 Variable* fvar = function_scope->DeclareFunctionVar(function_name);
1811 VariableProxy* fproxy = factory()->NewVariableProxy(fvar); 1761 DCHECK_NOT_NULL(fvar);
1812 statement = factory()->NewExpressionStatement(
1813 factory()->NewAssignment(Token::INIT, fproxy,
1814 factory()->NewThisFunction(pos),
1815 kNoSourcePosition),
1816 kNoSourcePosition);
1817 } 1762 }
1818 result->Set(index, statement);
1819 } 1763 }
1820 } 1764 }
1821 1765
1822 // !%_IsJSReceiver(result = iterator.next()) && 1766 // !%_IsJSReceiver(result = iterator.next()) &&
1823 // %ThrowIteratorResultNotAnObject(result) 1767 // %ThrowIteratorResultNotAnObject(result)
1824 Expression* Parser::BuildIteratorNextResult(Expression* iterator, 1768 Expression* Parser::BuildIteratorNextResult(Expression* iterator,
1825 Variable* result, int pos) { 1769 Variable* result, int pos) {
1826 Expression* next_literal = factory()->NewStringLiteral( 1770 Expression* next_literal = factory()->NewStringLiteral(
1827 ast_value_factory()->next_string(), kNoSourcePosition); 1771 ast_value_factory()->next_string(), kNoSourcePosition);
1828 Expression* next_property = 1772 Expression* next_property =
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after
2470 2414
2471 for (auto p : parameters.params) { 2415 for (auto p : parameters.params) {
2472 if (p->pattern != nullptr) reindexer.Reindex(p->pattern); 2416 if (p->pattern != nullptr) reindexer.Reindex(p->pattern);
2473 if (p->initializer != nullptr) reindexer.Reindex(p->initializer); 2417 if (p->initializer != nullptr) reindexer.Reindex(p->initializer);
2474 } 2418 }
2475 2419
2476 DCHECK(reindexer.count() <= function_state_->materialized_literal_count()); 2420 DCHECK(reindexer.count() <= function_state_->materialized_literal_count());
2477 } 2421 }
2478 } 2422 }
2479 2423
2480 void Parser::PrepareGeneratorVariables() {
2481 // For generators, allocating variables in contexts is currently a win because
2482 // it minimizes the work needed to suspend and resume an activation. The
2483 // code produced for generators relies on this forced context allocation (it
2484 // does not restore the frame's parameters upon resume).
2485 function_state_->scope()->ForceContextAllocation();
2486
2487 // Calling a generator returns a generator object. That object is stored
2488 // in a temporary variable, a definition that is used by "yield"
2489 // expressions.
2490 Variable* temp =
2491 NewTemporary(ast_value_factory()->dot_generator_object_string());
2492 function_state_->set_generator_object_variable(temp);
2493 }
2494
2495 FunctionLiteral* Parser::ParseFunctionLiteral( 2424 FunctionLiteral* Parser::ParseFunctionLiteral(
2496 const AstRawString* function_name, Scanner::Location function_name_location, 2425 const AstRawString* function_name, Scanner::Location function_name_location,
2497 FunctionNameValidity function_name_validity, FunctionKind kind, 2426 FunctionNameValidity function_name_validity, FunctionKind kind,
2498 int function_token_pos, FunctionLiteral::FunctionType function_type, 2427 int function_token_pos, FunctionLiteral::FunctionType function_type,
2499 LanguageMode language_mode, bool* ok) { 2428 LanguageMode language_mode, bool* ok) {
2500 // Function :: 2429 // Function ::
2501 // '(' FormalParameterList? ')' '{' FunctionBody '}' 2430 // '(' FormalParameterList? ')' '{' FunctionBody '}'
2502 // 2431 //
2503 // Getter :: 2432 // Getter ::
2504 // '(' ')' '{' FunctionBody '}' 2433 // '(' ')' '{' FunctionBody '}'
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
2599 2528
2600 DCHECK_IMPLIES( 2529 DCHECK_IMPLIES(
2601 (is_lazy_top_level_function || 2530 (is_lazy_top_level_function ||
2602 (parse_lazily() && function_type == FunctionLiteral::kDeclaration && 2531 (parse_lazily() && function_type == FunctionLiteral::kDeclaration &&
2603 eager_compile_hint == FunctionLiteral::kShouldLazyCompile)), 2532 eager_compile_hint == FunctionLiteral::kShouldLazyCompile)),
2604 can_preparse); 2533 can_preparse);
2605 bool is_lazy_inner_function = 2534 bool is_lazy_inner_function =
2606 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function; 2535 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function;
2607 2536
2608 ZoneList<Statement*>* body = nullptr; 2537 ZoneList<Statement*>* body = nullptr;
2538 Block* parameter_init_block = nullptr;
2609 int materialized_literal_count = -1; 2539 int materialized_literal_count = -1;
2610 int expected_property_count = -1; 2540 int expected_property_count = -1;
2611 bool should_be_used_once_hint = false; 2541 bool should_be_used_once_hint = false;
2612 int num_parameters = -1; 2542 int num_parameters = -1;
2613 int function_length = -1; 2543 int function_length = -1;
2614 bool has_duplicate_parameters = false; 2544 bool has_duplicate_parameters = false;
2615 int function_literal_id = GetNextFunctionLiteralId(); 2545 int function_literal_id = GetNextFunctionLiteralId();
2616 2546
2617 Zone* outer_zone = zone(); 2547 Zone* outer_zone = zone();
2618 DeclarationScope* scope; 2548 DeclarationScope* scope;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
2670 should_be_used_once_hint = true; 2600 should_be_used_once_hint = true;
2671 scope->ResetAfterPreparsing(ast_value_factory(), true); 2601 scope->ResetAfterPreparsing(ast_value_factory(), true);
2672 zone_scope.Reset(); 2602 zone_scope.Reset();
2673 use_temp_zone = false; 2603 use_temp_zone = false;
2674 } 2604 }
2675 } 2605 }
2676 2606
2677 if (!is_lazy_top_level_function && !is_lazy_inner_function) { 2607 if (!is_lazy_top_level_function && !is_lazy_inner_function) {
2678 body = ParseFunction( 2608 body = ParseFunction(
2679 function_name, pos, kind, function_type, scope, &num_parameters, 2609 function_name, pos, kind, function_type, scope, &num_parameters,
2680 &function_length, &has_duplicate_parameters, 2610 &parameter_init_block, &function_length, &has_duplicate_parameters,
2681 &materialized_literal_count, &expected_property_count, CHECK_OK); 2611 &materialized_literal_count, &expected_property_count, CHECK_OK);
2682 } 2612 }
2683 2613
2684 DCHECK(use_temp_zone || !is_lazy_top_level_function); 2614 DCHECK(use_temp_zone || !is_lazy_top_level_function);
2685 if (use_temp_zone) { 2615 if (use_temp_zone) {
2686 // If the preconditions are correct the function body should never be 2616 // If the preconditions are correct the function body should never be
2687 // accessed, but do this anyway for better behaviour if they're wrong. 2617 // accessed, but do this anyway for better behaviour if they're wrong.
2688 body = nullptr; 2618 body = nullptr;
2689 scope->AnalyzePartially(&previous_zone_ast_node_factory, 2619 scope->AnalyzePartially(&previous_zone_ast_node_factory,
2690 preparsed_scope_data_); 2620 preparsed_scope_data_);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
2732 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters 2662 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
2733 : FunctionLiteral::kNoDuplicateParameters; 2663 : FunctionLiteral::kNoDuplicateParameters;
2734 2664
2735 // Note that the FunctionLiteral needs to be created in the main Zone again. 2665 // Note that the FunctionLiteral needs to be created in the main Zone again.
2736 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 2666 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
2737 function_name, scope, body, materialized_literal_count, 2667 function_name, scope, body, materialized_literal_count,
2738 expected_property_count, num_parameters, function_length, 2668 expected_property_count, num_parameters, function_length,
2739 duplicate_parameters, function_type, eager_compile_hint, pos, true, 2669 duplicate_parameters, function_type, eager_compile_hint, pos, true,
2740 function_literal_id); 2670 function_literal_id);
2741 function_literal->set_function_token_position(function_token_pos); 2671 function_literal->set_function_token_position(function_token_pos);
2672 function_literal->set_parameter_init_block(parameter_init_block);
2742 if (should_be_used_once_hint) 2673 if (should_be_used_once_hint)
2743 function_literal->set_should_be_used_once_hint(); 2674 function_literal->set_should_be_used_once_hint();
2744 2675
2745 if (should_infer_name) { 2676 if (should_infer_name) {
2746 DCHECK_NOT_NULL(fni_); 2677 DCHECK_NOT_NULL(fni_);
2747 fni_->AddFunction(function_literal); 2678 fni_->AddFunction(function_literal);
2748 } 2679 }
2749 return function_literal; 2680 return function_literal;
2750 } 2681 }
2751 2682
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
2979 if (param_scope != nullptr) { 2910 if (param_scope != nullptr) {
2980 CheckConflictingVarDeclarations(param_scope, CHECK_OK); 2911 CheckConflictingVarDeclarations(param_scope, CHECK_OK);
2981 } 2912 }
2982 init_block->statements()->Add(param_block, zone()); 2913 init_block->statements()->Add(param_block, zone());
2983 } 2914 }
2984 ++index; 2915 ++index;
2985 } 2916 }
2986 return init_block; 2917 return init_block;
2987 } 2918 }
2988 2919
2989 Block* Parser::BuildRejectPromiseOnException(Block* inner_block) {
2990 // .promise = %AsyncFunctionPromiseCreate();
2991 // try {
2992 // <inner_block>
2993 // } catch (.catch) {
2994 // %RejectPromise(.promise, .catch);
2995 // return .promise;
2996 // } finally {
2997 // %AsyncFunctionPromiseRelease(.promise);
2998 // }
2999 Block* result = factory()->NewBlock(nullptr, 2, true, kNoSourcePosition);
3000
3001 // .promise = %AsyncFunctionPromiseCreate();
3002 Statement* set_promise;
3003 {
3004 Expression* create_promise = factory()->NewCallRuntime(
3005 Context::ASYNC_FUNCTION_PROMISE_CREATE_INDEX,
3006 new (zone()) ZoneList<Expression*>(0, zone()), kNoSourcePosition);
3007 Assignment* assign_promise = factory()->NewAssignment(
3008 Token::INIT, factory()->NewVariableProxy(PromiseVariable()),
3009 create_promise, kNoSourcePosition);
3010 set_promise =
3011 factory()->NewExpressionStatement(assign_promise, kNoSourcePosition);
3012 }
3013 result->statements()->Add(set_promise, zone());
3014
3015 // catch (.catch) { return %RejectPromise(.promise, .catch), .promise }
3016 Scope* catch_scope = NewScope(CATCH_SCOPE);
3017 catch_scope->set_is_hidden();
3018 Variable* catch_variable =
3019 catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(), VAR);
3020 Block* catch_block = factory()->NewBlock(nullptr, 1, true, kNoSourcePosition);
3021
3022 Expression* promise_reject = BuildRejectPromise(
3023 factory()->NewVariableProxy(catch_variable), kNoSourcePosition);
3024 ReturnStatement* return_promise_reject =
3025 factory()->NewReturnStatement(promise_reject, kNoSourcePosition);
3026 catch_block->statements()->Add(return_promise_reject, zone());
3027
3028 TryStatement* try_catch_statement =
3029 factory()->NewTryCatchStatementForAsyncAwait(inner_block, catch_scope,
3030 catch_variable, catch_block,
3031 kNoSourcePosition);
3032
3033 // There is no TryCatchFinally node, so wrap it in an outer try/finally
3034 Block* outer_try_block =
3035 factory()->NewBlock(nullptr, 1, true, kNoSourcePosition);
3036 outer_try_block->statements()->Add(try_catch_statement, zone());
3037
3038 // finally { %AsyncFunctionPromiseRelease(.promise) }
3039 Block* finally_block =
3040 factory()->NewBlock(nullptr, 1, true, kNoSourcePosition);
3041 {
3042 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone());
3043 args->Add(factory()->NewVariableProxy(PromiseVariable()), zone());
3044 Expression* call_promise_release = factory()->NewCallRuntime(
3045 Context::ASYNC_FUNCTION_PROMISE_RELEASE_INDEX, args, kNoSourcePosition);
3046 Statement* promise_release = factory()->NewExpressionStatement(
3047 call_promise_release, kNoSourcePosition);
3048 finally_block->statements()->Add(promise_release, zone());
3049 }
3050
3051 Statement* try_finally_statement = factory()->NewTryFinallyStatement(
3052 outer_try_block, finally_block, kNoSourcePosition);
3053
3054 result->statements()->Add(try_finally_statement, zone());
3055 return result;
3056 }
3057
3058 Assignment* Parser::BuildCreateJSGeneratorObject(int pos, FunctionKind kind) {
3059 // .generator = %CreateJSGeneratorObject(...);
3060 DCHECK_NOT_NULL(function_state_->generator_object_variable());
3061 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone());
3062 args->Add(factory()->NewThisFunction(pos), zone());
3063 args->Add(IsArrowFunction(kind) ? GetLiteralUndefined(pos)
3064 : ThisExpression(kNoSourcePosition),
3065 zone());
3066 Expression* allocation =
3067 factory()->NewCallRuntime(Runtime::kCreateJSGeneratorObject, args, pos);
3068 VariableProxy* proxy =
3069 factory()->NewVariableProxy(function_state_->generator_object_variable());
3070 return factory()->NewAssignment(Token::INIT, proxy, allocation,
3071 kNoSourcePosition);
3072 }
3073
3074 Expression* Parser::BuildResolvePromise(Expression* value, int pos) {
3075 // %ResolvePromise(.promise, value), .promise
3076 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone());
3077 args->Add(factory()->NewVariableProxy(PromiseVariable()), zone());
3078 args->Add(value, zone());
3079 Expression* call_runtime =
3080 factory()->NewCallRuntime(Context::PROMISE_RESOLVE_INDEX, args, pos);
3081 return factory()->NewBinaryOperation(
3082 Token::COMMA, call_runtime,
3083 factory()->NewVariableProxy(PromiseVariable()), pos);
3084 }
3085
3086 Expression* Parser::BuildRejectPromise(Expression* value, int pos) {
3087 // %promise_internal_reject(.promise, value, false), .promise
3088 // Disables the additional debug event for the rejection since a debug event
3089 // already happened for the exception that got us here.
3090 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(3, zone());
3091 args->Add(factory()->NewVariableProxy(PromiseVariable()), zone());
3092 args->Add(value, zone());
3093 args->Add(factory()->NewBooleanLiteral(false, pos), zone());
3094 Expression* call_runtime = factory()->NewCallRuntime(
3095 Context::PROMISE_INTERNAL_REJECT_INDEX, args, pos);
3096 return factory()->NewBinaryOperation(
3097 Token::COMMA, call_runtime,
3098 factory()->NewVariableProxy(PromiseVariable()), pos);
3099 }
3100
3101 Variable* Parser::PromiseVariable() {
3102 // Based on the various compilation paths, there are many different code
3103 // paths which may be the first to access the Promise temporary. Whichever
3104 // comes first should create it and stash it in the FunctionState.
3105 Variable* promise = function_state_->promise_variable();
3106 if (function_state_->promise_variable() == nullptr) {
3107 promise = scope()->NewTemporary(ast_value_factory()->empty_string());
3108 function_state_->set_promise_variable(promise);
3109 }
3110 return promise;
3111 }
3112
3113 Expression* Parser::BuildInitialYield(int pos, FunctionKind kind) {
3114 Assignment* assignment = BuildCreateJSGeneratorObject(pos, kind);
3115 VariableProxy* generator =
3116 factory()->NewVariableProxy(function_state_->generator_object_variable());
3117 // The position of the yield is important for reporting the exception
3118 // caused by calling the .throw method on a generator suspended at the
3119 // initial yield (i.e. right after generator instantiation).
3120 return factory()->NewYield(generator, assignment, scope()->start_position(),
3121 Yield::kOnExceptionThrow);
3122 }
3123
3124 ZoneList<Statement*>* Parser::ParseFunction( 2920 ZoneList<Statement*>* Parser::ParseFunction(
3125 const AstRawString* function_name, int pos, FunctionKind kind, 2921 const AstRawString* function_name, int pos, FunctionKind kind,
3126 FunctionLiteral::FunctionType function_type, 2922 FunctionLiteral::FunctionType function_type,
3127 DeclarationScope* function_scope, int* num_parameters, int* function_length, 2923 DeclarationScope* function_scope, int* num_parameters,
2924 Block** parameter_init_block, int* function_length,
3128 bool* has_duplicate_parameters, int* materialized_literal_count, 2925 bool* has_duplicate_parameters, int* materialized_literal_count,
3129 int* expected_property_count, bool* ok) { 2926 int* expected_property_count, bool* ok) {
3130 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY); 2927 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY);
3131 2928
3132 FunctionState function_state(&function_state_, &scope_state_, function_scope); 2929 FunctionState function_state(&function_state_, &scope_state_, function_scope);
3133 2930
3134 DuplicateFinder duplicate_finder; 2931 DuplicateFinder duplicate_finder;
3135 ExpressionClassifier formals_classifier(this, &duplicate_finder); 2932 ExpressionClassifier formals_classifier(this, &duplicate_finder);
3136 2933
3137 if (IsResumableFunction(kind)) PrepareGeneratorVariables(); 2934 if (IsResumableFunction(kind)) function_scope->ForceContextAllocation();
3138 2935
3139 ParserFormalParameters formals(function_scope); 2936 ParserFormalParameters formals(function_scope);
3140 ParseFormalParameterList(&formals, CHECK_OK); 2937 ParseFormalParameterList(&formals, CHECK_OK);
3141 Expect(Token::RPAREN, CHECK_OK); 2938 Expect(Token::RPAREN, CHECK_OK);
3142 int formals_end_position = scanner()->location().end_pos; 2939 int formals_end_position = scanner()->location().end_pos;
3143 *num_parameters = formals.num_parameters(); 2940 *num_parameters = formals.num_parameters();
3144 *function_length = formals.function_length; 2941 *function_length = formals.function_length;
3145 2942
3146 CheckArityRestrictions(formals.arity, kind, formals.has_rest, 2943 CheckArityRestrictions(formals.arity, kind, formals.has_rest,
3147 function_scope->start_position(), formals_end_position, 2944 function_scope->start_position(), formals_end_position,
3148 CHECK_OK); 2945 CHECK_OK);
3149 Expect(Token::LBRACE, CHECK_OK); 2946 Expect(Token::LBRACE, CHECK_OK);
3150 2947
3151 ZoneList<Statement*>* body = new (zone()) ZoneList<Statement*>(8, zone()); 2948 ZoneList<Statement*>* body = new (zone()) ZoneList<Statement*>(8, zone());
3152 ParseFunctionBody(body, function_name, pos, formals, kind, function_type, ok); 2949 ParseFunctionBody(body, parameter_init_block, function_name, pos, formals,
2950 kind, function_type, ok);
3153 2951
3154 // Validate parameter names. We can do this only after parsing the function, 2952 // Validate parameter names. We can do this only after parsing the function,
3155 // since the function can declare itself strict. 2953 // since the function can declare itself strict.
3156 const bool allow_duplicate_parameters = 2954 const bool allow_duplicate_parameters =
3157 is_sloppy(function_scope->language_mode()) && formals.is_simple && 2955 is_sloppy(function_scope->language_mode()) && formals.is_simple &&
3158 !IsConciseMethod(kind); 2956 !IsConciseMethod(kind);
3159 ValidateFormalParameters(function_scope->language_mode(), 2957 ValidateFormalParameters(function_scope->language_mode(),
3160 allow_duplicate_parameters, CHECK_OK); 2958 allow_duplicate_parameters, CHECK_OK);
3161 2959
3162 RewriteDestructuringAssignments(); 2960 RewriteDestructuringAssignments();
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after
3765 3563
3766 Expression* Parser::ExpressionListToExpression(ZoneList<Expression*>* args) { 3564 Expression* Parser::ExpressionListToExpression(ZoneList<Expression*>* args) {
3767 Expression* expr = args->at(0); 3565 Expression* expr = args->at(0);
3768 for (int i = 1; i < args->length(); ++i) { 3566 for (int i = 1; i < args->length(); ++i) {
3769 expr = factory()->NewBinaryOperation(Token::COMMA, expr, args->at(i), 3567 expr = factory()->NewBinaryOperation(Token::COMMA, expr, args->at(i),
3770 expr->position()); 3568 expr->position());
3771 } 3569 }
3772 return expr; 3570 return expr;
3773 } 3571 }
3774 3572
3775 // This method intoduces the line initializing the generator object
3776 // when desugaring the body of async_function.
3777 void Parser::PrepareAsyncFunctionBody(ZoneList<Statement*>* body,
3778 FunctionKind kind, int pos) {
3779 // When parsing an async arrow function, we get here without having called
3780 // PrepareGeneratorVariables yet, so do it now.
3781 if (function_state_->generator_object_variable() == nullptr) {
3782 PrepareGeneratorVariables();
3783 }
3784 body->Add(factory()->NewExpressionStatement(
3785 BuildCreateJSGeneratorObject(pos, kind), kNoSourcePosition),
3786 zone());
3787 }
3788
3789 // This method completes the desugaring of the body of async_function.
3790 void Parser::RewriteAsyncFunctionBody(ZoneList<Statement*>* body, Block* block,
3791 Expression* return_value, bool* ok) {
3792 // function async_function() {
3793 // .generator_object = %CreateJSGeneratorObject();
3794 // BuildRejectPromiseOnException({
3795 // ... block ...
3796 // return %ResolvePromise(.promise, expr), .promise;
3797 // })
3798 // }
3799
3800 return_value = BuildResolvePromise(return_value, return_value->position());
3801 block->statements()->Add(
3802 factory()->NewReturnStatement(return_value, return_value->position()),
3803 zone());
3804 block = BuildRejectPromiseOnException(block);
3805 body->Add(block, zone());
3806 }
3807
3808 Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) {
3809 // yield do {
3810 // tmp = <operand>;
3811 // %AsyncFunctionAwait(.generator_object, tmp, .promise);
3812 // .promise
3813 // }
3814 // The value of the expression is returned to the caller of the async
3815 // function for the first yield statement; for this, .promise is the
3816 // appropriate return value, being a Promise that will be fulfilled or
3817 // rejected with the appropriate value by the desugaring. Subsequent yield
3818 // occurrences will return to the AsyncFunctionNext call within the
3819 // implemementation of the intermediate throwaway Promise's then handler.
3820 // This handler has nothing useful to do with the value, as the Promise is
3821 // ignored. If we yielded the value of the throwawayPromise that
3822 // AsyncFunctionAwait creates as an intermediate, it would create a memory
3823 // leak; we must return .promise instead;
3824 // The operand needs to be evaluated on a separate statement in order to get
3825 // a break location, and the .promise needs to be read earlier so that it
3826 // doesn't insert a false location.
3827 // TODO(littledan): investigate why this ordering is needed in more detail.
3828 Variable* generator_object_variable =
3829 function_state_->generator_object_variable();
3830 DCHECK_NOT_NULL(generator_object_variable);
3831
3832 const int nopos = kNoSourcePosition;
3833
3834 Block* do_block = factory()->NewBlock(nullptr, 2, false, nopos);
3835
3836 Variable* promise = PromiseVariable();
3837
3838 // Wrap value evaluation to provide a break location.
3839 Variable* temp_var = NewTemporary(ast_value_factory()->empty_string());
3840 Expression* value_assignment = factory()->NewAssignment(
3841 Token::ASSIGN, factory()->NewVariableProxy(temp_var), value, nopos);
3842 do_block->statements()->Add(
3843 factory()->NewExpressionStatement(value_assignment, value->position()),
3844 zone());
3845
3846 ZoneList<Expression*>* async_function_await_args =
3847 new (zone()) ZoneList<Expression*>(3, zone());
3848 Expression* generator_object =
3849 factory()->NewVariableProxy(generator_object_variable);
3850 async_function_await_args->Add(generator_object, zone());
3851 async_function_await_args->Add(factory()->NewVariableProxy(temp_var), zone());
3852 async_function_await_args->Add(factory()->NewVariableProxy(promise), zone());
3853
3854 // The parser emits calls to AsyncFunctionAwaitCaught, but the
3855 // AstNumberingVisitor will rewrite this to AsyncFunctionAwaitUncaught
3856 // if there is no local enclosing try/catch block.
3857 Expression* async_function_await =
3858 factory()->NewCallRuntime(Context::ASYNC_FUNCTION_AWAIT_CAUGHT_INDEX,
3859 async_function_await_args, nopos);
3860 do_block->statements()->Add(
3861 factory()->NewExpressionStatement(async_function_await, await_pos),
3862 zone());
3863
3864 // Wrap await to provide a break location between value evaluation and yield.
3865 Expression* do_expr = factory()->NewDoExpression(do_block, promise, nopos);
3866
3867 generator_object = factory()->NewVariableProxy(generator_object_variable);
3868 return factory()->NewYield(generator_object, do_expr, nopos,
3869 Yield::kOnExceptionRethrow);
3870 }
3871
3872 class NonPatternRewriter : public AstExpressionRewriter { 3573 class NonPatternRewriter : public AstExpressionRewriter {
3873 public: 3574 public:
3874 NonPatternRewriter(uintptr_t stack_limit, Parser* parser) 3575 NonPatternRewriter(uintptr_t stack_limit, Parser* parser)
3875 : AstExpressionRewriter(stack_limit), parser_(parser) {} 3576 : AstExpressionRewriter(stack_limit), parser_(parser) {}
3876 ~NonPatternRewriter() override {} 3577 ~NonPatternRewriter() override {}
3877 3578
3878 private: 3579 private:
3879 bool RewriteExpression(Expression* expr) override { 3580 bool RewriteExpression(Expression* expr) override {
3880 if (expr->IsRewritableExpression()) return true; 3581 if (expr->IsRewritableExpression()) return true;
3881 // Rewrite only what could have been a pattern but is not. 3582 // Rewrite only what could have been a pattern but is not.
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
4214 // if (!IS_RECEIVER(output)) %ThrowIterResultNotAnObject(output); 3915 // if (!IS_RECEIVER(output)) %ThrowIterResultNotAnObject(output);
4215 // } 3916 // }
4216 // 3917 //
4217 // IteratorClose(iterator, input, output) expands to the following: 3918 // IteratorClose(iterator, input, output) expands to the following:
4218 // 3919 //
4219 // let iteratorReturn = iterator.return; 3920 // let iteratorReturn = iterator.return;
4220 // if (IS_NULL_OR_UNDEFINED(iteratorReturn)) return input; 3921 // if (IS_NULL_OR_UNDEFINED(iteratorReturn)) return input;
4221 // output = %_Call(iteratorReturn, iterator, input); 3922 // output = %_Call(iteratorReturn, iterator, input);
4222 // if (!IS_RECEIVER(output)) %ThrowIterResultNotAnObject(output); 3923 // if (!IS_RECEIVER(output)) %ThrowIterResultNotAnObject(output);
4223 3924
4224 Expression* Parser::RewriteYieldStar(Expression* generator, 3925 Expression* Parser::RewriteYieldStar(Expression* iterable, int pos) {
4225 Expression* iterable, int pos) {
4226 const int nopos = kNoSourcePosition; 3926 const int nopos = kNoSourcePosition;
4227 3927
4228 // Forward definition for break/continue statements. 3928 // Forward definition for break/continue statements.
4229 WhileStatement* loop = factory()->NewWhileStatement(nullptr, nopos); 3929 WhileStatement* loop = factory()->NewWhileStatement(nullptr, nopos);
4230 3930
4231 // let input = undefined; 3931 // let input = undefined;
4232 Variable* var_input = NewTemporary(ast_value_factory()->empty_string()); 3932 Variable* var_input = NewTemporary(ast_value_factory()->empty_string());
4233 Statement* initialize_input; 3933 Statement* initialize_input;
4234 { 3934 {
4235 Expression* input_proxy = factory()->NewVariableProxy(var_input); 3935 Expression* input_proxy = factory()->NewVariableProxy(var_input);
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
4414 factory()->NewSmiLiteral(JSGeneratorObject::kReturn, nopos); 4114 factory()->NewSmiLiteral(JSGeneratorObject::kReturn, nopos);
4415 Expression* assignment = 4115 Expression* assignment =
4416 factory()->NewAssignment(Token::ASSIGN, mode_proxy, kreturn, nopos); 4116 factory()->NewAssignment(Token::ASSIGN, mode_proxy, kreturn, nopos);
4417 set_mode_return = factory()->NewExpressionStatement(assignment, nopos); 4117 set_mode_return = factory()->NewExpressionStatement(assignment, nopos);
4418 } 4118 }
4419 4119
4420 // Yield(output); 4120 // Yield(output);
4421 Statement* yield_output; 4121 Statement* yield_output;
4422 { 4122 {
4423 Expression* output_proxy = factory()->NewVariableProxy(var_output); 4123 Expression* output_proxy = factory()->NewVariableProxy(var_output);
4424 Yield* yield = factory()->NewYield(generator, output_proxy, nopos, 4124 Yield* yield = factory()->NewYield(
4425 Yield::kOnExceptionThrow); 4125 output_proxy, nopos, Yield::kOnExceptionThrow, Yield::kNormal);
4426 yield_output = factory()->NewExpressionStatement(yield, nopos); 4126 yield_output = factory()->NewExpressionStatement(yield, nopos);
4427 } 4127 }
4428 4128
4429 // mode = kNext; 4129 // mode = kNext;
4430 Statement* set_mode_next; 4130 Statement* set_mode_next;
4431 { 4131 {
4432 Expression* mode_proxy = factory()->NewVariableProxy(var_mode); 4132 Expression* mode_proxy = factory()->NewVariableProxy(var_mode);
4433 Expression* knext = 4133 Expression* knext =
4434 factory()->NewSmiLiteral(JSGeneratorObject::kNext, nopos); 4134 factory()->NewSmiLiteral(JSGeneratorObject::kNext, nopos);
4435 Expression* assignment = 4135 Expression* assignment =
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after
5037 4737
5038 return final_loop; 4738 return final_loop;
5039 } 4739 }
5040 4740
5041 #undef CHECK_OK 4741 #undef CHECK_OK
5042 #undef CHECK_OK_VOID 4742 #undef CHECK_OK_VOID
5043 #undef CHECK_FAILED 4743 #undef CHECK_FAILED
5044 4744
5045 } // namespace internal 4745 } // namespace internal
5046 } // namespace v8 4746 } // namespace v8
OLDNEW
« src/ast/ast.h ('K') | « src/parsing/parser.h ('k') | src/parsing/parser-base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698