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),
50 continue_stack_(NULL),
57 // Return the current token.
62 if (this->unget_token_valid_)
63 return &this->unget_token_;
64 if (this->token_.is_invalid())
65 this->token_ = this->lex_->next_token();
69 // Advance to the next token and return it.
72 Parse::advance_token()
74 if (this->unget_token_valid_)
76 this->unget_token_valid_ = false;
77 if (!this->token_.is_invalid())
80 this->token_ = this->lex_->next_token();
84 // Push a token back on the input stream.
87 Parse::unget_token(const Token& token)
89 go_assert(!this->unget_token_valid_);
90 this->unget_token_ = token;
91 this->unget_token_valid_ = true;
94 // The location of the current token.
99 return this->peek_token()->location();
102 // IdentifierList = identifier { "," identifier } .
105 Parse::identifier_list(Typed_identifier_list* til)
107 const Token* token = this->peek_token();
110 if (!token->is_identifier())
112 error_at(this->location(), "expected identifier");
116 this->gogo_->pack_hidden_name(token->identifier(),
117 token->is_identifier_exported());
118 til->push_back(Typed_identifier(name, NULL, token->location()));
119 token = this->advance_token();
120 if (!token->is_op(OPERATOR_COMMA))
122 token = this->advance_token();
126 // ExpressionList = Expression { "," Expression } .
128 // If MAY_BE_SINK is true, the expressions in the list may be "_".
131 Parse::expression_list(Expression* first, bool may_be_sink)
133 Expression_list* ret = new Expression_list();
135 ret->push_back(first);
138 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink, true,
141 const Token* token = this->peek_token();
142 if (!token->is_op(OPERATOR_COMMA))
145 // Most expression lists permit a trailing comma.
146 Location location = token->location();
147 this->advance_token();
148 if (!this->expression_may_start_here())
150 this->unget_token(Token::make_operator_token(OPERATOR_COMMA,
157 // QualifiedIdent = [ PackageName "." ] identifier .
158 // PackageName = identifier .
160 // This sets *PNAME to the identifier and sets *PPACKAGE to the
161 // package or NULL if there isn't one. This returns true on success,
162 // false on failure in which case it will have emitted an error
166 Parse::qualified_ident(std::string* pname, Named_object** ppackage)
168 const Token* token = this->peek_token();
169 if (!token->is_identifier())
171 error_at(this->location(), "expected identifier");
175 std::string name = token->identifier();
176 bool is_exported = token->is_identifier_exported();
177 name = this->gogo_->pack_hidden_name(name, is_exported);
179 token = this->advance_token();
180 if (!token->is_op(OPERATOR_DOT))
187 Named_object* package = this->gogo_->lookup(name, NULL);
188 if (package == NULL || !package->is_package())
190 error_at(this->location(), "expected package");
191 // We expect . IDENTIFIER; skip both.
192 if (this->advance_token()->is_identifier())
193 this->advance_token();
197 package->package_value()->set_used();
199 token = this->advance_token();
200 if (!token->is_identifier())
202 error_at(this->location(), "expected identifier");
206 name = token->identifier();
210 error_at(this->location(), "invalid use of %<_%>");
214 if (package->name() == this->gogo_->package_name())
215 name = this->gogo_->pack_hidden_name(name,
216 token->is_identifier_exported());
221 this->advance_token();
226 // Type = TypeName | TypeLit | "(" Type ")" .
228 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
229 // SliceType | MapType | ChannelType .
234 const Token* token = this->peek_token();
235 if (token->is_identifier())
236 return this->type_name(true);
237 else if (token->is_op(OPERATOR_LSQUARE))
238 return this->array_type(false);
239 else if (token->is_keyword(KEYWORD_CHAN)
240 || token->is_op(OPERATOR_CHANOP))
241 return this->channel_type();
242 else if (token->is_keyword(KEYWORD_INTERFACE))
243 return this->interface_type();
244 else if (token->is_keyword(KEYWORD_FUNC))
246 Location location = token->location();
247 this->advance_token();
248 Type* type = this->signature(NULL, location);
250 return Type::make_error_type();
253 else if (token->is_keyword(KEYWORD_MAP))
254 return this->map_type();
255 else if (token->is_keyword(KEYWORD_STRUCT))
256 return this->struct_type();
257 else if (token->is_op(OPERATOR_MULT))
258 return this->pointer_type();
259 else if (token->is_op(OPERATOR_LPAREN))
261 this->advance_token();
262 Type* ret = this->type();
263 if (this->peek_token()->is_op(OPERATOR_RPAREN))
264 this->advance_token();
267 if (!ret->is_error_type())
268 error_at(this->location(), "expected %<)%>");
274 error_at(token->location(), "expected type");
275 return Type::make_error_type();
280 Parse::type_may_start_here()
282 const Token* token = this->peek_token();
283 return (token->is_identifier()
284 || token->is_op(OPERATOR_LSQUARE)
285 || token->is_op(OPERATOR_CHANOP)
286 || token->is_keyword(KEYWORD_CHAN)
287 || token->is_keyword(KEYWORD_INTERFACE)
288 || token->is_keyword(KEYWORD_FUNC)
289 || token->is_keyword(KEYWORD_MAP)
290 || token->is_keyword(KEYWORD_STRUCT)
291 || token->is_op(OPERATOR_MULT)
292 || token->is_op(OPERATOR_LPAREN));
295 // TypeName = QualifiedIdent .
297 // If MAY_BE_NIL is true, then an identifier with the value of the
298 // predefined constant nil is accepted, returning the nil type.
301 Parse::type_name(bool issue_error)
303 Location location = this->location();
306 Named_object* package;
307 if (!this->qualified_ident(&name, &package))
308 return Type::make_error_type();
310 Named_object* named_object;
312 named_object = this->gogo_->lookup(name, NULL);
315 named_object = package->package_value()->lookup(name);
316 if (named_object == NULL
318 && package->name() != this->gogo_->package_name())
320 // Check whether the name is there but hidden.
321 std::string s = ('.' + package->package_value()->unique_prefix()
322 + '.' + package->package_value()->name()
324 named_object = package->package_value()->lookup(s);
325 if (named_object != NULL)
327 const std::string& packname(package->package_value()->name());
328 error_at(location, "invalid reference to hidden type %<%s.%s%>",
329 Gogo::message_name(packname).c_str(),
330 Gogo::message_name(name).c_str());
337 if (named_object == NULL)
340 named_object = this->gogo_->add_unknown_name(name, location);
343 const std::string& packname(package->package_value()->name());
344 error_at(location, "reference to undefined identifier %<%s.%s%>",
345 Gogo::message_name(packname).c_str(),
346 Gogo::message_name(name).c_str());
351 else if (named_object->is_type())
353 if (!named_object->type_value()->is_visible())
356 else if (named_object->is_unknown() || named_object->is_type_declaration())
364 error_at(location, "expected type");
365 return Type::make_error_type();
368 if (named_object->is_type())
369 return named_object->type_value();
370 else if (named_object->is_unknown() || named_object->is_type_declaration())
371 return Type::make_forward_declaration(named_object);
376 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
377 // ArrayLength = Expression .
378 // ElementType = CompleteType .
381 Parse::array_type(bool may_use_ellipsis)
383 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
384 const Token* token = this->advance_token();
386 Expression* length = NULL;
387 if (token->is_op(OPERATOR_RSQUARE))
388 this->advance_token();
391 if (!token->is_op(OPERATOR_ELLIPSIS))
392 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
393 else if (may_use_ellipsis)
395 // An ellipsis is used in composite literals to represent a
396 // fixed array of the size of the number of elements. We
397 // use a length of nil to represent this, and change the
398 // length when parsing the composite literal.
399 length = Expression::make_nil(this->location());
400 this->advance_token();
404 error_at(this->location(),
405 "use of %<[...]%> outside of array literal");
406 length = Expression::make_error(this->location());
407 this->advance_token();
409 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
411 error_at(this->location(), "expected %<]%>");
412 return Type::make_error_type();
414 this->advance_token();
417 Type* element_type = this->type();
419 return Type::make_array_type(element_type, length);
422 // MapType = "map" "[" KeyType "]" ValueType .
423 // KeyType = CompleteType .
424 // ValueType = CompleteType .
429 Location location = this->location();
430 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
431 if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
433 error_at(this->location(), "expected %<[%>");
434 return Type::make_error_type();
436 this->advance_token();
438 Type* key_type = this->type();
440 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
442 error_at(this->location(), "expected %<]%>");
443 return Type::make_error_type();
445 this->advance_token();
447 Type* value_type = this->type();
449 if (key_type->is_error_type() || value_type->is_error_type())
450 return Type::make_error_type();
452 return Type::make_map_type(key_type, value_type, location);
455 // StructType = "struct" "{" { FieldDecl ";" } "}" .
460 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
461 Location location = this->location();
462 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
464 Location token_loc = this->location();
465 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
466 && this->advance_token()->is_op(OPERATOR_LCURLY))
467 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
470 error_at(this->location(), "expected %<{%>");
471 return Type::make_error_type();
474 this->advance_token();
476 Struct_field_list* sfl = new Struct_field_list;
477 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
479 this->field_decl(sfl);
480 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
481 this->advance_token();
482 else if (!this->peek_token()->is_op(OPERATOR_RCURLY))
484 error_at(this->location(), "expected %<;%> or %<}%> or newline");
485 if (!this->skip_past_error(OPERATOR_RCURLY))
486 return Type::make_error_type();
489 this->advance_token();
491 for (Struct_field_list::const_iterator pi = sfl->begin();
495 if (pi->type()->is_error_type())
497 for (Struct_field_list::const_iterator pj = pi + 1;
501 if (pi->field_name() == pj->field_name()
502 && !Gogo::is_sink_name(pi->field_name()))
503 error_at(pi->location(), "duplicate field name %<%s%>",
504 Gogo::message_name(pi->field_name()).c_str());
508 return Type::make_struct_type(sfl, location);
511 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
512 // Tag = string_lit .
515 Parse::field_decl(Struct_field_list* sfl)
517 const Token* token = this->peek_token();
518 Location location = token->location();
520 bool is_anonymous_pointer;
521 if (token->is_op(OPERATOR_MULT))
524 is_anonymous_pointer = true;
526 else if (token->is_identifier())
528 std::string id = token->identifier();
529 bool is_id_exported = token->is_identifier_exported();
530 Location id_location = token->location();
531 token = this->advance_token();
532 is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
533 || token->is_op(OPERATOR_RCURLY)
534 || token->is_op(OPERATOR_DOT)
535 || token->is_string());
536 is_anonymous_pointer = false;
537 this->unget_token(Token::make_identifier_token(id, is_id_exported,
542 error_at(this->location(), "expected field name");
543 this->gogo_->mark_locals_used();
544 while (!token->is_op(OPERATOR_SEMICOLON)
545 && !token->is_op(OPERATOR_RCURLY)
547 token = this->advance_token();
553 if (is_anonymous_pointer)
555 this->advance_token();
556 if (!this->peek_token()->is_identifier())
558 error_at(this->location(), "expected field name");
559 this->gogo_->mark_locals_used();
560 while (!token->is_op(OPERATOR_SEMICOLON)
561 && !token->is_op(OPERATOR_RCURLY)
563 token = this->advance_token();
567 Type* type = this->type_name(true);
570 if (this->peek_token()->is_string())
572 tag = this->peek_token()->string_value();
573 this->advance_token();
576 if (!type->is_error_type())
578 if (is_anonymous_pointer)
579 type = Type::make_pointer_type(type);
580 sfl->push_back(Struct_field(Typed_identifier("", type, location)));
582 sfl->back().set_tag(tag);
587 Typed_identifier_list til;
590 token = this->peek_token();
591 if (!token->is_identifier())
593 error_at(this->location(), "expected identifier");
597 this->gogo_->pack_hidden_name(token->identifier(),
598 token->is_identifier_exported());
599 til.push_back(Typed_identifier(name, NULL, token->location()));
600 if (!this->advance_token()->is_op(OPERATOR_COMMA))
602 this->advance_token();
605 Type* type = this->type();
608 if (this->peek_token()->is_string())
610 tag = this->peek_token()->string_value();
611 this->advance_token();
614 for (Typed_identifier_list::iterator p = til.begin();
619 sfl->push_back(Struct_field(*p));
621 sfl->back().set_tag(tag);
626 // PointerType = "*" Type .
629 Parse::pointer_type()
631 go_assert(this->peek_token()->is_op(OPERATOR_MULT));
632 this->advance_token();
633 Type* type = this->type();
634 if (type->is_error_type())
636 return Type::make_pointer_type(type);
639 // ChannelType = Channel | SendChannel | RecvChannel .
640 // Channel = "chan" ElementType .
641 // SendChannel = "chan" "<-" ElementType .
642 // RecvChannel = "<-" "chan" ElementType .
645 Parse::channel_type()
647 const Token* token = this->peek_token();
650 if (token->is_op(OPERATOR_CHANOP))
652 if (!this->advance_token()->is_keyword(KEYWORD_CHAN))
654 error_at(this->location(), "expected %<chan%>");
655 return Type::make_error_type();
658 this->advance_token();
662 go_assert(token->is_keyword(KEYWORD_CHAN));
663 if (this->advance_token()->is_op(OPERATOR_CHANOP))
666 this->advance_token();
670 // Better error messages for the common error of omitting the
671 // channel element type.
672 if (!this->type_may_start_here())
674 token = this->peek_token();
675 if (token->is_op(OPERATOR_RCURLY))
676 error_at(this->location(), "unexpected %<}%> in channel type");
677 else if (token->is_op(OPERATOR_RPAREN))
678 error_at(this->location(), "unexpected %<)%> in channel type");
679 else if (token->is_op(OPERATOR_COMMA))
680 error_at(this->location(), "unexpected comma in channel type");
682 error_at(this->location(), "expected channel element type");
683 return Type::make_error_type();
686 Type* element_type = this->type();
687 return Type::make_channel_type(send, receive, element_type);
690 // Give an error for a duplicate parameter or receiver name.
693 Parse::check_signature_names(const Typed_identifier_list* params,
696 for (Typed_identifier_list::const_iterator p = params->begin();
700 if (p->name().empty() || Gogo::is_sink_name(p->name()))
702 std::pair<std::string, const Typed_identifier*> val =
703 std::make_pair(p->name(), &*p);
704 std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
707 error_at(p->location(), "redefinition of %qs",
708 Gogo::message_name(p->name()).c_str());
709 inform(ins.first->second->location(),
710 "previous definition of %qs was here",
711 Gogo::message_name(p->name()).c_str());
716 // Signature = Parameters [ Result ] .
718 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
719 // location of the start of the type.
721 // This returns NULL on a parse error.
724 Parse::signature(Typed_identifier* receiver, Location location)
726 bool is_varargs = false;
727 Typed_identifier_list* params;
728 bool params_ok = this->parameters(¶ms, &is_varargs);
730 Typed_identifier_list* results = NULL;
731 if (this->peek_token()->is_op(OPERATOR_LPAREN)
732 || this->type_may_start_here())
734 if (!this->result(&results))
743 this->check_signature_names(params, &names);
745 this->check_signature_names(results, &names);
747 Function_type* ret = Type::make_function_type(receiver, params, results,
750 ret->set_is_varargs();
754 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
756 // This returns false on a parse error.
759 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
763 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
765 error_at(this->location(), "expected %<(%>");
769 Typed_identifier_list* params = NULL;
770 bool saw_error = false;
772 const Token* token = this->advance_token();
773 if (!token->is_op(OPERATOR_RPAREN))
775 params = this->parameter_list(is_varargs);
778 token = this->peek_token();
781 // The optional trailing comma is picked up in parameter_list.
783 if (!token->is_op(OPERATOR_RPAREN))
784 error_at(this->location(), "expected %<)%>");
786 this->advance_token();
795 // ParameterList = ParameterDecl { "," ParameterDecl } .
797 // This sets *IS_VARARGS if the list ends with an ellipsis.
798 // IS_VARARGS will be NULL if varargs are not permitted.
800 // We pick up an optional trailing comma.
802 // This returns NULL if some error is seen.
804 Typed_identifier_list*
805 Parse::parameter_list(bool* is_varargs)
807 Location location = this->location();
808 Typed_identifier_list* ret = new Typed_identifier_list();
810 bool saw_error = false;
812 // If we see an identifier and then a comma, then we don't know
813 // whether we are looking at a list of identifiers followed by a
814 // type, or a list of types given by name. We have to do an
815 // arbitrary lookahead to figure it out.
817 bool parameters_have_names;
818 const Token* token = this->peek_token();
819 if (!token->is_identifier())
821 // This must be a type which starts with something like '*'.
822 parameters_have_names = false;
826 std::string name = token->identifier();
827 bool is_exported = token->is_identifier_exported();
828 Location location = token->location();
829 token = this->advance_token();
830 if (!token->is_op(OPERATOR_COMMA))
832 if (token->is_op(OPERATOR_DOT))
834 // This is a qualified identifier, which must turn out
836 parameters_have_names = false;
838 else if (token->is_op(OPERATOR_RPAREN))
840 // A single identifier followed by a parenthesis must be
842 parameters_have_names = false;
846 // An identifier followed by something other than a
847 // comma or a dot or a right parenthesis must be a
848 // parameter name followed by a type.
849 parameters_have_names = true;
852 this->unget_token(Token::make_identifier_token(name, is_exported,
857 // An identifier followed by a comma may be the first in a
858 // list of parameter names followed by a type, or it may be
859 // the first in a list of types without parameter names. To
860 // find out we gather as many identifiers separated by
862 std::string id_name = this->gogo_->pack_hidden_name(name,
864 ret->push_back(Typed_identifier(id_name, NULL, location));
865 bool just_saw_comma = true;
866 while (this->advance_token()->is_identifier())
868 name = this->peek_token()->identifier();
869 is_exported = this->peek_token()->is_identifier_exported();
870 location = this->peek_token()->location();
871 id_name = this->gogo_->pack_hidden_name(name, is_exported);
872 ret->push_back(Typed_identifier(id_name, NULL, location));
873 if (!this->advance_token()->is_op(OPERATOR_COMMA))
875 just_saw_comma = false;
882 // We saw ID1 "," ID2 "," followed by something which
883 // was not an identifier. We must be seeing the start
884 // of a type, and ID1 and ID2 must be types, and the
885 // parameters don't have names.
886 parameters_have_names = false;
888 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
890 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
891 // and the parameters don't have names.
892 parameters_have_names = false;
894 else if (this->peek_token()->is_op(OPERATOR_DOT))
896 // We saw ID1 "," ID2 ".". ID2 must be a package name,
897 // ID1 must be a type, and the parameters don't have
899 parameters_have_names = false;
900 this->unget_token(Token::make_identifier_token(name, is_exported,
903 just_saw_comma = true;
907 // We saw ID1 "," ID2 followed by something other than
908 // ",", ".", or ")". We must be looking at the start of
909 // a type, and ID1 and ID2 must be parameter names.
910 parameters_have_names = true;
913 if (parameters_have_names)
915 go_assert(!just_saw_comma);
916 // We have just seen ID1, ID2 xxx.
918 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
922 error_at(this->location(), "%<...%> only permits one name");
924 this->advance_token();
927 for (size_t i = 0; i < ret->size(); ++i)
928 ret->set_type(i, type);
929 if (!this->peek_token()->is_op(OPERATOR_COMMA))
930 return saw_error ? NULL : ret;
931 if (this->advance_token()->is_op(OPERATOR_RPAREN))
932 return saw_error ? NULL : ret;
936 Typed_identifier_list* tret = new Typed_identifier_list();
937 for (Typed_identifier_list::const_iterator p = ret->begin();
941 Named_object* no = this->gogo_->lookup(p->name(), NULL);
944 no = this->gogo_->add_unknown_name(p->name(),
948 type = no->type_value();
949 else if (no->is_unknown() || no->is_type_declaration())
950 type = Type::make_forward_declaration(no);
953 error_at(p->location(), "expected %<%s%> to be a type",
954 Gogo::message_name(p->name()).c_str());
956 type = Type::make_error_type();
958 tret->push_back(Typed_identifier("", type, p->location()));
963 || this->peek_token()->is_op(OPERATOR_RPAREN))
964 return saw_error ? NULL : ret;
969 bool mix_error = false;
970 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
971 while (this->peek_token()->is_op(OPERATOR_COMMA))
973 if (is_varargs != NULL && *is_varargs)
975 error_at(this->location(), "%<...%> must be last parameter");
978 if (this->advance_token()->is_op(OPERATOR_RPAREN))
980 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
984 error_at(location, "invalid named/anonymous mix");
995 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
998 Parse::parameter_decl(bool parameters_have_names,
999 Typed_identifier_list* til,
1003 if (!parameters_have_names)
1006 Location location = this->location();
1007 if (!this->peek_token()->is_identifier())
1009 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1010 type = this->type();
1013 if (is_varargs == NULL)
1014 error_at(this->location(), "invalid use of %<...%>");
1017 this->advance_token();
1018 if (is_varargs == NULL
1019 && this->peek_token()->is_op(OPERATOR_RPAREN))
1020 type = Type::make_error_type();
1023 Type* element_type = this->type();
1024 type = Type::make_array_type(element_type, NULL);
1030 type = this->type_name(false);
1031 if (type->is_error_type()
1032 || (!this->peek_token()->is_op(OPERATOR_COMMA)
1033 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1036 while (!this->peek_token()->is_op(OPERATOR_COMMA)
1037 && !this->peek_token()->is_op(OPERATOR_RPAREN))
1038 this->advance_token();
1041 if (!type->is_error_type())
1042 til->push_back(Typed_identifier("", type, location));
1046 size_t orig_count = til->size();
1047 if (this->peek_token()->is_identifier())
1048 this->identifier_list(til);
1051 size_t new_count = til->size();
1054 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1055 type = this->type();
1058 if (is_varargs == NULL)
1059 error_at(this->location(), "invalid use of %<...%>");
1060 else if (new_count > orig_count + 1)
1061 error_at(this->location(), "%<...%> only permits one name");
1064 this->advance_token();
1065 Type* element_type = this->type();
1066 type = Type::make_array_type(element_type, NULL);
1068 for (size_t i = orig_count; i < new_count; ++i)
1069 til->set_type(i, type);
1073 // Result = Parameters | Type .
1075 // This returns false on a parse error.
1078 Parse::result(Typed_identifier_list** presults)
1080 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1081 return this->parameters(presults, NULL);
1084 Location location = this->location();
1085 Type* type = this->type();
1086 if (type->is_error_type())
1091 Typed_identifier_list* til = new Typed_identifier_list();
1092 til->push_back(Typed_identifier("", type, location));
1098 // Block = "{" [ StatementList ] "}" .
1100 // Returns the location of the closing brace.
1105 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1107 Location loc = this->location();
1108 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1109 && this->advance_token()->is_op(OPERATOR_LCURLY))
1110 error_at(loc, "unexpected semicolon or newline before %<{%>");
1113 error_at(this->location(), "expected %<{%>");
1114 return Linemap::unknown_location();
1118 const Token* token = this->advance_token();
1120 if (!token->is_op(OPERATOR_RCURLY))
1122 this->statement_list();
1123 token = this->peek_token();
1124 if (!token->is_op(OPERATOR_RCURLY))
1126 if (!token->is_eof() || !saw_errors())
1127 error_at(this->location(), "expected %<}%>");
1129 this->gogo_->mark_locals_used();
1131 // Skip ahead to the end of the block, in hopes of avoiding
1132 // lots of meaningless errors.
1133 Location ret = token->location();
1135 while (!token->is_eof())
1137 if (token->is_op(OPERATOR_LCURLY))
1139 else if (token->is_op(OPERATOR_RCURLY))
1144 this->advance_token();
1148 token = this->advance_token();
1149 ret = token->location();
1155 Location ret = token->location();
1156 this->advance_token();
1160 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1161 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1164 Parse::interface_type()
1166 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1167 Location location = this->location();
1169 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1171 Location token_loc = this->location();
1172 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1173 && this->advance_token()->is_op(OPERATOR_LCURLY))
1174 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1177 error_at(this->location(), "expected %<{%>");
1178 return Type::make_error_type();
1181 this->advance_token();
1183 Typed_identifier_list* methods = new Typed_identifier_list();
1184 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1186 this->method_spec(methods);
1187 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1189 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1191 this->method_spec(methods);
1193 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1195 error_at(this->location(), "expected %<}%>");
1196 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1198 if (this->peek_token()->is_eof())
1199 return Type::make_error_type();
1203 this->advance_token();
1205 if (methods->empty())
1211 Interface_type* ret = Type::make_interface_type(methods, location);
1212 this->gogo_->record_interface_type(ret);
1216 // MethodSpec = MethodName Signature | InterfaceTypeName .
1217 // MethodName = identifier .
1218 // InterfaceTypeName = TypeName .
1221 Parse::method_spec(Typed_identifier_list* methods)
1223 const Token* token = this->peek_token();
1224 if (!token->is_identifier())
1226 error_at(this->location(), "expected identifier");
1230 std::string name = token->identifier();
1231 bool is_exported = token->is_identifier_exported();
1232 Location location = token->location();
1234 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1236 // This is a MethodName.
1237 name = this->gogo_->pack_hidden_name(name, is_exported);
1238 Type* type = this->signature(NULL, location);
1241 methods->push_back(Typed_identifier(name, type, location));
1245 this->unget_token(Token::make_identifier_token(name, is_exported,
1247 Type* type = this->type_name(false);
1248 if (type->is_error_type()
1249 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1250 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1252 if (this->peek_token()->is_op(OPERATOR_COMMA))
1253 error_at(this->location(),
1254 "name list not allowed in interface type");
1256 error_at(location, "expected signature or type name");
1257 this->gogo_->mark_locals_used();
1258 token = this->peek_token();
1259 while (!token->is_eof()
1260 && !token->is_op(OPERATOR_SEMICOLON)
1261 && !token->is_op(OPERATOR_RCURLY))
1262 token = this->advance_token();
1265 // This must be an interface type, but we can't check that now.
1266 // We check it and pull out the methods in
1267 // Interface_type::do_verify.
1268 methods->push_back(Typed_identifier("", type, location));
1272 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1275 Parse::declaration()
1277 const Token* token = this->peek_token();
1278 if (token->is_keyword(KEYWORD_CONST))
1280 else if (token->is_keyword(KEYWORD_TYPE))
1282 else if (token->is_keyword(KEYWORD_VAR))
1284 else if (token->is_keyword(KEYWORD_FUNC))
1285 this->function_decl();
1288 error_at(this->location(), "expected declaration");
1289 this->advance_token();
1294 Parse::declaration_may_start_here()
1296 const Token* token = this->peek_token();
1297 return (token->is_keyword(KEYWORD_CONST)
1298 || token->is_keyword(KEYWORD_TYPE)
1299 || token->is_keyword(KEYWORD_VAR)
1300 || token->is_keyword(KEYWORD_FUNC));
1303 // Decl<P> = P | "(" [ List<P> ] ")" .
1306 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1308 if (this->peek_token()->is_eof())
1311 error_at(this->location(), "unexpected end of file");
1315 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1319 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1321 this->list(pfn, varg, true);
1322 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1324 error_at(this->location(), "missing %<)%>");
1325 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1327 if (this->peek_token()->is_eof())
1332 this->advance_token();
1336 // List<P> = P { ";" P } [ ";" ] .
1338 // In order to pick up the trailing semicolon we need to know what
1339 // might follow. This is either a '}' or a ')'.
1342 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1345 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1346 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1347 || this->peek_token()->is_op(OPERATOR_COMMA))
1349 if (this->peek_token()->is_op(OPERATOR_COMMA))
1350 error_at(this->location(), "unexpected comma");
1351 if (this->advance_token()->is_op(follow))
1357 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1362 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1363 this->advance_token();
1366 Type* last_type = NULL;
1367 Expression_list* last_expr_list = NULL;
1369 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1370 this->const_spec(&last_type, &last_expr_list);
1373 this->advance_token();
1374 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1376 this->const_spec(&last_type, &last_expr_list);
1377 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1378 this->advance_token();
1379 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1381 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1382 if (!this->skip_past_error(OPERATOR_RPAREN))
1386 this->advance_token();
1389 if (last_expr_list != NULL)
1390 delete last_expr_list;
1393 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1396 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1398 Typed_identifier_list til;
1399 this->identifier_list(&til);
1402 if (this->type_may_start_here())
1404 type = this->type();
1406 *last_expr_list = NULL;
1409 Expression_list *expr_list;
1410 if (!this->peek_token()->is_op(OPERATOR_EQ))
1412 if (*last_expr_list == NULL)
1414 error_at(this->location(), "expected %<=%>");
1418 expr_list = new Expression_list;
1419 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1420 p != (*last_expr_list)->end();
1422 expr_list->push_back((*p)->copy());
1426 this->advance_token();
1427 expr_list = this->expression_list(NULL, false);
1429 if (*last_expr_list != NULL)
1430 delete *last_expr_list;
1431 *last_expr_list = expr_list;
1434 Expression_list::const_iterator pe = expr_list->begin();
1435 for (Typed_identifier_list::iterator pi = til.begin();
1439 if (pe == expr_list->end())
1441 error_at(this->location(), "not enough initializers");
1447 if (!Gogo::is_sink_name(pi->name()))
1448 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1450 if (pe != expr_list->end())
1451 error_at(this->location(), "too many initializers");
1453 this->increment_iota();
1458 // TypeDecl = "type" Decl<TypeSpec> .
1463 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1464 this->advance_token();
1465 this->decl(&Parse::type_spec, NULL);
1468 // TypeSpec = identifier Type .
1471 Parse::type_spec(void*)
1473 const Token* token = this->peek_token();
1474 if (!token->is_identifier())
1476 error_at(this->location(), "expected identifier");
1479 std::string name = token->identifier();
1480 bool is_exported = token->is_identifier_exported();
1481 Location location = token->location();
1482 token = this->advance_token();
1484 // The scope of the type name starts at the point where the
1485 // identifier appears in the source code. We implement this by
1486 // declaring the type before we read the type definition.
1487 Named_object* named_type = NULL;
1490 name = this->gogo_->pack_hidden_name(name, is_exported);
1491 named_type = this->gogo_->declare_type(name, location);
1495 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1496 type = this->type();
1499 error_at(this->location(),
1500 "unexpected semicolon or newline in type declaration");
1501 type = Type::make_error_type();
1502 this->advance_token();
1505 if (type->is_error_type())
1507 this->gogo_->mark_locals_used();
1508 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1509 && !this->peek_token()->is_eof())
1510 this->advance_token();
1515 if (named_type->is_type_declaration())
1517 Type* ftype = type->forwarded();
1518 if (ftype->forward_declaration_type() != NULL
1519 && (ftype->forward_declaration_type()->named_object()
1522 error_at(location, "invalid recursive type");
1523 type = Type::make_error_type();
1526 this->gogo_->define_type(named_type,
1527 Type::make_named_type(named_type, type,
1529 go_assert(named_type->package() == NULL);
1533 // This will probably give a redefinition error.
1534 this->gogo_->add_type(name, type, location);
1539 // VarDecl = "var" Decl<VarSpec> .
1544 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1545 this->advance_token();
1546 this->decl(&Parse::var_spec, NULL);
1549 // VarSpec = IdentifierList
1550 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1553 Parse::var_spec(void*)
1555 // Get the variable names.
1556 Typed_identifier_list til;
1557 this->identifier_list(&til);
1559 Location location = this->location();
1562 Expression_list* init = NULL;
1563 if (!this->peek_token()->is_op(OPERATOR_EQ))
1565 type = this->type();
1566 if (type->is_error_type())
1568 this->gogo_->mark_locals_used();
1569 while (!this->peek_token()->is_op(OPERATOR_EQ)
1570 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1571 && !this->peek_token()->is_eof())
1572 this->advance_token();
1574 if (this->peek_token()->is_op(OPERATOR_EQ))
1576 this->advance_token();
1577 init = this->expression_list(NULL, false);
1582 this->advance_token();
1583 init = this->expression_list(NULL, false);
1586 this->init_vars(&til, type, init, false, location);
1592 // Create variables. TIL is a list of variable names. If TYPE is not
1593 // NULL, it is the type of all the variables. If INIT is not NULL, it
1594 // is an initializer list for the variables.
1597 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1598 Expression_list* init, bool is_coloneq,
1601 // Check for an initialization which can yield multiple values.
1602 if (init != NULL && init->size() == 1 && til->size() > 1)
1604 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1607 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1610 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1613 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1614 is_coloneq, location))
1618 if (init != NULL && init->size() != til->size())
1620 if (init->empty() || !init->front()->is_error_expression())
1621 error_at(location, "wrong number of initializations");
1624 type = Type::make_error_type();
1627 // Note that INIT was already parsed with the old name bindings, so
1628 // we don't have to worry that it will accidentally refer to the
1629 // newly declared variables.
1631 Expression_list::const_iterator pexpr;
1633 pexpr = init->begin();
1634 bool any_new = false;
1635 for (Typed_identifier_list::const_iterator p = til->begin();
1640 go_assert(pexpr != init->end());
1641 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1647 go_assert(pexpr == init->end());
1648 if (is_coloneq && !any_new)
1649 error_at(location, "variables redeclared but no variable is new");
1652 // See if we need to initialize a list of variables from a function
1653 // call. This returns true if we have set up the variables and the
1657 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1658 Expression* expr, bool is_coloneq,
1661 Call_expression* call = expr->call_expression();
1665 // This is a function call. We can't check here whether it returns
1666 // the right number of values, but it might. Declare the variables,
1667 // and then assign the results of the call to them.
1669 unsigned int index = 0;
1670 bool any_new = false;
1671 for (Typed_identifier_list::const_iterator pv = vars->begin();
1675 Expression* init = Expression::make_call_result(call, index);
1676 this->init_var(*pv, type, init, is_coloneq, false, &any_new);
1679 if (is_coloneq && !any_new)
1680 error_at(location, "variables redeclared but no variable is new");
1685 // See if we need to initialize a pair of values from a map index
1686 // expression. This returns true if we have set up the variables and
1687 // the initialization.
1690 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1691 Expression* expr, bool is_coloneq,
1694 Index_expression* index = expr->index_expression();
1697 if (vars->size() != 2)
1700 // This is an index which is being assigned to two variables. It
1701 // must be a map index. Declare the variables, and then assign the
1702 // results of the map index.
1703 bool any_new = false;
1704 Typed_identifier_list::const_iterator p = vars->begin();
1705 Expression* init = type == NULL ? index : NULL;
1706 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1707 type == NULL, &any_new);
1708 if (type == NULL && any_new && val_no->is_variable())
1709 val_no->var_value()->set_type_from_init_tuple();
1710 Expression* val_var = Expression::make_var_reference(val_no, location);
1713 Type* var_type = type;
1714 if (var_type == NULL)
1715 var_type = Type::lookup_bool_type();
1716 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1718 Expression* present_var = Expression::make_var_reference(no, location);
1720 if (is_coloneq && !any_new)
1721 error_at(location, "variables redeclared but no variable is new");
1723 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1726 if (!this->gogo_->in_global_scope())
1727 this->gogo_->add_statement(s);
1728 else if (!val_no->is_sink())
1730 if (val_no->is_variable())
1731 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1733 else if (!no->is_sink())
1735 if (no->is_variable())
1736 no->var_value()->add_preinit_statement(this->gogo_, s);
1740 // Execute the map index expression just so that we can fail if
1742 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1744 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1750 // See if we need to initialize a pair of values from a receive
1751 // expression. This returns true if we have set up the variables and
1752 // the initialization.
1755 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1756 Expression* expr, bool is_coloneq,
1759 Receive_expression* receive = expr->receive_expression();
1760 if (receive == NULL)
1762 if (vars->size() != 2)
1765 // This is a receive expression which is being assigned to two
1766 // variables. Declare the variables, and then assign the results of
1768 bool any_new = false;
1769 Typed_identifier_list::const_iterator p = vars->begin();
1770 Expression* init = type == NULL ? receive : NULL;
1771 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1772 type == NULL, &any_new);
1773 if (type == NULL && any_new && val_no->is_variable())
1774 val_no->var_value()->set_type_from_init_tuple();
1775 Expression* val_var = Expression::make_var_reference(val_no, location);
1778 Type* var_type = type;
1779 if (var_type == NULL)
1780 var_type = Type::lookup_bool_type();
1781 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1783 Expression* received_var = Expression::make_var_reference(no, location);
1785 if (is_coloneq && !any_new)
1786 error_at(location, "variables redeclared but no variable is new");
1788 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1793 if (!this->gogo_->in_global_scope())
1794 this->gogo_->add_statement(s);
1795 else if (!val_no->is_sink())
1797 if (val_no->is_variable())
1798 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1800 else if (!no->is_sink())
1802 if (no->is_variable())
1803 no->var_value()->add_preinit_statement(this->gogo_, s);
1807 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1809 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1815 // See if we need to initialize a pair of values from a type guard
1816 // expression. This returns true if we have set up the variables and
1817 // the initialization.
1820 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1821 Type* type, Expression* expr,
1822 bool is_coloneq, Location location)
1824 Type_guard_expression* type_guard = expr->type_guard_expression();
1825 if (type_guard == NULL)
1827 if (vars->size() != 2)
1830 // This is a type guard expression which is being assigned to two
1831 // variables. Declare the variables, and then assign the results of
1833 bool any_new = false;
1834 Typed_identifier_list::const_iterator p = vars->begin();
1835 Type* var_type = type;
1836 if (var_type == NULL)
1837 var_type = type_guard->type();
1838 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1840 Expression* val_var = Expression::make_var_reference(val_no, location);
1844 if (var_type == NULL)
1845 var_type = Type::lookup_bool_type();
1846 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1848 Expression* ok_var = Expression::make_var_reference(no, location);
1850 Expression* texpr = type_guard->expr();
1851 Type* t = type_guard->type();
1852 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1856 if (is_coloneq && !any_new)
1857 error_at(location, "variables redeclared but no variable is new");
1859 if (!this->gogo_->in_global_scope())
1860 this->gogo_->add_statement(s);
1861 else if (!val_no->is_sink())
1863 if (val_no->is_variable())
1864 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1866 else if (!no->is_sink())
1868 if (no->is_variable())
1869 no->var_value()->add_preinit_statement(this->gogo_, s);
1873 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1874 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1880 // Create a single variable. If IS_COLONEQ is true, we permit
1881 // redeclarations in the same block, and we set *IS_NEW when we find a
1882 // new variable which is not a redeclaration.
1885 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1886 bool is_coloneq, bool type_from_init, bool* is_new)
1888 Location location = tid.location();
1890 if (Gogo::is_sink_name(tid.name()))
1892 if (!type_from_init && init != NULL)
1894 if (this->gogo_->in_global_scope())
1895 return this->create_dummy_global(type, init, location);
1896 else if (type == NULL)
1897 this->gogo_->add_statement(Statement::make_statement(init, true));
1900 // With both a type and an initializer, create a dummy
1901 // variable so that we will check whether the
1902 // initializer can be assigned to the type.
1903 Variable* var = new Variable(type, init, false, false, false,
1908 snprintf(buf, sizeof buf, "sink$%d", count);
1910 return this->gogo_->add_variable(buf, var);
1913 return this->gogo_->add_sink();
1918 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1920 && (no->is_variable() || no->is_result_variable()))
1922 // INIT may be NULL even when IS_COLONEQ is true for cases
1923 // like v, ok := x.(int).
1924 if (!type_from_init && init != NULL)
1926 Expression *v = Expression::make_var_reference(no, location);
1927 Statement *s = Statement::make_assignment(v, init, location);
1928 this->gogo_->add_statement(s);
1934 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1935 false, false, location);
1936 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1937 if (!no->is_variable())
1939 // The name is already defined, so we just gave an error.
1940 return this->gogo_->add_sink();
1945 // Create a dummy global variable to force an initializer to be run in
1946 // the right place. This is used when a sink variable is initialized
1950 Parse::create_dummy_global(Type* type, Expression* init,
1953 if (type == NULL && init == NULL)
1954 type = Type::lookup_bool_type();
1955 Variable* var = new Variable(type, init, true, false, false, location);
1958 snprintf(buf, sizeof buf, "_.%d", count);
1960 return this->gogo_->add_variable(buf, var);
1963 // SimpleVarDecl = identifier ":=" Expression .
1965 // We've already seen the identifier.
1967 // FIXME: We also have to implement
1968 // IdentifierList ":=" ExpressionList
1969 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
1970 // tuple assignments here as well.
1972 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
1975 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
1976 // guard (var := expr.("type") using the literal keyword "type").
1979 Parse::simple_var_decl_or_assignment(const std::string& name,
1981 Range_clause* p_range_clause,
1982 Type_switch* p_type_switch)
1984 Typed_identifier_list til;
1985 til.push_back(Typed_identifier(name, NULL, location));
1987 // We've seen one identifier. If we see a comma now, this could be
1989 if (this->peek_token()->is_op(OPERATOR_COMMA))
1991 go_assert(p_type_switch == NULL);
1994 const Token* token = this->advance_token();
1995 if (!token->is_identifier())
1998 std::string id = token->identifier();
1999 bool is_id_exported = token->is_identifier_exported();
2000 Location id_location = token->location();
2002 token = this->advance_token();
2003 if (!token->is_op(OPERATOR_COMMA))
2005 if (token->is_op(OPERATOR_COLONEQ))
2007 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2008 til.push_back(Typed_identifier(id, NULL, location));
2011 this->unget_token(Token::make_identifier_token(id,
2017 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2018 til.push_back(Typed_identifier(id, NULL, location));
2021 // We have a comma separated list of identifiers in TIL. If the
2022 // next token is COLONEQ, then this is a simple var decl, and we
2023 // have the complete list of identifiers. If the next token is
2024 // not COLONEQ, then the only valid parse is a tuple assignment.
2025 // The list of identifiers we have so far is really a list of
2026 // expressions. There are more expressions following.
2028 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2030 Expression_list* exprs = new Expression_list;
2031 for (Typed_identifier_list::const_iterator p = til.begin();
2034 exprs->push_back(this->id_to_expression(p->name(),
2037 Expression_list* more_exprs = this->expression_list(NULL, true);
2038 for (Expression_list::const_iterator p = more_exprs->begin();
2039 p != more_exprs->end();
2041 exprs->push_back(*p);
2044 this->tuple_assignment(exprs, p_range_clause);
2049 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2050 const Token* token = this->advance_token();
2052 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2054 this->range_clause_decl(&til, p_range_clause);
2058 Expression_list* init;
2059 if (p_type_switch == NULL)
2060 init = this->expression_list(NULL, false);
2063 bool is_type_switch = false;
2064 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2068 p_type_switch->found = true;
2069 p_type_switch->name = name;
2070 p_type_switch->location = location;
2071 p_type_switch->expr = expr;
2075 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2077 init = new Expression_list();
2078 init->push_back(expr);
2082 this->advance_token();
2083 init = this->expression_list(expr, false);
2087 this->init_vars(&til, NULL, init, true, location);
2090 // FunctionDecl = "func" identifier Signature [ Block ] .
2091 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2094 // FunctionDecl = "func" identifier Signature
2095 // __asm__ "(" string_lit ")" .
2096 // This extension means a function whose real name is the identifier
2100 Parse::function_decl()
2102 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2103 Location location = this->location();
2104 const Token* token = this->advance_token();
2106 Typed_identifier* rec = NULL;
2107 if (token->is_op(OPERATOR_LPAREN))
2109 rec = this->receiver();
2110 token = this->peek_token();
2113 if (!token->is_identifier())
2115 error_at(this->location(), "expected function name");
2120 this->gogo_->pack_hidden_name(token->identifier(),
2121 token->is_identifier_exported());
2123 this->advance_token();
2125 Function_type* fntype = this->signature(rec, this->location());
2129 Named_object* named_object = NULL;
2131 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2133 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2135 error_at(this->location(), "expected %<(%>");
2138 token = this->advance_token();
2139 if (!token->is_string())
2141 error_at(this->location(), "expected string");
2144 std::string asm_name = token->string_value();
2145 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2147 error_at(this->location(), "expected %<)%>");
2150 this->advance_token();
2151 if (!Gogo::is_sink_name(name))
2153 named_object = this->gogo_->declare_function(name, fntype, location);
2154 if (named_object->is_function_declaration())
2155 named_object->func_declaration_value()->set_asm_name(asm_name);
2159 // Check for the easy error of a newline before the opening brace.
2160 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2162 Location semi_loc = this->location();
2163 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2164 error_at(this->location(),
2165 "unexpected semicolon or newline before %<{%>");
2167 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2171 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2173 if (named_object == NULL && !Gogo::is_sink_name(name))
2174 this->gogo_->declare_function(name, fntype, location);
2178 this->gogo_->start_function(name, fntype, true, location);
2179 Location end_loc = this->block();
2180 this->gogo_->finish_function(end_loc);
2184 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2185 // BaseTypeName = identifier .
2190 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2193 const Token* token = this->advance_token();
2194 Location location = token->location();
2195 if (!token->is_op(OPERATOR_MULT))
2197 if (!token->is_identifier())
2199 error_at(this->location(), "method has no receiver");
2200 this->gogo_->mark_locals_used();
2201 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2202 token = this->advance_token();
2203 if (!token->is_eof())
2204 this->advance_token();
2207 name = token->identifier();
2208 bool is_exported = token->is_identifier_exported();
2209 token = this->advance_token();
2210 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2212 // An identifier followed by something other than a dot or a
2213 // right parenthesis must be a receiver name followed by a
2215 name = this->gogo_->pack_hidden_name(name, is_exported);
2219 // This must be a type name.
2220 this->unget_token(Token::make_identifier_token(name, is_exported,
2222 token = this->peek_token();
2227 // Here the receiver name is in NAME (it is empty if the receiver is
2228 // unnamed) and TOKEN is the first token in the type.
2230 bool is_pointer = false;
2231 if (token->is_op(OPERATOR_MULT))
2234 token = this->advance_token();
2237 if (!token->is_identifier())
2239 error_at(this->location(), "expected receiver name or type");
2240 this->gogo_->mark_locals_used();
2241 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2242 while (!token->is_eof())
2244 token = this->advance_token();
2245 if (token->is_op(OPERATOR_LPAREN))
2247 else if (token->is_op(OPERATOR_RPAREN))
2254 if (!token->is_eof())
2255 this->advance_token();
2259 Type* type = this->type_name(true);
2261 if (is_pointer && !type->is_error_type())
2262 type = Type::make_pointer_type(type);
2264 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2265 this->advance_token();
2268 if (this->peek_token()->is_op(OPERATOR_COMMA))
2269 error_at(this->location(), "method has multiple receivers");
2271 error_at(this->location(), "expected %<)%>");
2272 this->gogo_->mark_locals_used();
2273 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2274 token = this->advance_token();
2275 if (!token->is_eof())
2276 this->advance_token();
2280 return new Typed_identifier(name, type, location);
2283 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2284 // Literal = BasicLit | CompositeLit | FunctionLit .
2285 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2287 // If MAY_BE_SINK is true, this operand may be "_".
2290 Parse::operand(bool may_be_sink)
2292 const Token* token = this->peek_token();
2294 switch (token->classification())
2296 case Token::TOKEN_IDENTIFIER:
2298 Location location = token->location();
2299 std::string id = token->identifier();
2300 bool is_exported = token->is_identifier_exported();
2301 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2303 Named_object* in_function;
2304 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2306 Package* package = NULL;
2307 if (named_object != NULL && named_object->is_package())
2309 if (!this->advance_token()->is_op(OPERATOR_DOT)
2310 || !this->advance_token()->is_identifier())
2312 error_at(location, "unexpected reference to package");
2313 return Expression::make_error(location);
2315 package = named_object->package_value();
2316 package->set_used();
2317 id = this->peek_token()->identifier();
2318 is_exported = this->peek_token()->is_identifier_exported();
2319 packed = this->gogo_->pack_hidden_name(id, is_exported);
2320 named_object = package->lookup(packed);
2321 location = this->location();
2322 go_assert(in_function == NULL);
2325 this->advance_token();
2327 if (named_object != NULL
2328 && named_object->is_type()
2329 && !named_object->type_value()->is_visible())
2331 go_assert(package != NULL);
2332 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2333 Gogo::message_name(package->name()).c_str(),
2334 Gogo::message_name(id).c_str());
2335 return Expression::make_error(location);
2339 if (named_object == NULL)
2341 if (package != NULL)
2343 std::string n1 = Gogo::message_name(package->name());
2344 std::string n2 = Gogo::message_name(id);
2347 ("invalid reference to unexported identifier "
2349 n1.c_str(), n2.c_str());
2352 "reference to undefined identifier %<%s.%s%>",
2353 n1.c_str(), n2.c_str());
2354 return Expression::make_error(location);
2357 named_object = this->gogo_->add_unknown_name(packed, location);
2360 if (in_function != NULL
2361 && in_function != this->gogo_->current_function()
2362 && (named_object->is_variable()
2363 || named_object->is_result_variable()))
2364 return this->enclosing_var_reference(in_function, named_object,
2367 switch (named_object->classification())
2369 case Named_object::NAMED_OBJECT_CONST:
2370 return Expression::make_const_reference(named_object, location);
2371 case Named_object::NAMED_OBJECT_TYPE:
2372 return Expression::make_type(named_object->type_value(), location);
2373 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2375 Type* t = Type::make_forward_declaration(named_object);
2376 return Expression::make_type(t, location);
2378 case Named_object::NAMED_OBJECT_VAR:
2379 case Named_object::NAMED_OBJECT_RESULT_VAR:
2380 this->mark_var_used(named_object);
2381 return Expression::make_var_reference(named_object, location);
2382 case Named_object::NAMED_OBJECT_SINK:
2384 return Expression::make_sink(location);
2387 error_at(location, "cannot use _ as value");
2388 return Expression::make_error(location);
2390 case Named_object::NAMED_OBJECT_FUNC:
2391 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2392 return Expression::make_func_reference(named_object, NULL,
2394 case Named_object::NAMED_OBJECT_UNKNOWN:
2395 return Expression::make_unknown_reference(named_object, location);
2402 case Token::TOKEN_STRING:
2403 ret = Expression::make_string(token->string_value(), token->location());
2404 this->advance_token();
2407 case Token::TOKEN_CHARACTER:
2408 ret = Expression::make_character(token->character_value(), NULL,
2410 this->advance_token();
2413 case Token::TOKEN_INTEGER:
2414 ret = Expression::make_integer(token->integer_value(), NULL,
2416 this->advance_token();
2419 case Token::TOKEN_FLOAT:
2420 ret = Expression::make_float(token->float_value(), NULL,
2422 this->advance_token();
2425 case Token::TOKEN_IMAGINARY:
2428 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2429 ret = Expression::make_complex(&zero, token->imaginary_value(),
2430 NULL, token->location());
2432 this->advance_token();
2436 case Token::TOKEN_KEYWORD:
2437 switch (token->keyword())
2440 return this->function_lit();
2442 case KEYWORD_INTERFACE:
2444 case KEYWORD_STRUCT:
2446 Location location = token->location();
2447 return Expression::make_type(this->type(), location);
2454 case Token::TOKEN_OPERATOR:
2455 if (token->is_op(OPERATOR_LPAREN))
2457 this->advance_token();
2458 ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2459 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2460 error_at(this->location(), "missing %<)%>");
2462 this->advance_token();
2465 else if (token->is_op(OPERATOR_LSQUARE))
2467 // Here we call array_type directly, as this is the only
2468 // case where an ellipsis is permitted for an array type.
2469 Location location = token->location();
2470 return Expression::make_type(this->array_type(true), location);
2478 error_at(this->location(), "expected operand");
2479 return Expression::make_error(this->location());
2482 // Handle a reference to a variable in an enclosing function. We add
2483 // it to a list of such variables. We return a reference to a field
2484 // in a struct which will be passed on the static chain when calling
2485 // the current function.
2488 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2491 go_assert(var->is_variable() || var->is_result_variable());
2493 this->mark_var_used(var);
2495 Named_object* this_function = this->gogo_->current_function();
2496 Named_object* closure = this_function->func_value()->closure_var();
2498 Enclosing_var ev(var, in_function, this->enclosing_vars_.size());
2499 std::pair<Enclosing_vars::iterator, bool> ins =
2500 this->enclosing_vars_.insert(ev);
2503 // This is a variable we have not seen before. Add a new field
2504 // to the closure type.
2505 this_function->func_value()->add_closure_field(var, location);
2508 Expression* closure_ref = Expression::make_var_reference(closure,
2510 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2512 // The closure structure holds pointers to the variables, so we need
2513 // to introduce an indirection.
2514 Expression* e = Expression::make_field_reference(closure_ref,
2517 e = Expression::make_unary(OPERATOR_MULT, e, location);
2521 // CompositeLit = LiteralType LiteralValue .
2522 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2523 // SliceType | MapType | TypeName .
2524 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2525 // ElementList = Element { "," Element } .
2526 // Element = [ Key ":" ] Value .
2527 // Key = FieldName | ElementIndex .
2528 // FieldName = identifier .
2529 // ElementIndex = Expression .
2530 // Value = Expression | LiteralValue .
2532 // We have already seen the type if there is one, and we are now
2533 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2534 // will be seen here as an array type whose length is "nil". The
2535 // DEPTH parameter is non-zero if this is an embedded composite
2536 // literal and the type was omitted. It gives the number of steps up
2537 // to the type which was provided. E.g., in [][]int{{1}} it will be
2538 // 1. In [][][]int{{{1}}} it will be 2.
2541 Parse::composite_lit(Type* type, int depth, Location location)
2543 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2544 this->advance_token();
2546 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2548 this->advance_token();
2549 return Expression::make_composite_literal(type, depth, false, NULL,
2553 bool has_keys = false;
2554 Expression_list* vals = new Expression_list;
2558 bool is_type_omitted = false;
2560 const Token* token = this->peek_token();
2562 if (token->is_identifier())
2564 std::string identifier = token->identifier();
2565 bool is_exported = token->is_identifier_exported();
2566 Location location = token->location();
2568 if (this->advance_token()->is_op(OPERATOR_COLON))
2570 // This may be a field name. We don't know for sure--it
2571 // could also be an expression for an array index. We
2572 // don't want to parse it as an expression because may
2573 // trigger various errors, e.g., if this identifier
2574 // happens to be the name of a package.
2575 Gogo* gogo = this->gogo_;
2576 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2582 this->unget_token(Token::make_identifier_token(identifier,
2585 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2588 else if (!token->is_op(OPERATOR_LCURLY))
2589 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2592 // This must be a composite literal inside another composite
2593 // literal, with the type omitted for the inner one.
2594 val = this->composite_lit(type, depth + 1, token->location());
2595 is_type_omitted = true;
2598 token = this->peek_token();
2599 if (!token->is_op(OPERATOR_COLON))
2602 vals->push_back(NULL);
2606 if (is_type_omitted && !val->is_error_expression())
2608 error_at(this->location(), "unexpected %<:%>");
2609 val = Expression::make_error(this->location());
2612 this->advance_token();
2614 if (!has_keys && !vals->empty())
2616 Expression_list* newvals = new Expression_list;
2617 for (Expression_list::const_iterator p = vals->begin();
2621 newvals->push_back(NULL);
2622 newvals->push_back(*p);
2629 if (val->unknown_expression() != NULL)
2630 val->unknown_expression()->set_is_composite_literal_key();
2632 vals->push_back(val);
2634 if (!token->is_op(OPERATOR_LCURLY))
2635 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2638 // This must be a composite literal inside another
2639 // composite literal, with the type omitted for the
2641 val = this->composite_lit(type, depth + 1, token->location());
2644 token = this->peek_token();
2647 vals->push_back(val);
2649 if (token->is_op(OPERATOR_COMMA))
2651 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2653 this->advance_token();
2657 else if (token->is_op(OPERATOR_RCURLY))
2659 this->advance_token();
2664 error_at(this->location(), "expected %<,%> or %<}%>");
2666 this->gogo_->mark_locals_used();
2668 while (!token->is_eof()
2669 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2671 if (token->is_op(OPERATOR_LCURLY))
2673 else if (token->is_op(OPERATOR_RCURLY))
2675 token = this->advance_token();
2677 if (token->is_op(OPERATOR_RCURLY))
2678 this->advance_token();
2680 return Expression::make_error(location);
2684 return Expression::make_composite_literal(type, depth, has_keys, vals,
2688 // FunctionLit = "func" Signature Block .
2691 Parse::function_lit()
2693 Location location = this->location();
2694 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2695 this->advance_token();
2697 Enclosing_vars hold_enclosing_vars;
2698 hold_enclosing_vars.swap(this->enclosing_vars_);
2700 Function_type* type = this->signature(NULL, location);
2702 type = Type::make_function_type(NULL, NULL, NULL, location);
2704 // For a function literal, the next token must be a '{'. If we
2705 // don't see that, then we may have a type expression.
2706 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2707 return Expression::make_type(type, location);
2709 Bc_stack* hold_break_stack = this->break_stack_;
2710 Bc_stack* hold_continue_stack = this->continue_stack_;
2711 this->break_stack_ = NULL;
2712 this->continue_stack_ = NULL;
2714 Named_object* no = this->gogo_->start_function("", type, true, location);
2716 Location end_loc = this->block();
2718 this->gogo_->finish_function(end_loc);
2720 if (this->break_stack_ != NULL)
2721 delete this->break_stack_;
2722 if (this->continue_stack_ != NULL)
2723 delete this->continue_stack_;
2724 this->break_stack_ = hold_break_stack;
2725 this->continue_stack_ = hold_continue_stack;
2727 hold_enclosing_vars.swap(this->enclosing_vars_);
2729 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2732 return Expression::make_func_reference(no, closure, location);
2735 // Create a closure for the nested function FUNCTION. This is based
2736 // on ENCLOSING_VARS, which is a list of all variables defined in
2737 // enclosing functions and referenced from FUNCTION. A closure is the
2738 // address of a struct which contains the addresses of all the
2739 // referenced variables. This returns NULL if no closure is required.
2742 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2745 if (enclosing_vars->empty())
2748 // Get the variables in order by their field index.
2750 size_t enclosing_var_count = enclosing_vars->size();
2751 std::vector<Enclosing_var> ev(enclosing_var_count);
2752 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2753 p != enclosing_vars->end();
2755 ev[p->index()] = *p;
2757 // Build an initializer for a composite literal of the closure's
2760 Named_object* enclosing_function = this->gogo_->current_function();
2761 Expression_list* initializer = new Expression_list;
2762 for (size_t i = 0; i < enclosing_var_count; ++i)
2764 go_assert(ev[i].index() == i);
2765 Named_object* var = ev[i].var();
2767 if (ev[i].in_function() == enclosing_function)
2768 ref = Expression::make_var_reference(var, location);
2770 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2772 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2774 initializer->push_back(refaddr);
2777 Named_object* closure_var = function->func_value()->closure_var();
2778 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2779 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2781 return Expression::make_heap_composite(cv, location);
2784 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2786 // If MAY_BE_SINK is true, this expression may be "_".
2788 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2791 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2792 // guard (var := expr.("type") using the literal keyword "type").
2795 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2796 bool* is_type_switch)
2798 Location start_loc = this->location();
2799 bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
2801 Expression* ret = this->operand(may_be_sink);
2803 // An unknown name followed by a curly brace must be a composite
2804 // literal, and the unknown name must be a type.
2805 if (may_be_composite_lit
2806 && !is_parenthesized
2807 && ret->unknown_expression() != NULL
2808 && this->peek_token()->is_op(OPERATOR_LCURLY))
2810 Named_object* no = ret->unknown_expression()->named_object();
2811 Type* type = Type::make_forward_declaration(no);
2812 ret = Expression::make_type(type, ret->location());
2815 // We handle composite literals and type casts here, as it is the
2816 // easiest way to handle types which are in parentheses, as in
2818 if (ret->is_type_expression())
2820 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2822 if (is_parenthesized)
2824 "cannot parenthesize type in composite literal");
2825 ret = this->composite_lit(ret->type(), 0, ret->location());
2827 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2829 Location loc = this->location();
2830 this->advance_token();
2831 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2833 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2835 error_at(this->location(),
2836 "invalid use of %<...%> in type conversion");
2837 this->advance_token();
2839 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2840 error_at(this->location(), "expected %<)%>");
2842 this->advance_token();
2843 if (expr->is_error_expression())
2847 Type* t = ret->type();
2848 if (t->classification() == Type::TYPE_ARRAY
2849 && t->array_type()->length() != NULL
2850 && t->array_type()->length()->is_nil_expression())
2852 error_at(ret->location(),
2853 "invalid use of %<...%> in type conversion");
2854 ret = Expression::make_error(loc);
2857 ret = Expression::make_cast(t, expr, loc);
2864 const Token* token = this->peek_token();
2865 if (token->is_op(OPERATOR_LPAREN))
2866 ret = this->call(this->verify_not_sink(ret));
2867 else if (token->is_op(OPERATOR_DOT))
2869 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
2870 if (is_type_switch != NULL && *is_type_switch)
2873 else if (token->is_op(OPERATOR_LSQUARE))
2874 ret = this->index(this->verify_not_sink(ret));
2882 // Selector = "." identifier .
2883 // TypeGuard = "." "(" QualifiedIdent ")" .
2885 // Note that Operand can expand to QualifiedIdent, which contains a
2886 // ".". That is handled directly in operand when it sees a package
2889 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2890 // guard (var := expr.("type") using the literal keyword "type").
2893 Parse::selector(Expression* left, bool* is_type_switch)
2895 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
2896 Location location = this->location();
2898 const Token* token = this->advance_token();
2899 if (token->is_identifier())
2901 // This could be a field in a struct, or a method in an
2902 // interface, or a method associated with a type. We can't know
2903 // which until we have seen all the types.
2905 this->gogo_->pack_hidden_name(token->identifier(),
2906 token->is_identifier_exported());
2907 if (token->identifier() == "_")
2909 error_at(this->location(), "invalid use of %<_%>");
2910 name = this->gogo_->pack_hidden_name("blank", false);
2912 this->advance_token();
2913 return Expression::make_selector(left, name, location);
2915 else if (token->is_op(OPERATOR_LPAREN))
2917 this->advance_token();
2919 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
2920 type = this->type();
2923 if (is_type_switch != NULL)
2924 *is_type_switch = true;
2927 error_at(this->location(),
2928 "use of %<.(type)%> outside type switch");
2929 type = Type::make_error_type();
2931 this->advance_token();
2933 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2934 error_at(this->location(), "missing %<)%>");
2936 this->advance_token();
2937 if (is_type_switch != NULL && *is_type_switch)
2939 return Expression::make_type_guard(left, type, location);
2943 error_at(this->location(), "expected identifier or %<(%>");
2948 // Index = "[" Expression "]" .
2949 // Slice = "[" Expression ":" [ Expression ] "]" .
2952 Parse::index(Expression* expr)
2954 Location location = this->location();
2955 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
2956 this->advance_token();
2959 if (!this->peek_token()->is_op(OPERATOR_COLON))
2960 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2964 mpz_init_set_ui(zero, 0);
2965 start = Expression::make_integer(&zero, NULL, location);
2969 Expression* end = NULL;
2970 if (this->peek_token()->is_op(OPERATOR_COLON))
2972 // We use nil to indicate a missing high expression.
2973 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
2974 end = Expression::make_nil(this->location());
2976 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
2978 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
2979 error_at(this->location(), "missing %<]%>");
2981 this->advance_token();
2982 return Expression::make_index(expr, start, end, location);
2985 // Call = "(" [ ArgumentList [ "," ] ] ")" .
2986 // ArgumentList = ExpressionList [ "..." ] .
2989 Parse::call(Expression* func)
2991 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2992 Expression_list* args = NULL;
2993 bool is_varargs = false;
2994 const Token* token = this->advance_token();
2995 if (!token->is_op(OPERATOR_RPAREN))
2997 args = this->expression_list(NULL, false);
2998 token = this->peek_token();
2999 if (token->is_op(OPERATOR_ELLIPSIS))
3002 token = this->advance_token();
3005 if (token->is_op(OPERATOR_COMMA))
3006 token = this->advance_token();
3007 if (!token->is_op(OPERATOR_RPAREN))
3008 error_at(this->location(), "missing %<)%>");
3010 this->advance_token();
3011 if (func->is_error_expression())
3013 return Expression::make_call(func, args, is_varargs, func->location());
3016 // Return an expression for a single unqualified identifier.
3019 Parse::id_to_expression(const std::string& name, Location location)
3021 Named_object* in_function;
3022 Named_object* named_object = this->gogo_->lookup(name, &in_function);
3023 if (named_object == NULL)
3024 named_object = this->gogo_->add_unknown_name(name, location);
3026 if (in_function != NULL
3027 && in_function != this->gogo_->current_function()
3028 && (named_object->is_variable() || named_object->is_result_variable()))
3029 return this->enclosing_var_reference(in_function, named_object,
3032 switch (named_object->classification())
3034 case Named_object::NAMED_OBJECT_CONST:
3035 return Expression::make_const_reference(named_object, location);
3036 case Named_object::NAMED_OBJECT_VAR:
3037 case Named_object::NAMED_OBJECT_RESULT_VAR:
3038 this->mark_var_used(named_object);
3039 return Expression::make_var_reference(named_object, location);
3040 case Named_object::NAMED_OBJECT_SINK:
3041 return Expression::make_sink(location);
3042 case Named_object::NAMED_OBJECT_FUNC:
3043 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3044 return Expression::make_func_reference(named_object, NULL, location);
3045 case Named_object::NAMED_OBJECT_UNKNOWN:
3046 return Expression::make_unknown_reference(named_object, location);
3047 case Named_object::NAMED_OBJECT_PACKAGE:
3048 case Named_object::NAMED_OBJECT_TYPE:
3049 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3050 // These cases can arise for a field name in a composite
3052 return Expression::make_unknown_reference(named_object, location);
3054 error_at(this->location(), "unexpected type of identifier");
3055 return Expression::make_error(location);
3059 // Expression = UnaryExpr { binary_op Expression } .
3061 // PRECEDENCE is the precedence of the current operator.
3063 // If MAY_BE_SINK is true, this expression may be "_".
3065 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3068 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3069 // guard (var := expr.("type") using the literal keyword "type").
3072 Parse::expression(Precedence precedence, bool may_be_sink,
3073 bool may_be_composite_lit, bool* is_type_switch)
3075 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3080 if (is_type_switch != NULL && *is_type_switch)
3083 const Token* token = this->peek_token();
3084 if (token->classification() != Token::TOKEN_OPERATOR)
3090 Precedence right_precedence;
3091 switch (token->op())
3094 right_precedence = PRECEDENCE_OROR;
3096 case OPERATOR_ANDAND:
3097 right_precedence = PRECEDENCE_ANDAND;
3100 case OPERATOR_NOTEQ:
3105 right_precedence = PRECEDENCE_RELOP;
3108 case OPERATOR_MINUS:
3111 right_precedence = PRECEDENCE_ADDOP;
3116 case OPERATOR_LSHIFT:
3117 case OPERATOR_RSHIFT:
3119 case OPERATOR_BITCLEAR:
3120 right_precedence = PRECEDENCE_MULOP;
3123 right_precedence = PRECEDENCE_INVALID;
3127 if (right_precedence == PRECEDENCE_INVALID)
3133 Operator op = token->op();
3134 Location binop_location = token->location();
3136 if (precedence >= right_precedence)
3138 // We've already seen A * B, and we see + C. We want to
3139 // return so that A * B becomes a group.
3143 this->advance_token();
3145 left = this->verify_not_sink(left);
3146 Expression* right = this->expression(right_precedence, false,
3147 may_be_composite_lit,
3149 left = Expression::make_binary(op, left, right, binop_location);
3154 Parse::expression_may_start_here()
3156 const Token* token = this->peek_token();
3157 switch (token->classification())
3159 case Token::TOKEN_INVALID:
3160 case Token::TOKEN_EOF:
3162 case Token::TOKEN_KEYWORD:
3163 switch (token->keyword())
3168 case KEYWORD_STRUCT:
3169 case KEYWORD_INTERFACE:
3174 case Token::TOKEN_IDENTIFIER:
3176 case Token::TOKEN_STRING:
3178 case Token::TOKEN_OPERATOR:
3179 switch (token->op())
3182 case OPERATOR_MINUS:
3186 case OPERATOR_CHANOP:
3188 case OPERATOR_LPAREN:
3189 case OPERATOR_LSQUARE:
3194 case Token::TOKEN_CHARACTER:
3195 case Token::TOKEN_INTEGER:
3196 case Token::TOKEN_FLOAT:
3197 case Token::TOKEN_IMAGINARY:
3204 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3206 // If MAY_BE_SINK is true, this expression may be "_".
3208 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3211 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3212 // guard (var := expr.("type") using the literal keyword "type").
3215 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3216 bool* is_type_switch)
3218 const Token* token = this->peek_token();
3219 if (token->is_op(OPERATOR_PLUS)
3220 || token->is_op(OPERATOR_MINUS)
3221 || token->is_op(OPERATOR_NOT)
3222 || token->is_op(OPERATOR_XOR)
3223 || token->is_op(OPERATOR_CHANOP)
3224 || token->is_op(OPERATOR_MULT)
3225 || token->is_op(OPERATOR_AND))
3227 Location location = token->location();
3228 Operator op = token->op();
3229 this->advance_token();
3231 if (op == OPERATOR_CHANOP
3232 && this->peek_token()->is_keyword(KEYWORD_CHAN))
3234 // This is "<- chan" which must be the start of a type.
3235 this->unget_token(Token::make_operator_token(op, location));
3236 return Expression::make_type(this->type(), location);
3239 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL);
3240 if (expr->is_error_expression())
3242 else if (op == OPERATOR_MULT && expr->is_type_expression())
3243 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3245 else if (op == OPERATOR_AND && expr->is_composite_literal())
3246 expr = Expression::make_heap_composite(expr, location);
3247 else if (op != OPERATOR_CHANOP)
3248 expr = Expression::make_unary(op, expr, location);
3250 expr = Expression::make_receive(expr, location);
3254 return this->primary_expr(may_be_sink, may_be_composite_lit,
3259 // Declaration | LabeledStmt | SimpleStmt |
3260 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3261 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3264 // LABEL is the label of this statement if it has one.
3267 Parse::statement(Label* label)
3269 const Token* token = this->peek_token();
3270 switch (token->classification())
3272 case Token::TOKEN_KEYWORD:
3274 switch (token->keyword())
3279 this->declaration();
3283 case KEYWORD_STRUCT:
3284 case KEYWORD_INTERFACE:
3285 this->simple_stat(true, NULL, NULL, NULL);
3289 this->go_or_defer_stat();
3291 case KEYWORD_RETURN:
3292 this->return_stat();
3297 case KEYWORD_CONTINUE:
3298 this->continue_stat();
3306 case KEYWORD_SWITCH:
3307 this->switch_stat(label);
3309 case KEYWORD_SELECT:
3310 this->select_stat(label);
3313 this->for_stat(label);
3316 error_at(this->location(), "expected statement");
3317 this->advance_token();
3323 case Token::TOKEN_IDENTIFIER:
3325 std::string identifier = token->identifier();
3326 bool is_exported = token->is_identifier_exported();
3327 Location location = token->location();
3328 if (this->advance_token()->is_op(OPERATOR_COLON))
3330 this->advance_token();
3331 this->labeled_stmt(identifier, location);
3335 this->unget_token(Token::make_identifier_token(identifier,
3338 this->simple_stat(true, NULL, NULL, NULL);
3343 case Token::TOKEN_OPERATOR:
3344 if (token->is_op(OPERATOR_LCURLY))
3346 Location location = token->location();
3347 this->gogo_->start_block(location);
3348 Location end_loc = this->block();
3349 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3352 else if (!token->is_op(OPERATOR_SEMICOLON))
3353 this->simple_stat(true, NULL, NULL, NULL);
3356 case Token::TOKEN_STRING:
3357 case Token::TOKEN_CHARACTER:
3358 case Token::TOKEN_INTEGER:
3359 case Token::TOKEN_FLOAT:
3360 case Token::TOKEN_IMAGINARY:
3361 this->simple_stat(true, NULL, NULL, NULL);
3365 error_at(this->location(), "expected statement");
3366 this->advance_token();
3372 Parse::statement_may_start_here()
3374 const Token* token = this->peek_token();
3375 switch (token->classification())
3377 case Token::TOKEN_KEYWORD:
3379 switch (token->keyword())
3386 case KEYWORD_STRUCT:
3387 case KEYWORD_INTERFACE:
3390 case KEYWORD_RETURN:
3392 case KEYWORD_CONTINUE:
3395 case KEYWORD_SWITCH:
3396 case KEYWORD_SELECT:
3406 case Token::TOKEN_IDENTIFIER:
3409 case Token::TOKEN_OPERATOR:
3410 if (token->is_op(OPERATOR_LCURLY)
3411 || token->is_op(OPERATOR_SEMICOLON))
3414 return this->expression_may_start_here();