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 "src/api.h" | 7 #include "src/api.h" |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
| 9 #include "src/ast/ast-expression-visitor.h" |
9 #include "src/ast/ast-literal-reindexer.h" | 10 #include "src/ast/ast-literal-reindexer.h" |
10 #include "src/ast/scopeinfo.h" | 11 #include "src/ast/scopeinfo.h" |
11 #include "src/bailout-reason.h" | 12 #include "src/bailout-reason.h" |
12 #include "src/base/platform/platform.h" | 13 #include "src/base/platform/platform.h" |
13 #include "src/bootstrapper.h" | 14 #include "src/bootstrapper.h" |
14 #include "src/char-predicates-inl.h" | 15 #include "src/char-predicates-inl.h" |
15 #include "src/codegen.h" | 16 #include "src/codegen.h" |
16 #include "src/compiler.h" | 17 #include "src/compiler.h" |
17 #include "src/messages.h" | 18 #include "src/messages.h" |
18 #include "src/parsing/parameter-initializer-rewriter.h" | 19 #include "src/parsing/parameter-initializer-rewriter.h" |
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
913 // ParseInfo during background parsing. | 914 // ParseInfo during background parsing. |
914 DCHECK(!info->script().is_null() || info->source_stream() != NULL); | 915 DCHECK(!info->script().is_null() || info->source_stream() != NULL); |
915 set_allow_lazy(info->allow_lazy_parsing()); | 916 set_allow_lazy(info->allow_lazy_parsing()); |
916 set_allow_natives(FLAG_allow_natives_syntax || info->is_native()); | 917 set_allow_natives(FLAG_allow_natives_syntax || info->is_native()); |
917 set_allow_harmony_sloppy(FLAG_harmony_sloppy); | 918 set_allow_harmony_sloppy(FLAG_harmony_sloppy); |
918 set_allow_harmony_sloppy_function(FLAG_harmony_sloppy_function); | 919 set_allow_harmony_sloppy_function(FLAG_harmony_sloppy_function); |
919 set_allow_harmony_sloppy_let(FLAG_harmony_sloppy_let); | 920 set_allow_harmony_sloppy_let(FLAG_harmony_sloppy_let); |
920 set_allow_harmony_rest_parameters(FLAG_harmony_rest_parameters); | 921 set_allow_harmony_rest_parameters(FLAG_harmony_rest_parameters); |
921 set_allow_harmony_default_parameters(FLAG_harmony_default_parameters); | 922 set_allow_harmony_default_parameters(FLAG_harmony_default_parameters); |
922 set_allow_harmony_destructuring_bind(FLAG_harmony_destructuring_bind); | 923 set_allow_harmony_destructuring_bind(FLAG_harmony_destructuring_bind); |
| 924 set_allow_harmony_destructuring_assignment( |
| 925 FLAG_harmony_destructuring_assignment); |
923 set_allow_strong_mode(FLAG_strong_mode); | 926 set_allow_strong_mode(FLAG_strong_mode); |
924 set_allow_legacy_const(FLAG_legacy_const); | 927 set_allow_legacy_const(FLAG_legacy_const); |
925 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions); | 928 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions); |
926 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; | 929 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; |
927 ++feature) { | 930 ++feature) { |
928 use_counts_[feature] = 0; | 931 use_counts_[feature] = 0; |
929 } | 932 } |
930 if (info->ast_value_factory() == NULL) { | 933 if (info->ast_value_factory() == NULL) { |
931 // info takes ownership of AstValueFactory. | 934 // info takes ownership of AstValueFactory. |
932 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); | 935 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 if (body->length() != 1 || | 1086 if (body->length() != 1 || |
1084 !body->at(0)->IsExpressionStatement() || | 1087 !body->at(0)->IsExpressionStatement() || |
1085 !body->at(0)->AsExpressionStatement()-> | 1088 !body->at(0)->AsExpressionStatement()-> |
1086 expression()->IsFunctionLiteral()) { | 1089 expression()->IsFunctionLiteral()) { |
1087 ReportMessage(MessageTemplate::kSingleFunctionLiteral); | 1090 ReportMessage(MessageTemplate::kSingleFunctionLiteral); |
1088 ok = false; | 1091 ok = false; |
1089 } | 1092 } |
1090 } | 1093 } |
1091 | 1094 |
1092 if (ok) { | 1095 if (ok) { |
| 1096 ParserTraits::RewriteDestructuringAssignments(); |
1093 result = factory()->NewFunctionLiteral( | 1097 result = factory()->NewFunctionLiteral( |
1094 ast_value_factory()->empty_string(), ast_value_factory(), scope_, | 1098 ast_value_factory()->empty_string(), ast_value_factory(), scope_, |
1095 body, function_state.materialized_literal_count(), | 1099 body, function_state.materialized_literal_count(), |
1096 function_state.expected_property_count(), 0, | 1100 function_state.expected_property_count(), 0, |
1097 FunctionLiteral::kNoDuplicateParameters, | 1101 FunctionLiteral::kNoDuplicateParameters, |
1098 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, | 1102 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, |
1099 FunctionLiteral::kShouldLazyCompile, FunctionKind::kNormalFunction, | 1103 FunctionLiteral::kShouldLazyCompile, FunctionKind::kNormalFunction, |
1100 0); | 1104 0); |
1101 } | 1105 } |
1102 } | 1106 } |
(...skipping 3289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4392 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), | 4396 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), |
4393 CHECK_OK); | 4397 CHECK_OK); |
4394 } | 4398 } |
4395 if (is_sloppy(language_mode) && allow_harmony_sloppy_function()) { | 4399 if (is_sloppy(language_mode) && allow_harmony_sloppy_function()) { |
4396 InsertSloppyBlockFunctionVarBindings(scope, CHECK_OK); | 4400 InsertSloppyBlockFunctionVarBindings(scope, CHECK_OK); |
4397 } | 4401 } |
4398 if (is_strict(language_mode) || allow_harmony_sloppy() || | 4402 if (is_strict(language_mode) || allow_harmony_sloppy() || |
4399 allow_harmony_destructuring_bind()) { | 4403 allow_harmony_destructuring_bind()) { |
4400 CheckConflictingVarDeclarations(scope, CHECK_OK); | 4404 CheckConflictingVarDeclarations(scope, CHECK_OK); |
4401 } | 4405 } |
| 4406 |
| 4407 if (body) { |
| 4408 // If body can be inspected, rewrite queued destructuring assignments |
| 4409 ParserTraits::RewriteDestructuringAssignments(); |
| 4410 } |
4402 } | 4411 } |
4403 | 4412 |
4404 bool has_duplicate_parameters = | 4413 bool has_duplicate_parameters = |
4405 !formals_classifier.is_valid_formal_parameter_list_without_duplicates(); | 4414 !formals_classifier.is_valid_formal_parameter_list_without_duplicates(); |
4406 FunctionLiteral::ParameterFlag duplicate_parameters = | 4415 FunctionLiteral::ParameterFlag duplicate_parameters = |
4407 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters | 4416 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters |
4408 : FunctionLiteral::kNoDuplicateParameters; | 4417 : FunctionLiteral::kNoDuplicateParameters; |
4409 | 4418 |
4410 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 4419 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
4411 function_name, ast_value_factory(), scope, body, | 4420 function_name, ast_value_factory(), scope, body, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4520 RelocInfo::kNoPosition); | 4529 RelocInfo::kNoPosition); |
4521 IfStatement* if_statement = factory()->NewIfStatement( | 4530 IfStatement* if_statement = factory()->NewIfStatement( |
4522 condition, factory()->NewExpressionStatement(throw_type_error, | 4531 condition, factory()->NewExpressionStatement(throw_type_error, |
4523 RelocInfo::kNoPosition), | 4532 RelocInfo::kNoPosition), |
4524 factory()->NewEmptyStatement(RelocInfo::kNoPosition), | 4533 factory()->NewEmptyStatement(RelocInfo::kNoPosition), |
4525 RelocInfo::kNoPosition); | 4534 RelocInfo::kNoPosition); |
4526 return if_statement; | 4535 return if_statement; |
4527 } | 4536 } |
4528 | 4537 |
4529 | 4538 |
| 4539 class InitializerRewriter : public AstExpressionVisitor { |
| 4540 public: |
| 4541 InitializerRewriter(uintptr_t stack_limit, Expression* root, Parser* parser, |
| 4542 Scope* scope) |
| 4543 : AstExpressionVisitor(stack_limit, root), |
| 4544 parser_(parser), |
| 4545 scope_(scope) {} |
| 4546 |
| 4547 private: |
| 4548 void VisitExpression(Expression* expr) { |
| 4549 RewritableExpression* to_rewrite = expr->AsRewritableExpression(); |
| 4550 if (to_rewrite == nullptr || to_rewrite->is_rewritten()) return; |
| 4551 |
| 4552 bool ok = true; |
| 4553 Parser::PatternRewriter::RewriteDestructuringAssignment(parser_, to_rewrite, |
| 4554 scope_, &ok); |
| 4555 DCHECK(ok); |
| 4556 } |
| 4557 |
| 4558 private: |
| 4559 Parser* parser_; |
| 4560 Scope* scope_; |
| 4561 }; |
| 4562 |
| 4563 |
| 4564 void Parser::RewriteParameterInitializer(Expression* expr, Scope* scope) { |
| 4565 InitializerRewriter rewriter(stack_limit_, expr, this, scope); |
| 4566 rewriter.Run(); |
| 4567 } |
| 4568 |
| 4569 |
4530 Block* Parser::BuildParameterInitializationBlock( | 4570 Block* Parser::BuildParameterInitializationBlock( |
4531 const ParserFormalParameters& parameters, bool* ok) { | 4571 const ParserFormalParameters& parameters, bool* ok) { |
4532 DCHECK(!parameters.is_simple); | 4572 DCHECK(!parameters.is_simple); |
4533 DCHECK(scope_->is_function_scope()); | 4573 DCHECK(scope_->is_function_scope()); |
4534 Block* init_block = | 4574 Block* init_block = |
4535 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); | 4575 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); |
4536 for (int i = 0; i < parameters.params.length(); ++i) { | 4576 for (int i = 0; i < parameters.params.length(); ++i) { |
4537 auto parameter = parameters.params[i]; | 4577 auto parameter = parameters.params[i]; |
4538 DeclarationDescriptor descriptor; | 4578 DeclarationDescriptor descriptor; |
4539 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; | 4579 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; |
(...skipping 12 matching lines...) Expand all Loading... |
4552 // it's just copying from a temp var to the real param var? | 4592 // it's just copying from a temp var to the real param var? |
4553 descriptor.initialization_pos = parameter.pattern->position(); | 4593 descriptor.initialization_pos = parameter.pattern->position(); |
4554 // The initializer position which will end up in, | 4594 // The initializer position which will end up in, |
4555 // Variable::initializer_position(), used for hole check elimination. | 4595 // Variable::initializer_position(), used for hole check elimination. |
4556 int initializer_position = parameter.pattern->position(); | 4596 int initializer_position = parameter.pattern->position(); |
4557 Expression* initial_value = | 4597 Expression* initial_value = |
4558 factory()->NewVariableProxy(parameters.scope->parameter(i)); | 4598 factory()->NewVariableProxy(parameters.scope->parameter(i)); |
4559 if (parameter.initializer != nullptr) { | 4599 if (parameter.initializer != nullptr) { |
4560 // IS_UNDEFINED($param) ? initializer : $param | 4600 // IS_UNDEFINED($param) ? initializer : $param |
4561 DCHECK(!parameter.is_rest); | 4601 DCHECK(!parameter.is_rest); |
| 4602 |
| 4603 // Ensure initializer is rewritten |
| 4604 RewriteParameterInitializer(parameter.initializer, scope_); |
| 4605 |
4562 auto condition = factory()->NewCompareOperation( | 4606 auto condition = factory()->NewCompareOperation( |
4563 Token::EQ_STRICT, | 4607 Token::EQ_STRICT, |
4564 factory()->NewVariableProxy(parameters.scope->parameter(i)), | 4608 factory()->NewVariableProxy(parameters.scope->parameter(i)), |
4565 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), | 4609 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), |
4566 RelocInfo::kNoPosition); | 4610 RelocInfo::kNoPosition); |
4567 initial_value = factory()->NewConditional( | 4611 initial_value = factory()->NewConditional( |
4568 condition, parameter.initializer, initial_value, | 4612 condition, parameter.initializer, initial_value, |
4569 RelocInfo::kNoPosition); | 4613 RelocInfo::kNoPosition); |
4570 descriptor.initialization_pos = parameter.initializer->position(); | 4614 descriptor.initialization_pos = parameter.initializer->position(); |
4571 initializer_position = parameter.initializer_end_position; | 4615 initializer_position = parameter.initializer_end_position; |
(...skipping 1883 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6455 ++use_counts_[feature]; | 6499 ++use_counts_[feature]; |
6456 scope->SetLanguageMode(mode); | 6500 scope->SetLanguageMode(mode); |
6457 } | 6501 } |
6458 | 6502 |
6459 | 6503 |
6460 void Parser::RaiseLanguageMode(LanguageMode mode) { | 6504 void Parser::RaiseLanguageMode(LanguageMode mode) { |
6461 SetLanguageMode(scope_, | 6505 SetLanguageMode(scope_, |
6462 static_cast<LanguageMode>(scope_->language_mode() | mode)); | 6506 static_cast<LanguageMode>(scope_->language_mode() | mode)); |
6463 } | 6507 } |
6464 | 6508 |
| 6509 |
| 6510 void ParserTraits::RewriteDestructuringAssignments() { |
| 6511 parser_->RewriteDestructuringAssignments(); |
| 6512 } |
| 6513 |
| 6514 |
| 6515 void Parser::RewriteDestructuringAssignments() { |
| 6516 FunctionState* func = function_state_; |
| 6517 if (!allow_harmony_destructuring_assignment()) return; |
| 6518 const List<DestructuringAssignment>& assignments = |
| 6519 func->destructuring_assignments_to_rewrite(); |
| 6520 for (int i = assignments.length() - 1; i >= 0; --i) { |
| 6521 // Rewrite list in reverse, so that nested assignment patterns are rewritten |
| 6522 // correctly. |
| 6523 DestructuringAssignment pair = assignments.at(i); |
| 6524 RewritableExpression* to_rewrite = |
| 6525 pair.assignment->AsRewritableExpression(); |
| 6526 Scope* scope = pair.scope; |
| 6527 DCHECK_NOT_NULL(to_rewrite); |
| 6528 if (!to_rewrite->is_rewritten()) { |
| 6529 bool ok = true; |
| 6530 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite, scope, |
| 6531 &ok); |
| 6532 DCHECK(ok); |
| 6533 } |
| 6534 } |
| 6535 } |
| 6536 |
| 6537 |
| 6538 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) { |
| 6539 DCHECK(expr->IsRewritableExpression()); |
| 6540 DCHECK(expr->AsRewritableExpression()->expression()->IsAssignment()); |
| 6541 parser_->function_state_->AddDestructuringAssignment( |
| 6542 Parser::DestructuringAssignment(expr, parser_->scope_)); |
| 6543 } |
| 6544 |
| 6545 |
6465 } // namespace internal | 6546 } // namespace internal |
6466 } // namespace v8 | 6547 } // namespace v8 |
OLD | NEW |