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