PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cmd/gc/go.y

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