Patchwork Go patch committed: Permit _ as a result variable name

login
register
mail settings
Submitter Ian Taylor
Date Dec. 15, 2010, 10:08 p.m.
Message ID <mcripyu1yfl.fsf@google.com>
Download mbox | patch
Permalink /patch/75685/
State New
Headers show

Comments

Ian Taylor - Dec. 15, 2010, 10:08 p.m.
This patch to the Go frontend permits _ as a result variable name.  The
name _ is special in Go: it means that the object has no name and can
not be referenced.  This is still meaningful for a result variable, as
the return statement will still work.  Bootstrapped and ran Go testsuite
on x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r 912813039662 go/gogo.cc
--- a/go/gogo.cc	Wed Dec 15 13:48:02 2010 -0800
+++ b/go/gogo.cc	Wed Dec 15 14:00:37 2010 -0800
@@ -640,7 +640,7 @@ 
 	}
     }
 
-  function->create_named_result_variables();
+  function->create_named_result_variables(this);
 
   const std::string* pname;
   std::string nested_name;
@@ -2473,7 +2473,7 @@ 
 // Create the named result variables.
 
 void
-Function::create_named_result_variables()
+Function::create_named_result_variables(Gogo* gogo)
 {
   const Typed_identifier_list* results = this->type_->results();
   if (results == NULL
@@ -2490,10 +2490,17 @@ 
        p != results->end();
        ++p, ++index)
     {
-      Result_variable* result = new Result_variable(p->type(), this,
-						    index);
-      Named_object* no = block->bindings()->add_result_variable(p->name(),
-								result);
+      std::string name = p->name();
+      if (Gogo::is_sink_name(name))
+	{
+	  static int unnamed_result_counter;
+	  char buf[100];
+	  snprintf(buf, sizeof buf, "_$%d", unnamed_result_counter);
+	  ++unnamed_result_counter;
+	  name = gogo->pack_hidden_name(buf, false);
+	}
+      Result_variable* result = new Result_variable(p->type(), this, index);
+      Named_object* no = block->bindings()->add_result_variable(name, result);
       this->named_results_->push_back(no);
     }
 }
diff -r 912813039662 go/gogo.h
--- a/go/gogo.h	Wed Dec 15 13:48:02 2010 -0800
+++ b/go/gogo.h	Wed Dec 15 14:00:37 2010 -0800
@@ -785,7 +785,7 @@ 
 
   // Create the named result variables in the outer block.
   void
-  create_named_result_variables();
+  create_named_result_variables(Gogo*);
 
   // Add a new field to the closure variable.
   void