OSDN Git Service

compiler: Use backend interface for type sizes and alignments.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / types.h
index 45297e1..200164c 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_;
 };
@@ -454,7 +459,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 +471,19 @@ 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 Type*
   make_type_descriptor_type();
@@ -487,7 +492,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*);
@@ -517,12 +522,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.
@@ -536,6 +564,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() const
+  { return this->do_compare_is_identity(); }
+
   // 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.
@@ -589,11 +624,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
@@ -722,9 +765,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*
@@ -787,46 +830,26 @@ 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&);
-
-  // 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*);
+  is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
+                               std::vector<const Named_type*>*);
 
-  // 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);
-
-  // 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); }
+  // Convert the builtin named types.
+  static void
+  convert_builtin_named_types(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); }
+  // Return the backend representation of this type.
+  Btype*
+  get_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
@@ -838,6 +861,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*) const;
+
+  // 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
@@ -865,22 +923,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() 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;
@@ -888,24 +938,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*
@@ -954,10 +999,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
@@ -1000,9 +1046,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*
@@ -1018,7 +1094,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);
@@ -1052,16 +1128,16 @@ 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
@@ -1071,25 +1147,37 @@ class Type
                       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;
+
+  // 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_trees type_trees;
+  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_;
+  // 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.
@@ -1116,7 +1204,7 @@ class Typed_identifier
 {
  public:
   Typed_identifier(const std::string& name, Type* type,
-                  source_location location)
+                  Location location)
     : name_(name), type_(type), location_(location)
   { }
 
@@ -1132,7 +1220,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_; }
 
@@ -1140,7 +1228,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;
   }
 
@@ -1150,7 +1238,7 @@ class Typed_identifier
   // Type.
   Type* type_;
   // The location where the name was seen.
-  source_location location_;
+  Location location_;
 };
 
 // A list of Typed_identifiers.
@@ -1186,7 +1274,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);
   }
 
@@ -1226,8 +1314,9 @@ 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()));
   }
 
   // Iterators.
@@ -1298,14 +1387,15 @@ class Integer_type : public Type
   is_identical(const Integer_type* t) const;
 
  protected:
+  bool
+  do_compare_is_identity() 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*);
@@ -1369,19 +1459,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() 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*);
@@ -1441,19 +1528,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() 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*);
@@ -1505,11 +1589,12 @@ class String_type : public Type
   do_has_pointer() const
   { return true; }
 
-  tree
-  do_get_tree(Gogo*);
+  bool
+  do_compare_is_identity() const
+  { return false; }
 
-  tree
-  do_get_init_tree(Gogo* gogo, tree, bool);
+  Btype*
+  do_get_backend(Gogo*);
 
   Expression*
   do_type_descriptor(Gogo*, Named_type*);
@@ -1531,7 +1616,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)
@@ -1563,7 +1648,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_; }
 
@@ -1606,6 +1691,13 @@ class Function_type : public Type
   Function_type*
   copy_with_receiver(Type*) const;
 
+  // Finishing converting function types.
+  static void
+  convert_types(Gogo*);
+
+  static Type*
+  make_function_type_descriptor_type();
+
  protected:
   int
   do_traverse(Traverse*);
@@ -1615,14 +1707,15 @@ class Function_type : public Type
   do_has_pointer() const
   { return true; }
 
+  bool
+  do_compare_is_identity() 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*);
@@ -1637,13 +1730,20 @@ 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*);
 
+  Btype*
+  get_function_backend(Gogo*);
+
+  // A list of function types with multiple results and their
+  // placeholder backend representations, used to postpone building
+  // the structs we use for multiple results until all types are
+  // converted.
+  typedef std::vector<std::pair<Function_type*, Btype*> > Placeholders;
+  static Placeholders placeholders;
+
   // The receiver name and type.  This will be NULL for a normal
   // function, non-NULL for a method.
   Typed_identifier* receiver_;
@@ -1655,7 +1755,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
@@ -1681,6 +1781,9 @@ class Pointer_type : public Type
   static Pointer_type*
   do_import(Import*);
 
+  static Type*
+  make_pointer_type_descriptor_type();
+
  protected:
   int
   do_traverse(Traverse*);
