github.com/reiver/go@v0.0.0-20150109200633-1d0c7792f172/src/cmd/gc/go.y (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 * Go language grammar. 7 * 8 * The Go semicolon rules are: 9 * 10 * 1. all statements and declarations are terminated by semicolons. 11 * 2. semicolons can be omitted before a closing ) or }. 12 * 3. semicolons are inserted by the lexer before a newline 13 * following a specific list of tokens. 14 * 15 * Rules #1 and #2 are accomplished by writing the lists as 16 * semicolon-separated lists with an optional trailing semicolon. 17 * Rule #3 is implemented in yylex. 18 */ 19 20 %{ 21 #include <u.h> 22 #include <stdio.h> /* if we don't, bison will, and go.h re-#defines getc */ 23 #include <libc.h> 24 #include "go.h" 25 26 static void fixlbrace(int); 27 %} 28 %union { 29 Node* node; 30 NodeList* list; 31 Type* type; 32 Sym* sym; 33 struct Val val; 34 int i; 35 } 36 37 // |sed 's/.* //' |9 fmt -l1 |sort |9 fmt -l50 | sed 's/^/%xxx /' 38 39 %token <val> LLITERAL 40 %token <i> LASOP LCOLAS 41 %token <sym> LBREAK LCASE LCHAN LCONST LCONTINUE LDDD 42 %token <sym> LDEFAULT LDEFER LELSE LFALL LFOR LFUNC LGO LGOTO 43 %token <sym> LIF LIMPORT LINTERFACE LMAP LNAME 44 %token <sym> LPACKAGE LRANGE LRETURN LSELECT LSTRUCT LSWITCH 45 %token <sym> LTYPE LVAR 46 47 %token LANDAND LANDNOT LBODY LCOMM LDEC LEQ LGE LGT 48 %token LIGNORE LINC LLE LLSH LLT LNE LOROR LRSH 49 50 %type <i> lbrace import_here 51 %type <sym> sym packname 52 %type <val> oliteral 53 54 %type <node> stmt ntype 55 %type <node> arg_type 56 %type <node> case caseblock 57 %type <node> compound_stmt dotname embed expr complitexpr bare_complitexpr 58 %type <node> expr_or_type 59 %type <node> fndcl hidden_fndcl fnliteral 60 %type <node> for_body for_header for_stmt if_header if_stmt non_dcl_stmt 61 %type <node> interfacedcl keyval labelname name 62 %type <node> name_or_type non_expr_type 63 %type <node> new_name dcl_name oexpr typedclname 64 %type <node> onew_name 65 %type <node> osimple_stmt pexpr pexpr_no_paren 66 %type <node> pseudocall range_stmt select_stmt 67 %type <node> simple_stmt 68 %type <node> switch_stmt uexpr 69 %type <node> xfndcl typedcl start_complit 70 71 %type <list> xdcl fnbody fnres loop_body dcl_name_list 72 %type <list> new_name_list expr_list keyval_list braced_keyval_list expr_or_type_list xdcl_list 73 %type <list> oexpr_list caseblock_list elseif elseif_list else stmt_list oarg_type_list_ocomma arg_type_list 74 %type <list> interfacedcl_list vardcl vardcl_list structdcl structdcl_list 75 %type <list> common_dcl constdcl constdcl1 constdcl_list typedcl_list 76 77 %type <node> convtype comptype dotdotdot 78 %type <node> indcl interfacetype structtype ptrtype 79 %type <node> recvchantype non_recvchantype othertype fnret_type fntype 80 81 %type <sym> hidden_importsym hidden_pkg_importsym 82 83 %type <node> hidden_constant hidden_literal hidden_funarg 84 %type <node> hidden_interfacedcl hidden_structdcl 85 86 %type <list> hidden_funres 87 %type <list> ohidden_funres 88 %type <list> hidden_funarg_list ohidden_funarg_list 89 %type <list> hidden_interfacedcl_list ohidden_interfacedcl_list 90 %type <list> hidden_structdcl_list ohidden_structdcl_list 91 92 %type <type> hidden_type hidden_type_misc hidden_pkgtype 93 %type <type> hidden_type_func 94 %type <type> hidden_type_recv_chan hidden_type_non_recv_chan 95 96 %left LCOMM /* outside the usual hierarchy; here for good error messages */ 97 98 %left LOROR 99 %left LANDAND 100 %left LEQ LNE LLE LGE LLT LGT 101 %left '+' '-' '|' '^' 102 %left '*' '/' '%' '&' LLSH LRSH LANDNOT 103 104 /* 105 * manual override of shift/reduce conflicts. 106 * the general form is that we assign a precedence 107 * to the token being shifted and then introduce 108 * NotToken with lower precedence or PreferToToken with higher 109 * and annotate the reducing rule accordingly. 110 */ 111 %left NotPackage 112 %left LPACKAGE 113 114 %left NotParen 115 %left '(' 116 117 %left ')' 118 %left PreferToRightParen 119 120 %error-verbose 121 122 %% 123 file: 124 loadsys 125 package 126 imports 127 xdcl_list 128 { 129 xtop = concat(xtop, $4); 130 } 131 132 package: 133 %prec NotPackage 134 { 135 prevlineno = lineno; 136 yyerror("package statement must be first"); 137 errorexit(); 138 } 139 | LPACKAGE sym ';' 140 { 141 mkpackage($2->name); 142 } 143 144 /* 145 * this loads the definitions for the low-level runtime functions, 146 * so that the compiler can generate calls to them, 147 * but does not make the name "runtime" visible as a package. 148 */ 149 loadsys: 150 { 151 importpkg = runtimepkg; 152 153 if(debug['A']) 154 cannedimports("runtime.builtin", "package runtime\n\n$$\n\n"); 155 else 156 cannedimports("runtime.builtin", runtimeimport); 157 curio.importsafe = 1; 158 } 159 import_package 160 import_there 161 { 162 importpkg = nil; 163 } 164 165 imports: 166 | imports import ';' 167 168 import: 169 LIMPORT import_stmt 170 | LIMPORT '(' import_stmt_list osemi ')' 171 | LIMPORT '(' ')' 172 173 import_stmt: 174 import_here import_package import_there 175 { 176 Pkg *ipkg; 177 Sym *my; 178 Node *pack; 179 180 ipkg = importpkg; 181 my = importmyname; 182 importpkg = nil; 183 importmyname = S; 184 185 if(my == nil) 186 my = lookup(ipkg->name); 187 188 pack = nod(OPACK, N, N); 189 pack->sym = my; 190 pack->pkg = ipkg; 191 pack->lineno = $1; 192 193 if(my->name[0] == '.') { 194 importdot(ipkg, pack); 195 break; 196 } 197 if(strcmp(my->name, "init") == 0) { 198 yyerror("cannot import package as init - init must be a func"); 199 break; 200 } 201 if(my->name[0] == '_' && my->name[1] == '\0') 202 break; 203 if(my->def) { 204 lineno = $1; 205 redeclare(my, "as imported package name"); 206 } 207 my->def = pack; 208 my->lastlineno = $1; 209 my->block = 1; // at top level 210 } 211 | import_here import_there 212 { 213 // When an invalid import path is passed to importfile, 214 // it calls yyerror and then sets up a fake import with 215 // no package statement. This allows us to test more 216 // than one invalid import statement in a single file. 217 if(nerrors == 0) 218 fatal("phase error in import"); 219 } 220 221 import_stmt_list: 222 import_stmt 223 | import_stmt_list ';' import_stmt 224 225 import_here: 226 LLITERAL 227 { 228 // import with original name 229 $$ = parserline(); 230 importmyname = S; 231 importfile(&$1, $$); 232 } 233 | sym LLITERAL 234 { 235 // import with given name 236 $$ = parserline(); 237 importmyname = $1; 238 importfile(&$2, $$); 239 } 240 | '.' LLITERAL 241 { 242 // import into my name space 243 $$ = parserline(); 244 importmyname = lookup("."); 245 importfile(&$2, $$); 246 } 247 248 import_package: 249 LPACKAGE LNAME import_safety ';' 250 { 251 if(importpkg->name == nil) { 252 importpkg->name = $2->name; 253 pkglookup($2->name, nil)->npkg++; 254 } else if(strcmp(importpkg->name, $2->name) != 0) 255 yyerror("conflicting names %s and %s for package \"%Z\"", importpkg->name, $2->name, importpkg->path); 256 importpkg->direct = 1; 257 importpkg->safe = curio.importsafe; 258 259 if(safemode && !curio.importsafe) 260 yyerror("cannot import unsafe package \"%Z\"", importpkg->path); 261 } 262 263 import_safety: 264 | LNAME 265 { 266 if(strcmp($1->name, "safe") == 0) 267 curio.importsafe = 1; 268 } 269 270 import_there: 271 { 272 defercheckwidth(); 273 } 274 hidden_import_list '$' '$' 275 { 276 resumecheckwidth(); 277 unimportfile(); 278 } 279 280 /* 281 * declarations 282 */ 283 xdcl: 284 { 285 yyerror("empty top-level declaration"); 286 $$ = nil; 287 } 288 | common_dcl 289 | xfndcl 290 { 291 $$ = list1($1); 292 } 293 | non_dcl_stmt 294 { 295 yyerror("non-declaration statement outside function body"); 296 $$ = nil; 297 } 298 | error 299 { 300 $$ = nil; 301 } 302 303 common_dcl: 304 LVAR vardcl 305 { 306 $$ = $2; 307 } 308 | LVAR '(' vardcl_list osemi ')' 309 { 310 $$ = $3; 311 } 312 | LVAR '(' ')' 313 { 314 $$ = nil; 315 } 316 | lconst constdcl 317 { 318 $$ = $2; 319 iota = -100000; 320 lastconst = nil; 321 } 322 | lconst '(' constdcl osemi ')' 323 { 324 $$ = $3; 325 iota = -100000; 326 lastconst = nil; 327 } 328 | lconst '(' constdcl ';' constdcl_list osemi ')' 329 { 330 $$ = concat($3, $5); 331 iota = -100000; 332 lastconst = nil; 333 } 334 | lconst '(' ')' 335 { 336 $$ = nil; 337 iota = -100000; 338 } 339 | LTYPE typedcl 340 { 341 $$ = list1($2); 342 } 343 | LTYPE '(' typedcl_list osemi ')' 344 { 345 $$ = $3; 346 } 347 | LTYPE '(' ')' 348 { 349 $$ = nil; 350 } 351 352 lconst: 353 LCONST 354 { 355 iota = 0; 356 } 357 358 vardcl: 359 dcl_name_list ntype 360 { 361 $$ = variter($1, $2, nil); 362 } 363 | dcl_name_list ntype '=' expr_list 364 { 365 $$ = variter($1, $2, $4); 366 } 367 | dcl_name_list '=' expr_list 368 { 369 $$ = variter($1, nil, $3); 370 } 371 372 constdcl: 373 dcl_name_list ntype '=' expr_list 374 { 375 $$ = constiter($1, $2, $4); 376 } 377 | dcl_name_list '=' expr_list 378 { 379 $$ = constiter($1, N, $3); 380 } 381 382 constdcl1: 383 constdcl 384 | dcl_name_list ntype 385 { 386 $$ = constiter($1, $2, nil); 387 } 388 | dcl_name_list 389 { 390 $$ = constiter($1, N, nil); 391 } 392 393 typedclname: 394 sym 395 { 396 // different from dclname because the name 397 // becomes visible right here, not at the end 398 // of the declaration. 399 $$ = typedcl0($1); 400 } 401 402 typedcl: 403 typedclname ntype 404 { 405 $$ = typedcl1($1, $2, 1); 406 } 407 408 simple_stmt: 409 expr 410 { 411 $$ = $1; 412 413 // These nodes do not carry line numbers. 414 // Since a bare name used as an expression is an error, 415 // introduce a wrapper node to give the correct line. 416 switch($$->op) { 417 case ONAME: 418 case ONONAME: 419 case OTYPE: 420 case OPACK: 421 case OLITERAL: 422 $$ = nod(OPAREN, $$, N); 423 $$->implicit = 1; 424 break; 425 } 426 } 427 | expr LASOP expr 428 { 429 $$ = nod(OASOP, $1, $3); 430 $$->etype = $2; // rathole to pass opcode 431 } 432 | expr_list '=' expr_list 433 { 434 if($1->next == nil && $3->next == nil) { 435 // simple 436 $$ = nod(OAS, $1->n, $3->n); 437 break; 438 } 439 // multiple 440 $$ = nod(OAS2, N, N); 441 $$->list = $1; 442 $$->rlist = $3; 443 } 444 | expr_list LCOLAS expr_list 445 { 446 if($3->n->op == OTYPESW) { 447 $$ = nod(OTYPESW, N, $3->n->right); 448 if($3->next != nil) 449 yyerror("expr.(type) must be alone in list"); 450 if($1->next != nil) 451 yyerror("argument count mismatch: %d = %d", count($1), 1); 452 else if(($1->n->op != ONAME && $1->n->op != OTYPE && $1->n->op != ONONAME) || isblank($1->n)) 453 yyerror("invalid variable name %N in type switch", $1->n); 454 else 455 $$->left = dclname($1->n->sym); // it's a colas, so must not re-use an oldname. 456 break; 457 } 458 $$ = colas($1, $3, $2); 459 } 460 | expr LINC 461 { 462 $$ = nod(OASOP, $1, nodintconst(1)); 463 $$->implicit = 1; 464 $$->etype = OADD; 465 } 466 | expr LDEC 467 { 468 $$ = nod(OASOP, $1, nodintconst(1)); 469 $$->implicit = 1; 470 $$->etype = OSUB; 471 } 472 473 case: 474 LCASE expr_or_type_list ':' 475 { 476 Node *n, *nn; 477 478 // will be converted to OCASE 479 // right will point to next case 480 // done in casebody() 481 markdcl(); 482 $$ = nod(OXCASE, N, N); 483 $$->list = $2; 484 if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) { 485 // type switch - declare variable 486 nn = newname(n->sym); 487 declare(nn, dclcontext); 488 $$->nname = nn; 489 490 // keep track of the instances for reporting unused 491 nn->defn = typesw->right; 492 } 493 } 494 | LCASE expr_or_type_list '=' expr ':' 495 { 496 Node *n; 497 498 // will be converted to OCASE 499 // right will point to next case 500 // done in casebody() 501 markdcl(); 502 $$ = nod(OXCASE, N, N); 503 if($2->next == nil) 504 n = nod(OAS, $2->n, $4); 505 else { 506 n = nod(OAS2, N, N); 507 n->list = $2; 508 n->rlist = list1($4); 509 } 510 $$->list = list1(n); 511 } 512 | LCASE expr_or_type_list LCOLAS expr ':' 513 { 514 // will be converted to OCASE 515 // right will point to next case 516 // done in casebody() 517 markdcl(); 518 $$ = nod(OXCASE, N, N); 519 $$->list = list1(colas($2, list1($4), $3)); 520 } 521 | LDEFAULT ':' 522 { 523 Node *n, *nn; 524 525 markdcl(); 526 $$ = nod(OXCASE, N, N); 527 if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) { 528 // type switch - declare variable 529 nn = newname(n->sym); 530 declare(nn, dclcontext); 531 $$->nname = nn; 532 533 // keep track of the instances for reporting unused 534 nn->defn = typesw->right; 535 } 536 } 537 538 compound_stmt: 539 '{' 540 { 541 markdcl(); 542 } 543 stmt_list '}' 544 { 545 if($3 == nil) 546 $$ = nod(OEMPTY, N, N); 547 else 548 $$ = liststmt($3); 549 popdcl(); 550 } 551 552 caseblock: 553 case 554 { 555 // If the last token read by the lexer was consumed 556 // as part of the case, clear it (parser has cleared yychar). 557 // If the last token read by the lexer was the lookahead 558 // leave it alone (parser has it cached in yychar). 559 // This is so that the stmt_list action doesn't look at 560 // the case tokens if the stmt_list is empty. 561 yylast = yychar; 562 $1->xoffset = block; 563 } 564 stmt_list 565 { 566 int last; 567 568 // This is the only place in the language where a statement 569 // list is not allowed to drop the final semicolon, because 570 // it's the only place where a statement list is not followed 571 // by a closing brace. Handle the error for pedantry. 572 573 // Find the final token of the statement list. 574 // yylast is lookahead; yyprev is last of stmt_list 575 last = yyprev; 576 577 if(last > 0 && last != ';' && yychar != '}') 578 yyerror("missing statement after label"); 579 $$ = $1; 580 $$->nbody = $3; 581 popdcl(); 582 } 583 584 caseblock_list: 585 { 586 $$ = nil; 587 } 588 | caseblock_list caseblock 589 { 590 $$ = list($1, $2); 591 } 592 593 loop_body: 594 LBODY 595 { 596 markdcl(); 597 } 598 stmt_list '}' 599 { 600 $$ = $3; 601 popdcl(); 602 } 603 604 range_stmt: 605 expr_list '=' LRANGE expr 606 { 607 $$ = nod(ORANGE, N, $4); 608 $$->list = $1; 609 $$->etype = 0; // := flag 610 } 611 | expr_list LCOLAS LRANGE expr 612 { 613 $$ = nod(ORANGE, N, $4); 614 $$->list = $1; 615 $$->colas = 1; 616 colasdefn($1, $$); 617 } 618 | LRANGE expr 619 { 620 $$ = nod(ORANGE, N, $2); 621 $$->etype = 0; // := flag 622 } 623 624 for_header: 625 osimple_stmt ';' osimple_stmt ';' osimple_stmt 626 { 627 // init ; test ; incr 628 if($5 != N && $5->colas != 0) 629 yyerror("cannot declare in the for-increment"); 630 $$ = nod(OFOR, N, N); 631 if($1 != N) 632 $$->ninit = list1($1); 633 $$->ntest = $3; 634 $$->nincr = $5; 635 } 636 | osimple_stmt 637 { 638 // normal test 639 $$ = nod(OFOR, N, N); 640 $$->ntest = $1; 641 } 642 | range_stmt 643 644 for_body: 645 for_header loop_body 646 { 647 $$ = $1; 648 $$->nbody = concat($$->nbody, $2); 649 } 650 651 for_stmt: 652 LFOR 653 { 654 markdcl(); 655 } 656 for_body 657 { 658 $$ = $3; 659 popdcl(); 660 } 661 662 if_header: 663 osimple_stmt 664 { 665 // test 666 $$ = nod(OIF, N, N); 667 $$->ntest = $1; 668 } 669 | osimple_stmt ';' osimple_stmt 670 { 671 // init ; test 672 $$ = nod(OIF, N, N); 673 if($1 != N) 674 $$->ninit = list1($1); 675 $$->ntest = $3; 676 } 677 678 /* IF cond body (ELSE IF cond body)* (ELSE block)? */ 679 if_stmt: 680 LIF 681 { 682 markdcl(); 683 } 684 if_header 685 { 686 if($3->ntest == N) 687 yyerror("missing condition in if statement"); 688 } 689 loop_body 690 { 691 $3->nbody = $5; 692 } 693 elseif_list else 694 { 695 Node *n; 696 NodeList *nn; 697 698 $$ = $3; 699 n = $3; 700 popdcl(); 701 for(nn = concat($7, $8); nn; nn = nn->next) { 702 if(nn->n->op == OIF) 703 popdcl(); 704 n->nelse = list1(nn->n); 705 n = nn->n; 706 } 707 } 708 709 elseif: 710 LELSE LIF 711 { 712 markdcl(); 713 } 714 if_header loop_body 715 { 716 if($4->ntest == N) 717 yyerror("missing condition in if statement"); 718 $4->nbody = $5; 719 $$ = list1($4); 720 } 721 722 elseif_list: 723 { 724 $$ = nil; 725 } 726 | elseif_list elseif 727 { 728 $$ = concat($1, $2); 729 } 730 731 else: 732 { 733 $$ = nil; 734 } 735 | LELSE compound_stmt 736 { 737 NodeList *node; 738 739 node = mal(sizeof *node); 740 node->n = $2; 741 node->end = node; 742 $$ = node; 743 } 744 745 switch_stmt: 746 LSWITCH 747 { 748 markdcl(); 749 } 750 if_header 751 { 752 Node *n; 753 n = $3->ntest; 754 if(n != N && n->op != OTYPESW) 755 n = N; 756 typesw = nod(OXXX, typesw, n); 757 } 758 LBODY caseblock_list '}' 759 { 760 $$ = $3; 761 $$->op = OSWITCH; 762 $$->list = $6; 763 typesw = typesw->left; 764 popdcl(); 765 } 766 767 select_stmt: 768 LSELECT 769 { 770 typesw = nod(OXXX, typesw, N); 771 } 772 LBODY caseblock_list '}' 773 { 774 $$ = nod(OSELECT, N, N); 775 $$->lineno = typesw->lineno; 776 $$->list = $4; 777 typesw = typesw->left; 778 } 779 780 /* 781 * expressions 782 */ 783 expr: 784 uexpr 785 | expr LOROR expr 786 { 787 $$ = nod(OOROR, $1, $3); 788 } 789 | expr LANDAND expr 790 { 791 $$ = nod(OANDAND, $1, $3); 792 } 793 | expr LEQ expr 794 { 795 $$ = nod(OEQ, $1, $3); 796 } 797 | expr LNE expr 798 { 799 $$ = nod(ONE, $1, $3); 800 } 801 | expr LLT expr 802 { 803 $$ = nod(OLT, $1, $3); 804 } 805 | expr LLE expr 806 { 807 $$ = nod(OLE, $1, $3); 808 } 809 | expr LGE expr 810 { 811 $$ = nod(OGE, $1, $3); 812 } 813 | expr LGT expr 814 { 815 $$ = nod(OGT, $1, $3); 816 } 817 | expr '+' expr 818 { 819 $$ = nod(OADD, $1, $3); 820 } 821 | expr '-' expr 822 { 823 $$ = nod(OSUB, $1, $3); 824 } 825 | expr '|' expr 826 { 827 $$ = nod(OOR, $1, $3); 828 } 829 | expr '^' expr 830 { 831 $$ = nod(OXOR, $1, $3); 832 } 833 | expr '*' expr 834 { 835 $$ = nod(OMUL, $1, $3); 836 } 837 | expr '/' expr 838 { 839 $$ = nod(ODIV, $1, $3); 840 } 841 | expr '%' expr 842 { 843 $$ = nod(OMOD, $1, $3); 844 } 845 | expr '&' expr 846 { 847 $$ = nod(OAND, $1, $3); 848 } 849 | expr LANDNOT expr 850 { 851 $$ = nod(OANDNOT, $1, $3); 852 } 853 | expr LLSH expr 854 { 855 $$ = nod(OLSH, $1, $3); 856 } 857 | expr LRSH expr 858 { 859 $$ = nod(ORSH, $1, $3); 860 } 861 /* not an expression anymore, but left in so we can give a good error */ 862 | expr LCOMM expr 863 { 864 $$ = nod(OSEND, $1, $3); 865 } 866 867 uexpr: 868 pexpr 869 | '*' uexpr 870 { 871 $$ = nod(OIND, $2, N); 872 } 873 | '&' uexpr 874 { 875 if($2->op == OCOMPLIT) { 876 // Special case for &T{...}: turn into (*T){...}. 877 $$ = $2; 878 $$->right = nod(OIND, $$->right, N); 879 $$->right->implicit = 1; 880 } else { 881 $$ = nod(OADDR, $2, N); 882 } 883 } 884 | '+' uexpr 885 { 886 $$ = nod(OPLUS, $2, N); 887 } 888 | '-' uexpr 889 { 890 $$ = nod(OMINUS, $2, N); 891 } 892 | '!' uexpr 893 { 894 $$ = nod(ONOT, $2, N); 895 } 896 | '~' uexpr 897 { 898 yyerror("the bitwise complement operator is ^"); 899 $$ = nod(OCOM, $2, N); 900 } 901 | '^' uexpr 902 { 903 $$ = nod(OCOM, $2, N); 904 } 905 | LCOMM uexpr 906 { 907 $$ = nod(ORECV, $2, N); 908 } 909 910 /* 911 * call-like statements that 912 * can be preceded by 'defer' and 'go' 913 */ 914 pseudocall: 915 pexpr '(' ')' 916 { 917 $$ = nod(OCALL, $1, N); 918 } 919 | pexpr '(' expr_or_type_list ocomma ')' 920 { 921 $$ = nod(OCALL, $1, N); 922 $$->list = $3; 923 } 924 | pexpr '(' expr_or_type_list LDDD ocomma ')' 925 { 926 $$ = nod(OCALL, $1, N); 927 $$->list = $3; 928 $$->isddd = 1; 929 } 930 931 pexpr_no_paren: 932 LLITERAL 933 { 934 $$ = nodlit($1); 935 } 936 | name 937 | pexpr '.' sym 938 { 939 if($1->op == OPACK) { 940 Sym *s; 941 s = restrictlookup($3->name, $1->pkg); 942 $1->used = 1; 943 $$ = oldname(s); 944 break; 945 } 946 $$ = nod(OXDOT, $1, newname($3)); 947 } 948 | pexpr '.' '(' expr_or_type ')' 949 { 950 $$ = nod(ODOTTYPE, $1, $4); 951 } 952 | pexpr '.' '(' LTYPE ')' 953 { 954 $$ = nod(OTYPESW, N, $1); 955 } 956 | pexpr '[' expr ']' 957 { 958 $$ = nod(OINDEX, $1, $3); 959 } 960 | pexpr '[' oexpr ':' oexpr ']' 961 { 962 $$ = nod(OSLICE, $1, nod(OKEY, $3, $5)); 963 } 964 | pexpr '[' oexpr ':' oexpr ':' oexpr ']' 965 { 966 if($5 == N) 967 yyerror("middle index required in 3-index slice"); 968 if($7 == N) 969 yyerror("final index required in 3-index slice"); 970 $$ = nod(OSLICE3, $1, nod(OKEY, $3, nod(OKEY, $5, $7))); 971 } 972 | pseudocall 973 | convtype '(' expr ocomma ')' 974 { 975 // conversion 976 $$ = nod(OCALL, $1, N); 977 $$->list = list1($3); 978 } 979 | comptype lbrace start_complit braced_keyval_list '}' 980 { 981 $$ = $3; 982 $$->right = $1; 983 $$->list = $4; 984 fixlbrace($2); 985 } 986 | pexpr_no_paren '{' start_complit braced_keyval_list '}' 987 { 988 $$ = $3; 989 $$->right = $1; 990 $$->list = $4; 991 } 992 | '(' expr_or_type ')' '{' start_complit braced_keyval_list '}' 993 { 994 yyerror("cannot parenthesize type in composite literal"); 995 $$ = $5; 996 $$->right = $2; 997 $$->list = $6; 998 } 999 | fnliteral 1000 1001 start_complit: 1002 { 1003 // composite expression. 1004 // make node early so we get the right line number. 1005 $$ = nod(OCOMPLIT, N, N); 1006 } 1007 1008 keyval: 1009 expr ':' complitexpr 1010 { 1011 $$ = nod(OKEY, $1, $3); 1012 } 1013 1014 bare_complitexpr: 1015 expr 1016 { 1017 // These nodes do not carry line numbers. 1018 // Since a composite literal commonly spans several lines, 1019 // the line number on errors may be misleading. 1020 // Introduce a wrapper node to give the correct line. 1021 $$ = $1; 1022 switch($$->op) { 1023 case ONAME: 1024 case ONONAME: 1025 case OTYPE: 1026 case OPACK: 1027 case OLITERAL: 1028 $$ = nod(OPAREN, $$, N); 1029 $$->implicit = 1; 1030 } 1031 } 1032 | '{' start_complit braced_keyval_list '}' 1033 { 1034 $$ = $2; 1035 $$->list = $3; 1036 } 1037 1038 complitexpr: 1039 expr 1040 | '{' start_complit braced_keyval_list '}' 1041 { 1042 $$ = $2; 1043 $$->list = $3; 1044 } 1045 1046 pexpr: 1047 pexpr_no_paren 1048 | '(' expr_or_type ')' 1049 { 1050 $$ = $2; 1051 1052 // Need to know on lhs of := whether there are ( ). 1053 // Don't bother with the OPAREN in other cases: 1054 // it's just a waste of memory and time. 1055 switch($$->op) { 1056 case ONAME: 1057 case ONONAME: 1058 case OPACK: 1059 case OTYPE: 1060 case OLITERAL: 1061 case OTYPESW: 1062 $$ = nod(OPAREN, $$, N); 1063 } 1064 } 1065 1066 expr_or_type: 1067 expr 1068 | non_expr_type %prec PreferToRightParen 1069 1070 name_or_type: 1071 ntype 1072 1073 lbrace: 1074 LBODY 1075 { 1076 $$ = LBODY; 1077 } 1078 | '{' 1079 { 1080 $$ = '{'; 1081 } 1082 1083 /* 1084 * names and types 1085 * newname is used before declared 1086 * oldname is used after declared 1087 */ 1088 new_name: 1089 sym 1090 { 1091 if($1 == S) 1092 $$ = N; 1093 else 1094 $$ = newname($1); 1095 } 1096 1097 dcl_name: 1098 sym 1099 { 1100 $$ = dclname($1); 1101 } 1102 1103 onew_name: 1104 { 1105 $$ = N; 1106 } 1107 | new_name 1108 1109 sym: 1110 LNAME 1111 { 1112 $$ = $1; 1113 // during imports, unqualified non-exported identifiers are from builtinpkg 1114 if(importpkg != nil && !exportname($1->name)) 1115 $$ = pkglookup($1->name, builtinpkg); 1116 } 1117 | hidden_importsym 1118 | '?' 1119 { 1120 $$ = S; 1121 } 1122 1123 hidden_importsym: 1124 '@' LLITERAL '.' LNAME 1125 { 1126 Pkg *p; 1127 1128 if($2.u.sval->len == 0) 1129 p = importpkg; 1130 else { 1131 if(isbadimport($2.u.sval)) 1132 errorexit(); 1133 p = mkpkg($2.u.sval); 1134 } 1135 $$ = pkglookup($4->name, p); 1136 } 1137 | '@' LLITERAL '.' '?' 1138 { 1139 Pkg *p; 1140 1141 if($2.u.sval->len == 0) 1142 p = importpkg; 1143 else { 1144 if(isbadimport($2.u.sval)) 1145 errorexit(); 1146 p = mkpkg($2.u.sval); 1147 } 1148 $$ = pkglookup("?", p); 1149 } 1150 1151 name: 1152 sym %prec NotParen 1153 { 1154 $$ = oldname($1); 1155 if($$->pack != N) 1156 $$->pack->used = 1; 1157 } 1158 1159 labelname: 1160 new_name 1161 1162 /* 1163 * to avoid parsing conflicts, type is split into 1164 * channel types 1165 * function types 1166 * parenthesized types 1167 * any other type 1168 * the type system makes additional restrictions, 1169 * but those are not implemented in the grammar. 1170 */ 1171 dotdotdot: 1172 LDDD 1173 { 1174 yyerror("final argument in variadic function missing type"); 1175 $$ = nod(ODDD, typenod(typ(TINTER)), N); 1176 } 1177 | LDDD ntype 1178 { 1179 $$ = nod(ODDD, $2, N); 1180 } 1181 1182 ntype: 1183 recvchantype 1184 | fntype 1185 | othertype 1186 | ptrtype 1187 | dotname 1188 | '(' ntype ')' 1189 { 1190 $$ = $2; 1191 } 1192 1193 non_expr_type: 1194 recvchantype 1195 | fntype 1196 | othertype 1197 | '*' non_expr_type 1198 { 1199 $$ = nod(OIND, $2, N); 1200 } 1201 1202 non_recvchantype: 1203 fntype 1204 | othertype 1205 | ptrtype 1206 | dotname 1207 | '(' ntype ')' 1208 { 1209 $$ = $2; 1210 } 1211 1212 convtype: 1213 fntype 1214 | othertype 1215 1216 comptype: 1217 othertype 1218 1219 fnret_type: 1220 recvchantype 1221 | fntype 1222 | othertype 1223 | ptrtype 1224 | dotname 1225 1226 dotname: 1227 name 1228 | name '.' sym 1229 { 1230 if($1->op == OPACK) { 1231 Sym *s; 1232 s = restrictlookup($3->name, $1->pkg); 1233 $1->used = 1; 1234 $$ = oldname(s); 1235 break; 1236 } 1237 $$ = nod(OXDOT, $1, newname($3)); 1238 } 1239 1240 othertype: 1241 '[' oexpr ']' ntype 1242 { 1243 $$ = nod(OTARRAY, $2, $4); 1244 } 1245 | '[' LDDD ']' ntype 1246 { 1247 // array literal of nelem 1248 $$ = nod(OTARRAY, nod(ODDD, N, N), $4); 1249 } 1250 | LCHAN non_recvchantype 1251 { 1252 $$ = nod(OTCHAN, $2, N); 1253 $$->etype = Cboth; 1254 } 1255 | LCHAN LCOMM ntype 1256 { 1257 $$ = nod(OTCHAN, $3, N); 1258 $$->etype = Csend; 1259 } 1260 | LMAP '[' ntype ']' ntype 1261 { 1262 $$ = nod(OTMAP, $3, $5); 1263 } 1264 | structtype 1265 | interfacetype 1266 1267 ptrtype: 1268 '*' ntype 1269 { 1270 $$ = nod(OIND, $2, N); 1271 } 1272 1273 recvchantype: 1274 LCOMM LCHAN ntype 1275 { 1276 $$ = nod(OTCHAN, $3, N); 1277 $$->etype = Crecv; 1278 } 1279 1280 structtype: 1281 LSTRUCT lbrace structdcl_list osemi '}' 1282 { 1283 $$ = nod(OTSTRUCT, N, N); 1284 $$->list = $3; 1285 fixlbrace($2); 1286 } 1287 | LSTRUCT lbrace '}' 1288 { 1289 $$ = nod(OTSTRUCT, N, N); 1290 fixlbrace($2); 1291 } 1292 1293 interfacetype: 1294 LINTERFACE lbrace interfacedcl_list osemi '}' 1295 { 1296 $$ = nod(OTINTER, N, N); 1297 $$->list = $3; 1298 fixlbrace($2); 1299 } 1300 | LINTERFACE lbrace '}' 1301 { 1302 $$ = nod(OTINTER, N, N); 1303 fixlbrace($2); 1304 } 1305 1306 /* 1307 * function stuff 1308 * all in one place to show how crappy it all is 1309 */ 1310 xfndcl: 1311 LFUNC fndcl fnbody 1312 { 1313 $$ = $2; 1314 if($$ == N) 1315 break; 1316 if(noescape && $3 != nil) 1317 yyerror("can only use //go:noescape with external func implementations"); 1318 $$->nbody = $3; 1319 $$->endlineno = lineno; 1320 $$->noescape = noescape; 1321 $$->nosplit = nosplit; 1322 $$->nowritebarrier = nowritebarrier; 1323 funcbody($$); 1324 } 1325 1326 fndcl: 1327 sym '(' oarg_type_list_ocomma ')' fnres 1328 { 1329 Node *t; 1330 1331 $$ = N; 1332 $3 = checkarglist($3, 1); 1333 1334 if(strcmp($1->name, "init") == 0) { 1335 $1 = renameinit(); 1336 if($3 != nil || $5 != nil) 1337 yyerror("func init must have no arguments and no return values"); 1338 } 1339 if(strcmp(localpkg->name, "main") == 0 && strcmp($1->name, "main") == 0) { 1340 if($3 != nil || $5 != nil) 1341 yyerror("func main must have no arguments and no return values"); 1342 } 1343 1344 t = nod(OTFUNC, N, N); 1345 t->list = $3; 1346 t->rlist = $5; 1347 1348 $$ = nod(ODCLFUNC, N, N); 1349 $$->nname = newname($1); 1350 $$->nname->defn = $$; 1351 $$->nname->ntype = t; // TODO: check if nname already has an ntype 1352 declare($$->nname, PFUNC); 1353 1354 funchdr($$); 1355 } 1356 | '(' oarg_type_list_ocomma ')' sym '(' oarg_type_list_ocomma ')' fnres 1357 { 1358 Node *rcvr, *t; 1359 1360 $$ = N; 1361 $2 = checkarglist($2, 0); 1362 $6 = checkarglist($6, 1); 1363 1364 if($2 == nil) { 1365 yyerror("method has no receiver"); 1366 break; 1367 } 1368 if($2->next != nil) { 1369 yyerror("method has multiple receivers"); 1370 break; 1371 } 1372 rcvr = $2->n; 1373 if(rcvr->op != ODCLFIELD) { 1374 yyerror("bad receiver in method"); 1375 break; 1376 } 1377 1378 t = nod(OTFUNC, rcvr, N); 1379 t->list = $6; 1380 t->rlist = $8; 1381 1382 $$ = nod(ODCLFUNC, N, N); 1383 $$->shortname = newname($4); 1384 $$->nname = methodname1($$->shortname, rcvr->right); 1385 $$->nname->defn = $$; 1386 $$->nname->ntype = t; 1387 $$->nname->nointerface = nointerface; 1388 declare($$->nname, PFUNC); 1389 1390 funchdr($$); 1391 } 1392 1393 hidden_fndcl: 1394 hidden_pkg_importsym '(' ohidden_funarg_list ')' ohidden_funres 1395 { 1396 Sym *s; 1397 Type *t; 1398 1399 $$ = N; 1400 1401 s = $1; 1402 t = functype(N, $3, $5); 1403 1404 importsym(s, ONAME); 1405 if(s->def != N && s->def->op == ONAME) { 1406 if(eqtype(t, s->def->type)) { 1407 dclcontext = PDISCARD; // since we skip funchdr below 1408 break; 1409 } 1410 yyerror("inconsistent definition for func %S during import\n\t%T\n\t%T", s, s->def->type, t); 1411 } 1412 1413 $$ = newname(s); 1414 $$->type = t; 1415 declare($$, PFUNC); 1416 1417 funchdr($$); 1418 } 1419 | '(' hidden_funarg_list ')' sym '(' ohidden_funarg_list ')' ohidden_funres 1420 { 1421 $$ = methodname1(newname($4), $2->n->right); 1422 $$->type = functype($2->n, $6, $8); 1423 1424 checkwidth($$->type); 1425 addmethod($4, $$->type, 0, nointerface); 1426 nointerface = 0; 1427 funchdr($$); 1428 1429 // inl.c's inlnode in on a dotmeth node expects to find the inlineable body as 1430 // (dotmeth's type)->nname->inl, and dotmeth's type has been pulled 1431 // out by typecheck's lookdot as this $$->ttype. So by providing 1432 // this back link here we avoid special casing there. 1433 $$->type->nname = $$; 1434 } 1435 1436 fntype: 1437 LFUNC '(' oarg_type_list_ocomma ')' fnres 1438 { 1439 $3 = checkarglist($3, 1); 1440 $$ = nod(OTFUNC, N, N); 1441 $$->list = $3; 1442 $$->rlist = $5; 1443 } 1444 1445 fnbody: 1446 { 1447 $$ = nil; 1448 } 1449 | '{' stmt_list '}' 1450 { 1451 $$ = $2; 1452 if($$ == nil) 1453 $$ = list1(nod(OEMPTY, N, N)); 1454 } 1455 1456 fnres: 1457 %prec NotParen 1458 { 1459 $$ = nil; 1460 } 1461 | fnret_type 1462 { 1463 $$ = list1(nod(ODCLFIELD, N, $1)); 1464 } 1465 | '(' oarg_type_list_ocomma ')' 1466 { 1467 $2 = checkarglist($2, 0); 1468 $$ = $2; 1469 } 1470 1471 fnlitdcl: 1472 fntype 1473 { 1474 closurehdr($1); 1475 } 1476 1477 fnliteral: 1478 fnlitdcl lbrace stmt_list '}' 1479 { 1480 $$ = closurebody($3); 1481 fixlbrace($2); 1482 } 1483 | fnlitdcl error 1484 { 1485 $$ = closurebody(nil); 1486 } 1487 1488 /* 1489 * lists of things 1490 * note that they are left recursive 1491 * to conserve yacc stack. they need to 1492 * be reversed to interpret correctly 1493 */ 1494 xdcl_list: 1495 { 1496 $$ = nil; 1497 } 1498 | xdcl_list xdcl ';' 1499 { 1500 $$ = concat($1, $2); 1501 if(nsyntaxerrors == 0) 1502 testdclstack(); 1503 nointerface = 0; 1504 noescape = 0; 1505 nosplit = 0; 1506 nowritebarrier = 0; 1507 } 1508 1509 vardcl_list: 1510 vardcl 1511 | vardcl_list ';' vardcl 1512 { 1513 $$ = concat($1, $3); 1514 } 1515 1516 constdcl_list: 1517 constdcl1 1518 | constdcl_list ';' constdcl1 1519 { 1520 $$ = concat($1, $3); 1521 } 1522 1523 typedcl_list: 1524 typedcl 1525 { 1526 $$ = list1($1); 1527 } 1528 | typedcl_list ';' typedcl 1529 { 1530 $$ = list($1, $3); 1531 } 1532 1533 structdcl_list: 1534 structdcl 1535 | structdcl_list ';' structdcl 1536 { 1537 $$ = concat($1, $3); 1538 } 1539 1540 interfacedcl_list: 1541 interfacedcl 1542 { 1543 $$ = list1($1); 1544 } 1545 | interfacedcl_list ';' interfacedcl 1546 { 1547 $$ = list($1, $3); 1548 } 1549 1550 structdcl: 1551 new_name_list ntype oliteral 1552 { 1553 NodeList *l; 1554 1555 Node *n; 1556 l = $1; 1557 if(l == nil) { 1558 // ? symbol, during import (list1(N) == nil) 1559 n = $2; 1560 if(n->op == OIND) 1561 n = n->left; 1562 n = embedded(n->sym, importpkg); 1563 n->right = $2; 1564 n->val = $3; 1565 $$ = list1(n); 1566 break; 1567 } 1568 1569 for(l=$1; l; l=l->next) { 1570 l->n = nod(ODCLFIELD, l->n, $2); 1571 l->n->val = $3; 1572 } 1573 } 1574 | embed oliteral 1575 { 1576 $1->val = $2; 1577 $$ = list1($1); 1578 } 1579 | '(' embed ')' oliteral 1580 { 1581 $2->val = $4; 1582 $$ = list1($2); 1583 yyerror("cannot parenthesize embedded type"); 1584 } 1585 | '*' embed oliteral 1586 { 1587 $2->right = nod(OIND, $2->right, N); 1588 $2->val = $3; 1589 $$ = list1($2); 1590 } 1591 | '(' '*' embed ')' oliteral 1592 { 1593 $3->right = nod(OIND, $3->right, N); 1594 $3->val = $5; 1595 $$ = list1($3); 1596 yyerror("cannot parenthesize embedded type"); 1597 } 1598 | '*' '(' embed ')' oliteral 1599 { 1600 $3->right = nod(OIND, $3->right, N); 1601 $3->val = $5; 1602 $$ = list1($3); 1603 yyerror("cannot parenthesize embedded type"); 1604 } 1605 1606 packname: 1607 LNAME 1608 { 1609 Node *n; 1610 1611 $$ = $1; 1612 n = oldname($1); 1613 if(n->pack != N) 1614 n->pack->used = 1; 1615 } 1616 | LNAME '.' sym 1617 { 1618 Pkg *pkg; 1619 1620 if($1->def == N || $1->def->op != OPACK) { 1621 yyerror("%S is not a package", $1); 1622 pkg = localpkg; 1623 } else { 1624 $1->def->used = 1; 1625 pkg = $1->def->pkg; 1626 } 1627 $$ = restrictlookup($3->name, pkg); 1628 } 1629 1630 embed: 1631 packname 1632 { 1633 $$ = embedded($1, localpkg); 1634 } 1635 1636 interfacedcl: 1637 new_name indcl 1638 { 1639 $$ = nod(ODCLFIELD, $1, $2); 1640 ifacedcl($$); 1641 } 1642 | packname 1643 { 1644 $$ = nod(ODCLFIELD, N, oldname($1)); 1645 } 1646 | '(' packname ')' 1647 { 1648 $$ = nod(ODCLFIELD, N, oldname($2)); 1649 yyerror("cannot parenthesize embedded type"); 1650 } 1651 1652 indcl: 1653 '(' oarg_type_list_ocomma ')' fnres 1654 { 1655 // without func keyword 1656 $2 = checkarglist($2, 1); 1657 $$ = nod(OTFUNC, fakethis(), N); 1658 $$->list = $2; 1659 $$->rlist = $4; 1660 } 1661 1662 /* 1663 * function arguments. 1664 */ 1665 arg_type: 1666 name_or_type 1667 | sym name_or_type 1668 { 1669 $$ = nod(ONONAME, N, N); 1670 $$->sym = $1; 1671 $$ = nod(OKEY, $$, $2); 1672 } 1673 | sym dotdotdot 1674 { 1675 $$ = nod(ONONAME, N, N); 1676 $$->sym = $1; 1677 $$ = nod(OKEY, $$, $2); 1678 } 1679 | dotdotdot 1680 1681 arg_type_list: 1682 arg_type 1683 { 1684 $$ = list1($1); 1685 } 1686 | arg_type_list ',' arg_type 1687 { 1688 $$ = list($1, $3); 1689 } 1690 1691 oarg_type_list_ocomma: 1692 { 1693 $$ = nil; 1694 } 1695 | arg_type_list ocomma 1696 { 1697 $$ = $1; 1698 } 1699 1700 /* 1701 * statement 1702 */ 1703 stmt: 1704 { 1705 $$ = N; 1706 } 1707 | compound_stmt 1708 | common_dcl 1709 { 1710 $$ = liststmt($1); 1711 } 1712 | non_dcl_stmt 1713 | error 1714 { 1715 $$ = N; 1716 } 1717 1718 non_dcl_stmt: 1719 simple_stmt 1720 | for_stmt 1721 | switch_stmt 1722 | select_stmt 1723 | if_stmt 1724 | labelname ':' 1725 { 1726 $1 = nod(OLABEL, $1, N); 1727 $1->sym = dclstack; // context, for goto restrictions 1728 } 1729 stmt 1730 { 1731 NodeList *l; 1732 1733 $1->defn = $4; 1734 l = list1($1); 1735 if($4) 1736 l = list(l, $4); 1737 $$ = liststmt(l); 1738 } 1739 | LFALL 1740 { 1741 // will be converted to OFALL 1742 $$ = nod(OXFALL, N, N); 1743 $$->xoffset = block; 1744 } 1745 | LBREAK onew_name 1746 { 1747 $$ = nod(OBREAK, $2, N); 1748 } 1749 | LCONTINUE onew_name 1750 { 1751 $$ = nod(OCONTINUE, $2, N); 1752 } 1753 | LGO pseudocall 1754 { 1755 $$ = nod(OPROC, $2, N); 1756 } 1757 | LDEFER pseudocall 1758 { 1759 $$ = nod(ODEFER, $2, N); 1760 } 1761 | LGOTO new_name 1762 { 1763 $$ = nod(OGOTO, $2, N); 1764 $$->sym = dclstack; // context, for goto restrictions 1765 } 1766 | LRETURN oexpr_list 1767 { 1768 $$ = nod(ORETURN, N, N); 1769 $$->list = $2; 1770 if($$->list == nil && curfn != N) { 1771 NodeList *l; 1772 1773 for(l=curfn->dcl; l; l=l->next) { 1774 if(l->n->class == PPARAM) 1775 continue; 1776 if(l->n->class != PPARAMOUT) 1777 break; 1778 if(l->n->sym->def != l->n) 1779 yyerror("%s is shadowed during return", l->n->sym->name); 1780 } 1781 } 1782 } 1783 1784 stmt_list: 1785 stmt 1786 { 1787 $$ = nil; 1788 if($1 != N) 1789 $$ = list1($1); 1790 } 1791 | stmt_list ';' stmt 1792 { 1793 $$ = $1; 1794 if($3 != N) 1795 $$ = list($$, $3); 1796 } 1797 1798 new_name_list: 1799 new_name 1800 { 1801 $$ = list1($1); 1802 } 1803 | new_name_list ',' new_name 1804 { 1805 $$ = list($1, $3); 1806 } 1807 1808 dcl_name_list: 1809 dcl_name 1810 { 1811 $$ = list1($1); 1812 } 1813 | dcl_name_list ',' dcl_name 1814 { 1815 $$ = list($1, $3); 1816 } 1817 1818 expr_list: 1819 expr 1820 { 1821 $$ = list1($1); 1822 } 1823 | expr_list ',' expr 1824 { 1825 $$ = list($1, $3); 1826 } 1827 1828 expr_or_type_list: 1829 expr_or_type 1830 { 1831 $$ = list1($1); 1832 } 1833 | expr_or_type_list ',' expr_or_type 1834 { 1835 $$ = list($1, $3); 1836 } 1837 1838 /* 1839 * list of combo of keyval and val 1840 */ 1841 keyval_list: 1842 keyval 1843 { 1844 $$ = list1($1); 1845 } 1846 | bare_complitexpr 1847 { 1848 $$ = list1($1); 1849 } 1850 | keyval_list ',' keyval 1851 { 1852 $$ = list($1, $3); 1853 } 1854 | keyval_list ',' bare_complitexpr 1855 { 1856 $$ = list($1, $3); 1857 } 1858 1859 braced_keyval_list: 1860 { 1861 $$ = nil; 1862 } 1863 | keyval_list ocomma 1864 { 1865 $$ = $1; 1866 } 1867 1868 /* 1869 * optional things 1870 */ 1871 osemi: 1872 | ';' 1873 1874 ocomma: 1875 | ',' 1876 1877 oexpr: 1878 { 1879 $$ = N; 1880 } 1881 | expr 1882 1883 oexpr_list: 1884 { 1885 $$ = nil; 1886 } 1887 | expr_list 1888 1889 osimple_stmt: 1890 { 1891 $$ = N; 1892 } 1893 | simple_stmt 1894 1895 ohidden_funarg_list: 1896 { 1897 $$ = nil; 1898 } 1899 | hidden_funarg_list 1900 1901 ohidden_structdcl_list: 1902 { 1903 $$ = nil; 1904 } 1905 | hidden_structdcl_list 1906 1907 ohidden_interfacedcl_list: 1908 { 1909 $$ = nil; 1910 } 1911 | hidden_interfacedcl_list 1912 1913 oliteral: 1914 { 1915 $$.ctype = CTxxx; 1916 } 1917 | LLITERAL 1918 1919 /* 1920 * import syntax from package header 1921 */ 1922 hidden_import: 1923 LIMPORT LNAME LLITERAL ';' 1924 { 1925 importimport($2, $3.u.sval); 1926 } 1927 | LVAR hidden_pkg_importsym hidden_type ';' 1928 { 1929 importvar($2, $3); 1930 } 1931 | LCONST hidden_pkg_importsym '=' hidden_constant ';' 1932 { 1933 importconst($2, types[TIDEAL], $4); 1934 } 1935 | LCONST hidden_pkg_importsym hidden_type '=' hidden_constant ';' 1936 { 1937 importconst($2, $3, $5); 1938 } 1939 | LTYPE hidden_pkgtype hidden_type ';' 1940 { 1941 importtype($2, $3); 1942 } 1943 | LFUNC hidden_fndcl fnbody ';' 1944 { 1945 if($2 == N) { 1946 dclcontext = PEXTERN; // since we skip the funcbody below 1947 break; 1948 } 1949 1950 $2->inl = $3; 1951 1952 funcbody($2); 1953 importlist = list(importlist, $2); 1954 1955 if(debug['E']) { 1956 print("import [%Z] func %lN \n", importpkg->path, $2); 1957 if(debug['m'] > 2 && $2->inl) 1958 print("inl body:%+H\n", $2->inl); 1959 } 1960 } 1961 1962 hidden_pkg_importsym: 1963 hidden_importsym 1964 { 1965 $$ = $1; 1966 structpkg = $$->pkg; 1967 } 1968 1969 hidden_pkgtype: 1970 hidden_pkg_importsym 1971 { 1972 $$ = pkgtype($1); 1973 importsym($1, OTYPE); 1974 } 1975 1976 /* 1977 * importing types 1978 */ 1979 1980 hidden_type: 1981 hidden_type_misc 1982 | hidden_type_recv_chan 1983 | hidden_type_func 1984 1985 hidden_type_non_recv_chan: 1986 hidden_type_misc 1987 | hidden_type_func 1988 1989 hidden_type_misc: 1990 hidden_importsym 1991 { 1992 $$ = pkgtype($1); 1993 } 1994 | LNAME 1995 { 1996 // predefined name like uint8 1997 $1 = pkglookup($1->name, builtinpkg); 1998 if($1->def == N || $1->def->op != OTYPE) { 1999 yyerror("%s is not a type", $1->name); 2000 $$ = T; 2001 } else 2002 $$ = $1->def->type; 2003 } 2004 | '[' ']' hidden_type 2005 { 2006 $$ = aindex(N, $3); 2007 } 2008 | '[' LLITERAL ']' hidden_type 2009 { 2010 $$ = aindex(nodlit($2), $4); 2011 } 2012 | LMAP '[' hidden_type ']' hidden_type 2013 { 2014 $$ = maptype($3, $5); 2015 } 2016 | LSTRUCT '{' ohidden_structdcl_list '}' 2017 { 2018 $$ = tostruct($3); 2019 } 2020 | LINTERFACE '{' ohidden_interfacedcl_list '}' 2021 { 2022 $$ = tointerface($3); 2023 } 2024 | '*' hidden_type 2025 { 2026 $$ = ptrto($2); 2027 } 2028 | LCHAN hidden_type_non_recv_chan 2029 { 2030 $$ = typ(TCHAN); 2031 $$->type = $2; 2032 $$->chan = Cboth; 2033 } 2034 | LCHAN '(' hidden_type_recv_chan ')' 2035 { 2036 $$ = typ(TCHAN); 2037 $$->type = $3; 2038 $$->chan = Cboth; 2039 } 2040 | LCHAN LCOMM hidden_type 2041 { 2042 $$ = typ(TCHAN); 2043 $$->type = $3; 2044 $$->chan = Csend; 2045 } 2046 2047 hidden_type_recv_chan: 2048 LCOMM LCHAN hidden_type 2049 { 2050 $$ = typ(TCHAN); 2051 $$->type = $3; 2052 $$->chan = Crecv; 2053 } 2054 2055 hidden_type_func: 2056 LFUNC '(' ohidden_funarg_list ')' ohidden_funres 2057 { 2058 $$ = functype(nil, $3, $5); 2059 } 2060 2061 hidden_funarg: 2062 sym hidden_type oliteral 2063 { 2064 $$ = nod(ODCLFIELD, N, typenod($2)); 2065 if($1) 2066 $$->left = newname($1); 2067 $$->val = $3; 2068 } 2069 | sym LDDD hidden_type oliteral 2070 { 2071 Type *t; 2072 2073 t = typ(TARRAY); 2074 t->bound = -1; 2075 t->type = $3; 2076 2077 $$ = nod(ODCLFIELD, N, typenod(t)); 2078 if($1) 2079 $$->left = newname($1); 2080 $$->isddd = 1; 2081 $$->val = $4; 2082 } 2083 2084 hidden_structdcl: 2085 sym hidden_type oliteral 2086 { 2087 Sym *s; 2088 Pkg *p; 2089 2090 if($1 != S && strcmp($1->name, "?") != 0) { 2091 $$ = nod(ODCLFIELD, newname($1), typenod($2)); 2092 $$->val = $3; 2093 } else { 2094 s = $2->sym; 2095 if(s == S && isptr[$2->etype]) 2096 s = $2->type->sym; 2097 p = importpkg; 2098 if($1 != S) 2099 p = $1->pkg; 2100 $$ = embedded(s, p); 2101 $$->right = typenod($2); 2102 $$->val = $3; 2103 } 2104 } 2105 2106 hidden_interfacedcl: 2107 sym '(' ohidden_funarg_list ')' ohidden_funres 2108 { 2109 $$ = nod(ODCLFIELD, newname($1), typenod(functype(fakethis(), $3, $5))); 2110 } 2111 | hidden_type 2112 { 2113 $$ = nod(ODCLFIELD, N, typenod($1)); 2114 } 2115 2116 ohidden_funres: 2117 { 2118 $$ = nil; 2119 } 2120 | hidden_funres 2121 2122 hidden_funres: 2123 '(' ohidden_funarg_list ')' 2124 { 2125 $$ = $2; 2126 } 2127 | hidden_type 2128 { 2129 $$ = list1(nod(ODCLFIELD, N, typenod($1))); 2130 } 2131 2132 /* 2133 * importing constants 2134 */ 2135 2136 hidden_literal: 2137 LLITERAL 2138 { 2139 $$ = nodlit($1); 2140 } 2141 | '-' LLITERAL 2142 { 2143 $$ = nodlit($2); 2144 switch($$->val.ctype){ 2145 case CTINT: 2146 case CTRUNE: 2147 mpnegfix($$->val.u.xval); 2148 break; 2149 case CTFLT: 2150 mpnegflt($$->val.u.fval); 2151 break; 2152 case CTCPLX: 2153 mpnegflt(&$$->val.u.cval->real); 2154 mpnegflt(&$$->val.u.cval->imag); 2155 break; 2156 default: 2157 yyerror("bad negated constant"); 2158 } 2159 } 2160 | sym 2161 { 2162 $$ = oldname(pkglookup($1->name, builtinpkg)); 2163 if($$->op != OLITERAL) 2164 yyerror("bad constant %S", $$->sym); 2165 } 2166 2167 hidden_constant: 2168 hidden_literal 2169 | '(' hidden_literal '+' hidden_literal ')' 2170 { 2171 if($2->val.ctype == CTRUNE && $4->val.ctype == CTINT) { 2172 $$ = $2; 2173 mpaddfixfix($2->val.u.xval, $4->val.u.xval, 0); 2174 break; 2175 } 2176 $4->val.u.cval->real = $4->val.u.cval->imag; 2177 mpmovecflt(&$4->val.u.cval->imag, 0.0); 2178 $$ = nodcplxlit($2->val, $4->val); 2179 } 2180 2181 hidden_import_list: 2182 | hidden_import_list hidden_import 2183 2184 hidden_funarg_list: 2185 hidden_funarg 2186 { 2187 $$ = list1($1); 2188 } 2189 | hidden_funarg_list ',' hidden_funarg 2190 { 2191 $$ = list($1, $3); 2192 } 2193 2194 hidden_structdcl_list: 2195 hidden_structdcl 2196 { 2197 $$ = list1($1); 2198 } 2199 | hidden_structdcl_list ';' hidden_structdcl 2200 { 2201 $$ = list($1, $3); 2202 } 2203 2204 hidden_interfacedcl_list: 2205 hidden_interfacedcl 2206 { 2207 $$ = list1($1); 2208 } 2209 | hidden_interfacedcl_list ';' hidden_interfacedcl 2210 { 2211 $$ = list($1, $3); 2212 } 2213 2214 %% 2215 2216 static void 2217 fixlbrace(int lbr) 2218 { 2219 // If the opening brace was an LBODY, 2220 // set up for another one now that we're done. 2221 // See comment in lex.c about loophack. 2222 if(lbr == LBODY) 2223 loophack = 1; 2224 } 2225