Rewrite conversion of named types to backend representation.
[gcc.git] / gcc / go / gofrontend / types.cc
index 8d440a89d4a671baa54c41c966098938fa187b97..368ecae127a084c8457bcae6a5433a76f306e2f8 100644 (file)
@@ -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;
     }
@@ -198,9 +194,9 @@ Type::make_non_abstract_type()
     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:
@@ -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;
     }
@@ -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,25 +374,37 @@ 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();
@@ -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.
@@ -468,6 +482,15 @@ bool
 Type::are_assignable(const Type* lhs, const Type* rhs, 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.
@@ -485,14 +508,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 +542,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;
 
@@ -533,15 +557,17 @@ Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
          || 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())
     {
@@ -581,7 +607,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 +620,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;
 
@@ -795,6 +822,9 @@ Type::get_tree(Gogo* gogo)
       || this->named_type() != NULL)
     return this->get_tree_without_hash(gogo);
 
+  if (this->is_error_type())
+    return error_mark_node;
+
   // 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
@@ -805,8 +835,9 @@ Type::get_tree(Gogo* gogo)
     Type::type_trees.insert(val);
   if (!ins.second && ins.first->second != NULL_TREE)
     {
-      this->tree_ = ins.first->second;
-      return this->tree_;
+      if (gogo != NULL && gogo->named_types_are_converted())
+       this->tree_ = ins.first->second;
+      return ins.first->second;
     }
 
   tree t = this->get_tree_without_hash(gogo);
@@ -820,6 +851,8 @@ Type::get_tree(Gogo* gogo)
       // 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;
+      if (gogo == NULL || !gogo->named_types_are_converted())
+       return t;
       this->tree_ = t;
     }
 
@@ -844,6 +877,9 @@ Type::get_tree_without_hash(Gogo* gogo)
       if (t == ptr_type_node && this->forward_declaration_type() != NULL)
        return t;
 
+      if (gogo == NULL || !gogo->named_types_are_converted())
+       return t;
+
       this->tree_ = t;
       go_preserve_from_gc(t);
     }
@@ -931,6 +967,10 @@ 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*
@@ -938,7 +978,25 @@ Type::make_builtin_named_type(const char* name, Type* type)
 {
   source_location bloc = BUILTINS_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();
+      gcc_assert(r);
+      (*p)->convert(gogo);
+    }
 }
 
 // Return the type of a type descriptor.  We should really tie this to
@@ -1842,8 +1900,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;
 }
 
@@ -1999,8 +2056,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;
 }
 
@@ -2321,7 +2377,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 +2461,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 +2476,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 +2508,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 +2553,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");
@@ -3233,26 +3293,35 @@ class Call_multiple_result_type : public Type
  protected:
   bool
   do_has_pointer() const
-  { gcc_unreachable(); }
+  {
+    gcc_assert(saw_errors());
+    return false;
+  }
 
   tree
   do_get_tree(Gogo*);
 
   tree
   do_get_init_tree(Gogo*, tree, bool)
-  { gcc_unreachable(); }
+  {
+    gcc_assert(saw_errors());
+    return error_mark_node;
+  }
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*)
-  { gcc_unreachable(); }
+  {
+    gcc_assert(saw_errors());
+    return Expression::make_error(UNKNOWN_LOCATION);
+  }
 
   void
   do_reflection(Gogo*, std::string*) const
-  { gcc_unreachable(); }
+  { gcc_assert(saw_errors()); }
 
   void
   do_mangled_name(Gogo*, std::string*) const
-  { gcc_unreachable(); }
+  { gcc_assert(saw_errors()); }
 
  private:
   // The expression being called.
@@ -3268,20 +3337,10 @@ Call_multiple_result_type::do_get_tree(Gogo* gogo)
   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);
+  tree fntype_tree = fntype->get_tree(gogo);
+  if (fntype_tree == error_mark_node)
+    return error_mark_node;
+  return TREE_TYPE(fntype_tree);
 }
 
 // Make a call result type.
@@ -3368,6 +3427,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 +3437,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())
        {
@@ -3389,7 +3449,7 @@ Struct_type::do_verify()
            }
        }
     }
-  return true;
+  return ret;
 }
 
 // Whether this contains a pointer.
@@ -3413,7 +3473,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 +3490,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())
        {
@@ -3539,7 +3601,8 @@ Struct_type::field_reference(Expression* struct_expr, const std::string& name,
                             source_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
@@ -3549,6 +3612,7 @@ Field_reference_expression*
 Struct_type::field_reference_depth(Expression* struct_expr,
                                   const std::string& name,
                                   source_location location,
+                                  Saw_named_type* saw,
                                   unsigned int* depth) const
 {
   const Struct_field_list* fields = this->fields_;
@@ -3584,13 +3648,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;
 
@@ -3677,6 +3769,8 @@ Struct_type::is_unexported_local_field(Gogo* gogo,
 void
 Struct_type::finalize_methods(Gogo* gogo)
 {
+  if (this->all_methods_ != NULL)
+    return;
   Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
 }
 
@@ -3712,9 +3806,12 @@ Struct_type::fill_in_tree(Gogo* gogo, tree type)
     {
       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;
+      gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
+
       tree field = build_decl(p->location(), FIELD_DECL, name_tree,
                              field_type_tree);
       DECL_CONTEXT(field) = type;
@@ -3751,13 +3848,16 @@ Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
   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)
+
+  tree field = TYPE_FIELDS(type_tree);
+  for (Struct_field_list::const_iterator p = this->fields_->begin();
+       p != this->fields_->end();
+       ++p, field = DECL_CHAIN(field))
     {
-      gcc_assert(p != this->fields_->end());
       tree value = p->type()->get_init_tree(gogo, is_clear);
+      if (value == error_mark_node)
+       return error_mark_node;
+      gcc_assert(field != NULL_TREE);
       if (value != NULL)
        {
          constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
@@ -3768,7 +3868,7 @@ Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
            is_constant = false;
        }
     }
-  gcc_assert(p == this->fields_->end());
+  gcc_assert(field == NULL_TREE);
 
   if (!any_fields_set)
     {
@@ -4107,9 +4207,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();
@@ -4167,6 +4268,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 +4279,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 +4303,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)
     {
@@ -4330,8 +4423,12 @@ Array_type::get_length_tree(Gogo* gogo)
          // 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);
+         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_;
@@ -4348,37 +4445,67 @@ 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);
+      return this->fill_in_slice_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;
+      tree array_type = make_node(ARRAY_TYPE);
+      return this->fill_in_array_tree(gogo, array_type);
+    }
+}
 
-      length_tree = fold_convert(sizetype, length_tree);
+// Fill in the fields for an array type.  This is used for named array
+// types.
 
-      // 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));
+tree
+Array_type::fill_in_array_tree(Gogo* gogo, tree array_type)
+{
+  gcc_assert(this->length_ != NULL);
 
-      return build_array_type(element_type_tree, index_type);
-    }
+  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;
+
+  gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
+
+  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));
+
+  TREE_TYPE(array_type) = element_type_tree;
+  TYPE_DOMAIN(array_type) = index_type;
+  TYPE_ADDR_SPACE(array_type) = TYPE_ADDR_SPACE(element_type_tree);
+  layout_type(array_type);
+
+  if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree)
+      || TYPE_STRUCTURAL_EQUALITY_P(index_type))
+    SET_TYPE_STRUCTURAL_EQUALITY(array_type);
+  else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
+          || TYPE_CANONICAL(index_type) != index_type)
+    TYPE_CANONICAL(array_type) =
+      build_array_type(TYPE_CANONICAL(element_type_tree),
+                      TYPE_CANONICAL(index_type));
+
+  return array_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)
+Array_type::fill_in_slice_tree(Gogo* gogo, tree struct_type)
 {
   gcc_assert(this->length_ == NULL);
 
   tree element_type_tree = this->element_type_->get_tree(gogo);
+  if (element_type_tree == error_mark_node)
+    return error_mark_node;
   tree field = TYPE_FIELDS(struct_type);
   gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
   gcc_assert(POINTER_TYPE_P(TREE_TYPE(field))
@@ -4425,8 +4552,13 @@ Array_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
       tree value = this->element_type_->get_init_tree(gogo, is_clear);
       if (value == NULL)
        return NULL;
+      if (value == error_mark_node)
+       return error_mark_node;
 
       tree length_tree = this->get_length_tree(gogo);
+      if (length_tree == error_mark_node)
+       return error_mark_node;
+
       length_tree = fold_convert(sizetype, length_tree);
       tree range = build2(RANGE_EXPR, sizetype, size_zero_node,
                          fold_build2(MINUS_EXPR, sizetype,
@@ -4466,6 +4598,8 @@ Array_type::do_make_expression_tree(Translate_context* context,
   tree element_size_tree = TYPE_SIZE_UNIT(element_type_tree);
 
   tree value = this->element_type_->get_init_tree(gogo, true);
+  if (value == error_mark_node)
+    return error_mark_node;
 
   // The first argument is the number of elements, the optional second
   // argument is the capacity.
@@ -4589,7 +4723,7 @@ Array_type::do_make_expression_tree(Translate_context* context,
   tree range = build2(RANGE_EXPR, sizetype, size_zero_node, max);
   tree space_init = build_constructor_single(array_type, range, value);
 
-  return build2(COMPOUND_EXPR, TREE_TYPE(space),
+  return build2(COMPOUND_EXPR, TREE_TYPE(constructor),
                build2(MODIFY_EXPR, void_type_node,
                       build_fold_indirect_ref(value_pointer),
                       space_init),
@@ -4783,7 +4917,7 @@ Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
 
   ++p;
   gcc_assert(p->field_name() == "len");
-  vals->push_back(this->length_);
+  vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
 
   ++p;
   gcc_assert(p == fields->end());
@@ -4920,10 +5054,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.
@@ -5062,6 +5198,8 @@ Map_type::do_make_expression_tree(Translate_context* context,
                                context->gogo()->map_descriptor(this),
                                sizetype,
                                expr_tree);
+  if (ret == error_mark_node)
+    return error_mark_node;
   // This can panic if the capacity is out of range.
   TREE_NOTHROW(new_map_fndecl) = 0;
 
@@ -5208,9 +5346,11 @@ 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_);
@@ -5310,6 +5450,8 @@ Channel_type::do_make_expression_tree(Translate_context* context,
                                element_size_tree,
                                sizetype,
                                expr_tree);
+  if (ret == error_mark_node)
+    return error_mark_node;
   // This can panic if the capacity is out of range.
   TREE_NOTHROW(new_channel_fndecl) = 0;
 
@@ -5494,6 +5636,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 +5645,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 +5682,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 +5713,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
@@ -5613,7 +5815,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 +5831,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 +5871,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)
            {
@@ -5802,7 +6006,7 @@ Interface_type::implements_interface(const Type* t, std::string* reason) const
       Function_type* m_fn_type = m->type()->function_type();
       gcc_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)
            {
@@ -5853,22 +6057,62 @@ tree
 Interface_type::do_get_tree(Gogo* gogo)
 {
   if (this->methods_ == NULL)
+    return Interface_type::empty_type_tree(gogo);
+  else
     {
-      // 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);
+      tree t = Interface_type::non_empty_type_tree(this->location_);
+      return this->fill_in_tree(gogo, t);
     }
+}
+
+// Return a singleton struct for an empty interface type.  We use the
+// same type for all empty interfaces.  This lets us assign them to
+// each other directly without triggering GIMPLE type errors.
+
+tree
+Interface_type::empty_type_tree(Gogo* gogo)
+{
+  static tree empty_interface;
+  if (empty_interface != NULL_TREE)
+    return empty_interface;
+
+  tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
+  dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
+  return Gogo::builtin_struct(&empty_interface, "__go_empty_interface",
+                             NULL_TREE, 2,
+                             "__type_descriptor",
+                             dtype,
+                             "__object",
+                             ptr_type_node);
+}
+
+// Return a new struct for a non-empty interface type.  The correct
+// values are filled in by fill_in_tree.
+
+tree
+Interface_type::non_empty_type_tree(source_location location)
+{
+  tree ret = make_node(RECORD_TYPE);
 
-  return this->fill_in_tree(gogo, make_node(RECORD_TYPE));
+  tree field_trees = NULL_TREE;
+  tree* pp = &field_trees;
+
+  tree name_tree = get_identifier("__methods");
+  tree field = build_decl(location, FIELD_DECL, name_tree, ptr_type_node);
+  DECL_CONTEXT(field) = ret;
+  *pp = field;
+  pp = &DECL_CHAIN(field);
+
+  name_tree = get_identifier("__object");
+  field = build_decl(location, FIELD_DECL, name_tree, ptr_type_node);
+  DECL_CONTEXT(field) = ret;
+  *pp = field;
+
+  TYPE_FIELDS(ret) = field_trees;
+
+  layout_type(ret);
+
+  return ret;
 }
 
 // Fill in the tree for an interface type.  This is used for named
@@ -5912,25 +6156,12 @@ Interface_type::fill_in_tree(Gogo* gogo, tree type)
     }
   layout_type(method_table);
 
-  tree mtype = build_pointer_type(method_table);
-
-  tree field_trees = NULL_TREE;
-  pp = &field_trees;
+  // Update the type of the __methods field from a generic pointer to
+  // a pointer to the method table.
+  field = TYPE_FIELDS(type);
+  gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
 
-  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);
-
-  name_tree = get_identifier("__object");
-  field = build_decl(this->location_, FIELD_DECL, name_tree, ptr_type_node);
-  DECL_CONTEXT(field) = type;
-  *pp = field;
-
-  TYPE_FIELDS(type) = field_trees;
-
-  layout_type(type);
+  TREE_TYPE(field) = build_pointer_type(method_table);
 
   return type;
 }
@@ -6448,6 +6679,45 @@ 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_ > 0)
+    return this;
+  ++this->seen_;
+  Type* ret = this->type_->base();
+  --this->seen_;
+  return ret;
+}
+
+const Type*
+Named_type::named_base() const
+{
+  if (this->seen_ > 0)
+    return this;
+  ++this->seen_;
+  const Type* ret = this->type_->base();
+  --this->seen_;
+  return ret;
+}
+
+// Return whether this is an error type.  We have to be careful about
+// circular type definitions, which are invalid but may be seen here.
+
+bool
+Named_type::is_named_error_type() const
+{
+  if (this->seen_ > 0)
+    return false;
+  ++this->seen_;
+  bool ret = this->type_->is_error_type();
+  --this->seen_;
+  return ret;
+}
+
 // Add a method to this type.
 
 Named_object*
@@ -6523,6 +6793,9 @@ Named_type::is_unexported_local_method(Gogo* gogo,
 void
 Named_type::finalize_methods(Gogo* gogo)
 {
+  if (this->all_methods_ != NULL)
+    return;
+
   if (this->local_methods_ != NULL
       && (this->points_to() != NULL || this->interface_type() != NULL))
     {
@@ -6591,11 +6864,11 @@ Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
 bool
 Named_type::named_type_has_hidden_fields(std::string* reason) const
 {
-  if (this->seen_)
+  if (this->seen_ > 0)
     return false;
-  this->seen_ = true;
+  ++this->seen_;
   bool ret = this->type_->has_hidden_fields(this, reason);
-  this->seen_ = false;
+  --this->seen_;
   return ret;
 }
 
@@ -6605,7 +6878,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 +6894,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,11 +6904,12 @@ 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.
@@ -6669,6 +6943,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:
+         gcc_unreachable();
+       }
+    }
+
   return TRAVERSE_CONTINUE;
 }
 
@@ -6727,6 +7037,19 @@ 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_ > 0)
+    return false;
+  ++this->seen_;
+  bool ret = this->type_->has_pointer();
+  --this->seen_;
+  return ret;
+}
+
 // Return a hash code.  This is used for method lookup.  We simply
 // hash on the name itself.
 
