PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cmd/gc/go.y

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