OSDN Git Service

compiler: Fix names for hash/equality functions for unnamed types.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / types.cc
index b030a42..21dac51 100644 (file)
@@ -31,13 +31,13 @@ extern "C"
 #include "statements.h"
 #include "export.h"
 #include "import.h"
+#include "backend.h"
 #include "types.h"
 
 // Class Type.
 
 Type::Type(Type_classification classification)
-  : classification_(classification), tree_(NULL_TREE),
-    type_descriptor_decl_(NULL_TREE)
+  : classification_(classification), btype_(NULL), type_descriptor_var_(NULL)
 {
 }
 
@@ -53,9 +53,9 @@ Type::base()
   switch (this->classification_)
     {
     case TYPE_NAMED:
-      return static_cast<Named_type*>(this)->real_type()->base();
+      return this->named_type()->named_base();
     case TYPE_FORWARD:
-      return static_cast<Forward_declaration_type*>(this)->real_type()->base();
+      return this->forward_declaration_type()->real_type()->base();
     default:
       return this;
     }
@@ -67,13 +67,9 @@ Type::base() const
   switch (this->classification_)
     {
     case TYPE_NAMED:
-      return static_cast<const Named_type*>(this)->real_type()->base();
+      return this->named_type()->named_base();
     case TYPE_FORWARD:
-      {
-       const Forward_declaration_type* ftype =
-         static_cast<const Forward_declaration_type*>(this);
-       return ftype->real_type()->base();
-      }
+      return this->forward_declaration_type()->real_type()->base();
     default:
       return this;
     }
@@ -161,7 +157,7 @@ Type::is_basic_type() const
       return this->base()->is_basic_type();
 
     default:
-      gcc_unreachable();
+      go_unreachable();
     }
 }
 
@@ -192,21 +188,21 @@ Type::is_abstract() const
 Type*
 Type::make_non_abstract_type()
 {
-  gcc_assert(this->is_abstract());
+  go_assert(this->is_abstract());
   switch (this->classification())
     {
     case TYPE_INTEGER:
       return Type::lookup_integer_type("int");
     case TYPE_FLOAT:
-      return Type::lookup_float_type("float");
+      return Type::lookup_float_type("float64");
     case TYPE_COMPLEX:
-      return Type::lookup_complex_type("complex");
+      return Type::lookup_complex_type("complex128");
     case TYPE_STRING:
       return Type::lookup_string_type();
     case TYPE_BOOLEAN:
       return Type::lookup_bool_type();
     default:
-      gcc_unreachable();
+      go_unreachable();
     }
 }
 
@@ -224,7 +220,7 @@ Type::is_error_type() const
     case TYPE_ERROR:
       return true;
     case TYPE_NAMED:
-      return t->named_type()->real_type()->is_error_type();
+      return t->named_type()->is_named_error_type();
     default:
       return false;
     }
@@ -244,7 +240,7 @@ Type::points_to() const
 // Return whether this is an open array type.
 
 bool
-Type::is_open_array_type() const
+Type::is_slice_type() const
 {
   return this->array_type() != NULL && this->array_type()->length() == NULL;
 }
@@ -274,7 +270,7 @@ Type::is_nil_constant_as_type() const
 int
 Type::traverse(Type* type, Traverse* traverse)
 {
-  gcc_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
+  go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
             || (traverse->traverse_mask()
                 & Traverse::traverse_expressions) != 0);
   if (traverse->remember_type(type))
@@ -305,16 +301,19 @@ Type::do_traverse(Traverse*)
   return TRAVERSE_CONTINUE;
 }
 
-// Return whether two types are identical.  If REASON is not NULL,
-// optionally set *REASON to the reason the types are not identical.
+// Return whether two types are identical.  If ERRORS_ARE_IDENTICAL,
+// then return true for all erroneous types; this is used to avoid
+// cascading errors.  If REASON is not NULL, optionally set *REASON to
+// the reason the types are not identical.
 
 bool
-Type::are_identical(const Type* t1, const Type* t2, std::string* reason)
+Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
+                   std::string* reason)
 {
   if (t1 == NULL || t2 == NULL)
     {
-      // Something is wrong.  Return true to avoid cascading errors.
-      return true;
+      // Something is wrong.
+      return errors_are_identical ? true : t1 == t2;
     }
 
   // Skip defined forward declarations.
@@ -324,15 +323,18 @@ Type::are_identical(const Type* t1, const Type* t2, std::string* reason)
   if (t1 == t2)
     return true;
 
-  // An undefined forward declaration is an error, so we return true
-  // to avoid cascading errors.
+  // An undefined forward declaration is an error.
   if (t1->forward_declaration_type() != NULL
       || t2->forward_declaration_type() != NULL)
-    return true;
+    return errors_are_identical;
 
   // Avoid cascading errors with error types.
   if (t1->is_error_type() || t2->is_error_type())
-    return true;
+    {
+      if (errors_are_identical)
+       return true;
+      return t1->is_error_type() && t2->is_error_type();
+    }
 
   // Get a good reason for the sink type.  Note that the sink type on
   // the left hand side of an assignment is handled in are_assignable.
@@ -372,28 +374,40 @@ Type::are_identical(const Type* t1, const Type* t2, std::string* reason)
     case TYPE_FUNCTION:
       return t1->function_type()->is_identical(t2->function_type(),
                                               false,
+                                              errors_are_identical,
                                               reason);
 
     case TYPE_POINTER:
-      return Type::are_identical(t1->points_to(), t2->points_to(), reason);
+      return Type::are_identical(t1->points_to(), t2->points_to(),
+                                errors_are_identical, reason);
 
     case TYPE_STRUCT:
-      return t1->struct_type()->is_identical(t2->struct_type());
+      return t1->struct_type()->is_identical(t2->struct_type(),
+                                            errors_are_identical);
 
     case TYPE_ARRAY:
-      return t1->array_type()->is_identical(t2->array_type());
+      return t1->array_type()->is_identical(t2->array_type(),
+                                           errors_are_identical);
 
     case TYPE_MAP:
-      return t1->map_type()->is_identical(t2->map_type());
+      return t1->map_type()->is_identical(t2->map_type(),
+                                         errors_are_identical);
 
     case TYPE_CHANNEL:
-      return t1->channel_type()->is_identical(t2->channel_type());
+      return t1->channel_type()->is_identical(t2->channel_type(),
+                                             errors_are_identical);
 
     case TYPE_INTERFACE:
-      return t1->interface_type()->is_identical(t2->interface_type());
+      return t1->interface_type()->is_identical(t2->interface_type(),
+                                               errors_are_identical);
+
+    case TYPE_CALL_MULTIPLE_RESULT:
+      if (reason != NULL)
+       *reason = "invalid use of multiple value function call";
+      return false;
 
     default:
-      gcc_unreachable();
+      go_unreachable();
     }
 }
 
@@ -403,7 +417,7 @@ Type::are_identical(const Type* t1, const Type* t2, std::string* reason)
 bool
 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
 {
-  if (Type::are_identical(lhs, rhs, NULL))
+  if (Type::are_identical(lhs, rhs, true, NULL))
     return true;
 
   // A constant of abstract bool type may be mixed with any bool type.
@@ -443,7 +457,7 @@ Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
   if (lhs->is_nil_type()
       && (rhs->points_to() != NULL
          || rhs->interface_type() != NULL
-         || rhs->is_open_array_type()
+         || rhs->is_slice_type()
          || rhs->map_type() != NULL
          || rhs->channel_type() != NULL
          || rhs->function_type() != NULL))
@@ -451,7 +465,7 @@ Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
   if (rhs->is_nil_type()
       && (lhs->points_to() != NULL
          || lhs->interface_type() != NULL
-         || lhs->is_open_array_type()
+         || lhs->is_slice_type()
          || lhs->map_type() != NULL
          || lhs->channel_type() != NULL
          || lhs->function_type() != NULL))
@@ -460,14 +474,135 @@ Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
   return false;
 }
 
+// Return true if a value with type T1 may be compared with a value of
+// type T2.  IS_EQUALITY_OP is true for == or !=, false for <, etc.
+
+bool
+Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
+                                   const Type *t2, std::string *reason)
+{
+  if (t1 != t2
+      && !Type::are_assignable(t1, t2, NULL)
+      && !Type::are_assignable(t2, t1, NULL))
+    {
+      if (reason != NULL)
+       *reason = "incompatible types in binary expression";
+      return false;
+    }
+
+  if (!is_equality_op)
+    {
+      if (t1->integer_type() == NULL
+         && t1->float_type() == NULL
+         && !t1->is_string_type())
+       {
+         if (reason != NULL)
+           *reason = _("invalid comparison of non-ordered type");
+         return false;
+       }
+    }
+  else if (t1->is_slice_type()
+          || t1->map_type() != NULL
+          || t1->function_type() != NULL
+          || t2->is_slice_type()
+          || t2->map_type() != NULL
+          || t2->function_type() != NULL)
+    {
+      if (!t1->is_nil_type() && !t2->is_nil_type())
+       {
+         if (reason != NULL)
+           {
+             if (t1->is_slice_type() || t2->is_slice_type())
+               *reason = _("slice can only be compared to nil");
+             else if (t1->map_type() != NULL || t2->map_type() != NULL)
+               *reason = _("map can only be compared to nil");
+             else
+               *reason = _("func can only be compared to nil");
+
+             // Match 6g error messages.
+             if (t1->interface_type() != NULL || t2->interface_type() != NULL)
+               {
+                 char buf[200];
+                 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
+                          reason->c_str());
+                 *reason = buf;
+               }
+           }
+         return false;
+       }
+    }
+  else
+    {
+      if (!t1->is_boolean_type()
+         && t1->integer_type() == NULL
+         && t1->float_type() == NULL
+         && t1->complex_type() == NULL
+         && !t1->is_string_type()
+         && t1->points_to() == NULL
+         && t1->channel_type() == NULL
+         && t1->interface_type() == NULL
+         && t1->struct_type() == NULL
+         && t1->array_type() == NULL
+         && !t1->is_nil_type())
+       {
+         if (reason != NULL)
+           *reason = _("invalid comparison of non-comparable type");
+         return false;
+       }
+
+      if (t1->named_type() != NULL)
+       return t1->named_type()->named_type_is_comparable(reason);
+      else if (t2->named_type() != NULL)
+       return t2->named_type()->named_type_is_comparable(reason);
+      else if (t1->struct_type() != NULL)
+       {
+         const Struct_field_list* fields = t1->struct_type()->fields();
+         for (Struct_field_list::const_iterator p = fields->begin();
+              p != fields->end();
+              ++p)
+           {
+             if (!p->type()->is_comparable())
+               {
+                 if (reason != NULL)
+                   *reason = _("invalid comparison of non-comparable struct");
+                 return false;
+               }
+           }
+       }
+      else if (t1->array_type() != NULL)
+       {
+         if (!t1->array_type()->element_type()->is_comparable())
+           {
+             if (reason != NULL)
+               *reason = _("invalid comparison of non-comparable array");
+             return false;
+           }
+       }
+    }
+
+  return true;
+}
+
 // Return true if a value with type RHS may be assigned to a variable
-// with type LHS.  If REASON is not NULL, set *REASON to the reason
-// the types are not assignable.
+// with type LHS.  If CHECK_HIDDEN_FIELDS is true, check whether any
+// hidden fields are modified.  If REASON is not NULL, set *REASON to
+// the reason the types are not assignable.
 
 bool
-Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
+Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
+                                 bool check_hidden_fields,
+                                 std::string* reason)
 {
   // Do some checks first.  Make sure the types are defined.
+  if (rhs != NULL
+      && rhs->forwarded()->forward_declaration_type() == NULL
+      && rhs->is_void_type())
+    {
+      if (reason != NULL)
+       *reason = "non-value used as value";
+      return false;
+    }
+
   if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
     {
       // Any value may be assigned to the blank identifier.
@@ -476,7 +611,9 @@ Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
 
       // All fields of a struct must be exported, or the assignment
       // must be in the same package.
-      if (rhs != NULL && rhs->forwarded()->forward_declaration_type() == NULL)
+      if (check_hidden_fields
+         && rhs != NULL
+         && rhs->forwarded()->forward_declaration_type() == NULL)
        {
          if (lhs->has_hidden_fields(NULL, reason)
              || rhs->has_hidden_fields(NULL, reason))
@@ -485,14 +622,14 @@ Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
     }
 
   // Identical types are assignable.
-  if (Type::are_identical(lhs, rhs, reason))
+  if (Type::are_identical(lhs, rhs, true, reason))
     return true;
 
   // The types are assignable if they have identical underlying types
   // and either LHS or RHS is not a named type.
   if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
        || (rhs->named_type() != NULL && lhs->named_type() == NULL))
-      && Type::are_identical(lhs->base(), rhs->base(), reason))
+      && Type::are_identical(lhs->base(), rhs->base(), true, reason))
     return true;
 
   // The types are assignable if LHS is an interface type and RHS
@@ -519,6 +656,7 @@ Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
       && (lhs->named_type() == NULL || rhs->named_type() == NULL)
       && Type::are_identical(lhs->channel_type()->element_type(),
                             rhs->channel_type()->element_type(),
+                            true,
                             reason))
     return true;
 
@@ -527,21 +665,23 @@ Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
   if (rhs->is_nil_type()
       && (lhs->points_to() != NULL
          || lhs->function_type() != NULL
-         || lhs->is_open_array_type()
+         || lhs->is_slice_type()
          || lhs->map_type() != NULL
          || lhs->channel_type() != NULL
          || lhs->interface_type() != NULL))
     return true;
 
-  // An untyped constant may be assigned to a numeric type if it is
-  // representable in that type.
-  if (rhs->is_abstract()
+  // An untyped numeric constant may be assigned to a numeric type if
+  // it is representable in that type.
+  if ((rhs->is_abstract()
+       && (rhs->integer_type() != NULL
+          || rhs->float_type() != NULL
+          || rhs->complex_type() != NULL))
       && (lhs->integer_type() != NULL
          || lhs->float_type() != NULL
          || lhs->complex_type() != NULL))
     return true;
 
-
   // Give some better error messages.
   if (reason != NULL && reason->empty())
     {
@@ -567,6 +707,25 @@ Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
   return false;
 }
 
+// Return true if a value with type RHS may be assigned to a variable
+// with type LHS.  If REASON is not NULL, set *REASON to the reason
+// the types are not assignable.
+
+bool
+Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
+{
+  return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
+}
+
+// Like are_assignable but don't check for hidden fields.
+
+bool
+Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
+                              std::string* reason)
+{
+  return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
+}
+
 // Return true if a value with type RHS may be converted to type LHS.
 // If REASON is not NULL, set *REASON to the reason the types are not
 // convertible.
@@ -581,7 +740,7 @@ Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
   // The types are convertible if they have identical underlying
   // types.
   if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
-      && Type::are_identical(lhs->base(), rhs->base(), reason))
+      && Type::are_identical(lhs->base(), rhs->base(), true, reason))
     return true;
 
   // The types are convertible if they are both unnamed pointer types
@@ -594,6 +753,7 @@ Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
          || rhs->points_to()->named_type() != NULL)
       && Type::are_identical(lhs->points_to()->base(),
                             rhs->points_to()->base(),
+                            true,
                             reason))
     return true;
 
@@ -611,7 +771,7 @@ Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
     {
       if (rhs->integer_type() != NULL)
        return true;
-      if (rhs->is_open_array_type() && rhs->named_type() == NULL)
+      if (rhs->is_slice_type())
        {
          const Type* e = rhs->array_type()->element_type()->forwarded();
          if (e->integer_type() != NULL
@@ -622,9 +782,7 @@ Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
     }
 
   // A string may be converted to []byte or []int.
-  if (rhs->is_string_type()
-      && lhs->is_open_array_type()
-      && lhs->named_type() == NULL)
+  if (rhs->is_string_type() && lhs->is_slice_type())
     {
       const Type* e = lhs->array_type()->element_type()->forwarded();
       if (e->integer_type() != NULL
@@ -717,178 +875,264 @@ Type::hash_string(const std::string& s, unsigned int h)
   return h;
 }
 
-// Default check for the expression passed to make.  Any type which
-// may be used with make implements its own version of this.
+// A hash table mapping unnamed types to the backend representation of
+// those types.
 
-bool
-Type::do_check_make_expression(Expression_list*, source_location)
-{
-  gcc_unreachable();
-}
+Type::Type_btypes Type::type_btypes;
 
-// Return whether an expression has an integer value.  Report an error
-// if not.  This is used when handling calls to the predeclared make
-// function.
+// Return a tree representing this type.
 
-bool
-Type::check_int_value(Expression* e, const char* errmsg,
-                     source_location location)
+Btype*
+Type::get_backend(Gogo* gogo)
 {
-  if (e->type()->integer_type() != NULL)
-    return true;
+  if (this->btype_ != NULL)
+    return this->btype_;
 
-  // Check for a floating point constant with integer value.
-  mpfr_t fval;
-  mpfr_init(fval);
-
-  Type* dummy;
-  if (e->float_constant_value(fval, &dummy))
-    {
-      mpz_t ival;
-      mpz_init(ival);
+  if (this->forward_declaration_type() != NULL
+      || this->named_type() != NULL)
+    return this->get_btype_without_hash(gogo);
 
-      bool ok = false;
+  if (this->is_error_type())
+    return gogo->backend()->error_type();
 
-      mpfr_clear_overflow();
-      mpfr_clear_erangeflag();
-      mpfr_get_z(ival, fval, GMP_RNDN);
-      if (!mpfr_overflow_p()
-         && !mpfr_erangeflag_p()
-         && mpz_sgn(ival) >= 0)
-       {
-         Named_type* ntype = Type::lookup_integer_type("int");
-         Integer_type* inttype = ntype->integer_type();
-         mpz_t max;
-         mpz_init_set_ui(max, 1);
-         mpz_mul_2exp(max, max, inttype->bits() - 1);
-         ok = mpz_cmp(ival, max) < 0;
-         mpz_clear(max);
-       }
-      mpz_clear(ival);
+  // To avoid confusing the backend, translate all identical Go types
+  // to the same backend representation.  We use a hash table to do
+  // that.  There is no need to use the hash table for named types, as
+  // named types are only identical to themselves.
 
-      if (ok)
-       {
-         mpfr_clear(fval);
-         return true;
-       }
+  std::pair<Type*, Btype*> val(this, NULL);
+  std::pair<Type_btypes::iterator, bool> ins =
+    Type::type_btypes.insert(val);
+  if (!ins.second && ins.first->second != NULL)
+    {
+      if (gogo != NULL && gogo->named_types_are_converted())
+       this->btype_ = ins.first->second;
+      return ins.first->second;
     }
 
-  mpfr_clear(fval);
-
-  error_at(location, "%s", errmsg);
-  return false;
-}
+  Btype* bt = this->get_btype_without_hash(gogo);
 
-// A hash table mapping unnamed types to trees.
+  if (ins.first->second == NULL)
+    ins.first->second = bt;
+  else
+    {
+      // We have already created a backend representation for this
+      // type.  This can happen when an unnamed type is defined using
+      // a named type which in turns uses an identical unnamed type.
+      // Use the tree we created earlier and ignore the one we just
+      // built.
+      bt = ins.first->second;
+      if (gogo == NULL || !gogo->named_types_are_converted())
+       return bt;
+      this->btype_ = bt;
+    }
 
-Type::Type_trees Type::type_trees;
+  return bt;
+}
 
-// Return a tree representing this type.
+// Return the backend representation for a type without looking in the
+// hash table for identical types.  This is used for named types,
+// since a named type is never identical to any other type.
 
-tree
-Type::get_tree(Gogo* gogo)
+Btype*
+Type::get_btype_without_hash(Gogo* gogo)
 {
-  if (this->tree_ != NULL)
-    return this->tree_;
+  if (this->btype_ == NULL)
+    {
+      Btype* bt = this->do_get_backend(gogo);
 
-  if (this->forward_declaration_type() != NULL
-      || this->named_type() != NULL)
-    return this->get_tree_without_hash(gogo);
+      // For a recursive function or pointer type, we will temporarily
+      // return a circular pointer type during the recursion.  We
+      // don't want to record that for a forwarding type, as it may
+      // confuse us later.
+      if (this->forward_declaration_type() != NULL
+         && gogo->backend()->is_circular_pointer_type(bt))
+       return bt;
 
-  // To avoid confusing GIMPLE, we need to translate all identical Go
-  // types to the same GIMPLE type.  We use a hash table to do that.
-  // There is no need to use the hash table for named types, as named
-  // types are only identical to themselves.
+      if (gogo == NULL || !gogo->named_types_are_converted())
+       return bt;
 
-  std::pair<Type*, tree> val(this, NULL);
-  std::pair<Type_trees::iterator, bool> ins =
-    Type::type_trees.insert(val);
-  if (!ins.second && ins.first->second != NULL_TREE)
-    {
-      this->tree_ = ins.first->second;
-      return this->tree_;
+      this->btype_ = bt;
     }
+  return this->btype_;
+}
 
-  tree t = this->get_tree_without_hash(gogo);
+// Return a pointer to the type descriptor for this type.
 
-  if (ins.first->second == NULL_TREE)
-    ins.first->second = t;
-  else
+tree
+Type::type_descriptor_pointer(Gogo* gogo, Location location)
+{
+  Type* t = this->forwarded();
+  if (t->type_descriptor_var_ == NULL)
     {
-      // We have already created a tree for this type.  This can
-      // happen when an unnamed type is defined using a named type
-      // which in turns uses an identical unnamed type.  Use the tree
-      // we created earlier and ignore the one we just built.
-      t = ins.first->second;
-      this->tree_ = t;
+      t->make_type_descriptor_var(gogo);
+      go_assert(t->type_descriptor_var_ != NULL);
     }
-
-  return t;
+  tree var_tree = var_to_tree(t->type_descriptor_var_);
+  if (var_tree == error_mark_node)
+    return error_mark_node;
+  return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
 }
 
-// Return a tree for a type without looking in the hash table for
-// identical types.  This is used for named types, since there is no
-// point to looking in the hash table for them.
+// A mapping from unnamed types to type descriptor variables.
 
-tree
-Type::get_tree_without_hash(Gogo* gogo)
+Type::Type_descriptor_vars Type::type_descriptor_vars;
+
+// Build the type descriptor for this type.
+
+void
+Type::make_type_descriptor_var(Gogo* gogo)
 {
-  if (this->tree_ == NULL_TREE)
+  go_assert(this->type_descriptor_var_ == NULL);
+
+  Named_type* nt = this->named_type();
+
+  // We can have multiple instances of unnamed types, but we only want
+  // to emit the type descriptor once.  We use a hash table.  This is
+  // not necessary for named types, as they are unique, and we store
+  // the type descriptor in the type itself.
+  Bvariable** phash = NULL;
+  if (nt == NULL)
     {
-      tree t = this->do_get_tree(gogo);
+      Bvariable* bvnull = NULL;
+      std::pair<Type_descriptor_vars::iterator, bool> ins =
+       Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
+      if (!ins.second)
+       {
+         // We've already build a type descriptor for this type.
+         this->type_descriptor_var_ = ins.first->second;
+         return;
+       }
+      phash = &ins.first->second;
+    }
 
-      // For a recursive function or pointer type, we will temporarily
-      // return ptr_type_node during the recursion.  We don't want to
-      // record that for a forwarding type, as it may confuse us
-      // later.
-      if (t == ptr_type_node && this->forward_declaration_type() != NULL)
-       return t;
+  std::string var_name = this->type_descriptor_var_name(gogo, nt);
+
+  // Build the contents of the type descriptor.
+  Expression* initializer = this->do_type_descriptor(gogo, NULL);
+
+  Btype* initializer_btype = initializer->type()->get_backend(gogo);
+
+  Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
 
-      this->tree_ = t;
-      go_preserve_from_gc(t);
+  const Package* dummy;
+  if (this->type_descriptor_defined_elsewhere(nt, &dummy))
+    {
+      this->type_descriptor_var_ =
+       gogo->backend()->immutable_struct_reference(var_name,
+                                                   initializer_btype,
+                                                   loc);
+      if (phash != NULL)
+       *phash = this->type_descriptor_var_;
+      return;
     }
 
-  return this->tree_;
-}
+  // See if this type descriptor can appear in multiple packages.
+  bool is_common = false;
+  if (nt != NULL)
+    {
+      // We create the descriptor for a builtin type whenever we need
+      // it.
+      is_common = nt->is_builtin();
+    }
+  else
+    {
+      // This is an unnamed type.  The descriptor could be defined in
+      // any package where it is needed, and the linker will pick one
+      // descriptor to keep.
+      is_common = true;
+    }
 
-// Return a tree representing a zero initialization for this type.
+  // We are going to build the type descriptor in this package.  We
+  // must create the variable before we convert the initializer to the
+  // backend representation, because the initializer may refer to the
+  // type descriptor of this type.  By setting type_descriptor_var_ we
+  // ensure that type_descriptor_pointer will work if called while
+  // converting INITIALIZER.
 
-tree
-Type::get_init_tree(Gogo* gogo, bool is_clear)
-{
-  tree type_tree = this->get_tree(gogo);
-  if (type_tree == error_mark_node)
-    return error_mark_node;
-  return this->do_get_init_tree(gogo, type_tree, is_clear);
+  this->type_descriptor_var_ =
+    gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
+                                     loc);
+  if (phash != NULL)
+    *phash = this->type_descriptor_var_;
+
+  Translate_context context(gogo, NULL, NULL, NULL);
+  context.set_is_const();
+  Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
+
+  gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
+                                            var_name, is_common,
+                                            initializer_btype, loc,
+                                            binitializer);
 }
 
-// Any type which supports the builtin make function must implement
-// this.
+// Return the name of the type descriptor variable.  If NT is not
+// NULL, use it to get the name.  Otherwise this is an unnamed type.
 
-tree
-Type::do_make_expression_tree(Translate_context*, Expression_list*,
-                             source_location)
+std::string
+Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
 {
-  gcc_unreachable();
+  if (nt == NULL)
+    return "__go_td_" + this->mangled_name(gogo);
+
+  Named_object* no = nt->named_object();
+  const Named_object* in_function = nt->in_function();
+  std::string ret = "__go_tdn_";
+  if (nt->is_builtin())
+    go_assert(in_function == NULL);
+  else
+    {
+      const std::string& unique_prefix(no->package() == NULL
+                                      ? gogo->unique_prefix()
+                                      : no->package()->unique_prefix());
+      const std::string& package_name(no->package() == NULL
+                                     ? gogo->package_name()
+                                     : no->package()->name());
+      ret.append(unique_prefix);
+      ret.append(1, '.');
+      ret.append(package_name);
+      ret.append(1, '.');
+      if (in_function != NULL)
+       {
+         ret.append(Gogo::unpack_hidden_name(in_function->name()));
+         ret.append(1, '.');
+       }
+    }
+  ret.append(no->name());
+  return ret;
 }
 
-// Return a pointer to the type descriptor for this type.
+// Return true if this type descriptor is defined in a different
+// package.  If this returns true it sets *PACKAGE to the package.
 
-tree
-Type::type_descriptor_pointer(Gogo* gogo)
+bool
+Type::type_descriptor_defined_elsewhere(Named_type* nt,
+                                       const Package** package)
 {
-  Type* t = this->forwarded();
-  if (t->type_descriptor_decl_ == NULL_TREE)
+  if (nt != NULL)
+    {
+      if (nt->named_object()->package() != NULL)
+       {
+         // This is a named type defined in a different package.  The
+         // type descriptor should be defined in that package.
+         *package = nt->named_object()->package();
+         return true;
+       }
+    }
+  else
     {
-      Expression* e = t->do_type_descriptor(gogo, NULL);
-      gogo->build_type_descriptor_decl(t, e, &t->type_descriptor_decl_);
-      gcc_assert(t->type_descriptor_decl_ != NULL_TREE
-                && (t->type_descriptor_decl_ == error_mark_node
-                    || DECL_P(t->type_descriptor_decl_)));
+      if (this->points_to() != NULL
+         && this->points_to()->named_type() != NULL
+         && this->points_to()->named_type()->named_object()->package() != NULL)
+       {
+         // This is an unnamed pointer to a named type defined in a
+         // different package.  The descriptor should be defined in
+         // that package.
+         *package = this->points_to()->named_type()->named_object()->package();
+         return true;
+       }
     }
-  if (t->type_descriptor_decl_ == error_mark_node)
-    return error_mark_node;
-  return build_fold_addr_expr(t->type_descriptor_decl_);
+  return false;
 }
 
 // Return a composite literal for a type descriptor.
@@ -904,7 +1148,7 @@ Type::type_descriptor(Gogo* gogo, Type* type)
 Expression*
 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
 {
-  gcc_assert(name != NULL && type->named_type() != name);
+  go_assert(name != NULL && type->named_type() != name);
   return type->do_type_descriptor(gogo, name);
 }
 
@@ -917,7 +1161,7 @@ Type::make_builtin_struct_type(int nfields, ...)
   va_list ap;
   va_start(ap, nfields);
 
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
   Struct_field_list* sfl = new Struct_field_list();
   for (int i = 0; i < nfields; i++)
     {
@@ -931,14 +1175,36 @@ Type::make_builtin_struct_type(int nfields, ...)
   return Type::make_struct_type(sfl, bloc);
 }
 
+// A list of builtin named types.
+
+std::vector<Named_type*> Type::named_builtin_types;
+
 // Make a builtin named type.
 
 Named_type*
 Type::make_builtin_named_type(const char* name, Type* type)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
   Named_object* no = Named_object::make_type(name, NULL, type, bloc);
-  return no->type_value();
+  Named_type* ret = no->type_value();
+  Type::named_builtin_types.push_back(ret);
+  return ret;
+}
+
+// Convert the named builtin types.
+
+void
+Type::convert_builtin_named_types(Gogo* gogo)
+{
+  for (std::vector<Named_type*>::const_iterator p =
+        Type::named_builtin_types.begin();
+       p != Type::named_builtin_types.end();
+       ++p)
+    {
+      bool r = (*p)->verify();
+      go_assert(r);
+      (*p)->convert(gogo);
+    }
 }
 
 // Return the type of a type descriptor.  We should really tie this to
@@ -951,7 +1217,7 @@ Type::make_type_descriptor_type()
   static Type* ret;
   if (ret == NULL)
     {
-      source_location bloc = BUILTINS_LOCATION;
+      Location bloc = Linemap::predeclared_location();
 
       Type* uint8_type = Type::lookup_integer_type("uint8");
       Type* uint32_type = Type::lookup_integer_type("uint32");
@@ -1000,8 +1266,8 @@ Type::make_type_descriptor_type()
       // The type descriptor type.
 
       Typed_identifier_list* params = new Typed_identifier_list();
-      params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
-      params->push_back(Typed_identifier("", uintptr_type, bloc));
+      params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
+      params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
 
       Typed_identifier_list* results = new Typed_identifier_list();
       results->push_back(Typed_identifier("", uintptr_type, bloc));
@@ -1009,9 +1275,9 @@ Type::make_type_descriptor_type()
       Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
 
       params = new Typed_identifier_list();
-      params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
-      params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
-      params->push_back(Typed_identifier("", uintptr_type, bloc));
+      params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
+      params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
+      params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
 
       results = new Typed_identifier_list();
       results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
@@ -1020,7 +1286,7 @@ Type::make_type_descriptor_type()
                                                    bloc);
 
       Struct_type* type_descriptor_type =
-       Type::make_builtin_struct_type(9,
+       Type::make_builtin_struct_type(10,
                                       "Kind", uint8_type,
                                       "align", uint8_type,
                                       "fieldAlign", uint8_type,
@@ -1029,7 +1295,9 @@ Type::make_type_descriptor_type()
                                       "hashfn", hashfn_type,
                                       "equalfn", equalfn_type,
                                       "string", pointer_string_type,
-                                      "", pointer_uncommon_type);
+                                      "", pointer_uncommon_type,
+                                      "ptrToThis",
+                                      pointer_type_descriptor_type);
 
       Named_type* named = Type::make_builtin_named_type("commonType",
                                                        type_descriptor_type);
@@ -1054,69 +1322,290 @@ Type::make_type_descriptor_ptr_type()
   return ret;
 }
 
