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
) const
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
,
2749 Expression::make_dereference(closure_ref
,
2750 Expression::NIL_CHECK_NOT_NEEDED
,
2753 // The closure structure holds pointers to the variables, so we need
2754 // to introduce an indirection.
2755 Expression
* e
= Expression::make_field_reference(closure_ref
,
2758 e
= Expression::make_dereference(e
, Expression::NIL_CHECK_NOT_NEEDED
,
2760 return Expression::make_enclosing_var_reference(e
, var
, location
);
2763 // CompositeLit = LiteralType LiteralValue .
2764 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2765 // SliceType | MapType | TypeName .
2766 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2767 // ElementList = Element { "," Element } .
2768 // Element = [ Key ":" ] Value .
2769 // Key = FieldName | ElementIndex .
2770 // FieldName = identifier .
2771 // ElementIndex = Expression .
2772 // Value = Expression | LiteralValue .
2774 // We have already seen the type if there is one, and we are now
2775 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2776 // will be seen here as an array type whose length is "nil". The
2777 // DEPTH parameter is non-zero if this is an embedded composite
2778 // literal and the type was omitted. It gives the number of steps up
2779 // to the type which was provided. E.g., in [][]int{{1}} it will be
2780 // 1. In [][][]int{{{1}}} it will be 2.
2783 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2785 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2786 this->advance_token();
2788 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2790 this->advance_token();
2791 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2795 bool has_keys
= false;
2796 bool all_are_names
= true;
2797 Expression_list
* vals
= new Expression_list
;
2801 bool is_type_omitted
= false;
2802 bool is_name
= false;
2804 const Token
* token
= this->peek_token();
2806 if (token
->is_identifier())
2808 std::string identifier
= token
->identifier();
2809 bool is_exported
= token
->is_identifier_exported();
2810 Location location
= token
->location();
2812 if (this->advance_token()->is_op(OPERATOR_COLON
))
2814 // This may be a field name. We don't know for sure--it
2815 // could also be an expression for an array index. We
2816 // don't want to parse it as an expression because may
2817 // trigger various errors, e.g., if this identifier
2818 // happens to be the name of a package.
2819 Gogo
* gogo
= this->gogo_
;
2820 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2827 this->unget_token(Token::make_identifier_token(identifier
,
2830 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2834 else if (!token
->is_op(OPERATOR_LCURLY
))
2835 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2838 // This must be a composite literal inside another composite
2839 // literal, with the type omitted for the inner one.
2840 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2841 is_type_omitted
= true;
2844 token
= this->peek_token();
2845 if (!token
->is_op(OPERATOR_COLON
))
2848 vals
->push_back(NULL
);
2853 if (is_type_omitted
)
2855 // VAL is a nested composite literal with an omitted type being
2856 // used a key. Record this information in VAL so that the correct
2857 // type is associated with the literal value if VAL is a
2859 val
->complit()->update_key_path(depth
);
2862 this->advance_token();
2864 if (!has_keys
&& !vals
->empty())
2866 Expression_list
* newvals
= new Expression_list
;
2867 for (Expression_list::const_iterator p
= vals
->begin();
2871 newvals
->push_back(NULL
);
2872 newvals
->push_back(*p
);
2879 if (val
->unknown_expression() != NULL
)
2880 val
->unknown_expression()->set_is_composite_literal_key();
2882 vals
->push_back(val
);
2884 if (!token
->is_op(OPERATOR_LCURLY
))
2885 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2888 // This must be a composite literal inside another
2889 // composite literal, with the type omitted for the
2891 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2894 token
= this->peek_token();
2897 vals
->push_back(val
);
2900 all_are_names
= false;
2902 if (token
->is_op(OPERATOR_COMMA
))
2904 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2906 this->advance_token();
2910 else if (token
->is_op(OPERATOR_RCURLY
))
2912 this->advance_token();
2917 if (token
->is_op(OPERATOR_SEMICOLON
))
2918 go_error_at(this->location(),
2919 ("need trailing comma before newline "
2920 "in composite literal"));
2922 go_error_at(this->location(), "expected %<,%> or %<}%>");
2924 this->gogo_
->mark_locals_used();
2926 while (!token
->is_eof()
2927 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2929 if (token
->is_op(OPERATOR_LCURLY
))
2931 else if (token
->is_op(OPERATOR_RCURLY
))
2933 token
= this->advance_token();
2935 if (token
->is_op(OPERATOR_RCURLY
))
2936 this->advance_token();
2938 return Expression::make_error(location
);
2942 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2943 all_are_names
, location
);
2946 // FunctionLit = "func" Signature Block .
2949 Parse::function_lit()
2951 Location location
= this->location();
2952 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2953 this->advance_token();
2955 Enclosing_vars hold_enclosing_vars
;
2956 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2958 Function_type
* type
= this->signature(NULL
, location
);
2959 bool fntype_is_error
= false;
2962 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2963 fntype_is_error
= true;
2966 // For a function literal, the next token must be a '{'. If we
2967 // don't see that, then we may have a type expression.
2968 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2970 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2971 return Expression::make_type(type
, location
);
2974 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2975 if (fntype_is_error
)
2976 this->is_erroneous_function_
= true;
2978 Bc_stack
* hold_break_stack
= this->break_stack_
;
2979 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2980 this->break_stack_
= NULL
;
2981 this->continue_stack_
= NULL
;
2983 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2985 Location end_loc
= this->block();
2987 this->gogo_
->finish_function(end_loc
);
2989 if (this->break_stack_
!= NULL
)
2990 delete this->break_stack_
;
2991 if (this->continue_stack_
!= NULL
)
2992 delete this->continue_stack_
;
2993 this->break_stack_
= hold_break_stack
;
2994 this->continue_stack_
= hold_continue_stack
;
2996 this->is_erroneous_function_
= hold_is_erroneous_function
;
2998 hold_enclosing_vars
.swap(this->enclosing_vars_
);
3000 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
3003 return Expression::make_func_reference(no
, closure
, location
);
3006 // Create a closure for the nested function FUNCTION. This is based
3007 // on ENCLOSING_VARS, which is a list of all variables defined in
3008 // enclosing functions and referenced from FUNCTION. A closure is the
3009 // address of a struct which point to the real function code and
3010 // contains the addresses of all the referenced variables. This
3011 // returns NULL if no closure is required.
3014 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
3017 if (enclosing_vars
->empty())
3020 // Get the variables in order by their field index.
3022 size_t enclosing_var_count
= enclosing_vars
->size();
3023 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
3024 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
3025 p
!= enclosing_vars
->end();
3028 // Subtract 1 because index 0 is the function code.
3029 ev
[p
->index() - 1] = *p
;
3032 // Build an initializer for a composite literal of the closure's
3035 Named_object
* enclosing_function
= this->gogo_
->current_function();
3036 Expression_list
* initializer
= new Expression_list
;
3038 initializer
->push_back(Expression::make_func_code_reference(function
,
3041 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
3043 // Add 1 to i because the first field in the closure is a
3044 // pointer to the function code.
3045 go_assert(ev
[i
].index() == i
+ 1);
3046 Named_object
* var
= ev
[i
].var();
3048 if (ev
[i
].in_function() == enclosing_function
)
3049 ref
= Expression::make_var_reference(var
, location
);
3051 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
3053 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
3055 initializer
->push_back(refaddr
);
3058 Named_object
* closure_var
= function
->func_value()->closure_var();
3059 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
3060 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
3063 // When compiling the runtime, closures do not escape. When escape
3064 // analysis becomes the default, and applies to closures, this
3065 // should be changed to make it an error if a closure escapes.
3066 if (this->gogo_
->compiling_runtime()
3067 && this->gogo_
->package_name() == "runtime")
3069 Temporary_statement
* ctemp
= Statement::make_temporary(st
, cv
, location
);
3070 this->gogo_
->add_statement(ctemp
);
3071 Expression
* ref
= Expression::make_temporary_reference(ctemp
, location
);
3072 Expression
* addr
= Expression::make_unary(OPERATOR_AND
, ref
, location
);
3073 addr
->unary_expression()->set_does_not_escape();
3077 return Expression::make_heap_expression(cv
, location
);
3080 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
3082 // If MAY_BE_SINK is true, this expression may be "_".
3084 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3087 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3088 // guard (var := expr.("type") using the literal keyword "type").
3090 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3091 // if the entire expression is in parentheses.
3094 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3095 bool* is_type_switch
, bool* is_parenthesized
)
3097 Location start_loc
= this->location();
3098 bool operand_is_parenthesized
= false;
3099 bool whole_is_parenthesized
= false;
3101 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
3103 whole_is_parenthesized
= operand_is_parenthesized
;
3105 // An unknown name followed by a curly brace must be a composite
3106 // literal, and the unknown name must be a type.
3107 if (may_be_composite_lit
3108 && !operand_is_parenthesized
3109 && ret
->unknown_expression() != NULL
3110 && this->peek_token()->is_op(OPERATOR_LCURLY
))
3112 Named_object
* no
= ret
->unknown_expression()->named_object();
3113 Type
* type
= Type::make_forward_declaration(no
);
3114 ret
= Expression::make_type(type
, ret
->location());
3117 // We handle composite literals and type casts here, as it is the
3118 // easiest way to handle types which are in parentheses, as in
3120 if (ret
->is_type_expression())
3122 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
3124 whole_is_parenthesized
= false;
3125 if (!may_be_composite_lit
)
3127 Type
* t
= ret
->type();
3128 if (t
->named_type() != NULL
3129 || t
->forward_declaration_type() != NULL
)
3130 go_error_at(start_loc
,
3131 _("parentheses required around this composite "
3132 "literal to avoid parsing ambiguity"));
3134 else if (operand_is_parenthesized
)
3135 go_error_at(start_loc
,
3136 "cannot parenthesize type in composite literal");
3137 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
3139 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
3141 whole_is_parenthesized
= false;
3142 Location loc
= this->location();
3143 this->advance_token();
3144 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
3146 if (this->peek_token()->is_op(OPERATOR_COMMA
))
3147 this->advance_token();
3148 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
3150 go_error_at(this->location(),
3151 "invalid use of %<...%> in type conversion");
3152 this->advance_token();
3154 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3155 go_error_at(this->location(), "expected %<)%>");
3157 this->advance_token();
3158 if (expr
->is_error_expression())
3162 Type
* t
= ret
->type();
3163 if (t
->classification() == Type::TYPE_ARRAY
3164 && t
->array_type()->length() != NULL
3165 && t
->array_type()->length()->is_nil_expression())
3167 go_error_at(ret
->location(),
3168 "use of %<[...]%> outside of array literal");
3169 ret
= Expression::make_error(loc
);
3172 ret
= Expression::make_cast(t
, expr
, loc
);
3179 const Token
* token
= this->peek_token();
3180 if (token
->is_op(OPERATOR_LPAREN
))
3182 whole_is_parenthesized
= false;
3183 ret
= this->call(this->verify_not_sink(ret
));
3185 else if (token
->is_op(OPERATOR_DOT
))
3187 whole_is_parenthesized
= false;
3188 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3189 if (is_type_switch
!= NULL
&& *is_type_switch
)
3192 else if (token
->is_op(OPERATOR_LSQUARE
))
3194 whole_is_parenthesized
= false;
3195 ret
= this->index(this->verify_not_sink(ret
));
3201 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3202 *is_parenthesized
= true;
3207 // Selector = "." identifier .
3208 // TypeGuard = "." "(" QualifiedIdent ")" .
3210 // Note that Operand can expand to QualifiedIdent, which contains a
3211 // ".". That is handled directly in operand when it sees a package
3214 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3215 // guard (var := expr.("type") using the literal keyword "type").
3218 Parse::selector(Expression
* left
, bool* is_type_switch
)
3220 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3221 Location location
= this->location();
3223 const Token
* token
= this->advance_token();
3224 if (token
->is_identifier())
3226 // This could be a field in a struct, or a method in an
3227 // interface, or a method associated with a type. We can't know
3228 // which until we have seen all the types.
3230 this->gogo_
->pack_hidden_name(token
->identifier(),
3231 token
->is_identifier_exported());
3232 if (token
->identifier() == "_")
3234 go_error_at(this->location(), "invalid use of %<_%>");
3235 name
= Gogo::erroneous_name();
3237 this->advance_token();
3238 return Expression::make_selector(left
, name
, location
);
3240 else if (token
->is_op(OPERATOR_LPAREN
))
3242 this->advance_token();
3244 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3245 type
= this->type();
3248 if (is_type_switch
!= NULL
)
3249 *is_type_switch
= true;
3252 go_error_at(this->location(),
3253 "use of %<.(type)%> outside type switch");
3254 type
= Type::make_error_type();
3256 this->advance_token();
3258 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3259 go_error_at(this->location(), "missing %<)%>");
3261 this->advance_token();
3262 if (is_type_switch
!= NULL
&& *is_type_switch
)
3264 return Expression::make_type_guard(left
, type
, location
);
3268 go_error_at(this->location(), "expected identifier or %<(%>");
3273 // Index = "[" Expression "]" .
3274 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3277 Parse::index(Expression
* expr
)
3279 Location location
= this->location();
3280 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3281 this->advance_token();
3284 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3285 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3287 start
= Expression::make_integer_ul(0, NULL
, location
);
3289 Expression
* end
= NULL
;
3290 if (this->peek_token()->is_op(OPERATOR_COLON
))
3292 // We use nil to indicate a missing high expression.
3293 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3294 end
= Expression::make_nil(this->location());
3295 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3297 go_error_at(this->location(),
3298 "middle index required in 3-index slice");
3299 end
= Expression::make_error(this->location());
3302 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3305 Expression
* cap
= NULL
;
3306 if (this->peek_token()->is_op(OPERATOR_COLON
))
3308 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3310 go_error_at(this->location(),
3311 "final index required in 3-index slice");
3312 cap
= Expression::make_error(this->location());
3315 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3317 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3318 go_error_at(this->location(), "missing %<]%>");
3320 this->advance_token();
3321 return Expression::make_index(expr
, start
, end
, cap
, location
);
3324 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3325 // ArgumentList = ExpressionList [ "..." ] .
3328 Parse::call(Expression
* func
)
3330 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3331 Expression_list
* args
= NULL
;
3332 bool is_varargs
= false;
3333 const Token
* token
= this->advance_token();
3334 if (!token
->is_op(OPERATOR_RPAREN
))
3336 args
= this->expression_list(NULL
, false, true);
3337 token
= this->peek_token();
3338 if (token
->is_op(OPERATOR_ELLIPSIS
))
3341 token
= this->advance_token();
3344 if (token
->is_op(OPERATOR_COMMA
))
3345 token
= this->advance_token();
3346 if (!token
->is_op(OPERATOR_RPAREN
))
3348 go_error_at(this->location(), "missing %<)%>");
3349 if (!this->skip_past_error(OPERATOR_RPAREN
))
3350 return Expression::make_error(this->location());
3352 this->advance_token();
3353 if (func
->is_error_expression())
3355 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3358 // Return an expression for a single unqualified identifier.
3361 Parse::id_to_expression(const std::string
& name
, Location location
,
3364 Named_object
* in_function
;
3365 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3366 if (named_object
== NULL
)
3367 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3369 if (in_function
!= NULL
3370 && in_function
!= this->gogo_
->current_function()
3371 && (named_object
->is_variable() || named_object
->is_result_variable()))
3372 return this->enclosing_var_reference(in_function
, named_object
, is_lhs
,
3375 switch (named_object
->classification())
3377 case Named_object::NAMED_OBJECT_CONST
:
3378 return Expression::make_const_reference(named_object
, location
);
3379 case Named_object::NAMED_OBJECT_VAR
:
3380 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3382 this->mark_var_used(named_object
);
3383 return Expression::make_var_reference(named_object
, location
);
3384 case Named_object::NAMED_OBJECT_SINK
:
3385 return Expression::make_sink(location
);
3386 case Named_object::NAMED_OBJECT_FUNC
:
3387 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3388 return Expression::make_func_reference(named_object
, NULL
, location
);
3389 case Named_object::NAMED_OBJECT_UNKNOWN
:
3391 Unknown_expression
* ue
=
3392 Expression::make_unknown_reference(named_object
, location
);
3393 if (this->is_erroneous_function_
)
3394 ue
->set_no_error_message();
3397 case Named_object::NAMED_OBJECT_PACKAGE
:
3398 case Named_object::NAMED_OBJECT_TYPE
:
3399 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3401 // These cases can arise for a field name in a composite
3402 // literal. Keep track of these as they might be fake uses of
3403 // the related package.
3404 Unknown_expression
* ue
=
3405 Expression::make_unknown_reference(named_object
, location
);
3406 if (named_object
->package() != NULL
)
3407 named_object
->package()->note_fake_usage(ue
);
3408 if (this->is_erroneous_function_
)
3409 ue
->set_no_error_message();
3412 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3413 return Expression::make_error(location
);
3415 go_error_at(this->location(), "unexpected type of identifier");
3416 return Expression::make_error(location
);
3420 // Expression = UnaryExpr { binary_op Expression } .
3422 // PRECEDENCE is the precedence of the current operator.
3424 // If MAY_BE_SINK is true, this expression may be "_".
3426 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3429 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3430 // guard (var := expr.("type") using the literal keyword "type").
3432 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3433 // if the entire expression is in parentheses.
3436 Parse::expression(Precedence precedence
, bool may_be_sink
,
3437 bool may_be_composite_lit
, bool* is_type_switch
,
3438 bool *is_parenthesized
)
3440 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3441 is_type_switch
, is_parenthesized
);
3445 if (is_type_switch
!= NULL
&& *is_type_switch
)
3448 const Token
* token
= this->peek_token();
3449 if (token
->classification() != Token::TOKEN_OPERATOR
)
3455 Precedence right_precedence
;
3456 switch (token
->op())
3459 right_precedence
= PRECEDENCE_OROR
;
3461 case OPERATOR_ANDAND
:
3462 right_precedence
= PRECEDENCE_ANDAND
;
3465 case OPERATOR_NOTEQ
:
3470 right_precedence
= PRECEDENCE_RELOP
;
3473 case OPERATOR_MINUS
:
3476 right_precedence
= PRECEDENCE_ADDOP
;
3481 case OPERATOR_LSHIFT
:
3482 case OPERATOR_RSHIFT
:
3484 case OPERATOR_BITCLEAR
:
3485 right_precedence
= PRECEDENCE_MULOP
;
3488 right_precedence
= PRECEDENCE_INVALID
;
3492 if (right_precedence
== PRECEDENCE_INVALID
)
3498 if (is_parenthesized
!= NULL
)
3499 *is_parenthesized
= false;
3501 Operator op
= token
->op();
3502 Location binop_location
= token
->location();
3504 if (precedence
>= right_precedence
)
3506 // We've already seen A * B, and we see + C. We want to
3507 // return so that A * B becomes a group.
3511 this->advance_token();
3513 left
= this->verify_not_sink(left
);
3514 Expression
* right
= this->expression(right_precedence
, false,
3515 may_be_composite_lit
,
3517 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3522 Parse::expression_may_start_here()
3524 const Token
* token
= this->peek_token();
3525 switch (token
->classification())
3527 case Token::TOKEN_INVALID
:
3528 case Token::TOKEN_EOF
:
3530 case Token::TOKEN_KEYWORD
:
3531 switch (token
->keyword())
3536 case KEYWORD_STRUCT
:
3537 case KEYWORD_INTERFACE
:
3542 case Token::TOKEN_IDENTIFIER
:
3544 case Token::TOKEN_STRING
:
3546 case Token::TOKEN_OPERATOR
:
3547 switch (token
->op())
3550 case OPERATOR_MINUS
:
3554 case OPERATOR_CHANOP
:
3556 case OPERATOR_LPAREN
:
3557 case OPERATOR_LSQUARE
:
3562 case Token::TOKEN_CHARACTER
:
3563 case Token::TOKEN_INTEGER
:
3564 case Token::TOKEN_FLOAT
:
3565 case Token::TOKEN_IMAGINARY
:
3572 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3574 // If MAY_BE_SINK is true, this expression may be "_".
3576 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3579 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3580 // guard (var := expr.("type") using the literal keyword "type").
3582 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3583 // if the entire expression is in parentheses.
3586 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3587 bool* is_type_switch
, bool* is_parenthesized
)
3589 const Token
* token
= this->peek_token();
3591 // There is a complex parse for <- chan. The choices are
3592 // Convert x to type <- chan int:
3594 // Receive from (x converted to type chan <- chan int):
3595 // (<- chan <- chan int (x))
3596 // Convert x to type <- chan (<- chan int).
3597 // (<- chan <- chan int)(x)
3598 if (token
->is_op(OPERATOR_CHANOP
))
3600 Location location
= token
->location();
3601 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3603 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3605 if (expr
->is_error_expression())
3607 else if (!expr
->is_type_expression())
3608 return Expression::make_receive(expr
, location
);
3611 if (expr
->type()->is_error_type())
3614 // We picked up "chan TYPE", but it is not a type
3616 Channel_type
* ct
= expr
->type()->channel_type();
3619 // This is probably impossible.
3620 go_error_at(location
, "expected channel type");
3621 return Expression::make_error(location
);
3623 else if (ct
->may_receive())
3626 Type
* t
= Type::make_channel_type(false, true,
3627 ct
->element_type());
3628 return Expression::make_type(t
, location
);
3632 // <- chan <- TYPE. Because we skipped the leading
3633 // <-, we parsed this as chan <- TYPE. With the
3634 // leading <-, we parse it as <- chan (<- TYPE).
3635 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3636 return Expression::make_type(t
, location
);
3641 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3642 token
= this->peek_token();
3645 if (token
->is_op(OPERATOR_PLUS
)
3646 || token
->is_op(OPERATOR_MINUS
)
3647 || token
->is_op(OPERATOR_NOT
)
3648 || token
->is_op(OPERATOR_XOR
)
3649 || token
->is_op(OPERATOR_CHANOP
)
3650 || token
->is_op(OPERATOR_MULT
)
3651 || token
->is_op(OPERATOR_AND
))
3653 Location location
= token
->location();
3654 Operator op
= token
->op();
3655 this->advance_token();
3657 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3659 if (expr
->is_error_expression())
3661 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3662 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3664 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3665 expr
= Expression::make_heap_expression(expr
, location
);
3666 else if (op
!= OPERATOR_CHANOP
)
3667 expr
= Expression::make_unary(op
, expr
, location
);
3669 expr
= Expression::make_receive(expr
, location
);
3673 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3674 is_type_switch
, is_parenthesized
);
3677 // This is called for the obscure case of
3678 // (<- chan <- chan int)(x)
3679 // In unary_expr we remove the leading <- and parse the remainder,
3681 // chan <- (chan int)
3682 // When we add the leading <- back in, we really want
3683 // <- chan (<- chan int)
3684 // This means that we need to reassociate.
3687 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3689 Channel_type
* ele
= ct
->element_type()->channel_type();
3692 go_error_at(location
, "parse error");
3693 return Type::make_error_type();
3696 if (ele
->may_send())
3697 sub
= Type::make_channel_type(false, true, ele
->element_type());
3699 sub
= this->reassociate_chan_direction(ele
, location
);
3700 return Type::make_channel_type(false, true, sub
);
3704 // Declaration | LabeledStmt | SimpleStmt |
3705 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3706 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3709 // LABEL is the label of this statement if it has one.
3712 Parse::statement(Label
* label
)
3714 const Token
* token
= this->peek_token();
3715 switch (token
->classification())
3717 case Token::TOKEN_KEYWORD
:
3719 switch (token
->keyword())
3724 this->declaration();
3728 case KEYWORD_STRUCT
:
3729 case KEYWORD_INTERFACE
:
3730 this->simple_stat(true, NULL
, NULL
, NULL
);
3734 this->go_or_defer_stat();
3736 case KEYWORD_RETURN
:
3737 this->return_stat();
3742 case KEYWORD_CONTINUE
:
3743 this->continue_stat();
3751 case KEYWORD_SWITCH
:
3752 this->switch_stat(label
);
3754 case KEYWORD_SELECT
:
3755 this->select_stat(label
);
3758 this->for_stat(label
);
3761 go_error_at(this->location(), "expected statement");
3762 this->advance_token();
3768 case Token::TOKEN_IDENTIFIER
:
3770 std::string identifier
= token
->identifier();
3771 bool is_exported
= token
->is_identifier_exported();
3772 Location location
= token
->location();
3773 if (this->advance_token()->is_op(OPERATOR_COLON
))
3775 this->advance_token();
3776 this->labeled_stmt(identifier
, location
);
3780 this->unget_token(Token::make_identifier_token(identifier
,
3783 this->simple_stat(true, NULL
, NULL
, NULL
);
3788 case Token::TOKEN_OPERATOR
:
3789 if (token
->is_op(OPERATOR_LCURLY
))
3791 Location location
= token
->location();
3792 this->gogo_
->start_block(location
);
3793 Location end_loc
= this->block();
3794 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3797 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3798 this->simple_stat(true, NULL
, NULL
, NULL
);
3801 case Token::TOKEN_STRING
:
3802 case Token::TOKEN_CHARACTER
:
3803 case Token::TOKEN_INTEGER
:
3804 case Token::TOKEN_FLOAT
:
3805 case Token::TOKEN_IMAGINARY
:
3806 this->simple_stat(true, NULL
, NULL
, NULL
);
3810 go_error_at(this->location(), "expected statement");
3811 this->advance_token();
3817 Parse::statement_may_start_here()
3819 const Token
* token
= this->peek_token();
3820 switch (token
->classification())
3822 case Token::TOKEN_KEYWORD
:
3824 switch (token
->keyword())
3831 case KEYWORD_STRUCT
:
3832 case KEYWORD_INTERFACE
:
3835 case KEYWORD_RETURN
:
3837 case KEYWORD_CONTINUE
:
3840 case KEYWORD_SWITCH
:
3841 case KEYWORD_SELECT
:
3851 case Token::TOKEN_IDENTIFIER
:
3854 case Token::TOKEN_OPERATOR
:
3855 if (token
->is_op(OPERATOR_LCURLY
)
3856 || token
->is_op(OPERATOR_SEMICOLON
))
3859 return this->expression_may_start_here();
3861 case Token::TOKEN_STRING
:
3862 case Token::TOKEN_CHARACTER
:
3863 case Token::TOKEN_INTEGER
:
3864 case Token::TOKEN_FLOAT
:
3865 case Token::TOKEN_IMAGINARY
:
3873 // LabeledStmt = Label ":" Statement .
3874 // Label = identifier .
3877 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3879 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3881 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3883 // This is a label at the end of a block. A program is
3884 // permitted to omit a semicolon here.
3888 if (!this->statement_may_start_here())
3890 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
3892 // We don't treat the fallthrough keyword as a statement,
3893 // because it can't appear most places where a statement is
3894 // permitted, but it may have a label. We introduce a
3895 // semicolon because the caller expects to see a statement.
3896 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3901 // Mark the label as used to avoid a useless error about an
3904 label
->set_is_used();
3906 go_error_at(location
, "missing statement after label");
3907 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3912 this->statement(label
);
3915 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3916 // Assignment | ShortVarDecl .
3918 // EmptyStmt was handled in Parse::statement.
3920 // In order to make this work for if and switch statements, if
3921 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3922 // expression rather than adding an expression statement to the
3923 // current block. If we see something other than an ExpressionStat,
3924 // we add the statement, set *RETURN_EXP to true if we saw a send
3925 // statement, and return NULL. The handling of send statements is for
3926 // better error messages.
3928 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3931 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3932 // guard (var := expr.("type") using the literal keyword "type").
3935 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3936 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3938 const Token
* token
= this->peek_token();
3940 // An identifier follow by := is a SimpleVarDecl.
3941 if (token
->is_identifier())
3943 std::string identifier
= token
->identifier();
3944 bool is_exported
= token
->is_identifier_exported();
3945 Location location
= token
->location();
3947 token
= this->advance_token();
3948 if (token
->is_op(OPERATOR_COLONEQ
)
3949 || token
->is_op(OPERATOR_COMMA
))
3951 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3952 this->simple_var_decl_or_assignment(identifier
, location
,
3953 may_be_composite_lit
,
3955 (token
->is_op(OPERATOR_COLONEQ
)
3961 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3964 else if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
3966 Typed_identifier_list til
;
3967 this->range_clause_decl(&til
, p_range_clause
);
3971 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3972 may_be_composite_lit
,
3973 (p_type_switch
== NULL
3975 : &p_type_switch
->found
),
3977 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3979 p_type_switch
->name
.clear();
3980 p_type_switch
->location
= exp
->location();
3981 p_type_switch
->expr
= this->verify_not_sink(exp
);
3984 token
= this->peek_token();
3985 if (token
->is_op(OPERATOR_CHANOP
))
3987 this->send_stmt(this->verify_not_sink(exp
), may_be_composite_lit
);
3988 if (return_exp
!= NULL
)
3991 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3992 || token
->is_op(OPERATOR_MINUSMINUS
))
3993 this->inc_dec_stat(this->verify_not_sink(exp
));
3994 else if (token
->is_op(OPERATOR_COMMA
)
3995 || token
->is_op(OPERATOR_EQ
))
3996 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3997 else if (token
->is_op(OPERATOR_PLUSEQ
)
3998 || token
->is_op(OPERATOR_MINUSEQ
)
3999 || token
->is_op(OPERATOR_OREQ
)
4000 || token
->is_op(OPERATOR_XOREQ
)
4001 || token
->is_op(OPERATOR_MULTEQ
)
4002 || token
->is_op(OPERATOR_DIVEQ
)
4003 || token
->is_op(OPERATOR_MODEQ
)
4004 || token
->is_op(OPERATOR_LSHIFTEQ
)
4005 || token
->is_op(OPERATOR_RSHIFTEQ
)
4006 || token
->is_op(OPERATOR_ANDEQ
)
4007 || token
->is_op(OPERATOR_BITCLEAREQ
))
4008 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
4010 else if (return_exp
!= NULL
)
4011 return this->verify_not_sink(exp
);
4014 exp
= this->verify_not_sink(exp
);
4016 if (token
->is_op(OPERATOR_COLONEQ
))
4018 if (!exp
->is_error_expression())
4019 go_error_at(token
->location(), "non-name on left side of %<:=%>");
4020 this->gogo_
->mark_locals_used();
4021 while (!token
->is_op(OPERATOR_SEMICOLON
)
4022 && !token
->is_eof())
4023 token
= this->advance_token();
4027 this->expression_stat(exp
);
4034 Parse::simple_stat_may_start_here()
4036 return this->expression_may_start_here();
4039 // Parse { Statement ";" } which is used in a few places. The list of
4040 // statements may end with a right curly brace, in which case the
4041 // semicolon may be omitted.
4044 Parse::statement_list()
4046 while (this->statement_may_start_here())
4048 this->statement(NULL
);
4049 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4050 this->advance_token();
4051 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4055 if (!this->peek_token()->is_eof() || !saw_errors())
4056 go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
4057 if (!this->skip_past_error(OPERATOR_RCURLY
))
4064 Parse::statement_list_may_start_here()
4066 return this->statement_may_start_here();
4069 // ExpressionStat = Expression .
4072 Parse::expression_stat(Expression
* exp
)
4074 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
4077 // SendStmt = Channel "<-" Expression .
4078 // Channel = Expression .
4081 Parse::send_stmt(Expression
* channel
, bool may_be_composite_lit
)
4083 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
4084 Location loc
= this->location();
4085 this->advance_token();
4086 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false,
4087 may_be_composite_lit
, NULL
, NULL
);
4088 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
4089 this->gogo_
->add_statement(s
);
4092 // IncDecStat = Expression ( "++" | "--" ) .
4095 Parse::inc_dec_stat(Expression
* exp
)
4097 const Token
* token
= this->peek_token();
4098 if (token
->is_op(OPERATOR_PLUSPLUS
))
4099 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
4100 else if (token
->is_op(OPERATOR_MINUSMINUS
))
4101 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
4104 this->advance_token();
4107 // Assignment = ExpressionList assign_op ExpressionList .
4109 // EXP is an expression that we have already parsed.
4111 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4112 // side may be a composite literal.
4114 // If RANGE_CLAUSE is not NULL, then this will recognize a
4118 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
4119 Range_clause
* p_range_clause
)
4121 Expression_list
* vars
;
4122 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4124 vars
= new Expression_list();
4125 vars
->push_back(expr
);
4129 this->advance_token();
4130 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
4133 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
4136 // An assignment statement. LHS is the list of expressions which
4137 // appear on the left hand side.
4139 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4140 // side may be a composite literal.
4142 // If RANGE_CLAUSE is not NULL, then this will recognize a
4146 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
4147 Range_clause
* p_range_clause
)
4149 const Token
* token
= this->peek_token();
4150 if (!token
->is_op(OPERATOR_EQ
)
4151 && !token
->is_op(OPERATOR_PLUSEQ
)
4152 && !token
->is_op(OPERATOR_MINUSEQ
)
4153 && !token
->is_op(OPERATOR_OREQ
)
4154 && !token
->is_op(OPERATOR_XOREQ
)
4155 && !token
->is_op(OPERATOR_MULTEQ
)
4156 && !token
->is_op(OPERATOR_DIVEQ
)
4157 && !token
->is_op(OPERATOR_MODEQ
)
4158 && !token
->is_op(OPERATOR_LSHIFTEQ
)
4159 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4160 && !token
->is_op(OPERATOR_ANDEQ
)
4161 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4163 go_error_at(this->location(), "expected assignment operator");
4166 Operator op
= token
->op();
4167 Location location
= token
->location();
4169 token
= this->advance_token();
4174 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4176 if (op
!= OPERATOR_EQ
)
4177 go_error_at(this->location(), "range clause requires %<=%>");
4178 this->range_clause_expr(lhs
, p_range_clause
);
4182 Expression_list
* vals
= this->expression_list(NULL
, false,
4183 may_be_composite_lit
);
4185 // We've parsed everything; check for errors.
4188 for (Expression_list::const_iterator pe
= lhs
->begin();
4192 if ((*pe
)->is_error_expression())
4194 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4195 go_error_at((*pe
)->location(), "cannot use _ as value");
4197 for (Expression_list::const_iterator pe
= vals
->begin();
4201 if ((*pe
)->is_error_expression())
4205 Call_expression
* call
;
4206 Index_expression
* map_index
;
4207 Receive_expression
* receive
;
4208 Type_guard_expression
* type_guard
;
4209 if (lhs
->size() == vals
->size())
4212 if (lhs
->size() > 1)
4214 if (op
!= OPERATOR_EQ
)
4215 go_error_at(location
, "multiple values only permitted with %<=%>");
4216 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4220 if (op
== OPERATOR_EQ
)
4221 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4224 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4225 vals
->front(), location
);
4229 this->gogo_
->add_statement(s
);
4231 else if (vals
->size() == 1
4232 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4234 if (op
!= OPERATOR_EQ
)
4235 go_error_at(location
, "multiple results only permitted with %<=%>");
4236 call
->set_expected_result_count(lhs
->size());
4238 vals
= new Expression_list
;
4239 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4240 vals
->push_back(Expression::make_call_result(call
, i
));
4241 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4242 this->gogo_
->add_statement(s
);
4244 else if (lhs
->size() == 2
4245 && vals
->size() == 1
4246 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4248 if (op
!= OPERATOR_EQ
)
4249 go_error_at(location
, "two values from map requires %<=%>");
4250 Expression
* val
= lhs
->front();
4251 Expression
* present
= lhs
->back();
4252 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4253 map_index
, location
);
4254 this->gogo_
->add_statement(s
);
4256 else if (lhs
->size() == 2
4257 && vals
->size() == 1
4258 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4260 if (op
!= OPERATOR_EQ
)
4261 go_error_at(location
, "two values from receive requires %<=%>");
4262 Expression
* val
= lhs
->front();
4263 Expression
* success
= lhs
->back();
4264 Expression
* channel
= receive
->channel();
4265 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4268 this->gogo_
->add_statement(s
);
4270 else if (lhs
->size() == 2
4271 && vals
->size() == 1
4272 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4274 if (op
!= OPERATOR_EQ
)
4275 go_error_at(location
, "two values from type guard requires %<=%>");
4276 Expression
* val
= lhs
->front();
4277 Expression
* ok
= lhs
->back();
4278 Expression
* expr
= type_guard
->expr();
4279 Type
* type
= type_guard
->type();
4280 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4283 this->gogo_
->add_statement(s
);
4287 go_error_at(location
, ("number of variables does not "
4288 "match number of values"));
4292 // GoStat = "go" Expression .
4293 // DeferStat = "defer" Expression .
4296 Parse::go_or_defer_stat()
4298 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4299 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4300 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4301 Location stat_location
= this->location();
4303 this->advance_token();
4304 Location expr_location
= this->location();
4306 bool is_parenthesized
= false;
4307 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4309 Call_expression
* call_expr
= expr
->call_expression();
4310 if (is_parenthesized
|| call_expr
== NULL
)
4312 go_error_at(expr_location
, "argument to go/defer must be function call");
4316 // Make it easier to simplify go/defer statements by putting every
4317 // statement in its own block.
4318 this->gogo_
->start_block(stat_location
);
4321 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4323 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4324 this->gogo_
->add_statement(stat
);
4325 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4329 // ReturnStat = "return" [ ExpressionList ] .
4332 Parse::return_stat()
4334 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4335 Location location
= this->location();
4336 this->advance_token();
4337 Expression_list
* vals
= NULL
;
4338 if (this->expression_may_start_here())
4339 vals
= this->expression_list(NULL
, false, true);
4340 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4343 && this->gogo_
->current_function()->func_value()->results_are_named())
4345 Named_object
* function
= this->gogo_
->current_function();
4346 Function::Results
* results
= function
->func_value()->result_variables();
4347 for (Function::Results::const_iterator p
= results
->begin();
4348 p
!= results
->end();
4351 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4353 go_assert(saw_errors());
4354 else if (!no
->is_result_variable())
4355 go_error_at(location
, "%qs is shadowed during return",
4356 (*p
)->message_name().c_str());
4361 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4362 // [ "else" ( IfStmt | Block ) ] .
4367 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4368 Location location
= this->location();
4369 this->advance_token();
4371 this->gogo_
->start_block(location
);
4373 bool saw_simple_stat
= false;
4374 Expression
* cond
= NULL
;
4375 bool saw_send_stmt
= false;
4376 if (this->simple_stat_may_start_here())
4378 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4379 saw_simple_stat
= true;
4381 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4383 // The SimpleStat is an expression statement.
4384 this->expression_stat(cond
);
4389 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4390 this->advance_token();
4391 else if (saw_simple_stat
)
4394 go_error_at(this->location(),
4395 ("send statement used as value; "
4396 "use select for non-blocking send"));
4398 go_error_at(this->location(),
4399 "expected %<;%> after statement in if expression");
4400 if (!this->expression_may_start_here())
4401 cond
= Expression::make_error(this->location());
4403 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4405 go_error_at(this->location(),
4406 "missing condition in if statement");
4407 cond
= Expression::make_error(this->location());
4410 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4413 // Check for the easy error of a newline before starting the block.
4414 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4416 Location semi_loc
= this->location();
4417 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4418 go_error_at(semi_loc
, "missing %<{%> after if clause");
4419 // Otherwise we will get an error when we call this->block
4423 this->gogo_
->start_block(this->location());
4424 Location end_loc
= this->block();
4425 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4427 // Check for the easy error of a newline before "else".
4428 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4430 Location semi_loc
= this->location();
4431 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4432 go_error_at(this->location(),
4433 "unexpected semicolon or newline before %<else%>");
4435 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4439 Block
* else_block
= NULL
;
4440 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4442 this->gogo_
->start_block(this->location());
4443 const Token
* token
= this->advance_token();
4444 if (token
->is_keyword(KEYWORD_IF
))
4446 else if (token
->is_op(OPERATOR_LCURLY
))
4450 go_error_at(this->location(), "expected %<if%> or %<{%>");
4451 this->statement(NULL
);
4453 else_block
= this->gogo_
->finish_block(this->location());
4456 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4460 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4464 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4465 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4466 // "{" { ExprCaseClause } "}" .
4467 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4468 // "{" { TypeCaseClause } "}" .
4469 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4472 Parse::switch_stat(Label
* label
)
4474 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4475 Location location
= this->location();
4476 this->advance_token();
4478 this->gogo_
->start_block(location
);
4480 bool saw_simple_stat
= false;
4481 Expression
* switch_val
= NULL
;
4483 Type_switch type_switch
;
4484 bool have_type_switch_block
= false;
4485 if (this->simple_stat_may_start_here())
4487 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4489 saw_simple_stat
= true;
4491 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4493 // The SimpleStat is an expression statement.
4494 this->expression_stat(switch_val
);
4497 if (switch_val
== NULL
&& !type_switch
.found
)
4499 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4500 this->advance_token();
4501 else if (saw_simple_stat
)
4504 go_error_at(this->location(),
4505 ("send statement used as value; "
4506 "use select for non-blocking send"));
4508 go_error_at(this->location(),
4509 "expected %<;%> after statement in switch expression");
4511 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4513 if (this->peek_token()->is_identifier())
4515 const Token
* token
= this->peek_token();
4516 std::string identifier
= token
->identifier();
4517 bool is_exported
= token
->is_identifier_exported();
4518 Location id_loc
= token
->location();
4520 token
= this->advance_token();
4521 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4522 this->unget_token(Token::make_identifier_token(identifier
,
4527 // This must be a TypeSwitchGuard. It is in a
4528 // different block from any initial SimpleStat.
4529 if (saw_simple_stat
)
4531 this->gogo_
->start_block(id_loc
);
4532 have_type_switch_block
= true;
4535 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4537 if (!type_switch
.found
)
4539 if (switch_val
== NULL
4540 || !switch_val
->is_error_expression())
4543 "expected type switch assignment");
4544 switch_val
= Expression::make_error(id_loc
);
4549 if (switch_val
== NULL
&& !type_switch
.found
)
4551 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4552 &type_switch
.found
, NULL
);
4553 if (type_switch
.found
)
4555 type_switch
.name
.clear();
4556 type_switch
.expr
= switch_val
;
4557 type_switch
.location
= switch_val
->location();
4563 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4565 Location token_loc
= this->location();
4566 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4567 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4568 go_error_at(token_loc
, "missing %<{%> after switch clause");
4569 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4571 go_error_at(token_loc
, "invalid variable name");
4572 this->advance_token();
4573 this->expression(PRECEDENCE_NORMAL
, false, false,
4574 &type_switch
.found
, NULL
);
4575 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4576 this->advance_token();
4577 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4579 if (have_type_switch_block
)
4580 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4582 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4586 if (type_switch
.found
)
4587 type_switch
.expr
= Expression::make_error(location
);
4591 go_error_at(this->location(), "expected %<{%>");
4592 if (have_type_switch_block
)
4593 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4595 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4600 this->advance_token();
4602 Statement
* statement
;
4603 if (type_switch
.found
)
4604 statement
= this->type_switch_body(label
, type_switch
, location
);
4606 statement
= this->expr_switch_body(label
, switch_val
, location
);
4608 if (statement
!= NULL
)
4609 this->gogo_
->add_statement(statement
);
4611 if (have_type_switch_block
)
4612 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4615 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4619 // The body of an expression switch.
4620 // "{" { ExprCaseClause } "}"
4623 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4626 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4629 this->push_break_statement(statement
, label
);
4631 Case_clauses
* case_clauses
= new Case_clauses();
4632 bool saw_default
= false;
4633 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4635 if (this->peek_token()->is_eof())
4638 go_error_at(this->location(), "missing %<}%>");
4641 this->expr_case_clause(case_clauses
, &saw_default
);
4643 this->advance_token();
4645 statement
->add_clauses(case_clauses
);
4647 this->pop_break_statement();
4652 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4653 // FallthroughStat = "fallthrough" .
4656 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4658 Location location
= this->location();
4660 bool is_default
= false;
4661 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4663 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4666 go_error_at(this->location(), "expected %<:%>");
4670 this->advance_token();
4672 Block
* statements
= NULL
;
4673 if (this->statement_list_may_start_here())
4675 this->gogo_
->start_block(this->location());
4676 this->statement_list();
4677 statements
= this->gogo_
->finish_block(this->location());
4680 bool is_fallthrough
= false;
4681 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4683 Location fallthrough_loc
= this->location();
4684 is_fallthrough
= true;
4685 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4686 this->advance_token();
4687 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4688 go_error_at(fallthrough_loc
,
4689 _("cannot fallthrough final case in switch"));
4696 go_error_at(location
, "multiple defaults in switch");
4699 *saw_default
= true;
4702 if (is_default
|| vals
!= NULL
)
4703 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4706 // ExprSwitchCase = "case" ExpressionList | "default" .
4709 Parse::expr_switch_case(bool* is_default
)
4711 const Token
* token
= this->peek_token();
4712 if (token
->is_keyword(KEYWORD_CASE
))
4714 this->advance_token();
4715 return this->expression_list(NULL
, false, true);
4717 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4719 this->advance_token();
4726 go_error_at(this->location(), "expected %<case%> or %<default%>");
4727 if (!token
->is_op(OPERATOR_RCURLY
))
4728 this->advance_token();
4733 // The body of a type switch.
4734 // "{" { TypeCaseClause } "}" .
4737 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4740 Expression
* init
= type_switch
.expr
;
4741 std::string var_name
= type_switch
.name
;
4742 if (!var_name
.empty())
4744 if (Gogo::is_sink_name(var_name
))
4746 go_error_at(type_switch
.location
,
4747 "no new variables on left side of %<:=%>");
4752 Location loc
= type_switch
.location
;
4753 Temporary_statement
* switch_temp
=
4754 Statement::make_temporary(NULL
, init
, loc
);
4755 this->gogo_
->add_statement(switch_temp
);
4756 init
= Expression::make_temporary_reference(switch_temp
, loc
);
4760 Type_switch_statement
* statement
=
4761 Statement::make_type_switch_statement(var_name
, init
, location
);
4762 this->push_break_statement(statement
, label
);
4764 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4765 bool saw_default
= false;
4766 std::vector
<Named_object
*> implicit_vars
;
4767 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4769 if (this->peek_token()->is_eof())
4771 go_error_at(this->location(), "missing %<}%>");
4774 this->type_case_clause(var_name
, init
, case_clauses
, &saw_default
,
4777 this->advance_token();
4779 statement
->add_clauses(case_clauses
);
4781 this->pop_break_statement();
4783 // If there is a type switch variable implicitly declared in each case clause,
4784 // check that it is used in at least one of the cases.
4785 if (!var_name
.empty())
4788 for (std::vector
<Named_object
*>::iterator p
= implicit_vars
.begin();
4789 p
!= implicit_vars
.end();
4792 if ((*p
)->var_value()->is_used())
4799 go_error_at(type_switch
.location
, "%qs declared and not used",
4800 Gogo::message_name(var_name
).c_str());
4805 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4806 // IMPLICIT_VARS is the list of variables implicitly declared for each type
4807 // case if there is a type switch variable declared.
4810 Parse::type_case_clause(const std::string
& var_name
, Expression
* init
,
4811 Type_case_clauses
* clauses
, bool* saw_default
,
4812 std::vector
<Named_object
*>* implicit_vars
)
4814 Location location
= this->location();
4816 std::vector
<Type
*> types
;
4817 bool is_default
= false;
4818 this->type_switch_case(&types
, &is_default
);
4820 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4821 go_error_at(this->location(), "expected %<:%>");
4823 this->advance_token();
4825 Block
* statements
= NULL
;
4826 if (this->statement_list_may_start_here())
4828 this->gogo_
->start_block(this->location());
4829 if (!var_name
.empty())
4832 Location var_loc
= init
->location();
4833 if (types
.size() == 1)
4835 type
= types
.front();
4836 init
= Expression::make_type_guard(init
, type
, location
);
4839 Variable
* v
= new Variable(type
, init
, false, false, false,
4842 v
->set_is_type_switch_var();
4843 implicit_vars
->push_back(this->gogo_
->add_variable(var_name
, v
));
4845 this->statement_list();
4846 statements
= this->gogo_
->finish_block(this->location());
4849 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4851 go_error_at(this->location(),
4852 "fallthrough is not permitted in a type switch");
4853 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4854 this->advance_token();
4859 go_assert(types
.empty());
4862 go_error_at(location
, "multiple defaults in type switch");
4865 *saw_default
= true;
4866 clauses
->add(NULL
, false, true, statements
, location
);
4868 else if (!types
.empty())
4870 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4871 p
+ 1 != types
.end();
4873 clauses
->add(*p
, true, false, NULL
, location
);
4874 clauses
->add(types
.back(), false, false, statements
, location
);
4877 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4880 // TypeSwitchCase = "case" type | "default"
4882 // We accept a comma separated list of types.
4885 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4887 const Token
* token
= this->peek_token();
4888 if (token
->is_keyword(KEYWORD_CASE
))
4890 this->advance_token();
4893 Type
* t
= this->type();
4895 if (!t
->is_error_type())
4896 types
->push_back(t
);
4899 this->gogo_
->mark_locals_used();
4900 token
= this->peek_token();
4901 while (!token
->is_op(OPERATOR_COLON
)
4902 && !token
->is_op(OPERATOR_COMMA
)
4903 && !token
->is_op(OPERATOR_RCURLY
)
4904 && !token
->is_eof())
4905 token
= this->advance_token();
4908 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4910 this->advance_token();
4913 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4915 this->advance_token();
4920 go_error_at(this->location(), "expected %<case%> or %<default%>");
4921 if (!token
->is_op(OPERATOR_RCURLY
))
4922 this->advance_token();
4926 // SelectStat = "select" "{" { CommClause } "}" .
4929 Parse::select_stat(Label
* label
)
4931 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4932 Location location
= this->location();
4933 const Token
* token
= this->advance_token();
4935 if (!token
->is_op(OPERATOR_LCURLY
))
4937 Location token_loc
= token
->location();
4938 if (token
->is_op(OPERATOR_SEMICOLON
)
4939 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4940 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4943 go_error_at(this->location(), "expected %<{%>");
4947 this->advance_token();
4949 Select_statement
* statement
= Statement::make_select_statement(location
);
4951 this->push_break_statement(statement
, label
);
4953 Select_clauses
* select_clauses
= new Select_clauses();
4954 bool saw_default
= false;
4955 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4957 if (this->peek_token()->is_eof())
4959 go_error_at(this->location(), "expected %<}%>");
4962 this->comm_clause(select_clauses
, &saw_default
);
4965 this->advance_token();
4967 statement
->add_clauses(select_clauses
);
4969 this->pop_break_statement();
4971 this->gogo_
->add_statement(statement
);
4974 // CommClause = CommCase ":" { Statement ";" } .
4977 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4979 Location location
= this->location();
4980 bool is_send
= false;
4981 Expression
* channel
= NULL
;
4982 Expression
* val
= NULL
;
4983 Expression
* closed
= NULL
;
4984 std::string varname
;
4985 std::string closedname
;
4986 bool is_default
= false;
4987 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4988 &varname
, &closedname
, &is_default
);
4990 if (this->peek_token()->is_op(OPERATOR_COLON
))
4991 this->advance_token();
4993 go_error_at(this->location(), "expected colon");
4995 this->gogo_
->start_block(this->location());
4997 Named_object
* var
= NULL
;
4998 if (!varname
.empty())
5000 // FIXME: LOCATION is slightly wrong here.
5001 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
5003 v
->set_type_from_chan_element();
5004 var
= this->gogo_
->add_variable(varname
, v
);
5007 Named_object
* closedvar
= NULL
;
5008 if (!closedname
.empty())
5010 // FIXME: LOCATION is slightly wrong here.
5011 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
5012 false, false, false, location
);
5013 closedvar
= this->gogo_
->add_variable(closedname
, v
);
5016 this->statement_list();
5018 Block
* statements
= this->gogo_
->finish_block(this->location());
5024 go_error_at(location
, "multiple defaults in select");
5027 *saw_default
= true;
5031 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
5032 statements
, location
);
5033 else if (statements
!= NULL
)
5035 // Add the statements to make sure that any names they define
5037 this->gogo_
->add_block(statements
, location
);
5041 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
5044 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
5045 Expression
** closed
, std::string
* varname
,
5046 std::string
* closedname
, bool* is_default
)
5048 const Token
* token
= this->peek_token();
5049 if (token
->is_keyword(KEYWORD_DEFAULT
))
5051 this->advance_token();
5054 else if (token
->is_keyword(KEYWORD_CASE
))
5056 this->advance_token();
5057 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
5063 go_error_at(this->location(), "expected %<case%> or %<default%>");
5064 if (!token
->is_op(OPERATOR_RCURLY
))
5065 this->advance_token();
5072 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
5073 // RecvExpr = Expression .
5076 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
5077 Expression
** closed
, std::string
* varname
,
5078 std::string
* closedname
)
5080 const Token
* token
= this->peek_token();
5081 bool saw_comma
= false;
5082 bool closed_is_id
= false;
5083 if (token
->is_identifier())
5085 Gogo
* gogo
= this->gogo_
;
5086 std::string recv_var
= token
->identifier();
5087 bool is_rv_exported
= token
->is_identifier_exported();
5088 Location recv_var_loc
= token
->location();
5089 token
= this->advance_token();
5090 if (token
->is_op(OPERATOR_COLONEQ
))
5093 this->advance_token();
5094 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
5096 Receive_expression
* re
= e
->receive_expression();
5099 if (!e
->is_error_expression())
5100 go_error_at(this->location(), "expected receive expression");
5103 if (recv_var
== "_")
5105 go_error_at(recv_var_loc
,
5106 "no new variables on left side of %<:=%>");
5107 recv_var
= Gogo::erroneous_name();
5110 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
5111 *channel
= re
->channel();
5114 else if (token
->is_op(OPERATOR_COMMA
))
5116 token
= this->advance_token();
5117 if (token
->is_identifier())
5119 std::string recv_closed
= token
->identifier();
5120 bool is_rc_exported
= token
->is_identifier_exported();
5121 Location recv_closed_loc
= token
->location();
5122 closed_is_id
= true;
5124 token
= this->advance_token();
5125 if (token
->is_op(OPERATOR_COLONEQ
))
5127 // case rv, rc := <-c:
5128 this->advance_token();
5129 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
5131 Receive_expression
* re
= e
->receive_expression();
5134 if (!e
->is_error_expression())
5135 go_error_at(this->location(),
5136 "expected receive expression");
5139 if (recv_var
== "_" && recv_closed
== "_")
5141 go_error_at(recv_var_loc
,
5142 "no new variables on left side of %<:=%>");
5143 recv_var
= Gogo::erroneous_name();
5146 if (recv_var
!= "_")
5147 *varname
= gogo
->pack_hidden_name(recv_var
,
5149 if (recv_closed
!= "_")
5150 *closedname
= gogo
->pack_hidden_name(recv_closed
,
5152 *channel
= re
->channel();
5156 this->unget_token(Token::make_identifier_token(recv_closed
,
5161 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5163 recv_var_loc
, true);
5167 this->unget_token(Token::make_identifier_token(recv_var
,
5172 // If SAW_COMMA is false, then we are looking at the start of the
5173 // send or receive expression. If SAW_COMMA is true, then *VAL is
5174 // set and we just read a comma.
5177 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5178 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5183 this->advance_token();
5184 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5186 // The next token should be ':'. If it is '<-', then we have
5188 // which is to say, send on a channel received from a channel.
5189 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5192 e
= Expression::make_receive(*channel
, (*channel
)->location());
5195 if (!saw_comma
&& this->peek_token()->is_op(OPERATOR_COMMA
))
5197 this->advance_token();
5199 if (!e
->is_sink_expression())
5201 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5205 if (this->peek_token()->is_op(OPERATOR_EQ
))
5207 if (!this->advance_token()->is_op(OPERATOR_CHANOP
))
5209 go_error_at(this->location(), "missing %<<-%>");
5213 this->advance_token();
5214 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5218 // *VAL is already set.
5219 if (!e
->is_sink_expression())
5225 if (!e
->is_sink_expression())
5234 go_error_at(this->location(), "expected %<=%> or %<:=%>");
5236 go_error_at(this->location(), "expected %<=%>");
5240 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5244 *channel
= this->verify_not_sink(e
);
5245 this->advance_token();
5246 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5250 go_error_at(this->location(), "expected %<<-%> or %<=%>");
5254 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5255 // Condition = Expression .
5258 Parse::for_stat(Label
* label
)
5260 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5261 Location location
= this->location();
5262 const Token
* token
= this->advance_token();
5264 // Open a block to hold any variables defined in the init statement
5265 // of the for statement.
5266 this->gogo_
->start_block(location
);
5269 Expression
* cond
= NULL
;
5271 Range_clause range_clause
;
5273 if (!token
->is_op(OPERATOR_LCURLY
))
5275 if (token
->is_keyword(KEYWORD_VAR
))
5277 go_error_at(this->location(),
5278 "var declaration not allowed in for initializer");
5282 if (token
->is_op(OPERATOR_SEMICOLON
))
5283 this->for_clause(&cond
, &post
);
5286 // We might be looking at a Condition, an InitStat, or a
5289 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5290 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5292 if (cond
== NULL
&& !range_clause
.found
)
5295 go_error_at(this->location(),
5296 ("send statement used as value; "
5297 "use select for non-blocking send"));
5299 go_error_at(this->location(),
5300 "parse error in for statement");
5305 if (range_clause
.found
)
5306 go_error_at(this->location(), "parse error after range clause");
5310 // COND is actually an expression statement for
5311 // InitStat at the start of a ForClause.
5312 this->expression_stat(cond
);
5316 this->for_clause(&cond
, &post
);
5321 // Check for the easy error of a newline before starting the block.
5322 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5324 Location semi_loc
= this->location();
5325 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5326 go_error_at(semi_loc
, "missing %<{%> after for clause");
5327 // Otherwise we will get an error when we call this->block
5331 // Build the For_statement and note that it is the current target
5332 // for break and continue statements.
5334 For_statement
* sfor
;
5335 For_range_statement
* srange
;
5337 if (!range_clause
.found
)
5339 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5345 srange
= Statement::make_for_range_statement(range_clause
.index
,
5353 this->push_break_statement(s
, label
);
5354 this->push_continue_statement(s
, label
);
5356 // Gather the block of statements in the loop and add them to the
5359 this->gogo_
->start_block(this->location());
5360 Location end_loc
= this->block();
5361 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5364 sfor
->add_statements(statements
);
5366 srange
->add_statements(statements
);
5368 // This is no longer the break/continue target.
5369 this->pop_break_statement();
5370 this->pop_continue_statement();
5372 // Add the For_statement to the list of statements, and close out
5373 // the block we started to hold any variables defined in the for
5376 this->gogo_
->add_statement(s
);
5378 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5382 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5383 // InitStat = SimpleStat .
5384 // PostStat = SimpleStat .
5386 // We have already read InitStat at this point.
5389 Parse::for_clause(Expression
** cond
, Block
** post
)
5391 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5392 this->advance_token();
5393 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5395 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5397 go_error_at(this->location(), "missing %<{%> after for clause");
5403 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5404 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5405 go_error_at(this->location(), "expected semicolon");
5407 this->advance_token();
5409 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5413 this->gogo_
->start_block(this->location());
5414 this->simple_stat(false, NULL
, NULL
, NULL
);
5415 *post
= this->gogo_
->finish_block(this->location());
5419 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5421 // This is the := version. It is called with a list of identifiers.
5424 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5425 Range_clause
* p_range_clause
)
5427 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5428 Location location
= this->location();
5430 p_range_clause
->found
= true;
5432 if (til
->size() > 2)
5433 go_error_at(this->location(), "too many variables for range clause");
5435 this->advance_token();
5436 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5438 p_range_clause
->range
= expr
;
5443 bool any_new
= false;
5445 const Typed_identifier
* pti
= &til
->front();
5446 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5448 if (any_new
&& no
->is_variable())
5449 no
->var_value()->set_type_from_range_index();
5450 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5452 if (til
->size() == 1)
5453 p_range_clause
->value
= NULL
;
5457 bool is_new
= false;
5458 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5459 if (is_new
&& no
->is_variable())
5460 no
->var_value()->set_type_from_range_value();
5463 if (!Gogo::is_sink_name(pti
->name()))
5464 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5468 go_error_at(location
, "variables redeclared but no variable is new");
5471 // The = version of RangeClause. This is called with a list of
5475 Parse::range_clause_expr(const Expression_list
* vals
,
5476 Range_clause
* p_range_clause
)
5478 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5480 p_range_clause
->found
= true;
5482 go_assert(vals
->size() >= 1);
5483 if (vals
->size() > 2)
5484 go_error_at(this->location(), "too many variables for range clause");
5486 this->advance_token();
5487 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5493 p_range_clause
->index
= vals
->front();
5494 if (vals
->size() == 1)
5495 p_range_clause
->value
= NULL
;
5497 p_range_clause
->value
= vals
->back();
5500 // Push a statement on the break stack.
5503 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5505 if (this->break_stack_
== NULL
)
5506 this->break_stack_
= new Bc_stack();
5507 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5510 // Push a statement on the continue stack.
5513 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5515 if (this->continue_stack_
== NULL
)
5516 this->continue_stack_
= new Bc_stack();
5517 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5520 // Pop the break stack.
5523 Parse::pop_break_statement()
5525 this->break_stack_
->pop_back();
5528 // Pop the continue stack.
5531 Parse::pop_continue_statement()
5533 this->continue_stack_
->pop_back();
5536 // Find a break or continue statement given a label name.
5539 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5541 if (bc_stack
== NULL
)
5543 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5544 p
!= bc_stack
->rend();
5547 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5549 p
->second
->set_is_used();
5556 // BreakStat = "break" [ identifier ] .
5561 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5562 Location location
= this->location();
5564 const Token
* token
= this->advance_token();
5565 Statement
* enclosing
;
5566 if (!token
->is_identifier())
5568 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5570 go_error_at(this->location(),
5571 "break statement not within for or switch or select");
5574 enclosing
= this->break_stack_
->back().first
;
5578 enclosing
= this->find_bc_statement(this->break_stack_
,
5579 token
->identifier());
5580 if (enclosing
== NULL
)
5582 // If there is a label with this name, mark it as used to
5583 // avoid a useless error about an unused label.
5584 this->gogo_
->add_label_reference(token
->identifier(),
5585 Linemap::unknown_location(), false);
5587 go_error_at(token
->location(), "invalid break label %qs",
5588 Gogo::message_name(token
->identifier()).c_str());
5589 this->advance_token();
5592 this->advance_token();
5595 Unnamed_label
* label
;
5596 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5597 label
= enclosing
->for_statement()->break_label();
5598 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5599 label
= enclosing
->for_range_statement()->break_label();
5600 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5601 label
= enclosing
->switch_statement()->break_label();
5602 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5603 label
= enclosing
->type_switch_statement()->break_label();
5604 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5605 label
= enclosing
->select_statement()->break_label();
5609 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5613 // ContinueStat = "continue" [ identifier ] .
5616 Parse::continue_stat()
5618 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5619 Location location
= this->location();
5621 const Token
* token
= this->advance_token();
5622 Statement
* enclosing
;
5623 if (!token
->is_identifier())
5625 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5627 go_error_at(this->location(), "continue statement not within for");
5630 enclosing
= this->continue_stack_
->back().first
;
5634 enclosing
= this->find_bc_statement(this->continue_stack_
,
5635 token
->identifier());
5636 if (enclosing
== NULL
)
5638 // If there is a label with this name, mark it as used to
5639 // avoid a useless error about an unused label.
5640 this->gogo_
->add_label_reference(token
->identifier(),
5641 Linemap::unknown_location(), false);
5643 go_error_at(token
->location(), "invalid continue label %qs",
5644 Gogo::message_name(token
->identifier()).c_str());
5645 this->advance_token();
5648 this->advance_token();
5651 Unnamed_label
* label
;
5652 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5653 label
= enclosing
->for_statement()->continue_label();
5654 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5655 label
= enclosing
->for_range_statement()->continue_label();
5659 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5663 // GotoStat = "goto" identifier .
5668 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5669 Location location
= this->location();
5670 const Token
* token
= this->advance_token();
5671 if (!token
->is_identifier())
5672 go_error_at(this->location(), "expected label for goto");
5675 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5677 Statement
* s
= Statement::make_goto_statement(label
, location
);
5678 this->gogo_
->add_statement(s
);
5679 this->advance_token();
5683 // PackageClause = "package" PackageName .
5686 Parse::package_clause()
5688 const Token
* token
= this->peek_token();
5689 Location location
= token
->location();
5691 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5693 go_error_at(this->location(), "program must start with package clause");
5698 token
= this->advance_token();
5699 if (token
->is_identifier())
5701 name
= token
->identifier();
5704 go_error_at(this->location(), "invalid package name _");
5705 name
= Gogo::erroneous_name();
5707 this->advance_token();
5711 go_error_at(this->location(), "package name must be an identifier");
5715 this->gogo_
->set_package_name(name
, location
);
5718 // ImportDecl = "import" Decl<ImportSpec> .
5721 Parse::import_decl()
5723 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5724 this->advance_token();
5725 this->decl(&Parse::import_spec
, NULL
, 0);
5728 // ImportSpec = [ "." | PackageName ] PackageFileName .
5731 Parse::import_spec(void*, unsigned int pragmas
)
5734 go_warning_at(this->location(), 0,
5735 "ignoring magic //go:... comment before import");
5737 const Token
* token
= this->peek_token();
5738 Location location
= token
->location();
5740 std::string local_name
;
5741 bool is_local_name_exported
= false;
5742 if (token
->is_op(OPERATOR_DOT
))
5745 token
= this->advance_token();
5747 else if (token
->is_identifier())
5749 local_name
= token
->identifier();
5750 is_local_name_exported
= token
->is_identifier_exported();
5751 token
= this->advance_token();
5754 if (!token
->is_string())
5756 go_error_at(this->location(), "import statement not a string");
5757 this->advance_token();
5761 this->gogo_
->import_package(token
->string_value(), local_name
,
5762 is_local_name_exported
, true, location
);
5764 this->advance_token();
5767 // SourceFile = PackageClause ";" { ImportDecl ";" }
5768 // { TopLevelDecl ";" } .
5773 this->package_clause();
5775 const Token
* token
= this->peek_token();
5776 if (token
->is_op(OPERATOR_SEMICOLON
))
5777 token
= this->advance_token();
5779 go_error_at(this->location(),
5780 "expected %<;%> or newline after package clause");
5782 while (token
->is_keyword(KEYWORD_IMPORT
))
5784 this->import_decl();
5785 token
= this->peek_token();
5786 if (token
->is_op(OPERATOR_SEMICOLON
))
5787 token
= this->advance_token();
5789 go_error_at(this->location(),
5790 "expected %<;%> or newline after import declaration");
5793 while (!token
->is_eof())
5795 if (this->declaration_may_start_here())
5796 this->declaration();
5799 go_error_at(this->location(), "expected declaration");
5800 this->gogo_
->mark_locals_used();
5802 this->advance_token();
5803 while (!this->peek_token()->is_eof()
5804 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5805 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5806 if (!this->peek_token()->is_eof()
5807 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5808 this->advance_token();
5810 token
= this->peek_token();
5811 if (token
->is_op(OPERATOR_SEMICOLON
))
5812 token
= this->advance_token();
5813 else if (!token
->is_eof() || !saw_errors())
5815 if (token
->is_op(OPERATOR_CHANOP
))
5816 go_error_at(this->location(),
5817 ("send statement used as value; "
5818 "use select for non-blocking send"));
5820 go_error_at(this->location(),
5821 ("expected %<;%> or newline after top "
5822 "level declaration"));
5823 this->skip_past_error(OPERATOR_INVALID
);
5828 // Reset the current iota value.
5836 // Return the current iota value.
5844 // Increment the current iota value.
5847 Parse::increment_iota()
5852 // Skip forward to a semicolon or OP. OP will normally be
5853 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5854 // past it and return. If we find OP, it will be the next token to
5855 // read. Return true if we are OK, false if we found EOF.
5858 Parse::skip_past_error(Operator op
)
5860 this->gogo_
->mark_locals_used();
5861 const Token
* token
= this->peek_token();
5862 while (!token
->is_op(op
))
5864 if (token
->is_eof())
5866 if (token
->is_op(OPERATOR_SEMICOLON
))
5868 this->advance_token();
5871 token
= this->advance_token();
5876 // Check that an expression is not a sink.
5879 Parse::verify_not_sink(Expression
* expr
)
5881 if (expr
->is_sink_expression())
5883 go_error_at(expr
->location(), "cannot use _ as value");
5884 expr
= Expression::make_error(expr
->location());
5887 // If this can not be a sink, and it is a variable, then we are
5888 // using the variable, not just assigning to it.
5889 if (expr
->var_expression() != NULL
)
5890 this->mark_var_used(expr
->var_expression()->named_object());
5891 else if (expr
->enclosed_var_expression() != NULL
)
5892 this->mark_var_used(expr
->enclosed_var_expression()->variable());
5896 // Mark a variable as used.
5899 Parse::mark_var_used(Named_object
* no
)
5901 if (no
->is_variable())
5902 no
->var_value()->set_is_used();