@@ -1689,14 +1792,15 @@ class Pointer_type : public Type
   do_has_pointer() const
   { return true; }
 
+  bool
+  do_compare_is_identity() 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*);
@@ -1711,9 +1815,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_;
 };
@@ -1731,13 +1832,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(); }
 
@@ -1750,7 +1855,7 @@ class Struct_field
   const std::string&
   tag() const
   {
-    gcc_assert(this->tag_ != NULL);
+    go_assert(this->tag_ != NULL);
     return *this->tag_;
   }
 
@@ -1840,7 +1945,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)
   { }
@@ -1877,7 +1982,7 @@ 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
@@ -1929,13 +2034,26 @@ 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);
+
   // 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
@@ -1947,14 +2065,14 @@ class Struct_type : public Type
   bool
   do_has_pointer() const;
 
+  bool
+  do_compare_is_identity() 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*);
@@ -1969,17 +2087,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_;
 };
@@ -2013,6 +2136,10 @@ class Array_type : public Type
   array_has_hidden_fields(const Named_type* within, std::string* reason) const
   { return this->element_type_->has_hidden_fields(within, reason); }
 
+  // Build the hash and equality functions if necessary.
+  void
+  finalize_methods(Gogo*);
+
   // Return a tree for the pointer to the values in an array.
   tree
   value_pointer_tree(Gogo*, tree array) const;
@@ -2029,9 +2156,27 @@ 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*);
+
+  // Return the backend representation of the length.
+  Bexpression*
+  get_backend_length(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
@@ -2046,21 +2191,18 @@ 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() const
+  {
+    return (this->length_ != NULL
+           && this->element_type_->compare_is_identity());
+  }
 
-  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*);
@@ -2081,12 +2223,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*);
 
@@ -2106,7 +2242,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)
   { }
@@ -2129,6 +2265,18 @@ class Map_type : public 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*);
@@ -2140,21 +2288,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() 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*);
@@ -2169,15 +2311,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.
@@ -2189,7 +2336,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
@@ -2215,6 +2362,9 @@ class Channel_type : public Type
   static Channel_type*
   do_import(Import*);
 
+  static Type*
+  make_chan_type_descriptor_type();
+
  protected:
   int
   do_traverse(Traverse* traverse)
@@ -2224,21 +2374,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() 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*);
@@ -2253,9 +2397,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.
@@ -2270,10 +2411,15 @@ 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()); }
+  { 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
@@ -2328,9 +2474,12 @@ 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*);
+
+  static Type*
+  make_interface_type_descriptor_type();
 
  protected:
   int
@@ -2340,14 +2489,15 @@ class Interface_type : public Type
   do_has_pointer() const
   { return true; }
 
+  bool
+  do_compare_is_identity() 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*);
@@ -2362,14 +2512,11 @@ class Interface_type : public Type
   do_export(Export*) const;
 
  private:
-  static Type*
-  make_interface_type_descriptor_type();
-
   // The list of methods associated with the interface.  This will be
   // NULL for the empty interface.
   Typed_identifier_list* methods_;
   // The location where the interface was defined.
-  source_location location_;
+  Location location_;
 };
 
 // The value we keep for a named type.  This lets us get the right
@@ -2381,13 +2528,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_(0)
+      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_get_backend_(false)
   { }
 
   // Return the associated Named_object.  This holds the actual name.
@@ -2436,7 +2585,7 @@ class Named_type : public Type
   { return this->type_; }
 
   // Return the location.
-  source_location
+  Location
   location() const
   { return this->location_; }
 
@@ -2458,7 +2607,13 @@ 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 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*
@@ -2471,6 +2626,11 @@ class Named_type : public 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*
   add_method(const std::string& name, Function*);
@@ -2478,7 +2638,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.
@@ -2534,6 +2694,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;
@@ -2542,6 +2715,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)
@@ -2551,27 +2728,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() 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*);
@@ -2586,6 +2752,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,
@@ -2611,10 +2781,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
@@ -2622,11 +2801,22 @@ 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 int seen_;
+  mutable bool seen_;
+  // 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
@@ -2668,7 +2858,7 @@ 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);
+                        Location);
 
  protected:
   int
@@ -2676,27 +2866,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() const
+  { return this->real_type()->compare_is_identity(); }
 
   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*);