-// Return the names of runtime functions which compute a hash code for
-// this type and which compare whether two values of this type are
-// equal.
+// Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
+// hash code for this type and which compare whether two values of
+// this type are equal.  If NAME is not NULL it is the name of this
+// type.  HASH_FNTYPE and EQUAL_FNTYPE are the types of these
+// functions, for convenience; they may be NULL.
 
 void
-Type::type_functions(const char** hash_fn, const char** equal_fn) const
+Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
+                    Function_type* equal_fntype, Named_object** hash_fn,
+                    Named_object** equal_fn)
 {
-  switch (this->base()->classification())
+  if (hash_fntype == NULL || equal_fntype == NULL)
     {
-    case Type::TYPE_ERROR:
-    case Type::TYPE_VOID:
-    case Type::TYPE_NIL:
-      // These types can not be hashed or compared.
-      *hash_fn = "__go_type_hash_error";
-      *equal_fn = "__go_type_equal_error";
-      break;
+      Location bloc = Linemap::predeclared_location();
 
-    case Type::TYPE_BOOLEAN:
-    case Type::TYPE_INTEGER:
-    case Type::TYPE_FLOAT:
-    case Type::TYPE_COMPLEX:
-    case Type::TYPE_POINTER:
-    case Type::TYPE_FUNCTION:
-    case Type::TYPE_MAP:
-    case Type::TYPE_CHANNEL:
-      *hash_fn = "__go_type_hash_identity";
-      *equal_fn = "__go_type_equal_identity";
-      break;
+      Type* uintptr_type = Type::lookup_integer_type("uintptr");
+      Type* void_type = Type::make_void_type();
+      Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
 
-    case Type::TYPE_STRING:
-      *hash_fn = "__go_type_hash_string";
-      *equal_fn = "__go_type_equal_string";
-      break;
+      if (hash_fntype == NULL)
+       {
+         Typed_identifier_list* params = new Typed_identifier_list();
+         params->push_back(Typed_identifier("key", unsafe_pointer_type,
+                                            bloc));
+         params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
 
-    case Type::TYPE_STRUCT:
-    case Type::TYPE_ARRAY:
-      // These types can not be hashed or compared.
-      *hash_fn = "__go_type_hash_error";
-      *equal_fn = "__go_type_equal_error";
-      break;
+         Typed_identifier_list* results = new Typed_identifier_list();
+         results->push_back(Typed_identifier("", uintptr_type, bloc));
 
-    case Type::TYPE_INTERFACE:
-      if (this->interface_type()->is_empty())
+         hash_fntype = Type::make_function_type(NULL, params, results, bloc);
+       }
+      if (equal_fntype == NULL)
        {
-         *hash_fn = "__go_type_hash_empty_interface";
-         *equal_fn = "__go_type_equal_empty_interface";
+         Typed_identifier_list* params = new Typed_identifier_list();
+         params->push_back(Typed_identifier("key1", unsafe_pointer_type,
+                                            bloc));
+         params->push_back(Typed_identifier("key2", unsafe_pointer_type,
+                                            bloc));
+         params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
+
+         Typed_identifier_list* results = new Typed_identifier_list();
+         results->push_back(Typed_identifier("", Type::lookup_bool_type(),
+                                             bloc));
+
+         equal_fntype = Type::make_function_type(NULL, params, results, bloc);
        }
-      else
+    }
+
+  const char* hash_fnname;
+  const char* equal_fnname;
+  if (this->compare_is_identity())
+    {
+      hash_fnname = "__go_type_hash_identity";
+      equal_fnname = "__go_type_equal_identity";
+    }
+  else if (!this->is_comparable())
+    {
+      hash_fnname = "__go_type_hash_error";
+      equal_fnname = "__go_type_equal_error";
+    }
+  else
+    {
+      switch (this->base()->classification())
        {
-         *hash_fn = "__go_type_hash_interface";
-         *equal_fn = "__go_type_equal_interface";
+       case Type::TYPE_ERROR:
+       case Type::TYPE_VOID:
+       case Type::TYPE_NIL:
+       case Type::TYPE_FUNCTION:
+       case Type::TYPE_MAP:
+         // For these types is_comparable should have returned false.
+         go_unreachable();
+
+       case Type::TYPE_BOOLEAN:
+       case Type::TYPE_INTEGER:
+       case Type::TYPE_POINTER:
+       case Type::TYPE_CHANNEL:
+         // For these types compare_is_identity should have returned true.
+         go_unreachable();
+
+       case Type::TYPE_FLOAT:
+         hash_fnname = "__go_type_hash_float";
+         equal_fnname = "__go_type_equal_float";
+         break;
+
+       case Type::TYPE_COMPLEX:
+         hash_fnname = "__go_type_hash_complex";
+         equal_fnname = "__go_type_equal_complex";
+         break;
+
+       case Type::TYPE_STRING:
+         hash_fnname = "__go_type_hash_string";
+         equal_fnname = "__go_type_equal_string";
+         break;
+
+       case Type::TYPE_STRUCT:
+         {
+           // This is a struct which can not be compared using a
+           // simple identity function.  We need to build a function
+           // for comparison.
+           this->specific_type_functions(gogo, name, hash_fntype,
+                                         equal_fntype, hash_fn, equal_fn);
+           return;
+         }
+
+       case Type::TYPE_ARRAY:
+         if (this->is_slice_type())
+           {
+             // Type::is_compatible_for_comparison should have
+             // returned false.
+             go_unreachable();
+           }
+         else
+           {
+             // This is an array which can not be compared using a
+             // simple identity function.  We need to build a
+             // function for comparison.
+             this->specific_type_functions(gogo, name, hash_fntype,
+                                           equal_fntype, hash_fn, equal_fn);
+             return;
+           }
+         break;
+
+       case Type::TYPE_INTERFACE:
+         if (this->interface_type()->is_empty())
+           {
+             hash_fnname = "__go_type_hash_empty_interface";
+             equal_fnname = "__go_type_equal_empty_interface";
+           }
+         else
+           {
+             hash_fnname = "__go_type_hash_interface";
+             equal_fnname = "__go_type_equal_interface";
+           }
+         break;
+
+       case Type::TYPE_NAMED:
+       case Type::TYPE_FORWARD:
+         go_unreachable();
+
+       default:
+         go_unreachable();
        }
-      break;
+    }
 
-    case Type::TYPE_NAMED:
-    case Type::TYPE_FORWARD:
-      gcc_unreachable();
 
-    default:
-      gcc_unreachable();
+  Location bloc = Linemap::predeclared_location();
+  *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
+                                                    hash_fntype, bloc);
+  (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
+  *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
+                                                     equal_fntype, bloc);
+  (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
+}
+
+// A hash table mapping types to the specific hash functions.
+
+Type::Type_functions Type::type_functions_table;
+
+// Handle a type function which is specific to a type: a struct or
+// array which can not use an identity comparison.
+
+void
+Type::specific_type_functions(Gogo* gogo, Named_type* name,
+                             Function_type* hash_fntype,
+                             Function_type* equal_fntype,
+                             Named_object** hash_fn,
+                             Named_object** equal_fn)
+{
+  Hash_equal_fn fnull(NULL, NULL);
+  std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
+  std::pair<Type_functions::iterator, bool> ins =
+    Type::type_functions_table.insert(val);
+  if (!ins.second)
+    {
+      // We already have functions for this type
+      *hash_fn = ins.first->second.first;
+      *equal_fn = ins.first->second.second;
+      return;
+    }
+
+  std::string base_name;
+  if (name == NULL)
+    {
+      // Mangled names can have '.' if they happen to refer to named
+      // types in some way.  That's fine if this is simply a named
+      // type, but otherwise it will confuse the code that builds
+      // function identifiers.  Remove '.' when necessary.
+      base_name = this->mangled_name(gogo);
+      size_t i;
+      while ((i = base_name.find('.')) != std::string::npos)
+       base_name[i] = '$';
+      base_name = gogo->pack_hidden_name(base_name, false);
+    }
+  else
+    {
+      // This name is already hidden or not as appropriate.
+      base_name = name->name();
+      const Named_object* in_function = name->in_function();
+      if (in_function != NULL)
+       base_name += '$' + in_function->name();
+    }
+  std::string hash_name = base_name + "$hash";
+  std::string equal_name = base_name + "$equal";
+
+  Location bloc = Linemap::predeclared_location();
+
+  const Package* package = NULL;
+  bool is_defined_elsewhere =
+    this->type_descriptor_defined_elsewhere(name, &package);
+  if (is_defined_elsewhere)
+    {
+      *hash_fn = Named_object::make_function_declaration(hash_name, package,
+                                                        hash_fntype, bloc);
+      *equal_fn = Named_object::make_function_declaration(equal_name, package,
+                                                         equal_fntype, bloc);
+    }
+  else
+    {
+      *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
+      *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
+                                                bloc);
+    }
+
+  ins.first->second.first = *hash_fn;
+  ins.first->second.second = *equal_fn;
+
+  if (!is_defined_elsewhere)
+    {
+      if (gogo->in_global_scope())
+       this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
+                                           equal_name, equal_fntype);
+      else
+       gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
+                                          equal_name, equal_fntype);
     }
 }
 
+// Write the hash and equality functions for a type which needs to be
+// written specially.
+
+void
+Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
+                                   const std::string& hash_name,
+                                   Function_type* hash_fntype,
+                                   const std::string& equal_name,
+                                   Function_type* equal_fntype)
+{
+  Location bloc = Linemap::predeclared_location();
+
+  Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
+                                              bloc);
+  gogo->start_block(bloc);
+
+  if (this->struct_type() != NULL)
+    this->struct_type()->write_hash_function(gogo, name, hash_fntype,
+                                            equal_fntype);
+  else if (this->array_type() != NULL)
+    this->array_type()->write_hash_function(gogo, name, hash_fntype,
+                                           equal_fntype);
+  else
+    go_unreachable();
+
+  Block* b = gogo->finish_block(bloc);
+  gogo->add_block(b, bloc);
+  gogo->lower_block(hash_fn, b);
+  gogo->finish_function(bloc);
+
+  Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
+                                               false, bloc);
+  gogo->start_block(bloc);
+
+  if (this->struct_type() != NULL)
+    this->struct_type()->write_equal_function(gogo, name);
+  else if (this->array_type() != NULL)
+    this->array_type()->write_equal_function(gogo, name);
+  else
+    go_unreachable();
+
+  b = gogo->finish_block(bloc);
+  gogo->add_block(b, bloc);
+  gogo->lower_block(equal_fn, b);
+  gogo->finish_function(bloc);
+}
+
 // Return a composite literal for the type descriptor for a plain type
 // of kind RUNTIME_TYPE_KIND named NAME.
 
@@ -1125,7 +1614,7 @@ Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
                                  Named_type* name, const Methods* methods,
                                  bool only_value_methods)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* td_type = Type::make_type_descriptor_type();
   const Struct_field_list* fields = td_type->struct_type()->fields();
@@ -1133,54 +1622,51 @@ Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
   Expression_list* vals = new Expression_list();
   vals->reserve(9);
 
+  if (!this->has_pointer())
+    runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "Kind");
+  go_assert(p->is_field_name("Kind"));
   mpz_t iv;
   mpz_init_set_ui(iv, runtime_type_kind);
   vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "align");
+  go_assert(p->is_field_name("align"));
   Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
   vals->push_back(Expression::make_type_info(this, type_info));
 
   ++p;
-  gcc_assert(p->field_name() == "fieldAlign");
+  go_assert(p->is_field_name("fieldAlign"));
   type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
   vals->push_back(Expression::make_type_info(this, type_info));
 
   ++p;
-  gcc_assert(p->field_name() == "size");
+  go_assert(p->is_field_name("size"));
   type_info = Expression::TYPE_INFO_SIZE;
   vals->push_back(Expression::make_type_info(this, type_info));
 
   ++p;
-  gcc_assert(p->field_name() == "hash");
+  go_assert(p->is_field_name("hash"));
   mpz_set_ui(iv, this->hash_for_method(gogo));
   vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
 
-  const char* hash_fn;
-  const char* equal_fn;
-  this->type_functions(&hash_fn, &equal_fn);
-
   ++p;
-  gcc_assert(p->field_name() == "hashfn");
-  Function_type* fntype = p->type()->function_type();
-  Named_object* no = Named_object::make_function_declaration(hash_fn, NULL,
-                                                            fntype,
-                                                            bloc);
-  no->func_declaration_value()->set_asm_name(hash_fn);
-  vals->push_back(Expression::make_func_reference(no, NULL, bloc));
+  go_assert(p->is_field_name("hashfn"));
+  Function_type* hash_fntype = p->type()->function_type();
 
   ++p;
-  gcc_assert(p->field_name() == "equalfn");
-  fntype = p->type()->function_type();
-  no = Named_object::make_function_declaration(equal_fn, NULL, fntype, bloc);
-  no->func_declaration_value()->set_asm_name(equal_fn);
-  vals->push_back(Expression::make_func_reference(no, NULL, bloc));
+  go_assert(p->is_field_name("equalfn"));
+  Function_type* equal_fntype = p->type()->function_type();
+
+  Named_object* hash_fn;
+  Named_object* equal_fn;
+  this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
+                      &equal_fn);
+  vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
+  vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "string");
+  go_assert(p->is_field_name("string"));
   Expression* s = Expression::make_string((name != NULL
                                           ? name->reflection(gogo)
                                           : this->reflection(gogo)),
@@ -1188,7 +1674,7 @@ Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
   vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "uncommonType");
+  go_assert(p->is_field_name("uncommonType"));
   if (name == NULL && methods == NULL)
     vals->push_back(Expression::make_nil(bloc));
   else
@@ -1202,7 +1688,17 @@ Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
     }
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p->is_field_name("ptrToThis"));
+  if (name == NULL)
+    vals->push_back(Expression::make_nil(bloc));
+  else
+    {
+      Type* pt = Type::make_pointer_type(name);
+      vals->push_back(Expression::make_type_descriptor(pt, bloc));
+    }
+
+  ++p;
+  go_assert(p == fields->end());
 
   mpz_clear(iv);
 
@@ -1221,7 +1717,7 @@ Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
                                Named_type* name, const Methods* methods,
                                bool only_value_methods) const
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   const Struct_field_list* fields = uncommon_type->struct_type()->fields();
 
@@ -1229,10 +1725,10 @@ Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
   vals->reserve(3);
 
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "name");
+  go_assert(p->is_field_name("name"));
 
   ++p;
-  gcc_assert(p->field_name() == "pkgPath");
+  go_assert(p->is_field_name("pkgPath"));
 
   if (name == NULL)
     {
@@ -1271,12 +1767,12 @@ Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
     }
 
   ++p;
-  gcc_assert(p->field_name() == "methods");
+  go_assert(p->is_field_name("methods"));
   vals->push_back(this->methods_constructor(gogo, p->type(), methods,
                                            only_value_methods));
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p == fields->end());
 
   Expression* r = Expression::make_struct_composite_literal(uncommon_type,
                                                            vals, bloc);
@@ -1304,7 +1800,7 @@ Type::methods_constructor(Gogo* gogo, Type* methods_type,
                          const Methods* methods,
                          bool only_value_methods) const
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   std::vector<std::pair<std::string, const Method*> > smethods;
   if (methods != NULL)
@@ -1336,7 +1832,7 @@ Type::methods_constructor(Gogo* gogo, Type* methods_type,
        p != smethods.end();
        ++p)
     vals->push_back(this->method_constructor(gogo, method_type, p->first,
-                                            p->second));
+                                            p->second, only_value_methods));
 
   return Expression::make_slice_composite_literal(methods_type, vals, bloc);
 }
@@ -1348,9 +1844,10 @@ Type::methods_constructor(Gogo* gogo, Type* methods_type,
 Expression*
 Type::method_constructor(Gogo*, Type* method_type,
                         const std::string& method_name,
-                        const Method* m) const
+                        const Method* m,
+                        bool only_value_methods) const
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   const Struct_field_list* fields = method_type->struct_type()->fields();
 
@@ -1358,13 +1855,13 @@ Type::method_constructor(Gogo*, Type* method_type,
   vals->reserve(5);
 
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "name");
+  go_assert(p->is_field_name("name"));
   const std::string n = Gogo::unpack_hidden_name(method_name);
   Expression* s = Expression::make_string(n, bloc);
   vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "pkgPath");
+  go_assert(p->is_field_name("pkgPath"));
   if (!Gogo::is_hidden_name(method_name))
     vals->push_back(Expression::make_nil(bloc));
   else
@@ -1382,23 +1879,42 @@ Type::method_constructor(Gogo*, Type* method_type,
     mtype = no->func_value()->type();
   else
     mtype = no->func_declaration_value()->type();
-  gcc_assert(mtype->is_method());
+  go_assert(mtype->is_method());
   Type* nonmethod_type = mtype->copy_without_receiver();
 
   ++p;
-  gcc_assert(p->field_name() == "mtyp");
+  go_assert(p->is_field_name("mtyp"));
   vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "typ");
+  go_assert(p->is_field_name("typ"));
+  if (!only_value_methods && m->is_value_method())
+    {
+      // This is a value method on a pointer type.  Change the type of
+      // the method to use a pointer receiver.  The implementation
+      // always uses a pointer receiver anyhow.
+      Type* rtype = mtype->receiver()->type();
+      Type* prtype = Type::make_pointer_type(rtype);
+      Typed_identifier* receiver =
+       new Typed_identifier(mtype->receiver()->name(), prtype,
+                            mtype->receiver()->location());
+      mtype = Type::make_function_type(receiver,
+                                      (mtype->parameters() == NULL
+                                       ? NULL
+                                       : mtype->parameters()->copy()),
+                                      (mtype->results() == NULL
+                                       ? NULL
+                                       : mtype->results()->copy()),
+                                      mtype->location());
+    }
   vals->push_back(Expression::make_type_descriptor(mtype, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "tfn");
+  go_assert(p->is_field_name("tfn"));
   vals->push_back(Expression::make_func_reference(no, NULL, bloc));
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p == fields->end());
 
   return Expression::make_struct_composite_literal(method_type, vals, bloc);
 }
@@ -1444,12 +1960,132 @@ Type::mangled_name(Gogo* gogo) const
   return ret;
 }
 
+// Return whether the backend size of the type is known.
+
+bool
+Type::is_backend_type_size_known(Gogo* gogo) const
+{
+  switch (this->classification_)
+    {
+    case TYPE_ERROR:
+    case TYPE_VOID:
+    case TYPE_BOOLEAN:
+    case TYPE_INTEGER:
+    case TYPE_FLOAT:
+    case TYPE_COMPLEX:
+    case TYPE_STRING:
+    case TYPE_FUNCTION:
+    case TYPE_POINTER:
+    case TYPE_NIL:
+    case TYPE_MAP:
+    case TYPE_CHANNEL:
+    case TYPE_INTERFACE:
+      return true;
+
+    case TYPE_STRUCT:
+      {
+       const Struct_field_list* fields = this->struct_type()->fields();
+       for (Struct_field_list::const_iterator pf = fields->begin();
+            pf != fields->end();
+            ++pf)
+         if (!pf->type()->is_backend_type_size_known(gogo))
+           return false;
+       return true;
+      }
+
+    case TYPE_ARRAY:
+      {
+       const Array_type* at = this->array_type();
+       if (at->length() == NULL)
+         return true;
+       else
+         {
+           mpz_t ival;
+           mpz_init(ival);
+           Type* dummy;
+           bool length_known = at->length()->integer_constant_value(true,
+                                                                    ival,
+                                                                    &dummy);
+           mpz_clear(ival);
+           if (!length_known)
+             return false;
+           return at->element_type()->is_backend_type_size_known(gogo);
+         }
+      }
+
+    case TYPE_NAMED:
+      return this->named_type()->is_named_backend_type_size_known();
+
+    case TYPE_FORWARD:
+      {
+       const Forward_declaration_type* fdt = this->forward_declaration_type();
+       return fdt->real_type()->is_backend_type_size_known(gogo);
+      }
+
+    case TYPE_SINK:
+    case TYPE_CALL_MULTIPLE_RESULT:
+      go_unreachable();
+
+    default:
+      go_unreachable();
+    }
+}
+
+// If the size of the type can be determined, set *PSIZE to the size
+// in bytes and return true.  Otherwise, return false.  This queries
+// the backend.
+
+bool
+Type::backend_type_size(Gogo* gogo, unsigned int *psize)
+{
+  Btype* btype = this->get_backend(gogo);
+  if (!this->is_backend_type_size_known(gogo))
+    return false;
+  size_t size = gogo->backend()->type_size(btype);
+  *psize = static_cast<unsigned int>(size);
+  if (*psize != size)
+    return false;
+  return true;
+}
+
+// If the alignment of the type can be determined, set *PALIGN to
+// the alignment in bytes and return true.  Otherwise, return false.
+
+bool
+Type::backend_type_align(Gogo* gogo, unsigned int *palign)
+{
+  Btype* btype = this->get_backend(gogo);
+  if (!this->is_backend_type_size_known(gogo))
+    return false;
+  size_t align = gogo->backend()->type_alignment(btype);
+  *palign = static_cast<unsigned int>(align);
+  if (*palign != align)
+    return false;
+  return true;
+}
+
+// Like backend_type_align, but return the alignment when used as a
+// field.
+
+bool
+Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
+{
+  Btype* btype = this->get_backend(gogo);
+  if (!this->is_backend_type_size_known(gogo))
+    return false;
+  size_t a = gogo->backend()->type_field_alignment(btype);
+  *palign = static_cast<unsigned int>(a);
+  if (*palign != a)
+    return false;
+  return true;
+}
+
 // Default function to export a type.
 
 void
 Type::do_export(Export*) const
 {
-  gcc_unreachable();
+  go_unreachable();
 }
 
 // Import a type.
