OSDN Git Service

compiler: Fix crash in go/defer of some builtin functions.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / statements.cc
index c6d2ace..58057f8 100644 (file)
@@ -20,7 +20,7 @@
 // Class Statement.
 
 Statement::Statement(Statement_classification classification,
-                    source_location location)
+                    Location location)
   : classification_(classification), location_(location)
 {
 }
@@ -175,7 +175,7 @@ Statement::report_error(const char* msg)
 class Error_statement : public Statement
 {
  public:
-  Error_statement(source_location location)
+  Error_statement(Location location)
     : Statement(STATEMENT_ERROR, location)
   { }
 
@@ -204,7 +204,7 @@ Error_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 // Make an error statement.
 
 Statement*
-Statement::make_error_statement(source_location location)
+Statement::make_error_statement(Location location)
 {
   return new Error_statement(location);
 }
@@ -268,7 +268,7 @@ Variable_declaration_statement::do_get_backend(Translate_context* context)
   // Something takes the address of this variable, so the value is
   // stored in the heap.  Initialize it to newly allocated memory
   // space, and assign the initial value to the new space.
-  source_location loc = this->location();
+  Location loc = this->location();
   Named_object* newfn = context->gogo()->lookup_global("new");
   go_assert(newfn != NULL && newfn->is_function_declaration());
   Expression* func = Expression::make_func_reference(newfn, NULL, loc);
@@ -308,11 +308,11 @@ Variable_declaration_statement::do_dump_statement(
     Ast_dump_context* ast_dump_context) const
 {
   ast_dump_context->print_indent();
-  
+
   go_assert(var_->is_variable());
   ast_dump_context->ostream() << "var " << this->var_->name() <<  " ";
   Variable* var = this->var_->var_value();
-  if (var->has_type()) 
+  if (var->has_type())
     {
       ast_dump_context->dump_type(var->type());
       ast_dump_context->ostream() << " ";
@@ -402,7 +402,13 @@ Temporary_statement::do_check_types(Gogo*)
   if (this->type_ != NULL && this->init_ != NULL)
     {
       std::string reason;
-      if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
+      bool ok;
+      if (this->are_hidden_fields_ok_)
+       ok = Type::are_assignable_hidden_ok(this->type_, this->init_->type(),
+                                           &reason);
+      else
+       ok = Type::are_assignable(this->type_, this->init_->type(), &reason);
+      if (!ok)
        {
          if (reason.empty())
            error_at(this->location(), "incompatible types in assignment");
@@ -478,7 +484,6 @@ Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
     {
       ast_dump_context->ostream() << " ";
       ast_dump_context->dump_type(this->type_);
-      
     }
   if (this->init_ != NULL)
     {
@@ -492,7 +497,7 @@ Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 
 Temporary_statement*
 Statement::make_temporary(Type* type, Expression* init,
-                         source_location location)
+                         Location location)
 {
   return new Temporary_statement(type, init, location);
 }
@@ -503,11 +508,17 @@ class Assignment_statement : public Statement
 {
  public:
   Assignment_statement(Expression* lhs, Expression* rhs,
-                      source_location location)
+                      Location location)
     : Statement(STATEMENT_ASSIGNMENT, location),
-      lhs_(lhs), rhs_(rhs)
+      lhs_(lhs), rhs_(rhs), are_hidden_fields_ok_(false)
   { }
 
+  // Note that it is OK for this assignment statement to set hidden
+  // fields.
+  void
+  set_hidden_fields_are_ok()
+  { this->are_hidden_fields_ok_ = true; }
+
  protected:
   int
   do_traverse(Traverse* traverse);
@@ -532,6 +543,9 @@ class Assignment_statement : public Statement
   Expression* lhs_;
   // Right hand side--the rvalue.
   Expression* rhs_;
+  // True if this statement may set hidden fields in the assignment
+  // statement.  This is used for generated method stubs.
+  bool are_hidden_fields_ok_;
 };
 
 // Traversal.
@@ -580,7 +594,12 @@ Assignment_statement::do_check_types(Gogo*)
   Type* lhs_type = this->lhs_->type();
   Type* rhs_type = this->rhs_->type();
   std::string reason;
-  if (!Type::are_assignable(lhs_type, rhs_type, &reason))
+  bool ok;
+  if (this->are_hidden_fields_ok_)
+    ok = Type::are_assignable_hidden_ok(lhs_type, rhs_type, &reason);
+  else
+    ok = Type::are_assignable(lhs_type, rhs_type, &reason);
+  if (!ok)
     {
       if (reason.empty())
        error_at(this->location(), "incompatible types in assignment");
@@ -628,11 +647,52 @@ Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
 
 Statement*
 Statement::make_assignment(Expression* lhs, Expression* rhs,
-                          source_location location)
+                          Location location)
 {
   return new Assignment_statement(lhs, rhs, location);
 }
 
+// The Move_subexpressions class is used to move all top-level
+// subexpressions of an expression.  This is used for things like
+// index expressions in which we must evaluate the index value before
+// it can be changed by a multiple assignment.
+
+class Move_subexpressions : public Traverse
+{
+ public:
+  Move_subexpressions(int skip, Block* block)
+    : Traverse(traverse_expressions),
+      skip_(skip), block_(block)
+  { }
+
+ protected:
+  int
+  expression(Expression**);
+
+ private:
+  // The number of subexpressions to skip moving.  This is used to
+  // avoid moving the array itself, as we only need to move the index.
+  int skip_;
+  // The block where new temporary variables should be added.
+  Block* block_;
+};
+
+int
+Move_subexpressions::expression(Expression** pexpr)
+{
+  if (this->skip_ > 0)
+    --this->skip_;
+  else if ((*pexpr)->temporary_reference_expression() == NULL)
+    {
+      Location loc = (*pexpr)->location();
+      Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
+      this->block_->add_statement(temp);
+      *pexpr = Expression::make_temporary_reference(temp, loc);
+    }
+  // We only need to move top-level subexpressions.
+  return TRAVERSE_SKIP_COMPONENTS;
+}
+
 // The Move_ordered_evals class is used to find any subexpressions of
 // an expression that have an evaluation order dependency.  It creates
 // temporary variables to hold them.
@@ -660,9 +720,18 @@ Move_ordered_evals::expression(Expression** pexpr)
   // We have to look at subexpressions first.
   if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
     return TRAVERSE_EXIT;
+
+  int i;
+  if ((*pexpr)->must_eval_subexpressions_in_order(&i))
+    {
+      Move_subexpressions ms(i, this->block_);
+      if ((*pexpr)->traverse_subexpressions(&ms) == TRAVERSE_EXIT)
+       return TRAVERSE_EXIT;
+    }
+
   if ((*pexpr)->must_eval_in_order())
     {
-      source_location loc = (*pexpr)->location();
+      Location loc = (*pexpr)->location();
       Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
       this->block_->add_statement(temp);
       *pexpr = Expression::make_temporary_reference(temp, loc);
@@ -676,7 +745,7 @@ class Assignment_operation_statement : public Statement
 {
  public:
   Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
-                                source_location location)
+                                Location location)
     : Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
       op_(op), lhs_(lhs), rhs_(rhs)
   { }
@@ -725,7 +794,7 @@ Statement*
 Assignment_operation_statement::do_lower(Gogo*, Named_object*,
                                         Block* enclosing, Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   // We have to evaluate the left hand side expression only once.  We
   // do this by moving out any expression with side effects.
@@ -797,7 +866,7 @@ Assignment_operation_statement::do_dump_statement(
 {
   ast_dump_context->print_indent();
   ast_dump_context->dump_expression(this->lhs_);
-  ast_dump_context->dump_operator(this->op_); 
+  ast_dump_context->dump_operator(this->op_);
   ast_dump_context->dump_expression(this->rhs_);
   ast_dump_context->ostream() << std::endl;
 }
@@ -806,7 +875,7 @@ Assignment_operation_statement::do_dump_statement(
 
 Statement*
 Statement::make_assignment_operation(Operator op, Expression* lhs,
-                                    Expression* rhs, source_location location)
+                                    Expression* rhs, Location location)
 {
   return new Assignment_operation_statement(op, lhs, rhs, location);
 }
@@ -819,11 +888,17 @@ class Tuple_assignment_statement : public Statement
 {
  public:
   Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
-                            source_location location)
+                            Location location)
     : Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
-      lhs_(lhs), rhs_(rhs)
+      lhs_(lhs), rhs_(rhs), are_hidden_fields_ok_(false)
   { }
 
+  // Note that it is OK for this assignment statement to set hidden
+  // fields.
+  void
+  set_hidden_fields_are_ok()
+  { this->are_hidden_fields_ok_ = true; }
+
  protected:
   int
   do_traverse(Traverse* traverse);
@@ -847,6 +922,9 @@ class Tuple_assignment_statement : public Statement
   Expression_list* lhs_;
   // Right hand side--a list of rvalues.
   Expression_list* rhs_;
+  // True if this statement may set hidden fields in the assignment
+  // statement.  This is used for generated method stubs.
+  bool are_hidden_fields_ok_;
 };
 
 // Traversal.
@@ -866,17 +944,17 @@ Statement*
 Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
                                     Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Block* b = new Block(enclosing, loc);
 
   // First move out any subexpressions on the left hand side.  The
   // right hand side will be evaluated in the required order anyhow.
   Move_ordered_evals moe(b);
-  for (Expression_list::const_iterator plhs = this->lhs_->begin();
+  for (Expression_list::iterator plhs = this->lhs_->begin();
        plhs != this->lhs_->end();
        ++plhs)
-    (*plhs)->traverse_subexpressions(&moe);
+    Expression::traverse(&*plhs, &moe);
 
   std::vector<Temporary_statement*> temps;
   temps.reserve(this->lhs_->size());
@@ -896,12 +974,14 @@ Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
 
       if ((*plhs)->is_sink_expression())
        {
-         b->add_statement(Statement::make_statement(*prhs));
+         b->add_statement(Statement::make_statement(*prhs, true));
          continue;
        }
 
       Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
                                                            *prhs, loc);
+      if (this->are_hidden_fields_ok_)
+       temp->set_hidden_fields_are_ok();
       b->add_statement(temp);
       temps.push_back(temp);
 
@@ -925,10 +1005,15 @@ Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
 
       Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
       Statement* s = Statement::make_assignment(*plhs, ref, loc);
+      if (this->are_hidden_fields_ok_)
+       {
+         Assignment_statement* as = static_cast<Assignment_statement*>(s);
+         as->set_hidden_fields_are_ok();
+       }
       b->add_statement(s);
       ++ptemp;
     }
-  go_assert(ptemp == temps.end());
+  go_assert(ptemp == temps.end() || saw_errors());
 
   return Statement::make_block_statement(b, loc);
 }
@@ -950,7 +1035,7 @@ Tuple_assignment_statement::do_dump_statement(
 
 Statement*
 Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
-                                source_location location)
+                                Location location)
 {
   return new Tuple_assignment_statement(lhs, rhs, location);
 }
@@ -963,7 +1048,7 @@ class Tuple_map_assignment_statement : public Statement
 public:
   Tuple_map_assignment_statement(Expression* val, Expression* present,
                                 Expression* map_index,
-                                source_location location)
+                                Location location)
     : Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
       val_(val), present_(present), map_index_(map_index)
   { }
@@ -1012,7 +1097,7 @@ Statement*
 Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
                                         Block* enclosing, Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Map_index_expression* map_index = this->map_index_->map_index_expression();
   if (map_index == NULL)
@@ -1050,14 +1135,16 @@ Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
     Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
   b->add_statement(present_temp);
 
-  // present_temp = mapaccess2(MAP, &key_temp, &val_temp)
+  // present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
+  Expression* a1 = Expression::make_type_descriptor(map_type, loc);
+  Expression* a2 = map_index->map();
   Temporary_reference_expression* ref =
     Expression::make_temporary_reference(key_temp, loc);