@@ -6760,21 +7083,109 @@ 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.
+  tree t = this->named_tree_;
+  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.
+      break;
+
+    case TYPE_STRUCT:
+      t = base->struct_type()->fill_in_tree(gogo, t);
+      break;
+
+    case TYPE_ARRAY:
+      if (!base->is_open_array_type())
+       t = base->array_type()->fill_in_array_tree(gogo, t);
+      break;
+
+    case TYPE_INTERFACE:
+      if (!base->interface_type()->is_empty())
+       t = base->interface_type()->fill_in_tree(gogo, t);
+      break;
+
+    case TYPE_ERROR:
+      return;
+
+    default:
+    case TYPE_SINK:
+    case TYPE_CALL_MULTIPLE_RESULT:
+    case TYPE_NAMED:
+    case TYPE_FORWARD:
+      gcc_unreachable();
+    }
+
+  this->named_tree_ = t;
+
+  if (t == error_mark_node)
+    this->is_error_ = true;
+  else
+    gcc_assert(TYPE_SIZE(t) != NULL_TREE);
+
+  this->is_converted_ = true;
+}
+
+// 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_tree_ = error_mark_node;
+
+  if (this->named_tree_ != NULL_TREE)
+    return;
 
-  // Go permits types to refer to themselves in various ways.  Break
-  // the recursion here.
+  // 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();
   tree t;
