diff mbox

Go patch committed: Support multiple init functions in a single file

Message ID mcr4o7ey5jp.fsf@google.com
State New
Headers show

Commit Message

Ian Lance Taylor March 7, 2011, 9:38 p.m. UTC
This patch to the Go frontend fixes the case of having multiple init
functions in a single file.  The earlier code, removed in this patch,
handled multiple init functions in a single package, but not in a single
file.  Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline.

Ian
diff mbox

Patch

diff -r eb5c54efea28 go/gogo-tree.cc
--- a/go/gogo-tree.cc	Mon Mar 07 13:29:00 2011 -0800
+++ b/go/gogo-tree.cc	Mon Mar 07 13:32:16 2011 -0800
@@ -839,19 +839,6 @@ 
       // types.
       decl_name = Gogo::unpack_hidden_name(this->name_);
     }
-  else if (this->is_function()
-	   && !this->func_value()->is_method()
-	   && this->package_ == NULL
-	   && Gogo::unpack_hidden_name(this->name_) == "init")
-    {
-      // A single package can have multiple "init" functions, which
-      // means that we need to give them different names.
-      static int init_index;
-      char buf[20];
-      snprintf(buf, sizeof buf, "%d", init_index);
-      ++init_index;
-      decl_name = gogo->package_name() + ".init." + buf;
-    }
   else
     {
       std::string package_name;
diff -r eb5c54efea28 go/gogo.cc
--- a/go/gogo.cc	Mon Mar 07 13:29:00 2011 -0800
+++ b/go/gogo.cc	Mon Mar 07 13:32:16 2011 -0800
@@ -211,12 +211,6 @@ 
   this->globals_->add_function_declaration("imag", NULL, imag_type, loc);
 
   this->define_builtin_function_trees();
-
-  // Declare "init", to ensure that it is not defined with parameters
-  // or return values.
-  this->declare_function("init",
-			 Type::make_function_type(NULL, NULL, NULL, loc),
-			 loc);
 }
 
 // Munge name for use in an error message.
@@ -660,7 +654,24 @@ 
 
   const std::string* pname;
   std::string nested_name;
-  if (!name.empty())
+  bool is_init = false;
+  if (Gogo::unpack_hidden_name(name) == "init" && !type->is_method())
+    {
+      if ((type->parameters() != NULL && !type->parameters()->empty())
+	  || (type->results() != NULL && !type->results()->empty()))
+	error_at(location,
+		 "func init must have no arguments and no return values");
+      // There can be multiple "init" functions, so give them each a
+      // different name.
+      static int init_count;
+      char buf[30];
+      snprintf(buf, sizeof buf, ".$init%d", init_count);
+      ++init_count;
+      nested_name = buf;
+      pname = &nested_name;
+      is_init = true;
+    }
+  else if (!name.empty())
     pname = &name;
   else
     {
@@ -753,7 +764,7 @@ 
   of.function = ret;
   of.blocks.push_back(block);
 
-  if (!type->is_method() && Gogo::unpack_hidden_name(name) == "init")
+  if (is_init)
     {
       this->init_functions_.push_back(ret);
       this->need_init_fn_ = true;