Sync to current external repository.
[official-gcc.git] / gcc / go / gofrontend / parse.cc
blob3d60171dde2772741f1a6313460eca1b0aabde44
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 Named_object* first_var = NULL;
1698 unsigned int index = 0;
1699 bool any_new = false;
1700 Expression_list* ivars = new Expression_list();
1701 Expression_list* ivals = new Expression_list();
1702 for (Typed_identifier_list::const_iterator pv = vars->begin();
1703 pv != vars->end();
1704 ++pv, ++index)
1706 Expression* init = Expression::make_call_result(call, index);
1707 Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
1708 &any_new, ivars, ivals);
1710 if (this->gogo_->in_global_scope() && no->is_variable())
1712 if (first_var == NULL)
1713 first_var = no;
1714 else
1716 // The subsequent vars have an implicit dependency on
1717 // the first one, so that everything gets initialized in
1718 // the right order and so that we detect cycles
1719 // correctly.
1720 this->gogo_->record_var_depends_on(no->var_value(), first_var);
1725 if (is_coloneq && !any_new)
1726 error_at(location, "variables redeclared but no variable is new");
1728 this->finish_init_vars(ivars, ivals, location);
1730 return true;
1733 // See if we need to initialize a pair of values from a map index
1734 // expression. This returns true if we have set up the variables and
1735 // the initialization.
1737 bool
1738 Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
1739 Expression* expr, bool is_coloneq,
1740 Location location)
1742 Index_expression* index = expr->index_expression();
1743 if (index == NULL)
1744 return false;
1745 if (vars->size() != 2)
1746 return false;
1748 // This is an index which is being assigned to two variables. It
1749 // must be a map index. Declare the variables, and then assign the
1750 // results of the map index.
1751 bool any_new = false;
1752 Typed_identifier_list::const_iterator p = vars->begin();
1753 Expression* init = type == NULL ? index : NULL;
1754 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1755 type == NULL, &any_new, NULL, NULL);
1756 if (type == NULL && any_new && val_no->is_variable())
1757 val_no->var_value()->set_type_from_init_tuple();
1758 Expression* val_var = Expression::make_var_reference(val_no, location);
1760 ++p;
1761 Type* var_type = type;
1762 if (var_type == NULL)
1763 var_type = Type::lookup_bool_type();
1764 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1765 &any_new, NULL, NULL);
1766 Expression* present_var = Expression::make_var_reference(no, location);
1768 if (is_coloneq && !any_new)
1769 error_at(location, "variables redeclared but no variable is new");
1771 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var,
1772 index, location);
1774 if (!this->gogo_->in_global_scope())
1775 this->gogo_->add_statement(s);
1776 else if (!val_no->is_sink())
1778 if (val_no->is_variable())
1779 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1781 else if (!no->is_sink())
1783 if (no->is_variable())
1784 no->var_value()->add_preinit_statement(this->gogo_, s);
1786 else
1788 // Execute the map index expression just so that we can fail if
1789 // the map is nil.
1790 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1791 NULL, location);
1792 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1795 return true;
1798 // See if we need to initialize a pair of values from a receive
1799 // expression. This returns true if we have set up the variables and
1800 // the initialization.
1802 bool
1803 Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
1804 Expression* expr, bool is_coloneq,
1805 Location location)
1807 Receive_expression* receive = expr->receive_expression();
1808 if (receive == NULL)
1809 return false;
1810 if (vars->size() != 2)
1811 return false;
1813 // This is a receive expression which is being assigned to two
1814 // variables. Declare the variables, and then assign the results of
1815 // the receive.
1816 bool any_new = false;
1817 Typed_identifier_list::const_iterator p = vars->begin();
1818 Expression* init = type == NULL ? receive : NULL;
1819 Named_object* val_no = this->init_var(*p, type, init, is_coloneq,
1820 type == NULL, &any_new, NULL, NULL);
1821 if (type == NULL && any_new && val_no->is_variable())
1822 val_no->var_value()->set_type_from_init_tuple();
1823 Expression* val_var = Expression::make_var_reference(val_no, location);
1825 ++p;
1826 Type* var_type = type;
1827 if (var_type == NULL)
1828 var_type = Type::lookup_bool_type();
1829 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1830 &any_new, NULL, NULL);
1831 Expression* received_var = Expression::make_var_reference(no, location);
1833 if (is_coloneq && !any_new)
1834 error_at(location, "variables redeclared but no variable is new");
1836 Statement* s = Statement::make_tuple_receive_assignment(val_var,
1837 received_var,
1838 receive->channel(),
1839 location);
1841 if (!this->gogo_->in_global_scope())
1842 this->gogo_->add_statement(s);
1843 else if (!val_no->is_sink())
1845 if (val_no->is_variable())
1846 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1848 else if (!no->is_sink())
1850 if (no->is_variable())
1851 no->var_value()->add_preinit_statement(this->gogo_, s);
1853 else
1855 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(),
1856 NULL, location);
1857 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1860 return true;
1863 // See if we need to initialize a pair of values from a type guard
1864 // expression. This returns true if we have set up the variables and
1865 // the initialization.
1867 bool
1868 Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
1869 Type* type, Expression* expr,
1870 bool is_coloneq, Location location)
1872 Type_guard_expression* type_guard = expr->type_guard_expression();
1873 if (type_guard == NULL)
1874 return false;
1875 if (vars->size() != 2)
1876 return false;
1878 // This is a type guard expression which is being assigned to two
1879 // variables. Declare the variables, and then assign the results of
1880 // the type guard.
1881 bool any_new = false;
1882 Typed_identifier_list::const_iterator p = vars->begin();
1883 Type* var_type = type;
1884 if (var_type == NULL)
1885 var_type = type_guard->type();
1886 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1887 &any_new, NULL, NULL);
1888 Expression* val_var = Expression::make_var_reference(val_no, location);
1890 ++p;
1891 var_type = type;
1892 if (var_type == NULL)
1893 var_type = Type::lookup_bool_type();
1894 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false,
1895 &any_new, NULL, NULL);
1896 Expression* ok_var = Expression::make_var_reference(no, location);
1898 Expression* texpr = type_guard->expr();
1899 Type* t = type_guard->type();
1900 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var,
1901 texpr, t,
1902 location);
1904 if (is_coloneq && !any_new)
1905 error_at(location, "variables redeclared but no variable is new");
1907 if (!this->gogo_->in_global_scope())
1908 this->gogo_->add_statement(s);
1909 else if (!val_no->is_sink())
1911 if (val_no->is_variable())
1912 val_no->var_value()->add_preinit_statement(this->gogo_, s);
1914 else if (!no->is_sink())
1916 if (no->is_variable())
1917 no->var_value()->add_preinit_statement(this->gogo_, s);
1919 else
1921 Named_object* dummy = this->create_dummy_global(type, NULL, location);
1922 dummy->var_value()->add_preinit_statement(this->gogo_, s);
1925 return true;
1928 // Create a single variable. If IS_COLONEQ is true, we permit
1929 // redeclarations in the same block, and we set *IS_NEW when we find a
1930 // new variable which is not a redeclaration.
1932 Named_object*
1933 Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
1934 bool is_coloneq, bool type_from_init, bool* is_new,
1935 Expression_list* vars, Expression_list* vals)
1937 Location location = tid.location();
1939 if (Gogo::is_sink_name(tid.name()))
1941 if (!type_from_init && init != NULL)
1943 if (this->gogo_->in_global_scope())
1944 return this->create_dummy_global(type, init, location);
1945 else
1947 // Create a dummy variable so that we will check whether the
1948 // initializer can be assigned to the type.
1949 Variable* var = new Variable(type, init, false, false, false,
1950 location);
1951 var->set_is_used();
1952 static int count;
1953 char buf[30];
1954 snprintf(buf, sizeof buf, "sink$%d", count);
1955 ++count;
1956 return this->gogo_->add_variable(buf, var);
1959 if (type != NULL)
1960 this->gogo_->add_type_to_verify(type);
1961 return this->gogo_->add_sink();
1964 if (is_coloneq)
1966 Named_object* no = this->gogo_->lookup_in_block(tid.name());
1967 if (no != NULL
1968 && (no->is_variable() || no->is_result_variable()))
1970 // INIT may be NULL even when IS_COLONEQ is true for cases
1971 // like v, ok := x.(int).
1972 if (!type_from_init && init != NULL)
1974 go_assert(vars != NULL && vals != NULL);
1975 vars->push_back(Expression::make_var_reference(no, location));
1976 vals->push_back(init);
1978 return no;
1981 *is_new = true;
1982 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(),
1983 false, false, location);
1984 Named_object* no = this->gogo_->add_variable(tid.name(), var);
1985 if (!no->is_variable())
1987 // The name is already defined, so we just gave an error.
1988 return this->gogo_->add_sink();
1990 return no;
1993 // Create a dummy global variable to force an initializer to be run in
1994 // the right place. This is used when a sink variable is initialized
1995 // at global scope.
1997 Named_object*
1998 Parse::create_dummy_global(Type* type, Expression* init,
1999 Location location)
2001 if (type == NULL && init == NULL)
2002 type = Type::lookup_bool_type();
2003 Variable* var = new Variable(type, init, true, false, false, location);
2004 static int count;
2005 char buf[30];
2006 snprintf(buf, sizeof buf, "_.%d", count);
2007 ++count;
2008 return this->gogo_->add_variable(buf, var);
2011 // Finish the variable initialization by executing any assignments to
2012 // existing variables when using :=. These must be done as a tuple
2013 // assignment in case of something like n, a, b := 1, b, a.
2015 void
2016 Parse::finish_init_vars(Expression_list* vars, Expression_list* vals,
2017 Location location)
2019 if (vars->empty())
2021 delete vars;
2022 delete vals;
2024 else if (vars->size() == 1)
2026 go_assert(!this->gogo_->in_global_scope());
2027 this->gogo_->add_statement(Statement::make_assignment(vars->front(),
2028 vals->front(),
2029 location));
2030 delete vars;
2031 delete vals;
2033 else
2035 go_assert(!this->gogo_->in_global_scope());
2036 this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals,
2037 location));
2041 // SimpleVarDecl = identifier ":=" Expression .
2043 // We've already seen the identifier.
2045 // FIXME: We also have to implement
2046 // IdentifierList ":=" ExpressionList
2047 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2048 // tuple assignments here as well.
2050 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2051 // side may be a composite literal.
2053 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2054 // RangeClause.
2056 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2057 // guard (var := expr.("type") using the literal keyword "type").
2059 void
2060 Parse::simple_var_decl_or_assignment(const std::string& name,
2061 Location location,
2062 bool may_be_composite_lit,
2063 Range_clause* p_range_clause,
2064 Type_switch* p_type_switch)
2066 Typed_identifier_list til;
2067 til.push_back(Typed_identifier(name, NULL, location));
2069 // We've seen one identifier. If we see a comma now, this could be
2070 // "a, *p = 1, 2".
2071 if (this->peek_token()->is_op(OPERATOR_COMMA))
2073 go_assert(p_type_switch == NULL);
2074 while (true)
2076 const Token* token = this->advance_token();
2077 if (!token->is_identifier())
2078 break;
2080 std::string id = token->identifier();
2081 bool is_id_exported = token->is_identifier_exported();
2082 Location id_location = token->location();
2084 token = this->advance_token();
2085 if (!token->is_op(OPERATOR_COMMA))
2087 if (token->is_op(OPERATOR_COLONEQ))
2089 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2090 til.push_back(Typed_identifier(id, NULL, location));
2092 else
2093 this->unget_token(Token::make_identifier_token(id,
2094 is_id_exported,
2095 id_location));
2096 break;
2099 id = this->gogo_->pack_hidden_name(id, is_id_exported);
2100 til.push_back(Typed_identifier(id, NULL, location));
2103 // We have a comma separated list of identifiers in TIL. If the
2104 // next token is COLONEQ, then this is a simple var decl, and we
2105 // have the complete list of identifiers. If the next token is
2106 // not COLONEQ, then the only valid parse is a tuple assignment.
2107 // The list of identifiers we have so far is really a list of
2108 // expressions. There are more expressions following.
2110 if (!this->peek_token()->is_op(OPERATOR_COLONEQ))
2112 Expression_list* exprs = new Expression_list;
2113 for (Typed_identifier_list::const_iterator p = til.begin();
2114 p != til.end();
2115 ++p)
2116 exprs->push_back(this->id_to_expression(p->name(),
2117 p->location()));
2119 Expression_list* more_exprs =
2120 this->expression_list(NULL, true, may_be_composite_lit);
2121 for (Expression_list::const_iterator p = more_exprs->begin();
2122 p != more_exprs->end();
2123 ++p)
2124 exprs->push_back(*p);
2125 delete more_exprs;
2127 this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
2128 return;
2132 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ));
2133 const Token* token = this->advance_token();
2135 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
2137 this->range_clause_decl(&til, p_range_clause);
2138 return;
2141 Expression_list* init;
2142 if (p_type_switch == NULL)
2143 init = this->expression_list(NULL, false, may_be_composite_lit);
2144 else
2146 bool is_type_switch = false;
2147 Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
2148 may_be_composite_lit,
2149 &is_type_switch, NULL);
2150 if (is_type_switch)
2152 p_type_switch->found = true;
2153 p_type_switch->name = name;
2154 p_type_switch->location = location;
2155 p_type_switch->expr = expr;
2156 return;
2159 if (!this->peek_token()->is_op(OPERATOR_COMMA))
2161 init = new Expression_list();
2162 init->push_back(expr);
2164 else
2166 this->advance_token();
2167 init = this->expression_list(expr, false, may_be_composite_lit);
2171 this->init_vars(&til, NULL, init, true, location);
2174 // FunctionDecl = "func" identifier Signature [ Block ] .
2175 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2177 // Deprecated gcc extension:
2178 // FunctionDecl = "func" identifier Signature
2179 // __asm__ "(" string_lit ")" .
2180 // This extension means a function whose real name is the identifier
2181 // inside the asm. This extension will be removed at some future
2182 // date. It has been replaced with //extern comments.
2184 // SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment,
2185 // which means that we omit the method from the type descriptor.
2187 void
2188 Parse::function_decl(bool saw_nointerface)
2190 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2191 Location location = this->location();
2192 std::string extern_name = this->lex_->extern_name();
2193 const Token* token = this->advance_token();
2195 Typed_identifier* rec = NULL;
2196 if (token->is_op(OPERATOR_LPAREN))
2198 rec = this->receiver();
2199 token = this->peek_token();
2201 else if (saw_nointerface)
2203 warning_at(location, 0,
2204 "ignoring magic //go:nointerface comment before non-method");
2205 saw_nointerface = false;
2208 if (!token->is_identifier())
2210 error_at(this->location(), "expected function name");
2211 return;
2214 std::string name =
2215 this->gogo_->pack_hidden_name(token->identifier(),
2216 token->is_identifier_exported());
2218 this->advance_token();
2220 Function_type* fntype = this->signature(rec, this->location());
2222 Named_object* named_object = NULL;
2224 if (this->peek_token()->is_keyword(KEYWORD_ASM))
2226 if (!this->advance_token()->is_op(OPERATOR_LPAREN))
2228 error_at(this->location(), "expected %<(%>");
2229 return;
2231 token = this->advance_token();
2232 if (!token->is_string())
2234 error_at(this->location(), "expected string");
2235 return;
2237 std::string asm_name = token->string_value();
2238 if (!this->advance_token()->is_op(OPERATOR_RPAREN))
2240 error_at(this->location(), "expected %<)%>");
2241 return;
2243 this->advance_token();
2244 if (!Gogo::is_sink_name(name))
2246 named_object = this->gogo_->declare_function(name, fntype, location);
2247 if (named_object->is_function_declaration())
2248 named_object->func_declaration_value()->set_asm_name(asm_name);
2252 // Check for the easy error of a newline before the opening brace.
2253 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
2255 Location semi_loc = this->location();
2256 if (this->advance_token()->is_op(OPERATOR_LCURLY))
2257 error_at(this->location(),
2258 "unexpected semicolon or newline before %<{%>");
2259 else
2260 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
2261 semi_loc));
2264 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2266 if (named_object == NULL && !Gogo::is_sink_name(name))
2268 if (fntype == NULL)
2269 this->gogo_->add_erroneous_name(name);
2270 else
2272 named_object = this->gogo_->declare_function(name, fntype,
2273 location);
2274 if (!extern_name.empty()
2275 && named_object->is_function_declaration())
2277 Function_declaration* fd =
2278 named_object->func_declaration_value();
2279 fd->set_asm_name(extern_name);
2284 if (saw_nointerface)
2285 warning_at(location, 0,
2286 ("ignoring magic //go:nointerface comment "
2287 "before declaration"));
2289 else
2291 bool hold_is_erroneous_function = this->is_erroneous_function_;
2292 if (fntype == NULL)
2294 fntype = Type::make_function_type(NULL, NULL, NULL, location);
2295 this->is_erroneous_function_ = true;
2296 if (!Gogo::is_sink_name(name))
2297 this->gogo_->add_erroneous_name(name);
2298 name = this->gogo_->pack_hidden_name("_", false);
2300 named_object = this->gogo_->start_function(name, fntype, true, location);
2301 Location end_loc = this->block();
2302 this->gogo_->finish_function(end_loc);
2303 if (saw_nointerface
2304 && !this->is_erroneous_function_
2305 && named_object->is_function())
2306 named_object->func_value()->set_nointerface();
2307 this->is_erroneous_function_ = hold_is_erroneous_function;
2311 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2312 // BaseTypeName = identifier .
2314 Typed_identifier*
2315 Parse::receiver()
2317 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
2319 std::string name;
2320 const Token* token = this->advance_token();
2321 Location location = token->location();
2322 if (!token->is_op(OPERATOR_MULT))
2324 if (!token->is_identifier())
2326 error_at(this->location(), "method has no receiver");
2327 this->gogo_->mark_locals_used();
2328 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2329 token = this->advance_token();
2330 if (!token->is_eof())
2331 this->advance_token();
2332 return NULL;
2334 name = token->identifier();
2335 bool is_exported = token->is_identifier_exported();
2336 token = this->advance_token();
2337 if (!token->is_op(OPERATOR_DOT) && !token->is_op(OPERATOR_RPAREN))
2339 // An identifier followed by something other than a dot or a
2340 // right parenthesis must be a receiver name followed by a
2341 // type.
2342 name = this->gogo_->pack_hidden_name(name, is_exported);
2344 else
2346 // This must be a type name.
2347 this->unget_token(Token::make_identifier_token(name, is_exported,
2348 location));
2349 token = this->peek_token();
2350 name.clear();
2354 // Here the receiver name is in NAME (it is empty if the receiver is
2355 // unnamed) and TOKEN is the first token in the type.
2357 bool is_pointer = false;
2358 if (token->is_op(OPERATOR_MULT))
2360 is_pointer = true;
2361 token = this->advance_token();
2364 if (!token->is_identifier())
2366 error_at(this->location(), "expected receiver name or type");
2367 this->gogo_->mark_locals_used();
2368 int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
2369 while (!token->is_eof())
2371 token = this->advance_token();
2372 if (token->is_op(OPERATOR_LPAREN))
2373 ++c;
2374 else if (token->is_op(OPERATOR_RPAREN))
2376 if (c == 0)
2377 break;
2378 --c;
2381 if (!token->is_eof())
2382 this->advance_token();
2383 return NULL;
2386 Type* type = this->type_name(true);
2388 if (is_pointer && !type->is_error_type())
2389 type = Type::make_pointer_type(type);
2391 if (this->peek_token()->is_op(OPERATOR_RPAREN))
2392 this->advance_token();
2393 else
2395 if (this->peek_token()->is_op(OPERATOR_COMMA))
2396 error_at(this->location(), "method has multiple receivers");
2397 else
2398 error_at(this->location(), "expected %<)%>");
2399 this->gogo_->mark_locals_used();
2400 while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
2401 token = this->advance_token();
2402 if (!token->is_eof())
2403 this->advance_token();
2404 return NULL;
2407 return new Typed_identifier(name, type, location);
2410 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2411 // Literal = BasicLit | CompositeLit | FunctionLit .
2412 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2414 // If MAY_BE_SINK is true, this operand may be "_".
2416 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2417 // if the entire expression is in parentheses.
2419 Expression*
2420 Parse::operand(bool may_be_sink, bool* is_parenthesized)
2422 const Token* token = this->peek_token();
2423 Expression* ret;
2424 switch (token->classification())
2426 case Token::TOKEN_IDENTIFIER:
2428 Location location = token->location();
2429 std::string id = token->identifier();
2430 bool is_exported = token->is_identifier_exported();
2431 std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
2433 Named_object* in_function;
2434 Named_object* named_object = this->gogo_->lookup(packed, &in_function);
2436 Package* package = NULL;
2437 if (named_object != NULL && named_object->is_package())
2439 if (!this->advance_token()->is_op(OPERATOR_DOT)
2440 || !this->advance_token()->is_identifier())
2442 error_at(location, "unexpected reference to package");
2443 return Expression::make_error(location);
2445 package = named_object->package_value();
2446 package->set_used();
2447 id = this->peek_token()->identifier();
2448 is_exported = this->peek_token()->is_identifier_exported();
2449 packed = this->gogo_->pack_hidden_name(id, is_exported);
2450 named_object = package->lookup(packed);
2451 location = this->location();
2452 go_assert(in_function == NULL);
2455 this->advance_token();
2457 if (named_object != NULL
2458 && named_object->is_type()
2459 && !named_object->type_value()->is_visible())
2461 go_assert(package != NULL);
2462 error_at(location, "invalid reference to hidden type %<%s.%s%>",
2463 Gogo::message_name(package->package_name()).c_str(),
2464 Gogo::message_name(id).c_str());
2465 return Expression::make_error(location);
2469 if (named_object == NULL)
2471 if (package != NULL)
2473 std::string n1 = Gogo::message_name(package->package_name());
2474 std::string n2 = Gogo::message_name(id);
2475 if (!is_exported)
2476 error_at(location,
2477 ("invalid reference to unexported identifier "
2478 "%<%s.%s%>"),
2479 n1.c_str(), n2.c_str());
2480 else
2481 error_at(location,
2482 "reference to undefined identifier %<%s.%s%>",
2483 n1.c_str(), n2.c_str());
2484 return Expression::make_error(location);
2487 named_object = this->gogo_->add_unknown_name(packed, location);
2490 if (in_function != NULL
2491 && in_function != this->gogo_->current_function()
2492 && (named_object->is_variable()
2493 || named_object->is_result_variable()))
2494 return this->enclosing_var_reference(in_function, named_object,
2495 location);
2497 switch (named_object->classification())
2499 case Named_object::NAMED_OBJECT_CONST:
2500 return Expression::make_const_reference(named_object, location);
2501 case Named_object::NAMED_OBJECT_TYPE:
2502 return Expression::make_type(named_object->type_value(), location);
2503 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
2505 Type* t = Type::make_forward_declaration(named_object);
2506 return Expression::make_type(t, location);
2508 case Named_object::NAMED_OBJECT_VAR:
2509 case Named_object::NAMED_OBJECT_RESULT_VAR:
2510 this->mark_var_used(named_object);
2511 return Expression::make_var_reference(named_object, location);
2512 case Named_object::NAMED_OBJECT_SINK:
2513 if (may_be_sink)
2514 return Expression::make_sink(location);
2515 else
2517 error_at(location, "cannot use _ as value");
2518 return Expression::make_error(location);
2520 case Named_object::NAMED_OBJECT_FUNC:
2521 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
2522 return Expression::make_func_reference(named_object, NULL,
2523 location);
2524 case Named_object::NAMED_OBJECT_UNKNOWN:
2526 Unknown_expression* ue =
2527 Expression::make_unknown_reference(named_object, location);
2528 if (this->is_erroneous_function_)
2529 ue->set_no_error_message();
2530 return ue;
2532 case Named_object::NAMED_OBJECT_ERRONEOUS:
2533 return Expression::make_error(location);
2534 default:
2535 go_unreachable();
2538 go_unreachable();
2540 case Token::TOKEN_STRING:
2541 ret = Expression::make_string(token->string_value(), token->location());
2542 this->advance_token();
2543 return ret;
2545 case Token::TOKEN_CHARACTER:
2546 ret = Expression::make_character(token->character_value(), NULL,
2547 token->location());
2548 this->advance_token();
2549 return ret;
2551 case Token::TOKEN_INTEGER:
2552 ret = Expression::make_integer(token->integer_value(), NULL,
2553 token->location());
2554 this->advance_token();
2555 return ret;
2557 case Token::TOKEN_FLOAT:
2558 ret = Expression::make_float(token->float_value(), NULL,
2559 token->location());
2560 this->advance_token();
2561 return ret;
2563 case Token::TOKEN_IMAGINARY:
2565 mpfr_t zero;
2566 mpfr_init_set_ui(zero, 0, GMP_RNDN);
2567 ret = Expression::make_complex(&zero, token->imaginary_value(),
2568 NULL, token->location());
2569 mpfr_clear(zero);
2570 this->advance_token();
2571 return ret;
2574 case Token::TOKEN_KEYWORD:
2575 switch (token->keyword())
2577 case KEYWORD_FUNC:
2578 return this->function_lit();
2579 case KEYWORD_CHAN:
2580 case KEYWORD_INTERFACE:
2581 case KEYWORD_MAP:
2582 case KEYWORD_STRUCT:
2584 Location location = token->location();
2585 return Expression::make_type(this->type(), location);
2587 default:
2588 break;
2590 break;
2592 case Token::TOKEN_OPERATOR:
2593 if (token->is_op(OPERATOR_LPAREN))
2595 this->advance_token();
2596 ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL,
2597 NULL);
2598 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
2599 error_at(this->location(), "missing %<)%>");
2600 else
2601 this->advance_token();
2602 if (is_parenthesized != NULL)
2603 *is_parenthesized = true;
2604 return ret;
2606 else if (token->is_op(OPERATOR_LSQUARE))
2608 // Here we call array_type directly, as this is the only
2609 // case where an ellipsis is permitted for an array type.
2610 Location location = token->location();
2611 return Expression::make_type(this->array_type(true), location);
2613 break;
2615 default:
2616 break;
2619 error_at(this->location(), "expected operand");
2620 return Expression::make_error(this->location());
2623 // Handle a reference to a variable in an enclosing function. We add
2624 // it to a list of such variables. We return a reference to a field
2625 // in a struct which will be passed on the static chain when calling
2626 // the current function.
2628 Expression*
2629 Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
2630 Location location)
2632 go_assert(var->is_variable() || var->is_result_variable());
2634 this->mark_var_used(var);
2636 Named_object* this_function = this->gogo_->current_function();
2637 Named_object* closure = this_function->func_value()->closure_var();
2639 // The last argument to the Enclosing_var constructor is the index
2640 // of this variable in the closure. We add 1 to the current number
2641 // of enclosed variables, because the first field in the closure
2642 // points to the function code.
2643 Enclosing_var ev(var, in_function, this->enclosing_vars_.size() + 1);
2644 std::pair<Enclosing_vars::iterator, bool> ins =
2645 this->enclosing_vars_.insert(ev);
2646 if (ins.second)
2648 // This is a variable we have not seen before. Add a new field
2649 // to the closure type.
2650 this_function->func_value()->add_closure_field(var, location);
2653 Expression* closure_ref = Expression::make_var_reference(closure,
2654 location);
2655 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location);
2657 // The closure structure holds pointers to the variables, so we need
2658 // to introduce an indirection.
2659 Expression* e = Expression::make_field_reference(closure_ref,
2660 ins.first->index(),
2661 location);
2662 e = Expression::make_unary(OPERATOR_MULT, e, location);
2663 return e;
2666 // CompositeLit = LiteralType LiteralValue .
2667 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2668 // SliceType | MapType | TypeName .
2669 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2670 // ElementList = Element { "," Element } .
2671 // Element = [ Key ":" ] Value .
2672 // Key = FieldName | ElementIndex .
2673 // FieldName = identifier .
2674 // ElementIndex = Expression .
2675 // Value = Expression | LiteralValue .
2677 // We have already seen the type if there is one, and we are now
2678 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2679 // will be seen here as an array type whose length is "nil". The
2680 // DEPTH parameter is non-zero if this is an embedded composite
2681 // literal and the type was omitted. It gives the number of steps up
2682 // to the type which was provided. E.g., in [][]int{{1}} it will be
2683 // 1. In [][][]int{{{1}}} it will be 2.
2685 Expression*
2686 Parse::composite_lit(Type* type, int depth, Location location)
2688 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
2689 this->advance_token();
2691 if (this->peek_token()->is_op(OPERATOR_RCURLY))
2693 this->advance_token();
2694 return Expression::make_composite_literal(type, depth, false, NULL,
2695 false, location);
2698 bool has_keys = false;
2699 bool all_are_names = true;
2700 Expression_list* vals = new Expression_list;
2701 while (true)
2703 Expression* val;
2704 bool is_type_omitted = false;
2705 bool is_name = false;
2707 const Token* token = this->peek_token();
2709 if (token->is_identifier())
2711 std::string identifier = token->identifier();
2712 bool is_exported = token->is_identifier_exported();
2713 Location location = token->location();
2715 if (this->advance_token()->is_op(OPERATOR_COLON))
2717 // This may be a field name. We don't know for sure--it
2718 // could also be an expression for an array index. We
2719 // don't want to parse it as an expression because may
2720 // trigger various errors, e.g., if this identifier
2721 // happens to be the name of a package.
2722 Gogo* gogo = this->gogo_;
2723 val = this->id_to_expression(gogo->pack_hidden_name(identifier,
2724 is_exported),
2725 location);
2726 is_name = true;
2728 else
2730 this->unget_token(Token::make_identifier_token(identifier,
2731 is_exported,
2732 location));
2733 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
2734 NULL);
2737 else if (!token->is_op(OPERATOR_LCURLY))
2738 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2739 else
2741 // This must be a composite literal inside another composite
2742 // literal, with the type omitted for the inner one.
2743 val = this->composite_lit(type, depth + 1, token->location());
2744 is_type_omitted = true;
2747 token = this->peek_token();
2748 if (!token->is_op(OPERATOR_COLON))
2750 if (has_keys)
2751 vals->push_back(NULL);
2752 is_name = false;
2754 else
2756 if (is_type_omitted && !val->is_error_expression())
2758 error_at(this->location(), "unexpected %<:%>");
2759 val = Expression::make_error(this->location());
2762 this->advance_token();
2764 if (!has_keys && !vals->empty())
2766 Expression_list* newvals = new Expression_list;
2767 for (Expression_list::const_iterator p = vals->begin();
2768 p != vals->end();
2769 ++p)
2771 newvals->push_back(NULL);
2772 newvals->push_back(*p);
2774 delete vals;
2775 vals = newvals;
2777 has_keys = true;
2779 if (val->unknown_expression() != NULL)
2780 val->unknown_expression()->set_is_composite_literal_key();
2782 vals->push_back(val);
2784 if (!token->is_op(OPERATOR_LCURLY))
2785 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
2786 else
2788 // This must be a composite literal inside another
2789 // composite literal, with the type omitted for the
2790 // inner one.
2791 val = this->composite_lit(type, depth + 1, token->location());
2794 token = this->peek_token();
2797 vals->push_back(val);
2799 if (!is_name)
2800 all_are_names = false;
2802 if (token->is_op(OPERATOR_COMMA))
2804 if (this->advance_token()->is_op(OPERATOR_RCURLY))
2806 this->advance_token();
2807 break;
2810 else if (token->is_op(OPERATOR_RCURLY))
2812 this->advance_token();
2813 break;
2815 else
2817 if (token->is_op(OPERATOR_SEMICOLON))
2818 error_at(this->location(),
2819 "need trailing comma before newline in composite literal");
2820 else
2821 error_at(this->location(), "expected %<,%> or %<}%>");
2823 this->gogo_->mark_locals_used();
2824 int depth = 0;
2825 while (!token->is_eof()
2826 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
2828 if (token->is_op(OPERATOR_LCURLY))
2829 ++depth;
2830 else if (token->is_op(OPERATOR_RCURLY))
2831 --depth;
2832 token = this->advance_token();
2834 if (token->is_op(OPERATOR_RCURLY))
2835 this->advance_token();
2837 return Expression::make_error(location);
2841 return Expression::make_composite_literal(type, depth, has_keys, vals,
2842 all_are_names, location);
2845 // FunctionLit = "func" Signature Block .
2847 Expression*
2848 Parse::function_lit()
2850 Location location = this->location();
2851 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
2852 this->advance_token();
2854 Enclosing_vars hold_enclosing_vars;
2855 hold_enclosing_vars.swap(this->enclosing_vars_);
2857 Function_type* type = this->signature(NULL, location);
2858 bool fntype_is_error = false;
2859 if (type == NULL)
2861 type = Type::make_function_type(NULL, NULL, NULL, location);
2862 fntype_is_error = true;
2865 // For a function literal, the next token must be a '{'. If we
2866 // don't see that, then we may have a type expression.
2867 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
2868 return Expression::make_type(type, location);
2870 bool hold_is_erroneous_function = this->is_erroneous_function_;
2871 if (fntype_is_error)
2872 this->is_erroneous_function_ = true;
2874 Bc_stack* hold_break_stack = this->break_stack_;
2875 Bc_stack* hold_continue_stack = this->continue_stack_;
2876 this->break_stack_ = NULL;
2877 this->continue_stack_ = NULL;
2879 Named_object* no = this->gogo_->start_function("", type, true, location);
2881 Location end_loc = this->block();
2883 this->gogo_->finish_function(end_loc);
2885 if (this->break_stack_ != NULL)
2886 delete this->break_stack_;
2887 if (this->continue_stack_ != NULL)
2888 delete this->continue_stack_;
2889 this->break_stack_ = hold_break_stack;
2890 this->continue_stack_ = hold_continue_stack;
2892 this->is_erroneous_function_ = hold_is_erroneous_function;
2894 hold_enclosing_vars.swap(this->enclosing_vars_);
2896 Expression* closure = this->create_closure(no, &hold_enclosing_vars,
2897 location);
2899 return Expression::make_func_reference(no, closure, location);
2902 // Create a closure for the nested function FUNCTION. This is based
2903 // on ENCLOSING_VARS, which is a list of all variables defined in
2904 // enclosing functions and referenced from FUNCTION. A closure is the
2905 // address of a struct which point to the real function code and
2906 // contains the addresses of all the referenced variables. This
2907 // returns NULL if no closure is required.
2909 Expression*
2910 Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
2911 Location location)
2913 if (enclosing_vars->empty())
2914 return NULL;
2916 // Get the variables in order by their field index.
2918 size_t enclosing_var_count = enclosing_vars->size();
2919 std::vector<Enclosing_var> ev(enclosing_var_count);
2920 for (Enclosing_vars::const_iterator p = enclosing_vars->begin();
2921 p != enclosing_vars->end();
2922 ++p)
2924 // Subtract 1 because index 0 is the function code.
2925 ev[p->index() - 1] = *p;
2928 // Build an initializer for a composite literal of the closure's
2929 // type.
2931 Named_object* enclosing_function = this->gogo_->current_function();
2932 Expression_list* initializer = new Expression_list;
2934 initializer->push_back(Expression::make_func_code_reference(function,
2935 location));
2937 for (size_t i = 0; i < enclosing_var_count; ++i)
2939 // Add 1 to i because the first field in the closure is a
2940 // pointer to the function code.
2941 go_assert(ev[i].index() == i + 1);
2942 Named_object* var = ev[i].var();
2943 Expression* ref;
2944 if (ev[i].in_function() == enclosing_function)
2945 ref = Expression::make_var_reference(var, location);
2946 else
2947 ref = this->enclosing_var_reference(ev[i].in_function(), var,
2948 location);
2949 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref,
2950 location);
2951 initializer->push_back(refaddr);
2954 Named_object* closure_var = function->func_value()->closure_var();
2955 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type();
2956 Expression* cv = Expression::make_struct_composite_literal(st, initializer,
2957 location);
2958 return Expression::make_heap_expression(cv, location);
2961 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2963 // If MAY_BE_SINK is true, this expression may be "_".
2965 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2966 // literal.
2968 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2969 // guard (var := expr.("type") using the literal keyword "type").
2971 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2972 // if the entire expression is in parentheses.
2974 Expression*
2975 Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
2976 bool* is_type_switch, bool* is_parenthesized)
2978 Location start_loc = this->location();
2979 bool operand_is_parenthesized = false;
2980 bool whole_is_parenthesized = false;
2982 Expression* ret = this->operand(may_be_sink, &operand_is_parenthesized);
2984 whole_is_parenthesized = operand_is_parenthesized;
2986 // An unknown name followed by a curly brace must be a composite
2987 // literal, and the unknown name must be a type.
2988 if (may_be_composite_lit
2989 && !operand_is_parenthesized
2990 && ret->unknown_expression() != NULL
2991 && this->peek_token()->is_op(OPERATOR_LCURLY))
2993 Named_object* no = ret->unknown_expression()->named_object();
2994 Type* type = Type::make_forward_declaration(no);
2995 ret = Expression::make_type(type, ret->location());
2998 // We handle composite literals and type casts here, as it is the
2999 // easiest way to handle types which are in parentheses, as in
3000 // "((uint))(1)".
3001 if (ret->is_type_expression())
3003 if (this->peek_token()->is_op(OPERATOR_LCURLY))
3005 whole_is_parenthesized = false;
3006 if (!may_be_composite_lit)
3008 Type* t = ret->type();
3009 if (t->named_type() != NULL
3010 || t->forward_declaration_type() != NULL)
3011 error_at(start_loc,
3012 _("parentheses required around this composite literal "
3013 "to avoid parsing ambiguity"));
3015 else if (operand_is_parenthesized)
3016 error_at(start_loc,
3017 "cannot parenthesize type in composite literal");
3018 ret = this->composite_lit(ret->type(), 0, ret->location());
3020 else if (this->peek_token()->is_op(OPERATOR_LPAREN))
3022 whole_is_parenthesized = false;
3023 Location loc = this->location();
3024 this->advance_token();
3025 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
3026 NULL, NULL);
3027 if (this->peek_token()->is_op(OPERATOR_COMMA))
3028 this->advance_token();
3029 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS))
3031 error_at(this->location(),
3032 "invalid use of %<...%> in type conversion");
3033 this->advance_token();
3035 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3036 error_at(this->location(), "expected %<)%>");
3037 else
3038 this->advance_token();
3039 if (expr->is_error_expression())
3040 ret = expr;
3041 else
3043 Type* t = ret->type();
3044 if (t->classification() == Type::TYPE_ARRAY
3045 && t->array_type()->length() != NULL
3046 && t->array_type()->length()->is_nil_expression())
3048 error_at(ret->location(),
3049 "use of %<[...]%> outside of array literal");
3050 ret = Expression::make_error(loc);
3052 else
3053 ret = Expression::make_cast(t, expr, loc);
3058 while (true)
3060 const Token* token = this->peek_token();
3061 if (token->is_op(OPERATOR_LPAREN))
3063 whole_is_parenthesized = false;
3064 ret = this->call(this->verify_not_sink(ret));
3066 else if (token->is_op(OPERATOR_DOT))
3068 whole_is_parenthesized = false;
3069 ret = this->selector(this->verify_not_sink(ret), is_type_switch);
3070 if (is_type_switch != NULL && *is_type_switch)
3071 break;
3073 else if (token->is_op(OPERATOR_LSQUARE))
3075 whole_is_parenthesized = false;
3076 ret = this->index(this->verify_not_sink(ret));
3078 else
3079 break;
3082 if (whole_is_parenthesized && is_parenthesized != NULL)
3083 *is_parenthesized = true;
3085 return ret;
3088 // Selector = "." identifier .
3089 // TypeGuard = "." "(" QualifiedIdent ")" .
3091 // Note that Operand can expand to QualifiedIdent, which contains a
3092 // ".". That is handled directly in operand when it sees a package
3093 // name.
3095 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3096 // guard (var := expr.("type") using the literal keyword "type").
3098 Expression*
3099 Parse::selector(Expression* left, bool* is_type_switch)
3101 go_assert(this->peek_token()->is_op(OPERATOR_DOT));
3102 Location location = this->location();
3104 const Token* token = this->advance_token();
3105 if (token->is_identifier())
3107 // This could be a field in a struct, or a method in an
3108 // interface, or a method associated with a type. We can't know
3109 // which until we have seen all the types.
3110 std::string name =
3111 this->gogo_->pack_hidden_name(token->identifier(),
3112 token->is_identifier_exported());
3113 if (token->identifier() == "_")
3115 error_at(this->location(), "invalid use of %<_%>");
3116 name = Gogo::erroneous_name();
3118 this->advance_token();
3119 return Expression::make_selector(left, name, location);
3121 else if (token->is_op(OPERATOR_LPAREN))
3123 this->advance_token();
3124 Type* type = NULL;
3125 if (!this->peek_token()->is_keyword(KEYWORD_TYPE))
3126 type = this->type();
3127 else
3129 if (is_type_switch != NULL)
3130 *is_type_switch = true;
3131 else
3133 error_at(this->location(),
3134 "use of %<.(type)%> outside type switch");
3135 type = Type::make_error_type();
3137 this->advance_token();
3139 if (!this->peek_token()->is_op(OPERATOR_RPAREN))
3140 error_at(this->location(), "missing %<)%>");
3141 else
3142 this->advance_token();
3143 if (is_type_switch != NULL && *is_type_switch)
3144 return left;
3145 return Expression::make_type_guard(left, type, location);
3147 else
3149 error_at(this->location(), "expected identifier or %<(%>");
3150 return left;
3154 // Index = "[" Expression "]" .
3155 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3157 Expression*
3158 Parse::index(Expression* expr)
3160 Location location = this->location();
3161 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
3162 this->advance_token();
3164 Expression* start;
3165 if (!this->peek_token()->is_op(OPERATOR_COLON))
3166 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3167 else
3169 mpz_t zero;
3170 mpz_init_set_ui(zero, 0);
3171 start = Expression::make_integer(&zero, NULL, location);
3172 mpz_clear(zero);
3175 Expression* end = NULL;
3176 if (this->peek_token()->is_op(OPERATOR_COLON))
3178 // We use nil to indicate a missing high expression.
3179 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3180 end = Expression::make_nil(this->location());
3181 else if (this->peek_token()->is_op(OPERATOR_COLON))
3183 error_at(this->location(), "middle index required in 3-index slice");
3184 end = Expression::make_error(this->location());
3186 else
3187 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3190 Expression* cap = NULL;
3191 if (this->peek_token()->is_op(OPERATOR_COLON))
3193 if (this->advance_token()->is_op(OPERATOR_RSQUARE))
3195 error_at(this->location(), "final index required in 3-index slice");
3196 cap = Expression::make_error(this->location());
3198 else
3199 cap = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
3201 if (!this->peek_token()->is_op(OPERATOR_RSQUARE))
3202 error_at(this->location(), "missing %<]%>");
3203 else
3204 this->advance_token();
3205 return Expression::make_index(expr, start, end, cap, location);
3208 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3209 // ArgumentList = ExpressionList [ "..." ] .
3211 Expression*
3212 Parse::call(Expression* func)
3214 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN));
3215 Expression_list* args = NULL;
3216 bool is_varargs = false;
3217 const Token* token = this->advance_token();
3218 if (!token->is_op(OPERATOR_RPAREN))
3220 args = this->expression_list(NULL, false, true);
3221 token = this->peek_token();
3222 if (token->is_op(OPERATOR_ELLIPSIS))
3224 is_varargs = true;
3225 token = this->advance_token();
3228 if (token->is_op(OPERATOR_COMMA))
3229 token = this->advance_token();
3230 if (!token->is_op(OPERATOR_RPAREN))
3231 error_at(this->location(), "missing %<)%>");
3232 else
3233 this->advance_token();
3234 if (func->is_error_expression())
3235 return func;
3236 return Expression::make_call(func, args, is_varargs, func->location());
3239 // Return an expression for a single unqualified identifier.
3241 Expression*
3242 Parse::id_to_expression(const std::string& name, Location location)
3244 Named_object* in_function;
3245 Named_object* named_object = this->gogo_->lookup(name, &in_function);
3246 if (named_object == NULL)
3247 named_object = this->gogo_->add_unknown_name(name, location);
3249 if (in_function != NULL
3250 && in_function != this->gogo_->current_function()
3251 && (named_object->is_variable() || named_object->is_result_variable()))
3252 return this->enclosing_var_reference(in_function, named_object,
3253 location);
3255 switch (named_object->classification())
3257 case Named_object::NAMED_OBJECT_CONST:
3258 return Expression::make_const_reference(named_object, location);
3259 case Named_object::NAMED_OBJECT_VAR:
3260 case Named_object::NAMED_OBJECT_RESULT_VAR:
3261 this->mark_var_used(named_object);
3262 return Expression::make_var_reference(named_object, location);
3263 case Named_object::NAMED_OBJECT_SINK:
3264 return Expression::make_sink(location);
3265 case Named_object::NAMED_OBJECT_FUNC:
3266 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3267 return Expression::make_func_reference(named_object, NULL, location);
3268 case Named_object::NAMED_OBJECT_UNKNOWN:
3270 Unknown_expression* ue =
3271 Expression::make_unknown_reference(named_object, location);
3272 if (this->is_erroneous_function_)
3273 ue->set_no_error_message();
3274 return ue;
3276 case Named_object::NAMED_OBJECT_PACKAGE:
3277 case Named_object::NAMED_OBJECT_TYPE:
3278 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3280 // These cases can arise for a field name in a composite
3281 // literal.
3282 Unknown_expression* ue =
3283 Expression::make_unknown_reference(named_object, location);
3284 if (this->is_erroneous_function_)
3285 ue->set_no_error_message();
3286 return ue;
3288 case Named_object::NAMED_OBJECT_ERRONEOUS:
3289 return Expression::make_error(location);
3290 default:
3291 error_at(this->location(), "unexpected type of identifier");
3292 return Expression::make_error(location);
3296 // Expression = UnaryExpr { binary_op Expression } .
3298 // PRECEDENCE is the precedence of the current operator.
3300 // If MAY_BE_SINK is true, this expression may be "_".
3302 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3303 // literal.
3305 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3306 // guard (var := expr.("type") using the literal keyword "type").
3308 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3309 // if the entire expression is in parentheses.
3311 Expression*
3312 Parse::expression(Precedence precedence, bool may_be_sink,
3313 bool may_be_composite_lit, bool* is_type_switch,
3314 bool *is_parenthesized)
3316 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit,
3317 is_type_switch, is_parenthesized);
3319 while (true)
3321 if (is_type_switch != NULL && *is_type_switch)
3322 return left;
3324 const Token* token = this->peek_token();
3325 if (token->classification() != Token::TOKEN_OPERATOR)
3327 // Not a binary_op.
3328 return left;
3331 Precedence right_precedence;
3332 switch (token->op())
3334 case OPERATOR_OROR:
3335 right_precedence = PRECEDENCE_OROR;
3336 break;
3337 case OPERATOR_ANDAND:
3338 right_precedence = PRECEDENCE_ANDAND;
3339 break;
3340 case OPERATOR_EQEQ:
3341 case OPERATOR_NOTEQ:
3342 case OPERATOR_LT:
3343 case OPERATOR_LE:
3344 case OPERATOR_GT:
3345 case OPERATOR_GE:
3346 right_precedence = PRECEDENCE_RELOP;
3347 break;
3348 case OPERATOR_PLUS:
3349 case OPERATOR_MINUS:
3350 case OPERATOR_OR:
3351 case OPERATOR_XOR:
3352 right_precedence = PRECEDENCE_ADDOP;
3353 break;
3354 case OPERATOR_MULT:
3355 case OPERATOR_DIV:
3356 case OPERATOR_MOD:
3357 case OPERATOR_LSHIFT:
3358 case OPERATOR_RSHIFT:
3359 case OPERATOR_AND:
3360 case OPERATOR_BITCLEAR:
3361 right_precedence = PRECEDENCE_MULOP;
3362 break;
3363 default:
3364 right_precedence = PRECEDENCE_INVALID;
3365 break;
3368 if (right_precedence == PRECEDENCE_INVALID)
3370 // Not a binary_op.
3371 return left;
3374 if (is_parenthesized != NULL)
3375 *is_parenthesized = false;
3377 Operator op = token->op();
3378 Location binop_location = token->location();
3380 if (precedence >= right_precedence)
3382 // We've already seen A * B, and we see + C. We want to
3383 // return so that A * B becomes a group.
3384 return left;
3387 this->advance_token();
3389 left = this->verify_not_sink(left);
3390 Expression* right = this->expression(right_precedence, false,
3391 may_be_composite_lit,
3392 NULL, NULL);
3393 left = Expression::make_binary(op, left, right, binop_location);
3397 bool
3398 Parse::expression_may_start_here()
3400 const Token* token = this->peek_token();
3401 switch (token->classification())
3403 case Token::TOKEN_INVALID:
3404 case Token::TOKEN_EOF:
3405 return false;
3406 case Token::TOKEN_KEYWORD:
3407 switch (token->keyword())
3409 case KEYWORD_CHAN:
3410 case KEYWORD_FUNC:
3411 case KEYWORD_MAP:
3412 case KEYWORD_STRUCT:
3413 case KEYWORD_INTERFACE:
3414 return true;
3415 default:
3416 return false;
3418 case Token::TOKEN_IDENTIFIER:
3419 return true;
3420 case Token::TOKEN_STRING:
3421 return true;
3422 case Token::TOKEN_OPERATOR:
3423 switch (token->op())
3425 case OPERATOR_PLUS:
3426 case OPERATOR_MINUS:
3427 case OPERATOR_NOT:
3428 case OPERATOR_XOR:
3429 case OPERATOR_MULT:
3430 case OPERATOR_CHANOP:
3431 case OPERATOR_AND:
3432 case OPERATOR_LPAREN:
3433 case OPERATOR_LSQUARE:
3434 return true;
3435 default:
3436 return false;
3438 case Token::TOKEN_CHARACTER:
3439 case Token::TOKEN_INTEGER:
3440 case Token::TOKEN_FLOAT:
3441 case Token::TOKEN_IMAGINARY:
3442 return true;
3443 default:
3444 go_unreachable();
3448 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3450 // If MAY_BE_SINK is true, this expression may be "_".
3452 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3453 // literal.
3455 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3456 // guard (var := expr.("type") using the literal keyword "type").
3458 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3459 // if the entire expression is in parentheses.
3461 Expression*
3462 Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit,
3463 bool* is_type_switch, bool* is_parenthesized)
3465 const Token* token = this->peek_token();
3467 // There is a complex parse for <- chan. The choices are
3468 // Convert x to type <- chan int:
3469 // (<- chan int)(x)
3470 // Receive from (x converted to type chan <- chan int):
3471 // (<- chan <- chan int (x))
3472 // Convert x to type <- chan (<- chan int).
3473 // (<- chan <- chan int)(x)
3474 if (token->is_op(OPERATOR_CHANOP))
3476 Location location = token->location();
3477 if (this->advance_token()->is_keyword(KEYWORD_CHAN))
3479 Expression* expr = this->primary_expr(false, may_be_composite_lit,
3480 NULL, NULL);
3481 if (expr->is_error_expression())
3482 return expr;
3483 else if (!expr->is_type_expression())
3484 return Expression::make_receive(expr, location);
3485 else
3487 if (expr->type()->is_error_type())
3488 return expr;
3490 // We picked up "chan TYPE", but it is not a type
3491 // conversion.
3492 Channel_type* ct = expr->type()->channel_type();
3493 if (ct == NULL)
3495 // This is probably impossible.
3496 error_at(location, "expected channel type");
3497 return Expression::make_error(location);
3499 else if (ct->may_receive())
3501 // <- chan TYPE.
3502 Type* t = Type::make_channel_type(false, true,
3503 ct->element_type());
3504 return Expression::make_type(t, location);
3506 else
3508 // <- chan <- TYPE. Because we skipped the leading
3509 // <-, we parsed this as chan <- TYPE. With the
3510 // leading <-, we parse it as <- chan (<- TYPE).
3511 Type *t = this->reassociate_chan_direction(ct, location);
3512 return Expression::make_type(t, location);
3517 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP, location));
3518 token = this->peek_token();
3521 if (token->is_op(OPERATOR_PLUS)
3522 || token->is_op(OPERATOR_MINUS)
3523 || token->is_op(OPERATOR_NOT)
3524 || token->is_op(OPERATOR_XOR)
3525 || token->is_op(OPERATOR_CHANOP)
3526 || token->is_op(OPERATOR_MULT)
3527 || token->is_op(OPERATOR_AND))
3529 Location location = token->location();
3530 Operator op = token->op();
3531 this->advance_token();
3533 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL,
3534 NULL);
3535 if (expr->is_error_expression())
3537 else if (op == OPERATOR_MULT && expr->is_type_expression())
3538 expr = Expression::make_type(Type::make_pointer_type(expr->type()),
3539 location);
3540 else if (op == OPERATOR_AND && expr->is_composite_literal())
3541 expr = Expression::make_heap_expression(expr, location);
3542 else if (op != OPERATOR_CHANOP)
3543 expr = Expression::make_unary(op, expr, location);
3544 else
3545 expr = Expression::make_receive(expr, location);
3546 return expr;
3548 else
3549 return this->primary_expr(may_be_sink, may_be_composite_lit,
3550 is_type_switch, is_parenthesized);
3553 // This is called for the obscure case of
3554 // (<- chan <- chan int)(x)
3555 // In unary_expr we remove the leading <- and parse the remainder,
3556 // which gives us
3557 // chan <- (chan int)
3558 // When we add the leading <- back in, we really want
3559 // <- chan (<- chan int)
3560 // This means that we need to reassociate.
3562 Type*
3563 Parse::reassociate_chan_direction(Channel_type *ct, Location location)
3565 Channel_type* ele = ct->element_type()->channel_type();
3566 if (ele == NULL)
3568 error_at(location, "parse error");
3569 return Type::make_error_type();
3571 Type* sub = ele;
3572 if (ele->may_send())
3573 sub = Type::make_channel_type(false, true, ele->element_type());
3574 else
3575 sub = this->reassociate_chan_direction(ele, location);
3576 return Type::make_channel_type(false, true, sub);
3579 // Statement =
3580 // Declaration | LabeledStmt | SimpleStmt |
3581 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3582 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3583 // DeferStmt .
3585 // LABEL is the label of this statement if it has one.
3587 void
3588 Parse::statement(Label* label)
3590 const Token* token = this->peek_token();
3591 switch (token->classification())
3593 case Token::TOKEN_KEYWORD:
3595 switch (token->keyword())
3597 case KEYWORD_CONST:
3598 case KEYWORD_TYPE:
3599 case KEYWORD_VAR:
3600 this->declaration();
3601 break;
3602 case KEYWORD_FUNC:
3603 case KEYWORD_MAP:
3604 case KEYWORD_STRUCT:
3605 case KEYWORD_INTERFACE:
3606 this->simple_stat(true, NULL, NULL, NULL);
3607 break;
3608 case KEYWORD_GO:
3609 case KEYWORD_DEFER:
3610 this->go_or_defer_stat();
3611 break;
3612 case KEYWORD_RETURN:
3613 this->return_stat();
3614 break;
3615 case KEYWORD_BREAK:
3616 this->break_stat();
3617 break;
3618 case KEYWORD_CONTINUE:
3619 this->continue_stat();
3620 break;
3621 case KEYWORD_GOTO:
3622 this->goto_stat();
3623 break;
3624 case KEYWORD_IF:
3625 this->if_stat();
3626 break;
3627 case KEYWORD_SWITCH:
3628 this->switch_stat(label);
3629 break;
3630 case KEYWORD_SELECT:
3631 this->select_stat(label);
3632 break;
3633 case KEYWORD_FOR:
3634 this->for_stat(label);
3635 break;
3636 default:
3637 error_at(this->location(), "expected statement");
3638 this->advance_token();
3639 break;
3642 break;
3644 case Token::TOKEN_IDENTIFIER:
3646 std::string identifier = token->identifier();
3647 bool is_exported = token->is_identifier_exported();
3648 Location location = token->location();
3649 if (this->advance_token()->is_op(OPERATOR_COLON))
3651 this->advance_token();
3652 this->labeled_stmt(identifier, location);
3654 else
3656 this->unget_token(Token::make_identifier_token(identifier,
3657 is_exported,
3658 location));
3659 this->simple_stat(true, NULL, NULL, NULL);
3662 break;
3664 case Token::TOKEN_OPERATOR:
3665 if (token->is_op(OPERATOR_LCURLY))
3667 Location location = token->location();
3668 this->gogo_->start_block(location);
3669 Location end_loc = this->block();
3670 this->gogo_->add_block(this->gogo_->finish_block(end_loc),
3671 location);
3673 else if (!token->is_op(OPERATOR_SEMICOLON))
3674 this->simple_stat(true, NULL, NULL, NULL);
3675 break;
3677 case Token::TOKEN_STRING:
3678 case Token::TOKEN_CHARACTER:
3679 case Token::TOKEN_INTEGER:
3680 case Token::TOKEN_FLOAT:
3681 case Token::TOKEN_IMAGINARY:
3682 this->simple_stat(true, NULL, NULL, NULL);
3683 break;
3685 default:
3686 error_at(this->location(), "expected statement");
3687 this->advance_token();
3688 break;
3692 bool
3693 Parse::statement_may_start_here()
3695 const Token* token = this->peek_token();
3696 switch (token->classification())
3698 case Token::TOKEN_KEYWORD:
3700 switch (token->keyword())
3702 case KEYWORD_CONST:
3703 case KEYWORD_TYPE:
3704 case KEYWORD_VAR:
3705 case KEYWORD_FUNC:
3706 case KEYWORD_MAP:
3707 case KEYWORD_STRUCT:
3708 case KEYWORD_INTERFACE:
3709 case KEYWORD_GO:
3710 case KEYWORD_DEFER:
3711 case KEYWORD_RETURN:
3712 case KEYWORD_BREAK:
3713 case KEYWORD_CONTINUE:
3714 case KEYWORD_GOTO:
3715 case KEYWORD_IF:
3716 case KEYWORD_SWITCH:
3717 case KEYWORD_SELECT:
3718 case KEYWORD_FOR:
3719 return true;
3721 default:
3722 return false;
3725 break;
3727 case Token::TOKEN_IDENTIFIER:
3728 return true;
3730 case Token::TOKEN_OPERATOR:
3731 if (token->is_op(OPERATOR_LCURLY)
3732 || token->is_op(OPERATOR_SEMICOLON))
3733 return true;
3734 else
3735 return this->expression_may_start_here();
3737 case Token::TOKEN_STRING:
3738 case Token::TOKEN_CHARACTER:
3739 case Token::TOKEN_INTEGER:
3740 case Token::TOKEN_FLOAT:
3741 case Token::TOKEN_IMAGINARY:
3742 return true;
3744 default:
3745 return false;
3749 // LabeledStmt = Label ":" Statement .
3750 // Label = identifier .
3752 void
3753 Parse::labeled_stmt(const std::string& label_name, Location location)
3755 Label* label = this->gogo_->add_label_definition(label_name, location);
3757 if (this->peek_token()->is_op(OPERATOR_RCURLY))
3759 // This is a label at the end of a block. A program is
3760 // permitted to omit a semicolon here.
3761 return;
3764 if (!this->statement_may_start_here())
3766 // Mark the label as used to avoid a useless error about an
3767 // unused label.
3768 if (label != NULL)
3769 label->set_is_used();
3771 error_at(location, "missing statement after label");
3772 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
3773 location));
3774 return;
3777 this->statement(label);
3780 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3781 // Assignment | ShortVarDecl .
3783 // EmptyStmt was handled in Parse::statement.
3785 // In order to make this work for if and switch statements, if
3786 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3787 // expression rather than adding an expression statement to the
3788 // current block. If we see something other than an ExpressionStat,
3789 // we add the statement, set *RETURN_EXP to true if we saw a send
3790 // statement, and return NULL. The handling of send statements is for
3791 // better error messages.
3793 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3794 // RangeClause.
3796 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3797 // guard (var := expr.("type") using the literal keyword "type").
3799 Expression*
3800 Parse::simple_stat(bool may_be_composite_lit, bool* return_exp,
3801 Range_clause* p_range_clause, Type_switch* p_type_switch)
3803 const Token* token = this->peek_token();
3805 // An identifier follow by := is a SimpleVarDecl.
3806 if (token->is_identifier())
3808 std::string identifier = token->identifier();
3809 bool is_exported = token->is_identifier_exported();
3810 Location location = token->location();
3812 token = this->advance_token();
3813 if (token->is_op(OPERATOR_COLONEQ)
3814 || token->is_op(OPERATOR_COMMA))
3816 identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
3817 this->simple_var_decl_or_assignment(identifier, location,
3818 may_be_composite_lit,
3819 p_range_clause,
3820 (token->is_op(OPERATOR_COLONEQ)
3821 ? p_type_switch
3822 : NULL));
3823 return NULL;
3826 this->unget_token(Token::make_identifier_token(identifier, is_exported,
3827 location));
3830 Expression* exp = this->expression(PRECEDENCE_NORMAL, true,
3831 may_be_composite_lit,
3832 (p_type_switch == NULL
3833 ? NULL
3834 : &p_type_switch->found),
3835 NULL);
3836 if (p_type_switch != NULL && p_type_switch->found)
3838 p_type_switch->name.clear();
3839 p_type_switch->location = exp->location();
3840 p_type_switch->expr = this->verify_not_sink(exp);
3841 return NULL;
3843 token = this->peek_token();
3844 if (token->is_op(OPERATOR_CHANOP))
3846 this->send_stmt(this->verify_not_sink(exp));
3847 if (return_exp != NULL)
3848 *return_exp = true;
3850 else if (token->is_op(OPERATOR_PLUSPLUS)
3851 || token->is_op(OPERATOR_MINUSMINUS))
3852 this->inc_dec_stat(this->verify_not_sink(exp));
3853 else if (token->is_op(OPERATOR_COMMA)
3854 || token->is_op(OPERATOR_EQ))
3855 this->assignment(exp, may_be_composite_lit, p_range_clause);
3856 else if (token->is_op(OPERATOR_PLUSEQ)
3857 || token->is_op(OPERATOR_MINUSEQ)
3858 || token->is_op(OPERATOR_OREQ)
3859 || token->is_op(OPERATOR_XOREQ)
3860 || token->is_op(OPERATOR_MULTEQ)
3861 || token->is_op(OPERATOR_DIVEQ)
3862 || token->is_op(OPERATOR_MODEQ)
3863 || token->is_op(OPERATOR_LSHIFTEQ)
3864 || token->is_op(OPERATOR_RSHIFTEQ)
3865 || token->is_op(OPERATOR_ANDEQ)
3866 || token->is_op(OPERATOR_BITCLEAREQ))
3867 this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
3868 p_range_clause);
3869 else if (return_exp != NULL)
3870 return this->verify_not_sink(exp);
3871 else
3873 exp = this->verify_not_sink(exp);
3875 if (token->is_op(OPERATOR_COLONEQ))
3877 if (!exp->is_error_expression())
3878 error_at(token->location(), "non-name on left side of %<:=%>");
3879 this->gogo_->mark_locals_used();
3880 while (!token->is_op(OPERATOR_SEMICOLON)
3881 && !token->is_eof())
3882 token = this->advance_token();
3883 return NULL;
3886 this->expression_stat(exp);
3889 return NULL;
3892 bool
3893 Parse::simple_stat_may_start_here()
3895 return this->expression_may_start_here();
3898 // Parse { Statement ";" } which is used in a few places. The list of
3899 // statements may end with a right curly brace, in which case the
3900 // semicolon may be omitted.
3902 void
3903 Parse::statement_list()
3905 while (this->statement_may_start_here())
3907 this->statement(NULL);
3908 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
3909 this->advance_token();
3910 else if (this->peek_token()->is_op(OPERATOR_RCURLY))
3911 break;
3912 else
3914 if (!this->peek_token()->is_eof() || !saw_errors())
3915 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3916 if (!this->skip_past_error(OPERATOR_RCURLY))
3917 return;
3922 bool
3923 Parse::statement_list_may_start_here()
3925 return this->statement_may_start_here();
3928 // ExpressionStat = Expression .
3930 void
3931 Parse::expression_stat(Expression* exp)
3933 this->gogo_->add_statement(Statement::make_statement(exp, false));
3936 // SendStmt = Channel "&lt;-" Expression .
3937 // Channel = Expression .
3939 void
3940 Parse::send_stmt(Expression* channel)
3942 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
3943 Location loc = this->location();
3944 this->advance_token();
3945 Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
3946 NULL);
3947 Statement* s = Statement::make_send_statement(channel, val, loc);
3948 this->gogo_->add_statement(s);
3951 // IncDecStat = Expression ( "++" | "--" ) .
3953 void
3954 Parse::inc_dec_stat(Expression* exp)
3956 const Token* token = this->peek_token();
3958 // Lvalue maps require special handling.
3959 if (exp->index_expression() != NULL)
3960 exp->index_expression()->set_is_lvalue();
3962 if (token->is_op(OPERATOR_PLUSPLUS))
3963 this->gogo_->add_statement(Statement::make_inc_statement(exp));
3964 else if (token->is_op(OPERATOR_MINUSMINUS))
3965 this->gogo_->add_statement(Statement::make_dec_statement(exp));
3966 else
3967 go_unreachable();
3968 this->advance_token();
3971 // Assignment = ExpressionList assign_op ExpressionList .
3973 // EXP is an expression that we have already parsed.
3975 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3976 // side may be a composite literal.
3978 // If RANGE_CLAUSE is not NULL, then this will recognize a
3979 // RangeClause.
3981 void
3982 Parse::assignment(Expression* expr, bool may_be_composite_lit,
3983 Range_clause* p_range_clause)
3985 Expression_list* vars;
3986 if (!this->peek_token()->is_op(OPERATOR_COMMA))
3988 vars = new Expression_list();
3989 vars->push_back(expr);
3991 else
3993 this->advance_token();
3994 vars = this->expression_list(expr, true, may_be_composite_lit);
3997 this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
4000 // An assignment statement. LHS is the list of expressions which
4001 // appear on the left hand side.
4003 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4004 // side may be a composite literal.
4006 // If RANGE_CLAUSE is not NULL, then this will recognize a
4007 // RangeClause.
4009 void
4010 Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
4011 Range_clause* p_range_clause)
4013 const Token* token = this->peek_token();
4014 if (!token->is_op(OPERATOR_EQ)
4015 && !token->is_op(OPERATOR_PLUSEQ)
4016 && !token->is_op(OPERATOR_MINUSEQ)
4017 && !token->is_op(OPERATOR_OREQ)
4018 && !token->is_op(OPERATOR_XOREQ)
4019 && !token->is_op(OPERATOR_MULTEQ)
4020 && !token->is_op(OPERATOR_DIVEQ)
4021 && !token->is_op(OPERATOR_MODEQ)
4022 && !token->is_op(OPERATOR_LSHIFTEQ)
4023 && !token->is_op(OPERATOR_RSHIFTEQ)
4024 && !token->is_op(OPERATOR_ANDEQ)
4025 && !token->is_op(OPERATOR_BITCLEAREQ))
4027 error_at(this->location(), "expected assignment operator");
4028 return;
4030 Operator op = token->op();
4031 Location location = token->location();
4033 token = this->advance_token();
4035 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE))
4037 if (op != OPERATOR_EQ)
4038 error_at(this->location(), "range clause requires %<=%>");
4039 this->range_clause_expr(lhs, p_range_clause);
4040 return;
4043 Expression_list* vals = this->expression_list(NULL, false,
4044 may_be_composite_lit);
4046 // We've parsed everything; check for errors.
4047 if (lhs == NULL || vals == NULL)
4048 return;
4049 for (Expression_list::const_iterator pe = lhs->begin();
4050 pe != lhs->end();
4051 ++pe)
4053 if ((*pe)->is_error_expression())
4054 return;
4055 if (op != OPERATOR_EQ && (*pe)->is_sink_expression())
4056 error_at((*pe)->location(), "cannot use _ as value");
4058 for (Expression_list::const_iterator pe = vals->begin();
4059 pe != vals->end();
4060 ++pe)
4062 if ((*pe)->is_error_expression())
4063 return;
4066 // Map expressions act differently when they are lvalues.
4067 for (Expression_list::iterator plv = lhs->begin();
4068 plv != lhs->end();
4069 ++plv)
4070 if ((*plv)->index_expression() != NULL)
4071 (*plv)->index_expression()->set_is_lvalue();
4073 Call_expression* call;
4074 Index_expression* map_index;
4075 Receive_expression* receive;
4076 Type_guard_expression* type_guard;
4077 if (lhs->size() == vals->size())
4079 Statement* s;
4080 if (lhs->size() > 1)
4082 if (op != OPERATOR_EQ)
4083 error_at(location, "multiple values only permitted with %<=%>");
4084 s = Statement::make_tuple_assignment(lhs, vals, location);
4086 else
4088 if (op == OPERATOR_EQ)
4089 s = Statement::make_assignment(lhs->front(), vals->front(),
4090 location);
4091 else
4092 s = Statement::make_assignment_operation(op, lhs->front(),
4093 vals->front(), location);
4094 delete lhs;
4095 delete vals;
4097 this->gogo_->add_statement(s);
4099 else if (vals->size() == 1
4100 && (call = (*vals->begin())->call_expression()) != NULL)
4102 if (op != OPERATOR_EQ)
4103 error_at(location, "multiple results only permitted with %<=%>");
4104 delete vals;
4105 vals = new Expression_list;
4106 for (unsigned int i = 0; i < lhs->size(); ++i)
4107 vals->push_back(Expression::make_call_result(call, i));
4108 Statement* s = Statement::make_tuple_assignment(lhs, vals, location);
4109 this->gogo_->add_statement(s);
4111 else if (lhs->size() == 2
4112 && vals->size() == 1
4113 && (map_index = (*vals->begin())->index_expression()) != NULL)
4115 if (op != OPERATOR_EQ)
4116 error_at(location, "two values from map requires %<=%>");
4117 Expression* val = lhs->front();
4118 Expression* present = lhs->back();
4119 Statement* s = Statement::make_tuple_map_assignment(val, present,
4120 map_index, location);
4121 this->gogo_->add_statement(s);
4123 else if (lhs->size() == 1
4124 && vals->size() == 2
4125 && (map_index = lhs->front()->index_expression()) != NULL)
4127 if (op != OPERATOR_EQ)
4128 error_at(location, "assigning tuple to map index requires %<=%>");
4129 Expression* val = vals->front();
4130 Expression* should_set = vals->back();
4131 Statement* s = Statement::make_map_assignment(map_index, val, should_set,
4132 location);
4133 this->gogo_->add_statement(s);
4135 else if (lhs->size() == 2
4136 && vals->size() == 1
4137 && (receive = (*vals->begin())->receive_expression()) != NULL)
4139 if (op != OPERATOR_EQ)
4140 error_at(location, "two values from receive requires %<=%>");
4141 Expression* val = lhs->front();
4142 Expression* success = lhs->back();
4143 Expression* channel = receive->channel();
4144 Statement* s = Statement::make_tuple_receive_assignment(val, success,
4145 channel,
4146 location);
4147 this->gogo_->add_statement(s);
4149 else if (lhs->size() == 2
4150 && vals->size() == 1
4151 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL)
4153 if (op != OPERATOR_EQ)
4154 error_at(location, "two values from type guard requires %<=%>");
4155 Expression* val = lhs->front();
4156 Expression* ok = lhs->back();
4157 Expression* expr = type_guard->expr();
4158 Type* type = type_guard->type();
4159 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok,
4160 expr, type,
4161 location);
4162 this->gogo_->add_statement(s);
4164 else
4166 error_at(location, "number of variables does not match number of values");
4170 // GoStat = "go" Expression .
4171 // DeferStat = "defer" Expression .
4173 void
4174 Parse::go_or_defer_stat()
4176 go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
4177 || this->peek_token()->is_keyword(KEYWORD_DEFER));
4178 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
4179 Location stat_location = this->location();
4181 this->advance_token();
4182 Location expr_location = this->location();
4184 bool is_parenthesized = false;
4185 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
4186 &is_parenthesized);
4187 Call_expression* call_expr = expr->call_expression();
4188 if (is_parenthesized || call_expr == NULL)
4190 error_at(expr_location, "argument to go/defer must be function call");
4191 return;
4194 // Make it easier to simplify go/defer statements by putting every
4195 // statement in its own block.
4196 this->gogo_->start_block(stat_location);
4197 Statement* stat;
4198 if (is_go)
4199 stat = Statement::make_go_statement(call_expr, stat_location);
4200 else
4201 stat = Statement::make_defer_statement(call_expr, stat_location);
4202 this->gogo_->add_statement(stat);
4203 this->gogo_->add_block(this->gogo_->finish_block(stat_location),
4204 stat_location);
4207 // ReturnStat = "return" [ ExpressionList ] .
4209 void
4210 Parse::return_stat()
4212 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
4213 Location location = this->location();
4214 this->advance_token();
4215 Expression_list* vals = NULL;
4216 if (this->expression_may_start_here())
4217 vals = this->expression_list(NULL, false, true);
4218 this->gogo_->add_statement(Statement::make_return_statement(vals, location));
4220 if (vals == NULL
4221 && this->gogo_->current_function()->func_value()->results_are_named())
4223 Named_object* function = this->gogo_->current_function();
4224 Function::Results* results = function->func_value()->result_variables();
4225 for (Function::Results::const_iterator p = results->begin();
4226 p != results->end();
4227 ++p)
4229 Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
4230 if (no == NULL)
4231 go_assert(saw_errors());
4232 else if (!no->is_result_variable())
4233 error_at(location, "%qs is shadowed during return",
4234 (*p)->message_name().c_str());
4239 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4240 // [ "else" ( IfStmt | Block ) ] .
4242 void
4243 Parse::if_stat()
4245 go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
4246 Location location = this->location();
4247 this->advance_token();
4249 this->gogo_->start_block(location);
4251 bool saw_simple_stat = false;
4252 Expression* cond = NULL;
4253 bool saw_send_stmt = false;
4254 if (this->simple_stat_may_start_here())
4256 cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
4257 saw_simple_stat = true;
4259 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4261 // The SimpleStat is an expression statement.
4262 this->expression_stat(cond);
4263 cond = NULL;
4265 if (cond == NULL)
4267 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4268 this->advance_token();
4269 else if (saw_simple_stat)
4271 if (saw_send_stmt)
4272 error_at(this->location(),
4273 ("send statement used as value; "
4274 "use select for non-blocking send"));
4275 else
4276 error_at(this->location(),
4277 "expected %<;%> after statement in if expression");
4278 if (!this->expression_may_start_here())
4279 cond = Expression::make_error(this->location());
4281 if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY))
4283 error_at(this->location(),
4284 "missing condition in if statement");
4285 cond = Expression::make_error(this->location());
4287 if (cond == NULL)
4288 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL, NULL);
4291 // Check for the easy error of a newline before starting the block.
4292 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4294 Location semi_loc = this->location();
4295 if (this->advance_token()->is_op(OPERATOR_LCURLY))
4296 error_at(semi_loc, "missing %<{%> after if clause");
4297 // Otherwise we will get an error when we call this->block
4298 // below.
4301 this->gogo_->start_block(this->location());
4302 Location end_loc = this->block();
4303 Block* then_block = this->gogo_->finish_block(end_loc);
4305 // Check for the easy error of a newline before "else".
4306 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4308 Location semi_loc = this->location();
4309 if (this->advance_token()->is_keyword(KEYWORD_ELSE))
4310 error_at(this->location(),
4311 "unexpected semicolon or newline before %<else%>");
4312 else
4313 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON,
4314 semi_loc));
4317 Block* else_block = NULL;
4318 if (this->peek_token()->is_keyword(KEYWORD_ELSE))
4320 this->gogo_->start_block(this->location());
4321 const Token* token = this->advance_token();
4322 if (token->is_keyword(KEYWORD_IF))
4323 this->if_stat();
4324 else if (token->is_op(OPERATOR_LCURLY))
4325 this->block();
4326 else
4328 error_at(this->location(), "expected %<if%> or %<{%>");
4329 this->statement(NULL);
4331 else_block = this->gogo_->finish_block(this->location());
4334 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block,
4335 else_block,
4336 location));
4338 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4339 location);
4342 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4343 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4344 // "{" { ExprCaseClause } "}" .
4345 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4346 // "{" { TypeCaseClause } "}" .
4347 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4349 void
4350 Parse::switch_stat(Label* label)
4352 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
4353 Location location = this->location();
4354 this->advance_token();
4356 this->gogo_->start_block(location);
4358 bool saw_simple_stat = false;
4359 Expression* switch_val = NULL;
4360 bool saw_send_stmt;
4361 Type_switch type_switch;
4362 bool have_type_switch_block = false;
4363 if (this->simple_stat_may_start_here())
4365 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4366 &type_switch);
4367 saw_simple_stat = true;
4369 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON))
4371 // The SimpleStat is an expression statement.
4372 this->expression_stat(switch_val);
4373 switch_val = NULL;
4375 if (switch_val == NULL && !type_switch.found)
4377 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4378 this->advance_token();
4379 else if (saw_simple_stat)
4381 if (saw_send_stmt)
4382 error_at(this->location(),
4383 ("send statement used as value; "
4384 "use select for non-blocking send"));
4385 else
4386 error_at(this->location(),
4387 "expected %<;%> after statement in switch expression");
4389 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4391 if (this->peek_token()->is_identifier())
4393 const Token* token = this->peek_token();
4394 std::string identifier = token->identifier();
4395 bool is_exported = token->is_identifier_exported();
4396 Location id_loc = token->location();
4398 token = this->advance_token();
4399 bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
4400 this->unget_token(Token::make_identifier_token(identifier,
4401 is_exported,
4402 id_loc));
4403 if (is_coloneq)
4405 // This must be a TypeSwitchGuard. It is in a
4406 // different block from any initial SimpleStat.
4407 if (saw_simple_stat)
4409 this->gogo_->start_block(id_loc);
4410 have_type_switch_block = true;
4413 switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
4414 &type_switch);
4415 if (!type_switch.found)
4417 if (switch_val == NULL
4418 || !switch_val->is_error_expression())
4420 error_at(id_loc, "expected type switch assignment");
4421 switch_val = Expression::make_error(id_loc);
4426 if (switch_val == NULL && !type_switch.found)
4428 switch_val = this->expression(PRECEDENCE_NORMAL, false, false,
4429 &type_switch.found, NULL);
4430 if (type_switch.found)
4432 type_switch.name.clear();
4433 type_switch.expr = switch_val;
4434 type_switch.location = switch_val->location();
4440 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4442 Location token_loc = this->location();
4443 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
4444 && this->advance_token()->is_op(OPERATOR_LCURLY))
4445 error_at(token_loc, "missing %<{%> after switch clause");
4446 else if (this->peek_token()->is_op(OPERATOR_COLONEQ))
4448 error_at(token_loc, "invalid variable name");
4449 this->advance_token();
4450 this->expression(PRECEDENCE_NORMAL, false, false,
4451 &type_switch.found, NULL);
4452 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
4453 this->advance_token();
4454 if (!this->peek_token()->is_op(OPERATOR_LCURLY))
4456 if (have_type_switch_block)
4457 this->gogo_->add_block(this->gogo_->finish_block(location),
4458 location);
4459 this->gogo_->add_block(this->gogo_->finish_block(location),
4460 location);
4461 return;
4463 if (type_switch.found)
4464 type_switch.expr = Expression::make_error(location);
4466 else
4468 error_at(this->location(), "expected %<{%>");
4469 if (have_type_switch_block)
4470 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4471 location);
4472 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4473 location);
4474 return;
4477 this->advance_token();
4479 Statement* statement;
4480 if (type_switch.found)
4481 statement = this->type_switch_body(label, type_switch, location);
4482 else
4483 statement = this->expr_switch_body(label, switch_val, location);
4485 if (statement != NULL)
4486 this->gogo_->add_statement(statement);
4488 if (have_type_switch_block)
4489 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4490 location);
4492 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
4493 location);
4496 // The body of an expression switch.
4497 // "{" { ExprCaseClause } "}"
4499 Statement*
4500 Parse::expr_switch_body(Label* label, Expression* switch_val,
4501 Location location)
4503 Switch_statement* statement = Statement::make_switch_statement(switch_val,
4504 location);
4506 this->push_break_statement(statement, label);
4508 Case_clauses* case_clauses = new Case_clauses();
4509 bool saw_default = false;
4510 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4512 if (this->peek_token()->is_eof())
4514 if (!saw_errors())
4515 error_at(this->location(), "missing %<}%>");
4516 return NULL;
4518 this->expr_case_clause(case_clauses, &saw_default);
4520 this->advance_token();
4522 statement->add_clauses(case_clauses);
4524 this->pop_break_statement();
4526 return statement;
4529 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4530 // FallthroughStat = "fallthrough" .
4532 void
4533 Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
4535 Location location = this->location();
4537 bool is_default = false;
4538 Expression_list* vals = this->expr_switch_case(&is_default);
4540 if (!this->peek_token()->is_op(OPERATOR_COLON))
4542 if (!saw_errors())
4543 error_at(this->location(), "expected %<:%>");
4544 return;
4546 else
4547 this->advance_token();
4549 Block* statements = NULL;
4550 if (this->statement_list_may_start_here())
4552 this->gogo_->start_block(this->location());
4553 this->statement_list();
4554 statements = this->gogo_->finish_block(this->location());
4557 bool is_fallthrough = false;
4558 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4560 Location fallthrough_loc = this->location();
4561 is_fallthrough = true;
4562 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4563 this->advance_token();
4564 if (this->peek_token()->is_op(OPERATOR_RCURLY))
4565 error_at(fallthrough_loc, _("cannot fallthrough final case in switch"));
4568 if (is_default)
4570 if (*saw_default)
4572 error_at(location, "multiple defaults in switch");
4573 return;
4575 *saw_default = true;
4578 if (is_default || vals != NULL)
4579 clauses->add(vals, is_default, statements, is_fallthrough, location);
4582 // ExprSwitchCase = "case" ExpressionList | "default" .
4584 Expression_list*
4585 Parse::expr_switch_case(bool* is_default)
4587 const Token* token = this->peek_token();
4588 if (token->is_keyword(KEYWORD_CASE))
4590 this->advance_token();
4591 return this->expression_list(NULL, false, true);
4593 else if (token->is_keyword(KEYWORD_DEFAULT))
4595 this->advance_token();
4596 *is_default = true;
4597 return NULL;
4599 else
4601 if (!saw_errors())
4602 error_at(this->location(), "expected %<case%> or %<default%>");
4603 if (!token->is_op(OPERATOR_RCURLY))
4604 this->advance_token();
4605 return NULL;
4609 // The body of a type switch.
4610 // "{" { TypeCaseClause } "}" .
4612 Statement*
4613 Parse::type_switch_body(Label* label, const Type_switch& type_switch,
4614 Location location)
4616 Named_object* switch_no = NULL;
4617 if (!type_switch.name.empty())
4619 if (Gogo::is_sink_name(type_switch.name))
4620 error_at(type_switch.location,
4621 "no new variables on left side of %<:=%>");
4622 else
4624 Variable* switch_var = new Variable(NULL, type_switch.expr, false,
4625 false, false,
4626 type_switch.location);
4627 switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
4631 Type_switch_statement* statement =
4632 Statement::make_type_switch_statement(switch_no,
4633 (switch_no == NULL
4634 ? type_switch.expr
4635 : NULL),
4636 location);
4638 this->push_break_statement(statement, label);
4640 Type_case_clauses* case_clauses = new Type_case_clauses();
4641 bool saw_default = false;
4642 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4644 if (this->peek_token()->is_eof())
4646 error_at(this->location(), "missing %<}%>");
4647 return NULL;
4649 this->type_case_clause(switch_no, case_clauses, &saw_default);
4651 this->advance_token();
4653 statement->add_clauses(case_clauses);
4655 this->pop_break_statement();
4657 return statement;
4660 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4662 void
4663 Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
4664 bool* saw_default)
4666 Location location = this->location();
4668 std::vector<Type*> types;
4669 bool is_default = false;
4670 this->type_switch_case(&types, &is_default);
4672 if (!this->peek_token()->is_op(OPERATOR_COLON))
4673 error_at(this->location(), "expected %<:%>");
4674 else
4675 this->advance_token();
4677 Block* statements = NULL;
4678 if (this->statement_list_may_start_here())
4680 this->gogo_->start_block(this->location());
4681 if (switch_no != NULL && types.size() == 1)
4683 Type* type = types.front();
4684 Expression* init = Expression::make_var_reference(switch_no,
4685 location);
4686 init = Expression::make_type_guard(init, type, location);
4687 Variable* v = new Variable(type, init, false, false, false,
4688 location);
4689 v->set_is_type_switch_var();
4690 Named_object* no = this->gogo_->add_variable(switch_no->name(), v);
4692 // We don't want to issue an error if the compiler
4693 // introduced special variable is not used. Instead we want
4694 // to issue an error if the variable defined by the switch
4695 // is not used. That is handled via type_switch_vars_ and
4696 // Parse::mark_var_used.
4697 v->set_is_used();
4698 this->type_switch_vars_[no] = switch_no;
4700 this->statement_list();
4701 statements = this->gogo_->finish_block(this->location());
4704 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH))
4706 error_at(this->location(),
4707 "fallthrough is not permitted in a type switch");
4708 if (this->advance_token()->is_op(OPERATOR_SEMICOLON))
4709 this->advance_token();
4712 if (is_default)
4714 go_assert(types.empty());
4715 if (*saw_default)
4717 error_at(location, "multiple defaults in type switch");
4718 return;
4720 *saw_default = true;
4721 clauses->add(NULL, false, true, statements, location);
4723 else if (!types.empty())
4725 for (std::vector<Type*>::const_iterator p = types.begin();
4726 p + 1 != types.end();
4727 ++p)
4728 clauses->add(*p, true, false, NULL, location);
4729 clauses->add(types.back(), false, false, statements, location);
4731 else
4732 clauses->add(Type::make_error_type(), false, false, statements, location);
4735 // TypeSwitchCase = "case" type | "default"
4737 // We accept a comma separated list of types.
4739 void
4740 Parse::type_switch_case(std::vector<Type*>* types, bool* is_default)
4742 const Token* token = this->peek_token();
4743 if (token->is_keyword(KEYWORD_CASE))
4745 this->advance_token();
4746 while (true)
4748 Type* t = this->type();
4750 if (!t->is_error_type())
4751 types->push_back(t);
4752 else
4754 this->gogo_->mark_locals_used();
4755 token = this->peek_token();
4756 while (!token->is_op(OPERATOR_COLON)
4757 && !token->is_op(OPERATOR_COMMA)
4758 && !token->is_op(OPERATOR_RCURLY)
4759 && !token->is_eof())
4760 token = this->advance_token();
4763 if (!this->peek_token()->is_op(OPERATOR_COMMA))
4764 break;
4765 this->advance_token();
4768 else if (token->is_keyword(KEYWORD_DEFAULT))
4770 this->advance_token();
4771 *is_default = true;
4773 else
4775 error_at(this->location(), "expected %<case%> or %<default%>");
4776 if (!token->is_op(OPERATOR_RCURLY))
4777 this->advance_token();
4781 // SelectStat = "select" "{" { CommClause } "}" .
4783 void
4784 Parse::select_stat(Label* label)
4786 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
4787 Location location = this->location();
4788 const Token* token = this->advance_token();
4790 if (!token->is_op(OPERATOR_LCURLY))
4792 Location token_loc = token->location();
4793 if (token->is_op(OPERATOR_SEMICOLON)
4794 && this->advance_token()->is_op(OPERATOR_LCURLY))
4795 error_at(token_loc, "unexpected semicolon or newline before %<{%>");
4796 else
4798 error_at(this->location(), "expected %<{%>");
4799 return;
4802 this->advance_token();
4804 Select_statement* statement = Statement::make_select_statement(location);
4806 this->push_break_statement(statement, label);
4808 Select_clauses* select_clauses = new Select_clauses();
4809 bool saw_default = false;
4810 while (!this->peek_token()->is_op(OPERATOR_RCURLY))
4812 if (this->peek_token()->is_eof())
4814 error_at(this->location(), "expected %<}%>");
4815 return;
4817 this->comm_clause(select_clauses, &saw_default);
4820 this->advance_token();
4822 statement->add_clauses(select_clauses);
4824 this->pop_break_statement();
4826 this->gogo_->add_statement(statement);
4829 // CommClause = CommCase ":" { Statement ";" } .
4831 void
4832 Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
4834 Location location = this->location();
4835 bool is_send = false;
4836 Expression* channel = NULL;
4837 Expression* val = NULL;
4838 Expression* closed = NULL;
4839 std::string varname;
4840 std::string closedname;
4841 bool is_default = false;
4842 bool got_case = this->comm_case(&is_send, &channel, &val, &closed,
4843 &varname, &closedname, &is_default);
4845 if (!is_send
4846 && varname.empty()
4847 && closedname.empty()
4848 && val != NULL
4849 && val->index_expression() != NULL)
4850 val->index_expression()->set_is_lvalue();
4852 if (this->peek_token()->is_op(OPERATOR_COLON))
4853 this->advance_token();
4854 else
4855 error_at(this->location(), "expected colon");
4857 this->gogo_->start_block(this->location());
4859 Named_object* var = NULL;
4860 if (!varname.empty())
4862 // FIXME: LOCATION is slightly wrong here.
4863 Variable* v = new Variable(NULL, channel, false, false, false,
4864 location);
4865 v->set_type_from_chan_element();
4866 var = this->gogo_->add_variable(varname, v);
4869 Named_object* closedvar = NULL;
4870 if (!closedname.empty())
4872 // FIXME: LOCATION is slightly wrong here.
4873 Variable* v = new Variable(Type::lookup_bool_type(), NULL,
4874 false, false, false, location);
4875 closedvar = this->gogo_->add_variable(closedname, v);
4878 this->statement_list();
4880 Block* statements = this->gogo_->finish_block(this->location());
4882 if (is_default)
4884 if (*saw_default)
4886 error_at(location, "multiple defaults in select");
4887 return;
4889 *saw_default = true;
4892 if (got_case)
4893 clauses->add(is_send, channel, val, closed, var, closedvar, is_default,
4894 statements, location);
4895 else if (statements != NULL)
4897 // Add the statements to make sure that any names they define
4898 // are traversed.
4899 this->gogo_->add_block(statements, location);
4903 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4905 bool
4906 Parse::comm_case(bool* is_send, Expression** channel, Expression** val,
4907 Expression** closed, std::string* varname,
4908 std::string* closedname, bool* is_default)
4910 const Token* token = this->peek_token();
4911 if (token->is_keyword(KEYWORD_DEFAULT))
4913 this->advance_token();
4914 *is_default = true;
4916 else if (token->is_keyword(KEYWORD_CASE))
4918 this->advance_token();
4919 if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname,
4920 closedname))
4921 return false;
4923 else
4925 error_at(this->location(), "expected %<case%> or %<default%>");
4926 if (!token->is_op(OPERATOR_RCURLY))
4927 this->advance_token();
4928 return false;
4931 return true;
4934 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4935 // RecvExpr = Expression .
4937 bool
4938 Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val,
4939 Expression** closed, std::string* varname,
4940 std::string* closedname)
4942 const Token* token = this->peek_token();
4943 bool saw_comma = false;
4944 bool closed_is_id = false;
4945 if (token->is_identifier())
4947 Gogo* gogo = this->gogo_;
4948 std::string recv_var = token->identifier();
4949 bool is_rv_exported = token->is_identifier_exported();
4950 Location recv_var_loc = token->location();
4951 token = this->advance_token();
4952 if (token->is_op(OPERATOR_COLONEQ))
4954 // case rv := <-c:
4955 this->advance_token();
4956 Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
4957 NULL, NULL);
4958 Receive_expression* re = e->receive_expression();
4959 if (re == NULL)
4961 if (!e->is_error_expression())
4962 error_at(this->location(), "expected receive expression");
4963 return false;
4965 if (recv_var == "_")
4967 error_at(recv_var_loc,
4968 "no new variables on left side of %<:=%>");
4969 recv_var = Gogo::erroneous_name();
4971 *is_send = false;
4972 *varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
4973 *channel = re->channel();
4974 return true;
4976 else if (token->is_op(OPERATOR_COMMA))
4978 token = this->advance_token();
4979 if (token->is_identifier())
4981 std::string recv_closed = token->identifier();
4982 bool is_rc_exported = token->is_identifier_exported();
4983 Location recv_closed_loc = token->location();
4984 closed_is_id = true;
4986 token = this->advance_token();
4987 if (token->is_op(OPERATOR_COLONEQ))
4989 // case rv, rc := <-c:
4990 this->advance_token();
4991 Expression* e = this->expression(PRECEDENCE_NORMAL, false,
4992 false, NULL, NULL);
4993 Receive_expression* re = e->receive_expression();
4994 if (re == NULL)
4996 if (!e->is_error_expression())
4997 error_at(this->location(),
4998 "expected receive expression");
4999 return false;
5001 if (recv_var == "_" && recv_closed == "_")
5003 error_at(recv_var_loc,
5004 "no new variables on left side of %<:=%>");
5005 recv_var = Gogo::erroneous_name();
5007 *is_send = false;
5008 if (recv_var != "_")
5009 *varname = gogo->pack_hidden_name(recv_var,
5010 is_rv_exported);
5011 if (recv_closed != "_")
5012 *closedname = gogo->pack_hidden_name(recv_closed,
5013 is_rc_exported);
5014 *channel = re->channel();
5015 return true;
5018 this->unget_token(Token::make_identifier_token(recv_closed,
5019 is_rc_exported,
5020 recv_closed_loc));
5023 *val = this->id_to_expression(gogo->pack_hidden_name(recv_var,
5024 is_rv_exported),
5025 recv_var_loc);
5026 saw_comma = true;
5028 else
5029 this->unget_token(Token::make_identifier_token(recv_var,
5030 is_rv_exported,
5031 recv_var_loc));
5034 // If SAW_COMMA is false, then we are looking at the start of the
5035 // send or receive expression. If SAW_COMMA is true, then *VAL is
5036 // set and we just read a comma.
5038 Expression* e;
5039 if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP))
5040 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL);
5041 else
5043 // case <-c:
5044 *is_send = false;
5045 this->advance_token();
5046 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5048 // The next token should be ':'. If it is '<-', then we have
5049 // case <-c <- v:
5050 // which is to say, send on a channel received from a channel.
5051 if (!this->peek_token()->is_op(OPERATOR_CHANOP))
5052 return true;
5054 e = Expression::make_receive(*channel, (*channel)->location());
5057 if (this->peek_token()->is_op(OPERATOR_EQ))
5059 if (!this->advance_token()->is_op(OPERATOR_CHANOP))
5061 error_at(this->location(), "missing %<<-%>");
5062 return false;
5064 *is_send = false;
5065 this->advance_token();
5066 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5067 if (saw_comma)
5069 // case v, e = <-c:
5070 // *VAL is already set.
5071 if (!e->is_sink_expression())
5072 *closed = e;
5074 else
5076 // case v = <-c:
5077 if (!e->is_sink_expression())
5078 *val = e;
5080 return true;
5083 if (saw_comma)
5085 if (closed_is_id)
5086 error_at(this->location(), "expected %<=%> or %<:=%>");
5087 else
5088 error_at(this->location(), "expected %<=%>");
5089 return false;
5092 if (this->peek_token()->is_op(OPERATOR_CHANOP))
5094 // case c <- v:
5095 *is_send = true;
5096 *channel = this->verify_not_sink(e);
5097 this->advance_token();
5098 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5099 return true;
5102 error_at(this->location(), "expected %<<-%> or %<=%>");
5103 return false;
5106 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5107 // Condition = Expression .
5109 void
5110 Parse::for_stat(Label* label)
5112 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
5113 Location location = this->location();
5114 const Token* token = this->advance_token();
5116 // Open a block to hold any variables defined in the init statement
5117 // of the for statement.
5118 this->gogo_->start_block(location);
5120 Block* init = NULL;
5121 Expression* cond = NULL;
5122 Block* post = NULL;
5123 Range_clause range_clause;
5125 if (!token->is_op(OPERATOR_LCURLY))
5127 if (token->is_keyword(KEYWORD_VAR))
5129 error_at(this->location(),
5130 "var declaration not allowed in for initializer");
5131 this->var_decl();
5134 if (token->is_op(OPERATOR_SEMICOLON))
5135 this->for_clause(&cond, &post);
5136 else
5138 // We might be looking at a Condition, an InitStat, or a
5139 // RangeClause.
5140 bool saw_send_stmt;
5141 cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL);
5142 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5144 if (cond == NULL && !range_clause.found)
5146 if (saw_send_stmt)
5147 error_at(this->location(),
5148 ("send statement used as value; "
5149 "use select for non-blocking send"));
5150 else
5151 error_at(this->location(), "parse error in for statement");
5154 else
5156 if (range_clause.found)
5157 error_at(this->location(), "parse error after range clause");
5159 if (cond != NULL)
5161 // COND is actually an expression statement for
5162 // InitStat at the start of a ForClause.
5163 this->expression_stat(cond);
5164 cond = NULL;
5167 this->for_clause(&cond, &post);
5172 // Check for the easy error of a newline before starting the block.
5173 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5175 Location semi_loc = this->location();
5176 if (this->advance_token()->is_op(OPERATOR_LCURLY))
5177 error_at(semi_loc, "missing %<{%> after for clause");
5178 // Otherwise we will get an error when we call this->block
5179 // below.
5182 // Build the For_statement and note that it is the current target
5183 // for break and continue statements.
5185 For_statement* sfor;
5186 For_range_statement* srange;
5187 Statement* s;
5188 if (!range_clause.found)
5190 sfor = Statement::make_for_statement(init, cond, post, location);
5191 s = sfor;
5192 srange = NULL;
5194 else
5196 srange = Statement::make_for_range_statement(range_clause.index,
5197 range_clause.value,
5198 range_clause.range,
5199 location);
5200 s = srange;
5201 sfor = NULL;
5204 this->push_break_statement(s, label);
5205 this->push_continue_statement(s, label);
5207 // Gather the block of statements in the loop and add them to the
5208 // For_statement.
5210 this->gogo_->start_block(this->location());
5211 Location end_loc = this->block();
5212 Block* statements = this->gogo_->finish_block(end_loc);
5214 if (sfor != NULL)
5215 sfor->add_statements(statements);
5216 else
5217 srange->add_statements(statements);
5219 // This is no longer the break/continue target.
5220 this->pop_break_statement();
5221 this->pop_continue_statement();
5223 // Add the For_statement to the list of statements, and close out
5224 // the block we started to hold any variables defined in the for
5225 // statement.
5227 this->gogo_->add_statement(s);
5229 this->gogo_->add_block(this->gogo_->finish_block(this->location()),
5230 location);
5233 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5234 // InitStat = SimpleStat .
5235 // PostStat = SimpleStat .
5237 // We have already read InitStat at this point.
5239 void
5240 Parse::for_clause(Expression** cond, Block** post)
5242 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON));
5243 this->advance_token();
5244 if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
5245 *cond = NULL;
5246 else if (this->peek_token()->is_op(OPERATOR_LCURLY))
5248 error_at(this->location(), "missing %<{%> after for clause");
5249 *cond = NULL;
5250 *post = NULL;
5251 return;
5253 else
5254 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL);
5255 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON))
5256 error_at(this->location(), "expected semicolon");
5257 else
5258 this->advance_token();
5260 if (this->peek_token()->is_op(OPERATOR_LCURLY))
5261 *post = NULL;
5262 else
5264 this->gogo_->start_block(this->location());
5265 this->simple_stat(false, NULL, NULL, NULL);
5266 *post = this->gogo_->finish_block(this->location());
5270 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
5272 // This is the := version. It is called with a list of identifiers.
5274 void
5275 Parse::range_clause_decl(const Typed_identifier_list* til,
5276 Range_clause* p_range_clause)
5278 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5279 Location location = this->location();
5281 p_range_clause->found = true;
5283 go_assert(til->size() >= 1);
5284 if (til->size() > 2)
5285 error_at(this->location(), "too many variables for range clause");
5287 this->advance_token();
5288 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL,
5289 NULL);
5290 p_range_clause->range = expr;
5292 bool any_new = false;
5294 const Typed_identifier* pti = &til->front();
5295 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new,
5296 NULL, NULL);
5297 if (any_new && no->is_variable())
5298 no->var_value()->set_type_from_range_index();
5299 p_range_clause->index = Expression::make_var_reference(no, location);
5301 if (til->size() == 1)
5302 p_range_clause->value = NULL;
5303 else
5305 pti = &til->back();
5306 bool is_new = false;
5307 no = this->init_var(*pti, NULL, expr, true, true, &is_new, NULL, NULL);
5308 if (is_new && no->is_variable())
5309 no->var_value()->set_type_from_range_value();
5310 if (is_new)
5311 any_new = true;
5312 if (!Gogo::is_sink_name(pti->name()))
5313 p_range_clause->value = Expression::make_var_reference(no, location);
5316 if (!any_new)
5317 error_at(location, "variables redeclared but no variable is new");
5320 // The = version of RangeClause. This is called with a list of
5321 // expressions.
5323 void
5324 Parse::range_clause_expr(const Expression_list* vals,
5325 Range_clause* p_range_clause)
5327 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
5329 p_range_clause->found = true;
5331 go_assert(vals->size() >= 1);
5332 if (vals->size() > 2)
5333 error_at(this->location(), "too many variables for range clause");
5335 this->advance_token();
5336 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false,
5337 NULL, NULL);
5339 p_range_clause->index = vals->front();
5340 if (vals->size() == 1)
5341 p_range_clause->value = NULL;
5342 else
5343 p_range_clause->value = vals->back();
5346 // Push a statement on the break stack.
5348 void
5349 Parse::push_break_statement(Statement* enclosing, Label* label)
5351 if (this->break_stack_ == NULL)
5352 this->break_stack_ = new Bc_stack();
5353 this->break_stack_->push_back(std::make_pair(enclosing, label));
5356 // Push a statement on the continue stack.
5358 void
5359 Parse::push_continue_statement(Statement* enclosing, Label* label)
5361 if (this->continue_stack_ == NULL)
5362 this->continue_stack_ = new Bc_stack();
5363 this->continue_stack_->push_back(std::make_pair(enclosing, label));
5366 // Pop the break stack.
5368 void
5369 Parse::pop_break_statement()
5371 this->break_stack_->pop_back();
5374 // Pop the continue stack.
5376 void
5377 Parse::pop_continue_statement()
5379 this->continue_stack_->pop_back();
5382 // Find a break or continue statement given a label name.
5384 Statement*
5385 Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label)
5387 if (bc_stack == NULL)
5388 return NULL;
5389 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin();
5390 p != bc_stack->rend();
5391 ++p)
5393 if (p->second != NULL && p->second->name() == label)
5395 p->second->set_is_used();
5396 return p->first;
5399 return NULL;
5402 // BreakStat = "break" [ identifier ] .
5404 void
5405 Parse::break_stat()
5407 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
5408 Location location = this->location();
5410 const Token* token = this->advance_token();
5411 Statement* enclosing;
5412 if (!token->is_identifier())
5414 if (this->break_stack_ == NULL || this->break_stack_->empty())
5416 error_at(this->location(),
5417 "break statement not within for or switch or select");
5418 return;
5420 enclosing = this->break_stack_->back().first;
5422 else
5424 enclosing = this->find_bc_statement(this->break_stack_,
5425 token->identifier());
5426 if (enclosing == NULL)
5428 // If there is a label with this name, mark it as used to
5429 // avoid a useless error about an unused label.
5430 this->gogo_->add_label_reference(token->identifier(),
5431 Linemap::unknown_location(), false);
5433 error_at(token->location(), "invalid break label %qs",
5434 Gogo::message_name(token->identifier()).c_str());
5435 this->advance_token();
5436 return;
5438 this->advance_token();
5441 Unnamed_label* label;
5442 if (enclosing->classification() == Statement::STATEMENT_FOR)
5443 label = enclosing->for_statement()->break_label();
5444 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5445 label = enclosing->for_range_statement()->break_label();
5446 else if (enclosing->classification() == Statement::STATEMENT_SWITCH)
5447 label = enclosing->switch_statement()->break_label();
5448 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH)
5449 label = enclosing->type_switch_statement()->break_label();
5450 else if (enclosing->classification() == Statement::STATEMENT_SELECT)
5451 label = enclosing->select_statement()->break_label();
5452 else
5453 go_unreachable();
5455 this->gogo_->add_statement(Statement::make_break_statement(label,
5456 location));
5459 // ContinueStat = "continue" [ identifier ] .
5461 void
5462 Parse::continue_stat()
5464 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
5465 Location location = this->location();
5467 const Token* token = this->advance_token();
5468 Statement* enclosing;
5469 if (!token->is_identifier())
5471 if (this->continue_stack_ == NULL || this->continue_stack_->empty())
5473 error_at(this->location(), "continue statement not within for");
5474 return;
5476 enclosing = this->continue_stack_->back().first;
5478 else
5480 enclosing = this->find_bc_statement(this->continue_stack_,
5481 token->identifier());
5482 if (enclosing == NULL)
5484 // If there is a label with this name, mark it as used to
5485 // avoid a useless error about an unused label.
5486 this->gogo_->add_label_reference(token->identifier(),
5487 Linemap::unknown_location(), false);
5489 error_at(token->location(), "invalid continue label %qs",
5490 Gogo::message_name(token->identifier()).c_str());
5491 this->advance_token();
5492 return;
5494 this->advance_token();
5497 Unnamed_label* label;
5498 if (enclosing->classification() == Statement::STATEMENT_FOR)
5499 label = enclosing->for_statement()->continue_label();
5500 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
5501 label = enclosing->for_range_statement()->continue_label();
5502 else
5503 go_unreachable();
5505 this->gogo_->add_statement(Statement::make_continue_statement(label,
5506 location));
5509 // GotoStat = "goto" identifier .
5511 void
5512 Parse::goto_stat()
5514 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
5515 Location location = this->location();
5516 const Token* token = this->advance_token();
5517 if (!token->is_identifier())
5518 error_at(this->location(), "expected label for goto");
5519 else
5521 Label* label = this->gogo_->add_label_reference(token->identifier(),
5522 location, true);
5523 Statement* s = Statement::make_goto_statement(label, location);
5524 this->gogo_->add_statement(s);
5525 this->advance_token();
5529 // PackageClause = "package" PackageName .
5531 void
5532 Parse::package_clause()
5534 const Token* token = this->peek_token();
5535 Location location = token->location();
5536 std::string name;
5537 if (!token->is_keyword(KEYWORD_PACKAGE))
5539 error_at(this->location(), "program must start with package clause");
5540 name = "ERROR";
5542 else
5544 token = this->advance_token();
5545 if (token->is_identifier())
5547 name = token->identifier();
5548 if (name == "_")
5550 error_at(this->location(), "invalid package name _");
5551 name = Gogo::erroneous_name();
5553 this->advance_token();
5555 else
5557 error_at(this->location(), "package name must be an identifier");
5558 name = "ERROR";
5561 this->gogo_->set_package_name(name, location);
5564 // ImportDecl = "import" Decl<ImportSpec> .
5566 void
5567 Parse::import_decl()
5569 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT));
5570 this->advance_token();
5571 this->decl(&Parse::import_spec, NULL);
5574 // ImportSpec = [ "." | PackageName ] PackageFileName .
5576 void
5577 Parse::import_spec(void*)
5579 const Token* token = this->peek_token();
5580 Location location = token->location();
5582 std::string local_name;
5583 bool is_local_name_exported = false;
5584 if (token->is_op(OPERATOR_DOT))
5586 local_name = ".";
5587 token = this->advance_token();
5589 else if (token->is_identifier())
5591 local_name = token->identifier();
5592 is_local_name_exported = token->is_identifier_exported();
5593 token = this->advance_token();
5596 if (!token->is_string())
5598 error_at(this->location(), "import statement not a string");
5599 this->advance_token();
5600 return;
5603 this->gogo_->import_package(token->string_value(), local_name,
5604 is_local_name_exported, location);
5606 this->advance_token();
5609 // SourceFile = PackageClause ";" { ImportDecl ";" }
5610 // { TopLevelDecl ";" } .
5612 void
5613 Parse::program()
5615 this->package_clause();
5617 const Token* token = this->peek_token();
5618 if (token->is_op(OPERATOR_SEMICOLON))
5619 token = this->advance_token();
5620 else
5621 error_at(this->location(),
5622 "expected %<;%> or newline after package clause");
5624 while (token->is_keyword(KEYWORD_IMPORT))
5626 this->import_decl();
5627 token = this->peek_token();
5628 if (token->is_op(OPERATOR_SEMICOLON))
5629 token = this->advance_token();
5630 else
5631 error_at(this->location(),
5632 "expected %<;%> or newline after import declaration");
5635 while (!token->is_eof())
5637 if (this->declaration_may_start_here())
5638 this->declaration();
5639 else
5641 error_at(this->location(), "expected declaration");
5642 this->gogo_->mark_locals_used();
5644 this->advance_token();
5645 while (!this->peek_token()->is_eof()
5646 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)
5647 && !this->peek_token()->is_op(OPERATOR_RCURLY));
5648 if (!this->peek_token()->is_eof()
5649 && !this->peek_token()->is_op(OPERATOR_SEMICOLON))
5650 this->advance_token();
5652 token = this->peek_token();
5653 if (token->is_op(OPERATOR_SEMICOLON))
5654 token = this->advance_token();
5655 else if (!token->is_eof() || !saw_errors())
5657 if (token->is_op(OPERATOR_CHANOP))
5658 error_at(this->location(),
5659 ("send statement used as value; "
5660 "use select for non-blocking send"));
5661 else
5662 error_at(this->location(),
5663 "expected %<;%> or newline after top level declaration");
5664 this->skip_past_error(OPERATOR_INVALID);
5669 // Reset the current iota value.
5671 void
5672 Parse::reset_iota()
5674 this->iota_ = 0;
5677 // Return the current iota value.
5680 Parse::iota_value()
5682 return this->iota_;
5685 // Increment the current iota value.
5687 void
5688 Parse::increment_iota()
5690 ++this->iota_;
5693 // Skip forward to a semicolon or OP. OP will normally be
5694 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5695 // past it and return. If we find OP, it will be the next token to
5696 // read. Return true if we are OK, false if we found EOF.
5698 bool
5699 Parse::skip_past_error(Operator op)
5701 this->gogo_->mark_locals_used();
5702 const Token* token = this->peek_token();
5703 while (!token->is_op(op))
5705 if (token->is_eof())
5706 return false;
5707 if (token->is_op(OPERATOR_SEMICOLON))
5709 this->advance_token();
5710 return true;
5712 token = this->advance_token();
5714 return true;
5717 // Check that an expression is not a sink.
5719 Expression*
5720 Parse::verify_not_sink(Expression* expr)
5722 if (expr->is_sink_expression())
5724 error_at(expr->location(), "cannot use _ as value");
5725 expr = Expression::make_error(expr->location());
5727 return expr;
5730 // Mark a variable as used.
5732 void
5733 Parse::mark_var_used(Named_object* no)
5735 if (no->is_variable())
5737 no->var_value()->set_is_used();
5739 // When a type switch uses := to define a variable, then for
5740 // each case with a single type we introduce a new variable with
5741 // the appropriate type. When we do, if the newly introduced
5742 // variable is used, then the type switch variable is used.
5743 Type_switch_vars::iterator p = this->type_switch_vars_.find(no);
5744 if (p != this->type_switch_vars_.end())
5745 p->second->var_value()->set_is_used();