This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Go patch committed: The "main" package is not special with -fgo-prefix


The -fgo-prefix option provides a way to disambiguate Go packages with
the same name.  When the option is used with the package named "main",
that package should not be special.  This permits building code which
imports a package normally named "main", in order to write internal
tests.  This patch to the Go frontend implements this.  Bootstrapped and
ran Go testsuite on x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

diff -r 36579ae99a4c go/gogo-tree.cc
--- a/go/gogo-tree.cc	Tue Feb 08 12:52:03 2011 -0800
+++ b/go/gogo-tree.cc	Wed Feb 09 14:24:52 2011 -0800
@@ -161,7 +161,7 @@
   if (this->init_fn_name_.empty())
     {
       gcc_assert(this->package_ != NULL);
-      if (this->package_name() == "main")
+      if (this->is_main_package())
 	{
 	  // Use a name which the runtime knows.
 	  this->init_fn_name_ = "__go_init_main";
@@ -186,7 +186,7 @@
 void
 Gogo::init_imports(tree* init_stmt_list)
 {
-  gcc_assert(this->package_name() == "main");
+  gcc_assert(this->is_main_package());
 
   if (this->imported_init_fns_.empty())
     return;
@@ -384,7 +384,7 @@
 {
   // Make sure that we thought we needed an initialization function,
   // as otherwise we will not have reported it in the export data.
-  gcc_assert(this->package_name() == "main" || this->need_init_fn_);
+  gcc_assert(this->is_main_package() || this->need_init_fn_);
 
   if (fndecl == NULL_TREE)
     fndecl = this->initialization_function_decl();
@@ -648,7 +648,7 @@
   tree init_fndecl = NULL_TREE;
   tree init_stmt_list = NULL_TREE;
 
-  if (this->package_name() == "main")
+  if (this->is_main_package())
     this->init_imports(&init_stmt_list);
 
   // A list of variable initializations.
@@ -804,7 +804,7 @@
   // This will be called if this package is imported.
   if (init_stmt_list != NULL_TREE
       || this->need_init_fn_
-      || this->package_name() == "main")
+      || this->is_main_package())
     this->write_initialization_function(init_fndecl, init_stmt_list);
 
   // Pass everything back to the middle-end.
@@ -1259,7 +1259,7 @@
 		   && !this->type_->is_method())
 	    ;
 	  else if (Gogo::unpack_hidden_name(no->name()) == "main"
-		   && gogo->package_name() == "main")
+		   && gogo->is_main_package())
 	    TREE_PUBLIC(decl) = 1;
 	  // Methods have to be public even if they are hidden because
 	  // they can be pulled into type descriptors when using
diff -r 36579ae99a4c go/gogo.cc
--- a/go/gogo.cc	Tue Feb 08 12:52:03 2011 -0800
+++ b/go/gogo.cc	Wed Feb 09 14:24:52 2011 -0800
@@ -33,6 +33,7 @@
     init_fn_name_(),
     imported_init_fns_(),
     unique_prefix_(),
+    unique_prefix_specified_(false),
     interface_types_()
 {
   const source_location loc = BUILTINS_LOCATION;
@@ -259,7 +260,7 @@
   // package name (e.g., P.x), but we no longer do.
   // this->globals_->add_package(package_name, this->package_);
 
-  if (package_name == "main")
+  if (this->is_main_package())
     {
       // Declare "main" as a function which takes no parameters and
       // returns no value.
@@ -270,6 +271,15 @@
     }
 }
 
+// Return whether this is the "main" package.  This is not true if
+// -fgo-prefix was used.
+
+bool
+Gogo::is_main_package() const
+{
+  return this->package_name() == "main" && !this->unique_prefix_specified_;
+}
+
 // Import a package.
 
 void
@@ -2446,6 +2456,7 @@
 {
   gcc_assert(this->unique_prefix_.empty());
   this->unique_prefix_ = arg;
+  this->unique_prefix_specified_ = true;
 }
 
 // Work out the package priority.  It is one more than the maximum
@@ -2477,7 +2488,7 @@
   exp.export_globals(this->package_name(),
 		     this->unique_prefix(),
 		     this->package_priority(),
-		     (this->need_init_fn_ && this->package_name() != "main"
+		     (this->need_init_fn_ && !this->is_main_package()
 		      ? this->get_init_fn_name()
 		      : ""),
 		     this->imported_init_fns_,
diff -r 36579ae99a4c go/gogo.h
--- a/go/gogo.h	Tue Feb 08 12:52:03 2011 -0800
+++ b/go/gogo.h	Wed Feb 09 14:24:52 2011 -0800
@@ -112,6 +112,10 @@
   void
   set_package_name(const std::string&, source_location);
 
+  // Return whether this is the "main" package.
+  bool
+  is_main_package() const;
+
   // If necessary, adjust the name to use for a hidden symbol.  We add
   // a prefix of the package name, so that hidden symbols in different
   // packages do not collide.
@@ -653,6 +657,8 @@
   std::set<Import_init> imported_init_fns_;
   // The unique prefix used for all global symbols.
   std::string unique_prefix_;
+  // Whether an explicit unique prefix was set by -fgo-prefix.
+  bool unique_prefix_specified_;
   // A list of interface types defined while parsing.
   std::vector<Interface_type*> interface_types_;
 };

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]