compiler: If a variable that is only set, give not used error.
[official-gcc.git] / gcc / go / gofrontend / parse.cc
blob90f1a3405c116bee73d4879d5c3b2fcb047d1e72
1 // parse.cc -- Go frontend parser.
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.
7 #include "go-system.h"
9 #include "lex.h"
10 #include "gogo.h"
11 #include "types.h"
12 #include "statements.h"
13 #include "expressions.h"
14 #include "parse.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
20 bool
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
22 const Enclosing_var& v2)
24 if (v1.var() == v2.var())
25 return false;
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;
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();
41 // Class Parse.
43 Parse::Parse(Lex* lex, Gogo* gogo)
44 : lex_(lex),
45 token_(Token::make_invalid_token(Linemap::unknown_location())),
46 unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
47 unget_token_valid_(false),
48 is_erroneous_function_(false),
49 gogo_(gogo),
50 break_stack_(NULL),
51 continue_stack_(NULL),
52 iota_(0),
53 enclosing_vars_(),
54 type_switch_vars_()
58 // Return the current token.
60 const Token*
61 Parse::peek_token()
63 if (this->unget_token_valid_)
64 return &this->unget_token_;
65 if (this->token_.is_invalid())
66 this->token_ = this->lex_->next_token();
67 return &this->token_;
70 // Advance to the next token and return it.
72 const Token*
73 Parse::advance_token()
75 if (this->unget_token_valid_)
77 this->unget_token_valid_ = false;
78 if (!this->token_.is_invalid())
79 return &this->token_;
81 this->token_ = this->lex_->next_token();
82 return &this->token_;
85 // Push a token back on the input stream.
87 void
88 Parse::unget_token(const Token& token)
90 go_assert(!this->unget_token_valid_);
91 this->unget_token_ = token;
92 this->unget_token_valid_ = true;
95 // The location of the current token.
97 Location
98 Parse::location()
100 return this->peek_token()->location();
103 // IdentifierList = identifier { "," identifier } .
105 void
106 Parse::identifier_list(Typed_identifier_list* til)
108 const Token* token = this->peek_token();
109 while (true)
111 if (!token->is_identifier())
113 error_at(this->location(), "expected identifier");
114 return;
116 std::string name =
117 this->gogo_->pack_hidden_name(token->identifier(),
118 token->is_identifier_exported());
119 til->push_back(Typed_identifier(name, NULL, token->location()));
120 token = this->advance_token();
121 if (!token->is_op(OPERATOR_COMMA))
122 return;
123 token = this->advance_token();
127 // ExpressionList = Expression { "," Expression } .
129 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
130 // literal.
132 // If MAY_BE_SINK is true, the expressions in the list may be "_".
134 Expression_list*
135 Parse::expression_list(Expression* first, bool may_be_sink,
136 bool may_be_composite_lit)
138 Expression_list* ret = new Expression_list();
139 if (first != NULL)
140 ret->push_back(first);
141 while (true)
143 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink,
144 may_be_composite_lit, NULL, NULL));
146 const Token* token = this->peek_token();
147 if (!token->is_op(OPERATOR_COMMA))
148 return ret;
150 // Most expression lists permit a trailing comma.
151 Location location = token->location();
152 this->advance_token();
153 if (!this->expression_may_start_here())
155 this->unget_token(Token::make_operator_token(OPERATOR_COMMA,
156 location));
157 return ret;
162 // QualifiedIdent = [ PackageName "." ] identifier .
163 // PackageName = identifier .
165 // This sets *PNAME to the identifier and sets *PPACKAGE to the
166 // package or NULL if there isn't one. This returns true on success,
167 // false on failure in which case it will have emitted an error
168 // message.
170 bool
171 Parse::qualified_ident(std::string* pname, Named_object** ppackage)
173 const Token* token = this->peek_token();
174 if (!token->is_identifier())
176 error_at(this->location(), "expected identifier");
177 return false;
180 std::string name = token->identifier();
181 bool is_exported = token->is_identifier_exported();
182 name = this->gogo_->pack_hidden_name(name, is_exported);
184 token = this->advance_token();
185 if (!token->is_op(OPERATOR_DOT))
187 *pname = name;
188 *ppackage = NULL;
189 return true;
192 Named_object* package = this->gogo_->lookup(name, NULL);
193 if (package == NULL || !package->is_package())
195 error_at(this->location(), "expected package");
196 // We expect . IDENTIFIER; skip both.
197 if (this->advance_token()->is_identifier())
198 this->advance_token();
199 return false;
202 package->package_value()->set_used();
204 token = this->advance_token();
205 if (!token->is_identifier())
207 error_at(this->location(), "expected identifier");
208 return false;
211 name = token->identifier();
213 if (name == "_")
215 error_at(this->location(), "invalid use of %<_%>");
216 name = Gogo::erroneous_name();
219 if (package->name() == this->gogo_->package_name())
220 name = this->gogo_->pack_hidden_name(name,
221 token->is_identifier_exported());
223 *pname = name;
224 *ppackage = package;
226 this->advance_token();
228 return true;
231 // Type = TypeName | TypeLit | "(" Type ")" .
232 // TypeLit =
233 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
234 // SliceType | MapType | ChannelType .
236 Type*
237 Parse::type()
239 const Token* token = this->peek_token();
240 if (token->is_identifier())
241 return this->type_name(true);
242 else if (token->is_op(OPERATOR_LSQUARE))
243 return this->array_type(false);
244 else if (token->is_keyword(KEYWORD_CHAN)
245 || token->is_op(OPERATOR_CHANOP))
246 return this->channel_type();
247 else if (token->is_keyword(KEYWORD_INTERFACE))
248 return this->interface_type();
249 else if (token->is_keyword(KEYWORD_FUNC))
251 Location location = token->location();
252 this->advance_token();
253 Type* type = this->signature(NULL, location);
254 if (type == NULL)
255 return Type::make_error_type();
256 return type;
258 else if (token->is_keyword(KEYWORD_MAP))
259 return this->map_type();
260 else if (token->is_keyword(KEYWORD_STRUCT))
261 return this->struct_type();
262 else if (token->is_op(OPERATOR_MULT))
263 return this->pointer_type();
264 else if (token->is_op(OPERATOR_LPAREN))
266 this->advance_token();
267 Type* ret = this->type();
268 if (this->peek_token()->is_op(OPERATOR_RPAREN))
269 this->advance_token();
270 else
272 if (!ret->is_error_type())
273 error_at(this->location(), "expected %<)%>");
275 return ret;
277 else
279 error_at(token->location(), "expected type");
280 return Type::make_error_type();
284 bool
285 Parse::type_may_start_here()
287 const Token* token = this->peek_token();
288 return (token->is_identifier()
289 || token->is_op(OPERATOR_LSQUARE)
290 || token->is_op(OPERATOR_CHANOP)
291 || token->is_keyword(KEYWORD_CHAN)
292 || token->is_keyword(KEYWORD_INTERFACE)
293 || token->is_keyword(KEYWORD_FUNC)
294 || token->is_keyword(KEYWORD_MAP)
295 || token->is_keyword(KEYWORD_STRUCT)
296 || token->is_op(OPERATOR_MULT)
297 || token->is_op(OPERATOR_LPAREN));
300 // TypeName = QualifiedIdent .
302 // If MAY_BE_NIL is true, then an identifier with the value of the
303 // predefined constant nil is accepted, returning the nil type.
305 Type*
306 Parse::type_name(bool issue_error)
308 Location location = this->location();
310 std::string name;
311 Named_object* package;
312 if (!this->qualified_ident(&name, &package))
313 return Type::make_error_type();
315 Named_object* named_object;
316 if (package == NULL)
317 named_object = this->gogo_->lookup(name, NULL);
318 else
320 named_object = package->package_value()->lookup(name);
321 if (named_object == NULL
322 && issue_error
323 && package->name() != this->gogo_->package_name())
325 // Check whether the name is there but hidden.
326 std::string s = ('.' + package->package_value()->pkgpath()
327 + '.' + name);
328 named_object = package->package_value()->lookup(s);
329 if (named_object != NULL)
331 Package* p = package->package_value();
332 const std::string& packname(p->package_name());
333 error_at(location, "invalid reference to hidden type %<%s.%s%>",
334 Gogo::message_name(packname).c_str(),
335 Gogo::message_name(name).c_str());
336 issue_error = false;
341 bool ok = true;
342 if (named_object == NULL)
344 if (package == NULL)
345 named_object = this->gogo_->add_unknown_name(name, location);
346 else
348 const std::string& packname(package->package_value()->package_name());
349 error_at(location, "reference to undefined identifier %<%s.%s%>",
350 Gogo::message_name(packname).c_str(),
351 Gogo::message_name(name).c_str());
352 issue_error = false;
353 ok = false;
356 else if (named_object->is_type())
358 if (!named_object->type_value()->is_visible())
359 ok = false;
361 else if (named_object->is_unknown() || named_object->is_type_declaration())
363 else
364 ok = false;
366 if (!ok)
368 if (issue_error)
369 error_at(location, "expected type");
370 return Type::make_error_type();
373 if (named_object->is_type())
374 return named_object->type_value();
375 else if (named_object->is_unknown() || named_object->is_type_declaration())
376 return Type::make_forward_declaration(named_object);
377 else
378 go_unreachable();
381 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
382 // ArrayLength = Expression .
383 // ElementType = CompleteType .
385 Type*
386 Parse::array_type(bool may_use_ellipsis)
388 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
389 const Token* token = this->advance_token();
391 Expression* length = NULL;
392 if (token->is_op(OPERATOR_RSQUARE))
393 this->advance_token();
394 else
396 if (!token->is_op(OPERATOR_ELLIPSIS))
397 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
398 else if (may_use_ellipsis)
400 // An ellipsis is used in composite literals to represent a
401 // fixed array of the size of the number of elements. We
402 // use a length of nil to represent this, and change the
403 // length when parsing the composite literal.
404 length = Expression::make_nil(this->location());
405 this->advance_token();
407 else
409 error_at(this->location(),
410 "use of %<[...]%> outside of array literal");
411 length = Expression::make_error(this->location());
412 this->advance_token();
414 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
416 error_at(this->location(), "expected %<]%>");
417 return Type::make_error_type();
419 this->advance_token();
422 Type* element_type = this->type();
424 return Type::make_array_type(element_type, length);
427 // MapType = "map" "[" KeyType "]" ValueType .
428 // KeyType = CompleteType .
429 // ValueType = CompleteType .
431 Type*
432 Parse::map_type()
434 Location location = this->location();
435 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
436 if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
438 error_at(this->location(), "expected %<[%>");
439 return Type::make_error_type();
441 this->advance_token();
443 Type* key_type = this->type();
445 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
447 error_at(this->location(), "expected %<]%>");
448 return Type::make_error_type();
450 this->advance_token();
452 Type* value_type = this->type();
454 if (key_type->is_error_type() || value_type->is_error_type())
455 return Type::make_error_type();
457 return Type::make_map_type(key_type, value_type, location);
460 // StructType = "struct" "{" { FieldDecl ";" } "}" .
462 Type*
463 Parse::struct_type()
465 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
466 Location location = this->location();
467 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
469 Location token_loc = this->location();
470 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
471 && this->advance_token()->is_op(OPERATOR_LCURLY))
472 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
473 else
475 error_at(this->location(), "expected %<{%>");
476 return Type::make_error_type();
479 this->advance_token();
481 Struct_field_list* sfl = new Struct_field_list;
482 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
484 this->field_decl(sfl);
485 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
486 this->advance_token();
487 else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
489 error_at(this->location(), "expected %<;%> or %<}%> or newline");
490 if (!this->skip_past_error(OPERATOR_RCURLY))
491 return Type::make_error_type();
494 this->advance_token();
496 for (Struct_field_list::const_iterator pi = sfl->begin();
497 pi != sfl->end();
498 ++pi)
500 if (pi->type()->is_error_type())
501 return pi->type();
502 for (Struct_field_list::const_iterator pj = pi + 1;
503 pj != sfl->end();
504 ++pj)
506 if (pi->field_name() == pj->field_name()
507 && !Gogo::is_sink_name(pi->field_name()))
508 error_at(pi->location(), "duplicate field name %<%s%>",
509 Gogo::message_name(pi->field_name()).c_str());
513 return Type::make_struct_type(sfl, location);
516 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
517 // Tag = string_lit .
519 void
520 Parse::field_decl(Struct_field_list* sfl)
522 const Token* token = this->peek_token();
523 Location location = token->location();
524 bool is_anonymous;
525 bool is_anonymous_pointer;
526 if (token->is_op(OPERATOR_MULT))
528 is_anonymous = true;
529 is_anonymous_pointer = true;
531 else if (token->is_identifier())
533 std::string id = token->identifier();
534 bool is_id_exported = token->is_identifier_exported();
535 Location id_location = token->location();
536 token = this->advance_token();
537 is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
538 || token->is_op(OPERATOR_RCURLY)
539 || token->is_op(OPERATOR_DOT)
540 || token->is_string());
541 is_anonymous_pointer = false;
542 this->unget_token(Token::make_identifier_token(id, is_id_exported,
543 id_location));
545 else
547 error_at(this->location(), "expected field name");
548 this->gogo_->mark_locals_used();
549 while (!token->is_op(OPERATOR_SEMICOLON)
550 && !token->is_op(OPERATOR_RCURLY)
551 && !token->is_eof())
552 token = this->advance_token();
553 return;
556 if (is_anonymous)
558 if (is_anonymous_pointer)
560 this->advance_token();
561 if (!this->peek_token()->is_identifier())
563 error_at(this->location(), "expected field name");
564 this->gogo_->mark_locals_used();
565 while (!token->is_op(OPERATOR_SEMICOLON)
566 && !token->is_op(OPERATOR_RCURLY)
567 && !token->is_eof())
568 token = this->advance_token();
569 return;
572 Type* type = this->type_name(true);
574 std::string tag;
575 if (this->peek_token()->is_string())
577 tag = this->peek_token()->string_value();
578 this->advance_token();
581 if (!type->is_error_type())
583 if (is_anonymous_pointer)
584 type = Type::make_pointer_type(type);
585 sfl->push_back(Struct_field(Typed_identifier("", type, location)));
586 if (!tag.empty())
587 sfl->back().set_tag(tag);
590 else
592 Typed_identifier_list til;
593 while (true)
595 token = this->peek_token();
596 if (!token->is_identifier())
598 error_at(this->location(), "expected identifier");
599 return;
601 std::string name =
602 this->gogo_->pack_hidden_name(token->identifier(),
603 token->is_identifier_exported());
604 til.push_back(Typed_identifier(name, NULL, token->location()));
605 if (!this->advance_token()->is_op(OPERATOR_COMMA))
606 break;
607 this->advance_token();
610 Type* type = this->type();
612 std::string tag;
613 if (this->peek_token()->is_string())
615 tag = this->peek_token()->string_value();
616 this->advance_token();
619 for (Typed_identifier_list::iterator p = til.begin();
620 p != til.end();
621 ++p)
623 p->set_type(type);
624 sfl->push_back(Struct_field(*p));
625 if (!tag.empty())
626 sfl->back().set_tag(tag);
631 // PointerType = "*" Type .
633 Type*
634 Parse::pointer_type()
636 go_assert(this->peek_token()->is_op(OPERATOR_MULT));
637 this->advance_token();
638 Type* type = this->type();
639 if (type->is_error_type())
640 return type;
641 return Type::make_pointer_type(type);
644 // ChannelType = Channel | SendChannel | RecvChannel .
645 // Channel = "chan" ElementType .
646 // SendChannel = "chan" "<-" ElementType .
647 // RecvChannel = "<-" "chan" ElementType .
649 Type*
650 Parse::channel_type()
652 const Token* token = this->peek_token();
653 bool send = true;
654 bool receive = true;
655 if (token->is_op(OPERATOR_CHANOP))
657 if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
659 error_at(this->location(), "expected %<chan%>");
660 return Type::make_error_type();
662 send = false;
663 this->advance_token();
665 else
667 go_assert(token->is_keyword(KEYWORD_CHAN));
668 if (this->advance_token()->is_op(OPERATOR_CHANOP))
670 receive = false;
671 this->advance_token();
675 // Better error messages for the common error of omitting the
676 // channel element type.
677 if (!this->type_may_start_here())
679 token = this->peek_token();
680 if (token->is_op(OPERATOR_RCURLY))
681 error_at(this->location(), "unexpected %<}%> in channel type");
682 else if (token->is_op(OPERATOR_RPAREN))
683 error_at(this->location(), "unexpected %<)%> in channel type");
684 else if (token->is_op(OPERATOR_COMMA))
685 error_at(this->location(), "unexpected comma in channel type");
686 else
687 error_at(this->location(), "expected channel element type");
688 return Type::make_error_type();
691 Type* element_type = this->type();
692 return Type::make_channel_type(send, receive, element_type);
695 // Give an error for a duplicate parameter or receiver name.
697 void
698 Parse::check_signature_names(const Typed_identifier_list* params,
699 Parse::Names* names)
701 for (Typed_identifier_list::const_iterator p = params->begin();
702 p != params->end();
703 ++p)
705 if (p->name().empty() || Gogo::is_sink_name(p->name()))
706 continue;
707 std::pair<std::string, const Typed_identifier*> val =
708 std::make_pair(p->name(), &*p);
709 std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
710 if (!ins.second)
712 error_at(p->location(), "redefinition of %qs",
713 Gogo::message_name(p->name()).c_str());
714 inform(ins.first->second->location(),
715 "previous definition of %qs was here",
716 Gogo::message_name(p->name()).c_str());
721 // Signature = Parameters [ Result ] .
723 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
724 // location of the start of the type.
726 // This returns NULL on a parse error.
728 Function_type*
729 Parse::signature(Typed_identifier* receiver, Location location)
731 bool is_varargs = false;
732 Typed_identifier_list* params;
733 bool params_ok = this->parameters(&params, &is_varargs);
735 Typed_identifier_list* results = NULL;
736 if (this->peek_token()->is_op(OPERATOR_LPAREN)
737 || this->type_may_start_here())
739 if (!this->result(&results))
740 return NULL;
743 if (!params_ok)
744 return NULL;
746 Parse::Names names;
747 if (receiver != NULL)
748 names[receiver->name()] = receiver;
749 if (params != NULL)
750 this->check_signature_names(params, &names);
751 if (results != NULL)
752 this->check_signature_names(results, &names);
754 Function_type* ret = Type::make_function_type(receiver, params, results,
755 location);
756 if (is_varargs)
757 ret->set_is_varargs();
758 return ret;
761 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
763 // This returns false on a parse error.
765 bool
766 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
768 *pparams = NULL;
770 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
772 error_at(this->location(), "expected %<(%>");
773 return false;
776 Typed_identifier_list* params = NULL;
777 bool saw_error = false;
779 const Token* token = this->advance_token();
780 if (!token->is_op(OPERATOR_RPAREN))
782 params = this->parameter_list(is_varargs);
783 if (params == NULL)
784 saw_error = true;
785 token = this->peek_token();
788 // The optional trailing comma is picked up in parameter_list.
790 if (!token->is_op(OPERATOR_RPAREN))
791 error_at(this->location(), "expected %<)%>");
792 else
793 this->advance_token();
795 if (saw_error)
796 return false;
798 *pparams = params;
799 return true;
802 // ParameterList = ParameterDecl { "," ParameterDecl } .
804 // This sets *IS_VARARGS if the list ends with an ellipsis.
805 // IS_VARARGS will be NULL if varargs are not permitted.
807 // We pick up an optional trailing comma.
809 // This returns NULL if some error is seen.
811 Typed_identifier_list*
812 Parse::parameter_list(bool* is_varargs)
814 Location location = this->location();
815 Typed_identifier_list* ret = new Typed_identifier_list();
817 bool saw_error = false;
819 // If we see an identifier and then a comma, then we don't know
820 // whether we are looking at a list of identifiers followed by a
821 // type, or a list of types given by name. We have to do an
822 // arbitrary lookahead to figure it out.
824 bool parameters_have_names;
825 const Token* token = this->peek_token();
826 if (!token->is_identifier())
828 // This must be a type which starts with something like '*'.
829 parameters_have_names = false;
831 else
833 std::string name = token->identifier();
834 bool is_exported = token->is_identifier_exported();
835 Location location = token->location();
836 token = this->advance_token();
837 if (!token->is_op(OPERATOR_COMMA))
839 if (token->is_op(OPERATOR_DOT))
841 // This is a qualified identifier, which must turn out
842 // to be a type.
843 parameters_have_names = false;
845 else if (token->is_op(OPERATOR_RPAREN))
847 // A single identifier followed by a parenthesis must be
848 // a type name.
849 parameters_have_names = false;
851 else
853 // An identifier followed by something other than a
854 // comma or a dot or a right parenthesis must be a
855 // parameter name followed by a type.
856 parameters_have_names = true;
859 this->unget_token(Token::make_identifier_token(name, is_exported,
860 location));
862 else
864 // An identifier followed by a comma may be the first in a
865 // list of parameter names followed by a type, or it may be
866 // the first in a list of types without parameter names. To
867 // find out we gather as many identifiers separated by
868 // commas as we can.
869 std::string id_name = this->gogo_->pack_hidden_name(name,
870 is_exported);
871 ret->push_back(Typed_identifier(id_name, NULL, location));
872 bool just_saw_comma = true;
873 while (this->advance_token()->is_identifier())
875 name = this->peek_token()->identifier();
876 is_exported = this->peek_token()->is_identifier_exported();
877 location = this->peek_token()->location();
878 id_name = this->gogo_->pack_hidden_name(name, is_exported);
879 ret->push_back(Typed_identifier(id_name, NULL, location));
880 if (!this->advance_token()->is_op(OPERATOR_COMMA))
882 just_saw_comma = false;
883 break;
887 if (just_saw_comma)
889 // We saw ID1 "," ID2 "," followed by something which
890 // was not an identifier. We must be seeing the start
891 // of a type, and ID1 and ID2 must be types, and the
892 // parameters don't have names.
893 parameters_have_names = false;
895 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
897 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
898 // and the parameters don't have names.
899 parameters_have_names = false;
901 else if (this->peek_token()->is_op(OPERATOR_DOT))
903 // We saw ID1 "," ID2 ".". ID2 must be a package name,
904 // ID1 must be a type, and the parameters don't have
905 // names.
906 parameters_have_names = false;
907 this->unget_token(Token::make_identifier_token(name, is_exported,
908 location));
909 ret->pop_back();
910 just_saw_comma = true;
912 else
914 // We saw ID1 "," ID2 followed by something other than
915 // ",", ".", or ")". We must be looking at the start of
916 // a type, and ID1 and ID2 must be parameter names.
917 parameters_have_names = true;
920 if (parameters_have_names)
922 go_assert(!just_saw_comma);
923 // We have just seen ID1, ID2 xxx.
924 Type* type;
925 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
926 type = this->type();
927 else
929 error_at(this->location(), "%<...%> only permits one name");
930 saw_error = true;
931 this->advance_token();
932 type = this->type();
934 for (size_t i = 0; i < ret->size(); ++i)
935 ret->set_type(i, type);
936 if (!this->peek_token()->is_op(OPERATOR_COMMA))
937 return saw_error ? NULL : ret;
938 if (this->advance_token()->is_op(OPERATOR_RPAREN))
939 return saw_error ? NULL : ret;
941 else
943 Typed_identifier_list* tret = new Typed_identifier_list();
944 for (Typed_identifier_list::const_iterator p = ret->begin();
945 p != ret->end();
946 ++p)
948 Named_object* no = this->gogo_->lookup(p->name(), NULL);
949 Type* type;
950 if (no == NULL)
951 no = this->gogo_->add_unknown_name(p->name(),
952 p->location());
954 if (no->is_type())
955 type = no->type_value();
956 else if (no->is_unknown() || no->is_type_declaration())
957 type = Type::make_forward_declaration(no);
958 else
960 error_at(p->location(), "expected %<%s%> to be a type",
961 Gogo::message_name(p->name()).c_str());
962 saw_error = true;
963 type = Type::make_error_type();
965 tret->push_back(Typed_identifier("", type, p->location()));
967 delete ret;
968 ret = tret;
969 if (!just_saw_comma
970 || this->peek_token()->is_op(OPERATOR_RPAREN))
971 return saw_error ? NULL : ret;
976 bool mix_error = false;
977 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
978 while (this->peek_token()->is_op(OPERATOR_COMMA))
980 if (this->advance_token()->is_op(OPERATOR_RPAREN))
981 break;
982 if (is_varargs != NULL && *is_varargs)
984 error_at(this->location(), "%<...%> must be last parameter");
985 saw_error = true;
987 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
989 if (mix_error)
991 error_at(location, "invalid named/anonymous mix");
992 saw_error = true;
994 if (saw_error)
996 delete ret;
997 return NULL;
999 return ret;
1002 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1004 void
1005 Parse::parameter_decl(bool parameters_have_names,
1006 Typed_identifier_list* til,
1007 bool* is_varargs,
1008 bool* mix_error)
1010 if (!parameters_have_names)
1012 Type* type;
1013 Location location = this->location();
1014 if (!this->peek_token()->is_identifier())
1016 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1017 type = this->type();
1018 else
1020 if (is_varargs == NULL)
1021 error_at(this->location(), "invalid use of %<...%>");
1022 else
1023 *is_varargs = true;
1024 this->advance_token();
1025 if (is_varargs == NULL
1026 && this->peek_token()->is_op(OPERATOR_RPAREN))
1027 type = Type::make_error_type();
1028 else
1030 Type* element_type = this->type();
1031 type = Type::make_array_type(element_type, NULL);
1035 else
1037 type = this->type_name(false);
1038 if (type->is_error_type()
1039 || (!this->peek_token()->is_op(OPERATOR_COMMA)
1040 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1042 *mix_error = true;
1043 while (!this->peek_token()->is_op(OPERATOR_COMMA)
1044 && !this->peek_token()->is_op(OPERATOR_RPAREN))
1045 this->advance_token();
1048 if (!type->is_error_type())
1049 til->push_back(Typed_identifier("", type, location));
1051 else
1053 size_t orig_count = til->size();
1054 if (this->peek_token()->is_identifier())
1055 this->identifier_list(til);
1056 else
1057 *mix_error = true;
1058 size_t new_count = til->size();
1060 Type* type;
1061 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1062 type = this->type();
1063 else
1065 if (is_varargs == NULL)
1066 error_at(this->location(), "invalid use of %<...%>");
1067 else if (new_count > orig_count + 1)
1068 error_at(this->location(), "%<...%> only permits one name");
1069 else
1070 *is_varargs = true;
1071 this->advance_token();
1072 Type* element_type = this->type();
1073 type = Type::make_array_type(element_type, NULL);
1075 for (size_t i = orig_count; i < new_count; ++i)
1076 til->set_type(i, type);
1080 // Result = Parameters | Type .
1082 // This returns false on a parse error.
1084 bool
1085 Parse::result(Typed_identifier_list** presults)
1087 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1088 return this->parameters(presults, NULL);
1089 else
1091 Location location = this->location();
1092 Type* type = this->type();
1093 if (type->is_error_type())
1095 *presults = NULL;
1096 return false;
1098 Typed_identifier_list* til = new Typed_identifier_list();
1099 til->push_back(Typed_identifier("", type, location));
1100 *presults = til;
1101 return true;
1105 // Block = "{" [ StatementList ] "}" .
1107 // Returns the location of the closing brace.
1109 Location
1110 Parse::block()
1112 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1114 Location loc = this->location();
1115 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1116 && this->advance_token()->is_op(OPERATOR_LCURLY))
1117 error_at(loc, "unexpected semicolon or newline before %<{%>");
1118 else
1120 error_at(this->location(), "expected %<{%>");
1121 return Linemap::unknown_location();
1125 const Token* token = this->advance_token();
1127 if (!token->is_op(OPERATOR_RCURLY))
1129 this->statement_list();
1130 token = this->peek_token();
1131 if (!token->is_op(OPERATOR_RCURLY))
1133 if (!token->is_eof() || !saw_errors())
1134 error_at(this->location(), "expected %<}%>");
1136 this->gogo_->mark_locals_used();
1138 // Skip ahead to the end of the block, in hopes of avoiding
1139 // lots of meaningless errors.
1140 Location ret = token->location();
1141 int nest = 0;
1142 while (!token->is_eof())
1144 if (token->is_op(OPERATOR_LCURLY))
1145 ++nest;
1146 else if (token->is_op(OPERATOR_RCURLY))
1148 --nest;
1149 if (nest < 0)
1151 this->advance_token();
1152 break;
1155 token = this->advance_token();
1156 ret = token->location();
1158 return ret;
1162 Location ret = token->location();
1163 this->advance_token();
1164 return ret;
1167 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1168 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1170 Type*
1171 Parse::interface_type()
1173 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1174 Location location = this->location();
1176 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1178 Location token_loc = this->location();
1179 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1180 && this->advance_token()->is_op(OPERATOR_LCURLY))
1181 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1182 else
1184 error_at(this->location(), "expected %<{%>");
1185 return Type::make_error_type();
1188 this->advance_token();
1190 Typed_identifier_list* methods = new Typed_identifier_list();
1191 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1193 this->method_spec(methods);
1194 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1196 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1197 break;
1198 this->method_spec(methods);
1200 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1202 error_at(this->location(), "expected %<}%>");
1203 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1205 if (this->peek_token()->is_eof())
1206 return Type::make_error_type();
1210 this->advance_token();
1212 if (methods->empty())
1214 delete methods;
1215 methods = NULL;
1218 Interface_type* ret = Type::make_interface_type(methods, location);
1219 this->gogo_->record_interface_type(ret);
1220 return ret;
1223 // MethodSpec = MethodName Signature | InterfaceTypeName .
1224 // MethodName = identifier .
1225 // InterfaceTypeName = TypeName .
1227 void
1228 Parse::method_spec(Typed_identifier_list* methods)
1230 const Token* token = this->peek_token();
1231 if (!token->is_identifier())
1233 error_at(this->location(), "expected identifier");
1234 return;
1237 std::string name = token->identifier();
1238 bool is_exported = token->is_identifier_exported();
1239 Location location = token->location();
1241 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1243 // This is a MethodName.
1244 name = this->gogo_->pack_hidden_name(name, is_exported);
1245 Type* type = this->signature(NULL, location);
1246 if (type == NULL)
1247 return;
1248 methods->push_back(Typed_identifier(name, type, location));
1250 else
1252 this->unget_token(Token::make_identifier_token(name, is_exported,
1253 location));
1254 Type* type = this->type_name(false);
1255 if (type->is_error_type()
1256 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1257 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1259 if (this->peek_token()->is_op(OPERATOR_COMMA))
1260 error_at(this->location(),
1261 "name list not allowed in interface type");
1262 else
1263 error_at(location, "expected signature or type name");
1264 this->gogo_->mark_locals_used();
1265 token = this->peek_token();
1266 while (!token->is_eof()
1267 && !token->is_op(OPERATOR_SEMICOLON)
1268 && !token->is_op(OPERATOR_RCURLY))
1269 token = this->advance_token();
1270 return;
1272 // This must be an interface type, but we can't check that now.
1273 // We check it and pull out the methods in
1274 // Interface_type::do_verify.
1275 methods->push_back(Typed_identifier("", type, location));
1279 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1281 void
1282 Parse::declaration()
1284 const Token* token = this->peek_token();
1286 bool saw_nointerface = this->lex_->get_and_clear_nointerface();
1287 if (saw_nointerface && !token->is_keyword(KEYWORD_FUNC))
1288 warning_at(token->location(), 0,
1289 "ignoring magic //go:nointerface comment before non-method");
1291 if (token->is_keyword(KEYWORD_CONST))
1292 this->const_decl();
1293 else if (token->is_keyword(KEYWORD_TYPE))
1294 this->type_decl();
1295 else if (token->is_keyword(KEYWORD_VAR))
1296 this->var_decl();
1297 else if (token->is_keyword(KEYWORD_FUNC))
1298 this->function_decl(saw_nointerface);
1299 else
1301 error_at(this->location(), "expected declaration");
1302 this->advance_token();
1306 bool
1307 Parse::declaration_may_start_here()
1309 const Token* token = this->peek_token();
1310 return (token->is_keyword(KEYWORD_CONST)
1311 || token->is_keyword(KEYWORD_TYPE)
1312 || token->is_keyword(KEYWORD_VAR)
1313 || token->is_keyword(KEYWORD_FUNC));
1316 // Decl<P> = P | "(" [ List<P> ] ")" .
1318 void
1319 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1321 if (this->peek_token()->is_eof())
1323 if (!saw_errors())
1324 error_at(this->location(), "unexpected end of file");
1325 return;
1328 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1329 (this->*pfn)(varg);
1330 else
1332 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1334 this->list(pfn, varg, true);
1335 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1337 error_at(this->location(), "missing %<)%>");
1338 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1340 if (this->peek_token()->is_eof())
1341 return;
1345 this->advance_token();
1349 // List<P> = P { ";" P } [ ";" ] .
1351 // In order to pick up the trailing semicolon we need to know what
1352 // might follow. This is either a '}' or a ')'.
1354 void
1355 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1357 (this->*pfn)(varg);
1358 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1359 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1360 || this->peek_token()->is_op(OPERATOR_COMMA))
1362 if (this->peek_token()->is_op(OPERATOR_COMMA))
1363 error_at(this->location(), "unexpected comma");
1364 if (this->advance_token()->is_op(follow))
1365 break;
1366 (this->*pfn)(varg);
1370 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1372 void
1373 Parse::const_decl()
1375 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1376 this->advance_token();
1377 this->reset_iota();
1379 Type* last_type = NULL;
1380 Expression_list* last_expr_list = NULL;
1382 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1383 this->const_spec(&last_type, &last_expr_list);
1384 else
1386 this->advance_token();
1387 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1389 this->const_spec(&last_type, &last_expr_list);
1390 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1391 this->advance_token();
1392 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1394 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1395 if (!this->skip_past_error(OPERATOR_RPAREN))
1396 return;
1399 this->advance_token();
1402 if (last_expr_list != NULL)
1403 delete last_expr_list;
1406 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1408 void
1409 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1411 Typed_identifier_list til;
1412 this->identifier_list(&til);
1414 Type* type = NULL;
1415 if (this->type_may_start_here())
1417 type = this->type();
1418 *last_type = NULL;
1419 *last_expr_list = NULL;
1422 Expression_list *expr_list;
1423 if (!this->peek_token()->is_op(OPERATOR_EQ))
1425 if (*last_expr_list == NULL)
1427 error_at(this->location(), "expected %<=%>");
1428 return;
1430 type = *last_type;
1431 expr_list = new Expression_list;
1432 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1433 p != (*last_expr_list)->end();
1434 ++p)
1435 expr_list->push_back((*p)->copy());
1437 else
1439 this->advance_token();
1440 expr_list = this->expression_list(NULL, false, true);
1441 *last_type = type;
1442 if (*last_expr_list != NULL)
1443 delete *last_expr_list;
1444 *last_expr_list = expr_list;
1447 Expression_list::const_iterator pe = expr_list->begin();
1448 for (Typed_identifier_list::iterator pi = til.begin();
1449 pi != til.end();
1450 ++pi, ++pe)
1452 if (pe == expr_list->end())
1454 error_at(this->location(), "not enough initializers");
1455 return;
1457 if (type != NULL)
1458 pi->set_type(type);
1460 if (!Gogo::is_sink_name(pi->name()))
1461 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1462 else
1464 static int count;
1465 char buf[30];
1466 snprintf(buf, sizeof buf, ".$sinkconst%d", count);
1467 ++count;
1468 Typed_identifier ti(std::string(buf), type, pi->location());
1469 Named_object* no = this->gogo_->add_constant(ti, *pe, this->iota_value());
1470 no->const_value()->set_is_sink();
1473 if (pe != expr_list->end())
1474 error_at(this->location(), "too many initializers");
1476 this->increment_iota();
1478 return;
1481 // TypeDecl = "type" Decl<TypeSpec> .
1483 void
1484 Parse::type_decl()
1486 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1487 this->advance_token();
1488 this->decl(&Parse::type_spec, NULL);
1491 // TypeSpec = identifier Type .
1493 void
1494 Parse::type_spec(void*)
1496 const Token* token = this->peek_token();
1497 if (!token->is_identifier())
1499 error_at(this->location(), "expected identifier");
1500 return;
1502 std::string name = token->identifier();
1503 bool is_exported = token->is_identifier_exported();
1504 Location location = token->location();
1505 token = this->advance_token();
1507 // The scope of the type name starts at the point where the
1508 // identifier appears in the source code. We implement this by
1509 // declaring the type before we read the type definition.
1510 Named_object* named_type = NULL;
1511 if (name != "_")
1513 name = this->gogo_->pack_hidden_name(name, is_exported);
1514 named_type = this->gogo_->declare_type(name, location);
1517 Type* type;
1518 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1519 type = this->type();
1520 else
1522 error_at(this->location(),
1523 "unexpected semicolon or newline in type declaration");
1524 type = Type::make_error_type();
1525 this->advance_token();
1528 if (type->is_error_type())
1530 this->gogo_->mark_locals_used();
1531 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1532 && !this->peek_token()->is_eof())
1533 this->advance_token();
1536 if (name != "_")
1538 if (named_type->is_type_declaration())
1540 Type* ftype = type->forwarded();
1541 if (ftype->forward_declaration_type() != NULL
1542 && (ftype->forward_declaration_type()->named_object()
1543 == named_type))
1545 error_at(location, "invalid recursive type");
1546 type = Type::make_error_type();
1549 this->gogo_->define_type(named_type,
1550 Type::make_named_type(named_type, type,
1551 location));
1552 go_assert(named_type->package() == NULL);
1554 else
1556 // This will probably give a redefinition error.
1557 this->gogo_->add_type(name, type, location);
1562 // VarDecl = "var" Decl<VarSpec> .
1564 void
1565 Parse::var_decl()
1567 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1568 this->advance_token();
1569 this->decl(&Parse::var_spec, NULL);
1572 // VarSpec = IdentifierList
1573 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1575 void
1576 Parse::var_spec(void*)
1578 // Get the variable names.
1579 Typed_identifier_list til;
1580 this->identifier_list(&til);
1582 Location location = this->location();
1584 Type* type = NULL;
1585 Expression_list* init = NULL;
1586 if (!this->peek_token()->is_op(OPERATOR_EQ))
1588 type = this->type();
1589 if (type->is_error_type())
1591 this->gogo_->mark_locals_used();
1592 while (!this->peek_token()->is_op(OPERATOR_EQ)
1593 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1594 && !this->peek_token()->is_eof())
1595 this->advance_token();
1597 if (this->peek_token()->is_op(OPERATOR_EQ))
1599 this->advance_token();
1600 init = this->expression_list(NULL, false, true);
1603 else
1605 this->advance_token();
1606 init = this->expression_list(NULL, false, true);
1609 this->init_vars(&til, type, init, false, location);
1611 if (init != NULL)
1612 delete init;
1615 // Create variables. TIL is a list of variable names. If TYPE is not
1616 // NULL, it is the type of all the variables. If INIT is not NULL, it
1617 // is an initializer list for the variables.
1619 void
1620 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1621 Expression_list* init, bool is_coloneq,
1622 Location location)
1624 // Check for an initialization which can yield multiple values.
1625 if (init != NULL && init->size() == 1 && til->size() > 1)
1627 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1628 location))
1629 return;
1630 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1631 location))
1632 return;
1633 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1634 location))
1635 return;
1636 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1637 is_coloneq, location))
1638 return;
1641 if (init != NULL && init->size() != til->size())
1643 if (init->empty() || !init->front()->is_error_expression())
1644 error_at(location, "wrong number of initializations");
1645 init = NULL;
1646 if (type == NULL)
1647 type = Type::make_error_type();
1650 // Note that INIT was already parsed with the old name bindings, so
1651 // we don't have to worry that it will accidentally refer to the
1652 // newly declared variables. But we do have to worry about a mix of
1653 // newly declared variables and old variables if the old variables
1654 // appear in the initializations.
1656 Expression_list::const_iterator pexpr;
1657 if (init != NULL)
1658 pexpr = init->begin();
1659 bool any_new = false;
1660 Expression_list* vars = new Expression_list();
1661 Expression_list* vals = new Expression_list();
1662 for (Typed_identifier_list::const_iterator p = til->begin();
1663 p != til->end();
1664 ++p)
1666 if (init != NULL)
1667 go_assert(pexpr != init->end());
1668 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1669 false, &any_new, vars, vals);
1670 if (init != NULL)
1671 ++pexpr;
1673 if (init != NULL)
1674 go_assert(pexpr == init->end());
1675 if (is_coloneq && !any_new)
1676 error_at(location, "variables redeclared but no variable is new");
1677 this->finish_init_vars(vars, vals, location);
1680 // See if we need to initialize a list of variables from a function
1681 // call. This returns true if we have set up the variables and the
1682 // initialization.
1684 bool
1685 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1686 Expression* expr, bool is_coloneq,
1687 Location location)
1689 Call_expression* call = expr->call_expression();
1690 if (call == NULL)
1691 return false;
1693 // This is a function call. We can't check here whether it returns
1694 // the right number of values, but it might. Declare the variables,
1695 // and then assign the results of the call to them.
1697 call->set_expected_result_count(vars->size());
1699 Named_object* first_var = NULL;
1700 unsigned int index = 0;
1701 bool any_new = false;
1702 Expression_list* ivars = new Expression_list();
1703 Expression_list* ivals = new Expression_list();
1704 for (Typed_identifier_list::const_iterator pv = vars->begin();
1705 pv != vars->end();
1706 ++pv, ++index)
1708 Expression* init = Expression::make_call_result(call, index);
1709 Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
1710 &any_new, ivars, ivals);
1712 if (this->gogo_->in_global_scope() && no->is_variable())
1714 if (first_var == NULL)
1715 first_var = no;
1716 else
1718 // The subsequent vars have an implicit dependency on
1719 // the first one, so that everything gets initialized in
1720 // the right order and so that we detect cycles
1721 // correctly.
1722 this->gogo_->record_var_depends_on(no->var_value(), first_var);
1727 if (is_coloneq && !any_new)
1728 error_at(location, "variables redeclared but no variable is new");
1730 this->finish_init_vars(ivars, ivals, location);
1732 return true;
1735 // See if we need to initialize a pair of values from a map index
1736 // expression. This returns true if we have set up the variables and
1737 // the initialization.
1739 bool
1740 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1741 Expression* expr, bool is_coloneq,
1742 Location location)
1744 Index_expression* index = expr->index_expression();
1745 if (index == NULL)
1746 return false;
1747 if (vars->size() != 2)
1748 return false;
1750 // This is an index which is being assigned to two variables. It
1751 // must be a map index. Declare the variables, and then assign the
1752 // results of the map index.
1753 bool any_new = false;
1754 Typed_identifier_list::const_iterator p = vars->begin();
1755 Expression* init = type == NULL ? index : NULL;
1756 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1757 type == NULL, &any_new, NULL, NULL);
1758 if (type == NULL && any_new && val_no->is_variable())
1759 val_no->var_value()->set_type_from_init_tuple();
1760 Expression* val_var = Expression::make_var_reference(val_no, location);
1762 ++p;
1763 Type* var_type = type;
1764 if (var_type == NULL)
1765 var_type = Type::lookup_bool_type();
1766 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1767 &any_new, NULL, NULL);
1768 Expression* present_var = Expression::make_var_reference(no, location);
1770 if (is_coloneq && !any_new)
1771 error_at(location, "variables redeclared but no variable is new");
1773 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1774 index, location);
1776 if (!this->gogo_->in_global_scope())
1777 this->gogo_->add_statement(s);
1778 else if (!val_no->is_sink())
1780 if (val_no->is_variable())
1781 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1783 else if (!no->is_sink())
1785 if (no->is_variable())
1786 no->var_value()->add_preinit_statement(this->gogo_, s);
1788 else
1790 // Execute the map index expression just so that we can fail if
1791 // the map is nil.
1792 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1793 NULL, location);
1794 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1797 return true;
1800 // See if we need to initialize a pair of values from a receive
1801 // expression. This returns true if we have set up the variables and
1802 // the initialization.
1804 bool
1805 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1806 Expression* expr, bool is_coloneq,
1807 Location location)
1809 Receive_expression* receive = expr->receive_expression();
1810 if (receive == NULL)
1811 return false;
1812 if (vars->size() != 2)
1813 return false;
1815 // This is a receive expression which is being assigned to two
1816 // variables. Declare the variables, and then assign the results of
1817 // the receive.
1818 bool any_new = false;
1819 Typed_identifier_list::const_iterator p = vars->begin();
1820 Expression* init = type == NULL ? receive : NULL;
1821 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1822 type == NULL, &any_new, NULL, NULL);
1823 if (type == NULL && any_new && val_no->is_variable())
1824 val_no->var_value()->set_type_from_init_tuple();
1825 Expression* val_var = Expression::make_var_reference(val_no, location);
1827 ++p;
1828 Type* var_type = type;
1829 if (var_type == NULL)
1830 var_type = Type::lookup_bool_type();
1831 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1832 &any_new, NULL, NULL);
1833 Expression* received_var = Expression::make_var_reference(no, location);
1835 if (is_coloneq && !any_new)
1836 error_at(location, "variables redeclared but no variable is new");
1838 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1839 received_var,
1840 receive->channel(),
1841 location);
1843 if (!this->gogo_->in_global_scope())
1844 this->gogo_->add_statement(s);
1845 else if (!val_no->is_sink())
1847 if (val_no->is_variable())
1848 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1850 else if (!no->is_sink())
1852 if (no->is_variable())
1853 no->var_value()->add_preinit_statement(this->gogo_, s);
1855 else
1857 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1858 NULL, location);
1859 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1862 return true;
1865 // See if we need to initialize a pair of values from a type guard
1866 // expression. This returns true if we have set up the variables and
1867 // the initialization.
1869 bool
1870 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1871 Type* type, Expression* expr,
1872 bool is_coloneq, Location location)
1874 Type_guard_expression* type_guard = expr->type_guard_expression();
1875 if (type_guard == NULL)
1876 return false;
1877 if (vars->size() != 2)
1878 return false;
1880 // This is a type guard expression which is being assigned to two
1881 // variables. Declare the variables, and then assign the results of
1882 // the type guard.
1883 bool any_new = false;
1884 Typed_identifier_list::const_iterator p = vars->begin();
1885 Type* var_type = type;
1886 if (var_type == NULL)
1887 var_type = type_guard->type();
1888 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1889 &any_new, NULL, NULL);
1890 Expression* val_var = Expression::make_var_reference(val_no, location);
1892 ++p;
1893 var_type = type;
1894 if (var_type == NULL)
1895 var_type = Type::lookup_bool_type();
1896 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1897 &any_new, NULL, NULL);
1898 Expression* ok_var = Expression::make_var_reference(no, location);
1900 Expression* texpr = type_guard->expr();
1901 Type* t = type_guard->type();
1902 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1903 texpr, t,
1904 location);
1906 if (is_coloneq && !any_new)
1907 error_at(location, "variables redeclared but no variable is new");
1909 if (!this->gogo_->in_global_scope())
1910 this->gogo_->add_statement(s);
1911 else if (!val_no->is_sink())
1913 if (val_no->is_variable())
1914 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1916 else if (!no->is_sink())
1918 if (no->is_variable())
1919 no->var_value()->add_preinit_statement(this->gogo_, s);
1921 else
1923 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1924 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1927 return true;
1930 // Create a single variable. If IS_COLONEQ is true, we permit
1931 // redeclarations in the same block, and we set *IS_NEW when we find a
1932 // new variable which is not a redeclaration.
1934 Named_object*
1935 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1936 bool is_coloneq, bool type_from_init, bool* is_new,
1937 Expression_list* vars, Expression_list* vals)
1939 Location location = tid.location();
1941 if (Gogo::is_sink_name(tid.name()))
1943 if (!type_from_init && init != NULL)
1945 if (this->gogo_->in_global_scope())
1946 return this->create_dummy_global(type, init, location);
1947 else
1949 // Create a dummy variable so that we will check whether the
1950 // initializer can be assigned to the type.
1951 Variable* var = new Variable(type, init, false, false, false,
1952 location);
1953 var->set_is_used();
1954 static int count;
1955 char buf[30];
1956 snprintf(buf, sizeof buf, "sink$%d", count);
1957 ++count;
1958 return this->gogo_->add_variable(buf, var);
1961 if (type != NULL)
1962 this->gogo_->add_type_to_verify(type);
1963 return this->gogo_->add_sink();
1966 if (is_coloneq)
1968 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1969 if (no != NULL
1970 && (no->is_variable() || no->is_result_variable()))
1972 // INIT may be NULL even when IS_COLONEQ is true for cases
1973 // like v, ok := x.(int).
1974 if (!type_from_init && init != NULL)
1976 go_assert(vars != NULL && vals != NULL);
1977 vars->push_back(Expression::make_var_reference(no, location));
1978 vals->push_back(init);
1980 return no;
1983 *is_new = true;
1984 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1985 false, false, location);
1986 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1987 if (!no->is_variable())
1989 // The name is already defined, so we just gave an error.
1990 return this->gogo_->add_sink();
1992 return no;
1995 // Create a dummy global variable to force an initializer to be run in
1996 // the right place. This is used when a sink variable is initialized
1997 // at global scope.
1999 Named_object*
2000 Parse::create_dummy_global(Type* type, Expression* init,
2001 Location location)
2003 if (type == NULL && init == NULL)
2004 type = Type::lookup_bool_type();
2005 Variable* var = new Variable(type, init, true, false, false, location);
2006 static int count;
2007 char buf[30];
2008 snprintf(buf, sizeof buf, "_.%d", count);
2009 ++count;
2010 return this->gogo_->add_variable(buf, var);
2013 // Finish the variable initialization by executing any assignments to
2014 // existing variables when using :=. These must be done as a tuple
2015 // assignment in case of something like n, a, b := 1, b, a.
2017 void
2018 Parse::finish_init_vars(Expression_list* vars, Expression_list* vals,
2019 Location location)
2021 if (vars->empty())
2023 delete vars;
2024 delete vals;
2026 else if (vars->size() == 1)
2028 go_assert(!this->gogo_->in_global_scope());
2029 this->gogo_->add_statement(Statement::make_assignment(vars->front(),
2030 vals->front(),
2031 location));
2032 delete vars;
2033 delete vals;
2035 else
2037 go_assert(!this->gogo_->in_global_scope());
2038 this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals,
2039 location));
2043 // SimpleVarDecl = identifier ":=" Expression .
2045 // We've already seen the identifier.
2047 // FIXME: We also have to implement
2048 // IdentifierList ":=" ExpressionList
2049 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2050 // tuple assignments here as well.
2052 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2053 // side may be a composite literal.
2055 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2056 // RangeClause.
2058 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2059 // guard (var := expr.("type") using the literal keyword "type").
2061 void
2062 Parse::simple_var_decl_or_assignment(const std::string& name,
2063 Location location,
2064 bool may_be_composite_lit,
2065 Range_clause* p_range_clause,
2066 Type_switch* p_type_switch)
2068 Typed_identifier_list til;
2069 til.push_back(Typed_identifier(name, NULL, location));
2071 // We've seen one identifier. If we see a comma now, this could be
2072 // "a, *p = 1, 2".
2073 if (this->peek_token()->is_op(OPERATOR_COMMA))
2075 go_assert(p_type_switch == NULL);
2076 while (true)
2078 const Token* token = this->advance_token();
2079 if (!token->is_identifier())
2080 break;
2082 std::string id = token->identifier();
2083 bool is_id_exported = token->is_identifier_exported();
2084 Location id_location = token->location();
2086 token = this->advance_token();
2087 if (!token->is_op(OPERATOR_COMMA))
2089 if (token->is_op(OPERATOR_COLONEQ))
2091 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2092 til.push_back(Typed_identifier(id, NULL, location));
2094 else
2095 this->unget_token(Token::make_identifier_token(id,
2096 is_id_exported,
2097 id_location));
2098 break;
2101 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2102 til.push_back(Typed_identifier(id, NULL, location));
2105 // We have a comma separated list of identifiers in TIL. If the
2106 // next token is COLONEQ, then this is a simple var decl, and we
2107 // have the complete list of identifiers. If the next token is
2108 // not COLONEQ, then the only valid parse is a tuple assignment.
2109 // The list of identifiers we have so far is really a list of
2110 // expressions. There are more expressions following.
2112 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2114 Expression_list* exprs = new Expression_list;
2115 for (Typed_identifier_list::const_iterator p = til.begin();
2116 p != til.end();
2117 ++p)
2118 exprs->push_back(this->id_to_expression(p->name(), p->location(),
2119 true));
2121 Expression_list* more_exprs =
2122 this->expression_list(NULL, true, may_be_composite_lit);
2123 for (Expression_list::const_iterator p = more_exprs->begin();
2124 p != more_exprs->end();
2125 ++p)
2126 exprs->push_back(*p);
2127 delete more_exprs;
2129 this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
2130 return;
2134 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2135 const Token* token = this->advance_token();
2137 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2139 this->range_clause_decl(&til, p_range_clause);
2140 return;
2143 Expression_list* init;
2144 if (p_type_switch == NULL)
2145 init = this->expression_list(NULL, false, may_be_composite_lit);
2146 else
2148 bool is_type_switch = false;
2149 Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
2150 may_be_composite_lit,
2151 &is_type_switch, NULL);
2152 if (is_type_switch)
2154 p_type_switch->found = true;
2155 p_type_switch->name = name;
2156 p_type_switch->location = location;
2157 p_type_switch->expr = expr;
2158 return;
2161 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2163 init = new Expression_list();
2164 init->push_back(expr);
2166 else
2168 this->advance_token();
2169 init = this->expression_list(expr, false, may_be_composite_lit);
2173 this->init_vars(&til, NULL, init, true, location);
2176 // FunctionDecl = "func" identifier Signature [ Block ] .
2177 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2179 // Deprecated gcc extension:
2180 // FunctionDecl = "func" identifier Signature
2181 // __asm__ "(" string_lit ")" .
2182 // This extension means a function whose real name is the identifier
2183 // inside the asm. This extension will be removed at some future
2184 // date. It has been replaced with //extern comments.
2186 // SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment,
2187 // which means that we omit the method from the type descriptor.
2189 void
2190 Parse::function_decl(bool saw_nointerface)
2192 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2193 Location location = this->location();
2194 std::string extern_name = this->lex_->extern_name();
2195 const Token* token = this->advance_token();
2197 Typed_identifier* rec = NULL;
2198 if (token->is_op(OPERATOR_LPAREN))
2200 rec = this->receiver();
2201 token = this->peek_token();
2203 else if (saw_nointerface)
2205 warning_at(location, 0,
2206 "ignoring magic //go:nointerface comment before non-method");
2207 saw_nointerface = false;
2210 if (!token->is_identifier())
2212 error_at(this->location(), "expected function name");
2213 return;
2216 std::string name =
2217 this->gogo_->pack_hidden_name(token->identifier(),
2218 token->is_identifier_exported());
2220 this->advance_token();
2222 Function_type* fntype = this->signature(rec, this->location());
2224 Named_object* named_object = NULL;
2226 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2228 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2230 error_at(this->location(), "expected %<(%>");
2231 return;
2233 token = this->advance_token();
2234 if (!token->is_string())
2236 error_at(this->location(), "expected string");
2237 return;
2239 std::string asm_name = token->string_value();
2240 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2242 error_at(this->location(), "expected %<)%>");
2243 return;
2245 this->advance_token();
2246 if (!Gogo::is_sink_name(name))
2248 named_object = this->gogo_->declare_function(name, fntype, location);
2249 if (named_object->is_function_declaration())
2250 named_object->func_declaration_value()->set_asm_name(asm_name);
2254 // Check for the easy error of a newline before the opening brace.
2255 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2257 Location semi_loc = this->location();
2258 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2259 error_at(this->location(),
2260 "unexpected semicolon or newline before %<{%>");
2261 else
2262 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2263 semi_loc));
2266 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2268 if (named_object == NULL && !Gogo::is_sink_name(name))
2270 if (fntype == NULL)
2271 this->gogo_->add_erroneous_name(name);
2272 else
2274 named_object = this->gogo_->declare_function(name, fntype,
2275 location);
2276 if (!extern_name.empty()
2277 && named_object->is_function_declaration())
2279 Function_declaration* fd =
2280 named_object->func_declaration_value();
2281 fd->set_asm_name(extern_name);
2286 if (saw_nointerface)
2287 warning_at(location, 0,
2288 ("ignoring magic //go:nointerface comment "
2289 "before declaration"));
2291 else
2293 bool hold_is_erroneous_function = this->is_erroneous_function_;
2294 if (fntype == NULL)
2296 fntype = Type::make_function_type(NULL, NULL, NULL, location);
2297 this->is_erroneous_function_ = true;
2298 if (!Gogo::is_sink_name(name))
2299 this->gogo_->add_erroneous_name(name);
2300 name = this->gogo_->pack_hidden_name("_", false);
2302 named_object = this->gogo_->start_function(name, fntype, true, location);
2303 Location end_loc = this->block();
2304 this->gogo_->finish_function(end_loc);
2305 if (saw_nointerface
2306 && !this->is_erroneous_function_
2307 && named_object->is_function())
2308 named_object->func_value()->set_nointerface();
2309 this->is_erroneous_function_ = hold_is_erroneous_function;
2313 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2314 // BaseTypeName = identifier .
2316 Typed_identifier*
2317 Parse::receiver()
2319 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2321 std::string name;
2322 const Token* token = this->advance_token();
2323 Location location = token->location();
2324 if (!token->is_op(OPERATOR_MULT))
2326 if (!token->is_identifier())
2328 error_at(this->location(), "method has no receiver");
2329 this->gogo_->mark_locals_used();
2330 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2331 token = this->advance_token();
2332 if (!token->is_eof())
2333 this->advance_token();
2334 return NULL;
2336 name = token->identifier();
2337 bool is_exported = token->is_identifier_exported();
2338 token = this->advance_token();
2339 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2341 // An identifier followed by something other than a dot or a
2342 // right parenthesis must be a receiver name followed by a
2343 // type.
2344 name = this->gogo_->pack_hidden_name(name, is_exported);
2346 else
2348 // This must be a type name.
2349 this->unget_token(Token::make_identifier_token(name, is_exported,
2350 location));
2351 token = this->peek_token();
2352 name.clear();
2356 // Here the receiver name is in NAME (it is empty if the receiver is
2357 // unnamed) and TOKEN is the first token in the type.
2359 bool is_pointer = false;
2360 if (token->is_op(OPERATOR_MULT))
2362 is_pointer = true;
2363 token = this->advance_token();
2366 if (!token->is_identifier())
2368 error_at(this->location(), "expected receiver name or type");
2369 this->gogo_->mark_locals_used();
2370 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2371 while (!token->is_eof())
2373 token = this->advance_token();
2374 if (token->is_op(OPERATOR_LPAREN))
2375 ++c;
2376 else if (token->is_op(OPERATOR_RPAREN))
2378 if (c == 0)
2379 break;
2380 --c;
2383 if (!token->is_eof())
2384 this->advance_token();
2385 return NULL;
2388 Type* type = this->type_name(true);
2390 if (is_pointer && !type->is_error_type())
2391 type = Type::make_pointer_type(type);
2393 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2394 this->advance_token();
2395 else
2397 if (this->peek_token()->is_op(OPERATOR_COMMA))
2398 error_at(this->location(), "method has multiple receivers");
2399 else
2400 error_at(this->location(), "expected %<)%>");
2401 this->gogo_->mark_locals_used();
2402 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2403 token = this->advance_token();
2404 if (!token->is_eof())
2405 this->advance_token();
2406 return NULL;
2409 return new Typed_identifier(name, type, location);
2412 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2413 // Literal = BasicLit | CompositeLit | FunctionLit .
2414 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2416 // If MAY_BE_SINK is true, this operand may be "_".
2418 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2419 // if the entire expression is in parentheses.
2421 Expression*
2422 Parse::operand(bool may_be_sink, bool* is_parenthesized)
2424 const Token* token = this->peek_token();
2425 Expression* ret;
2426 switch (token->classification())
2428 case Token::TOKEN_IDENTIFIER:
2430 Location location = token->location();
2431 std::string id = token->identifier();
2432 bool is_exported = token->is_identifier_exported();
2433 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2435 Named_object* in_function;
2436 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2438 Package* package = NULL;
2439 if (named_object != NULL && named_object->is_package())
2441 if (!this->advance_token()->is_op(OPERATOR_DOT)
2442 || !this->advance_token()->is_identifier())
2444 error_at(location, "unexpected reference to package");
2445 return Expression::make_error(location);
2447 package = named_object->package_value();
2448 package->set_used();
2449 id = this->peek_token()->identifier();
2450 is_exported = this->peek_token()->is_identifier_exported();
2451 packed = this->gogo_->pack_hidden_name(id, is_exported);
2452 named_object = package->lookup(packed);
2453 location = this->location();
2454 go_assert(in_function == NULL);
2457 this->advance_token();
2459 if (named_object != NULL
2460 && named_object->is_type()
2461 && !named_object->type_value()->is_visible())
2463 go_assert(package != NULL);
2464 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2465 Gogo::message_name(package->package_name()).c_str(),
2466 Gogo::message_name(id).c_str());
2467 return Expression::make_error(location);
2471 if (named_object == NULL)
2473 if (package != NULL)
2475 std::string n1 = Gogo::message_name(package->package_name());
2476 std::string n2 = Gogo::message_name(id);
2477 if (!is_exported)
2478 error_at(location,
2479 ("invalid reference to unexported identifier "
2480 "%<%s.%s%>"),
2481 n1.c_str(), n2.c_str());
2482 else
2483 error_at(location,
2484 "reference to undefined identifier %<%s.%s%>",
2485 n1.c_str(), n2.c_str());
2486 return Expression::make_error(location);
2489 named_object = this->gogo_->add_unknown_name(packed, location);
2492 if (in_function != NULL
2493 && in_function != this->gogo_->current_function()
2494 && (named_object->is_variable()
2495 || named_object->is_result_variable()))
2496 return this->enclosing_var_reference(in_function, named_object,
2497 location);
2499 switch (named_object->classification())
2501 case Named_object::NAMED_OBJECT_CONST:
2502 return Expression::make_const_reference(named_object, location);
2503 case Named_object::NAMED_OBJECT_TYPE:
2504 return Expression::make_type(named_object->type_value(), location);
2505 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2507 Type* t = Type::make_forward_declaration(named_object);
2508 return Expression::make_type(t, location);
2510 case Named_object::NAMED_OBJECT_VAR:
2511 case Named_object::NAMED_OBJECT_RESULT_VAR:
2512 // Any left-hand-side can be a sink, so if this can not be
2513 // a sink, then it must be a use of the variable.
2514 if (!may_be_sink)
2515 this->mark_var_used(named_object);
2516 return Expression::make_var_reference(named_object, location);
2517 case Named_object::NAMED_OBJECT_SINK:
2518 if (may_be_sink)
2519 return Expression::make_sink(location);
2520 else
2522 error_at(location, "cannot use _ as value");
2523 return Expression::make_error(location);
2525 case Named_object::NAMED_OBJECT_FUNC:
2526 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2527 return Expression::make_func_reference(named_object, NULL,
2528 location);
2529 case Named_object::NAMED_OBJECT_UNKNOWN:
2531 Unknown_expression* ue =
2532 Expression::make_unknown_reference(named_object, location);
2533 if (this->is_erroneous_function_)
2534 ue->set_no_error_message();
2535 return ue;
2537 case Named_object::NAMED_OBJECT_ERRONEOUS:
2538 return Expression::make_error(location);
2539 default:
2540 go_unreachable();
2543 go_unreachable();
2545 case Token::TOKEN_STRING:
2546 ret = Expression::make_string(token->string_value(), token->location());
2547 this->advance_token();
2548 return ret;
2550 case Token::TOKEN_CHARACTER:
2551 ret = Expression::make_character(token->character_value(), NULL,
2552 token->location());
2553 this->advance_token();
2554 return ret;
2556 case Token::TOKEN_INTEGER:
2557 ret = Expression::make_integer(token->integer_value(), NULL,
2558 token->location());
2559 this->advance_token();
2560 return ret;
2562 case Token::TOKEN_FLOAT:
2563 ret = Expression::make_float(token->float_value(), NULL,
2564 token->location());
2565 this->advance_token();
2566 return ret;
2568 case Token::TOKEN_IMAGINARY:
2570 mpfr_t zero;
2571 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2572 ret = Expression::make_complex(&zero, token->imaginary_value(),
2573 NULL, token->location());
2574 mpfr_clear(zero);
2575 this->advance_token();
2576 return ret;
2579 case Token::TOKEN_KEYWORD:
2580 switch (token->keyword())
2582 case KEYWORD_FUNC:
2583 return this->function_lit();
2584 case KEYWORD_CHAN:
2585 case KEYWORD_INTERFACE:
2586 case KEYWORD_MAP:
2587 case KEYWORD_STRUCT:
2589 Location location = token->location();
2590 return Expression::make_type(this->type(), location);
2592 default:
2593 break;
2595 break;
2597 case Token::TOKEN_OPERATOR:
2598 if (token->is_op(OPERATOR_LPAREN))
2600 this->advance_token();
2601 ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL,
2602 NULL);
2603 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2604 error_at(this->location(), "missing %<)%>");
2605 else
2606 this->advance_token();
2607 if (is_parenthesized != NULL)
2608 *is_parenthesized = true;
2609 return ret;
2611 else if (token->is_op(OPERATOR_LSQUARE))
2613 // Here we call array_type directly, as this is the only
2614 // case where an ellipsis is permitted for an array type.
2615 Location location = token->location();
2616 return Expression::make_type(this->array_type(true), location);
2618 break;
2620 default:
2621 break;
2624 error_at(this->location(), "expected operand");
2625 return Expression::make_error(this->location());
2628 // Handle a reference to a variable in an enclosing function. We add
2629 // it to a list of such variables. We return a reference to a field
2630 // in a struct which will be passed on the static chain when calling
2631 // the current function.
2633 Expression*
2634 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2635 Location location)
2637 go_assert(var->is_variable() || var->is_result_variable());
2639 this->mark_var_used(var);
2641 Named_object* this_function = this->gogo_->current_function();
2642 Named_object* closure = this_function->func_value()->closure_var();
2644 // The last argument to the Enclosing_var constructor is the index
2645 // of this variable in the closure. We add 1 to the current number
2646 // of enclosed variables, because the first field in the closure
2647 // points to the function code.
2648 Enclosing_var ev(var, in_function, this->enclosing_vars_.size() + 1);
2649 std::pair<Enclosing_vars::iterator, bool> ins =
2650 this->enclosing_vars_.insert(ev);
2651 if (ins.second)
2653 // This is a variable we have not seen before. Add a new field
2654 // to the closure type.
2655 this_function->func_value()->add_closure_field(var, location);
2658 Expression* closure_ref = Expression::make_var_reference(closure,
2659 location);
2660 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2662 // The closure structure holds pointers to the variables, so we need
2663 // to introduce an indirection.
2664 Expression* e = Expression::make_field_reference(closure_ref,
2665 ins.first->index(),
2666 location);
2667 e = Expression::make_unary(OPERATOR_MULT, e, location);
2668 return e;
2671 // CompositeLit = LiteralType LiteralValue .
2672 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2673 // SliceType | MapType | TypeName .
2674 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2675 // ElementList = Element { "," Element } .
2676 // Element = [ Key ":" ] Value .
2677 // Key = FieldName | ElementIndex .
2678 // FieldName = identifier .
2679 // ElementIndex = Expression .
2680 // Value = Expression | LiteralValue .
2682 // We have already seen the type if there is one, and we are now
2683 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2684 // will be seen here as an array type whose length is "nil". The
2685 // DEPTH parameter is non-zero if this is an embedded composite
2686 // literal and the type was omitted. It gives the number of steps up
2687 // to the type which was provided. E.g., in [][]int{{1}} it will be
2688 // 1. In [][][]int{{{1}}} it will be 2.
2690 Expression*
2691 Parse::composite_lit(Type* type, int depth, Location location)
2693 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2694 this->advance_token();
2696 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2698 this->advance_token();
2699 return Expression::make_composite_literal(type, depth, false, NULL,
2700 false, location);
2703 bool has_keys = false;
2704 bool all_are_names = true;
2705 Expression_list* vals = new Expression_list;
2706 while (true)
2708 Expression* val;
2709 bool is_type_omitted = false;
2710 bool is_name = false;
2712 const Token* token = this->peek_token();
2714 if (token->is_identifier())
2716 std::string identifier = token->identifier();
2717 bool is_exported = token->is_identifier_exported();
2718 Location location = token->location();
2720 if (this->advance_token()->is_op(OPERATOR_COLON))
2722 // This may be a field name. We don't know for sure--it
2723 // could also be an expression for an array index. We
2724 // don't want to parse it as an expression because may
2725 // trigger various errors, e.g., if this identifier
2726 // happens to be the name of a package.
2727 Gogo* gogo = this->gogo_;
2728 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2729 is_exported),
2730 location, false);
2731 is_name = true;
2733 else
2735 this->unget_token(Token::make_identifier_token(identifier,
2736 is_exported,
2737 location));
2738 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
2739 NULL);
2742 else if (!token->is_op(OPERATOR_LCURLY))
2743 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2744 else
2746 // This must be a composite literal inside another composite
2747 // literal, with the type omitted for the inner one.
2748 val = this->composite_lit(type, depth + 1, token->location());
2749 is_type_omitted = true;
2752 token = this->peek_token();
2753 if (!token->is_op(OPERATOR_COLON))
2755 if (has_keys)
2756 vals->push_back(NULL);
2757 is_name = false;
2759 else
2761 if (is_type_omitted && !val->is_error_expression())
2763 error_at(this->location(), "unexpected %<:%>");
2764 val = Expression::make_error(this->location());
2767 this->advance_token();
2769 if (!has_keys && !vals->empty())
2771 Expression_list* newvals = new Expression_list;
2772 for (Expression_list::const_iterator p = vals->begin();
2773 p != vals->end();
2774 ++p)
2776 newvals->push_back(NULL);
2777 newvals->push_back(*p);
2779 delete vals;
2780 vals = newvals;
2782 has_keys = true;
2784 if (val->unknown_expression() != NULL)
2785 val->unknown_expression()->set_is_composite_literal_key();
2787 vals->push_back(val);
2789 if (!token->is_op(OPERATOR_LCURLY))
2790 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2791 else
2793 // This must be a composite literal inside another
2794 // composite literal, with the type omitted for the
2795 // inner one.
2796 val = this->composite_lit(type, depth + 1, token->location());
2799 token = this->peek_token();
2802 vals->push_back(val);
2804 if (!is_name)
2805 all_are_names = false;
2807 if (token->is_op(OPERATOR_COMMA))
2809 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2811 this->advance_token();
2812 break;
2815 else if (token->is_op(OPERATOR_RCURLY))
2817 this->advance_token();
2818 break;
2820 else
2822 if (token->is_op(OPERATOR_SEMICOLON))
2823 error_at(this->location(),
2824 "need trailing comma before newline in composite literal");
2825 else
2826 error_at(this->location(), "expected %<,%> or %<}%>");
2828 this->gogo_->mark_locals_used();
2829 int depth = 0;
2830 while (!token->is_eof()
2831 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2833 if (token->is_op(OPERATOR_LCURLY))
2834 ++depth;
2835 else if (token->is_op(OPERATOR_RCURLY))
2836 --depth;
2837 token = this->advance_token();
2839 if (token->is_op(OPERATOR_RCURLY))
2840 this->advance_token();
2842 return Expression::make_error(location);
2846 return Expression::make_composite_literal(type, depth, has_keys, vals,
2847 all_are_names, location);
2850 // FunctionLit = "func" Signature Block .
2852 Expression*
2853 Parse::function_lit()
2855 Location location = this->location();
2856 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2857 this->advance_token();
2859 Enclosing_vars hold_enclosing_vars;
2860 hold_enclosing_vars.swap(this->enclosing_vars_);
2862 Function_type* type = this->signature(NULL, location);
2863 bool fntype_is_error = false;
2864 if (type == NULL)
2866 type = Type::make_function_type(NULL, NULL, NULL, location);
2867 fntype_is_error = true;
2870 // For a function literal, the next token must be a '{'. If we
2871 // don't see that, then we may have a type expression.
2872 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2873 return Expression::make_type(type, location);
2875 bool hold_is_erroneous_function = this->is_erroneous_function_;
2876 if (fntype_is_error)
2877 this->is_erroneous_function_ = true;
2879 Bc_stack* hold_break_stack = this->break_stack_;
2880 Bc_stack* hold_continue_stack = this->continue_stack_;
2881 this->break_stack_ = NULL;
2882 this->continue_stack_ = NULL;
2884 Named_object* no = this->gogo_->start_function("", type, true, location);
2886 Location end_loc = this->block();
2888 this->gogo_->finish_function(end_loc);
2890 if (this->break_stack_ != NULL)
2891 delete this->break_stack_;
2892 if (this->continue_stack_ != NULL)
2893 delete this->continue_stack_;
2894 this->break_stack_ = hold_break_stack;
2895 this->continue_stack_ = hold_continue_stack;
2897 this->is_erroneous_function_ = hold_is_erroneous_function;
2899 hold_enclosing_vars.swap(this->enclosing_vars_);
2901 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2902 location);
2904 return Expression::make_func_reference(no, closure, location);
2907 // Create a closure for the nested function FUNCTION. This is based
2908 // on ENCLOSING_VARS, which is a list of all variables defined in
2909 // enclosing functions and referenced from FUNCTION. A closure is the
2910 // address of a struct which point to the real function code and
2911 // contains the addresses of all the referenced variables. This
2912 // returns NULL if no closure is required.
2914 Expression*
2915 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2916 Location location)
2918 if (enclosing_vars->empty())
2919 return NULL;
2921 // Get the variables in order by their field index.
2923 size_t enclosing_var_count = enclosing_vars->size();
2924 std::vector<Enclosing_var> ev(enclosing_var_count);
2925 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2926 p != enclosing_vars->end();
2927 ++p)
2929 // Subtract 1 because index 0 is the function code.
2930 ev[p->index() - 1] = *p;
2933 // Build an initializer for a composite literal of the closure's
2934 // type.
2936 Named_object* enclosing_function = this->gogo_->current_function();
2937 Expression_list* initializer = new Expression_list;
2939 initializer->push_back(Expression::make_func_code_reference(function,
2940 location));
2942 for (size_t i = 0; i < enclosing_var_count; ++i)
2944 // Add 1 to i because the first field in the closure is a
2945 // pointer to the function code.
2946 go_assert(ev[i].index() == i + 1);
2947 Named_object* var = ev[i].var();
2948 Expression* ref;
2949 if (ev[i].in_function() == enclosing_function)
2950 ref = Expression::make_var_reference(var, location);
2951 else
2952 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2953 location);
2954 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2955 location);
2956 initializer->push_back(refaddr);
2959 Named_object* closure_var = function->func_value()->closure_var();
2960 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2961 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2962 location);
2963 return Expression::make_heap_expression(cv, location);
2966 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2968 // If MAY_BE_SINK is true, this expression may be "_".
2970 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2971 // literal.
2973 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2974 // guard (var := expr.("type") using the literal keyword "type").
2976 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2977 // if the entire expression is in parentheses.
2979 Expression*
2980 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2981 bool* is_type_switch, bool* is_parenthesized)
2983 Location start_loc = this->location();
2984 bool operand_is_parenthesized = false;
2985 bool whole_is_parenthesized = false;
2987 Expression* ret = this->operand(may_be_sink, &operand_is_parenthesized);
2989 whole_is_parenthesized = operand_is_parenthesized;
2991 // An unknown name followed by a curly brace must be a composite
2992 // literal, and the unknown name must be a type.
2993 if (may_be_composite_lit
2994 && !operand_is_parenthesized
2995 && ret->unknown_expression() != NULL
2996 && this->peek_token()->is_op(OPERATOR_LCURLY))
2998 Named_object* no = ret->unknown_expression()->named_object();
2999 Type* type = Type::make_forward_declaration(no);
3000 ret = Expression::make_type(type, ret->location());
3003 // We handle composite literals and type casts here, as it is the
3004 // easiest way to handle types which are in parentheses, as in
3005 // "((uint))(1)".
3006 if (ret->is_type_expression())
3008 if (this->peek_token()->is_op(OPERATOR_LCURLY))
3010 whole_is_parenthesized = false;
3011 if (!may_be_composite_lit)
3013 Type* t = ret->type();
3014 if (t->named_type() != NULL
3015 || t->forward_declaration_type() != NULL)
3016 error_at(start_loc,
3017 _("parentheses required around this composite literal "
3018 "to avoid parsing ambiguity"));
3020 else if (operand_is_parenthesized)
3021 error_at(start_loc,
3022 "cannot parenthesize type in composite literal");
3023 ret = this->composite_lit(ret->type(), 0, ret->location());
3025 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
3027 whole_is_parenthesized = false;
3028 Location loc = this->location();
3029 this->advance_token();
3030 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
3031 NULL, NULL);
3032 if (this->peek_token()->is_op(OPERATOR_COMMA))
3033 this->advance_token();
3034 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
3036 error_at(this->location(),
3037 "invalid use of %<...%> in type conversion");
3038 this->advance_token();
3040 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3041 error_at(this->location(), "expected %<)%>");
3042 else
3043 this->advance_token();
3044 if (expr->is_error_expression())
3045 ret = expr;
3046 else
3048 Type* t = ret->type();
3049 if (t->classification() == Type::TYPE_ARRAY
3050 && t->array_type()->length() != NULL
3051 && t->array_type()->length()->is_nil_expression())
3053 error_at(ret->location(),
3054 "use of %<[...]%> outside of array literal");
3055 ret = Expression::make_error(loc);
3057 else
3058 ret = Expression::make_cast(t, expr, loc);
3063 while (true)
3065 const Token* token = this->peek_token();
3066 if (token->is_op(OPERATOR_LPAREN))
3068 whole_is_parenthesized = false;
3069 ret = this->call(this->verify_not_sink(ret));
3071 else if (token->is_op(OPERATOR_DOT))
3073 whole_is_parenthesized = false;
3074 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
3075 if (is_type_switch != NULL && *is_type_switch)
3076 break;
3078 else if (token->is_op(OPERATOR_LSQUARE))
3080 whole_is_parenthesized = false;
3081 ret = this->index(this->verify_not_sink(ret));
3083 else
3084 break;
3087 if (whole_is_parenthesized && is_parenthesized != NULL)
3088 *is_parenthesized = true;
3090 return ret;
3093 // Selector = "." identifier .
3094 // TypeGuard = "." "(" QualifiedIdent ")" .
3096 // Note that Operand can expand to QualifiedIdent, which contains a
3097 // ".". That is handled directly in operand when it sees a package
3098 // name.
3100 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3101 // guard (var := expr.("type") using the literal keyword "type").
3103 Expression*
3104 Parse::selector(Expression* left, bool* is_type_switch)
3106 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
3107 Location location = this->location();
3109 const Token* token = this->advance_token();
3110 if (token->is_identifier())
3112 // This could be a field in a struct, or a method in an
3113 // interface, or a method associated with a type. We can't know
3114 // which until we have seen all the types.
3115 std::string name =
3116 this->gogo_->pack_hidden_name(token->identifier(),
3117 token->is_identifier_exported());
3118 if (token->identifier() == "_")
3120 error_at(this->location(), "invalid use of %<_%>");
3121 name = Gogo::erroneous_name();
3123 this->advance_token();
3124 return Expression::make_selector(left, name, location);
3126 else if (token->is_op(OPERATOR_LPAREN))
3128 this->advance_token();
3129 Type* type = NULL;
3130 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
3131 type = this->type();
3132 else
3134 if (is_type_switch != NULL)
3135 *is_type_switch = true;
3136 else
3138 error_at(this->location(),
3139 "use of %<.(type)%> outside type switch");
3140 type = Type::make_error_type();
3142 this->advance_token();
3144 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3145 error_at(this->location(), "missing %<)%>");
3146 else
3147 this->advance_token();
3148 if (is_type_switch != NULL && *is_type_switch)
3149 return left;
3150 return Expression::make_type_guard(left, type, location);
3152 else
3154 error_at(this->location(), "expected identifier or %<(%>");
3155 return left;
3159 // Index = "[" Expression "]" .
3160 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3162 Expression*
3163 Parse::index(Expression* expr)
3165 Location location = this->location();
3166 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
3167 this->advance_token();
3169 Expression* start;
3170 if (!this->peek_token()->is_op(OPERATOR_COLON))
3171 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3172 else
3174 mpz_t zero;
3175 mpz_init_set_ui(zero, 0);
3176 start = Expression::make_integer(&zero, NULL, location);
3177 mpz_clear(zero);
3180 Expression* end = NULL;
3181 if (this->peek_token()->is_op(OPERATOR_COLON))
3183 // We use nil to indicate a missing high expression.
3184 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3185 end = Expression::make_nil(this->location());
3186 else if (this->peek_token()->is_op(OPERATOR_COLON))
3188 error_at(this->location(), "middle index required in 3-index slice");
3189 end = Expression::make_error(this->location());
3191 else
3192 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3195 Expression* cap = NULL;
3196 if (this->peek_token()->is_op(OPERATOR_COLON))
3198 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3200 error_at(this->location(), "final index required in 3-index slice");
3201 cap = Expression::make_error(this->location());
3203 else
3204 cap = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3206 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
3207 error_at(this->location(), "missing %<]%>");
3208 else
3209 this->advance_token();
3210 return Expression::make_index(expr, start, end, cap, location);
3213 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3214 // ArgumentList = ExpressionList [ "..." ] .
3216 Expression*
3217 Parse::call(Expression* func)
3219 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
3220 Expression_list* args = NULL;
3221 bool is_varargs = false;
3222 const Token* token = this->advance_token();
3223 if (!token->is_op(OPERATOR_RPAREN))
3225 args = this->expression_list(NULL, false, true);
3226 token = this->peek_token();
3227 if (token->is_op(OPERATOR_ELLIPSIS))
3229 is_varargs = true;
3230 token = this->advance_token();
3233 if (token->is_op(OPERATOR_COMMA))
3234 token = this->advance_token();
3235 if (!token->is_op(OPERATOR_RPAREN))
3236 error_at(this->location(), "missing %<)%>");
3237 else
3238 this->advance_token();
3239 if (func->is_error_expression())
3240 return func;
3241 return Expression::make_call(func, args, is_varargs, func->location());
3244 // Return an expression for a single unqualified identifier.
3246 Expression*
3247 Parse::id_to_expression(const std::string& name, Location location,
3248 bool is_lhs)
3250 Named_object* in_function;
3251 Named_object* named_object = this->gogo_->lookup(name, &in_function);
3252 if (named_object == NULL)
3253 named_object = this->gogo_->add_unknown_name(name, location);
3255 if (in_function != NULL
3256 && in_function != this->gogo_->current_function()
3257 && (named_object->is_variable() || named_object->is_result_variable()))
3258 return this->enclosing_var_reference(in_function, named_object,
3259 location);
3261 switch (named_object->classification())
3263 case Named_object::NAMED_OBJECT_CONST:
3264 return Expression::make_const_reference(named_object, location);
3265 case Named_object::NAMED_OBJECT_VAR:
3266 case Named_object::NAMED_OBJECT_RESULT_VAR:
3267 if (!is_lhs)
3268 this->mark_var_used(named_object);
3269 return Expression::make_var_reference(named_object, location);
3270 case Named_object::NAMED_OBJECT_SINK:
3271 return Expression::make_sink(location);
3272 case Named_object::NAMED_OBJECT_FUNC:
3273 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3274 return Expression::make_func_reference(named_object, NULL, location);
3275 case Named_object::NAMED_OBJECT_UNKNOWN:
3277 Unknown_expression* ue =
3278 Expression::make_unknown_reference(named_object, location);
3279 if (this->is_erroneous_function_)
3280 ue->set_no_error_message();
3281 return ue;
3283 case Named_object::NAMED_OBJECT_PACKAGE:
3284 case Named_object::NAMED_OBJECT_TYPE:
3285 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3287 // These cases can arise for a field name in a composite
3288 // literal.
3289 Unknown_expression* ue =
3290 Expression::make_unknown_reference(named_object, location);
3291 if (this->is_erroneous_function_)
3292 ue->set_no_error_message();
3293 return ue;
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);
3303 // Expression = UnaryExpr { binary_op Expression } .
3305 // PRECEDENCE is the precedence of the current operator.
3307 // If MAY_BE_SINK is true, this expression may be "_".
3309 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3310 // literal.
3312 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3313 // guard (var := expr.("type") using the literal keyword "type").
3315 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3316 // if the entire expression is in parentheses.
3318 Expression*
3319 Parse::expression(Precedence precedence, bool may_be_sink,
3320 bool may_be_composite_lit, bool* is_type_switch,
3321 bool *is_parenthesized)
3323 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3324 is_type_switch, is_parenthesized);
3326 while (true)
3328 if (is_type_switch != NULL && *is_type_switch)
3329 return left;
3331 const Token* token = this->peek_token();
3332 if (token->classification() != Token::TOKEN_OPERATOR)
3334 // Not a binary_op.
3335 return left;
3338 Precedence right_precedence;
3339 switch (token->op())
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;
3375 if (right_precedence == PRECEDENCE_INVALID)
3377 // Not a binary_op.
3378 return left;
3381 if (is_parenthesized != NULL)
3382 *is_parenthesized = false;
3384 Operator op = token->op();
3385 Location binop_location = token->location();
3387 if (precedence >= right_precedence)
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;
3394 this->advance_token();
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);
3404 bool
3405 Parse::expression_may_start_here()
3407 const Token* token = this->peek_token();
3408 switch (token->classification())
3410 case Token::TOKEN_INVALID:
3411 case Token::TOKEN_EOF:
3412 return false;
3413 case Token::TOKEN_KEYWORD:
3414 switch (token->keyword())
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;
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())
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;
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();
3455 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3457 // If MAY_BE_SINK is true, this expression may be "_".
3459 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3460 // literal.
3462 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3463 // guard (var := expr.("type") using the literal keyword "type").
3465 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3466 // if the entire expression is in parentheses.
3468 Expression*
3469 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3470 bool* is_type_switch, bool* is_parenthesized)
3472 const Token* token = this->peek_token();
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))
3483 Location location = token->location();
3484 if (this->advance_token()->is_keyword(KEYWORD_CHAN))
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
3494 if (expr->type()->is_error_type())
3495 return expr;
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)
3502 // This is probably impossible.
3503 error_at(location, "expected channel type");
3504 return Expression::make_error(location);
3506 else if (ct->may_receive())
3508 // <- chan TYPE.
3509 Type* t = Type::make_channel_type(false, true,
3510 ct->element_type());
3511 return Expression::make_type(t, location);
3513 else
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);
3524 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP, location));
3525 token = this->peek_token();
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))
3536 Location location = token->location();
3537 Operator op = token->op();
3538 this->advance_token();
3540 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL,
3541 NULL);
3542 if (expr->is_error_expression())
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;
3555 else
3556 return this->primary_expr(may_be_sink, may_be_composite_lit,
3557 is_type_switch, is_parenthesized);
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.
3569 Type*
3570 Parse::reassociate_chan_direction(Channel_type *ct, Location location)
3572 Channel_type* ele = ct->element_type()->channel_type();
3573 if (ele == NULL)
3575 error_at(location, "parse error");
3576 return Type::make_error_type();
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);
3586 // Statement =
3587 // Declaration | LabeledStmt | SimpleStmt |
3588 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3589 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3590 // DeferStmt .
3592 // LABEL is the label of this statement if it has one.
3594 void
3595 Parse::statement(Label* label)
3597 const Token* token = this->peek_token();
3598 switch (token->classification())
3600 case Token::TOKEN_KEYWORD:
3602 switch (token->keyword())
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;
3649 break;
3651 case Token::TOKEN_IDENTIFIER:
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))
3658 this->advance_token();
3659 this->labeled_stmt(identifier, location);
3661 else
3663 this->unget_token(Token::make_identifier_token(identifier,
3664 is_exported,
3665 location));
3666 this->simple_stat(true, NULL, NULL, NULL);
3669 break;
3671 case Token::TOKEN_OPERATOR:
3672 if (token->is_op(OPERATOR_LCURLY))
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);
3680 else if (!token->is_op(OPERATOR_SEMICOLON))
3681 this->simple_stat(true, NULL, NULL, NULL);
3682 break;
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;
3692 default:
3693 error_at(this->location(), "expected statement");
3694 this->advance_token();
3695 break;
3699 bool
3700 Parse::statement_may_start_here()
3702 const Token* token = this->peek_token();
3703 switch (token->classification())
3705 case Token::TOKEN_KEYWORD:
3707 switch (token->keyword())
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;
3728 default:
3729 return false;
3732 break;
3734 case Token::TOKEN_IDENTIFIER:
3735 return true;
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();
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;
3751 default:
3752 return false;
3756 // LabeledStmt = Label ":" Statement .
3757 // Label = identifier .
3759 void
3760 Parse::labeled_stmt(const std::string& label_name, Location location)
3762 Label* label = this->gogo_->add_label_definition(label_name, location);
3764 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3766 // This is a label at the end of a block. A program is
3767 // permitted to omit a semicolon here.
3768 return;
3771 if (!this->statement_may_start_here())
3773 // Mark the label as used to avoid a useless error about an
3774 // unused label.
3775 if (label != NULL)
3776 label->set_is_used();
3778 error_at(location, "missing statement after label");
3779 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3780 location));
3781 return;
3784 this->statement(label);
3787 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3788 // Assignment | ShortVarDecl .
3790 // EmptyStmt was handled in Parse::statement.
3792 // In order to make this work for if and switch statements, if
3793 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3794 // expression rather than adding an expression statement to the
3795 // current block. If we see something other than an ExpressionStat,
3796 // we add the statement, set *RETURN_EXP to true if we saw a send
3797 // statement, and return NULL. The handling of send statements is for
3798 // better error messages.
3800 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3801 // RangeClause.
3803 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3804 // guard (var := expr.("type") using the literal keyword "type").
3806 Expression*
3807 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3808 Range_clause* p_range_clause, Type_switch* p_type_switch)
3810 const Token* token = this->peek_token();
3812 // An identifier follow by := is a SimpleVarDecl.
3813 if (token->is_identifier())
3815 std::string identifier = token->identifier();
3816 bool is_exported = token->is_identifier_exported();
3817 Location location = token->location();
3819 token = this->advance_token();
3820 if (token->is_op(OPERATOR_COLONEQ)
3821 || token->is_op(OPERATOR_COMMA))
3823 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3824 this->simple_var_decl_or_assignment(identifier, location,
3825 may_be_composite_lit,
3826 p_range_clause,
3827 (token->is_op(OPERATOR_COLONEQ)
3828 ? p_type_switch
3829 : NULL));
3830 return NULL;
3833 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3834 location));
3837 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3838 may_be_composite_lit,
3839 (p_type_switch == NULL
3840 ? NULL
3841 : &p_type_switch->found),
3842 NULL);
3843 if (p_type_switch != NULL && p_type_switch->found)
3845 p_type_switch->name.clear();
3846 p_type_switch->location = exp->location();
3847 p_type_switch->expr = this->verify_not_sink(exp);
3848 return NULL;
3850 token = this->peek_token();
3851 if (token->is_op(OPERATOR_CHANOP))
3853 this->send_stmt(this->verify_not_sink(exp));
3854 if (return_exp != NULL)
3855 *return_exp = true;
3857 else if (token->is_op(OPERATOR_PLUSPLUS)
3858 || token->is_op(OPERATOR_MINUSMINUS))
3859 this->inc_dec_stat(this->verify_not_sink(exp));
3860 else if (token->is_op(OPERATOR_COMMA)
3861 || token->is_op(OPERATOR_EQ))
3862 this->assignment(exp, may_be_composite_lit, p_range_clause);
3863 else if (token->is_op(OPERATOR_PLUSEQ)
3864 || token->is_op(OPERATOR_MINUSEQ)
3865 || token->is_op(OPERATOR_OREQ)
3866 || token->is_op(OPERATOR_XOREQ)
3867 || token->is_op(OPERATOR_MULTEQ)
3868 || token->is_op(OPERATOR_DIVEQ)
3869 || token->is_op(OPERATOR_MODEQ)
3870 || token->is_op(OPERATOR_LSHIFTEQ)
3871 || token->is_op(OPERATOR_RSHIFTEQ)
3872 || token->is_op(OPERATOR_ANDEQ)
3873 || token->is_op(OPERATOR_BITCLEAREQ))
3874 this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
3875 p_range_clause);
3876 else if (return_exp != NULL)
3877 return this->verify_not_sink(exp);
3878 else
3880 exp = this->verify_not_sink(exp);
3882 if (token->is_op(OPERATOR_COLONEQ))
3884 if (!exp->is_error_expression())
3885 error_at(token->location(), "non-name on left side of %<:=%>");
3886 this->gogo_->mark_locals_used();
3887 while (!token->is_op(OPERATOR_SEMICOLON)
3888 && !token->is_eof())
3889 token = this->advance_token();
3890 return NULL;
3893 this->expression_stat(exp);
3896 return NULL;
3899 bool
3900 Parse::simple_stat_may_start_here()
3902 return this->expression_may_start_here();
3905 // Parse { Statement ";" } which is used in a few places. The list of
3906 // statements may end with a right curly brace, in which case the
3907 // semicolon may be omitted.
3909 void
3910 Parse::statement_list()
3912 while (this->statement_may_start_here())
3914 this->statement(NULL);
3915 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3916 this->advance_token();
3917 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3918 break;
3919 else
3921 if (!this->peek_token()->is_eof() || !saw_errors())
3922 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3923 if (!this->skip_past_error(OPERATOR_RCURLY))
3924 return;
3929 bool
3930 Parse::statement_list_may_start_here()
3932 return this->statement_may_start_here();
3935 // ExpressionStat = Expression .
3937 void
3938 Parse::expression_stat(Expression* exp)
3940 this->gogo_->add_statement(Statement::make_statement(exp, false));
3943 // SendStmt = Channel "&lt;-" Expression .
3944 // Channel = Expression .
3946 void
3947 Parse::send_stmt(Expression* channel)
3949 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3950 Location loc = this->location();
3951 this->advance_token();
3952 Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
3953 NULL);
3954 Statement* s = Statement::make_send_statement(channel, val, loc);
3955 this->gogo_->add_statement(s);
3958 // IncDecStat = Expression ( "++" | "--" ) .
3960 void
3961 Parse::inc_dec_stat(Expression* exp)
3963 const Token* token = this->peek_token();
3965 // Lvalue maps require special handling.
3966 if (exp->index_expression() != NULL)
3967 exp->index_expression()->set_is_lvalue();
3969 if (token->is_op(OPERATOR_PLUSPLUS))
3970 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3971 else if (token->is_op(OPERATOR_MINUSMINUS))
3972 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3973 else
3974 go_unreachable();
3975 this->advance_token();
3978 // Assignment = ExpressionList assign_op ExpressionList .
3980 // EXP is an expression that we have already parsed.
3982 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3983 // side may be a composite literal.
3985 // If RANGE_CLAUSE is not NULL, then this will recognize a
3986 // RangeClause.
3988 void
3989 Parse::assignment(Expression* expr, bool may_be_composite_lit,
3990 Range_clause* p_range_clause)
3992 Expression_list* vars;
3993 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3995 vars = new Expression_list();
3996 vars->push_back(expr);
3998 else
4000 this->advance_token();
4001 vars = this->expression_list(expr, true, may_be_composite_lit);
4004 this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
4007 // An assignment statement. LHS is the list of expressions which
4008 // appear on the left hand side.
4010 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4011 // side may be a composite literal.
4013 // If RANGE_CLAUSE is not NULL, then this will recognize a
4014 // RangeClause.
4016 void
4017 Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
4018 Range_clause* p_range_clause)
4020 const Token* token = this->peek_token();
4021 if (!token->is_op(OPERATOR_EQ)
4022 && !token->is_op(OPERATOR_PLUSEQ)
4023 && !token->is_op(OPERATOR_MINUSEQ)
4024 && !token->is_op(OPERATOR_OREQ)
4025 && !token->is_op(OPERATOR_XOREQ)
4026 && !token->is_op(OPERATOR_MULTEQ)
4027 && !token->is_op(OPERATOR_DIVEQ)
4028 && !token->is_op(OPERATOR_MODEQ)
4029 && !token->is_op(OPERATOR_LSHIFTEQ)
4030 && !token->is_op(OPERATOR_RSHIFTEQ)
4031 && !token->is_op(OPERATOR_ANDEQ)
4032 && !token->is_op(OPERATOR_BITCLEAREQ))
4034 error_at(this->location(), "expected assignment operator");
4035 return;
4037 Operator op = token->op();
4038 Location location = token->location();
4040 token = this->advance_token();
4042 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
4044 if (op != OPERATOR_EQ)
4045 error_at(this->location(), "range clause requires %<=%>");
4046 this->range_clause_expr(lhs, p_range_clause);
4047 return;
4050 Expression_list* vals = this->expression_list(NULL, false,
4051 may_be_composite_lit);
4053 // We've parsed everything; check for errors.
4054 if (lhs == NULL || vals == NULL)
4055 return;
4056 for (Expression_list::const_iterator pe = lhs->begin();
4057 pe != lhs->end();
4058 ++pe)
4060 if ((*pe)->is_error_expression())
4061 return;
4062 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
4063 error_at((*pe)->location(), "cannot use _ as value");
4065 for (Expression_list::const_iterator pe = vals->begin();
4066 pe != vals->end();
4067 ++pe)
4069 if ((*pe)->is_error_expression())
4070 return;
4073 // Map expressions act differently when they are lvalues.
4074 for (Expression_list::iterator plv = lhs->begin();
4075 plv != lhs->end();
4076 ++plv)
4077 if ((*plv)->index_expression() != NULL)
4078 (*plv)->index_expression()->set_is_lvalue();
4080 Call_expression* call;
4081 Index_expression* map_index;
4082 Receive_expression* receive;
4083 Type_guard_expression* type_guard;
4084 if (lhs->size() == vals->size())
4086 Statement* s;
4087 if (lhs->size() > 1)
4089 if (op != OPERATOR_EQ)
4090 error_at(location, "multiple values only permitted with %<=%>");
4091 s = Statement::make_tuple_assignment(lhs, vals, location);
4093 else
4095 if (op == OPERATOR_EQ)
4096 s = Statement::make_assignment(lhs->front(), vals->front(),
4097 location);
4098 else
4099 s = Statement::make_assignment_operation(op, lhs->front(),
4100 vals->front(), location);
4101 delete lhs;
4102 delete vals;
4104 this->gogo_->add_statement(s);
4106 else if (vals->size() == 1
4107 && (call = (*vals->begin())->call_expression()) != NULL)
4109 if (op != OPERATOR_EQ)
4110 error_at(location, "multiple results only permitted with %<=%>");
4111 call->set_expected_result_count(lhs->size());
4112 delete vals;
4113 vals = new Expression_list;
4114 for (unsigned int i = 0; i < lhs->size(); ++i)
4115 vals->push_back(Expression::make_call_result(call, i));
4116 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
4117 this->gogo_->add_statement(s);
4119 else if (lhs->size() == 2
4120 && vals->size() == 1
4121 && (map_index = (*vals->begin())->index_expression()) != NULL)
4123 if (op != OPERATOR_EQ)
4124 error_at(location, "two values from map requires %<=%>");
4125 Expression* val = lhs->front();
4126 Expression* present = lhs->back();
4127 Statement* s = Statement::make_tuple_map_assignment(val, present,
4128 map_index, location);
4129 this->gogo_->add_statement(s);
4131 else if (lhs->size() == 1
4132 && vals->size() == 2
4133 && (map_index = lhs->front()->index_expression()) != NULL)
4135 if (op != OPERATOR_EQ)
4136 error_at(location, "assigning tuple to map index requires %<=%>");
4137 Expression* val = vals->front();
4138 Expression* should_set = vals->back();
4139 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
4140 location);
4141 this->gogo_->add_statement(s);
4143 else if (lhs->size() == 2
4144 && vals->size() == 1
4145 && (receive = (*vals->begin())->receive_expression()) != NULL)
4147 if (op != OPERATOR_EQ)
4148 error_at(location, "two values from receive requires %<=%>");
4149 Expression* val = lhs->front();
4150 Expression* success = lhs->back();
4151 Expression* channel = receive->channel();
4152 Statement* s = Statement::make_tuple_receive_assignment(val, success,
4153 channel,
4154 location);
4155 this->gogo_->add_statement(s);
4157 else if (lhs->size() == 2
4158 && vals->size() == 1
4159 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
4161 if (op != OPERATOR_EQ)
4162 error_at(location, "two values from type guard requires %<=%>");
4163 Expression* val = lhs->front();
4164 Expression* ok = lhs->back();
4165 Expression* expr = type_guard->expr();
4166 Type* type = type_guard->type();
4167 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
4168 expr, type,
4169 location);
4170 this->gogo_->add_statement(s);
4172 else
4174 error_at(location, "number of variables does not match number of values");
4178 // GoStat = "go" Expression .
4179 // DeferStat = "defer" Expression .
4181 void
4182 Parse::go_or_defer_stat()
4184 go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
4185 || this->peek_token()->is_keyword(KEYWORD_DEFER));
4186 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
4187 Location stat_location = this->location();
4189 this->advance_token();
4190 Location expr_location = this->location();
4192 bool is_parenthesized = false;
4193 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
4194 &is_parenthesized);
4195 Call_expression* call_expr = expr->call_expression();
4196 if (is_parenthesized || call_expr == NULL)
4198 error_at(expr_location, "argument to go/defer must be function call");
4199 return;
4202 // Make it easier to simplify go/defer statements by putting every
4203 // statement in its own block.
4204 this->gogo_->start_block(stat_location);
4205 Statement* stat;
4206 if (is_go)
4207 stat = Statement::make_go_statement(call_expr, stat_location);
4208 else
4209 stat = Statement::make_defer_statement(call_expr, stat_location);
4210 this->gogo_->add_statement(stat);
4211 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
4212 stat_location);
4215 // ReturnStat = "return" [ ExpressionList ] .
4217 void
4218 Parse::return_stat()
4220 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
4221 Location location = this->location();
4222 this->advance_token();
4223 Expression_list* vals = NULL;
4224 if (this->expression_may_start_here())
4225 vals = this->expression_list(NULL, false, true);
4226 this->gogo_->add_statement(Statement::make_return_statement(vals, location));
4228 if (vals == NULL
4229 && this->gogo_->current_function()->func_value()->results_are_named())
4231 Named_object* function = this->gogo_->current_function();
4232 Function::Results* results = function->func_value()->result_variables();
4233 for (Function::Results::const_iterator p = results->begin();
4234 p != results->end();
4235 ++p)
4237 Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
4238 if (no == NULL)
4239 go_assert(saw_errors());
4240 else if (!no->is_result_variable())
4241 error_at(location, "%qs is shadowed during return",
4242 (*p)->message_name().c_str());
4247 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4248 // [ "else" ( IfStmt | Block ) ] .
4250 void
4251 Parse::if_stat()
4253 go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
4254 Location location = this->location();
4255 this->advance_token();
4257 this->gogo_->start_block(location);
4259 bool saw_simple_stat = false;
4260 Expression* cond = NULL;
4261 bool saw_send_stmt = false;
4262 if (this->simple_stat_may_start_here())
4264 cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
4265 saw_simple_stat = true;
4267 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4269 // The SimpleStat is an expression statement.
4270 this->expression_stat(cond);
4271 cond = NULL;
4273 if (cond == NULL)
4275 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4276 this->advance_token();
4277 else if (saw_simple_stat)
4279 if (saw_send_stmt)
4280 error_at(this->location(),
4281 ("send statement used as value; "
4282 "use select for non-blocking send"));
4283 else
4284 error_at(this->location(),
4285 "expected %<;%> after statement in if expression");
4286 if (!this->expression_may_start_here())
4287 cond = Expression::make_error(this->location());
4289 if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
4291 error_at(this->location(),
4292 "missing condition in if statement");
4293 cond = Expression::make_error(this->location());
4295 if (cond == NULL)
4296 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL, NULL);
4299 // Check for the easy error of a newline before starting the block.
4300 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4302 Location semi_loc = this->location();
4303 if (this->advance_token()->is_op(OPERATOR_LCURLY))
4304 error_at(semi_loc, "missing %<{%> after if clause");
4305 // Otherwise we will get an error when we call this->block
4306 // below.
4309 this->gogo_->start_block(this->location());
4310 Location end_loc = this->block();
4311 Block* then_block = this->gogo_->finish_block(end_loc);
4313 // Check for the easy error of a newline before "else".
4314 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4316 Location semi_loc = this->location();
4317 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
4318 error_at(this->location(),
4319 "unexpected semicolon or newline before %<else%>");
4320 else
4321 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
4322 semi_loc));
4325 Block* else_block = NULL;
4326 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
4328 this->gogo_->start_block(this->location());
4329 const Token* token = this->advance_token();
4330 if (token->is_keyword(KEYWORD_IF))
4331 this->if_stat();
4332 else if (token->is_op(OPERATOR_LCURLY))
4333 this->block();
4334 else
4336 error_at(this->location(), "expected %<if%> or %<{%>");
4337 this->statement(NULL);
4339 else_block = this->gogo_->finish_block(this->location());
4342 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
4343 else_block,
4344 location));
4346 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4347 location);
4350 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4351 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4352 // "{" { ExprCaseClause } "}" .
4353 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4354 // "{" { TypeCaseClause } "}" .
4355 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4357 void
4358 Parse::switch_stat(Label* label)
4360 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
4361 Location location = this->location();
4362 this->advance_token();
4364 this->gogo_->start_block(location);
4366 bool saw_simple_stat = false;
4367 Expression* switch_val = NULL;
4368 bool saw_send_stmt;
4369 Type_switch type_switch;
4370 bool have_type_switch_block = false;
4371 if (this->simple_stat_may_start_here())
4373 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4374 &type_switch);
4375 saw_simple_stat = true;
4377 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4379 // The SimpleStat is an expression statement.
4380 this->expression_stat(switch_val);
4381 switch_val = NULL;
4383 if (switch_val == NULL && !type_switch.found)
4385 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4386 this->advance_token();
4387 else if (saw_simple_stat)
4389 if (saw_send_stmt)
4390 error_at(this->location(),
4391 ("send statement used as value; "
4392 "use select for non-blocking send"));
4393 else
4394 error_at(this->location(),
4395 "expected %<;%> after statement in switch expression");
4397 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4399 if (this->peek_token()->is_identifier())
4401 const Token* token = this->peek_token();
4402 std::string identifier = token->identifier();
4403 bool is_exported = token->is_identifier_exported();
4404 Location id_loc = token->location();
4406 token = this->advance_token();
4407 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
4408 this->unget_token(Token::make_identifier_token(identifier,
4409 is_exported,
4410 id_loc));
4411 if (is_coloneq)
4413 // This must be a TypeSwitchGuard. It is in a
4414 // different block from any initial SimpleStat.
4415 if (saw_simple_stat)
4417 this->gogo_->start_block(id_loc);
4418 have_type_switch_block = true;
4421 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4422 &type_switch);
4423 if (!type_switch.found)
4425 if (switch_val == NULL
4426 || !switch_val->is_error_expression())
4428 error_at(id_loc, "expected type switch assignment");
4429 switch_val = Expression::make_error(id_loc);
4434 if (switch_val == NULL && !type_switch.found)
4436 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
4437 &type_switch.found, NULL);
4438 if (type_switch.found)
4440 type_switch.name.clear();
4441 type_switch.expr = switch_val;
4442 type_switch.location = switch_val->location();
4448 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4450 Location token_loc = this->location();
4451 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
4452 && this->advance_token()->is_op(OPERATOR_LCURLY))
4453 error_at(token_loc, "missing %<{%> after switch clause");
4454 else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
4456 error_at(token_loc, "invalid variable name");
4457 this->advance_token();
4458 this->expression(PRECEDENCE_NORMAL, false, false,
4459 &type_switch.found, NULL);
4460 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4461 this->advance_token();
4462 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4464 if (have_type_switch_block)
4465 this->gogo_->add_block(this->gogo_->finish_block(location),
4466 location);
4467 this->gogo_->add_block(this->gogo_->finish_block(location),
4468 location);
4469 return;
4471 if (type_switch.found)
4472 type_switch.expr = Expression::make_error(location);
4474 else
4476 error_at(this->location(), "expected %<{%>");
4477 if (have_type_switch_block)
4478 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4479 location);
4480 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4481 location);
4482 return;
4485 this->advance_token();
4487 Statement* statement;
4488 if (type_switch.found)
4489 statement = this->type_switch_body(label, type_switch, location);
4490 else
4491 statement = this->expr_switch_body(label, switch_val, location);
4493 if (statement != NULL)
4494 this->gogo_->add_statement(statement);
4496 if (have_type_switch_block)
4497 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4498 location);
4500 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4501 location);
4504 // The body of an expression switch.
4505 // "{" { ExprCaseClause } "}"
4507 Statement*
4508 Parse::expr_switch_body(Label* label, Expression* switch_val,
4509 Location location)
4511 Switch_statement* statement = Statement::make_switch_statement(switch_val,
4512 location);
4514 this->push_break_statement(statement, label);
4516 Case_clauses* case_clauses = new Case_clauses();
4517 bool saw_default = false;
4518 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4520 if (this->peek_token()->is_eof())
4522 if (!saw_errors())
4523 error_at(this->location(), "missing %<}%>");
4524 return NULL;
4526 this->expr_case_clause(case_clauses, &saw_default);
4528 this->advance_token();
4530 statement->add_clauses(case_clauses);
4532 this->pop_break_statement();
4534 return statement;
4537 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4538 // FallthroughStat = "fallthrough" .
4540 void
4541 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
4543 Location location = this->location();
4545 bool is_default = false;
4546 Expression_list* vals = this->expr_switch_case(&is_default);
4548 if (!this->peek_token()->is_op(OPERATOR_COLON))
4550 if (!saw_errors())
4551 error_at(this->location(), "expected %<:%>");
4552 return;
4554 else
4555 this->advance_token();
4557 Block* statements = NULL;
4558 if (this->statement_list_may_start_here())
4560 this->gogo_->start_block(this->location());
4561 this->statement_list();
4562 statements = this->gogo_->finish_block(this->location());
4565 bool is_fallthrough = false;
4566 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4568 Location fallthrough_loc = this->location();
4569 is_fallthrough = true;
4570 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4571 this->advance_token();
4572 if (this->peek_token()->is_op(OPERATOR_RCURLY))
4573 error_at(fallthrough_loc, _("cannot fallthrough final case in switch"));
4576 if (is_default)
4578 if (*saw_default)
4580 error_at(location, "multiple defaults in switch");
4581 return;
4583 *saw_default = true;
4586 if (is_default || vals != NULL)
4587 clauses->add(vals, is_default, statements, is_fallthrough, location);
4590 // ExprSwitchCase = "case" ExpressionList | "default" .
4592 Expression_list*
4593 Parse::expr_switch_case(bool* is_default)
4595 const Token* token = this->peek_token();
4596 if (token->is_keyword(KEYWORD_CASE))
4598 this->advance_token();
4599 return this->expression_list(NULL, false, true);
4601 else if (token->is_keyword(KEYWORD_DEFAULT))
4603 this->advance_token();
4604 *is_default = true;
4605 return NULL;
4607 else
4609 if (!saw_errors())
4610 error_at(this->location(), "expected %<case%> or %<default%>");
4611 if (!token->is_op(OPERATOR_RCURLY))
4612 this->advance_token();
4613 return NULL;
4617 // The body of a type switch.
4618 // "{" { TypeCaseClause } "}" .
4620 Statement*
4621 Parse::type_switch_body(Label* label, const Type_switch& type_switch,
4622 Location location)
4624 Named_object* switch_no = NULL;
4625 if (!type_switch.name.empty())
4627 if (Gogo::is_sink_name(type_switch.name))
4628 error_at(type_switch.location,
4629 "no new variables on left side of %<:=%>");
4630 else
4632 Variable* switch_var = new Variable(NULL, type_switch.expr, false,
4633 false, false,
4634 type_switch.location);
4635 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
4639 Type_switch_statement* statement =
4640 Statement::make_type_switch_statement(switch_no,
4641 (switch_no == NULL
4642 ? type_switch.expr
4643 : NULL),
4644 location);
4646 this->push_break_statement(statement, label);
4648 Type_case_clauses* case_clauses = new Type_case_clauses();
4649 bool saw_default = false;
4650 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4652 if (this->peek_token()->is_eof())
4654 error_at(this->location(), "missing %<}%>");
4655 return NULL;
4657 this->type_case_clause(switch_no, case_clauses, &saw_default);
4659 this->advance_token();
4661 statement->add_clauses(case_clauses);
4663 this->pop_break_statement();
4665 return statement;
4668 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4670 void
4671 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4672 bool* saw_default)
4674 Location location = this->location();
4676 std::vector<Type*> types;
4677 bool is_default = false;
4678 this->type_switch_case(&types, &is_default);
4680 if (!this->peek_token()->is_op(OPERATOR_COLON))
4681 error_at(this->location(), "expected %<:%>");
4682 else
4683 this->advance_token();
4685 Block* statements = NULL;
4686 if (this->statement_list_may_start_here())
4688 this->gogo_->start_block(this->location());
4689 if (switch_no != NULL && types.size() == 1)
4691 Type* type = types.front();
4692 Expression* init = Expression::make_var_reference(switch_no,
4693 location);
4694 init = Expression::make_type_guard(init, type, location);
4695 Variable* v = new Variable(type, init, false, false, false,
4696 location);
4697 v->set_is_type_switch_var();
4698 Named_object* no = this->gogo_->add_variable(switch_no->name(), v);
4700 // We don't want to issue an error if the compiler
4701 // introduced special variable is not used. Instead we want
4702 // to issue an error if the variable defined by the switch
4703 // is not used. That is handled via type_switch_vars_ and
4704 // Parse::mark_var_used.
4705 v->set_is_used();
4706 this->type_switch_vars_[no] = switch_no;
4708 this->statement_list();
4709 statements = this->gogo_->finish_block(this->location());
4712 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4714 error_at(this->location(),
4715 "fallthrough is not permitted in a type switch");
4716 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4717 this->advance_token();
4720 if (is_default)
4722 go_assert(types.empty());
4723 if (*saw_default)
4725 error_at(location, "multiple defaults in type switch");
4726 return;
4728 *saw_default = true;
4729 clauses->add(NULL, false, true, statements, location);
4731 else if (!types.empty())
4733 for (std::vector<Type*>::const_iterator p = types.begin();
4734 p + 1 != types.end();
4735 ++p)
4736 clauses->add(*p, true, false, NULL, location);
4737 clauses->add(types.back(), false, false, statements, location);
4739 else
4740 clauses->add(Type::make_error_type(), false, false, statements, location);
4743 // TypeSwitchCase = "case" type | "default"
4745 // We accept a comma separated list of types.
4747 void
4748 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4750 const Token* token = this->peek_token();
4751 if (token->is_keyword(KEYWORD_CASE))
4753 this->advance_token();
4754 while (true)
4756 Type* t = this->type();
4758 if (!t->is_error_type())
4759 types->push_back(t);
4760 else
4762 this->gogo_->mark_locals_used();
4763 token = this->peek_token();
4764 while (!token->is_op(OPERATOR_COLON)
4765 && !token->is_op(OPERATOR_COMMA)
4766 && !token->is_op(OPERATOR_RCURLY)
4767 && !token->is_eof())
4768 token = this->advance_token();
4771 if (!this->peek_token()->is_op(OPERATOR_COMMA))
4772 break;
4773 this->advance_token();
4776 else if (token->is_keyword(KEYWORD_DEFAULT))
4778 this->advance_token();
4779 *is_default = true;
4781 else
4783 error_at(this->location(), "expected %<case%> or %<default%>");
4784 if (!token->is_op(OPERATOR_RCURLY))
4785 this->advance_token();
4789 // SelectStat = "select" "{" { CommClause } "}" .
4791 void
4792 Parse::select_stat(Label* label)
4794 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4795 Location location = this->location();
4796 const Token* token = this->advance_token();
4798 if (!token->is_op(OPERATOR_LCURLY))
4800 Location token_loc = token->location();
4801 if (token->is_op(OPERATOR_SEMICOLON)
4802 && this->advance_token()->is_op(OPERATOR_LCURLY))
4803 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4804 else
4806 error_at(this->location(), "expected %<{%>");
4807 return;
4810 this->advance_token();
4812 Select_statement* statement = Statement::make_select_statement(location);
4814 this->push_break_statement(statement, label);
4816 Select_clauses* select_clauses = new Select_clauses();
4817 bool saw_default = false;
4818 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4820 if (this->peek_token()->is_eof())
4822 error_at(this->location(), "expected %<}%>");
4823 return;
4825 this->comm_clause(select_clauses, &saw_default);
4828 this->advance_token();
4830 statement->add_clauses(select_clauses);
4832 this->pop_break_statement();
4834 this->gogo_->add_statement(statement);
4837 // CommClause = CommCase ":" { Statement ";" } .
4839 void
4840 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4842 Location location = this->location();
4843 bool is_send = false;
4844 Expression* channel = NULL;
4845 Expression* val = NULL;
4846 Expression* closed = NULL;
4847 std::string varname;
4848 std::string closedname;
4849 bool is_default = false;
4850 bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
4851 &varname, &closedname, &is_default);
4853 if (!is_send
4854 && varname.empty()
4855 && closedname.empty()
4856 && val != NULL
4857 && val->index_expression() != NULL)
4858 val->index_expression()->set_is_lvalue();
4860 if (this->peek_token()->is_op(OPERATOR_COLON))
4861 this->advance_token();
4862 else
4863 error_at(this->location(), "expected colon");
4865 this->gogo_->start_block(this->location());
4867 Named_object* var = NULL;
4868 if (!varname.empty())
4870 // FIXME: LOCATION is slightly wrong here.
4871 Variable* v = new Variable(NULL, channel, false, false, false,
4872 location);
4873 v->set_type_from_chan_element();
4874 var = this->gogo_->add_variable(varname, v);
4877 Named_object* closedvar = NULL;
4878 if (!closedname.empty())
4880 // FIXME: LOCATION is slightly wrong here.
4881 Variable* v = new Variable(Type::lookup_bool_type(), NULL,
4882 false, false, false, location);
4883 closedvar = this->gogo_->add_variable(closedname, v);
4886 this->statement_list();
4888 Block* statements = this->gogo_->finish_block(this->location());
4890 if (is_default)
4892 if (*saw_default)
4894 error_at(location, "multiple defaults in select");
4895 return;
4897 *saw_default = true;
4900 if (got_case)
4901 clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
4902 statements, location);
4903 else if (statements != NULL)
4905 // Add the statements to make sure that any names they define
4906 // are traversed.
4907 this->gogo_->add_block(statements, location);
4911 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4913 bool
4914 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4915 Expression** closed, std::string* varname,
4916 std::string* closedname, bool* is_default)
4918 const Token* token = this->peek_token();
4919 if (token->is_keyword(KEYWORD_DEFAULT))
4921 this->advance_token();
4922 *is_default = true;
4924 else if (token->is_keyword(KEYWORD_CASE))
4926 this->advance_token();
4927 if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
4928 closedname))
4929 return false;
4931 else
4933 error_at(this->location(), "expected %<case%> or %<default%>");
4934 if (!token->is_op(OPERATOR_RCURLY))
4935 this->advance_token();
4936 return false;
4939 return true;
4942 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4943 // RecvExpr = Expression .
4945 bool
4946 Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
4947 Expression** closed, std::string* varname,
4948 std::string* closedname)
4950 const Token* token = this->peek_token();
4951 bool saw_comma = false;
4952 bool closed_is_id = false;
4953 if (token->is_identifier())
4955 Gogo* gogo = this->gogo_;
4956 std::string recv_var = token->identifier();
4957 bool is_rv_exported = token->is_identifier_exported();
4958 Location recv_var_loc = token->location();
4959 token = this->advance_token();
4960 if (token->is_op(OPERATOR_COLONEQ))
4962 // case rv := <-c:
4963 this->advance_token();
4964 Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
4965 NULL, NULL);
4966 Receive_expression* re = e->receive_expression();
4967 if (re == NULL)
4969 if (!e->is_error_expression())
4970 error_at(this->location(), "expected receive expression");
4971 return false;
4973 if (recv_var == "_")
4975 error_at(recv_var_loc,
4976 "no new variables on left side of %<:=%>");
4977 recv_var = Gogo::erroneous_name();
4979 *is_send = false;
4980 *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
4981 *channel = re->channel();
4982 return true;
4984 else if (token->is_op(OPERATOR_COMMA))
4986 token = this->advance_token();
4987 if (token->is_identifier())
4989 std::string recv_closed = token->identifier();
4990 bool is_rc_exported = token->is_identifier_exported();
4991 Location recv_closed_loc = token->location();
4992 closed_is_id = true;
4994 token = this->advance_token();
4995 if (token->is_op(OPERATOR_COLONEQ))
4997 // case rv, rc := <-c:
4998 this->advance_token();
4999 Expression* e = this->expression(PRECEDENCE_NORMAL, false,
5000 false, NULL, NULL);
5001 Receive_expression* re = e->receive_expression();
5002 if (re == NULL)
5004 if (!e->is_error_expression())
5005 error_at(this->location(),
5006 "expected receive expression");
5007 return false;
5009 if (recv_var == "_" && recv_closed == "_")
5011 error_at(recv_var_loc,
5012 "no new variables on left side of %<:=%>");
5013 recv_var = Gogo::erroneous_name();
5015 *is_send = false;
5016 if (recv_var != "_")
5017 *varname = gogo->pack_hidden_name(recv_var,
5018 is_rv_exported);
5019 if (recv_closed != "_")
5020 *closedname = gogo->pack_hidden_name(recv_closed,
5021 is_rc_exported);
5022 *channel = re->channel();
5023 return true;
5026 this->unget_token(Token::make_identifier_token(recv_closed,
5027 is_rc_exported,
5028 recv_closed_loc));
5031 *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
5032 is_rv_exported),
5033 recv_var_loc, true);
5034 saw_comma = true;
5036 else
5037 this->unget_token(Token::make_identifier_token(recv_var,
5038 is_rv_exported,
5039 recv_var_loc));
5042 // If SAW_COMMA is false, then we are looking at the start of the
5043 // send or receive expression. If SAW_COMMA is true, then *VAL is
5044 // set and we just read a comma.
5046 Expression* e;
5047 if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
5048 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL);
5049 else
5051 // case <-c:
5052 *is_send = false;
5053 this->advance_token();
5054 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5056 // The next token should be ':'. If it is '<-', then we have
5057 // case <-c <- v:
5058 // which is to say, send on a channel received from a channel.
5059 if (!this->peek_token()->is_op(OPERATOR_CHANOP))
5060 return true;
5062 e = Expression::make_receive(*channel, (*channel)->location());
5065 if (this->peek_token()->is_op(OPERATOR_EQ))
5067 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
5069 error_at(this->location(), "missing %<<-%>");
5070 return false;
5072 *is_send = false;
5073 this->advance_token();
5074 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5075 if (saw_comma)
5077 // case v, e = <-c:
5078 // *VAL is already set.
5079 if (!e->is_sink_expression())
5080 *closed = e;
5082 else
5084 // case v = <-c:
5085 if (!e->is_sink_expression())
5086 *val = e;
5088 return true;
5091 if (saw_comma)
5093 if (closed_is_id)
5094 error_at(this->location(), "expected %<=%> or %<:=%>");
5095 else
5096 error_at(this->location(), "expected %<=%>");
5097 return false;
5100 if (this->peek_token()->is_op(OPERATOR_CHANOP))
5102 // case c <- v:
5103 *is_send = true;
5104 *channel = this->verify_not_sink(e);
5105 this->advance_token();
5106 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5107 return true;
5110 error_at(this->location(), "expected %<<-%> or %<=%>");
5111 return false;
5114 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5115 // Condition = Expression .
5117 void
5118 Parse::for_stat(Label* label)
5120 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
5121 Location location = this->location();
5122 const Token* token = this->advance_token();
5124 // Open a block to hold any variables defined in the init statement
5125 // of the for statement.
5126 this->gogo_->start_block(location);
5128 Block* init = NULL;
5129 Expression* cond = NULL;
5130 Block* post = NULL;
5131 Range_clause range_clause;
5133 if (!token->is_op(OPERATOR_LCURLY))
5135 if (token->is_keyword(KEYWORD_VAR))
5137 error_at(this->location(),
5138 "var declaration not allowed in for initializer");
5139 this->var_decl();
5142 if (token->is_op(OPERATOR_SEMICOLON))
5143 this->for_clause(&cond, &post);
5144 else
5146 // We might be looking at a Condition, an InitStat, or a
5147 // RangeClause.
5148 bool saw_send_stmt;
5149 cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
5150 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5152 if (cond == NULL && !range_clause.found)
5154 if (saw_send_stmt)
5155 error_at(this->location(),
5156 ("send statement used as value; "
5157 "use select for non-blocking send"));
5158 else
5159 error_at(this->location(), "parse error in for statement");
5162 else
5164 if (range_clause.found)
5165 error_at(this->location(), "parse error after range clause");
5167 if (cond != NULL)
5169 // COND is actually an expression statement for
5170 // InitStat at the start of a ForClause.
5171 this->expression_stat(cond);
5172 cond = NULL;
5175 this->for_clause(&cond, &post);
5180 // Check for the easy error of a newline before starting the block.
5181 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5183 Location semi_loc = this->location();
5184 if (this->advance_token()->is_op(OPERATOR_LCURLY))
5185 error_at(semi_loc, "missing %<{%> after for clause");
5186 // Otherwise we will get an error when we call this->block
5187 // below.
5190 // Build the For_statement and note that it is the current target
5191 // for break and continue statements.
5193 For_statement* sfor;
5194 For_range_statement* srange;
5195 Statement* s;
5196 if (!range_clause.found)
5198 sfor = Statement::make_for_statement(init, cond, post, location);
5199 s = sfor;
5200 srange = NULL;
5202 else
5204 srange = Statement::make_for_range_statement(range_clause.index,
5205 range_clause.value,
5206 range_clause.range,
5207 location);
5208 s = srange;
5209 sfor = NULL;
5212 this->push_break_statement(s, label);
5213 this->push_continue_statement(s, label);
5215 // Gather the block of statements in the loop and add them to the
5216 // For_statement.
5218 this->gogo_->start_block(this->location());
5219 Location end_loc = this->block();
5220 Block* statements = this->gogo_->finish_block(end_loc);
5222 if (sfor != NULL)
5223 sfor->add_statements(statements);
5224 else
5225 srange->add_statements(statements);
5227 // This is no longer the break/continue target.
5228 this->pop_break_statement();
5229 this->pop_continue_statement();
5231 // Add the For_statement to the list of statements, and close out
5232 // the block we started to hold any variables defined in the for
5233 // statement.
5235 this->gogo_->add_statement(s);
5237 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
5238 location);
5241 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5242 // InitStat = SimpleStat .
5243 // PostStat = SimpleStat .
5245 // We have already read InitStat at this point.
5247 void
5248 Parse::for_clause(Expression** cond, Block** post)
5250 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
5251 this->advance_token();
5252 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5253 *cond = NULL;
5254 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
5256 error_at(this->location(), "missing %<{%> after for clause");
5257 *cond = NULL;
5258 *post = NULL;
5259 return;
5261 else
5262 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5263 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5264 error_at(this->location(), "expected semicolon");
5265 else
5266 this->advance_token();
5268 if (this->peek_token()->is_op(OPERATOR_LCURLY))
5269 *post = NULL;
5270 else
5272 this->gogo_->start_block(this->location());
5273 this->simple_stat(false, NULL, NULL, NULL);
5274 *post = this->gogo_->finish_block(this->location());
5278 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
5280 // This is the := version. It is called with a list of identifiers.
5282 void
5283 Parse::range_clause_decl(const Typed_identifier_list* til,
5284 Range_clause* p_range_clause)
5286 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5287 Location location = this->location();
5289 p_range_clause->found = true;
5291 go_assert(til->size() >= 1);
5292 if (til->size() > 2)
5293 error_at(this->location(), "too many variables for range clause");
5295 this->advance_token();
5296 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL,
5297 NULL);
5298 p_range_clause->range = expr;
5300 bool any_new = false;
5302 const Typed_identifier* pti = &til->front();
5303 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new,
5304 NULL, NULL);
5305 if (any_new && no->is_variable())
5306 no->var_value()->set_type_from_range_index();
5307 p_range_clause->index = Expression::make_var_reference(no, location);
5309 if (til->size() == 1)
5310 p_range_clause->value = NULL;
5311 else
5313 pti = &til->back();
5314 bool is_new = false;
5315 no = this->init_var(*pti, NULL, expr, true, true, &is_new, NULL, NULL);
5316 if (is_new && no->is_variable())
5317 no->var_value()->set_type_from_range_value();
5318 if (is_new)
5319 any_new = true;
5320 if (!Gogo::is_sink_name(pti->name()))
5321 p_range_clause->value = Expression::make_var_reference(no, location);
5324 if (!any_new)
5325 error_at(location, "variables redeclared but no variable is new");
5328 // The = version of RangeClause. This is called with a list of
5329 // expressions.
5331 void
5332 Parse::range_clause_expr(const Expression_list* vals,
5333 Range_clause* p_range_clause)
5335 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5337 p_range_clause->found = true;
5339 go_assert(vals->size() >= 1);
5340 if (vals->size() > 2)
5341 error_at(this->location(), "too many variables for range clause");
5343 this->advance_token();
5344 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
5345 NULL, NULL);
5347 p_range_clause->index = vals->front();
5348 if (vals->size() == 1)
5349 p_range_clause->value = NULL;
5350 else
5351 p_range_clause->value = vals->back();
5354 // Push a statement on the break stack.
5356 void
5357 Parse::push_break_statement(Statement* enclosing, Label* label)
5359 if (this->break_stack_ == NULL)
5360 this->break_stack_ = new Bc_stack();
5361 this->break_stack_->push_back(std::make_pair(enclosing, label));
5364 // Push a statement on the continue stack.
5366 void
5367 Parse::push_continue_statement(Statement* enclosing, Label* label)
5369 if (this->continue_stack_ == NULL)
5370 this->continue_stack_ = new Bc_stack();
5371 this->continue_stack_->push_back(std::make_pair(enclosing, label));
5374 // Pop the break stack.
5376 void
5377 Parse::pop_break_statement()
5379 this->break_stack_->pop_back();
5382 // Pop the continue stack.
5384 void
5385 Parse::pop_continue_statement()
5387 this->continue_stack_->pop_back();
5390 // Find a break or continue statement given a label name.
5392 Statement*
5393 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
5395 if (bc_stack == NULL)
5396 return NULL;
5397 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
5398 p != bc_stack->rend();
5399 ++p)
5401 if (p->second != NULL && p->second->name() == label)
5403 p->second->set_is_used();
5404 return p->first;
5407 return NULL;
5410 // BreakStat = "break" [ identifier ] .
5412 void
5413 Parse::break_stat()
5415 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
5416 Location location = this->location();
5418 const Token* token = this->advance_token();
5419 Statement* enclosing;
5420 if (!token->is_identifier())
5422 if (this->break_stack_ == NULL || this->break_stack_->empty())
5424 error_at(this->location(),
5425 "break statement not within for or switch or select");
5426 return;
5428 enclosing = this->break_stack_->back().first;
5430 else
5432 enclosing = this->find_bc_statement(this->break_stack_,
5433 token->identifier());
5434 if (enclosing == NULL)
5436 // If there is a label with this name, mark it as used to
5437 // avoid a useless error about an unused label.
5438 this->gogo_->add_label_reference(token->identifier(),
5439 Linemap::unknown_location(), false);
5441 error_at(token->location(), "invalid break label %qs",
5442 Gogo::message_name(token->identifier()).c_str());
5443 this->advance_token();
5444 return;
5446 this->advance_token();
5449 Unnamed_label* label;
5450 if (enclosing->classification() == Statement::STATEMENT_FOR)
5451 label = enclosing->for_statement()->break_label();
5452 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5453 label = enclosing->for_range_statement()->break_label();
5454 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
5455 label = enclosing->switch_statement()->break_label();
5456 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
5457 label = enclosing->type_switch_statement()->break_label();
5458 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
5459 label = enclosing->select_statement()->break_label();
5460 else
5461 go_unreachable();
5463 this->gogo_->add_statement(Statement::make_break_statement(label,
5464 location));
5467 // ContinueStat = "continue" [ identifier ] .
5469 void
5470 Parse::continue_stat()
5472 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
5473 Location location = this->location();
5475 const Token* token = this->advance_token();
5476 Statement* enclosing;
5477 if (!token->is_identifier())
5479 if (this->continue_stack_ == NULL || this->continue_stack_->empty())
5481 error_at(this->location(), "continue statement not within for");
5482 return;
5484 enclosing = this->continue_stack_->back().first;
5486 else
5488 enclosing = this->find_bc_statement(this->continue_stack_,
5489 token->identifier());
5490 if (enclosing == NULL)
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);
5497 error_at(token->location(), "invalid continue label %qs",
5498 Gogo::message_name(token->identifier()).c_str());
5499 this->advance_token();
5500 return;
5502 this->advance_token();
5505 Unnamed_label* label;
5506 if (enclosing->classification() == Statement::STATEMENT_FOR)
5507 label = enclosing->for_statement()->continue_label();
5508 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5509 label = enclosing->for_range_statement()->continue_label();
5510 else
5511 go_unreachable();
5513 this->gogo_->add_statement(Statement::make_continue_statement(label,
5514 location));
5517 // GotoStat = "goto" identifier .
5519 void
5520 Parse::goto_stat()
5522 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
5523 Location location = this->location();
5524 const Token* token = this->advance_token();
5525 if (!token->is_identifier())
5526 error_at(this->location(), "expected label for goto");
5527 else
5529 Label* label = this->gogo_->add_label_reference(token->identifier(),
5530 location, true);
5531 Statement* s = Statement::make_goto_statement(label, location);
5532 this->gogo_->add_statement(s);
5533 this->advance_token();
5537 // PackageClause = "package" PackageName .
5539 void
5540 Parse::package_clause()
5542 const Token* token = this->peek_token();
5543 Location location = token->location();
5544 std::string name;
5545 if (!token->is_keyword(KEYWORD_PACKAGE))
5547 error_at(this->location(), "program must start with package clause");
5548 name = "ERROR";
5550 else
5552 token = this->advance_token();
5553 if (token->is_identifier())
5555 name = token->identifier();
5556 if (name == "_")
5558 error_at(this->location(), "invalid package name _");
5559 name = Gogo::erroneous_name();
5561 this->advance_token();
5563 else
5565 error_at(this->location(), "package name must be an identifier");
5566 name = "ERROR";
5569 this->gogo_->set_package_name(name, location);
5572 // ImportDecl = "import" Decl<ImportSpec> .
5574 void
5575 Parse::import_decl()
5577 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
5578 this->advance_token();
5579 this->decl(&Parse::import_spec, NULL);
5582 // ImportSpec = [ "." | PackageName ] PackageFileName .
5584 void
5585 Parse::import_spec(void*)
5587 const Token* token = this->peek_token();
5588 Location location = token->location();
5590 std::string local_name;
5591 bool is_local_name_exported = false;
5592 if (token->is_op(OPERATOR_DOT))
5594 local_name = ".";
5595 token = this->advance_token();
5597 else if (token->is_identifier())
5599 local_name = token->identifier();
5600 is_local_name_exported = token->is_identifier_exported();
5601 token = this->advance_token();
5604 if (!token->is_string())
5606 error_at(this->location(), "import statement not a string");
5607 this->advance_token();
5608 return;
5611 this->gogo_->import_package(token->string_value(), local_name,
5612 is_local_name_exported, location);
5614 this->advance_token();
5617 // SourceFile = PackageClause ";" { ImportDecl ";" }
5618 // { TopLevelDecl ";" } .
5620 void
5621 Parse::program()
5623 this->package_clause();
5625 const Token* token = this->peek_token();
5626 if (token->is_op(OPERATOR_SEMICOLON))
5627 token = this->advance_token();
5628 else
5629 error_at(this->location(),
5630 "expected %<;%> or newline after package clause");
5632 while (token->is_keyword(KEYWORD_IMPORT))
5634 this->import_decl();
5635 token = this->peek_token();
5636 if (token->is_op(OPERATOR_SEMICOLON))
5637 token = this->advance_token();
5638 else
5639 error_at(this->location(),
5640 "expected %<;%> or newline after import declaration");
5643 while (!token->is_eof())
5645 if (this->declaration_may_start_here())
5646 this->declaration();
5647 else
5649 error_at(this->location(), "expected declaration");
5650 this->gogo_->mark_locals_used();
5652 this->advance_token();
5653 while (!this->peek_token()->is_eof()
5654 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
5655 && !this->peek_token()->is_op(OPERATOR_RCURLY));
5656 if (!this->peek_token()->is_eof()
5657 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
5658 this->advance_token();
5660 token = this->peek_token();
5661 if (token->is_op(OPERATOR_SEMICOLON))
5662 token = this->advance_token();
5663 else if (!token->is_eof() || !saw_errors())
5665 if (token->is_op(OPERATOR_CHANOP))
5666 error_at(this->location(),
5667 ("send statement used as value; "
5668 "use select for non-blocking send"));
5669 else
5670 error_at(this->location(),
5671 "expected %<;%> or newline after top level declaration");
5672 this->skip_past_error(OPERATOR_INVALID);
5677 // Reset the current iota value.
5679 void
5680 Parse::reset_iota()
5682 this->iota_ = 0;
5685 // Return the current iota value.
5688 Parse::iota_value()
5690 return this->iota_;
5693 // Increment the current iota value.
5695 void
5696 Parse::increment_iota()
5698 ++this->iota_;
5701 // Skip forward to a semicolon or OP. OP will normally be
5702 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5703 // past it and return. If we find OP, it will be the next token to
5704 // read. Return true if we are OK, false if we found EOF.
5706 bool
5707 Parse::skip_past_error(Operator op)
5709 this->gogo_->mark_locals_used();
5710 const Token* token = this->peek_token();
5711 while (!token->is_op(op))
5713 if (token->is_eof())
5714 return false;
5715 if (token->is_op(OPERATOR_SEMICOLON))
5717 this->advance_token();
5718 return true;
5720 token = this->advance_token();
5722 return true;
5725 // Check that an expression is not a sink.
5727 Expression*
5728 Parse::verify_not_sink(Expression* expr)
5730 if (expr->is_sink_expression())
5732 error_at(expr->location(), "cannot use _ as value");
5733 expr = Expression::make_error(expr->location());
5736 // If this can not be a sink, and it is a variable, then we are
5737 // using the variable, not just assigning to it.
5738 Var_expression* ve = expr->var_expression();
5739 if (ve != NULL)
5740 this->mark_var_used(ve->named_object());
5742 return expr;
5745 // Mark a variable as used.
5747 void
5748 Parse::mark_var_used(Named_object* no)
5750 if (no->is_variable())
5752 no->var_value()->set_is_used();
5754 // When a type switch uses := to define a variable, then for
5755 // each case with a single type we introduce a new variable with
5756 // the appropriate type. When we do, if the newly introduced
5757 // variable is used, then the type switch variable is used.
5758 Type_switch_vars::iterator p = this->type_switch_vars_.find(no);
5759 if (p != this->type_switch_vars_.end())
5760 p->second->var_value()->set_is_used();