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),
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 gcc_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 return this->signature(NULL, location);
249 else if (token->is_keyword(KEYWORD_MAP))
250 return this->map_type();
251 else if (token->is_keyword(KEYWORD_STRUCT))
252 return this->struct_type();
253 else if (token->is_op(OPERATOR_MULT))
254 return this->pointer_type();
255 else if (token->is_op(OPERATOR_LPAREN))
257 this->advance_token();
258 Type* ret = this->type();
259 if (this->peek_token()->is_op(OPERATOR_RPAREN))
260 this->advance_token();
263 if (!ret->is_error_type())
264 error_at(this->location(), "expected %<)%>");
270 error_at(token->location(), "expected type");
271 return Type::make_error_type();
276 Parse::type_may_start_here()
278 const Token* token = this->peek_token();
279 return (token->is_identifier()
280 || token->is_op(OPERATOR_LSQUARE)
281 || token->is_op(OPERATOR_CHANOP)
282 || token->is_keyword(KEYWORD_CHAN)
283 || token->is_keyword(KEYWORD_INTERFACE)
284 || token->is_keyword(KEYWORD_FUNC)
285 || token->is_keyword(KEYWORD_MAP)
286 || token->is_keyword(KEYWORD_STRUCT)
287 || token->is_op(OPERATOR_MULT)
288 || token->is_op(OPERATOR_LPAREN));
291 // TypeName = QualifiedIdent .
293 // If MAY_BE_NIL is true, then an identifier with the value of the
294 // predefined constant nil is accepted, returning the nil type.
297 Parse::type_name(bool issue_error)
299 source_location location = this->location();
302 Named_object* package;
303 if (!this->qualified_ident(&name, &package))
304 return Type::make_error_type();
306 Named_object* named_object;
308 named_object = this->gogo_->lookup(name, NULL);
311 named_object = package->package_value()->lookup(name);
312 if (named_object == NULL
314 && package->name() != this->gogo_->package_name())
316 // Check whether the name is there but hidden.
317 std::string s = ('.' + package->package_value()->unique_prefix()
318 + '.' + package->package_value()->name()
320 named_object = package->package_value()->lookup(s);
321 if (named_object != NULL)
323 const std::string& packname(package->package_value()->name());
324 error_at(location, "invalid reference to hidden type %<%s.%s%>",
325 Gogo::message_name(packname).c_str(),
326 Gogo::message_name(name).c_str());
333 if (named_object == NULL)
338 named_object = this->gogo_->add_unknown_name(name, location);
340 else if (named_object->is_type())
342 if (!named_object->type_value()->is_visible())
345 else if (named_object->is_unknown() || named_object->is_type_declaration())
353 error_at(location, "expected type");
354 return Type::make_error_type();
357 if (named_object->is_type())
358 return named_object->type_value();
359 else if (named_object->is_unknown() || named_object->is_type_declaration())
360 return Type::make_forward_declaration(named_object);
365 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
366 // ArrayLength = Expression .
367 // ElementType = CompleteType .
370 Parse::array_type(bool may_use_ellipsis)
372 gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
373 const Token* token = this->advance_token();
375 Expression* length = NULL;
376 if (token->is_op(OPERATOR_RSQUARE))
377 this->advance_token();
380 if (!token->is_op(OPERATOR_ELLIPSIS))
381 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
382 else if (may_use_ellipsis)
384 // An ellipsis is used in composite literals to represent a
385 // fixed array of the size of the number of elements. We
386 // use a length of nil to represent this, and change the
387 // length when parsing the composite literal.
388 length = Expression::make_nil(this->location());
389 this->advance_token();
393 error_at(this->location(),
394 "use of %<[...]%> outside of array literal");
395 length = Expression::make_error(this->location());
396 this->advance_token();
398 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
400 error_at(this->location(), "expected %<]%>");
401 return Type::make_error_type();
403 this->advance_token();
406 Type* element_type = this->type();
408 return Type::make_array_type(element_type, length);
411 // MapType = "map" "[" KeyType "]" ValueType .
412 // KeyType = CompleteType .
413 // ValueType = CompleteType .
418 source_location location = this->location();
419 gcc_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
420 if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
422 error_at(this->location(), "expected %<[%>");
423 return Type::make_error_type();
425 this->advance_token();
427 Type* key_type = this->type();
429 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
431 error_at(this->location(), "expected %<]%>");
432 return Type::make_error_type();
434 this->advance_token();
436 Type* value_type = this->type();
438 if (key_type->is_error_type() || value_type->is_error_type())
439 return Type::make_error_type();
441 return Type::make_map_type(key_type, value_type, location);
444 // StructType = "struct" "{" { FieldDecl ";" } "}" .
449 gcc_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
450 source_location location = this->location();
451 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
453 source_location token_loc = this->location();
454 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
455 && this->advance_token()->is_op(OPERATOR_LCURLY))
456 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
459 error_at(this->location(), "expected %<{%>");
460 return Type::make_error_type();
463 this->advance_token();
465 Struct_field_list* sfl = new Struct_field_list;
466 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
468 this->field_decl(sfl);
469 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
470 this->advance_token();
471 else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
473 error_at(this->location(), "expected %<;%> or %<}%> or newline");
474 if (!this->skip_past_error(OPERATOR_RCURLY))
475 return Type::make_error_type();
478 this->advance_token();
480 for (Struct_field_list::const_iterator pi = sfl->begin();
484 if (pi->type()->is_error_type())
486 for (Struct_field_list::const_iterator pj = pi + 1;
490 if (pi->field_name() == pj->field_name()
491 && !Gogo::is_sink_name(pi->field_name()))
492 error_at(pi->location(), "duplicate field name %<%s%>",
493 Gogo::message_name(pi->field_name()).c_str());
497 return Type::make_struct_type(sfl, location);
500 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
501 // Tag = string_lit .
504 Parse::field_decl(Struct_field_list* sfl)
506 const Token* token = this->peek_token();
507 source_location location = token->location();
509 bool is_anonymous_pointer;
510 if (token->is_op(OPERATOR_MULT))
513 is_anonymous_pointer = true;
515 else if (token->is_identifier())
517 std::string id = token->identifier();
518 bool is_id_exported = token->is_identifier_exported();
519 source_location id_location = token->location();
520 token = this->advance_token();
521 is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
522 || token->is_op(OPERATOR_RCURLY)
523 || token->is_op(OPERATOR_DOT)
524 || token->is_string());
525 is_anonymous_pointer = false;
526 this->unget_token(Token::make_identifier_token(id, is_id_exported,
531 error_at(this->location(), "expected field name");
532 while (!token->is_op(OPERATOR_SEMICOLON)
533 && !token->is_op(OPERATOR_RCURLY)
535 token = this->advance_token();
541 if (is_anonymous_pointer)
543 this->advance_token();
544 if (!this->peek_token()->is_identifier())
546 error_at(this->location(), "expected field name");
547 while (!token->is_op(OPERATOR_SEMICOLON)
548 && !token->is_op(OPERATOR_RCURLY)
550 token = this->advance_token();
554 Type* type = this->type_name(true);
557 if (this->peek_token()->is_string())
559 tag = this->peek_token()->string_value();
560 this->advance_token();
563 if (!type->is_error_type())
565 if (is_anonymous_pointer)
566 type = Type::make_pointer_type(type);
567 sfl->push_back(Struct_field(Typed_identifier("", type, location)));
569 sfl->back().set_tag(tag);
574 Typed_identifier_list til;
577 token = this->peek_token();
578 if (!token->is_identifier())
580 error_at(this->location(), "expected identifier");
584 this->gogo_->pack_hidden_name(token->identifier(),
585 token->is_identifier_exported());
586 til.push_back(Typed_identifier(name, NULL, token->location()));
587 if (!this->advance_token()->is_op(OPERATOR_COMMA))
589 this->advance_token();
592 Type* type = this->type();
595 if (this->peek_token()->is_string())
597 tag = this->peek_token()->string_value();
598 this->advance_token();
601 for (Typed_identifier_list::iterator p = til.begin();
606 sfl->push_back(Struct_field(*p));
608 sfl->back().set_tag(tag);
613 // PointerType = "*" Type .
616 Parse::pointer_type()
618 gcc_assert(this->peek_token()->is_op(OPERATOR_MULT));
619 this->advance_token();
620 Type* type = this->type();
621 if (type->is_error_type())
623 return Type::make_pointer_type(type);
626 // ChannelType = Channel | SendChannel | RecvChannel .
627 // Channel = "chan" ElementType .
628 // SendChannel = "chan" "<-" ElementType .
629 // RecvChannel = "<-" "chan" ElementType .
632 Parse::channel_type()
634 const Token* token = this->peek_token();
637 if (token->is_op(OPERATOR_CHANOP))
639 if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
641 error_at(this->location(), "expected %<chan%>");
642 return Type::make_error_type();
645 this->advance_token();
649 gcc_assert(token->is_keyword(KEYWORD_CHAN));
650 if (this->advance_token()->is_op(OPERATOR_CHANOP))
653 this->advance_token();
656 Type* element_type = this->type();
657 return Type::make_channel_type(send, receive, element_type);
660 // Signature = Parameters [ Result ] .
662 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
663 // location of the start of the type.
666 Parse::signature(Typed_identifier* receiver, source_location location)
668 bool is_varargs = false;
669 Typed_identifier_list* params = this->parameters(&is_varargs);
671 Typed_identifier_list* result = NULL;
672 if (this->peek_token()->is_op(OPERATOR_LPAREN)
673 || this->type_may_start_here())
674 result = this->result();
676 Function_type* ret = Type::make_function_type(receiver, params, result,
679 ret->set_is_varargs();
683 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
685 Typed_identifier_list*
686 Parse::parameters(bool* is_varargs)
688 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
690 error_at(this->location(), "expected %<(%>");
694 Typed_identifier_list* params = NULL;
696 const Token* token = this->advance_token();
697 if (!token->is_op(OPERATOR_RPAREN))
699 params = this->parameter_list(is_varargs);
700 token = this->peek_token();
703 // The optional trailing comma is picked up in parameter_list.
705 if (!token->is_op(OPERATOR_RPAREN))
706 error_at(this->location(), "expected %<)%>");
708 this->advance_token();
713 // ParameterList = ParameterDecl { "," ParameterDecl } .
715 // This sets *IS_VARARGS if the list ends with an ellipsis.
716 // IS_VARARGS will be NULL if varargs are not permitted.
718 // We pick up an optional trailing comma.
720 Typed_identifier_list*
721 Parse::parameter_list(bool* is_varargs)
723 source_location location = this->location();
724 Typed_identifier_list* ret = new Typed_identifier_list();
726 // If we see an identifier and then a comma, then we don't know
727 // whether we are looking at a list of identifiers followed by a
728 // type, or a list of types given by name. We have to do an
729 // arbitrary lookahead to figure it out.
731 bool parameters_have_names;
732 const Token* token = this->peek_token();
733 if (!token->is_identifier())
735 // This must be a type which starts with something like '*'.
736 parameters_have_names = false;
740 std::string name = token->identifier();
741 bool is_exported = token->is_identifier_exported();
742 source_location location = token->location();
743 token = this->advance_token();
744 if (!token->is_op(OPERATOR_COMMA))
746 if (token->is_op(OPERATOR_DOT))
748 // This is a qualified identifier, which must turn out
750 parameters_have_names = false;
752 else if (token->is_op(OPERATOR_RPAREN))
754 // A single identifier followed by a parenthesis must be
756 parameters_have_names = false;
760 // An identifier followed by something other than a
761 // comma or a dot or a right parenthesis must be a
762 // parameter name followed by a type.
763 parameters_have_names = true;
766 this->unget_token(Token::make_identifier_token(name, is_exported,
771 // An identifier followed by a comma may be the first in a
772 // list of parameter names followed by a type, or it may be
773 // the first in a list of types without parameter names. To
774 // find out we gather as many identifiers separated by
776 std::string id_name = this->gogo_->pack_hidden_name(name,
778 ret->push_back(Typed_identifier(id_name, NULL, location));
779 bool just_saw_comma = true;
780 while (this->advance_token()->is_identifier())
782 name = this->peek_token()->identifier();
783 is_exported = this->peek_token()->is_identifier_exported();
784 location = this->peek_token()->location();
785 id_name = this->gogo_->pack_hidden_name(name, is_exported);
786 ret->push_back(Typed_identifier(id_name, NULL, location));
787 if (!this->advance_token()->is_op(OPERATOR_COMMA))
789 just_saw_comma = false;
796 // We saw ID1 "," ID2 "," followed by something which
797 // was not an identifier. We must be seeing the start
798 // of a type, and ID1 and ID2 must be types, and the
799 // parameters don't have names.
800 parameters_have_names = false;
802 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
804 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
805 // and the parameters don't have names.
806 parameters_have_names = false;
808 else if (this->peek_token()->is_op(OPERATOR_DOT))
810 // We saw ID1 "," ID2 ".". ID2 must be a package name,
811 // ID1 must be a type, and the parameters don't have
813 parameters_have_names = false;
814 this->unget_token(Token::make_identifier_token(name, is_exported,
817 just_saw_comma = true;
821 // We saw ID1 "," ID2 followed by something other than
822 // ",", ".", or ")". We must be looking at the start of
823 // a type, and ID1 and ID2 must be parameter names.
824 parameters_have_names = true;
827 if (parameters_have_names)
829 gcc_assert(!just_saw_comma);
830 // We have just seen ID1, ID2 xxx.
832 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
836 error_at(this->location(), "%<...%> only permits one name");
837 this->advance_token();
840 for (size_t i = 0; i < ret->size(); ++i)
841 ret->set_type(i, type);
842 if (!this->peek_token()->is_op(OPERATOR_COMMA))
844 if (this->advance_token()->is_op(OPERATOR_RPAREN))
849 Typed_identifier_list* tret = new Typed_identifier_list();
850 for (Typed_identifier_list::const_iterator p = ret->begin();
854 Named_object* no = this->gogo_->lookup(p->name(), NULL);
857 no = this->gogo_->add_unknown_name(p->name(),
861 type = no->type_value();
862 else if (no->is_unknown() || no->is_type_declaration())
863 type = Type::make_forward_declaration(no);
866 error_at(p->location(), "expected %<%s%> to be a type",
867 Gogo::message_name(p->name()).c_str());
868 type = Type::make_error_type();
870 tret->push_back(Typed_identifier("", type, p->location()));
875 || this->peek_token()->is_op(OPERATOR_RPAREN))
881 bool mix_error = false;
882 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
883 while (this->peek_token()->is_op(OPERATOR_COMMA))
885 if (is_varargs != NULL && *is_varargs)
886 error_at(this->location(), "%<...%> must be last parameter");
887 if (this->advance_token()->is_op(OPERATOR_RPAREN))
889 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
892 error_at(location, "invalid named/anonymous mix");
896 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
899 Parse::parameter_decl(bool parameters_have_names,
900 Typed_identifier_list* til,
904 if (!parameters_have_names)
907 source_location location = this->location();
908 if (!this->peek_token()->is_identifier())
910 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
914 if (is_varargs == NULL)
915 error_at(this->location(), "invalid use of %<...%>");
918 this->advance_token();
919 if (is_varargs == NULL
920 && this->peek_token()->is_op(OPERATOR_RPAREN))
921 type = Type::make_error_type();
924 Type* element_type = this->type();
925 type = Type::make_array_type(element_type, NULL);
931 type = this->type_name(false);
932 if (type->is_error_type()
933 || (!this->peek_token()->is_op(OPERATOR_COMMA)
934 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
937 while (!this->peek_token()->is_op(OPERATOR_COMMA)
938 && !this->peek_token()->is_op(OPERATOR_RPAREN))
939 this->advance_token();
942 if (!type->is_error_type())
943 til->push_back(Typed_identifier("", type, location));
947 size_t orig_count = til->size();
948 if (this->peek_token()->is_identifier())
949 this->identifier_list(til);
952 size_t new_count = til->size();
955 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
959 if (is_varargs == NULL)
960 error_at(this->location(), "invalid use of %<...%>");
961 else if (new_count > orig_count + 1)
962 error_at(this->location(), "%<...%> only permits one name");
965 this->advance_token();
966 Type* element_type = this->type();
967 type = Type::make_array_type(element_type, NULL);
969 for (size_t i = orig_count; i < new_count; ++i)
970 til->set_type(i, type);
974 // Result = Parameters | Type .
976 Typed_identifier_list*
979 if (this->peek_token()->is_op(OPERATOR_LPAREN))
980 return this->parameters(NULL);
983 source_location location = this->location();
984 Typed_identifier_list* til = new Typed_identifier_list();
985 Type* type = this->type();
986 til->push_back(Typed_identifier("", type, location));
991 // Block = "{" [ StatementList ] "}" .
993 // Returns the location of the closing brace.
998 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1000 source_location loc = this->location();
1001 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1002 && this->advance_token()->is_op(OPERATOR_LCURLY))
1003 error_at(loc, "unexpected semicolon or newline before %<{%>");
1006 error_at(this->location(), "expected %<{%>");
1007 return UNKNOWN_LOCATION;
1011 const Token* token = this->advance_token();
1013 if (!token->is_op(OPERATOR_RCURLY))
1015 this->statement_list();
1016 token = this->peek_token();
1017 if (!token->is_op(OPERATOR_RCURLY))
1019 if (!token->is_eof() || !saw_errors())
1020 error_at(this->location(), "expected %<}%>");
1022 // Skip ahead to the end of the block, in hopes of avoiding
1023 // lots of meaningless errors.
1024 source_location ret = token->location();
1026 while (!token->is_eof())
1028 if (token->is_op(OPERATOR_LCURLY))
1030 else if (token->is_op(OPERATOR_RCURLY))
1035 this->advance_token();
1039 token = this->advance_token();
1040 ret = token->location();
1046 source_location ret = token->location();
1047 this->advance_token();
1051 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1052 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1055 Parse::interface_type()
1057 gcc_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1058 source_location location = this->location();
1060 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1062 source_location token_loc = this->location();
1063 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1064 && this->advance_token()->is_op(OPERATOR_LCURLY))
1065 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1068 error_at(this->location(), "expected %<{%>");
1069 return Type::make_error_type();
1072 this->advance_token();
1074 Typed_identifier_list* methods = new Typed_identifier_list();
1075 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1077 this->method_spec(methods);
1078 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1080 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1082 this->method_spec(methods);
1084 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1086 error_at(this->location(), "expected %<}%>");
1087 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1089 if (this->peek_token()->is_eof())
1090 return Type::make_error_type();
1094 this->advance_token();
1096 if (methods->empty())
1102 Interface_type* ret = Type::make_interface_type(methods, location);
1103 this->gogo_->record_interface_type(ret);
1107 // MethodSpec = MethodName Signature | InterfaceTypeName .
1108 // MethodName = identifier .
1109 // InterfaceTypeName = TypeName .
1112 Parse::method_spec(Typed_identifier_list* methods)
1114 const Token* token = this->peek_token();
1115 if (!token->is_identifier())
1117 error_at(this->location(), "expected identifier");
1121 std::string name = token->identifier();
1122 bool is_exported = token->is_identifier_exported();
1123 source_location location = token->location();
1125 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1127 // This is a MethodName.
1128 name = this->gogo_->pack_hidden_name(name, is_exported);
1129 Function_type* type = this->signature(NULL, location);
1130 methods->push_back(Typed_identifier(name, type, location));
1134 this->unget_token(Token::make_identifier_token(name, is_exported,
1136 Type* type = this->type_name(false);
1137 if (type->is_error_type()
1138 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1139 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1141 if (this->peek_token()->is_op(OPERATOR_COMMA))
1142 error_at(this->location(),
1143 "name list not allowed in interface type");
1145 error_at(location, "expected signature or type name");
1146 token = this->peek_token();
1147 while (!token->is_eof()
1148 && !token->is_op(OPERATOR_SEMICOLON)
1149 && !token->is_op(OPERATOR_RCURLY))
1150 token = this->advance_token();
1153 // This must be an interface type, but we can't check that now.
1154 // We check it and pull out the methods in
1155 // Interface_type::do_verify.
1156 methods->push_back(Typed_identifier("", type, location));
1162 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1165 Parse::declaration()
1167 const Token* token = this->peek_token();
1168 if (token->is_keyword(KEYWORD_CONST))
1170 else if (token->is_keyword(KEYWORD_TYPE))
1172 else if (token->is_keyword(KEYWORD_VAR))
1174 else if (token->is_keyword(KEYWORD_FUNC))
1175 this->function_decl();
1178 error_at(this->location(), "expected declaration");
1179 this->advance_token();
1184 Parse::declaration_may_start_here()
1186 const Token* token = this->peek_token();
1187 return (token->is_keyword(KEYWORD_CONST)
1188 || token->is_keyword(KEYWORD_TYPE)
1189 || token->is_keyword(KEYWORD_VAR)
1190 || token->is_keyword(KEYWORD_FUNC));
1193 // Decl<P> = P | "(" [ List<P> ] ")" .
1196 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1198 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1202 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1204 this->list(pfn, varg, true);
1205 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1207 error_at(this->location(), "missing %<)%>");
1208 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1210 if (this->peek_token()->is_eof())
1215 this->advance_token();
1219 // List<P> = P { ";" P } [ ";" ] .
1221 // In order to pick up the trailing semicolon we need to know what
1222 // might follow. This is either a '}' or a ')'.
1225 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1228 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1229 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1230 || this->peek_token()->is_op(OPERATOR_COMMA))
1232 if (this->peek_token()->is_op(OPERATOR_COMMA))
1233 error_at(this->location(), "unexpected comma");
1234 if (this->advance_token()->is_op(follow))
1240 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1245 gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1246 this->advance_token();
1249 Type* last_type = NULL;
1250 Expression_list* last_expr_list = NULL;
1252 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1253 this->const_spec(&last_type, &last_expr_list);
1256 this->advance_token();
1257 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1259 this->const_spec(&last_type, &last_expr_list);
1260 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1261 this->advance_token();
1262 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1264 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1265 if (!this->skip_past_error(OPERATOR_RPAREN))
1269 this->advance_token();
1272 if (last_expr_list != NULL)
1273 delete last_expr_list;
1276 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1279 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1281 Typed_identifier_list til;
1282 this->identifier_list(&til);
1285 if (this->type_may_start_here())
1287 type = this->type();
1289 *last_expr_list = NULL;
1292 Expression_list *expr_list;
1293 if (!this->peek_token()->is_op(OPERATOR_EQ))
1295 if (*last_expr_list == NULL)
1297 error_at(this->location(), "expected %<=%>");
1301 expr_list = new Expression_list;
1302 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1303 p != (*last_expr_list)->end();
1305 expr_list->push_back((*p)->copy());
1309 this->advance_token();
1310 expr_list = this->expression_list(NULL, false);
1312 if (*last_expr_list != NULL)
1313 delete *last_expr_list;
1314 *last_expr_list = expr_list;
1317 Expression_list::const_iterator pe = expr_list->begin();
1318 for (Typed_identifier_list::iterator pi = til.begin();
1322 if (pe == expr_list->end())
1324 error_at(this->location(), "not enough initializers");
1330 if (!Gogo::is_sink_name(pi->name()))
1331 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1333 if (pe != expr_list->end())
1334 error_at(this->location(), "too many initializers");
1336 this->increment_iota();
1341 // TypeDecl = "type" Decl<TypeSpec> .
1346 gcc_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1347 this->advance_token();
1348 this->decl(&Parse::type_spec, NULL);
1351 // TypeSpec = identifier Type .
1354 Parse::type_spec(void*)
1356 const Token* token = this->peek_token();
1357 if (!token->is_identifier())
1359 error_at(this->location(), "expected identifier");
1362 std::string name = token->identifier();
1363 bool is_exported = token->is_identifier_exported();
1364 source_location location = token->location();
1365 token = this->advance_token();
1367 // The scope of the type name starts at the point where the
1368 // identifier appears in the source code. We implement this by
1369 // declaring the type before we read the type definition.
1370 Named_object* named_type = NULL;
1373 name = this->gogo_->pack_hidden_name(name, is_exported);
1374 named_type = this->gogo_->declare_type(name, location);
1378 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1379 type = this->type();
1382 error_at(this->location(),
1383 "unexpected semicolon or newline in type declaration");
1384 type = Type::make_error_type();
1385 this->advance_token();
1388 if (type->is_error_type())
1390 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1391 && !this->peek_token()->is_eof())
1392 this->advance_token();
1397 if (named_type->is_type_declaration())
1399 Type* ftype = type->forwarded();
1400 if (ftype->forward_declaration_type() != NULL
1401 && (ftype->forward_declaration_type()->named_object()
1404 error_at(location, "invalid recursive type");
1405 type = Type::make_error_type();
1408 this->gogo_->define_type(named_type,
1409 Type::make_named_type(named_type, type,
1411 gcc_assert(named_type->package() == NULL);
1415 // This will probably give a redefinition error.
1416 this->gogo_->add_type(name, type, location);
1421 // VarDecl = "var" Decl<VarSpec> .
1426 gcc_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1427 this->advance_token();
1428 this->decl(&Parse::var_spec, NULL);
1431 // VarSpec = IdentifierList
1432 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1435 Parse::var_spec(void*)
1437 // Get the variable names.
1438 Typed_identifier_list til;
1439 this->identifier_list(&til);
1441 source_location location = this->location();
1444 Expression_list* init = NULL;
1445 if (!this->peek_token()->is_op(OPERATOR_EQ))
1447 type = this->type();
1448 if (type->is_error_type())
1450 while (!this->peek_token()->is_op(OPERATOR_EQ)
1451 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1452 && !this->peek_token()->is_eof())
1453 this->advance_token();
1455 if (this->peek_token()->is_op(OPERATOR_EQ))
1457 this->advance_token();
1458 init = this->expression_list(NULL, false);
1463 this->advance_token();
1464 init = this->expression_list(NULL, false);
1467 this->init_vars(&til, type, init, false, location);
1473 // Create variables. TIL is a list of variable names. If TYPE is not
1474 // NULL, it is the type of all the variables. If INIT is not NULL, it
1475 // is an initializer list for the variables.
1478 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1479 Expression_list* init, bool is_coloneq,
1480 source_location location)
1482 // Check for an initialization which can yield multiple values.
1483 if (init != NULL && init->size() == 1 && til->size() > 1)
1485 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1488 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1491 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1494 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1495 is_coloneq, location))
1499 if (init != NULL && init->size() != til->size())
1501 if (init->empty() || !init->front()->is_error_expression())
1502 error_at(location, "wrong number of initializations");
1505 type = Type::make_error_type();
1508 // Note that INIT was already parsed with the old name bindings, so
1509 // we don't have to worry that it will accidentally refer to the
1510 // newly declared variables.
1512 Expression_list::const_iterator pexpr;
1514 pexpr = init->begin();
1515 bool any_new = false;
1516 for (Typed_identifier_list::const_iterator p = til->begin();
1521 gcc_assert(pexpr != init->end());
1522 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1528 gcc_assert(pexpr == init->end());
1529 if (is_coloneq && !any_new)
1530 error_at(location, "variables redeclared but no variable is new");
1533 // See if we need to initialize a list of variables from a function
1534 // call. This returns true if we have set up the variables and the
1538 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1539 Expression* expr, bool is_coloneq,
1540 source_location location)
1542 Call_expression* call = expr->call_expression();
1546 // This is a function call. We can't check here whether it returns
1547 // the right number of values, but it might. Declare the variables,
1548 // and then assign the results of the call to them.
1550 unsigned int index = 0;
1551 bool any_new = false;
1552 for (Typed_identifier_list::const_iterator pv = vars->begin();
1556 Expression* init = Expression::make_call_result(call, index);
1557 this->init_var(*pv, type, init, is_coloneq, false, &any_new);
1560 if (is_coloneq && !any_new)
1561 error_at(location, "variables redeclared but no variable is new");
1566 // See if we need to initialize a pair of values from a map index
1567 // expression. This returns true if we have set up the variables and
1568 // the initialization.
1571 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1572 Expression* expr, bool is_coloneq,
1573 source_location location)
1575 Index_expression* index = expr->index_expression();
1578 if (vars->size() != 2)
1581 // This is an index which is being assigned to two variables. It
1582 // must be a map index. Declare the variables, and then assign the
1583 // results of the map index.
1584 bool any_new = false;
1585 Typed_identifier_list::const_iterator p = vars->begin();
1586 Expression* init = type == NULL ? index : NULL;
1587 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1588 type == NULL, &any_new);
1589 if (type == NULL && any_new && val_no->is_variable())
1590 val_no->var_value()->set_type_from_init_tuple();
1591 Expression* val_var = Expression::make_var_reference(val_no, location);
1594 Type* var_type = type;
1595 if (var_type == NULL)
1596 var_type = Type::lookup_bool_type();
1597 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1599 Expression* present_var = Expression::make_var_reference(no, location);
1601 if (is_coloneq && !any_new)
1602 error_at(location, "variables redeclared but no variable is new");
1604 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1607 if (!this->gogo_->in_global_scope())
1608 this->gogo_->add_statement(s);
1610 val_no->var_value()->add_preinit_statement(s);
1615 // See if we need to initialize a pair of values from a receive
1616 // expression. This returns true if we have set up the variables and
1617 // the initialization.
1620 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1621 Expression* expr, bool is_coloneq,
1622 source_location location)
1624 Receive_expression* receive = expr->receive_expression();
1625 if (receive == NULL)
1627 if (vars->size() != 2)
1630 // This is a receive expression which is being assigned to two
1631 // variables. Declare the variables, and then assign the results of
1633 bool any_new = false;
1634 Typed_identifier_list::const_iterator p = vars->begin();
1635 Expression* init = type == NULL ? receive : NULL;
1636 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1637 type == NULL, &any_new);
1638 if (type == NULL && any_new && val_no->is_variable())
1639 val_no->var_value()->set_type_from_init_tuple();
1640 Expression* val_var = Expression::make_var_reference(val_no, location);
1643 Type* var_type = type;
1644 if (var_type == NULL)
1645 var_type = Type::lookup_bool_type();
1646 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1648 Expression* received_var = Expression::make_var_reference(no, location);
1650 if (is_coloneq && !any_new)
1651 error_at(location, "variables redeclared but no variable is new");
1653 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1658 if (!this->gogo_->in_global_scope())
1659 this->gogo_->add_statement(s);
1661 val_no->var_value()->add_preinit_statement(s);
1666 // See if we need to initialize a pair of values from a type guard
1667 // expression. This returns true if we have set up the variables and
1668 // the initialization.
1671 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1672 Type* type, Expression* expr,
1673 bool is_coloneq, source_location location)
1675 Type_guard_expression* type_guard = expr->type_guard_expression();
1676 if (type_guard == NULL)
1678 if (vars->size() != 2)
1681 // This is a type guard expression which is being assigned to two
1682 // variables. Declare the variables, and then assign the results of
1684 bool any_new = false;
1685 Typed_identifier_list::const_iterator p = vars->begin();
1686 Type* var_type = type;
1687 if (var_type == NULL)
1688 var_type = type_guard->type();
1689 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1691 Expression* val_var = Expression::make_var_reference(val_no, location);
1695 if (var_type == NULL)
1696 var_type = Type::lookup_bool_type();
1697 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1699 Expression* ok_var = Expression::make_var_reference(no, location);
1701 Expression* texpr = type_guard->expr();
1702 Type* t = type_guard->type();
1703 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1707 if (is_coloneq && !any_new)
1708 error_at(location, "variables redeclared but no variable is new");
1710 if (!this->gogo_->in_global_scope())
1711 this->gogo_->add_statement(s);
1713 val_no->var_value()->add_preinit_statement(s);
1718 // Create a single variable. If IS_COLONEQ is true, we permit
1719 // redeclarations in the same block, and we set *IS_NEW when we find a
1720 // new variable which is not a redeclaration.
1723 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1724 bool is_coloneq, bool type_from_init, bool* is_new)
1726 source_location location = tid.location();
1728 if (Gogo::is_sink_name(tid.name()))
1730 if (!type_from_init && init != NULL)
1732 if (!this->gogo_->in_global_scope())
1733 this->gogo_->add_statement(Statement::make_statement(init));
1736 // Create a dummy global variable to force the
1737 // initializer to be run in the right place.
1738 Variable* var = new Variable(type, init, true, false, false,
1742 snprintf(buf, sizeof buf, "_.%d", count);
1744 return this->gogo_->add_variable(buf, var);
1747 return this->gogo_->add_sink();
1752 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1754 && (no->is_variable() || no->is_result_variable()))
1756 // INIT may be NULL even when IS_COLONEQ is true for cases
1757 // like v, ok := x.(int).
1758 if (!type_from_init && init != NULL)
1760 Expression *v = Expression::make_var_reference(no, location);
1761 Statement *s = Statement::make_assignment(v, init, location);
1762 this->gogo_->add_statement(s);
1768 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1769 false, false, location);
1770 return this->gogo_->add_variable(tid.name(), var);
1773 // SimpleVarDecl = identifier ":=" Expression .
1775 // We've already seen the identifier.
1777 // FIXME: We also have to implement
1778 // IdentifierList ":=" ExpressionList
1779 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1780 // tuple assignments here as well.
1782 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
1785 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
1786 // guard (var := expr.("type") using the literal keyword "type").
1789 Parse::simple_var_decl_or_assignment(const std::string& name,
1790 source_location location,
1791 Range_clause* p_range_clause,
1792 Type_switch* p_type_switch)
1794 Typed_identifier_list til;
1795 til.push_back(Typed_identifier(name, NULL, location));
1797 // We've seen one identifier. If we see a comma now, this could be
1799 if (this->peek_token()->is_op(OPERATOR_COMMA))
1801 gcc_assert(p_type_switch == NULL);
1804 const Token* token = this->advance_token();
1805 if (!token->is_identifier())
1808 std::string id = token->identifier();
1809 bool is_id_exported = token->is_identifier_exported();
1810 source_location id_location = token->location();
1812 token = this->advance_token();
1813 if (!token->is_op(OPERATOR_COMMA))
1815 if (token->is_op(OPERATOR_COLONEQ))
1817 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1818 til.push_back(Typed_identifier(id, NULL, location));
1821 this->unget_token(Token::make_identifier_token(id,
1827 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1828 til.push_back(Typed_identifier(id, NULL, location));
1831 // We have a comma separated list of identifiers in TIL. If the
1832 // next token is COLONEQ, then this is a simple var decl, and we
1833 // have the complete list of identifiers. If the next token is
1834 // not COLONEQ, then the only valid parse is a tuple assignment.
1835 // The list of identifiers we have so far is really a list of
1836 // expressions. There are more expressions following.
1838 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
1840 Expression_list* exprs = new Expression_list;
1841 for (Typed_identifier_list::const_iterator p = til.begin();
1844 exprs->push_back(this->id_to_expression(p->name(),
1847 Expression_list* more_exprs = this->expression_list(NULL, true);
1848 for (Expression_list::const_iterator p = more_exprs->begin();
1849 p != more_exprs->end();
1851 exprs->push_back(*p);
1854 this->tuple_assignment(exprs, p_range_clause);
1859 gcc_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
1860 const Token* token = this->advance_token();
1862 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
1864 this->range_clause_decl(&til, p_range_clause);
1868 Expression_list* init;
1869 if (p_type_switch == NULL)
1870 init = this->expression_list(NULL, false);
1873 bool is_type_switch = false;
1874 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
1878 p_type_switch->found = true;
1879 p_type_switch->name = name;
1880 p_type_switch->location = location;
1881 p_type_switch->expr = expr;
1885 if (!this->peek_token()->is_op(OPERATOR_COMMA))
1887 init = new Expression_list();
1888 init->push_back(expr);
1892 this->advance_token();
1893 init = this->expression_list(expr, false);
1897 this->init_vars(&til, NULL, init, true, location);
1900 // FunctionDecl = "func" identifier Signature [ Block ] .
1901 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
1904 // FunctionDecl = "func" identifier Signature
1905 // __asm__ "(" string_lit ")" .
1906 // This extension means a function whose real name is the identifier
1910 Parse::function_decl()
1912 gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
1913 source_location location = this->location();
1914 const Token* token = this->advance_token();
1916 Typed_identifier* rec = NULL;
1917 if (token->is_op(OPERATOR_LPAREN))
1919 rec = this->receiver();
1920 token = this->peek_token();
1923 if (!token->is_identifier())
1925 error_at(this->location(), "expected function name");
1930 this->gogo_->pack_hidden_name(token->identifier(),
1931 token->is_identifier_exported());
1933 this->advance_token();
1935 Function_type* fntype = this->signature(rec, this->location());
1937 Named_object* named_object = NULL;
1939 if (this->peek_token()->is_keyword(KEYWORD_ASM))
1941 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
1943 error_at(this->location(), "expected %<(%>");
1946 token = this->advance_token();
1947 if (!token->is_string())
1949 error_at(this->location(), "expected string");
1952 std::string asm_name = token->string_value();
1953 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1955 error_at(this->location(), "expected %<)%>");
1958 this->advance_token();
1959 named_object = this->gogo_->declare_function(name, fntype, location);
1960 if (named_object->is_function_declaration())
1961 named_object->func_declaration_value()->set_asm_name(asm_name);
1964 // Check for the easy error of a newline before the opening brace.
1965 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1967 source_location semi_loc = this->location();
1968 if (this->advance_token()->is_op(OPERATOR_LCURLY))
1969 error_at(this->location(),
1970 "unexpected semicolon or newline before %<{%>");
1972 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
1976 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1978 if (named_object == NULL)
1979 named_object = this->gogo_->declare_function(name, fntype, location);
1983 this->gogo_->start_function(name, fntype, true, location);
1984 source_location end_loc = this->block();
1985 this->gogo_->finish_function(end_loc);
1989 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
1990 // BaseTypeName = identifier .
1995 gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
1998 const Token* token = this->advance_token();
1999 source_location location = token->location();
2000 if (!token->is_op(OPERATOR_MULT))
2002 if (!token->is_identifier())
2004 error_at(this->location(), "method has no receiver");
2005 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2006 token = this->advance_token();
2007 if (!token->is_eof())
2008 this->advance_token();
2011 name = token->identifier();
2012 bool is_exported = token->is_identifier_exported();
2013 token = this->advance_token();
2014 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2016 // An identifier followed by something other than a dot or a
2017 // right parenthesis must be a receiver name followed by a
2019 name = this->gogo_->pack_hidden_name(name, is_exported);
2023 // This must be a type name.
2024 this->unget_token(Token::make_identifier_token(name, is_exported,
2026 token = this->peek_token();
2031 // Here the receiver name is in NAME (it is empty if the receiver is
2032 // unnamed) and TOKEN is the first token in the type.
2034 bool is_pointer = false;
2035 if (token->is_op(OPERATOR_MULT))
2038 token = this->advance_token();
2041 if (!token->is_identifier())
2043 error_at(this->location(), "expected receiver name or type");
2044 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2045 while (!token->is_eof())
2047 token = this->advance_token();
2048 if (token->is_op(OPERATOR_LPAREN))
2050 else if (token->is_op(OPERATOR_RPAREN))
2057 if (!token->is_eof())
2058 this->advance_token();
2062 Type* type = this->type_name(true);
2064 if (is_pointer && !type->is_error_type())
2065 type = Type::make_pointer_type(type);
2067 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2068 this->advance_token();
2071 if (this->peek_token()->is_op(OPERATOR_COMMA))
2072 error_at(this->location(), "method has multiple receivers");
2074 error_at(this->location(), "expected %<)%>");
2075 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2076 token = this->advance_token();
2077 if (!token->is_eof())
2078 this->advance_token();
2082 return new Typed_identifier(name, type, location);
2085 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2086 // Literal = BasicLit | CompositeLit | FunctionLit .
2087 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2089 // If MAY_BE_SINK is true, this operand may be "_".
2092 Parse::operand(bool may_be_sink)
2094 const Token* token = this->peek_token();
2096 switch (token->classification())
2098 case Token::TOKEN_IDENTIFIER:
2100 source_location location = token->location();
2101 std::string id = token->identifier();
2102 bool is_exported = token->is_identifier_exported();
2103 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2105 Named_object* in_function;
2106 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2108 Package* package = NULL;
2109 if (named_object != NULL && named_object->is_package())
2111 if (!this->advance_token()->is_op(OPERATOR_DOT)
2112 || !this->advance_token()->is_identifier())
2114 error_at(location, "unexpected reference to package");
2115 return Expression::make_error(location);
2117 package = named_object->package_value();
2118 package->set_used();
2119 id = this->peek_token()->identifier();
2120 is_exported = this->peek_token()->is_identifier_exported();
2121 packed = this->gogo_->pack_hidden_name(id, is_exported);
2122 named_object = package->lookup(packed);
2123 location = this->location();
2124 gcc_assert(in_function == NULL);
2127 this->advance_token();
2129 if (named_object != NULL
2130 && named_object->is_type()
2131 && !named_object->type_value()->is_visible())
2133 gcc_assert(package != NULL);
2134 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2135 Gogo::message_name(package->name()).c_str(),
2136 Gogo::message_name(id).c_str());
2137 return Expression::make_error(location);
2141 if (named_object == NULL)
2143 if (package != NULL)
2145 std::string n1 = Gogo::message_name(package->name());
2146 std::string n2 = Gogo::message_name(id);
2149 ("invalid reference to unexported identifier "
2151 n1.c_str(), n2.c_str());
2154 "reference to undefined identifier %<%s.%s%>",
2155 n1.c_str(), n2.c_str());
2156 return Expression::make_error(location);
2159 named_object = this->gogo_->add_unknown_name(packed, location);
2162 if (in_function != NULL
2163 && in_function != this->gogo_->current_function()
2164 && (named_object->is_variable()
2165 || named_object->is_result_variable()))
2166 return this->enclosing_var_reference(in_function, named_object,
2169 switch (named_object->classification())
2171 case Named_object::NAMED_OBJECT_CONST:
2172 return Expression::make_const_reference(named_object, location);
2173 case Named_object::NAMED_OBJECT_TYPE:
2174 return Expression::make_type(named_object->type_value(), location);
2175 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2177 Type* t = Type::make_forward_declaration(named_object);
2178 return Expression::make_type(t, location);
2180 case Named_object::NAMED_OBJECT_VAR:
2181 case Named_object::NAMED_OBJECT_RESULT_VAR:
2182 return Expression::make_var_reference(named_object, location);
2183 case Named_object::NAMED_OBJECT_SINK:
2185 return Expression::make_sink(location);
2188 error_at(location, "cannot use _ as value");
2189 return Expression::make_error(location);
2191 case Named_object::NAMED_OBJECT_FUNC:
2192 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2193 return Expression::make_func_reference(named_object, NULL,
2195 case Named_object::NAMED_OBJECT_UNKNOWN:
2196 return Expression::make_unknown_reference(named_object, location);
2203 case Token::TOKEN_STRING:
2204 ret = Expression::make_string(token->string_value(), token->location());
2205 this->advance_token();
2208 case Token::TOKEN_INTEGER:
2209 ret = Expression::make_integer(token->integer_value(), NULL,
2211 this->advance_token();
2214 case Token::TOKEN_FLOAT:
2215 ret = Expression::make_float(token->float_value(), NULL,
2217 this->advance_token();
2220 case Token::TOKEN_IMAGINARY:
2223 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2224 ret = Expression::make_complex(&zero, token->imaginary_value(),
2225 NULL, token->location());
2227 this->advance_token();
2231 case Token::TOKEN_KEYWORD:
2232 switch (token->keyword())
2235 return this->function_lit();
2237 case KEYWORD_INTERFACE:
2239 case KEYWORD_STRUCT:
2241 source_location location = token->location();
2242 return Expression::make_type(this->type(), location);
2249 case Token::TOKEN_OPERATOR:
2250 if (token->is_op(OPERATOR_LPAREN))
2252 this->advance_token();
2253 ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2254 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2255 error_at(this->location(), "missing %<)%>");
2257 this->advance_token();
2260 else if (token->is_op(OPERATOR_LSQUARE))
2262 // Here we call array_type directly, as this is the only
2263 // case where an ellipsis is permitted for an array type.
2264 source_location location = token->location();
2265 return Expression::make_type(this->array_type(true), location);
2273 error_at(this->location(), "expected operand");
2274 return Expression::make_error(this->location());
2277 // Handle a reference to a variable in an enclosing function. We add
2278 // it to a list of such variables. We return a reference to a field
2279 // in a struct which will be passed on the static chain when calling
2280 // the current function.
2283 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2284 source_location location)
2286 gcc_assert(var->is_variable() || var->is_result_variable());
2288 Named_object* this_function = this->gogo_->current_function();
2289 Named_object* closure = this_function->func_value()->closure_var();
2291 Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2292 std::pair<Enclosing_vars::iterator, bool> ins =
2293 this->enclosing_vars_.insert(ev);
2296 // This is a variable we have not seen before. Add a new field
2297 // to the closure type.
2298 this_function->func_value()->add_closure_field(var, location);
2301 Expression* closure_ref = Expression::make_var_reference(closure,
2303 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2305 // The closure structure holds pointers to the variables, so we need
2306 // to introduce an indirection.
2307 Expression* e = Expression::make_field_reference(closure_ref,
2310 e = Expression::make_unary(OPERATOR_MULT, e, location);
2314 // CompositeLit = LiteralType LiteralValue .
2315 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2316 // SliceType | MapType | TypeName .
2317 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2318 // ElementList = Element { "," Element } .
2319 // Element = [ Key ":" ] Value .
2320 // Key = Expression .
2321 // Value = Expression | LiteralValue .
2323 // We have already seen the type if there is one, and we are now
2324 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2325 // will be seen here as an array type whose length is "nil". The
2326 // DEPTH parameter is non-zero if this is an embedded composite
2327 // literal and the type was omitted. It gives the number of steps up
2328 // to the type which was provided. E.g., in [][]int{{1}} it will be
2329 // 1. In [][][]int{{{1}}} it will be 2.
2332 Parse::composite_lit(Type* type, int depth, source_location location)
2334 gcc_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2335 this->advance_token();
2337 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2339 this->advance_token();
2340 return Expression::make_composite_literal(type, depth, false, NULL,
2344 bool has_keys = false;
2345 Expression_list* vals = new Expression_list;
2349 bool is_type_omitted = false;
2351 const Token* token = this->peek_token();
2353 if (!token->is_op(OPERATOR_LCURLY))
2354 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2357 // This must be a composite literal inside another composite
2358 // literal, with the type omitted for the inner one.
2359 val = this->composite_lit(type, depth + 1, token->location());
2360 is_type_omitted = true;
2363 token = this->peek_token();
2364 if (!token->is_op(OPERATOR_COLON))
2367 vals->push_back(NULL);
2371 if (is_type_omitted && !val->is_error_expression())
2373 error_at(this->location(), "unexpected %<:%>");
2374 val = Expression::make_error(this->location());
2377 this->advance_token();
2379 if (!has_keys && !vals->empty())
2381 Expression_list* newvals = new Expression_list;
2382 for (Expression_list::const_iterator p = vals->begin();
2386 newvals->push_back(NULL);
2387 newvals->push_back(*p);
2394 if (val->unknown_expression() != NULL)
2395 val->unknown_expression()->set_is_composite_literal_key();
2397 vals->push_back(val);
2399 if (!token->is_op(OPERATOR_LCURLY))
2400 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2403 // This must be a composite literal inside another
2404 // composite literal, with the type omitted for the
2406 val = this->composite_lit(type, depth + 1, token->location());
2409 token = this->peek_token();
2412 vals->push_back(val);
2414 if (token->is_op(OPERATOR_COMMA))
2416 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2418 this->advance_token();
2422 else if (token->is_op(OPERATOR_RCURLY))
2424 this->advance_token();
2429 error_at(this->location(), "expected %<,%> or %<}%>");
2432 while (!token->is_eof()
2433 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2435 if (token->is_op(OPERATOR_LCURLY))
2437 else if (token->is_op(OPERATOR_RCURLY))
2439 token = this->advance_token();
2441 if (token->is_op(OPERATOR_RCURLY))
2442 this->advance_token();
2444 return Expression::make_error(location);
2448 return Expression::make_composite_literal(type, depth, has_keys, vals,
2452 // FunctionLit = "func" Signature Block .
2455 Parse::function_lit()
2457 source_location location = this->location();
2458 gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2459 this->advance_token();
2461 Enclosing_vars hold_enclosing_vars;
2462 hold_enclosing_vars.swap(this->enclosing_vars_);
2464 Function_type* type = this->signature(NULL, location);
2466 // For a function literal, the next token must be a '{'. If we
2467 // don't see that, then we may have a type expression.
2468 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2469 return Expression::make_type(type, location);
2471 Named_object* no = this->gogo_->start_function("", type, true, location);
2473 source_location end_loc = this->block();
2475 this->gogo_->finish_function(end_loc);
2477 hold_enclosing_vars.swap(this->enclosing_vars_);
2479 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2482 return Expression::make_func_reference(no, closure, location);
2485 // Create a closure for the nested function FUNCTION. This is based
2486 // on ENCLOSING_VARS, which is a list of all variables defined in
2487 // enclosing functions and referenced from FUNCTION. A closure is the
2488 // address of a struct which contains the addresses of all the
2489 // referenced variables. This returns NULL if no closure is required.
2492 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2493 source_location location)
2495 if (enclosing_vars->empty())
2498 // Get the variables in order by their field index.
2500 size_t enclosing_var_count = enclosing_vars->size();
2501 std::vector<Enclosing_var> ev(enclosing_var_count);
2502 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2503 p != enclosing_vars->end();
2505 ev[p->index()] = *p;
2507 // Build an initializer for a composite literal of the closure's
2510 Named_object* enclosing_function = this->gogo_->current_function();
2511 Expression_list* initializer = new Expression_list;
2512 for (size_t i = 0; i < enclosing_var_count; ++i)
2514 gcc_assert(ev[i].index() == i);
2515 Named_object* var = ev[i].var();
2517 if (ev[i].in_function() == enclosing_function)
2518 ref = Expression::make_var_reference(var, location);
2520 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2522 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2524 initializer->push_back(refaddr);
2527 Named_object* closure_var = function->func_value()->closure_var();
2528 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2529 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2531 return Expression::make_heap_composite(cv, location);
2534 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2536 // If MAY_BE_SINK is true, this expression may be "_".
2538 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2541 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2542 // guard (var := expr.("type") using the literal keyword "type").
2545 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2546 bool* is_type_switch)
2548 source_location start_loc = this->location();
2549 bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2551 Expression* ret = this->operand(may_be_sink);
2553 // An unknown name followed by a curly brace must be a composite
2554 // literal, and the unknown name must be a type.
2555 if (may_be_composite_lit
2556 && !is_parenthesized
2557 && ret->unknown_expression() != NULL
2558 && this->peek_token()->is_op(OPERATOR_LCURLY))
2560 Named_object* no = ret->unknown_expression()->named_object();
2561 Type* type = Type::make_forward_declaration(no);
2562 ret = Expression::make_type(type, ret->location());
2565 // We handle composite literals and type casts here, as it is the
2566 // easiest way to handle types which are in parentheses, as in
2568 if (ret->is_type_expression())
2570 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2572 if (is_parenthesized)
2574 "cannot parenthesize type in composite literal");
2575 ret = this->composite_lit(ret->type(), 0, ret->location());
2577 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2579 source_location loc = this->location();
2580 this->advance_token();
2581 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2583 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2584 error_at(this->location(), "expected %<)%>");
2586 this->advance_token();
2587 if (expr->is_error_expression())
2589 ret = Expression::make_cast(ret->type(), expr, loc);
2595 const Token* token = this->peek_token();
2596 if (token->is_op(OPERATOR_LPAREN))
2597 ret = this->call(this->verify_not_sink(ret));
2598 else if (token->is_op(OPERATOR_DOT))
2600 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2601 if (is_type_switch != NULL && *is_type_switch)
2604 else if (token->is_op(OPERATOR_LSQUARE))
2605 ret = this->index(this->verify_not_sink(ret));
2613 // Selector = "." identifier .
2614 // TypeGuard = "." "(" QualifiedIdent ")" .
2616 // Note that Operand can expand to QualifiedIdent, which contains a
2617 // ".". That is handled directly in operand when it sees a package
2620 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2621 // guard (var := expr.("type") using the literal keyword "type").
2624 Parse::selector(Expression* left, bool* is_type_switch)
2626 gcc_assert(this->peek_token()->is_op(OPERATOR_DOT));
2627 source_location location = this->location();
2629 const Token* token = this->advance_token();
2630 if (token->is_identifier())
2632 // This could be a field in a struct, or a method in an
2633 // interface, or a method associated with a type. We can't know
2634 // which until we have seen all the types.
2636 this->gogo_->pack_hidden_name(token->identifier(),
2637 token->is_identifier_exported());
2638 if (token->identifier() == "_")
2640 error_at(this->location(), "invalid use of %<_%>");
2641 name = this->gogo_->pack_hidden_name("blank", false);
2643 this->advance_token();
2644 return Expression::make_selector(left, name, location);
2646 else if (token->is_op(OPERATOR_LPAREN))
2648 this->advance_token();
2650 if (is_type_switch == NULL
2651 || !this->peek_token()->is_keyword(KEYWORD_TYPE))
2652 type = this->type();
2655 *is_type_switch = true;
2656 this->advance_token();
2658 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2659 error_at(this->location(), "missing %<)%>");
2661 this->advance_token();
2662 if (is_type_switch != NULL && *is_type_switch)
2664 return Expression::make_type_guard(left, type, location);
2668 error_at(this->location(), "expected identifier or %<(%>");
2673 // Index = "[" Expression "]" .
2674 // Slice = "[" Expression ":" [ Expression ] "]" .
2677 Parse::index(Expression* expr)
2679 source_location location = this->location();
2680 gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
2681 this->advance_token();
2684 if (!this->peek_token()->is_op(OPERATOR_COLON))
2685 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2689 mpz_init_set_ui(zero, 0);
2690 start = Expression::make_integer(&zero, NULL, location);
2694 Expression* end = NULL;
2695 if (this->peek_token()->is_op(OPERATOR_COLON))
2697 // We use nil to indicate a missing high expression.
2698 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
2699 end = Expression::make_nil(this->location());
2701 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2703 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
2704 error_at(this->location(), "missing %<]%>");
2706 this->advance_token();
2707 return Expression::make_index(expr, start, end, location);
2710 // Call = "(" [ ArgumentList [ "," ] ] ")" .
2711 // ArgumentList = ExpressionList [ "..." ] .
2714 Parse::call(Expression* func)
2716 gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2717 Expression_list* args = NULL;
2718 bool is_varargs = false;
2719 const Token* token = this->advance_token();
2720 if (!token->is_op(OPERATOR_RPAREN))
2722 args = this->expression_list(NULL, false);
2723 token = this->peek_token();
2724 if (token->is_op(OPERATOR_ELLIPSIS))
2727 token = this->advance_token();
2730 if (token->is_op(OPERATOR_COMMA))
2731 token = this->advance_token();
2732 if (!token->is_op(OPERATOR_RPAREN))
2733 error_at(this->location(), "missing %<)%>");
2735 this->advance_token();
2736 if (func->is_error_expression())
2738 return Expression::make_call(func, args, is_varargs, func->location());
2741 // Return an expression for a single unqualified identifier.
2744 Parse::id_to_expression(const std::string& name, source_location location)
2746 Named_object* in_function;
2747 Named_object* named_object = this->gogo_->lookup(name, &in_function);
2748 if (named_object == NULL)
2749 named_object = this->gogo_->add_unknown_name(name, location);
2751 if (in_function != NULL
2752 && in_function != this->gogo_->current_function()
2753 && (named_object->is_variable() || named_object->is_result_variable()))
2754 return this->enclosing_var_reference(in_function, named_object,
2757 switch (named_object->classification())
2759 case Named_object::NAMED_OBJECT_CONST:
2760 return Expression::make_const_reference(named_object, location);
2761 case Named_object::NAMED_OBJECT_VAR:
2762 case Named_object::NAMED_OBJECT_RESULT_VAR:
2763 return Expression::make_var_reference(named_object, location);
2764 case Named_object::NAMED_OBJECT_SINK:
2765 return Expression::make_sink(location);
2766 case Named_object::NAMED_OBJECT_FUNC:
2767 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2768 return Expression::make_func_reference(named_object, NULL, location);
2769 case Named_object::NAMED_OBJECT_UNKNOWN:
2770 return Expression::make_unknown_reference(named_object, location);
2772 error_at(this->location(), "unexpected type of identifier");
2773 return Expression::make_error(location);
2777 // Expression = UnaryExpr { binary_op Expression } .
2779 // PRECEDENCE is the precedence of the current operator.
2781 // If MAY_BE_SINK is true, this expression may be "_".
2783 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2786 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2787 // guard (var := expr.("type") using the literal keyword "type").
2790 Parse::expression(Precedence precedence, bool may_be_sink,
2791 bool may_be_composite_lit, bool* is_type_switch)
2793 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
2798 if (is_type_switch != NULL && *is_type_switch)
2801 const Token* token = this->peek_token();
2802 if (token->classification() != Token::TOKEN_OPERATOR)
2808 Precedence right_precedence;
2809 switch (token->op())
2812 right_precedence = PRECEDENCE_OROR;
2814 case OPERATOR_ANDAND:
2815 right_precedence = PRECEDENCE_ANDAND;
2817 case OPERATOR_CHANOP:
2818 right_precedence = PRECEDENCE_CHANOP;
2821 case OPERATOR_NOTEQ:
2826 right_precedence = PRECEDENCE_RELOP;
2829 case OPERATOR_MINUS:
2832 right_precedence = PRECEDENCE_ADDOP;
2837 case OPERATOR_LSHIFT:
2838 case OPERATOR_RSHIFT:
2840 case OPERATOR_BITCLEAR:
2841 right_precedence = PRECEDENCE_MULOP;
2844 right_precedence = PRECEDENCE_INVALID;
2848 if (right_precedence == PRECEDENCE_INVALID)
2854 Operator op = token->op();
2855 source_location binop_location = token->location();
2857 if (precedence >= right_precedence)
2859 // We've already seen A * B, and we see + C. We want to
2860 // return so that A * B becomes a group.
2864 this->advance_token();
2866 left = this->verify_not_sink(left);
2867 Expression* right = this->expression(right_precedence, false,
2868 may_be_composite_lit,
2870 if (op == OPERATOR_CHANOP)
2871 left = Expression::make_send(left, right, binop_location);
2873 left = Expression::make_binary(op, left, right, binop_location);
2878 Parse::expression_may_start_here()
2880 const Token* token = this->peek_token();
2881 switch (token->classification())
2883 case Token::TOKEN_INVALID:
2884 case Token::TOKEN_EOF:
2886 case Token::TOKEN_KEYWORD:
2887 switch (token->keyword())
2892 case KEYWORD_STRUCT:
2893 case KEYWORD_INTERFACE:
2898 case Token::TOKEN_IDENTIFIER:
2900 case Token::TOKEN_STRING:
2902 case Token::TOKEN_OPERATOR:
2903 switch (token->op())
2906 case OPERATOR_MINUS:
2910 case OPERATOR_CHANOP:
2912 case OPERATOR_LPAREN:
2913 case OPERATOR_LSQUARE:
2918 case Token::TOKEN_INTEGER:
2919 case Token::TOKEN_FLOAT:
2920 case Token::TOKEN_IMAGINARY:
2927 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
2929 // If MAY_BE_SINK is true, this expression may be "_".
2931 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2934 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2935 // guard (var := expr.("type") using the literal keyword "type").
2938 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
2939 bool* is_type_switch)
2941 const Token* token = this->peek_token();
2942 if (token->is_op(OPERATOR_PLUS)
2943 || token->is_op(OPERATOR_MINUS)
2944 || token->is_op(OPERATOR_NOT)
2945 || token->is_op(OPERATOR_XOR)
2946 || token->is_op(OPERATOR_CHANOP)
2947 || token->is_op(OPERATOR_MULT)
2948 || token->is_op(OPERATOR_AND))
2950 source_location location = token->location();
2951 Operator op = token->op();
2952 this->advance_token();
2954 if (op == OPERATOR_CHANOP
2955 && this->peek_token()->is_keyword(KEYWORD_CHAN))
2957 // This is "<- chan" which must be the start of a type.
2958 this->unget_token(Token::make_operator_token(op, location));
2959 return Expression::make_type(this->type(), location);
2962 Expression* expr = this->unary_expr(false, may_be_composite_lit,
2964 if (expr->is_error_expression())
2966 else if (op == OPERATOR_MULT && expr->is_type_expression())
2967 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
2969 else if (op == OPERATOR_AND && expr->is_composite_literal())
2970 expr = Expression::make_heap_composite(expr, location);
2971 else if (op != OPERATOR_CHANOP)
2972 expr = Expression::make_unary(op, expr, location);
2974 expr = Expression::make_receive(expr, location);
2978 return this->primary_expr(may_be_sink, may_be_composite_lit,
2983 // Declaration | LabeledStmt | SimpleStmt |
2984 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
2985 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
2988 // LABEL is the label of this statement if it has one.
2991 Parse::statement(const Label* label)
2993 const Token* token = this->peek_token();
2994 switch (token->classification())
2996 case Token::TOKEN_KEYWORD:
2998 switch (token->keyword())
3003 this->declaration();
3007 case KEYWORD_STRUCT:
3008 case KEYWORD_INTERFACE:
3009 this->simple_stat(true, false, NULL, NULL);
3013 this->go_or_defer_stat();
3015 case KEYWORD_RETURN:
3016 this->return_stat();
3021 case KEYWORD_CONTINUE:
3022 this->continue_stat();
3030 case KEYWORD_SWITCH:
3031 this->switch_stat(label);
3033 case KEYWORD_SELECT:
3034 this->select_stat(label);
3037 this->for_stat(label);
3040 error_at(this->location(), "expected statement");
3041 this->advance_token();
3047 case Token::TOKEN_IDENTIFIER:
3049 std::string identifier = token->identifier();
3050 bool is_exported = token->is_identifier_exported();
3051 source_location location = token->location();
3052 if (this->advance_token()->is_op(OPERATOR_COLON))
3054 this->advance_token();
3055 this->labeled_stmt(identifier, location);
3059 this->unget_token(Token::make_identifier_token(identifier,
3062 this->simple_stat(true, false, NULL, NULL);
3067 case Token::TOKEN_OPERATOR:
3068 if (token->is_op(OPERATOR_LCURLY))
3070 source_location location = token->location();
3071 this->gogo_->start_block(location);
3072 source_location end_loc = this->block();
3073 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3076 else if (!token->is_op(OPERATOR_SEMICOLON))
3077 this->simple_stat(true, false, NULL, NULL);
3080 case Token::TOKEN_STRING:
3081 case Token::TOKEN_INTEGER:
3082 case Token::TOKEN_FLOAT:
3083 case Token::TOKEN_IMAGINARY:
3084 this->simple_stat(true, false, NULL, NULL);
3088 error_at(this->location(), "expected statement");
3089 this->advance_token();
3095 Parse::statement_may_start_here()
3097 const Token* token = this->peek_token();
3098 switch (token->classification())
3100 case Token::TOKEN_KEYWORD:
3102 switch (token->keyword())
3109 case KEYWORD_STRUCT:
3110 case KEYWORD_INTERFACE:
3113 case KEYWORD_RETURN:
3115 case KEYWORD_CONTINUE:
3118 case KEYWORD_SWITCH:
3119 case KEYWORD_SELECT:
3129 case Token::TOKEN_IDENTIFIER:
3132 case Token::TOKEN_OPERATOR:
3133 if (token->is_op(OPERATOR_LCURLY)
3134 || token->is_op(OPERATOR_SEMICOLON))
3137 return this->expression_may_start_here();
3139 case Token::TOKEN_STRING:
3140 case Token::TOKEN_INTEGER:
3141 case Token::TOKEN_FLOAT:
3142 case Token::TOKEN_IMAGINARY:
3150 // LabeledStmt = Label ":" Statement .
3151 // Label = identifier .
3154 Parse::labeled_stmt(const std::string& label_name, source_location location)
3156 Label* label = this->gogo_->add_label_definition(label_name, location);
3158 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3160 // This is a label at the end of a block. A program is
3161 // permitted to omit a semicolon here.
3165 if (!this->statement_may_start_here())
3167 error_at(location, "missing statement after label");
3168 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3173 this->statement(label);
3177 // ExpressionStat | IncDecStat | Assignment | SimpleVarDecl .
3179 // In order to make this work for if and switch statements, if
3180 // RETURN_EXP is true, and we see an ExpressionStat, we return the
3181 // expression rather than adding an expression statement to the
3182 // current block. If we see something other than an ExpressionStat,
3183 // we add the statement and return NULL.
3185 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3188 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3189 // guard (var := expr.("type") using the literal keyword "type").
3192 Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
3193 Range_clause* p_range_clause, Type_switch* p_type_switch)
3195 const Token* token = this->peek_token();
3197 // An identifier follow by := is a SimpleVarDecl.
3198 if (token->is_identifier())
3200 std::string identifier = token->identifier();
3201 bool is_exported = token->is_identifier_exported();
3202 source_location location = token->location();
3204 token = this->advance_token();
3205 if (token->is_op(OPERATOR_COLONEQ)
3206 || token->is_op(OPERATOR_COMMA))
3208 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3209 this->simple_var_decl_or_assignment(identifier, location,
3211 (token->is_op(OPERATOR_COLONEQ)
3217 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3221 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3222 may_be_composite_lit,
3223 (p_type_switch == NULL
3225 : &p_type_switch->found));
3226 if (p_type_switch != NULL && p_type_switch->found)
3228 p_type_switch->name.clear();
3229 p_type_switch->location = exp->location();
3230 p_type_switch->expr = this->verify_not_sink(exp);
3233 token = this->peek_token();
3234 if (token->is_op(OPERATOR_PLUSPLUS) || token->is_op(OPERATOR_MINUSMINUS))
3235 this->inc_dec_stat(this->verify_not_sink(exp));
3236 else if (token->is_op(OPERATOR_COMMA)
3237 || token->is_op(OPERATOR_EQ))
3238 this->assignment(exp, p_range_clause);
3239 else if (token->is_op(OPERATOR_PLUSEQ)
3240 || token->is_op(OPERATOR_MINUSEQ)
3241 || token->is_op(OPERATOR_OREQ)
3242 || token->is_op(OPERATOR_XOREQ)
3243 || token->is_op(OPERATOR_MULTEQ)
3244 || token->is_op(OPERATOR_DIVEQ)
3245 || token->is_op(OPERATOR_MODEQ)
3246 || token->is_op(OPERATOR_LSHIFTEQ)
3247 || token->is_op(OPERATOR_RSHIFTEQ)
3248 || token->is_op(OPERATOR_ANDEQ)
3249 || token->is_op(OPERATOR_BITCLEAREQ))
3250 this->assignment(this->verify_not_sink(exp), p_range_clause);
3251 else if (return_exp)
3252 return this->verify_not_sink(exp);
3254 this->expression_stat(this->verify_not_sink(exp));
3260 Parse::simple_stat_may_start_here()
3262 return this->expression_may_start_here();
3265 // Parse { Statement ";" } which is used in a few places. The list of
3266 // statements may end with a right curly brace, in which case the
3267 // semicolon may be omitted.
3270 Parse::statement_list()
3272 while (this->statement_may_start_here())
3274 this->statement(NULL);
3275 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3276 this->advance_token();
3277 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3281 if (!this->peek_token()->is_eof() || !saw_errors())
3282 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3283 if (!this->skip_past_error(OPERATOR_RCURLY))
3290 Parse::statement_list_may_start_here()
3292 return this->statement_may_start_here();
3295 // ExpressionStat = Expression .
3298 Parse::expression_stat(Expression* exp)
3300 exp->discarding_value();
3301 this->gogo_->add_statement(Statement::make_statement(exp));
3304 // IncDecStat = Expression ( "++" | "--" ) .
3307 Parse::inc_dec_stat(Expression* exp)
3309 const Token* token = this->peek_token();
3311 // Lvalue maps require special handling.
3312 if (exp->index_expression() != NULL)
3313 exp->index_expression()->set_is_lvalue();
3315 if (token->is_op(OPERATOR_PLUSPLUS))
3316 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3317 else if (token->is_op(OPERATOR_MINUSMINUS))
3318 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3321 this->advance_token();
3324 // Assignment = ExpressionList assign_op ExpressionList .
3326 // EXP is an expression that we have already parsed.
3328 // If RANGE_CLAUSE is not NULL, then this will recognize a
3332 Parse::assignment(Expression* expr, Range_clause* p_range_clause)
3334 Expression_list* vars;
3335 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3337 vars = new Expression_list();
3338 vars->push_back(expr);
3342 this->advance_token();
3343 vars = this->expression_list(expr, true);
3346 this->tuple_assignment(vars, p_range_clause);
3349 // An assignment statement. LHS is the list of expressions which
3350 // appear on the left hand side.
3352 // If RANGE_CLAUSE is not NULL, then this will recognize a
3356 Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
3358 const Token* token = this->peek_token();
3359 if (!token->is_op(OPERATOR_EQ)
3360 && !token->is_op(OPERATOR_PLUSEQ)
3361 && !token->is_op(OPERATOR_MINUSEQ)
3362 && !token->is_op(OPERATOR_OREQ)
3363 && !token->is_op(OPERATOR_XOREQ)
3364 && !token->is_op(OPERATOR_MULTEQ)
3365 && !token->is_op(OPERATOR_DIVEQ)
3366 && !token->is_op(OPERATOR_MODEQ)
3367 && !token->is_op(OPERATOR_LSHIFTEQ)
3368 && !token->is_op(OPERATOR_RSHIFTEQ)
3369 && !token->is_op(OPERATOR_ANDEQ)
3370 && !token->is_op(OPERATOR_BITCLEAREQ))
3372 error_at(this->location(), "expected assignment operator");
3375 Operator op = token->op();
3376 source_location location = token->location();
3378 token = this->advance_token();
3380 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
3382 if (op != OPERATOR_EQ)
3383 error_at(this->location(), "range clause requires %<=%>");
3384 this->range_clause_expr(lhs, p_range_clause);
3388 Expression_list* vals = this->expression_list(NULL, false);
3390 // We've parsed everything; check for errors.
3391 if (lhs == NULL || vals == NULL)
3393 for (Expression_list::const_iterator pe = lhs->begin();
3397 if ((*pe)->is_error_expression())
3399 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
3400 error_at((*pe)->location(), "cannot use _ as value");
3402 for (Expression_list::const_iterator pe = vals->begin();
3406 if ((*pe)->is_error_expression())
3410 // Map expressions act differently when they are lvalues.
3411 for (Expression_list::iterator plv = lhs->begin();
3414 if ((*plv)->index_expression() != NULL)
3415 (*plv)->index_expression()->set_is_lvalue();
3417 Call_expression* call;
3418 Index_expression* map_index;
3419 Receive_expression* receive;
3420 Type_guard_expression* type_guard;
3421 if (lhs->size() == vals->size())
3424 if (lhs->size() > 1)
3426 if (op != OPERATOR_EQ)
3427 error_at(location, "multiple values only permitted with %<=%>");
3428 s = Statement::make_tuple_assignment(lhs, vals, location);
3432 if (op == OPERATOR_EQ)
3433 s = Statement::make_assignment(lhs->front(), vals->front(),
3436 s = Statement::make_assignment_operation(op, lhs->front(),
3437 vals->front(), location);
3441 this->gogo_->add_statement(s);
3443 else if (vals->size() == 1
3444 && (call = (*vals->begin())->call_expression()) != NULL)
3446 if (op != OPERATOR_EQ)
3447 error_at(location, "multiple results only permitted with %<=%>");
3449 vals = new Expression_list;
3450 for (unsigned int i = 0; i < lhs->size(); ++i)
3451 vals->push_back(Expression::make_call_result(call, i));
3452 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
3453 this->gogo_->add_statement(s);
3455 else if (lhs->size() == 2
3456 && vals->size() == 1
3457 && (map_index = (*vals->begin())->index_expression()) != NULL)
3459 if (op != OPERATOR_EQ)
3460 error_at(location, "two values from map requires %<=%>");
3461 Expression* val = lhs->front();
3462 Expression* present = lhs->back();
3463 Statement* s = Statement::make_tuple_map_assignment(val, present,
3464 map_index, location);
3465 this->gogo_->add_statement(s);
3467 else if (lhs->size() == 1
3468 && vals->size() == 2
3469 && (map_index = lhs->front()->index_expression()) != NULL)
3471 if (op != OPERATOR_EQ)
3472 error_at(location, "assigning tuple to map index requires %<=%>");
3473 Expression* val = vals->front();
3474 Expression* should_set = vals->back();
3475 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
3477 this->gogo_->add_statement(s);
3479 else if (lhs->size() == 2
3480 && vals->size() == 1
3481 && (receive = (*vals->begin())->receive_expression()) != NULL)
3483 if (op != OPERATOR_EQ)
3484 error_at(location, "two values from receive requires %<=%>");
3485 Expression* val = lhs->front();
3486 Expression* success = lhs->back();
3487 Expression* channel = receive->channel();
3488 Statement* s = Statement::make_tuple_receive_assignment(val, success,
3491 this->gogo_->add_statement(s);
3493 else if (lhs->size() == 2
3494 && vals->size() == 1
3495 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
3497 if (op != OPERATOR_EQ)
3498 error_at(location, "two values from type guard requires %<=%>");
3499 Expression* val = lhs->front();
3500 Expression* ok = lhs->back();
3501 Expression* expr = type_guard->expr();
3502 Type* type = type_guard->type();
3503 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
3506 this->gogo_->add_statement(s);
3510 error_at(location, "number of variables does not match number of values");
3514 // GoStat = "go" Expression .
3515 // DeferStat = "defer" Expression .
3518 Parse::go_or_defer_stat()
3520 gcc_assert(this->peek_token()->is_keyword(KEYWORD_GO)
3521 || this->peek_token()->is_keyword(KEYWORD_DEFER));
3522 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
3523 source_location stat_location = this->location();
3524 this->advance_token();
3525 source_location expr_location = this->location();
3526 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3527 Call_expression* call_expr = expr->call_expression();
3528 if (call_expr == NULL)
3530 error_at(expr_location, "expected call expression");
3534 // Make it easier to simplify go/defer statements by putting every
3535 // statement in its own block.
3536 this->gogo_->start_block(stat_location);
3539 stat = Statement::make_go_statement(call_expr, stat_location);
3541 stat = Statement::make_defer_statement(call_expr, stat_location);
3542 this->gogo_->add_statement(stat);
3543 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
3547 // ReturnStat = "return" [ ExpressionList ] .
3550 Parse::return_stat()
3552 gcc_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
3553 source_location location = this->location();
3554 this->advance_token();
3555 Expression_list* vals = NULL;
3556 if (this->expression_may_start_here())
3557 vals = this->expression_list(NULL, false);
3558 const Function* function = this->gogo_->current_function()->func_value();
3559 const Typed_identifier_list* results = function->type()->results();
3560 this->gogo_->add_statement(Statement::make_return_statement(results, vals,
3564 // IfStat = "if" [ [ SimpleStat ] ";" ] [ Condition ]
3565 // Block [ "else" Statement ] .
3570 gcc_assert(this->peek_token()->is_keyword(KEYWORD_IF));
3571 source_location location = this->location();
3572 this->advance_token();
3574 this->gogo_->start_block(location);
3576 Expression* cond = NULL;
3577 if (this->simple_stat_may_start_here())
3578 cond = this->simple_stat(false, true, NULL, NULL);
3579 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3581 // The SimpleStat is an expression statement.
3582 this->expression_stat(cond);
3587 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3588 this->advance_token();
3589 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3590 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
3593 this->gogo_->start_block(this->location());
3594 source_location end_loc = this->block();
3595 Block* then_block = this->gogo_->finish_block(end_loc);
3597 // Check for the easy error of a newline before "else".
3598 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3600 source_location semi_loc = this->location();
3601 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
3602 error_at(this->location(),
3603 "unexpected semicolon or newline before %<else%>");
3605 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3609 Block* else_block = NULL;
3610 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
3612 this->advance_token();
3613 // We create a block to gather the statement.
3614 this->gogo_->start_block(this->location());
3615 this->statement(NULL);
3616 else_block = this->gogo_->finish_block(this->location());
3619 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
3623 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3627 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
3628 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
3629 // "{" { ExprCaseClause } "}" .
3630 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
3631 // "{" { TypeCaseClause } "}" .
3632 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
3635 Parse::switch_stat(const Label* label)
3637 gcc_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
3638 source_location location = this->location();
3639 this->advance_token();
3641 this->gogo_->start_block(location);
3643 Expression* switch_val = NULL;
3644 Type_switch type_switch;
3645 if (this->simple_stat_may_start_here())
3646 switch_val = this->simple_stat(false, true, NULL, &type_switch);
3647 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3649 // The SimpleStat is an expression statement.
3650 this->expression_stat(switch_val);
3653 if (switch_val == NULL && !type_switch.found)
3655 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3656 this->advance_token();
3657 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3659 if (this->peek_token()->is_identifier())
3661 const Token* token = this->peek_token();
3662 std::string identifier = token->identifier();
3663 bool is_exported = token->is_identifier_exported();
3664 source_location id_loc = token->location();
3666 token = this->advance_token();
3667 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
3668 this->unget_token(Token::make_identifier_token(identifier,
3673 // This must be a TypeSwitchGuard.
3674 switch_val = this->simple_stat(false, true, NULL,
3676 if (!type_switch.found
3677 && !switch_val->is_error_expression())
3679 error_at(id_loc, "expected type switch assignment");
3680 switch_val = Expression::make_error(id_loc);
3684 if (switch_val == NULL && !type_switch.found)
3686 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
3687 &type_switch.found);
3688 if (type_switch.found)
3690 type_switch.name.clear();
3691 type_switch.expr = switch_val;
3692 type_switch.location = switch_val->location();
3698 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3700 source_location token_loc = this->location();
3701 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
3702 && this->advance_token()->is_op(OPERATOR_LCURLY))
3703 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
3706 error_at(this->location(), "expected %<{%>");
3707 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3712 this->advance_token();
3714 Statement* statement;
3715 if (type_switch.found)
3716 statement = this->type_switch_body(label, type_switch, location);
3718 statement = this->expr_switch_body(label, switch_val, location);
3720 if (statement != NULL)
3721 this->gogo_->add_statement(statement);
3723 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3727 // The body of an expression switch.
3728 // "{" { ExprCaseClause } "}"
3731 Parse::expr_switch_body(const Label* label, Expression* switch_val,
3732 source_location location)
3734 Switch_statement* statement = Statement::make_switch_statement(switch_val,
3737 this->push_break_statement(statement, label);
3739 Case_clauses* case_clauses = new Case_clauses();
3740 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
3742 if (this->peek_token()->is_eof())
3745 error_at(this->location(), "missing %<}%>");
3748 this->expr_case_clause(case_clauses);
3750 this->advance_token();
3752 statement->add_clauses(case_clauses);
3754 this->pop_break_statement();
3759 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
3760 // FallthroughStat = "fallthrough" .
3763 Parse::expr_case_clause(Case_clauses* clauses)
3765 source_location location = this->location();
3767 bool is_default = false;
3768 Expression_list* vals = this->expr_switch_case(&is_default);
3770 if (!this->peek_token()->is_op(OPERATOR_COLON))
3773 error_at(this->location(), "expected %<:%>");
3777 this->advance_token();
3779 Block* statements = NULL;
3780 if (this->statement_list_may_start_here())
3782 this->gogo_->start_block(this->location());
3783 this->statement_list();
3784 statements = this->gogo_->finish_block(this->location());
3787 bool is_fallthrough = false;
3788 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
3790 is_fallthrough = true;
3791 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
3792 this->advance_token();
3795 if (is_default || vals != NULL)
3796 clauses->add(vals, is_default, statements, is_fallthrough, location);
3799 // ExprSwitchCase = "case" ExpressionList | "default" .
3802 Parse::expr_switch_case(bool* is_default)
3804 const Token* token = this->peek_token();
3805 if (token->is_keyword(KEYWORD_CASE))
3807 this->advance_token();
3808 return this->expression_list(NULL, false);
3810 else if (token->is_keyword(KEYWORD_DEFAULT))
3812 this->advance_token();
3819 error_at(this->location(), "expected %<case%> or %<default%>");
3820 if (!token->is_op(OPERATOR_RCURLY))
3821 this->advance_token();
3826 // The body of a type switch.
3827 // "{" { TypeCaseClause } "}" .
3830 Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
3831 source_location location)
3833 Named_object* switch_no = NULL;
3834 if (!type_switch.name.empty())
3836 Variable* switch_var = new Variable(NULL, type_switch.expr, false, false,
3837 false, type_switch.location);
3838 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
3841 Type_switch_statement* statement =
3842 Statement::make_type_switch_statement(switch_no,
3848 this->push_break_statement(statement, label);
3850 Type_case_clauses* case_clauses = new Type_case_clauses();
3851 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
3853 if (this->peek_token()->is_eof())
3855 error_at(this->location(), "missing %<}%>");
3858 this->type_case_clause(switch_no, case_clauses);
3860 this->advance_token();
3862 statement->add_clauses(case_clauses);
3864 this->pop_break_statement();
3869 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
3872 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses)
3874 source_location location = this->location();
3876 std::vector<Type*> types;
3877 bool is_default = false;
3878 this->type_switch_case(&types, &is_default);
3880 if (!this->peek_token()->is_op(OPERATOR_COLON))
3881 error_at(this->location(), "expected %<:%>");
3883 this->advance_token();
3885 Block* statements = NULL;
3886 if (this->statement_list_may_start_here())
3888 this->gogo_->start_block(this->location());
3889 if (switch_no != NULL && types.size() == 1)
3891 Type* type = types.front();
3892 Expression* init = Expression::make_var_reference(switch_no,
3894 init = Expression::make_type_guard(init, type, location);
3895 Variable* v = new Variable(type, init, false, false, false,
3897 v->set_is_type_switch_var();
3898 this->gogo_->add_variable(switch_no->name(), v);
3900 this->statement_list();
3901 statements = this->gogo_->finish_block(this->location());
3904 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
3906 error_at(this->location(),
3907 "fallthrough is not permitted in a type switch");
3908 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
3909 this->advance_token();
3914 gcc_assert(types.empty());
3915 clauses->add(NULL, false, true, statements, location);
3917 else if (!types.empty())
3919 for (std::vector<Type*>::const_iterator p = types.begin();
3920 p + 1 != types.end();
3922 clauses->add(*p, true, false, NULL, location);
3923 clauses->add(types.back(), false, false, statements, location);
3927 // TypeSwitchCase = "case" type | "default"
3929 // We accept a comma separated list of types.
3932 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
3934 const Token* token = this->peek_token();
3935 if (token->is_keyword(KEYWORD_CASE))
3937 this->advance_token();
3940 Type* t = this->type();
3941 if (!t->is_error_type())
3942 types->push_back(t);
3943 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3945 this->advance_token();
3948 else if (token->is_keyword(KEYWORD_DEFAULT))
3950 this->advance_token();
3955 error_at(this->location(), "expected %<case%> or %<default%>");
3956 if (!token->is_op(OPERATOR_RCURLY))
3957 this->advance_token();
3961 // SelectStat = "select" "{" { CommClause } "}" .
3964 Parse::select_stat(const Label* label)
3966 gcc_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
3967 source_location location = this->location();
3968 const Token* token = this->advance_token();
3970 if (!token->is_op(OPERATOR_LCURLY))
3972 source_location token_loc = token->location();
3973 if (token->is_op(OPERATOR_SEMICOLON)
3974 && this->advance_token()->is_op(OPERATOR_LCURLY))
3975 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
3978 error_at(this->location(), "expected %<{%>");
3982 this->advance_token();
3984 Select_statement* statement = Statement::make_select_statement(location);
3986 this->push_break_statement(statement, label);
3988 Select_clauses* select_clauses = new Select_clauses();
3989 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
3991 if (this->peek_token()->is_eof())
3993 error_at(this->location(), "expected %<}%>");
3996 this->comm_clause(select_clauses);
3999 this->advance_token();
4001 statement->add_clauses(select_clauses);
4003 this->pop_break_statement();
4005 this->gogo_->add_statement(statement);
4008 // CommClause = CommCase [ StatementList ] .
4011 Parse::comm_clause(Select_clauses* clauses)
4013 source_location location = this->location();
4014 bool is_send = false;
4015 Expression* channel = NULL;
4016 Expression* val = NULL;
4017 std::string varname;
4018 bool is_default = false;
4019 bool got_case = this->comm_case(&is_send, &channel, &val, &varname,
4022 Block* statements = NULL;
4023 Named_object* var = NULL;
4024 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4025 this->advance_token();
4026 else if (this->statement_list_may_start_here())
4028 this->gogo_->start_block(this->location());
4030 if (!varname.empty())
4032 // FIXME: LOCATION is slightly wrong here.
4033 Variable* v = new Variable(NULL, channel, false, false, false,
4035 v->set_type_from_chan_element();
4036 var = this->gogo_->add_variable(varname, v);
4039 this->statement_list();
4040 statements = this->gogo_->finish_block(this->location());
4044 clauses->add(is_send, channel, val, var, is_default, statements, location);
4047 // CommCase = ( "default" | ( "case" ( SendExpr | RecvExpr) ) ) ":" .
4050 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4051 std::string* varname, bool* is_default)
4053 const Token* token = this->peek_token();
4054 if (token->is_keyword(KEYWORD_DEFAULT))
4056 this->advance_token();
4059 else if (token->is_keyword(KEYWORD_CASE))
4061 this->advance_token();
4062 if (!this->send_or_recv_expr(is_send, channel, val, varname))
4067 error_at(this->location(), "expected %<case%> or %<default%>");
4068 if (!token->is_op(OPERATOR_RCURLY))
4069 this->advance_token();
4073 if (!this->peek_token()->is_op(OPERATOR_COLON))
4075 error_at(this->location(), "expected colon");
4079 this->advance_token();
4084 // SendExpr = Expression "<-" Expression .
4085 // RecvExpr = [ Expression ( "=" | ":=" ) ] "<-" Expression .
4088 Parse::send_or_recv_expr(bool* is_send, Expression** channel, Expression** val,
4089 std::string* varname)
4091 const Token* token = this->peek_token();
4092 source_location location = token->location();
4093 if (token->is_identifier())
4095 std::string recv_var = token->identifier();
4096 bool is_var_exported = token->is_identifier_exported();
4097 if (!this->advance_token()->is_op(OPERATOR_COLONEQ))
4098 this->unget_token(Token::make_identifier_token(recv_var,
4103 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4105 error_at(this->location(), "expected %<<-%>");
4109 *varname = this->gogo_->pack_hidden_name(recv_var, is_var_exported);
4110 this->advance_token();
4111 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4116 if (this->peek_token()->is_op(OPERATOR_CHANOP))
4119 this->advance_token();
4120 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4124 Expression* left = this->expression(PRECEDENCE_CHANOP, true, true, NULL);
4126 if (this->peek_token()->is_op(OPERATOR_EQ))
4128 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4130 error_at(this->location(), "missing %<<-%>");
4135 this->advance_token();
4136 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4138 else if (this->peek_token()->is_op(OPERATOR_CHANOP))
4141 *channel = this->verify_not_sink(left);
4142 this->advance_token();
4143 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4147 error_at(this->location(), "expected %<<-%> or %<=%>");
4155 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
4156 // Condition = Expression .
4159 Parse::for_stat(const Label* label)
4161 gcc_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
4162 source_location location = this->location();
4163 const Token* token = this->advance_token();
4165 // Open a block to hold any variables defined in the init statement
4166 // of the for statement.
4167 this->gogo_->start_block(location);
4170 Expression* cond = NULL;
4172 Range_clause range_clause;
4174 if (!token->is_op(OPERATOR_LCURLY))
4176 if (token->is_keyword(KEYWORD_VAR))
4178 error_at(this->location(),
4179 "var declaration not allowed in for initializer");
4183 if (token->is_op(OPERATOR_SEMICOLON))
4184 this->for_clause(&cond, &post);
4187 // We might be looking at a Condition, an InitStat, or a
4189 cond = this->simple_stat(false, true, &range_clause, NULL);
4190 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4192 if (cond == NULL && !range_clause.found)
4193 error_at(this->location(), "parse error in for statement");
4197 if (range_clause.found)
4198 error_at(this->location(), "parse error after range clause");
4202 // COND is actually an expression statement for
4203 // InitStat at the start of a ForClause.
4204 this->expression_stat(cond);
4208 this->for_clause(&cond, &post);
4213 // Build the For_statement and note that it is the current target
4214 // for break and continue statements.
4216 For_statement* sfor;
4217 For_range_statement* srange;
4219 if (!range_clause.found)
4221 sfor = Statement::make_for_statement(init, cond, post, location);
4227 srange = Statement::make_for_range_statement(range_clause.index,
4235 this->push_break_statement(s, label);
4236 this->push_continue_statement(s, label);
4238 // Gather the block of statements in the loop and add them to the
4241 this->gogo_->start_block(this->location());
4242 source_location end_loc = this->block();
4243 Block* statements = this->gogo_->finish_block(end_loc);
4246 sfor->add_statements(statements);
4248 srange->add_statements(statements);
4250 // This is no longer the break/continue target.
4251 this->pop_break_statement();
4252 this->pop_continue_statement();
4254 // Add the For_statement to the list of statements, and close out
4255 // the block we started to hold any variables defined in the for
4258 this->gogo_->add_statement(s);
4260 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4264 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
4265 // InitStat = SimpleStat .
4266 // PostStat = SimpleStat .
4268 // We have already read InitStat at this point.
4271 Parse::for_clause(Expression** cond, Block** post)
4273 gcc_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
4274 this->advance_token();
4275 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4277 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
4279 error_at(this->location(),
4280 "unexpected semicolon or newline before %<{%>");
4286 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4287 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4288 error_at(this->location(), "expected semicolon");
4290 this->advance_token();
4292 if (this->peek_token()->is_op(OPERATOR_LCURLY))
4296 this->gogo_->start_block(this->location());
4297 this->simple_stat(false, false, NULL, NULL);
4298 *post = this->gogo_->finish_block(this->location());
4302 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
4304 // This is the := version. It is called with a list of identifiers.
4307 Parse::range_clause_decl(const Typed_identifier_list* til,
4308 Range_clause* p_range_clause)
4310 gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4311 source_location location = this->location();
4313 p_range_clause->found = true;
4315 gcc_assert(til->size() >= 1);
4316 if (til->size() > 2)
4317 error_at(this->location(), "too many variables for range clause");
4319 this->advance_token();
4320 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
4321 p_range_clause->range = expr;
4323 bool any_new = false;
4325 const Typed_identifier* pti = &til->front();
4326 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new);
4327 if (any_new && no->is_variable())
4328 no->var_value()->set_type_from_range_index();
4329 p_range_clause->index = Expression::make_var_reference(no, location);
4331 if (til->size() == 1)
4332 p_range_clause->value = NULL;
4336 bool is_new = false;
4337 no = this->init_var(*pti, NULL, expr, true, true, &is_new);
4338 if (is_new && no->is_variable())
4339 no->var_value()->set_type_from_range_value();
4342 p_range_clause->value = Expression::make_var_reference(no, location);
4346 error_at(location, "variables redeclared but no variable is new");
4349 // The = version of RangeClause. This is called with a list of
4353 Parse::range_clause_expr(const Expression_list* vals,
4354 Range_clause* p_range_clause)
4356 gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4358 p_range_clause->found = true;
4360 gcc_assert(vals->size() >= 1);
4361 if (vals->size() > 2)
4362 error_at(this->location(), "too many variables for range clause");
4364 this->advance_token();
4365 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
4368 p_range_clause->index = vals->front();
4369 if (vals->size() == 1)
4370 p_range_clause->value = NULL;
4372 p_range_clause->value = vals->back();
4375 // Push a statement on the break stack.
4378 Parse::push_break_statement(Statement* enclosing, const Label* label)
4380 this->break_stack_.push_back(std::make_pair(enclosing, label));
4383 // Push a statement on the continue stack.
4386 Parse::push_continue_statement(Statement* enclosing, const Label* label)
4388 this->continue_stack_.push_back(std::make_pair(enclosing, label));
4391 // Pop the break stack.
4394 Parse::pop_break_statement()
4396 this->break_stack_.pop_back();
4399 // Pop the continue stack.
4402 Parse::pop_continue_statement()
4404 this->continue_stack_.pop_back();
4407 // Find a break or continue statement given a label name.
4410 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
4412 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
4413 p != bc_stack->rend();
4415 if (p->second != NULL && p->second->name() == label)
4420 // BreakStat = "break" [ identifier ] .
4425 gcc_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
4426 source_location location = this->location();
4428 const Token* token = this->advance_token();
4429 Statement* enclosing;
4430 if (!token->is_identifier())
4432 if (this->break_stack_.empty())
4434 error_at(this->location(),
4435 "break statement not within for or switch or select");
4438 enclosing = this->break_stack_.back().first;
4442 enclosing = this->find_bc_statement(&this->break_stack_,
4443 token->identifier());
4444 if (enclosing == NULL)
4446 error_at(token->location(),
4447 ("break label %qs not associated with "
4448 "for or switch or select"),
4449 Gogo::message_name(token->identifier()).c_str());
4450 this->advance_token();
4453 this->advance_token();
4456 Unnamed_label* label;
4457 if (enclosing->classification() == Statement::STATEMENT_FOR)
4458 label = enclosing->for_statement()->break_label();
4459 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
4460 label = enclosing->for_range_statement()->break_label();
4461 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
4462 label = enclosing->switch_statement()->break_label();
4463 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
4464 label = enclosing->type_switch_statement()->break_label();
4465 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
4466 label = enclosing->select_statement()->break_label();
4470 this->gogo_->add_statement(Statement::make_break_statement(label,
4474 // ContinueStat = "continue" [ identifier ] .
4477 Parse::continue_stat()
4479 gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
4480 source_location location = this->location();
4482 const Token* token = this->advance_token();
4483 Statement* enclosing;
4484 if (!token->is_identifier())
4486 if (this->continue_stack_.empty())
4488 error_at(this->location(), "continue statement not within for");
4491 enclosing = this->continue_stack_.back().first;
4495 enclosing = this->find_bc_statement(&this->continue_stack_,
4496 token->identifier());
4497 if (enclosing == NULL)
4499 error_at(token->location(),
4500 "continue label %qs not associated with for",
4501 Gogo::message_name(token->identifier()).c_str());
4502 this->advance_token();
4505 this->advance_token();
4508 Unnamed_label* label;
4509 if (enclosing->classification() == Statement::STATEMENT_FOR)
4510 label = enclosing->for_statement()->continue_label();
4511 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
4512 label = enclosing->for_range_statement()->continue_label();
4516 this->gogo_->add_statement(Statement::make_continue_statement(label,
4520 // GotoStat = "goto" identifier .
4525 gcc_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
4526 source_location location = this->location();
4527 const Token* token = this->advance_token();
4528 if (!token->is_identifier())
4529 error_at(this->location(), "expected label for goto");
4532 Label* label = this->gogo_->add_label_reference(token->identifier());
4533 Statement* s = Statement::make_goto_statement(label, location);
4534 this->gogo_->add_statement(s);
4535 this->advance_token();
4539 // PackageClause = "package" PackageName .
4542 Parse::package_clause()
4544 const Token* token = this->peek_token();
4545 source_location location = token->location();
4547 if (!token->is_keyword(KEYWORD_PACKAGE))
4549 error_at(this->location(), "program must start with package clause");
4554 token = this->advance_token();
4555 if (token->is_identifier())
4557 name = token->identifier();
4560 error_at(this->location(), "invalid package name _");
4563 this->advance_token();
4567 error_at(this->location(), "package name must be an identifier");
4571 this->gogo_->set_package_name(name, location);
4574 // ImportDecl = "import" Decl<ImportSpec> .
4577 Parse::import_decl()
4579 gcc_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
4580 this->advance_token();
4581 this->decl(&Parse::import_spec, NULL);
4584 // ImportSpec = [ "." | PackageName ] PackageFileName .
4587 Parse::import_spec(void*)
4589 const Token* token = this->peek_token();
4590 source_location location = token->location();
4592 std::string local_name;
4593 bool is_local_name_exported = false;
4594 if (token->is_op(OPERATOR_DOT))
4597 token = this->advance_token();
4599 else if (token->is_identifier())
4601 local_name = token->identifier();
4602 is_local_name_exported = token->is_identifier_exported();
4603 token = this->advance_token();
4606 if (!token->is_string())
4608 error_at(this->location(), "missing import package name");
4612 this->gogo_->import_package(token->string_value(), local_name,
4613 is_local_name_exported, location);
4615 this->advance_token();
4618 // SourceFile = PackageClause ";" { ImportDecl ";" }
4619 // { TopLevelDecl ";" } .
4624 this->package_clause();
4626 const Token* token = this->peek_token();
4627 if (token->is_op(OPERATOR_SEMICOLON))
4628 token = this->advance_token();
4630 error_at(this->location(),
4631 "expected %<;%> or newline after package clause");
4633 while (token->is_keyword(KEYWORD_IMPORT))
4635 this->import_decl();
4636 token = this->peek_token();
4637 if (token->is_op(OPERATOR_SEMICOLON))
4638 token = this->advance_token();
4640 error_at(this->location(),
4641 "expected %<;%> or newline after import declaration");
4644 while (!token->is_eof())
4646 if (this->declaration_may_start_here())
4647 this->declaration();
4650 error_at(this->location(), "expected declaration");
4652 this->advance_token();
4653 while (!this->peek_token()->is_eof()
4654 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
4655 && !this->peek_token()->is_op(OPERATOR_RCURLY));
4656 if (!this->peek_token()->is_eof()
4657 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
4658 this->advance_token();
4660 token = this->peek_token();
4661 if (token->is_op(OPERATOR_SEMICOLON))
4662 token = this->advance_token();
4663 else if (!token->is_eof() || !saw_errors())
4665 error_at(this->location(),
4666 "expected %<;%> or newline after top level declaration");
4667 this->skip_past_error(OPERATOR_INVALID);
4672 // Reset the current iota value.
4680 // Return the current iota value.
4688 // Increment the current iota value.
4691 Parse::increment_iota()
4696 // Skip forward to a semicolon or OP. OP will normally be
4697 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
4698 // past it and return. If we find OP, it will be the next token to
4699 // read. Return true if we are OK, false if we found EOF.
4702 Parse::skip_past_error(Operator op)
4704 const Token* token = this->peek_token();
4705 while (!token->is_op(op))
4707 if (token->is_eof())
4709 if (token->is_op(OPERATOR_SEMICOLON))
4711 this->advance_token();
4714 token = this->advance_token();
4719 // Check that an expression is not a sink.
4722 Parse::verify_not_sink(Expression* expr)
4724 if (expr->is_sink_expression())
4726 error_at(expr->location(), "cannot use _ as value");
4727 expr = Expression::make_error(expr->location());