@@ -1489,21 +2125,21 @@ class Error_type : public Type
   { }
 
  protected:
-  tree
-  do_get_tree(Gogo*)
-  { return error_mark_node; }
+  bool
+  do_compare_is_identity() const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo*, tree, bool)
-  { return error_mark_node; }
+  Btype*
+  do_get_backend(Gogo* gogo)
+  { return gogo->backend()->error_type(); }
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*)
-  { return Expression::make_error(BUILTINS_LOCATION); }
+  { return Expression::make_error(Linemap::predeclared_location()); }
 
   void
   do_reflection(Gogo*, std::string*) const
-  { gcc_assert(saw_errors()); }
+  { go_assert(saw_errors()); }
 
   void
   do_mangled_name(Gogo*, std::string* ret) const
@@ -1527,17 +2163,17 @@ class Void_type : public Type
   { }
 
  protected:
-  tree
-  do_get_tree(Gogo*)
-  { return void_type_node; }
+  bool
+  do_compare_is_identity() const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo*, tree, bool)
-  { gcc_unreachable(); }
+  Btype*
+  do_get_backend(Gogo* gogo)
+  { return gogo->backend()->void_type(); }
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*)
-  { gcc_unreachable(); }
+  { go_unreachable(); }
 
   void
   do_reflection(Gogo*, std::string*) const
@@ -1565,13 +2201,13 @@ class Boolean_type : public Type
   { }
 
  protected:
-  tree
-  do_get_tree(Gogo*)
-  { return boolean_type_node; }
+  bool
+  do_compare_is_identity() const
+  { return true; }
 
-  tree
-  do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
-  { return is_clear ? NULL : fold_convert(type_tree, boolean_false_node); }
+  Btype*
+  do_get_backend(Gogo* gogo)
+  { return gogo->backend()->bool_type(); }
 
   Expression*
   do_type_descriptor(Gogo*, Named_type* name);
@@ -1596,7 +2232,7 @@ Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   else
     {
       Named_object* no = gogo->lookup_global("bool");
-      gcc_assert(no != NULL);
+      go_assert(no != NULL);
       return Type::type_descriptor(gogo, no->type_value());
     }
 }
@@ -1626,9 +2262,9 @@ Named_type*
 Type::make_named_bool_type()
 {
   Type* bool_type = Type::make_boolean_type();
-  Named_object* named_object = Named_object::make_type("bool", NULL,
-                                                      bool_type,
-                                                      BUILTINS_LOCATION);
+  Named_object* named_object =
+    Named_object::make_type("bool", NULL, bool_type,
+                            Linemap::predeclared_location());
   Named_type* named_type = named_object->type_value();
   named_bool_type = named_type;
   return named_type;
@@ -1648,13 +2284,13 @@ Integer_type::create_integer_type(const char* name, bool is_unsigned,
   Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
                                                runtime_type_kind);
   std::string sname(name);
-  Named_object* named_object = Named_object::make_type(sname, NULL,
-                                                      integer_type,
-                                                      BUILTINS_LOCATION);
+  Named_object* named_object =
+    Named_object::make_type(sname, NULL, integer_type,
+                            Linemap::predeclared_location());
   Named_type* named_type = named_object->type_value();
   std::pair<Named_integer_types::iterator, bool> ins =
     Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
-  gcc_assert(ins.second);
+  go_assert(ins.second);
   return named_type;
 }
 
@@ -1665,7 +2301,7 @@ Integer_type::lookup_integer_type(const char* name)
 {
   Named_integer_types::const_iterator p =
     Integer_type::named_integer_types.find(name);
-  gcc_assert(p != Integer_type::named_integer_types.end());
+  go_assert(p != Integer_type::named_integer_types.end());
   return p->second;
 }
 
@@ -1701,48 +2337,17 @@ Integer_type::do_hash_for_method(Gogo*) const
          + ((this->is_abstract_ ? 1 : 0) << 9));
 }
 
-// Get the tree for an Integer_type.
+// Convert an Integer_type to the backend representation.
 
-tree
-Integer_type::do_get_tree(Gogo*)
-{
-  gcc_assert(!this->is_abstract_);
-  if (this->is_unsigned_)
-    {
-      if (this->bits_ == INT_TYPE_SIZE)
-       return unsigned_type_node;
-      else if (this->bits_ == CHAR_TYPE_SIZE)
-       return unsigned_char_type_node;
-      else if (this->bits_ == SHORT_TYPE_SIZE)
-       return short_unsigned_type_node;
-      else if (this->bits_ == LONG_TYPE_SIZE)
-       return long_unsigned_type_node;
-      else if (this->bits_ == LONG_LONG_TYPE_SIZE)
-       return long_long_unsigned_type_node;
-      else
-       return make_unsigned_type(this->bits_);
-    }
-  else
+Btype*
+Integer_type::do_get_backend(Gogo* gogo)
+{
+  if (this->is_abstract_)
     {
-      if (this->bits_ == INT_TYPE_SIZE)
-       return integer_type_node;
-      else if (this->bits_ == CHAR_TYPE_SIZE)
-       return signed_char_type_node;
-      else if (this->bits_ == SHORT_TYPE_SIZE)
-       return short_integer_type_node;
-      else if (this->bits_ == LONG_TYPE_SIZE)
-       return long_integer_type_node;
-      else if (this->bits_ == LONG_LONG_TYPE_SIZE)
-       return long_long_integer_type_node;
-      else
-       return make_signed_type(this->bits_);
+      go_assert(saw_errors());
+      return gogo->backend()->error_type();
     }
-}
-
-tree
-Integer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
-{
-  return is_clear ? NULL : build_int_cst(type_tree, 0);
+  return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
 }
 
 // The type descriptor for an integer type.  Integer types are always
@@ -1751,7 +2356,7 @@ Integer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
 Expression*
 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  gcc_assert(name != NULL);
+  go_assert(name != NULL);
   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
 }
 
@@ -1760,7 +2365,7 @@ Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 void
 Integer_type::do_reflection(Gogo*, std::string*) const
 {
-  gcc_unreachable();
+  go_assert(saw_errors());
 }
 
 // Mangled name.
@@ -1815,12 +2420,13 @@ Float_type::create_float_type(const char* name, int bits,
 {
   Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
   std::string sname(name);
-  Named_object* named_object = Named_object::make_type(sname, NULL, float_type,
-                                                      BUILTINS_LOCATION);
+  Named_object* named_object =
+    Named_object::make_type(sname, NULL, float_type,
+                            Linemap::predeclared_location());
   Named_type* named_type = named_object->type_value();
   std::pair<Named_float_types::iterator, bool> ins =
     Float_type::named_float_types.insert(std::make_pair(sname, named_type));
-  gcc_assert(ins.second);
+  go_assert(ins.second);
   return named_type;
 }
 
@@ -1831,7 +2437,7 @@ Float_type::lookup_float_type(const char* name)
 {
   Named_float_types::const_iterator p =
     Float_type::named_float_types.find(name);
-  gcc_assert(p != Float_type::named_float_types.end());
+  go_assert(p != Float_type::named_float_types.end());
   return p->second;
 }
 
@@ -1842,8 +2448,7 @@ Float_type::create_abstract_float_type()
 {
   static Float_type* abstract_type;
   if (abstract_type == NULL)
-    abstract_type = new Float_type(true, FLOAT_TYPE_SIZE,
-                                  RUNTIME_TYPE_KIND_FLOAT);
+    abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
   return abstract_type;
 }
 
@@ -1865,42 +2470,12 @@ Float_type::do_hash_for_method(Gogo*) const
   return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
 }
 
-// Get a tree without using a Gogo*.
-
-tree
-Float_type::type_tree() const
-{
-  if (this->bits_ == FLOAT_TYPE_SIZE)
-    return float_type_node;
-  else if (this->bits_ == DOUBLE_TYPE_SIZE)
-    return double_type_node;
-  else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE)
-    return long_double_type_node;
-  else
-    {
-      tree ret = make_node(REAL_TYPE);
-      TYPE_PRECISION(ret) = this->bits_;
-      layout_type(ret);
-      return ret;
-    }
-}
-
-// Get a tree.
+// Convert to the backend representation.
 
-tree
-Float_type::do_get_tree(Gogo*)
-{
-  return this->type_tree();
-}
-
-tree
-Float_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
+Btype*
+Float_type::do_get_backend(Gogo* gogo)
 {
-  if (is_clear)
-    return NULL;
-  REAL_VALUE_TYPE r;
-  real_from_integer(&r, TYPE_MODE(type_tree), 0, 0, 0);
-  return build_real(type_tree, r);
+  return gogo->backend()->float_type(this->bits_);
 }
 
 // The type descriptor for a float type.  Float types are always named.
@@ -1908,7 +2483,7 @@ Float_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
 Expression*
 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  gcc_assert(name != NULL);
+  go_assert(name != NULL);
   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
 }
 
@@ -1917,7 +2492,7 @@ Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 void
 Float_type::do_reflection(Gogo*, std::string*) const
 {
-  gcc_unreachable();
+  go_assert(saw_errors());
 }
 
 // Mangled name.
@@ -1970,14 +2545,14 @@ Complex_type::create_complex_type(const char* name, int bits,
   Complex_type* complex_type = new Complex_type(false, bits,
                                                runtime_type_kind);
   std::string sname(name);
-  Named_object* named_object = Named_object::make_type(sname, NULL,
-                                                      complex_type,
-                                                      BUILTINS_LOCATION);
+  Named_object* named_object =
+    Named_object::make_type(sname, NULL, complex_type,
+                            Linemap::predeclared_location());
   Named_type* named_type = named_object->type_value();
   std::pair<Named_complex_types::iterator, bool> ins =
     Complex_type::named_complex_types.insert(std::make_pair(sname,
                                                            named_type));
-  gcc_assert(ins.second);
+  go_assert(ins.second);
   return named_type;
 }
 
@@ -1988,7 +2563,7 @@ Complex_type::lookup_complex_type(const char* name)
 {
   Named_complex_types::const_iterator p =
     Complex_type::named_complex_types.find(name);
-  gcc_assert(p != Complex_type::named_complex_types.end());
+  go_assert(p != Complex_type::named_complex_types.end());
   return p->second;
 }
 
@@ -1999,8 +2574,7 @@ Complex_type::create_abstract_complex_type()
 {
   static Complex_type* abstract_type;
   if (abstract_type == NULL)
-    abstract_type = new Complex_type(true, FLOAT_TYPE_SIZE * 2,
-                                    RUNTIME_TYPE_KIND_FLOAT);
+    abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
   return abstract_type;
 }
 
@@ -2022,45 +2596,12 @@ Complex_type::do_hash_for_method(Gogo*) const
   return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
 }
 
-// Get a tree without using a Gogo*.
-
-tree
-Complex_type::type_tree() const
-{
-  if (this->bits_ == FLOAT_TYPE_SIZE * 2)
-    return complex_float_type_node;
-  else if (this->bits_ == DOUBLE_TYPE_SIZE * 2)
-    return complex_double_type_node;
-  else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE * 2)
-    return complex_long_double_type_node;
-  else
-    {
-      tree ret = make_node(REAL_TYPE);
-      TYPE_PRECISION(ret) = this->bits_ / 2;
-      layout_type(ret);
-      return build_complex_type(ret);
-    }
-}
-
-// Get a tree.
+// Convert to the backend representation.
 
-tree
-Complex_type::do_get_tree(Gogo*)
-{
-  return this->type_tree();
-}
-
-// Zero initializer.
-
-tree
-Complex_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
+Btype*
+Complex_type::do_get_backend(Gogo* gogo)
 {
-  if (is_clear)
-    return NULL;
-  REAL_VALUE_TYPE r;
-  real_from_integer(&r, TYPE_MODE(TREE_TYPE(type_tree)), 0, 0, 0);
-  return build_complex(type_tree, build_real(TREE_TYPE(type_tree), r),
-                      build_real(TREE_TYPE(type_tree), r));
+  return gogo->backend()->complex_type(this->bits_);
 }
 
 // The type descriptor for a complex type.  Complex types are always
@@ -2069,7 +2610,7 @@ Complex_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
 Expression*
 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  gcc_assert(name != NULL);
+  go_assert(name != NULL);
   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
 }
 
@@ -2078,7 +2619,7 @@ Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 void
 Complex_type::do_reflection(Gogo*, std::string*) const
 {
-  gcc_unreachable();
+  go_assert(saw_errors());
 }
 
 // Mangled name.
@@ -2119,18 +2660,31 @@ Type::lookup_complex_type(const char* name)
 
 // Class String_type.
 
-// Return the tree for String_type.  A string is a struct with two
-// fields: a pointer to the characters and a length.
+// Convert String_type to the backend representation.  A string is a
+// struct with two fields: a pointer to the characters and a length.
 
-tree
-String_type::do_get_tree(Gogo*)
+Btype*
+String_type::do_get_backend(Gogo* gogo)
 {
-  static tree struct_type;
-  return Gogo::builtin_struct(&struct_type, "__go_string", NULL_TREE, 2,
-                             "__data",
-                             build_pointer_type(unsigned_char_type_node),
-                             "__length",
-                             integer_type_node);
+  static Btype* backend_string_type;
+  if (backend_string_type == NULL)
+    {
+      std::vector<Backend::Btyped_identifier> fields(2);
+
+      Type* b = gogo->lookup_global("byte")->type_value();
+      Type* pb = Type::make_pointer_type(b);
+      fields[0].name = "__data";
+      fields[0].btype = pb->get_backend(gogo);
+      fields[0].location = Linemap::predeclared_location();
+
+      Type* int_type = Type::lookup_integer_type("int");
+      fields[1].name = "__length";
+      fields[1].btype = int_type->get_backend(gogo);
+      fields[1].location = fields[0].location;
+
+      backend_string_type = gogo->backend()->struct_type(fields);
+    }
+  return backend_string_type;
 }
 
 // Return a tree for the length of STRING.
@@ -2139,9 +2693,9 @@ tree
 String_type::length_tree(Gogo*, tree string)
 {
   tree string_type = TREE_TYPE(string);
-  gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
+  go_assert(TREE_CODE(string_type) == RECORD_TYPE);
   tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
-  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
+  go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
                    "__length") == 0);
   return fold_build3(COMPONENT_REF, integer_type_node, string,
                     length_field, NULL_TREE);
@@ -2153,40 +2707,14 @@ tree
 String_type::bytes_tree(Gogo*, tree string)
 {
   tree string_type = TREE_TYPE(string);
-  gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
+  go_assert(TREE_CODE(string_type) == RECORD_TYPE);
   tree bytes_field = TYPE_FIELDS(string_type);
-  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
+  go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
                    "__data") == 0);
   return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
                     bytes_field, NULL_TREE);
 }
 
-// We initialize a string to { NULL, 0 }.
-
-tree
-String_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
-{
-  if (is_clear)
-    return NULL_TREE;
-
-  gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
-
-  VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
-
-  for (tree field = TYPE_FIELDS(type_tree);
-       field != NULL_TREE;
-       field = DECL_CHAIN(field))
-    {
-      constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
-      elt->index = field;
-      elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
-    }
-
-  tree ret = build_constructor(type_tree, init);
-  TREE_CONSTANT(ret) = 1;
-  return ret;
-}
-
 // The type descriptor for the string type.
 
 Expression*
@@ -2197,7 +2725,7 @@ String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   else
     {
       Named_object* no = gogo->lookup_global("string");
-      gcc_assert(no != NULL);
+      go_assert(no != NULL);
       return Type::type_descriptor(gogo, no->type_value());
     }
 }
@@ -2245,9 +2773,9 @@ Named_type*
 Type::make_named_string_type()
 {
   Type* string_type = Type::make_string_type();
-  Named_object* named_object = Named_object::make_type("string", NULL,
-                                                      string_type,
-                                                      BUILTINS_LOCATION);
+  Named_object* named_object =
+    Named_object::make_type("string", NULL, string_type,
+                            Linemap::predeclared_location());
   Named_type* named_type = named_object->type_value();
   named_string_type = named_type;
   return named_type;
@@ -2264,25 +2792,25 @@ class Sink_type : public Type
   { }
 
  protected:
-  tree
-  do_get_tree(Gogo*)
-  { gcc_unreachable(); }
+  bool
+  do_compare_is_identity() const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo*, tree, bool)
-  { gcc_unreachable(); }
+  Btype*
+  do_get_backend(Gogo*)
+  { go_unreachable(); }
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*)
-  { gcc_unreachable(); }
+  { go_unreachable(); }
 
   void
   do_reflection(Gogo*, std::string*) const
-  { gcc_unreachable(); }
+  { go_unreachable(); }
 
   void
   do_mangled_name(Gogo*, std::string*) const
-  { gcc_unreachable(); }
+  { go_unreachable(); }
 };
 
 // Make the sink type.
@@ -2321,7 +2849,7 @@ bool
 Function_type::is_valid_redeclaration(const Function_type* t,
                                      std::string* reason) const
 {
-  if (!this->is_identical(t, false, reason))
+  if (!this->is_identical(t, false, true, reason))
     return false;
 
   // A redeclaration of a function is required to use the same names
@@ -2405,6 +2933,7 @@ Function_type::is_valid_redeclaration(const Function_type* t,
 
 bool
 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
+                           bool errors_are_identical,
                            std::string* reason) const
 {
   if (!ignore_receiver)
@@ -2419,7 +2948,8 @@ Function_type::is_identical(const Function_type* t, bool ignore_receiver,
        }
       if (r1 != NULL)
        {
-         if (!Type::are_identical(r1->type(), r2->type(), reason))
+         if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
+                                  reason))
            {
              if (reason != NULL && !reason->empty())
                *reason = "receiver: " + *reason;
@@ -2450,7 +2980,8 @@ Function_type::is_identical(const Function_type* t, bool ignore_receiver,
              return false;
            }
 
-         if (!Type::are_identical(p1->type(), p2->type(), NULL))
+         if (!Type::are_identical(p1->type(), p2->type(),
+                                  errors_are_identical, NULL))
            {
              if (reason != NULL)
                *reason = _("different parameter types");
@@ -2494,7 +3025,8 @@ Function_type::is_identical(const Function_type* t, bool ignore_receiver,
              return false;
            }
 
-         if (!Type::are_identical(res1->type(), res2->type(), NULL))
+         if (!Type::are_identical(res1->type(), res2->type(),
+                                  errors_are_identical, NULL))
            {
              if (reason != NULL)
                *reason = _("different result types");
@@ -2543,98 +3075,99 @@ Function_type::do_hash_for_method(Gogo* gogo) const
   return ret;
 }
 
-// Get the tree for a function type.
+// Get the backend representation for a function type.
 
-tree
-Function_type::do_get_tree(Gogo* gogo)
+Btype*
+Function_type::get_function_backend(Gogo* gogo)
 {
-  tree args = NULL_TREE;
-  tree* pp = &args;
-
+  Backend::Btyped_identifier breceiver;
   if (this->receiver_ != NULL)
     {
-      Type* rtype = this->receiver_->type();
-      tree ptype = rtype->get_tree(gogo);
-      if (ptype == error_mark_node)
-       return error_mark_node;
+      breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
 
       // We always pass the address of the receiver parameter, in
       // order to make interface calls work with unknown types.
+      Type* rtype = this->receiver_->type();
       if (rtype->points_to() == NULL)
-       ptype = build_pointer_type(ptype);
-
-      *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
-      pp = &TREE_CHAIN (*pp);
+       rtype = Type::make_pointer_type(rtype);
+      breceiver.btype = rtype->get_backend(gogo);
+      breceiver.location = this->receiver_->location();
     }
 
+  std::vector<Backend::Btyped_identifier> bparameters;
   if (this->parameters_ != NULL)
     {
+      bparameters.resize(this->parameters_->size());
+      size_t i = 0;
       for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
           p != this->parameters_->end();
-          ++p)
+          ++p, ++i)
        {
-         tree ptype = p->type()->get_tree(gogo);
-         if (ptype == error_mark_node)
-           return error_mark_node;
-         *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
-         pp = &TREE_CHAIN (*pp);
+         bparameters[i].name = Gogo::unpack_hidden_name(p->name());
+         bparameters[i].btype = p->type()->get_backend(gogo);
+         bparameters[i].location = p->location();
        }
+      go_assert(i == bparameters.size());
     }
 
-  // Varargs is handled entirely at the Go level.  At the tree level,
-  // functions are not varargs.
-  *pp = void_list_node;
-
-  tree result;
-  if (this->results_ == NULL)
-    result = void_type_node;
-  else if (this->results_->size() == 1)
-    result = this->results_->begin()->type()->get_tree(gogo);
-  else
+  std::vector<Backend::Btyped_identifier> bresults;
+  if (this->results_ != NULL)
     {
-      result = make_node(RECORD_TYPE);
-      tree field_trees = NULL_TREE;
-      tree* pp = &field_trees;
+      bresults.resize(this->results_->size());
+      size_t i = 0;
       for (Typed_identifier_list::const_iterator p = this->results_->begin();
           p != this->results_->end();
-          ++p)
+          ++p, ++i)
        {
-         const std::string name = (p->name().empty()
-                                   ? "UNNAMED"
-                                   : Gogo::unpack_hidden_name(p->name()));
-         tree name_tree = get_identifier_with_length(name.data(),
-                                                     name.length());
-         tree field_type_tree = p->type()->get_tree(gogo);
-         if (field_type_tree == error_mark_node)
-           return error_mark_node;
-         tree field = build_decl(this->location_, FIELD_DECL, name_tree,
-                                 field_type_tree);
-         DECL_CONTEXT(field) = result;
-         *pp = field;
-         pp = &DECL_CHAIN(field);
+         bresults[i].name = Gogo::unpack_hidden_name(p->name());
+         bresults[i].btype = p->type()->get_backend(gogo);
+         bresults[i].location = p->location();
        }
-      TYPE_FIELDS(result) = field_trees;
-      layout_type(result);
+      go_assert(i == bresults.size());
     }
 
-  if (result == error_mark_node)
-    return error_mark_node;
+  return gogo->backend()->function_type(breceiver, bparameters, bresults,
+                                       this->location());
+}
+
+// A hash table mapping function types to their backend placeholders.
+
+Function_type::Placeholders Function_type::placeholders;
 
-  tree fntype = build_function_type(result, args);
-  if (fntype == error_mark_node)
-    return fntype;
+// Get the backend representation for a function type.  If we are
+// still converting types, and this types has multiple results, return
+// a placeholder instead.  We do this because for multiple results we
+// build a struct, and we need to make sure that all the types in the
+// struct are valid before we create the struct.
 
-  return build_pointer_type(fntype);
+Btype*
+Function_type::do_get_backend(Gogo* gogo)
+{
+  if (!gogo->named_types_are_converted()
+      && this->results_ != NULL
+      && this->results_->size() > 1)
+    {
+      Btype* placeholder =
+       gogo->backend()->placeholder_pointer_type("", this->location(), true);
+      Function_type::placeholders.push_back(std::make_pair(this, placeholder));
+      return placeholder;
+    }
+  return this->get_function_backend(gogo);
 }
 
-// Functions are initialized to NULL.
+// Convert function types after all named types are converted.
 
-tree
-Function_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
+void
+Function_type::convert_types(Gogo* gogo)
 {
-  if (is_clear)
-    return NULL;
-  return fold_convert(type_tree, null_pointer_node);
+  for (Placeholders::const_iterator p = Function_type::placeholders.begin();
+       p != Function_type::placeholders.end();
+       ++p)
+    {
+      Btype* bt = p->first->get_function_backend(gogo);
+      if (!gogo->backend()->set_placeholder_function_type(p->second, bt))
+       go_assert(saw_errors());
+    }
 }
 
 // The type of a function type descriptor.
@@ -2669,7 +3202,7 @@ Function_type::make_function_type_descriptor_type()
 Expression*
 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* ftdt = Function_type::make_function_type_descriptor_type();
 
@@ -2679,27 +3212,27 @@ Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   vals->reserve(4);
 
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "commonType");
+  go_assert(p->is_field_name("commonType"));
   vals->push_back(this->type_descriptor_constructor(gogo,
                                                    RUNTIME_TYPE_KIND_FUNC,
                                                    name, NULL, true));
 
   ++p;
-  gcc_assert(p->field_name() == "dotdotdot");
+  go_assert(p->is_field_name("dotdotdot"));
   vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "in");
+  go_assert(p->is_field_name("in"));
   vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
                                               this->parameters()));
 
   ++p;
-  gcc_assert(p->field_name() == "out");
+  go_assert(p->is_field_name("out"));
   vals->push_back(this->type_descriptor_params(p->type(), NULL,
                                               this->results()));
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p == fields->end());
 
   return Expression::make_struct_composite_literal(ftdt, vals, bloc);
 }
@@ -2712,7 +3245,7 @@ Function_type::type_descriptor_params(Type* params_type,
                                      const Typed_identifier* receiver,
                                      const Typed_identifier_list* params)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   if (receiver == NULL && params == NULL)
     return Expression::make_slice_composite_literal(params_type, NULL, bloc);
@@ -2722,14 +3255,7 @@ Function_type::type_descriptor_params(Type* params_type,
                + (receiver != NULL ? 1 : 0));
 
   if (receiver != NULL)
-    {
-      Type* rtype = receiver->type();
-      // The receiver is always passed as a pointer.  FIXME: Is this
-      // right?  Should that fact affect the type descriptor?
-      if (rtype->points_to() == NULL)
-       rtype = Type::make_pointer_type(rtype);
-      vals->push_back(Expression::make_type_descriptor(rtype, bloc));
-    }
+    vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
 
   if (params != NULL)
     {
@@ -2749,7 +3275,7 @@ Function_type::do_reflection(Gogo* gogo, std::string* ret) const
 {
   // FIXME: Turn this off until we straighten out the type of the
   // struct field used in a go statement which calls a method.
-  // gcc_assert(this->receiver_ == NULL);
+  // go_assert(this->receiver_ == NULL);
 
   ret->append("func");
 
@@ -2931,7 +3457,7 @@ Function_type::do_import(Import* imp)
                                                 ptype, imp->location()));
          if (imp->peek_char() != ',')
            break;
