OSDN Git Service

Fix typo in error message.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / parse.cc
index 5f4cef5..b91bfbd 100644 (file)
@@ -35,7 +35,7 @@ Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
   // If we get here it means that a single nested function refers to
   // two different variables defined in enclosing functions, and both
   // variables have the same name.  I think this is impossible.
-  gcc_unreachable();
+  go_unreachable();
 }
 
 // Class Parse.
@@ -46,8 +46,8 @@ Parse::Parse(Lex* lex, Gogo* gogo)
     unget_token_(Token::make_invalid_token(0)),
     unget_token_valid_(false),
     gogo_(gogo),
-    break_stack_(),
-    continue_stack_(),
+    break_stack_(NULL),
+    continue_stack_(NULL),
     iota_(0),
     enclosing_vars_()
 {
@@ -85,7 +85,7 @@ Parse::advance_token()
 void
 Parse::unget_token(const Token& token)
 {
-  gcc_assert(!this->unget_token_valid_);
+  go_assert(!this->unget_token_valid_);
   this->unget_token_ = token;
   this->unget_token_valid_ = true;
 }
@@ -244,7 +244,10 @@ Parse::type()
     {
       source_location location = token->location();
       this->advance_token();
-      return this->signature(NULL, location);
+      Type* type = this->signature(NULL, location);
+      if (type == NULL)
+       return Type::make_error_type();
+      return type;
     }
   else if (token->is_keyword(KEYWORD_MAP))
     return this->map_type();
@@ -332,10 +335,17 @@ Parse::type_name(bool issue_error)
   bool ok = true;
   if (named_object == NULL)
     {
-      if (package != NULL)
-       ok = false;
-      else
+      if (package == NULL)
        named_object = this->gogo_->add_unknown_name(name, location);
+      else
+       {
+         const std::string& packname(package->package_value()->name());
+         error_at(location, "reference to undefined identifier %<%s.%s%>",
+                  Gogo::message_name(packname).c_str(),
+                  Gogo::message_name(name).c_str());
+         issue_error = false;
+         ok = false;
+       }
     }
   else if (named_object->is_type())
     {
@@ -359,7 +369,7 @@ Parse::type_name(bool issue_error)
   else if (named_object->is_unknown() || named_object->is_type_declaration())
     return Type::make_forward_declaration(named_object);
   else
-    gcc_unreachable();
+    go_unreachable();
 }
 
 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
@@ -369,7 +379,7 @@ Parse::type_name(bool issue_error)
 Type*
 Parse::array_type(bool may_use_ellipsis)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
+  go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
   const Token* token = this->advance_token();
 
   Expression* length = NULL;
@@ -416,7 +426,7 @@ Type*
 Parse::map_type()
 {
   source_location location = this->location();
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
   if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
     {
       error_at(this->location(), "expected %<[%>");
@@ -446,7 +456,7 @@ Parse::map_type()
 Type*
 Parse::struct_type()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
   source_location location = this->location();
   if (!this->advance_token()->is_op(OPERATOR_LCURLY))
     {
@@ -615,7 +625,7 @@ Parse::field_decl(Struct_field_list* sfl)
 Type*
 Parse::pointer_type()
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_MULT));
+  go_assert(this->peek_token()->is_op(OPERATOR_MULT));
   this->advance_token();
   Type* type = this->type();
   if (type->is_error_type())
@@ -646,34 +656,92 @@ Parse::channel_type()
     }
   else
     {
-      gcc_assert(token->is_keyword(KEYWORD_CHAN));
+      go_assert(token->is_keyword(KEYWORD_CHAN));
       if (this->advance_token()->is_op(OPERATOR_CHANOP))
        {
          receive = false;
          this->advance_token();
        }
     }
+
+  // Better error messages for the common error of omitting the
+  // channel element type.
+  if (!this->type_may_start_here())
+    {
+      token = this->peek_token();
+      if (token->is_op(OPERATOR_RCURLY))
+       error_at(this->location(), "unexpected %<}%> in channel type");
+      else if (token->is_op(OPERATOR_RPAREN))
+       error_at(this->location(), "unexpected %<)%> in channel type");
+      else if (token->is_op(OPERATOR_COMMA))
+       error_at(this->location(), "unexpected comma in channel type");
+      else
+       error_at(this->location(), "expected channel element type");
+      return Type::make_error_type();
+    }
+
   Type* element_type = this->type();
   return Type::make_channel_type(send, receive, element_type);
 }
 
+// Give an error for a duplicate parameter or receiver name.
+
+void
+Parse::check_signature_names(const Typed_identifier_list* params,
+                            Parse::Names* names)
+{
+  for (Typed_identifier_list::const_iterator p = params->begin();
+       p != params->end();
+       ++p)
+    {
+      if (p->name().empty() || Gogo::is_sink_name(p->name()))
+       continue;
+      std::pair<std::string, const Typed_identifier*> val =
+       std::make_pair(p->name(), &*p);
+      std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
+      if (!ins.second)
+       {
+         error_at(p->location(), "redefinition of %qs",
+                  Gogo::message_name(p->name()).c_str());
+         inform(ins.first->second->location(),
+                "previous definition of %qs was here",
+                Gogo::message_name(p->name()).c_str());
+       }
+    }
+}
+
 // Signature      = Parameters [ Result ] .
 
 // RECEIVER is the receiver if there is one, or NULL.  LOCATION is the
 // location of the start of the type.
 
