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