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 1554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1565 // other functions are set up when entering the surrounding scope. | 1565 // other functions are set up when entering the surrounding scope. |
1566 Declaration* decl = DeclareVariable(name, VAR, pos, CHECK_OK); | 1566 Declaration* decl = DeclareVariable(name, VAR, pos, CHECK_OK); |
1567 NativeFunctionLiteral* lit = | 1567 NativeFunctionLiteral* lit = |
1568 factory()->NewNativeFunctionLiteral(name, extension_, kNoSourcePosition); | 1568 factory()->NewNativeFunctionLiteral(name, extension_, kNoSourcePosition); |
1569 return factory()->NewExpressionStatement( | 1569 return factory()->NewExpressionStatement( |
1570 factory()->NewAssignment(Token::INIT, decl->proxy(), lit, | 1570 factory()->NewAssignment(Token::INIT, decl->proxy(), lit, |
1571 kNoSourcePosition), | 1571 kNoSourcePosition), |
1572 pos); | 1572 pos); |
1573 } | 1573 } |
1574 | 1574 |
1575 Statement* Parser::ParseAsyncFunctionDeclaration( | |
1576 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { | |
1577 DCHECK_EQ(scanner()->current_token(), Token::ASYNC); | |
1578 int pos = position(); | |
1579 if (scanner()->HasAnyLineTerminatorBeforeNext()) { | |
1580 *ok = false; | |
1581 ReportUnexpectedToken(scanner()->current_token()); | |
1582 return nullptr; | |
1583 } | |
1584 Expect(Token::FUNCTION, CHECK_OK); | |
1585 ParseFunctionFlags flags = ParseFunctionFlags::kIsAsync; | |
1586 return ParseHoistableDeclaration(pos, flags, names, default_export, ok); | |
1587 } | |
1588 | |
1589 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, | 1575 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, |
1590 bool default_export, bool* ok) { | 1576 bool default_export, bool* ok) { |
1591 // ClassDeclaration :: | 1577 // ClassDeclaration :: |
1592 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}' | 1578 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}' |
1593 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' | 1579 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' |
1594 // | 1580 // |
1595 // The anonymous form is allowed iff [default_export] is true. | 1581 // The anonymous form is allowed iff [default_export] is true. |
1596 // | 1582 // |
1597 // 'class' is expected to be consumed by the caller. | 1583 // 'class' is expected to be consumed by the caller. |
1598 // | 1584 // |
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2493 } | 2479 } |
2494 | 2480 |
2495 inner_scope->set_end_position(scanner()->location().end_pos); | 2481 inner_scope->set_end_position(scanner()->location().end_pos); |
2496 inner_block->set_scope(inner_scope); | 2482 inner_block->set_scope(inner_scope); |
2497 } | 2483 } |
2498 | 2484 |
2499 outer_loop->Initialize(NULL, NULL, NULL, inner_block); | 2485 outer_loop->Initialize(NULL, NULL, NULL, inner_block); |
2500 return outer_block; | 2486 return outer_block; |
2501 } | 2487 } |
2502 | 2488 |
2503 void Parser::ParseArrowFunctionFormalParameters( | 2489 void Parser::AddArrowFunctionFormalParameters( |
2504 ParserFormalParameters* parameters, Expression* expr, int end_pos, | 2490 ParserFormalParameters* parameters, Expression* expr, int end_pos, |
2505 bool* ok) { | 2491 bool* ok) { |
2506 // ArrowFunctionFormals :: | 2492 // ArrowFunctionFormals :: |
2507 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail) | 2493 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail) |
2508 // Tail | 2494 // Tail |
2509 // NonTailArrowFunctionFormals :: | 2495 // NonTailArrowFunctionFormals :: |
2510 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy) | 2496 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy) |
2511 // VariableProxy | 2497 // VariableProxy |
2512 // Tail :: | 2498 // Tail :: |
2513 // VariableProxy | 2499 // VariableProxy |
2514 // Spread(VariableProxy) | 2500 // Spread(VariableProxy) |
2515 // | 2501 // |
2516 // As we need to visit the parameters in left-to-right order, we recurse on | 2502 // As we need to visit the parameters in left-to-right order, we recurse on |
2517 // the left-hand side of comma expressions. | 2503 // the left-hand side of comma expressions. |
2518 // | 2504 // |
2519 if (expr->IsBinaryOperation()) { | 2505 if (expr->IsBinaryOperation()) { |
2520 BinaryOperation* binop = expr->AsBinaryOperation(); | 2506 BinaryOperation* binop = expr->AsBinaryOperation(); |
2521 // The classifier has already run, so we know that the expression is a valid | 2507 // The classifier has already run, so we know that the expression is a valid |
2522 // arrow function formals production. | 2508 // arrow function formals production. |
2523 DCHECK_EQ(binop->op(), Token::COMMA); | 2509 DCHECK_EQ(binop->op(), Token::COMMA); |
2524 Expression* left = binop->left(); | 2510 Expression* left = binop->left(); |
2525 Expression* right = binop->right(); | 2511 Expression* right = binop->right(); |
2526 int comma_pos = binop->position(); | 2512 int comma_pos = binop->position(); |
2527 ParseArrowFunctionFormalParameters(parameters, left, comma_pos, | 2513 AddArrowFunctionFormalParameters(parameters, left, comma_pos, |
2528 CHECK_OK_VOID); | 2514 CHECK_OK_VOID); |
2529 // LHS of comma expression should be unparenthesized. | 2515 // LHS of comma expression should be unparenthesized. |
2530 expr = right; | 2516 expr = right; |
2531 } | 2517 } |
2532 | 2518 |
2533 // Only the right-most expression may be a rest parameter. | 2519 // Only the right-most expression may be a rest parameter. |
2534 DCHECK(!parameters->has_rest); | 2520 DCHECK(!parameters->has_rest); |
2535 | 2521 |
2536 bool is_rest = expr->IsSpread(); | 2522 bool is_rest = expr->IsSpread(); |
2537 if (is_rest) { | 2523 if (is_rest) { |
2538 expr = expr->AsSpread()->expression(); | 2524 expr = expr->AsSpread()->expression(); |
2539 parameters->has_rest = true; | 2525 parameters->has_rest = true; |
2540 } | 2526 } |
2541 if (parameters->is_simple) { | 2527 if (parameters->is_simple) { |
2542 parameters->is_simple = !is_rest && expr->IsVariableProxy(); | 2528 parameters->is_simple = !is_rest && expr->IsVariableProxy(); |
2543 } | 2529 } |
2544 | 2530 |
2545 Expression* initializer = nullptr; | 2531 Expression* initializer = nullptr; |
2546 if (expr->IsAssignment()) { | 2532 if (expr->IsAssignment()) { |
2547 Assignment* assignment = expr->AsAssignment(); | 2533 Assignment* assignment = expr->AsAssignment(); |
2548 DCHECK(!assignment->is_compound()); | 2534 DCHECK(!assignment->is_compound()); |
2549 initializer = assignment->value(); | 2535 initializer = assignment->value(); |
2550 expr = assignment->target(); | 2536 expr = assignment->target(); |
2551 } | 2537 } |
2552 | 2538 |
2553 AddFormalParameter(parameters, expr, initializer, end_pos, is_rest); | 2539 AddFormalParameter(parameters, expr, initializer, end_pos, is_rest); |
2554 } | 2540 } |
2555 | 2541 |
2556 void Parser::DesugarAsyncFunctionBody(Scope* scope, ZoneList<Statement*>* body, | 2542 void Parser::DeclareArrowFunctionFormalParameters( |
2557 FunctionKind kind, | |
2558 FunctionBodyType body_type, | |
2559 bool accept_IN, int pos, bool* ok) { | |
2560 // function async_function() { | |
2561 // .generator_object = %CreateGeneratorObject(); | |
2562 // BuildRejectPromiseOnException({ | |
2563 // ... function body ... | |
2564 // return %ResolvePromise(.promise, expr), .promise; | |
2565 // }) | |
2566 // } | |
2567 scope->ForceContextAllocation(); | |
2568 Variable* temp = | |
2569 NewTemporary(ast_value_factory()->dot_generator_object_string()); | |
2570 function_state_->set_generator_object_variable(temp); | |
2571 | |
2572 Expression* init_generator_variable = factory()->NewAssignment( | |
2573 Token::INIT, factory()->NewVariableProxy(temp), | |
2574 BuildCreateJSGeneratorObject(pos, kind), kNoSourcePosition); | |
2575 body->Add(factory()->NewExpressionStatement(init_generator_variable, | |
2576 kNoSourcePosition), | |
2577 zone()); | |
2578 | |
2579 Block* block = factory()->NewBlock(NULL, 8, true, kNoSourcePosition); | |
2580 | |
2581 Expression* return_value = nullptr; | |
2582 if (body_type == FunctionBodyType::kNormal) { | |
2583 ParseStatementList(block->statements(), Token::RBRACE, CHECK_OK_VOID); | |
2584 return_value = factory()->NewUndefinedLiteral(kNoSourcePosition); | |
2585 } else { | |
2586 return_value = ParseAssignmentExpression(accept_IN, CHECK_OK_VOID); | |
2587 RewriteNonPattern(CHECK_OK_VOID); | |
2588 } | |
2589 | |
2590 return_value = BuildResolvePromise(return_value, return_value->position()); | |
2591 block->statements()->Add( | |
2592 factory()->NewReturnStatement(return_value, return_value->position()), | |
2593 zone()); | |
2594 block = BuildRejectPromiseOnException(block, CHECK_OK_VOID); | |
2595 body->Add(block, zone()); | |
2596 scope->set_end_position(scanner()->location().end_pos); | |
2597 } | |
2598 | |
2599 void Parser::ParseArrowFunctionFormalParameterList( | |
2600 ParserFormalParameters* parameters, Expression* expr, | 2543 ParserFormalParameters* parameters, Expression* expr, |
2601 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 2544 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, |
2602 const Scope::Snapshot& scope_snapshot, bool* ok) { | 2545 bool* ok) { |
2603 if (expr->IsEmptyParentheses()) return; | 2546 if (expr->IsEmptyParentheses()) return; |
2604 | 2547 |
2605 ParseArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, | 2548 AddArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos, |
2606 CHECK_OK_VOID); | 2549 CHECK_OK_VOID); |
2607 | |
2608 scope_snapshot.Reparent(parameters->scope); | |
2609 | 2550 |
2610 if (parameters->Arity() > Code::kMaxArguments) { | 2551 if (parameters->Arity() > Code::kMaxArguments) { |
2611 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); | 2552 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); |
2612 *ok = false; | 2553 *ok = false; |
2613 return; | 2554 return; |
2614 } | 2555 } |
2615 | 2556 |
2616 ExpressionClassifier classifier(this); | 2557 ExpressionClassifier classifier(this); |
2617 if (!parameters->is_simple) { | 2558 if (!parameters->is_simple) { |
2618 this->classifier()->RecordNonSimpleParameter(); | 2559 this->classifier()->RecordNonSimpleParameter(); |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2906 if (should_be_used_once_hint) | 2847 if (should_be_used_once_hint) |
2907 function_literal->set_should_be_used_once_hint(); | 2848 function_literal->set_should_be_used_once_hint(); |
2908 | 2849 |
2909 if (should_infer_name) { | 2850 if (should_infer_name) { |
2910 DCHECK_NOT_NULL(fni_); | 2851 DCHECK_NOT_NULL(fni_); |
2911 fni_->AddFunction(function_literal); | 2852 fni_->AddFunction(function_literal); |
2912 } | 2853 } |
2913 return function_literal; | 2854 return function_literal; |
2914 } | 2855 } |
2915 | 2856 |
2916 Expression* Parser::ParseAsyncFunctionExpression(bool* ok) { | |
2917 // AsyncFunctionDeclaration :: | |
2918 // async [no LineTerminator here] function ( FormalParameters[Await] ) | |
2919 // { AsyncFunctionBody } | |
2920 // | |
2921 // async [no LineTerminator here] function BindingIdentifier[Await] | |
2922 // ( FormalParameters[Await] ) { AsyncFunctionBody } | |
2923 DCHECK_EQ(scanner()->current_token(), Token::ASYNC); | |
2924 int pos = position(); | |
2925 Expect(Token::FUNCTION, CHECK_OK); | |
2926 bool is_strict_reserved = false; | |
2927 const AstRawString* name = nullptr; | |
2928 FunctionLiteral::FunctionType type = FunctionLiteral::kAnonymousExpression; | |
2929 | |
2930 if (peek_any_identifier()) { | |
2931 type = FunctionLiteral::kNamedExpression; | |
2932 name = ParseIdentifierOrStrictReservedWord(FunctionKind::kAsyncFunction, | |
2933 &is_strict_reserved, CHECK_OK); | |
2934 } | |
2935 return ParseFunctionLiteral(name, scanner()->location(), | |
2936 is_strict_reserved ? kFunctionNameIsStrictReserved | |
2937 : kFunctionNameValidityUnknown, | |
2938 FunctionKind::kAsyncFunction, pos, type, | |
2939 language_mode(), CHECK_OK); | |
2940 } | |
2941 | |
2942 Parser::LazyParsingResult Parser::SkipLazyFunctionBody( | 2857 Parser::LazyParsingResult Parser::SkipLazyFunctionBody( |
2943 int* materialized_literal_count, int* expected_property_count, | 2858 int* materialized_literal_count, int* expected_property_count, |
2944 bool is_inner_function, bool may_abort, bool* ok) { | 2859 bool is_inner_function, bool may_abort, bool* ok) { |
2945 if (produce_cached_parse_data()) CHECK(log_); | 2860 if (produce_cached_parse_data()) CHECK(log_); |
2946 | 2861 |
2947 int function_block_pos = position(); | 2862 int function_block_pos = position(); |
2948 DeclarationScope* scope = function_state_->scope(); | 2863 DeclarationScope* scope = function_state_->scope(); |
2949 DCHECK(scope->is_function_scope()); | 2864 DCHECK(scope->is_function_scope()); |
2950 scope->set_is_lazily_parsed(true); | 2865 scope->set_is_lazily_parsed(true); |
2951 // Inner functions are not part of the cached data. | 2866 // Inner functions are not part of the cached data. |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3361 Expression* call = factory()->NewCallRuntime( | 3276 Expression* call = factory()->NewCallRuntime( |
3362 Runtime::kInlineGeneratorClose, args, kNoSourcePosition); | 3277 Runtime::kInlineGeneratorClose, args, kNoSourcePosition); |
3363 finally_block->statements()->Add( | 3278 finally_block->statements()->Add( |
3364 factory()->NewExpressionStatement(call, kNoSourcePosition), zone()); | 3279 factory()->NewExpressionStatement(call, kNoSourcePosition), zone()); |
3365 | 3280 |
3366 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, | 3281 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, |
3367 kNoSourcePosition), | 3282 kNoSourcePosition), |
3368 zone()); | 3283 zone()); |
3369 } else if (IsAsyncFunction(kind)) { | 3284 } else if (IsAsyncFunction(kind)) { |
3370 const bool accept_IN = true; | 3285 const bool accept_IN = true; |
3371 DesugarAsyncFunctionBody(inner_scope, body, kind, | 3286 ParseAsyncFunctionBody(inner_scope, body, kind, FunctionBodyType::kNormal, |
3372 FunctionBodyType::kNormal, accept_IN, pos, | 3287 accept_IN, pos, CHECK_OK); |
3373 CHECK_OK); | |
3374 } else { | 3288 } else { |
3375 ParseStatementList(body, Token::RBRACE, CHECK_OK); | 3289 ParseStatementList(body, Token::RBRACE, CHECK_OK); |
3376 } | 3290 } |
3377 | 3291 |
3378 if (IsSubclassConstructor(kind)) { | 3292 if (IsSubclassConstructor(kind)) { |
3379 body->Add(factory()->NewReturnStatement(ThisExpression(kNoSourcePosition), | 3293 body->Add(factory()->NewReturnStatement(ThisExpression(kNoSourcePosition), |
3380 kNoSourcePosition), | 3294 kNoSourcePosition), |
3381 zone()); | 3295 zone()); |
3382 } | 3296 } |
3383 } | 3297 } |
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4338 | 4252 |
4339 Expression* Parser::ExpressionListToExpression(ZoneList<Expression*>* args) { | 4253 Expression* Parser::ExpressionListToExpression(ZoneList<Expression*>* args) { |
4340 Expression* expr = args->at(0); | 4254 Expression* expr = args->at(0); |
4341 for (int i = 1; i < args->length(); ++i) { | 4255 for (int i = 1; i < args->length(); ++i) { |
4342 expr = factory()->NewBinaryOperation(Token::COMMA, expr, args->at(i), | 4256 expr = factory()->NewBinaryOperation(Token::COMMA, expr, args->at(i), |
4343 expr->position()); | 4257 expr->position()); |
4344 } | 4258 } |
4345 return expr; | 4259 return expr; |
4346 } | 4260 } |
4347 | 4261 |
| 4262 // This method intoduces the line initializing the generator object |
| 4263 // when desugaring the body of async_function. |
| 4264 void Parser::PrepareAsyncFunctionBody(ZoneList<Statement*>* body, |
| 4265 FunctionKind kind, int pos) { |
| 4266 // function async_function() { |
| 4267 // .generator_object = %CreateGeneratorObject(); |
| 4268 // BuildRejectPromiseOnException({ |
| 4269 // ... block ... |
| 4270 // return %ResolvePromise(.promise, expr), .promise; |
| 4271 // }) |
| 4272 // } |
| 4273 |
| 4274 Variable* temp = |
| 4275 NewTemporary(ast_value_factory()->dot_generator_object_string()); |
| 4276 function_state_->set_generator_object_variable(temp); |
| 4277 |
| 4278 Expression* init_generator_variable = factory()->NewAssignment( |
| 4279 Token::INIT, factory()->NewVariableProxy(temp), |
| 4280 BuildCreateJSGeneratorObject(pos, kind), kNoSourcePosition); |
| 4281 body->Add(factory()->NewExpressionStatement(init_generator_variable, |
| 4282 kNoSourcePosition), |
| 4283 zone()); |
| 4284 } |
| 4285 |
| 4286 // This method completes the desugaring of the body of async_function. |
| 4287 void Parser::RewriteAsyncFunctionBody(ZoneList<Statement*>* body, Block* block, |
| 4288 Expression* return_value, bool* ok) { |
| 4289 // function async_function() { |
| 4290 // .generator_object = %CreateGeneratorObject(); |
| 4291 // BuildRejectPromiseOnException({ |
| 4292 // ... block ... |
| 4293 // return %ResolvePromise(.promise, expr), .promise; |
| 4294 // }) |
| 4295 // } |
| 4296 |
| 4297 return_value = BuildResolvePromise(return_value, return_value->position()); |
| 4298 block->statements()->Add( |
| 4299 factory()->NewReturnStatement(return_value, return_value->position()), |
| 4300 zone()); |
| 4301 block = BuildRejectPromiseOnException(block, CHECK_OK_VOID); |
| 4302 body->Add(block, zone()); |
| 4303 } |
| 4304 |
4348 Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) { | 4305 Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) { |
4349 // yield do { | 4306 // yield do { |
4350 // tmp = <operand>; | 4307 // tmp = <operand>; |
4351 // %AsyncFunctionAwait(.generator_object, tmp, .promise); | 4308 // %AsyncFunctionAwait(.generator_object, tmp, .promise); |
4352 // .promise | 4309 // .promise |
4353 // } | 4310 // } |
4354 // The value of the expression is returned to the caller of the async | 4311 // The value of the expression is returned to the caller of the async |
4355 // function for the first yield statement; for this, .promise is the | 4312 // function for the first yield statement; for this, .promise is the |
4356 // appropriate return value, being a Promise that will be fulfilled or | 4313 // appropriate return value, being a Promise that will be fulfilled or |
4357 // rejected with the appropriate value by the desugaring. Subsequent yield | 4314 // rejected with the appropriate value by the desugaring. Subsequent yield |
(...skipping 1247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5605 | 5562 |
5606 return final_loop; | 5563 return final_loop; |
5607 } | 5564 } |
5608 | 5565 |
5609 #undef CHECK_OK | 5566 #undef CHECK_OK |
5610 #undef CHECK_OK_VOID | 5567 #undef CHECK_OK_VOID |
5611 #undef CHECK_FAILED | 5568 #undef CHECK_FAILED |
5612 | 5569 |
5613 } // namespace internal | 5570 } // namespace internal |
5614 } // namespace v8 | 5571 } // namespace v8 |
OLD | NEW |