+// This returns NULL on a parse error.
+
 Function_type*
 Parse::signature(Typed_identifier* receiver, source_location location)
 {
   bool is_varargs = false;
-  Typed_identifier_list* params = this->parameters(&is_varargs);
+  Typed_identifier_list* params;
+  bool params_ok = this->parameters(&params, &is_varargs);
 
-  Typed_identifier_list* result = NULL;
+  Typed_identifier_list* results = NULL;
   if (this->peek_token()->is_op(OPERATOR_LPAREN)
       || this->type_may_start_here())
-    result = this->result();
+    {
+      if (!this->result(&results))
+       return NULL;
+    }
+
+  if (!params_ok)
+    return NULL;
 
-  Function_type* ret = Type::make_function_type(receiver, params, result,
+  Parse::Names names;
+  if (params != NULL)
+    this->check_signature_names(params, &names);
+  if (results != NULL)
+    this->check_signature_names(results, &names);
+
+  Function_type* ret = Type::make_function_type(receiver, params, results,
                                                location);
   if (is_varargs)
     ret->set_is_varargs();
@@ -682,21 +750,28 @@ Parse::signature(Typed_identifier* receiver, source_location location)
 
 // Parameters     = "(" [ ParameterList [ "," ] ] ")" .
 
-Typed_identifier_list*
-Parse::parameters(bool* is_varargs)
+// This returns false on a parse error.
+
+bool
+Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
 {
+  *pparams = NULL;
+
   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
     {
       error_at(this->location(), "expected %<(%>");
-      return NULL;
+      return false;
     }
 
   Typed_identifier_list* params = NULL;
+  bool saw_error = false;
 
   const Token* token = this->advance_token();
   if (!token->is_op(OPERATOR_RPAREN))
     {
       params = this->parameter_list(is_varargs);
+      if (params == NULL)
+       saw_error = true;
       token = this->peek_token();
     }
 
@@ -707,7 +782,11 @@ Parse::parameters(bool* is_varargs)
   else
     this->advance_token();
 
-  return params;
+  if (saw_error)
+    return false;
+
+  *pparams = params;
+  return true;
 }
 
 // ParameterList  = ParameterDecl { "," ParameterDecl } .
@@ -717,12 +796,16 @@ Parse::parameters(bool* is_varargs)
 
 // We pick up an optional trailing comma.
 
+// This returns NULL if some error is seen.
+
 Typed_identifier_list*
 Parse::parameter_list(bool* is_varargs)
 {
   source_location location = this->location();
   Typed_identifier_list* ret = new Typed_identifier_list();
 
+  bool saw_error = false;
+
   // If we see an identifier and then a comma, then we don't know
   // whether we are looking at a list of identifiers followed by a
   // type, or a list of types given by name.  We have to do an
@@ -826,7 +909,7 @@ Parse::parameter_list(bool* is_varargs)
 
          if (parameters_have_names)
            {
-             gcc_assert(!just_saw_comma);
+             go_assert(!just_saw_comma);
              // We have just seen ID1, ID2 xxx.
              Type* type;
              if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
@@ -834,15 +917,16 @@ Parse::parameter_list(bool* is_varargs)
              else
                {
                  error_at(this->location(), "%<...%> only permits one name");
+                 saw_error = true;
                  this->advance_token();
                  type = this->type();
                }
              for (size_t i = 0; i < ret->size(); ++i)
                ret->set_type(i, type);
              if (!this->peek_token()->is_op(OPERATOR_COMMA))
-               return ret;
+               return saw_error ? NULL : ret;
              if (this->advance_token()->is_op(OPERATOR_RPAREN))
-               return ret;
+               return saw_error ? NULL : ret;
            }
          else
            {
@@ -865,6 +949,7 @@ Parse::parameter_list(bool* is_varargs)
                    {
                      error_at(p->location(), "expected %<%s%> to be a type",
                               Gogo::message_name(p->name()).c_str());
+                     saw_error = true;
                      type = Type::make_error_type();
                    }
                  tret->push_back(Typed_identifier("", type, p->location()));
@@ -873,7 +958,7 @@ Parse::parameter_list(bool* is_varargs)
              ret = tret;
              if (!just_saw_comma
                  || this->peek_token()->is_op(OPERATOR_RPAREN))
-               return ret;
+               return saw_error ? NULL : ret;
            }
        }
     }
@@ -883,13 +968,24 @@ Parse::parameter_list(bool* is_varargs)
   while (this->peek_token()->is_op(OPERATOR_COMMA))
     {
       if (is_varargs != NULL && *is_varargs)
-       error_at(this->location(), "%<...%> must be last parameter");
+       {
+         error_at(this->location(), "%<...%> must be last parameter");
+         saw_error = true;
+       }
       if (this->advance_token()->is_op(OPERATOR_RPAREN))
        break;
       this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
     }
   if (mix_error)
-    error_at(location, "invalid named/anonymous mix");
+    {
+      error_at(location, "invalid named/anonymous mix");
+      saw_error = true;
+    }
+  if (saw_error)
+    {
+      delete ret;
+      return NULL;
+    }
   return ret;
 }
 
@@ -973,18 +1069,26 @@ Parse::parameter_decl(bool parameters_have_names,
 
 // Result         = Parameters | Type .
 
-Typed_identifier_list*
-Parse::result()
+// This returns false on a parse error.
+
+bool
+Parse::result(Typed_identifier_list** presults)
 {
   if (this->peek_token()->is_op(OPERATOR_LPAREN))
-    return this->parameters(NULL);
+    return this->parameters(presults, NULL);
   else
     {
       source_location location = this->location();
-      Typed_identifier_list* til = new Typed_identifier_list();
       Type* type = this->type();
+      if (type->is_error_type())
+       {
+         *presults = NULL;
+         return false;
+       }
+      Typed_identifier_list* til = new Typed_identifier_list();
       til->push_back(Typed_identifier("", type, location));
-      return til;
+      *presults = til;
+      return true;
     }
 }
 
@@ -1054,7 +1158,7 @@ Parse::block()
 Type*
 Parse::interface_type()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
   source_location location = this->location();
 
   if (!this->advance_token()->is_op(OPERATOR_LCURLY))
@@ -1108,14 +1212,14 @@ Parse::interface_type()
 // MethodName         = identifier .
 // InterfaceTypeName  = TypeName .
 
-bool
+void
 Parse::method_spec(Typed_identifier_list* methods)
 {
   const Token* token = this->peek_token();
   if (!token->is_identifier())
     {
       error_at(this->location(), "expected identifier");
-      return false;
+      return;
     }
 
   std::string name = token->identifier();
@@ -1126,7 +1230,9 @@ Parse::method_spec(Typed_identifier_list* methods)
     {
       // This is a MethodName.
       name = this->gogo_->pack_hidden_name(name, is_exported);
-      Function_type* type = this->signature(NULL, location);
+      Type* type = this->signature(NULL, location);
+      if (type == NULL)
+       return;
       methods->push_back(Typed_identifier(name, type, location));
     }
   else
@@ -1148,15 +1254,13 @@ Parse::method_spec(Typed_identifier_list* methods)
                 && !token->is_op(OPERATOR_SEMICOLON)
                 && !token->is_op(OPERATOR_RCURLY))
            token = this->advance_token();
-         return false;
+         return;
        }
       // This must be an interface type, but we can't check that now.
       // We check it and pull out the methods in
       // Interface_type::do_verify.
       methods->push_back(Typed_identifier("", type, location));
     }
-
-  return false;
 }
 
 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
@@ -1242,7 +1346,7 @@ Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
 void
 Parse::const_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
   this->advance_token();
   this->reset_iota();
 
@@ -1343,7 +1447,7 @@ Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
 void
 Parse::type_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
   this->advance_token();
   this->decl(&Parse::type_spec, NULL);
 }
@@ -1408,7 +1512,7 @@ Parse::type_spec(void*)
          this->gogo_->define_type(named_type,
                                   Type::make_named_type(named_type, type,
                                                         location));
-         gcc_assert(named_type->package() == NULL);
+         go_assert(named_type->package() == NULL);
        }
       else
        {
@@ -1423,7 +1527,7 @@ Parse::type_spec(void*)
 void
 Parse::var_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
   this->advance_token();
   this->decl(&Parse::var_spec, NULL);
 }
@@ -1518,14 +1622,14 @@ Parse::init_vars(const Typed_identifier_list* til, Type* type,
        ++p)
     {
       if (init != NULL)
-       gcc_assert(pexpr != init->end());
+       go_assert(pexpr != init->end());
       this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
                     false, &any_new);
       if (init != NULL)
        ++pexpr;
     }
   if (init != NULL)
-    gcc_assert(pexpr == init->end());
+    go_assert(pexpr == init->end());
   if (is_coloneq && !any_new)
     error_at(location, "variables redeclared but no variable is new");
 }
@@ -1606,8 +1710,24 @@ Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
 
   if (!this->gogo_->in_global_scope())
     this->gogo_->add_statement(s);
+  else if (!val_no->is_sink())
+    {
+      if (val_no->is_variable())
+       val_no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
+  else if (!no->is_sink())
+    {
+      if (no->is_variable())
+       no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
   else
-    val_no->var_value()->add_preinit_statement(s);
+    {
+      // Execute the map index expression just so that we can fail if
+      // the map is nil.
+      Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
+                                                     NULL, location);
+      dummy->var_value()->add_preinit_statement(this->gogo_, s);
+    }
 
   return true;
 }
@@ -1653,12 +1773,27 @@ Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
   Statement* s = Statement::make_tuple_receive_assignment(val_var,
                                                          received_var,
                                                          receive->channel(),
+                                                         false,
                                                          location);
 
   if (!this->gogo_->in_global_scope())
     this->gogo_->add_statement(s);
+  else if (!val_no->is_sink())
+    {
+      if (val_no->is_variable())
+       val_no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
+  else if (!no->is_sink())
+    {
+      if (no->is_variable())
+       no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
   else
-    val_no->var_value()->add_preinit_statement(s);
+    {
+      Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
+                                                     NULL, location);
+      dummy->var_value()->add_preinit_statement(this->gogo_, s);
+    }
 
   return true;
 }
@@ -1709,8 +1844,21 @@ Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
 
   if (!this->gogo_->in_global_scope())
     this->gogo_->add_statement(s);
