OSDN Git Service

compiler: Rewrite handling of untyped numeric constants.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / types.h
index 025f85f..edc46b7 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef GO_TYPES_H
 #define GO_TYPES_H
 
+#include "go-linemap.h"
+
 class Gogo;
 class Package;
 class Traverse;
@@ -42,6 +44,9 @@ class Function;
 class Translate_context;
 class Export;
 class Import;
+class Btype;
+class Bexpression;
+class Bvariable;
 
 // Type codes used in type descriptors.  These must match the values
 // in libgo/runtime/go-type.h.  They also match the values in the gc
@@ -60,22 +65,22 @@ static const int RUNTIME_TYPE_KIND_UINT16 = 9;
 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
-static const int RUNTIME_TYPE_KIND_FLOAT = 13;
-static const int RUNTIME_TYPE_KIND_FLOAT32 = 14;
-static const int RUNTIME_TYPE_KIND_FLOAT64 = 15;
-static const int RUNTIME_TYPE_KIND_COMPLEX = 16;
-static const int RUNTIME_TYPE_KIND_COMPLEX64 = 17;
-static const int RUNTIME_TYPE_KIND_COMPLEX128 = 18;
-static const int RUNTIME_TYPE_KIND_ARRAY = 19;
-static const int RUNTIME_TYPE_KIND_CHAN = 20;
-static const int RUNTIME_TYPE_KIND_FUNC = 21;
-static const int RUNTIME_TYPE_KIND_INTERFACE = 22;
-static const int RUNTIME_TYPE_KIND_MAP = 23;
-static const int RUNTIME_TYPE_KIND_PTR = 24;
-static const int RUNTIME_TYPE_KIND_SLICE = 25;
-static const int RUNTIME_TYPE_KIND_STRING = 26;
-static const int RUNTIME_TYPE_KIND_STRUCT = 27;
-static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 28;
+static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
+static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
+static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
+static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
+static const int RUNTIME_TYPE_KIND_ARRAY = 17;
+static const int RUNTIME_TYPE_KIND_CHAN = 18;
+static const int RUNTIME_TYPE_KIND_FUNC = 19;
+static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
+static const int RUNTIME_TYPE_KIND_MAP = 21;
+static const int RUNTIME_TYPE_KIND_PTR = 22;
+static const int RUNTIME_TYPE_KIND_SLICE = 23;
+static const int RUNTIME_TYPE_KIND_STRING = 24;
+static const int RUNTIME_TYPE_KIND_STRUCT = 25;
+static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
+
+static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
 
 // To build the complete list of methods for a named type we need to
 // gather all methods from anonymous fields.  Those methods may
@@ -144,14 +149,14 @@ class Method
   { return this->do_type(); }
 
   // Return the location of the method receiver.
-  source_location
+  Location
   receiver_location() const
   { return this->do_receiver_location(); }
 
   // Return an expression which binds this method to EXPR.  This is
   // something which can be used with a function call.
   Expression*
-  bind_method(Expression* expr, source_location location) const;
+  bind_method(Expression* expr, Location location) const;
 
   // Return the named object for this method.  This may only be called
   // after methods are finalized.
@@ -162,7 +167,7 @@ class Method
   Named_object*
   stub_object() const
   {
-    gcc_assert(this->stub_ != NULL);
+    go_assert(this->stub_ != NULL);
     return this->stub_;
   }
 
@@ -170,7 +175,7 @@ class Method
   void
   set_stub_object(Named_object* no)
   {
-    gcc_assert(this->stub_ == NULL);
+    go_assert(this->stub_ == NULL);
     this->stub_ = no;
   }
 
@@ -192,12 +197,12 @@ class Method
   do_type() const = 0;
 
   // Return the location of the method receiver.
-  virtual source_location
+  virtual Location
   do_receiver_location() const = 0;
 
   // Bind a method to an object.
   virtual Expression*
-  do_bind_method(Expression* expr, source_location location) const = 0;
+  do_bind_method(Expression* expr, Location location) const = 0;
 
  private:
   // The sequence of field indexes used for this method.  If this is
@@ -242,12 +247,12 @@ class Named_method : public Method
   do_type() const;
 
   // Return the location of the method receiver.
-  source_location
+  Location
   do_receiver_location() const;
 
   // Bind a method to an object.
   Expression*
-  do_bind_method(Expression* expr, source_location location) const;
+  do_bind_method(Expression* expr, Location location) const;
 
  private:
   // The method itself.  For a method which needs a stub, this starts
@@ -262,7 +267,7 @@ class Named_method : public Method
 class Interface_method : public Method
 {
  public:
-  Interface_method(const std::string& name, source_location location,
+  Interface_method(const std::string& name, Location location,
                   Function_type* fntype, const Field_indexes* field_indexes,
                   unsigned int depth)
     : Method(field_indexes, depth, true, true),
@@ -274,7 +279,7 @@ class Interface_method : public Method
   // called, as we always create a stub.
   Named_object*
   do_named_object() const
-  { gcc_unreachable(); }
+  { go_unreachable(); }
 
   // The type of the method.
   Function_type*
@@ -282,19 +287,19 @@ class Interface_method : public Method
   { return this->fntype_; }
 
   // Return the location of the method receiver.
-  source_location
+  Location
   do_receiver_location() const
   { return this->location_; }
 
   // Bind a method to an object.
   Expression*
-  do_bind_method(Expression* expr, source_location location) const;
+  do_bind_method(Expression* expr, Location location) const;
 
  private:
   // The name of the interface method to call.
   std::string name_;
   // The location of the definition of the interface method.
-  source_location location_;
+  Location location_;
   // The type of the interface method.
   Function_type* fntype_;
 };
@@ -396,6 +401,10 @@ class Type
   static Integer_type*
   make_abstract_integer_type();
 
+  // Make an abstract type for a character constant.
+  static Integer_type*
+  make_abstract_character_type();
+
   // Make a named integer type with a specified size.
   // RUNTIME_TYPE_KIND is the code to use in reflection information,
   // to distinguish int and int32.
@@ -454,7 +463,7 @@ class Type
   make_function_type(Typed_identifier* receiver,
                     Typed_identifier_list* parameters,
                     Typed_identifier_list* results,
-                    source_location);
+                    Location);
 
   static Pointer_type*
   make_pointer_type(Type*);
@@ -466,19 +475,22 @@ class Type
   make_call_multiple_result_type(Call_expression*);
 
   static Struct_type*