-  Expression* a1 = Expression::make_unary(OPERATOR_AND, ref, loc);
+  Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
   ref = Expression::make_temporary_reference(val_temp, loc);
-  Expression* a2 = Expression::make_unary(OPERATOR_AND, ref, loc);
-  Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 3,
-                                       map_index->map(), a1, a2);
+  Expression* a4 = Expression::make_unary(OPERATOR_AND, ref, loc);
+  Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 4,
+                                       a1, a2, a3, a4);
 
   ref = Expression::make_temporary_reference(present_temp, loc);
   ref->set_is_lvalue();
@@ -1097,7 +1184,7 @@ Tuple_map_assignment_statement::do_dump_statement(
 Statement*
 Statement::make_tuple_map_assignment(Expression* val, Expression* present,
                                     Expression* map_index,
-                                    source_location location)
+                                    Location location)
 {
   return new Tuple_map_assignment_statement(val, present, map_index, location);
 }
@@ -1110,7 +1197,7 @@ class Map_assignment_statement : public Statement
  public:
   Map_assignment_statement(Expression* map_index,
                           Expression* val, Expression* should_set,
-                          source_location location)
+                          Location location)
     : Statement(STATEMENT_MAP_ASSIGNMENT, location),
       map_index_(map_index), val_(val), should_set_(should_set)
   { }
@@ -1159,7 +1246,7 @@ Statement*
 Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
                                   Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Map_index_expression* map_index = this->map_index_->map_index_expression();
   if (map_index == NULL)
@@ -1205,7 +1292,7 @@ Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
   Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
   Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
                                        p1, p2, p3, p4);
-  Statement* s = Statement::make_statement(call);
+  Statement* s = Statement::make_statement(call, true);
   b->add_statement(s);
 
   return Statement::make_block_statement(b, loc);
@@ -1231,7 +1318,7 @@ Map_assignment_statement::do_dump_statement(
 Statement*
 Statement::make_map_assignment(Expression* map_index,
                               Expression* val, Expression* should_set,
-                              source_location location)
+                              Location location)
 {
   return new Map_assignment_statement(map_index, val, should_set, location);
 }
@@ -1242,10 +1329,9 @@ class Tuple_receive_assignment_statement : public Statement
 {
  public:
   Tuple_receive_assignment_statement(Expression* val, Expression* closed,
-                                    Expression* channel, bool for_select,
-                                    source_location location)
+                                    Expression* channel, Location location)
     : Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
-      val_(val), closed_(closed), channel_(channel), for_select_(for_select)
+      val_(val), closed_(closed), channel_(channel)
   { }
 
  protected:
@@ -1273,8 +1359,6 @@ class Tuple_receive_assignment_statement : public Statement
   Expression* closed_;
   // The channel on which we receive the value.
   Expression* channel_;
-  // Whether this is for a select statement.
-  bool for_select_;
 };
 
 // Traversal.
@@ -1295,7 +1379,7 @@ Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
                                             Block* enclosing,
                                             Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Channel_type* channel_type = this->channel_->type()->channel_type();
   if (channel_type == NULL)
@@ -1327,14 +1411,14 @@ Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
     Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
   b->add_statement(closed_temp);
 
-  // closed_temp = chanrecv[23](channel, &val_temp)
+  // closed_temp = chanrecv2(type, channel, &val_temp)
+  Expression* td = Expression::make_type_descriptor(this->channel_->type(),
+                                                   loc);
   Temporary_reference_expression* ref =
     Expression::make_temporary_reference(val_temp, loc);
   Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
-  Expression* call = Runtime::make_call((this->for_select_
-                                        ? Runtime::CHANRECV3
-                                        : Runtime::CHANRECV2),
-                                       loc, 2, this->channel_, p2);
+  Expression* call = Runtime::make_call(Runtime::CHANRECV2,
+                                       loc, 3, td, this->channel_, p2);
   ref = Expression::make_temporary_reference(closed_temp, loc);
   ref->set_is_lvalue();
   Statement* s = Statement::make_assignment(ref, call, loc);
@@ -1373,11 +1457,10 @@ Tuple_receive_assignment_statement::do_dump_statement(
 Statement*
 Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
                                         Expression* channel,
-                                        bool for_select,
-                                        source_location location)
+                                        Location location)
 {
   return new Tuple_receive_assignment_statement(val, closed, channel,
-                                               for_select, location);
+                                               location);
 }
 
 // An assignment to a pair of values from a type guard.  This is a
@@ -1388,7 +1471,7 @@ class Tuple_type_guard_assignment_statement : public Statement
  public:
   Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
                                        Expression* expr, Type* type,
-                                       source_location location)
+                                       Location location)
     : Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
       val_(val), ok_(ok), expr_(expr), type_(type)
   { }
@@ -1447,7 +1530,7 @@ Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
                                                Block* enclosing,
                                                Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Type* expr_type = this->expr_->type();
   if (expr_type->interface_type() == NULL)
@@ -1512,7 +1595,7 @@ Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
 Call_expression*
 Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
   return Runtime::make_call(code, loc, 2,
                            Expression::make_type_descriptor(this->type_, loc),
                            this->expr_);
