1 // parse.cc -- Go frontend parser.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 #include "statements.h"
13 #include "expressions.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
22 const Enclosing_var& v2)
24 if (v1.var() == v2.var())
27 const std::string& n1(v1.var()->name());
28 const std::string& n2(v2.var()->name());
29 int i = n1.compare(n2);
35 // If we get here it means that a single nested function refers to
36 // two different variables defined in enclosing functions, and both
37 // variables have the same name. I think this is impossible.
43 Parse::Parse(Lex* lex, Gogo* gogo)
45 token_(Token::make_invalid_token(0)),
46 unget_token_(Token::make_invalid_token(0)),
47 unget_token_valid_(false),
50 continue_stack_(NULL),
56 // Return the current token.
61 if (this->unget_token_valid_)
62 return &this->unget_token_;
63 if (this->token_.is_invalid())
64 this->token_ = this->lex_->next_token();
68 // Advance to the next token and return it.
71 Parse::advance_token()
73 if (this->unget_token_valid_)
75 this->unget_token_valid_ = false;
76 if (!this->token_.is_invalid())
79 this->token_ = this->lex_->next_token();
83 // Push a token back on the input stream.
86 Parse::unget_token(const Token& token)
88 go_assert(!this->unget_token_valid_);
89 this->unget_token_ = token;
90 this->unget_token_valid_ = true;
93 // The location of the current token.
98 return this->peek_token()->location();
101 // IdentifierList = identifier { "," identifier } .
104 Parse::identifier_list(Typed_identifier_list* til)
106 const Token* token = this->peek_token();
109 if (!token->is_identifier())
111 error_at(this->location(), "expected identifier");
115 this->gogo_->pack_hidden_name(token->identifier(),
116 token->is_identifier_exported());
117 til->push_back(Typed_identifier(name, NULL, token->location()));
118 token = this->advance_token();
119 if (!token->is_op(OPERATOR_COMMA))
121 token = this->advance_token();
125 // ExpressionList = Expression { "," Expression } .
127 // If MAY_BE_SINK is true, the expressions in the list may be "_".
130 Parse::expression_list(Expression* first, bool may_be_sink)
132 Expression_list* ret = new Expression_list();
134 ret->push_back(first);
137 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink, true,
140 const Token* token = this->peek_token();
141 if (!token->is_op(OPERATOR_COMMA))
144 // Most expression lists permit a trailing comma.
145 source_location location = token->location();
146 this->advance_token();
147 if (!this->expression_may_start_here())
149 this->unget_token(Token::make_operator_token(OPERATOR_COMMA,
156 // QualifiedIdent = [ PackageName "." ] identifier .
157 // PackageName = identifier .
159 // This sets *PNAME to the identifier and sets *PPACKAGE to the
160 // package or NULL if there isn't one. This returns true on success,
161 // false on failure in which case it will have emitted an error
165 Parse::qualified_ident(std::string* pname, Named_object** ppackage)
167 const Token* token = this->peek_token();
168 if (!token->is_identifier())
170 error_at(this->location(), "expected identifier");
174 std::string name = token->identifier();
175 bool is_exported = token->is_identifier_exported();
176 name = this->gogo_->pack_hidden_name(name, is_exported);
178 token = this->advance_token();
179 if (!token->is_op(OPERATOR_DOT))
186 Named_object* package = this->gogo_->lookup(name, NULL);
187 if (package == NULL || !package->is_package())
189 error_at(this->location(), "expected package");
190 // We expect . IDENTIFIER; skip both.
191 if (this->advance_token()->is_identifier())
192 this->advance_token();
196 package->package_value()->set_used();
198 token = this->advance_token();
199 if (!token->is_identifier())
201 error_at(this->location(), "expected identifier");
205 name = token->identifier();
209 error_at(this->location(), "invalid use of %<_%>");
213 if (package->name() == this->gogo_->package_name())
214 name = this->gogo_->pack_hidden_name(name,
215 token->is_identifier_exported());
220 this->advance_token();
225 // Type = TypeName | TypeLit | "(" Type ")" .
227 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
228 // SliceType | MapType | ChannelType .
233 const Token* token = this->peek_token();
234 if (token->is_identifier())
235 return this->type_name(true);
236 else if (token->is_op(OPERATOR_LSQUARE))
237 return this->array_type(false);
238 else if (token->is_keyword(KEYWORD_CHAN)
239 || token->is_op(OPERATOR_CHANOP))
240 return this->channel_type();
241 else if (token->is_keyword(KEYWORD_INTERFACE))
242 return this->interface_type();
243 else if (token->is_keyword(KEYWORD_FUNC))
245 source_location location = token->location();
246 this->advance_token();
247 Type* type = this->signature(NULL, location);
249 return Type::make_error_type();
252 else if (token->is_keyword(KEYWORD_MAP))
253 return this->map_type();
254 else if (token->is_keyword(KEYWORD_STRUCT))
255 return this->struct_type();
256 else if (token->is_op(OPERATOR_MULT))
257 return this->pointer_type();
258 else if (token->is_op(OPERATOR_LPAREN))
260 this->advance_token();
261 Type* ret = this->type();
262 if (this->peek_token()->is_op(OPERATOR_RPAREN))
263 this->advance_token();
266 if (!ret->is_error_type())
267 error_at(this->location(), "expected %<)%>");
273 error_at(token->location(), "expected type");
274 return Type::make_error_type();
279 Parse::type_may_start_here()
281 const Token* token = this->peek_token();
282 return (token->is_identifier()
283 || token->is_op(OPERATOR_LSQUARE)
284 || token->is_op(OPERATOR_CHANOP)
285 || token->is_keyword(KEYWORD_CHAN)
286 || token->is_keyword(KEYWORD_INTERFACE)
287 || token->is_keyword(KEYWORD_FUNC)
288 || token->is_keyword(KEYWORD_MAP)
289 || token->is_keyword(KEYWORD_STRUCT)
290 || token->is_op(OPERATOR_MULT)
291 || token->is_op(OPERATOR_LPAREN));
294 // TypeName = QualifiedIdent .
296 // If MAY_BE_NIL is true, then an identifier with the value of the
297 // predefined constant nil is accepted, returning the nil type.
300 Parse::type_name(bool issue_error)
302 source_location location = this->location();
305 Named_object* package;
306 if (!this->qualified_ident(&name, &package))
307 return Type::make_error_type();
309 Named_object* named_object;
311 named_object = this->gogo_->lookup(name, NULL);
314 named_object = package->package_value()->lookup(name);
315 if (named_object == NULL
317 && package->name() != this->gogo_->package_name())
319 // Check whether the name is there but hidden.
320 std::string s = ('.' + package->package_value()->unique_prefix()
321 + '.' + package->package_value()->name()
323 named_object = package->package_value()->lookup(s);
324 if (named_object != NULL)
326 const std::string& packname(package->package_value()->name());
327 error_at(location, "invalid reference to hidden type %<%s.%s%>",
328 Gogo::message_name(packname).c_str(),
329 Gogo::message_name(name).c_str());
336 if (named_object == NULL)
339 named_object = this->gogo_->add_unknown_name(name, location);
342 const std::string& packname(package->package_value()->name());
343 error_at(location, "reference to undefined identifier %<%s.%s%>",
344 Gogo::message_name(packname).c_str(),
345 Gogo::message_name(name).c_str());
350 else if (named_object->is_type())
352 if (!named_object->type_value()->is_visible())
355 else if (named_object->is_unknown() || named_object->is_type_declaration())
363 error_at(location, "expected type");
364 return Type::make_error_type();
367 if (named_object->is_type())
368 return named_object->type_value();
369 else if (named_object->is_unknown() || named_object->is_type_declaration())
370 return Type::make_forward_declaration(named_object);
375 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
376 // ArrayLength = Expression .
377 // ElementType = CompleteType .
380 Parse::array_type(bool may_use_ellipsis)
382 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
383 const Token* token = this->advance_token();
385 Expression* length = NULL;
386 if (token->is_op(OPERATOR_RSQUARE))
387 this->advance_token();
390 if (!token->is_op(OPERATOR_ELLIPSIS))
391 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
392 else if (may_use_ellipsis)
394 // An ellipsis is used in composite literals to represent a
395 // fixed array of the size of the number of elements. We
396 // use a length of nil to represent this, and change the
397 // length when parsing the composite literal.
398 length = Expression::make_nil(this->location());
399 this->advance_token();
403 error_at(this->location(),
404 "use of %<[...]%> outside of array literal");
405 length = Expression::make_error(this->location());
406 this->advance_token();
408 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
410 error_at(this->location(), "expected %<]%>");
411 return Type::make_error_type();
413 this->advance_token();
416 Type* element_type = this->type();
418 return Type::make_array_type(element_type, length);
421 // MapType = "map" "[" KeyType "]" ValueType .
422 // KeyType = CompleteType .
423 // ValueType = CompleteType .
428 source_location location = this->location();
429 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
430 if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
432 error_at(this->location(), "expected %<[%>");
433 return Type::make_error_type();
435 this->advance_token();
437 Type* key_type = this->type();
439 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
441 error_at(this->location(), "expected %<]%>");
442 return Type::make_error_type();
444 this->advance_token();
446 Type* value_type = this->type();
448 if (key_type->is_error_type() || value_type->is_error_type())
449 return Type::make_error_type();
451 return Type::make_map_type(key_type, value_type, location);
454 // StructType = "struct" "{" { FieldDecl ";" } "}" .
459 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
460 source_location location = this->location();
461 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
463 source_location token_loc = this->location();
464 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
465 && this->advance_token()->is_op(OPERATOR_LCURLY))
466 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
469 error_at(this->location(), "expected %<{%>");
470 return Type::make_error_type();
473 this->advance_token();
475 Struct_field_list* sfl = new Struct_field_list;
476 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
478 this->field_decl(sfl);
479 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
480 this->advance_token();
481 else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
483 error_at(this->location(), "expected %<;%> or %<}%> or newline");
484 if (!this->skip_past_error(OPERATOR_RCURLY))
485 return Type::make_error_type();
488 this->advance_token();
490 for (Struct_field_list::const_iterator pi = sfl->begin();
494 if (pi->type()->is_error_type())
496 for (Struct_field_list::const_iterator pj = pi + 1;
500 if (pi->field_name() == pj->field_name()
501 && !Gogo::is_sink_name(pi->field_name()))
502 error_at(pi->location(), "duplicate field name %<%s%>",
503 Gogo::message_name(pi->field_name()).c_str());
507 return Type::make_struct_type(sfl, location);
510 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
511 // Tag = string_lit .
514 Parse::field_decl(Struct_field_list* sfl)
516 const Token* token = this->peek_token();
517 source_location location = token->location();
519 bool is_anonymous_pointer;
520 if (token->is_op(OPERATOR_MULT))
523 is_anonymous_pointer = true;
525 else if (token->is_identifier())
527 std::string id = token->identifier();
528 bool is_id_exported = token->is_identifier_exported();
529 source_location id_location = token->location();
530 token = this->advance_token();
531 is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
532 || token->is_op(OPERATOR_RCURLY)
533 || token->is_op(OPERATOR_DOT)
534 || token->is_string());
535 is_anonymous_pointer = false;
536 this->unget_token(Token::make_identifier_token(id, is_id_exported,
541 error_at(this->location(), "expected field name");
542 while (!token->is_op(OPERATOR_SEMICOLON)
543 && !token->is_op(OPERATOR_RCURLY)
545 token = this->advance_token();
551 if (is_anonymous_pointer)
553 this->advance_token();
554 if (!this->peek_token()->is_identifier())
556 error_at(this->location(), "expected field name");
557 while (!token->is_op(OPERATOR_SEMICOLON)
558 && !token->is_op(OPERATOR_RCURLY)
560 token = this->advance_token();
564 Type* type = this->type_name(true);
567 if (this->peek_token()->is_string())
569 tag = this->peek_token()->string_value();
570 this->advance_token();
573 if (!type->is_error_type())
575 if (is_anonymous_pointer)
576 type = Type::make_pointer_type(type);
577 sfl->push_back(Struct_field(Typed_identifier("", type, location)));
579 sfl->back().set_tag(tag);
584 Typed_identifier_list til;
587 token = this->peek_token();
588 if (!token->is_identifier())
590 error_at(this->location(), "expected identifier");
594 this->gogo_->pack_hidden_name(token->identifier(),
595 token->is_identifier_exported());
596 til.push_back(Typed_identifier(name, NULL, token->location()));
597 if (!this->advance_token()->is_op(OPERATOR_COMMA))
599 this->advance_token();
602 Type* type = this->type();
605 if (this->peek_token()->is_string())
607 tag = this->peek_token()->string_value();
608 this->advance_token();
611 for (Typed_identifier_list::iterator p = til.begin();
616 sfl->push_back(Struct_field(*p));
618 sfl->back().set_tag(tag);
623 // PointerType = "*" Type .
626 Parse::pointer_type()
628 go_assert(this->peek_token()->is_op(OPERATOR_MULT));
629 this->advance_token();
630 Type* type = this->type();
631 if (type->is_error_type())
633 return Type::make_pointer_type(type);
636 // ChannelType = Channel | SendChannel | RecvChannel .
637 // Channel = "chan" ElementType .
638 // SendChannel = "chan" "<-" ElementType .
639 // RecvChannel = "<-" "chan" ElementType .
642 Parse::channel_type()
644 const Token* token = this->peek_token();
647 if (token->is_op(OPERATOR_CHANOP))
649 if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
651 error_at(this->location(), "expected %<chan%>");
652 return Type::make_error_type();
655 this->advance_token();
659 go_assert(token->is_keyword(KEYWORD_CHAN));
660 if (this->advance_token()->is_op(OPERATOR_CHANOP))
663 this->advance_token();
667 // Better error messages for the common error of omitting the
668 // channel element type.
669 if (!this->type_may_start_here())
671 token = this->peek_token();
672 if (token->is_op(OPERATOR_RCURLY))
673 error_at(this->location(), "unexpected %<}%> in channel type");
674 else if (token->is_op(OPERATOR_RPAREN))
675 error_at(this->location(), "unexpected %<)%> in channel type");
676 else if (token->is_op(OPERATOR_COMMA))
677 error_at(this->location(), "unexpected comma in channel type");
679 error_at(this->location(), "expected channel element type");
680 return Type::make_error_type();
683 Type* element_type = this->type();
684 return Type::make_channel_type(send, receive, element_type);
687 // Give an error for a duplicate parameter or receiver name.
690 Parse::check_signature_names(const Typed_identifier_list* params,
693 for (Typed_identifier_list::const_iterator p = params->begin();
697 if (p->name().empty() || Gogo::is_sink_name(p->name()))
699 std::pair<std::string, const Typed_identifier*> val =
700 std::make_pair(p->name(), &*p);
701 std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
704 error_at(p->location(), "redefinition of %qs",
705 Gogo::message_name(p->name()).c_str());
706 inform(ins.first->second->location(),
707 "previous definition of %qs was here",
708 Gogo::message_name(p->name()).c_str());
713 // Signature = Parameters [ Result ] .
715 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
716 // location of the start of the type.
718 // This returns NULL on a parse error.
721 Parse::signature(Typed_identifier* receiver, source_location location)
723 bool is_varargs = false;
724 Typed_identifier_list* params;
725 bool params_ok = this->parameters(¶ms, &is_varargs);
727 Typed_identifier_list* results = NULL;
728 if (this->peek_token()->is_op(OPERATOR_LPAREN)
729 || this->type_may_start_here())
731 if (!this->result(&results))
740 this->check_signature_names(params, &names);
742 this->check_signature_names(results, &names);
744 Function_type* ret = Type::make_function_type(receiver, params, results,
747 ret->set_is_varargs();
751 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
753 // This returns false on a parse error.
756 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
760 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
762 error_at(this->location(), "expected %<(%>");
766 Typed_identifier_list* params = NULL;
767 bool saw_error = false;
769 const Token* token = this->advance_token();
770 if (!token->is_op(OPERATOR_RPAREN))
772 params = this->parameter_list(is_varargs);
775 token = this->peek_token();
778 // The optional trailing comma is picked up in parameter_list.
780 if (!token->is_op(OPERATOR_RPAREN))
781 error_at(this->location(), "expected %<)%>");
783 this->advance_token();
792 // ParameterList = ParameterDecl { "," ParameterDecl } .
794 // This sets *IS_VARARGS if the list ends with an ellipsis.
795 // IS_VARARGS will be NULL if varargs are not permitted.
797 // We pick up an optional trailing comma.
799 // This returns NULL if some error is seen.
801 Typed_identifier_list*
802 Parse::parameter_list(bool* is_varargs)
804 source_location location = this->location();
805 Typed_identifier_list* ret = new Typed_identifier_list();
807 bool saw_error = false;
809 // If we see an identifier and then a comma, then we don't know
810 // whether we are looking at a list of identifiers followed by a
811 // type, or a list of types given by name. We have to do an
812 // arbitrary lookahead to figure it out.
814 bool parameters_have_names;
815 const Token* token = this->peek_token();
816 if (!token->is_identifier())
818 // This must be a type which starts with something like '*'.
819 parameters_have_names = false;
823 std::string name = token->identifier();
824 bool is_exported = token->is_identifier_exported();
825 source_location location = token->location();
826 token = this->advance_token();
827 if (!token->is_op(OPERATOR_COMMA))
829 if (token->is_op(OPERATOR_DOT))
831 // This is a qualified identifier, which must turn out
833 parameters_have_names = false;
835 else if (token->is_op(OPERATOR_RPAREN))
837 // A single identifier followed by a parenthesis must be
839 parameters_have_names = false;
843 // An identifier followed by something other than a
844 // comma or a dot or a right parenthesis must be a
845 // parameter name followed by a type.
846 parameters_have_names = true;
849 this->unget_token(Token::make_identifier_token(name, is_exported,
854 // An identifier followed by a comma may be the first in a
855 // list of parameter names followed by a type, or it may be
856 // the first in a list of types without parameter names. To
857 // find out we gather as many identifiers separated by
859 std::string id_name = this->gogo_->pack_hidden_name(name,
861 ret->push_back(Typed_identifier(id_name, NULL, location));
862 bool just_saw_comma = true;
863 while (this->advance_token()->is_identifier())
865 name = this->peek_token()->identifier();
866 is_exported = this->peek_token()->is_identifier_exported();
867 location = this->peek_token()->location();
868 id_name = this->gogo_->pack_hidden_name(name, is_exported);
869 ret->push_back(Typed_identifier(id_name, NULL, location));
870 if (!this->advance_token()->is_op(OPERATOR_COMMA))
872 just_saw_comma = false;
879 // We saw ID1 "," ID2 "," followed by something which
880 // was not an identifier. We must be seeing the start
881 // of a type, and ID1 and ID2 must be types, and the
882 // parameters don't have names.
883 parameters_have_names = false;
885 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
887 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
888 // and the parameters don't have names.
889 parameters_have_names = false;
891 else if (this->peek_token()->is_op(OPERATOR_DOT))
893 // We saw ID1 "," ID2 ".". ID2 must be a package name,
894 // ID1 must be a type, and the parameters don't have
896 parameters_have_names = false;
897 this->unget_token(Token::make_identifier_token(name, is_exported,
900 just_saw_comma = true;
904 // We saw ID1 "," ID2 followed by something other than
905 // ",", ".", or ")". We must be looking at the start of
906 // a type, and ID1 and ID2 must be parameter names.
907 parameters_have_names = true;
910 if (parameters_have_names)
912 go_assert(!just_saw_comma);
913 // We have just seen ID1, ID2 xxx.
915 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
919 error_at(this->location(), "%<...%> only permits one name");
921 this->advance_token();
924 for (size_t i = 0; i < ret->size(); ++i)
925 ret->set_type(i, type);
926 if (!this->peek_token()->is_op(OPERATOR_COMMA))
927 return saw_error ? NULL : ret;
928 if (this->advance_token()->is_op(OPERATOR_RPAREN))
929 return saw_error ? NULL : ret;
933 Typed_identifier_list* tret = new Typed_identifier_list();
934 for (Typed_identifier_list::const_iterator p = ret->begin();
938 Named_object* no = this->gogo_->lookup(p->name(), NULL);
941 no = this->gogo_->add_unknown_name(p->name(),
945 type = no->type_value();
946 else if (no->is_unknown() || no->is_type_declaration())
947 type = Type::make_forward_declaration(no);
950 error_at(p->location(), "expected %<%s%> to be a type",
951 Gogo::message_name(p->name()).c_str());
953 type = Type::make_error_type();
955 tret->push_back(Typed_identifier("", type, p->location()));
960 || this->peek_token()->is_op(OPERATOR_RPAREN))
961 return saw_error ? NULL : ret;
966 bool mix_error = false;
967 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
968 while (this->peek_token()->is_op(OPERATOR_COMMA))
970 if (is_varargs != NULL && *is_varargs)
972 error_at(this->location(), "%<...%> must be last parameter");
975 if (this->advance_token()->is_op(OPERATOR_RPAREN))
977 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
981 error_at(location, "invalid named/anonymous mix");
992 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
995 Parse::parameter_decl(bool parameters_have_names,
996 Typed_identifier_list* til,
1000 if (!parameters_have_names)
1003 source_location location = this->location();
1004 if (!this->peek_token()->is_identifier())
1006 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1007 type = this->type();
1010 if (is_varargs == NULL)
1011 error_at(this->location(), "invalid use of %<...%>");
1014 this->advance_token();
1015 if (is_varargs == NULL
1016 && this->peek_token()->is_op(OPERATOR_RPAREN))
1017 type = Type::make_error_type();
1020 Type* element_type = this->type();
1021 type = Type::make_array_type(element_type, NULL);
1027 type = this->type_name(false);
1028 if (type->is_error_type()
1029 || (!this->peek_token()->is_op(OPERATOR_COMMA)
1030 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1033 while (!this->peek_token()->is_op(OPERATOR_COMMA)
1034 && !this->peek_token()->is_op(OPERATOR_RPAREN))
1035 this->advance_token();
1038 if (!type->is_error_type())
1039 til->push_back(Typed_identifier("", type, location));
1043 size_t orig_count = til->size();
1044 if (this->peek_token()->is_identifier())
1045 this->identifier_list(til);
1048 size_t new_count = til->size();
1051 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1052 type = this->type();
1055 if (is_varargs == NULL)
1056 error_at(this->location(), "invalid use of %<...%>");
1057 else if (new_count > orig_count + 1)
1058 error_at(this->location(), "%<...%> only permits one name");
1061 this->advance_token();
1062 Type* element_type = this->type();
1063 type = Type::make_array_type(element_type, NULL);
1065 for (size_t i = orig_count; i < new_count; ++i)
1066 til->set_type(i, type);
1070 // Result = Parameters | Type .
1072 // This returns false on a parse error.
1075 Parse::result(Typed_identifier_list** presults)
1077 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1078 return this->parameters(presults, NULL);
1081 source_location location = this->location();
1082 Type* type = this->type();
1083 if (type->is_error_type())
1088 Typed_identifier_list* til = new Typed_identifier_list();
1089 til->push_back(Typed_identifier("", type, location));
1095 // Block = "{" [ StatementList ] "}" .
1097 // Returns the location of the closing brace.
1102 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1104 source_location loc = this->location();
1105 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1106 && this->advance_token()->is_op(OPERATOR_LCURLY))
1107 error_at(loc, "unexpected semicolon or newline before %<{%>");
1110 error_at(this->location(), "expected %<{%>");
1111 return UNKNOWN_LOCATION;
1115 const Token* token = this->advance_token();
1117 if (!token->is_op(OPERATOR_RCURLY))
1119 this->statement_list();
1120 token = this->peek_token();
1121 if (!token->is_op(OPERATOR_RCURLY))
1123 if (!token->is_eof() || !saw_errors())
1124 error_at(this->location(), "expected %<}%>");
1126 // Skip ahead to the end of the block, in hopes of avoiding
1127 // lots of meaningless errors.
1128 source_location ret = token->location();
1130 while (!token->is_eof())
1132 if (token->is_op(OPERATOR_LCURLY))
1134 else if (token->is_op(OPERATOR_RCURLY))
1139 this->advance_token();
1143 token = this->advance_token();
1144 ret = token->location();
1150 source_location ret = token->location();
1151 this->advance_token();
1155 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1156 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1159 Parse::interface_type()
1161 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1162 source_location location = this->location();
1164 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1166 source_location token_loc = this->location();
1167 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1168 && this->advance_token()->is_op(OPERATOR_LCURLY))
1169 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1172 error_at(this->location(), "expected %<{%>");
1173 return Type::make_error_type();
1176 this->advance_token();
1178 Typed_identifier_list* methods = new Typed_identifier_list();
1179 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1181 this->method_spec(methods);
1182 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1184 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1186 this->method_spec(methods);
1188 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1190 error_at(this->location(), "expected %<}%>");
1191 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1193 if (this->peek_token()->is_eof())
1194 return Type::make_error_type();
1198 this->advance_token();
1200 if (methods->empty())
1206 Interface_type* ret = Type::make_interface_type(methods, location);
1207 this->gogo_->record_interface_type(ret);
1211 // MethodSpec = MethodName Signature | InterfaceTypeName .
1212 // MethodName = identifier .
1213 // InterfaceTypeName = TypeName .
1216 Parse::method_spec(Typed_identifier_list* methods)
1218 const Token* token = this->peek_token();
1219 if (!token->is_identifier())
1221 error_at(this->location(), "expected identifier");
1225 std::string name = token->identifier();
1226 bool is_exported = token->is_identifier_exported();
1227 source_location location = token->location();
1229 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1231 // This is a MethodName.
1232 name = this->gogo_->pack_hidden_name(name, is_exported);
1233 Type* type = this->signature(NULL, location);
1236 methods->push_back(Typed_identifier(name, type, location));
1240 this->unget_token(Token::make_identifier_token(name, is_exported,
1242 Type* type = this->type_name(false);
1243 if (type->is_error_type()
1244 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1245 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1247 if (this->peek_token()->is_op(OPERATOR_COMMA))
1248 error_at(this->location(),
1249 "name list not allowed in interface type");
1251 error_at(location, "expected signature or type name");
1252 token = this->peek_token();
1253 while (!token->is_eof()
1254 && !token->is_op(OPERATOR_SEMICOLON)
1255 && !token->is_op(OPERATOR_RCURLY))
1256 token = this->advance_token();
1259 // This must be an interface type, but we can't check that now.
1260 // We check it and pull out the methods in
1261 // Interface_type::do_verify.
1262 methods->push_back(Typed_identifier("", type, location));
1266 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1269 Parse::declaration()
1271 const Token* token = this->peek_token();
1272 if (token->is_keyword(KEYWORD_CONST))
1274 else if (token->is_keyword(KEYWORD_TYPE))
1276 else if (token->is_keyword(KEYWORD_VAR))
1278 else if (token->is_keyword(KEYWORD_FUNC))
1279 this->function_decl();
1282 error_at(this->location(), "expected declaration");
1283 this->advance_token();
1288 Parse::declaration_may_start_here()
1290 const Token* token = this->peek_token();
1291 return (token->is_keyword(KEYWORD_CONST)
1292 || token->is_keyword(KEYWORD_TYPE)
1293 || token->is_keyword(KEYWORD_VAR)
1294 || token->is_keyword(KEYWORD_FUNC));
1297 // Decl<P> = P | "(" [ List<P> ] ")" .
1300 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1302 if (this->peek_token()->is_eof())
1305 error_at(this->location(), "unexpected end of file");
1309 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1313 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1315 this->list(pfn, varg, true);
1316 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1318 error_at(this->location(), "missing %<)%>");
1319 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1321 if (this->peek_token()->is_eof())
1326 this->advance_token();
1330 // List<P> = P { ";" P } [ ";" ] .
1332 // In order to pick up the trailing semicolon we need to know what
1333 // might follow. This is either a '}' or a ')'.
1336 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1339 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1340 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1341 || this->peek_token()->is_op(OPERATOR_COMMA))
1343 if (this->peek_token()->is_op(OPERATOR_COMMA))
1344 error_at(this->location(), "unexpected comma");
1345 if (this->advance_token()->is_op(follow))
1351 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1356 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1357 this->advance_token();
1360 Type* last_type = NULL;
1361 Expression_list* last_expr_list = NULL;
1363 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1364 this->const_spec(&last_type, &last_expr_list);
1367 this->advance_token();
1368 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1370 this->const_spec(&last_type, &last_expr_list);
1371 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1372 this->advance_token();
1373 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1375 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1376 if (!this->skip_past_error(OPERATOR_RPAREN))
1380 this->advance_token();
1383 if (last_expr_list != NULL)
1384 delete last_expr_list;
1387 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1390 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1392 Typed_identifier_list til;
1393 this->identifier_list(&til);
1396 if (this->type_may_start_here())
1398 type = this->type();
1400 *last_expr_list = NULL;
1403 Expression_list *expr_list;
1404 if (!this->peek_token()->is_op(OPERATOR_EQ))
1406 if (*last_expr_list == NULL)
1408 error_at(this->location(), "expected %<=%>");
1412 expr_list = new Expression_list;
1413 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1414 p != (*last_expr_list)->end();
1416 expr_list->push_back((*p)->copy());
1420 this->advance_token();
1421 expr_list = this->expression_list(NULL, false);
1423 if (*last_expr_list != NULL)
1424 delete *last_expr_list;
1425 *last_expr_list = expr_list;
1428 Expression_list::const_iterator pe = expr_list->begin();
1429 for (Typed_identifier_list::iterator pi = til.begin();
1433 if (pe == expr_list->end())
1435 error_at(this->location(), "not enough initializers");
1441 if (!Gogo::is_sink_name(pi->name()))
1442 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1444 if (pe != expr_list->end())
1445 error_at(this->location(), "too many initializers");
1447 this->increment_iota();
1452 // TypeDecl = "type" Decl<TypeSpec> .
1457 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1458 this->advance_token();
1459 this->decl(&Parse::type_spec, NULL);
1462 // TypeSpec = identifier Type .
1465 Parse::type_spec(void*)
1467 const Token* token = this->peek_token();
1468 if (!token->is_identifier())
1470 error_at(this->location(), "expected identifier");
1473 std::string name = token->identifier();
1474 bool is_exported = token->is_identifier_exported();
1475 source_location location = token->location();
1476 token = this->advance_token();
1478 // The scope of the type name starts at the point where the
1479 // identifier appears in the source code. We implement this by
1480 // declaring the type before we read the type definition.
1481 Named_object* named_type = NULL;
1484 name = this->gogo_->pack_hidden_name(name, is_exported);
1485 named_type = this->gogo_->declare_type(name, location);
1489 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1490 type = this->type();
1493 error_at(this->location(),
1494 "unexpected semicolon or newline in type declaration");
1495 type = Type::make_error_type();
1496 this->advance_token();
1499 if (type->is_error_type())
1501 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1502 && !this->peek_token()->is_eof())
1503 this->advance_token();
1508 if (named_type->is_type_declaration())
1510 Type* ftype = type->forwarded();
1511 if (ftype->forward_declaration_type() != NULL
1512 && (ftype->forward_declaration_type()->named_object()
1515 error_at(location, "invalid recursive type");
1516 type = Type::make_error_type();
1519 this->gogo_->define_type(named_type,
1520 Type::make_named_type(named_type, type,
1522 go_assert(named_type->package() == NULL);
1526 // This will probably give a redefinition error.
1527 this->gogo_->add_type(name, type, location);
1532 // VarDecl = "var" Decl<VarSpec> .
1537 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1538 this->advance_token();
1539 this->decl(&Parse::var_spec, NULL);
1542 // VarSpec = IdentifierList
1543 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1546 Parse::var_spec(void*)
1548 // Get the variable names.
1549 Typed_identifier_list til;
1550 this->identifier_list(&til);
1552 source_location location = this->location();
1555 Expression_list* init = NULL;
1556 if (!this->peek_token()->is_op(OPERATOR_EQ))
1558 type = this->type();
1559 if (type->is_error_type())
1561 while (!this->peek_token()->is_op(OPERATOR_EQ)
1562 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1563 && !this->peek_token()->is_eof())
1564 this->advance_token();
1566 if (this->peek_token()->is_op(OPERATOR_EQ))
1568 this->advance_token();
1569 init = this->expression_list(NULL, false);
1574 this->advance_token();
1575 init = this->expression_list(NULL, false);
1578 this->init_vars(&til, type, init, false, location);
1584 // Create variables. TIL is a list of variable names. If TYPE is not
1585 // NULL, it is the type of all the variables. If INIT is not NULL, it
1586 // is an initializer list for the variables.
1589 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1590 Expression_list* init, bool is_coloneq,
1591 source_location location)
1593 // Check for an initialization which can yield multiple values.
1594 if (init != NULL && init->size() == 1 && til->size() > 1)
1596 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1599 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1602 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1605 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1606 is_coloneq, location))
1610 if (init != NULL && init->size() != til->size())
1612 if (init->empty() || !init->front()->is_error_expression())
1613 error_at(location, "wrong number of initializations");
1616 type = Type::make_error_type();
1619 // Note that INIT was already parsed with the old name bindings, so
1620 // we don't have to worry that it will accidentally refer to the
1621 // newly declared variables.
1623 Expression_list::const_iterator pexpr;
1625 pexpr = init->begin();
1626 bool any_new = false;
1627 for (Typed_identifier_list::const_iterator p = til->begin();
1632 go_assert(pexpr != init->end());
1633 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1639 go_assert(pexpr == init->end());
1640 if (is_coloneq && !any_new)
1641 error_at(location, "variables redeclared but no variable is new");
1644 // See if we need to initialize a list of variables from a function
1645 // call. This returns true if we have set up the variables and the
1649 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1650 Expression* expr, bool is_coloneq,
1651 source_location location)
1653 Call_expression* call = expr->call_expression();
1657 // This is a function call. We can't check here whether it returns
1658 // the right number of values, but it might. Declare the variables,
1659 // and then assign the results of the call to them.
1661 unsigned int index = 0;
1662 bool any_new = false;
1663 for (Typed_identifier_list::const_iterator pv = vars->begin();
1667 Expression* init = Expression::make_call_result(call, index);
1668 this->init_var(*pv, type, init, is_coloneq, false, &any_new);
1671 if (is_coloneq && !any_new)
1672 error_at(location, "variables redeclared but no variable is new");
1677 // See if we need to initialize a pair of values from a map index
1678 // expression. This returns true if we have set up the variables and
1679 // the initialization.
1682 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1683 Expression* expr, bool is_coloneq,
1684 source_location location)
1686 Index_expression* index = expr->index_expression();
1689 if (vars->size() != 2)
1692 // This is an index which is being assigned to two variables. It
1693 // must be a map index. Declare the variables, and then assign the
1694 // results of the map index.
1695 bool any_new = false;
1696 Typed_identifier_list::const_iterator p = vars->begin();
1697 Expression* init = type == NULL ? index : NULL;
1698 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1699 type == NULL, &any_new);
1700 if (type == NULL && any_new && val_no->is_variable())
1701 val_no->var_value()->set_type_from_init_tuple();
1702 Expression* val_var = Expression::make_var_reference(val_no, location);
1705 Type* var_type = type;
1706 if (var_type == NULL)
1707 var_type = Type::lookup_bool_type();
1708 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1710 Expression* present_var = Expression::make_var_reference(no, location);
1712 if (is_coloneq && !any_new)
1713 error_at(location, "variables redeclared but no variable is new");
1715 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1718 if (!this->gogo_->in_global_scope())
1719 this->gogo_->add_statement(s);
1720 else if (!val_no->is_sink())
1722 if (val_no->is_variable())
1723 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1725 else if (!no->is_sink())
1727 if (no->is_variable())
1728 no->var_value()->add_preinit_statement(this->gogo_, s);
1732 // Execute the map index expression just so that we can fail if
1734 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1736 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1742 // See if we need to initialize a pair of values from a receive
1743 // expression. This returns true if we have set up the variables and
1744 // the initialization.
1747 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1748 Expression* expr, bool is_coloneq,
1749 source_location location)
1751 Receive_expression* receive = expr->receive_expression();
1752 if (receive == NULL)
1754 if (vars->size() != 2)
1757 // This is a receive expression which is being assigned to two
1758 // variables. Declare the variables, and then assign the results of
1760 bool any_new = false;
1761 Typed_identifier_list::const_iterator p = vars->begin();
1762 Expression* init = type == NULL ? receive : NULL;
1763 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1764 type == NULL, &any_new);
1765 if (type == NULL && any_new && val_no->is_variable())
1766 val_no->var_value()->set_type_from_init_tuple();
1767 Expression* val_var = Expression::make_var_reference(val_no, location);
1770 Type* var_type = type;
1771 if (var_type == NULL)
1772 var_type = Type::lookup_bool_type();
1773 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1775 Expression* received_var = Expression::make_var_reference(no, location);
1777 if (is_coloneq && !any_new)
1778 error_at(location, "variables redeclared but no variable is new");
1780 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1786 if (!this->gogo_->in_global_scope())
1787 this->gogo_->add_statement(s);
1788 else if (!val_no->is_sink())
1790 if (val_no->is_variable())
1791 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1793 else if (!no->is_sink())
1795 if (no->is_variable())
1796 no->var_value()->add_preinit_statement(this->gogo_, s);
1800 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1802 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1808 // See if we need to initialize a pair of values from a type guard
1809 // expression. This returns true if we have set up the variables and
1810 // the initialization.
1813 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1814 Type* type, Expression* expr,
1815 bool is_coloneq, source_location location)
1817 Type_guard_expression* type_guard = expr->type_guard_expression();
1818 if (type_guard == NULL)
1820 if (vars->size() != 2)
1823 // This is a type guard expression which is being assigned to two
1824 // variables. Declare the variables, and then assign the results of
1826 bool any_new = false;
1827 Typed_identifier_list::const_iterator p = vars->begin();
1828 Type* var_type = type;
1829 if (var_type == NULL)
1830 var_type = type_guard->type();
1831 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1833 Expression* val_var = Expression::make_var_reference(val_no, location);
1837 if (var_type == NULL)
1838 var_type = Type::lookup_bool_type();
1839 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1841 Expression* ok_var = Expression::make_var_reference(no, location);
1843 Expression* texpr = type_guard->expr();
1844 Type* t = type_guard->type();
1845 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1849 if (is_coloneq && !any_new)
1850 error_at(location, "variables redeclared but no variable is new");
1852 if (!this->gogo_->in_global_scope())
1853 this->gogo_->add_statement(s);
1854 else if (!val_no->is_sink())
1856 if (val_no->is_variable())
1857 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1859 else if (!no->is_sink())
1861 if (no->is_variable())
1862 no->var_value()->add_preinit_statement(this->gogo_, s);
1866 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1867 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1873 // Create a single variable. If IS_COLONEQ is true, we permit
1874 // redeclarations in the same block, and we set *IS_NEW when we find a
1875 // new variable which is not a redeclaration.
1878 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1879 bool is_coloneq, bool type_from_init, bool* is_new)
1881 source_location location = tid.location();
1883 if (Gogo::is_sink_name(tid.name()))
1885 if (!type_from_init && init != NULL)
1887 if (!this->gogo_->in_global_scope())
1888 this->gogo_->add_statement(Statement::make_statement(init, true));
1890 return this->create_dummy_global(type, init, location);
1892 return this->gogo_->add_sink();
1897 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1899 && (no->is_variable() || no->is_result_variable()))
1901 // INIT may be NULL even when IS_COLONEQ is true for cases
1902 // like v, ok := x.(int).
1903 if (!type_from_init && init != NULL)
1905 Expression *v = Expression::make_var_reference(no, location);
1906 Statement *s = Statement::make_assignment(v, init, location);
1907 this->gogo_->add_statement(s);
1913 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1914 false, false, location);
1915 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1916 if (!no->is_variable())
1918 // The name is already defined, so we just gave an error.
1919 return this->gogo_->add_sink();
1924 // Create a dummy global variable to force an initializer to be run in
1925 // the right place. This is used when a sink variable is initialized
1929 Parse::create_dummy_global(Type* type, Expression* init,
1930 source_location location)
1932 if (type == NULL && init == NULL)
1933 type = Type::lookup_bool_type();
1934 Variable* var = new Variable(type, init, true, false, false, location);
1937 snprintf(buf, sizeof buf, "_.%d", count);
1939 return this->gogo_->add_variable(buf, var);
1942 // SimpleVarDecl = identifier ":=" Expression .
1944 // We've already seen the identifier.
1946 // FIXME: We also have to implement
1947 // IdentifierList ":=" ExpressionList
1948 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1949 // tuple assignments here as well.
1951 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
1954 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
1955 // guard (var := expr.("type") using the literal keyword "type").
1958 Parse::simple_var_decl_or_assignment(const std::string& name,
1959 source_location location,
1960 Range_clause* p_range_clause,
1961 Type_switch* p_type_switch)
1963 Typed_identifier_list til;
1964 til.push_back(Typed_identifier(name, NULL, location));
1966 // We've seen one identifier. If we see a comma now, this could be
1968 if (this->peek_token()->is_op(OPERATOR_COMMA))
1970 go_assert(p_type_switch == NULL);
1973 const Token* token = this->advance_token();
1974 if (!token->is_identifier())
1977 std::string id = token->identifier();
1978 bool is_id_exported = token->is_identifier_exported();
1979 source_location id_location = token->location();
1981 token = this->advance_token();
1982 if (!token->is_op(OPERATOR_COMMA))
1984 if (token->is_op(OPERATOR_COLONEQ))
1986 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1987 til.push_back(Typed_identifier(id, NULL, location));
1990 this->unget_token(Token::make_identifier_token(id,
1996 id = this->gogo_->pack_hidden_name(id, is_id_exported);
1997 til.push_back(Typed_identifier(id, NULL, location));
2000 // We have a comma separated list of identifiers in TIL. If the
2001 // next token is COLONEQ, then this is a simple var decl, and we
2002 // have the complete list of identifiers. If the next token is
2003 // not COLONEQ, then the only valid parse is a tuple assignment.
2004 // The list of identifiers we have so far is really a list of
2005 // expressions. There are more expressions following.
2007 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2009 Expression_list* exprs = new Expression_list;
2010 for (Typed_identifier_list::const_iterator p = til.begin();
2013 exprs->push_back(this->id_to_expression(p->name(),
2016 Expression_list* more_exprs = this->expression_list(NULL, true);
2017 for (Expression_list::const_iterator p = more_exprs->begin();
2018 p != more_exprs->end();
2020 exprs->push_back(*p);
2023 this->tuple_assignment(exprs, p_range_clause);
2028 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2029 const Token* token = this->advance_token();
2031 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2033 this->range_clause_decl(&til, p_range_clause);
2037 Expression_list* init;
2038 if (p_type_switch == NULL)
2039 init = this->expression_list(NULL, false);
2042 bool is_type_switch = false;
2043 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2047 p_type_switch->found = true;
2048 p_type_switch->name = name;
2049 p_type_switch->location = location;
2050 p_type_switch->expr = expr;
2054 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2056 init = new Expression_list();
2057 init->push_back(expr);
2061 this->advance_token();
2062 init = this->expression_list(expr, false);
2066 this->init_vars(&til, NULL, init, true, location);
2069 // FunctionDecl = "func" identifier Signature [ Block ] .
2070 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2073 // FunctionDecl = "func" identifier Signature
2074 // __asm__ "(" string_lit ")" .
2075 // This extension means a function whose real name is the identifier
2079 Parse::function_decl()
2081 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2082 source_location location = this->location();
2083 const Token* token = this->advance_token();
2085 Typed_identifier* rec = NULL;
2086 if (token->is_op(OPERATOR_LPAREN))
2088 rec = this->receiver();
2089 token = this->peek_token();
2092 if (!token->is_identifier())
2094 error_at(this->location(), "expected function name");
2099 this->gogo_->pack_hidden_name(token->identifier(),
2100 token->is_identifier_exported());
2102 this->advance_token();
2104 Function_type* fntype = this->signature(rec, this->location());
2108 Named_object* named_object = NULL;
2110 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2112 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2114 error_at(this->location(), "expected %<(%>");
2117 token = this->advance_token();
2118 if (!token->is_string())
2120 error_at(this->location(), "expected string");
2123 std::string asm_name = token->string_value();
2124 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2126 error_at(this->location(), "expected %<)%>");
2129 this->advance_token();
2130 if (!Gogo::is_sink_name(name))
2132 named_object = this->gogo_->declare_function(name, fntype, location);
2133 if (named_object->is_function_declaration())
2134 named_object->func_declaration_value()->set_asm_name(asm_name);
2138 // Check for the easy error of a newline before the opening brace.
2139 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2141 source_location semi_loc = this->location();
2142 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2143 error_at(this->location(),
2144 "unexpected semicolon or newline before %<{%>");
2146 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2150 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2152 if (named_object == NULL && !Gogo::is_sink_name(name))
2153 this->gogo_->declare_function(name, fntype, location);
2157 this->gogo_->start_function(name, fntype, true, location);
2158 source_location end_loc = this->block();
2159 this->gogo_->finish_function(end_loc);
2163 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2164 // BaseTypeName = identifier .
2169 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2172 const Token* token = this->advance_token();
2173 source_location location = token->location();
2174 if (!token->is_op(OPERATOR_MULT))
2176 if (!token->is_identifier())
2178 error_at(this->location(), "method has no receiver");
2179 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2180 token = this->advance_token();
2181 if (!token->is_eof())
2182 this->advance_token();
2185 name = token->identifier();
2186 bool is_exported = token->is_identifier_exported();
2187 token = this->advance_token();
2188 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2190 // An identifier followed by something other than a dot or a
2191 // right parenthesis must be a receiver name followed by a
2193 name = this->gogo_->pack_hidden_name(name, is_exported);
2197 // This must be a type name.
2198 this->unget_token(Token::make_identifier_token(name, is_exported,
2200 token = this->peek_token();
2205 // Here the receiver name is in NAME (it is empty if the receiver is
2206 // unnamed) and TOKEN is the first token in the type.
2208 bool is_pointer = false;
2209 if (token->is_op(OPERATOR_MULT))
2212 token = this->advance_token();
2215 if (!token->is_identifier())
2217 error_at(this->location(), "expected receiver name or type");
2218 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2219 while (!token->is_eof())
2221 token = this->advance_token();
2222 if (token->is_op(OPERATOR_LPAREN))
2224 else if (token->is_op(OPERATOR_RPAREN))
2231 if (!token->is_eof())
2232 this->advance_token();
2236 Type* type = this->type_name(true);
2238 if (is_pointer && !type->is_error_type())
2239 type = Type::make_pointer_type(type);
2241 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2242 this->advance_token();
2245 if (this->peek_token()->is_op(OPERATOR_COMMA))
2246 error_at(this->location(), "method has multiple receivers");
2248 error_at(this->location(), "expected %<)%>");
2249 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2250 token = this->advance_token();
2251 if (!token->is_eof())
2252 this->advance_token();
2256 return new Typed_identifier(name, type, location);
2259 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2260 // Literal = BasicLit | CompositeLit | FunctionLit .
2261 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2263 // If MAY_BE_SINK is true, this operand may be "_".
2266 Parse::operand(bool may_be_sink)
2268 const Token* token = this->peek_token();
2270 switch (token->classification())
2272 case Token::TOKEN_IDENTIFIER:
2274 source_location location = token->location();
2275 std::string id = token->identifier();
2276 bool is_exported = token->is_identifier_exported();
2277 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2279 Named_object* in_function;
2280 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2282 Package* package = NULL;
2283 if (named_object != NULL && named_object->is_package())
2285 if (!this->advance_token()->is_op(OPERATOR_DOT)
2286 || !this->advance_token()->is_identifier())
2288 error_at(location, "unexpected reference to package");
2289 return Expression::make_error(location);
2291 package = named_object->package_value();
2292 package->set_used();
2293 id = this->peek_token()->identifier();
2294 is_exported = this->peek_token()->is_identifier_exported();
2295 packed = this->gogo_->pack_hidden_name(id, is_exported);
2296 named_object = package->lookup(packed);
2297 location = this->location();
2298 go_assert(in_function == NULL);
2301 this->advance_token();
2303 if (named_object != NULL
2304 && named_object->is_type()
2305 && !named_object->type_value()->is_visible())
2307 go_assert(package != NULL);
2308 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2309 Gogo::message_name(package->name()).c_str(),
2310 Gogo::message_name(id).c_str());
2311 return Expression::make_error(location);
2315 if (named_object == NULL)
2317 if (package != NULL)
2319 std::string n1 = Gogo::message_name(package->name());
2320 std::string n2 = Gogo::message_name(id);
2323 ("invalid reference to unexported identifier "
2325 n1.c_str(), n2.c_str());
2328 "reference to undefined identifier %<%s.%s%>",
2329 n1.c_str(), n2.c_str());
2330 return Expression::make_error(location);
2333 named_object = this->gogo_->add_unknown_name(packed, location);
2336 if (in_function != NULL
2337 && in_function != this->gogo_->current_function()
2338 && (named_object->is_variable()
2339 || named_object->is_result_variable()))
2340 return this->enclosing_var_reference(in_function, named_object,
2343 switch (named_object->classification())
2345 case Named_object::NAMED_OBJECT_CONST:
2346 return Expression::make_const_reference(named_object, location);
2347 case Named_object::NAMED_OBJECT_TYPE:
2348 return Expression::make_type(named_object->type_value(), location);
2349 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2351 Type* t = Type::make_forward_declaration(named_object);
2352 return Expression::make_type(t, location);
2354 case Named_object::NAMED_OBJECT_VAR:
2355 case Named_object::NAMED_OBJECT_RESULT_VAR:
2356 return Expression::make_var_reference(named_object, location);
2357 case Named_object::NAMED_OBJECT_SINK:
2359 return Expression::make_sink(location);
2362 error_at(location, "cannot use _ as value");
2363 return Expression::make_error(location);
2365 case Named_object::NAMED_OBJECT_FUNC:
2366 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2367 return Expression::make_func_reference(named_object, NULL,
2369 case Named_object::NAMED_OBJECT_UNKNOWN:
2370 return Expression::make_unknown_reference(named_object, location);
2377 case Token::TOKEN_STRING:
2378 ret = Expression::make_string(token->string_value(), token->location());
2379 this->advance_token();
2382 case Token::TOKEN_INTEGER:
2383 ret = Expression::make_integer(token->integer_value(), NULL,
2385 this->advance_token();
2388 case Token::TOKEN_FLOAT:
2389 ret = Expression::make_float(token->float_value(), NULL,
2391 this->advance_token();
2394 case Token::TOKEN_IMAGINARY:
2397 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2398 ret = Expression::make_complex(&zero, token->imaginary_value(),
2399 NULL, token->location());
2401 this->advance_token();
2405 case Token::TOKEN_KEYWORD:
2406 switch (token->keyword())
2409 return this->function_lit();
2411 case KEYWORD_INTERFACE:
2413 case KEYWORD_STRUCT:
2415 source_location location = token->location();
2416 return Expression::make_type(this->type(), location);
2423 case Token::TOKEN_OPERATOR:
2424 if (token->is_op(OPERATOR_LPAREN))
2426 this->advance_token();
2427 ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2428 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2429 error_at(this->location(), "missing %<)%>");
2431 this->advance_token();
2434 else if (token->is_op(OPERATOR_LSQUARE))
2436 // Here we call array_type directly, as this is the only
2437 // case where an ellipsis is permitted for an array type.
2438 source_location location = token->location();
2439 return Expression::make_type(this->array_type(true), location);
2447 error_at(this->location(), "expected operand");
2448 return Expression::make_error(this->location());
2451 // Handle a reference to a variable in an enclosing function. We add
2452 // it to a list of such variables. We return a reference to a field
2453 // in a struct which will be passed on the static chain when calling
2454 // the current function.
2457 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2458 source_location location)
2460 go_assert(var->is_variable() || var->is_result_variable());
2462 Named_object* this_function = this->gogo_->current_function();
2463 Named_object* closure = this_function->func_value()->closure_var();
2465 Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2466 std::pair<Enclosing_vars::iterator, bool> ins =
2467 this->enclosing_vars_.insert(ev);
2470 // This is a variable we have not seen before. Add a new field
2471 // to the closure type.
2472 this_function->func_value()->add_closure_field(var, location);
2475 Expression* closure_ref = Expression::make_var_reference(closure,
2477 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2479 // The closure structure holds pointers to the variables, so we need
2480 // to introduce an indirection.
2481 Expression* e = Expression::make_field_reference(closure_ref,
2484 e = Expression::make_unary(OPERATOR_MULT, e, location);
2488 // CompositeLit = LiteralType LiteralValue .
2489 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2490 // SliceType | MapType | TypeName .
2491 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2492 // ElementList = Element { "," Element } .
2493 // Element = [ Key ":" ] Value .
2494 // Key = FieldName | ElementIndex .
2495 // FieldName = identifier .
2496 // ElementIndex = Expression .
2497 // Value = Expression | LiteralValue .
2499 // We have already seen the type if there is one, and we are now
2500 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2501 // will be seen here as an array type whose length is "nil". The
2502 // DEPTH parameter is non-zero if this is an embedded composite
2503 // literal and the type was omitted. It gives the number of steps up
2504 // to the type which was provided. E.g., in [][]int{{1}} it will be
2505 // 1. In [][][]int{{{1}}} it will be 2.
2508 Parse::composite_lit(Type* type, int depth, source_location location)
2510 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2511 this->advance_token();
2513 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2515 this->advance_token();
2516 return Expression::make_composite_literal(type, depth, false, NULL,
2520 bool has_keys = false;
2521 Expression_list* vals = new Expression_list;
2525 bool is_type_omitted = false;
2527 const Token* token = this->peek_token();
2529 if (token->is_identifier())
2531 std::string identifier = token->identifier();
2532 bool is_exported = token->is_identifier_exported();
2533 source_location location = token->location();
2535 if (this->advance_token()->is_op(OPERATOR_COLON))
2537 // This may be a field name. We don't know for sure--it
2538 // could also be an expression for an array index. We
2539 // don't want to parse it as an expression because may
2540 // trigger various errors, e.g., if this identifier
2541 // happens to be the name of a package.
2542 Gogo* gogo = this->gogo_;
2543 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2549 this->unget_token(Token::make_identifier_token(identifier,
2552 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2555 else if (!token->is_op(OPERATOR_LCURLY))
2556 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2559 // This must be a composite literal inside another composite
2560 // literal, with the type omitted for the inner one.
2561 val = this->composite_lit(type, depth + 1, token->location());
2562 is_type_omitted = true;
2565 token = this->peek_token();
2566 if (!token->is_op(OPERATOR_COLON))
2569 vals->push_back(NULL);
2573 if (is_type_omitted && !val->is_error_expression())
2575 error_at(this->location(), "unexpected %<:%>");
2576 val = Expression::make_error(this->location());
2579 this->advance_token();
2581 if (!has_keys && !vals->empty())
2583 Expression_list* newvals = new Expression_list;
2584 for (Expression_list::const_iterator p = vals->begin();
2588 newvals->push_back(NULL);
2589 newvals->push_back(*p);
2596 if (val->unknown_expression() != NULL)
2597 val->unknown_expression()->set_is_composite_literal_key();
2599 vals->push_back(val);
2601 if (!token->is_op(OPERATOR_LCURLY))
2602 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2605 // This must be a composite literal inside another
2606 // composite literal, with the type omitted for the
2608 val = this->composite_lit(type, depth + 1, token->location());
2611 token = this->peek_token();
2614 vals->push_back(val);
2616 if (token->is_op(OPERATOR_COMMA))
2618 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2620 this->advance_token();
2624 else if (token->is_op(OPERATOR_RCURLY))
2626 this->advance_token();
2631 error_at(this->location(), "expected %<,%> or %<}%>");
2634 while (!token->is_eof()
2635 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2637 if (token->is_op(OPERATOR_LCURLY))
2639 else if (token->is_op(OPERATOR_RCURLY))
2641 token = this->advance_token();
2643 if (token->is_op(OPERATOR_RCURLY))
2644 this->advance_token();
2646 return Expression::make_error(location);
2650 return Expression::make_composite_literal(type, depth, has_keys, vals,
2654 // FunctionLit = "func" Signature Block .
2657 Parse::function_lit()
2659 source_location location = this->location();
2660 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2661 this->advance_token();
2663 Enclosing_vars hold_enclosing_vars;
2664 hold_enclosing_vars.swap(this->enclosing_vars_);
2666 Function_type* type = this->signature(NULL, location);
2668 type = Type::make_function_type(NULL, NULL, NULL, location);
2670 // For a function literal, the next token must be a '{'. If we
2671 // don't see that, then we may have a type expression.
2672 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2673 return Expression::make_type(type, location);
2675 Bc_stack* hold_break_stack = this->break_stack_;
2676 Bc_stack* hold_continue_stack = this->continue_stack_;
2677 this->break_stack_ = NULL;
2678 this->continue_stack_ = NULL;
2680 Named_object* no = this->gogo_->start_function("", type, true, location);
2682 source_location end_loc = this->block();
2684 this->gogo_->finish_function(end_loc);
2686 if (this->break_stack_ != NULL)
2687 delete this->break_stack_;
2688 if (this->continue_stack_ != NULL)
2689 delete this->continue_stack_;
2690 this->break_stack_ = hold_break_stack;
2691 this->continue_stack_ = hold_continue_stack;
2693 hold_enclosing_vars.swap(this->enclosing_vars_);
2695 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2698 return Expression::make_func_reference(no, closure, location);
2701 // Create a closure for the nested function FUNCTION. This is based
2702 // on ENCLOSING_VARS, which is a list of all variables defined in
2703 // enclosing functions and referenced from FUNCTION. A closure is the
2704 // address of a struct which contains the addresses of all the
2705 // referenced variables. This returns NULL if no closure is required.
2708 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2709 source_location location)
2711 if (enclosing_vars->empty())
2714 // Get the variables in order by their field index.
2716 size_t enclosing_var_count = enclosing_vars->size();
2717 std::vector<Enclosing_var> ev(enclosing_var_count);
2718 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2719 p != enclosing_vars->end();
2721 ev[p->index()] = *p;
2723 // Build an initializer for a composite literal of the closure's
2726 Named_object* enclosing_function = this->gogo_->current_function();
2727 Expression_list* initializer = new Expression_list;
2728 for (size_t i = 0; i < enclosing_var_count; ++i)
2730 go_assert(ev[i].index() == i);
2731 Named_object* var = ev[i].var();
2733 if (ev[i].in_function() == enclosing_function)
2734 ref = Expression::make_var_reference(var, location);
2736 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2738 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2740 initializer->push_back(refaddr);
2743 Named_object* closure_var = function->func_value()->closure_var();
2744 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2745 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2747 return Expression::make_heap_composite(cv, location);
2750 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2752 // If MAY_BE_SINK is true, this expression may be "_".
2754 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2757 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2758 // guard (var := expr.("type") using the literal keyword "type").
2761 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2762 bool* is_type_switch)
2764 source_location start_loc = this->location();
2765 bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2767 Expression* ret = this->operand(may_be_sink);
2769 // An unknown name followed by a curly brace must be a composite
2770 // literal, and the unknown name must be a type.
2771 if (may_be_composite_lit
2772 && !is_parenthesized
2773 && ret->unknown_expression() != NULL
2774 && this->peek_token()->is_op(OPERATOR_LCURLY))
2776 Named_object* no = ret->unknown_expression()->named_object();
2777 Type* type = Type::make_forward_declaration(no);
2778 ret = Expression::make_type(type, ret->location());
2781 // We handle composite literals and type casts here, as it is the
2782 // easiest way to handle types which are in parentheses, as in
2784 if (ret->is_type_expression())
2786 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2788 if (is_parenthesized)
2790 "cannot parenthesize type in composite literal");
2791 ret = this->composite_lit(ret->type(), 0, ret->location());
2793 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2795 source_location loc = this->location();
2796 this->advance_token();
2797 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2799 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2801 error_at(this->location(),
2802 "invalid use of %<...%> in type conversion");
2803 this->advance_token();
2805 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2806 error_at(this->location(), "expected %<)%>");
2808 this->advance_token();
2809 if (expr->is_error_expression())
2813 Type* t = ret->type();
2814 if (t->classification() == Type::TYPE_ARRAY
2815 && t->array_type()->length() != NULL
2816 && t->array_type()->length()->is_nil_expression())
2818 error_at(ret->location(),
2819 "invalid use of %<...%> in type conversion");
2820 ret = Expression::make_error(loc);
2823 ret = Expression::make_cast(t, expr, loc);
2830 const Token* token = this->peek_token();
2831 if (token->is_op(OPERATOR_LPAREN))
2832 ret = this->call(this->verify_not_sink(ret));
2833 else if (token->is_op(OPERATOR_DOT))
2835 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2836 if (is_type_switch != NULL && *is_type_switch)
2839 else if (token->is_op(OPERATOR_LSQUARE))
2840 ret = this->index(this->verify_not_sink(ret));
2848 // Selector = "." identifier .
2849 // TypeGuard = "." "(" QualifiedIdent ")" .
2851 // Note that Operand can expand to QualifiedIdent, which contains a
2852 // ".". That is handled directly in operand when it sees a package
2855 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2856 // guard (var := expr.("type") using the literal keyword "type").
2859 Parse::selector(Expression* left, bool* is_type_switch)
2861 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
2862 source_location location = this->location();
2864 const Token* token = this->advance_token();
2865 if (token->is_identifier())
2867 // This could be a field in a struct, or a method in an
2868 // interface, or a method associated with a type. We can't know
2869 // which until we have seen all the types.
2871 this->gogo_->pack_hidden_name(token->identifier(),
2872 token->is_identifier_exported());
2873 if (token->identifier() == "_")
2875 error_at(this->location(), "invalid use of %<_%>");
2876 name = this->gogo_->pack_hidden_name("blank", false);
2878 this->advance_token();
2879 return Expression::make_selector(left, name, location);
2881 else if (token->is_op(OPERATOR_LPAREN))
2883 this->advance_token();
2885 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
2886 type = this->type();
2889 if (is_type_switch != NULL)
2890 *is_type_switch = true;
2893 error_at(this->location(),
2894 "use of %<.(type)%> outside type switch");
2895 type = Type::make_error_type();
2897 this->advance_token();
2899 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2900 error_at(this->location(), "missing %<)%>");
2902 this->advance_token();
2903 if (is_type_switch != NULL && *is_type_switch)
2905 return Expression::make_type_guard(left, type, location);
2909 error_at(this->location(), "expected identifier or %<(%>");
2914 // Index = "[" Expression "]" .
2915 // Slice = "[" Expression ":" [ Expression ] "]" .
2918 Parse::index(Expression* expr)
2920 source_location location = this->location();
2921 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
2922 this->advance_token();
2925 if (!this->peek_token()->is_op(OPERATOR_COLON))
2926 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2930 mpz_init_set_ui(zero, 0);
2931 start = Expression::make_integer(&zero, NULL, location);
2935 Expression* end = NULL;
2936 if (this->peek_token()->is_op(OPERATOR_COLON))
2938 // We use nil to indicate a missing high expression.
2939 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
2940 end = Expression::make_nil(this->location());
2942 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2944 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
2945 error_at(this->location(), "missing %<]%>");
2947 this->advance_token();
2948 return Expression::make_index(expr, start, end, location);
2951 // Call = "(" [ ArgumentList [ "," ] ] ")" .
2952 // ArgumentList = ExpressionList [ "..." ] .
2955 Parse::call(Expression* func)
2957 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2958 Expression_list* args = NULL;
2959 bool is_varargs = false;
2960 const Token* token = this->advance_token();
2961 if (!token->is_op(OPERATOR_RPAREN))
2963 args = this->expression_list(NULL, false);
2964 token = this->peek_token();
2965 if (token->is_op(OPERATOR_ELLIPSIS))
2968 token = this->advance_token();
2971 if (token->is_op(OPERATOR_COMMA))
2972 token = this->advance_token();
2973 if (!token->is_op(OPERATOR_RPAREN))
2974 error_at(this->location(), "missing %<)%>");
2976 this->advance_token();
2977 if (func->is_error_expression())
2979 return Expression::make_call(func, args, is_varargs, func->location());
2982 // Return an expression for a single unqualified identifier.
2985 Parse::id_to_expression(const std::string& name, source_location location)
2987 Named_object* in_function;
2988 Named_object* named_object = this->gogo_->lookup(name, &in_function);
2989 if (named_object == NULL)
2990 named_object = this->gogo_->add_unknown_name(name, location);
2992 if (in_function != NULL
2993 && in_function != this->gogo_->current_function()
2994 && (named_object->is_variable() || named_object->is_result_variable()))
2995 return this->enclosing_var_reference(in_function, named_object,
2998 switch (named_object->classification())
3000 case Named_object::NAMED_OBJECT_CONST:
3001 return Expression::make_const_reference(named_object, location);
3002 case Named_object::NAMED_OBJECT_VAR:
3003 case Named_object::NAMED_OBJECT_RESULT_VAR:
3004 return Expression::make_var_reference(named_object, location);
3005 case Named_object::NAMED_OBJECT_SINK:
3006 return Expression::make_sink(location);
3007 case Named_object::NAMED_OBJECT_FUNC:
3008 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3009 return Expression::make_func_reference(named_object, NULL, location);
3010 case Named_object::NAMED_OBJECT_UNKNOWN:
3011 return Expression::make_unknown_reference(named_object, location);
3012 case Named_object::NAMED_OBJECT_PACKAGE:
3013 case Named_object::NAMED_OBJECT_TYPE:
3014 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3015 // These cases can arise for a field name in a composite
3017 return Expression::make_unknown_reference(named_object, location);
3019 error_at(this->location(), "unexpected type of identifier");
3020 return Expression::make_error(location);
3024 // Expression = UnaryExpr { binary_op Expression } .
3026 // PRECEDENCE is the precedence of the current operator.
3028 // If MAY_BE_SINK is true, this expression may be "_".
3030 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3033 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3034 // guard (var := expr.("type") using the literal keyword "type").
3037 Parse::expression(Precedence precedence, bool may_be_sink,
3038 bool may_be_composite_lit, bool* is_type_switch)
3040 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3045 if (is_type_switch != NULL && *is_type_switch)
3048 const Token* token = this->peek_token();
3049 if (token->classification() != Token::TOKEN_OPERATOR)
3055 Precedence right_precedence;
3056 switch (token->op())
3059 right_precedence = PRECEDENCE_OROR;
3061 case OPERATOR_ANDAND:
3062 right_precedence = PRECEDENCE_ANDAND;
3065 case OPERATOR_NOTEQ:
3070 right_precedence = PRECEDENCE_RELOP;
3073 case OPERATOR_MINUS:
3076 right_precedence = PRECEDENCE_ADDOP;
3081 case OPERATOR_LSHIFT:
3082 case OPERATOR_RSHIFT:
3084 case OPERATOR_BITCLEAR:
3085 right_precedence = PRECEDENCE_MULOP;
3088 right_precedence = PRECEDENCE_INVALID;
3092 if (right_precedence == PRECEDENCE_INVALID)
3098 Operator op = token->op();
3099 source_location binop_location = token->location();
3101 if (precedence >= right_precedence)
3103 // We've already seen A * B, and we see + C. We want to
3104 // return so that A * B becomes a group.
3108 this->advance_token();
3110 left = this->verify_not_sink(left);
3111 Expression* right = this->expression(right_precedence, false,
3112 may_be_composite_lit,
3114 left = Expression::make_binary(op, left, right, binop_location);
3119 Parse::expression_may_start_here()
3121 const Token* token = this->peek_token();
3122 switch (token->classification())
3124 case Token::TOKEN_INVALID:
3125 case Token::TOKEN_EOF:
3127 case Token::TOKEN_KEYWORD:
3128 switch (token->keyword())
3133 case KEYWORD_STRUCT:
3134 case KEYWORD_INTERFACE:
3139 case Token::TOKEN_IDENTIFIER:
3141 case Token::TOKEN_STRING:
3143 case Token::TOKEN_OPERATOR:
3144 switch (token->op())
3147 case OPERATOR_MINUS:
3151 case OPERATOR_CHANOP:
3153 case OPERATOR_LPAREN:
3154 case OPERATOR_LSQUARE:
3159 case Token::TOKEN_INTEGER:
3160 case Token::TOKEN_FLOAT:
3161 case Token::TOKEN_IMAGINARY:
3168 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3170 // If MAY_BE_SINK is true, this expression may be "_".
3172 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3175 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3176 // guard (var := expr.("type") using the literal keyword "type").
3179 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3180 bool* is_type_switch)
3182 const Token* token = this->peek_token();
3183 if (token->is_op(OPERATOR_PLUS)
3184 || token->is_op(OPERATOR_MINUS)
3185 || token->is_op(OPERATOR_NOT)
3186 || token->is_op(OPERATOR_XOR)
3187 || token->is_op(OPERATOR_CHANOP)
3188 || token->is_op(OPERATOR_MULT)
3189 || token->is_op(OPERATOR_AND))
3191 source_location location = token->location();
3192 Operator op = token->op();
3193 this->advance_token();
3195 if (op == OPERATOR_CHANOP
3196 && this->peek_token()->is_keyword(KEYWORD_CHAN))
3198 // This is "<- chan" which must be the start of a type.
3199 this->unget_token(Token::make_operator_token(op, location));
3200 return Expression::make_type(this->type(), location);
3203 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
3204 if (expr->is_error_expression())
3206 else if (op == OPERATOR_MULT && expr->is_type_expression())
3207 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3209 else if (op == OPERATOR_AND && expr->is_composite_literal())
3210 expr = Expression::make_heap_composite(expr, location);
3211 else if (op != OPERATOR_CHANOP)
3212 expr = Expression::make_unary(op, expr, location);
3214 expr = Expression::make_receive(expr, location);
3218 return this->primary_expr(may_be_sink, may_be_composite_lit,
3223 // Declaration | LabeledStmt | SimpleStmt |
3224 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3225 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3228 // LABEL is the label of this statement if it has one.
3231 Parse::statement(Label* label)
3233 const Token* token = this->peek_token();
3234 switch (token->classification())
3236 case Token::TOKEN_KEYWORD:
3238 switch (token->keyword())
3243 this->declaration();
3247 case KEYWORD_STRUCT:
3248 case KEYWORD_INTERFACE:
3249 this->simple_stat(true, NULL, NULL, NULL);
3253 this->go_or_defer_stat();
3255 case KEYWORD_RETURN:
3256 this->return_stat();
3261 case KEYWORD_CONTINUE:
3262 this->continue_stat();
3270 case KEYWORD_SWITCH:
3271 this->switch_stat(label);
3273 case KEYWORD_SELECT:
3274 this->select_stat(label);
3277 this->for_stat(label);
3280 error_at(this->location(), "expected statement");
3281 this->advance_token();
3287 case Token::TOKEN_IDENTIFIER:
3289 std::string identifier = token->identifier();
3290 bool is_exported = token->is_identifier_exported();
3291 source_location location = token->location();
3292 if (this->advance_token()->is_op(OPERATOR_COLON))
3294 this->advance_token();
3295 this->labeled_stmt(identifier, location);
3299 this->unget_token(Token::make_identifier_token(identifier,
3302 this->simple_stat(true, NULL, NULL, NULL);
3307 case Token::TOKEN_OPERATOR:
3308 if (token->is_op(OPERATOR_LCURLY))
3310 source_location location = token->location();
3311 this->gogo_->start_block(location);
3312 source_location end_loc = this->block();
3313 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3316 else if (!token->is_op(OPERATOR_SEMICOLON))
3317 this->simple_stat(true, NULL, NULL, NULL);
3320 case Token::TOKEN_STRING:
3321 case Token::TOKEN_INTEGER:
3322 case Token::TOKEN_FLOAT:
3323 case Token::TOKEN_IMAGINARY:
3324 this->simple_stat(true, NULL, NULL, NULL);
3328 error_at(this->location(), "expected statement");
3329 this->advance_token();
3335 Parse::statement_may_start_here()
3337 const Token* token = this->peek_token();
3338 switch (token->classification())
3340 case Token::TOKEN_KEYWORD:
3342 switch (token->keyword())
3349 case KEYWORD_STRUCT:
3350 case KEYWORD_INTERFACE:
3353 case KEYWORD_RETURN:
3355 case KEYWORD_CONTINUE:
3358 case KEYWORD_SWITCH:
3359 case KEYWORD_SELECT:
3369 case Token::TOKEN_IDENTIFIER:
3372 case Token::TOKEN_OPERATOR:
3373 if (token->is_op(OPERATOR_LCURLY)
3374 || token->is_op(OPERATOR_SEMICOLON))
3377 return this->expression_may_start_here();
3379 case Token::TOKEN_STRING:
3380 case Token::TOKEN_INTEGER:
3381 case Token::TOKEN_FLOAT:
3382 case Token::TOKEN_IMAGINARY:
3390 // LabeledStmt = Label ":" Statement .
3391 // Label = identifier .
3394 Parse::labeled_stmt(const std::string& label_name, source_location location)
3396 Label* label = this->gogo_->add_label_definition(label_name, location);
3398 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3400 // This is a label at the end of a block. A program is
3401 // permitted to omit a semicolon here.
3405 if (!this->statement_may_start_here())
3407 // Mark the label as used to avoid a useless error about an
3409 label->set_is_used();
3411 error_at(location, "missing statement after label");
3412 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3417 this->statement(label);