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(0)),
46 unget_token_(Token::make_invalid_token(0)),
47 unget_token_valid_(false),
50 continue_stack_(NULL),
56 // Return the current token.
61 if (this->unget_token_valid_)
62 return &this->unget_token_;
63 if (this->token_.is_invalid())
64 this->token_ = this->lex_->next_token();
68 // Advance to the next token and return it.
71 Parse::advance_token()
73 if (this->unget_token_valid_)
75 this->unget_token_valid_ = false;
76 if (!this->token_.is_invalid())
79 this->token_ = this->lex_->next_token();
83 // Push a token back on the input stream.
86 Parse::unget_token(const Token& token)
88 go_assert(!this->unget_token_valid_);
89 this->unget_token_ = token;
90 this->unget_token_valid_ = true;
93 // The location of the current token.
98 return this->peek_token()->location();
101 // IdentifierList = identifier { "," identifier } .
104 Parse::identifier_list(Typed_identifier_list* til)
106 const Token* token = this->peek_token();
109 if (!token->is_identifier())
111 error_at(this->location(), "expected identifier");
115 this->gogo_->pack_hidden_name(token->identifier(),
116 token->is_identifier_exported());
117 til->push_back(Typed_identifier(name, NULL, token->location()));
118 token = this->advance_token();
119 if (!token->is_op(OPERATOR_COMMA))
121 token = this->advance_token();
125 // ExpressionList = Expression { "," Expression } .
127 // If MAY_BE_SINK is true, the expressions in the list may be "_".
130 Parse::expression_list(Expression* first, bool may_be_sink)
132 Expression_list* ret = new Expression_list();
134 ret->push_back(first);
137 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink, true,
140 const Token* token = this->peek_token();
141 if (!token->is_op(OPERATOR_COMMA))
144 // Most expression lists permit a trailing comma.
145 source_location location = token->location();
146 this->advance_token();
147 if (!this->expression_may_start_here())
149 this->unget_token(Token::make_operator_token(OPERATOR_COMMA,
156 // QualifiedIdent = [ PackageName "." ] identifier .
157 // PackageName = identifier .
159 // This sets *PNAME to the identifier and sets *PPACKAGE to the
160 // package or NULL if there isn't one. This returns true on success,
161 // false on failure in which case it will have emitted an error
165 Parse::qualified_ident(std::string* pname, Named_object** ppackage)
167 const Token* token = this->peek_token();
168 if (!token->is_identifier())
170 error_at(this->location(), "expected identifier");
174 std::string name = token->identifier();
175 bool is_exported = token->is_identifier_exported();
176 name = this->gogo_->pack_hidden_name(name, is_exported);
178 token = this->advance_token();
179 if (!token->is_op(OPERATOR_DOT))
186 Named_object* package = this->gogo_->lookup(name, NULL);
187 if (package == NULL || !package->is_package())
189 error_at(this->location(), "expected package");
190 // We expect . IDENTIFIER; skip both.
191 if (this->advance_token()->is_identifier())
192 this->advance_token();
196 package->package_value()->set_used();
198 token = this->advance_token();
199 if (!token->is_identifier())
201 error_at(this->location(), "expected identifier");
205 name = token->identifier();
209 error_at(this->location(), "invalid use of %<_%>");
213 if (package->name() == this->gogo_->package_name())
214 name = this->gogo_->pack_hidden_name(name,
215 token->is_identifier_exported());
220 this->advance_token();
225 // Type = TypeName | TypeLit | "(" Type ")" .
227 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
228 // SliceType | MapType | ChannelType .
233 const Token* token = this->peek_token();
234 if (token->is_identifier())
235 return this->type_name(true);
236 else if (token->is_op(OPERATOR_LSQUARE))
237 return this->array_type(false);
238 else if (token->is_keyword(KEYWORD_CHAN)
239 || token->is_op(OPERATOR_CHANOP))
240 return this->channel_type();
241 else if (token->is_keyword(KEYWORD_INTERFACE))
242 return this->interface_type();
243 else if (token->is_keyword(KEYWORD_FUNC))
245 source_location location = token->location();
246 this->advance_token();
247 Type* type = this->signature(NULL, location);
249 return Type::make_error_type();
252 else if (token->is_keyword(KEYWORD_MAP))
253 return this->map_type();
254 else if (token->is_keyword(KEYWORD_STRUCT))
255 return this->struct_type();
256 else if (token->is_op(OPERATOR_MULT))
257 return this->pointer_type();
258 else if (token->is_op(OPERATOR_LPAREN))
260 this->advance_token();
261 Type* ret = this->type();
262 if (this->peek_token()->is_op(OPERATOR_RPAREN))
263 this->advance_token();
266 if (!ret->is_error_type())
267 error_at(this->location(), "expected %<)%>");
273 error_at(token->location(), "expected type");
274 return Type::make_error_type();
279 Parse::type_may_start_here()
281 const Token* token = this->peek_token();
282 return (token->is_identifier()
283 || token->is_op(OPERATOR_LSQUARE)
284 || token->is_op(OPERATOR_CHANOP)
285 || token->is_keyword(KEYWORD_CHAN)
286 || token->is_keyword(KEYWORD_INTERFACE)
287 || token->is_keyword(KEYWORD_FUNC)
288 || token->is_keyword(KEYWORD_MAP)
289 || token->is_keyword(KEYWORD_STRUCT)
290 || token->is_op(OPERATOR_MULT)
291 || token->is_op(OPERATOR_LPAREN));
294 // TypeName = QualifiedIdent .
296 // If MAY_BE_NIL is true, then an identifier with the value of the
297 // predefined constant nil is accepted, returning the nil type.
300 Parse::type_name(bool issue_error)
302 source_location location = this->location();
305 Named_object* package;
306 if (!this->qualified_ident(&name, &package))
307 return Type::make_error_type();
309 Named_object* named_object;
311 named_object = this->gogo_->lookup(name, NULL);
314 named_object = package->package_value()->lookup(name);
315 if (named_object == NULL
317 && package->name() != this->gogo_->package_name())
319 // Check whether the name is there but hidden.
320 std::string s = ('.' + package->package_value()->unique_prefix()
321 + '.' + package->package_value()->name()
323 named_object = package->package_value()->lookup(s);
324 if (named_object != NULL)
326 const std::string& packname(package->package_value()->name());
327 error_at(location, "invalid reference to hidden type %<%s.%s%>",
328 Gogo::message_name(packname).c_str(),
329 Gogo::message_name(name).c_str());
336 if (named_object == NULL)
341 named_object = this->gogo_->add_unknown_name(name, location);
343 else if (named_object->is_type())
345 if (!named_object->type_value()->is_visible())
348 else if (named_object->is_unknown() || named_object->is_type_declaration())
356 error_at(location, "expected type");
357 return Type::make_error_type();
360 if (named_object->is_type())
361 return named_object->type_value();
362 else if (named_object->is_unknown() || named_object->is_type_declaration())
363 return Type::make_forward_declaration(named_object);
368 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
369 // ArrayLength = Expression .
370 // ElementType = CompleteType .
373 Parse::array_type(bool may_use_ellipsis)
375 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
376 const Token* token = this->advance_token();
378 Expression* length = NULL;
379 if (token->is_op(OPERATOR_RSQUARE))
380 this->advance_token();
383 if (!token->is_op(OPERATOR_ELLIPSIS))
384 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
385 else if (may_use_ellipsis)
387 // An ellipsis is used in composite literals to represent a
388 // fixed array of the size of the number of elements. We
389 // use a length of nil to represent this, and change the
390 // length when parsing the composite literal.
391 length = Expression::make_nil(this->location());
392 this->advance_token();
396 error_at(this->location(),
397 "use of %<[...]%> outside of array literal");
398 length = Expression::make_error(this->location());
399 this->advance_token();
401 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
403 error_at(this->location(), "expected %<]%>");
404 return Type::make_error_type();
406 this->advance_token();
409 Type* element_type = this->type();
411 return Type::make_array_type(element_type, length);
414 // MapType = "map" "[" KeyType "]" ValueType .
415 // KeyType = CompleteType .
416 // ValueType = CompleteType .
421 source_location location = this->location();
422 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
423 if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
425 error_at(this->location(), "expected %<[%>");
426 return Type::make_error_type();
428 this->advance_token();
430 Type* key_type = this->type();
432 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
434 error_at(this->location(), "expected %<]%>");
435 return Type::make_error_type();
437 this->advance_token();
439 Type* value_type = this->type();
441 if (key_type->is_error_type() || value_type->is_error_type())
442 return Type::make_error_type();
444 return Type::make_map_type(key_type, value_type, location);
447 // StructType = "struct" "{" { FieldDecl ";" } "}" .
452 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
453 source_location location = this->location();
454 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
456 source_location token_loc = this->location();
457 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
458 && this->advance_token()->is_op(OPERATOR_LCURLY))
459 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
462 error_at(this->location(), "expected %<{%>");
463 return Type::make_error_type();
466 this->advance_token();
468 Struct_field_list* sfl = new Struct_field_list;
469 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
471 this->field_decl(sfl);
472 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
473 this->advance_token();
474 else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
476 error_at(this->location(), "expected %<;%> or %<}%> or newline");
477 if (!this->skip_past_error(OPERATOR_RCURLY))
478 return Type::make_error_type();
481 this->advance_token();
483 for (Struct_field_list::const_iterator pi = sfl->begin();
487 if (pi->type()->is_error_type())
489 for (Struct_field_list::const_iterator pj = pi + 1;
493 if (pi->field_name() == pj->field_name()
494 && !Gogo::is_sink_name(pi->field_name()))
495 error_at(pi->location(), "duplicate field name %<%s%>",
496 Gogo::message_name(pi->field_name()).c_str());
500 return Type::make_struct_type(sfl, location);
503 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
504 // Tag = string_lit .
507 Parse::field_decl(Struct_field_list* sfl)
509 const Token* token = this->peek_token();
510 source_location location = token->location();
512 bool is_anonymous_pointer;
513 if (token->is_op(OPERATOR_MULT))
516 is_anonymous_pointer = true;
518 else if (token->is_identifier())
520 std::string id = token->identifier();
521 bool is_id_exported = token->is_identifier_exported();
522 source_location id_location = token->location();
523 token = this->advance_token();
524 is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
525 || token->is_op(OPERATOR_RCURLY)
526 || token->is_op(OPERATOR_DOT)
527 || token->is_string());
528 is_anonymous_pointer = false;
529 this->unget_token(Token::make_identifier_token(id, is_id_exported,
534 error_at(this->location(), "expected field name");
535 while (!token->is_op(OPERATOR_SEMICOLON)
536 && !token->is_op(OPERATOR_RCURLY)
538 token = this->advance_token();
544 if (is_anonymous_pointer)
546 this->advance_token();
547 if (!this->peek_token()->is_identifier())
549 error_at(this->location(), "expected field name");
550 while (!token->is_op(OPERATOR_SEMICOLON)
551 && !token->is_op(OPERATOR_RCURLY)
553 token = this->advance_token();
557 Type* type = this->type_name(true);
560 if (this->peek_token()->is_string())
562 tag = this->peek_token()->string_value();
563 this->advance_token();
566 if (!type->is_error_type())
568 if (is_anonymous_pointer)
569 type = Type::make_pointer_type(type);
570 sfl->push_back(Struct_field(Typed_identifier("", type, location)));
572 sfl->back().set_tag(tag);
577 Typed_identifier_list til;
580 token = this->peek_token();
581 if (!token->is_identifier())
583 error_at(this->location(), "expected identifier");
587 this->gogo_->pack_hidden_name(token->identifier(),
588 token->is_identifier_exported());
589 til.push_back(Typed_identifier(name, NULL, token->location()));
590 if (!this->advance_token()->is_op(OPERATOR_COMMA))
592 this->advance_token();
595 Type* type = this->type();
598 if (this->peek_token()->is_string())
600 tag = this->peek_token()->string_value();
601 this->advance_token();
604 for (Typed_identifier_list::iterator p = til.begin();
609 sfl->push_back(Struct_field(*p));
611 sfl->back().set_tag(tag);
616 // PointerType = "*" Type .
619 Parse::pointer_type()
621 go_assert(this->peek_token()->is_op(OPERATOR_MULT));
622 this->advance_token();
623 Type* type = this->type();
624 if (type->is_error_type())
626 return Type::make_pointer_type(type);
629 // ChannelType = Channel | SendChannel | RecvChannel .
630 // Channel = "chan" ElementType .
631 // SendChannel = "chan" "<-" ElementType .
632 // RecvChannel = "<-" "chan" ElementType .
635 Parse::channel_type()
637 const Token* token = this->peek_token();
640 if (token->is_op(OPERATOR_CHANOP))
642 if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
644 error_at(this->location(), "expected %<chan%>");
645 return Type::make_error_type();
648 this->advance_token();
652 go_assert(token->is_keyword(KEYWORD_CHAN));
653 if (this->advance_token()->is_op(OPERATOR_CHANOP))
656 this->advance_token();
660 // Better error messages for the common error of omitting the
661 // channel element type.
662 if (!this->type_may_start_here())
664 token = this->peek_token();
665 if (token->is_op(OPERATOR_RCURLY))
666 error_at(this->location(), "unexpected %<}%> in channel type");
667 else if (token->is_op(OPERATOR_RPAREN))
668 error_at(this->location(), "unexpected %<)%> in channel type");
669 else if (token->is_op(OPERATOR_COMMA))
670 error_at(this->location(), "unexpected comma in channel type");
672 error_at(this->location(), "expected channel element type");
673 return Type::make_error_type();
676 Type* element_type = this->type();
677 return Type::make_channel_type(send, receive, element_type);
680 // Signature = Parameters [ Result ] .
682 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
683 // location of the start of the type.
685 // This returns NULL on a parse error.
688 Parse::signature(Typed_identifier* receiver, source_location location)
690 bool is_varargs = false;
691 Typed_identifier_list* params;
692 bool params_ok = this->parameters(¶ms, &is_varargs);
694 Typed_identifier_list* result = NULL;
695 if (this->peek_token()->is_op(OPERATOR_LPAREN)
696 || this->type_may_start_here())
698 if (!this->result(&result))
705 Function_type* ret = Type::make_function_type(receiver, params, result,
708 ret->set_is_varargs();
712 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
714 // This returns false on a parse error.
717 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
721 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
723 error_at(this->location(), "expected %<(%>");
727 Typed_identifier_list* params = NULL;
728 bool saw_error = false;
730 const Token* token = this->advance_token();
731 if (!token->is_op(OPERATOR_RPAREN))
733 params = this->parameter_list(is_varargs);
736 token = this->peek_token();
739 // The optional trailing comma is picked up in parameter_list.
741 if (!token->is_op(OPERATOR_RPAREN))
742 error_at(this->location(), "expected %<)%>");
744 this->advance_token();
753 // ParameterList = ParameterDecl { "," ParameterDecl } .
755 // This sets *IS_VARARGS if the list ends with an ellipsis.
756 // IS_VARARGS will be NULL if varargs are not permitted.
758 // We pick up an optional trailing comma.
760 // This returns NULL if some error is seen.
762 Typed_identifier_list*
763 Parse::parameter_list(bool* is_varargs)
765 source_location location = this->location();
766 Typed_identifier_list* ret = new Typed_identifier_list();
768 bool saw_error = false;
770 // If we see an identifier and then a comma, then we don't know
771 // whether we are looking at a list of identifiers followed by a
772 // type, or a list of types given by name. We have to do an
773 // arbitrary lookahead to figure it out.
775 bool parameters_have_names;
776 const Token* token = this->peek_token();
777 if (!token->is_identifier())
779 // This must be a type which starts with something like '*'.
780 parameters_have_names = false;
784 std::string name = token->identifier();
785 bool is_exported = token->is_identifier_exported();
786 source_location location = token->location();
787 token = this->advance_token();
788 if (!token->is_op(OPERATOR_COMMA))
790 if (token->is_op(OPERATOR_DOT))
792 // This is a qualified identifier, which must turn out
794 parameters_have_names = false;
796 else if (token->is_op(OPERATOR_RPAREN))
798 // A single identifier followed by a parenthesis must be
800 parameters_have_names = false;
804 // An identifier followed by something other than a
805 // comma or a dot or a right parenthesis must be a
806 // parameter name followed by a type.
807 parameters_have_names = true;
810 this->unget_token(Token::make_identifier_token(name, is_exported,
815 // An identifier followed by a comma may be the first in a
816 // list of parameter names followed by a type, or it may be
817 // the first in a list of types without parameter names. To
818 // find out we gather as many identifiers separated by
820 std::string id_name = this->gogo_->pack_hidden_name(name,
822 ret->push_back(Typed_identifier(id_name, NULL, location));
823 bool just_saw_comma = true;
824 while (this->advance_token()->is_identifier())
826 name = this->peek_token()->identifier();
827 is_exported = this->peek_token()->is_identifier_exported();
828 location = this->peek_token()->location();
829 id_name = this->gogo_->pack_hidden_name(name, is_exported);
830 ret->push_back(Typed_identifier(id_name, NULL, location));
831 if (!this->advance_token()->is_op(OPERATOR_COMMA))
833 just_saw_comma = false;
840 // We saw ID1 "," ID2 "," followed by something which
841 // was not an identifier. We must be seeing the start
842 // of a type, and ID1 and ID2 must be types, and the
843 // parameters don't have names.
844 parameters_have_names = false;
846 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
848 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
849 // and the parameters don't have names.
850 parameters_have_names = false;
852 else if (this->peek_token()->is_op(OPERATOR_DOT))
854 // We saw ID1 "," ID2 ".". ID2 must be a package name,
855 // ID1 must be a type, and the parameters don't have
857 parameters_have_names = false;
858 this->unget_token(Token::make_identifier_token(name, is_exported,
861 just_saw_comma = true;
865 // We saw ID1 "," ID2 followed by something other than
866 // ",", ".", or ")". We must be looking at the start of
867 // a type, and ID1 and ID2 must be parameter names.
868 parameters_have_names = true;
871 if (parameters_have_names)
873 go_assert(!just_saw_comma);
874 // We have just seen ID1, ID2 xxx.
876 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
880 error_at(this->location(), "%<...%> only permits one name");
882 this->advance_token();
885 for (size_t i = 0; i < ret->size(); ++i)
886 ret->set_type(i, type);
887 if (!this->peek_token()->is_op(OPERATOR_COMMA))
888 return saw_error ? NULL : ret;
889 if (this->advance_token()->is_op(OPERATOR_RPAREN))
890 return saw_error ? NULL : ret;
894 Typed_identifier_list* tret = new Typed_identifier_list();
895 for (Typed_identifier_list::const_iterator p = ret->begin();
899 Named_object* no = this->gogo_->lookup(p->name(), NULL);
902 no = this->gogo_->add_unknown_name(p->name(),
906 type = no->type_value();
907 else if (no->is_unknown() || no->is_type_declaration())
908 type = Type::make_forward_declaration(no);
911 error_at(p->location(), "expected %<%s%> to be a type",
912 Gogo::message_name(p->name()).c_str());
914 type = Type::make_error_type();
916 tret->push_back(Typed_identifier("", type, p->location()));
921 || this->peek_token()->is_op(OPERATOR_RPAREN))
922 return saw_error ? NULL : ret;
927 bool mix_error = false;
928 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
929 while (this->peek_token()->is_op(OPERATOR_COMMA))
931 if (is_varargs != NULL && *is_varargs)
933 error_at(this->location(), "%<...%> must be last parameter");
936 if (this->advance_token()->is_op(OPERATOR_RPAREN))
938 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
942 error_at(location, "invalid named/anonymous mix");
953 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
956 Parse::parameter_decl(bool parameters_have_names,
957 Typed_identifier_list* til,
961 if (!parameters_have_names)
964 source_location location = this->location();
965 if (!this->peek_token()->is_identifier())
967 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
971 if (is_varargs == NULL)
972 error_at(this->location(), "invalid use of %<...%>");
975 this->advance_token();
976 if (is_varargs == NULL
977 && this->peek_token()->is_op(OPERATOR_RPAREN))
978 type = Type::make_error_type();
981 Type* element_type = this->type();
982 type = Type::make_array_type(element_type, NULL);
988 type = this->type_name(false);
989 if (type->is_error_type()
990 || (!this->peek_token()->is_op(OPERATOR_COMMA)
991 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
994 while (!this->peek_token()->is_op(OPERATOR_COMMA)
995 && !this->peek_token()->is_op(OPERATOR_RPAREN))
996 this->advance_token();
999 if (!type->is_error_type())
1000 til->push_back(Typed_identifier("", type, location));
1004 size_t orig_count = til->size();
1005 if (this->peek_token()->is_identifier())
1006 this->identifier_list(til);
1009 size_t new_count = til->size();
1012 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1013 type = this->type();
1016 if (is_varargs == NULL)
1017 error_at(this->location(), "invalid use of %<...%>");
1018 else if (new_count > orig_count + 1)
1019 error_at(this->location(), "%<...%> only permits one name");
1022 this->advance_token();
1023 Type* element_type = this->type();
1024 type = Type::make_array_type(element_type, NULL);
1026 for (size_t i = orig_count; i < new_count; ++i)
1027 til->set_type(i, type);
1031 // Result = Parameters | Type .
1033 // This returns false on a parse error.
1036 Parse::result(Typed_identifier_list** presults)
1038 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1039 return this->parameters(presults, NULL);
1042 source_location location = this->location();
1043 Type* type = this->type();
1044 if (type->is_error_type())
1049 Typed_identifier_list* til = new Typed_identifier_list();
1050 til->push_back(Typed_identifier("", type, location));
1056 // Block = "{" [ StatementList ] "}" .
1058 // Returns the location of the closing brace.
1063 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1065 source_location loc = this->location();
1066 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1067 && this->advance_token()->is_op(OPERATOR_LCURLY))
1068 error_at(loc, "unexpected semicolon or newline before %<{%>");
1071 error_at(this->location(), "expected %<{%>");
1072 return UNKNOWN_LOCATION;
1076 const Token* token = this->advance_token();
1078 if (!token->is_op(OPERATOR_RCURLY))
1080 this->statement_list();
1081 token = this->peek_token();
1082 if (!token->is_op(OPERATOR_RCURLY))
1084 if (!token->is_eof() || !saw_errors())
1085 error_at(this->location(), "expected %<}%>");
1087 // Skip ahead to the end of the block, in hopes of avoiding
1088 // lots of meaningless errors.
1089 source_location ret = token->location();
1091 while (!token->is_eof())
1093 if (token->is_op(OPERATOR_LCURLY))
1095 else if (token->is_op(OPERATOR_RCURLY))
1100 this->advance_token();
1104 token = this->advance_token();
1105 ret = token->location();
1111 source_location ret = token->location();
1112 this->advance_token();
1116 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1117 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1120 Parse::interface_type()
1122 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1123 source_location location = this->location();
1125 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1127 source_location token_loc = this->location();
1128 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1129 && this->advance_token()->is_op(OPERATOR_LCURLY))
1130 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1133 error_at(this->location(), "expected %<{%>");
1134 return Type::make_error_type();
1137 this->advance_token();
1139 Typed_identifier_list* methods = new Typed_identifier_list();
1140 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1142 this->method_spec(methods);
1143 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1145 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1147 this->method_spec(methods);
1149 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1151 error_at(this->location(), "expected %<}%>");
1152 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1154 if (this->peek_token()->is_eof())
1155 return Type::make_error_type();
1159 this->advance_token();
1161 if (methods->empty())
1167 Interface_type* ret = Type::make_interface_type(methods, location);
1168 this->gogo_->record_interface_type(ret);
1172 // MethodSpec = MethodName Signature | InterfaceTypeName .
1173 // MethodName = identifier .
1174 // InterfaceTypeName = TypeName .
1177 Parse::method_spec(Typed_identifier_list* methods)
1179 const Token* token = this->peek_token();
1180 if (!token->is_identifier())
1182 error_at(this->location(), "expected identifier");
1186 std::string name = token->identifier();
1187 bool is_exported = token->is_identifier_exported();
1188 source_location location = token->location();
1190 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1192 // This is a MethodName.
1193 name = this->gogo_->pack_hidden_name(name, is_exported);
1194 Type* type = this->signature(NULL, location);
1197 methods->push_back(Typed_identifier(name, type, location));
1201 this->unget_token(Token::make_identifier_token(name, is_exported,
1203 Type* type = this->type_name(false);
1204 if (type->is_error_type()
1205 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1206 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1208 if (this->peek_token()->is_op(OPERATOR_COMMA))
1209 error_at(this->location(),
1210 "name list not allowed in interface type");
1212 error_at(location, "expected signature or type name");
1213 token = this->peek_token();
1214 while (!token->is_eof()
1215 && !token->is_op(OPERATOR_SEMICOLON)
1216 && !token->is_op(OPERATOR_RCURLY))
1217 token = this->advance_token();
1220 // This must be an interface type, but we can't check that now.
1221 // We check it and pull out the methods in
1222 // Interface_type::do_verify.
1223 methods->push_back(Typed_identifier("", type, location));
1227 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1230 Parse::declaration()
1232 const Token* token = this->peek_token();
1233 if (token->is_keyword(KEYWORD_CONST))
1235 else if (token->is_keyword(KEYWORD_TYPE))
1237 else if (token->is_keyword(KEYWORD_VAR))
1239 else if (token->is_keyword(KEYWORD_FUNC))
1240 this->function_decl();
1243 error_at(this->location(), "expected declaration");
1244 this->advance_token();
1249 Parse::declaration_may_start_here()
1251 const Token* token = this->peek_token();
1252 return (token->is_keyword(KEYWORD_CONST)
1253 || token->is_keyword(KEYWORD_TYPE)
1254 || token->is_keyword(KEYWORD_VAR)
1255 || token->is_keyword(KEYWORD_FUNC));
1258 // Decl<P> = P | "(" [ List<P> ] ")" .
1261 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1263 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1267 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1269 this->list(pfn, varg, true);
1270 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1272 error_at(this->location(), "missing %<)%>");
1273 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1275 if (this->peek_token()->is_eof())
1280 this->advance_token();
1284 // List<P> = P { ";" P } [ ";" ] .
1286 // In order to pick up the trailing semicolon we need to know what
1287 // might follow. This is either a '}' or a ')'.
1290 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1293 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1294 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1295 || this->peek_token()->is_op(OPERATOR_COMMA))
1297 if (this->peek_token()->is_op(OPERATOR_COMMA))
1298 error_at(this->location(), "unexpected comma");
1299 if (this->advance_token()->is_op(follow))
1305 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1310 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1311 this->advance_token();
1314 Type* last_type = NULL;
1315 Expression_list* last_expr_list = NULL;
1317 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1318 this->const_spec(&last_type, &last_expr_list);
1321 this->advance_token();
1322 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1324 this->const_spec(&last_type, &last_expr_list);
1325 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1326 this->advance_token();
1327 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1329 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1330 if (!this->skip_past_error(OPERATOR_RPAREN))
1334 this->advance_token();
1337 if (last_expr_list != NULL)
1338 delete last_expr_list;
1341 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1344 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1346 Typed_identifier_list til;
1347 this->identifier_list(&til);
1350 if (this->type_may_start_here())
1352 type = this->type();
1354 *last_expr_list = NULL;
1357 Expression_list *expr_list;
1358 if (!this->peek_token()->is_op(OPERATOR_EQ))
1360 if (*last_expr_list == NULL)
1362 error_at(this->location(), "expected %<=%>");
1366 expr_list = new Expression_list;
1367 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1368 p != (*last_expr_list)->end();
1370 expr_list->push_back((*p)->copy());
1374 this->advance_token();
1375 expr_list = this->expression_list(NULL, false);
1377 if (*last_expr_list != NULL)
1378 delete *last_expr_list;
1379 *last_expr_list = expr_list;
1382 Expression_list::const_iterator pe = expr_list->begin();
1383 for (Typed_identifier_list::iterator pi = til.begin();
1387 if (pe == expr_list->end())
1389 error_at(this->location(), "not enough initializers");
1395 if (!Gogo::is_sink_name(pi->name()))
1396 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1398 if (pe != expr_list->end())
1399 error_at(this->location(), "too many initializers");
1401 this->increment_iota();
1406 // TypeDecl = "type" Decl<TypeSpec> .
1411 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1412 this->advance_token();
1413 this->decl(&Parse::type_spec, NULL);
1416 // TypeSpec = identifier Type .
1419 Parse::type_spec(void*)
1421 const Token* token = this->peek_token();
1422 if (!token->is_identifier())
1424 error_at(this->location(), "expected identifier");
1427 std::string name = token->identifier();
1428 bool is_exported = token->is_identifier_exported();
1429 source_location location = token->location();
1430 token = this->advance_token();
1432 // The scope of the type name starts at the point where the
1433 // identifier appears in the source code. We implement this by
1434 // declaring the type before we read the type definition.
1435 Named_object* named_type = NULL;
1438 name = this->gogo_->pack_hidden_name(name, is_exported);
1439 named_type = this->gogo_->declare_type(name, location);
1443 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1444 type = this->type();
1447 error_at(this->location(),
1448 "unexpected semicolon or newline in type declaration");
1449 type = Type::make_error_type();
1450 this->advance_token();
1453 if (type->is_error_type())
1455 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1456 && !this->peek_token()->is_eof())
1457 this->advance_token();
1462 if (named_type->is_type_declaration())
1464 Type* ftype = type->forwarded();
1465 if (ftype->forward_declaration_type() != NULL
1466 && (ftype->forward_declaration_type()->named_object()
1469 error_at(location, "invalid recursive type");
1470 type = Type::make_error_type();
1473 this->gogo_->define_type(named_type,
1474 Type::make_named_type(named_type, type,
1476 go_assert(named_type->package() == NULL);
1480 // This will probably give a redefinition error.
1481 this->gogo_->add_type(name, type, location);
1486 // VarDecl = "var" Decl<VarSpec> .
1491 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1492 this->advance_token();
1493 this->decl(&Parse::var_spec, NULL);
1496 // VarSpec = IdentifierList
1497 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1500 Parse::var_spec(void*)
1502 // Get the variable names.
1503 Typed_identifier_list til;
1504 this->identifier_list(&til);
1506 source_location location = this->location();
1509 Expression_list* init = NULL;
1510 if (!this->peek_token()->is_op(OPERATOR_EQ))
1512 type = this->type();
1513 if (type->is_error_type())
1515 while (!this->peek_token()->is_op(OPERATOR_EQ)
1516 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1517 && !this->peek_token()->is_eof())
1518 this->advance_token();
1520 if (this->peek_token()->is_op(OPERATOR_EQ))
1522 this->advance_token();
1523 init = this->expression_list(NULL, false);
1528 this->advance_token();
1529 init = this->expression_list(NULL, false);
1532 this->init_vars(&til, type, init, false, location);
1538 // Create variables. TIL is a list of variable names. If TYPE is not
1539 // NULL, it is the type of all the variables. If INIT is not NULL, it
1540 // is an initializer list for the variables.
1543 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1544 Expression_list* init, bool is_coloneq,
1545 source_location location)
1547 // Check for an initialization which can yield multiple values.
1548 if (init != NULL && init->size() == 1 && til->size() > 1)
1550 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1553 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1556 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1559 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1560 is_coloneq, location))
1564 if (init != NULL && init->size() != til->size())
1566 if (init->empty() || !init->front()->is_error_expression())
1567 error_at(location, "wrong number of initializations");
1570 type = Type::make_error_type();
1573 // Note that INIT was already parsed with the old name bindings, so
1574 // we don't have to worry that it will accidentally refer to the
1575 // newly declared variables.
1577 Expression_list::const_iterator pexpr;
1579 pexpr = init->begin();
1580 bool any_new = false;
1581 for (Typed_identifier_list::const_iterator p = til->begin();
1586 go_assert(pexpr != init->end());
1587 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1593 go_assert(pexpr == init->end());
1594 if (is_coloneq && !any_new)
1595 error_at(location, "variables redeclared but no variable is new");
1598 // See if we need to initialize a list of variables from a function
1599 // call. This returns true if we have set up the variables and the
1603 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1604 Expression* expr, bool is_coloneq,
1605 source_location location)
1607 Call_expression* call = expr->call_expression();
1611 // This is a function call. We can't check here whether it returns
1612 // the right number of values, but it might. Declare the variables,
1613 // and then assign the results of the call to them.
1615 unsigned int index = 0;
1616 bool any_new = false;
1617 for (Typed_identifier_list::const_iterator pv = vars->begin();
1621 Expression* init = Expression::make_call_result(call, index);
1622 this->init_var(*pv, type, init, is_coloneq, false, &any_new);
1625 if (is_coloneq && !any_new)
1626 error_at(location, "variables redeclared but no variable is new");
1631 // See if we need to initialize a pair of values from a map index
1632 // expression. This returns true if we have set up the variables and
1633 // the initialization.
1636 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1637 Expression* expr, bool is_coloneq,
1638 source_location location)
1640 Index_expression* index = expr->index_expression();
1643 if (vars->size() != 2)
1646 // This is an index which is being assigned to two variables. It
1647 // must be a map index. Declare the variables, and then assign the
1648 // results of the map index.
1649 bool any_new = false;
1650 Typed_identifier_list::const_iterator p = vars->begin();
1651 Expression* init = type == NULL ? index : NULL;
1652 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1653 type == NULL, &any_new);
1654 if (type == NULL && any_new && val_no->is_variable())
1655 val_no->var_value()->set_type_from_init_tuple();
1656 Expression* val_var = Expression::make_var_reference(val_no, location);
1659 Type* var_type = type;
1660 if (var_type == NULL)
1661 var_type = Type::lookup_bool_type();
1662 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1664 Expression* present_var = Expression::make_var_reference(no, location);
1666 if (is_coloneq && !any_new)
1667 error_at(location, "variables redeclared but no variable is new");
1669 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1672 if (!this->gogo_->in_global_scope())
1673 this->gogo_->add_statement(s);
1674 else if (!val_no->is_sink())
1676 if (val_no->is_variable())
1677 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1679 else if (!no->is_sink())
1681 if (no->is_variable())
1682 no->var_value()->add_preinit_statement(this->gogo_, s);
1686 // Execute the map index expression just so that we can fail if
1688 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1690 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1696 // See if we need to initialize a pair of values from a receive
1697 // expression. This returns true if we have set up the variables and
1698 // the initialization.
1701 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1702 Expression* expr, bool is_coloneq,
1703 source_location location)
1705 Receive_expression* receive = expr->receive_expression();
1706 if (receive == NULL)
1708 if (vars->size() != 2)
1711 // This is a receive expression which is being assigned to two
1712 // variables. Declare the variables, and then assign the results of
1714 bool any_new = false;
1715 Typed_identifier_list::const_iterator p = vars->begin();
1716 Expression* init = type == NULL ? receive : NULL;
1717 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1718 type == NULL, &any_new);
1719 if (type == NULL && any_new && val_no->is_variable())
1720 val_no->var_value()->set_type_from_init_tuple();
1721 Expression* val_var = Expression::make_var_reference(val_no, location);
1724 Type* var_type = type;
1725 if (var_type == NULL)
1726 var_type = Type::lookup_bool_type();
1727 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1729 Expression* received_var = Expression::make_var_reference(no, location);
1731 if (is_coloneq && !any_new)
1732 error_at(location, "variables redeclared but no variable is new");
1734 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1740 if (!this->gogo_->in_global_scope())
1741 this->gogo_->add_statement(s);
1742 else if (!val_no->is_sink())
1744 if (val_no->is_variable())
1745 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1747 else if (!no->is_sink())
1749 if (no->is_variable())
1750 no->var_value()->add_preinit_statement(this->gogo_, s);
1754 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1756 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1762 // See if we need to initialize a pair of values from a type guard
1763 // expression. This returns true if we have set up the variables and
1764 // the initialization.
1767 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1768 Type* type, Expression* expr,
1769 bool is_coloneq, source_location location)
1771 Type_guard_expression* type_guard = expr->type_guard_expression();
1772 if (type_guard == NULL)
1774 if (vars->size() != 2)
1777 // This is a type guard expression which is being assigned to two
1778 // variables. Declare the variables, and then assign the results of
1780 bool any_new = false;
1781 Typed_identifier_list::const_iterator p = vars->begin();
1782 Type* var_type = type;
1783 if (var_type == NULL)
1784 var_type = type_guard->type();
1785 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1787 Expression* val_var = Expression::make_var_reference(val_no, location);
1791 if (var_type == NULL)
1792 var_type = Type::lookup_bool_type();
1793 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1795 Expression* ok_var = Expression::make_var_reference(no, location);
1797 Expression* texpr = type_guard->expr();
1798 Type* t = type_guard->type();
1799 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1803 if (is_coloneq && !any_new)
1804 error_at(location, "variables redeclared but no variable is new");
1806 if (!this->gogo_->in_global_scope())
1807 this->gogo_->add_statement(s);
1808 else if (!val_no->is_sink())
1810 if (val_no->is_variable())
1811 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1813 else if (!no->is_sink())
1815 if (no->is_variable())
1816 no->var_value()->add_preinit_statement(this->gogo_, s);
1820 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1821 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1827 // Create a single variable. If IS_COLONEQ is true, we permit
1828 // redeclarations in the same block, and we set *IS_NEW when we find a
1829 // new variable which is not a redeclaration.
1832 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1833 bool is_coloneq, bool type_from_init, bool* is_new)
1835 source_location location = tid.location();
1837 if (Gogo::is_sink_name(tid.name()))
1839 if (!type_from_init && init != NULL)
1841 if (!this->gogo_->in_global_scope())
1842 this->gogo_->add_statement(Statement::make_statement(init));
1844 return this->create_dummy_global(type, init, location);
1846 return this->gogo_->add_sink();
1851 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1853 && (no->is_variable() || no->is_result_variable()))
1855 // INIT may be NULL even when IS_COLONEQ is true for cases
1856 // like v, ok := x.(int).
1857 if (!type_from_init && init != NULL)
1859 Expression *v = Expression::make_var_reference(no, location);
1860 Statement *s = Statement::make_assignment(v, init, location);
1861 this->gogo_->add_statement(s);
1867 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1868 false, false, location);
1869 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1870 if (!no->is_variable())
1872 // The name is already defined, so we just gave an error.
1873 return this->gogo_->add_sink();
1878 // Create a dummy global variable to force an initializer to be run in
1879 // the right place. This is used when a sink variable is initialized
1883 Parse::create_dummy_global(Type* type, Expression* init,
1884 source_location location)
1886 if (type == NULL && init == NULL)
1887 type = Type::lookup_bool_type();
1888 Variable* var = new Variable(type, init, true, false, false, location);
1891 snprintf(buf, sizeof buf, "_.%d", count);
1893 return this->gogo_->add_variable(buf, var);
1896 // SimpleVarDecl = identifier ":=" Expression .
1898 // We've already seen the identifier.
1900 // FIXME: We also have to implement
1901 // IdentifierList ":=" ExpressionList
1902 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1903 // tuple assignments here as well.
1905 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
1908 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
1909 // guard (var := expr.("type") using the literal keyword "type").
1912 Parse::simple_var_decl_or_assignment(const std::string& name,
1913 source_location location,
1914 Range_clause* p_range_clause,
1915 Type_switch* p_type_switch)
1917 Typed_identifier_list til;
1918 til.push_back(Typed_identifier(name, NULL, location));
1920 // We've seen one identifier. If we see a comma now, this could be
1922 if (this->peek_token()->is_op(OPERATOR_COMMA))
1924 go_assert(p_type_switch == NULL);
1927 const Token* token = this->advance_token();
1928 if (!token->is_identifier())
1931 std::string id = token->identifier();
1932 bool is_id_exported = token->is_identifier_exported();
1933 source_location id_location = token->location();
1935 token = this->advance_token();
1936 if (!token->is_op(OPERATOR_COMMA))
1938 if (token->is_op(OPERATOR_COLONEQ))
1940 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1941 til.push_back(Typed_identifier(id, NULL, location));
1944 this->unget_token(Token::make_identifier_token(id,
1950 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1951 til.push_back(Typed_identifier(id, NULL, location));
1954 // We have a comma separated list of identifiers in TIL. If the
1955 // next token is COLONEQ, then this is a simple var decl, and we
1956 // have the complete list of identifiers. If the next token is
1957 // not COLONEQ, then the only valid parse is a tuple assignment.
1958 // The list of identifiers we have so far is really a list of
1959 // expressions. There are more expressions following.
1961 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
1963 Expression_list* exprs = new Expression_list;
1964 for (Typed_identifier_list::const_iterator p = til.begin();
1967 exprs->push_back(this->id_to_expression(p->name(),
1970 Expression_list* more_exprs = this->expression_list(NULL, true);
1971 for (Expression_list::const_iterator p = more_exprs->begin();
1972 p != more_exprs->end();
1974 exprs->push_back(*p);
1977 this->tuple_assignment(exprs, p_range_clause);
1982 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
1983 const Token* token = this->advance_token();
1985 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
1987 this->range_clause_decl(&til, p_range_clause);
1991 Expression_list* init;
1992 if (p_type_switch == NULL)
1993 init = this->expression_list(NULL, false);
1996 bool is_type_switch = false;
1997 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2001 p_type_switch->found = true;
2002 p_type_switch->name = name;
2003 p_type_switch->location = location;
2004 p_type_switch->expr = expr;
2008 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2010 init = new Expression_list();
2011 init->push_back(expr);
2015 this->advance_token();
2016 init = this->expression_list(expr, false);
2020 this->init_vars(&til, NULL, init, true, location);
2023 // FunctionDecl = "func" identifier Signature [ Block ] .
2024 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2027 // FunctionDecl = "func" identifier Signature
2028 // __asm__ "(" string_lit ")" .
2029 // This extension means a function whose real name is the identifier
2033 Parse::function_decl()
2035 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2036 source_location location = this->location();
2037 const Token* token = this->advance_token();
2039 Typed_identifier* rec = NULL;
2040 if (token->is_op(OPERATOR_LPAREN))
2042 rec = this->receiver();
2043 token = this->peek_token();
2046 if (!token->is_identifier())
2048 error_at(this->location(), "expected function name");
2053 this->gogo_->pack_hidden_name(token->identifier(),
2054 token->is_identifier_exported());
2056 this->advance_token();
2058 Function_type* fntype = this->signature(rec, this->location());
2062 Named_object* named_object = NULL;
2064 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2066 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2068 error_at(this->location(), "expected %<(%>");
2071 token = this->advance_token();
2072 if (!token->is_string())
2074 error_at(this->location(), "expected string");
2077 std::string asm_name = token->string_value();
2078 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2080 error_at(this->location(), "expected %<)%>");
2083 this->advance_token();
2084 if (!Gogo::is_sink_name(name))
2086 named_object = this->gogo_->declare_function(name, fntype, location);
2087 if (named_object->is_function_declaration())
2088 named_object->func_declaration_value()->set_asm_name(asm_name);
2092 // Check for the easy error of a newline before the opening brace.
2093 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2095 source_location semi_loc = this->location();
2096 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2097 error_at(this->location(),
2098 "unexpected semicolon or newline before %<{%>");
2100 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2104 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2106 if (named_object == NULL && !Gogo::is_sink_name(name))
2107 this->gogo_->declare_function(name, fntype, location);
2111 this->gogo_->start_function(name, fntype, true, location);
2112 source_location end_loc = this->block();
2113 this->gogo_->finish_function(end_loc);
2117 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2118 // BaseTypeName = identifier .
2123 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2126 const Token* token = this->advance_token();
2127 source_location location = token->location();
2128 if (!token->is_op(OPERATOR_MULT))
2130 if (!token->is_identifier())
2132 error_at(this->location(), "method has no receiver");
2133 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2134 token = this->advance_token();
2135 if (!token->is_eof())
2136 this->advance_token();
2139 name = token->identifier();
2140 bool is_exported = token->is_identifier_exported();
2141 token = this->advance_token();
2142 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2144 // An identifier followed by something other than a dot or a
2145 // right parenthesis must be a receiver name followed by a
2147 name = this->gogo_->pack_hidden_name(name, is_exported);
2151 // This must be a type name.
2152 this->unget_token(Token::make_identifier_token(name, is_exported,
2154 token = this->peek_token();
2159 // Here the receiver name is in NAME (it is empty if the receiver is
2160 // unnamed) and TOKEN is the first token in the type.
2162 bool is_pointer = false;
2163 if (token->is_op(OPERATOR_MULT))
2166 token = this->advance_token();
2169 if (!token->is_identifier())
2171 error_at(this->location(), "expected receiver name or type");
2172 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2173 while (!token->is_eof())
2175 token = this->advance_token();
2176 if (token->is_op(OPERATOR_LPAREN))
2178 else if (token->is_op(OPERATOR_RPAREN))
2185 if (!token->is_eof())
2186 this->advance_token();
2190 Type* type = this->type_name(true);
2192 if (is_pointer && !type->is_error_type())
2193 type = Type::make_pointer_type(type);
2195 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2196 this->advance_token();
2199 if (this->peek_token()->is_op(OPERATOR_COMMA))
2200 error_at(this->location(), "method has multiple receivers");
2202 error_at(this->location(), "expected %<)%>");
2203 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2204 token = this->advance_token();
2205 if (!token->is_eof())
2206 this->advance_token();
2210 return new Typed_identifier(name, type, location);
2213 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2214 // Literal = BasicLit | CompositeLit | FunctionLit .
2215 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2217 // If MAY_BE_SINK is true, this operand may be "_".
2220 Parse::operand(bool may_be_sink)
2222 const Token* token = this->peek_token();
2224 switch (token->classification())
2226 case Token::TOKEN_IDENTIFIER:
2228 source_location location = token->location();
2229 std::string id = token->identifier();
2230 bool is_exported = token->is_identifier_exported();
2231 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2233 Named_object* in_function;
2234 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2236 Package* package = NULL;
2237 if (named_object != NULL && named_object->is_package())
2239 if (!this->advance_token()->is_op(OPERATOR_DOT)
2240 || !this->advance_token()->is_identifier())
2242 error_at(location, "unexpected reference to package");
2243 return Expression::make_error(location);
2245 package = named_object->package_value();
2246 package->set_used();
2247 id = this->peek_token()->identifier();
2248 is_exported = this->peek_token()->is_identifier_exported();
2249 packed = this->gogo_->pack_hidden_name(id, is_exported);
2250 named_object = package->lookup(packed);
2251 location = this->location();
2252 go_assert(in_function == NULL);
2255 this->advance_token();
2257 if (named_object != NULL
2258 && named_object->is_type()
2259 && !named_object->type_value()->is_visible())
2261 go_assert(package != NULL);
2262 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2263 Gogo::message_name(package->name()).c_str(),
2264 Gogo::message_name(id).c_str());
2265 return Expression::make_error(location);
2269 if (named_object == NULL)
2271 if (package != NULL)
2273 std::string n1 = Gogo::message_name(package->name());
2274 std::string n2 = Gogo::message_name(id);
2277 ("invalid reference to unexported identifier "
2279 n1.c_str(), n2.c_str());
2282 "reference to undefined identifier %<%s.%s%>",
2283 n1.c_str(), n2.c_str());
2284 return Expression::make_error(location);
2287 named_object = this->gogo_->add_unknown_name(packed, location);
2290 if (in_function != NULL
2291 && in_function != this->gogo_->current_function()
2292 && (named_object->is_variable()
2293 || named_object->is_result_variable()))
2294 return this->enclosing_var_reference(in_function, named_object,
2297 switch (named_object->classification())
2299 case Named_object::NAMED_OBJECT_CONST:
2300 return Expression::make_const_reference(named_object, location);
2301 case Named_object::NAMED_OBJECT_TYPE:
2302 return Expression::make_type(named_object->type_value(), location);
2303 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2305 Type* t = Type::make_forward_declaration(named_object);
2306 return Expression::make_type(t, location);
2308 case Named_object::NAMED_OBJECT_VAR:
2309 case Named_object::NAMED_OBJECT_RESULT_VAR:
2310 return Expression::make_var_reference(named_object, location);
2311 case Named_object::NAMED_OBJECT_SINK:
2313 return Expression::make_sink(location);
2316 error_at(location, "cannot use _ as value");
2317 return Expression::make_error(location);
2319 case Named_object::NAMED_OBJECT_FUNC:
2320 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2321 return Expression::make_func_reference(named_object, NULL,
2323 case Named_object::NAMED_OBJECT_UNKNOWN:
2324 return Expression::make_unknown_reference(named_object, location);
2331 case Token::TOKEN_STRING:
2332 ret = Expression::make_string(token->string_value(), token->location());
2333 this->advance_token();
2336 case Token::TOKEN_INTEGER:
2337 ret = Expression::make_integer(token->integer_value(), NULL,
2339 this->advance_token();
2342 case Token::TOKEN_FLOAT:
2343 ret = Expression::make_float(token->float_value(), NULL,
2345 this->advance_token();
2348 case Token::TOKEN_IMAGINARY:
2351 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2352 ret = Expression::make_complex(&zero, token->imaginary_value(),
2353 NULL, token->location());
2355 this->advance_token();
2359 case Token::TOKEN_KEYWORD:
2360 switch (token->keyword())
2363 return this->function_lit();
2365 case KEYWORD_INTERFACE:
2367 case KEYWORD_STRUCT:
2369 source_location location = token->location();
2370 return Expression::make_type(this->type(), location);
2377 case Token::TOKEN_OPERATOR:
2378 if (token->is_op(OPERATOR_LPAREN))
2380 this->advance_token();
2381 ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2382 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2383 error_at(this->location(), "missing %<)%>");
2385 this->advance_token();
2388 else if (token->is_op(OPERATOR_LSQUARE))
2390 // Here we call array_type directly, as this is the only
2391 // case where an ellipsis is permitted for an array type.
2392 source_location location = token->location();
2393 return Expression::make_type(this->array_type(true), location);
2401 error_at(this->location(), "expected operand");
2402 return Expression::make_error(this->location());
2405 // Handle a reference to a variable in an enclosing function. We add
2406 // it to a list of such variables. We return a reference to a field
2407 // in a struct which will be passed on the static chain when calling
2408 // the current function.
2411 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2412 source_location location)
2414 go_assert(var->is_variable() || var->is_result_variable());
2416 Named_object* this_function = this->gogo_->current_function();
2417 Named_object* closure = this_function->func_value()->closure_var();
2419 Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2420 std::pair<Enclosing_vars::iterator, bool> ins =
2421 this->enclosing_vars_.insert(ev);
2424 // This is a variable we have not seen before. Add a new field
2425 // to the closure type.
2426 this_function->func_value()->add_closure_field(var, location);
2429 Expression* closure_ref = Expression::make_var_reference(closure,
2431 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2433 // The closure structure holds pointers to the variables, so we need
2434 // to introduce an indirection.
2435 Expression* e = Expression::make_field_reference(closure_ref,
2438 e = Expression::make_unary(OPERATOR_MULT, e, location);
2442 // CompositeLit = LiteralType LiteralValue .
2443 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2444 // SliceType | MapType | TypeName .
2445 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2446 // ElementList = Element { "," Element } .
2447 // Element = [ Key ":" ] Value .
2448 // Key = FieldName | ElementIndex .
2449 // FieldName = identifier .
2450 // ElementIndex = Expression .
2451 // Value = Expression | LiteralValue .
2453 // We have already seen the type if there is one, and we are now
2454 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2455 // will be seen here as an array type whose length is "nil". The
2456 // DEPTH parameter is non-zero if this is an embedded composite
2457 // literal and the type was omitted. It gives the number of steps up
2458 // to the type which was provided. E.g., in [][]int{{1}} it will be
2459 // 1. In [][][]int{{{1}}} it will be 2.
2462 Parse::composite_lit(Type* type, int depth, source_location location)
2464 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2465 this->advance_token();
2467 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2469 this->advance_token();
2470 return Expression::make_composite_literal(type, depth, false, NULL,
2474 bool has_keys = false;
2475 Expression_list* vals = new Expression_list;
2479 bool is_type_omitted = false;
2481 const Token* token = this->peek_token();
2483 if (token->is_identifier())
2485 std::string identifier = token->identifier();
2486 bool is_exported = token->is_identifier_exported();
2487 source_location location = token->location();
2489 if (this->advance_token()->is_op(OPERATOR_COLON))
2491 // This may be a field name. We don't know for sure--it
2492 // could also be an expression for an array index. We
2493 // don't want to parse it as an expression because may
2494 // trigger various errors, e.g., if this identifier
2495 // happens to be the name of a package.
2496 Gogo* gogo = this->gogo_;
2497 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2503 this->unget_token(Token::make_identifier_token(identifier,
2506 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2509 else if (!token->is_op(OPERATOR_LCURLY))
2510 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2513 // This must be a composite literal inside another composite
2514 // literal, with the type omitted for the inner one.
2515 val = this->composite_lit(type, depth + 1, token->location());
2516 is_type_omitted = true;
2519 token = this->peek_token();
2520 if (!token->is_op(OPERATOR_COLON))
2523 vals->push_back(NULL);
2527 if (is_type_omitted && !val->is_error_expression())
2529 error_at(this->location(), "unexpected %<:%>");
2530 val = Expression::make_error(this->location());
2533 this->advance_token();
2535 if (!has_keys && !vals->empty())
2537 Expression_list* newvals = new Expression_list;
2538 for (Expression_list::const_iterator p = vals->begin();
2542 newvals->push_back(NULL);
2543 newvals->push_back(*p);
2550 if (val->unknown_expression() != NULL)
2551 val->unknown_expression()->set_is_composite_literal_key();
2553 vals->push_back(val);
2555 if (!token->is_op(OPERATOR_LCURLY))
2556 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2559 // This must be a composite literal inside another
2560 // composite literal, with the type omitted for the
2562 val = this->composite_lit(type, depth + 1, token->location());
2565 token = this->peek_token();
2568 vals->push_back(val);
2570 if (token->is_op(OPERATOR_COMMA))
2572 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2574 this->advance_token();
2578 else if (token->is_op(OPERATOR_RCURLY))
2580 this->advance_token();
2585 error_at(this->location(), "expected %<,%> or %<}%>");
2588 while (!token->is_eof()
2589 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2591 if (token->is_op(OPERATOR_LCURLY))
2593 else if (token->is_op(OPERATOR_RCURLY))
2595 token = this->advance_token();
2597 if (token->is_op(OPERATOR_RCURLY))
2598 this->advance_token();
2600 return Expression::make_error(location);
2604 return Expression::make_composite_literal(type, depth, has_keys, vals,
2608 // FunctionLit = "func" Signature Block .
2611 Parse::function_lit()
2613 source_location location = this->location();
2614 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2615 this->advance_token();
2617 Enclosing_vars hold_enclosing_vars;
2618 hold_enclosing_vars.swap(this->enclosing_vars_);
2620 Function_type* type = this->signature(NULL, location);
2622 type = Type::make_function_type(NULL, NULL, NULL, location);
2624 // For a function literal, the next token must be a '{'. If we
2625 // don't see that, then we may have a type expression.
2626 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2627 return Expression::make_type(type, location);
2629 Bc_stack* hold_break_stack = this->break_stack_;
2630 Bc_stack* hold_continue_stack = this->continue_stack_;
2631 this->break_stack_ = NULL;
2632 this->continue_stack_ = NULL;
2634 Named_object* no = this->gogo_->start_function("", type, true, location);
2636 source_location end_loc = this->block();
2638 this->gogo_->finish_function(end_loc);
2640 if (this->break_stack_ != NULL)
2641 delete this->break_stack_;
2642 if (this->continue_stack_ != NULL)
2643 delete this->continue_stack_;
2644 this->break_stack_ = hold_break_stack;
2645 this->continue_stack_ = hold_continue_stack;
2647 hold_enclosing_vars.swap(this->enclosing_vars_);
2649 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2652 return Expression::make_func_reference(no, closure, location);
2655 // Create a closure for the nested function FUNCTION. This is based
2656 // on ENCLOSING_VARS, which is a list of all variables defined in
2657 // enclosing functions and referenced from FUNCTION. A closure is the
2658 // address of a struct which contains the addresses of all the
2659 // referenced variables. This returns NULL if no closure is required.
2662 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2663 source_location location)
2665 if (enclosing_vars->empty())
2668 // Get the variables in order by their field index.
2670 size_t enclosing_var_count = enclosing_vars->size();
2671 std::vector<Enclosing_var> ev(enclosing_var_count);
2672 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2673 p != enclosing_vars->end();
2675 ev[p->index()] = *p;
2677 // Build an initializer for a composite literal of the closure's
2680 Named_object* enclosing_function = this->gogo_->current_function();
2681 Expression_list* initializer = new Expression_list;
2682 for (size_t i = 0; i < enclosing_var_count; ++i)
2684 go_assert(ev[i].index() == i);
2685 Named_object* var = ev[i].var();
2687 if (ev[i].in_function() == enclosing_function)
2688 ref = Expression::make_var_reference(var, location);
2690 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2692 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2694 initializer->push_back(refaddr);
2697 Named_object* closure_var = function->func_value()->closure_var();
2698 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2699 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2701 return Expression::make_heap_composite(cv, location);
2704 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2706 // If MAY_BE_SINK is true, this expression may be "_".
2708 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2711 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2712 // guard (var := expr.("type") using the literal keyword "type").
2715 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2716 bool* is_type_switch)
2718 source_location start_loc = this->location();
2719 bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2721 Expression* ret = this->operand(may_be_sink);
2723 // An unknown name followed by a curly brace must be a composite
2724 // literal, and the unknown name must be a type.
2725 if (may_be_composite_lit
2726 && !is_parenthesized
2727 && ret->unknown_expression() != NULL
2728 && this->peek_token()->is_op(OPERATOR_LCURLY))
2730 Named_object* no = ret->unknown_expression()->named_object();
2731 Type* type = Type::make_forward_declaration(no);
2732 ret = Expression::make_type(type, ret->location());
2735 // We handle composite literals and type casts here, as it is the
2736 // easiest way to handle types which are in parentheses, as in
2738 if (ret->is_type_expression())
2740 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2742 if (is_parenthesized)
2744 "cannot parenthesize type in composite literal");
2745 ret = this->composite_lit(ret->type(), 0, ret->location());
2747 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2749 source_location loc = this->location();
2750 this->advance_token();
2751 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2753 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2755 error_at(this->location(),
2756 "invalid use of %<...%> in type conversion");
2757 this->advance_token();
2759 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2760 error_at(this->location(), "expected %<)%>");
2762 this->advance_token();
2763 if (expr->is_error_expression())
2767 Type* t = ret->type();
2768 if (t->classification() == Type::TYPE_ARRAY
2769 && t->array_type()->length() != NULL
2770 && t->array_type()->length()->is_nil_expression())
2772 error_at(ret->location(),
2773 "invalid use of %<...%> in type conversion");
2774 ret = Expression::make_error(loc);
2777 ret = Expression::make_cast(t, expr, loc);
2784 const Token* token = this->peek_token();
2785 if (token->is_op(OPERATOR_LPAREN))
2786 ret = this->call(this->verify_not_sink(ret));
2787 else if (token->is_op(OPERATOR_DOT))
2789 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2790 if (is_type_switch != NULL && *is_type_switch)
2793 else if (token->is_op(OPERATOR_LSQUARE))
2794 ret = this->index(this->verify_not_sink(ret));
2802 // Selector = "." identifier .
2803 // TypeGuard = "." "(" QualifiedIdent ")" .
2805 // Note that Operand can expand to QualifiedIdent, which contains a
2806 // ".". That is handled directly in operand when it sees a package
2809 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2810 // guard (var := expr.("type") using the literal keyword "type").
2813 Parse::selector(Expression* left, bool* is_type_switch)
2815 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
2816 source_location location = this->location();
2818 const Token* token = this->advance_token();
2819 if (token->is_identifier())
2821 // This could be a field in a struct, or a method in an
2822 // interface, or a method associated with a type. We can't know
2823 // which until we have seen all the types.
2825 this->gogo_->pack_hidden_name(token->identifier(),
2826 token->is_identifier_exported());
2827 if (token->identifier() == "_")
2829 error_at(this->location(), "invalid use of %<_%>");
2830 name = this->gogo_->pack_hidden_name("blank", false);
2832 this->advance_token();
2833 return Expression::make_selector(left, name, location);
2835 else if (token->is_op(OPERATOR_LPAREN))
2837 this->advance_token();
2839 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
2840 type = this->type();
2843 if (is_type_switch != NULL)
2844 *is_type_switch = true;
2847 error_at(this->location(),
2848 "use of %<.(type)%> outside type switch");
2849 type = Type::make_error_type();
2851 this->advance_token();
2853 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2854 error_at(this->location(), "missing %<)%>");
2856 this->advance_token();
2857 if (is_type_switch != NULL && *is_type_switch)
2859 return Expression::make_type_guard(left, type, location);
2863 error_at(this->location(), "expected identifier or %<(%>");
2868 // Index = "[" Expression "]" .
2869 // Slice = "[" Expression ":" [ Expression ] "]" .
2872 Parse::index(Expression* expr)
2874 source_location location = this->location();
2875 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
2876 this->advance_token();
2879 if (!this->peek_token()->is_op(OPERATOR_COLON))
2880 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2884 mpz_init_set_ui(zero, 0);
2885 start = Expression::make_integer(&zero, NULL, location);
2889 Expression* end = NULL;
2890 if (this->peek_token()->is_op(OPERATOR_COLON))
2892 // We use nil to indicate a missing high expression.
2893 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
2894 end = Expression::make_nil(this->location());
2896 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2898 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
2899 error_at(this->location(), "missing %<]%>");
2901 this->advance_token();
2902 return Expression::make_index(expr, start, end, location);
2905 // Call = "(" [ ArgumentList [ "," ] ] ")" .
2906 // ArgumentList = ExpressionList [ "..." ] .
2909 Parse::call(Expression* func)
2911 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2912 Expression_list* args = NULL;
2913 bool is_varargs = false;
2914 const Token* token = this->advance_token();
2915 if (!token->is_op(OPERATOR_RPAREN))
2917 args = this->expression_list(NULL, false);
2918 token = this->peek_token();
2919 if (token->is_op(OPERATOR_ELLIPSIS))
2922 token = this->advance_token();
2925 if (token->is_op(OPERATOR_COMMA))
2926 token = this->advance_token();
2927 if (!token->is_op(OPERATOR_RPAREN))
2928 error_at(this->location(), "missing %<)%>");
2930 this->advance_token();
2931 if (func->is_error_expression())
2933 return Expression::make_call(func, args, is_varargs, func->location());
2936 // Return an expression for a single unqualified identifier.
2939 Parse::id_to_expression(const std::string& name, source_location location)
2941 Named_object* in_function;
2942 Named_object* named_object = this->gogo_->lookup(name, &in_function);
2943 if (named_object == NULL)
2944 named_object = this->gogo_->add_unknown_name(name, location);
2946 if (in_function != NULL
2947 && in_function != this->gogo_->current_function()
2948 && (named_object->is_variable() || named_object->is_result_variable()))
2949 return this->enclosing_var_reference(in_function, named_object,
2952 switch (named_object->classification())
2954 case Named_object::NAMED_OBJECT_CONST:
2955 return Expression::make_const_reference(named_object, location);
2956 case Named_object::NAMED_OBJECT_VAR:
2957 case Named_object::NAMED_OBJECT_RESULT_VAR:
2958 return Expression::make_var_reference(named_object, location);
2959 case Named_object::NAMED_OBJECT_SINK:
2960 return Expression::make_sink(location);
2961 case Named_object::NAMED_OBJECT_FUNC:
2962 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2963 return Expression::make_func_reference(named_object, NULL, location);
2964 case Named_object::NAMED_OBJECT_UNKNOWN:
2965 return Expression::make_unknown_reference(named_object, location);
2966 case Named_object::NAMED_OBJECT_PACKAGE:
2967 case Named_object::NAMED_OBJECT_TYPE:
2968 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2969 // These cases can arise for a field name in a composite
2971 return Expression::make_unknown_reference(named_object, location);
2973 error_at(this->location(), "unexpected type of identifier");
2974 return Expression::make_error(location);
2978 // Expression = UnaryExpr { binary_op Expression } .
2980 // PRECEDENCE is the precedence of the current operator.
2982 // If MAY_BE_SINK is true, this expression may be "_".
2984 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2987 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2988 // guard (var := expr.("type") using the literal keyword "type").
2991 Parse::expression(Precedence precedence, bool may_be_sink,
2992 bool may_be_composite_lit, bool* is_type_switch)
2994 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
2999 if (is_type_switch != NULL && *is_type_switch)
3002 const Token* token = this->peek_token();
3003 if (token->classification() != Token::TOKEN_OPERATOR)
3009 Precedence right_precedence;
3010 switch (token->op())
3013 right_precedence = PRECEDENCE_OROR;
3015 case OPERATOR_ANDAND:
3016 right_precedence = PRECEDENCE_ANDAND;
3019 case OPERATOR_NOTEQ:
3024 right_precedence = PRECEDENCE_RELOP;
3027 case OPERATOR_MINUS:
3030 right_precedence = PRECEDENCE_ADDOP;
3035 case OPERATOR_LSHIFT:
3036 case OPERATOR_RSHIFT:
3038 case OPERATOR_BITCLEAR:
3039 right_precedence = PRECEDENCE_MULOP;
3042 right_precedence = PRECEDENCE_INVALID;
3046 if (right_precedence == PRECEDENCE_INVALID)
3052 Operator op = token->op();
3053 source_location binop_location = token->location();
3055 if (precedence >= right_precedence)
3057 // We've already seen A * B, and we see + C. We want to
3058 // return so that A * B becomes a group.
3062 this->advance_token();
3064 left = this->verify_not_sink(left);
3065 Expression* right = this->expression(right_precedence, false,
3066 may_be_composite_lit,
3068 left = Expression::make_binary(op, left, right, binop_location);
3073 Parse::expression_may_start_here()
3075 const Token* token = this->peek_token();
3076 switch (token->classification())
3078 case Token::TOKEN_INVALID:
3079 case Token::TOKEN_EOF:
3081 case Token::TOKEN_KEYWORD:
3082 switch (token->keyword())
3087 case KEYWORD_STRUCT:
3088 case KEYWORD_INTERFACE:
3093 case Token::TOKEN_IDENTIFIER:
3095 case Token::TOKEN_STRING:
3097 case Token::TOKEN_OPERATOR:
3098 switch (token->op())
3101 case OPERATOR_MINUS:
3105 case OPERATOR_CHANOP:
3107 case OPERATOR_LPAREN:
3108 case OPERATOR_LSQUARE:
3113 case Token::TOKEN_INTEGER:
3114 case Token::TOKEN_FLOAT:
3115 case Token::TOKEN_IMAGINARY:
3122 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3124 // If MAY_BE_SINK is true, this expression may be "_".
3126 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3129 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3130 // guard (var := expr.("type") using the literal keyword "type").
3133 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3134 bool* is_type_switch)
3136 const Token* token = this->peek_token();
3137 if (token->is_op(OPERATOR_PLUS)
3138 || token->is_op(OPERATOR_MINUS)
3139 || token->is_op(OPERATOR_NOT)
3140 || token->is_op(OPERATOR_XOR)
3141 || token->is_op(OPERATOR_CHANOP)
3142 || token->is_op(OPERATOR_MULT)
3143 || token->is_op(OPERATOR_AND))
3145 source_location location = token->location();
3146 Operator op = token->op();
3147 this->advance_token();
3149 if (op == OPERATOR_CHANOP
3150 && this->peek_token()->is_keyword(KEYWORD_CHAN))
3152 // This is "<- chan" which must be the start of a type.
3153 this->unget_token(Token::make_operator_token(op, location));
3154 return Expression::make_type(this->type(), location);
3157 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
3158 if (expr->is_error_expression())
3160 else if (op == OPERATOR_MULT && expr->is_type_expression())
3161 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3163 else if (op == OPERATOR_AND && expr->is_composite_literal())
3164 expr = Expression::make_heap_composite(expr, location);
3165 else if (op != OPERATOR_CHANOP)
3166 expr = Expression::make_unary(op, expr, location);
3168 expr = Expression::make_receive(expr, location);
3172 return this->primary_expr(may_be_sink, may_be_composite_lit,
3177 // Declaration | LabeledStmt | SimpleStmt |
3178 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3179 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3182 // LABEL is the label of this statement if it has one.
3185 Parse::statement(Label* label)
3187 const Token* token = this->peek_token();
3188 switch (token->classification())
3190 case Token::TOKEN_KEYWORD:
3192 switch (token->keyword())
3197 this->declaration();
3201 case KEYWORD_STRUCT:
3202 case KEYWORD_INTERFACE:
3203 this->simple_stat(true, NULL, NULL, NULL);
3207 this->go_or_defer_stat();
3209 case KEYWORD_RETURN:
3210 this->return_stat();
3215 case KEYWORD_CONTINUE:
3216 this->continue_stat();
3224 case KEYWORD_SWITCH:
3225 this->switch_stat(label);
3227 case KEYWORD_SELECT:
3228 this->select_stat(label);
3231 this->for_stat(label);
3234 error_at(this->location(), "expected statement");
3235 this->advance_token();
3241 case Token::TOKEN_IDENTIFIER:
3243 std::string identifier = token->identifier();
3244 bool is_exported = token->is_identifier_exported();
3245 source_location location = token->location();
3246 if (this->advance_token()->is_op(OPERATOR_COLON))
3248 this->advance_token();
3249 this->labeled_stmt(identifier, location);
3253 this->unget_token(Token::make_identifier_token(identifier,
3256 this->simple_stat(true, NULL, NULL, NULL);
3261 case Token::TOKEN_OPERATOR:
3262 if (token->is_op(OPERATOR_LCURLY))
3264 source_location location = token->location();
3265 this->gogo_->start_block(location);
3266 source_location end_loc = this->block();
3267 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3270 else if (!token->is_op(OPERATOR_SEMICOLON))
3271 this->simple_stat(true, NULL, NULL, NULL);
3274 case Token::TOKEN_STRING:
3275 case Token::TOKEN_INTEGER:
3276 case Token::TOKEN_FLOAT:
3277 case Token::TOKEN_IMAGINARY:
3278 this->simple_stat(true, NULL, NULL, NULL);
3282 error_at(this->location(), "expected statement");
3283 this->advance_token();
3289 Parse::statement_may_start_here()
3291 const Token* token = this->peek_token();
3292 switch (token->classification())
3294 case Token::TOKEN_KEYWORD:
3296 switch (token->keyword())
3303 case KEYWORD_STRUCT:
3304 case KEYWORD_INTERFACE:
3307 case KEYWORD_RETURN:
3309 case KEYWORD_CONTINUE:
3312 case KEYWORD_SWITCH:
3313 case KEYWORD_SELECT:
3323 case Token::TOKEN_IDENTIFIER:
3326 case Token::TOKEN_OPERATOR:
3327 if (token->is_op(OPERATOR_LCURLY)
3328 || token->is_op(OPERATOR_SEMICOLON))
3331 return this->expression_may_start_here();
3333 case Token::TOKEN_STRING:
3334 case Token::TOKEN_INTEGER:
3335 case Token::TOKEN_FLOAT:
3336 case Token::TOKEN_IMAGINARY:
3344 // LabeledStmt = Label ":" Statement .
3345 // Label = identifier .
3348 Parse::labeled_stmt(const std::string& label_name, source_location location)
3350 Label* label = this->gogo_->add_label_definition(label_name, location);
3352 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3354 // This is a label at the end of a block. A program is
3355 // permitted to omit a semicolon here.
3359 if (!this->statement_may_start_here())
3361 // Mark the label as used to avoid a useless error about an
3363 label->set_is_used();
3365 error_at(location, "missing statement after label");
3366 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3371 this->statement(label);
3374 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3375 // Assignment | ShortVarDecl .
3377 // EmptyStmt was handled in Parse::statement.
3379 // In order to make this work for if and switch statements, if
3380 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3381 // expression rather than adding an expression statement to the
3382 // current block. If we see something other than an ExpressionStat,
3383 // we add the statement, set *RETURN_EXP to true if we saw a send
3384 // statement, and return NULL. The handling of send statements is for
3385 // better error messages.
3387 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3390 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3391 // guard (var := expr.("type") using the literal keyword "type").
3394 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3395 Range_clause* p_range_clause, Type_switch* p_type_switch)
3397 const Token* token = this->peek_token();
3399 // An identifier follow by := is a SimpleVarDecl.
3400 if (token->is_identifier())
3402 std::string identifier = token->identifier();
3403 bool is_exported = token->is_identifier_exported();
3404 source_location location = token->location();
3406 token = this->advance_token();
3407 if (token->is_op(OPERATOR_COLONEQ)
3408 || token->is_op(OPERATOR_COMMA))
3410 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3411 this->simple_var_decl_or_assignment(identifier, location,
3413 (token->is_op(OPERATOR_COLONEQ)
3419 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3423 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3424 may_be_composite_lit,
3425 (p_type_switch == NULL
3427 : &p_type_switch->found));
3428 if (p_type_switch != NULL && p_type_switch->found)
3430 p_type_switch->name.clear();
3431 p_type_switch->location = exp->location();
3432 p_type_switch->expr = this->verify_not_sink(exp);
3435 token = this->peek_token();
3436 if (token->is_op(OPERATOR_CHANOP))
3438 this->send_stmt(this->verify_not_sink(exp));
3439 if (return_exp != NULL)
3442 else if (token->is_op(OPERATOR_PLUSPLUS)
3443 || token->is_op(OPERATOR_MINUSMINUS))
3444 this->inc_dec_stat(this->verify_not_sink(exp));
3445 else if (token->is_op(OPERATOR_COMMA)
3446 || token->is_op(OPERATOR_EQ))
3447 this->assignment(exp, p_range_clause);
3448 else if (token->is_op(OPERATOR_PLUSEQ)
3449 || token->is_op(OPERATOR_MINUSEQ)
3450 || token->is_op(OPERATOR_OREQ)
3451 || token->is_op(OPERATOR_XOREQ)
3452 || token->is_op(OPERATOR_MULTEQ)
3453 || token->is_op(OPERATOR_DIVEQ)
3454 || token->is_op(OPERATOR_MODEQ)
3455 || token->is_op(OPERATOR_LSHIFTEQ)
3456 || token->is_op(OPERATOR_RSHIFTEQ)
3457 || token->is_op(OPERATOR_ANDEQ)
3458 || token->is_op(OPERATOR_BITCLEAREQ))
3459 this->assignment(this->verify_not_sink(exp), p_range_clause);
3460 else if (return_exp != NULL)
3461 return this->verify_not_sink(exp);
3463 this->expression_stat(this->verify_not_sink(exp));
3469 Parse::simple_stat_may_start_here()
3471 return this->expression_may_start_here();
3474 // Parse { Statement ";" } which is used in a few places. The list of
3475 // statements may end with a right curly brace, in which case the
3476 // semicolon may be omitted.
3479 Parse::statement_list()
3481 while (this->statement_may_start_here())
3483 this->statement(NULL);
3484 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3485 this->advance_token();
3486 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3490 if (!this->peek_token()->is_eof() || !saw_errors())
3491 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3492 if (!this->skip_past_error(OPERATOR_RCURLY))
3499 Parse::statement_list_may_start_here()
3501 return this->statement_may_start_here();
3504 // ExpressionStat = Expression .
3507 Parse::expression_stat(Expression* exp)
3509 exp->discarding_value();
3510 this->gogo_->add_statement(Statement::make_statement(exp));
3513 // SendStmt = Channel "<-" Expression .
3514 // Channel = Expression .
3517 Parse::send_stmt(Expression* channel)
3519 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3520 source_location loc = this->location();
3521 this->advance_token();
3522 Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3523 Statement* s = Statement::make_send_statement(channel, val, loc);
3524 this->gogo_->add_statement(s);
3527 // IncDecStat = Expression ( "++" | "--" ) .
3530 Parse::inc_dec_stat(Expression* exp)
3532 const Token* token = this->peek_token();
3534 // Lvalue maps require special handling.
3535 if (exp->index_expression() != NULL)
3536 exp->index_expression()->set_is_lvalue();
3538 if (token->is_op(OPERATOR_PLUSPLUS))
3539 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3540 else if (token->is_op(OPERATOR_MINUSMINUS))
3541 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3544 this->advance_token();
3547 // Assignment = ExpressionList assign_op ExpressionList .
3549 // EXP is an expression that we have already parsed.
3551 // If RANGE_CLAUSE is not NULL, then this will recognize a
3555 Parse::assignment(Expression* expr, Range_clause* p_range_clause)
3557 Expression_list* vars;
3558 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3560 vars = new Expression_list();
3561 vars->push_back(expr);
3565 this->advance_token();
3566 vars = this->expression_list(expr, true);
3569 this->tuple_assignment(vars, p_range_clause);
3572 // An assignment statement. LHS is the list of expressions which
3573 // appear on the left hand side.
3575 // If RANGE_CLAUSE is not NULL, then this will recognize a
3579 Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
3581 const Token* token = this->peek_token();
3582 if (!token->is_op(OPERATOR_EQ)
3583 && !token->is_op(OPERATOR_PLUSEQ)
3584 && !token->is_op(OPERATOR_MINUSEQ)
3585 && !token->is_op(OPERATOR_OREQ)
3586 && !token->is_op(OPERATOR_XOREQ)
3587 && !token->is_op(OPERATOR_MULTEQ)
3588 && !token->is_op(OPERATOR_DIVEQ)
3589 && !token->is_op(OPERATOR_MODEQ)
3590 && !token->is_op(OPERATOR_LSHIFTEQ)
3591 && !token->is_op(OPERATOR_RSHIFTEQ)
3592 && !token->is_op(OPERATOR_ANDEQ)
3593 && !token->is_op(OPERATOR_BITCLEAREQ))
3595 error_at(this->location(), "expected assignment operator");
3598 Operator op = token->op();
3599 source_location location = token->location();
3601 token = this->advance_token();
3603 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
3605 if (op != OPERATOR_EQ)
3606 error_at(this->location(), "range clause requires %<=%>");
3607 this->range_clause_expr(lhs, p_range_clause);
3611 Expression_list* vals = this->expression_list(NULL, false);
3613 // We've parsed everything; check for errors.
3614 if (lhs == NULL || vals == NULL)
3616 for (Expression_list::const_iterator pe = lhs->begin();
3620 if ((*pe)->is_error_expression())
3622 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
3623 error_at((*pe)->location(), "cannot use _ as value");
3625 for (Expression_list::const_iterator pe = vals->begin();
3629 if ((*pe)->is_error_expression())
3633 // Map expressions act differently when they are lvalues.
3634 for (Expression_list::iterator plv = lhs->begin();
3637 if ((*plv)->index_expression() != NULL)
3638 (*plv)->index_expression()->set_is_lvalue();
3640 Call_expression* call;
3641 Index_expression* map_index;
3642 Receive_expression* receive;
3643 Type_guard_expression* type_guard;
3644 if (lhs->size() == vals->size())
3647 if (lhs->size() > 1)
3649 if (op != OPERATOR_EQ)
3650 error_at(location, "multiple values only permitted with %<=%>");
3651 s = Statement::make_tuple_assignment(lhs, vals, location);
3655 if (op == OPERATOR_EQ)
3656 s = Statement::make_assignment(lhs->front(), vals->front(),
3659 s = Statement::make_assignment_operation(op, lhs->front(),
3660 vals->front(), location);
3664 this->gogo_->add_statement(s);
3666 else if (vals->size() == 1
3667 && (call = (*vals->begin())->call_expression()) != NULL)
3669 if (op != OPERATOR_EQ)
3670 error_at(location, "multiple results only permitted with %<=%>");
3672 vals = new Expression_list;
3673 for (unsigned int i = 0; i < lhs->size(); ++i)
3674 vals->push_back(Expression::make_call_result(call, i));
3675 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
3676 this->gogo_->add_statement(s);
3678 else if (lhs->size() == 2
3679 && vals->size() == 1
3680 && (map_index = (*vals->begin())->index_expression()) != NULL)
3682 if (op != OPERATOR_EQ)
3683 error_at(location, "two values from map requires %<=%>");
3684 Expression* val = lhs->front();
3685 Expression* present = lhs->back();
3686 Statement* s = Statement::make_tuple_map_assignment(val, present,
3687 map_index, location);
3688 this->gogo_->add_statement(s);
3690 else if (lhs->size() == 1
3691 && vals->size() == 2
3692 && (map_index = lhs->front()->index_expression()) != NULL)
3694 if (op != OPERATOR_EQ)
3695 error_at(location, "assigning tuple to map index requires %<=%>");
3696 Expression* val = vals->front();
3697 Expression* should_set = vals->back();
3698 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
3700 this->gogo_->add_statement(s);
3702 else if (lhs->size() == 2
3703 && vals->size() == 1
3704 && (receive = (*vals->begin())->receive_expression()) != NULL)
3706 if (op != OPERATOR_EQ)
3707 error_at(location, "two values from receive requires %<=%>");
3708 Expression* val = lhs->front();
3709 Expression* success = lhs->back();
3710 Expression* channel = receive->channel();
3711 Statement* s = Statement::make_tuple_receive_assignment(val, success,
3715 this->gogo_->add_statement(s);
3717 else if (lhs->size() == 2
3718 && vals->size() == 1
3719 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
3721 if (op != OPERATOR_EQ)
3722 error_at(location, "two values from type guard requires %<=%>");
3723 Expression* val = lhs->front();
3724 Expression* ok = lhs->back();
3725 Expression* expr = type_guard->expr();
3726 Type* type = type_guard->type();
3727 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
3730 this->gogo_->add_statement(s);
3734 error_at(location, "number of variables does not match number of values");
3738 // GoStat = "go" Expression .
3739 // DeferStat = "defer" Expression .
3742 Parse::go_or_defer_stat()
3744 go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
3745 || this->peek_token()->is_keyword(KEYWORD_DEFER));
3746 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
3747 source_location stat_location = this->location();
3748 this->advance_token();
3749 source_location expr_location = this->location();
3750 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3751 Call_expression* call_expr = expr->call_expression();
3752 if (call_expr == NULL)
3754 error_at(expr_location, "expected call expression");
3758 // Make it easier to simplify go/defer statements by putting every
3759 // statement in its own block.
3760 this->gogo_->start_block(stat_location);
3763 stat = Statement::make_go_statement(call_expr, stat_location);
3765 stat = Statement::make_defer_statement(call_expr, stat_location);
3766 this->gogo_->add_statement(stat);
3767 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
3771 // ReturnStat = "return" [ ExpressionList ] .
3774 Parse::return_stat()
3776 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
3777 source_location location = this->location();
3778 this->advance_token();
3779 Expression_list* vals = NULL;
3780 if (this->expression_may_start_here())
3781 vals = this->expression_list(NULL, false);
3782 this->gogo_->add_statement(Statement::make_return_statement(vals, location));
3785 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" Statement ] .
3790 go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
3791 source_location location = this->location();
3792 this->advance_token();
3794 this->gogo_->start_block(location);
3796 bool saw_simple_stat = false;
3797 Expression* cond = NULL;
3799 if (this->simple_stat_may_start_here())
3801 cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
3802 saw_simple_stat = true;
3804 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3806 // The SimpleStat is an expression statement.
3807 this->expression_stat(cond);
3812 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3813 this->advance_token();
3814 else if (saw_simple_stat)
3817 error_at(this->location(),
3818 ("send statement used as value; "
3819 "use select for non-blocking send"));
3821 error_at(this->location(),
3822 "expected %<;%> after statement in if expression");
3823 if (!this->expression_may_start_here())
3824 cond = Expression::make_error(this->location());
3826 if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
3828 error_at(this->location(),
3829 "missing condition in if statement");
3830 cond = Expression::make_error(this->location());
3833 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
3836 this->gogo_->start_block(this->location());
3837 source_location end_loc = this->block();
3838 Block* then_block = this->gogo_->finish_block(end_loc);
3840 // Check for the easy error of a newline before "else".
3841 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3843 source_location semi_loc = this->location();
3844 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
3845 error_at(this->location(),
3846 "unexpected semicolon or newline before %<else%>");
3848 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3852 Block* else_block = NULL;
3853 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
3855 this->advance_token();
3856 // We create a block to gather the statement.
3857 this->gogo_->start_block(this->location());
3858 this->statement(NULL);
3859 else_block = this->gogo_->finish_block(this->location());
3862 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
3866 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3870 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
3871 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
3872 // "{" { ExprCaseClause } "}" .
3873 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
3874 // "{" { TypeCaseClause } "}" .
3875 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
3878 Parse::switch_stat(Label* label)
3880 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
3881 source_location location = this->location();
3882 this->advance_token();
3884 this->gogo_->start_block(location);
3886 bool saw_simple_stat = false;
3887 Expression* switch_val = NULL;
3889 Type_switch type_switch;
3890 if (this->simple_stat_may_start_here())
3892 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
3894 saw_simple_stat = true;
3896 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3898 // The SimpleStat is an expression statement.
3899 this->expression_stat(switch_val);
3902 if (switch_val == NULL && !type_switch.found)
3904 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3905 this->advance_token();
3906 else if (saw_simple_stat)
3909 error_at(this->location(),
3910 ("send statement used as value; "
3911 "use select for non-blocking send"));
3913 error_at(this->location(),
3914 "expected %<;%> after statement in switch expression");
3916 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3918 if (this->peek_token()->is_identifier())
3920 const Token* token = this->peek_token();
3921 std::string identifier = token->identifier();
3922 bool is_exported = token->is_identifier_exported();
3923 source_location id_loc = token->location();
3925 token = this->advance_token();
3926 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
3927 this->unget_token(Token::make_identifier_token(identifier,
3932 // This must be a TypeSwitchGuard.
3933 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
3935 if (!type_switch.found)
3937 if (switch_val == NULL
3938 || !switch_val->is_error_expression())
3940 error_at(id_loc, "expected type switch assignment");
3941 switch_val = Expression::make_error(id_loc);
3946 if (switch_val == NULL && !type_switch.found)
3948 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
3949 &type_switch.found);
3950 if (type_switch.found)
3952 type_switch.name.clear();
3953 type_switch.expr = switch_val;
3954 type_switch.location = switch_val->location();
3960 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3962 source_location token_loc = this->location();
3963 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
3964 && this->advance_token()->is_op(OPERATOR_LCURLY))
3965 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
3966 else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
3968 error_at(token_loc, "invalid variable name");
3969 this->advance_token();
3970 this->expression(PRECEDENCE_NORMAL, false, false,
3971 &type_switch.found);
3972 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3973 this->advance_token();
3974 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3976 if (type_switch.found)
3977 type_switch.expr = Expression::make_error(location);
3981 error_at(this->location(), "expected %<{%>");
3982 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3987 this->advance_token();
3989 Statement* statement;
3990 if (type_switch.found)
3991 statement = this->type_switch_body(label, type_switch, location);
3993 statement = this->expr_switch_body(label, switch_val, location);
3995 if (statement != NULL)
3996 this->gogo_->add_statement(statement);
3998 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4002 // The body of an expression switch.
4003 // "{" { ExprCaseClause } "}"
4006 Parse::expr_switch_body(Label* label, Expression* switch_val,
4007 source_location location)
4009 Switch_statement* statement = Statement::make_switch_statement(switch_val,
4012 this->push_break_statement(statement, label);
4014 Case_clauses* case_clauses = new Case_clauses();
4015 bool saw_default = false;
4016 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4018 if (this->peek_token()->is_eof())
4021 error_at(this->location(), "missing %<}%>");
4024 this->expr_case_clause(case_clauses, &saw_default);
4026 this->advance_token();
4028 statement->add_clauses(case_clauses);
4030 this->pop_break_statement();
4035 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4036 // FallthroughStat = "fallthrough" .
4039 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
4041 source_location location = this->location();
4043 bool is_default = false;
4044 Expression_list* vals = this->expr_switch_case(&is_default);
4046 if (!this->peek_token()->is_op(OPERATOR_COLON))
4049 error_at(this->location(), "expected %<:%>");
4053 this->advance_token();
4055 Block* statements = NULL;
4056 if (this->statement_list_may_start_here())
4058 this->gogo_->start_block(this->location());
4059 this->statement_list();
4060 statements = this->gogo_->finish_block(this->location());
4063 bool is_fallthrough = false;
4064 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4066 is_fallthrough = true;
4067 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4068 this->advance_token();
4075 error_at(location, "multiple defaults in switch");
4078 *saw_default = true;
4081 if (is_default || vals != NULL)
4082 clauses->add(vals, is_default, statements, is_fallthrough, location);
4085 // ExprSwitchCase = "case" ExpressionList | "default" .
4088 Parse::expr_switch_case(bool* is_default)
4090 const Token* token = this->peek_token();
4091 if (token->is_keyword(KEYWORD_CASE))
4093 this->advance_token();
4094 return this->expression_list(NULL, false);
4096 else if (token->is_keyword(KEYWORD_DEFAULT))
4098 this->advance_token();
4105 error_at(this->location(), "expected %<case%> or %<default%>");
4106 if (!token->is_op(OPERATOR_RCURLY))
4107 this->advance_token();
4112 // The body of a type switch.
4113 // "{" { TypeCaseClause } "}" .
4116 Parse::type_switch_body(Label* label, const Type_switch& type_switch,
4117 source_location location)
4119 Named_object* switch_no = NULL;
4120 if (!type_switch.name.empty())
4122 Variable* switch_var = new Variable(NULL, type_switch.expr, false, false,
4123 false, type_switch.location);
4124 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
4127 Type_switch_statement* statement =
4128 Statement::make_type_switch_statement(switch_no,
4134 this->push_break_statement(statement, label);
4136 Type_case_clauses* case_clauses = new Type_case_clauses();
4137 bool saw_default = false;
4138 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4140 if (this->peek_token()->is_eof())
4142 error_at(this->location(), "missing %<}%>");
4145 this->type_case_clause(switch_no, case_clauses, &saw_default);
4147 this->advance_token();
4149 statement->add_clauses(case_clauses);
4151 this->pop_break_statement();
4156 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4159 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4162 source_location location = this->location();
4164 std::vector<Type*> types;
4165 bool is_default = false;
4166 this->type_switch_case(&types, &is_default);
4168 if (!this->peek_token()->is_op(OPERATOR_COLON))
4169 error_at(this->location(), "expected %<:%>");
4171 this->advance_token();
4173 Block* statements = NULL;
4174 if (this->statement_list_may_start_here())
4176 this->gogo_->start_block(this->location());
4177 if (switch_no != NULL && types.size() == 1)
4179 Type* type = types.front();
4180 Expression* init = Expression::make_var_reference(switch_no,
4182 init = Expression::make_type_guard(init, type, location);
4183 Variable* v = new Variable(type, init, false, false, false,
4185 v->set_is_type_switch_var();
4186 this->gogo_->add_variable(switch_no->name(), v);
4188 this->statement_list();
4189 statements = this->gogo_->finish_block(this->location());
4192 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4194 error_at(this->location(),
4195 "fallthrough is not permitted in a type switch");
4196 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4197 this->advance_token();
4202 go_assert(types.empty());
4205 error_at(location, "multiple defaults in type switch");
4208 *saw_default = true;
4209 clauses->add(NULL, false, true, statements, location);
4211 else if (!types.empty())
4213 for (std::vector<Type*>::const_iterator p = types.begin();
4214 p + 1 != types.end();
4216 clauses->add(*p, true, false, NULL, location);
4217 clauses->add(types.back(), false, false, statements, location);
4220 clauses->add(Type::make_error_type(), false, false, statements, location);
4223 // TypeSwitchCase = "case" type | "default"
4225 // We accept a comma separated list of types.
4228 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4230 const Token* token = this->peek_token();
4231 if (token->is_keyword(KEYWORD_CASE))
4233 this->advance_token();
4236 Type* t = this->type();
4237 if (!t->is_error_type())
4238 types->push_back(t);
4239 if (!this->peek_token()->is_op(OPERATOR_COMMA))
4241 this->advance_token();
4244 else if (token->is_keyword(KEYWORD_DEFAULT))
4246 this->advance_token();
4251 error_at(this->location(), "expected %<case%> or %<default%>");
4252 if (!token->is_op(OPERATOR_RCURLY))
4253 this->advance_token();
4257 // SelectStat = "select" "{" { CommClause } "}" .
4260 Parse::select_stat(Label* label)
4262 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4263 source_location location = this->location();
4264 const Token* token = this->advance_token();
4266 if (!token->is_op(OPERATOR_LCURLY))
4268 source_location token_loc = token->location();
4269 if (token->is_op(OPERATOR_SEMICOLON)
4270 && this->advance_token()->is_op(OPERATOR_LCURLY))
4271 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4274 error_at(this->location(), "expected %<{%>");
4278 this->advance_token();
4280 Select_statement* statement = Statement::make_select_statement(location);
4282 this->push_break_statement(statement, label);
4284 Select_clauses* select_clauses = new Select_clauses();
4285 bool saw_default = false;
4286 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4288 if (this->peek_token()->is_eof())
4290 error_at(this->location(), "expected %<}%>");
4293 this->comm_clause(select_clauses, &saw_default);
4296 this->advance_token();
4298 statement->add_clauses(select_clauses);
4300 this->pop_break_statement();
4302 this->gogo_->add_statement(statement);
4305 // CommClause = CommCase ":" { Statement ";" } .
4308 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4310 source_location location = this->location();
4311 bool is_send = false;
4312 Expression* channel = NULL;
4313 Expression* val = NULL;
4314 Expression* closed = NULL;
4315 std::string varname;
4316 std::string closedname;
4317 bool is_default = false;
4318 bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
4319 &varname, &closedname, &is_default);
4323 && closedname.empty()
4325 && val->index_expression() != NULL)
4326 val->index_expression()->set_is_lvalue();
4328 if (this->peek_token()->is_op(OPERATOR_COLON))
4329 this->advance_token();
4331 error_at(this->location(), "expected colon");
4333 this->gogo_->start_block(this->location());
4335 Named_object* var = NULL;
4336 if (!varname.empty())
4338 // FIXME: LOCATION is slightly wrong here.
4339 Variable* v = new Variable(NULL, channel, false, false, false,
4341 v->set_type_from_chan_element();
4342 var = this->gogo_->add_variable(varname, v);
4345 Named_object* closedvar = NULL;
4346 if (!closedname.empty())
4348 // FIXME: LOCATION is slightly wrong here.
4349 Variable* v = new Variable(Type::lookup_bool_type(), NULL,
4350 false, false, false, location);
4351 closedvar = this->gogo_->add_variable(closedname, v);
4354 this->statement_list();
4356 Block* statements = this->gogo_->finish_block(this->location());
4362 error_at(location, "multiple defaults in select");
4365 *saw_default = true;
4369 clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
4370 statements, location);
4371 else if (statements != NULL)
4373 // Add the statements to make sure that any names they define
4375 this->gogo_->add_block(statements, location);
4379 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4382 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4383 Expression** closed, std::string* varname,
4384 std::string* closedname, bool* is_default)
4386 const Token* token = this->peek_token();
4387 if (token->is_keyword(KEYWORD_DEFAULT))
4389 this->advance_token();
4392 else if (token->is_keyword(KEYWORD_CASE))
4394 this->advance_token();
4395 if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
4401 error_at(this->location(), "expected %<case%> or %<default%>");
4402 if (!token->is_op(OPERATOR_RCURLY))
4403 this->advance_token();
4410 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4411 // RecvExpr = Expression .
4414 Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
4415 Expression** closed, std::string* varname,
4416 std::string* closedname)
4418 const Token* token = this->peek_token();
4419 bool saw_comma = false;
4420 bool closed_is_id = false;
4421 if (token->is_identifier())
4423 Gogo* gogo = this->gogo_;
4424 std::string recv_var = token->identifier();
4425 bool is_rv_exported = token->is_identifier_exported();
4426 source_location recv_var_loc = token->location();
4427 token = this->advance_token();
4428 if (token->is_op(OPERATOR_COLONEQ))
4431 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4433 error_at(this->location(), "expected %<<-%>");
4436 if (recv_var == "_")
4438 error_at(recv_var_loc,
4439 "no new variables on left side of %<:=%>");
4443 *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
4444 this->advance_token();
4445 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4448 else if (token->is_op(OPERATOR_COMMA))
4450 token = this->advance_token();
4451 if (token->is_identifier())
4453 std::string recv_closed = token->identifier();
4454 bool is_rc_exported = token->is_identifier_exported();
4455 source_location recv_closed_loc = token->location();
4456 closed_is_id = true;
4458 token = this->advance_token();
4459 if (token->is_op(OPERATOR_COLONEQ))
4461 // case rv, rc := <-c:
4462 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4464 error_at(this->location(), "expected %<<-%>");
4467 if (recv_var == "_" && recv_closed == "_")
4469 error_at(recv_var_loc,
4470 "no new variables on left side of %<:=%>");
4474 if (recv_var != "_")
4475 *varname = gogo->pack_hidden_name(recv_var,
4477 if (recv_closed != "_")
4478 *closedname = gogo->pack_hidden_name(recv_closed,
4480 this->advance_token();
4481 *channel = this->expression(PRECEDENCE_NORMAL, false, true,
4486 this->unget_token(Token::make_identifier_token(recv_closed,
4491 *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
4497 this->unget_token(Token::make_identifier_token(recv_var,
4502 // If SAW_COMMA is false, then we are looking at the start of the
4503 // send or receive expression. If SAW_COMMA is true, then *VAL is
4504 // set and we just read a comma.
4507 if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
4508 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL);
4513 this->advance_token();
4514 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4516 // The next token should be ':'. If it is '<-', then we have
4518 // which is to say, send on a channel received from a channel.
4519 if (!this->peek_token()->is_op(OPERATOR_CHANOP))
4522 e = Expression::make_receive(*channel, (*channel)->location());
4525 if (this->peek_token()->is_op(OPERATOR_EQ))
4527 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4529 error_at(this->location(), "missing %<<-%>");
4533 this->advance_token();
4534 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4538 // *VAL is already set.
4539 if (!e->is_sink_expression())
4545 if (!e->is_sink_expression())
4554 error_at(this->location(), "expected %<=%> or %<:=%>");
4556 error_at(this->location(), "expected %<=%>");
4560 if (this->peek_token()->is_op(OPERATOR_CHANOP))
4564 *channel = this->verify_not_sink(e);
4565 this->advance_token();
4566 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4570 error_at(this->location(), "expected %<<-%> or %<=%>");
4574 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
4575 // Condition = Expression .
4578 Parse::for_stat(Label* label)
4580 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
4581 source_location location = this->location();
4582 const Token* token = this->advance_token();
4584 // Open a block to hold any variables defined in the init statement
4585 // of the for statement.
4586 this->gogo_->start_block(location);
4589 Expression* cond = NULL;
4591 Range_clause range_clause;
4593 if (!token->is_op(OPERATOR_LCURLY))
4595 if (token->is_keyword(KEYWORD_VAR))
4597 error_at(this->location(),
4598 "var declaration not allowed in for initializer");
4602 if (token->is_op(OPERATOR_SEMICOLON))
4603 this->for_clause(&cond, &post);
4606 // We might be looking at a Condition, an InitStat, or a
4609 cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
4610 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4612 if (cond == NULL && !range_clause.found)
4615 error_at(this->location(),
4616 ("send statement used as value; "
4617 "use select for non-blocking send"));
4619 error_at(this->location(), "parse error in for statement");
4624 if (range_clause.found)
4625 error_at(this->location(), "parse error after range clause");
4629 // COND is actually an expression statement for
4630 // InitStat at the start of a ForClause.
4631 this->expression_stat(cond);
4635 this->for_clause(&cond, &post);
4640 // Build the For_statement and note that it is the current target
4641 // for break and continue statements.
4643 For_statement* sfor;
4644 For_range_statement* srange;
4646 if (!range_clause.found)
4648 sfor = Statement::make_for_statement(init, cond, post, location);
4654 srange = Statement::make_for_range_statement(range_clause.index,
4662 this->push_break_statement(s, label);
4663 this->push_continue_statement(s, label);
4665 // Gather the block of statements in the loop and add them to the
4668 this->gogo_->start_block(this->location());
4669 source_location end_loc = this->block();
4670 Block* statements = this->gogo_->finish_block(end_loc);
4673 sfor->add_statements(statements);
4675 srange->add_statements(statements);
4677 // This is no longer the break/continue target.
4678 this->pop_break_statement();
4679 this->pop_continue_statement();
4681 // Add the For_statement to the list of statements, and close out
4682 // the block we started to hold any variables defined in the for
4685 this->gogo_->add_statement(s);
4687 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4691 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
4692 // InitStat = SimpleStat .
4693 // PostStat = SimpleStat .
4695 // We have already read InitStat at this point.
4698 Parse::for_clause(Expression** cond, Block** post)
4700 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
4701 this->advance_token();
4702 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4704 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
4706 error_at(this->location(),
4707 "unexpected semicolon or newline before %<{%>");
4713 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4714 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4715 error_at(this->location(), "expected semicolon");
4717 this->advance_token();
4719 if (this->peek_token()->is_op(OPERATOR_LCURLY))
4723 this->gogo_->start_block(this->location());
4724 this->simple_stat(false, NULL, NULL, NULL);
4725 *post = this->gogo_->finish_block(this->location());
4729 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
4731 // This is the := version. It is called with a list of identifiers.
4734 Parse::range_clause_decl(const Typed_identifier_list* til,
4735 Range_clause* p_range_clause)
4737 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4738 source_location location = this->location();
4740 p_range_clause->found = true;
4742 go_assert(til->size() >= 1);
4743 if (til->size() > 2)
4744 error_at(this->location(), "too many variables for range clause");
4746 this->advance_token();
4747 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
4748 p_range_clause->range = expr;
4750 bool any_new = false;
4752 const Typed_identifier* pti = &til->front();
4753 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new);
4754 if (any_new && no->is_variable())
4755 no->var_value()->set_type_from_range_index();
4756 p_range_clause->index = Expression::make_var_reference(no, location);
4758 if (til->size() == 1)
4759 p_range_clause->value = NULL;
4763 bool is_new = false;
4764 no = this->init_var(*pti, NULL, expr, true, true, &is_new);
4765 if (is_new && no->is_variable())
4766 no->var_value()->set_type_from_range_value();
4769 p_range_clause->value = Expression::make_var_reference(no, location);
4773 error_at(location, "variables redeclared but no variable is new");
4776 // The = version of RangeClause. This is called with a list of
4780 Parse::range_clause_expr(const Expression_list* vals,
4781 Range_clause* p_range_clause)
4783 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4785 p_range_clause->found = true;
4787 go_assert(vals->size() >= 1);
4788 if (vals->size() > 2)
4789 error_at(this->location(), "too many variables for range clause");
4791 this->advance_token();
4792 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
4795 p_range_clause->index = vals->front();
4796 if (vals->size() == 1)
4797 p_range_clause->value = NULL;
4799 p_range_clause->value = vals->back();
4802 // Push a statement on the break stack.
4805 Parse::push_break_statement(Statement* enclosing, Label* label)
4807 if (this->break_stack_ == NULL)
4808 this->break_stack_ = new Bc_stack();
4809 this->break_stack_->push_back(std::make_pair(enclosing, label));
4812 // Push a statement on the continue stack.
4815 Parse::push_continue_statement(Statement* enclosing, Label* label)
4817 if (this->continue_stack_ == NULL)
4818 this->continue_stack_ = new Bc_stack();
4819 this->continue_stack_->push_back(std::make_pair(enclosing, label));
4822 // Pop the break stack.
4825 Parse::pop_break_statement()
4827 this->break_stack_->pop_back();
4830 // Pop the continue stack.
4833 Parse::pop_continue_statement()
4835 this->continue_stack_->pop_back();
4838 // Find a break or continue statement given a label name.
4841 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
4843 if (bc_stack == NULL)
4845 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
4846 p != bc_stack->rend();
4849 if (p->second != NULL && p->second->name() == label)
4851 p->second->set_is_used();
4858 // BreakStat = "break" [ identifier ] .
4863 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
4864 source_location location = this->location();
4866 const Token* token = this->advance_token();
4867 Statement* enclosing;
4868 if (!token->is_identifier())
4870 if (this->break_stack_ == NULL || this->break_stack_->empty())
4872 error_at(this->location(),
4873 "break statement not within for or switch or select");
4876 enclosing = this->break_stack_->back().first;
4880 enclosing = this->find_bc_statement(this->break_stack_,
4881 token->identifier());
4882 if (enclosing == NULL)
4884 // If there is a label with this name, mark it as used to
4885 // avoid a useless error about an unused label.
4886 this->gogo_->add_label_reference(token->identifier());
4888 error_at(token->location(), "invalid break label %qs",
4889 Gogo::message_name(token->identifier()).c_str());
4890 this->advance_token();
4893 this->advance_token();
4896 Unnamed_label* label;
4897 if (enclosing->classification() == Statement::STATEMENT_FOR)
4898 label = enclosing->for_statement()->break_label();
4899 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
4900 label = enclosing->for_range_statement()->break_label();
4901 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
4902 label = enclosing->switch_statement()->break_label();
4903 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
4904 label = enclosing->type_switch_statement()->break_label();
4905 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
4906 label = enclosing->select_statement()->break_label();
4910 this->gogo_->add_statement(Statement::make_break_statement(label,
4914 // ContinueStat = "continue" [ identifier ] .
4917 Parse::continue_stat()
4919 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
4920 source_location location = this->location();
4922 const Token* token = this->advance_token();
4923 Statement* enclosing;
4924 if (!token->is_identifier())
4926 if (this->continue_stack_ == NULL || this->continue_stack_->empty())
4928 error_at(this->location(), "continue statement not within for");
4931 enclosing = this->continue_stack_->back().first;
4935 enclosing = this->find_bc_statement(this->continue_stack_,
4936 token->identifier());
4937 if (enclosing == NULL)
4939 // If there is a label with this name, mark it as used to
4940 // avoid a useless error about an unused label.
4941 this->gogo_->add_label_reference(token->identifier());
4943 error_at(token->location(), "invalid continue label %qs",
4944 Gogo::message_name(token->identifier()).c_str());
4945 this->advance_token();
4948 this->advance_token();
4951 Unnamed_label* label;
4952 if (enclosing->classification() == Statement::STATEMENT_FOR)
4953 label = enclosing->for_statement()->continue_label();
4954 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
4955 label = enclosing->for_range_statement()->continue_label();
4959 this->gogo_->add_statement(Statement::make_continue_statement(label,
4963 // GotoStat = "goto" identifier .
4968 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
4969 source_location location = this->location();
4970 const Token* token = this->advance_token();
4971 if (!token->is_identifier())
4972 error_at(this->location(), "expected label for goto");
4975 Label* label = this->gogo_->add_label_reference(token->identifier());
4976 Statement* s = Statement::make_goto_statement(label, location);
4977 this->gogo_->add_statement(s);
4978 this->advance_token();
4982 // PackageClause = "package" PackageName .
4985 Parse::package_clause()
4987 const Token* token = this->peek_token();
4988 source_location location = token->location();
4990 if (!token->is_keyword(KEYWORD_PACKAGE))
4992 error_at(this->location(), "program must start with package clause");
4997 token = this->advance_token();
4998 if (token->is_identifier())
5000 name = token->identifier();
5003 error_at(this->location(), "invalid package name _");
5006 this->advance_token();
5010 error_at(this->location(), "package name must be an identifier");
5014 this->gogo_->set_package_name(name, location);
5017 // ImportDecl = "import" Decl<ImportSpec> .
5020 Parse::import_decl()
5022 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
5023 this->advance_token();
5024 this->decl(&Parse::import_spec, NULL);
5027 // ImportSpec = [ "." | PackageName ] PackageFileName .
5030 Parse::import_spec(void*)
5032 const Token* token = this->peek_token();
5033 source_location location = token->location();
5035 std::string local_name;
5036 bool is_local_name_exported = false;
5037 if (token->is_op(OPERATOR_DOT))
5040 token = this->advance_token();
5042 else if (token->is_identifier())
5044 local_name = token->identifier();
5045 is_local_name_exported = token->is_identifier_exported();
5046 token = this->advance_token();
5049 if (!token->is_string())
5051 error_at(this->location(), "missing import package name");
5055 this->gogo_->import_package(token->string_value(), local_name,
5056 is_local_name_exported, location);
5058 this->advance_token();
5061 // SourceFile = PackageClause ";" { ImportDecl ";" }
5062 // { TopLevelDecl ";" } .
5067 this->package_clause();
5069 const Token* token = this->peek_token();
5070 if (token->is_op(OPERATOR_SEMICOLON))
5071 token = this->advance_token();
5073 error_at(this->location(),
5074 "expected %<;%> or newline after package clause");
5076 while (token->is_keyword(KEYWORD_IMPORT))
5078 this->import_decl();
5079 token = this->peek_token();
5080 if (token->is_op(OPERATOR_SEMICOLON))
5081 token = this->advance_token();
5083 error_at(this->location(),
5084 "expected %<;%> or newline after import declaration");
5087 while (!token->is_eof())
5089 if (this->declaration_may_start_here())
5090 this->declaration();
5093 error_at(this->location(), "expected declaration");
5095 this->advance_token();
5096 while (!this->peek_token()->is_eof()
5097 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
5098 && !this->peek_token()->is_op(OPERATOR_RCURLY));
5099 if (!this->peek_token()->is_eof()
5100 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
5101 this->advance_token();
5103 token = this->peek_token();
5104 if (token->is_op(OPERATOR_SEMICOLON))
5105 token = this->advance_token();
5106 else if (!token->is_eof() || !saw_errors())
5108 if (token->is_op(OPERATOR_CHANOP))
5109 error_at(this->location(),
5110 ("send statement used as value; "
5111 "use select for non-blocking send"));
5113 error_at(this->location(),
5114 "expected %<;%> or newline after top level declaration");
5115 this->skip_past_error(OPERATOR_INVALID);
5120 // Reset the current iota value.
5128 // Return the current iota value.
5136 // Increment the current iota value.
5139 Parse::increment_iota()
5144 // Skip forward to a semicolon or OP. OP will normally be
5145 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5146 // past it and return. If we find OP, it will be the next token to
5147 // read. Return true if we are OK, false if we found EOF.
5150 Parse::skip_past_error(Operator op)
5152 const Token* token = this->peek_token();
5153 while (!token->is_op(op))
5155 if (token->is_eof())
5157 if (token->is_op(OPERATOR_SEMICOLON))
5159 this->advance_token();
5162 token = this->advance_token();
5167 // Check that an expression is not a sink.
5170 Parse::verify_not_sink(Expression* expr)
5172 if (expr->is_sink_expression())
5174 error_at(expr->location(), "cannot use _ as value");
5175 expr = Expression::make_error(expr->location());