1 // parse.cc -- Go frontend parser.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 #include "statements.h"
13 #include "expressions.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
22 const Enclosing_var& v2)
24 if (v1.var() == v2.var())
27 const std::string& n1(v1.var()->name());
28 const std::string& n2(v2.var()->name());
29 int i = n1.compare(n2);
35 // If we get here it means that a single nested function refers to
36 // two different variables defined in enclosing functions, and both
37 // variables have the same name. I think this is impossible.
43 Parse::Parse(Lex* lex, Gogo* gogo)
45 token_(Token::make_invalid_token(0)),
46 unget_token_(Token::make_invalid_token(0)),
47 unget_token_valid_(false),
50 continue_stack_(NULL),
56 // Return the current token.
61 if (this->unget_token_valid_)
62 return &this->unget_token_;
63 if (this->token_.is_invalid())
64 this->token_ = this->lex_->next_token();
68 // Advance to the next token and return it.
71 Parse::advance_token()
73 if (this->unget_token_valid_)
75 this->unget_token_valid_ = false;
76 if (!this->token_.is_invalid())
79 this->token_ = this->lex_->next_token();
83 // Push a token back on the input stream.
86 Parse::unget_token(const Token& token)
88 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 Type* type = this->signature(NULL, location);
249 return Type::make_error_type();
252 else if (token->is_keyword(KEYWORD_MAP))
253 return this->map_type();
254 else if (token->is_keyword(KEYWORD_STRUCT))
255 return this->struct_type();
256 else if (token->is_op(OPERATOR_MULT))
257 return this->pointer_type();
258 else if (token->is_op(OPERATOR_LPAREN))
260 this->advance_token();
261 Type* ret = this->type();
262 if (this->peek_token()->is_op(OPERATOR_RPAREN))
263 this->advance_token();
266 if (!ret->is_error_type())
267 error_at(this->location(), "expected %<)%>");
273 error_at(token->location(), "expected type");
274 return Type::make_error_type();
279 Parse::type_may_start_here()
281 const Token* token = this->peek_token();
282 return (token->is_identifier()
283 || token->is_op(OPERATOR_LSQUARE)
284 || token->is_op(OPERATOR_CHANOP)
285 || token->is_keyword(KEYWORD_CHAN)
286 || token->is_keyword(KEYWORD_INTERFACE)
287 || token->is_keyword(KEYWORD_FUNC)
288 || token->is_keyword(KEYWORD_MAP)
289 || token->is_keyword(KEYWORD_STRUCT)
290 || token->is_op(OPERATOR_MULT)
291 || token->is_op(OPERATOR_LPAREN));
294 // TypeName = QualifiedIdent .
296 // If MAY_BE_NIL is true, then an identifier with the value of the
297 // predefined constant nil is accepted, returning the nil type.
300 Parse::type_name(bool issue_error)
302 source_location location = this->location();
305 Named_object* package;
306 if (!this->qualified_ident(&name, &package))
307 return Type::make_error_type();
309 Named_object* named_object;
311 named_object = this->gogo_->lookup(name, NULL);
314 named_object = package->package_value()->lookup(name);
315 if (named_object == NULL
317 && package->name() != this->gogo_->package_name())
319 // Check whether the name is there but hidden.
320 std::string s = ('.' + package->package_value()->unique_prefix()
321 + '.' + package->package_value()->name()
323 named_object = package->package_value()->lookup(s);
324 if (named_object != NULL)
326 const std::string& packname(package->package_value()->name());
327 error_at(location, "invalid reference to hidden type %<%s.%s%>",
328 Gogo::message_name(packname).c_str(),
329 Gogo::message_name(name).c_str());
336 if (named_object == NULL)
341 named_object = this->gogo_->add_unknown_name(name, location);
343 else if (named_object->is_type())
345 if (!named_object->type_value()->is_visible())
348 else if (named_object->is_unknown() || named_object->is_type_declaration())
356 error_at(location, "expected type");
357 return Type::make_error_type();
360 if (named_object->is_type())
361 return named_object->type_value();
362 else if (named_object->is_unknown() || named_object->is_type_declaration())
363 return Type::make_forward_declaration(named_object);
368 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
369 // ArrayLength = Expression .
370 // ElementType = CompleteType .
373 Parse::array_type(bool may_use_ellipsis)
375 gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
376 const Token* token = this->advance_token();
378 Expression* length = NULL;
379 if (token->is_op(OPERATOR_RSQUARE))
380 this->advance_token();
383 if (!token->is_op(OPERATOR_ELLIPSIS))
384 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
385 else if (may_use_ellipsis)
387 // An ellipsis is used in composite literals to represent a
388 // fixed array of the size of the number of elements. We
389 // use a length of nil to represent this, and change the
390 // length when parsing the composite literal.
391 length = Expression::make_nil(this->location());
392 this->advance_token();
396 error_at(this->location(),
397 "use of %<[...]%> outside of array literal");
398 length = Expression::make_error(this->location());
399 this->advance_token();
401 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
403 error_at(this->location(), "expected %<]%>");
404 return Type::make_error_type();
406 this->advance_token();
409 Type* element_type = this->type();
411 return Type::make_array_type(element_type, length);
414 // MapType = "map" "[" KeyType "]" ValueType .
415 // KeyType = CompleteType .
416 // ValueType = CompleteType .
421 source_location location = this->location();
422 gcc_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
423 if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
425 error_at(this->location(), "expected %<[%>");
426 return Type::make_error_type();
428 this->advance_token();
430 Type* key_type = this->type();
432 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
434 error_at(this->location(), "expected %<]%>");
435 return Type::make_error_type();
437 this->advance_token();
439 Type* value_type = this->type();
441 if (key_type->is_error_type() || value_type->is_error_type())
442 return Type::make_error_type();
444 return Type::make_map_type(key_type, value_type, location);
447 // StructType = "struct" "{" { FieldDecl ";" } "}" .
452 gcc_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
453 source_location location = this->location();
454 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
456 source_location token_loc = this->location();
457 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
458 && this->advance_token()->is_op(OPERATOR_LCURLY))
459 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
462 error_at(this->location(), "expected %<{%>");
463 return Type::make_error_type();
466 this->advance_token();
468 Struct_field_list* sfl = new Struct_field_list;
469 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
471 this->field_decl(sfl);
472 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
473 this->advance_token();
474 else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
476 error_at(this->location(), "expected %<;%> or %<}%> or newline");
477 if (!this->skip_past_error(OPERATOR_RCURLY))
478 return Type::make_error_type();
481 this->advance_token();
483 for (Struct_field_list::const_iterator pi = sfl->begin();
487 if (pi->type()->is_error_type())
489 for (Struct_field_list::const_iterator pj = pi + 1;
493 if (pi->field_name() == pj->field_name()
494 && !Gogo::is_sink_name(pi->field_name()))
495 error_at(pi->location(), "duplicate field name %<%s%>",
496 Gogo::message_name(pi->field_name()).c_str());
500 return Type::make_struct_type(sfl, location);
503 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
504 // Tag = string_lit .
507 Parse::field_decl(Struct_field_list* sfl)
509 const Token* token = this->peek_token();
510 source_location location = token->location();
512 bool is_anonymous_pointer;
513 if (token->is_op(OPERATOR_MULT))
516 is_anonymous_pointer = true;
518 else if (token->is_identifier())
520 std::string id = token->identifier();
521 bool is_id_exported = token->is_identifier_exported();
522 source_location id_location = token->location();
523 token = this->advance_token();
524 is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
525 || token->is_op(OPERATOR_RCURLY)
526 || token->is_op(OPERATOR_DOT)
527 || token->is_string());
528 is_anonymous_pointer = false;
529 this->unget_token(Token::make_identifier_token(id, is_id_exported,
534 error_at(this->location(), "expected field name");
535 while (!token->is_op(OPERATOR_SEMICOLON)
536 && !token->is_op(OPERATOR_RCURLY)
538 token = this->advance_token();
544 if (is_anonymous_pointer)
546 this->advance_token();
547 if (!this->peek_token()->is_identifier())
549 error_at(this->location(), "expected field name");
550 while (!token->is_op(OPERATOR_SEMICOLON)
551 && !token->is_op(OPERATOR_RCURLY)
553 token = this->advance_token();
557 Type* type = this->type_name(true);
560 if (this->peek_token()->is_string())
562 tag = this->peek_token()->string_value();
563 this->advance_token();
566 if (!type->is_error_type())
568 if (is_anonymous_pointer)
569 type = Type::make_pointer_type(type);
570 sfl->push_back(Struct_field(Typed_identifier("", type, location)));
572 sfl->back().set_tag(tag);
577 Typed_identifier_list til;
580 token = this->peek_token();
581 if (!token->is_identifier())
583 error_at(this->location(), "expected identifier");
587 this->gogo_->pack_hidden_name(token->identifier(),
588 token->is_identifier_exported());
589 til.push_back(Typed_identifier(name, NULL, token->location()));
590 if (!this->advance_token()->is_op(OPERATOR_COMMA))
592 this->advance_token();
595 Type* type = this->type();
598 if (this->peek_token()->is_string())
600 tag = this->peek_token()->string_value();
601 this->advance_token();
604 for (Typed_identifier_list::iterator p = til.begin();
609 sfl->push_back(Struct_field(*p));
611 sfl->back().set_tag(tag);
616 // PointerType = "*" Type .
619 Parse::pointer_type()
621 gcc_assert(this->peek_token()->is_op(OPERATOR_MULT));
622 this->advance_token();
623 Type* type = this->type();
624 if (type->is_error_type())
626 return Type::make_pointer_type(type);
629 // ChannelType = Channel | SendChannel | RecvChannel .
630 // Channel = "chan" ElementType .
631 // SendChannel = "chan" "<-" ElementType .
632 // RecvChannel = "<-" "chan" ElementType .
635 Parse::channel_type()
637 const Token* token = this->peek_token();
640 if (token->is_op(OPERATOR_CHANOP))
642 if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
644 error_at(this->location(), "expected %<chan%>");
645 return Type::make_error_type();
648 this->advance_token();
652 gcc_assert(token->is_keyword(KEYWORD_CHAN));
653 if (this->advance_token()->is_op(OPERATOR_CHANOP))
656 this->advance_token();
659 Type* element_type = this->type();
660 return Type::make_channel_type(send, receive, element_type);
663 // Signature = Parameters [ Result ] .
665 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
666 // location of the start of the type.
668 // This returns NULL on a parse error.
671 Parse::signature(Typed_identifier* receiver, source_location location)
673 bool is_varargs = false;
674 Typed_identifier_list* params;
675 bool params_ok = this->parameters(¶ms, &is_varargs);
677 Typed_identifier_list* result = NULL;
678 if (this->peek_token()->is_op(OPERATOR_LPAREN)
679 || this->type_may_start_here())
681 if (!this->result(&result))
688 Function_type* ret = Type::make_function_type(receiver, params, result,
691 ret->set_is_varargs();
695 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
697 // This returns false on a parse error.
700 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
704 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
706 error_at(this->location(), "expected %<(%>");
710 Typed_identifier_list* params = NULL;
711 bool saw_error = false;
713 const Token* token = this->advance_token();
714 if (!token->is_op(OPERATOR_RPAREN))
716 params = this->parameter_list(is_varargs);
719 token = this->peek_token();
722 // The optional trailing comma is picked up in parameter_list.
724 if (!token->is_op(OPERATOR_RPAREN))
725 error_at(this->location(), "expected %<)%>");
727 this->advance_token();
736 // ParameterList = ParameterDecl { "," ParameterDecl } .
738 // This sets *IS_VARARGS if the list ends with an ellipsis.
739 // IS_VARARGS will be NULL if varargs are not permitted.
741 // We pick up an optional trailing comma.
743 // This returns NULL if some error is seen.
745 Typed_identifier_list*
746 Parse::parameter_list(bool* is_varargs)
748 source_location location = this->location();
749 Typed_identifier_list* ret = new Typed_identifier_list();
751 bool saw_error = false;
753 // If we see an identifier and then a comma, then we don't know
754 // whether we are looking at a list of identifiers followed by a
755 // type, or a list of types given by name. We have to do an
756 // arbitrary lookahead to figure it out.
758 bool parameters_have_names;
759 const Token* token = this->peek_token();
760 if (!token->is_identifier())
762 // This must be a type which starts with something like '*'.
763 parameters_have_names = false;
767 std::string name = token->identifier();
768 bool is_exported = token->is_identifier_exported();
769 source_location location = token->location();
770 token = this->advance_token();
771 if (!token->is_op(OPERATOR_COMMA))
773 if (token->is_op(OPERATOR_DOT))
775 // This is a qualified identifier, which must turn out
777 parameters_have_names = false;
779 else if (token->is_op(OPERATOR_RPAREN))
781 // A single identifier followed by a parenthesis must be
783 parameters_have_names = false;
787 // An identifier followed by something other than a
788 // comma or a dot or a right parenthesis must be a
789 // parameter name followed by a type.
790 parameters_have_names = true;
793 this->unget_token(Token::make_identifier_token(name, is_exported,
798 // An identifier followed by a comma may be the first in a
799 // list of parameter names followed by a type, or it may be
800 // the first in a list of types without parameter names. To
801 // find out we gather as many identifiers separated by
803 std::string id_name = this->gogo_->pack_hidden_name(name,
805 ret->push_back(Typed_identifier(id_name, NULL, location));
806 bool just_saw_comma = true;
807 while (this->advance_token()->is_identifier())
809 name = this->peek_token()->identifier();
810 is_exported = this->peek_token()->is_identifier_exported();
811 location = this->peek_token()->location();
812 id_name = this->gogo_->pack_hidden_name(name, is_exported);
813 ret->push_back(Typed_identifier(id_name, NULL, location));
814 if (!this->advance_token()->is_op(OPERATOR_COMMA))
816 just_saw_comma = false;
823 // We saw ID1 "," ID2 "," followed by something which
824 // was not an identifier. We must be seeing the start
825 // of a type, and ID1 and ID2 must be types, and the
826 // parameters don't have names.
827 parameters_have_names = false;
829 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
831 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
832 // and the parameters don't have names.
833 parameters_have_names = false;
835 else if (this->peek_token()->is_op(OPERATOR_DOT))
837 // We saw ID1 "," ID2 ".". ID2 must be a package name,
838 // ID1 must be a type, and the parameters don't have
840 parameters_have_names = false;
841 this->unget_token(Token::make_identifier_token(name, is_exported,
844 just_saw_comma = true;
848 // We saw ID1 "," ID2 followed by something other than
849 // ",", ".", or ")". We must be looking at the start of
850 // a type, and ID1 and ID2 must be parameter names.
851 parameters_have_names = true;
854 if (parameters_have_names)
856 gcc_assert(!just_saw_comma);
857 // We have just seen ID1, ID2 xxx.
859 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
863 error_at(this->location(), "%<...%> only permits one name");
865 this->advance_token();
868 for (size_t i = 0; i < ret->size(); ++i)
869 ret->set_type(i, type);
870 if (!this->peek_token()->is_op(OPERATOR_COMMA))
871 return saw_error ? NULL : ret;
872 if (this->advance_token()->is_op(OPERATOR_RPAREN))
873 return saw_error ? NULL : ret;
877 Typed_identifier_list* tret = new Typed_identifier_list();
878 for (Typed_identifier_list::const_iterator p = ret->begin();
882 Named_object* no = this->gogo_->lookup(p->name(), NULL);
885 no = this->gogo_->add_unknown_name(p->name(),
889 type = no->type_value();
890 else if (no->is_unknown() || no->is_type_declaration())
891 type = Type::make_forward_declaration(no);
894 error_at(p->location(), "expected %<%s%> to be a type",
895 Gogo::message_name(p->name()).c_str());
897 type = Type::make_error_type();
899 tret->push_back(Typed_identifier("", type, p->location()));
904 || this->peek_token()->is_op(OPERATOR_RPAREN))
905 return saw_error ? NULL : ret;
910 bool mix_error = false;
911 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
912 while (this->peek_token()->is_op(OPERATOR_COMMA))
914 if (is_varargs != NULL && *is_varargs)
916 error_at(this->location(), "%<...%> must be last parameter");
919 if (this->advance_token()->is_op(OPERATOR_RPAREN))
921 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
925 error_at(location, "invalid named/anonymous mix");
936 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
939 Parse::parameter_decl(bool parameters_have_names,
940 Typed_identifier_list* til,
944 if (!parameters_have_names)
947 source_location location = this->location();
948 if (!this->peek_token()->is_identifier())
950 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
954 if (is_varargs == NULL)
955 error_at(this->location(), "invalid use of %<...%>");
958 this->advance_token();
959 if (is_varargs == NULL
960 && this->peek_token()->is_op(OPERATOR_RPAREN))
961 type = Type::make_error_type();
964 Type* element_type = this->type();
965 type = Type::make_array_type(element_type, NULL);
971 type = this->type_name(false);
972 if (type->is_error_type()
973 || (!this->peek_token()->is_op(OPERATOR_COMMA)
974 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
977 while (!this->peek_token()->is_op(OPERATOR_COMMA)
978 && !this->peek_token()->is_op(OPERATOR_RPAREN))
979 this->advance_token();
982 if (!type->is_error_type())
983 til->push_back(Typed_identifier("", type, location));
987 size_t orig_count = til->size();
988 if (this->peek_token()->is_identifier())
989 this->identifier_list(til);
992 size_t new_count = til->size();
995 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
999 if (is_varargs == NULL)
1000 error_at(this->location(), "invalid use of %<...%>");
1001 else if (new_count > orig_count + 1)
1002 error_at(this->location(), "%<...%> only permits one name");
1005 this->advance_token();
1006 Type* element_type = this->type();
1007 type = Type::make_array_type(element_type, NULL);
1009 for (size_t i = orig_count; i < new_count; ++i)
1010 til->set_type(i, type);
1014 // Result = Parameters | Type .
1016 // This returns false on a parse error.
1019 Parse::result(Typed_identifier_list** presults)
1021 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1022 return this->parameters(presults, NULL);
1025 source_location location = this->location();
1026 Type* type = this->type();
1027 if (type->is_error_type())
1032 Typed_identifier_list* til = new Typed_identifier_list();
1033 til->push_back(Typed_identifier("", type, location));
1039 // Block = "{" [ StatementList ] "}" .
1041 // Returns the location of the closing brace.
1046 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1048 source_location loc = this->location();
1049 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1050 && this->advance_token()->is_op(OPERATOR_LCURLY))
1051 error_at(loc, "unexpected semicolon or newline before %<{%>");
1054 error_at(this->location(), "expected %<{%>");
1055 return UNKNOWN_LOCATION;
1059 const Token* token = this->advance_token();
1061 if (!token->is_op(OPERATOR_RCURLY))
1063 this->statement_list();
1064 token = this->peek_token();
1065 if (!token->is_op(OPERATOR_RCURLY))
1067 if (!token->is_eof() || !saw_errors())
1068 error_at(this->location(), "expected %<}%>");
1070 // Skip ahead to the end of the block, in hopes of avoiding
1071 // lots of meaningless errors.
1072 source_location ret = token->location();
1074 while (!token->is_eof())
1076 if (token->is_op(OPERATOR_LCURLY))
1078 else if (token->is_op(OPERATOR_RCURLY))
1083 this->advance_token();
1087 token = this->advance_token();
1088 ret = token->location();
1094 source_location ret = token->location();
1095 this->advance_token();
1099 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1100 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1103 Parse::interface_type()
1105 gcc_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1106 source_location location = this->location();
1108 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1110 source_location token_loc = this->location();
1111 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1112 && this->advance_token()->is_op(OPERATOR_LCURLY))
1113 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1116 error_at(this->location(), "expected %<{%>");
1117 return Type::make_error_type();
1120 this->advance_token();
1122 Typed_identifier_list* methods = new Typed_identifier_list();
1123 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1125 this->method_spec(methods);
1126 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1128 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1130 this->method_spec(methods);
1132 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1134 error_at(this->location(), "expected %<}%>");
1135 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1137 if (this->peek_token()->is_eof())
1138 return Type::make_error_type();
1142 this->advance_token();
1144 if (methods->empty())
1150 Interface_type* ret = Type::make_interface_type(methods, location);
1151 this->gogo_->record_interface_type(ret);
1155 // MethodSpec = MethodName Signature | InterfaceTypeName .
1156 // MethodName = identifier .
1157 // InterfaceTypeName = TypeName .
1160 Parse::method_spec(Typed_identifier_list* methods)
1162 const Token* token = this->peek_token();
1163 if (!token->is_identifier())
1165 error_at(this->location(), "expected identifier");
1169 std::string name = token->identifier();
1170 bool is_exported = token->is_identifier_exported();
1171 source_location location = token->location();
1173 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1175 // This is a MethodName.
1176 name = this->gogo_->pack_hidden_name(name, is_exported);
1177 Type* type = this->signature(NULL, location);
1180 methods->push_back(Typed_identifier(name, type, location));
1184 this->unget_token(Token::make_identifier_token(name, is_exported,
1186 Type* type = this->type_name(false);
1187 if (type->is_error_type()
1188 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1189 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1191 if (this->peek_token()->is_op(OPERATOR_COMMA))
1192 error_at(this->location(),
1193 "name list not allowed in interface type");
1195 error_at(location, "expected signature or type name");
1196 token = this->peek_token();
1197 while (!token->is_eof()
1198 && !token->is_op(OPERATOR_SEMICOLON)
1199 && !token->is_op(OPERATOR_RCURLY))
1200 token = this->advance_token();
1203 // This must be an interface type, but we can't check that now.
1204 // We check it and pull out the methods in
1205 // Interface_type::do_verify.
1206 methods->push_back(Typed_identifier("", type, location));
1210 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1213 Parse::declaration()
1215 const Token* token = this->peek_token();
1216 if (token->is_keyword(KEYWORD_CONST))
1218 else if (token->is_keyword(KEYWORD_TYPE))
1220 else if (token->is_keyword(KEYWORD_VAR))
1222 else if (token->is_keyword(KEYWORD_FUNC))
1223 this->function_decl();
1226 error_at(this->location(), "expected declaration");
1227 this->advance_token();
1232 Parse::declaration_may_start_here()
1234 const Token* token = this->peek_token();
1235 return (token->is_keyword(KEYWORD_CONST)
1236 || token->is_keyword(KEYWORD_TYPE)
1237 || token->is_keyword(KEYWORD_VAR)
1238 || token->is_keyword(KEYWORD_FUNC));
1241 // Decl<P> = P | "(" [ List<P> ] ")" .
1244 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1246 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1250 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1252 this->list(pfn, varg, true);
1253 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1255 error_at(this->location(), "missing %<)%>");
1256 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1258 if (this->peek_token()->is_eof())
1263 this->advance_token();
1267 // List<P> = P { ";" P } [ ";" ] .
1269 // In order to pick up the trailing semicolon we need to know what
1270 // might follow. This is either a '}' or a ')'.
1273 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1276 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1277 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1278 || this->peek_token()->is_op(OPERATOR_COMMA))
1280 if (this->peek_token()->is_op(OPERATOR_COMMA))
1281 error_at(this->location(), "unexpected comma");
1282 if (this->advance_token()->is_op(follow))
1288 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1293 gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1294 this->advance_token();
1297 Type* last_type = NULL;
1298 Expression_list* last_expr_list = NULL;
1300 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1301 this->const_spec(&last_type, &last_expr_list);
1304 this->advance_token();
1305 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1307 this->const_spec(&last_type, &last_expr_list);
1308 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1309 this->advance_token();
1310 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1312 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1313 if (!this->skip_past_error(OPERATOR_RPAREN))
1317 this->advance_token();
1320 if (last_expr_list != NULL)
1321 delete last_expr_list;
1324 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1327 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1329 Typed_identifier_list til;
1330 this->identifier_list(&til);
1333 if (this->type_may_start_here())
1335 type = this->type();
1337 *last_expr_list = NULL;
1340 Expression_list *expr_list;
1341 if (!this->peek_token()->is_op(OPERATOR_EQ))
1343 if (*last_expr_list == NULL)
1345 error_at(this->location(), "expected %<=%>");
1349 expr_list = new Expression_list;
1350 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1351 p != (*last_expr_list)->end();
1353 expr_list->push_back((*p)->copy());
1357 this->advance_token();
1358 expr_list = this->expression_list(NULL, false);
1360 if (*last_expr_list != NULL)
1361 delete *last_expr_list;
1362 *last_expr_list = expr_list;
1365 Expression_list::const_iterator pe = expr_list->begin();
1366 for (Typed_identifier_list::iterator pi = til.begin();
1370 if (pe == expr_list->end())
1372 error_at(this->location(), "not enough initializers");
1378 if (!Gogo::is_sink_name(pi->name()))
1379 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1381 if (pe != expr_list->end())
1382 error_at(this->location(), "too many initializers");
1384 this->increment_iota();
1389 // TypeDecl = "type" Decl<TypeSpec> .
1394 gcc_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1395 this->advance_token();
1396 this->decl(&Parse::type_spec, NULL);
1399 // TypeSpec = identifier Type .
1402 Parse::type_spec(void*)
1404 const Token* token = this->peek_token();
1405 if (!token->is_identifier())
1407 error_at(this->location(), "expected identifier");
1410 std::string name = token->identifier();
1411 bool is_exported = token->is_identifier_exported();
1412 source_location location = token->location();
1413 token = this->advance_token();
1415 // The scope of the type name starts at the point where the
1416 // identifier appears in the source code. We implement this by
1417 // declaring the type before we read the type definition.
1418 Named_object* named_type = NULL;
1421 name = this->gogo_->pack_hidden_name(name, is_exported);
1422 named_type = this->gogo_->declare_type(name, location);
1426 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1427 type = this->type();
1430 error_at(this->location(),
1431 "unexpected semicolon or newline in type declaration");
1432 type = Type::make_error_type();
1433 this->advance_token();
1436 if (type->is_error_type())
1438 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1439 && !this->peek_token()->is_eof())
1440 this->advance_token();
1445 if (named_type->is_type_declaration())
1447 Type* ftype = type->forwarded();
1448 if (ftype->forward_declaration_type() != NULL
1449 && (ftype->forward_declaration_type()->named_object()
1452 error_at(location, "invalid recursive type");
1453 type = Type::make_error_type();
1456 this->gogo_->define_type(named_type,
1457 Type::make_named_type(named_type, type,
1459 gcc_assert(named_type->package() == NULL);
1463 // This will probably give a redefinition error.
1464 this->gogo_->add_type(name, type, location);
1469 // VarDecl = "var" Decl<VarSpec> .
1474 gcc_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1475 this->advance_token();
1476 this->decl(&Parse::var_spec, NULL);
1479 // VarSpec = IdentifierList
1480 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1483 Parse::var_spec(void*)
1485 // Get the variable names.
1486 Typed_identifier_list til;
1487 this->identifier_list(&til);
1489 source_location location = this->location();
1492 Expression_list* init = NULL;
1493 if (!this->peek_token()->is_op(OPERATOR_EQ))
1495 type = this->type();
1496 if (type->is_error_type())
1498 while (!this->peek_token()->is_op(OPERATOR_EQ)
1499 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1500 && !this->peek_token()->is_eof())
1501 this->advance_token();
1503 if (this->peek_token()->is_op(OPERATOR_EQ))
1505 this->advance_token();
1506 init = this->expression_list(NULL, false);
1511 this->advance_token();
1512 init = this->expression_list(NULL, false);
1515 this->init_vars(&til, type, init, false, location);
1521 // Create variables. TIL is a list of variable names. If TYPE is not
1522 // NULL, it is the type of all the variables. If INIT is not NULL, it
1523 // is an initializer list for the variables.
1526 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1527 Expression_list* init, bool is_coloneq,
1528 source_location location)
1530 // Check for an initialization which can yield multiple values.
1531 if (init != NULL && init->size() == 1 && til->size() > 1)
1533 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1536 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1539 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1542 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1543 is_coloneq, location))
1547 if (init != NULL && init->size() != til->size())
1549 if (init->empty() || !init->front()->is_error_expression())
1550 error_at(location, "wrong number of initializations");
1553 type = Type::make_error_type();
1556 // Note that INIT was already parsed with the old name bindings, so
1557 // we don't have to worry that it will accidentally refer to the
1558 // newly declared variables.
1560 Expression_list::const_iterator pexpr;
1562 pexpr = init->begin();
1563 bool any_new = false;
1564 for (Typed_identifier_list::const_iterator p = til->begin();
1569 gcc_assert(pexpr != init->end());
1570 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1576 gcc_assert(pexpr == init->end());
1577 if (is_coloneq && !any_new)
1578 error_at(location, "variables redeclared but no variable is new");
1581 // See if we need to initialize a list of variables from a function
1582 // call. This returns true if we have set up the variables and the
1586 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1587 Expression* expr, bool is_coloneq,
1588 source_location location)
1590 Call_expression* call = expr->call_expression();
1594 // This is a function call. We can't check here whether it returns
1595 // the right number of values, but it might. Declare the variables,
1596 // and then assign the results of the call to them.
1598 unsigned int index = 0;
1599 bool any_new = false;
1600 for (Typed_identifier_list::const_iterator pv = vars->begin();
1604 Expression* init = Expression::make_call_result(call, index);
1605 this->init_var(*pv, type, init, is_coloneq, false, &any_new);
1608 if (is_coloneq && !any_new)
1609 error_at(location, "variables redeclared but no variable is new");
1614 // See if we need to initialize a pair of values from a map index
1615 // expression. This returns true if we have set up the variables and
1616 // the initialization.
1619 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1620 Expression* expr, bool is_coloneq,
1621 source_location location)
1623 Index_expression* index = expr->index_expression();
1626 if (vars->size() != 2)
1629 // This is an index which is being assigned to two variables. It
1630 // must be a map index. Declare the variables, and then assign the
1631 // results of the map index.
1632 bool any_new = false;
1633 Typed_identifier_list::const_iterator p = vars->begin();
1634 Expression* init = type == NULL ? index : NULL;
1635 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1636 type == NULL, &any_new);
1637 if (type == NULL && any_new && val_no->is_variable())
1638 val_no->var_value()->set_type_from_init_tuple();
1639 Expression* val_var = Expression::make_var_reference(val_no, location);
1642 Type* var_type = type;
1643 if (var_type == NULL)
1644 var_type = Type::lookup_bool_type();
1645 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1647 Expression* present_var = Expression::make_var_reference(no, location);
1649 if (is_coloneq && !any_new)
1650 error_at(location, "variables redeclared but no variable is new");
1652 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1655 if (!this->gogo_->in_global_scope())
1656 this->gogo_->add_statement(s);
1657 else if (!val_no->is_sink())
1659 if (val_no->is_variable())
1660 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1662 else if (!no->is_sink())
1664 if (no->is_variable())
1665 no->var_value()->add_preinit_statement(this->gogo_, s);
1669 // Execute the map index expression just so that we can fail if
1671 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1673 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1679 // See if we need to initialize a pair of values from a receive
1680 // expression. This returns true if we have set up the variables and
1681 // the initialization.
1684 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1685 Expression* expr, bool is_coloneq,
1686 source_location location)
1688 Receive_expression* receive = expr->receive_expression();
1689 if (receive == NULL)
1691 if (vars->size() != 2)
1694 // This is a receive expression which is being assigned to two
1695 // variables. Declare the variables, and then assign the results of
1697 bool any_new = false;
1698 Typed_identifier_list::const_iterator p = vars->begin();
1699 Expression* init = type == NULL ? receive : NULL;
1700 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1701 type == NULL, &any_new);
1702 if (type == NULL && any_new && val_no->is_variable())
1703 val_no->var_value()->set_type_from_init_tuple();
1704 Expression* val_var = Expression::make_var_reference(val_no, location);
1707 Type* var_type = type;
1708 if (var_type == NULL)
1709 var_type = Type::lookup_bool_type();
1710 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1712 Expression* received_var = Expression::make_var_reference(no, location);
1714 if (is_coloneq && !any_new)
1715 error_at(location, "variables redeclared but no variable is new");
1717 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1722 if (!this->gogo_->in_global_scope())
1723 this->gogo_->add_statement(s);
1724 else if (!val_no->is_sink())
1726 if (val_no->is_variable())
1727 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1729 else if (!no->is_sink())
1731 if (no->is_variable())
1732 no->var_value()->add_preinit_statement(this->gogo_, s);
1736 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1738 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1744 // See if we need to initialize a pair of values from a type guard
1745 // expression. This returns true if we have set up the variables and
1746 // the initialization.
1749 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1750 Type* type, Expression* expr,
1751 bool is_coloneq, source_location location)
1753 Type_guard_expression* type_guard = expr->type_guard_expression();
1754 if (type_guard == NULL)
1756 if (vars->size() != 2)
1759 // This is a type guard expression which is being assigned to two
1760 // variables. Declare the variables, and then assign the results of
1762 bool any_new = false;
1763 Typed_identifier_list::const_iterator p = vars->begin();
1764 Type* var_type = type;
1765 if (var_type == NULL)
1766 var_type = type_guard->type();
1767 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1769 Expression* val_var = Expression::make_var_reference(val_no, location);
1773 if (var_type == NULL)
1774 var_type = Type::lookup_bool_type();
1775 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1777 Expression* ok_var = Expression::make_var_reference(no, location);
1779 Expression* texpr = type_guard->expr();
1780 Type* t = type_guard->type();
1781 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1785 if (is_coloneq && !any_new)
1786 error_at(location, "variables redeclared but no variable is new");
1788 if (!this->gogo_->in_global_scope())
1789 this->gogo_->add_statement(s);
1790 else if (!val_no->is_sink())
1792 if (val_no->is_variable())
1793 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1795 else if (!no->is_sink())
1797 if (no->is_variable())
1798 no->var_value()->add_preinit_statement(this->gogo_, s);
1802 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1803 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1809 // Create a single variable. If IS_COLONEQ is true, we permit
1810 // redeclarations in the same block, and we set *IS_NEW when we find a
1811 // new variable which is not a redeclaration.
1814 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1815 bool is_coloneq, bool type_from_init, bool* is_new)
1817 source_location location = tid.location();
1819 if (Gogo::is_sink_name(tid.name()))
1821 if (!type_from_init && init != NULL)
1823 if (!this->gogo_->in_global_scope())
1824 this->gogo_->add_statement(Statement::make_statement(init));
1826 return this->create_dummy_global(type, init, location);
1828 return this->gogo_->add_sink();
1833 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1835 && (no->is_variable() || no->is_result_variable()))
1837 // INIT may be NULL even when IS_COLONEQ is true for cases
1838 // like v, ok := x.(int).
1839 if (!type_from_init && init != NULL)
1841 Expression *v = Expression::make_var_reference(no, location);
1842 Statement *s = Statement::make_assignment(v, init, location);
1843 this->gogo_->add_statement(s);
1849 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1850 false, false, location);
1851 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1852 if (!no->is_variable())
1854 // The name is already defined, so we just gave an error.
1855 return this->gogo_->add_sink();
1860 // Create a dummy global variable to force an initializer to be run in
1861 // the right place. This is used when a sink variable is initialized
1865 Parse::create_dummy_global(Type* type, Expression* init,
1866 source_location location)
1868 if (type == NULL && init == NULL)
1869 type = Type::lookup_bool_type();
1870 Variable* var = new Variable(type, init, true, false, false, location);
1873 snprintf(buf, sizeof buf, "_.%d", count);
1875 return this->gogo_->add_variable(buf, var);
1878 // SimpleVarDecl = identifier ":=" Expression .
1880 // We've already seen the identifier.
1882 // FIXME: We also have to implement
1883 // IdentifierList ":=" ExpressionList
1884 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1885 // tuple assignments here as well.
1887 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
1890 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
1891 // guard (var := expr.("type") using the literal keyword "type").
1894 Parse::simple_var_decl_or_assignment(const std::string& name,
1895 source_location location,
1896 Range_clause* p_range_clause,
1897 Type_switch* p_type_switch)
1899 Typed_identifier_list til;
1900 til.push_back(Typed_identifier(name, NULL, location));
1902 // We've seen one identifier. If we see a comma now, this could be
1904 if (this->peek_token()->is_op(OPERATOR_COMMA))
1906 gcc_assert(p_type_switch == NULL);
1909 const Token* token = this->advance_token();
1910 if (!token->is_identifier())
1913 std::string id = token->identifier();
1914 bool is_id_exported = token->is_identifier_exported();
1915 source_location id_location = token->location();
1917 token = this->advance_token();
1918 if (!token->is_op(OPERATOR_COMMA))
1920 if (token->is_op(OPERATOR_COLONEQ))
1922 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1923 til.push_back(Typed_identifier(id, NULL, location));
1926 this->unget_token(Token::make_identifier_token(id,
1932 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1933 til.push_back(Typed_identifier(id, NULL, location));
1936 // We have a comma separated list of identifiers in TIL. If the
1937 // next token is COLONEQ, then this is a simple var decl, and we
1938 // have the complete list of identifiers. If the next token is
1939 // not COLONEQ, then the only valid parse is a tuple assignment.
1940 // The list of identifiers we have so far is really a list of
1941 // expressions. There are more expressions following.
1943 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
1945 Expression_list* exprs = new Expression_list;
1946 for (Typed_identifier_list::const_iterator p = til.begin();
1949 exprs->push_back(this->id_to_expression(p->name(),
1952 Expression_list* more_exprs = this->expression_list(NULL, true);
1953 for (Expression_list::const_iterator p = more_exprs->begin();
1954 p != more_exprs->end();
1956 exprs->push_back(*p);
1959 this->tuple_assignment(exprs, p_range_clause);
1964 gcc_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
1965 const Token* token = this->advance_token();
1967 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
1969 this->range_clause_decl(&til, p_range_clause);
1973 Expression_list* init;
1974 if (p_type_switch == NULL)
1975 init = this->expression_list(NULL, false);
1978 bool is_type_switch = false;
1979 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
1983 p_type_switch->found = true;
1984 p_type_switch->name = name;
1985 p_type_switch->location = location;
1986 p_type_switch->expr = expr;
1990 if (!this->peek_token()->is_op(OPERATOR_COMMA))
1992 init = new Expression_list();
1993 init->push_back(expr);
1997 this->advance_token();
1998 init = this->expression_list(expr, false);
2002 this->init_vars(&til, NULL, init, true, location);
2005 // FunctionDecl = "func" identifier Signature [ Block ] .
2006 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2009 // FunctionDecl = "func" identifier Signature
2010 // __asm__ "(" string_lit ")" .
2011 // This extension means a function whose real name is the identifier
2015 Parse::function_decl()
2017 gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2018 source_location location = this->location();
2019 const Token* token = this->advance_token();
2021 Typed_identifier* rec = NULL;
2022 if (token->is_op(OPERATOR_LPAREN))
2024 rec = this->receiver();
2025 token = this->peek_token();
2028 if (!token->is_identifier())
2030 error_at(this->location(), "expected function name");
2035 this->gogo_->pack_hidden_name(token->identifier(),
2036 token->is_identifier_exported());
2038 this->advance_token();
2040 Function_type* fntype = this->signature(rec, this->location());
2044 Named_object* named_object = NULL;
2046 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2048 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2050 error_at(this->location(), "expected %<(%>");
2053 token = this->advance_token();
2054 if (!token->is_string())
2056 error_at(this->location(), "expected string");
2059 std::string asm_name = token->string_value();
2060 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2062 error_at(this->location(), "expected %<)%>");
2065 this->advance_token();
2066 named_object = this->gogo_->declare_function(name, fntype, location);
2067 if (named_object->is_function_declaration())
2068 named_object->func_declaration_value()->set_asm_name(asm_name);
2071 // Check for the easy error of a newline before the opening brace.
2072 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2074 source_location semi_loc = this->location();
2075 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2076 error_at(this->location(),
2077 "unexpected semicolon or newline before %<{%>");
2079 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2083 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2085 if (named_object == NULL)
2086 named_object = this->gogo_->declare_function(name, fntype, location);
2090 this->gogo_->start_function(name, fntype, true, location);
2091 source_location end_loc = this->block();
2092 this->gogo_->finish_function(end_loc);
2096 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2097 // BaseTypeName = identifier .
2102 gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2105 const Token* token = this->advance_token();
2106 source_location location = token->location();
2107 if (!token->is_op(OPERATOR_MULT))
2109 if (!token->is_identifier())
2111 error_at(this->location(), "method has no receiver");
2112 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2113 token = this->advance_token();
2114 if (!token->is_eof())
2115 this->advance_token();
2118 name = token->identifier();
2119 bool is_exported = token->is_identifier_exported();
2120 token = this->advance_token();
2121 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2123 // An identifier followed by something other than a dot or a
2124 // right parenthesis must be a receiver name followed by a
2126 name = this->gogo_->pack_hidden_name(name, is_exported);
2130 // This must be a type name.
2131 this->unget_token(Token::make_identifier_token(name, is_exported,
2133 token = this->peek_token();
2138 // Here the receiver name is in NAME (it is empty if the receiver is
2139 // unnamed) and TOKEN is the first token in the type.
2141 bool is_pointer = false;
2142 if (token->is_op(OPERATOR_MULT))
2145 token = this->advance_token();
2148 if (!token->is_identifier())
2150 error_at(this->location(), "expected receiver name or type");
2151 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2152 while (!token->is_eof())
2154 token = this->advance_token();
2155 if (token->is_op(OPERATOR_LPAREN))
2157 else if (token->is_op(OPERATOR_RPAREN))
2164 if (!token->is_eof())
2165 this->advance_token();
2169 Type* type = this->type_name(true);
2171 if (is_pointer && !type->is_error_type())
2172 type = Type::make_pointer_type(type);
2174 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2175 this->advance_token();
2178 if (this->peek_token()->is_op(OPERATOR_COMMA))
2179 error_at(this->location(), "method has multiple receivers");
2181 error_at(this->location(), "expected %<)%>");
2182 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2183 token = this->advance_token();
2184 if (!token->is_eof())
2185 this->advance_token();
2189 return new Typed_identifier(name, type, location);
2192 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2193 // Literal = BasicLit | CompositeLit | FunctionLit .
2194 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2196 // If MAY_BE_SINK is true, this operand may be "_".
2199 Parse::operand(bool may_be_sink)
2201 const Token* token = this->peek_token();
2203 switch (token->classification())
2205 case Token::TOKEN_IDENTIFIER:
2207 source_location location = token->location();
2208 std::string id = token->identifier();
2209 bool is_exported = token->is_identifier_exported();
2210 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2212 Named_object* in_function;
2213 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2215 Package* package = NULL;
2216 if (named_object != NULL && named_object->is_package())
2218 if (!this->advance_token()->is_op(OPERATOR_DOT)
2219 || !this->advance_token()->is_identifier())
2221 error_at(location, "unexpected reference to package");
2222 return Expression::make_error(location);
2224 package = named_object->package_value();
2225 package->set_used();
2226 id = this->peek_token()->identifier();
2227 is_exported = this->peek_token()->is_identifier_exported();
2228 packed = this->gogo_->pack_hidden_name(id, is_exported);
2229 named_object = package->lookup(packed);
2230 location = this->location();
2231 gcc_assert(in_function == NULL);
2234 this->advance_token();
2236 if (named_object != NULL
2237 && named_object->is_type()
2238 && !named_object->type_value()->is_visible())
2240 gcc_assert(package != NULL);
2241 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2242 Gogo::message_name(package->name()).c_str(),
2243 Gogo::message_name(id).c_str());
2244 return Expression::make_error(location);
2248 if (named_object == NULL)
2250 if (package != NULL)
2252 std::string n1 = Gogo::message_name(package->name());
2253 std::string n2 = Gogo::message_name(id);
2256 ("invalid reference to unexported identifier "
2258 n1.c_str(), n2.c_str());
2261 "reference to undefined identifier %<%s.%s%>",
2262 n1.c_str(), n2.c_str());
2263 return Expression::make_error(location);
2266 named_object = this->gogo_->add_unknown_name(packed, location);
2269 if (in_function != NULL
2270 && in_function != this->gogo_->current_function()
2271 && (named_object->is_variable()
2272 || named_object->is_result_variable()))
2273 return this->enclosing_var_reference(in_function, named_object,
2276 switch (named_object->classification())
2278 case Named_object::NAMED_OBJECT_CONST:
2279 return Expression::make_const_reference(named_object, location);
2280 case Named_object::NAMED_OBJECT_TYPE:
2281 return Expression::make_type(named_object->type_value(), location);
2282 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2284 Type* t = Type::make_forward_declaration(named_object);
2285 return Expression::make_type(t, location);
2287 case Named_object::NAMED_OBJECT_VAR:
2288 case Named_object::NAMED_OBJECT_RESULT_VAR:
2289 return Expression::make_var_reference(named_object, location);
2290 case Named_object::NAMED_OBJECT_SINK:
2292 return Expression::make_sink(location);
2295 error_at(location, "cannot use _ as value");
2296 return Expression::make_error(location);
2298 case Named_object::NAMED_OBJECT_FUNC:
2299 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2300 return Expression::make_func_reference(named_object, NULL,
2302 case Named_object::NAMED_OBJECT_UNKNOWN:
2303 return Expression::make_unknown_reference(named_object, location);
2310 case Token::TOKEN_STRING:
2311 ret = Expression::make_string(token->string_value(), token->location());
2312 this->advance_token();
2315 case Token::TOKEN_INTEGER:
2316 ret = Expression::make_integer(token->integer_value(), NULL,
2318 this->advance_token();
2321 case Token::TOKEN_FLOAT:
2322 ret = Expression::make_float(token->float_value(), NULL,
2324 this->advance_token();
2327 case Token::TOKEN_IMAGINARY:
2330 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2331 ret = Expression::make_complex(&zero, token->imaginary_value(),
2332 NULL, token->location());
2334 this->advance_token();
2338 case Token::TOKEN_KEYWORD:
2339 switch (token->keyword())
2342 return this->function_lit();
2344 case KEYWORD_INTERFACE:
2346 case KEYWORD_STRUCT:
2348 source_location location = token->location();
2349 return Expression::make_type(this->type(), location);
2356 case Token::TOKEN_OPERATOR:
2357 if (token->is_op(OPERATOR_LPAREN))
2359 this->advance_token();
2360 ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2361 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2362 error_at(this->location(), "missing %<)%>");
2364 this->advance_token();
2367 else if (token->is_op(OPERATOR_LSQUARE))
2369 // Here we call array_type directly, as this is the only
2370 // case where an ellipsis is permitted for an array type.
2371 source_location location = token->location();
2372 return Expression::make_type(this->array_type(true), location);
2380 error_at(this->location(), "expected operand");
2381 return Expression::make_error(this->location());
2384 // Handle a reference to a variable in an enclosing function. We add
2385 // it to a list of such variables. We return a reference to a field
2386 // in a struct which will be passed on the static chain when calling
2387 // the current function.
2390 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2391 source_location location)
2393 gcc_assert(var->is_variable() || var->is_result_variable());
2395 Named_object* this_function = this->gogo_->current_function();
2396 Named_object* closure = this_function->func_value()->closure_var();
2398 Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2399 std::pair<Enclosing_vars::iterator, bool> ins =
2400 this->enclosing_vars_.insert(ev);
2403 // This is a variable we have not seen before. Add a new field
2404 // to the closure type.
2405 this_function->func_value()->add_closure_field(var, location);
2408 Expression* closure_ref = Expression::make_var_reference(closure,
2410 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2412 // The closure structure holds pointers to the variables, so we need
2413 // to introduce an indirection.
2414 Expression* e = Expression::make_field_reference(closure_ref,
2417 e = Expression::make_unary(OPERATOR_MULT, e, location);
2421 // CompositeLit = LiteralType LiteralValue .
2422 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2423 // SliceType | MapType | TypeName .
2424 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2425 // ElementList = Element { "," Element } .
2426 // Element = [ Key ":" ] Value .
2427 // Key = Expression .
2428 // Value = Expression | LiteralValue .
2430 // We have already seen the type if there is one, and we are now
2431 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2432 // will be seen here as an array type whose length is "nil". The
2433 // DEPTH parameter is non-zero if this is an embedded composite
2434 // literal and the type was omitted. It gives the number of steps up
2435 // to the type which was provided. E.g., in [][]int{{1}} it will be
2436 // 1. In [][][]int{{{1}}} it will be 2.
2439 Parse::composite_lit(Type* type, int depth, source_location location)
2441 gcc_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2442 this->advance_token();
2444 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2446 this->advance_token();
2447 return Expression::make_composite_literal(type, depth, false, NULL,
2451 bool has_keys = false;
2452 Expression_list* vals = new Expression_list;
2456 bool is_type_omitted = false;
2458 const Token* token = this->peek_token();
2460 if (!token->is_op(OPERATOR_LCURLY))
2461 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2464 // This must be a composite literal inside another composite
2465 // literal, with the type omitted for the inner one.
2466 val = this->composite_lit(type, depth + 1, token->location());
2467 is_type_omitted = true;
2470 token = this->peek_token();
2471 if (!token->is_op(OPERATOR_COLON))
2474 vals->push_back(NULL);
2478 if (is_type_omitted && !val->is_error_expression())
2480 error_at(this->location(), "unexpected %<:%>");
2481 val = Expression::make_error(this->location());
2484 this->advance_token();
2486 if (!has_keys && !vals->empty())
2488 Expression_list* newvals = new Expression_list;
2489 for (Expression_list::const_iterator p = vals->begin();
2493 newvals->push_back(NULL);
2494 newvals->push_back(*p);
2501 if (val->unknown_expression() != NULL)
2502 val->unknown_expression()->set_is_composite_literal_key();
2504 vals->push_back(val);
2506 if (!token->is_op(OPERATOR_LCURLY))
2507 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2510 // This must be a composite literal inside another
2511 // composite literal, with the type omitted for the
2513 val = this->composite_lit(type, depth + 1, token->location());
2516 token = this->peek_token();
2519 vals->push_back(val);
2521 if (token->is_op(OPERATOR_COMMA))
2523 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2525 this->advance_token();
2529 else if (token->is_op(OPERATOR_RCURLY))
2531 this->advance_token();
2536 error_at(this->location(), "expected %<,%> or %<}%>");
2539 while (!token->is_eof()
2540 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2542 if (token->is_op(OPERATOR_LCURLY))
2544 else if (token->is_op(OPERATOR_RCURLY))
2546 token = this->advance_token();
2548 if (token->is_op(OPERATOR_RCURLY))
2549 this->advance_token();
2551 return Expression::make_error(location);
2555 return Expression::make_composite_literal(type, depth, has_keys, vals,
2559 // FunctionLit = "func" Signature Block .
2562 Parse::function_lit()
2564 source_location location = this->location();
2565 gcc_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2566 this->advance_token();
2568 Enclosing_vars hold_enclosing_vars;
2569 hold_enclosing_vars.swap(this->enclosing_vars_);
2571 Function_type* type = this->signature(NULL, location);
2573 type = Type::make_function_type(NULL, NULL, NULL, location);
2575 // For a function literal, the next token must be a '{'. If we
2576 // don't see that, then we may have a type expression.
2577 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2578 return Expression::make_type(type, location);
2580 Bc_stack* hold_break_stack = this->break_stack_;
2581 Bc_stack* hold_continue_stack = this->continue_stack_;
2582 this->break_stack_ = NULL;
2583 this->continue_stack_ = NULL;
2585 Named_object* no = this->gogo_->start_function("", type, true, location);
2587 source_location end_loc = this->block();
2589 this->gogo_->finish_function(end_loc);
2591 if (this->break_stack_ != NULL)
2592 delete this->break_stack_;
2593 if (this->continue_stack_ != NULL)
2594 delete this->continue_stack_;
2595 this->break_stack_ = hold_break_stack;
2596 this->continue_stack_ = hold_continue_stack;
2598 hold_enclosing_vars.swap(this->enclosing_vars_);
2600 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2603 return Expression::make_func_reference(no, closure, location);
2606 // Create a closure for the nested function FUNCTION. This is based
2607 // on ENCLOSING_VARS, which is a list of all variables defined in
2608 // enclosing functions and referenced from FUNCTION. A closure is the
2609 // address of a struct which contains the addresses of all the
2610 // referenced variables. This returns NULL if no closure is required.
2613 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2614 source_location location)
2616 if (enclosing_vars->empty())
2619 // Get the variables in order by their field index.
2621 size_t enclosing_var_count = enclosing_vars->size();
2622 std::vector<Enclosing_var> ev(enclosing_var_count);
2623 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2624 p != enclosing_vars->end();
2626 ev[p->index()] = *p;
2628 // Build an initializer for a composite literal of the closure's
2631 Named_object* enclosing_function = this->gogo_->current_function();
2632 Expression_list* initializer = new Expression_list;
2633 for (size_t i = 0; i < enclosing_var_count; ++i)
2635 gcc_assert(ev[i].index() == i);
2636 Named_object* var = ev[i].var();
2638 if (ev[i].in_function() == enclosing_function)
2639 ref = Expression::make_var_reference(var, location);
2641 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2643 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2645 initializer->push_back(refaddr);
2648 Named_object* closure_var = function->func_value()->closure_var();
2649 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2650 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2652 return Expression::make_heap_composite(cv, location);
2655 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2657 // If MAY_BE_SINK is true, this expression may be "_".
2659 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2662 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2663 // guard (var := expr.("type") using the literal keyword "type").
2666 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2667 bool* is_type_switch)
2669 source_location start_loc = this->location();
2670 bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2672 Expression* ret = this->operand(may_be_sink);
2674 // An unknown name followed by a curly brace must be a composite
2675 // literal, and the unknown name must be a type.
2676 if (may_be_composite_lit
2677 && !is_parenthesized
2678 && ret->unknown_expression() != NULL
2679 && this->peek_token()->is_op(OPERATOR_LCURLY))
2681 Named_object* no = ret->unknown_expression()->named_object();
2682 Type* type = Type::make_forward_declaration(no);
2683 ret = Expression::make_type(type, ret->location());
2686 // We handle composite literals and type casts here, as it is the
2687 // easiest way to handle types which are in parentheses, as in
2689 if (ret->is_type_expression())
2691 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2693 if (is_parenthesized)
2695 "cannot parenthesize type in composite literal");
2696 ret = this->composite_lit(ret->type(), 0, ret->location());
2698 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2700 source_location loc = this->location();
2701 this->advance_token();
2702 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2704 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2705 error_at(this->location(), "expected %<)%>");
2707 this->advance_token();
2708 if (expr->is_error_expression())
2710 ret = Expression::make_cast(ret->type(), expr, loc);
2716 const Token* token = this->peek_token();
2717 if (token->is_op(OPERATOR_LPAREN))
2718 ret = this->call(this->verify_not_sink(ret));
2719 else if (token->is_op(OPERATOR_DOT))
2721 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2722 if (is_type_switch != NULL && *is_type_switch)
2725 else if (token->is_op(OPERATOR_LSQUARE))
2726 ret = this->index(this->verify_not_sink(ret));
2734 // Selector = "." identifier .
2735 // TypeGuard = "." "(" QualifiedIdent ")" .
2737 // Note that Operand can expand to QualifiedIdent, which contains a
2738 // ".". That is handled directly in operand when it sees a package
2741 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2742 // guard (var := expr.("type") using the literal keyword "type").
2745 Parse::selector(Expression* left, bool* is_type_switch)
2747 gcc_assert(this->peek_token()->is_op(OPERATOR_DOT));
2748 source_location location = this->location();
2750 const Token* token = this->advance_token();
2751 if (token->is_identifier())
2753 // This could be a field in a struct, or a method in an
2754 // interface, or a method associated with a type. We can't know
2755 // which until we have seen all the types.
2757 this->gogo_->pack_hidden_name(token->identifier(),
2758 token->is_identifier_exported());
2759 if (token->identifier() == "_")
2761 error_at(this->location(), "invalid use of %<_%>");
2762 name = this->gogo_->pack_hidden_name("blank", false);
2764 this->advance_token();
2765 return Expression::make_selector(left, name, location);
2767 else if (token->is_op(OPERATOR_LPAREN))
2769 this->advance_token();
2771 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
2772 type = this->type();
2775 if (is_type_switch != NULL)
2776 *is_type_switch = true;
2779 error_at(this->location(),
2780 "use of %<.(type)%> outside type switch");
2781 type = Type::make_error_type();
2783 this->advance_token();
2785 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2786 error_at(this->location(), "missing %<)%>");
2788 this->advance_token();
2789 if (is_type_switch != NULL && *is_type_switch)
2791 return Expression::make_type_guard(left, type, location);
2795 error_at(this->location(), "expected identifier or %<(%>");
2800 // Index = "[" Expression "]" .
2801 // Slice = "[" Expression ":" [ Expression ] "]" .
2804 Parse::index(Expression* expr)
2806 source_location location = this->location();
2807 gcc_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
2808 this->advance_token();
2811 if (!this->peek_token()->is_op(OPERATOR_COLON))
2812 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2816 mpz_init_set_ui(zero, 0);
2817 start = Expression::make_integer(&zero, NULL, location);
2821 Expression* end = NULL;
2822 if (this->peek_token()->is_op(OPERATOR_COLON))
2824 // We use nil to indicate a missing high expression.
2825 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
2826 end = Expression::make_nil(this->location());
2828 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2830 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
2831 error_at(this->location(), "missing %<]%>");
2833 this->advance_token();
2834 return Expression::make_index(expr, start, end, location);
2837 // Call = "(" [ ArgumentList [ "," ] ] ")" .
2838 // ArgumentList = ExpressionList [ "..." ] .
2841 Parse::call(Expression* func)
2843 gcc_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2844 Expression_list* args = NULL;
2845 bool is_varargs = false;
2846 const Token* token = this->advance_token();
2847 if (!token->is_op(OPERATOR_RPAREN))
2849 args = this->expression_list(NULL, false);
2850 token = this->peek_token();
2851 if (token->is_op(OPERATOR_ELLIPSIS))
2854 token = this->advance_token();
2857 if (token->is_op(OPERATOR_COMMA))
2858 token = this->advance_token();
2859 if (!token->is_op(OPERATOR_RPAREN))
2860 error_at(this->location(), "missing %<)%>");
2862 this->advance_token();
2863 if (func->is_error_expression())
2865 return Expression::make_call(func, args, is_varargs, func->location());
2868 // Return an expression for a single unqualified identifier.
2871 Parse::id_to_expression(const std::string& name, source_location location)
2873 Named_object* in_function;
2874 Named_object* named_object = this->gogo_->lookup(name, &in_function);
2875 if (named_object == NULL)
2876 named_object = this->gogo_->add_unknown_name(name, location);
2878 if (in_function != NULL
2879 && in_function != this->gogo_->current_function()
2880 && (named_object->is_variable() || named_object->is_result_variable()))
2881 return this->enclosing_var_reference(in_function, named_object,
2884 switch (named_object->classification())
2886 case Named_object::NAMED_OBJECT_CONST:
2887 return Expression::make_const_reference(named_object, location);
2888 case Named_object::NAMED_OBJECT_VAR:
2889 case Named_object::NAMED_OBJECT_RESULT_VAR:
2890 return Expression::make_var_reference(named_object, location);
2891 case Named_object::NAMED_OBJECT_SINK:
2892 return Expression::make_sink(location);
2893 case Named_object::NAMED_OBJECT_FUNC:
2894 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2895 return Expression::make_func_reference(named_object, NULL, location);
2896 case Named_object::NAMED_OBJECT_UNKNOWN:
2897 return Expression::make_unknown_reference(named_object, location);
2899 error_at(this->location(), "unexpected type of identifier");
2900 return Expression::make_error(location);
2904 // Expression = UnaryExpr { binary_op Expression } .
2906 // PRECEDENCE is the precedence of the current operator.
2908 // If MAY_BE_SINK is true, this expression may be "_".
2910 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2913 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2914 // guard (var := expr.("type") using the literal keyword "type").
2917 Parse::expression(Precedence precedence, bool may_be_sink,
2918 bool may_be_composite_lit, bool* is_type_switch)
2920 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
2925 if (is_type_switch != NULL && *is_type_switch)
2928 const Token* token = this->peek_token();
2929 if (token->classification() != Token::TOKEN_OPERATOR)
2935 Precedence right_precedence;
2936 switch (token->op())
2939 right_precedence = PRECEDENCE_OROR;
2941 case OPERATOR_ANDAND:
2942 right_precedence = PRECEDENCE_ANDAND;
2944 case OPERATOR_CHANOP:
2945 right_precedence = PRECEDENCE_CHANOP;
2948 case OPERATOR_NOTEQ:
2953 right_precedence = PRECEDENCE_RELOP;
2956 case OPERATOR_MINUS:
2959 right_precedence = PRECEDENCE_ADDOP;
2964 case OPERATOR_LSHIFT:
2965 case OPERATOR_RSHIFT:
2967 case OPERATOR_BITCLEAR:
2968 right_precedence = PRECEDENCE_MULOP;
2971 right_precedence = PRECEDENCE_INVALID;
2975 if (right_precedence == PRECEDENCE_INVALID)
2981 Operator op = token->op();
2982 source_location binop_location = token->location();
2984 if (precedence >= right_precedence)
2986 // We've already seen A * B, and we see + C. We want to
2987 // return so that A * B becomes a group.
2991 this->advance_token();
2993 left = this->verify_not_sink(left);
2994 Expression* right = this->expression(right_precedence, false,
2995 may_be_composite_lit,
2997 if (op == OPERATOR_CHANOP)
2998 left = Expression::make_send(left, right, binop_location);
3000 left = Expression::make_binary(op, left, right, binop_location);
3005 Parse::expression_may_start_here()
3007 const Token* token = this->peek_token();
3008 switch (token->classification())
3010 case Token::TOKEN_INVALID:
3011 case Token::TOKEN_EOF:
3013 case Token::TOKEN_KEYWORD:
3014 switch (token->keyword())
3019 case KEYWORD_STRUCT:
3020 case KEYWORD_INTERFACE:
3025 case Token::TOKEN_IDENTIFIER:
3027 case Token::TOKEN_STRING:
3029 case Token::TOKEN_OPERATOR:
3030 switch (token->op())
3033 case OPERATOR_MINUS:
3037 case OPERATOR_CHANOP:
3039 case OPERATOR_LPAREN:
3040 case OPERATOR_LSQUARE:
3045 case Token::TOKEN_INTEGER:
3046 case Token::TOKEN_FLOAT:
3047 case Token::TOKEN_IMAGINARY:
3054 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3056 // If MAY_BE_SINK is true, this expression may be "_".
3058 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3061 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3062 // guard (var := expr.("type") using the literal keyword "type").
3065 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3066 bool* is_type_switch)
3068 const Token* token = this->peek_token();
3069 if (token->is_op(OPERATOR_PLUS)
3070 || token->is_op(OPERATOR_MINUS)
3071 || token->is_op(OPERATOR_NOT)
3072 || token->is_op(OPERATOR_XOR)
3073 || token->is_op(OPERATOR_CHANOP)
3074 || token->is_op(OPERATOR_MULT)
3075 || token->is_op(OPERATOR_AND))
3077 source_location location = token->location();
3078 Operator op = token->op();
3079 this->advance_token();
3081 if (op == OPERATOR_CHANOP
3082 && this->peek_token()->is_keyword(KEYWORD_CHAN))
3084 // This is "<- chan" which must be the start of a type.
3085 this->unget_token(Token::make_operator_token(op, location));
3086 return Expression::make_type(this->type(), location);
3089 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
3090 if (expr->is_error_expression())
3092 else if (op == OPERATOR_MULT && expr->is_type_expression())
3093 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3095 else if (op == OPERATOR_AND && expr->is_composite_literal())
3096 expr = Expression::make_heap_composite(expr, location);
3097 else if (op != OPERATOR_CHANOP)
3098 expr = Expression::make_unary(op, expr, location);
3100 expr = Expression::make_receive(expr, location);
3104 return this->primary_expr(may_be_sink, may_be_composite_lit,
3109 // Declaration | LabeledStmt | SimpleStmt |
3110 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3111 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3114 // LABEL is the label of this statement if it has one.
3117 Parse::statement(const Label* label)
3119 const Token* token = this->peek_token();
3120 switch (token->classification())
3122 case Token::TOKEN_KEYWORD:
3124 switch (token->keyword())
3129 this->declaration();
3133 case KEYWORD_STRUCT:
3134 case KEYWORD_INTERFACE:
3135 this->simple_stat(true, false, NULL, NULL);
3139 this->go_or_defer_stat();
3141 case KEYWORD_RETURN:
3142 this->return_stat();
3147 case KEYWORD_CONTINUE:
3148 this->continue_stat();
3156 case KEYWORD_SWITCH:
3157 this->switch_stat(label);
3159 case KEYWORD_SELECT:
3160 this->select_stat(label);
3163 this->for_stat(label);
3166 error_at(this->location(), "expected statement");
3167 this->advance_token();
3173 case Token::TOKEN_IDENTIFIER:
3175 std::string identifier = token->identifier();
3176 bool is_exported = token->is_identifier_exported();
3177 source_location location = token->location();
3178 if (this->advance_token()->is_op(OPERATOR_COLON))
3180 this->advance_token();
3181 this->labeled_stmt(identifier, location);
3185 this->unget_token(Token::make_identifier_token(identifier,
3188 this->simple_stat(true, false, NULL, NULL);
3193 case Token::TOKEN_OPERATOR:
3194 if (token->is_op(OPERATOR_LCURLY))
3196 source_location location = token->location();
3197 this->gogo_->start_block(location);
3198 source_location end_loc = this->block();
3199 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3202 else if (!token->is_op(OPERATOR_SEMICOLON))
3203 this->simple_stat(true, false, NULL, NULL);
3206 case Token::TOKEN_STRING:
3207 case Token::TOKEN_INTEGER:
3208 case Token::TOKEN_FLOAT:
3209 case Token::TOKEN_IMAGINARY:
3210 this->simple_stat(true, false, NULL, NULL);
3214 error_at(this->location(), "expected statement");
3215 this->advance_token();
3221 Parse::statement_may_start_here()
3223 const Token* token = this->peek_token();
3224 switch (token->classification())
3226 case Token::TOKEN_KEYWORD:
3228 switch (token->keyword())
3235 case KEYWORD_STRUCT:
3236 case KEYWORD_INTERFACE:
3239 case KEYWORD_RETURN:
3241 case KEYWORD_CONTINUE:
3244 case KEYWORD_SWITCH:
3245 case KEYWORD_SELECT:
3255 case Token::TOKEN_IDENTIFIER:
3258 case Token::TOKEN_OPERATOR:
3259 if (token->is_op(OPERATOR_LCURLY)
3260 || token->is_op(OPERATOR_SEMICOLON))
3263 return this->expression_may_start_here();
3265 case Token::TOKEN_STRING:
3266 case Token::TOKEN_INTEGER:
3267 case Token::TOKEN_FLOAT:
3268 case Token::TOKEN_IMAGINARY:
3276 // LabeledStmt = Label ":" Statement .
3277 // Label = identifier .
3280 Parse::labeled_stmt(const std::string& label_name, source_location location)
3282 Label* label = this->gogo_->add_label_definition(label_name, location);
3284 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3286 // This is a label at the end of a block. A program is
3287 // permitted to omit a semicolon here.
3291 if (!this->statement_may_start_here())
3293 error_at(location, "missing statement after label");
3294 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3299 this->statement(label);
3303 // ExpressionStat | IncDecStat | Assignment | SimpleVarDecl .
3305 // In order to make this work for if and switch statements, if
3306 // RETURN_EXP is true, and we see an ExpressionStat, we return the
3307 // expression rather than adding an expression statement to the
3308 // current block. If we see something other than an ExpressionStat,
3309 // we add the statement and return NULL.
3311 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3314 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3315 // guard (var := expr.("type") using the literal keyword "type").
3318 Parse::simple_stat(bool may_be_composite_lit, bool return_exp,
3319 Range_clause* p_range_clause, Type_switch* p_type_switch)
3321 const Token* token = this->peek_token();
3323 // An identifier follow by := is a SimpleVarDecl.
3324 if (token->is_identifier())
3326 std::string identifier = token->identifier();
3327 bool is_exported = token->is_identifier_exported();
3328 source_location location = token->location();
3330 token = this->advance_token();
3331 if (token->is_op(OPERATOR_COLONEQ)
3332 || token->is_op(OPERATOR_COMMA))
3334 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3335 this->simple_var_decl_or_assignment(identifier, location,
3337 (token->is_op(OPERATOR_COLONEQ)
3343 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3347 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3348 may_be_composite_lit,
3349 (p_type_switch == NULL
3351 : &p_type_switch->found));
3352 if (p_type_switch != NULL && p_type_switch->found)
3354 p_type_switch->name.clear();
3355 p_type_switch->location = exp->location();
3356 p_type_switch->expr = this->verify_not_sink(exp);
3359 token = this->peek_token();
3360 if (token->is_op(OPERATOR_PLUSPLUS) || token->is_op(OPERATOR_MINUSMINUS))
3361 this->inc_dec_stat(this->verify_not_sink(exp));
3362 else if (token->is_op(OPERATOR_COMMA)
3363 || token->is_op(OPERATOR_EQ))
3364 this->assignment(exp, p_range_clause);
3365 else if (token->is_op(OPERATOR_PLUSEQ)
3366 || token->is_op(OPERATOR_MINUSEQ)
3367 || token->is_op(OPERATOR_OREQ)
3368 || token->is_op(OPERATOR_XOREQ)
3369 || token->is_op(OPERATOR_MULTEQ)
3370 || token->is_op(OPERATOR_DIVEQ)
3371 || token->is_op(OPERATOR_MODEQ)
3372 || token->is_op(OPERATOR_LSHIFTEQ)
3373 || token->is_op(OPERATOR_RSHIFTEQ)
3374 || token->is_op(OPERATOR_ANDEQ)
3375 || token->is_op(OPERATOR_BITCLEAREQ))
3376 this->assignment(this->verify_not_sink(exp), p_range_clause);
3377 else if (return_exp)
3378 return this->verify_not_sink(exp);
3380 this->expression_stat(this->verify_not_sink(exp));
3386 Parse::simple_stat_may_start_here()
3388 return this->expression_may_start_here();
3391 // Parse { Statement ";" } which is used in a few places. The list of
3392 // statements may end with a right curly brace, in which case the
3393 // semicolon may be omitted.
3396 Parse::statement_list()
3398 while (this->statement_may_start_here())
3400 this->statement(NULL);
3401 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3402 this->advance_token();
3403 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3407 if (!this->peek_token()->is_eof() || !saw_errors())
3408 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3409 if (!this->skip_past_error(OPERATOR_RCURLY))
3416 Parse::statement_list_may_start_here()
3418 return this->statement_may_start_here();
3421 // ExpressionStat = Expression .
3424 Parse::expression_stat(Expression* exp)
3426 exp->discarding_value();
3427 this->gogo_->add_statement(Statement::make_statement(exp));
3430 // IncDecStat = Expression ( "++" | "--" ) .
3433 Parse::inc_dec_stat(Expression* exp)
3435 const Token* token = this->peek_token();
3437 // Lvalue maps require special handling.
3438 if (exp->index_expression() != NULL)
3439 exp->index_expression()->set_is_lvalue();
3441 if (token->is_op(OPERATOR_PLUSPLUS))
3442 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3443 else if (token->is_op(OPERATOR_MINUSMINUS))
3444 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3447 this->advance_token();
3450 // Assignment = ExpressionList assign_op ExpressionList .
3452 // EXP is an expression that we have already parsed.
3454 // If RANGE_CLAUSE is not NULL, then this will recognize a
3458 Parse::assignment(Expression* expr, Range_clause* p_range_clause)
3460 Expression_list* vars;
3461 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3463 vars = new Expression_list();
3464 vars->push_back(expr);
3468 this->advance_token();
3469 vars = this->expression_list(expr, true);
3472 this->tuple_assignment(vars, p_range_clause);
3475 // An assignment statement. LHS is the list of expressions which
3476 // appear on the left hand side.
3478 // If RANGE_CLAUSE is not NULL, then this will recognize a
3482 Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
3484 const Token* token = this->peek_token();
3485 if (!token->is_op(OPERATOR_EQ)
3486 && !token->is_op(OPERATOR_PLUSEQ)
3487 && !token->is_op(OPERATOR_MINUSEQ)
3488 && !token->is_op(OPERATOR_OREQ)
3489 && !token->is_op(OPERATOR_XOREQ)
3490 && !token->is_op(OPERATOR_MULTEQ)
3491 && !token->is_op(OPERATOR_DIVEQ)
3492 && !token->is_op(OPERATOR_MODEQ)
3493 && !token->is_op(OPERATOR_LSHIFTEQ)
3494 && !token->is_op(OPERATOR_RSHIFTEQ)
3495 && !token->is_op(OPERATOR_ANDEQ)
3496 && !token->is_op(OPERATOR_BITCLEAREQ))
3498 error_at(this->location(), "expected assignment operator");
3501 Operator op = token->op();
3502 source_location location = token->location();
3504 token = this->advance_token();
3506 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
3508 if (op != OPERATOR_EQ)
3509 error_at(this->location(), "range clause requires %<=%>");
3510 this->range_clause_expr(lhs, p_range_clause);
3514 Expression_list* vals = this->expression_list(NULL, false);
3516 // We've parsed everything; check for errors.
3517 if (lhs == NULL || vals == NULL)
3519 for (Expression_list::const_iterator pe = lhs->begin();
3523 if ((*pe)->is_error_expression())
3525 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
3526 error_at((*pe)->location(), "cannot use _ as value");
3528 for (Expression_list::const_iterator pe = vals->begin();
3532 if ((*pe)->is_error_expression())
3536 // Map expressions act differently when they are lvalues.
3537 for (Expression_list::iterator plv = lhs->begin();
3540 if ((*plv)->index_expression() != NULL)
3541 (*plv)->index_expression()->set_is_lvalue();
3543 Call_expression* call;
3544 Index_expression* map_index;
3545 Receive_expression* receive;
3546 Type_guard_expression* type_guard;
3547 if (lhs->size() == vals->size())
3550 if (lhs->size() > 1)
3552 if (op != OPERATOR_EQ)
3553 error_at(location, "multiple values only permitted with %<=%>");
3554 s = Statement::make_tuple_assignment(lhs, vals, location);
3558 if (op == OPERATOR_EQ)
3559 s = Statement::make_assignment(lhs->front(), vals->front(),
3562 s = Statement::make_assignment_operation(op, lhs->front(),
3563 vals->front(), location);
3567 this->gogo_->add_statement(s);
3569 else if (vals->size() == 1
3570 && (call = (*vals->begin())->call_expression()) != NULL)
3572 if (op != OPERATOR_EQ)
3573 error_at(location, "multiple results only permitted with %<=%>");
3575 vals = new Expression_list;
3576 for (unsigned int i = 0; i < lhs->size(); ++i)
3577 vals->push_back(Expression::make_call_result(call, i));
3578 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
3579 this->gogo_->add_statement(s);
3581 else if (lhs->size() == 2
3582 && vals->size() == 1
3583 && (map_index = (*vals->begin())->index_expression()) != NULL)
3585 if (op != OPERATOR_EQ)
3586 error_at(location, "two values from map requires %<=%>");
3587 Expression* val = lhs->front();
3588 Expression* present = lhs->back();
3589 Statement* s = Statement::make_tuple_map_assignment(val, present,
3590 map_index, location);
3591 this->gogo_->add_statement(s);
3593 else if (lhs->size() == 1
3594 && vals->size() == 2
3595 && (map_index = lhs->front()->index_expression()) != NULL)
3597 if (op != OPERATOR_EQ)
3598 error_at(location, "assigning tuple to map index requires %<=%>");
3599 Expression* val = vals->front();
3600 Expression* should_set = vals->back();
3601 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
3603 this->gogo_->add_statement(s);
3605 else if (lhs->size() == 2
3606 && vals->size() == 1
3607 && (receive = (*vals->begin())->receive_expression()) != NULL)
3609 if (op != OPERATOR_EQ)
3610 error_at(location, "two values from receive requires %<=%>");
3611 Expression* val = lhs->front();
3612 Expression* success = lhs->back();
3613 Expression* channel = receive->channel();
3614 Statement* s = Statement::make_tuple_receive_assignment(val, success,
3617 this->gogo_->add_statement(s);
3619 else if (lhs->size() == 2
3620 && vals->size() == 1
3621 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
3623 if (op != OPERATOR_EQ)
3624 error_at(location, "two values from type guard requires %<=%>");
3625 Expression* val = lhs->front();
3626 Expression* ok = lhs->back();
3627 Expression* expr = type_guard->expr();
3628 Type* type = type_guard->type();
3629 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
3632 this->gogo_->add_statement(s);
3636 error_at(location, "number of variables does not match number of values");
3640 // GoStat = "go" Expression .
3641 // DeferStat = "defer" Expression .
3644 Parse::go_or_defer_stat()
3646 gcc_assert(this->peek_token()->is_keyword(KEYWORD_GO)
3647 || this->peek_token()->is_keyword(KEYWORD_DEFER));
3648 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
3649 source_location stat_location = this->location();
3650 this->advance_token();
3651 source_location expr_location = this->location();
3652 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3653 Call_expression* call_expr = expr->call_expression();
3654 if (call_expr == NULL)
3656 error_at(expr_location, "expected call expression");
3660 // Make it easier to simplify go/defer statements by putting every
3661 // statement in its own block.
3662 this->gogo_->start_block(stat_location);
3665 stat = Statement::make_go_statement(call_expr, stat_location);
3667 stat = Statement::make_defer_statement(call_expr, stat_location);
3668 this->gogo_->add_statement(stat);
3669 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
3673 // ReturnStat = "return" [ ExpressionList ] .
3676 Parse::return_stat()
3678 gcc_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
3679 source_location location = this->location();
3680 this->advance_token();
3681 Expression_list* vals = NULL;
3682 if (this->expression_may_start_here())
3683 vals = this->expression_list(NULL, false);
3684 const Function* function = this->gogo_->current_function()->func_value();
3685 const Typed_identifier_list* results = function->type()->results();
3686 this->gogo_->add_statement(Statement::make_return_statement(results, vals,
3690 // IfStat = "if" [ [ SimpleStat ] ";" ] [ Condition ]
3691 // Block [ "else" Statement ] .
3696 gcc_assert(this->peek_token()->is_keyword(KEYWORD_IF));
3697 source_location location = this->location();
3698 this->advance_token();
3700 this->gogo_->start_block(location);
3702 Expression* cond = NULL;
3703 if (this->simple_stat_may_start_here())
3704 cond = this->simple_stat(false, true, NULL, NULL);
3705 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3707 // The SimpleStat is an expression statement.
3708 this->expression_stat(cond);
3713 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3714 this->advance_token();
3715 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3716 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
3719 this->gogo_->start_block(this->location());
3720 source_location end_loc = this->block();
3721 Block* then_block = this->gogo_->finish_block(end_loc);
3723 // Check for the easy error of a newline before "else".
3724 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3726 source_location semi_loc = this->location();
3727 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
3728 error_at(this->location(),
3729 "unexpected semicolon or newline before %<else%>");
3731 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3735 Block* else_block = NULL;
3736 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
3738 this->advance_token();
3739 // We create a block to gather the statement.
3740 this->gogo_->start_block(this->location());
3741 this->statement(NULL);
3742 else_block = this->gogo_->finish_block(this->location());
3745 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
3749 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3753 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
3754 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
3755 // "{" { ExprCaseClause } "}" .
3756 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
3757 // "{" { TypeCaseClause } "}" .
3758 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
3761 Parse::switch_stat(const Label* label)
3763 gcc_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
3764 source_location location = this->location();
3765 this->advance_token();
3767 this->gogo_->start_block(location);
3769 Expression* switch_val = NULL;
3770 Type_switch type_switch;
3771 if (this->simple_stat_may_start_here())
3772 switch_val = this->simple_stat(false, true, NULL, &type_switch);
3773 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
3775 // The SimpleStat is an expression statement.
3776 this->expression_stat(switch_val);
3779 if (switch_val == NULL && !type_switch.found)
3781 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3782 this->advance_token();
3783 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3785 if (this->peek_token()->is_identifier())
3787 const Token* token = this->peek_token();
3788 std::string identifier = token->identifier();
3789 bool is_exported = token->is_identifier_exported();
3790 source_location id_loc = token->location();
3792 token = this->advance_token();
3793 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
3794 this->unget_token(Token::make_identifier_token(identifier,
3799 // This must be a TypeSwitchGuard.
3800 switch_val = this->simple_stat(false, true, NULL,
3802 if (!type_switch.found
3803 && !switch_val->is_error_expression())
3805 error_at(id_loc, "expected type switch assignment");
3806 switch_val = Expression::make_error(id_loc);
3810 if (switch_val == NULL && !type_switch.found)
3812 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
3813 &type_switch.found);
3814 if (type_switch.found)
3816 type_switch.name.clear();
3817 type_switch.expr = switch_val;
3818 type_switch.location = switch_val->location();
3824 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
3826 source_location token_loc = this->location();
3827 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
3828 && this->advance_token()->is_op(OPERATOR_LCURLY))
3829 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
3832 error_at(this->location(), "expected %<{%>");
3833 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3838 this->advance_token();
3840 Statement* statement;
3841 if (type_switch.found)
3842 statement = this->type_switch_body(label, type_switch, location);
3844 statement = this->expr_switch_body(label, switch_val, location);
3846 if (statement != NULL)
3847 this->gogo_->add_statement(statement);
3849 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
3853 // The body of an expression switch.
3854 // "{" { ExprCaseClause } "}"
3857 Parse::expr_switch_body(const Label* label, Expression* switch_val,
3858 source_location location)
3860 Switch_statement* statement = Statement::make_switch_statement(switch_val,
3863 this->push_break_statement(statement, label);
3865 Case_clauses* case_clauses = new Case_clauses();
3866 bool saw_default = false;
3867 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
3869 if (this->peek_token()->is_eof())
3872 error_at(this->location(), "missing %<}%>");
3875 this->expr_case_clause(case_clauses, &saw_default);
3877 this->advance_token();
3879 statement->add_clauses(case_clauses);
3881 this->pop_break_statement();
3886 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
3887 // FallthroughStat = "fallthrough" .
3890 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
3892 source_location location = this->location();
3894 bool is_default = false;
3895 Expression_list* vals = this->expr_switch_case(&is_default);
3897 if (!this->peek_token()->is_op(OPERATOR_COLON))
3900 error_at(this->location(), "expected %<:%>");
3904 this->advance_token();
3906 Block* statements = NULL;
3907 if (this->statement_list_may_start_here())
3909 this->gogo_->start_block(this->location());
3910 this->statement_list();
3911 statements = this->gogo_->finish_block(this->location());
3914 bool is_fallthrough = false;
3915 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
3917 is_fallthrough = true;
3918 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
3919 this->advance_token();
3926 error_at(location, "multiple defaults in switch");
3929 *saw_default = true;
3932 if (is_default || vals != NULL)
3933 clauses->add(vals, is_default, statements, is_fallthrough, location);
3936 // ExprSwitchCase = "case" ExpressionList | "default" .
3939 Parse::expr_switch_case(bool* is_default)
3941 const Token* token = this->peek_token();
3942 if (token->is_keyword(KEYWORD_CASE))
3944 this->advance_token();
3945 return this->expression_list(NULL, false);
3947 else if (token->is_keyword(KEYWORD_DEFAULT))
3949 this->advance_token();
3956 error_at(this->location(), "expected %<case%> or %<default%>");
3957 if (!token->is_op(OPERATOR_RCURLY))
3958 this->advance_token();
3963 // The body of a type switch.
3964 // "{" { TypeCaseClause } "}" .
3967 Parse::type_switch_body(const Label* label, const Type_switch& type_switch,
3968 source_location location)
3970 Named_object* switch_no = NULL;
3971 if (!type_switch.name.empty())
3973 Variable* switch_var = new Variable(NULL, type_switch.expr, false, false,
3974 false, type_switch.location);
3975 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
3978 Type_switch_statement* statement =
3979 Statement::make_type_switch_statement(switch_no,
3985 this->push_break_statement(statement, label);
3987 Type_case_clauses* case_clauses = new Type_case_clauses();
3988 bool saw_default = false;
3989 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
3991 if (this->peek_token()->is_eof())
3993 error_at(this->location(), "missing %<}%>");
3996 this->type_case_clause(switch_no, case_clauses, &saw_default);
3998 this->advance_token();
4000 statement->add_clauses(case_clauses);
4002 this->pop_break_statement();
4007 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4010 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4013 source_location location = this->location();
4015 std::vector<Type*> types;
4016 bool is_default = false;
4017 this->type_switch_case(&types, &is_default);
4019 if (!this->peek_token()->is_op(OPERATOR_COLON))
4020 error_at(this->location(), "expected %<:%>");
4022 this->advance_token();
4024 Block* statements = NULL;
4025 if (this->statement_list_may_start_here())
4027 this->gogo_->start_block(this->location());
4028 if (switch_no != NULL && types.size() == 1)
4030 Type* type = types.front();
4031 Expression* init = Expression::make_var_reference(switch_no,
4033 init = Expression::make_type_guard(init, type, location);
4034 Variable* v = new Variable(type, init, false, false, false,
4036 v->set_is_type_switch_var();
4037 this->gogo_->add_variable(switch_no->name(), v);
4039 this->statement_list();
4040 statements = this->gogo_->finish_block(this->location());
4043 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4045 error_at(this->location(),
4046 "fallthrough is not permitted in a type switch");
4047 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4048 this->advance_token();
4053 gcc_assert(types.empty());
4056 error_at(location, "multiple defaults in type switch");
4059 *saw_default = true;
4060 clauses->add(NULL, false, true, statements, location);
4062 else if (!types.empty())
4064 for (std::vector<Type*>::const_iterator p = types.begin();
4065 p + 1 != types.end();
4067 clauses->add(*p, true, false, NULL, location);
4068 clauses->add(types.back(), false, false, statements, location);
4071 clauses->add(Type::make_error_type(), false, false, statements, location);
4074 // TypeSwitchCase = "case" type | "default"
4076 // We accept a comma separated list of types.
4079 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4081 const Token* token = this->peek_token();
4082 if (token->is_keyword(KEYWORD_CASE))
4084 this->advance_token();
4087 Type* t = this->type();
4088 if (!t->is_error_type())
4089 types->push_back(t);
4090 if (!this->peek_token()->is_op(OPERATOR_COMMA))
4092 this->advance_token();
4095 else if (token->is_keyword(KEYWORD_DEFAULT))
4097 this->advance_token();
4102 error_at(this->location(), "expected %<case%> or %<default%>");
4103 if (!token->is_op(OPERATOR_RCURLY))
4104 this->advance_token();
4108 // SelectStat = "select" "{" { CommClause } "}" .
4111 Parse::select_stat(const Label* label)
4113 gcc_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4114 source_location location = this->location();
4115 const Token* token = this->advance_token();
4117 if (!token->is_op(OPERATOR_LCURLY))
4119 source_location token_loc = token->location();
4120 if (token->is_op(OPERATOR_SEMICOLON)
4121 && this->advance_token()->is_op(OPERATOR_LCURLY))
4122 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4125 error_at(this->location(), "expected %<{%>");
4129 this->advance_token();
4131 Select_statement* statement = Statement::make_select_statement(location);
4133 this->push_break_statement(statement, label);
4135 Select_clauses* select_clauses = new Select_clauses();
4136 bool saw_default = false;
4137 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4139 if (this->peek_token()->is_eof())
4141 error_at(this->location(), "expected %<}%>");
4144 this->comm_clause(select_clauses, &saw_default);
4147 this->advance_token();
4149 statement->add_clauses(select_clauses);
4151 this->pop_break_statement();
4153 this->gogo_->add_statement(statement);
4156 // CommClause = CommCase [ StatementList ] .
4159 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4161 source_location location = this->location();
4162 bool is_send = false;
4163 Expression* channel = NULL;
4164 Expression* val = NULL;
4165 std::string varname;
4166 bool is_default = false;
4167 bool got_case = this->comm_case(&is_send, &channel, &val, &varname,
4170 Block* statements = NULL;
4171 Named_object* var = NULL;
4172 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4173 this->advance_token();
4174 else if (this->statement_list_may_start_here())
4176 this->gogo_->start_block(this->location());
4178 if (!varname.empty())
4180 // FIXME: LOCATION is slightly wrong here.
4181 Variable* v = new Variable(NULL, channel, false, false, false,
4183 v->set_type_from_chan_element();
4184 var = this->gogo_->add_variable(varname, v);
4187 this->statement_list();
4188 statements = this->gogo_->finish_block(this->location());
4195 error_at(location, "multiple defaults in select");
4198 *saw_default = true;
4202 clauses->add(is_send, channel, val, var, is_default, statements, location);
4205 // CommCase = ( "default" | ( "case" ( SendExpr | RecvExpr) ) ) ":" .
4208 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4209 std::string* varname, bool* is_default)
4211 const Token* token = this->peek_token();
4212 if (token->is_keyword(KEYWORD_DEFAULT))
4214 this->advance_token();
4217 else if (token->is_keyword(KEYWORD_CASE))
4219 this->advance_token();
4220 if (!this->send_or_recv_expr(is_send, channel, val, varname))
4225 error_at(this->location(), "expected %<case%> or %<default%>");
4226 if (!token->is_op(OPERATOR_RCURLY))
4227 this->advance_token();
4231 if (!this->peek_token()->is_op(OPERATOR_COLON))
4233 error_at(this->location(), "expected colon");
4237 this->advance_token();
4242 // SendExpr = Expression "<-" Expression .
4243 // RecvExpr = [ Expression ( "=" | ":=" ) ] "<-" Expression .
4246 Parse::send_or_recv_expr(bool* is_send, Expression** channel, Expression** val,
4247 std::string* varname)
4249 const Token* token = this->peek_token();
4250 source_location location = token->location();
4251 if (token->is_identifier())
4253 std::string recv_var = token->identifier();
4254 bool is_var_exported = token->is_identifier_exported();
4255 if (!this->advance_token()->is_op(OPERATOR_COLONEQ))
4256 this->unget_token(Token::make_identifier_token(recv_var,
4261 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4263 error_at(this->location(), "expected %<<-%>");
4267 *varname = this->gogo_->pack_hidden_name(recv_var, is_var_exported);
4268 this->advance_token();
4269 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4274 if (this->peek_token()->is_op(OPERATOR_CHANOP))
4277 this->advance_token();
4278 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4282 Expression* left = this->expression(PRECEDENCE_CHANOP, true, true, NULL);
4284 if (this->peek_token()->is_op(OPERATOR_EQ))
4286 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
4288 error_at(this->location(), "missing %<<-%>");
4293 this->advance_token();
4294 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4296 else if (this->peek_token()->is_op(OPERATOR_CHANOP))
4299 *channel = this->verify_not_sink(left);
4300 this->advance_token();
4301 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4305 error_at(this->location(), "expected %<<-%> or %<=%>");
4313 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
4314 // Condition = Expression .
4317 Parse::for_stat(const Label* label)
4319 gcc_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
4320 source_location location = this->location();
4321 const Token* token = this->advance_token();
4323 // Open a block to hold any variables defined in the init statement
4324 // of the for statement.
4325 this->gogo_->start_block(location);
4328 Expression* cond = NULL;
4330 Range_clause range_clause;
4332 if (!token->is_op(OPERATOR_LCURLY))
4334 if (token->is_keyword(KEYWORD_VAR))
4336 error_at(this->location(),
4337 "var declaration not allowed in for initializer");
4341 if (token->is_op(OPERATOR_SEMICOLON))
4342 this->for_clause(&cond, &post);
4345 // We might be looking at a Condition, an InitStat, or a
4347 cond = this->simple_stat(false, true, &range_clause, NULL);
4348 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4350 if (cond == NULL && !range_clause.found)
4351 error_at(this->location(), "parse error in for statement");
4355 if (range_clause.found)
4356 error_at(this->location(), "parse error after range clause");
4360 // COND is actually an expression statement for
4361 // InitStat at the start of a ForClause.
4362 this->expression_stat(cond);
4366 this->for_clause(&cond, &post);
4371 // Build the For_statement and note that it is the current target
4372 // for break and continue statements.
4374 For_statement* sfor;
4375 For_range_statement* srange;
4377 if (!range_clause.found)
4379 sfor = Statement::make_for_statement(init, cond, post, location);
4385 srange = Statement::make_for_range_statement(range_clause.index,
4393 this->push_break_statement(s, label);
4394 this->push_continue_statement(s, label);
4396 // Gather the block of statements in the loop and add them to the
4399 this->gogo_->start_block(this->location());
4400 source_location end_loc = this->block();
4401 Block* statements = this->gogo_->finish_block(end_loc);
4404 sfor->add_statements(statements);
4406 srange->add_statements(statements);
4408 // This is no longer the break/continue target.
4409 this->pop_break_statement();
4410 this->pop_continue_statement();
4412 // Add the For_statement to the list of statements, and close out
4413 // the block we started to hold any variables defined in the for
4416 this->gogo_->add_statement(s);
4418 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4422 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
4423 // InitStat = SimpleStat .
4424 // PostStat = SimpleStat .
4426 // We have already read InitStat at this point.
4429 Parse::for_clause(Expression** cond, Block** post)
4431 gcc_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
4432 this->advance_token();
4433 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4435 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
4437 error_at(this->location(),
4438 "unexpected semicolon or newline before %<{%>");
4444 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
4445 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
4446 error_at(this->location(), "expected semicolon");
4448 this->advance_token();
4450 if (this->peek_token()->is_op(OPERATOR_LCURLY))
4454 this->gogo_->start_block(this->location());
4455 this->simple_stat(false, false, NULL, NULL);
4456 *post = this->gogo_->finish_block(this->location());
4460 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
4462 // This is the := version. It is called with a list of identifiers.
4465 Parse::range_clause_decl(const Typed_identifier_list* til,
4466 Range_clause* p_range_clause)
4468 gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4469 source_location location = this->location();
4471 p_range_clause->found = true;
4473 gcc_assert(til->size() >= 1);
4474 if (til->size() > 2)
4475 error_at(this->location(), "too many variables for range clause");
4477 this->advance_token();
4478 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL);
4479 p_range_clause->range = expr;
4481 bool any_new = false;
4483 const Typed_identifier* pti = &til->front();
4484 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new);
4485 if (any_new && no->is_variable())
4486 no->var_value()->set_type_from_range_index();
4487 p_range_clause->index = Expression::make_var_reference(no, location);
4489 if (til->size() == 1)
4490 p_range_clause->value = NULL;
4494 bool is_new = false;
4495 no = this->init_var(*pti, NULL, expr, true, true, &is_new);
4496 if (is_new && no->is_variable())
4497 no->var_value()->set_type_from_range_value();
4500 p_range_clause->value = Expression::make_var_reference(no, location);
4504 error_at(location, "variables redeclared but no variable is new");
4507 // The = version of RangeClause. This is called with a list of
4511 Parse::range_clause_expr(const Expression_list* vals,
4512 Range_clause* p_range_clause)
4514 gcc_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
4516 p_range_clause->found = true;
4518 gcc_assert(vals->size() >= 1);
4519 if (vals->size() > 2)
4520 error_at(this->location(), "too many variables for range clause");
4522 this->advance_token();
4523 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
4526 p_range_clause->index = vals->front();
4527 if (vals->size() == 1)
4528 p_range_clause->value = NULL;
4530 p_range_clause->value = vals->back();
4533 // Push a statement on the break stack.
4536 Parse::push_break_statement(Statement* enclosing, const Label* label)
4538 if (this->break_stack_ == NULL)
4539 this->break_stack_ = new Bc_stack();
4540 this->break_stack_->push_back(std::make_pair(enclosing, label));
4543 // Push a statement on the continue stack.
4546 Parse::push_continue_statement(Statement* enclosing, const Label* label)
4548 if (this->continue_stack_ == NULL)
4549 this->continue_stack_ = new Bc_stack();
4550 this->continue_stack_->push_back(std::make_pair(enclosing, label));
4553 // Pop the break stack.
4556 Parse::pop_break_statement()
4558 this->break_stack_->pop_back();
4561 // Pop the continue stack.
4564 Parse::pop_continue_statement()
4566 this->continue_stack_->pop_back();
4569 // Find a break or continue statement given a label name.
4572 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
4574 if (bc_stack == NULL)
4576 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
4577 p != bc_stack->rend();
4579 if (p->second != NULL && p->second->name() == label)
4584 // BreakStat = "break" [ identifier ] .
4589 gcc_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
4590 source_location location = this->location();
4592 const Token* token = this->advance_token();
4593 Statement* enclosing;
4594 if (!token->is_identifier())
4596 if (this->break_stack_ == NULL || this->break_stack_->empty())
4598 error_at(this->location(),
4599 "break statement not within for or switch or select");
4602 enclosing = this->break_stack_->back().first;
4606 enclosing = this->find_bc_statement(this->break_stack_,
4607 token->identifier());
4608 if (enclosing == NULL)
4610 error_at(token->location(),
4611 ("break label %qs not associated with "
4612 "for or switch or select"),
4613 Gogo::message_name(token->identifier()).c_str());
4614 this->advance_token();
4617 this->advance_token();
4620 Unnamed_label* label;
4621 if (enclosing->classification() == Statement::STATEMENT_FOR)
4622 label = enclosing->for_statement()->break_label();
4623 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
4624 label = enclosing->for_range_statement()->break_label();
4625 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
4626 label = enclosing->switch_statement()->break_label();
4627 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
4628 label = enclosing->type_switch_statement()->break_label();
4629 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
4630 label = enclosing->select_statement()->break_label();
4634 this->gogo_->add_statement(Statement::make_break_statement(label,
4638 // ContinueStat = "continue" [ identifier ] .
4641 Parse::continue_stat()
4643 gcc_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
4644 source_location location = this->location();
4646 const Token* token = this->advance_token();
4647 Statement* enclosing;
4648 if (!token->is_identifier())
4650 if (this->continue_stack_ == NULL || this->continue_stack_->empty())
4652 error_at(this->location(), "continue statement not within for");
4655 enclosing = this->continue_stack_->back().first;
4659 enclosing = this->find_bc_statement(this->continue_stack_,
4660 token->identifier());
4661 if (enclosing == NULL)
4663 error_at(token->location(),
4664 "continue label %qs not associated with for",
4665 Gogo::message_name(token->identifier()).c_str());
4666 this->advance_token();
4669 this->advance_token();
4672 Unnamed_label* label;
4673 if (enclosing->classification() == Statement::STATEMENT_FOR)
4674 label = enclosing->for_statement()->continue_label();
4675 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
4676 label = enclosing->for_range_statement()->continue_label();
4680 this->gogo_->add_statement(Statement::make_continue_statement(label,
4684 // GotoStat = "goto" identifier .
4689 gcc_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
4690 source_location location = this->location();
4691 const Token* token = this->advance_token();
4692 if (!token->is_identifier())
4693 error_at(this->location(), "expected label for goto");
4696 Label* label = this->gogo_->add_label_reference(token->identifier());
4697 Statement* s = Statement::make_goto_statement(label, location);
4698 this->gogo_->add_statement(s);
4699 this->advance_token();
4703 // PackageClause = "package" PackageName .
4706 Parse::package_clause()
4708 const Token* token = this->peek_token();
4709 source_location location = token->location();
4711 if (!token->is_keyword(KEYWORD_PACKAGE))
4713 error_at(this->location(), "program must start with package clause");
4718 token = this->advance_token();
4719 if (token->is_identifier())
4721 name = token->identifier();
4724 error_at(this->location(), "invalid package name _");
4727 this->advance_token();
4731 error_at(this->location(), "package name must be an identifier");
4735 this->gogo_->set_package_name(name, location);
4738 // ImportDecl = "import" Decl<ImportSpec> .
4741 Parse::import_decl()
4743 gcc_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
4744 this->advance_token();
4745 this->decl(&Parse::import_spec, NULL);
4748 // ImportSpec = [ "." | PackageName ] PackageFileName .
4751 Parse::import_spec(void*)
4753 const Token* token = this->peek_token();
4754 source_location location = token->location();
4756 std::string local_name;
4757 bool is_local_name_exported = false;
4758 if (token->is_op(OPERATOR_DOT))
4761 token = this->advance_token();
4763 else if (token->is_identifier())
4765 local_name = token->identifier();
4766 is_local_name_exported = token->is_identifier_exported();
4767 token = this->advance_token();
4770 if (!token->is_string())
4772 error_at(this->location(), "missing import package name");
4776 this->gogo_->import_package(token->string_value(), local_name,
4777 is_local_name_exported, location);
4779 this->advance_token();
4782 // SourceFile = PackageClause ";" { ImportDecl ";" }
4783 // { TopLevelDecl ";" } .
4788 this->package_clause();
4790 const Token* token = this->peek_token();
4791 if (token->is_op(OPERATOR_SEMICOLON))
4792 token = this->advance_token();
4794 error_at(this->location(),
4795 "expected %<;%> or newline after package clause");
4797 while (token->is_keyword(KEYWORD_IMPORT))
4799 this->import_decl();
4800 token = this->peek_token();
4801 if (token->is_op(OPERATOR_SEMICOLON))
4802 token = this->advance_token();
4804 error_at(this->location(),
4805 "expected %<;%> or newline after import declaration");
4808 while (!token->is_eof())
4810 if (this->declaration_may_start_here())
4811 this->declaration();
4814 error_at(this->location(), "expected declaration");
4816 this->advance_token();
4817 while (!this->peek_token()->is_eof()
4818 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
4819 && !this->peek_token()->is_op(OPERATOR_RCURLY));
4820 if (!this->peek_token()->is_eof()
4821 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
4822 this->advance_token();
4824 token = this->peek_token();
4825 if (token->is_op(OPERATOR_SEMICOLON))
4826 token = this->advance_token();
4827 else if (!token->is_eof() || !saw_errors())
4829 error_at(this->location(),
4830 "expected %<;%> or newline after top level declaration");
4831 this->skip_past_error(OPERATOR_INVALID);
4836 // Reset the current iota value.
4844 // Return the current iota value.
4852 // Increment the current iota value.
4855 Parse::increment_iota()
4860 // Skip forward to a semicolon or OP. OP will normally be
4861 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
4862 // past it and return. If we find OP, it will be the next token to
4863 // read. Return true if we are OK, false if we found EOF.
4866 Parse::skip_past_error(Operator op)
4868 const Token* token = this->peek_token();
4869 while (!token->is_op(op))
4871 if (token->is_eof())
4873 if (token->is_op(OPERATOR_SEMICOLON))
4875 this->advance_token();
4878 token = this->advance_token();
4883 // Check that an expression is not a sink.
4886 Parse::verify_not_sink(Expression* expr)
4888 if (expr->is_sink_expression())
4890 error_at(expr->location(), "cannot use _ as value");
4891 expr = Expression::make_error(expr->location());