Commit 0f432ebb authored by wingo's avatar wingo Committed by Commit bot

Refactor formal parameter error locations into a class

This is a follow-up to https://codereview.chromium.org/1078093002.

R=rossberg@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1083623002

Cr-Commit-Position: refs/heads/master@{#27780}
parent 2f327a5c
...@@ -3706,8 +3706,7 @@ Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { ...@@ -3706,8 +3706,7 @@ Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression, bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression,
Scope* scope, int* num_params, Scope* scope, int* num_params,
Scanner::Location* undefined_loc, FormalParameterErrorLocations* locs) {
Scanner::Location* dupe_loc) {
// Case for empty parameter lists: // Case for empty parameter lists:
// () => ... // () => ...
if (expression == NULL) return true; if (expression == NULL) return true;
...@@ -3726,12 +3725,12 @@ bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression, ...@@ -3726,12 +3725,12 @@ bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression,
if (traits->IsEvalOrArguments(raw_name) || if (traits->IsEvalOrArguments(raw_name) ||
traits->IsFutureStrictReserved(raw_name)) traits->IsFutureStrictReserved(raw_name))
return false; return false;
if (traits->IsUndefined(raw_name) && !undefined_loc->IsValid()) { if (traits->IsUndefined(raw_name) && !locs->undefined_.IsValid()) {
*undefined_loc = Scanner::Location( locs->undefined_ = Scanner::Location(
expression->position(), expression->position() + raw_name->length()); expression->position(), expression->position() + raw_name->length());
} }
if (scope->IsDeclared(raw_name)) { if (scope->IsDeclared(raw_name)) {
*dupe_loc = Scanner::Location( locs->duplicate_ = Scanner::Location(
expression->position(), expression->position() + raw_name->length()); expression->position(), expression->position() + raw_name->length());
return false; return false;
} }
...@@ -3754,9 +3753,9 @@ bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression, ...@@ -3754,9 +3753,9 @@ bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression,
return false; return false;
return CheckAndDeclareArrowParameter(traits, binop->left(), scope, return CheckAndDeclareArrowParameter(traits, binop->left(), scope,
num_params, undefined_loc, dupe_loc) && num_params, locs) &&
CheckAndDeclareArrowParameter(traits, binop->right(), scope, CheckAndDeclareArrowParameter(traits, binop->right(), scope,
num_params, undefined_loc, dupe_loc); num_params, locs);
} }
// Any other kind of expression is not a valid parameter list. // Any other kind of expression is not a valid parameter list.
...@@ -3765,15 +3764,15 @@ bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression, ...@@ -3765,15 +3764,15 @@ bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression,
int ParserTraits::DeclareArrowParametersFromExpression( int ParserTraits::DeclareArrowParametersFromExpression(
Expression* expression, Scope* scope, Scanner::Location* undefined_loc, Expression* expression, Scope* scope, FormalParameterErrorLocations* locs,
Scanner::Location* dupe_loc, bool* ok) { bool* ok) {
int num_params = 0; int num_params = 0;
// Always reset the flag: It only needs to be set for the first expression // Always reset the flag: It only needs to be set for the first expression
// parsed as arrow function parameter list, because only top-level functions // parsed as arrow function parameter list, because only top-level functions
// are parsed lazily. // are parsed lazily.
parser_->parsing_lazy_arrow_parameters_ = false; parser_->parsing_lazy_arrow_parameters_ = false;
*ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, *ok =
undefined_loc, dupe_loc); CheckAndDeclareArrowParameter(this, expression, scope, &num_params, locs);
return num_params; return num_params;
} }
...@@ -3873,22 +3872,12 @@ FunctionLiteral* Parser::ParseFunctionLiteral( ...@@ -3873,22 +3872,12 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
function_state.set_generator_object_variable(temp); function_state.set_generator_object_variable(temp);
} }
// We don't yet know if the function will be strict, so we cannot yet FormalParameterErrorLocations error_locs;
// produce errors for parameter names or duplicates. However, we remember
// the locations of these errors if they occur and produce the errors later.
Scanner::Location eval_args_loc = Scanner::Location::invalid();
Scanner::Location dupe_loc = Scanner::Location::invalid();
Scanner::Location reserved_loc = Scanner::Location::invalid();
// Similarly for strong mode.
Scanner::Location undefined_loc = Scanner::Location::invalid();
bool has_rest = false; bool has_rest = false;
Expect(Token::LPAREN, CHECK_OK); Expect(Token::LPAREN, CHECK_OK);
int start_position = scanner()->location().beg_pos; int start_position = scanner()->location().beg_pos;
ZoneList<const AstRawString*>* params = ZoneList<const AstRawString*>* params =
ParseFormalParameterList(&eval_args_loc, &undefined_loc, &dupe_loc, ParseFormalParameterList(&error_locs, &has_rest, CHECK_OK);
&reserved_loc, &has_rest, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
int formals_end_position = scanner()->location().end_pos; int formals_end_position = scanner()->location().end_pos;
...@@ -3898,7 +3887,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral( ...@@ -3898,7 +3887,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
scope->set_start_position(start_position); scope->set_start_position(start_position);
num_parameters = params->length(); num_parameters = params->length();
if (dupe_loc.IsValid()) { if (error_locs.duplicate_.IsValid()) {
duplicate_parameters = FunctionLiteral::kHasDuplicateParameters; duplicate_parameters = FunctionLiteral::kHasDuplicateParameters;
} }
...@@ -3995,9 +3984,8 @@ FunctionLiteral* Parser::ParseFunctionLiteral( ...@@ -3995,9 +3984,8 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
name_is_strict_reserved, function_name_location, name_is_strict_reserved, function_name_location,
CHECK_OK); CHECK_OK);
const bool use_strict_params = has_rest || IsConciseMethod(kind); const bool use_strict_params = has_rest || IsConciseMethod(kind);
CheckFunctionParameterNames(language_mode(), use_strict_params, CheckFunctionParameterNames(language_mode(), use_strict_params, error_locs,
eval_args_loc, undefined_loc, dupe_loc, CHECK_OK);
reserved_loc, CHECK_OK);
if (is_strict(language_mode())) { if (is_strict(language_mode())) {
CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), CheckStrictOctalLiteral(scope->start_position(), scope->end_position(),
......
...@@ -755,8 +755,7 @@ class ParserTraits { ...@@ -755,8 +755,7 @@ class ParserTraits {
// Utility functions // Utility functions
int DeclareArrowParametersFromExpression(Expression* expression, Scope* scope, int DeclareArrowParametersFromExpression(Expression* expression, Scope* scope,
Scanner::Location* undefined_loc, FormalParameterErrorLocations* locs,
Scanner::Location* dupe_loc,
bool* ok); bool* ok);
// Temporary glue; these functions will move to ParserBase. // Temporary glue; these functions will move to ParserBase.
......
...@@ -908,22 +908,13 @@ PreParser::Expression PreParser::ParseFunctionLiteral( ...@@ -908,22 +908,13 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
PreParserFactory factory(NULL); PreParserFactory factory(NULL);
FunctionState function_state(&function_state_, &scope_, function_scope, kind, FunctionState function_state(&function_state_, &scope_, function_scope, kind,
&factory); &factory);
// We don't yet know if the function will be strict, so we cannot yet produce FormalParameterErrorLocations error_locs;
// errors for parameter names or duplicates. However, we remember the
// locations of these errors if they occur and produce the errors later.
Scanner::Location eval_args_loc = Scanner::Location::invalid();
Scanner::Location dupe_loc = Scanner::Location::invalid();
Scanner::Location reserved_loc = Scanner::Location::invalid();
// Similarly for strong mode.
Scanner::Location undefined_loc = Scanner::Location::invalid();
bool is_rest = false; bool is_rest = false;
Expect(Token::LPAREN, CHECK_OK); Expect(Token::LPAREN, CHECK_OK);
int start_position = scanner()->location().beg_pos; int start_position = scanner()->location().beg_pos;
PreParserFormalParameterList params = PreParserFormalParameterList params =
ParseFormalParameterList(&eval_args_loc, &undefined_loc, &dupe_loc, ParseFormalParameterList(&error_locs, &is_rest, CHECK_OK);
&reserved_loc, &is_rest, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
int formals_end_position = scanner()->location().end_pos; int formals_end_position = scanner()->location().end_pos;
...@@ -950,8 +941,8 @@ PreParser::Expression PreParser::ParseFunctionLiteral( ...@@ -950,8 +941,8 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
CheckFunctionName(language_mode(), kind, function_name, CheckFunctionName(language_mode(), kind, function_name,
name_is_strict_reserved, function_name_location, CHECK_OK); name_is_strict_reserved, function_name_location, CHECK_OK);
const bool use_strict_params = is_rest || IsConciseMethod(kind); const bool use_strict_params = is_rest || IsConciseMethod(kind);
CheckFunctionParameterNames(language_mode(), use_strict_params, eval_args_loc, CheckFunctionParameterNames(language_mode(), use_strict_params, error_locs,
undefined_loc, dupe_loc, reserved_loc, CHECK_OK); CHECK_OK);
if (is_strict(language_mode())) { if (is_strict(language_mode())) {
int end_position = scanner()->location().end_pos; int end_position = scanner()->location().end_pos;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment