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