-  make_struct_type(Struct_field_list* fields, source_location);
+  make_struct_type(Struct_field_list* fields, Location);
 
   static Array_type*
   make_array_type(Type* element_type, Expression* length);
 
   static Map_type*
-  make_map_type(Type* key_type, Type* value_type, source_location);
+  make_map_type(Type* key_type, Type* value_type, Location);
 
   static Channel_type*
   make_channel_type(bool send, bool receive, Type*);
 
   static Interface_type*
-  make_interface_type(Typed_identifier_list* methods, source_location);
+  make_interface_type(Typed_identifier_list* methods, Location);
+
+  static Interface_type*
+  make_empty_interface_type(Location);
 
   static Type*
   make_type_descriptor_type();
@@ -487,7 +499,7 @@ class Type
   make_type_descriptor_ptr_type();
 
   static Named_type*
-  make_named_type(Named_object*, Type*, source_location);
+  make_named_type(Named_object*, Type*, Location);
 
   static Type*
   make_forward_declaration(Named_object*);
@@ -498,15 +510,19 @@ class Type
 
   // Verify the type.  This is called after parsing, and verifies that
   // types are complete and meet the language requirements.  This
-  // returns false if the type is invalid.
+  // returns false if the type is invalid and we should not continue
+  // traversing it.
   bool
   verify()
   { return this->do_verify(); }
 
-  // Return true if two types are identical.  If this returns false,
+  // Return true if two types are identical.  If ERRORS_ARE_IDENTICAL,
+  // returns that an erroneous type is identical to any other type;
+  // this is used to avoid cascading errors.  If this returns false,
   // and REASON is not NULL, it may set *REASON.
   static bool
-  are_identical(const Type* lhs, const Type* rhs, std::string* reason);
+  are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
+               std::string* reason);
 
   // Return true if two types are compatible for use in a binary
   // operation, other than a shift, comparison, or channel send.  This
@@ -514,12 +530,35 @@ class Type
   static bool
   are_compatible_for_binop(const Type* t1, const Type* t2);
 
+  // Return true if two types are compatible for use with the
+  // comparison operator.  IS_EQUALITY_OP is true if this is an
+  // equality comparison, false if it is an ordered comparison.  This
+  // is an equivalence relation.  If this returns false, and REASON is
+  // not NULL, it sets *REASON.
+  static bool
+  are_compatible_for_comparison(bool is_equality_op, const Type *t1,
+                               const Type *t2, std::string* reason);
+
+  // Return true if a type is comparable with itself.  This is true of
+  // most types, but false for, e.g., function types.
+  bool
+  is_comparable() const
+  { return Type::are_compatible_for_comparison(true, this, this, NULL); }
+
   // Return true if a value with type RHS is assignable to a variable
   // with type LHS.  This is not an equivalence relation.  If this
   // returns false, and REASON is not NULL, it sets *REASON.
   static bool
   are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
 
+  // Return true if a value with type RHS is assignable to a variable
+  // with type LHS, ignoring any assignment of hidden fields
+  // (unexported fields of a type imported from another package).
+  // This is like the are_assignable method.
+  static bool
+  are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
+                          std::string* reason);
+
   // Return true if a value with type RHS may be converted to type
   // LHS.  If this returns false, and REASON is not NULL, it sets
   // *REASON.
@@ -533,6 +572,13 @@ class Type
   bool
   has_hidden_fields(const Named_type* within, std::string* reason) const;
 
+  // Return true if values of this type can be compared using an
+  // identity function which gets nothing but a pointer to the value
+  // and a size.
+  bool
+  compare_is_identity(Gogo* gogo) const
+  { return this->do_compare_is_identity(gogo); }
+
   // Return a hash code for this type for the method hash table.
   // Types which are equivalent according to are_identical will have
   // the same hash code.
@@ -586,11 +632,19 @@ class Type
   has_pointer() const
   { return this->do_has_pointer(); }
 
-  // Return true if this is an error type.  An error type indicates a
-  // parsing error.
+  // Return true if this is the error type.  This returns false for a
+  // type which is not defined, as it is called by the parser before
+  // all types are defined.
   bool
   is_error_type() const;
 
+  // Return true if this is the error type or if the type is
+  // undefined.  If the type is undefined, this will give an error.
+  // This should only be called after parsing is complete.
+  bool
+  is_error() const
+  { return this->base()->is_error_type(); }
+
   // Return true if this is a void type.
   bool
   is_void_type() const
@@ -626,6 +680,14 @@ class Type
   complex_type() const
   { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
 
+  // Return whether this is a numeric type.
+  bool
+  is_numeric_type() const
+  {
+    Type_classification tc = this->base()->classification_;
+    return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
+  }
+
   // Return true if this is a boolean type.
   bool
   is_boolean_type() const
@@ -719,9 +781,9 @@ class Type
   array_type() const
   { return this->convert<const Array_type, TYPE_ARRAY>(); }
 
-  // Return whether if this is an open array type.
+  // Return whether if this is a slice type.
   bool
-  is_open_array_type() const;
+  is_slice_type() const;
 
   // If this is a map type, return it.  Otherwise, return NULL.
   Map_type*
@@ -784,46 +846,36 @@ class Type
   // it, bound to EXPR.
   static Expression*
   bind_field_or_method(Gogo*, const Type* type, Expression* expr,
-                      const std::string& name, source_location);
+                      const std::string& name, Location);
 
   // Return true if NAME is an unexported field or method of TYPE.
   static bool
-  is_unexported_field_or_method(Gogo*, const Type*, const std::string&);
+  is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
+                               std::vector<const Named_type*>*);
 
-  // This type was passed to the builtin function make.  ARGS are the
-  // arguments passed to make after the type; this may be NULL if
-  // there were none.  Issue any required errors.
-  bool
-  check_make_expression(Expression_list* args, source_location location)
-  { return this->do_check_make_expression(args, location); }
-
-  // Return a tree representing this type.
-  tree
-  get_tree(Gogo*);
+  // Convert the builtin named types.
+  static void
+  convert_builtin_named_types(Gogo*);
 
-  // Return a tree representing a zero initialization for this type.
-  // This will be something like an INTEGER_CST or a CONSTRUCTOR.  If
-  // IS_CLEAR is true, then the memory is known to be zeroed; in that
-  // case, this will return NULL if there is nothing to be done.
-  tree
-  get_init_tree(Gogo*, bool is_clear);
+  // Return the backend representation of this type.
+  Btype*
+  get_backend(Gogo*);
 
