PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cmd/gc/go.y

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