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.
12 #include "statements.h"
13 #include "expressions.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
22 const Enclosing_var& v2)
24 if (v1.var() == v2.var())
27 const std::string& n1(v1.var()->name());
28 const std::string& n2(v2.var()->name());
29 int i = n1.compare(n2);
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.
43 Parse::Parse(Lex* lex, Gogo* gogo)
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),
51 continue_stack_(NULL),
58 // Return the current 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();
70 // Advance to the next token and return it.
73 Parse::advance_token()
75 if (this->unget_token_valid_)
77 this->unget_token_valid_ = false;
78 if (!this->token_.is_invalid())
81 this->token_ = this->lex_->next_token();
85 // Push a token back on the input stream.
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.
100 return this->peek_token()->location();
103 // IdentifierList = identifier { "," identifier } .
106 Parse::identifier_list(Typed_identifier_list* til)
108 const Token* token = this->peek_token();
111 if (!token->is_identifier())
113 error_at(this->location(), "expected identifier");
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))
123 token = this->advance_token();
127 // ExpressionList = Expression { "," Expression } .
129 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
132 // If MAY_BE_SINK is true, the expressions in the list may be "_".
135 Parse::expression_list(Expression* first, bool may_be_sink,
136 bool may_be_composite_lit)
138 Expression_list* ret = new Expression_list();
140 ret->push_back(first);
143 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink,
144 may_be_composite_lit, NULL));
146 const Token* token = this->peek_token();
147 if (!token->is_op(OPERATOR_COMMA))
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,
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
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");
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))
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();
202 package->package_value()->set_used();
204 token = this->advance_token();
205 if (!token->is_identifier())
207 error_at(this->location(), "expected identifier");
211 name = token->identifier();
215 error_at(this->location(), "invalid use of %<_%>");
219 if (package->name() == this->gogo_->package_name())
220 name = this->gogo_->pack_hidden_name(name,
221 token->is_identifier_exported());
226 this->advance_token();
231 // Type = TypeName | TypeLit | "(" Type ")" .
233 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
234 // SliceType | MapType | ChannelType .
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);
255 return Type::make_error_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();
272 if (!ret->is_error_type())
273 error_at(this->location(), "expected %<)%>");
279 error_at(token->location(), "expected type");
280 return Type::make_error_type();
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.
306 Parse::type_name(bool issue_error)
308 Location location = this->location();
311 Named_object* package;
312 if (!this->qualified_ident(&name, &package))
313 return Type::make_error_type();
315 Named_object* named_object;
317 named_object = this->gogo_->lookup(name, NULL);
320 named_object = package->package_value()->lookup(name);
321 if (named_object == NULL
323 && package->name() != this->gogo_->package_name())
325 // Check whether the name is there but hidden.
326 std::string s = ('.' + package->package_value()->unique_prefix()
327 + '.' + package->package_value()->name()
329 named_object = package->package_value()->lookup(s);
330 if (named_object != NULL)
332 const std::string& packname(package->package_value()->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());
342 if (named_object == NULL)
345 named_object = this->gogo_->add_unknown_name(name, location);
348 const std::string& packname(package->package_value()->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());
356 else if (named_object->is_type())
358 if (!named_object->type_value()->is_visible())
361 else if (named_object->is_unknown() || named_object->is_type_declaration())
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);
381 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
382 // ArrayLength = Expression .
383 // ElementType = CompleteType .
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();
396 if (!token->is_op(OPERATOR_ELLIPSIS))
397 length = this->expression(PRECEDENCE_NORMAL, false, true, 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();
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 .
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 ";" } "}" .
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 %<{%>");
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();
500 if (pi->type()->is_error_type())
502 for (Struct_field_list::const_iterator pj = pi + 1;
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 .
520 Parse::field_decl(Struct_field_list* sfl)
522 const Token* token = this->peek_token();
523 Location location = token->location();
525 bool is_anonymous_pointer;
526 if (token->is_op(OPERATOR_MULT))
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,
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)
552 token = this->advance_token();
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)
568 token = this->advance_token();
572 Type* type = this->type_name(true);
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)));
587 sfl->back().set_tag(tag);
592 Typed_identifier_list til;
595 token = this->peek_token();
596 if (!token->is_identifier())
598 error_at(this->location(), "expected identifier");
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))
607 this->advance_token();
610 Type* type = this->type();
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();
624 sfl->push_back(Struct_field(*p));
626 sfl->back().set_tag(tag);
631 // PointerType = "*" 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())
641 return Type::make_pointer_type(type);
644 // ChannelType = Channel | SendChannel | RecvChannel .
645 // Channel = "chan" ElementType .
646 // SendChannel = "chan" "<-" ElementType .
647 // RecvChannel = "<-" "chan" ElementType .
650 Parse::channel_type()
652 const Token* token = this->peek_token();
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();
663 this->advance_token();
667 go_assert(token->is_keyword(KEYWORD_CHAN));
668 if (this->advance_token()->is_op(OPERATOR_CHANOP))
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");
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.
698 Parse::check_signature_names(const Typed_identifier_list* params,
701 for (Typed_identifier_list::const_iterator p = params->begin();
705 if (p->name().empty() || Gogo::is_sink_name(p->name()))
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);
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.
729 Parse::signature(Typed_identifier* receiver, Location location)
731 bool is_varargs = false;
732 Typed_identifier_list* params;
733 bool params_ok = this->parameters(¶ms, &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))
748 this->check_signature_names(params, &names);
750 this->check_signature_names(results, &names);
752 Function_type* ret = Type::make_function_type(receiver, params, results,
755 ret->set_is_varargs();
759 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
761 // This returns false on a parse error.
764 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
768 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
770 error_at(this->location(), "expected %<(%>");
774 Typed_identifier_list* params = NULL;
775 bool saw_error = false;
777 const Token* token = this->advance_token();
778 if (!token->is_op(OPERATOR_RPAREN))
780 params = this->parameter_list(is_varargs);
783 token = this->peek_token();
786 // The optional trailing comma is picked up in parameter_list.
788 if (!token->is_op(OPERATOR_RPAREN))
789 error_at(this->location(), "expected %<)%>");
791 this->advance_token();
800 // ParameterList = ParameterDecl { "," ParameterDecl } .
802 // This sets *IS_VARARGS if the list ends with an ellipsis.
803 // IS_VARARGS will be NULL if varargs are not permitted.
805 // We pick up an optional trailing comma.
807 // This returns NULL if some error is seen.
809 Typed_identifier_list*
810 Parse::parameter_list(bool* is_varargs)
812 Location location = this->location();
813 Typed_identifier_list* ret = new Typed_identifier_list();
815 bool saw_error = false;
817 // If we see an identifier and then a comma, then we don't know
818 // whether we are looking at a list of identifiers followed by a
819 // type, or a list of types given by name. We have to do an
820 // arbitrary lookahead to figure it out.
822 bool parameters_have_names;
823 const Token* token = this->peek_token();
824 if (!token->is_identifier())
826 // This must be a type which starts with something like '*'.
827 parameters_have_names = false;
831 std::string name = token->identifier();
832 bool is_exported = token->is_identifier_exported();
833 Location location = token->location();
834 token = this->advance_token();
835 if (!token->is_op(OPERATOR_COMMA))
837 if (token->is_op(OPERATOR_DOT))
839 // This is a qualified identifier, which must turn out
841 parameters_have_names = false;
843 else if (token->is_op(OPERATOR_RPAREN))
845 // A single identifier followed by a parenthesis must be
847 parameters_have_names = false;
851 // An identifier followed by something other than a
852 // comma or a dot or a right parenthesis must be a
853 // parameter name followed by a type.
854 parameters_have_names = true;
857 this->unget_token(Token::make_identifier_token(name, is_exported,
862 // An identifier followed by a comma may be the first in a
863 // list of parameter names followed by a type, or it may be
864 // the first in a list of types without parameter names. To
865 // find out we gather as many identifiers separated by
867 std::string id_name = this->gogo_->pack_hidden_name(name,
869 ret->push_back(Typed_identifier(id_name, NULL, location));
870 bool just_saw_comma = true;
871 while (this->advance_token()->is_identifier())
873 name = this->peek_token()->identifier();
874 is_exported = this->peek_token()->is_identifier_exported();
875 location = this->peek_token()->location();
876 id_name = this->gogo_->pack_hidden_name(name, is_exported);
877 ret->push_back(Typed_identifier(id_name, NULL, location));
878 if (!this->advance_token()->is_op(OPERATOR_COMMA))
880 just_saw_comma = false;
887 // We saw ID1 "," ID2 "," followed by something which
888 // was not an identifier. We must be seeing the start
889 // of a type, and ID1 and ID2 must be types, and the
890 // parameters don't have names.
891 parameters_have_names = false;
893 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
895 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
896 // and the parameters don't have names.
897 parameters_have_names = false;
899 else if (this->peek_token()->is_op(OPERATOR_DOT))
901 // We saw ID1 "," ID2 ".". ID2 must be a package name,
902 // ID1 must be a type, and the parameters don't have
904 parameters_have_names = false;
905 this->unget_token(Token::make_identifier_token(name, is_exported,
908 just_saw_comma = true;
912 // We saw ID1 "," ID2 followed by something other than
913 // ",", ".", or ")". We must be looking at the start of
914 // a type, and ID1 and ID2 must be parameter names.
915 parameters_have_names = true;
918 if (parameters_have_names)
920 go_assert(!just_saw_comma);
921 // We have just seen ID1, ID2 xxx.
923 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
927 error_at(this->location(), "%<...%> only permits one name");
929 this->advance_token();
932 for (size_t i = 0; i < ret->size(); ++i)
933 ret->set_type(i, type);
934 if (!this->peek_token()->is_op(OPERATOR_COMMA))
935 return saw_error ? NULL : ret;
936 if (this->advance_token()->is_op(OPERATOR_RPAREN))
937 return saw_error ? NULL : ret;
941 Typed_identifier_list* tret = new Typed_identifier_list();
942 for (Typed_identifier_list::const_iterator p = ret->begin();
946 Named_object* no = this->gogo_->lookup(p->name(), NULL);
949 no = this->gogo_->add_unknown_name(p->name(),
953 type = no->type_value();
954 else if (no->is_unknown() || no->is_type_declaration())
955 type = Type::make_forward_declaration(no);
958 error_at(p->location(), "expected %<%s%> to be a type",
959 Gogo::message_name(p->name()).c_str());
961 type = Type::make_error_type();
963 tret->push_back(Typed_identifier("", type, p->location()));
968 || this->peek_token()->is_op(OPERATOR_RPAREN))
969 return saw_error ? NULL : ret;
974 bool mix_error = false;
975 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
976 while (this->peek_token()->is_op(OPERATOR_COMMA))
978 if (is_varargs != NULL && *is_varargs)
980 error_at(this->location(), "%<...%> must be last parameter");
983 if (this->advance_token()->is_op(OPERATOR_RPAREN))
985 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
989 error_at(location, "invalid named/anonymous mix");
1000 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1003 Parse::parameter_decl(bool parameters_have_names,
1004 Typed_identifier_list* til,
1008 if (!parameters_have_names)
1011 Location location = this->location();
1012 if (!this->peek_token()->is_identifier())
1014 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1015 type = this->type();
1018 if (is_varargs == NULL)
1019 error_at(this->location(), "invalid use of %<...%>");
1022 this->advance_token();
1023 if (is_varargs == NULL
1024 && this->peek_token()->is_op(OPERATOR_RPAREN))
1025 type = Type::make_error_type();
1028 Type* element_type = this->type();
1029 type = Type::make_array_type(element_type, NULL);
1035 type = this->type_name(false);
1036 if (type->is_error_type()
1037 || (!this->peek_token()->is_op(OPERATOR_COMMA)
1038 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1041 while (!this->peek_token()->is_op(OPERATOR_COMMA)
1042 && !this->peek_token()->is_op(OPERATOR_RPAREN))
1043 this->advance_token();
1046 if (!type->is_error_type())
1047 til->push_back(Typed_identifier("", type, location));
1051 size_t orig_count = til->size();
1052 if (this->peek_token()->is_identifier())
1053 this->identifier_list(til);
1056 size_t new_count = til->size();
1059 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1060 type = this->type();
1063 if (is_varargs == NULL)
1064 error_at(this->location(), "invalid use of %<...%>");
1065 else if (new_count > orig_count + 1)
1066 error_at(this->location(), "%<...%> only permits one name");
1069 this->advance_token();
1070 Type* element_type = this->type();
1071 type = Type::make_array_type(element_type, NULL);
1073 for (size_t i = orig_count; i < new_count; ++i)
1074 til->set_type(i, type);
1078 // Result = Parameters | Type .
1080 // This returns false on a parse error.
1083 Parse::result(Typed_identifier_list** presults)
1085 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1086 return this->parameters(presults, NULL);
1089 Location location = this->location();
1090 Type* type = this->type();
1091 if (type->is_error_type())
1096 Typed_identifier_list* til = new Typed_identifier_list();
1097 til->push_back(Typed_identifier("", type, location));
1103 // Block = "{" [ StatementList ] "}" .
1105 // Returns the location of the closing brace.
1110 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1112 Location loc = this->location();
1113 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1114 && this->advance_token()->is_op(OPERATOR_LCURLY))
1115 error_at(loc, "unexpected semicolon or newline before %<{%>");
1118 error_at(this->location(), "expected %<{%>");
1119 return Linemap::unknown_location();
1123 const Token* token = this->advance_token();
1125 if (!token->is_op(OPERATOR_RCURLY))
1127 this->statement_list();
1128 token = this->peek_token();
1129 if (!token->is_op(OPERATOR_RCURLY))
1131 if (!token->is_eof() || !saw_errors())
1132 error_at(this->location(), "expected %<}%>");
1134 this->gogo_->mark_locals_used();
1136 // Skip ahead to the end of the block, in hopes of avoiding
1137 // lots of meaningless errors.
1138 Location ret = token->location();
1140 while (!token->is_eof())
1142 if (token->is_op(OPERATOR_LCURLY))
1144 else if (token->is_op(OPERATOR_RCURLY))
1149 this->advance_token();
1153 token = this->advance_token();
1154 ret = token->location();
1160 Location ret = token->location();
1161 this->advance_token();
1165 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1166 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1169 Parse::interface_type()
1171 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1172 Location location = this->location();
1174 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1176 Location token_loc = this->location();
1177 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1178 && this->advance_token()->is_op(OPERATOR_LCURLY))
1179 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1182 error_at(this->location(), "expected %<{%>");
1183 return Type::make_error_type();
1186 this->advance_token();
1188 Typed_identifier_list* methods = new Typed_identifier_list();
1189 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1191 this->method_spec(methods);
1192 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1194 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1196 this->method_spec(methods);
1198 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1200 error_at(this->location(), "expected %<}%>");
1201 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1203 if (this->peek_token()->is_eof())
1204 return Type::make_error_type();
1208 this->advance_token();
1210 if (methods->empty())
1216 Interface_type* ret = Type::make_interface_type(methods, location);
1217 this->gogo_->record_interface_type(ret);
1221 // MethodSpec = MethodName Signature | InterfaceTypeName .
1222 // MethodName = identifier .
1223 // InterfaceTypeName = TypeName .
1226 Parse::method_spec(Typed_identifier_list* methods)
1228 const Token* token = this->peek_token();
1229 if (!token->is_identifier())
1231 error_at(this->location(), "expected identifier");
1235 std::string name = token->identifier();
1236 bool is_exported = token->is_identifier_exported();
1237 Location location = token->location();
1239 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1241 // This is a MethodName.
1242 name = this->gogo_->pack_hidden_name(name, is_exported);
1243 Type* type = this->signature(NULL, location);
1246 methods->push_back(Typed_identifier(name, type, location));
1250 this->unget_token(Token::make_identifier_token(name, is_exported,
1252 Type* type = this->type_name(false);
1253 if (type->is_error_type()
1254 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1255 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1257 if (this->peek_token()->is_op(OPERATOR_COMMA))
1258 error_at(this->location(),
1259 "name list not allowed in interface type");
1261 error_at(location, "expected signature or type name");
1262 this->gogo_->mark_locals_used();
1263 token = this->peek_token();
1264 while (!token->is_eof()
1265 && !token->is_op(OPERATOR_SEMICOLON)
1266 && !token->is_op(OPERATOR_RCURLY))
1267 token = this->advance_token();
1270 // This must be an interface type, but we can't check that now.
1271 // We check it and pull out the methods in
1272 // Interface_type::do_verify.
1273 methods->push_back(Typed_identifier("", type, location));
1277 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1280 Parse::declaration()
1282 const Token* token = this->peek_token();
1283 if (token->is_keyword(KEYWORD_CONST))
1285 else if (token->is_keyword(KEYWORD_TYPE))
1287 else if (token->is_keyword(KEYWORD_VAR))
1289 else if (token->is_keyword(KEYWORD_FUNC))
1290 this->function_decl();
1293 error_at(this->location(), "expected declaration");
1294 this->advance_token();
1299 Parse::declaration_may_start_here()
1301 const Token* token = this->peek_token();
1302 return (token->is_keyword(KEYWORD_CONST)
1303 || token->is_keyword(KEYWORD_TYPE)
1304 || token->is_keyword(KEYWORD_VAR)
1305 || token->is_keyword(KEYWORD_FUNC));
1308 // Decl<P> = P | "(" [ List<P> ] ")" .
1311 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1313 if (this->peek_token()->is_eof())
1316 error_at(this->location(), "unexpected end of file");
1320 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1324 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1326 this->list(pfn, varg, true);
1327 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1329 error_at(this->location(), "missing %<)%>");
1330 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1332 if (this->peek_token()->is_eof())
1337 this->advance_token();
1341 // List<P> = P { ";" P } [ ";" ] .
1343 // In order to pick up the trailing semicolon we need to know what
1344 // might follow. This is either a '}' or a ')'.
1347 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1350 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1351 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1352 || this->peek_token()->is_op(OPERATOR_COMMA))
1354 if (this->peek_token()->is_op(OPERATOR_COMMA))
1355 error_at(this->location(), "unexpected comma");
1356 if (this->advance_token()->is_op(follow))
1362 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1367 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1368 this->advance_token();
1371 Type* last_type = NULL;
1372 Expression_list* last_expr_list = NULL;
1374 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1375 this->const_spec(&last_type, &last_expr_list);
1378 this->advance_token();
1379 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1381 this->const_spec(&last_type, &last_expr_list);
1382 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1383 this->advance_token();
1384 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1386 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1387 if (!this->skip_past_error(OPERATOR_RPAREN))
1391 this->advance_token();
1394 if (last_expr_list != NULL)
1395 delete last_expr_list;
1398 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1401 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1403 Typed_identifier_list til;
1404 this->identifier_list(&til);
1407 if (this->type_may_start_here())
1409 type = this->type();
1411 *last_expr_list = NULL;
1414 Expression_list *expr_list;
1415 if (!this->peek_token()->is_op(OPERATOR_EQ))
1417 if (*last_expr_list == NULL)
1419 error_at(this->location(), "expected %<=%>");
1423 expr_list = new Expression_list;
1424 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1425 p != (*last_expr_list)->end();
1427 expr_list->push_back((*p)->copy());
1431 this->advance_token();
1432 expr_list = this->expression_list(NULL, false, true);
1434 if (*last_expr_list != NULL)
1435 delete *last_expr_list;
1436 *last_expr_list = expr_list;
1439 Expression_list::const_iterator pe = expr_list->begin();
1440 for (Typed_identifier_list::iterator pi = til.begin();
1444 if (pe == expr_list->end())
1446 error_at(this->location(), "not enough initializers");
1452 if (!Gogo::is_sink_name(pi->name()))
1453 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1455 if (pe != expr_list->end())
1456 error_at(this->location(), "too many initializers");
1458 this->increment_iota();
1463 // TypeDecl = "type" Decl<TypeSpec> .
1468 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1469 this->advance_token();
1470 this->decl(&Parse::type_spec, NULL);
1473 // TypeSpec = identifier Type .
1476 Parse::type_spec(void*)
1478 const Token* token = this->peek_token();
1479 if (!token->is_identifier())
1481 error_at(this->location(), "expected identifier");
1484 std::string name = token->identifier();
1485 bool is_exported = token->is_identifier_exported();
1486 Location location = token->location();
1487 token = this->advance_token();
1489 // The scope of the type name starts at the point where the
1490 // identifier appears in the source code. We implement this by
1491 // declaring the type before we read the type definition.
1492 Named_object* named_type = NULL;
1495 name = this->gogo_->pack_hidden_name(name, is_exported);
1496 named_type = this->gogo_->declare_type(name, location);
1500 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1501 type = this->type();
1504 error_at(this->location(),
1505 "unexpected semicolon or newline in type declaration");
1506 type = Type::make_error_type();
1507 this->advance_token();
1510 if (type->is_error_type())
1512 this->gogo_->mark_locals_used();
1513 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1514 && !this->peek_token()->is_eof())
1515 this->advance_token();
1520 if (named_type->is_type_declaration())
1522 Type* ftype = type->forwarded();
1523 if (ftype->forward_declaration_type() != NULL
1524 && (ftype->forward_declaration_type()->named_object()
1527 error_at(location, "invalid recursive type");
1528 type = Type::make_error_type();
1531 this->gogo_->define_type(named_type,
1532 Type::make_named_type(named_type, type,
1534 go_assert(named_type->package() == NULL);
1538 // This will probably give a redefinition error.
1539 this->gogo_->add_type(name, type, location);
1544 // VarDecl = "var" Decl<VarSpec> .
1549 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1550 this->advance_token();
1551 this->decl(&Parse::var_spec, NULL);
1554 // VarSpec = IdentifierList
1555 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1558 Parse::var_spec(void*)
1560 // Get the variable names.
1561 Typed_identifier_list til;
1562 this->identifier_list(&til);
1564 Location location = this->location();
1567 Expression_list* init = NULL;
1568 if (!this->peek_token()->is_op(OPERATOR_EQ))
1570 type = this->type();
1571 if (type->is_error_type())
1573 this->gogo_->mark_locals_used();
1574 while (!this->peek_token()->is_op(OPERATOR_EQ)
1575 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1576 && !this->peek_token()->is_eof())
1577 this->advance_token();
1579 if (this->peek_token()->is_op(OPERATOR_EQ))
1581 this->advance_token();
1582 init = this->expression_list(NULL, false, true);
1587 this->advance_token();
1588 init = this->expression_list(NULL, false, true);
1591 this->init_vars(&til, type, init, false, location);
1597 // Create variables. TIL is a list of variable names. If TYPE is not
1598 // NULL, it is the type of all the variables. If INIT is not NULL, it
1599 // is an initializer list for the variables.
1602 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1603 Expression_list* init, bool is_coloneq,
1606 // Check for an initialization which can yield multiple values.
1607 if (init != NULL && init->size() == 1 && til->size() > 1)
1609 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1612 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1615 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1618 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1619 is_coloneq, location))
1623 if (init != NULL && init->size() != til->size())
1625 if (init->empty() || !init->front()->is_error_expression())
1626 error_at(location, "wrong number of initializations");
1629 type = Type::make_error_type();
1632 // Note that INIT was already parsed with the old name bindings, so
1633 // we don't have to worry that it will accidentally refer to the
1634 // newly declared variables.
1636 Expression_list::const_iterator pexpr;
1638 pexpr = init->begin();
1639 bool any_new = false;
1640 for (Typed_identifier_list::const_iterator p = til->begin();
1645 go_assert(pexpr != init->end());
1646 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1652 go_assert(pexpr == init->end());
1653 if (is_coloneq && !any_new)
1654 error_at(location, "variables redeclared but no variable is new");
1657 // See if we need to initialize a list of variables from a function
1658 // call. This returns true if we have set up the variables and the
1662 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1663 Expression* expr, bool is_coloneq,
1666 Call_expression* call = expr->call_expression();
1670 // This is a function call. We can't check here whether it returns
1671 // the right number of values, but it might. Declare the variables,
1672 // and then assign the results of the call to them.
1674 Named_object* first_var = NULL;
1675 unsigned int index = 0;
1676 bool any_new = false;
1677 for (Typed_identifier_list::const_iterator pv = vars->begin();
1681 Expression* init = Expression::make_call_result(call, index);
1682 Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
1685 if (this->gogo_->in_global_scope() && no->is_variable())
1687 if (first_var == NULL)
1691 // The subsequent vars have an implicit dependency on
1692 // the first one, so that everything gets initialized in
1693 // the right order and so that we detect cycles
1695 this->gogo_->record_var_depends_on(no->var_value(), first_var);
1700 if (is_coloneq && !any_new)
1701 error_at(location, "variables redeclared but no variable is new");
1706 // See if we need to initialize a pair of values from a map index
1707 // expression. This returns true if we have set up the variables and
1708 // the initialization.
1711 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1712 Expression* expr, bool is_coloneq,
1715 Index_expression* index = expr->index_expression();
1718 if (vars->size() != 2)
1721 // This is an index which is being assigned to two variables. It
1722 // must be a map index. Declare the variables, and then assign the
1723 // results of the map index.
1724 bool any_new = false;
1725 Typed_identifier_list::const_iterator p = vars->begin();
1726 Expression* init = type == NULL ? index : NULL;
1727 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1728 type == NULL, &any_new);
1729 if (type == NULL && any_new && val_no->is_variable())
1730 val_no->var_value()->set_type_from_init_tuple();
1731 Expression* val_var = Expression::make_var_reference(val_no, location);
1734 Type* var_type = type;
1735 if (var_type == NULL)
1736 var_type = Type::lookup_bool_type();
1737 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1739 Expression* present_var = Expression::make_var_reference(no, location);
1741 if (is_coloneq && !any_new)
1742 error_at(location, "variables redeclared but no variable is new");
1744 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1747 if (!this->gogo_->in_global_scope())
1748 this->gogo_->add_statement(s);
1749 else if (!val_no->is_sink())
1751 if (val_no->is_variable())
1752 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1754 else if (!no->is_sink())
1756 if (no->is_variable())
1757 no->var_value()->add_preinit_statement(this->gogo_, s);
1761 // Execute the map index expression just so that we can fail if
1763 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1765 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1771 // See if we need to initialize a pair of values from a receive
1772 // expression. This returns true if we have set up the variables and
1773 // the initialization.
1776 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1777 Expression* expr, bool is_coloneq,
1780 Receive_expression* receive = expr->receive_expression();
1781 if (receive == NULL)
1783 if (vars->size() != 2)
1786 // This is a receive expression which is being assigned to two
1787 // variables. Declare the variables, and then assign the results of
1789 bool any_new = false;
1790 Typed_identifier_list::const_iterator p = vars->begin();
1791 Expression* init = type == NULL ? receive : NULL;
1792 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1793 type == NULL, &any_new);
1794 if (type == NULL && any_new && val_no->is_variable())
1795 val_no->var_value()->set_type_from_init_tuple();
1796 Expression* val_var = Expression::make_var_reference(val_no, location);
1799 Type* var_type = type;
1800 if (var_type == NULL)
1801 var_type = Type::lookup_bool_type();
1802 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1804 Expression* received_var = Expression::make_var_reference(no, location);
1806 if (is_coloneq && !any_new)
1807 error_at(location, "variables redeclared but no variable is new");
1809 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1814 if (!this->gogo_->in_global_scope())
1815 this->gogo_->add_statement(s);
1816 else if (!val_no->is_sink())
1818 if (val_no->is_variable())
1819 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1821 else if (!no->is_sink())
1823 if (no->is_variable())
1824 no->var_value()->add_preinit_statement(this->gogo_, s);
1828 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1830 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1836 // See if we need to initialize a pair of values from a type guard
1837 // expression. This returns true if we have set up the variables and
1838 // the initialization.
1841 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1842 Type* type, Expression* expr,
1843 bool is_coloneq, Location location)
1845 Type_guard_expression* type_guard = expr->type_guard_expression();
1846 if (type_guard == NULL)
1848 if (vars->size() != 2)
1851 // This is a type guard expression which is being assigned to two
1852 // variables. Declare the variables, and then assign the results of
1854 bool any_new = false;
1855 Typed_identifier_list::const_iterator p = vars->begin();
1856 Type* var_type = type;
1857 if (var_type == NULL)
1858 var_type = type_guard->type();
1859 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1861 Expression* val_var = Expression::make_var_reference(val_no, location);
1865 if (var_type == NULL)
1866 var_type = Type::lookup_bool_type();
1867 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1869 Expression* ok_var = Expression::make_var_reference(no, location);
1871 Expression* texpr = type_guard->expr();
1872 Type* t = type_guard->type();
1873 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1877 if (is_coloneq && !any_new)
1878 error_at(location, "variables redeclared but no variable is new");
1880 if (!this->gogo_->in_global_scope())
1881 this->gogo_->add_statement(s);
1882 else if (!val_no->is_sink())
1884 if (val_no->is_variable())
1885 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1887 else if (!no->is_sink())
1889 if (no->is_variable())
1890 no->var_value()->add_preinit_statement(this->gogo_, s);
1894 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1895 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1901 // Create a single variable. If IS_COLONEQ is true, we permit
1902 // redeclarations in the same block, and we set *IS_NEW when we find a
1903 // new variable which is not a redeclaration.
1906 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1907 bool is_coloneq, bool type_from_init, bool* is_new)
1909 Location location = tid.location();
1911 if (Gogo::is_sink_name(tid.name()))
1913 if (!type_from_init && init != NULL)
1915 if (this->gogo_->in_global_scope())
1916 return this->create_dummy_global(type, init, location);
1917 else if (type == NULL)
1918 this->gogo_->add_statement(Statement::make_statement(init, true));
1921 // With both a type and an initializer, create a dummy
1922 // variable so that we will check whether the
1923 // initializer can be assigned to the type.
1924 Variable* var = new Variable(type, init, false, false, false,
1929 snprintf(buf, sizeof buf, "sink$%d", count);
1931 return this->gogo_->add_variable(buf, var);
1935 this->gogo_->add_type_to_verify(type);
1936 return this->gogo_->add_sink();
1941 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1943 && (no->is_variable() || no->is_result_variable()))
1945 // INIT may be NULL even when IS_COLONEQ is true for cases
1946 // like v, ok := x.(int).
1947 if (!type_from_init && init != NULL)
1949 Expression *v = Expression::make_var_reference(no, location);
1950 Statement *s = Statement::make_assignment(v, init, location);
1951 this->gogo_->add_statement(s);
1957 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1958 false, false, location);
1959 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1960 if (!no->is_variable())
1962 // The name is already defined, so we just gave an error.
1963 return this->gogo_->add_sink();
1968 // Create a dummy global variable to force an initializer to be run in
1969 // the right place. This is used when a sink variable is initialized
1973 Parse::create_dummy_global(Type* type, Expression* init,
1976 if (type == NULL && init == NULL)
1977 type = Type::lookup_bool_type();
1978 Variable* var = new Variable(type, init, true, false, false, location);
1981 snprintf(buf, sizeof buf, "_.%d", count);
1983 return this->gogo_->add_variable(buf, var);
1986 // SimpleVarDecl = identifier ":=" Expression .
1988 // We've already seen the identifier.
1990 // FIXME: We also have to implement
1991 // IdentifierList ":=" ExpressionList
1992 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1993 // tuple assignments here as well.
1995 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
1996 // side may be a composite literal.
1998 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2001 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2002 // guard (var := expr.("type") using the literal keyword "type").
2005 Parse::simple_var_decl_or_assignment(const std::string& name,
2007 bool may_be_composite_lit,
2008 Range_clause* p_range_clause,
2009 Type_switch* p_type_switch)
2011 Typed_identifier_list til;
2012 til.push_back(Typed_identifier(name, NULL, location));
2014 // We've seen one identifier. If we see a comma now, this could be
2016 if (this->peek_token()->is_op(OPERATOR_COMMA))
2018 go_assert(p_type_switch == NULL);
2021 const Token* token = this->advance_token();
2022 if (!token->is_identifier())
2025 std::string id = token->identifier();
2026 bool is_id_exported = token->is_identifier_exported();
2027 Location id_location = token->location();
2029 token = this->advance_token();
2030 if (!token->is_op(OPERATOR_COMMA))
2032 if (token->is_op(OPERATOR_COLONEQ))
2034 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2035 til.push_back(Typed_identifier(id, NULL, location));
2038 this->unget_token(Token::make_identifier_token(id,
2044 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2045 til.push_back(Typed_identifier(id, NULL, location));
2048 // We have a comma separated list of identifiers in TIL. If the
2049 // next token is COLONEQ, then this is a simple var decl, and we
2050 // have the complete list of identifiers. If the next token is
2051 // not COLONEQ, then the only valid parse is a tuple assignment.
2052 // The list of identifiers we have so far is really a list of
2053 // expressions. There are more expressions following.
2055 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2057 Expression_list* exprs = new Expression_list;
2058 for (Typed_identifier_list::const_iterator p = til.begin();
2061 exprs->push_back(this->id_to_expression(p->name(),
2064 Expression_list* more_exprs =
2065 this->expression_list(NULL, true, may_be_composite_lit);
2066 for (Expression_list::const_iterator p = more_exprs->begin();
2067 p != more_exprs->end();
2069 exprs->push_back(*p);
2072 this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
2077 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2078 const Token* token = this->advance_token();
2080 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2082 this->range_clause_decl(&til, p_range_clause);
2086 Expression_list* init;
2087 if (p_type_switch == NULL)
2088 init = this->expression_list(NULL, false, may_be_composite_lit);
2091 bool is_type_switch = false;
2092 Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
2093 may_be_composite_lit,
2097 p_type_switch->found = true;
2098 p_type_switch->name = name;
2099 p_type_switch->location = location;
2100 p_type_switch->expr = expr;
2104 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2106 init = new Expression_list();
2107 init->push_back(expr);
2111 this->advance_token();
2112 init = this->expression_list(expr, false, may_be_composite_lit);
2116 this->init_vars(&til, NULL, init, true, location);
2119 // FunctionDecl = "func" identifier Signature [ Block ] .
2120 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2122 // Deprecated gcc extension:
2123 // FunctionDecl = "func" identifier Signature
2124 // __asm__ "(" string_lit ")" .
2125 // This extension means a function whose real name is the identifier
2126 // inside the asm. This extension will be removed at some future
2127 // date. It has been replaced with //extern comments.
2130 Parse::function_decl()
2132 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2133 Location location = this->location();
2134 std::string extern_name = this->lex_->extern_name();
2135 const Token* token = this->advance_token();
2137 Typed_identifier* rec = NULL;
2138 if (token->is_op(OPERATOR_LPAREN))
2140 rec = this->receiver();
2141 token = this->peek_token();
2144 if (!token->is_identifier())
2146 error_at(this->location(), "expected function name");
2151 this->gogo_->pack_hidden_name(token->identifier(),
2152 token->is_identifier_exported());
2154 this->advance_token();
2156 Function_type* fntype = this->signature(rec, this->location());
2158 Named_object* named_object = NULL;
2160 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2162 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2164 error_at(this->location(), "expected %<(%>");
2167 token = this->advance_token();
2168 if (!token->is_string())
2170 error_at(this->location(), "expected string");
2173 std::string asm_name = token->string_value();
2174 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2176 error_at(this->location(), "expected %<)%>");
2179 this->advance_token();
2180 if (!Gogo::is_sink_name(name))
2182 named_object = this->gogo_->declare_function(name, fntype, location);
2183 if (named_object->is_function_declaration())
2184 named_object->func_declaration_value()->set_asm_name(asm_name);
2188 // Check for the easy error of a newline before the opening brace.
2189 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2191 Location semi_loc = this->location();
2192 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2193 error_at(this->location(),
2194 "unexpected semicolon or newline before %<{%>");
2196 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2200 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2202 if (named_object == NULL && !Gogo::is_sink_name(name))
2205 this->gogo_->add_erroneous_name(name);
2208 named_object = this->gogo_->declare_function(name, fntype,
2210 if (!extern_name.empty()
2211 && named_object->is_function_declaration())
2213 Function_declaration* fd =
2214 named_object->func_declaration_value();
2215 fd->set_asm_name(extern_name);
2222 bool hold_is_erroneous_function = this->is_erroneous_function_;
2225 fntype = Type::make_function_type(NULL, NULL, NULL, location);
2226 this->is_erroneous_function_ = true;
2227 if (!Gogo::is_sink_name(name))
2228 this->gogo_->add_erroneous_name(name);
2229 name = this->gogo_->pack_hidden_name("_", false);
2231 this->gogo_->start_function(name, fntype, true, location);
2232 Location end_loc = this->block();
2233 this->gogo_->finish_function(end_loc);
2234 this->is_erroneous_function_ = hold_is_erroneous_function;
2238 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2239 // BaseTypeName = identifier .
2244 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2247 const Token* token = this->advance_token();
2248 Location location = token->location();
2249 if (!token->is_op(OPERATOR_MULT))
2251 if (!token->is_identifier())
2253 error_at(this->location(), "method has no receiver");
2254 this->gogo_->mark_locals_used();
2255 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2256 token = this->advance_token();
2257 if (!token->is_eof())
2258 this->advance_token();
2261 name = token->identifier();
2262 bool is_exported = token->is_identifier_exported();
2263 token = this->advance_token();
2264 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2266 // An identifier followed by something other than a dot or a
2267 // right parenthesis must be a receiver name followed by a
2269 name = this->gogo_->pack_hidden_name(name, is_exported);
2273 // This must be a type name.
2274 this->unget_token(Token::make_identifier_token(name, is_exported,
2276 token = this->peek_token();
2281 // Here the receiver name is in NAME (it is empty if the receiver is
2282 // unnamed) and TOKEN is the first token in the type.
2284 bool is_pointer = false;
2285 if (token->is_op(OPERATOR_MULT))
2288 token = this->advance_token();
2291 if (!token->is_identifier())
2293 error_at(this->location(), "expected receiver name or type");
2294 this->gogo_->mark_locals_used();
2295 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2296 while (!token->is_eof())
2298 token = this->advance_token();
2299 if (token->is_op(OPERATOR_LPAREN))
2301 else if (token->is_op(OPERATOR_RPAREN))
2308 if (!token->is_eof())
2309 this->advance_token();
2313 Type* type = this->type_name(true);
2315 if (is_pointer && !type->is_error_type())
2316 type = Type::make_pointer_type(type);
2318 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2319 this->advance_token();
2322 if (this->peek_token()->is_op(OPERATOR_COMMA))
2323 error_at(this->location(), "method has multiple receivers");
2325 error_at(this->location(), "expected %<)%>");
2326 this->gogo_->mark_locals_used();
2327 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2328 token = this->advance_token();
2329 if (!token->is_eof())
2330 this->advance_token();
2334 return new Typed_identifier(name, type, location);
2337 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2338 // Literal = BasicLit | CompositeLit | FunctionLit .
2339 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2341 // If MAY_BE_SINK is true, this operand may be "_".
2344 Parse::operand(bool may_be_sink)
2346 const Token* token = this->peek_token();
2348 switch (token->classification())
2350 case Token::TOKEN_IDENTIFIER:
2352 Location location = token->location();
2353 std::string id = token->identifier();
2354 bool is_exported = token->is_identifier_exported();
2355 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2357 Named_object* in_function;
2358 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2360 Package* package = NULL;
2361 if (named_object != NULL && named_object->is_package())
2363 if (!this->advance_token()->is_op(OPERATOR_DOT)
2364 || !this->advance_token()->is_identifier())
2366 error_at(location, "unexpected reference to package");
2367 return Expression::make_error(location);
2369 package = named_object->package_value();
2370 package->set_used();
2371 id = this->peek_token()->identifier();
2372 is_exported = this->peek_token()->is_identifier_exported();
2373 packed = this->gogo_->pack_hidden_name(id, is_exported);
2374 named_object = package->lookup(packed);
2375 location = this->location();
2376 go_assert(in_function == NULL);
2379 this->advance_token();
2381 if (named_object != NULL
2382 && named_object->is_type()
2383 && !named_object->type_value()->is_visible())
2385 go_assert(package != NULL);
2386 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2387 Gogo::message_name(package->name()).c_str(),
2388 Gogo::message_name(id).c_str());
2389 return Expression::make_error(location);
2393 if (named_object == NULL)
2395 if (package != NULL)
2397 std::string n1 = Gogo::message_name(package->name());
2398 std::string n2 = Gogo::message_name(id);
2401 ("invalid reference to unexported identifier "
2403 n1.c_str(), n2.c_str());
2406 "reference to undefined identifier %<%s.%s%>",
2407 n1.c_str(), n2.c_str());
2408 return Expression::make_error(location);
2411 named_object = this->gogo_->add_unknown_name(packed, location);
2414 if (in_function != NULL
2415 && in_function != this->gogo_->current_function()
2416 && (named_object->is_variable()
2417 || named_object->is_result_variable()))
2418 return this->enclosing_var_reference(in_function, named_object,
2421 switch (named_object->classification())
2423 case Named_object::NAMED_OBJECT_CONST:
2424 return Expression::make_const_reference(named_object, location);
2425 case Named_object::NAMED_OBJECT_TYPE:
2426 return Expression::make_type(named_object->type_value(), location);
2427 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2429 Type* t = Type::make_forward_declaration(named_object);
2430 return Expression::make_type(t, location);
2432 case Named_object::NAMED_OBJECT_VAR:
2433 case Named_object::NAMED_OBJECT_RESULT_VAR:
2434 this->mark_var_used(named_object);
2435 return Expression::make_var_reference(named_object, location);
2436 case Named_object::NAMED_OBJECT_SINK:
2438 return Expression::make_sink(location);
2441 error_at(location, "cannot use _ as value");
2442 return Expression::make_error(location);
2444 case Named_object::NAMED_OBJECT_FUNC:
2445 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2446 return Expression::make_func_reference(named_object, NULL,
2448 case Named_object::NAMED_OBJECT_UNKNOWN:
2450 Unknown_expression* ue =
2451 Expression::make_unknown_reference(named_object, location);
2452 if (this->is_erroneous_function_)
2453 ue->set_no_error_message();
2456 case Named_object::NAMED_OBJECT_ERRONEOUS:
2457 return Expression::make_error(location);
2464 case Token::TOKEN_STRING:
2465 ret = Expression::make_string(token->string_value(), token->location());
2466 this->advance_token();
2469 case Token::TOKEN_CHARACTER:
2470 ret = Expression::make_character(token->character_value(), NULL,
2472 this->advance_token();
2475 case Token::TOKEN_INTEGER:
2476 ret = Expression::make_integer(token->integer_value(), NULL,
2478 this->advance_token();
2481 case Token::TOKEN_FLOAT:
2482 ret = Expression::make_float(token->float_value(), NULL,
2484 this->advance_token();
2487 case Token::TOKEN_IMAGINARY:
2490 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2491 ret = Expression::make_complex(&zero, token->imaginary_value(),
2492 NULL, token->location());
2494 this->advance_token();
2498 case Token::TOKEN_KEYWORD:
2499 switch (token->keyword())
2502 return this->function_lit();
2504 case KEYWORD_INTERFACE:
2506 case KEYWORD_STRUCT:
2508 Location location = token->location();
2509 return Expression::make_type(this->type(), location);
2516 case Token::TOKEN_OPERATOR:
2517 if (token->is_op(OPERATOR_LPAREN))
2519 this->advance_token();
2520 ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL);
2521 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2522 error_at(this->location(), "missing %<)%>");
2524 this->advance_token();
2527 else if (token->is_op(OPERATOR_LSQUARE))
2529 // Here we call array_type directly, as this is the only
2530 // case where an ellipsis is permitted for an array type.
2531 Location location = token->location();
2532 return Expression::make_type(this->array_type(true), location);
2540 error_at(this->location(), "expected operand");
2541 return Expression::make_error(this->location());
2544 // Handle a reference to a variable in an enclosing function. We add
2545 // it to a list of such variables. We return a reference to a field
2546 // in a struct which will be passed on the static chain when calling
2547 // the current function.
2550 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2553 go_assert(var->is_variable() || var->is_result_variable());
2555 this->mark_var_used(var);
2557 Named_object* this_function = this->gogo_->current_function();
2558 Named_object* closure = this_function->func_value()->closure_var();
2560 Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2561 std::pair<Enclosing_vars::iterator, bool> ins =
2562 this->enclosing_vars_.insert(ev);
2565 // This is a variable we have not seen before. Add a new field
2566 // to the closure type.
2567 this_function->func_value()->add_closure_field(var, location);
2570 Expression* closure_ref = Expression::make_var_reference(closure,
2572 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2574 // The closure structure holds pointers to the variables, so we need
2575 // to introduce an indirection.
2576 Expression* e = Expression::make_field_reference(closure_ref,
2579 e = Expression::make_unary(OPERATOR_MULT, e, location);
2583 // CompositeLit = LiteralType LiteralValue .
2584 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2585 // SliceType | MapType | TypeName .
2586 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2587 // ElementList = Element { "," Element } .
2588 // Element = [ Key ":" ] Value .
2589 // Key = FieldName | ElementIndex .
2590 // FieldName = identifier .
2591 // ElementIndex = Expression .
2592 // Value = Expression | LiteralValue .
2594 // We have already seen the type if there is one, and we are now
2595 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2596 // will be seen here as an array type whose length is "nil". The
2597 // DEPTH parameter is non-zero if this is an embedded composite
2598 // literal and the type was omitted. It gives the number of steps up
2599 // to the type which was provided. E.g., in [][]int{{1}} it will be
2600 // 1. In [][][]int{{{1}}} it will be 2.
2603 Parse::composite_lit(Type* type, int depth, Location location)
2605 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2606 this->advance_token();
2608 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2610 this->advance_token();
2611 return Expression::make_composite_literal(type, depth, false, NULL,
2615 bool has_keys = false;
2616 Expression_list* vals = new Expression_list;
2620 bool is_type_omitted = false;
2622 const Token* token = this->peek_token();
2624 if (token->is_identifier())
2626 std::string identifier = token->identifier();
2627 bool is_exported = token->is_identifier_exported();
2628 Location location = token->location();
2630 if (this->advance_token()->is_op(OPERATOR_COLON))
2632 // This may be a field name. We don't know for sure--it
2633 // could also be an expression for an array index. We
2634 // don't want to parse it as an expression because may
2635 // trigger various errors, e.g., if this identifier
2636 // happens to be the name of a package.
2637 Gogo* gogo = this->gogo_;
2638 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2644 this->unget_token(Token::make_identifier_token(identifier,
2647 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2650 else if (!token->is_op(OPERATOR_LCURLY))
2651 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2654 // This must be a composite literal inside another composite
2655 // literal, with the type omitted for the inner one.
2656 val = this->composite_lit(type, depth + 1, token->location());
2657 is_type_omitted = true;
2660 token = this->peek_token();
2661 if (!token->is_op(OPERATOR_COLON))
2664 vals->push_back(NULL);
2668 if (is_type_omitted && !val->is_error_expression())
2670 error_at(this->location(), "unexpected %<:%>");
2671 val = Expression::make_error(this->location());
2674 this->advance_token();
2676 if (!has_keys && !vals->empty())
2678 Expression_list* newvals = new Expression_list;
2679 for (Expression_list::const_iterator p = vals->begin();
2683 newvals->push_back(NULL);
2684 newvals->push_back(*p);
2691 if (val->unknown_expression() != NULL)
2692 val->unknown_expression()->set_is_composite_literal_key();
2694 vals->push_back(val);
2696 if (!token->is_op(OPERATOR_LCURLY))
2697 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2700 // This must be a composite literal inside another
2701 // composite literal, with the type omitted for the
2703 val = this->composite_lit(type, depth + 1, token->location());
2706 token = this->peek_token();
2709 vals->push_back(val);
2711 if (token->is_op(OPERATOR_COMMA))
2713 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2715 this->advance_token();
2719 else if (token->is_op(OPERATOR_RCURLY))
2721 this->advance_token();
2726 error_at(this->location(), "expected %<,%> or %<}%>");
2728 this->gogo_->mark_locals_used();
2730 while (!token->is_eof()
2731 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2733 if (token->is_op(OPERATOR_LCURLY))
2735 else if (token->is_op(OPERATOR_RCURLY))
2737 token = this->advance_token();
2739 if (token->is_op(OPERATOR_RCURLY))
2740 this->advance_token();
2742 return Expression::make_error(location);
2746 return Expression::make_composite_literal(type, depth, has_keys, vals,
2750 // FunctionLit = "func" Signature Block .
2753 Parse::function_lit()
2755 Location location = this->location();
2756 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2757 this->advance_token();
2759 Enclosing_vars hold_enclosing_vars;
2760 hold_enclosing_vars.swap(this->enclosing_vars_);
2762 Function_type* type = this->signature(NULL, location);
2763 bool fntype_is_error = false;
2766 type = Type::make_function_type(NULL, NULL, NULL, location);
2767 fntype_is_error = true;
2770 // For a function literal, the next token must be a '{'. If we
2771 // don't see that, then we may have a type expression.
2772 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2773 return Expression::make_type(type, location);
2775 bool hold_is_erroneous_function = this->is_erroneous_function_;
2776 if (fntype_is_error)
2777 this->is_erroneous_function_ = true;
2779 Bc_stack* hold_break_stack = this->break_stack_;
2780 Bc_stack* hold_continue_stack = this->continue_stack_;
2781 this->break_stack_ = NULL;
2782 this->continue_stack_ = NULL;
2784 Named_object* no = this->gogo_->start_function("", type, true, location);
2786 Location end_loc = this->block();
2788 this->gogo_->finish_function(end_loc);
2790 if (this->break_stack_ != NULL)
2791 delete this->break_stack_;
2792 if (this->continue_stack_ != NULL)
2793 delete this->continue_stack_;
2794 this->break_stack_ = hold_break_stack;
2795 this->continue_stack_ = hold_continue_stack;
2797 this->is_erroneous_function_ = hold_is_erroneous_function;
2799 hold_enclosing_vars.swap(this->enclosing_vars_);
2801 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2804 return Expression::make_func_reference(no, closure, location);
2807 // Create a closure for the nested function FUNCTION. This is based
2808 // on ENCLOSING_VARS, which is a list of all variables defined in
2809 // enclosing functions and referenced from FUNCTION. A closure is the
2810 // address of a struct which contains the addresses of all the
2811 // referenced variables. This returns NULL if no closure is required.
2814 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2817 if (enclosing_vars->empty())
2820 // Get the variables in order by their field index.
2822 size_t enclosing_var_count = enclosing_vars->size();
2823 std::vector<Enclosing_var> ev(enclosing_var_count);
2824 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2825 p != enclosing_vars->end();
2827 ev[p->index()] = *p;
2829 // Build an initializer for a composite literal of the closure's
2832 Named_object* enclosing_function = this->gogo_->current_function();
2833 Expression_list* initializer = new Expression_list;
2834 for (size_t i = 0; i < enclosing_var_count; ++i)
2836 go_assert(ev[i].index() == i);
2837 Named_object* var = ev[i].var();
2839 if (ev[i].in_function() == enclosing_function)
2840 ref = Expression::make_var_reference(var, location);
2842 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2844 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2846 initializer->push_back(refaddr);
2849 Named_object* closure_var = function->func_value()->closure_var();
2850 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2851 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2853 return Expression::make_heap_composite(cv, location);
2856 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2858 // If MAY_BE_SINK is true, this expression may be "_".
2860 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2863 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2864 // guard (var := expr.("type") using the literal keyword "type").
2867 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2868 bool* is_type_switch)
2870 Location start_loc = this->location();
2871 bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2873 Expression* ret = this->operand(may_be_sink);
2875 // An unknown name followed by a curly brace must be a composite
2876 // literal, and the unknown name must be a type.
2877 if (may_be_composite_lit
2878 && !is_parenthesized
2879 && ret->unknown_expression() != NULL
2880 && this->peek_token()->is_op(OPERATOR_LCURLY))
2882 Named_object* no = ret->unknown_expression()->named_object();
2883 Type* type = Type::make_forward_declaration(no);
2884 ret = Expression::make_type(type, ret->location());
2887 // We handle composite literals and type casts here, as it is the
2888 // easiest way to handle types which are in parentheses, as in
2890 if (ret->is_type_expression())
2892 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2894 if (!may_be_composite_lit)
2896 Type* t = ret->type();
2897 if (t->named_type() != NULL
2898 || t->forward_declaration_type() != NULL)
2900 _("parentheses required around this composite literal"
2901 "to avoid parsing ambiguity"));
2903 else if (is_parenthesized)
2905 "cannot parenthesize type in composite literal");
2906 ret = this->composite_lit(ret->type(), 0, ret->location());
2908 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2910 Location loc = this->location();
2911 this->advance_token();
2912 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2914 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2916 error_at(this->location(),
2917 "invalid use of %<...%> in type conversion");
2918 this->advance_token();
2920 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2921 error_at(this->location(), "expected %<)%>");
2923 this->advance_token();
2924 if (expr->is_error_expression())
2928 Type* t = ret->type();
2929 if (t->classification() == Type::TYPE_ARRAY
2930 && t->array_type()->length() != NULL
2931 && t->array_type()->length()->is_nil_expression())
2933 error_at(ret->location(),
2934 "invalid use of %<...%> in type conversion");
2935 ret = Expression::make_error(loc);
2938 ret = Expression::make_cast(t, expr, loc);
2945 const Token* token = this->peek_token();
2946 if (token->is_op(OPERATOR_LPAREN))
2947 ret = this->call(this->verify_not_sink(ret));
2948 else if (token->is_op(OPERATOR_DOT))
2950 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2951 if (is_type_switch != NULL && *is_type_switch)
2954 else if (token->is_op(OPERATOR_LSQUARE))
2955 ret = this->index(this->verify_not_sink(ret));
2963 // Selector = "." identifier .
2964 // TypeGuard = "." "(" QualifiedIdent ")" .
2966 // Note that Operand can expand to QualifiedIdent, which contains a
2967 // ".". That is handled directly in operand when it sees a package
2970 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2971 // guard (var := expr.("type") using the literal keyword "type").
2974 Parse::selector(Expression* left, bool* is_type_switch)
2976 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
2977 Location location = this->location();
2979 const Token* token = this->advance_token();
2980 if (token->is_identifier())
2982 // This could be a field in a struct, or a method in an
2983 // interface, or a method associated with a type. We can't know
2984 // which until we have seen all the types.
2986 this->gogo_->pack_hidden_name(token->identifier(),
2987 token->is_identifier_exported());
2988 if (token->identifier() == "_")
2990 error_at(this->location(), "invalid use of %<_%>");
2991 name = this->gogo_->pack_hidden_name("blank", false);
2993 this->advance_token();
2994 return Expression::make_selector(left, name, location);
2996 else if (token->is_op(OPERATOR_LPAREN))
2998 this->advance_token();
3000 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
3001 type = this->type();
3004 if (is_type_switch != NULL)
3005 *is_type_switch = true;
3008 error_at(this->location(),
3009 "use of %<.(type)%> outside type switch");
3010 type = Type::make_error_type();
3012 this->advance_token();
3014 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3015 error_at(this->location(), "missing %<)%>");
3017 this->advance_token();
3018 if (is_type_switch != NULL && *is_type_switch)
3020 return Expression::make_type_guard(left, type, location);
3024 error_at(this->location(), "expected identifier or %<(%>");
3029 // Index = "[" Expression "]" .
3030 // Slice = "[" Expression ":" [ Expression ] "]" .
3033 Parse::index(Expression* expr)
3035 Location location = this->location();
3036 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
3037 this->advance_token();
3040 if (!this->peek_token()->is_op(OPERATOR_COLON))
3041 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3045 mpz_init_set_ui(zero, 0);
3046 start = Expression::make_integer(&zero, NULL, location);
3050 Expression* end = NULL;
3051 if (this->peek_token()->is_op(OPERATOR_COLON))
3053 // We use nil to indicate a missing high expression.
3054 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3055 end = Expression::make_nil(this->location());
3057 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3059 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
3060 error_at(this->location(), "missing %<]%>");
3062 this->advance_token();
3063 return Expression::make_index(expr, start, end, location);
3066 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3067 // ArgumentList = ExpressionList [ "..." ] .
3070 Parse::call(Expression* func)
3072 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
3073 Expression_list* args = NULL;
3074 bool is_varargs = false;
3075 const Token* token = this->advance_token();
3076 if (!token->is_op(OPERATOR_RPAREN))
3078 args = this->expression_list(NULL, false, true);
3079 token = this->peek_token();
3080 if (token->is_op(OPERATOR_ELLIPSIS))
3083 token = this->advance_token();
3086 if (token->is_op(OPERATOR_COMMA))
3087 token = this->advance_token();
3088 if (!token->is_op(OPERATOR_RPAREN))
3089 error_at(this->location(), "missing %<)%>");
3091 this->advance_token();
3092 if (func->is_error_expression())
3094 return Expression::make_call(func, args, is_varargs, func->location());
3097 // Return an expression for a single unqualified identifier.
3100 Parse::id_to_expression(const std::string& name, Location location)
3102 Named_object* in_function;
3103 Named_object* named_object = this->gogo_->lookup(name, &in_function);
3104 if (named_object == NULL)
3105 named_object = this->gogo_->add_unknown_name(name, location);
3107 if (in_function != NULL
3108 && in_function != this->gogo_->current_function()
3109 && (named_object->is_variable() || named_object->is_result_variable()))
3110 return this->enclosing_var_reference(in_function, named_object,
3113 switch (named_object->classification())
3115 case Named_object::NAMED_OBJECT_CONST:
3116 return Expression::make_const_reference(named_object, location);
3117 case Named_object::NAMED_OBJECT_VAR:
3118 case Named_object::NAMED_OBJECT_RESULT_VAR:
3119 this->mark_var_used(named_object);
3120 return Expression::make_var_reference(named_object, location);
3121 case Named_object::NAMED_OBJECT_SINK:
3122 return Expression::make_sink(location);
3123 case Named_object::NAMED_OBJECT_FUNC:
3124 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3125 return Expression::make_func_reference(named_object, NULL, location);
3126 case Named_object::NAMED_OBJECT_UNKNOWN:
3128 Unknown_expression* ue =
3129 Expression::make_unknown_reference(named_object, location);
3130 if (this->is_erroneous_function_)
3131 ue->set_no_error_message();
3134 case Named_object::NAMED_OBJECT_PACKAGE:
3135 case Named_object::NAMED_OBJECT_TYPE:
3136 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3138 // These cases can arise for a field name in a composite
3140 Unknown_expression* ue =
3141 Expression::make_unknown_reference(named_object, location);
3142 if (this->is_erroneous_function_)
3143 ue->set_no_error_message();
3146 case Named_object::NAMED_OBJECT_ERRONEOUS:
3147 return Expression::make_error(location);
3149 error_at(this->location(), "unexpected type of identifier");
3150 return Expression::make_error(location);
3154 // Expression = UnaryExpr { binary_op Expression } .
3156 // PRECEDENCE is the precedence of the current operator.
3158 // If MAY_BE_SINK is true, this expression may be "_".
3160 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3163 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3164 // guard (var := expr.("type") using the literal keyword "type").
3167 Parse::expression(Precedence precedence, bool may_be_sink,
3168 bool may_be_composite_lit, bool* is_type_switch)
3170 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3175 if (is_type_switch != NULL && *is_type_switch)
3178 const Token* token = this->peek_token();
3179 if (token->classification() != Token::TOKEN_OPERATOR)
3185 Precedence right_precedence;
3186 switch (token->op())
3189 right_precedence = PRECEDENCE_OROR;
3191 case OPERATOR_ANDAND:
3192 right_precedence = PRECEDENCE_ANDAND;
3195 case OPERATOR_NOTEQ:
3200 right_precedence = PRECEDENCE_RELOP;
3203 case OPERATOR_MINUS:
3206 right_precedence = PRECEDENCE_ADDOP;
3211 case OPERATOR_LSHIFT:
3212 case OPERATOR_RSHIFT:
3214 case OPERATOR_BITCLEAR:
3215 right_precedence = PRECEDENCE_MULOP;
3218 right_precedence = PRECEDENCE_INVALID;
3222 if (right_precedence == PRECEDENCE_INVALID)
3228 Operator op = token->op();
3229 Location binop_location = token->location();
3231 if (precedence >= right_precedence)
3233 // We've already seen A * B, and we see + C. We want to
3234 // return so that A * B becomes a group.
3238 this->advance_token();
3240 left = this->verify_not_sink(left);
3241 Expression* right = this->expression(right_precedence, false,
3242 may_be_composite_lit,
3244 left = Expression::make_binary(op, left, right, binop_location);
3249 Parse::expression_may_start_here()
3251 const Token* token = this->peek_token();
3252 switch (token->classification())
3254 case Token::TOKEN_INVALID:
3255 case Token::TOKEN_EOF:
3257 case Token::TOKEN_KEYWORD:
3258 switch (token->keyword())
3263 case KEYWORD_STRUCT:
3264 case KEYWORD_INTERFACE:
3269 case Token::TOKEN_IDENTIFIER:
3271 case Token::TOKEN_STRING:
3273 case Token::TOKEN_OPERATOR:
3274 switch (token->op())
3277 case OPERATOR_MINUS:
3281 case OPERATOR_CHANOP:
3283 case OPERATOR_LPAREN:
3284 case OPERATOR_LSQUARE:
3289 case Token::TOKEN_CHARACTER:
3290 case Token::TOKEN_INTEGER:
3291 case Token::TOKEN_FLOAT:
3292 case Token::TOKEN_IMAGINARY:
3299 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3301 // If MAY_BE_SINK is true, this expression may be "_".
3303 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3306 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3307 // guard (var := expr.("type") using the literal keyword "type").
3310 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3311 bool* is_type_switch)
3313 const Token* token = this->peek_token();
3314 if (token->is_op(OPERATOR_PLUS)
3315 || token->is_op(OPERATOR_MINUS)
3316 || token->is_op(OPERATOR_NOT)
3317 || token->is_op(OPERATOR_XOR)
3318 || token->is_op(OPERATOR_CHANOP)
3319 || token->is_op(OPERATOR_MULT)
3320 || token->is_op(OPERATOR_AND))
3322 Location location = token->location();
3323 Operator op = token->op();
3324 this->advance_token();
3326 if (op == OPERATOR_CHANOP
3327 && this->peek_token()->is_keyword(KEYWORD_CHAN))
3329 // This is "<- chan" which must be the start of a type.
3330 this->unget_token(Token::make_operator_token(op, location));
3331 return Expression::make_type(this->type(), location);
3334 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
3335 if (expr->is_error_expression())
3337 else if (op == OPERATOR_MULT && expr->is_type_expression())
3338 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3340 else if (op == OPERATOR_AND && expr->is_composite_literal())
3341 expr = Expression::make_heap_composite(expr, location);
3342 else if (op != OPERATOR_CHANOP)
3343 expr = Expression::make_unary(op, expr, location);
3345 expr = Expression::make_receive(expr, location);
3349 return this->primary_expr(may_be_sink, may_be_composite_lit,
3354 // Declaration | LabeledStmt | SimpleStmt |
3355 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3356 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3359 // LABEL is the label of this statement if it has one.
3362 Parse::statement(Label* label)
3364 const Token* token = this->peek_token();
3365 switch (token->classification())
3367 case Token::TOKEN_KEYWORD:
3369 switch (token->keyword())
3374 this->declaration();
3378 case KEYWORD_STRUCT:
3379 case KEYWORD_INTERFACE:
3380 this->simple_stat(true, NULL, NULL, NULL);
3384 this->go_or_defer_stat();
3386 case KEYWORD_RETURN:
3387 this->return_stat();
3392 case KEYWORD_CONTINUE:
3393 this->continue_stat();
3401 case KEYWORD_SWITCH:
3402 this->switch_stat(label);
3404 case KEYWORD_SELECT:
3405 this->select_stat(label);
3408 this->for_stat(label);
3411 error_at(this->location(), "expected statement");
3412 this->advance_token();
3418 case Token::TOKEN_IDENTIFIER:
3420 std::string identifier = token->identifier();
3421 bool is_exported = token->is_identifier_exported();
3422 Location location = token->location();
3423 if (this->advance_token()->is_op(OPERATOR_COLON))
3425 this->advance_token();
3426 this->labeled_stmt(identifier, location);
3430 this->unget_token(Token::make_identifier_token(identifier,
3433 this->simple_stat(true, NULL, NULL, NULL);
3438 case Token::TOKEN_OPERATOR:
3439 if (token->is_op(OPERATOR_LCURLY))
3441 Location location = token->location();
3442 this->gogo_->start_block(location);
3443 Location end_loc = this->block();
3444 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3447 else if (!token->is_op(OPERATOR_SEMICOLON))
3448 this->simple_stat(true, NULL, NULL, NULL);
3451 case Token::TOKEN_STRING:
3452 case Token::TOKEN_CHARACTER:
3453 case Token::TOKEN_INTEGER:
3454 case Token::TOKEN_FLOAT:
3455 case Token::TOKEN_IMAGINARY:
3456 this->simple_stat(true, NULL, NULL, NULL);
3460 error_at(this->location(), "expected statement");
3461 this->advance_token();
3467 Parse::statement_may_start_here()
3469 const Token* token = this->peek_token();
3470 switch (token->classification())
3472 case Token::TOKEN_KEYWORD:
3474 switch (token->keyword())
3481 case KEYWORD_STRUCT:
3482 case KEYWORD_INTERFACE:
3485 case KEYWORD_RETURN:
3487 case KEYWORD_CONTINUE:
3490 case KEYWORD_SWITCH:
3491 case KEYWORD_SELECT:
3501 case Token::TOKEN_IDENTIFIER:
3504 case Token::TOKEN_OPERATOR:
3505 if (token->is_op(OPERATOR_LCURLY)
3506 || token->is_op(OPERATOR_SEMICOLON))
3509 return this->expression_may_start_here();
3511 case Token::TOKEN_STRING:
3512 case Token::TOKEN_CHARACTER:
3513 case Token::TOKEN_INTEGER:
3514 case Token::TOKEN_FLOAT:
3515 case Token::TOKEN_IMAGINARY:
3523 // LabeledStmt = Label ":" Statement .
3524 // Label = identifier .
3527 Parse::labeled_stmt(const std::string& label_name, Location location)
3529 Label* label = this->gogo_->add_label_definition(label_name, location);
3531 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3533 // This is a label at the end of a block. A program is
3534 // permitted to omit a semicolon here.
3538 if (!this->statement_may_start_here())
3540 // Mark the label as used to avoid a useless error about an
3542 label->set_is_used();
3544 error_at(location, "missing statement after label");
3545 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3550 this->statement(label);
3553 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3554 // Assignment | ShortVarDecl .
3556 // EmptyStmt was handled in Parse::statement.
3558 // In order to make this work for if and switch statements, if
3559 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3560 // expression rather than adding an expression statement to the
3561 // current block. If we see something other than an ExpressionStat,
3562 // we add the statement, set *RETURN_EXP to true if we saw a send
3563 // statement, and return NULL. The handling of send statements is for
3564 // better error messages.
3566 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3569 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3570 // guard (var := expr.("type") using the literal keyword "type").
3573 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3574 Range_clause* p_range_clause, Type_switch* p_type_switch)
3576 const Token* token = this->peek_token();
3578 // An identifier follow by := is a SimpleVarDecl.
3579 if (token->is_identifier())
3581 std::string identifier = token->identifier();
3582 bool is_exported = token->is_identifier_exported();
3583 Location location = token->location();
3585 token = this->advance_token();
3586 if (token->is_op(OPERATOR_COLONEQ)
3587 || token->is_op(OPERATOR_COMMA))
3589 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3590 this->simple_var_decl_or_assignment(identifier, location,
3591 may_be_composite_lit,
3593 (token->is_op(OPERATOR_COLONEQ)
3599 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3603 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3604 may_be_composite_lit,
3605 (p_type_switch == NULL
3607 : &p_type_switch->found));
3608 if (p_type_switch != NULL && p_type_switch->found)
3610 p_type_switch->name.clear();
3611 p_type_switch->location = exp->location();
3612 p_type_switch->expr = this->verify_not_sink(exp);
3615 token = this->peek_token();
3616 if (token->is_op(OPERATOR_CHANOP))
3618 this->send_stmt(this->verify_not_sink(exp));
3619 if (return_exp != NULL)
3622 else if (token->is_op(OPERATOR_PLUSPLUS)
3623 || token->is_op(OPERATOR_MINUSMINUS))
3624 this->inc_dec_stat(this->verify_not_sink(exp));
3625 else if (token->is_op(OPERATOR_COMMA)
3626 || token->is_op(OPERATOR_EQ))
3627 this->assignment(exp, may_be_composite_lit, p_range_clause);
3628 else if (token->is_op(OPERATOR_PLUSEQ)
3629 || token->is_op(OPERATOR_MINUSEQ)
3630 || token->is_op(OPERATOR_OREQ)
3631 || token->is_op(OPERATOR_XOREQ)
3632 || token->is_op(OPERATOR_MULTEQ)
3633 || token->is_op(OPERATOR_DIVEQ)
3634 || token->is_op(OPERATOR_MODEQ)
3635 || token->is_op(OPERATOR_LSHIFTEQ)
3636 || token->is_op(OPERATOR_RSHIFTEQ)
3637 || token->is_op(OPERATOR_ANDEQ)
3638 || token->is_op(OPERATOR_BITCLEAREQ))
3639 this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
3641 else if (return_exp != NULL)
3642 return this->verify_not_sink(exp);
3645 exp = this->verify_not_sink(exp);
3647 if (token->is_op(OPERATOR_COLONEQ))
3649 if (!exp->is_error_expression())
3650 error_at(token->location(), "non-name on left side of %<:=%>");
3651 this->gogo_->mark_locals_used();
3652 while (!token->is_op(OPERATOR_SEMICOLON)
3653 && !token->is_eof())
3654 token = this->advance_token();
3658 this->expression_stat(exp);
3665 Parse::simple_stat_may_start_here()
3667 return this->expression_may_start_here();
3670 // Parse { Statement ";" } which is used in a few places. The list of
3671 // statements may end with a right curly brace, in which case the
3672 // semicolon may be omitted.
3675 Parse::statement_list()
3677 while (this->statement_may_start_here())
3679 this->statement(NULL);
3680 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3681 this->advance_token();
3682 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3686 if (!this->peek_token()->is_eof() || !saw_errors())
3687 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3688 if (!this->skip_past_error(OPERATOR_RCURLY))
3695 Parse::statement_list_may_start_here()
3697 return this->statement_may_start_here();
3700 // ExpressionStat = Expression .
3703 Parse::expression_stat(Expression* exp)
3705 this->gogo_->add_statement(Statement::make_statement(exp, false));
3708 // SendStmt = Channel "<-" Expression .
3709 // Channel = Expression .
3712 Parse::send_stmt(Expression* channel)
3714 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3715 Location loc = this->location();
3716 this->advance_token();
3717 Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3718 Statement* s = Statement::make_send_statement(channel, val, loc);
3719 this->gogo_->add_statement(s);
3722 // IncDecStat = Expression ( "++" | "--" ) .
3725 Parse::inc_dec_stat(Expression* exp)
3727 const Token* token = this->peek_token();
3729 // Lvalue maps require special handling.
3730 if (exp->index_expression() != NULL)
3731 exp->index_expression()->set_is_lvalue();
3733 if (token->is_op(OPERATOR_PLUSPLUS))
3734 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3735 else if (token->is_op(OPERATOR_MINUSMINUS))
3736 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3739 this->advance_token();
3742 // Assignment = ExpressionList assign_op ExpressionList .
3744 // EXP is an expression that we have already parsed.
3746 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3747 // side may be a composite literal.
3749 // If RANGE_CLAUSE is not NULL, then this will recognize a
3753 Parse::assignment(Expression* expr, bool may_be_composite_lit,
3754 Range_clause* p_range_clause)
3756 Expression_list* vars;
3757 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3759 vars = new Expression_list();
3760 vars->push_back(expr);
3764 this->advance_token();
3765 vars = this->expression_list(expr, true, may_be_composite_lit);
3768 this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
3771 // An assignment statement. LHS is the list of expressions which
3772 // appear on the left hand side.
3774 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3775 // side may be a composite literal.
3777 // If RANGE_CLAUSE is not NULL, then this will recognize a
3781 Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
3782 Range_clause* p_range_clause)
3784 const Token* token = this->peek_token();
3785 if (!token->is_op(OPERATOR_EQ)
3786 && !token->is_op(OPERATOR_PLUSEQ)
3787 && !token->is_op(OPERATOR_MINUSEQ)
3788 && !token->is_op(OPERATOR_OREQ)
3789 && !token->is_op(OPERATOR_XOREQ)
3790 && !token->is_op(OPERATOR_MULTEQ)
3791 && !token->is_op(OPERATOR_DIVEQ)
3792 && !token->is_op(OPERATOR_MODEQ)
3793 && !token->is_op(OPERATOR_LSHIFTEQ)
3794 && !token->is_op(OPERATOR_RSHIFTEQ)
3795 && !token->is_op(OPERATOR_ANDEQ)
3796 && !token->is_op(OPERATOR_BITCLEAREQ))
3798 error_at(this->location(), "expected assignment operator");
3801 Operator op = token->op();
3802 Location location = token->location();
3804 token = this->advance_token();
3806 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
3808 if (op != OPERATOR_EQ)
3809 error_at(this->location(), "range clause requires %<=%>");
3810 this->range_clause_expr(lhs, p_range_clause);
3814 Expression_list* vals = this->expression_list(NULL, false,
3815 may_be_composite_lit);
3817 // We've parsed everything; check for errors.
3818 if (lhs == NULL || vals == NULL)
3820 for (Expression_list::const_iterator pe = lhs->begin();
3824 if ((*pe)->is_error_expression())
3826 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
3827 error_at((*pe)->location(), "cannot use _ as value");
3829 for (Expression_list::const_iterator pe = vals->begin();
3833 if ((*pe)->is_error_expression())
3837 // Map expressions act differently when they are lvalues.
3838 for (Expression_list::iterator plv = lhs->begin();
3841 if ((*plv)->index_expression() != NULL)
3842 (*plv)->index_expression()->set_is_lvalue();
3844 Call_expression* call;
3845 Index_expression* map_index;
3846 Receive_expression* receive;
3847 Type_guard_expression* type_guard;
3848 if (lhs->size() == vals->size())
3851 if (lhs->size() > 1)
3853 if (op != OPERATOR_EQ)
3854 error_at(location, "multiple values only permitted with %<=%>");
3855 s = Statement::make_tuple_assignment(lhs, vals, location);
3859 if (op == OPERATOR_EQ)
3860 s = Statement::make_assignment(lhs->front(), vals->front(),
3863 s = Statement::make_assignment_operation(op, lhs->front(),
3864 vals->front(), location);
3868 this->gogo_->add_statement(s);
3870 else if (vals->size() == 1
3871 && (call = (*vals->begin())->call_expression()) != NULL)
3873 if (op != OPERATOR_EQ)
3874 error_at(location, "multiple results only permitted with %<=%>");
3876 vals = new Expression_list;
3877 for (unsigned int i = 0; i < lhs->size(); ++i)
3878 vals->push_back(Expression::make_call_result(call, i));
3879 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
3880 this->gogo_->add_statement(s);
3882 else if (lhs->size() == 2
3883 && vals->size() == 1
3884 && (map_index = (*vals->begin())->index_expression()) != NULL)
3886 if (op != OPERATOR_EQ)
3887 error_at(location, "two values from map requires %<=%>");
3888 Expression* val = lhs->front();
3889 Expression* present = lhs->back();
3890 Statement* s = Statement::make_tuple_map_assignment(val, present,
3891 map_index, location);
3892 this->gogo_->add_statement(s);
3894 else if (lhs->size() == 1
3895 && vals->size() == 2
3896 && (map_index = lhs->front()->index_expression()) != NULL)
3898 if (op != OPERATOR_EQ)
3899 error_at(location, "assigning tuple to map index requires %<=%>");
3900 Expression* val = vals->front();
3901 Expression* should_set = vals->back();
3902 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
3904 this->gogo_->add_statement(s);
3906 else if (lhs->size() == 2
3907 && vals->size() == 1
3908 && (receive = (*vals->begin())->receive_expression()) != NULL)
3910 if (op != OPERATOR_EQ)
3911 error_at(location, "two values from receive requires %<=%>");
3912 Expression* val = lhs->front();
3913 Expression* success = lhs->back();
3914 Expression* channel = receive->channel();
3915 Statement* s = Statement::make_tuple_receive_assignment(val, success,
3918 this->gogo_->add_statement(s);
3920 else if (lhs->size() == 2
3921 && vals->size() == 1
3922 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
3924 if (op != OPERATOR_EQ)
3925 error_at(location, "two values from type guard requires %<=%>");
3926 Expression* val = lhs->front();
3927 Expression* ok = lhs->back();
3928 Expression* expr = type_guard->expr();
3929 Type* type = type_guard->type();
3930 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
3933 this->gogo_->add_statement(s);
3937 error_at(location, "number of variables does not match number of values");
3941 // GoStat = "go" Expression .
3942 // DeferStat = "defer" Expression .
3945 Parse::go_or_defer_stat()
3947 go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
3948 || this->peek_token()->is_keyword(KEYWORD_DEFER));
3949 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
3950 Location stat_location = this->location();
3951 this->advance_token();
3952 Location expr_location = this->location();
3953 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3954 Call_expression* call_expr = expr->call_expression();
3955 if (call_expr == NULL)
3957 error_at(expr_location, "expected call expression");
3961 // Make it easier to simplify go/defer statements by putting every
3962 // statement in its own block.
3963 this->gogo_->start_block(stat_location);
3966 stat = Statement::make_go_statement(call_expr, stat_location);
3968 stat = Statement::make_defer_statement(call_expr, stat_location);
3969 this->gogo_->add_statement(stat);
3970 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
3974 // ReturnStat = "return" [ ExpressionList ] .
3977 Parse::return_stat()
3979 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
3980 Location location = this->location();
3981 this->advance_token();
3982 Expression_list* vals = NULL;
3983 if (this->expression_may_start_here())
3984 vals = this->expression_list(NULL, false, true);
3985 this->gogo_->add_statement(Statement::make_return_statement(vals, location));
3988 && this->gogo_->current_function()->func_value()->results_are_named())
3990 Named_object* function = this->gogo_->current_function();
3991 Function::Results* results = function->func_value()->result_variables();
3992 for (Function::Results::const_iterator p = results->begin();
3993 p != results->end();
3996 Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
3998 go_assert(saw_errors());
3999 else if (!no->is_result_variable())
4000 error_at(location, "%qs is shadowed during return",
4001 (*p)->message_name().c_str());
4006 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4007 // [ "else" ( IfStmt | Block ) ] .
4012 go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
4013 Location location = this->location();
4014 this->advance_token();
4016 this->gogo_->start_block(location);
4018 bool saw_simple_stat = false;
4019 Expression* cond = NULL;
4020 bool saw_send_stmt = false;
4021 if (this->simple_stat_may_start_here())
4023 cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
4024 saw_simple_stat = true;
4026 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4028 // The SimpleStat is an expression statement.
4029 this->expression_stat(cond);
4034 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4035 this->advance_token();
4036 else if (saw_simple_stat)
4039 error_at(this->location(),
4040 ("send statement used as value; "
4041 "use select for non-blocking send"));
4043 error_at(this->location(),
4044 "expected %<;%> after statement in if expression");
4045 if (!this->expression_may_start_here())
4046 cond = Expression::make_error(this->location());
4048 if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
4050 error_at(this->location(),
4051 "missing condition in if statement");
4052 cond = Expression::make_error(this->location());
4055 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
4058 this->gogo_->start_block(this->location());
4059 Location end_loc = this->block();
4060 Block* then_block = this->gogo_->finish_block(end_loc);
4062 // Check for the easy error of a newline before "else".
4063 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4065 Location semi_loc = this->location();
4066 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
4067 error_at(this->location(),
4068 "unexpected semicolon or newline before %<else%>");
4070 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
4074 Block* else_block = NULL;
4075 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
4077 this->gogo_->start_block(this->location());
4078 const Token* token = this->advance_token();
4079 if (token->is_keyword(KEYWORD_IF))
4081 else if (token->is_op(OPERATOR_LCURLY))
4085 error_at(this->location(), "expected %<if%> or %<{%>");
4086 this->statement(NULL);
4088 else_block = this->gogo_->finish_block(this->location());
4091 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
4095 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4099 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4100 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4101 // "{" { ExprCaseClause } "}" .
4102 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4103 // "{" { TypeCaseClause } "}" .
4104 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4107 Parse::switch_stat(Label* label)
4109 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
4110 Location location = this->location();
4111 this->advance_token();
4113 this->gogo_->start_block(location);
4115 bool saw_simple_stat = false;
4116 Expression* switch_val = NULL;
4118 Type_switch type_switch;
4119 bool have_type_switch_block = false;
4120 if (this->simple_stat_may_start_here())
4122 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4124 saw_simple_stat = true;
4126 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4128 // The SimpleStat is an expression statement.
4129 this->expression_stat(switch_val);
4132 if (switch_val == NULL && !type_switch.found)
4134 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4135 this->advance_token();
4136 else if (saw_simple_stat)
4139 error_at(this->location(),
4140 ("send statement used as value; "
4141 "use select for non-blocking send"));
4143 error_at(this->location(),
4144 "expected %<;%> after statement in switch expression");
4146 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4148 if (this->peek_token()->is_identifier())
4150 const Token* token = this->peek_token();
4151 std::string identifier = token->identifier();
4152 bool is_exported = token->is_identifier_exported();
4153 Location id_loc = token->location();
4155 token = this->advance_token();
4156 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
4157 this->unget_token(Token::make_identifier_token(identifier,
4162 // This must be a TypeSwitchGuard. It is in a
4163 // different block from any initial SimpleStat.
4164 if (saw_simple_stat)
4166 this->gogo_->start_block(id_loc);
4167 have_type_switch_block = true;
4170 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4172 if (!type_switch.found)
4174 if (switch_val == NULL
4175 || !switch_val->is_error_expression())
4177 error_at(id_loc, "expected type switch assignment");
4178 switch_val = Expression::make_error(id_loc);
4183 if (switch_val == NULL && !type_switch.found)
4185 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
4186 &type_switch.found);
4187 if (type_switch.found)
4189 type_switch.name.clear();
4190 type_switch.expr = switch_val;
4191 type_switch.location = switch_val->location();
4197 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4199 Location token_loc = this->location();
4200 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
4201 && this->advance_token()->is_op(OPERATOR_LCURLY))
4202 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4203 else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
4205 error_at(token_loc, "invalid variable name");
4206 this->advance_token();
4207 this->expression(PRECEDENCE_NORMAL, false, false,
4208 &type_switch.found);
4209 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4210 this->advance_token();
4211 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4213 if (have_type_switch_block)
4214 this->gogo_->add_block(this->gogo_->finish_block(location),
4216 this->gogo_->add_block(this->gogo_->finish_block(location),
4220 if (type_switch.found)
4221 type_switch.expr = Expression::make_error(location);
4225 error_at(this->location(), "expected %<{%>");
4226 if (have_type_switch_block)
4227 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4229 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4234 this->advance_token();
4236 Statement* statement;
4237 if (type_switch.found)
4238 statement = this->type_switch_body(label, type_switch, location);
4240 statement = this->expr_switch_body(label, switch_val, location);
4242 if (statement != NULL)
4243 this->gogo_->add_statement(statement);
4245 if (have_type_switch_block)
4246 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4249 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4253 // The body of an expression switch.
4254 // "{" { ExprCaseClause } "}"
4257 Parse::expr_switch_body(Label* label, Expression* switch_val,
4260 Switch_statement* statement = Statement::make_switch_statement(switch_val,
4263 this->push_break_statement(statement, label);
4265 Case_clauses* case_clauses = new Case_clauses();
4266 bool saw_default = false;
4267 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4269 if (this->peek_token()->is_eof())
4272 error_at(this->location(), "missing %<}%>");
4275 this->expr_case_clause(case_clauses, &saw_default);
4277 this->advance_token();
4279 statement->add_clauses(case_clauses);
4281 this->pop_break_statement();
4286 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4287 // FallthroughStat = "fallthrough" .
4290 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
4292 Location location = this->location();
4294 bool is_default = false;
4295 Expression_list* vals = this->expr_switch_case(&is_default);
4297 if (!this->peek_token()->is_op(OPERATOR_COLON))
4300 error_at(this->location(), "expected %<:%>");
4304 this->advance_token();
4306 Block* statements = NULL;
4307 if (this->statement_list_may_start_here())
4309 this->gogo_->start_block(this->location());
4310 this->statement_list();
4311 statements = this->gogo_->finish_block(this->location());
4314 bool is_fallthrough = false;
4315 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4317 is_fallthrough = true;
4318 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4319 this->advance_token();
4326 error_at(location, "multiple defaults in switch");
4329 *saw_default = true;
4332 if (is_default || vals != NULL)
4333 clauses->add(vals, is_default, statements, is_fallthrough, location);
4336 // ExprSwitchCase = "case" ExpressionList | "default" .
4339 Parse::expr_switch_case(bool* is_default)
4341 const Token* token = this->peek_token();
4342 if (token->is_keyword(KEYWORD_CASE))
4344 this->advance_token();
4345 return this->expression_list(NULL, false, true);
4347 else if (token->is_keyword(KEYWORD_DEFAULT))
4349 this->advance_token();
4356 error_at(this->location(), "expected %<case%> or %<default%>");
4357 if (!token->is_op(OPERATOR_RCURLY))
4358 this->advance_token();
4363 // The body of a type switch.
4364 // "{" { TypeCaseClause } "}" .
4367 Parse::type_switch_body(Label* label, const Type_switch& type_switch,
4370 Named_object* switch_no = NULL;
4371 if (!type_switch.name.empty())
4373 if (Gogo::is_sink_name(type_switch.name))
4374 error_at(type_switch.location,
4375 "no new variables on left side of %<:=%>");
4378 Variable* switch_var = new Variable(NULL, type_switch.expr, false,
4380 type_switch.location);
4381 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
4385 Type_switch_statement* statement =
4386 Statement::make_type_switch_statement(switch_no,
4392 this->push_break_statement(statement, label);
4394 Type_case_clauses* case_clauses = new Type_case_clauses();
4395 bool saw_default = false;
4396 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4398 if (this->peek_token()->is_eof())
4400 error_at(this->location(), "missing %<}%>");
4403 this->type_case_clause(switch_no, case_clauses, &saw_default);
4405 this->advance_token();
4407 statement->add_clauses(case_clauses);
4409 this->pop_break_statement();
4414 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4417 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4420 Location location = this->location();
4422 std::vector<Type*> types;
4423 bool is_default = false;
4424 this->type_switch_case(&types, &is_default);
4426 if (!this->peek_token()->is_op(OPERATOR_COLON))
4427 error_at(this->location(), "expected %<:%>");
4429 this->advance_token();
4431 Block* statements = NULL;
4432 if (this->statement_list_may_start_here())
4434 this->gogo_->start_block(this->location());
4435 if (switch_no != NULL && types.size() == 1)
4437 Type* type = types.front();
4438 Expression* init = Expression::make_var_reference(switch_no,
4440 init = Expression::make_type_guard(init, type, location);
4441 Variable* v = new Variable(type, init, false, false, false,
4443 v->set_is_type_switch_var();
4444 Named_object* no = this->gogo_->add_variable(switch_no->name(), v);
4446 // We don't want to issue an error if the compiler
4447 // introduced special variable is not used. Instead we want
4448 // to issue an error if the variable defined by the switch
4449 // is not used. That is handled via type_switch_vars_ and
4450 // Parse::mark_var_used.
4452 this->type_switch_vars_[no] = switch_no;
4454 this->statement_list();
4455 statements = this->gogo_->finish_block(this->location());
4458 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4460 error_at(this->location(),
4461 "fallthrough is not permitted in a type switch");
4462 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4463 this->advance_token();
4468 go_assert(types.empty());
4471 error_at(location, "multiple defaults in type switch");
4474 *saw_default = true;
4475 clauses->add(NULL, false, true, statements, location);
4477 else if (!types.empty())
4479 for (std::vector<Type*>::const_iterator p = types.begin();
4480 p + 1 != types.end();
4482 clauses->add(*p, true, false, NULL, location);
4483 clauses->add(types.back(), false, false, statements, location);
4486 clauses->add(Type::make_error_type(), false, false, statements, location);
4489 // TypeSwitchCase = "case" type | "default"
4491 // We accept a comma separated list of types.
4494 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4496 const Token* token = this->peek_token();
4497 if (token->is_keyword(KEYWORD_CASE))
4499 this->advance_token();
4502 Type* t = this->type();
4504 if (!t->is_error_type())
4505 types->push_back(t);
4508 this->gogo_->mark_locals_used();
4509 token = this->peek_token();
4510 while (!token->is_op(OPERATOR_COLON)
4511 && !token->is_op(OPERATOR_COMMA)
4512 && !token->is_op(OPERATOR_RCURLY)
4513 && !token->is_eof())
4514 token = this->advance_token();
4517 if (!this->peek_token()->is_op(OPERATOR_COMMA))
4519 this->advance_token();
4522 else if (token->is_keyword(KEYWORD_DEFAULT))
4524 this->advance_token();
4529 error_at(this->location(), "expected %<case%> or %<default%>");
4530 if (!token->is_op(OPERATOR_RCURLY))
4531 this->advance_token();
4535 // SelectStat = "select" "{" { CommClause } "}" .
4538 Parse::select_stat(Label* label)
4540 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4541 Location location = this->location();
4542 const Token* token = this->advance_token();
4544 if (!token->is_op(OPERATOR_LCURLY))
4546 Location token_loc = token->location();
4547 if (token->is_op(OPERATOR_SEMICOLON)
4548 && this->advance_token()->is_op(OPERATOR_LCURLY))
4549 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4552 error_at(this->location(), "expected %<{%>");
4556 this->advance_token();
4558 Select_statement* statement = Statement::make_select_statement(location);
4560 this->push_break_statement(statement, label);
4562 Select_clauses* select_clauses = new Select_clauses();
4563 bool saw_default = false;
4564 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4566 if (this->peek_token()->is_eof())
4568 error_at(this->location(), "expected %<}%>");
4571 this->comm_clause(select_clauses, &saw_default);
4574 this->advance_token();
4576 statement->add_clauses(select_clauses);
4578 this->pop_break_statement();
4580 this->gogo_->add_statement(statement);
4583 // CommClause = CommCase ":" { Statement ";" } .
4586 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4588 Location location = this->location();
4589 bool is_send = false;
4590 Expression* channel = NULL;
4591 Expression* val = NULL;
4592 Expression* closed = NULL;
4593 std::string varname;
4594 std::string closedname;
4595 bool is_default = false;
4596 bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
4597 &varname, &closedname, &is_default);
4601 && closedname.empty()
4603 && val->index_expression() != NULL)
4604 val->index_expression()->set_is_lvalue();
4606 if (this->peek_token()->is_op(OPERATOR_COLON))
4607 this->advance_token();
4609 error_at(this->location(), "expected colon");
4611 this->gogo_->start_block(this->location());
4613 Named_object* var = NULL;
4614 if (!varname.empty())
4616 // FIXME: LOCATION is slightly wrong here.
4617 Variable* v = new Variable(NULL, channel, false, false, false,
4619 v->set_type_from_chan_element();
4620 var = this->gogo_->add_variable(varname, v);
4623 Named_object* closedvar = NULL;
4624 if (!closedname.empty())
4626 // FIXME: LOCATION is slightly wrong here.
4627 Variable* v = new Variable(Type::lookup_bool_type(), NULL,
4628 false, false, false, location);
4629 closedvar = this->gogo_->add_variable(closedname, v);
4632 this->statement_list();
4634 Block* statements = this->gogo_->finish_block(this->location());
4640 error_at(location, "multiple defaults in select");
4643 *saw_default = true;
4647 clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
4648 statements, location);
4649 else if (statements != NULL)
4651 // Add the statements to make sure that any names they define
4653 this->gogo_->add_block(statements, location);
4657 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4660 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4661 Expression** closed, std::string* varname,
4662 std::string* closedname, bool* is_default)
4664 const Token* token = this->peek_token();
4665 if (token->is_keyword(KEYWORD_DEFAULT))
4667 this->advance_token();
4670 else if (token->is_keyword(KEYWORD_CASE))
4672 this->advance_token();
4673 if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
4679 error_at(this->location(), "expected %<case%> or %<default%>");
4680 if (!token->is_op(OPERATOR_RCURLY))
4681 this->advance_token();
4688 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4689 // RecvExpr = Expression .
4692 Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
4693 Expression** closed, std::string* varname,
4694 std::string* closedname)
4696 const Token* token = this->peek_token();
4697 bool saw_comma = false;
4698 bool closed_is_id = false;
4699 if (token->is_identifier())
4701 Gogo* gogo = this->gogo_;
4702 std::string recv_var = token->identifier();
4703 bool is_rv_exported = token->is_identifier_exported();
4704 Location recv_var_loc = token->location();
4705 token = this->advance_token();
4706 if (token->is_op(OPERATOR_COLONEQ))
4709 this->advance_token();
4710 Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
4712 Receive_expression* re = e->receive_expression();
4715 if (!e->is_error_expression())
4716 error_at(this->location(), "expected receive expression");
4719 if (recv_var == "_")
4721 error_at(recv_var_loc,
4722 "no new variables on left side of %<:=%>");
4726 *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
4727 *channel = re->channel();
4730 else if (token->is_op(OPERATOR_COMMA))
4732 token = this->advance_token();
4733 if (token->is_identifier())
4735 std::string recv_closed = token->identifier();
4736 bool is_rc_exported = token->is_identifier_exported();
4737 Location recv_closed_loc = token->location();
4738 closed_is_id = true;
4740 token = this->advance_token();
4741 if (token->is_op(OPERATOR_COLONEQ))
4743 // case rv, rc := <-c:
4744 this->advance_token();
4745 Expression* e = this->expression(PRECEDENCE_NORMAL, false,
4747 Receive_expression* re = e->receive_expression();
4750 if (!e->is_error_expression())
4751 error_at(this->location(),
4752 "expected receive expression");
4755 if (recv_var == "_" && recv_closed == "_")
4757 error_at(recv_var_loc,
4758 "no new variables on left side of %<:=%>");
4762 if (recv_var != "_")
4763 *varname = gogo->pack_hidden_name(recv_var,
4765 if (recv_closed != "_")
4766 *closedname = gogo->pack_hidden_name(recv_closed,
4768 *channel = re->channel();
4772 this->unget_token(Token::make_identifier_token(recv_closed,
4777 *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
4783 this->unget_token(Token::make_identifier_token(recv_var,
4788 // If SAW_COMMA is false, then we are looking at the start of the
4789 // send or receive expression. If SAW_COMMA is true, then *VAL is
4790 // set and we just read a comma.
4793 if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
4794 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL);
4799 this->advance_token();
4800 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4802 // The next token should be ':'. If it is '<-', then we have
4804 // which is to say, send on a channel received from a channel.
4805 if (!this->peek_token()->is_op(OPERATOR_CHANOP))
4808 e = Expression::make_receive(*channel, (*channel)->location());
4811 if (this->peek_token()->is_op(OPERATOR_EQ))
4813 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4815 error_at(this->location(), "missing %<<-%>");
4819 this->advance_token();
4820 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4824 // *VAL is already set.
4825 if (!e->is_sink_expression())
4831 if (!e->is_sink_expression())
4840 error_at(this->location(), "expected %<=%> or %<:=%>");
4842 error_at(this->location(), "expected %<=%>");
4846 if (this->peek_token()->is_op(OPERATOR_CHANOP))
4850 *channel = this->verify_not_sink(e);
4851 this->advance_token();
4852 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4856 error_at(this->location(), "expected %<<-%> or %<=%>");
4860 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
4861 // Condition = Expression .
4864 Parse::for_stat(Label* label)
4866 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
4867 Location location = this->location();
4868 const Token* token = this->advance_token();
4870 // Open a block to hold any variables defined in the init statement
4871 // of the for statement.
4872 this->gogo_->start_block(location);
4875 Expression* cond = NULL;
4877 Range_clause range_clause;
4879 if (!token->is_op(OPERATOR_LCURLY))
4881 if (token->is_keyword(KEYWORD_VAR))
4883 error_at(this->location(),
4884 "var declaration not allowed in for initializer");
4888 if (token->is_op(OPERATOR_SEMICOLON))
4889 this->for_clause(&cond, &post);
4892 // We might be looking at a Condition, an InitStat, or a
4895 cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
4896 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4898 if (cond == NULL && !range_clause.found)
4901 error_at(this->location(),
4902 ("send statement used as value; "
4903 "use select for non-blocking send"));
4905 error_at(this->location(), "parse error in for statement");
4910 if (range_clause.found)
4911 error_at(this->location(), "parse error after range clause");
4915 // COND is actually an expression statement for
4916 // InitStat at the start of a ForClause.
4917 this->expression_stat(cond);
4921 this->for_clause(&cond, &post);
4926 // Build the For_statement and note that it is the current target
4927 // for break and continue statements.
4929 For_statement* sfor;
4930 For_range_statement* srange;
4932 if (!range_clause.found)
4934 sfor = Statement::make_for_statement(init, cond, post, location);
4940 srange = Statement::make_for_range_statement(range_clause.index,
4948 this->push_break_statement(s, label);
4949 this->push_continue_statement(s, label);
4951 // Gather the block of statements in the loop and add them to the
4954 this->gogo_->start_block(this->location());
4955 Location end_loc = this->block();
4956 Block* statements = this->gogo_->finish_block(end_loc);
4959 sfor->add_statements(statements);
4961 srange->add_statements(statements);
4963 // This is no longer the break/continue target.
4964 this->pop_break_statement();
4965 this->pop_continue_statement();
4967 // Add the For_statement to the list of statements, and close out
4968 // the block we started to hold any variables defined in the for
4971 this->gogo_->add_statement(s);
4973 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4977 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
4978 // InitStat = SimpleStat .
4979 // PostStat = SimpleStat .
4981 // We have already read InitStat at this point.
4984 Parse::for_clause(Expression** cond, Block** post)
4986 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
4987 this->advance_token();
4988 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4990 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
4992 error_at(this->location(),
4993 "unexpected semicolon or newline before %<{%>");
4999 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
5000 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5001 error_at(this->location(), "expected semicolon");
5003 this->advance_token();
5005 if (this->peek_token()->is_op(OPERATOR_LCURLY))
5009 this->gogo_->start_block(this->location());
5010 this->simple_stat(false, NULL, NULL, NULL);
5011 *post = this->gogo_->finish_block(this->location());
5015 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
5017 // This is the := version. It is called with a list of identifiers.
5020 Parse::range_clause_decl(const Typed_identifier_list* til,
5021 Range_clause* p_range_clause)
5023 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5024 Location location = this->location();
5026 p_range_clause->found = true;
5028 go_assert(til->size() >= 1);
5029 if (til->size() > 2)
5030 error_at(this->location(), "too many variables for range clause");
5032 this->advance_token();
5033 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
5034 p_range_clause->range = expr;
5036 bool any_new = false;
5038 const Typed_identifier* pti = &til->front();
5039 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new);
5040 if (any_new && no->is_variable())
5041 no->var_value()->set_type_from_range_index();
5042 p_range_clause->index = Expression::make_var_reference(no, location);
5044 if (til->size() == 1)
5045 p_range_clause->value = NULL;
5049 bool is_new = false;
5050 no = this->init_var(*pti, NULL, expr, true, true, &is_new);
5051 if (is_new && no->is_variable())
5052 no->var_value()->set_type_from_range_value();
5055 p_range_clause->value = Expression::make_var_reference(no, location);
5059 error_at(location, "variables redeclared but no variable is new");
5062 // The = version of RangeClause. This is called with a list of
5066 Parse::range_clause_expr(const Expression_list* vals,
5067 Range_clause* p_range_clause)
5069 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5071 p_range_clause->found = true;
5073 go_assert(vals->size() >= 1);
5074 if (vals->size() > 2)
5075 error_at(this->location(), "too many variables for range clause");
5077 this->advance_token();
5078 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
5081 p_range_clause->index = vals->front();
5082 if (vals->size() == 1)
5083 p_range_clause->value = NULL;
5085 p_range_clause->value = vals->back();
5088 // Push a statement on the break stack.
5091 Parse::push_break_statement(Statement* enclosing, Label* label)
5093 if (this->break_stack_ == NULL)
5094 this->break_stack_ = new Bc_stack();
5095 this->break_stack_->push_back(std::make_pair(enclosing, label));
5098 // Push a statement on the continue stack.
5101 Parse::push_continue_statement(Statement* enclosing, Label* label)
5103 if (this->continue_stack_ == NULL)
5104 this->continue_stack_ = new Bc_stack();
5105 this->continue_stack_->push_back(std::make_pair(enclosing, label));
5108 // Pop the break stack.
5111 Parse::pop_break_statement()
5113 this->break_stack_->pop_back();
5116 // Pop the continue stack.
5119 Parse::pop_continue_statement()
5121 this->continue_stack_->pop_back();
5124 // Find a break or continue statement given a label name.
5127 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
5129 if (bc_stack == NULL)
5131 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
5132 p != bc_stack->rend();
5135 if (p->second != NULL && p->second->name() == label)
5137 p->second->set_is_used();
5144 // BreakStat = "break" [ identifier ] .
5149 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
5150 Location location = this->location();
5152 const Token* token = this->advance_token();
5153 Statement* enclosing;
5154 if (!token->is_identifier())
5156 if (this->break_stack_ == NULL || this->break_stack_->empty())
5158 error_at(this->location(),
5159 "break statement not within for or switch or select");
5162 enclosing = this->break_stack_->back().first;
5166 enclosing = this->find_bc_statement(this->break_stack_,
5167 token->identifier());
5168 if (enclosing == NULL)
5170 // If there is a label with this name, mark it as used to
5171 // avoid a useless error about an unused label.
5172 this->gogo_->add_label_reference(token->identifier(),
5173 Linemap::unknown_location(), false);
5175 error_at(token->location(), "invalid break label %qs",
5176 Gogo::message_name(token->identifier()).c_str());
5177 this->advance_token();
5180 this->advance_token();
5183 Unnamed_label* label;
5184 if (enclosing->classification() == Statement::STATEMENT_FOR)
5185 label = enclosing->for_statement()->break_label();
5186 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5187 label = enclosing->for_range_statement()->break_label();
5188 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
5189 label = enclosing->switch_statement()->break_label();
5190 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
5191 label = enclosing->type_switch_statement()->break_label();
5192 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
5193 label = enclosing->select_statement()->break_label();
5197 this->gogo_->add_statement(Statement::make_break_statement(label,
5201 // ContinueStat = "continue" [ identifier ] .
5204 Parse::continue_stat()
5206 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
5207 Location location = this->location();
5209 const Token* token = this->advance_token();
5210 Statement* enclosing;
5211 if (!token->is_identifier())
5213 if (this->continue_stack_ == NULL || this->continue_stack_->empty())
5215 error_at(this->location(), "continue statement not within for");
5218 enclosing = this->continue_stack_->back().first;
5222 enclosing = this->find_bc_statement(this->continue_stack_,
5223 token->identifier());
5224 if (enclosing == NULL)
5226 // If there is a label with this name, mark it as used to
5227 // avoid a useless error about an unused label.
5228 this->gogo_->add_label_reference(token->identifier(),
5229 Linemap::unknown_location(), false);
5231 error_at(token->location(), "invalid continue label %qs",
5232 Gogo::message_name(token->identifier()).c_str());
5233 this->advance_token();
5236 this->advance_token();
5239 Unnamed_label* label;
5240 if (enclosing->classification() == Statement::STATEMENT_FOR)
5241 label = enclosing->for_statement()->continue_label();
5242 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5243 label = enclosing->for_range_statement()->continue_label();
5247 this->gogo_->add_statement(Statement::make_continue_statement(label,
5251 // GotoStat = "goto" identifier .
5256 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
5257 Location location = this->location();
5258 const Token* token = this->advance_token();
5259 if (!token->is_identifier())
5260 error_at(this->location(), "expected label for goto");
5263 Label* label = this->gogo_->add_label_reference(token->identifier(),
5265 Statement* s = Statement::make_goto_statement(label, location);
5266 this->gogo_->add_statement(s);
5267 this->advance_token();
5271 // PackageClause = "package" PackageName .
5274 Parse::package_clause()
5276 const Token* token = this->peek_token();
5277 Location location = token->location();
5279 if (!token->is_keyword(KEYWORD_PACKAGE))
5281 error_at(this->location(), "program must start with package clause");
5286 token = this->advance_token();
5287 if (token->is_identifier())
5289 name = token->identifier();
5292 error_at(this->location(), "invalid package name _");
5295 this->advance_token();
5299 error_at(this->location(), "package name must be an identifier");
5303 this->gogo_->set_package_name(name, location);
5306 // ImportDecl = "import" Decl<ImportSpec> .
5309 Parse::import_decl()
5311 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
5312 this->advance_token();
5313 this->decl(&Parse::import_spec, NULL);
5316 // ImportSpec = [ "." | PackageName ] PackageFileName .
5319 Parse::import_spec(void*)
5321 const Token* token = this->peek_token();
5322 Location location = token->location();
5324 std::string local_name;
5325 bool is_local_name_exported = false;
5326 if (token->is_op(OPERATOR_DOT))
5329 token = this->advance_token();
5331 else if (token->is_identifier())
5333 local_name = token->identifier();
5334 is_local_name_exported = token->is_identifier_exported();
5335 token = this->advance_token();
5338 if (!token->is_string())
5340 error_at(this->location(), "missing import package name");
5344 this->gogo_->import_package(token->string_value(), local_name,
5345 is_local_name_exported, location);
5347 this->advance_token();
5350 // SourceFile = PackageClause ";" { ImportDecl ";" }
5351 // { TopLevelDecl ";" } .
5356 this->package_clause();
5358 const Token* token = this->peek_token();
5359 if (token->is_op(OPERATOR_SEMICOLON))
5360 token = this->advance_token();
5362 error_at(this->location(),
5363 "expected %<;%> or newline after package clause");
5365 while (token->is_keyword(KEYWORD_IMPORT))
5367 this->import_decl();
5368 token = this->peek_token();
5369 if (token->is_op(OPERATOR_SEMICOLON))
5370 token = this->advance_token();
5372 error_at(this->location(),
5373 "expected %<;%> or newline after import declaration");
5376 while (!token->is_eof())
5378 if (this->declaration_may_start_here())
5379 this->declaration();
5382 error_at(this->location(), "expected declaration");
5383 this->gogo_->mark_locals_used();
5385 this->advance_token();
5386 while (!this->peek_token()->is_eof()
5387 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
5388 && !this->peek_token()->is_op(OPERATOR_RCURLY));
5389 if (!this->peek_token()->is_eof()
5390 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
5391 this->advance_token();
5393 token = this->peek_token();
5394 if (token->is_op(OPERATOR_SEMICOLON))
5395 token = this->advance_token();
5396 else if (!token->is_eof() || !saw_errors())
5398 if (token->is_op(OPERATOR_CHANOP))
5399 error_at(this->location(),
5400 ("send statement used as value; "
5401 "use select for non-blocking send"));
5403 error_at(this->location(),
5404 "expected %<;%> or newline after top level declaration");
5405 this->skip_past_error(OPERATOR_INVALID);
5410 // Reset the current iota value.
5418 // Return the current iota value.
5426 // Increment the current iota value.
5429 Parse::increment_iota()
5434 // Skip forward to a semicolon or OP. OP will normally be
5435 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5436 // past it and return. If we find OP, it will be the next token to
5437 // read. Return true if we are OK, false if we found EOF.
5440 Parse::skip_past_error(Operator op)
5442 this->gogo_->mark_locals_used();
5443 const Token* token = this->peek_token();
5444 while (!token->is_op(op))
5446 if (token->is_eof())
5448 if (token->is_op(OPERATOR_SEMICOLON))
5450 this->advance_token();
5453 token = this->advance_token();
5458 // Check that an expression is not a sink.
5461 Parse::verify_not_sink(Expression* expr)
5463 if (expr->is_sink_expression())
5465 error_at(expr->location(), "cannot use _ as value");
5466 expr = Expression::make_error(expr->location());
5471 // Mark a variable as used.
5474 Parse::mark_var_used(Named_object* no)
5476 if (no->is_variable())
5478 no->var_value()->set_is_used();
5480 // When a type switch uses := to define a variable, then for
5481 // each case with a single type we introduce a new variable with
5482 // the appropriate type. When we do, if the newly introduced
5483 // variable is used, then the type switch variable is used.
5484 Type_switch_vars::iterator p = this->type_switch_vars_.find(no);
5485 if (p != this->type_switch_vars_.end())
5486 p->second->var_value()->set_is_used();