-  // Like get_init_tree, but passing in the type to use for the
-  // initializer.
-  tree
-  get_typed_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
-  { return this->do_get_init_tree(gogo, type_tree, is_clear); }
+  // Return a placeholder for the backend representation of the type.
+  // This will return a type of the correct size, but for which some
+  // of the fields may still need to be completed.
+  Btype*
+  get_backend_placeholder(Gogo*);
 
-  // Return a tree for a make expression applied to this type.
-  tree
-  make_expression_tree(Translate_context* context, Expression_list* args,
-                      source_location location)
-  { return this->do_make_expression_tree(context, args, location); }
+  // Finish the backend representation of a placeholder.
+  void
+  finish_backend(Gogo*);
 
   // Build a type descriptor entry for this type.  Return a pointer to
-  // it.
+  // it.  The location is the location which causes us to need the
+  // entry.
   tree
-  type_descriptor_pointer(Gogo* gogo);
+  type_descriptor_pointer(Gogo* gogo, Location);
 
   // Return the type reflection string for this type.
   std::string
@@ -835,6 +887,41 @@ class Type
   std::string
   mangled_name(Gogo*) const;
 
+  // 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
+  backend_type_size(Gogo*, unsigned int* psize);
+
+  // If the alignment of the type can be determined, set *PALIGN to
+  // the alignment in bytes and return true.  Otherwise, return false.
+  bool
+  backend_type_align(Gogo*, unsigned int* palign);
+
+  // If the alignment of a struct field of this type can be
+  // determined, set *PALIGN to the alignment in bytes and return
+  // true.  Otherwise, return false.
+  bool
+  backend_type_field_align(Gogo*, unsigned int* palign);
+
+  // Whether the backend size is known.
+  bool
+  is_backend_type_size_known(Gogo*);
+
+  // Get the hash and equality functions for a type.
+  void
+  type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
+                Function_type* equal_fntype, Named_object** hash_fn,
+                Named_object** equal_fn);
+
+  // Write the hash and equality type functions.
+  void
+  write_specific_type_functions(Gogo*, Named_type*,
+                               const std::string& hash_name,
+                               Function_type* hash_fntype,
+                               const std::string& equal_name,
+                               Function_type* equal_fntype);
+
   // Export the type.
   void
   export_type(Export* exp) const
@@ -862,22 +949,14 @@ class Type
   do_has_pointer() const
   { return false; }
 
-  virtual unsigned int
-  do_hash_for_method(Gogo*) const;
-
   virtual bool
-  do_check_make_expression(Expression_list* args, source_location);
-
+  do_compare_is_identity(Gogo*) const = 0;
 
-  virtual tree
-  do_get_tree(Gogo*) = 0;
-
-  virtual tree
-  do_get_init_tree(Gogo*, tree, bool) = 0;
+  virtual unsigned int
+  do_hash_for_method(Gogo*) const;
 
-  virtual tree
-  do_make_expression_tree(Translate_context*, Expression_list*,
-                         source_location);
+  virtual Btype*
+  do_get_backend(Gogo*) = 0;
 
   virtual Expression*
   do_type_descriptor(Gogo*, Named_type* name) = 0;
@@ -885,24 +964,19 @@ class Type
   virtual void
   do_reflection(Gogo*, std::string*) const = 0;
 
-
   virtual void
   do_mangled_name(Gogo*, std::string*) const = 0;
 
   virtual void
   do_export(Export*) const;
 
-  // Return whether an expression is an integer.
-  static bool
-  check_int_value(Expression*, const char*, source_location);
-
   // Return whether a method expects a pointer as the receiver.
   static bool
   method_expects_pointer(const Named_object*);
 
   // Finalize the methods for a type.
   static void
-  finalize_methods(Gogo*, const Type*, source_location, Methods**);
+  finalize_methods(Gogo*, const Type*, Location, Methods**);
 
   // Return a method from a set of methods.
   static Method*
@@ -951,10 +1025,11 @@ class Type
   static unsigned int
   hash_string(const std::string&, unsigned int);
 
-  // Return a tree for the underlying type of a named type.
-  static tree
-  get_named_type_tree(Gogo* gogo, Type* base_type)
-  { return base_type->get_tree_without_hash(gogo); }
+  // Return the backend representation for the underlying type of a
+  // named type.
+  static Btype*
+  get_named_base_btype(Gogo* gogo, Type* base_type)
+  { return base_type->get_btype_without_hash(gogo); }
 
  private:
   // Convert to the desired type classification, or return NULL.  This
@@ -997,9 +1072,39 @@ class Type
            : NULL);
   }
 
-  // Get the hash and equality functions for a type.
+  // Support for are_assignable and are_assignable_hidden_ok.
+  static bool
+  are_assignable_check_hidden(const Type* lhs, const Type* rhs,
+                             bool check_hidden_fields, std::string* reason);
+
+  // Map unnamed types to type descriptor decls.
+  typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
+                            Type_identical) Type_descriptor_vars;
+
+  static Type_descriptor_vars type_descriptor_vars;
+
+  // Build the type descriptor variable for this type.
+  void
+  make_type_descriptor_var(Gogo*);
+
+  // Return the name of the type descriptor variable.  If NAME is not
+  // NULL, it is the name to use.
+  std::string
+  type_descriptor_var_name(Gogo*, Named_type* name);
+
+  // Return true if the type descriptor for this type should be
+  // defined in some other package.  If NAME is not NULL, it is the
+  // name of this type.  If this returns true it sets *PACKAGE to the
+  // package where the type descriptor is defined.
+  bool
+  type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
+
+  // Build the hash and equality type functions for a type which needs
+  // specific functions.
   void
-  type_functions(const char** hash_fn, const char** equal_fn) const;
+  specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
+                         Function_type* equal_fntype, Named_object** hash_fn,
+                         Named_object** equal_fn);
 
   // Build a composite literal for the uncommon type information.
   Expression*
@@ -1015,7 +1120,7 @@ class Type
   // Build a composite literal for one method.
   Expression*
   method_constructor(Gogo*, Type* method_type, const std::string& name,
-                    const Method*) const;
+                    const Method*, bool only_value_methods) const;
 
   static tree
   build_receive_return_type(tree type);
@@ -1049,44 +1154,59 @@ class Type
   // Build stub methods for a type.
   static void
   build_stub_methods(Gogo*, const Type* type, const Methods* methods,
-                    source_location);
+                    Location);
 
   static void
   build_one_stub_method(Gogo*, Method*, const char* receiver_name,
                        const Typed_identifier_list*, bool is_varargs,
-                       source_location);
+                       Location);
 
   static Expression*
   apply_field_indexes(Expression*, const Method::Field_indexes*,
-                     source_location);
+                     Location);
 
   // Look for a field or method named NAME in TYPE.
   static bool
   find_field_or_method(const Type* type, const std::string& name,
                       bool receiver_can_be_pointer,
-                      int* level, bool* is_method,
-                      bool* found_pointer_method,
+                      std::vector<const Named_type*>*, int* level,
+                      bool* is_method, bool* found_pointer_method,
                       std::string* ambig1, std::string* ambig2);
 
