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

Unified Diff: src/parsing/preparser.cc

Issue 2472063002: Preparse lazy function parameters (Closed)
Patch Set: IsArrowFunction Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/parsing/preparser.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/parsing/preparser.cc
diff --git a/src/parsing/preparser.cc b/src/parsing/preparser.cc
index 025633c3f90cecec8a1b96ab42ac58fc2fb1573e..1ddda66b4e2037409af95aeeda141896b8e01a6c 100644
--- a/src/parsing/preparser.cc
+++ b/src/parsing/preparser.cc
@@ -84,8 +84,9 @@ PreParserIdentifier PreParser::GetSymbol() const {
}
PreParser::PreParseResult PreParser::PreParseFunction(
- DeclarationScope* function_scope, bool parsing_module, SingletonLogger* log,
- bool is_inner_function, bool may_abort, int* use_counts) {
+ FunctionKind kind, DeclarationScope* function_scope, bool parsing_module,
+ SingletonLogger* log, bool is_inner_function, bool may_abort,
+ int* use_counts) {
DCHECK_EQ(FUNCTION_SCOPE, function_scope->scope_type());
parsing_module_ = parsing_module;
log_ = log;
@@ -98,24 +99,63 @@ PreParser::PreParseResult PreParser::PreParseFunction(
// PreParser.
DCHECK_NULL(scope_state_);
FunctionState function_state(&function_state_, &scope_state_, function_scope);
- DCHECK_EQ(Token::LBRACE, scanner()->current_token());
- bool ok = true;
- int start_position = peek_position();
- LazyParsingResult result = ParseStatementListAndLogFunction(may_abort, &ok);
+ // This indirection is needed so that we can use the CHECK_OK macros.
+ bool ok_holder = true;
+ bool* ok = &ok_holder;
+
+ PreParserFormalParameters formals(function_scope);
+ bool has_duplicate_parameters = false;
+ DuplicateFinder duplicate_finder(scanner()->unicode_cache());
+ std::unique_ptr<ExpressionClassifier> formals_classifier;
+
+ // Parse non-arrow function parameters. For arrow functions, the parameters
+ // have already been parsed.
+ if (!IsArrowFunction(kind)) {
+ formals_classifier.reset(new ExpressionClassifier(this, &duplicate_finder));
+ // We return kPreParseSuccess in failure cases too - errors are retrieved
+ // separately by Parser::SkipLazyFunctionBody.
+ ParseFormalParameterList(&formals, CHECK_OK_VALUE(kPreParseSuccess));
+ Expect(Token::RPAREN, CHECK_OK_VALUE(kPreParseSuccess));
+ int formals_end_position = scanner()->location().end_pos;
+
+ CheckArityRestrictions(
+ formals.arity, kind, formals.has_rest, function_scope->start_position(),
+ formals_end_position, CHECK_OK_VALUE(kPreParseSuccess));
+ has_duplicate_parameters =
+ !classifier()->is_valid_formal_parameter_list_without_duplicates();
+ }
+
+ Expect(Token::LBRACE, CHECK_OK_VALUE(kPreParseSuccess));
+ LazyParsingResult result = ParseStatementListAndLogFunction(
+ function_scope->start_position(), &formals, has_duplicate_parameters,
+ may_abort, ok);
use_counts_ = nullptr;
track_unresolved_variables_ = false;
if (result == kLazyParsingAborted) {
return kPreParseAbort;
} else if (stack_overflow()) {
return kPreParseStackOverflow;
- } else if (!ok) {
+ } else if (!*ok) {
DCHECK(log->has_error());
} else {
DCHECK_EQ(Token::RBRACE, scanner()->peek());
+
+ if (!IsArrowFunction(kind)) {
+ // Validate parameter names. We can do this only after parsing the
+ // function, since the function can declare itself strict.
+ const bool allow_duplicate_parameters =
+ is_sloppy(function_scope->language_mode()) && formals.is_simple &&
+ !IsConciseMethod(kind);
+ ValidateFormalParameters(function_scope->language_mode(),
+ allow_duplicate_parameters,
+ CHECK_OK_VALUE(kPreParseSuccess));
+ }
+
if (is_strict(function_scope->language_mode())) {
int end_pos = scanner()->location().end_pos;
- CheckStrictOctalLiteral(start_position, end_pos, &ok);
- CheckDecimalLiteralWithLeadingZero(start_position, end_pos);
+ CheckStrictOctalLiteral(function_scope->start_position(), end_pos, ok);
+ CheckDecimalLiteralWithLeadingZero(function_scope->start_position(),
+ end_pos);
}
}
return kPreParseSuccess;
@@ -195,8 +235,8 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
}
PreParser::LazyParsingResult PreParser::ParseStatementListAndLogFunction(
- bool may_abort, bool* ok) {
- int body_start = position();
+ int start_position, PreParserFormalParameters* formals,
+ bool has_duplicate_parameters, bool may_abort, bool* ok) {
PreParserStatementList body;
LazyParsingResult result = ParseStatementList(
body, Token::RBRACE, may_abort, CHECK_OK_VALUE(kLazyParsingComplete));
@@ -207,7 +247,8 @@ PreParser::LazyParsingResult PreParser::ParseStatementListAndLogFunction(
int body_end = scanner()->peek_location().end_pos;
DeclarationScope* scope = this->scope()->AsDeclarationScope();
DCHECK(scope->is_function_scope());
- log_->LogFunction(body_start, body_end,
+ log_->LogFunction(start_position, body_end, formals->num_parameters(),
+ formals->function_length, has_duplicate_parameters,
function_state_->materialized_literal_count(),
function_state_->expected_property_count(), language_mode(),
scope->uses_super_property(), scope->calls_eval());
« no previous file with comments | « src/parsing/preparser.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698