-         gcc_assert(!is_varargs);
+         go_assert(!is_varargs);
          imp->require_c_string(", ");
        }
     }
@@ -2978,7 +3504,7 @@ Function_type::do_import(Import* imp)
 Function_type*
 Function_type::copy_without_receiver() const
 {
-  gcc_assert(this->is_method());
+  go_assert(this->is_method());
   Function_type *ret = Type::make_function_type(NULL, this->parameters_,
                                                this->results_,
                                                this->location_);
@@ -2994,7 +3520,7 @@ Function_type::copy_without_receiver() const
 Function_type*
 Function_type::copy_with_receiver(Type* receiver_type) const
 {
-  gcc_assert(!this->is_method());
+  go_assert(!this->is_method());
   Typed_identifier* receiver = new Typed_identifier("", receiver_type,
                                                    this->location_);
   return Type::make_function_type(receiver, this->parameters_,
@@ -3007,7 +3533,7 @@ Function_type*
 Type::make_function_type(Typed_identifier* receiver,
                         Typed_identifier_list* parameters,
                         Typed_identifier_list* results,
-                        source_location location)
+                        Location location)
 {
   return new Function_type(receiver, parameters, results, location);
 }
@@ -3032,20 +3558,11 @@ Pointer_type::do_hash_for_method(Gogo* gogo) const
 
 // The tree for a pointer type.
 
-tree
-Pointer_type::do_get_tree(Gogo* gogo)
-{
-  return build_pointer_type(this->to_type_->get_tree(gogo));
-}
-
-// Initialize a pointer type.
-
-tree
-Pointer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
+Btype*
+Pointer_type::do_get_backend(Gogo* gogo)
 {
-  if (is_clear)
-    return NULL;
-  return fold_convert(type_tree, null_pointer_node);
+  Btype* to_btype = this->to_type_->get_backend(gogo);
+  return gogo->backend()->pointer_type(to_btype);
 }
 
 // The type of a pointer type descriptor.
@@ -3076,14 +3593,14 @@ Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
   if (this->is_unsafe_pointer_type())
     {
-      gcc_assert(name != NULL);
+      go_assert(name != NULL);
       return this->plain_type_descriptor(gogo,
                                         RUNTIME_TYPE_KIND_UNSAFE_POINTER,
                                         name);
     }
   else
     {
-      source_location bloc = BUILTINS_LOCATION;
+      Location bloc = Linemap::predeclared_location();
 
       const Methods* methods;
       Type* deref = this->points_to();
@@ -3102,13 +3619,13 @@ Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
       vals->reserve(2);
 
       Struct_field_list::const_iterator p = fields->begin();
-      gcc_assert(p->field_name() == "commonType");
+      go_assert(p->is_field_name("commonType"));
       vals->push_back(this->type_descriptor_constructor(gogo,
                                                        RUNTIME_TYPE_KIND_PTR,
                                                        name, methods, false));
 
       ++p;
-      gcc_assert(p->field_name() == "elem");
+      go_assert(p->is_field_name("elem"));
       vals->push_back(Expression::make_type_descriptor(deref, bloc));
 
       return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
@@ -3187,21 +3704,21 @@ class Nil_type : public Type
   { }
 
  protected:
-  tree
-  do_get_tree(Gogo*)
-  { return ptr_type_node; }
+  bool
+  do_compare_is_identity() const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
-  { return is_clear ? NULL : fold_convert(type_tree, null_pointer_node); }
+  Btype*
+  do_get_backend(Gogo* gogo)
+  { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*)
-  { gcc_unreachable(); }
+  { go_unreachable(); }
 
   void
   do_reflection(Gogo*, std::string*) const
-  { gcc_unreachable(); }
+  { go_unreachable(); }
 
   void
   do_mangled_name(Gogo*, std::string* ret) const
@@ -3233,57 +3750,42 @@ class Call_multiple_result_type : public Type
  protected:
   bool
   do_has_pointer() const
-  { gcc_unreachable(); }
+  {
+    go_assert(saw_errors());
+    return false;
+  }
 
-  tree
-  do_get_tree(Gogo*);
+  bool
+  do_compare_is_identity() const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo*, tree, bool)
-  { gcc_unreachable(); }
+  Btype*
+  do_get_backend(Gogo* gogo)
+  {
+    go_assert(saw_errors());
+    return gogo->backend()->error_type();
+  }
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*)
-  { gcc_unreachable(); }
+  {
+    go_assert(saw_errors());
+    return Expression::make_error(Linemap::unknown_location());
+  }
 
   void
   do_reflection(Gogo*, std::string*) const
-  { gcc_unreachable(); }
+  { go_assert(saw_errors()); }
 
   void
   do_mangled_name(Gogo*, std::string*) const
-  { gcc_unreachable(); }
+  { go_assert(saw_errors()); }
 
  private:
   // The expression being called.
   Call_expression* call_;
 };
 
-// Return the tree for a call result.
-
-tree
-Call_multiple_result_type::do_get_tree(Gogo* gogo)
-{
-  Function_type* fntype = this->call_->get_function_type();
-  gcc_assert(fntype != NULL);
-  const Typed_identifier_list* results = fntype->results();
-  gcc_assert(results != NULL && results->size() > 1);
-
-  Struct_field_list* sfl = new Struct_field_list;
-  for (Typed_identifier_list::const_iterator p = results->begin();
-       p != results->end();
-       ++p)
-    {
-      const std::string name = ((p->name().empty()
-                                || p->name() == Import::import_marker)
-                               ? "UNNAMED"
-                               : p->name());
-      sfl->push_back(Struct_field(Typed_identifier(name, p->type(),
-                                                  this->call_->location())));
-    }
-  return Type::make_struct_type(sfl, this->call_->location())->get_tree(gogo);
-}
-
 // Make a call result type.
 
 Type*
@@ -3328,17 +3830,45 @@ Struct_field::field_name() const
        {
          // Avoid crashing in the erroneous case where T is named but
          // DT is not.
-         gcc_assert(t != dt);
+         go_assert(t != dt);
          if (t->forward_declaration_type() != NULL)
            return t->forward_declaration_type()->name();
          else if (t->named_type() != NULL)
            return t->named_type()->name();
          else
-           gcc_unreachable();
+           go_unreachable();
        }
     }
 }
 
+// Return whether this field is named NAME.
+
+bool
+Struct_field::is_field_name(const std::string& name) const
+{
+  const std::string& me(this->typed_identifier_.name());
+  if (!me.empty())
+    return me == name;
+  else
+    {
+      Type* t = this->typed_identifier_.type();
+      if (t->points_to() != NULL)
+       t = t->points_to();
+      Named_type* nt = t->named_type();
+      if (nt != NULL && nt->name() == name)
+       return true;
+
+      // This is a horrible hack caused by the fact that we don't pack
+      // the names of builtin types.  FIXME.
+      if (nt != NULL
+         && nt->is_builtin()
+         && nt->name() == Gogo::unpack_hidden_name(name))
+       return true;
+
+      return false;
+    }
+}
+
 // Class Struct_type.
 
 // Traversal.
@@ -3368,6 +3898,7 @@ Struct_type::do_verify()
   Struct_field_list* fields = this->fields_;
   if (fields == NULL)
     return true;
+  bool ret = true;
   for (Struct_field_list::iterator p = fields->begin();
        p != fields->end();
        ++p)
@@ -3377,7 +3908,7 @@ Struct_type::do_verify()
        {
          error_at(p->location(), "struct field type is incomplete");
          p->set_type(Type::make_error_type());
-         return false;
+         ret = false;
        }
       else if (p->is_anonymous())
        {
@@ -3387,9 +3918,17 @@ Struct_type::do_verify()
              p->set_type(Type::make_error_type());
              return false;
            }
+         if (t->points_to() != NULL
+             && t->points_to()->interface_type() != NULL)
+           {
+             error_at(p->location(),
+                      "embedded type may not be pointer to interface");
+             p->set_type(Type::make_error_type());
+             return false;
+           }
        }
     }
-  return true;
+  return ret;
 }
 
 // Whether this contains a pointer.
@@ -3413,7 +3952,8 @@ Struct_type::do_has_pointer() const
 // Whether this type is identical to T.
 
 bool
-Struct_type::is_identical(const Struct_type* t) const
+Struct_type::is_identical(const Struct_type* t,
+                         bool errors_are_identical) const
 {
   const Struct_field_list* fields1 = this->fields();
   const Struct_field_list* fields2 = t->fields();
@@ -3429,7 +3969,8 @@ Struct_type::is_identical(const Struct_type* t) const
       if (pf1->field_name() != pf2->field_name())
        return false;
       if (pf1->is_anonymous() != pf2->is_anonymous()
-         || !Type::are_identical(pf1->type(), pf2->type(), NULL))
+         || !Type::are_identical(pf1->type(), pf2->type(),
+                                 errors_are_identical, NULL))
        return false;
       if (!pf1->has_tag())
        {
@@ -3492,6 +4033,25 @@ Struct_type::struct_has_hidden_fields(const Named_type* within,
   return false;
 }
 
+// Whether comparisons of this struct type are simple identity
+// comparisons.
+
+bool
+Struct_type::do_compare_is_identity() const
+{
+  const Struct_field_list* fields = this->fields_;
+  if (fields == NULL)
+    return true;
+  for (Struct_field_list::const_iterator pf = fields->begin();
+       pf != fields->end();
+       ++pf)
+    if (!pf->type()->compare_is_identity())
+      return false;
+  return true;
+}
+
+// Build identity and hash functions for this struct.
+
 // Hash code.
 
 unsigned int
@@ -3522,7 +4082,7 @@ Struct_type::find_local_field(const std::string& name,
        pf != fields->end();
        ++pf, ++i)
     {
-      if (pf->field_name() == name)
+      if (pf->is_field_name(name))
        {
          if (pindex != NULL)
            *pindex = i;
@@ -3536,10 +4096,11 @@ Struct_type::find_local_field(const std::string& name,
 
 Field_reference_expression*
 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
-                            source_location location) const
+                            Location location) const
 {
   unsigned int depth;
-  return this->field_reference_depth(struct_expr, name, location, &depth);
+  return this->field_reference_depth(struct_expr, name, location, NULL,
+                                    &depth);
 }
 
 // Return an expression for a field, along with the depth at which it
@@ -3548,7 +4109,8 @@ Struct_type::field_reference(Expression* struct_expr, const std::string& name,
 Field_reference_expression*
 Struct_type::field_reference_depth(Expression* struct_expr,
                                   const std::string& name,
-                                  source_location location,
+                                  Location location,
+                                  Saw_named_type* saw,
                                   unsigned int* depth) const
 {
   const Struct_field_list* fields = this->fields_;
@@ -3561,7 +4123,7 @@ Struct_type::field_reference_depth(Expression* struct_expr,
        pf != fields->end();
        ++pf, ++i)
     {
-      if (pf->field_name() == name)
+      if (pf->is_field_name(name))
        {
          *depth = 0;
          return Expression::make_field_reference(struct_expr, i, location);
@@ -3584,13 +4146,41 @@ Struct_type::field_reference_depth(Expression* struct_expr,
       if (st == NULL)
        continue;
 
+      Saw_named_type* hold_saw = saw;
+      Saw_named_type saw_here;
+      Named_type* nt = pf->type()->named_type();
+      if (nt == NULL)
+       nt = pf->type()->deref()->named_type();
+      if (nt != NULL)
+       {
+         Saw_named_type* q;
+         for (q = saw; q != NULL; q = q->next)
+           {
+             if (q->nt == nt)
+               {
+                 // If this is an error, it will be reported
+                 // elsewhere.
+                 break;
+               }
+           }
+         if (q != NULL)
+           continue;
+         saw_here.next = saw;
+         saw_here.nt = nt;
+         saw = &saw_here;
+       }
+
       // Look for a reference using a NULL struct expression.  If we
       // find one, fill in the struct expression with a reference to
       // this field.
       unsigned int subdepth;
       Field_reference_expression* sub = st->field_reference_depth(NULL, name,
                                                                  location,
+                                                                 saw,
                                                                  &subdepth);
+
+      saw = hold_saw;
+
       if (sub == NULL)
        continue;
 
@@ -3607,7 +4197,7 @@ Struct_type::field_reference_depth(Expression* struct_expr,
          while (sub->expr() != NULL)
            {
              sub = sub->expr()->deref()->field_reference_expression();
-             gcc_assert(sub != NULL);
+             go_assert(sub != NULL);
            }
          sub->set_struct_expression(here);
        }
@@ -3677,6 +4267,19 @@ Struct_type::is_unexported_local_field(Gogo* gogo,
 void
 Struct_type::finalize_methods(Gogo* gogo)
 {
+  // If this type needs explicit comparison and hash functions, create
+  // them now.  It would be a bit better to do this only if the
+  // functions are needed, but they will be static so the backend can
+  // discard them if they are not used.
+  if (!this->compare_is_identity() && this->is_comparable())
+    {
+      Named_object* hash_fn;
+      Named_object* equal_fn;
+      this->type_functions(gogo, NULL, NULL, NULL, &hash_fn, &equal_fn);
+    }
+
+  if (this->all_methods_ != NULL)
+    return;
   Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
 }
 
@@ -3690,97 +4293,35 @@ Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
   return Type::method_function(this->all_methods_, name, is_ambiguous);
 }
 
-// Get the tree for a struct type.
-
-tree
-Struct_type::do_get_tree(Gogo* gogo)
-{
-  tree type = make_node(RECORD_TYPE);
-  return this->fill_in_tree(gogo, type);
-}
-
-// Fill in the fields for a struct type.
+// Convert struct fields to the backend representation.  This is not
+// declared in types.h so that types.h doesn't have to #include
+// backend.h.
 
-tree
-Struct_type::fill_in_tree(Gogo* gogo, tree type)
+static void
+get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
+                         std::vector<Backend::Btyped_identifier>* bfields)
 {
-  tree field_trees = NULL_TREE;
-  tree* pp = &field_trees;
-  for (Struct_field_list::const_iterator p = this->fields_->begin();
-       p != this->fields_->end();
-       ++p)
+  bfields->resize(fields->size());
+  size_t i = 0;
+  for (Struct_field_list::const_iterator p = fields->begin();
+       p != fields->end();
+       ++p, ++i)
     {
-      std::string name = Gogo::unpack_hidden_name(p->field_name());
-      tree name_tree = get_identifier_with_length(name.data(), name.length());
-      tree field_type_tree = p->type()->get_tree(gogo);
-      if (field_type_tree == error_mark_node)
-       return error_mark_node;
-      tree field = build_decl(p->location(), FIELD_DECL, name_tree,
-                             field_type_tree);
-      DECL_CONTEXT(field) = type;
-      *pp = field;
-      pp = &DECL_CHAIN(field);
+      (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
+      (*bfields)[i].btype = p->type()->get_backend(gogo);
+      (*bfields)[i].location = p->location();
     }
-
-  TYPE_FIELDS(type) = field_trees;
-
-  layout_type(type);
-
-  return type;
+  go_assert(i == fields->size());
 }
 
-// Initialize struct fields.
+// Get the tree for a struct type.
 
-tree
-Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
+Btype*
+Struct_type::do_get_backend(Gogo* gogo)
 {
-  if (this->fields_ == NULL || this->fields_->empty())
-    {
-      if (is_clear)
-       return NULL;
-      else
-       {
-         tree ret = build_constructor(type_tree,
-                                      VEC_alloc(constructor_elt, gc, 0));
-         TREE_CONSTANT(ret) = 1;
-         return ret;
-       }
-    }
-
-  bool is_constant = true;
-  bool any_fields_set = false;
-  VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc,
-                                           this->fields_->size());
-  Struct_field_list::const_iterator p = this->fields_->begin();
-  for (tree field = TYPE_FIELDS(type_tree);
-       field != NULL_TREE;
-       field = DECL_CHAIN(field), ++p)
-    {
-      gcc_assert(p != this->fields_->end());
-      tree value = p->type()->get_init_tree(gogo, is_clear);
-      if (value != NULL)
-       {
-         constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
-         elt->index = field;
-         elt->value = value;
-         any_fields_set = true;
-         if (!TREE_CONSTANT(value))
-           is_constant = false;
-       }
-    }
-  gcc_assert(p == this->fields_->end());
-
-  if (!any_fields_set)
-    {
-      gcc_assert(is_clear);
-      VEC_free(constructor_elt, gc, init);
-      return NULL;
-    }
-
-  tree ret = build_constructor(type_tree, init);
-  if (is_constant)
-    TREE_CONSTANT(ret) = 1;
-  return ret;
+  std::vector<Backend::Btyped_identifier> bfields;
+  get_backend_struct_fields(gogo, this->fields_, &bfields);
+  return gogo->backend()->struct_type(bfields);
 }
 
 // The type of a struct type descriptor.
@@ -3824,7 +4365,7 @@ Struct_type::make_struct_type_descriptor_type()
 Expression*
 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* stdt = Struct_type::make_struct_type_descriptor_type();
 
@@ -3836,16 +4377,16 @@ Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   const Methods* methods = this->methods();
   // A named struct should not have methods--the methods should attach
   // to the named type.
-  gcc_assert(methods == NULL || name == NULL);
+  go_assert(methods == NULL || name == NULL);
 
   Struct_field_list::const_iterator ps = fields->begin();
-  gcc_assert(ps->field_name() == "commonType");
+  go_assert(ps->is_field_name("commonType"));
   vals->push_back(this->type_descriptor_constructor(gogo,
                                                    RUNTIME_TYPE_KIND_STRUCT,
                                                    name, methods, true));
 
   ++ps;
-  gcc_assert(ps->field_name() == "fields");
+  go_assert(ps->is_field_name("fields"));
 
   Expression_list* elements = new Expression_list();
   elements->reserve(this->fields_->size());
@@ -3860,7 +4401,7 @@ Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
       fvals->reserve(5);
 
       Struct_field_list::const_iterator q = f->begin();
-      gcc_assert(q->field_name() == "name");
+      go_assert(q->is_field_name("name"));
       if (pf->is_anonymous())
        fvals->push_back(Expression::make_nil(bloc));
       else
@@ -3871,7 +4412,7 @@ Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
        }
 
       ++q;
-      gcc_assert(q->field_name() == "pkgPath");
+      go_assert(q->is_field_name("pkgPath"));
       if (!Gogo::is_hidden_name(pf->field_name()))
        fvals->push_back(Expression::make_nil(bloc));
       else
@@ -3882,11 +4423,11 @@ Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
        }
 
       ++q;
-      gcc_assert(q->field_name() == "typ");
+      go_assert(q->is_field_name("typ"));
       fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
 
       ++q;
-      gcc_assert(q->field_name() == "tag");
+      go_assert(q->is_field_name("tag"));
       if (!pf->has_tag())
        fvals->push_back(Expression::make_nil(bloc));
       else
@@ -3896,7 +4437,7 @@ Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
        }
 
       ++q;
-      gcc_assert(q->field_name() == "offset");
+      go_assert(q->is_field_name("offset"));
       fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
 
       Expression* v = Expression::make_struct_composite_literal(element_type,
@@ -3910,6 +4451,170 @@ Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   return Expression::make_struct_composite_literal(stdt, vals, bloc);
 }
 
+// Write the hash function for a struct which can not use the identity
+// function.
+
+void
+Struct_type::write_hash_function(Gogo* gogo, Named_type*,
+                                Function_type* hash_fntype,
+                                Function_type* equal_fntype)
+{
+  Location bloc = Linemap::predeclared_location();
+
+  // The pointer to the struct that we are going to hash.  This is an
+  // argument to the hash function we are implementing here.
+  Named_object* key_arg = gogo->lookup("key", NULL);
+  go_assert(key_arg != NULL);
+  Type* key_arg_type = key_arg->var_value()->type();
+
+  Type* uintptr_type = Type::lookup_integer_type("uintptr");
+
+  // Get a 0.
+  mpz_t ival;
+  mpz_init_set_ui(ival, 0);
+  Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
+  mpz_clear(ival);
+
+  // Make a temporary to hold the return value, initialized to 0.
+  Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
+                                                         bloc);
+  gogo->add_statement(retval);
+
+  // Make a temporary to hold the key as a uintptr.
+  Expression* ref = Expression::make_var_reference(key_arg, bloc);
+  ref = Expression::make_cast(uintptr_type, ref, bloc);
+  Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
+                                                      bloc);
+  gogo->add_statement(key);
+
+  // Loop over the struct fields.
+  bool first = true;
+  const Struct_field_list* fields = this->fields_;
+  for (Struct_field_list::const_iterator pf = fields->begin();
+       pf != fields->end();
+       ++pf)
+    {
+      if (first)
+       first = false;
+      else
+       {
+         // Multiply retval by 33.
+         mpz_init_set_ui(ival, 33);
+         Expression* i33 = Expression::make_integer(&ival, uintptr_type,
+                                                    bloc);
+         mpz_clear(ival);
+
+         ref = Expression::make_temporary_reference(retval, bloc);
+         Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ,
+                                                             ref, i33, bloc);
+         gogo->add_statement(s);
+       }
+
+      // Get a pointer to the value of this field.
+      Expression* offset = Expression::make_struct_field_offset(this, &*pf);
+      ref = Expression::make_temporary_reference(key, bloc);
+      Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
+                                                  bloc);
+      subkey = Expression::make_cast(key_arg_type, subkey, bloc);
+
+      // Get the size of this field.
+      Expression* size = Expression::make_type_info(pf->type(),
+                                                   Expression::TYPE_INFO_SIZE);
+
+      // Get the hash function to use for the type of this field.
+      Named_object* hash_fn;
+      Named_object* equal_fn;
+      pf->type()->type_functions(gogo, pf->type()->named_type(), hash_fntype,
+                                equal_fntype, &hash_fn, &equal_fn);
+
+      // Call the hash function for the field.
+      Expression_list* args = new Expression_list();
+      args->push_back(subkey);
+      args->push_back(size);
+      Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
+      Expression* call = Expression::make_call(func, args, false, bloc);
+
+      // Add the field's hash value to retval.
+      Temporary_reference_expression* tref =
+       Expression::make_temporary_reference(retval, bloc);
+      tref->set_is_lvalue();
+      Statement* s = Statement::make_assignment_operation(OPERATOR_PLUSEQ,
+                                                         tref, call, bloc);
+      gogo->add_statement(s);
+    }
+
+  // Return retval to the caller of the hash function.
+  Expression_list* vals = new Expression_list();
+  ref = Expression::make_temporary_reference(retval, bloc);
+  vals->push_back(ref);
+  Statement* s = Statement::make_return_statement(vals, bloc);
+  gogo->add_statement(s);
+}
+
+// Write the equality function for a struct which can not use the
+// identity function.
+
+void
+Struct_type::write_equal_function(Gogo* gogo, Named_type* name)
+{
+  Location bloc = Linemap::predeclared_location();
+
+  // The pointers to the structs we are going to compare.
+  Named_object* key1_arg = gogo->lookup("key1", NULL);
+  Named_object* key2_arg = gogo->lookup("key2", NULL);
+  go_assert(key1_arg != NULL && key2_arg != NULL);
+
+  // Build temporaries with the right types.
+  Type* pt = Type::make_pointer_type(name != NULL
+                                    ? static_cast<Type*>(name)
+                                    : static_cast<Type*>(this));
+
+  Expression* ref = Expression::make_var_reference(key1_arg, bloc);
+  ref = Expression::make_unsafe_cast(pt, ref, bloc);
+  Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
+  gogo->add_statement(p1);
+
+  ref = Expression::make_var_reference(key2_arg, bloc);
+  ref = Expression::make_unsafe_cast(pt, ref, bloc);
+  Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
+  gogo->add_statement(p2);
+
+  const Struct_field_list* fields = this->fields_;
+  unsigned int field_index = 0;
+  for (Struct_field_list::const_iterator pf = fields->begin();
+       pf != fields->end();
+       ++pf, ++field_index)
+    {
+      // Compare one field in both P1 and P2.
+      Expression* f1 = Expression::make_temporary_reference(p1, bloc);
+      f1 = Expression::make_unary(OPERATOR_MULT, f1, bloc);
+      f1 = Expression::make_field_reference(f1, field_index, bloc);
+
+      Expression* f2 = Expression::make_temporary_reference(p2, bloc);
+      f2 = Expression::make_unary(OPERATOR_MULT, f2, bloc);
+      f2 = Expression::make_field_reference(f2, field_index, bloc);
+
+      Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
+
+      // If the values are not equal, return false.
+      gogo->start_block(bloc);
+      Expression_list* vals = new Expression_list();
+      vals->push_back(Expression::make_boolean(false, bloc));
+      Statement* s = Statement::make_return_statement(vals, bloc);
+      gogo->add_statement(s);
+      Block* then_block = gogo->finish_block(bloc);
+
+      s = Statement::make_if_statement(cond, then_block, NULL, bloc);
+      gogo->add_statement(s);
+    }
+
+  // All the fields are equal, so return true.
+  Expression_list* vals = new Expression_list();
+  vals->push_back(Expression::make_boolean(true, bloc));
+  Statement* s = Statement::make_return_statement(vals, bloc);
+  gogo->add_statement(s);
+}
+
 // Reflection string.
 
 void
@@ -4014,6 +4719,24 @@ Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
   ret->push_back('e');
 }
 
+// If the offset of field INDEX in the backend implementation can be
+// determined, set *POFFSET to the offset in bytes and return true.
+// Otherwise, return false.
+
+bool
+Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
+                                 unsigned int* poffset)
+{
+  Btype* btype = this->get_backend(gogo);
+  if (!this->is_backend_type_size_known(gogo))
+    return false;
+  size_t offset = gogo->backend()->type_field_offset(btype, index);
+  *poffset = static_cast<unsigned int>(offset);
+  if (*poffset != offset)
+    return false;
+  return true;
+}
+
 // Export.
 
 void
