compiler: don't read known type, simplify Import::finalize_methods

With the current export format, if we already know the type, we don't
have to read and parse the definition.

We only use the finalizer in Import::finalize_methods, so make it a
local variable.  To match Finalize_methods::type, only put struct
types into real_for_named.

Change-Id: I9cf0b03917abb1bc09f1a09dfb53ec80c81dd326
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/197700
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/go/import.cc b/go/import.cc
index bbc8d7d..c63ae24 100644
--- a/go/import.cc
+++ b/go/import.cc
@@ -290,16 +290,10 @@
   : gogo_(NULL), stream_(stream), location_(location), package_(NULL),
     add_to_globals_(false), packages_(), type_data_(), type_pos_(0),
     type_offsets_(), builtin_types_((- SMALLEST_BUILTIN_CODE) + 1),
-    types_(), finalizer_(NULL), version_(EXPORT_FORMAT_UNKNOWN)
+    types_(), version_(EXPORT_FORMAT_UNKNOWN)
 {
 }
 
-Import::~Import()
-{
-  if (this->finalizer_ != NULL)
-    delete this->finalizer_;
-}
-
 // Import the data in the associated stream.
 
 Package*
@@ -692,16 +686,22 @@
 void
 Import::finalize_methods()
 {
-  if (this->finalizer_ == NULL)
-    this->finalizer_ = new Finalize_methods(gogo_);
+  Finalize_methods finalizer(this->gogo_);
   Unordered_set(Type*) real_for_named;
   for (size_t i = 1; i < this->types_.size(); i++)
     {
       Type* type = this->types_[i];
       if (type != NULL && type->named_type() != NULL)
         {
-          this->finalizer_->type(type);
-          real_for_named.insert(type->named_type()->real_type());
+          finalizer.type(type);
+
+	  // If the real type is a struct type, we don't want to
+	  // finalize its methods.  For a named type defined as a
+	  // struct type, we only want to finalize the methods of the
+	  // named type.  This is like Finalize_methods::type.
+	  Type* real_type = type->named_type()->real_type();
+	  if (real_type->struct_type() != NULL)
+	    real_for_named.insert(real_type);
         }
     }
   for (size_t i = 1; i < this->types_.size(); i++)
@@ -710,7 +710,7 @@
       if (type != NULL
           && type->named_type() == NULL
           && real_for_named.find(type) == real_for_named.end())
-        this->finalizer_->type(type);
+        finalizer.type(type);
     }
 }
 
@@ -1105,12 +1105,12 @@
     type = this->types_[index];
   else
     {
-      type = this->read_type();
-
       if (no->is_type_declaration())
 	{
 	  // We can define the type now.
 
+	  type = this->read_type();
+
 	  no = package->add_type(type_name, type, this->location_);
 	  Named_type* ntype = no->type_value();
 
@@ -1127,14 +1127,18 @@
 	}
       else if (no->is_type())
 	{
-	  // We have seen this type before.  FIXME: it would be a good
-	  // idea to check that the two imported types are identical,
-	  // but we have not finalized the methods yet, which means
-	  // that we can not reliably compare interface types.
+	  // We have seen this type before.
 	  type = no->type_value();
 
 	  // Don't change the visibility of the existing type.
+
+	  // For older export versions, we need to skip the type
+	  // definition in the stream.
+	  if (this->version_ < EXPORT_FORMAT_V3)
+	    this->read_type();
 	}
+      else
+	go_unreachable();
 
       this->types_[index] = type;
 
diff --git a/go/import.h b/go/import.h
index a78e48b..b12b3b8 100644
--- a/go/import.h
+++ b/go/import.h
@@ -208,8 +208,6 @@
   // Constructor.
   Import(Stream*, Location);
 
-  virtual ~Import();
-
   // Register the builtin types.
   void
   register_builtin_types(Gogo*);
@@ -450,8 +448,6 @@
   std::vector<Named_type*> builtin_types_;
   // Mapping from exported type codes to Type structures.
   std::vector<Type*> types_;
-  // Helper for finalizing methods.
-  Finalize_methods* finalizer_;
   // Version of export data we're reading.
   Export_data_version version_;
 };