compiler: type of string index expression is byte

To make this work from the do_type method, add "byte" and "rune" to
the list of known integer types, and look them up that way rather than
via gogo->lookup_global.

For golang/go#8745

Change-Id: Ie63fb3bc0efc1bad240c3d9585ee94eca5fe71ba
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/275653
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/go/expressions.cc b/go/expressions.cc
index ebe1b36..6d484d9 100644
--- a/go/expressions.cc
+++ b/go/expressions.cc
@@ -13468,7 +13468,7 @@
 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 @@
 
   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/go/gogo.cc b/go/gogo.cc
index a5e4521..e31a038 100644
--- a/go/gogo.cc
+++ b/go/gogo.cc
@@ -117,17 +117,11 @@
 
   // "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 @@
 
   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/go/statements.cc b/go/statements.cc
index 25e2536..af82f36 100644
--- a/go/statements.cc
+++ b/go/statements.cc
@@ -6341,7 +6341,7 @@
   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 @@
     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/go/types.cc b/go/types.cc
index 23d1647..d2741f6 100644
--- a/go/types.cc
+++ b/go/types.cc
@@ -2764,7 +2764,7 @@
   symname() const;
 
   Expression*
-  constructor(Gogo* gogo) const;
+  constructor() const;
 
  private:
   void
@@ -2959,10 +2959,10 @@
 // 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 @@
       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 @@
   end();
 
   Expression*
-  constructor(Gogo* gogo) const;
+  constructor() const;
 
  private:
   void
@@ -3357,7 +3357,7 @@
 // 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 @@
 
   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 @@
 
   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 @@
   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 @@
   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 @@
     {
       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/go/types.h b/go/types.h
index d097029..b7dd391 100644
--- a/go/types.h
+++ b/go/types.h
@@ -462,6 +462,10 @@
   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 @@
   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/go/wb.cc b/go/wb.cc
index 1eadb3e..ac1f0a1 100644
--- a/go/wb.cc
+++ b/go/wb.cc
@@ -683,8 +683,9 @@
       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,