OSDN Git Service

6567a42b36f58ba6a5399b2a329c2d3c0f4a4d7b
[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     is_erroneous_function_(false),
49     gogo_(gogo),
50     break_stack_(NULL),
51     continue_stack_(NULL),
52     iota_(0),
53     enclosing_vars_(),
54     type_switch_vars_()
55 {
56 }
57
58 // Return the current token.
59
60 const Token*
61 Parse::peek_token()
62 {
63   if (this->unget_token_valid_)
64     return &this->unget_token_;
65   if (this->token_.is_invalid())
66     this->token_ = this->lex_->next_token();
67   return &this->token_;
68 }
69
70 // Advance to the next token and return it.
71
72 const Token*
73 Parse::advance_token()
74 {
75   if (this->unget_token_valid_)
76     {
77       this->unget_token_valid_ = false;
78       if (!this->token_.is_invalid())
79         return &this->token_;
80     }
81   this->token_ = this->lex_->next_token();
82   return &this->token_;
83 }
84
85 // Push a token back on the input stream.
86
87 void
88 Parse::unget_token(const Token& token)
89 {
90   go_assert(!this->unget_token_valid_);
91   this->unget_token_ = token;
92   this->unget_token_valid_ = true;
93 }
94
95 // The location of the current token.
96
97 Location
98 Parse::location()
99 {
100   return this->peek_token()->location();
101 }
102
103 // IdentifierList = identifier { "," identifier } .
104
105 void
106 Parse::identifier_list(Typed_identifier_list* til)
107 {
108   const Token* token = this->peek_token();
109   while (true)
110     {
111       if (!token->is_identifier())
112         {
113           error_at(this->location(), "expected identifier");
114           return;
115         }
116       std::string name =
117         this->gogo_->pack_hidden_name(token->identifier(),
118                                       token->is_identifier_exported());
119       til->push_back(Typed_identifier(name, NULL, token->location()));
120       token = this->advance_token();
121       if (!token->is_op(OPERATOR_COMMA))
122         return;
123       token = this->advance_token();
124     }
125 }
126
127 // ExpressionList = Expression { "," Expression } .
128
129 // If MAY_BE_SINK is true, the expressions in the list may be "_".
130
131 Expression_list*
132 Parse::expression_list(Expression* first, bool may_be_sink)
133 {
134   Expression_list* ret = new Expression_list();
135   if (first != NULL)
136     ret->push_back(first);
137   while (true)
138     {
139       ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink, true,
140                                       NULL));
141
142       const Token* token = this->peek_token();
143       if (!token->is_op(OPERATOR_COMMA))
144         return ret;
145
146       // Most expression lists permit a trailing comma.
147       Location location = token->location();
148       this->advance_token();
149       if (!this->expression_may_start_here())
150         {
151           this->unget_token(Token::make_operator_token(OPERATOR_COMMA,
152                                                        location));
153           return ret;
154         }
155     }
156 }
157
158 // QualifiedIdent = [ PackageName "." ] identifier .
159 // PackageName = identifier .
160
161 // This sets *PNAME to the identifier and sets *PPACKAGE to the
162 // package or NULL if there isn't one.  This returns true on success,
163 // false on failure in which case it will have emitted an error
164 // message.
165
166 bool
167 Parse::qualified_ident(std::string* pname, Named_object** ppackage)
168 {
169   const Token* token = this->peek_token();
170   if (!token->is_identifier())
171     {
172       error_at(this->location(), "expected identifier");
173       return false;
174     }
175
176   std::string name = token->identifier();
177   bool is_exported = token->is_identifier_exported();
178   name = this->gogo_->pack_hidden_name(name, is_exported);
179
180   token = this->advance_token();
181   if (!token->is_op(OPERATOR_DOT))
182     {
183       *pname = name;
184       *ppackage = NULL;
185       return true;
186     }
187
188   Named_object* package = this->gogo_->lookup(name, NULL);
189   if (package == NULL || !package->is_package())
190     {
191       error_at(this->location(), "expected package");
192       // We expect . IDENTIFIER; skip both.
193       if (this->advance_token()->is_identifier())
194         this->advance_token();
195       return false;
196     }
197
198   package->package_value()->set_used();
199
200   token = this->advance_token();
201   if (!token->is_identifier())
202     {
203       error_at(this->location(), "expected identifier");
204       return false;
205     }
206
207   name = token->identifier();
208
209   if (name == "_")
210     {
211       error_at(this->location(), "invalid use of %<_%>");
212       name = "blank";
213     }
214
215   if (package->name() == this->gogo_->package_name())
216     name = this->gogo_->pack_hidden_name(name,
217                                          token->is_identifier_exported());
218
219   *pname = name;
220   *ppackage = package;
221
222   this->advance_token();
223
224   return true;
225 }
226
227 // Type = TypeName | TypeLit | "(" Type ")" .
228 // TypeLit =
229 //      ArrayType | StructType | PointerType | FunctionType | InterfaceType |
230 //      SliceType | MapType | ChannelType .
231
232 Type*
233 Parse::type()
234 {
235   const Token* token = this->peek_token();
236   if (token->is_identifier())
237     return this->type_name(true);
238   else if (token->is_op(OPERATOR_LSQUARE))
239     return this->array_type(false);
240   else if (token->is_keyword(KEYWORD_CHAN)
241            || token->is_op(OPERATOR_CHANOP))
242     return this->channel_type();
243   else if (token->is_keyword(KEYWORD_INTERFACE))
244     return this->interface_type();
245   else if (token->is_keyword(KEYWORD_FUNC))
246     {
247       Location location = token->location();
248       this->advance_token();
249       Type* type = this->signature(NULL, location);
250       if (type == NULL)
251         return Type::make_error_type();
252       return type;
253     }
254   else if (token->is_keyword(KEYWORD_MAP))
255     return this->map_type();
256   else if (token->is_keyword(KEYWORD_STRUCT))
257     return this->struct_type();
258   else if (token->is_op(OPERATOR_MULT))
259     return this->pointer_type();
260   else if (token->is_op(OPERATOR_LPAREN))
261     {
262       this->advance_token();
263       Type* ret = this->type();
264       if (this->peek_token()->is_op(OPERATOR_RPAREN))
265         this->advance_token();
266       else
267         {
268           if (!ret->is_error_type())
269             error_at(this->location(), "expected %<)%>");
270         }
271       return ret;
272     }
273   else
274     {
275       error_at(token->location(), "expected type");
276       return Type::make_error_type();
277     }
278 }
279
280 bool
281 Parse::type_may_start_here()
282 {
283   const Token* token = this->peek_token();
284   return (token->is_identifier()
285           || token->is_op(OPERATOR_LSQUARE)
286           || token->is_op(OPERATOR_CHANOP)
287           || token->is_keyword(KEYWORD_CHAN)
288           || token->is_keyword(KEYWORD_INTERFACE)
289           || token->is_keyword(KEYWORD_FUNC)
290           || token->is_keyword(KEYWORD_MAP)
291           || token->is_keyword(KEYWORD_STRUCT)
292           || token->is_op(OPERATOR_MULT)
293           || token->is_op(OPERATOR_LPAREN));
294 }
295
296 // TypeName = QualifiedIdent .
297
298 // If MAY_BE_NIL is true, then an identifier with the value of the
299 // predefined constant nil is accepted, returning the nil type.
300
301 Type*
302 Parse::type_name(bool issue_error)
303 {
304   Location location = this->location();
305
306   std::string name;
307   Named_object* package;
308   if (!this->qualified_ident(&name, &package))
309     return Type::make_error_type();
310
311   Named_object* named_object;
312   if (package == NULL)
313     named_object = this->gogo_->lookup(name, NULL);
314   else
315     {
316       named_object = package->package_value()->lookup(name);
317       if (named_object == NULL
318           && issue_error
319           && package->name() != this->gogo_->package_name())
320         {
321           // Check whether the name is there but hidden.
322           std::string s = ('.' + package->package_value()->unique_prefix()
323                            + '.' + package->package_value()->name()
324                            + '.' + name);
325           named_object = package->package_value()->lookup(s);
326           if (named_object != NULL)
327             {
328               const std::string& packname(package->package_value()->name());
329               error_at(location, "invalid reference to hidden type %<%s.%s%>",
330                        Gogo::message_name(packname).c_str(),
331                        Gogo::message_name(name).c_str());
332               issue_error = false;
333             }
334         }
335     }
336
337   bool ok = true;
338   if (named_object == NULL)
339     {
340       if (package == NULL)
341         named_object = this->gogo_->add_unknown_name(name, location);
342       else
343         {
344           const std::string& packname(package->package_value()->name());
345           error_at(location, "reference to undefined identifier %<%s.%s%>",
346                    Gogo::message_name(packname).c_str(),
347                    Gogo::message_name(name).c_str());
348           issue_error = false;
349           ok = false;
350         }
351     }
352   else if (named_object->is_type())
353     {
354       if (!named_object->type_value()->is_visible())
355         ok = false;
356     }
357   else if (named_object->is_unknown() || named_object->is_type_declaration())
358     ;
359   else
360     ok = false;
361
362   if (!ok)
363     {
364       if (issue_error)
365         error_at(location, "expected type");
366       return Type::make_error_type();
367     }
368
369   if (named_object->is_type())
370     return named_object->type_value();
371   else if (named_object->is_unknown() || named_object->is_type_declaration())
372     return Type::make_forward_declaration(named_object);
373   else
374     go_unreachable();
375 }
376
377 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
378 // ArrayLength = Expression .
379 // ElementType = CompleteType .
380
381 Type*
382 Parse::array_type(bool may_use_ellipsis)
383 {
384   go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
385   const Token* token = this->advance_token();
386
387   Expression* length = NULL;
388   if (token->is_op(OPERATOR_RSQUARE))
389     this->advance_token();
390   else
391     {
392       if (!token->is_op(OPERATOR_ELLIPSIS))
393         length = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
394       else if (may_use_ellipsis)
395         {
396           // An ellipsis is used in composite literals to represent a
397           // fixed array of the size of the number of elements.  We
398           // use a length of nil to represent this, and change the
399           // length when parsing the composite literal.
400           length = Expression::make_nil(this->location());
401           this->advance_token();
402         }
403       else
404         {
405           error_at(this->location(),
406                    "use of %<[...]%> outside of array literal");
407           length = Expression::make_error(this->location());
408           this->advance_token();
409         }
410       if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
411         {
412           error_at(this->location(), "expected %<]%>");
413           return Type::make_error_type();
414         }
415       this->advance_token();
416     }
417
418   Type* element_type = this->type();
419
420   return Type::make_array_type(element_type, length);
421 }
422
423 // MapType = "map" "[" KeyType "]" ValueType .
424 // KeyType = CompleteType .
425 // ValueType = CompleteType .
426
427 Type*
428 Parse::map_type()
429 {
430   Location location = this->location();
431   go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
432   if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
433     {
434       error_at(this->location(), "expected %<[%>");
435       return Type::make_error_type();
436     }
437   this->advance_token();
438
439   Type* key_type = this->type();
440
441   if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
442     {
443       error_at(this->location(), "expected %<]%>");
444       return Type::make_error_type();
445     }
446   this->advance_token();
447
448   Type* value_type = this->type();
449
450   if (key_type->is_error_type() || value_type->is_error_type())
451     return Type::make_error_type();
452
453   return Type::make_map_type(key_type, value_type, location);
454 }
455
456 // StructType     = "struct" "{" { FieldDecl ";" } "}" .
457
458 Type*
459 Parse::struct_type()
460 {
461   go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
462   Location location = this->location();
463   if (!this->advance_token()->is_op(OPERATOR_LCURLY))
464     {
465       Location token_loc = this->location();
466       if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
467           && this->advance_token()->is_op(OPERATOR_LCURLY))
468         error_at(token_loc, "unexpected semicolon or newline before %<{%>");
469       else
470         {
471           error_at(this->location(), "expected %<{%>");
472           return Type::make_error_type();
473         }
474     }
475   this->advance_token();
476
477   Struct_field_list* sfl = new Struct_field_list;
478   while (!this->peek_token()->is_op(OPERATOR_RCURLY))
479     {
480       this->field_decl(sfl);
481       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
482         this->advance_token();
483       else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
484         {
485           error_at(this->location(), "expected %<;%> or %<}%> or newline");
486           if (!this->skip_past_error(OPERATOR_RCURLY))
487             return Type::make_error_type();
488         }
489     }
490   this->advance_token();
491
492   for (Struct_field_list::const_iterator pi = sfl->begin();
493        pi != sfl->end();
494        ++pi)
495     {
496       if (pi->type()->is_error_type())
497         return pi->type();
498       for (Struct_field_list::const_iterator pj = pi + 1;
499            pj != sfl->end();
500            ++pj)
501         {
502           if (pi->field_name() == pj->field_name()
503               && !Gogo::is_sink_name(pi->field_name()))
504             error_at(pi->location(), "duplicate field name %<%s%>",
505                      Gogo::message_name(pi->field_name()).c_str());
506         }
507     }
508
509   return Type::make_struct_type(sfl, location);
510 }
511
512 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
513 // Tag = string_lit .
514
515 void
516 Parse::field_decl(Struct_field_list* sfl)
517 {
518   const Token* token = this->peek_token();
519   Location location = token->location();
520   bool is_anonymous;
521   bool is_anonymous_pointer;
522   if (token->is_op(OPERATOR_MULT))
523     {
524       is_anonymous = true;
525       is_anonymous_pointer = true;
526     }
527   else if (token->is_identifier())
528     {
529       std::string id = token->identifier();
530       bool is_id_exported = token->is_identifier_exported();
531       Location id_location = token->location();
532       token = this->advance_token();
533       is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
534                       || token->is_op(OPERATOR_RCURLY)
535                       || token->is_op(OPERATOR_DOT)
536                       || token->is_string());
537       is_anonymous_pointer = false;
538       this->unget_token(Token::make_identifier_token(id, is_id_exported,
539                                                      id_location));
540     }
541   else
542     {
543       error_at(this->location(), "expected field name");
544       this->gogo_->mark_locals_used();
545       while (!token->is_op(OPERATOR_SEMICOLON)
546              && !token->is_op(OPERATOR_RCURLY)
547              && !token->is_eof())
548         token = this->advance_token();
549       return;
550     }
551
552   if (is_anonymous)
553     {
554       if (is_anonymous_pointer)
555         {
556           this->advance_token();
557           if (!this->peek_token()->is_identifier())
558             {
559               error_at(this->location(), "expected field name");
560               this->gogo_->mark_locals_used();
561               while (!token->is_op(OPERATOR_SEMICOLON)
562                      && !token->is_op(OPERATOR_RCURLY)
563                      && !token->is_eof())
564                 token = this->advance_token();
565               return;
566             }
567         }
568       Type* type = this->type_name(true);
569
570       std::string tag;
571       if (this->peek_token()->is_string())
572         {
573           tag = this->peek_token()->string_value();
574           this->advance_token();
575         }
576
577       if (!type->is_error_type())
578         {
579           if (is_anonymous_pointer)
580             type = Type::make_pointer_type(type);
581           sfl->push_back(Struct_field(Typed_identifier("", type, location)));
582           if (!tag.empty())
583             sfl->back().set_tag(tag);
584         }
585     }
586   else
587     {
588       Typed_identifier_list til;
589       while (true)
590         {
591           token = this->peek_token();
592           if (!token->is_identifier())
593             {
594               error_at(this->location(), "expected identifier");
595               return;
596             }
597           std::string name =
598             this->gogo_->pack_hidden_name(token->identifier(),
599                                           token->is_identifier_exported());
600           til.push_back(Typed_identifier(name, NULL, token->location()));
601           if (!this->advance_token()->is_op(OPERATOR_COMMA))
602             break;
603           this->advance_token();
604         }
605
606       Type* type = this->type();
607
608       std::string tag;
609       if (this->peek_token()->is_string())
610         {
611           tag = this->peek_token()->string_value();
612           this->advance_token();
613         }
614
615       for (Typed_identifier_list::iterator p = til.begin();
616            p != til.end();
617            ++p)
618         {
619           p->set_type(type);
620           sfl->push_back(Struct_field(*p));
621           if (!tag.empty())
622             sfl->back().set_tag(tag);
623         }
624     }
625 }
626
627 // PointerType = "*" Type .
628
629 Type*
630 Parse::pointer_type()
631 {
632   go_assert(this->peek_token()->is_op(OPERATOR_MULT));
633   this->advance_token();
634   Type* type = this->type();
635   if (type->is_error_type())
636     return type;
637   return Type::make_pointer_type(type);
638 }
639
640 // ChannelType   = Channel | SendChannel | RecvChannel .
641 // Channel       = "chan" ElementType .
642 // SendChannel   = "chan" "<-" ElementType .
643 // RecvChannel   = "<-" "chan" ElementType .
644
645 Type*
646 Parse::channel_type()
647 {
648   const Token* token = this->peek_token();
649   bool send = true;
650   bool receive = true;
651   if (token->is_op(OPERATOR_CHANOP))
652     {
653       if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
654         {
655           error_at(this->location(), "expected %<chan%>");
656           return Type::make_error_type();
657         }
658       send = false;
659       this->advance_token();
660     }
661   else
662     {
663       go_assert(token->is_keyword(KEYWORD_CHAN));
664       if (this->advance_token()->is_op(OPERATOR_CHANOP))
665         {
666           receive = false;
667           this->advance_token();
668         }
669     }
670
671   // Better error messages for the common error of omitting the
672   // channel element type.
673   if (!this->type_may_start_here())
674     {
675       token = this->peek_token();
676       if (token->is_op(OPERATOR_RCURLY))
677         error_at(this->location(), "unexpected %<}%> in channel type");
678       else if (token->is_op(OPERATOR_RPAREN))
679         error_at(this->location(), "unexpected %<)%> in channel type");
680       else if (token->is_op(OPERATOR_COMMA))
681         error_at(this->location(), "unexpected comma in channel type");
682       else
683         error_at(this->location(), "expected channel element type");
684       return Type::make_error_type();
685     }
686
687   Type* element_type = this->type();
688   return Type::make_channel_type(send, receive, element_type);
689 }
690
691 // Give an error for a duplicate parameter or receiver name.
692
693 void
694 Parse::check_signature_names(const Typed_identifier_list* params,
695                              Parse::Names* names)
696 {
697   for (Typed_identifier_list::const_iterator p = params->begin();
698        p != params->end();
699        ++p)
700     {
701       if (p->name().empty() || Gogo::is_sink_name(p->name()))
702         continue;
703       std::pair<std::string, const Typed_identifier*> val =
704         std::make_pair(p->name(), &*p);
705       std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
706       if (!ins.second)
707         {
708           error_at(p->location(), "redefinition of %qs",
709                    Gogo::message_name(p->name()).c_str());
710           inform(ins.first->second->location(),
711                  "previous definition of %qs was here",
712                  Gogo::message_name(p->name()).c_str());
713         }
714     }
715 }
716
717 // Signature      = Parameters [ Result ] .
718
719 // RECEIVER is the receiver if there is one, or NULL.  LOCATION is the
720 // location of the start of the type.
721
722 // This returns NULL on a parse error.
723
724 Function_type*
725 Parse::signature(Typed_identifier* receiver, Location location)
726 {
727   bool is_varargs = false;
728   Typed_identifier_list* params;
729   bool params_ok = this->parameters(&params, &is_varargs);
730
731   Typed_identifier_list* results = NULL;
732   if (this->peek_token()->is_op(OPERATOR_LPAREN)
733       || this->type_may_start_here())
734     {
735       if (!this->result(&results))
736         return NULL;
737     }
738
739   if (!params_ok)
740     return NULL;
741
742   Parse::Names names;
743   if (params != NULL)
744     this->check_signature_names(params, &names);
745   if (results != NULL)
746     this->check_signature_names(results, &names);
747
748   Function_type* ret = Type::make_function_type(receiver, params, results,
749                                                 location);
750   if (is_varargs)
751     ret->set_is_varargs();
752   return ret;
753 }
754
755 // Parameters     = "(" [ ParameterList [ "," ] ] ")" .
756
757 // This returns false on a parse error.
758
759 bool
760 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
761 {
762   *pparams = NULL;
763
764   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
765     {
766       error_at(this->location(), "expected %<(%>");
767       return false;
768     }
769
770   Typed_identifier_list* params = NULL;
771   bool saw_error = false;
772
773   const Token* token = this->advance_token();
774   if (!token->is_op(OPERATOR_RPAREN))
775     {
776       params = this->parameter_list(is_varargs);
777       if (params == NULL)
778         saw_error = true;
779       token = this->peek_token();
780     }
781
782   // The optional trailing comma is picked up in parameter_list.
783
784   if (!token->is_op(OPERATOR_RPAREN))
785     error_at(this->location(), "expected %<)%>");
786   else
787     this->advance_token();
788
789   if (saw_error)
790     return false;
791
792   *pparams = params;
793   return true;
794 }
795
796 // ParameterList  = ParameterDecl { "," ParameterDecl } .
797
798 // This sets *IS_VARARGS if the list ends with an ellipsis.
799 // IS_VARARGS will be NULL if varargs are not permitted.
800
801 // We pick up an optional trailing comma.
802
803 // This returns NULL if some error is seen.
804
805 Typed_identifier_list*
806 Parse::parameter_list(bool* is_varargs)
807 {
808   Location location = this->location();
809   Typed_identifier_list* ret = new Typed_identifier_list();
810
811   bool saw_error = false;
812
813   // If we see an identifier and then a comma, then we don't know
814   // whether we are looking at a list of identifiers followed by a
815   // type, or a list of types given by name.  We have to do an
816   // arbitrary lookahead to figure it out.
817
818   bool parameters_have_names;
819   const Token* token = this->peek_token();
820   if (!token->is_identifier())
821     {
822       // This must be a type which starts with something like '*'.
823       parameters_have_names = false;
824     }
825   else
826     {
827       std::string name = token->identifier();
828       bool is_exported = token->is_identifier_exported();
829       Location location = token->location();
830       token = this->advance_token();
831       if (!token->is_op(OPERATOR_COMMA))
832         {
833           if (token->is_op(OPERATOR_DOT))
834             {
835               // This is a qualified identifier, which must turn out
836               // to be a type.
837               parameters_have_names = false;
838             }
839           else if (token->is_op(OPERATOR_RPAREN))
840             {
841               // A single identifier followed by a parenthesis must be
842               // a type name.
843               parameters_have_names = false;
844             }
845           else
846             {
847               // An identifier followed by something other than a
848               // comma or a dot or a right parenthesis must be a
849               // parameter name followed by a type.
850               parameters_have_names = true;
851             }
852
853           this->unget_token(Token::make_identifier_token(name, is_exported,
854                                                          location));
855         }
856       else
857         {
858           // An identifier followed by a comma may be the first in a
859           // list of parameter names followed by a type, or it may be
860           // the first in a list of types without parameter names.  To
861           // find out we gather as many identifiers separated by
862           // commas as we can.
863           std::string id_name = this->gogo_->pack_hidden_name(name,
864                                                               is_exported);
865           ret->push_back(Typed_identifier(id_name, NULL, location));
866           bool just_saw_comma = true;
867           while (this->advance_token()->is_identifier())
868             {
869               name = this->peek_token()->identifier();
870               is_exported = this->peek_token()->is_identifier_exported();
871               location = this->peek_token()->location();
872               id_name = this->gogo_->pack_hidden_name(name, is_exported);
873               ret->push_back(Typed_identifier(id_name, NULL, location));
874               if (!this->advance_token()->is_op(OPERATOR_COMMA))
875                 {
876                   just_saw_comma = false;
877                   break;
878                 }
879             }
880
881           if (just_saw_comma)
882             {
883               // We saw ID1 "," ID2 "," followed by something which
884               // was not an identifier.  We must be seeing the start
885               // of a type, and ID1 and ID2 must be types, and the
886               // parameters don't have names.
887               parameters_have_names = false;
888             }
889           else if (this->peek_token()->is_op(OPERATOR_RPAREN))
890             {
891               // We saw ID1 "," ID2 ")".  ID1 and ID2 must be types,
892               // and the parameters don't have names.
893               parameters_have_names = false;
894             }
895           else if (this->peek_token()->is_op(OPERATOR_DOT))
896             {
897               // We saw ID1 "," ID2 ".".  ID2 must be a package name,
898               // ID1 must be a type, and the parameters don't have
899               // names.
900               parameters_have_names = false;
901               this->unget_token(Token::make_identifier_token(name, is_exported,
902                                                              location));
903               ret->pop_back();
904               just_saw_comma = true;
905             }
906           else
907             {
908               // We saw ID1 "," ID2 followed by something other than
909               // ",", ".", or ")".  We must be looking at the start of
910               // a type, and ID1 and ID2 must be parameter names.
911               parameters_have_names = true;
912             }
913
914           if (parameters_have_names)
915             {
916               go_assert(!just_saw_comma);
917               // We have just seen ID1, ID2 xxx.
918               Type* type;
919               if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
920                 type = this->type();
921               else
922                 {
923                   error_at(this->location(), "%<...%> only permits one name");
924                   saw_error = true;
925                   this->advance_token();
926                   type = this->type();
927                 }
928               for (size_t i = 0; i < ret->size(); ++i)
929                 ret->set_type(i, type);
930               if (!this->peek_token()->is_op(OPERATOR_COMMA))
931                 return saw_error ? NULL : ret;
932               if (this->advance_token()->is_op(OPERATOR_RPAREN))
933                 return saw_error ? NULL : ret;
934             }
935           else
936             {
937               Typed_identifier_list* tret = new Typed_identifier_list();
938               for (Typed_identifier_list::const_iterator p = ret->begin();
939                    p != ret->end();
940                    ++p)
941                 {
942                   Named_object* no = this->gogo_->lookup(p->name(), NULL);
943                   Type* type;
944                   if (no == NULL)
945                     no = this->gogo_->add_unknown_name(p->name(),
946                                                        p->location());
947
948                   if (no->is_type())
949                     type = no->type_value();
950                   else if (no->is_unknown() || no->is_type_declaration())
951                     type = Type::make_forward_declaration(no);
952                   else
953                     {
954                       error_at(p->location(), "expected %<%s%> to be a type",
955                                Gogo::message_name(p->name()).c_str());
956                       saw_error = true;
957                       type = Type::make_error_type();
958                     }
959                   tret->push_back(Typed_identifier("", type, p->location()));
960                 }
961               delete ret;
962               ret = tret;
963               if (!just_saw_comma
964                   || this->peek_token()->is_op(OPERATOR_RPAREN))
965                 return saw_error ? NULL : ret;
966             }
967         }
968     }
969
970   bool mix_error = false;
971   this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
972   while (this->peek_token()->is_op(OPERATOR_COMMA))
973     {
974       if (is_varargs != NULL && *is_varargs)
975         {
976           error_at(this->location(), "%<...%> must be last parameter");
977           saw_error = true;
978         }
979       if (this->advance_token()->is_op(OPERATOR_RPAREN))
980         break;
981       this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
982     }
983   if (mix_error)
984     {
985       error_at(location, "invalid named/anonymous mix");
986       saw_error = true;
987     }
988   if (saw_error)
989     {
990       delete ret;
991       return NULL;
992     }
993   return ret;
994 }
995
996 // ParameterDecl  = [ IdentifierList ] [ "..." ] Type .
997
998 void
999 Parse::parameter_decl(bool parameters_have_names,
1000                       Typed_identifier_list* til,
1001                       bool* is_varargs,
1002                       bool* mix_error)
1003 {
1004   if (!parameters_have_names)
1005     {
1006       Type* type;
1007       Location location = this->location();
1008       if (!this->peek_token()->is_identifier())
1009         {
1010           if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1011             type = this->type();
1012           else
1013             {
1014               if (is_varargs == NULL)
1015                 error_at(this->location(), "invalid use of %<...%>");
1016               else
1017                 *is_varargs = true;
1018               this->advance_token();
1019               if (is_varargs == NULL
1020                   && this->peek_token()->is_op(OPERATOR_RPAREN))
1021                 type = Type::make_error_type();
1022               else
1023                 {
1024                   Type* element_type = this->type();
1025                   type = Type::make_array_type(element_type, NULL);
1026                 }
1027             }
1028         }
1029       else
1030         {
1031           type = this->type_name(false);
1032           if (type->is_error_type()
1033               || (!this->peek_token()->is_op(OPERATOR_COMMA)
1034                   && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1035             {
1036               *mix_error = true;
1037               while (!this->peek_token()->is_op(OPERATOR_COMMA)
1038                      && !this->peek_token()->is_op(OPERATOR_RPAREN))
1039                 this->advance_token();
1040             }
1041         }
1042       if (!type->is_error_type())
1043         til->push_back(Typed_identifier("", type, location));
1044     }
1045   else
1046     {
1047       size_t orig_count = til->size();
1048       if (this->peek_token()->is_identifier())
1049         this->identifier_list(til);
1050       else
1051         *mix_error = true;
1052       size_t new_count = til->size();
1053
1054       Type* type;
1055       if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1056         type = this->type();
1057       else
1058         {
1059           if (is_varargs == NULL)
1060             error_at(this->location(), "invalid use of %<...%>");
1061           else if (new_count > orig_count + 1)
1062             error_at(this->location(), "%<...%> only permits one name");
1063           else
1064             *is_varargs = true;
1065           this->advance_token();
1066           Type* element_type = this->type();
1067           type = Type::make_array_type(element_type, NULL);
1068         }
1069       for (size_t i = orig_count; i < new_count; ++i)
1070         til->set_type(i, type);
1071     }
1072 }
1073
1074 // Result         = Parameters | Type .
1075
1076 // This returns false on a parse error.
1077
1078 bool
1079 Parse::result(Typed_identifier_list** presults)
1080 {
1081   if (this->peek_token()->is_op(OPERATOR_LPAREN))
1082     return this->parameters(presults, NULL);
1083   else
1084     {
1085       Location location = this->location();
1086       Type* type = this->type();
1087       if (type->is_error_type())
1088         {
1089           *presults = NULL;
1090           return false;
1091         }
1092       Typed_identifier_list* til = new Typed_identifier_list();
1093       til->push_back(Typed_identifier("", type, location));
1094       *presults = til;
1095       return true;
1096     }
1097 }
1098
1099 // Block = "{" [ StatementList ] "}" .
1100
1101 // Returns the location of the closing brace.
1102
1103 Location
1104 Parse::block()
1105 {
1106   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1107     {
1108       Location loc = this->location();
1109       if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1110           && this->advance_token()->is_op(OPERATOR_LCURLY))
1111         error_at(loc, "unexpected semicolon or newline before %<{%>");
1112       else
1113         {
1114           error_at(this->location(), "expected %<{%>");
1115           return Linemap::unknown_location();
1116         }
1117     }
1118
1119   const Token* token = this->advance_token();
1120
1121   if (!token->is_op(OPERATOR_RCURLY))
1122     {
1123       this->statement_list();
1124       token = this->peek_token();
1125       if (!token->is_op(OPERATOR_RCURLY))
1126         {
1127           if (!token->is_eof() || !saw_errors())
1128             error_at(this->location(), "expected %<}%>");
1129
1130           this->gogo_->mark_locals_used();
1131
1132           // Skip ahead to the end of the block, in hopes of avoiding
1133           // lots of meaningless errors.
1134           Location ret = token->location();
1135           int nest = 0;
1136           while (!token->is_eof())
1137             {
1138               if (token->is_op(OPERATOR_LCURLY))
1139                 ++nest;
1140               else if (token->is_op(OPERATOR_RCURLY))
1141                 {
1142                   --nest;
1143                   if (nest < 0)
1144                     {
1145                       this->advance_token();
1146                       break;
1147                     }
1148                 }
1149               token = this->advance_token();
1150               ret = token->location();
1151             }
1152           return ret;
1153         }
1154     }
1155
1156   Location ret = token->location();
1157   this->advance_token();
1158   return ret;
1159 }
1160
1161 // InterfaceType      = "interface" "{" [ MethodSpecList ] "}" .
1162 // MethodSpecList     = MethodSpec { ";" MethodSpec } [ ";" ] .
1163
1164 Type*
1165 Parse::interface_type()
1166 {
1167   go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1168   Location location = this->location();
1169
1170   if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1171     {
1172       Location token_loc = this->location();
1173       if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1174           && this->advance_token()->is_op(OPERATOR_LCURLY))
1175         error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1176       else
1177         {
1178           error_at(this->location(), "expected %<{%>");
1179           return Type::make_error_type();
1180         }
1181     }
1182   this->advance_token();
1183
1184   Typed_identifier_list* methods = new Typed_identifier_list();
1185   if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1186     {
1187       this->method_spec(methods);
1188       while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1189         {
1190           if (this->advance_token()->is_op(OPERATOR_RCURLY))
1191             break;
1192           this->method_spec(methods);
1193         }
1194       if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1195         {
1196           error_at(this->location(), "expected %<}%>");
1197           while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1198             {
1199               if (this->peek_token()->is_eof())
1200                 return Type::make_error_type();
1201             }
1202         }
1203     }
1204   this->advance_token();
1205
1206   if (methods->empty())
1207     {
1208       delete methods;
1209       methods = NULL;
1210     }
1211
1212   Interface_type* ret = Type::make_interface_type(methods, location);
1213   this->gogo_->record_interface_type(ret);
1214   return ret;
1215 }
1216
1217 // MethodSpec         = MethodName Signature | InterfaceTypeName .
1218 // MethodName         = identifier .
1219 // InterfaceTypeName  = TypeName .
1220
1221 void
1222 Parse::method_spec(Typed_identifier_list* methods)
1223 {
1224   const Token* token = this->peek_token();
1225   if (!token->is_identifier())
1226     {
1227       error_at(this->location(), "expected identifier");
1228       return;
1229     }
1230
1231   std::string name = token->identifier();
1232   bool is_exported = token->is_identifier_exported();
1233   Location location = token->location();
1234
1235   if (this->advance_token()->is_op(OPERATOR_LPAREN))
1236     {
1237       // This is a MethodName.
1238       name = this->gogo_->pack_hidden_name(name, is_exported);
1239       Type* type = this->signature(NULL, location);
1240       if (type == NULL)
1241         return;
1242       methods->push_back(Typed_identifier(name, type, location));
1243     }
1244   else
1245     {
1246       this->unget_token(Token::make_identifier_token(name, is_exported,
1247                                                      location));
1248       Type* type = this->type_name(false);
1249       if (type->is_error_type()
1250           || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1251               && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1252         {
1253           if (this->peek_token()->is_op(OPERATOR_COMMA))
1254             error_at(this->location(),
1255                      "name list not allowed in interface type");
1256           else
1257             error_at(location, "expected signature or type name");
1258           this->gogo_->mark_locals_used();
1259           token = this->peek_token();
1260           while (!token->is_eof()
1261                  && !token->is_op(OPERATOR_SEMICOLON)
1262                  && !token->is_op(OPERATOR_RCURLY))
1263             token = this->advance_token();
1264           return;
1265         }
1266       // This must be an interface type, but we can't check that now.
1267       // We check it and pull out the methods in
1268       // Interface_type::do_verify.
1269       methods->push_back(Typed_identifier("", type, location));
1270     }
1271 }
1272
1273 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1274
1275 void
1276 Parse::declaration()
1277 {
1278   const Token* token = this->peek_token();
1279   if (token->is_keyword(KEYWORD_CONST))
1280     this->const_decl();
1281   else if (token->is_keyword(KEYWORD_TYPE))
1282     this->type_decl();
1283   else if (token->is_keyword(KEYWORD_VAR))
1284     this->var_decl();
1285   else if (token->is_keyword(KEYWORD_FUNC))
1286     this->function_decl();
1287   else
1288     {
1289       error_at(this->location(), "expected declaration");
1290       this->advance_token();
1291     }
1292 }
1293
1294 bool
1295 Parse::declaration_may_start_here()
1296 {
1297   const Token* token = this->peek_token();
1298   return (token->is_keyword(KEYWORD_CONST)
1299           || token->is_keyword(KEYWORD_TYPE)
1300           || token->is_keyword(KEYWORD_VAR)
1301           || token->is_keyword(KEYWORD_FUNC));
1302 }
1303
1304 // Decl<P> = P | "(" [ List<P> ] ")" .
1305
1306 void
1307 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1308 {
1309   if (this->peek_token()->is_eof())
1310     {
1311       if (!saw_errors())
1312         error_at(this->location(), "unexpected end of file");
1313       return;
1314     }
1315
1316   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1317     (this->*pfn)(varg);
1318   else
1319     {
1320       if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1321         {
1322           this->list(pfn, varg, true);
1323           if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1324             {
1325               error_at(this->location(), "missing %<)%>");
1326               while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1327                 {
1328                   if (this->peek_token()->is_eof())
1329                     return;
1330                 }
1331             }
1332         }
1333       this->advance_token();
1334     }
1335 }
1336
1337 // List<P> = P { ";" P } [ ";" ] .
1338
1339 // In order to pick up the trailing semicolon we need to know what
1340 // might follow.  This is either a '}' or a ')'.
1341
1342 void
1343 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1344 {
1345   (this->*pfn)(varg);
1346   Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1347   while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1348          || this->peek_token()->is_op(OPERATOR_COMMA))
1349     {
1350       if (this->peek_token()->is_op(OPERATOR_COMMA))
1351         error_at(this->location(), "unexpected comma");
1352       if (this->advance_token()->is_op(follow))
1353         break;
1354       (this->*pfn)(varg);
1355     }
1356 }
1357
1358 // ConstDecl      = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1359
1360 void
1361 Parse::const_decl()
1362 {
1363   go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1364   this->advance_token();
1365   this->reset_iota();
1366
1367   Type* last_type = NULL;
1368   Expression_list* last_expr_list = NULL;
1369
1370   if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1371     this->const_spec(&last_type, &last_expr_list);
1372   else
1373     {
1374       this->advance_token();
1375       while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1376         {
1377           this->const_spec(&last_type, &last_expr_list);
1378           if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1379             this->advance_token();
1380           else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1381             {
1382               error_at(this->location(), "expected %<;%> or %<)%> or newline");
1383               if (!this->skip_past_error(OPERATOR_RPAREN))
1384                 return;
1385             }
1386         }
1387       this->advance_token();
1388     }
1389
1390   if (last_expr_list != NULL)
1391     delete last_expr_list;
1392 }
1393
1394 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1395
1396 void
1397 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1398 {
1399   Typed_identifier_list til;
1400   this->identifier_list(&til);
1401
1402   Type* type = NULL;
1403   if (this->type_may_start_here())
1404     {
1405       type = this->type();
1406       *last_type = NULL;
1407       *last_expr_list = NULL;
1408     }
1409
1410   Expression_list *expr_list;
1411   if (!this->peek_token()->is_op(OPERATOR_EQ))
1412     {
1413       if (*last_expr_list == NULL)
1414         {
1415           error_at(this->location(), "expected %<=%>");
1416           return;
1417         }
1418       type = *last_type;
1419       expr_list = new Expression_list;
1420       for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1421            p != (*last_expr_list)->end();
1422            ++p)
1423         expr_list->push_back((*p)->copy());
1424     }
1425   else
1426     {
1427       this->advance_token();
1428       expr_list = this->expression_list(NULL, false);
1429       *last_type = type;
1430       if (*last_expr_list != NULL)
1431         delete *last_expr_list;
1432       *last_expr_list = expr_list;
1433     }
1434
1435   Expression_list::const_iterator pe = expr_list->begin();
1436   for (Typed_identifier_list::iterator pi = til.begin();
1437        pi != til.end();
1438        ++pi, ++pe)
1439     {
1440       if (pe == expr_list->end())
1441         {
1442           error_at(this->location(), "not enough initializers");
1443           return;
1444         }
1445       if (type != NULL)
1446         pi->set_type(type);
1447
1448       if (!Gogo::is_sink_name(pi->name()))
1449         this->gogo_->add_constant(*pi, *pe, this->iota_value());
1450     }
1451   if (pe != expr_list->end())
1452     error_at(this->location(), "too many initializers");
1453
1454   this->increment_iota();
1455
1456   return;
1457 }
1458
1459 // TypeDecl = "type" Decl<TypeSpec> .
1460
1461 void
1462 Parse::type_decl()
1463 {
1464   go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1465   this->advance_token();
1466   this->decl(&Parse::type_spec, NULL);
1467 }
1468
1469 // TypeSpec = identifier Type .
1470
1471 void
1472 Parse::type_spec(void*)
1473 {
1474   const Token* token = this->peek_token();
1475   if (!token->is_identifier())
1476     {
1477       error_at(this->location(), "expected identifier");
1478       return;
1479     }
1480   std::string name = token->identifier();
1481   bool is_exported = token->is_identifier_exported();
1482   Location location = token->location();
1483   token = this->advance_token();
1484
1485   // The scope of the type name starts at the point where the
1486   // identifier appears in the source code.  We implement this by
1487   // declaring the type before we read the type definition.
1488   Named_object* named_type = NULL;
1489   if (name != "_")
1490     {
1491       name = this->gogo_->pack_hidden_name(name, is_exported);
1492       named_type = this->gogo_->declare_type(name, location);
1493     }
1494
1495   Type* type;
1496   if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1497     type = this->type();
1498   else
1499     {
1500       error_at(this->location(),
1501                "unexpected semicolon or newline in type declaration");
1502       type = Type::make_error_type();
1503       this->advance_token();
1504     }
1505
1506   if (type->is_error_type())
1507     {
1508       this->gogo_->mark_locals_used();
1509       while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1510              && !this->peek_token()->is_eof())
1511         this->advance_token();
1512     }
1513
1514   if (name != "_")
1515     {
1516       if (named_type->is_type_declaration())
1517         {
1518           Type* ftype = type->forwarded();
1519           if (ftype->forward_declaration_type() != NULL
1520               && (ftype->forward_declaration_type()->named_object()
1521                   == named_type))
1522             {
1523               error_at(location, "invalid recursive type");
1524               type = Type::make_error_type();
1525             }
1526
1527           this->gogo_->define_type(named_type,
1528                                    Type::make_named_type(named_type, type,
1529                                                          location));
1530           go_assert(named_type->package() == NULL);
1531         }
1532       else
1533         {
1534           // This will probably give a redefinition error.
1535           this->gogo_->add_type(name, type, location);
1536         }
1537     }
1538 }
1539
1540 // VarDecl = "var" Decl<VarSpec> .
1541
1542 void
1543 Parse::var_decl()
1544 {
1545   go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1546   this->advance_token();
1547   this->decl(&Parse::var_spec, NULL);
1548 }
1549
1550 // VarSpec = IdentifierList
1551 //             ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1552
1553 void
1554 Parse::var_spec(void*)
1555 {
1556   // Get the variable names.
1557   Typed_identifier_list til;
1558   this->identifier_list(&til);
1559
1560   Location location = this->location();
1561
1562   Type* type = NULL;
1563   Expression_list* init = NULL;
1564   if (!this->peek_token()->is_op(OPERATOR_EQ))
1565     {
1566       type = this->type();
1567       if (type->is_error_type())
1568         {
1569           this->gogo_->mark_locals_used();
1570           while (!this->peek_token()->is_op(OPERATOR_EQ)
1571                  && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1572                  && !this->peek_token()->is_eof())
1573             this->advance_token();
1574         }
1575       if (this->peek_token()->is_op(OPERATOR_EQ))
1576         {
1577           this->advance_token();
1578           init = this->expression_list(NULL, false);
1579         }
1580     }
1581   else
1582     {
1583       this->advance_token();
1584       init = this->expression_list(NULL, false);
1585     }
1586
1587   this->init_vars(&til, type, init, false, location);
1588
1589   if (init != NULL)
1590     delete init;
1591 }
1592
1593 // Create variables.  TIL is a list of variable names.  If TYPE is not
1594 // NULL, it is the type of all the variables.  If INIT is not NULL, it
1595 // is an initializer list for the variables.
1596
1597 void
1598 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1599                  Expression_list* init, bool is_coloneq,
1600                  Location location)
1601 {
1602   // Check for an initialization which can yield multiple values.
1603   if (init != NULL && init->size() == 1 && til->size() > 1)
1604     {
1605       if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1606                                     location))
1607         return;
1608       if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1609                                    location))
1610         return;
1611       if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1612                                        location))
1613         return;
1614       if (this->init_vars_from_type_guard(til, type, *init->begin(),
1615                                           is_coloneq, location))
1616         return;
1617     }
1618
1619   if (init != NULL && init->size() != til->size())
1620     {
1621       if (init->empty() || !init->front()->is_error_expression())
1622         error_at(location, "wrong number of initializations");
1623       init = NULL;
1624       if (type == NULL)
1625         type = Type::make_error_type();
1626     }
1627
1628   // Note that INIT was already parsed with the old name bindings, so
1629   // we don't have to worry that it will accidentally refer to the
1630   // newly declared variables.
1631
1632   Expression_list::const_iterator pexpr;
1633   if (init != NULL)
1634     pexpr = init->begin();
1635   bool any_new = false;
1636   for (Typed_identifier_list::const_iterator p = til->begin();
1637        p != til->end();
1638        ++p)
1639     {
1640       if (init != NULL)
1641         go_assert(pexpr != init->end());
1642       this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1643                      false, &any_new);
1644       if (init != NULL)
1645         ++pexpr;
1646     }
1647   if (init != NULL)
1648     go_assert(pexpr == init->end());
1649   if (is_coloneq && !any_new)
1650     error_at(location, "variables redeclared but no variable is new");
1651 }
1652
1653 // See if we need to initialize a list of variables from a function
1654 // call.  This returns true if we have set up the variables and the
1655 // initialization.
1656
1657 bool
1658 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1659                            Expression* expr, bool is_coloneq,
1660                            Location location)
1661 {
1662   Call_expression* call = expr->call_expression();
1663   if (call == NULL)
1664     return false;
1665
1666   // This is a function call.  We can't check here whether it returns
1667   // the right number of values, but it might.  Declare the variables,
1668   // and then assign the results of the call to them.
1669
1670   Named_object* first_var = NULL;
1671   unsigned int index = 0;
1672   bool any_new = false;
1673   for (Typed_identifier_list::const_iterator pv = vars->begin();
1674        pv != vars->end();
1675        ++pv, ++index)
1676     {
1677       Expression* init = Expression::make_call_result(call, index);
1678       Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
1679                                         &any_new);
1680
1681       if (this->gogo_->in_global_scope() && no->is_variable())
1682         {
1683           if (first_var == NULL)
1684             first_var = no;
1685           else
1686             {
1687               // The subsequent vars have an implicit dependency on
1688               // the first one, so that everything gets initialized in
1689               // the right order and so that we detect cycles
1690               // correctly.
1691               this->gogo_->record_var_depends_on(no->var_value(), first_var);
1692             }
1693         }
1694     }
1695
1696   if (is_coloneq && !any_new)
1697     error_at(location, "variables redeclared but no variable is new");
1698
1699   return true;
1700 }
1701
1702 // See if we need to initialize a pair of values from a map index
1703 // expression.  This returns true if we have set up the variables and
1704 // the initialization.
1705
1706 bool
1707 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1708                           Expression* expr, bool is_coloneq,
1709                           Location location)
1710 {
1711   Index_expression* index = expr->index_expression();
1712   if (index == NULL)
1713     return false;
1714   if (vars->size() != 2)
1715     return false;
1716
1717   // This is an index which is being assigned to two variables.  It
1718   // must be a map index.  Declare the variables, and then assign the
1719   // results of the map index.
1720   bool any_new = false;
1721   Typed_identifier_list::const_iterator p = vars->begin();
1722   Expression* init = type == NULL ? index : NULL;
1723   Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1724                                         type == NULL, &any_new);
1725   if (type == NULL && any_new && val_no->is_variable())
1726     val_no->var_value()->set_type_from_init_tuple();
1727   Expression* val_var = Expression::make_var_reference(val_no, location);
1728
1729   ++p;
1730   Type* var_type = type;
1731   if (var_type == NULL)
1732     var_type = Type::lookup_bool_type();
1733   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1734                                     &any_new);
1735   Expression* present_var = Expression::make_var_reference(no, location);
1736
1737   if (is_coloneq && !any_new)
1738     error_at(location, "variables redeclared but no variable is new");
1739
1740   Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1741                                                       index, location);
1742
1743   if (!this->gogo_->in_global_scope())
1744     this->gogo_->add_statement(s);
1745   else if (!val_no->is_sink())
1746     {
1747       if (val_no->is_variable())
1748         val_no->var_value()->add_preinit_statement(this->gogo_, s);
1749     }
1750   else if (!no->is_sink())
1751     {
1752       if (no->is_variable())
1753         no->var_value()->add_preinit_statement(this->gogo_, s);
1754     }
1755   else
1756     {
1757       // Execute the map index expression just so that we can fail if
1758       // the map is nil.
1759       Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1760                                                       NULL, location);
1761       dummy->var_value()->add_preinit_statement(this->gogo_, s);
1762     }
1763
1764   return true;
1765 }
1766
1767 // See if we need to initialize a pair of values from a receive
1768 // expression.  This returns true if we have set up the variables and
1769 // the initialization.
1770
1771 bool
1772 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1773                               Expression* expr, bool is_coloneq,
1774                               Location location)
1775 {
1776   Receive_expression* receive = expr->receive_expression();
1777   if (receive == NULL)
1778     return false;
1779   if (vars->size() != 2)
1780     return false;
1781
1782   // This is a receive expression which is being assigned to two
1783   // variables.  Declare the variables, and then assign the results of
1784   // the receive.
1785   bool any_new = false;
1786   Typed_identifier_list::const_iterator p = vars->begin();
1787   Expression* init = type == NULL ? receive : NULL;
1788   Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1789                                         type == NULL, &any_new);
1790   if (type == NULL && any_new && val_no->is_variable())
1791     val_no->var_value()->set_type_from_init_tuple();
1792   Expression* val_var = Expression::make_var_reference(val_no, location);
1793
1794   ++p;
1795   Type* var_type = type;
1796   if (var_type == NULL)
1797     var_type = Type::lookup_bool_type();
1798   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1799                                     &any_new);
1800   Expression* received_var = Expression::make_var_reference(no, location);
1801
1802   if (is_coloneq && !any_new)
1803     error_at(location, "variables redeclared but no variable is new");
1804
1805   Statement* s = Statement::make_tuple_receive_assignment(val_var,
1806                                                           received_var,
1807                                                           receive->channel(),
1808                                                           location);
1809
1810   if (!this->gogo_->in_global_scope())
1811     this->gogo_->add_statement(s);
1812   else if (!val_no->is_sink())
1813     {
1814       if (val_no->is_variable())
1815         val_no->var_value()->add_preinit_statement(this->gogo_, s);
1816     }
1817   else if (!no->is_sink())
1818     {
1819       if (no->is_variable())
1820         no->var_value()->add_preinit_statement(this->gogo_, s);
1821     }
1822   else
1823     {
1824       Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1825                                                       NULL, location);
1826       dummy->var_value()->add_preinit_statement(this->gogo_, s);
1827     }
1828
1829   return true;
1830 }
1831
1832 // See if we need to initialize a pair of values from a type guard
1833 // expression.  This returns true if we have set up the variables and
1834 // the initialization.
1835
1836 bool
1837 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1838                                  Type* type, Expression* expr,
1839                                  bool is_coloneq, Location location)
1840 {
1841   Type_guard_expression* type_guard = expr->type_guard_expression();
1842   if (type_guard == NULL)
1843     return false;
1844   if (vars->size() != 2)
1845     return false;
1846
1847   // This is a type guard expression which is being assigned to two
1848   // variables.  Declare the variables, and then assign the results of
1849   // the type guard.
1850   bool any_new = false;
1851   Typed_identifier_list::const_iterator p = vars->begin();
1852   Type* var_type = type;
1853   if (var_type == NULL)
1854     var_type = type_guard->type();
1855   Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1856                                         &any_new);
1857   Expression* val_var = Expression::make_var_reference(val_no, location);
1858
1859   ++p;
1860   var_type = type;
1861   if (var_type == NULL)
1862     var_type = Type::lookup_bool_type();
1863   Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1864                                     &any_new);
1865   Expression* ok_var = Expression::make_var_reference(no, location);
1866
1867   Expression* texpr = type_guard->expr();
1868   Type* t = type_guard->type();
1869   Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1870                                                              texpr, t,
1871                                                              location);
1872
1873   if (is_coloneq && !any_new)
1874     error_at(location, "variables redeclared but no variable is new");
1875
1876   if (!this->gogo_->in_global_scope())
1877     this->gogo_->add_statement(s);
1878   else if (!val_no->is_sink())
1879     {
1880       if (val_no->is_variable())
1881         val_no->var_value()->add_preinit_statement(this->gogo_, s);
1882     }
1883   else if (!no->is_sink())
1884     {
1885       if (no->is_variable())
1886         no->var_value()->add_preinit_statement(this->gogo_, s);
1887     }
1888   else
1889     {
1890       Named_object* dummy = this->create_dummy_global(type, NULL, location);
1891       dummy->var_value()->add_preinit_statement(this->gogo_, s);
1892     }
1893
1894   return true;
1895 }
1896
1897 // Create a single variable.  If IS_COLONEQ is true, we permit
1898 // redeclarations in the same block, and we set *IS_NEW when we find a
1899 // new variable which is not a redeclaration.
1900
1901 Named_object*
1902 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1903                 bool is_coloneq, bool type_from_init, bool* is_new)
1904 {
1905   Location location = tid.location();
1906
1907   if (Gogo::is_sink_name(tid.name()))
1908     {
1909       if (!type_from_init && init != NULL)
1910         {
1911           if (this->gogo_->in_global_scope())
1912             return this->create_dummy_global(type, init, location);
1913           else if (type == NULL)
1914             this->gogo_->add_statement(Statement::make_statement(init, true));
1915           else
1916             {
1917               // With both a type and an initializer, create a dummy
1918               // variable so that we will check whether the
1919               // initializer can be assigned to the type.
1920               Variable* var = new Variable(type, init, false, false, false,
1921                                            location);
1922               var->set_is_used();
1923               static int count;
1924               char buf[30];
1925               snprintf(buf, sizeof buf, "sink$%d", count);
1926               ++count;
1927               return this->gogo_->add_variable(buf, var);
1928             }
1929         }
1930       if (type != NULL)
1931         this->gogo_->add_type_to_verify(type);
1932       return this->gogo_->add_sink();
1933     }
1934
1935   if (is_coloneq)
1936     {
1937       Named_object* no = this->gogo_->lookup_in_block(tid.name());
1938       if (no != NULL
1939           && (no->is_variable() || no->is_result_variable()))
1940         {
1941           // INIT may be NULL even when IS_COLONEQ is true for cases
1942           // like v, ok := x.(int).
1943           if (!type_from_init && init != NULL)
1944             {
1945               Expression *v = Expression::make_var_reference(no, location);
1946               Statement *s = Statement::make_assignment(v, init, location);
1947               this->gogo_->add_statement(s);
1948             }
1949           return no;
1950         }
1951     }
1952   *is_new = true;
1953   Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1954                                false, false, location);
1955   Named_object* no = this->gogo_->add_variable(tid.name(), var);
1956   if (!no->is_variable())
1957     {
1958       // The name is already defined, so we just gave an error.
1959       return this->gogo_->add_sink();
1960     }
1961   return no;
1962 }
1963
1964 // Create a dummy global variable to force an initializer to be run in
1965 // the right place.  This is used when a sink variable is initialized
1966 // at global scope.
1967
1968 Named_object*
1969 Parse::create_dummy_global(Type* type, Expression* init,
1970                            Location location)
1971 {
1972   if (type == NULL && init == NULL)
1973     type = Type::lookup_bool_type();
1974   Variable* var = new Variable(type, init, true, false, false, location);
1975   static int count;
1976   char buf[30];
1977   snprintf(buf, sizeof buf, "_.%d", count);
1978   ++count;
1979   return this->gogo_->add_variable(buf, var);
1980 }
1981
1982 // SimpleVarDecl = identifier ":=" Expression .
1983
1984 // We've already seen the identifier.
1985
1986 // FIXME: We also have to implement
1987 //  IdentifierList ":=" ExpressionList
1988 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1989 // tuple assignments here as well.
1990
1991 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
1992 // RangeClause.
1993
1994 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
1995 // guard (var := expr.("type") using the literal keyword "type").
1996
1997 void
1998 Parse::simple_var_decl_or_assignment(const std::string& name,
1999                                      Location location,
2000                                      Range_clause* p_range_clause,
2001                                      Type_switch* p_type_switch)
2002 {
2003   Typed_identifier_list til;
2004   til.push_back(Typed_identifier(name, NULL, location));
2005
2006   // We've seen one identifier.  If we see a comma now, this could be
2007   // "a, *p = 1, 2".
2008   if (this->peek_token()->is_op(OPERATOR_COMMA))
2009     {
2010       go_assert(p_type_switch == NULL);
2011       while (true)
2012         {
2013           const Token* token = this->advance_token();
2014           if (!token->is_identifier())
2015             break;
2016
2017           std::string id = token->identifier();
2018           bool is_id_exported = token->is_identifier_exported();
2019           Location id_location = token->location();
2020
2021           token = this->advance_token();
2022           if (!token->is_op(OPERATOR_COMMA))
2023             {
2024               if (token->is_op(OPERATOR_COLONEQ))
2025                 {
2026                   id = this->gogo_->pack_hidden_name(id, is_id_exported);
2027                   til.push_back(Typed_identifier(id, NULL, location));
2028                 }
2029               else
2030                 this->unget_token(Token::make_identifier_token(id,
2031                                                                is_id_exported,
2032                                                                id_location));
2033               break;
2034             }
2035
2036           id = this->gogo_->pack_hidden_name(id, is_id_exported);
2037           til.push_back(Typed_identifier(id, NULL, location));
2038         }
2039
2040       // We have a comma separated list of identifiers in TIL.  If the
2041       // next token is COLONEQ, then this is a simple var decl, and we
2042       // have the complete list of identifiers.  If the next token is
2043       // not COLONEQ, then the only valid parse is a tuple assignment.
2044       // The list of identifiers we have so far is really a list of
2045       // expressions.  There are more expressions following.
2046
2047       if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2048         {
2049           Expression_list* exprs = new Expression_list;
2050           for (Typed_identifier_list::const_iterator p = til.begin();
2051                p != til.end();
2052                ++p)
2053             exprs->push_back(this->id_to_expression(p->name(),
2054                                                     p->location()));
2055
2056           Expression_list* more_exprs = this->expression_list(NULL, true);
2057           for (Expression_list::const_iterator p = more_exprs->begin();
2058                p != more_exprs->end();
2059                ++p)
2060             exprs->push_back(*p);
2061           delete more_exprs;
2062
2063           this->tuple_assignment(exprs, p_range_clause);
2064           return;
2065         }
2066     }
2067
2068   go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2069   const Token* token = this->advance_token();
2070
2071   if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2072     {
2073       this->range_clause_decl(&til, p_range_clause);
2074       return;
2075     }
2076
2077   Expression_list* init;
2078   if (p_type_switch == NULL)
2079     init = this->expression_list(NULL, false);
2080   else
2081     {
2082       bool is_type_switch = false;
2083       Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2084                                           &is_type_switch);
2085       if (is_type_switch)
2086         {
2087           p_type_switch->found = true;
2088           p_type_switch->name = name;
2089           p_type_switch->location = location;
2090           p_type_switch->expr = expr;
2091           return;
2092         }
2093
2094       if (!this->peek_token()->is_op(OPERATOR_COMMA))
2095         {
2096           init = new Expression_list();
2097           init->push_back(expr);
2098         }
2099       else
2100         {
2101           this->advance_token();
2102           init = this->expression_list(expr, false);
2103         }
2104     }
2105
2106   this->init_vars(&til, NULL, init, true, location);
2107 }
2108
2109 // FunctionDecl = "func" identifier Signature [ Block ] .
2110 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2111
2112 // Deprecated gcc extension:
2113 //   FunctionDecl = "func" identifier Signature
2114 //                    __asm__ "(" string_lit ")" .
2115 // This extension means a function whose real name is the identifier
2116 // inside the asm.  This extension will be removed at some future
2117 // date.  It has been replaced with //extern comments.
2118
2119 void
2120 Parse::function_decl()
2121 {
2122   go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2123   Location location = this->location();
2124   std::string extern_name = this->lex_->extern_name();
2125   const Token* token = this->advance_token();
2126
2127   Typed_identifier* rec = NULL;
2128   if (token->is_op(OPERATOR_LPAREN))
2129     {
2130       rec = this->receiver();
2131       token = this->peek_token();
2132     }
2133
2134   if (!token->is_identifier())
2135     {
2136       error_at(this->location(), "expected function name");
2137       return;
2138     }
2139
2140   std::string name =
2141     this->gogo_->pack_hidden_name(token->identifier(),
2142                                   token->is_identifier_exported());
2143
2144   this->advance_token();
2145
2146   Function_type* fntype = this->signature(rec, this->location());
2147
2148   Named_object* named_object = NULL;
2149
2150   if (this->peek_token()->is_keyword(KEYWORD_ASM))
2151     {
2152       if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2153         {
2154           error_at(this->location(), "expected %<(%>");
2155           return;
2156         }
2157       token = this->advance_token();
2158       if (!token->is_string())
2159         {
2160           error_at(this->location(), "expected string");
2161           return;
2162         }
2163       std::string asm_name = token->string_value();
2164       if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2165         {
2166           error_at(this->location(), "expected %<)%>");
2167           return;
2168         }
2169       this->advance_token();
2170       if (!Gogo::is_sink_name(name))
2171         {
2172           named_object = this->gogo_->declare_function(name, fntype, location);
2173           if (named_object->is_function_declaration())
2174             named_object->func_declaration_value()->set_asm_name(asm_name);
2175         }
2176     }
2177
2178   // Check for the easy error of a newline before the opening brace.
2179   if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2180     {
2181       Location semi_loc = this->location();
2182       if (this->advance_token()->is_op(OPERATOR_LCURLY))
2183         error_at(this->location(),
2184                  "unexpected semicolon or newline before %<{%>");
2185       else
2186         this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2187                                                      semi_loc));
2188     }
2189
2190   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2191     {
2192       if (named_object == NULL && !Gogo::is_sink_name(name))
2193         {
2194           if (fntype == NULL)
2195             this->gogo_->add_erroneous_name(name);
2196           else
2197             {
2198               named_object = this->gogo_->declare_function(name, fntype,
2199                                                            location);
2200               if (!extern_name.empty()
2201                   && named_object->is_function_declaration())
2202                 {
2203                   Function_declaration* fd =
2204                     named_object->func_declaration_value();
2205                   fd->set_asm_name(extern_name);
2206                 }
2207             }
2208         }
2209     }
2210   else
2211     {
2212       bool hold_is_erroneous_function = this->is_erroneous_function_;
2213       if (fntype == NULL)
2214         {
2215           fntype = Type::make_function_type(NULL, NULL, NULL, location);
2216           this->is_erroneous_function_ = true;
2217           if (!Gogo::is_sink_name(name))
2218             this->gogo_->add_erroneous_name(name);
2219           name = this->gogo_->pack_hidden_name("_", false);
2220         }
2221       this->gogo_->start_function(name, fntype, true, location);
2222       Location end_loc = this->block();
2223       this->gogo_->finish_function(end_loc);
2224       this->is_erroneous_function_ = hold_is_erroneous_function;
2225     }
2226 }
2227
2228 // Receiver     = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2229 // BaseTypeName = identifier .
2230
2231 Typed_identifier*
2232 Parse::receiver()
2233 {
2234   go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2235
2236   std::string name;
2237   const Token* token = this->advance_token();
2238   Location location = token->location();
2239   if (!token->is_op(OPERATOR_MULT))
2240     {
2241       if (!token->is_identifier())
2242         {
2243           error_at(this->location(), "method has no receiver");
2244           this->gogo_->mark_locals_used();
2245           while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2246             token = this->advance_token();
2247           if (!token->is_eof())
2248             this->advance_token();
2249           return NULL;
2250         }
2251       name = token->identifier();
2252       bool is_exported = token->is_identifier_exported();
2253       token = this->advance_token();
2254       if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2255         {
2256           // An identifier followed by something other than a dot or a
2257           // right parenthesis must be a receiver name followed by a
2258           // type.
2259           name = this->gogo_->pack_hidden_name(name, is_exported);
2260         }
2261       else
2262         {
2263           // This must be a type name.
2264           this->unget_token(Token::make_identifier_token(name, is_exported,
2265                                                          location));
2266           token = this->peek_token();
2267           name.clear();
2268         }
2269     }
2270
2271   // Here the receiver name is in NAME (it is empty if the receiver is
2272   // unnamed) and TOKEN is the first token in the type.
2273
2274   bool is_pointer = false;
2275   if (token->is_op(OPERATOR_MULT))
2276     {
2277       is_pointer = true;
2278       token = this->advance_token();
2279     }
2280
2281   if (!token->is_identifier())
2282     {
2283       error_at(this->location(), "expected receiver name or type");
2284       this->gogo_->mark_locals_used();
2285       int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2286       while (!token->is_eof())
2287         {
2288           token = this->advance_token();
2289           if (token->is_op(OPERATOR_LPAREN))
2290             ++c;
2291           else if (token->is_op(OPERATOR_RPAREN))
2292             {
2293               if (c == 0)
2294                 break;
2295               --c;
2296             }
2297         }
2298       if (!token->is_eof())
2299         this->advance_token();
2300       return NULL;
2301     }
2302
2303   Type* type = this->type_name(true);
2304
2305   if (is_pointer && !type->is_error_type())
2306     type = Type::make_pointer_type(type);
2307
2308   if (this->peek_token()->is_op(OPERATOR_RPAREN))
2309     this->advance_token();
2310   else
2311     {
2312       if (this->peek_token()->is_op(OPERATOR_COMMA))
2313         error_at(this->location(), "method has multiple receivers");
2314       else
2315         error_at(this->location(), "expected %<)%>");
2316       this->gogo_->mark_locals_used();
2317       while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2318         token = this->advance_token();
2319       if (!token->is_eof())
2320         this->advance_token();
2321       return NULL;
2322     }
2323
2324   return new Typed_identifier(name, type, location);
2325 }
2326
2327 // Operand    = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2328 // Literal    = BasicLit | CompositeLit | FunctionLit .
2329 // BasicLit   = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2330
2331 // If MAY_BE_SINK is true, this operand may be "_".
2332
2333 Expression*
2334 Parse::operand(bool may_be_sink)
2335 {
2336   const Token* token = this->peek_token();
2337   Expression* ret;
2338   switch (token->classification())
2339     {
2340     case Token::TOKEN_IDENTIFIER:
2341       {
2342         Location location = token->location();
2343         std::string id = token->identifier();
2344         bool is_exported = token->is_identifier_exported();
2345         std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2346
2347         Named_object* in_function;
2348         Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2349
2350         Package* package = NULL;
2351         if (named_object != NULL && named_object->is_package())
2352           {
2353             if (!this->advance_token()->is_op(OPERATOR_DOT)
2354                 || !this->advance_token()->is_identifier())
2355               {
2356                 error_at(location, "unexpected reference to package");
2357                 return Expression::make_error(location);
2358               }
2359             package = named_object->package_value();
2360             package->set_used();
2361             id = this->peek_token()->identifier();
2362             is_exported = this->peek_token()->is_identifier_exported();
2363             packed = this->gogo_->pack_hidden_name(id, is_exported);
2364             named_object = package->lookup(packed);
2365             location = this->location();
2366             go_assert(in_function == NULL);
2367           }
2368
2369         this->advance_token();
2370
2371         if (named_object != NULL
2372             && named_object->is_type()
2373             && !named_object->type_value()->is_visible())
2374           {
2375             go_assert(package != NULL);
2376             error_at(location, "invalid reference to hidden type %<%s.%s%>",
2377                      Gogo::message_name(package->name()).c_str(),
2378                      Gogo::message_name(id).c_str());
2379             return Expression::make_error(location);
2380           }
2381
2382
2383         if (named_object == NULL)
2384           {
2385             if (package != NULL)
2386               {
2387                 std::string n1 = Gogo::message_name(package->name());
2388                 std::string n2 = Gogo::message_name(id);
2389                 if (!is_exported)
2390                   error_at(location,
2391                            ("invalid reference to unexported identifier "
2392                             "%<%s.%s%>"),
2393                            n1.c_str(), n2.c_str());
2394                 else
2395                   error_at(location,
2396                            "reference to undefined identifier %<%s.%s%>",
2397                            n1.c_str(), n2.c_str());
2398                 return Expression::make_error(location);
2399               }
2400
2401             named_object = this->gogo_->add_unknown_name(packed, location);
2402           }
2403
2404         if (in_function != NULL
2405             && in_function != this->gogo_->current_function()
2406             && (named_object->is_variable()
2407                 || named_object->is_result_variable()))
2408           return this->enclosing_var_reference(in_function, named_object,
2409                                                location);
2410
2411         switch (named_object->classification())
2412           {
2413           case Named_object::NAMED_OBJECT_CONST:
2414             return Expression::make_const_reference(named_object, location);
2415           case Named_object::NAMED_OBJECT_TYPE:
2416             return Expression::make_type(named_object->type_value(), location);
2417           case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2418             {
2419               Type* t = Type::make_forward_declaration(named_object);
2420               return Expression::make_type(t, location);
2421             }
2422           case Named_object::NAMED_OBJECT_VAR:
2423           case Named_object::NAMED_OBJECT_RESULT_VAR:
2424             this->mark_var_used(named_object);
2425             return Expression::make_var_reference(named_object, location);
2426           case Named_object::NAMED_OBJECT_SINK:
2427             if (may_be_sink)
2428               return Expression::make_sink(location);
2429             else
2430               {
2431                 error_at(location, "cannot use _ as value");
2432                 return Expression::make_error(location);
2433               }
2434           case Named_object::NAMED_OBJECT_FUNC:
2435           case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2436             return Expression::make_func_reference(named_object, NULL,
2437                                                    location);
2438           case Named_object::NAMED_OBJECT_UNKNOWN:
2439             {
2440               Unknown_expression* ue =
2441                 Expression::make_unknown_reference(named_object, location);
2442               if (this->is_erroneous_function_)
2443                 ue->set_no_error_message();
2444               return ue;
2445             }
2446           case Named_object::NAMED_OBJECT_ERRONEOUS:
2447             return Expression::make_error(location);
2448           default:
2449             go_unreachable();
2450           }
2451       }
2452       go_unreachable();
2453
2454     case Token::TOKEN_STRING:
2455       ret = Expression::make_string(token->string_value(), token->location());
2456       this->advance_token();
2457       return ret;
2458
2459     case Token::TOKEN_CHARACTER:
2460       ret = Expression::make_character(token->character_value(), NULL,
2461                                        token->location());
2462       this->advance_token();
2463       return ret;
2464
2465     case Token::TOKEN_INTEGER:
2466       ret = Expression::make_integer(token->integer_value(), NULL,
2467                                      token->location());
2468       this->advance_token();
2469       return ret;
2470
2471     case Token::TOKEN_FLOAT:
2472       ret = Expression::make_float(token->float_value(), NULL,
2473                                    token->location());
2474       this->advance_token();
2475       return ret;
2476
2477     case Token::TOKEN_IMAGINARY:
2478       {
2479         mpfr_t zero;
2480         mpfr_init_set_ui(zero, 0, GMP_RNDN);
2481         ret = Expression::make_complex(&zero, token->imaginary_value(),
2482                                        NULL, token->location());
2483         mpfr_clear(zero);
2484         this->advance_token();
2485         return ret;
2486       }
2487
2488     case Token::TOKEN_KEYWORD:
2489       switch (token->keyword())
2490         {
2491         case KEYWORD_FUNC:
2492           return this->function_lit();
2493         case KEYWORD_CHAN:
2494         case KEYWORD_INTERFACE:
2495         case KEYWORD_MAP:
2496         case KEYWORD_STRUCT:
2497           {
2498             Location location = token->location();
2499             return Expression::make_type(this->type(), location);
2500           }
2501         default:
2502           break;
2503         }
2504       break;
2505
2506     case Token::TOKEN_OPERATOR:
2507       if (token->is_op(OPERATOR_LPAREN))
2508         {
2509           this->advance_token();
2510           ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL);
2511           if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2512             error_at(this->location(), "missing %<)%>");
2513           else
2514             this->advance_token();
2515           return ret;
2516         }
2517       else if (token->is_op(OPERATOR_LSQUARE))
2518         {
2519           // Here we call array_type directly, as this is the only
2520           // case where an ellipsis is permitted for an array type.
2521           Location location = token->location();
2522           return Expression::make_type(this->array_type(true), location);
2523         }
2524       break;
2525
2526     default:
2527       break;
2528     }
2529
2530   error_at(this->location(), "expected operand");
2531   return Expression::make_error(this->location());
2532 }
2533
2534 // Handle a reference to a variable in an enclosing function.  We add
2535 // it to a list of such variables.  We return a reference to a field
2536 // in a struct which will be passed on the static chain when calling
2537 // the current function.
2538
2539 Expression*
2540 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2541                                Location location)
2542 {
2543   go_assert(var->is_variable() || var->is_result_variable());
2544
2545   this->mark_var_used(var);
2546
2547   Named_object* this_function = this->gogo_->current_function();
2548   Named_object* closure = this_function->func_value()->closure_var();
2549
2550   Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2551   std::pair<Enclosing_vars::iterator, bool> ins =
2552     this->enclosing_vars_.insert(ev);
2553   if (ins.second)
2554     {
2555       // This is a variable we have not seen before.  Add a new field
2556       // to the closure type.
2557       this_function->func_value()->add_closure_field(var, location);
2558     }
2559
2560   Expression* closure_ref = Expression::make_var_reference(closure,
2561                                                            location);
2562   closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2563
2564   // The closure structure holds pointers to the variables, so we need
2565   // to introduce an indirection.
2566   Expression* e = Expression::make_field_reference(closure_ref,
2567                                                    ins.first->index(),
2568                                                    location);
2569   e = Expression::make_unary(OPERATOR_MULT, e, location);
2570   return e;
2571 }
2572
2573 // CompositeLit  = LiteralType LiteralValue .
2574 // LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
2575 //                 SliceType | MapType | TypeName .
2576 // LiteralValue  = "{" [ ElementList [ "," ] ] "}" .
2577 // ElementList   = Element { "," Element } .
2578 // Element       = [ Key ":" ] Value .
2579 // Key           = FieldName | ElementIndex .
2580 // FieldName     = identifier .
2581 // ElementIndex  = Expression .
2582 // Value         = Expression | LiteralValue .
2583
2584 // We have already seen the type if there is one, and we are now
2585 // looking at the LiteralValue.  The case "[" "..."  "]" ElementType
2586 // will be seen here as an array type whose length is "nil".  The
2587 // DEPTH parameter is non-zero if this is an embedded composite
2588 // literal and the type was omitted.  It gives the number of steps up
2589 // to the type which was provided.  E.g., in [][]int{{1}} it will be
2590 // 1.  In [][][]int{{{1}}} it will be 2.
2591
2592 Expression*
2593 Parse::composite_lit(Type* type, int depth, Location location)
2594 {
2595   go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2596   this->advance_token();
2597
2598   if (this->peek_token()->is_op(OPERATOR_RCURLY))
2599     {
2600       this->advance_token();
2601       return Expression::make_composite_literal(type, depth, false, NULL,
2602                                                 location);
2603     }
2604
2605   bool has_keys = false;
2606   Expression_list* vals = new Expression_list;
2607   while (true)
2608     {
2609       Expression* val;
2610       bool is_type_omitted = false;
2611
2612       const Token* token = this->peek_token();
2613
2614       if (token->is_identifier())
2615         {
2616           std::string identifier = token->identifier();
2617           bool is_exported = token->is_identifier_exported();
2618           Location location = token->location();
2619
2620           if (this->advance_token()->is_op(OPERATOR_COLON))
2621             {
2622               // This may be a field name.  We don't know for sure--it
2623               // could also be an expression for an array index.  We
2624               // don't want to parse it as an expression because may
2625               // trigger various errors, e.g., if this identifier
2626               // happens to be the name of a package.
2627               Gogo* gogo = this->gogo_;
2628               val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2629                                                                   is_exported),
2630                                            location);
2631             }
2632           else
2633             {
2634               this->unget_token(Token::make_identifier_token(identifier,
2635                                                              is_exported,
2636                                                              location));
2637               val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2638             }
2639         }
2640       else if (!token->is_op(OPERATOR_LCURLY))
2641         val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2642       else
2643         {
2644           // This must be a composite literal inside another composite
2645           // literal, with the type omitted for the inner one.
2646           val = this->composite_lit(type, depth + 1, token->location());
2647           is_type_omitted = true;
2648         }
2649
2650       token = this->peek_token();
2651       if (!token->is_op(OPERATOR_COLON))
2652         {
2653           if (has_keys)
2654             vals->push_back(NULL);
2655         }
2656       else
2657         {
2658           if (is_type_omitted && !val->is_error_expression())
2659             {
2660               error_at(this->location(), "unexpected %<:%>");
2661               val = Expression::make_error(this->location());
2662             }
2663
2664           this->advance_token();
2665
2666           if (!has_keys && !vals->empty())
2667             {
2668               Expression_list* newvals = new Expression_list;
2669               for (Expression_list::const_iterator p = vals->begin();
2670                    p != vals->end();
2671                    ++p)
2672                 {
2673                   newvals->push_back(NULL);
2674                   newvals->push_back(*p);
2675                 }
2676               delete vals;
2677               vals = newvals;
2678             }
2679           has_keys = true;
2680
2681           if (val->unknown_expression() != NULL)
2682             val->unknown_expression()->set_is_composite_literal_key();
2683
2684           vals->push_back(val);
2685
2686           if (!token->is_op(OPERATOR_LCURLY))
2687             val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2688           else
2689             {
2690               // This must be a composite literal inside another
2691               // composite literal, with the type omitted for the
2692               // inner one.
2693               val = this->composite_lit(type, depth + 1, token->location());
2694             }
2695
2696           token = this->peek_token();
2697         }
2698
2699       vals->push_back(val);
2700
2701       if (token->is_op(OPERATOR_COMMA))
2702         {
2703           if (this->advance_token()->is_op(OPERATOR_RCURLY))
2704             {
2705               this->advance_token();
2706               break;
2707             }
2708         }
2709       else if (token->is_op(OPERATOR_RCURLY))
2710         {
2711           this->advance_token();
2712           break;
2713         }
2714       else
2715         {
2716           error_at(this->location(), "expected %<,%> or %<}%>");
2717
2718           this->gogo_->mark_locals_used();
2719           int depth = 0;
2720           while (!token->is_eof()
2721                  && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2722             {
2723               if (token->is_op(OPERATOR_LCURLY))
2724                 ++depth;
2725               else if (token->is_op(OPERATOR_RCURLY))
2726                 --depth;
2727               token = this->advance_token();
2728             }
2729           if (token->is_op(OPERATOR_RCURLY))
2730             this->advance_token();
2731
2732           return Expression::make_error(location);
2733         }
2734     }
2735
2736   return Expression::make_composite_literal(type, depth, has_keys, vals,
2737                                             location);
2738 }
2739
2740 // FunctionLit = "func" Signature Block .
2741
2742 Expression*
2743 Parse::function_lit()
2744 {
2745   Location location = this->location();
2746   go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2747   this->advance_token();
2748
2749   Enclosing_vars hold_enclosing_vars;
2750   hold_enclosing_vars.swap(this->enclosing_vars_);
2751
2752   Function_type* type = this->signature(NULL, location);
2753   bool fntype_is_error = false;
2754   if (type == NULL)
2755     {
2756       type = Type::make_function_type(NULL, NULL, NULL, location);
2757       fntype_is_error = true;
2758     }
2759
2760   // For a function literal, the next token must be a '{'.  If we
2761   // don't see that, then we may have a type expression.
2762   if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2763     return Expression::make_type(type, location);
2764
2765   bool hold_is_erroneous_function = this->is_erroneous_function_;
2766   if (fntype_is_error)
2767     this->is_erroneous_function_ = true;
2768
2769   Bc_stack* hold_break_stack = this->break_stack_;
2770   Bc_stack* hold_continue_stack = this->continue_stack_;
2771   this->break_stack_ = NULL;
2772   this->continue_stack_ = NULL;
2773
2774   Named_object* no = this->gogo_->start_function("", type, true, location);
2775
2776   Location end_loc = this->block();
2777
2778   this->gogo_->finish_function(end_loc);
2779
2780   if (this->break_stack_ != NULL)
2781     delete this->break_stack_;
2782   if (this->continue_stack_ != NULL)
2783     delete this->continue_stack_;
2784   this->break_stack_ = hold_break_stack;
2785   this->continue_stack_ = hold_continue_stack;
2786
2787   this->is_erroneous_function_ = hold_is_erroneous_function;
2788
2789   hold_enclosing_vars.swap(this->enclosing_vars_);
2790
2791   Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2792                                              location);
2793
2794   return Expression::make_func_reference(no, closure, location);
2795 }
2796
2797 // Create a closure for the nested function FUNCTION.  This is based
2798 // on ENCLOSING_VARS, which is a list of all variables defined in
2799 // enclosing functions and referenced from FUNCTION.  A closure is the
2800 // address of a struct which contains the addresses of all the
2801 // referenced variables.  This returns NULL if no closure is required.
2802
2803 Expression*
2804 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2805                       Location location)
2806 {
2807   if (enclosing_vars->empty())
2808     return NULL;
2809
2810   // Get the variables in order by their field index.
2811
2812   size_t enclosing_var_count = enclosing_vars->size();
2813   std::vector<Enclosing_var> ev(enclosing_var_count);
2814   for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2815        p != enclosing_vars->end();
2816        ++p)
2817     ev[p->index()] = *p;
2818
2819   // Build an initializer for a composite literal of the closure's
2820   // type.
2821
2822   Named_object* enclosing_function = this->gogo_->current_function();
2823   Expression_list* initializer = new Expression_list;
2824   for (size_t i = 0; i < enclosing_var_count; ++i)
2825     {
2826       go_assert(ev[i].index() == i);
2827       Named_object* var = ev[i].var();
2828       Expression* ref;
2829       if (ev[i].in_function() == enclosing_function)
2830         ref = Expression::make_var_reference(var, location);
2831       else
2832         ref = this->enclosing_var_reference(ev[i].in_function(), var,
2833                                             location);
2834       Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2835                                                    location);
2836       initializer->push_back(refaddr);
2837     }
2838
2839   Named_object* closure_var = function->func_value()->closure_var();
2840   Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2841   Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2842                                                              location);
2843   return Expression::make_heap_composite(cv, location);
2844 }
2845
2846 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2847
2848 // If MAY_BE_SINK is true, this expression may be "_".
2849
2850 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2851 // literal.
2852
2853 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2854 // guard (var := expr.("type") using the literal keyword "type").
2855
2856 Expression*
2857 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2858                     bool* is_type_switch)
2859 {
2860   Location start_loc = this->location();
2861   bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2862
2863   Expression* ret = this->operand(may_be_sink);
2864
2865   // An unknown name followed by a curly brace must be a composite
2866   // literal, and the unknown name must be a type.
2867   if (may_be_composite_lit
2868       && !is_parenthesized
2869       && ret->unknown_expression() != NULL
2870       && this->peek_token()->is_op(OPERATOR_LCURLY))
2871     {
2872       Named_object* no = ret->unknown_expression()->named_object();
2873       Type* type = Type::make_forward_declaration(no);
2874       ret = Expression::make_type(type, ret->location());
2875     }
2876
2877   // We handle composite literals and type casts here, as it is the
2878   // easiest way to handle types which are in parentheses, as in
2879   // "((uint))(1)".
2880   if (ret->is_type_expression())
2881     {
2882       if (this->peek_token()->is_op(OPERATOR_LCURLY))
2883         {
2884           if (!may_be_composite_lit)
2885             {
2886               Type* t = ret->type();
2887               if (t->named_type() != NULL
2888                   || t->forward_declaration_type() != NULL)
2889                 error_at(start_loc,
2890                          _("parentheses required around this composite literal"
2891                            "to avoid parsing ambiguity"));
2892             }
2893           else if (is_parenthesized)
2894             error_at(start_loc,
2895                      "cannot parenthesize type in composite literal");
2896           ret = this->composite_lit(ret->type(), 0, ret->location());
2897         }
2898       else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2899         {
2900           Location loc = this->location();
2901           this->advance_token();
2902           Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2903                                               NULL);
2904           if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2905             {
2906               error_at(this->location(),
2907                        "invalid use of %<...%> in type conversion");
2908               this->advance_token();
2909             }
2910           if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2911             error_at(this->location(), "expected %<)%>");
2912           else
2913             this->advance_token();
2914           if (expr->is_error_expression())
2915             ret = expr;
2916           else
2917             {
2918               Type* t = ret->type();
2919               if (t->classification() == Type::TYPE_ARRAY
2920                   && t->array_type()->length() != NULL
2921                   && t->array_type()->length()->is_nil_expression())
2922                 {
2923                   error_at(ret->location(),
2924                            "invalid use of %<...%> in type conversion");
2925                   ret = Expression::make_error(loc);
2926                 }
2927               else
2928                 ret = Expression::make_cast(t, expr, loc);
2929             }
2930         }
2931     }
2932
2933   while (true)
2934     {
2935       const Token* token = this->peek_token();
2936       if (token->is_op(OPERATOR_LPAREN))
2937         ret = this->call(this->verify_not_sink(ret));
2938       else if (token->is_op(OPERATOR_DOT))
2939         {
2940           ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2941           if (is_type_switch != NULL && *is_type_switch)
2942             break;
2943         }
2944       else if (token->is_op(OPERATOR_LSQUARE))
2945         ret = this->index(this->verify_not_sink(ret));
2946       else
2947         break;
2948     }
2949
2950   return ret;
2951 }
2952
2953 // Selector = "." identifier .
2954 // TypeGuard = "." "(" QualifiedIdent ")" .
2955
2956 // Note that Operand can expand to QualifiedIdent, which contains a
2957 // ".".  That is handled directly in operand when it sees a package
2958 // name.
2959
2960 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2961 // guard (var := expr.("type") using the literal keyword "type").
2962
2963 Expression*
2964 Parse::selector(Expression* left, bool* is_type_switch)
2965 {
2966   go_assert(this->peek_token()->is_op(OPERATOR_DOT));
2967   Location location = this->location();
2968
2969   const Token* token = this->advance_token();
2970   if (token->is_identifier())
2971     {
2972       // This could be a field in a struct, or a method in an
2973       // interface, or a method associated with a type.  We can't know
2974       // which until we have seen all the types.
2975       std::string name =
2976         this->gogo_->pack_hidden_name(token->identifier(),
2977                                       token->is_identifier_exported());
2978       if (token->identifier() == "_")
2979         {
2980           error_at(this->location(), "invalid use of %<_%>");
2981           name = this->gogo_->pack_hidden_name("blank", false);
2982         }
2983       this->advance_token();
2984       return Expression::make_selector(left, name, location);
2985     }
2986   else if (token->is_op(OPERATOR_LPAREN))
2987     {
2988       this->advance_token();
2989       Type* type = NULL;
2990       if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
2991         type = this->type();
2992       else
2993         {
2994           if (is_type_switch != NULL)
2995             *is_type_switch = true;
2996           else
2997             {
2998               error_at(this->location(),
2999                        "use of %<.(type)%> outside type switch");
3000               type = Type::make_error_type();
3001             }
3002           this->advance_token();
3003         }
3004       if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3005         error_at(this->location(), "missing %<)%>");
3006       else
3007         this->advance_token();
3008       if (is_type_switch != NULL && *is_type_switch)
3009         return left;
3010       return Expression::make_type_guard(left, type, location);
3011     }
3012   else
3013     {
3014       error_at(this->location(), "expected identifier or %<(%>");
3015       return left;
3016     }
3017 }
3018
3019 // Index          = "[" Expression "]" .
3020 // Slice          = "[" Expression ":" [ Expression ] "]" .
3021
3022 Expression*
3023 Parse::index(Expression* expr)
3024 {
3025   Location location = this->location();
3026   go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
3027   this->advance_token();
3028
3029   Expression* start;
3030   if (!this->peek_token()->is_op(OPERATOR_COLON))
3031     start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3032   else
3033     {
3034       mpz_t zero;
3035       mpz_init_set_ui(zero, 0);
3036       start = Expression::make_integer(&zero, NULL, location);
3037       mpz_clear(zero);
3038     }
3039
3040   Expression* end = NULL;
3041   if (this->peek_token()->is_op(OPERATOR_COLON))
3042     {
3043       // We use nil to indicate a missing high expression.
3044       if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3045         end = Expression::make_nil(this->location());
3046       else
3047         end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3048     }
3049   if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
3050     error_at(this->location(), "missing %<]%>");
3051   else
3052     this->advance_token();
3053   return Expression::make_index(expr, start, end, location);
3054 }
3055
3056 // Call           = "(" [ ArgumentList [ "," ] ] ")" .
3057 // ArgumentList   = ExpressionList [ "..." ] .
3058
3059 Expression*
3060 Parse::call(Expression* func)
3061 {
3062   go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
3063   Expression_list* args = NULL;
3064   bool is_varargs = false;
3065   const Token* token = this->advance_token();
3066   if (!token->is_op(OPERATOR_RPAREN))
3067     {
3068       args = this->expression_list(NULL, false);
3069       token = this->peek_token();
3070       if (token->is_op(OPERATOR_ELLIPSIS))
3071         {
3072           is_varargs = true;
3073           token = this->advance_token();
3074         }
3075     }
3076   if (token->is_op(OPERATOR_COMMA))
3077     token = this->advance_token();
3078   if (!token->is_op(OPERATOR_RPAREN))
3079     error_at(this->location(), "missing %<)%>");
3080   else
3081     this->advance_token();
3082   if (func->is_error_expression())
3083     return func;
3084   return Expression::make_call(func, args, is_varargs, func->location());
3085 }
3086
3087 // Return an expression for a single unqualified identifier.
3088
3089 Expression*
3090 Parse::id_to_expression(const std::string& name, Location location)
3091 {
3092   Named_object* in_function;
3093   Named_object* named_object = this->gogo_->lookup(name, &in_function);
3094   if (named_object == NULL)
3095     named_object = this->gogo_->add_unknown_name(name, location);
3096
3097   if (in_function != NULL
3098       && in_function != this->gogo_->current_function()
3099       && (named_object->is_variable() || named_object->is_result_variable()))
3100     return this->enclosing_var_reference(in_function, named_object,
3101                                          location);
3102
3103   switch (named_object->classification())
3104     {
3105     case Named_object::NAMED_OBJECT_CONST:
3106       return Expression::make_const_reference(named_object, location);
3107     case Named_object::NAMED_OBJECT_VAR:
3108     case Named_object::NAMED_OBJECT_RESULT_VAR:
3109       this->mark_var_used(named_object);
3110       return Expression::make_var_reference(named_object, location);
3111     case Named_object::NAMED_OBJECT_SINK:
3112       return Expression::make_sink(location);
3113     case Named_object::NAMED_OBJECT_FUNC:
3114     case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3115       return Expression::make_func_reference(named_object, NULL, location);
3116     case Named_object::NAMED_OBJECT_UNKNOWN:
3117       {
3118         Unknown_expression* ue =
3119           Expression::make_unknown_reference(named_object, location);
3120         if (this->is_erroneous_function_)
3121           ue->set_no_error_message();
3122         return ue;
3123       }
3124     case Named_object::NAMED_OBJECT_PACKAGE:
3125     case Named_object::NAMED_OBJECT_TYPE:
3126     case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3127       {
3128         // These cases can arise for a field name in a composite
3129         // literal.
3130         Unknown_expression* ue =
3131           Expression::make_unknown_reference(named_object, location);
3132         if (this->is_erroneous_function_)
3133           ue->set_no_error_message();
3134         return ue;
3135       }
3136     case Named_object::NAMED_OBJECT_ERRONEOUS:
3137       return Expression::make_error(location);
3138     default:
3139       error_at(this->location(), "unexpected type of identifier");
3140       return Expression::make_error(location);
3141     }
3142 }
3143
3144 // Expression = UnaryExpr { binary_op Expression } .
3145
3146 // PRECEDENCE is the precedence of the current operator.
3147
3148 // If MAY_BE_SINK is true, this expression may be "_".
3149
3150 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3151 // literal.
3152
3153 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3154 // guard (var := expr.("type") using the literal keyword "type").
3155
3156 Expression*
3157 Parse::expression(Precedence precedence, bool may_be_sink,
3158                   bool may_be_composite_lit, bool* is_type_switch)
3159 {
3160   Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3161                                       is_type_switch);
3162
3163   while (true)
3164     {
3165       if (is_type_switch != NULL && *is_type_switch)
3166         return left;
3167
3168       const Token* token = this->peek_token();
3169       if (token->classification() != Token::TOKEN_OPERATOR)
3170         {
3171           // Not a binary_op.
3172           return left;
3173         }
3174
3175       Precedence right_precedence;
3176       switch (token->op())
3177         {
3178         case OPERATOR_OROR:
3179           right_precedence = PRECEDENCE_OROR;
3180           break;
3181         case OPERATOR_ANDAND:
3182           right_precedence = PRECEDENCE_ANDAND;
3183           break;
3184         case OPERATOR_EQEQ:
3185         case OPERATOR_NOTEQ:
3186         case OPERATOR_LT:
3187         case OPERATOR_LE:
3188         case OPERATOR_GT:
3189         case OPERATOR_GE:
3190           right_precedence = PRECEDENCE_RELOP;
3191           break;
3192         case OPERATOR_PLUS:
3193         case OPERATOR_MINUS:
3194         case OPERATOR_OR:
3195         case OPERATOR_XOR:
3196           right_precedence = PRECEDENCE_ADDOP;
3197           break;
3198         case OPERATOR_MULT:
3199         case OPERATOR_DIV:
3200         case OPERATOR_MOD:
3201         case OPERATOR_LSHIFT:
3202         case OPERATOR_RSHIFT:
3203         case OPERATOR_AND:
3204         case OPERATOR_BITCLEAR:
3205           right_precedence = PRECEDENCE_MULOP;
3206           break;
3207         default:
3208           right_precedence = PRECEDENCE_INVALID;
3209           break;
3210         }
3211
3212       if (right_precedence == PRECEDENCE_INVALID)
3213         {
3214           // Not a binary_op.
3215           return left;
3216         }
3217
3218       Operator op = token->op();
3219       Location binop_location = token->location();
3220
3221       if (precedence >= right_precedence)
3222         {
3223           // We've already seen A * B, and we see + C.  We want to
3224           // return so that A * B becomes a group.
3225           return left;
3226         }
3227
3228       this->advance_token();
3229
3230       left = this->verify_not_sink(left);
3231       Expression* right = this->expression(right_precedence, false,
3232                                            may_be_composite_lit,
3233                                            NULL);
3234       left = Expression::make_binary(op, left, right, binop_location);
3235     }
3236 }
3237
3238 bool
3239 Parse::expression_may_start_here()
3240 {
3241   const Token* token = this->peek_token();
3242   switch (token->classification())
3243     {
3244     case Token::TOKEN_INVALID:
3245     case Token::TOKEN_EOF:
3246       return false;
3247     case Token::TOKEN_KEYWORD:
3248       switch (token->keyword())
3249         {
3250         case KEYWORD_CHAN:
3251         case KEYWORD_FUNC:
3252         case KEYWORD_MAP:
3253         case KEYWORD_STRUCT:
3254         case KEYWORD_INTERFACE:
3255           return true;
3256         default:
3257           return false;
3258         }
3259     case Token::TOKEN_IDENTIFIER:
3260       return true;
3261     case Token::TOKEN_STRING:
3262       return true;
3263     case Token::TOKEN_OPERATOR:
3264       switch (token->op())
3265         {
3266         case OPERATOR_PLUS:
3267         case OPERATOR_MINUS:
3268         case OPERATOR_NOT:
3269         case OPERATOR_XOR:
3270         case OPERATOR_MULT:
3271         case OPERATOR_CHANOP:
3272         case OPERATOR_AND:
3273         case OPERATOR_LPAREN:
3274         case OPERATOR_LSQUARE:
3275           return true;
3276         default:
3277           return false;
3278         }
3279     case Token::TOKEN_CHARACTER:
3280     case Token::TOKEN_INTEGER:
3281     case Token::TOKEN_FLOAT:
3282     case Token::TOKEN_IMAGINARY:
3283       return true;
3284     default:
3285       go_unreachable();
3286     }
3287 }
3288
3289 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3290
3291 // If MAY_BE_SINK is true, this expression may be "_".
3292
3293 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3294 // literal.
3295
3296 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3297 // guard (var := expr.("type") using the literal keyword "type").
3298
3299 Expression*
3300 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3301                   bool* is_type_switch)
3302 {
3303   const Token* token = this->peek_token();
3304   if (token->is_op(OPERATOR_PLUS)
3305       || token->is_op(OPERATOR_MINUS)
3306       || token->is_op(OPERATOR_NOT)
3307       || token->is_op(OPERATOR_XOR)
3308       || token->is_op(OPERATOR_CHANOP)
3309       || token->is_op(OPERATOR_MULT)
3310       || token->is_op(OPERATOR_AND))
3311     {
3312       Location location = token->location();
3313       Operator op = token->op();
3314       this->advance_token();
3315
3316       if (op == OPERATOR_CHANOP
3317           && this->peek_token()->is_keyword(KEYWORD_CHAN))
3318         {
3319           // This is "<- chan" which must be the start of a type.
3320           this->unget_token(Token::make_operator_token(op, location));
3321           return Expression::make_type(this->type(), location);
3322         }
3323
3324       Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
3325       if (expr->is_error_expression())
3326         ;
3327       else if (op == OPERATOR_MULT && expr->is_type_expression())
3328         expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3329                                      location);
3330       else if (op == OPERATOR_AND && expr->is_composite_literal())
3331         expr = Expression::make_heap_composite(expr, location);
3332       else if (op != OPERATOR_CHANOP)
3333         expr = Expression::make_unary(op, expr, location);
3334       else
3335         expr = Expression::make_receive(expr, location);
3336       return expr;
3337     }
3338   else
3339     return this->primary_expr(may_be_sink, may_be_composite_lit,
3340                               is_type_switch);
3341 }
3342
3343 // Statement =
3344 //      Declaration | LabeledStmt | SimpleStmt |
3345 //      GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3346 //      FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3347 //      DeferStmt .
3348
3349 // LABEL is the label of this statement if it has one.
3350
3351 void
3352 Parse::statement(Label* label)
3353 {
3354   const Token* token = this->peek_token();
3355   switch (token->classification())
3356     {
3357     case Token::TOKEN_KEYWORD:
3358       {
3359         switch (token->keyword())
3360           {
3361           case KEYWORD_CONST:
3362           case KEYWORD_TYPE:
3363           case KEYWORD_VAR:
3364             this->declaration();
3365             break;
3366           case KEYWORD_FUNC:
3367           case KEYWORD_MAP:
3368           case KEYWORD_STRUCT:
3369           case KEYWORD_INTERFACE:
3370             this->simple_stat(true, NULL, NULL, NULL);
3371             break;
3372           case KEYWORD_GO:
3373           case KEYWORD_DEFER:
3374             this->go_or_defer_stat();
3375             break;
3376           case KEYWORD_RETURN:
3377             this->return_stat();
3378             break;
3379           case KEYWORD_BREAK:
3380             this->break_stat();
3381             break;
3382           case KEYWORD_CONTINUE:
3383             this->continue_stat();
3384             break;
3385           case KEYWORD_GOTO:
3386             this->goto_stat();
3387             break;
3388           case KEYWORD_IF:
3389             this->if_stat();
3390             break;
3391           case KEYWORD_SWITCH:
3392             this->switch_stat(label);
3393             break;
3394           case KEYWORD_SELECT:
3395             this->select_stat(label);
3396             break;
3397           case KEYWORD_FOR:
3398             this->for_stat(label);
3399             break;
3400           default:
3401             error_at(this->location(), "expected statement");
3402             this->advance_token();
3403             break;
3404           }
3405       }
3406       break;
3407
3408     case Token::TOKEN_IDENTIFIER:
3409       {
3410         std::string identifier = token->identifier();
3411         bool is_exported = token->is_identifier_exported();
3412         Location location = token->location();
3413         if (this->advance_token()->is_op(OPERATOR_COLON))
3414           {
3415             this->advance_token();
3416             this->labeled_stmt(identifier, location);
3417           }
3418         else
3419           {
3420             this->unget_token(Token::make_identifier_token(identifier,
3421                                                            is_exported,
3422                                                            location));
3423             this->simple_stat(true, NULL, NULL, NULL);
3424           }
3425       }
3426       break;
3427
3428     case Token::TOKEN_OPERATOR:
3429       if (token->is_op(OPERATOR_LCURLY))
3430         {
3431           Location location = token->location();
3432           this->gogo_->start_block(location);
3433           Location end_loc = this->block();
3434           this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3435                                  location);
3436         }
3437       else if (!token->is_op(OPERATOR_SEMICOLON))
3438         this->simple_stat(true, NULL, NULL, NULL);
3439       break;
3440
3441     case Token::TOKEN_STRING:
3442     case Token::TOKEN_CHARACTER:
3443     case Token::TOKEN_INTEGER:
3444     case Token::TOKEN_FLOAT:
3445     case Token::TOKEN_IMAGINARY:
3446       this->simple_stat(true, NULL, NULL, NULL);
3447       break;
3448
3449     default:
3450       error_at(this->location(), "expected statement");
3451       this->advance_token();
3452       break;
3453     }
3454 }
3455
3456 bool
3457 Parse::statement_may_start_here()
3458 {
3459   const Token* token = this->peek_token();
3460   switch (token->classification())
3461     {
3462     case Token::TOKEN_KEYWORD:
3463       {
3464         switch (token->keyword())
3465           {
3466           case KEYWORD_CONST:
3467           case KEYWORD_TYPE:
3468           case KEYWORD_VAR:
3469           case KEYWORD_FUNC:
3470           case KEYWORD_MAP:
3471           case KEYWORD_STRUCT:
3472           case KEYWORD_INTERFACE:
3473           case KEYWORD_GO:
3474           case KEYWORD_DEFER:
3475           case KEYWORD_RETURN:
3476           case KEYWORD_BREAK:
3477           case KEYWORD_CONTINUE:
3478           case KEYWORD_GOTO:
3479           case KEYWORD_IF:
3480           case KEYWORD_SWITCH:
3481           case KEYWORD_SELECT:
3482           case KEYWORD_FOR:
3483             return true;
3484
3485           default:
3486             return false;
3487           }
3488       }
3489       break;
3490
3491     case Token::TOKEN_IDENTIFIER:
3492       return true;
3493
3494     case Token::TOKEN_OPERATOR:
3495       if (token->is_op(OPERATOR_LCURLY)
3496           || token->is_op(OPERATOR_SEMICOLON))
3497         return true;
3498       else
3499         return this->expression_may_start_here();
3500
3501     case Token::TOKEN_STRING:
3502     case Token::TOKEN_CHARACTER:
3503     case Token::TOKEN_INTEGER:
3504     case Token::TOKEN_FLOAT:
3505     case Token::TOKEN_IMAGINARY:
3506       return true;
3507
3508     default:
3509       return false;
3510     }
3511 }
3512
3513 // LabeledStmt = Label ":" Statement .
3514 // Label       = identifier .
3515
3516 void
3517 Parse::labeled_stmt(const std::string& label_name, Location location)
3518 {
3519   Label* label = this->gogo_->add_label_definition(label_name, location);
3520
3521   if (this->peek_token()->is_op(OPERATOR_RCURLY))
3522     {
3523       // This is a label at the end of a block.  A program is
3524       // permitted to omit a semicolon here.
3525       return;
3526     }
3527
3528   if (!this->statement_may_start_here())
3529     {
3530       // Mark the label as used to avoid a useless error about an
3531       // unused label.
3532       label->set_is_used();
3533
3534       error_at(location, "missing statement after label");
3535       this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3536                                                    location));
3537       return;
3538     }
3539
3540   this->statement(label);
3541 }
3542
3543 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3544 //      Assignment | ShortVarDecl .
3545
3546 // EmptyStmt was handled in Parse::statement.
3547
3548 // In order to make this work for if and switch statements, if
3549 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3550 // expression rather than adding an expression statement to the
3551 // current block.  If we see something other than an ExpressionStat,
3552 // we add the statement, set *RETURN_EXP to true if we saw a send
3553 // statement, and return NULL.  The handling of send statements is for
3554 // better error messages.
3555
3556 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3557 // RangeClause.
3558
3559 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3560 // guard (var := expr.("type") using the literal keyword "type").
3561
3562 Expression*
3563 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3564                    Range_clause* p_range_clause, Type_switch* p_type_switch)
3565 {
3566   const Token* token = this->peek_token();
3567
3568   // An identifier follow by := is a SimpleVarDecl.
3569   if (token->is_identifier())
3570     {
3571       std::string identifier = token->identifier();
3572       bool is_exported = token->is_identifier_exported();
3573       Location location = token->location();
3574
3575       token = this->advance_token();
3576       if (token->is_op(OPERATOR_COLONEQ)
3577           || token->is_op(OPERATOR_COMMA))
3578         {
3579           identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3580           this->simple_var_decl_or_assignment(identifier, location,
3581                                               p_range_clause,
3582                                               (token->is_op(OPERATOR_COLONEQ)
3583                                                ? p_type_switch
3584                                                : NULL));
3585           return NULL;
3586         }
3587
3588       this->unget_token(Token::make_identifier_token(identifier, is_exported,
3589                                                      location));
3590     }
3591
3592   Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3593                                      may_be_composite_lit,
3594                                      (p_type_switch == NULL
3595                                       ? NULL
3596                                       : &p_type_switch->found));
3597   if (p_type_switch != NULL && p_type_switch->found)
3598     {
3599       p_type_switch->name.clear();
3600       p_type_switch->location = exp->location();
3601       p_type_switch->expr = this->verify_not_sink(exp);
3602       return NULL;
3603     }
3604   token = this->peek_token();
3605   if (token->is_op(OPERATOR_CHANOP))
3606     {
3607       this->send_stmt(this->verify_not_sink(exp));
3608       if (return_exp != NULL)
3609         *return_exp = true;
3610     }
3611   else if (token->is_op(OPERATOR_PLUSPLUS)
3612            || token->is_op(OPERATOR_MINUSMINUS))
3613     this->inc_dec_stat(this->verify_not_sink(exp));
3614   else if (token->is_op(OPERATOR_COMMA)
3615            || token->is_op(OPERATOR_EQ))
3616     this->assignment(exp, p_range_clause);
3617   else if (token->is_op(OPERATOR_PLUSEQ)
3618            || token->is_op(OPERATOR_MINUSEQ)
3619            || token->is_op(OPERATOR_OREQ)
3620            || token->is_op(OPERATOR_XOREQ)
3621            || token->is_op(OPERATOR_MULTEQ)
3622            || token->is_op(OPERATOR_DIVEQ)
3623            || token->is_op(OPERATOR_MODEQ)
3624            || token->is_op(OPERATOR_LSHIFTEQ)
3625            || token->is_op(OPERATOR_RSHIFTEQ)
3626            || token->is_op(OPERATOR_ANDEQ)
3627            || token->is_op(OPERATOR_BITCLEAREQ))
3628     this->assignment(this->verify_not_sink(exp), p_range_clause);
3629   else if (return_exp != NULL)
3630     return this->verify_not_sink(exp);
3631   else
3632     {
3633       exp = this->verify_not_sink(exp);
3634
3635       if (token->is_op(OPERATOR_COLONEQ))
3636         {
3637           if (!exp->is_error_expression())
3638             error_at(token->location(), "non-name on left side of %<:=%>");
3639           this->gogo_->mark_locals_used();
3640           while (!token->is_op(OPERATOR_SEMICOLON)
3641                  && !token->is_eof())
3642             token = this->advance_token();
3643           return NULL;
3644         }
3645
3646       this->expression_stat(exp);
3647     }
3648
3649   return NULL;
3650 }
3651
3652 bool
3653 Parse::simple_stat_may_start_here()
3654 {
3655   return this->expression_may_start_here();
3656 }
3657
3658 // Parse { Statement ";" } which is used in a few places.  The list of
3659 // statements may end with a right curly brace, in which case the
3660 // semicolon may be omitted.
3661
3662 void
3663 Parse::statement_list()
3664 {
3665   while (this->statement_may_start_here())
3666     {
3667       this->statement(NULL);
3668       if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3669         this->advance_token();
3670       else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3671         break;
3672       else
3673         {
3674           if (!this->peek_token()->is_eof() || !saw_errors())
3675             error_at(this->location(), "expected %<;%> or %<}%> or newline");
3676           if (!this->skip_past_error(OPERATOR_RCURLY))
3677             return;
3678         }
3679     }
3680 }
3681
3682 bool
3683 Parse::statement_list_may_start_here()
3684 {
3685   return this->statement_may_start_here();
3686 }
3687
3688 // ExpressionStat = Expression .
3689
3690 void
3691 Parse::expression_stat(Expression* exp)
3692 {
3693   this->gogo_->add_statement(Statement::make_statement(exp, false));
3694 }
3695
3696 // SendStmt = Channel "&lt;-" Expression .
3697 // Channel  = Expression .
3698
3699 void
3700 Parse::send_stmt(Expression* channel)
3701 {
3702   go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3703   Location loc = this->location();
3704   this->advance_token();
3705   Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3706   Statement* s = Statement::make_send_statement(channel, val, loc);
3707   this->gogo_->add_statement(s);
3708 }
3709
3710 // IncDecStat = Expression ( "++" | "--" ) .
3711
3712 void
3713 Parse::inc_dec_stat(Expression* exp)
3714 {
3715   const Token* token = this->peek_token();
3716
3717   // Lvalue maps require special handling.
3718   if (exp->index_expression() != NULL)
3719     exp->index_expression()->set_is_lvalue();
3720
3721   if (token->is_op(OPERATOR_PLUSPLUS))
3722     this->gogo_->add_statement(Statement::make_inc_statement(exp));
3723   else if (token->is_op(OPERATOR_MINUSMINUS))
3724     this->gogo_->add_statement(Statement::make_dec_statement(exp));
3725   else
3726     go_unreachable();
3727   this->advance_token();
3728 }
3729
3730 // Assignment = ExpressionList assign_op ExpressionList .
3731
3732 // EXP is an expression that we have already parsed.
3733
3734 // If RANGE_CLAUSE is not NULL, then this will recognize a
3735 // RangeClause.
3736
3737 void
3738 Parse::assignment(Expression* expr, Range_clause* p_range_clause)
3739 {
3740   Expression_list* vars;
3741   if (!this->peek_token()->is_op(OPERATOR_COMMA))
3742     {
3743       vars = new Expression_list();
3744       vars->push_back(expr);
3745     }
3746   else
3747     {
3748       this->advance_token();
3749       vars = this->expression_list(expr, true);
3750     }
3751
3752   this->tuple_assignment(vars, p_range_clause);
3753 }
3754
3755 // An assignment statement.  LHS is the list of expressions which
3756 // appear on the left hand side.
3757
3758 // If RANGE_CLAUSE is not NULL, then this will recognize a
3759 // RangeClause.
3760
3761 void
3762 Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
3763 {
3764   const Token* token = this->peek_token();
3765   if (!token->is_op(OPERATOR_EQ)
3766       && !token->is_op(OPERATOR_PLUSEQ)
3767       && !token->is_op(OPERATOR_MINUSEQ)
3768       && !token->is_op(OPERATOR_OREQ)
3769       && !token->is_op(OPERATOR_XOREQ)
3770       && !token->is_op(OPERATOR_MULTEQ)
3771       && !token->is_op(OPERATOR_DIVEQ)
3772       && !token->is_op(OPERATOR_MODEQ)
3773       && !token->is_op(OPERATOR_LSHIFTEQ)
3774       && !token->is_op(OPERATOR_RSHIFTEQ)
3775       && !token->is_op(OPERATOR_ANDEQ)
3776       && !token->is_op(OPERATOR_BITCLEAREQ))
3777     {
3778       error_at(this->location(), "expected assignment operator");
3779       return;
3780     }
3781   Operator op = token->op();
3782   Location location = token->location();
3783
3784   token = this->advance_token();
3785
3786   if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
3787     {
3788       if (op != OPERATOR_EQ)
3789         error_at(this->location(), "range clause requires %<=%>");
3790       this->range_clause_expr(lhs, p_range_clause);
3791       return;
3792     }
3793
3794   Expression_list* vals = this->expression_list(NULL, false);
3795
3796   // We've parsed everything; check for errors.
3797   if (lhs == NULL || vals == NULL)
3798     return;
3799   for (Expression_list::const_iterator pe = lhs->begin();
3800        pe != lhs->end();
3801        ++pe)
3802     {
3803       if ((*pe)->is_error_expression())
3804         return;
3805       if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
3806         error_at((*pe)->location(), "cannot use _ as value");
3807     }
3808   for (Expression_list::const_iterator pe = vals->begin();
3809        pe != vals->end();
3810        ++pe)
3811     {
3812       if ((*pe)->is_error_expression())
3813         return;
3814     }
3815
3816   // Map expressions act differently when they are lvalues.
3817   for (Expression_list::iterator plv = lhs->begin();
3818        plv != lhs->end();
3819        ++plv)
3820     if ((*plv)->index_expression() != NULL)
3821       (*plv)->index_expression()->set_is_lvalue();
3822
3823   Call_expression* call;
3824   Index_expression* map_index;
3825   Receive_expression* receive;
3826   Type_guard_expression* type_guard;
3827   if (lhs->size() == vals->size())
3828     {
3829       Statement* s;
3830       if (lhs->size() > 1)
3831         {
3832           if (op != OPERATOR_EQ)
3833             error_at(location, "multiple values only permitted with %<=%>");
3834           s = Statement::make_tuple_assignment(lhs, vals, location);
3835         }
3836       else
3837         {
3838           if (op == OPERATOR_EQ)
3839             s = Statement::make_assignment(lhs->front(), vals->front(),
3840                                            location);
3841           else
3842             s = Statement::make_assignment_operation(op, lhs->front(),
3843                                                      vals->front(), location);
3844           delete lhs;
3845           delete vals;
3846         }
3847       this->gogo_->add_statement(s);
3848     }
3849   else if (vals->size() == 1
3850            && (call = (*vals->begin())->call_expression()) != NULL)
3851     {
3852       if (op != OPERATOR_EQ)
3853         error_at(location, "multiple results only permitted with %<=%>");
3854       delete vals;
3855       vals = new Expression_list;
3856       for (unsigned int i = 0; i < lhs->size(); ++i)
3857         vals->push_back(Expression::make_call_result(call, i));
3858       Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
3859       this->gogo_->add_statement(s);
3860     }
3861   else if (lhs->size() == 2
3862            && vals->size() == 1
3863            && (map_index = (*vals->begin())->index_expression()) != NULL)
3864     {
3865       if (op != OPERATOR_EQ)
3866         error_at(location, "two values from map requires %<=%>");
3867       Expression* val = lhs->front();
3868       Expression* present = lhs->back();
3869       Statement* s = Statement::make_tuple_map_assignment(val, present,
3870                                                           map_index, location);
3871       this->gogo_->add_statement(s);
3872     }
3873   else if (lhs->size() == 1
3874            && vals->size() == 2
3875            && (map_index = lhs->front()->index_expression()) != NULL)
3876     {
3877       if (op != OPERATOR_EQ)
3878         error_at(location, "assigning tuple to map index requires %<=%>");
3879       Expression* val = vals->front();
3880       Expression* should_set = vals->back();
3881       Statement* s = Statement::make_map_assignment(map_index, val, should_set,
3882                                                     location);
3883       this->gogo_->add_statement(s);
3884     }
3885   else if (lhs->size() == 2
3886            && vals->size() == 1
3887            && (receive = (*vals->begin())->receive_expression()) != NULL)
3888     {
3889       if (op != OPERATOR_EQ)
3890         error_at(location, "two values from receive requires %<=%>");
3891       Expression* val = lhs->front();
3892       Expression* success = lhs->back();
3893       Expression* channel = receive->channel();
3894       Statement* s = Statement::make_tuple_receive_assignment(val, success,
3895                                                               channel,
3896                                                               location);
3897       this->gogo_->add_statement(s);
3898     }
3899   else if (lhs->size() == 2
3900            && vals->size() == 1
3901            && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
3902     {
3903       if (op != OPERATOR_EQ)
3904         error_at(location, "two values from type guard requires %<=%>");
3905       Expression* val = lhs->front();
3906       Expression* ok = lhs->back();
3907       Expression* expr = type_guard->expr();
3908       Type* type = type_guard->type();
3909       Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
3910                                                                  expr, type,
3911                                                                  location);
3912       this->gogo_->add_statement(s);
3913     }
3914   else
3915     {
3916       error_at(location, "number of variables does not match number of values");
3917     }
3918 }
3919
3920 // GoStat = "go" Expression .
3921 // DeferStat = "defer" Expression .
3922
3923 void
3924 Parse::go_or_defer_stat()
3925 {
3926   go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
3927              || this->peek_token()->is_keyword(KEYWORD_DEFER));
3928   bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
3929   Location stat_location = this->location();
3930   this->advance_token();
3931   Location expr_location = this->location();
3932   Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3933   Call_expression* call_expr = expr->call_expression();
3934   if (call_expr == NULL)
3935     {
3936       error_at(expr_location, "expected call expression");
3937       return;
3938     }
3939
3940   // Make it easier to simplify go/defer statements by putting every
3941   // statement in its own block.
3942   this->gogo_->start_block(stat_location);
3943   Statement* stat;
3944   if (is_go)
3945     stat = Statement::make_go_statement(call_expr, stat_location);
3946   else
3947     stat = Statement::make_defer_statement(call_expr, stat_location);
3948   this->gogo_->add_statement(stat);
3949   this->gogo_->add_block(this->gogo_->finish_block(stat_location),
3950                          stat_location);
3951 }
3952
3953 // ReturnStat = "return" [ ExpressionList ] .
3954
3955 void
3956 Parse::return_stat()
3957 {
3958   go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
3959   Location location = this->location();
3960   this->advance_token();
3961   Expression_list* vals = NULL;
3962   if (this->expression_may_start_here())
3963     vals = this->expression_list(NULL, false);
3964   this->gogo_->add_statement(Statement::make_return_statement(vals, location));
3965
3966   if (vals == NULL
3967       && this->gogo_->current_function()->func_value()->results_are_named())
3968     {
3969       Named_object* function = this->gogo_->current_function();
3970       Function::Results* results = function->func_value()->result_variables();
3971       for (Function::Results::const_iterator p = results->begin();
3972            p != results->end();
3973            ++p)
3974         {
3975           Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
3976           if (no == NULL)
3977             go_assert(saw_errors());
3978           else if (!no->is_result_variable())
3979             error_at(location, "%qs is shadowed during return",
3980                      (*p)->message_name().c_str());
3981         }
3982     }
3983 }
3984
3985 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
3986 //          [ "else" ( IfStmt | Block ) ] .
3987
3988 void
3989 Parse::if_stat()