-  switch (this->type_->forwarded()->classification())
+  switch (base->classification())
     {
     case TYPE_ERROR:
-      return error_mark_node;
+      this->is_error_ = true;
+      this->named_tree_ = error_mark_node;
+      return;
 
     case TYPE_VOID:
     case TYPE_BOOLEAN:
@@ -6783,128 +7194,194 @@ Named_type::do_get_tree(Gogo* gogo)
     case TYPE_COMPLEX:
     case TYPE_STRING:
     case TYPE_NIL:
-      // These types can not refer to themselves.
-    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_);
+      // These are simple basic types, we can just create them
+      // directly.
+      t = Type::get_named_type_tree(gogo, base);
       if (t == error_mark_node)
-       return error_mark_node;
-      // Build a copy to set TYPE_NAME.
+       {
+         this->is_error_ = true;
+         this->named_tree_ = error_mark_node;
+         return;
+       }
       t = build_variant_type_copy(t);
       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;
+    case TYPE_MAP:
+    case TYPE_CHANNEL:
+      // All maps and channels have the same type in GENERIC.
+      t = Type::get_named_type_tree(gogo, base);
       if (t == error_mark_node)
-       return error_mark_node;
+       {
+         this->is_error_ = true;
+         this->named_tree_ = error_mark_node;
+         return;
+       }
       t = build_variant_type_copy(t);
       break;
 