-  // Get a tree for a type without looking in the hash table for
-  // identical types.
-  tree
-  get_tree_without_hash(Gogo*);
+  // Get the backend representation for a type without looking in the
+  // hash table for identical types.
+  Btype*
+  get_btype_without_hash(Gogo*);
 
-  // A mapping from Type to tree, used to ensure that the GIMPLE
+  // A mapping from Type to Btype*, used to ensure that the backend
   // representation of identical types is identical.
-  typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
-                            Type_identical) Type_trees;
+  typedef Unordered_map_hash(const Type*, Btype*, Type_hash_identical,
+                            Type_identical) Type_btypes;
+
+  static Type_btypes type_btypes;
+
+  // A list of builtin named types.
+  static std::vector<Named_type*> named_builtin_types;
 
-  static Type_trees type_trees;
+  // A map from types which need specific type functions to the type
+  // functions themselves.
+  typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
+  typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
+                            Type_identical) Type_functions;
+
+  static Type_functions type_functions_table;
 
   // The type classification.
   Type_classification classification_;
-  // The tree representation of the type, once it has been determined.
-  tree tree_;
-  // The decl for the type descriptor for this type.  This starts out
-  // as NULL and is filled in as needed.
-  tree type_descriptor_decl_;
+  // Whether btype_ is a placeholder type used while named types are
+  // being converted.
+  bool btype_is_placeholder_;
+  // The backend representation of the type, once it has been
+  // determined.
+  Btype* btype_;
+  // The type descriptor for this type.  This starts out as NULL and
+  // is filled in as needed.
+  Bvariable* type_descriptor_var_;
 };
 
 // Type hash table operations.
@@ -1104,7 +1224,7 @@ class Type_identical
  public:
   bool
   operator()(const Type* t1, const Type* t2) const
-  { return Type::are_identical(t1, t2, NULL); }
+  { return Type::are_identical(t1, t2, false, NULL); }
 };
 
 // An identifier with a type.
@@ -1113,7 +1233,7 @@ class Typed_identifier
 {
  public:
   Typed_identifier(const std::string& name, Type* type,
-                  source_location location)
+                  Location location)
     : name_(name), type_(type), location_(location)
   { }
 
@@ -1129,7 +1249,7 @@ class Typed_identifier
 
   // Return the location where the name was seen.  This is not always
   // meaningful.
-  source_location
+  Location
   location() const
   { return this->location_; }
 
@@ -1137,7 +1257,7 @@ class Typed_identifier
   void
   set_type(Type* type)
   {
-    gcc_assert(this->type_ == NULL || type->is_error_type());
+    go_assert(this->type_ == NULL || type->is_error_type());
     this->type_ = type;
   }
 
@@ -1147,7 +1267,7 @@ class Typed_identifier
   // Type.
   Type* type_;
   // The location where the name was seen.
-  source_location location_;
+  Location location_;
 };
 
 // A list of Typed_identifiers.
@@ -1183,7 +1303,7 @@ class Typed_identifier_list
   void
   set_type(size_t i, Type* type)
   {
-    gcc_assert(i < this->entries_.size());
+    go_assert(i < this->entries_.size());
     this->entries_[i].set_type(type);
   }
 
@@ -1223,10 +1343,15 @@ class Typed_identifier_list
   void
   resize(size_t c)
   {
-    gcc_assert(c <= this->entries_.size());
-    this->entries_.resize(c, Typed_identifier("", NULL, UNKNOWN_LOCATION));
+    go_assert(c <= this->entries_.size());
+    this->entries_.resize(c, Typed_identifier("", NULL,
+                                              Linemap::unknown_location()));
   }
 
+  void
+  reserve(size_t c)
+  { this->entries_.reserve(c); }
+
   // Iterators.
 
   typedef std::vector<Typed_identifier>::iterator iterator;
@@ -1275,6 +1400,10 @@ class Integer_type : public Type
   static Integer_type*
   create_abstract_integer_type();
 
+  // Create an abstract character type.
+  static Integer_type*
+  create_abstract_character_type();
+
   // Whether this is an abstract integer type.
   bool
   is_abstract() const
@@ -1294,15 +1423,36 @@ class Integer_type : public Type
   bool
   is_identical(const Integer_type* t) const;
 
- protected:
+  // Whether this is the type "byte" or another name for "byte".
+  bool
+  is_byte() const
+  { return this->is_byte_; }
+
+  // Mark this as the "byte" type.
+  void
+  set_is_byte()
+  { this->is_byte_ = true; }
+
+  // Whether this is the type "rune" or another name for "rune".
+  bool
+  is_rune() const
+  { return this->is_rune_; }
+
+  // Mark this as the "rune" type.
+  void
+  set_is_rune()
+  { this->is_rune_ = true; }
+
+protected:
+  bool
+  do_compare_is_identity(Gogo*) const
+  { return true; }
+
   unsigned int
   do_hash_for_method(Gogo*) const;
 
-  tree
-  do_get_tree(Gogo*);
-
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1317,8 +1467,8 @@ class Integer_type : public Type
   Integer_type(bool is_abstract, bool is_unsigned, int bits,
               int runtime_type_kind)
     : Type(TYPE_INTEGER),
-      is_abstract_(is_abstract), is_unsigned_(is_unsigned), bits_(bits),
-      runtime_type_kind_(runtime_type_kind)
+      is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
+      is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
   { }
 
   // Map names of integer types to the types themselves.
@@ -1329,6 +1479,10 @@ class Integer_type : public Type
   bool is_abstract_;
   // True if this is an unsigned type.
   bool is_unsigned_;
+  // True if this is the byte type.
+  bool is_byte_;
+  // True if this is the rune type.
+  bool is_rune_;
   // The number of bits.
   int bits_;
   // The runtime type code used in the type descriptor for this type.
@@ -1366,19 +1520,16 @@ class Float_type : public Type
   bool
   is_identical(const Float_type* t) const;
 
-  // Return a tree for this type without using a Gogo*.
-  tree
-  type_tree() const;
-
  protected:
