1 // parse.cc -- Go frontend parser.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 #include "statements.h"
13 #include "expressions.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
22 const Enclosing_var& v2)
24 if (v1.var() == v2.var())
27 const std::string& n1(v1.var()->name());
28 const std::string& n2(v2.var()->name());
29 int i = n1.compare(n2);
35 // If we get here it means that a single nested function refers to
36 // two different variables defined in enclosing functions, and both
37 // variables have the same name. I think this is impossible.
43 Parse::Parse(Lex* lex, Gogo* gogo)
45 token_(Token::make_invalid_token(Linemap::unknown_location())),
46 unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
47 unget_token_valid_(false),
48 is_erroneous_function_(false),
51 continue_stack_(NULL),
58 // Return the current token.
63 if (this->unget_token_valid_)
64 return &this->unget_token_;
65 if (this->token_.is_invalid())
66 this->token_ = this->lex_->next_token();
70 // Advance to the next token and return it.
73 Parse::advance_token()
75 if (this->unget_token_valid_)
77 this->unget_token_valid_ = false;
78 if (!this->token_.is_invalid())
81 this->token_ = this->lex_->next_token();
85 // Push a token back on the input stream.
88 Parse::unget_token(const Token& token)
90 go_assert(!this->unget_token_valid_);
91 this->unget_token_ = token;
92 this->unget_token_valid_ = true;
95 // The location of the current token.
100 return this->peek_token()->location();
103 // IdentifierList = identifier { "," identifier } .
106 Parse::identifier_list(Typed_identifier_list* til)
108 const Token* token = this->peek_token();
111 if (!token->is_identifier())
113 error_at(this->location(), "expected identifier");
117 this->gogo_->pack_hidden_name(token->identifier(),
118 token->is_identifier_exported());
119 til->push_back(Typed_identifier(name, NULL, token->location()));
120 token = this->advance_token();
121 if (!token->is_op(OPERATOR_COMMA))
123 token = this->advance_token();
127 // ExpressionList = Expression { "," Expression } .
129 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
132 // If MAY_BE_SINK is true, the expressions in the list may be "_".
135 Parse::expression_list(Expression* first, bool may_be_sink,
136 bool may_be_composite_lit)
138 Expression_list* ret = new Expression_list();
140 ret->push_back(first);
143 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink,
144 may_be_composite_lit, NULL));
146 const Token* token = this->peek_token();
147 if (!token->is_op(OPERATOR_COMMA))
150 // Most expression lists permit a trailing comma.
151 Location location = token->location();
152 this->advance_token();
153 if (!this->expression_may_start_here())
155 this->unget_token(Token::make_operator_token(OPERATOR_COMMA,
162 // QualifiedIdent = [ PackageName "." ] identifier .
163 // PackageName = identifier .
165 // This sets *PNAME to the identifier and sets *PPACKAGE to the
166 // package or NULL if there isn't one. This returns true on success,
167 // false on failure in which case it will have emitted an error
171 Parse::qualified_ident(std::string* pname, Named_object** ppackage)
173 const Token* token = this->peek_token();
174 if (!token->is_identifier())
176 error_at(this->location(), "expected identifier");
180 std::string name = token->identifier();
181 bool is_exported = token->is_identifier_exported();
182 name = this->gogo_->pack_hidden_name(name, is_exported);
184 token = this->advance_token();
185 if (!token->is_op(OPERATOR_DOT))
192 Named_object* package = this->gogo_->lookup(name, NULL);
193 if (package == NULL || !package->is_package())
195 error_at(this->location(), "expected package");
196 // We expect . IDENTIFIER; skip both.
197 if (this->advance_token()->is_identifier())
198 this->advance_token();
202 package->package_value()->set_used();
204 token = this->advance_token();
205 if (!token->is_identifier())
207 error_at(this->location(), "expected identifier");
211 name = token->identifier();
215 error_at(this->location(), "invalid use of %<_%>");
219 if (package->name() == this->gogo_->package_name())
220 name = this->gogo_->pack_hidden_name(name,
221 token->is_identifier_exported());
226 this->advance_token();
231 // Type = TypeName | TypeLit | "(" Type ")" .
233 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
234 // SliceType | MapType | ChannelType .
239 const Token* token = this->peek_token();
240 if (token->is_identifier())
241 return this->type_name(true);
242 else if (token->is_op(OPERATOR_LSQUARE))
243 return this->array_type(false);
244 else if (token->is_keyword(KEYWORD_CHAN)
245 || token->is_op(OPERATOR_CHANOP))
246 return this->channel_type();
247 else if (token->is_keyword(KEYWORD_INTERFACE))
248 return this->interface_type();
249 else if (token->is_keyword(KEYWORD_FUNC))
251 Location location = token->location();
252 this->advance_token();
253 Type* type = this->signature(NULL, location);
255 return Type::make_error_type();
258 else if (token->is_keyword(KEYWORD_MAP))
259 return this->map_type();
260 else if (token->is_keyword(KEYWORD_STRUCT))
261 return this->struct_type();
262 else if (token->is_op(OPERATOR_MULT))
263 return this->pointer_type();
264 else if (token->is_op(OPERATOR_LPAREN))
266 this->advance_token();
267 Type* ret = this->type();
268 if (this->peek_token()->is_op(OPERATOR_RPAREN))
269 this->advance_token();
272 if (!ret->is_error_type())
273 error_at(this->location(), "expected %<)%>");
279 error_at(token->location(), "expected type");
280 return Type::make_error_type();
285 Parse::type_may_start_here()
287 const Token* token = this->peek_token();
288 return (token->is_identifier()
289 || token->is_op(OPERATOR_LSQUARE)
290 || token->is_op(OPERATOR_CHANOP)
291 || token->is_keyword(KEYWORD_CHAN)
292 || token->is_keyword(KEYWORD_INTERFACE)
293 || token->is_keyword(KEYWORD_FUNC)
294 || token->is_keyword(KEYWORD_MAP)
295 || token->is_keyword(KEYWORD_STRUCT)
296 || token->is_op(OPERATOR_MULT)
297 || token->is_op(OPERATOR_LPAREN));
300 // TypeName = QualifiedIdent .
302 // If MAY_BE_NIL is true, then an identifier with the value of the
303 // predefined constant nil is accepted, returning the nil type.
306 Parse::type_name(bool issue_error)
308 Location location = this->location();
311 Named_object* package;
312 if (!this->qualified_ident(&name, &package))
313 return Type::make_error_type();
315 Named_object* named_object;
317 named_object = this->gogo_->lookup(name, NULL);
320 named_object = package->package_value()->lookup(name);
321 if (named_object == NULL
323 && package->name() != this->gogo_->package_name())
325 // Check whether the name is there but hidden.
326 std::string s = ('.' + package->package_value()->pkgpath()
328 named_object = package->package_value()->lookup(s);
329 if (named_object != NULL)
331 Package* p = package->package_value();
332 const std::string& packname(p->package_name());
333 error_at(location, "invalid reference to hidden type %<%s.%s%>",
334 Gogo::message_name(packname).c_str(),
335 Gogo::message_name(name).c_str());
342 if (named_object == NULL)
345 named_object = this->gogo_->add_unknown_name(name, location);
348 const std::string& packname(package->package_value()->package_name());
349 error_at(location, "reference to undefined identifier %<%s.%s%>",
350 Gogo::message_name(packname).c_str(),
351 Gogo::message_name(name).c_str());
356 else if (named_object->is_type())
358 if (!named_object->type_value()->is_visible())
361 else if (named_object->is_unknown() || named_object->is_type_declaration())
369 error_at(location, "expected type");
370 return Type::make_error_type();
373 if (named_object->is_type())
374 return named_object->type_value();
375 else if (named_object->is_unknown() || named_object->is_type_declaration())
376 return Type::make_forward_declaration(named_object);
381 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
382 // ArrayLength = Expression .
383 // ElementType = CompleteType .
386 Parse::array_type(bool may_use_ellipsis)
388 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
389 const Token* token = this->advance_token();
391 Expression* length = NULL;
392 if (token->is_op(OPERATOR_RSQUARE))
393 this->advance_token();
396 if (!token->is_op(OPERATOR_ELLIPSIS))
397 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
398 else if (may_use_ellipsis)
400 // An ellipsis is used in composite literals to represent a
401 // fixed array of the size of the number of elements. We
402 // use a length of nil to represent this, and change the
403 // length when parsing the composite literal.
404 length = Expression::make_nil(this->location());
405 this->advance_token();
409 error_at(this->location(),
410 "use of %<[...]%> outside of array literal");
411 length = Expression::make_error(this->location());
412 this->advance_token();
414 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
416 error_at(this->location(), "expected %<]%>");
417 return Type::make_error_type();
419 this->advance_token();
422 Type* element_type = this->type();
424 return Type::make_array_type(element_type, length);
427 // MapType = "map" "[" KeyType "]" ValueType .
428 // KeyType = CompleteType .
429 // ValueType = CompleteType .
434 Location location = this->location();
435 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
436 if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
438 error_at(this->location(), "expected %<[%>");
439 return Type::make_error_type();
441 this->advance_token();
443 Type* key_type = this->type();
445 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
447 error_at(this->location(), "expected %<]%>");
448 return Type::make_error_type();
450 this->advance_token();
452 Type* value_type = this->type();
454 if (key_type->is_error_type() || value_type->is_error_type())
455 return Type::make_error_type();
457 return Type::make_map_type(key_type, value_type, location);
460 // StructType = "struct" "{" { FieldDecl ";" } "}" .
465 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
466 Location location = this->location();
467 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
469 Location token_loc = this->location();
470 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
471 && this->advance_token()->is_op(OPERATOR_LCURLY))
472 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
475 error_at(this->location(), "expected %<{%>");
476 return Type::make_error_type();
479 this->advance_token();
481 Struct_field_list* sfl = new Struct_field_list;
482 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
484 this->field_decl(sfl);
485 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
486 this->advance_token();
487 else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
489 error_at(this->location(), "expected %<;%> or %<}%> or newline");
490 if (!this->skip_past_error(OPERATOR_RCURLY))
491 return Type::make_error_type();
494 this->advance_token();
496 for (Struct_field_list::const_iterator pi = sfl->begin();
500 if (pi->type()->is_error_type())
502 for (Struct_field_list::const_iterator pj = pi + 1;
506 if (pi->field_name() == pj->field_name()
507 && !Gogo::is_sink_name(pi->field_name()))
508 error_at(pi->location(), "duplicate field name %<%s%>",
509 Gogo::message_name(pi->field_name()).c_str());
513 return Type::make_struct_type(sfl, location);
516 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
517 // Tag = string_lit .
520 Parse::field_decl(Struct_field_list* sfl)
522 const Token* token = this->peek_token();
523 Location location = token->location();
525 bool is_anonymous_pointer;
526 if (token->is_op(OPERATOR_MULT))
529 is_anonymous_pointer = true;
531 else if (token->is_identifier())
533 std::string id = token->identifier();
534 bool is_id_exported = token->is_identifier_exported();
535 Location id_location = token->location();
536 token = this->advance_token();
537 is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
538 || token->is_op(OPERATOR_RCURLY)
539 || token->is_op(OPERATOR_DOT)
540 || token->is_string());
541 is_anonymous_pointer = false;
542 this->unget_token(Token::make_identifier_token(id, is_id_exported,
547 error_at(this->location(), "expected field name");
548 this->gogo_->mark_locals_used();
549 while (!token->is_op(OPERATOR_SEMICOLON)
550 && !token->is_op(OPERATOR_RCURLY)
552 token = this->advance_token();
558 if (is_anonymous_pointer)
560 this->advance_token();
561 if (!this->peek_token()->is_identifier())
563 error_at(this->location(), "expected field name");
564 this->gogo_->mark_locals_used();
565 while (!token->is_op(OPERATOR_SEMICOLON)
566 && !token->is_op(OPERATOR_RCURLY)
568 token = this->advance_token();
572 Type* type = this->type_name(true);
575 if (this->peek_token()->is_string())
577 tag = this->peek_token()->string_value();
578 this->advance_token();
581 if (!type->is_error_type())
583 if (is_anonymous_pointer)
584 type = Type::make_pointer_type(type);
585 sfl->push_back(Struct_field(Typed_identifier("", type, location)));
587 sfl->back().set_tag(tag);
592 Typed_identifier_list til;
595 token = this->peek_token();
596 if (!token->is_identifier())
598 error_at(this->location(), "expected identifier");
602 this->gogo_->pack_hidden_name(token->identifier(),
603 token->is_identifier_exported());
604 til.push_back(Typed_identifier(name, NULL, token->location()));
605 if (!this->advance_token()->is_op(OPERATOR_COMMA))
607 this->advance_token();
610 Type* type = this->type();
613 if (this->peek_token()->is_string())
615 tag = this->peek_token()->string_value();
616 this->advance_token();
619 for (Typed_identifier_list::iterator p = til.begin();
624 sfl->push_back(Struct_field(*p));
626 sfl->back().set_tag(tag);
631 // PointerType = "*" Type .
634 Parse::pointer_type()
636 go_assert(this->peek_token()->is_op(OPERATOR_MULT));
637 this->advance_token();
638 Type* type = this->type();
639 if (type->is_error_type())
641 return Type::make_pointer_type(type);
644 // ChannelType = Channel | SendChannel | RecvChannel .
645 // Channel = "chan" ElementType .
646 // SendChannel = "chan" "<-" ElementType .
647 // RecvChannel = "<-" "chan" ElementType .
650 Parse::channel_type()
652 const Token* token = this->peek_token();
655 if (token->is_op(OPERATOR_CHANOP))
657 if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
659 error_at(this->location(), "expected %<chan%>");
660 return Type::make_error_type();
663 this->advance_token();
667 go_assert(token->is_keyword(KEYWORD_CHAN));
668 if (this->advance_token()->is_op(OPERATOR_CHANOP))
671 this->advance_token();
675 // Better error messages for the common error of omitting the
676 // channel element type.
677 if (!this->type_may_start_here())
679 token = this->peek_token();
680 if (token->is_op(OPERATOR_RCURLY))
681 error_at(this->location(), "unexpected %<}%> in channel type");
682 else if (token->is_op(OPERATOR_RPAREN))
683 error_at(this->location(), "unexpected %<)%> in channel type");
684 else if (token->is_op(OPERATOR_COMMA))
685 error_at(this->location(), "unexpected comma in channel type");
687 error_at(this->location(), "expected channel element type");
688 return Type::make_error_type();
691 Type* element_type = this->type();
692 return Type::make_channel_type(send, receive, element_type);
695 // Give an error for a duplicate parameter or receiver name.
698 Parse::check_signature_names(const Typed_identifier_list* params,
701 for (Typed_identifier_list::const_iterator p = params->begin();
705 if (p->name().empty() || Gogo::is_sink_name(p->name()))
707 std::pair<std::string, const Typed_identifier*> val =
708 std::make_pair(p->name(), &*p);
709 std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
712 error_at(p->location(), "redefinition of %qs",
713 Gogo::message_name(p->name()).c_str());
714 inform(ins.first->second->location(),
715 "previous definition of %qs was here",
716 Gogo::message_name(p->name()).c_str());
721 // Signature = Parameters [ Result ] .
723 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
724 // location of the start of the type.
726 // This returns NULL on a parse error.
729 Parse::signature(Typed_identifier* receiver, Location location)
731 bool is_varargs = false;
732 Typed_identifier_list* params;
733 bool params_ok = this->parameters(¶ms, &is_varargs);
735 Typed_identifier_list* results = NULL;
736 if (this->peek_token()->is_op(OPERATOR_LPAREN)
737 || this->type_may_start_here())
739 if (!this->result(&results))
748 this->check_signature_names(params, &names);
750 this->check_signature_names(results, &names);
752 Function_type* ret = Type::make_function_type(receiver, params, results,
755 ret->set_is_varargs();
759 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
761 // This returns false on a parse error.
764 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
768 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
770 error_at(this->location(), "expected %<(%>");
774 Typed_identifier_list* params = NULL;
775 bool saw_error = false;
777 const Token* token = this->advance_token();
778 if (!token->is_op(OPERATOR_RPAREN))
780 params = this->parameter_list(is_varargs);
783 token = this->peek_token();
786 // The optional trailing comma is picked up in parameter_list.
788 if (!token->is_op(OPERATOR_RPAREN))
789 error_at(this->location(), "expected %<)%>");
791 this->advance_token();
800 // ParameterList = ParameterDecl { "," ParameterDecl } .
802 // This sets *IS_VARARGS if the list ends with an ellipsis.
803 // IS_VARARGS will be NULL if varargs are not permitted.
805 // We pick up an optional trailing comma.
807 // This returns NULL if some error is seen.
809 Typed_identifier_list*
810 Parse::parameter_list(bool* is_varargs)
812 Location location = this->location();
813 Typed_identifier_list* ret = new Typed_identifier_list();
815 bool saw_error = false;
817 // If we see an identifier and then a comma, then we don't know
818 // whether we are looking at a list of identifiers followed by a
819 // type, or a list of types given by name. We have to do an
820 // arbitrary lookahead to figure it out.
822 bool parameters_have_names;
823 const Token* token = this->peek_token();
824 if (!token->is_identifier())
826 // This must be a type which starts with something like '*'.
827 parameters_have_names = false;
831 std::string name = token->identifier();
832 bool is_exported = token->is_identifier_exported();
833 Location location = token->location();
834 token = this->advance_token();
835 if (!token->is_op(OPERATOR_COMMA))
837 if (token->is_op(OPERATOR_DOT))
839 // This is a qualified identifier, which must turn out
841 parameters_have_names = false;
843 else if (token->is_op(OPERATOR_RPAREN))
845 // A single identifier followed by a parenthesis must be
847 parameters_have_names = false;
851 // An identifier followed by something other than a
852 // comma or a dot or a right parenthesis must be a
853 // parameter name followed by a type.
854 parameters_have_names = true;
857 this->unget_token(Token::make_identifier_token(name, is_exported,
862 // An identifier followed by a comma may be the first in a
863 // list of parameter names followed by a type, or it may be
864 // the first in a list of types without parameter names. To
865 // find out we gather as many identifiers separated by
867 std::string id_name = this->gogo_->pack_hidden_name(name,
869 ret->push_back(Typed_identifier(id_name, NULL, location));
870 bool just_saw_comma = true;
871 while (this->advance_token()->is_identifier())
873 name = this->peek_token()->identifier();
874 is_exported = this->peek_token()->is_identifier_exported();
875 location = this->peek_token()->location();
876 id_name = this->gogo_->pack_hidden_name(name, is_exported);
877 ret->push_back(Typed_identifier(id_name, NULL, location));
878 if (!this->advance_token()->is_op(OPERATOR_COMMA))
880 just_saw_comma = false;
887 // We saw ID1 "," ID2 "," followed by something which
888 // was not an identifier. We must be seeing the start
889 // of a type, and ID1 and ID2 must be types, and the
890 // parameters don't have names.
891 parameters_have_names = false;
893 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
895 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
896 // and the parameters don't have names.
897 parameters_have_names = false;
899 else if (this->peek_token()->is_op(OPERATOR_DOT))
901 // We saw ID1 "," ID2 ".". ID2 must be a package name,
902 // ID1 must be a type, and the parameters don't have
904 parameters_have_names = false;
905 this->unget_token(Token::make_identifier_token(name, is_exported,
908 just_saw_comma = true;
912 // We saw ID1 "," ID2 followed by something other than
913 // ",", ".", or ")". We must be looking at the start of
914 // a type, and ID1 and ID2 must be parameter names.
915 parameters_have_names = true;
918 if (parameters_have_names)
920 go_assert(!just_saw_comma);
921 // We have just seen ID1, ID2 xxx.
923 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
927 error_at(this->location(), "%<...%> only permits one name");
929 this->advance_token();
932 for (size_t i = 0; i < ret->size(); ++i)
933 ret->set_type(i, type);
934 if (!this->peek_token()->is_op(OPERATOR_COMMA))
935 return saw_error ? NULL : ret;
936 if (this->advance_token()->is_op(OPERATOR_RPAREN))
937 return saw_error ? NULL : ret;
941 Typed_identifier_list* tret = new Typed_identifier_list();
942 for (Typed_identifier_list::const_iterator p = ret->begin();
946 Named_object* no = this->gogo_->lookup(p->name(), NULL);
949 no = this->gogo_->add_unknown_name(p->name(),
953 type = no->type_value();
954 else if (no->is_unknown() || no->is_type_declaration())
955 type = Type::make_forward_declaration(no);
958 error_at(p->location(), "expected %<%s%> to be a type",
959 Gogo::message_name(p->name()).c_str());
961 type = Type::make_error_type();
963 tret->push_back(Typed_identifier("", type, p->location()));
968 || this->peek_token()->is_op(OPERATOR_RPAREN))
969 return saw_error ? NULL : ret;
974 bool mix_error = false;
975 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
976 while (this->peek_token()->is_op(OPERATOR_COMMA))
978 if (is_varargs != NULL && *is_varargs)
980 error_at(this->location(), "%<...%> must be last parameter");
983 if (this->advance_token()->is_op(OPERATOR_RPAREN))
985 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
989 error_at(location, "invalid named/anonymous mix");
1000 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1003 Parse::parameter_decl(bool parameters_have_names,
1004 Typed_identifier_list* til,
1008 if (!parameters_have_names)
1011 Location location = this->location();
1012 if (!this->peek_token()->is_identifier())
1014 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1015 type = this->type();
1018 if (is_varargs == NULL)
1019 error_at(this->location(), "invalid use of %<...%>");
1022 this->advance_token();
1023 if (is_varargs == NULL
1024 && this->peek_token()->is_op(OPERATOR_RPAREN))
1025 type = Type::make_error_type();
1028 Type* element_type = this->type();
1029 type = Type::make_array_type(element_type, NULL);
1035 type = this->type_name(false);
1036 if (type->is_error_type()
1037 || (!this->peek_token()->is_op(OPERATOR_COMMA)
1038 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1041 while (!this->peek_token()->is_op(OPERATOR_COMMA)
1042 && !this->peek_token()->is_op(OPERATOR_RPAREN))
1043 this->advance_token();
1046 if (!type->is_error_type())
1047 til->push_back(Typed_identifier("", type, location));
1051 size_t orig_count = til->size();
1052 if (this->peek_token()->is_identifier())
1053 this->identifier_list(til);
1056 size_t new_count = til->size();
1059 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1060 type = this->type();
1063 if (is_varargs == NULL)
1064 error_at(this->location(), "invalid use of %<...%>");
1065 else if (new_count > orig_count + 1)
1066 error_at(this->location(), "%<...%> only permits one name");
1069 this->advance_token();
1070 Type* element_type = this->type();
1071 type = Type::make_array_type(element_type, NULL);
1073 for (size_t i = orig_count; i < new_count; ++i)
1074 til->set_type(i, type);
1078 // Result = Parameters | Type .
1080 // This returns false on a parse error.
1083 Parse::result(Typed_identifier_list** presults)
1085 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1086 return this->parameters(presults, NULL);
1089 Location location = this->location();
1090 Type* type = this->type();
1091 if (type->is_error_type())
1096 Typed_identifier_list* til = new Typed_identifier_list();
1097 til->push_back(Typed_identifier("", type, location));
1103 // Block = "{" [ StatementList ] "}" .
1105 // Returns the location of the closing brace.
1110 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1112 Location loc = this->location();
1113 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1114 && this->advance_token()->is_op(OPERATOR_LCURLY))
1115 error_at(loc, "unexpected semicolon or newline before %<{%>");
1118 error_at(this->location(), "expected %<{%>");
1119 return Linemap::unknown_location();
1123 const Token* token = this->advance_token();
1125 if (!token->is_op(OPERATOR_RCURLY))
1127 this->statement_list();
1128 token = this->peek_token();
1129 if (!token->is_op(OPERATOR_RCURLY))
1131 if (!token->is_eof() || !saw_errors())
1132 error_at(this->location(), "expected %<}%>");
1134 this->gogo_->mark_locals_used();
1136 // Skip ahead to the end of the block, in hopes of avoiding
1137 // lots of meaningless errors.
1138 Location ret = token->location();
1140 while (!token->is_eof())
1142 if (token->is_op(OPERATOR_LCURLY))
1144 else if (token->is_op(OPERATOR_RCURLY))
1149 this->advance_token();
1153 token = this->advance_token();
1154 ret = token->location();
1160 Location ret = token->location();
1161 this->advance_token();
1165 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1166 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1169 Parse::interface_type()
1171 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1172 Location location = this->location();
1174 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1176 Location token_loc = this->location();
1177 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1178 && this->advance_token()->is_op(OPERATOR_LCURLY))
1179 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1182 error_at(this->location(), "expected %<{%>");
1183 return Type::make_error_type();
1186 this->advance_token();
1188 Typed_identifier_list* methods = new Typed_identifier_list();
1189 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1191 this->method_spec(methods);
1192 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1194 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1196 this->method_spec(methods);
1198 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1200 error_at(this->location(), "expected %<}%>");
1201 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1203 if (this->peek_token()->is_eof())
1204 return Type::make_error_type();
1208 this->advance_token();
1210 if (methods->empty())
1216 Interface_type* ret = Type::make_interface_type(methods, location);
1217 this->gogo_->record_interface_type(ret);
1221 // MethodSpec = MethodName Signature | InterfaceTypeName .
1222 // MethodName = identifier .
1223 // InterfaceTypeName = TypeName .
1226 Parse::method_spec(Typed_identifier_list* methods)
1228 const Token* token = this->peek_token();
1229 if (!token->is_identifier())
1231 error_at(this->location(), "expected identifier");
1235 std::string name = token->identifier();
1236 bool is_exported = token->is_identifier_exported();
1237 Location location = token->location();
1239 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1241 // This is a MethodName.
1242 name = this->gogo_->pack_hidden_name(name, is_exported);
1243 Type* type = this->signature(NULL, location);
1246 methods->push_back(Typed_identifier(name, type, location));
1250 this->unget_token(Token::make_identifier_token(name, is_exported,
1252 Type* type = this->type_name(false);
1253 if (type->is_error_type()
1254 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1255 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1257 if (this->peek_token()->is_op(OPERATOR_COMMA))
1258 error_at(this->location(),
1259 "name list not allowed in interface type");
1261 error_at(location, "expected signature or type name");
1262 this->gogo_->mark_locals_used();
1263 token = this->peek_token();
1264 while (!token->is_eof()
1265 && !token->is_op(OPERATOR_SEMICOLON)
1266 && !token->is_op(OPERATOR_RCURLY))
1267 token = this->advance_token();
1270 // This must be an interface type, but we can't check that now.
1271 // We check it and pull out the methods in
1272 // Interface_type::do_verify.
1273 methods->push_back(Typed_identifier("", type, location));
1277 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1280 Parse::declaration()
1282 const Token* token = this->peek_token();
1283 if (token->is_keyword(KEYWORD_CONST))
1285 else if (token->is_keyword(KEYWORD_TYPE))
1287 else if (token->is_keyword(KEYWORD_VAR))
1289 else if (token->is_keyword(KEYWORD_FUNC))
1290 this->function_decl();
1293 error_at(this->location(), "expected declaration");
1294 this->advance_token();
1299 Parse::declaration_may_start_here()
1301 const Token* token = this->peek_token();
1302 return (token->is_keyword(KEYWORD_CONST)
1303 || token->is_keyword(KEYWORD_TYPE)
1304 || token->is_keyword(KEYWORD_VAR)
1305 || token->is_keyword(KEYWORD_FUNC));
1308 // Decl<P> = P | "(" [ List<P> ] ")" .
1311 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1313 if (this->peek_token()->is_eof())
1316 error_at(this->location(), "unexpected end of file");
1320 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1324 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1326 this->list(pfn, varg, true);
1327 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1329 error_at(this->location(), "missing %<)%>");
1330 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1332 if (this->peek_token()->is_eof())
1337 this->advance_token();
1341 // List<P> = P { ";" P } [ ";" ] .
1343 // In order to pick up the trailing semicolon we need to know what
1344 // might follow. This is either a '}' or a ')'.
1347 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1350 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1351 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1352 || this->peek_token()->is_op(OPERATOR_COMMA))
1354 if (this->peek_token()->is_op(OPERATOR_COMMA))
1355 error_at(this->location(), "unexpected comma");
1356 if (this->advance_token()->is_op(follow))
1362 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1367 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1368 this->advance_token();
1371 Type* last_type = NULL;
1372 Expression_list* last_expr_list = NULL;
1374 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1375 this->const_spec(&last_type, &last_expr_list);
1378 this->advance_token();
1379 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1381 this->const_spec(&last_type, &last_expr_list);
1382 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1383 this->advance_token();
1384 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1386 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1387 if (!this->skip_past_error(OPERATOR_RPAREN))
1391 this->advance_token();
1394 if (last_expr_list != NULL)
1395 delete last_expr_list;
1398 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1401 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1403 Typed_identifier_list til;
1404 this->identifier_list(&til);
1407 if (this->type_may_start_here())
1409 type = this->type();
1411 *last_expr_list = NULL;
1414 Expression_list *expr_list;
1415 if (!this->peek_token()->is_op(OPERATOR_EQ))
1417 if (*last_expr_list == NULL)
1419 error_at(this->location(), "expected %<=%>");
1423 expr_list = new Expression_list;
1424 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1425 p != (*last_expr_list)->end();
1427 expr_list->push_back((*p)->copy());
1431 this->advance_token();
1432 expr_list = this->expression_list(NULL, false, true);
1434 if (*last_expr_list != NULL)
1435 delete *last_expr_list;
1436 *last_expr_list = expr_list;
1439 Expression_list::const_iterator pe = expr_list->begin();
1440 for (Typed_identifier_list::iterator pi = til.begin();
1444 if (pe == expr_list->end())
1446 error_at(this->location(), "not enough initializers");
1452 if (!Gogo::is_sink_name(pi->name()))
1453 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1455 if (pe != expr_list->end())
1456 error_at(this->location(), "too many initializers");
1458 this->increment_iota();
1463 // TypeDecl = "type" Decl<TypeSpec> .
1468 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1469 this->advance_token();
1470 this->decl(&Parse::type_spec, NULL);
1473 // TypeSpec = identifier Type .
1476 Parse::type_spec(void*)
1478 const Token* token = this->peek_token();
1479 if (!token->is_identifier())
1481 error_at(this->location(), "expected identifier");
1484 std::string name = token->identifier();
1485 bool is_exported = token->is_identifier_exported();
1486 Location location = token->location();
1487 token = this->advance_token();
1489 // The scope of the type name starts at the point where the
1490 // identifier appears in the source code. We implement this by
1491 // declaring the type before we read the type definition.
1492 Named_object* named_type = NULL;
1495 name = this->gogo_->pack_hidden_name(name, is_exported);
1496 named_type = this->gogo_->declare_type(name, location);
1500 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1501 type = this->type();
1504 error_at(this->location(),
1505 "unexpected semicolon or newline in type declaration");
1506 type = Type::make_error_type();
1507 this->advance_token();
1510 if (type->is_error_type())
1512 this->gogo_->mark_locals_used();
1513 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1514 && !this->peek_token()->is_eof())
1515 this->advance_token();
1520 if (named_type->is_type_declaration())
1522 Type* ftype = type->forwarded();
1523 if (ftype->forward_declaration_type() != NULL
1524 && (ftype->forward_declaration_type()->named_object()
1527 error_at(location, "invalid recursive type");
1528 type = Type::make_error_type();
1531 this->gogo_->define_type(named_type,
1532 Type::make_named_type(named_type, type,
1534 go_assert(named_type->package() == NULL);
1538 // This will probably give a redefinition error.
1539 this->gogo_->add_type(name, type, location);
1544 // VarDecl = "var" Decl<VarSpec> .
1549 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1550 this->advance_token();
1551 this->decl(&Parse::var_spec, NULL);
1554 // VarSpec = IdentifierList
1555 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1558 Parse::var_spec(void*)
1560 // Get the variable names.
1561 Typed_identifier_list til;
1562 this->identifier_list(&til);
1564 Location location = this->location();
1567 Expression_list* init = NULL;
1568 if (!this->peek_token()->is_op(OPERATOR_EQ))
1570 type = this->type();
1571 if (type->is_error_type())
1573 this->gogo_->mark_locals_used();
1574 while (!this->peek_token()->is_op(OPERATOR_EQ)
1575 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1576 && !this->peek_token()->is_eof())
1577 this->advance_token();
1579 if (this->peek_token()->is_op(OPERATOR_EQ))
1581 this->advance_token();
1582 init = this->expression_list(NULL, false, true);
1587 this->advance_token();
1588 init = this->expression_list(NULL, false, true);
1591 this->init_vars(&til, type, init, false, location);
1597 // Create variables. TIL is a list of variable names. If TYPE is not
1598 // NULL, it is the type of all the variables. If INIT is not NULL, it
1599 // is an initializer list for the variables.
1602 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1603 Expression_list* init, bool is_coloneq,
1606 // Check for an initialization which can yield multiple values.
1607 if (init != NULL && init->size() == 1 && til->size() > 1)
1609 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1612 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1615 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1618 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1619 is_coloneq, location))
1623 if (init != NULL && init->size() != til->size())
1625 if (init->empty() || !init->front()->is_error_expression())
1626 error_at(location, "wrong number of initializations");
1629 type = Type::make_error_type();
1632 // Note that INIT was already parsed with the old name bindings, so
1633 // we don't have to worry that it will accidentally refer to the
1634 // newly declared variables. But we do have to worry about a mix of
1635 // newly declared variables and old variables if the old variables
1636 // appear in the initializations.
1638 Expression_list::const_iterator pexpr;
1640 pexpr = init->begin();
1641 bool any_new = false;
1642 Expression_list* vars = new Expression_list();
1643 Expression_list* vals = new Expression_list();
1644 for (Typed_identifier_list::const_iterator p = til->begin();
1649 go_assert(pexpr != init->end());
1650 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1651 false, &any_new, vars, vals);
1656 go_assert(pexpr == init->end());
1657 if (is_coloneq && !any_new)
1658 error_at(location, "variables redeclared but no variable is new");
1659 this->finish_init_vars(vars, vals, location);
1662 // See if we need to initialize a list of variables from a function
1663 // call. This returns true if we have set up the variables and the
1667 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1668 Expression* expr, bool is_coloneq,
1671 Call_expression* call = expr->call_expression();
1675 // This is a function call. We can't check here whether it returns
1676 // the right number of values, but it might. Declare the variables,
1677 // and then assign the results of the call to them.
1679 Named_object* first_var = NULL;
1680 unsigned int index = 0;
1681 bool any_new = false;
1682 Expression_list* ivars = new Expression_list();
1683 Expression_list* ivals = new Expression_list();
1684 for (Typed_identifier_list::const_iterator pv = vars->begin();
1688 Expression* init = Expression::make_call_result(call, index);
1689 Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
1690 &any_new, ivars, ivals);
1692 if (this->gogo_->in_global_scope() && no->is_variable())
1694 if (first_var == NULL)
1698 // The subsequent vars have an implicit dependency on
1699 // the first one, so that everything gets initialized in
1700 // the right order and so that we detect cycles
1702 this->gogo_->record_var_depends_on(no->var_value(), first_var);
1707 if (is_coloneq && !any_new)
1708 error_at(location, "variables redeclared but no variable is new");
1710 this->finish_init_vars(ivars, ivals, location);
1715 // See if we need to initialize a pair of values from a map index
1716 // expression. This returns true if we have set up the variables and
1717 // the initialization.
1720 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1721 Expression* expr, bool is_coloneq,
1724 Index_expression* index = expr->index_expression();
1727 if (vars->size() != 2)
1730 // This is an index which is being assigned to two variables. It
1731 // must be a map index. Declare the variables, and then assign the
1732 // results of the map index.
1733 bool any_new = false;
1734 Typed_identifier_list::const_iterator p = vars->begin();
1735 Expression* init = type == NULL ? index : NULL;
1736 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1737 type == NULL, &any_new, NULL, NULL);
1738 if (type == NULL && any_new && val_no->is_variable())
1739 val_no->var_value()->set_type_from_init_tuple();
1740 Expression* val_var = Expression::make_var_reference(val_no, location);
1743 Type* var_type = type;
1744 if (var_type == NULL)
1745 var_type = Type::lookup_bool_type();
1746 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1747 &any_new, NULL, NULL);
1748 Expression* present_var = Expression::make_var_reference(no, location);
1750 if (is_coloneq && !any_new)
1751 error_at(location, "variables redeclared but no variable is new");
1753 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1756 if (!this->gogo_->in_global_scope())
1757 this->gogo_->add_statement(s);
1758 else if (!val_no->is_sink())
1760 if (val_no->is_variable())
1761 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1763 else if (!no->is_sink())
1765 if (no->is_variable())
1766 no->var_value()->add_preinit_statement(this->gogo_, s);
1770 // Execute the map index expression just so that we can fail if
1772 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1774 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1780 // See if we need to initialize a pair of values from a receive
1781 // expression. This returns true if we have set up the variables and
1782 // the initialization.
1785 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1786 Expression* expr, bool is_coloneq,
1789 Receive_expression* receive = expr->receive_expression();
1790 if (receive == NULL)
1792 if (vars->size() != 2)
1795 // This is a receive expression which is being assigned to two
1796 // variables. Declare the variables, and then assign the results of
1798 bool any_new = false;
1799 Typed_identifier_list::const_iterator p = vars->begin();
1800 Expression* init = type == NULL ? receive : NULL;
1801 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1802 type == NULL, &any_new, NULL, NULL);
1803 if (type == NULL && any_new && val_no->is_variable())
1804 val_no->var_value()->set_type_from_init_tuple();
1805 Expression* val_var = Expression::make_var_reference(val_no, location);
1808 Type* var_type = type;
1809 if (var_type == NULL)
1810 var_type = Type::lookup_bool_type();
1811 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1812 &any_new, NULL, NULL);
1813 Expression* received_var = Expression::make_var_reference(no, location);
1815 if (is_coloneq && !any_new)
1816 error_at(location, "variables redeclared but no variable is new");
1818 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1823 if (!this->gogo_->in_global_scope())
1824 this->gogo_->add_statement(s);
1825 else if (!val_no->is_sink())
1827 if (val_no->is_variable())
1828 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1830 else if (!no->is_sink())
1832 if (no->is_variable())
1833 no->var_value()->add_preinit_statement(this->gogo_, s);
1837 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1839 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1845 // See if we need to initialize a pair of values from a type guard
1846 // expression. This returns true if we have set up the variables and
1847 // the initialization.
1850 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1851 Type* type, Expression* expr,
1852 bool is_coloneq, Location location)
1854 Type_guard_expression* type_guard = expr->type_guard_expression();
1855 if (type_guard == NULL)
1857 if (vars->size() != 2)
1860 // This is a type guard expression which is being assigned to two
1861 // variables. Declare the variables, and then assign the results of
1863 bool any_new = false;
1864 Typed_identifier_list::const_iterator p = vars->begin();
1865 Type* var_type = type;
1866 if (var_type == NULL)
1867 var_type = type_guard->type();
1868 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1869 &any_new, NULL, NULL);
1870 Expression* val_var = Expression::make_var_reference(val_no, location);
1874 if (var_type == NULL)
1875 var_type = Type::lookup_bool_type();
1876 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1877 &any_new, NULL, NULL);
1878 Expression* ok_var = Expression::make_var_reference(no, location);
1880 Expression* texpr = type_guard->expr();
1881 Type* t = type_guard->type();
1882 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1886 if (is_coloneq && !any_new)
1887 error_at(location, "variables redeclared but no variable is new");
1889 if (!this->gogo_->in_global_scope())
1890 this->gogo_->add_statement(s);
1891 else if (!val_no->is_sink())
1893 if (val_no->is_variable())
1894 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1896 else if (!no->is_sink())
1898 if (no->is_variable())
1899 no->var_value()->add_preinit_statement(this->gogo_, s);
1903 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1904 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1910 // Create a single variable. If IS_COLONEQ is true, we permit
1911 // redeclarations in the same block, and we set *IS_NEW when we find a
1912 // new variable which is not a redeclaration.
1915 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1916 bool is_coloneq, bool type_from_init, bool* is_new,
1917 Expression_list* vars, Expression_list* vals)
1919 Location location = tid.location();
1921 if (Gogo::is_sink_name(tid.name()))
1923 if (!type_from_init && init != NULL)
1925 if (this->gogo_->in_global_scope())
1926 return this->create_dummy_global(type, init, location);
1927 else if (type == NULL)
1928 this->gogo_->add_statement(Statement::make_statement(init, true));
1931 // With both a type and an initializer, create a dummy
1932 // variable so that we will check whether the
1933 // initializer can be assigned to the type.
1934 Variable* var = new Variable(type, init, false, false, false,
1939 snprintf(buf, sizeof buf, "sink$%d", count);
1941 return this->gogo_->add_variable(buf, var);
1945 this->gogo_->add_type_to_verify(type);
1946 return this->gogo_->add_sink();
1951 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1953 && (no->is_variable() || no->is_result_variable()))
1955 // INIT may be NULL even when IS_COLONEQ is true for cases
1956 // like v, ok := x.(int).
1957 if (!type_from_init && init != NULL)
1959 go_assert(vars != NULL && vals != NULL);
1960 vars->push_back(Expression::make_var_reference(no, location));
1961 vals->push_back(init);
1967 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1968 false, false, location);
1969 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1970 if (!no->is_variable())
1972 // The name is already defined, so we just gave an error.
1973 return this->gogo_->add_sink();
1978 // Create a dummy global variable to force an initializer to be run in
1979 // the right place. This is used when a sink variable is initialized
1983 Parse::create_dummy_global(Type* type, Expression* init,
1986 if (type == NULL && init == NULL)
1987 type = Type::lookup_bool_type();
1988 Variable* var = new Variable(type, init, true, false, false, location);
1991 snprintf(buf, sizeof buf, "_.%d", count);
1993 return this->gogo_->add_variable(buf, var);
1996 // Finish the variable initialization by executing any assignments to
1997 // existing variables when using :=. These must be done as a tuple
1998 // assignment in case of something like n, a, b := 1, b, a.
2001 Parse::finish_init_vars(Expression_list* vars, Expression_list* vals,
2009 else if (vars->size() == 1)
2011 go_assert(!this->gogo_->in_global_scope());
2012 this->gogo_->add_statement(Statement::make_assignment(vars->front(),
2020 go_assert(!this->gogo_->in_global_scope());
2021 this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals,
2026 // SimpleVarDecl = identifier ":=" Expression .
2028 // We've already seen the identifier.
2030 // FIXME: We also have to implement
2031 // IdentifierList ":=" ExpressionList
2032 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2033 // tuple assignments here as well.
2035 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2036 // side may be a composite literal.
2038 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2041 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2042 // guard (var := expr.("type") using the literal keyword "type").
2045 Parse::simple_var_decl_or_assignment(const std::string& name,
2047 bool may_be_composite_lit,
2048 Range_clause* p_range_clause,
2049 Type_switch* p_type_switch)
2051 Typed_identifier_list til;
2052 til.push_back(Typed_identifier(name, NULL, location));
2054 // We've seen one identifier. If we see a comma now, this could be
2056 if (this->peek_token()->is_op(OPERATOR_COMMA))
2058 go_assert(p_type_switch == NULL);
2061 const Token* token = this->advance_token();
2062 if (!token->is_identifier())
2065 std::string id = token->identifier();
2066 bool is_id_exported = token->is_identifier_exported();
2067 Location id_location = token->location();
2069 token = this->advance_token();
2070 if (!token->is_op(OPERATOR_COMMA))
2072 if (token->is_op(OPERATOR_COLONEQ))
2074 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2075 til.push_back(Typed_identifier(id, NULL, location));
2078 this->unget_token(Token::make_identifier_token(id,
2084 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2085 til.push_back(Typed_identifier(id, NULL, location));
2088 // We have a comma separated list of identifiers in TIL. If the
2089 // next token is COLONEQ, then this is a simple var decl, and we
2090 // have the complete list of identifiers. If the next token is
2091 // not COLONEQ, then the only valid parse is a tuple assignment.
2092 // The list of identifiers we have so far is really a list of
2093 // expressions. There are more expressions following.
2095 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2097 Expression_list* exprs = new Expression_list;
2098 for (Typed_identifier_list::const_iterator p = til.begin();
2101 exprs->push_back(this->id_to_expression(p->name(),
2104 Expression_list* more_exprs =
2105 this->expression_list(NULL, true, may_be_composite_lit);
2106 for (Expression_list::const_iterator p = more_exprs->begin();
2107 p != more_exprs->end();
2109 exprs->push_back(*p);
2112 this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
2117 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2118 const Token* token = this->advance_token();
2120 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2122 this->range_clause_decl(&til, p_range_clause);
2126 Expression_list* init;
2127 if (p_type_switch == NULL)
2128 init = this->expression_list(NULL, false, may_be_composite_lit);
2131 bool is_type_switch = false;
2132 Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
2133 may_be_composite_lit,
2137 p_type_switch->found = true;
2138 p_type_switch->name = name;
2139 p_type_switch->location = location;
2140 p_type_switch->expr = expr;
2144 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2146 init = new Expression_list();
2147 init->push_back(expr);
2151 this->advance_token();
2152 init = this->expression_list(expr, false, may_be_composite_lit);
2156 this->init_vars(&til, NULL, init, true, location);
2159 // FunctionDecl = "func" identifier Signature [ Block ] .
2160 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2162 // Deprecated gcc extension:
2163 // FunctionDecl = "func" identifier Signature
2164 // __asm__ "(" string_lit ")" .
2165 // This extension means a function whose real name is the identifier
2166 // inside the asm. This extension will be removed at some future
2167 // date. It has been replaced with //extern comments.
2170 Parse::function_decl()
2172 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2173 Location location = this->location();
2174 std::string extern_name = this->lex_->extern_name();
2175 const Token* token = this->advance_token();
2177 Typed_identifier* rec = NULL;
2178 if (token->is_op(OPERATOR_LPAREN))
2180 rec = this->receiver();
2181 token = this->peek_token();
2184 if (!token->is_identifier())
2186 error_at(this->location(), "expected function name");
2191 this->gogo_->pack_hidden_name(token->identifier(),
2192 token->is_identifier_exported());
2194 this->advance_token();
2196 Function_type* fntype = this->signature(rec, this->location());
2198 Named_object* named_object = NULL;
2200 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2202 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2204 error_at(this->location(), "expected %<(%>");
2207 token = this->advance_token();
2208 if (!token->is_string())
2210 error_at(this->location(), "expected string");
2213 std::string asm_name = token->string_value();
2214 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2216 error_at(this->location(), "expected %<)%>");
2219 this->advance_token();
2220 if (!Gogo::is_sink_name(name))
2222 named_object = this->gogo_->declare_function(name, fntype, location);
2223 if (named_object->is_function_declaration())
2224 named_object->func_declaration_value()->set_asm_name(asm_name);
2228 // Check for the easy error of a newline before the opening brace.
2229 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2231 Location semi_loc = this->location();
2232 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2233 error_at(this->location(),
2234 "unexpected semicolon or newline before %<{%>");
2236 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2240 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2242 if (named_object == NULL && !Gogo::is_sink_name(name))
2245 this->gogo_->add_erroneous_name(name);
2248 named_object = this->gogo_->declare_function(name, fntype,
2250 if (!extern_name.empty()
2251 && named_object->is_function_declaration())
2253 Function_declaration* fd =
2254 named_object->func_declaration_value();
2255 fd->set_asm_name(extern_name);
2262 bool hold_is_erroneous_function = this->is_erroneous_function_;
2265 fntype = Type::make_function_type(NULL, NULL, NULL, location);
2266 this->is_erroneous_function_ = true;
2267 if (!Gogo::is_sink_name(name))
2268 this->gogo_->add_erroneous_name(name);
2269 name = this->gogo_->pack_hidden_name("_", false);
2271 this->gogo_->start_function(name, fntype, true, location);
2272 Location end_loc = this->block();
2273 this->gogo_->finish_function(end_loc);
2274 this->is_erroneous_function_ = hold_is_erroneous_function;
2278 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2279 // BaseTypeName = identifier .
2284 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2287 const Token* token = this->advance_token();
2288 Location location = token->location();
2289 if (!token->is_op(OPERATOR_MULT))
2291 if (!token->is_identifier())
2293 error_at(this->location(), "method has no receiver");
2294 this->gogo_->mark_locals_used();
2295 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2296 token = this->advance_token();
2297 if (!token->is_eof())
2298 this->advance_token();
2301 name = token->identifier();
2302 bool is_exported = token->is_identifier_exported();
2303 token = this->advance_token();
2304 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2306 // An identifier followed by something other than a dot or a
2307 // right parenthesis must be a receiver name followed by a
2309 name = this->gogo_->pack_hidden_name(name, is_exported);
2313 // This must be a type name.
2314 this->unget_token(Token::make_identifier_token(name, is_exported,
2316 token = this->peek_token();
2321 // Here the receiver name is in NAME (it is empty if the receiver is
2322 // unnamed) and TOKEN is the first token in the type.
2324 bool is_pointer = false;
2325 if (token->is_op(OPERATOR_MULT))
2328 token = this->advance_token();
2331 if (!token->is_identifier())
2333 error_at(this->location(), "expected receiver name or type");
2334 this->gogo_->mark_locals_used();
2335 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2336 while (!token->is_eof())
2338 token = this->advance_token();
2339 if (token->is_op(OPERATOR_LPAREN))
2341 else if (token->is_op(OPERATOR_RPAREN))
2348 if (!token->is_eof())
2349 this->advance_token();
2353 Type* type = this->type_name(true);
2355 if (is_pointer && !type->is_error_type())
2356 type = Type::make_pointer_type(type);
2358 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2359 this->advance_token();
2362 if (this->peek_token()->is_op(OPERATOR_COMMA))
2363 error_at(this->location(), "method has multiple receivers");
2365 error_at(this->location(), "expected %<)%>");
2366 this->gogo_->mark_locals_used();
2367 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2368 token = this->advance_token();
2369 if (!token->is_eof())
2370 this->advance_token();
2374 return new Typed_identifier(name, type, location);
2377 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2378 // Literal = BasicLit | CompositeLit | FunctionLit .
2379 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2381 // If MAY_BE_SINK is true, this operand may be "_".
2384 Parse::operand(bool may_be_sink)
2386 const Token* token = this->peek_token();
2388 switch (token->classification())
2390 case Token::TOKEN_IDENTIFIER:
2392 Location location = token->location();
2393 std::string id = token->identifier();
2394 bool is_exported = token->is_identifier_exported();
2395 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2397 Named_object* in_function;
2398 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2400 Package* package = NULL;
2401 if (named_object != NULL && named_object->is_package())
2403 if (!this->advance_token()->is_op(OPERATOR_DOT)
2404 || !this->advance_token()->is_identifier())
2406 error_at(location, "unexpected reference to package");
2407 return Expression::make_error(location);
2409 package = named_object->package_value();
2410 package->set_used();
2411 id = this->peek_token()->identifier();
2412 is_exported = this->peek_token()->is_identifier_exported();
2413 packed = this->gogo_->pack_hidden_name(id, is_exported);
2414 named_object = package->lookup(packed);
2415 location = this->location();
2416 go_assert(in_function == NULL);
2419 this->advance_token();
2421 if (named_object != NULL
2422 && named_object->is_type()
2423 && !named_object->type_value()->is_visible())
2425 go_assert(package != NULL);
2426 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2427 Gogo::message_name(package->package_name()).c_str(),
2428 Gogo::message_name(id).c_str());
2429 return Expression::make_error(location);
2433 if (named_object == NULL)
2435 if (package != NULL)
2437 std::string n1 = Gogo::message_name(package->package_name());
2438 std::string n2 = Gogo::message_name(id);
2441 ("invalid reference to unexported identifier "
2443 n1.c_str(), n2.c_str());
2446 "reference to undefined identifier %<%s.%s%>",
2447 n1.c_str(), n2.c_str());
2448 return Expression::make_error(location);
2451 named_object = this->gogo_->add_unknown_name(packed, location);
2454 if (in_function != NULL
2455 && in_function != this->gogo_->current_function()
2456 && (named_object->is_variable()
2457 || named_object->is_result_variable()))
2458 return this->enclosing_var_reference(in_function, named_object,
2461 switch (named_object->classification())
2463 case Named_object::NAMED_OBJECT_CONST:
2464 return Expression::make_const_reference(named_object, location);
2465 case Named_object::NAMED_OBJECT_TYPE:
2466 return Expression::make_type(named_object->type_value(), location);
2467 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2469 Type* t = Type::make_forward_declaration(named_object);
2470 return Expression::make_type(t, location);
2472 case Named_object::NAMED_OBJECT_VAR:
2473 case Named_object::NAMED_OBJECT_RESULT_VAR:
2474 this->mark_var_used(named_object);
2475 return Expression::make_var_reference(named_object, location);
2476 case Named_object::NAMED_OBJECT_SINK:
2478 return Expression::make_sink(location);
2481 error_at(location, "cannot use _ as value");
2482 return Expression::make_error(location);
2484 case Named_object::NAMED_OBJECT_FUNC:
2485 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2486 return Expression::make_func_reference(named_object, NULL,
2488 case Named_object::NAMED_OBJECT_UNKNOWN:
2490 Unknown_expression* ue =
2491 Expression::make_unknown_reference(named_object, location);
2492 if (this->is_erroneous_function_)
2493 ue->set_no_error_message();
2496 case Named_object::NAMED_OBJECT_ERRONEOUS:
2497 return Expression::make_error(location);
2504 case Token::TOKEN_STRING:
2505 ret = Expression::make_string(token->string_value(), token->location());
2506 this->advance_token();
2509 case Token::TOKEN_CHARACTER:
2510 ret = Expression::make_character(token->character_value(), NULL,
2512 this->advance_token();
2515 case Token::TOKEN_INTEGER:
2516 ret = Expression::make_integer(token->integer_value(), NULL,
2518 this->advance_token();
2521 case Token::TOKEN_FLOAT:
2522 ret = Expression::make_float(token->float_value(), NULL,
2524 this->advance_token();
2527 case Token::TOKEN_IMAGINARY:
2530 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2531 ret = Expression::make_complex(&zero, token->imaginary_value(),
2532 NULL, token->location());
2534 this->advance_token();
2538 case Token::TOKEN_KEYWORD:
2539 switch (token->keyword())
2542 return this->function_lit();
2544 case KEYWORD_INTERFACE:
2546 case KEYWORD_STRUCT:
2548 Location location = token->location();
2549 return Expression::make_type(this->type(), location);
2556 case Token::TOKEN_OPERATOR:
2557 if (token->is_op(OPERATOR_LPAREN))
2559 this->advance_token();
2560 ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL);
2561 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2562 error_at(this->location(), "missing %<)%>");
2564 this->advance_token();
2567 else if (token->is_op(OPERATOR_LSQUARE))
2569 // Here we call array_type directly, as this is the only
2570 // case where an ellipsis is permitted for an array type.
2571 Location location = token->location();
2572 return Expression::make_type(this->array_type(true), location);
2580 error_at(this->location(), "expected operand");
2581 return Expression::make_error(this->location());
2584 // Handle a reference to a variable in an enclosing function. We add
2585 // it to a list of such variables. We return a reference to a field
2586 // in a struct which will be passed on the static chain when calling
2587 // the current function.
2590 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2593 go_assert(var->is_variable() || var->is_result_variable());
2595 this->mark_var_used(var);
2597 Named_object* this_function = this->gogo_->current_function();
2598 Named_object* closure = this_function->func_value()->closure_var();
2600 Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2601 std::pair<Enclosing_vars::iterator, bool> ins =
2602 this->enclosing_vars_.insert(ev);
2605 // This is a variable we have not seen before. Add a new field
2606 // to the closure type.
2607 this_function->func_value()->add_closure_field(var, location);
2610 Expression* closure_ref = Expression::make_var_reference(closure,
2612 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2614 // The closure structure holds pointers to the variables, so we need
2615 // to introduce an indirection.
2616 Expression* e = Expression::make_field_reference(closure_ref,
2619 e = Expression::make_unary(OPERATOR_MULT, e, location);
2623 // CompositeLit = LiteralType LiteralValue .
2624 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2625 // SliceType | MapType | TypeName .
2626 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2627 // ElementList = Element { "," Element } .
2628 // Element = [ Key ":" ] Value .
2629 // Key = FieldName | ElementIndex .
2630 // FieldName = identifier .
2631 // ElementIndex = Expression .
2632 // Value = Expression | LiteralValue .
2634 // We have already seen the type if there is one, and we are now
2635 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2636 // will be seen here as an array type whose length is "nil". The
2637 // DEPTH parameter is non-zero if this is an embedded composite
2638 // literal and the type was omitted. It gives the number of steps up
2639 // to the type which was provided. E.g., in [][]int{{1}} it will be
2640 // 1. In [][][]int{{{1}}} it will be 2.
2643 Parse::composite_lit(Type* type, int depth, Location location)
2645 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2646 this->advance_token();
2648 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2650 this->advance_token();
2651 return Expression::make_composite_literal(type, depth, false, NULL,
2655 bool has_keys = false;
2656 Expression_list* vals = new Expression_list;
2660 bool is_type_omitted = false;
2662 const Token* token = this->peek_token();
2664 if (token->is_identifier())
2666 std::string identifier = token->identifier();
2667 bool is_exported = token->is_identifier_exported();
2668 Location location = token->location();
2670 if (this->advance_token()->is_op(OPERATOR_COLON))
2672 // This may be a field name. We don't know for sure--it
2673 // could also be an expression for an array index. We
2674 // don't want to parse it as an expression because may
2675 // trigger various errors, e.g., if this identifier
2676 // happens to be the name of a package.
2677 Gogo* gogo = this->gogo_;
2678 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2684 this->unget_token(Token::make_identifier_token(identifier,
2687 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2690 else if (!token->is_op(OPERATOR_LCURLY))
2691 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2694 // This must be a composite literal inside another composite
2695 // literal, with the type omitted for the inner one.
2696 val = this->composite_lit(type, depth + 1, token->location());
2697 is_type_omitted = true;
2700 token = this->peek_token();
2701 if (!token->is_op(OPERATOR_COLON))
2704 vals->push_back(NULL);
2708 if (is_type_omitted && !val->is_error_expression())
2710 error_at(this->location(), "unexpected %<:%>");
2711 val = Expression::make_error(this->location());
2714 this->advance_token();
2716 if (!has_keys && !vals->empty())
2718 Expression_list* newvals = new Expression_list;
2719 for (Expression_list::const_iterator p = vals->begin();
2723 newvals->push_back(NULL);
2724 newvals->push_back(*p);
2731 if (val->unknown_expression() != NULL)
2732 val->unknown_expression()->set_is_composite_literal_key();
2734 vals->push_back(val);
2736 if (!token->is_op(OPERATOR_LCURLY))
2737 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2740 // This must be a composite literal inside another
2741 // composite literal, with the type omitted for the
2743 val = this->composite_lit(type, depth + 1, token->location());
2746 token = this->peek_token();
2749 vals->push_back(val);
2751 if (token->is_op(OPERATOR_COMMA))
2753 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2755 this->advance_token();
2759 else if (token->is_op(OPERATOR_RCURLY))
2761 this->advance_token();
2766 if (token->is_op(OPERATOR_SEMICOLON))
2767 error_at(this->location(),
2768 "need trailing comma before newline in composite literal");
2770 error_at(this->location(), "expected %<,%> or %<}%>");
2772 this->gogo_->mark_locals_used();
2774 while (!token->is_eof()
2775 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2777 if (token->is_op(OPERATOR_LCURLY))
2779 else if (token->is_op(OPERATOR_RCURLY))
2781 token = this->advance_token();
2783 if (token->is_op(OPERATOR_RCURLY))
2784 this->advance_token();
2786 return Expression::make_error(location);
2790 return Expression::make_composite_literal(type, depth, has_keys, vals,
2794 // FunctionLit = "func" Signature Block .
2797 Parse::function_lit()
2799 Location location = this->location();
2800 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2801 this->advance_token();
2803 Enclosing_vars hold_enclosing_vars;
2804 hold_enclosing_vars.swap(this->enclosing_vars_);
2806 Function_type* type = this->signature(NULL, location);
2807 bool fntype_is_error = false;
2810 type = Type::make_function_type(NULL, NULL, NULL, location);
2811 fntype_is_error = true;
2814 // For a function literal, the next token must be a '{'. If we
2815 // don't see that, then we may have a type expression.
2816 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2817 return Expression::make_type(type, location);
2819 bool hold_is_erroneous_function = this->is_erroneous_function_;
2820 if (fntype_is_error)
2821 this->is_erroneous_function_ = true;
2823 Bc_stack* hold_break_stack = this->break_stack_;
2824 Bc_stack* hold_continue_stack = this->continue_stack_;
2825 this->break_stack_ = NULL;
2826 this->continue_stack_ = NULL;
2828 Named_object* no = this->gogo_->start_function("", type, true, location);
2830 Location end_loc = this->block();
2832 this->gogo_->finish_function(end_loc);
2834 if (this->break_stack_ != NULL)
2835 delete this->break_stack_;
2836 if (this->continue_stack_ != NULL)
2837 delete this->continue_stack_;
2838 this->break_stack_ = hold_break_stack;
2839 this->continue_stack_ = hold_continue_stack;
2841 this->is_erroneous_function_ = hold_is_erroneous_function;
2843 hold_enclosing_vars.swap(this->enclosing_vars_);
2845 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2848 return Expression::make_func_reference(no, closure, location);
2851 // Create a closure for the nested function FUNCTION. This is based
2852 // on ENCLOSING_VARS, which is a list of all variables defined in
2853 // enclosing functions and referenced from FUNCTION. A closure is the
2854 // address of a struct which contains the addresses of all the
2855 // referenced variables. This returns NULL if no closure is required.
2858 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2861 if (enclosing_vars->empty())
2864 // Get the variables in order by their field index.
2866 size_t enclosing_var_count = enclosing_vars->size();
2867 std::vector<Enclosing_var> ev(enclosing_var_count);
2868 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2869 p != enclosing_vars->end();
2871 ev[p->index()] = *p;
2873 // Build an initializer for a composite literal of the closure's
2876 Named_object* enclosing_function = this->gogo_->current_function();
2877 Expression_list* initializer = new Expression_list;
2878 for (size_t i = 0; i < enclosing_var_count; ++i)
2880 go_assert(ev[i].index() == i);
2881 Named_object* var = ev[i].var();
2883 if (ev[i].in_function() == enclosing_function)
2884 ref = Expression::make_var_reference(var, location);
2886 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2888 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2890 initializer->push_back(refaddr);
2893 Named_object* closure_var = function->func_value()->closure_var();
2894 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2895 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2897 return Expression::make_heap_composite(cv, location);
2900 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2902 // If MAY_BE_SINK is true, this expression may be "_".
2904 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2907 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2908 // guard (var := expr.("type") using the literal keyword "type").
2911 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2912 bool* is_type_switch)
2914 Location start_loc = this->location();
2915 bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2917 Expression* ret = this->operand(may_be_sink);
2919 // An unknown name followed by a curly brace must be a composite
2920 // literal, and the unknown name must be a type.
2921 if (may_be_composite_lit
2922 && !is_parenthesized
2923 && ret->unknown_expression() != NULL
2924 && this->peek_token()->is_op(OPERATOR_LCURLY))
2926 Named_object* no = ret->unknown_expression()->named_object();
2927 Type* type = Type::make_forward_declaration(no);
2928 ret = Expression::make_type(type, ret->location());
2931 // We handle composite literals and type casts here, as it is the
2932 // easiest way to handle types which are in parentheses, as in
2934 if (ret->is_type_expression())
2936 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2938 if (!may_be_composite_lit)
2940 Type* t = ret->type();
2941 if (t->named_type() != NULL
2942 || t->forward_declaration_type() != NULL)
2944 _("parentheses required around this composite literal"
2945 "to avoid parsing ambiguity"));
2947 else if (is_parenthesized)
2949 "cannot parenthesize type in composite literal");
2950 ret = this->composite_lit(ret->type(), 0, ret->location());
2952 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2954 Location loc = this->location();
2955 this->advance_token();
2956 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2958 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2960 error_at(this->location(),
2961 "invalid use of %<...%> in type conversion");
2962 this->advance_token();
2964 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2965 error_at(this->location(), "expected %<)%>");
2967 this->advance_token();
2968 if (expr->is_error_expression())
2972 Type* t = ret->type();
2973 if (t->classification() == Type::TYPE_ARRAY
2974 && t->array_type()->length() != NULL
2975 && t->array_type()->length()->is_nil_expression())
2977 error_at(ret->location(),
2978 "invalid use of %<...%> in type conversion");
2979 ret = Expression::make_error(loc);
2982 ret = Expression::make_cast(t, expr, loc);
2989 const Token* token = this->peek_token();
2990 if (token->is_op(OPERATOR_LPAREN))
2991 ret = this->call(this->verify_not_sink(ret));
2992 else if (token->is_op(OPERATOR_DOT))
2994 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2995 if (is_type_switch != NULL && *is_type_switch)
2998 else if (token->is_op(OPERATOR_LSQUARE))
2999 ret = this->index(this->verify_not_sink(ret));
3007 // Selector = "." identifier .
3008 // TypeGuard = "." "(" QualifiedIdent ")" .
3010 // Note that Operand can expand to QualifiedIdent, which contains a
3011 // ".". That is handled directly in operand when it sees a package
3014 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3015 // guard (var := expr.("type") using the literal keyword "type").
3018 Parse::selector(Expression* left, bool* is_type_switch)
3020 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
3021 Location location = this->location();
3023 const Token* token = this->advance_token();
3024 if (token->is_identifier())
3026 // This could be a field in a struct, or a method in an
3027 // interface, or a method associated with a type. We can't know
3028 // which until we have seen all the types.
3030 this->gogo_->pack_hidden_name(token->identifier(),
3031 token->is_identifier_exported());
3032 if (token->identifier() == "_")
3034 error_at(this->location(), "invalid use of %<_%>");
3035 name = this->gogo_->pack_hidden_name("blank", false);
3037 this->advance_token();
3038 return Expression::make_selector(left, name, location);
3040 else if (token->is_op(OPERATOR_LPAREN))
3042 this->advance_token();
3044 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
3045 type = this->type();
3048 if (is_type_switch != NULL)
3049 *is_type_switch = true;
3052 error_at(this->location(),
3053 "use of %<.(type)%> outside type switch");
3054 type = Type::make_error_type();
3056 this->advance_token();
3058 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3059 error_at(this->location(), "missing %<)%>");
3061 this->advance_token();
3062 if (is_type_switch != NULL && *is_type_switch)
3064 return Expression::make_type_guard(left, type, location);
3068 error_at(this->location(), "expected identifier or %<(%>");
3073 // Index = "[" Expression "]" .
3074 // Slice = "[" Expression ":" [ Expression ] "]" .
3077 Parse::index(Expression* expr)
3079 Location location = this->location();
3080 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
3081 this->advance_token();
3084 if (!this->peek_token()->is_op(OPERATOR_COLON))
3085 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3089 mpz_init_set_ui(zero, 0);
3090 start = Expression::make_integer(&zero, NULL, location);
3094 Expression* end = NULL;
3095 if (this->peek_token()->is_op(OPERATOR_COLON))
3097 // We use nil to indicate a missing high expression.
3098 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3099 end = Expression::make_nil(this->location());
3101 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
3103 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
3104 error_at(this->location(), "missing %<]%>");
3106 this->advance_token();
3107 return Expression::make_index(expr, start, end, location);
3110 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3111 // ArgumentList = ExpressionList [ "..." ] .
3114 Parse::call(Expression* func)
3116 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
3117 Expression_list* args = NULL;
3118 bool is_varargs = false;
3119 const Token* token = this->advance_token();
3120 if (!token->is_op(OPERATOR_RPAREN))
3122 args = this->expression_list(NULL, false, true);
3123 token = this->peek_token();
3124 if (token->is_op(OPERATOR_ELLIPSIS))
3127 token = this->advance_token();
3130 if (token->is_op(OPERATOR_COMMA))
3131 token = this->advance_token();
3132 if (!token->is_op(OPERATOR_RPAREN))
3133 error_at(this->location(), "missing %<)%>");
3135 this->advance_token();
3136 if (func->is_error_expression())
3138 return Expression::make_call(func, args, is_varargs, func->location());
3141 // Return an expression for a single unqualified identifier.
3144 Parse::id_to_expression(const std::string& name, Location location)
3146 Named_object* in_function;
3147 Named_object* named_object = this->gogo_->lookup(name, &in_function);
3148 if (named_object == NULL)
3149 named_object = this->gogo_->add_unknown_name(name, location);
3151 if (in_function != NULL
3152 && in_function != this->gogo_->current_function()
3153 && (named_object->is_variable() || named_object->is_result_variable()))
3154 return this->enclosing_var_reference(in_function, named_object,
3157 switch (named_object->classification())
3159 case Named_object::NAMED_OBJECT_CONST:
3160 return Expression::make_const_reference(named_object, location);
3161 case Named_object::NAMED_OBJECT_VAR:
3162 case Named_object::NAMED_OBJECT_RESULT_VAR:
3163 this->mark_var_used(named_object);
3164 return Expression::make_var_reference(named_object, location);
3165 case Named_object::NAMED_OBJECT_SINK:
3166 return Expression::make_sink(location);
3167 case Named_object::NAMED_OBJECT_FUNC:
3168 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3169 return Expression::make_func_reference(named_object, NULL, location);
3170 case Named_object::NAMED_OBJECT_UNKNOWN:
3172 Unknown_expression* ue =
3173 Expression::make_unknown_reference(named_object, location);
3174 if (this->is_erroneous_function_)
3175 ue->set_no_error_message();
3178 case Named_object::NAMED_OBJECT_PACKAGE:
3179 case Named_object::NAMED_OBJECT_TYPE:
3180 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3182 // These cases can arise for a field name in a composite
3184 Unknown_expression* ue =
3185 Expression::make_unknown_reference(named_object, location);
3186 if (this->is_erroneous_function_)
3187 ue->set_no_error_message();
3190 case Named_object::NAMED_OBJECT_ERRONEOUS:
3191 return Expression::make_error(location);
3193 error_at(this->location(), "unexpected type of identifier");
3194 return Expression::make_error(location);
3198 // Expression = UnaryExpr { binary_op Expression } .
3200 // PRECEDENCE is the precedence of the current operator.
3202 // If MAY_BE_SINK is true, this expression may be "_".
3204 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3207 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3208 // guard (var := expr.("type") using the literal keyword "type").
3211 Parse::expression(Precedence precedence, bool may_be_sink,
3212 bool may_be_composite_lit, bool* is_type_switch)
3214 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3219 if (is_type_switch != NULL && *is_type_switch)
3222 const Token* token = this->peek_token();
3223 if (token->classification() != Token::TOKEN_OPERATOR)
3229 Precedence right_precedence;
3230 switch (token->op())
3233 right_precedence = PRECEDENCE_OROR;
3235 case OPERATOR_ANDAND:
3236 right_precedence = PRECEDENCE_ANDAND;
3239 case OPERATOR_NOTEQ:
3244 right_precedence = PRECEDENCE_RELOP;
3247 case OPERATOR_MINUS:
3250 right_precedence = PRECEDENCE_ADDOP;
3255 case OPERATOR_LSHIFT:
3256 case OPERATOR_RSHIFT:
3258 case OPERATOR_BITCLEAR:
3259 right_precedence = PRECEDENCE_MULOP;
3262 right_precedence = PRECEDENCE_INVALID;
3266 if (right_precedence == PRECEDENCE_INVALID)
3272 Operator op = token->op();
3273 Location binop_location = token->location();
3275 if (precedence >= right_precedence)
3277 // We've already seen A * B, and we see + C. We want to
3278 // return so that A * B becomes a group.
3282 this->advance_token();
3284 left = this->verify_not_sink(left);
3285 Expression* right = this->expression(right_precedence, false,
3286 may_be_composite_lit,
3288 left = Expression::make_binary(op, left, right, binop_location);
3293 Parse::expression_may_start_here()
3295 const Token* token = this->peek_token();
3296 switch (token->classification())
3298 case Token::TOKEN_INVALID:
3299 case Token::TOKEN_EOF:
3301 case Token::TOKEN_KEYWORD:
3302 switch (token->keyword())
3307 case KEYWORD_STRUCT:
3308 case KEYWORD_INTERFACE:
3313 case Token::TOKEN_IDENTIFIER:
3315 case Token::TOKEN_STRING:
3317 case Token::TOKEN_OPERATOR:
3318 switch (token->op())
3321 case OPERATOR_MINUS:
3325 case OPERATOR_CHANOP:
3327 case OPERATOR_LPAREN:
3328 case OPERATOR_LSQUARE:
3333 case Token::TOKEN_CHARACTER:
3334 case Token::TOKEN_INTEGER:
3335 case Token::TOKEN_FLOAT:
3336 case Token::TOKEN_IMAGINARY:
3343 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3345 // If MAY_BE_SINK is true, this expression may be "_".
3347 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3350 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3351 // guard (var := expr.("type") using the literal keyword "type").
3354 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3355 bool* is_type_switch)
3357 const Token* token = this->peek_token();
3359 // There is a complex parse for <- chan. The choices are
3360 // Convert x to type <- chan int:
3362 // Receive from (x converted to type chan <- chan int):
3363 // (<- chan <- chan int (x))
3364 // Convert x to type <- chan (<- chan int).
3365 // (<- chan <- chan int)(x)
3366 if (token->is_op(OPERATOR_CHANOP))
3368 Location location = token->location();
3369 if (this->advance_token()->is_keyword(KEYWORD_CHAN))
3371 Expression* expr = this->primary_expr(false, may_be_composite_lit,
3373 if (expr->is_error_expression())
3375 else if (!expr->is_type_expression())
3376 return Expression::make_receive(expr, location);
3379 if (expr->type()->is_error_type())
3382 // We picked up "chan TYPE", but it is not a type
3384 Channel_type* ct = expr->type()->channel_type();
3387 // This is probably impossible.
3388 error_at(location, "expected channel type");
3389 return Expression::make_error(location);
3391 else if (ct->may_receive())
3394 Type* t = Type::make_channel_type(false, true,
3395 ct->element_type());
3396 return Expression::make_type(t, location);