Reviewers: adamk, Dan Ehrenberg,

Description:
[es6] Refactor FormalParameter

Store arity in FormalParameters; store name (instead of var) and is_rest flag in
individual parameters.

This is preparation for more parameter destructuring adjustments. In particular, a follow-up CL will separate parameter recording from declaring the variables.

[email protected], [email protected]
BUG=v8:811
LOG=N

Please review this at https://codereview.chromium.org/1259013003/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+40, -33 lines):
  M src/parser.h
  M src/parser.cc
  M src/preparser.h
  M src/preparser.cc
  M test/cctest/test-parsing.cc


Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index 027880cd0504454cd8f53e1947f89407fbb4671c..73cf136cd2c549b46b28495c78ea18b2e8ecf897 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -917,7 +917,7 @@ Parser::Parser(ParseInfo* info)
   set_allow_harmony_unicode(FLAG_harmony_unicode);
   set_allow_harmony_computed_property_names(
       FLAG_harmony_computed_property_names);
-  set_allow_harmony_rest_params(FLAG_harmony_rest_parameters);
+  set_allow_harmony_rest_parameters(FLAG_harmony_rest_parameters);
   set_allow_harmony_spreadcalls(FLAG_harmony_spreadcalls);
   set_allow_harmony_destructuring(FLAG_harmony_destructuring);
   set_allow_harmony_spread_arrays(FLAG_harmony_spread_arrays);
