PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cmd/gc/go.y

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