@@ -1525,7 +1608,7 @@ Tuple_type_guard_assignment_statement::lower_to_object_type(
     Block* b,
     Runtime::Function code)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   // var val_temp TYPE
   Temporary_statement* val_temp = Statement::make_temporary(this->type_,
@@ -1548,7 +1631,7 @@ Tuple_type_guard_assignment_statement::lower_to_object_type(
 
 // Dump the AST representation for a tuple type guard statement.
 
-void 
+void
 Tuple_type_guard_assignment_statement::do_dump_statement(
     Ast_dump_context* ast_dump_context) const
 {
@@ -1568,7 +1651,7 @@ Tuple_type_guard_assignment_statement::do_dump_statement(
 Statement*
 Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
                                            Expression* expr, Type* type,
-                                           source_location location)
+                                           Location location)
 {
   return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
                                                   location);
@@ -1579,9 +1662,9 @@ Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
 class Expression_statement : public Statement
 {
  public:
-  Expression_statement(Expression* expr)
+  Expression_statement(Expression* expr, bool is_ignored)
     : Statement(STATEMENT_EXPRESSION, expr->location()),
-      expr_(expr)
+      expr_(expr), is_ignored_(is_ignored)
   { }
 
   Expression*
@@ -1597,6 +1680,9 @@ class Expression_statement : public Statement
   do_determine_types()
   { this->expr_->determine_type_no_context(); }
 
+  void
+  do_check_types(Gogo*);
+
   bool
   do_may_fall_through() const;
 
@@ -1608,8 +1694,21 @@ class Expression_statement : public Statement
 
  private:
   Expression* expr_;
+  // Whether the value of this expression is being explicitly ignored.
+  bool is_ignored_;
 };
 
+// Check the types of an expression statement.  The only check we do
+// is to possibly give an error about discarding the value of the
+// expression.
+
+void
+Expression_statement::do_check_types(Gogo*)
+{
+  if (!this->is_ignored_)
+    this->expr_->discarding_value();
+}
+
 // An expression statement may fall through unless it is a call to a
 // function which does not return.
 
@@ -1652,7 +1751,7 @@ Expression_statement::do_get_backend(Translate_context* context)
 
 // Dump the AST representation for an expression statement
 
-void 
+void
 Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
     const
 {
@@ -1664,9 +1763,9 @@ Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
 // Make an expression statement from an Expression.
 
 Statement*
-Statement::make_statement(Expression* expr)
+Statement::make_statement(Expression* expr, bool is_ignored)
 {
-  return new Expression_statement(expr);
+  return new Expression_statement(expr, is_ignored);
 }
 
 // A block statement--a list of statements which may include variable
@@ -1675,7 +1774,7 @@ Statement::make_statement(Expression* expr)
 class Block_statement : public Statement
 {
  public:
-  Block_statement(Block* block, source_location location)
+  Block_statement(Block* block, Location location)
     : Statement(STATEMENT_BLOCK, location),
       block_(block)
   { }
@@ -1723,7 +1822,7 @@ Block_statement::do_dump_statement(Ast_dump_context*) const
 // Make a block statement.
 
 Statement*
-Statement::make_block_statement(Block* block, source_location location)
+Statement::make_block_statement(Block* block, Location location)
 {
   return new Block_statement(block, location);
 }
@@ -1769,7 +1868,7 @@ class Inc_dec_statement : public Statement
 Statement*
 Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   mpz_t oval;
   mpz_init_set_ui(oval, 1UL);
@@ -1809,15 +1908,11 @@ Statement::make_dec_statement(Expression* expr)
 // Class Thunk_statement.  This is the base class for go and defer
 // statements.
 
-const char* const Thunk_statement::thunk_field_fn = "fn";
-
-const char* const Thunk_statement::thunk_field_receiver = "receiver";
-
 // Constructor.
 
 Thunk_statement::Thunk_statement(Statement_classification classification,
                                 Call_expression* call,
-                                source_location location)
+                                Location location)
     : Statement(classification, location),
       call_(call), struct_type_(NULL)
 {
@@ -1863,8 +1958,7 @@ Thunk_statement::is_simple(Function_type* fntype) const
   // If this calls something which is not a simple function, then we
   // need a thunk.
   Expression* fn = this->call_->call_expression()->fn();
-  if (fn->bound_method_expression() != NULL
-      || fn->interface_field_reference_expression() != NULL)
+  if (fn->interface_field_reference_expression() != NULL)
     return false;
 
   return true;
@@ -1912,6 +2006,8 @@ Thunk_statement::do_determine_types()
 void
 Thunk_statement::do_check_types(Gogo*)
 {
+  if (!this->call_->discarding_value())
+    return;
   Call_expression* ce = this->call_->call_expression();
   if (ce == NULL)
     {
@@ -1919,14 +2015,6 @@ Thunk_statement::do_check_types(Gogo*)
        this->report_error("expected call expression");
       return;
     }
-  Function_type* fntype = ce->get_function_type();
-  if (fntype != NULL && fntype->is_method())
-    {
-      Expression* fn = ce->fn();
-      if (fn->bound_method_expression() == NULL
-         && fn->interface_field_reference_expression() == NULL)
-       this->report_error(_("no object for method call"));
-    }
 }
 
 // The Traverse class used to find and simplify thunk statements.
@@ -1992,6 +2080,29 @@ Gogo::simplify_thunk_statements()
   this->traverse(&thunk_traverse);
 }
 
+// Return true if the thunk function is a constant, which means that
+// it does not need to be passed to the thunk routine.
+
+bool
+Thunk_statement::is_constant_function() const
+{
+  Call_expression* ce = this->call_->call_expression();
+  Function_type* fntype = ce->get_function_type();
+  if (fntype == NULL)
+    {
+      go_assert(saw_errors());
+      return false;
+    }
+  if (fntype->is_builtin())
+    return true;
+  Expression* fn = ce->fn();
+  if (fn->func_expression() != NULL)
+    return fn->func_expression()->closure() == NULL;
+  if (fn->interface_field_reference_expression() != NULL)
+    return true;
+  return false;
+}
+
 // Simplify complex thunk statements into simple ones.  A complicated
 // thunk statement is one which takes anything other than zero
 // parameters or a single pointer parameter.  We rewrite it into code
@@ -2029,17 +2140,15 @@ Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
     return false;
 
   Expression* fn = ce->fn();
-  Bound_method_expression* bound_method = fn->bound_method_expression();
   Interface_field_reference_expression* interface_method =
     fn->interface_field_reference_expression();
-  const bool is_method = bound_method != NULL || interface_method != NULL;
 
-  source_location location = this->location();
+  Location location = this->location();
 
   std::string thunk_name = Gogo::thunk_name();
 
   // Build the thunk.
-  this->build_thunk(gogo, thunk_name, fntype);
+  this->build_thunk(gogo, thunk_name);
 
   // Generate code to call the thunk.
 
@@ -2047,38 +2156,11 @@ Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
   // argument to the thunk.
 
   Expression_list* vals = new Expression_list();
-  if (fntype->is_builtin())
-    ;
-  else if (!is_method)
+  if (!this->is_constant_function())
     vals->push_back(fn);
-  else if (interface_method != NULL)
-    vals->push_back(interface_method->expr());
-  else if (bound_method != NULL)
-    {
-      vals->push_back(bound_method->method());
-      Expression* first_arg = bound_method->first_argument();
 
-      // We always pass a pointer when calling a method.
-      if (first_arg->type()->points_to() == NULL)
-       first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
-
-      // If we are calling a method which was inherited from an
-      // embedded struct, and the method did not get a stub, then the
-      // first type may be wrong.
-      Type* fatype = bound_method->first_argument_type();
-      if (fatype != NULL)
-       {
-         if (fatype->points_to() == NULL)
-           fatype = Type::make_pointer_type(fatype);
-         Type* unsafe = Type::make_pointer_type(Type::make_void_type());
-         first_arg = Expression::make_cast(unsafe, first_arg, location);
-         first_arg = Expression::make_cast(fatype, first_arg, location);
-       }
-
-      vals->push_back(first_arg);
-    }
-  else
-    go_unreachable();
+  if (interface_method != NULL)
+    vals->push_back(interface_method->expr());
 
   if (ce->args() != NULL)
     {
@@ -2146,52 +2228,40 @@ Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
 Struct_type*
 Thunk_statement::build_struct(Function_type* fntype)
 {
-  source_location location = this->location();
+  Location location = this->location();
 
   Struct_field_list* fields = new Struct_field_list();
 
   Call_expression* ce = this->call_->call_expression();
   Expression* fn = ce->fn();
 
+  if (!this->is_constant_function())
+    {
+      // The function to call.
+      fields->push_back(Struct_field(Typed_identifier("fn", fntype,
+                                                     location)));
+    }
+
+  // If this thunk statement calls a method on an interface, we pass
+  // the interface object to the thunk.
   Interface_field_reference_expression* interface_method =
     fn->interface_field_reference_expression();
   if (interface_method != NULL)
     {
-      // If this thunk statement calls a method on an interface, we
-      // pass the interface object to the thunk.
-      Typed_identifier tid(Thunk_statement::thunk_field_fn,
-                          interface_method->expr()->type(),
+      Typed_identifier tid("object", interface_method->expr()->type(),
                           location);
       fields->push_back(Struct_field(tid));
     }
-  else if (!fntype->is_builtin())
-    {
-      // The function to call.
-      Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
-      fields->push_back(Struct_field(tid));
-    }
-  else if (ce->is_recover_call())
+
+  // The predeclared recover function has no argument.  However, we
+  // add an argument when building recover thunks.  Handle that here.
+  if (ce->is_recover_call())
     {
-      // The predeclared recover function has no argument.  However,
-      // we add an argument when building recover thunks.  Handle that
-      // here.
       fields->push_back(Struct_field(Typed_identifier("can_recover",
                                                      Type::lookup_bool_type(),
                                                      location)));
     }
 
-  if (fn->bound_method_expression() != NULL)
-    {
-      go_assert(fntype->is_method());
-      Type* rtype = fntype->receiver()->type();
-      // We always pass the receiver as a pointer.
-      if (rtype->points_to() == NULL)
-       rtype = Type::make_pointer_type(rtype);
-      Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
-                          location);
-      fields->push_back(Struct_field(tid));
-    }
-
   const Expression_list* args = ce->args();
   if (args != NULL)
     {
@@ -2214,10 +2284,9 @@ Thunk_statement::build_struct(Function_type* fntype)
 // artificial, function.
 
 void
-Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
-                            Function_type* fntype)
+Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
 {
-  source_location location = this->location();
+  Location location = this->location();
 
   Call_expression* ce = this->call_->call_expression();
 
@@ -2269,10 +2338,10 @@ Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
 
   // For a defer statement, start with a call to
   // __go_set_defer_retaddr.  */
-  Label* retaddr_label = NULL; 
+  Label* retaddr_label = NULL;
   if (may_call_recover)
     {
-      retaddr_label = gogo->add_label_reference("retaddr");
+      retaddr_label = gogo->add_label_reference("retaddr", location, false);
       Expression* arg = Expression::make_label_addr(retaddr_label, location);
       Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
                                            location, 1, arg);
@@ -2302,43 +2371,33 @@ Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
   thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
                                           location);
 
-  Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
   Interface_field_reference_expression* interface_method =
     ce->fn()->interface_field_reference_expression();
 
   Expression* func_to_call;
   unsigned int next_index;
-  if (!fntype->is_builtin())
-    {
-      func_to_call = Expression::make_field_reference(thunk_parameter,
-                                                     0, location);
-      next_index = 1;
-    }
-  else
+  if (this->is_constant_function())
     {
-      go_assert(bound_method == NULL && interface_method == NULL);
       func_to_call = ce->fn();
       next_index = 0;
     }
-
-  if (bound_method != NULL)
+  else
     {
-      Expression* r = Expression::make_field_reference(thunk_parameter, 1,
-                                                      location);
-      // The main program passes in a function pointer from the
-      // interface expression, so here we can make a bound method in
-      // all cases.
-      func_to_call = Expression::make_bound_method(r, func_to_call,
-                                                  location);
-      next_index = 2;
+      func_to_call = Expression::make_field_reference(thunk_parameter,
+                                                     0, location);
+      next_index = 1;
     }
-  else if (interface_method != NULL)
+
+  if (interface_method != NULL)
     {
       // The main program passes the interface object.
+      go_assert(next_index == 0);
+      Expression* r = Expression::make_field_reference(thunk_parameter, 0,
+                                                      location);
       const std::string& name(interface_method->name());
-      func_to_call = Expression::make_interface_field_reference(func_to_call,
-                                                               name,
+      func_to_call = Expression::make_interface_field_reference(r, name,
                                                                location);
+      next_index = 1;
     }
 
   Expression_list* call_params = new Expression_list();
@@ -2374,7 +2433,14 @@ Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
 
   Call_expression* call = Expression::make_call(func_to_call, call_params,
                                                false, location);
-  Statement* call_statement = Statement::make_statement(call);
+
+  // This call expression was already lowered before entering the
+  // thunk statement.  Don't try to lower varargs again, as that will
+  // cause confusion for, e.g., method calls which already have a
+  // receiver parameter.
+  call->set_varargs_are_lowered();
+
+  Statement* call_statement = Statement::make_statement(call, true);
 
   gogo->add_statement(call_statement);
 
@@ -2407,11 +2473,15 @@ Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
       Expression_statement* es =
        static_cast<Expression_statement*>(call_statement);
       Call_expression* ce = es->expr()->call_expression();
-      go_assert(ce != NULL);
-      if (may_call_recover)
-       ce->set_is_deferred();
-      if (recover_arg != NULL)
-       ce->set_recover_arg(recover_arg);
+      if (ce == NULL)
+       go_assert(saw_errors());
+      else
+       {
+         if (may_call_recover)
+           ce->set_is_deferred();
+         if (recover_arg != NULL)
+           ce->set_recover_arg(recover_arg);
+       }
     }
 
   // That is all the thunk has to do.
@@ -2473,7 +2543,7 @@ Go_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 // Make a go statement.
 
 Statement*
-Statement::make_go_statement(Call_expression* call, source_location location)
+Statement::make_go_statement(Call_expression* call, Location location)
 {
   return new Go_statement(call, location);
 }
@@ -2488,7 +2558,7 @@ Defer_statement::do_get_backend(Translate_context* context)
   if (!this->get_fn_and_arg(&fn, &arg))
     return context->backend()->error_statement();
 
-  source_location loc = this->location();
+  Location loc = this->location();
   Expression* ds = context->function()->func_value()->defer_stack(loc);
 
   Expression* call = Runtime::make_call(Runtime::DEFER, loc, 3,
@@ -2513,7 +2583,7 @@ Defer_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 
 Statement*
 Statement::make_defer_statement(Call_expression* call,
-                               source_location location)
+                               Location location)
 {
   return new Defer_statement(call, location);
 }
@@ -2539,11 +2609,10 @@ Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
 
 // Lower a return statement.  If we are returning a function call
 // which returns multiple values which match the current function,
-// split up the call's results.  If the function has named result
-// variables, and the return statement lists explicit values, then
-// implement it by assigning the values to the result variables and
-// changing the statement to not list any values.  This lets
-// panic/recover work correctly.
+// split up the call's results.  If the return statement lists
+// explicit values, implement this statement by assigning the values
+// to the result variables and change this statement to a naked
+// return.  This lets panic/recover work correctly.
 
 Statement*
 Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
@@ -2556,7 +2625,7 @@ Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
   this->vals_ = NULL;
   this->is_lowered_ = true;
 
-  source_location loc = this->location();
+  Location loc = this->location();
 
   size_t vals_count = vals == NULL ? 0 : vals->size();
   Function::Results* results = function->func_value()->result_variables();
@@ -2628,7 +2697,12 @@ Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
       e->determine_type(&type_context);
 
       std::string reason;
-      if (Type::are_assignable(rvtype, e->type(), &reason))
+      bool ok;
+      if (this->are_hidden_fields_ok_)
+       ok = Type::are_assignable_hidden_ok(rvtype, e->type(), &reason);
+      else
+       ok = Type::are_assignable(rvtype, e->type(), &reason);
+      if (ok)
        {
          Expression* ve = Expression::make_var_reference(rv, e->location());
          lhs->push_back(ve);
@@ -2650,13 +2724,28 @@ Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
     ;
   else if (lhs->size() == 1)
     {
-      b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
-                                                 loc));
+      Statement* s = Statement::make_assignment(lhs->front(), rhs->front(),
+                                               loc);
+      if (this->are_hidden_fields_ok_)
+       {
+         Assignment_statement* as = static_cast<Assignment_statement*>(s);
+         as->set_hidden_fields_are_ok();
+       }
+      b->add_statement(s);
       delete lhs;
       delete rhs;
     }
   else
-    b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
+    {
+      Statement* s = Statement::make_tuple_assignment(lhs, rhs, loc);
+      if (this->are_hidden_fields_ok_)
+       {
+         Tuple_assignment_statement* tas =
+           static_cast<Tuple_assignment_statement*>(s);
+         tas->set_hidden_fields_are_ok();
+       }
+      b->add_statement(s);
+    }
 
   b->add_statement(this);
 
@@ -2670,7 +2759,7 @@ Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
 Bstatement*
 Return_statement::do_get_backend(Translate_context* context)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Function* function = context->function()->func_value();
   tree fndecl = function->get_decl();
@@ -2706,9 +2795,9 @@ Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 
 // Make a return statement.
 
-Statement*
+Return_statement*
 Statement::make_return_statement(Expression_list* vals,
-                                source_location location)
+                                Location location)
 {
   return new Return_statement(vals, location);
 }
@@ -2718,7 +2807,7 @@ Statement::make_return_statement(Expression_list* vals,
 class Bc_statement : public Statement
 {
  public:
-  Bc_statement(bool is_break, Unnamed_label* label, source_location location)
+  Bc_statement(bool is_break, Unnamed_label* label, Location location)
     : Statement(STATEMENT_BREAK_OR_CONTINUE, location),
       label_(label), is_break_(is_break)
   { }
@@ -2757,18 +2846,18 @@ Bc_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 {
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << (this->is_break_ ? "break" : "continue");
-  if (this->label_ != NULL) 
+  if (this->label_ != NULL)
     {
-      ast_dump_context->ostream() << " "; 
+      ast_dump_context->ostream() << " ";
       ast_dump_context->dump_label_name(this->label_);
     }
-  ast_dump_context->ostream() << std::endl; 
+  ast_dump_context->ostream() << std::endl;
 }
 
 // Make a break statement.
 
 Statement*
-Statement::make_break_statement(Unnamed_label* label, source_location location)
+Statement::make_break_statement(Unnamed_label* label, Location location)
 {
   return new Bc_statement(true, label, location);
 }
@@ -2777,7 +2866,7 @@ Statement::make_break_statement(Unnamed_label* label, source_location location)
 
 Statement*
 Statement::make_continue_statement(Unnamed_label* label,
-                                  source_location location)
+                                  Location location)
 {
   return new Bc_statement(false, label, location);
 }
@@ -2787,7 +2876,7 @@ Statement::make_continue_statement(Unnamed_label* label,
 class Goto_statement : public Statement
 {
  public:
-  Goto_statement(Label* label, source_location location)
+  Goto_statement(Label* label, Location location)
     : Statement(STATEMENT_GOTO, location),
       label_(label)
   { }
@@ -2849,7 +2938,7 @@ Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 // Make a goto statement.
 
 Statement*
-Statement::make_goto_statement(Label* label, source_location location)
+Statement::make_goto_statement(Label* label, Location location)
 {
   return new Goto_statement(label, location);
 }
@@ -2859,7 +2948,7 @@ Statement::make_goto_statement(Label* label, source_location location)
 class Goto_unnamed_statement : public Statement
 {
  public:
-  Goto_unnamed_statement(Unnamed_label* label, source_location location)
+  Goto_unnamed_statement(Unnamed_label* label, Location location)
     : Statement(STATEMENT_GOTO_UNNAMED, location),
       label_(label)
   { }
@@ -2900,7 +2989,7 @@ Goto_unnamed_statement::do_dump_statement(
 
 Statement*
 Statement::make_goto_unnamed_statement(Unnamed_label* label,
-                                      source_location location)
+                                      Location location)
 {
   return new Goto_unnamed_statement(label, location);
 }
@@ -2937,7 +3026,7 @@ Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 // Make a label statement.
 
 Statement*
-Statement::make_label_statement(Label* label, source_location location)
+Statement::make_label_statement(Label* label, Location location)
 {
   return new Label_statement(label, location);
 }
@@ -2994,7 +3083,7 @@ class If_statement : public Statement
 {
  public:
   If_statement(Expression* cond, Block* then_block, Block* else_block,
-              source_location location)
+              Location location)
     : Statement(STATEMENT_IF, location),
       cond_(cond), then_block_(then_block), else_block_(else_block)
   { }
@@ -3098,12 +3187,15 @@ If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
   ast_dump_context->ostream() << "if ";
   ast_dump_context->dump_expression(this->cond_);
   ast_dump_context->ostream() << std::endl;
-  ast_dump_context->dump_block(this->then_block_);
-  if (this->else_block_ != NULL) 
+  if (ast_dump_context->dump_subblocks())
     {
-      ast_dump_context->print_indent();
-      ast_dump_context->ostream() << "else" << std::endl;
-      ast_dump_context->dump_block(this->else_block_);
+      ast_dump_context->dump_block(this->then_block_);
+      if (this->else_block_ != NULL)
+       {
+         ast_dump_context->print_indent();
+         ast_dump_context->ostream() << "else" << std::endl;
+         ast_dump_context->dump_block(this->else_block_);
+       }
     }
 }
 
@@ -3111,7 +3203,7 @@ If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 
 Statement*
 Statement::make_if_statement(Expression* cond, Block* then_block,
-                            Block* else_block, source_location location)
+                            Block* else_block, Location location)
 {
   return new If_statement(cond, then_block, else_block, location);
 }
@@ -3128,10 +3220,9 @@ class Case_clauses::Hash_integer_value
 size_t
 Case_clauses::Hash_integer_value::operator()(Expression* pe) const
 {
-  Type* itype;
+  Numeric_constant nc;
   mpz_t ival;
-  mpz_init(ival);
-  if (!pe->integer_constant_value(true, ival, &itype))
+  if (!pe->numeric_constant_value(&nc) || !nc.to_int(&ival))
     go_unreachable();
   size_t ret = mpz_get_ui(ival);
   mpz_clear(ival);
@@ -3150,14 +3241,14 @@ class Case_clauses::Eq_integer_value
 bool
 Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
 {
-  Type* atype;
-  Type* btype;
+  Numeric_constant anc;
   mpz_t aval;
+  Numeric_constant bnc;
   mpz_t bval;
-  mpz_init(aval);
-  mpz_init(bval);
-  if (!a->integer_constant_value(true, aval, &atype)
-      || !b->integer_constant_value(true, bval, &btype))
+  if (!a->numeric_constant_value(&anc)
+      || !anc.to_int(&aval)
+      || !b->numeric_constant_value(&bnc)
+      || !bnc.to_int(&bval))
     go_unreachable();
   bool ret = mpz_cmp(aval, bval) == 0;
   mpz_clear(aval);
@@ -3213,7 +3304,7 @@ Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
                                 Unnamed_label* start_label,
                                 Unnamed_label* finish_label) const
 {
-  source_location loc = this->location_;
+  Location loc = this->location_;
   Unnamed_label* next_case_label;
   if (this->cases_ == NULL || this->cases_->empty())
     {
@@ -3228,16 +3319,10 @@ Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
           p != this->cases_->end();
           ++p)
        {
-         Expression* this_cond;
-         if (val_temp == NULL)
-           this_cond = *p;
-         else
-           {
-             Expression* ref = Expression::make_temporary_reference(val_temp,
-                                                                    loc);
-             this_cond = Expression::make_binary(OPERATOR_EQEQ, ref, *p, loc);
-           }
-
+         Expression* ref = Expression::make_temporary_reference(val_temp,
+                                                                loc);
+         Expression* this_cond = Expression::make_binary(OPERATOR_EQEQ, ref,
+                                                         *p, loc);
          if (cond == NULL)
            cond = this_cond;
          else
@@ -3245,7 +3330,7 @@ Case_clauses::Case_clause::lower(Block* b, Temporary_statement* val_temp,
        }
 
       Block* then_block = new Block(b, loc);
-      next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
+      next_case_label = new Unnamed_label(Linemap::unknown_location());
       Statement* s = Statement::make_goto_unnamed_statement(next_case_label,
                                                            loc);
       then_block->add_statement(s);
@@ -3345,18 +3430,17 @@ Case_clauses::Case_clause::get_backend(Translate_context* context,
          Expression* e = *p;
          if (e->classification() != Expression::EXPRESSION_INTEGER)
            {
-             Type* itype;
+             Numeric_constant nc;
              mpz_t ival;
-             mpz_init(ival);
-             if (!(*p)->integer_constant_value(true, ival, &itype))
+             if (!(*p)->numeric_constant_value(&nc) || !nc.to_int(&ival))
                {
                  // Something went wrong.  This can happen with a
                  // negative constant and an unsigned switch value.
                  go_assert(saw_errors());
                  continue;
                }
-             go_assert(itype != NULL);
-             e = Expression::make_integer(&ival, itype, e->location());
+             go_assert(nc.type() != NULL);
+             e = Expression::make_integer(&ival, nc.type(), e->location());
              mpz_clear(ival);
            }
 
@@ -3366,7 +3450,7 @@ Case_clauses::Case_clause::get_backend(Translate_context* context,
            {
              // Value was already present.
              error_at(this->location_, "duplicate case in switch");
-             continue;
+             e = Expression::make_error(this->location_);
            }
 
          tree case_tree = e->get_tree(context);
@@ -3401,7 +3485,7 @@ Case_clauses::Case_clause::get_backend(Translate_context* context,
 // Dump the AST representation for a case clause
 
 void
-Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context) 
+Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context)
     const
 {
   ast_dump_context->print_indent();
@@ -3508,7 +3592,6 @@ Case_clauses::lower(Block* b, Temporary_statement* val_temp,
   if (default_case != NULL)
     default_case->lower(b, val_temp, default_start_label,
                        default_finish_label);
-      
 }
 
 // Determine types.
@@ -3592,7 +3675,7 @@ Case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
 {
   for (Clauses::const_iterator p = this->clauses_.begin();
        p != this->clauses_.end();
-       ++p)    
+       ++p)
     p->dump_clause(ast_dump_context);
 }
 
@@ -3604,7 +3687,7 @@ class Constant_switch_statement : public Statement
  public:
   Constant_switch_statement(Expression* val, Case_clauses* clauses,
                            Unnamed_label* break_label,
-                           source_location location)
+                           Location location)
     : Statement(STATEMENT_CONSTANT_SWITCH, location),
       val_(val), clauses_(clauses), break_label_(break_label)
   { }
@@ -3716,9 +3799,15 @@ Constant_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "switch ";
   ast_dump_context->dump_expression(this->val_);
-  ast_dump_context->ostream() << " {" << std::endl;
-  this->clauses_->dump_clauses(ast_dump_context);
-  ast_dump_context->ostream() << "}" << std::endl;
+
+  if (ast_dump_context->dump_subblocks())
+    {
+      ast_dump_context->ostream() << " {" << std::endl;
+      this->clauses_->dump_clauses(ast_dump_context);
+      ast_dump_context->ostream() << "}";
+    }
+
+   ast_dump_context->ostream() << std::endl;
 }
 
 // Class Switch_statement.
@@ -3743,7 +3832,7 @@ Statement*
 Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
                           Statement_inserter*)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   if (this->val_ != NULL
       && (this->val_->is_error_expression()
@@ -3757,6 +3846,16 @@ Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
     return new Constant_switch_statement(this->val_, this->clauses_,
                                         this->break_label_, loc);
 
+  if (this->val_ != NULL
+      && !this->val_->type()->is_comparable()
+      && !Type::are_compatible_for_comparison(true, this->val_->type(),
+                                             Type::make_nil_type(), NULL))
+    {
+      error_at(this->val_->location(),
+              "cannot switch on value whose type that may not be compared");
+      return Statement::make_error_statement(loc);
+    }
+
   Block* b = new Block(enclosing, loc);
 
   if (this->clauses_->empty())
@@ -3764,18 +3863,15 @@ Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
       Expression* val = this->val_;
       if (val == NULL)
        val = Expression::make_boolean(true, loc);
-      return Statement::make_statement(val);
+      return Statement::make_statement(val, true);
     }
 
-  Temporary_statement* val_temp;
-  if (this->val_ == NULL)
-    val_temp = NULL;
-  else
-    {
-      // var val_temp VAL_TYPE = VAL
-      val_temp = Statement::make_temporary(NULL, this->val_, loc);
-      b->add_statement(val_temp);
-    }
+  // var val_temp VAL_TYPE = VAL
+  Expression* val = this->val_;
+  if (val == NULL)
+    val = Expression::make_boolean(true, loc);
+  Temporary_statement* val_temp = Statement::make_temporary(NULL, val, loc);
+  b->add_statement(val_temp);
 
   this->clauses_->lower(b, val_temp, this->break_label());
 
@@ -3806,18 +3902,21 @@ Switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
   if (this->val_ != NULL)
     {
       ast_dump_context->dump_expression(this->val_);
-      ast_dump_context->ostream() << " ";
     }
-  ast_dump_context->ostream() << "{" << std::endl;
-  this->clauses_->dump_clauses(ast_dump_context);
-  ast_dump_context->print_indent();
-  ast_dump_context->ostream() << "}" << std::endl;
+  if (ast_dump_context->dump_subblocks())
+    {
+      ast_dump_context->ostream() << " {" << std::endl;
+      this->clauses_->dump_clauses(ast_dump_context);
+      ast_dump_context->print_indent();
+      ast_dump_context->ostream() << "}";
+    }
+  ast_dump_context->ostream() << std::endl;
 }
 
 // Make a switch statement.
 
 Switch_statement*
-Statement::make_switch_statement(Expression* val, source_location location)
+Statement::make_switch_statement(Expression* val, Location location)
 {
   return new Switch_statement(val, location);
 }
@@ -3846,18 +3945,33 @@ Type_case_clauses::Type_case_clause::traverse(Traverse* traverse)
 // statements.
 
 void
-Type_case_clauses::Type_case_clause::lower(Block* b,
+Type_case_clauses::Type_case_clause::lower(Type* switch_val_type,
+                                          Block* b,
                                           Temporary_statement* descriptor_temp,
                                           Unnamed_label* break_label,
                                           Unnamed_label** stmts_label) const
 {
-  source_location loc = this->location_;
+  Location loc = this->location_;
 
   Unnamed_label* next_case_label = NULL;
   if (!this->is_default_)
     {
       Type* type = this->type_;
 
+      std::string reason;
+      if (switch_val_type->interface_type() != NULL
+         && !type->is_nil_constant_as_type()
+         && type->interface_type() == NULL
+         && !switch_val_type->interface_type()->implements_interface(type,
+                                                                     &reason))
+       {
+         if (reason.empty())
+           error_at(this->location_, "impossible type switch case");
+         else
+           error_at(this->location_, "impossible type switch case (%s)",
+                    reason.c_str());
+       }
+
       Expression* ref = Expression::make_temporary_reference(descriptor_temp,
                                                             loc);
 
@@ -3881,7 +3995,7 @@ Type_case_clauses::Type_case_clause::lower(Block* b,
       if (!this->is_fallthrough_)
        {
          // if !COND { goto NEXT_CASE_LABEL }
-         next_case_label = new Unnamed_label(UNKNOWN_LOCATION);
+         next_case_label = new Unnamed_label(Linemap::unknown_location());
          dest = next_case_label;
          cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
        }
@@ -3890,7 +4004,7 @@ Type_case_clauses::Type_case_clause::lower(Block* b,
          // if COND { goto STMTS_LABEL }
          go_assert(stmts_label != NULL);
          if (*stmts_label == NULL)
-           *stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
+           *stmts_label = new Unnamed_label(Linemap::unknown_location());
          dest = *stmts_label;
        }
       Block* then_block = new Block(b, loc);
@@ -3924,7 +4038,7 @@ Type_case_clauses::Type_case_clause::lower(Block* b,
     go_assert(next_case_label == NULL);
   else
     {
-      source_location gloc = (this->statements_ == NULL
+      Location gloc = (this->statements_ == NULL
                              ? loc
                              : this->statements_->end_location());
       b->add_statement(Statement::make_goto_unnamed_statement(break_label,
@@ -3951,7 +4065,7 @@ Type_case_clauses::Type_case_clause::dump_clause(
     }
   else
     {
-      ast_dump_context->ostream() << "case "; 
+      ast_dump_context->ostream() << "case ";
       ast_dump_context->dump_type(this->type_);
       ast_dump_context->ostream() << ":" ;
     }
@@ -4008,7 +4122,8 @@ Type_case_clauses::check_duplicates() const
 // BREAK_LABEL is the label at the end of the type switch.
 
 void
-Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
+Type_case_clauses::lower(Type* switch_val_type, Block* b,
+                        Temporary_statement* descriptor_temp,
                         Unnamed_label* break_label) const
 {
   const Type_case_clause* default_case = NULL;
@@ -4019,7 +4134,8 @@ Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
        ++p)
     {
       if (!p->is_default())
-       p->lower(b, descriptor_temp, break_label, &stmts_label);
+       p->lower(switch_val_type, b, descriptor_temp, break_label,
+                &stmts_label);
       else
        {
          // We are generating a series of tests, which means that we
@@ -4030,7 +4146,8 @@ Type_case_clauses::lower(Block* b, Temporary_statement* descriptor_temp,
   go_assert(stmts_label == NULL);
 
   if (default_case != NULL)
-    default_case->lower(b, descriptor_temp, break_label, NULL);
+    default_case->lower(switch_val_type, b, descriptor_temp, break_label,
+                       NULL);
 }
 
 // Dump the AST representation for case clauses (from a switch statement)
@@ -4040,7 +4157,7 @@ Type_case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
 {
   for (Type_clauses::const_iterator p = this->clauses_.begin();
        p != this->clauses_.end();
-       ++p)    
+       ++p)
     p->dump_clause(ast_dump_context);
 }
 
@@ -4071,7 +4188,7 @@ Statement*
 Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
                                Statement_inserter*)
 {
-  const source_location loc = this->location();
+  const Location loc = this->location();
 
   if (this->clauses_ != NULL)
     this->clauses_->check_duplicates();
@@ -4082,55 +4199,41 @@ Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
                    ? this->var_->var_value()->type()
                    : this->expr_->type());
 
+  if (val_type->interface_type() == NULL)
+    {
+      if (!val_type->is_error())
+       this->report_error(_("cannot type switch on non-interface value"));
+      return Statement::make_error_statement(loc);
+    }
+
   // var descriptor_temp DESCRIPTOR_TYPE
   Type* descriptor_type = Type::make_type_descriptor_ptr_type();
   Temporary_statement* descriptor_temp =
     Statement::make_temporary(descriptor_type, NULL, loc);
   b->add_statement(descriptor_temp);
 
-  if (val_type->interface_type() == NULL)
-    {
-      // Doing a type switch on a non-interface type.  Should we issue
-      // a warning for this case?
-      Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
-                                                            loc);
-      Expression* rhs;
-      if (val_type->is_nil_type())
-       rhs = Expression::make_nil(loc);
-      else
-       {
-         if (val_type->is_abstract())
-           val_type = val_type->make_non_abstract_type();
-         rhs = Expression::make_type_descriptor(val_type, loc);
-       }
-      Statement* s = Statement::make_assignment(lhs, rhs, loc);
-      b->add_statement(s);
-    }
+  // descriptor_temp = ifacetype(val_temp) FIXME: This should be
+  // inlined.
+  bool is_empty = val_type->interface_type()->is_empty();
+  Expression* ref;
+  if (this->var_ == NULL)
+    ref = this->expr_;
   else
-    {
-      // descriptor_temp = ifacetype(val_temp)
-      // FIXME: This should be inlined.
-      bool is_empty = val_type->interface_type()->is_empty();
-      Expression* ref;
-      if (this->var_ == NULL)
-       ref = this->expr_;
-      else
-       ref = Expression::make_var_reference(this->var_, loc);
-      Expression* call = Runtime::make_call((is_empty
-                                            ? Runtime::EFACETYPE
-                                            : Runtime::IFACETYPE),
-                                           loc, 1, ref);
-      Temporary_reference_expression* lhs =
-       Expression::make_temporary_reference(descriptor_temp, loc);
-      lhs->set_is_lvalue();
-      Statement* s = Statement::make_assignment(lhs, call, loc);
-      b->add_statement(s);
-    }
+    ref = Expression::make_var_reference(this->var_, loc);
+  Expression* call = Runtime::make_call((is_empty
+                                        ? Runtime::EFACETYPE
+                                        : Runtime::IFACETYPE),
+                                       loc, 1, ref);
+  Temporary_reference_expression* lhs =
+    Expression::make_temporary_reference(descriptor_temp, loc);
+  lhs->set_is_lvalue();
+  Statement* s = Statement::make_assignment(lhs, call, loc);
+  b->add_statement(s);
 
   if (this->clauses_ != NULL)
-    this->clauses_->lower(b, descriptor_temp, this->break_label());
+    this->clauses_->lower(val_type, b, descriptor_temp, this->break_label());
 
-  Statement* s = Statement::make_unnamed_label_statement(this->break_label_);
+  s = Statement::make_unnamed_label_statement(this->break_label_);
   b->add_statement(s);
 
   return Statement::make_block_statement(b, loc);
@@ -4150,22 +4253,27 @@ Type_switch_statement::break_label()
 // Dump the AST representation for a type switch statement
 
 void
-Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) 
+Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
     const
 {
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "switch " << this->var_->name() << " = ";
   ast_dump_context->dump_expression(this->expr_);
-  ast_dump_context->ostream() << " .(type) {" << std::endl;
-  this->clauses_->dump_clauses(ast_dump_context);
-  ast_dump_context->ostream() << "}" << std::endl;
+  ast_dump_context->ostream() << " .(type)";
+  if (ast_dump_context->dump_subblocks())
+    {
+      ast_dump_context->ostream() << " {" << std::endl;
+      this->clauses_->dump_clauses(ast_dump_context);
+      ast_dump_context->ostream() << "}";
+    }
+  ast_dump_context->ostream() << std::endl;
 }
 
 // Make a type switch statement.
 
 Type_switch_statement*
 Statement::make_type_switch_statement(Named_object* var, Expression* expr,
-                                     source_location location)
+                                     Location location)
 {
   return new Type_switch_statement(var, expr, location);
 }
@@ -4231,7 +4339,7 @@ Send_statement::do_check_types(Gogo*)
 Bstatement*
 Send_statement::do_get_backend(Translate_context* context)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Channel_type* channel_type = this->channel_->type()->channel_type();
   Type* element_type = channel_type->element_type();
@@ -4266,7 +4374,7 @@ Send_statement::do_get_backend(Translate_context* context)
 
     case Type::TYPE_ARRAY:
       is_small = false;
-      can_take_address = !element_type->is_open_array_type();
+      can_take_address = !element_type->is_slice_type();
       break;
 
     default:
@@ -4288,9 +4396,11 @@ Send_statement::do_get_backend(Translate_context* context)
       && val->temporary_reference_expression() == NULL)
     can_take_address = false;
 
+  Expression* td = Expression::make_type_descriptor(this->channel_->type(),
+                                                   loc);
+
   Runtime::Function code;
   Bstatement* btemp = NULL;
-  Expression* call;
   if (is_small)
       {
        // Type is small enough to handle as uint64.
@@ -4318,8 +4428,7 @@ Send_statement::do_get_backend(Translate_context* context)
       btemp = temp->get_backend(context);
     }
 
-  call = Runtime::make_call(code, loc, 3, this->channel_, val,
-                           Expression::make_boolean(this->for_select_, loc));
+  Expression* call = Runtime::make_call(code, loc, 3, td, this->channel_, val);
 
   context->gogo()->lower_expression(context->function(), NULL, &call);
   Bexpression* bcall = tree_to_expr(call->get_tree(context));
@@ -4347,7 +4456,7 @@ Send_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 
 Send_statement*
 Statement::make_send_statement(Expression* channel, Expression* val,
-                              source_location location)
+                              Location location)
 {
   return new Send_statement(channel, val, location);
 }
@@ -4387,118 +4496,178 @@ Select_clauses::Select_clause::traverse(Traverse* traverse)
   return TRAVERSE_CONTINUE;
 }
 
-// Lowering.  Here we pull out the channel and the send values, to
-// enforce the order of evaluation.  We also add explicit send and
-// receive statements to the clauses.
+// Lowering.  We call a function to register this clause, and arrange
+// to set any variables in any receive clause.
 
 void
 Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
-                                    Block* b)
+                                    Block* b, Temporary_statement* sel)
 {
+  Location loc = this->location_;
+
+  Expression* selref = Expression::make_temporary_reference(sel, loc);
+
+  mpz_t ival;
+  mpz_init_set_ui(ival, this->index_);
+  Expression* index_expr = Expression::make_integer(&ival, NULL, loc);
+  mpz_clear(ival);
+
   if (this->is_default_)
     {
       go_assert(this->channel_ == NULL && this->val_ == NULL);
+      this->lower_default(b, selref, index_expr);
       this->is_lowered_ = true;
       return;
     }
 
-  source_location loc = this->location_;
-
   // Evaluate the channel before the select statement.
   Temporary_statement* channel_temp = Statement::make_temporary(NULL,
                                                                this->channel_,
                                                                loc);
   b->add_statement(channel_temp);
-  this->channel_ = Expression::make_temporary_reference(channel_temp, loc);
+  Expression* chanref = Expression::make_temporary_reference(channel_temp,
+                                                            loc);
 
-  // If this is a send clause, evaluate the value to send before the
-  // select statement.
-  Temporary_statement* val_temp = NULL;
-  if (this->is_send_ && !this->val_->is_constant())
+  if (this->is_send_)
+    this->lower_send(b, selref, chanref, index_expr);
+  else
+    this->lower_recv(gogo, function, b, selref, chanref, index_expr);
+
+  // Now all references should be handled through the statements, not
+  // through here.
+  this->is_lowered_ = true;
+  this->val_ = NULL;
+  this->var_ = NULL;
+}
+
+// Lower a default clause in a select statement.
+
+void
+Select_clauses::Select_clause::lower_default(Block* b, Expression* selref,
+                                            Expression* index_expr)
+{
+  Location loc = this->location_;
+  Expression* call = Runtime::make_call(Runtime::SELECTDEFAULT, loc, 2, selref,
+                                       index_expr);
+  b->add_statement(Statement::make_statement(call, true));
+}
+
+// Lower a send clause in a select statement.
+
+void
+Select_clauses::Select_clause::lower_send(Block* b, Expression* selref,
+                                         Expression* chanref,
+                                         Expression* index_expr)
+{
+  Location loc = this->location_;
+
+  Channel_type* ct = this->channel_->type()->channel_type();
+  if (ct == NULL)
+    return;
+
+  Type* valtype = ct->element_type();
+
+  // Note that copying the value to a temporary here means that we
+  // evaluate the send values in the required order.
+  Temporary_statement* val = Statement::make_temporary(valtype, this->val_,
+                                                      loc);
+  b->add_statement(val);
+
+  Expression* valref = Expression::make_temporary_reference(val, loc);
+  Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
+
+  Expression* call = Runtime::make_call(Runtime::SELECTSEND, loc, 4, selref,
+                                       chanref, valaddr, index_expr);
+  b->add_statement(Statement::make_statement(call, true));
+}
+
+// Lower a receive clause in a select statement.
+
+void
+Select_clauses::Select_clause::lower_recv(Gogo* gogo, Named_object* function,
+                                         Block* b, Expression* selref,
+                                         Expression* chanref,
+                                         Expression* index_expr)
+{
+  Location loc = this->location_;
+
+  Channel_type* ct = this->channel_->type()->channel_type();
+  if (ct == NULL)
+    return;
+
+  Type* valtype = ct->element_type();
+  Temporary_statement* val = Statement::make_temporary(valtype, NULL, loc);
+  b->add_statement(val);
+
+  Expression* valref = Expression::make_temporary_reference(val, loc);
+  Expression* valaddr = Expression::make_unary(OPERATOR_AND, valref, loc);
+
+  Temporary_statement* closed_temp = NULL;
+
+  Expression* call;
+  if (this->closed_ == NULL && this->closedvar_ == NULL)
+    call = Runtime::make_call(Runtime::SELECTRECV, loc, 4, selref, chanref,
+                             valaddr, index_expr);
+  else
     {
-      val_temp = Statement::make_temporary(NULL, this->val_, loc);
-      b->add_statement(val_temp);
+      closed_temp = Statement::make_temporary(Type::lookup_bool_type(), NULL,
+                                             loc);
+      b->add_statement(closed_temp);
+      Expression* cref = Expression::make_temporary_reference(closed_temp,
+                                                             loc);
+      Expression* caddr = Expression::make_unary(OPERATOR_AND, cref, loc);
+      call = Runtime::make_call(Runtime::SELECTRECV2, loc, 5, selref, chanref,
+                               valaddr, caddr, index_expr);
     }
 
-  // Add the send or receive before the rest of the statements if any.
-  Block *init = new Block(b, loc);
-  Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
-  if (this->is_send_)
+  b->add_statement(Statement::make_statement(call, true));
+
+  // If the block of statements is executed, arrange for the received
+  // value to move from VAL to the place where the statements expect
+  // it.
+
+  Block* init = NULL;
+
+  if (this->var_ != NULL)
     {
-      Expression* ref2;
-      if (val_temp == NULL)
-       ref2 = this->val_;
-      else
-       ref2 = Expression::make_temporary_reference(val_temp, loc);
-      Send_statement* send = Statement::make_send_statement(ref, ref2, loc);
-      send->set_for_select();
-      init->add_statement(send);
+      go_assert(this->val_ == NULL);
+      valref = Expression::make_temporary_reference(val, loc);
+      this->var_->var_value()->set_init(valref);
+      this->var_->var_value()->clear_type_from_chan_element();
     }
-  else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
+  else if (this->val_ != NULL && !this->val_->is_sink_expression())
     {
-      go_assert(this->var_ == NULL && this->closedvar_ == NULL);
-      if (this->val_ == NULL)
-       this->val_ = Expression::make_sink(loc);
-      Statement* s = Statement::make_tuple_receive_assignment(this->val_,
-                                                             this->closed_,
-                                                             ref, true, loc);
-      init->add_statement(s);
+      init = new Block(b, loc);
+      valref = Expression::make_temporary_reference(val, loc);
+      init->add_statement(Statement::make_assignment(this->val_, valref, loc));
     }
-  else if (this->closedvar_ != NULL)
+
+  if (this->closedvar_ != NULL)
     {
-      go_assert(this->val_ == NULL);
-      Expression* val;
-      if (this->var_ == NULL)
-       val = Expression::make_sink(loc);
-      else
-       val = Expression::make_var_reference(this->var_, loc);
-      Expression* closed = Expression::make_var_reference(this->closedvar_,
-                                                         loc);
-      Statement* s = Statement::make_tuple_receive_assignment(val, closed, ref,
-                                                             true, loc);
-      // We have to put S in STATEMENTS_, because that is where the
-      // variables are declared.
-      go_assert(this->statements_ != NULL);
-      this->statements_->add_statement_at_front(s);
-      // We have to lower STATEMENTS_ again, to lower the tuple
-      // receive assignment we just added.
-      gogo->lower_block(function, this->statements_);
+      go_assert(this->closed_ == NULL);
+      Expression* cref = Expression::make_temporary_reference(closed_temp,
+                                                             loc);
+      this->closedvar_->var_value()->set_init(cref);
     }
-  else
+  else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
     {
-      Receive_expression* recv = Expression::make_receive(ref, loc);
-      recv->set_for_select();
-      if (this->val_ != NULL)
-       {
-         go_assert(this->var_ == NULL);
-         init->add_statement(Statement::make_assignment(this->val_, recv,
-                                                        loc));
-       }
-      else if (this->var_ != NULL)
-       {
-         this->var_->var_value()->set_init(recv);
-         this->var_->var_value()->clear_type_from_chan_element();
-       }
-      else
-       {
-         init->add_statement(Statement::make_statement(recv));
-       }
+      if (init == NULL)
+       init = new Block(b, loc);
+      Expression* cref = Expression::make_temporary_reference(closed_temp,
+                                                             loc);
+      init->add_statement(Statement::make_assignment(this->closed_, cref,
+                                                    loc));
     }
 
-  // Lower any statements we just created.
-  gogo->lower_block(function, init);
-
-  if (this->statements_ != NULL)
-    init->add_statement(Statement::make_block_statement(this->statements_,
-                                                       loc));
-
-  this->statements_ = init;
+  if (init != NULL)
+    {
+      gogo->lower_block(function, init);
 
-  // Now all references should be handled through the statements, not
-  // through here.
-  this->is_lowered_ = true;
-  this->val_ = NULL;
-  this->var_ = NULL;
+      if (this->statements_ != NULL)
+       init->add_statement(Statement::make_block_statement(this->statements_,
+                                                           loc));
+      this->statements_ = init;
+    }
 }
 
 // Determine types.
@@ -4511,6 +4680,27 @@ Select_clauses::Select_clause::determine_types()
     this->statements_->determine_types();
 }
 
+// Check types.
+
+void
+Select_clauses::Select_clause::check_types()
+{
+  if (this->is_default_)
+    return;
+
+  Channel_type* ct = this->channel_->type()->channel_type();
+  if (ct == NULL)
+    {
+      error_at(this->channel_->location(), "expected channel");
+      return;
+    }
+
+  if (this->is_send_ && !ct->may_send())
+    error_at(this->location(), "invalid send on receive-only channel");
+  else if (!this->is_send_ && !ct->may_receive())
+    error_at(this->location(), "invalid receive on send-only channel");
+}
+
 // Whether this clause may fall through to the statement which follows
 // the overall select statement.
 
@@ -4552,9 +4742,10 @@ Select_clauses::Select_clause::dump_clause(
         {
           ast_dump_context->dump_expression(this->channel_);
           ast_dump_context->ostream() << " <- " ;
-          ast_dump_context->dump_expression(this->val_);
+         if (this->val_ != NULL)
+           ast_dump_context->dump_expression(this->val_);
         }
-      else 
+      else
         {
          if (this->val_ != NULL)
            ast_dump_context->dump_expression(this->val_);
@@ -4564,10 +4755,9 @@ Select_clauses::Select_clause::dump_clause(
               ast_dump_context->ostream() << " , " ;
               ast_dump_context->dump_expression(this->closed_);
             }
-          if (this->closedvar_ != NULL ||
-              this->var_ != NULL)
+          if (this->closedvar_ != NULL || this->var_ != NULL)
             ast_dump_context->ostream() << " := " ;
-            
+
           ast_dump_context->ostream() << " <- " ;
           ast_dump_context->dump_expression(this->channel_);
         }
@@ -4598,12 +4788,13 @@ Select_clauses::traverse(Traverse* traverse)
 // receive statements to the clauses.
 
 void
-Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b)
+Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b,
+                     Temporary_statement* sel)
 {
   for (Clauses::iterator p = this->clauses_.begin();
        p != this->clauses_.end();
        ++p)
-    p->lower(gogo, function, b);
+    p->lower(gogo, function, b, sel);
 }
 
 // Determine types.
@@ -4617,6 +4808,17 @@ Select_clauses::determine_types()
     p->determine_types();
 }
 
+// Check types.
+
+void
+Select_clauses::check_types()
+{
+  for (Clauses::iterator p = this->clauses_.begin();
+       p != this->clauses_.end();
+       ++p)
+    p->check_types();
+}
+
 // Return whether these select clauses fall through to the statement
 // following the overall select statement.
 
@@ -4631,179 +4833,55 @@ Select_clauses::may_fall_through() const
   return false;
 }
 
-// Convert to the backend representation.  We build a call to
-//   size_t __go_select(size_t count, _Bool has_default,
-//                      channel* channels, _Bool* is_send)
-//
-// There are COUNT entries in the CHANNELS and IS_SEND arrays.  The
-// value in the IS_SEND array is true for send, false for receive.
-// __go_select returns an integer from 0 to COUNT, inclusive.  A
-// return of 0 means that the default case should be run; this only
-// happens if HAS_DEFAULT is non-zero.  Otherwise the number indicates
-// the case to run.
-
-// FIXME: This doesn't handle channels which send interface types
-// where the receiver has a static type which matches that interface.
+// Convert to the backend representation.  We have already accumulated
+// all the select information.  Now we call selectgo, which will
+// return the index of the clause to execute.
 
 Bstatement*
 Select_clauses::get_backend(Translate_context* context,
+                           Temporary_statement* sel,
                            Unnamed_label *break_label,
-                           source_location location)
+                           Location location)
 {
   size_t count = this->clauses_.size();
+  std::vector<std::vector<Bexpression*> > cases(count);
+  std::vector<Bstatement*> clauses(count);
 
-  Expression_list* chan_init = new Expression_list();
-  chan_init->reserve(count);
-
-  Expression_list* is_send_init = new Expression_list();
-  is_send_init->reserve(count);
-
-  Select_clause *default_clause = NULL;
-
-  Type* runtime_chanptr_type = Runtime::chanptr_type();
-  Type* runtime_chan_type = runtime_chanptr_type->points_to();
-
+  int i = 0;
   for (Clauses::iterator p = this->clauses_.begin();
        p != this->clauses_.end();
-       ++p)
+       ++p, ++i)
     {
-      if (p->is_default())
-       {
-         default_clause = &*p;
-         --count;
-         continue;
-       }
-
-      if (p->channel()->type()->channel_type() == NULL)
-       {
-         // We should have given an error in the send or receive
-         // statement we created via lowering.
-         go_assert(saw_errors());
-         return context->backend()->error_statement();
-       }
+      int index = p->index();
+      mpz_t ival;
+      mpz_init_set_ui(ival, index);
+      Expression* index_expr = Expression::make_integer(&ival, NULL, location);
+      mpz_clear(ival);
+      cases[i].push_back(tree_to_expr(index_expr->get_tree(context)));
 
-      Expression* c = p->channel();
-      c = Expression::make_unsafe_cast(runtime_chan_type, c, p->location());
-      chan_init->push_back(c);
-
-      is_send_init->push_back(Expression::make_boolean(p->is_send(),
-                                                      p->location()));
-    }
+      Bstatement* s = p->get_statements_backend(context);
+      Location gloc = (p->statements() == NULL
+                      ? p->location()
+                      : p->statements()->end_location());
+      Bstatement* g = break_label->get_goto(context, gloc);
 
-  if (chan_init->empty())
-    {
-      go_assert(count == 0);
-      Bstatement* s;
-      Bstatement* ldef = break_label->get_definition(context);
-      if (default_clause != NULL)
-       {
-         // There is a default clause and no cases.  Just execute the
-         // default clause.
-         s = default_clause->get_statements_backend(context);
-       }
-      else
-       {
-         // There isn't even a default clause.  In this case select
-         // pauses forever.  Call the runtime function with nils.
-         mpz_t zval;
-         mpz_init_set_ui(zval, 0);
-         Expression* zero = Expression::make_integer(&zval, NULL, location);
-         mpz_clear(zval);
-         Expression* default_arg = Expression::make_boolean(false, location);
-         Expression* nil1 = Expression::make_nil(location);
-         Expression* nil2 = nil1->copy();
-         Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
-                                               zero, default_arg, nil1, nil2);
-         context->gogo()->lower_expression(context->function(), NULL, &call);
-         Bexpression* bcall = tree_to_expr(call->get_tree(context));
-         s = context->backend()->expression_statement(bcall);
-       }
       if (s == NULL)
-       return ldef;
-      return context->backend()->compound_statement(s, ldef);
+       clauses[i] = g;
+      else
+       clauses[i] = context->backend()->compound_statement(s, g);
     }
-  go_assert(count > 0);
-
-  std::vector<Bstatement*> statements;
-
-  mpz_t ival;
-  mpz_init_set_ui(ival, count);
-  Expression* ecount = Expression::make_integer(&ival, NULL, location);
-  mpz_clear(ival);
 
-  Type* chan_array_type = Type::make_array_type(runtime_chan_type, ecount);
-  Expression* chans = Expression::make_composite_literal(chan_array_type, 0,
-                                                        false, chan_init,
-                                                        location);
-  context->gogo()->lower_expression(context->function(), NULL, &chans);
-  Temporary_statement* chan_temp = Statement::make_temporary(chan_array_type,
-                                                            chans,
-                                                            location);
-  statements.push_back(chan_temp->get_backend(context));
-
-  Type* is_send_array_type = Type::make_array_type(Type::lookup_bool_type(),
-                                                  ecount->copy());
-  Expression* is_sends = Expression::make_composite_literal(is_send_array_type,
-                                                           0, false,
-                                                           is_send_init,
-                                                           location);
-  context->gogo()->lower_expression(context->function(), NULL, &is_sends);
-  Temporary_statement* is_send_temp =
-    Statement::make_temporary(is_send_array_type, is_sends, location);
-  statements.push_back(is_send_temp->get_backend(context));
-
-  mpz_init_set_ui(ival, 0);
-  Expression* zero = Expression::make_integer(&ival, NULL, location);
-  mpz_clear(ival);
-
-  Expression* ref = Expression::make_temporary_reference(chan_temp, location);
-  Expression* chan_arg = Expression::make_array_index(ref, zero, NULL,
-                                                     location);
-  chan_arg = Expression::make_unary(OPERATOR_AND, chan_arg, location);
-  chan_arg = Expression::make_unsafe_cast(runtime_chanptr_type, chan_arg,
-                                         location);
-
-  ref = Expression::make_temporary_reference(is_send_temp, location);
-  Expression* is_send_arg = Expression::make_array_index(ref, zero->copy(),
-                                                        NULL, location);
-  is_send_arg = Expression::make_unary(OPERATOR_AND, is_send_arg, location);
-
-  Expression* default_arg = Expression::make_boolean(default_clause != NULL,
-                                                    location);
-  Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
-                                       ecount->copy(), default_arg,
-                                       chan_arg, is_send_arg);
+  Expression* selref = Expression::make_temporary_reference(sel, location);
+  Expression* call = Runtime::make_call(Runtime::SELECTGO, location, 1,
+                                       selref);
   context->gogo()->lower_expression(context->function(), NULL, &call);
   Bexpression* bcall = tree_to_expr(call->get_tree(context));
 
