1 // parse.cc -- Go frontend parser.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 #include "statements.h"
13 #include "expressions.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var
& v1
,
22 const Enclosing_var
& v2
)
24 if (v1
.var() == v2
.var())
27 const std::string
& n1(v1
.var()->name());
28 const std::string
& n2(v2
.var()->name());
29 int i
= n1
.compare(n2
);
35 // If we get here it means that a single nested function refers to
36 // two different variables defined in enclosing functions, and both
37 // variables have the same name. I think this is impossible.
43 Parse::Parse(Lex
* lex
, Gogo
* gogo
)
45 token_(Token::make_invalid_token(Linemap::unknown_location())),
46 unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
47 unget_token_valid_(false),
48 is_erroneous_function_(false),
51 continue_stack_(NULL
),
58 // Return the current token.
63 if (this->unget_token_valid_
)
64 return &this->unget_token_
;
65 if (this->token_
.is_invalid())
66 this->token_
= this->lex_
->next_token();
70 // Advance to the next token and return it.
73 Parse::advance_token()
75 if (this->unget_token_valid_
)
77 this->unget_token_valid_
= false;
78 if (!this->token_
.is_invalid())
81 this->token_
= this->lex_
->next_token();
85 // Push a token back on the input stream.
88 Parse::unget_token(const Token
& token
)
90 go_assert(!this->unget_token_valid_
);
91 this->unget_token_
= token
;
92 this->unget_token_valid_
= true;
95 // The location of the current token.
100 return this->peek_token()->location();
103 // IdentifierList = identifier { "," identifier } .
106 Parse::identifier_list(Typed_identifier_list
* til
)
108 const Token
* token
= this->peek_token();
111 if (!token
->is_identifier())
113 error_at(this->location(), "expected identifier");
117 this->gogo_
->pack_hidden_name(token
->identifier(),
118 token
->is_identifier_exported());
119 til
->push_back(Typed_identifier(name
, NULL
, token
->location()));
120 token
= this->advance_token();
121 if (!token
->is_op(OPERATOR_COMMA
))
123 token
= this->advance_token();
127 // ExpressionList = Expression { "," Expression } .
129 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
132 // If MAY_BE_SINK is true, the expressions in the list may be "_".
135 Parse::expression_list(Expression
* first
, bool may_be_sink
,
136 bool may_be_composite_lit
)
138 Expression_list
* ret
= new Expression_list();
140 ret
->push_back(first
);
143 ret
->push_back(this->expression(PRECEDENCE_NORMAL
, may_be_sink
,
144 may_be_composite_lit
, NULL
, NULL
));
146 const Token
* token
= this->peek_token();
147 if (!token
->is_op(OPERATOR_COMMA
))
150 // Most expression lists permit a trailing comma.
151 Location location
= token
->location();
152 this->advance_token();
153 if (!this->expression_may_start_here())
155 this->unget_token(Token::make_operator_token(OPERATOR_COMMA
,
162 // QualifiedIdent = [ PackageName "." ] identifier .
163 // PackageName = identifier .
165 // This sets *PNAME to the identifier and sets *PPACKAGE to the
166 // package or NULL if there isn't one. This returns true on success,
167 // false on failure in which case it will have emitted an error
171 Parse::qualified_ident(std::string
* pname
, Named_object
** ppackage
)
173 const Token
* token
= this->peek_token();
174 if (!token
->is_identifier())
176 error_at(this->location(), "expected identifier");
180 std::string name
= token
->identifier();
181 bool is_exported
= token
->is_identifier_exported();
182 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
184 token
= this->advance_token();
185 if (!token
->is_op(OPERATOR_DOT
))
192 Named_object
* package
= this->gogo_
->lookup(name
, NULL
);
193 if (package
== NULL
|| !package
->is_package())
195 error_at(this->location(), "expected package");
196 // We expect . IDENTIFIER; skip both.
197 if (this->advance_token()->is_identifier())
198 this->advance_token();
202 package
->package_value()->set_used();
204 token
= this->advance_token();
205 if (!token
->is_identifier())
207 error_at(this->location(), "expected identifier");
211 name
= token
->identifier();
215 error_at(this->location(), "invalid use of %<_%>");
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());
226 this->advance_token();
231 // Type = TypeName | TypeLit | "(" Type ")" .
233 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
234 // SliceType | MapType | ChannelType .
239 const Token
* token
= this->peek_token();
240 if (token
->is_identifier())
241 return this->type_name(true);
242 else if (token
->is_op(OPERATOR_LSQUARE
))
243 return this->array_type(false);
244 else if (token
->is_keyword(KEYWORD_CHAN
)
245 || token
->is_op(OPERATOR_CHANOP
))
246 return this->channel_type();
247 else if (token
->is_keyword(KEYWORD_INTERFACE
))
248 return this->interface_type(true);
249 else if (token
->is_keyword(KEYWORD_FUNC
))
251 Location location
= token
->location();
252 this->advance_token();
253 Type
* type
= this->signature(NULL
, location
);
255 return Type::make_error_type();
258 else if (token
->is_keyword(KEYWORD_MAP
))
259 return this->map_type();
260 else if (token
->is_keyword(KEYWORD_STRUCT
))
261 return this->struct_type();
262 else if (token
->is_op(OPERATOR_MULT
))
263 return this->pointer_type();
264 else if (token
->is_op(OPERATOR_LPAREN
))
266 this->advance_token();
267 Type
* ret
= this->type();
268 if (this->peek_token()->is_op(OPERATOR_RPAREN
))
269 this->advance_token();
272 if (!ret
->is_error_type())
273 error_at(this->location(), "expected %<)%>");
279 error_at(token
->location(), "expected type");
280 return Type::make_error_type();
285 Parse::type_may_start_here()
287 const Token
* token
= this->peek_token();
288 return (token
->is_identifier()
289 || token
->is_op(OPERATOR_LSQUARE
)
290 || token
->is_op(OPERATOR_CHANOP
)
291 || token
->is_keyword(KEYWORD_CHAN
)
292 || token
->is_keyword(KEYWORD_INTERFACE
)
293 || token
->is_keyword(KEYWORD_FUNC
)
294 || token
->is_keyword(KEYWORD_MAP
)
295 || token
->is_keyword(KEYWORD_STRUCT
)
296 || token
->is_op(OPERATOR_MULT
)
297 || token
->is_op(OPERATOR_LPAREN
));
300 // TypeName = QualifiedIdent .
302 // If MAY_BE_NIL is true, then an identifier with the value of the
303 // predefined constant nil is accepted, returning the nil type.
306 Parse::type_name(bool issue_error
)
308 Location location
= this->location();
311 Named_object
* package
;
312 if (!this->qualified_ident(&name
, &package
))
313 return Type::make_error_type();
315 Named_object
* named_object
;
317 named_object
= this->gogo_
->lookup(name
, NULL
);
320 named_object
= package
->package_value()->lookup(name
);
321 if (named_object
== NULL
323 && package
->name() != this->gogo_
->package_name())
325 // Check whether the name is there but hidden.
326 std::string s
= ('.' + package
->package_value()->pkgpath()
328 named_object
= package
->package_value()->lookup(s
);
329 if (named_object
!= NULL
)
331 Package
* p
= package
->package_value();
332 const std::string
& packname(p
->package_name());
333 error_at(location
, "invalid reference to hidden type %<%s.%s%>",
334 Gogo::message_name(packname
).c_str(),
335 Gogo::message_name(name
).c_str());
342 if (named_object
== NULL
)
345 named_object
= this->gogo_
->add_unknown_name(name
, location
);
348 const std::string
& packname(package
->package_value()->package_name());
349 error_at(location
, "reference to undefined identifier %<%s.%s%>",
350 Gogo::message_name(packname
).c_str(),
351 Gogo::message_name(name
).c_str());
356 else if (named_object
->is_type())
358 if (!named_object
->type_value()->is_visible())
361 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
369 error_at(location
, "expected type");
370 return Type::make_error_type();
373 if (named_object
->is_type())
374 return named_object
->type_value();
375 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
376 return Type::make_forward_declaration(named_object
);
381 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
382 // ArrayLength = Expression .
383 // ElementType = CompleteType .
386 Parse::array_type(bool may_use_ellipsis
)
388 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
389 const Token
* token
= this->advance_token();
391 Expression
* length
= NULL
;
392 if (token
->is_op(OPERATOR_RSQUARE
))
393 this->advance_token();
396 if (!token
->is_op(OPERATOR_ELLIPSIS
))
397 length
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
398 else if (may_use_ellipsis
)
400 // An ellipsis is used in composite literals to represent a
401 // fixed array of the size of the number of elements. We
402 // use a length of nil to represent this, and change the
403 // length when parsing the composite literal.
404 length
= Expression::make_nil(this->location());
405 this->advance_token();
409 error_at(this->location(),
410 "use of %<[...]%> outside of array literal");
411 length
= Expression::make_error(this->location());
412 this->advance_token();
414 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
416 error_at(this->location(), "expected %<]%>");
417 return Type::make_error_type();
419 this->advance_token();
422 Type
* element_type
= this->type();
424 return Type::make_array_type(element_type
, length
);
427 // MapType = "map" "[" KeyType "]" ValueType .
428 // KeyType = CompleteType .
429 // ValueType = CompleteType .
434 Location location
= this->location();
435 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP
));
436 if (!this->advance_token()->is_op(OPERATOR_LSQUARE
))
438 error_at(this->location(), "expected %<[%>");
439 return Type::make_error_type();
441 this->advance_token();
443 Type
* key_type
= this->type();
445 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
447 error_at(this->location(), "expected %<]%>");
448 return Type::make_error_type();
450 this->advance_token();
452 Type
* value_type
= this->type();
454 if (key_type
->is_error_type() || value_type
->is_error_type())
455 return Type::make_error_type();
457 return Type::make_map_type(key_type
, value_type
, location
);
460 // StructType = "struct" "{" { FieldDecl ";" } "}" .
465 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT
));
466 Location location
= this->location();
467 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
469 Location token_loc
= this->location();
470 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
471 && this->advance_token()->is_op(OPERATOR_LCURLY
))
472 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
475 error_at(this->location(), "expected %<{%>");
476 return Type::make_error_type();
479 this->advance_token();
481 Struct_field_list
* sfl
= new Struct_field_list
;
482 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
484 this->field_decl(sfl
);
485 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
486 this->advance_token();
487 else if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
489 error_at(this->location(), "expected %<;%> or %<}%> or newline");
490 if (!this->skip_past_error(OPERATOR_RCURLY
))
491 return Type::make_error_type();
494 this->advance_token();
496 for (Struct_field_list::const_iterator pi
= sfl
->begin();
500 if (pi
->type()->is_error_type())
502 for (Struct_field_list::const_iterator pj
= pi
+ 1;
506 if (pi
->field_name() == pj
->field_name()
507 && !Gogo::is_sink_name(pi
->field_name()))
508 error_at(pi
->location(), "duplicate field name %<%s%>",
509 Gogo::message_name(pi
->field_name()).c_str());
513 return Type::make_struct_type(sfl
, location
);
516 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
517 // Tag = string_lit .
520 Parse::field_decl(Struct_field_list
* sfl
)
522 const Token
* token
= this->peek_token();
523 Location location
= token
->location();
525 bool is_anonymous_pointer
;
526 if (token
->is_op(OPERATOR_MULT
))
529 is_anonymous_pointer
= true;
531 else if (token
->is_identifier())
533 std::string id
= token
->identifier();
534 bool is_id_exported
= token
->is_identifier_exported();
535 Location id_location
= token
->location();
536 token
= this->advance_token();
537 is_anonymous
= (token
->is_op(OPERATOR_SEMICOLON
)
538 || token
->is_op(OPERATOR_RCURLY
)
539 || token
->is_op(OPERATOR_DOT
)
540 || token
->is_string());
541 is_anonymous_pointer
= false;
542 this->unget_token(Token::make_identifier_token(id
, is_id_exported
,
547 error_at(this->location(), "expected field name");
548 this->gogo_
->mark_locals_used();
549 while (!token
->is_op(OPERATOR_SEMICOLON
)
550 && !token
->is_op(OPERATOR_RCURLY
)
552 token
= this->advance_token();
558 if (is_anonymous_pointer
)
560 this->advance_token();
561 if (!this->peek_token()->is_identifier())
563 error_at(this->location(), "expected field name");
564 this->gogo_
->mark_locals_used();
565 while (!token
->is_op(OPERATOR_SEMICOLON
)
566 && !token
->is_op(OPERATOR_RCURLY
)
568 token
= this->advance_token();
572 Type
* type
= this->type_name(true);
575 if (this->peek_token()->is_string())
577 tag
= this->peek_token()->string_value();
578 this->advance_token();
581 if (!type
->is_error_type())
583 if (is_anonymous_pointer
)
584 type
= Type::make_pointer_type(type
);
585 sfl
->push_back(Struct_field(Typed_identifier("", type
, location
)));
587 sfl
->back().set_tag(tag
);
592 Typed_identifier_list til
;
595 token
= this->peek_token();
596 if (!token
->is_identifier())
598 error_at(this->location(), "expected identifier");
602 this->gogo_
->pack_hidden_name(token
->identifier(),
603 token
->is_identifier_exported());
604 til
.push_back(Typed_identifier(name
, NULL
, token
->location()));
605 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
607 this->advance_token();
610 Type
* type
= this->type();
613 if (this->peek_token()->is_string())
615 tag
= this->peek_token()->string_value();
616 this->advance_token();
619 for (Typed_identifier_list::iterator p
= til
.begin();
624 sfl
->push_back(Struct_field(*p
));
626 sfl
->back().set_tag(tag
);
631 // PointerType = "*" Type .
634 Parse::pointer_type()
636 go_assert(this->peek_token()->is_op(OPERATOR_MULT
));
637 this->advance_token();
638 Type
* type
= this->type();
639 if (type
->is_error_type())
641 return Type::make_pointer_type(type
);
644 // ChannelType = Channel | SendChannel | RecvChannel .
645 // Channel = "chan" ElementType .
646 // SendChannel = "chan" "<-" ElementType .
647 // RecvChannel = "<-" "chan" ElementType .
650 Parse::channel_type()
652 const Token
* token
= this->peek_token();
655 if (token
->is_op(OPERATOR_CHANOP
))
657 if (!this->advance_token()->is_keyword(KEYWORD_CHAN
))
659 error_at(this->location(), "expected %<chan%>");
660 return Type::make_error_type();
663 this->advance_token();
667 go_assert(token
->is_keyword(KEYWORD_CHAN
));
668 if (this->advance_token()->is_op(OPERATOR_CHANOP
))
671 this->advance_token();
675 // Better error messages for the common error of omitting the
676 // channel element type.
677 if (!this->type_may_start_here())
679 token
= this->peek_token();
680 if (token
->is_op(OPERATOR_RCURLY
))
681 error_at(this->location(), "unexpected %<}%> in channel type");
682 else if (token
->is_op(OPERATOR_RPAREN
))
683 error_at(this->location(), "unexpected %<)%> in channel type");
684 else if (token
->is_op(OPERATOR_COMMA
))
685 error_at(this->location(), "unexpected comma in channel type");
687 error_at(this->location(), "expected channel element type");
688 return Type::make_error_type();
691 Type
* element_type
= this->type();
692 return Type::make_channel_type(send
, receive
, element_type
);
695 // Give an error for a duplicate parameter or receiver name.
698 Parse::check_signature_names(const Typed_identifier_list
* params
,
701 for (Typed_identifier_list::const_iterator p
= params
->begin();
705 if (p
->name().empty() || Gogo::is_sink_name(p
->name()))
707 std::pair
<std::string
, const Typed_identifier
*> val
=
708 std::make_pair(p
->name(), &*p
);
709 std::pair
<Parse::Names::iterator
, bool> ins
= names
->insert(val
);
712 error_at(p
->location(), "redefinition of %qs",
713 Gogo::message_name(p
->name()).c_str());
714 inform(ins
.first
->second
->location(),
715 "previous definition of %qs was here",
716 Gogo::message_name(p
->name()).c_str());
721 // Signature = Parameters [ Result ] .
723 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
724 // location of the start of the type.
726 // This returns NULL on a parse error.
729 Parse::signature(Typed_identifier
* receiver
, Location location
)
731 bool is_varargs
= false;
732 Typed_identifier_list
* params
;
733 bool params_ok
= this->parameters(¶ms
, &is_varargs
);
735 Typed_identifier_list
* results
= NULL
;
736 if (this->peek_token()->is_op(OPERATOR_LPAREN
)
737 || this->type_may_start_here())
739 if (!this->result(&results
))
747 if (receiver
!= NULL
)
748 names
[receiver
->name()] = receiver
;
750 this->check_signature_names(params
, &names
);
752 this->check_signature_names(results
, &names
);
754 Function_type
* ret
= Type::make_function_type(receiver
, params
, results
,
757 ret
->set_is_varargs();
761 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
763 // This returns false on a parse error.
766 Parse::parameters(Typed_identifier_list
** pparams
, bool* is_varargs
)
770 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
772 error_at(this->location(), "expected %<(%>");
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
);
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 %<)%>");
793 this->advance_token();
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;
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
843 parameters_have_names
= false;
845 else if (token
->is_op(OPERATOR_RPAREN
))
847 // A single identifier followed by a parenthesis must be
849 parameters_have_names
= false;
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
,
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
869 std::string id_name
= this->gogo_
->pack_hidden_name(name
,
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;
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
906 parameters_have_names
= false;
907 this->unget_token(Token::make_identifier_token(name
, is_exported
,
910 just_saw_comma
= true;
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.
925 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
929 error_at(this->location(), "%<...%> only permits one name");
931 this->advance_token();
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
;
943 Typed_identifier_list
* tret
= new Typed_identifier_list();
944 for (Typed_identifier_list::const_iterator p
= ret
->begin();
948 Named_object
* no
= this->gogo_
->lookup(p
->name(), NULL
);
951 no
= this->gogo_
->add_unknown_name(p
->name(),
955 type
= no
->type_value();
956 else if (no
->is_unknown() || no
->is_type_declaration())
957 type
= Type::make_forward_declaration(no
);
960 error_at(p
->location(), "expected %<%s%> to be a type",
961 Gogo::message_name(p
->name()).c_str());
963 type
= Type::make_error_type();
965 tret
->push_back(Typed_identifier("", type
, p
->location()));
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
,
979 while (this->peek_token()->is_op(OPERATOR_COMMA
))
981 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
983 if (is_varargs
!= NULL
&& *is_varargs
)
985 error_at(this->location(), "%<...%> must be last parameter");
988 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
993 error_at(location
, "invalid named/anonymous mix");
1004 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1007 Parse::parameter_decl(bool parameters_have_names
,
1008 Typed_identifier_list
* til
,
1013 if (!parameters_have_names
)
1016 Location location
= this->location();
1017 if (!this->peek_token()->is_identifier())
1019 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1020 type
= this->type();
1023 if (is_varargs
== NULL
)
1024 error_at(this->location(), "invalid use of %<...%>");
1027 this->advance_token();
1028 if (is_varargs
== NULL
1029 && this->peek_token()->is_op(OPERATOR_RPAREN
))
1030 type
= Type::make_error_type();
1033 Type
* element_type
= this->type();
1034 type
= Type::make_array_type(element_type
, NULL
);
1040 type
= this->type_name(false);
1041 if (type
->is_error_type()
1042 || (!this->peek_token()->is_op(OPERATOR_COMMA
)
1043 && !this->peek_token()->is_op(OPERATOR_RPAREN
)))
1046 while (!this->peek_token()->is_op(OPERATOR_COMMA
)
1047 && !this->peek_token()->is_op(OPERATOR_RPAREN
))
1048 this->advance_token();
1051 if (!type
->is_error_type())
1052 til
->push_back(Typed_identifier("", type
, location
));
1058 size_t orig_count
= til
->size();
1059 if (this->peek_token()->is_identifier())
1060 this->identifier_list(til
);
1063 size_t new_count
= til
->size();
1066 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1067 type
= this->type();
1070 if (is_varargs
== NULL
)
1072 error_at(this->location(), "invalid use of %<...%>");
1075 else if (new_count
> orig_count
+ 1)
1077 error_at(this->location(), "%<...%> only permits one name");
1082 this->advance_token();
1083 Type
* element_type
= this->type();
1084 type
= Type::make_array_type(element_type
, NULL
);
1086 for (size_t i
= orig_count
; i
< new_count
; ++i
)
1087 til
->set_type(i
, type
);
1091 // Result = Parameters | Type .
1093 // This returns false on a parse error.
1096 Parse::result(Typed_identifier_list
** presults
)
1098 if (this->peek_token()->is_op(OPERATOR_LPAREN
))
1099 return this->parameters(presults
, NULL
);
1102 Location location
= this->location();
1103 Type
* type
= this->type();
1104 if (type
->is_error_type())
1109 Typed_identifier_list
* til
= new Typed_identifier_list();
1110 til
->push_back(Typed_identifier("", type
, location
));
1116 // Block = "{" [ StatementList ] "}" .
1118 // Returns the location of the closing brace.
1123 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
1125 Location loc
= this->location();
1126 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1127 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1128 error_at(loc
, "unexpected semicolon or newline before %<{%>");
1131 error_at(this->location(), "expected %<{%>");
1132 return Linemap::unknown_location();
1136 const Token
* token
= this->advance_token();
1138 if (!token
->is_op(OPERATOR_RCURLY
))
1140 this->statement_list();
1141 token
= this->peek_token();
1142 if (!token
->is_op(OPERATOR_RCURLY
))
1144 if (!token
->is_eof() || !saw_errors())
1145 error_at(this->location(), "expected %<}%>");
1147 this->gogo_
->mark_locals_used();
1149 // Skip ahead to the end of the block, in hopes of avoiding
1150 // lots of meaningless errors.
1151 Location ret
= token
->location();
1153 while (!token
->is_eof())
1155 if (token
->is_op(OPERATOR_LCURLY
))
1157 else if (token
->is_op(OPERATOR_RCURLY
))
1162 this->advance_token();
1166 token
= this->advance_token();
1167 ret
= token
->location();
1173 Location ret
= token
->location();
1174 this->advance_token();
1178 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1179 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1182 Parse::interface_type(bool record
)
1184 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE
));
1185 Location location
= this->location();
1187 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
1189 Location token_loc
= this->location();
1190 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1191 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1192 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
1195 error_at(this->location(), "expected %<{%>");
1196 return Type::make_error_type();
1199 this->advance_token();
1201 Typed_identifier_list
* methods
= new Typed_identifier_list();
1202 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1204 this->method_spec(methods
);
1205 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1207 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
1209 this->method_spec(methods
);
1211 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1213 error_at(this->location(), "expected %<}%>");
1214 while (!this->advance_token()->is_op(OPERATOR_RCURLY
))
1216 if (this->peek_token()->is_eof())
1217 return Type::make_error_type();
1221 this->advance_token();
1223 if (methods
->empty())
1229 Interface_type
* ret
= Type::make_interface_type(methods
, location
);
1231 this->gogo_
->record_interface_type(ret
);
1235 // MethodSpec = MethodName Signature | InterfaceTypeName .
1236 // MethodName = identifier .
1237 // InterfaceTypeName = TypeName .
1240 Parse::method_spec(Typed_identifier_list
* methods
)
1242 const Token
* token
= this->peek_token();
1243 if (!token
->is_identifier())
1245 error_at(this->location(), "expected identifier");
1249 std::string name
= token
->identifier();
1250 bool is_exported
= token
->is_identifier_exported();
1251 Location location
= token
->location();
1253 if (this->advance_token()->is_op(OPERATOR_LPAREN
))
1255 // This is a MethodName.
1257 error_at(this->location(), "methods must have a unique non-blank name");
1258 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1259 Type
* type
= this->signature(NULL
, location
);
1262 methods
->push_back(Typed_identifier(name
, type
, location
));
1266 this->unget_token(Token::make_identifier_token(name
, is_exported
,
1268 Type
* type
= this->type_name(false);
1269 if (type
->is_error_type()
1270 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1271 && !this->peek_token()->is_op(OPERATOR_RCURLY
)))
1273 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1274 error_at(this->location(),
1275 "name list not allowed in interface type");
1277 error_at(location
, "expected signature or type name");
1278 this->gogo_
->mark_locals_used();
1279 token
= this->peek_token();
1280 while (!token
->is_eof()
1281 && !token
->is_op(OPERATOR_SEMICOLON
)
1282 && !token
->is_op(OPERATOR_RCURLY
))
1283 token
= this->advance_token();
1286 // This must be an interface type, but we can't check that now.
1287 // We check it and pull out the methods in
1288 // Interface_type::do_verify.
1289 methods
->push_back(Typed_identifier("", type
, location
));
1293 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1296 Parse::declaration()
1298 const Token
* token
= this->peek_token();
1300 bool saw_nointerface
= this->lex_
->get_and_clear_nointerface();
1301 if (saw_nointerface
&& !token
->is_keyword(KEYWORD_FUNC
))
1302 warning_at(token
->location(), 0,
1303 "ignoring magic //go:nointerface comment before non-method");
1305 if (token
->is_keyword(KEYWORD_CONST
))
1307 else if (token
->is_keyword(KEYWORD_TYPE
))
1309 else if (token
->is_keyword(KEYWORD_VAR
))
1311 else if (token
->is_keyword(KEYWORD_FUNC
))
1312 this->function_decl(saw_nointerface
);
1315 error_at(this->location(), "expected declaration");
1316 this->advance_token();
1321 Parse::declaration_may_start_here()
1323 const Token
* token
= this->peek_token();
1324 return (token
->is_keyword(KEYWORD_CONST
)
1325 || token
->is_keyword(KEYWORD_TYPE
)
1326 || token
->is_keyword(KEYWORD_VAR
)
1327 || token
->is_keyword(KEYWORD_FUNC
));
1330 // Decl<P> = P | "(" [ List<P> ] ")" .
1333 Parse::decl(void (Parse::*pfn
)(void*), void* varg
)
1335 if (this->peek_token()->is_eof())
1338 error_at(this->location(), "unexpected end of file");
1342 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1346 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1348 this->list(pfn
, varg
, true);
1349 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1351 error_at(this->location(), "missing %<)%>");
1352 while (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1354 if (this->peek_token()->is_eof())
1359 this->advance_token();
1363 // List<P> = P { ";" P } [ ";" ] .
1365 // In order to pick up the trailing semicolon we need to know what
1366 // might follow. This is either a '}' or a ')'.
1369 Parse::list(void (Parse::*pfn
)(void*), void* varg
, bool follow_is_paren
)
1372 Operator follow
= follow_is_paren
? OPERATOR_RPAREN
: OPERATOR_RCURLY
;
1373 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1374 || this->peek_token()->is_op(OPERATOR_COMMA
))
1376 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1377 error_at(this->location(), "unexpected comma");
1378 if (this->advance_token()->is_op(follow
))
1384 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1389 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST
));
1390 this->advance_token();
1393 Type
* last_type
= NULL
;
1394 Expression_list
* last_expr_list
= NULL
;
1396 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1397 this->const_spec(&last_type
, &last_expr_list
);
1400 this->advance_token();
1401 while (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1403 this->const_spec(&last_type
, &last_expr_list
);
1404 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1405 this->advance_token();
1406 else if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1408 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1409 if (!this->skip_past_error(OPERATOR_RPAREN
))
1413 this->advance_token();
1416 if (last_expr_list
!= NULL
)
1417 delete last_expr_list
;
1420 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1423 Parse::const_spec(Type
** last_type
, Expression_list
** last_expr_list
)
1425 Typed_identifier_list til
;
1426 this->identifier_list(&til
);
1429 if (this->type_may_start_here())
1431 type
= this->type();
1433 *last_expr_list
= NULL
;
1436 Expression_list
*expr_list
;
1437 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1439 if (*last_expr_list
== NULL
)
1441 error_at(this->location(), "expected %<=%>");
1445 expr_list
= new Expression_list
;
1446 for (Expression_list::const_iterator p
= (*last_expr_list
)->begin();
1447 p
!= (*last_expr_list
)->end();
1449 expr_list
->push_back((*p
)->copy());
1453 this->advance_token();
1454 expr_list
= this->expression_list(NULL
, false, true);
1456 if (*last_expr_list
!= NULL
)
1457 delete *last_expr_list
;
1458 *last_expr_list
= expr_list
;
1461 Expression_list::const_iterator pe
= expr_list
->begin();
1462 for (Typed_identifier_list::iterator pi
= til
.begin();
1466 if (pe
== expr_list
->end())
1468 error_at(this->location(), "not enough initializers");
1474 if (!Gogo::is_sink_name(pi
->name()))
1475 this->gogo_
->add_constant(*pi
, *pe
, this->iota_value());
1480 snprintf(buf
, sizeof buf
, ".$sinkconst%d", count
);
1482 Typed_identifier
ti(std::string(buf
), type
, pi
->location());
1483 Named_object
* no
= this->gogo_
->add_constant(ti
, *pe
, this->iota_value());
1484 no
->const_value()->set_is_sink();
1487 if (pe
!= expr_list
->end())
1488 error_at(this->location(), "too many initializers");
1490 this->increment_iota();
1495 // TypeDecl = "type" Decl<TypeSpec> .
1500 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE
));
1501 this->advance_token();
1502 this->decl(&Parse::type_spec
, NULL
);
1505 // TypeSpec = identifier Type .
1508 Parse::type_spec(void*)
1510 const Token
* token
= this->peek_token();
1511 if (!token
->is_identifier())
1513 error_at(this->location(), "expected identifier");
1516 std::string name
= token
->identifier();
1517 bool is_exported
= token
->is_identifier_exported();
1518 Location location
= token
->location();
1519 token
= this->advance_token();
1521 // The scope of the type name starts at the point where the
1522 // identifier appears in the source code. We implement this by
1523 // declaring the type before we read the type definition.
1524 Named_object
* named_type
= NULL
;
1527 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1528 named_type
= this->gogo_
->declare_type(name
, location
);
1532 if (name
== "_" && this->peek_token()->is_keyword(KEYWORD_INTERFACE
))
1534 // We call Parse::interface_type explicity here because we do not want
1535 // to record an interface with a blank type name.
1536 type
= this->interface_type(false);
1538 else if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1539 type
= this->type();
1542 error_at(this->location(),
1543 "unexpected semicolon or newline in type declaration");
1544 type
= Type::make_error_type();
1545 this->advance_token();
1548 if (type
->is_error_type())
1550 this->gogo_
->mark_locals_used();
1551 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1552 && !this->peek_token()->is_eof())
1553 this->advance_token();
1558 if (named_type
->is_type_declaration())
1560 Type
* ftype
= type
->forwarded();
1561 if (ftype
->forward_declaration_type() != NULL
1562 && (ftype
->forward_declaration_type()->named_object()
1565 error_at(location
, "invalid recursive type");
1566 type
= Type::make_error_type();
1569 this->gogo_
->define_type(named_type
,
1570 Type::make_named_type(named_type
, type
,
1572 go_assert(named_type
->package() == NULL
);
1576 // This will probably give a redefinition error.
1577 this->gogo_
->add_type(name
, type
, location
);
1582 // VarDecl = "var" Decl<VarSpec> .
1587 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR
));
1588 this->advance_token();
1589 this->decl(&Parse::var_spec
, NULL
);
1592 // VarSpec = IdentifierList
1593 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1596 Parse::var_spec(void*)
1598 // Get the variable names.
1599 Typed_identifier_list til
;
1600 this->identifier_list(&til
);
1602 Location location
= this->location();
1605 Expression_list
* init
= NULL
;
1606 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1608 type
= this->type();
1609 if (type
->is_error_type())
1611 this->gogo_
->mark_locals_used();
1612 while (!this->peek_token()->is_op(OPERATOR_EQ
)
1613 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1614 && !this->peek_token()->is_eof())
1615 this->advance_token();
1617 if (this->peek_token()->is_op(OPERATOR_EQ
))
1619 this->advance_token();
1620 init
= this->expression_list(NULL
, false, true);
1625 this->advance_token();
1626 init
= this->expression_list(NULL
, false, true);
1629 this->init_vars(&til
, type
, init
, false, location
);
1635 // Create variables. TIL is a list of variable names. If TYPE is not
1636 // NULL, it is the type of all the variables. If INIT is not NULL, it
1637 // is an initializer list for the variables.
1640 Parse::init_vars(const Typed_identifier_list
* til
, Type
* type
,
1641 Expression_list
* init
, bool is_coloneq
,
1644 // Check for an initialization which can yield multiple values.
1645 if (init
!= NULL
&& init
->size() == 1 && til
->size() > 1)
1647 if (this->init_vars_from_call(til
, type
, *init
->begin(), is_coloneq
,
1650 if (this->init_vars_from_map(til
, type
, *init
->begin(), is_coloneq
,
1653 if (this->init_vars_from_receive(til
, type
, *init
->begin(), is_coloneq
,
1656 if (this->init_vars_from_type_guard(til
, type
, *init
->begin(),
1657 is_coloneq
, location
))
1661 if (init
!= NULL
&& init
->size() != til
->size())
1663 if (init
->empty() || !init
->front()->is_error_expression())
1664 error_at(location
, "wrong number of initializations");
1667 type
= Type::make_error_type();
1670 // Note that INIT was already parsed with the old name bindings, so
1671 // we don't have to worry that it will accidentally refer to the
1672 // newly declared variables. But we do have to worry about a mix of
1673 // newly declared variables and old variables if the old variables
1674 // appear in the initializations.
1676 Expression_list::const_iterator pexpr
;
1678 pexpr
= init
->begin();
1679 bool any_new
= false;
1680 Expression_list
* vars
= new Expression_list();
1681 Expression_list
* vals
= new Expression_list();
1682 for (Typed_identifier_list::const_iterator p
= til
->begin();
1687 go_assert(pexpr
!= init
->end());
1688 this->init_var(*p
, type
, init
== NULL
? NULL
: *pexpr
, is_coloneq
,
1689 false, &any_new
, vars
, vals
);
1694 go_assert(pexpr
== init
->end());
1695 if (is_coloneq
&& !any_new
)
1696 error_at(location
, "variables redeclared but no variable is new");
1697 this->finish_init_vars(vars
, vals
, location
);
1700 // See if we need to initialize a list of variables from a function
1701 // call. This returns true if we have set up the variables and the
1705 Parse::init_vars_from_call(const Typed_identifier_list
* vars
, Type
* type
,
1706 Expression
* expr
, bool is_coloneq
,
1709 Call_expression
* call
= expr
->call_expression();
1713 // This is a function call. We can't check here whether it returns
1714 // the right number of values, but it might. Declare the variables,
1715 // and then assign the results of the call to them.
1717 call
->set_expected_result_count(vars
->size());
1719 Named_object
* first_var
= NULL
;
1720 unsigned int index
= 0;
1721 bool any_new
= false;
1722 Expression_list
* ivars
= new Expression_list();
1723 Expression_list
* ivals
= new Expression_list();
1724 for (Typed_identifier_list::const_iterator pv
= vars
->begin();
1728 Expression
* init
= Expression::make_call_result(call
, index
);
1729 Named_object
* no
= this->init_var(*pv
, type
, init
, is_coloneq
, false,
1730 &any_new
, ivars
, ivals
);
1732 if (this->gogo_
->in_global_scope() && no
->is_variable())
1734 if (first_var
== NULL
)
1738 // The subsequent vars have an implicit dependency on
1739 // the first one, so that everything gets initialized in
1740 // the right order and so that we detect cycles
1742 this->gogo_
->record_var_depends_on(no
->var_value(), first_var
);
1747 if (is_coloneq
&& !any_new
)
1748 error_at(location
, "variables redeclared but no variable is new");
1750 this->finish_init_vars(ivars
, ivals
, location
);
1755 // See if we need to initialize a pair of values from a map index
1756 // expression. This returns true if we have set up the variables and
1757 // the initialization.
1760 Parse::init_vars_from_map(const Typed_identifier_list
* vars
, Type
* type
,
1761 Expression
* expr
, bool is_coloneq
,
1764 Index_expression
* index
= expr
->index_expression();
1767 if (vars
->size() != 2)
1770 // This is an index which is being assigned to two variables. It
1771 // must be a map index. Declare the variables, and then assign the
1772 // results of the map index.
1773 bool any_new
= false;
1774 Typed_identifier_list::const_iterator p
= vars
->begin();
1775 Expression
* init
= type
== NULL
? index
: NULL
;
1776 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1777 type
== NULL
, &any_new
, NULL
, NULL
);
1778 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1779 val_no
->var_value()->set_type_from_init_tuple();
1780 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1783 Type
* var_type
= type
;
1784 if (var_type
== NULL
)
1785 var_type
= Type::lookup_bool_type();
1786 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1787 &any_new
, NULL
, NULL
);
1788 Expression
* present_var
= Expression::make_var_reference(no
, location
);
1790 if (is_coloneq
&& !any_new
)
1791 error_at(location
, "variables redeclared but no variable is new");
1793 Statement
* s
= Statement::make_tuple_map_assignment(val_var
, present_var
,
1796 if (!this->gogo_
->in_global_scope())
1797 this->gogo_
->add_statement(s
);
1798 else if (!val_no
->is_sink())
1800 if (val_no
->is_variable())
1801 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1803 else if (!no
->is_sink())
1805 if (no
->is_variable())
1806 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1810 // Execute the map index expression just so that we can fail if
1812 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1814 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1820 // See if we need to initialize a pair of values from a receive
1821 // expression. This returns true if we have set up the variables and
1822 // the initialization.
1825 Parse::init_vars_from_receive(const Typed_identifier_list
* vars
, Type
* type
,
1826 Expression
* expr
, bool is_coloneq
,
1829 Receive_expression
* receive
= expr
->receive_expression();
1830 if (receive
== NULL
)
1832 if (vars
->size() != 2)
1835 // This is a receive expression which is being assigned to two
1836 // variables. Declare the variables, and then assign the results of
1838 bool any_new
= false;
1839 Typed_identifier_list::const_iterator p
= vars
->begin();
1840 Expression
* init
= type
== NULL
? receive
: NULL
;
1841 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1842 type
== NULL
, &any_new
, NULL
, NULL
);
1843 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1844 val_no
->var_value()->set_type_from_init_tuple();
1845 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1848 Type
* var_type
= type
;
1849 if (var_type
== NULL
)
1850 var_type
= Type::lookup_bool_type();
1851 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1852 &any_new
, NULL
, NULL
);
1853 Expression
* received_var
= Expression::make_var_reference(no
, location
);
1855 if (is_coloneq
&& !any_new
)
1856 error_at(location
, "variables redeclared but no variable is new");
1858 Statement
* s
= Statement::make_tuple_receive_assignment(val_var
,
1863 if (!this->gogo_
->in_global_scope())
1864 this->gogo_
->add_statement(s
);
1865 else if (!val_no
->is_sink())
1867 if (val_no
->is_variable())
1868 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1870 else if (!no
->is_sink())
1872 if (no
->is_variable())
1873 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1877 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1879 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1885 // See if we need to initialize a pair of values from a type guard
1886 // expression. This returns true if we have set up the variables and
1887 // the initialization.
1890 Parse::init_vars_from_type_guard(const Typed_identifier_list
* vars
,
1891 Type
* type
, Expression
* expr
,
1892 bool is_coloneq
, Location location
)
1894 Type_guard_expression
* type_guard
= expr
->type_guard_expression();
1895 if (type_guard
== NULL
)
1897 if (vars
->size() != 2)
1900 // This is a type guard expression which is being assigned to two
1901 // variables. Declare the variables, and then assign the results of
1903 bool any_new
= false;
1904 Typed_identifier_list::const_iterator p
= vars
->begin();
1905 Type
* var_type
= type
;
1906 if (var_type
== NULL
)
1907 var_type
= type_guard
->type();
1908 Named_object
* val_no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1909 &any_new
, NULL
, NULL
);
1910 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1914 if (var_type
== NULL
)
1915 var_type
= Type::lookup_bool_type();
1916 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1917 &any_new
, NULL
, NULL
);
1918 Expression
* ok_var
= Expression::make_var_reference(no
, location
);
1920 Expression
* texpr
= type_guard
->expr();
1921 Type
* t
= type_guard
->type();
1922 Statement
* s
= Statement::make_tuple_type_guard_assignment(val_var
, ok_var
,
1926 if (is_coloneq
&& !any_new
)
1927 error_at(location
, "variables redeclared but no variable is new");
1929 if (!this->gogo_
->in_global_scope())
1930 this->gogo_
->add_statement(s
);
1931 else if (!val_no
->is_sink())
1933 if (val_no
->is_variable())
1934 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1936 else if (!no
->is_sink())
1938 if (no
->is_variable())
1939 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1943 Named_object
* dummy
= this->create_dummy_global(type
, NULL
, location
);
1944 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1950 // Create a single variable. If IS_COLONEQ is true, we permit
1951 // redeclarations in the same block, and we set *IS_NEW when we find a
1952 // new variable which is not a redeclaration.
1955 Parse::init_var(const Typed_identifier
& tid
, Type
* type
, Expression
* init
,
1956 bool is_coloneq
, bool type_from_init
, bool* is_new
,
1957 Expression_list
* vars
, Expression_list
* vals
)
1959 Location location
= tid
.location();
1961 if (Gogo::is_sink_name(tid
.name()))
1963 if (!type_from_init
&& init
!= NULL
)
1965 if (this->gogo_
->in_global_scope())
1966 return this->create_dummy_global(type
, init
, location
);
1969 // Create a dummy variable so that we will check whether the
1970 // initializer can be assigned to the type.
1971 Variable
* var
= new Variable(type
, init
, false, false, false,
1976 snprintf(buf
, sizeof buf
, "sink$%d", count
);
1978 return this->gogo_
->add_variable(buf
, var
);
1982 this->gogo_
->add_type_to_verify(type
);
1983 return this->gogo_
->add_sink();
1988 Named_object
* no
= this->gogo_
->lookup_in_block(tid
.name());
1990 && (no
->is_variable() || no
->is_result_variable()))
1992 // INIT may be NULL even when IS_COLONEQ is true for cases
1993 // like v, ok := x.(int).
1994 if (!type_from_init
&& init
!= NULL
)
1996 go_assert(vars
!= NULL
&& vals
!= NULL
);
1997 vars
->push_back(Expression::make_var_reference(no
, location
));
1998 vals
->push_back(init
);
2004 Variable
* var
= new Variable(type
, init
, this->gogo_
->in_global_scope(),
2005 false, false, location
);
2006 Named_object
* no
= this->gogo_
->add_variable(tid
.name(), var
);
2007 if (!no
->is_variable())
2009 // The name is already defined, so we just gave an error.
2010 return this->gogo_
->add_sink();
2015 // Create a dummy global variable to force an initializer to be run in
2016 // the right place. This is used when a sink variable is initialized
2020 Parse::create_dummy_global(Type
* type
, Expression
* init
,
2023 if (type
== NULL
&& init
== NULL
)
2024 type
= Type::lookup_bool_type();
2025 Variable
* var
= new Variable(type
, init
, true, false, false, location
);
2028 snprintf(buf
, sizeof buf
, "_.%d", count
);
2030 return this->gogo_
->add_variable(buf
, var
);
2033 // Finish the variable initialization by executing any assignments to
2034 // existing variables when using :=. These must be done as a tuple
2035 // assignment in case of something like n, a, b := 1, b, a.
2038 Parse::finish_init_vars(Expression_list
* vars
, Expression_list
* vals
,
2046 else if (vars
->size() == 1)
2048 go_assert(!this->gogo_
->in_global_scope());
2049 this->gogo_
->add_statement(Statement::make_assignment(vars
->front(),
2057 go_assert(!this->gogo_
->in_global_scope());
2058 this->gogo_
->add_statement(Statement::make_tuple_assignment(vars
, vals
,
2063 // SimpleVarDecl = identifier ":=" Expression .
2065 // We've already seen the identifier.
2067 // FIXME: We also have to implement
2068 // IdentifierList ":=" ExpressionList
2069 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2070 // tuple assignments here as well.
2072 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2073 // side may be a composite literal.
2075 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2078 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2079 // guard (var := expr.("type") using the literal keyword "type").
2082 Parse::simple_var_decl_or_assignment(const std::string
& name
,
2084 bool may_be_composite_lit
,
2085 Range_clause
* p_range_clause
,
2086 Type_switch
* p_type_switch
)
2088 Typed_identifier_list til
;
2089 til
.push_back(Typed_identifier(name
, NULL
, location
));
2091 std::set
<std::string
> uniq_idents
;
2092 uniq_idents
.insert(name
);
2094 // We've seen one identifier. If we see a comma now, this could be
2096 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2098 go_assert(p_type_switch
== NULL
);
2101 const Token
* token
= this->advance_token();
2102 if (!token
->is_identifier())
2105 std::string id
= token
->identifier();
2106 bool is_id_exported
= token
->is_identifier_exported();
2107 Location id_location
= token
->location();
2108 std::pair
<std::set
<std::string
>::iterator
, bool> ins
;
2110 token
= this->advance_token();
2111 if (!token
->is_op(OPERATOR_COMMA
))
2113 if (token
->is_op(OPERATOR_COLONEQ
))
2115 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2116 ins
= uniq_idents
.insert(id
);
2117 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2118 error_at(id_location
, "multiple assignments to %s",
2119 Gogo::message_name(id
).c_str());
2120 til
.push_back(Typed_identifier(id
, NULL
, location
));
2123 this->unget_token(Token::make_identifier_token(id
,
2129 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2130 ins
= uniq_idents
.insert(id
);
2131 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2132 error_at(id_location
, "multiple assignments to %s",
2133 Gogo::message_name(id
).c_str());
2134 til
.push_back(Typed_identifier(id
, NULL
, location
));
2137 // We have a comma separated list of identifiers in TIL. If the
2138 // next token is COLONEQ, then this is a simple var decl, and we
2139 // have the complete list of identifiers. If the next token is
2140 // not COLONEQ, then the only valid parse is a tuple assignment.
2141 // The list of identifiers we have so far is really a list of
2142 // expressions. There are more expressions following.
2144 if (!this->peek_token()->is_op(OPERATOR_COLONEQ
))
2146 Expression_list
* exprs
= new Expression_list
;
2147 for (Typed_identifier_list::const_iterator p
= til
.begin();
2150 exprs
->push_back(this->id_to_expression(p
->name(), p
->location(),
2153 Expression_list
* more_exprs
=
2154 this->expression_list(NULL
, true, may_be_composite_lit
);
2155 for (Expression_list::const_iterator p
= more_exprs
->begin();
2156 p
!= more_exprs
->end();
2158 exprs
->push_back(*p
);
2161 this->tuple_assignment(exprs
, may_be_composite_lit
, p_range_clause
);
2166 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ
));
2167 const Token
* token
= this->advance_token();
2169 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
2171 this->range_clause_decl(&til
, p_range_clause
);
2175 Expression_list
* init
;
2176 if (p_type_switch
== NULL
)
2177 init
= this->expression_list(NULL
, false, may_be_composite_lit
);
2180 bool is_type_switch
= false;
2181 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false,
2182 may_be_composite_lit
,
2183 &is_type_switch
, NULL
);
2186 p_type_switch
->found
= true;
2187 p_type_switch
->name
= name
;
2188 p_type_switch
->location
= location
;
2189 p_type_switch
->expr
= expr
;
2193 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
2195 init
= new Expression_list();
2196 init
->push_back(expr
);
2200 this->advance_token();
2201 init
= this->expression_list(expr
, false, may_be_composite_lit
);
2205 this->init_vars(&til
, NULL
, init
, true, location
);
2208 // FunctionDecl = "func" identifier Signature [ Block ] .
2209 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2211 // Deprecated gcc extension:
2212 // FunctionDecl = "func" identifier Signature
2213 // __asm__ "(" string_lit ")" .
2214 // This extension means a function whose real name is the identifier
2215 // inside the asm. This extension will be removed at some future
2216 // date. It has been replaced with //extern comments.
2218 // SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment,
2219 // which means that we omit the method from the type descriptor.
2222 Parse::function_decl(bool saw_nointerface
)
2224 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2225 Location location
= this->location();
2226 std::string extern_name
= this->lex_
->extern_name();
2227 const Token
* token
= this->advance_token();
2229 Typed_identifier
* rec
= NULL
;
2230 if (token
->is_op(OPERATOR_LPAREN
))
2232 rec
= this->receiver();
2233 token
= this->peek_token();
2235 else if (saw_nointerface
)
2237 warning_at(location
, 0,
2238 "ignoring magic //go:nointerface comment before non-method");
2239 saw_nointerface
= false;
2242 if (!token
->is_identifier())
2244 error_at(this->location(), "expected function name");
2249 this->gogo_
->pack_hidden_name(token
->identifier(),
2250 token
->is_identifier_exported());
2252 this->advance_token();
2254 Function_type
* fntype
= this->signature(rec
, this->location());
2256 Named_object
* named_object
= NULL
;
2258 if (this->peek_token()->is_keyword(KEYWORD_ASM
))
2260 if (!this->advance_token()->is_op(OPERATOR_LPAREN
))
2262 error_at(this->location(), "expected %<(%>");
2265 token
= this->advance_token();
2266 if (!token
->is_string())
2268 error_at(this->location(), "expected string");
2271 std::string asm_name
= token
->string_value();
2272 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
2274 error_at(this->location(), "expected %<)%>");
2277 this->advance_token();
2278 if (!Gogo::is_sink_name(name
))
2280 named_object
= this->gogo_
->declare_function(name
, fntype
, location
);
2281 if (named_object
->is_function_declaration())
2282 named_object
->func_declaration_value()->set_asm_name(asm_name
);
2286 // Check for the easy error of a newline before the opening brace.
2287 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
2289 Location semi_loc
= this->location();
2290 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
2291 error_at(this->location(),
2292 "unexpected semicolon or newline before %<{%>");
2294 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
2298 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2300 if (named_object
== NULL
&& !Gogo::is_sink_name(name
))
2303 this->gogo_
->add_erroneous_name(name
);
2306 named_object
= this->gogo_
->declare_function(name
, fntype
,
2308 if (!extern_name
.empty()
2309 && named_object
->is_function_declaration())
2311 Function_declaration
* fd
=
2312 named_object
->func_declaration_value();
2313 fd
->set_asm_name(extern_name
);
2318 if (saw_nointerface
)
2319 warning_at(location
, 0,
2320 ("ignoring magic //go:nointerface comment "
2321 "before declaration"));
2325 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2328 fntype
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2329 this->is_erroneous_function_
= true;
2330 if (!Gogo::is_sink_name(name
))
2331 this->gogo_
->add_erroneous_name(name
);
2332 name
= this->gogo_
->pack_hidden_name("_", false);
2334 named_object
= this->gogo_
->start_function(name
, fntype
, true, location
);
2335 Location end_loc
= this->block();
2336 this->gogo_
->finish_function(end_loc
);
2338 && !this->is_erroneous_function_
2339 && named_object
->is_function())
2340 named_object
->func_value()->set_nointerface();
2341 this->is_erroneous_function_
= hold_is_erroneous_function
;
2345 // Receiver = Parameters .
2350 Location location
= this->location();
2351 Typed_identifier_list
* til
;
2352 if (!this->parameters(&til
, NULL
))
2354 else if (til
== NULL
|| til
->empty())
2356 error_at(location
, "method has no receiver");
2359 else if (til
->size() > 1)
2361 error_at(location
, "method has multiple receivers");
2365 return &til
->front();
2368 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2369 // Literal = BasicLit | CompositeLit | FunctionLit .
2370 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2372 // If MAY_BE_SINK is true, this operand may be "_".
2374 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2375 // if the entire expression is in parentheses.
2378 Parse::operand(bool may_be_sink
, bool* is_parenthesized
)
2380 const Token
* token
= this->peek_token();
2382 switch (token
->classification())
2384 case Token::TOKEN_IDENTIFIER
:
2386 Location location
= token
->location();
2387 std::string id
= token
->identifier();
2388 bool is_exported
= token
->is_identifier_exported();
2389 std::string packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2391 Named_object
* in_function
;
2392 Named_object
* named_object
= this->gogo_
->lookup(packed
, &in_function
);
2394 Package
* package
= NULL
;
2395 if (named_object
!= NULL
&& named_object
->is_package())
2397 if (!this->advance_token()->is_op(OPERATOR_DOT
)
2398 || !this->advance_token()->is_identifier())
2400 error_at(location
, "unexpected reference to package");
2401 return Expression::make_error(location
);
2403 package
= named_object
->package_value();
2404 package
->set_used();
2405 id
= this->peek_token()->identifier();
2406 is_exported
= this->peek_token()->is_identifier_exported();
2407 packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2408 named_object
= package
->lookup(packed
);
2409 location
= this->location();
2410 go_assert(in_function
== NULL
);
2413 this->advance_token();
2415 if (named_object
!= NULL
2416 && named_object
->is_type()
2417 && !named_object
->type_value()->is_visible())
2419 go_assert(package
!= NULL
);
2420 error_at(location
, "invalid reference to hidden type %<%s.%s%>",
2421 Gogo::message_name(package
->package_name()).c_str(),
2422 Gogo::message_name(id
).c_str());
2423 return Expression::make_error(location
);
2427 if (named_object
== NULL
)
2429 if (package
!= NULL
)
2431 std::string n1
= Gogo::message_name(package
->package_name());
2432 std::string n2
= Gogo::message_name(id
);
2435 ("invalid reference to unexported identifier "
2437 n1
.c_str(), n2
.c_str());
2440 "reference to undefined identifier %<%s.%s%>",
2441 n1
.c_str(), n2
.c_str());
2442 return Expression::make_error(location
);
2445 named_object
= this->gogo_
->add_unknown_name(packed
, location
);
2448 if (in_function
!= NULL
2449 && in_function
!= this->gogo_
->current_function()
2450 && (named_object
->is_variable()
2451 || named_object
->is_result_variable()))
2452 return this->enclosing_var_reference(in_function
, named_object
,
2455 switch (named_object
->classification())
2457 case Named_object::NAMED_OBJECT_CONST
:
2458 return Expression::make_const_reference(named_object
, location
);
2459 case Named_object::NAMED_OBJECT_TYPE
:
2460 return Expression::make_type(named_object
->type_value(), location
);
2461 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
2463 Type
* t
= Type::make_forward_declaration(named_object
);
2464 return Expression::make_type(t
, location
);
2466 case Named_object::NAMED_OBJECT_VAR
:
2467 case Named_object::NAMED_OBJECT_RESULT_VAR
:
2468 // Any left-hand-side can be a sink, so if this can not be
2469 // a sink, then it must be a use of the variable.
2471 this->mark_var_used(named_object
);
2472 return Expression::make_var_reference(named_object
, location
);
2473 case Named_object::NAMED_OBJECT_SINK
:
2475 return Expression::make_sink(location
);
2478 error_at(location
, "cannot use _ as value");
2479 return Expression::make_error(location
);
2481 case Named_object::NAMED_OBJECT_FUNC
:
2482 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
2483 return Expression::make_func_reference(named_object
, NULL
,
2485 case Named_object::NAMED_OBJECT_UNKNOWN
:
2487 Unknown_expression
* ue
=
2488 Expression::make_unknown_reference(named_object
, location
);
2489 if (this->is_erroneous_function_
)
2490 ue
->set_no_error_message();
2493 case Named_object::NAMED_OBJECT_ERRONEOUS
:
2494 return Expression::make_error(location
);
2501 case Token::TOKEN_STRING
:
2502 ret
= Expression::make_string(token
->string_value(), token
->location());
2503 this->advance_token();
2506 case Token::TOKEN_CHARACTER
:
2507 ret
= Expression::make_character(token
->character_value(), NULL
,
2509 this->advance_token();
2512 case Token::TOKEN_INTEGER
:
2513 ret
= Expression::make_integer(token
->integer_value(), NULL
,
2515 this->advance_token();
2518 case Token::TOKEN_FLOAT
:
2519 ret
= Expression::make_float(token
->float_value(), NULL
,
2521 this->advance_token();
2524 case Token::TOKEN_IMAGINARY
:
2527 mpfr_init_set_ui(zero
, 0, GMP_RNDN
);
2528 ret
= Expression::make_complex(&zero
, token
->imaginary_value(),
2529 NULL
, token
->location());
2531 this->advance_token();
2535 case Token::TOKEN_KEYWORD
:
2536 switch (token
->keyword())
2539 return this->function_lit();
2541 case KEYWORD_INTERFACE
:
2543 case KEYWORD_STRUCT
:
2545 Location location
= token
->location();
2546 return Expression::make_type(this->type(), location
);
2553 case Token::TOKEN_OPERATOR
:
2554 if (token
->is_op(OPERATOR_LPAREN
))
2556 this->advance_token();
2557 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2559 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2560 error_at(this->location(), "missing %<)%>");
2562 this->advance_token();
2563 if (is_parenthesized
!= NULL
)
2564 *is_parenthesized
= true;
2567 else if (token
->is_op(OPERATOR_LSQUARE
))
2569 // Here we call array_type directly, as this is the only
2570 // case where an ellipsis is permitted for an array type.
2571 Location location
= token
->location();
2572 return Expression::make_type(this->array_type(true), location
);
2580 error_at(this->location(), "expected operand");
2581 return Expression::make_error(this->location());
2584 // Handle a reference to a variable in an enclosing function. We add
2585 // it to a list of such variables. We return a reference to a field
2586 // in a struct which will be passed on the static chain when calling
2587 // the current function.
2590 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2593 go_assert(var
->is_variable() || var
->is_result_variable());
2595 this->mark_var_used(var
);
2597 Named_object
* this_function
= this->gogo_
->current_function();
2598 Named_object
* closure
= this_function
->func_value()->closure_var();
2600 // The last argument to the Enclosing_var constructor is the index
2601 // of this variable in the closure. We add 1 to the current number
2602 // of enclosed variables, because the first field in the closure
2603 // points to the function code.
2604 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2605 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2606 this->enclosing_vars_
.insert(ev
);
2609 // This is a variable we have not seen before. Add a new field
2610 // to the closure type.
2611 this_function
->func_value()->add_closure_field(var
, location
);
2614 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2616 closure_ref
= Expression::make_unary(OPERATOR_MULT
, closure_ref
, location
);
2618 // The closure structure holds pointers to the variables, so we need
2619 // to introduce an indirection.
2620 Expression
* e
= Expression::make_field_reference(closure_ref
,
2623 e
= Expression::make_unary(OPERATOR_MULT
, e
, location
);
2627 // CompositeLit = LiteralType LiteralValue .
2628 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2629 // SliceType | MapType | TypeName .
2630 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2631 // ElementList = Element { "," Element } .
2632 // Element = [ Key ":" ] Value .
2633 // Key = FieldName | ElementIndex .
2634 // FieldName = identifier .
2635 // ElementIndex = Expression .
2636 // Value = Expression | LiteralValue .
2638 // We have already seen the type if there is one, and we are now
2639 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2640 // will be seen here as an array type whose length is "nil". The
2641 // DEPTH parameter is non-zero if this is an embedded composite
2642 // literal and the type was omitted. It gives the number of steps up
2643 // to the type which was provided. E.g., in [][]int{{1}} it will be
2644 // 1. In [][][]int{{{1}}} it will be 2.
2647 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2649 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2650 this->advance_token();
2652 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2654 this->advance_token();
2655 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2659 bool has_keys
= false;
2660 bool all_are_names
= true;
2661 Expression_list
* vals
= new Expression_list
;
2665 bool is_type_omitted
= false;
2666 bool is_name
= false;
2668 const Token
* token
= this->peek_token();
2670 if (token
->is_identifier())
2672 std::string identifier
= token
->identifier();
2673 bool is_exported
= token
->is_identifier_exported();
2674 Location location
= token
->location();
2676 if (this->advance_token()->is_op(OPERATOR_COLON
))
2678 // This may be a field name. We don't know for sure--it
2679 // could also be an expression for an array index. We
2680 // don't want to parse it as an expression because may
2681 // trigger various errors, e.g., if this identifier
2682 // happens to be the name of a package.
2683 Gogo
* gogo
= this->gogo_
;
2684 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2691 this->unget_token(Token::make_identifier_token(identifier
,
2694 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2698 else if (!token
->is_op(OPERATOR_LCURLY
))
2699 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2702 // This must be a composite literal inside another composite
2703 // literal, with the type omitted for the inner one.
2704 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2705 is_type_omitted
= true;
2708 token
= this->peek_token();
2709 if (!token
->is_op(OPERATOR_COLON
))
2712 vals
->push_back(NULL
);
2717 if (is_type_omitted
&& !val
->is_error_expression())
2719 error_at(this->location(), "unexpected %<:%>");
2720 val
= Expression::make_error(this->location());
2723 this->advance_token();
2725 if (!has_keys
&& !vals
->empty())
2727 Expression_list
* newvals
= new Expression_list
;
2728 for (Expression_list::const_iterator p
= vals
->begin();
2732 newvals
->push_back(NULL
);
2733 newvals
->push_back(*p
);
2740 if (val
->unknown_expression() != NULL
)
2741 val
->unknown_expression()->set_is_composite_literal_key();
2743 vals
->push_back(val
);
2745 if (!token
->is_op(OPERATOR_LCURLY
))
2746 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2749 // This must be a composite literal inside another
2750 // composite literal, with the type omitted for the
2752 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2755 token
= this->peek_token();
2758 vals
->push_back(val
);
2761 all_are_names
= false;
2763 if (token
->is_op(OPERATOR_COMMA
))
2765 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2767 this->advance_token();
2771 else if (token
->is_op(OPERATOR_RCURLY
))
2773 this->advance_token();
2778 if (token
->is_op(OPERATOR_SEMICOLON
))
2779 error_at(this->location(),
2780 "need trailing comma before newline in composite literal");
2782 error_at(this->location(), "expected %<,%> or %<}%>");
2784 this->gogo_
->mark_locals_used();
2786 while (!token
->is_eof()
2787 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2789 if (token
->is_op(OPERATOR_LCURLY
))
2791 else if (token
->is_op(OPERATOR_RCURLY
))
2793 token
= this->advance_token();
2795 if (token
->is_op(OPERATOR_RCURLY
))
2796 this->advance_token();
2798 return Expression::make_error(location
);
2802 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2803 all_are_names
, location
);
2806 // FunctionLit = "func" Signature Block .
2809 Parse::function_lit()
2811 Location location
= this->location();
2812 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2813 this->advance_token();
2815 Enclosing_vars hold_enclosing_vars
;
2816 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2818 Function_type
* type
= this->signature(NULL
, location
);
2819 bool fntype_is_error
= false;
2822 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2823 fntype_is_error
= true;
2826 // For a function literal, the next token must be a '{'. If we
2827 // don't see that, then we may have a type expression.
2828 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2830 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2831 return Expression::make_type(type
, location
);
2834 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2835 if (fntype_is_error
)
2836 this->is_erroneous_function_
= true;
2838 Bc_stack
* hold_break_stack
= this->break_stack_
;
2839 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2840 this->break_stack_
= NULL
;
2841 this->continue_stack_
= NULL
;
2843 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2845 Location end_loc
= this->block();
2847 this->gogo_
->finish_function(end_loc
);
2849 if (this->break_stack_
!= NULL
)
2850 delete this->break_stack_
;
2851 if (this->continue_stack_
!= NULL
)
2852 delete this->continue_stack_
;
2853 this->break_stack_
= hold_break_stack
;
2854 this->continue_stack_
= hold_continue_stack
;
2856 this->is_erroneous_function_
= hold_is_erroneous_function
;
2858 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2860 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
2863 return Expression::make_func_reference(no
, closure
, location
);
2866 // Create a closure for the nested function FUNCTION. This is based
2867 // on ENCLOSING_VARS, which is a list of all variables defined in
2868 // enclosing functions and referenced from FUNCTION. A closure is the
2869 // address of a struct which point to the real function code and
2870 // contains the addresses of all the referenced variables. This
2871 // returns NULL if no closure is required.
2874 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
2877 if (enclosing_vars
->empty())
2880 // Get the variables in order by their field index.
2882 size_t enclosing_var_count
= enclosing_vars
->size();
2883 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
2884 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
2885 p
!= enclosing_vars
->end();
2888 // Subtract 1 because index 0 is the function code.
2889 ev
[p
->index() - 1] = *p
;
2892 // Build an initializer for a composite literal of the closure's
2895 Named_object
* enclosing_function
= this->gogo_
->current_function();
2896 Expression_list
* initializer
= new Expression_list
;
2898 initializer
->push_back(Expression::make_func_code_reference(function
,
2901 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
2903 // Add 1 to i because the first field in the closure is a
2904 // pointer to the function code.
2905 go_assert(ev
[i
].index() == i
+ 1);
2906 Named_object
* var
= ev
[i
].var();
2908 if (ev
[i
].in_function() == enclosing_function
)
2909 ref
= Expression::make_var_reference(var
, location
);
2911 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
2913 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
2915 initializer
->push_back(refaddr
);
2918 Named_object
* closure_var
= function
->func_value()->closure_var();
2919 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
2920 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
2922 return Expression::make_heap_expression(cv
, location
);
2925 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2927 // If MAY_BE_SINK is true, this expression may be "_".
2929 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2932 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2933 // guard (var := expr.("type") using the literal keyword "type").
2935 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2936 // if the entire expression is in parentheses.
2939 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
2940 bool* is_type_switch
, bool* is_parenthesized
)
2942 Location start_loc
= this->location();
2943 bool operand_is_parenthesized
= false;
2944 bool whole_is_parenthesized
= false;
2946 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
2948 whole_is_parenthesized
= operand_is_parenthesized
;
2950 // An unknown name followed by a curly brace must be a composite
2951 // literal, and the unknown name must be a type.
2952 if (may_be_composite_lit
2953 && !operand_is_parenthesized
2954 && ret
->unknown_expression() != NULL
2955 && this->peek_token()->is_op(OPERATOR_LCURLY
))
2957 Named_object
* no
= ret
->unknown_expression()->named_object();
2958 Type
* type
= Type::make_forward_declaration(no
);
2959 ret
= Expression::make_type(type
, ret
->location());
2962 // We handle composite literals and type casts here, as it is the
2963 // easiest way to handle types which are in parentheses, as in
2965 if (ret
->is_type_expression())
2967 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
2969 whole_is_parenthesized
= false;
2970 if (!may_be_composite_lit
)
2972 Type
* t
= ret
->type();
2973 if (t
->named_type() != NULL
2974 || t
->forward_declaration_type() != NULL
)
2976 _("parentheses required around this composite literal "
2977 "to avoid parsing ambiguity"));
2979 else if (operand_is_parenthesized
)
2981 "cannot parenthesize type in composite literal");
2982 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
2984 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
2986 whole_is_parenthesized
= false;
2987 Location loc
= this->location();
2988 this->advance_token();
2989 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
2991 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2992 this->advance_token();
2993 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
2995 error_at(this->location(),
2996 "invalid use of %<...%> in type conversion");
2997 this->advance_token();
2999 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3000 error_at(this->location(), "expected %<)%>");
3002 this->advance_token();
3003 if (expr
->is_error_expression())
3007 Type
* t
= ret
->type();
3008 if (t
->classification() == Type::TYPE_ARRAY
3009 && t
->array_type()->length() != NULL
3010 && t
->array_type()->length()->is_nil_expression())
3012 error_at(ret
->location(),
3013 "use of %<[...]%> outside of array literal");
3014 ret
= Expression::make_error(loc
);
3017 ret
= Expression::make_cast(t
, expr
, loc
);
3024 const Token
* token
= this->peek_token();
3025 if (token
->is_op(OPERATOR_LPAREN
))
3027 whole_is_parenthesized
= false;
3028 ret
= this->call(this->verify_not_sink(ret
));
3030 else if (token
->is_op(OPERATOR_DOT
))
3032 whole_is_parenthesized
= false;
3033 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3034 if (is_type_switch
!= NULL
&& *is_type_switch
)
3037 else if (token
->is_op(OPERATOR_LSQUARE
))
3039 whole_is_parenthesized
= false;
3040 ret
= this->index(this->verify_not_sink(ret
));
3046 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3047 *is_parenthesized
= true;
3052 // Selector = "." identifier .
3053 // TypeGuard = "." "(" QualifiedIdent ")" .
3055 // Note that Operand can expand to QualifiedIdent, which contains a
3056 // ".". That is handled directly in operand when it sees a package
3059 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3060 // guard (var := expr.("type") using the literal keyword "type").
3063 Parse::selector(Expression
* left
, bool* is_type_switch
)
3065 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3066 Location location
= this->location();
3068 const Token
* token
= this->advance_token();
3069 if (token
->is_identifier())
3071 // This could be a field in a struct, or a method in an
3072 // interface, or a method associated with a type. We can't know
3073 // which until we have seen all the types.
3075 this->gogo_
->pack_hidden_name(token
->identifier(),
3076 token
->is_identifier_exported());
3077 if (token
->identifier() == "_")
3079 error_at(this->location(), "invalid use of %<_%>");
3080 name
= Gogo::erroneous_name();
3082 this->advance_token();
3083 return Expression::make_selector(left
, name
, location
);
3085 else if (token
->is_op(OPERATOR_LPAREN
))
3087 this->advance_token();
3089 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3090 type
= this->type();
3093 if (is_type_switch
!= NULL
)
3094 *is_type_switch
= true;
3097 error_at(this->location(),
3098 "use of %<.(type)%> outside type switch");
3099 type
= Type::make_error_type();
3101 this->advance_token();
3103 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3104 error_at(this->location(), "missing %<)%>");
3106 this->advance_token();
3107 if (is_type_switch
!= NULL
&& *is_type_switch
)
3109 return Expression::make_type_guard(left
, type
, location
);
3113 error_at(this->location(), "expected identifier or %<(%>");
3118 // Index = "[" Expression "]" .
3119 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3122 Parse::index(Expression
* expr
)
3124 Location location
= this->location();
3125 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3126 this->advance_token();
3129 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3130 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3134 mpz_init_set_ui(zero
, 0);
3135 start
= Expression::make_integer(&zero
, NULL
, location
);
3139 Expression
* end
= NULL
;
3140 if (this->peek_token()->is_op(OPERATOR_COLON
))
3142 // We use nil to indicate a missing high expression.
3143 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3144 end
= Expression::make_nil(this->location());
3145 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3147 error_at(this->location(), "middle index required in 3-index slice");
3148 end
= Expression::make_error(this->location());
3151 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3154 Expression
* cap
= NULL
;
3155 if (this->peek_token()->is_op(OPERATOR_COLON
))
3157 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3159 error_at(this->location(), "final index required in 3-index slice");
3160 cap
= Expression::make_error(this->location());
3163 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3165 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3166 error_at(this->location(), "missing %<]%>");
3168 this->advance_token();
3169 return Expression::make_index(expr
, start
, end
, cap
, location
);
3172 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3173 // ArgumentList = ExpressionList [ "..." ] .
3176 Parse::call(Expression
* func
)
3178 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3179 Expression_list
* args
= NULL
;
3180 bool is_varargs
= false;
3181 const Token
* token
= this->advance_token();
3182 if (!token
->is_op(OPERATOR_RPAREN
))
3184 args
= this->expression_list(NULL
, false, true);
3185 token
= this->peek_token();
3186 if (token
->is_op(OPERATOR_ELLIPSIS
))
3189 token
= this->advance_token();
3192 if (token
->is_op(OPERATOR_COMMA
))
3193 token
= this->advance_token();
3194 if (!token
->is_op(OPERATOR_RPAREN
))
3195 error_at(this->location(), "missing %<)%>");
3197 this->advance_token();
3198 if (func
->is_error_expression())
3200 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3203 // Return an expression for a single unqualified identifier.
3206 Parse::id_to_expression(const std::string
& name
, Location location
,
3209 Named_object
* in_function
;
3210 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3211 if (named_object
== NULL
)
3212 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3214 if (in_function
!= NULL
3215 && in_function
!= this->gogo_
->current_function()
3216 && (named_object
->is_variable() || named_object
->is_result_variable()))
3217 return this->enclosing_var_reference(in_function
, named_object
,
3220 switch (named_object
->classification())
3222 case Named_object::NAMED_OBJECT_CONST
:
3223 return Expression::make_const_reference(named_object
, location
);
3224 case Named_object::NAMED_OBJECT_VAR
:
3225 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3227 this->mark_var_used(named_object
);
3228 return Expression::make_var_reference(named_object
, location
);
3229 case Named_object::NAMED_OBJECT_SINK
:
3230 return Expression::make_sink(location
);
3231 case Named_object::NAMED_OBJECT_FUNC
:
3232 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3233 return Expression::make_func_reference(named_object
, NULL
, location
);
3234 case Named_object::NAMED_OBJECT_UNKNOWN
:
3236 Unknown_expression
* ue
=
3237 Expression::make_unknown_reference(named_object
, location
);
3238 if (this->is_erroneous_function_
)
3239 ue
->set_no_error_message();
3242 case Named_object::NAMED_OBJECT_PACKAGE
:
3243 case Named_object::NAMED_OBJECT_TYPE
:
3244 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3246 // These cases can arise for a field name in a composite
3248 Unknown_expression
* ue
=
3249 Expression::make_unknown_reference(named_object
, location
);
3250 if (this->is_erroneous_function_
)
3251 ue
->set_no_error_message();
3254 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3255 return Expression::make_error(location
);
3257 error_at(this->location(), "unexpected type of identifier");
3258 return Expression::make_error(location
);
3262 // Expression = UnaryExpr { binary_op Expression } .
3264 // PRECEDENCE is the precedence of the current operator.
3266 // If MAY_BE_SINK is true, this expression may be "_".
3268 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3271 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3272 // guard (var := expr.("type") using the literal keyword "type").
3274 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3275 // if the entire expression is in parentheses.
3278 Parse::expression(Precedence precedence
, bool may_be_sink
,
3279 bool may_be_composite_lit
, bool* is_type_switch
,
3280 bool *is_parenthesized
)
3282 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3283 is_type_switch
, is_parenthesized
);
3287 if (is_type_switch
!= NULL
&& *is_type_switch
)
3290 const Token
* token
= this->peek_token();
3291 if (token
->classification() != Token::TOKEN_OPERATOR
)
3297 Precedence right_precedence
;
3298 switch (token
->op())
3301 right_precedence
= PRECEDENCE_OROR
;
3303 case OPERATOR_ANDAND
:
3304 right_precedence
= PRECEDENCE_ANDAND
;
3307 case OPERATOR_NOTEQ
:
3312 right_precedence
= PRECEDENCE_RELOP
;
3315 case OPERATOR_MINUS
:
3318 right_precedence
= PRECEDENCE_ADDOP
;
3323 case OPERATOR_LSHIFT
:
3324 case OPERATOR_RSHIFT
:
3326 case OPERATOR_BITCLEAR
:
3327 right_precedence
= PRECEDENCE_MULOP
;
3330 right_precedence
= PRECEDENCE_INVALID
;
3334 if (right_precedence
== PRECEDENCE_INVALID
)
3340 if (is_parenthesized
!= NULL
)
3341 *is_parenthesized
= false;
3343 Operator op
= token
->op();
3344 Location binop_location
= token
->location();
3346 if (precedence
>= right_precedence
)
3348 // We've already seen A * B, and we see + C. We want to
3349 // return so that A * B becomes a group.
3353 this->advance_token();
3355 left
= this->verify_not_sink(left
);
3356 Expression
* right
= this->expression(right_precedence
, false,
3357 may_be_composite_lit
,
3359 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3364 Parse::expression_may_start_here()
3366 const Token
* token
= this->peek_token();
3367 switch (token
->classification())
3369 case Token::TOKEN_INVALID
:
3370 case Token::TOKEN_EOF
:
3372 case Token::TOKEN_KEYWORD
:
3373 switch (token
->keyword())
3378 case KEYWORD_STRUCT
:
3379 case KEYWORD_INTERFACE
:
3384 case Token::TOKEN_IDENTIFIER
:
3386 case Token::TOKEN_STRING
:
3388 case Token::TOKEN_OPERATOR
:
3389 switch (token
->op())
3392 case OPERATOR_MINUS
:
3396 case OPERATOR_CHANOP
:
3398 case OPERATOR_LPAREN
:
3399 case OPERATOR_LSQUARE
:
3404 case Token::TOKEN_CHARACTER
:
3405 case Token::TOKEN_INTEGER
:
3406 case Token::TOKEN_FLOAT
:
3407 case Token::TOKEN_IMAGINARY
:
3414 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3416 // If MAY_BE_SINK is true, this expression may be "_".
3418 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3421 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3422 // guard (var := expr.("type") using the literal keyword "type").
3424 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3425 // if the entire expression is in parentheses.
3428 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3429 bool* is_type_switch
, bool* is_parenthesized
)
3431 const Token
* token
= this->peek_token();
3433 // There is a complex parse for <- chan. The choices are
3434 // Convert x to type <- chan int:
3436 // Receive from (x converted to type chan <- chan int):
3437 // (<- chan <- chan int (x))
3438 // Convert x to type <- chan (<- chan int).
3439 // (<- chan <- chan int)(x)
3440 if (token
->is_op(OPERATOR_CHANOP
))
3442 Location location
= token
->location();
3443 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3445 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3447 if (expr
->is_error_expression())
3449 else if (!expr
->is_type_expression())
3450 return Expression::make_receive(expr
, location
);
3453 if (expr
->type()->is_error_type())
3456 // We picked up "chan TYPE", but it is not a type
3458 Channel_type
* ct
= expr
->type()->channel_type();
3461 // This is probably impossible.
3462 error_at(location
, "expected channel type");
3463 return Expression::make_error(location
);
3465 else if (ct
->may_receive())
3468 Type
* t
= Type::make_channel_type(false, true,
3469 ct
->element_type());
3470 return Expression::make_type(t
, location
);
3474 // <- chan <- TYPE. Because we skipped the leading
3475 // <-, we parsed this as chan <- TYPE. With the
3476 // leading <-, we parse it as <- chan (<- TYPE).
3477 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3478 return Expression::make_type(t
, location
);
3483 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3484 token
= this->peek_token();
3487 if (token
->is_op(OPERATOR_PLUS
)
3488 || token
->is_op(OPERATOR_MINUS
)
3489 || token
->is_op(OPERATOR_NOT
)
3490 || token
->is_op(OPERATOR_XOR
)
3491 || token
->is_op(OPERATOR_CHANOP
)
3492 || token
->is_op(OPERATOR_MULT
)
3493 || token
->is_op(OPERATOR_AND
))
3495 Location location
= token
->location();
3496 Operator op
= token
->op();
3497 this->advance_token();
3499 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3501 if (expr
->is_error_expression())
3503 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3504 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3506 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3507 expr
= Expression::make_heap_expression(expr
, location
);
3508 else if (op
!= OPERATOR_CHANOP
)
3509 expr
= Expression::make_unary(op
, expr
, location
);
3511 expr
= Expression::make_receive(expr
, location
);
3515 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3516 is_type_switch
, is_parenthesized
);
3519 // This is called for the obscure case of
3520 // (<- chan <- chan int)(x)
3521 // In unary_expr we remove the leading <- and parse the remainder,
3523 // chan <- (chan int)
3524 // When we add the leading <- back in, we really want
3525 // <- chan (<- chan int)
3526 // This means that we need to reassociate.
3529 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3531 Channel_type
* ele
= ct
->element_type()->channel_type();
3534 error_at(location
, "parse error");
3535 return Type::make_error_type();
3538 if (ele
->may_send())
3539 sub
= Type::make_channel_type(false, true, ele
->element_type());
3541 sub
= this->reassociate_chan_direction(ele
, location
);
3542 return Type::make_channel_type(false, true, sub
);
3546 // Declaration | LabeledStmt | SimpleStmt |
3547 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3548 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3551 // LABEL is the label of this statement if it has one.
3554 Parse::statement(Label
* label
)
3556 const Token
* token
= this->peek_token();
3557 switch (token
->classification())
3559 case Token::TOKEN_KEYWORD
:
3561 switch (token
->keyword())
3566 this->declaration();
3570 case KEYWORD_STRUCT
:
3571 case KEYWORD_INTERFACE
:
3572 this->simple_stat(true, NULL
, NULL
, NULL
);
3576 this->go_or_defer_stat();
3578 case KEYWORD_RETURN
:
3579 this->return_stat();
3584 case KEYWORD_CONTINUE
:
3585 this->continue_stat();
3593 case KEYWORD_SWITCH
:
3594 this->switch_stat(label
);
3596 case KEYWORD_SELECT
:
3597 this->select_stat(label
);
3600 this->for_stat(label
);
3603 error_at(this->location(), "expected statement");
3604 this->advance_token();
3610 case Token::TOKEN_IDENTIFIER
:
3612 std::string identifier
= token
->identifier();
3613 bool is_exported
= token
->is_identifier_exported();
3614 Location location
= token
->location();
3615 if (this->advance_token()->is_op(OPERATOR_COLON
))
3617 this->advance_token();
3618 this->labeled_stmt(identifier
, location
);
3622 this->unget_token(Token::make_identifier_token(identifier
,
3625 this->simple_stat(true, NULL
, NULL
, NULL
);
3630 case Token::TOKEN_OPERATOR
:
3631 if (token
->is_op(OPERATOR_LCURLY
))
3633 Location location
= token
->location();
3634 this->gogo_
->start_block(location
);
3635 Location end_loc
= this->block();
3636 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3639 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3640 this->simple_stat(true, NULL
, NULL
, NULL
);
3643 case Token::TOKEN_STRING
:
3644 case Token::TOKEN_CHARACTER
:
3645 case Token::TOKEN_INTEGER
:
3646 case Token::TOKEN_FLOAT
:
3647 case Token::TOKEN_IMAGINARY
:
3648 this->simple_stat(true, NULL
, NULL
, NULL
);
3652 error_at(this->location(), "expected statement");
3653 this->advance_token();
3659 Parse::statement_may_start_here()
3661 const Token
* token
= this->peek_token();
3662 switch (token
->classification())
3664 case Token::TOKEN_KEYWORD
:
3666 switch (token
->keyword())
3673 case KEYWORD_STRUCT
:
3674 case KEYWORD_INTERFACE
:
3677 case KEYWORD_RETURN
:
3679 case KEYWORD_CONTINUE
:
3682 case KEYWORD_SWITCH
:
3683 case KEYWORD_SELECT
:
3693 case Token::TOKEN_IDENTIFIER
:
3696 case Token::TOKEN_OPERATOR
:
3697 if (token
->is_op(OPERATOR_LCURLY
)
3698 || token
->is_op(OPERATOR_SEMICOLON
))
3701 return this->expression_may_start_here();
3703 case Token::TOKEN_STRING
:
3704 case Token::TOKEN_CHARACTER
:
3705 case Token::TOKEN_INTEGER
:
3706 case Token::TOKEN_FLOAT
:
3707 case Token::TOKEN_IMAGINARY
:
3715 // LabeledStmt = Label ":" Statement .
3716 // Label = identifier .
3719 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3721 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3723 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3725 // This is a label at the end of a block. A program is
3726 // permitted to omit a semicolon here.
3730 if (!this->statement_may_start_here())
3732 // Mark the label as used to avoid a useless error about an
3735 label
->set_is_used();
3737 error_at(location
, "missing statement after label");
3738 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3743 this->statement(label
);
3746 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3747 // Assignment | ShortVarDecl .
3749 // EmptyStmt was handled in Parse::statement.
3751 // In order to make this work for if and switch statements, if
3752 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3753 // expression rather than adding an expression statement to the
3754 // current block. If we see something other than an ExpressionStat,
3755 // we add the statement, set *RETURN_EXP to true if we saw a send
3756 // statement, and return NULL. The handling of send statements is for
3757 // better error messages.
3759 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3762 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3763 // guard (var := expr.("type") using the literal keyword "type").
3766 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3767 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3769 const Token
* token
= this->peek_token();
3771 // An identifier follow by := is a SimpleVarDecl.
3772 if (token
->is_identifier())
3774 std::string identifier
= token
->identifier();
3775 bool is_exported
= token
->is_identifier_exported();
3776 Location location
= token
->location();
3778 token
= this->advance_token();
3779 if (token
->is_op(OPERATOR_COLONEQ
)
3780 || token
->is_op(OPERATOR_COMMA
))
3782 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3783 this->simple_var_decl_or_assignment(identifier
, location
,
3784 may_be_composite_lit
,
3786 (token
->is_op(OPERATOR_COLONEQ
)
3792 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3795 else if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
3797 Typed_identifier_list til
;
3798 this->range_clause_decl(&til
, p_range_clause
);
3802 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3803 may_be_composite_lit
,
3804 (p_type_switch
== NULL
3806 : &p_type_switch
->found
),
3808 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3810 p_type_switch
->name
.clear();
3811 p_type_switch
->location
= exp
->location();
3812 p_type_switch
->expr
= this->verify_not_sink(exp
);
3815 token
= this->peek_token();
3816 if (token
->is_op(OPERATOR_CHANOP
))
3818 this->send_stmt(this->verify_not_sink(exp
));
3819 if (return_exp
!= NULL
)
3822 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3823 || token
->is_op(OPERATOR_MINUSMINUS
))
3824 this->inc_dec_stat(this->verify_not_sink(exp
));
3825 else if (token
->is_op(OPERATOR_COMMA
)
3826 || token
->is_op(OPERATOR_EQ
))
3827 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3828 else if (token
->is_op(OPERATOR_PLUSEQ
)
3829 || token
->is_op(OPERATOR_MINUSEQ
)
3830 || token
->is_op(OPERATOR_OREQ
)
3831 || token
->is_op(OPERATOR_XOREQ
)
3832 || token
->is_op(OPERATOR_MULTEQ
)
3833 || token
->is_op(OPERATOR_DIVEQ
)
3834 || token
->is_op(OPERATOR_MODEQ
)
3835 || token
->is_op(OPERATOR_LSHIFTEQ
)
3836 || token
->is_op(OPERATOR_RSHIFTEQ
)
3837 || token
->is_op(OPERATOR_ANDEQ
)
3838 || token
->is_op(OPERATOR_BITCLEAREQ
))
3839 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
3841 else if (return_exp
!= NULL
)
3842 return this->verify_not_sink(exp
);
3845 exp
= this->verify_not_sink(exp
);
3847 if (token
->is_op(OPERATOR_COLONEQ
))
3849 if (!exp
->is_error_expression())
3850 error_at(token
->location(), "non-name on left side of %<:=%>");
3851 this->gogo_
->mark_locals_used();
3852 while (!token
->is_op(OPERATOR_SEMICOLON
)
3853 && !token
->is_eof())
3854 token
= this->advance_token();
3858 this->expression_stat(exp
);
3865 Parse::simple_stat_may_start_here()
3867 return this->expression_may_start_here();
3870 // Parse { Statement ";" } which is used in a few places. The list of
3871 // statements may end with a right curly brace, in which case the
3872 // semicolon may be omitted.
3875 Parse::statement_list()
3877 while (this->statement_may_start_here())
3879 this->statement(NULL
);
3880 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
3881 this->advance_token();
3882 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3886 if (!this->peek_token()->is_eof() || !saw_errors())
3887 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3888 if (!this->skip_past_error(OPERATOR_RCURLY
))
3895 Parse::statement_list_may_start_here()
3897 return this->statement_may_start_here();
3900 // ExpressionStat = Expression .
3903 Parse::expression_stat(Expression
* exp
)
3905 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
3908 // SendStmt = Channel "<-" Expression .
3909 // Channel = Expression .
3912 Parse::send_stmt(Expression
* channel
)
3914 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
3915 Location loc
= this->location();
3916 this->advance_token();
3917 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
3919 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
3920 this->gogo_
->add_statement(s
);
3923 // IncDecStat = Expression ( "++" | "--" ) .
3926 Parse::inc_dec_stat(Expression
* exp
)
3928 const Token
* token
= this->peek_token();
3930 // Lvalue maps require special handling.
3931 if (exp
->index_expression() != NULL
)
3932 exp
->index_expression()->set_is_lvalue();
3934 if (token
->is_op(OPERATOR_PLUSPLUS
))
3935 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
3936 else if (token
->is_op(OPERATOR_MINUSMINUS
))
3937 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
3940 this->advance_token();
3943 // Assignment = ExpressionList assign_op ExpressionList .
3945 // EXP is an expression that we have already parsed.
3947 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3948 // side may be a composite literal.
3950 // If RANGE_CLAUSE is not NULL, then this will recognize a
3954 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
3955 Range_clause
* p_range_clause
)
3957 Expression_list
* vars
;
3958 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
3960 vars
= new Expression_list();
3961 vars
->push_back(expr
);
3965 this->advance_token();
3966 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
3969 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
3972 // An assignment statement. LHS is the list of expressions which
3973 // appear on the left hand side.
3975 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3976 // side may be a composite literal.
3978 // If RANGE_CLAUSE is not NULL, then this will recognize a
3982 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
3983 Range_clause
* p_range_clause
)
3985 const Token
* token
= this->peek_token();
3986 if (!token
->is_op(OPERATOR_EQ
)
3987 && !token
->is_op(OPERATOR_PLUSEQ
)
3988 && !token
->is_op(OPERATOR_MINUSEQ
)
3989 && !token
->is_op(OPERATOR_OREQ
)
3990 && !token
->is_op(OPERATOR_XOREQ
)
3991 && !token
->is_op(OPERATOR_MULTEQ
)
3992 && !token
->is_op(OPERATOR_DIVEQ
)
3993 && !token
->is_op(OPERATOR_MODEQ
)
3994 && !token
->is_op(OPERATOR_LSHIFTEQ
)
3995 && !token
->is_op(OPERATOR_RSHIFTEQ
)
3996 && !token
->is_op(OPERATOR_ANDEQ
)
3997 && !token
->is_op(OPERATOR_BITCLEAREQ
))
3999 error_at(this->location(), "expected assignment operator");
4002 Operator op
= token
->op();
4003 Location location
= token
->location();
4005 token
= this->advance_token();
4007 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4009 if (op
!= OPERATOR_EQ
)
4010 error_at(this->location(), "range clause requires %<=%>");
4011 this->range_clause_expr(lhs
, p_range_clause
);
4015 Expression_list
* vals
= this->expression_list(NULL
, false,
4016 may_be_composite_lit
);
4018 // We've parsed everything; check for errors.
4019 if (lhs
== NULL
|| vals
== NULL
)
4021 for (Expression_list::const_iterator pe
= lhs
->begin();
4025 if ((*pe
)->is_error_expression())
4027 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4028 error_at((*pe
)->location(), "cannot use _ as value");
4030 for (Expression_list::const_iterator pe
= vals
->begin();
4034 if ((*pe
)->is_error_expression())
4038 // Map expressions act differently when they are lvalues.
4039 for (Expression_list::iterator plv
= lhs
->begin();
4042 if ((*plv
)->index_expression() != NULL
)
4043 (*plv
)->index_expression()->set_is_lvalue();
4045 Call_expression
* call
;
4046 Index_expression
* map_index
;
4047 Receive_expression
* receive
;
4048 Type_guard_expression
* type_guard
;
4049 if (lhs
->size() == vals
->size())
4052 if (lhs
->size() > 1)
4054 if (op
!= OPERATOR_EQ
)
4055 error_at(location
, "multiple values only permitted with %<=%>");
4056 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4060 if (op
== OPERATOR_EQ
)
4061 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4064 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4065 vals
->front(), location
);
4069 this->gogo_
->add_statement(s
);
4071 else if (vals
->size() == 1
4072 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4074 if (op
!= OPERATOR_EQ
)
4075 error_at(location
, "multiple results only permitted with %<=%>");
4076 call
->set_expected_result_count(lhs
->size());
4078 vals
= new Expression_list
;
4079 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4080 vals
->push_back(Expression::make_call_result(call
, i
));
4081 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4082 this->gogo_
->add_statement(s
);
4084 else if (lhs
->size() == 2
4085 && vals
->size() == 1
4086 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4088 if (op
!= OPERATOR_EQ
)
4089 error_at(location
, "two values from map requires %<=%>");
4090 Expression
* val
= lhs
->front();
4091 Expression
* present
= lhs
->back();
4092 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4093 map_index
, location
);
4094 this->gogo_
->add_statement(s
);
4096 else if (lhs
->size() == 1
4097 && vals
->size() == 2
4098 && (map_index
= lhs
->front()->index_expression()) != NULL
)
4100 if (op
!= OPERATOR_EQ
)
4101 error_at(location
, "assigning tuple to map index requires %<=%>");
4102 Expression
* val
= vals
->front();
4103 Expression
* should_set
= vals
->back();
4104 Statement
* s
= Statement::make_map_assignment(map_index
, val
, should_set
,
4106 this->gogo_
->add_statement(s
);
4108 else if (lhs
->size() == 2
4109 && vals
->size() == 1
4110 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4112 if (op
!= OPERATOR_EQ
)
4113 error_at(location
, "two values from receive requires %<=%>");
4114 Expression
* val
= lhs
->front();
4115 Expression
* success
= lhs
->back();
4116 Expression
* channel
= receive
->channel();
4117 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4120 this->gogo_
->add_statement(s
);
4122 else if (lhs
->size() == 2
4123 && vals
->size() == 1
4124 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4126 if (op
!= OPERATOR_EQ
)
4127 error_at(location
, "two values from type guard requires %<=%>");
4128 Expression
* val
= lhs
->front();
4129 Expression
* ok
= lhs
->back();
4130 Expression
* expr
= type_guard
->expr();
4131 Type
* type
= type_guard
->type();
4132 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4135 this->gogo_
->add_statement(s
);
4139 error_at(location
, "number of variables does not match number of values");
4143 // GoStat = "go" Expression .
4144 // DeferStat = "defer" Expression .
4147 Parse::go_or_defer_stat()
4149 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4150 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4151 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4152 Location stat_location
= this->location();
4154 this->advance_token();
4155 Location expr_location
= this->location();
4157 bool is_parenthesized
= false;
4158 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4160 Call_expression
* call_expr
= expr
->call_expression();
4161 if (is_parenthesized
|| call_expr
== NULL
)
4163 error_at(expr_location
, "argument to go/defer must be function call");
4167 // Make it easier to simplify go/defer statements by putting every
4168 // statement in its own block.
4169 this->gogo_
->start_block(stat_location
);
4172 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4174 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4175 this->gogo_
->add_statement(stat
);
4176 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4180 // ReturnStat = "return" [ ExpressionList ] .
4183 Parse::return_stat()
4185 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4186 Location location
= this->location();
4187 this->advance_token();
4188 Expression_list
* vals
= NULL
;
4189 if (this->expression_may_start_here())
4190 vals
= this->expression_list(NULL
, false, true);
4191 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4194 && this->gogo_
->current_function()->func_value()->results_are_named())
4196 Named_object
* function
= this->gogo_
->current_function();
4197 Function::Results
* results
= function
->func_value()->result_variables();
4198 for (Function::Results::const_iterator p
= results
->begin();
4199 p
!= results
->end();
4202 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4204 go_assert(saw_errors());
4205 else if (!no
->is_result_variable())
4206 error_at(location
, "%qs is shadowed during return",
4207 (*p
)->message_name().c_str());
4212 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4213 // [ "else" ( IfStmt | Block ) ] .
4218 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4219 Location location
= this->location();
4220 this->advance_token();
4222 this->gogo_
->start_block(location
);
4224 bool saw_simple_stat
= false;
4225 Expression
* cond
= NULL
;
4226 bool saw_send_stmt
= false;
4227 if (this->simple_stat_may_start_here())
4229 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4230 saw_simple_stat
= true;
4232 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4234 // The SimpleStat is an expression statement.
4235 this->expression_stat(cond
);
4240 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4241 this->advance_token();
4242 else if (saw_simple_stat
)
4245 error_at(this->location(),
4246 ("send statement used as value; "
4247 "use select for non-blocking send"));
4249 error_at(this->location(),
4250 "expected %<;%> after statement in if expression");
4251 if (!this->expression_may_start_here())
4252 cond
= Expression::make_error(this->location());
4254 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4256 error_at(this->location(),
4257 "missing condition in if statement");
4258 cond
= Expression::make_error(this->location());
4261 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4264 // Check for the easy error of a newline before starting the block.
4265 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4267 Location semi_loc
= this->location();
4268 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4269 error_at(semi_loc
, "missing %<{%> after if clause");
4270 // Otherwise we will get an error when we call this->block
4274 this->gogo_
->start_block(this->location());
4275 Location end_loc
= this->block();
4276 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4278 // Check for the easy error of a newline before "else".
4279 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4281 Location semi_loc
= this->location();
4282 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4283 error_at(this->location(),
4284 "unexpected semicolon or newline before %<else%>");
4286 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4290 Block
* else_block
= NULL
;
4291 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4293 this->gogo_
->start_block(this->location());
4294 const Token
* token
= this->advance_token();
4295 if (token
->is_keyword(KEYWORD_IF
))
4297 else if (token
->is_op(OPERATOR_LCURLY
))
4301 error_at(this->location(), "expected %<if%> or %<{%>");
4302 this->statement(NULL
);
4304 else_block
= this->gogo_
->finish_block(this->location());
4307 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4311 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4315 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4316 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4317 // "{" { ExprCaseClause } "}" .
4318 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4319 // "{" { TypeCaseClause } "}" .
4320 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4323 Parse::switch_stat(Label
* label
)
4325 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4326 Location location
= this->location();
4327 this->advance_token();
4329 this->gogo_
->start_block(location
);
4331 bool saw_simple_stat
= false;
4332 Expression
* switch_val
= NULL
;
4334 Type_switch type_switch
;
4335 bool have_type_switch_block
= false;
4336 if (this->simple_stat_may_start_here())
4338 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4340 saw_simple_stat
= true;
4342 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4344 // The SimpleStat is an expression statement.
4345 this->expression_stat(switch_val
);
4348 if (switch_val
== NULL
&& !type_switch
.found
)
4350 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4351 this->advance_token();
4352 else if (saw_simple_stat
)
4355 error_at(this->location(),
4356 ("send statement used as value; "
4357 "use select for non-blocking send"));
4359 error_at(this->location(),
4360 "expected %<;%> after statement in switch expression");
4362 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4364 if (this->peek_token()->is_identifier())
4366 const Token
* token
= this->peek_token();
4367 std::string identifier
= token
->identifier();
4368 bool is_exported
= token
->is_identifier_exported();
4369 Location id_loc
= token
->location();
4371 token
= this->advance_token();
4372 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4373 this->unget_token(Token::make_identifier_token(identifier
,
4378 // This must be a TypeSwitchGuard. It is in a
4379 // different block from any initial SimpleStat.
4380 if (saw_simple_stat
)
4382 this->gogo_
->start_block(id_loc
);
4383 have_type_switch_block
= true;
4386 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4388 if (!type_switch
.found
)
4390 if (switch_val
== NULL
4391 || !switch_val
->is_error_expression())
4393 error_at(id_loc
, "expected type switch assignment");
4394 switch_val
= Expression::make_error(id_loc
);
4399 if (switch_val
== NULL
&& !type_switch
.found
)
4401 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4402 &type_switch
.found
, NULL
);
4403 if (type_switch
.found
)
4405 type_switch
.name
.clear();
4406 type_switch
.expr
= switch_val
;
4407 type_switch
.location
= switch_val
->location();
4413 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4415 Location token_loc
= this->location();
4416 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4417 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4418 error_at(token_loc
, "missing %<{%> after switch clause");
4419 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4421 error_at(token_loc
, "invalid variable name");
4422 this->advance_token();
4423 this->expression(PRECEDENCE_NORMAL
, false, false,
4424 &type_switch
.found
, NULL
);
4425 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4426 this->advance_token();
4427 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4429 if (have_type_switch_block
)
4430 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4432 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4436 if (type_switch
.found
)
4437 type_switch
.expr
= Expression::make_error(location
);
4441 error_at(this->location(), "expected %<{%>");
4442 if (have_type_switch_block
)
4443 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4445 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4450 this->advance_token();
4452 Statement
* statement
;
4453 if (type_switch
.found
)
4454 statement
= this->type_switch_body(label
, type_switch
, location
);
4456 statement
= this->expr_switch_body(label
, switch_val
, location
);
4458 if (statement
!= NULL
)
4459 this->gogo_
->add_statement(statement
);
4461 if (have_type_switch_block
)
4462 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4465 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4469 // The body of an expression switch.
4470 // "{" { ExprCaseClause } "}"
4473 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4476 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4479 this->push_break_statement(statement
, label
);
4481 Case_clauses
* case_clauses
= new Case_clauses();
4482 bool saw_default
= false;
4483 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4485 if (this->peek_token()->is_eof())
4488 error_at(this->location(), "missing %<}%>");
4491 this->expr_case_clause(case_clauses
, &saw_default
);
4493 this->advance_token();
4495 statement
->add_clauses(case_clauses
);
4497 this->pop_break_statement();
4502 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4503 // FallthroughStat = "fallthrough" .
4506 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4508 Location location
= this->location();
4510 bool is_default
= false;
4511 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4513 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4516 error_at(this->location(), "expected %<:%>");
4520 this->advance_token();
4522 Block
* statements
= NULL
;
4523 if (this->statement_list_may_start_here())
4525 this->gogo_
->start_block(this->location());
4526 this->statement_list();
4527 statements
= this->gogo_
->finish_block(this->location());
4530 bool is_fallthrough
= false;
4531 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4533 Location fallthrough_loc
= this->location();
4534 is_fallthrough
= true;
4535 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4536 this->advance_token();
4537 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4538 error_at(fallthrough_loc
, _("cannot fallthrough final case in switch"));
4545 error_at(location
, "multiple defaults in switch");
4548 *saw_default
= true;
4551 if (is_default
|| vals
!= NULL
)
4552 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4555 // ExprSwitchCase = "case" ExpressionList | "default" .
4558 Parse::expr_switch_case(bool* is_default
)
4560 const Token
* token
= this->peek_token();
4561 if (token
->is_keyword(KEYWORD_CASE
))
4563 this->advance_token();
4564 return this->expression_list(NULL
, false, true);
4566 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4568 this->advance_token();
4575 error_at(this->location(), "expected %<case%> or %<default%>");
4576 if (!token
->is_op(OPERATOR_RCURLY
))
4577 this->advance_token();
4582 // The body of a type switch.
4583 // "{" { TypeCaseClause } "}" .
4586 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4589 Named_object
* switch_no
= NULL
;
4590 if (!type_switch
.name
.empty())
4592 if (Gogo::is_sink_name(type_switch
.name
))
4593 error_at(type_switch
.location
,
4594 "no new variables on left side of %<:=%>");
4597 Variable
* switch_var
= new Variable(NULL
, type_switch
.expr
, false,
4599 type_switch
.location
);
4600 switch_no
= this->gogo_
->add_variable(type_switch
.name
, switch_var
);
4604 Type_switch_statement
* statement
=
4605 Statement::make_type_switch_statement(switch_no
,
4611 this->push_break_statement(statement
, label
);
4613 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4614 bool saw_default
= false;
4615 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4617 if (this->peek_token()->is_eof())
4619 error_at(this->location(), "missing %<}%>");
4622 this->type_case_clause(switch_no
, case_clauses
, &saw_default
);
4624 this->advance_token();
4626 statement
->add_clauses(case_clauses
);
4628 this->pop_break_statement();
4633 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4636 Parse::type_case_clause(Named_object
* switch_no
, Type_case_clauses
* clauses
,
4639 Location location
= this->location();
4641 std::vector
<Type
*> types
;
4642 bool is_default
= false;
4643 this->type_switch_case(&types
, &is_default
);
4645 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4646 error_at(this->location(), "expected %<:%>");
4648 this->advance_token();
4650 Block
* statements
= NULL
;
4651 if (this->statement_list_may_start_here())
4653 this->gogo_
->start_block(this->location());
4654 if (switch_no
!= NULL
&& types
.size() == 1)
4656 Type
* type
= types
.front();
4657 Expression
* init
= Expression::make_var_reference(switch_no
,
4659 init
= Expression::make_type_guard(init
, type
, location
);
4660 Variable
* v
= new Variable(type
, init
, false, false, false,
4662 v
->set_is_type_switch_var();
4663 Named_object
* no
= this->gogo_
->add_variable(switch_no
->name(), v
);
4665 // We don't want to issue an error if the compiler
4666 // introduced special variable is not used. Instead we want
4667 // to issue an error if the variable defined by the switch
4668 // is not used. That is handled via type_switch_vars_ and
4669 // Parse::mark_var_used.
4671 this->type_switch_vars_
[no
] = switch_no
;
4673 this->statement_list();
4674 statements
= this->gogo_
->finish_block(this->location());
4677 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4679 error_at(this->location(),
4680 "fallthrough is not permitted in a type switch");
4681 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4682 this->advance_token();
4687 go_assert(types
.empty());
4690 error_at(location
, "multiple defaults in type switch");
4693 *saw_default
= true;
4694 clauses
->add(NULL
, false, true, statements
, location
);
4696 else if (!types
.empty())
4698 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4699 p
+ 1 != types
.end();
4701 clauses
->add(*p
, true, false, NULL
, location
);
4702 clauses
->add(types
.back(), false, false, statements
, location
);
4705 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4708 // TypeSwitchCase = "case" type | "default"
4710 // We accept a comma separated list of types.
4713 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4715 const Token
* token
= this->peek_token();
4716 if (token
->is_keyword(KEYWORD_CASE
))
4718 this->advance_token();
4721 Type
* t
= this->type();
4723 if (!t
->is_error_type())
4724 types
->push_back(t
);
4727 this->gogo_
->mark_locals_used();
4728 token
= this->peek_token();
4729 while (!token
->is_op(OPERATOR_COLON
)
4730 && !token
->is_op(OPERATOR_COMMA
)
4731 && !token
->is_op(OPERATOR_RCURLY
)
4732 && !token
->is_eof())
4733 token
= this->advance_token();
4736 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4738 this->advance_token();
4741 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4743 this->advance_token();
4748 error_at(this->location(), "expected %<case%> or %<default%>");
4749 if (!token
->is_op(OPERATOR_RCURLY
))
4750 this->advance_token();
4754 // SelectStat = "select" "{" { CommClause } "}" .
4757 Parse::select_stat(Label
* label
)
4759 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4760 Location location
= this->location();
4761 const Token
* token
= this->advance_token();
4763 if (!token
->is_op(OPERATOR_LCURLY
))
4765 Location token_loc
= token
->location();
4766 if (token
->is_op(OPERATOR_SEMICOLON
)
4767 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4768 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4771 error_at(this->location(), "expected %<{%>");
4775 this->advance_token();
4777 Select_statement
* statement
= Statement::make_select_statement(location
);
4779 this->push_break_statement(statement
, label
);
4781 Select_clauses
* select_clauses
= new Select_clauses();
4782 bool saw_default
= false;
4783 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4785 if (this->peek_token()->is_eof())
4787 error_at(this->location(), "expected %<}%>");
4790 this->comm_clause(select_clauses
, &saw_default
);
4793 this->advance_token();
4795 statement
->add_clauses(select_clauses
);
4797 this->pop_break_statement();
4799 this->gogo_
->add_statement(statement
);
4802 // CommClause = CommCase ":" { Statement ";" } .
4805 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4807 Location location
= this->location();
4808 bool is_send
= false;
4809 Expression
* channel
= NULL
;
4810 Expression
* val
= NULL
;
4811 Expression
* closed
= NULL
;
4812 std::string varname
;
4813 std::string closedname
;
4814 bool is_default
= false;
4815 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4816 &varname
, &closedname
, &is_default
);
4820 && closedname
.empty()
4822 && val
->index_expression() != NULL
)
4823 val
->index_expression()->set_is_lvalue();
4825 if (this->peek_token()->is_op(OPERATOR_COLON
))
4826 this->advance_token();
4828 error_at(this->location(), "expected colon");
4830 this->gogo_
->start_block(this->location());
4832 Named_object
* var
= NULL
;
4833 if (!varname
.empty())
4835 // FIXME: LOCATION is slightly wrong here.
4836 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
4838 v
->set_type_from_chan_element();
4839 var
= this->gogo_
->add_variable(varname
, v
);
4842 Named_object
* closedvar
= NULL
;
4843 if (!closedname
.empty())
4845 // FIXME: LOCATION is slightly wrong here.
4846 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
4847 false, false, false, location
);
4848 closedvar
= this->gogo_
->add_variable(closedname
, v
);
4851 this->statement_list();
4853 Block
* statements
= this->gogo_
->finish_block(this->location());
4859 error_at(location
, "multiple defaults in select");
4862 *saw_default
= true;
4866 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
4867 statements
, location
);
4868 else if (statements
!= NULL
)
4870 // Add the statements to make sure that any names they define
4872 this->gogo_
->add_block(statements
, location
);
4876 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4879 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
4880 Expression
** closed
, std::string
* varname
,
4881 std::string
* closedname
, bool* is_default
)
4883 const Token
* token
= this->peek_token();
4884 if (token
->is_keyword(KEYWORD_DEFAULT
))
4886 this->advance_token();
4889 else if (token
->is_keyword(KEYWORD_CASE
))
4891 this->advance_token();
4892 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
4898 error_at(this->location(), "expected %<case%> or %<default%>");
4899 if (!token
->is_op(OPERATOR_RCURLY
))
4900 this->advance_token();
4907 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4908 // RecvExpr = Expression .
4911 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
4912 Expression
** closed
, std::string
* varname
,
4913 std::string
* closedname
)
4915 const Token
* token
= this->peek_token();
4916 bool saw_comma
= false;
4917 bool closed_is_id
= false;
4918 if (token
->is_identifier())
4920 Gogo
* gogo
= this->gogo_
;
4921 std::string recv_var
= token
->identifier();
4922 bool is_rv_exported
= token
->is_identifier_exported();
4923 Location recv_var_loc
= token
->location();
4924 token
= this->advance_token();
4925 if (token
->is_op(OPERATOR_COLONEQ
))
4928 this->advance_token();
4929 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
4931 Receive_expression
* re
= e
->receive_expression();
4934 if (!e
->is_error_expression())
4935 error_at(this->location(), "expected receive expression");
4938 if (recv_var
== "_")
4940 error_at(recv_var_loc
,
4941 "no new variables on left side of %<:=%>");
4942 recv_var
= Gogo::erroneous_name();
4945 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
4946 *channel
= re
->channel();
4949 else if (token
->is_op(OPERATOR_COMMA
))
4951 token
= this->advance_token();
4952 if (token
->is_identifier())
4954 std::string recv_closed
= token
->identifier();
4955 bool is_rc_exported
= token
->is_identifier_exported();
4956 Location recv_closed_loc
= token
->location();
4957 closed_is_id
= true;
4959 token
= this->advance_token();
4960 if (token
->is_op(OPERATOR_COLONEQ
))
4962 // case rv, rc := <-c:
4963 this->advance_token();
4964 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
4966 Receive_expression
* re
= e
->receive_expression();
4969 if (!e
->is_error_expression())
4970 error_at(this->location(),
4971 "expected receive expression");
4974 if (recv_var
== "_" && recv_closed
== "_")
4976 error_at(recv_var_loc
,
4977 "no new variables on left side of %<:=%>");
4978 recv_var
= Gogo::erroneous_name();
4981 if (recv_var
!= "_")
4982 *varname
= gogo
->pack_hidden_name(recv_var
,
4984 if (recv_closed
!= "_")
4985 *closedname
= gogo
->pack_hidden_name(recv_closed
,
4987 *channel
= re
->channel();
4991 this->unget_token(Token::make_identifier_token(recv_closed
,
4996 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
4998 recv_var_loc
, true);
5002 this->unget_token(Token::make_identifier_token(recv_var
,
5007 // If SAW_COMMA is false, then we are looking at the start of the
5008 // send or receive expression. If SAW_COMMA is true, then *VAL is
5009 // set and we just read a comma.
5012 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5013 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5018 this->advance_token();
5019 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5021 // The next token should be ':'. If it is '<-', then we have
5023 // which is to say, send on a channel received from a channel.
5024 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5027 e
= Expression::make_receive(*channel
, (*channel
)->location());
5030 if (this->peek_token()->is_op(OPERATOR_EQ
))
5032 if (!this->advance_token()->is_op(OPERATOR_CHANOP
))
5034 error_at(this->location(), "missing %<<-%>");
5038 this->advance_token();
5039 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5043 // *VAL is already set.
5044 if (!e
->is_sink_expression())
5050 if (!e
->is_sink_expression())
5059 error_at(this->location(), "expected %<=%> or %<:=%>");
5061 error_at(this->location(), "expected %<=%>");
5065 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5069 *channel
= this->verify_not_sink(e
);
5070 this->advance_token();
5071 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5075 error_at(this->location(), "expected %<<-%> or %<=%>");
5079 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5080 // Condition = Expression .
5083 Parse::for_stat(Label
* label
)
5085 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5086 Location location
= this->location();
5087 const Token
* token
= this->advance_token();
5089 // Open a block to hold any variables defined in the init statement
5090 // of the for statement.
5091 this->gogo_
->start_block(location
);
5094 Expression
* cond
= NULL
;
5096 Range_clause range_clause
;
5098 if (!token
->is_op(OPERATOR_LCURLY
))
5100 if (token
->is_keyword(KEYWORD_VAR
))
5102 error_at(this->location(),
5103 "var declaration not allowed in for initializer");
5107 if (token
->is_op(OPERATOR_SEMICOLON
))
5108 this->for_clause(&cond
, &post
);
5111 // We might be looking at a Condition, an InitStat, or a
5114 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5115 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5117 if (cond
== NULL
&& !range_clause
.found
)
5120 error_at(this->location(),
5121 ("send statement used as value; "
5122 "use select for non-blocking send"));
5124 error_at(this->location(), "parse error in for statement");
5129 if (range_clause
.found
)
5130 error_at(this->location(), "parse error after range clause");
5134 // COND is actually an expression statement for
5135 // InitStat at the start of a ForClause.
5136 this->expression_stat(cond
);
5140 this->for_clause(&cond
, &post
);
5145 // Check for the easy error of a newline before starting the block.
5146 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5148 Location semi_loc
= this->location();
5149 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5150 error_at(semi_loc
, "missing %<{%> after for clause");
5151 // Otherwise we will get an error when we call this->block
5155 // Build the For_statement and note that it is the current target
5156 // for break and continue statements.
5158 For_statement
* sfor
;
5159 For_range_statement
* srange
;
5161 if (!range_clause
.found
)
5163 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5169 srange
= Statement::make_for_range_statement(range_clause
.index
,
5177 this->push_break_statement(s
, label
);
5178 this->push_continue_statement(s
, label
);
5180 // Gather the block of statements in the loop and add them to the
5183 this->gogo_
->start_block(this->location());
5184 Location end_loc
= this->block();
5185 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5188 sfor
->add_statements(statements
);
5190 srange
->add_statements(statements
);
5192 // This is no longer the break/continue target.
5193 this->pop_break_statement();
5194 this->pop_continue_statement();
5196 // Add the For_statement to the list of statements, and close out
5197 // the block we started to hold any variables defined in the for
5200 this->gogo_
->add_statement(s
);
5202 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5206 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5207 // InitStat = SimpleStat .
5208 // PostStat = SimpleStat .
5210 // We have already read InitStat at this point.
5213 Parse::for_clause(Expression
** cond
, Block
** post
)
5215 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5216 this->advance_token();
5217 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5219 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5221 error_at(this->location(), "missing %<{%> after for clause");
5227 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5228 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5229 error_at(this->location(), "expected semicolon");
5231 this->advance_token();
5233 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5237 this->gogo_
->start_block(this->location());
5238 this->simple_stat(false, NULL
, NULL
, NULL
);
5239 *post
= this->gogo_
->finish_block(this->location());
5243 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5245 // This is the := version. It is called with a list of identifiers.
5248 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5249 Range_clause
* p_range_clause
)
5251 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5252 Location location
= this->location();
5254 p_range_clause
->found
= true;
5256 if (til
->size() > 2)
5257 error_at(this->location(), "too many variables for range clause");
5259 this->advance_token();
5260 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5262 p_range_clause
->range
= expr
;
5267 bool any_new
= false;
5269 const Typed_identifier
* pti
= &til
->front();
5270 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5272 if (any_new
&& no
->is_variable())
5273 no
->var_value()->set_type_from_range_index();
5274 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5276 if (til
->size() == 1)
5277 p_range_clause
->value
= NULL
;
5281 bool is_new
= false;
5282 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5283 if (is_new
&& no
->is_variable())
5284 no
->var_value()->set_type_from_range_value();
5287 if (!Gogo::is_sink_name(pti
->name()))
5288 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5292 error_at(location
, "variables redeclared but no variable is new");
5295 // The = version of RangeClause. This is called with a list of
5299 Parse::range_clause_expr(const Expression_list
* vals
,
5300 Range_clause
* p_range_clause
)
5302 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5304 p_range_clause
->found
= true;
5306 go_assert(vals
->size() >= 1);
5307 if (vals
->size() > 2)
5308 error_at(this->location(), "too many variables for range clause");
5310 this->advance_token();
5311 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5317 p_range_clause
->index
= vals
->front();
5318 if (vals
->size() == 1)
5319 p_range_clause
->value
= NULL
;
5321 p_range_clause
->value
= vals
->back();
5324 // Push a statement on the break stack.
5327 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5329 if (this->break_stack_
== NULL
)
5330 this->break_stack_
= new Bc_stack();
5331 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5334 // Push a statement on the continue stack.
5337 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5339 if (this->continue_stack_
== NULL
)
5340 this->continue_stack_
= new Bc_stack();
5341 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5344 // Pop the break stack.
5347 Parse::pop_break_statement()
5349 this->break_stack_
->pop_back();
5352 // Pop the continue stack.
5355 Parse::pop_continue_statement()
5357 this->continue_stack_
->pop_back();
5360 // Find a break or continue statement given a label name.
5363 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5365 if (bc_stack
== NULL
)
5367 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5368 p
!= bc_stack
->rend();
5371 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5373 p
->second
->set_is_used();
5380 // BreakStat = "break" [ identifier ] .
5385 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5386 Location location
= this->location();
5388 const Token
* token
= this->advance_token();
5389 Statement
* enclosing
;
5390 if (!token
->is_identifier())
5392 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5394 error_at(this->location(),
5395 "break statement not within for or switch or select");
5398 enclosing
= this->break_stack_
->back().first
;
5402 enclosing
= this->find_bc_statement(this->break_stack_
,
5403 token
->identifier());
5404 if (enclosing
== NULL
)
5406 // If there is a label with this name, mark it as used to
5407 // avoid a useless error about an unused label.
5408 this->gogo_
->add_label_reference(token
->identifier(),
5409 Linemap::unknown_location(), false);
5411 error_at(token
->location(), "invalid break label %qs",
5412 Gogo::message_name(token
->identifier()).c_str());
5413 this->advance_token();
5416 this->advance_token();
5419 Unnamed_label
* label
;
5420 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5421 label
= enclosing
->for_statement()->break_label();
5422 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5423 label
= enclosing
->for_range_statement()->break_label();
5424 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5425 label
= enclosing
->switch_statement()->break_label();
5426 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5427 label
= enclosing
->type_switch_statement()->break_label();
5428 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5429 label
= enclosing
->select_statement()->break_label();
5433 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5437 // ContinueStat = "continue" [ identifier ] .
5440 Parse::continue_stat()
5442 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5443 Location location
= this->location();
5445 const Token
* token
= this->advance_token();
5446 Statement
* enclosing
;
5447 if (!token
->is_identifier())
5449 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5451 error_at(this->location(), "continue statement not within for");
5454 enclosing
= this->continue_stack_
->back().first
;
5458 enclosing
= this->find_bc_statement(this->continue_stack_
,
5459 token
->identifier());
5460 if (enclosing
== NULL
)
5462 // If there is a label with this name, mark it as used to
5463 // avoid a useless error about an unused label.
5464 this->gogo_
->add_label_reference(token
->identifier(),
5465 Linemap::unknown_location(), false);
5467 error_at(token
->location(), "invalid continue label %qs",
5468 Gogo::message_name(token
->identifier()).c_str());
5469 this->advance_token();
5472 this->advance_token();
5475 Unnamed_label
* label
;
5476 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5477 label
= enclosing
->for_statement()->continue_label();
5478 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5479 label
= enclosing
->for_range_statement()->continue_label();
5483 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5487 // GotoStat = "goto" identifier .
5492 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5493 Location location
= this->location();
5494 const Token
* token
= this->advance_token();
5495 if (!token
->is_identifier())
5496 error_at(this->location(), "expected label for goto");
5499 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5501 Statement
* s
= Statement::make_goto_statement(label
, location
);
5502 this->gogo_
->add_statement(s
);
5503 this->advance_token();
5507 // PackageClause = "package" PackageName .
5510 Parse::package_clause()
5512 const Token
* token
= this->peek_token();
5513 Location location
= token
->location();
5515 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5517 error_at(this->location(), "program must start with package clause");
5522 token
= this->advance_token();
5523 if (token
->is_identifier())
5525 name
= token
->identifier();
5528 error_at(this->location(), "invalid package name _");
5529 name
= Gogo::erroneous_name();
5531 this->advance_token();
5535 error_at(this->location(), "package name must be an identifier");
5539 this->gogo_
->set_package_name(name
, location
);
5542 // ImportDecl = "import" Decl<ImportSpec> .
5545 Parse::import_decl()
5547 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5548 this->advance_token();
5549 this->decl(&Parse::import_spec
, NULL
);
5552 // ImportSpec = [ "." | PackageName ] PackageFileName .
5555 Parse::import_spec(void*)
5557 const Token
* token
= this->peek_token();
5558 Location location
= token
->location();
5560 std::string local_name
;
5561 bool is_local_name_exported
= false;
5562 if (token
->is_op(OPERATOR_DOT
))
5565 token
= this->advance_token();
5567 else if (token
->is_identifier())
5569 local_name
= token
->identifier();
5570 is_local_name_exported
= token
->is_identifier_exported();
5571 token
= this->advance_token();
5574 if (!token
->is_string())
5576 error_at(this->location(), "import statement not a string");
5577 this->advance_token();
5581 this->gogo_
->import_package(token
->string_value(), local_name
,
5582 is_local_name_exported
, location
);
5584 this->advance_token();
5587 // SourceFile = PackageClause ";" { ImportDecl ";" }
5588 // { TopLevelDecl ";" } .
5593 this->package_clause();
5595 const Token
* token
= this->peek_token();
5596 if (token
->is_op(OPERATOR_SEMICOLON
))
5597 token
= this->advance_token();
5599 error_at(this->location(),
5600 "expected %<;%> or newline after package clause");
5602 while (token
->is_keyword(KEYWORD_IMPORT
))
5604 this->import_decl();
5605 token
= this->peek_token();
5606 if (token
->is_op(OPERATOR_SEMICOLON
))
5607 token
= this->advance_token();
5609 error_at(this->location(),
5610 "expected %<;%> or newline after import declaration");
5613 while (!token
->is_eof())
5615 if (this->declaration_may_start_here())
5616 this->declaration();
5619 error_at(this->location(), "expected declaration");
5620 this->gogo_
->mark_locals_used();
5622 this->advance_token();
5623 while (!this->peek_token()->is_eof()
5624 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5625 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5626 if (!this->peek_token()->is_eof()
5627 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5628 this->advance_token();
5630 token
= this->peek_token();
5631 if (token
->is_op(OPERATOR_SEMICOLON
))
5632 token
= this->advance_token();
5633 else if (!token
->is_eof() || !saw_errors())
5635 if (token
->is_op(OPERATOR_CHANOP
))
5636 error_at(this->location(),
5637 ("send statement used as value; "
5638 "use select for non-blocking send"));
5640 error_at(this->location(),
5641 "expected %<;%> or newline after top level declaration");
5642 this->skip_past_error(OPERATOR_INVALID
);
5647 // Reset the current iota value.
5655 // Return the current iota value.
5663 // Increment the current iota value.
5666 Parse::increment_iota()
5671 // Skip forward to a semicolon or OP. OP will normally be
5672 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5673 // past it and return. If we find OP, it will be the next token to
5674 // read. Return true if we are OK, false if we found EOF.
5677 Parse::skip_past_error(Operator op
)
5679 this->gogo_
->mark_locals_used();
5680 const Token
* token
= this->peek_token();
5681 while (!token
->is_op(op
))
5683 if (token
->is_eof())
5685 if (token
->is_op(OPERATOR_SEMICOLON
))
5687 this->advance_token();
5690 token
= this->advance_token();
5695 // Check that an expression is not a sink.
5698 Parse::verify_not_sink(Expression
* expr
)
5700 if (expr
->is_sink_expression())
5702 error_at(expr
->location(), "cannot use _ as value");
5703 expr
= Expression::make_error(expr
->location());
5706 // If this can not be a sink, and it is a variable, then we are
5707 // using the variable, not just assigning to it.
5708 Var_expression
* ve
= expr
->var_expression();
5710 this->mark_var_used(ve
->named_object());
5715 // Mark a variable as used.
5718 Parse::mark_var_used(Named_object
* no
)
5720 if (no
->is_variable())
5722 no
->var_value()->set_is_used();
5724 // When a type switch uses := to define a variable, then for
5725 // each case with a single type we introduce a new variable with
5726 // the appropriate type. When we do, if the newly introduced
5727 // variable is used, then the type switch variable is used.
5728 Type_switch_vars::iterator p
= this->type_switch_vars_
.find(no
);
5729 if (p
!= this->type_switch_vars_
.end())
5730 p
->second
->var_value()->set_is_used();