1 // parse.cc -- Go frontend parser.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 #include "statements.h"
13 #include "expressions.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var
& v1
,
22 const Enclosing_var
& v2
)
24 if (v1
.var() == v2
.var())
27 const std::string
& n1(v1
.var()->name());
28 const std::string
& n2(v2
.var()->name());
29 int i
= n1
.compare(n2
);
35 // If we get here it means that a single nested function refers to
36 // two different variables defined in enclosing functions, and both
37 // variables have the same name. I think this is impossible.
43 Parse::Parse(Lex
* lex
, Gogo
* gogo
)
45 token_(Token::make_invalid_token(Linemap::unknown_location())),
46 unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
47 unget_token_valid_(false),
48 is_erroneous_function_(false),
51 continue_stack_(NULL
),
58 // Return the current token.
63 if (this->unget_token_valid_
)
64 return &this->unget_token_
;
65 if (this->token_
.is_invalid())
66 this->token_
= this->lex_
->next_token();
70 // Advance to the next token and return it.
73 Parse::advance_token()
75 if (this->unget_token_valid_
)
77 this->unget_token_valid_
= false;
78 if (!this->token_
.is_invalid())
81 this->token_
= this->lex_
->next_token();
85 // Push a token back on the input stream.
88 Parse::unget_token(const Token
& token
)
90 go_assert(!this->unget_token_valid_
);
91 this->unget_token_
= token
;
92 this->unget_token_valid_
= true;
95 // The location of the current token.
100 return this->peek_token()->location();
103 // IdentifierList = identifier { "," identifier } .
106 Parse::identifier_list(Typed_identifier_list
* til
)
108 const Token
* token
= this->peek_token();
111 if (!token
->is_identifier())
113 error_at(this->location(), "expected identifier");
117 this->gogo_
->pack_hidden_name(token
->identifier(),
118 token
->is_identifier_exported());
119 til
->push_back(Typed_identifier(name
, NULL
, token
->location()));
120 token
= this->advance_token();
121 if (!token
->is_op(OPERATOR_COMMA
))
123 token
= this->advance_token();
127 // ExpressionList = Expression { "," Expression } .
129 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
132 // If MAY_BE_SINK is true, the expressions in the list may be "_".
135 Parse::expression_list(Expression
* first
, bool may_be_sink
,
136 bool may_be_composite_lit
)
138 Expression_list
* ret
= new Expression_list();
140 ret
->push_back(first
);
143 ret
->push_back(this->expression(PRECEDENCE_NORMAL
, may_be_sink
,
144 may_be_composite_lit
, NULL
, NULL
));
146 const Token
* token
= this->peek_token();
147 if (!token
->is_op(OPERATOR_COMMA
))
150 // Most expression lists permit a trailing comma.
151 Location location
= token
->location();
152 this->advance_token();
153 if (!this->expression_may_start_here())
155 this->unget_token(Token::make_operator_token(OPERATOR_COMMA
,
162 // QualifiedIdent = [ PackageName "." ] identifier .
163 // PackageName = identifier .
165 // This sets *PNAME to the identifier and sets *PPACKAGE to the
166 // package or NULL if there isn't one. This returns true on success,
167 // false on failure in which case it will have emitted an error
171 Parse::qualified_ident(std::string
* pname
, Named_object
** ppackage
)
173 const Token
* token
= this->peek_token();
174 if (!token
->is_identifier())
176 error_at(this->location(), "expected identifier");
180 std::string name
= token
->identifier();
181 bool is_exported
= token
->is_identifier_exported();
182 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
184 token
= this->advance_token();
185 if (!token
->is_op(OPERATOR_DOT
))
192 Named_object
* package
= this->gogo_
->lookup(name
, NULL
);
193 if (package
== NULL
|| !package
->is_package())
195 error_at(this->location(), "expected package");
196 // We expect . IDENTIFIER; skip both.
197 if (this->advance_token()->is_identifier())
198 this->advance_token();
202 package
->package_value()->set_used();
204 token
= this->advance_token();
205 if (!token
->is_identifier())
207 error_at(this->location(), "expected identifier");
211 name
= token
->identifier();
215 error_at(this->location(), "invalid use of %<_%>");
216 name
= Gogo::erroneous_name();
219 if (package
->name() == this->gogo_
->package_name())
220 name
= this->gogo_
->pack_hidden_name(name
,
221 token
->is_identifier_exported());
226 this->advance_token();
231 // Type = TypeName | TypeLit | "(" Type ")" .
233 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
234 // SliceType | MapType | ChannelType .
239 const Token
* token
= this->peek_token();
240 if (token
->is_identifier())
241 return this->type_name(true);
242 else if (token
->is_op(OPERATOR_LSQUARE
))
243 return this->array_type(false);
244 else if (token
->is_keyword(KEYWORD_CHAN
)
245 || token
->is_op(OPERATOR_CHANOP
))
246 return this->channel_type();
247 else if (token
->is_keyword(KEYWORD_INTERFACE
))
248 return this->interface_type();
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
);
978 while (this->peek_token()->is_op(OPERATOR_COMMA
))
980 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
982 if (is_varargs
!= NULL
&& *is_varargs
)
984 error_at(this->location(), "%<...%> must be last parameter");
987 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
);
991 error_at(location
, "invalid named/anonymous mix");
1002 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1005 Parse::parameter_decl(bool parameters_have_names
,
1006 Typed_identifier_list
* til
,
1010 if (!parameters_have_names
)
1013 Location location
= this->location();
1014 if (!this->peek_token()->is_identifier())
1016 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1017 type
= this->type();
1020 if (is_varargs
== NULL
)
1021 error_at(this->location(), "invalid use of %<...%>");
1024 this->advance_token();
1025 if (is_varargs
== NULL
1026 && this->peek_token()->is_op(OPERATOR_RPAREN
))
1027 type
= Type::make_error_type();
1030 Type
* element_type
= this->type();
1031 type
= Type::make_array_type(element_type
, NULL
);
1037 type
= this->type_name(false);
1038 if (type
->is_error_type()
1039 || (!this->peek_token()->is_op(OPERATOR_COMMA
)
1040 && !this->peek_token()->is_op(OPERATOR_RPAREN
)))
1043 while (!this->peek_token()->is_op(OPERATOR_COMMA
)
1044 && !this->peek_token()->is_op(OPERATOR_RPAREN
))
1045 this->advance_token();
1048 if (!type
->is_error_type())
1049 til
->push_back(Typed_identifier("", type
, location
));
1053 size_t orig_count
= til
->size();
1054 if (this->peek_token()->is_identifier())
1055 this->identifier_list(til
);
1058 size_t new_count
= til
->size();
1061 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1062 type
= this->type();
1065 if (is_varargs
== NULL
)
1066 error_at(this->location(), "invalid use of %<...%>");
1067 else if (new_count
> orig_count
+ 1)
1068 error_at(this->location(), "%<...%> only permits one name");
1071 this->advance_token();
1072 Type
* element_type
= this->type();
1073 type
= Type::make_array_type(element_type
, NULL
);
1075 for (size_t i
= orig_count
; i
< new_count
; ++i
)
1076 til
->set_type(i
, type
);
1080 // Result = Parameters | Type .
1082 // This returns false on a parse error.
1085 Parse::result(Typed_identifier_list
** presults
)
1087 if (this->peek_token()->is_op(OPERATOR_LPAREN
))
1088 return this->parameters(presults
, NULL
);
1091 Location location
= this->location();
1092 Type
* type
= this->type();
1093 if (type
->is_error_type())
1098 Typed_identifier_list
* til
= new Typed_identifier_list();
1099 til
->push_back(Typed_identifier("", type
, location
));
1105 // Block = "{" [ StatementList ] "}" .
1107 // Returns the location of the closing brace.
1112 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
1114 Location loc
= this->location();
1115 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1116 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1117 error_at(loc
, "unexpected semicolon or newline before %<{%>");
1120 error_at(this->location(), "expected %<{%>");
1121 return Linemap::unknown_location();
1125 const Token
* token
= this->advance_token();
1127 if (!token
->is_op(OPERATOR_RCURLY
))
1129 this->statement_list();
1130 token
= this->peek_token();
1131 if (!token
->is_op(OPERATOR_RCURLY
))
1133 if (!token
->is_eof() || !saw_errors())
1134 error_at(this->location(), "expected %<}%>");
1136 this->gogo_
->mark_locals_used();
1138 // Skip ahead to the end of the block, in hopes of avoiding
1139 // lots of meaningless errors.
1140 Location ret
= token
->location();
1142 while (!token
->is_eof())
1144 if (token
->is_op(OPERATOR_LCURLY
))
1146 else if (token
->is_op(OPERATOR_RCURLY
))
1151 this->advance_token();
1155 token
= this->advance_token();
1156 ret
= token
->location();
1162 Location ret
= token
->location();
1163 this->advance_token();
1167 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1168 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1171 Parse::interface_type()
1173 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE
));
1174 Location location
= this->location();
1176 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
1178 Location token_loc
= this->location();
1179 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1180 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1181 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
1184 error_at(this->location(), "expected %<{%>");
1185 return Type::make_error_type();
1188 this->advance_token();
1190 Typed_identifier_list
* methods
= new Typed_identifier_list();
1191 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1193 this->method_spec(methods
);
1194 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1196 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
1198 this->method_spec(methods
);
1200 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1202 error_at(this->location(), "expected %<}%>");
1203 while (!this->advance_token()->is_op(OPERATOR_RCURLY
))
1205 if (this->peek_token()->is_eof())
1206 return Type::make_error_type();
1210 this->advance_token();
1212 if (methods
->empty())
1218 Interface_type
* ret
= Type::make_interface_type(methods
, location
);
1219 this->gogo_
->record_interface_type(ret
);
1223 // MethodSpec = MethodName Signature | InterfaceTypeName .
1224 // MethodName = identifier .
1225 // InterfaceTypeName = TypeName .
1228 Parse::method_spec(Typed_identifier_list
* methods
)
1230 const Token
* token
= this->peek_token();
1231 if (!token
->is_identifier())
1233 error_at(this->location(), "expected identifier");
1237 std::string name
= token
->identifier();
1238 bool is_exported
= token
->is_identifier_exported();
1239 Location location
= token
->location();
1241 if (this->advance_token()->is_op(OPERATOR_LPAREN
))
1243 // This is a MethodName.
1244 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1245 Type
* type
= this->signature(NULL
, location
);
1248 methods
->push_back(Typed_identifier(name
, type
, location
));
1252 this->unget_token(Token::make_identifier_token(name
, is_exported
,
1254 Type
* type
= this->type_name(false);
1255 if (type
->is_error_type()
1256 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1257 && !this->peek_token()->is_op(OPERATOR_RCURLY
)))
1259 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1260 error_at(this->location(),
1261 "name list not allowed in interface type");
1263 error_at(location
, "expected signature or type name");
1264 this->gogo_
->mark_locals_used();
1265 token
= this->peek_token();
1266 while (!token
->is_eof()
1267 && !token
->is_op(OPERATOR_SEMICOLON
)
1268 && !token
->is_op(OPERATOR_RCURLY
))
1269 token
= this->advance_token();
1272 // This must be an interface type, but we can't check that now.
1273 // We check it and pull out the methods in
1274 // Interface_type::do_verify.
1275 methods
->push_back(Typed_identifier("", type
, location
));
1279 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1282 Parse::declaration()
1284 const Token
* token
= this->peek_token();
1286 bool saw_nointerface
= this->lex_
->get_and_clear_nointerface();
1287 if (saw_nointerface
&& !token
->is_keyword(KEYWORD_FUNC
))
1288 warning_at(token
->location(), 0,
1289 "ignoring magic //go:nointerface comment before non-method");
1291 if (token
->is_keyword(KEYWORD_CONST
))
1293 else if (token
->is_keyword(KEYWORD_TYPE
))
1295 else if (token
->is_keyword(KEYWORD_VAR
))
1297 else if (token
->is_keyword(KEYWORD_FUNC
))
1298 this->function_decl(saw_nointerface
);
1301 error_at(this->location(), "expected declaration");
1302 this->advance_token();
1307 Parse::declaration_may_start_here()
1309 const Token
* token
= this->peek_token();
1310 return (token
->is_keyword(KEYWORD_CONST
)
1311 || token
->is_keyword(KEYWORD_TYPE
)
1312 || token
->is_keyword(KEYWORD_VAR
)
1313 || token
->is_keyword(KEYWORD_FUNC
));
1316 // Decl<P> = P | "(" [ List<P> ] ")" .
1319 Parse::decl(void (Parse::*pfn
)(void*), void* varg
)
1321 if (this->peek_token()->is_eof())
1324 error_at(this->location(), "unexpected end of file");
1328 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1332 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1334 this->list(pfn
, varg
, true);
1335 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1337 error_at(this->location(), "missing %<)%>");
1338 while (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1340 if (this->peek_token()->is_eof())
1345 this->advance_token();
1349 // List<P> = P { ";" P } [ ";" ] .
1351 // In order to pick up the trailing semicolon we need to know what
1352 // might follow. This is either a '}' or a ')'.
1355 Parse::list(void (Parse::*pfn
)(void*), void* varg
, bool follow_is_paren
)
1358 Operator follow
= follow_is_paren
? OPERATOR_RPAREN
: OPERATOR_RCURLY
;
1359 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1360 || this->peek_token()->is_op(OPERATOR_COMMA
))
1362 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1363 error_at(this->location(), "unexpected comma");
1364 if (this->advance_token()->is_op(follow
))
1370 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1375 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST
));
1376 this->advance_token();
1379 Type
* last_type
= NULL
;
1380 Expression_list
* last_expr_list
= NULL
;
1382 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1383 this->const_spec(&last_type
, &last_expr_list
);
1386 this->advance_token();
1387 while (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1389 this->const_spec(&last_type
, &last_expr_list
);
1390 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1391 this->advance_token();
1392 else if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1394 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1395 if (!this->skip_past_error(OPERATOR_RPAREN
))
1399 this->advance_token();
1402 if (last_expr_list
!= NULL
)
1403 delete last_expr_list
;
1406 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1409 Parse::const_spec(Type
** last_type
, Expression_list
** last_expr_list
)
1411 Typed_identifier_list til
;
1412 this->identifier_list(&til
);
1415 if (this->type_may_start_here())
1417 type
= this->type();
1419 *last_expr_list
= NULL
;
1422 Expression_list
*expr_list
;
1423 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1425 if (*last_expr_list
== NULL
)
1427 error_at(this->location(), "expected %<=%>");
1431 expr_list
= new Expression_list
;
1432 for (Expression_list::const_iterator p
= (*last_expr_list
)->begin();
1433 p
!= (*last_expr_list
)->end();
1435 expr_list
->push_back((*p
)->copy());
1439 this->advance_token();
1440 expr_list
= this->expression_list(NULL
, false, true);
1442 if (*last_expr_list
!= NULL
)
1443 delete *last_expr_list
;
1444 *last_expr_list
= expr_list
;
1447 Expression_list::const_iterator pe
= expr_list
->begin();
1448 for (Typed_identifier_list::iterator pi
= til
.begin();
1452 if (pe
== expr_list
->end())
1454 error_at(this->location(), "not enough initializers");
1460 if (!Gogo::is_sink_name(pi
->name()))
1461 this->gogo_
->add_constant(*pi
, *pe
, this->iota_value());
1466 snprintf(buf
, sizeof buf
, ".$sinkconst%d", count
);
1468 Typed_identifier
ti(std::string(buf
), type
, pi
->location());
1469 Named_object
* no
= this->gogo_
->add_constant(ti
, *pe
, this->iota_value());
1470 no
->const_value()->set_is_sink();
1473 if (pe
!= expr_list
->end())
1474 error_at(this->location(), "too many initializers");
1476 this->increment_iota();
1481 // TypeDecl = "type" Decl<TypeSpec> .
1486 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE
));
1487 this->advance_token();
1488 this->decl(&Parse::type_spec
, NULL
);
1491 // TypeSpec = identifier Type .
1494 Parse::type_spec(void*)
1496 const Token
* token
= this->peek_token();
1497 if (!token
->is_identifier())
1499 error_at(this->location(), "expected identifier");
1502 std::string name
= token
->identifier();
1503 bool is_exported
= token
->is_identifier_exported();
1504 Location location
= token
->location();
1505 token
= this->advance_token();
1507 // The scope of the type name starts at the point where the
1508 // identifier appears in the source code. We implement this by
1509 // declaring the type before we read the type definition.
1510 Named_object
* named_type
= NULL
;
1513 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1514 named_type
= this->gogo_
->declare_type(name
, location
);
1518 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1519 type
= this->type();
1522 error_at(this->location(),
1523 "unexpected semicolon or newline in type declaration");
1524 type
= Type::make_error_type();
1525 this->advance_token();
1528 if (type
->is_error_type())
1530 this->gogo_
->mark_locals_used();
1531 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1532 && !this->peek_token()->is_eof())
1533 this->advance_token();
1538 if (named_type
->is_type_declaration())
1540 Type
* ftype
= type
->forwarded();
1541 if (ftype
->forward_declaration_type() != NULL
1542 && (ftype
->forward_declaration_type()->named_object()
1545 error_at(location
, "invalid recursive type");
1546 type
= Type::make_error_type();
1549 this->gogo_
->define_type(named_type
,
1550 Type::make_named_type(named_type
, type
,
1552 go_assert(named_type
->package() == NULL
);
1556 // This will probably give a redefinition error.
1557 this->gogo_
->add_type(name
, type
, location
);
1562 // VarDecl = "var" Decl<VarSpec> .
1567 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR
));
1568 this->advance_token();
1569 this->decl(&Parse::var_spec
, NULL
);
1572 // VarSpec = IdentifierList
1573 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1576 Parse::var_spec(void*)
1578 // Get the variable names.
1579 Typed_identifier_list til
;
1580 this->identifier_list(&til
);
1582 Location location
= this->location();
1585 Expression_list
* init
= NULL
;
1586 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1588 type
= this->type();
1589 if (type
->is_error_type())
1591 this->gogo_
->mark_locals_used();
1592 while (!this->peek_token()->is_op(OPERATOR_EQ
)
1593 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1594 && !this->peek_token()->is_eof())
1595 this->advance_token();
1597 if (this->peek_token()->is_op(OPERATOR_EQ
))
1599 this->advance_token();
1600 init
= this->expression_list(NULL
, false, true);
1605 this->advance_token();
1606 init
= this->expression_list(NULL
, false, true);
1609 this->init_vars(&til
, type
, init
, false, location
);
1615 // Create variables. TIL is a list of variable names. If TYPE is not
1616 // NULL, it is the type of all the variables. If INIT is not NULL, it
1617 // is an initializer list for the variables.
1620 Parse::init_vars(const Typed_identifier_list
* til
, Type
* type
,
1621 Expression_list
* init
, bool is_coloneq
,
1624 // Check for an initialization which can yield multiple values.
1625 if (init
!= NULL
&& init
->size() == 1 && til
->size() > 1)
1627 if (this->init_vars_from_call(til
, type
, *init
->begin(), is_coloneq
,
1630 if (this->init_vars_from_map(til
, type
, *init
->begin(), is_coloneq
,
1633 if (this->init_vars_from_receive(til
, type
, *init
->begin(), is_coloneq
,
1636 if (this->init_vars_from_type_guard(til
, type
, *init
->begin(),
1637 is_coloneq
, location
))
1641 if (init
!= NULL
&& init
->size() != til
->size())
1643 if (init
->empty() || !init
->front()->is_error_expression())
1644 error_at(location
, "wrong number of initializations");
1647 type
= Type::make_error_type();
1650 // Note that INIT was already parsed with the old name bindings, so
1651 // we don't have to worry that it will accidentally refer to the
1652 // newly declared variables. But we do have to worry about a mix of
1653 // newly declared variables and old variables if the old variables
1654 // appear in the initializations.
1656 Expression_list::const_iterator pexpr
;
1658 pexpr
= init
->begin();
1659 bool any_new
= false;
1660 Expression_list
* vars
= new Expression_list();
1661 Expression_list
* vals
= new Expression_list();
1662 for (Typed_identifier_list::const_iterator p
= til
->begin();
1667 go_assert(pexpr
!= init
->end());
1668 this->init_var(*p
, type
, init
== NULL
? NULL
: *pexpr
, is_coloneq
,
1669 false, &any_new
, vars
, vals
);
1674 go_assert(pexpr
== init
->end());
1675 if (is_coloneq
&& !any_new
)
1676 error_at(location
, "variables redeclared but no variable is new");
1677 this->finish_init_vars(vars
, vals
, location
);
1680 // See if we need to initialize a list of variables from a function
1681 // call. This returns true if we have set up the variables and the
1685 Parse::init_vars_from_call(const Typed_identifier_list
* vars
, Type
* type
,
1686 Expression
* expr
, bool is_coloneq
,
1689 Call_expression
* call
= expr
->call_expression();
1693 // This is a function call. We can't check here whether it returns
1694 // the right number of values, but it might. Declare the variables,
1695 // and then assign the results of the call to them.
1697 call
->set_expected_result_count(vars
->size());
1699 Named_object
* first_var
= NULL
;
1700 unsigned int index
= 0;
1701 bool any_new
= false;
1702 Expression_list
* ivars
= new Expression_list();
1703 Expression_list
* ivals
= new Expression_list();
1704 for (Typed_identifier_list::const_iterator pv
= vars
->begin();
1708 Expression
* init
= Expression::make_call_result(call
, index
);
1709 Named_object
* no
= this->init_var(*pv
, type
, init
, is_coloneq
, false,
1710 &any_new
, ivars
, ivals
);
1712 if (this->gogo_
->in_global_scope() && no
->is_variable())
1714 if (first_var
== NULL
)
1718 // The subsequent vars have an implicit dependency on
1719 // the first one, so that everything gets initialized in
1720 // the right order and so that we detect cycles
1722 this->gogo_
->record_var_depends_on(no
->var_value(), first_var
);
1727 if (is_coloneq
&& !any_new
)
1728 error_at(location
, "variables redeclared but no variable is new");
1730 this->finish_init_vars(ivars
, ivals
, location
);
1735 // See if we need to initialize a pair of values from a map index
1736 // expression. This returns true if we have set up the variables and
1737 // the initialization.
1740 Parse::init_vars_from_map(const Typed_identifier_list
* vars
, Type
* type
,
1741 Expression
* expr
, bool is_coloneq
,
1744 Index_expression
* index
= expr
->index_expression();
1747 if (vars
->size() != 2)
1750 // This is an index which is being assigned to two variables. It
1751 // must be a map index. Declare the variables, and then assign the
1752 // results of the map index.
1753 bool any_new
= false;
1754 Typed_identifier_list::const_iterator p
= vars
->begin();
1755 Expression
* init
= type
== NULL
? index
: NULL
;
1756 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1757 type
== NULL
, &any_new
, NULL
, NULL
);
1758 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1759 val_no
->var_value()->set_type_from_init_tuple();
1760 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1763 Type
* var_type
= type
;
1764 if (var_type
== NULL
)
1765 var_type
= Type::lookup_bool_type();
1766 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1767 &any_new
, NULL
, NULL
);
1768 Expression
* present_var
= Expression::make_var_reference(no
, location
);
1770 if (is_coloneq
&& !any_new
)
1771 error_at(location
, "variables redeclared but no variable is new");
1773 Statement
* s
= Statement::make_tuple_map_assignment(val_var
, present_var
,
1776 if (!this->gogo_
->in_global_scope())
1777 this->gogo_
->add_statement(s
);
1778 else if (!val_no
->is_sink())
1780 if (val_no
->is_variable())
1781 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1783 else if (!no
->is_sink())
1785 if (no
->is_variable())
1786 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1790 // Execute the map index expression just so that we can fail if
1792 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1794 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1800 // See if we need to initialize a pair of values from a receive
1801 // expression. This returns true if we have set up the variables and
1802 // the initialization.
1805 Parse::init_vars_from_receive(const Typed_identifier_list
* vars
, Type
* type
,
1806 Expression
* expr
, bool is_coloneq
,
1809 Receive_expression
* receive
= expr
->receive_expression();
1810 if (receive
== NULL
)
1812 if (vars
->size() != 2)
1815 // This is a receive expression which is being assigned to two
1816 // variables. Declare the variables, and then assign the results of
1818 bool any_new
= false;
1819 Typed_identifier_list::const_iterator p
= vars
->begin();
1820 Expression
* init
= type
== NULL
? receive
: NULL
;
1821 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1822 type
== NULL
, &any_new
, NULL
, NULL
);
1823 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1824 val_no
->var_value()->set_type_from_init_tuple();
1825 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1828 Type
* var_type
= type
;
1829 if (var_type
== NULL
)
1830 var_type
= Type::lookup_bool_type();
1831 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1832 &any_new
, NULL
, NULL
);
1833 Expression
* received_var
= Expression::make_var_reference(no
, location
);
1835 if (is_coloneq
&& !any_new
)
1836 error_at(location
, "variables redeclared but no variable is new");
1838 Statement
* s
= Statement::make_tuple_receive_assignment(val_var
,
1843 if (!this->gogo_
->in_global_scope())
1844 this->gogo_
->add_statement(s
);
1845 else if (!val_no
->is_sink())
1847 if (val_no
->is_variable())
1848 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1850 else if (!no
->is_sink())
1852 if (no
->is_variable())
1853 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1857 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1859 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1865 // See if we need to initialize a pair of values from a type guard
1866 // expression. This returns true if we have set up the variables and
1867 // the initialization.
1870 Parse::init_vars_from_type_guard(const Typed_identifier_list
* vars
,
1871 Type
* type
, Expression
* expr
,
1872 bool is_coloneq
, Location location
)
1874 Type_guard_expression
* type_guard
= expr
->type_guard_expression();
1875 if (type_guard
== NULL
)
1877 if (vars
->size() != 2)
1880 // This is a type guard expression which is being assigned to two
1881 // variables. Declare the variables, and then assign the results of
1883 bool any_new
= false;
1884 Typed_identifier_list::const_iterator p
= vars
->begin();
1885 Type
* var_type
= type
;
1886 if (var_type
== NULL
)
1887 var_type
= type_guard
->type();
1888 Named_object
* val_no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1889 &any_new
, NULL
, NULL
);
1890 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1894 if (var_type
== NULL
)
1895 var_type
= Type::lookup_bool_type();
1896 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1897 &any_new
, NULL
, NULL
);
1898 Expression
* ok_var
= Expression::make_var_reference(no
, location
);
1900 Expression
* texpr
= type_guard
->expr();
1901 Type
* t
= type_guard
->type();
1902 Statement
* s
= Statement::make_tuple_type_guard_assignment(val_var
, ok_var
,
1906 if (is_coloneq
&& !any_new
)
1907 error_at(location
, "variables redeclared but no variable is new");
1909 if (!this->gogo_
->in_global_scope())
1910 this->gogo_
->add_statement(s
);
1911 else if (!val_no
->is_sink())
1913 if (val_no
->is_variable())
1914 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1916 else if (!no
->is_sink())
1918 if (no
->is_variable())
1919 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1923 Named_object
* dummy
= this->create_dummy_global(type
, NULL
, location
);
1924 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1930 // Create a single variable. If IS_COLONEQ is true, we permit
1931 // redeclarations in the same block, and we set *IS_NEW when we find a
1932 // new variable which is not a redeclaration.
1935 Parse::init_var(const Typed_identifier
& tid
, Type
* type
, Expression
* init
,
1936 bool is_coloneq
, bool type_from_init
, bool* is_new
,
1937 Expression_list
* vars
, Expression_list
* vals
)
1939 Location location
= tid
.location();
1941 if (Gogo::is_sink_name(tid
.name()))
1943 if (!type_from_init
&& init
!= NULL
)
1945 if (this->gogo_
->in_global_scope())
1946 return this->create_dummy_global(type
, init
, location
);
1949 // Create a dummy variable so that we will check whether the
1950 // initializer can be assigned to the type.
1951 Variable
* var
= new Variable(type
, init
, false, false, false,
1956 snprintf(buf
, sizeof buf
, "sink$%d", count
);
1958 return this->gogo_
->add_variable(buf
, var
);
1962 this->gogo_
->add_type_to_verify(type
);
1963 return this->gogo_
->add_sink();
1968 Named_object
* no
= this->gogo_
->lookup_in_block(tid
.name());
1970 && (no
->is_variable() || no
->is_result_variable()))
1972 // INIT may be NULL even when IS_COLONEQ is true for cases
1973 // like v, ok := x.(int).
1974 if (!type_from_init
&& init
!= NULL
)
1976 go_assert(vars
!= NULL
&& vals
!= NULL
);
1977 vars
->push_back(Expression::make_var_reference(no
, location
));
1978 vals
->push_back(init
);
1984 Variable
* var
= new Variable(type
, init
, this->gogo_
->in_global_scope(),
1985 false, false, location
);
1986 Named_object
* no
= this->gogo_
->add_variable(tid
.name(), var
);
1987 if (!no
->is_variable())
1989 // The name is already defined, so we just gave an error.
1990 return this->gogo_
->add_sink();
1995 // Create a dummy global variable to force an initializer to be run in
1996 // the right place. This is used when a sink variable is initialized
2000 Parse::create_dummy_global(Type
* type
, Expression
* init
,
2003 if (type
== NULL
&& init
== NULL
)
2004 type
= Type::lookup_bool_type();
2005 Variable
* var
= new Variable(type
, init
, true, false, false, location
);
2008 snprintf(buf
, sizeof buf
, "_.%d", count
);
2010 return this->gogo_
->add_variable(buf
, var
);
2013 // Finish the variable initialization by executing any assignments to
2014 // existing variables when using :=. These must be done as a tuple
2015 // assignment in case of something like n, a, b := 1, b, a.
2018 Parse::finish_init_vars(Expression_list
* vars
, Expression_list
* vals
,
2026 else if (vars
->size() == 1)
2028 go_assert(!this->gogo_
->in_global_scope());
2029 this->gogo_
->add_statement(Statement::make_assignment(vars
->front(),
2037 go_assert(!this->gogo_
->in_global_scope());
2038 this->gogo_
->add_statement(Statement::make_tuple_assignment(vars
, vals
,
2043 // SimpleVarDecl = identifier ":=" Expression .
2045 // We've already seen the identifier.
2047 // FIXME: We also have to implement
2048 // IdentifierList ":=" ExpressionList
2049 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2050 // tuple assignments here as well.
2052 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2053 // side may be a composite literal.
2055 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2058 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2059 // guard (var := expr.("type") using the literal keyword "type").
2062 Parse::simple_var_decl_or_assignment(const std::string
& name
,
2064 bool may_be_composite_lit
,
2065 Range_clause
* p_range_clause
,
2066 Type_switch
* p_type_switch
)
2068 Typed_identifier_list til
;
2069 til
.push_back(Typed_identifier(name
, NULL
, location
));
2071 // We've seen one identifier. If we see a comma now, this could be
2073 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2075 go_assert(p_type_switch
== NULL
);
2078 const Token
* token
= this->advance_token();
2079 if (!token
->is_identifier())
2082 std::string id
= token
->identifier();
2083 bool is_id_exported
= token
->is_identifier_exported();
2084 Location id_location
= token
->location();
2086 token
= this->advance_token();
2087 if (!token
->is_op(OPERATOR_COMMA
))
2089 if (token
->is_op(OPERATOR_COLONEQ
))
2091 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2092 til
.push_back(Typed_identifier(id
, NULL
, location
));
2095 this->unget_token(Token::make_identifier_token(id
,
2101 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2102 til
.push_back(Typed_identifier(id
, NULL
, location
));
2105 // We have a comma separated list of identifiers in TIL. If the
2106 // next token is COLONEQ, then this is a simple var decl, and we
2107 // have the complete list of identifiers. If the next token is
2108 // not COLONEQ, then the only valid parse is a tuple assignment.
2109 // The list of identifiers we have so far is really a list of
2110 // expressions. There are more expressions following.
2112 if (!this->peek_token()->is_op(OPERATOR_COLONEQ
))
2114 Expression_list
* exprs
= new Expression_list
;
2115 for (Typed_identifier_list::const_iterator p
= til
.begin();
2118 exprs
->push_back(this->id_to_expression(p
->name(),
2121 Expression_list
* more_exprs
=
2122 this->expression_list(NULL
, true, may_be_composite_lit
);
2123 for (Expression_list::const_iterator p
= more_exprs
->begin();
2124 p
!= more_exprs
->end();
2126 exprs
->push_back(*p
);
2129 this->tuple_assignment(exprs
, may_be_composite_lit
, p_range_clause
);
2134 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ
));
2135 const Token
* token
= this->advance_token();
2137 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
2139 this->range_clause_decl(&til
, p_range_clause
);
2143 Expression_list
* init
;
2144 if (p_type_switch
== NULL
)
2145 init
= this->expression_list(NULL
, false, may_be_composite_lit
);
2148 bool is_type_switch
= false;
2149 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false,
2150 may_be_composite_lit
,
2151 &is_type_switch
, NULL
);
2154 p_type_switch
->found
= true;
2155 p_type_switch
->name
= name
;
2156 p_type_switch
->location
= location
;
2157 p_type_switch
->expr
= expr
;
2161 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
2163 init
= new Expression_list();
2164 init
->push_back(expr
);
2168 this->advance_token();
2169 init
= this->expression_list(expr
, false, may_be_composite_lit
);
2173 this->init_vars(&til
, NULL
, init
, true, location
);
2176 // FunctionDecl = "func" identifier Signature [ Block ] .
2177 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2179 // Deprecated gcc extension:
2180 // FunctionDecl = "func" identifier Signature
2181 // __asm__ "(" string_lit ")" .
2182 // This extension means a function whose real name is the identifier
2183 // inside the asm. This extension will be removed at some future
2184 // date. It has been replaced with //extern comments.
2186 // SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment,
2187 // which means that we omit the method from the type descriptor.
2190 Parse::function_decl(bool saw_nointerface
)
2192 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2193 Location location
= this->location();
2194 std::string extern_name
= this->lex_
->extern_name();
2195 const Token
* token
= this->advance_token();
2197 Typed_identifier
* rec
= NULL
;
2198 if (token
->is_op(OPERATOR_LPAREN
))
2200 rec
= this->receiver();
2201 token
= this->peek_token();
2203 else if (saw_nointerface
)
2205 warning_at(location
, 0,
2206 "ignoring magic //go:nointerface comment before non-method");
2207 saw_nointerface
= false;
2210 if (!token
->is_identifier())
2212 error_at(this->location(), "expected function name");
2217 this->gogo_
->pack_hidden_name(token
->identifier(),
2218 token
->is_identifier_exported());
2220 this->advance_token();
2222 Function_type
* fntype
= this->signature(rec
, this->location());
2224 Named_object
* named_object
= NULL
;
2226 if (this->peek_token()->is_keyword(KEYWORD_ASM
))
2228 if (!this->advance_token()->is_op(OPERATOR_LPAREN
))
2230 error_at(this->location(), "expected %<(%>");
2233 token
= this->advance_token();
2234 if (!token
->is_string())
2236 error_at(this->location(), "expected string");
2239 std::string asm_name
= token
->string_value();
2240 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
2242 error_at(this->location(), "expected %<)%>");
2245 this->advance_token();
2246 if (!Gogo::is_sink_name(name
))
2248 named_object
= this->gogo_
->declare_function(name
, fntype
, location
);
2249 if (named_object
->is_function_declaration())
2250 named_object
->func_declaration_value()->set_asm_name(asm_name
);
2254 // Check for the easy error of a newline before the opening brace.
2255 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
2257 Location semi_loc
= this->location();
2258 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
2259 error_at(this->location(),
2260 "unexpected semicolon or newline before %<{%>");
2262 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
2266 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2268 if (named_object
== NULL
&& !Gogo::is_sink_name(name
))
2271 this->gogo_
->add_erroneous_name(name
);
2274 named_object
= this->gogo_
->declare_function(name
, fntype
,
2276 if (!extern_name
.empty()
2277 && named_object
->is_function_declaration())
2279 Function_declaration
* fd
=
2280 named_object
->func_declaration_value();
2281 fd
->set_asm_name(extern_name
);
2286 if (saw_nointerface
)
2287 warning_at(location
, 0,
2288 ("ignoring magic //go:nointerface comment "
2289 "before declaration"));
2293 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2296 fntype
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2297 this->is_erroneous_function_
= true;
2298 if (!Gogo::is_sink_name(name
))
2299 this->gogo_
->add_erroneous_name(name
);
2300 name
= this->gogo_
->pack_hidden_name("_", false);
2302 named_object
= this->gogo_
->start_function(name
, fntype
, true, location
);
2303 Location end_loc
= this->block();
2304 this->gogo_
->finish_function(end_loc
);
2306 && !this->is_erroneous_function_
2307 && named_object
->is_function())
2308 named_object
->func_value()->set_nointerface();
2309 this->is_erroneous_function_
= hold_is_erroneous_function
;
2313 // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
2314 // BaseTypeName = identifier .
2319 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
2322 const Token
* token
= this->advance_token();
2323 Location location
= token
->location();
2324 if (!token
->is_op(OPERATOR_MULT
))
2326 if (!token
->is_identifier())
2328 error_at(this->location(), "method has no receiver");
2329 this->gogo_
->mark_locals_used();
2330 while (!token
->is_eof() && !token
->is_op(OPERATOR_RPAREN
))
2331 token
= this->advance_token();
2332 if (!token
->is_eof())
2333 this->advance_token();
2336 name
= token
->identifier();
2337 bool is_exported
= token
->is_identifier_exported();
2338 token
= this->advance_token();
2339 if (!token
->is_op(OPERATOR_DOT
) && !token
->is_op(OPERATOR_RPAREN
))
2341 // An identifier followed by something other than a dot or a
2342 // right parenthesis must be a receiver name followed by a
2344 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
2348 // This must be a type name.
2349 this->unget_token(Token::make_identifier_token(name
, is_exported
,
2351 token
= this->peek_token();
2356 // Here the receiver name is in NAME (it is empty if the receiver is
2357 // unnamed) and TOKEN is the first token in the type.
2359 bool is_pointer
= false;
2360 if (token
->is_op(OPERATOR_MULT
))
2363 token
= this->advance_token();
2366 if (!token
->is_identifier())
2368 error_at(this->location(), "expected receiver name or type");
2369 this->gogo_
->mark_locals_used();
2370 int c
= token
->is_op(OPERATOR_LPAREN
) ? 1 : 0;
2371 while (!token
->is_eof())
2373 token
= this->advance_token();
2374 if (token
->is_op(OPERATOR_LPAREN
))
2376 else if (token
->is_op(OPERATOR_RPAREN
))
2383 if (!token
->is_eof())
2384 this->advance_token();
2388 Type
* type
= this->type_name(true);
2390 if (is_pointer
&& !type
->is_error_type())
2391 type
= Type::make_pointer_type(type
);
2393 if (this->peek_token()->is_op(OPERATOR_RPAREN
))
2394 this->advance_token();
2397 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2398 error_at(this->location(), "method has multiple receivers");
2400 error_at(this->location(), "expected %<)%>");
2401 this->gogo_
->mark_locals_used();
2402 while (!token
->is_eof() && !token
->is_op(OPERATOR_RPAREN
))
2403 token
= this->advance_token();
2404 if (!token
->is_eof())
2405 this->advance_token();
2409 return new Typed_identifier(name
, type
, location
);
2412 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2413 // Literal = BasicLit | CompositeLit | FunctionLit .
2414 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2416 // If MAY_BE_SINK is true, this operand may be "_".
2418 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2419 // if the entire expression is in parentheses.
2422 Parse::operand(bool may_be_sink
, bool* is_parenthesized
)
2424 const Token
* token
= this->peek_token();
2426 switch (token
->classification())
2428 case Token::TOKEN_IDENTIFIER
:
2430 Location location
= token
->location();
2431 std::string id
= token
->identifier();
2432 bool is_exported
= token
->is_identifier_exported();
2433 std::string packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2435 Named_object
* in_function
;
2436 Named_object
* named_object
= this->gogo_
->lookup(packed
, &in_function
);
2438 Package
* package
= NULL
;
2439 if (named_object
!= NULL
&& named_object
->is_package())
2441 if (!this->advance_token()->is_op(OPERATOR_DOT
)
2442 || !this->advance_token()->is_identifier())
2444 error_at(location
, "unexpected reference to package");
2445 return Expression::make_error(location
);
2447 package
= named_object
->package_value();
2448 package
->set_used();
2449 id
= this->peek_token()->identifier();
2450 is_exported
= this->peek_token()->is_identifier_exported();
2451 packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2452 named_object
= package
->lookup(packed
);
2453 location
= this->location();
2454 go_assert(in_function
== NULL
);
2457 this->advance_token();
2459 if (named_object
!= NULL
2460 && named_object
->is_type()
2461 && !named_object
->type_value()->is_visible())
2463 go_assert(package
!= NULL
);
2464 error_at(location
, "invalid reference to hidden type %<%s.%s%>",
2465 Gogo::message_name(package
->package_name()).c_str(),
2466 Gogo::message_name(id
).c_str());
2467 return Expression::make_error(location
);
2471 if (named_object
== NULL
)
2473 if (package
!= NULL
)
2475 std::string n1
= Gogo::message_name(package
->package_name());
2476 std::string n2
= Gogo::message_name(id
);
2479 ("invalid reference to unexported identifier "
2481 n1
.c_str(), n2
.c_str());
2484 "reference to undefined identifier %<%s.%s%>",
2485 n1
.c_str(), n2
.c_str());
2486 return Expression::make_error(location
);
2489 named_object
= this->gogo_
->add_unknown_name(packed
, location
);
2492 if (in_function
!= NULL
2493 && in_function
!= this->gogo_
->current_function()
2494 && (named_object
->is_variable()
2495 || named_object
->is_result_variable()))
2496 return this->enclosing_var_reference(in_function
, named_object
,
2499 switch (named_object
->classification())
2501 case Named_object::NAMED_OBJECT_CONST
:
2502 return Expression::make_const_reference(named_object
, location
);
2503 case Named_object::NAMED_OBJECT_TYPE
:
2504 return Expression::make_type(named_object
->type_value(), location
);
2505 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
2507 Type
* t
= Type::make_forward_declaration(named_object
);
2508 return Expression::make_type(t
, location
);
2510 case Named_object::NAMED_OBJECT_VAR
:
2511 case Named_object::NAMED_OBJECT_RESULT_VAR
:
2512 this->mark_var_used(named_object
);
2513 return Expression::make_var_reference(named_object
, location
);
2514 case Named_object::NAMED_OBJECT_SINK
:
2516 return Expression::make_sink(location
);
2519 error_at(location
, "cannot use _ as value");
2520 return Expression::make_error(location
);
2522 case Named_object::NAMED_OBJECT_FUNC
:
2523 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
2524 return Expression::make_func_reference(named_object
, NULL
,
2526 case Named_object::NAMED_OBJECT_UNKNOWN
:
2528 Unknown_expression
* ue
=
2529 Expression::make_unknown_reference(named_object
, location
);
2530 if (this->is_erroneous_function_
)
2531 ue
->set_no_error_message();
2534 case Named_object::NAMED_OBJECT_ERRONEOUS
:
2535 return Expression::make_error(location
);
2542 case Token::TOKEN_STRING
:
2543 ret
= Expression::make_string(token
->string_value(), token
->location());
2544 this->advance_token();
2547 case Token::TOKEN_CHARACTER
:
2548 ret
= Expression::make_character(token
->character_value(), NULL
,
2550 this->advance_token();
2553 case Token::TOKEN_INTEGER
:
2554 ret
= Expression::make_integer(token
->integer_value(), NULL
,
2556 this->advance_token();
2559 case Token::TOKEN_FLOAT
:
2560 ret
= Expression::make_float(token
->float_value(), NULL
,
2562 this->advance_token();
2565 case Token::TOKEN_IMAGINARY
:
2568 mpfr_init_set_ui(zero
, 0, GMP_RNDN
);
2569 ret
= Expression::make_complex(&zero
, token
->imaginary_value(),
2570 NULL
, token
->location());
2572 this->advance_token();
2576 case Token::TOKEN_KEYWORD
:
2577 switch (token
->keyword())
2580 return this->function_lit();
2582 case KEYWORD_INTERFACE
:
2584 case KEYWORD_STRUCT
:
2586 Location location
= token
->location();
2587 return Expression::make_type(this->type(), location
);
2594 case Token::TOKEN_OPERATOR
:
2595 if (token
->is_op(OPERATOR_LPAREN
))
2597 this->advance_token();
2598 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2600 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2601 error_at(this->location(), "missing %<)%>");
2603 this->advance_token();
2604 if (is_parenthesized
!= NULL
)
2605 *is_parenthesized
= true;
2608 else if (token
->is_op(OPERATOR_LSQUARE
))
2610 // Here we call array_type directly, as this is the only
2611 // case where an ellipsis is permitted for an array type.
2612 Location location
= token
->location();
2613 return Expression::make_type(this->array_type(true), location
);
2621 error_at(this->location(), "expected operand");
2622 return Expression::make_error(this->location());
2625 // Handle a reference to a variable in an enclosing function. We add
2626 // it to a list of such variables. We return a reference to a field
2627 // in a struct which will be passed on the static chain when calling
2628 // the current function.
2631 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2634 go_assert(var
->is_variable() || var
->is_result_variable());
2636 this->mark_var_used(var
);
2638 Named_object
* this_function
= this->gogo_
->current_function();
2639 Named_object
* closure
= this_function
->func_value()->closure_var();
2641 // The last argument to the Enclosing_var constructor is the index
2642 // of this variable in the closure. We add 1 to the current number
2643 // of enclosed variables, because the first field in the closure
2644 // points to the function code.
2645 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2646 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2647 this->enclosing_vars_
.insert(ev
);
2650 // This is a variable we have not seen before. Add a new field
2651 // to the closure type.
2652 this_function
->func_value()->add_closure_field(var
, location
);
2655 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2657 closure_ref
= Expression::make_unary(OPERATOR_MULT
, closure_ref
, location
);
2659 // The closure structure holds pointers to the variables, so we need
2660 // to introduce an indirection.
2661 Expression
* e
= Expression::make_field_reference(closure_ref
,
2664 e
= Expression::make_unary(OPERATOR_MULT
, e
, location
);
2668 // CompositeLit = LiteralType LiteralValue .
2669 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2670 // SliceType | MapType | TypeName .
2671 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2672 // ElementList = Element { "," Element } .
2673 // Element = [ Key ":" ] Value .
2674 // Key = FieldName | ElementIndex .
2675 // FieldName = identifier .
2676 // ElementIndex = Expression .
2677 // Value = Expression | LiteralValue .
2679 // We have already seen the type if there is one, and we are now
2680 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2681 // will be seen here as an array type whose length is "nil". The
2682 // DEPTH parameter is non-zero if this is an embedded composite
2683 // literal and the type was omitted. It gives the number of steps up
2684 // to the type which was provided. E.g., in [][]int{{1}} it will be
2685 // 1. In [][][]int{{{1}}} it will be 2.
2688 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2690 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2691 this->advance_token();
2693 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2695 this->advance_token();
2696 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2700 bool has_keys
= false;
2701 bool all_are_names
= true;
2702 Expression_list
* vals
= new Expression_list
;
2706 bool is_type_omitted
= false;
2707 bool is_name
= false;
2709 const Token
* token
= this->peek_token();
2711 if (token
->is_identifier())
2713 std::string identifier
= token
->identifier();
2714 bool is_exported
= token
->is_identifier_exported();
2715 Location location
= token
->location();
2717 if (this->advance_token()->is_op(OPERATOR_COLON
))
2719 // This may be a field name. We don't know for sure--it
2720 // could also be an expression for an array index. We
2721 // don't want to parse it as an expression because may
2722 // trigger various errors, e.g., if this identifier
2723 // happens to be the name of a package.
2724 Gogo
* gogo
= this->gogo_
;
2725 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2732 this->unget_token(Token::make_identifier_token(identifier
,
2735 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2739 else if (!token
->is_op(OPERATOR_LCURLY
))
2740 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2743 // This must be a composite literal inside another composite
2744 // literal, with the type omitted for the inner one.
2745 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2746 is_type_omitted
= true;
2749 token
= this->peek_token();
2750 if (!token
->is_op(OPERATOR_COLON
))
2753 vals
->push_back(NULL
);
2758 if (is_type_omitted
&& !val
->is_error_expression())
2760 error_at(this->location(), "unexpected %<:%>");
2761 val
= Expression::make_error(this->location());
2764 this->advance_token();
2766 if (!has_keys
&& !vals
->empty())
2768 Expression_list
* newvals
= new Expression_list
;
2769 for (Expression_list::const_iterator p
= vals
->begin();
2773 newvals
->push_back(NULL
);
2774 newvals
->push_back(*p
);
2781 if (val
->unknown_expression() != NULL
)
2782 val
->unknown_expression()->set_is_composite_literal_key();
2784 vals
->push_back(val
);
2786 if (!token
->is_op(OPERATOR_LCURLY
))
2787 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2790 // This must be a composite literal inside another
2791 // composite literal, with the type omitted for the
2793 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2796 token
= this->peek_token();
2799 vals
->push_back(val
);
2802 all_are_names
= false;
2804 if (token
->is_op(OPERATOR_COMMA
))
2806 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2808 this->advance_token();
2812 else if (token
->is_op(OPERATOR_RCURLY
))
2814 this->advance_token();
2819 if (token
->is_op(OPERATOR_SEMICOLON
))
2820 error_at(this->location(),
2821 "need trailing comma before newline in composite literal");
2823 error_at(this->location(), "expected %<,%> or %<}%>");
2825 this->gogo_
->mark_locals_used();
2827 while (!token
->is_eof()
2828 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2830 if (token
->is_op(OPERATOR_LCURLY
))
2832 else if (token
->is_op(OPERATOR_RCURLY
))
2834 token
= this->advance_token();
2836 if (token
->is_op(OPERATOR_RCURLY
))
2837 this->advance_token();
2839 return Expression::make_error(location
);
2843 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2844 all_are_names
, location
);
2847 // FunctionLit = "func" Signature Block .
2850 Parse::function_lit()
2852 Location location
= this->location();
2853 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2854 this->advance_token();
2856 Enclosing_vars hold_enclosing_vars
;
2857 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2859 Function_type
* type
= this->signature(NULL
, location
);
2860 bool fntype_is_error
= false;
2863 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2864 fntype_is_error
= true;
2867 // For a function literal, the next token must be a '{'. If we
2868 // don't see that, then we may have a type expression.
2869 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2870 return Expression::make_type(type
, location
);
2872 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2873 if (fntype_is_error
)
2874 this->is_erroneous_function_
= true;
2876 Bc_stack
* hold_break_stack
= this->break_stack_
;
2877 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2878 this->break_stack_
= NULL
;
2879 this->continue_stack_
= NULL
;
2881 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2883 Location end_loc
= this->block();
2885 this->gogo_
->finish_function(end_loc
);
2887 if (this->break_stack_
!= NULL
)
2888 delete this->break_stack_
;
2889 if (this->continue_stack_
!= NULL
)
2890 delete this->continue_stack_
;
2891 this->break_stack_
= hold_break_stack
;
2892 this->continue_stack_
= hold_continue_stack
;
2894 this->is_erroneous_function_
= hold_is_erroneous_function
;
2896 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2898 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
2901 return Expression::make_func_reference(no
, closure
, location
);
2904 // Create a closure for the nested function FUNCTION. This is based
2905 // on ENCLOSING_VARS, which is a list of all variables defined in
2906 // enclosing functions and referenced from FUNCTION. A closure is the
2907 // address of a struct which point to the real function code and
2908 // contains the addresses of all the referenced variables. This
2909 // returns NULL if no closure is required.
2912 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
2915 if (enclosing_vars
->empty())
2918 // Get the variables in order by their field index.
2920 size_t enclosing_var_count
= enclosing_vars
->size();
2921 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
2922 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
2923 p
!= enclosing_vars
->end();
2926 // Subtract 1 because index 0 is the function code.
2927 ev
[p
->index() - 1] = *p
;
2930 // Build an initializer for a composite literal of the closure's
2933 Named_object
* enclosing_function
= this->gogo_
->current_function();
2934 Expression_list
* initializer
= new Expression_list
;
2936 initializer
->push_back(Expression::make_func_code_reference(function
,
2939 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
2941 // Add 1 to i because the first field in the closure is a
2942 // pointer to the function code.
2943 go_assert(ev
[i
].index() == i
+ 1);
2944 Named_object
* var
= ev
[i
].var();
2946 if (ev
[i
].in_function() == enclosing_function
)
2947 ref
= Expression::make_var_reference(var
, location
);
2949 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
2951 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
2953 initializer
->push_back(refaddr
);
2956 Named_object
* closure_var
= function
->func_value()->closure_var();
2957 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
2958 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
2960 return Expression::make_heap_expression(cv
, location
);
2963 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2965 // If MAY_BE_SINK is true, this expression may be "_".
2967 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2970 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2971 // guard (var := expr.("type") using the literal keyword "type").
2973 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2974 // if the entire expression is in parentheses.
2977 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
2978 bool* is_type_switch
, bool* is_parenthesized
)
2980 Location start_loc
= this->location();
2981 bool operand_is_parenthesized
= false;
2982 bool whole_is_parenthesized
= false;
2984 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
2986 whole_is_parenthesized
= operand_is_parenthesized
;
2988 // An unknown name followed by a curly brace must be a composite
2989 // literal, and the unknown name must be a type.
2990 if (may_be_composite_lit
2991 && !operand_is_parenthesized
2992 && ret
->unknown_expression() != NULL
2993 && this->peek_token()->is_op(OPERATOR_LCURLY
))
2995 Named_object
* no
= ret
->unknown_expression()->named_object();
2996 Type
* type
= Type::make_forward_declaration(no
);
2997 ret
= Expression::make_type(type
, ret
->location());
3000 // We handle composite literals and type casts here, as it is the
3001 // easiest way to handle types which are in parentheses, as in
3003 if (ret
->is_type_expression())
3005 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
3007 whole_is_parenthesized
= false;
3008 if (!may_be_composite_lit
)
3010 Type
* t
= ret
->type();
3011 if (t
->named_type() != NULL
3012 || t
->forward_declaration_type() != NULL
)
3014 _("parentheses required around this composite literal "
3015 "to avoid parsing ambiguity"));
3017 else if (operand_is_parenthesized
)
3019 "cannot parenthesize type in composite literal");
3020 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
3022 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
3024 whole_is_parenthesized
= false;
3025 Location loc
= this->location();
3026 this->advance_token();
3027 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
3029 if (this->peek_token()->is_op(OPERATOR_COMMA
))
3030 this->advance_token();
3031 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
3033 error_at(this->location(),
3034 "invalid use of %<...%> in type conversion");
3035 this->advance_token();
3037 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3038 error_at(this->location(), "expected %<)%>");
3040 this->advance_token();
3041 if (expr
->is_error_expression())
3045 Type
* t
= ret
->type();
3046 if (t
->classification() == Type::TYPE_ARRAY
3047 && t
->array_type()->length() != NULL
3048 && t
->array_type()->length()->is_nil_expression())
3050 error_at(ret
->location(),
3051 "use of %<[...]%> outside of array literal");
3052 ret
= Expression::make_error(loc
);
3055 ret
= Expression::make_cast(t
, expr
, loc
);
3062 const Token
* token
= this->peek_token();
3063 if (token
->is_op(OPERATOR_LPAREN
))
3065 whole_is_parenthesized
= false;
3066 ret
= this->call(this->verify_not_sink(ret
));
3068 else if (token
->is_op(OPERATOR_DOT
))
3070 whole_is_parenthesized
= false;
3071 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3072 if (is_type_switch
!= NULL
&& *is_type_switch
)
3075 else if (token
->is_op(OPERATOR_LSQUARE
))
3077 whole_is_parenthesized
= false;
3078 ret
= this->index(this->verify_not_sink(ret
));
3084 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3085 *is_parenthesized
= true;
3090 // Selector = "." identifier .
3091 // TypeGuard = "." "(" QualifiedIdent ")" .
3093 // Note that Operand can expand to QualifiedIdent, which contains a
3094 // ".". That is handled directly in operand when it sees a package
3097 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3098 // guard (var := expr.("type") using the literal keyword "type").
3101 Parse::selector(Expression
* left
, bool* is_type_switch
)
3103 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3104 Location location
= this->location();
3106 const Token
* token
= this->advance_token();
3107 if (token
->is_identifier())
3109 // This could be a field in a struct, or a method in an
3110 // interface, or a method associated with a type. We can't know
3111 // which until we have seen all the types.
3113 this->gogo_
->pack_hidden_name(token
->identifier(),
3114 token
->is_identifier_exported());
3115 if (token
->identifier() == "_")
3117 error_at(this->location(), "invalid use of %<_%>");
3118 name
= Gogo::erroneous_name();
3120 this->advance_token();
3121 return Expression::make_selector(left
, name
, location
);
3123 else if (token
->is_op(OPERATOR_LPAREN
))
3125 this->advance_token();
3127 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3128 type
= this->type();
3131 if (is_type_switch
!= NULL
)
3132 *is_type_switch
= true;
3135 error_at(this->location(),
3136 "use of %<.(type)%> outside type switch");
3137 type
= Type::make_error_type();
3139 this->advance_token();
3141 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3142 error_at(this->location(), "missing %<)%>");
3144 this->advance_token();
3145 if (is_type_switch
!= NULL
&& *is_type_switch
)
3147 return Expression::make_type_guard(left
, type
, location
);
3151 error_at(this->location(), "expected identifier or %<(%>");
3156 // Index = "[" Expression "]" .
3157 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3160 Parse::index(Expression
* expr
)
3162 Location location
= this->location();
3163 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3164 this->advance_token();
3167 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3168 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3172 mpz_init_set_ui(zero
, 0);
3173 start
= Expression::make_integer(&zero
, NULL
, location
);
3177 Expression
* end
= NULL
;
3178 if (this->peek_token()->is_op(OPERATOR_COLON
))
3180 // We use nil to indicate a missing high expression.
3181 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3182 end
= Expression::make_nil(this->location());
3183 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3185 error_at(this->location(), "middle index required in 3-index slice");
3186 end
= Expression::make_error(this->location());
3189 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3192 Expression
* cap
= NULL
;
3193 if (this->peek_token()->is_op(OPERATOR_COLON
))
3195 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3197 error_at(this->location(), "final index required in 3-index slice");
3198 cap
= Expression::make_error(this->location());
3201 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3203 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3204 error_at(this->location(), "missing %<]%>");
3206 this->advance_token();
3207 return Expression::make_index(expr
, start
, end
, cap
, location
);
3210 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3211 // ArgumentList = ExpressionList [ "..." ] .
3214 Parse::call(Expression
* func
)
3216 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3217 Expression_list
* args
= NULL
;
3218 bool is_varargs
= false;
3219 const Token
* token
= this->advance_token();
3220 if (!token
->is_op(OPERATOR_RPAREN
))
3222 args
= this->expression_list(NULL
, false, true);
3223 token
= this->peek_token();
3224 if (token
->is_op(OPERATOR_ELLIPSIS
))
3227 token
= this->advance_token();
3230 if (token
->is_op(OPERATOR_COMMA
))
3231 token
= this->advance_token();
3232 if (!token
->is_op(OPERATOR_RPAREN
))
3233 error_at(this->location(), "missing %<)%>");
3235 this->advance_token();
3236 if (func
->is_error_expression())
3238 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3241 // Return an expression for a single unqualified identifier.
3244 Parse::id_to_expression(const std::string
& name
, Location location
)
3246 Named_object
* in_function
;
3247 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3248 if (named_object
== NULL
)
3249 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3251 if (in_function
!= NULL
3252 && in_function
!= this->gogo_
->current_function()
3253 && (named_object
->is_variable() || named_object
->is_result_variable()))
3254 return this->enclosing_var_reference(in_function
, named_object
,
3257 switch (named_object
->classification())
3259 case Named_object::NAMED_OBJECT_CONST
:
3260 return Expression::make_const_reference(named_object
, location
);
3261 case Named_object::NAMED_OBJECT_VAR
:
3262 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3263 this->mark_var_used(named_object
);
3264 return Expression::make_var_reference(named_object
, location
);
3265 case Named_object::NAMED_OBJECT_SINK
:
3266 return Expression::make_sink(location
);
3267 case Named_object::NAMED_OBJECT_FUNC
:
3268 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3269 return Expression::make_func_reference(named_object
, NULL
, location
);
3270 case Named_object::NAMED_OBJECT_UNKNOWN
:
3272 Unknown_expression
* ue
=
3273 Expression::make_unknown_reference(named_object
, location
);
3274 if (this->is_erroneous_function_
)
3275 ue
->set_no_error_message();
3278 case Named_object::NAMED_OBJECT_PACKAGE
:
3279 case Named_object::NAMED_OBJECT_TYPE
:
3280 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3282 // These cases can arise for a field name in a composite
3284 Unknown_expression
* ue
=
3285 Expression::make_unknown_reference(named_object
, location
);
3286 if (this->is_erroneous_function_
)
3287 ue
->set_no_error_message();
3290 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3291 return Expression::make_error(location
);
3293 error_at(this->location(), "unexpected type of identifier");
3294 return Expression::make_error(location
);
3298 // Expression = UnaryExpr { binary_op Expression } .
3300 // PRECEDENCE is the precedence of the current operator.
3302 // If MAY_BE_SINK is true, this expression may be "_".
3304 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3307 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3308 // guard (var := expr.("type") using the literal keyword "type").
3310 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3311 // if the entire expression is in parentheses.
3314 Parse::expression(Precedence precedence
, bool may_be_sink
,
3315 bool may_be_composite_lit
, bool* is_type_switch
,
3316 bool *is_parenthesized
)
3318 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3319 is_type_switch
, is_parenthesized
);
3323 if (is_type_switch
!= NULL
&& *is_type_switch
)
3326 const Token
* token
= this->peek_token();
3327 if (token
->classification() != Token::TOKEN_OPERATOR
)
3333 Precedence right_precedence
;
3334 switch (token
->op())
3337 right_precedence
= PRECEDENCE_OROR
;
3339 case OPERATOR_ANDAND
:
3340 right_precedence
= PRECEDENCE_ANDAND
;
3343 case OPERATOR_NOTEQ
:
3348 right_precedence
= PRECEDENCE_RELOP
;
3351 case OPERATOR_MINUS
:
3354 right_precedence
= PRECEDENCE_ADDOP
;
3359 case OPERATOR_LSHIFT
:
3360 case OPERATOR_RSHIFT
:
3362 case OPERATOR_BITCLEAR
:
3363 right_precedence
= PRECEDENCE_MULOP
;
3366 right_precedence
= PRECEDENCE_INVALID
;
3370 if (right_precedence
== PRECEDENCE_INVALID
)
3376 if (is_parenthesized
!= NULL
)
3377 *is_parenthesized
= false;
3379 Operator op
= token
->op();
3380 Location binop_location
= token
->location();
3382 if (precedence
>= right_precedence
)
3384 // We've already seen A * B, and we see + C. We want to
3385 // return so that A * B becomes a group.
3389 this->advance_token();
3391 left
= this->verify_not_sink(left
);
3392 Expression
* right
= this->expression(right_precedence
, false,
3393 may_be_composite_lit
,
3395 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3400 Parse::expression_may_start_here()
3402 const Token
* token
= this->peek_token();
3403 switch (token
->classification())
3405 case Token::TOKEN_INVALID
:
3406 case Token::TOKEN_EOF
:
3408 case Token::TOKEN_KEYWORD
:
3409 switch (token
->keyword())
3414 case KEYWORD_STRUCT
:
3415 case KEYWORD_INTERFACE
:
3420 case Token::TOKEN_IDENTIFIER
:
3422 case Token::TOKEN_STRING
:
3424 case Token::TOKEN_OPERATOR
:
3425 switch (token
->op())
3428 case OPERATOR_MINUS
:
3432 case OPERATOR_CHANOP
:
3434 case OPERATOR_LPAREN
:
3435 case OPERATOR_LSQUARE
:
3440 case Token::TOKEN_CHARACTER
:
3441 case Token::TOKEN_INTEGER
:
3442 case Token::TOKEN_FLOAT
:
3443 case Token::TOKEN_IMAGINARY
:
3450 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3452 // If MAY_BE_SINK is true, this expression may be "_".
3454 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3457 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3458 // guard (var := expr.("type") using the literal keyword "type").
3460 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3461 // if the entire expression is in parentheses.
3464 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3465 bool* is_type_switch
, bool* is_parenthesized
)
3467 const Token
* token
= this->peek_token();
3469 // There is a complex parse for <- chan. The choices are
3470 // Convert x to type <- chan int:
3472 // Receive from (x converted to type chan <- chan int):
3473 // (<- chan <- chan int (x))
3474 // Convert x to type <- chan (<- chan int).
3475 // (<- chan <- chan int)(x)
3476 if (token
->is_op(OPERATOR_CHANOP
))
3478 Location location
= token
->location();
3479 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3481 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3483 if (expr
->is_error_expression())
3485 else if (!expr
->is_type_expression())
3486 return Expression::make_receive(expr
, location
);
3489 if (expr
->type()->is_error_type())
3492 // We picked up "chan TYPE", but it is not a type
3494 Channel_type
* ct
= expr
->type()->channel_type();
3497 // This is probably impossible.
3498 error_at(location
, "expected channel type");
3499 return Expression::make_error(location
);
3501 else if (ct
->may_receive())
3504 Type
* t
= Type::make_channel_type(false, true,
3505 ct
->element_type());
3506 return Expression::make_type(t
, location
);
3510 // <- chan <- TYPE. Because we skipped the leading
3511 // <-, we parsed this as chan <- TYPE. With the
3512 // leading <-, we parse it as <- chan (<- TYPE).
3513 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3514 return Expression::make_type(t
, location
);
3519 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3520 token
= this->peek_token();
3523 if (token
->is_op(OPERATOR_PLUS
)
3524 || token
->is_op(OPERATOR_MINUS
)
3525 || token
->is_op(OPERATOR_NOT
)
3526 || token
->is_op(OPERATOR_XOR
)
3527 || token
->is_op(OPERATOR_CHANOP
)
3528 || token
->is_op(OPERATOR_MULT
)
3529 || token
->is_op(OPERATOR_AND
))
3531 Location location
= token
->location();
3532 Operator op
= token
->op();
3533 this->advance_token();
3535 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3537 if (expr
->is_error_expression())
3539 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3540 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3542 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3543 expr
= Expression::make_heap_expression(expr
, location
);
3544 else if (op
!= OPERATOR_CHANOP
)
3545 expr
= Expression::make_unary(op
, expr
, location
);
3547 expr
= Expression::make_receive(expr
, location
);
3551 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3552 is_type_switch
, is_parenthesized
);
3555 // This is called for the obscure case of
3556 // (<- chan <- chan int)(x)
3557 // In unary_expr we remove the leading <- and parse the remainder,
3559 // chan <- (chan int)
3560 // When we add the leading <- back in, we really want
3561 // <- chan (<- chan int)
3562 // This means that we need to reassociate.
3565 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3567 Channel_type
* ele
= ct
->element_type()->channel_type();
3570 error_at(location
, "parse error");
3571 return Type::make_error_type();
3574 if (ele
->may_send())
3575 sub
= Type::make_channel_type(false, true, ele
->element_type());
3577 sub
= this->reassociate_chan_direction(ele
, location
);
3578 return Type::make_channel_type(false, true, sub
);
3582 // Declaration | LabeledStmt | SimpleStmt |
3583 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3584 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3587 // LABEL is the label of this statement if it has one.
3590 Parse::statement(Label
* label
)
3592 const Token
* token
= this->peek_token();
3593 switch (token
->classification())
3595 case Token::TOKEN_KEYWORD
:
3597 switch (token
->keyword())
3602 this->declaration();
3606 case KEYWORD_STRUCT
:
3607 case KEYWORD_INTERFACE
:
3608 this->simple_stat(true, NULL
, NULL
, NULL
);
3612 this->go_or_defer_stat();
3614 case KEYWORD_RETURN
:
3615 this->return_stat();
3620 case KEYWORD_CONTINUE
:
3621 this->continue_stat();
3629 case KEYWORD_SWITCH
:
3630 this->switch_stat(label
);
3632 case KEYWORD_SELECT
:
3633 this->select_stat(label
);
3636 this->for_stat(label
);
3639 error_at(this->location(), "expected statement");
3640 this->advance_token();
3646 case Token::TOKEN_IDENTIFIER
:
3648 std::string identifier
= token
->identifier();
3649 bool is_exported
= token
->is_identifier_exported();
3650 Location location
= token
->location();
3651 if (this->advance_token()->is_op(OPERATOR_COLON
))
3653 this->advance_token();
3654 this->labeled_stmt(identifier
, location
);
3658 this->unget_token(Token::make_identifier_token(identifier
,
3661 this->simple_stat(true, NULL
, NULL
, NULL
);
3666 case Token::TOKEN_OPERATOR
:
3667 if (token
->is_op(OPERATOR_LCURLY
))
3669 Location location
= token
->location();
3670 this->gogo_
->start_block(location
);
3671 Location end_loc
= this->block();
3672 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3675 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3676 this->simple_stat(true, NULL
, NULL
, NULL
);
3679 case Token::TOKEN_STRING
:
3680 case Token::TOKEN_CHARACTER
:
3681 case Token::TOKEN_INTEGER
:
3682 case Token::TOKEN_FLOAT
:
3683 case Token::TOKEN_IMAGINARY
:
3684 this->simple_stat(true, NULL
, NULL
, NULL
);
3688 error_at(this->location(), "expected statement");
3689 this->advance_token();
3695 Parse::statement_may_start_here()
3697 const Token
* token
= this->peek_token();
3698 switch (token
->classification())
3700 case Token::TOKEN_KEYWORD
:
3702 switch (token
->keyword())
3709 case KEYWORD_STRUCT
:
3710 case KEYWORD_INTERFACE
:
3713 case KEYWORD_RETURN
:
3715 case KEYWORD_CONTINUE
:
3718 case KEYWORD_SWITCH
:
3719 case KEYWORD_SELECT
:
3729 case Token::TOKEN_IDENTIFIER
:
3732 case Token::TOKEN_OPERATOR
:
3733 if (token
->is_op(OPERATOR_LCURLY
)
3734 || token
->is_op(OPERATOR_SEMICOLON
))
3737 return this->expression_may_start_here();
3739 case Token::TOKEN_STRING
:
3740 case Token::TOKEN_CHARACTER
:
3741 case Token::TOKEN_INTEGER
:
3742 case Token::TOKEN_FLOAT
:
3743 case Token::TOKEN_IMAGINARY
:
3751 // LabeledStmt = Label ":" Statement .
3752 // Label = identifier .
3755 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3757 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3759 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3761 // This is a label at the end of a block. A program is
3762 // permitted to omit a semicolon here.
3766 if (!this->statement_may_start_here())
3768 // Mark the label as used to avoid a useless error about an
3771 label
->set_is_used();
3773 error_at(location
, "missing statement after label");
3774 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3779 this->statement(label
);
3782 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3783 // Assignment | ShortVarDecl .
3785 // EmptyStmt was handled in Parse::statement.
3787 // In order to make this work for if and switch statements, if
3788 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3789 // expression rather than adding an expression statement to the
3790 // current block. If we see something other than an ExpressionStat,
3791 // we add the statement, set *RETURN_EXP to true if we saw a send
3792 // statement, and return NULL. The handling of send statements is for
3793 // better error messages.
3795 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3798 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3799 // guard (var := expr.("type") using the literal keyword "type").
3802 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3803 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3805 const Token
* token
= this->peek_token();
3807 // An identifier follow by := is a SimpleVarDecl.
3808 if (token
->is_identifier())
3810 std::string identifier
= token
->identifier();
3811 bool is_exported
= token
->is_identifier_exported();
3812 Location location
= token
->location();
3814 token
= this->advance_token();
3815 if (token
->is_op(OPERATOR_COLONEQ
)
3816 || token
->is_op(OPERATOR_COMMA
))
3818 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3819 this->simple_var_decl_or_assignment(identifier
, location
,
3820 may_be_composite_lit
,
3822 (token
->is_op(OPERATOR_COLONEQ
)
3828 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3832 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3833 may_be_composite_lit
,
3834 (p_type_switch
== NULL
3836 : &p_type_switch
->found
),
3838 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3840 p_type_switch
->name
.clear();
3841 p_type_switch
->location
= exp
->location();
3842 p_type_switch
->expr
= this->verify_not_sink(exp
);
3845 token
= this->peek_token();
3846 if (token
->is_op(OPERATOR_CHANOP
))
3848 this->send_stmt(this->verify_not_sink(exp
));
3849 if (return_exp
!= NULL
)
3852 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3853 || token
->is_op(OPERATOR_MINUSMINUS
))
3854 this->inc_dec_stat(this->verify_not_sink(exp
));
3855 else if (token
->is_op(OPERATOR_COMMA
)
3856 || token
->is_op(OPERATOR_EQ
))
3857 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3858 else if (token
->is_op(OPERATOR_PLUSEQ
)
3859 || token
->is_op(OPERATOR_MINUSEQ
)
3860 || token
->is_op(OPERATOR_OREQ
)
3861 || token
->is_op(OPERATOR_XOREQ
)
3862 || token
->is_op(OPERATOR_MULTEQ
)
3863 || token
->is_op(OPERATOR_DIVEQ
)
3864 || token
->is_op(OPERATOR_MODEQ
)
3865 || token
->is_op(OPERATOR_LSHIFTEQ
)
3866 || token
->is_op(OPERATOR_RSHIFTEQ
)
3867 || token
->is_op(OPERATOR_ANDEQ
)
3868 || token
->is_op(OPERATOR_BITCLEAREQ
))
3869 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
3871 else if (return_exp
!= NULL
)
3872 return this->verify_not_sink(exp
);
3875 exp
= this->verify_not_sink(exp
);
3877 if (token
->is_op(OPERATOR_COLONEQ
))
3879 if (!exp
->is_error_expression())
3880 error_at(token
->location(), "non-name on left side of %<:=%>");
3881 this->gogo_
->mark_locals_used();
3882 while (!token
->is_op(OPERATOR_SEMICOLON
)
3883 && !token
->is_eof())
3884 token
= this->advance_token();
3888 this->expression_stat(exp
);
3895 Parse::simple_stat_may_start_here()
3897 return this->expression_may_start_here();
3900 // Parse { Statement ";" } which is used in a few places. The list of
3901 // statements may end with a right curly brace, in which case the
3902 // semicolon may be omitted.
3905 Parse::statement_list()
3907 while (this->statement_may_start_here())
3909 this->statement(NULL
);
3910 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
3911 this->advance_token();
3912 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3916 if (!this->peek_token()->is_eof() || !saw_errors())
3917 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3918 if (!this->skip_past_error(OPERATOR_RCURLY
))
3925 Parse::statement_list_may_start_here()
3927 return this->statement_may_start_here();
3930 // ExpressionStat = Expression .
3933 Parse::expression_stat(Expression
* exp
)
3935 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
3938 // SendStmt = Channel "<-" Expression .
3939 // Channel = Expression .
3942 Parse::send_stmt(Expression
* channel
)
3944 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
3945 Location loc
= this->location();
3946 this->advance_token();
3947 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
3949 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
3950 this->gogo_
->add_statement(s
);
3953 // IncDecStat = Expression ( "++" | "--" ) .
3956 Parse::inc_dec_stat(Expression
* exp
)
3958 const Token
* token
= this->peek_token();
3960 // Lvalue maps require special handling.
3961 if (exp
->index_expression() != NULL
)
3962 exp
->index_expression()->set_is_lvalue();
3964 if (token
->is_op(OPERATOR_PLUSPLUS
))
3965 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
3966 else if (token
->is_op(OPERATOR_MINUSMINUS
))
3967 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
3970 this->advance_token();
3973 // Assignment = ExpressionList assign_op ExpressionList .
3975 // EXP is an expression that we have already parsed.
3977 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3978 // side may be a composite literal.
3980 // If RANGE_CLAUSE is not NULL, then this will recognize a
3984 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
3985 Range_clause
* p_range_clause
)
3987 Expression_list
* vars
;
3988 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
3990 vars
= new Expression_list();
3991 vars
->push_back(expr
);
3995 this->advance_token();
3996 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
3999 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
4002 // An assignment statement. LHS is the list of expressions which
4003 // appear on the left hand side.
4005 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4006 // side may be a composite literal.
4008 // If RANGE_CLAUSE is not NULL, then this will recognize a
4012 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
4013 Range_clause
* p_range_clause
)
4015 const Token
* token
= this->peek_token();
4016 if (!token
->is_op(OPERATOR_EQ
)
4017 && !token
->is_op(OPERATOR_PLUSEQ
)
4018 && !token
->is_op(OPERATOR_MINUSEQ
)
4019 && !token
->is_op(OPERATOR_OREQ
)
4020 && !token
->is_op(OPERATOR_XOREQ
)
4021 && !token
->is_op(OPERATOR_MULTEQ
)
4022 && !token
->is_op(OPERATOR_DIVEQ
)
4023 && !token
->is_op(OPERATOR_MODEQ
)
4024 && !token
->is_op(OPERATOR_LSHIFTEQ
)
4025 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4026 && !token
->is_op(OPERATOR_ANDEQ
)
4027 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4029 error_at(this->location(), "expected assignment operator");
4032 Operator op
= token
->op();
4033 Location location
= token
->location();
4035 token
= this->advance_token();
4037 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4039 if (op
!= OPERATOR_EQ
)
4040 error_at(this->location(), "range clause requires %<=%>");
4041 this->range_clause_expr(lhs
, p_range_clause
);
4045 Expression_list
* vals
= this->expression_list(NULL
, false,
4046 may_be_composite_lit
);
4048 // We've parsed everything; check for errors.
4049 if (lhs
== NULL
|| vals
== NULL
)
4051 for (Expression_list::const_iterator pe
= lhs
->begin();
4055 if ((*pe
)->is_error_expression())
4057 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4058 error_at((*pe
)->location(), "cannot use _ as value");
4060 for (Expression_list::const_iterator pe
= vals
->begin();
4064 if ((*pe
)->is_error_expression())
4068 // Map expressions act differently when they are lvalues.
4069 for (Expression_list::iterator plv
= lhs
->begin();
4072 if ((*plv
)->index_expression() != NULL
)
4073 (*plv
)->index_expression()->set_is_lvalue();
4075 Call_expression
* call
;
4076 Index_expression
* map_index
;
4077 Receive_expression
* receive
;
4078 Type_guard_expression
* type_guard
;
4079 if (lhs
->size() == vals
->size())
4082 if (lhs
->size() > 1)
4084 if (op
!= OPERATOR_EQ
)
4085 error_at(location
, "multiple values only permitted with %<=%>");
4086 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4090 if (op
== OPERATOR_EQ
)
4091 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4094 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4095 vals
->front(), location
);
4099 this->gogo_
->add_statement(s
);
4101 else if (vals
->size() == 1
4102 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4104 if (op
!= OPERATOR_EQ
)
4105 error_at(location
, "multiple results only permitted with %<=%>");
4106 call
->set_expected_result_count(lhs
->size());
4108 vals
= new Expression_list
;
4109 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4110 vals
->push_back(Expression::make_call_result(call
, i
));
4111 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4112 this->gogo_
->add_statement(s
);
4114 else if (lhs
->size() == 2
4115 && vals
->size() == 1
4116 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4118 if (op
!= OPERATOR_EQ
)
4119 error_at(location
, "two values from map requires %<=%>");
4120 Expression
* val
= lhs
->front();
4121 Expression
* present
= lhs
->back();
4122 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4123 map_index
, location
);
4124 this->gogo_
->add_statement(s
);
4126 else if (lhs
->size() == 1
4127 && vals
->size() == 2
4128 && (map_index
= lhs
->front()->index_expression()) != NULL
)
4130 if (op
!= OPERATOR_EQ
)
4131 error_at(location
, "assigning tuple to map index requires %<=%>");
4132 Expression
* val
= vals
->front();
4133 Expression
* should_set
= vals
->back();
4134 Statement
* s
= Statement::make_map_assignment(map_index
, val
, should_set
,
4136 this->gogo_
->add_statement(s
);
4138 else if (lhs
->size() == 2
4139 && vals
->size() == 1
4140 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4142 if (op
!= OPERATOR_EQ
)
4143 error_at(location
, "two values from receive requires %<=%>");
4144 Expression
* val
= lhs
->front();
4145 Expression
* success
= lhs
->back();
4146 Expression
* channel
= receive
->channel();
4147 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4150 this->gogo_
->add_statement(s
);
4152 else if (lhs
->size() == 2
4153 && vals
->size() == 1
4154 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4156 if (op
!= OPERATOR_EQ
)
4157 error_at(location
, "two values from type guard requires %<=%>");
4158 Expression
* val
= lhs
->front();
4159 Expression
* ok
= lhs
->back();
4160 Expression
* expr
= type_guard
->expr();
4161 Type
* type
= type_guard
->type();
4162 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4165 this->gogo_
->add_statement(s
);
4169 error_at(location
, "number of variables does not match number of values");
4173 // GoStat = "go" Expression .
4174 // DeferStat = "defer" Expression .
4177 Parse::go_or_defer_stat()
4179 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4180 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4181 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4182 Location stat_location
= this->location();
4184 this->advance_token();
4185 Location expr_location
= this->location();
4187 bool is_parenthesized
= false;
4188 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4190 Call_expression
* call_expr
= expr
->call_expression();
4191 if (is_parenthesized
|| call_expr
== NULL
)
4193 error_at(expr_location
, "argument to go/defer must be function call");
4197 // Make it easier to simplify go/defer statements by putting every
4198 // statement in its own block.
4199 this->gogo_
->start_block(stat_location
);
4202 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4204 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4205 this->gogo_
->add_statement(stat
);
4206 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4210 // ReturnStat = "return" [ ExpressionList ] .
4213 Parse::return_stat()
4215 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4216 Location location
= this->location();
4217 this->advance_token();
4218 Expression_list
* vals
= NULL
;
4219 if (this->expression_may_start_here())
4220 vals
= this->expression_list(NULL
, false, true);
4221 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4224 && this->gogo_
->current_function()->func_value()->results_are_named())
4226 Named_object
* function
= this->gogo_
->current_function();
4227 Function::Results
* results
= function
->func_value()->result_variables();
4228 for (Function::Results::const_iterator p
= results
->begin();
4229 p
!= results
->end();
4232 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4234 go_assert(saw_errors());
4235 else if (!no
->is_result_variable())
4236 error_at(location
, "%qs is shadowed during return",
4237 (*p
)->message_name().c_str());
4242 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4243 // [ "else" ( IfStmt | Block ) ] .
4248 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4249 Location location
= this->location();
4250 this->advance_token();
4252 this->gogo_
->start_block(location
);
4254 bool saw_simple_stat
= false;
4255 Expression
* cond
= NULL
;
4256 bool saw_send_stmt
= false;
4257 if (this->simple_stat_may_start_here())
4259 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4260 saw_simple_stat
= true;
4262 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4264 // The SimpleStat is an expression statement.
4265 this->expression_stat(cond
);
4270 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4271 this->advance_token();
4272 else if (saw_simple_stat
)
4275 error_at(this->location(),
4276 ("send statement used as value; "
4277 "use select for non-blocking send"));
4279 error_at(this->location(),
4280 "expected %<;%> after statement in if expression");
4281 if (!this->expression_may_start_here())
4282 cond
= Expression::make_error(this->location());
4284 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4286 error_at(this->location(),
4287 "missing condition in if statement");
4288 cond
= Expression::make_error(this->location());
4291 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4294 // Check for the easy error of a newline before starting the block.
4295 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4297 Location semi_loc
= this->location();
4298 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4299 error_at(semi_loc
, "missing %<{%> after if clause");
4300 // Otherwise we will get an error when we call this->block
4304 this->gogo_
->start_block(this->location());
4305 Location end_loc
= this->block();
4306 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4308 // Check for the easy error of a newline before "else".
4309 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4311 Location semi_loc
= this->location();
4312 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4313 error_at(this->location(),
4314 "unexpected semicolon or newline before %<else%>");
4316 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4320 Block
* else_block
= NULL
;
4321 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4323 this->gogo_
->start_block(this->location());
4324 const Token
* token
= this->advance_token();
4325 if (token
->is_keyword(KEYWORD_IF
))
4327 else if (token
->is_op(OPERATOR_LCURLY
))
4331 error_at(this->location(), "expected %<if%> or %<{%>");
4332 this->statement(NULL
);
4334 else_block
= this->gogo_
->finish_block(this->location());
4337 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4341 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4345 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4346 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4347 // "{" { ExprCaseClause } "}" .
4348 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4349 // "{" { TypeCaseClause } "}" .
4350 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4353 Parse::switch_stat(Label
* label
)
4355 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4356 Location location
= this->location();
4357 this->advance_token();
4359 this->gogo_
->start_block(location
);
4361 bool saw_simple_stat
= false;
4362 Expression
* switch_val
= NULL
;
4364 Type_switch type_switch
;
4365 bool have_type_switch_block
= false;
4366 if (this->simple_stat_may_start_here())
4368 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4370 saw_simple_stat
= true;
4372 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4374 // The SimpleStat is an expression statement.
4375 this->expression_stat(switch_val
);
4378 if (switch_val
== NULL
&& !type_switch
.found
)
4380 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4381 this->advance_token();
4382 else if (saw_simple_stat
)
4385 error_at(this->location(),
4386 ("send statement used as value; "
4387 "use select for non-blocking send"));
4389 error_at(this->location(),
4390 "expected %<;%> after statement in switch expression");
4392 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4394 if (this->peek_token()->is_identifier())
4396 const Token
* token
= this->peek_token();
4397 std::string identifier
= token
->identifier();
4398 bool is_exported
= token
->is_identifier_exported();
4399 Location id_loc
= token
->location();
4401 token
= this->advance_token();
4402 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4403 this->unget_token(Token::make_identifier_token(identifier
,
4408 // This must be a TypeSwitchGuard. It is in a
4409 // different block from any initial SimpleStat.
4410 if (saw_simple_stat
)
4412 this->gogo_
->start_block(id_loc
);
4413 have_type_switch_block
= true;
4416 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4418 if (!type_switch
.found
)
4420 if (switch_val
== NULL
4421 || !switch_val
->is_error_expression())
4423 error_at(id_loc
, "expected type switch assignment");
4424 switch_val
= Expression::make_error(id_loc
);
4429 if (switch_val
== NULL
&& !type_switch
.found
)
4431 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4432 &type_switch
.found
, NULL
);
4433 if (type_switch
.found
)
4435 type_switch
.name
.clear();
4436 type_switch
.expr
= switch_val
;
4437 type_switch
.location
= switch_val
->location();
4443 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4445 Location token_loc
= this->location();
4446 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4447 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4448 error_at(token_loc
, "missing %<{%> after switch clause");
4449 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4451 error_at(token_loc
, "invalid variable name");
4452 this->advance_token();
4453 this->expression(PRECEDENCE_NORMAL
, false, false,
4454 &type_switch
.found
, NULL
);
4455 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4456 this->advance_token();
4457 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4459 if (have_type_switch_block
)
4460 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4462 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4466 if (type_switch
.found
)
4467 type_switch
.expr
= Expression::make_error(location
);
4471 error_at(this->location(), "expected %<{%>");
4472 if (have_type_switch_block
)
4473 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4475 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4480 this->advance_token();
4482 Statement
* statement
;
4483 if (type_switch
.found
)
4484 statement
= this->type_switch_body(label
, type_switch
, location
);
4486 statement
= this->expr_switch_body(label
, switch_val
, location
);
4488 if (statement
!= NULL
)
4489 this->gogo_
->add_statement(statement
);
4491 if (have_type_switch_block
)
4492 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4495 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4499 // The body of an expression switch.
4500 // "{" { ExprCaseClause } "}"
4503 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4506 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4509 this->push_break_statement(statement
, label
);
4511 Case_clauses
* case_clauses
= new Case_clauses();
4512 bool saw_default
= false;
4513 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4515 if (this->peek_token()->is_eof())
4518 error_at(this->location(), "missing %<}%>");
4521 this->expr_case_clause(case_clauses
, &saw_default
);
4523 this->advance_token();
4525 statement
->add_clauses(case_clauses
);
4527 this->pop_break_statement();
4532 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4533 // FallthroughStat = "fallthrough" .
4536 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4538 Location location
= this->location();
4540 bool is_default
= false;
4541 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4543 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4546 error_at(this->location(), "expected %<:%>");
4550 this->advance_token();
4552 Block
* statements
= NULL
;
4553 if (this->statement_list_may_start_here())
4555 this->gogo_
->start_block(this->location());
4556 this->statement_list();
4557 statements
= this->gogo_
->finish_block(this->location());
4560 bool is_fallthrough
= false;
4561 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4563 Location fallthrough_loc
= this->location();
4564 is_fallthrough
= true;
4565 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4566 this->advance_token();
4567 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4568 error_at(fallthrough_loc
, _("cannot fallthrough final case in switch"));
4575 error_at(location
, "multiple defaults in switch");
4578 *saw_default
= true;
4581 if (is_default
|| vals
!= NULL
)
4582 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4585 // ExprSwitchCase = "case" ExpressionList | "default" .
4588 Parse::expr_switch_case(bool* is_default
)
4590 const Token
* token
= this->peek_token();
4591 if (token
->is_keyword(KEYWORD_CASE
))
4593 this->advance_token();
4594 return this->expression_list(NULL
, false, true);
4596 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4598 this->advance_token();
4605 error_at(this->location(), "expected %<case%> or %<default%>");
4606 if (!token
->is_op(OPERATOR_RCURLY
))
4607 this->advance_token();
4612 // The body of a type switch.
4613 // "{" { TypeCaseClause } "}" .
4616 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4619 Named_object
* switch_no
= NULL
;
4620 if (!type_switch
.name
.empty())
4622 if (Gogo::is_sink_name(type_switch
.name
))
4623 error_at(type_switch
.location
,
4624 "no new variables on left side of %<:=%>");
4627 Variable
* switch_var
= new Variable(NULL
, type_switch
.expr
, false,
4629 type_switch
.location
);
4630 switch_no
= this->gogo_
->add_variable(type_switch
.name
, switch_var
);
4634 Type_switch_statement
* statement
=
4635 Statement::make_type_switch_statement(switch_no
,
4641 this->push_break_statement(statement
, label
);
4643 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4644 bool saw_default
= false;
4645 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4647 if (this->peek_token()->is_eof())
4649 error_at(this->location(), "missing %<}%>");
4652 this->type_case_clause(switch_no
, case_clauses
, &saw_default
);
4654 this->advance_token();
4656 statement
->add_clauses(case_clauses
);
4658 this->pop_break_statement();
4663 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4666 Parse::type_case_clause(Named_object
* switch_no
, Type_case_clauses
* clauses
,
4669 Location location
= this->location();
4671 std::vector
<Type
*> types
;
4672 bool is_default
= false;
4673 this->type_switch_case(&types
, &is_default
);
4675 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4676 error_at(this->location(), "expected %<:%>");
4678 this->advance_token();
4680 Block
* statements
= NULL
;
4681 if (this->statement_list_may_start_here())
4683 this->gogo_
->start_block(this->location());
4684 if (switch_no
!= NULL
&& types
.size() == 1)
4686 Type
* type
= types
.front();
4687 Expression
* init
= Expression::make_var_reference(switch_no
,
4689 init
= Expression::make_type_guard(init
, type
, location
);
4690 Variable
* v
= new Variable(type
, init
, false, false, false,
4692 v
->set_is_type_switch_var();
4693 Named_object
* no
= this->gogo_
->add_variable(switch_no
->name(), v
);
4695 // We don't want to issue an error if the compiler
4696 // introduced special variable is not used. Instead we want
4697 // to issue an error if the variable defined by the switch
4698 // is not used. That is handled via type_switch_vars_ and
4699 // Parse::mark_var_used.
4701 this->type_switch_vars_
[no
] = switch_no
;
4703 this->statement_list();
4704 statements
= this->gogo_
->finish_block(this->location());
4707 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4709 error_at(this->location(),
4710 "fallthrough is not permitted in a type switch");
4711 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4712 this->advance_token();
4717 go_assert(types
.empty());
4720 error_at(location
, "multiple defaults in type switch");
4723 *saw_default
= true;
4724 clauses
->add(NULL
, false, true, statements
, location
);
4726 else if (!types
.empty())
4728 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4729 p
+ 1 != types
.end();
4731 clauses
->add(*p
, true, false, NULL
, location
);
4732 clauses
->add(types
.back(), false, false, statements
, location
);
4735 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4738 // TypeSwitchCase = "case" type | "default"
4740 // We accept a comma separated list of types.
4743 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4745 const Token
* token
= this->peek_token();
4746 if (token
->is_keyword(KEYWORD_CASE
))
4748 this->advance_token();
4751 Type
* t
= this->type();
4753 if (!t
->is_error_type())
4754 types
->push_back(t
);
4757 this->gogo_
->mark_locals_used();
4758 token
= this->peek_token();
4759 while (!token
->is_op(OPERATOR_COLON
)
4760 && !token
->is_op(OPERATOR_COMMA
)
4761 && !token
->is_op(OPERATOR_RCURLY
)
4762 && !token
->is_eof())
4763 token
= this->advance_token();
4766 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4768 this->advance_token();
4771 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4773 this->advance_token();
4778 error_at(this->location(), "expected %<case%> or %<default%>");
4779 if (!token
->is_op(OPERATOR_RCURLY
))
4780 this->advance_token();
4784 // SelectStat = "select" "{" { CommClause } "}" .
4787 Parse::select_stat(Label
* label
)
4789 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4790 Location location
= this->location();
4791 const Token
* token
= this->advance_token();
4793 if (!token
->is_op(OPERATOR_LCURLY
))
4795 Location token_loc
= token
->location();
4796 if (token
->is_op(OPERATOR_SEMICOLON
)
4797 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4798 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4801 error_at(this->location(), "expected %<{%>");
4805 this->advance_token();
4807 Select_statement
* statement
= Statement::make_select_statement(location
);
4809 this->push_break_statement(statement
, label
);
4811 Select_clauses
* select_clauses
= new Select_clauses();
4812 bool saw_default
= false;
4813 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4815 if (this->peek_token()->is_eof())
4817 error_at(this->location(), "expected %<}%>");
4820 this->comm_clause(select_clauses
, &saw_default
);
4823 this->advance_token();
4825 statement
->add_clauses(select_clauses
);
4827 this->pop_break_statement();
4829 this->gogo_
->add_statement(statement
);
4832 // CommClause = CommCase ":" { Statement ";" } .
4835 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4837 Location location
= this->location();
4838 bool is_send
= false;
4839 Expression
* channel
= NULL
;
4840 Expression
* val
= NULL
;
4841 Expression
* closed
= NULL
;
4842 std::string varname
;
4843 std::string closedname
;
4844 bool is_default
= false;
4845 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4846 &varname
, &closedname
, &is_default
);
4850 && closedname
.empty()
4852 && val
->index_expression() != NULL
)
4853 val
->index_expression()->set_is_lvalue();
4855 if (this->peek_token()->is_op(OPERATOR_COLON
))
4856 this->advance_token();
4858 error_at(this->location(), "expected colon");
4860 this->gogo_
->start_block(this->location());
4862 Named_object
* var
= NULL
;
4863 if (!varname
.empty())
4865 // FIXME: LOCATION is slightly wrong here.
4866 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
4868 v
->set_type_from_chan_element();
4869 var
= this->gogo_
->add_variable(varname
, v
);
4872 Named_object
* closedvar
= NULL
;
4873 if (!closedname
.empty())
4875 // FIXME: LOCATION is slightly wrong here.
4876 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
4877 false, false, false, location
);
4878 closedvar
= this->gogo_
->add_variable(closedname
, v
);
4881 this->statement_list();
4883 Block
* statements
= this->gogo_
->finish_block(this->location());
4889 error_at(location
, "multiple defaults in select");
4892 *saw_default
= true;
4896 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
4897 statements
, location
);
4898 else if (statements
!= NULL
)
4900 // Add the statements to make sure that any names they define
4902 this->gogo_
->add_block(statements
, location
);
4906 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4909 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
4910 Expression
** closed
, std::string
* varname
,
4911 std::string
* closedname
, bool* is_default
)
4913 const Token
* token
= this->peek_token();
4914 if (token
->is_keyword(KEYWORD_DEFAULT
))
4916 this->advance_token();
4919 else if (token
->is_keyword(KEYWORD_CASE
))
4921 this->advance_token();
4922 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
4928 error_at(this->location(), "expected %<case%> or %<default%>");
4929 if (!token
->is_op(OPERATOR_RCURLY
))
4930 this->advance_token();
4937 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4938 // RecvExpr = Expression .
4941 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
4942 Expression
** closed
, std::string
* varname
,
4943 std::string
* closedname
)
4945 const Token
* token
= this->peek_token();
4946 bool saw_comma
= false;
4947 bool closed_is_id
= false;
4948 if (token
->is_identifier())
4950 Gogo
* gogo
= this->gogo_
;
4951 std::string recv_var
= token
->identifier();
4952 bool is_rv_exported
= token
->is_identifier_exported();
4953 Location recv_var_loc
= token
->location();
4954 token
= this->advance_token();
4955 if (token
->is_op(OPERATOR_COLONEQ
))
4958 this->advance_token();
4959 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
4961 Receive_expression
* re
= e
->receive_expression();
4964 if (!e
->is_error_expression())
4965 error_at(this->location(), "expected receive expression");
4968 if (recv_var
== "_")
4970 error_at(recv_var_loc
,
4971 "no new variables on left side of %<:=%>");
4972 recv_var
= Gogo::erroneous_name();
4975 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
4976 *channel
= re
->channel();
4979 else if (token
->is_op(OPERATOR_COMMA
))
4981 token
= this->advance_token();
4982 if (token
->is_identifier())
4984 std::string recv_closed
= token
->identifier();
4985 bool is_rc_exported
= token
->is_identifier_exported();
4986 Location recv_closed_loc
= token
->location();
4987 closed_is_id
= true;
4989 token
= this->advance_token();
4990 if (token
->is_op(OPERATOR_COLONEQ
))
4992 // case rv, rc := <-c:
4993 this->advance_token();
4994 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
4996 Receive_expression
* re
= e
->receive_expression();
4999 if (!e
->is_error_expression())
5000 error_at(this->location(),
5001 "expected receive expression");
5004 if (recv_var
== "_" && recv_closed
== "_")
5006 error_at(recv_var_loc
,
5007 "no new variables on left side of %<:=%>");
5008 recv_var
= Gogo::erroneous_name();
5011 if (recv_var
!= "_")
5012 *varname
= gogo
->pack_hidden_name(recv_var
,
5014 if (recv_closed
!= "_")
5015 *closedname
= gogo
->pack_hidden_name(recv_closed
,
5017 *channel
= re
->channel();
5021 this->unget_token(Token::make_identifier_token(recv_closed
,
5026 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5032 this->unget_token(Token::make_identifier_token(recv_var
,
5037 // If SAW_COMMA is false, then we are looking at the start of the
5038 // send or receive expression. If SAW_COMMA is true, then *VAL is
5039 // set and we just read a comma.
5042 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5043 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5048 this->advance_token();
5049 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5051 // The next token should be ':'. If it is '<-', then we have
5053 // which is to say, send on a channel received from a channel.
5054 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5057 e
= Expression::make_receive(*channel
, (*channel
)->location());
5060 if (this->peek_token()->is_op(OPERATOR_EQ
))
5062 if (!this->advance_token()->is_op(OPERATOR_CHANOP
))
5064 error_at(this->location(), "missing %<<-%>");
5068 this->advance_token();
5069 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5073 // *VAL is already set.
5074 if (!e
->is_sink_expression())
5080 if (!e
->is_sink_expression())
5089 error_at(this->location(), "expected %<=%> or %<:=%>");
5091 error_at(this->location(), "expected %<=%>");
5095 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5099 *channel
= this->verify_not_sink(e
);
5100 this->advance_token();
5101 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5105 error_at(this->location(), "expected %<<-%> or %<=%>");
5109 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5110 // Condition = Expression .
5113 Parse::for_stat(Label
* label
)
5115 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5116 Location location
= this->location();
5117 const Token
* token
= this->advance_token();
5119 // Open a block to hold any variables defined in the init statement
5120 // of the for statement.
5121 this->gogo_
->start_block(location
);
5124 Expression
* cond
= NULL
;
5126 Range_clause range_clause
;
5128 if (!token
->is_op(OPERATOR_LCURLY
))
5130 if (token
->is_keyword(KEYWORD_VAR
))
5132 error_at(this->location(),
5133 "var declaration not allowed in for initializer");
5137 if (token
->is_op(OPERATOR_SEMICOLON
))
5138 this->for_clause(&cond
, &post
);
5141 // We might be looking at a Condition, an InitStat, or a
5144 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5145 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5147 if (cond
== NULL
&& !range_clause
.found
)
5150 error_at(this->location(),
5151 ("send statement used as value; "
5152 "use select for non-blocking send"));
5154 error_at(this->location(), "parse error in for statement");
5159 if (range_clause
.found
)
5160 error_at(this->location(), "parse error after range clause");
5164 // COND is actually an expression statement for
5165 // InitStat at the start of a ForClause.
5166 this->expression_stat(cond
);
5170 this->for_clause(&cond
, &post
);
5175 // Check for the easy error of a newline before starting the block.
5176 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5178 Location semi_loc
= this->location();
5179 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5180 error_at(semi_loc
, "missing %<{%> after for clause");
5181 // Otherwise we will get an error when we call this->block
5185 // Build the For_statement and note that it is the current target
5186 // for break and continue statements.
5188 For_statement
* sfor
;
5189 For_range_statement
* srange
;
5191 if (!range_clause
.found
)
5193 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5199 srange
= Statement::make_for_range_statement(range_clause
.index
,
5207 this->push_break_statement(s
, label
);
5208 this->push_continue_statement(s
, label
);
5210 // Gather the block of statements in the loop and add them to the
5213 this->gogo_
->start_block(this->location());
5214 Location end_loc
= this->block();
5215 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5218 sfor
->add_statements(statements
);
5220 srange
->add_statements(statements
);
5222 // This is no longer the break/continue target.
5223 this->pop_break_statement();
5224 this->pop_continue_statement();
5226 // Add the For_statement to the list of statements, and close out
5227 // the block we started to hold any variables defined in the for
5230 this->gogo_
->add_statement(s
);
5232 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5236 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5237 // InitStat = SimpleStat .
5238 // PostStat = SimpleStat .
5240 // We have already read InitStat at this point.
5243 Parse::for_clause(Expression
** cond
, Block
** post
)
5245 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5246 this->advance_token();
5247 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5249 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5251 error_at(this->location(), "missing %<{%> after for clause");
5257 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5258 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5259 error_at(this->location(), "expected semicolon");
5261 this->advance_token();
5263 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5267 this->gogo_
->start_block(this->location());
5268 this->simple_stat(false, NULL
, NULL
, NULL
);
5269 *post
= this->gogo_
->finish_block(this->location());
5273 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
5275 // This is the := version. It is called with a list of identifiers.
5278 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5279 Range_clause
* p_range_clause
)
5281 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5282 Location location
= this->location();
5284 p_range_clause
->found
= true;
5286 go_assert(til
->size() >= 1);
5287 if (til
->size() > 2)
5288 error_at(this->location(), "too many variables for range clause");
5290 this->advance_token();
5291 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5293 p_range_clause
->range
= expr
;
5295 bool any_new
= false;
5297 const Typed_identifier
* pti
= &til
->front();
5298 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5300 if (any_new
&& no
->is_variable())
5301 no
->var_value()->set_type_from_range_index();
5302 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5304 if (til
->size() == 1)
5305 p_range_clause
->value
= NULL
;
5309 bool is_new
= false;
5310 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5311 if (is_new
&& no
->is_variable())
5312 no
->var_value()->set_type_from_range_value();
5315 if (!Gogo::is_sink_name(pti
->name()))
5316 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5320 error_at(location
, "variables redeclared but no variable is new");
5323 // The = version of RangeClause. This is called with a list of
5327 Parse::range_clause_expr(const Expression_list
* vals
,
5328 Range_clause
* p_range_clause
)
5330 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5332 p_range_clause
->found
= true;
5334 go_assert(vals
->size() >= 1);
5335 if (vals
->size() > 2)
5336 error_at(this->location(), "too many variables for range clause");
5338 this->advance_token();
5339 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5342 p_range_clause
->index
= vals
->front();
5343 if (vals
->size() == 1)
5344 p_range_clause
->value
= NULL
;
5346 p_range_clause
->value
= vals
->back();
5349 // Push a statement on the break stack.
5352 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5354 if (this->break_stack_
== NULL
)
5355 this->break_stack_
= new Bc_stack();
5356 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5359 // Push a statement on the continue stack.
5362 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5364 if (this->continue_stack_
== NULL
)
5365 this->continue_stack_
= new Bc_stack();
5366 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5369 // Pop the break stack.
5372 Parse::pop_break_statement()
5374 this->break_stack_
->pop_back();
5377 // Pop the continue stack.
5380 Parse::pop_continue_statement()
5382 this->continue_stack_
->pop_back();
5385 // Find a break or continue statement given a label name.
5388 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5390 if (bc_stack
== NULL
)
5392 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5393 p
!= bc_stack
->rend();
5396 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5398 p
->second
->set_is_used();
5405 // BreakStat = "break" [ identifier ] .
5410 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5411 Location location
= this->location();
5413 const Token
* token
= this->advance_token();
5414 Statement
* enclosing
;
5415 if (!token
->is_identifier())
5417 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5419 error_at(this->location(),
5420 "break statement not within for or switch or select");
5423 enclosing
= this->break_stack_
->back().first
;
5427 enclosing
= this->find_bc_statement(this->break_stack_
,
5428 token
->identifier());
5429 if (enclosing
== NULL
)
5431 // If there is a label with this name, mark it as used to
5432 // avoid a useless error about an unused label.
5433 this->gogo_
->add_label_reference(token
->identifier(),
5434 Linemap::unknown_location(), false);
5436 error_at(token
->location(), "invalid break label %qs",
5437 Gogo::message_name(token
->identifier()).c_str());
5438 this->advance_token();
5441 this->advance_token();
5444 Unnamed_label
* label
;
5445 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5446 label
= enclosing
->for_statement()->break_label();
5447 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5448 label
= enclosing
->for_range_statement()->break_label();
5449 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5450 label
= enclosing
->switch_statement()->break_label();
5451 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5452 label
= enclosing
->type_switch_statement()->break_label();
5453 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5454 label
= enclosing
->select_statement()->break_label();
5458 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5462 // ContinueStat = "continue" [ identifier ] .
5465 Parse::continue_stat()
5467 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5468 Location location
= this->location();
5470 const Token
* token
= this->advance_token();
5471 Statement
* enclosing
;
5472 if (!token
->is_identifier())
5474 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5476 error_at(this->location(), "continue statement not within for");
5479 enclosing
= this->continue_stack_
->back().first
;
5483 enclosing
= this->find_bc_statement(this->continue_stack_
,
5484 token
->identifier());
5485 if (enclosing
== NULL
)
5487 // If there is a label with this name, mark it as used to
5488 // avoid a useless error about an unused label.
5489 this->gogo_
->add_label_reference(token
->identifier(),
5490 Linemap::unknown_location(), false);
5492 error_at(token
->location(), "invalid continue label %qs",
5493 Gogo::message_name(token
->identifier()).c_str());
5494 this->advance_token();
5497 this->advance_token();
5500 Unnamed_label
* label
;
5501 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5502 label
= enclosing
->for_statement()->continue_label();
5503 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5504 label
= enclosing
->for_range_statement()->continue_label();
5508 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5512 // GotoStat = "goto" identifier .
5517 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5518 Location location
= this->location();
5519 const Token
* token
= this->advance_token();
5520 if (!token
->is_identifier())
5521 error_at(this->location(), "expected label for goto");
5524 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5526 Statement
* s
= Statement::make_goto_statement(label
, location
);
5527 this->gogo_
->add_statement(s
);
5528 this->advance_token();
5532 // PackageClause = "package" PackageName .
5535 Parse::package_clause()
5537 const Token
* token
= this->peek_token();
5538 Location location
= token
->location();
5540 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5542 error_at(this->location(), "program must start with package clause");
5547 token
= this->advance_token();
5548 if (token
->is_identifier())
5550 name
= token
->identifier();
5553 error_at(this->location(), "invalid package name _");
5554 name
= Gogo::erroneous_name();
5556 this->advance_token();
5560 error_at(this->location(), "package name must be an identifier");
5564 this->gogo_
->set_package_name(name
, location
);
5567 // ImportDecl = "import" Decl<ImportSpec> .
5570 Parse::import_decl()
5572 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5573 this->advance_token();
5574 this->decl(&Parse::import_spec
, NULL
);
5577 // ImportSpec = [ "." | PackageName ] PackageFileName .
5580 Parse::import_spec(void*)
5582 const Token
* token
= this->peek_token();
5583 Location location
= token
->location();
5585 std::string local_name
;
5586 bool is_local_name_exported
= false;
5587 if (token
->is_op(OPERATOR_DOT
))
5590 token
= this->advance_token();
5592 else if (token
->is_identifier())
5594 local_name
= token
->identifier();
5595 is_local_name_exported
= token
->is_identifier_exported();
5596 token
= this->advance_token();
5599 if (!token
->is_string())
5601 error_at(this->location(), "import statement not a string");
5602 this->advance_token();
5606 this->gogo_
->import_package(token
->string_value(), local_name
,
5607 is_local_name_exported
, location
);
5609 this->advance_token();
5612 // SourceFile = PackageClause ";" { ImportDecl ";" }
5613 // { TopLevelDecl ";" } .
5618 this->package_clause();
5620 const Token
* token
= this->peek_token();
5621 if (token
->is_op(OPERATOR_SEMICOLON
))
5622 token
= this->advance_token();
5624 error_at(this->location(),
5625 "expected %<;%> or newline after package clause");
5627 while (token
->is_keyword(KEYWORD_IMPORT
))
5629 this->import_decl();
5630 token
= this->peek_token();
5631 if (token
->is_op(OPERATOR_SEMICOLON
))
5632 token
= this->advance_token();
5634 error_at(this->location(),
5635 "expected %<;%> or newline after import declaration");
5638 while (!token
->is_eof())
5640 if (this->declaration_may_start_here())
5641 this->declaration();
5644 error_at(this->location(), "expected declaration");
5645 this->gogo_
->mark_locals_used();
5647 this->advance_token();
5648 while (!this->peek_token()->is_eof()
5649 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5650 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5651 if (!this->peek_token()->is_eof()
5652 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5653 this->advance_token();
5655 token
= this->peek_token();
5656 if (token
->is_op(OPERATOR_SEMICOLON
))
5657 token
= this->advance_token();
5658 else if (!token
->is_eof() || !saw_errors())
5660 if (token
->is_op(OPERATOR_CHANOP
))
5661 error_at(this->location(),
5662 ("send statement used as value; "
5663 "use select for non-blocking send"));
5665 error_at(this->location(),
5666 "expected %<;%> or newline after top level declaration");
5667 this->skip_past_error(OPERATOR_INVALID
);
5672 // Reset the current iota value.
5680 // Return the current iota value.
5688 // Increment the current iota value.
5691 Parse::increment_iota()
5696 // Skip forward to a semicolon or OP. OP will normally be
5697 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5698 // past it and return. If we find OP, it will be the next token to
5699 // read. Return true if we are OK, false if we found EOF.
5702 Parse::skip_past_error(Operator op
)
5704 this->gogo_
->mark_locals_used();
5705 const Token
* token
= this->peek_token();
5706 while (!token
->is_op(op
))
5708 if (token
->is_eof())
5710 if (token
->is_op(OPERATOR_SEMICOLON
))
5712 this->advance_token();
5715 token
= this->advance_token();
5720 // Check that an expression is not a sink.
5723 Parse::verify_not_sink(Expression
* expr
)
5725 if (expr
->is_sink_expression())
5727 error_at(expr
->location(), "cannot use _ as value");
5728 expr
= Expression::make_error(expr
->location());
5733 // Mark a variable as used.
5736 Parse::mark_var_used(Named_object
* no
)
5738 if (no
->is_variable())
5740 no
->var_value()->set_is_used();
5742 // When a type switch uses := to define a variable, then for
5743 // each case with a single type we introduce a new variable with
5744 // the appropriate type. When we do, if the newly introduced
5745 // variable is used, then the type switch variable is used.
5746 Type_switch_vars::iterator p
= this->type_switch_vars_
.find(no
);
5747 if (p
!= this->type_switch_vars_
.end())
5748 p
->second
->var_value()->set_is_used();