compiler: fix test for mismatch between function results and uses
[official-gcc.git] / gcc / go / gofrontend / parse.cc
blobd7d3a072040ef14bfe147e1108ad1bd6da109560
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();
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 while (this->peek_token()->is_op(OPERATOR_COMMA))
980 if (this->advance_token()->is_op(OPERATOR_RPAREN))
981 break;
982 if (is_varargs != NULL && *is_varargs)
984 error_at(this->location(), "%<...%> must be last parameter");
985 saw_error = true;
987 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error);
989 if (mix_error)
991 error_at(location, "invalid named/anonymous mix");
992 saw_error = true;
994 if (saw_error)
996 delete ret;
997 return NULL;
999 return ret;
1002 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1004 void
1005 Parse::parameter_decl(bool parameters_have_names,
1006 Typed_identifier_list* til,
1007 bool* is_varargs,
1008 bool* mix_error)
1010 if (!parameters_have_names)
1012 Type* type;
1013 Location location = this->location();
1014 if (!this->peek_token()->is_identifier())
1016 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1017 type = this->type();
1018 else
1020 if (is_varargs == NULL)
1021 error_at(this->location(), "invalid use of %<...%>");
1022 else
1023 *is_varargs = true;
1024 this->advance_token();
1025 if (is_varargs == NULL
1026 && this->peek_token()->is_op(OPERATOR_RPAREN))
1027 type = Type::make_error_type();
1028 else
1030 Type* element_type = this->type();
1031 type = Type::make_array_type(element_type, NULL);
1035 else
1037 type = this->type_name(false);
1038 if (type->is_error_type()
1039 || (!this->peek_token()->is_op(OPERATOR_COMMA)
1040 && !this->peek_token()->is_op(OPERATOR_RPAREN)))
1042 *mix_error = true;
1043 while (!this->peek_token()->is_op(OPERATOR_COMMA)
1044 && !this->peek_token()->is_op(OPERATOR_RPAREN))
1045 this->advance_token();
1048 if (!type->is_error_type())
1049 til->push_back(Typed_identifier("", type, location));
1051 else
1053 size_t orig_count = til->size();
1054 if (this->peek_token()->is_identifier())
1055 this->identifier_list(til);
1056 else
1057 *mix_error = true;
1058 size_t new_count = til->size();
1060 Type* type;
1061 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
1062 type = this->type();
1063 else
1065 if (is_varargs == NULL)
1066 error_at(this->location(), "invalid use of %<...%>");
1067 else if (new_count > orig_count + 1)
1068 error_at(this->location(), "%<...%> only permits one name");
1069 else
1070 *is_varargs = true;
1071 this->advance_token();
1072 Type* element_type = this->type();
1073 type = Type::make_array_type(element_type, NULL);
1075 for (size_t i = orig_count; i < new_count; ++i)
1076 til->set_type(i, type);
1080 // Result = Parameters | Type .
1082 // This returns false on a parse error.
1084 bool
1085 Parse::result(Typed_identifier_list** presults)
1087 if (this->peek_token()->is_op(OPERATOR_LPAREN))
1088 return this->parameters(presults, NULL);
1089 else
1091 Location location = this->location();
1092 Type* type = this->type();
1093 if (type->is_error_type())
1095 *presults = NULL;
1096 return false;
1098 Typed_identifier_list* til = new Typed_identifier_list();
1099 til->push_back(Typed_identifier("", type, location));
1100 *presults = til;
1101 return true;
1105 // Block = "{" [ StatementList ] "}" .
1107 // Returns the location of the closing brace.
1109 Location
1110 Parse::block()
1112 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
1114 Location loc = this->location();
1115 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1116 && this->advance_token()->is_op(OPERATOR_LCURLY))
1117 error_at(loc, "unexpected semicolon or newline before %<{%>");
1118 else
1120 error_at(this->location(), "expected %<{%>");
1121 return Linemap::unknown_location();
1125 const Token* token = this->advance_token();
1127 if (!token->is_op(OPERATOR_RCURLY))
1129 this->statement_list();
1130 token = this->peek_token();
1131 if (!token->is_op(OPERATOR_RCURLY))
1133 if (!token->is_eof() || !saw_errors())
1134 error_at(this->location(), "expected %<}%>");
1136 this->gogo_->mark_locals_used();
1138 // Skip ahead to the end of the block, in hopes of avoiding
1139 // lots of meaningless errors.
1140 Location ret = token->location();
1141 int nest = 0;
1142 while (!token->is_eof())
1144 if (token->is_op(OPERATOR_LCURLY))
1145 ++nest;
1146 else if (token->is_op(OPERATOR_RCURLY))
1148 --nest;
1149 if (nest < 0)
1151 this->advance_token();
1152 break;
1155 token = this->advance_token();
1156 ret = token->location();
1158 return ret;
1162 Location ret = token->location();
1163 this->advance_token();
1164 return ret;
1167 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1168 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1170 Type*
1171 Parse::interface_type()
1173 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
1174 Location location = this->location();
1176 if (!this->advance_token()->is_op(OPERATOR_LCURLY))
1178 Location token_loc = this->location();
1179 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1180 && this->advance_token()->is_op(OPERATOR_LCURLY))
1181 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
1182 else
1184 error_at(this->location(), "expected %<{%>");
1185 return Type::make_error_type();
1188 this->advance_token();
1190 Typed_identifier_list* methods = new Typed_identifier_list();
1191 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1193 this->method_spec(methods);
1194 while (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1196 if (this->advance_token()->is_op(OPERATOR_RCURLY))
1197 break;
1198 this->method_spec(methods);
1200 if (!this->peek_token()->is_op(OPERATOR_RCURLY))
1202 error_at(this->location(), "expected %<}%>");
1203 while (!this->advance_token()->is_op(OPERATOR_RCURLY))
1205 if (this->peek_token()->is_eof())
1206 return Type::make_error_type();
1210 this->advance_token();
1212 if (methods->empty())
1214 delete methods;
1215 methods = NULL;
1218 Interface_type* ret = Type::make_interface_type(methods, location);
1219 this->gogo_->record_interface_type(ret);
1220 return ret;
1223 // MethodSpec = MethodName Signature | InterfaceTypeName .
1224 // MethodName = identifier .
1225 // InterfaceTypeName = TypeName .
1227 void
1228 Parse::method_spec(Typed_identifier_list* methods)
1230 const Token* token = this->peek_token();
1231 if (!token->is_identifier())
1233 error_at(this->location(), "expected identifier");
1234 return;
1237 std::string name = token->identifier();
1238 bool is_exported = token->is_identifier_exported();
1239 Location location = token->location();
1241 if (this->advance_token()->is_op(OPERATOR_LPAREN))
1243 // This is a MethodName.
1244 name = this->gogo_->pack_hidden_name(name, is_exported);
1245 Type* type = this->signature(NULL, location);
1246 if (type == NULL)
1247 return;
1248 methods->push_back(Typed_identifier(name, type, location));
1250 else
1252 this->unget_token(Token::make_identifier_token(name, is_exported,
1253 location));
1254 Type* type = this->type_name(false);
1255 if (type->is_error_type()
1256 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1257 && !this->peek_token()->is_op(OPERATOR_RCURLY)))
1259 if (this->peek_token()->is_op(OPERATOR_COMMA))
1260 error_at(this->location(),
1261 "name list not allowed in interface type");
1262 else
1263 error_at(location, "expected signature or type name");
1264 this->gogo_->mark_locals_used();
1265 token = this->peek_token();
1266 while (!token->is_eof()
1267 && !token->is_op(OPERATOR_SEMICOLON)
1268 && !token->is_op(OPERATOR_RCURLY))
1269 token = this->advance_token();
1270 return;
1272 // This must be an interface type, but we can't check that now.
1273 // We check it and pull out the methods in
1274 // Interface_type::do_verify.
1275 methods->push_back(Typed_identifier("", type, location));
1279 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1281 void
1282 Parse::declaration()
1284 const Token* token = this->peek_token();
1286 bool saw_nointerface = this->lex_->get_and_clear_nointerface();
1287 if (saw_nointerface && !token->is_keyword(KEYWORD_FUNC))
1288 warning_at(token->location(), 0,
1289 "ignoring magic //go:nointerface comment before non-method");
1291 if (token->is_keyword(KEYWORD_CONST))
1292 this->const_decl();
1293 else if (token->is_keyword(KEYWORD_TYPE))
1294 this->type_decl();
1295 else if (token->is_keyword(KEYWORD_VAR))
1296 this->var_decl();
1297 else if (token->is_keyword(KEYWORD_FUNC))
1298 this->function_decl(saw_nointerface);
1299 else
1301 error_at(this->location(), "expected declaration");
1302 this->advance_token();
1306 bool
1307 Parse::declaration_may_start_here()
1309 const Token* token = this->peek_token();
1310 return (token->is_keyword(KEYWORD_CONST)
1311 || token->is_keyword(KEYWORD_TYPE)
1312 || token->is_keyword(KEYWORD_VAR)
1313 || token->is_keyword(KEYWORD_FUNC));
1316 // Decl<P> = P | "(" [ List<P> ] ")" .
1318 void
1319 Parse::decl(void (Parse::*pfn)(void*), void* varg)
1321 if (this->peek_token()->is_eof())
1323 if (!saw_errors())
1324 error_at(this->location(), "unexpected end of file");
1325 return;
1328 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1329 (this->*pfn)(varg);
1330 else
1332 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
1334 this->list(pfn, varg, true);
1335 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1337 error_at(this->location(), "missing %<)%>");
1338 while (!this->advance_token()->is_op(OPERATOR_RPAREN))
1340 if (this->peek_token()->is_eof())
1341 return;
1345 this->advance_token();
1349 // List<P> = P { ";" P } [ ";" ] .
1351 // In order to pick up the trailing semicolon we need to know what
1352 // might follow. This is either a '}' or a ')'.
1354 void
1355 Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren)
1357 (this->*pfn)(varg);
1358 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY;
1359 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)
1360 || this->peek_token()->is_op(OPERATOR_COMMA))
1362 if (this->peek_token()->is_op(OPERATOR_COMMA))
1363 error_at(this->location(), "unexpected comma");
1364 if (this->advance_token()->is_op(follow))
1365 break;
1366 (this->*pfn)(varg);
1370 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1372 void
1373 Parse::const_decl()
1375 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST));
1376 this->advance_token();
1377 this->reset_iota();
1379 Type* last_type = NULL;
1380 Expression_list* last_expr_list = NULL;
1382 if (!this->peek_token()->is_op(OPERATOR_LPAREN))
1383 this->const_spec(&last_type, &last_expr_list);
1384 else
1386 this->advance_token();
1387 while (!this->peek_token()->is_op(OPERATOR_RPAREN))
1389 this->const_spec(&last_type, &last_expr_list);
1390 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
1391 this->advance_token();
1392 else if (!this->peek_token()->is_op(OPERATOR_RPAREN))
1394 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1395 if (!this->skip_past_error(OPERATOR_RPAREN))
1396 return;
1399 this->advance_token();
1402 if (last_expr_list != NULL)
1403 delete last_expr_list;
1406 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1408 void
1409 Parse::const_spec(Type** last_type, Expression_list** last_expr_list)
1411 Typed_identifier_list til;
1412 this->identifier_list(&til);
1414 Type* type = NULL;
1415 if (this->type_may_start_here())
1417 type = this->type();
1418 *last_type = NULL;
1419 *last_expr_list = NULL;
1422 Expression_list *expr_list;
1423 if (!this->peek_token()->is_op(OPERATOR_EQ))
1425 if (*last_expr_list == NULL)
1427 error_at(this->location(), "expected %<=%>");
1428 return;
1430 type = *last_type;
1431 expr_list = new Expression_list;
1432 for (Expression_list::const_iterator p = (*last_expr_list)->begin();
1433 p != (*last_expr_list)->end();
1434 ++p)
1435 expr_list->push_back((*p)->copy());
1437 else
1439 this->advance_token();
1440 expr_list = this->expression_list(NULL, false, true);
1441 *last_type = type;
1442 if (*last_expr_list != NULL)
1443 delete *last_expr_list;
1444 *last_expr_list = expr_list;
1447 Expression_list::const_iterator pe = expr_list->begin();
1448 for (Typed_identifier_list::iterator pi = til.begin();
1449 pi != til.end();
1450 ++pi, ++pe)
1452 if (pe == expr_list->end())
1454 error_at(this->location(), "not enough initializers");
1455 return;
1457 if (type != NULL)
1458 pi->set_type(type);
1460 if (!Gogo::is_sink_name(pi->name()))
1461 this->gogo_->add_constant(*pi, *pe, this->iota_value());
1462 else
1464 static int count;
1465 char buf[30];
1466 snprintf(buf, sizeof buf, ".$sinkconst%d", count);
1467 ++count;
1468 Typed_identifier ti(std::string(buf), type, pi->location());
1469 Named_object* no = this->gogo_->add_constant(ti, *pe, this->iota_value());
1470 no->const_value()->set_is_sink();
1473 if (pe != expr_list->end())
1474 error_at(this->location(), "too many initializers");
1476 this->increment_iota();
1478 return;
1481 // TypeDecl = "type" Decl<TypeSpec> .
1483 void
1484 Parse::type_decl()
1486 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE));
1487 this->advance_token();
1488 this->decl(&Parse::type_spec, NULL);
1491 // TypeSpec = identifier Type .
1493 void
1494 Parse::type_spec(void*)
1496 const Token* token = this->peek_token();
1497 if (!token->is_identifier())
1499 error_at(this->location(), "expected identifier");
1500 return;
1502 std::string name = token->identifier();
1503 bool is_exported = token->is_identifier_exported();
1504 Location location = token->location();
1505 token = this->advance_token();
1507 // The scope of the type name starts at the point where the
1508 // identifier appears in the source code. We implement this by
1509 // declaring the type before we read the type definition.
1510 Named_object* named_type = NULL;
1511 if (name != "_")
1513 name = this->gogo_->pack_hidden_name(name, is_exported);
1514 named_type = this->gogo_->declare_type(name, location);
1517 Type* type;
1518 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
1519 type = this->type();
1520 else
1522 error_at(this->location(),
1523 "unexpected semicolon or newline in type declaration");
1524 type = Type::make_error_type();
1525 this->advance_token();
1528 if (type->is_error_type())
1530 this->gogo_->mark_locals_used();
1531 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
1532 && !this->peek_token()->is_eof())
1533 this->advance_token();
1536 if (name != "_")
1538 if (named_type->is_type_declaration())
1540 Type* ftype = type->forwarded();
1541 if (ftype->forward_declaration_type() != NULL
1542 && (ftype->forward_declaration_type()->named_object()
1543 == named_type))
1545 error_at(location, "invalid recursive type");
1546 type = Type::make_error_type();
1549 this->gogo_->define_type(named_type,
1550 Type::make_named_type(named_type, type,
1551 location));
1552 go_assert(named_type->package() == NULL);
1554 else
1556 // This will probably give a redefinition error.
1557 this->gogo_->add_type(name, type, location);
1562 // VarDecl = "var" Decl<VarSpec> .
1564 void
1565 Parse::var_decl()
1567 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR));
1568 this->advance_token();
1569 this->decl(&Parse::var_spec, NULL);
1572 // VarSpec = IdentifierList
1573 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1575 void
1576 Parse::var_spec(void*)
1578 // Get the variable names.
1579 Typed_identifier_list til;
1580 this->identifier_list(&til);
1582 Location location = this->location();
1584 Type* type = NULL;
1585 Expression_list* init = NULL;
1586 if (!this->peek_token()->is_op(OPERATOR_EQ))
1588 type = this->type();
1589 if (type->is_error_type())
1591 this->gogo_->mark_locals_used();
1592 while (!this->peek_token()->is_op(OPERATOR_EQ)
1593 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
1594 && !this->peek_token()->is_eof())
1595 this->advance_token();
1597 if (this->peek_token()->is_op(OPERATOR_EQ))
1599 this->advance_token();
1600 init = this->expression_list(NULL, false, true);
1603 else
1605 this->advance_token();
1606 init = this->expression_list(NULL, false, true);
1609 this->init_vars(&til, type, init, false, location);
1611 if (init != NULL)
1612 delete init;
1615 // Create variables. TIL is a list of variable names. If TYPE is not
1616 // NULL, it is the type of all the variables. If INIT is not NULL, it
1617 // is an initializer list for the variables.
1619 void
1620 Parse::init_vars(const Typed_identifier_list* til, Type* type,
1621 Expression_list* init, bool is_coloneq,
1622 Location location)
1624 // Check for an initialization which can yield multiple values.
1625 if (init != NULL && init->size() == 1 && til->size() > 1)
1627 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq,
1628 location))
1629 return;
1630 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq,
1631 location))
1632 return;
1633 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq,
1634 location))
1635 return;
1636 if (this->init_vars_from_type_guard(til, type, *init->begin(),
1637 is_coloneq, location))
1638 return;
1641 if (init != NULL && init->size() != til->size())
1643 if (init->empty() || !init->front()->is_error_expression())
1644 error_at(location, "wrong number of initializations");
1645 init = NULL;
1646 if (type == NULL)
1647 type = Type::make_error_type();
1650 // Note that INIT was already parsed with the old name bindings, so
1651 // we don't have to worry that it will accidentally refer to the
1652 // newly declared variables. But we do have to worry about a mix of
1653 // newly declared variables and old variables if the old variables
1654 // appear in the initializations.
1656 Expression_list::const_iterator pexpr;
1657 if (init != NULL)
1658 pexpr = init->begin();
1659 bool any_new = false;
1660 Expression_list* vars = new Expression_list();
1661 Expression_list* vals = new Expression_list();
1662 for (Typed_identifier_list::const_iterator p = til->begin();
1663 p != til->end();
1664 ++p)
1666 if (init != NULL)
1667 go_assert(pexpr != init->end());
1668 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq,
1669 false, &any_new, vars, vals);
1670 if (init != NULL)
1671 ++pexpr;
1673 if (init != NULL)
1674 go_assert(pexpr == init->end());
1675 if (is_coloneq && !any_new)
1676 error_at(location, "variables redeclared but no variable is new");
1677 this->finish_init_vars(vars, vals, location);
1680 // See if we need to initialize a list of variables from a function
1681 // call. This returns true if we have set up the variables and the
1682 // initialization.
1684 bool
1685 Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
1686 Expression* expr, bool is_coloneq,
1687 Location location)
1689 Call_expression* call = expr->call_expression();
1690 if (call == NULL)
1691 return false;
1693 // This is a function call. We can't check here whether it returns
1694 // the right number of values, but it might. Declare the variables,
1695 // and then assign the results of the call to them.
1697 call->set_expected_result_count(vars->size());
1699 Named_object* first_var = NULL;
1700 unsigned int index = 0;
1701 bool any_new = false;
1702 Expression_list* ivars = new Expression_list();
1703 Expression_list* ivals = new Expression_list();
1704 for (Typed_identifier_list::const_iterator pv = vars->begin();
1705 pv != vars->end();
1706 ++pv, ++index)
1708 Expression* init = Expression::make_call_result(call, index);
1709 Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
1710 &any_new, ivars, ivals);
1712 if (this->gogo_->in_global_scope() && no->is_variable())
1714 if (first_var == NULL)
1715 first_var = no;
1716 else
1718 // The subsequent vars have an implicit dependency on
1719 // the first one, so that everything gets initialized in
1720 // the right order and so that we detect cycles
1721 // correctly.
1722 this->gogo_->record_var_depends_on(no->var_value(), first_var);
1727 if (is_coloneq && !any_new)
1728 error_at(location, "variables redeclared but no variable is new");
1730 this->finish_init_vars(ivars, ivals, location);
1732 return true;
1735 // See if we need to initialize a pair of values from a map index
1736 // expression. This returns true if we have set up the variables and
1737 // the initialization.
1739 bool
1740 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1741 Expression* expr, bool is_coloneq,
1742 Location location)
1744 Index_expression* index = expr->index_expression();
1745 if (index == NULL)
1746 return false;
1747 if (vars->size() != 2)
1748 return false;
1750 // This is an index which is being assigned to two variables. It
1751 // must be a map index. Declare the variables, and then assign the
1752 // results of the map index.
1753 bool any_new = false;
1754 Typed_identifier_list::const_iterator p = vars->begin();
1755 Expression* init = type == NULL ? index : NULL;
1756 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1757 type == NULL, &any_new, NULL, NULL);
1758 if (type == NULL && any_new && val_no->is_variable())
1759 val_no->var_value()->set_type_from_init_tuple();
1760 Expression* val_var = Expression::make_var_reference(val_no, location);
1762 ++p;
1763 Type* var_type = type;
1764 if (var_type == NULL)
1765 var_type = Type::lookup_bool_type();
1766 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1767 &any_new, NULL, NULL);
1768 Expression* present_var = Expression::make_var_reference(no, location);
1770 if (is_coloneq && !any_new)
1771 error_at(location, "variables redeclared but no variable is new");
1773 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1774 index, location);
1776 if (!this->gogo_->in_global_scope())
1777 this->gogo_->add_statement(s);
1778 else if (!val_no->is_sink())
1780 if (val_no->is_variable())
1781 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1783 else if (!no->is_sink())
1785 if (no->is_variable())
1786 no->var_value()->add_preinit_statement(this->gogo_, s);
1788 else
1790 // Execute the map index expression just so that we can fail if
1791 // the map is nil.
1792 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1793 NULL, location);
1794 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1797 return true;
1800 // See if we need to initialize a pair of values from a receive
1801 // expression. This returns true if we have set up the variables and
1802 // the initialization.
1804 bool
1805 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1806 Expression* expr, bool is_coloneq,
1807 Location location)
1809 Receive_expression* receive = expr->receive_expression();
1810 if (receive == NULL)
1811 return false;
1812 if (vars->size() != 2)
1813 return false;
1815 // This is a receive expression which is being assigned to two
1816 // variables. Declare the variables, and then assign the results of
1817 // the receive.
1818 bool any_new = false;
1819 Typed_identifier_list::const_iterator p = vars->begin();
1820 Expression* init = type == NULL ? receive : NULL;
1821 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1822 type == NULL, &any_new, NULL, NULL);
1823 if (type == NULL && any_new && val_no->is_variable())
1824 val_no->var_value()->set_type_from_init_tuple();
1825 Expression* val_var = Expression::make_var_reference(val_no, location);
1827 ++p;
1828 Type* var_type = type;
1829 if (var_type == NULL)
1830 var_type = Type::lookup_bool_type();
1831 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1832 &any_new, NULL, NULL);
1833 Expression* received_var = Expression::make_var_reference(no, location);
1835 if (is_coloneq && !any_new)
1836 error_at(location, "variables redeclared but no variable is new");
1838 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1839 received_var,
1840 receive->channel(),
1841 location);
1843 if (!this->gogo_->in_global_scope())
1844 this->gogo_->add_statement(s);
1845 else if (!val_no->is_sink())
1847 if (val_no->is_variable())
1848 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1850 else if (!no->is_sink())
1852 if (no->is_variable())
1853 no->var_value()->add_preinit_statement(this->gogo_, s);
1855 else
1857 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1858 NULL, location);
1859 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1862 return true;
1865 // See if we need to initialize a pair of values from a type guard
1866 // expression. This returns true if we have set up the variables and
1867 // the initialization.
1869 bool
1870 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1871 Type* type, Expression* expr,
1872 bool is_coloneq, Location location)
1874 Type_guard_expression* type_guard = expr->type_guard_expression();
1875 if (type_guard == NULL)
1876 return false;
1877 if (vars->size() != 2)
1878 return false;
1880 // This is a type guard expression which is being assigned to two
1881 // variables. Declare the variables, and then assign the results of
1882 // the type guard.
1883 bool any_new = false;
1884 Typed_identifier_list::const_iterator p = vars->begin();
1885 Type* var_type = type;
1886 if (var_type == NULL)
1887 var_type = type_guard->type();
1888 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1889 &any_new, NULL, NULL);
1890 Expression* val_var = Expression::make_var_reference(val_no, location);
1892 ++p;
1893 var_type = type;
1894 if (var_type == NULL)
1895 var_type = Type::lookup_bool_type();
1896 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1897 &any_new, NULL, NULL);
1898 Expression* ok_var = Expression::make_var_reference(no, location);
1900 Expression* texpr = type_guard->expr();
1901 Type* t = type_guard->type();
1902 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1903 texpr, t,
1904 location);
1906 if (is_coloneq && !any_new)
1907 error_at(location, "variables redeclared but no variable is new");
1909 if (!this->gogo_->in_global_scope())
1910 this->gogo_->add_statement(s);
1911 else if (!val_no->is_sink())
1913 if (val_no->is_variable())
1914 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1916 else if (!no->is_sink())
1918 if (no->is_variable())
1919 no->var_value()->add_preinit_statement(this->gogo_, s);
1921 else
1923 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1924 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1927 return true;
1930 // Create a single variable. If IS_COLONEQ is true, we permit
1931 // redeclarations in the same block, and we set *IS_NEW when we find a
1932 // new variable which is not a redeclaration.
1934 Named_object*
1935 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1936 bool is_coloneq, bool type_from_init, bool* is_new,
1937 Expression_list* vars, Expression_list* vals)
1939 Location location = tid.location();
1941 if (Gogo::is_sink_name(tid.name()))
1943 if (!type_from_init && init != NULL)
1945 if (this->gogo_->in_global_scope())
1946 return this->create_dummy_global(type, init, location);
1947 else
1949 // Create a dummy variable so that we will check whether the
1950 // initializer can be assigned to the type.
1951 Variable* var = new Variable(type, init, false, false, false,
1952 location);
1953 var->set_is_used();
1954 static int count;
1955 char buf[30];
1956 snprintf(buf, sizeof buf, "sink$%d", count);
1957 ++count;
1958 return this->gogo_->add_variable(buf, var);
1961 if (type != NULL)
1962 this->gogo_->add_type_to_verify(type);
1963 return this->gogo_->add_sink();
1966 if (is_coloneq)
1968 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1969 if (no != NULL
1970 && (no->is_variable() || no->is_result_variable()))
1972 // INIT may be NULL even when IS_COLONEQ is true for cases
1973 // like v, ok := x.(int).
1974 if (!type_from_init && init != NULL)
1976 go_assert(vars != NULL && vals != NULL);
1977 vars->push_back(Expression::make_var_reference(no, location));
1978 vals->push_back(init);
1980 return no;
1983 *is_new = true;
1984 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1985 false, false, location);
1986 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1987 if (!no->is_variable())
1989 // The name is already defined, so we just gave an error.
1990 return this->gogo_->add_sink();
1992 return no;
1995 // Create a dummy global variable to force an initializer to be run in
1996 // the right place. This is used when a sink variable is initialized
1997 // at global scope.
1999 Named_object*
2000 Parse::create_dummy_global(Type* type, Expression* init,
2001 Location location)
2003 if (type == NULL && init == NULL)
2004 type = Type::lookup_bool_type();
2005 Variable* var = new Variable(type, init, true, false, false, location);
2006 static int count;
2007 char buf[30];
2008 snprintf(buf, sizeof buf, "_.%d", count);
2009 ++count;
2010 return this->gogo_->add_variable(buf, var);
2013 // Finish the variable initialization by executing any assignments to
2014 // existing variables when using :=. These must be done as a tuple
2015 // assignment in case of something like n, a, b := 1, b, a.
2017 void
2018 Parse::finish_init_vars(Expression_list* vars, Expression_list* vals,
2019 Location location)
2021 if (vars->empty())
2023 delete vars;
2024 delete vals;
2026 else if (vars->size() == 1)
2028 go_assert(!this->gogo_->in_global_scope());
2029 this->gogo_->add_statement(Statement::make_assignment(vars->front(),
2030 vals->front(),
2031 location));
2032 delete vars;
2033 delete vals;
2035 else
2037 go_assert(!this->gogo_->in_global_scope());
2038 this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals,
2039 location));
2043 // SimpleVarDecl = identifier ":=" Expression .
2045 // We've already seen the identifier.
2047 // FIXME: We also have to implement
2048 // IdentifierList ":=" ExpressionList
2049 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2050 // tuple assignments here as well.
2052 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2053 // side may be a composite literal.
2055 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2056 // RangeClause.
2058 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2059 // guard (var := expr.("type") using the literal keyword "type").
2061 void
2062 Parse::simple_var_decl_or_assignment(const std::string& name,
2063 Location location,
2064 bool may_be_composite_lit,
2065 Range_clause* p_range_clause,
2066 Type_switch* p_type_switch)
2068 Typed_identifier_list til;
2069 til.push_back(Typed_identifier(name, NULL, location));
2071 // We've seen one identifier. If we see a comma now, this could be
2072 // "a, *p = 1, 2".
2073 if (this->peek_token()->is_op(OPERATOR_COMMA))
2075 go_assert(p_type_switch == NULL);
2076 while (true)
2078 const Token* token = this->advance_token();
2079 if (!token->is_identifier())
2080 break;
2082 std::string id = token->identifier();
2083 bool is_id_exported = token->is_identifier_exported();
2084 Location id_location = token->location();
2086 token = this->advance_token();
2087 if (!token->is_op(OPERATOR_COMMA))
2089 if (token->is_op(OPERATOR_COLONEQ))
2091 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2092 til.push_back(Typed_identifier(id, NULL, location));
2094 else
2095 this->unget_token(Token::make_identifier_token(id,
2096 is_id_exported,
2097 id_location));
2098 break;
2101 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2102 til.push_back(Typed_identifier(id, NULL, location));
2105 // We have a comma separated list of identifiers in TIL. If the
2106 // next token is COLONEQ, then this is a simple var decl, and we
2107 // have the complete list of identifiers. If the next token is
2108 // not COLONEQ, then the only valid parse is a tuple assignment.
2109 // The list of identifiers we have so far is really a list of
2110 // expressions. There are more expressions following.
2112 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2114 Expression_list* exprs = new Expression_list;
2115 for (Typed_identifier_list::const_iterator p = til.begin();
2116 p != til.end();
2117 ++p)
2118 exprs->push_back(this->id_to_expression(p->name(),
2119 p->location()));
2121 Expression_list* more_exprs =
2122 this->expression_list(NULL, true, may_be_composite_lit);
2123 for (Expression_list::const_iterator p = more_exprs->begin();
2124 p != more_exprs->end();
2125 ++p)
2126 exprs->push_back(*p);
2127 delete more_exprs;
2129 this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
2130 return;
2134 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2135 const Token* token = this->advance_token();
2137 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2139 this->range_clause_decl(&til, p_range_clause);
2140 return;
2143 Expression_list* init;
2144 if (p_type_switch == NULL)
2145 init = this->expression_list(NULL, false, may_be_composite_lit);
2146 else
2148 bool is_type_switch = false;
2149 Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
2150 may_be_composite_lit,
2151 &is_type_switch, NULL);
2152 if (is_type_switch)
2154 p_type_switch->found = true;
2155 p_type_switch->name = name;
2156 p_type_switch->location = location;
2157 p_type_switch->expr = expr;
2158 return;
2161 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2163 init = new Expression_list();
2164 init->push_back(expr);
2166 else
2168 this->advance_token();
2169 init = this->expression_list(expr, false, may_be_composite_lit);
2173 this->init_vars(&til, NULL, init, true, location);
2176 // FunctionDecl = "func" identifier Signature [ Block ] .
2177 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2179 // Deprecated gcc extension:
2180 // FunctionDecl = "func" identifier Signature
2181 // __asm__ "(" string_lit ")" .
2182 // This extension means a function whose real name is the identifier
2183 // inside the asm. This extension will be removed at some future
2184 // date. It has been replaced with //extern comments.
2186 // SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment,
2187 // which means that we omit the method from the type descriptor.
2189 void
2190 Parse::function_decl(bool saw_nointerface)
2192 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2193 Location location = this->location();
2194 std::string extern_name = this->lex_->extern_name();
2195 const Token* token = this->advance_token();
2197 Typed_identifier* rec = NULL;
2198 if (token->is_op(OPERATOR_LPAREN))
2200 rec = this->receiver();
2201 token = this->peek_token();
2203 else if (saw_nointerface)
2205 warning_at(location, 0,
2206 "ignoring magic //go:nointerface comment before non-method");
2207 saw_nointerface = false;
2210 if (!token->is_identifier())
2212 error_at(this->location(), "expected function name");
2213 return;
2216 std::string name =
2217 this->gogo_->pack_hidden_name(token->identifier(),
2218 token->is_identifier_exported());
2220 this->advance_token();
2222 Function_type* fntype = this->signature(rec, this->location());
2224 Named_object* named_object = NULL;
2226 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2228 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2230 error_at(this->location(), "expected %<(%>");
2231 return;
2233 token = this->advance_token();
2234 if (!token->is_string())
2236 error_at(this->location(), "expected string");
2237 return;
2239 std::string asm_name = token->string_value();
2240 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2242 error_at(this->location(), "expected %<)%>");
2243 return;
2245 this->advance_token();
2246 if (!Gogo::is_sink_name(name))
2248 named_object = this->gogo_->declare_function(name, fntype, location);
2249 if (named_object->is_function_declaration())
2250 named_object->func_declaration_value()->set_asm_name(asm_name);
2254 // Check for the easy error of a newline before the opening brace.
2255 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2257 Location semi_loc = this->location();
2258 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2259 error_at(this->location(),
2260 "unexpected semicolon or newline before %<{%>");
2261 else
2262 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2263 semi_loc));
2266 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2268 if (named_object == NULL && !Gogo::is_sink_name(name))
2270 if (fntype == NULL)
2271 this->gogo_->add_erroneous_name(name);
2272 else
2274 named_object = this->gogo_->declare_function(name, fntype,
2275 location);
2276 if (!extern_name.empty()
2277 && named_object->is_function_declaration())
2279 Function_declaration* fd =
2280 named_object->func_declaration_value();
2281 fd->set_asm_name(extern_name);
2286 if (saw_nointerface)
2287 warning_at(location, 0,
2288 ("ignoring magic //go:nointerface comment "
2289 "before declaration"));
2291 else
2293 bool hold_is_erroneous_function = this->is_erroneous_function_;
2294 if (fntype == NULL)
2296 fntype = Type::make_function_type(NULL, NULL, NULL, location);
2297 this->is_erroneous_function_ = true;
2298 if (!Gogo::is_sink_name(name))
2299 this->gogo_->add_erroneous_name(name);
2300 name = this->gogo_->pack_hidden_name("_", false);
2302 named_object = this->gogo_->start_function(name, fntype, true, location);
2303 Location end_loc = this->block();
2304 this->gogo_->finish_function(end_loc);
2305 if (saw_nointerface
2306 && !this->is_erroneous_function_
2307 && named_object->is_function())
2308 named_object->func_value()->set_nointerface();
2309 this->is_erroneous_function_ = hold_is_erroneous_function;
2313 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2314 // BaseTypeName = identifier .
2316 Typed_identifier*
2317 Parse::receiver()
2319 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2321 std::string name;
2322 const Token* token = this->advance_token();
2323 Location location = token->location();
2324 if (!token->is_op(OPERATOR_MULT))
2326 if (!token->is_identifier())
2328 error_at(this->location(), "method has no receiver");
2329 this->gogo_->mark_locals_used();
2330 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2331 token = this->advance_token();
2332 if (!token->is_eof())
2333 this->advance_token();
2334 return NULL;
2336 name = token->identifier();
2337 bool is_exported = token->is_identifier_exported();
2338 token = this->advance_token();
2339 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2341 // An identifier followed by something other than a dot or a
2342 // right parenthesis must be a receiver name followed by a
2343 // type.
2344 name = this->gogo_->pack_hidden_name(name, is_exported);
2346 else
2348 // This must be a type name.
2349 this->unget_token(Token::make_identifier_token(name, is_exported,
2350 location));
2351 token = this->peek_token();
2352 name.clear();
2356 // Here the receiver name is in NAME (it is empty if the receiver is
2357 // unnamed) and TOKEN is the first token in the type.
2359 bool is_pointer = false;
2360 if (token->is_op(OPERATOR_MULT))
2362 is_pointer = true;
2363 token = this->advance_token();
2366 if (!token->is_identifier())
2368 error_at(this->location(), "expected receiver name or type");
2369 this->gogo_->mark_locals_used();
2370 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2371 while (!token->is_eof())
2373 token = this->advance_token();
2374 if (token->is_op(OPERATOR_LPAREN))
2375 ++c;
2376 else if (token->is_op(OPERATOR_RPAREN))
2378 if (c == 0)
2379 break;
2380 --c;
2383 if (!token->is_eof())
2384 this->advance_token();
2385 return NULL;
2388 Type* type = this->type_name(true);
2390 if (is_pointer && !type->is_error_type())
2391 type = Type::make_pointer_type(type);
2393 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2394 this->advance_token();
2395 else
2397 if (this->peek_token()->is_op(OPERATOR_COMMA))
2398 error_at(this->location(), "method has multiple receivers");
2399 else
2400 error_at(this->location(), "expected %<)%>");
2401 this->gogo_->mark_locals_used();
2402 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2403 token = this->advance_token();
2404 if (!token->is_eof())
2405 this->advance_token();
2406 return NULL;
2409 return new Typed_identifier(name, type, location);
2412 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2413 // Literal = BasicLit | CompositeLit | FunctionLit .
2414 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2416 // If MAY_BE_SINK is true, this operand may be "_".
2418 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2419 // if the entire expression is in parentheses.
2421 Expression*
2422 Parse::operand(bool may_be_sink, bool* is_parenthesized)
2424 const Token* token = this->peek_token();
2425 Expression* ret;
2426 switch (token->classification())
2428 case Token::TOKEN_IDENTIFIER:
2430 Location location = token->location();
2431 std::string id = token->identifier();
2432 bool is_exported = token->is_identifier_exported();
2433 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2435 Named_object* in_function;
2436 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2438 Package* package = NULL;
2439 if (named_object != NULL && named_object->is_package())
2441 if (!this->advance_token()->is_op(OPERATOR_DOT)
2442 || !this->advance_token()->is_identifier())
2444 error_at(location, "unexpected reference to package");
2445 return Expression::make_error(location);
2447 package = named_object->package_value();
2448 package->set_used();
2449 id = this->peek_token()->identifier();
2450 is_exported = this->peek_token()->is_identifier_exported();
2451 packed = this->gogo_->pack_hidden_name(id, is_exported);
2452 named_object = package->lookup(packed);
2453 location = this->location();
2454 go_assert(in_function == NULL);
2457 this->advance_token();
2459 if (named_object != NULL
2460 && named_object->is_type()
2461 && !named_object->type_value()->is_visible())
2463 go_assert(package != NULL);
2464 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2465 Gogo::message_name(package->package_name()).c_str(),
2466 Gogo::message_name(id).c_str());
2467 return Expression::make_error(location);
2471 if (named_object == NULL)
2473 if (package != NULL)
2475 std::string n1 = Gogo::message_name(package->package_name());
2476 std::string n2 = Gogo::message_name(id);
2477 if (!is_exported)
2478 error_at(location,
2479 ("invalid reference to unexported identifier "
2480 "%<%s.%s%>"),
2481 n1.c_str(), n2.c_str());
2482 else
2483 error_at(location,
2484 "reference to undefined identifier %<%s.%s%>",
2485 n1.c_str(), n2.c_str());
2486 return Expression::make_error(location);
2489 named_object = this->gogo_->add_unknown_name(packed, location);
2492 if (in_function != NULL
2493 && in_function != this->gogo_->current_function()
2494 && (named_object->is_variable()
2495 || named_object->is_result_variable()))
2496 return this->enclosing_var_reference(in_function, named_object,
2497 location);
2499 switch (named_object->classification())
2501 case Named_object::NAMED_OBJECT_CONST:
2502 return Expression::make_const_reference(named_object, location);
2503 case Named_object::NAMED_OBJECT_TYPE:
2504 return Expression::make_type(named_object->type_value(), location);
2505 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2507 Type* t = Type::make_forward_declaration(named_object);
2508 return Expression::make_type(t, location);
2510 case Named_object::NAMED_OBJECT_VAR:
2511 case Named_object::NAMED_OBJECT_RESULT_VAR:
2512 this->mark_var_used(named_object);
2513 return Expression::make_var_reference(named_object, location);
2514 case Named_object::NAMED_OBJECT_SINK:
2515 if (may_be_sink)
2516 return Expression::make_sink(location);
2517 else
2519 error_at(location, "cannot use _ as value");
2520 return Expression::make_error(location);
2522 case Named_object::NAMED_OBJECT_FUNC:
2523 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2524 return Expression::make_func_reference(named_object, NULL,
2525 location);
2526 case Named_object::NAMED_OBJECT_UNKNOWN:
2528 Unknown_expression* ue =
2529 Expression::make_unknown_reference(named_object, location);
2530 if (this->is_erroneous_function_)
2531 ue->set_no_error_message();
2532 return ue;
2534 case Named_object::NAMED_OBJECT_ERRONEOUS:
2535 return Expression::make_error(location);
2536 default:
2537 go_unreachable();
2540 go_unreachable();
2542 case Token::TOKEN_STRING:
2543 ret = Expression::make_string(token->string_value(), token->location());
2544 this->advance_token();
2545 return ret;
2547 case Token::TOKEN_CHARACTER:
2548 ret = Expression::make_character(token->character_value(), NULL,
2549 token->location());
2550 this->advance_token();
2551 return ret;
2553 case Token::TOKEN_INTEGER:
2554 ret = Expression::make_integer(token->integer_value(), NULL,
2555 token->location());
2556 this->advance_token();
2557 return ret;
2559 case Token::TOKEN_FLOAT:
2560 ret = Expression::make_float(token->float_value(), NULL,
2561 token->location());
2562 this->advance_token();
2563 return ret;
2565 case Token::TOKEN_IMAGINARY:
2567 mpfr_t zero;
2568 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2569 ret = Expression::make_complex(&zero, token->imaginary_value(),
2570 NULL, token->location());
2571 mpfr_clear(zero);
2572 this->advance_token();
2573 return ret;
2576 case Token::TOKEN_KEYWORD:
2577 switch (token->keyword())
2579 case KEYWORD_FUNC:
2580 return this->function_lit();
2581 case KEYWORD_CHAN:
2582 case KEYWORD_INTERFACE:
2583 case KEYWORD_MAP:
2584 case KEYWORD_STRUCT:
2586 Location location = token->location();
2587 return Expression::make_type(this->type(), location);
2589 default:
2590 break;
2592 break;
2594 case Token::TOKEN_OPERATOR:
2595 if (token->is_op(OPERATOR_LPAREN))
2597 this->advance_token();
2598 ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL,
2599 NULL);
2600 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2601 error_at(this->location(), "missing %<)%>");
2602 else
2603 this->advance_token();
2604 if (is_parenthesized != NULL)
2605 *is_parenthesized = true;
2606 return ret;
2608 else if (token->is_op(OPERATOR_LSQUARE))
2610 // Here we call array_type directly, as this is the only
2611 // case where an ellipsis is permitted for an array type.
2612 Location location = token->location();
2613 return Expression::make_type(this->array_type(true), location);
2615 break;
2617 default:
2618 break;
2621 error_at(this->location(), "expected operand");
2622 return Expression::make_error(this->location());
2625 // Handle a reference to a variable in an enclosing function. We add
2626 // it to a list of such variables. We return a reference to a field
2627 // in a struct which will be passed on the static chain when calling
2628 // the current function.
2630 Expression*
2631 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2632 Location location)
2634 go_assert(var->is_variable() || var->is_result_variable());
2636 this->mark_var_used(var);
2638 Named_object* this_function = this->gogo_->current_function();
2639 Named_object* closure = this_function->func_value()->closure_var();
2641 // The last argument to the Enclosing_var constructor is the index
2642 // of this variable in the closure. We add 1 to the current number
2643 // of enclosed variables, because the first field in the closure
2644 // points to the function code.
2645 Enclosing_var ev(var, in_function, this->enclosing_vars_.size() + 1);
2646 std::pair<Enclosing_vars::iterator, bool> ins =
2647 this->enclosing_vars_.insert(ev);
2648 if (ins.second)
2650 // This is a variable we have not seen before. Add a new field
2651 // to the closure type.
2652 this_function->func_value()->add_closure_field(var, location);
2655 Expression* closure_ref = Expression::make_var_reference(closure,
2656 location);
2657 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2659 // The closure structure holds pointers to the variables, so we need
2660 // to introduce an indirection.
2661 Expression* e = Expression::make_field_reference(closure_ref,
2662 ins.first->index(),
2663 location);
2664 e = Expression::make_unary(OPERATOR_MULT, e, location);
2665 return e;
2668 // CompositeLit = LiteralType LiteralValue .
2669 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2670 // SliceType | MapType | TypeName .
2671 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2672 // ElementList = Element { "," Element } .
2673 // Element = [ Key ":" ] Value .
2674 // Key = FieldName | ElementIndex .
2675 // FieldName = identifier .
2676 // ElementIndex = Expression .
2677 // Value = Expression | LiteralValue .
2679 // We have already seen the type if there is one, and we are now
2680 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2681 // will be seen here as an array type whose length is "nil". The
2682 // DEPTH parameter is non-zero if this is an embedded composite
2683 // literal and the type was omitted. It gives the number of steps up
2684 // to the type which was provided. E.g., in [][]int{{1}} it will be
2685 // 1. In [][][]int{{{1}}} it will be 2.
2687 Expression*
2688 Parse::composite_lit(Type* type, int depth, Location location)
2690 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2691 this->advance_token();
2693 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2695 this->advance_token();
2696 return Expression::make_composite_literal(type, depth, false, NULL,
2697 false, location);
2700 bool has_keys = false;
2701 bool all_are_names = true;
2702 Expression_list* vals = new Expression_list;
2703 while (true)
2705 Expression* val;
2706 bool is_type_omitted = false;
2707 bool is_name = false;
2709 const Token* token = this->peek_token();
2711 if (token->is_identifier())
2713 std::string identifier = token->identifier();
2714 bool is_exported = token->is_identifier_exported();
2715 Location location = token->location();
2717 if (this->advance_token()->is_op(OPERATOR_COLON))
2719 // This may be a field name. We don't know for sure--it
2720 // could also be an expression for an array index. We
2721 // don't want to parse it as an expression because may
2722 // trigger various errors, e.g., if this identifier
2723 // happens to be the name of a package.
2724 Gogo* gogo = this->gogo_;
2725 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2726 is_exported),
2727 location);
2728 is_name = true;
2730 else
2732 this->unget_token(Token::make_identifier_token(identifier,
2733 is_exported,
2734 location));
2735 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
2736 NULL);
2739 else if (!token->is_op(OPERATOR_LCURLY))
2740 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2741 else
2743 // This must be a composite literal inside another composite
2744 // literal, with the type omitted for the inner one.
2745 val = this->composite_lit(type, depth + 1, token->location());
2746 is_type_omitted = true;
2749 token = this->peek_token();
2750 if (!token->is_op(OPERATOR_COLON))
2752 if (has_keys)
2753 vals->push_back(NULL);
2754 is_name = false;
2756 else
2758 if (is_type_omitted && !val->is_error_expression())
2760 error_at(this->location(), "unexpected %<:%>");
2761 val = Expression::make_error(this->location());
2764 this->advance_token();
2766 if (!has_keys && !vals->empty())
2768 Expression_list* newvals = new Expression_list;
2769 for (Expression_list::const_iterator p = vals->begin();
2770 p != vals->end();
2771 ++p)
2773 newvals->push_back(NULL);
2774 newvals->push_back(*p);
2776 delete vals;
2777 vals = newvals;
2779 has_keys = true;
2781 if (val->unknown_expression() != NULL)
2782 val->unknown_expression()->set_is_composite_literal_key();
2784 vals->push_back(val);
2786 if (!token->is_op(OPERATOR_LCURLY))
2787 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2788 else
2790 // This must be a composite literal inside another
2791 // composite literal, with the type omitted for the
2792 // inner one.
2793 val = this->composite_lit(type, depth + 1, token->location());
2796 token = this->peek_token();
2799 vals->push_back(val);
2801 if (!is_name)
2802 all_are_names = false;
2804 if (token->is_op(OPERATOR_COMMA))
2806 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2808 this->advance_token();
2809 break;
2812 else if (token->is_op(OPERATOR_RCURLY))
2814 this->advance_token();
2815 break;
2817 else
2819 if (token->is_op(OPERATOR_SEMICOLON))
2820 error_at(this->location(),
2821 "need trailing comma before newline in composite literal");
2822 else
2823 error_at(this->location(), "expected %<,%> or %<}%>");
2825 this->gogo_->mark_locals_used();
2826 int depth = 0;
2827 while (!token->is_eof()
2828 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2830 if (token->is_op(OPERATOR_LCURLY))
2831 ++depth;
2832 else if (token->is_op(OPERATOR_RCURLY))
2833 --depth;
2834 token = this->advance_token();
2836 if (token->is_op(OPERATOR_RCURLY))
2837 this->advance_token();
2839 return Expression::make_error(location);
2843 return Expression::make_composite_literal(type, depth, has_keys, vals,
2844 all_are_names, location);
2847 // FunctionLit = "func" Signature Block .
2849 Expression*
2850 Parse::function_lit()
2852 Location location = this->location();
2853 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2854 this->advance_token();
2856 Enclosing_vars hold_enclosing_vars;
2857 hold_enclosing_vars.swap(this->enclosing_vars_);
2859 Function_type* type = this->signature(NULL, location);
2860 bool fntype_is_error = false;
2861 if (type == NULL)
2863 type = Type::make_function_type(NULL, NULL, NULL, location);
2864 fntype_is_error = true;
2867 // For a function literal, the next token must be a '{'. If we
2868 // don't see that, then we may have a type expression.
2869 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2870 return Expression::make_type(type, location);
2872 bool hold_is_erroneous_function = this->is_erroneous_function_;
2873 if (fntype_is_error)
2874 this->is_erroneous_function_ = true;
2876 Bc_stack* hold_break_stack = this->break_stack_;
2877 Bc_stack* hold_continue_stack = this->continue_stack_;
2878 this->break_stack_ = NULL;
2879 this->continue_stack_ = NULL;
2881 Named_object* no = this->gogo_->start_function("", type, true, location);
2883 Location end_loc = this->block();
2885 this->gogo_->finish_function(end_loc);
2887 if (this->break_stack_ != NULL)
2888 delete this->break_stack_;
2889 if (this->continue_stack_ != NULL)
2890 delete this->continue_stack_;
2891 this->break_stack_ = hold_break_stack;
2892 this->continue_stack_ = hold_continue_stack;
2894 this->is_erroneous_function_ = hold_is_erroneous_function;
2896 hold_enclosing_vars.swap(this->enclosing_vars_);
2898 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2899 location);
2901 return Expression::make_func_reference(no, closure, location);
2904 // Create a closure for the nested function FUNCTION. This is based
2905 // on ENCLOSING_VARS, which is a list of all variables defined in
2906 // enclosing functions and referenced from FUNCTION. A closure is the
2907 // address of a struct which point to the real function code and
2908 // contains the addresses of all the referenced variables. This
2909 // returns NULL if no closure is required.
2911 Expression*
2912 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2913 Location location)
2915 if (enclosing_vars->empty())
2916 return NULL;
2918 // Get the variables in order by their field index.
2920 size_t enclosing_var_count = enclosing_vars->size();
2921 std::vector<Enclosing_var> ev(enclosing_var_count);
2922 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2923 p != enclosing_vars->end();
2924 ++p)
2926 // Subtract 1 because index 0 is the function code.
2927 ev[p->index() - 1] = *p;
2930 // Build an initializer for a composite literal of the closure's
2931 // type.
2933 Named_object* enclosing_function = this->gogo_->current_function();
2934 Expression_list* initializer = new Expression_list;
2936 initializer->push_back(Expression::make_func_code_reference(function,
2937 location));
2939 for (size_t i = 0; i < enclosing_var_count; ++i)
2941 // Add 1 to i because the first field in the closure is a
2942 // pointer to the function code.
2943 go_assert(ev[i].index() == i + 1);
2944 Named_object* var = ev[i].var();
2945 Expression* ref;
2946 if (ev[i].in_function() == enclosing_function)
2947 ref = Expression::make_var_reference(var, location);
2948 else
2949 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2950 location);
2951 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2952 location);
2953 initializer->push_back(refaddr);
2956 Named_object* closure_var = function->func_value()->closure_var();
2957 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2958 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2959 location);
2960 return Expression::make_heap_expression(cv, location);
2963 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2965 // If MAY_BE_SINK is true, this expression may be "_".
2967 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2968 // literal.
2970 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2971 // guard (var := expr.("type") using the literal keyword "type").
2973 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2974 // if the entire expression is in parentheses.
2976 Expression*
2977 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2978 bool* is_type_switch, bool* is_parenthesized)
2980 Location start_loc = this->location();
2981 bool operand_is_parenthesized = false;
2982 bool whole_is_parenthesized = false;
2984 Expression* ret = this->operand(may_be_sink, &operand_is_parenthesized);
2986 whole_is_parenthesized = operand_is_parenthesized;
2988 // An unknown name followed by a curly brace must be a composite
2989 // literal, and the unknown name must be a type.
2990 if (may_be_composite_lit
2991 && !operand_is_parenthesized
2992 && ret->unknown_expression() != NULL
2993 && this->peek_token()->is_op(OPERATOR_LCURLY))
2995 Named_object* no = ret->unknown_expression()->named_object();
2996 Type* type = Type::make_forward_declaration(no);
2997 ret = Expression::make_type(type, ret->location());
3000 // We handle composite literals and type casts here, as it is the
3001 // easiest way to handle types which are in parentheses, as in
3002 // "((uint))(1)".
3003 if (ret->is_type_expression())
3005 if (this->peek_token()->is_op(OPERATOR_LCURLY))
3007 whole_is_parenthesized = false;
3008 if (!may_be_composite_lit)
3010 Type* t = ret->type();
3011 if (t->named_type() != NULL
3012 || t->forward_declaration_type() != NULL)
3013 error_at(start_loc,
3014 _("parentheses required around this composite literal "
3015 "to avoid parsing ambiguity"));
3017 else if (operand_is_parenthesized)
3018 error_at(start_loc,
3019 "cannot parenthesize type in composite literal");
3020 ret = this->composite_lit(ret->type(), 0, ret->location());
3022 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
3024 whole_is_parenthesized = false;
3025 Location loc = this->location();
3026 this->advance_token();
3027 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
3028 NULL, NULL);
3029 if (this->peek_token()->is_op(OPERATOR_COMMA))
3030 this->advance_token();
3031 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
3033 error_at(this->location(),
3034 "invalid use of %<...%> in type conversion");
3035 this->advance_token();
3037 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3038 error_at(this->location(), "expected %<)%>");
3039 else
3040 this->advance_token();
3041 if (expr->is_error_expression())
3042 ret = expr;
3043 else
3045 Type* t = ret->type();
3046 if (t->classification() == Type::TYPE_ARRAY
3047 && t->array_type()->length() != NULL
3048 && t->array_type()->length()->is_nil_expression())
3050 error_at(ret->location(),
3051 "use of %<[...]%> outside of array literal");
3052 ret = Expression::make_error(loc);
3054 else
3055 ret = Expression::make_cast(t, expr, loc);
3060 while (true)
3062 const Token* token = this->peek_token();
3063 if (token->is_op(OPERATOR_LPAREN))
3065 whole_is_parenthesized = false;
3066 ret = this->call(this->verify_not_sink(ret));
3068 else if (token->is_op(OPERATOR_DOT))
3070 whole_is_parenthesized = false;
3071 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
3072 if (is_type_switch != NULL && *is_type_switch)
3073 break;
3075 else if (token->is_op(OPERATOR_LSQUARE))
3077 whole_is_parenthesized = false;
3078 ret = this->index(this->verify_not_sink(ret));
3080 else
3081 break;
3084 if (whole_is_parenthesized && is_parenthesized != NULL)
3085 *is_parenthesized = true;
3087 return ret;
3090 // Selector = "." identifier .
3091 // TypeGuard = "." "(" QualifiedIdent ")" .
3093 // Note that Operand can expand to QualifiedIdent, which contains a
3094 // ".". That is handled directly in operand when it sees a package
3095 // name.
3097 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3098 // guard (var := expr.("type") using the literal keyword "type").
3100 Expression*
3101 Parse::selector(Expression* left, bool* is_type_switch)
3103 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
3104 Location location = this->location();
3106 const Token* token = this->advance_token();
3107 if (token->is_identifier())
3109 // This could be a field in a struct, or a method in an
3110 // interface, or a method associated with a type. We can't know
3111 // which until we have seen all the types.
3112 std::string name =
3113 this->gogo_->pack_hidden_name(token->identifier(),
3114 token->is_identifier_exported());
3115 if (token->identifier() == "_")
3117 error_at(this->location(), "invalid use of %<_%>");
3118 name = Gogo::erroneous_name();
3120 this->advance_token();
3121 return Expression::make_selector(left, name, location);
3123 else if (token->is_op(OPERATOR_LPAREN))
3125 this->advance_token();
3126 Type* type = NULL;
3127 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
3128 type = this->type();
3129 else
3131 if (is_type_switch != NULL)
3132 *is_type_switch = true;
3133 else
3135 error_at(this->location(),
3136 "use of %<.(type)%> outside type switch");
3137 type = Type::make_error_type();
3139 this->advance_token();
3141 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3142 error_at(this->location(), "missing %<)%>");
3143 else
3144 this->advance_token();
3145 if (is_type_switch != NULL && *is_type_switch)
3146 return left;
3147 return Expression::make_type_guard(left, type, location);
3149 else
3151 error_at(this->location(), "expected identifier or %<(%>");
3152 return left;
3156 // Index = "[" Expression "]" .
3157 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3159 Expression*
3160 Parse::index(Expression* expr)
3162 Location location = this->location();
3163 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
3164 this->advance_token();
3166 Expression* start;
3167 if (!this->peek_token()->is_op(OPERATOR_COLON))
3168 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3169 else
3171 mpz_t zero;
3172 mpz_init_set_ui(zero, 0);
3173 start = Expression::make_integer(&zero, NULL, location);
3174 mpz_clear(zero);
3177 Expression* end = NULL;
3178 if (this->peek_token()->is_op(OPERATOR_COLON))
3180 // We use nil to indicate a missing high expression.
3181 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3182 end = Expression::make_nil(this->location());
3183 else if (this->peek_token()->is_op(OPERATOR_COLON))
3185 error_at(this->location(), "middle index required in 3-index slice");
3186 end = Expression::make_error(this->location());
3188 else
3189 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3192 Expression* cap = NULL;
3193 if (this->peek_token()->is_op(OPERATOR_COLON))
3195 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3197 error_at(this->location(), "final index required in 3-index slice");
3198 cap = Expression::make_error(this->location());
3200 else
3201 cap = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3203 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
3204 error_at(this->location(), "missing %<]%>");
3205 else
3206 this->advance_token();
3207 return Expression::make_index(expr, start, end, cap, location);
3210 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3211 // ArgumentList = ExpressionList [ "..." ] .
3213 Expression*
3214 Parse::call(Expression* func)
3216 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
3217 Expression_list* args = NULL;
3218 bool is_varargs = false;
3219 const Token* token = this->advance_token();
3220 if (!token->is_op(OPERATOR_RPAREN))
3222 args = this->expression_list(NULL, false, true);
3223 token = this->peek_token();
3224 if (token->is_op(OPERATOR_ELLIPSIS))
3226 is_varargs = true;
3227 token = this->advance_token();
3230 if (token->is_op(OPERATOR_COMMA))
3231 token = this->advance_token();
3232 if (!token->is_op(OPERATOR_RPAREN))
3233 error_at(this->location(), "missing %<)%>");
3234 else
3235 this->advance_token();
3236 if (func->is_error_expression())
3237 return func;
3238 return Expression::make_call(func, args, is_varargs, func->location());
3241 // Return an expression for a single unqualified identifier.
3243 Expression*
3244 Parse::id_to_expression(const std::string& name, Location location)
3246 Named_object* in_function;
3247 Named_object* named_object = this->gogo_->lookup(name, &in_function);
3248 if (named_object == NULL)
3249 named_object = this->gogo_->add_unknown_name(name, location);
3251 if (in_function != NULL
3252 && in_function != this->gogo_->current_function()
3253 && (named_object->is_variable() || named_object->is_result_variable()))
3254 return this->enclosing_var_reference(in_function, named_object,
3255 location);
3257 switch (named_object->classification())
3259 case Named_object::NAMED_OBJECT_CONST:
3260 return Expression::make_const_reference(named_object, location);
3261 case Named_object::NAMED_OBJECT_VAR:
3262 case Named_object::NAMED_OBJECT_RESULT_VAR:
3263 this->mark_var_used(named_object);
3264 return Expression::make_var_reference(named_object, location);
3265 case Named_object::NAMED_OBJECT_SINK:
3266 return Expression::make_sink(location);
3267 case Named_object::NAMED_OBJECT_FUNC:
3268 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3269 return Expression::make_func_reference(named_object, NULL, location);
3270 case Named_object::NAMED_OBJECT_UNKNOWN:
3272 Unknown_expression* ue =
3273 Expression::make_unknown_reference(named_object, location);
3274 if (this->is_erroneous_function_)
3275 ue->set_no_error_message();
3276 return ue;
3278 case Named_object::NAMED_OBJECT_PACKAGE:
3279 case Named_object::NAMED_OBJECT_TYPE:
3280 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3282 // These cases can arise for a field name in a composite
3283 // literal.
3284 Unknown_expression* ue =
3285 Expression::make_unknown_reference(named_object, location);
3286 if (this->is_erroneous_function_)
3287 ue->set_no_error_message();
3288 return ue;
3290 case Named_object::NAMED_OBJECT_ERRONEOUS:
3291 return Expression::make_error(location);
3292 default:
3293 error_at(this->location(), "unexpected type of identifier");
3294 return Expression::make_error(location);
3298 // Expression = UnaryExpr { binary_op Expression } .
3300 // PRECEDENCE is the precedence of the current operator.
3302 // If MAY_BE_SINK is true, this expression may be "_".
3304 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3305 // literal.
3307 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3308 // guard (var := expr.("type") using the literal keyword "type").
3310 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3311 // if the entire expression is in parentheses.
3313 Expression*
3314 Parse::expression(Precedence precedence, bool may_be_sink,
3315 bool may_be_composite_lit, bool* is_type_switch,
3316 bool *is_parenthesized)
3318 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3319 is_type_switch, is_parenthesized);
3321 while (true)
3323 if (is_type_switch != NULL && *is_type_switch)
3324 return left;
3326 const Token* token = this->peek_token();
3327 if (token->classification() != Token::TOKEN_OPERATOR)
3329 // Not a binary_op.
3330 return left;
3333 Precedence right_precedence;
3334 switch (token->op())
3336 case OPERATOR_OROR:
3337 right_precedence = PRECEDENCE_OROR;
3338 break;
3339 case OPERATOR_ANDAND:
3340 right_precedence = PRECEDENCE_ANDAND;
3341 break;
3342 case OPERATOR_EQEQ:
3343 case OPERATOR_NOTEQ:
3344 case OPERATOR_LT:
3345 case OPERATOR_LE:
3346 case OPERATOR_GT:
3347 case OPERATOR_GE:
3348 right_precedence = PRECEDENCE_RELOP;
3349 break;
3350 case OPERATOR_PLUS:
3351 case OPERATOR_MINUS:
3352 case OPERATOR_OR:
3353 case OPERATOR_XOR:
3354 right_precedence = PRECEDENCE_ADDOP;
3355 break;
3356 case OPERATOR_MULT:
3357 case OPERATOR_DIV:
3358 case OPERATOR_MOD:
3359 case OPERATOR_LSHIFT:
3360 case OPERATOR_RSHIFT:
3361 case OPERATOR_AND:
3362 case OPERATOR_BITCLEAR:
3363 right_precedence = PRECEDENCE_MULOP;
3364 break;
3365 default:
3366 right_precedence = PRECEDENCE_INVALID;
3367 break;
3370 if (right_precedence == PRECEDENCE_INVALID)
3372 // Not a binary_op.
3373 return left;
3376 if (is_parenthesized != NULL)
3377 *is_parenthesized = false;
3379 Operator op = token->op();
3380 Location binop_location = token->location();
3382 if (precedence >= right_precedence)
3384 // We've already seen A * B, and we see + C. We want to
3385 // return so that A * B becomes a group.
3386 return left;
3389 this->advance_token();
3391 left = this->verify_not_sink(left);
3392 Expression* right = this->expression(right_precedence, false,
3393 may_be_composite_lit,
3394 NULL, NULL);
3395 left = Expression::make_binary(op, left, right, binop_location);
3399 bool
3400 Parse::expression_may_start_here()
3402 const Token* token = this->peek_token();
3403 switch (token->classification())
3405 case Token::TOKEN_INVALID:
3406 case Token::TOKEN_EOF:
3407 return false;
3408 case Token::TOKEN_KEYWORD:
3409 switch (token->keyword())
3411 case KEYWORD_CHAN:
3412 case KEYWORD_FUNC:
3413 case KEYWORD_MAP:
3414 case KEYWORD_STRUCT:
3415 case KEYWORD_INTERFACE:
3416 return true;
3417 default:
3418 return false;
3420 case Token::TOKEN_IDENTIFIER:
3421 return true;
3422 case Token::TOKEN_STRING:
3423 return true;
3424 case Token::TOKEN_OPERATOR:
3425 switch (token->op())
3427 case OPERATOR_PLUS:
3428 case OPERATOR_MINUS:
3429 case OPERATOR_NOT:
3430 case OPERATOR_XOR:
3431 case OPERATOR_MULT:
3432 case OPERATOR_CHANOP:
3433 case OPERATOR_AND:
3434 case OPERATOR_LPAREN:
3435 case OPERATOR_LSQUARE:
3436 return true;
3437 default:
3438 return false;
3440 case Token::TOKEN_CHARACTER:
3441 case Token::TOKEN_INTEGER:
3442 case Token::TOKEN_FLOAT:
3443 case Token::TOKEN_IMAGINARY:
3444 return true;
3445 default:
3446 go_unreachable();
3450 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3452 // If MAY_BE_SINK is true, this expression may be "_".
3454 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3455 // literal.
3457 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3458 // guard (var := expr.("type") using the literal keyword "type").
3460 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3461 // if the entire expression is in parentheses.
3463 Expression*
3464 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3465 bool* is_type_switch, bool* is_parenthesized)
3467 const Token* token = this->peek_token();
3469 // There is a complex parse for <- chan. The choices are
3470 // Convert x to type <- chan int:
3471 // (<- chan int)(x)
3472 // Receive from (x converted to type chan <- chan int):
3473 // (<- chan <- chan int (x))
3474 // Convert x to type <- chan (<- chan int).
3475 // (<- chan <- chan int)(x)
3476 if (token->is_op(OPERATOR_CHANOP))
3478 Location location = token->location();
3479 if (this->advance_token()->is_keyword(KEYWORD_CHAN))
3481 Expression* expr = this->primary_expr(false, may_be_composite_lit,
3482 NULL, NULL);
3483 if (expr->is_error_expression())
3484 return expr;
3485 else if (!expr->is_type_expression())
3486 return Expression::make_receive(expr, location);
3487 else
3489 if (expr->type()->is_error_type())
3490 return expr;
3492 // We picked up "chan TYPE", but it is not a type
3493 // conversion.
3494 Channel_type* ct = expr->type()->channel_type();
3495 if (ct == NULL)
3497 // This is probably impossible.
3498 error_at(location, "expected channel type");
3499 return Expression::make_error(location);
3501 else if (ct->may_receive())
3503 // <- chan TYPE.
3504 Type* t = Type::make_channel_type(false, true,
3505 ct->element_type());
3506 return Expression::make_type(t, location);
3508 else
3510 // <- chan <- TYPE. Because we skipped the leading
3511 // <-, we parsed this as chan <- TYPE. With the
3512 // leading <-, we parse it as <- chan (<- TYPE).
3513 Type *t = this->reassociate_chan_direction(ct, location);
3514 return Expression::make_type(t, location);
3519 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP, location));
3520 token = this->peek_token();
3523 if (token->is_op(OPERATOR_PLUS)
3524 || token->is_op(OPERATOR_MINUS)
3525 || token->is_op(OPERATOR_NOT)
3526 || token->is_op(OPERATOR_XOR)
3527 || token->is_op(OPERATOR_CHANOP)
3528 || token->is_op(OPERATOR_MULT)
3529 || token->is_op(OPERATOR_AND))
3531 Location location = token->location();
3532 Operator op = token->op();
3533 this->advance_token();
3535 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL,
3536 NULL);
3537 if (expr->is_error_expression())
3539 else if (op == OPERATOR_MULT && expr->is_type_expression())
3540 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3541 location);
3542 else if (op == OPERATOR_AND && expr->is_composite_literal())
3543 expr = Expression::make_heap_expression(expr, location);
3544 else if (op != OPERATOR_CHANOP)
3545 expr = Expression::make_unary(op, expr, location);
3546 else
3547 expr = Expression::make_receive(expr, location);
3548 return expr;
3550 else
3551 return this->primary_expr(may_be_sink, may_be_composite_lit,
3552 is_type_switch, is_parenthesized);
3555 // This is called for the obscure case of
3556 // (<- chan <- chan int)(x)
3557 // In unary_expr we remove the leading <- and parse the remainder,
3558 // which gives us
3559 // chan <- (chan int)
3560 // When we add the leading <- back in, we really want
3561 // <- chan (<- chan int)
3562 // This means that we need to reassociate.
3564 Type*
3565 Parse::reassociate_chan_direction(Channel_type *ct, Location location)
3567 Channel_type* ele = ct->element_type()->channel_type();
3568 if (ele == NULL)
3570 error_at(location, "parse error");
3571 return Type::make_error_type();
3573 Type* sub = ele;
3574 if (ele->may_send())
3575 sub = Type::make_channel_type(false, true, ele->element_type());
3576 else
3577 sub = this->reassociate_chan_direction(ele, location);
3578 return Type::make_channel_type(false, true, sub);
3581 // Statement =
3582 // Declaration | LabeledStmt | SimpleStmt |
3583 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3584 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3585 // DeferStmt .
3587 // LABEL is the label of this statement if it has one.
3589 void
3590 Parse::statement(Label* label)
3592 const Token* token = this->peek_token();
3593 switch (token->classification())
3595 case Token::TOKEN_KEYWORD:
3597 switch (token->keyword())
3599 case KEYWORD_CONST:
3600 case KEYWORD_TYPE:
3601 case KEYWORD_VAR:
3602 this->declaration();
3603 break;
3604 case KEYWORD_FUNC:
3605 case KEYWORD_MAP:
3606 case KEYWORD_STRUCT:
3607 case KEYWORD_INTERFACE:
3608 this->simple_stat(true, NULL, NULL, NULL);
3609 break;
3610 case KEYWORD_GO:
3611 case KEYWORD_DEFER:
3612 this->go_or_defer_stat();
3613 break;
3614 case KEYWORD_RETURN:
3615 this->return_stat();
3616 break;
3617 case KEYWORD_BREAK:
3618 this->break_stat();
3619 break;
3620 case KEYWORD_CONTINUE:
3621 this->continue_stat();
3622 break;
3623 case KEYWORD_GOTO:
3624 this->goto_stat();
3625 break;
3626 case KEYWORD_IF:
3627 this->if_stat();
3628 break;
3629 case KEYWORD_SWITCH:
3630 this->switch_stat(label);
3631 break;
3632 case KEYWORD_SELECT:
3633 this->select_stat(label);
3634 break;
3635 case KEYWORD_FOR:
3636 this->for_stat(label);
3637 break;
3638 default:
3639 error_at(this->location(), "expected statement");
3640 this->advance_token();
3641 break;
3644 break;
3646 case Token::TOKEN_IDENTIFIER:
3648 std::string identifier = token->identifier();
3649 bool is_exported = token->is_identifier_exported();
3650 Location location = token->location();
3651 if (this->advance_token()->is_op(OPERATOR_COLON))
3653 this->advance_token();
3654 this->labeled_stmt(identifier, location);
3656 else
3658 this->unget_token(Token::make_identifier_token(identifier,
3659 is_exported,
3660 location));
3661 this->simple_stat(true, NULL, NULL, NULL);
3664 break;
3666 case Token::TOKEN_OPERATOR:
3667 if (token->is_op(OPERATOR_LCURLY))
3669 Location location = token->location();
3670 this->gogo_->start_block(location);
3671 Location end_loc = this->block();
3672 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3673 location);
3675 else if (!token->is_op(OPERATOR_SEMICOLON))
3676 this->simple_stat(true, NULL, NULL, NULL);
3677 break;
3679 case Token::TOKEN_STRING:
3680 case Token::TOKEN_CHARACTER:
3681 case Token::TOKEN_INTEGER:
3682 case Token::TOKEN_FLOAT:
3683 case Token::TOKEN_IMAGINARY:
3684 this->simple_stat(true, NULL, NULL, NULL);
3685 break;
3687 default:
3688 error_at(this->location(), "expected statement");
3689 this->advance_token();
3690 break;
3694 bool
3695 Parse::statement_may_start_here()
3697 const Token* token = this->peek_token();
3698 switch (token->classification())
3700 case Token::TOKEN_KEYWORD:
3702 switch (token->keyword())
3704 case KEYWORD_CONST:
3705 case KEYWORD_TYPE:
3706 case KEYWORD_VAR:
3707 case KEYWORD_FUNC:
3708 case KEYWORD_MAP:
3709 case KEYWORD_STRUCT:
3710 case KEYWORD_INTERFACE:
3711 case KEYWORD_GO:
3712 case KEYWORD_DEFER:
3713 case KEYWORD_RETURN:
3714 case KEYWORD_BREAK:
3715 case KEYWORD_CONTINUE:
3716 case KEYWORD_GOTO:
3717 case KEYWORD_IF:
3718 case KEYWORD_SWITCH:
3719 case KEYWORD_SELECT:
3720 case KEYWORD_FOR:
3721 return true;
3723 default:
3724 return false;
3727 break;
3729 case Token::TOKEN_IDENTIFIER:
3730 return true;
3732 case Token::TOKEN_OPERATOR:
3733 if (token->is_op(OPERATOR_LCURLY)
3734 || token->is_op(OPERATOR_SEMICOLON))
3735 return true;
3736 else
3737 return this->expression_may_start_here();
3739 case Token::TOKEN_STRING:
3740 case Token::TOKEN_CHARACTER:
3741 case Token::TOKEN_INTEGER:
3742 case Token::TOKEN_FLOAT:
3743 case Token::TOKEN_IMAGINARY:
3744 return true;
3746 default:
3747 return false;
3751 // LabeledStmt = Label ":" Statement .
3752 // Label = identifier .
3754 void
3755 Parse::labeled_stmt(const std::string& label_name, Location location)
3757 Label* label = this->gogo_->add_label_definition(label_name, location);
3759 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3761 // This is a label at the end of a block. A program is
3762 // permitted to omit a semicolon here.
3763 return;
3766 if (!this->statement_may_start_here())
3768 // Mark the label as used to avoid a useless error about an
3769 // unused label.
3770 if (label != NULL)
3771 label->set_is_used();
3773 error_at(location, "missing statement after label");
3774 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3775 location));
3776 return;
3779 this->statement(label);
3782 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3783 // Assignment | ShortVarDecl .
3785 // EmptyStmt was handled in Parse::statement.
3787 // In order to make this work for if and switch statements, if
3788 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3789 // expression rather than adding an expression statement to the
3790 // current block. If we see something other than an ExpressionStat,
3791 // we add the statement, set *RETURN_EXP to true if we saw a send
3792 // statement, and return NULL. The handling of send statements is for
3793 // better error messages.
3795 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3796 // RangeClause.
3798 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3799 // guard (var := expr.("type") using the literal keyword "type").
3801 Expression*
3802 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3803 Range_clause* p_range_clause, Type_switch* p_type_switch)
3805 const Token* token = this->peek_token();
3807 // An identifier follow by := is a SimpleVarDecl.
3808 if (token->is_identifier())
3810 std::string identifier = token->identifier();
3811 bool is_exported = token->is_identifier_exported();
3812 Location location = token->location();
3814 token = this->advance_token();
3815 if (token->is_op(OPERATOR_COLONEQ)
3816 || token->is_op(OPERATOR_COMMA))
3818 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3819 this->simple_var_decl_or_assignment(identifier, location,
3820 may_be_composite_lit,
3821 p_range_clause,
3822 (token->is_op(OPERATOR_COLONEQ)
3823 ? p_type_switch
3824 : NULL));
3825 return NULL;
3828 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3829 location));
3832 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3833 may_be_composite_lit,
3834 (p_type_switch == NULL
3835 ? NULL
3836 : &p_type_switch->found),
3837 NULL);
3838 if (p_type_switch != NULL && p_type_switch->found)
3840 p_type_switch->name.clear();
3841 p_type_switch->location = exp->location();
3842 p_type_switch->expr = this->verify_not_sink(exp);
3843 return NULL;
3845 token = this->peek_token();
3846 if (token->is_op(OPERATOR_CHANOP))
3848 this->send_stmt(this->verify_not_sink(exp));
3849 if (return_exp != NULL)
3850 *return_exp = true;
3852 else if (token->is_op(OPERATOR_PLUSPLUS)
3853 || token->is_op(OPERATOR_MINUSMINUS))
3854 this->inc_dec_stat(this->verify_not_sink(exp));
3855 else if (token->is_op(OPERATOR_COMMA)
3856 || token->is_op(OPERATOR_EQ))
3857 this->assignment(exp, may_be_composite_lit, p_range_clause);
3858 else if (token->is_op(OPERATOR_PLUSEQ)
3859 || token->is_op(OPERATOR_MINUSEQ)
3860 || token->is_op(OPERATOR_OREQ)
3861 || token->is_op(OPERATOR_XOREQ)
3862 || token->is_op(OPERATOR_MULTEQ)
3863 || token->is_op(OPERATOR_DIVEQ)
3864 || token->is_op(OPERATOR_MODEQ)
3865 || token->is_op(OPERATOR_LSHIFTEQ)
3866 || token->is_op(OPERATOR_RSHIFTEQ)
3867 || token->is_op(OPERATOR_ANDEQ)
3868 || token->is_op(OPERATOR_BITCLEAREQ))
3869 this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
3870 p_range_clause);
3871 else if (return_exp != NULL)
3872 return this->verify_not_sink(exp);
3873 else
3875 exp = this->verify_not_sink(exp);
3877 if (token->is_op(OPERATOR_COLONEQ))
3879 if (!exp->is_error_expression())
3880 error_at(token->location(), "non-name on left side of %<:=%>");
3881 this->gogo_->mark_locals_used();
3882 while (!token->is_op(OPERATOR_SEMICOLON)
3883 && !token->is_eof())
3884 token = this->advance_token();
3885 return NULL;
3888 this->expression_stat(exp);
3891 return NULL;
3894 bool
3895 Parse::simple_stat_may_start_here()
3897 return this->expression_may_start_here();
3900 // Parse { Statement ";" } which is used in a few places. The list of
3901 // statements may end with a right curly brace, in which case the
3902 // semicolon may be omitted.
3904 void
3905 Parse::statement_list()
3907 while (this->statement_may_start_here())
3909 this->statement(NULL);
3910 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3911 this->advance_token();
3912 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3913 break;
3914 else
3916 if (!this->peek_token()->is_eof() || !saw_errors())
3917 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3918 if (!this->skip_past_error(OPERATOR_RCURLY))
3919 return;
3924 bool
3925 Parse::statement_list_may_start_here()
3927 return this->statement_may_start_here();
3930 // ExpressionStat = Expression .
3932 void
3933 Parse::expression_stat(Expression* exp)
3935 this->gogo_->add_statement(Statement::make_statement(exp, false));
3938 // SendStmt = Channel "&lt;-" Expression .
3939 // Channel = Expression .
3941 void
3942 Parse::send_stmt(Expression* channel)
3944 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3945 Location loc = this->location();
3946 this->advance_token();
3947 Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
3948 NULL);
3949 Statement* s = Statement::make_send_statement(channel, val, loc);
3950 this->gogo_->add_statement(s);
3953 // IncDecStat = Expression ( "++" | "--" ) .
3955 void
3956 Parse::inc_dec_stat(Expression* exp)
3958 const Token* token = this->peek_token();
3960 // Lvalue maps require special handling.
3961 if (exp->index_expression() != NULL)
3962 exp->index_expression()->set_is_lvalue();
3964 if (token->is_op(OPERATOR_PLUSPLUS))
3965 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3966 else if (token->is_op(OPERATOR_MINUSMINUS))
3967 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3968 else
3969 go_unreachable();
3970 this->advance_token();
3973 // Assignment = ExpressionList assign_op ExpressionList .
3975 // EXP is an expression that we have already parsed.
3977 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3978 // side may be a composite literal.
3980 // If RANGE_CLAUSE is not NULL, then this will recognize a
3981 // RangeClause.
3983 void
3984 Parse::assignment(Expression* expr, bool may_be_composite_lit,
3985 Range_clause* p_range_clause)
3987 Expression_list* vars;
3988 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3990 vars = new Expression_list();
3991 vars->push_back(expr);
3993 else
3995 this->advance_token();
3996 vars = this->expression_list(expr, true, may_be_composite_lit);
3999 this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
4002 // An assignment statement. LHS is the list of expressions which
4003 // appear on the left hand side.
4005 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4006 // side may be a composite literal.
4008 // If RANGE_CLAUSE is not NULL, then this will recognize a
4009 // RangeClause.
4011 void
4012 Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
4013 Range_clause* p_range_clause)
4015 const Token* token = this->peek_token();
4016 if (!token->is_op(OPERATOR_EQ)
4017 && !token->is_op(OPERATOR_PLUSEQ)
4018 && !token->is_op(OPERATOR_MINUSEQ)
4019 && !token->is_op(OPERATOR_OREQ)
4020 && !token->is_op(OPERATOR_XOREQ)
4021 && !token->is_op(OPERATOR_MULTEQ)
4022 && !token->is_op(OPERATOR_DIVEQ)
4023 && !token->is_op(OPERATOR_MODEQ)
4024 && !token->is_op(OPERATOR_LSHIFTEQ)
4025 && !token->is_op(OPERATOR_RSHIFTEQ)
4026 && !token->is_op(OPERATOR_ANDEQ)
4027 && !token->is_op(OPERATOR_BITCLEAREQ))
4029 error_at(this->location(), "expected assignment operator");
4030 return;
4032 Operator op = token->op();
4033 Location location = token->location();
4035 token = this->advance_token();
4037 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
4039 if (op != OPERATOR_EQ)
4040 error_at(this->location(), "range clause requires %<=%>");
4041 this->range_clause_expr(lhs, p_range_clause);
4042 return;
4045 Expression_list* vals = this->expression_list(NULL, false,
4046 may_be_composite_lit);
4048 // We've parsed everything; check for errors.
4049 if (lhs == NULL || vals == NULL)
4050 return;
4051 for (Expression_list::const_iterator pe = lhs->begin();
4052 pe != lhs->end();
4053 ++pe)
4055 if ((*pe)->is_error_expression())
4056 return;
4057 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
4058 error_at((*pe)->location(), "cannot use _ as value");
4060 for (Expression_list::const_iterator pe = vals->begin();
4061 pe != vals->end();
4062 ++pe)
4064 if ((*pe)->is_error_expression())
4065 return;
4068 // Map expressions act differently when they are lvalues.
4069 for (Expression_list::iterator plv = lhs->begin();
4070 plv != lhs->end();
4071 ++plv)
4072 if ((*plv)->index_expression() != NULL)
4073 (*plv)->index_expression()->set_is_lvalue();
4075 Call_expression* call;
4076 Index_expression* map_index;
4077 Receive_expression* receive;
4078 Type_guard_expression* type_guard;
4079 if (lhs->size() == vals->size())
4081 Statement* s;
4082 if (lhs->size() > 1)
4084 if (op != OPERATOR_EQ)
4085 error_at(location, "multiple values only permitted with %<=%>");
4086 s = Statement::make_tuple_assignment(lhs, vals, location);
4088 else
4090 if (op == OPERATOR_EQ)
4091 s = Statement::make_assignment(lhs->front(), vals->front(),
4092 location);
4093 else
4094 s = Statement::make_assignment_operation(op, lhs->front(),
4095 vals->front(), location);
4096 delete lhs;
4097 delete vals;
4099 this->gogo_->add_statement(s);
4101 else if (vals->size() == 1
4102 && (call = (*vals->begin())->call_expression()) != NULL)
4104 if (op != OPERATOR_EQ)
4105 error_at(location, "multiple results only permitted with %<=%>");
4106 call->set_expected_result_count(lhs->size());
4107 delete vals;
4108 vals = new Expression_list;
4109 for (unsigned int i = 0; i < lhs->size(); ++i)
4110 vals->push_back(Expression::make_call_result(call, i));
4111 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
4112 this->gogo_->add_statement(s);
4114 else if (lhs->size() == 2
4115 && vals->size() == 1
4116 && (map_index = (*vals->begin())->index_expression()) != NULL)
4118 if (op != OPERATOR_EQ)
4119 error_at(location, "two values from map requires %<=%>");
4120 Expression* val = lhs->front();
4121 Expression* present = lhs->back();
4122 Statement* s = Statement::make_tuple_map_assignment(val, present,
4123 map_index, location);
4124 this->gogo_->add_statement(s);
4126 else if (lhs->size() == 1
4127 && vals->size() == 2
4128 && (map_index = lhs->front()->index_expression()) != NULL)
4130 if (op != OPERATOR_EQ)
4131 error_at(location, "assigning tuple to map index requires %<=%>");
4132 Expression* val = vals->front();
4133 Expression* should_set = vals->back();
4134 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
4135 location);
4136 this->gogo_->add_statement(s);
4138 else if (lhs->size() == 2
4139 && vals->size() == 1
4140 && (receive = (*vals->begin())->receive_expression()) != NULL)
4142 if (op != OPERATOR_EQ)
4143 error_at(location, "two values from receive requires %<=%>");
4144 Expression* val = lhs->front();
4145 Expression* success = lhs->back();
4146 Expression* channel = receive->channel();
4147 Statement* s = Statement::make_tuple_receive_assignment(val, success,
4148 channel,
4149 location);
4150 this->gogo_->add_statement(s);
4152 else if (lhs->size() == 2
4153 && vals->size() == 1
4154 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
4156 if (op != OPERATOR_EQ)
4157 error_at(location, "two values from type guard requires %<=%>");
4158 Expression* val = lhs->front();
4159 Expression* ok = lhs->back();
4160 Expression* expr = type_guard->expr();
4161 Type* type = type_guard->type();
4162 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
4163 expr, type,
4164 location);
4165 this->gogo_->add_statement(s);
4167 else
4169 error_at(location, "number of variables does not match number of values");
4173 // GoStat = "go" Expression .
4174 // DeferStat = "defer" Expression .
4176 void
4177 Parse::go_or_defer_stat()
4179 go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
4180 || this->peek_token()->is_keyword(KEYWORD_DEFER));
4181 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
4182 Location stat_location = this->location();
4184 this->advance_token();
4185 Location expr_location = this->location();
4187 bool is_parenthesized = false;
4188 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
4189 &is_parenthesized);
4190 Call_expression* call_expr = expr->call_expression();
4191 if (is_parenthesized || call_expr == NULL)
4193 error_at(expr_location, "argument to go/defer must be function call");
4194 return;
4197 // Make it easier to simplify go/defer statements by putting every
4198 // statement in its own block.
4199 this->gogo_->start_block(stat_location);
4200 Statement* stat;
4201 if (is_go)
4202 stat = Statement::make_go_statement(call_expr, stat_location);
4203 else
4204 stat = Statement::make_defer_statement(call_expr, stat_location);
4205 this->gogo_->add_statement(stat);
4206 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
4207 stat_location);
4210 // ReturnStat = "return" [ ExpressionList ] .
4212 void
4213 Parse::return_stat()
4215 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
4216 Location location = this->location();
4217 this->advance_token();
4218 Expression_list* vals = NULL;
4219 if (this->expression_may_start_here())
4220 vals = this->expression_list(NULL, false, true);
4221 this->gogo_->add_statement(Statement::make_return_statement(vals, location));
4223 if (vals == NULL
4224 && this->gogo_->current_function()->func_value()->results_are_named())
4226 Named_object* function = this->gogo_->current_function();
4227 Function::Results* results = function->func_value()->result_variables();
4228 for (Function::Results::const_iterator p = results->begin();
4229 p != results->end();
4230 ++p)
4232 Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
4233 if (no == NULL)
4234 go_assert(saw_errors());
4235 else if (!no->is_result_variable())
4236 error_at(location, "%qs is shadowed during return",
4237 (*p)->message_name().c_str());
4242 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4243 // [ "else" ( IfStmt | Block ) ] .
4245 void
4246 Parse::if_stat()
4248 go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
4249 Location location = this->location();
4250 this->advance_token();
4252 this->gogo_->start_block(location);
4254 bool saw_simple_stat = false;
4255 Expression* cond = NULL;
4256 bool saw_send_stmt = false;
4257 if (this->simple_stat_may_start_here())
4259 cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
4260 saw_simple_stat = true;
4262 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4264 // The SimpleStat is an expression statement.
4265 this->expression_stat(cond);
4266 cond = NULL;
4268 if (cond == NULL)
4270 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4271 this->advance_token();
4272 else if (saw_simple_stat)
4274 if (saw_send_stmt)
4275 error_at(this->location(),
4276 ("send statement used as value; "
4277 "use select for non-blocking send"));
4278 else
4279 error_at(this->location(),
4280 "expected %<;%> after statement in if expression");
4281 if (!this->expression_may_start_here())
4282 cond = Expression::make_error(this->location());
4284 if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
4286 error_at(this->location(),
4287 "missing condition in if statement");
4288 cond = Expression::make_error(this->location());
4290 if (cond == NULL)
4291 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL, NULL);
4294 // Check for the easy error of a newline before starting the block.
4295 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4297 Location semi_loc = this->location();
4298 if (this->advance_token()->is_op(OPERATOR_LCURLY))
4299 error_at(semi_loc, "missing %<{%> after if clause");
4300 // Otherwise we will get an error when we call this->block
4301 // below.
4304 this->gogo_->start_block(this->location());
4305 Location end_loc = this->block();
4306 Block* then_block = this->gogo_->finish_block(end_loc);
4308 // Check for the easy error of a newline before "else".
4309 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4311 Location semi_loc = this->location();
4312 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
4313 error_at(this->location(),
4314 "unexpected semicolon or newline before %<else%>");
4315 else
4316 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
4317 semi_loc));
4320 Block* else_block = NULL;
4321 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
4323 this->gogo_->start_block(this->location());
4324 const Token* token = this->advance_token();
4325 if (token->is_keyword(KEYWORD_IF))
4326 this->if_stat();
4327 else if (token->is_op(OPERATOR_LCURLY))
4328 this->block();
4329 else
4331 error_at(this->location(), "expected %<if%> or %<{%>");
4332 this->statement(NULL);
4334 else_block = this->gogo_->finish_block(this->location());
4337 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
4338 else_block,
4339 location));
4341 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4342 location);
4345 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4346 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4347 // "{" { ExprCaseClause } "}" .
4348 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4349 // "{" { TypeCaseClause } "}" .
4350 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4352 void
4353 Parse::switch_stat(Label* label)
4355 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
4356 Location location = this->location();
4357 this->advance_token();
4359 this->gogo_->start_block(location);
4361 bool saw_simple_stat = false;
4362 Expression* switch_val = NULL;
4363 bool saw_send_stmt;
4364 Type_switch type_switch;
4365 bool have_type_switch_block = false;
4366 if (this->simple_stat_may_start_here())
4368 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4369 &type_switch);
4370 saw_simple_stat = true;
4372 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4374 // The SimpleStat is an expression statement.
4375 this->expression_stat(switch_val);
4376 switch_val = NULL;
4378 if (switch_val == NULL && !type_switch.found)
4380 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4381 this->advance_token();
4382 else if (saw_simple_stat)
4384 if (saw_send_stmt)
4385 error_at(this->location(),
4386 ("send statement used as value; "
4387 "use select for non-blocking send"));
4388 else
4389 error_at(this->location(),
4390 "expected %<;%> after statement in switch expression");
4392 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4394 if (this->peek_token()->is_identifier())
4396 const Token* token = this->peek_token();
4397 std::string identifier = token->identifier();
4398 bool is_exported = token->is_identifier_exported();
4399 Location id_loc = token->location();
4401 token = this->advance_token();
4402 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
4403 this->unget_token(Token::make_identifier_token(identifier,
4404 is_exported,
4405 id_loc));
4406 if (is_coloneq)
4408 // This must be a TypeSwitchGuard. It is in a
4409 // different block from any initial SimpleStat.
4410 if (saw_simple_stat)
4412 this->gogo_->start_block(id_loc);
4413 have_type_switch_block = true;
4416 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4417 &type_switch);
4418 if (!type_switch.found)
4420 if (switch_val == NULL
4421 || !switch_val->is_error_expression())
4423 error_at(id_loc, "expected type switch assignment");
4424 switch_val = Expression::make_error(id_loc);
4429 if (switch_val == NULL && !type_switch.found)
4431 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
4432 &type_switch.found, NULL);
4433 if (type_switch.found)
4435 type_switch.name.clear();
4436 type_switch.expr = switch_val;
4437 type_switch.location = switch_val->location();
4443 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4445 Location token_loc = this->location();
4446 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
4447 && this->advance_token()->is_op(OPERATOR_LCURLY))
4448 error_at(token_loc, "missing %<{%> after switch clause");
4449 else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
4451 error_at(token_loc, "invalid variable name");
4452 this->advance_token();
4453 this->expression(PRECEDENCE_NORMAL, false, false,
4454 &type_switch.found, NULL);
4455 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4456 this->advance_token();
4457 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4459 if (have_type_switch_block)
4460 this->gogo_->add_block(this->gogo_->finish_block(location),
4461 location);
4462 this->gogo_->add_block(this->gogo_->finish_block(location),
4463 location);
4464 return;
4466 if (type_switch.found)
4467 type_switch.expr = Expression::make_error(location);
4469 else
4471 error_at(this->location(), "expected %<{%>");
4472 if (have_type_switch_block)
4473 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4474 location);
4475 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4476 location);
4477 return;
4480 this->advance_token();
4482 Statement* statement;
4483 if (type_switch.found)
4484 statement = this->type_switch_body(label, type_switch, location);
4485 else
4486 statement = this->expr_switch_body(label, switch_val, location);
4488 if (statement != NULL)
4489 this->gogo_->add_statement(statement);
4491 if (have_type_switch_block)
4492 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4493 location);
4495 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4496 location);
4499 // The body of an expression switch.
4500 // "{" { ExprCaseClause } "}"
4502 Statement*
4503 Parse::expr_switch_body(Label* label, Expression* switch_val,
4504 Location location)
4506 Switch_statement* statement = Statement::make_switch_statement(switch_val,
4507 location);
4509 this->push_break_statement(statement, label);
4511 Case_clauses* case_clauses = new Case_clauses();
4512 bool saw_default = false;
4513 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4515 if (this->peek_token()->is_eof())
4517 if (!saw_errors())
4518 error_at(this->location(), "missing %<}%>");
4519 return NULL;
4521 this->expr_case_clause(case_clauses, &saw_default);
4523 this->advance_token();
4525 statement->add_clauses(case_clauses);
4527 this->pop_break_statement();
4529 return statement;
4532 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4533 // FallthroughStat = "fallthrough" .
4535 void
4536 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
4538 Location location = this->location();
4540 bool is_default = false;
4541 Expression_list* vals = this->expr_switch_case(&is_default);
4543 if (!this->peek_token()->is_op(OPERATOR_COLON))
4545 if (!saw_errors())
4546 error_at(this->location(), "expected %<:%>");
4547 return;
4549 else
4550 this->advance_token();
4552 Block* statements = NULL;
4553 if (this->statement_list_may_start_here())
4555 this->gogo_->start_block(this->location());
4556 this->statement_list();
4557 statements = this->gogo_->finish_block(this->location());
4560 bool is_fallthrough = false;
4561 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4563 Location fallthrough_loc = this->location();
4564 is_fallthrough = true;
4565 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4566 this->advance_token();
4567 if (this->peek_token()->is_op(OPERATOR_RCURLY))
4568 error_at(fallthrough_loc, _("cannot fallthrough final case in switch"));
4571 if (is_default)
4573 if (*saw_default)
4575 error_at(location, "multiple defaults in switch");
4576 return;
4578 *saw_default = true;
4581 if (is_default || vals != NULL)
4582 clauses->add(vals, is_default, statements, is_fallthrough, location);
4585 // ExprSwitchCase = "case" ExpressionList | "default" .
4587 Expression_list*
4588 Parse::expr_switch_case(bool* is_default)
4590 const Token* token = this->peek_token();
4591 if (token->is_keyword(KEYWORD_CASE))
4593 this->advance_token();
4594 return this->expression_list(NULL, false, true);
4596 else if (token->is_keyword(KEYWORD_DEFAULT))
4598 this->advance_token();
4599 *is_default = true;
4600 return NULL;
4602 else
4604 if (!saw_errors())
4605 error_at(this->location(), "expected %<case%> or %<default%>");
4606 if (!token->is_op(OPERATOR_RCURLY))
4607 this->advance_token();
4608 return NULL;
4612 // The body of a type switch.
4613 // "{" { TypeCaseClause } "}" .
4615 Statement*
4616 Parse::type_switch_body(Label* label, const Type_switch& type_switch,
4617 Location location)
4619 Named_object* switch_no = NULL;
4620 if (!type_switch.name.empty())
4622 if (Gogo::is_sink_name(type_switch.name))
4623 error_at(type_switch.location,
4624 "no new variables on left side of %<:=%>");
4625 else
4627 Variable* switch_var = new Variable(NULL, type_switch.expr, false,
4628 false, false,
4629 type_switch.location);
4630 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
4634 Type_switch_statement* statement =
4635 Statement::make_type_switch_statement(switch_no,
4636 (switch_no == NULL
4637 ? type_switch.expr
4638 : NULL),
4639 location);
4641 this->push_break_statement(statement, label);
4643 Type_case_clauses* case_clauses = new Type_case_clauses();
4644 bool saw_default = false;
4645 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4647 if (this->peek_token()->is_eof())
4649 error_at(this->location(), "missing %<}%>");
4650 return NULL;
4652 this->type_case_clause(switch_no, case_clauses, &saw_default);
4654 this->advance_token();
4656 statement->add_clauses(case_clauses);
4658 this->pop_break_statement();
4660 return statement;
4663 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4665 void
4666 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4667 bool* saw_default)
4669 Location location = this->location();
4671 std::vector<Type*> types;
4672 bool is_default = false;
4673 this->type_switch_case(&types, &is_default);
4675 if (!this->peek_token()->is_op(OPERATOR_COLON))
4676 error_at(this->location(), "expected %<:%>");
4677 else
4678 this->advance_token();
4680 Block* statements = NULL;
4681 if (this->statement_list_may_start_here())
4683 this->gogo_->start_block(this->location());
4684 if (switch_no != NULL && types.size() == 1)
4686 Type* type = types.front();
4687 Expression* init = Expression::make_var_reference(switch_no,
4688 location);
4689 init = Expression::make_type_guard(init, type, location);
4690 Variable* v = new Variable(type, init, false, false, false,
4691 location);
4692 v->set_is_type_switch_var();
4693 Named_object* no = this->gogo_->add_variable(switch_no->name(), v);
4695 // We don't want to issue an error if the compiler
4696 // introduced special variable is not used. Instead we want
4697 // to issue an error if the variable defined by the switch
4698 // is not used. That is handled via type_switch_vars_ and
4699 // Parse::mark_var_used.
4700 v->set_is_used();
4701 this->type_switch_vars_[no] = switch_no;
4703 this->statement_list();
4704 statements = this->gogo_->finish_block(this->location());
4707 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4709 error_at(this->location(),
4710 "fallthrough is not permitted in a type switch");
4711 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4712 this->advance_token();
4715 if (is_default)
4717 go_assert(types.empty());
4718 if (*saw_default)
4720 error_at(location, "multiple defaults in type switch");
4721 return;
4723 *saw_default = true;
4724 clauses->add(NULL, false, true, statements, location);
4726 else if (!types.empty())
4728 for (std::vector<Type*>::const_iterator p = types.begin();
4729 p + 1 != types.end();
4730 ++p)
4731 clauses->add(*p, true, false, NULL, location);
4732 clauses->add(types.back(), false, false, statements, location);
4734 else
4735 clauses->add(Type::make_error_type(), false, false, statements, location);
4738 // TypeSwitchCase = "case" type | "default"
4740 // We accept a comma separated list of types.
4742 void
4743 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4745 const Token* token = this->peek_token();
4746 if (token->is_keyword(KEYWORD_CASE))
4748 this->advance_token();
4749 while (true)
4751 Type* t = this->type();
4753 if (!t->is_error_type())
4754 types->push_back(t);
4755 else
4757 this->gogo_->mark_locals_used();
4758 token = this->peek_token();
4759 while (!token->is_op(OPERATOR_COLON)
4760 && !token->is_op(OPERATOR_COMMA)
4761 && !token->is_op(OPERATOR_RCURLY)
4762 && !token->is_eof())
4763 token = this->advance_token();
4766 if (!this->peek_token()->is_op(OPERATOR_COMMA))
4767 break;
4768 this->advance_token();
4771 else if (token->is_keyword(KEYWORD_DEFAULT))
4773 this->advance_token();
4774 *is_default = true;
4776 else
4778 error_at(this->location(), "expected %<case%> or %<default%>");
4779 if (!token->is_op(OPERATOR_RCURLY))
4780 this->advance_token();
4784 // SelectStat = "select" "{" { CommClause } "}" .
4786 void
4787 Parse::select_stat(Label* label)
4789 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4790 Location location = this->location();
4791 const Token* token = this->advance_token();
4793 if (!token->is_op(OPERATOR_LCURLY))
4795 Location token_loc = token->location();
4796 if (token->is_op(OPERATOR_SEMICOLON)
4797 && this->advance_token()->is_op(OPERATOR_LCURLY))
4798 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4799 else
4801 error_at(this->location(), "expected %<{%>");
4802 return;
4805 this->advance_token();
4807 Select_statement* statement = Statement::make_select_statement(location);
4809 this->push_break_statement(statement, label);
4811 Select_clauses* select_clauses = new Select_clauses();
4812 bool saw_default = false;
4813 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4815 if (this->peek_token()->is_eof())
4817 error_at(this->location(), "expected %<}%>");
4818 return;
4820 this->comm_clause(select_clauses, &saw_default);
4823 this->advance_token();
4825 statement->add_clauses(select_clauses);
4827 this->pop_break_statement();
4829 this->gogo_->add_statement(statement);
4832 // CommClause = CommCase ":" { Statement ";" } .
4834 void
4835 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4837 Location location = this->location();
4838 bool is_send = false;
4839 Expression* channel = NULL;
4840 Expression* val = NULL;
4841 Expression* closed = NULL;
4842 std::string varname;
4843 std::string closedname;
4844 bool is_default = false;
4845 bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
4846 &varname, &closedname, &is_default);
4848 if (!is_send
4849 && varname.empty()
4850 && closedname.empty()
4851 && val != NULL
4852 && val->index_expression() != NULL)
4853 val->index_expression()->set_is_lvalue();
4855 if (this->peek_token()->is_op(OPERATOR_COLON))
4856 this->advance_token();
4857 else
4858 error_at(this->location(), "expected colon");
4860 this->gogo_->start_block(this->location());
4862 Named_object* var = NULL;
4863 if (!varname.empty())
4865 // FIXME: LOCATION is slightly wrong here.
4866 Variable* v = new Variable(NULL, channel, false, false, false,
4867 location);
4868 v->set_type_from_chan_element();
4869 var = this->gogo_->add_variable(varname, v);
4872 Named_object* closedvar = NULL;
4873 if (!closedname.empty())
4875 // FIXME: LOCATION is slightly wrong here.
4876 Variable* v = new Variable(Type::lookup_bool_type(), NULL,
4877 false, false, false, location);
4878 closedvar = this->gogo_->add_variable(closedname, v);
4881 this->statement_list();
4883 Block* statements = this->gogo_->finish_block(this->location());
4885 if (is_default)
4887 if (*saw_default)
4889 error_at(location, "multiple defaults in select");
4890 return;
4892 *saw_default = true;
4895 if (got_case)
4896 clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
4897 statements, location);
4898 else if (statements != NULL)
4900 // Add the statements to make sure that any names they define
4901 // are traversed.
4902 this->gogo_->add_block(statements, location);
4906 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4908 bool
4909 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4910 Expression** closed, std::string* varname,
4911 std::string* closedname, bool* is_default)
4913 const Token* token = this->peek_token();
4914 if (token->is_keyword(KEYWORD_DEFAULT))
4916 this->advance_token();
4917 *is_default = true;
4919 else if (token->is_keyword(KEYWORD_CASE))
4921 this->advance_token();
4922 if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
4923 closedname))
4924 return false;
4926 else
4928 error_at(this->location(), "expected %<case%> or %<default%>");
4929 if (!token->is_op(OPERATOR_RCURLY))
4930 this->advance_token();
4931 return false;
4934 return true;
4937 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4938 // RecvExpr = Expression .
4940 bool
4941 Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
4942 Expression** closed, std::string* varname,
4943 std::string* closedname)
4945 const Token* token = this->peek_token();
4946 bool saw_comma = false;
4947 bool closed_is_id = false;
4948 if (token->is_identifier())
4950 Gogo* gogo = this->gogo_;
4951 std::string recv_var = token->identifier();
4952 bool is_rv_exported = token->is_identifier_exported();
4953 Location recv_var_loc = token->location();
4954 token = this->advance_token();
4955 if (token->is_op(OPERATOR_COLONEQ))
4957 // case rv := <-c:
4958 this->advance_token();
4959 Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
4960 NULL, NULL);
4961 Receive_expression* re = e->receive_expression();
4962 if (re == NULL)
4964 if (!e->is_error_expression())
4965 error_at(this->location(), "expected receive expression");
4966 return false;
4968 if (recv_var == "_")
4970 error_at(recv_var_loc,
4971 "no new variables on left side of %<:=%>");
4972 recv_var = Gogo::erroneous_name();
4974 *is_send = false;
4975 *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
4976 *channel = re->channel();
4977 return true;
4979 else if (token->is_op(OPERATOR_COMMA))
4981 token = this->advance_token();
4982 if (token->is_identifier())
4984 std::string recv_closed = token->identifier();
4985 bool is_rc_exported = token->is_identifier_exported();
4986 Location recv_closed_loc = token->location();
4987 closed_is_id = true;
4989 token = this->advance_token();
4990 if (token->is_op(OPERATOR_COLONEQ))
4992 // case rv, rc := <-c:
4993 this->advance_token();
4994 Expression* e = this->expression(PRECEDENCE_NORMAL, false,
4995 false, NULL, NULL);
4996 Receive_expression* re = e->receive_expression();
4997 if (re == NULL)
4999 if (!e->is_error_expression())
5000 error_at(this->location(),
5001 "expected receive expression");
5002 return false;
5004 if (recv_var == "_" && recv_closed == "_")
5006 error_at(recv_var_loc,
5007 "no new variables on left side of %<:=%>");
5008 recv_var = Gogo::erroneous_name();
5010 *is_send = false;
5011 if (recv_var != "_")
5012 *varname = gogo->pack_hidden_name(recv_var,
5013 is_rv_exported);
5014 if (recv_closed != "_")
5015 *closedname = gogo->pack_hidden_name(recv_closed,
5016 is_rc_exported);
5017 *channel = re->channel();
5018 return true;
5021 this->unget_token(Token::make_identifier_token(recv_closed,
5022 is_rc_exported,
5023 recv_closed_loc));
5026 *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
5027 is_rv_exported),
5028 recv_var_loc);
5029 saw_comma = true;
5031 else
5032 this->unget_token(Token::make_identifier_token(recv_var,
5033 is_rv_exported,
5034 recv_var_loc));
5037 // If SAW_COMMA is false, then we are looking at the start of the
5038 // send or receive expression. If SAW_COMMA is true, then *VAL is
5039 // set and we just read a comma.
5041 Expression* e;
5042 if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
5043 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL);
5044 else
5046 // case <-c:
5047 *is_send = false;
5048 this->advance_token();
5049 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5051 // The next token should be ':'. If it is '<-', then we have
5052 // case <-c <- v:
5053 // which is to say, send on a channel received from a channel.
5054 if (!this->peek_token()->is_op(OPERATOR_CHANOP))
5055 return true;
5057 e = Expression::make_receive(*channel, (*channel)->location());
5060 if (this->peek_token()->is_op(OPERATOR_EQ))
5062 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
5064 error_at(this->location(), "missing %<<-%>");
5065 return false;
5067 *is_send = false;
5068 this->advance_token();
5069 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5070 if (saw_comma)
5072 // case v, e = <-c:
5073 // *VAL is already set.
5074 if (!e->is_sink_expression())
5075 *closed = e;
5077 else
5079 // case v = <-c:
5080 if (!e->is_sink_expression())
5081 *val = e;
5083 return true;
5086 if (saw_comma)
5088 if (closed_is_id)
5089 error_at(this->location(), "expected %<=%> or %<:=%>");
5090 else
5091 error_at(this->location(), "expected %<=%>");
5092 return false;
5095 if (this->peek_token()->is_op(OPERATOR_CHANOP))
5097 // case c <- v:
5098 *is_send = true;
5099 *channel = this->verify_not_sink(e);
5100 this->advance_token();
5101 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5102 return true;
5105 error_at(this->location(), "expected %<<-%> or %<=%>");
5106 return false;
5109 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5110 // Condition = Expression .
5112 void
5113 Parse::for_stat(Label* label)
5115 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
5116 Location location = this->location();
5117 const Token* token = this->advance_token();
5119 // Open a block to hold any variables defined in the init statement
5120 // of the for statement.
5121 this->gogo_->start_block(location);
5123 Block* init = NULL;
5124 Expression* cond = NULL;
5125 Block* post = NULL;
5126 Range_clause range_clause;
5128 if (!token->is_op(OPERATOR_LCURLY))
5130 if (token->is_keyword(KEYWORD_VAR))
5132 error_at(this->location(),
5133 "var declaration not allowed in for initializer");
5134 this->var_decl();
5137 if (token->is_op(OPERATOR_SEMICOLON))
5138 this->for_clause(&cond, &post);
5139 else
5141 // We might be looking at a Condition, an InitStat, or a
5142 // RangeClause.
5143 bool saw_send_stmt;
5144 cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
5145 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5147 if (cond == NULL && !range_clause.found)
5149 if (saw_send_stmt)
5150 error_at(this->location(),
5151 ("send statement used as value; "
5152 "use select for non-blocking send"));
5153 else
5154 error_at(this->location(), "parse error in for statement");
5157 else
5159 if (range_clause.found)
5160 error_at(this->location(), "parse error after range clause");
5162 if (cond != NULL)
5164 // COND is actually an expression statement for
5165 // InitStat at the start of a ForClause.
5166 this->expression_stat(cond);
5167 cond = NULL;
5170 this->for_clause(&cond, &post);
5175 // Check for the easy error of a newline before starting the block.
5176 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5178 Location semi_loc = this->location();
5179 if (this->advance_token()->is_op(OPERATOR_LCURLY))
5180 error_at(semi_loc, "missing %<{%> after for clause");
5181 // Otherwise we will get an error when we call this->block
5182 // below.
5185 // Build the For_statement and note that it is the current target
5186 // for break and continue statements.
5188 For_statement* sfor;
5189 For_range_statement* srange;
5190 Statement* s;
5191 if (!range_clause.found)
5193 sfor = Statement::make_for_statement(init, cond, post, location);
5194 s = sfor;
5195 srange = NULL;
5197 else
5199 srange = Statement::make_for_range_statement(range_clause.index,
5200 range_clause.value,
5201 range_clause.range,
5202 location);
5203 s = srange;
5204 sfor = NULL;
5207 this->push_break_statement(s, label);
5208 this->push_continue_statement(s, label);
5210 // Gather the block of statements in the loop and add them to the
5211 // For_statement.
5213 this->gogo_->start_block(this->location());
5214 Location end_loc = this->block();
5215 Block* statements = this->gogo_->finish_block(end_loc);
5217 if (sfor != NULL)
5218 sfor->add_statements(statements);
5219 else
5220 srange->add_statements(statements);
5222 // This is no longer the break/continue target.
5223 this->pop_break_statement();
5224 this->pop_continue_statement();
5226 // Add the For_statement to the list of statements, and close out
5227 // the block we started to hold any variables defined in the for
5228 // statement.
5230 this->gogo_->add_statement(s);
5232 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
5233 location);
5236 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5237 // InitStat = SimpleStat .
5238 // PostStat = SimpleStat .
5240 // We have already read InitStat at this point.
5242 void
5243 Parse::for_clause(Expression** cond, Block** post)
5245 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
5246 this->advance_token();
5247 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5248 *cond = NULL;
5249 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
5251 error_at(this->location(), "missing %<{%> after for clause");
5252 *cond = NULL;
5253 *post = NULL;
5254 return;
5256 else
5257 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5258 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5259 error_at(this->location(), "expected semicolon");
5260 else
5261 this->advance_token();
5263 if (this->peek_token()->is_op(OPERATOR_LCURLY))
5264 *post = NULL;
5265 else
5267 this->gogo_->start_block(this->location());
5268 this->simple_stat(false, NULL, NULL, NULL);
5269 *post = this->gogo_->finish_block(this->location());
5273 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
5275 // This is the := version. It is called with a list of identifiers.
5277 void
5278 Parse::range_clause_decl(const Typed_identifier_list* til,
5279 Range_clause* p_range_clause)
5281 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5282 Location location = this->location();
5284 p_range_clause->found = true;
5286 go_assert(til->size() >= 1);
5287 if (til->size() > 2)
5288 error_at(this->location(), "too many variables for range clause");
5290 this->advance_token();
5291 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL,
5292 NULL);
5293 p_range_clause->range = expr;
5295 bool any_new = false;
5297 const Typed_identifier* pti = &til->front();
5298 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new,
5299 NULL, NULL);
5300 if (any_new && no->is_variable())
5301 no->var_value()->set_type_from_range_index();
5302 p_range_clause->index = Expression::make_var_reference(no, location);
5304 if (til->size() == 1)
5305 p_range_clause->value = NULL;
5306 else
5308 pti = &til->back();
5309 bool is_new = false;
5310 no = this->init_var(*pti, NULL, expr, true, true, &is_new, NULL, NULL);
5311 if (is_new && no->is_variable())
5312 no->var_value()->set_type_from_range_value();
5313 if (is_new)
5314 any_new = true;
5315 if (!Gogo::is_sink_name(pti->name()))
5316 p_range_clause->value = Expression::make_var_reference(no, location);
5319 if (!any_new)
5320 error_at(location, "variables redeclared but no variable is new");
5323 // The = version of RangeClause. This is called with a list of
5324 // expressions.
5326 void
5327 Parse::range_clause_expr(const Expression_list* vals,
5328 Range_clause* p_range_clause)
5330 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5332 p_range_clause->found = true;
5334 go_assert(vals->size() >= 1);
5335 if (vals->size() > 2)
5336 error_at(this->location(), "too many variables for range clause");
5338 this->advance_token();
5339 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
5340 NULL, NULL);
5342 p_range_clause->index = vals->front();
5343 if (vals->size() == 1)
5344 p_range_clause->value = NULL;
5345 else
5346 p_range_clause->value = vals->back();
5349 // Push a statement on the break stack.
5351 void
5352 Parse::push_break_statement(Statement* enclosing, Label* label)
5354 if (this->break_stack_ == NULL)
5355 this->break_stack_ = new Bc_stack();
5356 this->break_stack_->push_back(std::make_pair(enclosing, label));
5359 // Push a statement on the continue stack.
5361 void
5362 Parse::push_continue_statement(Statement* enclosing, Label* label)
5364 if (this->continue_stack_ == NULL)
5365 this->continue_stack_ = new Bc_stack();
5366 this->continue_stack_->push_back(std::make_pair(enclosing, label));
5369 // Pop the break stack.
5371 void
5372 Parse::pop_break_statement()
5374 this->break_stack_->pop_back();
5377 // Pop the continue stack.
5379 void
5380 Parse::pop_continue_statement()
5382 this->continue_stack_->pop_back();
5385 // Find a break or continue statement given a label name.
5387 Statement*
5388 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
5390 if (bc_stack == NULL)
5391 return NULL;
5392 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
5393 p != bc_stack->rend();
5394 ++p)
5396 if (p->second != NULL && p->second->name() == label)
5398 p->second->set_is_used();
5399 return p->first;
5402 return NULL;
5405 // BreakStat = "break" [ identifier ] .
5407 void
5408 Parse::break_stat()
5410 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
5411 Location location = this->location();
5413 const Token* token = this->advance_token();
5414 Statement* enclosing;
5415 if (!token->is_identifier())
5417 if (this->break_stack_ == NULL || this->break_stack_->empty())
5419 error_at(this->location(),
5420 "break statement not within for or switch or select");
5421 return;
5423 enclosing = this->break_stack_->back().first;
5425 else
5427 enclosing = this->find_bc_statement(this->break_stack_,
5428 token->identifier());
5429 if (enclosing == NULL)
5431 // If there is a label with this name, mark it as used to
5432 // avoid a useless error about an unused label.
5433 this->gogo_->add_label_reference(token->identifier(),
5434 Linemap::unknown_location(), false);
5436 error_at(token->location(), "invalid break label %qs",
5437 Gogo::message_name(token->identifier()).c_str());
5438 this->advance_token();
5439 return;
5441 this->advance_token();
5444 Unnamed_label* label;
5445 if (enclosing->classification() == Statement::STATEMENT_FOR)
5446 label = enclosing->for_statement()->break_label();
5447 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5448 label = enclosing->for_range_statement()->break_label();
5449 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
5450 label = enclosing->switch_statement()->break_label();
5451 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
5452 label = enclosing->type_switch_statement()->break_label();
5453 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
5454 label = enclosing->select_statement()->break_label();
5455 else
5456 go_unreachable();
5458 this->gogo_->add_statement(Statement::make_break_statement(label,
5459 location));
5462 // ContinueStat = "continue" [ identifier ] .
5464 void
5465 Parse::continue_stat()
5467 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
5468 Location location = this->location();
5470 const Token* token = this->advance_token();
5471 Statement* enclosing;
5472 if (!token->is_identifier())
5474 if (this->continue_stack_ == NULL || this->continue_stack_->empty())
5476 error_at(this->location(), "continue statement not within for");
5477 return;
5479 enclosing = this->continue_stack_->back().first;
5481 else
5483 enclosing = this->find_bc_statement(this->continue_stack_,
5484 token->identifier());
5485 if (enclosing == NULL)
5487 // If there is a label with this name, mark it as used to
5488 // avoid a useless error about an unused label.
5489 this->gogo_->add_label_reference(token->identifier(),
5490 Linemap::unknown_location(), false);
5492 error_at(token->location(), "invalid continue label %qs",
5493 Gogo::message_name(token->identifier()).c_str());
5494 this->advance_token();
5495 return;
5497 this->advance_token();
5500 Unnamed_label* label;
5501 if (enclosing->classification() == Statement::STATEMENT_FOR)
5502 label = enclosing->for_statement()->continue_label();
5503 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5504 label = enclosing->for_range_statement()->continue_label();
5505 else
5506 go_unreachable();
5508 this->gogo_->add_statement(Statement::make_continue_statement(label,
5509 location));
5512 // GotoStat = "goto" identifier .
5514 void
5515 Parse::goto_stat()
5517 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
5518 Location location = this->location();
5519 const Token* token = this->advance_token();
5520 if (!token->is_identifier())
5521 error_at(this->location(), "expected label for goto");
5522 else
5524 Label* label = this->gogo_->add_label_reference(token->identifier(),
5525 location, true);
5526 Statement* s = Statement::make_goto_statement(label, location);
5527 this->gogo_->add_statement(s);
5528 this->advance_token();
5532 // PackageClause = "package" PackageName .
5534 void
5535 Parse::package_clause()
5537 const Token* token = this->peek_token();
5538 Location location = token->location();
5539 std::string name;
5540 if (!token->is_keyword(KEYWORD_PACKAGE))
5542 error_at(this->location(), "program must start with package clause");
5543 name = "ERROR";
5545 else
5547 token = this->advance_token();
5548 if (token->is_identifier())
5550 name = token->identifier();
5551 if (name == "_")
5553 error_at(this->location(), "invalid package name _");
5554 name = Gogo::erroneous_name();
5556 this->advance_token();
5558 else
5560 error_at(this->location(), "package name must be an identifier");
5561 name = "ERROR";
5564 this->gogo_->set_package_name(name, location);
5567 // ImportDecl = "import" Decl<ImportSpec> .
5569 void
5570 Parse::import_decl()
5572 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
5573 this->advance_token();
5574 this->decl(&Parse::import_spec, NULL);
5577 // ImportSpec = [ "." | PackageName ] PackageFileName .
5579 void
5580 Parse::import_spec(void*)
5582 const Token* token = this->peek_token();
5583 Location location = token->location();
5585 std::string local_name;
5586 bool is_local_name_exported = false;
5587 if (token->is_op(OPERATOR_DOT))
5589 local_name = ".";
5590 token = this->advance_token();
5592 else if (token->is_identifier())
5594 local_name = token->identifier();
5595 is_local_name_exported = token->is_identifier_exported();
5596 token = this->advance_token();
5599 if (!token->is_string())
5601 error_at(this->location(), "import statement not a string");
5602 this->advance_token();
5603 return;
5606 this->gogo_->import_package(token->string_value(), local_name,
5607 is_local_name_exported, location);
5609 this->advance_token();
5612 // SourceFile = PackageClause ";" { ImportDecl ";" }
5613 // { TopLevelDecl ";" } .
5615 void
5616 Parse::program()
5618 this->package_clause();
5620 const Token* token = this->peek_token();
5621 if (token->is_op(OPERATOR_SEMICOLON))
5622 token = this->advance_token();
5623 else
5624 error_at(this->location(),
5625 "expected %<;%> or newline after package clause");
5627 while (token->is_keyword(KEYWORD_IMPORT))
5629 this->import_decl();
5630 token = this->peek_token();
5631 if (token->is_op(OPERATOR_SEMICOLON))
5632 token = this->advance_token();
5633 else
5634 error_at(this->location(),
5635 "expected %<;%> or newline after import declaration");
5638 while (!token->is_eof())
5640 if (this->declaration_may_start_here())
5641 this->declaration();
5642 else
5644 error_at(this->location(), "expected declaration");
5645 this->gogo_->mark_locals_used();
5647 this->advance_token();
5648 while (!this->peek_token()->is_eof()
5649 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
5650 && !this->peek_token()->is_op(OPERATOR_RCURLY));
5651 if (!this->peek_token()->is_eof()
5652 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
5653 this->advance_token();
5655 token = this->peek_token();
5656 if (token->is_op(OPERATOR_SEMICOLON))
5657 token = this->advance_token();
5658 else if (!token->is_eof() || !saw_errors())
5660 if (token->is_op(OPERATOR_CHANOP))
5661 error_at(this->location(),
5662 ("send statement used as value; "
5663 "use select for non-blocking send"));
5664 else
5665 error_at(this->location(),
5666 "expected %<;%> or newline after top level declaration");
5667 this->skip_past_error(OPERATOR_INVALID);
5672 // Reset the current iota value.
5674 void
5675 Parse::reset_iota()
5677 this->iota_ = 0;
5680 // Return the current iota value.
5683 Parse::iota_value()
5685 return this->iota_;
5688 // Increment the current iota value.
5690 void
5691 Parse::increment_iota()
5693 ++this->iota_;
5696 // Skip forward to a semicolon or OP. OP will normally be
5697 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5698 // past it and return. If we find OP, it will be the next token to
5699 // read. Return true if we are OK, false if we found EOF.
5701 bool
5702 Parse::skip_past_error(Operator op)
5704 this->gogo_->mark_locals_used();
5705 const Token* token = this->peek_token();
5706 while (!token->is_op(op))
5708 if (token->is_eof())
5709 return false;
5710 if (token->is_op(OPERATOR_SEMICOLON))
5712 this->advance_token();
5713 return true;
5715 token = this->advance_token();
5717 return true;
5720 // Check that an expression is not a sink.
5722 Expression*
5723 Parse::verify_not_sink(Expression* expr)
5725 if (expr->is_sink_expression())
5727 error_at(expr->location(), "cannot use _ as value");
5728 expr = Expression::make_error(expr->location());
5730 return expr;
5733 // Mark a variable as used.
5735 void
5736 Parse::mark_var_used(Named_object* no)
5738 if (no->is_variable())
5740 no->var_value()->set_is_used();
5742 // When a type switch uses := to define a variable, then for
5743 // each case with a single type we introduce a new variable with
5744 // the appropriate type. When we do, if the newly introduced
5745 // variable is used, then the type switch variable is used.
5746 Type_switch_vars::iterator p = this->type_switch_vars_.find(no);
5747 if (p != this->type_switch_vars_.end())
5748 p->second->var_value()->set_is_used();