diff mbox

Go patch committed: Accept "for range v"

Message ID mcr1tqghk69.fsf@iant-glaptop.roam.corp.google.com
State New
Headers show

Commit Message

Ian Lance Taylor Oct. 9, 2014, 11:56 p.m. UTC
The Go language was tweaked slightly to permit the variable to be
omitted in a range clause, so that one can write "for range v" instead
of having to write "for _ = range v".  This patch from Chris Manghane
implements this in gccgo.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian
diff mbox

Patch

diff -r 569e72035892 go/parse.cc
--- a/go/parse.cc	Wed Oct 08 07:02:06 2014 -0700
+++ b/go/parse.cc	Thu Oct 09 16:33:58 2014 -0700
@@ -3836,6 +3836,12 @@ 
       this->unget_token(Token::make_identifier_token(identifier, is_exported,
 						     location));
     }
+  else if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
+    {
+      Typed_identifier_list til;
+      this->range_clause_decl(&til, p_range_clause);
+      return NULL;
+    }
 
   Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
 				     may_be_composite_lit,
@@ -5278,7 +5284,7 @@ 
     }
 }
 
-// RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
+// RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
 
 // This is the := version.  It is called with a list of identifiers.
 
@@ -5291,7 +5297,6 @@ 
 
   p_range_clause->found = true;
 
-  go_assert(til->size() >= 1);
   if (til->size() > 2)
     error_at(this->location(), "too many variables for range clause");
 
@@ -5300,6 +5305,9 @@ 
 				      NULL);
   p_range_clause->range = expr;
 
+  if (til->empty())
+    return;
+
   bool any_new = false;
 
   const Typed_identifier* pti = &til->front();
@@ -5347,6 +5355,9 @@ 
   p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
 					   NULL, NULL);
 
+  if (vals->empty())
+    return;
+
   p_range_clause->index = vals->front();
   if (vals->size() == 1)
     p_range_clause->value = NULL;
diff -r 569e72035892 go/statements.cc
--- a/go/statements.cc	Wed Oct 08 07:02:06 2014 -0700
+++ b/go/statements.cc	Thu Oct 09 16:33:58 2014 -0700
@@ -5305,8 +5305,12 @@ 
 int
 For_range_statement::do_traverse(Traverse* traverse)
 {
-  if (this->traverse_expression(traverse, &this->index_var_) == TRAVERSE_EXIT)
-    return TRAVERSE_EXIT;
+  if (this->index_var_ != NULL)
+    {
+      if (this->traverse_expression(traverse, &this->index_var_)
+	  == TRAVERSE_EXIT)
+	return TRAVERSE_EXIT;
+    }
   if (this->value_var_ != NULL)
     {
       if (this->traverse_expression(traverse, &this->value_var_)
@@ -5434,25 +5438,27 @@ 
   if (iter_init != NULL)
     body->add_statement(Statement::make_block_statement(iter_init, loc));
 
-  Statement* assign;
-  Expression* index_ref = Expression::make_temporary_reference(index_temp, loc);
-  if (this->value_var_ == NULL)
+  if (this->index_var_ != NULL)
     {
-      assign = Statement::make_assignment(this->index_var_, index_ref, loc);
+      Statement* assign;
+      Expression* index_ref =
+	Expression::make_temporary_reference(index_temp, loc);
+      if (this->value_var_ == NULL)
+	assign = Statement::make_assignment(this->index_var_, index_ref, loc);
+      else
+	{
+	  Expression_list* lhs = new Expression_list();
+	  lhs->push_back(this->index_var_);
+	  lhs->push_back(this->value_var_);
+
+	  Expression_list* rhs = new Expression_list();
+	  rhs->push_back(index_ref);
+	  rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
+
+	  assign = Statement::make_tuple_assignment(lhs, rhs, loc);
+	}
+      body->add_statement(assign);
     }
-  else
-    {
-      Expression_list* lhs = new Expression_list();
-      lhs->push_back(this->index_var_);
-      lhs->push_back(this->value_var_);
-
-      Expression_list* rhs = new Expression_list();
-      rhs->push_back(index_ref);
-      rhs->push_back(Expression::make_temporary_reference(value_temp, loc));
-
-      assign = Statement::make_tuple_assignment(lhs, rhs, loc);
-    }
-  body->add_statement(assign);
 
   body->add_statement(Statement::make_block_statement(this->statements_, loc));