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(), p
->location(),
2121 Expression_list
* more_exprs
=
2122 this->expression_list(NULL
, true, may_be_composite_lit
);
2123 for (Expression_list::const_iterator p
= more_exprs
->begin();
2124 p
!= more_exprs
->end();
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 // Any left-hand-side can be a sink, so if this can not be
2513 // a sink, then it must be a use of the variable.
2515 this->mark_var_used(named_object
);
2516 return Expression::make_var_reference(named_object
, location
);
2517 case Named_object::NAMED_OBJECT_SINK
:
2519 return Expression::make_sink(location
);
2522 error_at(location
, "cannot use _ as value");
2523 return Expression::make_error(location
);
2525 case Named_object::NAMED_OBJECT_FUNC
:
2526 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
2527 return Expression::make_func_reference(named_object
, NULL
,
2529 case Named_object::NAMED_OBJECT_UNKNOWN
:
2531 Unknown_expression
* ue
=
2532 Expression::make_unknown_reference(named_object
, location
);
2533 if (this->is_erroneous_function_
)
2534 ue
->set_no_error_message();
2537 case Named_object::NAMED_OBJECT_ERRONEOUS
:
2538 return Expression::make_error(location
);
2545 case Token::TOKEN_STRING
:
2546 ret
= Expression::make_string(token
->string_value(), token
->location());
2547 this->advance_token();
2550 case Token::TOKEN_CHARACTER
:
2551 ret
= Expression::make_character(token
->character_value(), NULL
,
2553 this->advance_token();
2556 case Token::TOKEN_INTEGER
:
2557 ret
= Expression::make_integer(token
->integer_value(), NULL
,
2559 this->advance_token();
2562 case Token::TOKEN_FLOAT
:
2563 ret
= Expression::make_float(token
->float_value(), NULL
,
2565 this->advance_token();
2568 case Token::TOKEN_IMAGINARY
:
2571 mpfr_init_set_ui(zero
, 0, GMP_RNDN
);
2572 ret
= Expression::make_complex(&zero
, token
->imaginary_value(),
2573 NULL
, token
->location());
2575 this->advance_token();
2579 case Token::TOKEN_KEYWORD
:
2580 switch (token
->keyword())
2583 return this->function_lit();
2585 case KEYWORD_INTERFACE
:
2587 case KEYWORD_STRUCT
:
2589 Location location
= token
->location();
2590 return Expression::make_type(this->type(), location
);
2597 case Token::TOKEN_OPERATOR
:
2598 if (token
->is_op(OPERATOR_LPAREN
))
2600 this->advance_token();
2601 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2603 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2604 error_at(this->location(), "missing %<)%>");
2606 this->advance_token();
2607 if (is_parenthesized
!= NULL
)
2608 *is_parenthesized
= true;
2611 else if (token
->is_op(OPERATOR_LSQUARE
))
2613 // Here we call array_type directly, as this is the only
2614 // case where an ellipsis is permitted for an array type.
2615 Location location
= token
->location();
2616 return Expression::make_type(this->array_type(true), location
);
2624 error_at(this->location(), "expected operand");
2625 return Expression::make_error(this->location());
2628 // Handle a reference to a variable in an enclosing function. We add
2629 // it to a list of such variables. We return a reference to a field
2630 // in a struct which will be passed on the static chain when calling
2631 // the current function.
2634 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2637 go_assert(var
->is_variable() || var
->is_result_variable());
2639 this->mark_var_used(var
);
2641 Named_object
* this_function
= this->gogo_
->current_function();
2642 Named_object
* closure
= this_function
->func_value()->closure_var();
2644 // The last argument to the Enclosing_var constructor is the index
2645 // of this variable in the closure. We add 1 to the current number
2646 // of enclosed variables, because the first field in the closure
2647 // points to the function code.
2648 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2649 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2650 this->enclosing_vars_
.insert(ev
);
2653 // This is a variable we have not seen before. Add a new field
2654 // to the closure type.
2655 this_function
->func_value()->add_closure_field(var
, location
);
2658 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2660 closure_ref
= Expression::make_unary(OPERATOR_MULT
, closure_ref
, location
);
2662 // The closure structure holds pointers to the variables, so we need
2663 // to introduce an indirection.
2664 Expression
* e
= Expression::make_field_reference(closure_ref
,
2667 e
= Expression::make_unary(OPERATOR_MULT
, e
, location
);
2671 // CompositeLit = LiteralType LiteralValue .
2672 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2673 // SliceType | MapType | TypeName .
2674 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2675 // ElementList = Element { "," Element } .
2676 // Element = [ Key ":" ] Value .
2677 // Key = FieldName | ElementIndex .
2678 // FieldName = identifier .
2679 // ElementIndex = Expression .
2680 // Value = Expression | LiteralValue .
2682 // We have already seen the type if there is one, and we are now
2683 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2684 // will be seen here as an array type whose length is "nil". The
2685 // DEPTH parameter is non-zero if this is an embedded composite
2686 // literal and the type was omitted. It gives the number of steps up
2687 // to the type which was provided. E.g., in [][]int{{1}} it will be
2688 // 1. In [][][]int{{{1}}} it will be 2.
2691 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2693 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2694 this->advance_token();
2696 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2698 this->advance_token();
2699 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2703 bool has_keys
= false;
2704 bool all_are_names
= true;
2705 Expression_list
* vals
= new Expression_list
;
2709 bool is_type_omitted
= false;
2710 bool is_name
= false;
2712 const Token
* token
= this->peek_token();
2714 if (token
->is_identifier())
2716 std::string identifier
= token
->identifier();
2717 bool is_exported
= token
->is_identifier_exported();
2718 Location location
= token
->location();
2720 if (this->advance_token()->is_op(OPERATOR_COLON
))
2722 // This may be a field name. We don't know for sure--it
2723 // could also be an expression for an array index. We
2724 // don't want to parse it as an expression because may
2725 // trigger various errors, e.g., if this identifier
2726 // happens to be the name of a package.
2727 Gogo
* gogo
= this->gogo_
;
2728 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2735 this->unget_token(Token::make_identifier_token(identifier
,
2738 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2742 else if (!token
->is_op(OPERATOR_LCURLY
))
2743 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2746 // This must be a composite literal inside another composite
2747 // literal, with the type omitted for the inner one.
2748 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2749 is_type_omitted
= true;
2752 token
= this->peek_token();
2753 if (!token
->is_op(OPERATOR_COLON
))
2756 vals
->push_back(NULL
);
2761 if (is_type_omitted
&& !val
->is_error_expression())
2763 error_at(this->location(), "unexpected %<:%>");
2764 val
= Expression::make_error(this->location());
2767 this->advance_token();
2769 if (!has_keys
&& !vals
->empty())
2771 Expression_list
* newvals
= new Expression_list
;
2772 for (Expression_list::const_iterator p
= vals
->begin();
2776 newvals
->push_back(NULL
);
2777 newvals
->push_back(*p
);
2784 if (val
->unknown_expression() != NULL
)
2785 val
->unknown_expression()->set_is_composite_literal_key();
2787 vals
->push_back(val
);
2789 if (!token
->is_op(OPERATOR_LCURLY
))
2790 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2793 // This must be a composite literal inside another
2794 // composite literal, with the type omitted for the
2796 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2799 token
= this->peek_token();
2802 vals
->push_back(val
);
2805 all_are_names
= false;
2807 if (token
->is_op(OPERATOR_COMMA
))
2809 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2811 this->advance_token();
2815 else if (token
->is_op(OPERATOR_RCURLY
))
2817 this->advance_token();
2822 if (token
->is_op(OPERATOR_SEMICOLON
))
2823 error_at(this->location(),
2824 "need trailing comma before newline in composite literal");
2826 error_at(this->location(), "expected %<,%> or %<}%>");
2828 this->gogo_
->mark_locals_used();
2830 while (!token
->is_eof()
2831 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2833 if (token
->is_op(OPERATOR_LCURLY
))
2835 else if (token
->is_op(OPERATOR_RCURLY
))
2837 token
= this->advance_token();
2839 if (token
->is_op(OPERATOR_RCURLY
))
2840 this->advance_token();
2842 return Expression::make_error(location
);
2846 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2847 all_are_names
, location
);
2850 // FunctionLit = "func" Signature Block .
2853 Parse::function_lit()
2855 Location location
= this->location();
2856 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2857 this->advance_token();
2859 Enclosing_vars hold_enclosing_vars
;
2860 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2862 Function_type
* type
= this->signature(NULL
, location
);
2863 bool fntype_is_error
= false;
2866 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2867 fntype_is_error
= true;
2870 // For a function literal, the next token must be a '{'. If we
2871 // don't see that, then we may have a type expression.
2872 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2873 return Expression::make_type(type
, location
);
2875 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2876 if (fntype_is_error
)
2877 this->is_erroneous_function_
= true;
2879 Bc_stack
* hold_break_stack
= this->break_stack_
;
2880 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2881 this->break_stack_
= NULL
;
2882 this->continue_stack_
= NULL
;
2884 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2886 Location end_loc
= this->block();
2888 this->gogo_
->finish_function(end_loc
);
2890 if (this->break_stack_
!= NULL
)
2891 delete this->break_stack_
;
2892 if (this->continue_stack_
!= NULL
)
2893 delete this->continue_stack_
;
2894 this->break_stack_
= hold_break_stack
;
2895 this->continue_stack_
= hold_continue_stack
;
2897 this->is_erroneous_function_
= hold_is_erroneous_function
;
2899 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2901 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
2904 return Expression::make_func_reference(no
, closure
, location
);
2907 // Create a closure for the nested function FUNCTION. This is based
2908 // on ENCLOSING_VARS, which is a list of all variables defined in
2909 // enclosing functions and referenced from FUNCTION. A closure is the
2910 // address of a struct which point to the real function code and
2911 // contains the addresses of all the referenced variables. This
2912 // returns NULL if no closure is required.
2915 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
2918 if (enclosing_vars
->empty())
2921 // Get the variables in order by their field index.
2923 size_t enclosing_var_count
= enclosing_vars
->size();
2924 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
2925 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
2926 p
!= enclosing_vars
->end();
2929 // Subtract 1 because index 0 is the function code.
2930 ev
[p
->index() - 1] = *p
;
2933 // Build an initializer for a composite literal of the closure's
2936 Named_object
* enclosing_function
= this->gogo_
->current_function();
2937 Expression_list
* initializer
= new Expression_list
;
2939 initializer
->push_back(Expression::make_func_code_reference(function
,
2942 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
2944 // Add 1 to i because the first field in the closure is a
2945 // pointer to the function code.
2946 go_assert(ev
[i
].index() == i
+ 1);
2947 Named_object
* var
= ev
[i
].var();
2949 if (ev
[i
].in_function() == enclosing_function
)
2950 ref
= Expression::make_var_reference(var
, location
);
2952 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
2954 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
2956 initializer
->push_back(refaddr
);
2959 Named_object
* closure_var
= function
->func_value()->closure_var();
2960 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
2961 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
2963 return Expression::make_heap_expression(cv
, location
);
2966 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2968 // If MAY_BE_SINK is true, this expression may be "_".
2970 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2973 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2974 // guard (var := expr.("type") using the literal keyword "type").
2976 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2977 // if the entire expression is in parentheses.
2980 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
2981 bool* is_type_switch
, bool* is_parenthesized
)
2983 Location start_loc
= this->location();
2984 bool operand_is_parenthesized
= false;
2985 bool whole_is_parenthesized
= false;
2987 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
2989 whole_is_parenthesized
= operand_is_parenthesized
;
2991 // An unknown name followed by a curly brace must be a composite
2992 // literal, and the unknown name must be a type.
2993 if (may_be_composite_lit
2994 && !operand_is_parenthesized
2995 && ret
->unknown_expression() != NULL
2996 && this->peek_token()->is_op(OPERATOR_LCURLY
))
2998 Named_object
* no
= ret
->unknown_expression()->named_object();
2999 Type
* type
= Type::make_forward_declaration(no
);
3000 ret
= Expression::make_type(type
, ret
->location());
3003 // We handle composite literals and type casts here, as it is the
3004 // easiest way to handle types which are in parentheses, as in
3006 if (ret
->is_type_expression())
3008 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
3010 whole_is_parenthesized
= false;
3011 if (!may_be_composite_lit
)
3013 Type
* t
= ret
->type();
3014 if (t
->named_type() != NULL
3015 || t
->forward_declaration_type() != NULL
)
3017 _("parentheses required around this composite literal "
3018 "to avoid parsing ambiguity"));
3020 else if (operand_is_parenthesized
)
3022 "cannot parenthesize type in composite literal");
3023 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
3025 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
3027 whole_is_parenthesized
= false;
3028 Location loc
= this->location();
3029 this->advance_token();
3030 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
3032 if (this->peek_token()->is_op(OPERATOR_COMMA
))
3033 this->advance_token();
3034 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
3036 error_at(this->location(),
3037 "invalid use of %<...%> in type conversion");
3038 this->advance_token();
3040 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3041 error_at(this->location(), "expected %<)%>");
3043 this->advance_token();
3044 if (expr
->is_error_expression())
3048 Type
* t
= ret
->type();
3049 if (t
->classification() == Type::TYPE_ARRAY
3050 && t
->array_type()->length() != NULL
3051 && t
->array_type()->length()->is_nil_expression())
3053 error_at(ret
->location(),
3054 "use of %<[...]%> outside of array literal");
3055 ret
= Expression::make_error(loc
);
3058 ret
= Expression::make_cast(t
, expr
, loc
);
3065 const Token
* token
= this->peek_token();
3066 if (token
->is_op(OPERATOR_LPAREN
))
3068 whole_is_parenthesized
= false;
3069 ret
= this->call(this->verify_not_sink(ret
));
3071 else if (token
->is_op(OPERATOR_DOT
))
3073 whole_is_parenthesized
= false;
3074 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3075 if (is_type_switch
!= NULL
&& *is_type_switch
)
3078 else if (token
->is_op(OPERATOR_LSQUARE
))
3080 whole_is_parenthesized
= false;
3081 ret
= this->index(this->verify_not_sink(ret
));
3087 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3088 *is_parenthesized
= true;
3093 // Selector = "." identifier .
3094 // TypeGuard = "." "(" QualifiedIdent ")" .
3096 // Note that Operand can expand to QualifiedIdent, which contains a
3097 // ".". That is handled directly in operand when it sees a package
3100 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3101 // guard (var := expr.("type") using the literal keyword "type").
3104 Parse::selector(Expression
* left
, bool* is_type_switch
)
3106 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3107 Location location
= this->location();
3109 const Token
* token
= this->advance_token();
3110 if (token
->is_identifier())
3112 // This could be a field in a struct, or a method in an
3113 // interface, or a method associated with a type. We can't know
3114 // which until we have seen all the types.
3116 this->gogo_
->pack_hidden_name(token
->identifier(),
3117 token
->is_identifier_exported());
3118 if (token
->identifier() == "_")
3120 error_at(this->location(), "invalid use of %<_%>");
3121 name
= Gogo::erroneous_name();
3123 this->advance_token();
3124 return Expression::make_selector(left
, name
, location
);
3126 else if (token
->is_op(OPERATOR_LPAREN
))
3128 this->advance_token();
3130 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3131 type
= this->type();
3134 if (is_type_switch
!= NULL
)
3135 *is_type_switch
= true;
3138 error_at(this->location(),
3139 "use of %<.(type)%> outside type switch");
3140 type
= Type::make_error_type();
3142 this->advance_token();
3144 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3145 error_at(this->location(), "missing %<)%>");
3147 this->advance_token();
3148 if (is_type_switch
!= NULL
&& *is_type_switch
)
3150 return Expression::make_type_guard(left
, type
, location
);
3154 error_at(this->location(), "expected identifier or %<(%>");
3159 // Index = "[" Expression "]" .
3160 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3163 Parse::index(Expression
* expr
)
3165 Location location
= this->location();
3166 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3167 this->advance_token();
3170 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3171 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3175 mpz_init_set_ui(zero
, 0);
3176 start
= Expression::make_integer(&zero
, NULL
, location
);
3180 Expression
* end
= NULL
;
3181 if (this->peek_token()->is_op(OPERATOR_COLON
))
3183 // We use nil to indicate a missing high expression.
3184 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3185 end
= Expression::make_nil(this->location());
3186 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3188 error_at(this->location(), "middle index required in 3-index slice");
3189 end
= Expression::make_error(this->location());
3192 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3195 Expression
* cap
= NULL
;
3196 if (this->peek_token()->is_op(OPERATOR_COLON
))
3198 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3200 error_at(this->location(), "final index required in 3-index slice");
3201 cap
= Expression::make_error(this->location());
3204 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3206 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3207 error_at(this->location(), "missing %<]%>");
3209 this->advance_token();
3210 return Expression::make_index(expr
, start
, end
, cap
, location
);
3213 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3214 // ArgumentList = ExpressionList [ "..." ] .
3217 Parse::call(Expression
* func
)
3219 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3220 Expression_list
* args
= NULL
;
3221 bool is_varargs
= false;
3222 const Token
* token
= this->advance_token();
3223 if (!token
->is_op(OPERATOR_RPAREN
))
3225 args
= this->expression_list(NULL
, false, true);
3226 token
= this->peek_token();
3227 if (token
->is_op(OPERATOR_ELLIPSIS
))
3230 token
= this->advance_token();
3233 if (token
->is_op(OPERATOR_COMMA
))
3234 token
= this->advance_token();
3235 if (!token
->is_op(OPERATOR_RPAREN
))
3236 error_at(this->location(), "missing %<)%>");
3238 this->advance_token();
3239 if (func
->is_error_expression())
3241 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3244 // Return an expression for a single unqualified identifier.
3247 Parse::id_to_expression(const std::string
& name
, Location location
,
3250 Named_object
* in_function
;
3251 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3252 if (named_object
== NULL
)
3253 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3255 if (in_function
!= NULL
3256 && in_function
!= this->gogo_
->current_function()
3257 && (named_object
->is_variable() || named_object
->is_result_variable()))
3258 return this->enclosing_var_reference(in_function
, named_object
,
3261 switch (named_object
->classification())
3263 case Named_object::NAMED_OBJECT_CONST
:
3264 return Expression::make_const_reference(named_object
, location
);
3265 case Named_object::NAMED_OBJECT_VAR
:
3266 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3268 this->mark_var_used(named_object
);
3269 return Expression::make_var_reference(named_object
, location
);
3270 case Named_object::NAMED_OBJECT_SINK
:
3271 return Expression::make_sink(location
);
3272 case Named_object::NAMED_OBJECT_FUNC
:
3273 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3274 return Expression::make_func_reference(named_object
, NULL
, location
);
3275 case Named_object::NAMED_OBJECT_UNKNOWN
:
3277 Unknown_expression
* ue
=
3278 Expression::make_unknown_reference(named_object
, location
);
3279 if (this->is_erroneous_function_
)
3280 ue
->set_no_error_message();
3283 case Named_object::NAMED_OBJECT_PACKAGE
:
3284 case Named_object::NAMED_OBJECT_TYPE
:
3285 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3287 // These cases can arise for a field name in a composite
3289 Unknown_expression
* ue
=
3290 Expression::make_unknown_reference(named_object
, location
);
3291 if (this->is_erroneous_function_
)
3292 ue
->set_no_error_message();
3295 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3296 return Expression::make_error(location
);
3298 error_at(this->location(), "unexpected type of identifier");
3299 return Expression::make_error(location
);
3303 // Expression = UnaryExpr { binary_op Expression } .
3305 // PRECEDENCE is the precedence of the current operator.
3307 // If MAY_BE_SINK is true, this expression may be "_".
3309 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3312 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3313 // guard (var := expr.("type") using the literal keyword "type").
3315 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3316 // if the entire expression is in parentheses.
3319 Parse::expression(Precedence precedence
, bool may_be_sink
,
3320 bool may_be_composite_lit
, bool* is_type_switch
,
3321 bool *is_parenthesized
)
3323 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3324 is_type_switch
, is_parenthesized
);
3328 if (is_type_switch
!= NULL
&& *is_type_switch
)
3331 const Token
* token
= this->peek_token();
3332 if (token
->classification() != Token::TOKEN_OPERATOR
)
3338 Precedence right_precedence
;
3339 switch (token
->op())
3342 right_precedence
= PRECEDENCE_OROR
;
3344 case OPERATOR_ANDAND
:
3345 right_precedence
= PRECEDENCE_ANDAND
;
3348 case OPERATOR_NOTEQ
:
3353 right_precedence
= PRECEDENCE_RELOP
;
3356 case OPERATOR_MINUS
:
3359 right_precedence
= PRECEDENCE_ADDOP
;
3364 case OPERATOR_LSHIFT
:
3365 case OPERATOR_RSHIFT
:
3367 case OPERATOR_BITCLEAR
:
3368 right_precedence
= PRECEDENCE_MULOP
;
3371 right_precedence
= PRECEDENCE_INVALID
;
3375 if (right_precedence
== PRECEDENCE_INVALID
)
3381 if (is_parenthesized
!= NULL
)
3382 *is_parenthesized
= false;
3384 Operator op
= token
->op();
3385 Location binop_location
= token
->location();
3387 if (precedence
>= right_precedence
)
3389 // We've already seen A * B, and we see + C. We want to
3390 // return so that A * B becomes a group.
3394 this->advance_token();
3396 left
= this->verify_not_sink(left
);
3397 Expression
* right
= this->expression(right_precedence
, false,
3398 may_be_composite_lit
,
3400 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3405 Parse::expression_may_start_here()
3407 const Token
* token
= this->peek_token();
3408 switch (token
->classification())
3410 case Token::TOKEN_INVALID
:
3411 case Token::TOKEN_EOF
:
3413 case Token::TOKEN_KEYWORD
:
3414 switch (token
->keyword())
3419 case KEYWORD_STRUCT
:
3420 case KEYWORD_INTERFACE
:
3425 case Token::TOKEN_IDENTIFIER
:
3427 case Token::TOKEN_STRING
:
3429 case Token::TOKEN_OPERATOR
:
3430 switch (token
->op())
3433 case OPERATOR_MINUS
:
3437 case OPERATOR_CHANOP
:
3439 case OPERATOR_LPAREN
:
3440 case OPERATOR_LSQUARE
:
3445 case Token::TOKEN_CHARACTER
:
3446 case Token::TOKEN_INTEGER
:
3447 case Token::TOKEN_FLOAT
:
3448 case Token::TOKEN_IMAGINARY
:
3455 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3457 // If MAY_BE_SINK is true, this expression may be "_".
3459 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3462 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3463 // guard (var := expr.("type") using the literal keyword "type").
3465 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3466 // if the entire expression is in parentheses.
3469 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3470 bool* is_type_switch
, bool* is_parenthesized
)
3472 const Token
* token
= this->peek_token();
3474 // There is a complex parse for <- chan. The choices are
3475 // Convert x to type <- chan int:
3477 // Receive from (x converted to type chan <- chan int):
3478 // (<- chan <- chan int (x))
3479 // Convert x to type <- chan (<- chan int).
3480 // (<- chan <- chan int)(x)
3481 if (token
->is_op(OPERATOR_CHANOP
))
3483 Location location
= token
->location();
3484 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3486 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3488 if (expr
->is_error_expression())
3490 else if (!expr
->is_type_expression())
3491 return Expression::make_receive(expr
, location
);
3494 if (expr
->type()->is_error_type())
3497 // We picked up "chan TYPE", but it is not a type
3499 Channel_type
* ct
= expr
->type()->channel_type();
3502 // This is probably impossible.
3503 error_at(location
, "expected channel type");
3504 return Expression::make_error(location
);
3506 else if (ct
->may_receive())
3509 Type
* t
= Type::make_channel_type(false, true,
3510 ct
->element_type());
3511 return Expression::make_type(t
, location
);
3515 // <- chan <- TYPE. Because we skipped the leading
3516 // <-, we parsed this as chan <- TYPE. With the
3517 // leading <-, we parse it as <- chan (<- TYPE).
3518 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3519 return Expression::make_type(t
, location
);
3524 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3525 token
= this->peek_token();
3528 if (token
->is_op(OPERATOR_PLUS
)
3529 || token
->is_op(OPERATOR_MINUS
)
3530 || token
->is_op(OPERATOR_NOT
)
3531 || token
->is_op(OPERATOR_XOR
)
3532 || token
->is_op(OPERATOR_CHANOP
)
3533 || token
->is_op(OPERATOR_MULT
)
3534 || token
->is_op(OPERATOR_AND
))
3536 Location location
= token
->location();
3537 Operator op
= token
->op();
3538 this->advance_token();
3540 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3542 if (expr
->is_error_expression())
3544 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3545 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3547 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3548 expr
= Expression::make_heap_expression(expr
, location
);
3549 else if (op
!= OPERATOR_CHANOP
)
3550 expr
= Expression::make_unary(op
, expr
, location
);
3552 expr
= Expression::make_receive(expr
, location
);
3556 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3557 is_type_switch
, is_parenthesized
);
3560 // This is called for the obscure case of
3561 // (<- chan <- chan int)(x)
3562 // In unary_expr we remove the leading <- and parse the remainder,
3564 // chan <- (chan int)
3565 // When we add the leading <- back in, we really want
3566 // <- chan (<- chan int)
3567 // This means that we need to reassociate.
3570 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3572 Channel_type
* ele
= ct
->element_type()->channel_type();
3575 error_at(location
, "parse error");
3576 return Type::make_error_type();
3579 if (ele
->may_send())
3580 sub
= Type::make_channel_type(false, true, ele
->element_type());
3582 sub
= this->reassociate_chan_direction(ele
, location
);
3583 return Type::make_channel_type(false, true, sub
);
3587 // Declaration | LabeledStmt | SimpleStmt |
3588 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3589 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3592 // LABEL is the label of this statement if it has one.
3595 Parse::statement(Label
* label
)
3597 const Token
* token
= this->peek_token();
3598 switch (token
->classification())
3600 case Token::TOKEN_KEYWORD
:
3602 switch (token
->keyword())
3607 this->declaration();
3611 case KEYWORD_STRUCT
:
3612 case KEYWORD_INTERFACE
:
3613 this->simple_stat(true, NULL
, NULL
, NULL
);
3617 this->go_or_defer_stat();
3619 case KEYWORD_RETURN
:
3620 this->return_stat();
3625 case KEYWORD_CONTINUE
:
3626 this->continue_stat();
3634 case KEYWORD_SWITCH
:
3635 this->switch_stat(label
);
3637 case KEYWORD_SELECT
:
3638 this->select_stat(label
);
3641 this->for_stat(label
);
3644 error_at(this->location(), "expected statement");
3645 this->advance_token();
3651 case Token::TOKEN_IDENTIFIER
:
3653 std::string identifier
= token
->identifier();
3654 bool is_exported
= token
->is_identifier_exported();
3655 Location location
= token
->location();
3656 if (this->advance_token()->is_op(OPERATOR_COLON
))
3658 this->advance_token();
3659 this->labeled_stmt(identifier
, location
);
3663 this->unget_token(Token::make_identifier_token(identifier
,
3666 this->simple_stat(true, NULL
, NULL
, NULL
);
3671 case Token::TOKEN_OPERATOR
:
3672 if (token
->is_op(OPERATOR_LCURLY
))
3674 Location location
= token
->location();
3675 this->gogo_
->start_block(location
);
3676 Location end_loc
= this->block();
3677 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3680 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3681 this->simple_stat(true, NULL
, NULL
, NULL
);
3684 case Token::TOKEN_STRING
:
3685 case Token::TOKEN_CHARACTER
:
3686 case Token::TOKEN_INTEGER
:
3687 case Token::TOKEN_FLOAT
:
3688 case Token::TOKEN_IMAGINARY
:
3689 this->simple_stat(true, NULL
, NULL
, NULL
);
3693 error_at(this->location(), "expected statement");
3694 this->advance_token();
3700 Parse::statement_may_start_here()
3702 const Token
* token
= this->peek_token();
3703 switch (token
->classification())
3705 case Token::TOKEN_KEYWORD
:
3707 switch (token
->keyword())
3714 case KEYWORD_STRUCT
:
3715 case KEYWORD_INTERFACE
:
3718 case KEYWORD_RETURN
:
3720 case KEYWORD_CONTINUE
:
3723 case KEYWORD_SWITCH
:
3724 case KEYWORD_SELECT
:
3734 case Token::TOKEN_IDENTIFIER
:
3737 case Token::TOKEN_OPERATOR
:
3738 if (token
->is_op(OPERATOR_LCURLY
)
3739 || token
->is_op(OPERATOR_SEMICOLON
))
3742 return this->expression_may_start_here();
3744 case Token::TOKEN_STRING
:
3745 case Token::TOKEN_CHARACTER
:
3746 case Token::TOKEN_INTEGER
:
3747 case Token::TOKEN_FLOAT
:
3748 case Token::TOKEN_IMAGINARY
:
3756 // LabeledStmt = Label ":" Statement .
3757 // Label = identifier .
3760 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3762 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3764 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3766 // This is a label at the end of a block. A program is
3767 // permitted to omit a semicolon here.
3771 if (!this->statement_may_start_here())
3773 // Mark the label as used to avoid a useless error about an
3776 label
->set_is_used();
3778 error_at(location
, "missing statement after label");
3779 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3784 this->statement(label
);
3787 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3788 // Assignment | ShortVarDecl .
3790 // EmptyStmt was handled in Parse::statement.
3792 // In order to make this work for if and switch statements, if
3793 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3794 // expression rather than adding an expression statement to the
3795 // current block. If we see something other than an ExpressionStat,
3796 // we add the statement, set *RETURN_EXP to true if we saw a send
3797 // statement, and return NULL. The handling of send statements is for
3798 // better error messages.
3800 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3803 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3804 // guard (var := expr.("type") using the literal keyword "type").
3807 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3808 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3810 const Token
* token
= this->peek_token();
3812 // An identifier follow by := is a SimpleVarDecl.
3813 if (token
->is_identifier())
3815 std::string identifier
= token
->identifier();
3816 bool is_exported
= token
->is_identifier_exported();
3817 Location location
= token
->location();
3819 token
= this->advance_token();
3820 if (token
->is_op(OPERATOR_COLONEQ
)
3821 || token
->is_op(OPERATOR_COMMA
))
3823 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3824 this->simple_var_decl_or_assignment(identifier
, location
,
3825 may_be_composite_lit
,
3827 (token
->is_op(OPERATOR_COLONEQ
)
3833 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3837 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3838 may_be_composite_lit
,
3839 (p_type_switch
== NULL
3841 : &p_type_switch
->found
),
3843 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3845 p_type_switch
->name
.clear();
3846 p_type_switch
->location
= exp
->location();
3847 p_type_switch
->expr
= this->verify_not_sink(exp
);
3850 token
= this->peek_token();
3851 if (token
->is_op(OPERATOR_CHANOP
))
3853 this->send_stmt(this->verify_not_sink(exp
));
3854 if (return_exp
!= NULL
)
3857 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3858 || token
->is_op(OPERATOR_MINUSMINUS
))
3859 this->inc_dec_stat(this->verify_not_sink(exp
));
3860 else if (token
->is_op(OPERATOR_COMMA
)
3861 || token
->is_op(OPERATOR_EQ
))
3862 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3863 else if (token
->is_op(OPERATOR_PLUSEQ
)
3864 || token
->is_op(OPERATOR_MINUSEQ
)
3865 || token
->is_op(OPERATOR_OREQ
)
3866 || token
->is_op(OPERATOR_XOREQ
)
3867 || token
->is_op(OPERATOR_MULTEQ
)
3868 || token
->is_op(OPERATOR_DIVEQ
)
3869 || token
->is_op(OPERATOR_MODEQ
)
3870 || token
->is_op(OPERATOR_LSHIFTEQ
)
3871 || token
->is_op(OPERATOR_RSHIFTEQ
)
3872 || token
->is_op(OPERATOR_ANDEQ
)
3873 || token
->is_op(OPERATOR_BITCLEAREQ
))
3874 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
3876 else if (return_exp
!= NULL
)
3877 return this->verify_not_sink(exp
);
3880 exp
= this->verify_not_sink(exp
);
3882 if (token
->is_op(OPERATOR_COLONEQ
))
3884 if (!exp
->is_error_expression())
3885 error_at(token
->location(), "non-name on left side of %<:=%>");
3886 this->gogo_
->mark_locals_used();
3887 while (!token
->is_op(OPERATOR_SEMICOLON
)
3888 && !token
->is_eof())
3889 token
= this->advance_token();
3893 this->expression_stat(exp
);
3900 Parse::simple_stat_may_start_here()
3902 return this->expression_may_start_here();
3905 // Parse { Statement ";" } which is used in a few places. The list of
3906 // statements may end with a right curly brace, in which case the
3907 // semicolon may be omitted.
3910 Parse::statement_list()
3912 while (this->statement_may_start_here())
3914 this->statement(NULL
);
3915 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
3916 this->advance_token();
3917 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3921 if (!this->peek_token()->is_eof() || !saw_errors())
3922 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3923 if (!this->skip_past_error(OPERATOR_RCURLY
))
3930 Parse::statement_list_may_start_here()
3932 return this->statement_may_start_here();
3935 // ExpressionStat = Expression .
3938 Parse::expression_stat(Expression
* exp
)
3940 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
3943 // SendStmt = Channel "<-" Expression .
3944 // Channel = Expression .
3947 Parse::send_stmt(Expression
* channel
)
3949 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
3950 Location loc
= this->location();
3951 this->advance_token();
3952 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
3954 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
3955 this->gogo_
->add_statement(s
);
3958 // IncDecStat = Expression ( "++" | "--" ) .
3961 Parse::inc_dec_stat(Expression
* exp
)
3963 const Token
* token
= this->peek_token();
3965 // Lvalue maps require special handling.
3966 if (exp
->index_expression() != NULL
)
3967 exp
->index_expression()->set_is_lvalue();
3969 if (token
->is_op(OPERATOR_PLUSPLUS
))
3970 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
3971 else if (token
->is_op(OPERATOR_MINUSMINUS
))
3972 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
3975 this->advance_token();
3978 // Assignment = ExpressionList assign_op ExpressionList .
3980 // EXP is an expression that we have already parsed.
3982 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3983 // side may be a composite literal.
3985 // If RANGE_CLAUSE is not NULL, then this will recognize a
3989 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
3990 Range_clause
* p_range_clause
)
3992 Expression_list
* vars
;
3993 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
3995 vars
= new Expression_list();
3996 vars
->push_back(expr
);
4000 this->advance_token();
4001 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
4004 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
4007 // An assignment statement. LHS is the list of expressions which
4008 // appear on the left hand side.
4010 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4011 // side may be a composite literal.
4013 // If RANGE_CLAUSE is not NULL, then this will recognize a
4017 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
4018 Range_clause
* p_range_clause
)
4020 const Token
* token
= this->peek_token();
4021 if (!token
->is_op(OPERATOR_EQ
)
4022 && !token
->is_op(OPERATOR_PLUSEQ
)
4023 && !token
->is_op(OPERATOR_MINUSEQ
)
4024 && !token
->is_op(OPERATOR_OREQ
)
4025 && !token
->is_op(OPERATOR_XOREQ
)
4026 && !token
->is_op(OPERATOR_MULTEQ
)
4027 && !token
->is_op(OPERATOR_DIVEQ
)
4028 && !token
->is_op(OPERATOR_MODEQ
)
4029 && !token
->is_op(OPERATOR_LSHIFTEQ
)
4030 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4031 && !token
->is_op(OPERATOR_ANDEQ
)
4032 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4034 error_at(this->location(), "expected assignment operator");
4037 Operator op
= token
->op();
4038 Location location
= token
->location();
4040 token
= this->advance_token();
4042 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4044 if (op
!= OPERATOR_EQ
)
4045 error_at(this->location(), "range clause requires %<=%>");
4046 this->range_clause_expr(lhs
, p_range_clause
);
4050 Expression_list
* vals
= this->expression_list(NULL
, false,
4051 may_be_composite_lit
);
4053 // We've parsed everything; check for errors.
4054 if (lhs
== NULL
|| vals
== NULL
)
4056 for (Expression_list::const_iterator pe
= lhs
->begin();
4060 if ((*pe
)->is_error_expression())
4062 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4063 error_at((*pe
)->location(), "cannot use _ as value");
4065 for (Expression_list::const_iterator pe
= vals
->begin();
4069 if ((*pe
)->is_error_expression())
4073 // Map expressions act differently when they are lvalues.
4074 for (Expression_list::iterator plv
= lhs
->begin();
4077 if ((*plv
)->index_expression() != NULL
)
4078 (*plv
)->index_expression()->set_is_lvalue();
4080 Call_expression
* call
;
4081 Index_expression
* map_index
;
4082 Receive_expression
* receive
;
4083 Type_guard_expression
* type_guard
;
4084 if (lhs
->size() == vals
->size())
4087 if (lhs
->size() > 1)
4089 if (op
!= OPERATOR_EQ
)
4090 error_at(location
, "multiple values only permitted with %<=%>");
4091 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4095 if (op
== OPERATOR_EQ
)
4096 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4099 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4100 vals
->front(), location
);
4104 this->gogo_
->add_statement(s
);
4106 else if (vals
->size() == 1
4107 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4109 if (op
!= OPERATOR_EQ
)
4110 error_at(location
, "multiple results only permitted with %<=%>");
4111 call
->set_expected_result_count(lhs
->size());
4113 vals
= new Expression_list
;
4114 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4115 vals
->push_back(Expression::make_call_result(call
, i
));
4116 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4117 this->gogo_
->add_statement(s
);
4119 else if (lhs
->size() == 2
4120 && vals
->size() == 1
4121 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4123 if (op
!= OPERATOR_EQ
)
4124 error_at(location
, "two values from map requires %<=%>");
4125 Expression
* val
= lhs
->front();
4126 Expression
* present
= lhs
->back();
4127 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4128 map_index
, location
);
4129 this->gogo_
->add_statement(s
);
4131 else if (lhs
->size() == 1
4132 && vals
->size() == 2
4133 && (map_index
= lhs
->front()->index_expression()) != NULL
)
4135 if (op
!= OPERATOR_EQ
)
4136 error_at(location
, "assigning tuple to map index requires %<=%>");
4137 Expression
* val
= vals
->front();
4138 Expression
* should_set
= vals
->back();
4139 Statement
* s
= Statement::make_map_assignment(map_index
, val
, should_set
,
4141 this->gogo_
->add_statement(s
);
4143 else if (lhs
->size() == 2
4144 && vals
->size() == 1
4145 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4147 if (op
!= OPERATOR_EQ
)
4148 error_at(location
, "two values from receive requires %<=%>");
4149 Expression
* val
= lhs
->front();
4150 Expression
* success
= lhs
->back();
4151 Expression
* channel
= receive
->channel();
4152 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4155 this->gogo_
->add_statement(s
);
4157 else if (lhs
->size() == 2
4158 && vals
->size() == 1
4159 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4161 if (op
!= OPERATOR_EQ
)
4162 error_at(location
, "two values from type guard requires %<=%>");
4163 Expression
* val
= lhs
->front();
4164 Expression
* ok
= lhs
->back();
4165 Expression
* expr
= type_guard
->expr();
4166 Type
* type
= type_guard
->type();
4167 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4170 this->gogo_
->add_statement(s
);
4174 error_at(location
, "number of variables does not match number of values");
4178 // GoStat = "go" Expression .
4179 // DeferStat = "defer" Expression .
4182 Parse::go_or_defer_stat()
4184 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4185 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4186 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4187 Location stat_location
= this->location();
4189 this->advance_token();
4190 Location expr_location
= this->location();
4192 bool is_parenthesized
= false;
4193 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4195 Call_expression
* call_expr
= expr
->call_expression();
4196 if (is_parenthesized
|| call_expr
== NULL
)
4198 error_at(expr_location
, "argument to go/defer must be function call");
4202 // Make it easier to simplify go/defer statements by putting every
4203 // statement in its own block.
4204 this->gogo_
->start_block(stat_location
);
4207 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4209 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4210 this->gogo_
->add_statement(stat
);
4211 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4215 // ReturnStat = "return" [ ExpressionList ] .
4218 Parse::return_stat()
4220 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4221 Location location
= this->location();
4222 this->advance_token();
4223 Expression_list
* vals
= NULL
;
4224 if (this->expression_may_start_here())
4225 vals
= this->expression_list(NULL
, false, true);
4226 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4229 && this->gogo_
->current_function()->func_value()->results_are_named())
4231 Named_object
* function
= this->gogo_
->current_function();
4232 Function::Results
* results
= function
->func_value()->result_variables();
4233 for (Function::Results::const_iterator p
= results
->begin();
4234 p
!= results
->end();
4237 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4239 go_assert(saw_errors());
4240 else if (!no
->is_result_variable())
4241 error_at(location
, "%qs is shadowed during return",
4242 (*p
)->message_name().c_str());
4247 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4248 // [ "else" ( IfStmt | Block ) ] .
4253 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4254 Location location
= this->location();
4255 this->advance_token();
4257 this->gogo_
->start_block(location
);
4259 bool saw_simple_stat
= false;
4260 Expression
* cond
= NULL
;
4261 bool saw_send_stmt
= false;
4262 if (this->simple_stat_may_start_here())
4264 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4265 saw_simple_stat
= true;
4267 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4269 // The SimpleStat is an expression statement.
4270 this->expression_stat(cond
);
4275 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4276 this->advance_token();
4277 else if (saw_simple_stat
)
4280 error_at(this->location(),
4281 ("send statement used as value; "
4282 "use select for non-blocking send"));
4284 error_at(this->location(),
4285 "expected %<;%> after statement in if expression");
4286 if (!this->expression_may_start_here())
4287 cond
= Expression::make_error(this->location());
4289 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4291 error_at(this->location(),
4292 "missing condition in if statement");
4293 cond
= Expression::make_error(this->location());
4296 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4299 // Check for the easy error of a newline before starting the block.
4300 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4302 Location semi_loc
= this->location();
4303 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4304 error_at(semi_loc
, "missing %<{%> after if clause");
4305 // Otherwise we will get an error when we call this->block
4309 this->gogo_
->start_block(this->location());
4310 Location end_loc
= this->block();
4311 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4313 // Check for the easy error of a newline before "else".
4314 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4316 Location semi_loc
= this->location();
4317 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4318 error_at(this->location(),
4319 "unexpected semicolon or newline before %<else%>");
4321 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4325 Block
* else_block
= NULL
;
4326 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4328 this->gogo_
->start_block(this->location());
4329 const Token
* token
= this->advance_token();
4330 if (token
->is_keyword(KEYWORD_IF
))
4332 else if (token
->is_op(OPERATOR_LCURLY
))
4336 error_at(this->location(), "expected %<if%> or %<{%>");
4337 this->statement(NULL
);
4339 else_block
= this->gogo_
->finish_block(this->location());
4342 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4346 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4350 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4351 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4352 // "{" { ExprCaseClause } "}" .
4353 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4354 // "{" { TypeCaseClause } "}" .
4355 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4358 Parse::switch_stat(Label
* label
)
4360 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4361 Location location
= this->location();
4362 this->advance_token();
4364 this->gogo_
->start_block(location
);
4366 bool saw_simple_stat
= false;
4367 Expression
* switch_val
= NULL
;
4369 Type_switch type_switch
;
4370 bool have_type_switch_block
= false;
4371 if (this->simple_stat_may_start_here())
4373 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4375 saw_simple_stat
= true;
4377 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4379 // The SimpleStat is an expression statement.
4380 this->expression_stat(switch_val
);
4383 if (switch_val
== NULL
&& !type_switch
.found
)
4385 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4386 this->advance_token();
4387 else if (saw_simple_stat
)
4390 error_at(this->location(),
4391 ("send statement used as value; "
4392 "use select for non-blocking send"));
4394 error_at(this->location(),
4395 "expected %<;%> after statement in switch expression");
4397 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4399 if (this->peek_token()->is_identifier())
4401 const Token
* token
= this->peek_token();
4402 std::string identifier
= token
->identifier();
4403 bool is_exported
= token
->is_identifier_exported();
4404 Location id_loc
= token
->location();
4406 token
= this->advance_token();
4407 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4408 this->unget_token(Token::make_identifier_token(identifier
,
4413 // This must be a TypeSwitchGuard. It is in a
4414 // different block from any initial SimpleStat.
4415 if (saw_simple_stat
)
4417 this->gogo_
->start_block(id_loc
);
4418 have_type_switch_block
= true;
4421 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4423 if (!type_switch
.found
)
4425 if (switch_val
== NULL
4426 || !switch_val
->is_error_expression())
4428 error_at(id_loc
, "expected type switch assignment");
4429 switch_val
= Expression::make_error(id_loc
);
4434 if (switch_val
== NULL
&& !type_switch
.found
)
4436 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4437 &type_switch
.found
, NULL
);
4438 if (type_switch
.found
)
4440 type_switch
.name
.clear();
4441 type_switch
.expr
= switch_val
;
4442 type_switch
.location
= switch_val
->location();
4448 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4450 Location token_loc
= this->location();
4451 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4452 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4453 error_at(token_loc
, "missing %<{%> after switch clause");
4454 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4456 error_at(token_loc
, "invalid variable name");
4457 this->advance_token();
4458 this->expression(PRECEDENCE_NORMAL
, false, false,
4459 &type_switch
.found
, NULL
);
4460 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4461 this->advance_token();
4462 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4464 if (have_type_switch_block
)
4465 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4467 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4471 if (type_switch
.found
)
4472 type_switch
.expr
= Expression::make_error(location
);
4476 error_at(this->location(), "expected %<{%>");
4477 if (have_type_switch_block
)
4478 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4480 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4485 this->advance_token();
4487 Statement
* statement
;
4488 if (type_switch
.found
)
4489 statement
= this->type_switch_body(label
, type_switch
, location
);
4491 statement
= this->expr_switch_body(label
, switch_val
, location
);
4493 if (statement
!= NULL
)
4494 this->gogo_
->add_statement(statement
);
4496 if (have_type_switch_block
)
4497 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4500 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4504 // The body of an expression switch.
4505 // "{" { ExprCaseClause } "}"
4508 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4511 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4514 this->push_break_statement(statement
, label
);
4516 Case_clauses
* case_clauses
= new Case_clauses();
4517 bool saw_default
= false;
4518 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4520 if (this->peek_token()->is_eof())
4523 error_at(this->location(), "missing %<}%>");
4526 this->expr_case_clause(case_clauses
, &saw_default
);
4528 this->advance_token();
4530 statement
->add_clauses(case_clauses
);
4532 this->pop_break_statement();
4537 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4538 // FallthroughStat = "fallthrough" .
4541 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4543 Location location
= this->location();
4545 bool is_default
= false;
4546 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4548 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4551 error_at(this->location(), "expected %<:%>");
4555 this->advance_token();
4557 Block
* statements
= NULL
;
4558 if (this->statement_list_may_start_here())
4560 this->gogo_
->start_block(this->location());
4561 this->statement_list();
4562 statements
= this->gogo_
->finish_block(this->location());
4565 bool is_fallthrough
= false;
4566 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4568 Location fallthrough_loc
= this->location();
4569 is_fallthrough
= true;
4570 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4571 this->advance_token();
4572 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4573 error_at(fallthrough_loc
, _("cannot fallthrough final case in switch"));
4580 error_at(location
, "multiple defaults in switch");
4583 *saw_default
= true;
4586 if (is_default
|| vals
!= NULL
)
4587 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4590 // ExprSwitchCase = "case" ExpressionList | "default" .
4593 Parse::expr_switch_case(bool* is_default
)
4595 const Token
* token
= this->peek_token();
4596 if (token
->is_keyword(KEYWORD_CASE
))
4598 this->advance_token();
4599 return this->expression_list(NULL
, false, true);
4601 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4603 this->advance_token();
4610 error_at(this->location(), "expected %<case%> or %<default%>");
4611 if (!token
->is_op(OPERATOR_RCURLY
))
4612 this->advance_token();
4617 // The body of a type switch.
4618 // "{" { TypeCaseClause } "}" .
4621 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4624 Named_object
* switch_no
= NULL
;
4625 if (!type_switch
.name
.empty())
4627 if (Gogo::is_sink_name(type_switch
.name
))
4628 error_at(type_switch
.location
,
4629 "no new variables on left side of %<:=%>");
4632 Variable
* switch_var
= new Variable(NULL
, type_switch
.expr
, false,
4634 type_switch
.location
);
4635 switch_no
= this->gogo_
->add_variable(type_switch
.name
, switch_var
);
4639 Type_switch_statement
* statement
=
4640 Statement::make_type_switch_statement(switch_no
,
4646 this->push_break_statement(statement
, label
);
4648 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4649 bool saw_default
= false;
4650 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4652 if (this->peek_token()->is_eof())
4654 error_at(this->location(), "missing %<}%>");
4657 this->type_case_clause(switch_no
, case_clauses
, &saw_default
);
4659 this->advance_token();
4661 statement
->add_clauses(case_clauses
);
4663 this->pop_break_statement();
4668 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4671 Parse::type_case_clause(Named_object
* switch_no
, Type_case_clauses
* clauses
,
4674 Location location
= this->location();
4676 std::vector
<Type
*> types
;
4677 bool is_default
= false;
4678 this->type_switch_case(&types
, &is_default
);
4680 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4681 error_at(this->location(), "expected %<:%>");
4683 this->advance_token();
4685 Block
* statements
= NULL
;
4686 if (this->statement_list_may_start_here())
4688 this->gogo_
->start_block(this->location());
4689 if (switch_no
!= NULL
&& types
.size() == 1)
4691 Type
* type
= types
.front();
4692 Expression
* init
= Expression::make_var_reference(switch_no
,
4694 init
= Expression::make_type_guard(init
, type
, location
);
4695 Variable
* v
= new Variable(type
, init
, false, false, false,
4697 v
->set_is_type_switch_var();
4698 Named_object
* no
= this->gogo_
->add_variable(switch_no
->name(), v
);
4700 // We don't want to issue an error if the compiler
4701 // introduced special variable is not used. Instead we want
4702 // to issue an error if the variable defined by the switch
4703 // is not used. That is handled via type_switch_vars_ and
4704 // Parse::mark_var_used.
4706 this->type_switch_vars_
[no
] = switch_no
;
4708 this->statement_list();
4709 statements
= this->gogo_
->finish_block(this->location());
4712 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4714 error_at(this->location(),
4715 "fallthrough is not permitted in a type switch");
4716 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4717 this->advance_token();
4722 go_assert(types
.empty());
4725 error_at(location
, "multiple defaults in type switch");
4728 *saw_default
= true;
4729 clauses
->add(NULL
, false, true, statements
, location
);
4731 else if (!types
.empty())
4733 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4734 p
+ 1 != types
.end();
4736 clauses
->add(*p
, true, false, NULL
, location
);
4737 clauses
->add(types
.back(), false, false, statements
, location
);
4740 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4743 // TypeSwitchCase = "case" type | "default"
4745 // We accept a comma separated list of types.
4748 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4750 const Token
* token
= this->peek_token();
4751 if (token
->is_keyword(KEYWORD_CASE
))
4753 this->advance_token();
4756 Type
* t
= this->type();
4758 if (!t
->is_error_type())
4759 types
->push_back(t
);
4762 this->gogo_
->mark_locals_used();
4763 token
= this->peek_token();
4764 while (!token
->is_op(OPERATOR_COLON
)
4765 && !token
->is_op(OPERATOR_COMMA
)
4766 && !token
->is_op(OPERATOR_RCURLY
)
4767 && !token
->is_eof())
4768 token
= this->advance_token();
4771 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4773 this->advance_token();
4776 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4778 this->advance_token();
4783 error_at(this->location(), "expected %<case%> or %<default%>");
4784 if (!token
->is_op(OPERATOR_RCURLY
))
4785 this->advance_token();
4789 // SelectStat = "select" "{" { CommClause } "}" .
4792 Parse::select_stat(Label
* label
)
4794 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4795 Location location
= this->location();
4796 const Token
* token
= this->advance_token();
4798 if (!token
->is_op(OPERATOR_LCURLY
))
4800 Location token_loc
= token
->location();
4801 if (token
->is_op(OPERATOR_SEMICOLON
)
4802 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4803 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4806 error_at(this->location(), "expected %<{%>");
4810 this->advance_token();
4812 Select_statement
* statement
= Statement::make_select_statement(location
);
4814 this->push_break_statement(statement
, label
);
4816 Select_clauses
* select_clauses
= new Select_clauses();
4817 bool saw_default
= false;
4818 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4820 if (this->peek_token()->is_eof())
4822 error_at(this->location(), "expected %<}%>");
4825 this->comm_clause(select_clauses
, &saw_default
);
4828 this->advance_token();
4830 statement
->add_clauses(select_clauses
);
4832 this->pop_break_statement();
4834 this->gogo_
->add_statement(statement
);
4837 // CommClause = CommCase ":" { Statement ";" } .
4840 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4842 Location location
= this->location();
4843 bool is_send
= false;
4844 Expression
* channel
= NULL
;
4845 Expression
* val
= NULL
;
4846 Expression
* closed
= NULL
;
4847 std::string varname
;
4848 std::string closedname
;
4849 bool is_default
= false;
4850 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4851 &varname
, &closedname
, &is_default
);
4855 && closedname
.empty()
4857 && val
->index_expression() != NULL
)
4858 val
->index_expression()->set_is_lvalue();
4860 if (this->peek_token()->is_op(OPERATOR_COLON
))
4861 this->advance_token();
4863 error_at(this->location(), "expected colon");
4865 this->gogo_
->start_block(this->location());
4867 Named_object
* var
= NULL
;
4868 if (!varname
.empty())
4870 // FIXME: LOCATION is slightly wrong here.
4871 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
4873 v
->set_type_from_chan_element();
4874 var
= this->gogo_
->add_variable(varname
, v
);
4877 Named_object
* closedvar
= NULL
;
4878 if (!closedname
.empty())
4880 // FIXME: LOCATION is slightly wrong here.
4881 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
4882 false, false, false, location
);
4883 closedvar
= this->gogo_
->add_variable(closedname
, v
);
4886 this->statement_list();
4888 Block
* statements
= this->gogo_
->finish_block(this->location());
4894 error_at(location
, "multiple defaults in select");
4897 *saw_default
= true;
4901 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
4902 statements
, location
);
4903 else if (statements
!= NULL
)
4905 // Add the statements to make sure that any names they define
4907 this->gogo_
->add_block(statements
, location
);
4911 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4914 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
4915 Expression
** closed
, std::string
* varname
,
4916 std::string
* closedname
, bool* is_default
)
4918 const Token
* token
= this->peek_token();
4919 if (token
->is_keyword(KEYWORD_DEFAULT
))
4921 this->advance_token();
4924 else if (token
->is_keyword(KEYWORD_CASE
))
4926 this->advance_token();
4927 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
4933 error_at(this->location(), "expected %<case%> or %<default%>");
4934 if (!token
->is_op(OPERATOR_RCURLY
))
4935 this->advance_token();
4942 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4943 // RecvExpr = Expression .
4946 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
4947 Expression
** closed
, std::string
* varname
,
4948 std::string
* closedname
)
4950 const Token
* token
= this->peek_token();
4951 bool saw_comma
= false;
4952 bool closed_is_id
= false;
4953 if (token
->is_identifier())
4955 Gogo
* gogo
= this->gogo_
;
4956 std::string recv_var
= token
->identifier();
4957 bool is_rv_exported
= token
->is_identifier_exported();
4958 Location recv_var_loc
= token
->location();
4959 token
= this->advance_token();
4960 if (token
->is_op(OPERATOR_COLONEQ
))
4963 this->advance_token();
4964 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
4966 Receive_expression
* re
= e
->receive_expression();
4969 if (!e
->is_error_expression())
4970 error_at(this->location(), "expected receive expression");
4973 if (recv_var
== "_")
4975 error_at(recv_var_loc
,
4976 "no new variables on left side of %<:=%>");
4977 recv_var
= Gogo::erroneous_name();
4980 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
4981 *channel
= re
->channel();
4984 else if (token
->is_op(OPERATOR_COMMA
))
4986 token
= this->advance_token();
4987 if (token
->is_identifier())
4989 std::string recv_closed
= token
->identifier();
4990 bool is_rc_exported
= token
->is_identifier_exported();
4991 Location recv_closed_loc
= token
->location();
4992 closed_is_id
= true;
4994 token
= this->advance_token();
4995 if (token
->is_op(OPERATOR_COLONEQ
))
4997 // case rv, rc := <-c:
4998 this->advance_token();
4999 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
5001 Receive_expression
* re
= e
->receive_expression();
5004 if (!e
->is_error_expression())
5005 error_at(this->location(),
5006 "expected receive expression");
5009 if (recv_var
== "_" && recv_closed
== "_")
5011 error_at(recv_var_loc
,
5012 "no new variables on left side of %<:=%>");
5013 recv_var
= Gogo::erroneous_name();
5016 if (recv_var
!= "_")
5017 *varname
= gogo
->pack_hidden_name(recv_var
,
5019 if (recv_closed
!= "_")
5020 *closedname
= gogo
->pack_hidden_name(recv_closed
,
5022 *channel
= re
->channel();
5026 this->unget_token(Token::make_identifier_token(recv_closed
,
5031 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5033 recv_var_loc
, true);
5037 this->unget_token(Token::make_identifier_token(recv_var
,
5042 // If SAW_COMMA is false, then we are looking at the start of the
5043 // send or receive expression. If SAW_COMMA is true, then *VAL is
5044 // set and we just read a comma.
5047 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5048 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5053 this->advance_token();
5054 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5056 // The next token should be ':'. If it is '<-', then we have
5058 // which is to say, send on a channel received from a channel.
5059 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5062 e
= Expression::make_receive(*channel
, (*channel
)->location());
5065 if (this->peek_token()->is_op(OPERATOR_EQ
))
5067 if (!this->advance_token()->is_op(OPERATOR_CHANOP
))
5069 error_at(this->location(), "missing %<<-%>");
5073 this->advance_token();
5074 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5078 // *VAL is already set.
5079 if (!e
->is_sink_expression())
5085 if (!e
->is_sink_expression())
5094 error_at(this->location(), "expected %<=%> or %<:=%>");
5096 error_at(this->location(), "expected %<=%>");
5100 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5104 *channel
= this->verify_not_sink(e
);
5105 this->advance_token();
5106 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5110 error_at(this->location(), "expected %<<-%> or %<=%>");
5114 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5115 // Condition = Expression .
5118 Parse::for_stat(Label
* label
)
5120 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5121 Location location
= this->location();
5122 const Token
* token
= this->advance_token();
5124 // Open a block to hold any variables defined in the init statement
5125 // of the for statement.
5126 this->gogo_
->start_block(location
);
5129 Expression
* cond
= NULL
;
5131 Range_clause range_clause
;
5133 if (!token
->is_op(OPERATOR_LCURLY
))
5135 if (token
->is_keyword(KEYWORD_VAR
))
5137 error_at(this->location(),
5138 "var declaration not allowed in for initializer");
5142 if (token
->is_op(OPERATOR_SEMICOLON
))
5143 this->for_clause(&cond
, &post
);
5146 // We might be looking at a Condition, an InitStat, or a
5149 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5150 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5152 if (cond
== NULL
&& !range_clause
.found
)
5155 error_at(this->location(),
5156 ("send statement used as value; "
5157 "use select for non-blocking send"));
5159 error_at(this->location(), "parse error in for statement");
5164 if (range_clause
.found
)
5165 error_at(this->location(), "parse error after range clause");
5169 // COND is actually an expression statement for
5170 // InitStat at the start of a ForClause.
5171 this->expression_stat(cond
);
5175 this->for_clause(&cond
, &post
);
5180 // Check for the easy error of a newline before starting the block.
5181 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5183 Location semi_loc
= this->location();
5184 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5185 error_at(semi_loc
, "missing %<{%> after for clause");
5186 // Otherwise we will get an error when we call this->block
5190 // Build the For_statement and note that it is the current target
5191 // for break and continue statements.
5193 For_statement
* sfor
;
5194 For_range_statement
* srange
;
5196 if (!range_clause
.found
)
5198 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5204 srange
= Statement::make_for_range_statement(range_clause
.index
,
5212 this->push_break_statement(s
, label
);
5213 this->push_continue_statement(s
, label
);
5215 // Gather the block of statements in the loop and add them to the
5218 this->gogo_
->start_block(this->location());
5219 Location end_loc
= this->block();
5220 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5223 sfor
->add_statements(statements
);
5225 srange
->add_statements(statements
);
5227 // This is no longer the break/continue target.
5228 this->pop_break_statement();
5229 this->pop_continue_statement();
5231 // Add the For_statement to the list of statements, and close out
5232 // the block we started to hold any variables defined in the for
5235 this->gogo_
->add_statement(s
);
5237 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5241 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5242 // InitStat = SimpleStat .
5243 // PostStat = SimpleStat .
5245 // We have already read InitStat at this point.
5248 Parse::for_clause(Expression
** cond
, Block
** post
)
5250 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5251 this->advance_token();
5252 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5254 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5256 error_at(this->location(), "missing %<{%> after for clause");
5262 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5263 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5264 error_at(this->location(), "expected semicolon");
5266 this->advance_token();
5268 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5272 this->gogo_
->start_block(this->location());
5273 this->simple_stat(false, NULL
, NULL
, NULL
);
5274 *post
= this->gogo_
->finish_block(this->location());
5278 // RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
5280 // This is the := version. It is called with a list of identifiers.
5283 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5284 Range_clause
* p_range_clause
)
5286 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5287 Location location
= this->location();
5289 p_range_clause
->found
= true;
5291 go_assert(til
->size() >= 1);
5292 if (til
->size() > 2)
5293 error_at(this->location(), "too many variables for range clause");
5295 this->advance_token();
5296 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5298 p_range_clause
->range
= expr
;
5300 bool any_new
= false;
5302 const Typed_identifier
* pti
= &til
->front();
5303 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5305 if (any_new
&& no
->is_variable())
5306 no
->var_value()->set_type_from_range_index();
5307 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5309 if (til
->size() == 1)
5310 p_range_clause
->value
= NULL
;
5314 bool is_new
= false;
5315 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5316 if (is_new
&& no
->is_variable())
5317 no
->var_value()->set_type_from_range_value();
5320 if (!Gogo::is_sink_name(pti
->name()))
5321 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5325 error_at(location
, "variables redeclared but no variable is new");
5328 // The = version of RangeClause. This is called with a list of
5332 Parse::range_clause_expr(const Expression_list
* vals
,
5333 Range_clause
* p_range_clause
)
5335 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5337 p_range_clause
->found
= true;
5339 go_assert(vals
->size() >= 1);
5340 if (vals
->size() > 2)
5341 error_at(this->location(), "too many variables for range clause");
5343 this->advance_token();
5344 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5347 p_range_clause
->index
= vals
->front();
5348 if (vals
->size() == 1)
5349 p_range_clause
->value
= NULL
;
5351 p_range_clause
->value
= vals
->back();
5354 // Push a statement on the break stack.
5357 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5359 if (this->break_stack_
== NULL
)
5360 this->break_stack_
= new Bc_stack();
5361 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5364 // Push a statement on the continue stack.
5367 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5369 if (this->continue_stack_
== NULL
)
5370 this->continue_stack_
= new Bc_stack();
5371 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5374 // Pop the break stack.
5377 Parse::pop_break_statement()
5379 this->break_stack_
->pop_back();
5382 // Pop the continue stack.
5385 Parse::pop_continue_statement()
5387 this->continue_stack_
->pop_back();
5390 // Find a break or continue statement given a label name.
5393 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5395 if (bc_stack
== NULL
)
5397 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5398 p
!= bc_stack
->rend();
5401 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5403 p
->second
->set_is_used();
5410 // BreakStat = "break" [ identifier ] .
5415 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5416 Location location
= this->location();
5418 const Token
* token
= this->advance_token();
5419 Statement
* enclosing
;
5420 if (!token
->is_identifier())
5422 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5424 error_at(this->location(),
5425 "break statement not within for or switch or select");
5428 enclosing
= this->break_stack_
->back().first
;
5432 enclosing
= this->find_bc_statement(this->break_stack_
,
5433 token
->identifier());
5434 if (enclosing
== NULL
)
5436 // If there is a label with this name, mark it as used to
5437 // avoid a useless error about an unused label.
5438 this->gogo_
->add_label_reference(token
->identifier(),
5439 Linemap::unknown_location(), false);
5441 error_at(token
->location(), "invalid break label %qs",
5442 Gogo::message_name(token
->identifier()).c_str());
5443 this->advance_token();
5446 this->advance_token();
5449 Unnamed_label
* label
;
5450 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5451 label
= enclosing
->for_statement()->break_label();
5452 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5453 label
= enclosing
->for_range_statement()->break_label();
5454 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5455 label
= enclosing
->switch_statement()->break_label();
5456 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5457 label
= enclosing
->type_switch_statement()->break_label();
5458 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5459 label
= enclosing
->select_statement()->break_label();
5463 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5467 // ContinueStat = "continue" [ identifier ] .
5470 Parse::continue_stat()
5472 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5473 Location location
= this->location();
5475 const Token
* token
= this->advance_token();
5476 Statement
* enclosing
;
5477 if (!token
->is_identifier())
5479 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5481 error_at(this->location(), "continue statement not within for");
5484 enclosing
= this->continue_stack_
->back().first
;
5488 enclosing
= this->find_bc_statement(this->continue_stack_
,
5489 token
->identifier());
5490 if (enclosing
== NULL
)
5492 // If there is a label with this name, mark it as used to
5493 // avoid a useless error about an unused label.
5494 this->gogo_
->add_label_reference(token
->identifier(),
5495 Linemap::unknown_location(), false);
5497 error_at(token
->location(), "invalid continue label %qs",
5498 Gogo::message_name(token
->identifier()).c_str());
5499 this->advance_token();
5502 this->advance_token();
5505 Unnamed_label
* label
;
5506 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5507 label
= enclosing
->for_statement()->continue_label();
5508 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5509 label
= enclosing
->for_range_statement()->continue_label();
5513 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5517 // GotoStat = "goto" identifier .
5522 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5523 Location location
= this->location();
5524 const Token
* token
= this->advance_token();
5525 if (!token
->is_identifier())
5526 error_at(this->location(), "expected label for goto");
5529 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5531 Statement
* s
= Statement::make_goto_statement(label
, location
);
5532 this->gogo_
->add_statement(s
);
5533 this->advance_token();
5537 // PackageClause = "package" PackageName .
5540 Parse::package_clause()
5542 const Token
* token
= this->peek_token();
5543 Location location
= token
->location();
5545 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5547 error_at(this->location(), "program must start with package clause");
5552 token
= this->advance_token();
5553 if (token
->is_identifier())
5555 name
= token
->identifier();
5558 error_at(this->location(), "invalid package name _");
5559 name
= Gogo::erroneous_name();
5561 this->advance_token();
5565 error_at(this->location(), "package name must be an identifier");
5569 this->gogo_
->set_package_name(name
, location
);
5572 // ImportDecl = "import" Decl<ImportSpec> .
5575 Parse::import_decl()
5577 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5578 this->advance_token();
5579 this->decl(&Parse::import_spec
, NULL
);
5582 // ImportSpec = [ "." | PackageName ] PackageFileName .
5585 Parse::import_spec(void*)
5587 const Token
* token
= this->peek_token();
5588 Location location
= token
->location();
5590 std::string local_name
;
5591 bool is_local_name_exported
= false;
5592 if (token
->is_op(OPERATOR_DOT
))
5595 token
= this->advance_token();
5597 else if (token
->is_identifier())
5599 local_name
= token
->identifier();
5600 is_local_name_exported
= token
->is_identifier_exported();
5601 token
= this->advance_token();
5604 if (!token
->is_string())
5606 error_at(this->location(), "import statement not a string");
5607 this->advance_token();
5611 this->gogo_
->import_package(token
->string_value(), local_name
,
5612 is_local_name_exported
, location
);
5614 this->advance_token();
5617 // SourceFile = PackageClause ";" { ImportDecl ";" }
5618 // { TopLevelDecl ";" } .
5623 this->package_clause();
5625 const Token
* token
= this->peek_token();
5626 if (token
->is_op(OPERATOR_SEMICOLON
))
5627 token
= this->advance_token();
5629 error_at(this->location(),
5630 "expected %<;%> or newline after package clause");
5632 while (token
->is_keyword(KEYWORD_IMPORT
))
5634 this->import_decl();
5635 token
= this->peek_token();
5636 if (token
->is_op(OPERATOR_SEMICOLON
))
5637 token
= this->advance_token();
5639 error_at(this->location(),
5640 "expected %<;%> or newline after import declaration");
5643 while (!token
->is_eof())
5645 if (this->declaration_may_start_here())
5646 this->declaration();
5649 error_at(this->location(), "expected declaration");
5650 this->gogo_
->mark_locals_used();
5652 this->advance_token();
5653 while (!this->peek_token()->is_eof()
5654 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5655 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5656 if (!this->peek_token()->is_eof()
5657 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5658 this->advance_token();
5660 token
= this->peek_token();
5661 if (token
->is_op(OPERATOR_SEMICOLON
))
5662 token
= this->advance_token();
5663 else if (!token
->is_eof() || !saw_errors())
5665 if (token
->is_op(OPERATOR_CHANOP
))
5666 error_at(this->location(),
5667 ("send statement used as value; "
5668 "use select for non-blocking send"));
5670 error_at(this->location(),
5671 "expected %<;%> or newline after top level declaration");
5672 this->skip_past_error(OPERATOR_INVALID
);
5677 // Reset the current iota value.
5685 // Return the current iota value.
5693 // Increment the current iota value.
5696 Parse::increment_iota()
5701 // Skip forward to a semicolon or OP. OP will normally be
5702 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5703 // past it and return. If we find OP, it will be the next token to
5704 // read. Return true if we are OK, false if we found EOF.
5707 Parse::skip_past_error(Operator op
)
5709 this->gogo_
->mark_locals_used();
5710 const Token
* token
= this->peek_token();
5711 while (!token
->is_op(op
))
5713 if (token
->is_eof())
5715 if (token
->is_op(OPERATOR_SEMICOLON
))
5717 this->advance_token();
5720 token
= this->advance_token();
5725 // Check that an expression is not a sink.
5728 Parse::verify_not_sink(Expression
* expr
)
5730 if (expr
->is_sink_expression())
5732 error_at(expr
->location(), "cannot use _ as value");
5733 expr
= Expression::make_error(expr
->location());
5736 // If this can not be a sink, and it is a variable, then we are
5737 // using the variable, not just assigning to it.
5738 Var_expression
* ve
= expr
->var_expression();
5740 this->mark_var_used(ve
->named_object());
5745 // Mark a variable as used.
5748 Parse::mark_var_used(Named_object
* no
)
5750 if (no
->is_variable())
5752 no
->var_value()->set_is_used();
5754 // When a type switch uses := to define a variable, then for
5755 // each case with a single type we introduce a new variable with
5756 // the appropriate type. When we do, if the newly introduced
5757 // variable is used, then the type switch variable is used.
5758 Type_switch_vars::iterator p
= this->type_switch_vars_
.find(no
);
5759 if (p
!= this->type_switch_vars_
.end())
5760 p
->second
->var_value()->set_is_used();