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