+  bool
+  do_compare_is_identity(Gogo*) const
+  { return false; }
+
   unsigned int
   do_hash_for_method(Gogo*) const;
 
-  tree
-  do_get_tree(Gogo*);
-
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1438,19 +1589,16 @@ class Complex_type : public Type
   bool
   is_identical(const Complex_type* t) const;
 
-  // Return a tree for this type without using a Gogo*.
-  tree
-  type_tree() const;
-
  protected:
+  bool
+  do_compare_is_identity(Gogo*) const
+  { return false; }
+
   unsigned int
   do_hash_for_method(Gogo*) const;
 
-  tree
-  do_get_tree(Gogo*);
-
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1502,11 +1650,12 @@ class String_type : public Type
   do_has_pointer() const
   { return true; }
 
-  tree
-  do_get_tree(Gogo*);
+  bool
+  do_compare_is_identity(Gogo*) const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo* gogo, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1528,7 +1677,7 @@ class Function_type : public Type
 {
  public:
   Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
-               Typed_identifier_list* results, source_location location)
+               Typed_identifier_list* results, Location location)
     : Type(TYPE_FUNCTION),
       receiver_(receiver), parameters_(parameters), results_(results),
       location_(location), is_varargs_(false), is_builtin_(false)
@@ -1560,7 +1709,7 @@ class Function_type : public Type
   { return this->is_builtin_; }
 
   // The location where this type was defined.
-  source_location
+  Location
   location() const
   { return this->location_; }
 
@@ -1577,7 +1726,7 @@ class Function_type : public Type
   // Whether this type is the same as T.
   bool
   is_identical(const Function_type* t, bool ignore_receiver,
-              std::string*) const;
+              bool errors_are_identical, std::string*) const;
 
   // Record that this is a varargs function.
   void
@@ -1603,6 +1752,9 @@ class Function_type : public Type
   Function_type*
   copy_with_receiver(Type*) const;
 
+  static Type*
+  make_function_type_descriptor_type();
+
  protected:
   int
   do_traverse(Traverse*);
@@ -1612,14 +1764,15 @@ class Function_type : public Type
   do_has_pointer() const
   { return true; }
 
+  bool
+  do_compare_is_identity(Gogo*) const
+  { return false; }
+
   unsigned int
   do_hash_for_method(Gogo*) const;
 
-  tree
-  do_get_tree(Gogo*);
-
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1634,9 +1787,6 @@ class Function_type : public Type
   do_export(Export*) const;
 
  private:
-  static Type*
-  make_function_type_descriptor_type();
-
   Expression*
   type_descriptor_params(Type*, const Typed_identifier*,
                         const Typed_identifier_list*);
@@ -1652,7 +1802,7 @@ class Function_type : public Type
   // The location where this type was defined.  This exists solely to
   // give a location for the fields of the struct if this function
   // returns multiple values.
-  source_location location_;
+  Location location_;
   // Whether this function takes a variable number of arguments.
   bool is_varargs_;
   // Whether this is a special builtin function which can not simply
@@ -1678,6 +1828,9 @@ class Pointer_type : public Type
   static Pointer_type*
   do_import(Import*);
 
+  static Type*
+  make_pointer_type_descriptor_type();
+
  protected:
   int
   do_traverse(Traverse*);
@@ -1686,14 +1839,15 @@ class Pointer_type : public Type
   do_has_pointer() const
   { return true; }
 
+  bool
+  do_compare_is_identity(Gogo*) const
+  { return true; }
+
   unsigned int
   do_hash_for_method(Gogo*) const;
 
-  tree
-  do_get_tree(Gogo*);
-
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1708,9 +1862,6 @@ class Pointer_type : public Type
   do_export(Export*) const;
 
  private:
-  static Type*
-  make_pointer_type_descriptor_type();
-
   // The type to which this type points.
   Type* to_type_;
 };
@@ -1728,13 +1879,17 @@ class Struct_field
   const std::string&
   field_name() const;
 
+  // Return whether this struct field is named NAME.
+  bool
+  is_field_name(const std::string& name) const;
+
   // The field type.
   Type*
   type() const
   { return this->typed_identifier_.type(); }
 
   // The field location.
-  source_location
+  Location
   location() const
   { return this->typed_identifier_.location(); }
 
@@ -1747,7 +1902,7 @@ class Struct_field
   const std::string&
   tag() const
   {
-    gcc_assert(this->tag_ != NULL);
+    go_assert(this->tag_ != NULL);
     return *this->tag_;
   }
 
@@ -1837,7 +1992,7 @@ class Struct_field_list
 class Struct_type : public Type
 {
  public:
-  Struct_type(Struct_field_list* fields, source_location location)
+  Struct_type(Struct_field_list* fields, Location location)
     : Type(TYPE_STRUCT),
       fields_(fields), location_(location), all_methods_(NULL)
   { }
@@ -1874,17 +2029,17 @@ class Struct_type : public Type
   // NULL if there is no field with that name.
   Field_reference_expression*
   field_reference(Expression* struct_expr, const std::string& name,
-                 source_location) const;
+                 Location) const;
 
   // Return the total number of fields, including embedded fields.
-  // This is the number of values which can appear in a conversion to
+  // This is the number of values that can appear in a conversion to
   // this type.
   unsigned int
   total_field_count() const;
 
   // Whether this type is identical with T.
   bool
-  is_identical(const Struct_type* t) const;
+  is_identical(const Struct_type* t, bool errors_are_identical) const;
 
   // Whether this struct type has any hidden fields.  This returns
   // true if any fields have hidden names, or if any non-pointer
@@ -1926,13 +2081,30 @@ class Struct_type : public Type
   traverse_field_types(Traverse* traverse)
   { return this->do_traverse(traverse); }
 
+  // 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
+  backend_field_offset(Gogo*, unsigned int index, unsigned int* poffset);
+
+  // Finish the backend representation of all the fields.
+  void
+  finish_backend_fields(Gogo*);
+
   // Import a struct type.
   static Struct_type*
   do_import(Import*);
 
-  // Fill in the fields for a named struct type.
-  tree
-  fill_in_tree(Gogo*, tree);
+  static Type*
+  make_struct_type_descriptor_type();
+
+  // Write the hash function for this type.
+  void
+  write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
+
+  // Write the equality function for this type.
+  void
+  write_equal_function(Gogo*, Named_type*);
 
  protected:
   int
@@ -1944,14 +2116,14 @@ class Struct_type : public Type
   bool
   do_has_pointer() const;
 
+  bool
+  do_compare_is_identity(Gogo*) const;
+
   unsigned int
   do_hash_for_method(Gogo*) const;
 