+  else if (!val_no->is_sink())
+    {
+      if (val_no->is_variable())
+       val_no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
+  else if (!no->is_sink())
+    {
+      if (no->is_variable())
+       no->var_value()->add_preinit_statement(this->gogo_, s);
+    }
   else
-    val_no->var_value()->add_preinit_statement(s);
+    {
+      Named_object* dummy = this->create_dummy_global(type, NULL, location);
+      dummy->var_value()->add_preinit_statement(this->gogo_, s);
+    }
 
   return true;
 }
@@ -1730,19 +1878,9 @@ Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
       if (!type_from_init && init != NULL)
        {
          if (!this->gogo_->in_global_scope())
-           this->gogo_->add_statement(Statement::make_statement(init));
+           this->gogo_->add_statement(Statement::make_statement(init, true));
          else
-           {
-             // Create a dummy global variable to force the
-             // initializer to be run in the right place.
-             Variable* var = new Variable(type, init, true, false, false,
-                                          location);
-             static int count;
-             char buf[30];
-             snprintf(buf, sizeof buf, "_.%d", count);
-             ++count;
-             return this->gogo_->add_variable(buf, var);
-           }
+           return this->create_dummy_global(type, init, location);
        }
       return this->gogo_->add_sink();
     }
@@ -1767,7 +1905,31 @@ Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
   *is_new = true;
   Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
                               false, false, location);
-  return this->gogo_->add_variable(tid.name(), var);
+  Named_object* no = this->gogo_->add_variable(tid.name(), var);
+  if (!no->is_variable())
+    {
+      // The name is already defined, so we just gave an error.
+      return this->gogo_->add_sink();
+    }
+  return no;
+}
+
+// Create a dummy global variable to force an initializer to be run in
+// the right place.  This is used when a sink variable is initialized
+// at global scope.
+
+Named_object*
+Parse::create_dummy_global(Type* type, Expression* init,
+                          source_location location)
+{
+  if (type == NULL && init == NULL)
+    type = Type::lookup_bool_type();
+  Variable* var = new Variable(type, init, true, false, false, location);
+  static int count;
+  char buf[30];
+  snprintf(buf, sizeof buf, "_.%d", count);
+  ++count;
+  return this->gogo_->add_variable(buf, var);
 }
 
 // SimpleVarDecl = identifier ":=" Expression .
@@ -1798,7 +1960,7 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
   // "a, *p = 1, 2".
   if (this->peek_token()->is_op(OPERATOR_COMMA))
     {
-      gcc_assert(p_type_switch == NULL);
+      go_assert(p_type_switch == NULL);
       while (true)
        {
          const Token* token = this->advance_token();
@@ -1856,7 +2018,7 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
        }
     }
 
-  gcc_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
+  go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
   const Token* token = this->advance_token();
 
   if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
@@ -1909,7 +2071,7 @@ Parse::simple_var_decl_or_assignment(const std::string& name,
 void
 Parse::function_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
   source_location location = this->location();
   const Token* token = this->advance_token();
 
@@ -1933,6 +2095,8 @@ Parse::function_decl()
   this->advance_token();
 
   Function_type* fntype = this->signature(rec, this->location());
+  if (fntype == NULL)
+    return;
 
   Named_object* named_object = NULL;
 
@@ -1956,9 +2120,12 @@ Parse::function_decl()
          return;
        }
       this->advance_token();
-      named_object = this->gogo_->declare_function(name, fntype, location);
-      if (named_object->is_function_declaration())
-       named_object->func_declaration_value()->set_asm_name(asm_name);
+      if (!Gogo::is_sink_name(name))
+       {
+         named_object = this->gogo_->declare_function(name, fntype, location);
+         if (named_object->is_function_declaration())
+           named_object->func_declaration_value()->set_asm_name(asm_name);
+       }
     }
 
   // Check for the easy error of a newline before the opening brace.
@@ -1975,8 +2142,8 @@ Parse::function_decl()
 
   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
     {
-      if (named_object == NULL)
-       named_object = this->gogo_->declare_function(name, fntype, location);
+      if (named_object == NULL && !Gogo::is_sink_name(name))
+       this->gogo_->declare_function(name, fntype, location);
     }
   else
     {
@@ -1992,7 +2159,7 @@ Parse::function_decl()
 Typed_identifier*
 Parse::receiver()
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
+  go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
 
   std::string name;
   const Token* token = this->advance_token();
@@ -2121,7 +2288,7 @@ Parse::operand(bool may_be_sink)
            packed = this->gogo_->pack_hidden_name(id, is_exported);
            named_object = package->lookup(packed);
            location = this->location();
-           gcc_assert(in_function == NULL);
+           go_assert(in_function == NULL);
          }
 
        this->advance_token();
@@ -2130,7 +2297,7 @@ Parse::operand(bool may_be_sink)
            && named_object->is_type()
            && !named_object->type_value()->is_visible())
          {
-           gcc_assert(package != NULL);
+           go_assert(package != NULL);
            error_at(location, "invalid reference to hidden type %<%s.%s%>",
                     Gogo::message_name(package->name()).c_str(),
                     Gogo::message_name(id).c_str());
@@ -2195,10 +2362,10 @@ Parse::operand(bool may_be_sink)
          case Named_object::NAMED_OBJECT_UNKNOWN:
            return Expression::make_unknown_reference(named_object, location);
          default:
-           gcc_unreachable();
+           go_unreachable();
          }
       }
-      gcc_unreachable();
+      go_unreachable();
 
     case Token::TOKEN_STRING:
       ret = Expression::make_string(token->string_value(), token->location());
