OSDN Git Service

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