-  tree
-  do_get_tree(Gogo*);
-
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1966,17 +2138,22 @@ class Struct_type : public Type
   do_export(Export*) const;
 
  private:
+  // Used to avoid infinite loops in field_reference_depth.
+  struct Saw_named_type
+  {
+    Saw_named_type* next;
+    Named_type* nt;
+  };
+
   Field_reference_expression*
   field_reference_depth(Expression* struct_expr, const std::string& name,
-                       source_location, unsigned int* depth) const;
-
-  static Type*
-  make_struct_type_descriptor_type();
+                       Location, Saw_named_type*,
+                       unsigned int* depth) const;
 
   // The fields of the struct.
   Struct_field_list* fields_;
   // The place where the struct was declared.
-  source_location location_;
+  Location location_;
   // If this struct is unnamed, a list of methods.
   Methods* all_methods_;
 };
@@ -2003,7 +2180,7 @@ class Array_type : public Type
 
   // Whether this type is identical with T.
   bool
-  is_identical(const Array_type* t) const;
+  is_identical(const Array_type* t, bool errors_are_identical) const;
 
   // Whether this type has any hidden fields.
   bool
@@ -2026,9 +2203,31 @@ class Array_type : public Type
   static Array_type*
   do_import(Import*);
 
-  // Fill in the fields for a named slice type.
-  tree
-  fill_in_tree(Gogo*, tree);
+  // Return the backend representation of the element type.
+  Btype*
+  get_backend_element(Gogo*, bool use_placeholder);
+
+  // Return the backend representation of the length.
+  Bexpression*
+  get_backend_length(Gogo*);
+
+  // Finish the backend representation of the element type.
+  void
+  finish_backend_element(Gogo*);
+
+  static Type*
+  make_array_type_descriptor_type();
+
+  static Type*
+  make_slice_type_descriptor_type();
+
+  // Write the hash function for this type.
+  void
+  write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
+
+  // Write the equality function for this type.
+  void
+  write_equal_function(Gogo*, Named_type*);
 
  protected:
   int
@@ -2043,21 +2242,14 @@ class Array_type : public Type
     return this->length_ == NULL || this->element_type_->has_pointer();
   }
 
-  unsigned int
-  do_hash_for_method(Gogo*) const;
-
   bool
-  do_check_make_expression(Expression_list*, source_location);
-
-  tree
-  do_get_tree(Gogo*);
+  do_compare_is_identity(Gogo*) const;
 
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  unsigned int
+  do_hash_for_method(Gogo*) const;
 
-  tree
-  do_make_expression_tree(Translate_context*, Expression_list*,
-                         source_location);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -2078,12 +2270,6 @@ class Array_type : public Type
   tree
   get_length_tree(Gogo*);
 
-  Type*
-  make_array_type_descriptor_type();
-
-  Type*
-  make_slice_type_descriptor_type();
-
   Expression*
   array_type_descriptor(Gogo*, Named_type*);
 
@@ -2103,7 +2289,7 @@ class Array_type : public Type
 class Map_type : public Type
 {
  public:
-  Map_type(Type* key_type, Type* val_type, source_location location)
+  Map_type(Type* key_type, Type* val_type, Location location)
     : Type(TYPE_MAP),
       key_type_(key_type), val_type_(val_type), location_(location)
   { }
@@ -2120,12 +2306,24 @@ class Map_type : public Type
 
   // Whether this type is identical with T.
   bool
-  is_identical(const Map_type* t) const;
+  is_identical(const Map_type* t, bool errors_are_identical) const;
 
   // Import a map type.
   static Map_type*
   do_import(Import*);
 
+  static Type*
+  make_map_type_descriptor_type();
+
+  static Type*
+  make_map_descriptor_type();
+
+  // Build a map descriptor for this type.  Return a pointer to it.
+  // The location is the location which causes us to need the
+  // descriptor.
+  tree
+  map_descriptor_pointer(Gogo* gogo, Location);
+
  protected:
   int
   do_traverse(Traverse*);
@@ -2137,21 +2335,15 @@ class Map_type : public Type
   do_has_pointer() const
   { return true; }
 
-  unsigned int
-  do_hash_for_method(Gogo*) const;
-
   bool
-  do_check_make_expression(Expression_list*, source_location);
-
-  tree
-  do_get_tree(Gogo*);
+  do_compare_is_identity(Gogo*) const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  unsigned int
+  do_hash_for_method(Gogo*) const;
 
-  tree
-  do_make_expression_tree(Translate_context*, Expression_list*,
-                         source_location);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -2166,15 +2358,20 @@ class Map_type : public Type
   do_export(Export*) const;
 
  private:
-  static Type*
-  make_map_type_descriptor_type();
+  // Mapping from map types to map descriptors.
+  typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
+                            Type_identical) Map_descriptors;
+  static Map_descriptors map_descriptors;
+
+  Bvariable*
+  map_descriptor(Gogo*);
 
   // The key type.
   Type* key_type_;
   // The value type.
   Type* val_type_;
   // Where the type was defined.
-  source_location location_;
+  Location location_;
 };
 
 // The type of a channel.
@@ -2186,7 +2383,7 @@ class Channel_type : public Type
     : Type(TYPE_CHANNEL),
       may_send_(may_send), may_receive_(may_receive),
       element_type_(element_type)
-  { gcc_assert(may_send || may_receive); }
+  { go_assert(may_send || may_receive); }
 
   // Whether this channel can send data.
   bool
@@ -2206,12 +2403,15 @@ class Channel_type : public Type
 
   // Whether this type is identical with T.
   bool
-  is_identical(const Channel_type* t) const;
+  is_identical(const Channel_type* t, bool errors_are_identical) const;
 
   // Import a channel type.
   static Channel_type*
   do_import(Import*);
 
+  static Type*
+  make_chan_type_descriptor_type();
+
  protected:
   int
   do_traverse(Traverse* traverse)
@@ -2221,21 +2421,15 @@ class Channel_type : public Type
   do_has_pointer() const
   { return true; }
 
-  unsigned int
-  do_hash_for_method(Gogo*) const;
-
   bool
-  do_check_make_expression(Expression_list*, source_location);
-
-  tree
-  do_get_tree(Gogo*);
+  do_compare_is_identity(Gogo*) const
+  { return true; }
 
-  tree
-  do_get_init_tree(Gogo*, tree, bool);
+  unsigned int
+  do_hash_for_method(Gogo*) const;
 
-  tree
-  do_make_expression_tree(Translate_context*, Expression_list*,
-                         source_location);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -2250,9 +2444,6 @@ class Channel_type : public Type
   do_export(Export*) const;
 
  private:
