PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cmd/gc/go.y

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