@@ -2283,7 +2450,7 @@ Expression*
 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
                               source_location location)
 {
-  gcc_assert(var->is_variable() || var->is_result_variable());
+  go_assert(var->is_variable() || var->is_result_variable());
 
   Named_object* this_function = this->gogo_->current_function();
   Named_object* closure = this_function->func_value()->closure_var();
@@ -2317,7 +2484,9 @@ Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
 // LiteralValue  = "{" [ ElementList [ "," ] ] "}" .
 // ElementList   = Element { "," Element } .
 // Element       = [ Key ":" ] Value .
-// Key           = Expression .
+// Key           = FieldName | ElementIndex .
+// FieldName     = identifier .
+// ElementIndex  = Expression .
 // Value         = Expression | LiteralValue .
 
 // We have already seen the type if there is one, and we are now
@@ -2331,7 +2500,7 @@ Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
 Expression*
 Parse::composite_lit(Type* type, int depth, source_location location)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
+  go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
   this->advance_token();
 
   if (this->peek_token()->is_op(OPERATOR_RCURLY))
@@ -2350,7 +2519,33 @@ Parse::composite_lit(Type* type, int depth, source_location location)
 
       const Token* token = this->peek_token();
 
-      if (!token->is_op(OPERATOR_LCURLY))
+      if (token->is_identifier())
+       {
+         std::string identifier = token->identifier();
+         bool is_exported = token->is_identifier_exported();
+         source_location location = token->location();
+
+         if (this->advance_token()->is_op(OPERATOR_COLON))
+           {
+             // This may be a field name.  We don't know for sure--it
+             // could also be an expression for an array index.  We
+             // don't want to parse it as an expression because may
+             // trigger various errors, e.g., if this identifier
+             // happens to be the name of a package.
+             Gogo* gogo = this->gogo_;
+             val = this->id_to_expression(gogo->pack_hidden_name(identifier,
+                                                                 is_exported),
+                                          location);
+           }
+         else
+           {
+             this->unget_token(Token::make_identifier_token(identifier,
+                                                            is_exported,
+                                                            location));
+             val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+           }
+       }
+      else if (!token->is_op(OPERATOR_LCURLY))
        val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
       else
        {
@@ -2455,25 +2650,39 @@ Expression*
 Parse::function_lit()
 {
   source_location location = this->location();
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
   this->advance_token();
 
   Enclosing_vars hold_enclosing_vars;
   hold_enclosing_vars.swap(this->enclosing_vars_);
 
   Function_type* type = this->signature(NULL, location);
+  if (type == NULL)
+    type = Type::make_function_type(NULL, NULL, NULL, location);
 
   // For a function literal, the next token must be a '{'.  If we
   // don't see that, then we may have a type expression.
   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
     return Expression::make_type(type, location);
 
+  Bc_stack* hold_break_stack = this->break_stack_;
+  Bc_stack* hold_continue_stack = this->continue_stack_;
+  this->break_stack_ = NULL;
+  this->continue_stack_ = NULL;
+
   Named_object* no = this->gogo_->start_function("", type, true, location);
 
   source_location end_loc = this->block();
 
   this->gogo_->finish_function(end_loc);
 
+  if (this->break_stack_ != NULL)
+    delete this->break_stack_;
+  if (this->continue_stack_ != NULL)
+    delete this->continue_stack_;
+  this->break_stack_ = hold_break_stack;
+  this->continue_stack_ = hold_continue_stack;
+
   hold_enclosing_vars.swap(this->enclosing_vars_);
 
   Expression* closure = this->create_closure(no, &hold_enclosing_vars,
@@ -2511,7 +2720,7 @@ Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
   Expression_list* initializer = new Expression_list;
   for (size_t i = 0; i < enclosing_var_count; ++i)
     {
-      gcc_assert(ev[i].index() == i);
+      go_assert(ev[i].index() == i);
       Named_object* var = ev[i].var();
       Expression* ref;
       if (ev[i].in_function() == enclosing_function)
@@ -2580,13 +2789,32 @@ Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
          this->advance_token();
          Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
                                              NULL);
+         if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
+           {
+             error_at(this->location(),
+                      "invalid use of %<...%> in type conversion");
+             this->advance_token();
+           }
          if (!this->peek_token()->is_op(OPERATOR_RPAREN))
            error_at(this->location(), "expected %<)%>");
          else
            this->advance_token();
          if (expr->is_error_expression())
-           return expr;
-         ret = Expression::make_cast(ret->type(), expr, loc);
+           ret = expr;
+         else
+           {
+             Type* t = ret->type();
+             if (t->classification() == Type::TYPE_ARRAY
+                 && t->array_type()->length() != NULL
+                 && t->array_type()->length()->is_nil_expression())
+               {
+                 error_at(ret->location(),
+                          "invalid use of %<...%> in type conversion");
+                 ret = Expression::make_error(loc);
+               }
+             else
+               ret = Expression::make_cast(t, expr, loc);
+           }
        }
     }
 
@@ -2623,7 +2851,7 @@ Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
 Expression*
 Parse::selector(Expression* left, bool* is_type_switch)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_DOT));
+  go_assert(this->peek_token()->is_op(OPERATOR_DOT));
   source_location location = this->location();
 
   const Token* token = this->advance_token();
@@ -2683,7 +2911,7 @@ Expression*
 Parse::index(Expression* expr)
 {
   source_location location = this->location();
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
+  go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
   this->advance_token();
 
   Expression* start;
@@ -2719,7 +2947,7 @@ Parse::index(Expression* expr)
 Expression*
 Parse::call(Expression* func)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
+  go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
   Expression_list* args = NULL;
   bool is_varargs = false;
   const Token* token = this->advance_token();
@@ -2774,6 +3002,12 @@ Parse::id_to_expression(const std::string& name, source_location location)
       return Expression::make_func_reference(named_object, NULL, location);
     case Named_object::NAMED_OBJECT_UNKNOWN:
       return Expression::make_unknown_reference(named_object, location);
+    case Named_object::NAMED_OBJECT_PACKAGE:
+    case Named_object::NAMED_OBJECT_TYPE:
+    case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
+      // These cases can arise for a field name in a composite
+      // literal.
+      return Expression::make_unknown_reference(named_object, location);
     default:
       error_at(this->location(), "unexpected type of identifier");
       return Expression::make_error(location);
@@ -2820,9 +3054,6 @@ Parse::expression(Precedence precedence, bool may_be_sink,
        case OPERATOR_ANDAND:
          right_precedence = PRECEDENCE_ANDAND;
          break;
-       case OPERATOR_CHANOP:
-         right_precedence = PRECEDENCE_CHANOP;
-         break;
        case OPERATOR_EQEQ:
        case OPERATOR_NOTEQ:
        case OPERATOR_LT:
@@ -2873,10 +3104,7 @@ Parse::expression(Precedence precedence, bool may_be_sink,
       Expression* right = this->expression(right_precedence, false,
                                           may_be_composite_lit,
                                           NULL);
-      if (op == OPERATOR_CHANOP)
-       left = Expression::make_send(left, right, binop_location);
-      else
-       left = Expression::make_binary(op, left, right, binop_location);
+      left = Expression::make_binary(op, left, right, binop_location);
     }
 }
 
@@ -2926,7 +3154,7 @@ Parse::expression_may_start_here()
     case Token::TOKEN_IMAGINARY:
       return true;
     default:
-      gcc_unreachable();
+      go_unreachable();
     }
 }
 
@@ -2993,7 +3221,7 @@ Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
 // LABEL is the label of this statement if it has one.
 
 void
-Parse::statement(const Label* label)
+Parse::statement(Label* label)
 {
   const Token* token = this->peek_token();
   switch (token->classification())
@@ -3011,7 +3239,7 @@ Parse::statement(const Label* label)
          case KEYWORD_MAP:
          case KEYWORD_STRUCT:
          case KEYWORD_INTERFACE:
-           this->simple_stat(true, false, NULL, NULL);
+           this->simple_stat(true, NULL, NULL, NULL);
            break;
          case KEYWORD_GO:
          case KEYWORD_DEFER:
@@ -3064,7 +3292,7 @@ Parse::statement(const Label* label)
            this->unget_token(Token::make_identifier_token(identifier,
                                                           is_exported,
                                                           location));
-           this->simple_stat(true, false, NULL, NULL);
+           this->simple_stat(true, NULL, NULL, NULL);
          }
       }
       break;
@@ -3079,14 +3307,14 @@ Parse::statement(const Label* label)
                                 location);
        }
       else if (!token->is_op(OPERATOR_SEMICOLON))
-       this->simple_stat(true, false, NULL, NULL);
+       this->simple_stat(true, NULL, NULL, NULL);
       break;
 
     case Token::TOKEN_STRING:
     case Token::TOKEN_INTEGER:
     case Token::TOKEN_FLOAT:
     case Token::TOKEN_IMAGINARY:
-      this->simple_stat(true, false, NULL, NULL);
+      this->simple_stat(true, NULL, NULL, NULL);
       break;
 
     default:
@@ -3169,6 +3397,10 @@ Parse::labeled_stmt(const std::string& label_name, source_location location)
 
   if (!this->statement_may_start_here())
     {
+      // Mark the label as used to avoid a useless error about an
+      // unused label.
+      label->set_is_used();
+
       error_at(location, "missing statement after label");
       this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
                                                   location));
@@ -3178,14 +3410,18 @@ Parse::labeled_stmt(const std::string& label_name, source_location location)
   this->statement(label);
 }
 
-// SimpleStat =
-//   ExpressionStat | IncDecStat | Assignment | SimpleVarDecl .
+// SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
+//     Assignment | ShortVarDecl .
+
+// EmptyStmt was handled in Parse::statement.
 
 // In order to make this work for if and switch statements, if
-// RETURN_EXP is true, and we see an ExpressionStat, we return the
+// RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
 // expression rather than adding an expression statement to the
 // current block.  If we see something other than an ExpressionStat,
-// we add the statement and return NULL.
+// we add the statement, set *RETURN_EXP to true if we saw a send
+// statement, and return NULL.  The handling of send statements is for
+// better error messages.
 
 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
 // RangeClause.
