PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cmd/gc/go.y

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