github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/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 $$->etype = OADD; 464 } 465 | expr LDEC 466 { 467 $$ = nod(OASOP, $1, nodintconst(1)); 468 $$->etype = OSUB; 469 } 470 471 case: 472 LCASE expr_or_type_list ':' 473 { 474 Node *n, *nn; 475 476 // will be converted to OCASE 477 // right will point to next case 478 // done in casebody() 479 markdcl(); 480 $$ = nod(OXCASE, N, N); 481 $$->list = $2; 482 if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) { 483 // type switch - declare variable 484 nn = newname(n->sym); 485 declare(nn, dclcontext); 486 $$->nname = nn; 487 488 // keep track of the instances for reporting unused 489 nn->defn = typesw->right; 490 } 491 } 492 | LCASE expr_or_type_list '=' expr ':' 493 { 494 Node *n; 495 496 // will be converted to OCASE 497 // right will point to next case 498 // done in casebody() 499 markdcl(); 500 $$ = nod(OXCASE, N, N); 501 if($2->next == nil) 502 n = nod(OAS, $2->n, $4); 503 else { 504 n = nod(OAS2, N, N); 505 n->list = $2; 506 n->rlist = list1($4); 507 } 508 $$->list = list1(n); 509 } 510 | LCASE expr_or_type_list LCOLAS expr ':' 511 { 512 // will be converted to OCASE 513 // right will point to next case 514 // done in casebody() 515 markdcl(); 516 $$ = nod(OXCASE, N, N); 517 $$->list = list1(colas($2, list1($4), $3)); 518 } 519 | LDEFAULT ':' 520 { 521 Node *n, *nn; 522 523 markdcl(); 524 $$ = nod(OXCASE, N, N); 525 if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) { 526 // type switch - declare variable 527 nn = newname(n->sym); 528 declare(nn, dclcontext); 529 $$->nname = nn; 530 531 // keep track of the instances for reporting unused 532 nn->defn = typesw->right; 533 } 534 } 535 536 compound_stmt: 537 '{' 538 { 539 markdcl(); 540 } 541 stmt_list '}' 542 { 543 if($3 == nil) 544 $$ = nod(OEMPTY, N, N); 545 else 546 $$ = liststmt($3); 547 popdcl(); 548 } 549 550 caseblock: 551 case 552 { 553 // If the last token read by the lexer was consumed 554 // as part of the case, clear it (parser has cleared yychar). 555 // If the last token read by the lexer was the lookahead 556 // leave it alone (parser has it cached in yychar). 557 // This is so that the stmt_list action doesn't look at 558 // the case tokens if the stmt_list is empty. 559 yylast = yychar; 560 $1->xoffset = block; 561 } 562 stmt_list 563 { 564 int last; 565 566 // This is the only place in the language where a statement 567 // list is not allowed to drop the final semicolon, because 568 // it's the only place where a statement list is not followed 569 // by a closing brace. Handle the error for pedantry. 570 571 // Find the final token of the statement list. 572 // yylast is lookahead; yyprev is last of stmt_list 573 last = yyprev; 574 575 if(last > 0 && last != ';' && yychar != '}') 576 yyerror("missing statement after label"); 577 $$ = $1; 578 $$->nbody = $3; 579 popdcl(); 580 } 581 582 caseblock_list: 583 { 584 $$ = nil; 585 } 586 | caseblock_list caseblock 587 { 588 $$ = list($1, $2); 589 } 590 591 loop_body: 592 LBODY 593 { 594 markdcl(); 595 } 596 stmt_list '}' 597 { 598 $$ = $3; 599 popdcl(); 600 } 601 602 range_stmt: 603 expr_list '=' LRANGE expr 604 { 605 $$ = nod(ORANGE, N, $4); 606 $$->list = $1; 607 $$->etype = 0; // := flag 608 } 609 | expr_list LCOLAS LRANGE expr 610 { 611 $$ = nod(ORANGE, N, $4); 612 $$->list = $1; 613 $$->colas = 1; 614 colasdefn($1, $$); 615 } 616 617 for_header: 618 osimple_stmt ';' osimple_stmt ';' osimple_stmt 619 { 620 // init ; test ; incr 621 if($5 != N && $5->colas != 0) 622 yyerror("cannot declare in the for-increment"); 623 $$ = nod(OFOR, N, N); 624 if($1 != N) 625 $$->ninit = list1($1); 626 $$->ntest = $3; 627 $$->nincr = $5; 628 } 629 | osimple_stmt 630 { 631 // normal test 632 $$ = nod(OFOR, N, N); 633 $$->ntest = $1; 634 } 635 | range_stmt 636 637 for_body: 638 for_header loop_body 639 { 640 $$ = $1; 641 $$->nbody = concat($$->nbody, $2); 642 } 643 644 for_stmt: 645 LFOR 646 { 647 markdcl(); 648 } 649 for_body 650 { 651 $$ = $3; 652 popdcl(); 653 } 654 655 if_header: 656 osimple_stmt 657 { 658 // test 659 $$ = nod(OIF, N, N); 660 $$->ntest = $1; 661 } 662 | osimple_stmt ';' osimple_stmt 663 { 664 // init ; test 665 $$ = nod(OIF, N, N); 666 if($1 != N) 667 $$->ninit = list1($1); 668 $$->ntest = $3; 669 } 670 671 /* IF cond body (ELSE IF cond body)* (ELSE block)? */ 672 if_stmt: 673 LIF 674 { 675 markdcl(); 676 } 677 if_header 678 { 679 if($3->ntest == N) 680 yyerror("missing condition in if statement"); 681 } 682 loop_body 683 { 684 $3->nbody = $5; 685 } 686 elseif_list else 687 { 688 Node *n; 689 NodeList *nn; 690 691 $$ = $3; 692 n = $3; 693 popdcl(); 694 for(nn = concat($7, $8); nn; nn = nn->next) { 695 if(nn->n->op == OIF) 696 popdcl(); 697 n->nelse = list1(nn->n); 698 n = nn->n; 699 } 700 } 701 702 elseif: 703 LELSE LIF 704 { 705 markdcl(); 706 } 707 if_header loop_body 708 { 709 if($4->ntest == N) 710 yyerror("missing condition in if statement"); 711 $4->nbody = $5; 712 $$ = list1($4); 713 } 714 715 elseif_list: 716 { 717 $$ = nil; 718 } 719 | elseif_list elseif 720 { 721 $$ = concat($1, $2); 722 } 723 724 else: 725 { 726 $$ = nil; 727 } 728 | LELSE compound_stmt 729 { 730 NodeList *node; 731 732 node = mal(sizeof *node); 733 node->n = $2; 734 node->end = node; 735 $$ = node; 736 } 737 738 switch_stmt: 739 LSWITCH 740 { 741 markdcl(); 742 } 743 if_header 744 { 745 Node *n; 746 n = $3->ntest; 747 if(n != N && n->op != OTYPESW) 748 n = N; 749 typesw = nod(OXXX, typesw, n); 750 } 751 LBODY caseblock_list '}' 752 { 753 $$ = $3; 754 $$->op = OSWITCH; 755 $$->list = $6; 756 typesw = typesw->left; 757 popdcl(); 758 } 759 760 select_stmt: 761 LSELECT 762 { 763 typesw = nod(OXXX, typesw, N); 764 } 765 LBODY caseblock_list '}' 766 { 767 $$ = nod(OSELECT, N, N); 768 $$->lineno = typesw->lineno; 769 $$->list = $4; 770 typesw = typesw->left; 771 } 772 773 /* 774 * expressions 775 */ 776 expr: 777 uexpr 778 | expr LOROR expr 779 { 780 $$ = nod(OOROR, $1, $3); 781 } 782 | expr LANDAND expr 783 { 784 $$ = nod(OANDAND, $1, $3); 785 } 786 | expr LEQ expr 787 { 788 $$ = nod(OEQ, $1, $3); 789 } 790 | expr LNE expr 791 { 792 $$ = nod(ONE, $1, $3); 793 } 794 | expr LLT expr 795 { 796 $$ = nod(OLT, $1, $3); 797 } 798 | expr LLE expr 799 { 800 $$ = nod(OLE, $1, $3); 801 } 802 | expr LGE expr 803 { 804 $$ = nod(OGE, $1, $3); 805 } 806 | expr LGT expr 807 { 808 $$ = nod(OGT, $1, $3); 809 } 810 | expr '+' expr 811 { 812 $$ = nod(OADD, $1, $3); 813 } 814 | expr '-' expr 815 { 816 $$ = nod(OSUB, $1, $3); 817 } 818 | expr '|' expr 819 { 820 $$ = nod(OOR, $1, $3); 821 } 822 | expr '^' expr 823 { 824 $$ = nod(OXOR, $1, $3); 825 } 826 | expr '*' expr 827 { 828 $$ = nod(OMUL, $1, $3); 829 } 830 | expr '/' expr 831 { 832 $$ = nod(ODIV, $1, $3); 833 } 834 | expr '%' expr 835 { 836 $$ = nod(OMOD, $1, $3); 837 } 838 | expr '&' expr 839 { 840 $$ = nod(OAND, $1, $3); 841 } 842 | expr LANDNOT expr 843 { 844 $$ = nod(OANDNOT, $1, $3); 845 } 846 | expr LLSH expr 847 { 848 $$ = nod(OLSH, $1, $3); 849 } 850 | expr LRSH expr 851 { 852 $$ = nod(ORSH, $1, $3); 853 } 854 /* not an expression anymore, but left in so we can give a good error */ 855 | expr LCOMM expr 856 { 857 $$ = nod(OSEND, $1, $3); 858 } 859 860 uexpr: 861 pexpr 862 | '*' uexpr 863 { 864 $$ = nod(OIND, $2, N); 865 } 866 | '&' uexpr 867 { 868 if($2->op == OCOMPLIT) { 869 // Special case for &T{...}: turn into (*T){...}. 870 $$ = $2; 871 $$->right = nod(OIND, $$->right, N); 872 $$->right->implicit = 1; 873 } else { 874 $$ = nod(OADDR, $2, N); 875 } 876 } 877 | '+' uexpr 878 { 879 $$ = nod(OPLUS, $2, N); 880 } 881 | '-' uexpr 882 { 883 $$ = nod(OMINUS, $2, N); 884 } 885 | '!' uexpr 886 { 887 $$ = nod(ONOT, $2, N); 888 } 889 | '~' uexpr 890 { 891 yyerror("the bitwise complement operator is ^"); 892 $$ = nod(OCOM, $2, N); 893 } 894 | '^' uexpr 895 { 896 $$ = nod(OCOM, $2, N); 897 } 898 | LCOMM uexpr 899 { 900 $$ = nod(ORECV, $2, N); 901 } 902 903 /* 904 * call-like statements that 905 * can be preceded by 'defer' and 'go' 906 */ 907 pseudocall: 908 pexpr '(' ')' 909 { 910 $$ = nod(OCALL, $1, N); 911 } 912 | pexpr '(' expr_or_type_list ocomma ')' 913 { 914 $$ = nod(OCALL, $1, N); 915 $$->list = $3; 916 } 917 | pexpr '(' expr_or_type_list LDDD ocomma ')' 918 { 919 $$ = nod(OCALL, $1, N); 920 $$->list = $3; 921 $$->isddd = 1; 922 } 923 924 pexpr_no_paren: 925 LLITERAL 926 { 927 $$ = nodlit($1); 928 } 929 | name 930 | pexpr '.' sym 931 { 932 if($1->op == OPACK) { 933 Sym *s; 934 s = restrictlookup($3->name, $1->pkg); 935 $1->used = 1; 936 $$ = oldname(s); 937 break; 938 } 939 $$ = nod(OXDOT, $1, newname($3)); 940 } 941 | pexpr '.' '(' expr_or_type ')' 942 { 943 $$ = nod(ODOTTYPE, $1, $4); 944 } 945 | pexpr '.' '(' LTYPE ')' 946 { 947 $$ = nod(OTYPESW, N, $1); 948 } 949 | pexpr '[' expr ']' 950 { 951 $$ = nod(OINDEX, $1, $3); 952 } 953 | pexpr '[' oexpr ':' oexpr ']' 954 { 955 $$ = nod(OSLICE, $1, nod(OKEY, $3, $5)); 956 } 957 | pexpr '[' oexpr ':' oexpr ':' oexpr ']' 958 { 959 if($5 == N) 960 yyerror("middle index required in 3-index slice"); 961 if($7 == N) 962 yyerror("final index required in 3-index slice"); 963 $$ = nod(OSLICE3, $1, nod(OKEY, $3, nod(OKEY, $5, $7))); 964 } 965 | pseudocall 966 | convtype '(' expr ocomma ')' 967 { 968 // conversion 969 $$ = nod(OCALL, $1, N); 970 $$->list = list1($3); 971 } 972 | comptype lbrace start_complit braced_keyval_list '}' 973 { 974 $$ = $3; 975 $$->right = $1; 976 $$->list = $4; 977 fixlbrace($2); 978 } 979 | pexpr_no_paren '{' start_complit braced_keyval_list '}' 980 { 981 $$ = $3; 982 $$->right = $1; 983 $$->list = $4; 984 } 985 | '(' expr_or_type ')' '{' start_complit braced_keyval_list '}' 986 { 987 yyerror("cannot parenthesize type in composite literal"); 988 $$ = $5; 989 $$->right = $2; 990 $$->list = $6; 991 } 992 | fnliteral 993 994 start_complit: 995 { 996 // composite expression. 997 // make node early so we get the right line number. 998 $$ = nod(OCOMPLIT, N, N); 999 } 1000 1001 keyval: 1002 expr ':' complitexpr 1003 { 1004 $$ = nod(OKEY, $1, $3); 1005 } 1006 1007 bare_complitexpr: 1008 expr 1009 { 1010 // These nodes do not carry line numbers. 1011 // Since a composite literal commonly spans several lines, 1012 // the line number on errors may be misleading. 1013 // Introduce a wrapper node to give the correct line. 1014 $$ = $1; 1015 switch($$->op) { 1016 case ONAME: 1017 case ONONAME: 1018 case OTYPE: 1019 case OPACK: 1020 case OLITERAL: 1021 $$ = nod(OPAREN, $$, N); 1022 $$->implicit = 1; 1023 } 1024 } 1025 | '{' start_complit braced_keyval_list '}' 1026 { 1027 $$ = $2; 1028 $$->list = $3; 1029 } 1030 1031 complitexpr: 1032 expr 1033 | '{' start_complit braced_keyval_list '}' 1034 { 1035 $$ = $2; 1036 $$->list = $3; 1037 } 1038 1039 pexpr: 1040 pexpr_no_paren 1041 | '(' expr_or_type ')' 1042 { 1043 $$ = $2; 1044 1045 // Need to know on lhs of := whether there are ( ). 1046 // Don't bother with the OPAREN in other cases: 1047 // it's just a waste of memory and time. 1048 switch($$->op) { 1049 case ONAME: 1050 case ONONAME: 1051 case OPACK: 1052 case OTYPE: 1053 case OLITERAL: 1054 case OTYPESW: 1055 $$ = nod(OPAREN, $$, N); 1056 } 1057 } 1058 1059 expr_or_type: 1060 expr 1061 | non_expr_type %prec PreferToRightParen 1062 1063 name_or_type: 1064 ntype 1065 1066 lbrace: 1067 LBODY 1068 { 1069 $$ = LBODY; 1070 } 1071 | '{' 1072 { 1073 $$ = '{'; 1074 } 1075 1076 /* 1077 * names and types 1078 * newname is used before declared 1079 * oldname is used after declared 1080 */ 1081 new_name: 1082 sym 1083 { 1084 if($1 == S) 1085 $$ = N; 1086 else 1087 $$ = newname($1); 1088 } 1089 1090 dcl_name: 1091 sym 1092 { 1093 $$ = dclname($1); 1094 } 1095 1096 onew_name: 1097 { 1098 $$ = N; 1099 } 1100 | new_name 1101 1102 sym: 1103 LNAME 1104 { 1105 $$ = $1; 1106 // during imports, unqualified non-exported identifiers are from builtinpkg 1107 if(importpkg != nil && !exportname($1->name)) 1108 $$ = pkglookup($1->name, builtinpkg); 1109 } 1110 | hidden_importsym 1111 | '?' 1112 { 1113 $$ = S; 1114 } 1115 1116 hidden_importsym: 1117 '@' LLITERAL '.' LNAME 1118 { 1119 Pkg *p; 1120 1121 if($2.u.sval->len == 0) 1122 p = importpkg; 1123 else { 1124 if(isbadimport($2.u.sval)) 1125 errorexit(); 1126 p = mkpkg($2.u.sval); 1127 } 1128 $$ = pkglookup($4->name, p); 1129 } 1130 | '@' LLITERAL '.' '?' 1131 { 1132 Pkg *p; 1133 1134 if($2.u.sval->len == 0) 1135 p = importpkg; 1136 else { 1137 if(isbadimport($2.u.sval)) 1138 errorexit(); 1139 p = mkpkg($2.u.sval); 1140 } 1141 $$ = pkglookup("?", p); 1142 } 1143 1144 name: 1145 sym %prec NotParen 1146 { 1147 $$ = oldname($1); 1148 if($$->pack != N) 1149 $$->pack->used = 1; 1150 } 1151 1152 labelname: 1153 new_name 1154 1155 /* 1156 * to avoid parsing conflicts, type is split into 1157 * channel types 1158 * function types 1159 * parenthesized types 1160 * any other type 1161 * the type system makes additional restrictions, 1162 * but those are not implemented in the grammar. 1163 */ 1164 dotdotdot: 1165 LDDD 1166 { 1167 yyerror("final argument in variadic function missing type"); 1168 $$ = nod(ODDD, typenod(typ(TINTER)), N); 1169 } 1170 | LDDD ntype 1171 { 1172 $$ = nod(ODDD, $2, N); 1173 } 1174 1175 ntype: 1176 recvchantype 1177 | fntype 1178 | othertype 1179 | ptrtype 1180 | dotname 1181 | '(' ntype ')' 1182 { 1183 $$ = nod(OTPAREN, $2, N); 1184 } 1185 1186 non_expr_type: 1187 recvchantype 1188 | fntype 1189 | othertype 1190 | '*' non_expr_type 1191 { 1192 $$ = nod(OIND, $2, N); 1193 } 1194 1195 non_recvchantype: 1196 fntype 1197 | othertype 1198 | ptrtype 1199 | dotname 1200 | '(' ntype ')' 1201 { 1202 $$ = nod(OTPAREN, $2, N); 1203 } 1204 1205 convtype: 1206 fntype 1207 | othertype 1208 1209 comptype: 1210 othertype 1211 1212 fnret_type: 1213 recvchantype 1214 | fntype 1215 | othertype 1216 | ptrtype 1217 | dotname 1218 1219 dotname: 1220 name 1221 | name '.' sym 1222 { 1223 if($1->op == OPACK) { 1224 Sym *s; 1225 s = restrictlookup($3->name, $1->pkg); 1226 $1->used = 1; 1227 $$ = oldname(s); 1228 break; 1229 } 1230 $$ = nod(OXDOT, $1, newname($3)); 1231 } 1232 1233 othertype: 1234 '[' oexpr ']' ntype 1235 { 1236 $$ = nod(OTARRAY, $2, $4); 1237 } 1238 | '[' LDDD ']' ntype 1239 { 1240 // array literal of nelem 1241 $$ = nod(OTARRAY, nod(ODDD, N, N), $4); 1242 } 1243 | LCHAN non_recvchantype 1244 { 1245 $$ = nod(OTCHAN, $2, N); 1246 $$->etype = Cboth; 1247 } 1248 | LCHAN LCOMM ntype 1249 { 1250 $$ = nod(OTCHAN, $3, N); 1251 $$->etype = Csend; 1252 } 1253 | LMAP '[' ntype ']' ntype 1254 { 1255 $$ = nod(OTMAP, $3, $5); 1256 } 1257 | structtype 1258 | interfacetype 1259 1260 ptrtype: 1261 '*' ntype 1262 { 1263 $$ = nod(OIND, $2, N); 1264 } 1265 1266 recvchantype: 1267 LCOMM LCHAN ntype 1268 { 1269 $$ = nod(OTCHAN, $3, N); 1270 $$->etype = Crecv; 1271 } 1272 1273 structtype: 1274 LSTRUCT lbrace structdcl_list osemi '}' 1275 { 1276 $$ = nod(OTSTRUCT, N, N); 1277 $$->list = $3; 1278 fixlbrace($2); 1279 } 1280 | LSTRUCT lbrace '}' 1281 { 1282 $$ = nod(OTSTRUCT, N, N); 1283 fixlbrace($2); 1284 } 1285 1286 interfacetype: 1287 LINTERFACE lbrace interfacedcl_list osemi '}' 1288 { 1289 $$ = nod(OTINTER, N, N); 1290 $$->list = $3; 1291 fixlbrace($2); 1292 } 1293 | LINTERFACE lbrace '}' 1294 { 1295 $$ = nod(OTINTER, N, N); 1296 fixlbrace($2); 1297 } 1298 1299 /* 1300 * function stuff 1301 * all in one place to show how crappy it all is 1302 */ 1303 xfndcl: 1304 LFUNC fndcl fnbody 1305 { 1306 $$ = $2; 1307 if($$ == N) 1308 break; 1309 if(noescape && $3 != nil) 1310 yyerror("can only use //go:noescape with external func implementations"); 1311 $$->nbody = $3; 1312 $$->endlineno = lineno; 1313 $$->noescape = noescape; 1314 funcbody($$); 1315 } 1316 1317 fndcl: 1318 sym '(' oarg_type_list_ocomma ')' fnres 1319 { 1320 Node *t; 1321 1322 $$ = N; 1323 $3 = checkarglist($3, 1); 1324 1325 if(strcmp($1->name, "init") == 0) { 1326 $1 = renameinit(); 1327 if($3 != nil || $5 != nil) 1328 yyerror("func init must have no arguments and no return values"); 1329 } 1330 if(strcmp(localpkg->name, "main") == 0 && strcmp($1->name, "main") == 0) { 1331 if($3 != nil || $5 != nil) 1332 yyerror("func main must have no arguments and no return values"); 1333 } 1334 1335 t = nod(OTFUNC, N, N); 1336 t->list = $3; 1337 t->rlist = $5; 1338 1339 $$ = nod(ODCLFUNC, N, N); 1340 $$->nname = newname($1); 1341 $$->nname->defn = $$; 1342 $$->nname->ntype = t; // TODO: check if nname already has an ntype 1343 declare($$->nname, PFUNC); 1344 1345 funchdr($$); 1346 } 1347 | '(' oarg_type_list_ocomma ')' sym '(' oarg_type_list_ocomma ')' fnres 1348 { 1349 Node *rcvr, *t; 1350 1351 $$ = N; 1352 $2 = checkarglist($2, 0); 1353 $6 = checkarglist($6, 1); 1354 1355 if($2 == nil) { 1356 yyerror("method has no receiver"); 1357 break; 1358 } 1359 if($2->next != nil) { 1360 yyerror("method has multiple receivers"); 1361 break; 1362 } 1363 rcvr = $2->n; 1364 if(rcvr->op != ODCLFIELD) { 1365 yyerror("bad receiver in method"); 1366 break; 1367 } 1368 if(rcvr->right->op == OTPAREN || (rcvr->right->op == OIND && rcvr->right->left->op == OTPAREN)) 1369 yyerror("cannot parenthesize receiver type"); 1370 1371 t = nod(OTFUNC, rcvr, N); 1372 t->list = $6; 1373 t->rlist = $8; 1374 1375 $$ = nod(ODCLFUNC, N, N); 1376 $$->shortname = newname($4); 1377 $$->nname = methodname1($$->shortname, rcvr->right); 1378 $$->nname->defn = $$; 1379 $$->nname->ntype = t; 1380 $$->nname->nointerface = nointerface; 1381 declare($$->nname, PFUNC); 1382 1383 funchdr($$); 1384 } 1385 1386 hidden_fndcl: 1387 hidden_pkg_importsym '(' ohidden_funarg_list ')' ohidden_funres 1388 { 1389 Sym *s; 1390 Type *t; 1391 1392 $$ = N; 1393 1394 s = $1; 1395 t = functype(N, $3, $5); 1396 1397 importsym(s, ONAME); 1398 if(s->def != N && s->def->op == ONAME) { 1399 if(eqtype(t, s->def->type)) { 1400 dclcontext = PDISCARD; // since we skip funchdr below 1401 break; 1402 } 1403 yyerror("inconsistent definition for func %S during import\n\t%T\n\t%T", s, s->def->type, t); 1404 } 1405 1406 $$ = newname(s); 1407 $$->type = t; 1408 declare($$, PFUNC); 1409 1410 funchdr($$); 1411 } 1412 | '(' hidden_funarg_list ')' sym '(' ohidden_funarg_list ')' ohidden_funres 1413 { 1414 $$ = methodname1(newname($4), $2->n->right); 1415 $$->type = functype($2->n, $6, $8); 1416 1417 checkwidth($$->type); 1418 addmethod($4, $$->type, 0, nointerface); 1419 nointerface = 0; 1420 funchdr($$); 1421 1422 // inl.c's inlnode in on a dotmeth node expects to find the inlineable body as 1423 // (dotmeth's type)->nname->inl, and dotmeth's type has been pulled 1424 // out by typecheck's lookdot as this $$->ttype. So by providing 1425 // this back link here we avoid special casing there. 1426 $$->type->nname = $$; 1427 } 1428 1429 fntype: 1430 LFUNC '(' oarg_type_list_ocomma ')' fnres 1431 { 1432 $3 = checkarglist($3, 1); 1433 $$ = nod(OTFUNC, N, N); 1434 $$->list = $3; 1435 $$->rlist = $5; 1436 } 1437 1438 fnbody: 1439 { 1440 $$ = nil; 1441 } 1442 | '{' stmt_list '}' 1443 { 1444 $$ = $2; 1445 if($$ == nil) 1446 $$ = list1(nod(OEMPTY, N, N)); 1447 } 1448 1449 fnres: 1450 %prec NotParen 1451 { 1452 $$ = nil; 1453 } 1454 | fnret_type 1455 { 1456 $$ = list1(nod(ODCLFIELD, N, $1)); 1457 } 1458 | '(' oarg_type_list_ocomma ')' 1459 { 1460 $2 = checkarglist($2, 0); 1461 $$ = $2; 1462 } 1463 1464 fnlitdcl: 1465 fntype 1466 { 1467 closurehdr($1); 1468 } 1469 1470 fnliteral: 1471 fnlitdcl lbrace stmt_list '}' 1472 { 1473 $$ = closurebody($3); 1474 fixlbrace($2); 1475 } 1476 | fnlitdcl error 1477 { 1478 $$ = closurebody(nil); 1479 } 1480 1481 /* 1482 * lists of things 1483 * note that they are left recursive 1484 * to conserve yacc stack. they need to 1485 * be reversed to interpret correctly 1486 */ 1487 xdcl_list: 1488 { 1489 $$ = nil; 1490 } 1491 | xdcl_list xdcl ';' 1492 { 1493 $$ = concat($1, $2); 1494 if(nsyntaxerrors == 0) 1495 testdclstack(); 1496 nointerface = 0; 1497 noescape = 0; 1498 } 1499 1500 vardcl_list: 1501 vardcl 1502 | vardcl_list ';' vardcl 1503 { 1504 $$ = concat($1, $3); 1505 } 1506 1507 constdcl_list: 1508 constdcl1 1509 | constdcl_list ';' constdcl1 1510 { 1511 $$ = concat($1, $3); 1512 } 1513 1514 typedcl_list: 1515 typedcl 1516 { 1517 $$ = list1($1); 1518 } 1519 | typedcl_list ';' typedcl 1520 { 1521 $$ = list($1, $3); 1522 } 1523 1524 structdcl_list: 1525 structdcl 1526 | structdcl_list ';' structdcl 1527 { 1528 $$ = concat($1, $3); 1529 } 1530 1531 interfacedcl_list: 1532 interfacedcl 1533 { 1534 $$ = list1($1); 1535 } 1536 | interfacedcl_list ';' interfacedcl 1537 { 1538 $$ = list($1, $3); 1539 } 1540 1541 structdcl: 1542 new_name_list ntype oliteral 1543 { 1544 NodeList *l; 1545 1546 Node *n; 1547 l = $1; 1548 if(l == nil) { 1549 // ? symbol, during import (list1(N) == nil) 1550 n = $2; 1551 if(n->op == OIND) 1552 n = n->left; 1553 n = embedded(n->sym, importpkg); 1554 n->right = $2; 1555 n->val = $3; 1556 $$ = list1(n); 1557 break; 1558 } 1559 1560 for(l=$1; l; l=l->next) { 1561 l->n = nod(ODCLFIELD, l->n, $2); 1562 l->n->val = $3; 1563 } 1564 } 1565 | embed oliteral 1566 { 1567 $1->val = $2; 1568 $$ = list1($1); 1569 } 1570 | '(' embed ')' oliteral 1571 { 1572 $2->val = $4; 1573 $$ = list1($2); 1574 yyerror("cannot parenthesize embedded type"); 1575 } 1576 | '*' embed oliteral 1577 { 1578 $2->right = nod(OIND, $2->right, N); 1579 $2->val = $3; 1580 $$ = list1($2); 1581 } 1582 | '(' '*' embed ')' oliteral 1583 { 1584 $3->right = nod(OIND, $3->right, N); 1585 $3->val = $5; 1586 $$ = list1($3); 1587 yyerror("cannot parenthesize embedded type"); 1588 } 1589 | '*' '(' embed ')' oliteral 1590 { 1591 $3->right = nod(OIND, $3->right, N); 1592 $3->val = $5; 1593 $$ = list1($3); 1594 yyerror("cannot parenthesize embedded type"); 1595 } 1596 1597 packname: 1598 LNAME 1599 { 1600 Node *n; 1601 1602 $$ = $1; 1603 n = oldname($1); 1604 if(n->pack != N) 1605 n->pack->used = 1; 1606 } 1607 | LNAME '.' sym 1608 { 1609 Pkg *pkg; 1610 1611 if($1->def == N || $1->def->op != OPACK) { 1612 yyerror("%S is not a package", $1); 1613 pkg = localpkg; 1614 } else { 1615 $1->def->used = 1; 1616 pkg = $1->def->pkg; 1617 } 1618 $$ = restrictlookup($3->name, pkg); 1619 } 1620 1621 embed: 1622 packname 1623 { 1624 $$ = embedded($1, localpkg); 1625 } 1626 1627 interfacedcl: 1628 new_name indcl 1629 { 1630 $$ = nod(ODCLFIELD, $1, $2); 1631 ifacedcl($$); 1632 } 1633 | packname 1634 { 1635 $$ = nod(ODCLFIELD, N, oldname($1)); 1636 } 1637 | '(' packname ')' 1638 { 1639 $$ = nod(ODCLFIELD, N, oldname($2)); 1640 yyerror("cannot parenthesize embedded type"); 1641 } 1642 1643 indcl: 1644 '(' oarg_type_list_ocomma ')' fnres 1645 { 1646 // without func keyword 1647 $2 = checkarglist($2, 1); 1648 $$ = nod(OTFUNC, fakethis(), N); 1649 $$->list = $2; 1650 $$->rlist = $4; 1651 } 1652 1653 /* 1654 * function arguments. 1655 */ 1656 arg_type: 1657 name_or_type 1658 | sym name_or_type 1659 { 1660 $$ = nod(ONONAME, N, N); 1661 $$->sym = $1; 1662 $$ = nod(OKEY, $$, $2); 1663 } 1664 | sym dotdotdot 1665 { 1666 $$ = nod(ONONAME, N, N); 1667 $$->sym = $1; 1668 $$ = nod(OKEY, $$, $2); 1669 } 1670 | dotdotdot 1671 1672 arg_type_list: 1673 arg_type 1674 { 1675 $$ = list1($1); 1676 } 1677 | arg_type_list ',' arg_type 1678 { 1679 $$ = list($1, $3); 1680 } 1681 1682 oarg_type_list_ocomma: 1683 { 1684 $$ = nil; 1685 } 1686 | arg_type_list ocomma 1687 { 1688 $$ = $1; 1689 } 1690 1691 /* 1692 * statement 1693 */ 1694 stmt: 1695 { 1696 $$ = N; 1697 } 1698 | compound_stmt 1699 | common_dcl 1700 { 1701 $$ = liststmt($1); 1702 } 1703 | non_dcl_stmt 1704 | error 1705 { 1706 $$ = N; 1707 } 1708 1709 non_dcl_stmt: 1710 simple_stmt 1711 | for_stmt 1712 | switch_stmt 1713 | select_stmt 1714 | if_stmt 1715 | labelname ':' 1716 { 1717 $1 = nod(OLABEL, $1, N); 1718 $1->sym = dclstack; // context, for goto restrictions 1719 } 1720 stmt 1721 { 1722 NodeList *l; 1723 1724 $1->defn = $4; 1725 l = list1($1); 1726 if($4) 1727 l = list(l, $4); 1728 $$ = liststmt(l); 1729 } 1730 | LFALL 1731 { 1732 // will be converted to OFALL 1733 $$ = nod(OXFALL, N, N); 1734 $$->xoffset = block; 1735 } 1736 | LBREAK onew_name 1737 { 1738 $$ = nod(OBREAK, $2, N); 1739 } 1740 | LCONTINUE onew_name 1741 { 1742 $$ = nod(OCONTINUE, $2, N); 1743 } 1744 | LGO pseudocall 1745 { 1746 $$ = nod(OPROC, $2, N); 1747 } 1748 | LDEFER pseudocall 1749 { 1750 $$ = nod(ODEFER, $2, N); 1751 } 1752 | LGOTO new_name 1753 { 1754 $$ = nod(OGOTO, $2, N); 1755 $$->sym = dclstack; // context, for goto restrictions 1756 } 1757 | LRETURN oexpr_list 1758 { 1759 $$ = nod(ORETURN, N, N); 1760 $$->list = $2; 1761 if($$->list == nil && curfn != N) { 1762 NodeList *l; 1763 1764 for(l=curfn->dcl; l; l=l->next) { 1765 if(l->n->class == PPARAM) 1766 continue; 1767 if(l->n->class != PPARAMOUT) 1768 break; 1769 if(l->n->sym->def != l->n) 1770 yyerror("%s is shadowed during return", l->n->sym->name); 1771 } 1772 } 1773 } 1774 1775 stmt_list: 1776 stmt 1777 { 1778 $$ = nil; 1779 if($1 != N) 1780 $$ = list1($1); 1781 } 1782 | stmt_list ';' stmt 1783 { 1784 $$ = $1; 1785 if($3 != N) 1786 $$ = list($$, $3); 1787 } 1788 1789 new_name_list: 1790 new_name 1791 { 1792 $$ = list1($1); 1793 } 1794 | new_name_list ',' new_name 1795 { 1796 $$ = list($1, $3); 1797 } 1798 1799 dcl_name_list: 1800 dcl_name 1801 { 1802 $$ = list1($1); 1803 } 1804 | dcl_name_list ',' dcl_name 1805 { 1806 $$ = list($1, $3); 1807 } 1808 1809 expr_list: 1810 expr 1811 { 1812 $$ = list1($1); 1813 } 1814 | expr_list ',' expr 1815 { 1816 $$ = list($1, $3); 1817 } 1818 1819 expr_or_type_list: 1820 expr_or_type 1821 { 1822 $$ = list1($1); 1823 } 1824 | expr_or_type_list ',' expr_or_type 1825 { 1826 $$ = list($1, $3); 1827 } 1828 1829 /* 1830 * list of combo of keyval and val 1831 */ 1832 keyval_list: 1833 keyval 1834 { 1835 $$ = list1($1); 1836 } 1837 | bare_complitexpr 1838 { 1839 $$ = list1($1); 1840 } 1841 | keyval_list ',' keyval 1842 { 1843 $$ = list($1, $3); 1844 } 1845 | keyval_list ',' bare_complitexpr 1846 { 1847 $$ = list($1, $3); 1848 } 1849 1850 braced_keyval_list: 1851 { 1852 $$ = nil; 1853 } 1854 | keyval_list ocomma 1855 { 1856 $$ = $1; 1857 } 1858 1859 /* 1860 * optional things 1861 */ 1862 osemi: 1863 | ';' 1864 1865 ocomma: 1866 | ',' 1867 1868 oexpr: 1869 { 1870 $$ = N; 1871 } 1872 | expr 1873 1874 oexpr_list: 1875 { 1876 $$ = nil; 1877 } 1878 | expr_list 1879 1880 osimple_stmt: 1881 { 1882 $$ = N; 1883 } 1884 | simple_stmt 1885 1886 ohidden_funarg_list: 1887 { 1888 $$ = nil; 1889 } 1890 | hidden_funarg_list 1891 1892 ohidden_structdcl_list: 1893 { 1894 $$ = nil; 1895 } 1896 | hidden_structdcl_list 1897 1898 ohidden_interfacedcl_list: 1899 { 1900 $$ = nil; 1901 } 1902 | hidden_interfacedcl_list 1903 1904 oliteral: 1905 { 1906 $$.ctype = CTxxx; 1907 } 1908 | LLITERAL 1909 1910 /* 1911 * import syntax from package header 1912 */ 1913 hidden_import: 1914 LIMPORT LNAME LLITERAL ';' 1915 { 1916 importimport($2, $3.u.sval); 1917 } 1918 | LVAR hidden_pkg_importsym hidden_type ';' 1919 { 1920 importvar($2, $3); 1921 } 1922 | LCONST hidden_pkg_importsym '=' hidden_constant ';' 1923 { 1924 importconst($2, types[TIDEAL], $4); 1925 } 1926 | LCONST hidden_pkg_importsym hidden_type '=' hidden_constant ';' 1927 { 1928 importconst($2, $3, $5); 1929 } 1930 | LTYPE hidden_pkgtype hidden_type ';' 1931 { 1932 importtype($2, $3); 1933 } 1934 | LFUNC hidden_fndcl fnbody ';' 1935 { 1936 if($2 == N) { 1937 dclcontext = PEXTERN; // since we skip the funcbody below 1938 break; 1939 } 1940 1941 $2->inl = $3; 1942 1943 funcbody($2); 1944 importlist = list(importlist, $2); 1945 1946 if(debug['E']) { 1947 print("import [%Z] func %lN \n", importpkg->path, $2); 1948 if(debug['m'] > 2 && $2->inl) 1949 print("inl body:%+H\n", $2->inl); 1950 } 1951 } 1952 1953 hidden_pkg_importsym: 1954 hidden_importsym 1955 { 1956 $$ = $1; 1957 structpkg = $$->pkg; 1958 } 1959 1960 hidden_pkgtype: 1961 hidden_pkg_importsym 1962 { 1963 $$ = pkgtype($1); 1964 importsym($1, OTYPE); 1965 } 1966 1967 /* 1968 * importing types 1969 */ 1970 1971 hidden_type: 1972 hidden_type_misc 1973 | hidden_type_recv_chan 1974 | hidden_type_func 1975 1976 hidden_type_non_recv_chan: 1977 hidden_type_misc 1978 | hidden_type_func 1979 1980 hidden_type_misc: 1981 hidden_importsym 1982 { 1983 $$ = pkgtype($1); 1984 } 1985 | LNAME 1986 { 1987 // predefined name like uint8 1988 $1 = pkglookup($1->name, builtinpkg); 1989 if($1->def == N || $1->def->op != OTYPE) { 1990 yyerror("%s is not a type", $1->name); 1991 $$ = T; 1992 } else 1993 $$ = $1->def->type; 1994 } 1995 | '[' ']' hidden_type 1996 { 1997 $$ = aindex(N, $3); 1998 } 1999 | '[' LLITERAL ']' hidden_type 2000 { 2001 $$ = aindex(nodlit($2), $4); 2002 } 2003 | LMAP '[' hidden_type ']' hidden_type 2004 { 2005 $$ = maptype($3, $5); 2006 } 2007 | LSTRUCT '{' ohidden_structdcl_list '}' 2008 { 2009 $$ = tostruct($3); 2010 } 2011 | LINTERFACE '{' ohidden_interfacedcl_list '}' 2012 { 2013 $$ = tointerface($3); 2014 } 2015 | '*' hidden_type 2016 { 2017 $$ = ptrto($2); 2018 } 2019 | LCHAN hidden_type_non_recv_chan 2020 { 2021 $$ = typ(TCHAN); 2022 $$->type = $2; 2023 $$->chan = Cboth; 2024 } 2025 | LCHAN '(' hidden_type_recv_chan ')' 2026 { 2027 $$ = typ(TCHAN); 2028 $$->type = $3; 2029 $$->chan = Cboth; 2030 } 2031 | LCHAN LCOMM hidden_type 2032 { 2033 $$ = typ(TCHAN); 2034 $$->type = $3; 2035 $$->chan = Csend; 2036 } 2037 2038 hidden_type_recv_chan: 2039 LCOMM LCHAN hidden_type 2040 { 2041 $$ = typ(TCHAN); 2042 $$->type = $3; 2043 $$->chan = Crecv; 2044 } 2045 2046 hidden_type_func: 2047 LFUNC '(' ohidden_funarg_list ')' ohidden_funres 2048 { 2049 $$ = functype(nil, $3, $5); 2050 } 2051 2052 hidden_funarg: 2053 sym hidden_type oliteral 2054 { 2055 $$ = nod(ODCLFIELD, N, typenod($2)); 2056 if($1) 2057 $$->left = newname($1); 2058 $$->val = $3; 2059 } 2060 | sym LDDD hidden_type oliteral 2061 { 2062 Type *t; 2063 2064 t = typ(TARRAY); 2065 t->bound = -1; 2066 t->type = $3; 2067 2068 $$ = nod(ODCLFIELD, N, typenod(t)); 2069 if($1) 2070 $$->left = newname($1); 2071 $$->isddd = 1; 2072 $$->val = $4; 2073 } 2074 2075 hidden_structdcl: 2076 sym hidden_type oliteral 2077 { 2078 Sym *s; 2079 Pkg *p; 2080 2081 if($1 != S && strcmp($1->name, "?") != 0) { 2082 $$ = nod(ODCLFIELD, newname($1), typenod($2)); 2083 $$->val = $3; 2084 } else { 2085 s = $2->sym; 2086 if(s == S && isptr[$2->etype]) 2087 s = $2->type->sym; 2088 p = importpkg; 2089 if($1 != S) 2090 p = $1->pkg; 2091 $$ = embedded(s, p); 2092 $$->right = typenod($2); 2093 $$->val = $3; 2094 } 2095 } 2096 2097 hidden_interfacedcl: 2098 sym '(' ohidden_funarg_list ')' ohidden_funres 2099 { 2100 $$ = nod(ODCLFIELD, newname($1), typenod(functype(fakethis(), $3, $5))); 2101 } 2102 | hidden_type 2103 { 2104 $$ = nod(ODCLFIELD, N, typenod($1)); 2105 } 2106 2107 ohidden_funres: 2108 { 2109 $$ = nil; 2110 } 2111 | hidden_funres 2112 2113 hidden_funres: 2114 '(' ohidden_funarg_list ')' 2115 { 2116 $$ = $2; 2117 } 2118 | hidden_type 2119 { 2120 $$ = list1(nod(ODCLFIELD, N, typenod($1))); 2121 } 2122 2123 /* 2124 * importing constants 2125 */ 2126 2127 hidden_literal: 2128 LLITERAL 2129 { 2130 $$ = nodlit($1); 2131 } 2132 | '-' LLITERAL 2133 { 2134 $$ = nodlit($2); 2135 switch($$->val.ctype){ 2136 case CTINT: 2137 case CTRUNE: 2138 mpnegfix($$->val.u.xval); 2139 break; 2140 case CTFLT: 2141 mpnegflt($$->val.u.fval); 2142 break; 2143 case CTCPLX: 2144 mpnegflt(&$$->val.u.cval->real); 2145 mpnegflt(&$$->val.u.cval->imag); 2146 break; 2147 default: 2148 yyerror("bad negated constant"); 2149 } 2150 } 2151 | sym 2152 { 2153 $$ = oldname(pkglookup($1->name, builtinpkg)); 2154 if($$->op != OLITERAL) 2155 yyerror("bad constant %S", $$->sym); 2156 } 2157 2158 hidden_constant: 2159 hidden_literal 2160 | '(' hidden_literal '+' hidden_literal ')' 2161 { 2162 if($2->val.ctype == CTRUNE && $4->val.ctype == CTINT) { 2163 $$ = $2; 2164 mpaddfixfix($2->val.u.xval, $4->val.u.xval, 0); 2165 break; 2166 } 2167 $4->val.u.cval->real = $4->val.u.cval->imag; 2168 mpmovecflt(&$4->val.u.cval->imag, 0.0); 2169 $$ = nodcplxlit($2->val, $4->val); 2170 } 2171 2172 hidden_import_list: 2173 | hidden_import_list hidden_import 2174 2175 hidden_funarg_list: 2176 hidden_funarg 2177 { 2178 $$ = list1($1); 2179 } 2180 | hidden_funarg_list ',' hidden_funarg 2181 { 2182 $$ = list($1, $3); 2183 } 2184 2185 hidden_structdcl_list: 2186 hidden_structdcl 2187 { 2188 $$ = list1($1); 2189 } 2190 | hidden_structdcl_list ';' hidden_structdcl 2191 { 2192 $$ = list($1, $3); 2193 } 2194 2195 hidden_interfacedcl_list: 2196 hidden_interfacedcl 2197 { 2198 $$ = list1($1); 2199 } 2200 | hidden_interfacedcl_list ';' hidden_interfacedcl 2201 { 2202 $$ = list($1, $3); 2203 } 2204 2205 %% 2206 2207 static void 2208 fixlbrace(int lbr) 2209 { 2210 // If the opening brace was an LBODY, 2211 // set up for another one now that we're done. 2212 // See comment in lex.c about loophack. 2213 if(lbr == LBODY) 2214 loophack = 1; 2215 } 2216