PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cmd/gc/go.y

https://bitbucket.org/taruti/go.plan9
Happy | 1950 lines | 1775 code | 175 blank | 0 comment | 0 complexity | 9659c4ecd209065dcfdcaa7e75b728f3 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 "go.h"
  20. static void fixlbrace(int);
  21. %}
  22. %union {
  23. Node* node;
  24. NodeList* list;
  25. Type* type;
  26. Sym* sym;
  27. struct Val val;
  28. int lint;
  29. }
  30. // |sed 's/.* //' |9 fmt -l1 |sort |9 fmt -l50 | sed 's/^/%xxx /'
  31. %token <val> LLITERAL
  32. %token <lint> LASOP
  33. %token <sym> LBREAK LCASE LCHAN LCOLAS LCONST LCONTINUE LDDD
  34. %token <sym> LDEFAULT LDEFER LELSE LFALL LFOR LFUNC LGO LGOTO
  35. %token <sym> LIF LIMPORT LINTERFACE LMAP LNAME
  36. %token <sym> LPACKAGE LRANGE LRETURN LSELECT LSTRUCT LSWITCH
  37. %token <sym> LTYPE LVAR
  38. %token LANDAND LANDNOT LBODY LCOMM LDEC LEQ LGE LGT
  39. %token LIGNORE LINC LLE LLSH LLT LNE LOROR LRSH
  40. %type <lint> lbrace import_here
  41. %type <sym> sym packname
  42. %type <val> oliteral
  43. %type <node> stmt ntype
  44. %type <node> arg_type
  45. %type <node> case caseblock
  46. %type <node> compound_stmt dotname embed expr complitexpr
  47. %type <node> expr_or_type
  48. %type <node> fndcl fnliteral
  49. %type <node> for_body for_header for_stmt if_header if_stmt non_dcl_stmt
  50. %type <node> interfacedcl keyval labelname name
  51. %type <node> name_or_type non_expr_type
  52. %type <node> new_name dcl_name oexpr typedclname
  53. %type <node> onew_name
  54. %type <node> osimple_stmt pexpr pexpr_no_paren
  55. %type <node> pseudocall range_stmt select_stmt
  56. %type <node> simple_stmt
  57. %type <node> switch_stmt uexpr
  58. %type <node> xfndcl typedcl
  59. %type <list> xdcl fnbody fnres switch_body loop_body dcl_name_list
  60. %type <list> new_name_list expr_list keyval_list braced_keyval_list expr_or_type_list xdcl_list
  61. %type <list> oexpr_list caseblock_list stmt_list oarg_type_list_ocomma arg_type_list
  62. %type <list> interfacedcl_list vardcl vardcl_list structdcl structdcl_list
  63. %type <list> common_dcl constdcl constdcl1 constdcl_list typedcl_list
  64. %type <node> convtype comptype dotdotdot
  65. %type <node> indcl interfacetype structtype ptrtype
  66. %type <node> recvchantype non_recvchantype othertype fnret_type fntype
  67. %type <val> hidden_tag
  68. %type <sym> hidden_importsym hidden_pkg_importsym
  69. %type <node> hidden_constant hidden_literal hidden_dcl
  70. %type <node> hidden_interfacedcl hidden_structdcl hidden_opt_sym
  71. %type <list> hidden_funres
  72. %type <list> ohidden_funres
  73. %type <list> hidden_funarg_list ohidden_funarg_list
  74. %type <list> hidden_interfacedcl_list ohidden_interfacedcl_list
  75. %type <list> hidden_structdcl_list ohidden_structdcl_list
  76. %type <type> hidden_type hidden_type_misc hidden_pkgtype
  77. %type <type> hidden_type_func
  78. %type <type> hidden_type_recv_chan hidden_type_non_recv_chan
  79. %left LOROR
  80. %left LANDAND
  81. %left LCOMM
  82. %left LEQ LNE LLE LGE LLT LGT
  83. %left '+' '-' '|' '^'
  84. %left '*' '/' '%' '&' LLSH LRSH LANDNOT
  85. /*
  86. * manual override of shift/reduce conflicts.
  87. * the general form is that we assign a precedence
  88. * to the token being shifted and then introduce
  89. * NotToken with lower precedence or PreferToToken with higher
  90. * and annotate the reducing rule accordingly.
  91. */
  92. %left NotPackage
  93. %left LPACKAGE
  94. %left NotParen
  95. %left '('
  96. %left ')'
  97. %left PreferToRightParen
  98. %%
  99. file:
  100. loadsys
  101. package
  102. imports
  103. xdcl_list
  104. {
  105. xtop = concat(xtop, $4);
  106. }
  107. package:
  108. %prec NotPackage
  109. {
  110. prevlineno = lineno;
  111. yyerror("package statement must be first");
  112. flusherrors();
  113. mkpackage("main");
  114. }
  115. | LPACKAGE sym ';'
  116. {
  117. mkpackage($2->name);
  118. }
  119. /*
  120. * this loads the definitions for the low-level runtime functions,
  121. * so that the compiler can generate calls to them,
  122. * but does not make the name "runtime" visible as a package.
  123. */
  124. loadsys:
  125. {
  126. importpkg = runtimepkg;
  127. if(debug['A'])
  128. cannedimports("runtime.builtin", "package runtime\n\n$$\n\n");
  129. else
  130. cannedimports("runtime.builtin", runtimeimport);
  131. curio.importsafe = 1;
  132. }
  133. import_package
  134. import_there
  135. {
  136. importpkg = nil;
  137. }
  138. imports:
  139. | imports import ';'
  140. import:
  141. LIMPORT import_stmt
  142. | LIMPORT '(' import_stmt_list osemi ')'
  143. | LIMPORT '(' ')'
  144. import_stmt:
  145. import_here import_package import_there
  146. {
  147. Pkg *ipkg;
  148. Sym *my;
  149. Node *pack;
  150. ipkg = importpkg;
  151. my = importmyname;
  152. importpkg = nil;
  153. importmyname = S;
  154. if(my == nil)
  155. my = lookup(ipkg->name);
  156. pack = nod(OPACK, N, N);
  157. pack->sym = my;
  158. pack->pkg = ipkg;
  159. pack->lineno = $1;
  160. if(my->name[0] == '.') {
  161. importdot(ipkg, pack);
  162. break;
  163. }
  164. if(my->name[0] == '_' && my->name[1] == '\0')
  165. break;
  166. if(my->def) {
  167. lineno = $1;
  168. redeclare(my, "as imported package name");
  169. }
  170. my->def = pack;
  171. my->lastlineno = $1;
  172. my->block = 1; // at top level
  173. }
  174. import_stmt_list:
  175. import_stmt
  176. | import_stmt_list ';' import_stmt
  177. import_here:
  178. LLITERAL
  179. {
  180. // import with original name
  181. $$ = parserline();
  182. importmyname = S;
  183. importfile(&$1, $$);
  184. }
  185. | sym LLITERAL
  186. {
  187. // import with given name
  188. $$ = parserline();
  189. importmyname = $1;
  190. importfile(&$2, $$);
  191. }
  192. | '.' LLITERAL
  193. {
  194. // import into my name space
  195. $$ = parserline();
  196. importmyname = lookup(".");
  197. importfile(&$2, $$);
  198. }
  199. import_package:
  200. LPACKAGE sym import_safety ';'
  201. {
  202. importpkg->name = $2->name;
  203. importpkg->direct = 1;
  204. if(safemode && !curio.importsafe)
  205. yyerror("cannot import unsafe package %Z", importpkg->path);
  206. // NOTE(rsc): This is no longer a technical restriction:
  207. // the 6g tool chain would work just fine without giving
  208. // special meaning to a package being named main.
  209. // Other implementations might need the restriction
  210. // (gccgo does), so it stays in the language and the compiler.
  211. if(strcmp($2->name, "main") == 0)
  212. yyerror("cannot import package main");
  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. if($3->next != nil)
  375. yyerror("expr.(type) must be alone in list");
  376. else if($1->next != nil)
  377. yyerror("argument count mismatch: %d = %d", count($1), 1);
  378. $$ = nod(OTYPESW, $1->n, $3->n->right);
  379. break;
  380. }
  381. $$ = colas($1, $3);
  382. }
  383. | expr LINC
  384. {
  385. $$ = nod(OASOP, $1, nodintconst(1));
  386. $$->etype = OADD;
  387. }
  388. | expr LDEC
  389. {
  390. $$ = nod(OASOP, $1, nodintconst(1));
  391. $$->etype = OSUB;
  392. }
  393. case:
  394. LCASE expr_or_type_list ':'
  395. {
  396. Node *n;
  397. // will be converted to OCASE
  398. // right will point to next case
  399. // done in casebody()
  400. poptodcl();
  401. $$ = nod(OXCASE, N, N);
  402. $$->list = $2;
  403. if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) {
  404. // type switch - declare variable
  405. n = newname(n->sym);
  406. n->used = 1; // TODO(rsc): better job here
  407. declare(n, dclcontext);
  408. $$->nname = n;
  409. }
  410. break;
  411. }
  412. | LCASE expr '=' expr ':'
  413. {
  414. // will be converted to OCASE
  415. // right will point to next case
  416. // done in casebody()
  417. poptodcl();
  418. $$ = nod(OXCASE, N, N);
  419. $$->list = list1(nod(OAS, $2, $4));
  420. }
  421. | LCASE name LCOLAS expr ':'
  422. {
  423. // will be converted to OCASE
  424. // right will point to next case
  425. // done in casebody()
  426. poptodcl();
  427. $$ = nod(OXCASE, N, N);
  428. $$->list = list1(colas(list1($2), list1($4)));
  429. }
  430. | LDEFAULT ':'
  431. {
  432. Node *n;
  433. poptodcl();
  434. $$ = nod(OXCASE, N, N);
  435. if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) {
  436. // type switch - declare variable
  437. n = newname(n->sym);
  438. n->used = 1; // TODO(rsc): better job here
  439. declare(n, dclcontext);
  440. $$->nname = n;
  441. }
  442. }
  443. compound_stmt:
  444. '{'
  445. {
  446. markdcl();
  447. }
  448. stmt_list '}'
  449. {
  450. $$ = liststmt($3);
  451. popdcl();
  452. }
  453. switch_body:
  454. LBODY
  455. {
  456. markdcl();
  457. }
  458. caseblock_list '}'
  459. {
  460. $$ = $3;
  461. popdcl();
  462. }
  463. caseblock:
  464. case
  465. {
  466. // If the last token read by the lexer was consumed
  467. // as part of the case, clear it (parser has cleared yychar).
  468. // If the last token read by the lexer was the lookahead
  469. // leave it alone (parser has it cached in yychar).
  470. // This is so that the stmt_list action doesnt look at
  471. // the case tokens if the stmt_list is empty.
  472. yylast = yychar;
  473. }
  474. stmt_list
  475. {
  476. int last;
  477. // This is the only place in the language where a statement
  478. // list is not allowed to drop the final semicolon, because
  479. // its the only place where a statement list is not followed
  480. // by a closing brace. Handle the error for pedantry.
  481. // Find the final token of the statement list.
  482. // yylast is lookahead; yyprev is last of stmt_list
  483. last = yyprev;
  484. if(last > 0 && last != ';' && yychar != '}')
  485. yyerror("missing statement after label");
  486. $$ = $1;
  487. $$->nbody = $3;
  488. }
  489. caseblock_list:
  490. {
  491. $$ = nil;
  492. }
  493. | caseblock_list caseblock
  494. {
  495. $$ = list($1, $2);
  496. }
  497. loop_body:
  498. LBODY
  499. {
  500. markdcl();
  501. }
  502. stmt_list '}'
  503. {
  504. $$ = $3;
  505. popdcl();
  506. }
  507. range_stmt:
  508. expr_list '=' LRANGE expr
  509. {
  510. $$ = nod(ORANGE, N, $4);
  511. $$->list = $1;
  512. $$->etype = 0; // := flag
  513. }
  514. | expr_list LCOLAS LRANGE expr
  515. {
  516. $$ = nod(ORANGE, N, $4);
  517. $$->list = $1;
  518. $$->colas = 1;
  519. colasdefn($1, $$);
  520. }
  521. for_header:
  522. osimple_stmt ';' osimple_stmt ';' osimple_stmt
  523. {
  524. // init ; test ; incr
  525. if($5 != N && $5->colas != 0)
  526. yyerror("cannot declare in the for-increment");
  527. $$ = nod(OFOR, N, N);
  528. if($1 != N)
  529. $$->ninit = list1($1);
  530. $$->ntest = $3;
  531. $$->nincr = $5;
  532. }
  533. | osimple_stmt
  534. {
  535. // normal test
  536. $$ = nod(OFOR, N, N);
  537. $$->ntest = $1;
  538. }
  539. | range_stmt
  540. for_body:
  541. for_header loop_body
  542. {
  543. $$ = $1;
  544. $$->nbody = concat($$->nbody, $2);
  545. }
  546. for_stmt:
  547. LFOR
  548. {
  549. markdcl();
  550. }
  551. for_body
  552. {
  553. $$ = $3;
  554. popdcl();
  555. }
  556. if_header:
  557. osimple_stmt
  558. {
  559. // test
  560. $$ = nod(OIF, N, N);
  561. $$->ntest = $1;
  562. }
  563. | osimple_stmt ';' osimple_stmt
  564. {
  565. // init ; test
  566. $$ = nod(OIF, N, N);
  567. if($1 != N)
  568. $$->ninit = list1($1);
  569. $$->ntest = $3;
  570. }
  571. if_stmt:
  572. LIF
  573. {
  574. markdcl();
  575. }
  576. if_header loop_body
  577. {
  578. $$ = $3;
  579. $$->nbody = $4;
  580. // no popdcl; maybe theres an LELSE
  581. }
  582. switch_stmt:
  583. LSWITCH
  584. {
  585. markdcl();
  586. }
  587. if_header
  588. {
  589. Node *n;
  590. n = $3->ntest;
  591. if(n != N && n->op != OTYPESW)
  592. n = N;
  593. typesw = nod(OXXX, typesw, n);
  594. }
  595. switch_body
  596. {
  597. $$ = $3;
  598. $$->op = OSWITCH;
  599. $$->list = $5;
  600. typesw = typesw->left;
  601. popdcl();
  602. }
  603. select_stmt:
  604. LSELECT
  605. {
  606. markdcl();
  607. typesw = nod(OXXX, typesw, N);
  608. }
  609. switch_body
  610. {
  611. $$ = nod(OSELECT, N, N);
  612. $$->list = $3;
  613. typesw = typesw->left;
  614. popdcl();
  615. }
  616. /*
  617. * expressions
  618. */
  619. expr:
  620. uexpr
  621. | expr LOROR expr
  622. {
  623. $$ = nod(OOROR, $1, $3);
  624. }
  625. | expr LANDAND expr
  626. {
  627. $$ = nod(OANDAND, $1, $3);
  628. }
  629. | expr LEQ expr
  630. {
  631. $$ = nod(OEQ, $1, $3);
  632. }
  633. | expr LNE expr
  634. {
  635. $$ = nod(ONE, $1, $3);
  636. }
  637. | expr LLT expr
  638. {
  639. $$ = nod(OLT, $1, $3);
  640. }
  641. | expr LLE expr
  642. {
  643. $$ = nod(OLE, $1, $3);
  644. }
  645. | expr LGE expr
  646. {
  647. $$ = nod(OGE, $1, $3);
  648. }
  649. | expr LGT expr
  650. {
  651. $$ = nod(OGT, $1, $3);
  652. }
  653. | expr '+' expr
  654. {
  655. $$ = nod(OADD, $1, $3);
  656. }
  657. | expr '-' expr
  658. {
  659. $$ = nod(OSUB, $1, $3);
  660. }
  661. | expr '|' expr
  662. {
  663. $$ = nod(OOR, $1, $3);
  664. }
  665. | expr '^' expr
  666. {
  667. $$ = nod(OXOR, $1, $3);
  668. }
  669. | expr '*' expr
  670. {
  671. $$ = nod(OMUL, $1, $3);
  672. }
  673. | expr '/' expr
  674. {
  675. $$ = nod(ODIV, $1, $3);
  676. }
  677. | expr '%' expr
  678. {
  679. $$ = nod(OMOD, $1, $3);
  680. }
  681. | expr '&' expr
  682. {
  683. $$ = nod(OAND, $1, $3);
  684. }
  685. | expr LANDNOT expr
  686. {
  687. $$ = nod(OANDNOT, $1, $3);
  688. }
  689. | expr LLSH expr
  690. {
  691. $$ = nod(OLSH, $1, $3);
  692. }
  693. | expr LRSH expr
  694. {
  695. $$ = nod(ORSH, $1, $3);
  696. }
  697. | expr LCOMM expr
  698. {
  699. $$ = nod(OSEND, $1, $3);
  700. }
  701. uexpr:
  702. pexpr
  703. | '*' uexpr
  704. {
  705. $$ = nod(OIND, $2, N);
  706. }
  707. | '&' uexpr
  708. {
  709. $$ = nod(OADDR, $2, N);
  710. }
  711. | '+' uexpr
  712. {
  713. $$ = nod(OPLUS, $2, N);
  714. }
  715. | '-' uexpr
  716. {
  717. $$ = nod(OMINUS, $2, N);
  718. }
  719. | '!' uexpr
  720. {
  721. $$ = nod(ONOT, $2, N);
  722. }
  723. | '~' uexpr
  724. {
  725. yyerror("the bitwise complement operator is ^");
  726. $$ = nod(OCOM, $2, N);
  727. }
  728. | '^' uexpr
  729. {
  730. $$ = nod(OCOM, $2, N);
  731. }
  732. | LCOMM uexpr
  733. {
  734. $$ = nod(ORECV, $2, N);
  735. }
  736. /*
  737. * call-like statements that
  738. * can be preceded by 'defer' and 'go'
  739. */
  740. pseudocall:
  741. pexpr '(' ')'
  742. {
  743. $$ = nod(OCALL, $1, N);
  744. }
  745. | pexpr '(' expr_or_type_list ocomma ')'
  746. {
  747. $$ = nod(OCALL, $1, N);
  748. $$->list = $3;
  749. }
  750. | pexpr '(' expr_or_type_list LDDD ocomma ')'
  751. {
  752. $$ = nod(OCALL, $1, N);
  753. $$->list = $3;
  754. $$->isddd = 1;
  755. }
  756. pexpr_no_paren:
  757. LLITERAL
  758. {
  759. $$ = nodlit($1);
  760. }
  761. | name
  762. | pexpr '.' sym
  763. {
  764. if($1->op == OPACK) {
  765. Sym *s;
  766. s = restrictlookup($3->name, $1->pkg);
  767. $1->used = 1;
  768. $$ = oldname(s);
  769. break;
  770. }
  771. $$ = nod(OXDOT, $1, newname($3));
  772. }
  773. | pexpr '.' '(' expr_or_type ')'
  774. {
  775. $$ = nod(ODOTTYPE, $1, $4);
  776. }
  777. | pexpr '.' '(' LTYPE ')'
  778. {
  779. $$ = nod(OTYPESW, N, $1);
  780. }
  781. | pexpr '[' expr ']'
  782. {
  783. $$ = nod(OINDEX, $1, $3);
  784. }
  785. | pexpr '[' oexpr ':' oexpr ']'
  786. {
  787. $$ = nod(OSLICE, $1, nod(OKEY, $3, $5));
  788. }
  789. | pseudocall
  790. | convtype '(' expr ')'
  791. {
  792. // conversion
  793. $$ = nod(OCALL, $1, N);
  794. $$->list = list1($3);
  795. }
  796. | comptype lbrace braced_keyval_list '}'
  797. {
  798. // composite expression
  799. $$ = nod(OCOMPLIT, N, $1);
  800. $$->list = $3;
  801. fixlbrace($2);
  802. }
  803. | pexpr_no_paren '{' braced_keyval_list '}'
  804. {
  805. // composite expression
  806. $$ = nod(OCOMPLIT, N, $1);
  807. $$->list = $3;
  808. }
  809. | '(' expr_or_type ')' '{' braced_keyval_list '}'
  810. {
  811. yyerror("cannot parenthesize type in composite literal");
  812. // composite expression
  813. $$ = nod(OCOMPLIT, N, $2);
  814. $$->list = $5;
  815. }
  816. | fnliteral
  817. keyval:
  818. expr ':' complitexpr
  819. {
  820. $$ = nod(OKEY, $1, $3);
  821. }
  822. complitexpr:
  823. expr
  824. | '{' braced_keyval_list '}'
  825. {
  826. $$ = nod(OCOMPLIT, N, N);
  827. $$->list = $2;
  828. }
  829. pexpr:
  830. pexpr_no_paren
  831. | '(' expr_or_type ')'
  832. {
  833. $$ = $2;
  834. }
  835. expr_or_type:
  836. expr
  837. | non_expr_type %prec PreferToRightParen
  838. name_or_type:
  839. ntype
  840. lbrace:
  841. LBODY
  842. {
  843. $$ = LBODY;
  844. }
  845. | '{'
  846. {
  847. $$ = '{';
  848. }
  849. /*
  850. * names and types
  851. * newname is used before declared
  852. * oldname is used after declared
  853. */
  854. new_name:
  855. sym
  856. {
  857. $$ = newname($1);
  858. }
  859. dcl_name:
  860. sym
  861. {
  862. $$ = dclname($1);
  863. }
  864. onew_name:
  865. {
  866. $$ = N;
  867. }
  868. | new_name
  869. sym:
  870. LNAME
  871. name:
  872. sym %prec NotParen
  873. {
  874. $$ = oldname($1);
  875. if($$->pack != N)
  876. $$->pack->used = 1;
  877. }
  878. labelname:
  879. new_name
  880. /*
  881. * to avoid parsing conflicts, type is split into
  882. * channel types
  883. * function types
  884. * parenthesized types
  885. * any other type
  886. * the type system makes additional restrictions,
  887. * but those are not implemented in the grammar.
  888. */
  889. dotdotdot:
  890. LDDD
  891. {
  892. yyerror("final argument in variadic function missing type");
  893. $$ = nod(ODDD, typenod(typ(TINTER)), N);
  894. }
  895. | LDDD ntype
  896. {
  897. $$ = nod(ODDD, $2, N);
  898. }
  899. ntype:
  900. recvchantype
  901. | fntype
  902. | othertype
  903. | ptrtype
  904. | dotname
  905. | '(' ntype ')'
  906. {
  907. $$ = nod(OTPAREN, $2, N);
  908. }
  909. non_expr_type:
  910. recvchantype
  911. | fntype
  912. | othertype
  913. | '*' non_expr_type
  914. {
  915. $$ = nod(OIND, $2, N);
  916. }
  917. non_recvchantype:
  918. fntype
  919. | othertype
  920. | ptrtype
  921. | dotname
  922. | '(' ntype ')'
  923. {
  924. $$ = nod(OTPAREN, $2, N);
  925. }
  926. convtype:
  927. fntype
  928. | othertype
  929. comptype:
  930. othertype
  931. fnret_type:
  932. recvchantype
  933. | fntype
  934. | othertype
  935. | ptrtype
  936. | dotname
  937. dotname:
  938. name
  939. | name '.' sym
  940. {
  941. if($1->op == OPACK) {
  942. Sym *s;
  943. s = restrictlookup($3->name, $1->pkg);
  944. $1->used = 1;
  945. $$ = oldname(s);
  946. break;
  947. }
  948. $$ = nod(OXDOT, $1, newname($3));
  949. }
  950. othertype:
  951. '[' oexpr ']' ntype
  952. {
  953. $$ = nod(OTARRAY, $2, $4);
  954. }
  955. | '[' LDDD ']' ntype
  956. {
  957. // array literal of nelem
  958. $$ = nod(OTARRAY, nod(ODDD, N, N), $4);
  959. }
  960. | LCHAN non_recvchantype
  961. {
  962. $$ = nod(OTCHAN, $2, N);
  963. $$->etype = Cboth;
  964. }
  965. | LCHAN LCOMM ntype
  966. {
  967. $$ = nod(OTCHAN, $3, N);
  968. $$->etype = Csend;
  969. }
  970. | LMAP '[' ntype ']' ntype
  971. {
  972. $$ = nod(OTMAP, $3, $5);
  973. }
  974. | structtype
  975. | interfacetype
  976. ptrtype:
  977. '*' ntype
  978. {
  979. $$ = nod(OIND, $2, N);
  980. }
  981. recvchantype:
  982. LCOMM LCHAN ntype
  983. {
  984. $$ = nod(OTCHAN, $3, N);
  985. $$->etype = Crecv;
  986. }
  987. structtype:
  988. LSTRUCT lbrace structdcl_list osemi '}'
  989. {
  990. $$ = nod(OTSTRUCT, N, N);
  991. $$->list = $3;
  992. fixlbrace($2);
  993. }
  994. | LSTRUCT lbrace '}'
  995. {
  996. $$ = nod(OTSTRUCT, N, N);
  997. fixlbrace($2);
  998. }
  999. interfacetype:
  1000. LINTERFACE lbrace interfacedcl_list osemi '}'
  1001. {
  1002. $$ = nod(OTINTER, N, N);
  1003. $$->list = $3;
  1004. fixlbrace($2);
  1005. }
  1006. | LINTERFACE lbrace '}'
  1007. {
  1008. $$ = nod(OTINTER, N, N);
  1009. fixlbrace($2);
  1010. }
  1011. /*
  1012. * function stuff
  1013. * all in one place to show how crappy it all is
  1014. */
  1015. xfndcl:
  1016. LFUNC fndcl fnbody
  1017. {
  1018. $$ = $2;
  1019. if($$ == N)
  1020. break;
  1021. $$->nbody = $3;
  1022. $$->endlineno = lineno;
  1023. funcbody($$);
  1024. }
  1025. fndcl:
  1026. dcl_name '(' oarg_type_list_ocomma ')' fnres
  1027. {
  1028. Node *n;
  1029. $3 = checkarglist($3, 1);
  1030. $$ = nod(ODCLFUNC, N, N);
  1031. $$->nname = $1;
  1032. n = nod(OTFUNC, N, N);
  1033. n->list = $3;
  1034. n->rlist = $5;
  1035. if(strcmp($1->sym->name, "init") == 0) {
  1036. $$->nname = renameinit($1);
  1037. if($3 != nil || $5 != nil)
  1038. yyerror("func init must have no arguments and no return values");
  1039. }
  1040. if(strcmp(localpkg->name, "main") == 0 && strcmp($1->sym->name, "main") == 0) {
  1041. if($3 != nil || $5 != nil)
  1042. yyerror("func main must have no arguments and no return values");
  1043. }
  1044. // TODO: check if nname already has an ntype
  1045. $$->nname->ntype = n;
  1046. funchdr($$);
  1047. }
  1048. | '(' oarg_type_list_ocomma ')' sym '(' oarg_type_list_ocomma ')' fnres
  1049. {
  1050. Node *rcvr, *t;
  1051. Node *name;
  1052. name = newname($4);
  1053. $2 = checkarglist($2, 0);
  1054. $6 = checkarglist($6, 1);
  1055. $$ = N;
  1056. if($2 == nil) {
  1057. yyerror("method has no receiver");
  1058. break;
  1059. }
  1060. if($2->next != nil) {
  1061. yyerror("method has multiple receivers");
  1062. break;
  1063. }
  1064. rcvr = $2->n;
  1065. if(rcvr->op != ODCLFIELD) {
  1066. yyerror("bad receiver in method");
  1067. break;
  1068. }
  1069. if(rcvr->right->op == OTPAREN || (rcvr->right->op == OIND && rcvr->right->left->op == OTPAREN))
  1070. yyerror("cannot parenthesize receiver type");
  1071. $$ = nod(ODCLFUNC, N, N);
  1072. $$->nname = methodname1(name, rcvr->right);
  1073. t = nod(OTFUNC, rcvr, N);
  1074. t->list = $6;
  1075. t->rlist = $8;
  1076. $$->nname->ntype = t;
  1077. $$->shortname = name;
  1078. funchdr($$);
  1079. }
  1080. fntype:
  1081. LFUNC '(' oarg_type_list_ocomma ')' fnres
  1082. {
  1083. $3 = checkarglist($3, 1);
  1084. $$ = nod(OTFUNC, N, N);
  1085. $$->list = $3;
  1086. $$->rlist = $5;
  1087. }
  1088. fnbody:
  1089. {
  1090. $$ = nil;
  1091. }
  1092. | '{' stmt_list '}'
  1093. {
  1094. $$ = $2;
  1095. if($$ == nil)
  1096. $$ = list1(nod(OEMPTY, N, N));
  1097. }
  1098. fnres:
  1099. %prec NotParen
  1100. {
  1101. $$ = nil;
  1102. }
  1103. | fnret_type
  1104. {
  1105. $$ = list1(nod(ODCLFIELD, N, $1));
  1106. }
  1107. | '(' oarg_type_list_ocomma ')'
  1108. {
  1109. $2 = checkarglist($2, 0);
  1110. $$ = $2;
  1111. }
  1112. fnlitdcl:
  1113. fntype
  1114. {
  1115. closurehdr($1);
  1116. }
  1117. fnliteral:
  1118. fnlitdcl '{' stmt_list '}'
  1119. {
  1120. $$ = closurebody($3);
  1121. }
  1122. /*
  1123. * lists of things
  1124. * note that they are left recursive
  1125. * to conserve yacc stack. they need to
  1126. * be reversed to interpret correctly
  1127. */
  1128. xdcl_list:
  1129. {
  1130. $$ = nil;
  1131. }
  1132. | xdcl_list xdcl ';'
  1133. {
  1134. $$ = concat($1, $2);
  1135. if(nsyntaxerrors == 0)
  1136. testdclstack();
  1137. }
  1138. vardcl_list:
  1139. vardcl
  1140. | vardcl_list ';' vardcl
  1141. {
  1142. $$ = concat($1, $3);
  1143. }
  1144. constdcl_list:
  1145. constdcl1
  1146. | constdcl_list ';' constdcl1
  1147. {
  1148. $$ = concat($1, $3);
  1149. }
  1150. typedcl_list:
  1151. typedcl
  1152. {
  1153. $$ = list1($1);
  1154. }
  1155. | typedcl_list ';' typedcl
  1156. {
  1157. $$ = list($1, $3);
  1158. }
  1159. structdcl_list:
  1160. structdcl
  1161. | structdcl_list ';' structdcl
  1162. {
  1163. $$ = concat($1, $3);
  1164. }
  1165. interfacedcl_list:
  1166. interfacedcl
  1167. {
  1168. $$ = list1($1);
  1169. }
  1170. | interfacedcl_list ';' interfacedcl
  1171. {
  1172. $$ = list($1, $3);
  1173. }
  1174. structdcl:
  1175. new_name_list ntype oliteral
  1176. {
  1177. NodeList *l;
  1178. for(l=$1; l; l=l->next) {
  1179. l->n = nod(ODCLFIELD, l->n, $2);
  1180. l->n->val = $3;
  1181. }
  1182. }
  1183. | embed oliteral
  1184. {
  1185. $1->val = $2;
  1186. $$ = list1($1);
  1187. }
  1188. | '(' embed ')' oliteral
  1189. {
  1190. $2->val = $4;
  1191. $$ = list1($2);
  1192. yyerror("cannot parenthesize embedded type");
  1193. }
  1194. | '*' embed oliteral
  1195. {
  1196. $2->right = nod(OIND, $2->right, N);
  1197. $2->val = $3;
  1198. $$ = list1($2);
  1199. }
  1200. | '(' '*' embed ')' oliteral
  1201. {
  1202. $3->right = nod(OIND, $3->right, N);
  1203. $3->val = $5;
  1204. $$ = list1($3);
  1205. yyerror("cannot parenthesize embedded type");
  1206. }
  1207. | '*' '(' embed ')' oliteral
  1208. {
  1209. $3->right = nod(OIND, $3->right, N);
  1210. $3->val = $5;
  1211. $$ = list1($3);
  1212. yyerror("cannot parenthesize embedded type");
  1213. }
  1214. packname:
  1215. LNAME
  1216. {
  1217. Node *n;
  1218. $$ = $1;
  1219. n = oldname($1);
  1220. if(n->pack != N)
  1221. n->pack->used = 1;
  1222. }
  1223. | LNAME '.' sym
  1224. {
  1225. Pkg *pkg;
  1226. if($1->def == N || $1->def->op != OPACK) {
  1227. yyerror("%S is not a package", $1);
  1228. pkg = localpkg;
  1229. } else {
  1230. $1->def->used = 1;
  1231. pkg = $1->def->pkg;
  1232. }
  1233. $$ = restrictlookup($3->name, pkg);
  1234. }
  1235. embed:
  1236. packname
  1237. {
  1238. $$ = embedded($1);
  1239. }
  1240. interfacedcl:
  1241. new_name indcl
  1242. {
  1243. $$ = nod(ODCLFIELD, $1, $2);
  1244. }
  1245. | packname
  1246. {
  1247. $$ = nod(ODCLFIELD, N, oldname($1));
  1248. }
  1249. | '(' packname ')'
  1250. {
  1251. $$ = nod(ODCLFIELD, N, oldname($2));
  1252. yyerror("cannot parenthesize embedded type");
  1253. }
  1254. indcl:
  1255. '(' oarg_type_list_ocomma ')' fnres
  1256. {
  1257. // without func keyword
  1258. $2 = checkarglist($2, 1);
  1259. $$ = nod(OTFUNC, fakethis(), N);
  1260. $$->list = $2;
  1261. $$->rlist = $4;
  1262. }
  1263. /*
  1264. * function arguments.
  1265. */
  1266. arg_type:
  1267. name_or_type
  1268. | sym name_or_type
  1269. {
  1270. $$ = nod(ONONAME, N, N);
  1271. $$->sym = $1;
  1272. $$ = nod(OKEY, $$, $2);
  1273. }
  1274. | sym dotdotdot
  1275. {
  1276. $$ = nod(ONONAME, N, N);
  1277. $$->sym = $1;
  1278. $$ = nod(OKEY, $$, $2);
  1279. }
  1280. | dotdotdot
  1281. arg_type_list:
  1282. arg_type
  1283. {
  1284. $$ = list1($1);
  1285. }
  1286. | arg_type_list ',' arg_type
  1287. {
  1288. $$ = list($1, $3);
  1289. }
  1290. oarg_type_list_ocomma:
  1291. {
  1292. $$ = nil;
  1293. }
  1294. | arg_type_list ocomma
  1295. {
  1296. $$ = $1;
  1297. }
  1298. /*
  1299. * statement
  1300. */
  1301. stmt:
  1302. {
  1303. $$ = N;
  1304. }
  1305. | compound_stmt
  1306. | common_dcl
  1307. {
  1308. $$ = liststmt($1);
  1309. }
  1310. | non_dcl_stmt
  1311. | error
  1312. {
  1313. $$ = N;
  1314. }
  1315. non_dcl_stmt:
  1316. simple_stmt
  1317. | for_stmt
  1318. | switch_stmt
  1319. | select_stmt
  1320. | if_stmt
  1321. {
  1322. popdcl();
  1323. $$ = $1;
  1324. }
  1325. | if_stmt LELSE stmt
  1326. {
  1327. popdcl();
  1328. $$ = $1;
  1329. $$->nelse = list1($3);
  1330. }
  1331. | labelname ':' stmt
  1332. {
  1333. NodeList *l;
  1334. l = list1(nod(OLABEL, $1, $3));
  1335. if($3)
  1336. l = list(l, $3);
  1337. $$ = liststmt(l);
  1338. }
  1339. | LFALL
  1340. {
  1341. // will be converted to OFALL
  1342. $$ = nod(OXFALL, N, N);
  1343. }
  1344. | LBREAK onew_name
  1345. {
  1346. $$ = nod(OBREAK, $2, N);
  1347. }
  1348. | LCONTINUE onew_name
  1349. {
  1350. $$ = nod(OCONTINUE, $2, N);
  1351. }
  1352. | LGO pseudocall
  1353. {
  1354. $$ = nod(OPROC, $2, N);
  1355. }
  1356. | LDEFER pseudocall
  1357. {
  1358. $$ = nod(ODEFER, $2, N);
  1359. }
  1360. | LGOTO new_name
  1361. {
  1362. $$ = nod(OGOTO, $2, N);
  1363. }
  1364. | LRETURN oexpr_list
  1365. {
  1366. $$ = nod(ORETURN, N, N);
  1367. $$->list = $2;
  1368. }
  1369. stmt_list:
  1370. stmt
  1371. {
  1372. $$ = nil;
  1373. if($1 != N)
  1374. $$ = list1($1);
  1375. }
  1376. | stmt_list ';' stmt
  1377. {
  1378. $$ = $1;
  1379. if($3 != N)
  1380. $$ = list($$, $3);
  1381. }
  1382. new_name_list:
  1383. new_name
  1384. {
  1385. $$ = list1($1);
  1386. }
  1387. | new_name_list ',' new_name
  1388. {
  1389. $$ = list($1, $3);
  1390. }
  1391. dcl_name_list:
  1392. dcl_name
  1393. {
  1394. $$ = list1($1);
  1395. }
  1396. | dcl_name_list ',' dcl_name
  1397. {
  1398. $$ = list($1, $3);
  1399. }
  1400. expr_list:
  1401. expr
  1402. {
  1403. $$ = list1($1);
  1404. }
  1405. | expr_list ',' expr
  1406. {
  1407. $$ = list($1, $3);
  1408. }
  1409. expr_or_type_list:
  1410. expr_or_type
  1411. {
  1412. $$ = list1($1);
  1413. }
  1414. | expr_or_type_list ',' expr_or_type
  1415. {
  1416. $$ = list($1, $3);
  1417. }
  1418. /*
  1419. * list of combo of keyval and val
  1420. */
  1421. keyval_list:
  1422. keyval
  1423. {
  1424. $$ = list1($1);
  1425. }
  1426. | complitexpr
  1427. {
  1428. $$ = list1($1);
  1429. }
  1430. | keyval_list ',' keyval
  1431. {
  1432. $$ = list($1, $3);
  1433. }
  1434. | keyval_list ',' complitexpr
  1435. {
  1436. $$ = list($1, $3);
  1437. }
  1438. braced_keyval_list:
  1439. {
  1440. $$ = nil;
  1441. }
  1442. | keyval_list ocomma
  1443. {
  1444. $$ = $1;
  1445. }
  1446. /*
  1447. * optional things
  1448. */
  1449. osemi:
  1450. | ';'
  1451. ocomma:
  1452. | ','
  1453. oexpr:
  1454. {
  1455. $$ = N;
  1456. }
  1457. | expr
  1458. oexpr_list:
  1459. {
  1460. $$ = nil;
  1461. }
  1462. | expr_list
  1463. osimple_stmt:
  1464. {
  1465. $$ = N;
  1466. }
  1467. | simple_stmt
  1468. ohidden_funarg_list:
  1469. {
  1470. $$ = nil;
  1471. }
  1472. | hidden_funarg_list
  1473. ohidden_structdcl_list:
  1474. {
  1475. $$ = nil;
  1476. }
  1477. | hidden_structdcl_list
  1478. ohidden_interfacedcl_list:
  1479. {
  1480. $$ = nil;
  1481. }
  1482. | hidden_interfacedcl_list
  1483. oliteral:
  1484. {
  1485. $$.ctype = CTxxx;
  1486. }
  1487. | LLITERAL
  1488. /*
  1489. * import syntax from header of
  1490. * an output package
  1491. */
  1492. hidden_import:
  1493. LIMPORT sym LLITERAL ';'
  1494. {
  1495. // Informational: record package name
  1496. // associated with import path, for use in
  1497. // human-readable messages.
  1498. Pkg *p;
  1499. p = mkpkg($3.u.sval);
  1500. p->name = $2->name;
  1501. }
  1502. | LVAR hidden_pkg_importsym hidden_type ';'
  1503. {
  1504. importvar($2, $3, PEXTERN);
  1505. }
  1506. | LCONST hidden_pkg_importsym '=' hidden_constant ';'
  1507. {
  1508. importconst($2, types[TIDEAL], $4);
  1509. }
  1510. | LCONST hidden_pkg_importsym hidden_type '=' hidden_constant ';'
  1511. {
  1512. importconst($2, $3, $5);
  1513. }
  1514. | LTYPE hidden_pkgtype hidden_type ';'
  1515. {
  1516. importtype($2, $3);
  1517. }
  1518. | LFUNC hidden_pkg_importsym '(' ohidden_funarg_list ')' ohidden_funres ';'
  1519. {
  1520. importvar($2, functype(N, $4, $6), PFUNC);
  1521. }
  1522. | LFUNC '(' hidden_funarg_list ')' sym '(' ohidden_funarg_list ')' ohidden_funres ';'
  1523. {
  1524. if($3->next != nil || $3->n->op != ODCLFIELD) {
  1525. yyerror("bad receiver in method");
  1526. YYERROR;
  1527. }
  1528. importmethod($5, functype($3->n, $7, $9));
  1529. }
  1530. hidden_pkgtype:
  1531. hidden_pkg_importsym
  1532. {
  1533. $$ = pkgtype($1);
  1534. importsym($1, OTYPE);
  1535. }
  1536. hidden_type:
  1537. hidden_type_misc
  1538. | hidden_type_recv_chan
  1539. | hidden_type_func
  1540. hidden_type_non_recv_chan:
  1541. hidden_type_misc
  1542. | hidden_type_func
  1543. hidden_type_misc:
  1544. hidden_importsym
  1545. {
  1546. $$ = pkgtype($1);
  1547. }
  1548. | LNAME
  1549. {
  1550. // predefined name like uint8
  1551. $1 = pkglookup($1->name, builtinpkg);
  1552. if($1->def == N || $1->def->op != OTYPE) {
  1553. yyerror("%s is not a type", $1->name);
  1554. $$ = T;
  1555. } else
  1556. $$ = $1->def->type;
  1557. }
  1558. | '[' ']' hidden_type
  1559. {
  1560. $$ = aindex(N, $3);
  1561. }
  1562. | '[' LLITERAL ']' hidden_type
  1563. {
  1564. $$ = aindex(nodlit($2), $4);
  1565. }
  1566. | LMAP '[' hidden_type ']' hidden_type
  1567. {
  1568. $$ = maptype($3, $5);
  1569. }
  1570. | LSTRUCT '{' ohidden_structdcl_list '}'
  1571. {
  1572. $$ = dostruct($3, TSTRUCT);
  1573. }
  1574. | LINTERFACE '{' ohidden_interfacedcl_list '}'
  1575. {
  1576. $$ = dostruct($3, TINTER);
  1577. }
  1578. | '*' hidden_type
  1579. {
  1580. $$ = ptrto($2);
  1581. }
  1582. | LCHAN hidden_type_non_recv_chan
  1583. {
  1584. $$ = typ(TCHAN);
  1585. $$->type = $2;
  1586. $$->chan = Cboth;
  1587. }
  1588. | LCHAN '(' hidden_type_recv_chan ')'
  1589. {
  1590. $$ = typ(TCHAN);
  1591. $$->type = $3;
  1592. $$->chan = Cboth;
  1593. }
  1594. | LCHAN LCOMM hidden_type
  1595. {
  1596. $$ = typ(TCHAN);
  1597. $$->type = $3;
  1598. $$->chan = Csend;
  1599. }
  1600. hidden_type_recv_chan:
  1601. LCOMM LCHAN hidden_type
  1602. {
  1603. $$ = typ(TCHAN);
  1604. $$->type = $3;
  1605. $$->chan = Crecv;
  1606. }
  1607. hidden_type_func:
  1608. LFUNC '(' ohidden_funarg_list ')' ohidden_funres
  1609. {
  1610. $$ = functype(nil, $3, $5);
  1611. }
  1612. hidden_opt_sym:
  1613. sym
  1614. {
  1615. $$ = newname($1);
  1616. }
  1617. | '?'
  1618. {
  1619. $$ = N;
  1620. }
  1621. hidden_dcl:
  1622. hidden_opt_sym hidden_type
  1623. {
  1624. $$ = nod(ODCLFIELD, $1, typenod($2));
  1625. }
  1626. | hidden_opt_sym LDDD
  1627. {
  1628. Type *t;
  1629. yyerror("invalid variadic function type in import - recompile import");
  1630. t = typ(TARRAY);
  1631. t->bound = -1;
  1632. t->type = typ(TINTER);
  1633. $$ = nod(ODCLFIELD, $1, typenod(t));
  1634. $$->isddd = 1;
  1635. }
  1636. | hidden_opt_sym LDDD hidden_type
  1637. {
  1638. Type *t;
  1639. t = typ(TARRAY);
  1640. t->bound = -1;
  1641. t->type = $3;
  1642. $$ = nod(ODCLFIELD, $1, typenod(t));
  1643. $$->isddd = 1;
  1644. }
  1645. hidden_structdcl:
  1646. sym hidden_type hidden_tag
  1647. {
  1648. $$ = nod(ODCLFIELD, newname($1), typenod($2));
  1649. $$->val = $3;
  1650. }
  1651. | '?' hidden_type hidden_tag
  1652. {
  1653. Sym *s;
  1654. s = $2->sym;
  1655. if(s == S && isptr[$2->etype])
  1656. s = $2->type->sym;
  1657. if(s && s->pkg == builtinpkg)
  1658. s = lookup(s->name);
  1659. $$ = embedded(s);
  1660. $$->right = typenod($2);
  1661. $$->val = $3;
  1662. }
  1663. hidden_tag:
  1664. {
  1665. $$.ctype = CTxxx;
  1666. }
  1667. | ':' LLITERAL // extra colon avoids conflict with "" looking like beginning of "".typename
  1668. {
  1669. $$ = $2;
  1670. }
  1671. hidden_interfacedcl:
  1672. sym '(' ohidden_funarg_list ')' ohidden_funres
  1673. {
  1674. $$ = nod(ODCLFIELD, newname($1), typenod(functype(fakethis(), $3, $5)));
  1675. }
  1676. ohidden_funres:
  1677. {
  1678. $$ = nil;
  1679. }
  1680. | hidden_funres
  1681. hidden_funres:
  1682. '(' ohidden_funarg_list ')'
  1683. {
  1684. $$ = $2;
  1685. }
  1686. | hidden_type
  1687. {
  1688. $$ = list1(nod(ODCLFIELD, N, typenod($1)));
  1689. }
  1690. hidden_literal:
  1691. LLITERAL
  1692. {
  1693. $$ = nodlit($1);
  1694. }
  1695. | '-' LLITERAL
  1696. {
  1697. $$ = nodlit($2);
  1698. switch($$->val.ctype){
  1699. case CTINT:
  1700. mpnegfix($$->val.u.xval);
  1701. break;
  1702. case CTFLT:
  1703. mpnegflt($$->val.u.fval);
  1704. break;
  1705. default:
  1706. yyerror("bad negated constant");
  1707. }
  1708. }
  1709. | sym
  1710. {
  1711. $$ = oldname(pkglookup($1->name, builtinpkg));
  1712. if($$->op != OLITERAL)
  1713. yyerror("bad constant %S", $$->sym);
  1714. }
  1715. hidden_constant:
  1716. hidden_literal
  1717. | '(' hidden_literal '+' hidden_literal ')'
  1718. {
  1719. $$ = nodcplxlit($2->val, $4->val);
  1720. }
  1721. hidden_importsym:
  1722. LLITERAL '.' sym
  1723. {
  1724. Pkg *p;
  1725. if($1.u.sval->len == 0)
  1726. p = importpkg;
  1727. else
  1728. p = mkpkg($1.u.sval);
  1729. $$ = pkglookup($3->name, p);
  1730. }
  1731. hidden_pkg_importsym:
  1732. hidden_importsym
  1733. {
  1734. $$ = $1;
  1735. structpkg = $$->pkg;
  1736. }
  1737. hidden_import_list:
  1738. | hidden_import_list hidden_import
  1739. hidden_funarg_list:
  1740. hidden_dcl
  1741. {
  1742. $$ = list1($1);
  1743. }
  1744. | hidden_funarg_list ',' hidden_dcl
  1745. {
  1746. $$ = list($1, $3);
  1747. }
  1748. hidden_structdcl_list:
  1749. hidden_structdcl
  1750. {
  1751. $$ = list1($1);
  1752. }
  1753. | hidden_structdcl_list ';' hidden_structdcl
  1754. {
  1755. $$ = list($1, $3);
  1756. }
  1757. hidden_interfacedcl_list:
  1758. hidden_interfacedcl
  1759. {
  1760. $$ = list1($1);
  1761. }
  1762. | hidden_interfacedcl_list ';' hidden_interfacedcl
  1763. {
  1764. $$ = list($1, $3);
  1765. }
  1766. %%
  1767. static void
  1768. fixlbrace(int lbr)
  1769. {
  1770. // If the opening brace was an LBODY,
  1771. // set up for another one now that we're done.
  1772. // See comment in lex.c about loophack.
  1773. if(lbr == LBODY)
  1774. loophack = 1;
  1775. }