+    case TYPE_FUNCTION:
     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);
+      t = build_variant_type_copy(ptr_type_node);
       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);
       break;
 
     case TYPE_ARRAY:
-      if (!this->is_open_array_type())
-       t = Type::get_named_type_tree(gogo, this->type_);
+      if (base->is_open_array_type())
+       t = gogo->slice_type_tree(void_type_node);
       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);
-       }
-      if (t == error_mark_node)
-       return error_mark_node;
-      t = build_variant_type_copy(t);
+       t = make_node(ARRAY_TYPE);
       break;
 
     case TYPE_INTERFACE:
-      if (this->type_->interface_type()->is_empty())
+      if (base->interface_type()->is_empty())
        {
-         t = Type::get_named_type_tree(gogo, this->type_);
-         if (t == error_mark_node)
-           return error_mark_node;
+         t = Interface_type::empty_type_tree(gogo);
          t = build_variant_type_copy(t);
        }
       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);
-         if (t == error_mark_node)
-           return error_mark_node;
+         source_location loc = base->interface_type()->location();
+         t = Interface_type::non_empty_type_tree(loc);
        }
       break;
 
-    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;
-
     default:
     case TYPE_SINK:
     case TYPE_CALL_MULTIPLE_RESULT:
+    case TYPE_NAMED:
+    case TYPE_FORWARD:
       gcc_unreachable();
     }
 
+  // Create the named type.
+
   tree id = this->named_object_->get_id(gogo);
   tree decl = build_decl(this->location_, TYPE_DECL, id, t);
   TYPE_NAME(t) = decl;
 
