compiler: Don't allow tuple assignments to contain duplicate symbols.
[official-gcc.git] / gcc / go / gofrontend / parse.cc
blobc4bcf058d8eacd315eee97194fd1ee34054ee50e
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.
7 #include "go-system.h"
9 #include "lex.h"
10 #include "gogo.h"
11 #include "types.h"
12 #include "statements.h"
13 #include "expressions.h"
14 #include "parse.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
20 bool
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
22 const Enclosing_var& v2)
24 if (v1.var() == v2.var())
25 return false;
27 const std::string& n1(v1.var()->name());
28 const std::string& n2(v2.var()->name());
29 int i = n1.compare(n2);
30 if (i < 0)
31 return true;
32 else if (i > 0)
33 return false;
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.
38 go_unreachable();
41 // Class Parse.
43 Parse::Parse(Lex* lex, Gogo* gogo)
44 : lex_(lex),
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),
49 gogo_(gogo),
50 break_stack_(NULL),
51 continue_stack_(NULL),
52 iota_(0),
53 enclosing_vars_(),
54 type_switch_vars_()
58 // Return the current token.
60 const Token*
61 Parse::peek_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();
67 return &this->token_;
70 // Advance to the next token and return it.
72 const Token*
73 Parse::advance_token()
75 if (this->unget_token_valid_)
77 this->unget_token_valid_ = false;
78 if (!this->token_.is_invalid())
79 return &this->token_;
81 this->token_ = this->lex_->next_token();
82 return &this->token_;
85 // Push a token back on the input stream.
87 void
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.
97 Location
98 Parse::location()
100 return this->peek_token()->location();
103 // IdentifierList = identifier { "," identifier } .
105 void
106 Parse::identifier_list(Typed_identifier_list* til)
108 const Token* token = this->peek_token();
109 while (true)
111 if (!token->is_identifier())
113 error_at(this->location(), "expected identifier");
114 return;
116 std::string name =
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))
122 return;
123 token = this->advance_token();
127 // ExpressionList = Expression { "," Expression } .
129 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
130 // literal.
132 // If MAY_BE_SINK is true, the expressions in the list may be "_".
134 Expression_list*
135 Parse::expression_list(Expression* first, bool may_be_sink,
136 bool may_be_composite_lit)
138 Expression_list* ret = new Expression_list();
139 if (first != NULL)
140 ret->push_back(first);
141 while (true)
143 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink,
144 may_be_composite_lit, NULL, NULL));
146 const Token* token = this->peek_token();
147 if (!token->is_op(OPERATOR_COMMA))
148 return ret;
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,
156 location));
157 return ret;
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
168 // message.
170 bool
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");
177 return false;
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))
187 *pname = name;
188 *ppackage = NULL;
189 return true;
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();
199 return false;
202 package->package_value()->set_used();
204 token = this->advance_token();
205 if (!token->is_identifier())
207 error_at(this->location(), "expected identifier");
208 return false;
211 name = token->identifier();
213 if (name == "_")
215 error_at(this->location(), "invalid use of %<_%>");
216 name = Gogo::erroneous_name();
219 if (package->name() == this->gogo_->package_name())
220 name = this->gogo_->pack_hidden_name(name,
221 token->is_identifier_exported());
223 *pname = name;
224 *ppackage = package;
226 this->advance_token();
228 return true;
231 // Type = TypeName | TypeLit | "(" Type ")" .
232 // TypeLit =
233 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
234 // SliceType | MapType | ChannelType .
236 Type*
237 Parse::type()
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(true);
249 else if (token->is_keyword(KEYWORD_FUNC))
251 Location location = token->location();
252 this->advance_token();
253 Type* type = this->signature(NULL, location);
254 if (type == NULL)
255 return Type::make_error_type();
256 return 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();
270 else
272 if (!ret->is_error_type())
273 error_at(this->location(), "expected %<)%>");
275 return ret;
277 else
279 error_at(token->location(), "expected type");
280 return Type::make_error_type();
284 bool
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.
305 Type*
306 Parse::type_name(bool issue_error)
308 Location location = this->location();
310 std::string name;
311 Named_object* package;
312 if (!this->qualified_ident(&name, &package))
313 return Type::make_error_type();
315 Named_object* named_object;
316 if (package == NULL)
317 named_object = this->gogo_->lookup(name, NULL);
318 else
320 named_object = package->package_value()->lookup(name);
321 if (named_object == NULL
322 && issue_error
323 && package->name() != this->gogo_->package_name())
325 // Check whether the name is there but hidden.
326 std::string s = ('.' + package->package_value()->pkgpath()
327 + '.' + name);
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());
336 issue_error = false;
341 bool ok = true;
342 if (named_object == NULL)
344 if (package == NULL)
345 named_object = this->gogo_->add_unknown_name(name, location);
346 else
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());
352 issue_error = false;
353 ok = false;
356 else if (named_object->is_type())
358 if (!named_object->type_value()->is_visible())
359 ok = false;
361 else if (named_object->is_unknown() || named_object->is_type_declaration())
363 else
364 ok = false;
366 if (!ok)
368 if (issue_error)
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);
377 else
378 go_unreachable();
381 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
382 // ArrayLength = Expression .
383 // ElementType = CompleteType .
385 Type*
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();
394 else
396 if (!token->is_op(OPERATOR_ELLIPSIS))
397 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL, 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();
407 else
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 .
431 Type*
432 Parse::map_type()
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 ";" } "}" .
462 Type*
463 Parse::struct_type()
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 %<{%>");
473 else
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();
497 pi != sfl->end();
498 ++pi)
500 if (pi->type()->is_error_type())
501 return pi->type();
502 for (Struct_field_list::const_iterator pj = pi + 1;
503 pj != sfl->end();
504 ++pj)
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 .
519 void
520 Parse::field_decl(Struct_field_list* sfl)
522 const Token* token = this->peek_token();
523 Location location = token->location();
524 bool is_anonymous;
525 bool is_anonymous_pointer;
526 if (token->is_op(OPERATOR_MULT))
528 is_anonymous = true;
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,
543 id_location));
545 else
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)
551 && !token->is_eof())
552 token = this->advance_token();
553 return;
556 if (is_anonymous)
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)
567 && !token->is_eof())
568 token = this->advance_token();
569 return;
572 Type* type = this->type_name(true);
574 std::string tag;
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)));
586 if (!tag.empty())
587 sfl->back().set_tag(tag);
590 else
592 Typed_identifier_list til;
593 while (true)
595 token = this->peek_token();
596 if (!token->is_identifier())
598 error_at(this->location(), "expected identifier");
599 return;
601 std::string name =
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))
606 break;
607 this->advance_token();
610 Type* type = this->type();
612 std::string tag;
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();
620 p != til.end();
621 ++p)
623 p->set_type(type);
624 sfl->push_back(Struct_field(*p));
625 if (!tag.empty())
626 sfl->back().set_tag(tag);
631 // PointerType = "*" Type .
633 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())
640 return 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 .
649 Type*
650 Parse::channel_type()
652 const Token* token = this->peek_token();
653 bool send = true;
654 bool receive = true;
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();
662 send = false;
663 this->advance_token();
665 else
667 go_assert(token->is_keyword(KEYWORD_CHAN));
668 if (this->advance_token()->is_op(OPERATOR_CHANOP))
670 receive = false;
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");
686 else
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.
697 void
698 Parse::check_signature_names(const Typed_identifier_list* params,
699 Parse::Names* names)
701 for (Typed_identifier_list::const_iterator p = params->begin();
702 p != params->end();
703 ++p)
705 if (p->name().empty() || Gogo::is_sink_name(p->name()))
706 continue;
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);
710 if (!ins.second)
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.
728 Function_type*
729 Parse::signature(Typed_identifier* receiver, Location location)
731 bool is_varargs = false;
732 Typed_identifier_list* params;
733 bool params_ok = this->parameters(&params, &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))
740 return NULL;
743 if (!params_ok)
744 return NULL;
746 Parse::Names names;
747 if (receiver != NULL)
748 names[receiver->name()] = receiver;
749 if (params != NULL)
750 this->check_signature_names(params, &names);
751 if (results != NULL)
752 this->check_signature_names(results, &names);
754 Function_type* ret = Type::make_function_type(receiver, params, results,
755 location);
756 if (is_varargs)
757 ret->set_is_varargs();
758 return ret;
761 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
763 // This returns false on a parse error.
765 bool
766 Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs)
768 *pparams = NULL;
770 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
772 error_at(this->location(), "expected %<(%>");
773 return false;
776 Typed_identifier_list* params = NULL;
777 bool saw_error = false;
779 const Token* token = this->advance_token();
780 if (!token->is_op(OPERATOR_RPAREN))
782 params = this->parameter_list(is_varargs);
783 if (params == NULL)
784 saw_error = true;
785 token = this->peek_token();
788 // The optional trailing comma is picked up in parameter_list.
790 if (!token->is_op(OPERATOR_RPAREN))
791 error_at(this->location(), "expected %<)%>");
792 else
793 this->advance_token();
795 if (saw_error)
796 return false;
798 *pparams = params;
799 return true;
802 // ParameterList = ParameterDecl { "," ParameterDecl } .
804 // This sets *IS_VARARGS if the list ends with an ellipsis.
805 // IS_VARARGS will be NULL if varargs are not permitted.
807 // We pick up an optional trailing comma.
809 // This returns NULL if some error is seen.
811 Typed_identifier_list*
812 Parse::parameter_list(bool* is_varargs)
814 Location location = this->location();
815 Typed_identifier_list* ret = new Typed_identifier_list();
817 bool saw_error = false;
819 // If we see an identifier and then a comma, then we don't know
820 // whether we are looking at a list of identifiers followed by a
821 // type, or a list of types given by name. We have to do an
822 // arbitrary lookahead to figure it out.
824 bool parameters_have_names;
825 const Token* token = this->peek_token();
826 if (!token->is_identifier())
828 // This must be a type which starts with something like '*'.
829 parameters_have_names = false;
831 else
833 std::string name = token->identifier();
834 bool is_exported = token->is_identifier_exported();
835 Location location = token->location();
836 token = this->advance_token();
837 if (!token->is_op(OPERATOR_COMMA))
839 if (token->is_op(OPERATOR_DOT))
841 // This is a qualified identifier, which must turn out
842 // to be a type.
843 parameters_have_names = false;
845 else if (token->is_op(OPERATOR_RPAREN))
847 // A single identifier followed by a parenthesis must be
848 // a type name.
849 parameters_have_names = false;
851 else
853 // An identifier followed by something other than a
854 // comma or a dot or a right parenthesis must be a
855 // parameter name followed by a type.
856 parameters_have_names = true;
859 this->unget_token(Token::make_identifier_token(name, is_exported,
860 location));
862 else
864 // An identifier followed by a comma may be the first in a
865 // list of parameter names followed by a type, or it may be
866 // the first in a list of types without parameter names. To
867 // find out we gather as many identifiers separated by
868 // commas as we can.
869 std::string id_name = this->gogo_->pack_hidden_name(name,
870 is_exported);
871 ret->push_back(Typed_identifier(id_name, NULL, location));
872 bool just_saw_comma = true;
873 while (this->advance_token()->is_identifier())
875 name = this->peek_token()->identifier();
876 is_exported = this->peek_token()->is_identifier_exported();
877 location = this->peek_token()->location();
878 id_name = this->gogo_->pack_hidden_name(name, is_exported);
879 ret->push_back(Typed_identifier(id_name, NULL, location));
880 if (!this->advance_token()->is_op(OPERATOR_COMMA))
882 just_saw_comma = false;
883 break;
887 if (just_saw_comma)
889 // We saw ID1 "," ID2 "," followed by something which
890 // was not an identifier. We must be seeing the start
891 // of a type, and ID1 and ID2 must be types, and the
892 // parameters don't have names.
893 parameters_have_names = false;
895 else if (this->peek_token()->is_op(OPERATOR_RPAREN))
897 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
898 // and the parameters don't have names.
899 parameters_have_names = false;
901 else if (this->peek_token()->is_op(OPERATOR_DOT))
903 // We saw ID1 "," ID2 ".". ID2 must be a package name,
904 // ID1 must be a type, and the parameters don't have
905 // names.
906 parameters_have_names = false;
907 this->unget_token(Token::make_identifier_token(name, is_exported,
908 location));
909 ret->pop_back();
910 just_saw_comma = true;
912 else
914 // We saw ID1 "," ID2 followed by something other than
915 // ",", ".", or ")". We must be looking at the start of
916 // a type, and ID1 and ID2 must be parameter names.
917 parameters_have_names = true;
920 if (parameters_have_names)
922 go_assert(!just_saw_comma);
923 // We have just seen ID1, ID2 xxx.
924 Type* type;
925 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
926 type = this->type();
927 else
929 error_at(this->location(), "%<...%> only permits one name");
930 saw_error = true;
931 this->advance_token();
932 type = this->type();
934 for (size_t i = 0; i < ret->size(); ++i)
935 ret->set_type(i, type);
936 if (!this->peek_token()->is_op(OPERATOR_COMMA))
937 return saw_error ? NULL : ret;
938 if (this->advance_token()->is_op(OPERATOR_RPAREN))
939 return saw_error ? NULL : ret;
941 else
943 Typed_identifier_list* tret = new Typed_identifier_list();
944 for (Typed_identifier_list::const_iterator p = ret->begin();
945 p != ret->end();
946 ++p)
948 Named_object* no = this->gogo_->lookup(p->name(), NULL);
949 Type* type;
950 if (no == NULL)
951 no = this->gogo_->add_unknown_name(p->name(),
952 p->location());
954 if (no->is_type())
955 type = no->type_value();
956 else if (no->is_unknown() || no->is_type_declaration())
957 type = Type::make_forward_declaration(no);
958 else
960 error_at(p->location(), "expected %<%s%> to be a type",
961 Gogo::message_name(p->name()).c_str());
962 saw_error = true;
963 type = Type::make_error_type();
965 tret->push_back(Typed_identifier("", type, p->location()));
967 delete ret;
968 ret = tret;
969 if (!just_saw_comma
970 || this->peek_token()->is_op(OPERATOR_RPAREN))
971 return saw_error ? NULL : ret;
976 bool mix_error = false;
977 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error,
978 &saw_error);
979 while (this->peek_token()->is_op(OPERATOR_COMMA))
981 if (this->advance_token()->is_op(OPERATOR_RPAREN))
982 break;
983 if (is_varargs != NULL && *is_varargs)
985 error_at(this->location(), "%<...%> must be last parameter");
986 saw_error = true;
988 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error,
989 &saw_error);
991 if (mix_error)
993 error_at(location, "invalid named/anonymous mix");
994 saw_error = true;
996 if (saw_error)
998 delete ret;
999 return NULL;
1001 return ret;
1004 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1006 void
1007 Parse::parameter_decl(bool parameters_have_names,
1008 Typed_identifier_list* til,
1009 bool* is_varargs,
1010 bool* mix_error,
1011 bool* saw_error)
1013 if (!parameters_have_names)
1015 Type* type;
1016 Location location = this->location();
1017 if (!this->peek_token()->is_identifier())
1019 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1020 type = this->type();
1021 else
1023 if (is_varargs == NULL)
1024 error_at(this->location(), "invalid use of %<...%>");
1025 else
1026 *is_varargs = true;
1027 this->advance_token();
1028 if (is_varargs == NULL
1029 && this->peek_token()->is_op(OPERATOR_RPAREN))
1030 type = Type::make_error_type();
1031 else
1033 Type* element_type = this->type();
1034 type = Type::make_array_type(element_type, NULL);
1038 else
1040 type = this->type_name(false);
1041 if (type->is_error_type()
1042 || (!this->peek_token()->is_op(OPERATOR_COMMA)
1043 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1045 *mix_error = true;
1046 while (!this->peek_token()->is_op(OPERATOR_COMMA)
1047 && !this->peek_token()->is_op(OPERATOR_RPAREN))
1048 this->advance_token();
1051 if (!type->is_error_type())
1052 til->push_back(Typed_identifier("", type, location));
1053 else
1054 *saw_error = true;
1056 else
1058 size_t orig_count = til->size();
1059 if (this->peek_token()->is_identifier())
1060 this->identifier_list(til);
1061 else
1062 *mix_error = true;
1063 size_t new_count = til->size();
1065 Type* type;
1066 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1067 type = this->type();
1068 else
1070 if (is_varargs == NULL)
1072 error_at(this->location(), "invalid use of %<...%>");
1073 *saw_error = true;
1075 else if (new_count > orig_count + 1)
1077 error_at(this->location(), "%<...%> only permits one name");
1078 *saw_error = true;
1080 else
1081 *is_varargs = true;
1082 this->advance_token();
1083 Type* element_type = this->type();
1084 type = Type::make_array_type(element_type, NULL);
1086 for (size_t i = orig_count; i < new_count; ++i)
1087 til->set_type(i, type);
1091 // Result = Parameters | Type .
1093 // This returns false on a parse error.
1095 bool
1096 Parse::result(Typed_identifier_list** presults)
1098 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1099 return this->parameters(presults, NULL);
1100 else
1102 Location location = this->location();
1103 Type* type = this->type();
1104 if (type->is_error_type())
1106 *presults = NULL;
1107 return false;
1109 Typed_identifier_list* til = new Typed_identifier_list();
1110 til->push_back(Typed_identifier("", type, location));
1111 *presults = til;
1112 return true;
1116 // Block = "{" [ StatementList ] "}" .
1118 // Returns the location of the closing brace.
1120 Location
1121 Parse::block()
1123 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1125 Location loc = this->location();
1126 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1127 && this->advance_token()->is_op(OPERATOR_LCURLY))
1128 error_at(loc, "unexpected semicolon or newline before %<{%>");
1129 else
1131 error_at(this->location(), "expected %<{%>");
1132 return Linemap::unknown_location();
1136 const Token* token = this->advance_token();
1138 if (!token->is_op(OPERATOR_RCURLY))
1140 this->statement_list();
1141 token = this->peek_token();
1142 if (!token->is_op(OPERATOR_RCURLY))
1144 if (!token->is_eof() || !saw_errors())
1145 error_at(this->location(), "expected %<}%>");
1147 this->gogo_->mark_locals_used();
1149 // Skip ahead to the end of the block, in hopes of avoiding
1150 // lots of meaningless errors.
1151 Location ret = token->location();
1152 int nest = 0;
1153 while (!token->is_eof())
1155 if (token->is_op(OPERATOR_LCURLY))
1156 ++nest;
1157 else if (token->is_op(OPERATOR_RCURLY))
1159 --nest;
1160 if (nest < 0)
1162 this->advance_token();
1163 break;
1166 token = this->advance_token();
1167 ret = token->location();
1169 return ret;
1173 Location ret = token->location();
1174 this->advance_token();
1175 return ret;
1178 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1179 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1181 Type*
1182 Parse::interface_type(bool record)
1184 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1185 Location location = this->location();
1187 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1189 Location token_loc = this->location();
1190 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1191 && this->advance_token()->is_op(OPERATOR_LCURLY))
1192 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1193 else
1195 error_at(this->location(), "expected %<{%>");
1196 return Type::make_error_type();
1199 this->advance_token();
1201 Typed_identifier_list* methods = new Typed_identifier_list();
1202 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1204 this->method_spec(methods);
1205 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1207 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1208 break;
1209 this->method_spec(methods);
1211 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1213 error_at(this->location(), "expected %<}%>");
1214 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1216 if (this->peek_token()->is_eof())
1217 return Type::make_error_type();
1221 this->advance_token();
1223 if (methods->empty())
1225 delete methods;
1226 methods = NULL;
1229 Interface_type* ret = Type::make_interface_type(methods, location);
1230 if (record)
1231 this->gogo_->record_interface_type(ret);
1232 return ret;
1235 // MethodSpec = MethodName Signature | InterfaceTypeName .
1236 // MethodName = identifier .
1237 // InterfaceTypeName = TypeName .
1239 void
1240 Parse::method_spec(Typed_identifier_list* methods)
1242 const Token* token = this->peek_token();
1243 if (!token->is_identifier())
1245 error_at(this->location(), "expected identifier");
1246 return;
1249 std::string name = token->identifier();
1250 bool is_exported = token->is_identifier_exported();
1251 Location location = token->location();
1253 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1255 // This is a MethodName.
1256 if (name == "_")
1257 error_at(this->location(), "methods must have a unique non-blank name");
1258 name = this->gogo_->pack_hidden_name(name, is_exported);
1259 Type* type = this->signature(NULL, location);
1260 if (type == NULL)
1261 return;
1262 methods->push_back(Typed_identifier(name, type, location));
1264 else
1266 this->unget_token(Token::make_identifier_token(name, is_exported,
1267 location));
1268 Type* type = this->type_name(false);
1269 if (type->is_error_type()
1270 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1271 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1273 if (this->peek_token()->is_op(OPERATOR_COMMA))
1274 error_at(this->location(),
1275 "name list not allowed in interface type");
1276 else
1277 error_at(location, "expected signature or type name");
1278 this->gogo_->mark_locals_used();
1279 token = this->peek_token();
1280 while (!token->is_eof()
1281 && !token->is_op(OPERATOR_SEMICOLON)
1282 && !token->is_op(OPERATOR_RCURLY))
1283 token = this->advance_token();
1284 return;
1286 // This must be an interface type, but we can't check that now.
1287 // We check it and pull out the methods in
1288 // Interface_type::do_verify.
1289 methods->push_back(Typed_identifier("", type, location));
1293 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1295 void
1296 Parse::declaration()
1298 const Token* token = this->peek_token();
1300 bool saw_nointerface = this->lex_->get_and_clear_nointerface();
1301 if (saw_nointerface && !token->is_keyword(KEYWORD_FUNC))
1302 warning_at(token->location(), 0,
1303 "ignoring magic //go:nointerface comment before non-method");
1305 if (token->is_keyword(KEYWORD_CONST))
1306 this->const_decl();
1307 else if (token->is_keyword(KEYWORD_TYPE))
1308 this->type_decl();
1309 else if (token->is_keyword(KEYWORD_VAR))
1310 this->var_decl();
1311 else if (token->is_keyword(KEYWORD_FUNC))
1312 this->function_decl(saw_nointerface);
1313 else
1315 error_at(this->location(), "expected declaration");
1316 this->advance_token();
1320 bool
1321 Parse::declaration_may_start_here()
1323 const Token* token = this->peek_token();
1324 return (token->is_keyword(KEYWORD_CONST)
1325 || token->is_keyword(KEYWORD_TYPE)
1326 || token->is_keyword(KEYWORD_VAR)
1327 || token->is_keyword(KEYWORD_FUNC));
1330 // Decl<P> = P | "(" [ List<P> ] ")" .
1332 void
1333 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1335 if (this->peek_token()->is_eof())
1337 if (!saw_errors())
1338 error_at(this->location(), "unexpected end of file");
1339 return;
1342 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1343 (this->*pfn)(varg);
1344 else
1346 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1348 this->list(pfn, varg, true);
1349 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1351 error_at(this->location(), "missing %<)%>");
1352 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1354 if (this->peek_token()->is_eof())
1355 return;
1359 this->advance_token();
1363 // List<P> = P { ";" P } [ ";" ] .
1365 // In order to pick up the trailing semicolon we need to know what
1366 // might follow. This is either a '}' or a ')'.
1368 void
1369 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1371 (this->*pfn)(varg);
1372 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1373 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1374 || this->peek_token()->is_op(OPERATOR_COMMA))
1376 if (this->peek_token()->is_op(OPERATOR_COMMA))
1377 error_at(this->location(), "unexpected comma");
1378 if (this->advance_token()->is_op(follow))
1379 break;
1380 (this->*pfn)(varg);
1384 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1386 void
1387 Parse::const_decl()
1389 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1390 this->advance_token();
1391 this->reset_iota();
1393 Type* last_type = NULL;
1394 Expression_list* last_expr_list = NULL;
1396 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1397 this->const_spec(&last_type, &last_expr_list);
1398 else
1400 this->advance_token();
1401 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1403 this->const_spec(&last_type, &last_expr_list);
1404 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1405 this->advance_token();
1406 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1408 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1409 if (!this->skip_past_error(OPERATOR_RPAREN))
1410 return;
1413 this->advance_token();
1416 if (last_expr_list != NULL)
1417 delete last_expr_list;
1420 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1422 void
1423 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1425 Typed_identifier_list til;
1426 this->identifier_list(&til);
1428 Type* type = NULL;
1429 if (this->type_may_start_here())
1431 type = this->type();
1432 *last_type = NULL;
1433 *last_expr_list = NULL;
1436 Expression_list *expr_list;
1437 if (!this->peek_token()->is_op(OPERATOR_EQ))
1439 if (*last_expr_list == NULL)
1441 error_at(this->location(), "expected %<=%>");
1442 return;
1444 type = *last_type;
1445 expr_list = new Expression_list;
1446 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1447 p != (*last_expr_list)->end();
1448 ++p)
1449 expr_list->push_back((*p)->copy());
1451 else
1453 this->advance_token();
1454 expr_list = this->expression_list(NULL, false, true);
1455 *last_type = type;
1456 if (*last_expr_list != NULL)
1457 delete *last_expr_list;
1458 *last_expr_list = expr_list;
1461 Expression_list::const_iterator pe = expr_list->begin();
1462 for (Typed_identifier_list::iterator pi = til.begin();
1463 pi != til.end();
1464 ++pi, ++pe)
1466 if (pe == expr_list->end())
1468 error_at(this->location(), "not enough initializers");
1469 return;
1471 if (type != NULL)
1472 pi->set_type(type);
1474 if (!Gogo::is_sink_name(pi->name()))
1475 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1476 else
1478 static int count;
1479 char buf[30];
1480 snprintf(buf, sizeof buf, ".$sinkconst%d", count);
1481 ++count;
1482 Typed_identifier ti(std::string(buf), type, pi->location());
1483 Named_object* no = this->gogo_->add_constant(ti, *pe, this->iota_value());
1484 no->const_value()->set_is_sink();
1487 if (pe != expr_list->end())
1488 error_at(this->location(), "too many initializers");
1490 this->increment_iota();
1492 return;
1495 // TypeDecl = "type" Decl<TypeSpec> .
1497 void
1498 Parse::type_decl()
1500 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1501 this->advance_token();
1502 this->decl(&Parse::type_spec, NULL);
1505 // TypeSpec = identifier Type .
1507 void
1508 Parse::type_spec(void*)
1510 const Token* token = this->peek_token();
1511 if (!token->is_identifier())
1513 error_at(this->location(), "expected identifier");
1514 return;
1516 std::string name = token->identifier();
1517 bool is_exported = token->is_identifier_exported();
1518 Location location = token->location();
1519 token = this->advance_token();
1521 // The scope of the type name starts at the point where the
1522 // identifier appears in the source code. We implement this by
1523 // declaring the type before we read the type definition.
1524 Named_object* named_type = NULL;
1525 if (name != "_")
1527 name = this->gogo_->pack_hidden_name(name, is_exported);
1528 named_type = this->gogo_->declare_type(name, location);
1531 Type* type;
1532 if (name == "_" && this->peek_token()->is_keyword(KEYWORD_INTERFACE))
1534 // We call Parse::interface_type explicity here because we do not want
1535 // to record an interface with a blank type name.
1536 type = this->interface_type(false);
1538 else if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1539 type = this->type();
1540 else
1542 error_at(this->location(),
1543 "unexpected semicolon or newline in type declaration");
1544 type = Type::make_error_type();
1545 this->advance_token();
1548 if (type->is_error_type())
1550 this->gogo_->mark_locals_used();
1551 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1552 && !this->peek_token()->is_eof())
1553 this->advance_token();
1556 if (name != "_")
1558 if (named_type->is_type_declaration())
1560 Type* ftype = type->forwarded();
1561 if (ftype->forward_declaration_type() != NULL
1562 && (ftype->forward_declaration_type()->named_object()
1563 == named_type))
1565 error_at(location, "invalid recursive type");
1566 type = Type::make_error_type();
1569 this->gogo_->define_type(named_type,
1570 Type::make_named_type(named_type, type,
1571 location));
1572 go_assert(named_type->package() == NULL);
1574 else
1576 // This will probably give a redefinition error.
1577 this->gogo_->add_type(name, type, location);
1582 // VarDecl = "var" Decl<VarSpec> .
1584 void
1585 Parse::var_decl()
1587 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1588 this->advance_token();
1589 this->decl(&Parse::var_spec, NULL);
1592 // VarSpec = IdentifierList
1593 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1595 void
1596 Parse::var_spec(void*)
1598 // Get the variable names.
1599 Typed_identifier_list til;
1600 this->identifier_list(&til);
1602 Location location = this->location();
1604 Type* type = NULL;
1605 Expression_list* init = NULL;
1606 if (!this->peek_token()->is_op(OPERATOR_EQ))
1608 type = this->type();
1609 if (type->is_error_type())
1611 this->gogo_->mark_locals_used();
1612 while (!this->peek_token()->is_op(OPERATOR_EQ)
1613 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1614 && !this->peek_token()->is_eof())
1615 this->advance_token();
1617 if (this->peek_token()->is_op(OPERATOR_EQ))
1619 this->advance_token();
1620 init = this->expression_list(NULL, false, true);
1623 else
1625 this->advance_token();
1626 init = this->expression_list(NULL, false, true);
1629 this->init_vars(&til, type, init, false, location);
1631 if (init != NULL)
1632 delete init;
1635 // Create variables. TIL is a list of variable names. If TYPE is not
1636 // NULL, it is the type of all the variables. If INIT is not NULL, it
1637 // is an initializer list for the variables.
1639 void
1640 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1641 Expression_list* init, bool is_coloneq,
1642 Location location)
1644 // Check for an initialization which can yield multiple values.
1645 if (init != NULL && init->size() == 1 && til->size() > 1)
1647 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1648 location))
1649 return;
1650 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1651 location))
1652 return;
1653 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1654 location))
1655 return;
1656 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1657 is_coloneq, location))
1658 return;
1661 if (init != NULL && init->size() != til->size())
1663 if (init->empty() || !init->front()->is_error_expression())
1664 error_at(location, "wrong number of initializations");
1665 init = NULL;
1666 if (type == NULL)
1667 type = Type::make_error_type();
1670 // Note that INIT was already parsed with the old name bindings, so
1671 // we don't have to worry that it will accidentally refer to the
1672 // newly declared variables. But we do have to worry about a mix of
1673 // newly declared variables and old variables if the old variables
1674 // appear in the initializations.
1676 Expression_list::const_iterator pexpr;
1677 if (init != NULL)
1678 pexpr = init->begin();
1679 bool any_new = false;
1680 Expression_list* vars = new Expression_list();
1681 Expression_list* vals = new Expression_list();
1682 for (Typed_identifier_list::const_iterator p = til->begin();
1683 p != til->end();
1684 ++p)
1686 if (init != NULL)
1687 go_assert(pexpr != init->end());
1688 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1689 false, &any_new, vars, vals);
1690 if (init != NULL)
1691 ++pexpr;
1693 if (init != NULL)
1694 go_assert(pexpr == init->end());
1695 if (is_coloneq && !any_new)
1696 error_at(location, "variables redeclared but no variable is new");
1697 this->finish_init_vars(vars, vals, location);
1700 // See if we need to initialize a list of variables from a function
1701 // call. This returns true if we have set up the variables and the
1702 // initialization.
1704 bool
1705 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1706 Expression* expr, bool is_coloneq,
1707 Location location)
1709 Call_expression* call = expr->call_expression();
1710 if (call == NULL)
1711 return false;
1713 // This is a function call. We can't check here whether it returns
1714 // the right number of values, but it might. Declare the variables,
1715 // and then assign the results of the call to them.
1717 call->set_expected_result_count(vars->size());
1719 Named_object* first_var = NULL;
1720 unsigned int index = 0;
1721 bool any_new = false;
1722 Expression_list* ivars = new Expression_list();
1723 Expression_list* ivals = new Expression_list();
1724 for (Typed_identifier_list::const_iterator pv = vars->begin();
1725 pv != vars->end();
1726 ++pv, ++index)
1728 Expression* init = Expression::make_call_result(call, index);
1729 Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
1730 &any_new, ivars, ivals);
1732 if (this->gogo_->in_global_scope() && no->is_variable())
1734 if (first_var == NULL)
1735 first_var = no;
1736 else
1738 // The subsequent vars have an implicit dependency on
1739 // the first one, so that everything gets initialized in
1740 // the right order and so that we detect cycles
1741 // correctly.
1742 this->gogo_->record_var_depends_on(no->var_value(), first_var);
1747 if (is_coloneq && !any_new)
1748 error_at(location, "variables redeclared but no variable is new");
1750 this->finish_init_vars(ivars, ivals, location);
1752 return true;
1755 // See if we need to initialize a pair of values from a map index
1756 // expression. This returns true if we have set up the variables and
1757 // the initialization.
1759 bool
1760 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1761 Expression* expr, bool is_coloneq,
1762 Location location)
1764 Index_expression* index = expr->index_expression();
1765 if (index == NULL)
1766 return false;
1767 if (vars->size() != 2)
1768 return false;
1770 // This is an index which is being assigned to two variables. It
1771 // must be a map index. Declare the variables, and then assign the
1772 // results of the map index.
1773 bool any_new = false;
1774 Typed_identifier_list::const_iterator p = vars->begin();
1775 Expression* init = type == NULL ? index : NULL;
1776 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1777 type == NULL, &any_new, NULL, NULL);
1778 if (type == NULL && any_new && val_no->is_variable())
1779 val_no->var_value()->set_type_from_init_tuple();
1780 Expression* val_var = Expression::make_var_reference(val_no, location);
1782 ++p;
1783 Type* var_type = type;
1784 if (var_type == NULL)
1785 var_type = Type::lookup_bool_type();
1786 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1787 &any_new, NULL, NULL);
1788 Expression* present_var = Expression::make_var_reference(no, location);
1790 if (is_coloneq && !any_new)
1791 error_at(location, "variables redeclared but no variable is new");
1793 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1794 index, location);
1796 if (!this->gogo_->in_global_scope())
1797 this->gogo_->add_statement(s);
1798 else if (!val_no->is_sink())
1800 if (val_no->is_variable())
1801 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1803 else if (!no->is_sink())
1805 if (no->is_variable())
1806 no->var_value()->add_preinit_statement(this->gogo_, s);
1808 else
1810 // Execute the map index expression just so that we can fail if
1811 // the map is nil.
1812 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1813 NULL, location);
1814 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1817 return true;
1820 // See if we need to initialize a pair of values from a receive
1821 // expression. This returns true if we have set up the variables and
1822 // the initialization.
1824 bool
1825 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1826 Expression* expr, bool is_coloneq,
1827 Location location)
1829 Receive_expression* receive = expr->receive_expression();
1830 if (receive == NULL)
1831 return false;
1832 if (vars->size() != 2)
1833 return false;
1835 // This is a receive expression which is being assigned to two
1836 // variables. Declare the variables, and then assign the results of
1837 // the receive.
1838 bool any_new = false;
1839 Typed_identifier_list::const_iterator p = vars->begin();
1840 Expression* init = type == NULL ? receive : NULL;
1841 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1842 type == NULL, &any_new, NULL, NULL);
1843 if (type == NULL && any_new && val_no->is_variable())
1844 val_no->var_value()->set_type_from_init_tuple();
1845 Expression* val_var = Expression::make_var_reference(val_no, location);
1847 ++p;
1848 Type* var_type = type;
1849 if (var_type == NULL)
1850 var_type = Type::lookup_bool_type();
1851 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1852 &any_new, NULL, NULL);
1853 Expression* received_var = Expression::make_var_reference(no, location);
1855 if (is_coloneq && !any_new)
1856 error_at(location, "variables redeclared but no variable is new");
1858 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1859 received_var,
1860 receive->channel(),
1861 location);
1863 if (!this->gogo_->in_global_scope())
1864 this->gogo_->add_statement(s);
1865 else if (!val_no->is_sink())
1867 if (val_no->is_variable())
1868 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1870 else if (!no->is_sink())
1872 if (no->is_variable())
1873 no->var_value()->add_preinit_statement(this->gogo_, s);
1875 else
1877 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1878 NULL, location);
1879 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1882 return true;
1885 // See if we need to initialize a pair of values from a type guard
1886 // expression. This returns true if we have set up the variables and
1887 // the initialization.
1889 bool
1890 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1891 Type* type, Expression* expr,
1892 bool is_coloneq, Location location)
1894 Type_guard_expression* type_guard = expr->type_guard_expression();
1895 if (type_guard == NULL)
1896 return false;
1897 if (vars->size() != 2)
1898 return false;
1900 // This is a type guard expression which is being assigned to two
1901 // variables. Declare the variables, and then assign the results of
1902 // the type guard.
1903 bool any_new = false;
1904 Typed_identifier_list::const_iterator p = vars->begin();
1905 Type* var_type = type;
1906 if (var_type == NULL)
1907 var_type = type_guard->type();
1908 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1909 &any_new, NULL, NULL);
1910 Expression* val_var = Expression::make_var_reference(val_no, location);
1912 ++p;
1913 var_type = type;
1914 if (var_type == NULL)
1915 var_type = Type::lookup_bool_type();
1916 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1917 &any_new, NULL, NULL);
1918 Expression* ok_var = Expression::make_var_reference(no, location);
1920 Expression* texpr = type_guard->expr();
1921 Type* t = type_guard->type();
1922 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1923 texpr, t,
1924 location);
1926 if (is_coloneq && !any_new)
1927 error_at(location, "variables redeclared but no variable is new");
1929 if (!this->gogo_->in_global_scope())
1930 this->gogo_->add_statement(s);
1931 else if (!val_no->is_sink())
1933 if (val_no->is_variable())
1934 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1936 else if (!no->is_sink())
1938 if (no->is_variable())
1939 no->var_value()->add_preinit_statement(this->gogo_, s);
1941 else
1943 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1944 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1947 return true;
1950 // Create a single variable. If IS_COLONEQ is true, we permit
1951 // redeclarations in the same block, and we set *IS_NEW when we find a
1952 // new variable which is not a redeclaration.
1954 Named_object*
1955 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1956 bool is_coloneq, bool type_from_init, bool* is_new,
1957 Expression_list* vars, Expression_list* vals)
1959 Location location = tid.location();
1961 if (Gogo::is_sink_name(tid.name()))
1963 if (!type_from_init && init != NULL)
1965 if (this->gogo_->in_global_scope())
1966 return this->create_dummy_global(type, init, location);
1967 else
1969 // Create a dummy variable so that we will check whether the
1970 // initializer can be assigned to the type.
1971 Variable* var = new Variable(type, init, false, false, false,
1972 location);
1973 var->set_is_used();
1974 static int count;
1975 char buf[30];
1976 snprintf(buf, sizeof buf, "sink$%d", count);
1977 ++count;
1978 return this->gogo_->add_variable(buf, var);
1981 if (type != NULL)
1982 this->gogo_->add_type_to_verify(type);
1983 return this->gogo_->add_sink();
1986 if (is_coloneq)
1988 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1989 if (no != NULL
1990 && (no->is_variable() || no->is_result_variable()))
1992 // INIT may be NULL even when IS_COLONEQ is true for cases
1993 // like v, ok := x.(int).
1994 if (!type_from_init && init != NULL)
1996 go_assert(vars != NULL && vals != NULL);
1997 vars->push_back(Expression::make_var_reference(no, location));
1998 vals->push_back(init);
2000 return no;
2003 *is_new = true;
2004 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
2005 false, false, location);
2006 Named_object* no = this->gogo_->add_variable(tid.name(), var);
2007 if (!no->is_variable())
2009 // The name is already defined, so we just gave an error.
2010 return this->gogo_->add_sink();
2012 return no;
2015 // Create a dummy global variable to force an initializer to be run in
2016 // the right place. This is used when a sink variable is initialized
2017 // at global scope.
2019 Named_object*
2020 Parse::create_dummy_global(Type* type, Expression* init,
2021 Location location)
2023 if (type == NULL && init == NULL)
2024 type = Type::lookup_bool_type();
2025 Variable* var = new Variable(type, init, true, false, false, location);
2026 static int count;
2027 char buf[30];
2028 snprintf(buf, sizeof buf, "_.%d", count);
2029 ++count;
2030 return this->gogo_->add_variable(buf, var);
2033 // Finish the variable initialization by executing any assignments to
2034 // existing variables when using :=. These must be done as a tuple
2035 // assignment in case of something like n, a, b := 1, b, a.
2037 void
2038 Parse::finish_init_vars(Expression_list* vars, Expression_list* vals,
2039 Location location)
2041 if (vars->empty())
2043 delete vars;
2044 delete vals;
2046 else if (vars->size() == 1)
2048 go_assert(!this->gogo_->in_global_scope());
2049 this->gogo_->add_statement(Statement::make_assignment(vars->front(),
2050 vals->front(),
2051 location));
2052 delete vars;
2053 delete vals;
2055 else
2057 go_assert(!this->gogo_->in_global_scope());
2058 this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals,
2059 location));
2063 // SimpleVarDecl = identifier ":=" Expression .
2065 // We've already seen the identifier.
2067 // FIXME: We also have to implement
2068 // IdentifierList ":=" ExpressionList
2069 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2070 // tuple assignments here as well.
2072 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2073 // side may be a composite literal.
2075 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2076 // RangeClause.
2078 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2079 // guard (var := expr.("type") using the literal keyword "type").
2081 void
2082 Parse::simple_var_decl_or_assignment(const std::string& name,
2083 Location location,
2084 bool may_be_composite_lit,
2085 Range_clause* p_range_clause,
2086 Type_switch* p_type_switch)
2088 Typed_identifier_list til;
2089 til.push_back(Typed_identifier(name, NULL, location));
2091 std::set<std::string> uniq_idents;
2092 uniq_idents.insert(name);
2094 // We've seen one identifier. If we see a comma now, this could be
2095 // "a, *p = 1, 2".
2096 if (this->peek_token()->is_op(OPERATOR_COMMA))
2098 go_assert(p_type_switch == NULL);
2099 while (true)
2101 const Token* token = this->advance_token();
2102 if (!token->is_identifier())
2103 break;
2105 std::string id = token->identifier();
2106 bool is_id_exported = token->is_identifier_exported();
2107 Location id_location = token->location();
2108 std::pair<std::set<std::string>::iterator, bool> ins;
2110 token = this->advance_token();
2111 if (!token->is_op(OPERATOR_COMMA))
2113 if (token->is_op(OPERATOR_COLONEQ))
2115 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2116 ins = uniq_idents.insert(id);
2117 if (!ins.second && !Gogo::is_sink_name(id))
2118 error_at(id_location, "multiple assignments to %s",
2119 Gogo::message_name(id).c_str());
2120 til.push_back(Typed_identifier(id, NULL, location));
2122 else
2123 this->unget_token(Token::make_identifier_token(id,
2124 is_id_exported,
2125 id_location));
2126 break;
2129 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2130 ins = uniq_idents.insert(id);
2131 if (!ins.second && !Gogo::is_sink_name(id))
2132 error_at(id_location, "multiple assignments to %s",
2133 Gogo::message_name(id).c_str());
2134 til.push_back(Typed_identifier(id, NULL, location));
2137 // We have a comma separated list of identifiers in TIL. If the
2138 // next token is COLONEQ, then this is a simple var decl, and we
2139 // have the complete list of identifiers. If the next token is
2140 // not COLONEQ, then the only valid parse is a tuple assignment.
2141 // The list of identifiers we have so far is really a list of
2142 // expressions. There are more expressions following.
2144 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2146 Expression_list* exprs = new Expression_list;
2147 for (Typed_identifier_list::const_iterator p = til.begin();
2148 p != til.end();
2149 ++p)
2150 exprs->push_back(this->id_to_expression(p->name(), p->location(),
2151 true));
2153 Expression_list* more_exprs =
2154 this->expression_list(NULL, true, may_be_composite_lit);
2155 for (Expression_list::const_iterator p = more_exprs->begin();
2156 p != more_exprs->end();
2157 ++p)
2158 exprs->push_back(*p);
2159 delete more_exprs;
2161 this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
2162 return;
2166 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2167 const Token* token = this->advance_token();
2169 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2171 this->range_clause_decl(&til, p_range_clause);
2172 return;
2175 Expression_list* init;
2176 if (p_type_switch == NULL)
2177 init = this->expression_list(NULL, false, may_be_composite_lit);
2178 else
2180 bool is_type_switch = false;
2181 Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
2182 may_be_composite_lit,
2183 &is_type_switch, NULL);
2184 if (is_type_switch)
2186 p_type_switch->found = true;
2187 p_type_switch->name = name;
2188 p_type_switch->location = location;
2189 p_type_switch->expr = expr;
2190 return;
2193 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2195 init = new Expression_list();
2196 init->push_back(expr);
2198 else
2200 this->advance_token();
2201 init = this->expression_list(expr, false, may_be_composite_lit);
2205 this->init_vars(&til, NULL, init, true, location);
2208 // FunctionDecl = "func" identifier Signature [ Block ] .
2209 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2211 // Deprecated gcc extension:
2212 // FunctionDecl = "func" identifier Signature
2213 // __asm__ "(" string_lit ")" .
2214 // This extension means a function whose real name is the identifier
2215 // inside the asm. This extension will be removed at some future
2216 // date. It has been replaced with //extern comments.
2218 // SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment,
2219 // which means that we omit the method from the type descriptor.
2221 void
2222 Parse::function_decl(bool saw_nointerface)
2224 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2225 Location location = this->location();
2226 std::string extern_name = this->lex_->extern_name();
2227 const Token* token = this->advance_token();
2229 Typed_identifier* rec = NULL;
2230 if (token->is_op(OPERATOR_LPAREN))
2232 rec = this->receiver();
2233 token = this->peek_token();
2235 else if (saw_nointerface)
2237 warning_at(location, 0,
2238 "ignoring magic //go:nointerface comment before non-method");
2239 saw_nointerface = false;
2242 if (!token->is_identifier())
2244 error_at(this->location(), "expected function name");
2245 return;
2248 std::string name =
2249 this->gogo_->pack_hidden_name(token->identifier(),
2250 token->is_identifier_exported());
2252 this->advance_token();
2254 Function_type* fntype = this->signature(rec, this->location());
2256 Named_object* named_object = NULL;
2258 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2260 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2262 error_at(this->location(), "expected %<(%>");
2263 return;
2265 token = this->advance_token();
2266 if (!token->is_string())
2268 error_at(this->location(), "expected string");
2269 return;
2271 std::string asm_name = token->string_value();
2272 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2274 error_at(this->location(), "expected %<)%>");
2275 return;
2277 this->advance_token();
2278 if (!Gogo::is_sink_name(name))
2280 named_object = this->gogo_->declare_function(name, fntype, location);
2281 if (named_object->is_function_declaration())
2282 named_object->func_declaration_value()->set_asm_name(asm_name);
2286 // Check for the easy error of a newline before the opening brace.
2287 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2289 Location semi_loc = this->location();
2290 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2291 error_at(this->location(),
2292 "unexpected semicolon or newline before %<{%>");
2293 else
2294 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2295 semi_loc));
2298 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2300 if (named_object == NULL && !Gogo::is_sink_name(name))
2302 if (fntype == NULL)
2303 this->gogo_->add_erroneous_name(name);
2304 else
2306 named_object = this->gogo_->declare_function(name, fntype,
2307 location);
2308 if (!extern_name.empty()
2309 && named_object->is_function_declaration())
2311 Function_declaration* fd =
2312 named_object->func_declaration_value();
2313 fd->set_asm_name(extern_name);
2318 if (saw_nointerface)
2319 warning_at(location, 0,
2320 ("ignoring magic //go:nointerface comment "
2321 "before declaration"));
2323 else
2325 bool hold_is_erroneous_function = this->is_erroneous_function_;
2326 if (fntype == NULL)
2328 fntype = Type::make_function_type(NULL, NULL, NULL, location);
2329 this->is_erroneous_function_ = true;
2330 if (!Gogo::is_sink_name(name))
2331 this->gogo_->add_erroneous_name(name);
2332 name = this->gogo_->pack_hidden_name("_", false);
2334 named_object = this->gogo_->start_function(name, fntype, true, location);
2335 Location end_loc = this->block();
2336 this->gogo_->finish_function(end_loc);
2337 if (saw_nointerface
2338 && !this->is_erroneous_function_
2339 && named_object->is_function())
2340 named_object->func_value()->set_nointerface();
2341 this->is_erroneous_function_ = hold_is_erroneous_function;
2345 // Receiver = Parameters .
2347 Typed_identifier*
2348 Parse::receiver()
2350 Location location = this->location();
2351 Typed_identifier_list* til;
2352 if (!this->parameters(&til, NULL))
2353 return NULL;
2354 else if (til == NULL || til->empty())
2356 error_at(location, "method has no receiver");
2357 return NULL;
2359 else if (til->size() > 1)
2361 error_at(location, "method has multiple receivers");
2362 return NULL;
2364 else
2365 return &til->front();
2368 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2369 // Literal = BasicLit | CompositeLit | FunctionLit .
2370 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2372 // If MAY_BE_SINK is true, this operand may be "_".
2374 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2375 // if the entire expression is in parentheses.
2377 Expression*
2378 Parse::operand(bool may_be_sink, bool* is_parenthesized)
2380 const Token* token = this->peek_token();
2381 Expression* ret;
2382 switch (token->classification())
2384 case Token::TOKEN_IDENTIFIER:
2386 Location location = token->location();
2387 std::string id = token->identifier();
2388 bool is_exported = token->is_identifier_exported();
2389 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2391 Named_object* in_function;
2392 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2394 Package* package = NULL;
2395 if (named_object != NULL && named_object->is_package())
2397 if (!this->advance_token()->is_op(OPERATOR_DOT)
2398 || !this->advance_token()->is_identifier())
2400 error_at(location, "unexpected reference to package");
2401 return Expression::make_error(location);
2403 package = named_object->package_value();
2404 package->set_used();
2405 id = this->peek_token()->identifier();
2406 is_exported = this->peek_token()->is_identifier_exported();
2407 packed = this->gogo_->pack_hidden_name(id, is_exported);
2408 named_object = package->lookup(packed);
2409 location = this->location();
2410 go_assert(in_function == NULL);
2413 this->advance_token();
2415 if (named_object != NULL
2416 && named_object->is_type()
2417 && !named_object->type_value()->is_visible())
2419 go_assert(package != NULL);
2420 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2421 Gogo::message_name(package->package_name()).c_str(),
2422 Gogo::message_name(id).c_str());
2423 return Expression::make_error(location);
2427 if (named_object == NULL)
2429 if (package != NULL)
2431 std::string n1 = Gogo::message_name(package->package_name());
2432 std::string n2 = Gogo::message_name(id);
2433 if (!is_exported)
2434 error_at(location,
2435 ("invalid reference to unexported identifier "
2436 "%<%s.%s%>"),
2437 n1.c_str(), n2.c_str());
2438 else
2439 error_at(location,
2440 "reference to undefined identifier %<%s.%s%>",
2441 n1.c_str(), n2.c_str());
2442 return Expression::make_error(location);
2445 named_object = this->gogo_->add_unknown_name(packed, location);
2448 if (in_function != NULL
2449 && in_function != this->gogo_->current_function()
2450 && (named_object->is_variable()
2451 || named_object->is_result_variable()))
2452 return this->enclosing_var_reference(in_function, named_object,
2453 location);
2455 switch (named_object->classification())
2457 case Named_object::NAMED_OBJECT_CONST:
2458 return Expression::make_const_reference(named_object, location);
2459 case Named_object::NAMED_OBJECT_TYPE:
2460 return Expression::make_type(named_object->type_value(), location);
2461 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2463 Type* t = Type::make_forward_declaration(named_object);
2464 return Expression::make_type(t, location);
2466 case Named_object::NAMED_OBJECT_VAR:
2467 case Named_object::NAMED_OBJECT_RESULT_VAR:
2468 // Any left-hand-side can be a sink, so if this can not be
2469 // a sink, then it must be a use of the variable.
2470 if (!may_be_sink)
2471 this->mark_var_used(named_object);
2472 return Expression::make_var_reference(named_object, location);
2473 case Named_object::NAMED_OBJECT_SINK:
2474 if (may_be_sink)
2475 return Expression::make_sink(location);
2476 else
2478 error_at(location, "cannot use _ as value");
2479 return Expression::make_error(location);
2481 case Named_object::NAMED_OBJECT_FUNC:
2482 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2483 return Expression::make_func_reference(named_object, NULL,
2484 location);
2485 case Named_object::NAMED_OBJECT_UNKNOWN:
2487 Unknown_expression* ue =
2488 Expression::make_unknown_reference(named_object, location);
2489 if (this->is_erroneous_function_)
2490 ue->set_no_error_message();
2491 return ue;
2493 case Named_object::NAMED_OBJECT_ERRONEOUS:
2494 return Expression::make_error(location);
2495 default:
2496 go_unreachable();
2499 go_unreachable();
2501 case Token::TOKEN_STRING:
2502 ret = Expression::make_string(token->string_value(), token->location());
2503 this->advance_token();
2504 return ret;
2506 case Token::TOKEN_CHARACTER:
2507 ret = Expression::make_character(token->character_value(), NULL,
2508 token->location());
2509 this->advance_token();
2510 return ret;
2512 case Token::TOKEN_INTEGER:
2513 ret = Expression::make_integer(token->integer_value(), NULL,
2514 token->location());
2515 this->advance_token();
2516 return ret;
2518 case Token::TOKEN_FLOAT:
2519 ret = Expression::make_float(token->float_value(), NULL,
2520 token->location());
2521 this->advance_token();
2522 return ret;
2524 case Token::TOKEN_IMAGINARY:
2526 mpfr_t zero;
2527 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2528 ret = Expression::make_complex(&zero, token->imaginary_value(),
2529 NULL, token->location());
2530 mpfr_clear(zero);
2531 this->advance_token();
2532 return ret;
2535 case Token::TOKEN_KEYWORD:
2536 switch (token->keyword())
2538 case KEYWORD_FUNC:
2539 return this->function_lit();
2540 case KEYWORD_CHAN:
2541 case KEYWORD_INTERFACE:
2542 case KEYWORD_MAP:
2543 case KEYWORD_STRUCT:
2545 Location location = token->location();
2546 return Expression::make_type(this->type(), location);
2548 default:
2549 break;
2551 break;
2553 case Token::TOKEN_OPERATOR:
2554 if (token->is_op(OPERATOR_LPAREN))
2556 this->advance_token();
2557 ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL,
2558 NULL);
2559 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2560 error_at(this->location(), "missing %<)%>");
2561 else
2562 this->advance_token();
2563 if (is_parenthesized != NULL)
2564 *is_parenthesized = true;
2565 return ret;
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);
2574 break;
2576 default:
2577 break;
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.
2589 Expression*
2590 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2591 Location location)
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 // The last argument to the Enclosing_var constructor is the index
2601 // of this variable in the closure. We add 1 to the current number
2602 // of enclosed variables, because the first field in the closure
2603 // points to the function code.
2604 Enclosing_var ev(var, in_function, this->enclosing_vars_.size() + 1);
2605 std::pair<Enclosing_vars::iterator, bool> ins =
2606 this->enclosing_vars_.insert(ev);
2607 if (ins.second)
2609 // This is a variable we have not seen before. Add a new field
2610 // to the closure type.
2611 this_function->func_value()->add_closure_field(var, location);
2614 Expression* closure_ref = Expression::make_var_reference(closure,
2615 location);
2616 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2618 // The closure structure holds pointers to the variables, so we need
2619 // to introduce an indirection.
2620 Expression* e = Expression::make_field_reference(closure_ref,
2621 ins.first->index(),
2622 location);
2623 e = Expression::make_unary(OPERATOR_MULT, e, location);
2624 return e;
2627 // CompositeLit = LiteralType LiteralValue .
2628 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2629 // SliceType | MapType | TypeName .
2630 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2631 // ElementList = Element { "," Element } .
2632 // Element = [ Key ":" ] Value .
2633 // Key = FieldName | ElementIndex .
2634 // FieldName = identifier .
2635 // ElementIndex = Expression .
2636 // Value = Expression | LiteralValue .
2638 // We have already seen the type if there is one, and we are now
2639 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2640 // will be seen here as an array type whose length is "nil". The
2641 // DEPTH parameter is non-zero if this is an embedded composite
2642 // literal and the type was omitted. It gives the number of steps up
2643 // to the type which was provided. E.g., in [][]int{{1}} it will be
2644 // 1. In [][][]int{{{1}}} it will be 2.
2646 Expression*
2647 Parse::composite_lit(Type* type, int depth, Location location)
2649 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2650 this->advance_token();
2652 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2654 this->advance_token();
2655 return Expression::make_composite_literal(type, depth, false, NULL,
2656 false, location);
2659 bool has_keys = false;
2660 bool all_are_names = true;
2661 Expression_list* vals = new Expression_list;
2662 while (true)
2664 Expression* val;
2665 bool is_type_omitted = false;
2666 bool is_name = false;
2668 const Token* token = this->peek_token();
2670 if (token->is_identifier())
2672 std::string identifier = token->identifier();
2673 bool is_exported = token->is_identifier_exported();
2674 Location location = token->location();
2676 if (this->advance_token()->is_op(OPERATOR_COLON))
2678 // This may be a field name. We don't know for sure--it
2679 // could also be an expression for an array index. We
2680 // don't want to parse it as an expression because may
2681 // trigger various errors, e.g., if this identifier
2682 // happens to be the name of a package.
2683 Gogo* gogo = this->gogo_;
2684 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2685 is_exported),
2686 location, false);
2687 is_name = true;
2689 else
2691 this->unget_token(Token::make_identifier_token(identifier,
2692 is_exported,
2693 location));
2694 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
2695 NULL);
2698 else if (!token->is_op(OPERATOR_LCURLY))
2699 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2700 else
2702 // This must be a composite literal inside another composite
2703 // literal, with the type omitted for the inner one.
2704 val = this->composite_lit(type, depth + 1, token->location());
2705 is_type_omitted = true;
2708 token = this->peek_token();
2709 if (!token->is_op(OPERATOR_COLON))
2711 if (has_keys)
2712 vals->push_back(NULL);
2713 is_name = false;
2715 else
2717 if (is_type_omitted && !val->is_error_expression())
2719 error_at(this->location(), "unexpected %<:%>");
2720 val = Expression::make_error(this->location());
2723 this->advance_token();
2725 if (!has_keys && !vals->empty())
2727 Expression_list* newvals = new Expression_list;
2728 for (Expression_list::const_iterator p = vals->begin();
2729 p != vals->end();
2730 ++p)
2732 newvals->push_back(NULL);
2733 newvals->push_back(*p);
2735 delete vals;
2736 vals = newvals;
2738 has_keys = true;
2740 if (val->unknown_expression() != NULL)
2741 val->unknown_expression()->set_is_composite_literal_key();
2743 vals->push_back(val);
2745 if (!token->is_op(OPERATOR_LCURLY))
2746 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2747 else
2749 // This must be a composite literal inside another
2750 // composite literal, with the type omitted for the
2751 // inner one.
2752 val = this->composite_lit(type, depth + 1, token->location());
2755 token = this->peek_token();
2758 vals->push_back(val);
2760 if (!is_name)
2761 all_are_names = false;
2763 if (token->is_op(OPERATOR_COMMA))
2765 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2767 this->advance_token();
2768 break;
2771 else if (token->is_op(OPERATOR_RCURLY))
2773 this->advance_token();
2774 break;
2776 else
2778 if (token->is_op(OPERATOR_SEMICOLON))
2779 error_at(this->location(),
2780 "need trailing comma before newline in composite literal");
2781 else
2782 error_at(this->location(), "expected %<,%> or %<}%>");
2784 this->gogo_->mark_locals_used();
2785 int depth = 0;
2786 while (!token->is_eof()
2787 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2789 if (token->is_op(OPERATOR_LCURLY))
2790 ++depth;
2791 else if (token->is_op(OPERATOR_RCURLY))
2792 --depth;
2793 token = this->advance_token();
2795 if (token->is_op(OPERATOR_RCURLY))
2796 this->advance_token();
2798 return Expression::make_error(location);
2802 return Expression::make_composite_literal(type, depth, has_keys, vals,
2803 all_are_names, location);
2806 // FunctionLit = "func" Signature Block .
2808 Expression*
2809 Parse::function_lit()
2811 Location location = this->location();
2812 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2813 this->advance_token();
2815 Enclosing_vars hold_enclosing_vars;
2816 hold_enclosing_vars.swap(this->enclosing_vars_);
2818 Function_type* type = this->signature(NULL, location);
2819 bool fntype_is_error = false;
2820 if (type == NULL)
2822 type = Type::make_function_type(NULL, NULL, NULL, location);
2823 fntype_is_error = true;
2826 // For a function literal, the next token must be a '{'. If we
2827 // don't see that, then we may have a type expression.
2828 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2830 hold_enclosing_vars.swap(this->enclosing_vars_);
2831 return Expression::make_type(type, location);
2834 bool hold_is_erroneous_function = this->is_erroneous_function_;
2835 if (fntype_is_error)
2836 this->is_erroneous_function_ = true;
2838 Bc_stack* hold_break_stack = this->break_stack_;
2839 Bc_stack* hold_continue_stack = this->continue_stack_;
2840 this->break_stack_ = NULL;
2841 this->continue_stack_ = NULL;
2843 Named_object* no = this->gogo_->start_function("", type, true, location);
2845 Location end_loc = this->block();
2847 this->gogo_->finish_function(end_loc);
2849 if (this->break_stack_ != NULL)
2850 delete this->break_stack_;
2851 if (this->continue_stack_ != NULL)
2852 delete this->continue_stack_;
2853 this->break_stack_ = hold_break_stack;
2854 this->continue_stack_ = hold_continue_stack;
2856 this->is_erroneous_function_ = hold_is_erroneous_function;
2858 hold_enclosing_vars.swap(this->enclosing_vars_);
2860 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2861 location);
2863 return Expression::make_func_reference(no, closure, location);
2866 // Create a closure for the nested function FUNCTION. This is based
2867 // on ENCLOSING_VARS, which is a list of all variables defined in
2868 // enclosing functions and referenced from FUNCTION. A closure is the
2869 // address of a struct which point to the real function code and
2870 // contains the addresses of all the referenced variables. This
2871 // returns NULL if no closure is required.
2873 Expression*
2874 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2875 Location location)
2877 if (enclosing_vars->empty())
2878 return NULL;
2880 // Get the variables in order by their field index.
2882 size_t enclosing_var_count = enclosing_vars->size();
2883 std::vector<Enclosing_var> ev(enclosing_var_count);
2884 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2885 p != enclosing_vars->end();
2886 ++p)
2888 // Subtract 1 because index 0 is the function code.
2889 ev[p->index() - 1] = *p;
2892 // Build an initializer for a composite literal of the closure's
2893 // type.
2895 Named_object* enclosing_function = this->gogo_->current_function();
2896 Expression_list* initializer = new Expression_list;
2898 initializer->push_back(Expression::make_func_code_reference(function,
2899 location));
2901 for (size_t i = 0; i < enclosing_var_count; ++i)
2903 // Add 1 to i because the first field in the closure is a
2904 // pointer to the function code.
2905 go_assert(ev[i].index() == i + 1);
2906 Named_object* var = ev[i].var();
2907 Expression* ref;
2908 if (ev[i].in_function() == enclosing_function)
2909 ref = Expression::make_var_reference(var, location);
2910 else
2911 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2912 location);
2913 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2914 location);
2915 initializer->push_back(refaddr);
2918 Named_object* closure_var = function->func_value()->closure_var();
2919 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2920 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2921 location);
2922 return Expression::make_heap_expression(cv, location);
2925 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2927 // If MAY_BE_SINK is true, this expression may be "_".
2929 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2930 // literal.
2932 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2933 // guard (var := expr.("type") using the literal keyword "type").
2935 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2936 // if the entire expression is in parentheses.
2938 Expression*
2939 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2940 bool* is_type_switch, bool* is_parenthesized)
2942 Location start_loc = this->location();
2943 bool operand_is_parenthesized = false;
2944 bool whole_is_parenthesized = false;
2946 Expression* ret = this->operand(may_be_sink, &operand_is_parenthesized);
2948 whole_is_parenthesized = operand_is_parenthesized;
2950 // An unknown name followed by a curly brace must be a composite
2951 // literal, and the unknown name must be a type.
2952 if (may_be_composite_lit
2953 && !operand_is_parenthesized
2954 && ret->unknown_expression() != NULL
2955 && this->peek_token()->is_op(OPERATOR_LCURLY))
2957 Named_object* no = ret->unknown_expression()->named_object();
2958 Type* type = Type::make_forward_declaration(no);
2959 ret = Expression::make_type(type, ret->location());
2962 // We handle composite literals and type casts here, as it is the
2963 // easiest way to handle types which are in parentheses, as in
2964 // "((uint))(1)".
2965 if (ret->is_type_expression())
2967 if (this->peek_token()->is_op(OPERATOR_LCURLY))
2969 whole_is_parenthesized = false;
2970 if (!may_be_composite_lit)
2972 Type* t = ret->type();
2973 if (t->named_type() != NULL
2974 || t->forward_declaration_type() != NULL)
2975 error_at(start_loc,
2976 _("parentheses required around this composite literal "
2977 "to avoid parsing ambiguity"));
2979 else if (operand_is_parenthesized)
2980 error_at(start_loc,
2981 "cannot parenthesize type in composite literal");
2982 ret = this->composite_lit(ret->type(), 0, ret->location());
2984 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
2986 whole_is_parenthesized = false;
2987 Location loc = this->location();
2988 this->advance_token();
2989 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
2990 NULL, NULL);
2991 if (this->peek_token()->is_op(OPERATOR_COMMA))
2992 this->advance_token();
2993 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
2995 error_at(this->location(),
2996 "invalid use of %<...%> in type conversion");
2997 this->advance_token();
2999 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3000 error_at(this->location(), "expected %<)%>");
3001 else
3002 this->advance_token();
3003 if (expr->is_error_expression())
3004 ret = expr;
3005 else
3007 Type* t = ret->type();
3008 if (t->classification() == Type::TYPE_ARRAY
3009 && t->array_type()->length() != NULL
3010 && t->array_type()->length()->is_nil_expression())
3012 error_at(ret->location(),
3013 "use of %<[...]%> outside of array literal");
3014 ret = Expression::make_error(loc);
3016 else
3017 ret = Expression::make_cast(t, expr, loc);
3022 while (true)
3024 const Token* token = this->peek_token();
3025 if (token->is_op(OPERATOR_LPAREN))
3027 whole_is_parenthesized = false;
3028 ret = this->call(this->verify_not_sink(ret));
3030 else if (token->is_op(OPERATOR_DOT))
3032 whole_is_parenthesized = false;
3033 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
3034 if (is_type_switch != NULL && *is_type_switch)
3035 break;
3037 else if (token->is_op(OPERATOR_LSQUARE))
3039 whole_is_parenthesized = false;
3040 ret = this->index(this->verify_not_sink(ret));
3042 else
3043 break;
3046 if (whole_is_parenthesized && is_parenthesized != NULL)
3047 *is_parenthesized = true;
3049 return ret;
3052 // Selector = "." identifier .
3053 // TypeGuard = "." "(" QualifiedIdent ")" .
3055 // Note that Operand can expand to QualifiedIdent, which contains a
3056 // ".". That is handled directly in operand when it sees a package
3057 // name.
3059 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3060 // guard (var := expr.("type") using the literal keyword "type").
3062 Expression*
3063 Parse::selector(Expression* left, bool* is_type_switch)
3065 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
3066 Location location = this->location();
3068 const Token* token = this->advance_token();
3069 if (token->is_identifier())
3071 // This could be a field in a struct, or a method in an
3072 // interface, or a method associated with a type. We can't know
3073 // which until we have seen all the types.
3074 std::string name =
3075 this->gogo_->pack_hidden_name(token->identifier(),
3076 token->is_identifier_exported());
3077 if (token->identifier() == "_")
3079 error_at(this->location(), "invalid use of %<_%>");
3080 name = Gogo::erroneous_name();
3082 this->advance_token();
3083 return Expression::make_selector(left, name, location);
3085 else if (token->is_op(OPERATOR_LPAREN))
3087 this->advance_token();
3088 Type* type = NULL;
3089 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
3090 type = this->type();
3091 else
3093 if (is_type_switch != NULL)
3094 *is_type_switch = true;
3095 else
3097 error_at(this->location(),
3098 "use of %<.(type)%> outside type switch");
3099 type = Type::make_error_type();
3101 this->advance_token();
3103 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3104 error_at(this->location(), "missing %<)%>");
3105 else
3106 this->advance_token();
3107 if (is_type_switch != NULL && *is_type_switch)
3108 return left;
3109 return Expression::make_type_guard(left, type, location);
3111 else
3113 error_at(this->location(), "expected identifier or %<(%>");
3114 return left;
3118 // Index = "[" Expression "]" .
3119 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3121 Expression*
3122 Parse::index(Expression* expr)
3124 Location location = this->location();
3125 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
3126 this->advance_token();
3128 Expression* start;
3129 if (!this->peek_token()->is_op(OPERATOR_COLON))
3130 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3131 else
3133 mpz_t zero;
3134 mpz_init_set_ui(zero, 0);
3135 start = Expression::make_integer(&zero, NULL, location);
3136 mpz_clear(zero);
3139 Expression* end = NULL;
3140 if (this->peek_token()->is_op(OPERATOR_COLON))
3142 // We use nil to indicate a missing high expression.
3143 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3144 end = Expression::make_nil(this->location());
3145 else if (this->peek_token()->is_op(OPERATOR_COLON))
3147 error_at(this->location(), "middle index required in 3-index slice");
3148 end = Expression::make_error(this->location());
3150 else
3151 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3154 Expression* cap = NULL;
3155 if (this->peek_token()->is_op(OPERATOR_COLON))
3157 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3159 error_at(this->location(), "final index required in 3-index slice");
3160 cap = Expression::make_error(this->location());
3162 else
3163 cap = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3165 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
3166 error_at(this->location(), "missing %<]%>");
3167 else
3168 this->advance_token();
3169 return Expression::make_index(expr, start, end, cap, location);
3172 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3173 // ArgumentList = ExpressionList [ "..." ] .
3175 Expression*
3176 Parse::call(Expression* func)
3178 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
3179 Expression_list* args = NULL;
3180 bool is_varargs = false;
3181 const Token* token = this->advance_token();
3182 if (!token->is_op(OPERATOR_RPAREN))
3184 args = this->expression_list(NULL, false, true);
3185 token = this->peek_token();
3186 if (token->is_op(OPERATOR_ELLIPSIS))
3188 is_varargs = true;
3189 token = this->advance_token();
3192 if (token->is_op(OPERATOR_COMMA))
3193 token = this->advance_token();
3194 if (!token->is_op(OPERATOR_RPAREN))
3195 error_at(this->location(), "missing %<)%>");
3196 else
3197 this->advance_token();
3198 if (func->is_error_expression())
3199 return func;
3200 return Expression::make_call(func, args, is_varargs, func->location());
3203 // Return an expression for a single unqualified identifier.
3205 Expression*
3206 Parse::id_to_expression(const std::string& name, Location location,
3207 bool is_lhs)
3209 Named_object* in_function;
3210 Named_object* named_object = this->gogo_->lookup(name, &in_function);
3211 if (named_object == NULL)
3212 named_object = this->gogo_->add_unknown_name(name, location);
3214 if (in_function != NULL
3215 && in_function != this->gogo_->current_function()
3216 && (named_object->is_variable() || named_object->is_result_variable()))
3217 return this->enclosing_var_reference(in_function, named_object,
3218 location);
3220 switch (named_object->classification())
3222 case Named_object::NAMED_OBJECT_CONST:
3223 return Expression::make_const_reference(named_object, location);
3224 case Named_object::NAMED_OBJECT_VAR:
3225 case Named_object::NAMED_OBJECT_RESULT_VAR:
3226 if (!is_lhs)
3227 this->mark_var_used(named_object);
3228 return Expression::make_var_reference(named_object, location);
3229 case Named_object::NAMED_OBJECT_SINK:
3230 return Expression::make_sink(location);
3231 case Named_object::NAMED_OBJECT_FUNC:
3232 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3233 return Expression::make_func_reference(named_object, NULL, location);
3234 case Named_object::NAMED_OBJECT_UNKNOWN:
3236 Unknown_expression* ue =
3237 Expression::make_unknown_reference(named_object, location);
3238 if (this->is_erroneous_function_)
3239 ue->set_no_error_message();
3240 return ue;
3242 case Named_object::NAMED_OBJECT_PACKAGE:
3243 case Named_object::NAMED_OBJECT_TYPE:
3244 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3246 // These cases can arise for a field name in a composite
3247 // literal.
3248 Unknown_expression* ue =
3249 Expression::make_unknown_reference(named_object, location);
3250 if (this->is_erroneous_function_)
3251 ue->set_no_error_message();
3252 return ue;
3254 case Named_object::NAMED_OBJECT_ERRONEOUS:
3255 return Expression::make_error(location);
3256 default:
3257 error_at(this->location(), "unexpected type of identifier");
3258 return Expression::make_error(location);
3262 // Expression = UnaryExpr { binary_op Expression } .
3264 // PRECEDENCE is the precedence of the current operator.
3266 // If MAY_BE_SINK is true, this expression may be "_".
3268 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3269 // literal.
3271 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3272 // guard (var := expr.("type") using the literal keyword "type").
3274 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3275 // if the entire expression is in parentheses.
3277 Expression*
3278 Parse::expression(Precedence precedence, bool may_be_sink,
3279 bool may_be_composite_lit, bool* is_type_switch,
3280 bool *is_parenthesized)
3282 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3283 is_type_switch, is_parenthesized);
3285 while (true)
3287 if (is_type_switch != NULL && *is_type_switch)
3288 return left;
3290 const Token* token = this->peek_token();
3291 if (token->classification() != Token::TOKEN_OPERATOR)
3293 // Not a binary_op.
3294 return left;
3297 Precedence right_precedence;
3298 switch (token->op())
3300 case OPERATOR_OROR:
3301 right_precedence = PRECEDENCE_OROR;
3302 break;
3303 case OPERATOR_ANDAND:
3304 right_precedence = PRECEDENCE_ANDAND;
3305 break;
3306 case OPERATOR_EQEQ:
3307 case OPERATOR_NOTEQ:
3308 case OPERATOR_LT:
3309 case OPERATOR_LE:
3310 case OPERATOR_GT:
3311 case OPERATOR_GE:
3312 right_precedence = PRECEDENCE_RELOP;
3313 break;
3314 case OPERATOR_PLUS:
3315 case OPERATOR_MINUS:
3316 case OPERATOR_OR:
3317 case OPERATOR_XOR:
3318 right_precedence = PRECEDENCE_ADDOP;
3319 break;
3320 case OPERATOR_MULT:
3321 case OPERATOR_DIV:
3322 case OPERATOR_MOD:
3323 case OPERATOR_LSHIFT:
3324 case OPERATOR_RSHIFT:
3325 case OPERATOR_AND:
3326 case OPERATOR_BITCLEAR:
3327 right_precedence = PRECEDENCE_MULOP;
3328 break;
3329 default:
3330 right_precedence = PRECEDENCE_INVALID;
3331 break;
3334 if (right_precedence == PRECEDENCE_INVALID)
3336 // Not a binary_op.
3337 return left;
3340 if (is_parenthesized != NULL)
3341 *is_parenthesized = false;
3343 Operator op = token->op();
3344 Location binop_location = token->location();
3346 if (precedence >= right_precedence)
3348 // We've already seen A * B, and we see + C. We want to
3349 // return so that A * B becomes a group.
3350 return left;
3353 this->advance_token();
3355 left = this->verify_not_sink(left);
3356 Expression* right = this->expression(right_precedence, false,
3357 may_be_composite_lit,
3358 NULL, NULL);
3359 left = Expression::make_binary(op, left, right, binop_location);
3363 bool
3364 Parse::expression_may_start_here()
3366 const Token* token = this->peek_token();
3367 switch (token->classification())
3369 case Token::TOKEN_INVALID:
3370 case Token::TOKEN_EOF:
3371 return false;
3372 case Token::TOKEN_KEYWORD:
3373 switch (token->keyword())
3375 case KEYWORD_CHAN:
3376 case KEYWORD_FUNC:
3377 case KEYWORD_MAP:
3378 case KEYWORD_STRUCT:
3379 case KEYWORD_INTERFACE:
3380 return true;
3381 default:
3382 return false;
3384 case Token::TOKEN_IDENTIFIER:
3385 return true;
3386 case Token::TOKEN_STRING:
3387 return true;
3388 case Token::TOKEN_OPERATOR:
3389 switch (token->op())
3391 case OPERATOR_PLUS:
3392 case OPERATOR_MINUS:
3393 case OPERATOR_NOT:
3394 case OPERATOR_XOR:
3395 case OPERATOR_MULT:
3396 case OPERATOR_CHANOP:
3397 case OPERATOR_AND:
3398 case OPERATOR_LPAREN:
3399 case OPERATOR_LSQUARE:
3400 return true;
3401 default:
3402 return false;
3404 case Token::TOKEN_CHARACTER:
3405 case Token::TOKEN_INTEGER:
3406 case Token::TOKEN_FLOAT:
3407 case Token::TOKEN_IMAGINARY:
3408 return true;
3409 default:
3410 go_unreachable();
3414 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3416 // If MAY_BE_SINK is true, this expression may be "_".
3418 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3419 // literal.
3421 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3422 // guard (var := expr.("type") using the literal keyword "type").
3424 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3425 // if the entire expression is in parentheses.
3427 Expression*
3428 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3429 bool* is_type_switch, bool* is_parenthesized)
3431 const Token* token = this->peek_token();
3433 // There is a complex parse for <- chan. The choices are
3434 // Convert x to type <- chan int:
3435 // (<- chan int)(x)
3436 // Receive from (x converted to type chan <- chan int):
3437 // (<- chan <- chan int (x))
3438 // Convert x to type <- chan (<- chan int).
3439 // (<- chan <- chan int)(x)
3440 if (token->is_op(OPERATOR_CHANOP))
3442 Location location = token->location();
3443 if (this->advance_token()->is_keyword(KEYWORD_CHAN))
3445 Expression* expr = this->primary_expr(false, may_be_composite_lit,
3446 NULL, NULL);
3447 if (expr->is_error_expression())
3448 return expr;
3449 else if (!expr->is_type_expression())
3450 return Expression::make_receive(expr, location);
3451 else
3453 if (expr->type()->is_error_type())
3454 return expr;
3456 // We picked up "chan TYPE", but it is not a type
3457 // conversion.
3458 Channel_type* ct = expr->type()->channel_type();
3459 if (ct == NULL)
3461 // This is probably impossible.
3462 error_at(location, "expected channel type");
3463 return Expression::make_error(location);
3465 else if (ct->may_receive())
3467 // <- chan TYPE.
3468 Type* t = Type::make_channel_type(false, true,
3469 ct->element_type());
3470 return Expression::make_type(t, location);
3472 else
3474 // <- chan <- TYPE. Because we skipped the leading
3475 // <-, we parsed this as chan <- TYPE. With the
3476 // leading <-, we parse it as <- chan (<- TYPE).
3477 Type *t = this->reassociate_chan_direction(ct, location);
3478 return Expression::make_type(t, location);
3483 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP, location));
3484 token = this->peek_token();
3487 if (token->is_op(OPERATOR_PLUS)
3488 || token->is_op(OPERATOR_MINUS)
3489 || token->is_op(OPERATOR_NOT)
3490 || token->is_op(OPERATOR_XOR)
3491 || token->is_op(OPERATOR_CHANOP)
3492 || token->is_op(OPERATOR_MULT)
3493 || token->is_op(OPERATOR_AND))
3495 Location location = token->location();
3496 Operator op = token->op();
3497 this->advance_token();
3499 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL,
3500 NULL);
3501 if (expr->is_error_expression())
3503 else if (op == OPERATOR_MULT && expr->is_type_expression())
3504 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3505 location);
3506 else if (op == OPERATOR_AND && expr->is_composite_literal())
3507 expr = Expression::make_heap_expression(expr, location);
3508 else if (op != OPERATOR_CHANOP)
3509 expr = Expression::make_unary(op, expr, location);
3510 else
3511 expr = Expression::make_receive(expr, location);
3512 return expr;
3514 else
3515 return this->primary_expr(may_be_sink, may_be_composite_lit,
3516 is_type_switch, is_parenthesized);
3519 // This is called for the obscure case of
3520 // (<- chan <- chan int)(x)
3521 // In unary_expr we remove the leading <- and parse the remainder,
3522 // which gives us
3523 // chan <- (chan int)
3524 // When we add the leading <- back in, we really want
3525 // <- chan (<- chan int)
3526 // This means that we need to reassociate.
3528 Type*
3529 Parse::reassociate_chan_direction(Channel_type *ct, Location location)
3531 Channel_type* ele = ct->element_type()->channel_type();
3532 if (ele == NULL)
3534 error_at(location, "parse error");
3535 return Type::make_error_type();
3537 Type* sub = ele;
3538 if (ele->may_send())
3539 sub = Type::make_channel_type(false, true, ele->element_type());
3540 else
3541 sub = this->reassociate_chan_direction(ele, location);
3542 return Type::make_channel_type(false, true, sub);
3545 // Statement =
3546 // Declaration | LabeledStmt | SimpleStmt |
3547 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3548 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3549 // DeferStmt .
3551 // LABEL is the label of this statement if it has one.
3553 void
3554 Parse::statement(Label* label)
3556 const Token* token = this->peek_token();
3557 switch (token->classification())
3559 case Token::TOKEN_KEYWORD:
3561 switch (token->keyword())
3563 case KEYWORD_CONST:
3564 case KEYWORD_TYPE:
3565 case KEYWORD_VAR:
3566 this->declaration();
3567 break;
3568 case KEYWORD_FUNC:
3569 case KEYWORD_MAP:
3570 case KEYWORD_STRUCT:
3571 case KEYWORD_INTERFACE:
3572 this->simple_stat(true, NULL, NULL, NULL);
3573 break;
3574 case KEYWORD_GO:
3575 case KEYWORD_DEFER:
3576 this->go_or_defer_stat();
3577 break;
3578 case KEYWORD_RETURN:
3579 this->return_stat();
3580 break;
3581 case KEYWORD_BREAK:
3582 this->break_stat();
3583 break;
3584 case KEYWORD_CONTINUE:
3585 this->continue_stat();
3586 break;
3587 case KEYWORD_GOTO:
3588 this->goto_stat();
3589 break;
3590 case KEYWORD_IF:
3591 this->if_stat();
3592 break;
3593 case KEYWORD_SWITCH:
3594 this->switch_stat(label);
3595 break;
3596 case KEYWORD_SELECT:
3597 this->select_stat(label);
3598 break;
3599 case KEYWORD_FOR:
3600 this->for_stat(label);
3601 break;
3602 default:
3603 error_at(this->location(), "expected statement");
3604 this->advance_token();
3605 break;
3608 break;
3610 case Token::TOKEN_IDENTIFIER:
3612 std::string identifier = token->identifier();
3613 bool is_exported = token->is_identifier_exported();
3614 Location location = token->location();
3615 if (this->advance_token()->is_op(OPERATOR_COLON))
3617 this->advance_token();
3618 this->labeled_stmt(identifier, location);
3620 else
3622 this->unget_token(Token::make_identifier_token(identifier,
3623 is_exported,
3624 location));
3625 this->simple_stat(true, NULL, NULL, NULL);
3628 break;
3630 case Token::TOKEN_OPERATOR:
3631 if (token->is_op(OPERATOR_LCURLY))
3633 Location location = token->location();
3634 this->gogo_->start_block(location);
3635 Location end_loc = this->block();
3636 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3637 location);
3639 else if (!token->is_op(OPERATOR_SEMICOLON))
3640 this->simple_stat(true, NULL, NULL, NULL);
3641 break;
3643 case Token::TOKEN_STRING:
3644 case Token::TOKEN_CHARACTER:
3645 case Token::TOKEN_INTEGER:
3646 case Token::TOKEN_FLOAT:
3647 case Token::TOKEN_IMAGINARY:
3648 this->simple_stat(true, NULL, NULL, NULL);
3649 break;
3651 default:
3652 error_at(this->location(), "expected statement");
3653 this->advance_token();
3654 break;
3658 bool
3659 Parse::statement_may_start_here()
3661 const Token* token = this->peek_token();
3662 switch (token->classification())
3664 case Token::TOKEN_KEYWORD:
3666 switch (token->keyword())
3668 case KEYWORD_CONST:
3669 case KEYWORD_TYPE:
3670 case KEYWORD_VAR:
3671 case KEYWORD_FUNC:
3672 case KEYWORD_MAP:
3673 case KEYWORD_STRUCT:
3674 case KEYWORD_INTERFACE:
3675 case KEYWORD_GO:
3676 case KEYWORD_DEFER:
3677 case KEYWORD_RETURN:
3678 case KEYWORD_BREAK:
3679 case KEYWORD_CONTINUE:
3680 case KEYWORD_GOTO:
3681 case KEYWORD_IF:
3682 case KEYWORD_SWITCH:
3683 case KEYWORD_SELECT:
3684 case KEYWORD_FOR:
3685 return true;
3687 default:
3688 return false;
3691 break;
3693 case Token::TOKEN_IDENTIFIER:
3694 return true;
3696 case Token::TOKEN_OPERATOR:
3697 if (token->is_op(OPERATOR_LCURLY)
3698 || token->is_op(OPERATOR_SEMICOLON))
3699 return true;
3700 else
3701 return this->expression_may_start_here();
3703 case Token::TOKEN_STRING:
3704 case Token::TOKEN_CHARACTER:
3705 case Token::TOKEN_INTEGER:
3706 case Token::TOKEN_FLOAT:
3707 case Token::TOKEN_IMAGINARY:
3708 return true;
3710 default:
3711 return false;
3715 // LabeledStmt = Label ":" Statement .
3716 // Label = identifier .
3718 void
3719 Parse::labeled_stmt(const std::string& label_name, Location location)
3721 Label* label = this->gogo_->add_label_definition(label_name, location);
3723 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3725 // This is a label at the end of a block. A program is
3726 // permitted to omit a semicolon here.
3727 return;
3730 if (!this->statement_may_start_here())
3732 // Mark the label as used to avoid a useless error about an
3733 // unused label.
3734 if (label != NULL)
3735 label->set_is_used();
3737 error_at(location, "missing statement after label");
3738 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3739 location));
3740 return;
3743 this->statement(label);
3746 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3747 // Assignment | ShortVarDecl .
3749 // EmptyStmt was handled in Parse::statement.
3751 // In order to make this work for if and switch statements, if
3752 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3753 // expression rather than adding an expression statement to the
3754 // current block. If we see something other than an ExpressionStat,
3755 // we add the statement, set *RETURN_EXP to true if we saw a send
3756 // statement, and return NULL. The handling of send statements is for
3757 // better error messages.
3759 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3760 // RangeClause.
3762 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3763 // guard (var := expr.("type") using the literal keyword "type").
3765 Expression*
3766 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3767 Range_clause* p_range_clause, Type_switch* p_type_switch)
3769 const Token* token = this->peek_token();
3771 // An identifier follow by := is a SimpleVarDecl.
3772 if (token->is_identifier())
3774 std::string identifier = token->identifier();
3775 bool is_exported = token->is_identifier_exported();
3776 Location location = token->location();
3778 token = this->advance_token();
3779 if (token->is_op(OPERATOR_COLONEQ)
3780 || token->is_op(OPERATOR_COMMA))
3782 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3783 this->simple_var_decl_or_assignment(identifier, location,
3784 may_be_composite_lit,
3785 p_range_clause,
3786 (token->is_op(OPERATOR_COLONEQ)
3787 ? p_type_switch
3788 : NULL));
3789 return NULL;
3792 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3793 location));
3795 else if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
3797 Typed_identifier_list til;
3798 this->range_clause_decl(&til, p_range_clause);
3799 return NULL;
3802 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3803 may_be_composite_lit,
3804 (p_type_switch == NULL
3805 ? NULL
3806 : &p_type_switch->found),
3807 NULL);
3808 if (p_type_switch != NULL && p_type_switch->found)
3810 p_type_switch->name.clear();
3811 p_type_switch->location = exp->location();
3812 p_type_switch->expr = this->verify_not_sink(exp);
3813 return NULL;
3815 token = this->peek_token();
3816 if (token->is_op(OPERATOR_CHANOP))
3818 this->send_stmt(this->verify_not_sink(exp));
3819 if (return_exp != NULL)
3820 *return_exp = true;
3822 else if (token->is_op(OPERATOR_PLUSPLUS)
3823 || token->is_op(OPERATOR_MINUSMINUS))
3824 this->inc_dec_stat(this->verify_not_sink(exp));
3825 else if (token->is_op(OPERATOR_COMMA)
3826 || token->is_op(OPERATOR_EQ))
3827 this->assignment(exp, may_be_composite_lit, p_range_clause);
3828 else if (token->is_op(OPERATOR_PLUSEQ)
3829 || token->is_op(OPERATOR_MINUSEQ)
3830 || token->is_op(OPERATOR_OREQ)
3831 || token->is_op(OPERATOR_XOREQ)
3832 || token->is_op(OPERATOR_MULTEQ)
3833 || token->is_op(OPERATOR_DIVEQ)
3834 || token->is_op(OPERATOR_MODEQ)
3835 || token->is_op(OPERATOR_LSHIFTEQ)
3836 || token->is_op(OPERATOR_RSHIFTEQ)
3837 || token->is_op(OPERATOR_ANDEQ)
3838 || token->is_op(OPERATOR_BITCLEAREQ))
3839 this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
3840 p_range_clause);
3841 else if (return_exp != NULL)
3842 return this->verify_not_sink(exp);
3843 else
3845 exp = this->verify_not_sink(exp);
3847 if (token->is_op(OPERATOR_COLONEQ))
3849 if (!exp->is_error_expression())
3850 error_at(token->location(), "non-name on left side of %<:=%>");
3851 this->gogo_->mark_locals_used();
3852 while (!token->is_op(OPERATOR_SEMICOLON)
3853 && !token->is_eof())
3854 token = this->advance_token();
3855 return NULL;
3858 this->expression_stat(exp);
3861 return NULL;
3864 bool
3865 Parse::simple_stat_may_start_here()
3867 return this->expression_may_start_here();
3870 // Parse { Statement ";" } which is used in a few places. The list of
3871 // statements may end with a right curly brace, in which case the
3872 // semicolon may be omitted.
3874 void
3875 Parse::statement_list()
3877 while (this->statement_may_start_here())
3879 this->statement(NULL);
3880 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3881 this->advance_token();
3882 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3883 break;
3884 else
3886 if (!this->peek_token()->is_eof() || !saw_errors())
3887 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3888 if (!this->skip_past_error(OPERATOR_RCURLY))
3889 return;
3894 bool
3895 Parse::statement_list_may_start_here()
3897 return this->statement_may_start_here();
3900 // ExpressionStat = Expression .
3902 void
3903 Parse::expression_stat(Expression* exp)
3905 this->gogo_->add_statement(Statement::make_statement(exp, false));
3908 // SendStmt = Channel "&lt;-" Expression .
3909 // Channel = Expression .
3911 void
3912 Parse::send_stmt(Expression* channel)
3914 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3915 Location loc = this->location();
3916 this->advance_token();
3917 Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
3918 NULL);
3919 Statement* s = Statement::make_send_statement(channel, val, loc);
3920 this->gogo_->add_statement(s);
3923 // IncDecStat = Expression ( "++" | "--" ) .
3925 void
3926 Parse::inc_dec_stat(Expression* exp)
3928 const Token* token = this->peek_token();
3930 // Lvalue maps require special handling.
3931 if (exp->index_expression() != NULL)
3932 exp->index_expression()->set_is_lvalue();
3934 if (token->is_op(OPERATOR_PLUSPLUS))
3935 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3936 else if (token->is_op(OPERATOR_MINUSMINUS))
3937 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3938 else
3939 go_unreachable();
3940 this->advance_token();
3943 // Assignment = ExpressionList assign_op ExpressionList .
3945 // EXP is an expression that we have already parsed.
3947 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3948 // side may be a composite literal.
3950 // If RANGE_CLAUSE is not NULL, then this will recognize a
3951 // RangeClause.
3953 void
3954 Parse::assignment(Expression* expr, bool may_be_composite_lit,
3955 Range_clause* p_range_clause)
3957 Expression_list* vars;
3958 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3960 vars = new Expression_list();
3961 vars->push_back(expr);
3963 else
3965 this->advance_token();
3966 vars = this->expression_list(expr, true, may_be_composite_lit);
3969 this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
3972 // An assignment statement. LHS is the list of expressions which
3973 // appear on the left hand side.
3975 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3976 // side may be a composite literal.
3978 // If RANGE_CLAUSE is not NULL, then this will recognize a
3979 // RangeClause.
3981 void
3982 Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
3983 Range_clause* p_range_clause)
3985 const Token* token = this->peek_token();
3986 if (!token->is_op(OPERATOR_EQ)
3987 && !token->is_op(OPERATOR_PLUSEQ)
3988 && !token->is_op(OPERATOR_MINUSEQ)
3989 && !token->is_op(OPERATOR_OREQ)
3990 && !token->is_op(OPERATOR_XOREQ)
3991 && !token->is_op(OPERATOR_MULTEQ)
3992 && !token->is_op(OPERATOR_DIVEQ)
3993 && !token->is_op(OPERATOR_MODEQ)
3994 && !token->is_op(OPERATOR_LSHIFTEQ)
3995 && !token->is_op(OPERATOR_RSHIFTEQ)
3996 && !token->is_op(OPERATOR_ANDEQ)
3997 && !token->is_op(OPERATOR_BITCLEAREQ))
3999 error_at(this->location(), "expected assignment operator");
4000 return;
4002 Operator op = token->op();
4003 Location location = token->location();
4005 token = this->advance_token();
4007 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
4009 if (op != OPERATOR_EQ)
4010 error_at(this->location(), "range clause requires %<=%>");
4011 this->range_clause_expr(lhs, p_range_clause);
4012 return;
4015 Expression_list* vals = this->expression_list(NULL, false,
4016 may_be_composite_lit);
4018 // We've parsed everything; check for errors.
4019 if (lhs == NULL || vals == NULL)
4020 return;
4021 for (Expression_list::const_iterator pe = lhs->begin();
4022 pe != lhs->end();
4023 ++pe)
4025 if ((*pe)->is_error_expression())
4026 return;
4027 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
4028 error_at((*pe)->location(), "cannot use _ as value");
4030 for (Expression_list::const_iterator pe = vals->begin();
4031 pe != vals->end();
4032 ++pe)
4034 if ((*pe)->is_error_expression())
4035 return;
4038 // Map expressions act differently when they are lvalues.
4039 for (Expression_list::iterator plv = lhs->begin();
4040 plv != lhs->end();
4041 ++plv)
4042 if ((*plv)->index_expression() != NULL)
4043 (*plv)->index_expression()->set_is_lvalue();
4045 Call_expression* call;
4046 Index_expression* map_index;
4047 Receive_expression* receive;
4048 Type_guard_expression* type_guard;
4049 if (lhs->size() == vals->size())
4051 Statement* s;
4052 if (lhs->size() > 1)
4054 if (op != OPERATOR_EQ)
4055 error_at(location, "multiple values only permitted with %<=%>");
4056 s = Statement::make_tuple_assignment(lhs, vals, location);
4058 else
4060 if (op == OPERATOR_EQ)
4061 s = Statement::make_assignment(lhs->front(), vals->front(),
4062 location);
4063 else
4064 s = Statement::make_assignment_operation(op, lhs->front(),
4065 vals->front(), location);
4066 delete lhs;
4067 delete vals;
4069 this->gogo_->add_statement(s);
4071 else if (vals->size() == 1
4072 && (call = (*vals->begin())->call_expression()) != NULL)
4074 if (op != OPERATOR_EQ)
4075 error_at(location, "multiple results only permitted with %<=%>");
4076 call->set_expected_result_count(lhs->size());
4077 delete vals;
4078 vals = new Expression_list;
4079 for (unsigned int i = 0; i < lhs->size(); ++i)
4080 vals->push_back(Expression::make_call_result(call, i));
4081 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
4082 this->gogo_->add_statement(s);
4084 else if (lhs->size() == 2
4085 && vals->size() == 1
4086 && (map_index = (*vals->begin())->index_expression()) != NULL)
4088 if (op != OPERATOR_EQ)
4089 error_at(location, "two values from map requires %<=%>");
4090 Expression* val = lhs->front();
4091 Expression* present = lhs->back();
4092 Statement* s = Statement::make_tuple_map_assignment(val, present,
4093 map_index, location);
4094 this->gogo_->add_statement(s);
4096 else if (lhs->size() == 1
4097 && vals->size() == 2
4098 && (map_index = lhs->front()->index_expression()) != NULL)
4100 if (op != OPERATOR_EQ)
4101 error_at(location, "assigning tuple to map index requires %<=%>");
4102 Expression* val = vals->front();
4103 Expression* should_set = vals->back();
4104 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
4105 location);
4106 this->gogo_->add_statement(s);
4108 else if (lhs->size() == 2
4109 && vals->size() == 1
4110 && (receive = (*vals->begin())->receive_expression()) != NULL)
4112 if (op != OPERATOR_EQ)
4113 error_at(location, "two values from receive requires %<=%>");
4114 Expression* val = lhs->front();
4115 Expression* success = lhs->back();
4116 Expression* channel = receive->channel();
4117 Statement* s = Statement::make_tuple_receive_assignment(val, success,
4118 channel,
4119 location);
4120 this->gogo_->add_statement(s);
4122 else if (lhs->size() == 2
4123 && vals->size() == 1
4124 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
4126 if (op != OPERATOR_EQ)
4127 error_at(location, "two values from type guard requires %<=%>");
4128 Expression* val = lhs->front();
4129 Expression* ok = lhs->back();
4130 Expression* expr = type_guard->expr();
4131 Type* type = type_guard->type();
4132 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
4133 expr, type,
4134 location);
4135 this->gogo_->add_statement(s);
4137 else
4139 error_at(location, "number of variables does not match number of values");
4143 // GoStat = "go" Expression .
4144 // DeferStat = "defer" Expression .
4146 void
4147 Parse::go_or_defer_stat()
4149 go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
4150 || this->peek_token()->is_keyword(KEYWORD_DEFER));
4151 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
4152 Location stat_location = this->location();
4154 this->advance_token();
4155 Location expr_location = this->location();
4157 bool is_parenthesized = false;
4158 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
4159 &is_parenthesized);
4160 Call_expression* call_expr = expr->call_expression();
4161 if (is_parenthesized || call_expr == NULL)
4163 error_at(expr_location, "argument to go/defer must be function call");
4164 return;
4167 // Make it easier to simplify go/defer statements by putting every
4168 // statement in its own block.
4169 this->gogo_->start_block(stat_location);
4170 Statement* stat;
4171 if (is_go)
4172 stat = Statement::make_go_statement(call_expr, stat_location);
4173 else
4174 stat = Statement::make_defer_statement(call_expr, stat_location);
4175 this->gogo_->add_statement(stat);
4176 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
4177 stat_location);
4180 // ReturnStat = "return" [ ExpressionList ] .
4182 void
4183 Parse::return_stat()
4185 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
4186 Location location = this->location();
4187 this->advance_token();
4188 Expression_list* vals = NULL;
4189 if (this->expression_may_start_here())
4190 vals = this->expression_list(NULL, false, true);
4191 this->gogo_->add_statement(Statement::make_return_statement(vals, location));
4193 if (vals == NULL
4194 && this->gogo_->current_function()->func_value()->results_are_named())
4196 Named_object* function = this->gogo_->current_function();
4197 Function::Results* results = function->func_value()->result_variables();
4198 for (Function::Results::const_iterator p = results->begin();
4199 p != results->end();
4200 ++p)
4202 Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
4203 if (no == NULL)
4204 go_assert(saw_errors());
4205 else if (!no->is_result_variable())
4206 error_at(location, "%qs is shadowed during return",
4207 (*p)->message_name().c_str());
4212 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4213 // [ "else" ( IfStmt | Block ) ] .
4215 void
4216 Parse::if_stat()
4218 go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
4219 Location location = this->location();
4220 this->advance_token();
4222 this->gogo_->start_block(location);
4224 bool saw_simple_stat = false;
4225 Expression* cond = NULL;
4226 bool saw_send_stmt = false;
4227 if (this->simple_stat_may_start_here())
4229 cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
4230 saw_simple_stat = true;
4232 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4234 // The SimpleStat is an expression statement.
4235 this->expression_stat(cond);
4236 cond = NULL;
4238 if (cond == NULL)
4240 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4241 this->advance_token();
4242 else if (saw_simple_stat)
4244 if (saw_send_stmt)
4245 error_at(this->location(),
4246 ("send statement used as value; "
4247 "use select for non-blocking send"));
4248 else
4249 error_at(this->location(),
4250 "expected %<;%> after statement in if expression");
4251 if (!this->expression_may_start_here())
4252 cond = Expression::make_error(this->location());
4254 if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
4256 error_at(this->location(),
4257 "missing condition in if statement");
4258 cond = Expression::make_error(this->location());
4260 if (cond == NULL)
4261 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL, NULL);
4264 // Check for the easy error of a newline before starting the block.
4265 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4267 Location semi_loc = this->location();
4268 if (this->advance_token()->is_op(OPERATOR_LCURLY))
4269 error_at(semi_loc, "missing %<{%> after if clause");
4270 // Otherwise we will get an error when we call this->block
4271 // below.
4274 this->gogo_->start_block(this->location());
4275 Location end_loc = this->block();
4276 Block* then_block = this->gogo_->finish_block(end_loc);
4278 // Check for the easy error of a newline before "else".
4279 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4281 Location semi_loc = this->location();
4282 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
4283 error_at(this->location(),
4284 "unexpected semicolon or newline before %<else%>");
4285 else
4286 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
4287 semi_loc));
4290 Block* else_block = NULL;
4291 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
4293 this->gogo_->start_block(this->location());
4294 const Token* token = this->advance_token();
4295 if (token->is_keyword(KEYWORD_IF))
4296 this->if_stat();
4297 else if (token->is_op(OPERATOR_LCURLY))
4298 this->block();
4299 else
4301 error_at(this->location(), "expected %<if%> or %<{%>");
4302 this->statement(NULL);
4304 else_block = this->gogo_->finish_block(this->location());
4307 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
4308 else_block,
4309 location));
4311 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4312 location);
4315 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4316 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4317 // "{" { ExprCaseClause } "}" .
4318 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4319 // "{" { TypeCaseClause } "}" .
4320 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4322 void
4323 Parse::switch_stat(Label* label)
4325 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
4326 Location location = this->location();
4327 this->advance_token();
4329 this->gogo_->start_block(location);
4331 bool saw_simple_stat = false;
4332 Expression* switch_val = NULL;
4333 bool saw_send_stmt;
4334 Type_switch type_switch;
4335 bool have_type_switch_block = false;
4336 if (this->simple_stat_may_start_here())
4338 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4339 &type_switch);
4340 saw_simple_stat = true;
4342 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4344 // The SimpleStat is an expression statement.
4345 this->expression_stat(switch_val);
4346 switch_val = NULL;
4348 if (switch_val == NULL && !type_switch.found)
4350 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4351 this->advance_token();
4352 else if (saw_simple_stat)
4354 if (saw_send_stmt)
4355 error_at(this->location(),
4356 ("send statement used as value; "
4357 "use select for non-blocking send"));
4358 else
4359 error_at(this->location(),
4360 "expected %<;%> after statement in switch expression");
4362 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4364 if (this->peek_token()->is_identifier())
4366 const Token* token = this->peek_token();
4367 std::string identifier = token->identifier();
4368 bool is_exported = token->is_identifier_exported();
4369 Location id_loc = token->location();
4371 token = this->advance_token();
4372 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
4373 this->unget_token(Token::make_identifier_token(identifier,
4374 is_exported,
4375 id_loc));
4376 if (is_coloneq)
4378 // This must be a TypeSwitchGuard. It is in a
4379 // different block from any initial SimpleStat.
4380 if (saw_simple_stat)
4382 this->gogo_->start_block(id_loc);
4383 have_type_switch_block = true;
4386 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4387 &type_switch);
4388 if (!type_switch.found)
4390 if (switch_val == NULL
4391 || !switch_val->is_error_expression())
4393 error_at(id_loc, "expected type switch assignment");
4394 switch_val = Expression::make_error(id_loc);
4399 if (switch_val == NULL && !type_switch.found)
4401 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
4402 &type_switch.found, NULL);
4403 if (type_switch.found)
4405 type_switch.name.clear();
4406 type_switch.expr = switch_val;
4407 type_switch.location = switch_val->location();
4413 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4415 Location token_loc = this->location();
4416 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
4417 && this->advance_token()->is_op(OPERATOR_LCURLY))
4418 error_at(token_loc, "missing %<{%> after switch clause");
4419 else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
4421 error_at(token_loc, "invalid variable name");
4422 this->advance_token();
4423 this->expression(PRECEDENCE_NORMAL, false, false,
4424 &type_switch.found, NULL);
4425 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4426 this->advance_token();
4427 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4429 if (have_type_switch_block)
4430 this->gogo_->add_block(this->gogo_->finish_block(location),
4431 location);
4432 this->gogo_->add_block(this->gogo_->finish_block(location),
4433 location);
4434 return;
4436 if (type_switch.found)
4437 type_switch.expr = Expression::make_error(location);
4439 else
4441 error_at(this->location(), "expected %<{%>");
4442 if (have_type_switch_block)
4443 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4444 location);
4445 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4446 location);
4447 return;
4450 this->advance_token();
4452 Statement* statement;
4453 if (type_switch.found)
4454 statement = this->type_switch_body(label, type_switch, location);
4455 else
4456 statement = this->expr_switch_body(label, switch_val, location);
4458 if (statement != NULL)
4459 this->gogo_->add_statement(statement);
4461 if (have_type_switch_block)
4462 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4463 location);
4465 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4466 location);
4469 // The body of an expression switch.
4470 // "{" { ExprCaseClause } "}"
4472 Statement*
4473 Parse::expr_switch_body(Label* label, Expression* switch_val,
4474 Location location)
4476 Switch_statement* statement = Statement::make_switch_statement(switch_val,
4477 location);
4479 this->push_break_statement(statement, label);
4481 Case_clauses* case_clauses = new Case_clauses();
4482 bool saw_default = false;
4483 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4485 if (this->peek_token()->is_eof())
4487 if (!saw_errors())
4488 error_at(this->location(), "missing %<}%>");
4489 return NULL;
4491 this->expr_case_clause(case_clauses, &saw_default);
4493 this->advance_token();
4495 statement->add_clauses(case_clauses);
4497 this->pop_break_statement();
4499 return statement;
4502 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4503 // FallthroughStat = "fallthrough" .
4505 void
4506 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
4508 Location location = this->location();
4510 bool is_default = false;
4511 Expression_list* vals = this->expr_switch_case(&is_default);
4513 if (!this->peek_token()->is_op(OPERATOR_COLON))
4515 if (!saw_errors())
4516 error_at(this->location(), "expected %<:%>");
4517 return;
4519 else
4520 this->advance_token();
4522 Block* statements = NULL;
4523 if (this->statement_list_may_start_here())
4525 this->gogo_->start_block(this->location());
4526 this->statement_list();
4527 statements = this->gogo_->finish_block(this->location());
4530 bool is_fallthrough = false;
4531 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4533 Location fallthrough_loc = this->location();
4534 is_fallthrough = true;
4535 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4536 this->advance_token();
4537 if (this->peek_token()->is_op(OPERATOR_RCURLY))
4538 error_at(fallthrough_loc, _("cannot fallthrough final case in switch"));
4541 if (is_default)
4543 if (*saw_default)
4545 error_at(location, "multiple defaults in switch");
4546 return;
4548 *saw_default = true;
4551 if (is_default || vals != NULL)
4552 clauses->add(vals, is_default, statements, is_fallthrough, location);
4555 // ExprSwitchCase = "case" ExpressionList | "default" .
4557 Expression_list*
4558 Parse::expr_switch_case(bool* is_default)
4560 const Token* token = this->peek_token();
4561 if (token->is_keyword(KEYWORD_CASE))
4563 this->advance_token();
4564 return this->expression_list(NULL, false, true);
4566 else if (token->is_keyword(KEYWORD_DEFAULT))
4568 this->advance_token();
4569 *is_default = true;
4570 return NULL;
4572 else
4574 if (!saw_errors())
4575 error_at(this->location(), "expected %<case%> or %<default%>");
4576 if (!token->is_op(OPERATOR_RCURLY))
4577 this->advance_token();
4578 return NULL;
4582 // The body of a type switch.
4583 // "{" { TypeCaseClause } "}" .
4585 Statement*
4586 Parse::type_switch_body(Label* label, const Type_switch& type_switch,
4587 Location location)
4589 Named_object* switch_no = NULL;
4590 if (!type_switch.name.empty())
4592 if (Gogo::is_sink_name(type_switch.name))
4593 error_at(type_switch.location,
4594 "no new variables on left side of %<:=%>");
4595 else
4597 Variable* switch_var = new Variable(NULL, type_switch.expr, false,
4598 false, false,
4599 type_switch.location);
4600 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
4604 Type_switch_statement* statement =
4605 Statement::make_type_switch_statement(switch_no,
4606 (switch_no == NULL
4607 ? type_switch.expr
4608 : NULL),
4609 location);
4611 this->push_break_statement(statement, label);
4613 Type_case_clauses* case_clauses = new Type_case_clauses();
4614 bool saw_default = false;
4615 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4617 if (this->peek_token()->is_eof())
4619 error_at(this->location(), "missing %<}%>");
4620 return NULL;
4622 this->type_case_clause(switch_no, case_clauses, &saw_default);
4624 this->advance_token();
4626 statement->add_clauses(case_clauses);
4628 this->pop_break_statement();
4630 return statement;
4633 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4635 void
4636 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4637 bool* saw_default)
4639 Location location = this->location();
4641 std::vector<Type*> types;
4642 bool is_default = false;
4643 this->type_switch_case(&types, &is_default);
4645 if (!this->peek_token()->is_op(OPERATOR_COLON))
4646 error_at(this->location(), "expected %<:%>");
4647 else
4648 this->advance_token();
4650 Block* statements = NULL;
4651 if (this->statement_list_may_start_here())
4653 this->gogo_->start_block(this->location());
4654 if (switch_no != NULL && types.size() == 1)
4656 Type* type = types.front();
4657 Expression* init = Expression::make_var_reference(switch_no,
4658 location);
4659 init = Expression::make_type_guard(init, type, location);
4660 Variable* v = new Variable(type, init, false, false, false,
4661 location);
4662 v->set_is_type_switch_var();
4663 Named_object* no = this->gogo_->add_variable(switch_no->name(), v);
4665 // We don't want to issue an error if the compiler
4666 // introduced special variable is not used. Instead we want
4667 // to issue an error if the variable defined by the switch
4668 // is not used. That is handled via type_switch_vars_ and
4669 // Parse::mark_var_used.
4670 v->set_is_used();
4671 this->type_switch_vars_[no] = switch_no;
4673 this->statement_list();
4674 statements = this->gogo_->finish_block(this->location());
4677 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4679 error_at(this->location(),
4680 "fallthrough is not permitted in a type switch");
4681 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4682 this->advance_token();
4685 if (is_default)
4687 go_assert(types.empty());
4688 if (*saw_default)
4690 error_at(location, "multiple defaults in type switch");
4691 return;
4693 *saw_default = true;
4694 clauses->add(NULL, false, true, statements, location);
4696 else if (!types.empty())
4698 for (std::vector<Type*>::const_iterator p = types.begin();
4699 p + 1 != types.end();
4700 ++p)
4701 clauses->add(*p, true, false, NULL, location);
4702 clauses->add(types.back(), false, false, statements, location);
4704 else
4705 clauses->add(Type::make_error_type(), false, false, statements, location);
4708 // TypeSwitchCase = "case" type | "default"
4710 // We accept a comma separated list of types.
4712 void
4713 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4715 const Token* token = this->peek_token();
4716 if (token->is_keyword(KEYWORD_CASE))
4718 this->advance_token();
4719 while (true)
4721 Type* t = this->type();
4723 if (!t->is_error_type())
4724 types->push_back(t);
4725 else
4727 this->gogo_->mark_locals_used();
4728 token = this->peek_token();
4729 while (!token->is_op(OPERATOR_COLON)
4730 && !token->is_op(OPERATOR_COMMA)
4731 && !token->is_op(OPERATOR_RCURLY)
4732 && !token->is_eof())
4733 token = this->advance_token();
4736 if (!this->peek_token()->is_op(OPERATOR_COMMA))
4737 break;
4738 this->advance_token();
4741 else if (token->is_keyword(KEYWORD_DEFAULT))
4743 this->advance_token();
4744 *is_default = true;
4746 else
4748 error_at(this->location(), "expected %<case%> or %<default%>");
4749 if (!token->is_op(OPERATOR_RCURLY))
4750 this->advance_token();
4754 // SelectStat = "select" "{" { CommClause } "}" .
4756 void
4757 Parse::select_stat(Label* label)
4759 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4760 Location location = this->location();
4761 const Token* token = this->advance_token();
4763 if (!token->is_op(OPERATOR_LCURLY))
4765 Location token_loc = token->location();
4766 if (token->is_op(OPERATOR_SEMICOLON)
4767 && this->advance_token()->is_op(OPERATOR_LCURLY))
4768 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4769 else
4771 error_at(this->location(), "expected %<{%>");
4772 return;
4775 this->advance_token();
4777 Select_statement* statement = Statement::make_select_statement(location);
4779 this->push_break_statement(statement, label);
4781 Select_clauses* select_clauses = new Select_clauses();
4782 bool saw_default = false;
4783 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4785 if (this->peek_token()->is_eof())
4787 error_at(this->location(), "expected %<}%>");
4788 return;
4790 this->comm_clause(select_clauses, &saw_default);
4793 this->advance_token();
4795 statement->add_clauses(select_clauses);
4797 this->pop_break_statement();
4799 this->gogo_->add_statement(statement);
4802 // CommClause = CommCase ":" { Statement ";" } .
4804 void
4805 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4807 Location location = this->location();
4808 bool is_send = false;
4809 Expression* channel = NULL;
4810 Expression* val = NULL;
4811 Expression* closed = NULL;
4812 std::string varname;
4813 std::string closedname;
4814 bool is_default = false;
4815 bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
4816 &varname, &closedname, &is_default);
4818 if (!is_send
4819 && varname.empty()
4820 && closedname.empty()
4821 && val != NULL
4822 && val->index_expression() != NULL)
4823 val->index_expression()->set_is_lvalue();
4825 if (this->peek_token()->is_op(OPERATOR_COLON))
4826 this->advance_token();
4827 else
4828 error_at(this->location(), "expected colon");
4830 this->gogo_->start_block(this->location());
4832 Named_object* var = NULL;
4833 if (!varname.empty())
4835 // FIXME: LOCATION is slightly wrong here.
4836 Variable* v = new Variable(NULL, channel, false, false, false,
4837 location);
4838 v->set_type_from_chan_element();
4839 var = this->gogo_->add_variable(varname, v);
4842 Named_object* closedvar = NULL;
4843 if (!closedname.empty())
4845 // FIXME: LOCATION is slightly wrong here.
4846 Variable* v = new Variable(Type::lookup_bool_type(), NULL,
4847 false, false, false, location);
4848 closedvar = this->gogo_->add_variable(closedname, v);
4851 this->statement_list();
4853 Block* statements = this->gogo_->finish_block(this->location());
4855 if (is_default)
4857 if (*saw_default)
4859 error_at(location, "multiple defaults in select");
4860 return;
4862 *saw_default = true;
4865 if (got_case)
4866 clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
4867 statements, location);
4868 else if (statements != NULL)
4870 // Add the statements to make sure that any names they define
4871 // are traversed.
4872 this->gogo_->add_block(statements, location);
4876 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4878 bool
4879 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4880 Expression** closed, std::string* varname,
4881 std::string* closedname, bool* is_default)
4883 const Token* token = this->peek_token();
4884 if (token->is_keyword(KEYWORD_DEFAULT))
4886 this->advance_token();
4887 *is_default = true;
4889 else if (token->is_keyword(KEYWORD_CASE))
4891 this->advance_token();
4892 if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
4893 closedname))
4894 return false;
4896 else
4898 error_at(this->location(), "expected %<case%> or %<default%>");
4899 if (!token->is_op(OPERATOR_RCURLY))
4900 this->advance_token();
4901 return false;
4904 return true;
4907 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4908 // RecvExpr = Expression .
4910 bool
4911 Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
4912 Expression** closed, std::string* varname,
4913 std::string* closedname)
4915 const Token* token = this->peek_token();
4916 bool saw_comma = false;
4917 bool closed_is_id = false;
4918 if (token->is_identifier())
4920 Gogo* gogo = this->gogo_;
4921 std::string recv_var = token->identifier();
4922 bool is_rv_exported = token->is_identifier_exported();
4923 Location recv_var_loc = token->location();
4924 token = this->advance_token();
4925 if (token->is_op(OPERATOR_COLONEQ))
4927 // case rv := <-c:
4928 this->advance_token();
4929 Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
4930 NULL, NULL);
4931 Receive_expression* re = e->receive_expression();
4932 if (re == NULL)
4934 if (!e->is_error_expression())
4935 error_at(this->location(), "expected receive expression");
4936 return false;
4938 if (recv_var == "_")
4940 error_at(recv_var_loc,
4941 "no new variables on left side of %<:=%>");
4942 recv_var = Gogo::erroneous_name();
4944 *is_send = false;
4945 *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
4946 *channel = re->channel();
4947 return true;
4949 else if (token->is_op(OPERATOR_COMMA))
4951 token = this->advance_token();
4952 if (token->is_identifier())
4954 std::string recv_closed = token->identifier();
4955 bool is_rc_exported = token->is_identifier_exported();
4956 Location recv_closed_loc = token->location();
4957 closed_is_id = true;
4959 token = this->advance_token();
4960 if (token->is_op(OPERATOR_COLONEQ))
4962 // case rv, rc := <-c:
4963 this->advance_token();
4964 Expression* e = this->expression(PRECEDENCE_NORMAL, false,
4965 false, NULL, NULL);
4966 Receive_expression* re = e->receive_expression();
4967 if (re == NULL)
4969 if (!e->is_error_expression())
4970 error_at(this->location(),
4971 "expected receive expression");
4972 return false;
4974 if (recv_var == "_" && recv_closed == "_")
4976 error_at(recv_var_loc,
4977 "no new variables on left side of %<:=%>");
4978 recv_var = Gogo::erroneous_name();
4980 *is_send = false;
4981 if (recv_var != "_")
4982 *varname = gogo->pack_hidden_name(recv_var,
4983 is_rv_exported);
4984 if (recv_closed != "_")
4985 *closedname = gogo->pack_hidden_name(recv_closed,
4986 is_rc_exported);
4987 *channel = re->channel();
4988 return true;
4991 this->unget_token(Token::make_identifier_token(recv_closed,
4992 is_rc_exported,
4993 recv_closed_loc));
4996 *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
4997 is_rv_exported),
4998 recv_var_loc, true);
4999 saw_comma = true;
5001 else
5002 this->unget_token(Token::make_identifier_token(recv_var,
5003 is_rv_exported,
5004 recv_var_loc));
5007 // If SAW_COMMA is false, then we are looking at the start of the
5008 // send or receive expression. If SAW_COMMA is true, then *VAL is
5009 // set and we just read a comma.
5011 Expression* e;
5012 if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
5013 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL);
5014 else
5016 // case <-c:
5017 *is_send = false;
5018 this->advance_token();
5019 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5021 // The next token should be ':'. If it is '<-', then we have
5022 // case <-c <- v:
5023 // which is to say, send on a channel received from a channel.
5024 if (!this->peek_token()->is_op(OPERATOR_CHANOP))
5025 return true;
5027 e = Expression::make_receive(*channel, (*channel)->location());
5030 if (this->peek_token()->is_op(OPERATOR_EQ))
5032 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
5034 error_at(this->location(), "missing %<<-%>");
5035 return false;
5037 *is_send = false;
5038 this->advance_token();
5039 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5040 if (saw_comma)
5042 // case v, e = <-c:
5043 // *VAL is already set.
5044 if (!e->is_sink_expression())
5045 *closed = e;
5047 else
5049 // case v = <-c:
5050 if (!e->is_sink_expression())
5051 *val = e;
5053 return true;
5056 if (saw_comma)
5058 if (closed_is_id)
5059 error_at(this->location(), "expected %<=%> or %<:=%>");
5060 else
5061 error_at(this->location(), "expected %<=%>");
5062 return false;
5065 if (this->peek_token()->is_op(OPERATOR_CHANOP))
5067 // case c <- v:
5068 *is_send = true;
5069 *channel = this->verify_not_sink(e);
5070 this->advance_token();
5071 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5072 return true;
5075 error_at(this->location(), "expected %<<-%> or %<=%>");
5076 return false;
5079 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5080 // Condition = Expression .
5082 void
5083 Parse::for_stat(Label* label)
5085 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
5086 Location location = this->location();
5087 const Token* token = this->advance_token();
5089 // Open a block to hold any variables defined in the init statement
5090 // of the for statement.
5091 this->gogo_->start_block(location);
5093 Block* init = NULL;
5094 Expression* cond = NULL;
5095 Block* post = NULL;
5096 Range_clause range_clause;
5098 if (!token->is_op(OPERATOR_LCURLY))
5100 if (token->is_keyword(KEYWORD_VAR))
5102 error_at(this->location(),
5103 "var declaration not allowed in for initializer");
5104 this->var_decl();
5107 if (token->is_op(OPERATOR_SEMICOLON))
5108 this->for_clause(&cond, &post);
5109 else
5111 // We might be looking at a Condition, an InitStat, or a
5112 // RangeClause.
5113 bool saw_send_stmt;
5114 cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
5115 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5117 if (cond == NULL && !range_clause.found)
5119 if (saw_send_stmt)
5120 error_at(this->location(),
5121 ("send statement used as value; "
5122 "use select for non-blocking send"));
5123 else
5124 error_at(this->location(), "parse error in for statement");
5127 else
5129 if (range_clause.found)
5130 error_at(this->location(), "parse error after range clause");
5132 if (cond != NULL)
5134 // COND is actually an expression statement for
5135 // InitStat at the start of a ForClause.
5136 this->expression_stat(cond);
5137 cond = NULL;
5140 this->for_clause(&cond, &post);
5145 // Check for the easy error of a newline before starting the block.
5146 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5148 Location semi_loc = this->location();
5149 if (this->advance_token()->is_op(OPERATOR_LCURLY))
5150 error_at(semi_loc, "missing %<{%> after for clause");
5151 // Otherwise we will get an error when we call this->block
5152 // below.
5155 // Build the For_statement and note that it is the current target
5156 // for break and continue statements.
5158 For_statement* sfor;
5159 For_range_statement* srange;
5160 Statement* s;
5161 if (!range_clause.found)
5163 sfor = Statement::make_for_statement(init, cond, post, location);
5164 s = sfor;
5165 srange = NULL;
5167 else
5169 srange = Statement::make_for_range_statement(range_clause.index,
5170 range_clause.value,
5171 range_clause.range,
5172 location);
5173 s = srange;
5174 sfor = NULL;
5177 this->push_break_statement(s, label);
5178 this->push_continue_statement(s, label);
5180 // Gather the block of statements in the loop and add them to the
5181 // For_statement.
5183 this->gogo_->start_block(this->location());
5184 Location end_loc = this->block();
5185 Block* statements = this->gogo_->finish_block(end_loc);
5187 if (sfor != NULL)
5188 sfor->add_statements(statements);
5189 else
5190 srange->add_statements(statements);
5192 // This is no longer the break/continue target.
5193 this->pop_break_statement();
5194 this->pop_continue_statement();
5196 // Add the For_statement to the list of statements, and close out
5197 // the block we started to hold any variables defined in the for
5198 // statement.
5200 this->gogo_->add_statement(s);
5202 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
5203 location);
5206 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5207 // InitStat = SimpleStat .
5208 // PostStat = SimpleStat .
5210 // We have already read InitStat at this point.
5212 void
5213 Parse::for_clause(Expression** cond, Block** post)
5215 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
5216 this->advance_token();
5217 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5218 *cond = NULL;
5219 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
5221 error_at(this->location(), "missing %<{%> after for clause");
5222 *cond = NULL;
5223 *post = NULL;
5224 return;
5226 else
5227 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5228 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5229 error_at(this->location(), "expected semicolon");
5230 else
5231 this->advance_token();
5233 if (this->peek_token()->is_op(OPERATOR_LCURLY))
5234 *post = NULL;
5235 else
5237 this->gogo_->start_block(this->location());
5238 this->simple_stat(false, NULL, NULL, NULL);
5239 *post = this->gogo_->finish_block(this->location());
5243 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5245 // This is the := version. It is called with a list of identifiers.
5247 void
5248 Parse::range_clause_decl(const Typed_identifier_list* til,
5249 Range_clause* p_range_clause)
5251 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5252 Location location = this->location();
5254 p_range_clause->found = true;
5256 if (til->size() > 2)
5257 error_at(this->location(), "too many variables for range clause");
5259 this->advance_token();
5260 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL,
5261 NULL);
5262 p_range_clause->range = expr;
5264 if (til->empty())
5265 return;
5267 bool any_new = false;
5269 const Typed_identifier* pti = &til->front();
5270 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new,
5271 NULL, NULL);
5272 if (any_new && no->is_variable())
5273 no->var_value()->set_type_from_range_index();
5274 p_range_clause->index = Expression::make_var_reference(no, location);
5276 if (til->size() == 1)
5277 p_range_clause->value = NULL;
5278 else
5280 pti = &til->back();
5281 bool is_new = false;
5282 no = this->init_var(*pti, NULL, expr, true, true, &is_new, NULL, NULL);
5283 if (is_new && no->is_variable())
5284 no->var_value()->set_type_from_range_value();
5285 if (is_new)
5286 any_new = true;
5287 if (!Gogo::is_sink_name(pti->name()))
5288 p_range_clause->value = Expression::make_var_reference(no, location);
5291 if (!any_new)
5292 error_at(location, "variables redeclared but no variable is new");
5295 // The = version of RangeClause. This is called with a list of
5296 // expressions.
5298 void
5299 Parse::range_clause_expr(const Expression_list* vals,
5300 Range_clause* p_range_clause)
5302 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5304 p_range_clause->found = true;
5306 go_assert(vals->size() >= 1);
5307 if (vals->size() > 2)
5308 error_at(this->location(), "too many variables for range clause");
5310 this->advance_token();
5311 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
5312 NULL, NULL);
5314 if (vals->empty())
5315 return;
5317 p_range_clause->index = vals->front();
5318 if (vals->size() == 1)
5319 p_range_clause->value = NULL;
5320 else
5321 p_range_clause->value = vals->back();
5324 // Push a statement on the break stack.
5326 void
5327 Parse::push_break_statement(Statement* enclosing, Label* label)
5329 if (this->break_stack_ == NULL)
5330 this->break_stack_ = new Bc_stack();
5331 this->break_stack_->push_back(std::make_pair(enclosing, label));
5334 // Push a statement on the continue stack.
5336 void
5337 Parse::push_continue_statement(Statement* enclosing, Label* label)
5339 if (this->continue_stack_ == NULL)
5340 this->continue_stack_ = new Bc_stack();
5341 this->continue_stack_->push_back(std::make_pair(enclosing, label));
5344 // Pop the break stack.
5346 void
5347 Parse::pop_break_statement()
5349 this->break_stack_->pop_back();
5352 // Pop the continue stack.
5354 void
5355 Parse::pop_continue_statement()
5357 this->continue_stack_->pop_back();
5360 // Find a break or continue statement given a label name.
5362 Statement*
5363 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
5365 if (bc_stack == NULL)
5366 return NULL;
5367 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
5368 p != bc_stack->rend();
5369 ++p)
5371 if (p->second != NULL && p->second->name() == label)
5373 p->second->set_is_used();
5374 return p->first;
5377 return NULL;
5380 // BreakStat = "break" [ identifier ] .
5382 void
5383 Parse::break_stat()
5385 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
5386 Location location = this->location();
5388 const Token* token = this->advance_token();
5389 Statement* enclosing;
5390 if (!token->is_identifier())
5392 if (this->break_stack_ == NULL || this->break_stack_->empty())
5394 error_at(this->location(),
5395 "break statement not within for or switch or select");
5396 return;
5398 enclosing = this->break_stack_->back().first;
5400 else
5402 enclosing = this->find_bc_statement(this->break_stack_,
5403 token->identifier());
5404 if (enclosing == NULL)
5406 // If there is a label with this name, mark it as used to
5407 // avoid a useless error about an unused label.
5408 this->gogo_->add_label_reference(token->identifier(),
5409 Linemap::unknown_location(), false);
5411 error_at(token->location(), "invalid break label %qs",
5412 Gogo::message_name(token->identifier()).c_str());
5413 this->advance_token();
5414 return;
5416 this->advance_token();
5419 Unnamed_label* label;
5420 if (enclosing->classification() == Statement::STATEMENT_FOR)
5421 label = enclosing->for_statement()->break_label();
5422 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5423 label = enclosing->for_range_statement()->break_label();
5424 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
5425 label = enclosing->switch_statement()->break_label();
5426 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
5427 label = enclosing->type_switch_statement()->break_label();
5428 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
5429 label = enclosing->select_statement()->break_label();
5430 else
5431 go_unreachable();
5433 this->gogo_->add_statement(Statement::make_break_statement(label,
5434 location));
5437 // ContinueStat = "continue" [ identifier ] .
5439 void
5440 Parse::continue_stat()
5442 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
5443 Location location = this->location();
5445 const Token* token = this->advance_token();
5446 Statement* enclosing;
5447 if (!token->is_identifier())
5449 if (this->continue_stack_ == NULL || this->continue_stack_->empty())
5451 error_at(this->location(), "continue statement not within for");
5452 return;
5454 enclosing = this->continue_stack_->back().first;
5456 else
5458 enclosing = this->find_bc_statement(this->continue_stack_,
5459 token->identifier());
5460 if (enclosing == NULL)
5462 // If there is a label with this name, mark it as used to
5463 // avoid a useless error about an unused label.
5464 this->gogo_->add_label_reference(token->identifier(),
5465 Linemap::unknown_location(), false);
5467 error_at(token->location(), "invalid continue label %qs",
5468 Gogo::message_name(token->identifier()).c_str());
5469 this->advance_token();
5470 return;
5472 this->advance_token();
5475 Unnamed_label* label;
5476 if (enclosing->classification() == Statement::STATEMENT_FOR)
5477 label = enclosing->for_statement()->continue_label();
5478 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5479 label = enclosing->for_range_statement()->continue_label();
5480 else
5481 go_unreachable();
5483 this->gogo_->add_statement(Statement::make_continue_statement(label,
5484 location));
5487 // GotoStat = "goto" identifier .
5489 void
5490 Parse::goto_stat()
5492 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
5493 Location location = this->location();
5494 const Token* token = this->advance_token();
5495 if (!token->is_identifier())
5496 error_at(this->location(), "expected label for goto");
5497 else
5499 Label* label = this->gogo_->add_label_reference(token->identifier(),
5500 location, true);
5501 Statement* s = Statement::make_goto_statement(label, location);
5502 this->gogo_->add_statement(s);
5503 this->advance_token();
5507 // PackageClause = "package" PackageName .
5509 void
5510 Parse::package_clause()
5512 const Token* token = this->peek_token();
5513 Location location = token->location();
5514 std::string name;
5515 if (!token->is_keyword(KEYWORD_PACKAGE))
5517 error_at(this->location(), "program must start with package clause");
5518 name = "ERROR";
5520 else
5522 token = this->advance_token();
5523 if (token->is_identifier())
5525 name = token->identifier();
5526 if (name == "_")
5528 error_at(this->location(), "invalid package name _");
5529 name = Gogo::erroneous_name();
5531 this->advance_token();
5533 else
5535 error_at(this->location(), "package name must be an identifier");
5536 name = "ERROR";
5539 this->gogo_->set_package_name(name, location);
5542 // ImportDecl = "import" Decl<ImportSpec> .
5544 void
5545 Parse::import_decl()
5547 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
5548 this->advance_token();
5549 this->decl(&Parse::import_spec, NULL);
5552 // ImportSpec = [ "." | PackageName ] PackageFileName .
5554 void
5555 Parse::import_spec(void*)
5557 const Token* token = this->peek_token();
5558 Location location = token->location();
5560 std::string local_name;
5561 bool is_local_name_exported = false;
5562 if (token->is_op(OPERATOR_DOT))
5564 local_name = ".";
5565 token = this->advance_token();
5567 else if (token->is_identifier())
5569 local_name = token->identifier();
5570 is_local_name_exported = token->is_identifier_exported();
5571 token = this->advance_token();
5574 if (!token->is_string())
5576 error_at(this->location(), "import statement not a string");
5577 this->advance_token();
5578 return;
5581 this->gogo_->import_package(token->string_value(), local_name,
5582 is_local_name_exported, location);
5584 this->advance_token();
5587 // SourceFile = PackageClause ";" { ImportDecl ";" }
5588 // { TopLevelDecl ";" } .
5590 void
5591 Parse::program()
5593 this->package_clause();
5595 const Token* token = this->peek_token();
5596 if (token->is_op(OPERATOR_SEMICOLON))
5597 token = this->advance_token();
5598 else
5599 error_at(this->location(),
5600 "expected %<;%> or newline after package clause");
5602 while (token->is_keyword(KEYWORD_IMPORT))
5604 this->import_decl();
5605 token = this->peek_token();
5606 if (token->is_op(OPERATOR_SEMICOLON))
5607 token = this->advance_token();
5608 else
5609 error_at(this->location(),
5610 "expected %<;%> or newline after import declaration");
5613 while (!token->is_eof())
5615 if (this->declaration_may_start_here())
5616 this->declaration();
5617 else
5619 error_at(this->location(), "expected declaration");
5620 this->gogo_->mark_locals_used();
5622 this->advance_token();
5623 while (!this->peek_token()->is_eof()
5624 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
5625 && !this->peek_token()->is_op(OPERATOR_RCURLY));
5626 if (!this->peek_token()->is_eof()
5627 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
5628 this->advance_token();
5630 token = this->peek_token();
5631 if (token->is_op(OPERATOR_SEMICOLON))
5632 token = this->advance_token();
5633 else if (!token->is_eof() || !saw_errors())
5635 if (token->is_op(OPERATOR_CHANOP))
5636 error_at(this->location(),
5637 ("send statement used as value; "
5638 "use select for non-blocking send"));
5639 else
5640 error_at(this->location(),
5641 "expected %<;%> or newline after top level declaration");
5642 this->skip_past_error(OPERATOR_INVALID);
5647 // Reset the current iota value.
5649 void
5650 Parse::reset_iota()
5652 this->iota_ = 0;
5655 // Return the current iota value.
5658 Parse::iota_value()
5660 return this->iota_;
5663 // Increment the current iota value.
5665 void
5666 Parse::increment_iota()
5668 ++this->iota_;
5671 // Skip forward to a semicolon or OP. OP will normally be
5672 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5673 // past it and return. If we find OP, it will be the next token to
5674 // read. Return true if we are OK, false if we found EOF.
5676 bool
5677 Parse::skip_past_error(Operator op)
5679 this->gogo_->mark_locals_used();
5680 const Token* token = this->peek_token();
5681 while (!token->is_op(op))
5683 if (token->is_eof())
5684 return false;
5685 if (token->is_op(OPERATOR_SEMICOLON))
5687 this->advance_token();
5688 return true;
5690 token = this->advance_token();
5692 return true;
5695 // Check that an expression is not a sink.
5697 Expression*
5698 Parse::verify_not_sink(Expression* expr)
5700 if (expr->is_sink_expression())
5702 error_at(expr->location(), "cannot use _ as value");
5703 expr = Expression::make_error(expr->location());
5706 // If this can not be a sink, and it is a variable, then we are
5707 // using the variable, not just assigning to it.
5708 Var_expression* ve = expr->var_expression();
5709 if (ve != NULL)
5710 this->mark_var_used(ve->named_object());
5712 return expr;
5715 // Mark a variable as used.
5717 void
5718 Parse::mark_var_used(Named_object* no)
5720 if (no->is_variable())
5722 no->var_value()->set_is_used();
5724 // When a type switch uses := to define a variable, then for
5725 // each case with a single type we introduce a new variable with
5726 // the appropriate type. When we do, if the newly introduced
5727 // variable is used, then the type switch variable is used.
5728 Type_switch_vars::iterator p = this->type_switch_vars_.find(no);
5729 if (p != this->type_switch_vars_.end())
5730 p->second->var_value()->set_is_used();