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

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

Issue 1702063002: 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
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 1038 matching lines...) Expand 10 before | Expand all | Expand 10 after
1049 // These two bits only need to be explicitly set because we're 1049 // These two bits only need to be explicitly set because we're
1050 // not passing the ScopeInfo to the Scope constructor. 1050 // not passing the ScopeInfo to the Scope constructor.
1051 // TODO(adamk): Remove these calls once the above NewScope call 1051 // TODO(adamk): Remove these calls once the above NewScope call
1052 // passes the ScopeInfo. 1052 // passes the ScopeInfo.
1053 if (shared_info->scope_info()->CallsEval()) { 1053 if (shared_info->scope_info()->CallsEval()) {
1054 scope->RecordEvalCall(); 1054 scope->RecordEvalCall();
1055 } 1055 }
1056 SetLanguageMode(scope, shared_info->language_mode()); 1056 SetLanguageMode(scope, shared_info->language_mode());
1057 1057
1058 scope->set_start_position(shared_info->start_position()); 1058 scope->set_start_position(shared_info->start_position());
1059 ExpressionClassifier formals_classifier; 1059 ExpressionClassifier formals_classifier(this);
1060 ParserFormalParameters formals(scope); 1060 ParserFormalParameters formals(scope);
1061 Checkpoint checkpoint(this); 1061 Checkpoint checkpoint(this);
1062 { 1062 {
1063 // Parsing patterns as variable reference expression creates 1063 // Parsing patterns as variable reference expression creates
1064 // NewUnresolved references in current scope. Entrer arrow function 1064 // NewUnresolved references in current scope. Entrer arrow function
1065 // scope for formal parameter parsing. 1065 // scope for formal parameter parsing.
1066 BlockState block_state(&scope_, scope); 1066 BlockState block_state(&scope_, scope);
1067 if (Check(Token::LPAREN)) { 1067 if (Check(Token::LPAREN)) {
1068 // '(' StrictFormalParameters ')' 1068 // '(' StrictFormalParameters ')'
1069 ParseFormalParameterList(&formals, &formals_classifier, &ok); 1069 ParseFormalParameterList(&formals, &formals_classifier, &ok);
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after
1602 ParseClassLiteral(default_string, Scanner::Location::invalid(), 1602 ParseClassLiteral(default_string, Scanner::Location::invalid(),
1603 false, position(), CHECK_OK); 1603 false, position(), CHECK_OK);
1604 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); 1604 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1605 } else { 1605 } else {
1606 result = ParseClassDeclaration(&names, CHECK_OK); 1606 result = ParseClassDeclaration(&names, CHECK_OK);
1607 } 1607 }
1608 break; 1608 break;
1609 1609
1610 default: { 1610 default: {
1611 int pos = peek_position(); 1611 int pos = peek_position();
1612 ExpressionClassifier classifier; 1612 ExpressionClassifier classifier(this);
1613 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK); 1613 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK);
1614 expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK); 1614 RewriteNonPattern(&classifier, CHECK_OK);
1615 1615
1616 ExpectSemicolon(CHECK_OK); 1616 ExpectSemicolon(CHECK_OK);
1617 result = factory()->NewExpressionStatement(expr, pos); 1617 result = factory()->NewExpressionStatement(expr, pos);
1618 break; 1618 break;
1619 } 1619 }
1620 } 1620 }
1621 1621
1622 DCHECK_LE(names.length(), 1); 1622 DCHECK_LE(names.length(), 1);
1623 if (names.length() == 1) { 1623 if (names.length() == 1) {
1624 scope_->module()->AddLocalExport(default_string, names.first(), zone(), ok); 1624 scope_->module()->AddLocalExport(default_string, names.first(), zone(), ok);
(...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after
2396 int bindings_start = peek_position(); 2396 int bindings_start = peek_position();
2397 do { 2397 do {
2398 FuncNameInferrer::State fni_state(fni_); 2398 FuncNameInferrer::State fni_state(fni_);
2399 2399
2400 // Parse name. 2400 // Parse name.
2401 if (!first_declaration) Consume(Token::COMMA); 2401 if (!first_declaration) Consume(Token::COMMA);
2402 2402
2403 Expression* pattern; 2403 Expression* pattern;
2404 int decl_pos = peek_position(); 2404 int decl_pos = peek_position();
2405 { 2405 {
2406 ExpressionClassifier pattern_classifier; 2406 ExpressionClassifier pattern_classifier(this);
2407 Token::Value next = peek(); 2407 Token::Value next = peek();
2408 pattern = ParsePrimaryExpression(&pattern_classifier, ok); 2408 pattern = ParsePrimaryExpression(&pattern_classifier, ok);
2409 if (!*ok) return; 2409 if (!*ok) return;
2410 ValidateBindingPattern(&pattern_classifier, ok); 2410 ValidateBindingPattern(&pattern_classifier, ok);
2411 if (!*ok) return; 2411 if (!*ok) return;
2412 if (IsLexicalVariableMode(parsing_result->descriptor.mode)) { 2412 if (IsLexicalVariableMode(parsing_result->descriptor.mode)) {
2413 ValidateLetPattern(&pattern_classifier, ok); 2413 ValidateLetPattern(&pattern_classifier, ok);
2414 if (!*ok) return; 2414 if (!*ok) return;
2415 } 2415 }
2416 if (!allow_harmony_destructuring_bind() && !pattern->IsVariableProxy()) { 2416 if (!allow_harmony_destructuring_bind() && !pattern->IsVariableProxy()) {
2417 ReportUnexpectedToken(next); 2417 ReportUnexpectedToken(next);
2418 *ok = false; 2418 *ok = false;
2419 return; 2419 return;
2420 } 2420 }
2421 } 2421 }
2422 2422
2423 Scanner::Location variable_loc = scanner()->location(); 2423 Scanner::Location variable_loc = scanner()->location();
2424 const AstRawString* single_name = 2424 const AstRawString* single_name =
2425 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name() 2425 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name()
2426 : nullptr; 2426 : nullptr;
2427 if (single_name != nullptr) { 2427 if (single_name != nullptr) {
2428 if (fni_ != NULL) fni_->PushVariableName(single_name); 2428 if (fni_ != NULL) fni_->PushVariableName(single_name);
2429 } 2429 }
2430 2430
2431 Expression* value = NULL; 2431 Expression* value = NULL;
2432 int initializer_position = RelocInfo::kNoPosition; 2432 int initializer_position = RelocInfo::kNoPosition;
2433 if (Check(Token::ASSIGN)) { 2433 if (Check(Token::ASSIGN)) {
2434 ExpressionClassifier classifier; 2434 ExpressionClassifier classifier(this);
2435 value = ParseAssignmentExpression(var_context != kForStatement, 2435 value = ParseAssignmentExpression(var_context != kForStatement,
2436 &classifier, ok); 2436 &classifier, ok);
2437 if (!*ok) return; 2437 if (!*ok) return;
2438 value = ParserTraits::RewriteNonPattern(value, &classifier, ok); 2438 RewriteNonPattern(&classifier, ok);
2439 if (!*ok) return; 2439 if (!*ok) return;
2440 variable_loc.end_pos = scanner()->location().end_pos; 2440 variable_loc.end_pos = scanner()->location().end_pos;
2441 2441
2442 if (!parsing_result->first_initializer_loc.IsValid()) { 2442 if (!parsing_result->first_initializer_loc.IsValid()) {
2443 parsing_result->first_initializer_loc = variable_loc; 2443 parsing_result->first_initializer_loc = variable_loc;
2444 } 2444 }
2445 2445
2446 // Don't infer if it is "a = function(){...}();"-like expression. 2446 // Don't infer if it is "a = function(){...}();"-like expression.
2447 if (single_name) { 2447 if (single_name) {
2448 if (fni_ != NULL && value->AsCall() == NULL && 2448 if (fni_ != NULL && value->AsCall() == NULL &&
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
2530 return nullptr; 2530 return nullptr;
2531 2531
2532 case Token::THIS: 2532 case Token::THIS:
2533 if (!FLAG_strong_this) break; 2533 if (!FLAG_strong_this) break;
2534 // Fall through. 2534 // Fall through.
2535 case Token::SUPER: 2535 case Token::SUPER:
2536 if (is_strong(language_mode()) && 2536 if (is_strong(language_mode()) &&
2537 IsClassConstructor(function_state_->kind())) { 2537 IsClassConstructor(function_state_->kind())) {
2538 bool is_this = peek() == Token::THIS; 2538 bool is_this = peek() == Token::THIS;
2539 Expression* expr; 2539 Expression* expr;
2540 ExpressionClassifier classifier; 2540 ExpressionClassifier classifier(this);
2541 if (is_this) { 2541 if (is_this) {
2542 expr = ParseStrongInitializationExpression(&classifier, CHECK_OK); 2542 expr = ParseStrongInitializationExpression(&classifier, CHECK_OK);
2543 } else { 2543 } else {
2544 expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK); 2544 expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK);
2545 } 2545 }
2546 expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK); 2546 RewriteNonPattern(&classifier, CHECK_OK);
2547 switch (peek()) { 2547 switch (peek()) {
2548 case Token::SEMICOLON: 2548 case Token::SEMICOLON:
2549 Consume(Token::SEMICOLON); 2549 Consume(Token::SEMICOLON);
2550 break; 2550 break;
2551 case Token::RBRACE: 2551 case Token::RBRACE:
2552 case Token::EOS: 2552 case Token::EOS:
2553 break; 2553 break;
2554 default: 2554 default:
2555 if (!scanner()->HasAnyLineTerminatorBeforeNext()) { 2555 if (!scanner()->HasAnyLineTerminatorBeforeNext()) {
2556 ReportMessageAt(function_state_->this_location(), 2556 ReportMessageAt(function_state_->this_location(),
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
3069 Variable* catch_variable = NULL; 3069 Variable* catch_variable = NULL;
3070 Block* catch_block = NULL; 3070 Block* catch_block = NULL;
3071 List<Expression*> expressions_in_tail_position_in_catch_block; 3071 List<Expression*> expressions_in_tail_position_in_catch_block;
3072 if (tok == Token::CATCH) { 3072 if (tok == Token::CATCH) {
3073 Consume(Token::CATCH); 3073 Consume(Token::CATCH);
3074 3074
3075 Expect(Token::LPAREN, CHECK_OK); 3075 Expect(Token::LPAREN, CHECK_OK);
3076 catch_scope = NewScope(scope_, CATCH_SCOPE); 3076 catch_scope = NewScope(scope_, CATCH_SCOPE);
3077 catch_scope->set_start_position(scanner()->location().beg_pos); 3077 catch_scope->set_start_position(scanner()->location().beg_pos);
3078 3078
3079 ExpressionClassifier pattern_classifier; 3079 ExpressionClassifier pattern_classifier(this);
3080 Expression* pattern = ParsePrimaryExpression(&pattern_classifier, CHECK_OK); 3080 Expression* pattern = ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
3081 ValidateBindingPattern(&pattern_classifier, CHECK_OK); 3081 ValidateBindingPattern(&pattern_classifier, CHECK_OK);
3082 3082
3083 const AstRawString* name = ast_value_factory()->dot_catch_string(); 3083 const AstRawString* name = ast_value_factory()->dot_catch_string();
3084 bool is_simple = pattern->IsVariableProxy(); 3084 bool is_simple = pattern->IsVariableProxy();
3085 if (is_simple) { 3085 if (is_simple) {
3086 auto proxy = pattern->AsVariableProxy(); 3086 auto proxy = pattern->AsVariableProxy();
3087 scope_->RemoveUnresolved(proxy); 3087 scope_->RemoveUnresolved(proxy);
3088 name = proxy->raw_name(); 3088 name = proxy->raw_name();
3089 } 3089 }
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
3700 // } 3700 // }
3701 3701
3702 Variable* temp = 3702 Variable* temp =
3703 scope_->NewTemporary(ast_value_factory()->dot_for_string()); 3703 scope_->NewTemporary(ast_value_factory()->dot_for_string());
3704 ForEachStatement* loop = 3704 ForEachStatement* loop =
3705 factory()->NewForEachStatement(mode, labels, stmt_pos); 3705 factory()->NewForEachStatement(mode, labels, stmt_pos);
3706 Target target(&this->target_stack_, loop); 3706 Target target(&this->target_stack_, loop);
3707 3707
3708 Expression* enumerable; 3708 Expression* enumerable;
3709 if (mode == ForEachStatement::ITERATE) { 3709 if (mode == ForEachStatement::ITERATE) {
3710 ExpressionClassifier classifier; 3710 ExpressionClassifier classifier(this);
3711 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK); 3711 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK);
3712 enumerable = ParserTraits::RewriteNonPattern(enumerable, &classifier, 3712 RewriteNonPattern(&classifier, CHECK_OK);
3713 CHECK_OK);
3714 } else { 3713 } else {
3715 enumerable = ParseExpression(true, CHECK_OK); 3714 enumerable = ParseExpression(true, CHECK_OK);
3716 } 3715 }
3717 3716
3718 Expect(Token::RPAREN, CHECK_OK); 3717 Expect(Token::RPAREN, CHECK_OK);
3719 3718
3720 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE); 3719 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE);
3721 body_scope->set_start_position(scanner()->location().beg_pos); 3720 body_scope->set_start_position(scanner()->location().beg_pos);
3722 3721
3723 Block* body_block = 3722 Block* body_block =
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
3788 } 3787 }
3789 } else { 3788 } else {
3790 init = parsing_result.BuildInitializationBlock( 3789 init = parsing_result.BuildInitializationBlock(
3791 IsLexicalVariableMode(parsing_result.descriptor.mode) 3790 IsLexicalVariableMode(parsing_result.descriptor.mode)
3792 ? &lexical_bindings 3791 ? &lexical_bindings
3793 : nullptr, 3792 : nullptr,
3794 CHECK_OK); 3793 CHECK_OK);
3795 } 3794 }
3796 } else { 3795 } else {
3797 int lhs_beg_pos = peek_position(); 3796 int lhs_beg_pos = peek_position();
3798 ExpressionClassifier classifier; 3797 ExpressionClassifier classifier(this);
3799 Expression* expression = ParseExpression(false, &classifier, CHECK_OK); 3798 Expression* expression = ParseExpression(false, &classifier, CHECK_OK);
3800 int lhs_end_pos = scanner()->location().end_pos; 3799 int lhs_end_pos = scanner()->location().end_pos;
3801 ForEachStatement::VisitMode mode; 3800 ForEachStatement::VisitMode mode;
3802 is_let_identifier_expression = 3801 is_let_identifier_expression =
3803 expression->IsVariableProxy() && 3802 expression->IsVariableProxy() &&
3804 expression->AsVariableProxy()->raw_name() == 3803 expression->AsVariableProxy()->raw_name() ==
3805 ast_value_factory()->let_string(); 3804 ast_value_factory()->let_string();
3806 3805
3807 bool is_for_each = CheckInOrOf(&mode, ok); 3806 bool is_for_each = CheckInOrOf(&mode, ok);
3808 if (!*ok) return nullptr; 3807 if (!*ok) return nullptr;
3809 bool is_destructuring = 3808 bool is_destructuring =
3810 is_for_each && allow_harmony_destructuring_assignment() && 3809 is_for_each && allow_harmony_destructuring_assignment() &&
3811 (expression->IsArrayLiteral() || expression->IsObjectLiteral()); 3810 (expression->IsArrayLiteral() || expression->IsObjectLiteral());
3812 3811
3813 if (is_destructuring) { 3812 if (is_destructuring) {
3814 ValidateAssignmentPattern(&classifier, CHECK_OK); 3813 ValidateAssignmentPattern(&classifier, CHECK_OK);
3815 } else { 3814 } else {
3816 expression = 3815 RewriteNonPattern(&classifier, CHECK_OK);
3817 ParserTraits::RewriteNonPattern(expression, &classifier, CHECK_OK);
3818 } 3816 }
3819 3817
3820 if (is_for_each) { 3818 if (is_for_each) {
3821 if (!is_destructuring) { 3819 if (!is_destructuring) {
3822 expression = this->CheckAndRewriteReferenceExpression( 3820 expression = this->CheckAndRewriteReferenceExpression(
3823 expression, lhs_beg_pos, lhs_end_pos, 3821 expression, lhs_beg_pos, lhs_end_pos,
3824 MessageTemplate::kInvalidLhsInFor, kSyntaxError, CHECK_OK); 3822 MessageTemplate::kInvalidLhsInFor, kSyntaxError, CHECK_OK);
3825 } 3823 }
3826 3824
3827 ForEachStatement* loop = 3825 ForEachStatement* loop =
3828 factory()->NewForEachStatement(mode, labels, stmt_pos); 3826 factory()->NewForEachStatement(mode, labels, stmt_pos);
3829 Target target(&this->target_stack_, loop); 3827 Target target(&this->target_stack_, loop);
3830 3828
3831 Expression* enumerable; 3829 Expression* enumerable;
3832 if (mode == ForEachStatement::ITERATE) { 3830 if (mode == ForEachStatement::ITERATE) {
3833 ExpressionClassifier classifier; 3831 ExpressionClassifier classifier(this);
3834 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK); 3832 enumerable = ParseAssignmentExpression(true, &classifier, CHECK_OK);
3835 enumerable = ParserTraits::RewriteNonPattern(enumerable, &classifier, 3833 RewriteNonPattern(&classifier, CHECK_OK);
3836 CHECK_OK);
3837 } else { 3834 } else {
3838 enumerable = ParseExpression(true, CHECK_OK); 3835 enumerable = ParseExpression(true, CHECK_OK);
3839 } 3836 }
3840 3837
3841 Expect(Token::RPAREN, CHECK_OK); 3838 Expect(Token::RPAREN, CHECK_OK);
3842 3839
3843 // Make a block around the statement in case a lexical binding 3840 // Make a block around the statement in case a lexical binding
3844 // is introduced, e.g. by a FunctionDeclaration. 3841 // is introduced, e.g. by a FunctionDeclaration.
3845 // This block must not use for_scope as its scope because if a 3842 // This block must not use for_scope as its scope because if a
3846 // lexical binding is introduced which overlaps with the for-in/of, 3843 // lexical binding is introduced which overlaps with the for-in/of,
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
4111 4108
4112 void ParserTraits::ParseArrowFunctionFormalParameterList( 4109 void ParserTraits::ParseArrowFunctionFormalParameterList(
4113 ParserFormalParameters* parameters, Expression* expr, 4110 ParserFormalParameters* parameters, Expression* expr,
4114 const Scanner::Location& params_loc, 4111 const Scanner::Location& params_loc,
4115 Scanner::Location* duplicate_loc, bool* ok) { 4112 Scanner::Location* duplicate_loc, bool* ok) {
4116 if (expr->IsEmptyParentheses()) return; 4113 if (expr->IsEmptyParentheses()) return;
4117 4114
4118 ParseArrowFunctionFormalParameters(parameters, expr, params_loc, ok); 4115 ParseArrowFunctionFormalParameters(parameters, expr, params_loc, ok);
4119 if (!*ok) return; 4116 if (!*ok) return;
4120 4117
4121 ExpressionClassifier classifier; 4118 Type::ExpressionClassifier classifier(parser_);
4122 if (!parameters->is_simple) { 4119 if (!parameters->is_simple) {
4123 classifier.RecordNonSimpleParameter(); 4120 classifier.RecordNonSimpleParameter();
4124 } 4121 }
4125 for (int i = 0; i < parameters->Arity(); ++i) { 4122 for (int i = 0; i < parameters->Arity(); ++i) {
4126 auto parameter = parameters->at(i); 4123 auto parameter = parameters->at(i);
4127 DeclareFormalParameter(parameters->scope, parameter, &classifier); 4124 DeclareFormalParameter(parameters->scope, parameter, &classifier);
4128 if (!duplicate_loc->IsValid()) { 4125 if (!duplicate_loc->IsValid()) {
4129 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; 4126 *duplicate_loc = classifier.duplicate_formal_parameter_error().location;
4130 } 4127 }
4131 } 4128 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
4213 (original_scope_ == original_declaration_scope || 4210 (original_scope_ == original_declaration_scope ||
4214 declaration_scope != original_declaration_scope) 4211 declaration_scope != original_declaration_scope)
4215 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind) 4212 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind)
4216 : NewScope(scope_, FUNCTION_SCOPE, kind); 4213 : NewScope(scope_, FUNCTION_SCOPE, kind);
4217 SetLanguageMode(scope, language_mode); 4214 SetLanguageMode(scope, language_mode);
4218 ZoneList<Statement*>* body = NULL; 4215 ZoneList<Statement*>* body = NULL;
4219 int arity = -1; 4216 int arity = -1;
4220 int materialized_literal_count = -1; 4217 int materialized_literal_count = -1;
4221 int expected_property_count = -1; 4218 int expected_property_count = -1;
4222 DuplicateFinder duplicate_finder(scanner()->unicode_cache()); 4219 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
4223 ExpressionClassifier formals_classifier(&duplicate_finder);
4224 FunctionLiteral::EagerCompileHint eager_compile_hint = 4220 FunctionLiteral::EagerCompileHint eager_compile_hint =
4225 parenthesized_function_ ? FunctionLiteral::kShouldEagerCompile 4221 parenthesized_function_ ? FunctionLiteral::kShouldEagerCompile
4226 : FunctionLiteral::kShouldLazyCompile; 4222 : FunctionLiteral::kShouldLazyCompile;
4227 bool should_be_used_once_hint = false; 4223 bool should_be_used_once_hint = false;
4224 bool has_duplicate_parameters;
4228 // Parse function. 4225 // Parse function.
4229 { 4226 {
4230 AstNodeFactory function_factory(ast_value_factory()); 4227 AstNodeFactory function_factory(ast_value_factory());
4231 FunctionState function_state(&function_state_, &scope_, scope, kind, 4228 FunctionState function_state(&function_state_, &scope_, scope, kind,
4232 &function_factory); 4229 &function_factory);
4233 scope_->SetScopeName(function_name); 4230 scope_->SetScopeName(function_name);
4231 ExpressionClassifier formals_classifier(this, &duplicate_finder);
4234 4232
4235 if (is_generator) { 4233 if (is_generator) {
4236 // For generators, allocating variables in contexts is currently a win 4234 // For generators, allocating variables in contexts is currently a win
4237 // because it minimizes the work needed to suspend and resume an 4235 // because it minimizes the work needed to suspend and resume an
4238 // activation. 4236 // activation.
4239 scope_->ForceContextAllocation(); 4237 scope_->ForceContextAllocation();
4240 4238
4241 // Calling a generator returns a generator object. That object is stored 4239 // Calling a generator returns a generator object. That object is stored
4242 // in a temporary variable, a definition that is used by "yield" 4240 // in a temporary variable, a definition that is used by "yield"
4243 // expressions. This also marks the FunctionState as a generator. 4241 // expressions. This also marks the FunctionState as a generator.
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
4402 } 4400 }
4403 if (is_strict(language_mode) || allow_harmony_sloppy() || 4401 if (is_strict(language_mode) || allow_harmony_sloppy() ||
4404 allow_harmony_destructuring_bind()) { 4402 allow_harmony_destructuring_bind()) {
4405 CheckConflictingVarDeclarations(scope, CHECK_OK); 4403 CheckConflictingVarDeclarations(scope, CHECK_OK);
4406 } 4404 }
4407 4405
4408 if (body) { 4406 if (body) {
4409 // If body can be inspected, rewrite queued destructuring assignments 4407 // If body can be inspected, rewrite queued destructuring assignments
4410 ParserTraits::RewriteDestructuringAssignments(); 4408 ParserTraits::RewriteDestructuringAssignments();
4411 } 4409 }
4410 has_duplicate_parameters =
4411 !formals_classifier.is_valid_formal_parameter_list_without_duplicates();
4412 } 4412 }
4413 4413
4414 bool has_duplicate_parameters =
4415 !formals_classifier.is_valid_formal_parameter_list_without_duplicates();
4416 FunctionLiteral::ParameterFlag duplicate_parameters = 4414 FunctionLiteral::ParameterFlag duplicate_parameters =
4417 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters 4415 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
4418 : FunctionLiteral::kNoDuplicateParameters; 4416 : FunctionLiteral::kNoDuplicateParameters;
4419 4417
4420 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 4418 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
4421 function_name, scope, body, materialized_literal_count, 4419 function_name, scope, body, materialized_literal_count,
4422 expected_property_count, arity, duplicate_parameters, function_type, 4420 expected_property_count, arity, duplicate_parameters, function_type,
4423 eager_compile_hint, kind, pos); 4421 eager_compile_hint, kind, pos);
4424 function_literal->set_function_token_position(function_token_pos); 4422 function_literal->set_function_token_position(function_token_pos);
4425 if (should_be_used_once_hint) 4423 if (should_be_used_once_hint)
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
4539 class InitializerRewriter : public AstExpressionVisitor { 4537 class InitializerRewriter : public AstExpressionVisitor {
4540 public: 4538 public:
4541 InitializerRewriter(uintptr_t stack_limit, Expression* root, Parser* parser, 4539 InitializerRewriter(uintptr_t stack_limit, Expression* root, Parser* parser,
4542 Scope* scope) 4540 Scope* scope)
4543 : AstExpressionVisitor(stack_limit, root), 4541 : AstExpressionVisitor(stack_limit, root),
4544 parser_(parser), 4542 parser_(parser),
4545 scope_(scope) {} 4543 scope_(scope) {}
4546 4544
4547 private: 4545 private:
4548 void VisitExpression(Expression* expr) { 4546 void VisitExpression(Expression* expr) {
4549 RewritableAssignmentExpression* to_rewrite = 4547 RewritableExpression* to_rewrite = expr->AsRewritableExpression();
4550 expr->AsRewritableAssignmentExpression();
4551 if (to_rewrite == nullptr || to_rewrite->is_rewritten()) return; 4548 if (to_rewrite == nullptr || to_rewrite->is_rewritten()) return;
4552 4549
4553 Parser::PatternRewriter::RewriteDestructuringAssignment(parser_, to_rewrite, 4550 Parser::PatternRewriter::RewriteDestructuringAssignment(parser_, to_rewrite,
4554 scope_); 4551 scope_);
4555 } 4552 }
4556 4553
4557 private: 4554 private:
4558 Parser* parser_; 4555 Parser* parser_;
4559 Scope* scope_; 4556 Scope* scope_;
4560 }; 4557 };
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
4881 const bool is_class_declaration = true; 4878 const bool is_class_declaration = true;
4882 Declaration* declaration = factory()->NewVariableDeclaration( 4879 Declaration* declaration = factory()->NewVariableDeclaration(
4883 proxy, CONST, block_scope, pos, is_class_declaration, 4880 proxy, CONST, block_scope, pos, is_class_declaration,
4884 scope_->class_declaration_group_start()); 4881 scope_->class_declaration_group_start());
4885 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); 4882 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
4886 } 4883 }
4887 4884
4888 Expression* extends = NULL; 4885 Expression* extends = NULL;
4889 if (Check(Token::EXTENDS)) { 4886 if (Check(Token::EXTENDS)) {
4890 block_scope->set_start_position(scanner()->location().end_pos); 4887 block_scope->set_start_position(scanner()->location().end_pos);
4891 ExpressionClassifier classifier; 4888 ExpressionClassifier classifier(this);
4892 extends = ParseLeftHandSideExpression(&classifier, CHECK_OK); 4889 extends = ParseLeftHandSideExpression(&classifier, CHECK_OK);
4893 extends = ParserTraits::RewriteNonPattern(extends, &classifier, CHECK_OK); 4890 RewriteNonPattern(&classifier, CHECK_OK);
4894 } else { 4891 } else {
4895 block_scope->set_start_position(scanner()->location().end_pos); 4892 block_scope->set_start_position(scanner()->location().end_pos);
4896 } 4893 }
4897 4894
4898 4895
4899 ClassLiteralChecker checker(this); 4896 ClassLiteralChecker checker(this);
4900 ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4, zone()); 4897 ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4, zone());
4901 FunctionLiteral* constructor = NULL; 4898 FunctionLiteral* constructor = NULL;
4902 bool has_seen_constructor = false; 4899 bool has_seen_constructor = false;
4903 4900
4904 Expect(Token::LBRACE, CHECK_OK); 4901 Expect(Token::LBRACE, CHECK_OK);
4905 4902
4906 const bool has_extends = extends != nullptr; 4903 const bool has_extends = extends != nullptr;
4907 while (peek() != Token::RBRACE) { 4904 while (peek() != Token::RBRACE) {
4908 if (Check(Token::SEMICOLON)) continue; 4905 if (Check(Token::SEMICOLON)) continue;
4909 FuncNameInferrer::State fni_state(fni_); 4906 FuncNameInferrer::State fni_state(fni_);
4910 const bool in_class = true; 4907 const bool in_class = true;
4911 const bool is_static = false; 4908 const bool is_static = false;
4912 bool is_computed_name = false; // Classes do not care about computed 4909 bool is_computed_name = false; // Classes do not care about computed
4913 // property names here. 4910 // property names here.
4914 ExpressionClassifier classifier; 4911 ExpressionClassifier classifier(this);
4915 const AstRawString* property_name = nullptr; 4912 const AstRawString* property_name = nullptr;
4916 ObjectLiteral::Property* property = ParsePropertyDefinition( 4913 ObjectLiteral::Property* property = ParsePropertyDefinition(
4917 &checker, in_class, has_extends, is_static, &is_computed_name, 4914 &checker, in_class, has_extends, is_static, &is_computed_name,
4918 &has_seen_constructor, &classifier, &property_name, CHECK_OK); 4915 &has_seen_constructor, &classifier, &property_name, CHECK_OK);
4919 property = ParserTraits::RewriteNonPatternObjectLiteralProperty( 4916 RewriteNonPattern(&classifier, CHECK_OK);
4920 property, &classifier, CHECK_OK);
4921 4917
4922 if (has_seen_constructor && constructor == NULL) { 4918 if (has_seen_constructor && constructor == NULL) {
4923 constructor = GetPropertyValue(property)->AsFunctionLiteral(); 4919 constructor = GetPropertyValue(property)->AsFunctionLiteral();
4924 DCHECK_NOT_NULL(constructor); 4920 DCHECK_NOT_NULL(constructor);
4925 constructor->set_raw_name( 4921 constructor->set_raw_name(
4926 name != nullptr ? name : ast_value_factory()->empty_string()); 4922 name != nullptr ? name : ast_value_factory()->empty_string());
4927 } else { 4923 } else {
4928 properties->Add(property, zone()); 4924 properties->Add(property, zone());
4929 } 4925 }
4930 4926
(...skipping 30 matching lines...) Expand all
4961 Expression* Parser::ParseV8Intrinsic(bool* ok) { 4957 Expression* Parser::ParseV8Intrinsic(bool* ok) {
4962 // CallRuntime :: 4958 // CallRuntime ::
4963 // '%' Identifier Arguments 4959 // '%' Identifier Arguments
4964 4960
4965 int pos = peek_position(); 4961 int pos = peek_position();
4966 Expect(Token::MOD, CHECK_OK); 4962 Expect(Token::MOD, CHECK_OK);
4967 // Allow "eval" or "arguments" for backward compatibility. 4963 // Allow "eval" or "arguments" for backward compatibility.
4968 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers, 4964 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers,
4969 CHECK_OK); 4965 CHECK_OK);
4970 Scanner::Location spread_pos; 4966 Scanner::Location spread_pos;
4971 ExpressionClassifier classifier; 4967 ExpressionClassifier classifier(this);
4972 ZoneList<Expression*>* args = 4968 ZoneList<Expression*>* args =
4973 ParseArguments(&spread_pos, &classifier, CHECK_OK); 4969 ParseArguments(&spread_pos, &classifier, CHECK_OK);
4974 4970
4975 DCHECK(!spread_pos.IsValid()); 4971 DCHECK(!spread_pos.IsValid());
4976 4972
4977 if (extension_ != NULL) { 4973 if (extension_ != NULL) {
4978 // The extension structures are only accessible while parsing the 4974 // The extension structures are only accessible while parsing the
4979 // very first time not when reparsing because of lazy compilation. 4975 // very first time not when reparsing because of lazy compilation.
4980 scope_->DeclarationScope()->ForceEagerCompilation(); 4976 scope_->DeclarationScope()->ForceEagerCompilation();
4981 } 4977 }
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
5532 SetLanguageMode(scope_, 5528 SetLanguageMode(scope_,
5533 static_cast<LanguageMode>(scope_->language_mode() | mode)); 5529 static_cast<LanguageMode>(scope_->language_mode() | mode));
5534 } 5530 }
5535 5531
5536 5532
5537 void ParserTraits::RewriteDestructuringAssignments() { 5533 void ParserTraits::RewriteDestructuringAssignments() {
5538 parser_->RewriteDestructuringAssignments(); 5534 parser_->RewriteDestructuringAssignments();
5539 } 5535 }
5540 5536
5541 5537
5542 Expression* ParserTraits::RewriteNonPattern( 5538 void ParserTraits::RewriteNonPattern(Type::ExpressionClassifier* classifier,
5543 Expression* expr, const ExpressionClassifier* classifier, bool* ok) { 5539 bool* ok) {
5544 return parser_->RewriteNonPattern(expr, classifier, ok); 5540 parser_->RewriteNonPattern(classifier, ok);
5545 } 5541 }
5546 5542
5547 5543
5548 ObjectLiteralProperty* ParserTraits::RewriteNonPatternObjectLiteralProperty( 5544 Zone* ParserTraits::zone() const {
5549 ObjectLiteralProperty* property, const ExpressionClassifier* classifier, 5545 return parser_->function_state_->scope()->zone();
5550 bool* ok) {
5551 return parser_->RewriteNonPatternObjectLiteralProperty(property, classifier,
5552 ok);
5553 } 5546 }
5554 5547
5555 5548
5549 ZoneList<Expression*>* ParserTraits::GetNonPatternList() const {
5550 return parser_->function_state_->non_patterns_to_rewrite();
5551 }
5552
5553
5556 class NonPatternRewriter : public AstExpressionRewriter { 5554 class NonPatternRewriter : public AstExpressionRewriter {
5557 public: 5555 public:
5558 NonPatternRewriter(uintptr_t stack_limit, Parser* parser) 5556 NonPatternRewriter(uintptr_t stack_limit, Parser* parser)
5559 : AstExpressionRewriter(stack_limit), parser_(parser) {} 5557 : AstExpressionRewriter(stack_limit), parser_(parser) {}
5560 ~NonPatternRewriter() override {} 5558 ~NonPatternRewriter() override {}
5561 5559
5562 private: 5560 private:
5563 bool RewriteExpression(Expression* expr) override { 5561 bool RewriteExpression(Expression* expr) override {
5562 if (expr->IsRewritableExpression()) return true;
5564 // Rewrite only what could have been a pattern but is not. 5563 // Rewrite only what could have been a pattern but is not.
5565 if (expr->IsArrayLiteral()) { 5564 if (expr->IsArrayLiteral()) {
5566 // Spread rewriting in array literals. 5565 // Spread rewriting in array literals.
5567 ArrayLiteral* lit = expr->AsArrayLiteral(); 5566 ArrayLiteral* lit = expr->AsArrayLiteral();
5568 VisitExpressions(lit->values()); 5567 VisitExpressions(lit->values());
5569 replacement_ = parser_->RewriteSpreads(lit); 5568 replacement_ = parser_->RewriteSpreads(lit);
5570 return false; 5569 return false;
5571 } 5570 }
5572 if (expr->IsObjectLiteral()) { 5571 if (expr->IsObjectLiteral()) {
5573 return true; 5572 return true;
5574 } 5573 }
5575 if (expr->IsBinaryOperation() && 5574 if (expr->IsBinaryOperation() &&
5576 expr->AsBinaryOperation()->op() == Token::COMMA) { 5575 expr->AsBinaryOperation()->op() == Token::COMMA) {
5577 return true; 5576 return true;
5578 } 5577 }
5579 // Everything else does not need rewriting. 5578 // Everything else does not need rewriting.
5580 return false; 5579 return false;
5581 } 5580 }
5582 5581
5583 void VisitObjectLiteralProperty(ObjectLiteralProperty* property) override { 5582 void VisitObjectLiteralProperty(ObjectLiteralProperty* property) override {
5584 if (property == nullptr) return; 5583 if (property == nullptr) return;
5585 // Do not rewrite (computed) key expressions 5584 // Do not rewrite (computed) key expressions
5586 AST_REWRITE_PROPERTY(Expression, property, value); 5585 AST_REWRITE_PROPERTY(Expression, property, value);
5587 } 5586 }
5588 5587
5589 Parser* parser_; 5588 Parser* parser_;
5590 }; 5589 };
5591 5590
5592 5591
5593 Expression* Parser::RewriteNonPattern(Expression* expr, 5592 void Parser::RewriteNonPattern(ExpressionClassifier* classifier, bool* ok) {
5594 const ExpressionClassifier* classifier,
5595 bool* ok) {
5596 ValidateExpression(classifier, ok); 5593 ValidateExpression(classifier, ok);
5597 if (!*ok) return expr; 5594 if (!*ok) return;
5598 NonPatternRewriter rewriter(stack_limit_, this); 5595 auto non_patterns_to_rewrite = function_state_->non_patterns_to_rewrite();
5599 Expression* result = reinterpret_cast<Expression*>(rewriter.Rewrite(expr)); 5596 int begin = classifier->GetNonPatternBegin();
5600 DCHECK_NOT_NULL(result); 5597 int end = non_patterns_to_rewrite->length();
5601 return result; 5598 if (begin < end) {
5602 } 5599 NonPatternRewriter rewriter(stack_limit_, this);
5603 5600 for (int i = begin; i < end; i++) {
5604 5601 DCHECK(non_patterns_to_rewrite->at(i)->IsRewritableExpression());
5605 ObjectLiteralProperty* Parser::RewriteNonPatternObjectLiteralProperty( 5602 rewriter.Rewrite(non_patterns_to_rewrite->at(i));
5606 ObjectLiteralProperty* property, const ExpressionClassifier* classifier, 5603 }
5607 bool* ok) { 5604 non_patterns_to_rewrite->Rewind(begin);
5608 if (property != nullptr) {
5609 // Do not rewrite (computed) key expressions
5610 Expression* value = RewriteNonPattern(property->value(), classifier, ok);
5611 property->set_value(value);
5612 } 5605 }
5613 return property;
5614 } 5606 }
5615 5607
5616 5608
5617 void Parser::RewriteDestructuringAssignments() { 5609 void Parser::RewriteDestructuringAssignments() {
5618 FunctionState* func = function_state_;
5619 if (!allow_harmony_destructuring_assignment()) return; 5610 if (!allow_harmony_destructuring_assignment()) return;
5620 const List<DestructuringAssignment>& assignments = 5611 const auto& assignments =
5621 func->destructuring_assignments_to_rewrite(); 5612 function_state_->destructuring_assignments_to_rewrite();
5622 for (int i = assignments.length() - 1; i >= 0; --i) { 5613 for (int i = assignments.length() - 1; i >= 0; --i) {
5623 // Rewrite list in reverse, so that nested assignment patterns are rewritten 5614 // Rewrite list in reverse, so that nested assignment patterns are rewritten
5624 // correctly. 5615 // correctly.
5625 DestructuringAssignment pair = assignments.at(i); 5616 const DestructuringAssignment& pair = assignments.at(i);
5626 RewritableAssignmentExpression* to_rewrite = 5617 RewritableExpression* to_rewrite =
5627 pair.assignment->AsRewritableAssignmentExpression(); 5618 pair.assignment->AsRewritableExpression();
5628 Scope* scope = pair.scope;
5629 DCHECK_NOT_NULL(to_rewrite); 5619 DCHECK_NOT_NULL(to_rewrite);
5630 if (!to_rewrite->is_rewritten()) { 5620 if (!to_rewrite->is_rewritten()) {
5631 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite, scope); 5621 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite,
5622 pair.scope);
5632 } 5623 }
5633 } 5624 }
5634 } 5625 }
5635 5626
5636 5627
5637 Expression* Parser::RewriteSpreads(ArrayLiteral* lit) { 5628 Expression* Parser::RewriteSpreads(ArrayLiteral* lit) {
5638 // Array literals containing spreads are rewritten using do expressions, e.g. 5629 // Array literals containing spreads are rewritten using do expressions, e.g.
5639 // [1, 2, 3, ...x, 4, ...y, 5] 5630 // [1, 2, 3, ...x, 4, ...y, 5]
5640 // is roughly rewritten as: 5631 // is roughly rewritten as:
5641 // do { 5632 // do {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
5747 } 5738 }
5748 } 5739 }
5749 // Now, rewind the original array literal to truncate everything from the 5740 // Now, rewind the original array literal to truncate everything from the
5750 // first spread (included) until the end. This fixes $R's initialization. 5741 // first spread (included) until the end. This fixes $R's initialization.
5751 lit->RewindSpreads(); 5742 lit->RewindSpreads();
5752 return factory()->NewDoExpression(do_block, result, lit->position()); 5743 return factory()->NewDoExpression(do_block, result, lit->position());
5753 } 5744 }
5754 5745
5755 5746
5756 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) { 5747 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) {
5757 DCHECK(expr->IsRewritableAssignmentExpression()); 5748 DCHECK(expr->IsRewritableExpression());
5758 parser_->function_state_->AddDestructuringAssignment( 5749 parser_->function_state_->AddDestructuringAssignment(
5759 Parser::DestructuringAssignment(expr, parser_->scope_)); 5750 Parser::DestructuringAssignment(expr, parser_->scope_));
5760 } 5751 }
5761 5752
5762 5753
5754 void ParserTraits::QueueNonPatternForRewriting(Expression* expr) {
5755 DCHECK(expr->IsRewritableExpression());
5756 parser_->function_state_->AddNonPatternForRewriting(expr);
5757 }
5758
5759
5763 void ParserTraits::SetFunctionNameFromPropertyName( 5760 void ParserTraits::SetFunctionNameFromPropertyName(
5764 ObjectLiteralProperty* property, const AstRawString* name) { 5761 ObjectLiteralProperty* property, const AstRawString* name) {
5765 Expression* value = property->value(); 5762 Expression* value = property->value();
5766 if (!value->IsAnonymousFunctionDefinition()) return; 5763 if (!value->IsAnonymousFunctionDefinition()) return;
5767 5764
5768 // Computed name setting must happen at runtime. 5765 // Computed name setting must happen at runtime.
5769 if (property->is_computed_name()) return; 5766 if (property->is_computed_name()) return;
5770 5767
5771 DCHECK_NOT_NULL(name); 5768 DCHECK_NOT_NULL(name);
5772 5769
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
6382 6379
6383 statements->Add(get_return, zone); 6380 statements->Add(get_return, zone);
6384 statements->Add(check_return, zone); 6381 statements->Add(check_return, zone);
6385 statements->Add(call_return, zone); 6382 statements->Add(call_return, zone);
6386 statements->Add(validate_output, zone); 6383 statements->Add(validate_output, zone);
6387 } 6384 }
6388 6385
6389 6386
6390 } // namespace internal 6387 } // namespace internal
6391 } // namespace v8 6388 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698