1 // parse.cc -- Go frontend parser.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #include "go-diagnostics.h"
13 #include "statements.h"
14 #include "expressions.h"
17 // Struct Parse::Enclosing_var_comparison.
19 // Return true if v1 should be considered to be less than v2.
22 Parse::Enclosing_var_comparison::operator()(const Enclosing_var
& v1
,
23 const Enclosing_var
& v2
) const
25 if (v1
.var() == v2
.var())
28 const std::string
& n1(v1
.var()->name());
29 const std::string
& n2(v2
.var()->name());
30 int i
= n1
.compare(n2
);
36 // If we get here it means that a single nested function refers to
37 // two different variables defined in enclosing functions, and both
38 // variables have the same name. I think this is impossible.
44 Parse::Parse(Lex
* lex
, Gogo
* gogo
)
46 token_(Token::make_invalid_token(Linemap::unknown_location())),
47 unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
48 unget_token_valid_(false),
49 is_erroneous_function_(false),
52 continue_stack_(NULL
),
57 // Return the current token.
62 if (this->unget_token_valid_
)
63 return &this->unget_token_
;
64 if (this->token_
.is_invalid())
65 this->token_
= this->lex_
->next_token();
69 // Advance to the next token and return it.
72 Parse::advance_token()
74 if (this->unget_token_valid_
)
76 this->unget_token_valid_
= false;
77 if (!this->token_
.is_invalid())
80 this->token_
= this->lex_
->next_token();
84 // Push a token back on the input stream.
87 Parse::unget_token(const Token
& token
)
89 go_assert(!this->unget_token_valid_
);
90 this->unget_token_
= token
;
91 this->unget_token_valid_
= true;
94 // The location of the current token.
99 return this->peek_token()->location();
102 // IdentifierList = identifier { "," identifier } .
105 Parse::identifier_list(Typed_identifier_list
* til
)
107 const Token
* token
= this->peek_token();
110 if (!token
->is_identifier())
112 go_error_at(this->location(), "expected identifier");
116 this->gogo_
->pack_hidden_name(token
->identifier(),
117 token
->is_identifier_exported());
118 til
->push_back(Typed_identifier(name
, NULL
, token
->location()));
119 token
= this->advance_token();
120 if (!token
->is_op(OPERATOR_COMMA
))
122 token
= this->advance_token();
126 // ExpressionList = Expression { "," Expression } .
128 // If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
131 // If MAY_BE_SINK is true, the expressions in the list may be "_".
134 Parse::expression_list(Expression
* first
, bool may_be_sink
,
135 bool may_be_composite_lit
)
137 Expression_list
* ret
= new Expression_list();
139 ret
->push_back(first
);
142 ret
->push_back(this->expression(PRECEDENCE_NORMAL
, may_be_sink
,
143 may_be_composite_lit
, NULL
, NULL
));
145 const Token
* token
= this->peek_token();
146 if (!token
->is_op(OPERATOR_COMMA
))
149 // Most expression lists permit a trailing comma.
150 Location location
= token
->location();
151 this->advance_token();
152 if (!this->expression_may_start_here())
154 this->unget_token(Token::make_operator_token(OPERATOR_COMMA
,
161 // QualifiedIdent = [ PackageName "." ] identifier .
162 // PackageName = identifier .
164 // This sets *PNAME to the identifier and sets *PPACKAGE to the
165 // package or NULL if there isn't one. This returns true on success,
166 // false on failure in which case it will have emitted an error
170 Parse::qualified_ident(std::string
* pname
, Named_object
** ppackage
)
172 const Token
* token
= this->peek_token();
173 if (!token
->is_identifier())
175 go_error_at(this->location(), "expected identifier");
179 std::string name
= token
->identifier();
180 bool is_exported
= token
->is_identifier_exported();
181 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
183 token
= this->advance_token();
184 if (!token
->is_op(OPERATOR_DOT
))
191 Named_object
* package
= this->gogo_
->lookup(name
, NULL
);
192 if (package
== NULL
|| !package
->is_package())
194 go_error_at(this->location(), "expected package");
195 // We expect . IDENTIFIER; skip both.
196 if (this->advance_token()->is_identifier())
197 this->advance_token();
201 package
->package_value()->note_usage(Gogo::unpack_hidden_name(name
));
203 token
= this->advance_token();
204 if (!token
->is_identifier())
206 go_error_at(this->location(), "expected identifier");
210 name
= token
->identifier();
214 go_error_at(this->location(), "invalid use of %<_%>");
215 name
= Gogo::erroneous_name();
218 if (package
->name() == this->gogo_
->package_name())
219 name
= this->gogo_
->pack_hidden_name(name
,
220 token
->is_identifier_exported());
225 this->advance_token();
230 // Type = TypeName | TypeLit | "(" Type ")" .
232 // ArrayType | StructType | PointerType | FunctionType | InterfaceType |
233 // SliceType | MapType | ChannelType .
238 const Token
* token
= this->peek_token();
239 if (token
->is_identifier())
240 return this->type_name(true);
241 else if (token
->is_op(OPERATOR_LSQUARE
))
242 return this->array_type(false);
243 else if (token
->is_keyword(KEYWORD_CHAN
)
244 || token
->is_op(OPERATOR_CHANOP
))
245 return this->channel_type();
246 else if (token
->is_keyword(KEYWORD_INTERFACE
))
247 return this->interface_type(true);
248 else if (token
->is_keyword(KEYWORD_FUNC
))
250 Location location
= token
->location();
251 this->advance_token();
252 Type
* type
= this->signature(NULL
, location
);
254 return Type::make_error_type();
257 else if (token
->is_keyword(KEYWORD_MAP
))
258 return this->map_type();
259 else if (token
->is_keyword(KEYWORD_STRUCT
))
260 return this->struct_type();
261 else if (token
->is_op(OPERATOR_MULT
))
262 return this->pointer_type();
263 else if (token
->is_op(OPERATOR_LPAREN
))
265 this->advance_token();
266 Type
* ret
= this->type();
267 if (this->peek_token()->is_op(OPERATOR_RPAREN
))
268 this->advance_token();
271 if (!ret
->is_error_type())
272 go_error_at(this->location(), "expected %<)%>");
278 go_error_at(token
->location(), "expected type");
279 return Type::make_error_type();
284 Parse::type_may_start_here()
286 const Token
* token
= this->peek_token();
287 return (token
->is_identifier()
288 || token
->is_op(OPERATOR_LSQUARE
)
289 || token
->is_op(OPERATOR_CHANOP
)
290 || token
->is_keyword(KEYWORD_CHAN
)
291 || token
->is_keyword(KEYWORD_INTERFACE
)
292 || token
->is_keyword(KEYWORD_FUNC
)
293 || token
->is_keyword(KEYWORD_MAP
)
294 || token
->is_keyword(KEYWORD_STRUCT
)
295 || token
->is_op(OPERATOR_MULT
)
296 || token
->is_op(OPERATOR_LPAREN
));
299 // TypeName = QualifiedIdent .
301 // If MAY_BE_NIL is true, then an identifier with the value of the
302 // predefined constant nil is accepted, returning the nil type.
305 Parse::type_name(bool issue_error
)
307 Location location
= this->location();
310 Named_object
* package
;
311 if (!this->qualified_ident(&name
, &package
))
312 return Type::make_error_type();
314 Named_object
* named_object
;
316 named_object
= this->gogo_
->lookup(name
, NULL
);
319 named_object
= package
->package_value()->lookup(name
);
320 if (named_object
== NULL
322 && package
->name() != this->gogo_
->package_name())
324 // Check whether the name is there but hidden.
325 std::string s
= ('.' + package
->package_value()->pkgpath()
327 named_object
= package
->package_value()->lookup(s
);
328 if (named_object
!= NULL
)
330 Package
* p
= package
->package_value();
331 const std::string
& packname(p
->package_name());
332 go_error_at(location
,
333 "invalid reference to hidden type %<%s.%s%>",
334 Gogo::message_name(packname
).c_str(),
335 Gogo::message_name(name
).c_str());
342 if (named_object
== NULL
)
345 named_object
= this->gogo_
->add_unknown_name(name
, location
);
348 const std::string
& packname(package
->package_value()->package_name());
349 go_error_at(location
, "reference to undefined identifier %<%s.%s%>",
350 Gogo::message_name(packname
).c_str(),
351 Gogo::message_name(name
).c_str());
356 else if (named_object
->is_type())
358 if (!named_object
->type_value()->is_visible())
361 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
369 go_error_at(location
, "expected type");
370 return Type::make_error_type();
373 if (named_object
->is_type())
374 return named_object
->type_value();
375 else if (named_object
->is_unknown() || named_object
->is_type_declaration())
376 return Type::make_forward_declaration(named_object
);
381 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
382 // ArrayLength = Expression .
383 // ElementType = CompleteType .
386 Parse::array_type(bool may_use_ellipsis
)
388 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
389 const Token
* token
= this->advance_token();
391 Expression
* length
= NULL
;
392 if (token
->is_op(OPERATOR_RSQUARE
))
393 this->advance_token();
396 if (!token
->is_op(OPERATOR_ELLIPSIS
))
397 length
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
398 else if (may_use_ellipsis
)
400 // An ellipsis is used in composite literals to represent a
401 // fixed array of the size of the number of elements. We
402 // use a length of nil to represent this, and change the
403 // length when parsing the composite literal.
404 length
= Expression::make_nil(this->location());
405 this->advance_token();
409 go_error_at(this->location(),
410 "use of %<[...]%> outside of array literal");
411 length
= Expression::make_error(this->location());
412 this->advance_token();
414 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
416 go_error_at(this->location(), "expected %<]%>");
417 return Type::make_error_type();
419 this->advance_token();
422 Type
* element_type
= this->type();
423 if (element_type
->is_error_type())
424 return Type::make_error_type();
426 return Type::make_array_type(element_type
, length
);
429 // MapType = "map" "[" KeyType "]" ValueType .
430 // KeyType = CompleteType .
431 // ValueType = CompleteType .
436 Location location
= this->location();
437 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP
));
438 if (!this->advance_token()->is_op(OPERATOR_LSQUARE
))
440 go_error_at(this->location(), "expected %<[%>");
441 return Type::make_error_type();
443 this->advance_token();
445 Type
* key_type
= this->type();
447 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
449 go_error_at(this->location(), "expected %<]%>");
450 return Type::make_error_type();
452 this->advance_token();
454 Type
* value_type
= this->type();
456 if (key_type
->is_error_type() || value_type
->is_error_type())
457 return Type::make_error_type();
459 return Type::make_map_type(key_type
, value_type
, location
);
462 // StructType = "struct" "{" { FieldDecl ";" } "}" .
467 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT
));
468 Location location
= this->location();
469 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
471 Location token_loc
= this->location();
472 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
473 && this->advance_token()->is_op(OPERATOR_LCURLY
))
474 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
477 go_error_at(this->location(), "expected %<{%>");
478 return Type::make_error_type();
481 this->advance_token();
483 Struct_field_list
* sfl
= new Struct_field_list
;
484 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
486 this->field_decl(sfl
);
487 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
488 this->advance_token();
489 else if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
491 go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
492 if (!this->skip_past_error(OPERATOR_RCURLY
))
493 return Type::make_error_type();
496 this->advance_token();
498 for (Struct_field_list::const_iterator pi
= sfl
->begin();
502 if (pi
->type()->is_error_type())
504 for (Struct_field_list::const_iterator pj
= pi
+ 1;
508 if (pi
->field_name() == pj
->field_name()
509 && !Gogo::is_sink_name(pi
->field_name()))
510 go_error_at(pi
->location(), "duplicate field name %<%s%>",
511 Gogo::message_name(pi
->field_name()).c_str());
515 return Type::make_struct_type(sfl
, location
);
518 // FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] .
519 // Tag = string_lit .
522 Parse::field_decl(Struct_field_list
* sfl
)
524 const Token
* token
= this->peek_token();
525 Location location
= token
->location();
527 bool is_anonymous_pointer
;
528 if (token
->is_op(OPERATOR_MULT
))
531 is_anonymous_pointer
= true;
533 else if (token
->is_identifier())
535 std::string id
= token
->identifier();
536 bool is_id_exported
= token
->is_identifier_exported();
537 Location id_location
= token
->location();
538 token
= this->advance_token();
539 is_anonymous
= (token
->is_op(OPERATOR_SEMICOLON
)
540 || token
->is_op(OPERATOR_RCURLY
)
541 || token
->is_op(OPERATOR_DOT
)
542 || token
->is_string());
543 is_anonymous_pointer
= false;
544 this->unget_token(Token::make_identifier_token(id
, is_id_exported
,
549 go_error_at(this->location(), "expected field name");
550 this->gogo_
->mark_locals_used();
551 while (!token
->is_op(OPERATOR_SEMICOLON
)
552 && !token
->is_op(OPERATOR_RCURLY
)
554 token
= this->advance_token();
560 if (is_anonymous_pointer
)
562 this->advance_token();
563 if (!this->peek_token()->is_identifier())
565 go_error_at(this->location(), "expected field name");
566 this->gogo_
->mark_locals_used();
567 while (!token
->is_op(OPERATOR_SEMICOLON
)
568 && !token
->is_op(OPERATOR_RCURLY
)
570 token
= this->advance_token();
574 Type
* type
= this->type_name(true);
577 if (this->peek_token()->is_string())
579 tag
= this->peek_token()->string_value();
580 this->advance_token();
583 if (!type
->is_error_type())
585 if (is_anonymous_pointer
)
586 type
= Type::make_pointer_type(type
);
587 sfl
->push_back(Struct_field(Typed_identifier("", type
, location
)));
589 sfl
->back().set_tag(tag
);
594 Typed_identifier_list til
;
597 token
= this->peek_token();
598 if (!token
->is_identifier())
600 go_error_at(this->location(), "expected identifier");
604 this->gogo_
->pack_hidden_name(token
->identifier(),
605 token
->is_identifier_exported());
606 til
.push_back(Typed_identifier(name
, NULL
, token
->location()));
607 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
609 this->advance_token();
612 Type
* type
= this->type();
615 if (this->peek_token()->is_string())
617 tag
= this->peek_token()->string_value();
618 this->advance_token();
621 for (Typed_identifier_list::iterator p
= til
.begin();
626 sfl
->push_back(Struct_field(*p
));
628 sfl
->back().set_tag(tag
);
633 // PointerType = "*" Type .
636 Parse::pointer_type()
638 go_assert(this->peek_token()->is_op(OPERATOR_MULT
));
639 this->advance_token();
640 Type
* type
= this->type();
641 if (type
->is_error_type())
643 return Type::make_pointer_type(type
);
646 // ChannelType = Channel | SendChannel | RecvChannel .
647 // Channel = "chan" ElementType .
648 // SendChannel = "chan" "<-" ElementType .
649 // RecvChannel = "<-" "chan" ElementType .
652 Parse::channel_type()
654 const Token
* token
= this->peek_token();
657 if (token
->is_op(OPERATOR_CHANOP
))
659 if (!this->advance_token()->is_keyword(KEYWORD_CHAN
))
661 go_error_at(this->location(), "expected %<chan%>");
662 return Type::make_error_type();
665 this->advance_token();
669 go_assert(token
->is_keyword(KEYWORD_CHAN
));
670 if (this->advance_token()->is_op(OPERATOR_CHANOP
))
673 this->advance_token();
677 // Better error messages for the common error of omitting the
678 // channel element type.
679 if (!this->type_may_start_here())
681 token
= this->peek_token();
682 if (token
->is_op(OPERATOR_RCURLY
))
683 go_error_at(this->location(), "unexpected %<}%> in channel type");
684 else if (token
->is_op(OPERATOR_RPAREN
))
685 go_error_at(this->location(), "unexpected %<)%> in channel type");
686 else if (token
->is_op(OPERATOR_COMMA
))
687 go_error_at(this->location(), "unexpected comma in channel type");
689 go_error_at(this->location(), "expected channel element type");
690 return Type::make_error_type();
693 Type
* element_type
= this->type();
694 return Type::make_channel_type(send
, receive
, element_type
);
697 // Give an error for a duplicate parameter or receiver name.
700 Parse::check_signature_names(const Typed_identifier_list
* params
,
703 for (Typed_identifier_list::const_iterator p
= params
->begin();
707 if (p
->name().empty() || Gogo::is_sink_name(p
->name()))
709 std::pair
<std::string
, const Typed_identifier
*> val
=
710 std::make_pair(p
->name(), &*p
);
711 std::pair
<Parse::Names::iterator
, bool> ins
= names
->insert(val
);
714 go_error_at(p
->location(), "redefinition of %qs",
715 Gogo::message_name(p
->name()).c_str());
716 go_inform(ins
.first
->second
->location(),
717 "previous definition of %qs was here",
718 Gogo::message_name(p
->name()).c_str());
723 // Signature = Parameters [ Result ] .
725 // RECEIVER is the receiver if there is one, or NULL. LOCATION is the
726 // location of the start of the type.
728 // This returns NULL on a parse error.
731 Parse::signature(Typed_identifier
* receiver
, Location location
)
733 bool is_varargs
= false;
734 Typed_identifier_list
* params
;
735 bool params_ok
= this->parameters(¶ms
, &is_varargs
);
737 Typed_identifier_list
* results
= NULL
;
738 if (this->peek_token()->is_op(OPERATOR_LPAREN
)
739 || this->type_may_start_here())
741 if (!this->result(&results
))
749 if (receiver
!= NULL
)
750 names
[receiver
->name()] = receiver
;
752 this->check_signature_names(params
, &names
);
754 this->check_signature_names(results
, &names
);
756 Function_type
* ret
= Type::make_function_type(receiver
, params
, results
,
759 ret
->set_is_varargs();
763 // Parameters = "(" [ ParameterList [ "," ] ] ")" .
765 // This returns false on a parse error.
768 Parse::parameters(Typed_identifier_list
** pparams
, bool* is_varargs
)
772 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
774 go_error_at(this->location(), "expected %<(%>");
778 Typed_identifier_list
* params
= NULL
;
779 bool saw_error
= false;
781 const Token
* token
= this->advance_token();
782 if (!token
->is_op(OPERATOR_RPAREN
))
784 params
= this->parameter_list(is_varargs
);
787 token
= this->peek_token();
790 // The optional trailing comma is picked up in parameter_list.
792 if (!token
->is_op(OPERATOR_RPAREN
))
794 go_error_at(this->location(), "expected %<)%>");
797 this->advance_token();
806 // ParameterList = ParameterDecl { "," ParameterDecl } .
808 // This sets *IS_VARARGS if the list ends with an ellipsis.
809 // IS_VARARGS will be NULL if varargs are not permitted.
811 // We pick up an optional trailing comma.
813 // This returns NULL if some error is seen.
815 Typed_identifier_list
*
816 Parse::parameter_list(bool* is_varargs
)
818 Location location
= this->location();
819 Typed_identifier_list
* ret
= new Typed_identifier_list();
821 bool saw_error
= false;
823 // If we see an identifier and then a comma, then we don't know
824 // whether we are looking at a list of identifiers followed by a
825 // type, or a list of types given by name. We have to do an
826 // arbitrary lookahead to figure it out.
828 bool parameters_have_names
;
829 const Token
* token
= this->peek_token();
830 if (!token
->is_identifier())
832 // This must be a type which starts with something like '*'.
833 parameters_have_names
= false;
837 std::string name
= token
->identifier();
838 bool is_exported
= token
->is_identifier_exported();
839 Location id_location
= token
->location();
840 token
= this->advance_token();
841 if (!token
->is_op(OPERATOR_COMMA
))
843 if (token
->is_op(OPERATOR_DOT
))
845 // This is a qualified identifier, which must turn out
847 parameters_have_names
= false;
849 else if (token
->is_op(OPERATOR_RPAREN
))
851 // A single identifier followed by a parenthesis must be
853 parameters_have_names
= false;
857 // An identifier followed by something other than a
858 // comma or a dot or a right parenthesis must be a
859 // parameter name followed by a type.
860 parameters_have_names
= true;
863 this->unget_token(Token::make_identifier_token(name
, is_exported
,
868 // An identifier followed by a comma may be the first in a
869 // list of parameter names followed by a type, or it may be
870 // the first in a list of types without parameter names. To
871 // find out we gather as many identifiers separated by
873 std::string id_name
= this->gogo_
->pack_hidden_name(name
,
875 ret
->push_back(Typed_identifier(id_name
, NULL
, id_location
));
876 bool just_saw_comma
= true;
877 while (this->advance_token()->is_identifier())
879 name
= this->peek_token()->identifier();
880 is_exported
= this->peek_token()->is_identifier_exported();
881 id_location
= this->peek_token()->location();
882 id_name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
883 ret
->push_back(Typed_identifier(id_name
, NULL
, id_location
));
884 if (!this->advance_token()->is_op(OPERATOR_COMMA
))
886 just_saw_comma
= false;
893 // We saw ID1 "," ID2 "," followed by something which
894 // was not an identifier. We must be seeing the start
895 // of a type, and ID1 and ID2 must be types, and the
896 // parameters don't have names.
897 parameters_have_names
= false;
899 else if (this->peek_token()->is_op(OPERATOR_RPAREN
))
901 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types,
902 // and the parameters don't have names.
903 parameters_have_names
= false;
905 else if (this->peek_token()->is_op(OPERATOR_DOT
))
907 // We saw ID1 "," ID2 ".". ID2 must be a package name,
908 // ID1 must be a type, and the parameters don't have
910 parameters_have_names
= false;
911 this->unget_token(Token::make_identifier_token(name
, is_exported
,
914 just_saw_comma
= true;
918 // We saw ID1 "," ID2 followed by something other than
919 // ",", ".", or ")". We must be looking at the start of
920 // a type, and ID1 and ID2 must be parameter names.
921 parameters_have_names
= true;
924 if (parameters_have_names
)
926 go_assert(!just_saw_comma
);
927 // We have just seen ID1, ID2 xxx.
929 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
933 go_error_at(this->location(),
934 "%<...%> only permits one name");
936 this->advance_token();
939 for (size_t i
= 0; i
< ret
->size(); ++i
)
940 ret
->set_type(i
, type
);
941 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
942 return saw_error
? NULL
: ret
;
943 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
944 return saw_error
? NULL
: ret
;
948 Typed_identifier_list
* tret
= new Typed_identifier_list();
949 for (Typed_identifier_list::const_iterator p
= ret
->begin();
953 Named_object
* no
= this->gogo_
->lookup(p
->name(), NULL
);
956 no
= this->gogo_
->add_unknown_name(p
->name(),
960 type
= no
->type_value();
961 else if (no
->is_unknown() || no
->is_type_declaration())
962 type
= Type::make_forward_declaration(no
);
965 go_error_at(p
->location(), "expected %<%s%> to be a type",
966 Gogo::message_name(p
->name()).c_str());
968 type
= Type::make_error_type();
970 tret
->push_back(Typed_identifier("", type
, p
->location()));
975 || this->peek_token()->is_op(OPERATOR_RPAREN
))
976 return saw_error
? NULL
: ret
;
981 bool mix_error
= false;
982 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
984 while (this->peek_token()->is_op(OPERATOR_COMMA
))
986 if (this->advance_token()->is_op(OPERATOR_RPAREN
))
988 if (is_varargs
!= NULL
&& *is_varargs
)
990 go_error_at(this->location(), "%<...%> must be last parameter");
993 this->parameter_decl(parameters_have_names
, ret
, is_varargs
, &mix_error
,
998 go_error_at(location
, "mixed named and unnamed function parameters");
1009 // ParameterDecl = [ IdentifierList ] [ "..." ] Type .
1012 Parse::parameter_decl(bool parameters_have_names
,
1013 Typed_identifier_list
* til
,
1018 if (!parameters_have_names
)
1021 Location location
= this->location();
1022 if (!this->peek_token()->is_identifier())
1024 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1025 type
= this->type();
1028 if (is_varargs
== NULL
)
1029 go_error_at(this->location(), "invalid use of %<...%>");
1032 this->advance_token();
1033 if (is_varargs
== NULL
1034 && this->peek_token()->is_op(OPERATOR_RPAREN
))
1035 type
= Type::make_error_type();
1038 Type
* element_type
= this->type();
1039 type
= Type::make_array_type(element_type
, NULL
);
1045 type
= this->type_name(false);
1046 if (type
->is_error_type()
1047 || (!this->peek_token()->is_op(OPERATOR_COMMA
)
1048 && !this->peek_token()->is_op(OPERATOR_RPAREN
)))
1051 while (!this->peek_token()->is_op(OPERATOR_COMMA
)
1052 && !this->peek_token()->is_op(OPERATOR_RPAREN
)
1053 && !this->peek_token()->is_eof())
1054 this->advance_token();
1057 if (!type
->is_error_type())
1058 til
->push_back(Typed_identifier("", type
, location
));
1064 size_t orig_count
= til
->size();
1065 if (this->peek_token()->is_identifier())
1066 this->identifier_list(til
);
1069 size_t new_count
= til
->size();
1072 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
1073 type
= this->type();
1076 if (is_varargs
== NULL
)
1078 go_error_at(this->location(), "invalid use of %<...%>");
1081 else if (new_count
> orig_count
+ 1)
1083 go_error_at(this->location(), "%<...%> only permits one name");
1088 this->advance_token();
1089 Type
* element_type
= this->type();
1090 type
= Type::make_array_type(element_type
, NULL
);
1092 for (size_t i
= orig_count
; i
< new_count
; ++i
)
1093 til
->set_type(i
, type
);
1097 // Result = Parameters | Type .
1099 // This returns false on a parse error.
1102 Parse::result(Typed_identifier_list
** presults
)
1104 if (this->peek_token()->is_op(OPERATOR_LPAREN
))
1105 return this->parameters(presults
, NULL
);
1108 Location location
= this->location();
1109 Type
* type
= this->type();
1110 if (type
->is_error_type())
1115 Typed_identifier_list
* til
= new Typed_identifier_list();
1116 til
->push_back(Typed_identifier("", type
, location
));
1122 // Block = "{" [ StatementList ] "}" .
1124 // Returns the location of the closing brace.
1129 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
1131 Location loc
= this->location();
1132 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1133 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1134 go_error_at(loc
, "unexpected semicolon or newline before %<{%>");
1137 go_error_at(this->location(), "expected %<{%>");
1138 return Linemap::unknown_location();
1142 const Token
* token
= this->advance_token();
1144 if (!token
->is_op(OPERATOR_RCURLY
))
1146 this->statement_list();
1147 token
= this->peek_token();
1148 if (!token
->is_op(OPERATOR_RCURLY
))
1150 if (!token
->is_eof() || !saw_errors())
1151 go_error_at(this->location(), "expected %<}%>");
1153 this->gogo_
->mark_locals_used();
1155 // Skip ahead to the end of the block, in hopes of avoiding
1156 // lots of meaningless errors.
1157 Location ret
= token
->location();
1159 while (!token
->is_eof())
1161 if (token
->is_op(OPERATOR_LCURLY
))
1163 else if (token
->is_op(OPERATOR_RCURLY
))
1168 this->advance_token();
1172 token
= this->advance_token();
1173 ret
= token
->location();
1179 Location ret
= token
->location();
1180 this->advance_token();
1184 // InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
1185 // MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1188 Parse::interface_type(bool record
)
1190 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE
));
1191 Location location
= this->location();
1193 if (!this->advance_token()->is_op(OPERATOR_LCURLY
))
1195 Location token_loc
= this->location();
1196 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1197 && this->advance_token()->is_op(OPERATOR_LCURLY
))
1198 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
1201 go_error_at(this->location(), "expected %<{%>");
1202 return Type::make_error_type();
1205 this->advance_token();
1207 Typed_identifier_list
* methods
= new Typed_identifier_list();
1208 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1210 this->method_spec(methods
);
1211 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1213 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
1215 this->method_spec(methods
);
1217 if (!this->peek_token()->is_op(OPERATOR_RCURLY
))
1219 go_error_at(this->location(), "expected %<}%>");
1220 while (!this->advance_token()->is_op(OPERATOR_RCURLY
))
1222 if (this->peek_token()->is_eof())
1223 return Type::make_error_type();
1227 this->advance_token();
1229 if (methods
->empty())
1235 Interface_type
* ret
;
1236 if (methods
== NULL
)
1237 ret
= Type::make_empty_interface_type(location
);
1239 ret
= Type::make_interface_type(methods
, location
);
1241 this->gogo_
->record_interface_type(ret
);
1245 // MethodSpec = MethodName Signature | InterfaceTypeName .
1246 // MethodName = identifier .
1247 // InterfaceTypeName = TypeName .
1250 Parse::method_spec(Typed_identifier_list
* methods
)
1252 const Token
* token
= this->peek_token();
1253 if (!token
->is_identifier())
1255 go_error_at(this->location(), "expected identifier");
1259 std::string name
= token
->identifier();
1260 bool is_exported
= token
->is_identifier_exported();
1261 Location location
= token
->location();
1263 if (this->advance_token()->is_op(OPERATOR_LPAREN
))
1265 // This is a MethodName.
1267 go_error_at(this->location(),
1268 "methods must have a unique non-blank name");
1269 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1270 Type
* type
= this->signature(NULL
, location
);
1273 methods
->push_back(Typed_identifier(name
, type
, location
));
1277 this->unget_token(Token::make_identifier_token(name
, is_exported
,
1279 Type
* type
= this->type_name(false);
1280 if (type
->is_error_type()
1281 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1282 && !this->peek_token()->is_op(OPERATOR_RCURLY
)))
1284 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1285 go_error_at(this->location(),
1286 "name list not allowed in interface type");
1288 go_error_at(location
, "expected signature or type name");
1289 this->gogo_
->mark_locals_used();
1290 token
= this->peek_token();
1291 while (!token
->is_eof()
1292 && !token
->is_op(OPERATOR_SEMICOLON
)
1293 && !token
->is_op(OPERATOR_RCURLY
))
1294 token
= this->advance_token();
1297 // This must be an interface type, but we can't check that now.
1298 // We check it and pull out the methods in
1299 // Interface_type::do_verify.
1300 methods
->push_back(Typed_identifier("", type
, location
));
1304 // Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1307 Parse::declaration()
1309 const Token
* token
= this->peek_token();
1311 unsigned int pragmas
= this->lex_
->get_and_clear_pragmas();
1313 && !token
->is_keyword(KEYWORD_FUNC
)
1314 && !token
->is_keyword(KEYWORD_TYPE
))
1315 go_warning_at(token
->location(), 0,
1316 "ignoring magic comment before non-function");
1318 if (token
->is_keyword(KEYWORD_CONST
))
1320 else if (token
->is_keyword(KEYWORD_TYPE
))
1321 this->type_decl(pragmas
);
1322 else if (token
->is_keyword(KEYWORD_VAR
))
1324 else if (token
->is_keyword(KEYWORD_FUNC
))
1325 this->function_decl(pragmas
);
1328 go_error_at(this->location(), "expected declaration");
1329 this->advance_token();
1334 Parse::declaration_may_start_here()
1336 const Token
* token
= this->peek_token();
1337 return (token
->is_keyword(KEYWORD_CONST
)
1338 || token
->is_keyword(KEYWORD_TYPE
)
1339 || token
->is_keyword(KEYWORD_VAR
)
1340 || token
->is_keyword(KEYWORD_FUNC
));
1343 // Decl<P> = P | "(" [ List<P> ] ")" .
1346 Parse::decl(void (Parse::*pfn
)(void*, unsigned int), void* varg
,
1347 unsigned int pragmas
)
1349 if (this->peek_token()->is_eof())
1352 go_error_at(this->location(), "unexpected end of file");
1356 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1357 (this->*pfn
)(varg
, pragmas
);
1361 go_warning_at(this->location(), 0,
1362 "ignoring magic %<//go:...%> comment before group");
1363 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1365 this->list(pfn
, varg
, true);
1366 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1368 go_error_at(this->location(), "missing %<)%>");
1369 while (!this->advance_token()->is_op(OPERATOR_RPAREN
))
1371 if (this->peek_token()->is_eof())
1376 this->advance_token();
1380 // List<P> = P { ";" P } [ ";" ] .
1382 // In order to pick up the trailing semicolon we need to know what
1383 // might follow. This is either a '}' or a ')'.
1386 Parse::list(void (Parse::*pfn
)(void*, unsigned int), void* varg
,
1387 bool follow_is_paren
)
1389 (this->*pfn
)(varg
, 0);
1390 Operator follow
= follow_is_paren
? OPERATOR_RPAREN
: OPERATOR_RCURLY
;
1391 while (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1392 || this->peek_token()->is_op(OPERATOR_COMMA
))
1394 if (this->peek_token()->is_op(OPERATOR_COMMA
))
1395 go_error_at(this->location(), "unexpected comma");
1396 if (this->advance_token()->is_op(follow
))
1398 (this->*pfn
)(varg
, 0);
1402 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
1407 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST
));
1408 this->advance_token();
1411 Type
* last_type
= NULL
;
1412 Expression_list
* last_expr_list
= NULL
;
1414 if (!this->peek_token()->is_op(OPERATOR_LPAREN
))
1415 this->const_spec(iota
, &last_type
, &last_expr_list
);
1418 this->advance_token();
1419 while (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1421 this->const_spec(iota
, &last_type
, &last_expr_list
);
1423 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
1424 this->advance_token();
1425 else if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
1427 go_error_at(this->location(),
1428 "expected %<;%> or %<)%> or newline");
1429 if (!this->skip_past_error(OPERATOR_RPAREN
))
1433 this->advance_token();
1436 if (last_expr_list
!= NULL
)
1437 delete last_expr_list
;
1440 // ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
1443 Parse::const_spec(int iota
, Type
** last_type
, Expression_list
** last_expr_list
)
1445 Location loc
= this->location();
1446 Typed_identifier_list til
;
1447 this->identifier_list(&til
);
1450 if (this->type_may_start_here())
1452 type
= this->type();
1454 *last_expr_list
= NULL
;
1457 Expression_list
*expr_list
;
1458 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1460 if (*last_expr_list
== NULL
)
1462 go_error_at(this->location(), "expected %<=%>");
1466 expr_list
= new Expression_list
;
1467 for (Expression_list::const_iterator p
= (*last_expr_list
)->begin();
1468 p
!= (*last_expr_list
)->end();
1471 Expression
* copy
= (*p
)->copy();
1472 copy
->set_location(loc
);
1473 expr_list
->push_back(copy
);
1478 this->advance_token();
1479 expr_list
= this->expression_list(NULL
, false, true);
1481 if (*last_expr_list
!= NULL
)
1482 delete *last_expr_list
;
1483 *last_expr_list
= expr_list
;
1486 Expression_list::const_iterator pe
= expr_list
->begin();
1487 for (Typed_identifier_list::iterator pi
= til
.begin();
1491 if (pe
== expr_list
->end())
1493 go_error_at(this->location(), "not enough initializers");
1499 if (!Gogo::is_sink_name(pi
->name()))
1500 this->gogo_
->add_constant(*pi
, *pe
, iota
);
1505 snprintf(buf
, sizeof buf
, ".$sinkconst%d", count
);
1507 Typed_identifier
ti(std::string(buf
), type
, pi
->location());
1508 Named_object
* no
= this->gogo_
->add_constant(ti
, *pe
, iota
);
1509 no
->const_value()->set_is_sink();
1512 if (pe
!= expr_list
->end())
1513 go_error_at(this->location(), "too many initializers");
1518 // TypeDecl = "type" Decl<TypeSpec> .
1521 Parse::type_decl(unsigned int pragmas
)
1523 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE
));
1524 this->advance_token();
1525 this->decl(&Parse::type_spec
, NULL
, pragmas
);
1528 // TypeSpec = identifier ["="] Type .
1531 Parse::type_spec(void*, unsigned int pragmas
)
1533 const Token
* token
= this->peek_token();
1534 if (!token
->is_identifier())
1536 go_error_at(this->location(), "expected identifier");
1539 std::string name
= token
->identifier();
1540 bool is_exported
= token
->is_identifier_exported();
1541 Location location
= token
->location();
1542 token
= this->advance_token();
1544 bool is_alias
= false;
1545 if (token
->is_op(OPERATOR_EQ
))
1548 token
= this->advance_token();
1551 // The scope of the type name starts at the point where the
1552 // identifier appears in the source code. We implement this by
1553 // declaring the type before we read the type definition.
1554 Named_object
* named_type
= NULL
;
1557 name
= this->gogo_
->pack_hidden_name(name
, is_exported
);
1558 named_type
= this->gogo_
->declare_type(name
, location
);
1562 if (name
== "_" && token
->is_keyword(KEYWORD_INTERFACE
))
1564 // We call Parse::interface_type explicity here because we do not want
1565 // to record an interface with a blank type name.
1566 type
= this->interface_type(false);
1568 else if (!token
->is_op(OPERATOR_SEMICOLON
))
1569 type
= this->type();
1572 go_error_at(this->location(),
1573 "unexpected semicolon or newline in type declaration");
1574 type
= Type::make_error_type();
1577 if (type
->is_error_type())
1579 this->gogo_
->mark_locals_used();
1580 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1581 && !this->peek_token()->is_eof())
1582 this->advance_token();
1587 if (named_type
->is_type_declaration())
1589 Type
* ftype
= type
->forwarded();
1590 if (ftype
->forward_declaration_type() != NULL
1591 && (ftype
->forward_declaration_type()->named_object()
1594 go_error_at(location
, "invalid recursive type");
1595 type
= Type::make_error_type();
1598 Named_type
* nt
= Type::make_named_type(named_type
, type
, location
);
1602 this->gogo_
->define_type(named_type
, nt
);
1603 go_assert(named_type
->package() == NULL
);
1605 if ((pragmas
& GOPRAGMA_NOTINHEAP
) != 0)
1607 nt
->set_not_in_heap();
1608 pragmas
&= ~GOPRAGMA_NOTINHEAP
;
1611 go_warning_at(location
, 0,
1612 "ignoring magic %<//go:...%> comment before type");
1616 // This will probably give a redefinition error.
1617 this->gogo_
->add_type(name
, type
, location
);
1622 // VarDecl = "var" Decl<VarSpec> .
1627 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR
));
1628 this->advance_token();
1629 this->decl(&Parse::var_spec
, NULL
, 0);
1632 // VarSpec = IdentifierList
1633 // ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1636 Parse::var_spec(void*, unsigned int pragmas
)
1639 go_warning_at(this->location(), 0,
1640 "ignoring magic %<//go:...%> comment before var");
1642 // Get the variable names.
1643 Typed_identifier_list til
;
1644 this->identifier_list(&til
);
1646 Location location
= this->location();
1649 Expression_list
* init
= NULL
;
1650 if (!this->peek_token()->is_op(OPERATOR_EQ
))
1652 type
= this->type();
1653 if (type
->is_error_type())
1655 this->gogo_
->mark_locals_used();
1656 while (!this->peek_token()->is_op(OPERATOR_EQ
)
1657 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
1658 && !this->peek_token()->is_eof())
1659 this->advance_token();
1661 if (this->peek_token()->is_op(OPERATOR_EQ
))
1663 this->advance_token();
1664 init
= this->expression_list(NULL
, false, true);
1669 this->advance_token();
1670 init
= this->expression_list(NULL
, false, true);
1673 this->init_vars(&til
, type
, init
, false, location
);
1679 // Create variables. TIL is a list of variable names. If TYPE is not
1680 // NULL, it is the type of all the variables. If INIT is not NULL, it
1681 // is an initializer list for the variables.
1684 Parse::init_vars(const Typed_identifier_list
* til
, Type
* type
,
1685 Expression_list
* init
, bool is_coloneq
,
1688 // Check for an initialization which can yield multiple values.
1689 if (init
!= NULL
&& init
->size() == 1 && til
->size() > 1)
1691 if (this->init_vars_from_call(til
, type
, *init
->begin(), is_coloneq
,
1694 if (this->init_vars_from_map(til
, type
, *init
->begin(), is_coloneq
,
1697 if (this->init_vars_from_receive(til
, type
, *init
->begin(), is_coloneq
,
1700 if (this->init_vars_from_type_guard(til
, type
, *init
->begin(),
1701 is_coloneq
, location
))
1705 if (init
!= NULL
&& init
->size() != til
->size())
1707 if (init
->empty() || !init
->front()->is_error_expression())
1708 go_error_at(location
, "wrong number of initializations");
1711 type
= Type::make_error_type();
1714 // Note that INIT was already parsed with the old name bindings, so
1715 // we don't have to worry that it will accidentally refer to the
1716 // newly declared variables. But we do have to worry about a mix of
1717 // newly declared variables and old variables if the old variables
1718 // appear in the initializations.
1720 Expression_list::const_iterator pexpr
;
1722 pexpr
= init
->begin();
1723 bool any_new
= false;
1724 Expression_list
* vars
= new Expression_list();
1725 Expression_list
* vals
= new Expression_list();
1726 for (Typed_identifier_list::const_iterator p
= til
->begin();
1731 go_assert(pexpr
!= init
->end());
1732 this->init_var(*p
, type
, init
== NULL
? NULL
: *pexpr
, is_coloneq
,
1733 false, &any_new
, vars
, vals
);
1738 go_assert(pexpr
== init
->end());
1739 if (is_coloneq
&& !any_new
)
1740 go_error_at(location
, "variables redeclared but no variable is new");
1741 this->finish_init_vars(vars
, vals
, location
);
1744 // See if we need to initialize a list of variables from a function
1745 // call. This returns true if we have set up the variables and the
1749 Parse::init_vars_from_call(const Typed_identifier_list
* vars
, Type
* type
,
1750 Expression
* expr
, bool is_coloneq
,
1753 Call_expression
* call
= expr
->call_expression();
1757 // This is a function call. We can't check here whether it returns
1758 // the right number of values, but it might. Declare the variables,
1759 // and then assign the results of the call to them.
1761 call
->set_expected_result_count(vars
->size());
1763 Named_object
* first_var
= NULL
;
1764 unsigned int index
= 0;
1765 bool any_new
= false;
1766 Expression_list
* ivars
= new Expression_list();
1767 Expression_list
* ivals
= new Expression_list();
1768 for (Typed_identifier_list::const_iterator pv
= vars
->begin();
1772 Expression
* init
= Expression::make_call_result(call
, index
);
1773 Named_object
* no
= this->init_var(*pv
, type
, init
, is_coloneq
, false,
1774 &any_new
, ivars
, ivals
);
1776 if (this->gogo_
->in_global_scope() && no
->is_variable())
1778 if (first_var
== NULL
)
1782 // If the current object is a redefinition of another object, we
1783 // might have already recorded the dependency relationship between
1784 // it and the first variable. Either way, an error will be
1785 // reported for the redefinition and we don't need to properly
1786 // record dependency information for an invalid program.
1787 if (no
->is_redefinition())
1790 // The subsequent vars have an implicit dependency on
1791 // the first one, so that everything gets initialized in
1792 // the right order and so that we detect cycles
1794 this->gogo_
->record_var_depends_on(no
->var_value(), first_var
);
1799 if (is_coloneq
&& !any_new
)
1800 go_error_at(location
, "variables redeclared but no variable is new");
1802 this->finish_init_vars(ivars
, ivals
, location
);
1807 // See if we need to initialize a pair of values from a map index
1808 // expression. This returns true if we have set up the variables and
1809 // the initialization.
1812 Parse::init_vars_from_map(const Typed_identifier_list
* vars
, Type
* type
,
1813 Expression
* expr
, bool is_coloneq
,
1816 Index_expression
* index
= expr
->index_expression();
1819 if (vars
->size() != 2)
1822 // This is an index which is being assigned to two variables. It
1823 // must be a map index. Declare the variables, and then assign the
1824 // results of the map index.
1825 bool any_new
= false;
1826 Typed_identifier_list::const_iterator p
= vars
->begin();
1827 Expression
* init
= type
== NULL
? index
: NULL
;
1828 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1829 type
== NULL
, &any_new
, NULL
, NULL
);
1830 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1831 val_no
->var_value()->set_type_from_init_tuple();
1832 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1835 Type
* var_type
= type
;
1836 if (var_type
== NULL
)
1837 var_type
= Type::lookup_bool_type();
1838 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1839 &any_new
, NULL
, NULL
);
1840 Expression
* present_var
= Expression::make_var_reference(no
, location
);
1842 if (is_coloneq
&& !any_new
)
1843 go_error_at(location
, "variables redeclared but no variable is new");
1845 Statement
* s
= Statement::make_tuple_map_assignment(val_var
, present_var
,
1848 if (!this->gogo_
->in_global_scope())
1849 this->gogo_
->add_statement(s
);
1850 else if (!val_no
->is_sink())
1852 if (val_no
->is_variable())
1853 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1855 else if (!no
->is_sink())
1857 if (no
->is_variable())
1858 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1862 // Execute the map index expression just so that we can fail if
1864 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1866 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1872 // See if we need to initialize a pair of values from a receive
1873 // expression. This returns true if we have set up the variables and
1874 // the initialization.
1877 Parse::init_vars_from_receive(const Typed_identifier_list
* vars
, Type
* type
,
1878 Expression
* expr
, bool is_coloneq
,
1881 Receive_expression
* receive
= expr
->receive_expression();
1882 if (receive
== NULL
)
1884 if (vars
->size() != 2)
1887 // This is a receive expression which is being assigned to two
1888 // variables. Declare the variables, and then assign the results of
1890 bool any_new
= false;
1891 Typed_identifier_list::const_iterator p
= vars
->begin();
1892 Expression
* init
= type
== NULL
? receive
: NULL
;
1893 Named_object
* val_no
= this->init_var(*p
, type
, init
, is_coloneq
,
1894 type
== NULL
, &any_new
, NULL
, NULL
);
1895 if (type
== NULL
&& any_new
&& val_no
->is_variable())
1896 val_no
->var_value()->set_type_from_init_tuple();
1897 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1900 Type
* var_type
= type
;
1901 if (var_type
== NULL
)
1902 var_type
= Type::lookup_bool_type();
1903 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1904 &any_new
, NULL
, NULL
);
1905 Expression
* received_var
= Expression::make_var_reference(no
, location
);
1907 if (is_coloneq
&& !any_new
)
1908 go_error_at(location
, "variables redeclared but no variable is new");
1910 Statement
* s
= Statement::make_tuple_receive_assignment(val_var
,
1915 if (!this->gogo_
->in_global_scope())
1916 this->gogo_
->add_statement(s
);
1917 else if (!val_no
->is_sink())
1919 if (val_no
->is_variable())
1920 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1922 else if (!no
->is_sink())
1924 if (no
->is_variable())
1925 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1929 Named_object
* dummy
= this->create_dummy_global(Type::lookup_bool_type(),
1931 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
1937 // See if we need to initialize a pair of values from a type guard
1938 // expression. This returns true if we have set up the variables and
1939 // the initialization.
1942 Parse::init_vars_from_type_guard(const Typed_identifier_list
* vars
,
1943 Type
* type
, Expression
* expr
,
1944 bool is_coloneq
, Location location
)
1946 Type_guard_expression
* type_guard
= expr
->type_guard_expression();
1947 if (type_guard
== NULL
)
1949 if (vars
->size() != 2)
1952 // This is a type guard expression which is being assigned to two
1953 // variables. Declare the variables, and then assign the results of
1955 bool any_new
= false;
1956 Typed_identifier_list::const_iterator p
= vars
->begin();
1957 Type
* var_type
= type
;
1958 if (var_type
== NULL
)
1959 var_type
= type_guard
->type();
1960 Named_object
* val_no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1961 &any_new
, NULL
, NULL
);
1962 Expression
* val_var
= Expression::make_var_reference(val_no
, location
);
1966 if (var_type
== NULL
)
1967 var_type
= Type::lookup_bool_type();
1968 Named_object
* no
= this->init_var(*p
, var_type
, NULL
, is_coloneq
, false,
1969 &any_new
, NULL
, NULL
);
1970 Expression
* ok_var
= Expression::make_var_reference(no
, location
);
1972 Expression
* texpr
= type_guard
->expr();
1973 Type
* t
= type_guard
->type();
1974 Statement
* s
= Statement::make_tuple_type_guard_assignment(val_var
, ok_var
,
1978 if (is_coloneq
&& !any_new
)
1979 go_error_at(location
, "variables redeclared but no variable is new");
1981 if (!this->gogo_
->in_global_scope())
1982 this->gogo_
->add_statement(s
);
1983 else if (!val_no
->is_sink())
1985 if (val_no
->is_variable())
1986 val_no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1988 else if (!no
->is_sink())
1990 if (no
->is_variable())
1991 no
->var_value()->add_preinit_statement(this->gogo_
, s
);
1995 Named_object
* dummy
= this->create_dummy_global(type
, NULL
, location
);
1996 dummy
->var_value()->add_preinit_statement(this->gogo_
, s
);
2002 // Create a single variable. If IS_COLONEQ is true, we permit
2003 // redeclarations in the same block, and we set *IS_NEW when we find a
2004 // new variable which is not a redeclaration.
2007 Parse::init_var(const Typed_identifier
& tid
, Type
* type
, Expression
* init
,
2008 bool is_coloneq
, bool type_from_init
, bool* is_new
,
2009 Expression_list
* vars
, Expression_list
* vals
)
2011 Location location
= tid
.location();
2013 if (Gogo::is_sink_name(tid
.name()))
2015 if (!type_from_init
&& init
!= NULL
)
2017 if (this->gogo_
->in_global_scope())
2018 return this->create_dummy_global(type
, init
, location
);
2021 // Create a dummy variable so that we will check whether the
2022 // initializer can be assigned to the type.
2023 Variable
* var
= new Variable(type
, init
, false, false, false,
2028 snprintf(buf
, sizeof buf
, "sink$%d", count
);
2030 return this->gogo_
->add_variable(buf
, var
);
2034 this->gogo_
->add_type_to_verify(type
);
2035 return this->gogo_
->add_sink();
2040 Named_object
* no
= this->gogo_
->lookup_in_block(tid
.name());
2042 && (no
->is_variable() || no
->is_result_variable()))
2044 // INIT may be NULL even when IS_COLONEQ is true for cases
2045 // like v, ok := x.(int).
2046 if (!type_from_init
&& init
!= NULL
)
2048 go_assert(vars
!= NULL
&& vals
!= NULL
);
2049 vars
->push_back(Expression::make_var_reference(no
, location
));
2050 vals
->push_back(init
);
2056 Variable
* var
= new Variable(type
, init
, this->gogo_
->in_global_scope(),
2057 false, false, location
);
2058 Named_object
* no
= this->gogo_
->add_variable(tid
.name(), var
);
2059 if (!no
->is_variable())
2061 // The name is already defined, so we just gave an error.
2062 return this->gogo_
->add_sink();
2067 // Create a dummy global variable to force an initializer to be run in
2068 // the right place. This is used when a sink variable is initialized
2072 Parse::create_dummy_global(Type
* type
, Expression
* init
,
2075 if (type
== NULL
&& init
== NULL
)
2076 type
= Type::lookup_bool_type();
2077 Variable
* var
= new Variable(type
, init
, true, false, false, location
);
2078 var
->set_is_global_sink();
2081 snprintf(buf
, sizeof buf
, "_.%d", count
);
2083 return this->gogo_
->add_variable(buf
, var
);
2086 // Finish the variable initialization by executing any assignments to
2087 // existing variables when using :=. These must be done as a tuple
2088 // assignment in case of something like n, a, b := 1, b, a.
2091 Parse::finish_init_vars(Expression_list
* vars
, Expression_list
* vals
,
2099 else if (vars
->size() == 1)
2101 go_assert(!this->gogo_
->in_global_scope());
2102 this->gogo_
->add_statement(Statement::make_assignment(vars
->front(),
2110 go_assert(!this->gogo_
->in_global_scope());
2111 this->gogo_
->add_statement(Statement::make_tuple_assignment(vars
, vals
,
2116 // SimpleVarDecl = identifier ":=" Expression .
2118 // We've already seen the identifier.
2120 // FIXME: We also have to implement
2121 // IdentifierList ":=" ExpressionList
2122 // In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
2123 // tuple assignments here as well.
2125 // If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
2126 // side may be a composite literal.
2128 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
2131 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
2132 // guard (var := expr.("type") using the literal keyword "type").
2135 Parse::simple_var_decl_or_assignment(const std::string
& name
,
2137 bool may_be_composite_lit
,
2138 Range_clause
* p_range_clause
,
2139 Type_switch
* p_type_switch
)
2141 Typed_identifier_list til
;
2142 til
.push_back(Typed_identifier(name
, NULL
, location
));
2144 std::set
<std::string
> uniq_idents
;
2145 uniq_idents
.insert(name
);
2146 std::string dup_name
;
2149 // We've seen one identifier. If we see a comma now, this could be
2151 if (this->peek_token()->is_op(OPERATOR_COMMA
))
2153 go_assert(p_type_switch
== NULL
);
2156 const Token
* token
= this->advance_token();
2157 if (!token
->is_identifier())
2160 std::string id
= token
->identifier();
2161 bool is_id_exported
= token
->is_identifier_exported();
2162 Location id_location
= token
->location();
2163 std::pair
<std::set
<std::string
>::iterator
, bool> ins
;
2165 token
= this->advance_token();
2166 if (!token
->is_op(OPERATOR_COMMA
))
2168 if (token
->is_op(OPERATOR_COLONEQ
))
2170 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2171 ins
= uniq_idents
.insert(id
);
2172 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2174 // Use %s to print := to avoid -Wformat-diag warning.
2175 go_error_at(id_location
,
2176 "%qs repeated on left side of %s",
2177 Gogo::message_name(id
).c_str(), ":=");
2179 til
.push_back(Typed_identifier(id
, NULL
, location
));
2182 this->unget_token(Token::make_identifier_token(id
,
2188 id
= this->gogo_
->pack_hidden_name(id
, is_id_exported
);
2189 ins
= uniq_idents
.insert(id
);
2190 if (!ins
.second
&& !Gogo::is_sink_name(id
))
2192 dup_name
= Gogo::message_name(id
);
2193 dup_loc
= id_location
;
2195 til
.push_back(Typed_identifier(id
, NULL
, location
));
2198 // We have a comma separated list of identifiers in TIL. If the
2199 // next token is COLONEQ, then this is a simple var decl, and we
2200 // have the complete list of identifiers. If the next token is
2201 // not COLONEQ, then the only valid parse is a tuple assignment.
2202 // The list of identifiers we have so far is really a list of
2203 // expressions. There are more expressions following.
2205 if (!this->peek_token()->is_op(OPERATOR_COLONEQ
))
2207 Expression_list
* exprs
= new Expression_list
;
2208 for (Typed_identifier_list::const_iterator p
= til
.begin();
2211 exprs
->push_back(this->id_to_expression(p
->name(), p
->location(),
2214 Expression_list
* more_exprs
=
2215 this->expression_list(NULL
, true, may_be_composite_lit
);
2216 for (Expression_list::const_iterator p
= more_exprs
->begin();
2217 p
!= more_exprs
->end();
2219 exprs
->push_back(*p
);
2222 this->tuple_assignment(exprs
, may_be_composite_lit
, p_range_clause
);
2227 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ
));
2228 const Token
* token
= this->advance_token();
2230 if (!dup_name
.empty())
2232 // Use %s to print := to avoid -Wformat-diag warning.
2233 go_error_at(dup_loc
, "%qs repeated on left side of %s",
2234 dup_name
.c_str(), ":=");
2237 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
2239 this->range_clause_decl(&til
, p_range_clause
);
2243 Expression_list
* init
;
2244 if (p_type_switch
== NULL
)
2245 init
= this->expression_list(NULL
, false, may_be_composite_lit
);
2248 bool is_type_switch
= false;
2249 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false,
2250 may_be_composite_lit
,
2251 &is_type_switch
, NULL
);
2254 p_type_switch
->found
= true;
2255 p_type_switch
->name
= name
;
2256 p_type_switch
->location
= location
;
2257 p_type_switch
->expr
= expr
;
2261 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
2263 init
= new Expression_list();
2264 init
->push_back(expr
);
2268 this->advance_token();
2269 init
= this->expression_list(expr
, false, may_be_composite_lit
);
2273 this->init_vars(&til
, NULL
, init
, true, location
);
2276 // FunctionDecl = "func" identifier Signature [ Block ] .
2277 // MethodDecl = "func" Receiver identifier Signature [ Block ] .
2279 // Deprecated gcc extension:
2280 // FunctionDecl = "func" identifier Signature
2281 // __asm__ "(" string_lit ")" .
2282 // This extension means a function whose real name is the identifier
2283 // inside the asm. This extension will be removed at some future
2284 // date. It has been replaced with //extern or //go:linkname comments.
2286 // PRAGMAS is a bitset of magic comments.
2289 Parse::function_decl(unsigned int pragmas
)
2291 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2292 Location location
= this->location();
2293 std::string extern_name
= this->lex_
->extern_name();
2294 const Token
* token
= this->advance_token();
2296 bool expected_receiver
= false;
2297 Typed_identifier
* rec
= NULL
;
2298 if (token
->is_op(OPERATOR_LPAREN
))
2300 expected_receiver
= true;
2301 rec
= this->receiver();
2302 token
= this->peek_token();
2305 if (!token
->is_identifier())
2307 go_error_at(this->location(), "expected function name");
2312 this->gogo_
->pack_hidden_name(token
->identifier(),
2313 token
->is_identifier_exported());
2315 this->advance_token();
2317 Function_type
* fntype
= this->signature(rec
, this->location());
2319 Named_object
* named_object
= NULL
;
2321 if (this->peek_token()->is_keyword(KEYWORD_ASM
))
2323 if (!this->advance_token()->is_op(OPERATOR_LPAREN
))
2325 go_error_at(this->location(), "expected %<(%>");
2328 token
= this->advance_token();
2329 if (!token
->is_string())
2331 go_error_at(this->location(), "expected string");
2334 std::string asm_name
= token
->string_value();
2335 if (!this->advance_token()->is_op(OPERATOR_RPAREN
))
2337 go_error_at(this->location(), "expected %<)%>");
2340 this->advance_token();
2341 if (!Gogo::is_sink_name(name
))
2343 named_object
= this->gogo_
->declare_function(name
, fntype
, location
);
2344 if (named_object
->is_function_declaration())
2345 named_object
->func_declaration_value()->set_asm_name(asm_name
);
2349 // Check for the easy error of a newline before the opening brace.
2350 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
2352 Location semi_loc
= this->location();
2353 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
2354 go_error_at(this->location(),
2355 "unexpected semicolon or newline before %<{%>");
2357 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
2369 { GOPRAGMA_NOINTERFACE
, "nointerface", false, false, true },
2370 { GOPRAGMA_NOESCAPE
, "noescape", true, false, false },
2371 { GOPRAGMA_NORACE
, "norace", false, true, true },
2372 { GOPRAGMA_NOSPLIT
, "nosplit", false, true, true },
2373 { GOPRAGMA_NOINLINE
, "noinline", false, true, true },
2374 { GOPRAGMA_SYSTEMSTACK
, "systemstack", false, true, true },
2375 { GOPRAGMA_NOWRITEBARRIER
, "nowritebarrier", false, true, true },
2376 { GOPRAGMA_NOWRITEBARRIERREC
, "nowritebarrierrec", false, true,
2378 { GOPRAGMA_YESWRITEBARRIERREC
, "yeswritebarrierrec", false, true,
2380 { GOPRAGMA_CGOUNSAFEARGS
, "cgo_unsafe_args", false, true, true },
2381 { GOPRAGMA_UINTPTRESCAPES
, "uintptrescapes", true, true, true },
2384 bool is_decl
= !this->peek_token()->is_op(OPERATOR_LCURLY
);
2388 i
< sizeof(pragma_check
) / sizeof(pragma_check
[0]);
2391 if ((pragmas
& pragma_check
[i
].bit
) == 0)
2396 if (pragma_check
[i
].decl_ok
)
2398 go_warning_at(location
, 0,
2399 ("ignoring magic %<//go:%s%> comment "
2400 "before declaration"),
2401 pragma_check
[i
].name
);
2403 else if (rec
== NULL
)
2405 if (pragma_check
[i
].func_ok
)
2407 go_warning_at(location
, 0,
2408 ("ignoring magic %<//go:%s%> comment "
2409 "before function definition"),
2410 pragma_check
[i
].name
);
2414 if (pragma_check
[i
].method_ok
)
2416 go_warning_at(location
, 0,
2417 ("ignoring magic %<//go:%s%> comment "
2418 "before method definition"),
2419 pragma_check
[i
].name
);
2422 pragmas
&= ~ pragma_check
[i
].bit
;
2428 if (named_object
== NULL
)
2430 // Function declarations with the blank identifier as a name are
2431 // mostly ignored since they cannot be called. We make an object
2432 // for this declaration for type-checking purposes.
2433 if (Gogo::is_sink_name(name
))
2437 snprintf(buf
, sizeof buf
, ".$sinkfndecl%d", count
);
2439 name
= std::string(buf
);
2443 || (expected_receiver
&& rec
== NULL
))
2444 this->gogo_
->add_erroneous_name(name
);
2447 named_object
= this->gogo_
->declare_function(name
, fntype
,
2449 if (!extern_name
.empty()
2450 && named_object
->is_function_declaration())
2452 Function_declaration
* fd
=
2453 named_object
->func_declaration_value();
2454 fd
->set_asm_name(extern_name
);
2459 if (pragmas
!= 0 && named_object
->is_function_declaration())
2460 named_object
->func_declaration_value()->set_pragmas(pragmas
);
2464 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2467 fntype
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2468 this->is_erroneous_function_
= true;
2469 if (!Gogo::is_sink_name(name
))
2470 this->gogo_
->add_erroneous_name(name
);
2471 name
= this->gogo_
->pack_hidden_name("_", false);
2473 named_object
= this->gogo_
->start_function(name
, fntype
, true, location
);
2474 Location end_loc
= this->block();
2475 this->gogo_
->finish_function(end_loc
);
2478 && !this->is_erroneous_function_
2479 && named_object
->is_function())
2480 named_object
->func_value()->set_pragmas(pragmas
);
2481 this->is_erroneous_function_
= hold_is_erroneous_function
;
2485 // Receiver = Parameters .
2490 Location location
= this->location();
2491 Typed_identifier_list
* til
;
2492 if (!this->parameters(&til
, NULL
))
2494 else if (til
== NULL
|| til
->empty())
2496 go_error_at(location
, "method has no receiver");
2499 else if (til
->size() > 1)
2501 go_error_at(location
, "method has multiple receivers");
2505 return &til
->front();
2508 // Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
2509 // Literal = BasicLit | CompositeLit | FunctionLit .
2510 // BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
2512 // If MAY_BE_SINK is true, this operand may be "_".
2514 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
2515 // if the entire expression is in parentheses.
2518 Parse::operand(bool may_be_sink
, bool* is_parenthesized
)
2520 const Token
* token
= this->peek_token();
2522 switch (token
->classification())
2524 case Token::TOKEN_IDENTIFIER
:
2526 Location location
= token
->location();
2527 std::string id
= token
->identifier();
2528 bool is_exported
= token
->is_identifier_exported();
2529 std::string packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2531 Named_object
* in_function
;
2532 Named_object
* named_object
= this->gogo_
->lookup(packed
, &in_function
);
2534 Package
* package
= NULL
;
2535 if (named_object
!= NULL
&& named_object
->is_package())
2537 if (!this->advance_token()->is_op(OPERATOR_DOT
)
2538 || !this->advance_token()->is_identifier())
2540 go_error_at(location
, "unexpected reference to package");
2541 return Expression::make_error(location
);
2543 package
= named_object
->package_value();
2544 package
->note_usage(id
);
2545 id
= this->peek_token()->identifier();
2546 is_exported
= this->peek_token()->is_identifier_exported();
2547 packed
= this->gogo_
->pack_hidden_name(id
, is_exported
);
2548 named_object
= package
->lookup(packed
);
2549 location
= this->location();
2550 go_assert(in_function
== NULL
);
2553 this->advance_token();
2555 if (named_object
!= NULL
2556 && named_object
->is_type()
2557 && !named_object
->type_value()->is_visible())
2559 go_assert(package
!= NULL
);
2560 go_error_at(location
, "invalid reference to hidden type %<%s.%s%>",
2561 Gogo::message_name(package
->package_name()).c_str(),
2562 Gogo::message_name(id
).c_str());
2563 return Expression::make_error(location
);
2567 if (named_object
== NULL
)
2569 if (package
!= NULL
)
2571 std::string n1
= Gogo::message_name(package
->package_name());
2572 std::string n2
= Gogo::message_name(id
);
2574 go_error_at(location
,
2575 ("invalid reference to unexported identifier "
2577 n1
.c_str(), n2
.c_str());
2579 go_error_at(location
,
2580 "reference to undefined identifier %<%s.%s%>",
2581 n1
.c_str(), n2
.c_str());
2582 return Expression::make_error(location
);
2585 named_object
= this->gogo_
->add_unknown_name(packed
, location
);
2588 if (in_function
!= NULL
2589 && in_function
!= this->gogo_
->current_function()
2590 && (named_object
->is_variable()
2591 || named_object
->is_result_variable()))
2592 return this->enclosing_var_reference(in_function
, named_object
,
2593 may_be_sink
, location
);
2595 switch (named_object
->classification())
2597 case Named_object::NAMED_OBJECT_CONST
:
2598 return Expression::make_const_reference(named_object
, location
);
2599 case Named_object::NAMED_OBJECT_TYPE
:
2600 return Expression::make_type(named_object
->type_value(), location
);
2601 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
2603 Type
* t
= Type::make_forward_declaration(named_object
);
2604 return Expression::make_type(t
, location
);
2606 case Named_object::NAMED_OBJECT_VAR
:
2607 case Named_object::NAMED_OBJECT_RESULT_VAR
:
2608 // Any left-hand-side can be a sink, so if this can not be
2609 // a sink, then it must be a use of the variable.
2611 this->mark_var_used(named_object
);
2612 return Expression::make_var_reference(named_object
, location
);
2613 case Named_object::NAMED_OBJECT_SINK
:
2615 return Expression::make_sink(location
);
2618 go_error_at(location
, "cannot use %<_%> as value");
2619 return Expression::make_error(location
);
2621 case Named_object::NAMED_OBJECT_FUNC
:
2622 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
2623 return Expression::make_func_reference(named_object
, NULL
,
2625 case Named_object::NAMED_OBJECT_UNKNOWN
:
2627 Unknown_expression
* ue
=
2628 Expression::make_unknown_reference(named_object
, location
);
2629 if (this->is_erroneous_function_
)
2630 ue
->set_no_error_message();
2633 case Named_object::NAMED_OBJECT_ERRONEOUS
:
2634 return Expression::make_error(location
);
2641 case Token::TOKEN_STRING
:
2642 ret
= Expression::make_string(token
->string_value(), token
->location());
2643 this->advance_token();
2646 case Token::TOKEN_CHARACTER
:
2647 ret
= Expression::make_character(token
->character_value(), NULL
,
2649 this->advance_token();
2652 case Token::TOKEN_INTEGER
:
2653 ret
= Expression::make_integer_z(token
->integer_value(), NULL
,
2655 this->advance_token();
2658 case Token::TOKEN_FLOAT
:
2659 ret
= Expression::make_float(token
->float_value(), NULL
,
2661 this->advance_token();
2664 case Token::TOKEN_IMAGINARY
:
2667 mpfr_init_set_ui(zero
, 0, MPFR_RNDN
);
2669 mpc_init2(val
, mpc_precision
);
2670 mpc_set_fr_fr(val
, zero
, *token
->imaginary_value(), MPC_RNDNN
);
2672 ret
= Expression::make_complex(&val
, NULL
, token
->location());
2674 this->advance_token();
2678 case Token::TOKEN_KEYWORD
:
2679 switch (token
->keyword())
2682 return this->function_lit();
2684 case KEYWORD_INTERFACE
:
2686 case KEYWORD_STRUCT
:
2688 Location location
= token
->location();
2689 return Expression::make_type(this->type(), location
);
2696 case Token::TOKEN_OPERATOR
:
2697 if (token
->is_op(OPERATOR_LPAREN
))
2699 this->advance_token();
2700 ret
= this->expression(PRECEDENCE_NORMAL
, may_be_sink
, true, NULL
,
2702 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
2703 go_error_at(this->location(), "missing %<)%>");
2705 this->advance_token();
2706 if (is_parenthesized
!= NULL
)
2707 *is_parenthesized
= true;
2710 else if (token
->is_op(OPERATOR_LSQUARE
))
2712 // Here we call array_type directly, as this is the only
2713 // case where an ellipsis is permitted for an array type.
2714 Location location
= token
->location();
2715 return Expression::make_type(this->array_type(true), location
);
2723 go_error_at(this->location(), "expected operand");
2724 return Expression::make_error(this->location());
2727 // Handle a reference to a variable in an enclosing function. We add
2728 // it to a list of such variables. We return a reference to a field
2729 // in a struct which will be passed on the static chain when calling
2730 // the current function.
2733 Parse::enclosing_var_reference(Named_object
* in_function
, Named_object
* var
,
2734 bool may_be_sink
, Location location
)
2736 go_assert(var
->is_variable() || var
->is_result_variable());
2738 // Any left-hand-side can be a sink, so if this can not be
2739 // a sink, then it must be a use of the variable.
2741 this->mark_var_used(var
);
2743 Named_object
* this_function
= this->gogo_
->current_function();
2744 Named_object
* closure
= this_function
->func_value()->closure_var();
2746 // The last argument to the Enclosing_var constructor is the index
2747 // of this variable in the closure. We add 1 to the current number
2748 // of enclosed variables, because the first field in the closure
2749 // points to the function code.
2750 Enclosing_var
ev(var
, in_function
, this->enclosing_vars_
.size() + 1);
2751 std::pair
<Enclosing_vars::iterator
, bool> ins
=
2752 this->enclosing_vars_
.insert(ev
);
2755 // This is a variable we have not seen before. Add a new field
2756 // to the closure type.
2757 this_function
->func_value()->add_closure_field(var
, location
);
2760 Expression
* closure_ref
= Expression::make_var_reference(closure
,
2763 Expression::make_dereference(closure_ref
,
2764 Expression::NIL_CHECK_NOT_NEEDED
,
2767 // The closure structure holds pointers to the variables, so we need
2768 // to introduce an indirection.
2769 Expression
* e
= Expression::make_field_reference(closure_ref
,
2772 e
= Expression::make_dereference(e
, Expression::NIL_CHECK_NOT_NEEDED
,
2774 return Expression::make_enclosing_var_reference(e
, var
, location
);
2777 // CompositeLit = LiteralType LiteralValue .
2778 // LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
2779 // SliceType | MapType | TypeName .
2780 // LiteralValue = "{" [ ElementList [ "," ] ] "}" .
2781 // ElementList = Element { "," Element } .
2782 // Element = [ Key ":" ] Value .
2783 // Key = FieldName | ElementIndex .
2784 // FieldName = identifier .
2785 // ElementIndex = Expression .
2786 // Value = Expression | LiteralValue .
2788 // We have already seen the type if there is one, and we are now
2789 // looking at the LiteralValue. The case "[" "..." "]" ElementType
2790 // will be seen here as an array type whose length is "nil". The
2791 // DEPTH parameter is non-zero if this is an embedded composite
2792 // literal and the type was omitted. It gives the number of steps up
2793 // to the type which was provided. E.g., in [][]int{{1}} it will be
2794 // 1. In [][][]int{{{1}}} it will be 2.
2797 Parse::composite_lit(Type
* type
, int depth
, Location location
)
2799 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY
));
2800 this->advance_token();
2802 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
2804 this->advance_token();
2805 return Expression::make_composite_literal(type
, depth
, false, NULL
,
2809 bool has_keys
= false;
2810 bool all_are_names
= true;
2811 Expression_list
* vals
= new Expression_list
;
2815 bool is_type_omitted
= false;
2816 bool is_name
= false;
2818 const Token
* token
= this->peek_token();
2820 if (token
->is_identifier())
2822 std::string identifier
= token
->identifier();
2823 bool is_exported
= token
->is_identifier_exported();
2824 Location id_location
= token
->location();
2826 if (this->advance_token()->is_op(OPERATOR_COLON
))
2828 // This may be a field name. We don't know for sure--it
2829 // could also be an expression for an array index. We
2830 // don't want to parse it as an expression because may
2831 // trigger various errors, e.g., if this identifier
2832 // happens to be the name of a package.
2833 Gogo
* gogo
= this->gogo_
;
2834 val
= this->id_to_expression(gogo
->pack_hidden_name(identifier
,
2836 id_location
, false, true);
2841 this->unget_token(Token::make_identifier_token(identifier
,
2844 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
2848 else if (!token
->is_op(OPERATOR_LCURLY
))
2849 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2852 // This must be a composite literal inside another composite
2853 // literal, with the type omitted for the inner one.
2854 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2855 is_type_omitted
= true;
2858 token
= this->peek_token();
2859 if (!token
->is_op(OPERATOR_COLON
))
2862 vals
->push_back(NULL
);
2867 if (is_type_omitted
)
2869 // VAL is a nested composite literal with an omitted type being
2870 // used a key. Record this information in VAL so that the correct
2871 // type is associated with the literal value if VAL is a
2873 val
->complit()->update_key_path(depth
);
2876 this->advance_token();
2878 if (!has_keys
&& !vals
->empty())
2880 Expression_list
* newvals
= new Expression_list
;
2881 for (Expression_list::const_iterator p
= vals
->begin();
2885 newvals
->push_back(NULL
);
2886 newvals
->push_back(*p
);
2893 vals
->push_back(val
);
2895 if (!token
->is_op(OPERATOR_LCURLY
))
2896 val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
2899 // This must be a composite literal inside another
2900 // composite literal, with the type omitted for the
2902 val
= this->composite_lit(type
, depth
+ 1, token
->location());
2905 token
= this->peek_token();
2908 vals
->push_back(val
);
2911 all_are_names
= false;
2913 if (token
->is_op(OPERATOR_COMMA
))
2915 if (this->advance_token()->is_op(OPERATOR_RCURLY
))
2917 this->advance_token();
2921 else if (token
->is_op(OPERATOR_RCURLY
))
2923 this->advance_token();
2928 if (token
->is_op(OPERATOR_SEMICOLON
))
2929 go_error_at(this->location(),
2930 ("need trailing comma before newline "
2931 "in composite literal"));
2933 go_error_at(this->location(), "expected %<,%> or %<}%>");
2935 this->gogo_
->mark_locals_used();
2937 while (!token
->is_eof()
2938 && (edepth
> 0 || !token
->is_op(OPERATOR_RCURLY
)))
2940 if (token
->is_op(OPERATOR_LCURLY
))
2942 else if (token
->is_op(OPERATOR_RCURLY
))
2944 token
= this->advance_token();
2946 if (token
->is_op(OPERATOR_RCURLY
))
2947 this->advance_token();
2949 return Expression::make_error(location
);
2953 return Expression::make_composite_literal(type
, depth
, has_keys
, vals
,
2954 all_are_names
, location
);
2957 // FunctionLit = "func" Signature Block .
2960 Parse::function_lit()
2962 Location location
= this->location();
2963 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC
));
2964 this->advance_token();
2966 Enclosing_vars hold_enclosing_vars
;
2967 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2969 Function_type
* type
= this->signature(NULL
, location
);
2970 bool fntype_is_error
= false;
2973 type
= Type::make_function_type(NULL
, NULL
, NULL
, location
);
2974 fntype_is_error
= true;
2977 // For a function literal, the next token must be a '{'. If we
2978 // don't see that, then we may have a type expression.
2979 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
2981 hold_enclosing_vars
.swap(this->enclosing_vars_
);
2982 return Expression::make_type(type
, location
);
2985 bool hold_is_erroneous_function
= this->is_erroneous_function_
;
2986 if (fntype_is_error
)
2987 this->is_erroneous_function_
= true;
2989 Bc_stack
* hold_break_stack
= this->break_stack_
;
2990 Bc_stack
* hold_continue_stack
= this->continue_stack_
;
2991 this->break_stack_
= NULL
;
2992 this->continue_stack_
= NULL
;
2994 Named_object
* no
= this->gogo_
->start_function("", type
, true, location
);
2996 Location end_loc
= this->block();
2998 this->gogo_
->finish_function(end_loc
);
3000 if (this->break_stack_
!= NULL
)
3001 delete this->break_stack_
;
3002 if (this->continue_stack_
!= NULL
)
3003 delete this->continue_stack_
;
3004 this->break_stack_
= hold_break_stack
;
3005 this->continue_stack_
= hold_continue_stack
;
3007 this->is_erroneous_function_
= hold_is_erroneous_function
;
3009 hold_enclosing_vars
.swap(this->enclosing_vars_
);
3011 Expression
* closure
= this->create_closure(no
, &hold_enclosing_vars
,
3014 return Expression::make_func_reference(no
, closure
, location
);
3017 // Create a closure for the nested function FUNCTION. This is based
3018 // on ENCLOSING_VARS, which is a list of all variables defined in
3019 // enclosing functions and referenced from FUNCTION. A closure is the
3020 // address of a struct which point to the real function code and
3021 // contains the addresses of all the referenced variables. This
3022 // returns NULL if no closure is required.
3025 Parse::create_closure(Named_object
* function
, Enclosing_vars
* enclosing_vars
,
3028 if (enclosing_vars
->empty())
3031 // Get the variables in order by their field index.
3033 size_t enclosing_var_count
= enclosing_vars
->size();
3034 std::vector
<Enclosing_var
> ev(enclosing_var_count
);
3035 for (Enclosing_vars::const_iterator p
= enclosing_vars
->begin();
3036 p
!= enclosing_vars
->end();
3039 // Subtract 1 because index 0 is the function code.
3040 ev
[p
->index() - 1] = *p
;
3043 // Build an initializer for a composite literal of the closure's
3046 Named_object
* enclosing_function
= this->gogo_
->current_function();
3047 Expression_list
* initializer
= new Expression_list
;
3049 initializer
->push_back(Expression::make_func_code_reference(function
,
3052 for (size_t i
= 0; i
< enclosing_var_count
; ++i
)
3054 // Add 1 to i because the first field in the closure is a
3055 // pointer to the function code.
3056 go_assert(ev
[i
].index() == i
+ 1);
3057 Named_object
* var
= ev
[i
].var();
3059 if (ev
[i
].in_function() == enclosing_function
)
3060 ref
= Expression::make_var_reference(var
, location
);
3062 ref
= this->enclosing_var_reference(ev
[i
].in_function(), var
,
3064 Expression
* refaddr
= Expression::make_unary(OPERATOR_AND
, ref
,
3066 initializer
->push_back(refaddr
);
3069 Named_object
* closure_var
= function
->func_value()->closure_var();
3070 Struct_type
* st
= closure_var
->var_value()->type()->deref()->struct_type();
3071 Expression
* cv
= Expression::make_struct_composite_literal(st
, initializer
,
3073 return Expression::make_heap_expression(cv
, location
);
3076 // PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } .
3078 // If MAY_BE_SINK is true, this expression may be "_".
3080 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3083 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3084 // guard (var := expr.("type") using the literal keyword "type").
3086 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3087 // if the entire expression is in parentheses.
3090 Parse::primary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3091 bool* is_type_switch
, bool* is_parenthesized
)
3093 Location start_loc
= this->location();
3094 bool operand_is_parenthesized
= false;
3095 bool whole_is_parenthesized
= false;
3097 Expression
* ret
= this->operand(may_be_sink
, &operand_is_parenthesized
);
3099 whole_is_parenthesized
= operand_is_parenthesized
;
3101 // An unknown name followed by a curly brace must be a composite
3102 // literal, and the unknown name must be a type.
3103 if (may_be_composite_lit
3104 && !operand_is_parenthesized
3105 && ret
->unknown_expression() != NULL
3106 && this->peek_token()->is_op(OPERATOR_LCURLY
))
3108 Named_object
* no
= ret
->unknown_expression()->named_object();
3109 Type
* type
= Type::make_forward_declaration(no
);
3110 ret
= Expression::make_type(type
, ret
->location());
3113 // We handle composite literals and type casts here, as it is the
3114 // easiest way to handle types which are in parentheses, as in
3116 if (ret
->is_type_expression())
3118 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
3120 whole_is_parenthesized
= false;
3121 if (!may_be_composite_lit
)
3123 Type
* t
= ret
->type();
3124 if (t
->named_type() != NULL
3125 || t
->forward_declaration_type() != NULL
)
3126 go_error_at(start_loc
,
3127 _("parentheses required around this composite "
3128 "literal to avoid parsing ambiguity"));
3130 else if (operand_is_parenthesized
)
3131 go_error_at(start_loc
,
3132 "cannot parenthesize type in composite literal");
3133 ret
= this->composite_lit(ret
->type(), 0, ret
->location());
3135 else if (this->peek_token()->is_op(OPERATOR_LPAREN
))
3137 whole_is_parenthesized
= false;
3138 Location loc
= this->location();
3139 this->advance_token();
3140 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true,
3142 if (this->peek_token()->is_op(OPERATOR_COMMA
))
3143 this->advance_token();
3144 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS
))
3146 go_error_at(this->location(),
3147 "invalid use of %<...%> in type conversion");
3148 this->advance_token();
3150 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3151 go_error_at(this->location(), "expected %<)%>");
3153 this->advance_token();
3154 if (expr
->is_error_expression())
3158 Type
* t
= ret
->type();
3159 if (t
->classification() == Type::TYPE_ARRAY
3160 && t
->array_type()->length() != NULL
3161 && t
->array_type()->length()->is_nil_expression())
3163 go_error_at(ret
->location(),
3164 "use of %<[...]%> outside of array literal");
3165 ret
= Expression::make_error(loc
);
3168 ret
= Expression::make_cast(t
, expr
, loc
);
3175 const Token
* token
= this->peek_token();
3176 if (token
->is_op(OPERATOR_LPAREN
))
3178 whole_is_parenthesized
= false;
3179 ret
= this->call(this->verify_not_sink(ret
));
3181 else if (token
->is_op(OPERATOR_DOT
))
3183 whole_is_parenthesized
= false;
3184 ret
= this->selector(this->verify_not_sink(ret
), is_type_switch
);
3185 if (is_type_switch
!= NULL
&& *is_type_switch
)
3188 else if (token
->is_op(OPERATOR_LSQUARE
))
3190 whole_is_parenthesized
= false;
3191 ret
= this->index(this->verify_not_sink(ret
));
3197 if (whole_is_parenthesized
&& is_parenthesized
!= NULL
)
3198 *is_parenthesized
= true;
3203 // Selector = "." identifier .
3204 // TypeGuard = "." "(" QualifiedIdent ")" .
3206 // Note that Operand can expand to QualifiedIdent, which contains a
3207 // ".". That is handled directly in operand when it sees a package
3210 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3211 // guard (var := expr.("type") using the literal keyword "type").
3214 Parse::selector(Expression
* left
, bool* is_type_switch
)
3216 go_assert(this->peek_token()->is_op(OPERATOR_DOT
));
3217 Location location
= this->location();
3219 const Token
* token
= this->advance_token();
3220 if (token
->is_identifier())
3222 // This could be a field in a struct, or a method in an
3223 // interface, or a method associated with a type. We can't know
3224 // which until we have seen all the types.
3226 this->gogo_
->pack_hidden_name(token
->identifier(),
3227 token
->is_identifier_exported());
3228 if (token
->identifier() == "_")
3230 go_error_at(this->location(), "invalid use of %<_%>");
3231 name
= Gogo::erroneous_name();
3233 this->advance_token();
3234 return Expression::make_selector(left
, name
, location
);
3236 else if (token
->is_op(OPERATOR_LPAREN
))
3238 this->advance_token();
3240 if (!this->peek_token()->is_keyword(KEYWORD_TYPE
))
3241 type
= this->type();
3244 if (is_type_switch
!= NULL
)
3245 *is_type_switch
= true;
3248 go_error_at(this->location(),
3249 "use of %<.(type)%> outside type switch");
3250 type
= Type::make_error_type();
3252 this->advance_token();
3254 if (!this->peek_token()->is_op(OPERATOR_RPAREN
))
3255 go_error_at(this->location(), "missing %<)%>");
3257 this->advance_token();
3258 if (is_type_switch
!= NULL
&& *is_type_switch
)
3260 return Expression::make_type_guard(left
, type
, location
);
3264 go_error_at(this->location(), "expected identifier or %<(%>");
3269 // Index = "[" Expression "]" .
3270 // Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" .
3273 Parse::index(Expression
* expr
)
3275 Location location
= this->location();
3276 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE
));
3277 this->advance_token();
3280 if (!this->peek_token()->is_op(OPERATOR_COLON
))
3281 start
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3283 start
= Expression::make_integer_ul(0, NULL
, location
);
3285 Expression
* end
= NULL
;
3286 if (this->peek_token()->is_op(OPERATOR_COLON
))
3288 // We use nil to indicate a missing high expression.
3289 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3290 end
= Expression::make_nil(this->location());
3291 else if (this->peek_token()->is_op(OPERATOR_COLON
))
3293 go_error_at(this->location(),
3294 "middle index required in 3-index slice");
3295 end
= Expression::make_error(this->location());
3298 end
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3301 Expression
* cap
= NULL
;
3302 if (this->peek_token()->is_op(OPERATOR_COLON
))
3304 if (this->advance_token()->is_op(OPERATOR_RSQUARE
))
3306 go_error_at(this->location(),
3307 "final index required in 3-index slice");
3308 cap
= Expression::make_error(this->location());
3311 cap
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
3313 if (!this->peek_token()->is_op(OPERATOR_RSQUARE
))
3314 go_error_at(this->location(), "missing %<]%>");
3316 this->advance_token();
3317 return Expression::make_index(expr
, start
, end
, cap
, location
);
3320 // Call = "(" [ ArgumentList [ "," ] ] ")" .
3321 // ArgumentList = ExpressionList [ "..." ] .
3324 Parse::call(Expression
* func
)
3326 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN
));
3327 Expression_list
* args
= NULL
;
3328 bool is_varargs
= false;
3329 const Token
* token
= this->advance_token();
3330 if (!token
->is_op(OPERATOR_RPAREN
))
3332 args
= this->expression_list(NULL
, false, true);
3333 token
= this->peek_token();
3334 if (token
->is_op(OPERATOR_ELLIPSIS
))
3337 token
= this->advance_token();
3340 if (token
->is_op(OPERATOR_COMMA
))
3341 token
= this->advance_token();
3342 if (!token
->is_op(OPERATOR_RPAREN
))
3344 go_error_at(this->location(), "missing %<)%>");
3345 if (!this->skip_past_error(OPERATOR_RPAREN
))
3346 return Expression::make_error(this->location());
3348 this->advance_token();
3349 if (func
->is_error_expression())
3351 return Expression::make_call(func
, args
, is_varargs
, func
->location());
3354 // Return an expression for a single unqualified identifier.
3357 Parse::id_to_expression(const std::string
& name
, Location location
,
3358 bool is_lhs
, bool is_composite_literal_key
)
3360 Named_object
* in_function
;
3361 Named_object
* named_object
= this->gogo_
->lookup(name
, &in_function
);
3362 if (named_object
== NULL
)
3364 if (is_composite_literal_key
)
3366 // This is a composite literal key, which means that it
3367 // could just be a struct field name, so avoid confusiong by
3368 // not adding it to the bindings. We'll look up the name
3369 // later during the lowering phase if necessary.
3370 return Expression::make_composite_literal_key(name
, location
);
3372 named_object
= this->gogo_
->add_unknown_name(name
, location
);
3375 if (in_function
!= NULL
3376 && in_function
!= this->gogo_
->current_function()
3377 && (named_object
->is_variable() || named_object
->is_result_variable()))
3378 return this->enclosing_var_reference(in_function
, named_object
, is_lhs
,
3381 switch (named_object
->classification())
3383 case Named_object::NAMED_OBJECT_CONST
:
3384 return Expression::make_const_reference(named_object
, location
);
3385 case Named_object::NAMED_OBJECT_VAR
:
3386 case Named_object::NAMED_OBJECT_RESULT_VAR
:
3388 this->mark_var_used(named_object
);
3389 return Expression::make_var_reference(named_object
, location
);
3390 case Named_object::NAMED_OBJECT_SINK
:
3391 return Expression::make_sink(location
);
3392 case Named_object::NAMED_OBJECT_FUNC
:
3393 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
3394 return Expression::make_func_reference(named_object
, NULL
, location
);
3395 case Named_object::NAMED_OBJECT_UNKNOWN
:
3397 Unknown_expression
* ue
=
3398 Expression::make_unknown_reference(named_object
, location
);
3399 if (this->is_erroneous_function_
)
3400 ue
->set_no_error_message();
3403 case Named_object::NAMED_OBJECT_PACKAGE
:
3404 case Named_object::NAMED_OBJECT_TYPE
:
3405 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
3407 // These cases can arise for a field name in a composite
3408 // literal. Keep track of these as they might be fake uses of
3409 // the related package.
3410 Unknown_expression
* ue
=
3411 Expression::make_unknown_reference(named_object
, location
);
3412 if (named_object
->package() != NULL
)
3413 named_object
->package()->note_fake_usage(ue
);
3414 if (this->is_erroneous_function_
)
3415 ue
->set_no_error_message();
3418 case Named_object::NAMED_OBJECT_ERRONEOUS
:
3419 return Expression::make_error(location
);
3421 go_error_at(this->location(), "unexpected type of identifier");
3422 return Expression::make_error(location
);
3426 // Expression = UnaryExpr { binary_op Expression } .
3428 // PRECEDENCE is the precedence of the current operator.
3430 // If MAY_BE_SINK is true, this expression may be "_".
3432 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3435 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3436 // guard (var := expr.("type") using the literal keyword "type").
3438 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3439 // if the entire expression is in parentheses.
3442 Parse::expression(Precedence precedence
, bool may_be_sink
,
3443 bool may_be_composite_lit
, bool* is_type_switch
,
3444 bool *is_parenthesized
)
3446 Expression
* left
= this->unary_expr(may_be_sink
, may_be_composite_lit
,
3447 is_type_switch
, is_parenthesized
);
3451 if (is_type_switch
!= NULL
&& *is_type_switch
)
3454 const Token
* token
= this->peek_token();
3455 if (token
->classification() != Token::TOKEN_OPERATOR
)
3461 Precedence right_precedence
;
3462 switch (token
->op())
3465 right_precedence
= PRECEDENCE_OROR
;
3467 case OPERATOR_ANDAND
:
3468 right_precedence
= PRECEDENCE_ANDAND
;
3471 case OPERATOR_NOTEQ
:
3476 right_precedence
= PRECEDENCE_RELOP
;
3479 case OPERATOR_MINUS
:
3482 right_precedence
= PRECEDENCE_ADDOP
;
3487 case OPERATOR_LSHIFT
:
3488 case OPERATOR_RSHIFT
:
3490 case OPERATOR_BITCLEAR
:
3491 right_precedence
= PRECEDENCE_MULOP
;
3494 right_precedence
= PRECEDENCE_INVALID
;
3498 if (right_precedence
== PRECEDENCE_INVALID
)
3504 if (is_parenthesized
!= NULL
)
3505 *is_parenthesized
= false;
3507 Operator op
= token
->op();
3508 Location binop_location
= token
->location();
3510 if (precedence
>= right_precedence
)
3512 // We've already seen A * B, and we see + C. We want to
3513 // return so that A * B becomes a group.
3517 this->advance_token();
3519 left
= this->verify_not_sink(left
);
3520 Expression
* right
= this->expression(right_precedence
, false,
3521 may_be_composite_lit
,
3523 left
= Expression::make_binary(op
, left
, right
, binop_location
);
3528 Parse::expression_may_start_here()
3530 const Token
* token
= this->peek_token();
3531 switch (token
->classification())
3533 case Token::TOKEN_INVALID
:
3534 case Token::TOKEN_EOF
:
3536 case Token::TOKEN_KEYWORD
:
3537 switch (token
->keyword())
3542 case KEYWORD_STRUCT
:
3543 case KEYWORD_INTERFACE
:
3548 case Token::TOKEN_IDENTIFIER
:
3550 case Token::TOKEN_STRING
:
3552 case Token::TOKEN_OPERATOR
:
3553 switch (token
->op())
3556 case OPERATOR_MINUS
:
3560 case OPERATOR_CHANOP
:
3562 case OPERATOR_LPAREN
:
3563 case OPERATOR_LSQUARE
:
3568 case Token::TOKEN_CHARACTER
:
3569 case Token::TOKEN_INTEGER
:
3570 case Token::TOKEN_FLOAT
:
3571 case Token::TOKEN_IMAGINARY
:
3578 // UnaryExpr = unary_op UnaryExpr | PrimaryExpr .
3580 // If MAY_BE_SINK is true, this expression may be "_".
3582 // If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite
3585 // If IS_TYPE_SWITCH is not NULL, this will recognize a type switch
3586 // guard (var := expr.("type") using the literal keyword "type").
3588 // If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true
3589 // if the entire expression is in parentheses.
3592 Parse::unary_expr(bool may_be_sink
, bool may_be_composite_lit
,
3593 bool* is_type_switch
, bool* is_parenthesized
)
3595 const Token
* token
= this->peek_token();
3597 // There is a complex parse for <- chan. The choices are
3598 // Convert x to type <- chan int:
3600 // Receive from (x converted to type chan <- chan int):
3601 // (<- chan <- chan int (x))
3602 // Convert x to type <- chan (<- chan int).
3603 // (<- chan <- chan int)(x)
3604 if (token
->is_op(OPERATOR_CHANOP
))
3606 Location location
= token
->location();
3607 if (this->advance_token()->is_keyword(KEYWORD_CHAN
))
3609 Expression
* expr
= this->primary_expr(false, may_be_composite_lit
,
3611 if (expr
->is_error_expression())
3613 else if (!expr
->is_type_expression())
3614 return Expression::make_receive(expr
, location
);
3617 if (expr
->type()->is_error_type())
3620 // We picked up "chan TYPE", but it is not a type
3622 Channel_type
* ct
= expr
->type()->channel_type();
3625 // This is probably impossible.
3626 go_error_at(location
, "expected channel type");
3627 return Expression::make_error(location
);
3629 else if (ct
->may_receive())
3632 Type
* t
= Type::make_channel_type(false, true,
3633 ct
->element_type());
3634 return Expression::make_type(t
, location
);
3638 // <- chan <- TYPE. Because we skipped the leading
3639 // <-, we parsed this as chan <- TYPE. With the
3640 // leading <-, we parse it as <- chan (<- TYPE).
3641 Type
*t
= this->reassociate_chan_direction(ct
, location
);
3642 return Expression::make_type(t
, location
);
3647 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP
, location
));
3648 token
= this->peek_token();
3651 if (token
->is_op(OPERATOR_PLUS
)
3652 || token
->is_op(OPERATOR_MINUS
)
3653 || token
->is_op(OPERATOR_NOT
)
3654 || token
->is_op(OPERATOR_XOR
)
3655 || token
->is_op(OPERATOR_CHANOP
)
3656 || token
->is_op(OPERATOR_MULT
)
3657 || token
->is_op(OPERATOR_AND
))
3659 Location location
= token
->location();
3660 Operator op
= token
->op();
3661 this->advance_token();
3663 Expression
* expr
= this->unary_expr(false, may_be_composite_lit
, NULL
,
3665 if (expr
->is_error_expression())
3667 else if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3668 expr
= Expression::make_type(Type::make_pointer_type(expr
->type()),
3670 else if (op
== OPERATOR_AND
&& expr
->is_composite_literal())
3671 expr
= Expression::make_heap_expression(expr
, location
);
3672 else if (op
!= OPERATOR_CHANOP
)
3673 expr
= Expression::make_unary(op
, expr
, location
);
3675 expr
= Expression::make_receive(expr
, location
);
3679 return this->primary_expr(may_be_sink
, may_be_composite_lit
,
3680 is_type_switch
, is_parenthesized
);
3683 // This is called for the obscure case of
3684 // (<- chan <- chan int)(x)
3685 // In unary_expr we remove the leading <- and parse the remainder,
3687 // chan <- (chan int)
3688 // When we add the leading <- back in, we really want
3689 // <- chan (<- chan int)
3690 // This means that we need to reassociate.
3693 Parse::reassociate_chan_direction(Channel_type
*ct
, Location location
)
3695 Channel_type
* ele
= ct
->element_type()->channel_type();
3698 go_error_at(location
, "parse error");
3699 return Type::make_error_type();
3702 if (ele
->may_send())
3703 sub
= Type::make_channel_type(false, true, ele
->element_type());
3705 sub
= this->reassociate_chan_direction(ele
, location
);
3706 return Type::make_channel_type(false, true, sub
);
3710 // Declaration | LabeledStmt | SimpleStmt |
3711 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3712 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3715 // LABEL is the label of this statement if it has one.
3718 Parse::statement(Label
* label
)
3720 const Token
* token
= this->peek_token();
3721 switch (token
->classification())
3723 case Token::TOKEN_KEYWORD
:
3725 switch (token
->keyword())
3730 this->declaration();
3734 case KEYWORD_STRUCT
:
3735 case KEYWORD_INTERFACE
:
3736 this->simple_stat(true, NULL
, NULL
, NULL
);
3740 this->go_or_defer_stat();
3742 case KEYWORD_RETURN
:
3743 this->return_stat();
3748 case KEYWORD_CONTINUE
:
3749 this->continue_stat();
3757 case KEYWORD_SWITCH
:
3758 this->switch_stat(label
);
3760 case KEYWORD_SELECT
:
3761 this->select_stat(label
);
3764 this->for_stat(label
);
3767 go_error_at(this->location(), "expected statement");
3768 this->advance_token();
3774 case Token::TOKEN_IDENTIFIER
:
3776 std::string identifier
= token
->identifier();
3777 bool is_exported
= token
->is_identifier_exported();
3778 Location location
= token
->location();
3779 if (this->advance_token()->is_op(OPERATOR_COLON
))
3781 this->advance_token();
3782 this->labeled_stmt(identifier
, location
);
3786 this->unget_token(Token::make_identifier_token(identifier
,
3789 this->simple_stat(true, NULL
, NULL
, NULL
);
3794 case Token::TOKEN_OPERATOR
:
3795 if (token
->is_op(OPERATOR_LCURLY
))
3797 Location location
= token
->location();
3798 this->gogo_
->start_block(location
);
3799 Location end_loc
= this->block();
3800 this->gogo_
->add_block(this->gogo_
->finish_block(end_loc
),
3803 else if (!token
->is_op(OPERATOR_SEMICOLON
))
3804 this->simple_stat(true, NULL
, NULL
, NULL
);
3807 case Token::TOKEN_STRING
:
3808 case Token::TOKEN_CHARACTER
:
3809 case Token::TOKEN_INTEGER
:
3810 case Token::TOKEN_FLOAT
:
3811 case Token::TOKEN_IMAGINARY
:
3812 this->simple_stat(true, NULL
, NULL
, NULL
);
3816 go_error_at(this->location(), "expected statement");
3817 this->advance_token();
3823 Parse::statement_may_start_here()
3825 const Token
* token
= this->peek_token();
3826 switch (token
->classification())
3828 case Token::TOKEN_KEYWORD
:
3830 switch (token
->keyword())
3837 case KEYWORD_STRUCT
:
3838 case KEYWORD_INTERFACE
:
3841 case KEYWORD_RETURN
:
3843 case KEYWORD_CONTINUE
:
3846 case KEYWORD_SWITCH
:
3847 case KEYWORD_SELECT
:
3857 case Token::TOKEN_IDENTIFIER
:
3860 case Token::TOKEN_OPERATOR
:
3861 if (token
->is_op(OPERATOR_LCURLY
)
3862 || token
->is_op(OPERATOR_SEMICOLON
))
3865 return this->expression_may_start_here();
3867 case Token::TOKEN_STRING
:
3868 case Token::TOKEN_CHARACTER
:
3869 case Token::TOKEN_INTEGER
:
3870 case Token::TOKEN_FLOAT
:
3871 case Token::TOKEN_IMAGINARY
:
3879 // LabeledStmt = Label ":" Statement .
3880 // Label = identifier .
3883 Parse::labeled_stmt(const std::string
& label_name
, Location location
)
3885 Label
* label
= this->gogo_
->add_label_definition(label_name
, location
);
3887 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
3889 // This is a label at the end of a block. A program is
3890 // permitted to omit a semicolon here.
3894 if (!this->statement_may_start_here())
3896 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
3898 // We don't treat the fallthrough keyword as a statement,
3899 // because it can't appear most places where a statement is
3900 // permitted, but it may have a label. We introduce a
3901 // semicolon because the caller expects to see a statement.
3902 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3907 // Mark the label as used to avoid a useless error about an
3910 label
->set_is_used();
3912 go_error_at(location
, "missing statement after label");
3913 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
3918 this->statement(label
);
3921 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt |
3922 // Assignment | ShortVarDecl .
3924 // EmptyStmt was handled in Parse::statement.
3926 // In order to make this work for if and switch statements, if
3927 // RETURN_EXP is not NULL, and we see an ExpressionStat, we return the
3928 // expression rather than adding an expression statement to the
3929 // current block. If we see something other than an ExpressionStat,
3930 // we add the statement, set *RETURN_EXP to true if we saw a send
3931 // statement, and return NULL. The handling of send statements is for
3932 // better error messages.
3934 // If P_RANGE_CLAUSE is not NULL, then this will recognize a
3937 // If P_TYPE_SWITCH is not NULL, this will recognize a type switch
3938 // guard (var := expr.("type") using the literal keyword "type").
3941 Parse::simple_stat(bool may_be_composite_lit
, bool* return_exp
,
3942 Range_clause
* p_range_clause
, Type_switch
* p_type_switch
)
3944 const Token
* token
= this->peek_token();
3946 // An identifier follow by := is a SimpleVarDecl.
3947 if (token
->is_identifier())
3949 std::string identifier
= token
->identifier();
3950 bool is_exported
= token
->is_identifier_exported();
3951 Location location
= token
->location();
3953 token
= this->advance_token();
3954 if (token
->is_op(OPERATOR_COLONEQ
)
3955 || token
->is_op(OPERATOR_COMMA
))
3957 identifier
= this->gogo_
->pack_hidden_name(identifier
, is_exported
);
3958 this->simple_var_decl_or_assignment(identifier
, location
,
3959 may_be_composite_lit
,
3961 (token
->is_op(OPERATOR_COLONEQ
)
3967 this->unget_token(Token::make_identifier_token(identifier
, is_exported
,
3970 else if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
3972 Typed_identifier_list til
;
3973 this->range_clause_decl(&til
, p_range_clause
);
3977 Expression
* exp
= this->expression(PRECEDENCE_NORMAL
, true,
3978 may_be_composite_lit
,
3979 (p_type_switch
== NULL
3981 : &p_type_switch
->found
),
3983 if (p_type_switch
!= NULL
&& p_type_switch
->found
)
3985 p_type_switch
->name
.clear();
3986 p_type_switch
->location
= exp
->location();
3987 p_type_switch
->expr
= this->verify_not_sink(exp
);
3990 token
= this->peek_token();
3991 if (token
->is_op(OPERATOR_CHANOP
))
3993 this->send_stmt(this->verify_not_sink(exp
), may_be_composite_lit
);
3994 if (return_exp
!= NULL
)
3997 else if (token
->is_op(OPERATOR_PLUSPLUS
)
3998 || token
->is_op(OPERATOR_MINUSMINUS
))
3999 this->inc_dec_stat(this->verify_not_sink(exp
));
4000 else if (token
->is_op(OPERATOR_COMMA
)
4001 || token
->is_op(OPERATOR_EQ
))
4002 this->assignment(exp
, may_be_composite_lit
, p_range_clause
);
4003 else if (token
->is_op(OPERATOR_PLUSEQ
)
4004 || token
->is_op(OPERATOR_MINUSEQ
)
4005 || token
->is_op(OPERATOR_OREQ
)
4006 || token
->is_op(OPERATOR_XOREQ
)
4007 || token
->is_op(OPERATOR_MULTEQ
)
4008 || token
->is_op(OPERATOR_DIVEQ
)
4009 || token
->is_op(OPERATOR_MODEQ
)
4010 || token
->is_op(OPERATOR_LSHIFTEQ
)
4011 || token
->is_op(OPERATOR_RSHIFTEQ
)
4012 || token
->is_op(OPERATOR_ANDEQ
)
4013 || token
->is_op(OPERATOR_BITCLEAREQ
))
4014 this->assignment(this->verify_not_sink(exp
), may_be_composite_lit
,
4016 else if (return_exp
!= NULL
)
4017 return this->verify_not_sink(exp
);
4020 exp
= this->verify_not_sink(exp
);
4022 if (token
->is_op(OPERATOR_COLONEQ
))
4024 if (!exp
->is_error_expression())
4025 go_error_at(token
->location(), "non-name on left side of %<:=%>");
4026 this->gogo_
->mark_locals_used();
4027 while (!token
->is_op(OPERATOR_SEMICOLON
)
4028 && !token
->is_eof())
4029 token
= this->advance_token();
4033 this->expression_stat(exp
);
4040 Parse::simple_stat_may_start_here()
4042 return this->expression_may_start_here();
4045 // Parse { Statement ";" } which is used in a few places. The list of
4046 // statements may end with a right curly brace, in which case the
4047 // semicolon may be omitted.
4050 Parse::statement_list()
4052 while (this->statement_may_start_here())
4054 this->statement(NULL
);
4055 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4056 this->advance_token();
4057 else if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4061 if (!this->peek_token()->is_eof() || !saw_errors())
4062 go_error_at(this->location(), "expected %<;%> or %<}%> or newline");
4063 if (!this->skip_past_error(OPERATOR_RCURLY
))
4070 Parse::statement_list_may_start_here()
4072 return this->statement_may_start_here();
4075 // ExpressionStat = Expression .
4078 Parse::expression_stat(Expression
* exp
)
4080 this->gogo_
->add_statement(Statement::make_statement(exp
, false));
4083 // SendStmt = Channel "<-" Expression .
4084 // Channel = Expression .
4087 Parse::send_stmt(Expression
* channel
, bool may_be_composite_lit
)
4089 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP
));
4090 Location loc
= this->location();
4091 this->advance_token();
4092 Expression
* val
= this->expression(PRECEDENCE_NORMAL
, false,
4093 may_be_composite_lit
, NULL
, NULL
);
4094 Statement
* s
= Statement::make_send_statement(channel
, val
, loc
);
4095 this->gogo_
->add_statement(s
);
4098 // IncDecStat = Expression ( "++" | "--" ) .
4101 Parse::inc_dec_stat(Expression
* exp
)
4103 const Token
* token
= this->peek_token();
4104 if (token
->is_op(OPERATOR_PLUSPLUS
))
4105 this->gogo_
->add_statement(Statement::make_inc_statement(exp
));
4106 else if (token
->is_op(OPERATOR_MINUSMINUS
))
4107 this->gogo_
->add_statement(Statement::make_dec_statement(exp
));
4110 this->advance_token();
4113 // Assignment = ExpressionList assign_op ExpressionList .
4115 // EXP is an expression that we have already parsed.
4117 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4118 // side may be a composite literal.
4120 // If RANGE_CLAUSE is not NULL, then this will recognize a
4124 Parse::assignment(Expression
* expr
, bool may_be_composite_lit
,
4125 Range_clause
* p_range_clause
)
4127 Expression_list
* vars
;
4128 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4130 vars
= new Expression_list();
4131 vars
->push_back(expr
);
4135 this->advance_token();
4136 vars
= this->expression_list(expr
, true, may_be_composite_lit
);
4139 this->tuple_assignment(vars
, may_be_composite_lit
, p_range_clause
);
4142 // An assignment statement. LHS is the list of expressions which
4143 // appear on the left hand side.
4145 // If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
4146 // side may be a composite literal.
4148 // If RANGE_CLAUSE is not NULL, then this will recognize a
4152 Parse::tuple_assignment(Expression_list
* lhs
, bool may_be_composite_lit
,
4153 Range_clause
* p_range_clause
)
4155 const Token
* token
= this->peek_token();
4156 if (!token
->is_op(OPERATOR_EQ
)
4157 && !token
->is_op(OPERATOR_PLUSEQ
)
4158 && !token
->is_op(OPERATOR_MINUSEQ
)
4159 && !token
->is_op(OPERATOR_OREQ
)
4160 && !token
->is_op(OPERATOR_XOREQ
)
4161 && !token
->is_op(OPERATOR_MULTEQ
)
4162 && !token
->is_op(OPERATOR_DIVEQ
)
4163 && !token
->is_op(OPERATOR_MODEQ
)
4164 && !token
->is_op(OPERATOR_LSHIFTEQ
)
4165 && !token
->is_op(OPERATOR_RSHIFTEQ
)
4166 && !token
->is_op(OPERATOR_ANDEQ
)
4167 && !token
->is_op(OPERATOR_BITCLEAREQ
))
4169 go_error_at(this->location(), "expected assignment operator");
4172 Operator op
= token
->op();
4173 Location location
= token
->location();
4175 token
= this->advance_token();
4180 if (p_range_clause
!= NULL
&& token
->is_keyword(KEYWORD_RANGE
))
4182 if (op
!= OPERATOR_EQ
)
4183 go_error_at(this->location(), "range clause requires %<=%>");
4184 this->range_clause_expr(lhs
, p_range_clause
);
4188 Expression_list
* vals
= this->expression_list(NULL
, false,
4189 may_be_composite_lit
);
4191 // We've parsed everything; check for errors.
4194 for (Expression_list::const_iterator pe
= lhs
->begin();
4198 if ((*pe
)->is_error_expression())
4200 if (op
!= OPERATOR_EQ
&& (*pe
)->is_sink_expression())
4201 go_error_at((*pe
)->location(), "cannot use %<_%> as value");
4203 for (Expression_list::const_iterator pe
= vals
->begin();
4207 if ((*pe
)->is_error_expression())
4211 Call_expression
* call
;
4212 Index_expression
* map_index
;
4213 Receive_expression
* receive
;
4214 Type_guard_expression
* type_guard
;
4215 if (lhs
->size() == vals
->size())
4218 if (lhs
->size() > 1)
4220 if (op
!= OPERATOR_EQ
)
4221 go_error_at(location
, "multiple values only permitted with %<=%>");
4222 s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4226 if (op
== OPERATOR_EQ
)
4227 s
= Statement::make_assignment(lhs
->front(), vals
->front(),
4230 s
= Statement::make_assignment_operation(op
, lhs
->front(),
4231 vals
->front(), location
);
4235 this->gogo_
->add_statement(s
);
4237 else if (vals
->size() == 1
4238 && (call
= (*vals
->begin())->call_expression()) != NULL
)
4240 if (op
!= OPERATOR_EQ
)
4241 go_error_at(location
, "multiple results only permitted with %<=%>");
4242 call
->set_expected_result_count(lhs
->size());
4244 vals
= new Expression_list
;
4245 for (unsigned int i
= 0; i
< lhs
->size(); ++i
)
4246 vals
->push_back(Expression::make_call_result(call
, i
));
4247 Statement
* s
= Statement::make_tuple_assignment(lhs
, vals
, location
);
4248 this->gogo_
->add_statement(s
);
4250 else if (lhs
->size() == 2
4251 && vals
->size() == 1
4252 && (map_index
= (*vals
->begin())->index_expression()) != NULL
)
4254 if (op
!= OPERATOR_EQ
)
4255 go_error_at(location
, "two values from map requires %<=%>");
4256 Expression
* val
= lhs
->front();
4257 Expression
* present
= lhs
->back();
4258 Statement
* s
= Statement::make_tuple_map_assignment(val
, present
,
4259 map_index
, location
);
4260 this->gogo_
->add_statement(s
);
4262 else if (lhs
->size() == 2
4263 && vals
->size() == 1
4264 && (receive
= (*vals
->begin())->receive_expression()) != NULL
)
4266 if (op
!= OPERATOR_EQ
)
4267 go_error_at(location
, "two values from receive requires %<=%>");
4268 Expression
* val
= lhs
->front();
4269 Expression
* success
= lhs
->back();
4270 Expression
* channel
= receive
->channel();
4271 Statement
* s
= Statement::make_tuple_receive_assignment(val
, success
,
4274 this->gogo_
->add_statement(s
);
4276 else if (lhs
->size() == 2
4277 && vals
->size() == 1
4278 && (type_guard
= (*vals
->begin())->type_guard_expression()) != NULL
)
4280 if (op
!= OPERATOR_EQ
)
4281 go_error_at(location
, "two values from type guard requires %<=%>");
4282 Expression
* val
= lhs
->front();
4283 Expression
* ok
= lhs
->back();
4284 Expression
* expr
= type_guard
->expr();
4285 Type
* type
= type_guard
->type();
4286 Statement
* s
= Statement::make_tuple_type_guard_assignment(val
, ok
,
4289 this->gogo_
->add_statement(s
);
4293 go_error_at(location
, ("number of variables does not "
4294 "match number of values"));
4298 // GoStat = "go" Expression .
4299 // DeferStat = "defer" Expression .
4302 Parse::go_or_defer_stat()
4304 go_assert(this->peek_token()->is_keyword(KEYWORD_GO
)
4305 || this->peek_token()->is_keyword(KEYWORD_DEFER
));
4306 bool is_go
= this->peek_token()->is_keyword(KEYWORD_GO
);
4307 Location stat_location
= this->location();
4309 this->advance_token();
4310 Location expr_location
= this->location();
4312 bool is_parenthesized
= false;
4313 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
,
4315 Call_expression
* call_expr
= expr
->call_expression();
4316 if (is_parenthesized
|| call_expr
== NULL
)
4318 go_error_at(expr_location
, "argument to go/defer must be function call");
4322 // Make it easier to simplify go/defer statements by putting every
4323 // statement in its own block.
4324 this->gogo_
->start_block(stat_location
);
4328 stat
= Statement::make_go_statement(call_expr
, stat_location
);
4329 call_expr
->set_is_concurrent();
4333 stat
= Statement::make_defer_statement(call_expr
, stat_location
);
4334 call_expr
->set_is_deferred();
4336 this->gogo_
->add_statement(stat
);
4337 this->gogo_
->add_block(this->gogo_
->finish_block(stat_location
),
4341 // ReturnStat = "return" [ ExpressionList ] .
4344 Parse::return_stat()
4346 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN
));
4347 Location location
= this->location();
4348 this->advance_token();
4349 Expression_list
* vals
= NULL
;
4350 if (this->expression_may_start_here())
4351 vals
= this->expression_list(NULL
, false, true);
4352 this->gogo_
->add_statement(Statement::make_return_statement(vals
, location
));
4355 && this->gogo_
->current_function()->func_value()->results_are_named())
4357 Named_object
* function
= this->gogo_
->current_function();
4358 Function::Results
* results
= function
->func_value()->result_variables();
4359 for (Function::Results::const_iterator p
= results
->begin();
4360 p
!= results
->end();
4363 Named_object
* no
= this->gogo_
->lookup((*p
)->name(), NULL
);
4365 go_assert(saw_errors());
4366 else if (!no
->is_result_variable())
4367 go_error_at(location
, "%qs is shadowed during return",
4368 (*p
)->message_name().c_str());
4373 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block
4374 // [ "else" ( IfStmt | Block ) ] .
4379 go_assert(this->peek_token()->is_keyword(KEYWORD_IF
));
4380 Location location
= this->location();
4381 this->advance_token();
4383 this->gogo_
->start_block(location
);
4385 bool saw_simple_stat
= false;
4386 Expression
* cond
= NULL
;
4387 bool saw_send_stmt
= false;
4388 if (this->simple_stat_may_start_here())
4390 cond
= this->simple_stat(false, &saw_send_stmt
, NULL
, NULL
);
4391 saw_simple_stat
= true;
4393 if (cond
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4395 // The SimpleStat is an expression statement.
4396 this->expression_stat(cond
);
4401 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4402 this->advance_token();
4403 else if (saw_simple_stat
)
4406 go_error_at(this->location(),
4407 ("send statement used as value; "
4408 "use select for non-blocking send"));
4410 go_error_at(this->location(),
4411 "expected %<;%> after statement in if expression");
4412 if (!this->expression_may_start_here())
4413 cond
= Expression::make_error(this->location());
4415 if (cond
== NULL
&& this->peek_token()->is_op(OPERATOR_LCURLY
))
4417 go_error_at(this->location(),
4418 "missing condition in if statement");
4419 cond
= Expression::make_error(this->location());
4422 cond
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
, NULL
);
4425 // Check for the easy error of a newline before starting the block.
4426 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4428 Location semi_loc
= this->location();
4429 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
4430 go_error_at(semi_loc
, "unexpected semicolon or newline, expecting %<{%> after if clause");
4431 // Otherwise we will get an error when we call this->block
4435 this->gogo_
->start_block(this->location());
4436 Location end_loc
= this->block();
4437 Block
* then_block
= this->gogo_
->finish_block(end_loc
);
4439 // Check for the easy error of a newline before "else".
4440 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4442 Location semi_loc
= this->location();
4443 if (this->advance_token()->is_keyword(KEYWORD_ELSE
))
4444 go_error_at(this->location(),
4445 "unexpected semicolon or newline before %<else%>");
4447 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON
,
4451 Block
* else_block
= NULL
;
4452 if (this->peek_token()->is_keyword(KEYWORD_ELSE
))
4454 this->gogo_
->start_block(this->location());
4455 const Token
* token
= this->advance_token();
4456 if (token
->is_keyword(KEYWORD_IF
))
4458 else if (token
->is_op(OPERATOR_LCURLY
))
4462 go_error_at(this->location(), "expected %<if%> or %<{%>");
4463 this->statement(NULL
);
4465 else_block
= this->gogo_
->finish_block(this->location());
4468 this->gogo_
->add_statement(Statement::make_if_statement(cond
, then_block
,
4472 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4476 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
4477 // ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ]
4478 // "{" { ExprCaseClause } "}" .
4479 // TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard
4480 // "{" { TypeCaseClause } "}" .
4481 // TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
4484 Parse::switch_stat(Label
* label
)
4486 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH
));
4487 Location location
= this->location();
4488 this->advance_token();
4490 this->gogo_
->start_block(location
);
4492 bool saw_simple_stat
= false;
4493 Expression
* switch_val
= NULL
;
4494 bool saw_send_stmt
= false;
4495 Type_switch type_switch
;
4496 bool have_type_switch_block
= false;
4497 if (this->simple_stat_may_start_here())
4499 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4501 saw_simple_stat
= true;
4503 if (switch_val
!= NULL
&& this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4505 // The SimpleStat is an expression statement.
4506 this->expression_stat(switch_val
);
4509 if (switch_val
== NULL
&& !type_switch
.found
)
4511 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4512 this->advance_token();
4513 else if (saw_simple_stat
)
4516 go_error_at(this->location(),
4517 ("send statement used as value; "
4518 "use select for non-blocking send"));
4520 go_error_at(this->location(),
4521 "expected %<;%> after statement in switch expression");
4523 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4525 if (this->peek_token()->is_identifier())
4527 const Token
* token
= this->peek_token();
4528 std::string identifier
= token
->identifier();
4529 bool is_exported
= token
->is_identifier_exported();
4530 Location id_loc
= token
->location();
4532 token
= this->advance_token();
4533 bool is_coloneq
= token
->is_op(OPERATOR_COLONEQ
);
4534 this->unget_token(Token::make_identifier_token(identifier
,
4539 // This must be a TypeSwitchGuard. It is in a
4540 // different block from any initial SimpleStat.
4541 if (saw_simple_stat
)
4543 this->gogo_
->start_block(id_loc
);
4544 have_type_switch_block
= true;
4547 switch_val
= this->simple_stat(false, &saw_send_stmt
, NULL
,
4549 if (!type_switch
.found
)
4551 if (switch_val
== NULL
4552 || !switch_val
->is_error_expression())
4555 "expected type switch assignment");
4556 switch_val
= Expression::make_error(id_loc
);
4561 if (switch_val
== NULL
&& !type_switch
.found
)
4563 switch_val
= this->expression(PRECEDENCE_NORMAL
, false, false,
4564 &type_switch
.found
, NULL
);
4565 if (type_switch
.found
)
4567 type_switch
.name
.clear();
4568 type_switch
.expr
= switch_val
;
4569 type_switch
.location
= switch_val
->location();
4575 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4577 Location token_loc
= this->location();
4578 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
)
4579 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4580 go_error_at(token_loc
, "missing %<{%> after switch clause");
4581 else if (this->peek_token()->is_op(OPERATOR_COLONEQ
))
4583 go_error_at(token_loc
, "invalid variable name");
4584 this->advance_token();
4585 this->expression(PRECEDENCE_NORMAL
, false, false,
4586 &type_switch
.found
, NULL
);
4587 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
4588 this->advance_token();
4589 if (!this->peek_token()->is_op(OPERATOR_LCURLY
))
4591 if (have_type_switch_block
)
4592 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4594 this->gogo_
->add_block(this->gogo_
->finish_block(location
),
4598 if (type_switch
.found
)
4599 type_switch
.expr
= Expression::make_error(location
);
4603 go_error_at(this->location(), "expected %<{%>");
4604 if (have_type_switch_block
)
4605 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4607 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4612 this->advance_token();
4614 Statement
* statement
;
4615 if (type_switch
.found
)
4616 statement
= this->type_switch_body(label
, type_switch
, location
);
4618 statement
= this->expr_switch_body(label
, switch_val
, location
);
4620 if (statement
!= NULL
)
4621 this->gogo_
->add_statement(statement
);
4623 if (have_type_switch_block
)
4624 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4627 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
4631 // The body of an expression switch.
4632 // "{" { ExprCaseClause } "}"
4635 Parse::expr_switch_body(Label
* label
, Expression
* switch_val
,
4638 Switch_statement
* statement
= Statement::make_switch_statement(switch_val
,
4641 this->push_break_statement(statement
, label
);
4643 Case_clauses
* case_clauses
= new Case_clauses();
4644 bool saw_default
= false;
4645 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4647 if (this->peek_token()->is_eof())
4650 go_error_at(this->location(), "missing %<}%>");
4653 this->expr_case_clause(case_clauses
, &saw_default
);
4655 this->advance_token();
4657 statement
->add_clauses(case_clauses
);
4659 this->pop_break_statement();
4664 // ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
4665 // FallthroughStat = "fallthrough" .
4668 Parse::expr_case_clause(Case_clauses
* clauses
, bool* saw_default
)
4670 Location location
= this->location();
4672 bool is_default
= false;
4673 Expression_list
* vals
= this->expr_switch_case(&is_default
);
4675 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4678 go_error_at(this->location(), "expected %<:%>");
4682 this->advance_token();
4684 Block
* statements
= NULL
;
4685 if (this->statement_list_may_start_here())
4687 this->gogo_
->start_block(this->location());
4688 this->statement_list();
4689 statements
= this->gogo_
->finish_block(this->location());
4692 bool is_fallthrough
= false;
4693 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4695 Location fallthrough_loc
= this->location();
4696 is_fallthrough
= true;
4697 while (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4699 if (this->peek_token()->is_op(OPERATOR_RCURLY
))
4700 go_error_at(fallthrough_loc
,
4701 _("cannot fallthrough final case in switch"));
4702 else if (!this->peek_token()->is_keyword(KEYWORD_CASE
)
4703 && !this->peek_token()->is_keyword(KEYWORD_DEFAULT
))
4705 go_error_at(fallthrough_loc
, "fallthrough statement out of place");
4706 while (!this->peek_token()->is_keyword(KEYWORD_CASE
)
4707 && !this->peek_token()->is_keyword(KEYWORD_DEFAULT
)
4708 && !this->peek_token()->is_op(OPERATOR_RCURLY
)
4709 && !this->peek_token()->is_eof())
4711 if (this->statement_may_start_here())
4712 this->statement_list();
4714 this->advance_token();
4723 go_error_at(location
, "multiple defaults in switch");
4726 *saw_default
= true;
4729 if (is_default
|| vals
!= NULL
)
4730 clauses
->add(vals
, is_default
, statements
, is_fallthrough
, location
);
4733 // ExprSwitchCase = "case" ExpressionList | "default" .
4736 Parse::expr_switch_case(bool* is_default
)
4738 const Token
* token
= this->peek_token();
4739 if (token
->is_keyword(KEYWORD_CASE
))
4741 this->advance_token();
4742 return this->expression_list(NULL
, false, true);
4744 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4746 this->advance_token();
4753 go_error_at(this->location(), "expected %<case%> or %<default%>");
4754 if (!token
->is_op(OPERATOR_RCURLY
))
4755 this->advance_token();
4760 // The body of a type switch.
4761 // "{" { TypeCaseClause } "}" .
4764 Parse::type_switch_body(Label
* label
, const Type_switch
& type_switch
,
4767 Expression
* init
= type_switch
.expr
;
4768 std::string var_name
= type_switch
.name
;
4769 if (!var_name
.empty())
4771 if (Gogo::is_sink_name(var_name
))
4773 go_error_at(type_switch
.location
,
4774 "no new variables on left side of %<:=%>");
4779 Location loc
= type_switch
.location
;
4780 Temporary_statement
* switch_temp
=
4781 Statement::make_temporary(NULL
, init
, loc
);
4782 this->gogo_
->add_statement(switch_temp
);
4783 init
= Expression::make_temporary_reference(switch_temp
, loc
);
4787 Type_switch_statement
* statement
=
4788 Statement::make_type_switch_statement(var_name
, init
, location
);
4789 this->push_break_statement(statement
, label
);
4791 Type_case_clauses
* case_clauses
= new Type_case_clauses();
4792 bool saw_default
= false;
4793 std::vector
<Named_object
*> implicit_vars
;
4794 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4796 if (this->peek_token()->is_eof())
4798 go_error_at(this->location(), "missing %<}%>");
4801 this->type_case_clause(var_name
, init
, case_clauses
, &saw_default
,
4804 this->advance_token();
4806 statement
->add_clauses(case_clauses
);
4808 this->pop_break_statement();
4810 // If there is a type switch variable implicitly declared in each case clause,
4811 // check that it is used in at least one of the cases.
4812 if (!var_name
.empty())
4815 for (std::vector
<Named_object
*>::iterator p
= implicit_vars
.begin();
4816 p
!= implicit_vars
.end();
4819 if ((*p
)->var_value()->is_used())
4826 go_error_at(type_switch
.location
, "%qs declared but not used",
4827 Gogo::message_name(var_name
).c_str());
4832 // TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
4833 // IMPLICIT_VARS is the list of variables implicitly declared for each type
4834 // case if there is a type switch variable declared.
4837 Parse::type_case_clause(const std::string
& var_name
, Expression
* init
,
4838 Type_case_clauses
* clauses
, bool* saw_default
,
4839 std::vector
<Named_object
*>* implicit_vars
)
4841 Location location
= this->location();
4843 std::vector
<Type
*> types
;
4844 bool is_default
= false;
4845 this->type_switch_case(&types
, &is_default
);
4847 if (!this->peek_token()->is_op(OPERATOR_COLON
))
4848 go_error_at(this->location(), "expected %<:%>");
4850 this->advance_token();
4852 Block
* statements
= NULL
;
4853 if (this->statement_list_may_start_here())
4855 this->gogo_
->start_block(this->location());
4856 if (!var_name
.empty())
4859 Location var_loc
= init
->location();
4860 if (types
.size() == 1)
4862 type
= types
.front();
4863 init
= Expression::make_type_guard(init
, type
, location
);
4866 Variable
* v
= new Variable(type
, init
, false, false, false,
4869 v
->set_is_type_switch_var();
4870 implicit_vars
->push_back(this->gogo_
->add_variable(var_name
, v
));
4872 this->statement_list();
4873 statements
= this->gogo_
->finish_block(this->location());
4876 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH
))
4878 go_error_at(this->location(),
4879 "fallthrough is not permitted in a type switch");
4880 if (this->advance_token()->is_op(OPERATOR_SEMICOLON
))
4881 this->advance_token();
4886 go_assert(types
.empty());
4889 go_error_at(location
, "multiple defaults in type switch");
4892 *saw_default
= true;
4893 clauses
->add(NULL
, false, true, statements
, location
);
4895 else if (!types
.empty())
4897 for (std::vector
<Type
*>::const_iterator p
= types
.begin();
4898 p
+ 1 != types
.end();
4900 clauses
->add(*p
, true, false, NULL
, location
);
4901 clauses
->add(types
.back(), false, false, statements
, location
);
4904 clauses
->add(Type::make_error_type(), false, false, statements
, location
);
4907 // TypeSwitchCase = "case" type | "default"
4909 // We accept a comma separated list of types.
4912 Parse::type_switch_case(std::vector
<Type
*>* types
, bool* is_default
)
4914 const Token
* token
= this->peek_token();
4915 if (token
->is_keyword(KEYWORD_CASE
))
4917 this->advance_token();
4920 Type
* t
= this->type();
4922 if (!t
->is_error_type())
4923 types
->push_back(t
);
4926 this->gogo_
->mark_locals_used();
4927 token
= this->peek_token();
4928 while (!token
->is_op(OPERATOR_COLON
)
4929 && !token
->is_op(OPERATOR_COMMA
)
4930 && !token
->is_op(OPERATOR_RCURLY
)
4931 && !token
->is_eof())
4932 token
= this->advance_token();
4935 if (!this->peek_token()->is_op(OPERATOR_COMMA
))
4937 this->advance_token();
4940 else if (token
->is_keyword(KEYWORD_DEFAULT
))
4942 this->advance_token();
4947 go_error_at(this->location(), "expected %<case%> or %<default%>");
4948 if (!token
->is_op(OPERATOR_RCURLY
))
4949 this->advance_token();
4953 // SelectStat = "select" "{" { CommClause } "}" .
4956 Parse::select_stat(Label
* label
)
4958 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT
));
4959 Location location
= this->location();
4960 const Token
* token
= this->advance_token();
4962 if (!token
->is_op(OPERATOR_LCURLY
))
4964 Location token_loc
= token
->location();
4965 if (token
->is_op(OPERATOR_SEMICOLON
)
4966 && this->advance_token()->is_op(OPERATOR_LCURLY
))
4967 go_error_at(token_loc
, "unexpected semicolon or newline before %<{%>");
4970 go_error_at(this->location(), "expected %<{%>");
4974 this->advance_token();
4976 Select_statement
* statement
= Statement::make_select_statement(location
);
4978 this->push_break_statement(statement
, label
);
4980 Select_clauses
* select_clauses
= new Select_clauses();
4981 bool saw_default
= false;
4982 while (!this->peek_token()->is_op(OPERATOR_RCURLY
))
4984 if (this->peek_token()->is_eof())
4986 go_error_at(this->location(), "expected %<}%>");
4989 this->comm_clause(select_clauses
, &saw_default
);
4992 this->advance_token();
4994 statement
->add_clauses(select_clauses
);
4996 this->pop_break_statement();
4998 this->gogo_
->add_statement(statement
);
5001 // CommClause = CommCase ":" { Statement ";" } .
5004 Parse::comm_clause(Select_clauses
* clauses
, bool* saw_default
)
5006 Location location
= this->location();
5007 bool is_send
= false;
5008 Expression
* channel
= NULL
;
5009 Expression
* val
= NULL
;
5010 Expression
* closed
= NULL
;
5011 std::string varname
;
5012 std::string closedname
;
5013 bool is_default
= false;
5014 bool got_case
= this->comm_case(&is_send
, &channel
, &val
, &closed
,
5015 &varname
, &closedname
, &is_default
);
5017 if (this->peek_token()->is_op(OPERATOR_COLON
))
5018 this->advance_token();
5020 go_error_at(this->location(), "expected colon");
5022 this->gogo_
->start_block(this->location());
5024 Named_object
* var
= NULL
;
5025 if (!varname
.empty())
5027 // FIXME: LOCATION is slightly wrong here.
5028 Variable
* v
= new Variable(NULL
, channel
, false, false, false,
5030 v
->set_type_from_chan_element();
5031 var
= this->gogo_
->add_variable(varname
, v
);
5034 Named_object
* closedvar
= NULL
;
5035 if (!closedname
.empty())
5037 // FIXME: LOCATION is slightly wrong here.
5038 Variable
* v
= new Variable(Type::lookup_bool_type(), NULL
,
5039 false, false, false, location
);
5040 closedvar
= this->gogo_
->add_variable(closedname
, v
);
5043 this->statement_list();
5045 Block
* statements
= this->gogo_
->finish_block(this->location());
5051 go_error_at(location
, "multiple defaults in select");
5054 *saw_default
= true;
5058 clauses
->add(is_send
, channel
, val
, closed
, var
, closedvar
, is_default
,
5059 statements
, location
);
5060 else if (statements
!= NULL
)
5062 // Add the statements to make sure that any names they define
5064 this->gogo_
->add_block(statements
, location
);
5068 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
5071 Parse::comm_case(bool* is_send
, Expression
** channel
, Expression
** val
,
5072 Expression
** closed
, std::string
* varname
,
5073 std::string
* closedname
, bool* is_default
)
5075 const Token
* token
= this->peek_token();
5076 if (token
->is_keyword(KEYWORD_DEFAULT
))
5078 this->advance_token();
5081 else if (token
->is_keyword(KEYWORD_CASE
))
5083 this->advance_token();
5084 if (!this->send_or_recv_stmt(is_send
, channel
, val
, closed
, varname
,
5090 go_error_at(this->location(), "expected %<case%> or %<default%>");
5091 if (!token
->is_op(OPERATOR_RCURLY
))
5092 this->advance_token();
5099 // RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
5100 // RecvExpr = Expression .
5103 Parse::send_or_recv_stmt(bool* is_send
, Expression
** channel
, Expression
** val
,
5104 Expression
** closed
, std::string
* varname
,
5105 std::string
* closedname
)
5107 const Token
* token
= this->peek_token();
5108 bool saw_comma
= false;
5109 bool closed_is_id
= false;
5110 if (token
->is_identifier())
5112 Gogo
* gogo
= this->gogo_
;
5113 std::string recv_var
= token
->identifier();
5114 bool is_rv_exported
= token
->is_identifier_exported();
5115 Location recv_var_loc
= token
->location();
5116 token
= this->advance_token();
5117 if (token
->is_op(OPERATOR_COLONEQ
))
5120 this->advance_token();
5121 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false, false,
5123 Receive_expression
* re
= e
->receive_expression();
5126 if (!e
->is_error_expression())
5127 go_error_at(this->location(), "expected receive expression");
5130 if (recv_var
== "_")
5132 go_error_at(recv_var_loc
,
5133 "no new variables on left side of %<:=%>");
5134 recv_var
= Gogo::erroneous_name();
5137 *varname
= gogo
->pack_hidden_name(recv_var
, is_rv_exported
);
5138 *channel
= re
->channel();
5141 else if (token
->is_op(OPERATOR_COMMA
))
5143 token
= this->advance_token();
5144 if (token
->is_identifier())
5146 std::string recv_closed
= token
->identifier();
5147 bool is_rc_exported
= token
->is_identifier_exported();
5148 Location recv_closed_loc
= token
->location();
5149 closed_is_id
= true;
5151 token
= this->advance_token();
5152 if (token
->is_op(OPERATOR_COLONEQ
))
5154 // case rv, rc := <-c:
5155 this->advance_token();
5156 Expression
* e
= this->expression(PRECEDENCE_NORMAL
, false,
5158 Receive_expression
* re
= e
->receive_expression();
5161 if (!e
->is_error_expression())
5162 go_error_at(this->location(),
5163 "expected receive expression");
5166 if (recv_var
== "_" && recv_closed
== "_")
5168 go_error_at(recv_var_loc
,
5169 "no new variables on left side of %<:=%>");
5170 recv_var
= Gogo::erroneous_name();
5173 if (recv_var
!= "_")
5174 *varname
= gogo
->pack_hidden_name(recv_var
,
5176 if (recv_closed
!= "_")
5177 *closedname
= gogo
->pack_hidden_name(recv_closed
,
5179 *channel
= re
->channel();
5183 this->unget_token(Token::make_identifier_token(recv_closed
,
5188 *val
= this->id_to_expression(gogo
->pack_hidden_name(recv_var
,
5190 recv_var_loc
, true, false);
5194 this->unget_token(Token::make_identifier_token(recv_var
,
5199 // If SAW_COMMA is false, then we are looking at the start of the
5200 // send or receive expression. If SAW_COMMA is true, then *VAL is
5201 // set and we just read a comma.
5204 if (saw_comma
|| !this->peek_token()->is_op(OPERATOR_CHANOP
))
5206 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5207 if (e
->receive_expression() != NULL
)
5210 *channel
= e
->receive_expression()->channel();
5211 // This is 'case (<-c):'. We now expect ':'. If we see
5212 // '<-', then we have case (<-c)<-v:
5213 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5221 this->advance_token();
5222 *channel
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5224 // The next token should be ':'. If it is '<-', then we have
5226 // which is to say, send on a channel received from a channel.
5227 if (!this->peek_token()->is_op(OPERATOR_CHANOP
))
5230 e
= Expression::make_receive(*channel
, (*channel
)->location());
5233 if (!saw_comma
&& this->peek_token()->is_op(OPERATOR_COMMA
))
5235 this->advance_token();
5237 if (!e
->is_sink_expression())
5239 e
= this->expression(PRECEDENCE_NORMAL
, true, true, NULL
, NULL
);
5243 if (this->peek_token()->is_op(OPERATOR_EQ
))
5246 this->advance_token();
5247 Location recvloc
= this->location();
5248 Expression
* recvexpr
= this->expression(PRECEDENCE_NORMAL
, false,
5250 if (recvexpr
->receive_expression() == NULL
)
5252 go_error_at(recvloc
, "missing %<<-%>");
5255 *channel
= recvexpr
->receive_expression()->channel();
5259 // *VAL is already set.
5260 if (!e
->is_sink_expression())
5266 if (!e
->is_sink_expression())
5275 go_error_at(this->location(), "expected %<=%> or %<:=%>");
5277 go_error_at(this->location(), "expected %<=%>");
5281 if (this->peek_token()->is_op(OPERATOR_CHANOP
))
5285 *channel
= this->verify_not_sink(e
);
5286 this->advance_token();
5287 *val
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5291 go_error_at(this->location(), "expected %<<-%> or %<=%>");
5295 // ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
5296 // Condition = Expression .
5299 Parse::for_stat(Label
* label
)
5301 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR
));
5302 Location location
= this->location();
5303 const Token
* token
= this->advance_token();
5305 // Open a block to hold any variables defined in the init statement
5306 // of the for statement.
5307 this->gogo_
->start_block(location
);
5310 Expression
* cond
= NULL
;
5312 Range_clause range_clause
;
5314 if (!token
->is_op(OPERATOR_LCURLY
))
5316 if (token
->is_keyword(KEYWORD_VAR
))
5318 go_error_at(this->location(),
5319 "var declaration not allowed in for initializer");
5323 if (token
->is_op(OPERATOR_SEMICOLON
))
5324 this->for_clause(&cond
, &post
);
5327 // We might be looking at a Condition, an InitStat, or a
5329 bool saw_send_stmt
= false;
5330 cond
= this->simple_stat(false, &saw_send_stmt
, &range_clause
, NULL
);
5331 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5333 if (cond
== NULL
&& !range_clause
.found
)
5336 go_error_at(this->location(),
5337 ("send statement used as value; "
5338 "use select for non-blocking send"));
5340 go_error_at(this->location(),
5341 "parse error in for statement");
5346 if (range_clause
.found
)
5347 go_error_at(this->location(), "parse error after range clause");
5351 // COND is actually an expression statement for
5352 // InitStat at the start of a ForClause.
5353 this->expression_stat(cond
);
5357 this->for_clause(&cond
, &post
);
5362 // Check for the easy error of a newline before starting the block.
5363 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5365 Location semi_loc
= this->location();
5366 if (this->advance_token()->is_op(OPERATOR_LCURLY
))
5367 go_error_at(semi_loc
, "unexpected semicolon or newline, expecting %<{%> after for clause");
5368 // Otherwise we will get an error when we call this->block
5372 // Build the For_statement and note that it is the current target
5373 // for break and continue statements.
5375 For_statement
* sfor
;
5376 For_range_statement
* srange
;
5378 if (!range_clause
.found
)
5380 sfor
= Statement::make_for_statement(init
, cond
, post
, location
);
5386 srange
= Statement::make_for_range_statement(range_clause
.index
,
5394 this->push_break_statement(s
, label
);
5395 this->push_continue_statement(s
, label
);
5397 // Gather the block of statements in the loop and add them to the
5400 this->gogo_
->start_block(this->location());
5401 Location end_loc
= this->block();
5402 Block
* statements
= this->gogo_
->finish_block(end_loc
);
5405 sfor
->add_statements(statements
);
5407 srange
->add_statements(statements
);
5409 // This is no longer the break/continue target.
5410 this->pop_break_statement();
5411 this->pop_continue_statement();
5413 // Add the For_statement to the list of statements, and close out
5414 // the block we started to hold any variables defined in the for
5417 this->gogo_
->add_statement(s
);
5419 this->gogo_
->add_block(this->gogo_
->finish_block(this->location()),
5423 // ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
5424 // InitStat = SimpleStat .
5425 // PostStat = SimpleStat .
5427 // We have already read InitStat at this point.
5430 Parse::for_clause(Expression
** cond
, Block
** post
)
5432 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON
));
5433 this->advance_token();
5434 if (this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5436 else if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5438 go_error_at(this->location(), "unexpected semicolon or newline, expecting %<{%> after for clause");
5444 *cond
= this->expression(PRECEDENCE_NORMAL
, false, true, NULL
, NULL
);
5445 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5446 go_error_at(this->location(), "expected semicolon");
5448 this->advance_token();
5450 if (this->peek_token()->is_op(OPERATOR_LCURLY
))
5454 this->gogo_
->start_block(this->location());
5455 this->simple_stat(false, NULL
, NULL
, NULL
);
5456 *post
= this->gogo_
->finish_block(this->location());
5460 // RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression .
5462 // This is the := version. It is called with a list of identifiers.
5465 Parse::range_clause_decl(const Typed_identifier_list
* til
,
5466 Range_clause
* p_range_clause
)
5468 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5469 Location location
= this->location();
5471 p_range_clause
->found
= true;
5473 if (til
->size() > 2)
5474 go_error_at(this->location(), "too many variables for range clause");
5476 this->advance_token();
5477 Expression
* expr
= this->expression(PRECEDENCE_NORMAL
, false, false, NULL
,
5479 p_range_clause
->range
= expr
;
5484 bool any_new
= false;
5486 const Typed_identifier
* pti
= &til
->front();
5487 Named_object
* no
= this->init_var(*pti
, NULL
, expr
, true, true, &any_new
,
5489 if (any_new
&& no
->is_variable())
5490 no
->var_value()->set_type_from_range_index();
5491 p_range_clause
->index
= Expression::make_var_reference(no
, location
);
5493 if (til
->size() == 1)
5494 p_range_clause
->value
= NULL
;
5498 bool is_new
= false;
5499 no
= this->init_var(*pti
, NULL
, expr
, true, true, &is_new
, NULL
, NULL
);
5500 if (is_new
&& no
->is_variable())
5501 no
->var_value()->set_type_from_range_value();
5504 p_range_clause
->value
= Expression::make_var_reference(no
, location
);
5508 go_error_at(location
, "variables redeclared but no variable is new");
5511 // The = version of RangeClause. This is called with a list of
5515 Parse::range_clause_expr(const Expression_list
* vals
,
5516 Range_clause
* p_range_clause
)
5518 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE
));
5520 p_range_clause
->found
= true;
5522 go_assert(vals
->size() >= 1);
5523 if (vals
->size() > 2)
5524 go_error_at(this->location(), "too many variables for range clause");
5526 this->advance_token();
5527 p_range_clause
->range
= this->expression(PRECEDENCE_NORMAL
, false, false,
5533 p_range_clause
->index
= vals
->front();
5534 if (vals
->size() == 1)
5535 p_range_clause
->value
= NULL
;
5537 p_range_clause
->value
= vals
->back();
5540 // Push a statement on the break stack.
5543 Parse::push_break_statement(Statement
* enclosing
, Label
* label
)
5545 if (this->break_stack_
== NULL
)
5546 this->break_stack_
= new Bc_stack();
5547 this->break_stack_
->push_back(std::make_pair(enclosing
, label
));
5550 // Push a statement on the continue stack.
5553 Parse::push_continue_statement(Statement
* enclosing
, Label
* label
)
5555 if (this->continue_stack_
== NULL
)
5556 this->continue_stack_
= new Bc_stack();
5557 this->continue_stack_
->push_back(std::make_pair(enclosing
, label
));
5560 // Pop the break stack.
5563 Parse::pop_break_statement()
5565 this->break_stack_
->pop_back();
5568 // Pop the continue stack.
5571 Parse::pop_continue_statement()
5573 this->continue_stack_
->pop_back();
5576 // Find a break or continue statement given a label name.
5579 Parse::find_bc_statement(const Bc_stack
* bc_stack
, const std::string
& label
)
5581 if (bc_stack
== NULL
)
5583 for (Bc_stack::const_reverse_iterator p
= bc_stack
->rbegin();
5584 p
!= bc_stack
->rend();
5587 if (p
->second
!= NULL
&& p
->second
->name() == label
)
5589 p
->second
->set_is_used();
5596 // BreakStat = "break" [ identifier ] .
5601 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK
));
5602 Location location
= this->location();
5604 const Token
* token
= this->advance_token();
5605 Statement
* enclosing
;
5606 if (!token
->is_identifier())
5608 if (this->break_stack_
== NULL
|| this->break_stack_
->empty())
5610 go_error_at(this->location(),
5611 "break statement not within for or switch or select");
5614 enclosing
= this->break_stack_
->back().first
;
5618 enclosing
= this->find_bc_statement(this->break_stack_
,
5619 token
->identifier());
5620 if (enclosing
== NULL
)
5622 // If there is a label with this name, mark it as used to
5623 // avoid a useless error about an unused label.
5624 this->gogo_
->add_label_reference(token
->identifier(),
5625 Linemap::unknown_location(), false);
5627 go_error_at(token
->location(), "invalid break label %qs",
5628 Gogo::message_name(token
->identifier()).c_str());
5629 this->advance_token();
5632 this->advance_token();
5635 Unnamed_label
* label
;
5636 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5637 label
= enclosing
->for_statement()->break_label();
5638 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5639 label
= enclosing
->for_range_statement()->break_label();
5640 else if (enclosing
->classification() == Statement::STATEMENT_SWITCH
)
5641 label
= enclosing
->switch_statement()->break_label();
5642 else if (enclosing
->classification() == Statement::STATEMENT_TYPE_SWITCH
)
5643 label
= enclosing
->type_switch_statement()->break_label();
5644 else if (enclosing
->classification() == Statement::STATEMENT_SELECT
)
5645 label
= enclosing
->select_statement()->break_label();
5649 this->gogo_
->add_statement(Statement::make_break_statement(label
,
5653 // ContinueStat = "continue" [ identifier ] .
5656 Parse::continue_stat()
5658 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE
));
5659 Location location
= this->location();
5661 const Token
* token
= this->advance_token();
5662 Statement
* enclosing
;
5663 if (!token
->is_identifier())
5665 if (this->continue_stack_
== NULL
|| this->continue_stack_
->empty())
5667 go_error_at(this->location(), "continue statement not within for");
5670 enclosing
= this->continue_stack_
->back().first
;
5674 enclosing
= this->find_bc_statement(this->continue_stack_
,
5675 token
->identifier());
5676 if (enclosing
== NULL
)
5678 // If there is a label with this name, mark it as used to
5679 // avoid a useless error about an unused label.
5680 this->gogo_
->add_label_reference(token
->identifier(),
5681 Linemap::unknown_location(), false);
5683 go_error_at(token
->location(), "invalid continue label %qs",
5684 Gogo::message_name(token
->identifier()).c_str());
5685 this->advance_token();
5688 this->advance_token();
5691 Unnamed_label
* label
;
5692 if (enclosing
->classification() == Statement::STATEMENT_FOR
)
5693 label
= enclosing
->for_statement()->continue_label();
5694 else if (enclosing
->classification() == Statement::STATEMENT_FOR_RANGE
)
5695 label
= enclosing
->for_range_statement()->continue_label();
5699 this->gogo_
->add_statement(Statement::make_continue_statement(label
,
5703 // GotoStat = "goto" identifier .
5708 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO
));
5709 Location location
= this->location();
5710 const Token
* token
= this->advance_token();
5711 if (!token
->is_identifier())
5712 go_error_at(this->location(), "expected label for goto");
5715 Label
* label
= this->gogo_
->add_label_reference(token
->identifier(),
5717 Statement
* s
= Statement::make_goto_statement(label
, location
);
5718 this->gogo_
->add_statement(s
);
5719 this->advance_token();
5723 // PackageClause = "package" PackageName .
5726 Parse::package_clause()
5728 const Token
* token
= this->peek_token();
5729 Location location
= token
->location();
5731 if (!token
->is_keyword(KEYWORD_PACKAGE
))
5733 go_error_at(this->location(), "program must start with package clause");
5738 token
= this->advance_token();
5739 if (token
->is_identifier())
5741 name
= token
->identifier();
5744 go_error_at(this->location(), "invalid package name %<_%>");
5745 name
= Gogo::erroneous_name();
5747 this->advance_token();
5751 go_error_at(this->location(), "package name must be an identifier");
5755 this->gogo_
->set_package_name(name
, location
);
5758 // ImportDecl = "import" Decl<ImportSpec> .
5761 Parse::import_decl()
5763 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT
));
5764 this->advance_token();
5765 this->decl(&Parse::import_spec
, NULL
, 0);
5768 // ImportSpec = [ "." | PackageName ] PackageFileName .
5771 Parse::import_spec(void*, unsigned int pragmas
)
5774 go_warning_at(this->location(), 0,
5775 "ignoring magic %<//go:...%> comment before import");
5777 const Token
* token
= this->peek_token();
5778 Location location
= token
->location();
5780 std::string local_name
;
5781 bool is_local_name_exported
= false;
5782 if (token
->is_op(OPERATOR_DOT
))
5785 token
= this->advance_token();
5787 else if (token
->is_identifier())
5789 local_name
= token
->identifier();
5790 is_local_name_exported
= token
->is_identifier_exported();
5791 token
= this->advance_token();
5794 if (!token
->is_string())
5796 go_error_at(this->location(), "import path must be a string");
5797 this->advance_token();
5801 this->gogo_
->import_package(token
->string_value(), local_name
,
5802 is_local_name_exported
, true, location
);
5804 this->advance_token();
5807 // SourceFile = PackageClause ";" { ImportDecl ";" }
5808 // { TopLevelDecl ";" } .
5813 this->package_clause();
5815 const Token
* token
= this->peek_token();
5816 if (token
->is_op(OPERATOR_SEMICOLON
))
5817 token
= this->advance_token();
5819 go_error_at(this->location(),
5820 "expected %<;%> or newline after package clause");
5822 while (token
->is_keyword(KEYWORD_IMPORT
))
5824 this->import_decl();
5825 token
= this->peek_token();
5826 if (token
->is_op(OPERATOR_SEMICOLON
))
5827 token
= this->advance_token();
5829 go_error_at(this->location(),
5830 "expected %<;%> or newline after import declaration");
5833 while (!token
->is_eof())
5835 if (this->declaration_may_start_here())
5836 this->declaration();
5839 go_error_at(this->location(), "expected declaration");
5840 this->gogo_
->mark_locals_used();
5842 this->advance_token();
5843 while (!this->peek_token()->is_eof()
5844 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
)
5845 && !this->peek_token()->is_op(OPERATOR_RCURLY
));
5846 if (!this->peek_token()->is_eof()
5847 && !this->peek_token()->is_op(OPERATOR_SEMICOLON
))
5848 this->advance_token();
5850 token
= this->peek_token();
5851 if (token
->is_op(OPERATOR_SEMICOLON
))
5852 token
= this->advance_token();
5853 else if (!token
->is_eof() || !saw_errors())
5855 if (token
->is_op(OPERATOR_CHANOP
))
5856 go_error_at(this->location(),
5857 ("send statement used as value; "
5858 "use select for non-blocking send"));
5860 go_error_at(this->location(),
5861 ("expected %<;%> or newline after top "
5862 "level declaration"));
5863 this->skip_past_error(OPERATOR_INVALID
);
5868 // Skip forward to a semicolon or OP. OP will normally be
5869 // OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move
5870 // past it and return. If we find OP, it will be the next token to
5871 // read. Return true if we are OK, false if we found EOF.
5874 Parse::skip_past_error(Operator op
)
5876 this->gogo_
->mark_locals_used();
5877 const Token
* token
= this->peek_token();
5878 while (!token
->is_op(op
))
5880 if (token
->is_eof())
5882 if (token
->is_op(OPERATOR_SEMICOLON
))
5884 this->advance_token();
5887 token
= this->advance_token();
5892 // Check that an expression is not a sink.
5895 Parse::verify_not_sink(Expression
* expr
)
5897 if (expr
->is_sink_expression())
5899 go_error_at(expr
->location(), "cannot use %<_%> as value");
5900 expr
= Expression::make_error(expr
->location());
5903 // If this can not be a sink, and it is a variable, then we are
5904 // using the variable, not just assigning to it.
5905 if (expr
->var_expression() != NULL
)
5906 this->mark_var_used(expr
->var_expression()->named_object());
5907 else if (expr
->enclosed_var_expression() != NULL
)
5908 this->mark_var_used(expr
->enclosed_var_expression()->variable());
5912 // Mark a variable as used.
5915 Parse::mark_var_used(Named_object
* no
)
5917 if (no
->is_variable())
5918 no
->var_value()->set_is_used();