-  std::vector<std::vector<Bexpression*> > cases;
-  std::vector<Bstatement*> clauses;
-
-  cases.resize(count + (default_clause != NULL ? 1 : 0));
-  clauses.resize(count + (default_clause != NULL ? 1 : 0));
-
-  int index = 0;
+  if (count == 0)
+    return context->backend()->expression_statement(bcall);
 
-  if (default_clause != NULL)
-    {
-      this->add_clause_backend(context, location, index, 0, default_clause,
-                              break_label, &cases, &clauses);
-      ++index;
-    }
-
-  int i = 1;
-  for (Clauses::iterator p = this->clauses_.begin();
-       p != this->clauses_.end();
-       ++p)
-    {
-      if (!p->is_default())
-       {
-         this->add_clause_backend(context, location, index, i, &*p,
-                                  break_label, &cases, &clauses);
-         ++i;
-         ++index;
-       }
-    }
+  std::vector<Bstatement*> statements;
+  statements.reserve(2);
 
   Bstatement* switch_stmt = context->backend()->switch_statement(bcall,
                                                                 cases,
@@ -4816,39 +4894,6 @@ Select_clauses::get_backend(Translate_context* context,
 
   return context->backend()->statement_list(statements);
 }
-
-// Add CLAUSE to CASES/CLAUSES at INDEX.
-
-void
-Select_clauses::add_clause_backend(
-    Translate_context* context,
-    source_location location,
-    int index,
-    int case_value,
-    Select_clause* clause,
-    Unnamed_label* bottom_label,
-    std::vector<std::vector<Bexpression*> > *cases,
-    std::vector<Bstatement*>* clauses)
-{
-  mpz_t ival;
-  mpz_init_set_ui(ival, case_value);
-  Expression* e = Expression::make_integer(&ival, NULL, location);
-  mpz_clear(ival);
-  (*cases)[index].push_back(tree_to_expr(e->get_tree(context)));
-
-  Bstatement* s = clause->get_statements_backend(context);
-
-  source_location gloc = (clause->statements() == NULL
-                         ? clause->location()
-                         : clause->statements()->end_location());
-  Bstatement* g = bottom_label->get_goto(context, gloc);
-                               
-  if (s == NULL)
-    (*clauses)[index] = g;
-  else
-    (*clauses)[index] = context->backend()->compound_statement(s, g);
-}
-
 // Dump the AST representation for select clauses.
 
 void
@@ -4856,7 +4901,7 @@ Select_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
 {
   for (Clauses::const_iterator p = this->clauses_.begin();
        p != this->clauses_.end();
-       ++p)    
+       ++p)
     p->dump_clause(ast_dump_context);
 }
 