-  static Type*
-  make_chan_type_descriptor_type();
-
   // Whether this channel can send data.
   bool may_send_;
   // Whether this channel can receive data.
@@ -2267,26 +2458,42 @@ class Channel_type : public Type
 class Interface_type : public Type
 {
  public:
-  Interface_type(Typed_identifier_list* methods, source_location location)
+  Interface_type(Typed_identifier_list* methods, Location location)
     : Type(TYPE_INTERFACE),
-      methods_(methods), location_(location)
-  { gcc_assert(methods == NULL || !methods->empty()); }
+      parse_methods_(methods), all_methods_(NULL), location_(location),
+      interface_btype_(NULL), assume_identical_(NULL),
+      methods_are_finalized_(false), seen_(false)
+  { go_assert(methods == NULL || !methods->empty()); }
+
+  // The location where the interface type was defined.
+  Location
+  location() const
+  { return this->location_; }
 
   // Return whether this is an empty interface.
   bool
   is_empty() const
-  { return this->methods_ == NULL; }
+  {
+    go_assert(this->methods_are_finalized_);
+    return this->all_methods_ == NULL;
+  }
 
   // Return the list of methods.  This will return NULL for an empty
   // interface.
   const Typed_identifier_list*
   methods() const
-  { return this->methods_; }
+  {
+    go_assert(this->methods_are_finalized_);
+    return this->all_methods_;
+  }
 
   // Return the number of methods.
   size_t
   method_count() const
-  { return this->methods_ == NULL ? 0 : this->methods_->size(); }
+  {
+    go_assert(this->methods_are_finalized_);
+    return this->all_methods_ == NULL ? 0 : this->all_methods_->size();
+  }
 
   // Return the method NAME, or NULL.
   const Typed_identifier*
@@ -2296,7 +2503,8 @@ class Interface_type : public Type
   size_t
   method_index(const std::string& name) const;
 
-  // Finalize the methods.  This handles interface inheritance.
+  // Finalize the methods.  This sets all_methods_.  This handles
+  // interface inheritance.
   void
   finalize_methods();
 
@@ -2309,7 +2517,7 @@ class Interface_type : public Type
   // Whether this type is identical with T.  REASON is as in
   // implements_interface.
   bool
-  is_identical(const Interface_type* t) const;
+  is_identical(const Interface_type* t, bool errors_are_identical) const;
 
   // Whether we can assign T to this type.  is_identical is known to
   // be false.
@@ -2325,9 +2533,16 @@ class Interface_type : public Type
   static Interface_type*
   do_import(Import*);
 
-  // Fill in the fields for a named interface type.
-  tree
-  fill_in_tree(Gogo*, tree);
+  // Make a struct for an empty interface type.
+  static Btype*
+  get_backend_empty_interface_type(Gogo*);
+
+  // Finish the backend representation of the method types.
+  void
+  finish_backend_methods(Gogo*);
+
+  static Type*
+  make_interface_type_descriptor_type();
 
  protected:
   int
@@ -2337,14 +2552,15 @@ class Interface_type : public Type
   do_has_pointer() const
   { return true; }
 
+  bool
+  do_compare_is_identity(Gogo*) const
+  { return false; }
+
   unsigned int
   do_hash_for_method(Gogo*) const;
 
-  tree
-  do_get_tree(Gogo*);
-
-  tree
-  do_get_init_tree(Gogo* gogo, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -2359,14 +2575,41 @@ class Interface_type : public Type
   do_export(Export*) const;
 
  private:
-  static Type*
-  make_interface_type_descriptor_type();
+  // This type guards against infinite recursion when comparing
+  // interface types.  We keep a list of interface types assumed to be
+  // identical during comparison.  We just keep the list on the stack.
+  // This permits us to compare cases like
+  // type I1 interface { F() interface{I1} }
+  // type I2 interface { F() interface{I2} }
+  struct Assume_identical
+  {
+    Assume_identical* next;
+    const Interface_type* t1;
+    const Interface_type* t2;
+  };
 
-  // The list of methods associated with the interface.  This will be
-  // NULL for the empty interface.
-  Typed_identifier_list* methods_;
+  bool
+  assume_identical(const Interface_type*, const Interface_type*) const;
+
+  // The list of methods associated with the interface from the
+  // parser.  This will be NULL for the empty interface.  This may
+  // include unnamed interface types.
+  Typed_identifier_list* parse_methods_;
+  // The list of all methods associated with the interface.  This
+  // expands any interface types listed in methods_.  It is set by
+  // finalize_methods.  This will be NULL for the empty interface.
+  Typed_identifier_list* all_methods_;
   // The location where the interface was defined.
-  source_location location_;
+  Location location_;
+  // The backend representation of this type during backend conversion.
+  Btype* interface_btype_;
+  // A list of interface types assumed to be identical during
+  // interface comparison.
+  mutable Assume_identical* assume_identical_;
+  // Whether the methods have been finalized.
+  bool methods_are_finalized_;
+  // Used to avoid endless recursion in do_mangled_name.
+  mutable bool seen_;
 };
 
 // The value we keep for a named type.  This lets us get the right
@@ -2378,13 +2621,15 @@ class Interface_type : public Type
 class Named_type : public Type
 {
  public:
-  Named_type(Named_object* named_object, Type* type, source_location location)
+  Named_type(Named_object* named_object, Type* type, Location location)
     : Type(TYPE_NAMED),
       named_object_(named_object), in_function_(NULL), type_(type),
       local_methods_(NULL), all_methods_(NULL),
       interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
-      location_(location), named_tree_(NULL), is_visible_(true),
-      is_error_(false), seen_(false)
+      location_(location), named_btype_(NULL), dependencies_(),
+      is_visible_(true), is_error_(false), is_placeholder_(false),
+      is_converted_(false), is_circular_(false), seen_(false),
+      seen_in_compare_is_identity_(false), seen_in_get_backend_(false)
   { }
 
   // Return the associated Named_object.  This holds the actual name.
@@ -2433,7 +2678,7 @@ class Named_type : public Type
   { return this->type_; }
 
   // Return the location.
-  source_location
+  Location
   location() const
   { return this->location_; }
 
@@ -2455,7 +2700,34 @@ class Named_type : public Type
   // Whether this is a builtin type.
   bool
   is_builtin() const
-  { return this->location_ == BUILTINS_LOCATION; }
+  { return Linemap::is_predeclared_location(this->location_); }
+
+  // Whether this is an alias.  There are currently two aliases: byte
+  // and rune.
+  bool
+  is_alias() const;
+
+  // Whether this is a circular type: a pointer or function type that
+  // refers to itself, which is not possible in C.
+  bool
+  is_circular() const
+  { return this->is_circular_; }
+
+  // Return the base type for this type.
+  Type*
+  named_base();
+
+  const Type*
+  named_base() const;
+
+  // Return whether this is an error type.
+  bool
+  is_named_error_type() const;
+
+  // Return whether this type is comparable.  If REASON is not NULL,
+  // set *REASON when returning false.
+  bool
+  named_type_is_comparable(std::string* reason) const;
 
   // Add a method to this type.
   Named_object*
@@ -2464,7 +2736,7 @@ class Named_type : public Type
   // Add a method declaration to this type.
   Named_object*
   add_method_declaration(const std::string& name, Package* package,
-                        Function_type* type, source_location location);
+                        Function_type* type, Location location);
 
   // Add an existing method--one defined before the type itself was
   // defined--to a type.
@@ -2520,6 +2792,19 @@ class Named_type : public Type
   bool
   named_type_has_hidden_fields(std::string* reason) const;
 
+  // Note that a type must be converted to the backend representation
+  // before we convert this type.
+  void
+  add_dependency(Named_type* nt)
+  { this->dependencies_.push_back(nt); }
+
+  // Return true if the size and alignment of the backend
+  // representation of this type is known.  This is always true after
+  // types have been converted, but may be false beforehand.
+  bool
+  is_named_backend_type_size_known() const
+  { return this->named_btype_ != NULL && !this->is_placeholder_; }
+
   // Export the type.
   void
   export_named_type(Export*, const std::string& name) const;
@@ -2528,6 +2813,10 @@ class Named_type : public Type
   static void
   import_named_type(Import*, Named_type**);
 
+  // Initial conversion to backend representation.
+  void
+  convert(Gogo*);
+
  protected:
   int
   do_traverse(Traverse* traverse)
@@ -2537,27 +2826,16 @@ class Named_type : public Type
   do_verify();
 
   bool
-  do_has_pointer() const
-  { return this->type_->has_pointer(); }
-
-  unsigned int
-  do_hash_for_method(Gogo*) const;
+  do_has_pointer() const;
 
   bool
-  do_check_make_expression(Expression_list* args, source_location location)
-  { return this->type_->check_make_expression(args, location); }
-
-  tree
-  do_get_tree(Gogo*);
+  do_compare_is_identity(Gogo*) const;
 
-  tree
-  do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
-  { return this->type_->get_typed_init_tree(gogo, type_tree, is_clear); }
+  unsigned int
+  do_hash_for_method(Gogo*) const;
 
-  tree
-  do_make_expression_tree(Translate_context* context, Expression_list* args,
-                         source_location location)
-  { return this->type_->make_expression_tree(context, args, location); }
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -2572,6 +2850,10 @@ class Named_type : public Type
   do_export(Export*) const;
 
  private:
+  // Create the placeholder during conversion.
+  void
+  create_placeholder(Gogo*);
+
   // A mapping from interfaces to the associated interface method
   // tables for this type.  This maps to a decl.
   typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
@@ -2597,10 +2879,19 @@ class Named_type : public Type
   // tables for pointers to this type.
   Interface_method_tables* pointer_interface_method_tables_;
   // The location where this type was defined.
-  source_location location_;
-  // The tree for this type while converting to GENERIC.  This is used
-  // to avoid endless recursion when a named type refers to itself.
-  tree named_tree_;
+  Location location_;
+  // The backend representation of this type during backend
+  // conversion.  This is used to avoid endless recursion when a named
+  // type refers to itself.
+  Btype* named_btype_;
+  // A list of types which must be converted to the backend
+  // representation before this type can be converted.  This is for
+  // cases like
+  //   type S1 { p *S2 }
+  //   type S2 { s S1 }
+  // where we can't convert S2 to the backend representation unless we
+  // have converted S1.
+  std::vector<Named_type*> dependencies_;
   // Whether this type is visible.  This is false if this type was
   // created because it was referenced by an imported object, but the
   // type itself was not exported.  This will always be true for types
@@ -2608,11 +2899,24 @@ class Named_type : public Type
   bool is_visible_;
   // Whether this type is erroneous.
   bool is_error_;
+  // Whether the current value of named_btype_ is a placeholder for
+  // which the final size of the type is not known.
+  bool is_placeholder_;
+  // Whether this type has been converted to the backend
+  // representation.  Implies that is_placeholder_ is false.
+  bool is_converted_;
+  // Whether this is a pointer or function type which refers to the
+  // type itself.
+  bool is_circular_;
   // In a recursive operation such as has_hidden_fields, this flag is
   // used to prevent infinite recursion when a type refers to itself.
   // This is mutable because it is always reset to false when the
   // function exits.
   mutable bool seen_;
+  // Like seen_, but used only by do_compare_is_identity.
+  mutable bool seen_in_compare_is_identity_;
+  // Like seen_, but used only by do_get_backend.
+  bool seen_in_get_backend_;
 };
 
 // A forward declaration.  This handles a type which has been declared
