compiler: fix undefined symbol error with unexported method

When an interface I1 in an imported package has an unexported method,
and is then embedded into another interface I2, in a different
package, that has other methods, and a type T2 is converted to I2, we
failed to ever define the required interface method table.  Naturally
T2 must implement the unexported method, and must therefore either be
defined in the same package as I1, or embed a type from that package.
In this case the compiler was assuming that that package would define
the interface method table, but of course, since I2 was not defined in
that package, that did not happen.

The fix is to only assume that the interface method table will be
defined elsewhere in the case where T2 and I2 are defined in the same
package.  The compiler ensures that all such interface method tables
are created, in Gogo::build_interface_method_tables.  This requires
knowing the package in which an interface type is defined, a simple
tweak to the importer.

Testing this revealed that the special case for stub methods created
for the embedded unexported methods of T2 needs to be done for
function declarations as it currently is for function definitions, so
that the newly created interface method tables use the correct name.

Testing that revealed that the code to determine the pkgpath symbol
for such stub methods was wrong.  It assumed that one could call
pkgpath_for_symbol on the pkgpath to get the pkgpath symbol.  Would
that it twere so simple.  Instead, add a function to look up the
package, which must be known, and fetch the pkgpath symbol.

The test for this is https://golang.org/cl/45085.

Change-Id: Ib7c47f0758c20393f99c6f0e612536ee08a5aaa2
Reviewed-on: https://go-review.googlesource.com/45086
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/go/expressions.cc b/go/expressions.cc
index a656b06..eaf492f 100644
--- a/go/expressions.cc
+++ b/go/expressions.cc
@@ -15430,27 +15430,32 @@
 			      + "__"
 			      + this->type_->mangled_name(gogo));
 
-  // See whether this interface has any hidden methods.
-  bool has_hidden_methods = false;
-  for (Typed_identifier_list::const_iterator p = interface_methods->begin();
-       p != interface_methods->end();
-       ++p)
+  // Set is_public if we are converting a named type to an interface
+  // type that is defined in the same package as the named type, and
+  // the interface has hidden methods.  In that case the interface
+  // method table will be defined by the package that defines the
+  // types.
+  bool is_public = false;
+  if (this->type_->named_type() != NULL
+      && (this->type_->named_type()->named_object()->package()
+	  == this->itype_->package()))
     {
-      if (Gogo::is_hidden_name(p->name()))
+      for (Typed_identifier_list::const_iterator p = interface_methods->begin();
+	   p != interface_methods->end();
+	   ++p)
 	{
-	  has_hidden_methods = true;
-	  break;
+	  if (Gogo::is_hidden_name(p->name()))
+	    {
+	      is_public = true;
+	      break;
+	    }
 	}
     }
 
-  // We already know that the named type is convertible to the
-  // interface.  If the interface has hidden methods, and the named
-  // type is defined in a different package, then the interface
-  // conversion table will be defined by that other package.
-  if (has_hidden_methods
-      && this->type_->named_type() != NULL
+  if (is_public
       && this->type_->named_type()->named_object()->package() != NULL)
     {
+      // The interface conversion table is defined elsewhere.
       Btype* btype = this->type()->get_backend(gogo);
       std::string asm_name(go_selectively_encode_id(mangled_name));
       this->bvar_ =
@@ -15517,7 +15522,6 @@
   Bexpression* ctor =
       gogo->backend()->constructor_expression(btype, ctor_bexprs, loc);
 
-  bool is_public = has_hidden_methods && this->type_->named_type() != NULL;
   std::string asm_name(go_selectively_encode_id(mangled_name));
   this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false,
 						  !is_public, btype, loc);
diff --git a/go/gogo.cc b/go/gogo.cc
index 26f6441..9071bc8 100644
--- a/go/gogo.cc
+++ b/go/gogo.cc
@@ -1688,6 +1688,16 @@
   return package;
 }
 
+// Return the pkgpath symbol for a package, given the pkgpath.
+
+std::string
+Gogo::pkgpath_symbol_for_package(const std::string& pkgpath)
+{
+  Packages::iterator p = this->packages_.find(pkgpath);
+  go_assert(p != this->packages_.end());
+  return p->second->pkgpath_symbol();
+}
+
 // Start compiling a function.
 
 Named_object*
