OSDN Git Service

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