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

Side by Side Diff: src/parser.cc

Issue 1178523002: Support rest parameters in arrow functions (Closed) Base URL: https://chromium.googlesource.com/v8/v8@master
Patch Set: Fix error locations for param-after-rest errors Created 5 years, 6 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/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 3741 matching lines...) Expand 10 before | Expand all | Expand 10 after
3752 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot)); 3752 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
3753 return static_cast<LiteralType>(literal_type->value()); 3753 return static_cast<LiteralType>(literal_type->value());
3754 } 3754 }
3755 3755
3756 3756
3757 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { 3757 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3758 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); 3758 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3759 } 3759 }
3760 3760
3761 3761
3762 void ParserTraits::DeclareArrowFunctionParameters( 3762 void ParserTraits::ParseArrowFunctionFormalParameters(
3763 Scope* scope, Expression* expr, const Scanner::Location& params_loc, 3763 Scope* scope, Expression* expr, const Scanner::Location& params_loc,
3764 Scanner::Location* duplicate_loc, bool* ok) { 3764 bool* has_rest, Scanner::Location* duplicate_loc, bool* ok) {
3765 if (scope->num_parameters() >= Code::kMaxArguments) { 3765 if (scope->num_parameters() >= Code::kMaxArguments) {
3766 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList); 3766 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
3767 *ok = false; 3767 *ok = false;
3768 return; 3768 return;
3769 } 3769 }
3770 3770
3771 // ArrowFunctionFormals :: 3771 // ArrowFunctionFormals ::
3772 // Binary(Token::COMMA, ArrowFunctionFormals, VariableProxy) 3772 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail)
3773 // Tail
3774 // NonTailArrowFunctionFormals ::
3775 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy)
3773 // VariableProxy 3776 // VariableProxy
3777 // Tail ::
3778 // VariableProxy
3779 // Spread(VariableProxy)
3774 // 3780 //
3775 // As we need to visit the parameters in left-to-right order, we recurse on 3781 // As we need to visit the parameters in left-to-right order, we recurse on
3776 // the left-hand side of comma expressions. 3782 // the left-hand side of comma expressions.
3777 // 3783 //
3778 // Sadly, for the various malformed_arrow_function_parameter_list errors, we
3779 // can't be more specific on the error message or on the location because we
3780 // need to match the pre-parser's behavior.
3781 if (expr->IsBinaryOperation()) { 3784 if (expr->IsBinaryOperation()) {
3782 BinaryOperation* binop = expr->AsBinaryOperation(); 3785 BinaryOperation* binop = expr->AsBinaryOperation();
3783 // The classifier has already run, so we know that the expression is a valid 3786 // The classifier has already run, so we know that the expression is a valid
3784 // arrow function formals production. 3787 // arrow function formals production.
3785 DCHECK_EQ(binop->op(), Token::COMMA); 3788 DCHECK_EQ(binop->op(), Token::COMMA);
3786 Expression* left = binop->left(); 3789 Expression* left = binop->left();
3787 Expression* right = binop->right(); 3790 Expression* right = binop->right();
3788 DeclareArrowFunctionParameters(scope, left, params_loc, duplicate_loc, ok); 3791 ParseArrowFunctionFormalParameters(scope, left, params_loc, has_rest,
3792 duplicate_loc, ok);
3789 if (!*ok) return; 3793 if (!*ok) return;
3790 // LHS of comma expression should be unparenthesized. 3794 // LHS of comma expression should be unparenthesized.
3791 expr = right; 3795 expr = right;
3792 } 3796 }
3793 3797
3794 // TODO(wingo): Support rest parameters. 3798 // Only the right-most expression may be a rest parameter.
3799 DCHECK(!*has_rest);
3800
3801 if (expr->IsSpread()) {
3802 *has_rest = true;
3803 expr = expr->AsSpread()->expression();
3804 }
3805
3795 DCHECK(expr->IsVariableProxy()); 3806 DCHECK(expr->IsVariableProxy());
3796 DCHECK(!expr->AsVariableProxy()->is_this()); 3807 DCHECK(!expr->AsVariableProxy()->is_this());
3797 3808
3798 const AstRawString* raw_name = expr->AsVariableProxy()->raw_name(); 3809 const AstRawString* raw_name = expr->AsVariableProxy()->raw_name();
3799 Scanner::Location param_location(expr->position(), 3810 Scanner::Location param_location(expr->position(),
3800 expr->position() + raw_name->length()); 3811 expr->position() + raw_name->length());
3801 3812
3802 // When the formal parameter was originally seen, it was parsed as a 3813 // When the formal parameter was originally seen, it was parsed as a
3803 // VariableProxy and recorded as unresolved in the scope. Here we undo that 3814 // VariableProxy and recorded as unresolved in the scope. Here we undo that
3804 // parse-time side-effect. 3815 // parse-time side-effect.
3805 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); 3816 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
3806 3817
3807 bool is_rest = false;
3808 ExpressionClassifier classifier; 3818 ExpressionClassifier classifier;
3809 DeclareFormalParameter(scope, raw_name, &classifier, is_rest); 3819 DeclareFormalParameter(scope, raw_name, &classifier, *has_rest);
3810 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; 3820 if (!duplicate_loc->IsValid()) {
3821 *duplicate_loc = classifier.duplicate_formal_parameter_error().location;
3822 }
3811 } 3823 }
3812 3824
3813 3825
3814 void ParserTraits::ParseArrowFunctionFormalParameters(
3815 Scope* scope, Expression* params, const Scanner::Location& params_loc,
3816 bool* is_rest, Scanner::Location* duplicate_loc, bool* ok) {
3817 DeclareArrowFunctionParameters(scope, params, params_loc, duplicate_loc, ok);
3818 }
3819
3820
3821 FunctionLiteral* Parser::ParseFunctionLiteral( 3826 FunctionLiteral* Parser::ParseFunctionLiteral(
3822 const AstRawString* function_name, Scanner::Location function_name_location, 3827 const AstRawString* function_name, Scanner::Location function_name_location,
3823 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos, 3828 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos,
3824 FunctionLiteral::FunctionType function_type, 3829 FunctionLiteral::FunctionType function_type,
3825 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { 3830 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
3826 // Function :: 3831 // Function ::
3827 // '(' FormalParameterList? ')' '{' FunctionBody '}' 3832 // '(' FormalParameterList? ')' '{' FunctionBody '}'
3828 // 3833 //
3829 // Getter :: 3834 // Getter ::
3830 // '(' ')' '{' FunctionBody '}' 3835 // '(' ')' '{' FunctionBody '}'
(...skipping 1988 matching lines...) Expand 10 before | Expand all | Expand 10 after
5819 Expression* Parser::SpreadCallNew(Expression* function, 5824 Expression* Parser::SpreadCallNew(Expression* function,
5820 ZoneList<v8::internal::Expression*>* args, 5825 ZoneList<v8::internal::Expression*>* args,
5821 int pos) { 5826 int pos) {
5822 args->InsertAt(0, function, zone()); 5827 args->InsertAt(0, function, zone());
5823 5828
5824 return factory()->NewCallRuntime( 5829 return factory()->NewCallRuntime(
5825 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5830 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5826 } 5831 }
5827 } // namespace internal 5832 } // namespace internal
5828 } // namespace v8 5833 } // namespace v8
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | test/message/arrow-param-after-rest.out » ('J')

Powered by Google App Engine
This is Rietveld 408576698