@@ -3194,7 +3430,7 @@ Parse::labeled_stmt(const std::string& label_name, source_location location)
 // guard (var := expr.("type") using the literal keyword "type").
 
 Expression*
-Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
+Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
                   Range_clause* p_range_clause, Type_switch* p_type_switch)
 {
   const Token* token = this->peek_token();
@@ -3236,7 +3472,14 @@ Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
       return NULL;
     }
   token = this->peek_token();
-  if (token->is_op(OPERATOR_PLUSPLUS) || token->is_op(OPERATOR_MINUSMINUS))
+  if (token->is_op(OPERATOR_CHANOP))
+    {
+      this->send_stmt(this->verify_not_sink(exp));
+      if (return_exp != NULL)
+       *return_exp = true;
+    }
+  else if (token->is_op(OPERATOR_PLUSPLUS)
+          || token->is_op(OPERATOR_MINUSMINUS))
     this->inc_dec_stat(this->verify_not_sink(exp));
   else if (token->is_op(OPERATOR_COMMA)
           || token->is_op(OPERATOR_EQ))
@@ -3253,7 +3496,7 @@ Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
           || token->is_op(OPERATOR_ANDEQ)
           || token->is_op(OPERATOR_BITCLEAREQ))
     this->assignment(this->verify_not_sink(exp), p_range_clause);
-  else if (return_exp)
+  else if (return_exp != NULL)
     return this->verify_not_sink(exp);
   else
     this->expression_stat(this->verify_not_sink(exp));
@@ -3302,8 +3545,21 @@ Parse::statement_list_may_start_here()
 void
 Parse::expression_stat(Expression* exp)
 {
-  exp->discarding_value();
-  this->gogo_->add_statement(Statement::make_statement(exp));
+  this->gogo_->add_statement(Statement::make_statement(exp, false));
+}
+
+// SendStmt = Channel "&lt;-" Expression .
+// Channel  = Expression .
+
+void
+Parse::send_stmt(Expression* channel)
+{
+  go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
+  source_location loc = this->location();
+  this->advance_token();
+  Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+  Statement* s = Statement::make_send_statement(channel, val, loc);
+  this->gogo_->add_statement(s);
 }
 
 // IncDecStat = Expression ( "++" | "--" ) .
@@ -3322,7 +3578,7 @@ Parse::inc_dec_stat(Expression* exp)
   else if (token->is_op(OPERATOR_MINUSMINUS))
     this->gogo_->add_statement(Statement::make_dec_statement(exp));
   else
-    gcc_unreachable();
+    go_unreachable();
   this->advance_token();
 }
 
@@ -3492,6 +3748,7 @@ Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
       Expression* channel = receive->channel();
       Statement* s = Statement::make_tuple_receive_assignment(val, success,
                                                              channel,
+                                                             false,
                                                              location);
       this->gogo_->add_statement(s);
     }
@@ -3522,7 +3779,7 @@ Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
 void
 Parse::go_or_defer_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_GO)
+  go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
             || this->peek_token()->is_keyword(KEYWORD_DEFER));
   bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
   source_location stat_location = this->location();
@@ -3554,33 +3811,35 @@ Parse::go_or_defer_stat()
 void
 Parse::return_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
   source_location location = this->location();
   this->advance_token();
   Expression_list* vals = NULL;
   if (this->expression_may_start_here())
     vals = this->expression_list(NULL, false);
-  const Function* function = this->gogo_->current_function()->func_value();
-  const Typed_identifier_list* results = function->type()->results();
-  this->gogo_->add_statement(Statement::make_return_statement(results, vals,
-                                                             location));
+  this->gogo_->add_statement(Statement::make_return_statement(vals, location));
 }
 
-// IfStat = "if" [ [ SimpleStat ] ";" ] [ Condition ]
-//             Block [ "else" Statement ] .
+// IfStmt = "if" [ SimpleStmt ";" ] Expression Block
+//          [ "else" ( IfStmt | Block ) ] .
 
 void
 Parse::if_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_IF));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
   source_location location = this->location();
   this->advance_token();
 
   this->gogo_->start_block(location);
 
+  bool saw_simple_stat = false;
   Expression* cond = NULL;
+  bool saw_send_stmt;
   if (this->simple_stat_may_start_here())
