This patch to the Go frontend sets the type of a string index
expression to byte, which is a type alias.  To make this work from the
do_type method, we add "byte" and "rune" to the list of known integer
types, and look them up that way rather than via gogo->lookup_global.
This is for https://golang.org/issue/8745.  Bootstrapped and ran Go
testsuite on x86_64-pc-linux-gnu.  Committed to mainline.

Ian
94f7dfa37366244e0bb12497df3d1527b07f141c
diff --git a/gcc/go/gofrontend/MERGE b/gcc/go/gofrontend/MERGE
index 02083edeece..711353d2550 100644
--- a/gcc/go/gofrontend/MERGE
+++ b/gcc/go/gofrontend/MERGE
@@ -1,4 +1,4 @@
-2184750d74d37580486e90df1284c07fdee91670
+81687fccc568a088fee8f627ddde599e17c648c2
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc
index ebe1b36eb53..6d484d9a339 100644
--- a/gcc/go/gofrontend/expressions.cc
+++ b/gcc/go/gofrontend/expressions.cc
@@ -13468,7 +13468,7 @@ Type*
 String_index_expression::do_type()
 {
   if (this->end_ == NULL)
-    return Type::lookup_integer_type("uint8");
+    return Type::lookup_integer_type("byte");
   else
     return this->string_->type();
 }
@@ -14021,7 +14021,7 @@ Field_reference_expression::do_lower(Gogo* gogo, 
Named_object* function,
 
   Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc);
 
-  Type* byte_type = gogo->lookup_global("byte")->type_value();
+  Type* byte_type = Type::lookup_integer_type("byte");
   Array_type* array_type = Type::make_array_type(byte_type, length_expr);
   array_type->set_is_array_incomparable();
 
diff --git a/gcc/go/gofrontend/gogo.cc b/gcc/go/gofrontend/gogo.cc
index a5e4521469b..e31a038dbd3 100644
--- a/gcc/go/gofrontend/gogo.cc
+++ b/gcc/go/gofrontend/gogo.cc
@@ -117,17 +117,11 @@ Gogo::Gogo(Backend* backend, Linemap* linemap, int, int 
pointer_size)
 
   // "byte" is an alias for "uint8".
   uint8_type->integer_type()->set_is_byte();
-  Named_object* byte_type = Named_object::make_type("byte", NULL, uint8_type,
-                                                   loc);
-  byte_type->type_value()->set_is_alias();
-  this->add_named_type(byte_type->type_value());
+  this->add_named_type(Type::make_integer_type_alias("byte", uint8_type));
 
   // "rune" is an alias for "int32".
   int32_type->integer_type()->set_is_rune();
-  Named_object* rune_type = Named_object::make_type("rune", NULL, int32_type,
-                                                   loc);
-  rune_type->type_value()->set_is_alias();
-  this->add_named_type(rune_type->type_value());
+  this->add_named_type(Type::make_integer_type_alias("rune", int32_type));
 
   this->add_named_type(Type::make_named_bool_type());
 
