Go patch committed: Don't read a known type from import data

Ian Lance Taylor iant@golang.org
Fri Sep 27 17:52:00 GMT 2019


With the current Go export format, if we already know the type, we
don't have to read and parse the definition.  This patch to the Go
frontend implements that.

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

Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu.  Committed
to mainline.

Ian
-------------- next part --------------
Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE	(revision 276187)
+++ gcc/go/gofrontend/MERGE	(working copy)
@@ -1,4 +1,4 @@
-27d1f3031197428b5745d09c167f982d638b8776
+9112ea664ed9ee5f108158a913812adaf03edf6e
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
Index: gcc/go/gofrontend/import.cc
===================================================================
--- gcc/go/gofrontend/import.cc	(revision 275698)
+++ gcc/go/gofrontend/import.cc	(working copy)
@@ -290,16 +290,10 @@ Import::Import(Stream* stream, Location
   : 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 @@ Import::read_types()
 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 @@ Import::finalize_methods()
       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 @@ Import::read_named_type(int index)
     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 @@ Import::read_named_type(int index)
 	}
       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;
 
Index: gcc/go/gofrontend/import.h
===================================================================
--- gcc/go/gofrontend/import.h	(revision 275698)
+++ gcc/go/gofrontend/import.h	(working copy)
@@ -208,8 +208,6 @@ class Import : public Import_expression
   // Constructor.
   Import(Stream*, Location);
 
-  virtual ~Import();
-
   // Register the builtin types.
   void
   register_builtin_types(Gogo*);
@@ -450,8 +448,6 @@ class Import : public Import_expression
   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_;
 };


More information about the Gcc-patches mailing list