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

/src/cmd/gc/go.y

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