-    cond = this->simple_stat(false, true, NULL, NULL);
+    {
+      cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
+      saw_simple_stat = true;
+    }
   if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
     {
       // The SimpleStat is an expression statement.
@@ -3591,7 +3850,25 @@ Parse::if_stat()
     {
       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
        this->advance_token();
-      if (!this->peek_token()->is_op(OPERATOR_LCURLY))
+      else if (saw_simple_stat)
+       {
+         if (saw_send_stmt)
+           error_at(this->location(),
+                    ("send statement used as value; "
+                     "use select for non-blocking send"));
+         else
+           error_at(this->location(),
+                    "expected %<;%> after statement in if expression");
+         if (!this->expression_may_start_here())
+           cond = Expression::make_error(this->location());
+       }
+      if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
+       {
+         error_at(this->location(),
+                  "missing condition in if statement");
+         cond = Expression::make_error(this->location());
+       }
+      if (cond == NULL)
        cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
     }
 
@@ -3614,10 +3891,17 @@ Parse::if_stat()
   Block* else_block = NULL;
   if (this->peek_token()->is_keyword(KEYWORD_ELSE))
     {
-      this->advance_token();
-      // We create a block to gather the statement.
       this->gogo_->start_block(this->location());
-      this->statement(NULL);
+      const Token* token = this->advance_token();
+      if (token->is_keyword(KEYWORD_IF))
+       this->if_stat();
+      else if (token->is_op(OPERATOR_LCURLY))
+       this->block();
+      else
+       {
+         error_at(this->location(), "expected %<if%> or %<{%>");
+         this->statement(NULL);
+       }
       else_block = this->gogo_->finish_block(this->location());
     }
 
@@ -3637,18 +3921,24 @@ Parse::if_stat()
 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
 
 void
-Parse::switch_stat(const Label* label)
+Parse::switch_stat(Label* label)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
   source_location location = this->location();
   this->advance_token();
 
   this->gogo_->start_block(location);
 
+  bool saw_simple_stat = false;
   Expression* switch_val = NULL;
+  bool saw_send_stmt;
   Type_switch type_switch;
   if (this->simple_stat_may_start_here())
-    switch_val = this->simple_stat(false, true, NULL, &type_switch);
+    {
+      switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
+                                    &type_switch);
+      saw_simple_stat = true;
+    }
   if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
     {
       // The SimpleStat is an expression statement.
@@ -3659,6 +3949,16 @@ Parse::switch_stat(const Label* label)
     {
       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
        this->advance_token();
+      else if (saw_simple_stat)
+       {
+         if (saw_send_stmt)
+           error_at(this->location(),
+                    ("send statement used as value; "
+                     "use select for non-blocking send"));
+         else
+           error_at(this->location(),
+                    "expected %<;%> after statement in switch expression");
+       }
       if (!this->peek_token()->is_op(OPERATOR_LCURLY))
        {
          if (this->peek_token()->is_identifier())
@@ -3676,13 +3976,16 @@ Parse::switch_stat(const Label* label)
              if (is_coloneq)
                {
                  // This must be a TypeSwitchGuard.
-                 switch_val = this->simple_stat(false, true, NULL,
+                 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
                                                 &type_switch);
-                 if (!type_switch.found
-                     && !switch_val->is_error_expression())
+                 if (!type_switch.found)
                    {
-                     error_at(id_loc, "expected type switch assignment");
-                     switch_val = Expression::make_error(id_loc);
+                     if (switch_val == NULL
+                         || !switch_val->is_error_expression())
+                       {
+                         error_at(id_loc, "expected type switch assignment");
+                         switch_val = Expression::make_error(id_loc);
+                       }
                    }
                }
            }
@@ -3706,6 +4009,19 @@ Parse::switch_stat(const Label* label)
       if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
          && this->advance_token()->is_op(OPERATOR_LCURLY))
        error_at(token_loc, "unexpected semicolon or newline before %<{%>");
+      else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
+       {
+         error_at(token_loc, "invalid variable name");
+         this->advance_token();
+         this->expression(PRECEDENCE_NORMAL, false, false,
+                          &type_switch.found);
+         if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
+           this->advance_token();
+         if (!this->peek_token()->is_op(OPERATOR_LCURLY))
+           return;
+         if (type_switch.found)
+           type_switch.expr = Expression::make_error(location);
+       }
       else
        {
          error_at(this->location(), "expected %<{%>");
@@ -3733,7 +4049,7 @@ Parse::switch_stat(const Label* label)
 //   "{" { ExprCaseClause } "}"
 
 Statement*
-Parse::expr_switch_body(const Label* label, Expression* switch_val,
+Parse::expr_switch_body(Label* label, Expression* switch_val,
                        source_location location)
 {
   Switch_statement* statement = Statement::make_switch_statement(switch_val,
@@ -3742,6 +4058,7 @@ Parse::expr_switch_body(const Label* label, Expression* switch_val,
   this->push_break_statement(statement, label);
 
   Case_clauses* case_clauses = new Case_clauses();
+  bool saw_default = false;
   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
     {
       if (this->peek_token()->is_eof())
@@ -3750,7 +4067,7 @@ Parse::expr_switch_body(const Label* label, Expression* switch_val,
            error_at(this->location(), "missing %<}%>");
          return NULL;
        }
-      this->expr_case_clause(case_clauses);
+      this->expr_case_clause(case_clauses, &saw_default);
     }
   this->advance_token();
 
@@ -3765,7 +4082,7 @@ Parse::expr_switch_body(const Label* label, Expression* switch_val,
 // FallthroughStat = "fallthrough" .
 
 void
-Parse::expr_case_clause(Case_clauses* clauses)
+Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
 {
   source_location location = this->location();
 
@@ -3797,6 +4114,16 @@ Parse::expr_case_clause(Case_clauses* clauses)
        this->advance_token();
     }
 
+  if (is_default)
+    {
+      if (*saw_default)
+       {
+         error_at(location, "multiple defaults in switch");
+         return;
+       }
+      *saw_default = true;
+    }
+
   if (is_default || vals != NULL)
     clauses->add(vals, is_default, statements, is_fallthrough, location);
 }
@@ -3832,7 +4159,7 @@ Parse::expr_switch_case(bool* is_default)
 //   "{" { TypeCaseClause } "}" .
 
 Statement*
-Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
+Parse::type_switch_body(Label* label, const Type_switch& type_switch,
                        source_location location)
 {
   Named_object* switch_no = NULL;
@@ -3853,6 +4180,7 @@ Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
   this->push_break_statement(statement, label);
 
   Type_case_clauses* case_clauses = new Type_case_clauses();
+  bool saw_default = false;
   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
     {
       if (this->peek_token()->is_eof())
@@ -3860,7 +4188,7 @@ Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
          error_at(this->location(), "missing %<}%>");
          return NULL;
        }
-      this->type_case_clause(switch_no, case_clauses);
+      this->type_case_clause(switch_no, case_clauses, &saw_default);
     }
   this->advance_token();
 
@@ -3874,7 +4202,8 @@ Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
 // TypeCaseClause  = TypeSwitchCase ":" [ StatementList ] .
 
 void
-Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
+Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
+                       bool* saw_default)
 {
   source_location location = this->location();
 
@@ -3916,7 +4245,13 @@ Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
 
   if (is_default)
     {
-      gcc_assert(types.empty());
+      go_assert(types.empty());
+      if (*saw_default)
+       {
+         error_at(location, "multiple defaults in type switch");
+         return;
+       }
+      *saw_default = true;
       clauses->add(NULL, false, true, statements, location);
     }
   else if (!types.empty())
@@ -3927,6 +4262,8 @@ Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
        clauses->add(*p, true, false, NULL, location);
       clauses->add(types.back(), false, false, statements, location);
     }
+  else
+    clauses->add(Type::make_error_type(), false, false, statements, location);
 }
 
 // TypeSwitchCase  = "case" type | "default"
@@ -3966,9 +4303,9 @@ Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
 // SelectStat = "select" "{" { CommClause } "}" .
 
 void
-Parse::select_stat(const Label* label)
+Parse::select_stat(Label* label)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
   source_location location = this->location();
   const Token* token = this->advance_token();
 
@@ -3991,6 +4328,7 @@ Parse::select_stat(const Label* label)
   this->push_break_statement(statement, label);
 
   Select_clauses* select_clauses = new Select_clauses();
+  bool saw_default = false;
   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
     {
       if (this->peek_token()->is_eof())
@@ -3998,7 +4336,7 @@ Parse::select_stat(const Label* label)
          error_at(this->location(), "expected %<}%>");
          return;
        }
-      this->comm_clause(select_clauses);
+      this->comm_clause(select_clauses, &saw_default);
     }
 
   this->advance_token();
@@ -4010,50 +4348,86 @@ Parse::select_stat(const Label* label)
   this->gogo_->add_statement(statement);
 }
 
-// CommClause = CommCase [ StatementList ] .
+// CommClause = CommCase ":" { Statement ";" } .
 
 void
-Parse::comm_clause(Select_clauses* clauses)
+Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
 {
   source_location location = this->location();
   bool is_send = false;
   Expression* channel = NULL;
   Expression* val = NULL;
+  Expression* closed = NULL;
   std::string varname;
+  std::string closedname;
   bool is_default = false;
-  bool got_case = this->comm_case(&is_send, &channel, &val, &varname,
-                                 &is_default);
+  bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
+                                 &varname, &closedname, &is_default);
 
-  Block* statements = NULL;
-  Named_object* var = NULL;
-  if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
+  if (!is_send
+      && varname.empty()
+      && closedname.empty()
+      && val != NULL
+      && val->index_expression() != NULL)
+    val->index_expression()->set_is_lvalue();
+
+  if (this->peek_token()->is_op(OPERATOR_COLON))
     this->advance_token();
-  else if (this->statement_list_may_start_here())
+  else
+    error_at(this->location(), "expected colon");
+
+  this->gogo_->start_block(this->location());
+
+  Named_object* var = NULL;
+  if (!varname.empty())
     {
-      this->gogo_->start_block(this->location());
+      // FIXME: LOCATION is slightly wrong here.
+      Variable* v = new Variable(NULL, channel, false, false, false,
+                                location);
+      v->set_type_from_chan_element();
+      var = this->gogo_->add_variable(varname, v);
+    }
+
+  Named_object* closedvar = NULL;
+  if (!closedname.empty())
+    {
+      // FIXME: LOCATION is slightly wrong here.
+      Variable* v = new Variable(Type::lookup_bool_type(), NULL,
+                                false, false, false, location);
+      closedvar = this->gogo_->add_variable(closedname, v);
+    }
+
+  this->statement_list();
 
-      if (!varname.empty())
+  Block* statements = this->gogo_->finish_block(this->location());
+
+  if (is_default)
+    {
+      if (*saw_default)
        {
-         // FIXME: LOCATION is slightly wrong here.
-         Variable* v = new Variable(NULL, channel, false, false, false,
-                                    location);
-         v->set_type_from_chan_element();
-         var = this->gogo_->add_variable(varname, v);
+         error_at(location, "multiple defaults in select");
+         return;
        }
-
-      this->statement_list();
-      statements = this->gogo_->finish_block(this->location());
+      *saw_default = true;
     }
 
   if (got_case)
-    clauses->add(is_send, channel, val, var, is_default, statements, location);
+    clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
+                statements, location);
+  else if (statements != NULL)
+    {
+      // Add the statements to make sure that any names they define
+      // are traversed.
+      this->gogo_->add_block(statements, location);
+    }
 }
 