-  return t;
+  this->named_tree_ = t;
+}
+
+// Get a tree for a named type.
+
+tree
+Named_type::do_get_tree(Gogo* gogo)
+{
+  if (this->is_error_)
+    return error_mark_node;
+
+  tree t = this->named_tree_;
+
+  // FIXME: GOGO can be NULL when called from go_type_for_size, which
+  // is only used for basic types.
+  if (gogo == NULL || !gogo->named_types_are_converted())
+    {
+      // We have not completed converting named types.  NAMED_TREE_ is
+      // a placeholder and we shouldn't do anything further.
+      if (t != NULL_TREE)
+       return t;
+
+      // 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);
+      t = this->named_tree_;
+      gcc_assert(t != NULL_TREE);
+      return t;
+    }
+
+  // We are not converting types.  This should only be called if the
+  // type has already been converted.
+  gcc_assert(this->is_converted_);
+  gcc_assert(t != NULL_TREE && TYPE_SIZE(t) != NULL_TREE);
+
+  // Complete the tree.
+  Type* base = this->type_->base();
+  tree t1;
+  switch (base->classification())
+    {
+    case TYPE_ERROR:
+      return error_mark_node;
+
+    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_INTERFACE:
+      return t;
+
+    case TYPE_FUNCTION:
+      // Don't build a circular data structure.  GENERIC can't handle
+      // it.
+      if (this->seen_ > 0)
+       {
+         this->is_circular_ = true;
+         return ptr_type_node;
+       }
+      ++this->seen_;
+      t1 = Type::get_named_type_tree(gogo, base);
+      --this->seen_;
+      if (t1 == error_mark_node)
+       return error_mark_node;
+      if (this->is_circular_)
+       t1 = ptr_type_node;
+      gcc_assert(t != NULL_TREE && TREE_CODE(t) == POINTER_TYPE);
+      gcc_assert(TREE_CODE(t1) == POINTER_TYPE);
+      TREE_TYPE(t) = TREE_TYPE(t1);
+      return t;
+
+    case TYPE_POINTER:
+      // Don't build a circular data structure. GENERIC can't handle
+      // it.
+      if (this->seen_ > 0)
+       {
+         this->is_circular_ = true;
+         return ptr_type_node;
+       }
+      ++this->seen_;
+      t1 = Type::get_named_type_tree(gogo, base);
+      --this->seen_;
+      if (t1 == error_mark_node)
+       return error_mark_node;
+      if (this->is_circular_)
+       t1 = ptr_type_node;
+      gcc_assert(t != NULL_TREE && TREE_CODE(t) == POINTER_TYPE);
+      gcc_assert(TREE_CODE(t1) == POINTER_TYPE);
+      TREE_TYPE(t) = TREE_TYPE(t1);
+      return t;
+
+    case TYPE_ARRAY:
+      if (base->is_open_array_type())
+       {
+         if (this->seen_ > 0)
+           return t;
+         else
+           {
+             ++this->seen_;
+             t = base->array_type()->fill_in_slice_tree(gogo, t);
+             --this->seen_;
+           }
+       }
+      return t;
+
+    default:
+    case TYPE_SINK:
+    case TYPE_CALL_MULTIPLE_RESULT:
+    case TYPE_NAMED:
+    case TYPE_FORWARD:
+      gcc_unreachable();
+    }
+
+  gcc_unreachable();
 }
 
 // Build a type descriptor for a named type.
@@ -7226,7 +7703,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;
+       }
+      gcc_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);
@@ -7477,7 +7960,7 @@ Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
                           const std::string& name,
                           source_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();
@@ -7496,6 +7979,8 @@ 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();
@@ -7503,13 +7988,14 @@ Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
 
   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)
@@ -7566,8 +8052,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",
@@ -7585,13 +8073,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
@@ -7604,6 +8095,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,
@@ -7630,6 +8122,17 @@ 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.
@@ -7649,6 +8152,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;
@@ -7661,13 +8167,21 @@ Type::find_field_or_method(const Type* type,
       if (pf->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();
+      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();
       gcc_assert(fnt != NULL);
 
       int sublevel = level == NULL ? 1 : *level + 1;
@@ -7677,6 +8191,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,
@@ -7733,6 +8248,9 @@ 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())
@@ -7757,13 +8275,30 @@ 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;
+           }
+       }
+    }
+
+  type = type->deref();
 
   const Interface_type* it = type->interface_type();
   if (it != NULL && it->is_unexported_method(gogo, name))
@@ -7780,19 +8315,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;
 }
 
@@ -7910,7 +8463,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);
 }
 
@@ -7923,7 +8477,8 @@ Forward_declaration_type::add_method_declaration(const std::string& name,
                                                 source_location location)
 {
   Named_object* no = this->named_object();
-  gcc_assert(no->is_type_declaration());
+  if (no->is_unknown())
+    no->declare_as_type();
   Type_declaration* td = no->type_declaration_value();
   return td->add_method_declaration(name, type, location);
 }
@@ -7958,6 +8513,7 @@ Forward_declaration_type::do_get_tree(Gogo* gogo)
   tree id = no->get_id(gogo);
   tree decl = build_decl(no->location(), TYPE_DECL, id, type_tree);
   TYPE_NAME(type_tree) = decl;
+  layout_type(type_tree);
   return type_tree;
 }