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()->note_usage();
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
->note_usage();
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_z(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
);
2529 mpc_init2(val
, mpc_precision
);
2530 mpc_set_fr_fr(val
, zero
, *token
->imaginary_value(), MPC_RNDNN
);
2532 ret
= Expression::make_complex(&val
, NULL
, token
->location());
2534 this->advance_token();
2538 case Token::TOKEN_KEYWORD
:
2539 switch (token
->keyword())
2542 return this->function_lit();
2544 case KEYWORD_INTERFACE
:
2546 case KEYWORD_STRUCT
:
2548 Location location
= token
->location();
2549 return Expression::make_type(this->type(), location
);
2556 case Token::TOKEN_OPERATOR
:
2557 if (token
->is_op(OPERATOR_LPAREN
))
2559 this->advance_token();
2560 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2562 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2563 error_at(this->location(), "missing %<)%>");
2565 this->advance_token();
2566 if (is_parenthesized
!= NULL
)
2567 *is_parenthesized
= true;
2570 else if (token
->is_op(OPERATOR_LSQUARE
))
2572 // Here we call array_type directly, as this is the only
2573 // case where an ellipsis is permitted for an array type.
2574 Location location
= token
->location();
2575 return Expression::make_type(this->array_type(true), location
);
2583 error_at(this->location(), "expected operand");
2584 return Expression::make_error(this->location());
2587 // Handle a reference to a variable in an enclosing function. We add
2588 // it to a list of such variables. We return a reference to a field
2589 // in a struct which will be passed on the static chain when calling
2590 // the current function.
2593 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2596 go_assert(var
->is_variable() || var
->is_result_variable());
2598 this->mark_var_used(var
);
2600 Named_object
* this_function
= this->gogo_
->current_function();
2601 Named_object
* closure
= this_function
->func_value()->closure_var();
2603 // The last argument to the Enclosing_var constructor is the index
2604 // of this variable in the closure. We add 1 to the current number
2605 // of enclosed variables, because the first field in the closure
2606 // points to the function code.
2607 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2608 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2609 this->enclosing_vars_
.insert(ev
);
2612 // This is a variable we have not seen before. Add a new field
2613 // to the closure type.
2614 this_function
->func_value()->add_closure_field(var
, location
);
2617 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2619 closure_ref
= Expression::make_unary(OPERATOR_MULT
, closure_ref
, location
);
2621 // The closure structure holds pointers to the variables, so we need
2622 // to introduce an indirection.
2623 Expression
* e
= Expression::make_field_reference(closure_ref
,
2626 e
= Expression::make_unary(OPERATOR_MULT
, e
, location
);
2630 // CompositeLit = LiteralType LiteralValue .
2631 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2632 // SliceType | MapType | TypeName .
2633 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2634 // ElementList = Element { "," Element } .
2635 // Element = [ Key ":" ] Value .
2636 // Key = FieldName | ElementIndex .
2637 // FieldName = identifier .
2638 // ElementIndex = Expression .
2639 // Value = Expression | LiteralValue .
2641 // We have already seen the type if there is one, and we are now
2642 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2643 // will be seen here as an array type whose length is "nil". The
2644 // DEPTH parameter is non-zero if this is an embedded composite
2645 // literal and the type was omitted. It gives the number of steps up
2646 // to the type which was provided. E.g., in [][]int{{1}} it will be
2647 // 1. In [][][]int{{{1}}} it will be 2.
2650 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2652 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2653 this->advance_token();
2655 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2657 this->advance_token();
2658 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2662 bool has_keys
= false;
2663 bool all_are_names
= true;
2664 Expression_list
* vals
= new Expression_list
;
2668 bool is_type_omitted
= false;
2669 bool is_name
= false;
2671 const Token
* token
= this->peek_token();
2673 if (token
->is_identifier())
2675 std::string identifier
= token
->identifier();
2676 bool is_exported
= token
->is_identifier_exported();
2677 Location location
= token
->location();
2679 if (this->advance_token()->is_op(OPERATOR_COLON
))
2681 // This may be a field name. We don't know for sure--it
2682 // could also be an expression for an array index. We
2683 // don't want to parse it as an expression because may
2684 // trigger various errors, e.g., if this identifier
2685 // happens to be the name of a package.
2686 Gogo
* gogo
= this->gogo_
;
2687 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2694 this->unget_token(Token::make_identifier_token(identifier
,
2697 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2701 else if (!token
->is_op(OPERATOR_LCURLY
))
2702 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2705 // This must be a composite literal inside another composite
2706 // literal, with the type omitted for the inner one.
2707 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2708 is_type_omitted
= true;
2711 token
= this->peek_token();
2712 if (!token
->is_op(OPERATOR_COLON
))
2715 vals
->push_back(NULL
);
2720 if (is_type_omitted
&& !val
->is_error_expression())
2722 error_at(this->location(), "unexpected %<:%>");
2723 val
= Expression::make_error(this->location());
2726 this->advance_token();
2728 if (!has_keys
&& !vals
->empty())
2730 Expression_list
* newvals
= new Expression_list
;
2731 for (Expression_list::const_iterator p
= vals
->begin();
2735 newvals
->push_back(NULL
);
2736 newvals
->push_back(*p
);
2743 if (val
->unknown_expression() != NULL
)
2744 val
->unknown_expression()->set_is_composite_literal_key();
2746 vals
->push_back(val
);
2748 if (!token
->is_op(OPERATOR_LCURLY
))
2749 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2752 // This must be a composite literal inside another
2753 // composite literal, with the type omitted for the
2755 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2758 token
= this->peek_token();
2761 vals
->push_back(val
);
2764 all_are_names
= false;
2766 if (token
->is_op(OPERATOR_COMMA
))
2768 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2770 this->advance_token();
2774 else if (token
->is_op(OPERATOR_RCURLY
))
2776 this->advance_token();
2781 if (token
->is_op(OPERATOR_SEMICOLON
))
2782 error_at(this->location(),
2783 "need trailing comma before newline in composite literal");
2785 error_at(this->location(), "expected %<,%> or %<}%>");
2787 this->gogo_
->mark_locals_used();
2789 while (!token
->is_eof()
2790 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2792 if (token
->is_op(OPERATOR_LCURLY
))
2794 else if (token
->is_op(OPERATOR_RCURLY
))
2796 token
= this->advance_token();
2798 if (token
->is_op(OPERATOR_RCURLY
))
2799 this->advance_token();
2801 return Expression::make_error(location
);
2805 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2806 all_are_names
, location
);
2809 // FunctionLit = "func" Signature Block .
2812 Parse::function_lit()
2814 Location location
= this->location();
2815 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2816 this->advance_token();
2818 Enclosing_vars hold_enclosing_vars
;
2819 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2821 Function_type
* type
= this->signature(NULL
, location
);
2822 bool fntype_is_error
= false;
2825 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2826 fntype_is_error
= true;
2829 // For a function literal, the next token must be a '{'. If we
2830 // don't see that, then we may have a type expression.
2831 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2833 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2834 return Expression::make_type(type
, location
);
2837 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2838 if (fntype_is_error
)
2839 this->is_erroneous_function_
= true;
2841 Bc_stack
* hold_break_stack
= this->break_stack_
;
2842 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2843 this->break_stack_
= NULL
;
2844 this->continue_stack_
= NULL
;
2846 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2848 Location end_loc
= this->block();
2850 this->gogo_
->finish_function(end_loc
);
2852 if (this->break_stack_
!= NULL
)
2853 delete this->break_stack_
;
2854 if (this->continue_stack_
!= NULL
)
2855 delete this->continue_stack_
;
2856 this->break_stack_
= hold_break_stack
;
2857 this->continue_stack_
= hold_continue_stack
;
2859 this->is_erroneous_function_
= hold_is_erroneous_function
;
2861 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2863 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
2866 return Expression::make_func_reference(no
, closure
, location
);
2869 // Create a closure for the nested function FUNCTION. This is based
2870 // on ENCLOSING_VARS, which is a list of all variables defined in
2871 // enclosing functions and referenced from FUNCTION. A closure is the
2872 // address of a struct which point to the real function code and
2873 // contains the addresses of all the referenced variables. This
2874 // returns NULL if no closure is required.
2877 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
2880 if (enclosing_vars
->empty())
2883 // Get the variables in order by their field index.
2885 size_t enclosing_var_count
= enclosing_vars
->size();
2886 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
2887 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
2888 p
!= enclosing_vars
->end();
2891 // Subtract 1 because index 0 is the function code.
2892 ev
[p
->index() - 1] = *p
;
2895 // Build an initializer for a composite literal of the closure's
2898 Named_object
* enclosing_function
= this->gogo_
->current_function();
2899 Expression_list
* initializer
= new Expression_list
;
2901 initializer
->push_back(Expression::make_func_code_reference(function
,
2904 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
2906 // Add 1 to i because the first field in the closure is a
2907 // pointer to the function code.
2908 go_assert(ev
[i
].index() == i
+ 1);
2909 Named_object
* var
= ev
[i
].var();
2911 if (ev
[i
].in_function() == enclosing_function
)
2912 ref
= Expression::make_var_reference(var
, location
);
2914 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
2916 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
2918 initializer
->push_back(refaddr
);
2921 Named_object
* closure_var
= function
->func_value()->closure_var();
2922 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
2923 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
2925 return Expression::make_heap_expression(cv
, location
);
2928 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2930 // If MAY_BE_SINK is true, this expression may be "_".
2932 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2935 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2936 // guard (var := expr.("type") using the literal keyword "type").
2938 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2939 // if the entire expression is in parentheses.
2942 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
2943 bool* is_type_switch
, bool* is_parenthesized
)
2945 Location start_loc
= this->location();
2946 bool operand_is_parenthesized
= false;
2947 bool whole_is_parenthesized
= false;
2949 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
2951 whole_is_parenthesized
= operand_is_parenthesized
;
2953 // An unknown name followed by a curly brace must be a composite
2954 // literal, and the unknown name must be a type.
2955 if (may_be_composite_lit
2956 && !operand_is_parenthesized
2957 && ret
->unknown_expression() != NULL
2958 && this->peek_token()->is_op(OPERATOR_LCURLY
))
2960 Named_object
* no
= ret
->unknown_expression()->named_object();
2961 Type
* type
= Type::make_forward_declaration(no
);
2962 ret
= Expression::make_type(type
, ret
->location());
2965 // We handle composite literals and type casts here, as it is the
2966 // easiest way to handle types which are in parentheses, as in
2968 if (ret
->is_type_expression())
2970 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
2972 whole_is_parenthesized
= false;
2973 if (!may_be_composite_lit
)
2975 Type
* t
= ret
->type();
2976 if (t
->named_type() != NULL
2977 || t
->forward_declaration_type() != NULL
)
2979 _("parentheses required around this composite literal "
2980 "to avoid parsing ambiguity"));
2982 else if (operand_is_parenthesized
)
2984 "cannot parenthesize type in composite literal");
2985 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
2987 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
2989 whole_is_parenthesized
= false;
2990 Location loc
= this->location();
2991 this->advance_token();
2992 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
2994 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2995 this->advance_token();
2996 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
2998 error_at(this->location(),
2999 "invalid use of %<...%> in type conversion");
3000 this->advance_token();
3002 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3003 error_at(this->location(), "expected %<)%>");
3005 this->advance_token();
3006 if (expr
->is_error_expression())
3010 Type
* t
= ret
->type();
3011 if (t
->classification() == Type::TYPE_ARRAY
3012 && t
->array_type()->length() != NULL
3013 && t
->array_type()->length()->is_nil_expression())
3015 error_at(ret
->location(),
3016 "use of %<[...]%> outside of array literal");
3017 ret
= Expression::make_error(loc
);
3020 ret
= Expression::make_cast(t
, expr
, loc
);
3027 const Token
* token
= this->peek_token();
3028 if (token
->is_op(OPERATOR_LPAREN
))
3030 whole_is_parenthesized
= false;
3031 ret
= this->call(this->verify_not_sink(ret
));
3033 else if (token
->is_op(OPERATOR_DOT
))
3035 whole_is_parenthesized
= false;
3036 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3037 if (is_type_switch
!= NULL
&& *is_type_switch
)
3040 else if (token
->is_op(OPERATOR_LSQUARE
))
3042 whole_is_parenthesized
= false;
3043 ret
= this->index(this->verify_not_sink(ret
));
3049 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3050 *is_parenthesized
= true;
3055 // Selector = "." identifier .
3056 // TypeGuard = "." "(" QualifiedIdent ")" .
3058 // Note that Operand can expand to QualifiedIdent, which contains a
3059 // ".". That is handled directly in operand when it sees a package
3062 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3063 // guard (var := expr.("type") using the literal keyword "type").
3066 Parse::selector(Expression
* left
, bool* is_type_switch
)
3068 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3069 Location location
= this->location();
3071 const Token
* token
= this->advance_token();
3072 if (token
->is_identifier())
3074 // This could be a field in a struct, or a method in an
3075 // interface, or a method associated with a type. We can't know
3076 // which until we have seen all the types.
3078 this->gogo_
->pack_hidden_name(token
->identifier(),
3079 token
->is_identifier_exported());
3080 if (token
->identifier() == "_")
3082 error_at(this->location(), "invalid use of %<_%>");
3083 name
= Gogo::erroneous_name();
3085 this->advance_token();
3086 return Expression::make_selector(left
, name
, location
);
3088 else if (token
->is_op(OPERATOR_LPAREN
))
3090 this->advance_token();
3092 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3093 type
= this->type();
3096 if (is_type_switch
!= NULL
)
3097 *is_type_switch
= true;
3100 error_at(this->location(),
3101 "use of %<.(type)%> outside type switch");
3102 type
= Type::make_error_type();
3104 this->advance_token();
3106 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3107 error_at(this->location(), "missing %<)%>");
3109 this->advance_token();
3110 if (is_type_switch
!= NULL
&& *is_type_switch
)
3112 return Expression::make_type_guard(left
, type
, location
);
3116 error_at(this->location(), "expected identifier or %<(%>");
3121 // Index = "[" Expression "]" .
3122 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3125 Parse::index(Expression
* expr
)
3127 Location location
= this->location();
3128 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3129 this->advance_token();
3132 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3133 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3135 start
= Expression::make_integer_ul(0, NULL
, location
);
3137 Expression
* end
= NULL
;
3138 if (this->peek_token()->is_op(OPERATOR_COLON
))
3140 // We use nil to indicate a missing high expression.
3141 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3142 end
= Expression::make_nil(this->location());
3143 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3145 error_at(this->location(), "middle index required in 3-index slice");
3146 end
= Expression::make_error(this->location());
3149 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3152 Expression
* cap
= NULL
;
3153 if (this->peek_token()->is_op(OPERATOR_COLON
))
3155 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3157 error_at(this->location(), "final index required in 3-index slice");
3158 cap
= Expression::make_error(this->location());
3161 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3163 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3164 error_at(this->location(), "missing %<]%>");
3166 this->advance_token();
3167 return Expression::make_index(expr
, start
, end
, cap
, location
);
3170 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3171 // ArgumentList = ExpressionList [ "..." ] .
3174 Parse::call(Expression
* func
)
3176 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3177 Expression_list
* args
= NULL
;
3178 bool is_varargs
= false;
3179 const Token
* token
= this->advance_token();
3180 if (!token
->is_op(OPERATOR_RPAREN
))
3182 args
= this->expression_list(NULL
, false, true);
3183 token
= this->peek_token();
3184 if (token
->is_op(OPERATOR_ELLIPSIS
))
3187 token
= this->advance_token();
3190 if (token
->is_op(OPERATOR_COMMA
))
3191 token
= this->advance_token();
3192 if (!token
->is_op(OPERATOR_RPAREN
))
3194 error_at(this->location(), "missing %<)%>");
3195 if (!this->skip_past_error(OPERATOR_RPAREN
))
3196 return Expression::make_error(this->location());
3198 this->advance_token();
3199 if (func
->is_error_expression())
3201 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3204 // Return an expression for a single unqualified identifier.
3207 Parse::id_to_expression(const std::string
& name
, Location location
,
3210 Named_object
* in_function
;
3211 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3212 if (named_object
== NULL
)
3213 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3215 if (in_function
!= NULL
3216 && in_function
!= this->gogo_
->current_function()
3217 && (named_object
->is_variable() || named_object
->is_result_variable()))
3218 return this->enclosing_var_reference(in_function
, named_object
,
3221 switch (named_object
->classification())
3223 case Named_object::NAMED_OBJECT_CONST
:
3224 return Expression::make_const_reference(named_object
, location
);
3225 case Named_object::NAMED_OBJECT_VAR
:
3226 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3228 this->mark_var_used(named_object
);
3229 return Expression::make_var_reference(named_object
, location
);
3230 case Named_object::NAMED_OBJECT_SINK
:
3231 return Expression::make_sink(location
);
3232 case Named_object::NAMED_OBJECT_FUNC
:
3233 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3234 return Expression::make_func_reference(named_object
, NULL
, location
);
3235 case Named_object::NAMED_OBJECT_UNKNOWN
:
3237 Unknown_expression
* ue
=
3238 Expression::make_unknown_reference(named_object
, location
);
3239 if (this->is_erroneous_function_
)
3240 ue
->set_no_error_message();
3243 case Named_object::NAMED_OBJECT_PACKAGE
:
3244 case Named_object::NAMED_OBJECT_TYPE
:
3245 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3247 // These cases can arise for a field name in a composite
3248 // literal. Keep track of these as they might be fake uses of
3249 // the related package.
3250 Unknown_expression
* ue
=
3251 Expression::make_unknown_reference(named_object
, location
);
3252 if (named_object
->package() != NULL
)
3253 named_object
->package()->note_fake_usage(ue
);
3254 if (this->is_erroneous_function_
)
3255 ue
->set_no_error_message();
3258 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3259 return Expression::make_error(location
);
3261 error_at(this->location(), "unexpected type of identifier");
3262 return Expression::make_error(location
);
3266 // Expression = UnaryExpr { binary_op Expression } .
3268 // PRECEDENCE is the precedence of the current operator.
3270 // If MAY_BE_SINK is true, this expression may be "_".
3272 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3275 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3276 // guard (var := expr.("type") using the literal keyword "type").
3278 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3279 // if the entire expression is in parentheses.
3282 Parse::expression(Precedence precedence
, bool may_be_sink
,
3283 bool may_be_composite_lit
, bool* is_type_switch
,
3284 bool *is_parenthesized
)
3286 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3287 is_type_switch
, is_parenthesized
);
3291 if (is_type_switch
!= NULL
&& *is_type_switch
)
3294 const Token
* token
= this->peek_token();
3295 if (token
->classification() != Token::TOKEN_OPERATOR
)
3301 Precedence right_precedence
;
3302 switch (token
->op())
3305 right_precedence
= PRECEDENCE_OROR
;
3307 case OPERATOR_ANDAND
:
3308 right_precedence
= PRECEDENCE_ANDAND
;
3311 case OPERATOR_NOTEQ
:
3316 right_precedence
= PRECEDENCE_RELOP
;
3319 case OPERATOR_MINUS
:
3322 right_precedence
= PRECEDENCE_ADDOP
;
3327 case OPERATOR_LSHIFT
:
3328 case OPERATOR_RSHIFT
:
3330 case OPERATOR_BITCLEAR
:
3331 right_precedence
= PRECEDENCE_MULOP
;
3334 right_precedence
= PRECEDENCE_INVALID
;
3338 if (right_precedence
== PRECEDENCE_INVALID
)
3344 if (is_parenthesized
!= NULL
)
3345 *is_parenthesized
= false;
3347 Operator op
= token
->op();
3348 Location binop_location
= token
->location();
3350 if (precedence
>= right_precedence
)
3352 // We've already seen A * B, and we see + C. We want to
3353 // return so that A * B becomes a group.
3357 this->advance_token();
3359 left
= this->verify_not_sink(left
);
3360 Expression
* right
= this->expression(right_precedence
, false,
3361 may_be_composite_lit
,
3363 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3368 Parse::expression_may_start_here()
3370 const Token
* token
= this->peek_token();
3371 switch (token
->classification())
3373 case Token::TOKEN_INVALID
:
3374 case Token::TOKEN_EOF
:
3376 case Token::TOKEN_KEYWORD
:
3377 switch (token
->keyword())
3382 case KEYWORD_STRUCT
:
3383 case KEYWORD_INTERFACE
:
3388 case Token::TOKEN_IDENTIFIER
:
3390 case Token::TOKEN_STRING
:
3392 case Token::TOKEN_OPERATOR
:
3393 switch (token
->op())
3396 case OPERATOR_MINUS
:
3400 case OPERATOR_CHANOP
:
3402 case OPERATOR_LPAREN
:
3403 case OPERATOR_LSQUARE
:
3408 case Token::TOKEN_CHARACTER
:
3409 case Token::TOKEN_INTEGER
:
3410 case Token::TOKEN_FLOAT
:
3411 case Token::TOKEN_IMAGINARY
:
3418 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3420 // If MAY_BE_SINK is true, this expression may be "_".
3422 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3425 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3426 // guard (var := expr.("type") using the literal keyword "type").
3428 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3429 // if the entire expression is in parentheses.
3432 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3433 bool* is_type_switch
, bool* is_parenthesized
)
3435 const Token
* token
= this->peek_token();
3437 // There is a complex parse for <- chan. The choices are
3438 // Convert x to type <- chan int:
3440 // Receive from (x converted to type chan <- chan int):
3441 // (<- chan <- chan int (x))
3442 // Convert x to type <- chan (<- chan int).
3443 // (<- chan <- chan int)(x)
3444 if (token
->is_op(OPERATOR_CHANOP
))
3446 Location location
= token
->location();
3447 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3449 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3451 if (expr
->is_error_expression())
3453 else if (!expr
->is_type_expression())
3454 return Expression::make_receive(expr
, location
);
3457 if (expr
->type()->is_error_type())
3460 // We picked up "chan TYPE", but it is not a type
3462 Channel_type
* ct
= expr
->type()->channel_type();
3465 // This is probably impossible.
3466 error_at(location
, "expected channel type");
3467 return Expression::make_error(location
);
3469 else if (ct
->may_receive())
3472 Type
* t
= Type::make_channel_type(false, true,
3473 ct
->element_type());
3474 return Expression::make_type(t
, location
);
3478 // <- chan <- TYPE. Because we skipped the leading
3479 // <-, we parsed this as chan <- TYPE. With the
3480 // leading <-, we parse it as <- chan (<- TYPE).
3481 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3482 return Expression::make_type(t
, location
);
3487 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3488 token
= this->peek_token();
3491 if (token
->is_op(OPERATOR_PLUS
)
3492 || token
->is_op(OPERATOR_MINUS
)
3493 || token
->is_op(OPERATOR_NOT
)
3494 || token
->is_op(OPERATOR_XOR
)
3495 || token
->is_op(OPERATOR_CHANOP
)
3496 || token
->is_op(OPERATOR_MULT
)
3497 || token
->is_op(OPERATOR_AND
))
3499 Location location
= token
->location();
3500 Operator op
= token
->op();
3501 this->advance_token();
3503 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3505 if (expr
->is_error_expression())
3507 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3508 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3510 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3511 expr
= Expression::make_heap_expression(expr
, location
);
3512 else if (op
!= OPERATOR_CHANOP
)
3513 expr
= Expression::make_unary(op
, expr
, location
);
3515 expr
= Expression::make_receive(expr
, location
);
3519 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3520 is_type_switch
, is_parenthesized
);
3523 // This is called for the obscure case of
3524 // (<- chan <- chan int)(x)
3525 // In unary_expr we remove the leading <- and parse the remainder,
3527 // chan <- (chan int)
3528 // When we add the leading <- back in, we really want
3529 // <- chan (<- chan int)
3530 // This means that we need to reassociate.
3533 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3535 Channel_type
* ele
= ct
->element_type()->channel_type();
3538 error_at(location
, "parse error");
3539 return Type::make_error_type();
3542 if (ele
->may_send())
3543 sub
= Type::make_channel_type(false, true, ele
->element_type());
3545 sub
= this->reassociate_chan_direction(ele
, location
);
3546 return Type::make_channel_type(false, true, sub
);
3550 // Declaration | LabeledStmt | SimpleStmt |
3551 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3552 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3555 // LABEL is the label of this statement if it has one.
3558 Parse::statement(Label
* label
)
3560 const Token
* token
= this->peek_token();
3561 switch (token
->classification())
3563 case Token::TOKEN_KEYWORD
:
3565 switch (token
->keyword())
3570 this->declaration();
3574 case KEYWORD_STRUCT
:
3575 case KEYWORD_INTERFACE
:
3576 this->simple_stat(true, NULL
, NULL
, NULL
);
3580 this->go_or_defer_stat();
3582 case KEYWORD_RETURN
:
3583 this->return_stat();
3588 case KEYWORD_CONTINUE
:
3589 this->continue_stat();
3597 case KEYWORD_SWITCH
:
3598 this->switch_stat(label
);
3600 case KEYWORD_SELECT
:
3601 this->select_stat(label
);
3604 this->for_stat(label
);
3607 error_at(this->location(), "expected statement");
3608 this->advance_token();
3614 case Token::TOKEN_IDENTIFIER
:
3616 std::string identifier
= token
->identifier();
3617 bool is_exported
= token
->is_identifier_exported();
3618 Location location
= token
->location();
3619 if (this->advance_token()->is_op(OPERATOR_COLON
))
3621 this->advance_token();
3622 this->labeled_stmt(identifier
, location
);
3626 this->unget_token(Token::make_identifier_token(identifier
,
3629 this->simple_stat(true, NULL
, NULL
, NULL
);
3634 case Token::TOKEN_OPERATOR
:
3635 if (token
->is_op(OPERATOR_LCURLY
))
3637 Location location
= token
->location();
3638 this->gogo_
->start_block(location
);
3639 Location end_loc
= this->block();
3640 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3643 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3644 this->simple_stat(true, NULL
, NULL
, NULL
);
3647 case Token::TOKEN_STRING
:
3648 case Token::TOKEN_CHARACTER
:
3649 case Token::TOKEN_INTEGER
:
3650 case Token::TOKEN_FLOAT
:
3651 case Token::TOKEN_IMAGINARY
:
3652 this->simple_stat(true, NULL
, NULL
, NULL
);
3656 error_at(this->location(), "expected statement");
3657 this->advance_token();
3663 Parse::statement_may_start_here()
3665 const Token
* token
= this->peek_token();
3666 switch (token
->classification())
3668 case Token::TOKEN_KEYWORD
:
3670 switch (token
->keyword())
3677 case KEYWORD_STRUCT
:
3678 case KEYWORD_INTERFACE
:
3681 case KEYWORD_RETURN
:
3683 case KEYWORD_CONTINUE
:
3686 case KEYWORD_SWITCH
:
3687 case KEYWORD_SELECT
:
3697 case Token::TOKEN_IDENTIFIER
:
3700 case Token::TOKEN_OPERATOR
:
3701 if (token
->is_op(OPERATOR_LCURLY
)
3702 || token
->is_op(OPERATOR_SEMICOLON
))
3705 return this->expression_may_start_here();
3707 case Token::TOKEN_STRING
:
3708 case Token::TOKEN_CHARACTER
:
3709 case Token::TOKEN_INTEGER
:
3710 case Token::TOKEN_FLOAT
:
3711 case Token::TOKEN_IMAGINARY
:
3719 // LabeledStmt = Label ":" Statement .
3720 // Label = identifier .
3723 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3725 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3727 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3729 // This is a label at the end of a block. A program is
3730 // permitted to omit a semicolon here.
3734 if (!this->statement_may_start_here())
3736 // Mark the label as used to avoid a useless error about an
3739 label
->set_is_used();
3741 error_at(location
, "missing statement after label");
3742 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3747 this->statement(label
);
3750 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3751 // Assignment | ShortVarDecl .
3753 // EmptyStmt was handled in Parse::statement.
3755 // In order to make this work for if and switch statements, if
3756 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3757 // expression rather than adding an expression statement to the
3758 // current block. If we see something other than an ExpressionStat,
3759 // we add the statement, set *RETURN_EXP to true if we saw a send
3760 // statement, and return NULL. The handling of send statements is for
3761 // better error messages.
3763 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3766 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3767 // guard (var := expr.("type") using the literal keyword "type").
3770 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3771 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3773 const Token
* token
= this->peek_token();
3775 // An identifier follow by := is a SimpleVarDecl.
3776 if (token
->is_identifier())
3778 std::string identifier
= token
->identifier();
3779 bool is_exported
= token
->is_identifier_exported();
3780 Location location
= token
->location();
3782 token
= this->advance_token();
3783 if (token
->is_op(OPERATOR_COLONEQ
)
3784 || token
->is_op(OPERATOR_COMMA
))
3786 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3787 this->simple_var_decl_or_assignment(identifier
, location
,
3788 may_be_composite_lit
,
3790 (token
->is_op(OPERATOR_COLONEQ
)
3796 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3799 else if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
3801 Typed_identifier_list til
;
3802 this->range_clause_decl(&til
, p_range_clause
);
3806 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3807 may_be_composite_lit
,
3808 (p_type_switch
== NULL
3810 : &p_type_switch
->found
),
3812 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3814 p_type_switch
->name
.clear();
3815 p_type_switch
->location
= exp
->location();
3816 p_type_switch
->expr
= this->verify_not_sink(exp
);
3819 token
= this->peek_token();
3820 if (token
->is_op(OPERATOR_CHANOP
))
3822 this->send_stmt(this->verify_not_sink(exp
), may_be_composite_lit
);
3823 if (return_exp
!= NULL
)
3826 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3827 || token
->is_op(OPERATOR_MINUSMINUS
))
3828 this->inc_dec_stat(this->verify_not_sink(exp
));
3829 else if (token
->is_op(OPERATOR_COMMA
)
3830 || token
->is_op(OPERATOR_EQ
))
3831 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3832 else if (token
->is_op(OPERATOR_PLUSEQ
)
3833 || token
->is_op(OPERATOR_MINUSEQ
)
3834 || token
->is_op(OPERATOR_OREQ
)
3835 || token
->is_op(OPERATOR_XOREQ
)
3836 || token
->is_op(OPERATOR_MULTEQ
)
3837 || token
->is_op(OPERATOR_DIVEQ
)
3838 || token
->is_op(OPERATOR_MODEQ
)
3839 || token
->is_op(OPERATOR_LSHIFTEQ
)
3840 || token
->is_op(OPERATOR_RSHIFTEQ
)
3841 || token
->is_op(OPERATOR_ANDEQ
)
3842 || token
->is_op(OPERATOR_BITCLEAREQ
))
3843 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
3845 else if (return_exp
!= NULL
)
3846 return this->verify_not_sink(exp
);
3849 exp
= this->verify_not_sink(exp
);
3851 if (token
->is_op(OPERATOR_COLONEQ
))
3853 if (!exp
->is_error_expression())
3854 error_at(token
->location(), "non-name on left side of %<:=%>");
3855 this->gogo_
->mark_locals_used();
3856 while (!token
->is_op(OPERATOR_SEMICOLON
)
3857 && !token
->is_eof())
3858 token
= this->advance_token();
3862 this->expression_stat(exp
);
3869 Parse::simple_stat_may_start_here()
3871 return this->expression_may_start_here();
3874 // Parse { Statement ";" } which is used in a few places. The list of
3875 // statements may end with a right curly brace, in which case the
3876 // semicolon may be omitted.
3879 Parse::statement_list()
3881 while (this->statement_may_start_here())
3883 this->statement(NULL
);
3884 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
3885 this->advance_token();
3886 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3890 if (!this->peek_token()->is_eof() || !saw_errors())
3891 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3892 if (!this->skip_past_error(OPERATOR_RCURLY
))
3899 Parse::statement_list_may_start_here()
3901 return this->statement_may_start_here();
3904 // ExpressionStat = Expression .
3907 Parse::expression_stat(Expression
* exp
)
3909 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
3912 // SendStmt = Channel "<-" Expression .
3913 // Channel = Expression .
3916 Parse::send_stmt(Expression
* channel
, bool may_be_composite_lit
)
3918 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
3919 Location loc
= this->location();
3920 this->advance_token();
3921 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false,
3922 may_be_composite_lit
, NULL
, NULL
);
3923 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
3924 this->gogo_
->add_statement(s
);
3927 // IncDecStat = Expression ( "++" | "--" ) .
3930 Parse::inc_dec_stat(Expression
* exp
)
3932 const Token
* token
= this->peek_token();
3934 // Lvalue maps require special handling.
3935 if (exp
->index_expression() != NULL
)
3936 exp
->index_expression()->set_is_lvalue();
3938 if (token
->is_op(OPERATOR_PLUSPLUS
))
3939 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
3940 else if (token
->is_op(OPERATOR_MINUSMINUS
))
3941 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
3944 this->advance_token();
3947 // Assignment = ExpressionList assign_op ExpressionList .
3949 // EXP is an expression that we have already parsed.
3951 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3952 // side may be a composite literal.
3954 // If RANGE_CLAUSE is not NULL, then this will recognize a
3958 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
3959 Range_clause
* p_range_clause
)
3961 Expression_list
* vars
;
3962 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
3964 vars
= new Expression_list();
3965 vars
->push_back(expr
);
3969 this->advance_token();
3970 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
3973 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
3976 // An assignment statement. LHS is the list of expressions which
3977 // appear on the left hand side.
3979 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3980 // side may be a composite literal.
3982 // If RANGE_CLAUSE is not NULL, then this will recognize a
3986 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
3987 Range_clause
* p_range_clause
)
3989 const Token
* token
= this->peek_token();
3990 if (!token
->is_op(OPERATOR_EQ
)
3991 && !token
->is_op(OPERATOR_PLUSEQ
)
3992 && !token
->is_op(OPERATOR_MINUSEQ
)
3993 && !token
->is_op(OPERATOR_OREQ
)
3994 && !token
->is_op(OPERATOR_XOREQ
)
3995 && !token
->is_op(OPERATOR_MULTEQ
)
3996 && !token
->is_op(OPERATOR_DIVEQ
)
3997 && !token
->is_op(OPERATOR_MODEQ
)
3998 && !token
->is_op(OPERATOR_LSHIFTEQ
)
3999 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4000 && !token
->is_op(OPERATOR_ANDEQ
)
4001 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4003 error_at(this->location(), "expected assignment operator");
4006 Operator op
= token
->op();
4007 Location location
= token
->location();
4009 token
= this->advance_token();
4011 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4013 if (op
!= OPERATOR_EQ
)
4014 error_at(this->location(), "range clause requires %<=%>");
4015 this->range_clause_expr(lhs
, p_range_clause
);
4019 Expression_list
* vals
= this->expression_list(NULL
, false,
4020 may_be_composite_lit
);
4022 // We've parsed everything; check for errors.
4023 if (lhs
== NULL
|| vals
== NULL
)
4025 for (Expression_list::const_iterator pe
= lhs
->begin();
4029 if ((*pe
)->is_error_expression())
4031 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4032 error_at((*pe
)->location(), "cannot use _ as value");
4034 for (Expression_list::const_iterator pe
= vals
->begin();
4038 if ((*pe
)->is_error_expression())
4042 // Map expressions act differently when they are lvalues.
4043 for (Expression_list::iterator plv
= lhs
->begin();
4046 if ((*plv
)->index_expression() != NULL
)
4047 (*plv
)->index_expression()->set_is_lvalue();
4049 Call_expression
* call
;
4050 Index_expression
* map_index
;
4051 Receive_expression
* receive
;
4052 Type_guard_expression
* type_guard
;
4053 if (lhs
->size() == vals
->size())
4056 if (lhs
->size() > 1)
4058 if (op
!= OPERATOR_EQ
)
4059 error_at(location
, "multiple values only permitted with %<=%>");
4060 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4064 if (op
== OPERATOR_EQ
)
4065 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4068 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4069 vals
->front(), location
);
4073 this->gogo_
->add_statement(s
);
4075 else if (vals
->size() == 1
4076 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4078 if (op
!= OPERATOR_EQ
)
4079 error_at(location
, "multiple results only permitted with %<=%>");
4080 call
->set_expected_result_count(lhs
->size());
4082 vals
= new Expression_list
;
4083 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4084 vals
->push_back(Expression::make_call_result(call
, i
));
4085 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4086 this->gogo_
->add_statement(s
);
4088 else if (lhs
->size() == 2
4089 && vals
->size() == 1
4090 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4092 if (op
!= OPERATOR_EQ
)
4093 error_at(location
, "two values from map requires %<=%>");
4094 Expression
* val
= lhs
->front();
4095 Expression
* present
= lhs
->back();
4096 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4097 map_index
, location
);
4098 this->gogo_
->add_statement(s
);
4100 else if (lhs
->size() == 1
4101 && vals
->size() == 2
4102 && (map_index
= lhs
->front()->index_expression()) != NULL
)
4104 if (op
!= OPERATOR_EQ
)
4105 error_at(location
, "assigning tuple to map index requires %<=%>");
4106 Expression
* val
= vals
->front();
4107 Expression
* should_set
= vals
->back();
4108 Statement
* s
= Statement::make_map_assignment(map_index
, val
, should_set
,
4110 this->gogo_
->add_statement(s
);
4112 else if (lhs
->size() == 2
4113 && vals
->size() == 1
4114 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4116 if (op
!= OPERATOR_EQ
)
4117 error_at(location
, "two values from receive requires %<=%>");
4118 Expression
* val
= lhs
->front();
4119 Expression
* success
= lhs
->back();
4120 Expression
* channel
= receive
->channel();
4121 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4124 this->gogo_
->add_statement(s
);
4126 else if (lhs
->size() == 2
4127 && vals
->size() == 1
4128 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4130 if (op
!= OPERATOR_EQ
)
4131 error_at(location
, "two values from type guard requires %<=%>");
4132 Expression
* val
= lhs
->front();
4133 Expression
* ok
= lhs
->back();
4134 Expression
* expr
= type_guard
->expr();
4135 Type
* type
= type_guard
->type();
4136 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4139 this->gogo_
->add_statement(s
);
4143 error_at(location
, "number of variables does not match number of values");
4147 // GoStat = "go" Expression .
4148 // DeferStat = "defer" Expression .
4151 Parse::go_or_defer_stat()
4153 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4154 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4155 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4156 Location stat_location
= this->location();
4158 this->advance_token();
4159 Location expr_location
= this->location();
4161 bool is_parenthesized
= false;
4162 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4164 Call_expression
* call_expr
= expr
->call_expression();
4165 if (is_parenthesized
|| call_expr
== NULL
)
4167 error_at(expr_location
, "argument to go/defer must be function call");
4171 // Make it easier to simplify go/defer statements by putting every
4172 // statement in its own block.
4173 this->gogo_
->start_block(stat_location
);
4176 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4178 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4179 this->gogo_
->add_statement(stat
);
4180 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4184 // ReturnStat = "return" [ ExpressionList ] .
4187 Parse::return_stat()
4189 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4190 Location location
= this->location();
4191 this->advance_token();
4192 Expression_list
* vals
= NULL
;
4193 if (this->expression_may_start_here())
4194 vals
= this->expression_list(NULL
, false, true);
4195 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4198 && this->gogo_
->current_function()->func_value()->results_are_named())
4200 Named_object
* function
= this->gogo_
->current_function();
4201 Function::Results
* results
= function
->func_value()->result_variables();
4202 for (Function::Results::const_iterator p
= results
->begin();
4203 p
!= results
->end();
4206 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4208 go_assert(saw_errors());
4209 else if (!no
->is_result_variable())
4210 error_at(location
, "%qs is shadowed during return",
4211 (*p
)->message_name().c_str());
4216 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4217 // [ "else" ( IfStmt | Block ) ] .
4222 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4223 Location location
= this->location();
4224 this->advance_token();
4226 this->gogo_
->start_block(location
);
4228 bool saw_simple_stat
= false;
4229 Expression
* cond
= NULL
;
4230 bool saw_send_stmt
= false;
4231 if (this->simple_stat_may_start_here())
4233 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4234 saw_simple_stat
= true;
4236 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4238 // The SimpleStat is an expression statement.
4239 this->expression_stat(cond
);
4244 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4245 this->advance_token();
4246 else if (saw_simple_stat
)
4249 error_at(this->location(),
4250 ("send statement used as value; "
4251 "use select for non-blocking send"));
4253 error_at(this->location(),
4254 "expected %<;%> after statement in if expression");
4255 if (!this->expression_may_start_here())
4256 cond
= Expression::make_error(this->location());
4258 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4260 error_at(this->location(),
4261 "missing condition in if statement");
4262 cond
= Expression::make_error(this->location());
4265 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4268 // Check for the easy error of a newline before starting the block.
4269 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4271 Location semi_loc
= this->location();
4272 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4273 error_at(semi_loc
, "missing %<{%> after if clause");
4274 // Otherwise we will get an error when we call this->block
4278 this->gogo_
->start_block(this->location());
4279 Location end_loc
= this->block();
4280 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4282 // Check for the easy error of a newline before "else".
4283 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4285 Location semi_loc
= this->location();
4286 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4287 error_at(this->location(),
4288 "unexpected semicolon or newline before %<else%>");
4290 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4294 Block
* else_block
= NULL
;
4295 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4297 this->gogo_
->start_block(this->location());
4298 const Token
* token
= this->advance_token();
4299 if (token
->is_keyword(KEYWORD_IF
))
4301 else if (token
->is_op(OPERATOR_LCURLY
))
4305 error_at(this->location(), "expected %<if%> or %<{%>");
4306 this->statement(NULL
);
4308 else_block
= this->gogo_
->finish_block(this->location());
4311 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4315 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4319 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4320 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4321 // "{" { ExprCaseClause } "}" .
4322 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4323 // "{" { TypeCaseClause } "}" .
4324 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4327 Parse::switch_stat(Label
* label
)
4329 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4330 Location location
= this->location();
4331 this->advance_token();
4333 this->gogo_
->start_block(location
);
4335 bool saw_simple_stat
= false;
4336 Expression
* switch_val
= NULL
;
4338 Type_switch type_switch
;
4339 bool have_type_switch_block
= false;
4340 if (this->simple_stat_may_start_here())
4342 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4344 saw_simple_stat
= true;
4346 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4348 // The SimpleStat is an expression statement.
4349 this->expression_stat(switch_val
);
4352 if (switch_val
== NULL
&& !type_switch
.found
)
4354 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4355 this->advance_token();
4356 else if (saw_simple_stat
)
4359 error_at(this->location(),
4360 ("send statement used as value; "
4361 "use select for non-blocking send"));
4363 error_at(this->location(),
4364 "expected %<;%> after statement in switch expression");
4366 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4368 if (this->peek_token()->is_identifier())
4370 const Token
* token
= this->peek_token();
4371 std::string identifier
= token
->identifier();
4372 bool is_exported
= token
->is_identifier_exported();
4373 Location id_loc
= token
->location();
4375 token
= this->advance_token();
4376 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4377 this->unget_token(Token::make_identifier_token(identifier
,
4382 // This must be a TypeSwitchGuard. It is in a
4383 // different block from any initial SimpleStat.
4384 if (saw_simple_stat
)
4386 this->gogo_
->start_block(id_loc
);
4387 have_type_switch_block
= true;
4390 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4392 if (!type_switch
.found
)
4394 if (switch_val
== NULL
4395 || !switch_val
->is_error_expression())
4397 error_at(id_loc
, "expected type switch assignment");
4398 switch_val
= Expression::make_error(id_loc
);
4403 if (switch_val
== NULL
&& !type_switch
.found
)
4405 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4406 &type_switch
.found
, NULL
);
4407 if (type_switch
.found
)
4409 type_switch
.name
.clear();
4410 type_switch
.expr
= switch_val
;
4411 type_switch
.location
= switch_val
->location();
4417 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4419 Location token_loc
= this->location();
4420 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4421 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4422 error_at(token_loc
, "missing %<{%> after switch clause");
4423 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4425 error_at(token_loc
, "invalid variable name");
4426 this->advance_token();
4427 this->expression(PRECEDENCE_NORMAL
, false, false,
4428 &type_switch
.found
, NULL
);
4429 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4430 this->advance_token();
4431 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4433 if (have_type_switch_block
)
4434 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4436 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4440 if (type_switch
.found
)
4441 type_switch
.expr
= Expression::make_error(location
);
4445 error_at(this->location(), "expected %<{%>");
4446 if (have_type_switch_block
)
4447 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4449 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4454 this->advance_token();
4456 Statement
* statement
;
4457 if (type_switch
.found
)
4458 statement
= this->type_switch_body(label
, type_switch
, location
);
4460 statement
= this->expr_switch_body(label
, switch_val
, location
);
4462 if (statement
!= NULL
)
4463 this->gogo_
->add_statement(statement
);
4465 if (have_type_switch_block
)
4466 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4469 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4473 // The body of an expression switch.
4474 // "{" { ExprCaseClause } "}"
4477 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4480 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4483 this->push_break_statement(statement
, label
);
4485 Case_clauses
* case_clauses
= new Case_clauses();
4486 bool saw_default
= false;
4487 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4489 if (this->peek_token()->is_eof())
4492 error_at(this->location(), "missing %<}%>");
4495 this->expr_case_clause(case_clauses
, &saw_default
);
4497 this->advance_token();
4499 statement
->add_clauses(case_clauses
);
4501 this->pop_break_statement();
4506 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4507 // FallthroughStat = "fallthrough" .
4510 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4512 Location location
= this->location();
4514 bool is_default
= false;
4515 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4517 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4520 error_at(this->location(), "expected %<:%>");
4524 this->advance_token();
4526 Block
* statements
= NULL
;
4527 if (this->statement_list_may_start_here())
4529 this->gogo_
->start_block(this->location());
4530 this->statement_list();
4531 statements
= this->gogo_
->finish_block(this->location());
4534 bool is_fallthrough
= false;
4535 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4537 Location fallthrough_loc
= this->location();
4538 is_fallthrough
= true;
4539 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4540 this->advance_token();
4541 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4542 error_at(fallthrough_loc
, _("cannot fallthrough final case in switch"));
4549 error_at(location
, "multiple defaults in switch");
4552 *saw_default
= true;
4555 if (is_default
|| vals
!= NULL
)
4556 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4559 // ExprSwitchCase = "case" ExpressionList | "default" .
4562 Parse::expr_switch_case(bool* is_default
)
4564 const Token
* token
= this->peek_token();
4565 if (token
->is_keyword(KEYWORD_CASE
))
4567 this->advance_token();
4568 return this->expression_list(NULL
, false, true);
4570 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4572 this->advance_token();
4579 error_at(this->location(), "expected %<case%> or %<default%>");
4580 if (!token
->is_op(OPERATOR_RCURLY
))
4581 this->advance_token();
4586 // The body of a type switch.
4587 // "{" { TypeCaseClause } "}" .
4590 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4593 Named_object
* switch_no
= NULL
;
4594 if (!type_switch
.name
.empty())
4596 if (Gogo::is_sink_name(type_switch
.name
))
4597 error_at(type_switch
.location
,
4598 "no new variables on left side of %<:=%>");
4601 Variable
* switch_var
= new Variable(NULL
, type_switch
.expr
, false,
4603 type_switch
.location
);
4604 switch_no
= this->gogo_
->add_variable(type_switch
.name
, switch_var
);
4608 Type_switch_statement
* statement
=
4609 Statement::make_type_switch_statement(switch_no
,
4615 this->push_break_statement(statement
, label
);
4617 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4618 bool saw_default
= false;
4619 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4621 if (this->peek_token()->is_eof())
4623 error_at(this->location(), "missing %<}%>");
4626 this->type_case_clause(switch_no
, case_clauses
, &saw_default
);
4628 this->advance_token();
4630 statement
->add_clauses(case_clauses
);
4632 this->pop_break_statement();
4637 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4640 Parse::type_case_clause(Named_object
* switch_no
, Type_case_clauses
* clauses
,
4643 Location location
= this->location();
4645 std::vector
<Type
*> types
;
4646 bool is_default
= false;
4647 this->type_switch_case(&types
, &is_default
);
4649 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4650 error_at(this->location(), "expected %<:%>");
4652 this->advance_token();
4654 Block
* statements
= NULL
;
4655 if (this->statement_list_may_start_here())
4657 this->gogo_
->start_block(this->location());
4658 if (switch_no
!= NULL
&& types
.size() == 1)
4660 Type
* type
= types
.front();
4661 Expression
* init
= Expression::make_var_reference(switch_no
,
4663 init
= Expression::make_type_guard(init
, type
, location
);
4664 Variable
* v
= new Variable(type
, init
, false, false, false,
4666 v
->set_is_type_switch_var();
4667 Named_object
* no
= this->gogo_
->add_variable(switch_no
->name(), v
);
4669 // We don't want to issue an error if the compiler
4670 // introduced special variable is not used. Instead we want
4671 // to issue an error if the variable defined by the switch
4672 // is not used. That is handled via type_switch_vars_ and
4673 // Parse::mark_var_used.
4675 this->type_switch_vars_
[no
] = switch_no
;
4677 this->statement_list();
4678 statements
= this->gogo_
->finish_block(this->location());
4681 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4683 error_at(this->location(),
4684 "fallthrough is not permitted in a type switch");
4685 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4686 this->advance_token();
4691 go_assert(types
.empty());
4694 error_at(location
, "multiple defaults in type switch");
4697 *saw_default
= true;
4698 clauses
->add(NULL
, false, true, statements
, location
);
4700 else if (!types
.empty())
4702 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4703 p
+ 1 != types
.end();
4705 clauses
->add(*p
, true, false, NULL
, location
);
4706 clauses
->add(types
.back(), false, false, statements
, location
);
4709 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4712 // TypeSwitchCase = "case" type | "default"
4714 // We accept a comma separated list of types.
4717 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4719 const Token
* token
= this->peek_token();
4720 if (token
->is_keyword(KEYWORD_CASE
))
4722 this->advance_token();
4725 Type
* t
= this->type();
4727 if (!t
->is_error_type())
4728 types
->push_back(t
);
4731 this->gogo_
->mark_locals_used();
4732 token
= this->peek_token();
4733 while (!token
->is_op(OPERATOR_COLON
)
4734 && !token
->is_op(OPERATOR_COMMA
)
4735 && !token
->is_op(OPERATOR_RCURLY
)
4736 && !token
->is_eof())
4737 token
= this->advance_token();
4740 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4742 this->advance_token();
4745 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4747 this->advance_token();
4752 error_at(this->location(), "expected %<case%> or %<default%>");
4753 if (!token
->is_op(OPERATOR_RCURLY
))
4754 this->advance_token();
4758 // SelectStat = "select" "{" { CommClause } "}" .
4761 Parse::select_stat(Label
* label
)
4763 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4764 Location location
= this->location();
4765 const Token
* token
= this->advance_token();
4767 if (!token
->is_op(OPERATOR_LCURLY
))
4769 Location token_loc
= token
->location();
4770 if (token
->is_op(OPERATOR_SEMICOLON
)
4771 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4772 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4775 error_at(this->location(), "expected %<{%>");
4779 this->advance_token();
4781 Select_statement
* statement
= Statement::make_select_statement(location
);
4783 this->push_break_statement(statement
, label
);
4785 Select_clauses
* select_clauses
= new Select_clauses();
4786 bool saw_default
= false;
4787 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4789 if (this->peek_token()->is_eof())
4791 error_at(this->location(), "expected %<}%>");
4794 this->comm_clause(select_clauses
, &saw_default
);
4797 this->advance_token();
4799 statement
->add_clauses(select_clauses
);
4801 this->pop_break_statement();
4803 this->gogo_
->add_statement(statement
);
4806 // CommClause = CommCase ":" { Statement ";" } .
4809 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4811 Location location
= this->location();
4812 bool is_send
= false;
4813 Expression
* channel
= NULL
;
4814 Expression
* val
= NULL
;
4815 Expression
* closed
= NULL
;
4816 std::string varname
;
4817 std::string closedname
;
4818 bool is_default
= false;
4819 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4820 &varname
, &closedname
, &is_default
);
4824 && closedname
.empty()
4826 && val
->index_expression() != NULL
)
4827 val
->index_expression()->set_is_lvalue();
4829 if (this->peek_token()->is_op(OPERATOR_COLON
))
4830 this->advance_token();
4832 error_at(this->location(), "expected colon");
4834 this->gogo_
->start_block(this->location());
4836 Named_object
* var
= NULL
;
4837 if (!varname
.empty())
4839 // FIXME: LOCATION is slightly wrong here.
4840 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
4842 v
->set_type_from_chan_element();
4843 var
= this->gogo_
->add_variable(varname
, v
);
4846 Named_object
* closedvar
= NULL
;
4847 if (!closedname
.empty())
4849 // FIXME: LOCATION is slightly wrong here.
4850 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
4851 false, false, false, location
);
4852 closedvar
= this->gogo_
->add_variable(closedname
, v
);
4855 this->statement_list();
4857 Block
* statements
= this->gogo_
->finish_block(this->location());
4863 error_at(location
, "multiple defaults in select");
4866 *saw_default
= true;
4870 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
4871 statements
, location
);
4872 else if (statements
!= NULL
)
4874 // Add the statements to make sure that any names they define
4876 this->gogo_
->add_block(statements
, location
);
4880 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4883 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
4884 Expression
** closed
, std::string
* varname
,
4885 std::string
* closedname
, bool* is_default
)
4887 const Token
* token
= this->peek_token();
4888 if (token
->is_keyword(KEYWORD_DEFAULT
))
4890 this->advance_token();
4893 else if (token
->is_keyword(KEYWORD_CASE
))
4895 this->advance_token();
4896 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
4902 error_at(this->location(), "expected %<case%> or %<default%>");
4903 if (!token
->is_op(OPERATOR_RCURLY
))
4904 this->advance_token();
4911 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4912 // RecvExpr = Expression .
4915 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
4916 Expression
** closed
, std::string
* varname
,
4917 std::string
* closedname
)
4919 const Token
* token
= this->peek_token();
4920 bool saw_comma
= false;
4921 bool closed_is_id
= false;
4922 if (token
->is_identifier())
4924 Gogo
* gogo
= this->gogo_
;
4925 std::string recv_var
= token
->identifier();
4926 bool is_rv_exported
= token
->is_identifier_exported();
4927 Location recv_var_loc
= token
->location();
4928 token
= this->advance_token();
4929 if (token
->is_op(OPERATOR_COLONEQ
))
4932 this->advance_token();
4933 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
4935 Receive_expression
* re
= e
->receive_expression();
4938 if (!e
->is_error_expression())
4939 error_at(this->location(), "expected receive expression");
4942 if (recv_var
== "_")
4944 error_at(recv_var_loc
,
4945 "no new variables on left side of %<:=%>");
4946 recv_var
= Gogo::erroneous_name();
4949 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
4950 *channel
= re
->channel();
4953 else if (token
->is_op(OPERATOR_COMMA
))
4955 token
= this->advance_token();
4956 if (token
->is_identifier())
4958 std::string recv_closed
= token
->identifier();
4959 bool is_rc_exported
= token
->is_identifier_exported();
4960 Location recv_closed_loc
= token
->location();
4961 closed_is_id
= true;
4963 token
= this->advance_token();
4964 if (token
->is_op(OPERATOR_COLONEQ
))
4966 // case rv, rc := <-c:
4967 this->advance_token();
4968 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
4970 Receive_expression
* re
= e
->receive_expression();
4973 if (!e
->is_error_expression())
4974 error_at(this->location(),
4975 "expected receive expression");
4978 if (recv_var
== "_" && recv_closed
== "_")
4980 error_at(recv_var_loc
,
4981 "no new variables on left side of %<:=%>");
4982 recv_var
= Gogo::erroneous_name();
4985 if (recv_var
!= "_")
4986 *varname
= gogo
->pack_hidden_name(recv_var
,
4988 if (recv_closed
!= "_")
4989 *closedname
= gogo
->pack_hidden_name(recv_closed
,
4991 *channel
= re
->channel();
4995 this->unget_token(Token::make_identifier_token(recv_closed
,
5000 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5002 recv_var_loc
, true);
5006 this->unget_token(Token::make_identifier_token(recv_var
,
5011 // If SAW_COMMA is false, then we are looking at the start of the
5012 // send or receive expression. If SAW_COMMA is true, then *VAL is
5013 // set and we just read a comma.
5016 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5017 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5022 this->advance_token();
5023 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5025 // The next token should be ':'. If it is '<-', then we have
5027 // which is to say, send on a channel received from a channel.
5028 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5031 e
= Expression::make_receive(*channel
, (*channel
)->location());
5034 if (!saw_comma
&& this->peek_token()->is_op(OPERATOR_COMMA
))
5036 this->advance_token();
5038 if (!e
->is_sink_expression())
5040 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5044 if (this->peek_token()->is_op(OPERATOR_EQ
))
5046 if (!this->advance_token()->is_op(OPERATOR_CHANOP
))
5048 error_at(this->location(), "missing %<<-%>");
5052 this->advance_token();
5053 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5057 // *VAL is already set.
5058 if (!e
->is_sink_expression())
5064 if (!e
->is_sink_expression())
5073 error_at(this->location(), "expected %<=%> or %<:=%>");
5075 error_at(this->location(), "expected %<=%>");
5079 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5083 *channel
= this->verify_not_sink(e
);
5084 this->advance_token();
5085 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5089 error_at(this->location(), "expected %<<-%> or %<=%>");
5093 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5094 // Condition = Expression .
5097 Parse::for_stat(Label
* label
)
5099 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5100 Location location
= this->location();
5101 const Token
* token
= this->advance_token();
5103 // Open a block to hold any variables defined in the init statement
5104 // of the for statement.
5105 this->gogo_
->start_block(location
);
5108 Expression
* cond
= NULL
;
5110 Range_clause range_clause
;
5112 if (!token
->is_op(OPERATOR_LCURLY
))
5114 if (token
->is_keyword(KEYWORD_VAR
))
5116 error_at(this->location(),
5117 "var declaration not allowed in for initializer");
5121 if (token
->is_op(OPERATOR_SEMICOLON
))
5122 this->for_clause(&cond
, &post
);
5125 // We might be looking at a Condition, an InitStat, or a
5128 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5129 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5131 if (cond
== NULL
&& !range_clause
.found
)
5134 error_at(this->location(),
5135 ("send statement used as value; "
5136 "use select for non-blocking send"));
5138 error_at(this->location(), "parse error in for statement");
5143 if (range_clause
.found
)
5144 error_at(this->location(), "parse error after range clause");
5148 // COND is actually an expression statement for
5149 // InitStat at the start of a ForClause.
5150 this->expression_stat(cond
);
5154 this->for_clause(&cond
, &post
);
5159 // Check for the easy error of a newline before starting the block.
5160 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5162 Location semi_loc
= this->location();
5163 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5164 error_at(semi_loc
, "missing %<{%> after for clause");
5165 // Otherwise we will get an error when we call this->block
5169 // Build the For_statement and note that it is the current target
5170 // for break and continue statements.
5172 For_statement
* sfor
;
5173 For_range_statement
* srange
;
5175 if (!range_clause
.found
)
5177 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5183 srange
= Statement::make_for_range_statement(range_clause
.index
,
5191 this->push_break_statement(s
, label
);
5192 this->push_continue_statement(s
, label
);
5194 // Gather the block of statements in the loop and add them to the
5197 this->gogo_
->start_block(this->location());
5198 Location end_loc
= this->block();
5199 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5202 sfor
->add_statements(statements
);
5204 srange
->add_statements(statements
);
5206 // This is no longer the break/continue target.
5207 this->pop_break_statement();
5208 this->pop_continue_statement();
5210 // Add the For_statement to the list of statements, and close out
5211 // the block we started to hold any variables defined in the for
5214 this->gogo_
->add_statement(s
);
5216 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5220 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5221 // InitStat = SimpleStat .
5222 // PostStat = SimpleStat .
5224 // We have already read InitStat at this point.
5227 Parse::for_clause(Expression
** cond
, Block
** post
)
5229 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5230 this->advance_token();
5231 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5233 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5235 error_at(this->location(), "missing %<{%> after for clause");
5241 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5242 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5243 error_at(this->location(), "expected semicolon");
5245 this->advance_token();
5247 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5251 this->gogo_
->start_block(this->location());
5252 this->simple_stat(false, NULL
, NULL
, NULL
);
5253 *post
= this->gogo_
->finish_block(this->location());
5257 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5259 // This is the := version. It is called with a list of identifiers.
5262 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5263 Range_clause
* p_range_clause
)
5265 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5266 Location location
= this->location();
5268 p_range_clause
->found
= true;
5270 if (til
->size() > 2)
5271 error_at(this->location(), "too many variables for range clause");
5273 this->advance_token();
5274 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5276 p_range_clause
->range
= expr
;
5281 bool any_new
= false;
5283 const Typed_identifier
* pti
= &til
->front();
5284 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5286 if (any_new
&& no
->is_variable())
5287 no
->var_value()->set_type_from_range_index();
5288 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5290 if (til
->size() == 1)
5291 p_range_clause
->value
= NULL
;
5295 bool is_new
= false;
5296 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5297 if (is_new
&& no
->is_variable())
5298 no
->var_value()->set_type_from_range_value();
5301 if (!Gogo::is_sink_name(pti
->name()))
5302 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5306 error_at(location
, "variables redeclared but no variable is new");
5309 // The = version of RangeClause. This is called with a list of
5313 Parse::range_clause_expr(const Expression_list
* vals
,
5314 Range_clause
* p_range_clause
)
5316 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5318 p_range_clause
->found
= true;
5320 go_assert(vals
->size() >= 1);
5321 if (vals
->size() > 2)
5322 error_at(this->location(), "too many variables for range clause");
5324 this->advance_token();
5325 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5331 p_range_clause
->index
= vals
->front();
5332 if (vals
->size() == 1)
5333 p_range_clause
->value
= NULL
;
5335 p_range_clause
->value
= vals
->back();
5338 // Push a statement on the break stack.
5341 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5343 if (this->break_stack_
== NULL
)
5344 this->break_stack_
= new Bc_stack();
5345 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5348 // Push a statement on the continue stack.
5351 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5353 if (this->continue_stack_
== NULL
)
5354 this->continue_stack_
= new Bc_stack();
5355 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5358 // Pop the break stack.
5361 Parse::pop_break_statement()
5363 this->break_stack_
->pop_back();
5366 // Pop the continue stack.
5369 Parse::pop_continue_statement()
5371 this->continue_stack_
->pop_back();
5374 // Find a break or continue statement given a label name.
5377 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5379 if (bc_stack
== NULL
)
5381 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5382 p
!= bc_stack
->rend();
5385 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5387 p
->second
->set_is_used();
5394 // BreakStat = "break" [ identifier ] .
5399 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5400 Location location
= this->location();
5402 const Token
* token
= this->advance_token();
5403 Statement
* enclosing
;
5404 if (!token
->is_identifier())
5406 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5408 error_at(this->location(),
5409 "break statement not within for or switch or select");
5412 enclosing
= this->break_stack_
->back().first
;
5416 enclosing
= this->find_bc_statement(this->break_stack_
,
5417 token
->identifier());
5418 if (enclosing
== NULL
)
5420 // If there is a label with this name, mark it as used to
5421 // avoid a useless error about an unused label.
5422 this->gogo_
->add_label_reference(token
->identifier(),
5423 Linemap::unknown_location(), false);
5425 error_at(token
->location(), "invalid break label %qs",
5426 Gogo::message_name(token
->identifier()).c_str());
5427 this->advance_token();
5430 this->advance_token();
5433 Unnamed_label
* label
;
5434 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5435 label
= enclosing
->for_statement()->break_label();
5436 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5437 label
= enclosing
->for_range_statement()->break_label();
5438 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5439 label
= enclosing
->switch_statement()->break_label();
5440 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5441 label
= enclosing
->type_switch_statement()->break_label();
5442 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5443 label
= enclosing
->select_statement()->break_label();
5447 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5451 // ContinueStat = "continue" [ identifier ] .
5454 Parse::continue_stat()
5456 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5457 Location location
= this->location();
5459 const Token
* token
= this->advance_token();
5460 Statement
* enclosing
;
5461 if (!token
->is_identifier())
5463 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5465 error_at(this->location(), "continue statement not within for");
5468 enclosing
= this->continue_stack_
->back().first
;
5472 enclosing
= this->find_bc_statement(this->continue_stack_
,
5473 token
->identifier());
5474 if (enclosing
== NULL
)
5476 // If there is a label with this name, mark it as used to
5477 // avoid a useless error about an unused label.
5478 this->gogo_
->add_label_reference(token
->identifier(),
5479 Linemap::unknown_location(), false);
5481 error_at(token
->location(), "invalid continue label %qs",
5482 Gogo::message_name(token
->identifier()).c_str());
5483 this->advance_token();
5486 this->advance_token();
5489 Unnamed_label
* label
;
5490 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5491 label
= enclosing
->for_statement()->continue_label();
5492 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5493 label
= enclosing
->for_range_statement()->continue_label();
5497 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5501 // GotoStat = "goto" identifier .
5506 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5507 Location location
= this->location();
5508 const Token
* token
= this->advance_token();
5509 if (!token
->is_identifier())
5510 error_at(this->location(), "expected label for goto");
5513 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5515 Statement
* s
= Statement::make_goto_statement(label
, location
);
5516 this->gogo_
->add_statement(s
);
5517 this->advance_token();
5521 // PackageClause = "package" PackageName .
5524 Parse::package_clause()
5526 const Token
* token
= this->peek_token();
5527 Location location
= token
->location();
5529 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5531 error_at(this->location(), "program must start with package clause");
5536 token
= this->advance_token();
5537 if (token
->is_identifier())
5539 name
= token
->identifier();
5542 error_at(this->location(), "invalid package name _");
5543 name
= Gogo::erroneous_name();
5545 this->advance_token();
5549 error_at(this->location(), "package name must be an identifier");
5553 this->gogo_
->set_package_name(name
, location
);
5556 // ImportDecl = "import" Decl<ImportSpec> .
5559 Parse::import_decl()
5561 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5562 this->advance_token();
5563 this->decl(&Parse::import_spec
, NULL
);
5566 // ImportSpec = [ "." | PackageName ] PackageFileName .
5569 Parse::import_spec(void*)
5571 const Token
* token
= this->peek_token();
5572 Location location
= token
->location();
5574 std::string local_name
;
5575 bool is_local_name_exported
= false;
5576 if (token
->is_op(OPERATOR_DOT
))
5579 token
= this->advance_token();
5581 else if (token
->is_identifier())
5583 local_name
= token
->identifier();
5584 is_local_name_exported
= token
->is_identifier_exported();
5585 token
= this->advance_token();
5588 if (!token
->is_string())
5590 error_at(this->location(), "import statement not a string");
5591 this->advance_token();
5595 this->gogo_
->import_package(token
->string_value(), local_name
,
5596 is_local_name_exported
, location
);
5598 this->advance_token();
5601 // SourceFile = PackageClause ";" { ImportDecl ";" }
5602 // { TopLevelDecl ";" } .
5607 this->package_clause();
5609 const Token
* token
= this->peek_token();
5610 if (token
->is_op(OPERATOR_SEMICOLON
))
5611 token
= this->advance_token();
5613 error_at(this->location(),
5614 "expected %<;%> or newline after package clause");
5616 while (token
->is_keyword(KEYWORD_IMPORT
))
5618 this->import_decl();
5619 token
= this->peek_token();
5620 if (token
->is_op(OPERATOR_SEMICOLON
))
5621 token
= this->advance_token();
5623 error_at(this->location(),
5624 "expected %<;%> or newline after import declaration");
5627 while (!token
->is_eof())
5629 if (this->declaration_may_start_here())
5630 this->declaration();
5633 error_at(this->location(), "expected declaration");
5634 this->gogo_
->mark_locals_used();
5636 this->advance_token();
5637 while (!this->peek_token()->is_eof()
5638 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5639 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5640 if (!this->peek_token()->is_eof()
5641 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5642 this->advance_token();
5644 token
= this->peek_token();
5645 if (token
->is_op(OPERATOR_SEMICOLON
))
5646 token
= this->advance_token();
5647 else if (!token
->is_eof() || !saw_errors())
5649 if (token
->is_op(OPERATOR_CHANOP
))
5650 error_at(this->location(),
5651 ("send statement used as value; "
5652 "use select for non-blocking send"));
5654 error_at(this->location(),
5655 "expected %<;%> or newline after top level declaration");
5656 this->skip_past_error(OPERATOR_INVALID
);
5661 // Reset the current iota value.
5669 // Return the current iota value.
5677 // Increment the current iota value.
5680 Parse::increment_iota()
5685 // Skip forward to a semicolon or OP. OP will normally be
5686 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5687 // past it and return. If we find OP, it will be the next token to
5688 // read. Return true if we are OK, false if we found EOF.
5691 Parse::skip_past_error(Operator op
)
5693 this->gogo_
->mark_locals_used();
5694 const Token
* token
= this->peek_token();
5695 while (!token
->is_op(op
))
5697 if (token
->is_eof())
5699 if (token
->is_op(OPERATOR_SEMICOLON
))
5701 this->advance_token();
5704 token
= this->advance_token();
5709 // Check that an expression is not a sink.
5712 Parse::verify_not_sink(Expression
* expr
)
5714 if (expr
->is_sink_expression())
5716 error_at(expr
->location(), "cannot use _ as value");
5717 expr
= Expression::make_error(expr
->location());
5720 // If this can not be a sink, and it is a variable, then we are
5721 // using the variable, not just assigning to it.
5722 Var_expression
* ve
= expr
->var_expression();
5724 this->mark_var_used(ve
->named_object());
5729 // Mark a variable as used.
5732 Parse::mark_var_used(Named_object
* no
)
5734 if (no
->is_variable())
5736 no
->var_value()->set_is_used();
5738 // When a type switch uses := to define a variable, then for
5739 // each case with a single type we introduce a new variable with
5740 // the appropriate type. When we do, if the newly introduced
5741 // variable is used, then the type switch variable is used.
5742 Type_switch_vars::iterator p
= this->type_switch_vars_
.find(no
);
5743 if (p
!= this->type_switch_vars_
.end())
5744 p
->second
->var_value()->set_is_used();