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.
11 #include "go-diagnostics.h"
13 #include "statements.h"
14 #include "expressions.h"
17 // Struct Parse::Enclosing_var_comparison.
19 // Return true if v1 should be considered to be less than v2.
22 Parse::Enclosing_var_comparison::operator()(const Enclosing_var
& v1
,
23 const Enclosing_var
& v2
)
25 if (v1
.var() == v2
.var())
28 const std::string
& n1(v1
.var()->name());
29 const std::string
& n2(v2
.var()->name());
30 int i
= n1
.compare(n2
);
36 // If we get here it means that a single nested function refers to
37 // two different variables defined in enclosing functions, and both
38 // variables have the same name. I think this is impossible.
44 Parse::Parse(Lex
* lex
, Gogo
* gogo
)
46 token_(Token::make_invalid_token(Linemap::unknown_location())),
47 unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
48 unget_token_valid_(false),
49 is_erroneous_function_(false),
52 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 go_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 go_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 go_error_at(this->location(), "expected package");
196 // We expect . IDENTIFIER; skip both.
197 if (this->advance_token()->is_identifier())
198 this->advance_token();
202 package
->package_value()->note_usage(Gogo::unpack_hidden_name(name
));
204 token
= this->advance_token();
205 if (!token
->is_identifier())
207 go_error_at(this->location(), "expected identifier");
211 name
= token
->identifier();
215 go_error_at(this->location(), "invalid use of %<_%>");
216 name
= Gogo::erroneous_name();
219 if (package
->name() == this->gogo_
->package_name())
220 name
= this->gogo_
->pack_hidden_name(name
,
221 token
->is_identifier_exported());
226 this->advance_token();
231 // Type = TypeName | TypeLit | "(" Type ")" .
233 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
234 // SliceType | MapType | ChannelType .
239 const Token
* token
= this->peek_token();
240 if (token
->is_identifier())
241 return this->type_name(true);
242 else if (token
->is_op(OPERATOR_LSQUARE
))
243 return this->array_type(false);
244 else if (token
->is_keyword(KEYWORD_CHAN
)
245 || token
->is_op(OPERATOR_CHANOP
))
246 return this->channel_type();
247 else if (token
->is_keyword(KEYWORD_INTERFACE
))
248 return this->interface_type(true);
249 else if (token
->is_keyword(KEYWORD_FUNC
))
251 Location location
= token
->location();
252 this->advance_token();
253 Type
* type
= this->signature(NULL
, location
);
255 return Type::make_error_type();
258 else if (token
->is_keyword(KEYWORD_MAP
))
259 return this->map_type();
260 else if (token
->is_keyword(KEYWORD_STRUCT
))
261 return this->struct_type();
262 else if (token
->is_op(OPERATOR_MULT
))
263 return this->pointer_type();
264 else if (token
->is_op(OPERATOR_LPAREN
))
266 this->advance_token();
267 Type
* ret
= this->type();
268 if (this->peek_token()->is_op(OPERATOR_RPAREN
))
269 this->advance_token();
272 if (!ret
->is_error_type())
273 go_error_at(this->location(), "expected %<)%>");
279 go_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 go_error_at(location
,
334 "invalid reference to hidden type %<%s.%s%>",
335 Gogo::message_name(packname
).c_str(),
336 Gogo::message_name(name
).c_str());
343 if (named_object
== NULL
)
346 named_object
= this->gogo_
->add_unknown_name(name
, location
);
349 const std::string
& packname(package
->package_value()->package_name());
350 go_error_at(location
, "reference to undefined identifier %<%s.%s%>",
351 Gogo::message_name(packname
).c_str(),
352 Gogo::message_name(name
).c_str());
357 else if (named_object
->is_type())
359 if (!named_object
->type_value()->is_visible())
362 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
370 go_error_at(location
, "expected type");
371 return Type::make_error_type();
374 if (named_object
->is_type())
375 return named_object
->type_value();
376 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
377 return Type::make_forward_declaration(named_object
);
382 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
383 // ArrayLength = Expression .
384 // ElementType = CompleteType .
387 Parse::array_type(bool may_use_ellipsis
)
389 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
390 const Token
* token
= this->advance_token();
392 Expression
* length
= NULL
;
393 if (token
->is_op(OPERATOR_RSQUARE
))
394 this->advance_token();
397 if (!token
->is_op(OPERATOR_ELLIPSIS
))
398 length
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
399 else if (may_use_ellipsis
)
401 // An ellipsis is used in composite literals to represent a
402 // fixed array of the size of the number of elements. We
403 // use a length of nil to represent this, and change the
404 // length when parsing the composite literal.
405 length
= Expression::make_nil(this->location());
406 this->advance_token();
410 go_error_at(this->location(),
411 "use of %<[...]%> outside of array literal");
412 length
= Expression::make_error(this->location());
413 this->advance_token();
415 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
417 go_error_at(this->location(), "expected %<]%>");
418 return Type::make_error_type();
420 this->advance_token();
423 Type
* element_type
= this->type();
424 if (element_type
->is_error_type())
425 return Type::make_error_type();
427 return Type::make_array_type(element_type
, length
);
430 // MapType = "map" "[" KeyType "]" ValueType .
431 // KeyType = CompleteType .
432 // ValueType = CompleteType .
437 Location location
= this->location();
438 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP
));
439 if (!this->advance_token()->is_op(OPERATOR_LSQUARE
))
441 go_error_at(this->location(), "expected %<[%>");
442 return Type::make_error_type();
444 this->advance_token();
446 Type
* key_type
= this->type();
448 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
450 go_error_at(this->location(), "expected %<]%>");
451 return Type::make_error_type();
453 this->advance_token();
455 Type
* value_type
= this->type();
457 if (key_type
->is_error_type() || value_type
->is_error_type())
458 return Type::make_error_type();
460 return Type::make_map_type(key_type
, value_type
, location
);
463 // StructType = "struct" "{" { FieldDecl ";" } "}" .
468 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT
));
469 Location location
= this->location();
470 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
472 Location token_loc
= this->location();
473 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
474 && this->advance_token()->is_op(OPERATOR_LCURLY
))
475 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
478 go_error_at(this->location(), "expected %<{%>");
479 return Type::make_error_type();
482 this->advance_token();
484 Struct_field_list
* sfl
= new Struct_field_list
;
485 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
487 this->field_decl(sfl
);
488 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
489 this->advance_token();
490 else if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
492 go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
493 if (!this->skip_past_error(OPERATOR_RCURLY
))
494 return Type::make_error_type();
497 this->advance_token();
499 for (Struct_field_list::const_iterator pi
= sfl
->begin();
503 if (pi
->type()->is_error_type())
505 for (Struct_field_list::const_iterator pj
= pi
+ 1;
509 if (pi
->field_name() == pj
->field_name()
510 && !Gogo::is_sink_name(pi
->field_name()))
511 go_error_at(pi
->location(), "duplicate field name %<%s%>",
512 Gogo::message_name(pi
->field_name()).c_str());
516 return Type::make_struct_type(sfl
, location
);
519 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
520 // Tag = string_lit .
523 Parse::field_decl(Struct_field_list
* sfl
)
525 const Token
* token
= this->peek_token();
526 Location location
= token
->location();
528 bool is_anonymous_pointer
;
529 if (token
->is_op(OPERATOR_MULT
))
532 is_anonymous_pointer
= true;
534 else if (token
->is_identifier())
536 std::string id
= token
->identifier();
537 bool is_id_exported
= token
->is_identifier_exported();
538 Location id_location
= token
->location();
539 token
= this->advance_token();
540 is_anonymous
= (token
->is_op(OPERATOR_SEMICOLON
)
541 || token
->is_op(OPERATOR_RCURLY
)
542 || token
->is_op(OPERATOR_DOT
)
543 || token
->is_string());
544 is_anonymous_pointer
= false;
545 this->unget_token(Token::make_identifier_token(id
, is_id_exported
,
550 go_error_at(this->location(), "expected field name");
551 this->gogo_
->mark_locals_used();
552 while (!token
->is_op(OPERATOR_SEMICOLON
)
553 && !token
->is_op(OPERATOR_RCURLY
)
555 token
= this->advance_token();
561 if (is_anonymous_pointer
)
563 this->advance_token();
564 if (!this->peek_token()->is_identifier())
566 go_error_at(this->location(), "expected field name");
567 this->gogo_
->mark_locals_used();
568 while (!token
->is_op(OPERATOR_SEMICOLON
)
569 && !token
->is_op(OPERATOR_RCURLY
)
571 token
= this->advance_token();
575 Type
* type
= this->type_name(true);
578 if (this->peek_token()->is_string())
580 tag
= this->peek_token()->string_value();
581 this->advance_token();
584 if (!type
->is_error_type())
586 if (is_anonymous_pointer
)
587 type
= Type::make_pointer_type(type
);
588 sfl
->push_back(Struct_field(Typed_identifier("", type
, location
)));
590 sfl
->back().set_tag(tag
);
595 Typed_identifier_list til
;
598 token
= this->peek_token();
599 if (!token
->is_identifier())
601 go_error_at(this->location(), "expected identifier");
605 this->gogo_
->pack_hidden_name(token
->identifier(),
606 token
->is_identifier_exported());
607 til
.push_back(Typed_identifier(name
, NULL
, token
->location()));
608 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
610 this->advance_token();
613 Type
* type
= this->type();
616 if (this->peek_token()->is_string())
618 tag
= this->peek_token()->string_value();
619 this->advance_token();
622 for (Typed_identifier_list::iterator p
= til
.begin();
627 sfl
->push_back(Struct_field(*p
));
629 sfl
->back().set_tag(tag
);
634 // PointerType = "*" Type .
637 Parse::pointer_type()
639 go_assert(this->peek_token()->is_op(OPERATOR_MULT
));
640 this->advance_token();
641 Type
* type
= this->type();
642 if (type
->is_error_type())
644 return Type::make_pointer_type(type
);
647 // ChannelType = Channel | SendChannel | RecvChannel .
648 // Channel = "chan" ElementType .
649 // SendChannel = "chan" "<-" ElementType .
650 // RecvChannel = "<-" "chan" ElementType .
653 Parse::channel_type()
655 const Token
* token
= this->peek_token();
658 if (token
->is_op(OPERATOR_CHANOP
))
660 if (!this->advance_token()->is_keyword(KEYWORD_CHAN
))
662 go_error_at(this->location(), "expected %<chan%>");
663 return Type::make_error_type();
666 this->advance_token();
670 go_assert(token
->is_keyword(KEYWORD_CHAN
));
671 if (this->advance_token()->is_op(OPERATOR_CHANOP
))
674 this->advance_token();
678 // Better error messages for the common error of omitting the
679 // channel element type.
680 if (!this->type_may_start_here())
682 token
= this->peek_token();
683 if (token
->is_op(OPERATOR_RCURLY
))
684 go_error_at(this->location(), "unexpected %<}%> in channel type");
685 else if (token
->is_op(OPERATOR_RPAREN
))
686 go_error_at(this->location(), "unexpected %<)%> in channel type");
687 else if (token
->is_op(OPERATOR_COMMA
))
688 go_error_at(this->location(), "unexpected comma in channel type");
690 go_error_at(this->location(), "expected channel element type");
691 return Type::make_error_type();
694 Type
* element_type
= this->type();
695 return Type::make_channel_type(send
, receive
, element_type
);
698 // Give an error for a duplicate parameter or receiver name.
701 Parse::check_signature_names(const Typed_identifier_list
* params
,
704 for (Typed_identifier_list::const_iterator p
= params
->begin();
708 if (p
->name().empty() || Gogo::is_sink_name(p
->name()))
710 std::pair
<std::string
, const Typed_identifier
*> val
=
711 std::make_pair(p
->name(), &*p
);
712 std::pair
<Parse::Names::iterator
, bool> ins
= names
->insert(val
);
715 go_error_at(p
->location(), "redefinition of %qs",
716 Gogo::message_name(p
->name()).c_str());
717 go_inform(ins
.first
->second
->location(),
718 "previous definition of %qs was here",
719 Gogo::message_name(p
->name()).c_str());
724 // Signature = Parameters [ Result ] .
726 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
727 // location of the start of the type.
729 // This returns NULL on a parse error.
732 Parse::signature(Typed_identifier
* receiver
, Location location
)
734 bool is_varargs
= false;
735 Typed_identifier_list
* params
;
736 bool params_ok
= this->parameters(¶ms
, &is_varargs
);
738 Typed_identifier_list
* results
= NULL
;
739 if (this->peek_token()->is_op(OPERATOR_LPAREN
)
740 || this->type_may_start_here())
742 if (!this->result(&results
))
750 if (receiver
!= NULL
)
751 names
[receiver
->name()] = receiver
;
753 this->check_signature_names(params
, &names
);
755 this->check_signature_names(results
, &names
);
757 Function_type
* ret
= Type::make_function_type(receiver
, params
, results
,
760 ret
->set_is_varargs();
764 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
766 // This returns false on a parse error.
769 Parse::parameters(Typed_identifier_list
** pparams
, bool* is_varargs
)
773 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
775 go_error_at(this->location(), "expected %<(%>");
779 Typed_identifier_list
* params
= NULL
;
780 bool saw_error
= false;
782 const Token
* token
= this->advance_token();
783 if (!token
->is_op(OPERATOR_RPAREN
))
785 params
= this->parameter_list(is_varargs
);
788 token
= this->peek_token();
791 // The optional trailing comma is picked up in parameter_list.
793 if (!token
->is_op(OPERATOR_RPAREN
))
795 go_error_at(this->location(), "expected %<)%>");
798 this->advance_token();
807 // ParameterList = ParameterDecl { "," ParameterDecl } .
809 // This sets *IS_VARARGS if the list ends with an ellipsis.
810 // IS_VARARGS will be NULL if varargs are not permitted.
812 // We pick up an optional trailing comma.
814 // This returns NULL if some error is seen.
816 Typed_identifier_list
*
817 Parse::parameter_list(bool* is_varargs
)
819 Location location
= this->location();
820 Typed_identifier_list
* ret
= new Typed_identifier_list();
822 bool saw_error
= false;
824 // If we see an identifier and then a comma, then we don't know
825 // whether we are looking at a list of identifiers followed by a
826 // type, or a list of types given by name. We have to do an
827 // arbitrary lookahead to figure it out.
829 bool parameters_have_names
;
830 const Token
* token
= this->peek_token();
831 if (!token
->is_identifier())
833 // This must be a type which starts with something like '*'.
834 parameters_have_names
= false;
838 std::string name
= token
->identifier();
839 bool is_exported
= token
->is_identifier_exported();
840 Location location
= token
->location();
841 token
= this->advance_token();
842 if (!token
->is_op(OPERATOR_COMMA
))
844 if (token
->is_op(OPERATOR_DOT
))
846 // This is a qualified identifier, which must turn out
848 parameters_have_names
= false;
850 else if (token
->is_op(OPERATOR_RPAREN
))
852 // A single identifier followed by a parenthesis must be
854 parameters_have_names
= false;
858 // An identifier followed by something other than a
859 // comma or a dot or a right parenthesis must be a
860 // parameter name followed by a type.
861 parameters_have_names
= true;
864 this->unget_token(Token::make_identifier_token(name
, is_exported
,
869 // An identifier followed by a comma may be the first in a
870 // list of parameter names followed by a type, or it may be
871 // the first in a list of types without parameter names. To
872 // find out we gather as many identifiers separated by
874 std::string id_name
= this->gogo_
->pack_hidden_name(name
,
876 ret
->push_back(Typed_identifier(id_name
, NULL
, location
));
877 bool just_saw_comma
= true;
878 while (this->advance_token()->is_identifier())
880 name
= this->peek_token()->identifier();
881 is_exported
= this->peek_token()->is_identifier_exported();
882 location
= this->peek_token()->location();
883 id_name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
884 ret
->push_back(Typed_identifier(id_name
, NULL
, location
));
885 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
887 just_saw_comma
= false;
894 // We saw ID1 "," ID2 "," followed by something which
895 // was not an identifier. We must be seeing the start
896 // of a type, and ID1 and ID2 must be types, and the
897 // parameters don't have names.
898 parameters_have_names
= false;
900 else if (this->peek_token()->is_op(OPERATOR_RPAREN
))
902 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
903 // and the parameters don't have names.
904 parameters_have_names
= false;
906 else if (this->peek_token()->is_op(OPERATOR_DOT
))
908 // We saw ID1 "," ID2 ".". ID2 must be a package name,
909 // ID1 must be a type, and the parameters don't have
911 parameters_have_names
= false;
912 this->unget_token(Token::make_identifier_token(name
, is_exported
,
915 just_saw_comma
= true;
919 // We saw ID1 "," ID2 followed by something other than
920 // ",", ".", or ")". We must be looking at the start of
921 // a type, and ID1 and ID2 must be parameter names.
922 parameters_have_names
= true;
925 if (parameters_have_names
)
927 go_assert(!just_saw_comma
);
928 // We have just seen ID1, ID2 xxx.
930 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
934 go_error_at(this->location(),
935 "%<...%> only permits one name");
937 this->advance_token();
940 for (size_t i
= 0; i
< ret
->size(); ++i
)
941 ret
->set_type(i
, type
);
942 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
943 return saw_error
? NULL
: ret
;
944 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
945 return saw_error
? NULL
: ret
;
949 Typed_identifier_list
* tret
= new Typed_identifier_list();
950 for (Typed_identifier_list::const_iterator p
= ret
->begin();
954 Named_object
* no
= this->gogo_
->lookup(p
->name(), NULL
);
957 no
= this->gogo_
->add_unknown_name(p
->name(),
961 type
= no
->type_value();
962 else if (no
->is_unknown() || no
->is_type_declaration())
963 type
= Type::make_forward_declaration(no
);
966 go_error_at(p
->location(), "expected %<%s%> to be a type",
967 Gogo::message_name(p
->name()).c_str());
969 type
= Type::make_error_type();
971 tret
->push_back(Typed_identifier("", type
, p
->location()));
976 || this->peek_token()->is_op(OPERATOR_RPAREN
))
977 return saw_error
? NULL
: ret
;
982 bool mix_error
= false;
983 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
985 while (this->peek_token()->is_op(OPERATOR_COMMA
))
987 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
989 if (is_varargs
!= NULL
&& *is_varargs
)
991 go_error_at(this->location(), "%<...%> must be last parameter");
994 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
999 go_error_at(location
, "invalid named/anonymous mix");
1010 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1013 Parse::parameter_decl(bool parameters_have_names
,
1014 Typed_identifier_list
* til
,
1019 if (!parameters_have_names
)
1022 Location location
= this->location();
1023 if (!this->peek_token()->is_identifier())
1025 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1026 type
= this->type();
1029 if (is_varargs
== NULL
)
1030 go_error_at(this->location(), "invalid use of %<...%>");
1033 this->advance_token();
1034 if (is_varargs
== NULL
1035 && this->peek_token()->is_op(OPERATOR_RPAREN
))
1036 type
= Type::make_error_type();
1039 Type
* element_type
= this->type();
1040 type
= Type::make_array_type(element_type
, NULL
);
1046 type
= this->type_name(false);
1047 if (type
->is_error_type()
1048 || (!this->peek_token()->is_op(OPERATOR_COMMA
)
1049 && !this->peek_token()->is_op(OPERATOR_RPAREN
)))
1052 while (!this->peek_token()->is_op(OPERATOR_COMMA
)
1053 && !this->peek_token()->is_op(OPERATOR_RPAREN
)
1054 && !this->peek_token()->is_eof())
1055 this->advance_token();
1058 if (!type
->is_error_type())
1059 til
->push_back(Typed_identifier("", type
, location
));
1065 size_t orig_count
= til
->size();
1066 if (this->peek_token()->is_identifier())
1067 this->identifier_list(til
);
1070 size_t new_count
= til
->size();
1073 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1074 type
= this->type();
1077 if (is_varargs
== NULL
)
1079 go_error_at(this->location(), "invalid use of %<...%>");
1082 else if (new_count
> orig_count
+ 1)
1084 go_error_at(this->location(), "%<...%> only permits one name");
1089 this->advance_token();
1090 Type
* element_type
= this->type();
1091 type
= Type::make_array_type(element_type
, NULL
);
1093 for (size_t i
= orig_count
; i
< new_count
; ++i
)
1094 til
->set_type(i
, type
);
1098 // Result = Parameters | Type .
1100 // This returns false on a parse error.
1103 Parse::result(Typed_identifier_list
** presults
)
1105 if (this->peek_token()->is_op(OPERATOR_LPAREN
))
1106 return this->parameters(presults
, NULL
);
1109 Location location
= this->location();
1110 Type
* type
= this->type();
1111 if (type
->is_error_type())
1116 Typed_identifier_list
* til
= new Typed_identifier_list();
1117 til
->push_back(Typed_identifier("", type
, location
));
1123 // Block = "{" [ StatementList ] "}" .
1125 // Returns the location of the closing brace.
1130 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
1132 Location loc
= this->location();
1133 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1134 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1135 go_error_at(loc
, "unexpected semicolon or newline before %<{%>");
1138 go_error_at(this->location(), "expected %<{%>");
1139 return Linemap::unknown_location();
1143 const Token
* token
= this->advance_token();
1145 if (!token
->is_op(OPERATOR_RCURLY
))
1147 this->statement_list();
1148 token
= this->peek_token();
1149 if (!token
->is_op(OPERATOR_RCURLY
))
1151 if (!token
->is_eof() || !saw_errors())
1152 go_error_at(this->location(), "expected %<}%>");
1154 this->gogo_
->mark_locals_used();
1156 // Skip ahead to the end of the block, in hopes of avoiding
1157 // lots of meaningless errors.
1158 Location ret
= token
->location();
1160 while (!token
->is_eof())
1162 if (token
->is_op(OPERATOR_LCURLY
))
1164 else if (token
->is_op(OPERATOR_RCURLY
))
1169 this->advance_token();
1173 token
= this->advance_token();
1174 ret
= token
->location();
1180 Location ret
= token
->location();
1181 this->advance_token();
1185 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1186 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1189 Parse::interface_type(bool record
)
1191 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE
));
1192 Location location
= this->location();
1194 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
1196 Location token_loc
= this->location();
1197 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1198 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1199 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
1202 go_error_at(this->location(), "expected %<{%>");
1203 return Type::make_error_type();
1206 this->advance_token();
1208 Typed_identifier_list
* methods
= new Typed_identifier_list();
1209 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1211 this->method_spec(methods
);
1212 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1214 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
1216 this->method_spec(methods
);
1218 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1220 go_error_at(this->location(), "expected %<}%>");
1221 while (!this->advance_token()->is_op(OPERATOR_RCURLY
))
1223 if (this->peek_token()->is_eof())
1224 return Type::make_error_type();
1228 this->advance_token();
1230 if (methods
->empty())
1236 Interface_type
* ret
;
1237 if (methods
== NULL
)
1238 ret
= Type::make_empty_interface_type(location
);
1240 ret
= Type::make_interface_type(methods
, location
);
1242 this->gogo_
->record_interface_type(ret
);
1246 // MethodSpec = MethodName Signature | InterfaceTypeName .
1247 // MethodName = identifier .
1248 // InterfaceTypeName = TypeName .
1251 Parse::method_spec(Typed_identifier_list
* methods
)
1253 const Token
* token
= this->peek_token();
1254 if (!token
->is_identifier())
1256 go_error_at(this->location(), "expected identifier");
1260 std::string name
= token
->identifier();
1261 bool is_exported
= token
->is_identifier_exported();
1262 Location location
= token
->location();
1264 if (this->advance_token()->is_op(OPERATOR_LPAREN
))
1266 // This is a MethodName.
1268 go_error_at(this->location(),
1269 "methods must have a unique non-blank name");
1270 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1271 Type
* type
= this->signature(NULL
, location
);
1274 methods
->push_back(Typed_identifier(name
, type
, location
));
1278 this->unget_token(Token::make_identifier_token(name
, is_exported
,
1280 Type
* type
= this->type_name(false);
1281 if (type
->is_error_type()
1282 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1283 && !this->peek_token()->is_op(OPERATOR_RCURLY
)))
1285 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1286 go_error_at(this->location(),
1287 "name list not allowed in interface type");
1289 go_error_at(location
, "expected signature or type name");
1290 this->gogo_
->mark_locals_used();
1291 token
= this->peek_token();
1292 while (!token
->is_eof()
1293 && !token
->is_op(OPERATOR_SEMICOLON
)
1294 && !token
->is_op(OPERATOR_RCURLY
))
1295 token
= this->advance_token();
1298 // This must be an interface type, but we can't check that now.
1299 // We check it and pull out the methods in
1300 // Interface_type::do_verify.
1301 methods
->push_back(Typed_identifier("", type
, location
));
1305 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1308 Parse::declaration()
1310 const Token
* token
= this->peek_token();
1312 unsigned int pragmas
= this->lex_
->get_and_clear_pragmas();
1314 && !token
->is_keyword(KEYWORD_FUNC
)
1315 && !token
->is_keyword(KEYWORD_TYPE
))
1316 go_warning_at(token
->location(), 0,
1317 "ignoring magic comment before non-function");
1319 if (token
->is_keyword(KEYWORD_CONST
))
1321 else if (token
->is_keyword(KEYWORD_TYPE
))
1322 this->type_decl(pragmas
);
1323 else if (token
->is_keyword(KEYWORD_VAR
))
1325 else if (token
->is_keyword(KEYWORD_FUNC
))
1326 this->function_decl(pragmas
);
1329 go_error_at(this->location(), "expected declaration");
1330 this->advance_token();
1335 Parse::declaration_may_start_here()
1337 const Token
* token
= this->peek_token();
1338 return (token
->is_keyword(KEYWORD_CONST
)
1339 || token
->is_keyword(KEYWORD_TYPE
)
1340 || token
->is_keyword(KEYWORD_VAR
)
1341 || token
->is_keyword(KEYWORD_FUNC
));
1344 // Decl<P> = P | "(" [ List<P> ] ")" .
1347 Parse::decl(void (Parse::*pfn
)(void*, unsigned int), void* varg
,
1348 unsigned int pragmas
)
1350 if (this->peek_token()->is_eof())
1353 go_error_at(this->location(), "unexpected end of file");
1357 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1358 (this->*pfn
)(varg
, pragmas
);
1362 go_warning_at(this->location(), 0,
1363 "ignoring magic //go:... comment before group");
1364 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1366 this->list(pfn
, varg
, true);
1367 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1369 go_error_at(this->location(), "missing %<)%>");
1370 while (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1372 if (this->peek_token()->is_eof())
1377 this->advance_token();
1381 // List<P> = P { ";" P } [ ";" ] .
1383 // In order to pick up the trailing semicolon we need to know what
1384 // might follow. This is either a '}' or a ')'.
1387 Parse::list(void (Parse::*pfn
)(void*, unsigned int), void* varg
,
1388 bool follow_is_paren
)
1390 (this->*pfn
)(varg
, 0);
1391 Operator follow
= follow_is_paren
? OPERATOR_RPAREN
: OPERATOR_RCURLY
;
1392 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1393 || this->peek_token()->is_op(OPERATOR_COMMA
))
1395 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1396 go_error_at(this->location(), "unexpected comma");
1397 if (this->advance_token()->is_op(follow
))
1399 (this->*pfn
)(varg
, 0);
1403 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1408 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST
));
1409 this->advance_token();
1412 Type
* last_type
= NULL
;
1413 Expression_list
* last_expr_list
= NULL
;
1415 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1416 this->const_spec(&last_type
, &last_expr_list
);
1419 this->advance_token();
1420 while (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1422 this->const_spec(&last_type
, &last_expr_list
);
1423 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1424 this->advance_token();
1425 else if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1427 go_error_at(this->location(),
1428 "expected %<;%> or %<)%> or newline");
1429 if (!this->skip_past_error(OPERATOR_RPAREN
))
1433 this->advance_token();
1436 if (last_expr_list
!= NULL
)
1437 delete last_expr_list
;
1440 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1443 Parse::const_spec(Type
** last_type
, Expression_list
** last_expr_list
)
1445 Typed_identifier_list til
;
1446 this->identifier_list(&til
);
1449 if (this->type_may_start_here())
1451 type
= this->type();
1453 *last_expr_list
= NULL
;
1456 Expression_list
*expr_list
;
1457 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1459 if (*last_expr_list
== NULL
)
1461 go_error_at(this->location(), "expected %<=%>");
1465 expr_list
= new Expression_list
;
1466 for (Expression_list::const_iterator p
= (*last_expr_list
)->begin();
1467 p
!= (*last_expr_list
)->end();
1469 expr_list
->push_back((*p
)->copy());
1473 this->advance_token();
1474 expr_list
= this->expression_list(NULL
, false, true);
1476 if (*last_expr_list
!= NULL
)
1477 delete *last_expr_list
;
1478 *last_expr_list
= expr_list
;
1481 Expression_list::const_iterator pe
= expr_list
->begin();
1482 for (Typed_identifier_list::iterator pi
= til
.begin();
1486 if (pe
== expr_list
->end())
1488 go_error_at(this->location(), "not enough initializers");
1494 if (!Gogo::is_sink_name(pi
->name()))
1495 this->gogo_
->add_constant(*pi
, *pe
, this->iota_value());
1500 snprintf(buf
, sizeof buf
, ".$sinkconst%d", count
);
1502 Typed_identifier
ti(std::string(buf
), type
, pi
->location());
1503 Named_object
* no
= this->gogo_
->add_constant(ti
, *pe
, this->iota_value());
1504 no
->const_value()->set_is_sink();
1507 if (pe
!= expr_list
->end())
1508 go_error_at(this->location(), "too many initializers");
1510 this->increment_iota();
1515 // TypeDecl = "type" Decl<TypeSpec> .
1518 Parse::type_decl(unsigned int pragmas
)
1520 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE
));
1521 this->advance_token();
1522 this->decl(&Parse::type_spec
, NULL
, pragmas
);
1525 // TypeSpec = identifier ["="] Type .
1528 Parse::type_spec(void*, unsigned int pragmas
)
1530 const Token
* token
= this->peek_token();
1531 if (!token
->is_identifier())
1533 go_error_at(this->location(), "expected identifier");
1536 std::string name
= token
->identifier();
1537 bool is_exported
= token
->is_identifier_exported();
1538 Location location
= token
->location();
1539 token
= this->advance_token();
1541 bool is_alias
= false;
1542 if (token
->is_op(OPERATOR_EQ
))
1545 token
= this->advance_token();
1548 // The scope of the type name starts at the point where the
1549 // identifier appears in the source code. We implement this by
1550 // declaring the type before we read the type definition.
1551 Named_object
* named_type
= NULL
;
1554 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1555 named_type
= this->gogo_
->declare_type(name
, location
);
1559 if (name
== "_" && token
->is_keyword(KEYWORD_INTERFACE
))
1561 // We call Parse::interface_type explicity here because we do not want
1562 // to record an interface with a blank type name.
1563 type
= this->interface_type(false);
1565 else if (!token
->is_op(OPERATOR_SEMICOLON
))
1566 type
= this->type();
1569 go_error_at(this->location(),
1570 "unexpected semicolon or newline in type declaration");
1571 type
= Type::make_error_type();
1572 this->advance_token();
1575 if (type
->is_error_type())
1577 this->gogo_
->mark_locals_used();
1578 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1579 && !this->peek_token()->is_eof())
1580 this->advance_token();
1585 if (named_type
->is_type_declaration())
1587 Type
* ftype
= type
->forwarded();
1588 if (ftype
->forward_declaration_type() != NULL
1589 && (ftype
->forward_declaration_type()->named_object()
1592 go_error_at(location
, "invalid recursive type");
1593 type
= Type::make_error_type();
1596 Named_type
* nt
= Type::make_named_type(named_type
, type
, location
);
1600 this->gogo_
->define_type(named_type
, nt
);
1601 go_assert(named_type
->package() == NULL
);
1603 if ((pragmas
& GOPRAGMA_NOTINHEAP
) != 0)
1605 nt
->set_not_in_heap();
1606 pragmas
&= ~GOPRAGMA_NOTINHEAP
;
1609 go_warning_at(location
, 0,
1610 "ignoring magic //go:... comment before type");
1614 // This will probably give a redefinition error.
1615 this->gogo_
->add_type(name
, type
, location
);
1620 // VarDecl = "var" Decl<VarSpec> .
1625 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR
));
1626 this->advance_token();
1627 this->decl(&Parse::var_spec
, NULL
, 0);
1630 // VarSpec = IdentifierList
1631 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1634 Parse::var_spec(void*, unsigned int pragmas
)
1637 go_warning_at(this->location(), 0,
1638 "ignoring magic //go:... comment before var");
1640 // Get the variable names.
1641 Typed_identifier_list til
;
1642 this->identifier_list(&til
);
1644 Location location
= this->location();
1647 Expression_list
* init
= NULL
;
1648 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1650 type
= this->type();
1651 if (type
->is_error_type())
1653 this->gogo_
->mark_locals_used();
1654 while (!this->peek_token()->is_op(OPERATOR_EQ
)
1655 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1656 && !this->peek_token()->is_eof())
1657 this->advance_token();
1659 if (this->peek_token()->is_op(OPERATOR_EQ
))
1661 this->advance_token();
1662 init
= this->expression_list(NULL
, false, true);
1667 this->advance_token();
1668 init
= this->expression_list(NULL
, false, true);
1671 this->init_vars(&til
, type
, init
, false, location
);
1677 // Create variables. TIL is a list of variable names. If TYPE is not
1678 // NULL, it is the type of all the variables. If INIT is not NULL, it
1679 // is an initializer list for the variables.
1682 Parse::init_vars(const Typed_identifier_list
* til
, Type
* type
,
1683 Expression_list
* init
, bool is_coloneq
,
1686 // Check for an initialization which can yield multiple values.
1687 if (init
!= NULL
&& init
->size() == 1 && til
->size() > 1)
1689 if (this->init_vars_from_call(til
, type
, *init
->begin(), is_coloneq
,
1692 if (this->init_vars_from_map(til
, type
, *init
->begin(), is_coloneq
,
1695 if (this->init_vars_from_receive(til
, type
, *init
->begin(), is_coloneq
,
1698 if (this->init_vars_from_type_guard(til
, type
, *init
->begin(),
1699 is_coloneq
, location
))
1703 if (init
!= NULL
&& init
->size() != til
->size())
1705 if (init
->empty() || !init
->front()->is_error_expression())
1706 go_error_at(location
, "wrong number of initializations");
1709 type
= Type::make_error_type();
1712 // Note that INIT was already parsed with the old name bindings, so
1713 // we don't have to worry that it will accidentally refer to the
1714 // newly declared variables. But we do have to worry about a mix of
1715 // newly declared variables and old variables if the old variables
1716 // appear in the initializations.
1718 Expression_list::const_iterator pexpr
;
1720 pexpr
= init
->begin();
1721 bool any_new
= false;
1722 Expression_list
* vars
= new Expression_list();
1723 Expression_list
* vals
= new Expression_list();
1724 for (Typed_identifier_list::const_iterator p
= til
->begin();
1729 go_assert(pexpr
!= init
->end());
1730 this->init_var(*p
, type
, init
== NULL
? NULL
: *pexpr
, is_coloneq
,
1731 false, &any_new
, vars
, vals
);
1736 go_assert(pexpr
== init
->end());
1737 if (is_coloneq
&& !any_new
)
1738 go_error_at(location
, "variables redeclared but no variable is new");
1739 this->finish_init_vars(vars
, vals
, location
);
1742 // See if we need to initialize a list of variables from a function
1743 // call. This returns true if we have set up the variables and the
1747 Parse::init_vars_from_call(const Typed_identifier_list
* vars
, Type
* type
,
1748 Expression
* expr
, bool is_coloneq
,
1751 Call_expression
* call
= expr
->call_expression();
1755 // This is a function call. We can't check here whether it returns
1756 // the right number of values, but it might. Declare the variables,
1757 // and then assign the results of the call to them.
1759 call
->set_expected_result_count(vars
->size());
1761 Named_object
* first_var
= NULL
;
1762 unsigned int index
= 0;
1763 bool any_new
= false;
1764 Expression_list
* ivars
= new Expression_list();
1765 Expression_list
* ivals
= new Expression_list();
1766 for (Typed_identifier_list::const_iterator pv
= vars
->begin();
1770 Expression
* init
= Expression::make_call_result(call
, index
);
1771 Named_object
* no
= this->init_var(*pv
, type
, init
, is_coloneq
, false,
1772 &any_new
, ivars
, ivals
);
1774 if (this->gogo_
->in_global_scope() && no
->is_variable())
1776 if (first_var
== NULL
)
1780 // If the current object is a redefinition of another object, we
1781 // might have already recorded the dependency relationship between
1782 // it and the first variable. Either way, an error will be
1783 // reported for the redefinition and we don't need to properly
1784 // record dependency information for an invalid program.
1785 if (no
->is_redefinition())
1788 // The subsequent vars have an implicit dependency on
1789 // the first one, so that everything gets initialized in
1790 // the right order and so that we detect cycles
1792 this->gogo_
->record_var_depends_on(no
->var_value(), first_var
);
1797 if (is_coloneq
&& !any_new
)
1798 go_error_at(location
, "variables redeclared but no variable is new");
1800 this->finish_init_vars(ivars
, ivals
, location
);
1805 // See if we need to initialize a pair of values from a map index
1806 // expression. This returns true if we have set up the variables and
1807 // the initialization.
1810 Parse::init_vars_from_map(const Typed_identifier_list
* vars
, Type
* type
,
1811 Expression
* expr
, bool is_coloneq
,
1814 Index_expression
* index
= expr
->index_expression();
1817 if (vars
->size() != 2)
1820 // This is an index which is being assigned to two variables. It
1821 // must be a map index. Declare the variables, and then assign the
1822 // results of the map index.
1823 bool any_new
= false;
1824 Typed_identifier_list::const_iterator p
= vars
->begin();
1825 Expression
* init
= type
== NULL
? index
: NULL
;
1826 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1827 type
== NULL
, &any_new
, NULL
, NULL
);
1828 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1829 val_no
->var_value()->set_type_from_init_tuple();
1830 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1833 Type
* var_type
= type
;
1834 if (var_type
== NULL
)
1835 var_type
= Type::lookup_bool_type();
1836 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1837 &any_new
, NULL
, NULL
);
1838 Expression
* present_var
= Expression::make_var_reference(no
, location
);
1840 if (is_coloneq
&& !any_new
)
1841 go_error_at(location
, "variables redeclared but no variable is new");
1843 Statement
* s
= Statement::make_tuple_map_assignment(val_var
, present_var
,
1846 if (!this->gogo_
->in_global_scope())
1847 this->gogo_
->add_statement(s
);
1848 else if (!val_no
->is_sink())
1850 if (val_no
->is_variable())
1851 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1853 else if (!no
->is_sink())
1855 if (no
->is_variable())
1856 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1860 // Execute the map index expression just so that we can fail if
1862 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1864 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1870 // See if we need to initialize a pair of values from a receive
1871 // expression. This returns true if we have set up the variables and
1872 // the initialization.
1875 Parse::init_vars_from_receive(const Typed_identifier_list
* vars
, Type
* type
,
1876 Expression
* expr
, bool is_coloneq
,
1879 Receive_expression
* receive
= expr
->receive_expression();
1880 if (receive
== NULL
)
1882 if (vars
->size() != 2)
1885 // This is a receive expression which is being assigned to two
1886 // variables. Declare the variables, and then assign the results of
1888 bool any_new
= false;
1889 Typed_identifier_list::const_iterator p
= vars
->begin();
1890 Expression
* init
= type
== NULL
? receive
: NULL
;
1891 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1892 type
== NULL
, &any_new
, NULL
, NULL
);
1893 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1894 val_no
->var_value()->set_type_from_init_tuple();
1895 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1898 Type
* var_type
= type
;
1899 if (var_type
== NULL
)
1900 var_type
= Type::lookup_bool_type();
1901 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1902 &any_new
, NULL
, NULL
);
1903 Expression
* received_var
= Expression::make_var_reference(no
, location
);
1905 if (is_coloneq
&& !any_new
)
1906 go_error_at(location
, "variables redeclared but no variable is new");
1908 Statement
* s
= Statement::make_tuple_receive_assignment(val_var
,
1913 if (!this->gogo_
->in_global_scope())
1914 this->gogo_
->add_statement(s
);
1915 else if (!val_no
->is_sink())
1917 if (val_no
->is_variable())
1918 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1920 else if (!no
->is_sink())
1922 if (no
->is_variable())
1923 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1927 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1929 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1935 // See if we need to initialize a pair of values from a type guard
1936 // expression. This returns true if we have set up the variables and
1937 // the initialization.
1940 Parse::init_vars_from_type_guard(const Typed_identifier_list
* vars
,
1941 Type
* type
, Expression
* expr
,
1942 bool is_coloneq
, Location location
)
1944 Type_guard_expression
* type_guard
= expr
->type_guard_expression();
1945 if (type_guard
== NULL
)
1947 if (vars
->size() != 2)
1950 // This is a type guard expression which is being assigned to two
1951 // variables. Declare the variables, and then assign the results of
1953 bool any_new
= false;
1954 Typed_identifier_list::const_iterator p
= vars
->begin();
1955 Type
* var_type
= type
;
1956 if (var_type
== NULL
)
1957 var_type
= type_guard
->type();
1958 Named_object
* val_no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1959 &any_new
, NULL
, NULL
);
1960 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1964 if (var_type
== NULL
)
1965 var_type
= Type::lookup_bool_type();
1966 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1967 &any_new
, NULL
, NULL
);
1968 Expression
* ok_var
= Expression::make_var_reference(no
, location
);
1970 Expression
* texpr
= type_guard
->expr();
1971 Type
* t
= type_guard
->type();
1972 Statement
* s
= Statement::make_tuple_type_guard_assignment(val_var
, ok_var
,
1976 if (is_coloneq
&& !any_new
)
1977 go_error_at(location
, "variables redeclared but no variable is new");
1979 if (!this->gogo_
->in_global_scope())
1980 this->gogo_
->add_statement(s
);
1981 else if (!val_no
->is_sink())
1983 if (val_no
->is_variable())
1984 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1986 else if (!no
->is_sink())
1988 if (no
->is_variable())
1989 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1993 Named_object
* dummy
= this->create_dummy_global(type
, NULL
, location
);
1994 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
2000 // Create a single variable. If IS_COLONEQ is true, we permit
2001 // redeclarations in the same block, and we set *IS_NEW when we find a
2002 // new variable which is not a redeclaration.
2005 Parse::init_var(const Typed_identifier
& tid
, Type
* type
, Expression
* init
,
2006 bool is_coloneq
, bool type_from_init
, bool* is_new
,
2007 Expression_list
* vars
, Expression_list
* vals
)
2009 Location location
= tid
.location();
2011 if (Gogo::is_sink_name(tid
.name()))
2013 if (!type_from_init
&& init
!= NULL
)
2015 if (this->gogo_
->in_global_scope())
2016 return this->create_dummy_global(type
, init
, location
);
2019 // Create a dummy variable so that we will check whether the
2020 // initializer can be assigned to the type.
2021 Variable
* var
= new Variable(type
, init
, false, false, false,
2026 snprintf(buf
, sizeof buf
, "sink$%d", count
);
2028 return this->gogo_
->add_variable(buf
, var
);
2032 this->gogo_
->add_type_to_verify(type
);
2033 return this->gogo_
->add_sink();
2038 Named_object
* no
= this->gogo_
->lookup_in_block(tid
.name());
2040 && (no
->is_variable() || no
->is_result_variable()))
2042 // INIT may be NULL even when IS_COLONEQ is true for cases
2043 // like v, ok := x.(int).
2044 if (!type_from_init
&& init
!= NULL
)
2046 go_assert(vars
!= NULL
&& vals
!= NULL
);
2047 vars
->push_back(Expression::make_var_reference(no
, location
));
2048 vals
->push_back(init
);
2054 Variable
* var
= new Variable(type
, init
, this->gogo_
->in_global_scope(),
2055 false, false, location
);
2056 Named_object
* no
= this->gogo_
->add_variable(tid
.name(), var
);
2057 if (!no
->is_variable())
2059 // The name is already defined, so we just gave an error.
2060 return this->gogo_
->add_sink();
2065 // Create a dummy global variable to force an initializer to be run in
2066 // the right place. This is used when a sink variable is initialized
2070 Parse::create_dummy_global(Type
* type
, Expression
* init
,
2073 if (type
== NULL
&& init
== NULL
)
2074 type
= Type::lookup_bool_type();
2075 Variable
* var
= new Variable(type
, init
, true, false, false, location
);
2078 snprintf(buf
, sizeof buf
, "_.%d", count
);
2080 return this->gogo_
->add_variable(buf
, var
);
2083 // Finish the variable initialization by executing any assignments to
2084 // existing variables when using :=. These must be done as a tuple
2085 // assignment in case of something like n, a, b := 1, b, a.
2088 Parse::finish_init_vars(Expression_list
* vars
, Expression_list
* vals
,
2096 else if (vars
->size() == 1)
2098 go_assert(!this->gogo_
->in_global_scope());
2099 this->gogo_
->add_statement(Statement::make_assignment(vars
->front(),
2107 go_assert(!this->gogo_
->in_global_scope());
2108 this->gogo_
->add_statement(Statement::make_tuple_assignment(vars
, vals
,
2113 // SimpleVarDecl = identifier ":=" Expression .
2115 // We've already seen the identifier.
2117 // FIXME: We also have to implement
2118 // IdentifierList ":=" ExpressionList
2119 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2120 // tuple assignments here as well.
2122 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2123 // side may be a composite literal.
2125 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2128 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2129 // guard (var := expr.("type") using the literal keyword "type").
2132 Parse::simple_var_decl_or_assignment(const std::string
& name
,
2134 bool may_be_composite_lit
,
2135 Range_clause
* p_range_clause
,
2136 Type_switch
* p_type_switch
)
2138 Typed_identifier_list til
;
2139 til
.push_back(Typed_identifier(name
, NULL
, location
));
2141 std::set
<std::string
> uniq_idents
;
2142 uniq_idents
.insert(name
);
2143 std::string dup_name
;
2146 // We've seen one identifier. If we see a comma now, this could be
2148 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2150 go_assert(p_type_switch
== NULL
);
2153 const Token
* token
= this->advance_token();
2154 if (!token
->is_identifier())
2157 std::string id
= token
->identifier();
2158 bool is_id_exported
= token
->is_identifier_exported();
2159 Location id_location
= token
->location();
2160 std::pair
<std::set
<std::string
>::iterator
, bool> ins
;
2162 token
= this->advance_token();
2163 if (!token
->is_op(OPERATOR_COMMA
))
2165 if (token
->is_op(OPERATOR_COLONEQ
))
2167 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2168 ins
= uniq_idents
.insert(id
);
2169 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2170 go_error_at(id_location
, "multiple assignments to %s",
2171 Gogo::message_name(id
).c_str());
2172 til
.push_back(Typed_identifier(id
, NULL
, location
));
2175 this->unget_token(Token::make_identifier_token(id
,
2181 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2182 ins
= uniq_idents
.insert(id
);
2183 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2185 dup_name
= Gogo::message_name(id
);
2186 dup_loc
= id_location
;
2188 til
.push_back(Typed_identifier(id
, NULL
, location
));
2191 // We have a comma separated list of identifiers in TIL. If the
2192 // next token is COLONEQ, then this is a simple var decl, and we
2193 // have the complete list of identifiers. If the next token is
2194 // not COLONEQ, then the only valid parse is a tuple assignment.
2195 // The list of identifiers we have so far is really a list of
2196 // expressions. There are more expressions following.
2198 if (!this->peek_token()->is_op(OPERATOR_COLONEQ
))
2200 Expression_list
* exprs
= new Expression_list
;
2201 for (Typed_identifier_list::const_iterator p
= til
.begin();
2204 exprs
->push_back(this->id_to_expression(p
->name(), p
->location(),
2207 Expression_list
* more_exprs
=
2208 this->expression_list(NULL
, true, may_be_composite_lit
);
2209 for (Expression_list::const_iterator p
= more_exprs
->begin();
2210 p
!= more_exprs
->end();
2212 exprs
->push_back(*p
);
2215 this->tuple_assignment(exprs
, may_be_composite_lit
, p_range_clause
);
2220 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ
));
2221 const Token
* token
= this->advance_token();
2223 if (!dup_name
.empty())
2224 go_error_at(dup_loc
, "multiple assignments to %s", dup_name
.c_str());
2226 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
2228 this->range_clause_decl(&til
, p_range_clause
);
2232 Expression_list
* init
;
2233 if (p_type_switch
== NULL
)
2234 init
= this->expression_list(NULL
, false, may_be_composite_lit
);
2237 bool is_type_switch
= false;
2238 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false,
2239 may_be_composite_lit
,
2240 &is_type_switch
, NULL
);
2243 p_type_switch
->found
= true;
2244 p_type_switch
->name
= name
;
2245 p_type_switch
->location
= location
;
2246 p_type_switch
->expr
= expr
;
2250 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
2252 init
= new Expression_list();
2253 init
->push_back(expr
);
2257 this->advance_token();
2258 init
= this->expression_list(expr
, false, may_be_composite_lit
);
2262 this->init_vars(&til
, NULL
, init
, true, location
);
2265 // FunctionDecl = "func" identifier Signature [ Block ] .
2266 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2268 // Deprecated gcc extension:
2269 // FunctionDecl = "func" identifier Signature
2270 // __asm__ "(" string_lit ")" .
2271 // This extension means a function whose real name is the identifier
2272 // inside the asm. This extension will be removed at some future
2273 // date. It has been replaced with //extern or //go:linkname comments.
2275 // PRAGMAS is a bitset of magic comments.
2278 Parse::function_decl(unsigned int pragmas
)
2280 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2281 Location location
= this->location();
2282 std::string extern_name
= this->lex_
->extern_name();
2283 const Token
* token
= this->advance_token();
2285 bool expected_receiver
= false;
2286 Typed_identifier
* rec
= NULL
;
2287 if (token
->is_op(OPERATOR_LPAREN
))
2289 expected_receiver
= true;
2290 rec
= this->receiver();
2291 token
= this->peek_token();
2294 if (!token
->is_identifier())
2296 go_error_at(this->location(), "expected function name");
2301 this->gogo_
->pack_hidden_name(token
->identifier(),
2302 token
->is_identifier_exported());
2304 this->advance_token();
2306 Function_type
* fntype
= this->signature(rec
, this->location());
2308 Named_object
* named_object
= NULL
;
2310 if (this->peek_token()->is_keyword(KEYWORD_ASM
))
2312 if (!this->advance_token()->is_op(OPERATOR_LPAREN
))
2314 go_error_at(this->location(), "expected %<(%>");
2317 token
= this->advance_token();
2318 if (!token
->is_string())
2320 go_error_at(this->location(), "expected string");
2323 std::string asm_name
= token
->string_value();
2324 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
2326 go_error_at(this->location(), "expected %<)%>");
2329 this->advance_token();
2330 if (!Gogo::is_sink_name(name
))
2332 named_object
= this->gogo_
->declare_function(name
, fntype
, location
);
2333 if (named_object
->is_function_declaration())
2334 named_object
->func_declaration_value()->set_asm_name(asm_name
);
2338 // Check for the easy error of a newline before the opening brace.
2339 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
2341 Location semi_loc
= this->location();
2342 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
2343 go_error_at(this->location(),
2344 "unexpected semicolon or newline before %<{%>");
2346 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
2358 { GOPRAGMA_NOINTERFACE
, "nointerface", false, false, true },
2359 { GOPRAGMA_NOESCAPE
, "noescape", true, false, false },
2360 { GOPRAGMA_NORACE
, "norace", false, true, true },
2361 { GOPRAGMA_NOSPLIT
, "nosplit", false, true, true },
2362 { GOPRAGMA_NOINLINE
, "noinline", false, true, true },
2363 { GOPRAGMA_SYSTEMSTACK
, "systemstack", false, true, true },
2364 { GOPRAGMA_NOWRITEBARRIER
, "nowritebarrier", false, true, true },
2365 { GOPRAGMA_NOWRITEBARRIERREC
, "nowritebarrierrec", false, true, true },
2366 { GOPRAGMA_CGOUNSAFEARGS
, "cgo_unsafe_args", false, true, true },
2367 { GOPRAGMA_UINTPTRESCAPES
, "uintptrescapes", true, true, true },
2370 bool is_decl
= !this->peek_token()->is_op(OPERATOR_LCURLY
);
2374 i
< sizeof(pragma_check
) / sizeof(pragma_check
[0]);
2377 if ((pragmas
& pragma_check
[i
].bit
) == 0)
2382 if (pragma_check
[i
].decl_ok
)
2384 go_warning_at(location
, 0,
2385 ("ignoring magic //go:%s comment "
2386 "before declaration"),
2387 pragma_check
[i
].name
);
2389 else if (rec
== NULL
)
2391 if (pragma_check
[i
].func_ok
)
2393 go_warning_at(location
, 0,
2394 ("ignoring magic //go:%s comment "
2395 "before function definition"),
2396 pragma_check
[i
].name
);
2400 if (pragma_check
[i
].method_ok
)
2402 go_warning_at(location
, 0,
2403 ("ignoring magic //go:%s comment "
2404 "before method definition"),
2405 pragma_check
[i
].name
);
2408 pragmas
&= ~ pragma_check
[i
].bit
;
2414 if (named_object
== NULL
)
2416 // Function declarations with the blank identifier as a name are
2417 // mostly ignored since they cannot be called. We make an object
2418 // for this declaration for type-checking purposes.
2419 if (Gogo::is_sink_name(name
))
2423 snprintf(buf
, sizeof buf
, ".$sinkfndecl%d", count
);
2425 name
= std::string(buf
);
2429 || (expected_receiver
&& rec
== NULL
))
2430 this->gogo_
->add_erroneous_name(name
);
2433 named_object
= this->gogo_
->declare_function(name
, fntype
,
2435 if (!extern_name
.empty()
2436 && named_object
->is_function_declaration())
2438 Function_declaration
* fd
=
2439 named_object
->func_declaration_value();
2440 fd
->set_asm_name(extern_name
);
2445 if (pragmas
!= 0 && named_object
->is_function_declaration())
2446 named_object
->func_declaration_value()->set_pragmas(pragmas
);
2450 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2453 fntype
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2454 this->is_erroneous_function_
= true;
2455 if (!Gogo::is_sink_name(name
))
2456 this->gogo_
->add_erroneous_name(name
);
2457 name
= this->gogo_
->pack_hidden_name("_", false);
2459 named_object
= this->gogo_
->start_function(name
, fntype
, true, location
);
2460 Location end_loc
= this->block();
2461 this->gogo_
->finish_function(end_loc
);
2464 && !this->is_erroneous_function_
2465 && named_object
->is_function())
2466 named_object
->func_value()->set_pragmas(pragmas
);
2467 this->is_erroneous_function_
= hold_is_erroneous_function
;
2471 // Receiver = Parameters .
2476 Location location
= this->location();
2477 Typed_identifier_list
* til
;
2478 if (!this->parameters(&til
, NULL
))
2480 else if (til
== NULL
|| til
->empty())
2482 go_error_at(location
, "method has no receiver");
2485 else if (til
->size() > 1)
2487 go_error_at(location
, "method has multiple receivers");
2491 return &til
->front();
2494 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2495 // Literal = BasicLit | CompositeLit | FunctionLit .
2496 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2498 // If MAY_BE_SINK is true, this operand may be "_".
2500 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2501 // if the entire expression is in parentheses.
2504 Parse::operand(bool may_be_sink
, bool* is_parenthesized
)
2506 const Token
* token
= this->peek_token();
2508 switch (token
->classification())
2510 case Token::TOKEN_IDENTIFIER
:
2512 Location location
= token
->location();
2513 std::string id
= token
->identifier();
2514 bool is_exported
= token
->is_identifier_exported();
2515 std::string packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2517 Named_object
* in_function
;
2518 Named_object
* named_object
= this->gogo_
->lookup(packed
, &in_function
);
2520 Package
* package
= NULL
;
2521 if (named_object
!= NULL
&& named_object
->is_package())
2523 if (!this->advance_token()->is_op(OPERATOR_DOT
)
2524 || !this->advance_token()->is_identifier())
2526 go_error_at(location
, "unexpected reference to package");
2527 return Expression::make_error(location
);
2529 package
= named_object
->package_value();
2530 package
->note_usage(id
);
2531 id
= this->peek_token()->identifier();
2532 is_exported
= this->peek_token()->is_identifier_exported();
2533 packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2534 named_object
= package
->lookup(packed
);
2535 location
= this->location();
2536 go_assert(in_function
== NULL
);
2539 this->advance_token();
2541 if (named_object
!= NULL
2542 && named_object
->is_type()
2543 && !named_object
->type_value()->is_visible())
2545 go_assert(package
!= NULL
);
2546 go_error_at(location
, "invalid reference to hidden type %<%s.%s%>",
2547 Gogo::message_name(package
->package_name()).c_str(),
2548 Gogo::message_name(id
).c_str());
2549 return Expression::make_error(location
);
2553 if (named_object
== NULL
)
2555 if (package
!= NULL
)
2557 std::string n1
= Gogo::message_name(package
->package_name());
2558 std::string n2
= Gogo::message_name(id
);
2560 go_error_at(location
,
2561 ("invalid reference to unexported identifier "
2563 n1
.c_str(), n2
.c_str());
2565 go_error_at(location
,
2566 "reference to undefined identifier %<%s.%s%>",
2567 n1
.c_str(), n2
.c_str());
2568 return Expression::make_error(location
);
2571 named_object
= this->gogo_
->add_unknown_name(packed
, location
);
2574 if (in_function
!= NULL
2575 && in_function
!= this->gogo_
->current_function()
2576 && (named_object
->is_variable()
2577 || named_object
->is_result_variable()))
2578 return this->enclosing_var_reference(in_function
, named_object
,
2579 may_be_sink
, location
);
2581 switch (named_object
->classification())
2583 case Named_object::NAMED_OBJECT_CONST
:
2584 return Expression::make_const_reference(named_object
, location
);
2585 case Named_object::NAMED_OBJECT_TYPE
:
2586 return Expression::make_type(named_object
->type_value(), location
);
2587 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
2589 Type
* t
= Type::make_forward_declaration(named_object
);
2590 return Expression::make_type(t
, location
);
2592 case Named_object::NAMED_OBJECT_VAR
:
2593 case Named_object::NAMED_OBJECT_RESULT_VAR
:
2594 // Any left-hand-side can be a sink, so if this can not be
2595 // a sink, then it must be a use of the variable.
2597 this->mark_var_used(named_object
);
2598 return Expression::make_var_reference(named_object
, location
);
2599 case Named_object::NAMED_OBJECT_SINK
:
2601 return Expression::make_sink(location
);
2604 go_error_at(location
, "cannot use _ as value");
2605 return Expression::make_error(location
);
2607 case Named_object::NAMED_OBJECT_FUNC
:
2608 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
2609 return Expression::make_func_reference(named_object
, NULL
,
2611 case Named_object::NAMED_OBJECT_UNKNOWN
:
2613 Unknown_expression
* ue
=
2614 Expression::make_unknown_reference(named_object
, location
);
2615 if (this->is_erroneous_function_
)
2616 ue
->set_no_error_message();
2619 case Named_object::NAMED_OBJECT_ERRONEOUS
:
2620 return Expression::make_error(location
);
2627 case Token::TOKEN_STRING
:
2628 ret
= Expression::make_string(token
->string_value(), token
->location());
2629 this->advance_token();
2632 case Token::TOKEN_CHARACTER
:
2633 ret
= Expression::make_character(token
->character_value(), NULL
,
2635 this->advance_token();
2638 case Token::TOKEN_INTEGER
:
2639 ret
= Expression::make_integer_z(token
->integer_value(), NULL
,
2641 this->advance_token();
2644 case Token::TOKEN_FLOAT
:
2645 ret
= Expression::make_float(token
->float_value(), NULL
,
2647 this->advance_token();
2650 case Token::TOKEN_IMAGINARY
:
2653 mpfr_init_set_ui(zero
, 0, GMP_RNDN
);
2655 mpc_init2(val
, mpc_precision
);
2656 mpc_set_fr_fr(val
, zero
, *token
->imaginary_value(), MPC_RNDNN
);
2658 ret
= Expression::make_complex(&val
, NULL
, token
->location());
2660 this->advance_token();
2664 case Token::TOKEN_KEYWORD
:
2665 switch (token
->keyword())
2668 return this->function_lit();
2670 case KEYWORD_INTERFACE
:
2672 case KEYWORD_STRUCT
:
2674 Location location
= token
->location();
2675 return Expression::make_type(this->type(), location
);
2682 case Token::TOKEN_OPERATOR
:
2683 if (token
->is_op(OPERATOR_LPAREN
))
2685 this->advance_token();
2686 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2688 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2689 go_error_at(this->location(), "missing %<)%>");
2691 this->advance_token();
2692 if (is_parenthesized
!= NULL
)
2693 *is_parenthesized
= true;
2696 else if (token
->is_op(OPERATOR_LSQUARE
))
2698 // Here we call array_type directly, as this is the only
2699 // case where an ellipsis is permitted for an array type.
2700 Location location
= token
->location();
2701 return Expression::make_type(this->array_type(true), location
);
2709 go_error_at(this->location(), "expected operand");
2710 return Expression::make_error(this->location());
2713 // Handle a reference to a variable in an enclosing function. We add
2714 // it to a list of such variables. We return a reference to a field
2715 // in a struct which will be passed on the static chain when calling
2716 // the current function.
2719 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2720 bool may_be_sink
, Location location
)
2722 go_assert(var
->is_variable() || var
->is_result_variable());
2724 // Any left-hand-side can be a sink, so if this can not be
2725 // a sink, then it must be a use of the variable.
2727 this->mark_var_used(var
);
2729 Named_object
* this_function
= this->gogo_
->current_function();
2730 Named_object
* closure
= this_function
->func_value()->closure_var();
2732 // The last argument to the Enclosing_var constructor is the index
2733 // of this variable in the closure. We add 1 to the current number
2734 // of enclosed variables, because the first field in the closure
2735 // points to the function code.
2736 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2737 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2738 this->enclosing_vars_
.insert(ev
);
2741 // This is a variable we have not seen before. Add a new field
2742 // to the closure type.
2743 this_function
->func_value()->add_closure_field(var
, location
);
2746 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2748 closure_ref
= Expression::make_unary(OPERATOR_MULT
, closure_ref
, location
);
2750 // The closure structure holds pointers to the variables, so we need
2751 // to introduce an indirection.
2752 Expression
* e
= Expression::make_field_reference(closure_ref
,
2755 e
= Expression::make_unary(OPERATOR_MULT
, e
, location
);
2756 return Expression::make_enclosing_var_reference(e
, var
, location
);
2759 // CompositeLit = LiteralType LiteralValue .
2760 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2761 // SliceType | MapType | TypeName .
2762 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2763 // ElementList = Element { "," Element } .
2764 // Element = [ Key ":" ] Value .
2765 // Key = FieldName | ElementIndex .
2766 // FieldName = identifier .
2767 // ElementIndex = Expression .
2768 // Value = Expression | LiteralValue .
2770 // We have already seen the type if there is one, and we are now
2771 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2772 // will be seen here as an array type whose length is "nil". The
2773 // DEPTH parameter is non-zero if this is an embedded composite
2774 // literal and the type was omitted. It gives the number of steps up
2775 // to the type which was provided. E.g., in [][]int{{1}} it will be
2776 // 1. In [][][]int{{{1}}} it will be 2.
2779 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2781 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2782 this->advance_token();
2784 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2786 this->advance_token();
2787 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2791 bool has_keys
= false;
2792 bool all_are_names
= true;
2793 Expression_list
* vals
= new Expression_list
;
2797 bool is_type_omitted
= false;
2798 bool is_name
= false;
2800 const Token
* token
= this->peek_token();
2802 if (token
->is_identifier())
2804 std::string identifier
= token
->identifier();
2805 bool is_exported
= token
->is_identifier_exported();
2806 Location location
= token
->location();
2808 if (this->advance_token()->is_op(OPERATOR_COLON
))
2810 // This may be a field name. We don't know for sure--it
2811 // could also be an expression for an array index. We
2812 // don't want to parse it as an expression because may
2813 // trigger various errors, e.g., if this identifier
2814 // happens to be the name of a package.
2815 Gogo
* gogo
= this->gogo_
;
2816 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2823 this->unget_token(Token::make_identifier_token(identifier
,
2826 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2830 else if (!token
->is_op(OPERATOR_LCURLY
))
2831 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2834 // This must be a composite literal inside another composite
2835 // literal, with the type omitted for the inner one.
2836 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2837 is_type_omitted
= true;
2840 token
= this->peek_token();
2841 if (!token
->is_op(OPERATOR_COLON
))
2844 vals
->push_back(NULL
);
2849 if (is_type_omitted
)
2851 // VAL is a nested composite literal with an omitted type being
2852 // used a key. Record this information in VAL so that the correct
2853 // type is associated with the literal value if VAL is a
2855 val
->complit()->update_key_path(depth
);
2858 this->advance_token();
2860 if (!has_keys
&& !vals
->empty())
2862 Expression_list
* newvals
= new Expression_list
;
2863 for (Expression_list::const_iterator p
= vals
->begin();
2867 newvals
->push_back(NULL
);
2868 newvals
->push_back(*p
);
2875 if (val
->unknown_expression() != NULL
)
2876 val
->unknown_expression()->set_is_composite_literal_key();
2878 vals
->push_back(val
);
2880 if (!token
->is_op(OPERATOR_LCURLY
))
2881 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2884 // This must be a composite literal inside another
2885 // composite literal, with the type omitted for the
2887 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2890 token
= this->peek_token();
2893 vals
->push_back(val
);
2896 all_are_names
= false;
2898 if (token
->is_op(OPERATOR_COMMA
))
2900 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2902 this->advance_token();
2906 else if (token
->is_op(OPERATOR_RCURLY
))
2908 this->advance_token();
2913 if (token
->is_op(OPERATOR_SEMICOLON
))
2914 go_error_at(this->location(),
2915 ("need trailing comma before newline "
2916 "in composite literal"));
2918 go_error_at(this->location(), "expected %<,%> or %<}%>");
2920 this->gogo_
->mark_locals_used();
2922 while (!token
->is_eof()
2923 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2925 if (token
->is_op(OPERATOR_LCURLY
))
2927 else if (token
->is_op(OPERATOR_RCURLY
))
2929 token
= this->advance_token();
2931 if (token
->is_op(OPERATOR_RCURLY
))
2932 this->advance_token();
2934 return Expression::make_error(location
);
2938 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2939 all_are_names
, location
);
2942 // FunctionLit = "func" Signature Block .
2945 Parse::function_lit()
2947 Location location
= this->location();
2948 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2949 this->advance_token();
2951 Enclosing_vars hold_enclosing_vars
;
2952 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2954 Function_type
* type
= this->signature(NULL
, location
);
2955 bool fntype_is_error
= false;
2958 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2959 fntype_is_error
= true;
2962 // For a function literal, the next token must be a '{'. If we
2963 // don't see that, then we may have a type expression.
2964 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2966 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2967 return Expression::make_type(type
, location
);
2970 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2971 if (fntype_is_error
)
2972 this->is_erroneous_function_
= true;
2974 Bc_stack
* hold_break_stack
= this->break_stack_
;
2975 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2976 this->break_stack_
= NULL
;
2977 this->continue_stack_
= NULL
;
2979 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2981 Location end_loc
= this->block();
2983 this->gogo_
->finish_function(end_loc
);
2985 if (this->break_stack_
!= NULL
)
2986 delete this->break_stack_
;
2987 if (this->continue_stack_
!= NULL
)
2988 delete this->continue_stack_
;
2989 this->break_stack_
= hold_break_stack
;
2990 this->continue_stack_
= hold_continue_stack
;
2992 this->is_erroneous_function_
= hold_is_erroneous_function
;
2994 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2996 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
2999 return Expression::make_func_reference(no
, closure
, location
);
3002 // Create a closure for the nested function FUNCTION. This is based
3003 // on ENCLOSING_VARS, which is a list of all variables defined in
3004 // enclosing functions and referenced from FUNCTION. A closure is the
3005 // address of a struct which point to the real function code and
3006 // contains the addresses of all the referenced variables. This
3007 // returns NULL if no closure is required.
3010 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
3013 if (enclosing_vars
->empty())
3016 // Get the variables in order by their field index.
3018 size_t enclosing_var_count
= enclosing_vars
->size();
3019 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
3020 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
3021 p
!= enclosing_vars
->end();
3024 // Subtract 1 because index 0 is the function code.
3025 ev
[p
->index() - 1] = *p
;
3028 // Build an initializer for a composite literal of the closure's
3031 Named_object
* enclosing_function
= this->gogo_
->current_function();
3032 Expression_list
* initializer
= new Expression_list
;
3034 initializer
->push_back(Expression::make_func_code_reference(function
,
3037 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
3039 // Add 1 to i because the first field in the closure is a
3040 // pointer to the function code.
3041 go_assert(ev
[i
].index() == i
+ 1);
3042 Named_object
* var
= ev
[i
].var();
3044 if (ev
[i
].in_function() == enclosing_function
)
3045 ref
= Expression::make_var_reference(var
, location
);
3047 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
3049 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
3051 initializer
->push_back(refaddr
);
3054 Named_object
* closure_var
= function
->func_value()->closure_var();
3055 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
3056 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
3059 // When compiling the runtime, closures do not escape. When escape
3060 // analysis becomes the default, and applies to closures, this
3061 // should be changed to make it an error if a closure escapes.
3062 if (this->gogo_
->compiling_runtime()
3063 && this->gogo_
->package_name() == "runtime")
3065 Temporary_statement
* ctemp
= Statement::make_temporary(st
, cv
, location
);
3066 this->gogo_
->add_statement(ctemp
);
3067 Expression
* ref
= Expression::make_temporary_reference(ctemp
, location
);
3068 Expression
* addr
= Expression::make_unary(OPERATOR_AND
, ref
, location
);
3069 addr
->unary_expression()->set_does_not_escape();
3073 return Expression::make_heap_expression(cv
, location
);
3076 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
3078 // If MAY_BE_SINK is true, this expression may be "_".
3080 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3083 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3084 // guard (var := expr.("type") using the literal keyword "type").
3086 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3087 // if the entire expression is in parentheses.
3090 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3091 bool* is_type_switch
, bool* is_parenthesized
)
3093 Location start_loc
= this->location();
3094 bool operand_is_parenthesized
= false;
3095 bool whole_is_parenthesized
= false;
3097 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
3099 whole_is_parenthesized
= operand_is_parenthesized
;
3101 // An unknown name followed by a curly brace must be a composite
3102 // literal, and the unknown name must be a type.
3103 if (may_be_composite_lit
3104 && !operand_is_parenthesized
3105 && ret
->unknown_expression() != NULL
3106 && this->peek_token()->is_op(OPERATOR_LCURLY
))
3108 Named_object
* no
= ret
->unknown_expression()->named_object();
3109 Type
* type
= Type::make_forward_declaration(no
);
3110 ret
= Expression::make_type(type
, ret
->location());
3113 // We handle composite literals and type casts here, as it is the
3114 // easiest way to handle types which are in parentheses, as in
3116 if (ret
->is_type_expression())
3118 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
3120 whole_is_parenthesized
= false;
3121 if (!may_be_composite_lit
)
3123 Type
* t
= ret
->type();
3124 if (t
->named_type() != NULL
3125 || t
->forward_declaration_type() != NULL
)
3126 go_error_at(start_loc
,
3127 _("parentheses required around this composite "
3128 "literal to avoid parsing ambiguity"));
3130 else if (operand_is_parenthesized
)
3131 go_error_at(start_loc
,
3132 "cannot parenthesize type in composite literal");
3133 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
3135 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
3137 whole_is_parenthesized
= false;
3138 Location loc
= this->location();
3139 this->advance_token();
3140 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
3142 if (this->peek_token()->is_op(OPERATOR_COMMA
))
3143 this->advance_token();
3144 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
3146 go_error_at(this->location(),
3147 "invalid use of %<...%> in type conversion");
3148 this->advance_token();
3150 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3151 go_error_at(this->location(), "expected %<)%>");
3153 this->advance_token();
3154 if (expr
->is_error_expression())
3158 Type
* t
= ret
->type();
3159 if (t
->classification() == Type::TYPE_ARRAY
3160 && t
->array_type()->length() != NULL
3161 && t
->array_type()->length()->is_nil_expression())
3163 go_error_at(ret
->location(),
3164 "use of %<[...]%> outside of array literal");
3165 ret
= Expression::make_error(loc
);
3168 ret
= Expression::make_cast(t
, expr
, loc
);
3175 const Token
* token
= this->peek_token();
3176 if (token
->is_op(OPERATOR_LPAREN
))
3178 whole_is_parenthesized
= false;
3179 ret
= this->call(this->verify_not_sink(ret
));
3181 else if (token
->is_op(OPERATOR_DOT
))
3183 whole_is_parenthesized
= false;
3184 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3185 if (is_type_switch
!= NULL
&& *is_type_switch
)
3188 else if (token
->is_op(OPERATOR_LSQUARE
))
3190 whole_is_parenthesized
= false;
3191 ret
= this->index(this->verify_not_sink(ret
));
3197 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3198 *is_parenthesized
= true;
3203 // Selector = "." identifier .
3204 // TypeGuard = "." "(" QualifiedIdent ")" .
3206 // Note that Operand can expand to QualifiedIdent, which contains a
3207 // ".". That is handled directly in operand when it sees a package
3210 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3211 // guard (var := expr.("type") using the literal keyword "type").
3214 Parse::selector(Expression
* left
, bool* is_type_switch
)
3216 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3217 Location location
= this->location();
3219 const Token
* token
= this->advance_token();
3220 if (token
->is_identifier())
3222 // This could be a field in a struct, or a method in an
3223 // interface, or a method associated with a type. We can't know
3224 // which until we have seen all the types.
3226 this->gogo_
->pack_hidden_name(token
->identifier(),
3227 token
->is_identifier_exported());
3228 if (token
->identifier() == "_")
3230 go_error_at(this->location(), "invalid use of %<_%>");
3231 name
= Gogo::erroneous_name();
3233 this->advance_token();
3234 return Expression::make_selector(left
, name
, location
);
3236 else if (token
->is_op(OPERATOR_LPAREN
))
3238 this->advance_token();
3240 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3241 type
= this->type();
3244 if (is_type_switch
!= NULL
)
3245 *is_type_switch
= true;
3248 go_error_at(this->location(),
3249 "use of %<.(type)%> outside type switch");
3250 type
= Type::make_error_type();
3252 this->advance_token();
3254 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3255 go_error_at(this->location(), "missing %<)%>");
3257 this->advance_token();
3258 if (is_type_switch
!= NULL
&& *is_type_switch
)
3260 return Expression::make_type_guard(left
, type
, location
);
3264 go_error_at(this->location(), "expected identifier or %<(%>");
3269 // Index = "[" Expression "]" .
3270 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3273 Parse::index(Expression
* expr
)
3275 Location location
= this->location();
3276 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3277 this->advance_token();
3280 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3281 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3283 start
= Expression::make_integer_ul(0, NULL
, location
);
3285 Expression
* end
= NULL
;
3286 if (this->peek_token()->is_op(OPERATOR_COLON
))
3288 // We use nil to indicate a missing high expression.
3289 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3290 end
= Expression::make_nil(this->location());
3291 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3293 go_error_at(this->location(),
3294 "middle index required in 3-index slice");
3295 end
= Expression::make_error(this->location());
3298 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3301 Expression
* cap
= NULL
;
3302 if (this->peek_token()->is_op(OPERATOR_COLON
))
3304 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3306 go_error_at(this->location(),
3307 "final index required in 3-index slice");
3308 cap
= Expression::make_error(this->location());
3311 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3313 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3314 go_error_at(this->location(), "missing %<]%>");
3316 this->advance_token();
3317 return Expression::make_index(expr
, start
, end
, cap
, location
);
3320 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3321 // ArgumentList = ExpressionList [ "..." ] .
3324 Parse::call(Expression
* func
)
3326 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3327 Expression_list
* args
= NULL
;
3328 bool is_varargs
= false;
3329 const Token
* token
= this->advance_token();
3330 if (!token
->is_op(OPERATOR_RPAREN
))
3332 args
= this->expression_list(NULL
, false, true);
3333 token
= this->peek_token();
3334 if (token
->is_op(OPERATOR_ELLIPSIS
))
3337 token
= this->advance_token();
3340 if (token
->is_op(OPERATOR_COMMA
))
3341 token
= this->advance_token();
3342 if (!token
->is_op(OPERATOR_RPAREN
))
3344 go_error_at(this->location(), "missing %<)%>");
3345 if (!this->skip_past_error(OPERATOR_RPAREN
))
3346 return Expression::make_error(this->location());
3348 this->advance_token();
3349 if (func
->is_error_expression())
3351 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3354 // Return an expression for a single unqualified identifier.
3357 Parse::id_to_expression(const std::string
& name
, Location location
,
3360 Named_object
* in_function
;
3361 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3362 if (named_object
== NULL
)
3363 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3365 if (in_function
!= NULL
3366 && in_function
!= this->gogo_
->current_function()
3367 && (named_object
->is_variable() || named_object
->is_result_variable()))
3368 return this->enclosing_var_reference(in_function
, named_object
, is_lhs
,
3371 switch (named_object
->classification())
3373 case Named_object::NAMED_OBJECT_CONST
:
3374 return Expression::make_const_reference(named_object
, location
);
3375 case Named_object::NAMED_OBJECT_VAR
:
3376 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3378 this->mark_var_used(named_object
);
3379 return Expression::make_var_reference(named_object
, location
);
3380 case Named_object::NAMED_OBJECT_SINK
:
3381 return Expression::make_sink(location
);
3382 case Named_object::NAMED_OBJECT_FUNC
:
3383 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3384 return Expression::make_func_reference(named_object
, NULL
, location
);
3385 case Named_object::NAMED_OBJECT_UNKNOWN
:
3387 Unknown_expression
* ue
=
3388 Expression::make_unknown_reference(named_object
, location
);
3389 if (this->is_erroneous_function_
)
3390 ue
->set_no_error_message();
3393 case Named_object::NAMED_OBJECT_PACKAGE
:
3394 case Named_object::NAMED_OBJECT_TYPE
:
3395 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3397 // These cases can arise for a field name in a composite
3398 // literal. Keep track of these as they might be fake uses of
3399 // the related package.
3400 Unknown_expression
* ue
=
3401 Expression::make_unknown_reference(named_object
, location
);
3402 if (named_object
->package() != NULL
)
3403 named_object
->package()->note_fake_usage(ue
);
3404 if (this->is_erroneous_function_
)
3405 ue
->set_no_error_message();
3408 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3409 return Expression::make_error(location
);
3411 go_error_at(this->location(), "unexpected type of identifier");
3412 return Expression::make_error(location
);
3416 // Expression = UnaryExpr { binary_op Expression } .
3418 // PRECEDENCE is the precedence of the current operator.
3420 // If MAY_BE_SINK is true, this expression may be "_".
3422 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3425 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3426 // guard (var := expr.("type") using the literal keyword "type").
3428 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3429 // if the entire expression is in parentheses.
3432 Parse::expression(Precedence precedence
, bool may_be_sink
,
3433 bool may_be_composite_lit
, bool* is_type_switch
,
3434 bool *is_parenthesized
)
3436 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3437 is_type_switch
, is_parenthesized
);
3441 if (is_type_switch
!= NULL
&& *is_type_switch
)
3444 const Token
* token
= this->peek_token();
3445 if (token
->classification() != Token::TOKEN_OPERATOR
)
3451 Precedence right_precedence
;
3452 switch (token
->op())
3455 right_precedence
= PRECEDENCE_OROR
;
3457 case OPERATOR_ANDAND
:
3458 right_precedence
= PRECEDENCE_ANDAND
;
3461 case OPERATOR_NOTEQ
:
3466 right_precedence
= PRECEDENCE_RELOP
;
3469 case OPERATOR_MINUS
:
3472 right_precedence
= PRECEDENCE_ADDOP
;
3477 case OPERATOR_LSHIFT
:
3478 case OPERATOR_RSHIFT
:
3480 case OPERATOR_BITCLEAR
:
3481 right_precedence
= PRECEDENCE_MULOP
;
3484 right_precedence
= PRECEDENCE_INVALID
;
3488 if (right_precedence
== PRECEDENCE_INVALID
)
3494 if (is_parenthesized
!= NULL
)
3495 *is_parenthesized
= false;
3497 Operator op
= token
->op();
3498 Location binop_location
= token
->location();
3500 if (precedence
>= right_precedence
)
3502 // We've already seen A * B, and we see + C. We want to
3503 // return so that A * B becomes a group.
3507 this->advance_token();
3509 left
= this->verify_not_sink(left
);
3510 Expression
* right
= this->expression(right_precedence
, false,
3511 may_be_composite_lit
,
3513 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3518 Parse::expression_may_start_here()
3520 const Token
* token
= this->peek_token();
3521 switch (token
->classification())
3523 case Token::TOKEN_INVALID
:
3524 case Token::TOKEN_EOF
:
3526 case Token::TOKEN_KEYWORD
:
3527 switch (token
->keyword())
3532 case KEYWORD_STRUCT
:
3533 case KEYWORD_INTERFACE
:
3538 case Token::TOKEN_IDENTIFIER
:
3540 case Token::TOKEN_STRING
:
3542 case Token::TOKEN_OPERATOR
:
3543 switch (token
->op())
3546 case OPERATOR_MINUS
:
3550 case OPERATOR_CHANOP
:
3552 case OPERATOR_LPAREN
:
3553 case OPERATOR_LSQUARE
:
3558 case Token::TOKEN_CHARACTER
:
3559 case Token::TOKEN_INTEGER
:
3560 case Token::TOKEN_FLOAT
:
3561 case Token::TOKEN_IMAGINARY
:
3568 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3570 // If MAY_BE_SINK is true, this expression may be "_".
3572 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3575 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3576 // guard (var := expr.("type") using the literal keyword "type").
3578 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3579 // if the entire expression is in parentheses.
3582 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3583 bool* is_type_switch
, bool* is_parenthesized
)
3585 const Token
* token
= this->peek_token();
3587 // There is a complex parse for <- chan. The choices are
3588 // Convert x to type <- chan int:
3590 // Receive from (x converted to type chan <- chan int):
3591 // (<- chan <- chan int (x))
3592 // Convert x to type <- chan (<- chan int).
3593 // (<- chan <- chan int)(x)
3594 if (token
->is_op(OPERATOR_CHANOP
))
3596 Location location
= token
->location();
3597 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3599 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3601 if (expr
->is_error_expression())
3603 else if (!expr
->is_type_expression())
3604 return Expression::make_receive(expr
, location
);
3607 if (expr
->type()->is_error_type())
3610 // We picked up "chan TYPE", but it is not a type
3612 Channel_type
* ct
= expr
->type()->channel_type();
3615 // This is probably impossible.
3616 go_error_at(location
, "expected channel type");
3617 return Expression::make_error(location
);
3619 else if (ct
->may_receive())
3622 Type
* t
= Type::make_channel_type(false, true,
3623 ct
->element_type());
3624 return Expression::make_type(t
, location
);
3628 // <- chan <- TYPE. Because we skipped the leading
3629 // <-, we parsed this as chan <- TYPE. With the
3630 // leading <-, we parse it as <- chan (<- TYPE).
3631 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3632 return Expression::make_type(t
, location
);
3637 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3638 token
= this->peek_token();
3641 if (token
->is_op(OPERATOR_PLUS
)
3642 || token
->is_op(OPERATOR_MINUS
)
3643 || token
->is_op(OPERATOR_NOT
)
3644 || token
->is_op(OPERATOR_XOR
)
3645 || token
->is_op(OPERATOR_CHANOP
)
3646 || token
->is_op(OPERATOR_MULT
)
3647 || token
->is_op(OPERATOR_AND
))
3649 Location location
= token
->location();
3650 Operator op
= token
->op();
3651 this->advance_token();
3653 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3655 if (expr
->is_error_expression())
3657 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3658 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3660 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3661 expr
= Expression::make_heap_expression(expr
, location
);
3662 else if (op
!= OPERATOR_CHANOP
)
3663 expr
= Expression::make_unary(op
, expr
, location
);
3665 expr
= Expression::make_receive(expr
, location
);
3669 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3670 is_type_switch
, is_parenthesized
);
3673 // This is called for the obscure case of
3674 // (<- chan <- chan int)(x)
3675 // In unary_expr we remove the leading <- and parse the remainder,
3677 // chan <- (chan int)
3678 // When we add the leading <- back in, we really want
3679 // <- chan (<- chan int)
3680 // This means that we need to reassociate.
3683 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3685 Channel_type
* ele
= ct
->element_type()->channel_type();
3688 go_error_at(location
, "parse error");
3689 return Type::make_error_type();
3692 if (ele
->may_send())
3693 sub
= Type::make_channel_type(false, true, ele
->element_type());
3695 sub
= this->reassociate_chan_direction(ele
, location
);
3696 return Type::make_channel_type(false, true, sub
);
3700 // Declaration | LabeledStmt | SimpleStmt |
3701 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3702 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3705 // LABEL is the label of this statement if it has one.
3708 Parse::statement(Label
* label
)
3710 const Token
* token
= this->peek_token();
3711 switch (token
->classification())
3713 case Token::TOKEN_KEYWORD
:
3715 switch (token
->keyword())
3720 this->declaration();
3724 case KEYWORD_STRUCT
:
3725 case KEYWORD_INTERFACE
:
3726 this->simple_stat(true, NULL
, NULL
, NULL
);
3730 this->go_or_defer_stat();
3732 case KEYWORD_RETURN
:
3733 this->return_stat();
3738 case KEYWORD_CONTINUE
:
3739 this->continue_stat();
3747 case KEYWORD_SWITCH
:
3748 this->switch_stat(label
);
3750 case KEYWORD_SELECT
:
3751 this->select_stat(label
);
3754 this->for_stat(label
);
3757 go_error_at(this->location(), "expected statement");
3758 this->advance_token();
3764 case Token::TOKEN_IDENTIFIER
:
3766 std::string identifier
= token
->identifier();
3767 bool is_exported
= token
->is_identifier_exported();
3768 Location location
= token
->location();
3769 if (this->advance_token()->is_op(OPERATOR_COLON
))
3771 this->advance_token();
3772 this->labeled_stmt(identifier
, location
);
3776 this->unget_token(Token::make_identifier_token(identifier
,
3779 this->simple_stat(true, NULL
, NULL
, NULL
);
3784 case Token::TOKEN_OPERATOR
:
3785 if (token
->is_op(OPERATOR_LCURLY
))
3787 Location location
= token
->location();
3788 this->gogo_
->start_block(location
);
3789 Location end_loc
= this->block();
3790 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3793 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3794 this->simple_stat(true, NULL
, NULL
, NULL
);
3797 case Token::TOKEN_STRING
:
3798 case Token::TOKEN_CHARACTER
:
3799 case Token::TOKEN_INTEGER
:
3800 case Token::TOKEN_FLOAT
:
3801 case Token::TOKEN_IMAGINARY
:
3802 this->simple_stat(true, NULL
, NULL
, NULL
);
3806 go_error_at(this->location(), "expected statement");
3807 this->advance_token();
3813 Parse::statement_may_start_here()
3815 const Token
* token
= this->peek_token();
3816 switch (token
->classification())
3818 case Token::TOKEN_KEYWORD
:
3820 switch (token
->keyword())
3827 case KEYWORD_STRUCT
:
3828 case KEYWORD_INTERFACE
:
3831 case KEYWORD_RETURN
:
3833 case KEYWORD_CONTINUE
:
3836 case KEYWORD_SWITCH
:
3837 case KEYWORD_SELECT
:
3847 case Token::TOKEN_IDENTIFIER
:
3850 case Token::TOKEN_OPERATOR
:
3851 if (token
->is_op(OPERATOR_LCURLY
)
3852 || token
->is_op(OPERATOR_SEMICOLON
))
3855 return this->expression_may_start_here();
3857 case Token::TOKEN_STRING
:
3858 case Token::TOKEN_CHARACTER
:
3859 case Token::TOKEN_INTEGER
:
3860 case Token::TOKEN_FLOAT
:
3861 case Token::TOKEN_IMAGINARY
:
3869 // LabeledStmt = Label ":" Statement .
3870 // Label = identifier .
3873 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3875 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3877 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3879 // This is a label at the end of a block. A program is
3880 // permitted to omit a semicolon here.
3884 if (!this->statement_may_start_here())
3886 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
3888 // We don't treat the fallthrough keyword as a statement,
3889 // because it can't appear most places where a statement is
3890 // permitted, but it may have a label. We introduce a
3891 // semicolon because the caller expects to see a statement.
3892 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3897 // Mark the label as used to avoid a useless error about an
3900 label
->set_is_used();
3902 go_error_at(location
, "missing statement after label");
3903 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3908 this->statement(label
);
3911 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3912 // Assignment | ShortVarDecl .
3914 // EmptyStmt was handled in Parse::statement.
3916 // In order to make this work for if and switch statements, if
3917 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3918 // expression rather than adding an expression statement to the
3919 // current block. If we see something other than an ExpressionStat,
3920 // we add the statement, set *RETURN_EXP to true if we saw a send
3921 // statement, and return NULL. The handling of send statements is for
3922 // better error messages.
3924 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3927 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3928 // guard (var := expr.("type") using the literal keyword "type").
3931 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3932 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3934 const Token
* token
= this->peek_token();
3936 // An identifier follow by := is a SimpleVarDecl.
3937 if (token
->is_identifier())
3939 std::string identifier
= token
->identifier();
3940 bool is_exported
= token
->is_identifier_exported();
3941 Location location
= token
->location();
3943 token
= this->advance_token();
3944 if (token
->is_op(OPERATOR_COLONEQ
)
3945 || token
->is_op(OPERATOR_COMMA
))
3947 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3948 this->simple_var_decl_or_assignment(identifier
, location
,
3949 may_be_composite_lit
,
3951 (token
->is_op(OPERATOR_COLONEQ
)
3957 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3960 else if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
3962 Typed_identifier_list til
;
3963 this->range_clause_decl(&til
, p_range_clause
);
3967 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3968 may_be_composite_lit
,
3969 (p_type_switch
== NULL
3971 : &p_type_switch
->found
),
3973 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3975 p_type_switch
->name
.clear();
3976 p_type_switch
->location
= exp
->location();
3977 p_type_switch
->expr
= this->verify_not_sink(exp
);
3980 token
= this->peek_token();
3981 if (token
->is_op(OPERATOR_CHANOP
))
3983 this->send_stmt(this->verify_not_sink(exp
), may_be_composite_lit
);
3984 if (return_exp
!= NULL
)
3987 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3988 || token
->is_op(OPERATOR_MINUSMINUS
))
3989 this->inc_dec_stat(this->verify_not_sink(exp
));
3990 else if (token
->is_op(OPERATOR_COMMA
)
3991 || token
->is_op(OPERATOR_EQ
))
3992 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3993 else if (token
->is_op(OPERATOR_PLUSEQ
)
3994 || token
->is_op(OPERATOR_MINUSEQ
)
3995 || token
->is_op(OPERATOR_OREQ
)
3996 || token
->is_op(OPERATOR_XOREQ
)
3997 || token
->is_op(OPERATOR_MULTEQ
)
3998 || token
->is_op(OPERATOR_DIVEQ
)
3999 || token
->is_op(OPERATOR_MODEQ
)
4000 || token
->is_op(OPERATOR_LSHIFTEQ
)
4001 || token
->is_op(OPERATOR_RSHIFTEQ
)
4002 || token
->is_op(OPERATOR_ANDEQ
)
4003 || token
->is_op(OPERATOR_BITCLEAREQ
))
4004 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
4006 else if (return_exp
!= NULL
)
4007 return this->verify_not_sink(exp
);
4010 exp
= this->verify_not_sink(exp
);
4012 if (token
->is_op(OPERATOR_COLONEQ
))
4014 if (!exp
->is_error_expression())
4015 go_error_at(token
->location(), "non-name on left side of %<:=%>");
4016 this->gogo_
->mark_locals_used();
4017 while (!token
->is_op(OPERATOR_SEMICOLON
)
4018 && !token
->is_eof())
4019 token
= this->advance_token();
4023 this->expression_stat(exp
);
4030 Parse::simple_stat_may_start_here()
4032 return this->expression_may_start_here();
4035 // Parse { Statement ";" } which is used in a few places. The list of
4036 // statements may end with a right curly brace, in which case the
4037 // semicolon may be omitted.
4040 Parse::statement_list()
4042 while (this->statement_may_start_here())
4044 this->statement(NULL
);
4045 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4046 this->advance_token();
4047 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4051 if (!this->peek_token()->is_eof() || !saw_errors())
4052 go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
4053 if (!this->skip_past_error(OPERATOR_RCURLY
))
4060 Parse::statement_list_may_start_here()
4062 return this->statement_may_start_here();
4065 // ExpressionStat = Expression .
4068 Parse::expression_stat(Expression
* exp
)
4070 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
4073 // SendStmt = Channel "<-" Expression .
4074 // Channel = Expression .
4077 Parse::send_stmt(Expression
* channel
, bool may_be_composite_lit
)
4079 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
4080 Location loc
= this->location();
4081 this->advance_token();
4082 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false,
4083 may_be_composite_lit
, NULL
, NULL
);
4084 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
4085 this->gogo_
->add_statement(s
);
4088 // IncDecStat = Expression ( "++" | "--" ) .
4091 Parse::inc_dec_stat(Expression
* exp
)
4093 const Token
* token
= this->peek_token();
4094 if (token
->is_op(OPERATOR_PLUSPLUS
))
4095 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
4096 else if (token
->is_op(OPERATOR_MINUSMINUS
))
4097 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
4100 this->advance_token();
4103 // Assignment = ExpressionList assign_op ExpressionList .
4105 // EXP is an expression that we have already parsed.
4107 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4108 // side may be a composite literal.
4110 // If RANGE_CLAUSE is not NULL, then this will recognize a
4114 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
4115 Range_clause
* p_range_clause
)
4117 Expression_list
* vars
;
4118 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4120 vars
= new Expression_list();
4121 vars
->push_back(expr
);
4125 this->advance_token();
4126 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
4129 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
4132 // An assignment statement. LHS is the list of expressions which
4133 // appear on the left hand side.
4135 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4136 // side may be a composite literal.
4138 // If RANGE_CLAUSE is not NULL, then this will recognize a
4142 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
4143 Range_clause
* p_range_clause
)
4145 const Token
* token
= this->peek_token();
4146 if (!token
->is_op(OPERATOR_EQ
)
4147 && !token
->is_op(OPERATOR_PLUSEQ
)
4148 && !token
->is_op(OPERATOR_MINUSEQ
)
4149 && !token
->is_op(OPERATOR_OREQ
)
4150 && !token
->is_op(OPERATOR_XOREQ
)
4151 && !token
->is_op(OPERATOR_MULTEQ
)
4152 && !token
->is_op(OPERATOR_DIVEQ
)
4153 && !token
->is_op(OPERATOR_MODEQ
)
4154 && !token
->is_op(OPERATOR_LSHIFTEQ
)
4155 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4156 && !token
->is_op(OPERATOR_ANDEQ
)
4157 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4159 go_error_at(this->location(), "expected assignment operator");
4162 Operator op
= token
->op();
4163 Location location
= token
->location();
4165 token
= this->advance_token();
4170 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4172 if (op
!= OPERATOR_EQ
)
4173 go_error_at(this->location(), "range clause requires %<=%>");
4174 this->range_clause_expr(lhs
, p_range_clause
);
4178 Expression_list
* vals
= this->expression_list(NULL
, false,
4179 may_be_composite_lit
);
4181 // We've parsed everything; check for errors.
4184 for (Expression_list::const_iterator pe
= lhs
->begin();
4188 if ((*pe
)->is_error_expression())
4190 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4191 go_error_at((*pe
)->location(), "cannot use _ as value");
4193 for (Expression_list::const_iterator pe
= vals
->begin();
4197 if ((*pe
)->is_error_expression())
4201 Call_expression
* call
;
4202 Index_expression
* map_index
;
4203 Receive_expression
* receive
;
4204 Type_guard_expression
* type_guard
;
4205 if (lhs
->size() == vals
->size())
4208 if (lhs
->size() > 1)
4210 if (op
!= OPERATOR_EQ
)
4211 go_error_at(location
, "multiple values only permitted with %<=%>");
4212 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4216 if (op
== OPERATOR_EQ
)
4217 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4220 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4221 vals
->front(), location
);
4225 this->gogo_
->add_statement(s
);
4227 else if (vals
->size() == 1
4228 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4230 if (op
!= OPERATOR_EQ
)
4231 go_error_at(location
, "multiple results only permitted with %<=%>");
4232 call
->set_expected_result_count(lhs
->size());
4234 vals
= new Expression_list
;
4235 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4236 vals
->push_back(Expression::make_call_result(call
, i
));
4237 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4238 this->gogo_
->add_statement(s
);
4240 else if (lhs
->size() == 2
4241 && vals
->size() == 1
4242 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4244 if (op
!= OPERATOR_EQ
)
4245 go_error_at(location
, "two values from map requires %<=%>");
4246 Expression
* val
= lhs
->front();
4247 Expression
* present
= lhs
->back();
4248 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4249 map_index
, location
);
4250 this->gogo_
->add_statement(s
);
4252 else if (lhs
->size() == 2
4253 && vals
->size() == 1
4254 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4256 if (op
!= OPERATOR_EQ
)
4257 go_error_at(location
, "two values from receive requires %<=%>");
4258 Expression
* val
= lhs
->front();
4259 Expression
* success
= lhs
->back();
4260 Expression
* channel
= receive
->channel();
4261 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4264 this->gogo_
->add_statement(s
);
4266 else if (lhs
->size() == 2
4267 && vals
->size() == 1
4268 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4270 if (op
!= OPERATOR_EQ
)
4271 go_error_at(location
, "two values from type guard requires %<=%>");
4272 Expression
* val
= lhs
->front();
4273 Expression
* ok
= lhs
->back();
4274 Expression
* expr
= type_guard
->expr();
4275 Type
* type
= type_guard
->type();
4276 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4279 this->gogo_
->add_statement(s
);
4283 go_error_at(location
, ("number of variables does not "
4284 "match number of values"));
4288 // GoStat = "go" Expression .
4289 // DeferStat = "defer" Expression .
4292 Parse::go_or_defer_stat()
4294 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4295 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4296 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4297 Location stat_location
= this->location();
4299 this->advance_token();
4300 Location expr_location
= this->location();
4302 bool is_parenthesized
= false;
4303 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4305 Call_expression
* call_expr
= expr
->call_expression();
4306 if (is_parenthesized
|| call_expr
== NULL
)
4308 go_error_at(expr_location
, "argument to go/defer must be function call");
4312 // Make it easier to simplify go/defer statements by putting every
4313 // statement in its own block.
4314 this->gogo_
->start_block(stat_location
);
4317 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4319 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4320 this->gogo_
->add_statement(stat
);
4321 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4325 // ReturnStat = "return" [ ExpressionList ] .
4328 Parse::return_stat()
4330 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4331 Location location
= this->location();
4332 this->advance_token();
4333 Expression_list
* vals
= NULL
;
4334 if (this->expression_may_start_here())
4335 vals
= this->expression_list(NULL
, false, true);
4336 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4339 && this->gogo_
->current_function()->func_value()->results_are_named())
4341 Named_object
* function
= this->gogo_
->current_function();
4342 Function::Results
* results
= function
->func_value()->result_variables();
4343 for (Function::Results::const_iterator p
= results
->begin();
4344 p
!= results
->end();
4347 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4349 go_assert(saw_errors());
4350 else if (!no
->is_result_variable())
4351 go_error_at(location
, "%qs is shadowed during return",
4352 (*p
)->message_name().c_str());
4357 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4358 // [ "else" ( IfStmt | Block ) ] .
4363 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4364 Location location
= this->location();
4365 this->advance_token();
4367 this->gogo_
->start_block(location
);
4369 bool saw_simple_stat
= false;
4370 Expression
* cond
= NULL
;
4371 bool saw_send_stmt
= false;
4372 if (this->simple_stat_may_start_here())
4374 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4375 saw_simple_stat
= true;
4377 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4379 // The SimpleStat is an expression statement.
4380 this->expression_stat(cond
);
4385 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4386 this->advance_token();
4387 else if (saw_simple_stat
)
4390 go_error_at(this->location(),
4391 ("send statement used as value; "
4392 "use select for non-blocking send"));
4394 go_error_at(this->location(),
4395 "expected %<;%> after statement in if expression");
4396 if (!this->expression_may_start_here())
4397 cond
= Expression::make_error(this->location());
4399 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4401 go_error_at(this->location(),
4402 "missing condition in if statement");
4403 cond
= Expression::make_error(this->location());
4406 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4409 // Check for the easy error of a newline before starting the block.
4410 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4412 Location semi_loc
= this->location();
4413 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4414 go_error_at(semi_loc
, "missing %<{%> after if clause");
4415 // Otherwise we will get an error when we call this->block
4419 this->gogo_
->start_block(this->location());
4420 Location end_loc
= this->block();
4421 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4423 // Check for the easy error of a newline before "else".
4424 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4426 Location semi_loc
= this->location();
4427 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4428 go_error_at(this->location(),
4429 "unexpected semicolon or newline before %<else%>");
4431 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4435 Block
* else_block
= NULL
;
4436 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4438 this->gogo_
->start_block(this->location());
4439 const Token
* token
= this->advance_token();
4440 if (token
->is_keyword(KEYWORD_IF
))
4442 else if (token
->is_op(OPERATOR_LCURLY
))
4446 go_error_at(this->location(), "expected %<if%> or %<{%>");
4447 this->statement(NULL
);
4449 else_block
= this->gogo_
->finish_block(this->location());
4452 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4456 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4460 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4461 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4462 // "{" { ExprCaseClause } "}" .
4463 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4464 // "{" { TypeCaseClause } "}" .
4465 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4468 Parse::switch_stat(Label
* label
)
4470 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4471 Location location
= this->location();
4472 this->advance_token();
4474 this->gogo_
->start_block(location
);
4476 bool saw_simple_stat
= false;
4477 Expression
* switch_val
= NULL
;
4479 Type_switch type_switch
;
4480 bool have_type_switch_block
= false;
4481 if (this->simple_stat_may_start_here())
4483 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4485 saw_simple_stat
= true;
4487 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4489 // The SimpleStat is an expression statement.
4490 this->expression_stat(switch_val
);
4493 if (switch_val
== NULL
&& !type_switch
.found
)
4495 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4496 this->advance_token();
4497 else if (saw_simple_stat
)
4500 go_error_at(this->location(),
4501 ("send statement used as value; "
4502 "use select for non-blocking send"));
4504 go_error_at(this->location(),
4505 "expected %<;%> after statement in switch expression");
4507 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4509 if (this->peek_token()->is_identifier())
4511 const Token
* token
= this->peek_token();
4512 std::string identifier
= token
->identifier();
4513 bool is_exported
= token
->is_identifier_exported();
4514 Location id_loc
= token
->location();
4516 token
= this->advance_token();
4517 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4518 this->unget_token(Token::make_identifier_token(identifier
,
4523 // This must be a TypeSwitchGuard. It is in a
4524 // different block from any initial SimpleStat.
4525 if (saw_simple_stat
)
4527 this->gogo_
->start_block(id_loc
);
4528 have_type_switch_block
= true;
4531 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4533 if (!type_switch
.found
)
4535 if (switch_val
== NULL
4536 || !switch_val
->is_error_expression())
4539 "expected type switch assignment");
4540 switch_val
= Expression::make_error(id_loc
);
4545 if (switch_val
== NULL
&& !type_switch
.found
)
4547 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4548 &type_switch
.found
, NULL
);
4549 if (type_switch
.found
)
4551 type_switch
.name
.clear();
4552 type_switch
.expr
= switch_val
;
4553 type_switch
.location
= switch_val
->location();
4559 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4561 Location token_loc
= this->location();
4562 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4563 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4564 go_error_at(token_loc
, "missing %<{%> after switch clause");
4565 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4567 go_error_at(token_loc
, "invalid variable name");
4568 this->advance_token();
4569 this->expression(PRECEDENCE_NORMAL
, false, false,
4570 &type_switch
.found
, NULL
);
4571 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4572 this->advance_token();
4573 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4575 if (have_type_switch_block
)
4576 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4578 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4582 if (type_switch
.found
)
4583 type_switch
.expr
= Expression::make_error(location
);
4587 go_error_at(this->location(), "expected %<{%>");
4588 if (have_type_switch_block
)
4589 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4591 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4596 this->advance_token();
4598 Statement
* statement
;
4599 if (type_switch
.found
)
4600 statement
= this->type_switch_body(label
, type_switch
, location
);
4602 statement
= this->expr_switch_body(label
, switch_val
, location
);
4604 if (statement
!= NULL
)
4605 this->gogo_
->add_statement(statement
);
4607 if (have_type_switch_block
)
4608 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4611 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4615 // The body of an expression switch.
4616 // "{" { ExprCaseClause } "}"
4619 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4622 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4625 this->push_break_statement(statement
, label
);
4627 Case_clauses
* case_clauses
= new Case_clauses();
4628 bool saw_default
= false;
4629 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4631 if (this->peek_token()->is_eof())
4634 go_error_at(this->location(), "missing %<}%>");
4637 this->expr_case_clause(case_clauses
, &saw_default
);
4639 this->advance_token();
4641 statement
->add_clauses(case_clauses
);
4643 this->pop_break_statement();
4648 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4649 // FallthroughStat = "fallthrough" .
4652 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4654 Location location
= this->location();
4656 bool is_default
= false;
4657 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4659 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4662 go_error_at(this->location(), "expected %<:%>");
4666 this->advance_token();
4668 Block
* statements
= NULL
;
4669 if (this->statement_list_may_start_here())
4671 this->gogo_
->start_block(this->location());
4672 this->statement_list();
4673 statements
= this->gogo_
->finish_block(this->location());
4676 bool is_fallthrough
= false;
4677 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4679 Location fallthrough_loc
= this->location();
4680 is_fallthrough
= true;
4681 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4682 this->advance_token();
4683 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4684 go_error_at(fallthrough_loc
,
4685 _("cannot fallthrough final case in switch"));
4692 go_error_at(location
, "multiple defaults in switch");
4695 *saw_default
= true;
4698 if (is_default
|| vals
!= NULL
)
4699 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4702 // ExprSwitchCase = "case" ExpressionList | "default" .
4705 Parse::expr_switch_case(bool* is_default
)
4707 const Token
* token
= this->peek_token();
4708 if (token
->is_keyword(KEYWORD_CASE
))
4710 this->advance_token();
4711 return this->expression_list(NULL
, false, true);
4713 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4715 this->advance_token();
4722 go_error_at(this->location(), "expected %<case%> or %<default%>");
4723 if (!token
->is_op(OPERATOR_RCURLY
))
4724 this->advance_token();
4729 // The body of a type switch.
4730 // "{" { TypeCaseClause } "}" .
4733 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4736 Expression
* init
= type_switch
.expr
;
4737 std::string var_name
= type_switch
.name
;
4738 if (!var_name
.empty())
4740 if (Gogo::is_sink_name(var_name
))
4742 go_error_at(type_switch
.location
,
4743 "no new variables on left side of %<:=%>");
4748 Location loc
= type_switch
.location
;
4749 Temporary_statement
* switch_temp
=
4750 Statement::make_temporary(NULL
, init
, loc
);
4751 this->gogo_
->add_statement(switch_temp
);
4752 init
= Expression::make_temporary_reference(switch_temp
, loc
);
4756 Type_switch_statement
* statement
=
4757 Statement::make_type_switch_statement(var_name
, init
, location
);
4758 this->push_break_statement(statement
, label
);
4760 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4761 bool saw_default
= false;
4762 std::vector
<Named_object
*> implicit_vars
;
4763 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4765 if (this->peek_token()->is_eof())
4767 go_error_at(this->location(), "missing %<}%>");
4770 this->type_case_clause(var_name
, init
, case_clauses
, &saw_default
,
4773 this->advance_token();
4775 statement
->add_clauses(case_clauses
);
4777 this->pop_break_statement();
4779 // If there is a type switch variable implicitly declared in each case clause,
4780 // check that it is used in at least one of the cases.
4781 if (!var_name
.empty())
4784 for (std::vector
<Named_object
*>::iterator p
= implicit_vars
.begin();
4785 p
!= implicit_vars
.end();
4788 if ((*p
)->var_value()->is_used())
4795 go_error_at(type_switch
.location
, "%qs declared and not used",
4796 Gogo::message_name(var_name
).c_str());
4801 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4802 // IMPLICIT_VARS is the list of variables implicitly declared for each type
4803 // case if there is a type switch variable declared.
4806 Parse::type_case_clause(const std::string
& var_name
, Expression
* init
,
4807 Type_case_clauses
* clauses
, bool* saw_default
,
4808 std::vector
<Named_object
*>* implicit_vars
)
4810 Location location
= this->location();
4812 std::vector
<Type
*> types
;
4813 bool is_default
= false;
4814 this->type_switch_case(&types
, &is_default
);
4816 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4817 go_error_at(this->location(), "expected %<:%>");
4819 this->advance_token();
4821 Block
* statements
= NULL
;
4822 if (this->statement_list_may_start_here())
4824 this->gogo_
->start_block(this->location());
4825 if (!var_name
.empty())
4828 Location var_loc
= init
->location();
4829 if (types
.size() == 1)
4831 type
= types
.front();
4832 init
= Expression::make_type_guard(init
, type
, location
);
4835 Variable
* v
= new Variable(type
, init
, false, false, false,
4838 v
->set_is_type_switch_var();
4839 implicit_vars
->push_back(this->gogo_
->add_variable(var_name
, v
));
4841 this->statement_list();
4842 statements
= this->gogo_
->finish_block(this->location());
4845 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4847 go_error_at(this->location(),
4848 "fallthrough is not permitted in a type switch");
4849 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4850 this->advance_token();
4855 go_assert(types
.empty());
4858 go_error_at(location
, "multiple defaults in type switch");
4861 *saw_default
= true;
4862 clauses
->add(NULL
, false, true, statements
, location
);
4864 else if (!types
.empty())
4866 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4867 p
+ 1 != types
.end();
4869 clauses
->add(*p
, true, false, NULL
, location
);
4870 clauses
->add(types
.back(), false, false, statements
, location
);
4873 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4876 // TypeSwitchCase = "case" type | "default"
4878 // We accept a comma separated list of types.
4881 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4883 const Token
* token
= this->peek_token();
4884 if (token
->is_keyword(KEYWORD_CASE
))
4886 this->advance_token();
4889 Type
* t
= this->type();
4891 if (!t
->is_error_type())
4892 types
->push_back(t
);
4895 this->gogo_
->mark_locals_used();
4896 token
= this->peek_token();
4897 while (!token
->is_op(OPERATOR_COLON
)
4898 && !token
->is_op(OPERATOR_COMMA
)
4899 && !token
->is_op(OPERATOR_RCURLY
)
4900 && !token
->is_eof())
4901 token
= this->advance_token();
4904 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4906 this->advance_token();
4909 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4911 this->advance_token();
4916 go_error_at(this->location(), "expected %<case%> or %<default%>");
4917 if (!token
->is_op(OPERATOR_RCURLY
))
4918 this->advance_token();
4922 // SelectStat = "select" "{" { CommClause } "}" .
4925 Parse::select_stat(Label
* label
)
4927 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4928 Location location
= this->location();
4929 const Token
* token
= this->advance_token();
4931 if (!token
->is_op(OPERATOR_LCURLY
))
4933 Location token_loc
= token
->location();
4934 if (token
->is_op(OPERATOR_SEMICOLON
)
4935 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4936 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4939 go_error_at(this->location(), "expected %<{%>");
4943 this->advance_token();
4945 Select_statement
* statement
= Statement::make_select_statement(location
);
4947 this->push_break_statement(statement
, label
);
4949 Select_clauses
* select_clauses
= new Select_clauses();
4950 bool saw_default
= false;
4951 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4953 if (this->peek_token()->is_eof())
4955 go_error_at(this->location(), "expected %<}%>");
4958 this->comm_clause(select_clauses
, &saw_default
);
4961 this->advance_token();
4963 statement
->add_clauses(select_clauses
);
4965 this->pop_break_statement();
4967 this->gogo_
->add_statement(statement
);
4970 // CommClause = CommCase ":" { Statement ";" } .
4973 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4975 Location location
= this->location();
4976 bool is_send
= false;
4977 Expression
* channel
= NULL
;
4978 Expression
* val
= NULL
;
4979 Expression
* closed
= NULL
;
4980 std::string varname
;
4981 std::string closedname
;
4982 bool is_default
= false;
4983 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4984 &varname
, &closedname
, &is_default
);
4986 if (this->peek_token()->is_op(OPERATOR_COLON
))
4987 this->advance_token();
4989 go_error_at(this->location(), "expected colon");
4991 this->gogo_
->start_block(this->location());
4993 Named_object
* var
= NULL
;
4994 if (!varname
.empty())
4996 // FIXME: LOCATION is slightly wrong here.
4997 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
4999 v
->set_type_from_chan_element();
5000 var
= this->gogo_
->add_variable(varname
, v
);
5003 Named_object
* closedvar
= NULL
;
5004 if (!closedname
.empty())
5006 // FIXME: LOCATION is slightly wrong here.
5007 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
5008 false, false, false, location
);
5009 closedvar
= this->gogo_
->add_variable(closedname
, v
);
5012 this->statement_list();
5014 Block
* statements
= this->gogo_
->finish_block(this->location());
5020 go_error_at(location
, "multiple defaults in select");
5023 *saw_default
= true;
5027 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
5028 statements
, location
);
5029 else if (statements
!= NULL
)
5031 // Add the statements to make sure that any names they define
5033 this->gogo_
->add_block(statements
, location
);
5037 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
5040 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
5041 Expression
** closed
, std::string
* varname
,
5042 std::string
* closedname
, bool* is_default
)
5044 const Token
* token
= this->peek_token();
5045 if (token
->is_keyword(KEYWORD_DEFAULT
))
5047 this->advance_token();
5050 else if (token
->is_keyword(KEYWORD_CASE
))
5052 this->advance_token();
5053 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
5059 go_error_at(this->location(), "expected %<case%> or %<default%>");
5060 if (!token
->is_op(OPERATOR_RCURLY
))
5061 this->advance_token();
5068 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
5069 // RecvExpr = Expression .
5072 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
5073 Expression
** closed
, std::string
* varname
,
5074 std::string
* closedname
)
5076 const Token
* token
= this->peek_token();
5077 bool saw_comma
= false;
5078 bool closed_is_id
= false;
5079 if (token
->is_identifier())
5081 Gogo
* gogo
= this->gogo_
;
5082 std::string recv_var
= token
->identifier();
5083 bool is_rv_exported
= token
->is_identifier_exported();
5084 Location recv_var_loc
= token
->location();
5085 token
= this->advance_token();
5086 if (token
->is_op(OPERATOR_COLONEQ
))
5089 this->advance_token();
5090 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
5092 Receive_expression
* re
= e
->receive_expression();
5095 if (!e
->is_error_expression())
5096 go_error_at(this->location(), "expected receive expression");
5099 if (recv_var
== "_")
5101 go_error_at(recv_var_loc
,
5102 "no new variables on left side of %<:=%>");
5103 recv_var
= Gogo::erroneous_name();
5106 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
5107 *channel
= re
->channel();
5110 else if (token
->is_op(OPERATOR_COMMA
))
5112 token
= this->advance_token();
5113 if (token
->is_identifier())
5115 std::string recv_closed
= token
->identifier();
5116 bool is_rc_exported
= token
->is_identifier_exported();
5117 Location recv_closed_loc
= token
->location();
5118 closed_is_id
= true;
5120 token
= this->advance_token();
5121 if (token
->is_op(OPERATOR_COLONEQ
))
5123 // case rv, rc := <-c:
5124 this->advance_token();
5125 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
5127 Receive_expression
* re
= e
->receive_expression();
5130 if (!e
->is_error_expression())
5131 go_error_at(this->location(),
5132 "expected receive expression");
5135 if (recv_var
== "_" && recv_closed
== "_")
5137 go_error_at(recv_var_loc
,
5138 "no new variables on left side of %<:=%>");
5139 recv_var
= Gogo::erroneous_name();
5142 if (recv_var
!= "_")
5143 *varname
= gogo
->pack_hidden_name(recv_var
,
5145 if (recv_closed
!= "_")
5146 *closedname
= gogo
->pack_hidden_name(recv_closed
,
5148 *channel
= re
->channel();
5152 this->unget_token(Token::make_identifier_token(recv_closed
,
5157 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5159 recv_var_loc
, true);
5163 this->unget_token(Token::make_identifier_token(recv_var
,
5168 // If SAW_COMMA is false, then we are looking at the start of the
5169 // send or receive expression. If SAW_COMMA is true, then *VAL is
5170 // set and we just read a comma.
5173 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5174 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5179 this->advance_token();
5180 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5182 // The next token should be ':'. If it is '<-', then we have
5184 // which is to say, send on a channel received from a channel.
5185 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5188 e
= Expression::make_receive(*channel
, (*channel
)->location());
5191 if (!saw_comma
&& this->peek_token()->is_op(OPERATOR_COMMA
))
5193 this->advance_token();
5195 if (!e
->is_sink_expression())
5197 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5201 if (this->peek_token()->is_op(OPERATOR_EQ
))
5203 if (!this->advance_token()->is_op(OPERATOR_CHANOP
))
5205 go_error_at(this->location(), "missing %<<-%>");
5209 this->advance_token();
5210 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5214 // *VAL is already set.
5215 if (!e
->is_sink_expression())
5221 if (!e
->is_sink_expression())
5230 go_error_at(this->location(), "expected %<=%> or %<:=%>");
5232 go_error_at(this->location(), "expected %<=%>");
5236 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5240 *channel
= this->verify_not_sink(e
);
5241 this->advance_token();
5242 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5246 go_error_at(this->location(), "expected %<<-%> or %<=%>");
5250 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5251 // Condition = Expression .
5254 Parse::for_stat(Label
* label
)
5256 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5257 Location location
= this->location();
5258 const Token
* token
= this->advance_token();
5260 // Open a block to hold any variables defined in the init statement
5261 // of the for statement.
5262 this->gogo_
->start_block(location
);
5265 Expression
* cond
= NULL
;
5267 Range_clause range_clause
;
5269 if (!token
->is_op(OPERATOR_LCURLY
))
5271 if (token
->is_keyword(KEYWORD_VAR
))
5273 go_error_at(this->location(),
5274 "var declaration not allowed in for initializer");
5278 if (token
->is_op(OPERATOR_SEMICOLON
))
5279 this->for_clause(&cond
, &post
);
5282 // We might be looking at a Condition, an InitStat, or a
5285 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5286 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5288 if (cond
== NULL
&& !range_clause
.found
)
5291 go_error_at(this->location(),
5292 ("send statement used as value; "
5293 "use select for non-blocking send"));
5295 go_error_at(this->location(),
5296 "parse error in for statement");
5301 if (range_clause
.found
)
5302 go_error_at(this->location(), "parse error after range clause");
5306 // COND is actually an expression statement for
5307 // InitStat at the start of a ForClause.
5308 this->expression_stat(cond
);
5312 this->for_clause(&cond
, &post
);
5317 // Check for the easy error of a newline before starting the block.
5318 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5320 Location semi_loc
= this->location();
5321 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5322 go_error_at(semi_loc
, "missing %<{%> after for clause");
5323 // Otherwise we will get an error when we call this->block
5327 // Build the For_statement and note that it is the current target
5328 // for break and continue statements.
5330 For_statement
* sfor
;
5331 For_range_statement
* srange
;
5333 if (!range_clause
.found
)
5335 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5341 srange
= Statement::make_for_range_statement(range_clause
.index
,
5349 this->push_break_statement(s
, label
);
5350 this->push_continue_statement(s
, label
);
5352 // Gather the block of statements in the loop and add them to the
5355 this->gogo_
->start_block(this->location());
5356 Location end_loc
= this->block();
5357 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5360 sfor
->add_statements(statements
);
5362 srange
->add_statements(statements
);
5364 // This is no longer the break/continue target.
5365 this->pop_break_statement();
5366 this->pop_continue_statement();
5368 // Add the For_statement to the list of statements, and close out
5369 // the block we started to hold any variables defined in the for
5372 this->gogo_
->add_statement(s
);
5374 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5378 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5379 // InitStat = SimpleStat .
5380 // PostStat = SimpleStat .
5382 // We have already read InitStat at this point.
5385 Parse::for_clause(Expression
** cond
, Block
** post
)
5387 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5388 this->advance_token();
5389 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5391 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5393 go_error_at(this->location(), "missing %<{%> after for clause");
5399 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5400 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5401 go_error_at(this->location(), "expected semicolon");
5403 this->advance_token();
5405 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5409 this->gogo_
->start_block(this->location());
5410 this->simple_stat(false, NULL
, NULL
, NULL
);
5411 *post
= this->gogo_
->finish_block(this->location());
5415 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5417 // This is the := version. It is called with a list of identifiers.
5420 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5421 Range_clause
* p_range_clause
)
5423 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5424 Location location
= this->location();
5426 p_range_clause
->found
= true;
5428 if (til
->size() > 2)
5429 go_error_at(this->location(), "too many variables for range clause");
5431 this->advance_token();
5432 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5434 p_range_clause
->range
= expr
;
5439 bool any_new
= false;
5441 const Typed_identifier
* pti
= &til
->front();
5442 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5444 if (any_new
&& no
->is_variable())
5445 no
->var_value()->set_type_from_range_index();
5446 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5448 if (til
->size() == 1)
5449 p_range_clause
->value
= NULL
;
5453 bool is_new
= false;
5454 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5455 if (is_new
&& no
->is_variable())
5456 no
->var_value()->set_type_from_range_value();
5459 if (!Gogo::is_sink_name(pti
->name()))
5460 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5464 go_error_at(location
, "variables redeclared but no variable is new");
5467 // The = version of RangeClause. This is called with a list of
5471 Parse::range_clause_expr(const Expression_list
* vals
,
5472 Range_clause
* p_range_clause
)
5474 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5476 p_range_clause
->found
= true;
5478 go_assert(vals
->size() >= 1);
5479 if (vals
->size() > 2)
5480 go_error_at(this->location(), "too many variables for range clause");
5482 this->advance_token();
5483 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5489 p_range_clause
->index
= vals
->front();
5490 if (vals
->size() == 1)
5491 p_range_clause
->value
= NULL
;
5493 p_range_clause
->value
= vals
->back();
5496 // Push a statement on the break stack.
5499 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5501 if (this->break_stack_
== NULL
)
5502 this->break_stack_
= new Bc_stack();
5503 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5506 // Push a statement on the continue stack.
5509 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5511 if (this->continue_stack_
== NULL
)
5512 this->continue_stack_
= new Bc_stack();
5513 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5516 // Pop the break stack.
5519 Parse::pop_break_statement()
5521 this->break_stack_
->pop_back();
5524 // Pop the continue stack.
5527 Parse::pop_continue_statement()
5529 this->continue_stack_
->pop_back();
5532 // Find a break or continue statement given a label name.
5535 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5537 if (bc_stack
== NULL
)
5539 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5540 p
!= bc_stack
->rend();
5543 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5545 p
->second
->set_is_used();
5552 // BreakStat = "break" [ identifier ] .
5557 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5558 Location location
= this->location();
5560 const Token
* token
= this->advance_token();
5561 Statement
* enclosing
;
5562 if (!token
->is_identifier())
5564 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5566 go_error_at(this->location(),
5567 "break statement not within for or switch or select");
5570 enclosing
= this->break_stack_
->back().first
;
5574 enclosing
= this->find_bc_statement(this->break_stack_
,
5575 token
->identifier());
5576 if (enclosing
== NULL
)
5578 // If there is a label with this name, mark it as used to
5579 // avoid a useless error about an unused label.
5580 this->gogo_
->add_label_reference(token
->identifier(),
5581 Linemap::unknown_location(), false);
5583 go_error_at(token
->location(), "invalid break label %qs",
5584 Gogo::message_name(token
->identifier()).c_str());
5585 this->advance_token();
5588 this->advance_token();
5591 Unnamed_label
* label
;
5592 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5593 label
= enclosing
->for_statement()->break_label();
5594 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5595 label
= enclosing
->for_range_statement()->break_label();
5596 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5597 label
= enclosing
->switch_statement()->break_label();
5598 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5599 label
= enclosing
->type_switch_statement()->break_label();
5600 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5601 label
= enclosing
->select_statement()->break_label();
5605 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5609 // ContinueStat = "continue" [ identifier ] .
5612 Parse::continue_stat()
5614 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5615 Location location
= this->location();
5617 const Token
* token
= this->advance_token();
5618 Statement
* enclosing
;
5619 if (!token
->is_identifier())
5621 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5623 go_error_at(this->location(), "continue statement not within for");
5626 enclosing
= this->continue_stack_
->back().first
;
5630 enclosing
= this->find_bc_statement(this->continue_stack_
,
5631 token
->identifier());
5632 if (enclosing
== NULL
)
5634 // If there is a label with this name, mark it as used to
5635 // avoid a useless error about an unused label.
5636 this->gogo_
->add_label_reference(token
->identifier(),
5637 Linemap::unknown_location(), false);
5639 go_error_at(token
->location(), "invalid continue label %qs",
5640 Gogo::message_name(token
->identifier()).c_str());
5641 this->advance_token();
5644 this->advance_token();
5647 Unnamed_label
* label
;
5648 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5649 label
= enclosing
->for_statement()->continue_label();
5650 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5651 label
= enclosing
->for_range_statement()->continue_label();
5655 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5659 // GotoStat = "goto" identifier .
5664 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5665 Location location
= this->location();
5666 const Token
* token
= this->advance_token();
5667 if (!token
->is_identifier())
5668 go_error_at(this->location(), "expected label for goto");
5671 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5673 Statement
* s
= Statement::make_goto_statement(label
, location
);
5674 this->gogo_
->add_statement(s
);
5675 this->advance_token();
5679 // PackageClause = "package" PackageName .
5682 Parse::package_clause()
5684 const Token
* token
= this->peek_token();
5685 Location location
= token
->location();
5687 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5689 go_error_at(this->location(), "program must start with package clause");
5694 token
= this->advance_token();
5695 if (token
->is_identifier())
5697 name
= token
->identifier();
5700 go_error_at(this->location(), "invalid package name _");
5701 name
= Gogo::erroneous_name();
5703 this->advance_token();
5707 go_error_at(this->location(), "package name must be an identifier");
5711 this->gogo_
->set_package_name(name
, location
);
5714 // ImportDecl = "import" Decl<ImportSpec> .
5717 Parse::import_decl()
5719 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5720 this->advance_token();
5721 this->decl(&Parse::import_spec
, NULL
, 0);
5724 // ImportSpec = [ "." | PackageName ] PackageFileName .
5727 Parse::import_spec(void*, unsigned int pragmas
)
5730 go_warning_at(this->location(), 0,
5731 "ignoring magic //go:... comment before import");
5733 const Token
* token
= this->peek_token();
5734 Location location
= token
->location();
5736 std::string local_name
;
5737 bool is_local_name_exported
= false;
5738 if (token
->is_op(OPERATOR_DOT
))
5741 token
= this->advance_token();
5743 else if (token
->is_identifier())
5745 local_name
= token
->identifier();
5746 is_local_name_exported
= token
->is_identifier_exported();
5747 token
= this->advance_token();
5750 if (!token
->is_string())
5752 go_error_at(this->location(), "import statement not a string");
5753 this->advance_token();
5757 this->gogo_
->import_package(token
->string_value(), local_name
,
5758 is_local_name_exported
, true, location
);
5760 this->advance_token();
5763 // SourceFile = PackageClause ";" { ImportDecl ";" }
5764 // { TopLevelDecl ";" } .
5769 this->package_clause();
5771 const Token
* token
= this->peek_token();
5772 if (token
->is_op(OPERATOR_SEMICOLON
))
5773 token
= this->advance_token();
5775 go_error_at(this->location(),
5776 "expected %<;%> or newline after package clause");
5778 while (token
->is_keyword(KEYWORD_IMPORT
))
5780 this->import_decl();
5781 token
= this->peek_token();
5782 if (token
->is_op(OPERATOR_SEMICOLON
))
5783 token
= this->advance_token();
5785 go_error_at(this->location(),
5786 "expected %<;%> or newline after import declaration");
5789 while (!token
->is_eof())
5791 if (this->declaration_may_start_here())
5792 this->declaration();
5795 go_error_at(this->location(), "expected declaration");
5796 this->gogo_
->mark_locals_used();
5798 this->advance_token();
5799 while (!this->peek_token()->is_eof()
5800 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5801 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5802 if (!this->peek_token()->is_eof()
5803 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5804 this->advance_token();
5806 token
= this->peek_token();
5807 if (token
->is_op(OPERATOR_SEMICOLON
))
5808 token
= this->advance_token();
5809 else if (!token
->is_eof() || !saw_errors())
5811 if (token
->is_op(OPERATOR_CHANOP
))
5812 go_error_at(this->location(),
5813 ("send statement used as value; "
5814 "use select for non-blocking send"));
5816 go_error_at(this->location(),
5817 ("expected %<;%> or newline after top "
5818 "level declaration"));
5819 this->skip_past_error(OPERATOR_INVALID
);
5824 // Reset the current iota value.
5832 // Return the current iota value.
5840 // Increment the current iota value.
5843 Parse::increment_iota()
5848 // Skip forward to a semicolon or OP. OP will normally be
5849 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5850 // past it and return. If we find OP, it will be the next token to
5851 // read. Return true if we are OK, false if we found EOF.
5854 Parse::skip_past_error(Operator op
)
5856 this->gogo_
->mark_locals_used();
5857 const Token
* token
= this->peek_token();
5858 while (!token
->is_op(op
))
5860 if (token
->is_eof())
5862 if (token
->is_op(OPERATOR_SEMICOLON
))
5864 this->advance_token();
5867 token
= this->advance_token();
5872 // Check that an expression is not a sink.
5875 Parse::verify_not_sink(Expression
* expr
)
5877 if (expr
->is_sink_expression())
5879 go_error_at(expr
->location(), "cannot use _ as value");
5880 expr
= Expression::make_error(expr
->location());
5883 // If this can not be a sink, and it is a variable, then we are
5884 // using the variable, not just assigning to it.
5885 if (expr
->var_expression() != NULL
)
5886 this->mark_var_used(expr
->var_expression()->named_object());
5887 else if (expr
->enclosed_var_expression() != NULL
)
5888 this->mark_var_used(expr
->enclosed_var_expression()->variable());
5892 // Mark a variable as used.
5895 Parse::mark_var_used(Named_object
* no
)
5897 if (no
->is_variable())
5898 no
->var_value()->set_is_used();