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