@@ -4021,7 +4744,7 @@ Struct_type::do_export(Export* exp) const
 {
   exp->write_c_string("struct { ");
   const Struct_field_list* fields = this->fields_;
-  gcc_assert(fields != NULL);
+  go_assert(fields != NULL);
   for (Struct_field_list::const_iterator p = fields->begin();
        p != fields->end();
        ++p)
@@ -4038,8 +4761,8 @@ Struct_type::do_export(Export* exp) const
       if (p->has_tag())
        {
          exp->write_c_string(" ");
-         Expression* expr = Expression::make_string(p->tag(),
-                                                    BUILTINS_LOCATION);
+         Expression* expr =
+            Expression::make_string(p->tag(), Linemap::predeclared_location());
          expr->export_expression(exp);
          delete expr;
        }
@@ -4077,7 +4800,7 @@ Struct_type::do_import(Import* imp)
              imp->advance(1);
              Expression* expr = Expression::import_expression(imp);
              String_expression* sexpr = expr->string_expression();
-             gcc_assert(sexpr != NULL);
+             go_assert(sexpr != NULL);
              sf.set_tag(sexpr->val());
              delete sexpr;
            }
@@ -4097,7 +4820,7 @@ Struct_type::do_import(Import* imp)
 
 Struct_type*
 Type::make_struct_type(Struct_field_list* fields,
-                      source_location location)
+                      Location location)
 {
   return new Struct_type(fields, location);
 }
@@ -4107,9 +4830,10 @@ Type::make_struct_type(Struct_field_list* fields,
 // Whether two array types are identical.
 
 bool
-Array_type::is_identical(const Array_type* t) const
+Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
 {
-  if (!Type::are_identical(this->element_type(), t->element_type(), NULL))
+  if (!Type::are_identical(this->element_type(), t->element_type(),
+                          errors_are_identical, NULL))
     return false;
 
   Expression* l1 = this->length();
@@ -4147,6 +4871,25 @@ Array_type::is_identical(const Array_type* t) const
   return false;
 }
 
+// If this type needs explicit comparison and hash functions, create
+// them now.  It would be a bit better to do this only if the
+// functions are needed, but they will be static so the backend can
+// discard them if they are not used.
+
+void
+Array_type::finalize_methods(Gogo* gogo)
+{
+  if (this->length_ != NULL
+      && !this->length_->is_nil_expression()
+      && !this->compare_is_identity()
+      && this->is_comparable())
+    {
+      Named_object* hash_fn;
+      Named_object* equal_fn;
+      this->type_functions(gogo, NULL, NULL, NULL, &hash_fn, &equal_fn);
+    }
+}
+
 // Traversal.
 
 int
@@ -4167,6 +4910,10 @@ Array_type::verify_length()
 {
   if (this->length_ == NULL)
     return true;
+
+  Type_context context(Type::lookup_integer_type("int"), false);
+  this->length_->determine_type(&context);
+
   if (!this->length_->is_constant())
     {
       error_at(this->length_->location(), "array bound is not constant");
@@ -4174,30 +4921,23 @@ Array_type::verify_length()
     }
 
   mpz_t val;
-
-  Type* t = this->length_->type();
-  if (t->integer_type() != NULL)
-    {
-      Type* vt;
-      mpz_init(val);
-      if (!this->length_->integer_constant_value(true, val, &vt))
-       {
-         error_at(this->length_->location(),
-                  "array bound is not constant");
-         mpz_clear(val);
-         return false;
-       }
-    }
-  else if (t->float_type() != NULL)
+  mpz_init(val);
+  Type* vt;
+  if (!this->length_->integer_constant_value(true, val, &vt))
     {
-      Type* vt;
       mpfr_t fval;
       mpfr_init(fval);
       if (!this->length_->float_constant_value(fval, &vt))
        {
-         error_at(this->length_->location(),
-                  "array bound is not constant");
+         if (this->length_->type()->integer_type() != NULL
+             || this->length_->type()->float_type() != NULL)
+           error_at(this->length_->location(),
+                    "array bound is not constant");
+         else
+           error_at(this->length_->location(),
+                    "array bound is not numeric");
          mpfr_clear(fval);
+         mpz_clear(val);
          return false;
        }
       if (!mpfr_integer_p(fval))
@@ -4205,18 +4945,13 @@ Array_type::verify_length()
          error_at(this->length_->location(),
                   "array bound truncated to integer");
          mpfr_clear(fval);
+         mpz_clear(val);
          return false;
        }
       mpz_init(val);
       mpfr_get_z(val, fval, GMP_RNDN);
       mpfr_clear(fval);
     }
-  else
-    {
-      if (!t->is_error_type())
-       error_at(this->length_->location(), "array bound is not numeric");
-      return false;
-    }
 
   if (mpz_sgn(val) < 0)
     {
@@ -4263,41 +4998,196 @@ Array_type::do_hash_for_method(Gogo* gogo) const
   return this->element_type_->hash_for_method(gogo) + 1;
 }
 
-// See if the expression passed to make is suitable.  The first
-// argument is required, and gives the length.  An optional second
-// argument is permitted for the capacity.
+// Write the hash function for an array which can not use the identify
+// function.
 
-bool
-Array_type::do_check_make_expression(Expression_list* args,
-                                    source_location location)
+void
+Array_type::write_hash_function(Gogo* gogo, Named_type* name,
+                               Function_type* hash_fntype,
+                               Function_type* equal_fntype)
+{
+  Location bloc = Linemap::predeclared_location();
+
+  // The pointer to the array that we are going to hash.  This is an
+  // argument to the hash function we are implementing here.
+  Named_object* key_arg = gogo->lookup("key", NULL);
+  go_assert(key_arg != NULL);
+  Type* key_arg_type = key_arg->var_value()->type();
+
+  Type* uintptr_type = Type::lookup_integer_type("uintptr");
+
+  // Get a 0.
+  mpz_t ival;
+  mpz_init_set_ui(ival, 0);
+  Expression* zero = Expression::make_integer(&ival, uintptr_type, bloc);
+  mpz_clear(ival);
+
+  // Make a temporary to hold the return value, initialized to 0.
+  Temporary_statement* retval = Statement::make_temporary(uintptr_type, zero,
+                                                         bloc);
+  gogo->add_statement(retval);
+
+  // Make a temporary to hold the key as a uintptr.
+  Expression* ref = Expression::make_var_reference(key_arg, bloc);
+  ref = Expression::make_cast(uintptr_type, ref, bloc);
+  Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
+                                                      bloc);
+  gogo->add_statement(key);
+
+  // Loop over the array elements.
+  // for i = range a
+  Type* int_type = Type::lookup_integer_type("int");
+  Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
+  gogo->add_statement(index);
+
+  Expression* iref = Expression::make_temporary_reference(index, bloc);
+  Expression* aref = Expression::make_var_reference(key_arg, bloc);
+  Type* pt = Type::make_pointer_type(name != NULL
+                                    ? static_cast<Type*>(name)
+                                    : static_cast<Type*>(this));
+  aref = Expression::make_cast(pt, aref, bloc);
+  For_range_statement* for_range = Statement::make_for_range_statement(iref,
+                                                                      NULL,
+                                                                      aref,
+                                                                      bloc);
+
+  gogo->start_block(bloc);
+
+  // Multiply retval by 33.
+  mpz_init_set_ui(ival, 33);
+  Expression* i33 = Expression::make_integer(&ival, uintptr_type, bloc);
+  mpz_clear(ival);
+
+  ref = Expression::make_temporary_reference(retval, bloc);
+  Statement* s = Statement::make_assignment_operation(OPERATOR_MULTEQ, ref,
+                                                     i33, bloc);
+  gogo->add_statement(s);
+
+  // Get the hash function for the element type.
+  Named_object* hash_fn;
+  Named_object* equal_fn;
+  this->element_type_->type_functions(gogo, this->element_type_->named_type(),
+                                     hash_fntype, equal_fntype, &hash_fn,
+                                     &equal_fn);
+
+  // Get a pointer to this element in the loop.
+  Expression* subkey = Expression::make_temporary_reference(key, bloc);
+  subkey = Expression::make_cast(key_arg_type, subkey, bloc);
+
+  // Get the size of each element.
+  Expression* ele_size = Expression::make_type_info(this->element_type_,
+                                                   Expression::TYPE_INFO_SIZE);
+
+  // Get the hash of this element.
+  Expression_list* args = new Expression_list();
+  args->push_back(subkey);
+  args->push_back(ele_size);
+  Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
+  Expression* call = Expression::make_call(func, args, false, bloc);
+
+  // Add the element's hash value to retval.
+  Temporary_reference_expression* tref =
+    Expression::make_temporary_reference(retval, bloc);
+  tref->set_is_lvalue();
+  s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, call, bloc);
+  gogo->add_statement(s);
+
+  // Increase the element pointer.
+  tref = Expression::make_temporary_reference(key, bloc);
+  tref->set_is_lvalue();
+  s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
+                                          bloc);
+
+  Block* statements = gogo->finish_block(bloc);
+
+  for_range->add_statements(statements);
+  gogo->add_statement(for_range);
+
+  // Return retval to the caller of the hash function.
+  Expression_list* vals = new Expression_list();
+  ref = Expression::make_temporary_reference(retval, bloc);
+  vals->push_back(ref);
+  s = Statement::make_return_statement(vals, bloc);
+  gogo->add_statement(s);
+}
+
+// Write the equality function for an array which can not use the
+// identity function.
+
+void
+Array_type::write_equal_function(Gogo* gogo, Named_type* name)
 {
-  gcc_assert(this->length_ == NULL);
-  if (args == NULL || args->empty())
-    {
-      error_at(location, "length required when allocating a slice");
-      return false;
-    }
-  else if (args->size() > 2)
-    {
-      error_at(location, "too many expressions passed to make");
-      return false;
-    }
-  else
-    {
-      if (!Type::check_int_value(args->front(),
-                                _("bad length when making slice"), location))
-       return false;
+  Location bloc = Linemap::predeclared_location();
 
-      if (args->size() > 1)
-       {
-         if (!Type::check_int_value(args->back(),
-                                    _("bad capacity when making slice"),
-                                    location))
-           return false;
-       }
+  // The pointers to the arrays we are going to compare.
+  Named_object* key1_arg = gogo->lookup("key1", NULL);
+  Named_object* key2_arg = gogo->lookup("key2", NULL);
+  go_assert(key1_arg != NULL && key2_arg != NULL);
 
-      return true;
-    }
+  // Build temporaries for the keys with the right types.
+  Type* pt = Type::make_pointer_type(name != NULL
+                                    ? static_cast<Type*>(name)
+                                    : static_cast<Type*>(this));
+
+  Expression* ref = Expression::make_var_reference(key1_arg, bloc);
+  ref = Expression::make_unsafe_cast(pt, ref, bloc);
+  Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
+  gogo->add_statement(p1);
+
+  ref = Expression::make_var_reference(key2_arg, bloc);
+  ref = Expression::make_unsafe_cast(pt, ref, bloc);
+  Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
+  gogo->add_statement(p2);
+
+  // Loop over the array elements.
+  // for i = range a
+  Type* int_type = Type::lookup_integer_type("int");
+  Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
+  gogo->add_statement(index);
+
+  Expression* iref = Expression::make_temporary_reference(index, bloc);
+  Expression* aref = Expression::make_temporary_reference(p1, bloc);
+  For_range_statement* for_range = Statement::make_for_range_statement(iref,
+                                                                      NULL,
+                                                                      aref,
+                                                                      bloc);
+
+  gogo->start_block(bloc);
+
+  // Compare element in P1 and P2.
+  Expression* e1 = Expression::make_temporary_reference(p1, bloc);
+  e1 = Expression::make_unary(OPERATOR_MULT, e1, bloc);
+  ref = Expression::make_temporary_reference(index, bloc);
+  e1 = Expression::make_array_index(e1, ref, NULL, bloc);
+
+  Expression* e2 = Expression::make_temporary_reference(p2, bloc);
+  e2 = Expression::make_unary(OPERATOR_MULT, e2, bloc);
+  ref = Expression::make_temporary_reference(index, bloc);
+  e2 = Expression::make_array_index(e2, ref, NULL, bloc);
+
+  Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
+
+  // If the elements are not equal, return false.
+  gogo->start_block(bloc);
+  Expression_list* vals = new Expression_list();
+  vals->push_back(Expression::make_boolean(false, bloc));
+  Statement* s = Statement::make_return_statement(vals, bloc);
+  gogo->add_statement(s);
+  Block* then_block = gogo->finish_block(bloc);
+
+  s = Statement::make_if_statement(cond, then_block, NULL, bloc);
+  gogo->add_statement(s);
+
+  Block* statements = gogo->finish_block(bloc);
+
+  for_range->add_statements(statements);
+  gogo->add_statement(for_range);
+
+  // All the elements are equal, so return true.
+  vals = new Expression_list();
+  vals->push_back(Expression::make_boolean(true, bloc));
+  s = Statement::make_return_statement(vals, bloc);
+  gogo->add_statement(s);
 }
 
 // Get a tree for the length of a fixed array.  The length may be
@@ -4306,7 +5196,7 @@ Array_type::do_check_make_expression(Expression_list* args,
 tree
 Array_type::get_length_tree(Gogo* gogo)
 {
-  gcc_assert(this->length_ != NULL);
+  go_assert(this->length_ != NULL);
   if (this->length_tree_ == NULL_TREE)
     {
       mpz_t val;
@@ -4318,282 +5208,100 @@ Array_type::get_length_tree(Gogo* gogo)
            t = Type::lookup_integer_type("int");
          else if (t->is_abstract())
            t = t->make_non_abstract_type();
-         tree tt = t->get_tree(gogo);
+         tree tt = type_to_tree(t->get_backend(gogo));
          this->length_tree_ = Expression::integer_constant_tree(val, tt);
          mpz_clear(val);
        }
-      else
-       {
-         mpz_clear(val);
-
-         // Make up a translation context for the array length
-         // expression.  FIXME: This won't work in general.
-         Translate_context context(gogo, NULL, NULL, NULL_TREE);
-         tree len = this->length_->get_tree(&context);
-         len = convert_to_integer(integer_type_node, len);
-         this->length_tree_ = save_expr(len);
-       }
-    }
-  return this->length_tree_;
-}
-
-// Get a tree for the type of this array.  A fixed array is simply
-// represented as ARRAY_TYPE with the appropriate index--i.e., it is
-// just like an array in C.  An open array is a struct with three
-// fields: a data pointer, the length, and the capacity.
-
-tree
-Array_type::do_get_tree(Gogo* gogo)
-{
-  if (this->length_ == NULL)
-    {
-      tree struct_type = gogo->slice_type_tree(void_type_node);
-      return this->fill_in_tree(gogo, struct_type);
-    }
-  else
-    {
-      tree element_type_tree = this->element_type_->get_tree(gogo);
-      tree length_tree = this->get_length_tree(gogo);
-      if (element_type_tree == error_mark_node
-         || length_tree == error_mark_node)
-       return error_mark_node;
-
-      length_tree = fold_convert(sizetype, length_tree);
-
-      // build_index_type takes the maximum index, which is one less
-      // than the length.
-      tree index_type = build_index_type(fold_build2(MINUS_EXPR, sizetype,
-                                                    length_tree,
-                                                    size_one_node));
-
-      return build_array_type(element_type_tree, index_type);
-    }
-}
-
-// Fill in the fields for a slice type.  This is used for named slice
-// types.
-
-tree
-Array_type::fill_in_tree(Gogo* gogo, tree struct_type)
-{
-  gcc_assert(this->length_ == NULL);
-
-  tree element_type_tree = this->element_type_->get_tree(gogo);
-  tree field = TYPE_FIELDS(struct_type);
-  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
-  gcc_assert(POINTER_TYPE_P(TREE_TYPE(field))
-            && TREE_TYPE(TREE_TYPE(field)) == void_type_node);
-  TREE_TYPE(field) = build_pointer_type(element_type_tree);
-
-  return struct_type;
-}
-
-// Return an initializer for an array type.
-
-tree
-Array_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
-{
-  if (this->length_ == NULL)
-    {
-      // Open array.
-
-      if (is_clear)
-       return NULL;
-
-      gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
-
-      VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
-
-      for (tree field = TYPE_FIELDS(type_tree);
-          field != NULL_TREE;
-          field = DECL_CHAIN(field))
-       {
-         constructor_elt* elt = VEC_quick_push(constructor_elt, init,
-                                               NULL);
-         elt->index = field;
-         elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
-       }
-
-      tree ret = build_constructor(type_tree, init);
-      TREE_CONSTANT(ret) = 1;
-      return ret;
-    }
-  else
-    {
-      // Fixed array.
+      else
+       {
+         mpz_clear(val);
 
-      tree value = this->element_type_->get_init_tree(gogo, is_clear);
-      if (value == NULL)
-       return NULL;
-
-      tree length_tree = this->get_length_tree(gogo);
-      length_tree = fold_convert(sizetype, length_tree);
-      tree range = build2(RANGE_EXPR, sizetype, size_zero_node,
-                         fold_build2(MINUS_EXPR, sizetype,
-                                     length_tree, size_one_node));
-      tree ret = build_constructor_single(type_tree, range, value);
-      if (TREE_CONSTANT(value))
-       TREE_CONSTANT(ret) = 1;
-      return ret;
+         // Make up a translation context for the array length
+         // expression.  FIXME: This won't work in general.
+         Translate_context context(gogo, NULL, NULL, NULL);
+         tree len = this->length_->get_tree(&context);
+         if (len != error_mark_node)
+           {
+             len = convert_to_integer(integer_type_node, len);
+             len = save_expr(len);
+           }
+         this->length_tree_ = len;
+       }
     }
+  return this->length_tree_;
 }
 
-// Handle the builtin make function for a slice.
+// Get the backend representation of the fields of a slice.  This is
+// not declared in types.h so that types.h doesn't have to #include
+// backend.h.
+//
+// We use int for the count and capacity fields.  This matches 6g.
+// The language more or less assumes that we can't allocate space of a
+// size which does not fit in int.
 
-tree
-Array_type::do_make_expression_tree(Translate_context* context,
-                                   Expression_list* args,
-                                   source_location location)
+static void
+get_backend_slice_fields(Gogo* gogo, Array_type* type,
+                        std::vector<Backend::Btyped_identifier>* bfields)
 {
-  gcc_assert(this->length_ == NULL);
-
-  Gogo* gogo = context->gogo();
-  tree type_tree = this->get_tree(gogo);
-  if (type_tree == error_mark_node)
-    return error_mark_node;
-
-  tree values_field = TYPE_FIELDS(type_tree);
-  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field)),
-                   "__values") == 0);
-
-  tree count_field = DECL_CHAIN(values_field);
-  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field)),
-                   "__count") == 0);
-
-  tree element_type_tree = this->element_type_->get_tree(gogo);
-  if (element_type_tree == error_mark_node)
-    return error_mark_node;
-  tree element_size_tree = TYPE_SIZE_UNIT(element_type_tree);
+  bfields->resize(3);
 
-  tree value = this->element_type_->get_init_tree(gogo, true);
+  Type* pet = Type::make_pointer_type(type->element_type());
+  Btype* pbet = pet->get_backend(gogo);
+  Location ploc = Linemap::predeclared_location();
 
-  // The first argument is the number of elements, the optional second
-  // argument is the capacity.
-  gcc_assert(args != NULL && args->size() >= 1 && args->size() <= 2);
+  Backend::Btyped_identifier* p = &(*bfields)[0];
+  p->name = "__values";
+  p->btype = pbet;
+  p->location = ploc;
 
-  tree length_tree = args->front()->get_tree(context);
-  if (length_tree == error_mark_node)
-    return error_mark_node;
-  if (!DECL_P(length_tree))
-    length_tree = save_expr(length_tree);
-  if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree)))
-    length_tree = convert_to_integer(TREE_TYPE(count_field), length_tree);
-
-  tree bad_index = Expression::check_bounds(length_tree,
-                                           TREE_TYPE(count_field),
-                                           NULL_TREE, location);
-
-  length_tree = fold_convert_loc(location, TREE_TYPE(count_field), length_tree);
-  tree capacity_tree;
-  if (args->size() == 1)
-    capacity_tree = length_tree;
-  else
-    {
-      capacity_tree = args->back()->get_tree(context);
-      if (capacity_tree == error_mark_node)
-       return error_mark_node;
-      if (!DECL_P(capacity_tree))
-       capacity_tree = save_expr(capacity_tree);
-      if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree)))
-       capacity_tree = convert_to_integer(TREE_TYPE(count_field),
-                                          capacity_tree);
-
-      bad_index = Expression::check_bounds(capacity_tree,
-                                          TREE_TYPE(count_field),
-                                          bad_index, location);
-
-      tree chktype = (((TYPE_SIZE(TREE_TYPE(capacity_tree))
-                       > TYPE_SIZE(TREE_TYPE(length_tree)))
-                      || ((TYPE_SIZE(TREE_TYPE(capacity_tree))
-                           == TYPE_SIZE(TREE_TYPE(length_tree)))
-                          && TYPE_UNSIGNED(TREE_TYPE(capacity_tree))))
-                     ? TREE_TYPE(capacity_tree)
-                     : TREE_TYPE(length_tree));
-      tree chk = fold_build2_loc(location, LT_EXPR, boolean_type_node,
-                                fold_convert_loc(location, chktype,
-                                                 capacity_tree),
-                                fold_convert_loc(location, chktype,
-                                                 length_tree));
-      if (bad_index == NULL_TREE)
-       bad_index = chk;
-      else
-       bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
-                                   bad_index, chk);
-
-      capacity_tree = fold_convert_loc(location, TREE_TYPE(count_field),
-                                      capacity_tree);
-    }
-
-  tree size_tree = fold_build2_loc(location, MULT_EXPR, sizetype,
-                                  element_size_tree,
-                                  fold_convert_loc(location, sizetype,
-                                                   capacity_tree));
-
-  tree chk = fold_build2_loc(location, TRUTH_AND_EXPR, boolean_type_node,
-                            fold_build2_loc(location, GT_EXPR,
-                                            boolean_type_node,
-                                            fold_convert_loc(location,
-                                                             sizetype,
-                                                             capacity_tree),
-                                            size_zero_node),
-                            fold_build2_loc(location, LT_EXPR,
-                                            boolean_type_node,
-                                            size_tree, element_size_tree));
-  if (bad_index == NULL_TREE)
-    bad_index = chk;
-  else
-    bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
-                               bad_index, chk);
+  Type* int_type = Type::lookup_integer_type("int");
 
-  tree space = context->gogo()->allocate_memory(this->element_type_,
-                                               size_tree, location);
+  p = &(*bfields)[1];
+  p->name = "__count";
+  p->btype = int_type->get_backend(gogo);
+  p->location = ploc;
 
-  if (value != NULL_TREE)
-    space = save_expr(space);
+  p = &(*bfields)[2];
+  p->name = "__capacity";
+  p->btype = int_type->get_backend(gogo);
+  p->location = ploc;
+}
 
-  space = fold_convert(TREE_TYPE(values_field), space);
+// Get a tree for the type of this array.  A fixed array is simply
+// represented as ARRAY_TYPE with the appropriate index--i.e., it is
+// just like an array in C.  An open array is a struct with three
+// fields: a data pointer, the length, and the capacity.
 
-  if (bad_index != NULL_TREE && bad_index != boolean_false_node)
+Btype*
+Array_type::do_get_backend(Gogo* gogo)
+{
+  if (this->length_ == NULL)
     {
-      tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS,
-                                      location);
-      space = build2(COMPOUND_EXPR, TREE_TYPE(space),
-                    build3(COND_EXPR, void_type_node,
-                           bad_index, crash, NULL_TREE),
-                    space);
+      std::vector<Backend::Btyped_identifier> bfields;
+      get_backend_slice_fields(gogo, this, &bfields);
+      return gogo->backend()->struct_type(bfields);
     }
-
-  tree constructor = gogo->slice_constructor(type_tree, space, length_tree,
-                                            capacity_tree);
-
-  if (value == NULL_TREE)
+  else
     {
-      // The array contents are zero initialized.
-      return constructor;
+      Btype* element = this->get_backend_element(gogo);
+      Bexpression* len = this->get_backend_length(gogo);
+      return gogo->backend()->array_type(element, len);
     }
+}
 
-  // The elements must be initialized.
-
-  tree max = fold_build2_loc(location, MINUS_EXPR, TREE_TYPE(count_field),
-                            capacity_tree,
-                            fold_convert_loc(location, TREE_TYPE(count_field),
-                                             integer_one_node));
-
-  tree array_type = build_array_type(element_type_tree,
-                                    build_index_type(max));
-
-  tree value_pointer = fold_convert_loc(location,
-                                       build_pointer_type(array_type),
-                                       space);
+// Return the backend representation of the element type.
+Btype*
+Array_type::get_backend_element(Gogo* gogo)
+{
+  return this->element_type_->get_backend(gogo);
+}
 
-  tree range = build2(RANGE_EXPR, sizetype, size_zero_node, max);
-  tree space_init = build_constructor_single(array_type, range, value);
+// Return the backend representation of the length.
 
-  return build2(COMPOUND_EXPR, TREE_TYPE(space),
-               build2(MODIFY_EXPR, void_type_node,
-                      build_fold_indirect_ref(value_pointer),
-                      space_init),
-               constructor);
+Bexpression*
+Array_type::get_backend_length(Gogo* gogo)
+{
+  return tree_to_expr(this->get_length_tree(gogo));
 }
 
 // Return a tree for a pointer to the values in ARRAY.
@@ -4612,7 +5320,7 @@ Array_type::value_pointer_tree(Gogo*, tree array) const
     {
       // Open array.
       tree field = TYPE_FIELDS(TREE_TYPE(array));
-      gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
+      go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
                        "__values") == 0);
       ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
                        NULL_TREE);
@@ -4640,10 +5348,10 @@ Array_type::length_tree(Gogo* gogo, tree array)
   // This is an open array.  We need to read the length field.
 
   tree type = TREE_TYPE(array);
-  gcc_assert(TREE_CODE(type) == RECORD_TYPE);
+  go_assert(TREE_CODE(type) == RECORD_TYPE);
 
   tree field = DECL_CHAIN(TYPE_FIELDS(type));
-  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
+  go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
 
   tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
   if (TREE_CONSTANT(array))
@@ -4663,10 +5371,10 @@ Array_type::capacity_tree(Gogo* gogo, tree array)
   // This is an open array.  We need to read the capacity field.
 
   tree type = TREE_TYPE(array);
-  gcc_assert(TREE_CODE(type) == RECORD_TYPE);
+  go_assert(TREE_CODE(type) == RECORD_TYPE);
 
   tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
-  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
+  go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
 
   return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
 }
@@ -4713,9 +5421,10 @@ Array_type::make_array_type_descriptor_type()
       Type* uintptr_type = Type::lookup_integer_type("uintptr");
 
       Struct_type* sf =
