OSDN Git Service

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