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/parser.h" | 5 #include "src/parser.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/ast.h" | 8 #include "src/ast.h" |
9 #include "src/ast-literal-reindexer.h" | 9 #include "src/ast-literal-reindexer.h" |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 2363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2374 } | 2374 } |
2375 } | 2375 } |
2376 Expect(Token::RBRACE, CHECK_OK); | 2376 Expect(Token::RBRACE, CHECK_OK); |
2377 block_scope->set_end_position(scanner()->location().end_pos); | 2377 block_scope->set_end_position(scanner()->location().end_pos); |
2378 block_scope = block_scope->FinalizeBlockScope(); | 2378 block_scope = block_scope->FinalizeBlockScope(); |
2379 body->set_scope(block_scope); | 2379 body->set_scope(block_scope); |
2380 return body; | 2380 return body; |
2381 } | 2381 } |
2382 | 2382 |
2383 | 2383 |
2384 const AstRawString* Parser::DeclarationParsingResult::SingleName() const { | |
2385 if (declarations.length() != 1) return nullptr; | |
2386 const Declaration& declaration = declarations.at(0); | |
2387 if (declaration.pattern->IsVariableProxy()) { | |
2388 return declaration.pattern->AsVariableProxy()->raw_name(); | |
2389 } | |
2390 return nullptr; | |
2391 } | |
2392 | |
2393 | |
2394 Block* Parser::DeclarationParsingResult::BuildInitializationBlock( | 2384 Block* Parser::DeclarationParsingResult::BuildInitializationBlock( |
2395 ZoneList<const AstRawString*>* names, bool* ok) { | 2385 ZoneList<const AstRawString*>* names, bool* ok) { |
2396 Block* result = descriptor.parser->factory()->NewBlock( | 2386 Block* result = descriptor.parser->factory()->NewBlock( |
2397 NULL, 1, true, descriptor.declaration_pos); | 2387 NULL, 1, true, descriptor.declaration_pos); |
2398 for (auto declaration : declarations) { | 2388 for (auto declaration : declarations) { |
2399 PatternRewriter::DeclareAndInitializeVariables( | 2389 PatternRewriter::DeclareAndInitializeVariables( |
2400 result, &descriptor, &declaration, names, CHECK_OK); | 2390 result, &descriptor, &declaration, names, CHECK_OK); |
2401 } | 2391 } |
2402 return result; | 2392 return result; |
2403 } | 2393 } |
(...skipping 1267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3671 if (!*ok) return nullptr; | 3661 if (!*ok) return nullptr; |
3672 if (num_decl != 1) { | 3662 if (num_decl != 1) { |
3673 const char* loop_type = | 3663 const char* loop_type = |
3674 mode == ForEachStatement::ITERATE ? "for-of" : "for-in"; | 3664 mode == ForEachStatement::ITERATE ? "for-of" : "for-in"; |
3675 ParserTraits::ReportMessageAt( | 3665 ParserTraits::ReportMessageAt( |
3676 parsing_result.bindings_loc, | 3666 parsing_result.bindings_loc, |
3677 MessageTemplate::kForInOfLoopMultiBindings, loop_type); | 3667 MessageTemplate::kForInOfLoopMultiBindings, loop_type); |
3678 *ok = false; | 3668 *ok = false; |
3679 return nullptr; | 3669 return nullptr; |
3680 } | 3670 } |
| 3671 DeclarationParsingResult::Declaration& decl = |
| 3672 parsing_result.declarations[0]; |
3681 if (parsing_result.first_initializer_loc.IsValid() && | 3673 if (parsing_result.first_initializer_loc.IsValid() && |
3682 (is_strict(language_mode()) || mode == ForEachStatement::ITERATE || | 3674 (is_strict(language_mode()) || mode == ForEachStatement::ITERATE || |
3683 IsLexicalVariableMode(parsing_result.descriptor.mode))) { | 3675 IsLexicalVariableMode(parsing_result.descriptor.mode) || |
| 3676 !decl.pattern->IsVariableProxy())) { |
3684 if (mode == ForEachStatement::ITERATE) { | 3677 if (mode == ForEachStatement::ITERATE) { |
3685 ReportMessageAt(parsing_result.first_initializer_loc, | 3678 ReportMessageAt(parsing_result.first_initializer_loc, |
3686 MessageTemplate::kForOfLoopInitializer); | 3679 MessageTemplate::kForOfLoopInitializer); |
3687 } else { | 3680 } else { |
3688 // TODO(caitp): This should be an error in sloppy mode too. | 3681 // TODO(caitp): This should be an error in sloppy mode too. |
3689 ReportMessageAt(parsing_result.first_initializer_loc, | 3682 ReportMessageAt(parsing_result.first_initializer_loc, |
3690 MessageTemplate::kForInLoopInitializer); | 3683 MessageTemplate::kForInLoopInitializer); |
3691 } | 3684 } |
3692 *ok = false; | 3685 *ok = false; |
3693 return nullptr; | 3686 return nullptr; |
3694 } | 3687 } |
3695 | 3688 |
3696 DCHECK(parsing_result.declarations.length() == 1); | |
3697 Block* init_block = nullptr; | 3689 Block* init_block = nullptr; |
3698 | 3690 |
3699 // special case for legacy for (var/const x =.... in) | 3691 // special case for legacy for (var/const x =.... in) |
3700 if (!IsLexicalVariableMode(parsing_result.descriptor.mode) && | 3692 if (!IsLexicalVariableMode(parsing_result.descriptor.mode) && |
3701 parsing_result.declarations[0].initializer != nullptr) { | 3693 decl.pattern->IsVariableProxy() && decl.initializer != nullptr) { |
| 3694 const AstRawString* name = |
| 3695 decl.pattern->AsVariableProxy()->raw_name(); |
3702 VariableProxy* single_var = scope_->NewUnresolved( | 3696 VariableProxy* single_var = scope_->NewUnresolved( |
3703 factory(), parsing_result.SingleName(), Variable::NORMAL, | 3697 factory(), name, Variable::NORMAL, each_beg_pos, each_end_pos); |
3704 each_beg_pos, each_end_pos); | |
3705 init_block = factory()->NewBlock( | 3698 init_block = factory()->NewBlock( |
3706 nullptr, 2, true, parsing_result.descriptor.declaration_pos); | 3699 nullptr, 2, true, parsing_result.descriptor.declaration_pos); |
3707 init_block->statements()->Add( | 3700 init_block->statements()->Add( |
3708 factory()->NewExpressionStatement( | 3701 factory()->NewExpressionStatement( |
3709 factory()->NewAssignment( | 3702 factory()->NewAssignment(Token::ASSIGN, single_var, |
3710 Token::ASSIGN, single_var, | 3703 decl.initializer, |
3711 parsing_result.declarations[0].initializer, | 3704 RelocInfo::kNoPosition), |
3712 RelocInfo::kNoPosition), | |
3713 RelocInfo::kNoPosition), | 3705 RelocInfo::kNoPosition), |
3714 zone()); | 3706 zone()); |
3715 } | 3707 } |
3716 | 3708 |
3717 // Rewrite a for-in/of statement of the form | 3709 // Rewrite a for-in/of statement of the form |
3718 // | 3710 // |
3719 // for (let/const/var x in/of e) b | 3711 // for (let/const/var x in/of e) b |
3720 // | 3712 // |
3721 // into | 3713 // into |
3722 // | 3714 // |
(...skipping 22 matching lines...) Expand all Loading... |
3745 scope_ = body_scope; | 3737 scope_ = body_scope; |
3746 | 3738 |
3747 Statement* body = ParseSubStatement(NULL, CHECK_OK); | 3739 Statement* body = ParseSubStatement(NULL, CHECK_OK); |
3748 | 3740 |
3749 Block* body_block = | 3741 Block* body_block = |
3750 factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition); | 3742 factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition); |
3751 | 3743 |
3752 auto each_initialization_block = | 3744 auto each_initialization_block = |
3753 factory()->NewBlock(nullptr, 1, true, RelocInfo::kNoPosition); | 3745 factory()->NewBlock(nullptr, 1, true, RelocInfo::kNoPosition); |
3754 { | 3746 { |
3755 DCHECK(parsing_result.declarations.length() == 1); | |
3756 DeclarationParsingResult::Declaration decl = | |
3757 parsing_result.declarations[0]; | |
3758 auto descriptor = parsing_result.descriptor; | 3747 auto descriptor = parsing_result.descriptor; |
3759 descriptor.declaration_pos = RelocInfo::kNoPosition; | 3748 descriptor.declaration_pos = RelocInfo::kNoPosition; |
3760 descriptor.initialization_pos = RelocInfo::kNoPosition; | 3749 descriptor.initialization_pos = RelocInfo::kNoPosition; |
3761 decl.initializer = factory()->NewVariableProxy(temp); | 3750 decl.initializer = factory()->NewVariableProxy(temp); |
3762 | 3751 |
3763 PatternRewriter::DeclareAndInitializeVariables( | 3752 PatternRewriter::DeclareAndInitializeVariables( |
3764 each_initialization_block, &descriptor, &decl, | 3753 each_initialization_block, &descriptor, &decl, |
3765 IsLexicalVariableMode(descriptor.mode) ? &lexical_bindings | 3754 IsLexicalVariableMode(descriptor.mode) ? &lexical_bindings |
3766 : nullptr, | 3755 : nullptr, |
3767 CHECK_OK); | 3756 CHECK_OK); |
(...skipping 2688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6456 } | 6445 } |
6457 | 6446 |
6458 | 6447 |
6459 void Parser::RaiseLanguageMode(LanguageMode mode) { | 6448 void Parser::RaiseLanguageMode(LanguageMode mode) { |
6460 SetLanguageMode(scope_, | 6449 SetLanguageMode(scope_, |
6461 static_cast<LanguageMode>(scope_->language_mode() | mode)); | 6450 static_cast<LanguageMode>(scope_->language_mode() | mode)); |
6462 } | 6451 } |
6463 | 6452 |
6464 } // namespace internal | 6453 } // namespace internal |
6465 } // namespace v8 | 6454 } // namespace v8 |
OLD | NEW |