-// CommCase = ( "default" | ( "case" ( SendExpr | RecvExpr) ) ) ":" .
+// CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
 
 bool
 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
-                std::string* varname, bool* is_default)
+                Expression** closed, std::string* varname,
+                std::string* closedname, bool* is_default)
 {
   const Token* token = this->peek_token();
   if (token->is_keyword(KEYWORD_DEFAULT))
@@ -4064,7 +4438,8 @@ Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
   else if (token->is_keyword(KEYWORD_CASE))
     {
       this->advance_token();
-      if (!this->send_or_recv_expr(is_send, channel, val, varname))
+      if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
+                                  closedname))
        return false;
     }
   else
@@ -4075,95 +4450,180 @@ Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
       return false;
     }
 
-  if (!this->peek_token()->is_op(OPERATOR_COLON))
-    {
-      error_at(this->location(), "expected colon");
-      return false;
-    }
-
-  this->advance_token();
-
   return true;
 }
 
-// SendExpr = Expression "<-" Expression .
-// RecvExpr =  [ Expression ( "=" | ":=" ) ] "<-" Expression .
+// RecvStmt   = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
+// RecvExpr   = Expression .
 
 bool
-Parse::send_or_recv_expr(bool* is_send, Expression** channel, Expression** val,
-                        std::string* varname)
+Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
+                        Expression** closed, std::string* varname,
+                        std::string* closedname)
 {
   const Token* token = this->peek_token();
-  source_location location = token->location();
+  bool saw_comma = false;
+  bool closed_is_id = false;
   if (token->is_identifier())
     {
+      Gogo* gogo = this->gogo_;
       std::string recv_var = token->identifier();
-      bool is_var_exported = token->is_identifier_exported();
-      if (!this->advance_token()->is_op(OPERATOR_COLONEQ))
-       this->unget_token(Token::make_identifier_token(recv_var,
-                                                      is_var_exported,
-                                                      location));
-      else
+      bool is_rv_exported = token->is_identifier_exported();
+      source_location recv_var_loc = token->location();
+      token = this->advance_token();
+      if (token->is_op(OPERATOR_COLONEQ))
        {
+         // case rv := <-c:
          if (!this->advance_token()->is_op(OPERATOR_CHANOP))
            {
              error_at(this->location(), "expected %<<-%>");
              return false;
            }
+         if (recv_var == "_")
+           {
+             error_at(recv_var_loc,
+                      "no new variables on left side of %<:=%>");
+             recv_var = "blank";
+           }
          *is_send = false;
-         *varname = this->gogo_->pack_hidden_name(recv_var, is_var_exported);
+         *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
          this->advance_token();
          *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
          return true;
        }
+      else if (token->is_op(OPERATOR_COMMA))
+       {
+         token = this->advance_token();
+         if (token->is_identifier())
+           {
+             std::string recv_closed = token->identifier();
+             bool is_rc_exported = token->is_identifier_exported();
+             source_location recv_closed_loc = token->location();
+             closed_is_id = true;
+
+             token = this->advance_token();
+             if (token->is_op(OPERATOR_COLONEQ))
+               {
+                 // case rv, rc := <-c:
+                 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
+                   {
+                     error_at(this->location(), "expected %<<-%>");
+                     return false;
+                   }
+                 if (recv_var == "_" && recv_closed == "_")
+                   {
+                     error_at(recv_var_loc,
+                              "no new variables on left side of %<:=%>");
+                     recv_var = "blank";
+                   }
+                 *is_send = false;
+                 if (recv_var != "_")
+                   *varname = gogo->pack_hidden_name(recv_var,
+                                                     is_rv_exported);
+                 if (recv_closed != "_")
+                   *closedname = gogo->pack_hidden_name(recv_closed,
+                                                        is_rc_exported);
+                 this->advance_token();
+                 *channel = this->expression(PRECEDENCE_NORMAL, false, true,
+                                             NULL);
+                 return true;
+               }
+
+             this->unget_token(Token::make_identifier_token(recv_closed,
+                                                            is_rc_exported,
+                                                            recv_closed_loc));
+           }
+
+         *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
+                                                              is_rv_exported),
+                                       recv_var_loc);
+         saw_comma = true;
+       }
+      else
+       this->unget_token(Token::make_identifier_token(recv_var,
+                                                      is_rv_exported,
+                                                      recv_var_loc));
     }
 
-  if (this->peek_token()->is_op(OPERATOR_CHANOP))
+  // If SAW_COMMA is false, then we are looking at the start of the
+  // send or receive expression.  If SAW_COMMA is true, then *VAL is
+  // set and we just read a comma.
+
+  Expression* e;
+  if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
+    e = this->expression(PRECEDENCE_NORMAL, true, true, NULL);
+  else
     {
+      // case <-c:
       *is_send = false;
       this->advance_token();
       *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+
+      // The next token should be ':'.  If it is '<-', then we have
+      // case <-c <- v:
+      // which is to say, send on a channel received from a channel.
+      if (!this->peek_token()->is_op(OPERATOR_CHANOP))
+       return true;
+
+      e = Expression::make_receive(*channel, (*channel)->location());
     }
-  else
-    {
-      Expression* left = this->expression(PRECEDENCE_CHANOP, true, true, NULL);
 
-      if (this->peek_token()->is_op(OPERATOR_EQ))
+  if (this->peek_token()->is_op(OPERATOR_EQ))
+    {
+      if (!this->advance_token()->is_op(OPERATOR_CHANOP))
        {
-         if (!this->advance_token()->is_op(OPERATOR_CHANOP))
-           {
-             error_at(this->location(), "missing %<<-%>");
-             return false;
-           }
-         *is_send = false;
-         *val = left;
-         this->advance_token();
-         *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+         error_at(this->location(), "missing %<<-%>");
+         return false;
        }
-      else if (this->peek_token()->is_op(OPERATOR_CHANOP))
+      *is_send = false;
+      this->advance_token();
+      *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+      if (saw_comma)
        {
-         *is_send = true;
-         *channel = this->verify_not_sink(left);
-         this->advance_token();
-         *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+         // case v, e = <-c:
+         // *VAL is already set.
+         if (!e->is_sink_expression())
+           *closed = e;
        }
       else
        {
-         error_at(this->location(), "expected %<<-%> or %<=%>");
-         return false;
+         // case v = <-c:
+         if (!e->is_sink_expression())
+           *val = e;
        }
+      return true;
     }
 
-  return true;
+  if (saw_comma)
+    {
+      if (closed_is_id)
+       error_at(this->location(), "expected %<=%> or %<:=%>");
+      else
+       error_at(this->location(), "expected %<=%>");
+      return false;
+    }
+
+  if (this->peek_token()->is_op(OPERATOR_CHANOP))
+    {
+      // case c <- v:
+      *is_send = true;
+      *channel = this->verify_not_sink(e);
+      this->advance_token();
+      *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+      return true;
+    }
+
+  error_at(this->location(), "expected %<<-%> or %<=%>");
+  return false;
 }
 
 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
 // Condition = Expression .
 
 void
-Parse::for_stat(const Label* label)
+Parse::for_stat(Label* label)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
   source_location location = this->location();
   const Token* token = this->advance_token();
 