@@ -4884,11 +4929,28 @@ Select_statement::do_lower(Gogo* gogo, Named_object* function,
 {
   if (this->is_lowered_)
     return this;
-  Block* b = new Block(enclosing, this->location());
-  this->clauses_->lower(gogo, function, b);
+
+  Location loc = this->location();
+
+  Block* b = new Block(enclosing, loc);
+
+  go_assert(this->sel_ == NULL);
+
+  mpz_t ival;
+  mpz_init_set_ui(ival, this->clauses_->size());
+  Expression* size_expr = Expression::make_integer(&ival, NULL, loc);
+  mpz_clear(ival);
+
+  Expression* call = Runtime::make_call(Runtime::NEWSELECT, loc, 1, size_expr);
+
+  this->sel_ = Statement::make_temporary(NULL, call, loc);
+  b->add_statement(this->sel_);
+
+  this->clauses_->lower(gogo, function, b, this->sel_);
   this->is_lowered_ = true;
   b->add_statement(this);
-  return Statement::make_block_statement(b, this->location());
+
+  return Statement::make_block_statement(b, loc);
 }
 
 // Return the backend representation for a select statement.
@@ -4896,25 +4958,30 @@ Select_statement::do_lower(Gogo* gogo, Named_object* function,
 Bstatement*
 Select_statement::do_get_backend(Translate_context* context)
 {
-  return this->clauses_->get_backend(context, this->break_label(),
+  return this->clauses_->get_backend(context, this->sel_, this->break_label(),
                                     this->location());
 }
 
 // Dump the AST representation for a select statement.
 
-void 
+void
 Select_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 {
   ast_dump_context->print_indent();
-  ast_dump_context->ostream() << "select {" << std::endl;
-  this->clauses_->dump_clauses(ast_dump_context);
-  ast_dump_context->ostream() << "}" << std::endl;
+  ast_dump_context->ostream() << "select";
+  if (ast_dump_context->dump_subblocks())
+    {
+      ast_dump_context->ostream() << " {" << std::endl;
+      this->clauses_->dump_clauses(ast_dump_context);
+      ast_dump_context->ostream() << "}";
+    }
+  ast_dump_context->ostream() << std::endl;
 }
 
 // Make a select statement.
 
 Select_statement*
-Statement::make_select_statement(source_location location)
+Statement::make_select_statement(Location location)
 {
   return new Select_statement(location);
 }
@@ -4952,7 +5019,7 @@ For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
                        Statement_inserter*)
 {
   Statement* s;
-  source_location loc = this->location();
+  Location loc = this->location();
 
   Block* b = new Block(enclosing, this->location());
   if (this->init_ != NULL)
@@ -4976,7 +5043,7 @@ For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
                                      this->statements_->start_location());
   b->add_statement(s);
 
-  source_location end_loc = this->statements_->end_location();
+  Location end_loc = this->statements_->end_location();
 
   Unnamed_label* cont = this->continue_label_;
   if (cont != NULL)
@@ -4996,7 +5063,7 @@ For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
     {
       b->add_statement(Statement::make_unnamed_label_statement(entry));
 
-      source_location cond_loc = this->cond_->location();
+      Location cond_loc = this->cond_->location();
       Block* then_block = new Block(b, cond_loc);
       s = Statement::make_goto_unnamed_statement(top, cond_loc);
       then_block->add_statement(s);
@@ -5051,7 +5118,7 @@ For_statement::set_break_continue_labels(Unnamed_label* break_label,
 void
 For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 {
-  if (this->init_ != NULL)
+  if (this->init_ != NULL && ast_dump_context->dump_subblocks())
     {
       ast_dump_context->print_indent();
       ast_dump_context->indent();
@@ -5063,26 +5130,31 @@ For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
   ast_dump_context->ostream() << "for ";
   if (this->cond_ != NULL)
     ast_dump_context->dump_expression(this->cond_);
-  ast_dump_context->ostream() << " {" << std::endl;
-  ast_dump_context->indent();
 
-  ast_dump_context->dump_block(this->statements_);
-  if (this->init_ != NULL)
+  if (ast_dump_context->dump_subblocks())
     {
+      ast_dump_context->ostream() << " {" << std::endl;
+      ast_dump_context->dump_block(this->statements_);
+      if (this->init_ != NULL)
+       {
+         ast_dump_context->print_indent();
+         ast_dump_context->ostream() << "// POST " << std::endl;
+         ast_dump_context->dump_block(this->post_);
+       }
+      ast_dump_context->unindent();
+
       ast_dump_context->print_indent();
-      ast_dump_context->ostream() << "// POST " << std::endl;
-      ast_dump_context->dump_block(this->post_);
+      ast_dump_context->ostream() << "}";
     }
-  ast_dump_context->unindent();
-  ast_dump_context->print_indent();
-  ast_dump_context->ostream() << "}" << std::endl;
+
+  ast_dump_context->ostream() << std::endl;
 }
 
 // Make a for statement.
 
 For_statement*
 Statement::make_for_statement(Block* init, Expression* cond, Block* post,
-                             source_location location)
+                             Location location)
 {
   return new For_statement(init, cond, post, location);
 }
@@ -5118,7 +5190,7 @@ For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
   Type* range_type = this->range_->type();
   if (range_type->points_to() != NULL
       && range_type->points_to()->array_type() != NULL
-      && !range_type->points_to()->is_open_array_type())
+      && !range_type->points_to()->is_slice_type())
     range_type = range_type->points_to();
 
   Type* index_type;
@@ -5131,7 +5203,7 @@ For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
   else if (range_type->is_string_type())
     {
       index_type = Type::lookup_integer_type("int");
-      value_type = index_type;
+      value_type = Type::lookup_integer_type("int32");
     }
   else if (range_type->map_type() != NULL)
     {
@@ -5156,7 +5228,7 @@ For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
       return Statement::make_error_statement(this->location());
     }
 
-  source_location loc = this->location();
+  Location loc = this->location();
   Block* temp_block = new Block(enclosing, loc);
 
   Named_object* range_object = NULL;
@@ -5197,7 +5269,11 @@ For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
   //           original statements
   //   }
 
-  if (range_type->array_type() != NULL)
+  if (range_type->is_slice_type())
+    this->lower_range_slice(gogo, temp_block, body, range_object, range_temp,
+                           index_temp, value_temp, &init, &cond, &iter_init,
+                           &post);
+  else if (range_type->array_type() != NULL)
     this->lower_range_array(gogo, temp_block, body, range_object, range_temp,
                            index_temp, value_temp, &init, &cond, &iter_init,
                            &post);
@@ -5259,7 +5335,7 @@ For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
 Expression*
 For_range_statement::make_range_ref(Named_object* range_object,
                                    Temporary_statement* range_temp,
-                                   source_location loc)
+                                   Location loc)
 {
   if (range_object != NULL)
     return Expression::make_var_reference(range_object, loc);
@@ -5273,7 +5349,7 @@ For_range_statement::make_range_ref(Named_object* range_object,
 Expression*
 For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
                                  Expression* arg,
-                                 source_location loc)
+                                 Location loc)
 {
   Named_object* no = gogo->lookup_global(funcname);
   go_assert(no != NULL && no->is_function_declaration());
@@ -5283,7 +5359,7 @@ For_range_statement::call_builtin(Gogo* gogo, const char* funcname,
   return Expression::make_call(func, params, false, loc);
 }
 
-// Lower a for range over an array or slice.
+// Lower a for range over an array.
 
 void
 For_range_statement::lower_range_array(Gogo* gogo,
@@ -5298,7 +5374,7 @@ For_range_statement::lower_range_array(Gogo* gogo,
                                       Block** piter_init,
                                       Block** ppost)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   // The loop we generate:
   //   len_temp := len(range)
@@ -5375,6 +5451,107 @@ For_range_statement::lower_range_array(Gogo* gogo,
   *ppost = post;
 }
 
+// Lower a for range over a slice.
+
+void
+For_range_statement::lower_range_slice(Gogo* gogo,
+                                      Block* enclosing,
+                                      Block* body_block,
+                                      Named_object* range_object,
+                                      Temporary_statement* range_temp,
+                                      Temporary_statement* index_temp,
+                                      Temporary_statement* value_temp,
+                                      Block** pinit,
+                                      Expression** pcond,
+                                      Block** piter_init,
+                                      Block** ppost)
+{
+  Location loc = this->location();
+
+  // The loop we generate:
+  //   for_temp := range
+  //   len_temp := len(for_temp)
+  //   for index_temp = 0; index_temp < len_temp; index_temp++ {
+  //           value_temp = for_temp[index_temp]
+  //           index = index_temp
+  //           value = value_temp
+  //           original body
+  //   }
+  //
+  // Using for_temp means that we don't need to check bounds when
+  // fetching range_temp[index_temp].
+
+  // Set *PINIT to
+  //   range_temp := range
+  //   var len_temp int
+  //   len_temp = len(range_temp)
+  //   index_temp = 0
+
+  Block* init = new Block(enclosing, loc);
+
+  Expression* ref = this->make_range_ref(range_object, range_temp, loc);
+  Temporary_statement* for_temp = Statement::make_temporary(NULL, ref, loc);
+  init->add_statement(for_temp);
+
+  ref = Expression::make_temporary_reference(for_temp, loc);
+  Expression* len_call = this->call_builtin(gogo, "len", ref, loc);
+  Temporary_statement* len_temp = Statement::make_temporary(index_temp->type(),
+                                                           len_call, loc);
+  init->add_statement(len_temp);
+
+  mpz_t zval;
+  mpz_init_set_ui(zval, 0UL);
+  Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
+  mpz_clear(zval);
+
+  Temporary_reference_expression* tref =
+    Expression::make_temporary_reference(index_temp, loc);
+  tref->set_is_lvalue();
+  Statement* s = Statement::make_assignment(tref, zexpr, loc);
+  init->add_statement(s);
+
+  *pinit = init;
+
+  // Set *PCOND to
+  //   index_temp < len_temp
+
+  ref = Expression::make_temporary_reference(index_temp, loc);
+  Expression* ref2 = Expression::make_temporary_reference(len_temp, loc);
+  Expression* lt = Expression::make_binary(OPERATOR_LT, ref, ref2, loc);
+
+  *pcond = lt;
+
+  // Set *PITER_INIT to
+  //   value_temp = range[index_temp]
+
+  Block* iter_init = NULL;
+  if (value_temp != NULL)
+    {
+      iter_init = new Block(body_block, loc);
+
+      ref = Expression::make_temporary_reference(for_temp, loc);
+      Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
+      Expression* index = Expression::make_index(ref, ref2, NULL, loc);
+
+      tref = Expression::make_temporary_reference(value_temp, loc);
+      tref->set_is_lvalue();
+      s = Statement::make_assignment(tref, index, loc);
+
+      iter_init->add_statement(s);
+    }
+  *piter_init = iter_init;
+
+  // Set *PPOST to
+  //   index_temp++
+
+  Block* post = new Block(enclosing, loc);
+  tref = Expression::make_temporary_reference(index_temp, loc);
+  tref->set_is_lvalue();
+  s = Statement::make_inc_statement(tref);
+  post->add_statement(s);
+  *ppost = post;
+}
+
 // Lower a for range over a string.
 
 void
@@ -5390,7 +5567,7 @@ For_range_statement::lower_range_string(Gogo*,
                                        Block** piter_init,
                                        Block** ppost)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   // The loop we generate:
   //   var next_index_temp int
@@ -5518,7 +5695,7 @@ For_range_statement::lower_range_map(Gogo*,
                                     Block** piter_init,
                                     Block** ppost)
 {
-  source_location loc = this->location();
+  Location loc = this->location();
 
   // The runtime uses a struct to handle ranges over a map.  The
   // struct is four pointers long.  The first pointer is NULL when we
@@ -5548,7 +5725,7 @@ For_range_statement::lower_range_map(Gogo*,
   Expression* ref = Expression::make_temporary_reference(hiter, loc);
   Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
   Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
-  init->add_statement(Statement::make_statement(call));
+  init->add_statement(Statement::make_statement(call, true));
 
   *pinit = init;
 
@@ -5589,7 +5766,7 @@ For_range_statement::lower_range_map(Gogo*,
       Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
       call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
     }
-  iter_init->add_statement(Statement::make_statement(call));
+  iter_init->add_statement(Statement::make_statement(call, true));
 
   *piter_init = iter_init;
 
@@ -5601,7 +5778,7 @@ For_range_statement::lower_range_map(Gogo*,
   ref = Expression::make_temporary_reference(hiter, loc);
   p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
   call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
-  post->add_statement(Statement::make_statement(call));
+  post->add_statement(Statement::make_statement(call, true));
 
   *ppost = post;
 }
@@ -5623,7 +5800,7 @@ For_range_statement::lower_range_channel(Gogo*,
 {
   go_assert(value_temp == NULL);
 
-  source_location loc = this->location();
+  Location loc = this->location();
 
   // The loop we generate:
   //   for {
@@ -5661,7 +5838,7 @@ For_range_statement::lower_range_channel(Gogo*,
     Expression::make_temporary_reference(ok_temp, loc);
   oref->set_is_lvalue();
   Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
-                                                         false, loc);
+                                                         loc);
   iter_init->add_statement(s);
 
   Block* then_block = new Block(iter_init, loc);
@@ -5701,7 +5878,7 @@ For_range_statement::continue_label()
 void
 For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
 {
-  
+
   ast_dump_context->print_indent();
   ast_dump_context->ostream() << "for ";
   ast_dump_context->dump_expression(this->index_var_);
@@ -5710,17 +5887,22 @@ For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
       ast_dump_context->ostream() << ", ";
       ast_dump_context->dump_expression(this->value_var_);
     }
-    
-  ast_dump_context->ostream() << " = range ";      
+
+  ast_dump_context->ostream() << " = range ";
   ast_dump_context->dump_expression(this->range_);
-  ast_dump_context->ostream() << " {" << std::endl;
-  ast_dump_context->indent();
+  if (ast_dump_context->dump_subblocks())
+    {
+      ast_dump_context->ostream() << " {" << std::endl;
 
-  ast_dump_context->dump_block(this->statements_);
+      ast_dump_context->indent();
 
-  ast_dump_context->unindent();
-  ast_dump_context->print_indent();
-  ast_dump_context->ostream() << "}" << std::endl;
+      ast_dump_context->dump_block(this->statements_);
+
+      ast_dump_context->unindent();
+      ast_dump_context->print_indent();
+      ast_dump_context->ostream() << "}";
+    }
+  ast_dump_context->ostream() << std::endl;
 }
 
 // Make a for statement with a range clause.
@@ -5729,7 +5911,7 @@ For_range_statement*
 Statement::make_for_range_statement(Expression* index_var,
                                    Expression* value_var,
                                    Expression* range,
-                                   source_location location)
+                                   Location location)
 {
   return new For_range_statement(index_var, value_var, range, location);
 }