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

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

Issue 1309813007: [es6] implement destructuring assignment (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Oh god it seems to work again thank goodness Created 5 years 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 "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
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
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
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
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 ||
4551 to_rewrite->IsRewrittenAs(
4552 RewritableExpression::kDestructuringAssignment)) {
4553 return;
4554 }
4555
4556 bool ok = true;
4557 Parser::PatternRewriter::RewriteDestructuringAssignment(parser_, to_rewrite,
4558 scope_, &ok);
4559 DCHECK(ok);
4560 }
4561
4562 private:
4563 Parser* parser_;
4564 Scope* scope_;
4565 };
4566
4567
4568 void Parser::RewriteParameterInitializer(Expression* expr, Scope* scope) {
4569 InitializerRewriter rewriter(stack_limit_, expr, this, scope);
4570 rewriter.Run();
4571 }
4572
4573
4530 Block* Parser::BuildParameterInitializationBlock( 4574 Block* Parser::BuildParameterInitializationBlock(
4531 const ParserFormalParameters& parameters, bool* ok) { 4575 const ParserFormalParameters& parameters, bool* ok) {
4532 DCHECK(!parameters.is_simple); 4576 DCHECK(!parameters.is_simple);
4533 DCHECK(scope_->is_function_scope()); 4577 DCHECK(scope_->is_function_scope());
4534 Block* init_block = 4578 Block* init_block =
4535 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); 4579 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
4536 for (int i = 0; i < parameters.params.length(); ++i) { 4580 for (int i = 0; i < parameters.params.length(); ++i) {
4537 auto parameter = parameters.params[i]; 4581 auto parameter = parameters.params[i];
4538 DeclarationDescriptor descriptor; 4582 DeclarationDescriptor descriptor;
4539 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; 4583 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
(...skipping 12 matching lines...) Expand all
4552 // it's just copying from a temp var to the real param var? 4596 // it's just copying from a temp var to the real param var?
4553 descriptor.initialization_pos = parameter.pattern->position(); 4597 descriptor.initialization_pos = parameter.pattern->position();
4554 // The initializer position which will end up in, 4598 // The initializer position which will end up in,
4555 // Variable::initializer_position(), used for hole check elimination. 4599 // Variable::initializer_position(), used for hole check elimination.
4556 int initializer_position = parameter.pattern->position(); 4600 int initializer_position = parameter.pattern->position();
4557 Expression* initial_value = 4601 Expression* initial_value =
4558 factory()->NewVariableProxy(parameters.scope->parameter(i)); 4602 factory()->NewVariableProxy(parameters.scope->parameter(i));
4559 if (parameter.initializer != nullptr) { 4603 if (parameter.initializer != nullptr) {
4560 // IS_UNDEFINED($param) ? initializer : $param 4604 // IS_UNDEFINED($param) ? initializer : $param
4561 DCHECK(!parameter.is_rest); 4605 DCHECK(!parameter.is_rest);
4606
4607 // Ensure initializer is rewritten
4608 RewriteParameterInitializer(parameter.initializer, scope_);
4609
4562 auto condition = factory()->NewCompareOperation( 4610 auto condition = factory()->NewCompareOperation(
4563 Token::EQ_STRICT, 4611 Token::EQ_STRICT,
4564 factory()->NewVariableProxy(parameters.scope->parameter(i)), 4612 factory()->NewVariableProxy(parameters.scope->parameter(i)),
4565 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), 4613 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
4566 RelocInfo::kNoPosition); 4614 RelocInfo::kNoPosition);
4567 initial_value = factory()->NewConditional( 4615 initial_value = factory()->NewConditional(
4568 condition, parameter.initializer, initial_value, 4616 condition, parameter.initializer, initial_value,
4569 RelocInfo::kNoPosition); 4617 RelocInfo::kNoPosition);
4570 descriptor.initialization_pos = parameter.initializer->position(); 4618 descriptor.initialization_pos = parameter.initializer->position();
4571 initializer_position = parameter.initializer_end_position; 4619 initializer_position = parameter.initializer_end_position;
(...skipping 1883 matching lines...) Expand 10 before | Expand all | Expand 10 after
6455 ++use_counts_[feature]; 6503 ++use_counts_[feature];
6456 scope->SetLanguageMode(mode); 6504 scope->SetLanguageMode(mode);
6457 } 6505 }
6458 6506
6459 6507
6460 void Parser::RaiseLanguageMode(LanguageMode mode) { 6508 void Parser::RaiseLanguageMode(LanguageMode mode) {
6461 SetLanguageMode(scope_, 6509 SetLanguageMode(scope_,
6462 static_cast<LanguageMode>(scope_->language_mode() | mode)); 6510 static_cast<LanguageMode>(scope_->language_mode() | mode));
6463 } 6511 }
6464 6512
6513
6514 void ParserTraits::RewriteDestructuringAssignments() {
6515 parser_->RewriteDestructuringAssignments();
6516 }
6517
6518
6519 void Parser::RewriteDestructuringAssignments() {
6520 FunctionState* func = function_state_;
6521 if (!allow_harmony_destructuring_assignment()) return;
6522 const List<DestructuringAssignment>& assignments =
6523 func->destructuring_assignments_to_rewrite();
6524 for (int i = assignments.length() - 1; i >= 0; --i) {
6525 // Rewrite list in reverse, so that nested assignment patterns are rewritten
6526 // correctly.
6527 DestructuringAssignment pair = assignments.at(i);
6528 RewritableExpression* to_rewrite =
6529 pair.assignment->AsRewritableExpression();
6530 Scope* scope = pair.scope;
6531 DCHECK_NOT_NULL(to_rewrite);
6532 if (!to_rewrite->IsRewrittenAs(
6533 RewritableExpression::kDestructuringAssignment)) {
6534 bool ok = true;
6535 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite, scope,
6536 &ok);
6537 DCHECK(ok);
6538 }
6539 }
6540 }
6541
6542
6543 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) {
6544 DCHECK(expr->IsRewritableExpression());
6545 DCHECK(expr->AsRewritableExpression()->expression()->IsAssignment());
6546 DCHECK(expr->AsRewritableExpression()->hints() &
6547 RewritableExpression::kDestructuringAssignment);
6548 parser_->function_state_->AddDestructuringAssignment(
6549 Parser::DestructuringAssignment(expr, parser_->scope_));
6550 }
6551
6552
6465 } // namespace internal 6553 } // namespace internal
6466 } // namespace v8 6554 } // namespace v8
OLDNEW
« src/ast/ast.h ('K') | « src/parsing/parser.h ('k') | src/parsing/pattern-rewriter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698