OSDN Git Service

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