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

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

Issue 1712203002: Revert of Non-pattern rewriting revisited (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 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
« no previous file with comments | « src/parsing/parser.h ('k') | src/parsing/parser-base.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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-rewriter.h" 9 #include "src/ast/ast-expression-rewriter.h"
10 #include "src/ast/ast-expression-visitor.h" 10 #include "src/ast/ast-expression-visitor.h"
(...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 // These two bits only need to be explicitly set because we're 1056 // These two bits only need to be explicitly set because we're
1057 // not passing the ScopeInfo to the Scope constructor. 1057 // not passing the ScopeInfo to the Scope constructor.
1058 // TODO(adamk): Remove these calls once the above NewScope call 1058 // TODO(adamk): Remove these calls once the above NewScope call
1059 // passes the ScopeInfo. 1059 // passes the ScopeInfo.
1060 if (shared_info->scope_info()->CallsEval()) { 1060 if (shared_info->scope_info()->CallsEval()) {
1061 scope->RecordEvalCall(); 1061 scope->RecordEvalCall();
1062 } 1062 }
1063 SetLanguageMode(scope, shared_info->language_mode()); 1063 SetLanguageMode(scope, shared_info->language_mode());
1064 1064
1065 scope->set_start_position(shared_info->start_position()); 1065 scope->set_start_position(shared_info->start_position());
1066 ExpressionClassifier formals_classifier(this); 1066 ExpressionClassifier formals_classifier;
1067 ParserFormalParameters formals(scope); 1067 ParserFormalParameters formals(scope);
1068 Checkpoint checkpoint(this); 1068 Checkpoint checkpoint(this);
1069 { 1069 {
1070 // Parsing patterns as variable reference expression creates 1070 // Parsing patterns as variable reference expression creates
1071 // NewUnresolved references in current scope. Entrer arrow function 1071 // NewUnresolved references in current scope. Entrer arrow function
1072 // scope for formal parameter parsing. 1072 // scope for formal parameter parsing.
1073 BlockState block_state(&scope_, scope); 1073 BlockState block_state(&scope_, scope);
1074 if (Check(Token::LPAREN)) { 1074 if (Check(Token::LPAREN)) {
1075 // '(' StrictFormalParameters ')' 1075 // '(' StrictFormalParameters ')'
1076 ParseFormalParameterList(&formals, &formals_classifier, &ok); 1076 ParseFormalParameterList(&formals, &formals_classifier, &ok);
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after
1598 ParseClassLiteral(default_string, Scanner::Location::invalid(), 1598 ParseClassLiteral(default_string, Scanner::Location::invalid(),
1599 false, position(), CHECK_OK); 1599 false, position(), CHECK_OK);
1600 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); 1600 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1601 } else { 1601 } else {
1602 result = ParseClassDeclaration(&names, CHECK_OK); 1602 result = ParseClassDeclaration(&names, CHECK_OK);
1603 } 1603 }
1604 break; 1604 break;
1605 1605
1606 default: { 1606 default: {
1607 int pos = peek_position(); 1607 int pos = peek_position();
1608 ExpressionClassifier classifier(this); 1608 ExpressionClassifier classifier;
1609 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK); 1609 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK);
1610 RewriteNonPattern(&classifier, CHECK_OK); 1610 expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK);
1611 1611
1612 ExpectSemicolon(CHECK_OK); 1612 ExpectSemicolon(CHECK_OK);
1613 result = factory()->NewExpressionStatement(expr, pos); 1613 result = factory()->NewExpressionStatement(expr, pos);
1614 break; 1614 break;
1615 } 1615 }
1616 } 1616 }
1617 1617
1618 DCHECK_LE(names.length(), 1); 1618 DCHECK_LE(names.length(), 1);
1619 if (names.length() == 1) { 1619 if (names.length() == 1) {
1620 scope_->module()->AddLocalExport(default_string, names.first(), zone(), ok); 1620 scope_->module()->AddLocalExport(default_string, names.first(), zone(), ok);
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after
2370 int bindings_start = peek_position(); 2370 int bindings_start = peek_position();
2371 do { 2371 do {
2372 FuncNameInferrer::State fni_state(fni_); 2372 FuncNameInferrer::State fni_state(fni_);
2373 2373
2374 // Parse name. 2374 // Parse name.
2375 if (!first_declaration) Consume(Token::COMMA); 2375 if (!first_declaration) Consume(Token::COMMA);
2376 2376
2377 Expression* pattern; 2377 Expression* pattern;
2378 int decl_pos = peek_position(); 2378 int decl_pos = peek_position();
2379 { 2379 {
2380 ExpressionClassifier pattern_classifier(this); 2380 ExpressionClassifier pattern_classifier;
2381 Token::Value next = peek(); 2381 Token::Value next = peek();
2382 pattern = ParsePrimaryExpression(&pattern_classifier, CHECK_OK); 2382 pattern = ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
2383 ValidateBindingPattern(&pattern_classifier, CHECK_OK); 2383 ValidateBindingPattern(&pattern_classifier, CHECK_OK);
2384 if (IsLexicalVariableMode(parsing_result->descriptor.mode)) { 2384 if (IsLexicalVariableMode(parsing_result->descriptor.mode)) {
2385 ValidateLetPattern(&pattern_classifier, CHECK_OK); 2385 ValidateLetPattern(&pattern_classifier, CHECK_OK);
2386 } 2386 }
2387 if (!allow_harmony_destructuring_bind() && !pattern->IsVariableProxy()) { 2387 if (!allow_harmony_destructuring_bind() && !pattern->IsVariableProxy()) {
2388 ReportUnexpectedToken(next); 2388 ReportUnexpectedToken(next);
2389 *ok = false; 2389 *ok = false;
2390 return nullptr; 2390 return nullptr;
2391 } 2391 }
2392 } 2392 }
2393 2393
2394 Scanner::Location variable_loc = scanner()->location(); 2394 Scanner::Location variable_loc = scanner()->location();
2395 const AstRawString* single_name = 2395 const AstRawString* single_name =
2396 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name() 2396 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name()
2397 : nullptr; 2397 : nullptr;
2398 if (single_name != nullptr) { 2398 if (single_name != nullptr) {
2399 if (fni_ != NULL) fni_->PushVariableName(single_name); 2399 if (fni_ != NULL) fni_->PushVariableName(single_name);
2400 } 2400 }
2401 2401
2402 Expression* value = NULL; 2402 Expression* value = NULL;
2403 int initializer_position = RelocInfo::kNoPosition; 2403 int initializer_position = RelocInfo::kNoPosition;
2404 if (Check(Token::ASSIGN)) { 2404 if (Check(Token::ASSIGN)) {
2405 ExpressionClassifier classifier(this); 2405 ExpressionClassifier classifier;
2406 value = ParseAssignmentExpression(var_context != kForStatement, 2406 value = ParseAssignmentExpression(var_context != kForStatement,
2407 &classifier, CHECK_OK); 2407 &classifier, CHECK_OK);
2408 RewriteNonPattern(&classifier, ok); 2408 value = ParserTraits::RewriteNonPattern(value, &classifier, CHECK_OK);
2409 variable_loc.end_pos = scanner()->location().end_pos; 2409 variable_loc.end_pos = scanner()->location().end_pos;
2410 2410
2411 if (!parsing_result->first_initializer_loc.IsValid()) { 2411 if (!parsing_result->first_initializer_loc.IsValid()) {
2412 parsing_result->first_initializer_loc = variable_loc; 2412 parsing_result->first_initializer_loc = variable_loc;
2413 } 2413 }
2414 2414
2415 // Don't infer if it is "a = function(){...}();"-like expression. 2415 // Don't infer if it is "a = function(){...}();"-like expression.
2416 if (single_name) { 2416 if (single_name) {
2417 if (fni_ != NULL && value->AsCall() == NULL && 2417 if (fni_ != NULL && value->AsCall() == NULL &&
2418 value->AsCallNew() == NULL) { 2418 value->AsCallNew() == NULL) {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
2514 return nullptr; 2514 return nullptr;
2515 2515
2516 case Token::THIS: 2516 case Token::THIS:
2517 if (!FLAG_strong_this) break; 2517 if (!FLAG_strong_this) break;
2518 // Fall through. 2518 // Fall through.
2519 case Token::SUPER: 2519 case Token::SUPER:
2520 if (is_strong(language_mode()) && 2520 if (is_strong(language_mode()) &&
2521 IsClassConstructor(function_state_->kind())) { 2521 IsClassConstructor(function_state_->kind())) {
2522 bool is_this = peek() == Token::THIS; 2522 bool is_this = peek() == Token::THIS;
2523 Expression* expr; 2523 Expression* expr;
2524 ExpressionClassifier classifier(this); 2524 ExpressionClassifier classifier;
2525 if (is_this) { 2525 if (is_this) {
2526 expr = ParseStrongInitializationExpression(&classifier, CHECK_OK); 2526 expr = ParseStrongInitializationExpression(&classifier, CHECK_OK);
2527 } else { 2527 } else {
2528 expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK); 2528 expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK);
2529 } 2529 }
2530 RewriteNonPattern(&classifier, CHECK_OK); 2530 expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK);
2531 switch (peek()) { 2531 switch (peek()) {
2532 case Token::SEMICOLON: 2532 case Token::SEMICOLON:
2533 Consume(Token::SEMICOLON); 2533 Consume(Token::SEMICOLON);
2534 break; 2534 break;
2535 case Token::RBRACE: 2535 case Token::RBRACE:
2536 case Token::EOS: 2536 case Token::EOS:
2537 break; 2537 break;
2538 default: 2538 default:
2539 if (!scanner()->HasAnyLineTerminatorBeforeNext()) { 2539 if (!scanner()->HasAnyLineTerminatorBeforeNext()) {
2540 ReportMessageAt(function_state_->this_location(), 2540 ReportMessageAt(function_state_->this_location(),
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
3053 Variable* catch_variable = NULL; 3053 Variable* catch_variable = NULL;
3054 Block* catch_block = NULL; 3054 Block* catch_block = NULL;
3055 List<Expression*> expressions_in_tail_position_in_catch_block; 3055 List<Expression*> expressions_in_tail_position_in_catch_block;
3056 if (tok == Token::CATCH) { 3056 if (tok == Token::CATCH) {
3057 Consume(Token::CATCH); 3057 Consume(Token::CATCH);
3058 3058
3059 Expect(Token::LPAREN, CHECK_OK); 3059 Expect(Token::LPAREN, CHECK_OK);
3060 catch_scope = NewScope(scope_, CATCH_SCOPE); 3060 catch_scope = NewScope(scope_, CATCH_SCOPE);
3061 catch_scope->set_start_position(scanner()->location().beg_pos); 3061 catch_scope->set_start_position(scanner()->location().beg_pos);
3062 3062
3063 ExpressionClassifier pattern_classifier(this); 3063 ExpressionClassifier pattern_classifier;
3064 Expression* pattern = ParsePrimaryExpression(&pattern_classifier, CHECK_OK); 3064 Expression* pattern = ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
3065 ValidateBindingPattern(&pattern_classifier, CHECK_OK); 3065 ValidateBindingPattern(&pattern_classifier, CHECK_OK);
3066 3066
3067 const AstRawString* name = ast_value_factory()->dot_catch_string(); 3067 const AstRawString* name = ast_value_factory()->dot_catch_string();
3068 bool is_simple = pattern->IsVariableProxy(); 3068 bool is_simple = pattern->IsVariableProxy();
3069 if (is_simple) { 3069 if (is_simple) {
3070 auto proxy = pattern->AsVariableProxy(); 3070 auto proxy = pattern->AsVariableProxy();
3071 scope_->RemoveUnresolved(proxy); 3071 scope_->RemoveUnresolved(proxy);
3072 name = proxy->raw_name(); 3072 name = proxy->raw_name();
3073 } 3073 }
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
3683 // } 3683 // }
3684 3684
3685 Variable* temp = 3685 Variable* temp =
3686 scope_->NewTemporary(ast_value_factory()->dot_for_string()); 3686 scope_->NewTemporary(ast_value_factory()->dot_for_string());
3687 ForEachStatement* loop = 3687 ForEachStatement* loop =
3688 factory()->NewForEachStatement(mode, labels, stmt_pos); 3688 factory()->NewForEachStatement(mode, labels, stmt_pos);
3689 Target target(&this->target_stack_, loop); 3689 Target target(&this->target_stack_, loop);
3690 3690
3691 Expression* enumerable; 3691 Expression* enumerable;
3692 if (mode == ForEachStatement::ITERATE) { 3692 if (mode == ForEachStatement::ITERATE) {
3693 ExpressionClassifier classifier(this); 3693 ExpressionClassifier classifier;
3694 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK); 3694 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK);
3695 RewriteNonPattern(&classifier, CHECK_OK); 3695 enumerable = ParserTraits::RewriteNonPattern(enumerable, &classifier,
3696 CHECK_OK);
3696 } else { 3697 } else {
3697 enumerable = ParseExpression(true, CHECK_OK); 3698 enumerable = ParseExpression(true, CHECK_OK);
3698 } 3699 }
3699 3700
3700 Expect(Token::RPAREN, CHECK_OK); 3701 Expect(Token::RPAREN, CHECK_OK);
3701 3702
3702 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE); 3703 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE);
3703 body_scope->set_start_position(scanner()->location().beg_pos); 3704 body_scope->set_start_position(scanner()->location().beg_pos);
3704 3705
3705 Block* body_block = 3706 Block* body_block =
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
3775 } 3776 }
3776 } else { 3777 } else {
3777 init = parsing_result.BuildInitializationBlock( 3778 init = parsing_result.BuildInitializationBlock(
3778 IsLexicalVariableMode(parsing_result.descriptor.mode) 3779 IsLexicalVariableMode(parsing_result.descriptor.mode)
3779 ? &lexical_bindings 3780 ? &lexical_bindings
3780 : nullptr, 3781 : nullptr,
3781 CHECK_OK); 3782 CHECK_OK);
3782 } 3783 }
3783 } else { 3784 } else {
3784 int lhs_beg_pos = peek_position(); 3785 int lhs_beg_pos = peek_position();
3785 ExpressionClassifier classifier(this); 3786 ExpressionClassifier classifier;
3786 Expression* expression = ParseExpression(false, &classifier, CHECK_OK); 3787 Expression* expression = ParseExpression(false, &classifier, CHECK_OK);
3787 int lhs_end_pos = scanner()->location().end_pos; 3788 int lhs_end_pos = scanner()->location().end_pos;
3788 ForEachStatement::VisitMode mode; 3789 ForEachStatement::VisitMode mode;
3789 is_let_identifier_expression = 3790 is_let_identifier_expression =
3790 expression->IsVariableProxy() && 3791 expression->IsVariableProxy() &&
3791 expression->AsVariableProxy()->raw_name() == 3792 expression->AsVariableProxy()->raw_name() ==
3792 ast_value_factory()->let_string(); 3793 ast_value_factory()->let_string();
3793 3794
3794 bool is_for_each = CheckInOrOf(&mode, ok); 3795 bool is_for_each = CheckInOrOf(&mode, ok);
3795 if (!*ok) return nullptr; 3796 if (!*ok) return nullptr;
3796 bool is_destructuring = 3797 bool is_destructuring =
3797 is_for_each && allow_harmony_destructuring_assignment() && 3798 is_for_each && allow_harmony_destructuring_assignment() &&
3798 (expression->IsArrayLiteral() || expression->IsObjectLiteral()); 3799 (expression->IsArrayLiteral() || expression->IsObjectLiteral());
3799 3800
3800 if (is_destructuring) { 3801 if (is_destructuring) {
3801 ValidateAssignmentPattern(&classifier, CHECK_OK); 3802 ValidateAssignmentPattern(&classifier, CHECK_OK);
3802 } else { 3803 } else {
3803 RewriteNonPattern(&classifier, CHECK_OK); 3804 expression =
3805 ParserTraits::RewriteNonPattern(expression, &classifier, CHECK_OK);
3804 } 3806 }
3805 3807
3806 if (is_for_each) { 3808 if (is_for_each) {
3807 if (!is_destructuring) { 3809 if (!is_destructuring) {
3808 expression = this->CheckAndRewriteReferenceExpression( 3810 expression = this->CheckAndRewriteReferenceExpression(
3809 expression, lhs_beg_pos, lhs_end_pos, 3811 expression, lhs_beg_pos, lhs_end_pos,
3810 MessageTemplate::kInvalidLhsInFor, kSyntaxError, CHECK_OK); 3812 MessageTemplate::kInvalidLhsInFor, kSyntaxError, CHECK_OK);
3811 } 3813 }
3812 3814
3813 ForEachStatement* loop = 3815 ForEachStatement* loop =
3814 factory()->NewForEachStatement(mode, labels, stmt_pos); 3816 factory()->NewForEachStatement(mode, labels, stmt_pos);
3815 Target target(&this->target_stack_, loop); 3817 Target target(&this->target_stack_, loop);
3816 3818
3817 Expression* enumerable; 3819 Expression* enumerable;
3818 if (mode == ForEachStatement::ITERATE) { 3820 if (mode == ForEachStatement::ITERATE) {
3819 ExpressionClassifier classifier(this); 3821 ExpressionClassifier classifier;
3820 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK); 3822 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK);
3821 RewriteNonPattern(&classifier, CHECK_OK); 3823 enumerable = ParserTraits::RewriteNonPattern(enumerable, &classifier,
3824 CHECK_OK);
3822 } else { 3825 } else {
3823 enumerable = ParseExpression(true, CHECK_OK); 3826 enumerable = ParseExpression(true, CHECK_OK);
3824 } 3827 }
3825 3828
3826 Expect(Token::RPAREN, CHECK_OK); 3829 Expect(Token::RPAREN, CHECK_OK);
3827 3830
3828 // Make a block around the statement in case a lexical binding 3831 // Make a block around the statement in case a lexical binding
3829 // is introduced, e.g. by a FunctionDeclaration. 3832 // is introduced, e.g. by a FunctionDeclaration.
3830 // This block must not use for_scope as its scope because if a 3833 // This block must not use for_scope as its scope because if a
3831 // lexical binding is introduced which overlaps with the for-in/of, 3834 // lexical binding is introduced which overlaps with the for-in/of,
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
4103 4106
4104 void ParserTraits::ParseArrowFunctionFormalParameterList( 4107 void ParserTraits::ParseArrowFunctionFormalParameterList(
4105 ParserFormalParameters* parameters, Expression* expr, 4108 ParserFormalParameters* parameters, Expression* expr,
4106 const Scanner::Location& params_loc, 4109 const Scanner::Location& params_loc,
4107 Scanner::Location* duplicate_loc, bool* ok) { 4110 Scanner::Location* duplicate_loc, bool* ok) {
4108 if (expr->IsEmptyParentheses()) return; 4111 if (expr->IsEmptyParentheses()) return;
4109 4112
4110 ParseArrowFunctionFormalParameters(parameters, expr, params_loc, ok); 4113 ParseArrowFunctionFormalParameters(parameters, expr, params_loc, ok);
4111 if (!*ok) return; 4114 if (!*ok) return;
4112 4115
4113 Type::ExpressionClassifier classifier(parser_); 4116 ExpressionClassifier classifier;
4114 if (!parameters->is_simple) { 4117 if (!parameters->is_simple) {
4115 classifier.RecordNonSimpleParameter(); 4118 classifier.RecordNonSimpleParameter();
4116 } 4119 }
4117 for (int i = 0; i < parameters->Arity(); ++i) { 4120 for (int i = 0; i < parameters->Arity(); ++i) {
4118 auto parameter = parameters->at(i); 4121 auto parameter = parameters->at(i);
4119 DeclareFormalParameter(parameters->scope, parameter, &classifier); 4122 DeclareFormalParameter(parameters->scope, parameter, &classifier);
4120 if (!duplicate_loc->IsValid()) { 4123 if (!duplicate_loc->IsValid()) {
4121 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; 4124 *duplicate_loc = classifier.duplicate_formal_parameter_error().location;
4122 } 4125 }
4123 } 4126 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
4204 (original_scope_ == original_declaration_scope || 4207 (original_scope_ == original_declaration_scope ||
4205 declaration_scope != original_declaration_scope) 4208 declaration_scope != original_declaration_scope)
4206 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind) 4209 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind)
4207 : NewScope(scope_, FUNCTION_SCOPE, kind); 4210 : NewScope(scope_, FUNCTION_SCOPE, kind);
4208 SetLanguageMode(scope, language_mode); 4211 SetLanguageMode(scope, language_mode);
4209 ZoneList<Statement*>* body = NULL; 4212 ZoneList<Statement*>* body = NULL;
4210 int arity = -1; 4213 int arity = -1;
4211 int materialized_literal_count = -1; 4214 int materialized_literal_count = -1;
4212 int expected_property_count = -1; 4215 int expected_property_count = -1;
4213 DuplicateFinder duplicate_finder(scanner()->unicode_cache()); 4216 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
4217 ExpressionClassifier formals_classifier(&duplicate_finder);
4214 FunctionLiteral::EagerCompileHint eager_compile_hint = 4218 FunctionLiteral::EagerCompileHint eager_compile_hint =
4215 parenthesized_function_ ? FunctionLiteral::kShouldEagerCompile 4219 parenthesized_function_ ? FunctionLiteral::kShouldEagerCompile
4216 : FunctionLiteral::kShouldLazyCompile; 4220 : FunctionLiteral::kShouldLazyCompile;
4217 bool should_be_used_once_hint = false; 4221 bool should_be_used_once_hint = false;
4218 bool has_duplicate_parameters;
4219 // Parse function. 4222 // Parse function.
4220 { 4223 {
4221 AstNodeFactory function_factory(ast_value_factory()); 4224 AstNodeFactory function_factory(ast_value_factory());
4222 FunctionState function_state(&function_state_, &scope_, scope, kind, 4225 FunctionState function_state(&function_state_, &scope_, scope, kind,
4223 &function_factory); 4226 &function_factory);
4224 scope_->SetScopeName(function_name); 4227 scope_->SetScopeName(function_name);
4225 ExpressionClassifier formals_classifier(this, &duplicate_finder);
4226 4228
4227 if (is_generator) { 4229 if (is_generator) {
4228 // For generators, allocating variables in contexts is currently a win 4230 // For generators, allocating variables in contexts is currently a win
4229 // because it minimizes the work needed to suspend and resume an 4231 // because it minimizes the work needed to suspend and resume an
4230 // activation. 4232 // activation.
4231 scope_->ForceContextAllocation(); 4233 scope_->ForceContextAllocation();
4232 4234
4233 // Calling a generator returns a generator object. That object is stored 4235 // Calling a generator returns a generator object. That object is stored
4234 // in a temporary variable, a definition that is used by "yield" 4236 // in a temporary variable, a definition that is used by "yield"
4235 // expressions. This also marks the FunctionState as a generator. 4237 // expressions. This also marks the FunctionState as a generator.
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
4393 } 4395 }
4394 if (is_strict(language_mode) || allow_harmony_sloppy() || 4396 if (is_strict(language_mode) || allow_harmony_sloppy() ||
4395 allow_harmony_destructuring_bind()) { 4397 allow_harmony_destructuring_bind()) {
4396 CheckConflictingVarDeclarations(scope, CHECK_OK); 4398 CheckConflictingVarDeclarations(scope, CHECK_OK);
4397 } 4399 }
4398 4400
4399 if (body) { 4401 if (body) {
4400 // If body can be inspected, rewrite queued destructuring assignments 4402 // If body can be inspected, rewrite queued destructuring assignments
4401 ParserTraits::RewriteDestructuringAssignments(); 4403 ParserTraits::RewriteDestructuringAssignments();
4402 } 4404 }
4403 has_duplicate_parameters =
4404 !formals_classifier.is_valid_formal_parameter_list_without_duplicates();
4405 } 4405 }
4406 4406
4407 bool has_duplicate_parameters =
4408 !formals_classifier.is_valid_formal_parameter_list_without_duplicates();
4407 FunctionLiteral::ParameterFlag duplicate_parameters = 4409 FunctionLiteral::ParameterFlag duplicate_parameters =
4408 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters 4410 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
4409 : FunctionLiteral::kNoDuplicateParameters; 4411 : FunctionLiteral::kNoDuplicateParameters;
4410 4412
4411 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 4413 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
4412 function_name, scope, body, materialized_literal_count, 4414 function_name, scope, body, materialized_literal_count,
4413 expected_property_count, arity, duplicate_parameters, function_type, 4415 expected_property_count, arity, duplicate_parameters, function_type,
4414 eager_compile_hint, kind, pos); 4416 eager_compile_hint, kind, pos);
4415 function_literal->set_function_token_position(function_token_pos); 4417 function_literal->set_function_token_position(function_token_pos);
4416 if (should_be_used_once_hint) 4418 if (should_be_used_once_hint)
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
4530 class InitializerRewriter : public AstExpressionVisitor { 4532 class InitializerRewriter : public AstExpressionVisitor {
4531 public: 4533 public:
4532 InitializerRewriter(uintptr_t stack_limit, Expression* root, Parser* parser, 4534 InitializerRewriter(uintptr_t stack_limit, Expression* root, Parser* parser,
4533 Scope* scope) 4535 Scope* scope)
4534 : AstExpressionVisitor(stack_limit, root), 4536 : AstExpressionVisitor(stack_limit, root),
4535 parser_(parser), 4537 parser_(parser),
4536 scope_(scope) {} 4538 scope_(scope) {}
4537 4539
4538 private: 4540 private:
4539 void VisitExpression(Expression* expr) { 4541 void VisitExpression(Expression* expr) {
4540 RewritableExpression* to_rewrite = expr->AsRewritableExpression(); 4542 RewritableAssignmentExpression* to_rewrite =
4543 expr->AsRewritableAssignmentExpression();
4541 if (to_rewrite == nullptr || to_rewrite->is_rewritten()) return; 4544 if (to_rewrite == nullptr || to_rewrite->is_rewritten()) return;
4542 4545
4543 Parser::PatternRewriter::RewriteDestructuringAssignment(parser_, to_rewrite, 4546 Parser::PatternRewriter::RewriteDestructuringAssignment(parser_, to_rewrite,
4544 scope_); 4547 scope_);
4545 } 4548 }
4546 4549
4547 private: 4550 private:
4548 Parser* parser_; 4551 Parser* parser_;
4549 Scope* scope_; 4552 Scope* scope_;
4550 }; 4553 };
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
4872 if (name != NULL) { 4875 if (name != NULL) {
4873 proxy = NewUnresolved(name, CONST); 4876 proxy = NewUnresolved(name, CONST);
4874 Declaration* declaration = 4877 Declaration* declaration =
4875 factory()->NewVariableDeclaration(proxy, CONST, block_scope, pos); 4878 factory()->NewVariableDeclaration(proxy, CONST, block_scope, pos);
4876 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); 4879 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
4877 } 4880 }
4878 4881
4879 Expression* extends = NULL; 4882 Expression* extends = NULL;
4880 if (Check(Token::EXTENDS)) { 4883 if (Check(Token::EXTENDS)) {
4881 block_scope->set_start_position(scanner()->location().end_pos); 4884 block_scope->set_start_position(scanner()->location().end_pos);
4882 ExpressionClassifier classifier(this); 4885 ExpressionClassifier classifier;
4883 extends = ParseLeftHandSideExpression(&classifier, CHECK_OK); 4886 extends = ParseLeftHandSideExpression(&classifier, CHECK_OK);
4884 RewriteNonPattern(&classifier, CHECK_OK); 4887 extends = ParserTraits::RewriteNonPattern(extends, &classifier, CHECK_OK);
4885 } else { 4888 } else {
4886 block_scope->set_start_position(scanner()->location().end_pos); 4889 block_scope->set_start_position(scanner()->location().end_pos);
4887 } 4890 }
4888 4891
4889 4892
4890 ClassLiteralChecker checker(this); 4893 ClassLiteralChecker checker(this);
4891 ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4, zone()); 4894 ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4, zone());
4892 FunctionLiteral* constructor = NULL; 4895 FunctionLiteral* constructor = NULL;
4893 bool has_seen_constructor = false; 4896 bool has_seen_constructor = false;
4894 4897
4895 Expect(Token::LBRACE, CHECK_OK); 4898 Expect(Token::LBRACE, CHECK_OK);
4896 4899
4897 const bool has_extends = extends != nullptr; 4900 const bool has_extends = extends != nullptr;
4898 while (peek() != Token::RBRACE) { 4901 while (peek() != Token::RBRACE) {
4899 if (Check(Token::SEMICOLON)) continue; 4902 if (Check(Token::SEMICOLON)) continue;
4900 FuncNameInferrer::State fni_state(fni_); 4903 FuncNameInferrer::State fni_state(fni_);
4901 const bool in_class = true; 4904 const bool in_class = true;
4902 const bool is_static = false; 4905 const bool is_static = false;
4903 bool is_computed_name = false; // Classes do not care about computed 4906 bool is_computed_name = false; // Classes do not care about computed
4904 // property names here. 4907 // property names here.
4905 ExpressionClassifier classifier(this); 4908 ExpressionClassifier classifier;
4906 const AstRawString* property_name = nullptr; 4909 const AstRawString* property_name = nullptr;
4907 ObjectLiteral::Property* property = ParsePropertyDefinition( 4910 ObjectLiteral::Property* property = ParsePropertyDefinition(
4908 &checker, in_class, has_extends, is_static, &is_computed_name, 4911 &checker, in_class, has_extends, is_static, &is_computed_name,
4909 &has_seen_constructor, &classifier, &property_name, CHECK_OK); 4912 &has_seen_constructor, &classifier, &property_name, CHECK_OK);
4910 RewriteNonPattern(&classifier, CHECK_OK); 4913 property = ParserTraits::RewriteNonPatternObjectLiteralProperty(
4914 property, &classifier, CHECK_OK);
4911 4915
4912 if (has_seen_constructor && constructor == NULL) { 4916 if (has_seen_constructor && constructor == NULL) {
4913 constructor = GetPropertyValue(property)->AsFunctionLiteral(); 4917 constructor = GetPropertyValue(property)->AsFunctionLiteral();
4914 DCHECK_NOT_NULL(constructor); 4918 DCHECK_NOT_NULL(constructor);
4915 constructor->set_raw_name( 4919 constructor->set_raw_name(
4916 name != nullptr ? name : ast_value_factory()->empty_string()); 4920 name != nullptr ? name : ast_value_factory()->empty_string());
4917 } else { 4921 } else {
4918 properties->Add(property, zone()); 4922 properties->Add(property, zone());
4919 } 4923 }
4920 4924
(...skipping 30 matching lines...) Expand all
4951 Expression* Parser::ParseV8Intrinsic(bool* ok) { 4955 Expression* Parser::ParseV8Intrinsic(bool* ok) {
4952 // CallRuntime :: 4956 // CallRuntime ::
4953 // '%' Identifier Arguments 4957 // '%' Identifier Arguments
4954 4958
4955 int pos = peek_position(); 4959 int pos = peek_position();
4956 Expect(Token::MOD, CHECK_OK); 4960 Expect(Token::MOD, CHECK_OK);
4957 // Allow "eval" or "arguments" for backward compatibility. 4961 // Allow "eval" or "arguments" for backward compatibility.
4958 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers, 4962 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers,
4959 CHECK_OK); 4963 CHECK_OK);
4960 Scanner::Location spread_pos; 4964 Scanner::Location spread_pos;
4961 ExpressionClassifier classifier(this); 4965 ExpressionClassifier classifier;
4962 ZoneList<Expression*>* args = 4966 ZoneList<Expression*>* args =
4963 ParseArguments(&spread_pos, &classifier, CHECK_OK); 4967 ParseArguments(&spread_pos, &classifier, CHECK_OK);
4964 4968
4965 DCHECK(!spread_pos.IsValid()); 4969 DCHECK(!spread_pos.IsValid());
4966 4970
4967 if (extension_ != NULL) { 4971 if (extension_ != NULL) {
4968 // The extension structures are only accessible while parsing the 4972 // The extension structures are only accessible while parsing the
4969 // very first time not when reparsing because of lazy compilation. 4973 // very first time not when reparsing because of lazy compilation.
4970 scope_->DeclarationScope()->ForceEagerCompilation(); 4974 scope_->DeclarationScope()->ForceEagerCompilation();
4971 } 4975 }
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
5522 SetLanguageMode(scope_, 5526 SetLanguageMode(scope_,
5523 static_cast<LanguageMode>(scope_->language_mode() | mode)); 5527 static_cast<LanguageMode>(scope_->language_mode() | mode));
5524 } 5528 }
5525 5529
5526 5530
5527 void ParserTraits::RewriteDestructuringAssignments() { 5531 void ParserTraits::RewriteDestructuringAssignments() {
5528 parser_->RewriteDestructuringAssignments(); 5532 parser_->RewriteDestructuringAssignments();
5529 } 5533 }
5530 5534
5531 5535
5532 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier, 5536 Expression* ParserTraits::RewriteNonPattern(
5533 bool* ok) { 5537 Expression* expr, const ExpressionClassifier* classifier, bool* ok) {
5534 parser_->RewriteNonPattern(classifier, ok); 5538 return parser_->RewriteNonPattern(expr, classifier, ok);
5535 } 5539 }
5536 5540
5537 5541
5538 Zone* ParserTraits::zone() const { 5542 ObjectLiteralProperty* ParserTraits::RewriteNonPatternObjectLiteralProperty(
5539 return parser_->function_state_->scope()->zone(); 5543 ObjectLiteralProperty* property, const ExpressionClassifier* classifier,
5544 bool* ok) {
5545 return parser_->RewriteNonPatternObjectLiteralProperty(property, classifier,
5546 ok);
5540 } 5547 }
5541 5548
5542 5549
5543 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const {
5544 return parser_->function_state_->non_patterns_to_rewrite();
5545 }
5546
5547
5548 class NonPatternRewriter : public AstExpressionRewriter { 5550 class NonPatternRewriter : public AstExpressionRewriter {
5549 public: 5551 public:
5550 NonPatternRewriter(uintptr_t stack_limit, Parser* parser) 5552 NonPatternRewriter(uintptr_t stack_limit, Parser* parser)
5551 : AstExpressionRewriter(stack_limit), parser_(parser) {} 5553 : AstExpressionRewriter(stack_limit), parser_(parser) {}
5552 ~NonPatternRewriter() override {} 5554 ~NonPatternRewriter() override {}
5553 5555
5554 private: 5556 private:
5555 bool RewriteExpression(Expression* expr) override { 5557 bool RewriteExpression(Expression* expr) override {
5556 if (expr->IsRewritableExpression()) return true;
5557 // Rewrite only what could have been a pattern but is not. 5558 // Rewrite only what could have been a pattern but is not.
5558 if (expr->IsArrayLiteral()) { 5559 if (expr->IsArrayLiteral()) {
5559 // Spread rewriting in array literals. 5560 // Spread rewriting in array literals.
5560 ArrayLiteral* lit = expr->AsArrayLiteral(); 5561 ArrayLiteral* lit = expr->AsArrayLiteral();
5561 VisitExpressions(lit->values()); 5562 VisitExpressions(lit->values());
5562 replacement_ = parser_->RewriteSpreads(lit); 5563 replacement_ = parser_->RewriteSpreads(lit);
5563 return false; 5564 return false;
5564 } 5565 }
5565 if (expr->IsObjectLiteral()) { 5566 if (expr->IsObjectLiteral()) {
5566 return true; 5567 return true;
5567 } 5568 }
5568 if (expr->IsBinaryOperation() && 5569 if (expr->IsBinaryOperation() &&
5569 expr->AsBinaryOperation()->op() == Token::COMMA) { 5570 expr->AsBinaryOperation()->op() == Token::COMMA) {
5570 return true; 5571 return true;
5571 } 5572 }
5572 // Everything else does not need rewriting. 5573 // Everything else does not need rewriting.
5573 return false; 5574 return false;
5574 } 5575 }
5575 5576
5576 void VisitObjectLiteralProperty(ObjectLiteralProperty* property) override { 5577 void VisitObjectLiteralProperty(ObjectLiteralProperty* property) override {
5577 if (property == nullptr) return; 5578 if (property == nullptr) return;
5578 // Do not rewrite (computed) key expressions 5579 // Do not rewrite (computed) key expressions
5579 AST_REWRITE_PROPERTY(Expression, property, value); 5580 AST_REWRITE_PROPERTY(Expression, property, value);
5580 } 5581 }
5581 5582
5582 Parser* parser_; 5583 Parser* parser_;
5583 }; 5584 };
5584 5585
5585 5586
5586 void Parser::RewriteNonPattern(ExpressionClassifier* classifier, bool* ok) { 5587 Expression* Parser::RewriteNonPattern(Expression* expr,
5588 const ExpressionClassifier* classifier,
5589 bool* ok) {
5587 ValidateExpression(classifier, ok); 5590 ValidateExpression(classifier, ok);
5588 if (!*ok) return; 5591 if (!*ok) return expr;
5589 auto non_patterns_to_rewrite = function_state_->non_patterns_to_rewrite(); 5592 NonPatternRewriter rewriter(stack_limit_, this);
5590 int begin = classifier->GetNonPatternBegin(); 5593 Expression* result = reinterpret_cast<Expression*>(rewriter.Rewrite(expr));
5591 int end = non_patterns_to_rewrite->length(); 5594 DCHECK_NOT_NULL(result);
5592 if (begin < end) { 5595 return result;
5593 NonPatternRewriter rewriter(stack_limit_, this); 5596 }
5594 for (int i = begin; i < end; i++) { 5597
5595 DCHECK(non_patterns_to_rewrite->at(i)->IsRewritableExpression()); 5598
5596 rewriter.Rewrite(non_patterns_to_rewrite->at(i)); 5599 ObjectLiteralProperty* Parser::RewriteNonPatternObjectLiteralProperty(
5597 } 5600 ObjectLiteralProperty* property, const ExpressionClassifier* classifier,
5598 non_patterns_to_rewrite->Rewind(begin); 5601 bool* ok) {
5602 if (property != nullptr) {
5603 // Do not rewrite (computed) key expressions
5604 Expression* value = RewriteNonPattern(property->value(), classifier, ok);
5605 property->set_value(value);
5599 } 5606 }
5607 return property;
5600 } 5608 }
5601 5609
5602 5610
5603 void Parser::RewriteDestructuringAssignments() { 5611 void Parser::RewriteDestructuringAssignments() {
5612 FunctionState* func = function_state_;
5604 if (!allow_harmony_destructuring_assignment()) return; 5613 if (!allow_harmony_destructuring_assignment()) return;
5605 const auto& assignments = 5614 const List<DestructuringAssignment>& assignments =
5606 function_state_->destructuring_assignments_to_rewrite(); 5615 func->destructuring_assignments_to_rewrite();
5607 for (int i = assignments.length() - 1; i >= 0; --i) { 5616 for (int i = assignments.length() - 1; i >= 0; --i) {
5608 // Rewrite list in reverse, so that nested assignment patterns are rewritten 5617 // Rewrite list in reverse, so that nested assignment patterns are rewritten
5609 // correctly. 5618 // correctly.
5610 const DestructuringAssignment& pair = assignments.at(i); 5619 DestructuringAssignment pair = assignments.at(i);
5611 RewritableExpression* to_rewrite = 5620 RewritableAssignmentExpression* to_rewrite =
5612 pair.assignment->AsRewritableExpression(); 5621 pair.assignment->AsRewritableAssignmentExpression();
5622 Scope* scope = pair.scope;
5613 DCHECK_NOT_NULL(to_rewrite); 5623 DCHECK_NOT_NULL(to_rewrite);
5614 if (!to_rewrite->is_rewritten()) { 5624 if (!to_rewrite->is_rewritten()) {
5615 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite, 5625 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite, scope);
5616 pair.scope);
5617 } 5626 }
5618 } 5627 }
5619 } 5628 }
5620 5629
5621 5630
5622 Expression* Parser::RewriteSpreads(ArrayLiteral* lit) { 5631 Expression* Parser::RewriteSpreads(ArrayLiteral* lit) {
5623 // Array literals containing spreads are rewritten using do expressions, e.g. 5632 // Array literals containing spreads are rewritten using do expressions, e.g.
5624 // [1, 2, 3, ...x, 4, ...y, 5] 5633 // [1, 2, 3, ...x, 4, ...y, 5]
5625 // is roughly rewritten as: 5634 // is roughly rewritten as:
5626 // do { 5635 // do {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
5732 } 5741 }
5733 } 5742 }
5734 // Now, rewind the original array literal to truncate everything from the 5743 // Now, rewind the original array literal to truncate everything from the
5735 // first spread (included) until the end. This fixes $R's initialization. 5744 // first spread (included) until the end. This fixes $R's initialization.
5736 lit->RewindSpreads(); 5745 lit->RewindSpreads();
5737 return factory()->NewDoExpression(do_block, result, lit->position()); 5746 return factory()->NewDoExpression(do_block, result, lit->position());
5738 } 5747 }
5739 5748
5740 5749
5741 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) { 5750 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) {
5742 DCHECK(expr->IsRewritableExpression()); 5751 DCHECK(expr->IsRewritableAssignmentExpression());
5743 parser_->function_state_->AddDestructuringAssignment( 5752 parser_->function_state_->AddDestructuringAssignment(
5744 Parser::DestructuringAssignment(expr, parser_->scope_)); 5753 Parser::DestructuringAssignment(expr, parser_->scope_));
5745 } 5754 }
5746 5755
5747 5756
5748 void ParserTraits::QueueNonPatternForRewriting(Expression* expr) {
5749 DCHECK(expr->IsRewritableExpression());
5750 parser_->function_state_->AddNonPatternForRewriting(expr);
5751 }
5752
5753
5754 void ParserTraits::SetFunctionNameFromPropertyName( 5757 void ParserTraits::SetFunctionNameFromPropertyName(
5755 ObjectLiteralProperty* property, const AstRawString* name) { 5758 ObjectLiteralProperty* property, const AstRawString* name) {
5756 Expression* value = property->value(); 5759 Expression* value = property->value();
5757 5760
5758 // Computed name setting must happen at runtime. 5761 // Computed name setting must happen at runtime.
5759 if (property->is_computed_name()) return; 5762 if (property->is_computed_name()) return;
5760 5763
5761 // Getter and setter names are handled here because their names 5764 // Getter and setter names are handled here because their names
5762 // change in ES2015, even though they are not anonymous. 5765 // change in ES2015, even though they are not anonymous.
5763 auto function = value->AsFunctionLiteral(); 5766 auto function = value->AsFunctionLiteral();
(...skipping 967 matching lines...) Expand 10 before | Expand all | Expand 10 after
6731 new_body->statements()->Add(loop->body(), zone); 6734 new_body->statements()->Add(loop->body(), zone);
6732 new_body->statements()->Add(set_completion_normal, zone); 6735 new_body->statements()->Add(set_completion_normal, zone);
6733 6736
6734 loop->set_body(new_body); 6737 loop->set_body(new_body);
6735 return final_loop; 6738 return final_loop;
6736 } 6739 }
6737 6740
6738 6741
6739 } // namespace internal 6742 } // namespace internal
6740 } // namespace v8 6743 } // namespace v8
OLDNEW
« no previous file with comments | « 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