@@ -4191,11 +4651,19 @@ Parse::for_stat(const Label* label)
        {
          // We might be looking at a Condition, an InitStat, or a
          // RangeClause.
-         cond = this->simple_stat(false, true, &range_clause, NULL);
+         bool saw_send_stmt;
+         cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
          if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
            {
              if (cond == NULL && !range_clause.found)
-               error_at(this->location(), "parse error in for statement");
+               {
+                 if (saw_send_stmt)
+                   error_at(this->location(),
+                            ("send statement used as value; "
+                             "use select for non-blocking send"));
+                 else
+                   error_at(this->location(), "parse error in for statement");
+               }
            }
          else
            {
@@ -4275,7 +4743,7 @@ Parse::for_stat(const Label* label)
 void
 Parse::for_clause(Expression** cond, Block** post)
 {
-  gcc_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
+  go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
   this->advance_token();
   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
     *cond = NULL;
@@ -4299,7 +4767,7 @@ Parse::for_clause(Expression** cond, Block** post)
   else
     {
       this->gogo_->start_block(this->location());
-      this->simple_stat(false, false, NULL, NULL);
+      this->simple_stat(false, NULL, NULL, NULL);
       *post = this->gogo_->finish_block(this->location());
     }
 }
@@ -4312,12 +4780,12 @@ void
 Parse::range_clause_decl(const Typed_identifier_list* til,
                         Range_clause* p_range_clause)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
   source_location location = this->location();
 
   p_range_clause->found = true;
 
-  gcc_assert(til->size() >= 1);
+  go_assert(til->size() >= 1);
   if (til->size() > 2)
     error_at(this->location(), "too many variables for range clause");
 
@@ -4358,11 +4826,11 @@ void
 Parse::range_clause_expr(const Expression_list* vals,
                         Range_clause* p_range_clause)
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
 
   p_range_clause->found = true;
 
-  gcc_assert(vals->size() >= 1);
+  go_assert(vals->size() >= 1);
   if (vals->size() > 2)
     error_at(this->location(), "too many variables for range clause");
 
@@ -4380,17 +4848,21 @@ Parse::range_clause_expr(const Expression_list* vals,
 // Push a statement on the break stack.
 
 void
-Parse::push_break_statement(Statement* enclosing, const Label* label)
+Parse::push_break_statement(Statement* enclosing, Label* label)
 {
-  this->break_stack_.push_back(std::make_pair(enclosing, label));
+  if (this->break_stack_ == NULL)
+    this->break_stack_ = new Bc_stack();
+  this->break_stack_->push_back(std::make_pair(enclosing, label));
 }
 
 // Push a statement on the continue stack.
 
 void
-Parse::push_continue_statement(Statement* enclosing, const Label* label)
+Parse::push_continue_statement(Statement* enclosing, Label* label)
 {
-  this->continue_stack_.push_back(std::make_pair(enclosing, label));
+  if (this->continue_stack_ == NULL)
+    this->continue_stack_ = new Bc_stack();
+  this->continue_stack_->push_back(std::make_pair(enclosing, label));
 }
 
 // Pop the break stack.
@@ -4398,7 +4870,7 @@ Parse::push_continue_statement(Statement* enclosing, const Label* label)
 void
 Parse::pop_break_statement()
 {
-  this->break_stack_.pop_back();
+  this->break_stack_->pop_back();
 }
 
 // Pop the continue stack.
@@ -4406,7 +4878,7 @@ Parse::pop_break_statement()
 void
 Parse::pop_continue_statement()
 {
-  this->continue_stack_.pop_back();
+  this->continue_stack_->pop_back();
 }
 
 // Find a break or continue statement given a label name.
@@ -4414,11 +4886,18 @@ Parse::pop_continue_statement()
 Statement*
 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
 {
+  if (bc_stack == NULL)
+    return NULL;
   for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
        p != bc_stack->rend();
        ++p)
-    if (p->second != NULL && p->second->name() == label)
-      return p->first;
+    {
+      if (p->second != NULL && p->second->name() == label)
+       {
+         p->second->set_is_used();
+         return p->first;
+       }
+    }
   return NULL;
 }
 
@@ -4427,30 +4906,32 @@ Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
 void
 Parse::break_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
   source_location location = this->location();
 
   const Token* token = this->advance_token();
   Statement* enclosing;
   if (!token->is_identifier())
     {
-      if (this->break_stack_.empty())
+      if (this->break_stack_ == NULL || this->break_stack_->empty())
        {
          error_at(this->location(),
                   "break statement not within for or switch or select");
          return;
        }
-      enclosing = this->break_stack_.back().first;
+      enclosing = this->break_stack_->back().first;
     }
   else
     {
-      enclosing = this->find_bc_statement(&this->break_stack_,
+      enclosing = this->find_bc_statement(this->break_stack_,
                                          token->identifier());
       if (enclosing == NULL)
        {
-         error_at(token->location(),
-                  ("break label %qs not associated with "
-                   "for or switch or select"),
+         // If there is a label with this name, mark it as used to
+         // avoid a useless error about an unused label.
+         this->gogo_->add_label_reference(token->identifier(), 0, false);
+
+         error_at(token->location(), "invalid break label %qs",
                   Gogo::message_name(token->identifier()).c_str());
          this->advance_token();
          return;
@@ -4470,7 +4951,7 @@ Parse::break_stat()
   else if (enclosing->classification() == Statement::STATEMENT_SELECT)
     label = enclosing->select_statement()->break_label();
   else
-    gcc_unreachable();
+    go_unreachable();
 
   this->gogo_->add_statement(Statement::make_break_statement(label,
                                                             location));
@@ -4481,28 +4962,31 @@ Parse::break_stat()
 void
 Parse::continue_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
   source_location location = this->location();
 
   const Token* token = this->advance_token();
   Statement* enclosing;
   if (!token->is_identifier())
     {
-      if (this->continue_stack_.empty())
+      if (this->continue_stack_ == NULL || this->continue_stack_->empty())
        {
          error_at(this->location(), "continue statement not within for");
          return;
        }
-      enclosing = this->continue_stack_.back().first;
+      enclosing = this->continue_stack_->back().first;
     }
   else
     {
-      enclosing = this->find_bc_statement(&this->continue_stack_,
+      enclosing = this->find_bc_statement(this->continue_stack_,
                                          token->identifier());
       if (enclosing == NULL)
        {
-         error_at(token->location(),
-                  "continue label %qs not associated with for",
+         // If there is a label with this name, mark it as used to
+         // avoid a useless error about an unused label.
+         this->gogo_->add_label_reference(token->identifier(), 0, false);
+
+         error_at(token->location(), "invalid continue label %qs",
                   Gogo::message_name(token->identifier()).c_str());
          this->advance_token();
          return;
@@ -4516,7 +5000,7 @@ Parse::continue_stat()
   else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
     label = enclosing->for_range_statement()->continue_label();
   else
-    gcc_unreachable();
+    go_unreachable();
 
   this->gogo_->add_statement(Statement::make_continue_statement(label,
                                                                location));
@@ -4527,14 +5011,15 @@ Parse::continue_stat()
 void
 Parse::goto_stat()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
   source_location location = this->location();
   const Token* token = this->advance_token();
   if (!token->is_identifier())
     error_at(this->location(), "expected label for goto");
   else
     {
-      Label* label = this->gogo_->add_label_reference(token->identifier());
+      Label* label = this->gogo_->add_label_reference(token->identifier(),
+                                                     location, true);
       Statement* s = Statement::make_goto_statement(label, location);
       this->gogo_->add_statement(s);
       this->advance_token();
@@ -4581,7 +5066,7 @@ Parse::package_clause()
 void
 Parse::import_decl()
 {
-  gcc_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
+  go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
   this->advance_token();
   this->decl(&Parse::import_spec, NULL);
 }
@@ -4667,8 +5152,13 @@ Parse::program()
        token = this->advance_token();
       else if (!token->is_eof() || !saw_errors())
        {
-         error_at(this->location(),
-                  "expected %<;%> or newline after top level declaration");
+         if (token->is_op(OPERATOR_CHANOP))
+           error_at(this->location(),
+                    ("send statement used as value; "
+                     "use select for non-blocking send"));
+         else
+           error_at(this->location(),
+                    "expected %<;%> or newline after top level declaration");
          this->skip_past_error(OPERATOR_INVALID);
        }
     }