@@ -3847,7 +3847,7 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
     ParserFormalParameters* parameters, Expression* expr,
     const Scanner::Location& params_loc,
     Scanner::Location* duplicate_loc, bool* ok) {
-  if (parameters->scope->num_parameters() >= Code::kMaxArguments) {
+  if (parameters->arity >= Code::kMaxArguments) {
ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
     *ok = false;
     return;
@@ -3901,6 +3901,7 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
     parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
   }

+  ++parameters->arity;
   ExpressionClassifier classifier;
   DeclareFormalParameter(parameters, expr, is_rest, &classifier);
   if (!duplicate_loc->IsValid()) {
@@ -3989,7 +3990,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
                      : NewScope(scope_, FUNCTION_SCOPE, kind);
   scope->SetLanguageMode(language_mode);
   ZoneList<Statement*>* body = NULL;
-  int arity = 0;
+  int arity = -1;
   int materialized_literal_count = -1;
   int expected_property_count = -1;
   DuplicateFinder duplicate_finder(scanner()->unicode_cache());
@@ -4023,7 +4024,9 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
     int start_position = scanner()->location().beg_pos;
     scope_->set_start_position(start_position);
     ParserFormalParameters formals(scope);
- arity = ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK);
+    ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK);
+    arity = formals.arity;
+    DCHECK(arity == formals.params.length());
     Expect(Token::RPAREN, CHECK_OK);
     int formals_end_position = scanner()->location().end_pos;

@@ -4312,7 +4315,8 @@ Block* Parser::BuildParameterInitializationBlock(
   DCHECK(scope_->is_function_scope());
   Block* init_block =
       factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
-  for (auto parameter : parameters.params) {
+  for (int i = 0; i < parameters.params.length(); ++i) {
+    auto parameter = parameters.params[i];
     if (parameter.pattern == nullptr) continue;
     DeclarationDescriptor descriptor;
     descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
@@ -4327,7 +4331,7 @@ Block* Parser::BuildParameterInitializationBlock(
     descriptor.init_op = Token::INIT_LET;
     DeclarationParsingResult::Declaration decl(
         parameter.pattern, parameter.pattern->position(),
-        factory()->NewVariableProxy(parameter.var));
+        factory()->NewVariableProxy(parameters.scope->parameter(i)));
     PatternRewriter::DeclareAndInitializeVariables(init_block, &descriptor,
&decl, nullptr, CHECK_OK);
   }
@@ -4463,7 +4467,7 @@ PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser(
     SET_ALLOW(harmony_sloppy_let);
     SET_ALLOW(harmony_unicode);
     SET_ALLOW(harmony_computed_property_names);
-    SET_ALLOW(harmony_rest_params);
+    SET_ALLOW(harmony_rest_parameters);
     SET_ALLOW(harmony_spreadcalls);
     SET_ALLOW(harmony_destructuring);
     SET_ALLOW(harmony_spread_arrays);
Index: src/parser.h
diff --git a/src/parser.h b/src/parser.h
index 87269ed619581862cb4038173286a1c4b0d42b28..1974f296f0756b0739ff6970453d1cbad7534a9a 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -541,10 +541,11 @@ class SingletonLogger;

 struct ParserFormalParameters : public PreParserFormalParameters {
   struct Parameter {
-    Parameter(Variable* var, Expression* pattern)
-        : var(var), pattern(pattern) {}
-    Variable* var;
+    Parameter(const AstRawString* name, Expression* pattern, bool is_rest)
+        : name(name), pattern(pattern), is_rest(is_rest) {}
+    const AstRawString* name;
     Expression* pattern;
+    bool is_rest;
   };

   explicit ParserFormalParameters(Scope* scope)
@@ -552,8 +553,10 @@ struct ParserFormalParameters : public PreParserFormalParameters {

   ZoneList<Parameter> params;

-  void AddParameter(Variable* var, Expression* pattern) {
-    params.Add(Parameter(var, pattern), scope->zone());
+  void AddParameter(
+      const AstRawString* name, Expression* pattern, bool is_rest) {
+    params.Add(Parameter(name, pattern, is_rest), scope->zone());
+    DCHECK(arity == params.length());
   }
 };

@@ -579,7 +582,7 @@ class ParserTraits {
     typedef ObjectLiteral::Property* ObjectLiteralProperty;
     typedef ZoneList<v8::internal::Expression*>* ExpressionList;
     typedef ZoneList<ObjectLiteral::Property*>* PropertyList;
-    typedef const v8::internal::AstRawString* FormalParameter;
+    typedef ParserFormalParameters::Parameter FormalParameter;
     typedef ParserFormalParameters FormalParameters;
     typedef ZoneList<v8::internal::Statement*>* StatementList;

@@ -1320,7 +1323,7 @@ void ParserTraits::DeclareFormalParameter(
: parser_->ast_value_factory()->empty_string();
   Variable* var =
parameters->scope->DeclareParameter(name, VAR, is_rest, &is_duplicate);
-  parameters->AddParameter(var, is_simple ? nullptr : pattern);
+  parameters->AddParameter(name, is_simple ? nullptr : pattern, is_rest);
   if (is_duplicate) {
     classifier->RecordDuplicateFormalParameterError(
         parser_->scanner()->location());
Index: src/preparser.cc
diff --git a/src/preparser.cc b/src/preparser.cc
index d60fa5659d8c460f004b62f9b97ec240b2268ab1..ec0ea2de3a04ba42bf5b32a54722420fb33de5d7 100644
--- a/src/preparser.cc
+++ b/src/preparser.cc
@@ -1053,11 +1053,11 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
   int start_position = scanner()->location().beg_pos;
   function_scope->set_start_position(start_position);
   PreParserFormalParameters formals(nullptr);
- int arity = ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK);
+  ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK);
   Expect(Token::RPAREN, CHECK_OK);
   int formals_end_position = scanner()->location().end_pos;

-  CheckArityRestrictions(arity, arity_restriction,
+  CheckArityRestrictions(formals.arity, arity_restriction,
                          formals.has_rest, start_position,
                          formals_end_position, CHECK_OK);

Index: src/preparser.h
diff --git a/src/preparser.h b/src/preparser.h
index 18a0162913d1774a8c1d3077c1e073f7a0a3762b..e0b24328172aa3ec37bc0e6d151d089596490d6c 100644
--- a/src/preparser.h
+++ b/src/preparser.h
@@ -103,7 +103,7 @@ class ParserBase : public Traits {
         allow_harmony_sloppy_(false),
         allow_harmony_sloppy_let_(false),
         allow_harmony_computed_property_names_(false),
-        allow_harmony_rest_params_(false),
+        allow_harmony_rest_parameters_(false),
         allow_harmony_spreadcalls_(false),
         allow_harmony_destructuring_(false),
         allow_harmony_spread_arrays_(false),
@@ -121,7 +121,7 @@ class ParserBase : public Traits {
   ALLOW_ACCESSORS(harmony_sloppy);
   ALLOW_ACCESSORS(harmony_sloppy_let);
   ALLOW_ACCESSORS(harmony_computed_property_names);
-  ALLOW_ACCESSORS(harmony_rest_params);
+  ALLOW_ACCESSORS(harmony_rest_parameters);
   ALLOW_ACCESSORS(harmony_spreadcalls);
   ALLOW_ACCESSORS(harmony_destructuring);
   ALLOW_ACCESSORS(harmony_spread_arrays);
@@ -702,8 +702,8 @@ class ParserBase : public Traits {
   void ParseFormalParameter(bool is_rest,
                             FormalParametersT* parameters,
                             ExpressionClassifier* classifier, bool* ok);
-  int ParseFormalParameterList(FormalParametersT* parameters,
-                               ExpressionClassifier* classifier, bool* ok);
+  void ParseFormalParameterList(FormalParametersT* parameters,
+ ExpressionClassifier* classifier, bool* ok);
   void CheckArityRestrictions(
       int param_count, FunctionLiteral::ArityRestriction arity_restriction,
       bool has_rest, int formals_start_pos, int formals_end_pos, bool* ok);
@@ -801,7 +801,7 @@ class ParserBase : public Traits {
   bool allow_harmony_sloppy_;
   bool allow_harmony_sloppy_let_;
   bool allow_harmony_computed_property_names_;
-  bool allow_harmony_rest_params_;
+  bool allow_harmony_rest_parameters_;
   bool allow_harmony_spreadcalls_;
   bool allow_harmony_destructuring_;
   bool allow_harmony_spread_arrays_;
@@ -1315,10 +1315,12 @@ class PreParserFactory {
 struct PreParserFormalParameters {
   explicit PreParserFormalParameters(Scope* scope)
       : scope(scope),
+        arity(0),
         has_rest(false),
         is_simple(true),
         materialized_literals_count(0) {}
   Scope* scope;
+  int arity;
   bool has_rest;
   bool is_simple;
   int materialized_literals_count;
@@ -2275,7 +2277,7 @@ ParserBase<Traits>::ParsePrimaryExpression(ExpressionClassifier* classifier, result = this->ParseArrowFunctionLiteral(parameters, args_classifier,
                                                  CHECK_OK);
       } else if (allow_harmony_arrow_functions() &&
-                 allow_harmony_rest_params() && Check(Token::ELLIPSIS)) {
+ allow_harmony_rest_parameters() && Check(Token::ELLIPSIS)) {
         // (...x) => y
         Scope* scope =
this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); @@ -2387,7 +2389,7 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseExpression(
     }
     Consume(Token::COMMA);
     bool is_rest = false;
-    if (allow_harmony_rest_params() && peek() == Token::ELLIPSIS) {
+    if (allow_harmony_rest_parameters() && peek() == Token::ELLIPSIS) {
// 'x, y, ...z' in CoverParenthesizedExpressionAndArrowParameterList only // as the formal parameters of'(x, y, ...z) => foo', and is not itself a
       // valid expression or binding pattern.
@@ -3646,12 +3648,13 @@ void ParserBase<Traits>::ParseFormalParameter(
     *ok = false;
     return;
   }
+  ++parameters->arity;
   Traits::DeclareFormalParameter(parameters, pattern, is_rest, classifier);
 }


 template <class Traits>
-int ParserBase<Traits>::ParseFormalParameterList(
+void ParserBase<Traits>::ParseFormalParameterList(
FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) {
   // FormalParameters[Yield,GeneratorParameter] :
   //   [empty]
@@ -3667,29 +3670,26 @@ int ParserBase<Traits>::ParseFormalParameterList(
   //   FormalsList[?Yield, ?GeneratorParameter] ,
   //     FormalParameter[?Yield,?GeneratorParameter]

-  int arity = 0;
+  DCHECK(parameters->arity == 0);

   if (peek() != Token::RPAREN) {
     do {
-      if (++arity > Code::kMaxArguments) {
+      if (parameters->arity > Code::kMaxArguments) {
         ReportMessage(MessageTemplate::kTooManyParameters);
         *ok = false;
-        return -1;
+        return;
       }
-      bool is_rest = allow_harmony_rest_params() && Check(Token::ELLIPSIS);
+ bool is_rest = allow_harmony_rest_parameters() && Check(Token::ELLIPSIS);
       ParseFormalParameter(is_rest, parameters, classifier, ok);
-      if (!*ok) return -1;
+      if (!*ok) return;
     } while (!parameters->has_rest && Check(Token::COMMA));

     if (parameters->has_rest && peek() == Token::COMMA) {
       ReportMessageAt(scanner()->peek_location(),
                       MessageTemplate::kParamAfterRest);
       *ok = false;
-      return -1;
     }
   }
-
-  return arity;
 }


Index: test/cctest/test-parsing.cc
diff --git a/test/cctest/test-parsing.cc b/test/cctest/test-parsing.cc
index 5c78f43dd037db63e2eaff331153d26d36d07dcd..66f7f75efaf6ff9886b5dbd22ce9e89a8d4366bc 100644
--- a/test/cctest/test-parsing.cc
+++ b/test/cctest/test-parsing.cc
@@ -1457,7 +1457,7 @@ void SetParserFlags(i::ParserBase<Traits>* parser,
   parser->set_allow_harmony_modules(flags.Contains(kAllowHarmonyModules));
   parser->set_allow_harmony_arrow_functions(
       flags.Contains(kAllowHarmonyArrowFunctions));
-  parser->set_allow_harmony_rest_params(
+  parser->set_allow_harmony_rest_parameters(
       flags.Contains(kAllowHarmonyRestParameters));
   parser->set_allow_harmony_spreadcalls(
       flags.Contains(kAllowHarmonySpreadCalls));


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to