@@ -2653,8 +2957,8 @@ class Forward_declaration_type : public Type
 
   // Add a method declaration to this type.
   Named_object*
-  add_method_declaration(const std::string& name, Function_type*,
-                        source_location);
+  add_method_declaration(const std::string& name, Package*, Function_type*,
+                        Location);
 
  protected:
   int
@@ -2662,27 +2966,18 @@ class Forward_declaration_type : public Type
 
   bool
   do_has_pointer() const
-  { return this->base()->has_pointer(); }
+  { return this->real_type()->has_pointer(); }
+
+  bool
+  do_compare_is_identity(Gogo* gogo) const
+  { return this->real_type()->compare_is_identity(gogo); }
 
   unsigned int
   do_hash_for_method(Gogo* gogo) const
   { return this->real_type()->hash_for_method(gogo); }
 
-  bool
-  do_check_make_expression(Expression_list* args, source_location location)
-  { return this->base()->check_make_expression(args, location); }
-
-  tree
-  do_get_tree(Gogo* gogo);
-
-  tree
-  do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
-  { return this->base()->get_typed_init_tree(gogo, type_tree, is_clear); }
-
-  tree
-  do_make_expression_tree(Translate_context* context, Expression_list* args,
-                         source_location location)
-  { return this->base()->make_expression_tree(context, args, location); }
+  Btype*
+  do_get_backend(Gogo* gogo);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);