@@ -5418,8 +5428,8 @@
               // use the pkgpath of the imported package to avoid
               // a possible name collision.  See bug478 for a test
               // case.
-              pkgpath = Gogo::hidden_name_pkgpath(no->name());
-              pkgpath = Gogo::pkgpath_for_symbol(pkgpath);
+	      std::string p = Gogo::hidden_name_pkgpath(no->name());
+	      pkgpath = gogo->pkgpath_symbol_for_package(p);
             }
 
           asm_name = pkgpath;
@@ -5514,8 +5524,19 @@
       if (this->asm_name_.empty())
         {
           asm_name = (no->package() == NULL
-                                  ? gogo->pkgpath_symbol()
-                                  : no->package()->pkgpath_symbol());
+		      ? gogo->pkgpath_symbol()
+		      : no->package()->pkgpath_symbol());
+	  if (this->fntype_->is_method()
+	      && Gogo::is_hidden_name(no->name())
+	      && Gogo::hidden_name_pkgpath(no->name()) != gogo->pkgpath())
+	    {
+	      // This is a method created for an unexported method of
+	      // an imported embedded type.  Use the pkgpath of the
+	      // imported package.  This matches code in
+	      // Function::get_or_make_decl, above.
+	      std::string p = Gogo::hidden_name_pkgpath(no->name());
+	      asm_name = gogo->pkgpath_symbol_for_package(p);
+	    }
           asm_name.append(1, '.');
           asm_name.append(Gogo::unpack_hidden_name(no->name()));
           if (this->fntype_->is_method())
diff --git a/go/gogo.h b/go/gogo.h
index 787a3e3..c3e3f30 100644
--- a/go/gogo.h
+++ b/go/gogo.h
@@ -362,6 +362,10 @@
   register_package(const std::string& pkgpath,
 		   const std::string& pkgpath_symbol, Location);
 
+  // Look up a package by pkgpath, and return its pkgpath_symbol.
+  std::string
+  pkgpath_symbol_for_package(const std::string&);
+
   // Start compiling a function.  ADD_METHOD_TO_TYPE is true if a
   // method function should be added to the type of its receiver.
   Named_object*
diff --git a/go/types.cc b/go/types.cc
index a3ec52c..61a3363 100644
--- a/go/types.cc
+++ b/go/types.cc
@@ -9816,7 +9816,9 @@
       methods = NULL;
     }
 
-  return Type::make_interface_type(methods, imp->location());
+  Interface_type* ret = Type::make_interface_type(methods, imp->location());
+  ret->package_ = imp->package();
+  return ret;
 }
 
 // Make an interface type.
diff --git a/go/types.h b/go/types.h
index 53d6a2c..3f6240b 100644
--- a/go/types.h
+++ b/go/types.h
@@ -2854,9 +2854,9 @@
   Interface_type(Typed_identifier_list* methods, Location location)
     : Type(TYPE_INTERFACE),
       parse_methods_(methods), all_methods_(NULL), location_(location),
-      interface_btype_(NULL), bmethods_(NULL), assume_identical_(NULL),
-      methods_are_finalized_(false), bmethods_is_placeholder_(false),
-      seen_(false)
+      package_(NULL), interface_btype_(NULL), bmethods_(NULL),
+      assume_identical_(NULL), methods_are_finalized_(false),
+      bmethods_is_placeholder_(false), seen_(false)
   { go_assert(methods == NULL || !methods->empty()); }
 
   // The location where the interface type was defined.
@@ -2864,6 +2864,12 @@
   location() const
   { return this->location_; }
 
+  // The package where the interface type was defined.  Returns NULL
+  // for the package currently being compiled.
+  Package*
+  package() const
+  { return this->package_; }
+
   // Return whether this is an empty interface.
   bool
   is_empty() const
@@ -3008,6 +3014,9 @@
   Typed_identifier_list* all_methods_;
   // The location where the interface was defined.
   Location location_;
+  // The package where the interface was defined.  This is NULL for
+  // the package being compiled.
+  Package* package_;
   // The backend representation of this type during backend conversion.
   Btype* interface_btype_;
   // The backend representation of the pointer to the method table.