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