Patchwork Go patch committed: Don't crash when declaring method on unknown name

login
register
mail settings
Submitter Ian Taylor
Date Dec. 21, 2010, 8:32 p.m.
Message ID <mcr4oa6n9y0.fsf@google.com>
Download mbox | patch
Permalink /patch/76327/
State New
Headers show

Comments

Ian Taylor - Dec. 21, 2010, 8:32 p.m.
This patch to the Go frontend avoids a compiler crash when declaring a
method on an unknown name.  This may be valid if the name is later
defined as a type.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r 784214e96a81 go/gogo.cc
--- a/go/gogo.cc	Tue Dec 21 10:58:41 2010 -0800
+++ b/go/gogo.cc	Tue Dec 21 11:53:06 2010 -0800
@@ -3710,6 +3710,18 @@ 
   this->u_.func_value = function;
 }
 
+// Declare an unknown object as a type declaration.
+
+void
+Named_object::declare_as_type()
+{
+  gcc_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
+  Unknown_name* unk = this->u_.unknown_value;
+  this->classification_ = NAMED_OBJECT_TYPE_DECLARATION;
+  this->u_.type_declaration = new Type_declaration(unk->location());
+  delete unk;
+}
+
 // Return the location of a named object.
 
 source_location
diff -r 784214e96a81 go/gogo.h
--- a/go/gogo.h	Tue Dec 21 10:58:41 2010 -0800
+++ b/go/gogo.h	Tue Dec 21 11:53:06 2010 -0800
@@ -1815,6 +1815,10 @@ 
   void
   set_function_value(Function*);
 
+  // Declare an unknown name as a type declaration.
+  void
+  declare_as_type();
+
   // Export this object.
   void
   export_named_object(Export*) const;
diff -r 784214e96a81 go/types.cc
--- a/go/types.cc	Tue Dec 21 10:58:41 2010 -0800
+++ b/go/types.cc	Tue Dec 21 11:53:06 2010 -0800
@@ -8013,7 +8013,8 @@ 
 				     Function* function)
 {
   Named_object* no = this->named_object();
-  gcc_assert(no->is_type_declaration());
+  if (no->is_unknown())
+    no->declare_as_type();
   return no->type_declaration_value()->add_method(name, function);
 }
 
@@ -8026,7 +8027,8 @@ 
 						 source_location location)
 {
   Named_object* no = this->named_object();
-  gcc_assert(no->is_type_declaration());
+  if (no->is_unknown())
+    no->declare_as_type();
   Type_declaration* td = no->type_declaration_value();
   return td->add_method_declaration(name, type, location);
 }