-       Type::make_builtin_struct_type(3,
+       Type::make_builtin_struct_type(4,
                                       "", tdt,
                                       "elem", ptdt,
+                                      "slice", ptdt,
                                       "len", uintptr_type);
 
       ret = Type::make_builtin_named_type("ArrayType", sf);
@@ -4762,7 +5471,7 @@ Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 Expression*
 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* atdt = Array_type::make_array_type_descriptor_type();
 
@@ -4772,21 +5481,26 @@ Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
   vals->reserve(3);
 
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "commonType");
+  go_assert(p->is_field_name("commonType"));
   vals->push_back(this->type_descriptor_constructor(gogo,
                                                    RUNTIME_TYPE_KIND_ARRAY,
                                                    name, NULL, true));
 
   ++p;
-  gcc_assert(p->field_name() == "elem");
+  go_assert(p->is_field_name("elem"));
   vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "len");
-  vals->push_back(this->length_);
+  go_assert(p->is_field_name("slice"));
+  Type* slice_type = Type::make_array_type(this->element_type_, NULL);
+  vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p->is_field_name("len"));
+  vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
+
+  ++p;
+  go_assert(p == fields->end());
 
   return Expression::make_struct_composite_literal(atdt, vals, bloc);
 }
@@ -4796,7 +5510,7 @@ Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
 Expression*
 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* stdt = Array_type::make_slice_type_descriptor_type();
 
@@ -4806,17 +5520,17 @@ Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
   vals->reserve(2);
 
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "commonType");
+  go_assert(p->is_field_name("commonType"));
   vals->push_back(this->type_descriptor_constructor(gogo,
                                                    RUNTIME_TYPE_KIND_SLICE,
                                                    name, NULL, true));
 
   ++p;
-  gcc_assert(p->field_name() == "elem");
+  go_assert(p->is_field_name("elem"));
   vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p == fields->end());
 
   return Expression::make_struct_composite_literal(stdt, vals, bloc);
 }
@@ -4908,8 +5622,8 @@ Map_type::do_traverse(Traverse* traverse)
 bool
 Map_type::do_verify()
 {
-  if (this->key_type_->struct_type() != NULL
-      || this->key_type_->array_type() != NULL)
+  // The runtime support uses "map[void]void".
+  if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
     {
       error_at(this->location_, "invalid map key type");
       return false;
@@ -4920,10 +5634,12 @@ Map_type::do_verify()
 // Whether two map types are identical.
 
 bool
-Map_type::is_identical(const Map_type* t) const
+Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
 {
-  return (Type::are_identical(this->key_type(), t->key_type(), NULL)
-         && Type::are_identical(this->val_type(), t->val_type(), NULL));
+  return (Type::are_identical(this->key_type(), t->key_type(),
+                             errors_are_identical, NULL)
+         && Type::are_identical(this->val_type(), t->val_type(),
+                                errors_are_identical, NULL));
 }
 
 // Hash code.
@@ -4936,146 +5652,46 @@ Map_type::do_hash_for_method(Gogo* gogo) const
          + 2);
 }
 
-// Check that a call to the builtin make function is valid.  For a map
-// the optional argument is the number of spaces to preallocate for
-// values.
-
-bool
-Map_type::do_check_make_expression(Expression_list* args,
-                                  source_location location)
-{
-  if (args != NULL && !args->empty())
-    {
-      if (!Type::check_int_value(args->front(), _("bad size when making map"),
-                                location))
-       return false;
-      else if (args->size() > 1)
-       {
-         error_at(location, "too many arguments when making map");
-         return false;
-       }
-    }
-  return true;
-}
-
-// Get a tree for a map type.  A map type is represented as a pointer
-// to a struct.  The struct is __go_map in libgo/map.h.
+// Get the backend representation for a map type.  A map type is
+// represented as a pointer to a struct.  The struct is __go_map in
+// libgo/map.h.
 
-tree
-Map_type::do_get_tree(Gogo* gogo)
+Btype*
+Map_type::do_get_backend(Gogo* gogo)
 {
-  static tree type_tree;
-  if (type_tree == NULL_TREE)
+  static Btype* backend_map_type;
+  if (backend_map_type == NULL)
     {
-      tree struct_type = make_node(RECORD_TYPE);
+      std::vector<Backend::Btyped_identifier> bfields(4);
 
-      tree map_descriptor_type = gogo->map_descriptor_type();
-      tree const_map_descriptor_type =
-       build_qualified_type(map_descriptor_type, TYPE_QUAL_CONST);
-      tree name = get_identifier("__descriptor");
-      tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
-                             build_pointer_type(const_map_descriptor_type));
-      DECL_CONTEXT(field) = struct_type;
-      TYPE_FIELDS(struct_type) = field;
-      tree last_field = field;
+      Location bloc = Linemap::predeclared_location();
 
-      name = get_identifier("__element_count");
-      field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
-      DECL_CONTEXT(field) = struct_type;
-      DECL_CHAIN(last_field) = field;
-      last_field = field;
+      Type* pdt = Type::make_type_descriptor_ptr_type();
+      bfields[0].name = "__descriptor";
+      bfields[0].btype = pdt->get_backend(gogo);
+      bfields[0].location = bloc;
 
-      name = get_identifier("__bucket_count");
-      field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
-      DECL_CONTEXT(field) = struct_type;
-      DECL_CHAIN(last_field) = field;
-      last_field = field;
-
-      name = get_identifier("__buckets");
-      field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
-                        build_pointer_type(ptr_type_node));
-      DECL_CONTEXT(field) = struct_type;
-      DECL_CHAIN(last_field) = field;
-
-      layout_type(struct_type);
-
-      // Give the struct a name for better debugging info.
-      name = get_identifier("__go_map");
-      tree type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
-                                 struct_type);
-      DECL_ARTIFICIAL(type_decl) = 1;
-      TYPE_NAME(struct_type) = type_decl;
-      go_preserve_from_gc(type_decl);
-      rest_of_decl_compilation(type_decl, 1, 0);
-
-      type_tree = build_pointer_type(struct_type);
-      go_preserve_from_gc(type_tree);
-    }
-
-  return type_tree;
-}
-
-// Initialize a map.
-
-tree
-Map_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
-{
-  if (is_clear)
-    return NULL;
-  return fold_convert(type_tree, null_pointer_node);
-}
+      Type* uintptr_type = Type::lookup_integer_type("uintptr");
+      bfields[1].name = "__element_count";
+      bfields[1].btype = uintptr_type->get_backend(gogo);
+      bfields[1].location = bloc;
 
-// Return an expression for a newly allocated map.
+      bfields[2].name = "__bucket_count";
+      bfields[2].btype = bfields[1].btype;
+      bfields[2].location = bloc;
 
-tree
-Map_type::do_make_expression_tree(Translate_context* context,
-                                 Expression_list* args,
-                                 source_location location)
-{
-  tree bad_index = NULL_TREE;
+      Btype* bvt = gogo->backend()->void_type();
+      Btype* bpvt = gogo->backend()->pointer_type(bvt);
+      Btype* bppvt = gogo->backend()->pointer_type(bpvt);
+      bfields[3].name = "__buckets";
+      bfields[3].btype = bppvt;
+      bfields[3].location = bloc;
 
-  tree expr_tree;
-  if (args == NULL || args->empty())
-    expr_tree = size_zero_node;
-  else
-    {
-      expr_tree = args->front()->get_tree(context);
-      if (expr_tree == error_mark_node)
-       return error_mark_node;
-      if (!DECL_P(expr_tree))
-       expr_tree = save_expr(expr_tree);
-      if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
-       expr_tree = convert_to_integer(sizetype, expr_tree);
-      bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
-                                          location);
-    }
-
-  tree map_type = this->get_tree(context->gogo());
-
-  static tree new_map_fndecl;
-  tree ret = Gogo::call_builtin(&new_map_fndecl,
-                               location,
-                               "__go_new_map",
-                               2,
-                               map_type,
-                               TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type))),
-                               context->gogo()->map_descriptor(this),
-                               sizetype,
-                               expr_tree);
-  // This can panic if the capacity is out of range.
-  TREE_NOTHROW(new_map_fndecl) = 0;
-
-  if (bad_index == NULL_TREE)
-    return ret;
-  else
-    {
-      tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS,
-                                      location);
-      return build2(COMPOUND_EXPR, TREE_TYPE(ret),
-                   build3(COND_EXPR, void_type_node,
-                          bad_index, crash, NULL_TREE),
-                   ret);
+      Btype *bt = gogo->backend()->struct_type(bfields);
+      bt = gogo->backend()->named_type("__go_map", bt, bloc);
+      backend_map_type = gogo->backend()->pointer_type(bt);
     }
+  return backend_map_type;
 }
 
 // The type of a map type descriptor.
@@ -5106,7 +5722,7 @@ Map_type::make_map_type_descriptor_type()
 Expression*
 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* mtdt = Map_type::make_map_type_descriptor_type();
 
@@ -5116,25 +5732,148 @@ Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   vals->reserve(3);
 
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "commonType");
+  go_assert(p->is_field_name("commonType"));
   vals->push_back(this->type_descriptor_constructor(gogo,
                                                    RUNTIME_TYPE_KIND_MAP,
                                                    name, NULL, true));
 
   ++p;
-  gcc_assert(p->field_name() == "key");
+  go_assert(p->is_field_name("key"));
   vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "elem");
+  go_assert(p->is_field_name("elem"));
   vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p == fields->end());
 
   return Expression::make_struct_composite_literal(mtdt, vals, bloc);
 }
 
+// A mapping from map types to map descriptors.
+
+Map_type::Map_descriptors Map_type::map_descriptors;
+
+// Build a map descriptor for this type.  Return a pointer to it.
+
+tree
+Map_type::map_descriptor_pointer(Gogo* gogo, Location location)
+{
+  Bvariable* bvar = this->map_descriptor(gogo);
+  tree var_tree = var_to_tree(bvar);
+  if (var_tree == error_mark_node)
+    return error_mark_node;
+  return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
+}
+
+// Build a map descriptor for this type.
+
+Bvariable*
+Map_type::map_descriptor(Gogo* gogo)
+{
+  std::pair<Map_type*, Bvariable*> val(this, NULL);
+  std::pair<Map_type::Map_descriptors::iterator, bool> ins =
+    Map_type::map_descriptors.insert(val);
+  if (!ins.second)
+    return ins.first->second;
+
+  Type* key_type = this->key_type_;
+  Type* val_type = this->val_type_;
+
+  // The map entry type is a struct with three fields.  Build that
+  // struct so that we can get the offsets of the key and value within
+  // a map entry.  The first field should technically be a pointer to
+  // this type itself, but since we only care about field offsets we
+  // just use pointer to bool.
+  Type* pbool = Type::make_pointer_type(Type::make_boolean_type());
+  Struct_type* map_entry_type =
+    Type::make_builtin_struct_type(3,
+                                  "__next", pbool,
+                                  "__key", key_type,
+                                  "__val", val_type);
+
+  Type* map_descriptor_type = Map_type::make_map_descriptor_type();
+
+  const Struct_field_list* fields =
+    map_descriptor_type->struct_type()->fields();
+
+  Expression_list* vals = new Expression_list();
+  vals->reserve(4);
+
+  Location bloc = Linemap::predeclared_location();
+
+  Struct_field_list::const_iterator p = fields->begin();
+
+  go_assert(p->is_field_name("__map_descriptor"));
+  vals->push_back(Expression::make_type_descriptor(this, bloc));
+
+  ++p;
+  go_assert(p->is_field_name("__entry_size"));
+  Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
+  vals->push_back(Expression::make_type_info(map_entry_type, type_info));
+
+  Struct_field_list::const_iterator pf = map_entry_type->fields()->begin();
+  ++pf;
+  go_assert(pf->is_field_name("__key"));
+
+  ++p;
+  go_assert(p->is_field_name("__key_offset"));
+  vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
+
+  ++pf;
+  go_assert(pf->is_field_name("__val"));
+
+  ++p;
+  go_assert(p->is_field_name("__val_offset"));
+  vals->push_back(Expression::make_struct_field_offset(map_entry_type, &*pf));
+
+  ++p;
+  go_assert(p == fields->end());
+
+  Expression* initializer =
+    Expression::make_struct_composite_literal(map_descriptor_type, vals, bloc);
+
+  std::string mangled_name = "__go_map_" + this->mangled_name(gogo);
+  Btype* map_descriptor_btype = map_descriptor_type->get_backend(gogo);
+  Bvariable* bvar = gogo->backend()->immutable_struct(mangled_name, true,
+                                                     map_descriptor_btype,
+                                                     bloc);
+
+  Translate_context context(gogo, NULL, NULL, NULL);
+  context.set_is_const();
+  Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
+
+  gogo->backend()->immutable_struct_set_init(bvar, mangled_name, true,
+                                            map_descriptor_btype, bloc,
+                                            binitializer);
+
+  ins.first->second = bvar;
+  return bvar;
+}
+
+// Build the type of a map descriptor.  This must match the struct
+// __go_map_descriptor in libgo/runtime/map.h.
+
+Type*
+Map_type::make_map_descriptor_type()
+{
+  static Type* ret;
+  if (ret == NULL)
+    {
+      Type* ptdt = Type::make_type_descriptor_ptr_type();
+      Type* uintptr_type = Type::lookup_integer_type("uintptr");
+      Struct_type* sf =
+       Type::make_builtin_struct_type(4,
+                                      "__map_descriptor", ptdt,
+                                      "__entry_size", uintptr_type,
+                                      "__key_offset", uintptr_type,
+                                      "__val_offset", uintptr_type);
+      ret = Type::make_builtin_named_type("__go_map_descriptor", sf);
+    }
+  return ret;
+}
+
 // Reflection string for a map.
 
 void
@@ -5142,7 +5881,7 @@ Map_type::do_reflection(Gogo* gogo, std::string* ret) const
 {
   ret->append("map[");
   this->append_reflection(this->key_type_, gogo, ret);
-  ret->append("] ");
+  ret->append("]");
   this->append_reflection(this->val_type_, gogo, ret);
 }
 
@@ -5183,7 +5922,7 @@ Map_type::do_import(Import* imp)
 // Make a map type.
 
 Map_type*
-Type::make_map_type(Type* key_type, Type* val_type, source_location location)
+Type::make_map_type(Type* key_type, Type* val_type, Location location)
 {
   return new Map_type(key_type, val_type, location);
 }
@@ -5208,122 +5947,33 @@ Channel_type::do_hash_for_method(Gogo* gogo) const
 // Whether this type is the same as T.
 
 bool
-Channel_type::is_identical(const Channel_type* t) const
+Channel_type::is_identical(const Channel_type* t,
+                          bool errors_are_identical) const
 {
-  if (!Type::are_identical(this->element_type(), t->element_type(), NULL))
+  if (!Type::are_identical(this->element_type(), t->element_type(),
+                          errors_are_identical, NULL))
     return false;
   return (this->may_send_ == t->may_send_
          && this->may_receive_ == t->may_receive_);
 }
 
-// Check whether the parameters for a call to the builtin function
-// make are OK for a channel.  A channel can take an optional single
-// parameter which is the buffer size.
-
-bool
-Channel_type::do_check_make_expression(Expression_list* args,
-                                     source_location location)
-{
-  if (args != NULL && !args->empty())
-    {
-      if (!Type::check_int_value(args->front(),
-                                _("bad buffer size when making channel"),
-                                location))
-       return false;
-      else if (args->size() > 1)
-       {
-         error_at(location, "too many arguments when making channel");
-         return false;
-       }
-    }
-  return true;
-}
-
 // Return the tree for a channel type.  A channel is a pointer to a
 // __go_channel struct.  The __go_channel struct is defined in
 // libgo/runtime/channel.h.
 
-tree
-Channel_type::do_get_tree(Gogo*)
-{
-  static tree type_tree;
-  if (type_tree == NULL_TREE)
-    {
-      tree ret = make_node(RECORD_TYPE);
-      TYPE_NAME(ret) = get_identifier("__go_channel");
-      TYPE_STUB_DECL(ret) = build_decl(BUILTINS_LOCATION, TYPE_DECL, NULL_TREE,
-                                      ret);
-      type_tree = build_pointer_type(ret);
-      go_preserve_from_gc(type_tree);
-    }
-  return type_tree;
-}
-
-// Initialize a channel variable.
-
-tree
-Channel_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
-{
-  if (is_clear)
-    return NULL;
-  return fold_convert(type_tree, null_pointer_node);
-}
-
-// Handle the builtin function make for a channel.
-
-tree
-Channel_type::do_make_expression_tree(Translate_context* context,
-                                     Expression_list* args,
-                                     source_location location)
+Btype*
+Channel_type::do_get_backend(Gogo* gogo)
 {
-  Gogo* gogo = context->gogo();
-  tree channel_type = this->get_tree(gogo);
-
-  tree element_tree = this->element_type_->get_tree(gogo);
-  tree element_size_tree = size_in_bytes(element_tree);
-
-  tree bad_index = NULL_TREE;
-
-  tree expr_tree;
-  if (args == NULL || args->empty())
-    expr_tree = size_zero_node;
-  else
-    {
-      expr_tree = args->front()->get_tree(context);
-      if (expr_tree == error_mark_node)
-       return error_mark_node;
-      if (!DECL_P(expr_tree))
-       expr_tree = save_expr(expr_tree);
-      if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
-       expr_tree = convert_to_integer(sizetype, expr_tree);
-      bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
-                                          location);
-    }
-
-  static tree new_channel_fndecl;
-  tree ret = Gogo::call_builtin(&new_channel_fndecl,
-                               location,
-                               "__go_new_channel",
-                               2,
-                               channel_type,
-                               sizetype,
-                               element_size_tree,
-                               sizetype,
-                               expr_tree);
-  // This can panic if the capacity is out of range.
-  TREE_NOTHROW(new_channel_fndecl) = 0;
-
-  if (bad_index == NULL_TREE)
-    return ret;
-  else
+  static Btype* backend_channel_type;
+  if (backend_channel_type == NULL)
     {
-      tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS,
-                                      location);
-      return build2(COMPOUND_EXPR, TREE_TYPE(ret),
-                   build3(COND_EXPR, void_type_node,
-                          bad_index, crash, NULL_TREE),
-                   ret);
+      std::vector<Backend::Btyped_identifier> bfields;
+      Btype* bt = gogo->backend()->struct_type(bfields);
+      bt = gogo->backend()->named_type("__go_channel", bt,
+                                       Linemap::predeclared_location());
+      backend_channel_type = gogo->backend()->pointer_type(bt);
     }
+  return backend_channel_type;
 }
 
 // Build a type descriptor for a channel type.
@@ -5356,7 +6006,7 @@ Channel_type::make_chan_type_descriptor_type()
 Expression*
 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* ctdt = Channel_type::make_chan_type_descriptor_type();
 
@@ -5366,17 +6016,17 @@ Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   vals->reserve(3);
 
   Struct_field_list::const_iterator p = fields->begin();
-  gcc_assert(p->field_name() == "commonType");
+  go_assert(p->is_field_name("commonType"));
   vals->push_back(this->type_descriptor_constructor(gogo,
                                                    RUNTIME_TYPE_KIND_CHAN,
                                                    name, NULL, true));
 
   ++p;
-  gcc_assert(p->field_name() == "elem");
+  go_assert(p->is_field_name("elem"));
   vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
 
   ++p;
-  gcc_assert(p->field_name() == "dir");
+  go_assert(p->is_field_name("dir"));
   // These bits must match the ones in libgo/runtime/go-type.h.
   int val = 0;
   if (this->may_receive_)
@@ -5389,7 +6039,7 @@ Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   mpz_clear(iv);
 
   ++p;
-  gcc_assert(p == fields->end());
+  go_assert(p == fields->end());
 
   return Expression::make_struct_composite_literal(ctdt, vals, bloc);
 }
