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
),
57 // Return the current token.
62 if (this->unget_token_valid_
)
63 return &this->unget_token_
;
64 if (this->token_
.is_invalid())
65 this->token_
= this->lex_
->next_token();
69 // Advance to the next token and return it.
72 Parse::advance_token()
74 if (this->unget_token_valid_
)
76 this->unget_token_valid_
= false;
77 if (!this->token_
.is_invalid())
80 this->token_
= this->lex_
->next_token();
84 // Push a token back on the input stream.
87 Parse::unget_token(const Token
& token
)
89 go_assert(!this->unget_token_valid_
);
90 this->unget_token_
= token
;
91 this->unget_token_valid_
= true;
94 // The location of the current token.
99 return this->peek_token()->location();
102 // IdentifierList = identifier { "," identifier } .
105 Parse::identifier_list(Typed_identifier_list
* til
)
107 const Token
* token
= this->peek_token();
110 if (!token
->is_identifier())
112 go_error_at(this->location(), "expected identifier");
116 this->gogo_
->pack_hidden_name(token
->identifier(),
117 token
->is_identifier_exported());
118 til
->push_back(Typed_identifier(name
, NULL
, token
->location()));
119 token
= this->advance_token();
120 if (!token
->is_op(OPERATOR_COMMA
))
122 token
= this->advance_token();
126 // ExpressionList = Expression { "," Expression } .
128 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
131 // If MAY_BE_SINK is true, the expressions in the list may be "_".
134 Parse::expression_list(Expression
* first
, bool may_be_sink
,
135 bool may_be_composite_lit
)
137 Expression_list
* ret
= new Expression_list();
139 ret
->push_back(first
);
142 ret
->push_back(this->expression(PRECEDENCE_NORMAL
, may_be_sink
,
143 may_be_composite_lit
, NULL
, NULL
));
145 const Token
* token
= this->peek_token();
146 if (!token
->is_op(OPERATOR_COMMA
))
149 // Most expression lists permit a trailing comma.
150 Location location
= token
->location();
151 this->advance_token();
152 if (!this->expression_may_start_here())
154 this->unget_token(Token::make_operator_token(OPERATOR_COMMA
,
161 // QualifiedIdent = [ PackageName "." ] identifier .
162 // PackageName = identifier .
164 // This sets *PNAME to the identifier and sets *PPACKAGE to the
165 // package or NULL if there isn't one. This returns true on success,
166 // false on failure in which case it will have emitted an error
170 Parse::qualified_ident(std::string
* pname
, Named_object
** ppackage
)
172 const Token
* token
= this->peek_token();
173 if (!token
->is_identifier())
175 go_error_at(this->location(), "expected identifier");
179 std::string name
= token
->identifier();
180 bool is_exported
= token
->is_identifier_exported();
181 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
183 token
= this->advance_token();
184 if (!token
->is_op(OPERATOR_DOT
))
191 Named_object
* package
= this->gogo_
->lookup(name
, NULL
);
192 if (package
== NULL
|| !package
->is_package())
194 go_error_at(this->location(), "expected package");
195 // We expect . IDENTIFIER; skip both.
196 if (this->advance_token()->is_identifier())
197 this->advance_token();
201 package
->package_value()->note_usage(Gogo::unpack_hidden_name(name
));
203 token
= this->advance_token();
204 if (!token
->is_identifier())
206 go_error_at(this->location(), "expected identifier");
210 name
= token
->identifier();
214 go_error_at(this->location(), "invalid use of %<_%>");
215 name
= Gogo::erroneous_name();
218 if (package
->name() == this->gogo_
->package_name())
219 name
= this->gogo_
->pack_hidden_name(name
,
220 token
->is_identifier_exported());
225 this->advance_token();
230 // Type = TypeName | TypeLit | "(" Type ")" .
232 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
233 // SliceType | MapType | ChannelType .
238 const Token
* token
= this->peek_token();
239 if (token
->is_identifier())
240 return this->type_name(true);
241 else if (token
->is_op(OPERATOR_LSQUARE
))
242 return this->array_type(false);
243 else if (token
->is_keyword(KEYWORD_CHAN
)
244 || token
->is_op(OPERATOR_CHANOP
))
245 return this->channel_type();
246 else if (token
->is_keyword(KEYWORD_INTERFACE
))
247 return this->interface_type(true);
248 else if (token
->is_keyword(KEYWORD_FUNC
))
250 Location location
= token
->location();
251 this->advance_token();
252 Type
* type
= this->signature(NULL
, location
);
254 return Type::make_error_type();
257 else if (token
->is_keyword(KEYWORD_MAP
))
258 return this->map_type();
259 else if (token
->is_keyword(KEYWORD_STRUCT
))
260 return this->struct_type();
261 else if (token
->is_op(OPERATOR_MULT
))
262 return this->pointer_type();
263 else if (token
->is_op(OPERATOR_LPAREN
))
265 this->advance_token();
266 Type
* ret
= this->type();
267 if (this->peek_token()->is_op(OPERATOR_RPAREN
))
268 this->advance_token();
271 if (!ret
->is_error_type())
272 go_error_at(this->location(), "expected %<)%>");
278 go_error_at(token
->location(), "expected type");
279 return Type::make_error_type();
284 Parse::type_may_start_here()
286 const Token
* token
= this->peek_token();
287 return (token
->is_identifier()
288 || token
->is_op(OPERATOR_LSQUARE
)
289 || token
->is_op(OPERATOR_CHANOP
)
290 || token
->is_keyword(KEYWORD_CHAN
)
291 || token
->is_keyword(KEYWORD_INTERFACE
)
292 || token
->is_keyword(KEYWORD_FUNC
)
293 || token
->is_keyword(KEYWORD_MAP
)
294 || token
->is_keyword(KEYWORD_STRUCT
)
295 || token
->is_op(OPERATOR_MULT
)
296 || token
->is_op(OPERATOR_LPAREN
));
299 // TypeName = QualifiedIdent .
301 // If MAY_BE_NIL is true, then an identifier with the value of the
302 // predefined constant nil is accepted, returning the nil type.
305 Parse::type_name(bool issue_error
)
307 Location location
= this->location();
310 Named_object
* package
;
311 if (!this->qualified_ident(&name
, &package
))
312 return Type::make_error_type();
314 Named_object
* named_object
;
316 named_object
= this->gogo_
->lookup(name
, NULL
);
319 named_object
= package
->package_value()->lookup(name
);
320 if (named_object
== NULL
322 && package
->name() != this->gogo_
->package_name())
324 // Check whether the name is there but hidden.
325 std::string s
= ('.' + package
->package_value()->pkgpath()
327 named_object
= package
->package_value()->lookup(s
);
328 if (named_object
!= NULL
)
330 Package
* p
= package
->package_value();
331 const std::string
& packname(p
->package_name());
332 go_error_at(location
,
333 "invalid reference to hidden type %<%s.%s%>",
334 Gogo::message_name(packname
).c_str(),
335 Gogo::message_name(name
).c_str());
342 if (named_object
== NULL
)
345 named_object
= this->gogo_
->add_unknown_name(name
, location
);
348 const std::string
& packname(package
->package_value()->package_name());
349 go_error_at(location
, "reference to undefined identifier %<%s.%s%>",
350 Gogo::message_name(packname
).c_str(),
351 Gogo::message_name(name
).c_str());
356 else if (named_object
->is_type())
358 if (!named_object
->type_value()->is_visible())
361 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
369 go_error_at(location
, "expected type");
370 return Type::make_error_type();
373 if (named_object
->is_type())
374 return named_object
->type_value();
375 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
376 return Type::make_forward_declaration(named_object
);
381 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
382 // ArrayLength = Expression .
383 // ElementType = CompleteType .
386 Parse::array_type(bool may_use_ellipsis
)
388 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
389 const Token
* token
= this->advance_token();
391 Expression
* length
= NULL
;
392 if (token
->is_op(OPERATOR_RSQUARE
))
393 this->advance_token();
396 if (!token
->is_op(OPERATOR_ELLIPSIS
))
397 length
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
398 else if (may_use_ellipsis
)
400 // An ellipsis is used in composite literals to represent a
401 // fixed array of the size of the number of elements. We
402 // use a length of nil to represent this, and change the
403 // length when parsing the composite literal.
404 length
= Expression::make_nil(this->location());
405 this->advance_token();
409 go_error_at(this->location(),
410 "use of %<[...]%> outside of array literal");
411 length
= Expression::make_error(this->location());
412 this->advance_token();
414 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
416 go_error_at(this->location(), "expected %<]%>");
417 return Type::make_error_type();
419 this->advance_token();
422 Type
* element_type
= this->type();
423 if (element_type
->is_error_type())
424 return Type::make_error_type();
426 return Type::make_array_type(element_type
, length
);
429 // MapType = "map" "[" KeyType "]" ValueType .
430 // KeyType = CompleteType .
431 // ValueType = CompleteType .
436 Location location
= this->location();
437 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP
));
438 if (!this->advance_token()->is_op(OPERATOR_LSQUARE
))
440 go_error_at(this->location(), "expected %<[%>");
441 return Type::make_error_type();
443 this->advance_token();
445 Type
* key_type
= this->type();
447 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
449 go_error_at(this->location(), "expected %<]%>");
450 return Type::make_error_type();
452 this->advance_token();
454 Type
* value_type
= this->type();
456 if (key_type
->is_error_type() || value_type
->is_error_type())
457 return Type::make_error_type();
459 return Type::make_map_type(key_type
, value_type
, location
);
462 // StructType = "struct" "{" { FieldDecl ";" } "}" .
467 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT
));
468 Location location
= this->location();
469 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
471 Location token_loc
= this->location();
472 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
473 && this->advance_token()->is_op(OPERATOR_LCURLY
))
474 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
477 go_error_at(this->location(), "expected %<{%>");
478 return Type::make_error_type();
481 this->advance_token();
483 Struct_field_list
* sfl
= new Struct_field_list
;
484 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
486 this->field_decl(sfl
);
487 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
488 this->advance_token();
489 else if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
491 go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
492 if (!this->skip_past_error(OPERATOR_RCURLY
))
493 return Type::make_error_type();
496 this->advance_token();
498 for (Struct_field_list::const_iterator pi
= sfl
->begin();
502 if (pi
->type()->is_error_type())
504 for (Struct_field_list::const_iterator pj
= pi
+ 1;
508 if (pi
->field_name() == pj
->field_name()
509 && !Gogo::is_sink_name(pi
->field_name()))
510 go_error_at(pi
->location(), "duplicate field name %<%s%>",
511 Gogo::message_name(pi
->field_name()).c_str());
515 return Type::make_struct_type(sfl
, location
);
518 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
519 // Tag = string_lit .
522 Parse::field_decl(Struct_field_list
* sfl
)
524 const Token
* token
= this->peek_token();
525 Location location
= token
->location();
527 bool is_anonymous_pointer
;
528 if (token
->is_op(OPERATOR_MULT
))
531 is_anonymous_pointer
= true;
533 else if (token
->is_identifier())
535 std::string id
= token
->identifier();
536 bool is_id_exported
= token
->is_identifier_exported();
537 Location id_location
= token
->location();
538 token
= this->advance_token();
539 is_anonymous
= (token
->is_op(OPERATOR_SEMICOLON
)
540 || token
->is_op(OPERATOR_RCURLY
)
541 || token
->is_op(OPERATOR_DOT
)
542 || token
->is_string());
543 is_anonymous_pointer
= false;
544 this->unget_token(Token::make_identifier_token(id
, is_id_exported
,
549 go_error_at(this->location(), "expected field name");
550 this->gogo_
->mark_locals_used();
551 while (!token
->is_op(OPERATOR_SEMICOLON
)
552 && !token
->is_op(OPERATOR_RCURLY
)
554 token
= this->advance_token();
560 if (is_anonymous_pointer
)
562 this->advance_token();
563 if (!this->peek_token()->is_identifier())
565 go_error_at(this->location(), "expected field name");
566 this->gogo_
->mark_locals_used();
567 while (!token
->is_op(OPERATOR_SEMICOLON
)
568 && !token
->is_op(OPERATOR_RCURLY
)
570 token
= this->advance_token();
574 Type
* type
= this->type_name(true);
577 if (this->peek_token()->is_string())
579 tag
= this->peek_token()->string_value();
580 this->advance_token();
583 if (!type
->is_error_type())
585 if (is_anonymous_pointer
)
586 type
= Type::make_pointer_type(type
);
587 sfl
->push_back(Struct_field(Typed_identifier("", type
, location
)));
589 sfl
->back().set_tag(tag
);
594 Typed_identifier_list til
;
597 token
= this->peek_token();
598 if (!token
->is_identifier())
600 go_error_at(this->location(), "expected identifier");
604 this->gogo_
->pack_hidden_name(token
->identifier(),
605 token
->is_identifier_exported());
606 til
.push_back(Typed_identifier(name
, NULL
, token
->location()));
607 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
609 this->advance_token();
612 Type
* type
= this->type();
615 if (this->peek_token()->is_string())
617 tag
= this->peek_token()->string_value();
618 this->advance_token();
621 for (Typed_identifier_list::iterator p
= til
.begin();
626 sfl
->push_back(Struct_field(*p
));
628 sfl
->back().set_tag(tag
);
633 // PointerType = "*" Type .
636 Parse::pointer_type()
638 go_assert(this->peek_token()->is_op(OPERATOR_MULT
));
639 this->advance_token();
640 Type
* type
= this->type();
641 if (type
->is_error_type())
643 return Type::make_pointer_type(type
);
646 // ChannelType = Channel | SendChannel | RecvChannel .
647 // Channel = "chan" ElementType .
648 // SendChannel = "chan" "<-" ElementType .
649 // RecvChannel = "<-" "chan" ElementType .
652 Parse::channel_type()
654 const Token
* token
= this->peek_token();
657 if (token
->is_op(OPERATOR_CHANOP
))
659 if (!this->advance_token()->is_keyword(KEYWORD_CHAN
))
661 go_error_at(this->location(), "expected %<chan%>");
662 return Type::make_error_type();
665 this->advance_token();
669 go_assert(token
->is_keyword(KEYWORD_CHAN
));
670 if (this->advance_token()->is_op(OPERATOR_CHANOP
))
673 this->advance_token();
677 // Better error messages for the common error of omitting the
678 // channel element type.
679 if (!this->type_may_start_here())
681 token
= this->peek_token();
682 if (token
->is_op(OPERATOR_RCURLY
))
683 go_error_at(this->location(), "unexpected %<}%> in channel type");
684 else if (token
->is_op(OPERATOR_RPAREN
))
685 go_error_at(this->location(), "unexpected %<)%> in channel type");
686 else if (token
->is_op(OPERATOR_COMMA
))
687 go_error_at(this->location(), "unexpected comma in channel type");
689 go_error_at(this->location(), "expected channel element type");
690 return Type::make_error_type();
693 Type
* element_type
= this->type();
694 return Type::make_channel_type(send
, receive
, element_type
);
697 // Give an error for a duplicate parameter or receiver name.
700 Parse::check_signature_names(const Typed_identifier_list
* params
,
703 for (Typed_identifier_list::const_iterator p
= params
->begin();
707 if (p
->name().empty() || Gogo::is_sink_name(p
->name()))
709 std::pair
<std::string
, const Typed_identifier
*> val
=
710 std::make_pair(p
->name(), &*p
);
711 std::pair
<Parse::Names::iterator
, bool> ins
= names
->insert(val
);
714 go_error_at(p
->location(), "redefinition of %qs",
715 Gogo::message_name(p
->name()).c_str());
716 go_inform(ins
.first
->second
->location(),
717 "previous definition of %qs was here",
718 Gogo::message_name(p
->name()).c_str());
723 // Signature = Parameters [ Result ] .
725 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
726 // location of the start of the type.
728 // This returns NULL on a parse error.
731 Parse::signature(Typed_identifier
* receiver
, Location location
)
733 bool is_varargs
= false;
734 Typed_identifier_list
* params
;
735 bool params_ok
= this->parameters(¶ms
, &is_varargs
);
737 Typed_identifier_list
* results
= NULL
;
738 if (this->peek_token()->is_op(OPERATOR_LPAREN
)
739 || this->type_may_start_here())
741 if (!this->result(&results
))
749 if (receiver
!= NULL
)
750 names
[receiver
->name()] = receiver
;
752 this->check_signature_names(params
, &names
);
754 this->check_signature_names(results
, &names
);
756 Function_type
* ret
= Type::make_function_type(receiver
, params
, results
,
759 ret
->set_is_varargs();
763 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
765 // This returns false on a parse error.
768 Parse::parameters(Typed_identifier_list
** pparams
, bool* is_varargs
)
772 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
774 go_error_at(this->location(), "expected %<(%>");
778 Typed_identifier_list
* params
= NULL
;
779 bool saw_error
= false;
781 const Token
* token
= this->advance_token();
782 if (!token
->is_op(OPERATOR_RPAREN
))
784 params
= this->parameter_list(is_varargs
);
787 token
= this->peek_token();
790 // The optional trailing comma is picked up in parameter_list.
792 if (!token
->is_op(OPERATOR_RPAREN
))
794 go_error_at(this->location(), "expected %<)%>");
797 this->advance_token();
806 // ParameterList = ParameterDecl { "," ParameterDecl } .
808 // This sets *IS_VARARGS if the list ends with an ellipsis.
809 // IS_VARARGS will be NULL if varargs are not permitted.
811 // We pick up an optional trailing comma.
813 // This returns NULL if some error is seen.
815 Typed_identifier_list
*
816 Parse::parameter_list(bool* is_varargs
)
818 Location location
= this->location();
819 Typed_identifier_list
* ret
= new Typed_identifier_list();
821 bool saw_error
= false;
823 // If we see an identifier and then a comma, then we don't know
824 // whether we are looking at a list of identifiers followed by a
825 // type, or a list of types given by name. We have to do an
826 // arbitrary lookahead to figure it out.
828 bool parameters_have_names
;
829 const Token
* token
= this->peek_token();
830 if (!token
->is_identifier())
832 // This must be a type which starts with something like '*'.
833 parameters_have_names
= false;
837 std::string name
= token
->identifier();
838 bool is_exported
= token
->is_identifier_exported();
839 Location location
= token
->location();
840 token
= this->advance_token();
841 if (!token
->is_op(OPERATOR_COMMA
))
843 if (token
->is_op(OPERATOR_DOT
))
845 // This is a qualified identifier, which must turn out
847 parameters_have_names
= false;
849 else if (token
->is_op(OPERATOR_RPAREN
))
851 // A single identifier followed by a parenthesis must be
853 parameters_have_names
= false;
857 // An identifier followed by something other than a
858 // comma or a dot or a right parenthesis must be a
859 // parameter name followed by a type.
860 parameters_have_names
= true;
863 this->unget_token(Token::make_identifier_token(name
, is_exported
,
868 // An identifier followed by a comma may be the first in a
869 // list of parameter names followed by a type, or it may be
870 // the first in a list of types without parameter names. To
871 // find out we gather as many identifiers separated by
873 std::string id_name
= this->gogo_
->pack_hidden_name(name
,
875 ret
->push_back(Typed_identifier(id_name
, NULL
, location
));
876 bool just_saw_comma
= true;
877 while (this->advance_token()->is_identifier())
879 name
= this->peek_token()->identifier();
880 is_exported
= this->peek_token()->is_identifier_exported();
881 location
= this->peek_token()->location();
882 id_name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
883 ret
->push_back(Typed_identifier(id_name
, NULL
, location
));
884 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
886 just_saw_comma
= false;
893 // We saw ID1 "," ID2 "," followed by something which
894 // was not an identifier. We must be seeing the start
895 // of a type, and ID1 and ID2 must be types, and the
896 // parameters don't have names.
897 parameters_have_names
= false;
899 else if (this->peek_token()->is_op(OPERATOR_RPAREN
))
901 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
902 // and the parameters don't have names.
903 parameters_have_names
= false;
905 else if (this->peek_token()->is_op(OPERATOR_DOT
))
907 // We saw ID1 "," ID2 ".". ID2 must be a package name,
908 // ID1 must be a type, and the parameters don't have
910 parameters_have_names
= false;
911 this->unget_token(Token::make_identifier_token(name
, is_exported
,
914 just_saw_comma
= true;
918 // We saw ID1 "," ID2 followed by something other than
919 // ",", ".", or ")". We must be looking at the start of
920 // a type, and ID1 and ID2 must be parameter names.
921 parameters_have_names
= true;
924 if (parameters_have_names
)
926 go_assert(!just_saw_comma
);
927 // We have just seen ID1, ID2 xxx.
929 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
933 go_error_at(this->location(),
934 "%<...%> only permits one name");
936 this->advance_token();
939 for (size_t i
= 0; i
< ret
->size(); ++i
)
940 ret
->set_type(i
, type
);
941 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
942 return saw_error
? NULL
: ret
;
943 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
944 return saw_error
? NULL
: ret
;
948 Typed_identifier_list
* tret
= new Typed_identifier_list();
949 for (Typed_identifier_list::const_iterator p
= ret
->begin();
953 Named_object
* no
= this->gogo_
->lookup(p
->name(), NULL
);
956 no
= this->gogo_
->add_unknown_name(p
->name(),
960 type
= no
->type_value();
961 else if (no
->is_unknown() || no
->is_type_declaration())
962 type
= Type::make_forward_declaration(no
);
965 go_error_at(p
->location(), "expected %<%s%> to be a type",
966 Gogo::message_name(p
->name()).c_str());
968 type
= Type::make_error_type();
970 tret
->push_back(Typed_identifier("", type
, p
->location()));
975 || this->peek_token()->is_op(OPERATOR_RPAREN
))
976 return saw_error
? NULL
: ret
;
981 bool mix_error
= false;
982 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
984 while (this->peek_token()->is_op(OPERATOR_COMMA
))
986 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
988 if (is_varargs
!= NULL
&& *is_varargs
)
990 go_error_at(this->location(), "%<...%> must be last parameter");
993 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
998 go_error_at(location
, "invalid named/anonymous mix");
1009 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1012 Parse::parameter_decl(bool parameters_have_names
,
1013 Typed_identifier_list
* til
,
1018 if (!parameters_have_names
)
1021 Location location
= this->location();
1022 if (!this->peek_token()->is_identifier())
1024 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1025 type
= this->type();
1028 if (is_varargs
== NULL
)
1029 go_error_at(this->location(), "invalid use of %<...%>");
1032 this->advance_token();
1033 if (is_varargs
== NULL
1034 && this->peek_token()->is_op(OPERATOR_RPAREN
))
1035 type
= Type::make_error_type();
1038 Type
* element_type
= this->type();
1039 type
= Type::make_array_type(element_type
, NULL
);
1045 type
= this->type_name(false);
1046 if (type
->is_error_type()
1047 || (!this->peek_token()->is_op(OPERATOR_COMMA
)
1048 && !this->peek_token()->is_op(OPERATOR_RPAREN
)))
1051 while (!this->peek_token()->is_op(OPERATOR_COMMA
)
1052 && !this->peek_token()->is_op(OPERATOR_RPAREN
)
1053 && !this->peek_token()->is_eof())
1054 this->advance_token();
1057 if (!type
->is_error_type())
1058 til
->push_back(Typed_identifier("", type
, location
));
1064 size_t orig_count
= til
->size();
1065 if (this->peek_token()->is_identifier())
1066 this->identifier_list(til
);
1069 size_t new_count
= til
->size();
1072 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1073 type
= this->type();
1076 if (is_varargs
== NULL
)
1078 go_error_at(this->location(), "invalid use of %<...%>");
1081 else if (new_count
> orig_count
+ 1)
1083 go_error_at(this->location(), "%<...%> only permits one name");
1088 this->advance_token();
1089 Type
* element_type
= this->type();
1090 type
= Type::make_array_type(element_type
, NULL
);
1092 for (size_t i
= orig_count
; i
< new_count
; ++i
)
1093 til
->set_type(i
, type
);
1097 // Result = Parameters | Type .
1099 // This returns false on a parse error.
1102 Parse::result(Typed_identifier_list
** presults
)
1104 if (this->peek_token()->is_op(OPERATOR_LPAREN
))
1105 return this->parameters(presults
, NULL
);
1108 Location location
= this->location();
1109 Type
* type
= this->type();
1110 if (type
->is_error_type())
1115 Typed_identifier_list
* til
= new Typed_identifier_list();
1116 til
->push_back(Typed_identifier("", type
, location
));
1122 // Block = "{" [ StatementList ] "}" .
1124 // Returns the location of the closing brace.
1129 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
1131 Location loc
= this->location();
1132 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1133 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1134 go_error_at(loc
, "unexpected semicolon or newline before %<{%>");
1137 go_error_at(this->location(), "expected %<{%>");
1138 return Linemap::unknown_location();
1142 const Token
* token
= this->advance_token();
1144 if (!token
->is_op(OPERATOR_RCURLY
))
1146 this->statement_list();
1147 token
= this->peek_token();
1148 if (!token
->is_op(OPERATOR_RCURLY
))
1150 if (!token
->is_eof() || !saw_errors())
1151 go_error_at(this->location(), "expected %<}%>");
1153 this->gogo_
->mark_locals_used();
1155 // Skip ahead to the end of the block, in hopes of avoiding
1156 // lots of meaningless errors.
1157 Location ret
= token
->location();
1159 while (!token
->is_eof())
1161 if (token
->is_op(OPERATOR_LCURLY
))
1163 else if (token
->is_op(OPERATOR_RCURLY
))
1168 this->advance_token();
1172 token
= this->advance_token();
1173 ret
= token
->location();
1179 Location ret
= token
->location();
1180 this->advance_token();
1184 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1185 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1188 Parse::interface_type(bool record
)
1190 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE
));
1191 Location location
= this->location();
1193 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
1195 Location token_loc
= this->location();
1196 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1197 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1198 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
1201 go_error_at(this->location(), "expected %<{%>");
1202 return Type::make_error_type();
1205 this->advance_token();
1207 Typed_identifier_list
* methods
= new Typed_identifier_list();
1208 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1210 this->method_spec(methods
);
1211 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1213 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
1215 this->method_spec(methods
);
1217 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1219 go_error_at(this->location(), "expected %<}%>");
1220 while (!this->advance_token()->is_op(OPERATOR_RCURLY
))
1222 if (this->peek_token()->is_eof())
1223 return Type::make_error_type();
1227 this->advance_token();
1229 if (methods
->empty())
1235 Interface_type
* ret
;
1236 if (methods
== NULL
)
1237 ret
= Type::make_empty_interface_type(location
);
1239 ret
= Type::make_interface_type(methods
, location
);
1241 this->gogo_
->record_interface_type(ret
);
1245 // MethodSpec = MethodName Signature | InterfaceTypeName .
1246 // MethodName = identifier .
1247 // InterfaceTypeName = TypeName .
1250 Parse::method_spec(Typed_identifier_list
* methods
)
1252 const Token
* token
= this->peek_token();
1253 if (!token
->is_identifier())
1255 go_error_at(this->location(), "expected identifier");
1259 std::string name
= token
->identifier();
1260 bool is_exported
= token
->is_identifier_exported();
1261 Location location
= token
->location();
1263 if (this->advance_token()->is_op(OPERATOR_LPAREN
))
1265 // This is a MethodName.
1267 go_error_at(this->location(),
1268 "methods must have a unique non-blank name");
1269 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1270 Type
* type
= this->signature(NULL
, location
);
1273 methods
->push_back(Typed_identifier(name
, type
, location
));
1277 this->unget_token(Token::make_identifier_token(name
, is_exported
,
1279 Type
* type
= this->type_name(false);
1280 if (type
->is_error_type()
1281 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1282 && !this->peek_token()->is_op(OPERATOR_RCURLY
)))
1284 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1285 go_error_at(this->location(),
1286 "name list not allowed in interface type");
1288 go_error_at(location
, "expected signature or type name");
1289 this->gogo_
->mark_locals_used();
1290 token
= this->peek_token();
1291 while (!token
->is_eof()
1292 && !token
->is_op(OPERATOR_SEMICOLON
)
1293 && !token
->is_op(OPERATOR_RCURLY
))
1294 token
= this->advance_token();
1297 // This must be an interface type, but we can't check that now.
1298 // We check it and pull out the methods in
1299 // Interface_type::do_verify.
1300 methods
->push_back(Typed_identifier("", type
, location
));
1304 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1307 Parse::declaration()
1309 const Token
* token
= this->peek_token();
1311 unsigned int pragmas
= this->lex_
->get_and_clear_pragmas();
1313 && !token
->is_keyword(KEYWORD_FUNC
)
1314 && !token
->is_keyword(KEYWORD_TYPE
))
1315 go_warning_at(token
->location(), 0,
1316 "ignoring magic comment before non-function");
1318 if (token
->is_keyword(KEYWORD_CONST
))
1320 else if (token
->is_keyword(KEYWORD_TYPE
))
1321 this->type_decl(pragmas
);
1322 else if (token
->is_keyword(KEYWORD_VAR
))
1324 else if (token
->is_keyword(KEYWORD_FUNC
))
1325 this->function_decl(pragmas
);
1328 go_error_at(this->location(), "expected declaration");
1329 this->advance_token();
1334 Parse::declaration_may_start_here()
1336 const Token
* token
= this->peek_token();
1337 return (token
->is_keyword(KEYWORD_CONST
)
1338 || token
->is_keyword(KEYWORD_TYPE
)
1339 || token
->is_keyword(KEYWORD_VAR
)
1340 || token
->is_keyword(KEYWORD_FUNC
));
1343 // Decl<P> = P | "(" [ List<P> ] ")" .
1346 Parse::decl(void (Parse::*pfn
)(void*, unsigned int), void* varg
,
1347 unsigned int pragmas
)
1349 if (this->peek_token()->is_eof())
1352 go_error_at(this->location(), "unexpected end of file");
1356 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1357 (this->*pfn
)(varg
, pragmas
);
1361 go_warning_at(this->location(), 0,
1362 "ignoring magic //go:... comment before group");
1363 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1365 this->list(pfn
, varg
, true);
1366 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1368 go_error_at(this->location(), "missing %<)%>");
1369 while (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1371 if (this->peek_token()->is_eof())
1376 this->advance_token();
1380 // List<P> = P { ";" P } [ ";" ] .
1382 // In order to pick up the trailing semicolon we need to know what
1383 // might follow. This is either a '}' or a ')'.
1386 Parse::list(void (Parse::*pfn
)(void*, unsigned int), void* varg
,
1387 bool follow_is_paren
)
1389 (this->*pfn
)(varg
, 0);
1390 Operator follow
= follow_is_paren
? OPERATOR_RPAREN
: OPERATOR_RCURLY
;
1391 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1392 || this->peek_token()->is_op(OPERATOR_COMMA
))
1394 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1395 go_error_at(this->location(), "unexpected comma");
1396 if (this->advance_token()->is_op(follow
))
1398 (this->*pfn
)(varg
, 0);
1402 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1407 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST
));
1408 this->advance_token();
1411 Type
* last_type
= NULL
;
1412 Expression_list
* last_expr_list
= NULL
;
1414 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1415 this->const_spec(iota
, &last_type
, &last_expr_list
);
1418 this->advance_token();
1419 while (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1421 this->const_spec(iota
, &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(int iota
, 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
, iota
);
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
, iota
);
1504 no
->const_value()->set_is_sink();
1507 if (pe
!= expr_list
->end())
1508 go_error_at(this->location(), "too many initializers");
1513 // TypeDecl = "type" Decl<TypeSpec> .
1516 Parse::type_decl(unsigned int pragmas
)
1518 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE
));
1519 this->advance_token();
1520 this->decl(&Parse::type_spec
, NULL
, pragmas
);
1523 // TypeSpec = identifier ["="] Type .
1526 Parse::type_spec(void*, unsigned int pragmas
)
1528 const Token
* token
= this->peek_token();
1529 if (!token
->is_identifier())
1531 go_error_at(this->location(), "expected identifier");
1534 std::string name
= token
->identifier();
1535 bool is_exported
= token
->is_identifier_exported();
1536 Location location
= token
->location();
1537 token
= this->advance_token();
1539 bool is_alias
= false;
1540 if (token
->is_op(OPERATOR_EQ
))
1543 token
= this->advance_token();
1546 // The scope of the type name starts at the point where the
1547 // identifier appears in the source code. We implement this by
1548 // declaring the type before we read the type definition.
1549 Named_object
* named_type
= NULL
;
1552 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1553 named_type
= this->gogo_
->declare_type(name
, location
);
1557 if (name
== "_" && token
->is_keyword(KEYWORD_INTERFACE
))
1559 // We call Parse::interface_type explicity here because we do not want
1560 // to record an interface with a blank type name.
1561 type
= this->interface_type(false);
1563 else if (!token
->is_op(OPERATOR_SEMICOLON
))
1564 type
= this->type();
1567 go_error_at(this->location(),
1568 "unexpected semicolon or newline in type declaration");
1569 type
= Type::make_error_type();
1570 this->advance_token();
1573 if (type
->is_error_type())
1575 this->gogo_
->mark_locals_used();
1576 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1577 && !this->peek_token()->is_eof())
1578 this->advance_token();
1583 if (named_type
->is_type_declaration())
1585 Type
* ftype
= type
->forwarded();
1586 if (ftype
->forward_declaration_type() != NULL
1587 && (ftype
->forward_declaration_type()->named_object()
1590 go_error_at(location
, "invalid recursive type");
1591 type
= Type::make_error_type();
1594 Named_type
* nt
= Type::make_named_type(named_type
, type
, location
);
1598 this->gogo_
->define_type(named_type
, nt
);
1599 go_assert(named_type
->package() == NULL
);
1601 if ((pragmas
& GOPRAGMA_NOTINHEAP
) != 0)
1603 nt
->set_not_in_heap();
1604 pragmas
&= ~GOPRAGMA_NOTINHEAP
;
1607 go_warning_at(location
, 0,
1608 "ignoring magic //go:... comment before type");
1612 // This will probably give a redefinition error.
1613 this->gogo_
->add_type(name
, type
, location
);
1618 // VarDecl = "var" Decl<VarSpec> .
1623 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR
));
1624 this->advance_token();
1625 this->decl(&Parse::var_spec
, NULL
, 0);
1628 // VarSpec = IdentifierList
1629 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1632 Parse::var_spec(void*, unsigned int pragmas
)
1635 go_warning_at(this->location(), 0,
1636 "ignoring magic //go:... comment before var");
1638 // Get the variable names.
1639 Typed_identifier_list til
;
1640 this->identifier_list(&til
);
1642 Location location
= this->location();
1645 Expression_list
* init
= NULL
;
1646 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1648 type
= this->type();
1649 if (type
->is_error_type())
1651 this->gogo_
->mark_locals_used();
1652 while (!this->peek_token()->is_op(OPERATOR_EQ
)
1653 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1654 && !this->peek_token()->is_eof())
1655 this->advance_token();
1657 if (this->peek_token()->is_op(OPERATOR_EQ
))
1659 this->advance_token();
1660 init
= this->expression_list(NULL
, false, true);
1665 this->advance_token();
1666 init
= this->expression_list(NULL
, false, true);
1669 this->init_vars(&til
, type
, init
, false, location
);
1675 // Create variables. TIL is a list of variable names. If TYPE is not
1676 // NULL, it is the type of all the variables. If INIT is not NULL, it
1677 // is an initializer list for the variables.
1680 Parse::init_vars(const Typed_identifier_list
* til
, Type
* type
,
1681 Expression_list
* init
, bool is_coloneq
,
1684 // Check for an initialization which can yield multiple values.
1685 if (init
!= NULL
&& init
->size() == 1 && til
->size() > 1)
1687 if (this->init_vars_from_call(til
, type
, *init
->begin(), is_coloneq
,
1690 if (this->init_vars_from_map(til
, type
, *init
->begin(), is_coloneq
,
1693 if (this->init_vars_from_receive(til
, type
, *init
->begin(), is_coloneq
,
1696 if (this->init_vars_from_type_guard(til
, type
, *init
->begin(),
1697 is_coloneq
, location
))
1701 if (init
!= NULL
&& init
->size() != til
->size())
1703 if (init
->empty() || !init
->front()->is_error_expression())
1704 go_error_at(location
, "wrong number of initializations");
1707 type
= Type::make_error_type();
1710 // Note that INIT was already parsed with the old name bindings, so
1711 // we don't have to worry that it will accidentally refer to the
1712 // newly declared variables. But we do have to worry about a mix of
1713 // newly declared variables and old variables if the old variables
1714 // appear in the initializations.
1716 Expression_list::const_iterator pexpr
;
1718 pexpr
= init
->begin();
1719 bool any_new
= false;
1720 Expression_list
* vars
= new Expression_list();
1721 Expression_list
* vals
= new Expression_list();
1722 for (Typed_identifier_list::const_iterator p
= til
->begin();
1727 go_assert(pexpr
!= init
->end());
1728 this->init_var(*p
, type
, init
== NULL
? NULL
: *pexpr
, is_coloneq
,
1729 false, &any_new
, vars
, vals
);
1734 go_assert(pexpr
== init
->end());
1735 if (is_coloneq
&& !any_new
)
1736 go_error_at(location
, "variables redeclared but no variable is new");
1737 this->finish_init_vars(vars
, vals
, location
);
1740 // See if we need to initialize a list of variables from a function
1741 // call. This returns true if we have set up the variables and the
1745 Parse::init_vars_from_call(const Typed_identifier_list
* vars
, Type
* type
,
1746 Expression
* expr
, bool is_coloneq
,
1749 Call_expression
* call
= expr
->call_expression();
1753 // This is a function call. We can't check here whether it returns
1754 // the right number of values, but it might. Declare the variables,
1755 // and then assign the results of the call to them.
1757 call
->set_expected_result_count(vars
->size());
1759 Named_object
* first_var
= NULL
;
1760 unsigned int index
= 0;
1761 bool any_new
= false;
1762 Expression_list
* ivars
= new Expression_list();
1763 Expression_list
* ivals
= new Expression_list();
1764 for (Typed_identifier_list::const_iterator pv
= vars
->begin();
1768 Expression
* init
= Expression::make_call_result(call
, index
);
1769 Named_object
* no
= this->init_var(*pv
, type
, init
, is_coloneq
, false,
1770 &any_new
, ivars
, ivals
);
1772 if (this->gogo_
->in_global_scope() && no
->is_variable())
1774 if (first_var
== NULL
)
1778 // If the current object is a redefinition of another object, we
1779 // might have already recorded the dependency relationship between
1780 // it and the first variable. Either way, an error will be
1781 // reported for the redefinition and we don't need to properly
1782 // record dependency information for an invalid program.
1783 if (no
->is_redefinition())
1786 // The subsequent vars have an implicit dependency on
1787 // the first one, so that everything gets initialized in
1788 // the right order and so that we detect cycles
1790 this->gogo_
->record_var_depends_on(no
->var_value(), first_var
);
1795 if (is_coloneq
&& !any_new
)
1796 go_error_at(location
, "variables redeclared but no variable is new");
1798 this->finish_init_vars(ivars
, ivals
, location
);
1803 // See if we need to initialize a pair of values from a map index
1804 // expression. This returns true if we have set up the variables and
1805 // the initialization.
1808 Parse::init_vars_from_map(const Typed_identifier_list
* vars
, Type
* type
,
1809 Expression
* expr
, bool is_coloneq
,
1812 Index_expression
* index
= expr
->index_expression();
1815 if (vars
->size() != 2)
1818 // This is an index which is being assigned to two variables. It
1819 // must be a map index. Declare the variables, and then assign the
1820 // results of the map index.
1821 bool any_new
= false;
1822 Typed_identifier_list::const_iterator p
= vars
->begin();
1823 Expression
* init
= type
== NULL
? index
: NULL
;
1824 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1825 type
== NULL
, &any_new
, NULL
, NULL
);
1826 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1827 val_no
->var_value()->set_type_from_init_tuple();
1828 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1831 Type
* var_type
= type
;
1832 if (var_type
== NULL
)
1833 var_type
= Type::lookup_bool_type();
1834 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1835 &any_new
, NULL
, NULL
);
1836 Expression
* present_var
= Expression::make_var_reference(no
, location
);
1838 if (is_coloneq
&& !any_new
)
1839 go_error_at(location
, "variables redeclared but no variable is new");
1841 Statement
* s
= Statement::make_tuple_map_assignment(val_var
, present_var
,
1844 if (!this->gogo_
->in_global_scope())
1845 this->gogo_
->add_statement(s
);
1846 else if (!val_no
->is_sink())
1848 if (val_no
->is_variable())
1849 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1851 else if (!no
->is_sink())
1853 if (no
->is_variable())
1854 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1858 // Execute the map index expression just so that we can fail if
1860 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1862 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1868 // See if we need to initialize a pair of values from a receive
1869 // expression. This returns true if we have set up the variables and
1870 // the initialization.
1873 Parse::init_vars_from_receive(const Typed_identifier_list
* vars
, Type
* type
,
1874 Expression
* expr
, bool is_coloneq
,
1877 Receive_expression
* receive
= expr
->receive_expression();
1878 if (receive
== NULL
)
1880 if (vars
->size() != 2)
1883 // This is a receive expression which is being assigned to two
1884 // variables. Declare the variables, and then assign the results of
1886 bool any_new
= false;
1887 Typed_identifier_list::const_iterator p
= vars
->begin();
1888 Expression
* init
= type
== NULL
? receive
: NULL
;
1889 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1890 type
== NULL
, &any_new
, NULL
, NULL
);
1891 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1892 val_no
->var_value()->set_type_from_init_tuple();
1893 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1896 Type
* var_type
= type
;
1897 if (var_type
== NULL
)
1898 var_type
= Type::lookup_bool_type();
1899 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1900 &any_new
, NULL
, NULL
);
1901 Expression
* received_var
= Expression::make_var_reference(no
, location
);
1903 if (is_coloneq
&& !any_new
)
1904 go_error_at(location
, "variables redeclared but no variable is new");
1906 Statement
* s
= Statement::make_tuple_receive_assignment(val_var
,
1911 if (!this->gogo_
->in_global_scope())
1912 this->gogo_
->add_statement(s
);
1913 else if (!val_no
->is_sink())
1915 if (val_no
->is_variable())
1916 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1918 else if (!no
->is_sink())
1920 if (no
->is_variable())
1921 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1925 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1927 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1933 // See if we need to initialize a pair of values from a type guard
1934 // expression. This returns true if we have set up the variables and
1935 // the initialization.
1938 Parse::init_vars_from_type_guard(const Typed_identifier_list
* vars
,
1939 Type
* type
, Expression
* expr
,
1940 bool is_coloneq
, Location location
)
1942 Type_guard_expression
* type_guard
= expr
->type_guard_expression();
1943 if (type_guard
== NULL
)
1945 if (vars
->size() != 2)
1948 // This is a type guard expression which is being assigned to two
1949 // variables. Declare the variables, and then assign the results of
1951 bool any_new
= false;
1952 Typed_identifier_list::const_iterator p
= vars
->begin();
1953 Type
* var_type
= type
;
1954 if (var_type
== NULL
)
1955 var_type
= type_guard
->type();
1956 Named_object
* val_no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1957 &any_new
, NULL
, NULL
);
1958 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1962 if (var_type
== NULL
)
1963 var_type
= Type::lookup_bool_type();
1964 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1965 &any_new
, NULL
, NULL
);
1966 Expression
* ok_var
= Expression::make_var_reference(no
, location
);
1968 Expression
* texpr
= type_guard
->expr();
1969 Type
* t
= type_guard
->type();
1970 Statement
* s
= Statement::make_tuple_type_guard_assignment(val_var
, ok_var
,
1974 if (is_coloneq
&& !any_new
)
1975 go_error_at(location
, "variables redeclared but no variable is new");
1977 if (!this->gogo_
->in_global_scope())
1978 this->gogo_
->add_statement(s
);
1979 else if (!val_no
->is_sink())
1981 if (val_no
->is_variable())
1982 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1984 else if (!no
->is_sink())
1986 if (no
->is_variable())
1987 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1991 Named_object
* dummy
= this->create_dummy_global(type
, NULL
, location
);
1992 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1998 // Create a single variable. If IS_COLONEQ is true, we permit
1999 // redeclarations in the same block, and we set *IS_NEW when we find a
2000 // new variable which is not a redeclaration.
2003 Parse::init_var(const Typed_identifier
& tid
, Type
* type
, Expression
* init
,
2004 bool is_coloneq
, bool type_from_init
, bool* is_new
,
2005 Expression_list
* vars
, Expression_list
* vals
)
2007 Location location
= tid
.location();
2009 if (Gogo::is_sink_name(tid
.name()))
2011 if (!type_from_init
&& init
!= NULL
)
2013 if (this->gogo_
->in_global_scope())
2014 return this->create_dummy_global(type
, init
, location
);
2017 // Create a dummy variable so that we will check whether the
2018 // initializer can be assigned to the type.
2019 Variable
* var
= new Variable(type
, init
, false, false, false,
2024 snprintf(buf
, sizeof buf
, "sink$%d", count
);
2026 return this->gogo_
->add_variable(buf
, var
);
2030 this->gogo_
->add_type_to_verify(type
);
2031 return this->gogo_
->add_sink();
2036 Named_object
* no
= this->gogo_
->lookup_in_block(tid
.name());
2038 && (no
->is_variable() || no
->is_result_variable()))
2040 // INIT may be NULL even when IS_COLONEQ is true for cases
2041 // like v, ok := x.(int).
2042 if (!type_from_init
&& init
!= NULL
)
2044 go_assert(vars
!= NULL
&& vals
!= NULL
);
2045 vars
->push_back(Expression::make_var_reference(no
, location
));
2046 vals
->push_back(init
);
2052 Variable
* var
= new Variable(type
, init
, this->gogo_
->in_global_scope(),
2053 false, false, location
);
2054 Named_object
* no
= this->gogo_
->add_variable(tid
.name(), var
);
2055 if (!no
->is_variable())
2057 // The name is already defined, so we just gave an error.
2058 return this->gogo_
->add_sink();
2063 // Create a dummy global variable to force an initializer to be run in
2064 // the right place. This is used when a sink variable is initialized
2068 Parse::create_dummy_global(Type
* type
, Expression
* init
,
2071 if (type
== NULL
&& init
== NULL
)
2072 type
= Type::lookup_bool_type();
2073 Variable
* var
= new Variable(type
, init
, true, false, false, location
);
2076 snprintf(buf
, sizeof buf
, "_.%d", count
);
2078 return this->gogo_
->add_variable(buf
, var
);
2081 // Finish the variable initialization by executing any assignments to
2082 // existing variables when using :=. These must be done as a tuple
2083 // assignment in case of something like n, a, b := 1, b, a.
2086 Parse::finish_init_vars(Expression_list
* vars
, Expression_list
* vals
,
2094 else if (vars
->size() == 1)
2096 go_assert(!this->gogo_
->in_global_scope());
2097 this->gogo_
->add_statement(Statement::make_assignment(vars
->front(),
2105 go_assert(!this->gogo_
->in_global_scope());
2106 this->gogo_
->add_statement(Statement::make_tuple_assignment(vars
, vals
,
2111 // SimpleVarDecl = identifier ":=" Expression .
2113 // We've already seen the identifier.
2115 // FIXME: We also have to implement
2116 // IdentifierList ":=" ExpressionList
2117 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2118 // tuple assignments here as well.
2120 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2121 // side may be a composite literal.
2123 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2126 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2127 // guard (var := expr.("type") using the literal keyword "type").
2130 Parse::simple_var_decl_or_assignment(const std::string
& name
,
2132 bool may_be_composite_lit
,
2133 Range_clause
* p_range_clause
,
2134 Type_switch
* p_type_switch
)
2136 Typed_identifier_list til
;
2137 til
.push_back(Typed_identifier(name
, NULL
, location
));
2139 std::set
<std::string
> uniq_idents
;
2140 uniq_idents
.insert(name
);
2141 std::string dup_name
;
2144 // We've seen one identifier. If we see a comma now, this could be
2146 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2148 go_assert(p_type_switch
== NULL
);
2151 const Token
* token
= this->advance_token();
2152 if (!token
->is_identifier())
2155 std::string id
= token
->identifier();
2156 bool is_id_exported
= token
->is_identifier_exported();
2157 Location id_location
= token
->location();
2158 std::pair
<std::set
<std::string
>::iterator
, bool> ins
;
2160 token
= this->advance_token();
2161 if (!token
->is_op(OPERATOR_COMMA
))
2163 if (token
->is_op(OPERATOR_COLONEQ
))
2165 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2166 ins
= uniq_idents
.insert(id
);
2167 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2168 go_error_at(id_location
, "multiple assignments to %s",
2169 Gogo::message_name(id
).c_str());
2170 til
.push_back(Typed_identifier(id
, NULL
, location
));
2173 this->unget_token(Token::make_identifier_token(id
,
2179 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2180 ins
= uniq_idents
.insert(id
);
2181 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2183 dup_name
= Gogo::message_name(id
);
2184 dup_loc
= id_location
;
2186 til
.push_back(Typed_identifier(id
, NULL
, location
));
2189 // We have a comma separated list of identifiers in TIL. If the
2190 // next token is COLONEQ, then this is a simple var decl, and we
2191 // have the complete list of identifiers. If the next token is
2192 // not COLONEQ, then the only valid parse is a tuple assignment.
2193 // The list of identifiers we have so far is really a list of
2194 // expressions. There are more expressions following.
2196 if (!this->peek_token()->is_op(OPERATOR_COLONEQ
))
2198 Expression_list
* exprs
= new Expression_list
;
2199 for (Typed_identifier_list::const_iterator p
= til
.begin();
2202 exprs
->push_back(this->id_to_expression(p
->name(), p
->location(),
2205 Expression_list
* more_exprs
=
2206 this->expression_list(NULL
, true, may_be_composite_lit
);
2207 for (Expression_list::const_iterator p
= more_exprs
->begin();
2208 p
!= more_exprs
->end();
2210 exprs
->push_back(*p
);
2213 this->tuple_assignment(exprs
, may_be_composite_lit
, p_range_clause
);
2218 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ
));
2219 const Token
* token
= this->advance_token();
2221 if (!dup_name
.empty())
2222 go_error_at(dup_loc
, "multiple assignments to %s", dup_name
.c_str());
2224 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
2226 this->range_clause_decl(&til
, p_range_clause
);
2230 Expression_list
* init
;
2231 if (p_type_switch
== NULL
)
2232 init
= this->expression_list(NULL
, false, may_be_composite_lit
);
2235 bool is_type_switch
= false;
2236 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false,
2237 may_be_composite_lit
,
2238 &is_type_switch
, NULL
);
2241 p_type_switch
->found
= true;
2242 p_type_switch
->name
= name
;
2243 p_type_switch
->location
= location
;
2244 p_type_switch
->expr
= expr
;
2248 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
2250 init
= new Expression_list();
2251 init
->push_back(expr
);
2255 this->advance_token();
2256 init
= this->expression_list(expr
, false, may_be_composite_lit
);
2260 this->init_vars(&til
, NULL
, init
, true, location
);
2263 // FunctionDecl = "func" identifier Signature [ Block ] .
2264 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2266 // Deprecated gcc extension:
2267 // FunctionDecl = "func" identifier Signature
2268 // __asm__ "(" string_lit ")" .
2269 // This extension means a function whose real name is the identifier
2270 // inside the asm. This extension will be removed at some future
2271 // date. It has been replaced with //extern or //go:linkname comments.
2273 // PRAGMAS is a bitset of magic comments.
2276 Parse::function_decl(unsigned int pragmas
)
2278 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2279 Location location
= this->location();
2280 std::string extern_name
= this->lex_
->extern_name();
2281 const Token
* token
= this->advance_token();
2283 bool expected_receiver
= false;
2284 Typed_identifier
* rec
= NULL
;
2285 if (token
->is_op(OPERATOR_LPAREN
))
2287 expected_receiver
= true;
2288 rec
= this->receiver();
2289 token
= this->peek_token();
2292 if (!token
->is_identifier())
2294 go_error_at(this->location(), "expected function name");
2299 this->gogo_
->pack_hidden_name(token
->identifier(),
2300 token
->is_identifier_exported());
2302 this->advance_token();
2304 Function_type
* fntype
= this->signature(rec
, this->location());
2306 Named_object
* named_object
= NULL
;
2308 if (this->peek_token()->is_keyword(KEYWORD_ASM
))
2310 if (!this->advance_token()->is_op(OPERATOR_LPAREN
))
2312 go_error_at(this->location(), "expected %<(%>");
2315 token
= this->advance_token();
2316 if (!token
->is_string())
2318 go_error_at(this->location(), "expected string");
2321 std::string asm_name
= token
->string_value();
2322 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
2324 go_error_at(this->location(), "expected %<)%>");
2327 this->advance_token();
2328 if (!Gogo::is_sink_name(name
))
2330 named_object
= this->gogo_
->declare_function(name
, fntype
, location
);
2331 if (named_object
->is_function_declaration())
2332 named_object
->func_declaration_value()->set_asm_name(asm_name
);
2336 // Check for the easy error of a newline before the opening brace.
2337 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
2339 Location semi_loc
= this->location();
2340 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
2341 go_error_at(this->location(),
2342 "unexpected semicolon or newline before %<{%>");
2344 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
2356 { GOPRAGMA_NOINTERFACE
, "nointerface", false, false, true },
2357 { GOPRAGMA_NOESCAPE
, "noescape", true, false, false },
2358 { GOPRAGMA_NORACE
, "norace", false, true, true },
2359 { GOPRAGMA_NOSPLIT
, "nosplit", false, true, true },
2360 { GOPRAGMA_NOINLINE
, "noinline", false, true, true },
2361 { GOPRAGMA_SYSTEMSTACK
, "systemstack", false, true, true },
2362 { GOPRAGMA_NOWRITEBARRIER
, "nowritebarrier", false, true, true },
2363 { GOPRAGMA_NOWRITEBARRIERREC
, "nowritebarrierrec", false, true, true },
2364 { GOPRAGMA_CGOUNSAFEARGS
, "cgo_unsafe_args", false, true, true },
2365 { GOPRAGMA_UINTPTRESCAPES
, "uintptrescapes", true, true, true },
2368 bool is_decl
= !this->peek_token()->is_op(OPERATOR_LCURLY
);
2372 i
< sizeof(pragma_check
) / sizeof(pragma_check
[0]);
2375 if ((pragmas
& pragma_check
[i
].bit
) == 0)
2380 if (pragma_check
[i
].decl_ok
)
2382 go_warning_at(location
, 0,
2383 ("ignoring magic //go:%s comment "
2384 "before declaration"),
2385 pragma_check
[i
].name
);
2387 else if (rec
== NULL
)
2389 if (pragma_check
[i
].func_ok
)
2391 go_warning_at(location
, 0,
2392 ("ignoring magic //go:%s comment "
2393 "before function definition"),
2394 pragma_check
[i
].name
);
2398 if (pragma_check
[i
].method_ok
)
2400 go_warning_at(location
, 0,
2401 ("ignoring magic //go:%s comment "
2402 "before method definition"),
2403 pragma_check
[i
].name
);
2406 pragmas
&= ~ pragma_check
[i
].bit
;
2412 if (named_object
== NULL
)
2414 // Function declarations with the blank identifier as a name are
2415 // mostly ignored since they cannot be called. We make an object
2416 // for this declaration for type-checking purposes.
2417 if (Gogo::is_sink_name(name
))
2421 snprintf(buf
, sizeof buf
, ".$sinkfndecl%d", count
);
2423 name
= std::string(buf
);
2427 || (expected_receiver
&& rec
== NULL
))
2428 this->gogo_
->add_erroneous_name(name
);
2431 named_object
= this->gogo_
->declare_function(name
, fntype
,
2433 if (!extern_name
.empty()
2434 && named_object
->is_function_declaration())
2436 Function_declaration
* fd
=
2437 named_object
->func_declaration_value();
2438 fd
->set_asm_name(extern_name
);
2443 if (pragmas
!= 0 && named_object
->is_function_declaration())
2444 named_object
->func_declaration_value()->set_pragmas(pragmas
);
2448 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2451 fntype
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2452 this->is_erroneous_function_
= true;
2453 if (!Gogo::is_sink_name(name
))
2454 this->gogo_
->add_erroneous_name(name
);
2455 name
= this->gogo_
->pack_hidden_name("_", false);
2457 named_object
= this->gogo_
->start_function(name
, fntype
, true, location
);
2458 Location end_loc
= this->block();
2459 this->gogo_
->finish_function(end_loc
);
2462 && !this->is_erroneous_function_
2463 && named_object
->is_function())
2464 named_object
->func_value()->set_pragmas(pragmas
);
2465 this->is_erroneous_function_
= hold_is_erroneous_function
;
2469 // Receiver = Parameters .
2474 Location location
= this->location();
2475 Typed_identifier_list
* til
;
2476 if (!this->parameters(&til
, NULL
))
2478 else if (til
== NULL
|| til
->empty())
2480 go_error_at(location
, "method has no receiver");
2483 else if (til
->size() > 1)
2485 go_error_at(location
, "method has multiple receivers");
2489 return &til
->front();
2492 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2493 // Literal = BasicLit | CompositeLit | FunctionLit .
2494 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2496 // If MAY_BE_SINK is true, this operand may be "_".
2498 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2499 // if the entire expression is in parentheses.
2502 Parse::operand(bool may_be_sink
, bool* is_parenthesized
)
2504 const Token
* token
= this->peek_token();
2506 switch (token
->classification())
2508 case Token::TOKEN_IDENTIFIER
:
2510 Location location
= token
->location();
2511 std::string id
= token
->identifier();
2512 bool is_exported
= token
->is_identifier_exported();
2513 std::string packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2515 Named_object
* in_function
;
2516 Named_object
* named_object
= this->gogo_
->lookup(packed
, &in_function
);
2518 Package
* package
= NULL
;
2519 if (named_object
!= NULL
&& named_object
->is_package())
2521 if (!this->advance_token()->is_op(OPERATOR_DOT
)
2522 || !this->advance_token()->is_identifier())
2524 go_error_at(location
, "unexpected reference to package");
2525 return Expression::make_error(location
);
2527 package
= named_object
->package_value();
2528 package
->note_usage(id
);
2529 id
= this->peek_token()->identifier();
2530 is_exported
= this->peek_token()->is_identifier_exported();
2531 packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2532 named_object
= package
->lookup(packed
);
2533 location
= this->location();
2534 go_assert(in_function
== NULL
);
2537 this->advance_token();
2539 if (named_object
!= NULL
2540 && named_object
->is_type()
2541 && !named_object
->type_value()->is_visible())
2543 go_assert(package
!= NULL
);
2544 go_error_at(location
, "invalid reference to hidden type %<%s.%s%>",
2545 Gogo::message_name(package
->package_name()).c_str(),
2546 Gogo::message_name(id
).c_str());
2547 return Expression::make_error(location
);
2551 if (named_object
== NULL
)
2553 if (package
!= NULL
)
2555 std::string n1
= Gogo::message_name(package
->package_name());
2556 std::string n2
= Gogo::message_name(id
);
2558 go_error_at(location
,
2559 ("invalid reference to unexported identifier "
2561 n1
.c_str(), n2
.c_str());
2563 go_error_at(location
,
2564 "reference to undefined identifier %<%s.%s%>",
2565 n1
.c_str(), n2
.c_str());
2566 return Expression::make_error(location
);
2569 named_object
= this->gogo_
->add_unknown_name(packed
, location
);
2572 if (in_function
!= NULL
2573 && in_function
!= this->gogo_
->current_function()
2574 && (named_object
->is_variable()
2575 || named_object
->is_result_variable()))
2576 return this->enclosing_var_reference(in_function
, named_object
,
2577 may_be_sink
, location
);
2579 switch (named_object
->classification())
2581 case Named_object::NAMED_OBJECT_CONST
:
2582 return Expression::make_const_reference(named_object
, location
);
2583 case Named_object::NAMED_OBJECT_TYPE
:
2584 return Expression::make_type(named_object
->type_value(), location
);
2585 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
2587 Type
* t
= Type::make_forward_declaration(named_object
);
2588 return Expression::make_type(t
, location
);
2590 case Named_object::NAMED_OBJECT_VAR
:
2591 case Named_object::NAMED_OBJECT_RESULT_VAR
:
2592 // Any left-hand-side can be a sink, so if this can not be
2593 // a sink, then it must be a use of the variable.
2595 this->mark_var_used(named_object
);
2596 return Expression::make_var_reference(named_object
, location
);
2597 case Named_object::NAMED_OBJECT_SINK
:
2599 return Expression::make_sink(location
);
2602 go_error_at(location
, "cannot use _ as value");
2603 return Expression::make_error(location
);
2605 case Named_object::NAMED_OBJECT_FUNC
:
2606 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
2607 return Expression::make_func_reference(named_object
, NULL
,
2609 case Named_object::NAMED_OBJECT_UNKNOWN
:
2611 Unknown_expression
* ue
=
2612 Expression::make_unknown_reference(named_object
, location
);
2613 if (this->is_erroneous_function_
)
2614 ue
->set_no_error_message();
2617 case Named_object::NAMED_OBJECT_ERRONEOUS
:
2618 return Expression::make_error(location
);
2625 case Token::TOKEN_STRING
:
2626 ret
= Expression::make_string(token
->string_value(), token
->location());
2627 this->advance_token();
2630 case Token::TOKEN_CHARACTER
:
2631 ret
= Expression::make_character(token
->character_value(), NULL
,
2633 this->advance_token();
2636 case Token::TOKEN_INTEGER
:
2637 ret
= Expression::make_integer_z(token
->integer_value(), NULL
,
2639 this->advance_token();
2642 case Token::TOKEN_FLOAT
:
2643 ret
= Expression::make_float(token
->float_value(), NULL
,
2645 this->advance_token();
2648 case Token::TOKEN_IMAGINARY
:
2651 mpfr_init_set_ui(zero
, 0, GMP_RNDN
);
2653 mpc_init2(val
, mpc_precision
);
2654 mpc_set_fr_fr(val
, zero
, *token
->imaginary_value(), MPC_RNDNN
);
2656 ret
= Expression::make_complex(&val
, NULL
, token
->location());
2658 this->advance_token();
2662 case Token::TOKEN_KEYWORD
:
2663 switch (token
->keyword())
2666 return this->function_lit();
2668 case KEYWORD_INTERFACE
:
2670 case KEYWORD_STRUCT
:
2672 Location location
= token
->location();
2673 return Expression::make_type(this->type(), location
);
2680 case Token::TOKEN_OPERATOR
:
2681 if (token
->is_op(OPERATOR_LPAREN
))
2683 this->advance_token();
2684 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2686 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2687 go_error_at(this->location(), "missing %<)%>");
2689 this->advance_token();
2690 if (is_parenthesized
!= NULL
)
2691 *is_parenthesized
= true;
2694 else if (token
->is_op(OPERATOR_LSQUARE
))
2696 // Here we call array_type directly, as this is the only
2697 // case where an ellipsis is permitted for an array type.
2698 Location location
= token
->location();
2699 return Expression::make_type(this->array_type(true), location
);
2707 go_error_at(this->location(), "expected operand");
2708 return Expression::make_error(this->location());
2711 // Handle a reference to a variable in an enclosing function. We add
2712 // it to a list of such variables. We return a reference to a field
2713 // in a struct which will be passed on the static chain when calling
2714 // the current function.
2717 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2718 bool may_be_sink
, Location location
)
2720 go_assert(var
->is_variable() || var
->is_result_variable());
2722 // Any left-hand-side can be a sink, so if this can not be
2723 // a sink, then it must be a use of the variable.
2725 this->mark_var_used(var
);
2727 Named_object
* this_function
= this->gogo_
->current_function();
2728 Named_object
* closure
= this_function
->func_value()->closure_var();
2730 // The last argument to the Enclosing_var constructor is the index
2731 // of this variable in the closure. We add 1 to the current number
2732 // of enclosed variables, because the first field in the closure
2733 // points to the function code.
2734 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2735 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2736 this->enclosing_vars_
.insert(ev
);
2739 // This is a variable we have not seen before. Add a new field
2740 // to the closure type.
2741 this_function
->func_value()->add_closure_field(var
, location
);
2744 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2747 Expression::make_dereference(closure_ref
,
2748 Expression::NIL_CHECK_NOT_NEEDED
,
2751 // The closure structure holds pointers to the variables, so we need
2752 // to introduce an indirection.
2753 Expression
* e
= Expression::make_field_reference(closure_ref
,
2756 e
= Expression::make_dereference(e
, Expression::NIL_CHECK_NOT_NEEDED
,
2758 return Expression::make_enclosing_var_reference(e
, var
, location
);
2761 // CompositeLit = LiteralType LiteralValue .
2762 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2763 // SliceType | MapType | TypeName .
2764 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2765 // ElementList = Element { "," Element } .
2766 // Element = [ Key ":" ] Value .
2767 // Key = FieldName | ElementIndex .
2768 // FieldName = identifier .
2769 // ElementIndex = Expression .
2770 // Value = Expression | LiteralValue .
2772 // We have already seen the type if there is one, and we are now
2773 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2774 // will be seen here as an array type whose length is "nil". The
2775 // DEPTH parameter is non-zero if this is an embedded composite
2776 // literal and the type was omitted. It gives the number of steps up
2777 // to the type which was provided. E.g., in [][]int{{1}} it will be
2778 // 1. In [][][]int{{{1}}} it will be 2.
2781 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2783 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2784 this->advance_token();
2786 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2788 this->advance_token();
2789 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2793 bool has_keys
= false;
2794 bool all_are_names
= true;
2795 Expression_list
* vals
= new Expression_list
;
2799 bool is_type_omitted
= false;
2800 bool is_name
= false;
2802 const Token
* token
= this->peek_token();
2804 if (token
->is_identifier())
2806 std::string identifier
= token
->identifier();
2807 bool is_exported
= token
->is_identifier_exported();
2808 Location location
= token
->location();
2810 if (this->advance_token()->is_op(OPERATOR_COLON
))
2812 // This may be a field name. We don't know for sure--it
2813 // could also be an expression for an array index. We
2814 // don't want to parse it as an expression because may
2815 // trigger various errors, e.g., if this identifier
2816 // happens to be the name of a package.
2817 Gogo
* gogo
= this->gogo_
;
2818 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2825 this->unget_token(Token::make_identifier_token(identifier
,
2828 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2832 else if (!token
->is_op(OPERATOR_LCURLY
))
2833 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2836 // This must be a composite literal inside another composite
2837 // literal, with the type omitted for the inner one.
2838 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2839 is_type_omitted
= true;
2842 token
= this->peek_token();
2843 if (!token
->is_op(OPERATOR_COLON
))
2846 vals
->push_back(NULL
);
2851 if (is_type_omitted
)
2853 // VAL is a nested composite literal with an omitted type being
2854 // used a key. Record this information in VAL so that the correct
2855 // type is associated with the literal value if VAL is a
2857 val
->complit()->update_key_path(depth
);
2860 this->advance_token();
2862 if (!has_keys
&& !vals
->empty())
2864 Expression_list
* newvals
= new Expression_list
;
2865 for (Expression_list::const_iterator p
= vals
->begin();
2869 newvals
->push_back(NULL
);
2870 newvals
->push_back(*p
);
2877 if (val
->unknown_expression() != NULL
)
2878 val
->unknown_expression()->set_is_composite_literal_key();
2880 vals
->push_back(val
);
2882 if (!token
->is_op(OPERATOR_LCURLY
))
2883 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2886 // This must be a composite literal inside another
2887 // composite literal, with the type omitted for the
2889 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2892 token
= this->peek_token();
2895 vals
->push_back(val
);
2898 all_are_names
= false;
2900 if (token
->is_op(OPERATOR_COMMA
))
2902 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2904 this->advance_token();
2908 else if (token
->is_op(OPERATOR_RCURLY
))
2910 this->advance_token();
2915 if (token
->is_op(OPERATOR_SEMICOLON
))
2916 go_error_at(this->location(),
2917 ("need trailing comma before newline "
2918 "in composite literal"));
2920 go_error_at(this->location(), "expected %<,%> or %<}%>");
2922 this->gogo_
->mark_locals_used();
2924 while (!token
->is_eof()
2925 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2927 if (token
->is_op(OPERATOR_LCURLY
))
2929 else if (token
->is_op(OPERATOR_RCURLY
))
2931 token
= this->advance_token();
2933 if (token
->is_op(OPERATOR_RCURLY
))
2934 this->advance_token();
2936 return Expression::make_error(location
);
2940 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2941 all_are_names
, location
);
2944 // FunctionLit = "func" Signature Block .
2947 Parse::function_lit()
2949 Location location
= this->location();
2950 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2951 this->advance_token();
2953 Enclosing_vars hold_enclosing_vars
;
2954 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2956 Function_type
* type
= this->signature(NULL
, location
);
2957 bool fntype_is_error
= false;
2960 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2961 fntype_is_error
= true;
2964 // For a function literal, the next token must be a '{'. If we
2965 // don't see that, then we may have a type expression.
2966 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2968 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2969 return Expression::make_type(type
, location
);
2972 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2973 if (fntype_is_error
)
2974 this->is_erroneous_function_
= true;
2976 Bc_stack
* hold_break_stack
= this->break_stack_
;
2977 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2978 this->break_stack_
= NULL
;
2979 this->continue_stack_
= NULL
;
2981 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2983 Location end_loc
= this->block();
2985 this->gogo_
->finish_function(end_loc
);
2987 if (this->break_stack_
!= NULL
)
2988 delete this->break_stack_
;
2989 if (this->continue_stack_
!= NULL
)
2990 delete this->continue_stack_
;
2991 this->break_stack_
= hold_break_stack
;
2992 this->continue_stack_
= hold_continue_stack
;
2994 this->is_erroneous_function_
= hold_is_erroneous_function
;
2996 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2998 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
3001 return Expression::make_func_reference(no
, closure
, location
);
3004 // Create a closure for the nested function FUNCTION. This is based
3005 // on ENCLOSING_VARS, which is a list of all variables defined in
3006 // enclosing functions and referenced from FUNCTION. A closure is the
3007 // address of a struct which point to the real function code and
3008 // contains the addresses of all the referenced variables. This
3009 // returns NULL if no closure is required.
3012 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
3015 if (enclosing_vars
->empty())
3018 // Get the variables in order by their field index.
3020 size_t enclosing_var_count
= enclosing_vars
->size();
3021 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
3022 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
3023 p
!= enclosing_vars
->end();
3026 // Subtract 1 because index 0 is the function code.
3027 ev
[p
->index() - 1] = *p
;
3030 // Build an initializer for a composite literal of the closure's
3033 Named_object
* enclosing_function
= this->gogo_
->current_function();
3034 Expression_list
* initializer
= new Expression_list
;
3036 initializer
->push_back(Expression::make_func_code_reference(function
,
3039 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
3041 // Add 1 to i because the first field in the closure is a
3042 // pointer to the function code.
3043 go_assert(ev
[i
].index() == i
+ 1);
3044 Named_object
* var
= ev
[i
].var();
3046 if (ev
[i
].in_function() == enclosing_function
)
3047 ref
= Expression::make_var_reference(var
, location
);
3049 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
3051 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
3053 initializer
->push_back(refaddr
);
3056 Named_object
* closure_var
= function
->func_value()->closure_var();
3057 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
3058 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
3060 return Expression::make_heap_expression(cv
, location
);
3063 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
3065 // If MAY_BE_SINK is true, this expression may be "_".
3067 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3070 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3071 // guard (var := expr.("type") using the literal keyword "type").
3073 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3074 // if the entire expression is in parentheses.
3077 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3078 bool* is_type_switch
, bool* is_parenthesized
)
3080 Location start_loc
= this->location();
3081 bool operand_is_parenthesized
= false;
3082 bool whole_is_parenthesized
= false;
3084 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
3086 whole_is_parenthesized
= operand_is_parenthesized
;
3088 // An unknown name followed by a curly brace must be a composite
3089 // literal, and the unknown name must be a type.
3090 if (may_be_composite_lit
3091 && !operand_is_parenthesized
3092 && ret
->unknown_expression() != NULL
3093 && this->peek_token()->is_op(OPERATOR_LCURLY
))
3095 Named_object
* no
= ret
->unknown_expression()->named_object();
3096 Type
* type
= Type::make_forward_declaration(no
);
3097 ret
= Expression::make_type(type
, ret
->location());
3100 // We handle composite literals and type casts here, as it is the
3101 // easiest way to handle types which are in parentheses, as in
3103 if (ret
->is_type_expression())
3105 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
3107 whole_is_parenthesized
= false;
3108 if (!may_be_composite_lit
)
3110 Type
* t
= ret
->type();
3111 if (t
->named_type() != NULL
3112 || t
->forward_declaration_type() != NULL
)
3113 go_error_at(start_loc
,
3114 _("parentheses required around this composite "
3115 "literal to avoid parsing ambiguity"));
3117 else if (operand_is_parenthesized
)
3118 go_error_at(start_loc
,
3119 "cannot parenthesize type in composite literal");
3120 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
3122 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
3124 whole_is_parenthesized
= false;
3125 Location loc
= this->location();
3126 this->advance_token();
3127 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
3129 if (this->peek_token()->is_op(OPERATOR_COMMA
))
3130 this->advance_token();
3131 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
3133 go_error_at(this->location(),
3134 "invalid use of %<...%> in type conversion");
3135 this->advance_token();
3137 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3138 go_error_at(this->location(), "expected %<)%>");
3140 this->advance_token();
3141 if (expr
->is_error_expression())
3145 Type
* t
= ret
->type();
3146 if (t
->classification() == Type::TYPE_ARRAY
3147 && t
->array_type()->length() != NULL
3148 && t
->array_type()->length()->is_nil_expression())
3150 go_error_at(ret
->location(),
3151 "use of %<[...]%> outside of array literal");
3152 ret
= Expression::make_error(loc
);
3155 ret
= Expression::make_cast(t
, expr
, loc
);
3162 const Token
* token
= this->peek_token();
3163 if (token
->is_op(OPERATOR_LPAREN
))
3165 whole_is_parenthesized
= false;
3166 ret
= this->call(this->verify_not_sink(ret
));
3168 else if (token
->is_op(OPERATOR_DOT
))
3170 whole_is_parenthesized
= false;
3171 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3172 if (is_type_switch
!= NULL
&& *is_type_switch
)
3175 else if (token
->is_op(OPERATOR_LSQUARE
))
3177 whole_is_parenthesized
= false;
3178 ret
= this->index(this->verify_not_sink(ret
));
3184 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3185 *is_parenthesized
= true;
3190 // Selector = "." identifier .
3191 // TypeGuard = "." "(" QualifiedIdent ")" .
3193 // Note that Operand can expand to QualifiedIdent, which contains a
3194 // ".". That is handled directly in operand when it sees a package
3197 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3198 // guard (var := expr.("type") using the literal keyword "type").
3201 Parse::selector(Expression
* left
, bool* is_type_switch
)
3203 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3204 Location location
= this->location();
3206 const Token
* token
= this->advance_token();
3207 if (token
->is_identifier())
3209 // This could be a field in a struct, or a method in an
3210 // interface, or a method associated with a type. We can't know
3211 // which until we have seen all the types.
3213 this->gogo_
->pack_hidden_name(token
->identifier(),
3214 token
->is_identifier_exported());
3215 if (token
->identifier() == "_")
3217 go_error_at(this->location(), "invalid use of %<_%>");
3218 name
= Gogo::erroneous_name();
3220 this->advance_token();
3221 return Expression::make_selector(left
, name
, location
);
3223 else if (token
->is_op(OPERATOR_LPAREN
))
3225 this->advance_token();
3227 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3228 type
= this->type();
3231 if (is_type_switch
!= NULL
)
3232 *is_type_switch
= true;
3235 go_error_at(this->location(),
3236 "use of %<.(type)%> outside type switch");
3237 type
= Type::make_error_type();
3239 this->advance_token();
3241 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3242 go_error_at(this->location(), "missing %<)%>");
3244 this->advance_token();
3245 if (is_type_switch
!= NULL
&& *is_type_switch
)
3247 return Expression::make_type_guard(left
, type
, location
);
3251 go_error_at(this->location(), "expected identifier or %<(%>");
3256 // Index = "[" Expression "]" .
3257 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3260 Parse::index(Expression
* expr
)
3262 Location location
= this->location();
3263 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3264 this->advance_token();
3267 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3268 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3270 start
= Expression::make_integer_ul(0, NULL
, location
);
3272 Expression
* end
= NULL
;
3273 if (this->peek_token()->is_op(OPERATOR_COLON
))
3275 // We use nil to indicate a missing high expression.
3276 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3277 end
= Expression::make_nil(this->location());
3278 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3280 go_error_at(this->location(),
3281 "middle index required in 3-index slice");
3282 end
= Expression::make_error(this->location());
3285 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3288 Expression
* cap
= NULL
;
3289 if (this->peek_token()->is_op(OPERATOR_COLON
))
3291 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3293 go_error_at(this->location(),
3294 "final index required in 3-index slice");
3295 cap
= Expression::make_error(this->location());
3298 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3300 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3301 go_error_at(this->location(), "missing %<]%>");
3303 this->advance_token();
3304 return Expression::make_index(expr
, start
, end
, cap
, location
);
3307 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3308 // ArgumentList = ExpressionList [ "..." ] .
3311 Parse::call(Expression
* func
)
3313 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3314 Expression_list
* args
= NULL
;
3315 bool is_varargs
= false;
3316 const Token
* token
= this->advance_token();
3317 if (!token
->is_op(OPERATOR_RPAREN
))
3319 args
= this->expression_list(NULL
, false, true);
3320 token
= this->peek_token();
3321 if (token
->is_op(OPERATOR_ELLIPSIS
))
3324 token
= this->advance_token();
3327 if (token
->is_op(OPERATOR_COMMA
))
3328 token
= this->advance_token();
3329 if (!token
->is_op(OPERATOR_RPAREN
))
3331 go_error_at(this->location(), "missing %<)%>");
3332 if (!this->skip_past_error(OPERATOR_RPAREN
))
3333 return Expression::make_error(this->location());
3335 this->advance_token();
3336 if (func
->is_error_expression())
3338 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3341 // Return an expression for a single unqualified identifier.
3344 Parse::id_to_expression(const std::string
& name
, Location location
,
3347 Named_object
* in_function
;
3348 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3349 if (named_object
== NULL
)
3350 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3352 if (in_function
!= NULL
3353 && in_function
!= this->gogo_
->current_function()
3354 && (named_object
->is_variable() || named_object
->is_result_variable()))
3355 return this->enclosing_var_reference(in_function
, named_object
, is_lhs
,
3358 switch (named_object
->classification())
3360 case Named_object::NAMED_OBJECT_CONST
:
3361 return Expression::make_const_reference(named_object
, location
);
3362 case Named_object::NAMED_OBJECT_VAR
:
3363 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3365 this->mark_var_used(named_object
);
3366 return Expression::make_var_reference(named_object
, location
);
3367 case Named_object::NAMED_OBJECT_SINK
:
3368 return Expression::make_sink(location
);
3369 case Named_object::NAMED_OBJECT_FUNC
:
3370 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3371 return Expression::make_func_reference(named_object
, NULL
, location
);
3372 case Named_object::NAMED_OBJECT_UNKNOWN
:
3374 Unknown_expression
* ue
=
3375 Expression::make_unknown_reference(named_object
, location
);
3376 if (this->is_erroneous_function_
)
3377 ue
->set_no_error_message();
3380 case Named_object::NAMED_OBJECT_PACKAGE
:
3381 case Named_object::NAMED_OBJECT_TYPE
:
3382 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3384 // These cases can arise for a field name in a composite
3385 // literal. Keep track of these as they might be fake uses of
3386 // the related package.
3387 Unknown_expression
* ue
=
3388 Expression::make_unknown_reference(named_object
, location
);
3389 if (named_object
->package() != NULL
)
3390 named_object
->package()->note_fake_usage(ue
);
3391 if (this->is_erroneous_function_
)
3392 ue
->set_no_error_message();
3395 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3396 return Expression::make_error(location
);
3398 go_error_at(this->location(), "unexpected type of identifier");
3399 return Expression::make_error(location
);
3403 // Expression = UnaryExpr { binary_op Expression } .
3405 // PRECEDENCE is the precedence of the current operator.
3407 // If MAY_BE_SINK is true, this expression may be "_".
3409 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3412 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3413 // guard (var := expr.("type") using the literal keyword "type").
3415 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3416 // if the entire expression is in parentheses.
3419 Parse::expression(Precedence precedence
, bool may_be_sink
,
3420 bool may_be_composite_lit
, bool* is_type_switch
,
3421 bool *is_parenthesized
)
3423 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3424 is_type_switch
, is_parenthesized
);
3428 if (is_type_switch
!= NULL
&& *is_type_switch
)
3431 const Token
* token
= this->peek_token();
3432 if (token
->classification() != Token::TOKEN_OPERATOR
)
3438 Precedence right_precedence
;
3439 switch (token
->op())
3442 right_precedence
= PRECEDENCE_OROR
;
3444 case OPERATOR_ANDAND
:
3445 right_precedence
= PRECEDENCE_ANDAND
;
3448 case OPERATOR_NOTEQ
:
3453 right_precedence
= PRECEDENCE_RELOP
;
3456 case OPERATOR_MINUS
:
3459 right_precedence
= PRECEDENCE_ADDOP
;
3464 case OPERATOR_LSHIFT
:
3465 case OPERATOR_RSHIFT
:
3467 case OPERATOR_BITCLEAR
:
3468 right_precedence
= PRECEDENCE_MULOP
;
3471 right_precedence
= PRECEDENCE_INVALID
;
3475 if (right_precedence
== PRECEDENCE_INVALID
)
3481 if (is_parenthesized
!= NULL
)
3482 *is_parenthesized
= false;
3484 Operator op
= token
->op();
3485 Location binop_location
= token
->location();
3487 if (precedence
>= right_precedence
)
3489 // We've already seen A * B, and we see + C. We want to
3490 // return so that A * B becomes a group.
3494 this->advance_token();
3496 left
= this->verify_not_sink(left
);
3497 Expression
* right
= this->expression(right_precedence
, false,
3498 may_be_composite_lit
,
3500 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3505 Parse::expression_may_start_here()
3507 const Token
* token
= this->peek_token();
3508 switch (token
->classification())
3510 case Token::TOKEN_INVALID
:
3511 case Token::TOKEN_EOF
:
3513 case Token::TOKEN_KEYWORD
:
3514 switch (token
->keyword())
3519 case KEYWORD_STRUCT
:
3520 case KEYWORD_INTERFACE
:
3525 case Token::TOKEN_IDENTIFIER
:
3527 case Token::TOKEN_STRING
:
3529 case Token::TOKEN_OPERATOR
:
3530 switch (token
->op())
3533 case OPERATOR_MINUS
:
3537 case OPERATOR_CHANOP
:
3539 case OPERATOR_LPAREN
:
3540 case OPERATOR_LSQUARE
:
3545 case Token::TOKEN_CHARACTER
:
3546 case Token::TOKEN_INTEGER
:
3547 case Token::TOKEN_FLOAT
:
3548 case Token::TOKEN_IMAGINARY
:
3555 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3557 // If MAY_BE_SINK is true, this expression may be "_".
3559 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3562 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3563 // guard (var := expr.("type") using the literal keyword "type").
3565 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3566 // if the entire expression is in parentheses.
3569 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3570 bool* is_type_switch
, bool* is_parenthesized
)
3572 const Token
* token
= this->peek_token();
3574 // There is a complex parse for <- chan. The choices are
3575 // Convert x to type <- chan int:
3577 // Receive from (x converted to type chan <- chan int):
3578 // (<- chan <- chan int (x))
3579 // Convert x to type <- chan (<- chan int).
3580 // (<- chan <- chan int)(x)
3581 if (token
->is_op(OPERATOR_CHANOP
))
3583 Location location
= token
->location();
3584 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3586 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3588 if (expr
->is_error_expression())
3590 else if (!expr
->is_type_expression())
3591 return Expression::make_receive(expr
, location
);
3594 if (expr
->type()->is_error_type())
3597 // We picked up "chan TYPE", but it is not a type
3599 Channel_type
* ct
= expr
->type()->channel_type();
3602 // This is probably impossible.
3603 go_error_at(location
, "expected channel type");
3604 return Expression::make_error(location
);
3606 else if (ct
->may_receive())
3609 Type
* t
= Type::make_channel_type(false, true,
3610 ct
->element_type());
3611 return Expression::make_type(t
, location
);
3615 // <- chan <- TYPE. Because we skipped the leading
3616 // <-, we parsed this as chan <- TYPE. With the
3617 // leading <-, we parse it as <- chan (<- TYPE).
3618 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3619 return Expression::make_type(t
, location
);
3624 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3625 token
= this->peek_token();
3628 if (token
->is_op(OPERATOR_PLUS
)
3629 || token
->is_op(OPERATOR_MINUS
)
3630 || token
->is_op(OPERATOR_NOT
)
3631 || token
->is_op(OPERATOR_XOR
)
3632 || token
->is_op(OPERATOR_CHANOP
)
3633 || token
->is_op(OPERATOR_MULT
)
3634 || token
->is_op(OPERATOR_AND
))
3636 Location location
= token
->location();
3637 Operator op
= token
->op();
3638 this->advance_token();
3640 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3642 if (expr
->is_error_expression())
3644 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3645 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3647 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3648 expr
= Expression::make_heap_expression(expr
, location
);
3649 else if (op
!= OPERATOR_CHANOP
)
3650 expr
= Expression::make_unary(op
, expr
, location
);
3652 expr
= Expression::make_receive(expr
, location
);
3656 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3657 is_type_switch
, is_parenthesized
);
3660 // This is called for the obscure case of
3661 // (<- chan <- chan int)(x)
3662 // In unary_expr we remove the leading <- and parse the remainder,
3664 // chan <- (chan int)
3665 // When we add the leading <- back in, we really want
3666 // <- chan (<- chan int)
3667 // This means that we need to reassociate.
3670 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3672 Channel_type
* ele
= ct
->element_type()->channel_type();
3675 go_error_at(location
, "parse error");
3676 return Type::make_error_type();
3679 if (ele
->may_send())
3680 sub
= Type::make_channel_type(false, true, ele
->element_type());
3682 sub
= this->reassociate_chan_direction(ele
, location
);
3683 return Type::make_channel_type(false, true, sub
);
3687 // Declaration | LabeledStmt | SimpleStmt |
3688 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3689 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3692 // LABEL is the label of this statement if it has one.
3695 Parse::statement(Label
* label
)
3697 const Token
* token
= this->peek_token();
3698 switch (token
->classification())
3700 case Token::TOKEN_KEYWORD
:
3702 switch (token
->keyword())
3707 this->declaration();
3711 case KEYWORD_STRUCT
:
3712 case KEYWORD_INTERFACE
:
3713 this->simple_stat(true, NULL
, NULL
, NULL
);
3717 this->go_or_defer_stat();
3719 case KEYWORD_RETURN
:
3720 this->return_stat();
3725 case KEYWORD_CONTINUE
:
3726 this->continue_stat();
3734 case KEYWORD_SWITCH
:
3735 this->switch_stat(label
);
3737 case KEYWORD_SELECT
:
3738 this->select_stat(label
);
3741 this->for_stat(label
);
3744 go_error_at(this->location(), "expected statement");
3745 this->advance_token();
3751 case Token::TOKEN_IDENTIFIER
:
3753 std::string identifier
= token
->identifier();
3754 bool is_exported
= token
->is_identifier_exported();
3755 Location location
= token
->location();
3756 if (this->advance_token()->is_op(OPERATOR_COLON
))
3758 this->advance_token();
3759 this->labeled_stmt(identifier
, location
);
3763 this->unget_token(Token::make_identifier_token(identifier
,
3766 this->simple_stat(true, NULL
, NULL
, NULL
);
3771 case Token::TOKEN_OPERATOR
:
3772 if (token
->is_op(OPERATOR_LCURLY
))
3774 Location location
= token
->location();
3775 this->gogo_
->start_block(location
);
3776 Location end_loc
= this->block();
3777 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3780 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3781 this->simple_stat(true, NULL
, NULL
, NULL
);
3784 case Token::TOKEN_STRING
:
3785 case Token::TOKEN_CHARACTER
:
3786 case Token::TOKEN_INTEGER
:
3787 case Token::TOKEN_FLOAT
:
3788 case Token::TOKEN_IMAGINARY
:
3789 this->simple_stat(true, NULL
, NULL
, NULL
);
3793 go_error_at(this->location(), "expected statement");
3794 this->advance_token();
3800 Parse::statement_may_start_here()
3802 const Token
* token
= this->peek_token();
3803 switch (token
->classification())
3805 case Token::TOKEN_KEYWORD
:
3807 switch (token
->keyword())
3814 case KEYWORD_STRUCT
:
3815 case KEYWORD_INTERFACE
:
3818 case KEYWORD_RETURN
:
3820 case KEYWORD_CONTINUE
:
3823 case KEYWORD_SWITCH
:
3824 case KEYWORD_SELECT
:
3834 case Token::TOKEN_IDENTIFIER
:
3837 case Token::TOKEN_OPERATOR
:
3838 if (token
->is_op(OPERATOR_LCURLY
)
3839 || token
->is_op(OPERATOR_SEMICOLON
))
3842 return this->expression_may_start_here();
3844 case Token::TOKEN_STRING
:
3845 case Token::TOKEN_CHARACTER
:
3846 case Token::TOKEN_INTEGER
:
3847 case Token::TOKEN_FLOAT
:
3848 case Token::TOKEN_IMAGINARY
:
3856 // LabeledStmt = Label ":" Statement .
3857 // Label = identifier .
3860 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3862 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3864 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3866 // This is a label at the end of a block. A program is
3867 // permitted to omit a semicolon here.
3871 if (!this->statement_may_start_here())
3873 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
3875 // We don't treat the fallthrough keyword as a statement,
3876 // because it can't appear most places where a statement is
3877 // permitted, but it may have a label. We introduce a
3878 // semicolon because the caller expects to see a statement.
3879 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3884 // Mark the label as used to avoid a useless error about an
3887 label
->set_is_used();
3889 go_error_at(location
, "missing statement after label");
3890 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3895 this->statement(label
);
3898 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3899 // Assignment | ShortVarDecl .
3901 // EmptyStmt was handled in Parse::statement.
3903 // In order to make this work for if and switch statements, if
3904 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3905 // expression rather than adding an expression statement to the
3906 // current block. If we see something other than an ExpressionStat,
3907 // we add the statement, set *RETURN_EXP to true if we saw a send
3908 // statement, and return NULL. The handling of send statements is for
3909 // better error messages.
3911 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3914 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3915 // guard (var := expr.("type") using the literal keyword "type").
3918 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3919 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3921 const Token
* token
= this->peek_token();
3923 // An identifier follow by := is a SimpleVarDecl.
3924 if (token
->is_identifier())
3926 std::string identifier
= token
->identifier();
3927 bool is_exported
= token
->is_identifier_exported();
3928 Location location
= token
->location();
3930 token
= this->advance_token();
3931 if (token
->is_op(OPERATOR_COLONEQ
)
3932 || token
->is_op(OPERATOR_COMMA
))
3934 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3935 this->simple_var_decl_or_assignment(identifier
, location
,
3936 may_be_composite_lit
,
3938 (token
->is_op(OPERATOR_COLONEQ
)
3944 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3947 else if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
3949 Typed_identifier_list til
;
3950 this->range_clause_decl(&til
, p_range_clause
);
3954 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3955 may_be_composite_lit
,
3956 (p_type_switch
== NULL
3958 : &p_type_switch
->found
),
3960 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3962 p_type_switch
->name
.clear();
3963 p_type_switch
->location
= exp
->location();
3964 p_type_switch
->expr
= this->verify_not_sink(exp
);
3967 token
= this->peek_token();
3968 if (token
->is_op(OPERATOR_CHANOP
))
3970 this->send_stmt(this->verify_not_sink(exp
), may_be_composite_lit
);
3971 if (return_exp
!= NULL
)
3974 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3975 || token
->is_op(OPERATOR_MINUSMINUS
))
3976 this->inc_dec_stat(this->verify_not_sink(exp
));
3977 else if (token
->is_op(OPERATOR_COMMA
)
3978 || token
->is_op(OPERATOR_EQ
))
3979 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3980 else if (token
->is_op(OPERATOR_PLUSEQ
)
3981 || token
->is_op(OPERATOR_MINUSEQ
)
3982 || token
->is_op(OPERATOR_OREQ
)
3983 || token
->is_op(OPERATOR_XOREQ
)
3984 || token
->is_op(OPERATOR_MULTEQ
)
3985 || token
->is_op(OPERATOR_DIVEQ
)
3986 || token
->is_op(OPERATOR_MODEQ
)
3987 || token
->is_op(OPERATOR_LSHIFTEQ
)
3988 || token
->is_op(OPERATOR_RSHIFTEQ
)
3989 || token
->is_op(OPERATOR_ANDEQ
)
3990 || token
->is_op(OPERATOR_BITCLEAREQ
))
3991 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
3993 else if (return_exp
!= NULL
)
3994 return this->verify_not_sink(exp
);
3997 exp
= this->verify_not_sink(exp
);
3999 if (token
->is_op(OPERATOR_COLONEQ
))
4001 if (!exp
->is_error_expression())
4002 go_error_at(token
->location(), "non-name on left side of %<:=%>");
4003 this->gogo_
->mark_locals_used();
4004 while (!token
->is_op(OPERATOR_SEMICOLON
)
4005 && !token
->is_eof())
4006 token
= this->advance_token();
4010 this->expression_stat(exp
);
4017 Parse::simple_stat_may_start_here()
4019 return this->expression_may_start_here();
4022 // Parse { Statement ";" } which is used in a few places. The list of
4023 // statements may end with a right curly brace, in which case the
4024 // semicolon may be omitted.
4027 Parse::statement_list()
4029 while (this->statement_may_start_here())
4031 this->statement(NULL
);
4032 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4033 this->advance_token();
4034 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4038 if (!this->peek_token()->is_eof() || !saw_errors())
4039 go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
4040 if (!this->skip_past_error(OPERATOR_RCURLY
))
4047 Parse::statement_list_may_start_here()
4049 return this->statement_may_start_here();
4052 // ExpressionStat = Expression .
4055 Parse::expression_stat(Expression
* exp
)
4057 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
4060 // SendStmt = Channel "<-" Expression .
4061 // Channel = Expression .
4064 Parse::send_stmt(Expression
* channel
, bool may_be_composite_lit
)
4066 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
4067 Location loc
= this->location();
4068 this->advance_token();
4069 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false,
4070 may_be_composite_lit
, NULL
, NULL
);
4071 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
4072 this->gogo_
->add_statement(s
);
4075 // IncDecStat = Expression ( "++" | "--" ) .
4078 Parse::inc_dec_stat(Expression
* exp
)
4080 const Token
* token
= this->peek_token();
4081 if (token
->is_op(OPERATOR_PLUSPLUS
))
4082 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
4083 else if (token
->is_op(OPERATOR_MINUSMINUS
))
4084 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
4087 this->advance_token();
4090 // Assignment = ExpressionList assign_op ExpressionList .
4092 // EXP is an expression that we have already parsed.
4094 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4095 // side may be a composite literal.
4097 // If RANGE_CLAUSE is not NULL, then this will recognize a
4101 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
4102 Range_clause
* p_range_clause
)
4104 Expression_list
* vars
;
4105 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4107 vars
= new Expression_list();
4108 vars
->push_back(expr
);
4112 this->advance_token();
4113 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
4116 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
4119 // An assignment statement. LHS is the list of expressions which
4120 // appear on the left hand side.
4122 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4123 // side may be a composite literal.
4125 // If RANGE_CLAUSE is not NULL, then this will recognize a
4129 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
4130 Range_clause
* p_range_clause
)
4132 const Token
* token
= this->peek_token();
4133 if (!token
->is_op(OPERATOR_EQ
)
4134 && !token
->is_op(OPERATOR_PLUSEQ
)
4135 && !token
->is_op(OPERATOR_MINUSEQ
)
4136 && !token
->is_op(OPERATOR_OREQ
)
4137 && !token
->is_op(OPERATOR_XOREQ
)
4138 && !token
->is_op(OPERATOR_MULTEQ
)
4139 && !token
->is_op(OPERATOR_DIVEQ
)
4140 && !token
->is_op(OPERATOR_MODEQ
)
4141 && !token
->is_op(OPERATOR_LSHIFTEQ
)
4142 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4143 && !token
->is_op(OPERATOR_ANDEQ
)
4144 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4146 go_error_at(this->location(), "expected assignment operator");
4149 Operator op
= token
->op();
4150 Location location
= token
->location();
4152 token
= this->advance_token();
4157 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4159 if (op
!= OPERATOR_EQ
)
4160 go_error_at(this->location(), "range clause requires %<=%>");
4161 this->range_clause_expr(lhs
, p_range_clause
);
4165 Expression_list
* vals
= this->expression_list(NULL
, false,
4166 may_be_composite_lit
);
4168 // We've parsed everything; check for errors.
4171 for (Expression_list::const_iterator pe
= lhs
->begin();
4175 if ((*pe
)->is_error_expression())
4177 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4178 go_error_at((*pe
)->location(), "cannot use _ as value");
4180 for (Expression_list::const_iterator pe
= vals
->begin();
4184 if ((*pe
)->is_error_expression())
4188 Call_expression
* call
;
4189 Index_expression
* map_index
;
4190 Receive_expression
* receive
;
4191 Type_guard_expression
* type_guard
;
4192 if (lhs
->size() == vals
->size())
4195 if (lhs
->size() > 1)
4197 if (op
!= OPERATOR_EQ
)
4198 go_error_at(location
, "multiple values only permitted with %<=%>");
4199 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4203 if (op
== OPERATOR_EQ
)
4204 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4207 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4208 vals
->front(), location
);
4212 this->gogo_
->add_statement(s
);
4214 else if (vals
->size() == 1
4215 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4217 if (op
!= OPERATOR_EQ
)
4218 go_error_at(location
, "multiple results only permitted with %<=%>");
4219 call
->set_expected_result_count(lhs
->size());
4221 vals
= new Expression_list
;
4222 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4223 vals
->push_back(Expression::make_call_result(call
, i
));
4224 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4225 this->gogo_
->add_statement(s
);
4227 else if (lhs
->size() == 2
4228 && vals
->size() == 1
4229 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4231 if (op
!= OPERATOR_EQ
)
4232 go_error_at(location
, "two values from map requires %<=%>");
4233 Expression
* val
= lhs
->front();
4234 Expression
* present
= lhs
->back();
4235 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4236 map_index
, location
);
4237 this->gogo_
->add_statement(s
);
4239 else if (lhs
->size() == 2
4240 && vals
->size() == 1
4241 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4243 if (op
!= OPERATOR_EQ
)
4244 go_error_at(location
, "two values from receive requires %<=%>");
4245 Expression
* val
= lhs
->front();
4246 Expression
* success
= lhs
->back();
4247 Expression
* channel
= receive
->channel();
4248 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4251 this->gogo_
->add_statement(s
);
4253 else if (lhs
->size() == 2
4254 && vals
->size() == 1
4255 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4257 if (op
!= OPERATOR_EQ
)
4258 go_error_at(location
, "two values from type guard requires %<=%>");
4259 Expression
* val
= lhs
->front();
4260 Expression
* ok
= lhs
->back();
4261 Expression
* expr
= type_guard
->expr();
4262 Type
* type
= type_guard
->type();
4263 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4266 this->gogo_
->add_statement(s
);
4270 go_error_at(location
, ("number of variables does not "
4271 "match number of values"));
4275 // GoStat = "go" Expression .
4276 // DeferStat = "defer" Expression .
4279 Parse::go_or_defer_stat()
4281 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4282 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4283 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4284 Location stat_location
= this->location();
4286 this->advance_token();
4287 Location expr_location
= this->location();
4289 bool is_parenthesized
= false;
4290 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4292 Call_expression
* call_expr
= expr
->call_expression();
4293 if (is_parenthesized
|| call_expr
== NULL
)
4295 go_error_at(expr_location
, "argument to go/defer must be function call");
4299 // Make it easier to simplify go/defer statements by putting every
4300 // statement in its own block.
4301 this->gogo_
->start_block(stat_location
);
4304 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4306 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4307 this->gogo_
->add_statement(stat
);
4308 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4312 // ReturnStat = "return" [ ExpressionList ] .
4315 Parse::return_stat()
4317 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4318 Location location
= this->location();
4319 this->advance_token();
4320 Expression_list
* vals
= NULL
;
4321 if (this->expression_may_start_here())
4322 vals
= this->expression_list(NULL
, false, true);
4323 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4326 && this->gogo_
->current_function()->func_value()->results_are_named())
4328 Named_object
* function
= this->gogo_
->current_function();
4329 Function::Results
* results
= function
->func_value()->result_variables();
4330 for (Function::Results::const_iterator p
= results
->begin();
4331 p
!= results
->end();
4334 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4336 go_assert(saw_errors());
4337 else if (!no
->is_result_variable())
4338 go_error_at(location
, "%qs is shadowed during return",
4339 (*p
)->message_name().c_str());
4344 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4345 // [ "else" ( IfStmt | Block ) ] .
4350 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4351 Location location
= this->location();
4352 this->advance_token();
4354 this->gogo_
->start_block(location
);
4356 bool saw_simple_stat
= false;
4357 Expression
* cond
= NULL
;
4358 bool saw_send_stmt
= false;
4359 if (this->simple_stat_may_start_here())
4361 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4362 saw_simple_stat
= true;
4364 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4366 // The SimpleStat is an expression statement.
4367 this->expression_stat(cond
);
4372 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4373 this->advance_token();
4374 else if (saw_simple_stat
)
4377 go_error_at(this->location(),
4378 ("send statement used as value; "
4379 "use select for non-blocking send"));
4381 go_error_at(this->location(),
4382 "expected %<;%> after statement in if expression");
4383 if (!this->expression_may_start_here())
4384 cond
= Expression::make_error(this->location());
4386 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4388 go_error_at(this->location(),
4389 "missing condition in if statement");
4390 cond
= Expression::make_error(this->location());
4393 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4396 // Check for the easy error of a newline before starting the block.
4397 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4399 Location semi_loc
= this->location();
4400 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4401 go_error_at(semi_loc
, "missing %<{%> after if clause");
4402 // Otherwise we will get an error when we call this->block
4406 this->gogo_
->start_block(this->location());
4407 Location end_loc
= this->block();
4408 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4410 // Check for the easy error of a newline before "else".
4411 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4413 Location semi_loc
= this->location();
4414 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4415 go_error_at(this->location(),
4416 "unexpected semicolon or newline before %<else%>");
4418 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4422 Block
* else_block
= NULL
;
4423 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4425 this->gogo_
->start_block(this->location());
4426 const Token
* token
= this->advance_token();
4427 if (token
->is_keyword(KEYWORD_IF
))
4429 else if (token
->is_op(OPERATOR_LCURLY
))
4433 go_error_at(this->location(), "expected %<if%> or %<{%>");
4434 this->statement(NULL
);
4436 else_block
= this->gogo_
->finish_block(this->location());
4439 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4443 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4447 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4448 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4449 // "{" { ExprCaseClause } "}" .
4450 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4451 // "{" { TypeCaseClause } "}" .
4452 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4455 Parse::switch_stat(Label
* label
)
4457 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4458 Location location
= this->location();
4459 this->advance_token();
4461 this->gogo_
->start_block(location
);
4463 bool saw_simple_stat
= false;
4464 Expression
* switch_val
= NULL
;
4466 Type_switch type_switch
;
4467 bool have_type_switch_block
= false;
4468 if (this->simple_stat_may_start_here())
4470 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4472 saw_simple_stat
= true;
4474 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4476 // The SimpleStat is an expression statement.
4477 this->expression_stat(switch_val
);
4480 if (switch_val
== NULL
&& !type_switch
.found
)
4482 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4483 this->advance_token();
4484 else if (saw_simple_stat
)
4487 go_error_at(this->location(),
4488 ("send statement used as value; "
4489 "use select for non-blocking send"));
4491 go_error_at(this->location(),
4492 "expected %<;%> after statement in switch expression");
4494 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4496 if (this->peek_token()->is_identifier())
4498 const Token
* token
= this->peek_token();
4499 std::string identifier
= token
->identifier();
4500 bool is_exported
= token
->is_identifier_exported();
4501 Location id_loc
= token
->location();
4503 token
= this->advance_token();
4504 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4505 this->unget_token(Token::make_identifier_token(identifier
,
4510 // This must be a TypeSwitchGuard. It is in a
4511 // different block from any initial SimpleStat.
4512 if (saw_simple_stat
)
4514 this->gogo_
->start_block(id_loc
);
4515 have_type_switch_block
= true;
4518 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4520 if (!type_switch
.found
)
4522 if (switch_val
== NULL
4523 || !switch_val
->is_error_expression())
4526 "expected type switch assignment");
4527 switch_val
= Expression::make_error(id_loc
);
4532 if (switch_val
== NULL
&& !type_switch
.found
)
4534 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4535 &type_switch
.found
, NULL
);
4536 if (type_switch
.found
)
4538 type_switch
.name
.clear();
4539 type_switch
.expr
= switch_val
;
4540 type_switch
.location
= switch_val
->location();
4546 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4548 Location token_loc
= this->location();
4549 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4550 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4551 go_error_at(token_loc
, "missing %<{%> after switch clause");
4552 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4554 go_error_at(token_loc
, "invalid variable name");
4555 this->advance_token();
4556 this->expression(PRECEDENCE_NORMAL
, false, false,
4557 &type_switch
.found
, NULL
);
4558 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4559 this->advance_token();
4560 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4562 if (have_type_switch_block
)
4563 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4565 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4569 if (type_switch
.found
)
4570 type_switch
.expr
= Expression::make_error(location
);
4574 go_error_at(this->location(), "expected %<{%>");
4575 if (have_type_switch_block
)
4576 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4578 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4583 this->advance_token();
4585 Statement
* statement
;
4586 if (type_switch
.found
)
4587 statement
= this->type_switch_body(label
, type_switch
, location
);
4589 statement
= this->expr_switch_body(label
, switch_val
, location
);
4591 if (statement
!= NULL
)
4592 this->gogo_
->add_statement(statement
);
4594 if (have_type_switch_block
)
4595 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4598 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4602 // The body of an expression switch.
4603 // "{" { ExprCaseClause } "}"
4606 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4609 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4612 this->push_break_statement(statement
, label
);
4614 Case_clauses
* case_clauses
= new Case_clauses();
4615 bool saw_default
= false;
4616 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4618 if (this->peek_token()->is_eof())
4621 go_error_at(this->location(), "missing %<}%>");
4624 this->expr_case_clause(case_clauses
, &saw_default
);
4626 this->advance_token();
4628 statement
->add_clauses(case_clauses
);
4630 this->pop_break_statement();
4635 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4636 // FallthroughStat = "fallthrough" .
4639 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4641 Location location
= this->location();
4643 bool is_default
= false;
4644 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4646 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4649 go_error_at(this->location(), "expected %<:%>");
4653 this->advance_token();
4655 Block
* statements
= NULL
;
4656 if (this->statement_list_may_start_here())
4658 this->gogo_
->start_block(this->location());
4659 this->statement_list();
4660 statements
= this->gogo_
->finish_block(this->location());
4663 bool is_fallthrough
= false;
4664 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4666 Location fallthrough_loc
= this->location();
4667 is_fallthrough
= true;
4668 while (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4670 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4671 go_error_at(fallthrough_loc
,
4672 _("cannot fallthrough final case in switch"));
4673 else if (!this->peek_token()->is_keyword(KEYWORD_CASE
)
4674 && !this->peek_token()->is_keyword(KEYWORD_DEFAULT
))
4676 go_error_at(fallthrough_loc
, "fallthrough statement out of place");
4677 while (!this->peek_token()->is_keyword(KEYWORD_CASE
)
4678 && !this->peek_token()->is_keyword(KEYWORD_DEFAULT
)
4679 && !this->peek_token()->is_op(OPERATOR_RCURLY
)
4680 && !this->peek_token()->is_eof())
4682 if (this->statement_may_start_here())
4683 this->statement_list();
4685 this->advance_token();
4694 go_error_at(location
, "multiple defaults in switch");
4697 *saw_default
= true;
4700 if (is_default
|| vals
!= NULL
)
4701 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4704 // ExprSwitchCase = "case" ExpressionList | "default" .
4707 Parse::expr_switch_case(bool* is_default
)
4709 const Token
* token
= this->peek_token();
4710 if (token
->is_keyword(KEYWORD_CASE
))
4712 this->advance_token();
4713 return this->expression_list(NULL
, false, true);
4715 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4717 this->advance_token();
4724 go_error_at(this->location(), "expected %<case%> or %<default%>");
4725 if (!token
->is_op(OPERATOR_RCURLY
))
4726 this->advance_token();
4731 // The body of a type switch.
4732 // "{" { TypeCaseClause } "}" .
4735 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4738 Expression
* init
= type_switch
.expr
;
4739 std::string var_name
= type_switch
.name
;
4740 if (!var_name
.empty())
4742 if (Gogo::is_sink_name(var_name
))
4744 go_error_at(type_switch
.location
,
4745 "no new variables on left side of %<:=%>");
4750 Location loc
= type_switch
.location
;
4751 Temporary_statement
* switch_temp
=
4752 Statement::make_temporary(NULL
, init
, loc
);
4753 this->gogo_
->add_statement(switch_temp
);
4754 init
= Expression::make_temporary_reference(switch_temp
, loc
);
4758 Type_switch_statement
* statement
=
4759 Statement::make_type_switch_statement(var_name
, init
, location
);
4760 this->push_break_statement(statement
, label
);
4762 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4763 bool saw_default
= false;
4764 std::vector
<Named_object
*> implicit_vars
;
4765 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4767 if (this->peek_token()->is_eof())
4769 go_error_at(this->location(), "missing %<}%>");
4772 this->type_case_clause(var_name
, init
, case_clauses
, &saw_default
,
4775 this->advance_token();
4777 statement
->add_clauses(case_clauses
);
4779 this->pop_break_statement();
4781 // If there is a type switch variable implicitly declared in each case clause,
4782 // check that it is used in at least one of the cases.
4783 if (!var_name
.empty())
4786 for (std::vector
<Named_object
*>::iterator p
= implicit_vars
.begin();
4787 p
!= implicit_vars
.end();
4790 if ((*p
)->var_value()->is_used())
4797 go_error_at(type_switch
.location
, "%qs declared and not used",
4798 Gogo::message_name(var_name
).c_str());
4803 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4804 // IMPLICIT_VARS is the list of variables implicitly declared for each type
4805 // case if there is a type switch variable declared.
4808 Parse::type_case_clause(const std::string
& var_name
, Expression
* init
,
4809 Type_case_clauses
* clauses
, bool* saw_default
,
4810 std::vector
<Named_object
*>* implicit_vars
)
4812 Location location
= this->location();
4814 std::vector
<Type
*> types
;
4815 bool is_default
= false;
4816 this->type_switch_case(&types
, &is_default
);
4818 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4819 go_error_at(this->location(), "expected %<:%>");
4821 this->advance_token();
4823 Block
* statements
= NULL
;
4824 if (this->statement_list_may_start_here())
4826 this->gogo_
->start_block(this->location());
4827 if (!var_name
.empty())
4830 Location var_loc
= init
->location();
4831 if (types
.size() == 1)
4833 type
= types
.front();
4834 init
= Expression::make_type_guard(init
, type
, location
);
4837 Variable
* v
= new Variable(type
, init
, false, false, false,
4840 v
->set_is_type_switch_var();
4841 implicit_vars
->push_back(this->gogo_
->add_variable(var_name
, v
));
4843 this->statement_list();
4844 statements
= this->gogo_
->finish_block(this->location());
4847 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4849 go_error_at(this->location(),
4850 "fallthrough is not permitted in a type switch");
4851 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4852 this->advance_token();
4857 go_assert(types
.empty());
4860 go_error_at(location
, "multiple defaults in type switch");
4863 *saw_default
= true;
4864 clauses
->add(NULL
, false, true, statements
, location
);
4866 else if (!types
.empty())
4868 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4869 p
+ 1 != types
.end();
4871 clauses
->add(*p
, true, false, NULL
, location
);
4872 clauses
->add(types
.back(), false, false, statements
, location
);
4875 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4878 // TypeSwitchCase = "case" type | "default"
4880 // We accept a comma separated list of types.
4883 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4885 const Token
* token
= this->peek_token();
4886 if (token
->is_keyword(KEYWORD_CASE
))
4888 this->advance_token();
4891 Type
* t
= this->type();
4893 if (!t
->is_error_type())
4894 types
->push_back(t
);
4897 this->gogo_
->mark_locals_used();
4898 token
= this->peek_token();
4899 while (!token
->is_op(OPERATOR_COLON
)
4900 && !token
->is_op(OPERATOR_COMMA
)
4901 && !token
->is_op(OPERATOR_RCURLY
)
4902 && !token
->is_eof())
4903 token
= this->advance_token();
4906 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4908 this->advance_token();
4911 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4913 this->advance_token();
4918 go_error_at(this->location(), "expected %<case%> or %<default%>");
4919 if (!token
->is_op(OPERATOR_RCURLY
))
4920 this->advance_token();
4924 // SelectStat = "select" "{" { CommClause } "}" .
4927 Parse::select_stat(Label
* label
)
4929 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4930 Location location
= this->location();
4931 const Token
* token
= this->advance_token();
4933 if (!token
->is_op(OPERATOR_LCURLY
))
4935 Location token_loc
= token
->location();
4936 if (token
->is_op(OPERATOR_SEMICOLON
)
4937 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4938 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4941 go_error_at(this->location(), "expected %<{%>");
4945 this->advance_token();
4947 Select_statement
* statement
= Statement::make_select_statement(location
);
4949 this->push_break_statement(statement
, label
);
4951 Select_clauses
* select_clauses
= new Select_clauses();
4952 bool saw_default
= false;
4953 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4955 if (this->peek_token()->is_eof())
4957 go_error_at(this->location(), "expected %<}%>");
4960 this->comm_clause(select_clauses
, &saw_default
);
4963 this->advance_token();
4965 statement
->add_clauses(select_clauses
);
4967 this->pop_break_statement();
4969 this->gogo_
->add_statement(statement
);
4972 // CommClause = CommCase ":" { Statement ";" } .
4975 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4977 Location location
= this->location();
4978 bool is_send
= false;
4979 Expression
* channel
= NULL
;
4980 Expression
* val
= NULL
;
4981 Expression
* closed
= NULL
;
4982 std::string varname
;
4983 std::string closedname
;
4984 bool is_default
= false;
4985 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4986 &varname
, &closedname
, &is_default
);
4988 if (this->peek_token()->is_op(OPERATOR_COLON
))
4989 this->advance_token();
4991 go_error_at(this->location(), "expected colon");
4993 this->gogo_
->start_block(this->location());
4995 Named_object
* var
= NULL
;
4996 if (!varname
.empty())
4998 // FIXME: LOCATION is slightly wrong here.
4999 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
5001 v
->set_type_from_chan_element();
5002 var
= this->gogo_
->add_variable(varname
, v
);
5005 Named_object
* closedvar
= NULL
;
5006 if (!closedname
.empty())
5008 // FIXME: LOCATION is slightly wrong here.
5009 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
5010 false, false, false, location
);
5011 closedvar
= this->gogo_
->add_variable(closedname
, v
);
5014 this->statement_list();
5016 Block
* statements
= this->gogo_
->finish_block(this->location());
5022 go_error_at(location
, "multiple defaults in select");
5025 *saw_default
= true;
5029 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
5030 statements
, location
);
5031 else if (statements
!= NULL
)
5033 // Add the statements to make sure that any names they define
5035 this->gogo_
->add_block(statements
, location
);
5039 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
5042 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
5043 Expression
** closed
, std::string
* varname
,
5044 std::string
* closedname
, bool* is_default
)
5046 const Token
* token
= this->peek_token();
5047 if (token
->is_keyword(KEYWORD_DEFAULT
))
5049 this->advance_token();
5052 else if (token
->is_keyword(KEYWORD_CASE
))
5054 this->advance_token();
5055 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
5061 go_error_at(this->location(), "expected %<case%> or %<default%>");
5062 if (!token
->is_op(OPERATOR_RCURLY
))
5063 this->advance_token();
5070 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
5071 // RecvExpr = Expression .
5074 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
5075 Expression
** closed
, std::string
* varname
,
5076 std::string
* closedname
)
5078 const Token
* token
= this->peek_token();
5079 bool saw_comma
= false;
5080 bool closed_is_id
= false;
5081 if (token
->is_identifier())
5083 Gogo
* gogo
= this->gogo_
;
5084 std::string recv_var
= token
->identifier();
5085 bool is_rv_exported
= token
->is_identifier_exported();
5086 Location recv_var_loc
= token
->location();
5087 token
= this->advance_token();
5088 if (token
->is_op(OPERATOR_COLONEQ
))
5091 this->advance_token();
5092 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
5094 Receive_expression
* re
= e
->receive_expression();
5097 if (!e
->is_error_expression())
5098 go_error_at(this->location(), "expected receive expression");
5101 if (recv_var
== "_")
5103 go_error_at(recv_var_loc
,
5104 "no new variables on left side of %<:=%>");
5105 recv_var
= Gogo::erroneous_name();
5108 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
5109 *channel
= re
->channel();
5112 else if (token
->is_op(OPERATOR_COMMA
))
5114 token
= this->advance_token();
5115 if (token
->is_identifier())
5117 std::string recv_closed
= token
->identifier();
5118 bool is_rc_exported
= token
->is_identifier_exported();
5119 Location recv_closed_loc
= token
->location();
5120 closed_is_id
= true;
5122 token
= this->advance_token();
5123 if (token
->is_op(OPERATOR_COLONEQ
))
5125 // case rv, rc := <-c:
5126 this->advance_token();
5127 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
5129 Receive_expression
* re
= e
->receive_expression();
5132 if (!e
->is_error_expression())
5133 go_error_at(this->location(),
5134 "expected receive expression");
5137 if (recv_var
== "_" && recv_closed
== "_")
5139 go_error_at(recv_var_loc
,
5140 "no new variables on left side of %<:=%>");
5141 recv_var
= Gogo::erroneous_name();
5144 if (recv_var
!= "_")
5145 *varname
= gogo
->pack_hidden_name(recv_var
,
5147 if (recv_closed
!= "_")
5148 *closedname
= gogo
->pack_hidden_name(recv_closed
,
5150 *channel
= re
->channel();
5154 this->unget_token(Token::make_identifier_token(recv_closed
,
5159 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5161 recv_var_loc
, true);
5165 this->unget_token(Token::make_identifier_token(recv_var
,
5170 // If SAW_COMMA is false, then we are looking at the start of the
5171 // send or receive expression. If SAW_COMMA is true, then *VAL is
5172 // set and we just read a comma.
5175 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5177 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5178 if (e
->receive_expression() != NULL
)
5181 *channel
= e
->receive_expression()->channel();
5182 // This is 'case (<-c):'. We now expect ':'. If we see
5183 // '<-', then we have case (<-c)<-v:
5184 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5192 this->advance_token();
5193 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5195 // The next token should be ':'. If it is '<-', then we have
5197 // which is to say, send on a channel received from a channel.
5198 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5201 e
= Expression::make_receive(*channel
, (*channel
)->location());
5204 if (!saw_comma
&& this->peek_token()->is_op(OPERATOR_COMMA
))
5206 this->advance_token();
5208 if (!e
->is_sink_expression())
5210 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5214 if (this->peek_token()->is_op(OPERATOR_EQ
))
5217 this->advance_token();
5218 Location recvloc
= this->location();
5219 Expression
* recvexpr
= this->expression(PRECEDENCE_NORMAL
, false,
5221 if (recvexpr
->receive_expression() == NULL
)
5223 go_error_at(recvloc
, "missing %<<-%>");
5226 *channel
= recvexpr
->receive_expression()->channel();
5230 // *VAL is already set.
5231 if (!e
->is_sink_expression())
5237 if (!e
->is_sink_expression())
5246 go_error_at(this->location(), "expected %<=%> or %<:=%>");
5248 go_error_at(this->location(), "expected %<=%>");
5252 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5256 *channel
= this->verify_not_sink(e
);
5257 this->advance_token();
5258 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5262 go_error_at(this->location(), "expected %<<-%> or %<=%>");
5266 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5267 // Condition = Expression .
5270 Parse::for_stat(Label
* label
)
5272 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5273 Location location
= this->location();
5274 const Token
* token
= this->advance_token();
5276 // Open a block to hold any variables defined in the init statement
5277 // of the for statement.
5278 this->gogo_
->start_block(location
);
5281 Expression
* cond
= NULL
;
5283 Range_clause range_clause
;
5285 if (!token
->is_op(OPERATOR_LCURLY
))
5287 if (token
->is_keyword(KEYWORD_VAR
))
5289 go_error_at(this->location(),
5290 "var declaration not allowed in for initializer");
5294 if (token
->is_op(OPERATOR_SEMICOLON
))
5295 this->for_clause(&cond
, &post
);
5298 // We might be looking at a Condition, an InitStat, or a
5301 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5302 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5304 if (cond
== NULL
&& !range_clause
.found
)
5307 go_error_at(this->location(),
5308 ("send statement used as value; "
5309 "use select for non-blocking send"));
5311 go_error_at(this->location(),
5312 "parse error in for statement");
5317 if (range_clause
.found
)
5318 go_error_at(this->location(), "parse error after range clause");
5322 // COND is actually an expression statement for
5323 // InitStat at the start of a ForClause.
5324 this->expression_stat(cond
);
5328 this->for_clause(&cond
, &post
);
5333 // Check for the easy error of a newline before starting the block.
5334 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5336 Location semi_loc
= this->location();
5337 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5338 go_error_at(semi_loc
, "missing %<{%> after for clause");
5339 // Otherwise we will get an error when we call this->block
5343 // Build the For_statement and note that it is the current target
5344 // for break and continue statements.
5346 For_statement
* sfor
;
5347 For_range_statement
* srange
;
5349 if (!range_clause
.found
)
5351 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5357 srange
= Statement::make_for_range_statement(range_clause
.index
,
5365 this->push_break_statement(s
, label
);
5366 this->push_continue_statement(s
, label
);
5368 // Gather the block of statements in the loop and add them to the
5371 this->gogo_
->start_block(this->location());
5372 Location end_loc
= this->block();
5373 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5376 sfor
->add_statements(statements
);
5378 srange
->add_statements(statements
);
5380 // This is no longer the break/continue target.
5381 this->pop_break_statement();
5382 this->pop_continue_statement();
5384 // Add the For_statement to the list of statements, and close out
5385 // the block we started to hold any variables defined in the for
5388 this->gogo_
->add_statement(s
);
5390 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5394 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5395 // InitStat = SimpleStat .
5396 // PostStat = SimpleStat .
5398 // We have already read InitStat at this point.
5401 Parse::for_clause(Expression
** cond
, Block
** post
)
5403 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5404 this->advance_token();
5405 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5407 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5409 go_error_at(this->location(), "missing %<{%> after for clause");
5415 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5416 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5417 go_error_at(this->location(), "expected semicolon");
5419 this->advance_token();
5421 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5425 this->gogo_
->start_block(this->location());
5426 this->simple_stat(false, NULL
, NULL
, NULL
);
5427 *post
= this->gogo_
->finish_block(this->location());
5431 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5433 // This is the := version. It is called with a list of identifiers.
5436 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5437 Range_clause
* p_range_clause
)
5439 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5440 Location location
= this->location();
5442 p_range_clause
->found
= true;
5444 if (til
->size() > 2)
5445 go_error_at(this->location(), "too many variables for range clause");
5447 this->advance_token();
5448 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5450 p_range_clause
->range
= expr
;
5455 bool any_new
= false;
5457 const Typed_identifier
* pti
= &til
->front();
5458 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5460 if (any_new
&& no
->is_variable())
5461 no
->var_value()->set_type_from_range_index();
5462 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5464 if (til
->size() == 1)
5465 p_range_clause
->value
= NULL
;
5469 bool is_new
= false;
5470 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5471 if (is_new
&& no
->is_variable())
5472 no
->var_value()->set_type_from_range_value();
5475 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5479 go_error_at(location
, "variables redeclared but no variable is new");
5482 // The = version of RangeClause. This is called with a list of
5486 Parse::range_clause_expr(const Expression_list
* vals
,
5487 Range_clause
* p_range_clause
)
5489 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5491 p_range_clause
->found
= true;
5493 go_assert(vals
->size() >= 1);
5494 if (vals
->size() > 2)
5495 go_error_at(this->location(), "too many variables for range clause");
5497 this->advance_token();
5498 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5504 p_range_clause
->index
= vals
->front();
5505 if (vals
->size() == 1)
5506 p_range_clause
->value
= NULL
;
5508 p_range_clause
->value
= vals
->back();
5511 // Push a statement on the break stack.
5514 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5516 if (this->break_stack_
== NULL
)
5517 this->break_stack_
= new Bc_stack();
5518 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5521 // Push a statement on the continue stack.
5524 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5526 if (this->continue_stack_
== NULL
)
5527 this->continue_stack_
= new Bc_stack();
5528 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5531 // Pop the break stack.
5534 Parse::pop_break_statement()
5536 this->break_stack_
->pop_back();
5539 // Pop the continue stack.
5542 Parse::pop_continue_statement()
5544 this->continue_stack_
->pop_back();
5547 // Find a break or continue statement given a label name.
5550 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5552 if (bc_stack
== NULL
)
5554 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5555 p
!= bc_stack
->rend();
5558 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5560 p
->second
->set_is_used();
5567 // BreakStat = "break" [ identifier ] .
5572 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5573 Location location
= this->location();
5575 const Token
* token
= this->advance_token();
5576 Statement
* enclosing
;
5577 if (!token
->is_identifier())
5579 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5581 go_error_at(this->location(),
5582 "break statement not within for or switch or select");
5585 enclosing
= this->break_stack_
->back().first
;
5589 enclosing
= this->find_bc_statement(this->break_stack_
,
5590 token
->identifier());
5591 if (enclosing
== NULL
)
5593 // If there is a label with this name, mark it as used to
5594 // avoid a useless error about an unused label.
5595 this->gogo_
->add_label_reference(token
->identifier(),
5596 Linemap::unknown_location(), false);
5598 go_error_at(token
->location(), "invalid break label %qs",
5599 Gogo::message_name(token
->identifier()).c_str());
5600 this->advance_token();
5603 this->advance_token();
5606 Unnamed_label
* label
;
5607 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5608 label
= enclosing
->for_statement()->break_label();
5609 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5610 label
= enclosing
->for_range_statement()->break_label();
5611 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5612 label
= enclosing
->switch_statement()->break_label();
5613 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5614 label
= enclosing
->type_switch_statement()->break_label();
5615 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5616 label
= enclosing
->select_statement()->break_label();
5620 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5624 // ContinueStat = "continue" [ identifier ] .
5627 Parse::continue_stat()
5629 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5630 Location location
= this->location();
5632 const Token
* token
= this->advance_token();
5633 Statement
* enclosing
;
5634 if (!token
->is_identifier())
5636 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5638 go_error_at(this->location(), "continue statement not within for");
5641 enclosing
= this->continue_stack_
->back().first
;
5645 enclosing
= this->find_bc_statement(this->continue_stack_
,
5646 token
->identifier());
5647 if (enclosing
== NULL
)
5649 // If there is a label with this name, mark it as used to
5650 // avoid a useless error about an unused label.
5651 this->gogo_
->add_label_reference(token
->identifier(),
5652 Linemap::unknown_location(), false);
5654 go_error_at(token
->location(), "invalid continue label %qs",
5655 Gogo::message_name(token
->identifier()).c_str());
5656 this->advance_token();
5659 this->advance_token();
5662 Unnamed_label
* label
;
5663 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5664 label
= enclosing
->for_statement()->continue_label();
5665 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5666 label
= enclosing
->for_range_statement()->continue_label();
5670 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5674 // GotoStat = "goto" identifier .
5679 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5680 Location location
= this->location();
5681 const Token
* token
= this->advance_token();
5682 if (!token
->is_identifier())
5683 go_error_at(this->location(), "expected label for goto");
5686 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5688 Statement
* s
= Statement::make_goto_statement(label
, location
);
5689 this->gogo_
->add_statement(s
);
5690 this->advance_token();
5694 // PackageClause = "package" PackageName .
5697 Parse::package_clause()
5699 const Token
* token
= this->peek_token();
5700 Location location
= token
->location();
5702 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5704 go_error_at(this->location(), "program must start with package clause");
5709 token
= this->advance_token();
5710 if (token
->is_identifier())
5712 name
= token
->identifier();
5715 go_error_at(this->location(), "invalid package name _");
5716 name
= Gogo::erroneous_name();
5718 this->advance_token();
5722 go_error_at(this->location(), "package name must be an identifier");
5726 this->gogo_
->set_package_name(name
, location
);
5729 // ImportDecl = "import" Decl<ImportSpec> .
5732 Parse::import_decl()
5734 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5735 this->advance_token();
5736 this->decl(&Parse::import_spec
, NULL
, 0);
5739 // ImportSpec = [ "." | PackageName ] PackageFileName .
5742 Parse::import_spec(void*, unsigned int pragmas
)
5745 go_warning_at(this->location(), 0,
5746 "ignoring magic //go:... comment before import");
5748 const Token
* token
= this->peek_token();
5749 Location location
= token
->location();
5751 std::string local_name
;
5752 bool is_local_name_exported
= false;
5753 if (token
->is_op(OPERATOR_DOT
))
5756 token
= this->advance_token();
5758 else if (token
->is_identifier())
5760 local_name
= token
->identifier();
5761 is_local_name_exported
= token
->is_identifier_exported();
5762 token
= this->advance_token();
5765 if (!token
->is_string())
5767 go_error_at(this->location(), "import statement not a string");
5768 this->advance_token();
5772 this->gogo_
->import_package(token
->string_value(), local_name
,
5773 is_local_name_exported
, true, location
);
5775 this->advance_token();
5778 // SourceFile = PackageClause ";" { ImportDecl ";" }
5779 // { TopLevelDecl ";" } .
5784 this->package_clause();
5786 const Token
* token
= this->peek_token();
5787 if (token
->is_op(OPERATOR_SEMICOLON
))
5788 token
= this->advance_token();
5790 go_error_at(this->location(),
5791 "expected %<;%> or newline after package clause");
5793 while (token
->is_keyword(KEYWORD_IMPORT
))
5795 this->import_decl();
5796 token
= this->peek_token();
5797 if (token
->is_op(OPERATOR_SEMICOLON
))
5798 token
= this->advance_token();
5800 go_error_at(this->location(),
5801 "expected %<;%> or newline after import declaration");
5804 while (!token
->is_eof())
5806 if (this->declaration_may_start_here())
5807 this->declaration();
5810 go_error_at(this->location(), "expected declaration");
5811 this->gogo_
->mark_locals_used();
5813 this->advance_token();
5814 while (!this->peek_token()->is_eof()
5815 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5816 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5817 if (!this->peek_token()->is_eof()
5818 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5819 this->advance_token();
5821 token
= this->peek_token();
5822 if (token
->is_op(OPERATOR_SEMICOLON
))
5823 token
= this->advance_token();
5824 else if (!token
->is_eof() || !saw_errors())
5826 if (token
->is_op(OPERATOR_CHANOP
))
5827 go_error_at(this->location(),
5828 ("send statement used as value; "
5829 "use select for non-blocking send"));
5831 go_error_at(this->location(),
5832 ("expected %<;%> or newline after top "
5833 "level declaration"));
5834 this->skip_past_error(OPERATOR_INVALID
);
5839 // Skip forward to a semicolon or OP. OP will normally be
5840 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5841 // past it and return. If we find OP, it will be the next token to
5842 // read. Return true if we are OK, false if we found EOF.
5845 Parse::skip_past_error(Operator op
)
5847 this->gogo_
->mark_locals_used();
5848 const Token
* token
= this->peek_token();
5849 while (!token
->is_op(op
))
5851 if (token
->is_eof())
5853 if (token
->is_op(OPERATOR_SEMICOLON
))
5855 this->advance_token();
5858 token
= this->advance_token();
5863 // Check that an expression is not a sink.
5866 Parse::verify_not_sink(Expression
* expr
)
5868 if (expr
->is_sink_expression())
5870 go_error_at(expr
->location(), "cannot use _ as value");
5871 expr
= Expression::make_error(expr
->location());
5874 // If this can not be a sink, and it is a variable, then we are
5875 // using the variable, not just assigning to it.
5876 if (expr
->var_expression() != NULL
)
5877 this->mark_var_used(expr
->var_expression()->named_object());
5878 else if (expr
->enclosed_var_expression() != NULL
)
5879 this->mark_var_used(expr
->enclosed_var_expression()->variable());
5883 // Mark a variable as used.
5886 Parse::mark_var_used(Named_object
* no
)
5888 if (no
->is_variable())
5889 no
->var_value()->set_is_used();