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