@@ -5494,6 +6144,7 @@ Interface_type::finalize_methods()
 {
   if (this->methods_ == NULL)
     return;
+  std::vector<Named_type*> seen;
   bool is_recursive = false;
   size_t from = 0;
   size_t to = 0;
@@ -5502,12 +6153,26 @@ Interface_type::finalize_methods()
       const Typed_identifier* p = &this->methods_->at(from);
       if (!p->name().empty())
        {
-         if (from != to)
-           this->methods_->set(to, *p);
+         size_t i;
+         for (i = 0; i < to; ++i)
+           {
+             if (this->methods_->at(i).name() == p->name())
+               {
+                 error_at(p->location(), "duplicate method %qs",
+                          Gogo::message_name(p->name()).c_str());
+                 break;
+               }
+           }
+         if (i == to)
+           {
+             if (from != to)
+               this->methods_->set(to, *p);
+             ++to;
+           }
          ++from;
-         ++to;
          continue;
        }
+
       Interface_type* it = p->type()->interface_type();
       if (it == NULL)
        {
@@ -5525,6 +6190,27 @@ Interface_type::finalize_methods()
          ++from;
          continue;
        }
+
+      Named_type* nt = p->type()->named_type();
+      if (nt != NULL)
+       {
+         std::vector<Named_type*>::const_iterator q;
+         for (q = seen.begin(); q != seen.end(); ++q)
+           {
+             if (*q == nt)
+               {
+                 error_at(p->location(), "inherited interface loop");
+                 break;
+               }
+           }
+         if (q != seen.end())
+           {
+             ++from;
+             continue;
+           }
+         seen.push_back(nt);
+       }
+
       const Typed_identifier_list* methods = it->methods();
       if (methods == NULL)
        {
@@ -5535,7 +6221,31 @@ Interface_type::finalize_methods()
           q != methods->end();
           ++q)
        {
-         if (q->name().empty() || this->find_method(q->name()) == NULL)
+         if (q->name().empty())
+           {
+             if (q->type()->forwarded() == p->type()->forwarded())
+               error_at(p->location(), "interface inheritance loop");
+             else
+               {
+                 size_t i;
+                 for (i = from + 1; i < this->methods_->size(); ++i)
+                   {
+                     const Typed_identifier* r = &this->methods_->at(i);
+                     if (r->name().empty()
+                         && r->type()->forwarded() == q->type()->forwarded())
+                       {
+                         error_at(p->location(),
+                                  "inherited interface listed twice");
+                         break;
+                       }
+                   }
+                 if (i == this->methods_->size())
+                   this->methods_->push_back(Typed_identifier(q->name(),
+                                                              q->type(),
+                                                              p->location()));
+               }
+           }
+         else if (this->find_method(q->name()) == NULL)
            this->methods_->push_back(Typed_identifier(q->name(), q->type(),
                                                       p->location()));
          else
@@ -5579,14 +6289,14 @@ Interface_type::find_method(const std::string& name) const
 size_t
 Interface_type::method_index(const std::string& name) const
 {
-  gcc_assert(this->methods_ != NULL);
+  go_assert(this->methods_ != NULL);
   size_t ret = 0;
   for (Typed_identifier_list::const_iterator p = this->methods_->begin();
        p != this->methods_->end();
        ++p, ++ret)
     if (p->name() == name)
       return ret;
-  gcc_unreachable();
+  go_unreachable();
 }
 
 // Return whether NAME is an unexported method, for better error
@@ -5613,7 +6323,8 @@ Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
 // Whether this type is identical with T.
 
 bool
-Interface_type::is_identical(const Interface_type* t) const
+Interface_type::is_identical(const Interface_type* t,
+                            bool errors_are_identical) const
 {
   // We require the same methods with the same types.  The methods
   // have already been sorted.
@@ -5628,7 +6339,8 @@ Interface_type::is_identical(const Interface_type* t) const
       if (p1 == this->methods()->end())
        return false;
       if (p1->name() != p2->name()
-         || !Type::are_identical(p1->type(), p2->type(), NULL))
+         || !Type::are_identical(p1->type(), p2->type(),
+                                 errors_are_identical, NULL))
        return false;
     }
   if (p1 != this->methods()->end())
@@ -5667,7 +6379,7 @@ Interface_type::is_compatible_for_assign(const Interface_type* t,
        }
 
       std::string subreason;
-      if (!Type::are_identical(p->type(), m->type(), &subreason))
+      if (!Type::are_identical(p->type(), m->type(), true, &subreason))
        {
          if (reason != NULL)
            {
@@ -5800,9 +6512,9 @@ Interface_type::implements_interface(const Type* t, std::string* reason) const
 
       Function_type *p_fn_type = p->type()->function_type();
       Function_type* m_fn_type = m->type()->function_type();
-      gcc_assert(p_fn_type != NULL && m_fn_type != NULL);
+      go_assert(p_fn_type != NULL && m_fn_type != NULL);
       std::string subreason;
-      if (!p_fn_type->is_identical(m_fn_type, true, &subreason))
+      if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
        {
          if (reason != NULL)
            {
@@ -5842,120 +6554,97 @@ Interface_type::implements_interface(const Type* t, std::string* reason) const
   return true;
 }
 
-// Return a tree for an interface type.  An interface is a pointer to
-// a struct.  The struct has three fields.  The first field is a
-// pointer to the type descriptor for the dynamic type of the object.
-// The second field is a pointer to a table of methods for the
-// interface to be used with the object.  The third field is the value
-// of the object itself.
+// Return the backend representation of the empty interface type.  We
+// use the same struct for all empty interfaces.
 
-tree
-Interface_type::do_get_tree(Gogo* gogo)
+Btype*
+Interface_type::get_backend_empty_interface_type(Gogo* gogo)
 {
-  if (this->methods_ == NULL)
+  static Btype* empty_interface_type;
+  if (empty_interface_type == NULL)
     {
-      // At the tree level, use the same type for all empty
-      // interfaces.  This lets us assign them to each other directly
-      // without triggering GIMPLE type errors.
-      tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
-      dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
-      static tree empty_interface;
-      return Gogo::builtin_struct(&empty_interface, "__go_empty_interface",
-                                 NULL_TREE, 2,
-                                 "__type_descriptor",
-                                 dtype,
-                                 "__object",
-                                 ptr_type_node);
-    }
+      std::vector<Backend::Btyped_identifier> bfields(2);
 
-  return this->fill_in_tree(gogo, make_node(RECORD_TYPE));
+      Location bloc = Linemap::predeclared_location();
+
+      Type* pdt = Type::make_type_descriptor_ptr_type();
+      bfields[0].name = "__type_descriptor";
+      bfields[0].btype = pdt->get_backend(gogo);
+      bfields[0].location = bloc;
+
+      Type* vt = Type::make_pointer_type(Type::make_void_type());
+      bfields[1].name = "__object";
+      bfields[1].btype = vt->get_backend(gogo);
+      bfields[1].location = bloc;
+
+      empty_interface_type = gogo->backend()->struct_type(bfields);
+    }
+  return empty_interface_type;
 }
 
-// Fill in the tree for an interface type.  This is used for named
-// interface types.
+// Return the fields of a non-empty interface type.  This is not
+// declared in types.h so that types.h doesn't have to #include
+// backend.h.
 
-tree
-Interface_type::fill_in_tree(Gogo* gogo, tree type)
+static void
+get_backend_interface_fields(Gogo* gogo, Interface_type* type,
+                            std::vector<Backend::Btyped_identifier>* bfields)
 {
-  gcc_assert(this->methods_ != NULL);
-
-  // Build the type of the table of methods.
+  Location loc = type->location();
 
-  tree method_table = make_node(RECORD_TYPE);
+  std::vector<Backend::Btyped_identifier> mfields(type->methods()->size() + 1);
 
-  // The first field is a pointer to the type descriptor.
-  tree name_tree = get_identifier("__type_descriptor");
-  tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
-  dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
-  tree field = build_decl(this->location_, FIELD_DECL, name_tree, dtype);
-  DECL_CONTEXT(field) = method_table;
-  TYPE_FIELDS(method_table) = field;
+  Type* pdt = Type::make_type_descriptor_ptr_type();
+  mfields[0].name = "__type_descriptor";
+  mfields[0].btype = pdt->get_backend(gogo);
+  mfields[0].location = loc;
 
   std::string last_name = "";
-  tree* pp = &DECL_CHAIN(field);
-  for (Typed_identifier_list::const_iterator p = this->methods_->begin();
-       p != this->methods_->end();
-       ++p)
-    {
-      std::string name = Gogo::unpack_hidden_name(p->name());
-      name_tree = get_identifier_with_length(name.data(), name.length());
-      tree field_type = p->type()->get_tree(gogo);
-      if (field_type == error_mark_node)
-       return error_mark_node;
-      field = build_decl(this->location_, FIELD_DECL, name_tree, field_type);
-      DECL_CONTEXT(field) = method_table;
-      *pp = field;
-      pp = &DECL_CHAIN(field);
+  size_t i = 1;
+  for (Typed_identifier_list::const_iterator p = type->methods()->begin();
+       p != type->methods()->end();
+       ++p, ++i)
+    {
+      mfields[i].name = Gogo::unpack_hidden_name(p->name());
+      mfields[i].btype = p->type()->get_backend(gogo);
+      mfields[i].location = loc;
       // Sanity check: the names should be sorted.
-      gcc_assert(p->name() > last_name);
+      go_assert(p->name() > last_name);
       last_name = p->name();
     }
-  layout_type(method_table);
-
-  tree mtype = build_pointer_type(method_table);
-
-  tree field_trees = NULL_TREE;
-  pp = &field_trees;
 
-  name_tree = get_identifier("__methods");
-  field = build_decl(this->location_, FIELD_DECL, name_tree, mtype);
-  DECL_CONTEXT(field) = type;
-  *pp = field;
-  pp = &DECL_CHAIN(field);
+  Btype* methods = gogo->backend()->struct_type(mfields);
 
-  name_tree = get_identifier("__object");
-  field = build_decl(this->location_, FIELD_DECL, name_tree, ptr_type_node);
-  DECL_CONTEXT(field) = type;
-  *pp = field;
+  bfields->resize(2);
 
-  TYPE_FIELDS(type) = field_trees;
+  (*bfields)[0].name = "__methods";
+  (*bfields)[0].btype = gogo->backend()->pointer_type(methods);
+  (*bfields)[0].location = loc;
 
-  layout_type(type);
-
-  return type;
+  Type* vt = Type::make_pointer_type(Type::make_void_type());
+  (*bfields)[1].name = "__object";
+  (*bfields)[1].btype = vt->get_backend(gogo);
+  (*bfields)[1].location = Linemap::predeclared_location();
 }
 
-// Initialization value.
+// Return a tree for an interface type.  An interface is a pointer to
+// a struct.  The struct has three fields.  The first field is a
+// pointer to the type descriptor for the dynamic type of the object.
+// The second field is a pointer to a table of methods for the
+// interface to be used with the object.  The third field is the value
+// of the object itself.
 
-tree
-Interface_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
+Btype*
+Interface_type::do_get_backend(Gogo* gogo)
 {
-  if (is_clear)
-    return NULL;
-
-  VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
-  for (tree field = TYPE_FIELDS(type_tree);
-       field != NULL_TREE;
-       field = DECL_CHAIN(field))
+  if (this->methods_ == NULL)
+    return Interface_type::get_backend_empty_interface_type(gogo);
+  else
     {
-      constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
-      elt->index = field;
-      elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
+      std::vector<Backend::Btyped_identifier> bfields;
+      get_backend_interface_fields(gogo, this, &bfields);
+      return gogo->backend()->struct_type(bfields);
     }
-
-  tree ret = build_constructor(type_tree, init);
-  TREE_CONSTANT(ret) = 1;
-  return ret;
 }
 
 // The type of an interface type descriptor.
@@ -5997,7 +6686,7 @@ Interface_type::make_interface_type_descriptor_type()
 Expression*
 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
-  source_location bloc = BUILTINS_LOCATION;
+  Location bloc = Linemap::predeclared_location();
 
   Type* itdt = Interface_type::make_interface_type_descriptor_type();
 
@@ -6007,13 +6696,13 @@ Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
   ivals->reserve(2);
 
   Struct_field_list::const_iterator pif = ifields->begin();
-  gcc_assert(pif->field_name() == "commonType");
-  ivals->push_back(this->type_descriptor_constructor(gogo,
-                                                    RUNTIME_TYPE_KIND_INTERFACE,
-                                                    name, NULL, true));
+  go_assert(pif->is_field_name("commonType"));
+  const int rt = RUNTIME_TYPE_KIND_INTERFACE;
+  ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
+                                                    true));
 
   ++pif;
-  gcc_assert(pif->field_name() == "methods");
+  go_assert(pif->is_field_name("methods"));
 
   Expression_list* methods = new Expression_list();
   if (this->methods_ != NULL && !this->methods_->empty())
@@ -6031,13 +6720,13 @@ Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
          mvals->reserve(3);
 
          Struct_field_list::const_iterator pmf = mfields->begin();
-         gcc_assert(pmf->field_name() == "name");
+         go_assert(pmf->is_field_name("name"));
          std::string s = Gogo::unpack_hidden_name(pm->name());
          Expression* e = Expression::make_string(s, bloc);
          mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
 
          ++pmf;
-         gcc_assert(pmf->field_name() == "pkgPath");
+         go_assert(pmf->is_field_name("pkgPath"));
          if (!Gogo::is_hidden_name(pm->name()))
            mvals->push_back(Expression::make_nil(bloc));
          else
@@ -6048,11 +6737,11 @@ Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
            }
 
          ++pmf;
-         gcc_assert(pmf->field_name() == "typ");
+         go_assert(pmf->is_field_name("typ"));
          mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
 
          ++pmf;
-         gcc_assert(pmf == mfields->end());
+         go_assert(pmf == mfields->end());
 
          e = Expression::make_struct_composite_literal(elemtype, mvals,
                                                        bloc);
@@ -6064,7 +6753,7 @@ Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
                                                            methods, bloc));
 
   ++pif;
-  gcc_assert(pif == ifields->end());
+  go_assert(pif == ifields->end());
 
   return Expression::make_struct_composite_literal(itdt, ivals, bloc);
 }
@@ -6077,21 +6766,31 @@ Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
   ret->append("interface {");
   if (this->methods_ != NULL)
     {
+      ret->push_back(' ');
       for (Typed_identifier_list::const_iterator p = this->methods_->begin();
           p != this->methods_->end();
           ++p)
        {
          if (p != this->methods_->begin())
-           ret->append(";");
-         ret->push_back(' ');
-         ret->append(Gogo::unpack_hidden_name(p->name()));
+           ret->append("; ");
+         if (!Gogo::is_hidden_name(p->name()))
+           ret->append(p->name());
+         else
+           {
+             // This matches what the gc compiler does.
+             std::string prefix = Gogo::hidden_name_prefix(p->name());
+             ret->append(prefix.substr(prefix.find('.') + 1));
+             ret->push_back('.');
+             ret->append(Gogo::unpack_hidden_name(p->name()));
+           }
          std::string sub = p->type()->reflection(gogo);
-         gcc_assert(sub.compare(0, 4, "func") == 0);
+         go_assert(sub.compare(0, 4, "func") == 0);
          sub = sub.substr(4);
          ret->append(sub);
        }
+      ret->push_back(' ');
     }
-  ret->append(" }");
+  ret->append("}");
 }
 
 // Mangled name.
@@ -6234,7 +6933,7 @@ Interface_type::do_import(Import* imp)
                                                     ptype, imp->location()));
              if (imp->peek_char() != ',')
                break;
-             gcc_assert(!is_varargs);
+             go_assert(!is_varargs);
              imp->require_c_string(", ");
            }
        }
@@ -6294,7 +6993,7 @@ Interface_type::do_import(Import* imp)
 
 Interface_type*
 Type::make_interface_type(Typed_identifier_list* methods,
-                         source_location location)
+                         Location location)
 {
   return new Interface_type(methods, location);
 }
@@ -6304,7 +7003,7 @@ Type::make_interface_type(Typed_identifier_list* methods,
 // Bind a method to an object.
 
 Expression*
-Method::bind_method(Expression* expr, source_location location) const
+Method::bind_method(Expression* expr, Location location) const
 {
   if (this->stub_ == NULL)
     {
@@ -6312,10 +7011,7 @@ Method::bind_method(Expression* expr, source_location location) const
       // the child class.
       return this->do_bind_method(expr, location);
     }
-
-  Expression* func = Expression::make_func_reference(this->stub_, NULL,
-                                                    location);
-  return Expression::make_bound_method(expr, func, location);
+  return Expression::make_bound_method(expr, this->stub_, location);
 }
 
 // Return the named object associated with a method.  This may only be
@@ -6341,12 +7037,12 @@ Named_method::do_type() const
   else if (this->named_object_->is_function_declaration())
     return this->named_object_->func_declaration_value()->type();
   else
-    gcc_unreachable();
+    go_unreachable();
 }
 
 // Return the location of the method receiver.
 