@@ -765,7 +759,7 @@ Gogo::register_gc_vars(const std::vector<Named_object*>& 
var_gc,
 
   Type* pvt = Type::make_pointer_type(Type::make_void_type());
   Type* uintptr_type = Type::lookup_integer_type("uintptr");
-  Type* byte_type = this->lookup_global("byte")->type_value();
+  Type* byte_type = Type::lookup_integer_type("byte");
   Type* pointer_byte_type = Type::make_pointer_type(byte_type);
   Struct_type* root_type =
     Type::make_builtin_struct_type(4,
diff --git a/gcc/go/gofrontend/statements.cc b/gcc/go/gofrontend/statements.cc
index 25e25364cee..af82f36bc43 100644
--- a/gcc/go/gofrontend/statements.cc
+++ b/gcc/go/gofrontend/statements.cc
@@ -6341,7 +6341,7 @@ For_range_statement::do_lower(Gogo* gogo, Named_object*, 
Block* enclosing,
   else if (range_type->is_string_type())
     {
       index_type = Type::lookup_integer_type("int");
-      value_type = gogo->lookup_global("rune")->type_value();
+      value_type = Type::lookup_integer_type("rune");
     }
   else if (range_type->map_type() != NULL)
     {
@@ -6812,7 +6812,7 @@ For_range_statement::lower_range_string(Gogo* gogo,
     rune_type = value_temp->type();
   else
     {
-      rune_type = gogo->lookup_global("rune")->type_value();
+      rune_type = Type::lookup_integer_type("rune");
       value_temp = Statement::make_temporary(rune_type, NULL, loc);
       init->add_statement(value_temp);
     }
diff --git a/gcc/go/gofrontend/types.cc b/gcc/go/gofrontend/types.cc
index 23d1647c1fa..d2741f6db58 100644
--- a/gcc/go/gofrontend/types.cc
+++ b/gcc/go/gofrontend/types.cc
@@ -2764,7 +2764,7 @@ class Ptrmask
   symname() const;
 
   Expression*
-  constructor(Gogo* gogo) const;
+  constructor() const;
 
  private:
   void
@@ -2959,10 +2959,10 @@ Ptrmask::symname() const
 // initialize the runtime ptrmask value.
 
 Expression*
-Ptrmask::constructor(Gogo* gogo) const
+Ptrmask::constructor() const
 {
   Location bloc = Linemap::predeclared_location();
-  Type* byte_type = gogo->lookup_global("byte")->type_value();
+  Type* byte_type = Type::lookup_integer_type("byte");
   Expression* len = Expression::make_integer_ul(this->bits_.size(), NULL,
                                                bloc);
   Array_type* at = Type::make_array_type(byte_type, len);
@@ -3007,7 +3007,7 @@ Type::gc_ptrmask_var(Gogo* gogo, int64_t ptrsize, int64_t 
ptrdata)
       return ins.first->second;
     }
 
-  Expression* val = ptrmask.constructor(gogo);
+  Expression* val = ptrmask.constructor();
   Translate_context context(gogo, NULL, NULL, NULL);
   context.set_is_const();
   Bexpression* bval = val->get_backend(&context);
@@ -3046,7 +3046,7 @@ class GCProg
   end();
 
   Expression*
-  constructor(Gogo* gogo) const;
+  constructor() const;
 
  private:
   void
@@ -3357,7 +3357,7 @@ GCProg::end()
 // Return an Expression for the bytes in a GC program.
 
 Expression*
-GCProg::constructor(Gogo* gogo) const
+GCProg::constructor() const
 {
   Location bloc = Linemap::predeclared_location();
 
@@ -3367,7 +3367,7 @@ GCProg::constructor(Gogo* gogo) const
 
   Type* uint32_type = Type::lookup_integer_type("uint32");
 
-  Type* byte_type = gogo->lookup_global("byte")->type_value();
+  Type* byte_type = Type::lookup_integer_type("byte");
   Expression* len = Expression::make_integer_ul(this->bytes_.size(), NULL,
                                                bloc);
   Array_type* at = Type::make_array_type(byte_type, len);
@@ -3414,7 +3414,7 @@ Type::gcprog_constructor(Gogo* gogo, int64_t ptrsize, 
int64_t ptrdata)
 
   go_assert(offset >= ptrdata && offset <= type_size);
 
-  return prog.constructor(gogo);
+  return prog.constructor();
 }
 
 // Return a composite literal for the uncommon type information for
@@ -4141,6 +4141,23 @@ Integer_type::create_abstract_character_type()
   return abstract_type;
 }
 
+// Create an alias to an integer type.  This is used for byte and rune.
+
+Named_type*
+Integer_type::create_integer_type_alias(const char* name,
+                                       Named_type* real_type)
+{
+  std::string sname(name);
+  Named_object* no = Named_object::make_type(sname, NULL, real_type,
+                                            Linemap::predeclared_location());
+  Named_type* nt = no->type_value();
+  nt->set_is_alias();
+  std::pair<Named_integer_types::iterator, bool> ins =
+    Integer_type::named_integer_types.insert(std::make_pair(sname, nt));
+  go_assert(ins.second);
+  return nt;
+}
+
 // Integer type compatibility.
 
 bool
@@ -4218,6 +4235,14 @@ Type::make_abstract_character_type()
   return Integer_type::create_abstract_character_type();
 }
 
+// Make an integer type alias.
+
+Named_type*
+Type::make_integer_type_alias(const char* name, Named_type* real_type)
+{
+  return Integer_type::create_integer_type_alias(name, real_type);
+}
+
 // Look up an integer type.
 
 Named_type*
@@ -4466,7 +4491,7 @@ String_type::do_get_backend(Gogo* gogo)
     {
       std::vector<Backend::Btyped_identifier> fields(2);
 
-      Type* b = gogo->lookup_global("byte")->type_value();
+      Type* b = Type::lookup_integer_type("byte");
       Type* pb = Type::make_pointer_type(b);
 
       // We aren't going to get back to this field to finish the
diff --git a/gcc/go/gofrontend/types.h b/gcc/go/gofrontend/types.h
index d0970295d75..b7dd391d321 100644
--- a/gcc/go/gofrontend/types.h
+++ b/gcc/go/gofrontend/types.h
@@ -462,6 +462,10 @@ class Type
   make_integer_type(const char* name, bool is_unsigned, int bits,
                    int runtime_type_kind);
 
+  // Make a named integer type alias.  This is used for byte and rune.
+  static Named_type*
+  make_integer_type_alias(const char* name, Named_type* real_type);
+
   // Look up a named integer type.
   static Named_type*
   lookup_integer_type(const char* name);
@@ -1743,6 +1747,10 @@ class Integer_type : public Type
   static Integer_type*
   create_abstract_character_type();
 
+  // Create an alias to an integer type.
+  static Named_type*
+  create_integer_type_alias(const char* name, Named_type* real_type);
+
   // Whether this is an abstract integer type.
   bool
   is_abstract() const
diff --git a/gcc/go/gofrontend/wb.cc b/gcc/go/gofrontend/wb.cc
index 1eadb3e9e62..ac1f0a1eff9 100644
--- a/gcc/go/gofrontend/wb.cc
+++ b/gcc/go/gofrontend/wb.cc
@@ -683,8 +683,9 @@ Gogo::write_barrier_variable()
       Location bloc = Linemap::predeclared_location();
 
       Type* bool_type = Type::lookup_bool_type();
-      Array_type* pad_type = 
Type::make_array_type(this->lookup_global("byte")->type_value(),
-                                                  
Expression::make_integer_ul(3, NULL, bloc));
+      Array_type* pad_type =
+       Type::make_array_type(Type::lookup_integer_type("byte"),
+                             Expression::make_integer_ul(3, NULL, bloc));
       Type* uint64_type = Type::lookup_integer_type("uint64");
       Type* wb_type = Type::make_builtin_struct_type(5,
                                                     "enabled", bool_type,

Reply via email to