1// parse.cc -- Go frontend parser. 2 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. 6 7#include "go-system.h" 8 9#include "lex.h" 10#include "gogo.h" 11#include "types.h" 12#include "statements.h" 13#include "expressions.h" 14#include "parse.h" 15 16// Struct Parse::Enclosing_var_comparison. 17 18// Return true if v1 should be considered to be less than v2. 19 20bool 21Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1, 22 const Enclosing_var& v2) 23{ 24 if (v1.var() == v2.var()) 25 return false; 26 27 const std::string& n1(v1.var()->name()); 28 const std::string& n2(v2.var()->name()); 29 int i = n1.compare(n2); 30 if (i < 0) 31 return true; 32 else if (i > 0) 33 return false; 34 35 // If we get here it means that a single nested function refers to 36 // two different variables defined in enclosing functions, and both 37 // variables have the same name. I think this is impossible. 38 go_unreachable(); 39} 40 41// Class Parse. 42 43Parse::Parse(Lex* lex, Gogo* gogo) 44 : lex_(lex), 45 token_(Token::make_invalid_token(Linemap::unknown_location())), 46 unget_token_(Token::make_invalid_token(Linemap::unknown_location())), 47 unget_token_valid_(false), 48 is_erroneous_function_(false), 49 gogo_(gogo), 50 break_stack_(NULL), 51 continue_stack_(NULL), 52 iota_(0), 53 enclosing_vars_() 54{ 55} 56 57// Return the current token. 58 59const Token* 60Parse::peek_token() 61{ 62 if (this->unget_token_valid_) 63 return &this->unget_token_; 64 if (this->token_.is_invalid()) 65 this->token_ = this->lex_->next_token(); 66 return &this->token_; 67} 68 69// Advance to the next token and return it. 70 71const Token* 72Parse::advance_token() 73{ 74 if (this->unget_token_valid_) 75 { 76 this->unget_token_valid_ = false; 77 if (!this->token_.is_invalid()) 78 return &this->token_; 79 } 80 this->token_ = this->lex_->next_token(); 81 return &this->token_; 82} 83 84// Push a token back on the input stream. 85 86void 87Parse::unget_token(const Token& token) 88{ 89 go_assert(!this->unget_token_valid_); 90 this->unget_token_ = token; 91 this->unget_token_valid_ = true; 92} 93 94// The location of the current token. 95 96Location 97Parse::location() 98{ 99 return this->peek_token()->location(); 100} 101 102// IdentifierList = identifier { "," identifier } . 103 104void 105Parse::identifier_list(Typed_identifier_list* til) 106{ 107 const Token* token = this->peek_token(); 108 while (true) 109 { 110 if (!token->is_identifier()) 111 { 112 error_at(this->location(), "expected identifier"); 113 return; 114 } 115 std::string name = 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)) 121 return; 122 token = this->advance_token(); 123 } 124} 125 126// ExpressionList = Expression { "," Expression } . 127 128// If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite 129// literal. 130 131// If MAY_BE_SINK is true, the expressions in the list may be "_". 132 133Expression_list* 134Parse::expression_list(Expression* first, bool may_be_sink, 135 bool may_be_composite_lit) 136{ 137 Expression_list* ret = new Expression_list(); 138 if (first != NULL) 139 ret->push_back(first); 140 while (true) 141 { 142 ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink, 143 may_be_composite_lit, NULL, NULL)); 144 145 const Token* token = this->peek_token(); 146 if (!token->is_op(OPERATOR_COMMA)) 147 return ret; 148 149 // Most expression lists permit a trailing comma. 150 Location location = token->location(); 151 this->advance_token(); 152 if (!this->expression_may_start_here()) 153 { 154 this->unget_token(Token::make_operator_token(OPERATOR_COMMA, 155 location)); 156 return ret; 157 } 158 } 159} 160 161// QualifiedIdent = [ PackageName "." ] identifier . 162// PackageName = identifier . 163 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 167// message. 168 169bool 170Parse::qualified_ident(std::string* pname, Named_object** ppackage) 171{ 172 const Token* token = this->peek_token(); 173 if (!token->is_identifier()) 174 { 175 error_at(this->location(), "expected identifier"); 176 return false; 177 } 178 179 std::string name = token->identifier(); 180 bool is_exported = token->is_identifier_exported(); 181 name = this->gogo_->pack_hidden_name(name, is_exported); 182 183 token = this->advance_token(); 184 if (!token->is_op(OPERATOR_DOT)) 185 { 186 *pname = name; 187 *ppackage = NULL; 188 return true; 189 } 190 191 Named_object* package = this->gogo_->lookup(name, NULL); 192 if (package == NULL || !package->is_package()) 193 { 194 error_at(this->location(), "expected package"); 195 // We expect . IDENTIFIER; skip both. 196 if (this->advance_token()->is_identifier()) 197 this->advance_token(); 198 return false; 199 } 200 201 package->package_value()->note_usage(); 202 203 token = this->advance_token(); 204 if (!token->is_identifier()) 205 { 206 error_at(this->location(), "expected identifier"); 207 return false; 208 } 209 210 name = token->identifier(); 211 212 if (name == "_") 213 { 214 error_at(this->location(), "invalid use of %<_%>"); 215 name = Gogo::erroneous_name(); 216 } 217 218 if (package->name() == this->gogo_->package_name()) 219 name = this->gogo_->pack_hidden_name(name, 220 token->is_identifier_exported()); 221 222 *pname = name; 223 *ppackage = package; 224 225 this->advance_token(); 226 227 return true; 228} 229 230// Type = TypeName | TypeLit | "(" Type ")" . 231// TypeLit = 232// ArrayType | StructType | PointerType | FunctionType | InterfaceType | 233// SliceType | MapType | ChannelType . 234 235Type* 236Parse::type() 237{ 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)) 249 { 250 Location location = token->location(); 251 this->advance_token(); 252 Type* type = this->signature(NULL, location); 253 if (type == NULL) 254 return Type::make_error_type(); 255 return type; 256 } 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)) 264 { 265 this->advance_token(); 266 Type* ret = this->type(); 267 if (this->peek_token()->is_op(OPERATOR_RPAREN)) 268 this->advance_token(); 269 else 270 { 271 if (!ret->is_error_type()) 272 error_at(this->location(), "expected %<)%>"); 273 } 274 return ret; 275 } 276 else 277 { 278 error_at(token->location(), "expected type"); 279 return Type::make_error_type(); 280 } 281} 282 283bool 284Parse::type_may_start_here() 285{ 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)); 297} 298 299// TypeName = QualifiedIdent . 300 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. 303 304Type* 305Parse::type_name(bool issue_error) 306{ 307 Location location = this->location(); 308 309 std::string name; 310 Named_object* package; 311 if (!this->qualified_ident(&name, &package)) 312 return Type::make_error_type(); 313 314 Named_object* named_object; 315 if (package == NULL) 316 named_object = this->gogo_->lookup(name, NULL); 317 else 318 { 319 named_object = package->package_value()->lookup(name); 320 if (named_object == NULL 321 && issue_error 322 && package->name() != this->gogo_->package_name()) 323 { 324 // Check whether the name is there but hidden. 325 std::string s = ('.' + package->package_value()->pkgpath() 326 + '.' + name); 327 named_object = package->package_value()->lookup(s); 328 if (named_object != NULL) 329 { 330 Package* p = package->package_value(); 331 const std::string& packname(p->package_name()); 332 error_at(location, "invalid reference to hidden type %<%s.%s%>", 333 Gogo::message_name(packname).c_str(), 334 Gogo::message_name(name).c_str()); 335 issue_error = false; 336 } 337 } 338 } 339 340 bool ok = true; 341 if (named_object == NULL) 342 { 343 if (package == NULL) 344 named_object = this->gogo_->add_unknown_name(name, location); 345 else 346 { 347 const std::string& packname(package->package_value()->package_name()); 348 error_at(location, "reference to undefined identifier %<%s.%s%>", 349 Gogo::message_name(packname).c_str(), 350 Gogo::message_name(name).c_str()); 351 issue_error = false; 352 ok = false; 353 } 354 } 355 else if (named_object->is_type()) 356 { 357 if (!named_object->type_value()->is_visible()) 358 ok = false; 359 } 360 else if (named_object->is_unknown() || named_object->is_type_declaration()) 361 ; 362 else 363 ok = false; 364 365 if (!ok) 366 { 367 if (issue_error) 368 error_at(location, "expected type"); 369 return Type::make_error_type(); 370 } 371 372 if (named_object->is_type()) 373 return named_object->type_value(); 374 else if (named_object->is_unknown() || named_object->is_type_declaration()) 375 return Type::make_forward_declaration(named_object); 376 else 377 go_unreachable(); 378} 379 380// ArrayType = "[" [ ArrayLength ] "]" ElementType . 381// ArrayLength = Expression . 382// ElementType = CompleteType . 383 384Type* 385Parse::array_type(bool may_use_ellipsis) 386{ 387 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE)); 388 const Token* token = this->advance_token(); 389 390 Expression* length = NULL; 391 if (token->is_op(OPERATOR_RSQUARE)) 392 this->advance_token(); 393 else 394 { 395 if (!token->is_op(OPERATOR_ELLIPSIS)) 396 length = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 397 else if (may_use_ellipsis) 398 { 399 // An ellipsis is used in composite literals to represent a 400 // fixed array of the size of the number of elements. We 401 // use a length of nil to represent this, and change the 402 // length when parsing the composite literal. 403 length = Expression::make_nil(this->location()); 404 this->advance_token(); 405 } 406 else 407 { 408 error_at(this->location(), 409 "use of %<[...]%> outside of array literal"); 410 length = Expression::make_error(this->location()); 411 this->advance_token(); 412 } 413 if (!this->peek_token()->is_op(OPERATOR_RSQUARE)) 414 { 415 error_at(this->location(), "expected %<]%>"); 416 return Type::make_error_type(); 417 } 418 this->advance_token(); 419 } 420 421 Type* element_type = this->type(); 422 423 return Type::make_array_type(element_type, length); 424} 425 426// MapType = "map" "[" KeyType "]" ValueType . 427// KeyType = CompleteType . 428// ValueType = CompleteType . 429 430Type* 431Parse::map_type() 432{ 433 Location location = this->location(); 434 go_assert(this->peek_token()->is_keyword(KEYWORD_MAP)); 435 if (!this->advance_token()->is_op(OPERATOR_LSQUARE)) 436 { 437 error_at(this->location(), "expected %<[%>"); 438 return Type::make_error_type(); 439 } 440 this->advance_token(); 441 442 Type* key_type = this->type(); 443 444 if (!this->peek_token()->is_op(OPERATOR_RSQUARE)) 445 { 446 error_at(this->location(), "expected %<]%>"); 447 return Type::make_error_type(); 448 } 449 this->advance_token(); 450 451 Type* value_type = this->type(); 452 453 if (key_type->is_error_type() || value_type->is_error_type()) 454 return Type::make_error_type(); 455 456 return Type::make_map_type(key_type, value_type, location); 457} 458 459// StructType = "struct" "{" { FieldDecl ";" } "}" . 460 461Type* 462Parse::struct_type() 463{ 464 go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT)); 465 Location location = this->location(); 466 if (!this->advance_token()->is_op(OPERATOR_LCURLY)) 467 { 468 Location token_loc = this->location(); 469 if (this->peek_token()->is_op(OPERATOR_SEMICOLON) 470 && this->advance_token()->is_op(OPERATOR_LCURLY)) 471 error_at(token_loc, "unexpected semicolon or newline before %<{%>"); 472 else 473 { 474 error_at(this->location(), "expected %<{%>"); 475 return Type::make_error_type(); 476 } 477 } 478 this->advance_token(); 479 480 Struct_field_list* sfl = new Struct_field_list; 481 while (!this->peek_token()->is_op(OPERATOR_RCURLY)) 482 { 483 this->field_decl(sfl); 484 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 485 this->advance_token(); 486 else if (!this->peek_token()->is_op(OPERATOR_RCURLY)) 487 { 488 error_at(this->location(), "expected %<;%> or %<}%> or newline"); 489 if (!this->skip_past_error(OPERATOR_RCURLY)) 490 return Type::make_error_type(); 491 } 492 } 493 this->advance_token(); 494 495 for (Struct_field_list::const_iterator pi = sfl->begin(); 496 pi != sfl->end(); 497 ++pi) 498 { 499 if (pi->type()->is_error_type()) 500 return pi->type(); 501 for (Struct_field_list::const_iterator pj = pi + 1; 502 pj != sfl->end(); 503 ++pj) 504 { 505 if (pi->field_name() == pj->field_name() 506 && !Gogo::is_sink_name(pi->field_name())) 507 error_at(pi->location(), "duplicate field name %<%s%>", 508 Gogo::message_name(pi->field_name()).c_str()); 509 } 510 } 511 512 return Type::make_struct_type(sfl, location); 513} 514 515// FieldDecl = (IdentifierList CompleteType | TypeName) [ Tag ] . 516// Tag = string_lit . 517 518void 519Parse::field_decl(Struct_field_list* sfl) 520{ 521 const Token* token = this->peek_token(); 522 Location location = token->location(); 523 bool is_anonymous; 524 bool is_anonymous_pointer; 525 if (token->is_op(OPERATOR_MULT)) 526 { 527 is_anonymous = true; 528 is_anonymous_pointer = true; 529 } 530 else if (token->is_identifier()) 531 { 532 std::string id = token->identifier(); 533 bool is_id_exported = token->is_identifier_exported(); 534 Location id_location = token->location(); 535 token = this->advance_token(); 536 is_anonymous = (token->is_op(OPERATOR_SEMICOLON) 537 || token->is_op(OPERATOR_RCURLY) 538 || token->is_op(OPERATOR_DOT) 539 || token->is_string()); 540 is_anonymous_pointer = false; 541 this->unget_token(Token::make_identifier_token(id, is_id_exported, 542 id_location)); 543 } 544 else 545 { 546 error_at(this->location(), "expected field name"); 547 this->gogo_->mark_locals_used(); 548 while (!token->is_op(OPERATOR_SEMICOLON) 549 && !token->is_op(OPERATOR_RCURLY) 550 && !token->is_eof()) 551 token = this->advance_token(); 552 return; 553 } 554 555 if (is_anonymous) 556 { 557 if (is_anonymous_pointer) 558 { 559 this->advance_token(); 560 if (!this->peek_token()->is_identifier()) 561 { 562 error_at(this->location(), "expected field name"); 563 this->gogo_->mark_locals_used(); 564 while (!token->is_op(OPERATOR_SEMICOLON) 565 && !token->is_op(OPERATOR_RCURLY) 566 && !token->is_eof()) 567 token = this->advance_token(); 568 return; 569 } 570 } 571 Type* type = this->type_name(true); 572 573 std::string tag; 574 if (this->peek_token()->is_string()) 575 { 576 tag = this->peek_token()->string_value(); 577 this->advance_token(); 578 } 579 580 if (!type->is_error_type()) 581 { 582 if (is_anonymous_pointer) 583 type = Type::make_pointer_type(type); 584 sfl->push_back(Struct_field(Typed_identifier("", type, location))); 585 if (!tag.empty()) 586 sfl->back().set_tag(tag); 587 } 588 } 589 else 590 { 591 Typed_identifier_list til; 592 while (true) 593 { 594 token = this->peek_token(); 595 if (!token->is_identifier()) 596 { 597 error_at(this->location(), "expected identifier"); 598 return; 599 } 600 std::string name = 601 this->gogo_->pack_hidden_name(token->identifier(), 602 token->is_identifier_exported()); 603 til.push_back(Typed_identifier(name, NULL, token->location())); 604 if (!this->advance_token()->is_op(OPERATOR_COMMA)) 605 break; 606 this->advance_token(); 607 } 608 609 Type* type = this->type(); 610 611 std::string tag; 612 if (this->peek_token()->is_string()) 613 { 614 tag = this->peek_token()->string_value(); 615 this->advance_token(); 616 } 617 618 for (Typed_identifier_list::iterator p = til.begin(); 619 p != til.end(); 620 ++p) 621 { 622 p->set_type(type); 623 sfl->push_back(Struct_field(*p)); 624 if (!tag.empty()) 625 sfl->back().set_tag(tag); 626 } 627 } 628} 629 630// PointerType = "*" Type . 631 632Type* 633Parse::pointer_type() 634{ 635 go_assert(this->peek_token()->is_op(OPERATOR_MULT)); 636 this->advance_token(); 637 Type* type = this->type(); 638 if (type->is_error_type()) 639 return type; 640 return Type::make_pointer_type(type); 641} 642 643// ChannelType = Channel | SendChannel | RecvChannel . 644// Channel = "chan" ElementType . 645// SendChannel = "chan" "<-" ElementType . 646// RecvChannel = "<-" "chan" ElementType . 647 648Type* 649Parse::channel_type() 650{ 651 const Token* token = this->peek_token(); 652 bool send = true; 653 bool receive = true; 654 if (token->is_op(OPERATOR_CHANOP)) 655 { 656 if (!this->advance_token()->is_keyword(KEYWORD_CHAN)) 657 { 658 error_at(this->location(), "expected %<chan%>"); 659 return Type::make_error_type(); 660 } 661 send = false; 662 this->advance_token(); 663 } 664 else 665 { 666 go_assert(token->is_keyword(KEYWORD_CHAN)); 667 if (this->advance_token()->is_op(OPERATOR_CHANOP)) 668 { 669 receive = false; 670 this->advance_token(); 671 } 672 } 673 674 // Better error messages for the common error of omitting the 675 // channel element type. 676 if (!this->type_may_start_here()) 677 { 678 token = this->peek_token(); 679 if (token->is_op(OPERATOR_RCURLY)) 680 error_at(this->location(), "unexpected %<}%> in channel type"); 681 else if (token->is_op(OPERATOR_RPAREN)) 682 error_at(this->location(), "unexpected %<)%> in channel type"); 683 else if (token->is_op(OPERATOR_COMMA)) 684 error_at(this->location(), "unexpected comma in channel type"); 685 else 686 error_at(this->location(), "expected channel element type"); 687 return Type::make_error_type(); 688 } 689 690 Type* element_type = this->type(); 691 return Type::make_channel_type(send, receive, element_type); 692} 693 694// Give an error for a duplicate parameter or receiver name. 695 696void 697Parse::check_signature_names(const Typed_identifier_list* params, 698 Parse::Names* names) 699{ 700 for (Typed_identifier_list::const_iterator p = params->begin(); 701 p != params->end(); 702 ++p) 703 { 704 if (p->name().empty() || Gogo::is_sink_name(p->name())) 705 continue; 706 std::pair<std::string, const Typed_identifier*> val = 707 std::make_pair(p->name(), &*p); 708 std::pair<Parse::Names::iterator, bool> ins = names->insert(val); 709 if (!ins.second) 710 { 711 error_at(p->location(), "redefinition of %qs", 712 Gogo::message_name(p->name()).c_str()); 713 inform(ins.first->second->location(), 714 "previous definition of %qs was here", 715 Gogo::message_name(p->name()).c_str()); 716 } 717 } 718} 719 720// Signature = Parameters [ Result ] . 721 722// RECEIVER is the receiver if there is one, or NULL. LOCATION is the 723// location of the start of the type. 724 725// This returns NULL on a parse error. 726 727Function_type* 728Parse::signature(Typed_identifier* receiver, Location location) 729{ 730 bool is_varargs = false; 731 Typed_identifier_list* params; 732 bool params_ok = this->parameters(¶ms, &is_varargs); 733 734 Typed_identifier_list* results = NULL; 735 if (this->peek_token()->is_op(OPERATOR_LPAREN) 736 || this->type_may_start_here()) 737 { 738 if (!this->result(&results)) 739 return NULL; 740 } 741 742 if (!params_ok) 743 return NULL; 744 745 Parse::Names names; 746 if (receiver != NULL) 747 names[receiver->name()] = receiver; 748 if (params != NULL) 749 this->check_signature_names(params, &names); 750 if (results != NULL) 751 this->check_signature_names(results, &names); 752 753 Function_type* ret = Type::make_function_type(receiver, params, results, 754 location); 755 if (is_varargs) 756 ret->set_is_varargs(); 757 return ret; 758} 759 760// Parameters = "(" [ ParameterList [ "," ] ] ")" . 761 762// This returns false on a parse error. 763 764bool 765Parse::parameters(Typed_identifier_list** pparams, bool* is_varargs) 766{ 767 *pparams = NULL; 768 769 if (!this->peek_token()->is_op(OPERATOR_LPAREN)) 770 { 771 error_at(this->location(), "expected %<(%>"); 772 return false; 773 } 774 775 Typed_identifier_list* params = NULL; 776 bool saw_error = false; 777 778 const Token* token = this->advance_token(); 779 if (!token->is_op(OPERATOR_RPAREN)) 780 { 781 params = this->parameter_list(is_varargs); 782 if (params == NULL) 783 saw_error = true; 784 token = this->peek_token(); 785 } 786 787 // The optional trailing comma is picked up in parameter_list. 788 789 if (!token->is_op(OPERATOR_RPAREN)) 790 error_at(this->location(), "expected %<)%>"); 791 else 792 this->advance_token(); 793 794 if (saw_error) 795 return false; 796 797 *pparams = params; 798 return true; 799} 800 801// ParameterList = ParameterDecl { "," ParameterDecl } . 802 803// This sets *IS_VARARGS if the list ends with an ellipsis. 804// IS_VARARGS will be NULL if varargs are not permitted. 805 806// We pick up an optional trailing comma. 807 808// This returns NULL if some error is seen. 809 810Typed_identifier_list* 811Parse::parameter_list(bool* is_varargs) 812{ 813 Location location = this->location(); 814 Typed_identifier_list* ret = new Typed_identifier_list(); 815 816 bool saw_error = false; 817 818 // If we see an identifier and then a comma, then we don't know 819 // whether we are looking at a list of identifiers followed by a 820 // type, or a list of types given by name. We have to do an 821 // arbitrary lookahead to figure it out. 822 823 bool parameters_have_names; 824 const Token* token = this->peek_token(); 825 if (!token->is_identifier()) 826 { 827 // This must be a type which starts with something like '*'. 828 parameters_have_names = false; 829 } 830 else 831 { 832 std::string name = token->identifier(); 833 bool is_exported = token->is_identifier_exported(); 834 Location location = token->location(); 835 token = this->advance_token(); 836 if (!token->is_op(OPERATOR_COMMA)) 837 { 838 if (token->is_op(OPERATOR_DOT)) 839 { 840 // This is a qualified identifier, which must turn out 841 // to be a type. 842 parameters_have_names = false; 843 } 844 else if (token->is_op(OPERATOR_RPAREN)) 845 { 846 // A single identifier followed by a parenthesis must be 847 // a type name. 848 parameters_have_names = false; 849 } 850 else 851 { 852 // An identifier followed by something other than a 853 // comma or a dot or a right parenthesis must be a 854 // parameter name followed by a type. 855 parameters_have_names = true; 856 } 857 858 this->unget_token(Token::make_identifier_token(name, is_exported, 859 location)); 860 } 861 else 862 { 863 // An identifier followed by a comma may be the first in a 864 // list of parameter names followed by a type, or it may be 865 // the first in a list of types without parameter names. To 866 // find out we gather as many identifiers separated by 867 // commas as we can. 868 std::string id_name = this->gogo_->pack_hidden_name(name, 869 is_exported); 870 ret->push_back(Typed_identifier(id_name, NULL, location)); 871 bool just_saw_comma = true; 872 while (this->advance_token()->is_identifier()) 873 { 874 name = this->peek_token()->identifier(); 875 is_exported = this->peek_token()->is_identifier_exported(); 876 location = this->peek_token()->location(); 877 id_name = this->gogo_->pack_hidden_name(name, is_exported); 878 ret->push_back(Typed_identifier(id_name, NULL, location)); 879 if (!this->advance_token()->is_op(OPERATOR_COMMA)) 880 { 881 just_saw_comma = false; 882 break; 883 } 884 } 885 886 if (just_saw_comma) 887 { 888 // We saw ID1 "," ID2 "," followed by something which 889 // was not an identifier. We must be seeing the start 890 // of a type, and ID1 and ID2 must be types, and the 891 // parameters don't have names. 892 parameters_have_names = false; 893 } 894 else if (this->peek_token()->is_op(OPERATOR_RPAREN)) 895 { 896 // We saw ID1 "," ID2 ")". ID1 and ID2 must be types, 897 // and the parameters don't have names. 898 parameters_have_names = false; 899 } 900 else if (this->peek_token()->is_op(OPERATOR_DOT)) 901 { 902 // We saw ID1 "," ID2 ".". ID2 must be a package name, 903 // ID1 must be a type, and the parameters don't have 904 // names. 905 parameters_have_names = false; 906 this->unget_token(Token::make_identifier_token(name, is_exported, 907 location)); 908 ret->pop_back(); 909 just_saw_comma = true; 910 } 911 else 912 { 913 // We saw ID1 "," ID2 followed by something other than 914 // ",", ".", or ")". We must be looking at the start of 915 // a type, and ID1 and ID2 must be parameter names. 916 parameters_have_names = true; 917 } 918 919 if (parameters_have_names) 920 { 921 go_assert(!just_saw_comma); 922 // We have just seen ID1, ID2 xxx. 923 Type* type; 924 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS)) 925 type = this->type(); 926 else 927 { 928 error_at(this->location(), "%<...%> only permits one name"); 929 saw_error = true; 930 this->advance_token(); 931 type = this->type(); 932 } 933 for (size_t i = 0; i < ret->size(); ++i) 934 ret->set_type(i, type); 935 if (!this->peek_token()->is_op(OPERATOR_COMMA)) 936 return saw_error ? NULL : ret; 937 if (this->advance_token()->is_op(OPERATOR_RPAREN)) 938 return saw_error ? NULL : ret; 939 } 940 else 941 { 942 Typed_identifier_list* tret = new Typed_identifier_list(); 943 for (Typed_identifier_list::const_iterator p = ret->begin(); 944 p != ret->end(); 945 ++p) 946 { 947 Named_object* no = this->gogo_->lookup(p->name(), NULL); 948 Type* type; 949 if (no == NULL) 950 no = this->gogo_->add_unknown_name(p->name(), 951 p->location()); 952 953 if (no->is_type()) 954 type = no->type_value(); 955 else if (no->is_unknown() || no->is_type_declaration()) 956 type = Type::make_forward_declaration(no); 957 else 958 { 959 error_at(p->location(), "expected %<%s%> to be a type", 960 Gogo::message_name(p->name()).c_str()); 961 saw_error = true; 962 type = Type::make_error_type(); 963 } 964 tret->push_back(Typed_identifier("", type, p->location())); 965 } 966 delete ret; 967 ret = tret; 968 if (!just_saw_comma 969 || this->peek_token()->is_op(OPERATOR_RPAREN)) 970 return saw_error ? NULL : ret; 971 } 972 } 973 } 974 975 bool mix_error = false; 976 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error, 977 &saw_error); 978 while (this->peek_token()->is_op(OPERATOR_COMMA)) 979 { 980 if (this->advance_token()->is_op(OPERATOR_RPAREN)) 981 break; 982 if (is_varargs != NULL && *is_varargs) 983 { 984 error_at(this->location(), "%<...%> must be last parameter"); 985 saw_error = true; 986 } 987 this->parameter_decl(parameters_have_names, ret, is_varargs, &mix_error, 988 &saw_error); 989 } 990 if (mix_error) 991 { 992 error_at(location, "invalid named/anonymous mix"); 993 saw_error = true; 994 } 995 if (saw_error) 996 { 997 delete ret; 998 return NULL; 999 } 1000 return ret; 1001} 1002 1003// ParameterDecl = [ IdentifierList ] [ "..." ] Type . 1004 1005void 1006Parse::parameter_decl(bool parameters_have_names, 1007 Typed_identifier_list* til, 1008 bool* is_varargs, 1009 bool* mix_error, 1010 bool* saw_error) 1011{ 1012 if (!parameters_have_names) 1013 { 1014 Type* type; 1015 Location location = this->location(); 1016 if (!this->peek_token()->is_identifier()) 1017 { 1018 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS)) 1019 type = this->type(); 1020 else 1021 { 1022 if (is_varargs == NULL) 1023 error_at(this->location(), "invalid use of %<...%>"); 1024 else 1025 *is_varargs = true; 1026 this->advance_token(); 1027 if (is_varargs == NULL 1028 && this->peek_token()->is_op(OPERATOR_RPAREN)) 1029 type = Type::make_error_type(); 1030 else 1031 { 1032 Type* element_type = this->type(); 1033 type = Type::make_array_type(element_type, NULL); 1034 } 1035 } 1036 } 1037 else 1038 { 1039 type = this->type_name(false); 1040 if (type->is_error_type() 1041 || (!this->peek_token()->is_op(OPERATOR_COMMA) 1042 && !this->peek_token()->is_op(OPERATOR_RPAREN))) 1043 { 1044 *mix_error = true; 1045 while (!this->peek_token()->is_op(OPERATOR_COMMA) 1046 && !this->peek_token()->is_op(OPERATOR_RPAREN)) 1047 this->advance_token(); 1048 } 1049 } 1050 if (!type->is_error_type()) 1051 til->push_back(Typed_identifier("", type, location)); 1052 else 1053 *saw_error = true; 1054 } 1055 else 1056 { 1057 size_t orig_count = til->size(); 1058 if (this->peek_token()->is_identifier()) 1059 this->identifier_list(til); 1060 else 1061 *mix_error = true; 1062 size_t new_count = til->size(); 1063 1064 Type* type; 1065 if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS)) 1066 type = this->type(); 1067 else 1068 { 1069 if (is_varargs == NULL) 1070 { 1071 error_at(this->location(), "invalid use of %<...%>"); 1072 *saw_error = true; 1073 } 1074 else if (new_count > orig_count + 1) 1075 { 1076 error_at(this->location(), "%<...%> only permits one name"); 1077 *saw_error = true; 1078 } 1079 else 1080 *is_varargs = true; 1081 this->advance_token(); 1082 Type* element_type = this->type(); 1083 type = Type::make_array_type(element_type, NULL); 1084 } 1085 for (size_t i = orig_count; i < new_count; ++i) 1086 til->set_type(i, type); 1087 } 1088} 1089 1090// Result = Parameters | Type . 1091 1092// This returns false on a parse error. 1093 1094bool 1095Parse::result(Typed_identifier_list** presults) 1096{ 1097 if (this->peek_token()->is_op(OPERATOR_LPAREN)) 1098 return this->parameters(presults, NULL); 1099 else 1100 { 1101 Location location = this->location(); 1102 Type* type = this->type(); 1103 if (type->is_error_type()) 1104 { 1105 *presults = NULL; 1106 return false; 1107 } 1108 Typed_identifier_list* til = new Typed_identifier_list(); 1109 til->push_back(Typed_identifier("", type, location)); 1110 *presults = til; 1111 return true; 1112 } 1113} 1114 1115// Block = "{" [ StatementList ] "}" . 1116 1117// Returns the location of the closing brace. 1118 1119Location 1120Parse::block() 1121{ 1122 if (!this->peek_token()->is_op(OPERATOR_LCURLY)) 1123 { 1124 Location loc = this->location(); 1125 if (this->peek_token()->is_op(OPERATOR_SEMICOLON) 1126 && this->advance_token()->is_op(OPERATOR_LCURLY)) 1127 error_at(loc, "unexpected semicolon or newline before %<{%>"); 1128 else 1129 { 1130 error_at(this->location(), "expected %<{%>"); 1131 return Linemap::unknown_location(); 1132 } 1133 } 1134 1135 const Token* token = this->advance_token(); 1136 1137 if (!token->is_op(OPERATOR_RCURLY)) 1138 { 1139 this->statement_list(); 1140 token = this->peek_token(); 1141 if (!token->is_op(OPERATOR_RCURLY)) 1142 { 1143 if (!token->is_eof() || !saw_errors()) 1144 error_at(this->location(), "expected %<}%>"); 1145 1146 this->gogo_->mark_locals_used(); 1147 1148 // Skip ahead to the end of the block, in hopes of avoiding 1149 // lots of meaningless errors. 1150 Location ret = token->location(); 1151 int nest = 0; 1152 while (!token->is_eof()) 1153 { 1154 if (token->is_op(OPERATOR_LCURLY)) 1155 ++nest; 1156 else if (token->is_op(OPERATOR_RCURLY)) 1157 { 1158 --nest; 1159 if (nest < 0) 1160 { 1161 this->advance_token(); 1162 break; 1163 } 1164 } 1165 token = this->advance_token(); 1166 ret = token->location(); 1167 } 1168 return ret; 1169 } 1170 } 1171 1172 Location ret = token->location(); 1173 this->advance_token(); 1174 return ret; 1175} 1176 1177// InterfaceType = "interface" "{" [ MethodSpecList ] "}" . 1178// MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] . 1179 1180Type* 1181Parse::interface_type(bool record) 1182{ 1183 go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE)); 1184 Location location = this->location(); 1185 1186 if (!this->advance_token()->is_op(OPERATOR_LCURLY)) 1187 { 1188 Location token_loc = this->location(); 1189 if (this->peek_token()->is_op(OPERATOR_SEMICOLON) 1190 && this->advance_token()->is_op(OPERATOR_LCURLY)) 1191 error_at(token_loc, "unexpected semicolon or newline before %<{%>"); 1192 else 1193 { 1194 error_at(this->location(), "expected %<{%>"); 1195 return Type::make_error_type(); 1196 } 1197 } 1198 this->advance_token(); 1199 1200 Typed_identifier_list* methods = new Typed_identifier_list(); 1201 if (!this->peek_token()->is_op(OPERATOR_RCURLY)) 1202 { 1203 this->method_spec(methods); 1204 while (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 1205 { 1206 if (this->advance_token()->is_op(OPERATOR_RCURLY)) 1207 break; 1208 this->method_spec(methods); 1209 } 1210 if (!this->peek_token()->is_op(OPERATOR_RCURLY)) 1211 { 1212 error_at(this->location(), "expected %<}%>"); 1213 while (!this->advance_token()->is_op(OPERATOR_RCURLY)) 1214 { 1215 if (this->peek_token()->is_eof()) 1216 return Type::make_error_type(); 1217 } 1218 } 1219 } 1220 this->advance_token(); 1221 1222 if (methods->empty()) 1223 { 1224 delete methods; 1225 methods = NULL; 1226 } 1227 1228 Interface_type* ret = Type::make_interface_type(methods, location); 1229 if (record) 1230 this->gogo_->record_interface_type(ret); 1231 return ret; 1232} 1233 1234// MethodSpec = MethodName Signature | InterfaceTypeName . 1235// MethodName = identifier . 1236// InterfaceTypeName = TypeName . 1237 1238void 1239Parse::method_spec(Typed_identifier_list* methods) 1240{ 1241 const Token* token = this->peek_token(); 1242 if (!token->is_identifier()) 1243 { 1244 error_at(this->location(), "expected identifier"); 1245 return; 1246 } 1247 1248 std::string name = token->identifier(); 1249 bool is_exported = token->is_identifier_exported(); 1250 Location location = token->location(); 1251 1252 if (this->advance_token()->is_op(OPERATOR_LPAREN)) 1253 { 1254 // This is a MethodName. 1255 if (name == "_") 1256 error_at(this->location(), "methods must have a unique non-blank name"); 1257 name = this->gogo_->pack_hidden_name(name, is_exported); 1258 Type* type = this->signature(NULL, location); 1259 if (type == NULL) 1260 return; 1261 methods->push_back(Typed_identifier(name, type, location)); 1262 } 1263 else 1264 { 1265 this->unget_token(Token::make_identifier_token(name, is_exported, 1266 location)); 1267 Type* type = this->type_name(false); 1268 if (type->is_error_type() 1269 || (!this->peek_token()->is_op(OPERATOR_SEMICOLON) 1270 && !this->peek_token()->is_op(OPERATOR_RCURLY))) 1271 { 1272 if (this->peek_token()->is_op(OPERATOR_COMMA)) 1273 error_at(this->location(), 1274 "name list not allowed in interface type"); 1275 else 1276 error_at(location, "expected signature or type name"); 1277 this->gogo_->mark_locals_used(); 1278 token = this->peek_token(); 1279 while (!token->is_eof() 1280 && !token->is_op(OPERATOR_SEMICOLON) 1281 && !token->is_op(OPERATOR_RCURLY)) 1282 token = this->advance_token(); 1283 return; 1284 } 1285 // This must be an interface type, but we can't check that now. 1286 // We check it and pull out the methods in 1287 // Interface_type::do_verify. 1288 methods->push_back(Typed_identifier("", type, location)); 1289 } 1290} 1291 1292// Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl . 1293 1294void 1295Parse::declaration() 1296{ 1297 const Token* token = this->peek_token(); 1298 1299 bool saw_nointerface = this->lex_->get_and_clear_nointerface(); 1300 if (saw_nointerface && !token->is_keyword(KEYWORD_FUNC)) 1301 warning_at(token->location(), 0, 1302 "ignoring magic //go:nointerface comment before non-method"); 1303 1304 if (token->is_keyword(KEYWORD_CONST)) 1305 this->const_decl(); 1306 else if (token->is_keyword(KEYWORD_TYPE)) 1307 this->type_decl(); 1308 else if (token->is_keyword(KEYWORD_VAR)) 1309 this->var_decl(); 1310 else if (token->is_keyword(KEYWORD_FUNC)) 1311 this->function_decl(saw_nointerface); 1312 else 1313 { 1314 error_at(this->location(), "expected declaration"); 1315 this->advance_token(); 1316 } 1317} 1318 1319bool 1320Parse::declaration_may_start_here() 1321{ 1322 const Token* token = this->peek_token(); 1323 return (token->is_keyword(KEYWORD_CONST) 1324 || token->is_keyword(KEYWORD_TYPE) 1325 || token->is_keyword(KEYWORD_VAR) 1326 || token->is_keyword(KEYWORD_FUNC)); 1327} 1328 1329// Decl<P> = P | "(" [ List<P> ] ")" . 1330 1331void 1332Parse::decl(void (Parse::*pfn)(void*), void* varg) 1333{ 1334 if (this->peek_token()->is_eof()) 1335 { 1336 if (!saw_errors()) 1337 error_at(this->location(), "unexpected end of file"); 1338 return; 1339 } 1340 1341 if (!this->peek_token()->is_op(OPERATOR_LPAREN)) 1342 (this->*pfn)(varg); 1343 else 1344 { 1345 if (!this->advance_token()->is_op(OPERATOR_RPAREN)) 1346 { 1347 this->list(pfn, varg, true); 1348 if (!this->peek_token()->is_op(OPERATOR_RPAREN)) 1349 { 1350 error_at(this->location(), "missing %<)%>"); 1351 while (!this->advance_token()->is_op(OPERATOR_RPAREN)) 1352 { 1353 if (this->peek_token()->is_eof()) 1354 return; 1355 } 1356 } 1357 } 1358 this->advance_token(); 1359 } 1360} 1361 1362// List<P> = P { ";" P } [ ";" ] . 1363 1364// In order to pick up the trailing semicolon we need to know what 1365// might follow. This is either a '}' or a ')'. 1366 1367void 1368Parse::list(void (Parse::*pfn)(void*), void* varg, bool follow_is_paren) 1369{ 1370 (this->*pfn)(varg); 1371 Operator follow = follow_is_paren ? OPERATOR_RPAREN : OPERATOR_RCURLY; 1372 while (this->peek_token()->is_op(OPERATOR_SEMICOLON) 1373 || this->peek_token()->is_op(OPERATOR_COMMA)) 1374 { 1375 if (this->peek_token()->is_op(OPERATOR_COMMA)) 1376 error_at(this->location(), "unexpected comma"); 1377 if (this->advance_token()->is_op(follow)) 1378 break; 1379 (this->*pfn)(varg); 1380 } 1381} 1382 1383// ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) . 1384 1385void 1386Parse::const_decl() 1387{ 1388 go_assert(this->peek_token()->is_keyword(KEYWORD_CONST)); 1389 this->advance_token(); 1390 this->reset_iota(); 1391 1392 Type* last_type = NULL; 1393 Expression_list* last_expr_list = NULL; 1394 1395 if (!this->peek_token()->is_op(OPERATOR_LPAREN)) 1396 this->const_spec(&last_type, &last_expr_list); 1397 else 1398 { 1399 this->advance_token(); 1400 while (!this->peek_token()->is_op(OPERATOR_RPAREN)) 1401 { 1402 this->const_spec(&last_type, &last_expr_list); 1403 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 1404 this->advance_token(); 1405 else if (!this->peek_token()->is_op(OPERATOR_RPAREN)) 1406 { 1407 error_at(this->location(), "expected %<;%> or %<)%> or newline"); 1408 if (!this->skip_past_error(OPERATOR_RPAREN)) 1409 return; 1410 } 1411 } 1412 this->advance_token(); 1413 } 1414 1415 if (last_expr_list != NULL) 1416 delete last_expr_list; 1417} 1418 1419// ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] . 1420 1421void 1422Parse::const_spec(Type** last_type, Expression_list** last_expr_list) 1423{ 1424 Typed_identifier_list til; 1425 this->identifier_list(&til); 1426 1427 Type* type = NULL; 1428 if (this->type_may_start_here()) 1429 { 1430 type = this->type(); 1431 *last_type = NULL; 1432 *last_expr_list = NULL; 1433 } 1434 1435 Expression_list *expr_list; 1436 if (!this->peek_token()->is_op(OPERATOR_EQ)) 1437 { 1438 if (*last_expr_list == NULL) 1439 { 1440 error_at(this->location(), "expected %<=%>"); 1441 return; 1442 } 1443 type = *last_type; 1444 expr_list = new Expression_list; 1445 for (Expression_list::const_iterator p = (*last_expr_list)->begin(); 1446 p != (*last_expr_list)->end(); 1447 ++p) 1448 expr_list->push_back((*p)->copy()); 1449 } 1450 else 1451 { 1452 this->advance_token(); 1453 expr_list = this->expression_list(NULL, false, true); 1454 *last_type = type; 1455 if (*last_expr_list != NULL) 1456 delete *last_expr_list; 1457 *last_expr_list = expr_list; 1458 } 1459 1460 Expression_list::const_iterator pe = expr_list->begin(); 1461 for (Typed_identifier_list::iterator pi = til.begin(); 1462 pi != til.end(); 1463 ++pi, ++pe) 1464 { 1465 if (pe == expr_list->end()) 1466 { 1467 error_at(this->location(), "not enough initializers"); 1468 return; 1469 } 1470 if (type != NULL) 1471 pi->set_type(type); 1472 1473 if (!Gogo::is_sink_name(pi->name())) 1474 this->gogo_->add_constant(*pi, *pe, this->iota_value()); 1475 else 1476 { 1477 static int count; 1478 char buf[30]; 1479 snprintf(buf, sizeof buf, ".$sinkconst%d", count); 1480 ++count; 1481 Typed_identifier ti(std::string(buf), type, pi->location()); 1482 Named_object* no = this->gogo_->add_constant(ti, *pe, this->iota_value()); 1483 no->const_value()->set_is_sink(); 1484 } 1485 } 1486 if (pe != expr_list->end()) 1487 error_at(this->location(), "too many initializers"); 1488 1489 this->increment_iota(); 1490 1491 return; 1492} 1493 1494// TypeDecl = "type" Decl<TypeSpec> . 1495 1496void 1497Parse::type_decl() 1498{ 1499 go_assert(this->peek_token()->is_keyword(KEYWORD_TYPE)); 1500 this->advance_token(); 1501 this->decl(&Parse::type_spec, NULL); 1502} 1503 1504// TypeSpec = identifier Type . 1505 1506void 1507Parse::type_spec(void*) 1508{ 1509 const Token* token = this->peek_token(); 1510 if (!token->is_identifier()) 1511 { 1512 error_at(this->location(), "expected identifier"); 1513 return; 1514 } 1515 std::string name = token->identifier(); 1516 bool is_exported = token->is_identifier_exported(); 1517 Location location = token->location(); 1518 token = this->advance_token(); 1519 1520 // The scope of the type name starts at the point where the 1521 // identifier appears in the source code. We implement this by 1522 // declaring the type before we read the type definition. 1523 Named_object* named_type = NULL; 1524 if (name != "_") 1525 { 1526 name = this->gogo_->pack_hidden_name(name, is_exported); 1527 named_type = this->gogo_->declare_type(name, location); 1528 } 1529 1530 Type* type; 1531 if (name == "_" && this->peek_token()->is_keyword(KEYWORD_INTERFACE)) 1532 { 1533 // We call Parse::interface_type explicity here because we do not want 1534 // to record an interface with a blank type name. 1535 type = this->interface_type(false); 1536 } 1537 else if (!this->peek_token()->is_op(OPERATOR_SEMICOLON)) 1538 type = this->type(); 1539 else 1540 { 1541 error_at(this->location(), 1542 "unexpected semicolon or newline in type declaration"); 1543 type = Type::make_error_type(); 1544 this->advance_token(); 1545 } 1546 1547 if (type->is_error_type()) 1548 { 1549 this->gogo_->mark_locals_used(); 1550 while (!this->peek_token()->is_op(OPERATOR_SEMICOLON) 1551 && !this->peek_token()->is_eof()) 1552 this->advance_token(); 1553 } 1554 1555 if (name != "_") 1556 { 1557 if (named_type->is_type_declaration()) 1558 { 1559 Type* ftype = type->forwarded(); 1560 if (ftype->forward_declaration_type() != NULL 1561 && (ftype->forward_declaration_type()->named_object() 1562 == named_type)) 1563 { 1564 error_at(location, "invalid recursive type"); 1565 type = Type::make_error_type(); 1566 } 1567 1568 this->gogo_->define_type(named_type, 1569 Type::make_named_type(named_type, type, 1570 location)); 1571 go_assert(named_type->package() == NULL); 1572 } 1573 else 1574 { 1575 // This will probably give a redefinition error. 1576 this->gogo_->add_type(name, type, location); 1577 } 1578 } 1579} 1580 1581// VarDecl = "var" Decl<VarSpec> . 1582 1583void 1584Parse::var_decl() 1585{ 1586 go_assert(this->peek_token()->is_keyword(KEYWORD_VAR)); 1587 this->advance_token(); 1588 this->decl(&Parse::var_spec, NULL); 1589} 1590 1591// VarSpec = IdentifierList 1592// ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) . 1593 1594void 1595Parse::var_spec(void*) 1596{ 1597 // Get the variable names. 1598 Typed_identifier_list til; 1599 this->identifier_list(&til); 1600 1601 Location location = this->location(); 1602 1603 Type* type = NULL; 1604 Expression_list* init = NULL; 1605 if (!this->peek_token()->is_op(OPERATOR_EQ)) 1606 { 1607 type = this->type(); 1608 if (type->is_error_type()) 1609 { 1610 this->gogo_->mark_locals_used(); 1611 while (!this->peek_token()->is_op(OPERATOR_EQ) 1612 && !this->peek_token()->is_op(OPERATOR_SEMICOLON) 1613 && !this->peek_token()->is_eof()) 1614 this->advance_token(); 1615 } 1616 if (this->peek_token()->is_op(OPERATOR_EQ)) 1617 { 1618 this->advance_token(); 1619 init = this->expression_list(NULL, false, true); 1620 } 1621 } 1622 else 1623 { 1624 this->advance_token(); 1625 init = this->expression_list(NULL, false, true); 1626 } 1627 1628 this->init_vars(&til, type, init, false, location); 1629 1630 if (init != NULL) 1631 delete init; 1632} 1633 1634// Create variables. TIL is a list of variable names. If TYPE is not 1635// NULL, it is the type of all the variables. If INIT is not NULL, it 1636// is an initializer list for the variables. 1637 1638void 1639Parse::init_vars(const Typed_identifier_list* til, Type* type, 1640 Expression_list* init, bool is_coloneq, 1641 Location location) 1642{ 1643 // Check for an initialization which can yield multiple values. 1644 if (init != NULL && init->size() == 1 && til->size() > 1) 1645 { 1646 if (this->init_vars_from_call(til, type, *init->begin(), is_coloneq, 1647 location)) 1648 return; 1649 if (this->init_vars_from_map(til, type, *init->begin(), is_coloneq, 1650 location)) 1651 return; 1652 if (this->init_vars_from_receive(til, type, *init->begin(), is_coloneq, 1653 location)) 1654 return; 1655 if (this->init_vars_from_type_guard(til, type, *init->begin(), 1656 is_coloneq, location)) 1657 return; 1658 } 1659 1660 if (init != NULL && init->size() != til->size()) 1661 { 1662 if (init->empty() || !init->front()->is_error_expression()) 1663 error_at(location, "wrong number of initializations"); 1664 init = NULL; 1665 if (type == NULL) 1666 type = Type::make_error_type(); 1667 } 1668 1669 // Note that INIT was already parsed with the old name bindings, so 1670 // we don't have to worry that it will accidentally refer to the 1671 // newly declared variables. But we do have to worry about a mix of 1672 // newly declared variables and old variables if the old variables 1673 // appear in the initializations. 1674 1675 Expression_list::const_iterator pexpr; 1676 if (init != NULL) 1677 pexpr = init->begin(); 1678 bool any_new = false; 1679 Expression_list* vars = new Expression_list(); 1680 Expression_list* vals = new Expression_list(); 1681 for (Typed_identifier_list::const_iterator p = til->begin(); 1682 p != til->end(); 1683 ++p) 1684 { 1685 if (init != NULL) 1686 go_assert(pexpr != init->end()); 1687 this->init_var(*p, type, init == NULL ? NULL : *pexpr, is_coloneq, 1688 false, &any_new, vars, vals); 1689 if (init != NULL) 1690 ++pexpr; 1691 } 1692 if (init != NULL) 1693 go_assert(pexpr == init->end()); 1694 if (is_coloneq && !any_new) 1695 error_at(location, "variables redeclared but no variable is new"); 1696 this->finish_init_vars(vars, vals, location); 1697} 1698 1699// See if we need to initialize a list of variables from a function 1700// call. This returns true if we have set up the variables and the 1701// initialization. 1702 1703bool 1704Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type, 1705 Expression* expr, bool is_coloneq, 1706 Location location) 1707{ 1708 Call_expression* call = expr->call_expression(); 1709 if (call == NULL) 1710 return false; 1711 1712 // This is a function call. We can't check here whether it returns 1713 // the right number of values, but it might. Declare the variables, 1714 // and then assign the results of the call to them. 1715 1716 call->set_expected_result_count(vars->size()); 1717 1718 Named_object* first_var = NULL; 1719 unsigned int index = 0; 1720 bool any_new = false; 1721 Expression_list* ivars = new Expression_list(); 1722 Expression_list* ivals = new Expression_list(); 1723 for (Typed_identifier_list::const_iterator pv = vars->begin(); 1724 pv != vars->end(); 1725 ++pv, ++index) 1726 { 1727 Expression* init = Expression::make_call_result(call, index); 1728 Named_object* no = this->init_var(*pv, type, init, is_coloneq, false, 1729 &any_new, ivars, ivals); 1730 1731 if (this->gogo_->in_global_scope() && no->is_variable()) 1732 { 1733 if (first_var == NULL) 1734 first_var = no; 1735 else 1736 { 1737 // The subsequent vars have an implicit dependency on 1738 // the first one, so that everything gets initialized in 1739 // the right order and so that we detect cycles 1740 // correctly. 1741 this->gogo_->record_var_depends_on(no->var_value(), first_var); 1742 } 1743 } 1744 } 1745 1746 if (is_coloneq && !any_new) 1747 error_at(location, "variables redeclared but no variable is new"); 1748 1749 this->finish_init_vars(ivars, ivals, location); 1750 1751 return true; 1752} 1753 1754// See if we need to initialize a pair of values from a map index 1755// expression. This returns true if we have set up the variables and 1756// the initialization. 1757 1758bool 1759Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type, 1760 Expression* expr, bool is_coloneq, 1761 Location location) 1762{ 1763 Index_expression* index = expr->index_expression(); 1764 if (index == NULL) 1765 return false; 1766 if (vars->size() != 2) 1767 return false; 1768 1769 // This is an index which is being assigned to two variables. It 1770 // must be a map index. Declare the variables, and then assign the 1771 // results of the map index. 1772 bool any_new = false; 1773 Typed_identifier_list::const_iterator p = vars->begin(); 1774 Expression* init = type == NULL ? index : NULL; 1775 Named_object* val_no = this->init_var(*p, type, init, is_coloneq, 1776 type == NULL, &any_new, NULL, NULL); 1777 if (type == NULL && any_new && val_no->is_variable()) 1778 val_no->var_value()->set_type_from_init_tuple(); 1779 Expression* val_var = Expression::make_var_reference(val_no, location); 1780 1781 ++p; 1782 Type* var_type = type; 1783 if (var_type == NULL) 1784 var_type = Type::lookup_bool_type(); 1785 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false, 1786 &any_new, NULL, NULL); 1787 Expression* present_var = Expression::make_var_reference(no, location); 1788 1789 if (is_coloneq && !any_new) 1790 error_at(location, "variables redeclared but no variable is new"); 1791 1792 Statement* s = Statement::make_tuple_map_assignment(val_var, present_var, 1793 index, location); 1794 1795 if (!this->gogo_->in_global_scope()) 1796 this->gogo_->add_statement(s); 1797 else if (!val_no->is_sink()) 1798 { 1799 if (val_no->is_variable()) 1800 val_no->var_value()->add_preinit_statement(this->gogo_, s); 1801 } 1802 else if (!no->is_sink()) 1803 { 1804 if (no->is_variable()) 1805 no->var_value()->add_preinit_statement(this->gogo_, s); 1806 } 1807 else 1808 { 1809 // Execute the map index expression just so that we can fail if 1810 // the map is nil. 1811 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(), 1812 NULL, location); 1813 dummy->var_value()->add_preinit_statement(this->gogo_, s); 1814 } 1815 1816 return true; 1817} 1818 1819// See if we need to initialize a pair of values from a receive 1820// expression. This returns true if we have set up the variables and 1821// the initialization. 1822 1823bool 1824Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type, 1825 Expression* expr, bool is_coloneq, 1826 Location location) 1827{ 1828 Receive_expression* receive = expr->receive_expression(); 1829 if (receive == NULL) 1830 return false; 1831 if (vars->size() != 2) 1832 return false; 1833 1834 // This is a receive expression which is being assigned to two 1835 // variables. Declare the variables, and then assign the results of 1836 // the receive. 1837 bool any_new = false; 1838 Typed_identifier_list::const_iterator p = vars->begin(); 1839 Expression* init = type == NULL ? receive : NULL; 1840 Named_object* val_no = this->init_var(*p, type, init, is_coloneq, 1841 type == NULL, &any_new, NULL, NULL); 1842 if (type == NULL && any_new && val_no->is_variable()) 1843 val_no->var_value()->set_type_from_init_tuple(); 1844 Expression* val_var = Expression::make_var_reference(val_no, location); 1845 1846 ++p; 1847 Type* var_type = type; 1848 if (var_type == NULL) 1849 var_type = Type::lookup_bool_type(); 1850 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false, 1851 &any_new, NULL, NULL); 1852 Expression* received_var = Expression::make_var_reference(no, location); 1853 1854 if (is_coloneq && !any_new) 1855 error_at(location, "variables redeclared but no variable is new"); 1856 1857 Statement* s = Statement::make_tuple_receive_assignment(val_var, 1858 received_var, 1859 receive->channel(), 1860 location); 1861 1862 if (!this->gogo_->in_global_scope()) 1863 this->gogo_->add_statement(s); 1864 else if (!val_no->is_sink()) 1865 { 1866 if (val_no->is_variable()) 1867 val_no->var_value()->add_preinit_statement(this->gogo_, s); 1868 } 1869 else if (!no->is_sink()) 1870 { 1871 if (no->is_variable()) 1872 no->var_value()->add_preinit_statement(this->gogo_, s); 1873 } 1874 else 1875 { 1876 Named_object* dummy = this->create_dummy_global(Type::lookup_bool_type(), 1877 NULL, location); 1878 dummy->var_value()->add_preinit_statement(this->gogo_, s); 1879 } 1880 1881 return true; 1882} 1883 1884// See if we need to initialize a pair of values from a type guard 1885// expression. This returns true if we have set up the variables and 1886// the initialization. 1887 1888bool 1889Parse::init_vars_from_type_guard(const Typed_identifier_list* vars, 1890 Type* type, Expression* expr, 1891 bool is_coloneq, Location location) 1892{ 1893 Type_guard_expression* type_guard = expr->type_guard_expression(); 1894 if (type_guard == NULL) 1895 return false; 1896 if (vars->size() != 2) 1897 return false; 1898 1899 // This is a type guard expression which is being assigned to two 1900 // variables. Declare the variables, and then assign the results of 1901 // the type guard. 1902 bool any_new = false; 1903 Typed_identifier_list::const_iterator p = vars->begin(); 1904 Type* var_type = type; 1905 if (var_type == NULL) 1906 var_type = type_guard->type(); 1907 Named_object* val_no = this->init_var(*p, var_type, NULL, is_coloneq, false, 1908 &any_new, NULL, NULL); 1909 Expression* val_var = Expression::make_var_reference(val_no, location); 1910 1911 ++p; 1912 var_type = type; 1913 if (var_type == NULL) 1914 var_type = Type::lookup_bool_type(); 1915 Named_object* no = this->init_var(*p, var_type, NULL, is_coloneq, false, 1916 &any_new, NULL, NULL); 1917 Expression* ok_var = Expression::make_var_reference(no, location); 1918 1919 Expression* texpr = type_guard->expr(); 1920 Type* t = type_guard->type(); 1921 Statement* s = Statement::make_tuple_type_guard_assignment(val_var, ok_var, 1922 texpr, t, 1923 location); 1924 1925 if (is_coloneq && !any_new) 1926 error_at(location, "variables redeclared but no variable is new"); 1927 1928 if (!this->gogo_->in_global_scope()) 1929 this->gogo_->add_statement(s); 1930 else if (!val_no->is_sink()) 1931 { 1932 if (val_no->is_variable()) 1933 val_no->var_value()->add_preinit_statement(this->gogo_, s); 1934 } 1935 else if (!no->is_sink()) 1936 { 1937 if (no->is_variable()) 1938 no->var_value()->add_preinit_statement(this->gogo_, s); 1939 } 1940 else 1941 { 1942 Named_object* dummy = this->create_dummy_global(type, NULL, location); 1943 dummy->var_value()->add_preinit_statement(this->gogo_, s); 1944 } 1945 1946 return true; 1947} 1948 1949// Create a single variable. If IS_COLONEQ is true, we permit 1950// redeclarations in the same block, and we set *IS_NEW when we find a 1951// new variable which is not a redeclaration. 1952 1953Named_object* 1954Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init, 1955 bool is_coloneq, bool type_from_init, bool* is_new, 1956 Expression_list* vars, Expression_list* vals) 1957{ 1958 Location location = tid.location(); 1959 1960 if (Gogo::is_sink_name(tid.name())) 1961 { 1962 if (!type_from_init && init != NULL) 1963 { 1964 if (this->gogo_->in_global_scope()) 1965 return this->create_dummy_global(type, init, location); 1966 else 1967 { 1968 // Create a dummy variable so that we will check whether the 1969 // initializer can be assigned to the type. 1970 Variable* var = new Variable(type, init, false, false, false, 1971 location); 1972 var->set_is_used(); 1973 static int count; 1974 char buf[30]; 1975 snprintf(buf, sizeof buf, "sink$%d", count); 1976 ++count; 1977 return this->gogo_->add_variable(buf, var); 1978 } 1979 } 1980 if (type != NULL) 1981 this->gogo_->add_type_to_verify(type); 1982 return this->gogo_->add_sink(); 1983 } 1984 1985 if (is_coloneq) 1986 { 1987 Named_object* no = this->gogo_->lookup_in_block(tid.name()); 1988 if (no != NULL 1989 && (no->is_variable() || no->is_result_variable())) 1990 { 1991 // INIT may be NULL even when IS_COLONEQ is true for cases 1992 // like v, ok := x.(int). 1993 if (!type_from_init && init != NULL) 1994 { 1995 go_assert(vars != NULL && vals != NULL); 1996 vars->push_back(Expression::make_var_reference(no, location)); 1997 vals->push_back(init); 1998 } 1999 return no; 2000 } 2001 } 2002 *is_new = true; 2003 Variable* var = new Variable(type, init, this->gogo_->in_global_scope(), 2004 false, false, location); 2005 Named_object* no = this->gogo_->add_variable(tid.name(), var); 2006 if (!no->is_variable()) 2007 { 2008 // The name is already defined, so we just gave an error. 2009 return this->gogo_->add_sink(); 2010 } 2011 return no; 2012} 2013 2014// Create a dummy global variable to force an initializer to be run in 2015// the right place. This is used when a sink variable is initialized 2016// at global scope. 2017 2018Named_object* 2019Parse::create_dummy_global(Type* type, Expression* init, 2020 Location location) 2021{ 2022 if (type == NULL && init == NULL) 2023 type = Type::lookup_bool_type(); 2024 Variable* var = new Variable(type, init, true, false, false, location); 2025 static int count; 2026 char buf[30]; 2027 snprintf(buf, sizeof buf, "_.%d", count); 2028 ++count; 2029 return this->gogo_->add_variable(buf, var); 2030} 2031 2032// Finish the variable initialization by executing any assignments to 2033// existing variables when using :=. These must be done as a tuple 2034// assignment in case of something like n, a, b := 1, b, a. 2035 2036void 2037Parse::finish_init_vars(Expression_list* vars, Expression_list* vals, 2038 Location location) 2039{ 2040 if (vars->empty()) 2041 { 2042 delete vars; 2043 delete vals; 2044 } 2045 else if (vars->size() == 1) 2046 { 2047 go_assert(!this->gogo_->in_global_scope()); 2048 this->gogo_->add_statement(Statement::make_assignment(vars->front(), 2049 vals->front(), 2050 location)); 2051 delete vars; 2052 delete vals; 2053 } 2054 else 2055 { 2056 go_assert(!this->gogo_->in_global_scope()); 2057 this->gogo_->add_statement(Statement::make_tuple_assignment(vars, vals, 2058 location)); 2059 } 2060} 2061 2062// SimpleVarDecl = identifier ":=" Expression . 2063 2064// We've already seen the identifier. 2065 2066// FIXME: We also have to implement 2067// IdentifierList ":=" ExpressionList 2068// In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept 2069// tuple assignments here as well. 2070 2071// If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand 2072// side may be a composite literal. 2073 2074// If P_RANGE_CLAUSE is not NULL, then this will recognize a 2075// RangeClause. 2076 2077// If P_TYPE_SWITCH is not NULL, this will recognize a type switch 2078// guard (var := expr.("type") using the literal keyword "type"). 2079 2080void 2081Parse::simple_var_decl_or_assignment(const std::string& name, 2082 Location location, 2083 bool may_be_composite_lit, 2084 Range_clause* p_range_clause, 2085 Type_switch* p_type_switch) 2086{ 2087 Typed_identifier_list til; 2088 til.push_back(Typed_identifier(name, NULL, location)); 2089 2090 std::set<std::string> uniq_idents; 2091 uniq_idents.insert(name); 2092 2093 // We've seen one identifier. If we see a comma now, this could be 2094 // "a, *p = 1, 2". 2095 if (this->peek_token()->is_op(OPERATOR_COMMA)) 2096 { 2097 go_assert(p_type_switch == NULL); 2098 while (true) 2099 { 2100 const Token* token = this->advance_token(); 2101 if (!token->is_identifier()) 2102 break; 2103 2104 std::string id = token->identifier(); 2105 bool is_id_exported = token->is_identifier_exported(); 2106 Location id_location = token->location(); 2107 std::pair<std::set<std::string>::iterator, bool> ins; 2108 2109 token = this->advance_token(); 2110 if (!token->is_op(OPERATOR_COMMA)) 2111 { 2112 if (token->is_op(OPERATOR_COLONEQ)) 2113 { 2114 id = this->gogo_->pack_hidden_name(id, is_id_exported); 2115 ins = uniq_idents.insert(id); 2116 if (!ins.second && !Gogo::is_sink_name(id)) 2117 error_at(id_location, "multiple assignments to %s", 2118 Gogo::message_name(id).c_str()); 2119 til.push_back(Typed_identifier(id, NULL, location)); 2120 } 2121 else 2122 this->unget_token(Token::make_identifier_token(id, 2123 is_id_exported, 2124 id_location)); 2125 break; 2126 } 2127 2128 id = this->gogo_->pack_hidden_name(id, is_id_exported); 2129 ins = uniq_idents.insert(id); 2130 if (!ins.second && !Gogo::is_sink_name(id)) 2131 error_at(id_location, "multiple assignments to %s", 2132 Gogo::message_name(id).c_str()); 2133 til.push_back(Typed_identifier(id, NULL, location)); 2134 } 2135 2136 // We have a comma separated list of identifiers in TIL. If the 2137 // next token is COLONEQ, then this is a simple var decl, and we 2138 // have the complete list of identifiers. If the next token is 2139 // not COLONEQ, then the only valid parse is a tuple assignment. 2140 // The list of identifiers we have so far is really a list of 2141 // expressions. There are more expressions following. 2142 2143 if (!this->peek_token()->is_op(OPERATOR_COLONEQ)) 2144 { 2145 Expression_list* exprs = new Expression_list; 2146 for (Typed_identifier_list::const_iterator p = til.begin(); 2147 p != til.end(); 2148 ++p) 2149 exprs->push_back(this->id_to_expression(p->name(), p->location(), 2150 true)); 2151 2152 Expression_list* more_exprs = 2153 this->expression_list(NULL, true, may_be_composite_lit); 2154 for (Expression_list::const_iterator p = more_exprs->begin(); 2155 p != more_exprs->end(); 2156 ++p) 2157 exprs->push_back(*p); 2158 delete more_exprs; 2159 2160 this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause); 2161 return; 2162 } 2163 } 2164 2165 go_assert(this->peek_token()->is_op(OPERATOR_COLONEQ)); 2166 const Token* token = this->advance_token(); 2167 2168 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE)) 2169 { 2170 this->range_clause_decl(&til, p_range_clause); 2171 return; 2172 } 2173 2174 Expression_list* init; 2175 if (p_type_switch == NULL) 2176 init = this->expression_list(NULL, false, may_be_composite_lit); 2177 else 2178 { 2179 bool is_type_switch = false; 2180 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, 2181 may_be_composite_lit, 2182 &is_type_switch, NULL); 2183 if (is_type_switch) 2184 { 2185 p_type_switch->found = true; 2186 p_type_switch->name = name; 2187 p_type_switch->location = location; 2188 p_type_switch->expr = expr; 2189 return; 2190 } 2191 2192 if (!this->peek_token()->is_op(OPERATOR_COMMA)) 2193 { 2194 init = new Expression_list(); 2195 init->push_back(expr); 2196 } 2197 else 2198 { 2199 this->advance_token(); 2200 init = this->expression_list(expr, false, may_be_composite_lit); 2201 } 2202 } 2203 2204 this->init_vars(&til, NULL, init, true, location); 2205} 2206 2207// FunctionDecl = "func" identifier Signature [ Block ] . 2208// MethodDecl = "func" Receiver identifier Signature [ Block ] . 2209 2210// Deprecated gcc extension: 2211// FunctionDecl = "func" identifier Signature 2212// __asm__ "(" string_lit ")" . 2213// This extension means a function whose real name is the identifier 2214// inside the asm. This extension will be removed at some future 2215// date. It has been replaced with //extern comments. 2216 2217// SAW_NOINTERFACE is true if we saw a magic //go:nointerface comment, 2218// which means that we omit the method from the type descriptor. 2219 2220void 2221Parse::function_decl(bool saw_nointerface) 2222{ 2223 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC)); 2224 Location location = this->location(); 2225 std::string extern_name = this->lex_->extern_name(); 2226 const Token* token = this->advance_token(); 2227 2228 Typed_identifier* rec = NULL; 2229 if (token->is_op(OPERATOR_LPAREN)) 2230 { 2231 rec = this->receiver(); 2232 token = this->peek_token(); 2233 } 2234 else if (saw_nointerface) 2235 { 2236 warning_at(location, 0, 2237 "ignoring magic //go:nointerface comment before non-method"); 2238 saw_nointerface = false; 2239 } 2240 2241 if (!token->is_identifier()) 2242 { 2243 error_at(this->location(), "expected function name"); 2244 return; 2245 } 2246 2247 std::string name = 2248 this->gogo_->pack_hidden_name(token->identifier(), 2249 token->is_identifier_exported()); 2250 2251 this->advance_token(); 2252 2253 Function_type* fntype = this->signature(rec, this->location()); 2254 2255 Named_object* named_object = NULL; 2256 2257 if (this->peek_token()->is_keyword(KEYWORD_ASM)) 2258 { 2259 if (!this->advance_token()->is_op(OPERATOR_LPAREN)) 2260 { 2261 error_at(this->location(), "expected %<(%>"); 2262 return; 2263 } 2264 token = this->advance_token(); 2265 if (!token->is_string()) 2266 { 2267 error_at(this->location(), "expected string"); 2268 return; 2269 } 2270 std::string asm_name = token->string_value(); 2271 if (!this->advance_token()->is_op(OPERATOR_RPAREN)) 2272 { 2273 error_at(this->location(), "expected %<)%>"); 2274 return; 2275 } 2276 this->advance_token(); 2277 if (!Gogo::is_sink_name(name)) 2278 { 2279 named_object = this->gogo_->declare_function(name, fntype, location); 2280 if (named_object->is_function_declaration()) 2281 named_object->func_declaration_value()->set_asm_name(asm_name); 2282 } 2283 } 2284 2285 // Check for the easy error of a newline before the opening brace. 2286 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 2287 { 2288 Location semi_loc = this->location(); 2289 if (this->advance_token()->is_op(OPERATOR_LCURLY)) 2290 error_at(this->location(), 2291 "unexpected semicolon or newline before %<{%>"); 2292 else 2293 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON, 2294 semi_loc)); 2295 } 2296 2297 if (!this->peek_token()->is_op(OPERATOR_LCURLY)) 2298 { 2299 if (named_object == NULL && !Gogo::is_sink_name(name)) 2300 { 2301 if (fntype == NULL) 2302 this->gogo_->add_erroneous_name(name); 2303 else 2304 { 2305 named_object = this->gogo_->declare_function(name, fntype, 2306 location); 2307 if (!extern_name.empty() 2308 && named_object->is_function_declaration()) 2309 { 2310 Function_declaration* fd = 2311 named_object->func_declaration_value(); 2312 fd->set_asm_name(extern_name); 2313 } 2314 } 2315 } 2316 2317 if (saw_nointerface) 2318 warning_at(location, 0, 2319 ("ignoring magic //go:nointerface comment " 2320 "before declaration")); 2321 } 2322 else 2323 { 2324 bool hold_is_erroneous_function = this->is_erroneous_function_; 2325 if (fntype == NULL) 2326 { 2327 fntype = Type::make_function_type(NULL, NULL, NULL, location); 2328 this->is_erroneous_function_ = true; 2329 if (!Gogo::is_sink_name(name)) 2330 this->gogo_->add_erroneous_name(name); 2331 name = this->gogo_->pack_hidden_name("_", false); 2332 } 2333 named_object = this->gogo_->start_function(name, fntype, true, location); 2334 Location end_loc = this->block(); 2335 this->gogo_->finish_function(end_loc); 2336 if (saw_nointerface 2337 && !this->is_erroneous_function_ 2338 && named_object->is_function()) 2339 named_object->func_value()->set_nointerface(); 2340 this->is_erroneous_function_ = hold_is_erroneous_function; 2341 } 2342} 2343 2344// Receiver = Parameters . 2345 2346Typed_identifier* 2347Parse::receiver() 2348{ 2349 Location location = this->location(); 2350 Typed_identifier_list* til; 2351 if (!this->parameters(&til, NULL)) 2352 return NULL; 2353 else if (til == NULL || til->empty()) 2354 { 2355 error_at(location, "method has no receiver"); 2356 return NULL; 2357 } 2358 else if (til->size() > 1) 2359 { 2360 error_at(location, "method has multiple receivers"); 2361 return NULL; 2362 } 2363 else 2364 return &til->front(); 2365} 2366 2367// Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" . 2368// Literal = BasicLit | CompositeLit | FunctionLit . 2369// BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit . 2370 2371// If MAY_BE_SINK is true, this operand may be "_". 2372 2373// If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true 2374// if the entire expression is in parentheses. 2375 2376Expression* 2377Parse::operand(bool may_be_sink, bool* is_parenthesized) 2378{ 2379 const Token* token = this->peek_token(); 2380 Expression* ret; 2381 switch (token->classification()) 2382 { 2383 case Token::TOKEN_IDENTIFIER: 2384 { 2385 Location location = token->location(); 2386 std::string id = token->identifier(); 2387 bool is_exported = token->is_identifier_exported(); 2388 std::string packed = this->gogo_->pack_hidden_name(id, is_exported); 2389 2390 Named_object* in_function; 2391 Named_object* named_object = this->gogo_->lookup(packed, &in_function); 2392 2393 Package* package = NULL; 2394 if (named_object != NULL && named_object->is_package()) 2395 { 2396 if (!this->advance_token()->is_op(OPERATOR_DOT) 2397 || !this->advance_token()->is_identifier()) 2398 { 2399 error_at(location, "unexpected reference to package"); 2400 return Expression::make_error(location); 2401 } 2402 package = named_object->package_value(); 2403 package->note_usage(); 2404 id = this->peek_token()->identifier(); 2405 is_exported = this->peek_token()->is_identifier_exported(); 2406 packed = this->gogo_->pack_hidden_name(id, is_exported); 2407 named_object = package->lookup(packed); 2408 location = this->location(); 2409 go_assert(in_function == NULL); 2410 } 2411 2412 this->advance_token(); 2413 2414 if (named_object != NULL 2415 && named_object->is_type() 2416 && !named_object->type_value()->is_visible()) 2417 { 2418 go_assert(package != NULL); 2419 error_at(location, "invalid reference to hidden type %<%s.%s%>", 2420 Gogo::message_name(package->package_name()).c_str(), 2421 Gogo::message_name(id).c_str()); 2422 return Expression::make_error(location); 2423 } 2424 2425 2426 if (named_object == NULL) 2427 { 2428 if (package != NULL) 2429 { 2430 std::string n1 = Gogo::message_name(package->package_name()); 2431 std::string n2 = Gogo::message_name(id); 2432 if (!is_exported) 2433 error_at(location, 2434 ("invalid reference to unexported identifier " 2435 "%<%s.%s%>"), 2436 n1.c_str(), n2.c_str()); 2437 else 2438 error_at(location, 2439 "reference to undefined identifier %<%s.%s%>", 2440 n1.c_str(), n2.c_str()); 2441 return Expression::make_error(location); 2442 } 2443 2444 named_object = this->gogo_->add_unknown_name(packed, location); 2445 } 2446 2447 if (in_function != NULL 2448 && in_function != this->gogo_->current_function() 2449 && (named_object->is_variable() 2450 || named_object->is_result_variable())) 2451 return this->enclosing_var_reference(in_function, named_object, 2452 may_be_sink, location); 2453 2454 switch (named_object->classification()) 2455 { 2456 case Named_object::NAMED_OBJECT_CONST: 2457 return Expression::make_const_reference(named_object, location); 2458 case Named_object::NAMED_OBJECT_TYPE: 2459 return Expression::make_type(named_object->type_value(), location); 2460 case Named_object::NAMED_OBJECT_TYPE_DECLARATION: 2461 { 2462 Type* t = Type::make_forward_declaration(named_object); 2463 return Expression::make_type(t, location); 2464 } 2465 case Named_object::NAMED_OBJECT_VAR: 2466 case Named_object::NAMED_OBJECT_RESULT_VAR: 2467 // Any left-hand-side can be a sink, so if this can not be 2468 // a sink, then it must be a use of the variable. 2469 if (!may_be_sink) 2470 this->mark_var_used(named_object); 2471 return Expression::make_var_reference(named_object, location); 2472 case Named_object::NAMED_OBJECT_SINK: 2473 if (may_be_sink) 2474 return Expression::make_sink(location); 2475 else 2476 { 2477 error_at(location, "cannot use _ as value"); 2478 return Expression::make_error(location); 2479 } 2480 case Named_object::NAMED_OBJECT_FUNC: 2481 case Named_object::NAMED_OBJECT_FUNC_DECLARATION: 2482 return Expression::make_func_reference(named_object, NULL, 2483 location); 2484 case Named_object::NAMED_OBJECT_UNKNOWN: 2485 { 2486 Unknown_expression* ue = 2487 Expression::make_unknown_reference(named_object, location); 2488 if (this->is_erroneous_function_) 2489 ue->set_no_error_message(); 2490 return ue; 2491 } 2492 case Named_object::NAMED_OBJECT_ERRONEOUS: 2493 return Expression::make_error(location); 2494 default: 2495 go_unreachable(); 2496 } 2497 } 2498 go_unreachable(); 2499 2500 case Token::TOKEN_STRING: 2501 ret = Expression::make_string(token->string_value(), token->location()); 2502 this->advance_token(); 2503 return ret; 2504 2505 case Token::TOKEN_CHARACTER: 2506 ret = Expression::make_character(token->character_value(), NULL, 2507 token->location()); 2508 this->advance_token(); 2509 return ret; 2510 2511 case Token::TOKEN_INTEGER: 2512 ret = Expression::make_integer_z(token->integer_value(), NULL, 2513 token->location()); 2514 this->advance_token(); 2515 return ret; 2516 2517 case Token::TOKEN_FLOAT: 2518 ret = Expression::make_float(token->float_value(), NULL, 2519 token->location()); 2520 this->advance_token(); 2521 return ret; 2522 2523 case Token::TOKEN_IMAGINARY: 2524 { 2525 mpfr_t zero; 2526 mpfr_init_set_ui(zero, 0, GMP_RNDN); 2527 mpc_t val; 2528 mpc_init2(val, mpc_precision); 2529 mpc_set_fr_fr(val, zero, *token->imaginary_value(), MPC_RNDNN); 2530 mpfr_clear(zero); 2531 ret = Expression::make_complex(&val, NULL, token->location()); 2532 mpc_clear(val); 2533 this->advance_token(); 2534 return ret; 2535 } 2536 2537 case Token::TOKEN_KEYWORD: 2538 switch (token->keyword()) 2539 { 2540 case KEYWORD_FUNC: 2541 return this->function_lit(); 2542 case KEYWORD_CHAN: 2543 case KEYWORD_INTERFACE: 2544 case KEYWORD_MAP: 2545 case KEYWORD_STRUCT: 2546 { 2547 Location location = token->location(); 2548 return Expression::make_type(this->type(), location); 2549 } 2550 default: 2551 break; 2552 } 2553 break; 2554 2555 case Token::TOKEN_OPERATOR: 2556 if (token->is_op(OPERATOR_LPAREN)) 2557 { 2558 this->advance_token(); 2559 ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL, 2560 NULL); 2561 if (!this->peek_token()->is_op(OPERATOR_RPAREN)) 2562 error_at(this->location(), "missing %<)%>"); 2563 else 2564 this->advance_token(); 2565 if (is_parenthesized != NULL) 2566 *is_parenthesized = true; 2567 return ret; 2568 } 2569 else if (token->is_op(OPERATOR_LSQUARE)) 2570 { 2571 // Here we call array_type directly, as this is the only 2572 // case where an ellipsis is permitted for an array type. 2573 Location location = token->location(); 2574 return Expression::make_type(this->array_type(true), location); 2575 } 2576 break; 2577 2578 default: 2579 break; 2580 } 2581 2582 error_at(this->location(), "expected operand"); 2583 return Expression::make_error(this->location()); 2584} 2585 2586// Handle a reference to a variable in an enclosing function. We add 2587// it to a list of such variables. We return a reference to a field 2588// in a struct which will be passed on the static chain when calling 2589// the current function. 2590 2591Expression* 2592Parse::enclosing_var_reference(Named_object* in_function, Named_object* var, 2593 bool may_be_sink, Location location) 2594{ 2595 go_assert(var->is_variable() || var->is_result_variable()); 2596 2597 // Any left-hand-side can be a sink, so if this can not be 2598 // a sink, then it must be a use of the variable. 2599 if (!may_be_sink) 2600 this->mark_var_used(var); 2601 2602 Named_object* this_function = this->gogo_->current_function(); 2603 Named_object* closure = this_function->func_value()->closure_var(); 2604 2605 // The last argument to the Enclosing_var constructor is the index 2606 // of this variable in the closure. We add 1 to the current number 2607 // of enclosed variables, because the first field in the closure 2608 // points to the function code. 2609 Enclosing_var ev(var, in_function, this->enclosing_vars_.size() + 1); 2610 std::pair<Enclosing_vars::iterator, bool> ins = 2611 this->enclosing_vars_.insert(ev); 2612 if (ins.second) 2613 { 2614 // This is a variable we have not seen before. Add a new field 2615 // to the closure type. 2616 this_function->func_value()->add_closure_field(var, location); 2617 } 2618 2619 Expression* closure_ref = Expression::make_var_reference(closure, 2620 location); 2621 closure_ref = Expression::make_unary(OPERATOR_MULT, closure_ref, location); 2622 2623 // The closure structure holds pointers to the variables, so we need 2624 // to introduce an indirection. 2625 Expression* e = Expression::make_field_reference(closure_ref, 2626 ins.first->index(), 2627 location); 2628 e = Expression::make_unary(OPERATOR_MULT, e, location); 2629 return e; 2630} 2631 2632// CompositeLit = LiteralType LiteralValue . 2633// LiteralType = StructType | ArrayType | "[" "..." "]" ElementType | 2634// SliceType | MapType | TypeName . 2635// LiteralValue = "{" [ ElementList [ "," ] ] "}" . 2636// ElementList = Element { "," Element } . 2637// Element = [ Key ":" ] Value . 2638// Key = FieldName | ElementIndex . 2639// FieldName = identifier . 2640// ElementIndex = Expression . 2641// Value = Expression | LiteralValue . 2642 2643// We have already seen the type if there is one, and we are now 2644// looking at the LiteralValue. The case "[" "..." "]" ElementType 2645// will be seen here as an array type whose length is "nil". The 2646// DEPTH parameter is non-zero if this is an embedded composite 2647// literal and the type was omitted. It gives the number of steps up 2648// to the type which was provided. E.g., in [][]int{{1}} it will be 2649// 1. In [][][]int{{{1}}} it will be 2. 2650 2651Expression* 2652Parse::composite_lit(Type* type, int depth, Location location) 2653{ 2654 go_assert(this->peek_token()->is_op(OPERATOR_LCURLY)); 2655 this->advance_token(); 2656 2657 if (this->peek_token()->is_op(OPERATOR_RCURLY)) 2658 { 2659 this->advance_token(); 2660 return Expression::make_composite_literal(type, depth, false, NULL, 2661 false, location); 2662 } 2663 2664 bool has_keys = false; 2665 bool all_are_names = true; 2666 Expression_list* vals = new Expression_list; 2667 while (true) 2668 { 2669 Expression* val; 2670 bool is_type_omitted = false; 2671 bool is_name = false; 2672 2673 const Token* token = this->peek_token(); 2674 2675 if (token->is_identifier()) 2676 { 2677 std::string identifier = token->identifier(); 2678 bool is_exported = token->is_identifier_exported(); 2679 Location location = token->location(); 2680 2681 if (this->advance_token()->is_op(OPERATOR_COLON)) 2682 { 2683 // This may be a field name. We don't know for sure--it 2684 // could also be an expression for an array index. We 2685 // don't want to parse it as an expression because may 2686 // trigger various errors, e.g., if this identifier 2687 // happens to be the name of a package. 2688 Gogo* gogo = this->gogo_; 2689 val = this->id_to_expression(gogo->pack_hidden_name(identifier, 2690 is_exported), 2691 location, false); 2692 is_name = true; 2693 } 2694 else 2695 { 2696 this->unget_token(Token::make_identifier_token(identifier, 2697 is_exported, 2698 location)); 2699 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, 2700 NULL); 2701 } 2702 } 2703 else if (!token->is_op(OPERATOR_LCURLY)) 2704 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 2705 else 2706 { 2707 // This must be a composite literal inside another composite 2708 // literal, with the type omitted for the inner one. 2709 val = this->composite_lit(type, depth + 1, token->location()); 2710 is_type_omitted = true; 2711 } 2712 2713 token = this->peek_token(); 2714 if (!token->is_op(OPERATOR_COLON)) 2715 { 2716 if (has_keys) 2717 vals->push_back(NULL); 2718 is_name = false; 2719 } 2720 else 2721 { 2722 if (is_type_omitted && !val->is_error_expression()) 2723 { 2724 error_at(this->location(), "unexpected %<:%>"); 2725 val = Expression::make_error(this->location()); 2726 } 2727 2728 this->advance_token(); 2729 2730 if (!has_keys && !vals->empty()) 2731 { 2732 Expression_list* newvals = new Expression_list; 2733 for (Expression_list::const_iterator p = vals->begin(); 2734 p != vals->end(); 2735 ++p) 2736 { 2737 newvals->push_back(NULL); 2738 newvals->push_back(*p); 2739 } 2740 delete vals; 2741 vals = newvals; 2742 } 2743 has_keys = true; 2744 2745 if (val->unknown_expression() != NULL) 2746 val->unknown_expression()->set_is_composite_literal_key(); 2747 2748 vals->push_back(val); 2749 2750 if (!token->is_op(OPERATOR_LCURLY)) 2751 val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 2752 else 2753 { 2754 // This must be a composite literal inside another 2755 // composite literal, with the type omitted for the 2756 // inner one. 2757 val = this->composite_lit(type, depth + 1, token->location()); 2758 } 2759 2760 token = this->peek_token(); 2761 } 2762 2763 vals->push_back(val); 2764 2765 if (!is_name) 2766 all_are_names = false; 2767 2768 if (token->is_op(OPERATOR_COMMA)) 2769 { 2770 if (this->advance_token()->is_op(OPERATOR_RCURLY)) 2771 { 2772 this->advance_token(); 2773 break; 2774 } 2775 } 2776 else if (token->is_op(OPERATOR_RCURLY)) 2777 { 2778 this->advance_token(); 2779 break; 2780 } 2781 else 2782 { 2783 if (token->is_op(OPERATOR_SEMICOLON)) 2784 error_at(this->location(), 2785 "need trailing comma before newline in composite literal"); 2786 else 2787 error_at(this->location(), "expected %<,%> or %<}%>"); 2788 2789 this->gogo_->mark_locals_used(); 2790 int depth = 0; 2791 while (!token->is_eof() 2792 && (depth > 0 || !token->is_op(OPERATOR_RCURLY))) 2793 { 2794 if (token->is_op(OPERATOR_LCURLY)) 2795 ++depth; 2796 else if (token->is_op(OPERATOR_RCURLY)) 2797 --depth; 2798 token = this->advance_token(); 2799 } 2800 if (token->is_op(OPERATOR_RCURLY)) 2801 this->advance_token(); 2802 2803 return Expression::make_error(location); 2804 } 2805 } 2806 2807 return Expression::make_composite_literal(type, depth, has_keys, vals, 2808 all_are_names, location); 2809} 2810 2811// FunctionLit = "func" Signature Block . 2812 2813Expression* 2814Parse::function_lit() 2815{ 2816 Location location = this->location(); 2817 go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC)); 2818 this->advance_token(); 2819 2820 Enclosing_vars hold_enclosing_vars; 2821 hold_enclosing_vars.swap(this->enclosing_vars_); 2822 2823 Function_type* type = this->signature(NULL, location); 2824 bool fntype_is_error = false; 2825 if (type == NULL) 2826 { 2827 type = Type::make_function_type(NULL, NULL, NULL, location); 2828 fntype_is_error = true; 2829 } 2830 2831 // For a function literal, the next token must be a '{'. If we 2832 // don't see that, then we may have a type expression. 2833 if (!this->peek_token()->is_op(OPERATOR_LCURLY)) 2834 { 2835 hold_enclosing_vars.swap(this->enclosing_vars_); 2836 return Expression::make_type(type, location); 2837 } 2838 2839 bool hold_is_erroneous_function = this->is_erroneous_function_; 2840 if (fntype_is_error) 2841 this->is_erroneous_function_ = true; 2842 2843 Bc_stack* hold_break_stack = this->break_stack_; 2844 Bc_stack* hold_continue_stack = this->continue_stack_; 2845 this->break_stack_ = NULL; 2846 this->continue_stack_ = NULL; 2847 2848 Named_object* no = this->gogo_->start_function("", type, true, location); 2849 2850 Location end_loc = this->block(); 2851 2852 this->gogo_->finish_function(end_loc); 2853 2854 if (this->break_stack_ != NULL) 2855 delete this->break_stack_; 2856 if (this->continue_stack_ != NULL) 2857 delete this->continue_stack_; 2858 this->break_stack_ = hold_break_stack; 2859 this->continue_stack_ = hold_continue_stack; 2860 2861 this->is_erroneous_function_ = hold_is_erroneous_function; 2862 2863 hold_enclosing_vars.swap(this->enclosing_vars_); 2864 2865 Expression* closure = this->create_closure(no, &hold_enclosing_vars, 2866 location); 2867 2868 return Expression::make_func_reference(no, closure, location); 2869} 2870 2871// Create a closure for the nested function FUNCTION. This is based 2872// on ENCLOSING_VARS, which is a list of all variables defined in 2873// enclosing functions and referenced from FUNCTION. A closure is the 2874// address of a struct which point to the real function code and 2875// contains the addresses of all the referenced variables. This 2876// returns NULL if no closure is required. 2877 2878Expression* 2879Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars, 2880 Location location) 2881{ 2882 if (enclosing_vars->empty()) 2883 return NULL; 2884 2885 // Get the variables in order by their field index. 2886 2887 size_t enclosing_var_count = enclosing_vars->size(); 2888 std::vector<Enclosing_var> ev(enclosing_var_count); 2889 for (Enclosing_vars::const_iterator p = enclosing_vars->begin(); 2890 p != enclosing_vars->end(); 2891 ++p) 2892 { 2893 // Subtract 1 because index 0 is the function code. 2894 ev[p->index() - 1] = *p; 2895 } 2896 2897 // Build an initializer for a composite literal of the closure's 2898 // type. 2899 2900 Named_object* enclosing_function = this->gogo_->current_function(); 2901 Expression_list* initializer = new Expression_list; 2902 2903 initializer->push_back(Expression::make_func_code_reference(function, 2904 location)); 2905 2906 for (size_t i = 0; i < enclosing_var_count; ++i) 2907 { 2908 // Add 1 to i because the first field in the closure is a 2909 // pointer to the function code. 2910 go_assert(ev[i].index() == i + 1); 2911 Named_object* var = ev[i].var(); 2912 Expression* ref; 2913 if (ev[i].in_function() == enclosing_function) 2914 ref = Expression::make_var_reference(var, location); 2915 else 2916 ref = this->enclosing_var_reference(ev[i].in_function(), var, 2917 true, location); 2918 Expression* refaddr = Expression::make_unary(OPERATOR_AND, ref, 2919 location); 2920 initializer->push_back(refaddr); 2921 } 2922 2923 Named_object* closure_var = function->func_value()->closure_var(); 2924 Struct_type* st = closure_var->var_value()->type()->deref()->struct_type(); 2925 Expression* cv = Expression::make_struct_composite_literal(st, initializer, 2926 location); 2927 return Expression::make_heap_expression(cv, location); 2928} 2929 2930// PrimaryExpr = Operand { Selector | Index | Slice | TypeGuard | Call } . 2931 2932// If MAY_BE_SINK is true, this expression may be "_". 2933 2934// If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite 2935// literal. 2936 2937// If IS_TYPE_SWITCH is not NULL, this will recognize a type switch 2938// guard (var := expr.("type") using the literal keyword "type"). 2939 2940// If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true 2941// if the entire expression is in parentheses. 2942 2943Expression* 2944Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit, 2945 bool* is_type_switch, bool* is_parenthesized) 2946{ 2947 Location start_loc = this->location(); 2948 bool operand_is_parenthesized = false; 2949 bool whole_is_parenthesized = false; 2950 2951 Expression* ret = this->operand(may_be_sink, &operand_is_parenthesized); 2952 2953 whole_is_parenthesized = operand_is_parenthesized; 2954 2955 // An unknown name followed by a curly brace must be a composite 2956 // literal, and the unknown name must be a type. 2957 if (may_be_composite_lit 2958 && !operand_is_parenthesized 2959 && ret->unknown_expression() != NULL 2960 && this->peek_token()->is_op(OPERATOR_LCURLY)) 2961 { 2962 Named_object* no = ret->unknown_expression()->named_object(); 2963 Type* type = Type::make_forward_declaration(no); 2964 ret = Expression::make_type(type, ret->location()); 2965 } 2966 2967 // We handle composite literals and type casts here, as it is the 2968 // easiest way to handle types which are in parentheses, as in 2969 // "((uint))(1)". 2970 if (ret->is_type_expression()) 2971 { 2972 if (this->peek_token()->is_op(OPERATOR_LCURLY)) 2973 { 2974 whole_is_parenthesized = false; 2975 if (!may_be_composite_lit) 2976 { 2977 Type* t = ret->type(); 2978 if (t->named_type() != NULL 2979 || t->forward_declaration_type() != NULL) 2980 error_at(start_loc, 2981 _("parentheses required around this composite literal " 2982 "to avoid parsing ambiguity")); 2983 } 2984 else if (operand_is_parenthesized) 2985 error_at(start_loc, 2986 "cannot parenthesize type in composite literal"); 2987 ret = this->composite_lit(ret->type(), 0, ret->location()); 2988 } 2989 else if (this->peek_token()->is_op(OPERATOR_LPAREN)) 2990 { 2991 whole_is_parenthesized = false; 2992 Location loc = this->location(); 2993 this->advance_token(); 2994 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, 2995 NULL, NULL); 2996 if (this->peek_token()->is_op(OPERATOR_COMMA)) 2997 this->advance_token(); 2998 if (this->peek_token()->is_op(OPERATOR_ELLIPSIS)) 2999 { 3000 error_at(this->location(), 3001 "invalid use of %<...%> in type conversion"); 3002 this->advance_token(); 3003 } 3004 if (!this->peek_token()->is_op(OPERATOR_RPAREN)) 3005 error_at(this->location(), "expected %<)%>"); 3006 else 3007 this->advance_token(); 3008 if (expr->is_error_expression()) 3009 ret = expr; 3010 else 3011 { 3012 Type* t = ret->type(); 3013 if (t->classification() == Type::TYPE_ARRAY 3014 && t->array_type()->length() != NULL 3015 && t->array_type()->length()->is_nil_expression()) 3016 { 3017 error_at(ret->location(), 3018 "use of %<[...]%> outside of array literal"); 3019 ret = Expression::make_error(loc); 3020 } 3021 else 3022 ret = Expression::make_cast(t, expr, loc); 3023 } 3024 } 3025 } 3026 3027 while (true) 3028 { 3029 const Token* token = this->peek_token(); 3030 if (token->is_op(OPERATOR_LPAREN)) 3031 { 3032 whole_is_parenthesized = false; 3033 ret = this->call(this->verify_not_sink(ret)); 3034 } 3035 else if (token->is_op(OPERATOR_DOT)) 3036 { 3037 whole_is_parenthesized = false; 3038 ret = this->selector(this->verify_not_sink(ret), is_type_switch); 3039 if (is_type_switch != NULL && *is_type_switch) 3040 break; 3041 } 3042 else if (token->is_op(OPERATOR_LSQUARE)) 3043 { 3044 whole_is_parenthesized = false; 3045 ret = this->index(this->verify_not_sink(ret)); 3046 } 3047 else 3048 break; 3049 } 3050 3051 if (whole_is_parenthesized && is_parenthesized != NULL) 3052 *is_parenthesized = true; 3053 3054 return ret; 3055} 3056 3057// Selector = "." identifier . 3058// TypeGuard = "." "(" QualifiedIdent ")" . 3059 3060// Note that Operand can expand to QualifiedIdent, which contains a 3061// ".". That is handled directly in operand when it sees a package 3062// name. 3063 3064// If IS_TYPE_SWITCH is not NULL, this will recognize a type switch 3065// guard (var := expr.("type") using the literal keyword "type"). 3066 3067Expression* 3068Parse::selector(Expression* left, bool* is_type_switch) 3069{ 3070 go_assert(this->peek_token()->is_op(OPERATOR_DOT)); 3071 Location location = this->location(); 3072 3073 const Token* token = this->advance_token(); 3074 if (token->is_identifier()) 3075 { 3076 // This could be a field in a struct, or a method in an 3077 // interface, or a method associated with a type. We can't know 3078 // which until we have seen all the types. 3079 std::string name = 3080 this->gogo_->pack_hidden_name(token->identifier(), 3081 token->is_identifier_exported()); 3082 if (token->identifier() == "_") 3083 { 3084 error_at(this->location(), "invalid use of %<_%>"); 3085 name = Gogo::erroneous_name(); 3086 } 3087 this->advance_token(); 3088 return Expression::make_selector(left, name, location); 3089 } 3090 else if (token->is_op(OPERATOR_LPAREN)) 3091 { 3092 this->advance_token(); 3093 Type* type = NULL; 3094 if (!this->peek_token()->is_keyword(KEYWORD_TYPE)) 3095 type = this->type(); 3096 else 3097 { 3098 if (is_type_switch != NULL) 3099 *is_type_switch = true; 3100 else 3101 { 3102 error_at(this->location(), 3103 "use of %<.(type)%> outside type switch"); 3104 type = Type::make_error_type(); 3105 } 3106 this->advance_token(); 3107 } 3108 if (!this->peek_token()->is_op(OPERATOR_RPAREN)) 3109 error_at(this->location(), "missing %<)%>"); 3110 else 3111 this->advance_token(); 3112 if (is_type_switch != NULL && *is_type_switch) 3113 return left; 3114 return Expression::make_type_guard(left, type, location); 3115 } 3116 else 3117 { 3118 error_at(this->location(), "expected identifier or %<(%>"); 3119 return left; 3120 } 3121} 3122 3123// Index = "[" Expression "]" . 3124// Slice = "[" Expression ":" [ Expression ] [ ":" Expression ] "]" . 3125 3126Expression* 3127Parse::index(Expression* expr) 3128{ 3129 Location location = this->location(); 3130 go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE)); 3131 this->advance_token(); 3132 3133 Expression* start; 3134 if (!this->peek_token()->is_op(OPERATOR_COLON)) 3135 start = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 3136 else 3137 start = Expression::make_integer_ul(0, NULL, location); 3138 3139 Expression* end = NULL; 3140 if (this->peek_token()->is_op(OPERATOR_COLON)) 3141 { 3142 // We use nil to indicate a missing high expression. 3143 if (this->advance_token()->is_op(OPERATOR_RSQUARE)) 3144 end = Expression::make_nil(this->location()); 3145 else if (this->peek_token()->is_op(OPERATOR_COLON)) 3146 { 3147 error_at(this->location(), "middle index required in 3-index slice"); 3148 end = Expression::make_error(this->location()); 3149 } 3150 else 3151 end = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 3152 } 3153 3154 Expression* cap = NULL; 3155 if (this->peek_token()->is_op(OPERATOR_COLON)) 3156 { 3157 if (this->advance_token()->is_op(OPERATOR_RSQUARE)) 3158 { 3159 error_at(this->location(), "final index required in 3-index slice"); 3160 cap = Expression::make_error(this->location()); 3161 } 3162 else 3163 cap = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 3164 } 3165 if (!this->peek_token()->is_op(OPERATOR_RSQUARE)) 3166 error_at(this->location(), "missing %<]%>"); 3167 else 3168 this->advance_token(); 3169 return Expression::make_index(expr, start, end, cap, location); 3170} 3171 3172// Call = "(" [ ArgumentList [ "," ] ] ")" . 3173// ArgumentList = ExpressionList [ "..." ] . 3174 3175Expression* 3176Parse::call(Expression* func) 3177{ 3178 go_assert(this->peek_token()->is_op(OPERATOR_LPAREN)); 3179 Expression_list* args = NULL; 3180 bool is_varargs = false; 3181 const Token* token = this->advance_token(); 3182 if (!token->is_op(OPERATOR_RPAREN)) 3183 { 3184 args = this->expression_list(NULL, false, true); 3185 token = this->peek_token(); 3186 if (token->is_op(OPERATOR_ELLIPSIS)) 3187 { 3188 is_varargs = true; 3189 token = this->advance_token(); 3190 } 3191 } 3192 if (token->is_op(OPERATOR_COMMA)) 3193 token = this->advance_token(); 3194 if (!token->is_op(OPERATOR_RPAREN)) 3195 { 3196 error_at(this->location(), "missing %<)%>"); 3197 if (!this->skip_past_error(OPERATOR_RPAREN)) 3198 return Expression::make_error(this->location()); 3199 } 3200 this->advance_token(); 3201 if (func->is_error_expression()) 3202 return func; 3203 return Expression::make_call(func, args, is_varargs, func->location()); 3204} 3205 3206// Return an expression for a single unqualified identifier. 3207 3208Expression* 3209Parse::id_to_expression(const std::string& name, Location location, 3210 bool is_lhs) 3211{ 3212 Named_object* in_function; 3213 Named_object* named_object = this->gogo_->lookup(name, &in_function); 3214 if (named_object == NULL) 3215 named_object = this->gogo_->add_unknown_name(name, location); 3216 3217 if (in_function != NULL 3218 && in_function != this->gogo_->current_function() 3219 && (named_object->is_variable() || named_object->is_result_variable())) 3220 return this->enclosing_var_reference(in_function, named_object, is_lhs, 3221 location); 3222 3223 switch (named_object->classification()) 3224 { 3225 case Named_object::NAMED_OBJECT_CONST: 3226 return Expression::make_const_reference(named_object, location); 3227 case Named_object::NAMED_OBJECT_VAR: 3228 case Named_object::NAMED_OBJECT_RESULT_VAR: 3229 if (!is_lhs) 3230 this->mark_var_used(named_object); 3231 return Expression::make_var_reference(named_object, location); 3232 case Named_object::NAMED_OBJECT_SINK: 3233 return Expression::make_sink(location); 3234 case Named_object::NAMED_OBJECT_FUNC: 3235 case Named_object::NAMED_OBJECT_FUNC_DECLARATION: 3236 return Expression::make_func_reference(named_object, NULL, location); 3237 case Named_object::NAMED_OBJECT_UNKNOWN: 3238 { 3239 Unknown_expression* ue = 3240 Expression::make_unknown_reference(named_object, location); 3241 if (this->is_erroneous_function_) 3242 ue->set_no_error_message(); 3243 return ue; 3244 } 3245 case Named_object::NAMED_OBJECT_PACKAGE: 3246 case Named_object::NAMED_OBJECT_TYPE: 3247 case Named_object::NAMED_OBJECT_TYPE_DECLARATION: 3248 { 3249 // These cases can arise for a field name in a composite 3250 // literal. Keep track of these as they might be fake uses of 3251 // the related package. 3252 Unknown_expression* ue = 3253 Expression::make_unknown_reference(named_object, location); 3254 if (named_object->package() != NULL) 3255 named_object->package()->note_fake_usage(ue); 3256 if (this->is_erroneous_function_) 3257 ue->set_no_error_message(); 3258 return ue; 3259 } 3260 case Named_object::NAMED_OBJECT_ERRONEOUS: 3261 return Expression::make_error(location); 3262 default: 3263 error_at(this->location(), "unexpected type of identifier"); 3264 return Expression::make_error(location); 3265 } 3266} 3267 3268// Expression = UnaryExpr { binary_op Expression } . 3269 3270// PRECEDENCE is the precedence of the current operator. 3271 3272// If MAY_BE_SINK is true, this expression may be "_". 3273 3274// If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite 3275// literal. 3276 3277// If IS_TYPE_SWITCH is not NULL, this will recognize a type switch 3278// guard (var := expr.("type") using the literal keyword "type"). 3279 3280// If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true 3281// if the entire expression is in parentheses. 3282 3283Expression* 3284Parse::expression(Precedence precedence, bool may_be_sink, 3285 bool may_be_composite_lit, bool* is_type_switch, 3286 bool *is_parenthesized) 3287{ 3288 Expression* left = this->unary_expr(may_be_sink, may_be_composite_lit, 3289 is_type_switch, is_parenthesized); 3290 3291 while (true) 3292 { 3293 if (is_type_switch != NULL && *is_type_switch) 3294 return left; 3295 3296 const Token* token = this->peek_token(); 3297 if (token->classification() != Token::TOKEN_OPERATOR) 3298 { 3299 // Not a binary_op. 3300 return left; 3301 } 3302 3303 Precedence right_precedence; 3304 switch (token->op()) 3305 { 3306 case OPERATOR_OROR: 3307 right_precedence = PRECEDENCE_OROR; 3308 break; 3309 case OPERATOR_ANDAND: 3310 right_precedence = PRECEDENCE_ANDAND; 3311 break; 3312 case OPERATOR_EQEQ: 3313 case OPERATOR_NOTEQ: 3314 case OPERATOR_LT: 3315 case OPERATOR_LE: 3316 case OPERATOR_GT: 3317 case OPERATOR_GE: 3318 right_precedence = PRECEDENCE_RELOP; 3319 break; 3320 case OPERATOR_PLUS: 3321 case OPERATOR_MINUS: 3322 case OPERATOR_OR: 3323 case OPERATOR_XOR: 3324 right_precedence = PRECEDENCE_ADDOP; 3325 break; 3326 case OPERATOR_MULT: 3327 case OPERATOR_DIV: 3328 case OPERATOR_MOD: 3329 case OPERATOR_LSHIFT: 3330 case OPERATOR_RSHIFT: 3331 case OPERATOR_AND: 3332 case OPERATOR_BITCLEAR: 3333 right_precedence = PRECEDENCE_MULOP; 3334 break; 3335 default: 3336 right_precedence = PRECEDENCE_INVALID; 3337 break; 3338 } 3339 3340 if (right_precedence == PRECEDENCE_INVALID) 3341 { 3342 // Not a binary_op. 3343 return left; 3344 } 3345 3346 if (is_parenthesized != NULL) 3347 *is_parenthesized = false; 3348 3349 Operator op = token->op(); 3350 Location binop_location = token->location(); 3351 3352 if (precedence >= right_precedence) 3353 { 3354 // We've already seen A * B, and we see + C. We want to 3355 // return so that A * B becomes a group. 3356 return left; 3357 } 3358 3359 this->advance_token(); 3360 3361 left = this->verify_not_sink(left); 3362 Expression* right = this->expression(right_precedence, false, 3363 may_be_composite_lit, 3364 NULL, NULL); 3365 left = Expression::make_binary(op, left, right, binop_location); 3366 } 3367} 3368 3369bool 3370Parse::expression_may_start_here() 3371{ 3372 const Token* token = this->peek_token(); 3373 switch (token->classification()) 3374 { 3375 case Token::TOKEN_INVALID: 3376 case Token::TOKEN_EOF: 3377 return false; 3378 case Token::TOKEN_KEYWORD: 3379 switch (token->keyword()) 3380 { 3381 case KEYWORD_CHAN: 3382 case KEYWORD_FUNC: 3383 case KEYWORD_MAP: 3384 case KEYWORD_STRUCT: 3385 case KEYWORD_INTERFACE: 3386 return true; 3387 default: 3388 return false; 3389 } 3390 case Token::TOKEN_IDENTIFIER: 3391 return true; 3392 case Token::TOKEN_STRING: 3393 return true; 3394 case Token::TOKEN_OPERATOR: 3395 switch (token->op()) 3396 { 3397 case OPERATOR_PLUS: 3398 case OPERATOR_MINUS: 3399 case OPERATOR_NOT: 3400 case OPERATOR_XOR: 3401 case OPERATOR_MULT: 3402 case OPERATOR_CHANOP: 3403 case OPERATOR_AND: 3404 case OPERATOR_LPAREN: 3405 case OPERATOR_LSQUARE: 3406 return true; 3407 default: 3408 return false; 3409 } 3410 case Token::TOKEN_CHARACTER: 3411 case Token::TOKEN_INTEGER: 3412 case Token::TOKEN_FLOAT: 3413 case Token::TOKEN_IMAGINARY: 3414 return true; 3415 default: 3416 go_unreachable(); 3417 } 3418} 3419 3420// UnaryExpr = unary_op UnaryExpr | PrimaryExpr . 3421 3422// If MAY_BE_SINK is true, this expression may be "_". 3423 3424// If MAY_BE_COMPOSITE_LIT is true, this expression may be a composite 3425// literal. 3426 3427// If IS_TYPE_SWITCH is not NULL, this will recognize a type switch 3428// guard (var := expr.("type") using the literal keyword "type"). 3429 3430// If IS_PARENTHESIZED is not NULL, *IS_PARENTHESIZED is set to true 3431// if the entire expression is in parentheses. 3432 3433Expression* 3434Parse::unary_expr(bool may_be_sink, bool may_be_composite_lit, 3435 bool* is_type_switch, bool* is_parenthesized) 3436{ 3437 const Token* token = this->peek_token(); 3438 3439 // There is a complex parse for <- chan. The choices are 3440 // Convert x to type <- chan int: 3441 // (<- chan int)(x) 3442 // Receive from (x converted to type chan <- chan int): 3443 // (<- chan <- chan int (x)) 3444 // Convert x to type <- chan (<- chan int). 3445 // (<- chan <- chan int)(x) 3446 if (token->is_op(OPERATOR_CHANOP)) 3447 { 3448 Location location = token->location(); 3449 if (this->advance_token()->is_keyword(KEYWORD_CHAN)) 3450 { 3451 Expression* expr = this->primary_expr(false, may_be_composite_lit, 3452 NULL, NULL); 3453 if (expr->is_error_expression()) 3454 return expr; 3455 else if (!expr->is_type_expression()) 3456 return Expression::make_receive(expr, location); 3457 else 3458 { 3459 if (expr->type()->is_error_type()) 3460 return expr; 3461 3462 // We picked up "chan TYPE", but it is not a type 3463 // conversion. 3464 Channel_type* ct = expr->type()->channel_type(); 3465 if (ct == NULL) 3466 { 3467 // This is probably impossible. 3468 error_at(location, "expected channel type"); 3469 return Expression::make_error(location); 3470 } 3471 else if (ct->may_receive()) 3472 { 3473 // <- chan TYPE. 3474 Type* t = Type::make_channel_type(false, true, 3475 ct->element_type()); 3476 return Expression::make_type(t, location); 3477 } 3478 else 3479 { 3480 // <- chan <- TYPE. Because we skipped the leading 3481 // <-, we parsed this as chan <- TYPE. With the 3482 // leading <-, we parse it as <- chan (<- TYPE). 3483 Type *t = this->reassociate_chan_direction(ct, location); 3484 return Expression::make_type(t, location); 3485 } 3486 } 3487 } 3488 3489 this->unget_token(Token::make_operator_token(OPERATOR_CHANOP, location)); 3490 token = this->peek_token(); 3491 } 3492 3493 if (token->is_op(OPERATOR_PLUS) 3494 || token->is_op(OPERATOR_MINUS) 3495 || token->is_op(OPERATOR_NOT) 3496 || token->is_op(OPERATOR_XOR) 3497 || token->is_op(OPERATOR_CHANOP) 3498 || token->is_op(OPERATOR_MULT) 3499 || token->is_op(OPERATOR_AND)) 3500 { 3501 Location location = token->location(); 3502 Operator op = token->op(); 3503 this->advance_token(); 3504 3505 Expression* expr = this->unary_expr(false, may_be_composite_lit, NULL, 3506 NULL); 3507 if (expr->is_error_expression()) 3508 ; 3509 else if (op == OPERATOR_MULT && expr->is_type_expression()) 3510 expr = Expression::make_type(Type::make_pointer_type(expr->type()), 3511 location); 3512 else if (op == OPERATOR_AND && expr->is_composite_literal()) 3513 expr = Expression::make_heap_expression(expr, location); 3514 else if (op != OPERATOR_CHANOP) 3515 expr = Expression::make_unary(op, expr, location); 3516 else 3517 expr = Expression::make_receive(expr, location); 3518 return expr; 3519 } 3520 else 3521 return this->primary_expr(may_be_sink, may_be_composite_lit, 3522 is_type_switch, is_parenthesized); 3523} 3524 3525// This is called for the obscure case of 3526// (<- chan <- chan int)(x) 3527// In unary_expr we remove the leading <- and parse the remainder, 3528// which gives us 3529// chan <- (chan int) 3530// When we add the leading <- back in, we really want 3531// <- chan (<- chan int) 3532// This means that we need to reassociate. 3533 3534Type* 3535Parse::reassociate_chan_direction(Channel_type *ct, Location location) 3536{ 3537 Channel_type* ele = ct->element_type()->channel_type(); 3538 if (ele == NULL) 3539 { 3540 error_at(location, "parse error"); 3541 return Type::make_error_type(); 3542 } 3543 Type* sub = ele; 3544 if (ele->may_send()) 3545 sub = Type::make_channel_type(false, true, ele->element_type()); 3546 else 3547 sub = this->reassociate_chan_direction(ele, location); 3548 return Type::make_channel_type(false, true, sub); 3549} 3550 3551// Statement = 3552// Declaration | LabeledStmt | SimpleStmt | 3553// GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt | 3554// FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt | 3555// DeferStmt . 3556 3557// LABEL is the label of this statement if it has one. 3558 3559void 3560Parse::statement(Label* label) 3561{ 3562 const Token* token = this->peek_token(); 3563 switch (token->classification()) 3564 { 3565 case Token::TOKEN_KEYWORD: 3566 { 3567 switch (token->keyword()) 3568 { 3569 case KEYWORD_CONST: 3570 case KEYWORD_TYPE: 3571 case KEYWORD_VAR: 3572 this->declaration(); 3573 break; 3574 case KEYWORD_FUNC: 3575 case KEYWORD_MAP: 3576 case KEYWORD_STRUCT: 3577 case KEYWORD_INTERFACE: 3578 this->simple_stat(true, NULL, NULL, NULL); 3579 break; 3580 case KEYWORD_GO: 3581 case KEYWORD_DEFER: 3582 this->go_or_defer_stat(); 3583 break; 3584 case KEYWORD_RETURN: 3585 this->return_stat(); 3586 break; 3587 case KEYWORD_BREAK: 3588 this->break_stat(); 3589 break; 3590 case KEYWORD_CONTINUE: 3591 this->continue_stat(); 3592 break; 3593 case KEYWORD_GOTO: 3594 this->goto_stat(); 3595 break; 3596 case KEYWORD_IF: 3597 this->if_stat(); 3598 break; 3599 case KEYWORD_SWITCH: 3600 this->switch_stat(label); 3601 break; 3602 case KEYWORD_SELECT: 3603 this->select_stat(label); 3604 break; 3605 case KEYWORD_FOR: 3606 this->for_stat(label); 3607 break; 3608 default: 3609 error_at(this->location(), "expected statement"); 3610 this->advance_token(); 3611 break; 3612 } 3613 } 3614 break; 3615 3616 case Token::TOKEN_IDENTIFIER: 3617 { 3618 std::string identifier = token->identifier(); 3619 bool is_exported = token->is_identifier_exported(); 3620 Location location = token->location(); 3621 if (this->advance_token()->is_op(OPERATOR_COLON)) 3622 { 3623 this->advance_token(); 3624 this->labeled_stmt(identifier, location); 3625 } 3626 else 3627 { 3628 this->unget_token(Token::make_identifier_token(identifier, 3629 is_exported, 3630 location)); 3631 this->simple_stat(true, NULL, NULL, NULL); 3632 } 3633 } 3634 break; 3635 3636 case Token::TOKEN_OPERATOR: 3637 if (token->is_op(OPERATOR_LCURLY)) 3638 { 3639 Location location = token->location(); 3640 this->gogo_->start_block(location); 3641 Location end_loc = this->block(); 3642 this->gogo_->add_block(this->gogo_->finish_block(end_loc), 3643 location); 3644 } 3645 else if (!token->is_op(OPERATOR_SEMICOLON)) 3646 this->simple_stat(true, NULL, NULL, NULL); 3647 break; 3648 3649 case Token::TOKEN_STRING: 3650 case Token::TOKEN_CHARACTER: 3651 case Token::TOKEN_INTEGER: 3652 case Token::TOKEN_FLOAT: 3653 case Token::TOKEN_IMAGINARY: 3654 this->simple_stat(true, NULL, NULL, NULL); 3655 break; 3656 3657 default: 3658 error_at(this->location(), "expected statement"); 3659 this->advance_token(); 3660 break; 3661 } 3662} 3663 3664bool 3665Parse::statement_may_start_here() 3666{ 3667 const Token* token = this->peek_token(); 3668 switch (token->classification()) 3669 { 3670 case Token::TOKEN_KEYWORD: 3671 { 3672 switch (token->keyword()) 3673 { 3674 case KEYWORD_CONST: 3675 case KEYWORD_TYPE: 3676 case KEYWORD_VAR: 3677 case KEYWORD_FUNC: 3678 case KEYWORD_MAP: 3679 case KEYWORD_STRUCT: 3680 case KEYWORD_INTERFACE: 3681 case KEYWORD_GO: 3682 case KEYWORD_DEFER: 3683 case KEYWORD_RETURN: 3684 case KEYWORD_BREAK: 3685 case KEYWORD_CONTINUE: 3686 case KEYWORD_GOTO: 3687 case KEYWORD_IF: 3688 case KEYWORD_SWITCH: 3689 case KEYWORD_SELECT: 3690 case KEYWORD_FOR: 3691 return true; 3692 3693 default: 3694 return false; 3695 } 3696 } 3697 break; 3698 3699 case Token::TOKEN_IDENTIFIER: 3700 return true; 3701 3702 case Token::TOKEN_OPERATOR: 3703 if (token->is_op(OPERATOR_LCURLY) 3704 || token->is_op(OPERATOR_SEMICOLON)) 3705 return true; 3706 else 3707 return this->expression_may_start_here(); 3708 3709 case Token::TOKEN_STRING: 3710 case Token::TOKEN_CHARACTER: 3711 case Token::TOKEN_INTEGER: 3712 case Token::TOKEN_FLOAT: 3713 case Token::TOKEN_IMAGINARY: 3714 return true; 3715 3716 default: 3717 return false; 3718 } 3719} 3720 3721// LabeledStmt = Label ":" Statement . 3722// Label = identifier . 3723 3724void 3725Parse::labeled_stmt(const std::string& label_name, Location location) 3726{ 3727 Label* label = this->gogo_->add_label_definition(label_name, location); 3728 3729 if (this->peek_token()->is_op(OPERATOR_RCURLY)) 3730 { 3731 // This is a label at the end of a block. A program is 3732 // permitted to omit a semicolon here. 3733 return; 3734 } 3735 3736 if (!this->statement_may_start_here()) 3737 { 3738 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH)) 3739 { 3740 // We don't treat the fallthrough keyword as a statement, 3741 // because it can't appear most places where a statement is 3742 // permitted, but it may have a label. We introduce a 3743 // semicolon because the caller expects to see a statement. 3744 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON, 3745 location)); 3746 return; 3747 } 3748 3749 // Mark the label as used to avoid a useless error about an 3750 // unused label. 3751 if (label != NULL) 3752 label->set_is_used(); 3753 3754 error_at(location, "missing statement after label"); 3755 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON, 3756 location)); 3757 return; 3758 } 3759 3760 this->statement(label); 3761} 3762 3763// SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | 3764// Assignment | ShortVarDecl . 3765 3766// EmptyStmt was handled in Parse::statement. 3767 3768// In order to make this work for if and switch statements, if 3769// RETURN_EXP is not NULL, and we see an ExpressionStat, we return the 3770// expression rather than adding an expression statement to the 3771// current block. If we see something other than an ExpressionStat, 3772// we add the statement, set *RETURN_EXP to true if we saw a send 3773// statement, and return NULL. The handling of send statements is for 3774// better error messages. 3775 3776// If P_RANGE_CLAUSE is not NULL, then this will recognize a 3777// RangeClause. 3778 3779// If P_TYPE_SWITCH is not NULL, this will recognize a type switch 3780// guard (var := expr.("type") using the literal keyword "type"). 3781 3782Expression* 3783Parse::simple_stat(bool may_be_composite_lit, bool* return_exp, 3784 Range_clause* p_range_clause, Type_switch* p_type_switch) 3785{ 3786 const Token* token = this->peek_token(); 3787 3788 // An identifier follow by := is a SimpleVarDecl. 3789 if (token->is_identifier()) 3790 { 3791 std::string identifier = token->identifier(); 3792 bool is_exported = token->is_identifier_exported(); 3793 Location location = token->location(); 3794 3795 token = this->advance_token(); 3796 if (token->is_op(OPERATOR_COLONEQ) 3797 || token->is_op(OPERATOR_COMMA)) 3798 { 3799 identifier = this->gogo_->pack_hidden_name(identifier, is_exported); 3800 this->simple_var_decl_or_assignment(identifier, location, 3801 may_be_composite_lit, 3802 p_range_clause, 3803 (token->is_op(OPERATOR_COLONEQ) 3804 ? p_type_switch 3805 : NULL)); 3806 return NULL; 3807 } 3808 3809 this->unget_token(Token::make_identifier_token(identifier, is_exported, 3810 location)); 3811 } 3812 else if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE)) 3813 { 3814 Typed_identifier_list til; 3815 this->range_clause_decl(&til, p_range_clause); 3816 return NULL; 3817 } 3818 3819 Expression* exp = this->expression(PRECEDENCE_NORMAL, true, 3820 may_be_composite_lit, 3821 (p_type_switch == NULL 3822 ? NULL 3823 : &p_type_switch->found), 3824 NULL); 3825 if (p_type_switch != NULL && p_type_switch->found) 3826 { 3827 p_type_switch->name.clear(); 3828 p_type_switch->location = exp->location(); 3829 p_type_switch->expr = this->verify_not_sink(exp); 3830 return NULL; 3831 } 3832 token = this->peek_token(); 3833 if (token->is_op(OPERATOR_CHANOP)) 3834 { 3835 this->send_stmt(this->verify_not_sink(exp), may_be_composite_lit); 3836 if (return_exp != NULL) 3837 *return_exp = true; 3838 } 3839 else if (token->is_op(OPERATOR_PLUSPLUS) 3840 || token->is_op(OPERATOR_MINUSMINUS)) 3841 this->inc_dec_stat(this->verify_not_sink(exp)); 3842 else if (token->is_op(OPERATOR_COMMA) 3843 || token->is_op(OPERATOR_EQ)) 3844 this->assignment(exp, may_be_composite_lit, p_range_clause); 3845 else if (token->is_op(OPERATOR_PLUSEQ) 3846 || token->is_op(OPERATOR_MINUSEQ) 3847 || token->is_op(OPERATOR_OREQ) 3848 || token->is_op(OPERATOR_XOREQ) 3849 || token->is_op(OPERATOR_MULTEQ) 3850 || token->is_op(OPERATOR_DIVEQ) 3851 || token->is_op(OPERATOR_MODEQ) 3852 || token->is_op(OPERATOR_LSHIFTEQ) 3853 || token->is_op(OPERATOR_RSHIFTEQ) 3854 || token->is_op(OPERATOR_ANDEQ) 3855 || token->is_op(OPERATOR_BITCLEAREQ)) 3856 this->assignment(this->verify_not_sink(exp), may_be_composite_lit, 3857 p_range_clause); 3858 else if (return_exp != NULL) 3859 return this->verify_not_sink(exp); 3860 else 3861 { 3862 exp = this->verify_not_sink(exp); 3863 3864 if (token->is_op(OPERATOR_COLONEQ)) 3865 { 3866 if (!exp->is_error_expression()) 3867 error_at(token->location(), "non-name on left side of %<:=%>"); 3868 this->gogo_->mark_locals_used(); 3869 while (!token->is_op(OPERATOR_SEMICOLON) 3870 && !token->is_eof()) 3871 token = this->advance_token(); 3872 return NULL; 3873 } 3874 3875 this->expression_stat(exp); 3876 } 3877 3878 return NULL; 3879} 3880 3881bool 3882Parse::simple_stat_may_start_here() 3883{ 3884 return this->expression_may_start_here(); 3885} 3886 3887// Parse { Statement ";" } which is used in a few places. The list of 3888// statements may end with a right curly brace, in which case the 3889// semicolon may be omitted. 3890 3891void 3892Parse::statement_list() 3893{ 3894 while (this->statement_may_start_here()) 3895 { 3896 this->statement(NULL); 3897 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 3898 this->advance_token(); 3899 else if (this->peek_token()->is_op(OPERATOR_RCURLY)) 3900 break; 3901 else 3902 { 3903 if (!this->peek_token()->is_eof() || !saw_errors()) 3904 error_at(this->location(), "expected %<;%> or %<}%> or newline"); 3905 if (!this->skip_past_error(OPERATOR_RCURLY)) 3906 return; 3907 } 3908 } 3909} 3910 3911bool 3912Parse::statement_list_may_start_here() 3913{ 3914 return this->statement_may_start_here(); 3915} 3916 3917// ExpressionStat = Expression . 3918 3919void 3920Parse::expression_stat(Expression* exp) 3921{ 3922 this->gogo_->add_statement(Statement::make_statement(exp, false)); 3923} 3924 3925// SendStmt = Channel "<-" Expression . 3926// Channel = Expression . 3927 3928void 3929Parse::send_stmt(Expression* channel, bool may_be_composite_lit) 3930{ 3931 go_assert(this->peek_token()->is_op(OPERATOR_CHANOP)); 3932 Location loc = this->location(); 3933 this->advance_token(); 3934 Expression* val = this->expression(PRECEDENCE_NORMAL, false, 3935 may_be_composite_lit, NULL, NULL); 3936 Statement* s = Statement::make_send_statement(channel, val, loc); 3937 this->gogo_->add_statement(s); 3938} 3939 3940// IncDecStat = Expression ( "++" | "--" ) . 3941 3942void 3943Parse::inc_dec_stat(Expression* exp) 3944{ 3945 const Token* token = this->peek_token(); 3946 3947 // Lvalue maps require special handling. 3948 if (exp->index_expression() != NULL) 3949 exp->index_expression()->set_is_lvalue(); 3950 3951 if (token->is_op(OPERATOR_PLUSPLUS)) 3952 this->gogo_->add_statement(Statement::make_inc_statement(exp)); 3953 else if (token->is_op(OPERATOR_MINUSMINUS)) 3954 this->gogo_->add_statement(Statement::make_dec_statement(exp)); 3955 else 3956 go_unreachable(); 3957 this->advance_token(); 3958} 3959 3960// Assignment = ExpressionList assign_op ExpressionList . 3961 3962// EXP is an expression that we have already parsed. 3963 3964// If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand 3965// side may be a composite literal. 3966 3967// If RANGE_CLAUSE is not NULL, then this will recognize a 3968// RangeClause. 3969 3970void 3971Parse::assignment(Expression* expr, bool may_be_composite_lit, 3972 Range_clause* p_range_clause) 3973{ 3974 Expression_list* vars; 3975 if (!this->peek_token()->is_op(OPERATOR_COMMA)) 3976 { 3977 vars = new Expression_list(); 3978 vars->push_back(expr); 3979 } 3980 else 3981 { 3982 this->advance_token(); 3983 vars = this->expression_list(expr, true, may_be_composite_lit); 3984 } 3985 3986 this->tuple_assignment(vars, may_be_composite_lit, p_range_clause); 3987} 3988 3989// An assignment statement. LHS is the list of expressions which 3990// appear on the left hand side. 3991 3992// If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand 3993// side may be a composite literal. 3994 3995// If RANGE_CLAUSE is not NULL, then this will recognize a 3996// RangeClause. 3997 3998void 3999Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit, 4000 Range_clause* p_range_clause) 4001{ 4002 const Token* token = this->peek_token(); 4003 if (!token->is_op(OPERATOR_EQ) 4004 && !token->is_op(OPERATOR_PLUSEQ) 4005 && !token->is_op(OPERATOR_MINUSEQ) 4006 && !token->is_op(OPERATOR_OREQ) 4007 && !token->is_op(OPERATOR_XOREQ) 4008 && !token->is_op(OPERATOR_MULTEQ) 4009 && !token->is_op(OPERATOR_DIVEQ) 4010 && !token->is_op(OPERATOR_MODEQ) 4011 && !token->is_op(OPERATOR_LSHIFTEQ) 4012 && !token->is_op(OPERATOR_RSHIFTEQ) 4013 && !token->is_op(OPERATOR_ANDEQ) 4014 && !token->is_op(OPERATOR_BITCLEAREQ)) 4015 { 4016 error_at(this->location(), "expected assignment operator"); 4017 return; 4018 } 4019 Operator op = token->op(); 4020 Location location = token->location(); 4021 4022 token = this->advance_token(); 4023 4024 if (lhs == NULL) 4025 return; 4026 4027 // Map expressions act differently when they are lvalues. 4028 for (Expression_list::iterator plv = lhs->begin(); 4029 plv != lhs->end(); 4030 ++plv) 4031 if ((*plv)->index_expression() != NULL) 4032 (*plv)->index_expression()->set_is_lvalue(); 4033 4034 if (p_range_clause != NULL && token->is_keyword(KEYWORD_RANGE)) 4035 { 4036 if (op != OPERATOR_EQ) 4037 error_at(this->location(), "range clause requires %<=%>"); 4038 this->range_clause_expr(lhs, p_range_clause); 4039 return; 4040 } 4041 4042 Expression_list* vals = this->expression_list(NULL, false, 4043 may_be_composite_lit); 4044 4045 // We've parsed everything; check for errors. 4046 if (vals == NULL) 4047 return; 4048 for (Expression_list::const_iterator pe = lhs->begin(); 4049 pe != lhs->end(); 4050 ++pe) 4051 { 4052 if ((*pe)->is_error_expression()) 4053 return; 4054 if (op != OPERATOR_EQ && (*pe)->is_sink_expression()) 4055 error_at((*pe)->location(), "cannot use _ as value"); 4056 } 4057 for (Expression_list::const_iterator pe = vals->begin(); 4058 pe != vals->end(); 4059 ++pe) 4060 { 4061 if ((*pe)->is_error_expression()) 4062 return; 4063 } 4064 4065 Call_expression* call; 4066 Index_expression* map_index; 4067 Receive_expression* receive; 4068 Type_guard_expression* type_guard; 4069 if (lhs->size() == vals->size()) 4070 { 4071 Statement* s; 4072 if (lhs->size() > 1) 4073 { 4074 if (op != OPERATOR_EQ) 4075 error_at(location, "multiple values only permitted with %<=%>"); 4076 s = Statement::make_tuple_assignment(lhs, vals, location); 4077 } 4078 else 4079 { 4080 if (op == OPERATOR_EQ) 4081 s = Statement::make_assignment(lhs->front(), vals->front(), 4082 location); 4083 else 4084 s = Statement::make_assignment_operation(op, lhs->front(), 4085 vals->front(), location); 4086 delete lhs; 4087 delete vals; 4088 } 4089 this->gogo_->add_statement(s); 4090 } 4091 else if (vals->size() == 1 4092 && (call = (*vals->begin())->call_expression()) != NULL) 4093 { 4094 if (op != OPERATOR_EQ) 4095 error_at(location, "multiple results only permitted with %<=%>"); 4096 call->set_expected_result_count(lhs->size()); 4097 delete vals; 4098 vals = new Expression_list; 4099 for (unsigned int i = 0; i < lhs->size(); ++i) 4100 vals->push_back(Expression::make_call_result(call, i)); 4101 Statement* s = Statement::make_tuple_assignment(lhs, vals, location); 4102 this->gogo_->add_statement(s); 4103 } 4104 else if (lhs->size() == 2 4105 && vals->size() == 1 4106 && (map_index = (*vals->begin())->index_expression()) != NULL) 4107 { 4108 if (op != OPERATOR_EQ) 4109 error_at(location, "two values from map requires %<=%>"); 4110 Expression* val = lhs->front(); 4111 Expression* present = lhs->back(); 4112 Statement* s = Statement::make_tuple_map_assignment(val, present, 4113 map_index, location); 4114 this->gogo_->add_statement(s); 4115 } 4116 else if (lhs->size() == 1 4117 && vals->size() == 2 4118 && (map_index = lhs->front()->index_expression()) != NULL) 4119 { 4120 if (op != OPERATOR_EQ) 4121 error_at(location, "assigning tuple to map index requires %<=%>"); 4122 Expression* val = vals->front(); 4123 Expression* should_set = vals->back(); 4124 Statement* s = Statement::make_map_assignment(map_index, val, should_set, 4125 location); 4126 this->gogo_->add_statement(s); 4127 } 4128 else if (lhs->size() == 2 4129 && vals->size() == 1 4130 && (receive = (*vals->begin())->receive_expression()) != NULL) 4131 { 4132 if (op != OPERATOR_EQ) 4133 error_at(location, "two values from receive requires %<=%>"); 4134 Expression* val = lhs->front(); 4135 Expression* success = lhs->back(); 4136 Expression* channel = receive->channel(); 4137 Statement* s = Statement::make_tuple_receive_assignment(val, success, 4138 channel, 4139 location); 4140 this->gogo_->add_statement(s); 4141 } 4142 else if (lhs->size() == 2 4143 && vals->size() == 1 4144 && (type_guard = (*vals->begin())->type_guard_expression()) != NULL) 4145 { 4146 if (op != OPERATOR_EQ) 4147 error_at(location, "two values from type guard requires %<=%>"); 4148 Expression* val = lhs->front(); 4149 Expression* ok = lhs->back(); 4150 Expression* expr = type_guard->expr(); 4151 Type* type = type_guard->type(); 4152 Statement* s = Statement::make_tuple_type_guard_assignment(val, ok, 4153 expr, type, 4154 location); 4155 this->gogo_->add_statement(s); 4156 } 4157 else 4158 { 4159 error_at(location, "number of variables does not match number of values"); 4160 } 4161} 4162 4163// GoStat = "go" Expression . 4164// DeferStat = "defer" Expression . 4165 4166void 4167Parse::go_or_defer_stat() 4168{ 4169 go_assert(this->peek_token()->is_keyword(KEYWORD_GO) 4170 || this->peek_token()->is_keyword(KEYWORD_DEFER)); 4171 bool is_go = this->peek_token()->is_keyword(KEYWORD_GO); 4172 Location stat_location = this->location(); 4173 4174 this->advance_token(); 4175 Location expr_location = this->location(); 4176 4177 bool is_parenthesized = false; 4178 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL, 4179 &is_parenthesized); 4180 Call_expression* call_expr = expr->call_expression(); 4181 if (is_parenthesized || call_expr == NULL) 4182 { 4183 error_at(expr_location, "argument to go/defer must be function call"); 4184 return; 4185 } 4186 4187 // Make it easier to simplify go/defer statements by putting every 4188 // statement in its own block. 4189 this->gogo_->start_block(stat_location); 4190 Statement* stat; 4191 if (is_go) 4192 stat = Statement::make_go_statement(call_expr, stat_location); 4193 else 4194 stat = Statement::make_defer_statement(call_expr, stat_location); 4195 this->gogo_->add_statement(stat); 4196 this->gogo_->add_block(this->gogo_->finish_block(stat_location), 4197 stat_location); 4198} 4199 4200// ReturnStat = "return" [ ExpressionList ] . 4201 4202void 4203Parse::return_stat() 4204{ 4205 go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN)); 4206 Location location = this->location(); 4207 this->advance_token(); 4208 Expression_list* vals = NULL; 4209 if (this->expression_may_start_here()) 4210 vals = this->expression_list(NULL, false, true); 4211 this->gogo_->add_statement(Statement::make_return_statement(vals, location)); 4212 4213 if (vals == NULL 4214 && this->gogo_->current_function()->func_value()->results_are_named()) 4215 { 4216 Named_object* function = this->gogo_->current_function(); 4217 Function::Results* results = function->func_value()->result_variables(); 4218 for (Function::Results::const_iterator p = results->begin(); 4219 p != results->end(); 4220 ++p) 4221 { 4222 Named_object* no = this->gogo_->lookup((*p)->name(), NULL); 4223 if (no == NULL) 4224 go_assert(saw_errors()); 4225 else if (!no->is_result_variable()) 4226 error_at(location, "%qs is shadowed during return", 4227 (*p)->message_name().c_str()); 4228 } 4229 } 4230} 4231 4232// IfStmt = "if" [ SimpleStmt ";" ] Expression Block 4233// [ "else" ( IfStmt | Block ) ] . 4234 4235void 4236Parse::if_stat() 4237{ 4238 go_assert(this->peek_token()->is_keyword(KEYWORD_IF)); 4239 Location location = this->location(); 4240 this->advance_token(); 4241 4242 this->gogo_->start_block(location); 4243 4244 bool saw_simple_stat = false; 4245 Expression* cond = NULL; 4246 bool saw_send_stmt = false; 4247 if (this->simple_stat_may_start_here()) 4248 { 4249 cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL); 4250 saw_simple_stat = true; 4251 } 4252 if (cond != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON)) 4253 { 4254 // The SimpleStat is an expression statement. 4255 this->expression_stat(cond); 4256 cond = NULL; 4257 } 4258 if (cond == NULL) 4259 { 4260 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 4261 this->advance_token(); 4262 else if (saw_simple_stat) 4263 { 4264 if (saw_send_stmt) 4265 error_at(this->location(), 4266 ("send statement used as value; " 4267 "use select for non-blocking send")); 4268 else 4269 error_at(this->location(), 4270 "expected %<;%> after statement in if expression"); 4271 if (!this->expression_may_start_here()) 4272 cond = Expression::make_error(this->location()); 4273 } 4274 if (cond == NULL && this->peek_token()->is_op(OPERATOR_LCURLY)) 4275 { 4276 error_at(this->location(), 4277 "missing condition in if statement"); 4278 cond = Expression::make_error(this->location()); 4279 } 4280 if (cond == NULL) 4281 cond = this->expression(PRECEDENCE_NORMAL, false, false, NULL, NULL); 4282 } 4283 4284 // Check for the easy error of a newline before starting the block. 4285 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 4286 { 4287 Location semi_loc = this->location(); 4288 if (this->advance_token()->is_op(OPERATOR_LCURLY)) 4289 error_at(semi_loc, "missing %<{%> after if clause"); 4290 // Otherwise we will get an error when we call this->block 4291 // below. 4292 } 4293 4294 this->gogo_->start_block(this->location()); 4295 Location end_loc = this->block(); 4296 Block* then_block = this->gogo_->finish_block(end_loc); 4297 4298 // Check for the easy error of a newline before "else". 4299 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 4300 { 4301 Location semi_loc = this->location(); 4302 if (this->advance_token()->is_keyword(KEYWORD_ELSE)) 4303 error_at(this->location(), 4304 "unexpected semicolon or newline before %<else%>"); 4305 else 4306 this->unget_token(Token::make_operator_token(OPERATOR_SEMICOLON, 4307 semi_loc)); 4308 } 4309 4310 Block* else_block = NULL; 4311 if (this->peek_token()->is_keyword(KEYWORD_ELSE)) 4312 { 4313 this->gogo_->start_block(this->location()); 4314 const Token* token = this->advance_token(); 4315 if (token->is_keyword(KEYWORD_IF)) 4316 this->if_stat(); 4317 else if (token->is_op(OPERATOR_LCURLY)) 4318 this->block(); 4319 else 4320 { 4321 error_at(this->location(), "expected %<if%> or %<{%>"); 4322 this->statement(NULL); 4323 } 4324 else_block = this->gogo_->finish_block(this->location()); 4325 } 4326 4327 this->gogo_->add_statement(Statement::make_if_statement(cond, then_block, 4328 else_block, 4329 location)); 4330 4331 this->gogo_->add_block(this->gogo_->finish_block(this->location()), 4332 location); 4333} 4334 4335// SwitchStmt = ExprSwitchStmt | TypeSwitchStmt . 4336// ExprSwitchStmt = "switch" [ [ SimpleStat ] ";" ] [ Expression ] 4337// "{" { ExprCaseClause } "}" . 4338// TypeSwitchStmt = "switch" [ [ SimpleStat ] ";" ] TypeSwitchGuard 4339// "{" { TypeCaseClause } "}" . 4340// TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" . 4341 4342void 4343Parse::switch_stat(Label* label) 4344{ 4345 go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH)); 4346 Location location = this->location(); 4347 this->advance_token(); 4348 4349 this->gogo_->start_block(location); 4350 4351 bool saw_simple_stat = false; 4352 Expression* switch_val = NULL; 4353 bool saw_send_stmt; 4354 Type_switch type_switch; 4355 bool have_type_switch_block = false; 4356 if (this->simple_stat_may_start_here()) 4357 { 4358 switch_val = this->simple_stat(false, &saw_send_stmt, NULL, 4359 &type_switch); 4360 saw_simple_stat = true; 4361 } 4362 if (switch_val != NULL && this->peek_token()->is_op(OPERATOR_SEMICOLON)) 4363 { 4364 // The SimpleStat is an expression statement. 4365 this->expression_stat(switch_val); 4366 switch_val = NULL; 4367 } 4368 if (switch_val == NULL && !type_switch.found) 4369 { 4370 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 4371 this->advance_token(); 4372 else if (saw_simple_stat) 4373 { 4374 if (saw_send_stmt) 4375 error_at(this->location(), 4376 ("send statement used as value; " 4377 "use select for non-blocking send")); 4378 else 4379 error_at(this->location(), 4380 "expected %<;%> after statement in switch expression"); 4381 } 4382 if (!this->peek_token()->is_op(OPERATOR_LCURLY)) 4383 { 4384 if (this->peek_token()->is_identifier()) 4385 { 4386 const Token* token = this->peek_token(); 4387 std::string identifier = token->identifier(); 4388 bool is_exported = token->is_identifier_exported(); 4389 Location id_loc = token->location(); 4390 4391 token = this->advance_token(); 4392 bool is_coloneq = token->is_op(OPERATOR_COLONEQ); 4393 this->unget_token(Token::make_identifier_token(identifier, 4394 is_exported, 4395 id_loc)); 4396 if (is_coloneq) 4397 { 4398 // This must be a TypeSwitchGuard. It is in a 4399 // different block from any initial SimpleStat. 4400 if (saw_simple_stat) 4401 { 4402 this->gogo_->start_block(id_loc); 4403 have_type_switch_block = true; 4404 } 4405 4406 switch_val = this->simple_stat(false, &saw_send_stmt, NULL, 4407 &type_switch); 4408 if (!type_switch.found) 4409 { 4410 if (switch_val == NULL 4411 || !switch_val->is_error_expression()) 4412 { 4413 error_at(id_loc, "expected type switch assignment"); 4414 switch_val = Expression::make_error(id_loc); 4415 } 4416 } 4417 } 4418 } 4419 if (switch_val == NULL && !type_switch.found) 4420 { 4421 switch_val = this->expression(PRECEDENCE_NORMAL, false, false, 4422 &type_switch.found, NULL); 4423 if (type_switch.found) 4424 { 4425 type_switch.name.clear(); 4426 type_switch.expr = switch_val; 4427 type_switch.location = switch_val->location(); 4428 } 4429 } 4430 } 4431 } 4432 4433 if (!this->peek_token()->is_op(OPERATOR_LCURLY)) 4434 { 4435 Location token_loc = this->location(); 4436 if (this->peek_token()->is_op(OPERATOR_SEMICOLON) 4437 && this->advance_token()->is_op(OPERATOR_LCURLY)) 4438 error_at(token_loc, "missing %<{%> after switch clause"); 4439 else if (this->peek_token()->is_op(OPERATOR_COLONEQ)) 4440 { 4441 error_at(token_loc, "invalid variable name"); 4442 this->advance_token(); 4443 this->expression(PRECEDENCE_NORMAL, false, false, 4444 &type_switch.found, NULL); 4445 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 4446 this->advance_token(); 4447 if (!this->peek_token()->is_op(OPERATOR_LCURLY)) 4448 { 4449 if (have_type_switch_block) 4450 this->gogo_->add_block(this->gogo_->finish_block(location), 4451 location); 4452 this->gogo_->add_block(this->gogo_->finish_block(location), 4453 location); 4454 return; 4455 } 4456 if (type_switch.found) 4457 type_switch.expr = Expression::make_error(location); 4458 } 4459 else 4460 { 4461 error_at(this->location(), "expected %<{%>"); 4462 if (have_type_switch_block) 4463 this->gogo_->add_block(this->gogo_->finish_block(this->location()), 4464 location); 4465 this->gogo_->add_block(this->gogo_->finish_block(this->location()), 4466 location); 4467 return; 4468 } 4469 } 4470 this->advance_token(); 4471 4472 Statement* statement; 4473 if (type_switch.found) 4474 statement = this->type_switch_body(label, type_switch, location); 4475 else 4476 statement = this->expr_switch_body(label, switch_val, location); 4477 4478 if (statement != NULL) 4479 this->gogo_->add_statement(statement); 4480 4481 if (have_type_switch_block) 4482 this->gogo_->add_block(this->gogo_->finish_block(this->location()), 4483 location); 4484 4485 this->gogo_->add_block(this->gogo_->finish_block(this->location()), 4486 location); 4487} 4488 4489// The body of an expression switch. 4490// "{" { ExprCaseClause } "}" 4491 4492Statement* 4493Parse::expr_switch_body(Label* label, Expression* switch_val, 4494 Location location) 4495{ 4496 Switch_statement* statement = Statement::make_switch_statement(switch_val, 4497 location); 4498 4499 this->push_break_statement(statement, label); 4500 4501 Case_clauses* case_clauses = new Case_clauses(); 4502 bool saw_default = false; 4503 while (!this->peek_token()->is_op(OPERATOR_RCURLY)) 4504 { 4505 if (this->peek_token()->is_eof()) 4506 { 4507 if (!saw_errors()) 4508 error_at(this->location(), "missing %<}%>"); 4509 return NULL; 4510 } 4511 this->expr_case_clause(case_clauses, &saw_default); 4512 } 4513 this->advance_token(); 4514 4515 statement->add_clauses(case_clauses); 4516 4517 this->pop_break_statement(); 4518 4519 return statement; 4520} 4521 4522// ExprCaseClause = ExprSwitchCase ":" [ StatementList ] . 4523// FallthroughStat = "fallthrough" . 4524 4525void 4526Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default) 4527{ 4528 Location location = this->location(); 4529 4530 bool is_default = false; 4531 Expression_list* vals = this->expr_switch_case(&is_default); 4532 4533 if (!this->peek_token()->is_op(OPERATOR_COLON)) 4534 { 4535 if (!saw_errors()) 4536 error_at(this->location(), "expected %<:%>"); 4537 return; 4538 } 4539 else 4540 this->advance_token(); 4541 4542 Block* statements = NULL; 4543 if (this->statement_list_may_start_here()) 4544 { 4545 this->gogo_->start_block(this->location()); 4546 this->statement_list(); 4547 statements = this->gogo_->finish_block(this->location()); 4548 } 4549 4550 bool is_fallthrough = false; 4551 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH)) 4552 { 4553 Location fallthrough_loc = this->location(); 4554 is_fallthrough = true; 4555 if (this->advance_token()->is_op(OPERATOR_SEMICOLON)) 4556 this->advance_token(); 4557 if (this->peek_token()->is_op(OPERATOR_RCURLY)) 4558 error_at(fallthrough_loc, _("cannot fallthrough final case in switch")); 4559 } 4560 4561 if (is_default) 4562 { 4563 if (*saw_default) 4564 { 4565 error_at(location, "multiple defaults in switch"); 4566 return; 4567 } 4568 *saw_default = true; 4569 } 4570 4571 if (is_default || vals != NULL) 4572 clauses->add(vals, is_default, statements, is_fallthrough, location); 4573} 4574 4575// ExprSwitchCase = "case" ExpressionList | "default" . 4576 4577Expression_list* 4578Parse::expr_switch_case(bool* is_default) 4579{ 4580 const Token* token = this->peek_token(); 4581 if (token->is_keyword(KEYWORD_CASE)) 4582 { 4583 this->advance_token(); 4584 return this->expression_list(NULL, false, true); 4585 } 4586 else if (token->is_keyword(KEYWORD_DEFAULT)) 4587 { 4588 this->advance_token(); 4589 *is_default = true; 4590 return NULL; 4591 } 4592 else 4593 { 4594 if (!saw_errors()) 4595 error_at(this->location(), "expected %<case%> or %<default%>"); 4596 if (!token->is_op(OPERATOR_RCURLY)) 4597 this->advance_token(); 4598 return NULL; 4599 } 4600} 4601 4602// The body of a type switch. 4603// "{" { TypeCaseClause } "}" . 4604 4605Statement* 4606Parse::type_switch_body(Label* label, const Type_switch& type_switch, 4607 Location location) 4608{ 4609 Expression* init = type_switch.expr; 4610 std::string var_name = type_switch.name; 4611 if (!var_name.empty()) 4612 { 4613 if (Gogo::is_sink_name(var_name)) 4614 { 4615 error_at(type_switch.location, 4616 "no new variables on left side of %<:=%>"); 4617 var_name.clear(); 4618 } 4619 else 4620 { 4621 Location loc = type_switch.location; 4622 Temporary_statement* switch_temp = 4623 Statement::make_temporary(NULL, init, loc); 4624 this->gogo_->add_statement(switch_temp); 4625 init = Expression::make_temporary_reference(switch_temp, loc); 4626 } 4627 } 4628 4629 Type_switch_statement* statement = 4630 Statement::make_type_switch_statement(var_name, init, location); 4631 this->push_break_statement(statement, label); 4632 4633 Type_case_clauses* case_clauses = new Type_case_clauses(); 4634 bool saw_default = false; 4635 std::vector<Named_object*> implicit_vars; 4636 while (!this->peek_token()->is_op(OPERATOR_RCURLY)) 4637 { 4638 if (this->peek_token()->is_eof()) 4639 { 4640 error_at(this->location(), "missing %<}%>"); 4641 return NULL; 4642 } 4643 this->type_case_clause(var_name, init, case_clauses, &saw_default, 4644 &implicit_vars); 4645 } 4646 this->advance_token(); 4647 4648 statement->add_clauses(case_clauses); 4649 4650 this->pop_break_statement(); 4651 4652 // If there is a type switch variable implicitly declared in each case clause, 4653 // check that it is used in at least one of the cases. 4654 if (!var_name.empty()) 4655 { 4656 bool used = false; 4657 for (std::vector<Named_object*>::iterator p = implicit_vars.begin(); 4658 p != implicit_vars.end(); 4659 ++p) 4660 { 4661 if ((*p)->var_value()->is_used()) 4662 { 4663 used = true; 4664 break; 4665 } 4666 } 4667 if (!used) 4668 error_at(type_switch.location, "%qs declared and not used", 4669 Gogo::message_name(var_name).c_str()); 4670 } 4671 return statement; 4672} 4673 4674// TypeCaseClause = TypeSwitchCase ":" [ StatementList ] . 4675// IMPLICIT_VARS is the list of variables implicitly declared for each type 4676// case if there is a type switch variable declared. 4677 4678void 4679Parse::type_case_clause(const std::string& var_name, Expression* init, 4680 Type_case_clauses* clauses, bool* saw_default, 4681 std::vector<Named_object*>* implicit_vars) 4682{ 4683 Location location = this->location(); 4684 4685 std::vector<Type*> types; 4686 bool is_default = false; 4687 this->type_switch_case(&types, &is_default); 4688 4689 if (!this->peek_token()->is_op(OPERATOR_COLON)) 4690 error_at(this->location(), "expected %<:%>"); 4691 else 4692 this->advance_token(); 4693 4694 Block* statements = NULL; 4695 if (this->statement_list_may_start_here()) 4696 { 4697 this->gogo_->start_block(this->location()); 4698 if (!var_name.empty()) 4699 { 4700 Type* type = NULL; 4701 Location var_loc = init->location(); 4702 if (types.size() == 1) 4703 { 4704 type = types.front(); 4705 init = Expression::make_type_guard(init, type, location); 4706 } 4707 4708 Variable* v = new Variable(type, init, false, false, false, 4709 var_loc); 4710 v->set_is_used(); 4711 v->set_is_type_switch_var(); 4712 implicit_vars->push_back(this->gogo_->add_variable(var_name, v)); 4713 } 4714 this->statement_list(); 4715 statements = this->gogo_->finish_block(this->location()); 4716 } 4717 4718 if (this->peek_token()->is_keyword(KEYWORD_FALLTHROUGH)) 4719 { 4720 error_at(this->location(), 4721 "fallthrough is not permitted in a type switch"); 4722 if (this->advance_token()->is_op(OPERATOR_SEMICOLON)) 4723 this->advance_token(); 4724 } 4725 4726 if (is_default) 4727 { 4728 go_assert(types.empty()); 4729 if (*saw_default) 4730 { 4731 error_at(location, "multiple defaults in type switch"); 4732 return; 4733 } 4734 *saw_default = true; 4735 clauses->add(NULL, false, true, statements, location); 4736 } 4737 else if (!types.empty()) 4738 { 4739 for (std::vector<Type*>::const_iterator p = types.begin(); 4740 p + 1 != types.end(); 4741 ++p) 4742 clauses->add(*p, true, false, NULL, location); 4743 clauses->add(types.back(), false, false, statements, location); 4744 } 4745 else 4746 clauses->add(Type::make_error_type(), false, false, statements, location); 4747} 4748 4749// TypeSwitchCase = "case" type | "default" 4750 4751// We accept a comma separated list of types. 4752 4753void 4754Parse::type_switch_case(std::vector<Type*>* types, bool* is_default) 4755{ 4756 const Token* token = this->peek_token(); 4757 if (token->is_keyword(KEYWORD_CASE)) 4758 { 4759 this->advance_token(); 4760 while (true) 4761 { 4762 Type* t = this->type(); 4763 4764 if (!t->is_error_type()) 4765 types->push_back(t); 4766 else 4767 { 4768 this->gogo_->mark_locals_used(); 4769 token = this->peek_token(); 4770 while (!token->is_op(OPERATOR_COLON) 4771 && !token->is_op(OPERATOR_COMMA) 4772 && !token->is_op(OPERATOR_RCURLY) 4773 && !token->is_eof()) 4774 token = this->advance_token(); 4775 } 4776 4777 if (!this->peek_token()->is_op(OPERATOR_COMMA)) 4778 break; 4779 this->advance_token(); 4780 } 4781 } 4782 else if (token->is_keyword(KEYWORD_DEFAULT)) 4783 { 4784 this->advance_token(); 4785 *is_default = true; 4786 } 4787 else 4788 { 4789 error_at(this->location(), "expected %<case%> or %<default%>"); 4790 if (!token->is_op(OPERATOR_RCURLY)) 4791 this->advance_token(); 4792 } 4793} 4794 4795// SelectStat = "select" "{" { CommClause } "}" . 4796 4797void 4798Parse::select_stat(Label* label) 4799{ 4800 go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT)); 4801 Location location = this->location(); 4802 const Token* token = this->advance_token(); 4803 4804 if (!token->is_op(OPERATOR_LCURLY)) 4805 { 4806 Location token_loc = token->location(); 4807 if (token->is_op(OPERATOR_SEMICOLON) 4808 && this->advance_token()->is_op(OPERATOR_LCURLY)) 4809 error_at(token_loc, "unexpected semicolon or newline before %<{%>"); 4810 else 4811 { 4812 error_at(this->location(), "expected %<{%>"); 4813 return; 4814 } 4815 } 4816 this->advance_token(); 4817 4818 Select_statement* statement = Statement::make_select_statement(location); 4819 4820 this->push_break_statement(statement, label); 4821 4822 Select_clauses* select_clauses = new Select_clauses(); 4823 bool saw_default = false; 4824 while (!this->peek_token()->is_op(OPERATOR_RCURLY)) 4825 { 4826 if (this->peek_token()->is_eof()) 4827 { 4828 error_at(this->location(), "expected %<}%>"); 4829 return; 4830 } 4831 this->comm_clause(select_clauses, &saw_default); 4832 } 4833 4834 this->advance_token(); 4835 4836 statement->add_clauses(select_clauses); 4837 4838 this->pop_break_statement(); 4839 4840 this->gogo_->add_statement(statement); 4841} 4842 4843// CommClause = CommCase ":" { Statement ";" } . 4844 4845void 4846Parse::comm_clause(Select_clauses* clauses, bool* saw_default) 4847{ 4848 Location location = this->location(); 4849 bool is_send = false; 4850 Expression* channel = NULL; 4851 Expression* val = NULL; 4852 Expression* closed = NULL; 4853 std::string varname; 4854 std::string closedname; 4855 bool is_default = false; 4856 bool got_case = this->comm_case(&is_send, &channel, &val, &closed, 4857 &varname, &closedname, &is_default); 4858 4859 if (!is_send 4860 && varname.empty() 4861 && closedname.empty() 4862 && val != NULL 4863 && val->index_expression() != NULL) 4864 val->index_expression()->set_is_lvalue(); 4865 4866 if (this->peek_token()->is_op(OPERATOR_COLON)) 4867 this->advance_token(); 4868 else 4869 error_at(this->location(), "expected colon"); 4870 4871 this->gogo_->start_block(this->location()); 4872 4873 Named_object* var = NULL; 4874 if (!varname.empty()) 4875 { 4876 // FIXME: LOCATION is slightly wrong here. 4877 Variable* v = new Variable(NULL, channel, false, false, false, 4878 location); 4879 v->set_type_from_chan_element(); 4880 var = this->gogo_->add_variable(varname, v); 4881 } 4882 4883 Named_object* closedvar = NULL; 4884 if (!closedname.empty()) 4885 { 4886 // FIXME: LOCATION is slightly wrong here. 4887 Variable* v = new Variable(Type::lookup_bool_type(), NULL, 4888 false, false, false, location); 4889 closedvar = this->gogo_->add_variable(closedname, v); 4890 } 4891 4892 this->statement_list(); 4893 4894 Block* statements = this->gogo_->finish_block(this->location()); 4895 4896 if (is_default) 4897 { 4898 if (*saw_default) 4899 { 4900 error_at(location, "multiple defaults in select"); 4901 return; 4902 } 4903 *saw_default = true; 4904 } 4905 4906 if (got_case) 4907 clauses->add(is_send, channel, val, closed, var, closedvar, is_default, 4908 statements, location); 4909 else if (statements != NULL) 4910 { 4911 // Add the statements to make sure that any names they define 4912 // are traversed. 4913 this->gogo_->add_block(statements, location); 4914 } 4915} 4916 4917// CommCase = "case" ( SendStmt | RecvStmt ) | "default" . 4918 4919bool 4920Parse::comm_case(bool* is_send, Expression** channel, Expression** val, 4921 Expression** closed, std::string* varname, 4922 std::string* closedname, bool* is_default) 4923{ 4924 const Token* token = this->peek_token(); 4925 if (token->is_keyword(KEYWORD_DEFAULT)) 4926 { 4927 this->advance_token(); 4928 *is_default = true; 4929 } 4930 else if (token->is_keyword(KEYWORD_CASE)) 4931 { 4932 this->advance_token(); 4933 if (!this->send_or_recv_stmt(is_send, channel, val, closed, varname, 4934 closedname)) 4935 return false; 4936 } 4937 else 4938 { 4939 error_at(this->location(), "expected %<case%> or %<default%>"); 4940 if (!token->is_op(OPERATOR_RCURLY)) 4941 this->advance_token(); 4942 return false; 4943 } 4944 4945 return true; 4946} 4947 4948// RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr . 4949// RecvExpr = Expression . 4950 4951bool 4952Parse::send_or_recv_stmt(bool* is_send, Expression** channel, Expression** val, 4953 Expression** closed, std::string* varname, 4954 std::string* closedname) 4955{ 4956 const Token* token = this->peek_token(); 4957 bool saw_comma = false; 4958 bool closed_is_id = false; 4959 if (token->is_identifier()) 4960 { 4961 Gogo* gogo = this->gogo_; 4962 std::string recv_var = token->identifier(); 4963 bool is_rv_exported = token->is_identifier_exported(); 4964 Location recv_var_loc = token->location(); 4965 token = this->advance_token(); 4966 if (token->is_op(OPERATOR_COLONEQ)) 4967 { 4968 // case rv := <-c: 4969 this->advance_token(); 4970 Expression* e = this->expression(PRECEDENCE_NORMAL, false, false, 4971 NULL, NULL); 4972 Receive_expression* re = e->receive_expression(); 4973 if (re == NULL) 4974 { 4975 if (!e->is_error_expression()) 4976 error_at(this->location(), "expected receive expression"); 4977 return false; 4978 } 4979 if (recv_var == "_") 4980 { 4981 error_at(recv_var_loc, 4982 "no new variables on left side of %<:=%>"); 4983 recv_var = Gogo::erroneous_name(); 4984 } 4985 *is_send = false; 4986 *varname = gogo->pack_hidden_name(recv_var, is_rv_exported); 4987 *channel = re->channel(); 4988 return true; 4989 } 4990 else if (token->is_op(OPERATOR_COMMA)) 4991 { 4992 token = this->advance_token(); 4993 if (token->is_identifier()) 4994 { 4995 std::string recv_closed = token->identifier(); 4996 bool is_rc_exported = token->is_identifier_exported(); 4997 Location recv_closed_loc = token->location(); 4998 closed_is_id = true; 4999 5000 token = this->advance_token(); 5001 if (token->is_op(OPERATOR_COLONEQ)) 5002 { 5003 // case rv, rc := <-c: 5004 this->advance_token(); 5005 Expression* e = this->expression(PRECEDENCE_NORMAL, false, 5006 false, NULL, NULL); 5007 Receive_expression* re = e->receive_expression(); 5008 if (re == NULL) 5009 { 5010 if (!e->is_error_expression()) 5011 error_at(this->location(), 5012 "expected receive expression"); 5013 return false; 5014 } 5015 if (recv_var == "_" && recv_closed == "_") 5016 { 5017 error_at(recv_var_loc, 5018 "no new variables on left side of %<:=%>"); 5019 recv_var = Gogo::erroneous_name(); 5020 } 5021 *is_send = false; 5022 if (recv_var != "_") 5023 *varname = gogo->pack_hidden_name(recv_var, 5024 is_rv_exported); 5025 if (recv_closed != "_") 5026 *closedname = gogo->pack_hidden_name(recv_closed, 5027 is_rc_exported); 5028 *channel = re->channel(); 5029 return true; 5030 } 5031 5032 this->unget_token(Token::make_identifier_token(recv_closed, 5033 is_rc_exported, 5034 recv_closed_loc)); 5035 } 5036 5037 *val = this->id_to_expression(gogo->pack_hidden_name(recv_var, 5038 is_rv_exported), 5039 recv_var_loc, true); 5040 saw_comma = true; 5041 } 5042 else 5043 this->unget_token(Token::make_identifier_token(recv_var, 5044 is_rv_exported, 5045 recv_var_loc)); 5046 } 5047 5048 // If SAW_COMMA is false, then we are looking at the start of the 5049 // send or receive expression. If SAW_COMMA is true, then *VAL is 5050 // set and we just read a comma. 5051 5052 Expression* e; 5053 if (saw_comma || !this->peek_token()->is_op(OPERATOR_CHANOP)) 5054 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL); 5055 else 5056 { 5057 // case <-c: 5058 *is_send = false; 5059 this->advance_token(); 5060 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 5061 5062 // The next token should be ':'. If it is '<-', then we have 5063 // case <-c <- v: 5064 // which is to say, send on a channel received from a channel. 5065 if (!this->peek_token()->is_op(OPERATOR_CHANOP)) 5066 return true; 5067 5068 e = Expression::make_receive(*channel, (*channel)->location()); 5069 } 5070 5071 if (!saw_comma && this->peek_token()->is_op(OPERATOR_COMMA)) 5072 { 5073 this->advance_token(); 5074 // case v, e = <-c: 5075 if (!e->is_sink_expression()) 5076 *val = e; 5077 e = this->expression(PRECEDENCE_NORMAL, true, true, NULL, NULL); 5078 saw_comma = true; 5079 } 5080 5081 if (this->peek_token()->is_op(OPERATOR_EQ)) 5082 { 5083 if (!this->advance_token()->is_op(OPERATOR_CHANOP)) 5084 { 5085 error_at(this->location(), "missing %<<-%>"); 5086 return false; 5087 } 5088 *is_send = false; 5089 this->advance_token(); 5090 *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 5091 if (saw_comma) 5092 { 5093 // case v, e = <-c: 5094 // *VAL is already set. 5095 if (!e->is_sink_expression()) 5096 *closed = e; 5097 } 5098 else 5099 { 5100 // case v = <-c: 5101 if (!e->is_sink_expression()) 5102 *val = e; 5103 } 5104 return true; 5105 } 5106 5107 if (saw_comma) 5108 { 5109 if (closed_is_id) 5110 error_at(this->location(), "expected %<=%> or %<:=%>"); 5111 else 5112 error_at(this->location(), "expected %<=%>"); 5113 return false; 5114 } 5115 5116 if (this->peek_token()->is_op(OPERATOR_CHANOP)) 5117 { 5118 // case c <- v: 5119 *is_send = true; 5120 *channel = this->verify_not_sink(e); 5121 this->advance_token(); 5122 *val = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 5123 return true; 5124 } 5125 5126 error_at(this->location(), "expected %<<-%> or %<=%>"); 5127 return false; 5128} 5129 5130// ForStat = "for" [ Condition | ForClause | RangeClause ] Block . 5131// Condition = Expression . 5132 5133void 5134Parse::for_stat(Label* label) 5135{ 5136 go_assert(this->peek_token()->is_keyword(KEYWORD_FOR)); 5137 Location location = this->location(); 5138 const Token* token = this->advance_token(); 5139 5140 // Open a block to hold any variables defined in the init statement 5141 // of the for statement. 5142 this->gogo_->start_block(location); 5143 5144 Block* init = NULL; 5145 Expression* cond = NULL; 5146 Block* post = NULL; 5147 Range_clause range_clause; 5148 5149 if (!token->is_op(OPERATOR_LCURLY)) 5150 { 5151 if (token->is_keyword(KEYWORD_VAR)) 5152 { 5153 error_at(this->location(), 5154 "var declaration not allowed in for initializer"); 5155 this->var_decl(); 5156 } 5157 5158 if (token->is_op(OPERATOR_SEMICOLON)) 5159 this->for_clause(&cond, &post); 5160 else 5161 { 5162 // We might be looking at a Condition, an InitStat, or a 5163 // RangeClause. 5164 bool saw_send_stmt; 5165 cond = this->simple_stat(false, &saw_send_stmt, &range_clause, NULL); 5166 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON)) 5167 { 5168 if (cond == NULL && !range_clause.found) 5169 { 5170 if (saw_send_stmt) 5171 error_at(this->location(), 5172 ("send statement used as value; " 5173 "use select for non-blocking send")); 5174 else 5175 error_at(this->location(), "parse error in for statement"); 5176 } 5177 } 5178 else 5179 { 5180 if (range_clause.found) 5181 error_at(this->location(), "parse error after range clause"); 5182 5183 if (cond != NULL) 5184 { 5185 // COND is actually an expression statement for 5186 // InitStat at the start of a ForClause. 5187 this->expression_stat(cond); 5188 cond = NULL; 5189 } 5190 5191 this->for_clause(&cond, &post); 5192 } 5193 } 5194 } 5195 5196 // Check for the easy error of a newline before starting the block. 5197 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 5198 { 5199 Location semi_loc = this->location(); 5200 if (this->advance_token()->is_op(OPERATOR_LCURLY)) 5201 error_at(semi_loc, "missing %<{%> after for clause"); 5202 // Otherwise we will get an error when we call this->block 5203 // below. 5204 } 5205 5206 // Build the For_statement and note that it is the current target 5207 // for break and continue statements. 5208 5209 For_statement* sfor; 5210 For_range_statement* srange; 5211 Statement* s; 5212 if (!range_clause.found) 5213 { 5214 sfor = Statement::make_for_statement(init, cond, post, location); 5215 s = sfor; 5216 srange = NULL; 5217 } 5218 else 5219 { 5220 srange = Statement::make_for_range_statement(range_clause.index, 5221 range_clause.value, 5222 range_clause.range, 5223 location); 5224 s = srange; 5225 sfor = NULL; 5226 } 5227 5228 this->push_break_statement(s, label); 5229 this->push_continue_statement(s, label); 5230 5231 // Gather the block of statements in the loop and add them to the 5232 // For_statement. 5233 5234 this->gogo_->start_block(this->location()); 5235 Location end_loc = this->block(); 5236 Block* statements = this->gogo_->finish_block(end_loc); 5237 5238 if (sfor != NULL) 5239 sfor->add_statements(statements); 5240 else 5241 srange->add_statements(statements); 5242 5243 // This is no longer the break/continue target. 5244 this->pop_break_statement(); 5245 this->pop_continue_statement(); 5246 5247 // Add the For_statement to the list of statements, and close out 5248 // the block we started to hold any variables defined in the for 5249 // statement. 5250 5251 this->gogo_->add_statement(s); 5252 5253 this->gogo_->add_block(this->gogo_->finish_block(this->location()), 5254 location); 5255} 5256 5257// ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] . 5258// InitStat = SimpleStat . 5259// PostStat = SimpleStat . 5260 5261// We have already read InitStat at this point. 5262 5263void 5264Parse::for_clause(Expression** cond, Block** post) 5265{ 5266 go_assert(this->peek_token()->is_op(OPERATOR_SEMICOLON)); 5267 this->advance_token(); 5268 if (this->peek_token()->is_op(OPERATOR_SEMICOLON)) 5269 *cond = NULL; 5270 else if (this->peek_token()->is_op(OPERATOR_LCURLY)) 5271 { 5272 error_at(this->location(), "missing %<{%> after for clause"); 5273 *cond = NULL; 5274 *post = NULL; 5275 return; 5276 } 5277 else 5278 *cond = this->expression(PRECEDENCE_NORMAL, false, true, NULL, NULL); 5279 if (!this->peek_token()->is_op(OPERATOR_SEMICOLON)) 5280 error_at(this->location(), "expected semicolon"); 5281 else 5282 this->advance_token(); 5283 5284 if (this->peek_token()->is_op(OPERATOR_LCURLY)) 5285 *post = NULL; 5286 else 5287 { 5288 this->gogo_->start_block(this->location()); 5289 this->simple_stat(false, NULL, NULL, NULL); 5290 *post = this->gogo_->finish_block(this->location()); 5291 } 5292} 5293 5294// RangeClause = [ IdentifierList ( "=" | ":=" ) ] "range" Expression . 5295 5296// This is the := version. It is called with a list of identifiers. 5297 5298void 5299Parse::range_clause_decl(const Typed_identifier_list* til, 5300 Range_clause* p_range_clause) 5301{ 5302 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE)); 5303 Location location = this->location(); 5304 5305 p_range_clause->found = true; 5306 5307 if (til->size() > 2) 5308 error_at(this->location(), "too many variables for range clause"); 5309 5310 this->advance_token(); 5311 Expression* expr = this->expression(PRECEDENCE_NORMAL, false, false, NULL, 5312 NULL); 5313 p_range_clause->range = expr; 5314 5315 if (til->empty()) 5316 return; 5317 5318 bool any_new = false; 5319 5320 const Typed_identifier* pti = &til->front(); 5321 Named_object* no = this->init_var(*pti, NULL, expr, true, true, &any_new, 5322 NULL, NULL); 5323 if (any_new && no->is_variable()) 5324 no->var_value()->set_type_from_range_index(); 5325 p_range_clause->index = Expression::make_var_reference(no, location); 5326 5327 if (til->size() == 1) 5328 p_range_clause->value = NULL; 5329 else 5330 { 5331 pti = &til->back(); 5332 bool is_new = false; 5333 no = this->init_var(*pti, NULL, expr, true, true, &is_new, NULL, NULL); 5334 if (is_new && no->is_variable()) 5335 no->var_value()->set_type_from_range_value(); 5336 if (is_new) 5337 any_new = true; 5338 if (!Gogo::is_sink_name(pti->name())) 5339 p_range_clause->value = Expression::make_var_reference(no, location); 5340 } 5341 5342 if (!any_new) 5343 error_at(location, "variables redeclared but no variable is new"); 5344} 5345 5346// The = version of RangeClause. This is called with a list of 5347// expressions. 5348 5349void 5350Parse::range_clause_expr(const Expression_list* vals, 5351 Range_clause* p_range_clause) 5352{ 5353 go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE)); 5354 5355 p_range_clause->found = true; 5356 5357 go_assert(vals->size() >= 1); 5358 if (vals->size() > 2) 5359 error_at(this->location(), "too many variables for range clause"); 5360 5361 this->advance_token(); 5362 p_range_clause->range = this->expression(PRECEDENCE_NORMAL, false, false, 5363 NULL, NULL); 5364 5365 if (vals->empty()) 5366 return; 5367 5368 p_range_clause->index = vals->front(); 5369 if (vals->size() == 1) 5370 p_range_clause->value = NULL; 5371 else 5372 p_range_clause->value = vals->back(); 5373} 5374 5375// Push a statement on the break stack. 5376 5377void 5378Parse::push_break_statement(Statement* enclosing, Label* label) 5379{ 5380 if (this->break_stack_ == NULL) 5381 this->break_stack_ = new Bc_stack(); 5382 this->break_stack_->push_back(std::make_pair(enclosing, label)); 5383} 5384 5385// Push a statement on the continue stack. 5386 5387void 5388Parse::push_continue_statement(Statement* enclosing, Label* label) 5389{ 5390 if (this->continue_stack_ == NULL) 5391 this->continue_stack_ = new Bc_stack(); 5392 this->continue_stack_->push_back(std::make_pair(enclosing, label)); 5393} 5394 5395// Pop the break stack. 5396 5397void 5398Parse::pop_break_statement() 5399{ 5400 this->break_stack_->pop_back(); 5401} 5402 5403// Pop the continue stack. 5404 5405void 5406Parse::pop_continue_statement() 5407{ 5408 this->continue_stack_->pop_back(); 5409} 5410 5411// Find a break or continue statement given a label name. 5412 5413Statement* 5414Parse::find_bc_statement(const Bc_stack* bc_stack, const std::string& label) 5415{ 5416 if (bc_stack == NULL) 5417 return NULL; 5418 for (Bc_stack::const_reverse_iterator p = bc_stack->rbegin(); 5419 p != bc_stack->rend(); 5420 ++p) 5421 { 5422 if (p->second != NULL && p->second->name() == label) 5423 { 5424 p->second->set_is_used(); 5425 return p->first; 5426 } 5427 } 5428 return NULL; 5429} 5430 5431// BreakStat = "break" [ identifier ] . 5432 5433void 5434Parse::break_stat() 5435{ 5436 go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK)); 5437 Location location = this->location(); 5438 5439 const Token* token = this->advance_token(); 5440 Statement* enclosing; 5441 if (!token->is_identifier()) 5442 { 5443 if (this->break_stack_ == NULL || this->break_stack_->empty()) 5444 { 5445 error_at(this->location(), 5446 "break statement not within for or switch or select"); 5447 return; 5448 } 5449 enclosing = this->break_stack_->back().first; 5450 } 5451 else 5452 { 5453 enclosing = this->find_bc_statement(this->break_stack_, 5454 token->identifier()); 5455 if (enclosing == NULL) 5456 { 5457 // If there is a label with this name, mark it as used to 5458 // avoid a useless error about an unused label. 5459 this->gogo_->add_label_reference(token->identifier(), 5460 Linemap::unknown_location(), false); 5461 5462 error_at(token->location(), "invalid break label %qs", 5463 Gogo::message_name(token->identifier()).c_str()); 5464 this->advance_token(); 5465 return; 5466 } 5467 this->advance_token(); 5468 } 5469 5470 Unnamed_label* label; 5471 if (enclosing->classification() == Statement::STATEMENT_FOR) 5472 label = enclosing->for_statement()->break_label(); 5473 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE) 5474 label = enclosing->for_range_statement()->break_label(); 5475 else if (enclosing->classification() == Statement::STATEMENT_SWITCH) 5476 label = enclosing->switch_statement()->break_label(); 5477 else if (enclosing->classification() == Statement::STATEMENT_TYPE_SWITCH) 5478 label = enclosing->type_switch_statement()->break_label(); 5479 else if (enclosing->classification() == Statement::STATEMENT_SELECT) 5480 label = enclosing->select_statement()->break_label(); 5481 else 5482 go_unreachable(); 5483 5484 this->gogo_->add_statement(Statement::make_break_statement(label, 5485 location)); 5486} 5487 5488// ContinueStat = "continue" [ identifier ] . 5489 5490void 5491Parse::continue_stat() 5492{ 5493 go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE)); 5494 Location location = this->location(); 5495 5496 const Token* token = this->advance_token(); 5497 Statement* enclosing; 5498 if (!token->is_identifier()) 5499 { 5500 if (this->continue_stack_ == NULL || this->continue_stack_->empty()) 5501 { 5502 error_at(this->location(), "continue statement not within for"); 5503 return; 5504 } 5505 enclosing = this->continue_stack_->back().first; 5506 } 5507 else 5508 { 5509 enclosing = this->find_bc_statement(this->continue_stack_, 5510 token->identifier()); 5511 if (enclosing == NULL) 5512 { 5513 // If there is a label with this name, mark it as used to 5514 // avoid a useless error about an unused label. 5515 this->gogo_->add_label_reference(token->identifier(), 5516 Linemap::unknown_location(), false); 5517 5518 error_at(token->location(), "invalid continue label %qs", 5519 Gogo::message_name(token->identifier()).c_str()); 5520 this->advance_token(); 5521 return; 5522 } 5523 this->advance_token(); 5524 } 5525 5526 Unnamed_label* label; 5527 if (enclosing->classification() == Statement::STATEMENT_FOR) 5528 label = enclosing->for_statement()->continue_label(); 5529 else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE) 5530 label = enclosing->for_range_statement()->continue_label(); 5531 else 5532 go_unreachable(); 5533 5534 this->gogo_->add_statement(Statement::make_continue_statement(label, 5535 location)); 5536} 5537 5538// GotoStat = "goto" identifier . 5539 5540void 5541Parse::goto_stat() 5542{ 5543 go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO)); 5544 Location location = this->location(); 5545 const Token* token = this->advance_token(); 5546 if (!token->is_identifier()) 5547 error_at(this->location(), "expected label for goto"); 5548 else 5549 { 5550 Label* label = this->gogo_->add_label_reference(token->identifier(), 5551 location, true); 5552 Statement* s = Statement::make_goto_statement(label, location); 5553 this->gogo_->add_statement(s); 5554 this->advance_token(); 5555 } 5556} 5557 5558// PackageClause = "package" PackageName . 5559 5560void 5561Parse::package_clause() 5562{ 5563 const Token* token = this->peek_token(); 5564 Location location = token->location(); 5565 std::string name; 5566 if (!token->is_keyword(KEYWORD_PACKAGE)) 5567 { 5568 error_at(this->location(), "program must start with package clause"); 5569 name = "ERROR"; 5570 } 5571 else 5572 { 5573 token = this->advance_token(); 5574 if (token->is_identifier()) 5575 { 5576 name = token->identifier(); 5577 if (name == "_") 5578 { 5579 error_at(this->location(), "invalid package name _"); 5580 name = Gogo::erroneous_name(); 5581 } 5582 this->advance_token(); 5583 } 5584 else 5585 { 5586 error_at(this->location(), "package name must be an identifier"); 5587 name = "ERROR"; 5588 } 5589 } 5590 this->gogo_->set_package_name(name, location); 5591} 5592 5593// ImportDecl = "import" Decl<ImportSpec> . 5594 5595void 5596Parse::import_decl() 5597{ 5598 go_assert(this->peek_token()->is_keyword(KEYWORD_IMPORT)); 5599 this->advance_token(); 5600 this->decl(&Parse::import_spec, NULL); 5601} 5602 5603// ImportSpec = [ "." | PackageName ] PackageFileName . 5604 5605void 5606Parse::import_spec(void*) 5607{ 5608 const Token* token = this->peek_token(); 5609 Location location = token->location(); 5610 5611 std::string local_name; 5612 bool is_local_name_exported = false; 5613 if (token->is_op(OPERATOR_DOT)) 5614 { 5615 local_name = "."; 5616 token = this->advance_token(); 5617 } 5618 else if (token->is_identifier()) 5619 { 5620 local_name = token->identifier(); 5621 is_local_name_exported = token->is_identifier_exported(); 5622 token = this->advance_token(); 5623 } 5624 5625 if (!token->is_string()) 5626 { 5627 error_at(this->location(), "import statement not a string"); 5628 this->advance_token(); 5629 return; 5630 } 5631 5632 this->gogo_->import_package(token->string_value(), local_name, 5633 is_local_name_exported, location); 5634 5635 this->advance_token(); 5636} 5637 5638// SourceFile = PackageClause ";" { ImportDecl ";" } 5639// { TopLevelDecl ";" } . 5640 5641void 5642Parse::program() 5643{ 5644 this->package_clause(); 5645 5646 const Token* token = this->peek_token(); 5647 if (token->is_op(OPERATOR_SEMICOLON)) 5648 token = this->advance_token(); 5649 else 5650 error_at(this->location(), 5651 "expected %<;%> or newline after package clause"); 5652 5653 while (token->is_keyword(KEYWORD_IMPORT)) 5654 { 5655 this->import_decl(); 5656 token = this->peek_token(); 5657 if (token->is_op(OPERATOR_SEMICOLON)) 5658 token = this->advance_token(); 5659 else 5660 error_at(this->location(), 5661 "expected %<;%> or newline after import declaration"); 5662 } 5663 5664 while (!token->is_eof()) 5665 { 5666 if (this->declaration_may_start_here()) 5667 this->declaration(); 5668 else 5669 { 5670 error_at(this->location(), "expected declaration"); 5671 this->gogo_->mark_locals_used(); 5672 do 5673 this->advance_token(); 5674 while (!this->peek_token()->is_eof() 5675 && !this->peek_token()->is_op(OPERATOR_SEMICOLON) 5676 && !this->peek_token()->is_op(OPERATOR_RCURLY)); 5677 if (!this->peek_token()->is_eof() 5678 && !this->peek_token()->is_op(OPERATOR_SEMICOLON)) 5679 this->advance_token(); 5680 } 5681 token = this->peek_token(); 5682 if (token->is_op(OPERATOR_SEMICOLON)) 5683 token = this->advance_token(); 5684 else if (!token->is_eof() || !saw_errors()) 5685 { 5686 if (token->is_op(OPERATOR_CHANOP)) 5687 error_at(this->location(), 5688 ("send statement used as value; " 5689 "use select for non-blocking send")); 5690 else 5691 error_at(this->location(), 5692 "expected %<;%> or newline after top level declaration"); 5693 this->skip_past_error(OPERATOR_INVALID); 5694 } 5695 } 5696} 5697 5698// Reset the current iota value. 5699 5700void 5701Parse::reset_iota() 5702{ 5703 this->iota_ = 0; 5704} 5705 5706// Return the current iota value. 5707 5708int 5709Parse::iota_value() 5710{ 5711 return this->iota_; 5712} 5713 5714// Increment the current iota value. 5715 5716void 5717Parse::increment_iota() 5718{ 5719 ++this->iota_; 5720} 5721 5722// Skip forward to a semicolon or OP. OP will normally be 5723// OPERATOR_RPAREN or OPERATOR_RCURLY. If we find a semicolon, move 5724// past it and return. If we find OP, it will be the next token to 5725// read. Return true if we are OK, false if we found EOF. 5726 5727bool 5728Parse::skip_past_error(Operator op) 5729{ 5730 this->gogo_->mark_locals_used(); 5731 const Token* token = this->peek_token(); 5732 while (!token->is_op(op)) 5733 { 5734 if (token->is_eof()) 5735 return false; 5736 if (token->is_op(OPERATOR_SEMICOLON)) 5737 { 5738 this->advance_token(); 5739 return true; 5740 } 5741 token = this->advance_token(); 5742 } 5743 return true; 5744} 5745 5746// Check that an expression is not a sink. 5747 5748Expression* 5749Parse::verify_not_sink(Expression* expr) 5750{ 5751 if (expr->is_sink_expression()) 5752 { 5753 error_at(expr->location(), "cannot use _ as value"); 5754 expr = Expression::make_error(expr->location()); 5755 } 5756 5757 // If this can not be a sink, and it is a variable, then we are 5758 // using the variable, not just assigning to it. 5759 Var_expression* ve = expr->var_expression(); 5760 if (ve != NULL) 5761 this->mark_var_used(ve->named_object()); 5762 else if (expr->deref()->field_reference_expression() != NULL 5763 && this->gogo_->current_function() != NULL) 5764 { 5765 // We could be looking at a variable referenced from a closure. 5766 // If so, we need to get the enclosed variable and mark it as used. 5767 Function* this_function = this->gogo_->current_function()->func_value(); 5768 Named_object* closure = this_function->closure_var(); 5769 if (closure != NULL) 5770 { 5771 unsigned int var_index = 5772 expr->deref()->field_reference_expression()->field_index(); 5773 this->mark_var_used(this_function->enclosing_var(var_index - 1)); 5774 } 5775 } 5776 5777 return expr; 5778} 5779 5780// Mark a variable as used. 5781 5782void 5783Parse::mark_var_used(Named_object* no) 5784{ 5785 if (no->is_variable()) 5786 no->var_value()->set_is_used(); 5787} 5788