OSDN Git Service

Better handling of unexpected EOF in parser.
[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_eof())
1303     {
1304       if (!saw_errors())
1305         error_at(this->location(), "unexpected end of file");
1306       return;
1307     }
1308
1309   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1310     (this->*pfn)(varg);
1311   else
1312     {
1313       if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1314         {
1315           this->list(pfn, varg, true);
1316           if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1317             {
1318               error_at(this->location(), "missing %<)%>");
1319               while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1320                 {
1321                   if (this->peek_token()->is_eof())
1322                     return;
1323                 }
1324             }
1325         }
1326       this->advance_token();
1327     }
1328 }
1329
1330 // List<P> = P { ";" P } [ ";" ] .
1331
1332 // In order to pick up the trailing semicolon we need to know what
1333 // might follow.  This is either a '}' or a ')'.
1334
1335 void
1336 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1337 {
1338   (this->*pfn)(varg);
1339   Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1340   while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1341          || this->peek_token()->is_op(OPERATOR_COMMA))
1342     {
1343       if (this->peek_token()->is_op(OPERATOR_COMMA))
1344         error_at(this->location(), "unexpected comma");
1345       if (this->advance_token()->is_op(follow))
1346         break;
1347       (this->*pfn)(varg);
1348     }
1349 }
1350
1351 // ConstDecl      = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1352
1353 void
1354 Parse::const_decl()
1355 {
1356   go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1357   this->advance_token();
1358   this->reset_iota();
1359
1360   Type* last_type = NULL;
1361   Expression_list* last_expr_list = NULL;
1362
1363   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1364     this->const_spec(&last_type, &last_expr_list);
1365   else
1366     {
1367       this->advance_token();
1368       while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1369         {
1370           this->const_spec(&last_type, &last_expr_list);
1371           if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1372             this->advance_token();
1373           else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1374             {
1375               error_at(this->location(), "expected %<;%> or %<)%> or newline");
1376               if (!this->skip_past_error(OPERATOR_RPAREN))
1377                 return;
1378             }
1379         }
1380       this->advance_token();
1381     }
1382
1383   if (last_expr_list != NULL)
1384     delete last_expr_list;
1385 }
1386
1387 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1388
1389 void
1390 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1391 {
1392   Typed_identifier_list til;
1393   this->identifier_list(&til);
1394
1395   Type* type = NULL;
1396   if (this->type_may_start_here())
1397     {
1398       type = this->type();
1399       *last_type = NULL;
1400       *last_expr_list = NULL;
1401     }
1402
1403   Expression_list *expr_list;
1404   if (!this->peek_token()->is_op(OPERATOR_EQ))
1405     {
1406       if (*last_expr_list == NULL)
1407         {
1408           error_at(this->location(), "expected %<=%>");
1409           return;
1410         }
1411       type = *last_type;
1412       expr_list = new Expression_list;
1413       for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1414            p != (*last_expr_list)->end();
1415            ++p)
1416         expr_list->push_back((*p)->copy());
1417     }
1418   else
1419     {
1420       this->advance_token();
1421       expr_list = this->expression_list(NULL, false);
1422       *last_type = type;
1423       if (*last_expr_list != NULL)
1424         delete *last_expr_list;
1425       *last_expr_list = expr_list;
1426     }
1427
1428   Expression_list::const_iterator pe = expr_list->begin();
1429   for (Typed_identifier_list::iterator pi = til.begin();
1430        pi != til.end();
1431        ++pi, ++pe)
1432     {
1433       if (pe == expr_list->end())
1434         {
1435           error_at(this->location(), "not enough initializers");
1436           return;
1437         }
1438       if (type != NULL)
1439         pi->set_type(type);
1440
1441       if (!Gogo::is_sink_name(pi->name()))
1442         this->gogo_->add_constant(*pi, *pe, this->iota_value());
1443     }
1444   if (pe != expr_list->end())
1445     error_at(this->location(), "too many initializers");
1446
1447   this->increment_iota();
1448
1449   return;
1450 }
1451
1452 // TypeDecl = "type" Decl<TypeSpec> .
1453
1454 void
1455 Parse::type_decl()
1456 {
1457   go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1458   this->advance_token();
1459   this->decl(&Parse::type_spec, NULL);
1460 }
1461
1462 // TypeSpec = identifier Type .
1463
1464 void
1465 Parse::type_spec(void*)
1466 {
1467   const Token* token = this->peek_token();
1468   if (!token->is_identifier())
1469     {
1470       error_at(this->location(), "expected identifier");
1471       return;
1472     }
1473   std::string name = token->identifier();
1474   bool is_exported = token->is_identifier_exported();
1475   source_location location = token->location();
1476   token = this->advance_token();
1477
1478   // The scope of the type name starts at the point where the
1479   // identifier appears in the source code.  We implement this by
1480   // declaring the type before we read the type definition.
1481   Named_object* named_type = NULL;
1482   if (name != "_")
1483     {
1484       name = this->gogo_->pack_hidden_name(name, is_exported);
1485       named_type = this->gogo_->declare_type(name, location);
1486     }
1487
1488   Type* type;
1489   if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1490     type = this->type();
1491   else
1492     {
1493       error_at(this->location(),
1494                "unexpected semicolon or newline in type declaration");
1495       type = Type::make_error_type();
1496       this->advance_token();
1497     }
1498
1499   if (type->is_error_type())
1500     {
1501       while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1502              && !this->peek_token()->is_eof())
1503         this->advance_token();
1504     }
1505
1506   if (name != "_")
1507     {
1508       if (named_type->is_type_declaration())
1509         {
1510           Type* ftype = type->forwarded();
1511           if (ftype->forward_declaration_type() != NULL
1512               && (ftype->forward_declaration_type()->named_object()
1513                   == named_type))
1514             {
1515               error_at(location, "invalid recursive type");
1516               type = Type::make_error_type();
1517             }
1518
1519           this->gogo_->define_type(named_type,
1520                                    Type::make_named_type(named_type, type,
1521                                                          location));
1522           go_assert(named_type->package() == NULL);
1523         }
1524       else
1525         {
1526           // This will probably give a redefinition error.
1527           this->gogo_->add_type(name, type, location);
1528         }
1529     }
1530 }
1531
1532 // VarDecl = "var" Decl<VarSpec> .
1533
1534 void
1535 Parse::var_decl()
1536 {
1537   go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1538   this->advance_token();
1539   this->decl(&Parse::var_spec, NULL);
1540 }
1541
1542 // VarSpec = IdentifierList
1543 //             ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1544
1545 void
1546 Parse::var_spec(void*)
1547 {
1548   // Get the variable names.
1549   Typed_identifier_list til;
1550   this->identifier_list(&til);
1551
1552   source_location location = this->location();
1553
1554   Type* type = NULL;
1555   Expression_list* init = NULL;
1556   if (!this->peek_token()->is_op(OPERATOR_EQ))
1557     {
1558       type = this->type();
1559       if (type->is_error_type())
1560         {
1561           while (!this->peek_token()->is_op(OPERATOR_EQ)
1562                  && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1563                  && !this->peek_token()->is_eof())
1564             this->advance_token();
1565         }
1566       if (this->peek_token()->is_op(OPERATOR_EQ))
1567         {
1568           this->advance_token();
1569           init = this->expression_list(NULL, false);
1570         }
1571     }
1572   else
1573     {
1574       this->advance_token();
1575       init = this->expression_list(NULL, false);
1576     }
1577
1578   this->init_vars(&til, type, init, false, location);
1579
1580   if (init != NULL)
1581     delete init;
1582 }
1583
1584 // Create variables.  TIL is a list of variable names.  If TYPE is not
1585 // NULL, it is the type of all the variables.  If INIT is not NULL, it
1586 // is an initializer list for the variables.
1587
1588 void
1589 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1590                  Expression_list* init, bool is_coloneq,
1591                  source_location location)
1592 {
1593   // Check for an initialization which can yield multiple values.
1594   if (init != NULL && init->size() == 1 && til->size() > 1)
1595     {
1596       if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1597                                     location))
1598         return;
1599       if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1600                                    location))
1601         return;
1602       if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1603                                        location))
1604         return;
1605       if (this->init_vars_from_type_guard(til, type, *init->begin(),
1606                                           is_coloneq, location))
1607         return;
1608     }
1609
1610   if (init != NULL && init->size() != til->size())
1611     {
1612       if (init->empty() || !init->front()->is_error_expression())
1613         error_at(location, "wrong number of initializations");
1614       init = NULL;
1615       if (type == NULL)
1616         type = Type::make_error_type();
1617     }
1618
1619   // Note that INIT was already parsed with the old name bindings, so
1620   // we don't have to worry that it will accidentally refer to the
1621   // newly declared variables.
1622
1623   Expression_list::const_iterator pexpr;
1624   if (init != NULL)
1625     pexpr = init->begin();
1626   bool any_new = false;
1627   for (Typed_identifier_list::const_iterator p = til->begin();
1628        p != til->end();
1629        ++p)
1630     {
1631       if (init != NULL)
1632         go_assert(pexpr != init->end());
1633       this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1634                      false, &any_new);
1635       if (init != NULL)
1636         ++pexpr;
1637     }
1638   if (init != NULL)
1639     go_assert(pexpr == init->end());
1640   if (is_coloneq && !any_new)
1641     error_at(location, "variables redeclared but no variable is new");
1642 }
1643
1644 // See if we need to initialize a list of variables from a function
1645 // call.  This returns true if we have set up the variables and the
1646 // initialization.
1647
1648 bool
1649 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1650                            Expression* expr, bool is_coloneq,
1651                            source_location location)
1652 {
1653   Call_expression* call = expr->call_expression();
1654   if (call == NULL)
1655     return false;
1656
1657   // This is a function call.  We can't check here whether it returns
1658   // the right number of values, but it might.  Declare the variables,
1659   // and then assign the results of the call to them.
1660
1661   unsigned int index = 0;
1662   bool any_new = false;
1663   for (Typed_identifier_list::const_iterator pv = vars->begin();
1664        pv != vars->end();
1665        ++pv, ++index)
1666     {
1667       Expression* init = Expression::make_call_result(call, index);
1668       this->init_var(*pv, type, init, is_coloneq, false, &any_new);
1669     }
1670
1671   if (is_coloneq && !any_new)
1672     error_at(location, "variables redeclared but no variable is new");
1673
1674   return true;
1675 }
1676
1677 // See if we need to initialize a pair of values from a map index
1678 // expression.  This returns true if we have set up the variables and
1679 // the initialization.
1680
1681 bool
1682 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1683                           Expression* expr, bool is_coloneq,
1684                           source_location location)
1685 {
1686   Index_expression* index = expr->index_expression();
1687   if (index == NULL)
1688     return false;
1689   if (vars->size() != 2)
1690     return false;
1691
1692   // This is an index which is being assigned to two variables.  It
1693   // must be a map index.  Declare the variables, and then assign the
1694   // results of the map index.
1695   bool any_new = false;
1696   Typed_identifier_list::const_iterator p = vars->begin();
1697   Expression* init = type == NULL ? index : NULL;
1698   Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1699                                         type == NULL, &any_new);
1700   if (type == NULL && any_new && val_no->is_variable())
1701     val_no->var_value()->set_type_from_init_tuple();
1702   Expression* val_var = Expression::make_var_reference(val_no, location);
1703
1704   ++p;
1705   Type* var_type = type;
1706   if (var_type == NULL)
1707     var_type = Type::lookup_bool_type();
1708   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1709                                     &any_new);
1710   Expression* present_var = Expression::make_var_reference(no, location);
1711
1712   if (is_coloneq && !any_new)
1713     error_at(location, "variables redeclared but no variable is new");
1714
1715   Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1716                                                       index, location);
1717
1718   if (!this->gogo_->in_global_scope())
1719     this->gogo_->add_statement(s);
1720   else if (!val_no->is_sink())
1721     {
1722       if (val_no->is_variable())
1723         val_no->var_value()->add_preinit_statement(this->gogo_, s);
1724     }
1725   else if (!no->is_sink())
1726     {
1727       if (no->is_variable())
1728         no->var_value()->add_preinit_statement(this->gogo_, s);
1729     }
1730   else
1731     {
1732       // Execute the map index expression just so that we can fail if
1733       // the map is nil.
1734       Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1735                                                       NULL, location);
1736       dummy->var_value()->add_preinit_statement(this->gogo_, s);
1737     }
1738
1739   return true;
1740 }
1741
1742 // See if we need to initialize a pair of values from a receive
1743 // expression.  This returns true if we have set up the variables and
1744 // the initialization.
1745
1746 bool
1747 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1748                               Expression* expr, bool is_coloneq,
1749                               source_location location)
1750 {
1751   Receive_expression* receive = expr->receive_expression();
1752   if (receive == NULL)
1753     return false;
1754   if (vars->size() != 2)
1755     return false;
1756
1757   // This is a receive expression which is being assigned to two
1758   // variables.  Declare the variables, and then assign the results of
1759   // the receive.
1760   bool any_new = false;
1761   Typed_identifier_list::const_iterator p = vars->begin();
1762   Expression* init = type == NULL ? receive : NULL;
1763   Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1764                                         type == NULL, &any_new);
1765   if (type == NULL && any_new && val_no->is_variable())
1766     val_no->var_value()->set_type_from_init_tuple();
1767   Expression* val_var = Expression::make_var_reference(val_no, location);
1768
1769   ++p;
1770   Type* var_type = type;
1771   if (var_type == NULL)
1772     var_type = Type::lookup_bool_type();
1773   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1774                                     &any_new);
1775   Expression* received_var = Expression::make_var_reference(no, location);
1776
1777   if (is_coloneq && !any_new)
1778     error_at(location, "variables redeclared but no variable is new");
1779
1780   Statement* s = Statement::make_tuple_receive_assignment(val_var,
1781                                                           received_var,
1782                                                           receive->channel(),
1783                                                           false,
1784                                                           location);
1785
1786   if (!this->gogo_->in_global_scope())
1787     this->gogo_->add_statement(s);
1788   else if (!val_no->is_sink())
1789     {
1790       if (val_no->is_variable())
1791         val_no->var_value()->add_preinit_statement(this->gogo_, s);
1792     }
1793   else if (!no->is_sink())
1794     {
1795       if (no->is_variable())
1796         no->var_value()->add_preinit_statement(this->gogo_, s);
1797     }
1798   else
1799     {
1800       Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1801                                                       NULL, location);
1802       dummy->var_value()->add_preinit_statement(this->gogo_, s);
1803     }
1804
1805   return true;
1806 }
1807
1808 // See if we need to initialize a pair of values from a type guard
1809 // expression.  This returns true if we have set up the variables and
1810 // the initialization.
1811
1812 bool
1813 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1814                                  Type* type, Expression* expr,
1815                                  bool is_coloneq, source_location location)
1816 {
1817   Type_guard_expression* type_guard = expr->type_guard_expression();
1818   if (type_guard == NULL)
1819     return false;
1820   if (vars->size() != 2)
1821     return false;
1822
1823   // This is a type guard expression which is being assigned to two
1824   // variables.  Declare the variables, and then assign the results of
1825   // the type guard.
1826   bool any_new = false;
1827   Typed_identifier_list::const_iterator p = vars->begin();
1828   Type* var_type = type;
1829   if (var_type == NULL)
1830     var_type = type_guard->type();
1831   Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1832                                         &any_new);
1833   Expression* val_var = Expression::make_var_reference(val_no, location);
1834
1835   ++p;
1836   var_type = type;
1837   if (var_type == NULL)
1838     var_type = Type::lookup_bool_type();
1839   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1840                                     &any_new);
1841   Expression* ok_var = Expression::make_var_reference(no, location);
1842
1843   Expression* texpr = type_guard->expr();
1844   Type* t = type_guard->type();
1845   Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1846                                                              texpr, t,
1847                                                              location);
1848
1849   if (is_coloneq && !any_new)
1850     error_at(location, "variables redeclared but no variable is new");
1851
1852   if (!this->gogo_->in_global_scope())
1853     this->gogo_->add_statement(s);
1854   else if (!val_no->is_sink())
1855     {
1856       if (val_no->is_variable())
1857         val_no->var_value()->add_preinit_statement(this->gogo_, s);
1858     }
1859   else if (!no->is_sink())
1860     {
1861       if (no->is_variable())
1862         no->var_value()->add_preinit_statement(this->gogo_, s);
1863     }
1864   else
1865     {
1866       Named_object* dummy = this->create_dummy_global(type, NULL, location);
1867       dummy->var_value()->add_preinit_statement(this->gogo_, s);
1868     }
1869
1870   return true;
1871 }
1872
1873 // Create a single variable.  If IS_COLONEQ is true, we permit
1874 // redeclarations in the same block, and we set *IS_NEW when we find a
1875 // new variable which is not a redeclaration.
1876
1877 Named_object*
1878 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1879                 bool is_coloneq, bool type_from_init, bool* is_new)
1880 {
1881   source_location location = tid.location();
1882
1883   if (Gogo::is_sink_name(tid.name()))
1884     {
1885       if (!type_from_init && init != NULL)
1886         {
1887           if (!this->gogo_->in_global_scope())
1888             this->gogo_->add_statement(Statement::make_statement(init, true));
1889           else
1890             return this->create_dummy_global(type, init, location);
1891         }
1892       return this->gogo_->add_sink();
1893     }
1894
1895   if (is_coloneq)
1896     {
1897       Named_object* no = this->gogo_->lookup_in_block(tid.name());
1898       if (no != NULL
1899           && (no->is_variable() || no->is_result_variable()))
1900         {
1901           // INIT may be NULL even when IS_COLONEQ is true for cases
1902           // like v, ok := x.(int).
1903           if (!type_from_init && init != NULL)
1904             {
1905               Expression *v = Expression::make_var_reference(no, location);
1906               Statement *s = Statement::make_assignment(v, init, location);
1907               this->gogo_->add_statement(s);
1908             }
1909           return no;
1910         }
1911     }
1912   *is_new = true;
1913   Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1914                                false, false, location);
1915   Named_object* no = this->gogo_->add_variable(tid.name(), var);
1916   if (!no->is_variable())
1917     {
1918       // The name is already defined, so we just gave an error.
1919       return this->gogo_->add_sink();
1920     }
1921   return no;
1922 }
1923
1924 // Create a dummy global variable to force an initializer to be run in
1925 // the right place.  This is used when a sink variable is initialized
1926 // at global scope.
1927
1928 Named_object*
1929 Parse::create_dummy_global(Type* type, Expression* init,
1930                            source_location location)
1931 {
1932   if (type == NULL && init == NULL)
1933     type = Type::lookup_bool_type();
1934   Variable* var = new Variable(type, init, true, false, false, location);
1935   static int count;
1936   char buf[30];
1937   snprintf(buf, sizeof buf, "_.%d", count);
1938   ++count;
1939   return this->gogo_->add_variable(buf, var);
1940 }
1941
1942 // SimpleVarDecl = identifier ":=" Expression .
1943
1944 // We've already seen the identifier.
1945
1946 // FIXME: We also have to implement
1947 //  IdentifierList ":=" ExpressionList
1948 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1949 // tuple assignments here as well.
1950
1951 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
1952 // RangeClause.
1953
1954 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
1955 // guard (var := expr.("type") using the literal keyword "type").
1956
1957 void
1958 Parse::simple_var_decl_or_assignment(const std::string& name,
1959                                      source_location location,
1960                                      Range_clause* p_range_clause,
1961                                      Type_switch* p_type_switch)
1962 {
1963   Typed_identifier_list til;
1964   til.push_back(Typed_identifier(name, NULL, location));
1965
1966   // We've seen one identifier.  If we see a comma now, this could be
1967   // "a, *p = 1, 2".
1968   if (this->peek_token()->is_op(OPERATOR_COMMA))
1969     {
1970       go_assert(p_type_switch == NULL);
1971       while (true)
1972         {
1973           const Token* token = this->advance_token();
1974           if (!token->is_identifier())
1975             break;
1976
1977           std::string id = token->identifier();
1978           bool is_id_exported = token->is_identifier_exported();
1979           source_location id_location = token->location();
1980
1981           token = this->advance_token();
1982           if (!token->is_op(OPERATOR_COMMA))
1983             {
1984               if (token->is_op(OPERATOR_COLONEQ))
1985                 {
1986                   id = this->gogo_->pack_hidden_name(id, is_id_exported);
1987                   til.push_back(Typed_identifier(id, NULL, location));
1988                 }
1989               else
1990                 this->unget_token(Token::make_identifier_token(id,
1991                                                                is_id_exported,
1992                                                                id_location));
1993               break;
1994             }
1995
1996           id = this->gogo_->pack_hidden_name(id, is_id_exported);
1997           til.push_back(Typed_identifier(id, NULL, location));
1998         }
1999
2000       // We have a comma separated list of identifiers in TIL.  If the
2001       // next token is COLONEQ, then this is a simple var decl, and we
2002       // have the complete list of identifiers.  If the next token is
2003       // not COLONEQ, then the only valid parse is a tuple assignment.
2004       // The list of identifiers we have so far is really a list of
2005       // expressions.  There are more expressions following.
2006
2007       if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2008         {
2009           Expression_list* exprs = new Expression_list;
2010           for (Typed_identifier_list::const_iterator p = til.begin();
2011                p != til.end();
2012                ++p)
2013             exprs->push_back(this->id_to_expression(p->name(),
2014                                                     p->location()));
2015
2016           Expression_list* more_exprs = this->expression_list(NULL, true);
2017           for (Expression_list::const_iterator p = more_exprs->begin();
2018                p != more_exprs->end();
2019                ++p)
2020             exprs->push_back(*p);
2021           delete more_exprs;
2022
2023           this->tuple_assignment(exprs, p_range_clause);
2024           return;
2025         }
2026     }
2027
2028   go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2029   const Token* token = this->advance_token();
2030
2031   if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2032     {
2033       this->range_clause_decl(&til, p_range_clause);
2034       return;
2035     }
2036
2037   Expression_list* init;
2038   if (p_type_switch == NULL)
2039     init = this->expression_list(NULL, false);
2040   else
2041     {
2042       bool is_type_switch = false;
2043       Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2044                                           &is_type_switch);
2045       if (is_type_switch)
2046         {
2047           p_type_switch->found = true;
2048           p_type_switch->name = name;
2049           p_type_switch->location = location;
2050           p_type_switch->expr = expr;
2051           return;
2052         }
2053
2054       if (!this->peek_token()->is_op(OPERATOR_COMMA))
2055         {
2056           init = new Expression_list();
2057           init->push_back(expr);
2058         }
2059       else
2060         {
2061           this->advance_token();
2062           init = this->expression_list(expr, false);
2063         }
2064     }
2065
2066   this->init_vars(&til, NULL, init, true, location);
2067 }
2068
2069 // FunctionDecl = "func" identifier Signature [ Block ] .
2070 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2071
2072 // gcc extension:
2073 //   FunctionDecl = "func" identifier Signature
2074 //                    __asm__ "(" string_lit ")" .
2075 // This extension means a function whose real name is the identifier
2076 // inside the asm.
2077
2078 void
2079 Parse::function_decl()
2080 {
2081   go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2082   source_location location = this->location();
2083   const Token* token = this->advance_token();
2084
2085   Typed_identifier* rec = NULL;
2086   if (token->is_op(OPERATOR_LPAREN))
2087     {
2088       rec = this->receiver();
2089       token = this->peek_token();
2090     }
2091
2092   if (!token->is_identifier())
2093     {
2094       error_at(this->location(), "expected function name");
2095       return;
2096     }
2097
2098   std::string name =
2099     this->gogo_->pack_hidden_name(token->identifier(),
2100                                   token->is_identifier_exported());
2101
2102   this->advance_token();
2103
2104   Function_type* fntype = this->signature(rec, this->location());
2105   if (fntype == NULL)
2106     return;
2107
2108   Named_object* named_object = NULL;
2109
2110   if (this->peek_token()->is_keyword(KEYWORD_ASM))
2111     {
2112       if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2113         {
2114           error_at(this->location(), "expected %<(%>");
2115           return;
2116         }
2117       token = this->advance_token();
2118       if (!token->is_string())
2119         {
2120           error_at(this->location(), "expected string");
2121           return;
2122         }
2123       std::string asm_name = token->string_value();
2124       if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2125         {
2126           error_at(this->location(), "expected %<)%>");
2127           return;
2128         }
2129       this->advance_token();
2130       if (!Gogo::is_sink_name(name))
2131         {
2132           named_object = this->gogo_->declare_function(name, fntype, location);
2133           if (named_object->is_function_declaration())
2134             named_object->func_declaration_value()->set_asm_name(asm_name);
2135         }
2136     }
2137
2138   // Check for the easy error of a newline before the opening brace.
2139   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2140     {
2141       source_location semi_loc = this->location();
2142       if (this->advance_token()->is_op(OPERATOR_LCURLY))
2143         error_at(this->location(),
2144                  "unexpected semicolon or newline before %<{%>");
2145       else
2146         this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2147                                                      semi_loc));
2148     }
2149
2150   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2151     {
2152       if (named_object == NULL && !Gogo::is_sink_name(name))
2153         this->gogo_->declare_function(name, fntype, location);
2154     }
2155   else
2156     {
2157       this->gogo_->start_function(name, fntype, true, location);
2158       source_location end_loc = this->block();
2159       this->gogo_->finish_function(end_loc);
2160     }
2161 }
2162
2163 // Receiver     = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2164 // BaseTypeName = identifier .
2165
2166 Typed_identifier*
2167 Parse::receiver()
2168 {
2169   go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2170
2171   std::string name;
2172   const Token* token = this->advance_token();
2173   source_location location = token->location();
2174   if (!token->is_op(OPERATOR_MULT))
2175     {
2176       if (!token->is_identifier())
2177         {
2178           error_at(this->location(), "method has no receiver");
2179           while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2180             token = this->advance_token();
2181           if (!token->is_eof())
2182             this->advance_token();
2183           return NULL;
2184         }
2185       name = token->identifier();
2186       bool is_exported = token->is_identifier_exported();
2187       token = this->advance_token();
2188       if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2189         {
2190           // An identifier followed by something other than a dot or a
2191           // right parenthesis must be a receiver name followed by a
2192           // type.
2193           name = this->gogo_->pack_hidden_name(name, is_exported);
2194         }
2195       else
2196         {
2197           // This must be a type name.
2198           this->unget_token(Token::make_identifier_token(name, is_exported,
2199                                                          location));
2200           token = this->peek_token();
2201           name.clear();
2202         }
2203     }
2204
2205   // Here the receiver name is in NAME (it is empty if the receiver is
2206   // unnamed) and TOKEN is the first token in the type.
2207
2208   bool is_pointer = false;
2209   if (token->is_op(OPERATOR_MULT))
2210     {
2211       is_pointer = true;
2212       token = this->advance_token();
2213     }
2214
2215   if (!token->is_identifier())
2216     {
2217       error_at(this->location(), "expected receiver name or type");
2218       int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2219       while (!token->is_eof())
2220         {
2221           token = this->advance_token();
2222           if (token->is_op(OPERATOR_LPAREN))
2223             ++c;
2224           else if (token->is_op(OPERATOR_RPAREN))
2225             {
2226               if (c == 0)
2227                 break;
2228               --c;
2229             }
2230         }
2231       if (!token->is_eof())
2232         this->advance_token();
2233       return NULL;
2234     }
2235
2236   Type* type = this->type_name(true);
2237
2238   if (is_pointer && !type->is_error_type())
2239     type = Type::make_pointer_type(type);
2240
2241   if (this->peek_token()->is_op(OPERATOR_RPAREN))
2242     this->advance_token();
2243   else
2244     {
2245       if (this->peek_token()->is_op(OPERATOR_COMMA))
2246         error_at(this->location(), "method has multiple receivers");
2247       else
2248         error_at(this->location(), "expected %<)%>");
2249       while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2250         token = this->advance_token();
2251       if (!token->is_eof())
2252         this->advance_token();
2253       return NULL;
2254     }
2255
2256   return new Typed_identifier(name, type, location);
2257 }
2258
2259 // Operand    = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2260 // Literal    = BasicLit | CompositeLit | FunctionLit .
2261 // BasicLit   = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2262
2263 // If MAY_BE_SINK is true, this operand may be "_".
2264
2265 Expression*
2266 Parse::operand(bool may_be_sink)
2267 {
2268   const Token* token = this->peek_token();
2269   Expression* ret;
2270   switch (token->classification())
2271     {
2272     case Token::TOKEN_IDENTIFIER:
2273       {
2274         source_location location = token->location();
2275         std::string id = token->identifier();
2276         bool is_exported = token->is_identifier_exported();
2277         std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2278
2279         Named_object* in_function;
2280         Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2281
2282         Package* package = NULL;
2283         if (named_object != NULL && named_object->is_package())
2284           {
2285             if (!this->advance_token()->is_op(OPERATOR_DOT)
2286                 || !this->advance_token()->is_identifier())
2287               {
2288                 error_at(location, "unexpected reference to package");
2289                 return Expression::make_error(location);
2290               }
2291             package = named_object->package_value();
2292             package->set_used();
2293             id = this->peek_token()->identifier();
2294             is_exported = this->peek_token()->is_identifier_exported();
2295             packed = this->gogo_->pack_hidden_name(id, is_exported);
2296             named_object = package->lookup(packed);
2297             location = this->location();
2298             go_assert(in_function == NULL);
2299           }
2300
2301         this->advance_token();
2302
2303         if (named_object != NULL
2304             && named_object->is_type()
2305             && !named_object->type_value()->is_visible())
2306           {
2307             go_assert(package != NULL);
2308             error_at(location, "invalid reference to hidden type %<%s.%s%>",
2309                      Gogo::message_name(package->name()).c_str(),
2310                      Gogo::message_name(id).c_str());
2311             return Expression::make_error(location);
2312           }
2313
2314
2315         if (named_object == NULL)
2316           {
2317             if (package != NULL)
2318               {
2319                 std::string n1 = Gogo::message_name(package->name());
2320                 std::string n2 = Gogo::message_name(id);
2321                 if (!is_exported)
2322                   error_at(location,
2323                            ("invalid reference to unexported identifier "
2324                             "%<%s.%s%>"),
2325                            n1.c_str(), n2.c_str());
2326                 else
2327                   error_at(location,
2328                            "reference to undefined identifier %<%s.%s%>",
2329                            n1.c_str(), n2.c_str());
2330                 return Expression::make_error(location);
2331               }
2332
2333             named_object = this->gogo_->add_unknown_name(packed, location);
2334           }
2335
2336         if (in_function != NULL
2337             && in_function != this->gogo_->current_function()
2338             && (named_object->is_variable()
2339                 || named_object->is_result_variable()))
2340           return this->enclosing_var_reference(in_function, named_object,
2341                                                location);
2342
2343         switch (named_object->classification())
2344           {
2345           case Named_object::NAMED_OBJECT_CONST:
2346             return Expression::make_const_reference(named_object, location);
2347           case Named_object::NAMED_OBJECT_TYPE:
2348             return Expression::make_type(named_object->type_value(), location);
2349           case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2350             {
2351               Type* t = Type::make_forward_declaration(named_object);
2352               return Expression::make_type(t, location);
2353             }
2354           case Named_object::NAMED_OBJECT_VAR:
2355           case Named_object::NAMED_OBJECT_RESULT_VAR:
2356             return Expression::make_var_reference(named_object, location);
2357           case Named_object::NAMED_OBJECT_SINK:
2358             if (may_be_sink)
2359               return Expression::make_sink(location);
2360             else
2361               {
2362                 error_at(location, "cannot use _ as value");
2363                 return Expression::make_error(location);
2364               }
2365           case Named_object::NAMED_OBJECT_FUNC:
2366           case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2367             return Expression::make_func_reference(named_object, NULL,
2368                                                    location);
2369           case Named_object::NAMED_OBJECT_UNKNOWN:
2370             return Expression::make_unknown_reference(named_object, location);
2371           default:
2372             go_unreachable();
2373           }
2374       }
2375       go_unreachable();
2376
2377     case Token::TOKEN_STRING:
2378       ret = Expression::make_string(token->string_value(), token->location());
2379       this->advance_token();
2380       return ret;
2381
2382     case Token::TOKEN_INTEGER:
2383       ret = Expression::make_integer(token->integer_value(), NULL,
2384                                      token->location());
2385       this->advance_token();
2386       return ret;
2387
2388     case Token::TOKEN_FLOAT:
2389       ret = Expression::make_float(token->float_value(), NULL,
2390                                    token->location());
2391       this->advance_token();
2392       return ret;
2393
2394     case Token::TOKEN_IMAGINARY:
2395       {
2396         mpfr_t zero;
2397         mpfr_init_set_ui(zero, 0, GMP_RNDN);
2398         ret = Expression::make_complex(&zero, token->imaginary_value(),
2399                                        NULL, token->location());
2400         mpfr_clear(zero);
2401         this->advance_token();
2402         return ret;
2403       }
2404
2405     case Token::TOKEN_KEYWORD:
2406       switch (token->keyword())
2407         {
2408         case KEYWORD_FUNC:
2409           return this->function_lit();
2410         case KEYWORD_CHAN:
2411         case KEYWORD_INTERFACE:
2412         case KEYWORD_MAP:
2413         case KEYWORD_STRUCT:
2414           {
2415             source_location location = token->location();
2416             return Expression::make_type(this->type(), location);
2417           }
2418         default:
2419           break;
2420         }
2421       break;
2422
2423     case Token::TOKEN_OPERATOR:
2424       if (token->is_op(OPERATOR_LPAREN))
2425         {
2426           this->advance_token();
2427           ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2428           if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2429             error_at(this->location(), "missing %<)%>");
2430           else
2431             this->advance_token();
2432           return ret;
2433         }
2434       else if (token->is_op(OPERATOR_LSQUARE))
2435         {
2436           // Here we call array_type directly, as this is the only
2437           // case where an ellipsis is permitted for an array type.
2438           source_location location = token->location();
2439           return Expression::make_type(this->array_type(true), location);
2440         }
2441       break;
2442
2443     default:
2444       break;
2445     }
2446
2447   error_at(this->location(), "expected operand");
2448   return Expression::make_error(this->location());
2449 }
2450
2451 // Handle a reference to a variable in an enclosing function.  We add
2452 // it to a list of such variables.  We return a reference to a field
2453 // in a struct which will be passed on the static chain when calling
2454 // the current function.
2455
2456 Expression*
2457 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2458                                source_location location)
2459 {
2460   go_assert(var->is_variable() || var->is_result_variable());
2461
2462   Named_object* this_function = this->gogo_->current_function();
2463   Named_object* closure = this_function->func_value()->closure_var();
2464
2465   Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2466   std::pair<Enclosing_vars::iterator, bool> ins =
2467     this->enclosing_vars_.insert(ev);
2468   if (ins.second)
2469     {
2470       // This is a variable we have not seen before.  Add a new field
2471       // to the closure type.
2472       this_function->func_value()->add_closure_field(var, location);
2473     }
2474
2475   Expression* closure_ref = Expression::make_var_reference(closure,
2476                                                            location);
2477   closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2478
2479   // The closure structure holds pointers to the variables, so we need
2480   // to introduce an indirection.
2481   Expression* e = Expression::make_field_reference(closure_ref,
2482                                                    ins.first->index(),
2483                                                    location);
2484   e = Expression::make_unary(OPERATOR_MULT, e, location);
2485   return e;
2486 }
2487
2488 // CompositeLit  = LiteralType LiteralValue .
2489 // LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
2490 //                 SliceType | MapType | TypeName .
2491 // LiteralValue  = "{" [ ElementList [ "," ] ] "}" .
2492 // ElementList   = Element { "," Element } .
2493 // Element       = [ Key ":" ] Value .
2494 // Key           = FieldName | ElementIndex .
2495 // FieldName     = identifier .
2496 // ElementIndex  = Expression .
2497 // Value         = Expression | LiteralValue .
2498
2499 // We have already seen the type if there is one, and we are now
2500 // looking at the LiteralValue.  The case "[" "..."  "]" ElementType
2501 // will be seen here as an array type whose length is "nil".  The
2502 // DEPTH parameter is non-zero if this is an embedded composite
2503 // literal and the type was omitted.  It gives the number of steps up
2504 // to the type which was provided.  E.g., in [][]int{{1}} it will be
2505 // 1.  In [][][]int{{{1}}} it will be 2.
2506
2507 Expression*
2508 Parse::composite_lit(Type* type, int depth, source_location location)
2509 {
2510   go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2511   this->advance_token();
2512
2513   if (this->peek_token()->is_op(OPERATOR_RCURLY))
2514     {
2515       this->advance_token();
2516       return Expression::make_composite_literal(type, depth, false, NULL,
2517                                                 location);
2518     }
2519
2520   bool has_keys = false;
2521   Expression_list* vals = new Expression_list;
2522   while (true)
2523     {
2524       Expression* val;
2525       bool is_type_omitted = false;
2526
2527       const Token* token = this->peek_token();
2528
2529       if (token->is_identifier())
2530         {
2531           std::string identifier = token->identifier();
2532           bool is_exported = token->is_identifier_exported();
2533           source_location location = token->location();
2534
2535           if (this->advance_token()->is_op(OPERATOR_COLON))
2536             {
2537               // This may be a field name.  We don't know for sure--it
2538               // could also be an expression for an array index.  We
2539               // don't want to parse it as an expression because may
2540               // trigger various errors, e.g., if this identifier
2541               // happens to be the name of a package.
2542               Gogo* gogo = this->gogo_;
2543               val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2544                                                                   is_exported),
2545                                            location);
2546             }
2547           else
2548             {
2549               this->unget_token(Token::make_identifier_token(identifier,
2550                                                              is_exported,
2551                                                              location));
2552               val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2553             }
2554         }
2555       else if (!token->is_op(OPERATOR_LCURLY))
2556         val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2557       else
2558         {
2559           // This must be a composite literal inside another composite
2560           // literal, with the type omitted for the inner one.
2561           val = this->composite_lit(type, depth + 1, token->location());
2562           is_type_omitted = true;
2563         }
2564
2565       token = this->peek_token();
2566       if (!token->is_op(OPERATOR_COLON))
2567         {
2568           if (has_keys)
2569             vals->push_back(NULL);
2570         }
2571       else
2572         {
2573           if (is_type_omitted && !val->is_error_expression())
2574             {
2575               error_at(this->location(), "unexpected %<:%>");
2576               val = Expression::make_error(this->location());
2577             }
2578
2579           this->advance_token();
2580
2581           if (!has_keys && !vals->empty())
2582             {
2583               Expression_list* newvals = new Expression_list;
2584               for (Expression_list::const_iterator p = vals->begin();
2585                    p != vals->end();
2586                    ++p)
2587                 {
2588                   newvals->push_back(NULL);
2589                   newvals->push_back(*p);
2590                 }
2591               delete vals;
2592               vals = newvals;
2593             }
2594           has_keys = true;
2595
2596           if (val->unknown_expression() != NULL)
2597             val->unknown_expression()->set_is_composite_literal_key();
2598
2599           vals->push_back(val);
2600
2601           if (!token->is_op(OPERATOR_LCURLY))
2602             val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2603           else
2604             {
2605               // This must be a composite literal inside another
2606               // composite literal, with the type omitted for the
2607               // inner one.
2608               val = this->composite_lit(type, depth + 1, token->location());
2609             }
2610
2611           token = this->peek_token();
2612         }
2613
2614       vals->push_back(val);
2615
2616       if (token->is_op(OPERATOR_COMMA))
2617         {
2618           if (this->advance_token()->is_op(OPERATOR_RCURLY))
2619             {
2620               this->advance_token();
2621               break;
2622             }
2623         }
2624       else if (token->is_op(OPERATOR_RCURLY))
2625         {
2626           this->advance_token();
2627           break;
2628         }
2629       else
2630         {
2631           error_at(this->location(), "expected %<,%> or %<}%>");
2632
2633           int depth = 0;
2634           while (!token->is_eof()
2635                  && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2636             {
2637               if (token->is_op(OPERATOR_LCURLY))
2638                 ++depth;
2639               else if (token->is_op(OPERATOR_RCURLY))
2640                 --depth;
2641               token = this->advance_token();
2642             }
2643           if (token->is_op(OPERATOR_RCURLY))
2644             this->advance_token();
2645
2646           return Expression::make_error(location);
2647         }
2648     }
2649
2650   return Expression::make_composite_literal(type, depth, has_keys, vals,
2651                                             location);
2652 }
2653
2654 // FunctionLit = "func" Signature Block .
2655
2656 Expression*
2657 Parse::function_lit()
2658 {
2659   source_location location = this->location();
2660   go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2661   this->advance_token();
2662
2663   Enclosing_vars hold_enclosing_vars;
2664   hold_enclosing_vars.swap(this->enclosing_vars_);
2665
2666   Function_type* type = this->signature(NULL, location);
2667   if (type == NULL)
2668     type = Type::make_function_type(NULL, NULL, NULL, location);
2669
2670   // For a function literal, the next token must be a '{'.  If we
2671   // don't see that, then we may have a type expression.
2672   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2673     return Expression::make_type(type, location);
2674
2675   Bc_stack* hold_break_stack = this->break_stack_;
2676   Bc_stack* hold_continue_stack = this->continue_stack_;
2677   this->break_stack_ = NULL;
2678   this->continue_stack_ = NULL;
2679
2680   Named_object* no = this->gogo_->start_function("", type, true, location);
2681
2682   source_location end_loc = this->block();
2683
2684   this->gogo_->finish_function(end_loc);
2685
2686   if (this->break_stack_ != NULL)
2687     delete this->break_stack_;
2688   if (this->continue_stack_ != NULL)
2689     delete this->continue_stack_;
2690   this->break_stack_ = hold_break_stack;
2691   this->continue_stack_ = hold_continue_stack;
2692
2693   hold_enclosing_vars.swap(this->enclosing_vars_);
2694
2695   Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2696                                              location);
2697
2698   return Expression::make_func_reference(no, closure, location);
2699 }
2700
2701 // Create a closure for the nested function FUNCTION.  This is based
2702 // on ENCLOSING_VARS, which is a list of all variables defined in
2703 // enclosing functions and referenced from FUNCTION.  A closure is the
2704 // address of a struct which contains the addresses of all the
2705 // referenced variables.  This returns NULL if no closure is required.
2706
2707 Expression*
2708 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2709                       source_location location)
2710 {
2711   if (enclosing_vars->empty())
2712     return NULL;
2713
2714   // Get the variables in order by their field index.
2715
2716   size_t enclosing_var_count = enclosing_vars->size();
2717   std::vector<Enclosing_var> ev(enclosing_var_count);
2718   for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2719        p != enclosing_vars->end();
2720        ++p)
2721     ev[p->index()] = *p;
2722
2723   // Build an initializer for a composite literal of the closure's
2724   // type.
2725
2726   Named_object* enclosing_function = this->gogo_->current_function();
2727   Expression_list* initializer = new Expression_list;
2728   for (size_t i = 0; i < enclosing_var_count; ++i)
2729     {
2730       go_assert(ev[i].index() == i);
2731       Named_object* var = ev[i].var();
2732       Expression* ref;
2733       if (ev[i].in_function() == enclosing_function)
2734         ref = Expression::make_var_reference(var, location);
2735       else
2736         ref = this->enclosing_var_reference(ev[i].in_function(), var,
2737                                             location);
2738       Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2739                                                    location);
2740       initializer->push_back(refaddr);
2741     }
2742
2743   Named_object* closure_var = function->func_value()->closure_var();
2744   Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2745   Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2746                                                              location);
2747   return Expression::make_heap_composite(cv, location);
2748 }
2749
2750 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2751
2752 // If MAY_BE_SINK is true, this expression may be "_".
2753
2754 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2755 // literal.
2756
2757 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2758 // guard (var := expr.("type") using the literal keyword "type").
2759
2760 Expression*
2761 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2762                     bool* is_type_switch)
2763 {
2764   source_location start_loc = this->location();
2765   bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2766
2767   Expression* ret = this->operand(may_be_sink);
2768
2769   // An unknown name followed by a curly brace must be a composite
2770   // literal, and the unknown name must be a type.
2771   if (may_be_composite_lit
2772       && !is_parenthesized
2773       && ret->unknown_expression() != NULL
2774       && this->peek_token()->is_op(OPERATOR_LCURLY))
2775     {
2776       Named_object* no = ret->unknown_expression()->named_object();
2777       Type* type = Type::make_forward_declaration(no);
2778       ret = Expression::make_type(type, ret->location());
2779     }
2780
2781   // We handle composite literals and type casts here, as it is the
2782   // easiest way to handle types which are in parentheses, as in
2783   // "((uint))(1)".
2784   if (ret->is_type_expression())
2785     {
2786       if (this->peek_token()->is_op(OPERATOR_LCURLY))
2787         {
2788           if (is_parenthesized)
2789             error_at(start_loc,
2790                      "cannot parenthesize type in composite literal");
2791           ret = this->composite_lit(ret->type(), 0, ret->location());
2792         }
2793       else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2794         {
2795           source_location loc = this->location();
2796           this->advance_token();
2797           Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2798                                               NULL);
2799           if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2800             {
2801               error_at(this->location(),
2802                        "invalid use of %<...%> in type conversion");
2803               this->advance_token();
2804             }
2805           if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2806             error_at(this->location(), "expected %<)%>");
2807           else
2808             this->advance_token();
2809           if (expr->is_error_expression())
2810             ret = expr;
2811           else
2812             {
2813               Type* t = ret->type();
2814               if (t->classification() == Type::TYPE_ARRAY
2815                   && t->array_type()->length() != NULL
2816                   && t->array_type()->length()->is_nil_expression())
2817                 {
2818                   error_at(ret->location(),
2819                            "invalid use of %<...%> in type conversion");
2820                   ret = Expression::make_error(loc);
2821                 }
2822               else
2823                 ret = Expression::make_cast(t, expr, loc);
2824             }
2825         }
2826     }
2827
2828   while (true)
2829     {
2830       const Token* token = this->peek_token();
2831       if (token->is_op(OPERATOR_LPAREN))
2832         ret = this->call(this->verify_not_sink(ret));
2833       else if (token->is_op(OPERATOR_DOT))
2834         {
2835           ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2836           if (is_type_switch != NULL && *is_type_switch)
2837             break;
2838         }
2839       else if (token->is_op(OPERATOR_LSQUARE))
2840         ret = this->index(this->verify_not_sink(ret));
2841       else
2842         break;
2843     }
2844
2845   return ret;
2846 }
2847
2848 // Selector = "." identifier .
2849 // TypeGuard = "." "(" QualifiedIdent ")" .
2850
2851 // Note that Operand can expand to QualifiedIdent, which contains a
2852 // ".".  That is handled directly in operand when it sees a package
2853 // name.
2854
2855 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2856 // guard (var := expr.("type") using the literal keyword "type").
2857
2858 Expression*
2859 Parse::selector(Expression* left, bool* is_type_switch)
2860 {
2861   go_assert(this->peek_token()->is_op(OPERATOR_DOT));
2862   source_location location = this->location();
2863
2864   const Token* token = this->advance_token();
2865   if (token->is_identifier())
2866     {
2867       // This could be a field in a struct, or a method in an
2868       // interface, or a method associated with a type.  We can't know
2869       // which until we have seen all the types.
2870       std::string name =
2871         this->gogo_->pack_hidden_name(token->identifier(),
2872                                       token->is_identifier_exported());
2873       if (token->identifier() == "_")
2874         {
2875           error_at(this->location(), "invalid use of %<_%>");
2876           name = this->gogo_->pack_hidden_name("blank", false);
2877         }
2878       this->advance_token();
2879       return Expression::make_selector(left, name, location);
2880     }
2881   else if (token->is_op(OPERATOR_LPAREN))
2882     {
2883       this->advance_token();
2884       Type* type = NULL;
2885       if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
2886         type = this->type();
2887       else
2888         {
2889           if (is_type_switch != NULL)
2890             *is_type_switch = true;
2891           else
2892             {
2893               error_at(this->location(),
2894                        "use of %<.(type)%> outside type switch");
2895               type = Type::make_error_type();
2896             }
2897           this->advance_token();
2898         }
2899       if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2900         error_at(this->location(), "missing %<)%>");
2901       else
2902         this->advance_token();
2903       if (is_type_switch != NULL && *is_type_switch)
2904         return left;
2905       return Expression::make_type_guard(left, type, location);
2906     }
2907   else
2908     {
2909       error_at(this->location(), "expected identifier or %<(%>");
2910       return left;
2911     }
2912 }
2913
2914 // Index          = "[" Expression "]" .
2915 // Slice          = "[" Expression ":" [ Expression ] "]" .
2916
2917 Expression*
2918 Parse::index(Expression* expr)
2919 {
2920   source_location location = this->location();
2921   go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
2922   this->advance_token();
2923
2924   Expression* start;
2925   if (!this->peek_token()->is_op(OPERATOR_COLON))
2926     start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2927   else
2928     {
2929       mpz_t zero;
2930       mpz_init_set_ui(zero, 0);
2931       start = Expression::make_integer(&zero, NULL, location);
2932       mpz_clear(zero);
2933     }
2934
2935   Expression* end = NULL;
2936   if (this->peek_token()->is_op(OPERATOR_COLON))
2937     {
2938       // We use nil to indicate a missing high expression.
2939       if (this->advance_token()->is_op(OPERATOR_RSQUARE))
2940         end = Expression::make_nil(this->location());
2941       else
2942         end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2943     }
2944   if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
2945     error_at(this->location(), "missing %<]%>");
2946   else
2947     this->advance_token();
2948   return Expression::make_index(expr, start, end, location);
2949 }
2950
2951 // Call           = "(" [ ArgumentList [ "," ] ] ")" .
2952 // ArgumentList   = ExpressionList [ "..." ] .
2953
2954 Expression*
2955 Parse::call(Expression* func)
2956 {
2957   go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2958   Expression_list* args = NULL;
2959   bool is_varargs = false;
2960   const Token* token = this->advance_token();
2961   if (!token->is_op(OPERATOR_RPAREN))
2962     {
2963       args = this->expression_list(NULL, false);
2964       token = this->peek_token();
2965       if (token->is_op(OPERATOR_ELLIPSIS))
2966         {
2967           is_varargs = true;
2968           token = this->advance_token();
2969         }
2970     }
2971   if (token->is_op(OPERATOR_COMMA))
2972     token = this->advance_token();
2973   if (!token->is_op(OPERATOR_RPAREN))
2974     error_at(this->location(), "missing %<)%>");
2975   else
2976     this->advance_token();
2977   if (func->is_error_expression())
2978     return func;
2979   return Expression::make_call(func, args, is_varargs, func->location());
2980 }
2981
2982 // Return an expression for a single unqualified identifier.
2983
2984 Expression*
2985 Parse::id_to_expression(const std::string& name, source_location location)
2986 {
2987   Named_object* in_function;
2988   Named_object* named_object = this->gogo_->lookup(name, &in_function);
2989   if (named_object == NULL)
2990     named_object = this->gogo_->add_unknown_name(name, location);
2991
2992   if (in_function != NULL
2993       && in_function != this->gogo_->current_function()
2994       && (named_object->is_variable() || named_object->is_result_variable()))
2995     return this->enclosing_var_reference(in_function, named_object,
2996                                          location);
2997
2998   switch (named_object->classification())
2999     {
3000     case Named_object::NAMED_OBJECT_CONST:
3001       return Expression::make_const_reference(named_object, location);
3002     case Named_object::NAMED_OBJECT_VAR:
3003     case Named_object::NAMED_OBJECT_RESULT_VAR:
3004       return Expression::make_var_reference(named_object, location);
3005     case Named_object::NAMED_OBJECT_SINK:
3006       return Expression::make_sink(location);
3007     case Named_object::NAMED_OBJECT_FUNC:
3008     case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3009       return Expression::make_func_reference(named_object, NULL, location);
3010     case Named_object::NAMED_OBJECT_UNKNOWN:
3011       return Expression::make_unknown_reference(named_object, location);
3012     case Named_object::NAMED_OBJECT_PACKAGE:
3013     case Named_object::NAMED_OBJECT_TYPE:
3014     case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3015       // These cases can arise for a field name in a composite
3016       // literal.
3017       return Expression::make_unknown_reference(named_object, location);
3018     default:
3019       error_at(this->location(), "unexpected type of identifier");
3020       return Expression::make_error(location);
3021     }
3022 }
3023
3024 // Expression = UnaryExpr { binary_op Expression } .
3025
3026 // PRECEDENCE is the precedence of the current operator.
3027
3028 // If MAY_BE_SINK is true, this expression may be "_".
3029
3030 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3031 // literal.
3032
3033 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3034 // guard (var := expr.("type") using the literal keyword "type").
3035
3036 Expression*
3037 Parse::expression(Precedence precedence, bool may_be_sink,
3038                   bool may_be_composite_lit, bool* is_type_switch)
3039 {
3040   Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3041                                       is_type_switch);
3042
3043   while (true)
3044     {
3045       if (is_type_switch != NULL && *is_type_switch)
3046         return left;
3047
3048       const Token* token = this->peek_token();
3049       if (token->classification() != Token::TOKEN_OPERATOR)
3050         {
3051           // Not a binary_op.
3052           return left;
3053         }
3054
3055       Precedence right_precedence;
3056       switch (token->op())
3057         {
3058         case OPERATOR_OROR:
3059           right_precedence = PRECEDENCE_OROR;
3060           break;
3061         case OPERATOR_ANDAND:
3062           right_precedence = PRECEDENCE_ANDAND;
3063           break;
3064         case OPERATOR_EQEQ:
3065         case OPERATOR_NOTEQ:
3066         case OPERATOR_LT:
3067         case OPERATOR_LE:
3068         case OPERATOR_GT:
3069         case OPERATOR_GE:
3070           right_precedence = PRECEDENCE_RELOP;
3071           break;
3072         case OPERATOR_PLUS:
3073         case OPERATOR_MINUS:
3074         case OPERATOR_OR:
3075         case OPERATOR_XOR:
3076           right_precedence = PRECEDENCE_ADDOP;
3077           break;
3078         case OPERATOR_MULT:
3079         case OPERATOR_DIV:
3080         case OPERATOR_MOD:
3081         case OPERATOR_LSHIFT:
3082         case OPERATOR_RSHIFT:
3083         case OPERATOR_AND:
3084         case OPERATOR_BITCLEAR:
3085           right_precedence = PRECEDENCE_MULOP;
3086           break;
3087         default:
3088           right_precedence = PRECEDENCE_INVALID;
3089           break;
3090         }
3091
3092       if (right_precedence == PRECEDENCE_INVALID)
3093         {
3094           // Not a binary_op.
3095           return left;
3096         }
3097
3098       Operator op = token->op();
3099       source_location binop_location = token->location();
3100
3101       if (precedence >= right_precedence)
3102         {
3103           // We've already seen A * B, and we see + C.  We want to
3104           // return so that A * B becomes a group.
3105           return left;
3106         }
3107
3108       this->advance_token();
3109
3110       left = this->verify_not_sink(left);
3111       Expression* right = this->expression(right_precedence, false,
3112                                            may_be_composite_lit,
3113                                            NULL);
3114       left = Expression::make_binary(op, left, right, binop_location);
3115     }
3116 }
3117
3118 bool
3119 Parse::expression_may_start_here()
3120 {
3121   const Token* token = this->peek_token();
3122   switch (token->classification())
3123     {
3124     case Token::TOKEN_INVALID:
3125     case Token::TOKEN_EOF:
3126       return false;
3127     case Token::TOKEN_KEYWORD:
3128       switch (token->keyword())
3129         {
3130         case KEYWORD_CHAN:
3131         case KEYWORD_FUNC:
3132         case KEYWORD_MAP:
3133         case KEYWORD_STRUCT:
3134         case KEYWORD_INTERFACE:
3135           return true;
3136         default:
3137           return false;
3138         }
3139     case Token::TOKEN_IDENTIFIER:
3140       return true;
3141     case Token::TOKEN_STRING:
3142       return true;
3143     case Token::TOKEN_OPERATOR:
3144       switch (token->op())
3145         {
3146         case OPERATOR_PLUS:
3147         case OPERATOR_MINUS:
3148         case OPERATOR_NOT:
3149         case OPERATOR_XOR:
3150         case OPERATOR_MULT:
3151         case OPERATOR_CHANOP:
3152         case OPERATOR_AND:
3153         case OPERATOR_LPAREN:
3154         case OPERATOR_LSQUARE:
3155           return true;
3156         default:
3157           return false;
3158         }
3159     case Token::TOKEN_INTEGER:
3160     case Token::TOKEN_FLOAT:
3161     case Token::TOKEN_IMAGINARY:
3162       return true;
3163     default:
3164       go_unreachable();
3165     }
3166 }
3167
3168 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3169
3170 // If MAY_BE_SINK is true, this expression may be "_".
3171
3172 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3173 // literal.
3174
3175 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3176 // guard (var := expr.("type") using the literal keyword "type").
3177
3178 Expression*
3179 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3180                   bool* is_type_switch)
3181 {
3182   const Token* token = this->peek_token();
3183   if (token->is_op(OPERATOR_PLUS)
3184       || token->is_op(OPERATOR_MINUS)
3185       || token->is_op(OPERATOR_NOT)
3186       || token->is_op(OPERATOR_XOR)
3187       || token->is_op(OPERATOR_CHANOP)
3188       || token->is_op(OPERATOR_MULT)
3189       || token->is_op(OPERATOR_AND))
3190     {
3191       source_location location = token->location();
3192       Operator op = token->op();
3193       this->advance_token();
3194
3195       if (op == OPERATOR_CHANOP
3196           && this->peek_token()->is_keyword(KEYWORD_CHAN))
3197         {
3198           // This is "<- chan" which must be the start of a type.
3199           this->unget_token(Token::make_operator_token(op, location));
3200           return Expression::make_type(this->type(), location);
3201         }
3202
3203       Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
3204       if (expr->is_error_expression())
3205         ;
3206       else if (op == OPERATOR_MULT && expr->is_type_expression())
3207         expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3208                                      location);
3209       else if (op == OPERATOR_AND && expr->is_composite_literal())
3210         expr = Expression::make_heap_composite(expr, location);
3211       else if (op != OPERATOR_CHANOP)
3212         expr = Expression::make_unary(op, expr, location);
3213       else
3214         expr = Expression::make_receive(expr, location);
3215       return expr;
3216     }
3217   else
3218     return this->primary_expr(may_be_sink, may_be_composite_lit,
3219                               is_type_switch);
3220 }
3221
3222 // Statement =
3223 //      Declaration | LabeledStmt | SimpleStmt |
3224 //      GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3225 //      FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3226 //      DeferStmt .
3227
3228 // LABEL is the label of this statement if it has one.
3229
3230 void
3231 Parse::statement(Label* label)
3232 {
3233   const Token* token = this->peek_token();
3234   switch (token->classification())
3235     {
3236     case Token::TOKEN_KEYWORD:
3237       {
3238         switch (token->keyword())
3239           {
3240           case KEYWORD_CONST:
3241           case KEYWORD_TYPE:
3242           case KEYWORD_VAR:
3243             this->declaration();
3244             break;
3245           case KEYWORD_FUNC:
3246           case KEYWORD_MAP:
3247           case KEYWORD_STRUCT:
3248           case KEYWORD_INTERFACE:
3249             this->simple_stat(true, NULL, NULL, NULL);
3250             break;
3251           case KEYWORD_GO:
3252           case KEYWORD_DEFER:
3253             this->go_or_defer_stat();
3254             break;
3255           case KEYWORD_RETURN:
3256             this->return_stat();
3257             break;
3258           case KEYWORD_BREAK:
3259             this->break_stat();
3260             break;
3261           case KEYWORD_CONTINUE:
3262             this->continue_stat();
3263             break;
3264           case KEYWORD_GOTO:
3265             this->goto_stat();
3266             break;
3267           case KEYWORD_IF:
3268             this->if_stat();
3269             break;
3270           case KEYWORD_SWITCH:
3271             this->switch_stat(label);
3272             break;
3273           case KEYWORD_SELECT:
3274             this->select_stat(label);
3275             break;
3276           case KEYWORD_FOR:
3277             this->for_stat(label);
3278             break;
3279           default:
3280             error_at(this->location(), "expected statement");
3281             this->advance_token();
3282             break;
3283           }
3284       }
3285       break;
3286
3287     case Token::TOKEN_IDENTIFIER:
3288       {
3289         std::string identifier = token->identifier();
3290         bool is_exported = token->is_identifier_exported();
3291         source_location location = token->location();
3292         if (this->advance_token()->is_op(OPERATOR_COLON))
3293           {
3294             this->advance_token();
3295             this->labeled_stmt(identifier, location);
3296           }
3297         else
3298           {
3299             this->unget_token(Token::make_identifier_token(identifier,
3300                                                            is_exported,
3301                                                            location));
3302             this->simple_stat(true, NULL, NULL, NULL);
3303           }
3304       }
3305       break;
3306
3307     case Token::TOKEN_OPERATOR:
3308       if (token->is_op(OPERATOR_LCURLY))
3309         {
3310           source_location location = token->location();
3311           this->gogo_->start_block(location);
3312           source_location end_loc = this->block();
3313           this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3314                                  location);
3315         }
3316       else if (!token->is_op(OPERATOR_SEMICOLON))
3317         this->simple_stat(true, NULL, NULL, NULL);
3318       break;
3319
3320     case Token::TOKEN_STRING:
3321     case Token::TOKEN_INTEGER:
3322     case Token::TOKEN_FLOAT:
3323     case Token::TOKEN_IMAGINARY:
3324       this->simple_stat(true, NULL, NULL, NULL);
3325       break;
3326
3327     default:
3328       error_at(this->location(), "expected statement");
3329       this->advance_token();
3330       break;
3331     }
3332 }
3333
3334 bool
3335 Parse::statement_may_start_here()
3336 {
3337   const Token* token = this->peek_token();
3338   switch (token->classification())
3339     {
3340     case Token::TOKEN_KEYWORD:
3341       {
3342         switch (token->keyword())
3343           {
3344           case KEYWORD_CONST:
3345           case KEYWORD_TYPE:
3346           case KEYWORD_VAR:
3347           case KEYWORD_FUNC:
3348           case KEYWORD_MAP:
3349           case KEYWORD_STRUCT:
3350           case KEYWORD_INTERFACE:
3351           case KEYWORD_GO:
3352           case KEYWORD_DEFER:
3353           case KEYWORD_RETURN:
3354           case KEYWORD_BREAK:
3355           case KEYWORD_CONTINUE:
3356           case KEYWORD_GOTO:
3357           case KEYWORD_IF:
3358           case KEYWORD_SWITCH:
3359           case KEYWORD_SELECT:
3360           case KEYWORD_FOR:
3361             return true;
3362
3363           default:
3364             return false;
3365           }
3366       }
3367       break;
3368
3369     case Token::TOKEN_IDENTIFIER:
3370       return true;
3371
3372     case Token::TOKEN_OPERATOR:
3373       if (token->is_op(OPERATOR_LCURLY)
3374           || token->is_op(OPERATOR_SEMICOLON))
3375         return true;
3376       else
3377         return this->expression_may_start_here();
3378
3379     case Token::TOKEN_STRING:
3380     case Token::TOKEN_INTEGER:
3381     case Token::TOKEN_FLOAT:
3382     case Token::TOKEN_IMAGINARY:
3383       return true;
3384
3385     default:
3386       return false;
3387     }
3388 }
3389
3390 // LabeledStmt = Label ":" Statement .
3391 // Label       = identifier .
3392
3393 void
3394 Parse::labeled_stmt(const std::string& label_name, source_location location)
3395 {
3396   Label* label = this->gogo_->add_label_definition(label_name, location);
3397
3398   if (this->peek_token()->is_op(OPERATOR_RCURLY))
3399     {
3400       // This is a label at the end of a block.  A program is
3401       // permitted to omit a semicolon here.
3402       return;
3403     }
3404
3405   if (!this->statement_may_start_here())
3406     {
3407       // Mark the label as used to avoid a useless error about an
3408       // unused label.
3409       label->set_is_used();
3410
3411       error_at(location, "missing statement after label");
3412       this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3413                                                    location));
3414       return;
3415     }
3416
3417   this->statement(label);
3418 }
3419
3420 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3421 //      Assignment | ShortVarDecl .
3422
3423 // EmptyStmt was handled in Parse::statement.
3424
3425 // In order to make this work for if and switch statements, if
3426 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3427 // expression rather than adding an expression statement to the
3428 // current block.  If we see something other than an ExpressionStat,
3429 // we add the statement, set *RETURN_EXP to true if we saw a send
3430 // statement, and return NULL.  The handling of send statements is for
3431 // better error messages.
3432
3433 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3434 // RangeClause.
3435
3436 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3437 // guard (var := expr.("type") using the literal keyword "type").
3438
3439 Expression*
3440 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3441                    Range_clause* p_range_clause, Type_switch* p_type_switch)
3442 {
3443   const Token* token = this->peek_token();
3444
3445   // An identifier follow by := is a SimpleVarDecl.
3446   if (token->is_identifier())
3447     {
3448       std::string identifier = token->identifier();
3449       bool is_exported = token->is_identifier_exported();
3450       source_location location = token->location();
3451
3452       token = this->advance_token();
3453       if (token->is_op(OPERATOR_COLONEQ)
3454           || token->is_op(OPERATOR_COMMA))
3455         {
3456           identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3457           this->simple_var_decl_or_assignment(identifier, location,
3458                                               p_range_clause,
3459                                               (token->is_op(OPERATOR_COLONEQ)
3460                                                ? p_type_switch
3461                                                : NULL));
3462           return NULL;
3463         }
3464
3465       this->unget_token(Token::make_identifier_token(identifier, is_exported,
3466                                                      location));
3467     }
3468
3469   Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3470                                      may_be_composite_lit,
3471                                      (p_type_switch == NULL
3472                                       ? NULL
3473                                       : &p_type_switch->found));
3474   if (p_type_switch != NULL && p_type_switch->found)
3475     {
3476       p_type_switch->name.clear();
3477       p_type_switch->location = exp->location();
3478       p_type_switch->expr = this->verify_not_sink(exp);
3479       return NULL;
3480     }
3481   token = this->peek_token();
3482   if (token->is_op(OPERATOR_CHANOP))
3483     {
3484       this->send_stmt(this->verify_not_sink(exp));
3485       if (return_exp != NULL)
3486         *return_exp = true;
3487     }
3488   else if (token->is_op(OPERATOR_PLUSPLUS)
3489            || token->is_op(OPERATOR_MINUSMINUS))
3490     this->inc_dec_stat(this->verify_not_sink(exp));
3491   else if (token->is_op(OPERATOR_COMMA)
3492            || token->is_op(OPERATOR_EQ))
3493     this->assignment(exp, p_range_clause);
3494   else if (token->is_op(OPERATOR_PLUSEQ)
3495            || token->is_op(OPERATOR_MINUSEQ)
3496            || token->is_op(OPERATOR_OREQ)
3497            || token->is_op(OPERATOR_XOREQ)
3498            || token->is_op(OPERATOR_MULTEQ)
3499            || token->is_op(OPERATOR_DIVEQ)
3500            || token->is_op(OPERATOR_MODEQ)
3501            || token->is_op(OPERATOR_LSHIFTEQ)
3502            || token->is_op(OPERATOR_RSHIFTEQ)
3503            || token->is_op(OPERATOR_ANDEQ)
3504            || token->is_op(OPERATOR_BITCLEAREQ))
3505     this->assignment(this->verify_not_sink(exp), p_range_clause);
3506   else if (return_exp != NULL)
3507     return this->verify_not_sink(exp);
3508   else
3509     this->expression_stat(this->verify_not_sink(exp));
3510
3511   return NULL;
3512 }
3513
3514 bool
3515 Parse::simple_stat_may_start_here()
3516 {
3517   return this->expression_may_start_here();
3518 }
3519
3520 // Parse { Statement ";" } which is used in a few places.  The list of
3521 // statements may end with a right curly brace, in which case the
3522 // semicolon may be omitted.
3523
3524 void
3525 Parse::statement_list()
3526 {
3527   while (this->statement_may_start_here())
3528     {
3529       this->statement(NULL);
3530       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3531         this->advance_token();
3532       else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3533         break;
3534       else
3535         {
3536           if (!this->peek_token()->is_eof() || !saw_errors())
3537             error_at(this->location(), "expected %<;%> or %<}%> or newline");
3538           if (!this->skip_past_error(OPERATOR_RCURLY))
3539             return;
3540         }
3541     }
3542 }
3543
3544 bool
3545 Parse::statement_list_may_start_here()
3546 {
3547   return this->statement_may_start_here();
3548 }
3549
3550 // ExpressionStat = Expression .
3551
3552 void
3553 Parse::expression_stat(Expression* exp)
3554 {
3555   this->gogo_->add_statement(Statement::make_statement(exp, false));
3556 }
3557
3558 // SendStmt = Channel "&lt;-" Expression .
3559 // Channel  = Expression .
3560
3561 void
3562 Parse::send_stmt(Expression* channel)
3563 {
3564   go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3565   source_location loc = this->location();
3566   this->advance_token();
3567   Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3568   Statement* s = Statement::make_send_statement(channel, val, loc);
3569   this->gogo_->add_statement(s);
3570 }
3571
3572 // IncDecStat = Expression ( "++" | "--" ) .
3573
3574 void
3575 Parse::inc_dec_stat(Expression* exp)
3576 {
3577   const Token* token = this->peek_token();
3578
3579   // Lvalue maps require special handling.
3580   if (exp->index_expression() != NULL)
3581     exp->index_expression()->set_is_lvalue();
3582
3583   if (token->is_op(OPERATOR_PLUSPLUS))
3584     this->gogo_->add_statement(Statement::make_inc_statement(exp));
3585   else if (token->is_op(OPERATOR_MINUSMINUS))
3586     this->gogo_->add_statement(Statement::make_dec_statement(exp));
3587   else
3588     go_unreachable();
3589   this->advance_token();
3590 }
3591
3592 // Assignment = ExpressionList assign_op ExpressionList .
3593
3594 // EXP is an expression that we have already parsed.
3595
3596 // If RANGE_CLAUSE is not NULL, then this will recognize a
3597 // RangeClause.
3598
3599 void
3600 Parse::assignment(Expression* expr, Range_clause* p_range_clause)
3601 {
3602   Expression_list* vars;
3603   if (!this->peek_token()->is_op(OPERATOR_COMMA))
3604     {
3605       vars = new Expression_list();
3606       vars->push_back(expr);
3607     }
3608   else
3609     {
3610       this->advance_token();
3611       vars = this->expression_list(expr, true);
3612     }
3613
3614   this->tuple_assignment(vars, p_range_clause);
3615 }
3616
3617 // An assignment statement.  LHS is the list of expressions which
3618 // appear on the left hand side.
3619
3620 // If RANGE_CLAUSE is not NULL, then this will recognize a
3621 // RangeClause.
3622
3623 void
3624 Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
3625 {
3626   const Token* token = this->peek_token();
3627   if (!token->is_op(OPERATOR_EQ)
3628       && !token->is_op(OPERATOR_PLUSEQ)
3629       && !token->is_op(OPERATOR_MINUSEQ)
3630       && !token->is_op(OPERATOR_OREQ)
3631       && !token->is_op(OPERATOR_XOREQ)
3632       && !token->is_op(OPERATOR_MULTEQ)
3633       && !token->is_op(OPERATOR_DIVEQ)
3634       && !token->is_op(OPERATOR_MODEQ)
3635       && !token->is_op(OPERATOR_LSHIFTEQ)
3636       && !token->is_op(OPERATOR_RSHIFTEQ)
3637       && !token->is_op(OPERATOR_ANDEQ)
3638       && !token->is_op(OPERATOR_BITCLEAREQ))
3639     {
3640       error_at(this->location(), "expected assignment operator");
3641       return;
3642     }
3643   Operator op = token->op();
3644   source_location location = token->location();
3645
3646   token = this->advance_token();
3647
3648   if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
3649     {
3650       if (op != OPERATOR_EQ)
3651         error_at(this->location(), "range clause requires %<=%>");
3652       this->range_clause_expr(lhs, p_range_clause);
3653       return;
3654     }
3655
3656   Expression_list* vals = this->expression_list(NULL, false);
3657
3658   // We've parsed everything; check for errors.
3659   if (lhs == NULL || vals == NULL)
3660     return;
3661   for (Expression_list::const_iterator pe = lhs->begin();
3662        pe != lhs->end();
3663        ++pe)
3664     {
3665       if ((*pe)->is_error_expression())
3666         return;
3667       if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
3668         error_at((*pe)->location(), "cannot use _ as value");
3669     }
3670   for (Expression_list::const_iterator pe = vals->begin();
3671        pe != vals->end();
3672        ++pe)
3673     {
3674       if ((*pe)->is_error_expression())
3675         return;
3676     }
3677
3678   // Map expressions act differently when they are lvalues.
3679   for (Expression_list::iterator plv = lhs->begin();
3680        plv != lhs->end();
3681        ++plv)
3682     if ((*plv)->index_expression() != NULL)
3683       (*plv)->index_expression()->set_is_lvalue();
3684
3685   Call_expression* call;
3686   Index_expression* map_index;
3687   Receive_expression* receive;
3688   Type_guard_expression* type_guard;
3689   if (lhs->size() == vals->size())
3690     {
3691       Statement* s;
3692       if (lhs->size() > 1)
3693         {
3694           if (op != OPERATOR_EQ)
3695             error_at(location, "multiple values only permitted with %<=%>");
3696           s = Statement::make_tuple_assignment(lhs, vals, location);
3697         }
3698       else
3699         {
3700           if (op == OPERATOR_EQ)
3701             s = Statement::make_assignment(lhs->front(), vals->front(),
3702                                            location);
3703           else
3704             s = Statement::make_assignment_operation(op, lhs->front(),
3705                                                      vals->front(), location);
3706           delete lhs;
3707           delete vals;
3708         }
3709       this->gogo_->add_statement(s);
3710     }
3711   else if (vals->size() == 1
3712            && (call = (*vals->begin())->call_expression()) != NULL)
3713     {
3714       if (op != OPERATOR_EQ)
3715         error_at(location, "multiple results only permitted with %<=%>");
3716       delete vals;
3717       vals = new Expression_list;
3718       for (unsigned int i = 0; i < lhs->size(); ++i)
3719         vals->push_back(Expression::make_call_result(call, i));
3720       Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
3721       this->gogo_->add_statement(s);
3722     }
3723   else if (lhs->size() == 2
3724            && vals->size() == 1
3725            && (map_index = (*vals->begin())->index_expression()) != NULL)
3726     {
3727       if (op != OPERATOR_EQ)
3728         error_at(location, "two values from map requires %<=%>");
3729       Expression* val = lhs->front();
3730       Expression* present = lhs->back();
3731       Statement* s = Statement::make_tuple_map_assignment(val, present,
3732                                                           map_index, location);
3733       this->gogo_->add_statement(s);
3734     }
3735   else if (lhs->size() == 1
3736            && vals->size() == 2
3737            && (map_index = lhs->front()->index_expression()) != NULL)
3738     {
3739       if (op != OPERATOR_EQ)
3740         error_at(location, "assigning tuple to map index requires %<=%>");
3741       Expression* val = vals->front();
3742       Expression* should_set = vals->back();
3743       Statement* s = Statement::make_map_assignment(map_index, val, should_set,
3744                                                     location);
3745       this->gogo_->add_statement(s);
3746     }
3747   else if (lhs->size() == 2
3748            && vals->size() == 1
3749            && (receive = (*vals->begin())->receive_expression()) != NULL)
3750     {
3751       if (op != OPERATOR_EQ)
3752         error_at(location, "two values from receive requires %<=%>");
3753       Expression* val = lhs->front();
3754       Expression* success = lhs->back();
3755       Expression* channel = receive->channel();
3756       Statement* s = Statement::make_tuple_receive_assignment(val, success,
3757                                                               channel,
3758                                                               false,
3759                                                               location);
3760       this->gogo_->add_statement(s);
3761     }
3762   else if (lhs->size() == 2
3763            && vals->size() == 1
3764            && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
3765     {
3766       if (op != OPERATOR_EQ)
3767         error_at(location, "two values from type guard requires %<=%>");
3768       Expression* val = lhs->front();
3769       Expression* ok = lhs->back();
3770       Expression* expr = type_guard->expr();
3771       Type* type = type_guard->type();
3772       Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
3773                                                                  expr, type,
3774                                                                  location);
3775       this->gogo_->add_statement(s);
3776     }
3777   else
3778     {
3779       error_at(location, "number of variables does not match number of values");
3780     }
3781 }
3782
3783 // GoStat = "go" Expression .
3784 // DeferStat = "defer" Expression .
3785
3786 void
3787 Parse::go_or_defer_stat()
3788 {
3789   go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
3790              || this->peek_token()->is_keyword(KEYWORD_DEFER));
3791   bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
3792   source_location stat_location = this->location();
3793   this->advance_token();
3794   source_location expr_location = this->location();
3795   Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3796   Call_expression* call_expr = expr->call_expression();
3797   if (call_expr == NULL)
3798     {
3799       error_at(expr_location, "expected call expression");
3800       return;
3801     }
3802
3803   // Make it easier to simplify go/defer statements by putting every
3804   // statement in its own block.
3805   this->gogo_->start_block(stat_location);
3806   Statement* stat;
3807   if (is_go)
3808     stat = Statement::make_go_statement(call_expr, stat_location);
3809   else
3810     stat = Statement::make_defer_statement(call_expr, stat_location);
3811   this->gogo_->add_statement(stat);
3812   this->gogo_->add_block(this->gogo_->finish_block(stat_location),
3813                          stat_location);
3814 }
3815
3816 // ReturnStat = "return" [ ExpressionList ] .
3817
3818 void
3819 Parse::return_stat()
3820 {
3821   go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
3822   source_location location = this->location();
3823   this->advance_token();
3824   Expression_list* vals = NULL;
3825   if (this->expression_may_start_here())
3826     vals = this->expression_list(NULL, false);
3827   this->gogo_->add_statement(Statement::make_return_statement(vals, location));
3828 }
3829
3830 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
3831 //          [ "else" ( IfStmt | Block ) ] .
3832
3833 void
3834 Parse::if_stat()
3835 {
3836   go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
3837   source_location location = this->location();
3838   this->advance_token();
3839
3840   this->gogo_->start_block(location);
3841
3842   bool saw_simple_stat = false;
3843   Expression* cond = NULL;
3844   bool saw_send_stmt;
3845   if (this->simple_stat_may_start_here())
3846     {
3847       cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
3848       saw_simple_stat = true;
3849     }
3850   if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3851     {
3852       // The SimpleStat is an expression statement.
3853       this->expression_stat(cond);
3854       cond = NULL;
3855     }
3856   if (cond == NULL)
3857     {
3858       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3859         this->advance_token();
3860       else if (saw_simple_stat)
3861         {
3862           if (saw_send_stmt)
3863             error_at(this->location(),
3864                      ("send statement used as value; "
3865                       "use select for non-blocking send"));
3866           else
3867             error_at(this->location(),
3868                      "expected %<;%> after statement in if expression");
3869           if (!this->expression_may_start_here())
3870             cond = Expression::make_error(this->location());
3871         }
3872       if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
3873         {
3874           error_at(this->location(),
3875                    "missing condition in if statement");
3876           cond = Expression::make_error(this->location());
3877         }
3878       if (cond == NULL)
3879         cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
3880     }
3881
3882   this->gogo_->start_block(this->location());
3883   source_location end_loc = this->block();
3884   Block* then_block = this->gogo_->finish_block(end_loc);
3885
3886   // Check for the easy error of a newline before "else".
3887   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3888     {
3889       source_location semi_loc = this->location();
3890       if (this->advance_token()->is_keyword(KEYWORD_ELSE))
3891         error_at(this->location(),
3892                  "unexpected semicolon or newline before %<else%>");
3893       else
3894         this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3895                                                      semi_loc));
3896     }
3897
3898   Block* else_block = NULL;
3899   if (this->peek_token()->is_keyword(KEYWORD_ELSE))
3900     {
3901       this->gogo_->start_block(this->location());
3902       const Token* token = this->advance_token();
3903       if (token->is_keyword(KEYWORD_IF))
3904         this->if_stat();
3905       else if (token->is_op(OPERATOR_LCURLY))
3906         this->block();
3907       else
3908         {
3909           error_at(this->location(), "expected %<if%> or %<{%>");
3910           this->statement(NULL);
3911         }
3912       else_block = this->gogo_->finish_block(this->location());
3913     }
3914
3915   this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
3916                                                           else_block,
3917                                                           location));
3918
3919   this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3920                          location);
3921 }
3922
3923 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
3924 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
3925 //                      "{" { ExprCaseClause } "}" .
3926 // TypeSwitchStmt  = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
3927 //                      "{" { TypeCaseClause } "}" .
3928 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
3929
3930 void
3931 Parse::switch_stat(Label* label)
3932 {
3933   go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
3934   source_location location = this->location();
3935   this->advance_token();
3936
3937   this->gogo_->start_block(location);
3938
3939   bool saw_simple_stat = false;
3940   Expression* switch_val = NULL;
3941   bool saw_send_stmt;
3942   Type_switch type_switch;
3943   if (this->simple_stat_may_start_here())
3944     {
3945       switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
3946                                      &type_switch);
3947       saw_simple_stat = true;
3948     }
3949   if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3950     {
3951       // The SimpleStat is an expression statement.
3952       this->expression_stat(switch_val);
3953       switch_val = NULL;
3954     }
3955   if (switch_val == NULL && !type_switch.found)
3956     {
3957       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3958         this->advance_token();
3959       else if (saw_simple_stat)
3960         {
3961           if (saw_send_stmt)
3962             error_at(this->location(),
3963                      ("send statement used as value; "
3964                       "use select for non-blocking send"));
3965           else
3966             error_at(this->location(),
3967                      "expected %<;%> after statement in switch expression");
3968         }
3969       if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3970         {
3971           if (this->peek_token()->is_identifier())
3972             {
3973               const Token* token = this->peek_token();
3974               std::string identifier = token->identifier();
3975               bool is_exported = token->is_identifier_exported();
3976               source_location id_loc = token->location();
3977
3978               token = this->advance_token();
3979               bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
3980               this->unget_token(Token::make_identifier_token(identifier,
3981                                                              is_exported,
3982                                                              id_loc));
3983               if (is_coloneq)
3984                 {
3985                   // This must be a TypeSwitchGuard.
3986                   switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
3987                                                  &type_switch);
3988                   if (!type_switch.found)
3989                     {
3990                       if (switch_val == NULL
3991                           || !switch_val->is_error_expression())
3992                         {
3993                           error_at(id_loc, "expected type switch assignment");
3994                           switch_val = Expression::make_error(id_loc);
3995                         }
3996                     }
3997                 }
3998             }
3999           if (switch_val == NULL && !type_switch.found)
4000             {
4001               switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
4002                                             &type_switch.found);
4003               if (type_switch.found)
4004                 {
4005                   type_switch.name.clear();
4006                   type_switch.expr = switch_val;
4007                   type_switch.location = switch_val->location();
4008                 }
4009             }
4010         }
4011     }
4012
4013   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4014     {
4015       source_location token_loc = this->location();
4016       if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
4017           && this->advance_token()->is_op(OPERATOR_LCURLY))
4018         error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4019       else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
4020         {
4021           error_at(token_loc, "invalid variable name");
4022           this->advance_token();
4023           this->expression(PRECEDENCE_NORMAL, false, false,
4024                            &type_switch.found);
4025           if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4026             this->advance_token();
4027           if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4028             return;
4029           if (type_switch.found)
4030             type_switch.expr = Expression::make_error(location);
4031         }
4032       else
4033         {
4034           error_at(this->location(), "expected %<{%>");
4035           this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4036                                  location);
4037           return;
4038         }
4039     }
4040   this->advance_token();
4041
4042   Statement* statement;
4043   if (type_switch.found)
4044     statement = this->type_switch_body(label, type_switch, location);
4045   else
4046     statement = this->expr_switch_body(label, switch_val, location);
4047
4048   if (statement != NULL)
4049     this->gogo_->add_statement(statement);
4050
4051   this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4052                          location);
4053 }
4054
4055 // The body of an expression switch.
4056 //   "{" { ExprCaseClause } "}"
4057
4058 Statement*
4059 Parse::expr_switch_body(Label* label, Expression* switch_val,
4060                         source_location location)
4061 {
4062   Switch_statement* statement = Statement::make_switch_statement(switch_val,
4063                                                                  location);
4064
4065   this->push_break_statement(statement, label);
4066
4067   Case_clauses* case_clauses = new Case_clauses();
4068   bool saw_default = false;
4069   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4070     {
4071       if (this->peek_token()->is_eof())
4072         {
4073           if (!saw_errors())
4074             error_at(this->location(), "missing %<}%>");
4075           return NULL;
4076         }
4077       this->expr_case_clause(case_clauses, &saw_default);
4078     }
4079   this->advance_token();
4080
4081   statement->add_clauses(case_clauses);
4082
4083   this->pop_break_statement();
4084
4085   return statement;
4086 }
4087
4088 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4089 // FallthroughStat = "fallthrough" .
4090
4091 void
4092 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
4093 {
4094   source_location location = this->location();
4095
4096   bool is_default = false;
4097   Expression_list* vals = this->expr_switch_case(&is_default);
4098
4099   if (!this->peek_token()->is_op(OPERATOR_COLON))
4100     {
4101       if (!saw_errors())
4102         error_at(this->location(), "expected %<:%>");
4103       return;
4104     }
4105   else
4106     this->advance_token();
4107
4108   Block* statements = NULL;
4109   if (this->statement_list_may_start_here())
4110     {
4111       this->gogo_->start_block(this->location());
4112       this->statement_list();
4113       statements = this->gogo_->finish_block(this->location());
4114     }
4115
4116   bool is_fallthrough = false;
4117   if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4118     {
4119       is_fallthrough = true;
4120       if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4121         this->advance_token();
4122     }
4123
4124   if (is_default)
4125     {
4126       if (*saw_default)
4127         {
4128           error_at(location, "multiple defaults in switch");
4129           return;
4130         }
4131       *saw_default = true;
4132     }
4133
4134   if (is_default || vals != NULL)
4135     clauses->add(vals, is_default, statements, is_fallthrough, location);
4136 }
4137
4138 // ExprSwitchCase = "case" ExpressionList | "default" .
4139
4140 Expression_list*
4141 Parse::expr_switch_case(bool* is_default)
4142 {
4143   const Token* token = this->peek_token();
4144   if (token->is_keyword(KEYWORD_CASE))
4145     {
4146       this->advance_token();
4147       return this->expression_list(NULL, false);
4148     }
4149   else if (token->is_keyword(KEYWORD_DEFAULT))
4150     {
4151       this->advance_token();
4152       *is_default = true;
4153       return NULL;
4154     }
4155   else
4156     {
4157       if (!saw_errors())
4158         error_at(this->location(), "expected %<case%> or %<default%>");
4159       if (!token->is_op(OPERATOR_RCURLY))
4160         this->advance_token();
4161       return NULL;
4162     }
4163 }
4164
4165 // The body of a type switch.
4166 //   "{" { TypeCaseClause } "}" .
4167
4168 Statement*
4169 Parse::type_switch_body(Label* label, const Type_switch& type_switch,
4170                         source_location location)
4171 {
4172   Named_object* switch_no = NULL;
4173   if (!type_switch.name.empty())
4174     {
4175       Variable* switch_var = new Variable(NULL, type_switch.expr, false, false,
4176                                           false, type_switch.location);
4177       switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
4178     }
4179
4180   Type_switch_statement* statement =
4181     Statement::make_type_switch_statement(switch_no,
4182                                           (switch_no == NULL
4183                                            ? type_switch.expr
4184                                            : NULL),
4185                                           location);
4186
4187   this->push_break_statement(statement, label);
4188
4189   Type_case_clauses* case_clauses = new Type_case_clauses();
4190   bool saw_default = false;
4191   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4192     {
4193       if (this->peek_token()->is_eof())
4194         {
4195           error_at(this->location(), "missing %<}%>");
4196           return NULL;
4197         }
4198       this->type_case_clause(switch_no, case_clauses, &saw_default);
4199     }
4200   this->advance_token();
4201
4202   statement->add_clauses(case_clauses);
4203
4204   this->pop_break_statement();
4205
4206   return statement;
4207 }
4208
4209 // TypeCaseClause  = TypeSwitchCase ":" [ StatementList ] .
4210
4211 void
4212 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4213                         bool* saw_default)
4214 {
4215   source_location location = this->location();
4216
4217   std::vector<Type*> types;
4218   bool is_default = false;
4219   this->type_switch_case(&types, &is_default);
4220
4221   if (!this->peek_token()->is_op(OPERATOR_COLON))
4222     error_at(this->location(), "expected %<:%>");
4223   else
4224     this->advance_token();
4225
4226   Block* statements = NULL;
4227   if (this->statement_list_may_start_here())
4228     {
4229       this->gogo_->start_block(this->location());
4230       if (switch_no != NULL && types.size() == 1)
4231         {
4232           Type* type = types.front();
4233           Expression* init = Expression::make_var_reference(switch_no,
4234                                                             location);
4235           init = Expression::make_type_guard(init, type, location);
4236           Variable* v = new Variable(type, init, false, false, false,
4237                                      location);
4238           v->set_is_type_switch_var();
4239           this->gogo_->add_variable(switch_no->name(), v);
4240         }
4241       this->statement_list();
4242       statements = this->gogo_->finish_block(this->location());
4243     }
4244
4245   if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4246     {
4247       error_at(this->location(),
4248                "fallthrough is not permitted in a type switch");
4249       if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4250         this->advance_token();
4251     }
4252
4253   if (is_default)
4254     {
4255       go_assert(types.empty());
4256       if (*saw_default)
4257         {
4258           error_at(location, "multiple defaults in type switch");
4259           return;
4260         }
4261       *saw_default = true;
4262       clauses->add(NULL, false, true, statements, location);
4263     }
4264   else if (!types.empty())
4265     {
4266       for (std::vector<Type*>::const_iterator p = types.begin();
4267            p + 1 != types.end();
4268            ++p)
4269         clauses->add(*p, true, false, NULL, location);
4270       clauses->add(types.back(), false, false, statements, location);
4271     }
4272   else
4273     clauses->add(Type::make_error_type(), false, false, statements, location);
4274 }
4275
4276 // TypeSwitchCase  = "case" type | "default"
4277
4278 // We accept a comma separated list of types.
4279
4280 void
4281 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4282 {
4283   const Token* token = this->peek_token();
4284   if (token->is_keyword(KEYWORD_CASE))
4285     {
4286       this->advance_token();
4287       while (true)
4288         {
4289           Type* t = this->type();
4290           if (!t->is_error_type())
4291             types->push_back(t);
4292           if (!this->peek_token()->is_op(OPERATOR_COMMA))
4293             break;
4294           this->advance_token();
4295         }
4296     }
4297   else if (token->is_keyword(KEYWORD_DEFAULT))
4298     {
4299       this->advance_token();
4300       *is_default = true;
4301     }
4302   else
4303     {
4304       error_at(this->location(), "expected %<case%> or %<default%>");
4305       if (!token->is_op(OPERATOR_RCURLY))
4306         this->advance_token();
4307     }
4308 }
4309
4310 // SelectStat = "select" "{" { CommClause } "}" .
4311
4312 void
4313 Parse::select_stat(Label* label)
4314 {
4315   go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4316   source_location location = this->location();
4317   const Token* token = this->advance_token();
4318
4319   if (!token->is_op(OPERATOR_LCURLY))
4320     {
4321       source_location token_loc = token->location();
4322       if (token->is_op(OPERATOR_SEMICOLON)
4323           && this->advance_token()->is_op(OPERATOR_LCURLY))
4324         error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4325       else
4326         {
4327           error_at(this->location(), "expected %<{%>");
4328           return;
4329         }
4330     }
4331   this->advance_token();
4332
4333   Select_statement* statement = Statement::make_select_statement(location);
4334
4335   this->push_break_statement(statement, label);
4336
4337   Select_clauses* select_clauses = new Select_clauses();
4338   bool saw_default = false;
4339   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4340     {
4341       if (this->peek_token()->is_eof())
4342         {
4343           error_at(this->location(), "expected %<}%>");
4344           return;
4345         }
4346       this->comm_clause(select_clauses, &saw_default);
4347     }
4348
4349   this->advance_token();
4350
4351   statement->add_clauses(select_clauses);
4352
4353   this->pop_break_statement();
4354
4355   this->gogo_->add_statement(statement);
4356 }
4357
4358 // CommClause = CommCase ":" { Statement ";" } .
4359
4360 void
4361 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4362 {
4363   source_location location = this->location();
4364   bool is_send = false;
4365   Expression* channel = NULL;
4366   Expression* val = NULL;
4367   Expression* closed = NULL;
4368   std::string varname;
4369   std::string closedname;
4370   bool is_default = false;
4371   bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
4372                                   &varname, &closedname, &is_default);
4373
4374   if (!is_send
4375       && varname.empty()
4376       && closedname.empty()
4377       && val != NULL
4378       && val->index_expression() != NULL)
4379     val->index_expression()->set_is_lvalue();
4380
4381   if (this->peek_token()->is_op(OPERATOR_COLON))
4382     this->advance_token();
4383   else
4384     error_at(this->location(), "expected colon");
4385
4386   this->gogo_->start_block(this->location());
4387
4388   Named_object* var = NULL;
4389   if (!varname.empty())
4390     {
4391       // FIXME: LOCATION is slightly wrong here.
4392       Variable* v = new Variable(NULL, channel, false, false, false,
4393                                  location);
4394       v->set_type_from_chan_element();
4395       var = this->gogo_->add_variable(varname, v);
4396     }
4397
4398   Named_object* closedvar = NULL;
4399   if (!closedname.empty())
4400     {
4401       // FIXME: LOCATION is slightly wrong here.
4402       Variable* v = new Variable(Type::lookup_bool_type(), NULL,
4403                                  false, false, false, location);
4404       closedvar = this->gogo_->add_variable(closedname, v);
4405     }
4406
4407   this->statement_list();
4408
4409   Block* statements = this->gogo_->finish_block(this->location());
4410
4411   if (is_default)
4412     {
4413       if (*saw_default)
4414         {
4415           error_at(location, "multiple defaults in select");
4416           return;
4417         }
4418       *saw_default = true;
4419     }
4420
4421   if (got_case)
4422     clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
4423                  statements, location);
4424   else if (statements != NULL)
4425     {
4426       // Add the statements to make sure that any names they define
4427       // are traversed.
4428       this->gogo_->add_block(statements, location);
4429     }
4430 }
4431
4432 // CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
4433
4434 bool
4435 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4436                  Expression** closed, std::string* varname,
4437                  std::string* closedname, bool* is_default)
4438 {
4439   const Token* token = this->peek_token();
4440   if (token->is_keyword(KEYWORD_DEFAULT))
4441     {
4442       this->advance_token();
4443       *is_default = true;
4444     }
4445   else if (token->is_keyword(KEYWORD_CASE))
4446     {
4447       this->advance_token();
4448       if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
4449                                    closedname))
4450         return false;
4451     }
4452   else
4453     {
4454       error_at(this->location(), "expected %<case%> or %<default%>");
4455       if (!token->is_op(OPERATOR_RCURLY))
4456         this->advance_token();
4457       return false;
4458     }
4459
4460   return true;
4461 }
4462
4463 // RecvStmt   = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4464 // RecvExpr   = Expression .
4465
4466 bool
4467 Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
4468                          Expression** closed, std::string* varname,
4469                          std::string* closedname)
4470 {
4471   const Token* token = this->peek_token();
4472   bool saw_comma = false;
4473   bool closed_is_id = false;
4474   if (token->is_identifier())
4475     {
4476       Gogo* gogo = this->gogo_;
4477       std::string recv_var = token->identifier();
4478       bool is_rv_exported = token->is_identifier_exported();
4479       source_location recv_var_loc = token->location();
4480       token = this->advance_token();
4481       if (token->is_op(OPERATOR_COLONEQ))
4482         {
4483           // case rv := <-c:
4484           if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4485             {
4486               error_at(this->location(), "expected %<<-%>");
4487               return false;
4488             }
4489           if (recv_var == "_")
4490             {
4491               error_at(recv_var_loc,
4492                        "no new variables on left side of %<:=%>");
4493               recv_var = "blank";
4494             }
4495           *is_send = false;
4496           *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
4497           this->advance_token();
4498           *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4499           return true;
4500         }
4501       else if (token->is_op(OPERATOR_COMMA))
4502         {
4503           token = this->advance_token();
4504           if (token->is_identifier())
4505             {
4506               std::string recv_closed = token->identifier();
4507               bool is_rc_exported = token->is_identifier_exported();
4508               source_location recv_closed_loc = token->location();
4509               closed_is_id = true;
4510
4511               token = this->advance_token();
4512               if (token->is_op(OPERATOR_COLONEQ))
4513                 {
4514                   // case rv, rc := <-c:
4515                   if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4516                     {
4517                       error_at(this->location(), "expected %<<-%>");
4518                       return false;
4519                     }
4520                   if (recv_var == "_" && recv_closed == "_")
4521                     {
4522                       error_at(recv_var_loc,
4523                                "no new variables on left side of %<:=%>");
4524                       recv_var = "blank";
4525                     }
4526                   *is_send = false;
4527                   if (recv_var != "_")
4528                     *varname = gogo->pack_hidden_name(recv_var,
4529                                                       is_rv_exported);
4530                   if (recv_closed != "_")
4531                     *closedname = gogo->pack_hidden_name(recv_closed,
4532                                                          is_rc_exported);
4533                   this->advance_token();
4534                   *channel = this->expression(PRECEDENCE_NORMAL, false, true,
4535                                               NULL);
4536                   return true;
4537                 }
4538
4539               this->unget_token(Token::make_identifier_token(recv_closed,
4540                                                              is_rc_exported,
4541                                                              recv_closed_loc));
4542             }
4543
4544           *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
4545                                                                is_rv_exported),
4546                                         recv_var_loc);
4547           saw_comma = true;
4548         }
4549       else
4550         this->unget_token(Token::make_identifier_token(recv_var,
4551                                                        is_rv_exported,
4552                                                        recv_var_loc));
4553     }
4554
4555   // If SAW_COMMA is false, then we are looking at the start of the
4556   // send or receive expression.  If SAW_COMMA is true, then *VAL is
4557   // set and we just read a comma.
4558
4559   Expression* e;
4560   if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
4561     e = this->expression(PRECEDENCE_NORMAL, true, true, NULL);
4562   else
4563     {
4564       // case <-c:
4565       *is_send = false;
4566       this->advance_token();
4567       *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4568
4569       // The next token should be ':'.  If it is '<-', then we have
4570       // case <-c <- v:
4571       // which is to say, send on a channel received from a channel.
4572       if (!this->peek_token()->is_op(OPERATOR_CHANOP))
4573         return true;
4574
4575       e = Expression::make_receive(*channel, (*channel)->location());
4576     }
4577
4578   if (this->peek_token()->is_op(OPERATOR_EQ))
4579     {
4580       if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4581         {
4582           error_at(this->location(), "missing %<<-%>");
4583           return false;
4584         }
4585       *is_send = false;
4586       this->advance_token();
4587       *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4588       if (saw_comma)
4589         {
4590           // case v, e = <-c:
4591           // *VAL is already set.
4592           if (!e->is_sink_expression())
4593             *closed = e;
4594         }
4595       else
4596         {
4597           // case v = <-c:
4598           if (!e->is_sink_expression())
4599             *val = e;
4600         }
4601       return true;
4602     }
4603
4604   if (saw_comma)
4605     {
4606       if (closed_is_id)
4607         error_at(this->location(), "expected %<=%> or %<:=%>");
4608       else
4609         error_at(this->location(), "expected %<=%>");
4610       return false;
4611     }
4612
4613   if (this->peek_token()->is_op(OPERATOR_CHANOP))
4614     {
4615       // case c <- v:
4616       *is_send = true;
4617       *channel = this->verify_not_sink(e);
4618       this->advance_token();
4619       *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4620       return true;
4621     }
4622
4623   error_at(this->location(), "expected %<<-%> or %<=%>");
4624   return false;
4625 }
4626
4627 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
4628 // Condition = Expression .
4629
4630 void
4631 Parse::for_stat(Label* label)
4632 {
4633   go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
4634   source_location location = this->location();
4635   const Token* token = this->advance_token();
4636
4637   // Open a block to hold any variables defined in the init statement
4638   // of the for statement.
4639   this->gogo_->start_block(location);
4640
4641   Block* init = NULL;
4642   Expression* cond = NULL;
4643   Block* post = NULL;
4644   Range_clause range_clause;
4645
4646   if (!token->is_op(OPERATOR_LCURLY))
4647     {
4648       if (token->is_keyword(KEYWORD_VAR))
4649         {
4650           error_at(this->location(),
4651                    "var declaration not allowed in for initializer");
4652           this->var_decl();
4653         }
4654
4655       if (token->is_op(OPERATOR_SEMICOLON))
4656         this->for_clause(&cond, &post);
4657       else
4658         {
4659           // We might be looking at a Condition, an InitStat, or a
4660           // RangeClause.
4661           bool saw_send_stmt;
4662           cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
4663           if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4664             {
4665               if (cond == NULL && !range_clause.found)
4666                 {
4667                   if (saw_send_stmt)
4668                     error_at(this->location(),
4669                              ("send statement used as value; "
4670                               "use select for non-blocking send"));
4671                   else
4672                     error_at(this->location(), "parse error in for statement");
4673                 }
4674             }
4675           else
4676             {
4677               if (range_clause.found)
4678                 error_at(this->location(), "parse error after range clause");
4679
4680               if (cond != NULL)
4681                 {
4682                   // COND is actually an expression statement for
4683                   // InitStat at the start of a ForClause.
4684                   this->expression_stat(cond);
4685                   cond = NULL;
4686                 }
4687
4688               this->for_clause(&cond, &post);
4689             }
4690         }
4691     }
4692
4693   // Build the For_statement and note that it is the current target
4694   // for break and continue statements.
4695
4696   For_statement* sfor;
4697   For_range_statement* srange;
4698   Statement* s;
4699   if (!range_clause.found)
4700     {
4701       sfor = Statement::make_for_statement(init, cond, post, location);
4702       s = sfor;
4703       srange = NULL;
4704     }
4705   else
4706     {
4707       srange = Statement::make_for_range_statement(range_clause.index,
4708                                                    range_clause.value,
4709                                                    range_clause.range,
4710                                                    location);
4711       s = srange;
4712       sfor = NULL;
4713     }
4714
4715   this->push_break_statement(s, label);
4716   this->push_continue_statement(s, label);
4717
4718   // Gather the block of statements in the loop and add them to the
4719   // For_statement.
4720
4721   this->gogo_->start_block(this->location());
4722   source_location end_loc = this->block();
4723   Block* statements = this->gogo_->finish_block(end_loc);
4724
4725   if (sfor != NULL)
4726     sfor->add_statements(statements);
4727   else
4728     srange->add_statements(statements);
4729
4730   // This is no longer the break/continue target.
4731   this->pop_break_statement();
4732   this->pop_continue_statement();
4733
4734   // Add the For_statement to the list of statements, and close out
4735   // the block we started to hold any variables defined in the for
4736   // statement.
4737
4738   this->gogo_->add_statement(s);
4739
4740   this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4741                          location);
4742 }
4743
4744 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
4745 // InitStat = SimpleStat .
4746 // PostStat = SimpleStat .
4747
4748 // We have already read InitStat at this point.
4749
4750 void
4751 Parse::for_clause(Expression** cond, Block** post)
4752 {
4753   go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
4754   this->advance_token();
4755   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4756     *cond = NULL;
4757   else if (this->peek_token()->is_op(OPERATOR_LCURLY))
4758     {
4759       error_at(this->location(),
4760                "unexpected semicolon or newline before %<{%>");
4761       *cond = NULL;
4762       *post = NULL;
4763       return;
4764     }
4765   else
4766     *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4767   if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4768     error_at(this->location(), "expected semicolon");
4769   else
4770     this->advance_token();
4771
4772   if (this->peek_token()->is_op(OPERATOR_LCURLY))
4773     *post = NULL;
4774   else
4775     {
4776       this->gogo_->start_block(this->location());
4777       this->simple_stat(false, NULL, NULL, NULL);
4778       *post = this->gogo_->finish_block(this->location());
4779     }
4780 }
4781
4782 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
4783
4784 // This is the := version.  It is called with a list of identifiers.
4785
4786 void
4787 Parse::range_clause_decl(const Typed_identifier_list* til,
4788                          Range_clause* p_range_clause)
4789 {
4790   go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4791   source_location location = this->location();
4792
4793   p_range_clause->found = true;
4794
4795   go_assert(til->size() >= 1);
4796   if (til->size() > 2)
4797     error_at(this->location(), "too many variables for range clause");
4798
4799   this->advance_token();
4800   Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
4801   p_range_clause->range = expr;
4802
4803   bool any_new = false;
4804
4805   const Typed_identifier* pti = &til->front();
4806   Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new);
4807   if (any_new && no->is_variable())
4808     no->var_value()->set_type_from_range_index();
4809   p_range_clause->index = Expression::make_var_reference(no, location);
4810
4811   if (til->size() == 1)
4812     p_range_clause->value = NULL;
4813   else
4814     {
4815       pti = &til->back();
4816       bool is_new = false;
4817       no = this->init_var(*pti, NULL, expr, true, true, &is_new);
4818       if (is_new && no->is_variable())
4819         no->var_value()->set_type_from_range_value();
4820       if (is_new)
4821         any_new = true;
4822       p_range_clause->value = Expression::make_var_reference(no, location);
4823     }
4824
4825   if (!any_new)
4826     error_at(location, "variables redeclared but no variable is new");
4827 }
4828
4829 // The = version of RangeClause.  This is called with a list of
4830 // expressions.
4831
4832 void
4833 Parse::range_clause_expr(const Expression_list* vals,
4834                          Range_clause* p_range_clause)
4835 {
4836   go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4837
4838   p_range_clause->found = true;
4839
4840   go_assert(vals->size() >= 1);
4841   if (vals->size() > 2)
4842     error_at(this->location(), "too many variables for range clause");
4843
4844   this->advance_token();
4845   p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
4846                                            NULL);
4847
4848   p_range_clause->index = vals->front();
4849   if (vals->size() == 1)
4850     p_range_clause->value = NULL;
4851   else
4852     p_range_clause->value = vals->back();
4853 }
4854
4855 // Push a statement on the break stack.
4856
4857 void
4858 Parse::push_break_statement(Statement* enclosing, Label* label)
4859 {
4860   if (this->break_stack_ == NULL)
4861     this->break_stack_ = new Bc_stack();
4862   this->break_stack_->push_back(std::make_pair(enclosing, label));
4863 }
4864
4865 // Push a statement on the continue stack.
4866
4867 void
4868 Parse::push_continue_statement(Statement* enclosing, Label* label)
4869 {
4870   if (this->continue_stack_ == NULL)
4871     this->continue_stack_ = new Bc_stack();
4872   this->continue_stack_->push_back(std::make_pair(enclosing, label));
4873 }
4874
4875 // Pop the break stack.
4876
4877 void
4878 Parse::pop_break_statement()
4879 {
4880   this->break_stack_->pop_back();
4881 }
4882
4883 // Pop the continue stack.
4884
4885 void
4886 Parse::pop_continue_statement()
4887 {
4888   this->continue_stack_->pop_back();
4889 }
4890
4891 // Find a break or continue statement given a label name.
4892
4893 Statement*
4894 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
4895 {
4896   if (bc_stack == NULL)
4897     return NULL;
4898   for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
4899        p != bc_stack->rend();
4900        ++p)
4901     {
4902       if (p->second != NULL && p->second->name() == label)
4903         {
4904           p->second->set_is_used();
4905           return p->first;
4906         }
4907     }
4908   return NULL;
4909 }
4910
4911 // BreakStat = "break" [ identifier ] .
4912
4913 void
4914 Parse::break_stat()
4915 {
4916   go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
4917   source_location location = this->location();
4918
4919   const Token* token = this->advance_token();
4920   Statement* enclosing;
4921   if (!token->is_identifier())
4922     {
4923       if (this->break_stack_ == NULL || this->break_stack_->empty())
4924         {
4925           error_at(this->location(),
4926                    "break statement not within for or switch or select");
4927           return;
4928         }
4929       enclosing = this->break_stack_->back().first;
4930     }
4931   else
4932     {
4933       enclosing = this->find_bc_statement(this->break_stack_,
4934                                           token->identifier());
4935       if (enclosing == NULL)
4936         {
4937           // If there is a label with this name, mark it as used to
4938           // avoid a useless error about an unused label.
4939           this->gogo_->add_label_reference(token->identifier(), 0, false);
4940
4941           error_at(token->location(), "invalid break label %qs",
4942                    Gogo::message_name(token->identifier()).c_str());
4943           this->advance_token();
4944           return;
4945         }
4946       this->advance_token();
4947     }
4948
4949   Unnamed_label* label;
4950   if (enclosing->classification() == Statement::STATEMENT_FOR)
4951     label = enclosing->for_statement()->break_label();
4952   else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
4953     label = enclosing->for_range_statement()->break_label();
4954   else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
4955     label = enclosing->switch_statement()->break_label();
4956   else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
4957     label = enclosing->type_switch_statement()->break_label();
4958   else if (enclosing->classification() == Statement::STATEMENT_SELECT)
4959     label = enclosing->select_statement()->break_label();
4960   else
4961     go_unreachable();
4962
4963   this->gogo_->add_statement(Statement::make_break_statement(label,
4964                                                              location));
4965 }
4966
4967 // ContinueStat = "continue" [ identifier ] .
4968
4969 void
4970 Parse::continue_stat()
4971 {
4972   go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
4973   source_location location = this->location();
4974
4975   const Token* token = this->advance_token();
4976   Statement* enclosing;
4977   if (!token->is_identifier())
4978     {
4979       if (this->continue_stack_ == NULL || this->continue_stack_->empty())
4980         {
4981           error_at(this->location(), "continue statement not within for");
4982           return;
4983         }
4984       enclosing = this->continue_stack_->back().first;
4985     }
4986   else
4987     {
4988       enclosing = this->find_bc_statement(this->continue_stack_,
4989                                           token->identifier());
4990       if (enclosing == NULL)
4991         {
4992           // If there is a label with this name, mark it as used to
4993           // avoid a useless error about an unused label.
4994           this->gogo_->add_label_reference(token->identifier(), 0, false);
4995
4996           error_at(token->location(), "invalid continue label %qs",
4997                    Gogo::message_name(token->identifier()).c_str());
4998           this->advance_token();
4999           return;
5000         }
5001       this->advance_token();
5002     }
5003
5004   Unnamed_label* label;
5005   if (enclosing->classification() == Statement::STATEMENT_FOR)
5006     label = enclosing->for_statement()->continue_label();
5007   else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5008     label = enclosing->for_range_statement()->continue_label();
5009   else
5010     go_unreachable();
5011
5012   this->gogo_->add_statement(Statement::make_continue_statement(label,
5013                                                                 location));
5014 }
5015
5016 // GotoStat = "goto" identifier .
5017
5018 void
5019 Parse::goto_stat()
5020 {
5021   go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
5022   source_location location = this->location();
5023   const Token* token = this->advance_token();
5024   if (!token->is_identifier())
5025     error_at(this->location(), "expected label for goto");
5026   else
5027     {
5028       Label* label = this->gogo_->add_label_reference(token->identifier(),
5029                                                       location, true);
5030       Statement* s = Statement::make_goto_statement(label, location);
5031       this->gogo_->add_statement(s);
5032       this->advance_token();
5033     }
5034 }
5035
5036 // PackageClause = "package" PackageName .
5037
5038 void
5039 Parse::package_clause()
5040 {
5041   const Token* token = this->peek_token();
5042   source_location location = token->location();
5043   std::string name;
5044   if (!token->is_keyword(KEYWORD_PACKAGE))
5045     {
5046       error_at(this->location(), "program must start with package clause");
5047       name = "ERROR";
5048     }
5049   else
5050     {
5051       token = this->advance_token();
5052       if (token->is_identifier())
5053         {
5054           name = token->identifier();
5055           if (name == "_")
5056             {
5057               error_at(this->location(), "invalid package name _");
5058               name = "blank";
5059             }
5060           this->advance_token();
5061         }
5062       else
5063         {
5064           error_at(this->location(), "package name must be an identifier");
5065           name = "ERROR";
5066         }
5067     }
5068   this->gogo_->set_package_name(name, location);
5069 }
5070
5071 // ImportDecl = "import" Decl<ImportSpec> .
5072
5073 void
5074 Parse::import_decl()
5075 {
5076   go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
5077   this->advance_token();
5078   this->decl(&Parse::import_spec, NULL);
5079 }
5080
5081 // ImportSpec = [ "." | PackageName ] PackageFileName .
5082
5083 void
5084 Parse::import_spec(void*)
5085 {
5086   const Token* token = this->peek_token();
5087   source_location location = token->location();
5088
5089   std::string local_name;
5090   bool is_local_name_exported = false;
5091   if (token->is_op(OPERATOR_DOT))
5092     {
5093       local_name = ".";
5094       token = this->advance_token();
5095     }
5096   else if (token->is_identifier())
5097     {
5098       local_name = token->identifier();
5099       is_local_name_exported = token->is_identifier_exported();
5100       token = this->advance_token();
5101     }
5102
5103   if (!token->is_string())
5104     {
5105       error_at(this->location(), "missing import package name");
5106       return;
5107     }
5108
5109   this->gogo_->import_package(token->string_value(), local_name,
5110                               is_local_name_exported, location);
5111
5112   this->advance_token();
5113 }
5114
5115 // SourceFile       = PackageClause ";" { ImportDecl ";" }
5116 //                      { TopLevelDecl ";" } .
5117
5118 void
5119 Parse::program()
5120 {
5121   this->package_clause();
5122
5123   const Token* token = this->peek_token();
5124   if (token->is_op(OPERATOR_SEMICOLON))
5125     token = this->advance_token();
5126   else
5127     error_at(this->location(),
5128              "expected %<;%> or newline after package clause");
5129
5130   while (token->is_keyword(KEYWORD_IMPORT))
5131     {
5132       this->import_decl();
5133       token = this->peek_token();
5134       if (token->is_op(OPERATOR_SEMICOLON))
5135         token = this->advance_token();
5136       else
5137         error_at(this->location(),
5138                  "expected %<;%> or newline after import declaration");
5139     }
5140
5141   while (!token->is_eof())
5142     {
5143       if (this->declaration_may_start_here())
5144         this->declaration();
5145       else
5146         {
5147           error_at(this->location(), "expected declaration");
5148           do
5149             this->advance_token();
5150           while (!this->peek_token()->is_eof()
5151                  && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
5152                  && !this->peek_token()->is_op(OPERATOR_RCURLY));
5153           if (!this->peek_token()->is_eof()
5154               && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
5155             this->advance_token();
5156         }
5157       token = this->peek_token();
5158       if (token->is_op(OPERATOR_SEMICOLON))
5159         token = this->advance_token();
5160       else if (!token->is_eof() || !saw_errors())
5161         {
5162           if (token->is_op(OPERATOR_CHANOP))
5163             error_at(this->location(),
5164                      ("send statement used as value; "
5165                       "use select for non-blocking send"));
5166           else
5167             error_at(this->location(),
5168                      "expected %<;%> or newline after top level declaration");
5169           this->skip_past_error(OPERATOR_INVALID);
5170         }
5171     }
5172 }
5173
5174 // Reset the current iota value.
5175
5176 void
5177 Parse::reset_iota()
5178 {
5179   this->iota_ = 0;
5180 }
5181
5182 // Return the current iota value.
5183
5184 int
5185 Parse::iota_value()
5186 {
5187   return this->iota_;
5188 }
5189
5190 // Increment the current iota value.
5191
5192 void
5193 Parse::increment_iota()
5194 {
5195   ++this->iota_;
5196 }
5197
5198 // Skip forward to a semicolon or OP.  OP will normally be
5199 // OPERATOR_RPAREN or OPERATOR_RCURLY.  If we find a semicolon, move
5200 // past it and return.  If we find OP, it will be the next token to
5201 // read.  Return true if we are OK, false if we found EOF.
5202
5203 bool
5204 Parse::skip_past_error(Operator op)
5205 {
5206   const Token* token = this->peek_token();
5207   while (!token->is_op(op))
5208     {
5209       if (token->is_eof())
5210         return false;
5211       if (token->is_op(OPERATOR_SEMICOLON))
5212         {
5213           this->advance_token();
5214           return true;
5215         }
5216       token = this->advance_token();
5217     }
5218   return true;
5219 }
5220
5221 // Check that an expression is not a sink.
5222
5223 Expression*
5224 Parse::verify_not_sink(Expression* expr)
5225 {
5226   if (expr->is_sink_expression())
5227     {
5228       error_at(expr->location(), "cannot use _ as value");
5229       expr = Expression::make_error(expr->location());
5230     }
5231   return expr;
5232 }