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

Unified Diff: src/preparser.h

Issue 1272673003: [es6] Re-implement rest parameters via desugaring. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Tiny bit more cleanup Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: src/preparser.h
diff --git a/src/preparser.h b/src/preparser.h
index 07e431bfc54790b0a65c081f9ea8021ab6fba84c..ac2c3edf8a2a5affe8a8b68d63cfd5dc05e71aac 100644
--- a/src/preparser.h
+++ b/src/preparser.h
@@ -31,6 +31,7 @@ struct FormalParametersBase {
bool has_rest = false;
bool is_simple = true;
int materialized_literals_count = 0;
+ mutable int rest_array_literal_index = -1;
};
@@ -917,7 +918,10 @@ class PreParserExpression {
static PreParserExpression BinaryOperation(PreParserExpression left,
Token::Value op,
PreParserExpression right) {
- return PreParserExpression(TypeField::encode(kBinaryOperationExpression));
+ return PreParserExpression(
+ TypeField::encode(kBinaryOperationExpression) |
+ HasRestField::encode(op == Token::COMMA &&
+ right->IsSpreadExpression()));
}
static PreParserExpression StringLiteral() {
@@ -1034,6 +1038,12 @@ class PreParserExpression {
return TypeField::decode(code_) == kSpreadExpression;
}
+ bool HasRestParameter() const {
+ // Preparser's way of determining if an arrow function has a rest parameter.
+ return IsSpreadExpression() ||
+ (IsBinaryOperation() && HasRestField::decode(code_));
wingo 2015/08/31 13:07:31 Need more comments about the preconditions, i.e. t
caitp (gmail) 2015/08/31 13:23:40 I'll inline it
caitp (gmail) 2015/08/31 13:28:17 Actually, it can't really be inlined since there's
+ }
+
PreParserExpression AsFunctionLiteral() { return *this; }
bool IsBinaryOperation() const {
@@ -1082,6 +1092,7 @@ class PreParserExpression {
typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseStrongField;
typedef BitField<PreParserIdentifier::Type, TypeField::kNext, 10>
IdentifierTypeField;
+ typedef BitField<bool, TypeField::kNext, 1> HasRestField;
uint32_t code_;
};
@@ -1854,6 +1865,12 @@ void PreParserTraits::ParseArrowFunctionFormalParameterList(
Scanner::Location* duplicate_loc, bool* ok) {
// TODO(wingo): Detect duplicated identifiers in paramlists. Detect parameter
// lists that are too long.
+
+ // Accomodate array literal for rest parameter.
+ if (params.HasRestParameter()) {
+ ++parameters->materialized_literals_count;
+ pre_parser_->function_state_->NextMaterializedLiteralIndex();
+ }
}
@@ -2392,6 +2409,7 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseExpression(
ExpressionClassifier::AllProductions);
bool is_simple_parameter_list = this->IsIdentifier(result);
bool seen_rest = false;
+
while (peek() == Token::COMMA) {
if (seen_rest) {
// At this point the production can't possibly be valid, but we don't know
@@ -2400,6 +2418,8 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseExpression(
scanner()->peek_location(), MessageTemplate::kParamAfterRest);
}
Consume(Token::COMMA);
+ Token::Value next = peek();
+ Scanner::Location next_location = scanner()->peek_location();
bool is_rest = false;
if (allow_harmony_rest_parameters() && peek() == Token::ELLIPSIS) {
// 'x, y, ...z' in CoverParenthesizedExpressionAndArrowParameterList only
@@ -2413,7 +2433,15 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseExpression(
int pos = position();
ExpressionT right = this->ParseAssignmentExpression(
accept_IN, &binding_classifier, CHECK_OK);
- if (is_rest) right = factory()->NewSpread(right, pos);
+ if (is_rest) {
+ if (!this->IsIdentifier(right)) {
+ ReportUnexpectedTokenAt(next_location, next);
+ *ok = false;
+ return this->EmptyExpression();
+ }
+ right = factory()->NewSpread(right, pos);
+ }
+
is_simple_parameter_list =
is_simple_parameter_list && this->IsIdentifier(right);
classifier->Accumulate(binding_classifier,
@@ -2862,16 +2890,19 @@ ParserBase<Traits>::ParseAssignmentExpression(bool accept_IN,
scope->SetHasNonSimpleParameters();
parameters.is_simple = false;
}
- checkpoint.Restore(&parameters.materialized_literals_count);
- scope->set_start_position(lhs_beg_pos);
Scanner::Location duplicate_loc = Scanner::Location::invalid();
this->ParseArrowFunctionFormalParameterList(&parameters, expression, loc,
&duplicate_loc, CHECK_OK);
+
+ checkpoint.Restore(&parameters.materialized_literals_count);
+
+ scope->set_start_position(lhs_beg_pos);
if (duplicate_loc.IsValid()) {
arrow_formals_classifier.RecordDuplicateFormalParameterError(
duplicate_loc);
}
+
expression = this->ParseArrowFunctionLiteral(
parameters, arrow_formals_classifier, CHECK_OK);
return expression;
@@ -3656,8 +3687,9 @@ ParserBase<Traits>::ParseMemberExpressionContinuation(
template <class Traits>
-void ParserBase<Traits>::ParseFormalParameter(
- FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) {
+void ParserBase<Traits>::ParseFormalParameter(FormalParametersT* parameters,
+ ExpressionClassifier* classifier,
+ bool* ok) {
// FormalParameter[Yield,GeneratorParameter] :
// BindingElement[?Yield, ?GeneratorParameter]
bool is_rest = parameters->has_rest;
@@ -3679,6 +3711,12 @@ void ParserBase<Traits>::ParseFormalParameter(
classifier->RecordNonSimpleParameter();
}
+ if (is_rest) {
+ parameters->rest_array_literal_index =
+ function_state_->NextMaterializedLiteralIndex();
+ ++parameters->materialized_literals_count;
+ }
+
ExpressionT initializer = Traits::EmptyExpression();
if (!is_rest && allow_harmony_default_parameters() && Check(Token::ASSIGN)) {
ExpressionClassifier init_classifier;
@@ -3817,6 +3855,11 @@ ParserBase<Traits>::ParseArrowFunctionLiteral(
body = this->NewStatementList(0, zone());
this->SkipLazyFunctionBody(&materialized_literal_count,
&expected_property_count, CHECK_OK);
+
+ if (formal_parameters.materialized_literals_count > 0) {
+ materialized_literal_count +=
+ formal_parameters.materialized_literals_count;
+ }
} else {
body = this->ParseEagerFunctionBody(
this->EmptyIdentifier(), RelocInfo::kNoPosition, formal_parameters,

Powered by Google App Engine
This is Rietveld 408576698