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: Handle predeclared names as field names in literals


This patch to the Go frontend fixes the case where a struct composite
literal uses a field name which happens to be a predeclared name.  The
problem was that predeclared names aren't packed as hidden names,
because the package name is not known when they are declared.  The field
names in structs are, however, packed when appropriate.  Bootstrapped
and ran Go testsuite on x86_64-unknown-linux-gnu.  Committed to
mainline.

Ian

diff -r 6b5a5e74bfb4 go/expressions.cc
--- a/go/expressions.cc	Wed Mar 02 22:49:50 2011 -0800
+++ b/go/expressions.cc	Mon Mar 07 13:18:54 2011 -0800
@@ -936,14 +936,6 @@
   return this;
 }
 
-// Return the name of the variable.
-
-const std::string&
-Var_expression::name() const
-{
-  return this->variable_->name();
-}
-
 // Return the type of a reference to a variable.
 
 Type*
@@ -1125,14 +1117,6 @@
 // a function seems like it could work, though there might be little
 // point to it.
 
-// Return the name of the function.
-
-const std::string&
-Func_expression::name() const
-{
-  return this->function_->name();
-}
-
 // Traversal.
 
 int
@@ -2359,10 +2343,6 @@
   named_object()
   { return this->constant_; }
 
-  const std::string&
-  name() const
-  { return this->constant_->name(); }
-
   // Check that the initializer does not refer to the constant itself.
   void
   check_for_init_loop();
@@ -11645,7 +11625,7 @@
 
  private:
   Expression*
-  lower_struct(Type*);
+  lower_struct(Gogo*, Type*);
 
   Expression*
   lower_array(Type*);
@@ -11706,7 +11686,7 @@
   if (type->is_error_type())
     return Expression::make_error(this->location());
   else if (type->struct_type() != NULL)
-    return this->lower_struct(type);
+    return this->lower_struct(gogo, type);
   else if (type->array_type() != NULL)
     return this->lower_array(type);
   else if (type->map_type() != NULL)
@@ -11723,7 +11703,7 @@
 // Lower a struct composite literal.
 
 Expression*
-Composite_literal_expression::lower_struct(Type* type)
+Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
 {
   source_location location = this->location();
   Struct_type* st = type->struct_type();
@@ -11751,6 +11731,7 @@
 
       bool bad_key = false;
       std::string name;
+      const Named_object* no = NULL;
       switch (name_expr->classification())
 	{
 	case EXPRESSION_UNKNOWN_REFERENCE:
@@ -11758,7 +11739,7 @@
 	  break;
 
 	case EXPRESSION_CONST_REFERENCE:
-	  name = static_cast<Const_expression*>(name_expr)->name();
+	  no = static_cast<Const_expression*>(name_expr)->named_object();
 	  break;
 
 	case EXPRESSION_TYPE:
@@ -11768,16 +11749,16 @@
 	    if (nt == NULL)
 	      bad_key = true;
 	    else
-	      name = nt->name();
+	      no = nt->named_object();
 	  }
 	  break;
 
 	case EXPRESSION_VAR_REFERENCE:
-	  name = name_expr->var_expression()->name();
+	  no = name_expr->var_expression()->named_object();
 	  break;
 
 	case EXPRESSION_FUNC_REFERENCE:
-	  name = name_expr->func_expression()->name();
+	  no = name_expr->func_expression()->named_object();
 	  break;
 
 	case EXPRESSION_UNARY:
@@ -11825,6 +11806,23 @@
 	  return Expression::make_error(location);
 	}
 
+      if (no != NULL)
+	{
+	  name = no->name();
+
+	  // A predefined name won't be packed.  If it starts with a
+	  // lower case letter we need to check for that case, because
+	  // the field name will be packed.
+	  if (!Gogo::is_hidden_name(name)
+	      && name[0] >= 'a'
+	      && name[0] <= 'z')
+	    {
+	      Named_object* gno = gogo->lookup_global(name.c_str());
+	      if (gno == no)
+		name = gogo->pack_hidden_name(name, false);
+	    }
+	}
+
       unsigned int index;
       const Struct_field* sf = st->find_local_field(name, &index);
       if (sf == NULL)
diff -r 6b5a5e74bfb4 go/expressions.h
--- a/go/expressions.h	Wed Mar 02 22:49:50 2011 -0800
+++ b/go/expressions.h	Mon Mar 07 13:18:54 2011 -0800
@@ -903,10 +903,6 @@
   named_object() const
   { return this->variable_; }
 
-  // Return the name of the variable.
-  const std::string&
-  name() const;
-
  protected:
   Expression*
   do_lower(Gogo*, Named_object*, int);
@@ -1314,10 +1310,6 @@
   named_object() const
   { return this->function_; }
 
-  // Return the name of the function.
-  const std::string&
-  name() const;
-
   // Return the closure for this function.  This will return NULL if
   // the function has no closure, which is the normal case.
   Expression*

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