1 // parse.cc -- Go frontend parser.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
12 #include "statements.h"
13 #include "expressions.h"
16 // Struct Parse::Enclosing_var_comparison.
18 // Return true if v1 should be considered to be less than v2.
21 Parse::Enclosing_var_comparison::operator()(const Enclosing_var
& v1
,
22 const Enclosing_var
& v2
)
24 if (v1
.var() == v2
.var())
27 const std::string
& n1(v1
.var()->name());
28 const std::string
& n2(v2
.var()->name());
29 int i
= n1
.compare(n2
);
35 // If we get here it means that a single nested function refers to
36 // two different variables defined in enclosing functions, and both
37 // variables have the same name. I think this is impossible.
43 Parse::Parse(Lex
* lex
, Gogo
* gogo
)
45 token_(Token::make_invalid_token(Linemap::unknown_location())),
46 unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
47 unget_token_valid_(false),
48 is_erroneous_function_(false),
51 continue_stack_(NULL
),
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 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 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 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();
203 token
= this->advance_token();
204 if (!token
->is_identifier())
206 error_at(this->location(), "expected identifier");
210 name
= token
->identifier();
214 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 error_at(this->location(), "expected %<)%>");
278 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 error_at(location
, "invalid reference to hidden type %<%s.%s%>",
333 Gogo::message_name(packname
).c_str(),
334 Gogo::message_name(name
).c_str());
341 if (named_object
== NULL
)
344 named_object
= this->gogo_
->add_unknown_name(name
, location
);
347 const std::string
& packname(package
->package_value()->package_name());
348 error_at(location
, "reference to undefined identifier %<%s.%s%>",
349 Gogo::message_name(packname
).c_str(),
350 Gogo::message_name(name
).c_str());
355 else if (named_object
->is_type())
357 if (!named_object
->type_value()->is_visible())
360 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
368 error_at(location
, "expected type");
369 return Type::make_error_type();
372 if (named_object
->is_type())
373 return named_object
->type_value();
374 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
375 return Type::make_forward_declaration(named_object
);
380 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
381 // ArrayLength = Expression .
382 // ElementType = CompleteType .
385 Parse::array_type(bool may_use_ellipsis
)
387 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
388 const Token
* token
= this->advance_token();
390 Expression
* length
= NULL
;
391 if (token
->is_op(OPERATOR_RSQUARE
))
392 this->advance_token();
395 if (!token
->is_op(OPERATOR_ELLIPSIS
))
396 length
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
397 else if (may_use_ellipsis
)
399 // An ellipsis is used in composite literals to represent a
400 // fixed array of the size of the number of elements. We
401 // use a length of nil to represent this, and change the
402 // length when parsing the composite literal.
403 length
= Expression::make_nil(this->location());
404 this->advance_token();
408 error_at(this->location(),
409 "use of %<[...]%> outside of array literal");
410 length
= Expression::make_error(this->location());
411 this->advance_token();
413 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
415 error_at(this->location(), "expected %<]%>");
416 return Type::make_error_type();
418 this->advance_token();
421 Type
* element_type
= this->type();
423 return Type::make_array_type(element_type
, length
);
426 // MapType = "map" "[" KeyType "]" ValueType .
427 // KeyType = CompleteType .
428 // ValueType = CompleteType .
433 Location location
= this->location();
434 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP
));
435 if (!this->advance_token()->is_op(OPERATOR_LSQUARE
))
437 error_at(this->location(), "expected %<[%>");
438 return Type::make_error_type();
440 this->advance_token();
442 Type
* key_type
= this->type();
444 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
446 error_at(this->location(), "expected %<]%>");
447 return Type::make_error_type();
449 this->advance_token();
451 Type
* value_type
= this->type();
453 if (key_type
->is_error_type() || value_type
->is_error_type())
454 return Type::make_error_type();
456 return Type::make_map_type(key_type
, value_type
, location
);
459 // StructType = "struct" "{" { FieldDecl ";" } "}" .
464 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT
));
465 Location location
= this->location();
466 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
468 Location token_loc
= this->location();
469 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
470 && this->advance_token()->is_op(OPERATOR_LCURLY
))
471 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
474 error_at(this->location(), "expected %<{%>");
475 return Type::make_error_type();
478 this->advance_token();
480 Struct_field_list
* sfl
= new Struct_field_list
;
481 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
483 this->field_decl(sfl
);
484 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
485 this->advance_token();
486 else if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
488 error_at(this->location(), "expected %<;%> or %<}%> or newline");
489 if (!this->skip_past_error(OPERATOR_RCURLY
))
490 return Type::make_error_type();
493 this->advance_token();
495 for (Struct_field_list::const_iterator pi
= sfl
->begin();
499 if (pi
->type()->is_error_type())
501 for (Struct_field_list::const_iterator pj
= pi
+ 1;
505 if (pi
->field_name() == pj
->field_name()
506 && !Gogo::is_sink_name(pi
->field_name()))
507 error_at(pi
->location(), "duplicate field name %<%s%>",
508 Gogo::message_name(pi
->field_name()).c_str());
512 return Type::make_struct_type(sfl
, location
);
515 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
516 // Tag = string_lit .
519 Parse::field_decl(Struct_field_list
* sfl
)
521 const Token
* token
= this->peek_token();
522 Location location
= token
->location();
524 bool is_anonymous_pointer
;
525 if (token
->is_op(OPERATOR_MULT
))
528 is_anonymous_pointer
= true;
530 else if (token
->is_identifier())
532 std::string id
= token
->identifier();
533 bool is_id_exported
= token
->is_identifier_exported();
534 Location id_location
= token
->location();
535 token
= this->advance_token();
536 is_anonymous
= (token
->is_op(OPERATOR_SEMICOLON
)
537 || token
->is_op(OPERATOR_RCURLY
)
538 || token
->is_op(OPERATOR_DOT
)
539 || token
->is_string());
540 is_anonymous_pointer
= false;
541 this->unget_token(Token::make_identifier_token(id
, is_id_exported
,
546 error_at(this->location(), "expected field name");
547 this->gogo_
->mark_locals_used();
548 while (!token
->is_op(OPERATOR_SEMICOLON
)
549 && !token
->is_op(OPERATOR_RCURLY
)
551 token
= this->advance_token();
557 if (is_anonymous_pointer
)
559 this->advance_token();
560 if (!this->peek_token()->is_identifier())
562 error_at(this->location(), "expected field name");
563 this->gogo_
->mark_locals_used();
564 while (!token
->is_op(OPERATOR_SEMICOLON
)
565 && !token
->is_op(OPERATOR_RCURLY
)
567 token
= this->advance_token();
571 Type
* type
= this->type_name(true);
574 if (this->peek_token()->is_string())
576 tag
= this->peek_token()->string_value();
577 this->advance_token();
580 if (!type
->is_error_type())
582 if (is_anonymous_pointer
)
583 type
= Type::make_pointer_type(type
);
584 sfl
->push_back(Struct_field(Typed_identifier("", type
, location
)));
586 sfl
->back().set_tag(tag
);
591 Typed_identifier_list til
;
594 token
= this->peek_token();
595 if (!token
->is_identifier())
597 error_at(this->location(), "expected identifier");
601 this->gogo_
->pack_hidden_name(token
->identifier(),
602 token
->is_identifier_exported());
603 til
.push_back(Typed_identifier(name
, NULL
, token
->location()));
604 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
606 this->advance_token();
609 Type
* type
= this->type();
612 if (this->peek_token()->is_string())
614 tag
= this->peek_token()->string_value();
615 this->advance_token();
618 for (Typed_identifier_list::iterator p
= til
.begin();
623 sfl
->push_back(Struct_field(*p
));
625 sfl
->back().set_tag(tag
);
630 // PointerType = "*" Type .
633 Parse::pointer_type()
635 go_assert(this->peek_token()->is_op(OPERATOR_MULT
));
636 this->advance_token();
637 Type
* type
= this->type();
638 if (type
->is_error_type())
640 return Type::make_pointer_type(type
);
643 // ChannelType = Channel | SendChannel | RecvChannel .
644 // Channel = "chan" ElementType .
645 // SendChannel = "chan" "<-" ElementType .
646 // RecvChannel = "<-" "chan" ElementType .
649 Parse::channel_type()
651 const Token
* token
= this->peek_token();
654 if (token
->is_op(OPERATOR_CHANOP
))
656 if (!this->advance_token()->is_keyword(KEYWORD_CHAN
))
658 error_at(this->location(), "expected %<chan%>");
659 return Type::make_error_type();
662 this->advance_token();
666 go_assert(token
->is_keyword(KEYWORD_CHAN
));
667 if (this->advance_token()->is_op(OPERATOR_CHANOP
))
670 this->advance_token();
674 // Better error messages for the common error of omitting the
675 // channel element type.
676 if (!this->type_may_start_here())
678 token
= this->peek_token();
679 if (token
->is_op(OPERATOR_RCURLY
))
680 error_at(this->location(), "unexpected %<}%> in channel type");
681 else if (token
->is_op(OPERATOR_RPAREN
))
682 error_at(this->location(), "unexpected %<)%> in channel type");
683 else if (token
->is_op(OPERATOR_COMMA
))
684 error_at(this->location(), "unexpected comma in channel type");
686 error_at(this->location(), "expected channel element type");
687 return Type::make_error_type();
690 Type
* element_type
= this->type();
691 return Type::make_channel_type(send
, receive
, element_type
);
694 // Give an error for a duplicate parameter or receiver name.
697 Parse::check_signature_names(const Typed_identifier_list
* params
,
700 for (Typed_identifier_list::const_iterator p
= params
->begin();
704 if (p
->name().empty() || Gogo::is_sink_name(p
->name()))
706 std::pair
<std::string
, const Typed_identifier
*> val
=
707 std::make_pair(p
->name(), &*p
);
708 std::pair
<Parse::Names::iterator
, bool> ins
= names
->insert(val
);
711 error_at(p
->location(), "redefinition of %qs",
712 Gogo::message_name(p
->name()).c_str());
713 inform(ins
.first
->second
->location(),
714 "previous definition of %qs was here",
715 Gogo::message_name(p
->name()).c_str());
720 // Signature = Parameters [ Result ] .
722 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
723 // location of the start of the type.
725 // This returns NULL on a parse error.
728 Parse::signature(Typed_identifier
* receiver
, Location location
)
730 bool is_varargs
= false;
731 Typed_identifier_list
* params
;
732 bool params_ok
= this->parameters(¶ms
, &is_varargs
);
734 Typed_identifier_list
* results
= NULL
;
735 if (this->peek_token()->is_op(OPERATOR_LPAREN
)
736 || this->type_may_start_here())
738 if (!this->result(&results
))
746 if (receiver
!= NULL
)
747 names
[receiver
->name()] = receiver
;
749 this->check_signature_names(params
, &names
);
751 this->check_signature_names(results
, &names
);
753 Function_type
* ret
= Type::make_function_type(receiver
, params
, results
,
756 ret
->set_is_varargs();
760 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
762 // This returns false on a parse error.
765 Parse::parameters(Typed_identifier_list
** pparams
, bool* is_varargs
)
769 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
771 error_at(this->location(), "expected %<(%>");
775 Typed_identifier_list
* params
= NULL
;
776 bool saw_error
= false;
778 const Token
* token
= this->advance_token();
779 if (!token
->is_op(OPERATOR_RPAREN
))
781 params
= this->parameter_list(is_varargs
);
784 token
= this->peek_token();
787 // The optional trailing comma is picked up in parameter_list.
789 if (!token
->is_op(OPERATOR_RPAREN
))
791 error_at(this->location(), "expected %<)%>");
794 this->advance_token();
803 // ParameterList = ParameterDecl { "," ParameterDecl } .
805 // This sets *IS_VARARGS if the list ends with an ellipsis.
806 // IS_VARARGS will be NULL if varargs are not permitted.
808 // We pick up an optional trailing comma.
810 // This returns NULL if some error is seen.
812 Typed_identifier_list
*
813 Parse::parameter_list(bool* is_varargs
)
815 Location location
= this->location();
816 Typed_identifier_list
* ret
= new Typed_identifier_list();
818 bool saw_error
= false;
820 // If we see an identifier and then a comma, then we don't know
821 // whether we are looking at a list of identifiers followed by a
822 // type, or a list of types given by name. We have to do an
823 // arbitrary lookahead to figure it out.
825 bool parameters_have_names
;
826 const Token
* token
= this->peek_token();
827 if (!token
->is_identifier())
829 // This must be a type which starts with something like '*'.
830 parameters_have_names
= false;
834 std::string name
= token
->identifier();
835 bool is_exported
= token
->is_identifier_exported();
836 Location location
= token
->location();
837 token
= this->advance_token();
838 if (!token
->is_op(OPERATOR_COMMA
))
840 if (token
->is_op(OPERATOR_DOT
))
842 // This is a qualified identifier, which must turn out
844 parameters_have_names
= false;
846 else if (token
->is_op(OPERATOR_RPAREN
))
848 // A single identifier followed by a parenthesis must be
850 parameters_have_names
= false;
854 // An identifier followed by something other than a
855 // comma or a dot or a right parenthesis must be a
856 // parameter name followed by a type.
857 parameters_have_names
= true;
860 this->unget_token(Token::make_identifier_token(name
, is_exported
,
865 // An identifier followed by a comma may be the first in a
866 // list of parameter names followed by a type, or it may be
867 // the first in a list of types without parameter names. To
868 // find out we gather as many identifiers separated by
870 std::string id_name
= this->gogo_
->pack_hidden_name(name
,
872 ret
->push_back(Typed_identifier(id_name
, NULL
, location
));
873 bool just_saw_comma
= true;
874 while (this->advance_token()->is_identifier())
876 name
= this->peek_token()->identifier();
877 is_exported
= this->peek_token()->is_identifier_exported();
878 location
= this->peek_token()->location();
879 id_name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
880 ret
->push_back(Typed_identifier(id_name
, NULL
, location
));
881 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
883 just_saw_comma
= false;
890 // We saw ID1 "," ID2 "," followed by something which
891 // was not an identifier. We must be seeing the start
892 // of a type, and ID1 and ID2 must be types, and the
893 // parameters don't have names.
894 parameters_have_names
= false;
896 else if (this->peek_token()->is_op(OPERATOR_RPAREN
))
898 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
899 // and the parameters don't have names.
900 parameters_have_names
= false;
902 else if (this->peek_token()->is_op(OPERATOR_DOT
))
904 // We saw ID1 "," ID2 ".". ID2 must be a package name,
905 // ID1 must be a type, and the parameters don't have
907 parameters_have_names
= false;
908 this->unget_token(Token::make_identifier_token(name
, is_exported
,
911 just_saw_comma
= true;
915 // We saw ID1 "," ID2 followed by something other than
916 // ",", ".", or ")". We must be looking at the start of
917 // a type, and ID1 and ID2 must be parameter names.
918 parameters_have_names
= true;
921 if (parameters_have_names
)
923 go_assert(!just_saw_comma
);
924 // We have just seen ID1, ID2 xxx.
926 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
930 error_at(this->location(), "%<...%> only permits one name");
932 this->advance_token();
935 for (size_t i
= 0; i
< ret
->size(); ++i
)
936 ret
->set_type(i
, type
);
937 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
938 return saw_error
? NULL
: ret
;
939 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
940 return saw_error
? NULL
: ret
;
944 Typed_identifier_list
* tret
= new Typed_identifier_list();
945 for (Typed_identifier_list::const_iterator p
= ret
->begin();
949 Named_object
* no
= this->gogo_
->lookup(p
->name(), NULL
);
952 no
= this->gogo_
->add_unknown_name(p
->name(),
956 type
= no
->type_value();
957 else if (no
->is_unknown() || no
->is_type_declaration())
958 type
= Type::make_forward_declaration(no
);
961 error_at(p
->location(), "expected %<%s%> to be a type",
962 Gogo::message_name(p
->name()).c_str());
964 type
= Type::make_error_type();
966 tret
->push_back(Typed_identifier("", type
, p
->location()));
971 || this->peek_token()->is_op(OPERATOR_RPAREN
))
972 return saw_error
? NULL
: ret
;
977 bool mix_error
= false;
978 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
980 while (this->peek_token()->is_op(OPERATOR_COMMA
))
982 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
984 if (is_varargs
!= NULL
&& *is_varargs
)
986 error_at(this->location(), "%<...%> must be last parameter");
989 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
994 error_at(location
, "invalid named/anonymous mix");
1005 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1008 Parse::parameter_decl(bool parameters_have_names
,
1009 Typed_identifier_list
* til
,
1014 if (!parameters_have_names
)
1017 Location location
= this->location();
1018 if (!this->peek_token()->is_identifier())
1020 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1021 type
= this->type();
1024 if (is_varargs
== NULL
)
1025 error_at(this->location(), "invalid use of %<...%>");
1028 this->advance_token();
1029 if (is_varargs
== NULL
1030 && this->peek_token()->is_op(OPERATOR_RPAREN
))
1031 type
= Type::make_error_type();
1034 Type
* element_type
= this->type();
1035 type
= Type::make_array_type(element_type
, NULL
);
1041 type
= this->type_name(false);
1042 if (type
->is_error_type()
1043 || (!this->peek_token()->is_op(OPERATOR_COMMA
)
1044 && !this->peek_token()->is_op(OPERATOR_RPAREN
)))
1047 while (!this->peek_token()->is_op(OPERATOR_COMMA
)
1048 && !this->peek_token()->is_op(OPERATOR_RPAREN
)
1049 && !this->peek_token()->is_eof())
1050 this->advance_token();
1053 if (!type
->is_error_type())
1054 til
->push_back(Typed_identifier("", type
, location
));
1060 size_t orig_count
= til
->size();
1061 if (this->peek_token()->is_identifier())
1062 this->identifier_list(til
);
1065 size_t new_count
= til
->size();
1068 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1069 type
= this->type();
1072 if (is_varargs
== NULL
)
1074 error_at(this->location(), "invalid use of %<...%>");
1077 else if (new_count
> orig_count
+ 1)
1079 error_at(this->location(), "%<...%> only permits one name");
1084 this->advance_token();
1085 Type
* element_type
= this->type();
1086 type
= Type::make_array_type(element_type
, NULL
);
1088 for (size_t i
= orig_count
; i
< new_count
; ++i
)
1089 til
->set_type(i
, type
);
1093 // Result = Parameters | Type .
1095 // This returns false on a parse error.
1098 Parse::result(Typed_identifier_list
** presults
)
1100 if (this->peek_token()->is_op(OPERATOR_LPAREN
))
1101 return this->parameters(presults
, NULL
);
1104 Location location
= this->location();
1105 Type
* type
= this->type();
1106 if (type
->is_error_type())
1111 Typed_identifier_list
* til
= new Typed_identifier_list();
1112 til
->push_back(Typed_identifier("", type
, location
));
1118 // Block = "{" [ StatementList ] "}" .
1120 // Returns the location of the closing brace.
1125 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
1127 Location loc
= this->location();
1128 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1129 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1130 error_at(loc
, "unexpected semicolon or newline before %<{%>");
1133 error_at(this->location(), "expected %<{%>");
1134 return Linemap::unknown_location();
1138 const Token
* token
= this->advance_token();
1140 if (!token
->is_op(OPERATOR_RCURLY
))
1142 this->statement_list();
1143 token
= this->peek_token();
1144 if (!token
->is_op(OPERATOR_RCURLY
))
1146 if (!token
->is_eof() || !saw_errors())
1147 error_at(this->location(), "expected %<}%>");
1149 this->gogo_
->mark_locals_used();
1151 // Skip ahead to the end of the block, in hopes of avoiding
1152 // lots of meaningless errors.
1153 Location ret
= token
->location();
1155 while (!token
->is_eof())
1157 if (token
->is_op(OPERATOR_LCURLY
))
1159 else if (token
->is_op(OPERATOR_RCURLY
))
1164 this->advance_token();
1168 token
= this->advance_token();
1169 ret
= token
->location();
1175 Location ret
= token
->location();
1176 this->advance_token();
1180 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1181 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1184 Parse::interface_type(bool record
)
1186 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE
));
1187 Location location
= this->location();
1189 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
1191 Location token_loc
= this->location();
1192 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1193 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1194 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
1197 error_at(this->location(), "expected %<{%>");
1198 return Type::make_error_type();
1201 this->advance_token();
1203 Typed_identifier_list
* methods
= new Typed_identifier_list();
1204 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1206 this->method_spec(methods
);
1207 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1209 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
1211 this->method_spec(methods
);
1213 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1215 error_at(this->location(), "expected %<}%>");
1216 while (!this->advance_token()->is_op(OPERATOR_RCURLY
))
1218 if (this->peek_token()->is_eof())
1219 return Type::make_error_type();
1223 this->advance_token();
1225 if (methods
->empty())
1231 Interface_type
* ret
;
1232 if (methods
== NULL
)
1233 ret
= Type::make_empty_interface_type(location
);
1235 ret
= Type::make_interface_type(methods
, location
);
1237 this->gogo_
->record_interface_type(ret
);
1241 // MethodSpec = MethodName Signature | InterfaceTypeName .
1242 // MethodName = identifier .
1243 // InterfaceTypeName = TypeName .
1246 Parse::method_spec(Typed_identifier_list
* methods
)
1248 const Token
* token
= this->peek_token();
1249 if (!token
->is_identifier())
1251 error_at(this->location(), "expected identifier");
1255 std::string name
= token
->identifier();
1256 bool is_exported
= token
->is_identifier_exported();
1257 Location location
= token
->location();
1259 if (this->advance_token()->is_op(OPERATOR_LPAREN
))
1261 // This is a MethodName.
1263 error_at(this->location(), "methods must have a unique non-blank name");
1264 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1265 Type
* type
= this->signature(NULL
, location
);
1268 methods
->push_back(Typed_identifier(name
, type
, location
));
1272 this->unget_token(Token::make_identifier_token(name
, is_exported
,
1274 Type
* type
= this->type_name(false);
1275 if (type
->is_error_type()
1276 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1277 && !this->peek_token()->is_op(OPERATOR_RCURLY
)))
1279 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1280 error_at(this->location(),
1281 "name list not allowed in interface type");
1283 error_at(location
, "expected signature or type name");
1284 this->gogo_
->mark_locals_used();
1285 token
= this->peek_token();
1286 while (!token
->is_eof()
1287 && !token
->is_op(OPERATOR_SEMICOLON
)
1288 && !token
->is_op(OPERATOR_RCURLY
))
1289 token
= this->advance_token();
1292 // This must be an interface type, but we can't check that now.
1293 // We check it and pull out the methods in
1294 // Interface_type::do_verify.
1295 methods
->push_back(Typed_identifier("", type
, location
));
1299 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1302 Parse::declaration()
1304 const Token
* token
= this->peek_token();
1306 bool saw_nointerface
= this->lex_
->get_and_clear_nointerface();
1307 if (saw_nointerface
&& !token
->is_keyword(KEYWORD_FUNC
))
1308 warning_at(token
->location(), 0,
1309 "ignoring magic //go:nointerface comment before non-method");
1311 if (token
->is_keyword(KEYWORD_CONST
))
1313 else if (token
->is_keyword(KEYWORD_TYPE
))
1315 else if (token
->is_keyword(KEYWORD_VAR
))
1317 else if (token
->is_keyword(KEYWORD_FUNC
))
1318 this->function_decl(saw_nointerface
);
1321 error_at(this->location(), "expected declaration");
1322 this->advance_token();
1327 Parse::declaration_may_start_here()
1329 const Token
* token
= this->peek_token();
1330 return (token
->is_keyword(KEYWORD_CONST
)
1331 || token
->is_keyword(KEYWORD_TYPE
)
1332 || token
->is_keyword(KEYWORD_VAR
)
1333 || token
->is_keyword(KEYWORD_FUNC
));
1336 // Decl<P> = P | "(" [ List<P> ] ")" .
1339 Parse::decl(void (Parse::*pfn
)(void*), void* varg
)
1341 if (this->peek_token()->is_eof())
1344 error_at(this->location(), "unexpected end of file");
1348 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1352 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1354 this->list(pfn
, varg
, true);
1355 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1357 error_at(this->location(), "missing %<)%>");
1358 while (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1360 if (this->peek_token()->is_eof())
1365 this->advance_token();
1369 // List<P> = P { ";" P } [ ";" ] .
1371 // In order to pick up the trailing semicolon we need to know what
1372 // might follow. This is either a '}' or a ')'.
1375 Parse::list(void (Parse::*pfn
)(void*), void* varg
, bool follow_is_paren
)
1378 Operator follow
= follow_is_paren
? OPERATOR_RPAREN
: OPERATOR_RCURLY
;
1379 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1380 || this->peek_token()->is_op(OPERATOR_COMMA
))
1382 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1383 error_at(this->location(), "unexpected comma");
1384 if (this->advance_token()->is_op(follow
))
1390 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1395 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST
));
1396 this->advance_token();
1399 Type
* last_type
= NULL
;
1400 Expression_list
* last_expr_list
= NULL
;
1402 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1403 this->const_spec(&last_type
, &last_expr_list
);
1406 this->advance_token();
1407 while (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1409 this->const_spec(&last_type
, &last_expr_list
);
1410 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1411 this->advance_token();
1412 else if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1414 error_at(this->location(), "expected %<;%> or %<)%> or newline");
1415 if (!this->skip_past_error(OPERATOR_RPAREN
))
1419 this->advance_token();
1422 if (last_expr_list
!= NULL
)
1423 delete last_expr_list
;
1426 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1429 Parse::const_spec(Type
** last_type
, Expression_list
** last_expr_list
)
1431 Typed_identifier_list til
;
1432 this->identifier_list(&til
);
1435 if (this->type_may_start_here())
1437 type
= this->type();
1439 *last_expr_list
= NULL
;
1442 Expression_list
*expr_list
;
1443 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1445 if (*last_expr_list
== NULL
)
1447 error_at(this->location(), "expected %<=%>");
1451 expr_list
= new Expression_list
;
1452 for (Expression_list::const_iterator p
= (*last_expr_list
)->begin();
1453 p
!= (*last_expr_list
)->end();
1455 expr_list
->push_back((*p
)->copy());
1459 this->advance_token();
1460 expr_list
= this->expression_list(NULL
, false, true);
1462 if (*last_expr_list
!= NULL
)
1463 delete *last_expr_list
;
1464 *last_expr_list
= expr_list
;
1467 Expression_list::const_iterator pe
= expr_list
->begin();
1468 for (Typed_identifier_list::iterator pi
= til
.begin();
1472 if (pe
== expr_list
->end())
1474 error_at(this->location(), "not enough initializers");
1480 if (!Gogo::is_sink_name(pi
->name()))
1481 this->gogo_
->add_constant(*pi
, *pe
, this->iota_value());
1486 snprintf(buf
, sizeof buf
, ".$sinkconst%d", count
);
1488 Typed_identifier
ti(std::string(buf
), type
, pi
->location());
1489 Named_object
* no
= this->gogo_
->add_constant(ti
, *pe
, this->iota_value());
1490 no
->const_value()->set_is_sink();
1493 if (pe
!= expr_list
->end())
1494 error_at(this->location(), "too many initializers");
1496 this->increment_iota();
1501 // TypeDecl = "type" Decl<TypeSpec> .
1506 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE
));
1507 this->advance_token();
1508 this->decl(&Parse::type_spec
, NULL
);
1511 // TypeSpec = identifier Type .
1514 Parse::type_spec(void*)
1516 const Token
* token
= this->peek_token();
1517 if (!token
->is_identifier())
1519 error_at(this->location(), "expected identifier");
1522 std::string name
= token
->identifier();
1523 bool is_exported
= token
->is_identifier_exported();
1524 Location location
= token
->location();
1525 token
= this->advance_token();
1527 // The scope of the type name starts at the point where the
1528 // identifier appears in the source code. We implement this by
1529 // declaring the type before we read the type definition.
1530 Named_object
* named_type
= NULL
;
1533 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1534 named_type
= this->gogo_
->declare_type(name
, location
);
1538 if (name
== "_" && this->peek_token()->is_keyword(KEYWORD_INTERFACE
))
1540 // We call Parse::interface_type explicity here because we do not want
1541 // to record an interface with a blank type name.
1542 type
= this->interface_type(false);
1544 else if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1545 type
= this->type();
1548 error_at(this->location(),
1549 "unexpected semicolon or newline in type declaration");
1550 type
= Type::make_error_type();
1551 this->advance_token();
1554 if (type
->is_error_type())
1556 this->gogo_
->mark_locals_used();
1557 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1558 && !this->peek_token()->is_eof())
1559 this->advance_token();
1564 if (named_type
->is_type_declaration())
1566 Type
* ftype
= type
->forwarded();
1567 if (ftype
->forward_declaration_type() != NULL
1568 && (ftype
->forward_declaration_type()->named_object()
1571 error_at(location
, "invalid recursive type");
1572 type
= Type::make_error_type();
1575 this->gogo_
->define_type(named_type
,
1576 Type::make_named_type(named_type
, type
,
1578 go_assert(named_type
->package() == NULL
);
1582 // This will probably give a redefinition error.
1583 this->gogo_
->add_type(name
, type
, location
);
1588 // VarDecl = "var" Decl<VarSpec> .
1593 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR
));
1594 this->advance_token();
1595 this->decl(&Parse::var_spec
, NULL
);
1598 // VarSpec = IdentifierList
1599 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1602 Parse::var_spec(void*)
1604 // Get the variable names.
1605 Typed_identifier_list til
;
1606 this->identifier_list(&til
);
1608 Location location
= this->location();
1611 Expression_list
* init
= NULL
;
1612 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1614 type
= this->type();
1615 if (type
->is_error_type())
1617 this->gogo_
->mark_locals_used();
1618 while (!this->peek_token()->is_op(OPERATOR_EQ
)
1619 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1620 && !this->peek_token()->is_eof())
1621 this->advance_token();
1623 if (this->peek_token()->is_op(OPERATOR_EQ
))
1625 this->advance_token();
1626 init
= this->expression_list(NULL
, false, true);
1631 this->advance_token();
1632 init
= this->expression_list(NULL
, false, true);
1635 this->init_vars(&til
, type
, init
, false, location
);
1641 // Create variables. TIL is a list of variable names. If TYPE is not
1642 // NULL, it is the type of all the variables. If INIT is not NULL, it
1643 // is an initializer list for the variables.
1646 Parse::init_vars(const Typed_identifier_list
* til
, Type
* type
,
1647 Expression_list
* init
, bool is_coloneq
,
1650 // Check for an initialization which can yield multiple values.
1651 if (init
!= NULL
&& init
->size() == 1 && til
->size() > 1)
1653 if (this->init_vars_from_call(til
, type
, *init
->begin(), is_coloneq
,
1656 if (this->init_vars_from_map(til
, type
, *init
->begin(), is_coloneq
,
1659 if (this->init_vars_from_receive(til
, type
, *init
->begin(), is_coloneq
,
1662 if (this->init_vars_from_type_guard(til
, type
, *init
->begin(),
1663 is_coloneq
, location
))
1667 if (init
!= NULL
&& init
->size() != til
->size())
1669 if (init
->empty() || !init
->front()->is_error_expression())
1670 error_at(location
, "wrong number of initializations");
1673 type
= Type::make_error_type();
1676 // Note that INIT was already parsed with the old name bindings, so
1677 // we don't have to worry that it will accidentally refer to the
1678 // newly declared variables. But we do have to worry about a mix of
1679 // newly declared variables and old variables if the old variables
1680 // appear in the initializations.
1682 Expression_list::const_iterator pexpr
;
1684 pexpr
= init
->begin();
1685 bool any_new
= false;
1686 Expression_list
* vars
= new Expression_list();
1687 Expression_list
* vals
= new Expression_list();
1688 for (Typed_identifier_list::const_iterator p
= til
->begin();
1693 go_assert(pexpr
!= init
->end());
1694 this->init_var(*p
, type
, init
== NULL
? NULL
: *pexpr
, is_coloneq
,
1695 false, &any_new
, vars
, vals
);
1700 go_assert(pexpr
== init
->end());
1701 if (is_coloneq
&& !any_new
)
1702 error_at(location
, "variables redeclared but no variable is new");
1703 this->finish_init_vars(vars
, vals
, location
);
1706 // See if we need to initialize a list of variables from a function
1707 // call. This returns true if we have set up the variables and the
1711 Parse::init_vars_from_call(const Typed_identifier_list
* vars
, Type
* type
,
1712 Expression
* expr
, bool is_coloneq
,
1715 Call_expression
* call
= expr
->call_expression();
1719 // This is a function call. We can't check here whether it returns
1720 // the right number of values, but it might. Declare the variables,
1721 // and then assign the results of the call to them.
1723 call
->set_expected_result_count(vars
->size());
1725 Named_object
* first_var
= NULL
;
1726 unsigned int index
= 0;
1727 bool any_new
= false;
1728 Expression_list
* ivars
= new Expression_list();
1729 Expression_list
* ivals
= new Expression_list();
1730 for (Typed_identifier_list::const_iterator pv
= vars
->begin();
1734 Expression
* init
= Expression::make_call_result(call
, index
);
1735 Named_object
* no
= this->init_var(*pv
, type
, init
, is_coloneq
, false,
1736 &any_new
, ivars
, ivals
);
1738 if (this->gogo_
->in_global_scope() && no
->is_variable())
1740 if (first_var
== NULL
)
1744 // If the current object is a redefinition of another object, we
1745 // might have already recorded the dependency relationship between
1746 // it and the first variable. Either way, an error will be
1747 // reported for the redefinition and we don't need to properly
1748 // record dependency information for an invalid program.
1749 if (no
->is_redefinition())
1752 // The subsequent vars have an implicit dependency on
1753 // the first one, so that everything gets initialized in
1754 // the right order and so that we detect cycles
1756 this->gogo_
->record_var_depends_on(no
->var_value(), first_var
);
1761 if (is_coloneq
&& !any_new
)
1762 error_at(location
, "variables redeclared but no variable is new");
1764 this->finish_init_vars(ivars
, ivals
, location
);
1769 // See if we need to initialize a pair of values from a map index
1770 // expression. This returns true if we have set up the variables and
1771 // the initialization.
1774 Parse::init_vars_from_map(const Typed_identifier_list
* vars
, Type
* type
,
1775 Expression
* expr
, bool is_coloneq
,
1778 Index_expression
* index
= expr
->index_expression();
1781 if (vars
->size() != 2)
1784 // This is an index which is being assigned to two variables. It
1785 // must be a map index. Declare the variables, and then assign the
1786 // results of the map index.
1787 bool any_new
= false;
1788 Typed_identifier_list::const_iterator p
= vars
->begin();
1789 Expression
* init
= type
== NULL
? index
: NULL
;
1790 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1791 type
== NULL
, &any_new
, NULL
, NULL
);
1792 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1793 val_no
->var_value()->set_type_from_init_tuple();
1794 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1797 Type
* var_type
= type
;
1798 if (var_type
== NULL
)
1799 var_type
= Type::lookup_bool_type();
1800 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1801 &any_new
, NULL
, NULL
);
1802 Expression
* present_var
= Expression::make_var_reference(no
, location
);
1804 if (is_coloneq
&& !any_new
)
1805 error_at(location
, "variables redeclared but no variable is new");
1807 Statement
* s
= Statement::make_tuple_map_assignment(val_var
, present_var
,
1810 if (!this->gogo_
->in_global_scope())
1811 this->gogo_
->add_statement(s
);
1812 else if (!val_no
->is_sink())
1814 if (val_no
->is_variable())
1815 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1817 else if (!no
->is_sink())
1819 if (no
->is_variable())
1820 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1824 // Execute the map index expression just so that we can fail if
1826 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1828 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1834 // See if we need to initialize a pair of values from a receive
1835 // expression. This returns true if we have set up the variables and
1836 // the initialization.
1839 Parse::init_vars_from_receive(const Typed_identifier_list
* vars
, Type
* type
,
1840 Expression
* expr
, bool is_coloneq
,
1843 Receive_expression
* receive
= expr
->receive_expression();
1844 if (receive
== NULL
)
1846 if (vars
->size() != 2)
1849 // This is a receive expression which is being assigned to two
1850 // variables. Declare the variables, and then assign the results of
1852 bool any_new
= false;
1853 Typed_identifier_list::const_iterator p
= vars
->begin();
1854 Expression
* init
= type
== NULL
? receive
: NULL
;
1855 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1856 type
== NULL
, &any_new
, NULL
, NULL
);
1857 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1858 val_no
->var_value()->set_type_from_init_tuple();
1859 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1862 Type
* var_type
= type
;
1863 if (var_type
== NULL
)
1864 var_type
= Type::lookup_bool_type();
1865 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1866 &any_new
, NULL
, NULL
);
1867 Expression
* received_var
= Expression::make_var_reference(no
, location
);
1869 if (is_coloneq
&& !any_new
)
1870 error_at(location
, "variables redeclared but no variable is new");
1872 Statement
* s
= Statement::make_tuple_receive_assignment(val_var
,
1877 if (!this->gogo_
->in_global_scope())
1878 this->gogo_
->add_statement(s
);
1879 else if (!val_no
->is_sink())
1881 if (val_no
->is_variable())
1882 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1884 else if (!no
->is_sink())
1886 if (no
->is_variable())
1887 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1891 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1893 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1899 // See if we need to initialize a pair of values from a type guard
1900 // expression. This returns true if we have set up the variables and
1901 // the initialization.
1904 Parse::init_vars_from_type_guard(const Typed_identifier_list
* vars
,
1905 Type
* type
, Expression
* expr
,
1906 bool is_coloneq
, Location location
)
1908 Type_guard_expression
* type_guard
= expr
->type_guard_expression();
1909 if (type_guard
== NULL
)
1911 if (vars
->size() != 2)
1914 // This is a type guard expression which is being assigned to two
1915 // variables. Declare the variables, and then assign the results of
1917 bool any_new
= false;
1918 Typed_identifier_list::const_iterator p
= vars
->begin();
1919 Type
* var_type
= type
;
1920 if (var_type
== NULL
)
1921 var_type
= type_guard
->type();
1922 Named_object
* val_no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1923 &any_new
, NULL
, NULL
);
1924 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1928 if (var_type
== NULL
)
1929 var_type
= Type::lookup_bool_type();
1930 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1931 &any_new
, NULL
, NULL
);
1932 Expression
* ok_var
= Expression::make_var_reference(no
, location
);
1934 Expression
* texpr
= type_guard
->expr();
1935 Type
* t
= type_guard
->type();
1936 Statement
* s
= Statement::make_tuple_type_guard_assignment(val_var
, ok_var
,
1940 if (is_coloneq
&& !any_new
)
1941 error_at(location
, "variables redeclared but no variable is new");
1943 if (!this->gogo_
->in_global_scope())
1944 this->gogo_
->add_statement(s
);
1945 else if (!val_no
->is_sink())
1947 if (val_no
->is_variable())
1948 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1950 else if (!no
->is_sink())
1952 if (no
->is_variable())
1953 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1957 Named_object
* dummy
= this->create_dummy_global(type
, NULL
, location
);
1958 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1964 // Create a single variable. If IS_COLONEQ is true, we permit
1965 // redeclarations in the same block, and we set *IS_NEW when we find a
1966 // new variable which is not a redeclaration.
1969 Parse::init_var(const Typed_identifier
& tid
, Type
* type
, Expression
* init
,
1970 bool is_coloneq
, bool type_from_init
, bool* is_new
,
1971 Expression_list
* vars
, Expression_list
* vals
)
1973 Location location
= tid
.location();
1975 if (Gogo::is_sink_name(tid
.name()))
1977 if (!type_from_init
&& init
!= NULL
)
1979 if (this->gogo_
->in_global_scope())
1980 return this->create_dummy_global(type
, init
, location
);
1983 // Create a dummy variable so that we will check whether the
1984 // initializer can be assigned to the type.
1985 Variable
* var
= new Variable(type
, init
, false, false, false,
1990 snprintf(buf
, sizeof buf
, "sink$%d", count
);
1992 return this->gogo_
->add_variable(buf
, var
);
1996 this->gogo_
->add_type_to_verify(type
);
1997 return this->gogo_
->add_sink();
2002 Named_object
* no
= this->gogo_
->lookup_in_block(tid
.name());
2004 && (no
->is_variable() || no
->is_result_variable()))
2006 // INIT may be NULL even when IS_COLONEQ is true for cases
2007 // like v, ok := x.(int).
2008 if (!type_from_init
&& init
!= NULL
)
2010 go_assert(vars
!= NULL
&& vals
!= NULL
);
2011 vars
->push_back(Expression::make_var_reference(no
, location
));
2012 vals
->push_back(init
);
2018 Variable
* var
= new Variable(type
, init
, this->gogo_
->in_global_scope(),
2019 false, false, location
);
2020 Named_object
* no
= this->gogo_
->add_variable(tid
.name(), var
);
2021 if (!no
->is_variable())
2023 // The name is already defined, so we just gave an error.
2024 return this->gogo_
->add_sink();
2029 // Create a dummy global variable to force an initializer to be run in
2030 // the right place. This is used when a sink variable is initialized
2034 Parse::create_dummy_global(Type
* type
, Expression
* init
,
2037 if (type
== NULL
&& init
== NULL
)
2038 type
= Type::lookup_bool_type();
2039 Variable
* var
= new Variable(type
, init
, true, false, false, location
);
2042 snprintf(buf
, sizeof buf
, "_.%d", count
);
2044 return this->gogo_
->add_variable(buf
, var
);
2047 // Finish the variable initialization by executing any assignments to
2048 // existing variables when using :=. These must be done as a tuple
2049 // assignment in case of something like n, a, b := 1, b, a.
2052 Parse::finish_init_vars(Expression_list
* vars
, Expression_list
* vals
,
2060 else if (vars
->size() == 1)
2062 go_assert(!this->gogo_
->in_global_scope());
2063 this->gogo_
->add_statement(Statement::make_assignment(vars
->front(),
2071 go_assert(!this->gogo_
->in_global_scope());
2072 this->gogo_
->add_statement(Statement::make_tuple_assignment(vars
, vals
,
2077 // SimpleVarDecl = identifier ":=" Expression .
2079 // We've already seen the identifier.
2081 // FIXME: We also have to implement
2082 // IdentifierList ":=" ExpressionList
2083 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2084 // tuple assignments here as well.
2086 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2087 // side may be a composite literal.
2089 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2092 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2093 // guard (var := expr.("type") using the literal keyword "type").
2096 Parse::simple_var_decl_or_assignment(const std::string
& name
,
2098 bool may_be_composite_lit
,
2099 Range_clause
* p_range_clause
,
2100 Type_switch
* p_type_switch
)
2102 Typed_identifier_list til
;
2103 til
.push_back(Typed_identifier(name
, NULL
, location
));
2105 std::set
<std::string
> uniq_idents
;
2106 uniq_idents
.insert(name
);
2108 // We've seen one identifier. If we see a comma now, this could be
2110 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2112 go_assert(p_type_switch
== NULL
);
2115 const Token
* token
= this->advance_token();
2116 if (!token
->is_identifier())
2119 std::string id
= token
->identifier();
2120 bool is_id_exported
= token
->is_identifier_exported();
2121 Location id_location
= token
->location();
2122 std::pair
<std::set
<std::string
>::iterator
, bool> ins
;
2124 token
= this->advance_token();
2125 if (!token
->is_op(OPERATOR_COMMA
))
2127 if (token
->is_op(OPERATOR_COLONEQ
))
2129 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2130 ins
= uniq_idents
.insert(id
);
2131 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2132 error_at(id_location
, "multiple assignments to %s",
2133 Gogo::message_name(id
).c_str());
2134 til
.push_back(Typed_identifier(id
, NULL
, location
));
2137 this->unget_token(Token::make_identifier_token(id
,
2143 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2144 ins
= uniq_idents
.insert(id
);
2145 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2146 error_at(id_location
, "multiple assignments to %s",
2147 Gogo::message_name(id
).c_str());
2148 til
.push_back(Typed_identifier(id
, NULL
, location
));
2151 // We have a comma separated list of identifiers in TIL. If the
2152 // next token is COLONEQ, then this is a simple var decl, and we
2153 // have the complete list of identifiers. If the next token is
2154 // not COLONEQ, then the only valid parse is a tuple assignment.
2155 // The list of identifiers we have so far is really a list of
2156 // expressions. There are more expressions following.
2158 if (!this->peek_token()->is_op(OPERATOR_COLONEQ
))
2160 Expression_list
* exprs
= new Expression_list
;
2161 for (Typed_identifier_list::const_iterator p
= til
.begin();
2164 exprs
->push_back(this->id_to_expression(p
->name(), p
->location(),
2167 Expression_list
* more_exprs
=
2168 this->expression_list(NULL
, true, may_be_composite_lit
);
2169 for (Expression_list::const_iterator p
= more_exprs
->begin();
2170 p
!= more_exprs
->end();
2172 exprs
->push_back(*p
);
2175 this->tuple_assignment(exprs
, may_be_composite_lit
, p_range_clause
);
2180 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ
));
2181 const Token
* token
= this->advance_token();
2183 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
2185 this->range_clause_decl(&til
, p_range_clause
);
2189 Expression_list
* init
;
2190 if (p_type_switch
== NULL
)
2191 init
= this->expression_list(NULL
, false, may_be_composite_lit
);
2194 bool is_type_switch
= false;
2195 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false,
2196 may_be_composite_lit
,
2197 &is_type_switch
, NULL
);
2200 p_type_switch
->found
= true;
2201 p_type_switch
->name
= name
;
2202 p_type_switch
->location
= location
;
2203 p_type_switch
->expr
= expr
;
2207 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
2209 init
= new Expression_list();
2210 init
->push_back(expr
);
2214 this->advance_token();
2215 init
= this->expression_list(expr
, false, may_be_composite_lit
);
2219 this->init_vars(&til
, NULL
, init
, true, location
);
2222 // FunctionDecl = "func" identifier Signature [ Block ] .
2223 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2225 // Deprecated gcc extension:
2226 // FunctionDecl = "func" identifier Signature
2227 // __asm__ "(" string_lit ")" .
2228 // This extension means a function whose real name is the identifier
2229 // inside the asm. This extension will be removed at some future
2230 // date. It has been replaced with //extern comments.
2232 // SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment,
2233 // which means that we omit the method from the type descriptor.
2236 Parse::function_decl(bool saw_nointerface
)
2238 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2239 Location location
= this->location();
2240 std::string extern_name
= this->lex_
->extern_name();
2241 const Token
* token
= this->advance_token();
2243 bool expected_receiver
= false;
2244 Typed_identifier
* rec
= NULL
;
2245 if (token
->is_op(OPERATOR_LPAREN
))
2247 expected_receiver
= true;
2248 rec
= this->receiver();
2249 token
= this->peek_token();
2251 else if (saw_nointerface
)
2253 warning_at(location
, 0,
2254 "ignoring magic //go:nointerface comment before non-method");
2255 saw_nointerface
= false;
2258 if (!token
->is_identifier())
2260 error_at(this->location(), "expected function name");
2265 this->gogo_
->pack_hidden_name(token
->identifier(),
2266 token
->is_identifier_exported());
2268 this->advance_token();
2270 Function_type
* fntype
= this->signature(rec
, this->location());
2272 Named_object
* named_object
= NULL
;
2274 if (this->peek_token()->is_keyword(KEYWORD_ASM
))
2276 if (!this->advance_token()->is_op(OPERATOR_LPAREN
))
2278 error_at(this->location(), "expected %<(%>");
2281 token
= this->advance_token();
2282 if (!token
->is_string())
2284 error_at(this->location(), "expected string");
2287 std::string asm_name
= token
->string_value();
2288 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
2290 error_at(this->location(), "expected %<)%>");
2293 this->advance_token();
2294 if (!Gogo::is_sink_name(name
))
2296 named_object
= this->gogo_
->declare_function(name
, fntype
, location
);
2297 if (named_object
->is_function_declaration())
2298 named_object
->func_declaration_value()->set_asm_name(asm_name
);
2302 // Check for the easy error of a newline before the opening brace.
2303 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
2305 Location semi_loc
= this->location();
2306 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
2307 error_at(this->location(),
2308 "unexpected semicolon or newline before %<{%>");
2310 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
2314 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2316 if (named_object
== NULL
)
2318 // Function declarations with the blank identifier as a name are
2319 // mostly ignored since they cannot be called. We make an object
2320 // for this declaration for type-checking purposes.
2321 if (Gogo::is_sink_name(name
))
2325 snprintf(buf
, sizeof buf
, ".$sinkfndecl%d", count
);
2327 name
= std::string(buf
);
2331 || (expected_receiver
&& rec
== NULL
))
2332 this->gogo_
->add_erroneous_name(name
);
2335 named_object
= this->gogo_
->declare_function(name
, fntype
,
2337 if (!extern_name
.empty()
2338 && named_object
->is_function_declaration())
2340 Function_declaration
* fd
=
2341 named_object
->func_declaration_value();
2342 fd
->set_asm_name(extern_name
);
2347 if (saw_nointerface
)
2348 warning_at(location
, 0,
2349 ("ignoring magic //go:nointerface comment "
2350 "before declaration"));
2354 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2357 fntype
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2358 this->is_erroneous_function_
= true;
2359 if (!Gogo::is_sink_name(name
))
2360 this->gogo_
->add_erroneous_name(name
);
2361 name
= this->gogo_
->pack_hidden_name("_", false);
2363 named_object
= this->gogo_
->start_function(name
, fntype
, true, location
);
2364 Location end_loc
= this->block();
2365 this->gogo_
->finish_function(end_loc
);
2367 && !this->is_erroneous_function_
2368 && named_object
->is_function())
2369 named_object
->func_value()->set_nointerface();
2370 this->is_erroneous_function_
= hold_is_erroneous_function
;
2374 // Receiver = Parameters .
2379 Location location
= this->location();
2380 Typed_identifier_list
* til
;
2381 if (!this->parameters(&til
, NULL
))
2383 else if (til
== NULL
|| til
->empty())
2385 error_at(location
, "method has no receiver");
2388 else if (til
->size() > 1)
2390 error_at(location
, "method has multiple receivers");
2394 return &til
->front();
2397 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2398 // Literal = BasicLit | CompositeLit | FunctionLit .
2399 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2401 // If MAY_BE_SINK is true, this operand may be "_".
2403 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2404 // if the entire expression is in parentheses.
2407 Parse::operand(bool may_be_sink
, bool* is_parenthesized
)
2409 const Token
* token
= this->peek_token();
2411 switch (token
->classification())
2413 case Token::TOKEN_IDENTIFIER
:
2415 Location location
= token
->location();
2416 std::string id
= token
->identifier();
2417 bool is_exported
= token
->is_identifier_exported();
2418 std::string packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2420 Named_object
* in_function
;
2421 Named_object
* named_object
= this->gogo_
->lookup(packed
, &in_function
);
2423 Package
* package
= NULL
;
2424 if (named_object
!= NULL
&& named_object
->is_package())
2426 if (!this->advance_token()->is_op(OPERATOR_DOT
)
2427 || !this->advance_token()->is_identifier())
2429 error_at(location
, "unexpected reference to package");
2430 return Expression::make_error(location
);
2432 package
= named_object
->package_value();
2433 package
->note_usage();
2434 id
= this->peek_token()->identifier();
2435 is_exported
= this->peek_token()->is_identifier_exported();
2436 packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2437 named_object
= package
->lookup(packed
);
2438 location
= this->location();
2439 go_assert(in_function
== NULL
);
2442 this->advance_token();
2444 if (named_object
!= NULL
2445 && named_object
->is_type()
2446 && !named_object
->type_value()->is_visible())
2448 go_assert(package
!= NULL
);
2449 error_at(location
, "invalid reference to hidden type %<%s.%s%>",
2450 Gogo::message_name(package
->package_name()).c_str(),
2451 Gogo::message_name(id
).c_str());
2452 return Expression::make_error(location
);
2456 if (named_object
== NULL
)
2458 if (package
!= NULL
)
2460 std::string n1
= Gogo::message_name(package
->package_name());
2461 std::string n2
= Gogo::message_name(id
);
2464 ("invalid reference to unexported identifier "
2466 n1
.c_str(), n2
.c_str());
2469 "reference to undefined identifier %<%s.%s%>",
2470 n1
.c_str(), n2
.c_str());
2471 return Expression::make_error(location
);
2474 named_object
= this->gogo_
->add_unknown_name(packed
, location
);
2477 if (in_function
!= NULL
2478 && in_function
!= this->gogo_
->current_function()
2479 && (named_object
->is_variable()
2480 || named_object
->is_result_variable()))
2481 return this->enclosing_var_reference(in_function
, named_object
,
2482 may_be_sink
, location
);
2484 switch (named_object
->classification())
2486 case Named_object::NAMED_OBJECT_CONST
:
2487 return Expression::make_const_reference(named_object
, location
);
2488 case Named_object::NAMED_OBJECT_TYPE
:
2489 return Expression::make_type(named_object
->type_value(), location
);
2490 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
2492 Type
* t
= Type::make_forward_declaration(named_object
);
2493 return Expression::make_type(t
, location
);
2495 case Named_object::NAMED_OBJECT_VAR
:
2496 case Named_object::NAMED_OBJECT_RESULT_VAR
:
2497 // Any left-hand-side can be a sink, so if this can not be
2498 // a sink, then it must be a use of the variable.
2500 this->mark_var_used(named_object
);
2501 return Expression::make_var_reference(named_object
, location
);
2502 case Named_object::NAMED_OBJECT_SINK
:
2504 return Expression::make_sink(location
);
2507 error_at(location
, "cannot use _ as value");
2508 return Expression::make_error(location
);
2510 case Named_object::NAMED_OBJECT_FUNC
:
2511 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
2512 return Expression::make_func_reference(named_object
, NULL
,
2514 case Named_object::NAMED_OBJECT_UNKNOWN
:
2516 Unknown_expression
* ue
=
2517 Expression::make_unknown_reference(named_object
, location
);
2518 if (this->is_erroneous_function_
)
2519 ue
->set_no_error_message();
2522 case Named_object::NAMED_OBJECT_ERRONEOUS
:
2523 return Expression::make_error(location
);
2530 case Token::TOKEN_STRING
:
2531 ret
= Expression::make_string(token
->string_value(), token
->location());
2532 this->advance_token();
2535 case Token::TOKEN_CHARACTER
:
2536 ret
= Expression::make_character(token
->character_value(), NULL
,
2538 this->advance_token();
2541 case Token::TOKEN_INTEGER
:
2542 ret
= Expression::make_integer_z(token
->integer_value(), NULL
,
2544 this->advance_token();
2547 case Token::TOKEN_FLOAT
:
2548 ret
= Expression::make_float(token
->float_value(), NULL
,
2550 this->advance_token();
2553 case Token::TOKEN_IMAGINARY
:
2556 mpfr_init_set_ui(zero
, 0, GMP_RNDN
);
2558 mpc_init2(val
, mpc_precision
);
2559 mpc_set_fr_fr(val
, zero
, *token
->imaginary_value(), MPC_RNDNN
);
2561 ret
= Expression::make_complex(&val
, NULL
, token
->location());
2563 this->advance_token();
2567 case Token::TOKEN_KEYWORD
:
2568 switch (token
->keyword())
2571 return this->function_lit();
2573 case KEYWORD_INTERFACE
:
2575 case KEYWORD_STRUCT
:
2577 Location location
= token
->location();
2578 return Expression::make_type(this->type(), location
);
2585 case Token::TOKEN_OPERATOR
:
2586 if (token
->is_op(OPERATOR_LPAREN
))
2588 this->advance_token();
2589 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2591 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2592 error_at(this->location(), "missing %<)%>");
2594 this->advance_token();
2595 if (is_parenthesized
!= NULL
)
2596 *is_parenthesized
= true;
2599 else if (token
->is_op(OPERATOR_LSQUARE
))
2601 // Here we call array_type directly, as this is the only
2602 // case where an ellipsis is permitted for an array type.
2603 Location location
= token
->location();
2604 return Expression::make_type(this->array_type(true), location
);
2612 error_at(this->location(), "expected operand");
2613 return Expression::make_error(this->location());
2616 // Handle a reference to a variable in an enclosing function. We add
2617 // it to a list of such variables. We return a reference to a field
2618 // in a struct which will be passed on the static chain when calling
2619 // the current function.
2622 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2623 bool may_be_sink
, Location location
)
2625 go_assert(var
->is_variable() || var
->is_result_variable());
2627 // Any left-hand-side can be a sink, so if this can not be
2628 // a sink, then it must be a use of the variable.
2630 this->mark_var_used(var
);
2632 Named_object
* this_function
= this->gogo_
->current_function();
2633 Named_object
* closure
= this_function
->func_value()->closure_var();
2635 // The last argument to the Enclosing_var constructor is the index
2636 // of this variable in the closure. We add 1 to the current number
2637 // of enclosed variables, because the first field in the closure
2638 // points to the function code.
2639 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2640 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2641 this->enclosing_vars_
.insert(ev
);
2644 // This is a variable we have not seen before. Add a new field
2645 // to the closure type.
2646 this_function
->func_value()->add_closure_field(var
, location
);
2649 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2651 closure_ref
= Expression::make_unary(OPERATOR_MULT
, closure_ref
, location
);
2653 // The closure structure holds pointers to the variables, so we need
2654 // to introduce an indirection.
2655 Expression
* e
= Expression::make_field_reference(closure_ref
,
2658 e
= Expression::make_unary(OPERATOR_MULT
, e
, location
);
2662 // CompositeLit = LiteralType LiteralValue .
2663 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2664 // SliceType | MapType | TypeName .
2665 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2666 // ElementList = Element { "," Element } .
2667 // Element = [ Key ":" ] Value .
2668 // Key = FieldName | ElementIndex .
2669 // FieldName = identifier .
2670 // ElementIndex = Expression .
2671 // Value = Expression | LiteralValue .
2673 // We have already seen the type if there is one, and we are now
2674 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2675 // will be seen here as an array type whose length is "nil". The
2676 // DEPTH parameter is non-zero if this is an embedded composite
2677 // literal and the type was omitted. It gives the number of steps up
2678 // to the type which was provided. E.g., in [][]int{{1}} it will be
2679 // 1. In [][][]int{{{1}}} it will be 2.
2682 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2684 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2685 this->advance_token();
2687 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2689 this->advance_token();
2690 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2694 bool has_keys
= false;
2695 bool all_are_names
= true;
2696 Expression_list
* vals
= new Expression_list
;
2700 bool is_type_omitted
= false;
2701 bool is_name
= false;
2703 const Token
* token
= this->peek_token();
2705 if (token
->is_identifier())
2707 std::string identifier
= token
->identifier();
2708 bool is_exported
= token
->is_identifier_exported();
2709 Location location
= token
->location();
2711 if (this->advance_token()->is_op(OPERATOR_COLON
))
2713 // This may be a field name. We don't know for sure--it
2714 // could also be an expression for an array index. We
2715 // don't want to parse it as an expression because may
2716 // trigger various errors, e.g., if this identifier
2717 // happens to be the name of a package.
2718 Gogo
* gogo
= this->gogo_
;
2719 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2726 this->unget_token(Token::make_identifier_token(identifier
,
2729 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2733 else if (!token
->is_op(OPERATOR_LCURLY
))
2734 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2737 // This must be a composite literal inside another composite
2738 // literal, with the type omitted for the inner one.
2739 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2740 is_type_omitted
= true;
2743 token
= this->peek_token();
2744 if (!token
->is_op(OPERATOR_COLON
))
2747 vals
->push_back(NULL
);
2752 if (is_type_omitted
&& !val
->is_error_expression())
2754 error_at(this->location(), "unexpected %<:%>");
2755 val
= Expression::make_error(this->location());
2758 this->advance_token();
2760 if (!has_keys
&& !vals
->empty())
2762 Expression_list
* newvals
= new Expression_list
;
2763 for (Expression_list::const_iterator p
= vals
->begin();
2767 newvals
->push_back(NULL
);
2768 newvals
->push_back(*p
);
2775 if (val
->unknown_expression() != NULL
)
2776 val
->unknown_expression()->set_is_composite_literal_key();
2778 vals
->push_back(val
);
2780 if (!token
->is_op(OPERATOR_LCURLY
))
2781 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2784 // This must be a composite literal inside another
2785 // composite literal, with the type omitted for the
2787 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2790 token
= this->peek_token();
2793 vals
->push_back(val
);
2796 all_are_names
= false;
2798 if (token
->is_op(OPERATOR_COMMA
))
2800 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2802 this->advance_token();
2806 else if (token
->is_op(OPERATOR_RCURLY
))
2808 this->advance_token();
2813 if (token
->is_op(OPERATOR_SEMICOLON
))
2814 error_at(this->location(),
2815 "need trailing comma before newline in composite literal");
2817 error_at(this->location(), "expected %<,%> or %<}%>");
2819 this->gogo_
->mark_locals_used();
2821 while (!token
->is_eof()
2822 && (depth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2824 if (token
->is_op(OPERATOR_LCURLY
))
2826 else if (token
->is_op(OPERATOR_RCURLY
))
2828 token
= this->advance_token();
2830 if (token
->is_op(OPERATOR_RCURLY
))
2831 this->advance_token();
2833 return Expression::make_error(location
);
2837 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2838 all_are_names
, location
);
2841 // FunctionLit = "func" Signature Block .
2844 Parse::function_lit()
2846 Location location
= this->location();
2847 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2848 this->advance_token();
2850 Enclosing_vars hold_enclosing_vars
;
2851 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2853 Function_type
* type
= this->signature(NULL
, location
);
2854 bool fntype_is_error
= false;
2857 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2858 fntype_is_error
= true;
2861 // For a function literal, the next token must be a '{'. If we
2862 // don't see that, then we may have a type expression.
2863 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2865 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2866 return Expression::make_type(type
, location
);
2869 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2870 if (fntype_is_error
)
2871 this->is_erroneous_function_
= true;
2873 Bc_stack
* hold_break_stack
= this->break_stack_
;
2874 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2875 this->break_stack_
= NULL
;
2876 this->continue_stack_
= NULL
;
2878 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2880 Location end_loc
= this->block();
2882 this->gogo_
->finish_function(end_loc
);
2884 if (this->break_stack_
!= NULL
)
2885 delete this->break_stack_
;
2886 if (this->continue_stack_
!= NULL
)
2887 delete this->continue_stack_
;
2888 this->break_stack_
= hold_break_stack
;
2889 this->continue_stack_
= hold_continue_stack
;
2891 this->is_erroneous_function_
= hold_is_erroneous_function
;
2893 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2895 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
2898 return Expression::make_func_reference(no
, closure
, location
);
2901 // Create a closure for the nested function FUNCTION. This is based
2902 // on ENCLOSING_VARS, which is a list of all variables defined in
2903 // enclosing functions and referenced from FUNCTION. A closure is the
2904 // address of a struct which point to the real function code and
2905 // contains the addresses of all the referenced variables. This
2906 // returns NULL if no closure is required.
2909 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
2912 if (enclosing_vars
->empty())
2915 // Get the variables in order by their field index.
2917 size_t enclosing_var_count
= enclosing_vars
->size();
2918 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
2919 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
2920 p
!= enclosing_vars
->end();
2923 // Subtract 1 because index 0 is the function code.
2924 ev
[p
->index() - 1] = *p
;
2927 // Build an initializer for a composite literal of the closure's
2930 Named_object
* enclosing_function
= this->gogo_
->current_function();
2931 Expression_list
* initializer
= new Expression_list
;
2933 initializer
->push_back(Expression::make_func_code_reference(function
,
2936 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
2938 // Add 1 to i because the first field in the closure is a
2939 // pointer to the function code.
2940 go_assert(ev
[i
].index() == i
+ 1);
2941 Named_object
* var
= ev
[i
].var();
2943 if (ev
[i
].in_function() == enclosing_function
)
2944 ref
= Expression::make_var_reference(var
, location
);
2946 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
2948 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
2950 initializer
->push_back(refaddr
);
2953 Named_object
* closure_var
= function
->func_value()->closure_var();
2954 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
2955 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
2957 return Expression::make_heap_expression(cv
, location
);
2960 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
2962 // If MAY_BE_SINK is true, this expression may be "_".
2964 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
2967 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
2968 // guard (var := expr.("type") using the literal keyword "type").
2970 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2971 // if the entire expression is in parentheses.
2974 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
2975 bool* is_type_switch
, bool* is_parenthesized
)
2977 Location start_loc
= this->location();
2978 bool operand_is_parenthesized
= false;
2979 bool whole_is_parenthesized
= false;
2981 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
2983 whole_is_parenthesized
= operand_is_parenthesized
;
2985 // An unknown name followed by a curly brace must be a composite
2986 // literal, and the unknown name must be a type.
2987 if (may_be_composite_lit
2988 && !operand_is_parenthesized
2989 && ret
->unknown_expression() != NULL
2990 && this->peek_token()->is_op(OPERATOR_LCURLY
))
2992 Named_object
* no
= ret
->unknown_expression()->named_object();
2993 Type
* type
= Type::make_forward_declaration(no
);
2994 ret
= Expression::make_type(type
, ret
->location());
2997 // We handle composite literals and type casts here, as it is the
2998 // easiest way to handle types which are in parentheses, as in
3000 if (ret
->is_type_expression())
3002 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
3004 whole_is_parenthesized
= false;
3005 if (!may_be_composite_lit
)
3007 Type
* t
= ret
->type();
3008 if (t
->named_type() != NULL
3009 || t
->forward_declaration_type() != NULL
)
3011 _("parentheses required around this composite literal "
3012 "to avoid parsing ambiguity"));
3014 else if (operand_is_parenthesized
)
3016 "cannot parenthesize type in composite literal");
3017 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
3019 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
3021 whole_is_parenthesized
= false;
3022 Location loc
= this->location();
3023 this->advance_token();
3024 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
3026 if (this->peek_token()->is_op(OPERATOR_COMMA
))
3027 this->advance_token();
3028 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
3030 error_at(this->location(),
3031 "invalid use of %<...%> in type conversion");
3032 this->advance_token();
3034 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3035 error_at(this->location(), "expected %<)%>");
3037 this->advance_token();
3038 if (expr
->is_error_expression())
3042 Type
* t
= ret
->type();
3043 if (t
->classification() == Type::TYPE_ARRAY
3044 && t
->array_type()->length() != NULL
3045 && t
->array_type()->length()->is_nil_expression())
3047 error_at(ret
->location(),
3048 "use of %<[...]%> outside of array literal");
3049 ret
= Expression::make_error(loc
);
3052 ret
= Expression::make_cast(t
, expr
, loc
);
3059 const Token
* token
= this->peek_token();
3060 if (token
->is_op(OPERATOR_LPAREN
))
3062 whole_is_parenthesized
= false;
3063 ret
= this->call(this->verify_not_sink(ret
));
3065 else if (token
->is_op(OPERATOR_DOT
))
3067 whole_is_parenthesized
= false;
3068 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3069 if (is_type_switch
!= NULL
&& *is_type_switch
)
3072 else if (token
->is_op(OPERATOR_LSQUARE
))
3074 whole_is_parenthesized
= false;
3075 ret
= this->index(this->verify_not_sink(ret
));
3081 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3082 *is_parenthesized
= true;
3087 // Selector = "." identifier .
3088 // TypeGuard = "." "(" QualifiedIdent ")" .
3090 // Note that Operand can expand to QualifiedIdent, which contains a
3091 // ".". That is handled directly in operand when it sees a package
3094 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3095 // guard (var := expr.("type") using the literal keyword "type").
3098 Parse::selector(Expression
* left
, bool* is_type_switch
)
3100 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3101 Location location
= this->location();
3103 const Token
* token
= this->advance_token();
3104 if (token
->is_identifier())
3106 // This could be a field in a struct, or a method in an
3107 // interface, or a method associated with a type. We can't know
3108 // which until we have seen all the types.
3110 this->gogo_
->pack_hidden_name(token
->identifier(),
3111 token
->is_identifier_exported());
3112 if (token
->identifier() == "_")
3114 error_at(this->location(), "invalid use of %<_%>");
3115 name
= Gogo::erroneous_name();
3117 this->advance_token();
3118 return Expression::make_selector(left
, name
, location
);
3120 else if (token
->is_op(OPERATOR_LPAREN
))
3122 this->advance_token();
3124 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3125 type
= this->type();
3128 if (is_type_switch
!= NULL
)
3129 *is_type_switch
= true;
3132 error_at(this->location(),
3133 "use of %<.(type)%> outside type switch");
3134 type
= Type::make_error_type();
3136 this->advance_token();
3138 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3139 error_at(this->location(), "missing %<)%>");
3141 this->advance_token();
3142 if (is_type_switch
!= NULL
&& *is_type_switch
)
3144 return Expression::make_type_guard(left
, type
, location
);
3148 error_at(this->location(), "expected identifier or %<(%>");
3153 // Index = "[" Expression "]" .
3154 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3157 Parse::index(Expression
* expr
)
3159 Location location
= this->location();
3160 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3161 this->advance_token();
3164 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3165 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3167 start
= Expression::make_integer_ul(0, NULL
, location
);
3169 Expression
* end
= NULL
;
3170 if (this->peek_token()->is_op(OPERATOR_COLON
))
3172 // We use nil to indicate a missing high expression.
3173 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3174 end
= Expression::make_nil(this->location());
3175 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3177 error_at(this->location(), "middle index required in 3-index slice");
3178 end
= Expression::make_error(this->location());
3181 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3184 Expression
* cap
= NULL
;
3185 if (this->peek_token()->is_op(OPERATOR_COLON
))
3187 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3189 error_at(this->location(), "final index required in 3-index slice");
3190 cap
= Expression::make_error(this->location());
3193 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3195 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3196 error_at(this->location(), "missing %<]%>");
3198 this->advance_token();
3199 return Expression::make_index(expr
, start
, end
, cap
, location
);
3202 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3203 // ArgumentList = ExpressionList [ "..." ] .
3206 Parse::call(Expression
* func
)
3208 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3209 Expression_list
* args
= NULL
;
3210 bool is_varargs
= false;
3211 const Token
* token
= this->advance_token();
3212 if (!token
->is_op(OPERATOR_RPAREN
))
3214 args
= this->expression_list(NULL
, false, true);
3215 token
= this->peek_token();
3216 if (token
->is_op(OPERATOR_ELLIPSIS
))
3219 token
= this->advance_token();
3222 if (token
->is_op(OPERATOR_COMMA
))
3223 token
= this->advance_token();
3224 if (!token
->is_op(OPERATOR_RPAREN
))
3226 error_at(this->location(), "missing %<)%>");
3227 if (!this->skip_past_error(OPERATOR_RPAREN
))
3228 return Expression::make_error(this->location());
3230 this->advance_token();
3231 if (func
->is_error_expression())
3233 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3236 // Return an expression for a single unqualified identifier.
3239 Parse::id_to_expression(const std::string
& name
, Location location
,
3242 Named_object
* in_function
;
3243 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3244 if (named_object
== NULL
)
3245 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3247 if (in_function
!= NULL
3248 && in_function
!= this->gogo_
->current_function()
3249 && (named_object
->is_variable() || named_object
->is_result_variable()))
3250 return this->enclosing_var_reference(in_function
, named_object
, is_lhs
,
3253 switch (named_object
->classification())
3255 case Named_object::NAMED_OBJECT_CONST
:
3256 return Expression::make_const_reference(named_object
, location
);
3257 case Named_object::NAMED_OBJECT_VAR
:
3258 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3260 this->mark_var_used(named_object
);
3261 return Expression::make_var_reference(named_object
, location
);
3262 case Named_object::NAMED_OBJECT_SINK
:
3263 return Expression::make_sink(location
);
3264 case Named_object::NAMED_OBJECT_FUNC
:
3265 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3266 return Expression::make_func_reference(named_object
, NULL
, location
);
3267 case Named_object::NAMED_OBJECT_UNKNOWN
:
3269 Unknown_expression
* ue
=
3270 Expression::make_unknown_reference(named_object
, location
);
3271 if (this->is_erroneous_function_
)
3272 ue
->set_no_error_message();
3275 case Named_object::NAMED_OBJECT_PACKAGE
:
3276 case Named_object::NAMED_OBJECT_TYPE
:
3277 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3279 // These cases can arise for a field name in a composite
3280 // literal. Keep track of these as they might be fake uses of
3281 // the related package.
3282 Unknown_expression
* ue
=
3283 Expression::make_unknown_reference(named_object
, location
);
3284 if (named_object
->package() != NULL
)
3285 named_object
->package()->note_fake_usage(ue
);
3286 if (this->is_erroneous_function_
)
3287 ue
->set_no_error_message();
3290 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3291 return Expression::make_error(location
);
3293 error_at(this->location(), "unexpected type of identifier");
3294 return Expression::make_error(location
);
3298 // Expression = UnaryExpr { binary_op Expression } .
3300 // PRECEDENCE is the precedence of the current operator.
3302 // If MAY_BE_SINK is true, this expression may be "_".
3304 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3307 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3308 // guard (var := expr.("type") using the literal keyword "type").
3310 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3311 // if the entire expression is in parentheses.
3314 Parse::expression(Precedence precedence
, bool may_be_sink
,
3315 bool may_be_composite_lit
, bool* is_type_switch
,
3316 bool *is_parenthesized
)
3318 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3319 is_type_switch
, is_parenthesized
);
3323 if (is_type_switch
!= NULL
&& *is_type_switch
)
3326 const Token
* token
= this->peek_token();
3327 if (token
->classification() != Token::TOKEN_OPERATOR
)
3333 Precedence right_precedence
;
3334 switch (token
->op())
3337 right_precedence
= PRECEDENCE_OROR
;
3339 case OPERATOR_ANDAND
:
3340 right_precedence
= PRECEDENCE_ANDAND
;
3343 case OPERATOR_NOTEQ
:
3348 right_precedence
= PRECEDENCE_RELOP
;
3351 case OPERATOR_MINUS
:
3354 right_precedence
= PRECEDENCE_ADDOP
;
3359 case OPERATOR_LSHIFT
:
3360 case OPERATOR_RSHIFT
:
3362 case OPERATOR_BITCLEAR
:
3363 right_precedence
= PRECEDENCE_MULOP
;
3366 right_precedence
= PRECEDENCE_INVALID
;
3370 if (right_precedence
== PRECEDENCE_INVALID
)
3376 if (is_parenthesized
!= NULL
)
3377 *is_parenthesized
= false;
3379 Operator op
= token
->op();
3380 Location binop_location
= token
->location();
3382 if (precedence
>= right_precedence
)
3384 // We've already seen A * B, and we see + C. We want to
3385 // return so that A * B becomes a group.
3389 this->advance_token();
3391 left
= this->verify_not_sink(left
);
3392 Expression
* right
= this->expression(right_precedence
, false,
3393 may_be_composite_lit
,
3395 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3400 Parse::expression_may_start_here()
3402 const Token
* token
= this->peek_token();
3403 switch (token
->classification())
3405 case Token::TOKEN_INVALID
:
3406 case Token::TOKEN_EOF
:
3408 case Token::TOKEN_KEYWORD
:
3409 switch (token
->keyword())
3414 case KEYWORD_STRUCT
:
3415 case KEYWORD_INTERFACE
:
3420 case Token::TOKEN_IDENTIFIER
:
3422 case Token::TOKEN_STRING
:
3424 case Token::TOKEN_OPERATOR
:
3425 switch (token
->op())
3428 case OPERATOR_MINUS
:
3432 case OPERATOR_CHANOP
:
3434 case OPERATOR_LPAREN
:
3435 case OPERATOR_LSQUARE
:
3440 case Token::TOKEN_CHARACTER
:
3441 case Token::TOKEN_INTEGER
:
3442 case Token::TOKEN_FLOAT
:
3443 case Token::TOKEN_IMAGINARY
:
3450 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3452 // If MAY_BE_SINK is true, this expression may be "_".
3454 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3457 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3458 // guard (var := expr.("type") using the literal keyword "type").
3460 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3461 // if the entire expression is in parentheses.
3464 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3465 bool* is_type_switch
, bool* is_parenthesized
)
3467 const Token
* token
= this->peek_token();
3469 // There is a complex parse for <- chan. The choices are
3470 // Convert x to type <- chan int:
3472 // Receive from (x converted to type chan <- chan int):
3473 // (<- chan <- chan int (x))
3474 // Convert x to type <- chan (<- chan int).
3475 // (<- chan <- chan int)(x)
3476 if (token
->is_op(OPERATOR_CHANOP
))
3478 Location location
= token
->location();
3479 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3481 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3483 if (expr
->is_error_expression())
3485 else if (!expr
->is_type_expression())
3486 return Expression::make_receive(expr
, location
);
3489 if (expr
->type()->is_error_type())
3492 // We picked up "chan TYPE", but it is not a type
3494 Channel_type
* ct
= expr
->type()->channel_type();
3497 // This is probably impossible.
3498 error_at(location
, "expected channel type");
3499 return Expression::make_error(location
);
3501 else if (ct
->may_receive())
3504 Type
* t
= Type::make_channel_type(false, true,
3505 ct
->element_type());
3506 return Expression::make_type(t
, location
);
3510 // <- chan <- TYPE. Because we skipped the leading
3511 // <-, we parsed this as chan <- TYPE. With the
3512 // leading <-, we parse it as <- chan (<- TYPE).
3513 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3514 return Expression::make_type(t
, location
);
3519 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3520 token
= this->peek_token();
3523 if (token
->is_op(OPERATOR_PLUS
)
3524 || token
->is_op(OPERATOR_MINUS
)
3525 || token
->is_op(OPERATOR_NOT
)
3526 || token
->is_op(OPERATOR_XOR
)
3527 || token
->is_op(OPERATOR_CHANOP
)
3528 || token
->is_op(OPERATOR_MULT
)
3529 || token
->is_op(OPERATOR_AND
))
3531 Location location
= token
->location();
3532 Operator op
= token
->op();
3533 this->advance_token();
3535 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3537 if (expr
->is_error_expression())
3539 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3540 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3542 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3543 expr
= Expression::make_heap_expression(expr
, location
);
3544 else if (op
!= OPERATOR_CHANOP
)
3545 expr
= Expression::make_unary(op
, expr
, location
);
3547 expr
= Expression::make_receive(expr
, location
);
3551 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3552 is_type_switch
, is_parenthesized
);
3555 // This is called for the obscure case of
3556 // (<- chan <- chan int)(x)
3557 // In unary_expr we remove the leading <- and parse the remainder,
3559 // chan <- (chan int)
3560 // When we add the leading <- back in, we really want
3561 // <- chan (<- chan int)
3562 // This means that we need to reassociate.
3565 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3567 Channel_type
* ele
= ct
->element_type()->channel_type();
3570 error_at(location
, "parse error");
3571 return Type::make_error_type();
3574 if (ele
->may_send())
3575 sub
= Type::make_channel_type(false, true, ele
->element_type());
3577 sub
= this->reassociate_chan_direction(ele
, location
);
3578 return Type::make_channel_type(false, true, sub
);
3582 // Declaration | LabeledStmt | SimpleStmt |
3583 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3584 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3587 // LABEL is the label of this statement if it has one.
3590 Parse::statement(Label
* label
)
3592 const Token
* token
= this->peek_token();
3593 switch (token
->classification())
3595 case Token::TOKEN_KEYWORD
:
3597 switch (token
->keyword())
3602 this->declaration();
3606 case KEYWORD_STRUCT
:
3607 case KEYWORD_INTERFACE
:
3608 this->simple_stat(true, NULL
, NULL
, NULL
);
3612 this->go_or_defer_stat();
3614 case KEYWORD_RETURN
:
3615 this->return_stat();
3620 case KEYWORD_CONTINUE
:
3621 this->continue_stat();
3629 case KEYWORD_SWITCH
:
3630 this->switch_stat(label
);
3632 case KEYWORD_SELECT
:
3633 this->select_stat(label
);
3636 this->for_stat(label
);
3639 error_at(this->location(), "expected statement");
3640 this->advance_token();
3646 case Token::TOKEN_IDENTIFIER
:
3648 std::string identifier
= token
->identifier();
3649 bool is_exported
= token
->is_identifier_exported();
3650 Location location
= token
->location();
3651 if (this->advance_token()->is_op(OPERATOR_COLON
))
3653 this->advance_token();
3654 this->labeled_stmt(identifier
, location
);
3658 this->unget_token(Token::make_identifier_token(identifier
,
3661 this->simple_stat(true, NULL
, NULL
, NULL
);
3666 case Token::TOKEN_OPERATOR
:
3667 if (token
->is_op(OPERATOR_LCURLY
))
3669 Location location
= token
->location();
3670 this->gogo_
->start_block(location
);
3671 Location end_loc
= this->block();
3672 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3675 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3676 this->simple_stat(true, NULL
, NULL
, NULL
);
3679 case Token::TOKEN_STRING
:
3680 case Token::TOKEN_CHARACTER
:
3681 case Token::TOKEN_INTEGER
:
3682 case Token::TOKEN_FLOAT
:
3683 case Token::TOKEN_IMAGINARY
:
3684 this->simple_stat(true, NULL
, NULL
, NULL
);
3688 error_at(this->location(), "expected statement");
3689 this->advance_token();
3695 Parse::statement_may_start_here()
3697 const Token
* token
= this->peek_token();
3698 switch (token
->classification())
3700 case Token::TOKEN_KEYWORD
:
3702 switch (token
->keyword())
3709 case KEYWORD_STRUCT
:
3710 case KEYWORD_INTERFACE
:
3713 case KEYWORD_RETURN
:
3715 case KEYWORD_CONTINUE
:
3718 case KEYWORD_SWITCH
:
3719 case KEYWORD_SELECT
:
3729 case Token::TOKEN_IDENTIFIER
:
3732 case Token::TOKEN_OPERATOR
:
3733 if (token
->is_op(OPERATOR_LCURLY
)
3734 || token
->is_op(OPERATOR_SEMICOLON
))
3737 return this->expression_may_start_here();
3739 case Token::TOKEN_STRING
:
3740 case Token::TOKEN_CHARACTER
:
3741 case Token::TOKEN_INTEGER
:
3742 case Token::TOKEN_FLOAT
:
3743 case Token::TOKEN_IMAGINARY
:
3751 // LabeledStmt = Label ":" Statement .
3752 // Label = identifier .
3755 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3757 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3759 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3761 // This is a label at the end of a block. A program is
3762 // permitted to omit a semicolon here.
3766 if (!this->statement_may_start_here())
3768 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
3770 // We don't treat the fallthrough keyword as a statement,
3771 // because it can't appear most places where a statement is
3772 // permitted, but it may have a label. We introduce a
3773 // semicolon because the caller expects to see a statement.
3774 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3779 // Mark the label as used to avoid a useless error about an
3782 label
->set_is_used();
3784 error_at(location
, "missing statement after label");
3785 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3790 this->statement(label
);
3793 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3794 // Assignment | ShortVarDecl .
3796 // EmptyStmt was handled in Parse::statement.
3798 // In order to make this work for if and switch statements, if
3799 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3800 // expression rather than adding an expression statement to the
3801 // current block. If we see something other than an ExpressionStat,
3802 // we add the statement, set *RETURN_EXP to true if we saw a send
3803 // statement, and return NULL. The handling of send statements is for
3804 // better error messages.
3806 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3809 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3810 // guard (var := expr.("type") using the literal keyword "type").
3813 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3814 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3816 const Token
* token
= this->peek_token();
3818 // An identifier follow by := is a SimpleVarDecl.
3819 if (token
->is_identifier())
3821 std::string identifier
= token
->identifier();
3822 bool is_exported
= token
->is_identifier_exported();
3823 Location location
= token
->location();
3825 token
= this->advance_token();
3826 if (token
->is_op(OPERATOR_COLONEQ
)
3827 || token
->is_op(OPERATOR_COMMA
))
3829 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3830 this->simple_var_decl_or_assignment(identifier
, location
,
3831 may_be_composite_lit
,
3833 (token
->is_op(OPERATOR_COLONEQ
)
3839 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3842 else if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
3844 Typed_identifier_list til
;
3845 this->range_clause_decl(&til
, p_range_clause
);
3849 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3850 may_be_composite_lit
,
3851 (p_type_switch
== NULL
3853 : &p_type_switch
->found
),
3855 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3857 p_type_switch
->name
.clear();
3858 p_type_switch
->location
= exp
->location();
3859 p_type_switch
->expr
= this->verify_not_sink(exp
);
3862 token
= this->peek_token();
3863 if (token
->is_op(OPERATOR_CHANOP
))
3865 this->send_stmt(this->verify_not_sink(exp
), may_be_composite_lit
);
3866 if (return_exp
!= NULL
)
3869 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3870 || token
->is_op(OPERATOR_MINUSMINUS
))
3871 this->inc_dec_stat(this->verify_not_sink(exp
));
3872 else if (token
->is_op(OPERATOR_COMMA
)
3873 || token
->is_op(OPERATOR_EQ
))
3874 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
3875 else if (token
->is_op(OPERATOR_PLUSEQ
)
3876 || token
->is_op(OPERATOR_MINUSEQ
)
3877 || token
->is_op(OPERATOR_OREQ
)
3878 || token
->is_op(OPERATOR_XOREQ
)
3879 || token
->is_op(OPERATOR_MULTEQ
)
3880 || token
->is_op(OPERATOR_DIVEQ
)
3881 || token
->is_op(OPERATOR_MODEQ
)
3882 || token
->is_op(OPERATOR_LSHIFTEQ
)
3883 || token
->is_op(OPERATOR_RSHIFTEQ
)
3884 || token
->is_op(OPERATOR_ANDEQ
)
3885 || token
->is_op(OPERATOR_BITCLEAREQ
))
3886 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
3888 else if (return_exp
!= NULL
)
3889 return this->verify_not_sink(exp
);
3892 exp
= this->verify_not_sink(exp
);
3894 if (token
->is_op(OPERATOR_COLONEQ
))
3896 if (!exp
->is_error_expression())
3897 error_at(token
->location(), "non-name on left side of %<:=%>");
3898 this->gogo_
->mark_locals_used();
3899 while (!token
->is_op(OPERATOR_SEMICOLON
)
3900 && !token
->is_eof())
3901 token
= this->advance_token();
3905 this->expression_stat(exp
);
3912 Parse::simple_stat_may_start_here()
3914 return this->expression_may_start_here();
3917 // Parse { Statement ";" } which is used in a few places. The list of
3918 // statements may end with a right curly brace, in which case the
3919 // semicolon may be omitted.
3922 Parse::statement_list()
3924 while (this->statement_may_start_here())
3926 this->statement(NULL
);
3927 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
3928 this->advance_token();
3929 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3933 if (!this->peek_token()->is_eof() || !saw_errors())
3934 error_at(this->location(), "expected %<;%> or %<}%> or newline");
3935 if (!this->skip_past_error(OPERATOR_RCURLY
))
3942 Parse::statement_list_may_start_here()
3944 return this->statement_may_start_here();
3947 // ExpressionStat = Expression .
3950 Parse::expression_stat(Expression
* exp
)
3952 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
3955 // SendStmt = Channel "<-" Expression .
3956 // Channel = Expression .
3959 Parse::send_stmt(Expression
* channel
, bool may_be_composite_lit
)
3961 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
3962 Location loc
= this->location();
3963 this->advance_token();
3964 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false,
3965 may_be_composite_lit
, NULL
, NULL
);
3966 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
3967 this->gogo_
->add_statement(s
);
3970 // IncDecStat = Expression ( "++" | "--" ) .
3973 Parse::inc_dec_stat(Expression
* exp
)
3975 const Token
* token
= this->peek_token();
3977 // Lvalue maps require special handling.
3978 if (exp
->index_expression() != NULL
)
3979 exp
->index_expression()->set_is_lvalue();
3981 if (token
->is_op(OPERATOR_PLUSPLUS
))
3982 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
3983 else if (token
->is_op(OPERATOR_MINUSMINUS
))
3984 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
3987 this->advance_token();
3990 // Assignment = ExpressionList assign_op ExpressionList .
3992 // EXP is an expression that we have already parsed.
3994 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
3995 // side may be a composite literal.
3997 // If RANGE_CLAUSE is not NULL, then this will recognize a
4001 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
4002 Range_clause
* p_range_clause
)
4004 Expression_list
* vars
;
4005 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4007 vars
= new Expression_list();
4008 vars
->push_back(expr
);
4012 this->advance_token();
4013 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
4016 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
4019 // An assignment statement. LHS is the list of expressions which
4020 // appear on the left hand side.
4022 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4023 // side may be a composite literal.
4025 // If RANGE_CLAUSE is not NULL, then this will recognize a
4029 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
4030 Range_clause
* p_range_clause
)
4032 const Token
* token
= this->peek_token();
4033 if (!token
->is_op(OPERATOR_EQ
)
4034 && !token
->is_op(OPERATOR_PLUSEQ
)
4035 && !token
->is_op(OPERATOR_MINUSEQ
)
4036 && !token
->is_op(OPERATOR_OREQ
)
4037 && !token
->is_op(OPERATOR_XOREQ
)
4038 && !token
->is_op(OPERATOR_MULTEQ
)
4039 && !token
->is_op(OPERATOR_DIVEQ
)
4040 && !token
->is_op(OPERATOR_MODEQ
)
4041 && !token
->is_op(OPERATOR_LSHIFTEQ
)
4042 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4043 && !token
->is_op(OPERATOR_ANDEQ
)
4044 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4046 error_at(this->location(), "expected assignment operator");
4049 Operator op
= token
->op();
4050 Location location
= token
->location();
4052 token
= this->advance_token();
4057 // Map expressions act differently when they are lvalues.
4058 for (Expression_list::iterator plv
= lhs
->begin();
4061 if ((*plv
)->index_expression() != NULL
)
4062 (*plv
)->index_expression()->set_is_lvalue();
4064 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4066 if (op
!= OPERATOR_EQ
)
4067 error_at(this->location(), "range clause requires %<=%>");
4068 this->range_clause_expr(lhs
, p_range_clause
);
4072 Expression_list
* vals
= this->expression_list(NULL
, false,
4073 may_be_composite_lit
);
4075 // We've parsed everything; check for errors.
4078 for (Expression_list::const_iterator pe
= lhs
->begin();
4082 if ((*pe
)->is_error_expression())
4084 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4085 error_at((*pe
)->location(), "cannot use _ as value");
4087 for (Expression_list::const_iterator pe
= vals
->begin();
4091 if ((*pe
)->is_error_expression())
4095 Call_expression
* call
;
4096 Index_expression
* map_index
;
4097 Receive_expression
* receive
;
4098 Type_guard_expression
* type_guard
;
4099 if (lhs
->size() == vals
->size())
4102 if (lhs
->size() > 1)
4104 if (op
!= OPERATOR_EQ
)
4105 error_at(location
, "multiple values only permitted with %<=%>");
4106 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4110 if (op
== OPERATOR_EQ
)
4111 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4114 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4115 vals
->front(), location
);
4119 this->gogo_
->add_statement(s
);
4121 else if (vals
->size() == 1
4122 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4124 if (op
!= OPERATOR_EQ
)
4125 error_at(location
, "multiple results only permitted with %<=%>");
4126 call
->set_expected_result_count(lhs
->size());
4128 vals
= new Expression_list
;
4129 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4130 vals
->push_back(Expression::make_call_result(call
, i
));
4131 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4132 this->gogo_
->add_statement(s
);
4134 else if (lhs
->size() == 2
4135 && vals
->size() == 1
4136 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4138 if (op
!= OPERATOR_EQ
)
4139 error_at(location
, "two values from map requires %<=%>");
4140 Expression
* val
= lhs
->front();
4141 Expression
* present
= lhs
->back();
4142 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4143 map_index
, location
);
4144 this->gogo_
->add_statement(s
);
4146 else if (lhs
->size() == 1
4147 && vals
->size() == 2
4148 && (map_index
= lhs
->front()->index_expression()) != NULL
)
4150 if (op
!= OPERATOR_EQ
)
4151 error_at(location
, "assigning tuple to map index requires %<=%>");
4152 Expression
* val
= vals
->front();
4153 Expression
* should_set
= vals
->back();
4154 Statement
* s
= Statement::make_map_assignment(map_index
, val
, should_set
,
4156 this->gogo_
->add_statement(s
);
4158 else if (lhs
->size() == 2
4159 && vals
->size() == 1
4160 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4162 if (op
!= OPERATOR_EQ
)
4163 error_at(location
, "two values from receive requires %<=%>");
4164 Expression
* val
= lhs
->front();
4165 Expression
* success
= lhs
->back();
4166 Expression
* channel
= receive
->channel();
4167 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4170 this->gogo_
->add_statement(s
);
4172 else if (lhs
->size() == 2
4173 && vals
->size() == 1
4174 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4176 if (op
!= OPERATOR_EQ
)
4177 error_at(location
, "two values from type guard requires %<=%>");
4178 Expression
* val
= lhs
->front();
4179 Expression
* ok
= lhs
->back();
4180 Expression
* expr
= type_guard
->expr();
4181 Type
* type
= type_guard
->type();
4182 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4185 this->gogo_
->add_statement(s
);
4189 error_at(location
, "number of variables does not match number of values");
4193 // GoStat = "go" Expression .
4194 // DeferStat = "defer" Expression .
4197 Parse::go_or_defer_stat()
4199 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4200 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4201 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4202 Location stat_location
= this->location();
4204 this->advance_token();
4205 Location expr_location
= this->location();
4207 bool is_parenthesized
= false;
4208 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4210 Call_expression
* call_expr
= expr
->call_expression();
4211 if (is_parenthesized
|| call_expr
== NULL
)
4213 error_at(expr_location
, "argument to go/defer must be function call");
4217 // Make it easier to simplify go/defer statements by putting every
4218 // statement in its own block.
4219 this->gogo_
->start_block(stat_location
);
4222 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4224 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4225 this->gogo_
->add_statement(stat
);
4226 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4230 // ReturnStat = "return" [ ExpressionList ] .
4233 Parse::return_stat()
4235 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4236 Location location
= this->location();
4237 this->advance_token();
4238 Expression_list
* vals
= NULL
;
4239 if (this->expression_may_start_here())
4240 vals
= this->expression_list(NULL
, false, true);
4241 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4244 && this->gogo_
->current_function()->func_value()->results_are_named())
4246 Named_object
* function
= this->gogo_
->current_function();
4247 Function::Results
* results
= function
->func_value()->result_variables();
4248 for (Function::Results::const_iterator p
= results
->begin();
4249 p
!= results
->end();
4252 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4254 go_assert(saw_errors());
4255 else if (!no
->is_result_variable())
4256 error_at(location
, "%qs is shadowed during return",
4257 (*p
)->message_name().c_str());
4262 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4263 // [ "else" ( IfStmt | Block ) ] .
4268 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4269 Location location
= this->location();
4270 this->advance_token();
4272 this->gogo_
->start_block(location
);
4274 bool saw_simple_stat
= false;
4275 Expression
* cond
= NULL
;
4276 bool saw_send_stmt
= false;
4277 if (this->simple_stat_may_start_here())
4279 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4280 saw_simple_stat
= true;
4282 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4284 // The SimpleStat is an expression statement.
4285 this->expression_stat(cond
);
4290 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4291 this->advance_token();
4292 else if (saw_simple_stat
)
4295 error_at(this->location(),
4296 ("send statement used as value; "
4297 "use select for non-blocking send"));
4299 error_at(this->location(),
4300 "expected %<;%> after statement in if expression");
4301 if (!this->expression_may_start_here())
4302 cond
= Expression::make_error(this->location());
4304 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4306 error_at(this->location(),
4307 "missing condition in if statement");
4308 cond
= Expression::make_error(this->location());
4311 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4314 // Check for the easy error of a newline before starting the block.
4315 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4317 Location semi_loc
= this->location();
4318 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4319 error_at(semi_loc
, "missing %<{%> after if clause");
4320 // Otherwise we will get an error when we call this->block
4324 this->gogo_
->start_block(this->location());
4325 Location end_loc
= this->block();
4326 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4328 // Check for the easy error of a newline before "else".
4329 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4331 Location semi_loc
= this->location();
4332 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4333 error_at(this->location(),
4334 "unexpected semicolon or newline before %<else%>");
4336 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4340 Block
* else_block
= NULL
;
4341 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4343 this->gogo_
->start_block(this->location());
4344 const Token
* token
= this->advance_token();
4345 if (token
->is_keyword(KEYWORD_IF
))
4347 else if (token
->is_op(OPERATOR_LCURLY
))
4351 error_at(this->location(), "expected %<if%> or %<{%>");
4352 this->statement(NULL
);
4354 else_block
= this->gogo_
->finish_block(this->location());
4357 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4361 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4365 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4366 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4367 // "{" { ExprCaseClause } "}" .
4368 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4369 // "{" { TypeCaseClause } "}" .
4370 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4373 Parse::switch_stat(Label
* label
)
4375 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4376 Location location
= this->location();
4377 this->advance_token();
4379 this->gogo_
->start_block(location
);
4381 bool saw_simple_stat
= false;
4382 Expression
* switch_val
= NULL
;
4384 Type_switch type_switch
;
4385 bool have_type_switch_block
= false;
4386 if (this->simple_stat_may_start_here())
4388 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4390 saw_simple_stat
= true;
4392 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4394 // The SimpleStat is an expression statement.
4395 this->expression_stat(switch_val
);
4398 if (switch_val
== NULL
&& !type_switch
.found
)
4400 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4401 this->advance_token();
4402 else if (saw_simple_stat
)
4405 error_at(this->location(),
4406 ("send statement used as value; "
4407 "use select for non-blocking send"));
4409 error_at(this->location(),
4410 "expected %<;%> after statement in switch expression");
4412 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4414 if (this->peek_token()->is_identifier())
4416 const Token
* token
= this->peek_token();
4417 std::string identifier
= token
->identifier();
4418 bool is_exported
= token
->is_identifier_exported();
4419 Location id_loc
= token
->location();
4421 token
= this->advance_token();
4422 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4423 this->unget_token(Token::make_identifier_token(identifier
,
4428 // This must be a TypeSwitchGuard. It is in a
4429 // different block from any initial SimpleStat.
4430 if (saw_simple_stat
)
4432 this->gogo_
->start_block(id_loc
);
4433 have_type_switch_block
= true;
4436 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4438 if (!type_switch
.found
)
4440 if (switch_val
== NULL
4441 || !switch_val
->is_error_expression())
4443 error_at(id_loc
, "expected type switch assignment");
4444 switch_val
= Expression::make_error(id_loc
);
4449 if (switch_val
== NULL
&& !type_switch
.found
)
4451 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4452 &type_switch
.found
, NULL
);
4453 if (type_switch
.found
)
4455 type_switch
.name
.clear();
4456 type_switch
.expr
= switch_val
;
4457 type_switch
.location
= switch_val
->location();
4463 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4465 Location token_loc
= this->location();
4466 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4467 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4468 error_at(token_loc
, "missing %<{%> after switch clause");
4469 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4471 error_at(token_loc
, "invalid variable name");
4472 this->advance_token();
4473 this->expression(PRECEDENCE_NORMAL
, false, false,
4474 &type_switch
.found
, NULL
);
4475 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4476 this->advance_token();
4477 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4479 if (have_type_switch_block
)
4480 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4482 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4486 if (type_switch
.found
)
4487 type_switch
.expr
= Expression::make_error(location
);
4491 error_at(this->location(), "expected %<{%>");
4492 if (have_type_switch_block
)
4493 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4495 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4500 this->advance_token();
4502 Statement
* statement
;
4503 if (type_switch
.found
)
4504 statement
= this->type_switch_body(label
, type_switch
, location
);
4506 statement
= this->expr_switch_body(label
, switch_val
, location
);
4508 if (statement
!= NULL
)
4509 this->gogo_
->add_statement(statement
);
4511 if (have_type_switch_block
)
4512 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4515 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4519 // The body of an expression switch.
4520 // "{" { ExprCaseClause } "}"
4523 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4526 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4529 this->push_break_statement(statement
, label
);
4531 Case_clauses
* case_clauses
= new Case_clauses();
4532 bool saw_default
= false;
4533 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4535 if (this->peek_token()->is_eof())
4538 error_at(this->location(), "missing %<}%>");
4541 this->expr_case_clause(case_clauses
, &saw_default
);
4543 this->advance_token();
4545 statement
->add_clauses(case_clauses
);
4547 this->pop_break_statement();
4552 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4553 // FallthroughStat = "fallthrough" .
4556 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4558 Location location
= this->location();
4560 bool is_default
= false;
4561 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4563 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4566 error_at(this->location(), "expected %<:%>");
4570 this->advance_token();
4572 Block
* statements
= NULL
;
4573 if (this->statement_list_may_start_here())
4575 this->gogo_
->start_block(this->location());
4576 this->statement_list();
4577 statements
= this->gogo_
->finish_block(this->location());
4580 bool is_fallthrough
= false;
4581 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4583 Location fallthrough_loc
= this->location();
4584 is_fallthrough
= true;
4585 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4586 this->advance_token();
4587 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4588 error_at(fallthrough_loc
, _("cannot fallthrough final case in switch"));
4595 error_at(location
, "multiple defaults in switch");
4598 *saw_default
= true;
4601 if (is_default
|| vals
!= NULL
)
4602 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4605 // ExprSwitchCase = "case" ExpressionList | "default" .
4608 Parse::expr_switch_case(bool* is_default
)
4610 const Token
* token
= this->peek_token();
4611 if (token
->is_keyword(KEYWORD_CASE
))
4613 this->advance_token();
4614 return this->expression_list(NULL
, false, true);
4616 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4618 this->advance_token();
4625 error_at(this->location(), "expected %<case%> or %<default%>");
4626 if (!token
->is_op(OPERATOR_RCURLY
))
4627 this->advance_token();
4632 // The body of a type switch.
4633 // "{" { TypeCaseClause } "}" .
4636 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4639 Expression
* init
= type_switch
.expr
;
4640 std::string var_name
= type_switch
.name
;
4641 if (!var_name
.empty())
4643 if (Gogo::is_sink_name(var_name
))
4645 error_at(type_switch
.location
,
4646 "no new variables on left side of %<:=%>");
4651 Location loc
= type_switch
.location
;
4652 Temporary_statement
* switch_temp
=
4653 Statement::make_temporary(NULL
, init
, loc
);
4654 this->gogo_
->add_statement(switch_temp
);
4655 init
= Expression::make_temporary_reference(switch_temp
, loc
);
4659 Type_switch_statement
* statement
=
4660 Statement::make_type_switch_statement(var_name
, init
, location
);
4661 this->push_break_statement(statement
, label
);
4663 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4664 bool saw_default
= false;
4665 std::vector
<Named_object
*> implicit_vars
;
4666 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4668 if (this->peek_token()->is_eof())
4670 error_at(this->location(), "missing %<}%>");
4673 this->type_case_clause(var_name
, init
, case_clauses
, &saw_default
,
4676 this->advance_token();
4678 statement
->add_clauses(case_clauses
);
4680 this->pop_break_statement();
4682 // If there is a type switch variable implicitly declared in each case clause,
4683 // check that it is used in at least one of the cases.
4684 if (!var_name
.empty())
4687 for (std::vector
<Named_object
*>::iterator p
= implicit_vars
.begin();
4688 p
!= implicit_vars
.end();
4691 if ((*p
)->var_value()->is_used())
4698 error_at(type_switch
.location
, "%qs declared and not used",
4699 Gogo::message_name(var_name
).c_str());
4704 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4705 // IMPLICIT_VARS is the list of variables implicitly declared for each type
4706 // case if there is a type switch variable declared.
4709 Parse::type_case_clause(const std::string
& var_name
, Expression
* init
,
4710 Type_case_clauses
* clauses
, bool* saw_default
,
4711 std::vector
<Named_object
*>* implicit_vars
)
4713 Location location
= this->location();
4715 std::vector
<Type
*> types
;
4716 bool is_default
= false;
4717 this->type_switch_case(&types
, &is_default
);
4719 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4720 error_at(this->location(), "expected %<:%>");
4722 this->advance_token();
4724 Block
* statements
= NULL
;
4725 if (this->statement_list_may_start_here())
4727 this->gogo_
->start_block(this->location());
4728 if (!var_name
.empty())
4731 Location var_loc
= init
->location();
4732 if (types
.size() == 1)
4734 type
= types
.front();
4735 init
= Expression::make_type_guard(init
, type
, location
);
4738 Variable
* v
= new Variable(type
, init
, false, false, false,
4741 v
->set_is_type_switch_var();
4742 implicit_vars
->push_back(this->gogo_
->add_variable(var_name
, v
));
4744 this->statement_list();
4745 statements
= this->gogo_
->finish_block(this->location());
4748 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4750 error_at(this->location(),
4751 "fallthrough is not permitted in a type switch");
4752 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4753 this->advance_token();
4758 go_assert(types
.empty());
4761 error_at(location
, "multiple defaults in type switch");
4764 *saw_default
= true;
4765 clauses
->add(NULL
, false, true, statements
, location
);
4767 else if (!types
.empty())
4769 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4770 p
+ 1 != types
.end();
4772 clauses
->add(*p
, true, false, NULL
, location
);
4773 clauses
->add(types
.back(), false, false, statements
, location
);
4776 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4779 // TypeSwitchCase = "case" type | "default"
4781 // We accept a comma separated list of types.
4784 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4786 const Token
* token
= this->peek_token();
4787 if (token
->is_keyword(KEYWORD_CASE
))
4789 this->advance_token();
4792 Type
* t
= this->type();
4794 if (!t
->is_error_type())
4795 types
->push_back(t
);
4798 this->gogo_
->mark_locals_used();
4799 token
= this->peek_token();
4800 while (!token
->is_op(OPERATOR_COLON
)
4801 && !token
->is_op(OPERATOR_COMMA
)
4802 && !token
->is_op(OPERATOR_RCURLY
)
4803 && !token
->is_eof())
4804 token
= this->advance_token();
4807 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4809 this->advance_token();
4812 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4814 this->advance_token();
4819 error_at(this->location(), "expected %<case%> or %<default%>");
4820 if (!token
->is_op(OPERATOR_RCURLY
))
4821 this->advance_token();
4825 // SelectStat = "select" "{" { CommClause } "}" .
4828 Parse::select_stat(Label
* label
)
4830 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4831 Location location
= this->location();
4832 const Token
* token
= this->advance_token();
4834 if (!token
->is_op(OPERATOR_LCURLY
))
4836 Location token_loc
= token
->location();
4837 if (token
->is_op(OPERATOR_SEMICOLON
)
4838 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4839 error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4842 error_at(this->location(), "expected %<{%>");
4846 this->advance_token();
4848 Select_statement
* statement
= Statement::make_select_statement(location
);
4850 this->push_break_statement(statement
, label
);
4852 Select_clauses
* select_clauses
= new Select_clauses();
4853 bool saw_default
= false;
4854 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4856 if (this->peek_token()->is_eof())
4858 error_at(this->location(), "expected %<}%>");
4861 this->comm_clause(select_clauses
, &saw_default
);
4864 this->advance_token();
4866 statement
->add_clauses(select_clauses
);
4868 this->pop_break_statement();
4870 this->gogo_
->add_statement(statement
);
4873 // CommClause = CommCase ":" { Statement ";" } .
4876 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
4878 Location location
= this->location();
4879 bool is_send
= false;
4880 Expression
* channel
= NULL
;
4881 Expression
* val
= NULL
;
4882 Expression
* closed
= NULL
;
4883 std::string varname
;
4884 std::string closedname
;
4885 bool is_default
= false;
4886 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
4887 &varname
, &closedname
, &is_default
);
4891 && closedname
.empty()
4893 && val
->index_expression() != NULL
)
4894 val
->index_expression()->set_is_lvalue();
4896 if (this->peek_token()->is_op(OPERATOR_COLON
))
4897 this->advance_token();
4899 error_at(this->location(), "expected colon");
4901 this->gogo_
->start_block(this->location());
4903 Named_object
* var
= NULL
;
4904 if (!varname
.empty())
4906 // FIXME: LOCATION is slightly wrong here.
4907 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
4909 v
->set_type_from_chan_element();
4910 var
= this->gogo_
->add_variable(varname
, v
);
4913 Named_object
* closedvar
= NULL
;
4914 if (!closedname
.empty())
4916 // FIXME: LOCATION is slightly wrong here.
4917 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
4918 false, false, false, location
);
4919 closedvar
= this->gogo_
->add_variable(closedname
, v
);
4922 this->statement_list();
4924 Block
* statements
= this->gogo_
->finish_block(this->location());
4930 error_at(location
, "multiple defaults in select");
4933 *saw_default
= true;
4937 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
4938 statements
, location
);
4939 else if (statements
!= NULL
)
4941 // Add the statements to make sure that any names they define
4943 this->gogo_
->add_block(statements
, location
);
4947 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4950 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
4951 Expression
** closed
, std::string
* varname
,
4952 std::string
* closedname
, bool* is_default
)
4954 const Token
* token
= this->peek_token();
4955 if (token
->is_keyword(KEYWORD_DEFAULT
))
4957 this->advance_token();
4960 else if (token
->is_keyword(KEYWORD_CASE
))
4962 this->advance_token();
4963 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
4969 error_at(this->location(), "expected %<case%> or %<default%>");
4970 if (!token
->is_op(OPERATOR_RCURLY
))
4971 this->advance_token();
4978 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
4979 // RecvExpr = Expression .
4982 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
4983 Expression
** closed
, std::string
* varname
,
4984 std::string
* closedname
)
4986 const Token
* token
= this->peek_token();
4987 bool saw_comma
= false;
4988 bool closed_is_id
= false;
4989 if (token
->is_identifier())
4991 Gogo
* gogo
= this->gogo_
;
4992 std::string recv_var
= token
->identifier();
4993 bool is_rv_exported
= token
->is_identifier_exported();
4994 Location recv_var_loc
= token
->location();
4995 token
= this->advance_token();
4996 if (token
->is_op(OPERATOR_COLONEQ
))
4999 this->advance_token();
5000 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
5002 Receive_expression
* re
= e
->receive_expression();
5005 if (!e
->is_error_expression())
5006 error_at(this->location(), "expected receive expression");
5009 if (recv_var
== "_")
5011 error_at(recv_var_loc
,
5012 "no new variables on left side of %<:=%>");
5013 recv_var
= Gogo::erroneous_name();
5016 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
5017 *channel
= re
->channel();
5020 else if (token
->is_op(OPERATOR_COMMA
))
5022 token
= this->advance_token();
5023 if (token
->is_identifier())
5025 std::string recv_closed
= token
->identifier();
5026 bool is_rc_exported
= token
->is_identifier_exported();
5027 Location recv_closed_loc
= token
->location();
5028 closed_is_id
= true;
5030 token
= this->advance_token();
5031 if (token
->is_op(OPERATOR_COLONEQ
))
5033 // case rv, rc := <-c:
5034 this->advance_token();
5035 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
5037 Receive_expression
* re
= e
->receive_expression();
5040 if (!e
->is_error_expression())
5041 error_at(this->location(),
5042 "expected receive expression");
5045 if (recv_var
== "_" && recv_closed
== "_")
5047 error_at(recv_var_loc
,
5048 "no new variables on left side of %<:=%>");
5049 recv_var
= Gogo::erroneous_name();
5052 if (recv_var
!= "_")
5053 *varname
= gogo
->pack_hidden_name(recv_var
,
5055 if (recv_closed
!= "_")
5056 *closedname
= gogo
->pack_hidden_name(recv_closed
,
5058 *channel
= re
->channel();
5062 this->unget_token(Token::make_identifier_token(recv_closed
,
5067 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5069 recv_var_loc
, true);
5073 this->unget_token(Token::make_identifier_token(recv_var
,
5078 // If SAW_COMMA is false, then we are looking at the start of the
5079 // send or receive expression. If SAW_COMMA is true, then *VAL is
5080 // set and we just read a comma.
5083 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5084 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5089 this->advance_token();
5090 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5092 // The next token should be ':'. If it is '<-', then we have
5094 // which is to say, send on a channel received from a channel.
5095 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5098 e
= Expression::make_receive(*channel
, (*channel
)->location());
5101 if (!saw_comma
&& this->peek_token()->is_op(OPERATOR_COMMA
))
5103 this->advance_token();
5105 if (!e
->is_sink_expression())
5107 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5111 if (this->peek_token()->is_op(OPERATOR_EQ
))
5113 if (!this->advance_token()->is_op(OPERATOR_CHANOP
))
5115 error_at(this->location(), "missing %<<-%>");
5119 this->advance_token();
5120 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5124 // *VAL is already set.
5125 if (!e
->is_sink_expression())
5131 if (!e
->is_sink_expression())
5140 error_at(this->location(), "expected %<=%> or %<:=%>");
5142 error_at(this->location(), "expected %<=%>");
5146 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5150 *channel
= this->verify_not_sink(e
);
5151 this->advance_token();
5152 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5156 error_at(this->location(), "expected %<<-%> or %<=%>");
5160 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5161 // Condition = Expression .
5164 Parse::for_stat(Label
* label
)
5166 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5167 Location location
= this->location();
5168 const Token
* token
= this->advance_token();
5170 // Open a block to hold any variables defined in the init statement
5171 // of the for statement.
5172 this->gogo_
->start_block(location
);
5175 Expression
* cond
= NULL
;
5177 Range_clause range_clause
;
5179 if (!token
->is_op(OPERATOR_LCURLY
))
5181 if (token
->is_keyword(KEYWORD_VAR
))
5183 error_at(this->location(),
5184 "var declaration not allowed in for initializer");
5188 if (token
->is_op(OPERATOR_SEMICOLON
))
5189 this->for_clause(&cond
, &post
);
5192 // We might be looking at a Condition, an InitStat, or a
5195 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5196 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5198 if (cond
== NULL
&& !range_clause
.found
)
5201 error_at(this->location(),
5202 ("send statement used as value; "
5203 "use select for non-blocking send"));
5205 error_at(this->location(), "parse error in for statement");
5210 if (range_clause
.found
)
5211 error_at(this->location(), "parse error after range clause");
5215 // COND is actually an expression statement for
5216 // InitStat at the start of a ForClause.
5217 this->expression_stat(cond
);
5221 this->for_clause(&cond
, &post
);
5226 // Check for the easy error of a newline before starting the block.
5227 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5229 Location semi_loc
= this->location();
5230 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5231 error_at(semi_loc
, "missing %<{%> after for clause");
5232 // Otherwise we will get an error when we call this->block
5236 // Build the For_statement and note that it is the current target
5237 // for break and continue statements.
5239 For_statement
* sfor
;
5240 For_range_statement
* srange
;
5242 if (!range_clause
.found
)
5244 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5250 srange
= Statement::make_for_range_statement(range_clause
.index
,
5258 this->push_break_statement(s
, label
);
5259 this->push_continue_statement(s
, label
);
5261 // Gather the block of statements in the loop and add them to the
5264 this->gogo_
->start_block(this->location());
5265 Location end_loc
= this->block();
5266 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5269 sfor
->add_statements(statements
);
5271 srange
->add_statements(statements
);
5273 // This is no longer the break/continue target.
5274 this->pop_break_statement();
5275 this->pop_continue_statement();
5277 // Add the For_statement to the list of statements, and close out
5278 // the block we started to hold any variables defined in the for
5281 this->gogo_
->add_statement(s
);
5283 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5287 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5288 // InitStat = SimpleStat .
5289 // PostStat = SimpleStat .
5291 // We have already read InitStat at this point.
5294 Parse::for_clause(Expression
** cond
, Block
** post
)
5296 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5297 this->advance_token();
5298 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5300 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5302 error_at(this->location(), "missing %<{%> after for clause");
5308 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5309 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5310 error_at(this->location(), "expected semicolon");
5312 this->advance_token();
5314 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5318 this->gogo_
->start_block(this->location());
5319 this->simple_stat(false, NULL
, NULL
, NULL
);
5320 *post
= this->gogo_
->finish_block(this->location());
5324 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5326 // This is the := version. It is called with a list of identifiers.
5329 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5330 Range_clause
* p_range_clause
)
5332 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5333 Location location
= this->location();
5335 p_range_clause
->found
= true;
5337 if (til
->size() > 2)
5338 error_at(this->location(), "too many variables for range clause");
5340 this->advance_token();
5341 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5343 p_range_clause
->range
= expr
;
5348 bool any_new
= false;
5350 const Typed_identifier
* pti
= &til
->front();
5351 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5353 if (any_new
&& no
->is_variable())
5354 no
->var_value()->set_type_from_range_index();
5355 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5357 if (til
->size() == 1)
5358 p_range_clause
->value
= NULL
;
5362 bool is_new
= false;
5363 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5364 if (is_new
&& no
->is_variable())
5365 no
->var_value()->set_type_from_range_value();
5368 if (!Gogo::is_sink_name(pti
->name()))
5369 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5373 error_at(location
, "variables redeclared but no variable is new");
5376 // The = version of RangeClause. This is called with a list of
5380 Parse::range_clause_expr(const Expression_list
* vals
,
5381 Range_clause
* p_range_clause
)
5383 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5385 p_range_clause
->found
= true;
5387 go_assert(vals
->size() >= 1);
5388 if (vals
->size() > 2)
5389 error_at(this->location(), "too many variables for range clause");
5391 this->advance_token();
5392 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5398 p_range_clause
->index
= vals
->front();
5399 if (vals
->size() == 1)
5400 p_range_clause
->value
= NULL
;
5402 p_range_clause
->value
= vals
->back();
5405 // Push a statement on the break stack.
5408 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5410 if (this->break_stack_
== NULL
)
5411 this->break_stack_
= new Bc_stack();
5412 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5415 // Push a statement on the continue stack.
5418 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5420 if (this->continue_stack_
== NULL
)
5421 this->continue_stack_
= new Bc_stack();
5422 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5425 // Pop the break stack.
5428 Parse::pop_break_statement()
5430 this->break_stack_
->pop_back();
5433 // Pop the continue stack.
5436 Parse::pop_continue_statement()
5438 this->continue_stack_
->pop_back();
5441 // Find a break or continue statement given a label name.
5444 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5446 if (bc_stack
== NULL
)
5448 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5449 p
!= bc_stack
->rend();
5452 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5454 p
->second
->set_is_used();
5461 // BreakStat = "break" [ identifier ] .
5466 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5467 Location location
= this->location();
5469 const Token
* token
= this->advance_token();
5470 Statement
* enclosing
;
5471 if (!token
->is_identifier())
5473 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5475 error_at(this->location(),
5476 "break statement not within for or switch or select");
5479 enclosing
= this->break_stack_
->back().first
;
5483 enclosing
= this->find_bc_statement(this->break_stack_
,
5484 token
->identifier());
5485 if (enclosing
== NULL
)
5487 // If there is a label with this name, mark it as used to
5488 // avoid a useless error about an unused label.
5489 this->gogo_
->add_label_reference(token
->identifier(),
5490 Linemap::unknown_location(), false);
5492 error_at(token
->location(), "invalid break label %qs",
5493 Gogo::message_name(token
->identifier()).c_str());
5494 this->advance_token();
5497 this->advance_token();
5500 Unnamed_label
* label
;
5501 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5502 label
= enclosing
->for_statement()->break_label();
5503 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5504 label
= enclosing
->for_range_statement()->break_label();
5505 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5506 label
= enclosing
->switch_statement()->break_label();
5507 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5508 label
= enclosing
->type_switch_statement()->break_label();
5509 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5510 label
= enclosing
->select_statement()->break_label();
5514 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5518 // ContinueStat = "continue" [ identifier ] .
5521 Parse::continue_stat()
5523 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5524 Location location
= this->location();
5526 const Token
* token
= this->advance_token();
5527 Statement
* enclosing
;
5528 if (!token
->is_identifier())
5530 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5532 error_at(this->location(), "continue statement not within for");
5535 enclosing
= this->continue_stack_
->back().first
;
5539 enclosing
= this->find_bc_statement(this->continue_stack_
,
5540 token
->identifier());
5541 if (enclosing
== NULL
)
5543 // If there is a label with this name, mark it as used to
5544 // avoid a useless error about an unused label.
5545 this->gogo_
->add_label_reference(token
->identifier(),
5546 Linemap::unknown_location(), false);
5548 error_at(token
->location(), "invalid continue label %qs",
5549 Gogo::message_name(token
->identifier()).c_str());
5550 this->advance_token();
5553 this->advance_token();
5556 Unnamed_label
* label
;
5557 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5558 label
= enclosing
->for_statement()->continue_label();
5559 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5560 label
= enclosing
->for_range_statement()->continue_label();
5564 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5568 // GotoStat = "goto" identifier .
5573 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5574 Location location
= this->location();
5575 const Token
* token
= this->advance_token();
5576 if (!token
->is_identifier())
5577 error_at(this->location(), "expected label for goto");
5580 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5582 Statement
* s
= Statement::make_goto_statement(label
, location
);
5583 this->gogo_
->add_statement(s
);
5584 this->advance_token();
5588 // PackageClause = "package" PackageName .
5591 Parse::package_clause()
5593 const Token
* token
= this->peek_token();
5594 Location location
= token
->location();
5596 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5598 error_at(this->location(), "program must start with package clause");
5603 token
= this->advance_token();
5604 if (token
->is_identifier())
5606 name
= token
->identifier();
5609 error_at(this->location(), "invalid package name _");
5610 name
= Gogo::erroneous_name();
5612 this->advance_token();
5616 error_at(this->location(), "package name must be an identifier");
5620 this->gogo_
->set_package_name(name
, location
);
5623 // ImportDecl = "import" Decl<ImportSpec> .
5626 Parse::import_decl()
5628 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5629 this->advance_token();
5630 this->decl(&Parse::import_spec
, NULL
);
5633 // ImportSpec = [ "." | PackageName ] PackageFileName .
5636 Parse::import_spec(void*)
5638 const Token
* token
= this->peek_token();
5639 Location location
= token
->location();
5641 std::string local_name
;
5642 bool is_local_name_exported
= false;
5643 if (token
->is_op(OPERATOR_DOT
))
5646 token
= this->advance_token();
5648 else if (token
->is_identifier())
5650 local_name
= token
->identifier();
5651 is_local_name_exported
= token
->is_identifier_exported();
5652 token
= this->advance_token();
5655 if (!token
->is_string())
5657 error_at(this->location(), "import statement not a string");
5658 this->advance_token();
5662 this->gogo_
->import_package(token
->string_value(), local_name
,
5663 is_local_name_exported
, location
);
5665 this->advance_token();
5668 // SourceFile = PackageClause ";" { ImportDecl ";" }
5669 // { TopLevelDecl ";" } .
5674 this->package_clause();
5676 const Token
* token
= this->peek_token();
5677 if (token
->is_op(OPERATOR_SEMICOLON
))
5678 token
= this->advance_token();
5680 error_at(this->location(),
5681 "expected %<;%> or newline after package clause");
5683 while (token
->is_keyword(KEYWORD_IMPORT
))
5685 this->import_decl();
5686 token
= this->peek_token();
5687 if (token
->is_op(OPERATOR_SEMICOLON
))
5688 token
= this->advance_token();
5690 error_at(this->location(),
5691 "expected %<;%> or newline after import declaration");
5694 while (!token
->is_eof())
5696 if (this->declaration_may_start_here())
5697 this->declaration();
5700 error_at(this->location(), "expected declaration");
5701 this->gogo_
->mark_locals_used();
5703 this->advance_token();
5704 while (!this->peek_token()->is_eof()
5705 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5706 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5707 if (!this->peek_token()->is_eof()
5708 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5709 this->advance_token();
5711 token
= this->peek_token();
5712 if (token
->is_op(OPERATOR_SEMICOLON
))
5713 token
= this->advance_token();
5714 else if (!token
->is_eof() || !saw_errors())
5716 if (token
->is_op(OPERATOR_CHANOP
))
5717 error_at(this->location(),
5718 ("send statement used as value; "
5719 "use select for non-blocking send"));
5721 error_at(this->location(),
5722 "expected %<;%> or newline after top level declaration");
5723 this->skip_past_error(OPERATOR_INVALID
);
5728 // Reset the current iota value.
5736 // Return the current iota value.
5744 // Increment the current iota value.
5747 Parse::increment_iota()
5752 // Skip forward to a semicolon or OP. OP will normally be
5753 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5754 // past it and return. If we find OP, it will be the next token to
5755 // read. Return true if we are OK, false if we found EOF.
5758 Parse::skip_past_error(Operator op
)
5760 this->gogo_
->mark_locals_used();
5761 const Token
* token
= this->peek_token();
5762 while (!token
->is_op(op
))
5764 if (token
->is_eof())
5766 if (token
->is_op(OPERATOR_SEMICOLON
))
5768 this->advance_token();
5771 token
= this->advance_token();
5776 // Check that an expression is not a sink.
5779 Parse::verify_not_sink(Expression
* expr
)
5781 if (expr
->is_sink_expression())
5783 error_at(expr
->location(), "cannot use _ as value");
5784 expr
= Expression::make_error(expr
->location());
5787 // If this can not be a sink, and it is a variable, then we are
5788 // using the variable, not just assigning to it.
5789 Var_expression
* ve
= expr
->var_expression();
5791 this->mark_var_used(ve
->named_object());
5792 else if (expr
->deref()->field_reference_expression() != NULL
5793 && this->gogo_
->current_function() != NULL
)
5795 // We could be looking at a variable referenced from a closure.
5796 // If so, we need to get the enclosed variable and mark it as used.
5797 Function
* this_function
= this->gogo_
->current_function()->func_value();
5798 Named_object
* closure
= this_function
->closure_var();
5799 if (closure
!= NULL
)
5801 unsigned int var_index
=
5802 expr
->deref()->field_reference_expression()->field_index();
5803 this->mark_var_used(this_function
->enclosing_var(var_index
- 1));
5810 // Mark a variable as used.
5813 Parse::mark_var_used(Named_object
* no
)
5815 if (no
->is_variable())
5816 no
->var_value()->set_is_used();