github.com/kidsbmilk/gofronted_all@v0.0.0-20220701224323-6479d5976c5d/go/parse.cc (about)

     1  // parse.cc -- Go frontend parser.
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  #include "go-system.h"
     8  
     9  #include "lex.h"
    10  #include "gogo.h"
    11  #include "go-diagnostics.h"
    12  #include "types.h"
    13  #include "statements.h"
    14  #include "expressions.h"
    15  #include "parse.h"
    16  
    17  // Struct Parse::Enclosing_var_comparison.
    18  
    19  // Return true if v1 should be considered to be less than v2.
    20  
    21  bool
    22  Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
    23  					    const Enclosing_var& v2) const
    24  {
    25    if (v1.var() == v2.var())
    26      return false;
    27  
    28    const std::string& n1(v1.var()->name());
    29    const std::string& n2(v2.var()->name());
    30    int i = n1.compare(n2);
    31    if (i < 0)
    32      return true;
    33    else if (i > 0)
    34      return false;
    35  
    36    // If we get here it means that a single nested function refers to
    37    // two different variables defined in enclosing functions, and both
    38    // variables have the same name.  I think this is impossible.
    39    go_unreachable();
    40  }
    41  
    42  // Class Parse.
    43  
    44  Parse::Parse(Lex* lex, Gogo* gogo)
    45    : lex_(lex),
    46      token_(Token::make_invalid_token(Linemap::unknown_location())),
    47      unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
    48      unget_token_valid_(false),
    49      is_erroneous_function_(false),
    50      gogo_(gogo),
    51      break_stack_(NULL),
    52      continue_stack_(NULL),
    53      enclosing_vars_()
    54  {
    55  }
    56  
    57  // Return the current token.
    58  
    59  const Token*
    60  Parse::peek_token()
    61  {
    62    if (this->unget_token_valid_)
    63      return &this->unget_token_;
    64    if (this->token_.is_invalid())
    65      this->token_ = this->lex_->next_token();
    66    return &this->token_;
    67  }
    68  
    69  // Advance to the next token and return it.
    70  
    71  const Token*
    72  Parse::advance_token()
    73  {
    74    if (this->unget_token_valid_)
    75      {
    76        this->unget_token_valid_ = false;
    77        if (!this->token_.is_invalid())
    78  	return &this->token_;
    79      }
    80    this->token_ = this->lex_->next_token();
    81    return &this->token_;
    82  }
    83  
    84  // Push a token back on the input stream.
    85  
    86  void
    87  Parse::unget_token(const Token& token)
    88  {
    89    go_assert(!this->unget_token_valid_);
    90    this->unget_token_ = token;
    91    this->unget_token_valid_ = true;
    92  }
    93  
    94  // The location of the current token.
    95  
    96  Location
    97  Parse::location()
    98  {
    99    return this->peek_token()->location();
   100  }
   101  
   102  // IdentifierList = identifier { "," identifier } .
   103  
   104  void
   105  Parse::identifier_list(Typed_identifier_list* til)
   106  {
   107    const Token* token = this->peek_token();
   108    while (true)
   109      {
   110        if (!token->is_identifier())
   111  	{
   112  	  go_error_at(this->location(), "expected identifier");
   113  	  return;
   114  	}
   115        std::string name =
   116  	this->gogo_->pack_hidden_name(token->identifier(),
   117  				      token->is_identifier_exported());
   118        til->push_back(Typed_identifier(name, NULL, token->location()));
   119        token = this->advance_token();
   120        if (!token->is_op(OPERATOR_COMMA))
   121  	return;
   122        token = this->advance_token();
   123      }
   124  }
   125  
   126  // ExpressionList = Expression { "," Expression } .
   127  
   128  // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
   129  // literal.
   130  
   131  // If MAY_BE_SINK is true, the expressions in the list may be "_".
   132  
   133  Expression_list*
   134  Parse::expression_list(Expression* first, bool may_be_sink,
   135  		       bool may_be_composite_lit)
   136  {
   137    Expression_list* ret = new Expression_list();
   138    if (first != NULL)
   139      ret->push_back(first);
   140    while (true)
   141      {
   142        ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink,
   143  				      may_be_composite_lit, NULL, NULL));
   144  
   145        const Token* token = this->peek_token();
   146        if (!token->is_op(OPERATOR_COMMA))
   147  	return ret;
   148  
   149        // Most expression lists permit a trailing comma.
   150        Location location = token->location();
   151        this->advance_token();
   152        if (!this->expression_may_start_here())
   153  	{
   154  	  this->unget_token(Token::make_operator_token(OPERATOR_COMMA,
   155  						       location));
   156  	  return ret;
   157  	}
   158      }
   159  }
   160  
   161  // QualifiedIdent = [ PackageName "." ] identifier .
   162  // PackageName = identifier .
   163  
   164  // This sets *PNAME to the identifier and sets *PPACKAGE to the
   165  // package or NULL if there isn't one.  This returns true on success,
   166  // false on failure in which case it will have emitted an error
   167  // message.
   168  
   169  bool
   170  Parse::qualified_ident(std::string* pname, Named_object** ppackage)
   171  {
   172    const Token* token = this->peek_token();
   173    if (!token->is_identifier())
   174      {
   175        go_error_at(this->location(), "expected identifier");
   176        return false;
   177      }
   178  
   179    std::string name = token->identifier();
   180    bool is_exported = token->is_identifier_exported();
   181    name = this->gogo_->pack_hidden_name(name, is_exported);
   182  
   183    token = this->advance_token();
   184    if (!token->is_op(OPERATOR_DOT))
   185      {
   186        *pname = name;
   187        *ppackage = NULL;
   188        return true;
   189      }
   190  
   191    Named_object* package = this->gogo_->lookup(name, NULL);
   192    if (package == NULL || !package->is_package())
   193      {
   194        go_error_at(this->location(), "expected package");
   195        // We expect . IDENTIFIER; skip both.
   196        if (this->advance_token()->is_identifier())
   197  	this->advance_token();
   198        return false;
   199      }
   200  
   201    package->package_value()->note_usage(Gogo::unpack_hidden_name(name));
   202  
   203    token = this->advance_token();
   204    if (!token->is_identifier())
   205      {
   206        go_error_at(this->location(), "expected identifier");
   207        return false;
   208      }
   209  
   210    name = token->identifier();
   211  
   212    if (name == "_")
   213      {
   214        go_error_at(this->location(), "invalid use of %<_%>");
   215        name = Gogo::erroneous_name();
   216      }
   217  
   218    if (package->name() == this->gogo_->package_name())
   219      name = this->gogo_->pack_hidden_name(name,
   220  					 token->is_identifier_exported());
   221  
   222    *pname = name;
   223    *ppackage = package;
   224  
   225    this->advance_token();
   226  
   227    return true;
   228  }
   229  
   230  // Type = TypeName | TypeLit | "(" Type ")" .
   231  // TypeLit =
   232  // 	ArrayType | StructType | PointerType | FunctionType | InterfaceType |
   233  // 	SliceType | MapType | ChannelType .
   234  
   235  Type*
   236  Parse::type()
   237  {
   238    const Token* token = this->peek_token();
   239    if (token->is_identifier())
   240      return this->type_name(true);
   241    else if (token->is_op(OPERATOR_LSQUARE))
   242      return this->array_type(false);
   243    else if (token->is_keyword(KEYWORD_CHAN)
   244  	   || token->is_op(OPERATOR_CHANOP))
   245      return this->channel_type();
   246    else if (token->is_keyword(KEYWORD_INTERFACE))
   247      return this->interface_type(true);
   248    else if (token->is_keyword(KEYWORD_FUNC))
   249      {
   250        Location location = token->location();
   251        this->advance_token();
   252        Type* type = this->signature(NULL, location);
   253        if (type == NULL)
   254  	return Type::make_error_type();
   255        return type;
   256      }
   257    else if (token->is_keyword(KEYWORD_MAP))
   258      return this->map_type();
   259    else if (token->is_keyword(KEYWORD_STRUCT))
   260      return this->struct_type();
   261    else if (token->is_op(OPERATOR_MULT))
   262      return this->pointer_type();
   263    else if (token->is_op(OPERATOR_LPAREN))
   264      {
   265        this->advance_token();
   266        Type* ret = this->type();
   267        if (this->peek_token()->is_op(OPERATOR_RPAREN))
   268  	this->advance_token();
   269        else
   270  	{
   271  	  if (!ret->is_error_type())
   272  	    go_error_at(this->location(), "expected %<)%>");
   273  	}
   274        return ret;
   275      }
   276    else
   277      {
   278        go_error_at(token->location(), "expected type");
   279        return Type::make_error_type();
   280      }
   281  }
   282  
   283  bool
   284  Parse::type_may_start_here()
   285  {
   286    const Token* token = this->peek_token();
   287    return (token->is_identifier()
   288  	  || token->is_op(OPERATOR_LSQUARE)
   289  	  || token->is_op(OPERATOR_CHANOP)
   290  	  || token->is_keyword(KEYWORD_CHAN)
   291  	  || token->is_keyword(KEYWORD_INTERFACE)
   292  	  || token->is_keyword(KEYWORD_FUNC)
   293  	  || token->is_keyword(KEYWORD_MAP)
   294  	  || token->is_keyword(KEYWORD_STRUCT)
   295  	  || token->is_op(OPERATOR_MULT)
   296  	  || token->is_op(OPERATOR_LPAREN));
   297  }
   298  
   299  // TypeName = QualifiedIdent .
   300  
   301  // If MAY_BE_NIL is true, then an identifier with the value of the
   302  // predefined constant nil is accepted, returning the nil type.
   303  
   304  Type*
   305  Parse::type_name(bool issue_error)
   306  {
   307    Location location = this->location();
   308  
   309    std::string name;
   310    Named_object* package;
   311    if (!this->qualified_ident(&name, &package))
   312      return Type::make_error_type();
   313  
   314    Named_object* named_object;
   315    if (package == NULL)
   316      named_object = this->gogo_->lookup(name, NULL);
   317    else
   318      {
   319        named_object = package->package_value()->lookup(name);
   320        if (named_object == NULL
   321  	  && issue_error
   322  	  && package->name() != this->gogo_->package_name())
   323  	{
   324  	  // Check whether the name is there but hidden.
   325  	  std::string s = ('.' + package->package_value()->pkgpath()
   326  			   + '.' + name);
   327  	  named_object = package->package_value()->lookup(s);
   328  	  if (named_object != NULL)
   329  	    {
   330  	      Package* p = package->package_value();
   331  	      const std::string& packname(p->package_name());
   332  	      go_error_at(location,
   333  			  "invalid reference to hidden type %<%s.%s%>",
   334  			  Gogo::message_name(packname).c_str(),
   335  			  Gogo::message_name(name).c_str());
   336  	      issue_error = false;
   337  	    }
   338  	}
   339      }
   340  
   341    bool ok = true;
   342    if (named_object == NULL)
   343      {
   344        if (package == NULL)
   345  	named_object = this->gogo_->add_unknown_name(name, location);
   346        else
   347  	{
   348  	  const std::string& packname(package->package_value()->package_name());
   349  	  go_error_at(location, "reference to undefined identifier %<%s.%s%>",
   350  		      Gogo::message_name(packname).c_str(),
   351  		      Gogo::message_name(name).c_str());
   352  	  issue_error = false;
   353  	  ok = false;
   354  	}
   355      }
   356    else if (named_object->is_type())
   357      {
   358        if (!named_object->type_value()->is_visible())
   359  	ok = false;
   360      }
   361    else if (named_object->is_unknown() || named_object->is_type_declaration())
   362      ;
   363    else
   364      ok = false;
   365  
   366    if (!ok)
   367      {
   368        if (issue_error)
   369  	go_error_at(location, "expected type");
   370        return Type::make_error_type();
   371      }
   372  
   373    if (named_object->is_type())
   374      return named_object->type_value();
   375    else if (named_object->is_unknown() || named_object->is_type_declaration())
   376      return Type::make_forward_declaration(named_object);
   377    else
   378      go_unreachable();
   379  }
   380  
   381  // ArrayType = "[" [ ArrayLength ] "]" ElementType .
   382  // ArrayLength = Expression .
   383  // ElementType = CompleteType .
   384  
   385  Type*
   386  Parse::array_type(bool may_use_ellipsis)
   387  {
   388    go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
   389    const Token* token = this->advance_token();
   390  
   391    Expression* length = NULL;
   392    if (token->is_op(OPERATOR_RSQUARE))
   393      this->advance_token();
   394    else
   395      {
   396        if (!token->is_op(OPERATOR_ELLIPSIS))
   397  	length = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
   398        else if (may_use_ellipsis)
   399  	{
   400  	  // An ellipsis is used in composite literals to represent a
   401  	  // fixed array of the size of the number of elements.  We
   402  	  // use a length of nil to represent this, and change the
   403  	  // length when parsing the composite literal.
   404  	  length = Expression::make_nil(this->location());
   405  	  this->advance_token();
   406  	}
   407        else
   408  	{
   409  	  go_error_at(this->location(),
   410  		      "use of %<[...]%> outside of array literal");
   411  	  length = Expression::make_error(this->location());
   412  	  this->advance_token();
   413  	}
   414        if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
   415  	{
   416  	  go_error_at(this->location(), "expected %<]%>");
   417  	  return Type::make_error_type();
   418  	}
   419        this->advance_token();
   420      }
   421  
   422    Type* element_type = this->type();
   423    if (element_type->is_error_type())
   424      return Type::make_error_type();
   425  
   426    return Type::make_array_type(element_type, length);
   427  }
   428  
   429  // MapType = "map" "[" KeyType "]" ValueType .
   430  // KeyType = CompleteType .
   431  // ValueType = CompleteType .
   432  
   433  Type*
   434  Parse::map_type()
   435  {
   436    Location location = this->location();
   437    go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
   438    if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
   439      {
   440        go_error_at(this->location(), "expected %<[%>");
   441        return Type::make_error_type();
   442      }
   443    this->advance_token();
   444  
   445    Type* key_type = this->type();
   446  
   447    if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
   448      {
   449        go_error_at(this->location(), "expected %<]%>");
   450        return Type::make_error_type();
   451      }
   452    this->advance_token();
   453  
   454    Type* value_type = this->type();
   455  
   456    if (key_type->is_error_type() || value_type->is_error_type())
   457      return Type::make_error_type();
   458  
   459    return Type::make_map_type(key_type, value_type, location);
   460  }
   461  
   462  // StructType     = "struct" "{" { FieldDecl ";" } "}" .
   463  
   464  Type*
   465  Parse::struct_type()
   466  {
   467    go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
   468    Location location = this->location();
   469    if (!this->advance_token()->is_op(OPERATOR_LCURLY))
   470      {
   471        Location token_loc = this->location();
   472        if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
   473  	  && this->advance_token()->is_op(OPERATOR_LCURLY))
   474  	go_error_at(token_loc, "unexpected semicolon or newline before %<{%>");
   475        else
   476  	{
   477  	  go_error_at(this->location(), "expected %<{%>");
   478  	  return Type::make_error_type();
   479  	}
   480      }
   481    this->advance_token();
   482  
   483    Struct_field_list* sfl = new Struct_field_list;
   484    while (!this->peek_token()->is_op(OPERATOR_RCURLY))
   485      {
   486        this->field_decl(sfl);
   487        if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
   488  	this->advance_token();
   489        else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
   490  	{
   491  	  go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
   492  	  if (!this->skip_past_error(OPERATOR_RCURLY))
   493  	    return Type::make_error_type();
   494  	}
   495      }
   496    this->advance_token();
   497  
   498    for (Struct_field_list::const_iterator pi = sfl->begin();
   499         pi != sfl->end();
   500         ++pi)
   501      {
   502        if (pi->type()->is_error_type())
   503  	return pi->type();
   504        for (Struct_field_list::const_iterator pj = pi + 1;
   505  	   pj != sfl->end();
   506  	   ++pj)
   507  	{
   508  	  if (pi->field_name() == pj->field_name()
   509  	      && !Gogo::is_sink_name(pi->field_name()))
   510  	    go_error_at(pi->location(), "duplicate field name %<%s%>",
   511  			Gogo::message_name(pi->field_name()).c_str());
   512  	}
   513      }
   514  
   515    return Type::make_struct_type(sfl, location);
   516  }
   517  
   518  // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
   519  // Tag = string_lit .
   520  
   521  void
   522  Parse::field_decl(Struct_field_list* sfl)
   523  {
   524    const Token* token = this->peek_token();
   525    Location location = token->location();
   526    bool is_anonymous;
   527    bool is_anonymous_pointer;
   528    if (token->is_op(OPERATOR_MULT))
   529      {
   530        is_anonymous = true;
   531        is_anonymous_pointer = true;
   532      }
   533    else if (token->is_identifier())
   534      {
   535        std::string id = token->identifier();
   536        bool is_id_exported = token->is_identifier_exported();
   537        Location id_location = token->location();
   538        token = this->advance_token();
   539        is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
   540  		      || token->is_op(OPERATOR_RCURLY)
   541  		      || token->is_op(OPERATOR_DOT)
   542  		      || token->is_string());
   543        is_anonymous_pointer = false;
   544        this->unget_token(Token::make_identifier_token(id, is_id_exported,
   545  						     id_location));
   546      }
   547    else
   548      {
   549        go_error_at(this->location(), "expected field name");
   550        this->gogo_->mark_locals_used();
   551        while (!token->is_op(OPERATOR_SEMICOLON)
   552  	     && !token->is_op(OPERATOR_RCURLY)
   553  	     && !token->is_eof())
   554  	token = this->advance_token();
   555        return;
   556      }
   557  
   558    if (is_anonymous)
   559      {
   560        if (is_anonymous_pointer)
   561  	{
   562  	  this->advance_token();
   563  	  if (!this->peek_token()->is_identifier())
   564  	    {
   565  	      go_error_at(this->location(), "expected field name");
   566  	      this->gogo_->mark_locals_used();
   567  	      while (!token->is_op(OPERATOR_SEMICOLON)
   568  		     && !token->is_op(OPERATOR_RCURLY)
   569  		     && !token->is_eof())
   570  		token = this->advance_token();
   571  	      return;
   572  	    }
   573  	}
   574        Type* type = this->type_name(true);
   575  
   576        std::string tag;
   577        if (this->peek_token()->is_string())
   578  	{
   579  	  tag = this->peek_token()->string_value();
   580  	  this->advance_token();
   581  	}
   582  
   583        if (!type->is_error_type())
   584  	{
   585  	  if (is_anonymous_pointer)
   586  	    type = Type::make_pointer_type(type);
   587  	  sfl->push_back(Struct_field(Typed_identifier("", type, location)));
   588  	  if (!tag.empty())
   589  	    sfl->back().set_tag(tag);
   590  	}
   591      }
   592    else
   593      {
   594        Typed_identifier_list til;
   595        while (true)
   596  	{
   597  	  token = this->peek_token();
   598  	  if (!token->is_identifier())
   599  	    {
   600  	      go_error_at(this->location(), "expected identifier");
   601  	      return;
   602  	    }
   603  	  std::string name =
   604  	    this->gogo_->pack_hidden_name(token->identifier(),
   605  					  token->is_identifier_exported());
   606  	  til.push_back(Typed_identifier(name, NULL, token->location()));
   607  	  if (!this->advance_token()->is_op(OPERATOR_COMMA))
   608  	    break;
   609  	  this->advance_token();
   610  	}
   611  
   612        Type* type = this->type();
   613  
   614        std::string tag;
   615        if (this->peek_token()->is_string())
   616  	{
   617  	  tag = this->peek_token()->string_value();
   618  	  this->advance_token();
   619  	}
   620  
   621        for (Typed_identifier_list::iterator p = til.begin();
   622  	   p != til.end();
   623  	   ++p)
   624  	{
   625  	  p->set_type(type);
   626  	  sfl->push_back(Struct_field(*p));
   627  	  if (!tag.empty())
   628  	    sfl->back().set_tag(tag);
   629  	}
   630      }
   631  }
   632  
   633  // PointerType = "*" Type .
   634  
   635  Type*
   636  Parse::pointer_type()
   637  {
   638    go_assert(this->peek_token()->is_op(OPERATOR_MULT));
   639    this->advance_token();
   640    Type* type = this->type();
   641    if (type->is_error_type())
   642      return type;
   643    return Type::make_pointer_type(type);
   644  }
   645  
   646  // ChannelType   = Channel | SendChannel | RecvChannel .
   647  // Channel       = "chan" ElementType .
   648  // SendChannel   = "chan" "<-" ElementType .
   649  // RecvChannel   = "<-" "chan" ElementType .
   650  
   651  Type*
   652  Parse::channel_type()
   653  {
   654    const Token* token = this->peek_token();
   655    bool send = true;
   656    bool receive = true;
   657    if (token->is_op(OPERATOR_CHANOP))
   658      {
   659        if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
   660  	{
   661  	  go_error_at(this->location(), "expected %<chan%>");
   662  	  return Type::make_error_type();
   663  	}
   664        send = false;
   665        this->advance_token();
   666      }
   667    else
   668      {
   669        go_assert(token->is_keyword(KEYWORD_CHAN));
   670        if (this->advance_token()->is_op(OPERATOR_CHANOP))
   671  	{
   672  	  receive = false;
   673  	  this->advance_token();
   674  	}
   675      }
   676  
   677    // Better error messages for the common error of omitting the
   678    // channel element type.
   679    if (!this->type_may_start_here())
   680      {
   681        token = this->peek_token();
   682        if (token->is_op(OPERATOR_RCURLY))
   683  	go_error_at(this->location(), "unexpected %<}%> in channel type");
   684        else if (token->is_op(OPERATOR_RPAREN))
   685  	go_error_at(this->location(), "unexpected %<)%> in channel type");
   686        else if (token->is_op(OPERATOR_COMMA))
   687  	go_error_at(this->location(), "unexpected comma in channel type");
   688        else
   689  	go_error_at(this->location(), "expected channel element type");
   690        return Type::make_error_type();
   691      }
   692  
   693    Type* element_type = this->type();
   694    return Type::make_channel_type(send, receive, element_type);
   695  }
   696  
   697  // Give an error for a duplicate parameter or receiver name.
   698  
   699  void
   700  Parse::check_signature_names(const Typed_identifier_list* params,
   701  			     Parse::Names* names)
   702  {
   703    for (Typed_identifier_list::const_iterator p = params->begin();
   704         p != params->end();
   705         ++p)
   706      {
   707        if (p->name().empty() || Gogo::is_sink_name(p->name()))
   708  	continue;
   709        std::pair<std::string, const Typed_identifier*> val =
   710  	std::make_pair(p->name(), &*p);
   711        std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
   712        if (!ins.second)
   713  	{
   714  	  go_error_at(p->location(), "redefinition of %qs",
   715  		      Gogo::message_name(p->name()).c_str());
   716  	  go_inform(ins.first->second->location(),
   717  		    "previous definition of %qs was here",
   718  		    Gogo::message_name(p->name()).c_str());
   719  	}
   720      }
   721  }
   722  
   723  // Signature      = Parameters [ Result ] .
   724  
   725  // RECEIVER is the receiver if there is one, or NULL.  LOCATION is the
   726  // location of the start of the type.
   727  
   728  // This returns NULL on a parse error.
   729  
   730  Function_type*
   731  Parse::signature(Typed_identifier* receiver, Location location)
   732  {
   733    bool is_varargs = false;
   734    Typed_identifier_list* params;
   735    bool params_ok = this->parameters(&params, &is_varargs);
   736  
   737    Typed_identifier_list* results = NULL;
   738    if (this->peek_token()->is_op(OPERATOR_LPAREN)
   739        || this->type_may_start_here())
   740      {
   741        if (!this->result(&results))
   742  	return NULL;
   743      }
   744  
   745    if (!params_ok)
   746      return NULL;
   747  
   748    Parse::Names names;
   749    if (receiver != NULL)
   750      names[receiver->name()] = receiver;
   751    if (params != NULL)
   752      this->check_signature_names(params, &names);
   753    if (results != NULL)
   754      this->check_signature_names(results, &names);
   755  
   756    Function_type* ret = Type::make_function_type(receiver, params, results,
   757  						location);
   758    if (is_varargs)
   759      ret->set_is_varargs();
   760    return ret;
   761  }
   762  
   763  // Parameters     = "(" [ ParameterList [ "," ] ] ")" .
   764  
   765  // This returns false on a parse error.
   766  
   767  bool
   768  Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
   769  {
   770    *pparams = NULL;
   771  
   772    if (!this->peek_token()->is_op(OPERATOR_LPAREN))
   773      {
   774        go_error_at(this->location(), "expected %<(%>");
   775        return false;
   776      }
   777  
   778    Typed_identifier_list* params = NULL;
   779    bool saw_error = false;
   780  
   781    const Token* token = this->advance_token();
   782    if (!token->is_op(OPERATOR_RPAREN))
   783      {
   784        params = this->parameter_list(is_varargs);
   785        if (params == NULL)
   786  	saw_error = true;
   787        token = this->peek_token();
   788      }
   789  
   790    // The optional trailing comma is picked up in parameter_list.
   791  
   792    if (!token->is_op(OPERATOR_RPAREN))
   793      {
   794        go_error_at(this->location(), "expected %<)%>");
   795        return false;
   796      }
   797    this->advance_token();
   798  
   799    if (saw_error)
   800      return false;
   801  
   802    *pparams = params;
   803    return true;
   804  }
   805  
   806  // ParameterList  = ParameterDecl { "," ParameterDecl } .
   807  
   808  // This sets *IS_VARARGS if the list ends with an ellipsis.
   809  // IS_VARARGS will be NULL if varargs are not permitted.
   810  
   811  // We pick up an optional trailing comma.
   812  
   813  // This returns NULL if some error is seen.
   814  
   815  Typed_identifier_list*
   816  Parse::parameter_list(bool* is_varargs)
   817  {
   818    Location location = this->location();
   819    Typed_identifier_list* ret = new Typed_identifier_list();
   820  
   821    bool saw_error = false;
   822  
   823    // If we see an identifier and then a comma, then we don't know
   824    // whether we are looking at a list of identifiers followed by a
   825    // type, or a list of types given by name.  We have to do an
   826    // arbitrary lookahead to figure it out.
   827  
   828    bool parameters_have_names;
   829    const Token* token = this->peek_token();
   830    if (!token->is_identifier())
   831      {
   832        // This must be a type which starts with something like '*'.
   833        parameters_have_names = false;
   834      }
   835    else
   836      {
   837        std::string name = token->identifier();
   838        bool is_exported = token->is_identifier_exported();
   839        Location id_location = token->location();
   840        token = this->advance_token();
   841        if (!token->is_op(OPERATOR_COMMA))
   842  	{
   843  	  if (token->is_op(OPERATOR_DOT))
   844  	    {
   845  	      // This is a qualified identifier, which must turn out
   846  	      // to be a type.
   847  	      parameters_have_names = false;
   848  	    }
   849  	  else if (token->is_op(OPERATOR_RPAREN))
   850  	    {
   851  	      // A single identifier followed by a parenthesis must be
   852  	      // a type name.
   853  	      parameters_have_names = false;
   854  	    }
   855  	  else
   856  	    {
   857  	      // An identifier followed by something other than a
   858  	      // comma or a dot or a right parenthesis must be a
   859  	      // parameter name followed by a type.
   860  	      parameters_have_names = true;
   861  	    }
   862  
   863  	  this->unget_token(Token::make_identifier_token(name, is_exported,
   864  							 id_location));
   865  	}
   866        else
   867  	{
   868  	  // An identifier followed by a comma may be the first in a
   869  	  // list of parameter names followed by a type, or it may be
   870  	  // the first in a list of types without parameter names.  To
   871  	  // find out we gather as many identifiers separated by
   872  	  // commas as we can.
   873  	  std::string id_name = this->gogo_->pack_hidden_name(name,
   874  							      is_exported);
   875  	  ret->push_back(Typed_identifier(id_name, NULL, id_location));
   876  	  bool just_saw_comma = true;
   877  	  while (this->advance_token()->is_identifier())
   878  	    {
   879  	      name = this->peek_token()->identifier();
   880  	      is_exported = this->peek_token()->is_identifier_exported();
   881  	      id_location = this->peek_token()->location();
   882  	      id_name = this->gogo_->pack_hidden_name(name, is_exported);
   883  	      ret->push_back(Typed_identifier(id_name, NULL, id_location));
   884  	      if (!this->advance_token()->is_op(OPERATOR_COMMA))
   885  		{
   886  		  just_saw_comma = false;
   887  		  break;
   888  		}
   889  	    }
   890  
   891  	  if (just_saw_comma)
   892  	    {
   893  	      // We saw ID1 "," ID2 "," followed by something which
   894  	      // was not an identifier.  We must be seeing the start
   895  	      // of a type, and ID1 and ID2 must be types, and the
   896  	      // parameters don't have names.
   897  	      parameters_have_names = false;
   898  	    }
   899  	  else if (this->peek_token()->is_op(OPERATOR_RPAREN))
   900  	    {
   901  	      // We saw ID1 "," ID2 ")".  ID1 and ID2 must be types,
   902  	      // and the parameters don't have names.
   903  	      parameters_have_names = false;
   904  	    }
   905  	  else if (this->peek_token()->is_op(OPERATOR_DOT))
   906  	    {
   907  	      // We saw ID1 "," ID2 ".".  ID2 must be a package name,
   908  	      // ID1 must be a type, and the parameters don't have
   909  	      // names.
   910  	      parameters_have_names = false;
   911  	      this->unget_token(Token::make_identifier_token(name, is_exported,
   912  							     id_location));
   913  	      ret->pop_back();
   914  	      just_saw_comma = true;
   915  	    }
   916  	  else
   917  	    {
   918  	      // We saw ID1 "," ID2 followed by something other than
   919  	      // ",", ".", or ")".  We must be looking at the start of
   920  	      // a type, and ID1 and ID2 must be parameter names.
   921  	      parameters_have_names = true;
   922  	    }
   923  
   924  	  if (parameters_have_names)
   925  	    {
   926  	      go_assert(!just_saw_comma);
   927  	      // We have just seen ID1, ID2 xxx.
   928  	      Type* type;
   929  	      if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
   930  		type = this->type();
   931  	      else
   932  		{
   933  		  go_error_at(this->location(),
   934  			      "%<...%> only permits one name");
   935  		  saw_error = true;
   936  		  this->advance_token();
   937  		  type = this->type();
   938  		}
   939  	      for (size_t i = 0; i < ret->size(); ++i)
   940  		ret->set_type(i, type);
   941  	      if (!this->peek_token()->is_op(OPERATOR_COMMA))
   942  		return saw_error ? NULL : ret;
   943  	      if (this->advance_token()->is_op(OPERATOR_RPAREN))
   944  		return saw_error ? NULL : ret;
   945  	    }
   946  	  else
   947  	    {
   948  	      Typed_identifier_list* tret = new Typed_identifier_list();
   949  	      for (Typed_identifier_list::const_iterator p = ret->begin();
   950  		   p != ret->end();
   951  		   ++p)
   952  		{
   953  		  Named_object* no = this->gogo_->lookup(p->name(), NULL);
   954  		  Type* type;
   955  		  if (no == NULL)
   956  		    no = this->gogo_->add_unknown_name(p->name(),
   957  						       p->location());
   958  
   959  		  if (no->is_type())
   960  		    type = no->type_value();
   961  		  else if (no->is_unknown() || no->is_type_declaration())
   962  		    type = Type::make_forward_declaration(no);
   963  		  else
   964  		    {
   965  		      go_error_at(p->location(), "expected %<%s%> to be a type",
   966  				  Gogo::message_name(p->name()).c_str());
   967  		      saw_error = true;
   968  		      type = Type::make_error_type();
   969  		    }
   970  		  tret->push_back(Typed_identifier("", type, p->location()));
   971  		}
   972  	      delete ret;
   973  	      ret = tret;
   974  	      if (!just_saw_comma
   975  		  || this->peek_token()->is_op(OPERATOR_RPAREN))
   976  		return saw_error ? NULL : ret;
   977  	    }
   978  	}
   979      }
   980  
   981    bool mix_error = false;
   982    this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error,
   983  		       &saw_error);
   984    while (this->peek_token()->is_op(OPERATOR_COMMA))
   985      {
   986        if (this->advance_token()->is_op(OPERATOR_RPAREN))
   987  	break;
   988        if (is_varargs != NULL && *is_varargs)
   989  	{
   990  	  go_error_at(this->location(), "%<...%> must be last parameter");
   991  	  saw_error = true;
   992  	}
   993        this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error,
   994  			   &saw_error);
   995      }
   996    if (mix_error)
   997      {
   998        go_error_at(location, "mixed named and unnamed function parameters");
   999        saw_error = true;
  1000      }
  1001    if (saw_error)
  1002      {
  1003        delete ret;
  1004        return NULL;
  1005      }
  1006    return ret;
  1007  }
  1008  
  1009  // ParameterDecl  = [ IdentifierList ] [ "..." ] Type .
  1010  
  1011  void
  1012  Parse::parameter_decl(bool parameters_have_names,
  1013  		      Typed_identifier_list* til,
  1014  		      bool* is_varargs,
  1015  		      bool* mix_error,
  1016  		      bool* saw_error)
  1017  {
  1018    if (!parameters_have_names)
  1019      {
  1020        Type* type;
  1021        Location location = this->location();
  1022        if (!this->peek_token()->is_identifier())
  1023  	{
  1024  	  if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
  1025  	    type = this->type();
  1026  	  else
  1027  	    {
  1028  	      if (is_varargs == NULL)
  1029  		go_error_at(this->location(), "invalid use of %<...%>");
  1030  	      else
  1031  		*is_varargs = true;
  1032  	      this->advance_token();
  1033  	      if (is_varargs == NULL
  1034  		  && this->peek_token()->is_op(OPERATOR_RPAREN))
  1035  		type = Type::make_error_type();
  1036  	      else
  1037  		{
  1038  		  Type* element_type = this->type();
  1039  		  type = Type::make_array_type(element_type, NULL);
  1040  		}
  1041  	    }
  1042  	}
  1043        else
  1044  	{
  1045  	  type = this->type_name(false);
  1046  	  if (type->is_error_type()
  1047  	      || (!this->peek_token()->is_op(OPERATOR_COMMA)
  1048  		  && !this->peek_token()->is_op(OPERATOR_RPAREN)))
  1049  	    {
  1050  	      *mix_error = true;
  1051  	      while (!this->peek_token()->is_op(OPERATOR_COMMA)
  1052  		     && !this->peek_token()->is_op(OPERATOR_RPAREN)
  1053                       && !this->peek_token()->is_eof())
  1054  		this->advance_token();
  1055  	    }
  1056  	}
  1057        if (!type->is_error_type())
  1058  	til->push_back(Typed_identifier("", type, location));
  1059        else
  1060  	*saw_error = true;
  1061      }
  1062    else
  1063      {
  1064        size_t orig_count = til->size();
  1065        if (this->peek_token()->is_identifier())
  1066  	this->identifier_list(til);
  1067        else
  1068  	*mix_error = true;
  1069        size_t new_count = til->size();
  1070  
  1071        Type* type;
  1072        if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
  1073  	type = this->type();
  1074        else
  1075  	{
  1076  	  if (is_varargs == NULL)
  1077  	    {
  1078  	      go_error_at(this->location(), "invalid use of %<...%>");
  1079  	      *saw_error = true;
  1080  	    }
  1081  	  else if (new_count > orig_count + 1)
  1082  	    {
  1083  	      go_error_at(this->location(), "%<...%> only permits one name");
  1084  	      *saw_error = true;
  1085  	    }
  1086  	  else
  1087  	    *is_varargs = true;
  1088  	  this->advance_token();
  1089  	  Type* element_type = this->type();
  1090  	  type = Type::make_array_type(element_type, NULL);
  1091  	}
  1092        for (size_t i = orig_count; i < new_count; ++i)
  1093  	til->set_type(i, type);
  1094      }
  1095  }
  1096  
  1097  // Result         = Parameters | Type .
  1098  
  1099  // This returns false on a parse error.
  1100  
  1101  bool
  1102  Parse::result(Typed_identifier_list** presults)
  1103  {
  1104    if (this->peek_token()->is_op(OPERATOR_LPAREN))
  1105      return this->parameters(presults, NULL);
  1106    else
  1107      {
  1108        Location location = this->location();
  1109        Type* type = this->type();
  1110        if (type->is_error_type())
  1111  	{
  1112  	  *presults = NULL;
  1113  	  return false;
  1114  	}
  1115        Typed_identifier_list* til = new Typed_identifier_list();
  1116        til->push_back(Typed_identifier("", type, location));
  1117        *presults = til;
  1118        return true;
  1119      }
  1120  }
  1121  
  1122  // Block = "{" [ StatementList ] "}" .
  1123  
  1124  // Returns the location of the closing brace.
  1125  
  1126  Location
  1127  Parse::block()
  1128  {
  1129    if (!this->peek_token()->is_op(OPERATOR_LCURLY))
  1130      {
  1131        Location loc = this->location();
  1132        if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
  1133  	  && this->advance_token()->is_op(OPERATOR_LCURLY))
  1134  	go_error_at(loc, "unexpected semicolon or newline before %<{%>");
  1135        else
  1136  	{
  1137  	  go_error_at(this->location(), "expected %<{%>");
  1138  	  return Linemap::unknown_location();
  1139  	}
  1140      }
  1141  
  1142    const Token* token = this->advance_token();
  1143  
  1144    if (!token->is_op(OPERATOR_RCURLY))
  1145      {
  1146        this->statement_list();
  1147        token = this->peek_token();
  1148        if (!token->is_op(OPERATOR_RCURLY))
  1149  	{
  1150  	  if (!token->is_eof() || !saw_errors())
  1151  	    go_error_at(this->location(), "expected %<}%>");
  1152  
  1153  	  this->gogo_->mark_locals_used();
  1154  
  1155  	  // Skip ahead to the end of the block, in hopes of avoiding
  1156  	  // lots of meaningless errors.
  1157  	  Location ret = token->location();
  1158  	  int nest = 0;
  1159  	  while (!token->is_eof())
  1160  	    {
  1161  	      if (token->is_op(OPERATOR_LCURLY))
  1162  		++nest;
  1163  	      else if (token->is_op(OPERATOR_RCURLY))
  1164  		{
  1165  		  --nest;
  1166  		  if (nest < 0)
  1167  		    {
  1168  		      this->advance_token();
  1169  		      break;
  1170  		    }
  1171  		}
  1172  	      token = this->advance_token();
  1173  	      ret = token->location();
  1174  	    }
  1175  	  return ret;
  1176  	}
  1177      }
  1178  
  1179    Location ret = token->location();
  1180    this->advance_token();
  1181    return ret;
  1182  }
  1183  
  1184  // InterfaceType      = "interface" "{" [ MethodSpecList ] "}" .
  1185  // MethodSpecList     = MethodSpec { ";" MethodSpec } [ ";" ] .
  1186  
  1187  Type*
  1188  Parse::interface_type(bool record)
  1189  {
  1190    go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
  1191    Location location = this->location();
  1192  
  1193    if (!this->advance_token()->is_op(OPERATOR_LCURLY))
  1194      {
  1195        Location token_loc = this->location();
  1196        if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
  1197  	  && this->advance_token()->is_op(OPERATOR_LCURLY))
  1198  	go_error_at(token_loc, "unexpected semicolon or newline before %<{%>");
  1199        else
  1200  	{
  1201  	  go_error_at(this->location(), "expected %<{%>");
  1202  	  return Type::make_error_type();
  1203  	}
  1204      }
  1205    this->advance_token();
  1206  
  1207    Typed_identifier_list* methods = new Typed_identifier_list();
  1208    if (!this->peek_token()->is_op(OPERATOR_RCURLY))
  1209      {
  1210        this->method_spec(methods);
  1211        while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  1212  	{
  1213  	  if (this->advance_token()->is_op(OPERATOR_RCURLY))
  1214  	    break;
  1215  	  this->method_spec(methods);
  1216  	}
  1217        if (!this->peek_token()->is_op(OPERATOR_RCURLY))
  1218  	{
  1219  	  go_error_at(this->location(), "expected %<}%>");
  1220  	  while (!this->advance_token()->is_op(OPERATOR_RCURLY))
  1221  	    {
  1222  	      if (this->peek_token()->is_eof())
  1223  		return Type::make_error_type();
  1224  	    }
  1225  	}
  1226      }
  1227    this->advance_token();
  1228  
  1229    if (methods->empty())
  1230      {
  1231        delete methods;
  1232        methods = NULL;
  1233      }
  1234  
  1235    Interface_type* ret;
  1236    if (methods == NULL)
  1237      ret = Type::make_empty_interface_type(location);
  1238    else
  1239      ret = Type::make_interface_type(methods, location);
  1240    if (record)
  1241      this->gogo_->record_interface_type(ret);
  1242    return ret;
  1243  }
  1244  
  1245  // MethodSpec         = MethodName Signature | InterfaceTypeName .
  1246  // MethodName         = identifier .
  1247  // InterfaceTypeName  = TypeName .
  1248  
  1249  void
  1250  Parse::method_spec(Typed_identifier_list* methods)
  1251  {
  1252    const Token* token = this->peek_token();
  1253    if (!token->is_identifier())
  1254      {
  1255        go_error_at(this->location(), "expected identifier");
  1256        return;
  1257      }
  1258  
  1259    std::string name = token->identifier();
  1260    bool is_exported = token->is_identifier_exported();
  1261    Location location = token->location();
  1262  
  1263    if (this->advance_token()->is_op(OPERATOR_LPAREN))
  1264      {
  1265        // This is a MethodName.
  1266        if (name == "_")
  1267  	go_error_at(this->location(),
  1268                      "methods must have a unique non-blank name");
  1269        name = this->gogo_->pack_hidden_name(name, is_exported);
  1270        Type* type = this->signature(NULL, location);
  1271        if (type == NULL)
  1272  	return;
  1273        methods->push_back(Typed_identifier(name, type, location));
  1274      }
  1275    else
  1276      {
  1277        this->unget_token(Token::make_identifier_token(name, is_exported,
  1278  						     location));
  1279        Type* type = this->type_name(false);
  1280        if (type->is_error_type()
  1281  	  || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
  1282  	      && !this->peek_token()->is_op(OPERATOR_RCURLY)))
  1283  	{
  1284  	  if (this->peek_token()->is_op(OPERATOR_COMMA))
  1285  	    go_error_at(this->location(),
  1286  			"name list not allowed in interface type");
  1287  	  else
  1288  	    go_error_at(location, "expected signature or type name");
  1289  	  this->gogo_->mark_locals_used();
  1290  	  token = this->peek_token();
  1291  	  while (!token->is_eof()
  1292  		 && !token->is_op(OPERATOR_SEMICOLON)
  1293  		 && !token->is_op(OPERATOR_RCURLY))
  1294  	    token = this->advance_token();
  1295  	  return;
  1296  	}
  1297        // This must be an interface type, but we can't check that now.
  1298        // We check it and pull out the methods in
  1299        // Interface_type::do_verify.
  1300        methods->push_back(Typed_identifier("", type, location));
  1301      }
  1302  }
  1303  
  1304  // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
  1305  
  1306  void
  1307  Parse::declaration()
  1308  {
  1309    const Token* token = this->peek_token();
  1310    if (token->is_keyword(KEYWORD_CONST))
  1311      this->const_decl();
  1312    else if (token->is_keyword(KEYWORD_TYPE))
  1313      this->type_decl();
  1314    else if (token->is_keyword(KEYWORD_VAR))
  1315      this->var_decl();
  1316    else if (token->is_keyword(KEYWORD_FUNC))
  1317      this->function_decl();
  1318    else
  1319      {
  1320        go_error_at(this->location(), "expected declaration");
  1321        this->advance_token();
  1322      }
  1323  }
  1324  
  1325  bool
  1326  Parse::declaration_may_start_here()
  1327  {
  1328    const Token* token = this->peek_token();
  1329    return (token->is_keyword(KEYWORD_CONST)
  1330  	  || token->is_keyword(KEYWORD_TYPE)
  1331  	  || token->is_keyword(KEYWORD_VAR)
  1332  	  || token->is_keyword(KEYWORD_FUNC));
  1333  }
  1334  
  1335  // Decl<P> = P | "(" [ List<P> ] ")" .
  1336  
  1337  void
  1338  Parse::decl(void (Parse::*pfn)())
  1339  {
  1340    if (this->peek_token()->is_eof())
  1341      {
  1342        if (!saw_errors())
  1343  	go_error_at(this->location(), "unexpected end of file");
  1344        return;
  1345      }
  1346  
  1347    if (!this->peek_token()->is_op(OPERATOR_LPAREN))
  1348      (this->*pfn)();
  1349    else
  1350      {
  1351        if (this->lex_->get_and_clear_pragmas() != 0)
  1352  	go_error_at(this->location(),
  1353  		    "ignoring compiler directive before group");
  1354        if (this->lex_->has_embeds())
  1355  	{
  1356  	  this->lex_->clear_embeds();
  1357  	  go_error_at(this->location(),
  1358  		      "ignoring %<//go:embed%> comment before group");
  1359  	}
  1360        if (!this->advance_token()->is_op(OPERATOR_RPAREN))
  1361  	{
  1362  	  this->list(pfn, true);
  1363  	  if (!this->peek_token()->is_op(OPERATOR_RPAREN))
  1364  	    {
  1365  	      go_error_at(this->location(), "missing %<)%>");
  1366  	      while (!this->advance_token()->is_op(OPERATOR_RPAREN))
  1367  		{
  1368  		  if (this->peek_token()->is_eof())
  1369  		    return;
  1370  		}
  1371  	    }
  1372  	}
  1373        this->advance_token();
  1374      }
  1375  }
  1376  
  1377  // List<P> = P { ";" P } [ ";" ] .
  1378  
  1379  // In order to pick up the trailing semicolon we need to know what
  1380  // might follow.  This is either a '}' or a ')'.
  1381  
  1382  void
  1383  Parse::list(void (Parse::*pfn)(), bool follow_is_paren)
  1384  {
  1385    (this->*pfn)();
  1386    Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
  1387    while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
  1388  	 || this->peek_token()->is_op(OPERATOR_COMMA))
  1389      {
  1390        if (this->peek_token()->is_op(OPERATOR_COMMA))
  1391  	go_error_at(this->location(), "unexpected comma");
  1392        if (this->advance_token()->is_op(follow))
  1393  	break;
  1394        (this->*pfn)();
  1395      }
  1396  }
  1397  
  1398  // ConstDecl      = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
  1399  
  1400  void
  1401  Parse::const_decl()
  1402  {
  1403    go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
  1404    this->advance_token();
  1405  
  1406    int iota = 0;
  1407    Type* last_type = NULL;
  1408    Expression_list* last_expr_list = NULL;
  1409  
  1410    if (!this->peek_token()->is_op(OPERATOR_LPAREN))
  1411      this->const_spec(iota, &last_type, &last_expr_list);
  1412    else
  1413      {
  1414        this->advance_token();
  1415        while (!this->peek_token()->is_op(OPERATOR_RPAREN))
  1416  	{
  1417  	  this->const_spec(iota, &last_type, &last_expr_list);
  1418  	  ++iota;
  1419  	  if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  1420  	    this->advance_token();
  1421  	  else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
  1422  	    {
  1423  	      go_error_at(this->location(),
  1424  			  "expected %<;%> or %<)%> or newline");
  1425  	      if (!this->skip_past_error(OPERATOR_RPAREN))
  1426  		return;
  1427  	    }
  1428  	}
  1429        this->advance_token();
  1430      }
  1431  
  1432    if (last_expr_list != NULL)
  1433      delete last_expr_list;
  1434  }
  1435  
  1436  // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
  1437  
  1438  void
  1439  Parse::const_spec(int iota, Type** last_type, Expression_list** last_expr_list)
  1440  {
  1441    this->check_directives();
  1442  
  1443    Location loc = this->location();
  1444    Typed_identifier_list til;
  1445    this->identifier_list(&til);
  1446  
  1447    Type* type = NULL;
  1448    if (this->type_may_start_here())
  1449      {
  1450        type = this->type();
  1451        *last_type = NULL;
  1452        *last_expr_list = NULL;
  1453      }
  1454  
  1455    Expression_list *expr_list;
  1456    if (!this->peek_token()->is_op(OPERATOR_EQ))
  1457      {
  1458        if (*last_expr_list == NULL)
  1459  	{
  1460  	  go_error_at(this->location(), "expected %<=%>");
  1461  	  return;
  1462  	}
  1463        type = *last_type;
  1464        expr_list = new Expression_list;
  1465        for (Expression_list::const_iterator p = (*last_expr_list)->begin();
  1466  	   p != (*last_expr_list)->end();
  1467  	   ++p)
  1468  	{
  1469  	  Expression* copy = (*p)->copy();
  1470  	  copy->set_location(loc);
  1471  	  this->update_references(&copy);
  1472  	  expr_list->push_back(copy);
  1473  	}
  1474      }
  1475    else
  1476      {
  1477        this->advance_token();
  1478        expr_list = this->expression_list(NULL, false, true);
  1479        *last_type = type;
  1480        if (*last_expr_list != NULL)
  1481  	delete *last_expr_list;
  1482        *last_expr_list = expr_list;
  1483      }
  1484  
  1485    Expression_list::const_iterator pe = expr_list->begin();
  1486    for (Typed_identifier_list::iterator pi = til.begin();
  1487         pi != til.end();
  1488         ++pi, ++pe)
  1489      {
  1490        if (pe == expr_list->end())
  1491  	{
  1492  	  go_error_at(this->location(), "not enough initializers");
  1493  	  return;
  1494  	}
  1495        if (type != NULL)
  1496  	pi->set_type(type);
  1497  
  1498        if (!Gogo::is_sink_name(pi->name()))
  1499  	this->gogo_->add_constant(*pi, *pe, iota);
  1500        else
  1501  	{
  1502  	  static int count;
  1503  	  char buf[30];
  1504  	  snprintf(buf, sizeof buf, ".$sinkconst%d", count);
  1505  	  ++count;
  1506  	  Typed_identifier ti(std::string(buf), type, pi->location());
  1507  	  Named_object* no = this->gogo_->add_constant(ti, *pe, iota);
  1508  	  no->const_value()->set_is_sink();
  1509  	}
  1510      }
  1511    if (pe != expr_list->end())
  1512      go_error_at(this->location(), "too many initializers");
  1513  
  1514    return;
  1515  }
  1516  
  1517  // Update any references to names to refer to the current names,
  1518  // for weird cases like
  1519  //
  1520  // const X = 1
  1521  // func F() {
  1522  // 	const (
  1523  // 		X = X + X
  1524  //		Y
  1525  // 	)
  1526  // }
  1527  //
  1528  // where the X + X for the first X is the outer X, but the X + X
  1529  // copied for Y is the inner X.
  1530  
  1531  class Update_references : public Traverse
  1532  {
  1533   public:
  1534    Update_references(Gogo* gogo)
  1535      : Traverse(traverse_expressions),
  1536        gogo_(gogo)
  1537    { }
  1538  
  1539    int
  1540    expression(Expression**);
  1541  
  1542   private:
  1543    Gogo* gogo_;
  1544  };
  1545  
  1546  int
  1547  Update_references::expression(Expression** pexpr)
  1548  {
  1549    Named_object* old_no;
  1550    switch ((*pexpr)->classification())
  1551      {
  1552      case Expression::EXPRESSION_CONST_REFERENCE:
  1553        old_no = (*pexpr)->const_expression()->named_object();
  1554        break;
  1555      case Expression::EXPRESSION_VAR_REFERENCE:
  1556        old_no = (*pexpr)->var_expression()->named_object();
  1557        break;
  1558      case Expression::EXPRESSION_ENCLOSED_VAR_REFERENCE:
  1559        old_no = (*pexpr)->enclosed_var_expression()->variable();
  1560        break;
  1561      case Expression::EXPRESSION_FUNC_REFERENCE:
  1562        old_no = (*pexpr)->func_expression()->named_object();
  1563        break;
  1564      case Expression::EXPRESSION_UNKNOWN_REFERENCE:
  1565        old_no = (*pexpr)->unknown_expression()->named_object();
  1566        break;
  1567      default:
  1568        return TRAVERSE_CONTINUE;
  1569      }
  1570  
  1571    if (old_no->package() != NULL)
  1572      {
  1573        // This is a qualified reference, so it can't have changed in
  1574        // scope.  FIXME: This probably doesn't handle dot imports
  1575        // correctly.
  1576        return TRAVERSE_CONTINUE;
  1577      }
  1578  
  1579    Named_object* in_function;
  1580    Named_object* new_no = this->gogo_->lookup(old_no->name(), &in_function);
  1581    if (new_no == old_no)
  1582      return TRAVERSE_CONTINUE;
  1583  
  1584    // The new name must be a constant, since that is all we have
  1585    // introduced into scope.
  1586    if (!new_no->is_const())
  1587      {
  1588        go_assert(saw_errors());
  1589        return TRAVERSE_CONTINUE;
  1590      }
  1591  
  1592    *pexpr = Expression::make_const_reference(new_no, (*pexpr)->location());
  1593  
  1594    return TRAVERSE_CONTINUE;
  1595  }
  1596  
  1597  void
  1598  Parse::update_references(Expression** pexpr)
  1599  {
  1600    Update_references ur(this->gogo_);
  1601    ur.expression(pexpr);
  1602    (*pexpr)->traverse_subexpressions(&ur);
  1603  }
  1604  
  1605  // TypeDecl = "type" Decl<TypeSpec> .
  1606  
  1607  void
  1608  Parse::type_decl()
  1609  {
  1610    go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
  1611    this->advance_token();
  1612    this->decl(&Parse::type_spec);
  1613  }
  1614  
  1615  // TypeSpec = identifier ["="] Type .
  1616  
  1617  void
  1618  Parse::type_spec()
  1619  {
  1620    unsigned int pragmas = this->lex_->get_and_clear_pragmas();
  1621    this->check_directives();
  1622  
  1623    const Token* token = this->peek_token();
  1624    if (!token->is_identifier())
  1625      {
  1626        go_error_at(this->location(), "expected identifier");
  1627        return;
  1628      }
  1629    std::string name = token->identifier();
  1630    bool is_exported = token->is_identifier_exported();
  1631    Location location = token->location();
  1632    token = this->advance_token();
  1633  
  1634    bool is_alias = false;
  1635    if (token->is_op(OPERATOR_EQ))
  1636      {
  1637        is_alias = true;
  1638        token = this->advance_token();
  1639      }
  1640  
  1641    // The scope of the type name starts at the point where the
  1642    // identifier appears in the source code.  We implement this by
  1643    // declaring the type before we read the type definition.
  1644    Named_object* named_type = NULL;
  1645    if (name != "_")
  1646      {
  1647        name = this->gogo_->pack_hidden_name(name, is_exported);
  1648        named_type = this->gogo_->declare_type(name, location);
  1649      }
  1650  
  1651    Type* type;
  1652    if (name == "_" && token->is_keyword(KEYWORD_INTERFACE))
  1653      {
  1654        // We call Parse::interface_type explicity here because we do not want
  1655        // to record an interface with a blank type name.
  1656        type = this->interface_type(false);
  1657      }
  1658    else if (!token->is_op(OPERATOR_SEMICOLON))
  1659      type = this->type();
  1660    else
  1661      {
  1662        go_error_at(this->location(),
  1663  		  "unexpected semicolon or newline in type declaration");
  1664        type = Type::make_error_type();
  1665      }
  1666  
  1667    if (type->is_error_type())
  1668      {
  1669        this->gogo_->mark_locals_used();
  1670        while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
  1671  	     && !this->peek_token()->is_eof())
  1672  	this->advance_token();
  1673      }
  1674  
  1675    if (name != "_")
  1676      {
  1677        if (named_type->is_type_declaration())
  1678  	{
  1679  	  Type* ftype = type->forwarded();
  1680  	  if (ftype->forward_declaration_type() != NULL
  1681  	      && (ftype->forward_declaration_type()->named_object()
  1682  		  == named_type))
  1683  	    {
  1684  	      go_error_at(location, "invalid recursive type");
  1685  	      type = Type::make_error_type();
  1686  	    }
  1687  
  1688  	  Named_type* nt = Type::make_named_type(named_type, type, location);
  1689  	  if (is_alias)
  1690  	    nt->set_is_alias();
  1691  
  1692  	  this->gogo_->define_type(named_type, nt);
  1693  	  go_assert(named_type->package() == NULL);
  1694  
  1695  	  if ((pragmas & GOPRAGMA_NOTINHEAP) != 0)
  1696  	    {
  1697  	      nt->set_not_in_heap();
  1698  	      pragmas &= ~GOPRAGMA_NOTINHEAP;
  1699  	    }
  1700  	  if (pragmas != 0)
  1701  	    go_warning_at(location, 0,
  1702  			  "ignoring magic %<//go:...%> comment before type");
  1703  	}
  1704        else
  1705  	{
  1706  	  // This will probably give a redefinition error.
  1707  	  this->gogo_->add_type(name, type, location);
  1708  	}
  1709      }
  1710  }
  1711  
  1712  // VarDecl = "var" Decl<VarSpec> .
  1713  
  1714  void
  1715  Parse::var_decl()
  1716  {
  1717    go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
  1718    this->advance_token();
  1719    this->decl(&Parse::var_spec);
  1720  }
  1721  
  1722  // VarSpec = IdentifierList
  1723  //             ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
  1724  
  1725  void
  1726  Parse::var_spec()
  1727  {
  1728    Location loc = this->location();
  1729  
  1730    std::vector<std::string>* embeds = NULL;
  1731    if (this->lex_->has_embeds())
  1732      {
  1733        if (!this->gogo_->current_file_imported_embed())
  1734  	go_error_at(loc, "invalid go:embed: missing import %<embed%>");
  1735        else
  1736  	{
  1737  	  embeds = new(std::vector<std::string>);
  1738  	  this->lex_->get_and_clear_embeds(embeds);
  1739  	}
  1740      }
  1741  
  1742    this->check_directives();
  1743  
  1744    // Get the variable names.
  1745    Typed_identifier_list til;
  1746    this->identifier_list(&til);
  1747  
  1748    if (embeds != NULL)
  1749      {
  1750        if (!this->gogo_->in_global_scope())
  1751  	{
  1752  	  go_error_at(loc, "go:embed only permitted at package scope");
  1753  	  embeds = NULL;
  1754  	}
  1755        if (til.size() > 1)
  1756  	{
  1757  	  go_error_at(loc, "go:embed cannot apply to multiple vars");
  1758  	  embeds = NULL;
  1759  	}
  1760      }
  1761  
  1762    Location location = this->location();
  1763  
  1764    Type* type = NULL;
  1765    Expression_list* init = NULL;
  1766    if (!this->peek_token()->is_op(OPERATOR_EQ))
  1767      {
  1768        type = this->type();
  1769        if (type->is_error_type())
  1770  	{
  1771  	  this->gogo_->mark_locals_used();
  1772  	  while (!this->peek_token()->is_op(OPERATOR_EQ)
  1773  		 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
  1774  		 && !this->peek_token()->is_eof())
  1775  	    this->advance_token();
  1776  	}
  1777        if (this->peek_token()->is_op(OPERATOR_EQ))
  1778  	{
  1779  	  this->advance_token();
  1780  	  init = this->expression_list(NULL, false, true);
  1781  	}
  1782      }
  1783    else
  1784      {
  1785        this->advance_token();
  1786        init = this->expression_list(NULL, false, true);
  1787      }
  1788  
  1789    if (embeds != NULL && init != NULL)
  1790      {
  1791        go_error_at(loc, "go:embed cannot apply to var with initializer");
  1792        embeds = NULL;
  1793      }
  1794  
  1795    this->init_vars(&til, type, init, false, embeds, location);
  1796  
  1797    if (init != NULL)
  1798      delete init;
  1799  }
  1800  
  1801  // Create variables.  TIL is a list of variable names.  If TYPE is not
  1802  // NULL, it is the type of all the variables.  If INIT is not NULL, it
  1803  // is an initializer list for the variables.
  1804  
  1805  void
  1806  Parse::init_vars(const Typed_identifier_list* til, Type* type,
  1807  		 Expression_list* init, bool is_coloneq,
  1808  		 std::vector<std::string>* embeds, Location location)
  1809  {
  1810    // Check for an initialization which can yield multiple values.
  1811    if (init != NULL && init->size() == 1 && til->size() > 1)
  1812      {
  1813        go_assert(embeds == NULL);
  1814        if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
  1815  				    location))
  1816  	return;
  1817        if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
  1818  				   location))
  1819  	return;
  1820        if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
  1821  				       location))
  1822  	return;
  1823        if (this->init_vars_from_type_guard(til, type, *init->begin(),
  1824  					  is_coloneq, location))
  1825  	return;
  1826      }
  1827  
  1828    if (init != NULL && init->size() != til->size())
  1829      {
  1830        if (init->empty() || !init->front()->is_error_expression())
  1831  	go_error_at(location, "wrong number of initializations");
  1832        init = NULL;
  1833        if (type == NULL)
  1834  	type = Type::make_error_type();
  1835      }
  1836  
  1837    // Note that INIT was already parsed with the old name bindings, so
  1838    // we don't have to worry that it will accidentally refer to the
  1839    // newly declared variables.  But we do have to worry about a mix of
  1840    // newly declared variables and old variables if the old variables
  1841    // appear in the initializations.
  1842  
  1843    Expression_list::const_iterator pexpr;
  1844    if (init != NULL)
  1845      pexpr = init->begin();
  1846    bool any_new = false;
  1847    Expression_list* vars = new Expression_list();
  1848    Expression_list* vals = new Expression_list();
  1849    for (Typed_identifier_list::const_iterator p = til->begin();
  1850         p != til->end();
  1851         ++p)
  1852      {
  1853        if (init != NULL)
  1854  	go_assert(pexpr != init->end());
  1855        Named_object* no = this->init_var(*p, type,
  1856  					init == NULL ? NULL : *pexpr,
  1857  					is_coloneq, false, &any_new,
  1858  					vars, vals);
  1859        if (embeds != NULL && no->is_variable())
  1860  	no->var_value()->set_embeds(embeds);
  1861        if (init != NULL)
  1862  	++pexpr;
  1863      }
  1864    if (init != NULL)
  1865      go_assert(pexpr == init->end());
  1866    if (is_coloneq && !any_new)
  1867      go_error_at(location, "variables redeclared but no variable is new");
  1868    this->finish_init_vars(vars, vals, location);
  1869  }
  1870  
  1871  // See if we need to initialize a list of variables from a function
  1872  // call.  This returns true if we have set up the variables and the
  1873  // initialization.
  1874  
  1875  bool
  1876  Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
  1877  			   Expression* expr, bool is_coloneq,
  1878  			   Location location)
  1879  {
  1880    Call_expression* call = expr->call_expression();
  1881    if (call == NULL)
  1882      return false;
  1883  
  1884    // This is a function call.  We can't check here whether it returns
  1885    // the right number of values, but it might.  Declare the variables,
  1886    // and then assign the results of the call to them.
  1887  
  1888    call->set_expected_result_count(vars->size());
  1889  
  1890    Named_object* first_var = NULL;
  1891    unsigned int index = 0;
  1892    bool any_new = false;
  1893    Expression_list* ivars = new Expression_list();
  1894    Expression_list* ivals = new Expression_list();
  1895    for (Typed_identifier_list::const_iterator pv = vars->begin();
  1896         pv != vars->end();
  1897         ++pv, ++index)
  1898      {
  1899        Expression* init = Expression::make_call_result(call, index);
  1900        Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
  1901  					&any_new, ivars, ivals);
  1902  
  1903        if (this->gogo_->in_global_scope() && no->is_variable())
  1904  	{
  1905  	  if (first_var == NULL)
  1906  	    first_var = no;
  1907  	  else
  1908  	    {
  1909                // If the current object is a redefinition of another object, we
  1910                // might have already recorded the dependency relationship between
  1911                // it and the first variable.  Either way, an error will be
  1912                // reported for the redefinition and we don't need to properly
  1913                // record dependency information for an invalid program.
  1914                if (no->is_redefinition())
  1915                  continue;
  1916  
  1917  	      // The subsequent vars have an implicit dependency on
  1918  	      // the first one, so that everything gets initialized in
  1919  	      // the right order and so that we detect cycles
  1920  	      // correctly.
  1921  	      this->gogo_->record_var_depends_on(no->var_value(), first_var);
  1922  	    }
  1923  	}
  1924      }
  1925  
  1926    if (is_coloneq && !any_new)
  1927      go_error_at(location, "variables redeclared but no variable is new");
  1928  
  1929    this->finish_init_vars(ivars, ivals, location);
  1930  
  1931    return true;
  1932  }
  1933  
  1934  // See if we need to initialize a pair of values from a map index
  1935  // expression.  This returns true if we have set up the variables and
  1936  // the initialization.
  1937  
  1938  bool
  1939  Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
  1940  			  Expression* expr, bool is_coloneq,
  1941  			  Location location)
  1942  {
  1943    Index_expression* index = expr->index_expression();
  1944    if (index == NULL)
  1945      return false;
  1946    if (vars->size() != 2)
  1947      return false;
  1948  
  1949    // This is an index which is being assigned to two variables.  It
  1950    // must be a map index.  Declare the variables, and then assign the
  1951    // results of the map index.
  1952    bool any_new = false;
  1953    Typed_identifier_list::const_iterator p = vars->begin();
  1954    Expression* init = type == NULL ? index : NULL;
  1955    Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
  1956  					type == NULL, &any_new, NULL, NULL);
  1957    if (type == NULL && any_new && val_no->is_variable())
  1958      val_no->var_value()->set_type_from_init_tuple();
  1959    Expression* val_var = Expression::make_var_reference(val_no, location);
  1960  
  1961    ++p;
  1962    Type* var_type = type;
  1963    if (var_type == NULL)
  1964      var_type = Type::lookup_bool_type();
  1965    Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
  1966  				    &any_new, NULL, NULL);
  1967    Expression* present_var = Expression::make_var_reference(no, location);
  1968  
  1969    if (is_coloneq && !any_new)
  1970      go_error_at(location, "variables redeclared but no variable is new");
  1971  
  1972    Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
  1973  						      index, location);
  1974  
  1975    if (!this->gogo_->in_global_scope())
  1976      this->gogo_->add_statement(s);
  1977    else if (!val_no->is_sink())
  1978      {
  1979        if (val_no->is_variable())
  1980  	{
  1981  	  val_no->var_value()->add_preinit_statement(this->gogo_, s);
  1982  	  if (no->is_variable())
  1983  	    this->gogo_->record_var_depends_on(no->var_value(), val_no);
  1984  	}
  1985      }
  1986    else if (!no->is_sink())
  1987      {
  1988        if (no->is_variable())
  1989  	no->var_value()->add_preinit_statement(this->gogo_, s);
  1990      }
  1991    else
  1992      {
  1993        // Execute the map index expression just so that we can fail if
  1994        // the map is nil.
  1995        Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
  1996  						      NULL, location);
  1997        dummy->var_value()->add_preinit_statement(this->gogo_, s);
  1998      }
  1999  
  2000    return true;
  2001  }
  2002  
  2003  // See if we need to initialize a pair of values from a receive
  2004  // expression.  This returns true if we have set up the variables and
  2005  // the initialization.
  2006  
  2007  bool
  2008  Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
  2009  			      Expression* expr, bool is_coloneq,
  2010  			      Location location)
  2011  {
  2012    Receive_expression* receive = expr->receive_expression();
  2013    if (receive == NULL)
  2014      return false;
  2015    if (vars->size() != 2)
  2016      return false;
  2017  
  2018    // This is a receive expression which is being assigned to two
  2019    // variables.  Declare the variables, and then assign the results of
  2020    // the receive.
  2021    bool any_new = false;
  2022    Typed_identifier_list::const_iterator p = vars->begin();
  2023    Expression* init = type == NULL ? receive : NULL;
  2024    Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
  2025  					type == NULL, &any_new, NULL, NULL);
  2026    if (type == NULL && any_new && val_no->is_variable())
  2027      val_no->var_value()->set_type_from_init_tuple();
  2028    Expression* val_var = Expression::make_var_reference(val_no, location);
  2029  
  2030    ++p;
  2031    Type* var_type = type;
  2032    if (var_type == NULL)
  2033      var_type = Type::lookup_bool_type();
  2034    Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
  2035  				    &any_new, NULL, NULL);
  2036    Expression* received_var = Expression::make_var_reference(no, location);
  2037  
  2038    if (is_coloneq && !any_new)
  2039      go_error_at(location, "variables redeclared but no variable is new");
  2040  
  2041    Statement* s = Statement::make_tuple_receive_assignment(val_var,
  2042  							  received_var,
  2043  							  receive->channel(),
  2044  							  location);
  2045  
  2046    if (!this->gogo_->in_global_scope())
  2047      this->gogo_->add_statement(s);
  2048    else if (!val_no->is_sink())
  2049      {
  2050        if (val_no->is_variable())
  2051  	{
  2052  	  val_no->var_value()->add_preinit_statement(this->gogo_, s);
  2053  	  if (no->is_variable())
  2054  	    this->gogo_->record_var_depends_on(no->var_value(), val_no);
  2055  	}
  2056      }
  2057    else if (!no->is_sink())
  2058      {
  2059        if (no->is_variable())
  2060  	no->var_value()->add_preinit_statement(this->gogo_, s);
  2061      }
  2062    else
  2063      {
  2064        Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
  2065  						      NULL, location);
  2066        dummy->var_value()->add_preinit_statement(this->gogo_, s);
  2067      }
  2068  
  2069    return true;
  2070  }
  2071  
  2072  // See if we need to initialize a pair of values from a type guard
  2073  // expression.  This returns true if we have set up the variables and
  2074  // the initialization.
  2075  
  2076  bool
  2077  Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
  2078  				 Type* type, Expression* expr,
  2079  				 bool is_coloneq, Location location)
  2080  {
  2081    Type_guard_expression* type_guard = expr->type_guard_expression();
  2082    if (type_guard == NULL)
  2083      return false;
  2084    if (vars->size() != 2)
  2085      return false;
  2086  
  2087    // This is a type guard expression which is being assigned to two
  2088    // variables.  Declare the variables, and then assign the results of
  2089    // the type guard.
  2090    bool any_new = false;
  2091    Typed_identifier_list::const_iterator p = vars->begin();
  2092    Type* var_type = type;
  2093    if (var_type == NULL)
  2094      var_type = type_guard->type();
  2095    Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
  2096  					&any_new, NULL, NULL);
  2097    Expression* val_var = Expression::make_var_reference(val_no, location);
  2098  
  2099    ++p;
  2100    var_type = type;
  2101    if (var_type == NULL)
  2102      var_type = Type::lookup_bool_type();
  2103    Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
  2104  				    &any_new, NULL, NULL);
  2105    Expression* ok_var = Expression::make_var_reference(no, location);
  2106  
  2107    Expression* texpr = type_guard->expr();
  2108    Type* t = type_guard->type();
  2109    Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
  2110  							     texpr, t,
  2111  							     location);
  2112  
  2113    if (is_coloneq && !any_new)
  2114      go_error_at(location, "variables redeclared but no variable is new");
  2115  
  2116    if (!this->gogo_->in_global_scope())
  2117      this->gogo_->add_statement(s);
  2118    else if (!val_no->is_sink())
  2119      {
  2120        if (val_no->is_variable())
  2121  	{
  2122  	  val_no->var_value()->add_preinit_statement(this->gogo_, s);
  2123  	  if (no->is_variable())
  2124  	    this->gogo_->record_var_depends_on(no->var_value(), val_no);
  2125  	}
  2126      }
  2127    else if (!no->is_sink())
  2128      {
  2129        if (no->is_variable())
  2130  	no->var_value()->add_preinit_statement(this->gogo_, s);
  2131      }
  2132    else
  2133      {
  2134        Named_object* dummy = this->create_dummy_global(type, NULL, location);
  2135        dummy->var_value()->add_preinit_statement(this->gogo_, s);
  2136      }
  2137  
  2138    return true;
  2139  }
  2140  
  2141  // Create a single variable.  If IS_COLONEQ is true, we permit
  2142  // redeclarations in the same block, and we set *IS_NEW when we find a
  2143  // new variable which is not a redeclaration.
  2144  
  2145  Named_object*
  2146  Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
  2147  		bool is_coloneq, bool type_from_init, bool* is_new,
  2148  		Expression_list* vars, Expression_list* vals)
  2149  {
  2150    Location location = tid.location();
  2151  
  2152    if (Gogo::is_sink_name(tid.name()))
  2153      {
  2154        if (!type_from_init && init != NULL)
  2155  	{
  2156  	  if (this->gogo_->in_global_scope())
  2157  	    return this->create_dummy_global(type, init, location);
  2158  	  else
  2159  	    {
  2160  	      // Create a dummy variable so that we will check whether the
  2161  	      // initializer can be assigned to the type.
  2162  	      Variable* var = new Variable(type, init, false, false, false,
  2163  					   location);
  2164  	      var->set_is_used();
  2165  	      static int count;
  2166  	      char buf[30];
  2167  	      snprintf(buf, sizeof buf, "sink$%d", count);
  2168  	      ++count;
  2169  	      return this->gogo_->add_variable(buf, var);
  2170  	    }
  2171  	}
  2172        if (type != NULL)
  2173  	this->gogo_->add_type_to_verify(type);
  2174        return this->gogo_->add_sink();
  2175      }
  2176  
  2177    if (is_coloneq)
  2178      {
  2179        Named_object* no = this->gogo_->lookup_in_block(tid.name());
  2180        if (no != NULL
  2181  	  && (no->is_variable() || no->is_result_variable()))
  2182  	{
  2183  	  // INIT may be NULL even when IS_COLONEQ is true for cases
  2184  	  // like v, ok := x.(int).
  2185  	  if (!type_from_init && init != NULL)
  2186  	    {
  2187  	      go_assert(vars != NULL && vals != NULL);
  2188  	      vars->push_back(Expression::make_var_reference(no, location));
  2189  	      vals->push_back(init);
  2190  	    }
  2191  	  return no;
  2192  	}
  2193      }
  2194    *is_new = true;
  2195    Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
  2196  			       false, false, location);
  2197    Named_object* no = this->gogo_->add_variable(tid.name(), var);
  2198    if (!no->is_variable())
  2199      {
  2200        // The name is already defined, so we just gave an error.
  2201        return this->gogo_->add_sink();
  2202      }
  2203    return no;
  2204  }
  2205  
  2206  // Create a dummy global variable to force an initializer to be run in
  2207  // the right place.  This is used when a sink variable is initialized
  2208  // at global scope.
  2209  
  2210  Named_object*
  2211  Parse::create_dummy_global(Type* type, Expression* init,
  2212  			   Location location)
  2213  {
  2214    if (type == NULL && init == NULL)
  2215      type = Type::lookup_bool_type();
  2216    Variable* var = new Variable(type, init, true, false, false, location);
  2217    var->set_is_global_sink();
  2218    static int count;
  2219    char buf[30];
  2220    snprintf(buf, sizeof buf, "_.%d", count);
  2221    ++count;
  2222    return this->gogo_->add_variable(buf, var);
  2223  }
  2224  
  2225  // Finish the variable initialization by executing any assignments to
  2226  // existing variables when using :=.  These must be done as a tuple
  2227  // assignment in case of something like n, a, b := 1, b, a.
  2228  
  2229  void
  2230  Parse::finish_init_vars(Expression_list* vars, Expression_list* vals,
  2231  			Location location)
  2232  {
  2233    if (vars->empty())
  2234      {
  2235        delete vars;
  2236        delete vals;
  2237      }
  2238    else if (vars->size() == 1)
  2239      {
  2240        go_assert(!this->gogo_->in_global_scope());
  2241        this->gogo_->add_statement(Statement::make_assignment(vars->front(),
  2242  							    vals->front(),
  2243  							    location));
  2244        delete vars;
  2245        delete vals;
  2246      }
  2247    else
  2248      {
  2249        go_assert(!this->gogo_->in_global_scope());
  2250        this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals,
  2251  								  location));
  2252      }
  2253  }
  2254  
  2255  // SimpleVarDecl = identifier ":=" Expression .
  2256  
  2257  // We've already seen the identifier.
  2258  
  2259  // FIXME: We also have to implement
  2260  //  IdentifierList ":=" ExpressionList
  2261  // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
  2262  // tuple assignments here as well.
  2263  
  2264  // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
  2265  // side may be a composite literal.
  2266  
  2267  // If P_RANGE_CLAUSE is not NULL, then this will recognize a
  2268  // RangeClause.
  2269  
  2270  // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
  2271  // guard (var := expr.("type") using the literal keyword "type").
  2272  
  2273  void
  2274  Parse::simple_var_decl_or_assignment(const std::string& name,
  2275  				     Location location,
  2276  				     bool may_be_composite_lit,
  2277  				     Range_clause* p_range_clause,
  2278  				     Type_switch* p_type_switch)
  2279  {
  2280    Typed_identifier_list til;
  2281    til.push_back(Typed_identifier(name, NULL, location));
  2282  
  2283    std::set<std::string> uniq_idents;
  2284    uniq_idents.insert(name);
  2285    std::string dup_name;
  2286    Location dup_loc;
  2287  
  2288    // We've seen one identifier.  If we see a comma now, this could be
  2289    // "a, *p = 1, 2".
  2290    if (this->peek_token()->is_op(OPERATOR_COMMA))
  2291      {
  2292        go_assert(p_type_switch == NULL);
  2293        while (true)
  2294  	{
  2295  	  const Token* token = this->advance_token();
  2296  	  if (!token->is_identifier())
  2297  	    break;
  2298  
  2299  	  std::string id = token->identifier();
  2300  	  bool is_id_exported = token->is_identifier_exported();
  2301  	  Location id_location = token->location();
  2302  	  std::pair<std::set<std::string>::iterator, bool> ins;
  2303  
  2304  	  token = this->advance_token();
  2305  	  if (!token->is_op(OPERATOR_COMMA))
  2306  	    {
  2307  	      if (token->is_op(OPERATOR_COLONEQ))
  2308  		{
  2309  		  id = this->gogo_->pack_hidden_name(id, is_id_exported);
  2310  		  ins = uniq_idents.insert(id);
  2311  		  if (!ins.second && !Gogo::is_sink_name(id))
  2312  		    {
  2313  		      // Use %s to print := to avoid -Wformat-diag warning.
  2314  		      go_error_at(id_location,
  2315  				  "%qs repeated on left side of %s",
  2316  				  Gogo::message_name(id).c_str(), ":=");
  2317  		    }
  2318  		  til.push_back(Typed_identifier(id, NULL, location));
  2319  		}
  2320  	      else
  2321  		this->unget_token(Token::make_identifier_token(id,
  2322  							       is_id_exported,
  2323  							       id_location));
  2324  	      break;
  2325  	    }
  2326  
  2327  	  id = this->gogo_->pack_hidden_name(id, is_id_exported);
  2328  	  ins = uniq_idents.insert(id);
  2329  	  if (!ins.second && !Gogo::is_sink_name(id))
  2330  	    {
  2331  	      dup_name = Gogo::message_name(id);
  2332  	      dup_loc = id_location;
  2333  	    }
  2334  	  til.push_back(Typed_identifier(id, NULL, location));
  2335  	}
  2336  
  2337        // We have a comma separated list of identifiers in TIL.  If the
  2338        // next token is COLONEQ, then this is a simple var decl, and we
  2339        // have the complete list of identifiers.  If the next token is
  2340        // not COLONEQ, then the only valid parse is a tuple assignment.
  2341        // The list of identifiers we have so far is really a list of
  2342        // expressions.  There are more expressions following.
  2343  
  2344        if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
  2345  	{
  2346  	  Expression_list* exprs = new Expression_list;
  2347  	  for (Typed_identifier_list::const_iterator p = til.begin();
  2348  	       p != til.end();
  2349  	       ++p)
  2350  	    exprs->push_back(this->id_to_expression(p->name(), p->location(),
  2351  						    true, false));
  2352  
  2353  	  Expression_list* more_exprs =
  2354  	    this->expression_list(NULL, true, may_be_composite_lit);
  2355  	  for (Expression_list::const_iterator p = more_exprs->begin();
  2356  	       p != more_exprs->end();
  2357  	       ++p)
  2358  	    exprs->push_back(*p);
  2359  	  delete more_exprs;
  2360  
  2361  	  this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
  2362  	  return;
  2363  	}
  2364      }
  2365  
  2366    go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
  2367    const Token* token = this->advance_token();
  2368  
  2369    if (!dup_name.empty())
  2370      {
  2371        // Use %s to print := to avoid -Wformat-diag warning.
  2372        go_error_at(dup_loc, "%qs repeated on left side of %s",
  2373  		  dup_name.c_str(), ":=");
  2374      }
  2375  
  2376    if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
  2377      {
  2378        this->range_clause_decl(&til, p_range_clause);
  2379        return;
  2380      }
  2381  
  2382    Expression_list* init;
  2383    if (p_type_switch == NULL)
  2384      init = this->expression_list(NULL, false, may_be_composite_lit);
  2385    else
  2386      {
  2387        bool is_type_switch = false;
  2388        Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
  2389  					  may_be_composite_lit,
  2390  					  &is_type_switch, NULL);
  2391        if (is_type_switch)
  2392  	{
  2393  	  p_type_switch->found = true;
  2394  	  p_type_switch->name = name;
  2395  	  p_type_switch->location = location;
  2396  	  p_type_switch->expr = expr;
  2397  	  return;
  2398  	}
  2399  
  2400        if (!this->peek_token()->is_op(OPERATOR_COMMA))
  2401  	{
  2402  	  init = new Expression_list();
  2403  	  init->push_back(expr);
  2404  	}
  2405        else
  2406  	{
  2407  	  this->advance_token();
  2408  	  init = this->expression_list(expr, false, may_be_composite_lit);
  2409  	}
  2410      }
  2411  
  2412    this->init_vars(&til, NULL, init, true, NULL, location);
  2413  }
  2414  
  2415  // FunctionDecl = "func" identifier Signature [ Block ] .
  2416  // MethodDecl = "func" Receiver identifier Signature [ Block ] .
  2417  
  2418  // Deprecated gcc extension:
  2419  //   FunctionDecl = "func" identifier Signature
  2420  //                    __asm__ "(" string_lit ")" .
  2421  // This extension means a function whose real name is the identifier
  2422  // inside the asm.  This extension will be removed at some future
  2423  // date.  It has been replaced with //extern or //go:linkname comments.
  2424  //
  2425  // PRAGMAS is a bitset of magic comments.
  2426  
  2427  void
  2428  Parse::function_decl()
  2429  {
  2430    go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
  2431  
  2432    unsigned int pragmas = this->lex_->get_and_clear_pragmas();
  2433    this->check_directives();
  2434  
  2435    Location location = this->location();
  2436    std::string extern_name = this->lex_->extern_name();
  2437    const Token* token = this->advance_token();
  2438  
  2439    bool expected_receiver = false;
  2440    Typed_identifier* rec = NULL;
  2441    if (token->is_op(OPERATOR_LPAREN))
  2442      {
  2443        expected_receiver = true;
  2444        rec = this->receiver();
  2445        token = this->peek_token();
  2446      }
  2447  
  2448    if (!token->is_identifier())
  2449      {
  2450        go_error_at(this->location(), "expected function name");
  2451        return;
  2452      }
  2453  
  2454    std::string name =
  2455      this->gogo_->pack_hidden_name(token->identifier(),
  2456  				  token->is_identifier_exported());
  2457  
  2458    this->advance_token();
  2459  
  2460    Function_type* fntype = this->signature(rec, this->location());
  2461  
  2462    Named_object* named_object = NULL;
  2463  
  2464    if (this->peek_token()->is_keyword(KEYWORD_ASM))
  2465      {
  2466        if (!this->advance_token()->is_op(OPERATOR_LPAREN))
  2467  	{
  2468  	  go_error_at(this->location(), "expected %<(%>");
  2469  	  return;
  2470  	}
  2471        token = this->advance_token();
  2472        if (!token->is_string())
  2473  	{
  2474  	  go_error_at(this->location(), "expected string");
  2475  	  return;
  2476  	}
  2477        std::string asm_name = token->string_value();
  2478        if (!this->advance_token()->is_op(OPERATOR_RPAREN))
  2479  	{
  2480  	  go_error_at(this->location(), "expected %<)%>");
  2481  	  return;
  2482  	}
  2483        this->advance_token();
  2484        if (!Gogo::is_sink_name(name))
  2485  	{
  2486  	  named_object = this->gogo_->declare_function(name, fntype, location);
  2487  	  if (named_object->is_function_declaration())
  2488  	    named_object->func_declaration_value()->set_asm_name(asm_name);
  2489  	}
  2490      }
  2491  
  2492    // Check for the easy error of a newline before the opening brace.
  2493    if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  2494      {
  2495        Location semi_loc = this->location();
  2496        if (this->advance_token()->is_op(OPERATOR_LCURLY))
  2497  	go_error_at(this->location(),
  2498  		    "unexpected semicolon or newline before %<{%>");
  2499        else
  2500  	this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
  2501  						     semi_loc));
  2502      }
  2503  
  2504    static struct {
  2505      unsigned int bit;
  2506      const char* name;
  2507      bool decl_ok;
  2508      bool func_ok;
  2509      bool method_ok;
  2510    } pragma_check[] =
  2511        {
  2512  	{ GOPRAGMA_NOINTERFACE, "nointerface", false, false, true },
  2513  	{ GOPRAGMA_NOESCAPE, "noescape", true, false, false },
  2514  	{ GOPRAGMA_NORACE, "norace", false, true, true },
  2515  	{ GOPRAGMA_NOSPLIT, "nosplit", false, true, true },
  2516  	{ GOPRAGMA_NOINLINE, "noinline", false, true, true },
  2517  	{ GOPRAGMA_SYSTEMSTACK, "systemstack", false, true, true },
  2518  	{ GOPRAGMA_NOWRITEBARRIER, "nowritebarrier", false, true, true },
  2519  	{ GOPRAGMA_NOWRITEBARRIERREC, "nowritebarrierrec", false, true,
  2520  	  true },
  2521  	{ GOPRAGMA_YESWRITEBARRIERREC, "yeswritebarrierrec", false, true,
  2522  	  true },
  2523  	{ GOPRAGMA_CGOUNSAFEARGS, "cgo_unsafe_args", false, true, true },
  2524  	{ GOPRAGMA_UINTPTRESCAPES, "uintptrescapes", true, true, true },
  2525        };
  2526  
  2527    bool is_decl = !this->peek_token()->is_op(OPERATOR_LCURLY);
  2528    if (pragmas != 0)
  2529      {
  2530        for (size_t i = 0;
  2531  	   i < sizeof(pragma_check) / sizeof(pragma_check[0]);
  2532  	   ++i)
  2533  	{
  2534  	  if ((pragmas & pragma_check[i].bit) == 0)
  2535  	    continue;
  2536  
  2537  	  if (is_decl)
  2538  	    {
  2539  	      if (pragma_check[i].decl_ok)
  2540  		continue;
  2541  	      go_warning_at(location, 0,
  2542  			    ("ignoring magic %<//go:%s%> comment "
  2543  			     "before declaration"),
  2544  			    pragma_check[i].name);
  2545  	    }
  2546  	  else if (rec == NULL)
  2547  	    {
  2548  	      if (pragma_check[i].func_ok)
  2549  		continue;
  2550  	      go_warning_at(location, 0,
  2551  			    ("ignoring magic %<//go:%s%> comment "
  2552  			     "before function definition"),
  2553  			    pragma_check[i].name);
  2554  	    }
  2555  	  else
  2556  	    {
  2557  	      if (pragma_check[i].method_ok)
  2558  		continue;
  2559  	      go_warning_at(location, 0,
  2560  			    ("ignoring magic %<//go:%s%> comment "
  2561  			     "before method definition"),
  2562  			    pragma_check[i].name);
  2563  	    }
  2564  
  2565  	  pragmas &= ~ pragma_check[i].bit;
  2566  	}
  2567      }
  2568  
  2569    if (is_decl)
  2570      {
  2571        if (named_object == NULL)
  2572  	{
  2573            // Function declarations with the blank identifier as a name are
  2574            // mostly ignored since they cannot be called.  We make an object
  2575            // for this declaration for type-checking purposes.
  2576            if (Gogo::is_sink_name(name))
  2577              {
  2578                static int count;
  2579                char buf[30];
  2580                snprintf(buf, sizeof buf, ".$sinkfndecl%d", count);
  2581                ++count;
  2582                name = std::string(buf);
  2583              }
  2584  
  2585  	  if (fntype == NULL
  2586                || (expected_receiver && rec == NULL))
  2587  	    this->gogo_->add_erroneous_name(name);
  2588  	  else
  2589  	    {
  2590  	      named_object = this->gogo_->declare_function(name, fntype,
  2591  							   location);
  2592  	      if (!extern_name.empty()
  2593  		  && named_object->is_function_declaration())
  2594  		{
  2595  		  Function_declaration* fd =
  2596  		    named_object->func_declaration_value();
  2597  		  fd->set_asm_name(extern_name);
  2598  		}
  2599  	    }
  2600  	}
  2601  
  2602        if (pragmas != 0 && named_object->is_function_declaration())
  2603  	named_object->func_declaration_value()->set_pragmas(pragmas);
  2604      }
  2605    else
  2606      {
  2607        bool hold_is_erroneous_function = this->is_erroneous_function_;
  2608        if (fntype == NULL)
  2609  	{
  2610  	  fntype = Type::make_function_type(NULL, NULL, NULL, location);
  2611  	  this->is_erroneous_function_ = true;
  2612  	  if (!Gogo::is_sink_name(name))
  2613  	    this->gogo_->add_erroneous_name(name);
  2614  	  name = this->gogo_->pack_hidden_name("_", false);
  2615  	}
  2616        named_object = this->gogo_->start_function(name, fntype, true, location);
  2617        Location end_loc = this->block();
  2618        this->gogo_->finish_function(end_loc);
  2619  
  2620        if (pragmas != 0
  2621  	  && !this->is_erroneous_function_
  2622  	  && named_object->is_function())
  2623  	named_object->func_value()->set_pragmas(pragmas);
  2624        this->is_erroneous_function_ = hold_is_erroneous_function;
  2625      }
  2626  }
  2627  
  2628  // Receiver = Parameters .
  2629  
  2630  Typed_identifier*
  2631  Parse::receiver()
  2632  {
  2633    Location location = this->location();
  2634    Typed_identifier_list* til;
  2635    if (!this->parameters(&til, NULL))
  2636      return NULL;
  2637    else if (til == NULL || til->empty())
  2638      {
  2639        go_error_at(location, "method has no receiver");
  2640        return NULL;
  2641      }
  2642    else if (til->size() > 1)
  2643      {
  2644        go_error_at(location, "method has multiple receivers");
  2645        return NULL;
  2646      }
  2647    else
  2648      return &til->front();
  2649  }
  2650  
  2651  // Operand    = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
  2652  // Literal    = BasicLit | CompositeLit | FunctionLit .
  2653  // BasicLit   = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
  2654  
  2655  // If MAY_BE_SINK is true, this operand may be "_".
  2656  
  2657  // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
  2658  // if the entire expression is in parentheses.
  2659  
  2660  Expression*
  2661  Parse::operand(bool may_be_sink, bool* is_parenthesized)
  2662  {
  2663    const Token* token = this->peek_token();
  2664    Expression* ret;
  2665    switch (token->classification())
  2666      {
  2667      case Token::TOKEN_IDENTIFIER:
  2668        {
  2669  	Location location = token->location();
  2670  	std::string id = token->identifier();
  2671  	bool is_exported = token->is_identifier_exported();
  2672  	std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
  2673  
  2674  	Named_object* in_function;
  2675  	Named_object* named_object = this->gogo_->lookup(packed, &in_function);
  2676  
  2677  	Package* package = NULL;
  2678  	if (named_object != NULL && named_object->is_package())
  2679  	  {
  2680  	    if (!this->advance_token()->is_op(OPERATOR_DOT)
  2681  		|| !this->advance_token()->is_identifier())
  2682  	      {
  2683  		go_error_at(location, "unexpected reference to package");
  2684  		return Expression::make_error(location);
  2685  	      }
  2686  	    package = named_object->package_value();
  2687  	    package->note_usage(id);
  2688  	    id = this->peek_token()->identifier();
  2689  	    is_exported = this->peek_token()->is_identifier_exported();
  2690  	    packed = this->gogo_->pack_hidden_name(id, is_exported);
  2691  	    named_object = package->lookup(packed);
  2692  	    location = this->location();
  2693  	    go_assert(in_function == NULL);
  2694  	  }
  2695  
  2696  	this->advance_token();
  2697  
  2698  	if (named_object != NULL
  2699  	    && named_object->is_type()
  2700  	    && !named_object->type_value()->is_visible())
  2701  	  {
  2702  	    go_assert(package != NULL);
  2703  	    go_error_at(location, "invalid reference to hidden type %<%s.%s%>",
  2704  			Gogo::message_name(package->package_name()).c_str(),
  2705  			Gogo::message_name(id).c_str());
  2706  	    return Expression::make_error(location);
  2707  	  }
  2708  
  2709  
  2710  	if (named_object == NULL)
  2711  	  {
  2712  	    if (package != NULL)
  2713  	      {
  2714  		std::string n1 = Gogo::message_name(package->package_name());
  2715  		std::string n2 = Gogo::message_name(id);
  2716  		if (!is_exported)
  2717  		  go_error_at(location,
  2718  			      ("invalid reference to unexported identifier "
  2719  			       "%<%s.%s%>"),
  2720  			      n1.c_str(), n2.c_str());
  2721  		else
  2722  		  go_error_at(location,
  2723  			      "reference to undefined identifier %<%s.%s%>",
  2724  			      n1.c_str(), n2.c_str());
  2725  		return Expression::make_error(location);
  2726  	      }
  2727  
  2728  	    named_object = this->gogo_->add_unknown_name(packed, location);
  2729  	  }
  2730  
  2731  	if (in_function != NULL
  2732  	    && in_function != this->gogo_->current_function()
  2733  	    && (named_object->is_variable()
  2734  		|| named_object->is_result_variable()))
  2735  	  return this->enclosing_var_reference(in_function, named_object,
  2736  					       may_be_sink, location);
  2737  
  2738  	switch (named_object->classification())
  2739  	  {
  2740  	  case Named_object::NAMED_OBJECT_CONST:
  2741  	    return Expression::make_const_reference(named_object, location);
  2742  	  case Named_object::NAMED_OBJECT_TYPE:
  2743  	    return Expression::make_type(named_object->type_value(), location);
  2744  	  case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
  2745  	    {
  2746  	      Type* t = Type::make_forward_declaration(named_object);
  2747  	      return Expression::make_type(t, location);
  2748  	    }
  2749  	  case Named_object::NAMED_OBJECT_VAR:
  2750  	  case Named_object::NAMED_OBJECT_RESULT_VAR:
  2751  	    // Any left-hand-side can be a sink, so if this can not be
  2752  	    // a sink, then it must be a use of the variable.
  2753  	    if (!may_be_sink)
  2754  	      this->mark_var_used(named_object);
  2755  	    return Expression::make_var_reference(named_object, location);
  2756  	  case Named_object::NAMED_OBJECT_SINK:
  2757  	    if (may_be_sink)
  2758  	      return Expression::make_sink(location);
  2759  	    else
  2760  	      {
  2761  		go_error_at(location, "cannot use %<_%> as value");
  2762  		return Expression::make_error(location);
  2763  	      }
  2764  	  case Named_object::NAMED_OBJECT_FUNC:
  2765  	  case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
  2766  	    return Expression::make_func_reference(named_object, NULL,
  2767  						   location);
  2768  	  case Named_object::NAMED_OBJECT_UNKNOWN:
  2769  	    {
  2770  	      Unknown_expression* ue =
  2771  		Expression::make_unknown_reference(named_object, location);
  2772  	      if (this->is_erroneous_function_)
  2773  		ue->set_no_error_message();
  2774  	      return ue;
  2775  	    }
  2776  	  case Named_object::NAMED_OBJECT_ERRONEOUS:
  2777  	    return Expression::make_error(location);
  2778  	  default:
  2779  	    go_unreachable();
  2780  	  }
  2781        }
  2782        go_unreachable();
  2783  
  2784      case Token::TOKEN_STRING:
  2785        ret = Expression::make_string(token->string_value(), token->location());
  2786        this->advance_token();
  2787        return ret;
  2788  
  2789      case Token::TOKEN_CHARACTER:
  2790        ret = Expression::make_character(token->character_value(), NULL,
  2791  				       token->location());
  2792        this->advance_token();
  2793        return ret;
  2794  
  2795      case Token::TOKEN_INTEGER:
  2796        ret = Expression::make_integer_z(token->integer_value(), NULL,
  2797  				       token->location());
  2798        this->advance_token();
  2799        return ret;
  2800  
  2801      case Token::TOKEN_FLOAT:
  2802        ret = Expression::make_float(token->float_value(), NULL,
  2803  				   token->location());
  2804        this->advance_token();
  2805        return ret;
  2806  
  2807      case Token::TOKEN_IMAGINARY:
  2808        {
  2809  	mpfr_t zero;
  2810  	mpfr_init_set_ui(zero, 0, MPFR_RNDN);
  2811  	mpc_t val;
  2812  	mpc_init2(val, mpc_precision);
  2813  	mpc_set_fr_fr(val, zero, *token->imaginary_value(), MPC_RNDNN);
  2814  	mpfr_clear(zero);
  2815  	ret = Expression::make_complex(&val, NULL, token->location());
  2816  	mpc_clear(val);
  2817  	this->advance_token();
  2818  	return ret;
  2819        }
  2820  
  2821      case Token::TOKEN_KEYWORD:
  2822        switch (token->keyword())
  2823  	{
  2824  	case KEYWORD_FUNC:
  2825  	  return this->function_lit();
  2826  	case KEYWORD_CHAN:
  2827  	case KEYWORD_INTERFACE:
  2828  	case KEYWORD_MAP:
  2829  	case KEYWORD_STRUCT:
  2830  	  {
  2831  	    Location location = token->location();
  2832  	    return Expression::make_type(this->type(), location);
  2833  	  }
  2834  	default:
  2835  	  break;
  2836  	}
  2837        break;
  2838  
  2839      case Token::TOKEN_OPERATOR:
  2840        if (token->is_op(OPERATOR_LPAREN))
  2841  	{
  2842  	  this->advance_token();
  2843  	  ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL,
  2844  				 NULL);
  2845  	  if (!this->peek_token()->is_op(OPERATOR_RPAREN))
  2846  	    go_error_at(this->location(), "missing %<)%>");
  2847  	  else
  2848  	    this->advance_token();
  2849  	  if (is_parenthesized != NULL)
  2850  	    *is_parenthesized = true;
  2851  	  return ret;
  2852  	}
  2853        else if (token->is_op(OPERATOR_LSQUARE))
  2854  	{
  2855  	  // Here we call array_type directly, as this is the only
  2856  	  // case where an ellipsis is permitted for an array type.
  2857  	  Location location = token->location();
  2858  	  return Expression::make_type(this->array_type(true), location);
  2859  	}
  2860        break;
  2861  
  2862      default:
  2863        break;
  2864      }
  2865  
  2866    go_error_at(this->location(), "expected operand");
  2867    return Expression::make_error(this->location());
  2868  }
  2869  
  2870  // Handle a reference to a variable in an enclosing function.  We add
  2871  // it to a list of such variables.  We return a reference to a field
  2872  // in a struct which will be passed on the static chain when calling
  2873  // the current function.
  2874  
  2875  Expression*
  2876  Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
  2877  			       bool may_be_sink, Location location)
  2878  {
  2879    go_assert(var->is_variable() || var->is_result_variable());
  2880  
  2881    // Any left-hand-side can be a sink, so if this can not be
  2882    // a sink, then it must be a use of the variable.
  2883    if (!may_be_sink)
  2884      this->mark_var_used(var);
  2885  
  2886    Named_object* this_function = this->gogo_->current_function();
  2887    Named_object* closure = this_function->func_value()->closure_var();
  2888  
  2889    // The last argument to the Enclosing_var constructor is the index
  2890    // of this variable in the closure.  We add 1 to the current number
  2891    // of enclosed variables, because the first field in the closure
  2892    // points to the function code.
  2893    Enclosing_var ev(var, in_function, this->enclosing_vars_.size() + 1);
  2894    std::pair<Enclosing_vars::iterator, bool> ins =
  2895      this->enclosing_vars_.insert(ev);
  2896    if (ins.second)
  2897      {
  2898        // This is a variable we have not seen before.  Add a new field
  2899        // to the closure type.
  2900        this_function->func_value()->add_closure_field(var, location);
  2901      }
  2902  
  2903    Expression* closure_ref = Expression::make_var_reference(closure,
  2904  							   location);
  2905    closure_ref =
  2906        Expression::make_dereference(closure_ref,
  2907                                     Expression::NIL_CHECK_NOT_NEEDED,
  2908                                     location);
  2909  
  2910    // The closure structure holds pointers to the variables, so we need
  2911    // to introduce an indirection.
  2912    Expression* e = Expression::make_field_reference(closure_ref,
  2913  						   ins.first->index(),
  2914  						   location);
  2915    e = Expression::make_dereference(e, Expression::NIL_CHECK_NOT_NEEDED,
  2916                                     location);
  2917    return Expression::make_enclosing_var_reference(e, var, location);
  2918  }
  2919  
  2920  // CompositeLit  = LiteralType LiteralValue .
  2921  // LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
  2922  //                 SliceType | MapType | TypeName .
  2923  // LiteralValue  = "{" [ ElementList [ "," ] ] "}" .
  2924  // ElementList   = Element { "," Element } .
  2925  // Element       = [ Key ":" ] Value .
  2926  // Key           = FieldName | ElementIndex .
  2927  // FieldName     = identifier .
  2928  // ElementIndex  = Expression .
  2929  // Value         = Expression | LiteralValue .
  2930  
  2931  // We have already seen the type if there is one, and we are now
  2932  // looking at the LiteralValue.  The case "[" "..."  "]" ElementType
  2933  // will be seen here as an array type whose length is "nil".  The
  2934  // DEPTH parameter is non-zero if this is an embedded composite
  2935  // literal and the type was omitted.  It gives the number of steps up
  2936  // to the type which was provided.  E.g., in [][]int{{1}} it will be
  2937  // 1.  In [][][]int{{{1}}} it will be 2.
  2938  
  2939  Expression*
  2940  Parse::composite_lit(Type* type, int depth, Location location)
  2941  {
  2942    go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
  2943    this->advance_token();
  2944  
  2945    if (this->peek_token()->is_op(OPERATOR_RCURLY))
  2946      {
  2947        this->advance_token();
  2948        return Expression::make_composite_literal(type, depth, false, NULL,
  2949  						false, location);
  2950      }
  2951  
  2952    bool has_keys = false;
  2953    bool all_are_names = true;
  2954    Expression_list* vals = new Expression_list;
  2955    while (true)
  2956      {
  2957        Expression* val;
  2958        bool is_type_omitted = false;
  2959        bool is_name = false;
  2960  
  2961        const Token* token = this->peek_token();
  2962  
  2963        if (token->is_identifier())
  2964  	{
  2965  	  std::string identifier = token->identifier();
  2966  	  bool is_exported = token->is_identifier_exported();
  2967  	  Location id_location = token->location();
  2968  
  2969  	  if (this->advance_token()->is_op(OPERATOR_COLON))
  2970  	    {
  2971  	      // This may be a field name.  We don't know for sure--it
  2972  	      // could also be an expression for an array index.  We
  2973  	      // don't want to parse it as an expression because may
  2974  	      // trigger various errors, e.g., if this identifier
  2975  	      // happens to be the name of a package.
  2976  	      Gogo* gogo = this->gogo_;
  2977  	      val = this->id_to_expression(gogo->pack_hidden_name(identifier,
  2978  								  is_exported),
  2979  					   id_location, false, true);
  2980  	      is_name = true;
  2981  	    }
  2982  	  else
  2983  	    {
  2984  	      this->unget_token(Token::make_identifier_token(identifier,
  2985  							     is_exported,
  2986  							     id_location));
  2987  	      val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
  2988  				     NULL);
  2989  	    }
  2990  	}
  2991        else if (!token->is_op(OPERATOR_LCURLY))
  2992  	val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  2993        else
  2994  	{
  2995  	  // This must be a composite literal inside another composite
  2996  	  // literal, with the type omitted for the inner one.
  2997  	  val = this->composite_lit(type, depth + 1, token->location());
  2998            is_type_omitted = true;
  2999  	}
  3000  
  3001        token = this->peek_token();
  3002        if (!token->is_op(OPERATOR_COLON))
  3003  	{
  3004  	  if (has_keys)
  3005  	    vals->push_back(NULL);
  3006  	  is_name = false;
  3007  	}
  3008        else
  3009  	{
  3010            if (is_type_omitted)
  3011              {
  3012                // VAL is a nested composite literal with an omitted type being
  3013                // used a key.  Record this information in VAL so that the correct
  3014                // type is associated with the literal value if VAL is a
  3015                // map literal.
  3016                val->complit()->update_key_path(depth);
  3017              }
  3018  
  3019  	  this->advance_token();
  3020  
  3021  	  if (!has_keys && !vals->empty())
  3022  	    {
  3023  	      Expression_list* newvals = new Expression_list;
  3024  	      for (Expression_list::const_iterator p = vals->begin();
  3025  		   p != vals->end();
  3026  		   ++p)
  3027  		{
  3028  		  newvals->push_back(NULL);
  3029  		  newvals->push_back(*p);
  3030  		}
  3031  	      delete vals;
  3032  	      vals = newvals;
  3033  	    }
  3034  	  has_keys = true;
  3035  
  3036  	  vals->push_back(val);
  3037  
  3038  	  if (!token->is_op(OPERATOR_LCURLY))
  3039  	    val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  3040  	  else
  3041  	    {
  3042  	      // This must be a composite literal inside another
  3043  	      // composite literal, with the type omitted for the
  3044  	      // inner one.
  3045  	      val = this->composite_lit(type, depth + 1, token->location());
  3046  	    }
  3047  
  3048  	  token = this->peek_token();
  3049  	}
  3050  
  3051        vals->push_back(val);
  3052  
  3053        if (!is_name)
  3054  	all_are_names = false;
  3055  
  3056        if (token->is_op(OPERATOR_COMMA))
  3057  	{
  3058  	  if (this->advance_token()->is_op(OPERATOR_RCURLY))
  3059  	    {
  3060  	      this->advance_token();
  3061  	      break;
  3062  	    }
  3063  	}
  3064        else if (token->is_op(OPERATOR_RCURLY))
  3065  	{
  3066  	  this->advance_token();
  3067  	  break;
  3068  	}
  3069        else
  3070  	{
  3071  	  if (token->is_op(OPERATOR_SEMICOLON))
  3072  	    go_error_at(this->location(),
  3073  			("need trailing comma before newline "
  3074  			 "in composite literal"));
  3075  	  else
  3076  	    go_error_at(this->location(), "expected %<,%> or %<}%>");
  3077  
  3078  	  this->gogo_->mark_locals_used();
  3079  	  int edepth = 0;
  3080  	  while (!token->is_eof()
  3081  		 && (edepth > 0 || !token->is_op(OPERATOR_RCURLY)))
  3082  	    {
  3083  	      if (token->is_op(OPERATOR_LCURLY))
  3084  		++edepth;
  3085  	      else if (token->is_op(OPERATOR_RCURLY))
  3086  		--edepth;
  3087  	      token = this->advance_token();
  3088  	    }
  3089  	  if (token->is_op(OPERATOR_RCURLY))
  3090  	    this->advance_token();
  3091  
  3092  	  return Expression::make_error(location);
  3093  	}
  3094      }
  3095  
  3096    return Expression::make_composite_literal(type, depth, has_keys, vals,
  3097  					    all_are_names, location);
  3098  }
  3099  
  3100  // FunctionLit = "func" Signature Block .
  3101  
  3102  Expression*
  3103  Parse::function_lit()
  3104  {
  3105    Location location = this->location();
  3106    go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
  3107    this->advance_token();
  3108  
  3109    Enclosing_vars hold_enclosing_vars;
  3110    hold_enclosing_vars.swap(this->enclosing_vars_);
  3111  
  3112    Function_type* type = this->signature(NULL, location);
  3113    bool fntype_is_error = false;
  3114    if (type == NULL)
  3115      {
  3116        type = Type::make_function_type(NULL, NULL, NULL, location);
  3117        fntype_is_error = true;
  3118      }
  3119  
  3120    // For a function literal, the next token must be a '{'.  If we
  3121    // don't see that, then we may have a type expression.
  3122    if (!this->peek_token()->is_op(OPERATOR_LCURLY))
  3123      {
  3124        hold_enclosing_vars.swap(this->enclosing_vars_);
  3125        return Expression::make_type(type, location);
  3126      }
  3127  
  3128    bool hold_is_erroneous_function = this->is_erroneous_function_;
  3129    if (fntype_is_error)
  3130      this->is_erroneous_function_ = true;
  3131  
  3132    Bc_stack* hold_break_stack = this->break_stack_;
  3133    Bc_stack* hold_continue_stack = this->continue_stack_;
  3134    this->break_stack_ = NULL;
  3135    this->continue_stack_ = NULL;
  3136  
  3137    Named_object* no = this->gogo_->start_function("", type, true, location);
  3138  
  3139    Location end_loc = this->block();
  3140  
  3141    this->gogo_->finish_function(end_loc);
  3142  
  3143    if (this->break_stack_ != NULL)
  3144      delete this->break_stack_;
  3145    if (this->continue_stack_ != NULL)
  3146      delete this->continue_stack_;
  3147    this->break_stack_ = hold_break_stack;
  3148    this->continue_stack_ = hold_continue_stack;
  3149  
  3150    this->is_erroneous_function_ = hold_is_erroneous_function;
  3151  
  3152    hold_enclosing_vars.swap(this->enclosing_vars_);
  3153  
  3154    Expression* closure = this->create_closure(no, &hold_enclosing_vars,
  3155  					     location);
  3156  
  3157    return Expression::make_func_reference(no, closure, location);
  3158  }
  3159  
  3160  // Create a closure for the nested function FUNCTION.  This is based
  3161  // on ENCLOSING_VARS, which is a list of all variables defined in
  3162  // enclosing functions and referenced from FUNCTION.  A closure is the
  3163  // address of a struct which point to the real function code and
  3164  // contains the addresses of all the referenced variables.  This
  3165  // returns NULL if no closure is required.
  3166  
  3167  Expression*
  3168  Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
  3169  		      Location location)
  3170  {
  3171    if (enclosing_vars->empty())
  3172      return NULL;
  3173  
  3174    // Get the variables in order by their field index.
  3175  
  3176    size_t enclosing_var_count = enclosing_vars->size();
  3177    std::vector<Enclosing_var> ev(enclosing_var_count);
  3178    for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
  3179         p != enclosing_vars->end();
  3180         ++p)
  3181      {
  3182        // Subtract 1 because index 0 is the function code.
  3183        ev[p->index() - 1] = *p;
  3184      }
  3185  
  3186    // Build an initializer for a composite literal of the closure's
  3187    // type.
  3188  
  3189    Named_object* enclosing_function = this->gogo_->current_function();
  3190    Expression_list* initializer = new Expression_list;
  3191  
  3192    initializer->push_back(Expression::make_func_code_reference(function,
  3193  							      location));
  3194  
  3195    for (size_t i = 0; i < enclosing_var_count; ++i)
  3196      {
  3197        // Add 1 to i because the first field in the closure is a
  3198        // pointer to the function code.
  3199        go_assert(ev[i].index() == i + 1);
  3200        Named_object* var = ev[i].var();
  3201        Expression* ref;
  3202        if (ev[i].in_function() == enclosing_function)
  3203  	ref = Expression::make_var_reference(var, location);
  3204        else
  3205  	ref = this->enclosing_var_reference(ev[i].in_function(), var,
  3206  					    true, location);
  3207        Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
  3208  						   location);
  3209        initializer->push_back(refaddr);
  3210      }
  3211  
  3212    Named_object* closure_var = function->func_value()->closure_var();
  3213    Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
  3214    Expression* cv = Expression::make_struct_composite_literal(st, initializer,
  3215  							     location);
  3216    return Expression::make_heap_expression(cv, location);
  3217  }
  3218  
  3219  // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
  3220  
  3221  // If MAY_BE_SINK is true, this expression may be "_".
  3222  
  3223  // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
  3224  // literal.
  3225  
  3226  // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
  3227  // guard (var := expr.("type") using the literal keyword "type").
  3228  
  3229  // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
  3230  // if the entire expression is in parentheses.
  3231  
  3232  Expression*
  3233  Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
  3234  		    bool* is_type_switch, bool* is_parenthesized)
  3235  {
  3236    Location start_loc = this->location();
  3237    bool operand_is_parenthesized = false;
  3238    bool whole_is_parenthesized = false;
  3239  
  3240    Expression* ret = this->operand(may_be_sink, &operand_is_parenthesized);
  3241  
  3242    whole_is_parenthesized = operand_is_parenthesized;
  3243  
  3244    // An unknown name followed by a curly brace must be a composite
  3245    // literal, and the unknown name must be a type.
  3246    if (may_be_composite_lit
  3247        && !operand_is_parenthesized
  3248        && ret->unknown_expression() != NULL
  3249        && this->peek_token()->is_op(OPERATOR_LCURLY))
  3250      {
  3251        Named_object* no = ret->unknown_expression()->named_object();
  3252        Type* type = Type::make_forward_declaration(no);
  3253        ret = Expression::make_type(type, ret->location());
  3254      }
  3255  
  3256    // We handle composite literals and type casts here, as it is the
  3257    // easiest way to handle types which are in parentheses, as in
  3258    // "((uint))(1)".
  3259    if (ret->is_type_expression())
  3260      {
  3261        if (this->peek_token()->is_op(OPERATOR_LCURLY))
  3262  	{
  3263  	  whole_is_parenthesized = false;
  3264  	  if (!may_be_composite_lit)
  3265  	    {
  3266  	      Type* t = ret->type();
  3267  	      if (t->named_type() != NULL
  3268  		  || t->forward_declaration_type() != NULL)
  3269  		go_error_at(start_loc,
  3270  			    _("parentheses required around this composite "
  3271  			      "literal to avoid parsing ambiguity"));
  3272  	    }
  3273  	  else if (operand_is_parenthesized)
  3274  	    go_error_at(start_loc,
  3275  			"cannot parenthesize type in composite literal");
  3276  	  ret = this->composite_lit(ret->type(), 0, ret->location());
  3277  	}
  3278        else if (this->peek_token()->is_op(OPERATOR_LPAREN))
  3279  	{
  3280  	  whole_is_parenthesized = false;
  3281  	  Location loc = this->location();
  3282  	  this->advance_token();
  3283  	  Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
  3284  					      NULL, NULL);
  3285  	  if (this->peek_token()->is_op(OPERATOR_COMMA))
  3286  	    this->advance_token();
  3287  	  if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
  3288  	    {
  3289  	      go_error_at(this->location(),
  3290  			  "invalid use of %<...%> in type conversion");
  3291  	      this->advance_token();
  3292  	    }
  3293  	  if (!this->peek_token()->is_op(OPERATOR_RPAREN))
  3294  	    go_error_at(this->location(), "expected %<)%>");
  3295  	  else
  3296  	    this->advance_token();
  3297  	  if (expr->is_error_expression())
  3298  	    ret = expr;
  3299  	  else
  3300  	    {
  3301  	      Type* t = ret->type();
  3302  	      if (t->classification() == Type::TYPE_ARRAY
  3303  		  && t->array_type()->length() != NULL
  3304  		  && t->array_type()->length()->is_nil_expression())
  3305  		{
  3306  		  go_error_at(ret->location(),
  3307  			      "use of %<[...]%> outside of array literal");
  3308  		  ret = Expression::make_error(loc);
  3309  		}
  3310  	      else
  3311  		ret = Expression::make_cast(t, expr, loc);
  3312  	    }
  3313  	}
  3314      }
  3315  
  3316    while (true)
  3317      {
  3318        const Token* token = this->peek_token();
  3319        if (token->is_op(OPERATOR_LPAREN))
  3320  	{
  3321  	  whole_is_parenthesized = false;
  3322  	  ret = this->call(this->verify_not_sink(ret));
  3323  	}
  3324        else if (token->is_op(OPERATOR_DOT))
  3325  	{
  3326  	  whole_is_parenthesized = false;
  3327  	  ret = this->selector(this->verify_not_sink(ret), is_type_switch);
  3328  	  if (is_type_switch != NULL && *is_type_switch)
  3329  	    break;
  3330  	}
  3331        else if (token->is_op(OPERATOR_LSQUARE))
  3332  	{
  3333  	  whole_is_parenthesized = false;
  3334  	  ret = this->index(this->verify_not_sink(ret));
  3335  	}
  3336        else
  3337  	break;
  3338      }
  3339  
  3340    if (whole_is_parenthesized && is_parenthesized != NULL)
  3341      *is_parenthesized = true;
  3342  
  3343    return ret;
  3344  }
  3345  
  3346  // Selector = "." identifier .
  3347  // TypeGuard = "." "(" QualifiedIdent ")" .
  3348  
  3349  // Note that Operand can expand to QualifiedIdent, which contains a
  3350  // ".".  That is handled directly in operand when it sees a package
  3351  // name.
  3352  
  3353  // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
  3354  // guard (var := expr.("type") using the literal keyword "type").
  3355  
  3356  Expression*
  3357  Parse::selector(Expression* left, bool* is_type_switch)
  3358  {
  3359    go_assert(this->peek_token()->is_op(OPERATOR_DOT));
  3360    Location location = this->location();
  3361  
  3362    const Token* token = this->advance_token();
  3363    if (token->is_identifier())
  3364      {
  3365        // This could be a field in a struct, or a method in an
  3366        // interface, or a method associated with a type.  We can't know
  3367        // which until we have seen all the types.
  3368        std::string name =
  3369  	this->gogo_->pack_hidden_name(token->identifier(),
  3370  				      token->is_identifier_exported());
  3371        if (token->identifier() == "_")
  3372  	{
  3373  	  go_error_at(this->location(), "invalid use of %<_%>");
  3374  	  name = Gogo::erroneous_name();
  3375  	}
  3376        this->advance_token();
  3377        return Expression::make_selector(left, name, location);
  3378      }
  3379    else if (token->is_op(OPERATOR_LPAREN))
  3380      {
  3381        this->advance_token();
  3382        Type* type = NULL;
  3383        if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
  3384  	type = this->type();
  3385        else
  3386  	{
  3387  	  if (is_type_switch != NULL)
  3388  	    *is_type_switch = true;
  3389  	  else
  3390  	    {
  3391  	      go_error_at(this->location(),
  3392  			  "use of %<.(type)%> outside type switch");
  3393  	      type = Type::make_error_type();
  3394  	    }
  3395  	  this->advance_token();
  3396  	}
  3397        if (!this->peek_token()->is_op(OPERATOR_RPAREN))
  3398  	go_error_at(this->location(), "missing %<)%>");
  3399        else
  3400  	this->advance_token();
  3401        if (is_type_switch != NULL && *is_type_switch)
  3402  	return left;
  3403        return Expression::make_type_guard(left, type, location);
  3404      }
  3405    else
  3406      {
  3407        go_error_at(this->location(), "expected identifier or %<(%>");
  3408        return left;
  3409      }
  3410  }
  3411  
  3412  // Index          = "[" Expression "]" .
  3413  // Slice          = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
  3414  
  3415  Expression*
  3416  Parse::index(Expression* expr)
  3417  {
  3418    Location location = this->location();
  3419    go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
  3420    this->advance_token();
  3421  
  3422    Expression* start;
  3423    if (!this->peek_token()->is_op(OPERATOR_COLON))
  3424      start = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  3425    else
  3426      start = Expression::make_integer_ul(0, NULL, location);
  3427  
  3428    Expression* end = NULL;
  3429    if (this->peek_token()->is_op(OPERATOR_COLON))
  3430      {
  3431        // We use nil to indicate a missing high expression.
  3432        if (this->advance_token()->is_op(OPERATOR_RSQUARE))
  3433  	end = Expression::make_nil(this->location());
  3434        else if (this->peek_token()->is_op(OPERATOR_COLON))
  3435  	{
  3436  	  go_error_at(this->location(),
  3437  		      "middle index required in 3-index slice");
  3438  	  end = Expression::make_error(this->location());
  3439  	}
  3440        else
  3441  	end = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  3442      }
  3443  
  3444    Expression* cap = NULL;
  3445    if (this->peek_token()->is_op(OPERATOR_COLON))
  3446      {
  3447        if (this->advance_token()->is_op(OPERATOR_RSQUARE))
  3448  	{
  3449  	  go_error_at(this->location(),
  3450  		      "final index required in 3-index slice");
  3451  	  cap = Expression::make_error(this->location());
  3452  	}
  3453        else
  3454          cap = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  3455      }
  3456    if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
  3457      go_error_at(this->location(), "missing %<]%>");
  3458    else
  3459      this->advance_token();
  3460    return Expression::make_index(expr, start, end, cap, location);
  3461  }
  3462  
  3463  // Call           = "(" [ ArgumentList [ "," ] ] ")" .
  3464  // ArgumentList   = ExpressionList [ "..." ] .
  3465  
  3466  Expression*
  3467  Parse::call(Expression* func)
  3468  {
  3469    go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
  3470    Expression_list* args = NULL;
  3471    bool is_varargs = false;
  3472    const Token* token = this->advance_token();
  3473    if (!token->is_op(OPERATOR_RPAREN))
  3474      {
  3475        args = this->expression_list(NULL, false, true);
  3476        token = this->peek_token();
  3477        if (token->is_op(OPERATOR_ELLIPSIS))
  3478  	{
  3479  	  is_varargs = true;
  3480  	  token = this->advance_token();
  3481  	}
  3482      }
  3483    if (token->is_op(OPERATOR_COMMA))
  3484      token = this->advance_token();
  3485    if (!token->is_op(OPERATOR_RPAREN))
  3486      {
  3487        go_error_at(this->location(), "missing %<)%>");
  3488        if (!this->skip_past_error(OPERATOR_RPAREN))
  3489  	return Expression::make_error(this->location());
  3490      }
  3491    this->advance_token();
  3492    if (func->is_error_expression())
  3493      return func;
  3494    return Expression::make_call(func, args, is_varargs, func->location());
  3495  }
  3496  
  3497  // Return an expression for a single unqualified identifier.
  3498  
  3499  Expression*
  3500  Parse::id_to_expression(const std::string& name, Location location,
  3501  			bool is_lhs, bool is_composite_literal_key)
  3502  {
  3503    Named_object* in_function;
  3504    Named_object* named_object = this->gogo_->lookup(name, &in_function);
  3505    if (named_object == NULL)
  3506      {
  3507        if (is_composite_literal_key)
  3508  	{
  3509  	  // This is a composite literal key, which means that it
  3510  	  // could just be a struct field name, so avoid confusiong by
  3511  	  // not adding it to the bindings.  We'll look up the name
  3512  	  // later during the lowering phase if necessary.
  3513  	  return Expression::make_composite_literal_key(name, location);
  3514  	}
  3515        named_object = this->gogo_->add_unknown_name(name, location);
  3516      }
  3517  
  3518    if (in_function != NULL
  3519        && in_function != this->gogo_->current_function()
  3520        && (named_object->is_variable() || named_object->is_result_variable()))
  3521      return this->enclosing_var_reference(in_function, named_object, is_lhs,
  3522  					 location);
  3523  
  3524    switch (named_object->classification())
  3525      {
  3526      case Named_object::NAMED_OBJECT_CONST:
  3527        return Expression::make_const_reference(named_object, location);
  3528      case Named_object::NAMED_OBJECT_VAR:
  3529      case Named_object::NAMED_OBJECT_RESULT_VAR:
  3530        if (!is_lhs)
  3531  	this->mark_var_used(named_object);
  3532        return Expression::make_var_reference(named_object, location);
  3533      case Named_object::NAMED_OBJECT_SINK:
  3534        return Expression::make_sink(location);
  3535      case Named_object::NAMED_OBJECT_FUNC:
  3536      case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
  3537        return Expression::make_func_reference(named_object, NULL, location);
  3538      case Named_object::NAMED_OBJECT_UNKNOWN:
  3539        {
  3540  	Unknown_expression* ue =
  3541  	  Expression::make_unknown_reference(named_object, location);
  3542  	if (this->is_erroneous_function_)
  3543  	  ue->set_no_error_message();
  3544  	return ue;
  3545        }
  3546      case Named_object::NAMED_OBJECT_PACKAGE:
  3547      case Named_object::NAMED_OBJECT_TYPE:
  3548      case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
  3549        {
  3550  	// These cases can arise for a field name in a composite
  3551  	// literal.  Keep track of these as they might be fake uses of
  3552  	// the related package.
  3553  	Unknown_expression* ue =
  3554  	  Expression::make_unknown_reference(named_object, location);
  3555  	if (named_object->package() != NULL)
  3556  	  named_object->package()->note_fake_usage(ue);
  3557  	if (this->is_erroneous_function_)
  3558  	  ue->set_no_error_message();
  3559  	return ue;
  3560        }
  3561      case Named_object::NAMED_OBJECT_ERRONEOUS:
  3562        return Expression::make_error(location);
  3563      default:
  3564        go_error_at(this->location(), "unexpected type of identifier");
  3565        return Expression::make_error(location);
  3566      }
  3567  }
  3568  
  3569  // Expression = UnaryExpr { binary_op Expression } .
  3570  
  3571  // PRECEDENCE is the precedence of the current operator.
  3572  
  3573  // If MAY_BE_SINK is true, this expression may be "_".
  3574  
  3575  // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
  3576  // literal.
  3577  
  3578  // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
  3579  // guard (var := expr.("type") using the literal keyword "type").
  3580  
  3581  // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
  3582  // if the entire expression is in parentheses.
  3583  
  3584  Expression*
  3585  Parse::expression(Precedence precedence, bool may_be_sink,
  3586  		  bool may_be_composite_lit, bool* is_type_switch,
  3587  		  bool *is_parenthesized)
  3588  {
  3589    Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
  3590  				      is_type_switch, is_parenthesized);
  3591  
  3592    while (true)
  3593      {
  3594        if (is_type_switch != NULL && *is_type_switch)
  3595  	return left;
  3596  
  3597        const Token* token = this->peek_token();
  3598        if (token->classification() != Token::TOKEN_OPERATOR)
  3599  	{
  3600  	  // Not a binary_op.
  3601  	  return left;
  3602  	}
  3603  
  3604        Precedence right_precedence;
  3605        switch (token->op())
  3606  	{
  3607  	case OPERATOR_OROR:
  3608  	  right_precedence = PRECEDENCE_OROR;
  3609  	  break;
  3610  	case OPERATOR_ANDAND:
  3611  	  right_precedence = PRECEDENCE_ANDAND;
  3612  	  break;
  3613  	case OPERATOR_EQEQ:
  3614  	case OPERATOR_NOTEQ:
  3615  	case OPERATOR_LT:
  3616  	case OPERATOR_LE:
  3617  	case OPERATOR_GT:
  3618  	case OPERATOR_GE:
  3619  	  right_precedence = PRECEDENCE_RELOP;
  3620  	  break;
  3621  	case OPERATOR_PLUS:
  3622  	case OPERATOR_MINUS:
  3623  	case OPERATOR_OR:
  3624  	case OPERATOR_XOR:
  3625  	  right_precedence = PRECEDENCE_ADDOP;
  3626  	  break;
  3627  	case OPERATOR_MULT:
  3628  	case OPERATOR_DIV:
  3629  	case OPERATOR_MOD:
  3630  	case OPERATOR_LSHIFT:
  3631  	case OPERATOR_RSHIFT:
  3632  	case OPERATOR_AND:
  3633  	case OPERATOR_BITCLEAR:
  3634  	  right_precedence = PRECEDENCE_MULOP;
  3635  	  break;
  3636  	default:
  3637  	  right_precedence = PRECEDENCE_INVALID;
  3638  	  break;
  3639  	}
  3640  
  3641        if (right_precedence == PRECEDENCE_INVALID)
  3642  	{
  3643  	  // Not a binary_op.
  3644  	  return left;
  3645  	}
  3646  
  3647        if (is_parenthesized != NULL)
  3648  	*is_parenthesized = false;
  3649  
  3650        Operator op = token->op();
  3651        Location binop_location = token->location();
  3652  
  3653        if (precedence >= right_precedence)
  3654  	{
  3655  	  // We've already seen A * B, and we see + C.  We want to
  3656  	  // return so that A * B becomes a group.
  3657  	  return left;
  3658  	}
  3659  
  3660        this->advance_token();
  3661  
  3662        left = this->verify_not_sink(left);
  3663        Expression* right = this->expression(right_precedence, false,
  3664  					   may_be_composite_lit,
  3665  					   NULL, NULL);
  3666        left = Expression::make_binary(op, left, right, binop_location);
  3667      }
  3668  }
  3669  
  3670  bool
  3671  Parse::expression_may_start_here()
  3672  {
  3673    const Token* token = this->peek_token();
  3674    switch (token->classification())
  3675      {
  3676      case Token::TOKEN_INVALID:
  3677      case Token::TOKEN_EOF:
  3678        return false;
  3679      case Token::TOKEN_KEYWORD:
  3680        switch (token->keyword())
  3681  	{
  3682  	case KEYWORD_CHAN:
  3683  	case KEYWORD_FUNC:
  3684  	case KEYWORD_MAP:
  3685  	case KEYWORD_STRUCT:
  3686  	case KEYWORD_INTERFACE:
  3687  	  return true;
  3688  	default:
  3689  	  return false;
  3690  	}
  3691      case Token::TOKEN_IDENTIFIER:
  3692        return true;
  3693      case Token::TOKEN_STRING:
  3694        return true;
  3695      case Token::TOKEN_OPERATOR:
  3696        switch (token->op())
  3697  	{
  3698  	case OPERATOR_PLUS:
  3699  	case OPERATOR_MINUS:
  3700  	case OPERATOR_NOT:
  3701  	case OPERATOR_XOR:
  3702  	case OPERATOR_MULT:
  3703  	case OPERATOR_CHANOP:
  3704  	case OPERATOR_AND:
  3705  	case OPERATOR_LPAREN:
  3706  	case OPERATOR_LSQUARE:
  3707  	  return true;
  3708  	default:
  3709  	  return false;
  3710  	}
  3711      case Token::TOKEN_CHARACTER:
  3712      case Token::TOKEN_INTEGER:
  3713      case Token::TOKEN_FLOAT:
  3714      case Token::TOKEN_IMAGINARY:
  3715        return true;
  3716      default:
  3717        go_unreachable();
  3718      }
  3719  }
  3720  
  3721  // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
  3722  
  3723  // If MAY_BE_SINK is true, this expression may be "_".
  3724  
  3725  // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
  3726  // literal.
  3727  
  3728  // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
  3729  // guard (var := expr.("type") using the literal keyword "type").
  3730  
  3731  // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
  3732  // if the entire expression is in parentheses.
  3733  
  3734  Expression*
  3735  Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
  3736  		  bool* is_type_switch, bool* is_parenthesized)
  3737  {
  3738    const Token* token = this->peek_token();
  3739  
  3740    // There is a complex parse for <- chan.  The choices are
  3741    // Convert x to type <- chan int:
  3742    //   (<- chan int)(x)
  3743    // Receive from (x converted to type chan <- chan int):
  3744    //   (<- chan <- chan int (x))
  3745    // Convert x to type <- chan (<- chan int).
  3746    //   (<- chan <- chan int)(x)
  3747    if (token->is_op(OPERATOR_CHANOP))
  3748      {
  3749        Location location = token->location();
  3750        if (this->advance_token()->is_keyword(KEYWORD_CHAN))
  3751  	{
  3752  	  Expression* expr = this->primary_expr(false, may_be_composite_lit,
  3753  						NULL, NULL);
  3754  	  if (expr->is_error_expression())
  3755  	    return expr;
  3756  	  else if (!expr->is_type_expression())
  3757  	    return Expression::make_receive(expr, location);
  3758  	  else
  3759  	    {
  3760  	      if (expr->type()->is_error_type())
  3761  		return expr;
  3762  
  3763  	      // We picked up "chan TYPE", but it is not a type
  3764  	      // conversion.
  3765  	      Channel_type* ct = expr->type()->channel_type();
  3766  	      if (ct == NULL)
  3767  		{
  3768  		  // This is probably impossible.
  3769  		  go_error_at(location, "expected channel type");
  3770  		  return Expression::make_error(location);
  3771  		}
  3772  	      else if (ct->may_receive())
  3773  		{
  3774  		  // <- chan TYPE.
  3775  		  Type* t = Type::make_channel_type(false, true,
  3776  						    ct->element_type());
  3777  		  return Expression::make_type(t, location);
  3778  		}
  3779  	      else
  3780  		{
  3781  		  // <- chan <- TYPE.  Because we skipped the leading
  3782  		  // <-, we parsed this as chan <- TYPE.  With the
  3783  		  // leading <-, we parse it as <- chan (<- TYPE).
  3784  		  Type *t = this->reassociate_chan_direction(ct, location);
  3785  		  return Expression::make_type(t, location);
  3786  		}
  3787  	    }
  3788  	}
  3789  
  3790        this->unget_token(Token::make_operator_token(OPERATOR_CHANOP, location));
  3791        token = this->peek_token();
  3792      }
  3793  
  3794    if (token->is_op(OPERATOR_PLUS)
  3795        || token->is_op(OPERATOR_MINUS)
  3796        || token->is_op(OPERATOR_NOT)
  3797        || token->is_op(OPERATOR_XOR)
  3798        || token->is_op(OPERATOR_CHANOP)
  3799        || token->is_op(OPERATOR_MULT)
  3800        || token->is_op(OPERATOR_AND))
  3801      {
  3802        Location location = token->location();
  3803        Operator op = token->op();
  3804        this->advance_token();
  3805  
  3806        Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL,
  3807  					  NULL);
  3808        if (expr->is_error_expression())
  3809  	;
  3810        else if (op == OPERATOR_MULT && expr->is_type_expression())
  3811  	expr = Expression::make_type(Type::make_pointer_type(expr->type()),
  3812  				     location);
  3813        else if (op == OPERATOR_AND && expr->is_composite_literal())
  3814  	expr = Expression::make_heap_expression(expr, location);
  3815        else if (op != OPERATOR_CHANOP)
  3816  	expr = Expression::make_unary(op, expr, location);
  3817        else
  3818  	expr = Expression::make_receive(expr, location);
  3819        return expr;
  3820      }
  3821    else
  3822      return this->primary_expr(may_be_sink, may_be_composite_lit,
  3823  			      is_type_switch, is_parenthesized);
  3824  }
  3825  
  3826  // This is called for the obscure case of
  3827  //   (<- chan <- chan int)(x)
  3828  // In unary_expr we remove the leading <- and parse the remainder,
  3829  // which gives us
  3830  //   chan <- (chan int)
  3831  // When we add the leading <- back in, we really want
  3832  //   <- chan (<- chan int)
  3833  // This means that we need to reassociate.
  3834  
  3835  Type*
  3836  Parse::reassociate_chan_direction(Channel_type *ct, Location location)
  3837  {
  3838    Channel_type* ele = ct->element_type()->channel_type();
  3839    if (ele == NULL)
  3840      {
  3841        go_error_at(location, "parse error");
  3842        return Type::make_error_type();
  3843      }
  3844    Type* sub = ele;
  3845    if (ele->may_send())
  3846      sub = Type::make_channel_type(false, true, ele->element_type());
  3847    else
  3848      sub = this->reassociate_chan_direction(ele, location);
  3849    return Type::make_channel_type(false, true, sub);
  3850  }
  3851  
  3852  // Statement =
  3853  //	Declaration | LabeledStmt | SimpleStmt |
  3854  //	GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
  3855  //	FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
  3856  //	DeferStmt .
  3857  
  3858  // LABEL is the label of this statement if it has one.
  3859  
  3860  void
  3861  Parse::statement(Label* label)
  3862  {
  3863    const Token* token = this->peek_token();
  3864    switch (token->classification())
  3865      {
  3866      case Token::TOKEN_KEYWORD:
  3867        {
  3868  	switch (token->keyword())
  3869  	  {
  3870  	  case KEYWORD_CONST:
  3871  	  case KEYWORD_TYPE:
  3872  	  case KEYWORD_VAR:
  3873  	    this->declaration();
  3874  	    break;
  3875  	  case KEYWORD_FUNC:
  3876  	  case KEYWORD_MAP:
  3877  	  case KEYWORD_STRUCT:
  3878  	  case KEYWORD_INTERFACE:
  3879  	    this->simple_stat(true, NULL, NULL, NULL);
  3880  	    break;
  3881  	  case KEYWORD_GO:
  3882  	  case KEYWORD_DEFER:
  3883  	    this->go_or_defer_stat();
  3884  	    break;
  3885  	  case KEYWORD_RETURN:
  3886  	    this->return_stat();
  3887  	    break;
  3888  	  case KEYWORD_BREAK:
  3889  	    this->break_stat();
  3890  	    break;
  3891  	  case KEYWORD_CONTINUE:
  3892  	    this->continue_stat();
  3893  	    break;
  3894  	  case KEYWORD_GOTO:
  3895  	    this->goto_stat();
  3896  	    break;
  3897  	  case KEYWORD_IF:
  3898  	    this->if_stat();
  3899  	    break;
  3900  	  case KEYWORD_SWITCH:
  3901  	    this->switch_stat(label);
  3902  	    break;
  3903  	  case KEYWORD_SELECT:
  3904  	    this->select_stat(label);
  3905  	    break;
  3906  	  case KEYWORD_FOR:
  3907  	    this->for_stat(label);
  3908  	    break;
  3909  	  default:
  3910  	    go_error_at(this->location(), "expected statement");
  3911  	    this->advance_token();
  3912  	    break;
  3913  	  }
  3914        }
  3915        break;
  3916  
  3917      case Token::TOKEN_IDENTIFIER:
  3918        {
  3919  	std::string identifier = token->identifier();
  3920  	bool is_exported = token->is_identifier_exported();
  3921  	Location location = token->location();
  3922  	if (this->advance_token()->is_op(OPERATOR_COLON))
  3923  	  {
  3924  	    this->advance_token();
  3925  	    this->labeled_stmt(identifier, location);
  3926  	  }
  3927  	else
  3928  	  {
  3929  	    this->unget_token(Token::make_identifier_token(identifier,
  3930  							   is_exported,
  3931  							   location));
  3932  	    this->simple_stat(true, NULL, NULL, NULL);
  3933  	  }
  3934        }
  3935        break;
  3936  
  3937      case Token::TOKEN_OPERATOR:
  3938        if (token->is_op(OPERATOR_LCURLY))
  3939  	{
  3940  	  Location location = token->location();
  3941  	  this->gogo_->start_block(location);
  3942  	  Location end_loc = this->block();
  3943  	  this->gogo_->add_block(this->gogo_->finish_block(end_loc),
  3944  				 location);
  3945  	}
  3946        else if (!token->is_op(OPERATOR_SEMICOLON))
  3947  	this->simple_stat(true, NULL, NULL, NULL);
  3948        break;
  3949  
  3950      case Token::TOKEN_STRING:
  3951      case Token::TOKEN_CHARACTER:
  3952      case Token::TOKEN_INTEGER:
  3953      case Token::TOKEN_FLOAT:
  3954      case Token::TOKEN_IMAGINARY:
  3955        this->simple_stat(true, NULL, NULL, NULL);
  3956        break;
  3957  
  3958      default:
  3959        go_error_at(this->location(), "expected statement");
  3960        this->advance_token();
  3961        break;
  3962      }
  3963  }
  3964  
  3965  bool
  3966  Parse::statement_may_start_here()
  3967  {
  3968    const Token* token = this->peek_token();
  3969    switch (token->classification())
  3970      {
  3971      case Token::TOKEN_KEYWORD:
  3972        {
  3973  	switch (token->keyword())
  3974  	  {
  3975  	  case KEYWORD_CONST:
  3976  	  case KEYWORD_TYPE:
  3977  	  case KEYWORD_VAR:
  3978  	  case KEYWORD_FUNC:
  3979  	  case KEYWORD_MAP:
  3980  	  case KEYWORD_STRUCT:
  3981  	  case KEYWORD_INTERFACE:
  3982  	  case KEYWORD_GO:
  3983  	  case KEYWORD_DEFER:
  3984  	  case KEYWORD_RETURN:
  3985  	  case KEYWORD_BREAK:
  3986  	  case KEYWORD_CONTINUE:
  3987  	  case KEYWORD_GOTO:
  3988  	  case KEYWORD_IF:
  3989  	  case KEYWORD_SWITCH:
  3990  	  case KEYWORD_SELECT:
  3991  	  case KEYWORD_FOR:
  3992  	    return true;
  3993  
  3994  	  default:
  3995  	    return false;
  3996  	  }
  3997        }
  3998        break;
  3999  
  4000      case Token::TOKEN_IDENTIFIER:
  4001        return true;
  4002  
  4003      case Token::TOKEN_OPERATOR:
  4004        if (token->is_op(OPERATOR_LCURLY)
  4005  	  || token->is_op(OPERATOR_SEMICOLON))
  4006  	return true;
  4007        else
  4008  	return this->expression_may_start_here();
  4009  
  4010      case Token::TOKEN_STRING:
  4011      case Token::TOKEN_CHARACTER:
  4012      case Token::TOKEN_INTEGER:
  4013      case Token::TOKEN_FLOAT:
  4014      case Token::TOKEN_IMAGINARY:
  4015        return true;
  4016  
  4017      default:
  4018        return false;
  4019      }
  4020  }
  4021  
  4022  // LabeledStmt = Label ":" Statement .
  4023  // Label       = identifier .
  4024  
  4025  void
  4026  Parse::labeled_stmt(const std::string& label_name, Location location)
  4027  {
  4028    Label* label = this->gogo_->add_label_definition(label_name, location);
  4029  
  4030    if (this->peek_token()->is_op(OPERATOR_RCURLY))
  4031      {
  4032        // This is a label at the end of a block.  A program is
  4033        // permitted to omit a semicolon here.
  4034        return;
  4035      }
  4036  
  4037    if (!this->statement_may_start_here())
  4038      {
  4039        if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
  4040  	{
  4041  	  // We don't treat the fallthrough keyword as a statement,
  4042  	  // because it can't appear most places where a statement is
  4043  	  // permitted, but it may have a label.  We introduce a
  4044  	  // semicolon because the caller expects to see a statement.
  4045  	  this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
  4046  						       location));
  4047  	  return;
  4048  	}
  4049  
  4050        // Mark the label as used to avoid a useless error about an
  4051        // unused label.
  4052        if (label != NULL)
  4053          label->set_is_used();
  4054  
  4055        go_error_at(location, "missing statement after label");
  4056        this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
  4057  						   location));
  4058        return;
  4059      }
  4060  
  4061    this->statement(label);
  4062  }
  4063  
  4064  // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
  4065  //	Assignment | ShortVarDecl .
  4066  
  4067  // EmptyStmt was handled in Parse::statement.
  4068  
  4069  // In order to make this work for if and switch statements, if
  4070  // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
  4071  // expression rather than adding an expression statement to the
  4072  // current block.  If we see something other than an ExpressionStat,
  4073  // we add the statement, set *RETURN_EXP to true if we saw a send
  4074  // statement, and return NULL.  The handling of send statements is for
  4075  // better error messages.
  4076  
  4077  // If P_RANGE_CLAUSE is not NULL, then this will recognize a
  4078  // RangeClause.
  4079  
  4080  // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
  4081  // guard (var := expr.("type") using the literal keyword "type").
  4082  
  4083  Expression*
  4084  Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
  4085  		   Range_clause* p_range_clause, Type_switch* p_type_switch)
  4086  {
  4087    const Token* token = this->peek_token();
  4088  
  4089    // An identifier follow by := is a SimpleVarDecl.
  4090    if (token->is_identifier())
  4091      {
  4092        std::string identifier = token->identifier();
  4093        bool is_exported = token->is_identifier_exported();
  4094        Location location = token->location();
  4095  
  4096        token = this->advance_token();
  4097        if (token->is_op(OPERATOR_COLONEQ)
  4098  	  || token->is_op(OPERATOR_COMMA))
  4099  	{
  4100  	  identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
  4101  	  this->simple_var_decl_or_assignment(identifier, location,
  4102  					      may_be_composite_lit,
  4103  					      p_range_clause,
  4104  					      (token->is_op(OPERATOR_COLONEQ)
  4105  					       ? p_type_switch
  4106  					       : NULL));
  4107  	  return NULL;
  4108  	}
  4109  
  4110        this->unget_token(Token::make_identifier_token(identifier, is_exported,
  4111  						     location));
  4112      }
  4113    else if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
  4114      {
  4115        Typed_identifier_list til;
  4116        this->range_clause_decl(&til, p_range_clause);
  4117        return NULL;
  4118      }
  4119  
  4120    Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
  4121  				     may_be_composite_lit,
  4122  				     (p_type_switch == NULL
  4123  				      ? NULL
  4124  				      : &p_type_switch->found),
  4125  				     NULL);
  4126    if (p_type_switch != NULL && p_type_switch->found)
  4127      {
  4128        p_type_switch->name.clear();
  4129        p_type_switch->location = exp->location();
  4130        p_type_switch->expr = this->verify_not_sink(exp);
  4131        return NULL;
  4132      }
  4133    token = this->peek_token();
  4134    if (token->is_op(OPERATOR_CHANOP))
  4135      {
  4136        this->send_stmt(this->verify_not_sink(exp), may_be_composite_lit);
  4137        if (return_exp != NULL)
  4138  	*return_exp = true;
  4139      }
  4140    else if (token->is_op(OPERATOR_PLUSPLUS)
  4141  	   || token->is_op(OPERATOR_MINUSMINUS))
  4142      this->inc_dec_stat(this->verify_not_sink(exp));
  4143    else if (token->is_op(OPERATOR_COMMA)
  4144  	   || token->is_op(OPERATOR_EQ))
  4145      this->assignment(exp, may_be_composite_lit, p_range_clause);
  4146    else if (token->is_op(OPERATOR_PLUSEQ)
  4147  	   || token->is_op(OPERATOR_MINUSEQ)
  4148  	   || token->is_op(OPERATOR_OREQ)
  4149  	   || token->is_op(OPERATOR_XOREQ)
  4150  	   || token->is_op(OPERATOR_MULTEQ)
  4151  	   || token->is_op(OPERATOR_DIVEQ)
  4152  	   || token->is_op(OPERATOR_MODEQ)
  4153  	   || token->is_op(OPERATOR_LSHIFTEQ)
  4154  	   || token->is_op(OPERATOR_RSHIFTEQ)
  4155  	   || token->is_op(OPERATOR_ANDEQ)
  4156  	   || token->is_op(OPERATOR_BITCLEAREQ))
  4157      this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
  4158  		     p_range_clause);
  4159    else if (return_exp != NULL)
  4160      return this->verify_not_sink(exp);
  4161    else
  4162      {
  4163        exp = this->verify_not_sink(exp);
  4164  
  4165        if (token->is_op(OPERATOR_COLONEQ))
  4166  	{
  4167  	  if (!exp->is_error_expression())
  4168  	    go_error_at(token->location(), "non-name on left side of %<:=%>");
  4169  	  this->gogo_->mark_locals_used();
  4170  	  while (!token->is_op(OPERATOR_SEMICOLON)
  4171  		 && !token->is_eof())
  4172  	    token = this->advance_token();
  4173  	  return NULL;
  4174  	}
  4175  
  4176        this->expression_stat(exp);
  4177      }
  4178  
  4179    return NULL;
  4180  }
  4181  
  4182  bool
  4183  Parse::simple_stat_may_start_here()
  4184  {
  4185    return this->expression_may_start_here();
  4186  }
  4187  
  4188  // Parse { Statement ";" } which is used in a few places.  The list of
  4189  // statements may end with a right curly brace, in which case the
  4190  // semicolon may be omitted.
  4191  
  4192  void
  4193  Parse::statement_list()
  4194  {
  4195    while (this->statement_may_start_here())
  4196      {
  4197        this->statement(NULL);
  4198        if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4199  	this->advance_token();
  4200        else if (this->peek_token()->is_op(OPERATOR_RCURLY))
  4201  	break;
  4202        else
  4203  	{
  4204  	  if (!this->peek_token()->is_eof() || !saw_errors())
  4205  	    go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
  4206  	  if (!this->skip_past_error(OPERATOR_RCURLY))
  4207  	    return;
  4208  	}
  4209      }
  4210  }
  4211  
  4212  bool
  4213  Parse::statement_list_may_start_here()
  4214  {
  4215    return this->statement_may_start_here();
  4216  }
  4217  
  4218  // ExpressionStat = Expression .
  4219  
  4220  void
  4221  Parse::expression_stat(Expression* exp)
  4222  {
  4223    this->gogo_->add_statement(Statement::make_statement(exp, false));
  4224  }
  4225  
  4226  // SendStmt = Channel "&lt;-" Expression .
  4227  // Channel  = Expression .
  4228  
  4229  void
  4230  Parse::send_stmt(Expression* channel, bool may_be_composite_lit)
  4231  {
  4232    go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
  4233    Location loc = this->location();
  4234    this->advance_token();
  4235    Expression* val = this->expression(PRECEDENCE_NORMAL, false,
  4236  				     may_be_composite_lit, NULL, NULL);
  4237    Statement* s = Statement::make_send_statement(channel, val, loc);
  4238    this->gogo_->add_statement(s);
  4239  }
  4240  
  4241  // IncDecStat = Expression ( "++" | "--" ) .
  4242  
  4243  void
  4244  Parse::inc_dec_stat(Expression* exp)
  4245  {
  4246    const Token* token = this->peek_token();
  4247    if (token->is_op(OPERATOR_PLUSPLUS))
  4248      this->gogo_->add_statement(Statement::make_inc_statement(exp));
  4249    else if (token->is_op(OPERATOR_MINUSMINUS))
  4250      this->gogo_->add_statement(Statement::make_dec_statement(exp));
  4251    else
  4252      go_unreachable();
  4253    this->advance_token();
  4254  }
  4255  
  4256  // Assignment = ExpressionList assign_op ExpressionList .
  4257  
  4258  // EXP is an expression that we have already parsed.
  4259  
  4260  // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
  4261  // side may be a composite literal.
  4262  
  4263  // If RANGE_CLAUSE is not NULL, then this will recognize a
  4264  // RangeClause.
  4265  
  4266  void
  4267  Parse::assignment(Expression* expr, bool may_be_composite_lit,
  4268  		  Range_clause* p_range_clause)
  4269  {
  4270    Expression_list* vars;
  4271    if (!this->peek_token()->is_op(OPERATOR_COMMA))
  4272      {
  4273        vars = new Expression_list();
  4274        vars->push_back(expr);
  4275      }
  4276    else
  4277      {
  4278        this->advance_token();
  4279        vars = this->expression_list(expr, true, may_be_composite_lit);
  4280      }
  4281  
  4282    this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
  4283  }
  4284  
  4285  // An assignment statement.  LHS is the list of expressions which
  4286  // appear on the left hand side.
  4287  
  4288  // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
  4289  // side may be a composite literal.
  4290  
  4291  // If RANGE_CLAUSE is not NULL, then this will recognize a
  4292  // RangeClause.
  4293  
  4294  void
  4295  Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
  4296  			Range_clause* p_range_clause)
  4297  {
  4298    const Token* token = this->peek_token();
  4299    if (!token->is_op(OPERATOR_EQ)
  4300        && !token->is_op(OPERATOR_PLUSEQ)
  4301        && !token->is_op(OPERATOR_MINUSEQ)
  4302        && !token->is_op(OPERATOR_OREQ)
  4303        && !token->is_op(OPERATOR_XOREQ)
  4304        && !token->is_op(OPERATOR_MULTEQ)
  4305        && !token->is_op(OPERATOR_DIVEQ)
  4306        && !token->is_op(OPERATOR_MODEQ)
  4307        && !token->is_op(OPERATOR_LSHIFTEQ)
  4308        && !token->is_op(OPERATOR_RSHIFTEQ)
  4309        && !token->is_op(OPERATOR_ANDEQ)
  4310        && !token->is_op(OPERATOR_BITCLEAREQ))
  4311      {
  4312        go_error_at(this->location(), "expected assignment operator");
  4313        return;
  4314      }
  4315    Operator op = token->op();
  4316    Location location = token->location();
  4317  
  4318    token = this->advance_token();
  4319  
  4320    if (lhs == NULL)
  4321      return;
  4322  
  4323    if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
  4324      {
  4325        if (op != OPERATOR_EQ)
  4326  	go_error_at(this->location(), "range clause requires %<=%>");
  4327        this->range_clause_expr(lhs, p_range_clause);
  4328        return;
  4329      }
  4330  
  4331    Expression_list* vals = this->expression_list(NULL, false,
  4332  						may_be_composite_lit);
  4333  
  4334    // We've parsed everything; check for errors.
  4335    if (vals == NULL)
  4336      return;
  4337    for (Expression_list::const_iterator pe = lhs->begin();
  4338         pe != lhs->end();
  4339         ++pe)
  4340      {
  4341        if ((*pe)->is_error_expression())
  4342  	return;
  4343        if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
  4344  	go_error_at((*pe)->location(), "cannot use %<_%> as value");
  4345      }
  4346    for (Expression_list::const_iterator pe = vals->begin();
  4347         pe != vals->end();
  4348         ++pe)
  4349      {
  4350        if ((*pe)->is_error_expression())
  4351  	return;
  4352      }
  4353  
  4354    Call_expression* call;
  4355    Index_expression* map_index;
  4356    Receive_expression* receive;
  4357    Type_guard_expression* type_guard;
  4358    if (lhs->size() == vals->size())
  4359      {
  4360        Statement* s;
  4361        if (lhs->size() > 1)
  4362  	{
  4363  	  if (op != OPERATOR_EQ)
  4364  	    go_error_at(location, "multiple values only permitted with %<=%>");
  4365  	  s = Statement::make_tuple_assignment(lhs, vals, location);
  4366  	}
  4367        else
  4368  	{
  4369  	  if (op == OPERATOR_EQ)
  4370  	    s = Statement::make_assignment(lhs->front(), vals->front(),
  4371  					   location);
  4372  	  else
  4373  	    s = Statement::make_assignment_operation(op, lhs->front(),
  4374  						     vals->front(), location);
  4375  	  delete lhs;
  4376  	  delete vals;
  4377  	}
  4378        this->gogo_->add_statement(s);
  4379      }
  4380    else if (vals->size() == 1
  4381  	   && (call = (*vals->begin())->call_expression()) != NULL)
  4382      {
  4383        if (op != OPERATOR_EQ)
  4384  	go_error_at(location, "multiple results only permitted with %<=%>");
  4385        call->set_expected_result_count(lhs->size());
  4386        delete vals;
  4387        vals = new Expression_list;
  4388        for (unsigned int i = 0; i < lhs->size(); ++i)
  4389  	vals->push_back(Expression::make_call_result(call, i));
  4390        Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
  4391        this->gogo_->add_statement(s);
  4392      }
  4393    else if (lhs->size() == 2
  4394  	   && vals->size() == 1
  4395  	   && (map_index = (*vals->begin())->index_expression()) != NULL)
  4396      {
  4397        if (op != OPERATOR_EQ)
  4398  	go_error_at(location, "two values from map requires %<=%>");
  4399        Expression* val = lhs->front();
  4400        Expression* present = lhs->back();
  4401        Statement* s = Statement::make_tuple_map_assignment(val, present,
  4402  							  map_index, location);
  4403        this->gogo_->add_statement(s);
  4404      }
  4405    else if (lhs->size() == 2
  4406  	   && vals->size() == 1
  4407  	   && (receive = (*vals->begin())->receive_expression()) != NULL)
  4408      {
  4409        if (op != OPERATOR_EQ)
  4410  	go_error_at(location, "two values from receive requires %<=%>");
  4411        Expression* val = lhs->front();
  4412        Expression* success = lhs->back();
  4413        Expression* channel = receive->channel();
  4414        Statement* s = Statement::make_tuple_receive_assignment(val, success,
  4415  							      channel,
  4416  							      location);
  4417        this->gogo_->add_statement(s);
  4418      }
  4419    else if (lhs->size() == 2
  4420  	   && vals->size() == 1
  4421  	   && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
  4422      {
  4423        if (op != OPERATOR_EQ)
  4424  	go_error_at(location, "two values from type guard requires %<=%>");
  4425        Expression* val = lhs->front();
  4426        Expression* ok = lhs->back();
  4427        Expression* expr = type_guard->expr();
  4428        Type* type = type_guard->type();
  4429        Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
  4430  								 expr, type,
  4431  								 location);
  4432        this->gogo_->add_statement(s);
  4433      }
  4434    else
  4435      {
  4436        go_error_at(location, ("number of variables does not "
  4437                               "match number of values"));
  4438      }
  4439  }
  4440  
  4441  // GoStat = "go" Expression .
  4442  // DeferStat = "defer" Expression .
  4443  
  4444  void
  4445  Parse::go_or_defer_stat()
  4446  {
  4447    go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
  4448  	     || this->peek_token()->is_keyword(KEYWORD_DEFER));
  4449    bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
  4450    Location stat_location = this->location();
  4451  
  4452    this->advance_token();
  4453    Location expr_location = this->location();
  4454  
  4455    bool is_parenthesized = false;
  4456    Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
  4457  				      &is_parenthesized);
  4458    Call_expression* call_expr = expr->call_expression();
  4459    if (is_parenthesized || call_expr == NULL)
  4460      {
  4461        go_error_at(expr_location, "argument to go/defer must be function call");
  4462        return;
  4463      }
  4464  
  4465    // Make it easier to simplify go/defer statements by putting every
  4466    // statement in its own block.
  4467    this->gogo_->start_block(stat_location);
  4468    Statement* stat;
  4469    if (is_go)
  4470      {
  4471        stat = Statement::make_go_statement(call_expr, stat_location);
  4472        call_expr->set_is_concurrent();
  4473      }
  4474    else
  4475      {
  4476        stat = Statement::make_defer_statement(call_expr, stat_location);
  4477        call_expr->set_is_deferred();
  4478      }
  4479    this->gogo_->add_statement(stat);
  4480    this->gogo_->add_block(this->gogo_->finish_block(stat_location),
  4481  			 stat_location);
  4482  }
  4483  
  4484  // ReturnStat = "return" [ ExpressionList ] .
  4485  
  4486  void
  4487  Parse::return_stat()
  4488  {
  4489    go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
  4490    Location location = this->location();
  4491    this->advance_token();
  4492    Expression_list* vals = NULL;
  4493    if (this->expression_may_start_here())
  4494      vals = this->expression_list(NULL, false, true);
  4495    this->gogo_->add_statement(Statement::make_return_statement(vals, location));
  4496  
  4497    if (vals == NULL
  4498        && this->gogo_->current_function()->func_value()->results_are_named())
  4499      {
  4500        Named_object* function = this->gogo_->current_function();
  4501        Function::Results* results = function->func_value()->result_variables();
  4502        for (Function::Results::const_iterator p = results->begin();
  4503  	   p != results->end();
  4504  	   ++p)
  4505  	{
  4506  	  Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
  4507  	  if (no == NULL)
  4508  	    go_assert(saw_errors());
  4509  	  else if (!no->is_result_variable())
  4510  	    go_error_at(location, "%qs is shadowed during return",
  4511  			(*p)->message_name().c_str());
  4512  	}
  4513      }
  4514  }
  4515  
  4516  // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
  4517  //          [ "else" ( IfStmt | Block ) ] .
  4518  
  4519  void
  4520  Parse::if_stat()
  4521  {
  4522    go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
  4523    Location location = this->location();
  4524    this->advance_token();
  4525  
  4526    this->gogo_->start_block(location);
  4527  
  4528    bool saw_simple_stat = false;
  4529    Expression* cond = NULL;
  4530    bool saw_send_stmt = false;
  4531    if (this->simple_stat_may_start_here())
  4532      {
  4533        cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
  4534        saw_simple_stat = true;
  4535      }
  4536    if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4537      {
  4538        // The SimpleStat is an expression statement.
  4539        this->expression_stat(cond);
  4540        cond = NULL;
  4541      }
  4542    if (cond == NULL)
  4543      {
  4544        if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4545  	this->advance_token();
  4546        else if (saw_simple_stat)
  4547  	{
  4548  	  if (saw_send_stmt)
  4549  	    go_error_at(this->location(),
  4550  			("send statement used as value; "
  4551  			 "use select for non-blocking send"));
  4552  	  else
  4553  	    go_error_at(this->location(),
  4554  			"expected %<;%> after statement in if expression");
  4555  	  if (!this->expression_may_start_here())
  4556  	    cond = Expression::make_error(this->location());
  4557  	}
  4558        if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
  4559  	{
  4560  	  go_error_at(this->location(),
  4561  		      "missing condition in if statement");
  4562  	  cond = Expression::make_error(this->location());
  4563  	}
  4564        if (cond == NULL)
  4565  	cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL, NULL);
  4566      }
  4567  
  4568    // Check for the easy error of a newline before starting the block.
  4569    if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4570      {
  4571        Location semi_loc = this->location();
  4572        if (this->advance_token()->is_op(OPERATOR_LCURLY))
  4573  	go_error_at(semi_loc, "unexpected semicolon or newline, expecting %<{%> after if clause");
  4574        // Otherwise we will get an error when we call this->block
  4575        // below.
  4576      }
  4577  
  4578    this->gogo_->start_block(this->location());
  4579    Location end_loc = this->block();
  4580    Block* then_block = this->gogo_->finish_block(end_loc);
  4581  
  4582    // Check for the easy error of a newline before "else".
  4583    if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4584      {
  4585        Location semi_loc = this->location();
  4586        if (this->advance_token()->is_keyword(KEYWORD_ELSE))
  4587  	go_error_at(this->location(),
  4588  		 "unexpected semicolon or newline before %<else%>");
  4589        else
  4590  	this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
  4591  						     semi_loc));
  4592      }
  4593  
  4594    Block* else_block = NULL;
  4595    if (this->peek_token()->is_keyword(KEYWORD_ELSE))
  4596      {
  4597        this->gogo_->start_block(this->location());
  4598        const Token* token = this->advance_token();
  4599        if (token->is_keyword(KEYWORD_IF))
  4600  	this->if_stat();
  4601        else if (token->is_op(OPERATOR_LCURLY))
  4602  	this->block();
  4603        else
  4604  	{
  4605  	  go_error_at(this->location(), "expected %<if%> or %<{%>");
  4606  	  this->statement(NULL);
  4607  	}
  4608        else_block = this->gogo_->finish_block(this->location());
  4609      }
  4610  
  4611    this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
  4612  							  else_block,
  4613  							  location));
  4614  
  4615    this->gogo_->add_block(this->gogo_->finish_block(this->location()),
  4616  			 location);
  4617  }
  4618  
  4619  // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
  4620  // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
  4621  //			"{" { ExprCaseClause } "}" .
  4622  // TypeSwitchStmt  = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
  4623  //			"{" { TypeCaseClause } "}" .
  4624  // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
  4625  
  4626  void
  4627  Parse::switch_stat(Label* label)
  4628  {
  4629    go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
  4630    Location location = this->location();
  4631    this->advance_token();
  4632  
  4633    this->gogo_->start_block(location);
  4634  
  4635    bool saw_simple_stat = false;
  4636    Expression* switch_val = NULL;
  4637    bool saw_send_stmt = false;
  4638    Type_switch type_switch;
  4639    bool have_type_switch_block = false;
  4640    if (this->simple_stat_may_start_here())
  4641      {
  4642        switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
  4643  				     &type_switch);
  4644        saw_simple_stat = true;
  4645      }
  4646    if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4647      {
  4648        // The SimpleStat is an expression statement.
  4649        this->expression_stat(switch_val);
  4650        switch_val = NULL;
  4651      }
  4652    if (switch_val == NULL && !type_switch.found)
  4653      {
  4654        if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4655  	this->advance_token();
  4656        else if (saw_simple_stat)
  4657  	{
  4658  	  if (saw_send_stmt)
  4659  	    go_error_at(this->location(),
  4660  			("send statement used as value; "
  4661  			 "use select for non-blocking send"));
  4662  	  else
  4663  	    go_error_at(this->location(),
  4664  			"expected %<;%> after statement in switch expression");
  4665  	}
  4666        if (!this->peek_token()->is_op(OPERATOR_LCURLY))
  4667  	{
  4668  	  if (this->peek_token()->is_identifier())
  4669  	    {
  4670  	      const Token* token = this->peek_token();
  4671  	      std::string identifier = token->identifier();
  4672  	      bool is_exported = token->is_identifier_exported();
  4673  	      Location id_loc = token->location();
  4674  
  4675  	      token = this->advance_token();
  4676  	      bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
  4677  	      this->unget_token(Token::make_identifier_token(identifier,
  4678  							     is_exported,
  4679  							     id_loc));
  4680  	      if (is_coloneq)
  4681  		{
  4682  		  // This must be a TypeSwitchGuard.  It is in a
  4683  		  // different block from any initial SimpleStat.
  4684  		  if (saw_simple_stat)
  4685  		    {
  4686  		      this->gogo_->start_block(id_loc);
  4687  		      have_type_switch_block = true;
  4688  		    }
  4689  
  4690  		  switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
  4691  						 &type_switch);
  4692  		  if (!type_switch.found)
  4693  		    {
  4694  		      if (switch_val == NULL
  4695  			  || !switch_val->is_error_expression())
  4696  			{
  4697  			  go_error_at(id_loc,
  4698  				      "expected type switch assignment");
  4699  			  switch_val = Expression::make_error(id_loc);
  4700  			}
  4701  		    }
  4702  		}
  4703  	    }
  4704  	  if (switch_val == NULL && !type_switch.found)
  4705  	    {
  4706  	      switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
  4707  					    &type_switch.found, NULL);
  4708  	      if (type_switch.found)
  4709  		{
  4710  		  type_switch.name.clear();
  4711  		  type_switch.expr = switch_val;
  4712  		  type_switch.location = switch_val->location();
  4713  		}
  4714  	    }
  4715  	}
  4716      }
  4717  
  4718    if (!this->peek_token()->is_op(OPERATOR_LCURLY))
  4719      {
  4720        Location token_loc = this->location();
  4721        if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
  4722  	  && this->advance_token()->is_op(OPERATOR_LCURLY))
  4723  	go_error_at(token_loc, "missing %<{%> after switch clause");
  4724        else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
  4725  	{
  4726  	  go_error_at(token_loc, "invalid variable name");
  4727  	  this->advance_token();
  4728  	  this->expression(PRECEDENCE_NORMAL, false, false,
  4729  			   &type_switch.found, NULL);
  4730  	  if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  4731  	    this->advance_token();
  4732  	  if (!this->peek_token()->is_op(OPERATOR_LCURLY))
  4733  	    {
  4734  	      if (have_type_switch_block)
  4735  		this->gogo_->add_block(this->gogo_->finish_block(location),
  4736  				       location);
  4737  	      this->gogo_->add_block(this->gogo_->finish_block(location),
  4738  				     location);
  4739  	      return;
  4740  	    }
  4741  	  if (type_switch.found)
  4742  	    type_switch.expr = Expression::make_error(location);
  4743  	}
  4744        else
  4745  	{
  4746  	  go_error_at(this->location(), "expected %<{%>");
  4747  	  if (have_type_switch_block)
  4748  	    this->gogo_->add_block(this->gogo_->finish_block(this->location()),
  4749  				   location);
  4750  	  this->gogo_->add_block(this->gogo_->finish_block(this->location()),
  4751  				 location);
  4752  	  return;
  4753  	}
  4754      }
  4755    this->advance_token();
  4756  
  4757    Statement* statement;
  4758    if (type_switch.found)
  4759      statement = this->type_switch_body(label, type_switch, location);
  4760    else
  4761      statement = this->expr_switch_body(label, switch_val, location);
  4762  
  4763    if (statement != NULL)
  4764      this->gogo_->add_statement(statement);
  4765  
  4766    if (have_type_switch_block)
  4767      this->gogo_->add_block(this->gogo_->finish_block(this->location()),
  4768  			   location);
  4769  
  4770    this->gogo_->add_block(this->gogo_->finish_block(this->location()),
  4771  			 location);
  4772  }
  4773  
  4774  // The body of an expression switch.
  4775  //   "{" { ExprCaseClause } "}"
  4776  
  4777  Statement*
  4778  Parse::expr_switch_body(Label* label, Expression* switch_val,
  4779  			Location location)
  4780  {
  4781    Switch_statement* statement = Statement::make_switch_statement(switch_val,
  4782  								 location);
  4783  
  4784    this->push_break_statement(statement, label);
  4785  
  4786    Case_clauses* case_clauses = new Case_clauses();
  4787    bool saw_default = false;
  4788    while (!this->peek_token()->is_op(OPERATOR_RCURLY))
  4789      {
  4790        if (this->peek_token()->is_eof())
  4791  	{
  4792  	  if (!saw_errors())
  4793  	    go_error_at(this->location(), "missing %<}%>");
  4794  	  return NULL;
  4795  	}
  4796        this->expr_case_clause(case_clauses, &saw_default);
  4797      }
  4798    this->advance_token();
  4799  
  4800    statement->add_clauses(case_clauses);
  4801  
  4802    this->pop_break_statement();
  4803  
  4804    return statement;
  4805  }
  4806  
  4807  // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
  4808  // FallthroughStat = "fallthrough" .
  4809  
  4810  void
  4811  Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
  4812  {
  4813    Location location = this->location();
  4814  
  4815    bool is_default = false;
  4816    Expression_list* vals = this->expr_switch_case(&is_default);
  4817  
  4818    if (!this->peek_token()->is_op(OPERATOR_COLON))
  4819      {
  4820        if (!saw_errors())
  4821  	go_error_at(this->location(), "expected %<:%>");
  4822        return;
  4823      }
  4824    else
  4825      this->advance_token();
  4826  
  4827    Block* statements = NULL;
  4828    if (this->statement_list_may_start_here())
  4829      {
  4830        this->gogo_->start_block(this->location());
  4831        this->statement_list();
  4832        statements = this->gogo_->finish_block(this->location());
  4833      }
  4834  
  4835    bool is_fallthrough = false;
  4836    if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
  4837      {
  4838        Location fallthrough_loc = this->location();
  4839        is_fallthrough = true;
  4840        while (this->advance_token()->is_op(OPERATOR_SEMICOLON))
  4841  	;
  4842        if (this->peek_token()->is_op(OPERATOR_RCURLY))
  4843  	go_error_at(fallthrough_loc,
  4844  		    _("cannot fallthrough final case in switch"));
  4845        else if (!this->peek_token()->is_keyword(KEYWORD_CASE)
  4846  	       && !this->peek_token()->is_keyword(KEYWORD_DEFAULT))
  4847  	{
  4848  	  go_error_at(fallthrough_loc, "fallthrough statement out of place");
  4849  	  while (!this->peek_token()->is_keyword(KEYWORD_CASE)
  4850  		 && !this->peek_token()->is_keyword(KEYWORD_DEFAULT)
  4851  		 && !this->peek_token()->is_op(OPERATOR_RCURLY)
  4852  		 && !this->peek_token()->is_eof())
  4853  	    {
  4854  	      if (this->statement_may_start_here())
  4855  		this->statement_list();
  4856  	      else
  4857  		this->advance_token();
  4858  	    }
  4859  	}
  4860      }
  4861  
  4862    if (is_default)
  4863      {
  4864        if (*saw_default)
  4865  	{
  4866  	  go_error_at(location, "multiple defaults in switch");
  4867  	  return;
  4868  	}
  4869        *saw_default = true;
  4870      }
  4871  
  4872    if (is_default || vals != NULL)
  4873      clauses->add(vals, is_default, statements, is_fallthrough, location);
  4874  }
  4875  
  4876  // ExprSwitchCase = "case" ExpressionList | "default" .
  4877  
  4878  Expression_list*
  4879  Parse::expr_switch_case(bool* is_default)
  4880  {
  4881    const Token* token = this->peek_token();
  4882    if (token->is_keyword(KEYWORD_CASE))
  4883      {
  4884        this->advance_token();
  4885        return this->expression_list(NULL, false, true);
  4886      }
  4887    else if (token->is_keyword(KEYWORD_DEFAULT))
  4888      {
  4889        this->advance_token();
  4890        *is_default = true;
  4891        return NULL;
  4892      }
  4893    else
  4894      {
  4895        if (!saw_errors())
  4896  	go_error_at(this->location(), "expected %<case%> or %<default%>");
  4897        if (!token->is_op(OPERATOR_RCURLY))
  4898  	this->advance_token();
  4899        return NULL;
  4900      }
  4901  }
  4902  
  4903  // The body of a type switch.
  4904  //   "{" { TypeCaseClause } "}" .
  4905  
  4906  Statement*
  4907  Parse::type_switch_body(Label* label, const Type_switch& type_switch,
  4908  			Location location)
  4909  {
  4910    Expression* init = type_switch.expr;
  4911    std::string var_name = type_switch.name;
  4912    if (!var_name.empty())
  4913      {
  4914        if (Gogo::is_sink_name(var_name))
  4915          {
  4916  	  go_error_at(type_switch.location,
  4917  		      "no new variables on left side of %<:=%>");
  4918            var_name.clear();
  4919          }
  4920        else
  4921  	{
  4922            Location loc = type_switch.location;
  4923  	  Temporary_statement* switch_temp =
  4924                Statement::make_temporary(NULL, init, loc);
  4925  	  this->gogo_->add_statement(switch_temp);
  4926            init = Expression::make_temporary_reference(switch_temp, loc);
  4927  	}
  4928      }
  4929  
  4930    Type_switch_statement* statement =
  4931        Statement::make_type_switch_statement(var_name, init, location);
  4932    this->push_break_statement(statement, label);
  4933  
  4934    Type_case_clauses* case_clauses = new Type_case_clauses();
  4935    bool saw_default = false;
  4936    std::vector<Named_object*> implicit_vars;
  4937    while (!this->peek_token()->is_op(OPERATOR_RCURLY))
  4938      {
  4939        if (this->peek_token()->is_eof())
  4940  	{
  4941  	  go_error_at(this->location(), "missing %<}%>");
  4942  	  return NULL;
  4943  	}
  4944        this->type_case_clause(var_name, init, case_clauses, &saw_default,
  4945                               &implicit_vars);
  4946      }
  4947    this->advance_token();
  4948  
  4949    statement->add_clauses(case_clauses);
  4950  
  4951    this->pop_break_statement();
  4952  
  4953    // If there is a type switch variable implicitly declared in each case clause,
  4954    // check that it is used in at least one of the cases.
  4955    if (!var_name.empty())
  4956      {
  4957        bool used = false;
  4958        for (std::vector<Named_object*>::iterator p = implicit_vars.begin();
  4959  	   p != implicit_vars.end();
  4960  	   ++p)
  4961  	{
  4962  	  if ((*p)->var_value()->is_used())
  4963  	    {
  4964  	      used = true;
  4965  	      break;
  4966  	    }
  4967  	}
  4968        if (!used)
  4969  	go_error_at(type_switch.location, "%qs declared but not used",
  4970  		    Gogo::message_name(var_name).c_str());
  4971      }
  4972    return statement;
  4973  }
  4974  
  4975  // TypeCaseClause  = TypeSwitchCase ":" [ StatementList ] .
  4976  // IMPLICIT_VARS is the list of variables implicitly declared for each type
  4977  // case if there is a type switch variable declared.
  4978  
  4979  void
  4980  Parse::type_case_clause(const std::string& var_name, Expression* init,
  4981                          Type_case_clauses* clauses, bool* saw_default,
  4982  			std::vector<Named_object*>* implicit_vars)
  4983  {
  4984    Location location = this->location();
  4985  
  4986    std::vector<Type*> types;
  4987    bool is_default = false;
  4988    this->type_switch_case(&types, &is_default);
  4989  
  4990    if (!this->peek_token()->is_op(OPERATOR_COLON))
  4991      go_error_at(this->location(), "expected %<:%>");
  4992    else
  4993      this->advance_token();
  4994  
  4995    Block* statements = NULL;
  4996    if (this->statement_list_may_start_here())
  4997      {
  4998        this->gogo_->start_block(this->location());
  4999        if (!var_name.empty())
  5000  	{
  5001  	  Type* type = NULL;
  5002            Location var_loc = init->location();
  5003  	  if (types.size() == 1)
  5004  	    {
  5005  	      type = types.front();
  5006  	      init = Expression::make_type_guard(init, type, location);
  5007  	    }
  5008  
  5009  	  Variable* v = new Variable(type, init, false, false, false,
  5010  				     var_loc);
  5011  	  v->set_is_used();
  5012  	  v->set_is_type_switch_var();
  5013  	  implicit_vars->push_back(this->gogo_->add_variable(var_name, v));
  5014  	}
  5015        this->statement_list();
  5016        statements = this->gogo_->finish_block(this->location());
  5017      }
  5018  
  5019    if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
  5020      {
  5021        go_error_at(this->location(),
  5022  		  "fallthrough is not permitted in a type switch");
  5023        if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
  5024  	this->advance_token();
  5025      }
  5026  
  5027    if (is_default)
  5028      {
  5029        go_assert(types.empty());
  5030        if (*saw_default)
  5031  	{
  5032  	  go_error_at(location, "multiple defaults in type switch");
  5033  	  return;
  5034  	}
  5035        *saw_default = true;
  5036        clauses->add(NULL, false, true, statements, location);
  5037      }
  5038    else if (!types.empty())
  5039      {
  5040        for (std::vector<Type*>::const_iterator p = types.begin();
  5041  	   p + 1 != types.end();
  5042  	   ++p)
  5043  	clauses->add(*p, true, false, NULL, location);
  5044        clauses->add(types.back(), false, false, statements, location);
  5045      }
  5046    else
  5047      clauses->add(Type::make_error_type(), false, false, statements, location);
  5048  }
  5049  
  5050  // TypeSwitchCase  = "case" type | "default"
  5051  
  5052  // We accept a comma separated list of types.
  5053  
  5054  void
  5055  Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
  5056  {
  5057    const Token* token = this->peek_token();
  5058    if (token->is_keyword(KEYWORD_CASE))
  5059      {
  5060        this->advance_token();
  5061        while (true)
  5062  	{
  5063  	  Type* t = this->type();
  5064  
  5065  	  if (!t->is_error_type())
  5066  	    types->push_back(t);
  5067  	  else
  5068  	    {
  5069  	      this->gogo_->mark_locals_used();
  5070  	      token = this->peek_token();
  5071  	      while (!token->is_op(OPERATOR_COLON)
  5072  		     && !token->is_op(OPERATOR_COMMA)
  5073  		     && !token->is_op(OPERATOR_RCURLY)
  5074  		     && !token->is_eof())
  5075  		token = this->advance_token();
  5076  	    }
  5077  
  5078  	  if (!this->peek_token()->is_op(OPERATOR_COMMA))
  5079  	    break;
  5080  	  this->advance_token();
  5081  	}
  5082      }
  5083    else if (token->is_keyword(KEYWORD_DEFAULT))
  5084      {
  5085        this->advance_token();
  5086        *is_default = true;
  5087      }
  5088    else
  5089      {
  5090        go_error_at(this->location(), "expected %<case%> or %<default%>");
  5091        if (!token->is_op(OPERATOR_RCURLY))
  5092  	this->advance_token();
  5093      }
  5094  }
  5095  
  5096  // SelectStat = "select" "{" { CommClause } "}" .
  5097  
  5098  void
  5099  Parse::select_stat(Label* label)
  5100  {
  5101    go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
  5102    Location location = this->location();
  5103    const Token* token = this->advance_token();
  5104  
  5105    if (!token->is_op(OPERATOR_LCURLY))
  5106      {
  5107        Location token_loc = token->location();
  5108        if (token->is_op(OPERATOR_SEMICOLON)
  5109  	  && this->advance_token()->is_op(OPERATOR_LCURLY))
  5110  	go_error_at(token_loc, "unexpected semicolon or newline before %<{%>");
  5111        else
  5112  	{
  5113  	  go_error_at(this->location(), "expected %<{%>");
  5114  	  return;
  5115  	}
  5116      }
  5117    this->advance_token();
  5118  
  5119    Select_statement* statement = Statement::make_select_statement(location);
  5120  
  5121    this->push_break_statement(statement, label);
  5122  
  5123    Select_clauses* select_clauses = new Select_clauses();
  5124    bool saw_default = false;
  5125    while (!this->peek_token()->is_op(OPERATOR_RCURLY))
  5126      {
  5127        if (this->peek_token()->is_eof())
  5128  	{
  5129  	  go_error_at(this->location(), "expected %<}%>");
  5130  	  return;
  5131  	}
  5132        this->comm_clause(select_clauses, &saw_default);
  5133      }
  5134  
  5135    this->advance_token();
  5136  
  5137    statement->add_clauses(select_clauses);
  5138  
  5139    this->pop_break_statement();
  5140  
  5141    this->gogo_->add_statement(statement);
  5142  }
  5143  
  5144  // CommClause = CommCase ":" { Statement ";" } .
  5145  
  5146  void
  5147  Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
  5148  {
  5149    Location location = this->location();
  5150    bool is_send = false;
  5151    Expression* channel = NULL;
  5152    Expression* val = NULL;
  5153    Expression* closed = NULL;
  5154    std::string varname;
  5155    std::string closedname;
  5156    bool is_default = false;
  5157    bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
  5158  				  &varname, &closedname, &is_default);
  5159  
  5160    if (this->peek_token()->is_op(OPERATOR_COLON))
  5161      this->advance_token();
  5162    else
  5163      go_error_at(this->location(), "expected colon");
  5164  
  5165    this->gogo_->start_block(this->location());
  5166  
  5167    Named_object* var = NULL;
  5168    if (!varname.empty())
  5169      {
  5170        // FIXME: LOCATION is slightly wrong here.
  5171        Variable* v = new Variable(NULL, channel, false, false, false,
  5172  				 location);
  5173        v->set_type_from_chan_element();
  5174        var = this->gogo_->add_variable(varname, v);
  5175      }
  5176  
  5177    Named_object* closedvar = NULL;
  5178    if (!closedname.empty())
  5179      {
  5180        // FIXME: LOCATION is slightly wrong here.
  5181        Variable* v = new Variable(Type::lookup_bool_type(), NULL,
  5182  				 false, false, false, location);
  5183        closedvar = this->gogo_->add_variable(closedname, v);
  5184      }
  5185  
  5186    this->statement_list();
  5187  
  5188    Block* statements = this->gogo_->finish_block(this->location());
  5189  
  5190    if (is_default)
  5191      {
  5192        if (*saw_default)
  5193  	{
  5194  	  go_error_at(location, "multiple defaults in select");
  5195  	  return;
  5196  	}
  5197        *saw_default = true;
  5198      }
  5199  
  5200    if (got_case)
  5201      clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
  5202  		 statements, location);
  5203    else if (statements != NULL)
  5204      {
  5205        // Add the statements to make sure that any names they define
  5206        // are traversed.
  5207        this->gogo_->add_block(statements, location);
  5208      }
  5209  }
  5210  
  5211  // CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
  5212  
  5213  bool
  5214  Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
  5215  		 Expression** closed, std::string* varname,
  5216  		 std::string* closedname, bool* is_default)
  5217  {
  5218    const Token* token = this->peek_token();
  5219    if (token->is_keyword(KEYWORD_DEFAULT))
  5220      {
  5221        this->advance_token();
  5222        *is_default = true;
  5223      }
  5224    else if (token->is_keyword(KEYWORD_CASE))
  5225      {
  5226        this->advance_token();
  5227        if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
  5228  				   closedname))
  5229  	return false;
  5230      }
  5231    else
  5232      {
  5233        go_error_at(this->location(), "expected %<case%> or %<default%>");
  5234        if (!token->is_op(OPERATOR_RCURLY))
  5235  	this->advance_token();
  5236        return false;
  5237      }
  5238  
  5239    return true;
  5240  }
  5241  
  5242  // RecvStmt   = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
  5243  // RecvExpr   = Expression .
  5244  
  5245  bool
  5246  Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
  5247  			 Expression** closed, std::string* varname,
  5248  			 std::string* closedname)
  5249  {
  5250    const Token* token = this->peek_token();
  5251    bool saw_comma = false;
  5252    bool closed_is_id = false;
  5253    if (token->is_identifier())
  5254      {
  5255        Gogo* gogo = this->gogo_;
  5256        std::string recv_var = token->identifier();
  5257        bool is_rv_exported = token->is_identifier_exported();
  5258        Location recv_var_loc = token->location();
  5259        token = this->advance_token();
  5260        if (token->is_op(OPERATOR_COLONEQ))
  5261  	{
  5262  	  // case rv := <-c:
  5263  	  this->advance_token();
  5264  	  Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
  5265  					   NULL, NULL);
  5266  	  Receive_expression* re = e->receive_expression();
  5267  	  if (re == NULL)
  5268  	    {
  5269  	      if (!e->is_error_expression())
  5270  		go_error_at(this->location(), "expected receive expression");
  5271  	      return false;
  5272  	    }
  5273  	  if (recv_var == "_")
  5274  	    {
  5275  	      go_error_at(recv_var_loc,
  5276  			  "no new variables on left side of %<:=%>");
  5277  	      recv_var = Gogo::erroneous_name();
  5278  	    }
  5279  	  *is_send = false;
  5280  	  *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
  5281  	  *channel = re->channel();
  5282  	  return true;
  5283  	}
  5284        else if (token->is_op(OPERATOR_COMMA))
  5285  	{
  5286  	  token = this->advance_token();
  5287  	  if (token->is_identifier())
  5288  	    {
  5289  	      std::string recv_closed = token->identifier();
  5290  	      bool is_rc_exported = token->is_identifier_exported();
  5291  	      Location recv_closed_loc = token->location();
  5292  	      closed_is_id = true;
  5293  
  5294  	      token = this->advance_token();
  5295  	      if (token->is_op(OPERATOR_COLONEQ))
  5296  		{
  5297  		  // case rv, rc := <-c:
  5298  		  this->advance_token();
  5299  		  Expression* e = this->expression(PRECEDENCE_NORMAL, false,
  5300  						   false, NULL, NULL);
  5301  		  Receive_expression* re = e->receive_expression();
  5302  		  if (re == NULL)
  5303  		    {
  5304  		      if (!e->is_error_expression())
  5305  			go_error_at(this->location(),
  5306  				 "expected receive expression");
  5307  		      return false;
  5308  		    }
  5309  		  if (recv_var == "_" && recv_closed == "_")
  5310  		    {
  5311  		      go_error_at(recv_var_loc,
  5312  				  "no new variables on left side of %<:=%>");
  5313  		      recv_var = Gogo::erroneous_name();
  5314  		    }
  5315  		  *is_send = false;
  5316  		  if (recv_var != "_")
  5317  		    *varname = gogo->pack_hidden_name(recv_var,
  5318  						      is_rv_exported);
  5319  		  if (recv_closed != "_")
  5320  		    *closedname = gogo->pack_hidden_name(recv_closed,
  5321  							 is_rc_exported);
  5322  		  *channel = re->channel();
  5323  		  return true;
  5324  		}
  5325  
  5326  	      this->unget_token(Token::make_identifier_token(recv_closed,
  5327  							     is_rc_exported,
  5328  							     recv_closed_loc));
  5329  	    }
  5330  
  5331  	  *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
  5332  							       is_rv_exported),
  5333  					recv_var_loc, true, false);
  5334  	  saw_comma = true;
  5335  	}
  5336        else
  5337  	this->unget_token(Token::make_identifier_token(recv_var,
  5338  						       is_rv_exported,
  5339  						       recv_var_loc));
  5340      }
  5341  
  5342    // If SAW_COMMA is false, then we are looking at the start of the
  5343    // send or receive expression.  If SAW_COMMA is true, then *VAL is
  5344    // set and we just read a comma.
  5345  
  5346    Expression* e;
  5347    if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
  5348      {
  5349        e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL);
  5350        if (e->receive_expression() != NULL)
  5351  	{
  5352  	  *is_send = false;
  5353  	  *channel = e->receive_expression()->channel();
  5354  	  // This is 'case (<-c):'.  We now expect ':'.  If we see
  5355  	  // '<-', then we have case (<-c)<-v:
  5356  	  if (!this->peek_token()->is_op(OPERATOR_CHANOP))
  5357  	    return true;
  5358  	}
  5359      }
  5360    else
  5361      {
  5362        // case <-c:
  5363        *is_send = false;
  5364        this->advance_token();
  5365        *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  5366  
  5367        // The next token should be ':'.  If it is '<-', then we have
  5368        // case <-c <- v:
  5369        // which is to say, send on a channel received from a channel.
  5370        if (!this->peek_token()->is_op(OPERATOR_CHANOP))
  5371  	return true;
  5372  
  5373        e = Expression::make_receive(*channel, (*channel)->location());
  5374      }
  5375  
  5376    if (!saw_comma && this->peek_token()->is_op(OPERATOR_COMMA))
  5377      {
  5378        this->advance_token();
  5379        // case v, e = <-c:
  5380        if (!e->is_sink_expression())
  5381  	*val = e;
  5382        e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL);
  5383        saw_comma = true;
  5384      }
  5385  
  5386    if (this->peek_token()->is_op(OPERATOR_EQ))
  5387      {
  5388        *is_send = false;
  5389        this->advance_token();
  5390        Location recvloc = this->location();
  5391        Expression* recvexpr = this->expression(PRECEDENCE_NORMAL, false,
  5392  					      true, NULL, NULL);
  5393        if (recvexpr->receive_expression() == NULL)
  5394  	{
  5395  	  go_error_at(recvloc, "missing %<<-%>");
  5396  	  return false;
  5397  	}
  5398        *channel = recvexpr->receive_expression()->channel();
  5399        if (saw_comma)
  5400  	{
  5401  	  // case v, e = <-c:
  5402  	  // *VAL is already set.
  5403  	  if (!e->is_sink_expression())
  5404  	    *closed = e;
  5405  	}
  5406        else
  5407  	{
  5408  	  // case v = <-c:
  5409  	  if (!e->is_sink_expression())
  5410  	    *val = e;
  5411  	}
  5412        return true;
  5413      }
  5414  
  5415    if (saw_comma)
  5416      {
  5417        if (closed_is_id)
  5418  	go_error_at(this->location(), "expected %<=%> or %<:=%>");
  5419        else
  5420  	go_error_at(this->location(), "expected %<=%>");
  5421        return false;
  5422      }
  5423  
  5424    if (this->peek_token()->is_op(OPERATOR_CHANOP))
  5425      {
  5426        // case c <- v:
  5427        *is_send = true;
  5428        *channel = this->verify_not_sink(e);
  5429        this->advance_token();
  5430        *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  5431        return true;
  5432      }
  5433  
  5434    go_error_at(this->location(), "expected %<<-%> or %<=%>");
  5435    return false;
  5436  }
  5437  
  5438  // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
  5439  // Condition = Expression .
  5440  
  5441  void
  5442  Parse::for_stat(Label* label)
  5443  {
  5444    go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
  5445    Location location = this->location();
  5446    const Token* token = this->advance_token();
  5447  
  5448    // Open a block to hold any variables defined in the init statement
  5449    // of the for statement.
  5450    this->gogo_->start_block(location);
  5451  
  5452    Block* init = NULL;
  5453    Expression* cond = NULL;
  5454    Block* post = NULL;
  5455    Range_clause range_clause;
  5456  
  5457    if (!token->is_op(OPERATOR_LCURLY))
  5458      {
  5459        if (token->is_keyword(KEYWORD_VAR))
  5460  	{
  5461  	  go_error_at(this->location(),
  5462                        "var declaration not allowed in for initializer");
  5463  	  this->var_decl();
  5464  	}
  5465  
  5466        if (token->is_op(OPERATOR_SEMICOLON))
  5467  	this->for_clause(&cond, &post);
  5468        else
  5469  	{
  5470  	  // We might be looking at a Condition, an InitStat, or a
  5471  	  // RangeClause.
  5472  	  bool saw_send_stmt = false;
  5473  	  cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
  5474  	  if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
  5475  	    {
  5476  	      if (cond == NULL && !range_clause.found)
  5477  		{
  5478  		  if (saw_send_stmt)
  5479  		    go_error_at(this->location(),
  5480                                  ("send statement used as value; "
  5481                                   "use select for non-blocking send"));
  5482  		  else
  5483  		    go_error_at(this->location(),
  5484                                  "parse error in for statement");
  5485  		}
  5486  	    }
  5487  	  else
  5488  	    {
  5489  	      if (range_clause.found)
  5490  		go_error_at(this->location(), "parse error after range clause");
  5491  
  5492  	      if (cond != NULL)
  5493  		{
  5494  		  // COND is actually an expression statement for
  5495  		  // InitStat at the start of a ForClause.
  5496  		  this->expression_stat(cond);
  5497  		  cond = NULL;
  5498  		}
  5499  
  5500  	      this->for_clause(&cond, &post);
  5501  	    }
  5502  	}
  5503      }
  5504  
  5505    // Check for the easy error of a newline before starting the block.
  5506    if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  5507      {
  5508        Location semi_loc = this->location();
  5509        if (this->advance_token()->is_op(OPERATOR_LCURLY))
  5510  	go_error_at(semi_loc, "unexpected semicolon or newline, expecting %<{%> after for clause");
  5511        // Otherwise we will get an error when we call this->block
  5512        // below.
  5513      }
  5514  
  5515    // Build the For_statement and note that it is the current target
  5516    // for break and continue statements.
  5517  
  5518    For_statement* sfor;
  5519    For_range_statement* srange;
  5520    Statement* s;
  5521    if (!range_clause.found)
  5522      {
  5523        sfor = Statement::make_for_statement(init, cond, post, location);
  5524        s = sfor;
  5525        srange = NULL;
  5526      }
  5527    else
  5528      {
  5529        srange = Statement::make_for_range_statement(range_clause.index,
  5530  						   range_clause.value,
  5531  						   range_clause.range,
  5532  						   location);
  5533        s = srange;
  5534        sfor = NULL;
  5535      }
  5536  
  5537    this->push_break_statement(s, label);
  5538    this->push_continue_statement(s, label);
  5539  
  5540    // Gather the block of statements in the loop and add them to the
  5541    // For_statement.
  5542  
  5543    this->gogo_->start_block(this->location());
  5544    Location end_loc = this->block();
  5545    Block* statements = this->gogo_->finish_block(end_loc);
  5546  
  5547    if (sfor != NULL)
  5548      sfor->add_statements(statements);
  5549    else
  5550      srange->add_statements(statements);
  5551  
  5552    // This is no longer the break/continue target.
  5553    this->pop_break_statement();
  5554    this->pop_continue_statement();
  5555  
  5556    // Add the For_statement to the list of statements, and close out
  5557    // the block we started to hold any variables defined in the for
  5558    // statement.
  5559  
  5560    this->gogo_->add_statement(s);
  5561  
  5562    this->gogo_->add_block(this->gogo_->finish_block(this->location()),
  5563  			 location);
  5564  }
  5565  
  5566  // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
  5567  // InitStat = SimpleStat .
  5568  // PostStat = SimpleStat .
  5569  
  5570  // We have already read InitStat at this point.
  5571  
  5572  void
  5573  Parse::for_clause(Expression** cond, Block** post)
  5574  {
  5575    go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
  5576    this->advance_token();
  5577    if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
  5578      *cond = NULL;
  5579    else if (this->peek_token()->is_op(OPERATOR_LCURLY))
  5580      {
  5581        go_error_at(this->location(), "unexpected semicolon or newline, expecting %<{%> after for clause");
  5582        *cond = NULL;
  5583        *post = NULL;
  5584        return;
  5585      }
  5586    else
  5587      *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
  5588    if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
  5589      go_error_at(this->location(), "expected semicolon");
  5590    else
  5591      this->advance_token();
  5592  
  5593    if (this->peek_token()->is_op(OPERATOR_LCURLY))
  5594      *post = NULL;
  5595    else
  5596      {
  5597        this->gogo_->start_block(this->location());
  5598        this->simple_stat(false, NULL, NULL, NULL);
  5599        *post = this->gogo_->finish_block(this->location());
  5600      }
  5601  }
  5602  
  5603  // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
  5604  
  5605  // This is the := version.  It is called with a list of identifiers.
  5606  
  5607  void
  5608  Parse::range_clause_decl(const Typed_identifier_list* til,
  5609  			 Range_clause* p_range_clause)
  5610  {
  5611    go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
  5612    Location location = this->location();
  5613  
  5614    p_range_clause->found = true;
  5615  
  5616    if (til->size() > 2)
  5617      go_error_at(this->location(), "too many variables for range clause");
  5618  
  5619    this->advance_token();
  5620    Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL,
  5621  				      NULL);
  5622    p_range_clause->range = expr;
  5623  
  5624    if (til->empty())
  5625      return;
  5626  
  5627    bool any_new = false;
  5628  
  5629    const Typed_identifier* pti = &til->front();
  5630    Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new,
  5631  				    NULL, NULL);
  5632    if (any_new && no->is_variable())
  5633      no->var_value()->set_type_from_range_index();
  5634    p_range_clause->index = Expression::make_var_reference(no, location);
  5635  
  5636    if (til->size() == 1)
  5637      p_range_clause->value = NULL;
  5638    else
  5639      {
  5640        pti = &til->back();
  5641        bool is_new = false;
  5642        no = this->init_var(*pti, NULL, expr, true, true, &is_new, NULL, NULL);
  5643        if (is_new && no->is_variable())
  5644  	no->var_value()->set_type_from_range_value();
  5645        if (is_new)
  5646  	any_new = true;
  5647        p_range_clause->value = Expression::make_var_reference(no, location);
  5648      }
  5649  
  5650    if (!any_new)
  5651      go_error_at(location, "variables redeclared but no variable is new");
  5652  }
  5653  
  5654  // The = version of RangeClause.  This is called with a list of
  5655  // expressions.
  5656  
  5657  void
  5658  Parse::range_clause_expr(const Expression_list* vals,
  5659  			 Range_clause* p_range_clause)
  5660  {
  5661    go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
  5662  
  5663    p_range_clause->found = true;
  5664  
  5665    go_assert(vals->size() >= 1);
  5666    if (vals->size() > 2)
  5667      go_error_at(this->location(), "too many variables for range clause");
  5668  
  5669    this->advance_token();
  5670    p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
  5671  					   NULL, NULL);
  5672  
  5673    if (vals->empty())
  5674      return;
  5675  
  5676    p_range_clause->index = vals->front();
  5677    if (vals->size() == 1)
  5678      p_range_clause->value = NULL;
  5679    else
  5680      p_range_clause->value = vals->back();
  5681  }
  5682  
  5683  // Push a statement on the break stack.
  5684  
  5685  void
  5686  Parse::push_break_statement(Statement* enclosing, Label* label)
  5687  {
  5688    if (this->break_stack_ == NULL)
  5689      this->break_stack_ = new Bc_stack();
  5690    this->break_stack_->push_back(std::make_pair(enclosing, label));
  5691  }
  5692  
  5693  // Push a statement on the continue stack.
  5694  
  5695  void
  5696  Parse::push_continue_statement(Statement* enclosing, Label* label)
  5697  {
  5698    if (this->continue_stack_ == NULL)
  5699      this->continue_stack_ = new Bc_stack();
  5700    this->continue_stack_->push_back(std::make_pair(enclosing, label));
  5701  }
  5702  
  5703  // Pop the break stack.
  5704  
  5705  void
  5706  Parse::pop_break_statement()
  5707  {
  5708    this->break_stack_->pop_back();
  5709  }
  5710  
  5711  // Pop the continue stack.
  5712  
  5713  void
  5714  Parse::pop_continue_statement()
  5715  {
  5716    this->continue_stack_->pop_back();
  5717  }
  5718  
  5719  // Find a break or continue statement given a label name.
  5720  
  5721  Statement*
  5722  Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
  5723  {
  5724    if (bc_stack == NULL)
  5725      return NULL;
  5726    for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
  5727         p != bc_stack->rend();
  5728         ++p)
  5729      {
  5730        if (p->second != NULL && p->second->name() == label)
  5731  	{
  5732  	  p->second->set_is_used();
  5733  	  return p->first;
  5734  	}
  5735      }
  5736    return NULL;
  5737  }
  5738  
  5739  // BreakStat = "break" [ identifier ] .
  5740  
  5741  void
  5742  Parse::break_stat()
  5743  {
  5744    go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
  5745    Location location = this->location();
  5746  
  5747    const Token* token = this->advance_token();
  5748    Statement* enclosing;
  5749    if (!token->is_identifier())
  5750      {
  5751        if (this->break_stack_ == NULL || this->break_stack_->empty())
  5752  	{
  5753  	  go_error_at(this->location(),
  5754                        "break statement not within for or switch or select");
  5755  	  return;
  5756  	}
  5757        enclosing = this->break_stack_->back().first;
  5758      }
  5759    else
  5760      {
  5761        enclosing = this->find_bc_statement(this->break_stack_,
  5762  					  token->identifier());
  5763        if (enclosing == NULL)
  5764  	{
  5765  	  // If there is a label with this name, mark it as used to
  5766  	  // avoid a useless error about an unused label.
  5767  	  this->gogo_->add_label_reference(token->identifier(),
  5768                                             Linemap::unknown_location(), false);
  5769  
  5770  	  go_error_at(token->location(), "invalid break label %qs",
  5771                        Gogo::message_name(token->identifier()).c_str());
  5772  	  this->advance_token();
  5773  	  return;
  5774  	}
  5775        this->advance_token();
  5776      }
  5777  
  5778    Unnamed_label* label;
  5779    if (enclosing->classification() == Statement::STATEMENT_FOR)
  5780      label = enclosing->for_statement()->break_label();
  5781    else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
  5782      label = enclosing->for_range_statement()->break_label();
  5783    else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
  5784      label = enclosing->switch_statement()->break_label();
  5785    else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
  5786      label = enclosing->type_switch_statement()->break_label();
  5787    else if (enclosing->classification() == Statement::STATEMENT_SELECT)
  5788      label = enclosing->select_statement()->break_label();
  5789    else
  5790      go_unreachable();
  5791  
  5792    this->gogo_->add_statement(Statement::make_break_statement(label,
  5793  							     location));
  5794  }
  5795  
  5796  // ContinueStat = "continue" [ identifier ] .
  5797  
  5798  void
  5799  Parse::continue_stat()
  5800  {
  5801    go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
  5802    Location location = this->location();
  5803  
  5804    const Token* token = this->advance_token();
  5805    Statement* enclosing;
  5806    if (!token->is_identifier())
  5807      {
  5808        if (this->continue_stack_ == NULL || this->continue_stack_->empty())
  5809  	{
  5810  	  go_error_at(this->location(), "continue statement not within for");
  5811  	  return;
  5812  	}
  5813        enclosing = this->continue_stack_->back().first;
  5814      }
  5815    else
  5816      {
  5817        enclosing = this->find_bc_statement(this->continue_stack_,
  5818  					  token->identifier());
  5819        if (enclosing == NULL)
  5820  	{
  5821  	  // If there is a label with this name, mark it as used to
  5822  	  // avoid a useless error about an unused label.
  5823  	  this->gogo_->add_label_reference(token->identifier(),
  5824                                             Linemap::unknown_location(), false);
  5825  
  5826  	  go_error_at(token->location(), "invalid continue label %qs",
  5827                        Gogo::message_name(token->identifier()).c_str());
  5828  	  this->advance_token();
  5829  	  return;
  5830  	}
  5831        this->advance_token();
  5832      }
  5833  
  5834    Unnamed_label* label;
  5835    if (enclosing->classification() == Statement::STATEMENT_FOR)
  5836      label = enclosing->for_statement()->continue_label();
  5837    else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
  5838      label = enclosing->for_range_statement()->continue_label();
  5839    else
  5840      go_unreachable();
  5841  
  5842    this->gogo_->add_statement(Statement::make_continue_statement(label,
  5843  								location));
  5844  }
  5845  
  5846  // GotoStat = "goto" identifier .
  5847  
  5848  void
  5849  Parse::goto_stat()
  5850  {
  5851    go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
  5852    Location location = this->location();
  5853    const Token* token = this->advance_token();
  5854    if (!token->is_identifier())
  5855      go_error_at(this->location(), "expected label for goto");
  5856    else
  5857      {
  5858        Label* label = this->gogo_->add_label_reference(token->identifier(),
  5859  						      location, true);
  5860        Statement* s = Statement::make_goto_statement(label, location);
  5861        this->gogo_->add_statement(s);
  5862        this->advance_token();
  5863      }
  5864  }
  5865  
  5866  // PackageClause = "package" PackageName .
  5867  
  5868  void
  5869  Parse::package_clause()
  5870  {
  5871    const Token* token = this->peek_token();
  5872    Location location = token->location();
  5873    std::string name;
  5874    if (!token->is_keyword(KEYWORD_PACKAGE))
  5875      {
  5876        go_error_at(this->location(), "program must start with package clause");
  5877        name = "ERROR";
  5878      }
  5879    else
  5880      {
  5881        token = this->advance_token();
  5882        if (token->is_identifier())
  5883  	{
  5884  	  name = token->identifier();
  5885  	  if (name == "_")
  5886  	    {
  5887  	      go_error_at(this->location(), "invalid package name %<_%>");
  5888  	      name = Gogo::erroneous_name();
  5889  	    }
  5890  	  this->advance_token();
  5891  	}
  5892        else
  5893  	{
  5894  	  go_error_at(this->location(), "package name must be an identifier");
  5895  	  name = "ERROR";
  5896  	}
  5897      }
  5898    this->gogo_->set_package_name(name, location);
  5899  }
  5900  
  5901  // ImportDecl = "import" Decl<ImportSpec> .
  5902  
  5903  void
  5904  Parse::import_decl()
  5905  {
  5906    go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
  5907    this->advance_token();
  5908    this->decl(&Parse::import_spec);
  5909  }
  5910  
  5911  // ImportSpec = [ "." | PackageName ] PackageFileName .
  5912  
  5913  void
  5914  Parse::import_spec()
  5915  {
  5916    this->check_directives();
  5917  
  5918    const Token* token = this->peek_token();
  5919    Location location = token->location();
  5920  
  5921    std::string local_name;
  5922    bool is_local_name_exported = false;
  5923    if (token->is_op(OPERATOR_DOT))
  5924      {
  5925        local_name = ".";
  5926        token = this->advance_token();
  5927      }
  5928    else if (token->is_identifier())
  5929      {
  5930        local_name = token->identifier();
  5931        is_local_name_exported = token->is_identifier_exported();
  5932        token = this->advance_token();
  5933      }
  5934  
  5935    if (!token->is_string())
  5936      {
  5937        go_error_at(this->location(), "import path must be a string");
  5938        this->advance_token();
  5939        return;
  5940      }
  5941  
  5942    this->gogo_->import_package(token->string_value(), local_name,
  5943  			      is_local_name_exported, true, location);
  5944  
  5945    this->advance_token();
  5946  }
  5947  
  5948  // SourceFile       = PackageClause ";" { ImportDecl ";" }
  5949  //			{ TopLevelDecl ";" } .
  5950  
  5951  void
  5952  Parse::program()
  5953  {
  5954    this->package_clause();
  5955  
  5956    const Token* token = this->peek_token();
  5957    if (token->is_op(OPERATOR_SEMICOLON))
  5958      token = this->advance_token();
  5959    else
  5960      go_error_at(this->location(),
  5961                  "expected %<;%> or newline after package clause");
  5962  
  5963    while (token->is_keyword(KEYWORD_IMPORT))
  5964      {
  5965        this->import_decl();
  5966        token = this->peek_token();
  5967        if (token->is_op(OPERATOR_SEMICOLON))
  5968  	token = this->advance_token();
  5969        else
  5970  	go_error_at(this->location(),
  5971                      "expected %<;%> or newline after import declaration");
  5972      }
  5973  
  5974    while (!token->is_eof())
  5975      {
  5976        if (this->declaration_may_start_here())
  5977  	this->declaration();
  5978        else
  5979  	{
  5980  	  go_error_at(this->location(), "expected declaration");
  5981  	  this->gogo_->mark_locals_used();
  5982  	  do
  5983  	    this->advance_token();
  5984  	  while (!this->peek_token()->is_eof()
  5985  		 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
  5986  		 && !this->peek_token()->is_op(OPERATOR_RCURLY));
  5987  	  if (!this->peek_token()->is_eof()
  5988  	      && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
  5989  	    this->advance_token();
  5990  	}
  5991        token = this->peek_token();
  5992        if (token->is_op(OPERATOR_SEMICOLON))
  5993  	token = this->advance_token();
  5994        else if (!token->is_eof() || !saw_errors())
  5995  	{
  5996  	  if (token->is_op(OPERATOR_CHANOP))
  5997  	    go_error_at(this->location(),
  5998                          ("send statement used as value; "
  5999                           "use select for non-blocking send"));
  6000  	  else
  6001  	    go_error_at(this->location(),
  6002                          ("expected %<;%> or newline after top "
  6003                           "level declaration"));
  6004  	  this->skip_past_error(OPERATOR_INVALID);
  6005  	}
  6006      }
  6007  
  6008    this->check_directives();
  6009  }
  6010  
  6011  // If there are any pending compiler directives, clear them and give
  6012  // an error.  This is called when directives are not permitted.
  6013  
  6014  void
  6015  Parse::check_directives()
  6016  {
  6017    if (this->lex_->get_and_clear_pragmas() != 0)
  6018      go_error_at(this->location(), "misplaced compiler directive");
  6019    if (this->lex_->has_embeds())
  6020      {
  6021        this->lex_->clear_embeds();
  6022        go_error_at(this->location(), "misplaced go:embed directive");
  6023      }
  6024  }
  6025  
  6026  // Skip forward to a semicolon or OP.  OP will normally be
  6027  // OPERATOR_RPAREN or OPERATOR_RCURLY.  If we find a semicolon, move
  6028  // past it and return.  If we find OP, it will be the next token to
  6029  // read.  Return true if we are OK, false if we found EOF.
  6030  
  6031  bool
  6032  Parse::skip_past_error(Operator op)
  6033  {
  6034    this->gogo_->mark_locals_used();
  6035    const Token* token = this->peek_token();
  6036    while (!token->is_op(op))
  6037      {
  6038        if (token->is_eof())
  6039  	return false;
  6040        if (token->is_op(OPERATOR_SEMICOLON))
  6041  	{
  6042  	  this->advance_token();
  6043  	  return true;
  6044  	}
  6045        token = this->advance_token();
  6046      }
  6047    return true;
  6048  }
  6049  
  6050  // Check that an expression is not a sink.
  6051  
  6052  Expression*
  6053  Parse::verify_not_sink(Expression* expr)
  6054  {
  6055    if (expr->is_sink_expression())
  6056      {
  6057        go_error_at(expr->location(), "cannot use %<_%> as value");
  6058        expr = Expression::make_error(expr->location());
  6059      }
  6060  
  6061    // If this can not be a sink, and it is a variable, then we are
  6062    // using the variable, not just assigning to it.
  6063    if (expr->var_expression() != NULL)
  6064      this->mark_var_used(expr->var_expression()->named_object());
  6065    else if (expr->enclosed_var_expression() != NULL)
  6066      this->mark_var_used(expr->enclosed_var_expression()->variable());
  6067    return expr;
  6068  }
  6069  
  6070  // Mark a variable as used.
  6071  
  6072  void
  6073  Parse::mark_var_used(Named_object* no)
  6074  {
  6075    if (no->is_variable())
  6076      no->var_value()->set_is_used();
  6077  }