PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cmd/gc/go.y

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