-source_location
+Location
 Named_method::do_receiver_location() const
 {
   return this->do_type()->receiver()->location();
@@ -6355,11 +7051,10 @@ Named_method::do_receiver_location() const
 // Bind a method to an object.
 
 Expression*
-Named_method::do_bind_method(Expression* expr, source_location location) const
+Named_method::do_bind_method(Expression* expr, Location location) const
 {
-  Expression* func = Expression::make_func_reference(this->named_object_, NULL,
-                                                    location);
-  Bound_method_expression* bme = Expression::make_bound_method(expr, func,
+  Named_object* no = this->named_object_;
+  Bound_method_expression* bme = Expression::make_bound_method(expr, no,
                                                               location);
   // If this is not a local method, and it does not use a stub, then
   // the real method expects a different type.  We need to cast the
@@ -6367,7 +7062,7 @@ Named_method::do_bind_method(Expression* expr, source_location location) const
   if (this->depth() > 0 && !this->needs_stub_method())
     {
       Function_type* ftype = this->do_type();
-      gcc_assert(ftype->is_method());
+      go_assert(ftype->is_method());
       Type* frtype = ftype->receiver()->type();
       bme->set_first_argument_type(frtype);
     }
@@ -6380,7 +7075,7 @@ Named_method::do_bind_method(Expression* expr, source_location location) const
 
 Expression*
 Interface_method::do_bind_method(Expression* expr,
-                                source_location location) const
+                                Location location) const
 {
   return Expression::make_interface_field_reference(expr, this->name_,
                                                    location);
@@ -6430,22 +7125,76 @@ Methods::count() const
   return ret;
 }
 
-// Class Named_type.
+// Class Named_type.
+
+// Return the name of the type.
+
+const std::string&
+Named_type::name() const
+{
+  return this->named_object_->name();
+}
+
+// Return the name of the type to use in an error message.
+
+std::string
+Named_type::message_name() const
+{
+  return this->named_object_->message_name();
+}
+
+// Return the base type for this type.  We have to be careful about
+// circular type definitions, which are invalid but may be seen here.
+
+Type*
+Named_type::named_base()
+{
+  if (this->seen_)
+    return this;
+  this->seen_ = true;
+  Type* ret = this->type_->base();
+  this->seen_ = false;
+  return ret;
+}
+
+const Type*
+Named_type::named_base() const
+{
+  if (this->seen_)
+    return this;
+  this->seen_ = true;
+  const Type* ret = this->type_->base();
+  this->seen_ = false;
+  return ret;
+}
 
-// Return the name of the type.
+// Return whether this is an error type.  We have to be careful about
+// circular type definitions, which are invalid but may be seen here.
 
-const std::string&
-Named_type::name() const
+bool
+Named_type::is_named_error_type() const
 {
-  return this->named_object_->name();
+  if (this->seen_)
+    return false;
+  this->seen_ = true;
+  bool ret = this->type_->is_error_type();
+  this->seen_ = false;
+  return ret;
 }
 
-// Return the name of the type to use in an error message.
+// Whether this type is comparable.  We have to be careful about
+// circular type definitions.
 
-std::string
-Named_type::message_name() const
+bool
+Named_type::named_type_is_comparable(std::string* reason) const
 {
-  return this->named_object_->message_name();
+  if (this->seen_)
+    return false;
+  this->seen_ = true;
+  bool ret = Type::are_compatible_for_comparison(true, this->type_,
+                                                this->type_, reason);
+  this->seen_ = false;
+  return ret;
 }
 
 // Add a method to this type.
@@ -6463,7 +7212,7 @@ Named_type::add_method(const std::string& name, Function* function)
 Named_object*
 Named_type::add_method_declaration(const std::string& name, Package* package,
                                   Function_type* type,
-                                  source_location location)
+                                  Location location)
 {
   if (this->local_methods_ == NULL)
     this->local_methods_ = new Bindings(NULL);
@@ -6523,6 +7272,23 @@ Named_type::is_unexported_local_method(Gogo* gogo,
 void
 Named_type::finalize_methods(Gogo* gogo)
 {
+  // If this type needs explicit comparison and hash functions, create
+  // them now.  It would be a bit better to do this only if the
+  // functions are needed, but they will be static so the backend can
+  // discard them if they are not used.
+  if ((this->struct_type() != NULL
+       || (this->array_type() != NULL && !this->is_slice_type()))
+      && !this->compare_is_identity()
+      && this->is_comparable())
+    {
+      Named_object* hash_fn;
+      Named_object* equal_fn;
+      this->type_functions(gogo, this, NULL, NULL, &hash_fn, &equal_fn);
+    }
+
+  if (this->all_methods_ != NULL)
+    return;
+
   if (this->local_methods_ != NULL
       && (this->points_to() != NULL || this->interface_type() != NULL))
     {
@@ -6558,7 +7324,7 @@ tree
 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
                                   bool is_pointer)
 {
-  gcc_assert(!interface->is_empty());
+  go_assert(!interface->is_empty());
 
   Interface_method_tables** pimt = (is_pointer
                                    ? &this->interface_method_tables_
@@ -6573,7 +7339,7 @@ Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
   if (ins.second)
     {
       // This is a new entry in the hash table.
-      gcc_assert(ins.first->second == NULL_TREE);
+      go_assert(ins.first->second == NULL_TREE);
       ins.first->second = gogo->interface_method_table_for_type(interface,
                                                                this,
                                                                is_pointer);
@@ -6582,7 +7348,7 @@ Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
   tree decl = ins.first->second;
   if (decl == error_mark_node)
     return error_mark_node;
-  gcc_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
+  go_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
   return build_fold_addr_expr(decl);
 }
 
@@ -6605,7 +7371,7 @@ Named_type::named_type_has_hidden_fields(std::string* reason) const
 class Find_type_use : public Traverse
 {
  public:
-  Find_type_use(Type* find_type)
+  Find_type_use(Named_type* find_type)
     : Traverse(traverse_types),
       find_type_(find_type), found_(false)
   { }
@@ -6621,7 +7387,7 @@ class Find_type_use : public Traverse
 
  private:
   // The type we are looking for.
-  Type* find_type_;
+  Named_type* find_type_;
   // Whether we found the type.
   bool found_;
 };
@@ -6631,16 +7397,17 @@ class Find_type_use : public Traverse
 int
 Find_type_use::type(Type* type)
 {
-  if (this->find_type_ == type)
+  if (type->named_type() != NULL && this->find_type_ == type->named_type())
     {
       this->found_ = true;
       return TRAVERSE_EXIT;
     }
+
   // It's OK if we see a reference to the type in any type which is
   // essentially a pointer: a pointer, a slice, a function, a map, or
   // a channel.
   if (type->points_to() != NULL
-      || type->is_open_array_type()
+      || type->is_slice_type()
       || type->function_type() != NULL
       || type->map_type() != NULL
       || type->channel_type() != NULL)
@@ -6669,6 +7436,42 @@ Find_type_use::type(Type* type)
       return TRAVERSE_SKIP_COMPONENTS;
     }
 
+  // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
+  // to convert TYPE to the backend representation before we convert
+  // FIND_TYPE_.
+  if (type->named_type() != NULL)
+    {
+      switch (type->base()->classification())
+       {
+       case Type::TYPE_ERROR:
+       case Type::TYPE_BOOLEAN:
+       case Type::TYPE_INTEGER:
+       case Type::TYPE_FLOAT:
+       case Type::TYPE_COMPLEX:
+       case Type::TYPE_STRING:
+       case Type::TYPE_NIL:
+         break;
+
+       case Type::TYPE_ARRAY:
+       case Type::TYPE_STRUCT:
+         this->find_type_->add_dependency(type->named_type());
+         break;
+
+       case Type::TYPE_VOID:
+       case Type::TYPE_SINK:
+       case Type::TYPE_FUNCTION:
+       case Type::TYPE_POINTER:
+       case Type::TYPE_CALL_MULTIPLE_RESULT:
+       case Type::TYPE_MAP:
+       case Type::TYPE_CHANNEL:
+       case Type::TYPE_INTERFACE:
+       case Type::TYPE_NAMED:
+       case Type::TYPE_FORWARD:
+       default:
+         go_unreachable();
+       }
+    }
+
   return TRAVERSE_CONTINUE;
 }
 
@@ -6694,9 +7497,8 @@ Named_type::do_verify()
   if (this->local_methods_ != NULL)
     {
       Struct_type* st = this->type_->struct_type();
-      Interface_type* it = this->type_->interface_type();
       bool found_dup = false;
-      if (st != NULL || it != NULL)
+      if (st != NULL)
        {
          for (Bindings::const_declarations_iterator p =
                 this->local_methods_->begin_declarations();
@@ -6711,13 +7513,6 @@ Named_type::do_verify()
                           Gogo::message_name(name).c_str());
                  found_dup = true;
                }
-             if (it != NULL && it->find_method(name) != NULL)
-               {
-                 error_at(p->second->location(),
-                          "method %qs redeclares interface method name",
-                          Gogo::message_name(name).c_str());
-                 found_dup = true;
-               }
            }
        }
       if (found_dup)
@@ -6727,6 +7522,33 @@ Named_type::do_verify()
   return true;
 }
 
+// Return whether this type is or contains a pointer.
+
+bool
+Named_type::do_has_pointer() const
+{
+  if (this->seen_)
+    return false;
+  this->seen_ = true;
+  bool ret = this->type_->has_pointer();
+  this->seen_ = false;
+  return ret;
+}
+
+// Return whether comparisons for this type can use the identity
+// function.
+
+bool
+Named_type::do_compare_is_identity() const
+{
+  if (this->seen_)
+    return false;
+  this->seen_ = true;
+  bool ret = this->type_->compare_is_identity();
+  this->seen_ = false;
+  return ret;
+}
+
 // Return a hash code.  This is used for method lookup.  We simply
 // hash on the name itself.
 
@@ -6760,21 +7582,118 @@ Named_type::do_hash_for_method(Gogo* gogo) const
   return ret;
 }
 
-// Get a tree for a named type.
+// Convert a named type to the backend representation.  In order to
+// get dependencies right, we fill in a dummy structure for this type,
+// then convert all the dependencies, then complete this type.  When
+// this function is complete, the size of the type is known.
 
-tree
-Named_type::do_get_tree(Gogo* gogo)
+void
+Named_type::convert(Gogo* gogo)
+{
+  if (this->is_error_ || this->is_converted_)
+    return;
+
+  this->create_placeholder(gogo);
+
+  // Convert all the dependencies.  If they refer indirectly back to
+  // this type, they will pick up the intermediate tree we just
+  // created.
+  for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
+       p != this->dependencies_.end();
+       ++p)
+    (*p)->convert(gogo);
+
+  // Complete this type.
+  Btype* bt = this->named_btype_;
+  Type* base = this->type_->base();
+  switch (base->classification())
+    {
+    case TYPE_VOID:
+    case TYPE_BOOLEAN:
+    case TYPE_INTEGER:
+    case TYPE_FLOAT:
+    case TYPE_COMPLEX:
+    case TYPE_STRING:
+    case TYPE_NIL:
+      break;
+
+    case TYPE_MAP:
+    case TYPE_CHANNEL:
+      break;
+
+    case TYPE_FUNCTION:
+    case TYPE_POINTER:
+      // The size of these types is already correct.  We don't worry
+      // about filling them in until later, when we also track
+      // circular references.
+      break;
+
+    case TYPE_STRUCT:
+      {
+       std::vector<Backend::Btyped_identifier> bfields;
+       get_backend_struct_fields(gogo, base->struct_type()->fields(),
+                                 &bfields);
+       if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
+         bt = gogo->backend()->error_type();
+      }
+      break;
+
+    case TYPE_ARRAY:
+      // Slice types were completed in create_placeholder.
+      if (!base->is_slice_type())
+       {
+         Btype* bet = base->array_type()->get_backend_element(gogo);
+         Bexpression* blen = base->array_type()->get_backend_length(gogo);
+         if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
+           bt = gogo->backend()->error_type();
+       }
+      break;
+
+    case TYPE_INTERFACE:
+      // Interface types were completed in create_placeholder.
+      break;
+
+    case TYPE_ERROR:
+      return;
+
+    default:
+    case TYPE_SINK:
+    case TYPE_CALL_MULTIPLE_RESULT:
+    case TYPE_NAMED:
+    case TYPE_FORWARD:
+      go_unreachable();
+    }
+
+  this->named_btype_ = bt;
+  this->is_converted_ = true;
+  this->is_placeholder_ = false;
+}
+
+// Create the placeholder for a named type.  This is the first step in
+// converting to the backend representation.
+
+void
+Named_type::create_placeholder(Gogo* gogo)
 {
   if (this->is_error_)
-    return error_mark_node;
+    this->named_btype_ = gogo->backend()->error_type();
+
+  if (this->named_btype_ != NULL)
+    return;
 
-  // Go permits types to refer to themselves in various ways.  Break
-  // the recursion here.
-  tree t;
-  switch (this->type_->forwarded()->classification())
+  // Create the structure for this type.  Note that because we call
+  // base() here, we don't attempt to represent a named type defined
+  // as another named type.  Instead both named types will point to
+  // different base representations.
+  Type* base = this->type_->base();
+  Btype* bt;
+  bool set_name = true;
+  switch (base->classification())
     {
     case TYPE_ERROR:
-      return error_mark_node;
+      this->is_error_ = true;
+      this->named_btype_ = gogo->backend()->error_type();
+      return;
 
     case TYPE_VOID:
     case TYPE_BOOLEAN:
@@ -6783,126 +7702,196 @@ Named_type::do_get_tree(Gogo* gogo)
     case TYPE_COMPLEX:
     case TYPE_STRING:
     case TYPE_NIL:
-      // These types can not refer to themselves.
+      // These are simple basic types, we can just create them
+      // directly.
+      bt = Type::get_named_base_btype(gogo, base);
+      break;
+
     case TYPE_MAP:
     case TYPE_CHANNEL:
-      // All maps and channels have the same type in GENERIC.
-      t = Type::get_named_type_tree(gogo, this->type_);
-      if (t == error_mark_node)
-       return error_mark_node;
-      // Build a copy to set TYPE_NAME.
-      t = build_variant_type_copy(t);
+      // All maps and channels have the same backend representation.
+      bt = Type::get_named_base_btype(gogo, base);
       break;
 
     case TYPE_FUNCTION:
-      // GENERIC can't handle a pointer to a function type whose
-      // return type is a pointer to the function type itself.  It
-      // does into infinite loops when walking the types.
-      if (this->seen_
-         && this->function_type()->results() != NULL
-         && this->function_type()->results()->size() == 1
-         && (this->function_type()->results()->front().type()->forwarded()
-             == this))
-       return ptr_type_node;
-      this->seen_ = true;
-      t = Type::get_named_type_tree(gogo, this->type_);
-      this->seen_ = false;
-      if (t == error_mark_node)
-       return error_mark_node;
-      t = build_variant_type_copy(t);
-      break;
-
     case TYPE_POINTER:
-      // GENERIC can't handle a pointer type which points to itself.
-      // It goes into infinite loops when walking the types.
-      if (this->seen_ && this->points_to()->forwarded() == this)
-       return ptr_type_node;
-      this->seen_ = true;
-      t = Type::get_named_type_tree(gogo, this->type_);
-      this->seen_ = false;
-      if (t == error_mark_node)
-       return error_mark_node;
-      t = build_variant_type_copy(t);
+      {
+       bool for_function = base->classification() == TYPE_FUNCTION;
+       bt = gogo->backend()->placeholder_pointer_type(this->name(),
+                                                      this->location_,
+                                                      for_function);
+       set_name = false;
+      }
       break;
 
     case TYPE_STRUCT:
-      if (this->named_tree_ != NULL_TREE)
-       return this->named_tree_;
-      t = make_node(RECORD_TYPE);
-      this->named_tree_ = t;
-      this->type_->struct_type()->fill_in_tree(gogo, t);
+      bt = gogo->backend()->placeholder_struct_type(this->name(),
+                                                   this->location_);
+      this->is_placeholder_ = true;
+      set_name = false;
       break;
 
     case TYPE_ARRAY:
-      if (!this->is_open_array_type())
-       t = Type::get_named_type_tree(gogo, this->type_);
+      if (base->is_slice_type())
+       bt = gogo->backend()->placeholder_struct_type(this->name(),
+                                                     this->location_);
       else
        {
-         if (this->named_tree_ != NULL_TREE)
-           return this->named_tree_;
-         t = gogo->slice_type_tree(void_type_node);
-         this->named_tree_ = t;
-         t = this->type_->array_type()->fill_in_tree(gogo, t);
+         bt = gogo->backend()->placeholder_array_type(this->name(),
+                                                      this->location_);
+         this->is_placeholder_ = true;
        }
-      if (t == error_mark_node)
-       return error_mark_node;
-      t = build_variant_type_copy(t);
+      set_name = false;
       break;
 
     case TYPE_INTERFACE:
-      if (this->type_->interface_type()->is_empty())
-       {
-         t = Type::get_named_type_tree(gogo, this->type_);
-         if (t == error_mark_node)
-           return error_mark_node;
-         t = build_variant_type_copy(t);
-       }
+      if (base->interface_type()->is_empty())
+       bt = Interface_type::get_backend_empty_interface_type(gogo);
       else
        {
-         if (this->named_tree_ != NULL_TREE)
-           return this->named_tree_;
-         t = make_node(RECORD_TYPE);
-         this->named_tree_ = t;
-         t = this->type_->interface_type()->fill_in_tree(gogo, t);
+         bt = gogo->backend()->placeholder_struct_type(this->name(),
+                                                       this->location_);
+         set_name = false;
        }
       break;
 
+    default:
+    case TYPE_SINK:
+    case TYPE_CALL_MULTIPLE_RESULT:
     case TYPE_NAMED:
-      {
-       // When a named type T1 is defined as another named type T2,
-       // the definition must simply be "type T1 T2".  If the
-       // definition of T2 may refer to T1, then we must simply
-       // return the type for T2 here.  It's not precisely correct,
-       // but it's as close as we can get with GENERIC.
-       bool was_seen = this->seen_;
-       this->seen_ = true;
-       t = Type::get_named_type_tree(gogo, this->type_);
-       this->seen_ = was_seen;
-       if (was_seen)
-         return t;
-       if (t == error_mark_node)
-         return error_mark_node;
-       t = build_variant_type_copy(t);
-      }
-      break;
-
     case TYPE_FORWARD:
-      // An undefined forwarding type.  Make sure the error is
-      // emitted.
-      this->type_->forward_declaration_type()->real_type();
-      return error_mark_node;
+      go_unreachable();
+    }
+
+  if (set_name)
+    bt = gogo->backend()->named_type(this->name(), bt, this->location_);
+
+  this->named_btype_ = bt;
+
+  if (base->is_slice_type())
+    {
+      // We do not record slices as dependencies of other types,
+      // because we can fill them in completely here with the final
+      // size.
+      std::vector<Backend::Btyped_identifier> bfields;
+      get_backend_slice_fields(gogo, base->array_type(), &bfields);
+      if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
+       this->named_btype_ = gogo->backend()->error_type();
+    }
+  else if (base->interface_type() != NULL
+          && !base->interface_type()->is_empty())
+    {
+      // We do not record interfaces as dependencies of other types,
+      // because we can fill them in completely here with the final
+      // size.
+      std::vector<Backend::Btyped_identifier> bfields;
+      get_backend_interface_fields(gogo, base->interface_type(), &bfields);
+      if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
+       this->named_btype_ = gogo->backend()->error_type();
+    }
+}
+
+// Get a tree for a named type.
+
+Btype*
+Named_type::do_get_backend(Gogo* gogo)
+{
+  if (this->is_error_)
+    return gogo->backend()->error_type();
+
+  Btype* bt = this->named_btype_;
+
+  if (!gogo->named_types_are_converted())
+    {
+      // We have not completed converting named types.  NAMED_BTYPE_
+      // is a placeholder and we shouldn't do anything further.
+      if (bt != NULL)
+       return bt;
+
+      // We don't build dependencies for types whose sizes do not
+      // change or are not relevant, so we may see them here while
+      // converting types.
+      this->create_placeholder(gogo);
+      bt = this->named_btype_;
+      go_assert(bt != NULL);
+      return bt;
+    }
+
+  // We are not converting types.  This should only be called if the
+  // type has already been converted.
+  if (!this->is_converted_)
+    {
+      go_assert(saw_errors());
+      return gogo->backend()->error_type();
+    }
+
+  go_assert(bt != NULL);
+
+  // Complete the tree.
+  Type* base = this->type_->base();
+  Btype* bt1;
+  switch (base->classification())
+    {
+    case TYPE_ERROR:
+      return gogo->backend()->error_type();
+
+    case TYPE_VOID:
+    case TYPE_BOOLEAN:
+    case TYPE_INTEGER:
+    case TYPE_FLOAT:
+    case TYPE_COMPLEX:
+    case TYPE_STRING:
+    case TYPE_NIL:
+    case TYPE_MAP:
+    case TYPE_CHANNEL:
+    case TYPE_STRUCT:
+    case TYPE_ARRAY:
+    case TYPE_INTERFACE:
+      return bt;
+
+    case TYPE_FUNCTION:
+      // Don't build a circular data structure.  GENERIC can't handle
+      // it.
+      if (this->seen_in_get_backend_)
+       {
+         this->is_circular_ = true;
+         return gogo->backend()->circular_pointer_type(bt, true);
+       }
+      this->seen_in_get_backend_ = true;
+      bt1 = Type::get_named_base_btype(gogo, base);
+      this->seen_in_get_backend_ = false;
+      if (this->is_circular_)
+       bt1 = gogo->backend()->circular_pointer_type(bt, true);
+      if (!gogo->backend()->set_placeholder_function_type(bt, bt1))
+       bt = gogo->backend()->error_type();
+      return bt;
+
+    case TYPE_POINTER:
+      // Don't build a circular data structure. GENERIC can't handle
+      // it.
+      if (this->seen_in_get_backend_)
+       {
+         this->is_circular_ = true;
+         return gogo->backend()->circular_pointer_type(bt, false);
+       }
+      this->seen_in_get_backend_ = true;
+      bt1 = Type::get_named_base_btype(gogo, base);
+      this->seen_in_get_backend_ = false;
+      if (this->is_circular_)
+       bt1 = gogo->backend()->circular_pointer_type(bt, false);
+      if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
+       bt = gogo->backend()->error_type();
+      return bt;
 
     default:
     case TYPE_SINK:
     case TYPE_CALL_MULTIPLE_RESULT:
-      gcc_unreachable();
+    case TYPE_NAMED:
+    case TYPE_FORWARD:
+      go_unreachable();
     }
 
-  tree id = this->named_object_->get_id(gogo);
-  tree decl = build_decl(this->location_, TYPE_DECL, id, t);
-  TYPE_NAME(t) = decl;
-
-  return t;
+  go_unreachable();
 }
 
 // Build a type descriptor for a named type.
@@ -6924,7 +7913,7 @@ Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 void
 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
 {
-  if (this->location() != BUILTINS_LOCATION)
+  if (!Linemap::is_predeclared_location(this->location()))
     {
       const Package* package = this->named_object_->package();
       if (package != NULL)
@@ -6948,8 +7937,8 @@ Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
 {
   Named_object* no = this->named_object_;
   std::string name;
-  if (this->location() == BUILTINS_LOCATION)
-    gcc_assert(this->in_function_ == NULL);
+  if (Linemap::is_predeclared_location(this->location()))
+    go_assert(this->in_function_ == NULL);
   else
     {
       const std::string& unique_prefix(no->package() == NULL
@@ -6995,7 +7984,7 @@ Named_type::import_named_type(Import* imp, Named_type** ptype)
   imp->require_c_string("type ");
   Type *type = imp->read_type();
   *ptype = type->named_type();
-  gcc_assert(*ptype != NULL);
+  go_assert(*ptype != NULL);
   imp->require_c_string(";\n");
 }
 
@@ -7038,7 +8027,7 @@ Named_type::do_export(Export* exp) const
 
 Named_type*
 Type::make_named_type(Named_object* named_object, Type* type,
-                     source_location location)
+                     Location location)
 {
   return new Named_type(named_object, type, location);
 }
@@ -7048,7 +8037,7 @@ Type::make_named_type(Named_object* named_object, Type* type,
 // all required stubs.
 
 void
-Type::finalize_methods(Gogo* gogo, const Type* type, source_location location,
+Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
                       Methods** all_methods)
 {
   *all_methods = NULL;
@@ -7224,7 +8213,13 @@ Type::add_interface_methods_for_type(const Type* type,
        ++pm)
     {
       Function_type* fntype = pm->type()->function_type();
-      gcc_assert(fntype != NULL && !fntype->is_method());
+      if (fntype == NULL)
+       {
+         // This is an error, but it should be reported elsewhere
+         // when we look at the methods for IT.
+         continue;
+       }
+      go_assert(!fntype->is_method());
       fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
       Method* m = new Interface_method(pm->name(), pm->location(), fntype,
                                       field_indexes, depth);
@@ -7242,7 +8237,7 @@ Type::add_interface_methods_for_type(const Type* type,
 
 void
 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
-                        source_location location)
+                        Location location)
 {
   if (methods == NULL)
     return;
@@ -7268,7 +8263,7 @@ Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
       Type* receiver_type = const_cast<Type*>(type);
       if (!m->is_value_method())
        receiver_type = Type::make_pointer_type(receiver_type);
-      source_location receiver_location = m->receiver_location();
+      Location receiver_location = m->receiver_location();
       Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
                                                        receiver_location);
 
@@ -7348,10 +8343,10 @@ Type::build_one_stub_method(Gogo* gogo, Method* method,
                            const char* receiver_name,
                            const Typed_identifier_list* params,
                            bool is_varargs,
-                           source_location location)
+                           Location location)
 {
   Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
-  gcc_assert(receiver_object != NULL);
+  go_assert(receiver_object != NULL);
 
   Expression* expr = Expression::make_var_reference(receiver_object, location);
   expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
@@ -7369,7 +8364,7 @@ Type::build_one_stub_method(Gogo* gogo, Method* method,
           ++p)
        {
          Named_object* param = gogo->lookup(p->name(), NULL);
-         gcc_assert(param != NULL);
+         go_assert(param != NULL);
          Expression* param_ref = Expression::make_var_reference(param,
                                                                 location);
          arguments->push_back(param_ref);
@@ -7377,12 +8372,13 @@ Type::build_one_stub_method(Gogo* gogo, Method* method,
     }
 
   Expression* func = method->bind_method(expr, location);
-  gcc_assert(func != NULL);
+  go_assert(func != NULL);
   Call_expression* call = Expression::make_call(func, arguments, is_varargs,
                                                location);
+  call->set_hidden_fields_are_ok();
   size_t count = call->result_count();
   if (count == 0)
-    gogo->add_statement(Statement::make_statement(call));
+    gogo->add_statement(Statement::make_statement(call, true));
   else
     {
       Expression_list* retvals = new Expression_list();
@@ -7393,10 +8389,13 @@ Type::build_one_stub_method(Gogo* gogo, Method* method,
          for (size_t i = 0; i < count; ++i)
            retvals->push_back(Expression::make_call_result(call, i));
        }
-      const Function* function = gogo->current_function()->func_value();
-      const Typed_identifier_list* results = function->type()->results();
-      Statement* retstat = Statement::make_return_statement(results, retvals,
-                                                           location);
+      Return_statement* retstat = Statement::make_return_statement(retvals,
+                                                                  location);
+
+      // We can return values with hidden fields from a stub.  This is
+      // necessary if the method is itself hidden.
+      retstat->set_hidden_fields_are_ok();
+
       gogo->add_statement(retstat);
     }
 }
@@ -7407,19 +8406,19 @@ Type::build_one_stub_method(Gogo* gogo, Method* method,
 Expression*
 Type::apply_field_indexes(Expression* expr,
                          const Method::Field_indexes* field_indexes,
-                         source_location location)
+                         Location location)
 {
   if (field_indexes == NULL)
     return expr;
   expr = Type::apply_field_indexes(expr, field_indexes->next, location);
   Struct_type* stype = expr->type()->deref()->struct_type();
-  gcc_assert(stype != NULL
+  go_assert(stype != NULL
             && field_indexes->field_index < stype->field_count());
   if (expr->type()->struct_type() == NULL)
     {
-      gcc_assert(expr->type()->points_to() != NULL);
+      go_assert(expr->type()->points_to() != NULL);
       expr = Expression::make_unary(OPERATOR_MULT, expr, location);
-      gcc_assert(expr->type()->struct_type() == stype);
+      go_assert(expr->type()->struct_type() == stype);
     }
   return Expression::make_field_reference(expr, field_indexes->field_index,
                                          location);
@@ -7436,7 +8435,7 @@ Type::method_expects_pointer(const Named_object* no)
   else if (no->is_function_declaration())
     fntype = no->func_declaration_value()->type();
   else
-    gcc_unreachable();
+    go_unreachable();
   return fntype->receiver()->type()->points_to() != NULL;
 }
 
@@ -7473,16 +8472,14 @@ Type::method_function(const Methods* methods, const std::string& name,
 Expression*
 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
                           const std::string& name,
-                          source_location location)
+                          Location location)
 {
-  if (type->is_error_type())
+  if (type->deref()->is_error_type())
     return Expression::make_error(location);
 
-  const Named_type* nt = type->named_type();
-  if (nt == NULL)
-    nt = type->deref()->named_type();
+  const Named_type* nt = type->deref()->named_type();
   const Struct_type* st = type->deref()->struct_type();
-  const Interface_type* it = type->deref()->interface_type();
+  const Interface_type* it = type->interface_type();
 
   // If this is a pointer to a pointer, then it is possible that the
   // pointed-to type has methods.
@@ -7494,31 +8491,33 @@ Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
     {
       expr = Expression::make_unary(OPERATOR_MULT, expr, location);
       type = type->points_to();
+      if (type->deref()->is_error_type())
+       return Expression::make_error(location);
       nt = type->points_to()->named_type();
       st = type->points_to()->struct_type();
-      it = type->points_to()->interface_type();
     }
 
   bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
                                  || expr->is_addressable());
+  std::vector<const Named_type*> seen;
   bool is_method = false;
   bool found_pointer_method = false;
   std::string ambig1;
   std::string ambig2;
-  if (Type::find_field_or_method(type, name, receiver_can_be_pointer, NULL,
-                                &is_method, &found_pointer_method,
-                                &ambig1, &ambig2))
+  if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
+                                &seen, NULL, &is_method,
+                                &found_pointer_method, &ambig1, &ambig2))
     {
       Expression* ret;
       if (!is_method)
        {
-         gcc_assert(st != NULL);
+         go_assert(st != NULL);
          if (type->struct_type() == NULL)
            {
-             gcc_assert(type->points_to() != NULL);
+             go_assert(type->points_to() != NULL);
              expr = Expression::make_unary(OPERATOR_MULT, expr,
                                            location);
-             gcc_assert(expr->type()->struct_type() == st);
+             go_assert(expr->type()->struct_type() == st);
            }
          ret = st->field_reference(expr, name, location);
        }
@@ -7533,22 +8532,21 @@ Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
          else if (st != NULL)
            m = st->method_function(name, NULL);
          else
-           gcc_unreachable();
-         gcc_assert(m != NULL);
+           go_unreachable();
+         go_assert(m != NULL);
          if (!m->is_value_method() && expr->type()->points_to() == NULL)
            expr = Expression::make_unary(OPERATOR_AND, expr, location);
          ret = m->bind_method(expr, location);
        }
-      gcc_assert(ret != NULL);
+      go_assert(ret != NULL);
       return ret;
     }
   else
     {
       if (!ambig1.empty())
        error_at(location, "%qs is ambiguous via %qs and %qs",
-                Gogo::message_name(name).c_str(),
-                Gogo::message_name(ambig1).c_str(),
-                Gogo::message_name(ambig2).c_str());
+                Gogo::message_name(name).c_str(), ambig1.c_str(),
+                ambig2.c_str());
       else if (found_pointer_method)
        error_at(location, "method requires a pointer");
       else if (nt == NULL && st == NULL && it == NULL)
@@ -7564,8 +8562,10 @@ Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
          else
            {
              std::string unpacked = Gogo::unpack_hidden_name(name);
+             seen.clear();
              is_unexported = Type::is_unexported_field_or_method(gogo, type,
-                                                                 unpacked);
+                                                                 unpacked,
+                                                                 &seen);
            }
          if (is_unexported)
            error_at(location, "reference to unexported field or method %qs",
@@ -7583,13 +8583,16 @@ Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
 // ambiguity.  If a method is found, sets *IS_METHOD to true;
 // otherwise, if a field is found, set it to false.  If
 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
-// whose address can not be taken.  When returning false, this sets
-// *FOUND_POINTER_METHOD if we found a method we couldn't use because
-// it requires a pointer.  LEVEL is used for recursive calls, and can
-// be NULL for a non-recursive call.  When this function returns false
-// because it finds that the name is ambiguous, it will store a path
-// to the ambiguous names in *AMBIG1 and *AMBIG2.  If the name is not
-// found at all, *AMBIG1 and *AMBIG2 will be unchanged.
+// whose address can not be taken.  SEEN is used to avoid infinite
+// recursion on invalid types.
+
+// When returning false, this sets *FOUND_POINTER_METHOD if we found a
+// method we couldn't use because it requires a pointer.  LEVEL is
+// used for recursive calls, and can be NULL for a non-recursive call.
+// When this function returns false because it finds that the name is
+// ambiguous, it will store a path to the ambiguous names in *AMBIG1
+// and *AMBIG2.  If the name is not found at all, *AMBIG1 and *AMBIG2
+// will be unchanged.
 
 // This function just returns whether or not there is a field or
 // method, and whether it is a field or method.  It doesn't build an
@@ -7602,6 +8605,7 @@ bool
 Type::find_field_or_method(const Type* type,
                           const std::string& name,
                           bool receiver_can_be_pointer,
+                          std::vector<const Named_type*>* seen,
                           int* level,
                           bool* is_method,
                           bool* found_pointer_method,
@@ -7628,10 +8632,21 @@ Type::find_field_or_method(const Type* type,
          // else.
          *found_pointer_method = true;
        }
+
+      for (std::vector<const Named_type*>::const_iterator p = seen->begin();
+          p != seen->end();
+          ++p)
+       {
+         if (*p == nt)
+           {
+             // We've already seen this type when searching for methods.
+             return false;
+           }
+       }
     }
 
   // Interface types can have methods.
-  const Interface_type* it = type->deref()->interface_type();
+  const Interface_type* it = type->interface_type();
   if (it != NULL && it->find_method(name) != NULL)
     {
       *is_method = true;
@@ -7647,6 +8662,9 @@ Type::find_field_or_method(const Type* type,
   if (fields == NULL)
     return false;
 
+  if (nt != NULL)
+    seen->push_back(nt);
+
   int found_level = 0;
   bool found_is_method = false;
   std::string found_ambig1;
@@ -7656,17 +8674,25 @@ Type::find_field_or_method(const Type* type,
        pf != fields->end();
        ++pf)
     {
-      if (pf->field_name() == name)
+      if (pf->is_field_name(name))
        {
          *is_method = false;
+         if (nt != NULL)
+           seen->pop_back();
          return true;
        }
 
       if (!pf->is_anonymous())
        continue;
 
-      Named_type* fnt = pf->type()->deref()->named_type();
-      gcc_assert(fnt != NULL);
+      if (pf->type()->deref()->is_error_type()
+         || pf->type()->deref()->is_undefined())
+       continue;
+
+      Named_type* fnt = pf->type()->named_type();
+      if (fnt == NULL)
+       fnt = pf->type()->deref()->named_type();
+      go_assert(fnt != NULL);
 
       int sublevel = level == NULL ? 1 : *level + 1;
       bool sub_is_method;
@@ -7675,6 +8701,7 @@ Type::find_field_or_method(const Type* type,
       bool subfound = Type::find_field_or_method(fnt,
                                                 name,
                                                 receiver_can_be_pointer,
+                                                seen,
                                                 &sublevel,
                                                 &sub_is_method,
                                                 found_pointer_method,
@@ -7690,8 +8717,10 @@ Type::find_field_or_method(const Type* type,
              // pass the ambiguity back to the caller.
              if (found_level == 0 || sublevel <= found_level)
                {
-                 found_ambig1 = pf->field_name() + '.' + subambig1;
-                 found_ambig2 = pf->field_name() + '.' + subambig2;
+                 found_ambig1 = (Gogo::message_name(pf->field_name())
+                                 + '.' + subambig1);
+                 found_ambig2 = (Gogo::message_name(pf->field_name())
+                                 + '.' + subambig2);
                  found_level = sublevel;
                }
            }
@@ -7714,9 +8743,9 @@ Type::find_field_or_method(const Type* type,
          else if (found_ambig1.empty())
            {
              // We found an ambiguity.
-             gcc_assert(found_parent != NULL);
-             found_ambig1 = found_parent->field_name();
-             found_ambig2 = pf->field_name();
+             go_assert(found_parent != NULL);
+             found_ambig1 = Gogo::message_name(found_parent->field_name());
+             found_ambig2 = Gogo::message_name(pf->field_name());
            }
          else
            {
@@ -7731,11 +8760,14 @@ Type::find_field_or_method(const Type* type,
   // FOUND_AMBIG2 are not empty.  If we found the field, FOUND_LEVEL
   // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
 
+  if (nt != NULL)
+    seen->pop_back();
+
   if (found_level == 0)
     return false;
   else if (!found_ambig1.empty())
     {
-      gcc_assert(!found_ambig1.empty());
+      go_assert(!found_ambig1.empty());
       ambig1->assign(found_ambig1);
       ambig2->assign(found_ambig2);
       if (level != NULL)
@@ -7755,18 +8787,35 @@ Type::find_field_or_method(const Type* type,
 
 bool
 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
-                                   const std::string& name)
+                                   const std::string& name,
+                                   std::vector<const Named_type*>* seen)
 {
-  type = type->deref();
-
   const Named_type* nt = type->named_type();
-  if (nt != NULL && nt->is_unexported_local_method(gogo, name))
-    return true;
+  if (nt == NULL)
+    nt = type->deref()->named_type();
+  if (nt != NULL)
+    {
+      if (nt->is_unexported_local_method(gogo, name))
+       return true;
+
+      for (std::vector<const Named_type*>::const_iterator p = seen->begin();
+          p != seen->end();
+          ++p)
+       {
+         if (*p == nt)
+           {
+             // We've already seen this type.
+             return false;
+           }
+       }
+    }
 
   const Interface_type* it = type->interface_type();
   if (it != NULL && it->is_unexported_method(gogo, name))
     return true;
 
+  type = type->deref();
+
   const Struct_type* st = type->struct_type();
   if (st != NULL && st->is_unexported_local_field(gogo, name))
     return true;
@@ -7778,19 +8827,37 @@ Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
   if (fields == NULL)
     return false;
 
+  if (nt != NULL)
+    seen->push_back(nt);
+
   for (Struct_field_list::const_iterator pf = fields->begin();
        pf != fields->end();
        ++pf)
     {
-      if (pf->is_anonymous())
+      if (pf->is_anonymous()
+         && !pf->type()->deref()->is_error_type()
+         && !pf->type()->deref()->is_undefined())
        {
-         Named_type* subtype = pf->type()->deref()->named_type();
-         gcc_assert(subtype != NULL);
-         if (Type::is_unexported_field_or_method(gogo, subtype, name))
-           return true;
+         Named_type* subtype = pf->type()->named_type();
+         if (subtype == NULL)
+           subtype = pf->type()->deref()->named_type();
+         if (subtype == NULL)
+           {
+             // This is an error, but it will be diagnosed elsewhere.
+             continue;
+           }
+         if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
+           {
+             if (nt != NULL)
+               seen->pop_back();
+             return true;
+           }
        }
     }
 
+  if (nt != NULL)
+    seen->pop_back();
+
   return false;
 }
 
@@ -7800,7 +8867,7 @@ Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
   : Type(TYPE_FORWARD),
     named_object_(named_object->resolve()), warned_(false)
 {
-  gcc_assert(this->named_object_->is_unknown()
+  go_assert(this->named_object_->is_unknown()
             || this->named_object_->is_type_declaration());
 }
 
@@ -7908,7 +8975,8 @@ Forward_declaration_type::add_method(const std::string& name,
                                     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);
 }
 
@@ -7918,10 +8986,11 @@ Forward_declaration_type::add_method(const std::string& name,
 Named_object*
 Forward_declaration_type::add_method_declaration(const std::string& name,
                                                 Function_type* type,
-                                                source_location location)
+                                                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);
 }
@@ -7937,26 +9006,24 @@ Forward_declaration_type::do_traverse(Traverse* traverse)
   return TRAVERSE_CONTINUE;
 }
 
-// Get a tree for the type.
+// Get the backend representation for the type.
 
-tree
-Forward_declaration_type::do_get_tree(Gogo* gogo)
+Btype*
+Forward_declaration_type::do_get_backend(Gogo* gogo)
 {
   if (this->is_defined())
-    return Type::get_named_type_tree(gogo, this->real_type());
+    return Type::get_named_base_btype(gogo, this->real_type());
 
   if (this->warned_)
-    return error_mark_node;
+    return gogo->backend()->error_type();
 
   // We represent an undefined type as a struct with no fields.  That
-  // should work fine for the middle-end, since the same case can
-  // arise in C.
-  Named_object* no = this->named_object();
-  tree type_tree = make_node(RECORD_TYPE);
-  tree id = no->get_id(gogo);
-  tree decl = build_decl(no->location(), TYPE_DECL, id, type_tree);
-  TYPE_NAME(type_tree) = decl;
-  return type_tree;
+  // should work fine for the backend, since the same case can arise
+  // in C.
+  std::vector<Backend::Btyped_identifier> fields;
+  Btype* bt = gogo->backend()->struct_type(fields);
+  return gogo->backend()->named_type(this->name(), bt,
+                                    this->named_object()->location());
 }
 
 // Build a type descriptor for a forwarded type.
@@ -7964,15 +9031,16 @@ Forward_declaration_type::do_get_tree(Gogo* gogo)
 Expression*
 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
 {
+  Location ploc = Linemap::predeclared_location();
   if (!this->is_defined())
-    return Expression::make_nil(BUILTINS_LOCATION);
+    return Expression::make_nil(ploc);
   else
     {
       Type* t = this->real_type();
       if (name != NULL)
        return this->named_type_descriptor(gogo, t, name);
       else
-       return Expression::make_type_descriptor(t, BUILTINS_LOCATION);
+       return Expression::make_type_descriptor(t, ploc);
     }
 }
 
@@ -8017,7 +9085,7 @@ void
 Forward_declaration_type::do_export(Export*) const
 {
   // If there is a base type, that should be exported instead of this.
-  gcc_assert(!this->is_defined());
+  go_assert(!this->is_defined());
 
   // We don't output anything.
 }