PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/parser/parser.y

https://code.google.com/
Happy | 3233 lines | 2814 code | 419 blank | 0 comment | 0 complexity | 51b75304e15e25bcbee69e801de89474 MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. %{
  2. // Copyright 2012 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. // CAUTION: If this file is 'parser.go', it was generated
  6. // automatically from 'parser.y' - DO NOT EDIT in that case!
  7. /*
  8. Tracks:
  9. -----------------------------------------------
  10. Revision: f1deaf35e1d1
  11. Author: Luuk van Dijk <l...@golang.org>
  12. Date: Jan 17 2012
  13. Branch: default
  14. */
  15. /*TODO
  16. OPACK handling
  17. "release":
  18. - i_, val_, sym_, node_ list_ prefixes?
  19. */
  20. package parser
  21. import (
  22. "fmt"
  23. "go/token"
  24. )
  25. %}
  26. %union{
  27. pos token.Pos
  28. list []*Node
  29. node *Node
  30. sym *Sym
  31. val *Val
  32. closure func()
  33. i int
  34. }
  35. %token <val> val_LLITERAL
  36. %token <i> i_LASOP
  37. %token LBREAK LCASE LCHAN LCOLAS LCONST LCONTINUE LDDD
  38. %token LDEFAULT LDEFER LELSE LFALL LFOR LFUNC LGO LGOTO
  39. %token LIF LIMPORT LINTERFACE LMAP
  40. %token <sym> sym_LNAME
  41. %token LPACKAGE LRANGE LRETURN LSELECT LSTRUCT LSWITCH
  42. %token LTYPE LVAR
  43. %token LANDAND LANDNOT LBODY LCOMM LDEC LEQ LGE LGT
  44. %token LIGNORE LINC LLE LLSH LLT LNE LOROR LRSH
  45. %type <i> i_lbrace i_import_here
  46. %type <sym> sym_sym sym_packname
  47. %type <val> val_oliteral
  48. %type <node> node_stmt node_ntype
  49. %type <node> node_arg_type
  50. %type <node> node_case node_caseblock
  51. %type <node> node_compound_stmt node_dotname node_embed node_expr node_complitexpr
  52. %type <node> node_expr_or_type
  53. %type <node> node_fndcl /*node_hidden_fndcl*/ node_fnliteral
  54. %type <node> node_for_body node_for_header node_for_stmt node_if_header node_if_stmt node_else node_non_dcl_stmt
  55. %type <node> node_interfacedcl node_keyval node_labelname node_name
  56. %type <node> node_name_or_type node_non_expr_type
  57. %type <node> node_new_name node_dcl_name node_oexpr node_typedclname
  58. %type <node> node_onew_name
  59. %type <node> node_osimple_stmt node_pexpr node_pexpr_no_paren
  60. %type <node> node_pseudocall node_range_stmt node_select_stmt
  61. %type <node> node_simple_stmt
  62. %type <node> node_switch_stmt node_uexpr
  63. %type <node> node_xfndcl node_typedcl node_start_complit
  64. %type <node> node_fnlitdcl
  65. %type <list> list_xdcl list_fnbody list_fnres list_loop_body list_dcl_name_list
  66. %type <list> list_new_name_list list_expr_list list_keyval_list list_braced_keyval_list list_expr_or_type_list list_xdcl_list
  67. %type <list> list_oexpr_list list_caseblock_list list_stmt_list list_oarg_type_list_ocomma list_arg_type_list
  68. %type <list> list_interfacedcl_list list_vardcl list_vardcl_list list_structdcl list_structdcl_list
  69. %type <list> list_common_dcl list_constdcl list_constdcl1 list_constdcl_list list_typedcl_list
  70. %type <node> node_convtype node_comptype node_dotdotdot
  71. %type <node> node_indcl node_interfacetype node_structtype node_ptrtype
  72. %type <node> node_recvchantype node_non_recvchantype node_othertype node_fnret_type node_fntype
  73. /*
  74. %type <sym> sym_hidden_importsym sym_hidden_pkg_importsym
  75. %type <node> node_hidden_constant node_hidden_literal node_hidden_funarg
  76. %type <node> node_hidden_interfacedcl node_hidden_structdcl
  77. %type <list> list_hidden_funres
  78. %type <list> list_ohidden_funres
  79. %type <list> list_hidden_funarg_list list_ohidden_funarg_list
  80. %type <list> list_hidden_interfacedcl_list list_ohidden_interfacedcl_list
  81. %type <list> list_hidden_structdcl_list list_ohidden_structdcl_list
  82. %type <type> type_hidden_type type_hidden_type_misc type_hidden_pkgtype
  83. %type <type> type_hidden_type_func
  84. %type <type> type_hidden_type_recv_chan type_hidden_type_non_recv_chan
  85. */
  86. %left LCOMM /* outside the usual hierarchy; here for good error messages */
  87. %left LOROR
  88. %left LANDAND
  89. %left LEQ LNE LLE LGE LLT LGT
  90. %left '+' '-' '|' '^'
  91. %left '*' '/' '%' '&' LLSH LRSH LANDNOT
  92. /*
  93. * manual override of shift/reduce conflicts.
  94. * the general form is that we assign a precedence
  95. * to the token being shifted and then introduce
  96. * NotToken with lower precedence or PreferToToken with higher
  97. * and annotate the reducing rule accordingly.
  98. */
  99. %left NotPackage
  100. %left LPACKAGE
  101. %left NotParen
  102. %left '('
  103. %left ')'
  104. %left PreferToRightParen
  105. %%
  106. file:
  107. /*
  108. loadsys
  109. */
  110. package
  111. imports
  112. list_xdcl_list
  113. {
  114. /*
  115. xtop = concat(xtop, $4);
  116. */
  117. yylex.top = append(yylex.top, $3...)
  118. if !yylex.errored {
  119. s := yylex.scope
  120. if s == nil || s.Tag != FileScope {
  121. internalError("!= FileScope")
  122. }
  123. s = s.Outer
  124. if s == nil || s.Tag != PackageScope {
  125. internalError("!= PackageScope")
  126. }
  127. s = s.Outer
  128. if s == nil || s.Tag != UniverseScope {
  129. internalError("!= UniverseScope")
  130. }
  131. }
  132. for yylex.scope != nil && yylex.scope.Tag != PackageScope {
  133. yylex.scope = yylex.scope.Outer
  134. }
  135. }
  136. package:
  137. %prec NotPackage
  138. {
  139. /*
  140. prevlineno = lineno;
  141. yyerror("package statement must be first");
  142. flusherrors();
  143. mkpackage("main");
  144. */
  145. yylex.Error("package statement must be first")
  146. }
  147. | LPACKAGE sym_sym ';'
  148. {
  149. //mkpackage($2->node_name);
  150. new := string($2.Name)
  151. if new == "_" {
  152. yylex.error($<pos>2, "invalid package name _")
  153. }
  154. if old := yylex.pkg; old != "" && old != new {
  155. yylex.error($<pos>2, "expected package %q got %q", old, new)
  156. }
  157. yylex.pkg = new
  158. }
  159. /*
  160. * this loads the definitions for the low-level runtime functions,
  161. * so that the compiler can generate calls to them,
  162. * but does not make the node_name "runtime" visible as a package.
  163. */
  164. /*
  165. loadsys:
  166. {
  167. importpkg = runtimepkg;
  168. if(debug['A'])
  169. cannedimports("runtime.builtin", "package runtime\n\n$$\n\n");
  170. else
  171. cannedimports("runtime.builtin", runtimeimport);
  172. curio.importsafe = 1;
  173. }
  174. import_package
  175. import_there
  176. {
  177. importpkg = nil;
  178. }
  179. */
  180. imports:
  181. | imports import ';'
  182. import:
  183. LIMPORT import_stmt
  184. | LIMPORT '(' import_stmt_list osemi ')'
  185. | LIMPORT '(' ')'
  186. import_stmt:
  187. i_import_here /*import_package import_there*/
  188. {
  189. /*
  190. Pkg *ipkg;
  191. Sym *my;
  192. Node *pack;
  193. ipkg = importpkg;
  194. my = importmyname;
  195. importpkg = nil;
  196. importmyname = S;
  197. if(my == nil)
  198. my = lookup(ipkg->node_name);
  199. pack = nod(OPACK, N, N);
  200. pack->sym_sym = my;
  201. pack->pkg = ipkg;
  202. pack->lineno = $1;
  203. if(my->node_name[0] == '.') {
  204. importdot(ipkg, pack);
  205. break;
  206. }
  207. if(my->node_name[0] == '_' && my->node_name[1] == '\0')
  208. break;
  209. if(my->def) {
  210. lineno = $1;
  211. redeclare(my, "as imported package node_name");
  212. }
  213. my->def = pack;
  214. my->lastlineno = $1;
  215. my->block = 1; // at top level
  216. */
  217. }
  218. import_stmt_list:
  219. import_stmt
  220. | import_stmt_list ';' import_stmt
  221. i_import_here:
  222. val_LLITERAL
  223. {
  224. /*
  225. // import with original name
  226. $$ = parserline();
  227. importmyname = S;
  228. importfile(&$1, $$);
  229. */
  230. $$ = -1
  231. /*internalError(243)*/ //TODO
  232. }
  233. | sym_sym val_LLITERAL
  234. {
  235. /*
  236. // import with given name
  237. $$ = parserline();
  238. importmyname = $1;
  239. importfile(&$2, $$);
  240. */
  241. $$ = -1
  242. /*internalError(255)*/ //TODO
  243. }
  244. | '.' val_LLITERAL
  245. {
  246. /*
  247. // import into my name space
  248. $$ = parserline();
  249. importmyname = lookup(".");
  250. importfile(&$2, $$);
  251. */
  252. $$ = -1
  253. /*internalError(267)*/ //TODO
  254. }
  255. /*
  256. import_package:
  257. LPACKAGE sym_LNAME import_safety ';'
  258. {
  259. if(importpkg->node_name == nil) {
  260. importpkg->node_name = $2->node_name;
  261. pkglookup($2->node_name, nil)->npkg++;
  262. } else if(strcmp(importpkg->node_name, $2->node_name) != 0)
  263. yyerror("conflicting names %s and %s for package \"%Z\"", importpkg->node_name, $2->node_name, importpkg->path);
  264. importpkg->direct = 1;
  265. if(safemode && !curio.importsafe)
  266. yyerror("cannot import unsafe package \"%Z\"", importpkg->path);
  267. }
  268. import_safety:
  269. | sym_LNAME
  270. {
  271. if(strcmp($1->node_name, "safe") == 0)
  272. curio.importsafe = 1;
  273. }
  274. import_there:
  275. {
  276. defercheckwidth();
  277. }
  278. hidden_import_list '$' '$'
  279. {
  280. resumecheckwidth();
  281. unimportfile();
  282. }
  283. */
  284. /*
  285. * declarations
  286. */
  287. list_xdcl:
  288. {
  289. /*
  290. yyerror("empty top-level declaration");
  291. $$ = nil;
  292. */
  293. $$ = nil
  294. yylex.Error("empty top-level declaration")
  295. }
  296. | list_common_dcl
  297. | node_xfndcl
  298. {
  299. /*
  300. $$ = list1($1);
  301. */
  302. $$ = $1.list()
  303. }
  304. | node_non_dcl_stmt
  305. {
  306. /*
  307. yyerror("non-declaration statement outside function body");
  308. $$ = nil;
  309. */
  310. yylex.error($<pos>1, "non-declaration statement outside function body")
  311. $$ = nil
  312. }
  313. | error
  314. {
  315. /*
  316. $$ = nil;
  317. */
  318. $$ = nil
  319. //TODO yylex.error()?
  320. }
  321. list_common_dcl:
  322. LVAR list_vardcl
  323. {
  324. /*
  325. $$ = $2;
  326. */
  327. $$ = $2
  328. }
  329. | LVAR '(' list_vardcl_list osemi ')'
  330. {
  331. /*
  332. $$ = $3;
  333. */
  334. $$ = $3
  335. }
  336. | LVAR '(' ')'
  337. {
  338. /*
  339. $$ = nil;
  340. */
  341. $$ = nil
  342. }
  343. | lconst list_constdcl
  344. {
  345. /*
  346. $$ = $2;
  347. iota = -100000;
  348. lastconst = nil;
  349. */
  350. $$ = $2
  351. }
  352. | lconst '(' list_constdcl osemi ')'
  353. {
  354. /*
  355. $$ = $3;
  356. iota = -100000;
  357. lastconst = nil;
  358. */
  359. $$ = $3
  360. }
  361. | lconst '(' list_constdcl ';' list_constdcl_list osemi ')'
  362. {
  363. /*
  364. $$ = concat($3, $5);
  365. iota = -100000;
  366. lastconst = nil;
  367. */
  368. $$ = append($3, $5...)
  369. }
  370. | lconst '(' ')'
  371. {
  372. /*
  373. $$ = nil;
  374. iota = -100000;
  375. */
  376. $$ = nil
  377. }
  378. | LTYPE node_typedcl
  379. {
  380. /*
  381. $$ = list1($2);
  382. */
  383. $$ = $2.list()
  384. }
  385. | LTYPE '(' list_typedcl_list osemi ')'
  386. {
  387. /*
  388. $$ = $3;
  389. */
  390. $$ = $3
  391. }
  392. | LTYPE '(' ')'
  393. {
  394. /*
  395. $$ = nil;
  396. */
  397. $$ = nil
  398. }
  399. lconst:
  400. LCONST
  401. {
  402. /*
  403. iota = 0;
  404. */
  405. yylex.iota = 0
  406. }
  407. list_vardcl: //TODO -> <node>?
  408. list_dcl_name_list node_ntype
  409. {
  410. /*
  411. $$ = variter($1, $2, nil);
  412. */
  413. $$ = (&Node{Pos: $<pos>1, Op: ODCL, LL: $1, NL: $2}).list()
  414. }
  415. | list_dcl_name_list node_ntype '=' list_expr_list
  416. {
  417. /*
  418. $$ = variter($1, $2, $4);
  419. */
  420. $$ = (&Node{Pos: $<pos>1, Op: ODCL, LL: $1, NL: $2, LR: $4}).list()
  421. }
  422. | list_dcl_name_list '=' list_expr_list
  423. {
  424. /*
  425. $$ = variter($1, nil, $3);
  426. */
  427. $$ = (&Node{Pos: $<pos>1, Op: ODCL, LL: $1, LR: $3}).list()
  428. }
  429. list_constdcl: //TODO -> <node>?
  430. list_dcl_name_list node_ntype '=' list_expr_list
  431. {
  432. /*
  433. $$ = constiter($1, $2, $4);
  434. */
  435. for _, v := range $1 { //TODO -DRY
  436. v.Iota = yylex.iota
  437. }
  438. yylex.iota++
  439. $$ = (&Node{Pos: $<pos>1, Op: ODCLCONST, LL: $1, NL: $2, LR: $4}).list()
  440. }
  441. | list_dcl_name_list '=' list_expr_list
  442. {
  443. /*
  444. $$ = constiter($1, N, $3);
  445. */
  446. for _, v := range $1 { //TODO -DRY
  447. v.Iota = yylex.iota
  448. }
  449. yylex.iota++
  450. $$ = (&Node{Pos: $<pos>1, Op: ODCLCONST, LL: $1, LR: $3}).list()
  451. }
  452. list_constdcl1:
  453. list_constdcl
  454. | list_dcl_name_list node_ntype
  455. {
  456. /*
  457. $$ = constiter($1, $2, nil);
  458. */
  459. for _, v := range $1 { //TODO -DRY
  460. v.Iota = yylex.iota
  461. }
  462. yylex.iota++
  463. $$ = (&Node{Pos: $<pos>1, Op: ODCLCONST, LL: $1, NL: $2}).list()
  464. yylex.error($<pos>2, "constdcl cannot have type without expr")
  465. }
  466. | list_dcl_name_list
  467. {
  468. /*
  469. $$ = constiter($1, N, nil);
  470. */
  471. for _, v := range $1 { //TODO -DRY
  472. v.Iota = yylex.iota
  473. }
  474. yylex.iota++
  475. $$ = (&Node{Pos: $<pos>1, Op: ODCLCONST, LL: $1}).list()
  476. }
  477. node_typedclname:
  478. sym_sym
  479. {
  480. /*
  481. // different from dclname because the node_name
  482. // becomes visible right here, not at the end
  483. // of the declaration.
  484. $$ = typedcl0($1);
  485. */
  486. $$ = &Node{Pos: $<pos>1, Op: ONAME, Name: $1}
  487. }
  488. node_typedcl:
  489. node_typedclname node_ntype
  490. {
  491. /*
  492. $$ = typedcl1($1, $2, 1);
  493. */
  494. if $2.Op == OTARRAY && $2.NL != nil && $2.NL.Op == ODDD {
  495. yylex.error($2.NL.Pos, "use of [...] array outside of array literal")
  496. }
  497. $$ = &Node{Pos: $<pos>1, Op: ODCLTYPE, NL: $1, NR: $2}
  498. }
  499. node_simple_stmt:
  500. node_expr
  501. /*
  502. {
  503. $$ = $1;
  504. }
  505. */
  506. | node_expr i_LASOP node_expr
  507. {
  508. /*
  509. $$ = nod(OASOP, $1, $3);
  510. $$->etype = $2; // rathole to pass opcode
  511. */
  512. $$ = &Node{Pos: $<pos>1, Op: OASOP, NL: $1, AsOpTok: $2, NR: $3}
  513. }
  514. | list_expr_list '=' list_expr_list
  515. {
  516. /*
  517. if($1->next == nil && $3->next == nil) {
  518. // simple
  519. $$ = nod(OAS, $1->n, $3->n);
  520. break;
  521. }
  522. // multiple
  523. $$ = nod(OAS2, N, N);
  524. $$->list = $1;
  525. $$->rlist = $3;
  526. */
  527. if len($1) == 1 && len($3) == 1 {
  528. $$ = &Node{Pos: $<pos>1, Op: OAS, NL: $1[0], NR: $3[0]}
  529. break
  530. }
  531. $$ = &Node{Pos: $<pos>1, Op: OAS2, LL: $1, LR: $3}
  532. }
  533. | list_expr_list LCOLAS list_expr_list
  534. {
  535. /*
  536. if($3->n->op == OTYPESW) {
  537. $$ = nod(OTYPESW, N, $3->n->right);
  538. if($3->next != nil)
  539. yyerror("node_expr.(type) must be alone in list");
  540. if($1->next != nil)
  541. yyerror("argument count mismatch: %d = %d", count($1), 1);
  542. else if($1->n->op != ONAME && $1->n->op != OTYPE && $1->n->op != ONONAME)
  543. yyerror("invalid variable name %N in type switch", $1->n);
  544. else
  545. $$->left = dclname($1->n->sym_sym); // it's a colas, so must not re-use an oldname.
  546. break;
  547. }
  548. $$ = colas($1, $3);
  549. */
  550. //TODO above!
  551. if n := $3[0]; n.Op == OTYPESW {
  552. $$ = n
  553. if len($3) != 1 {
  554. yylex.error($3[1].Pos, "node_expr.(type) must be alone in list")
  555. }
  556. if n := len($1); n > 1 {
  557. yylex.error($<pos>1, "argument count mismatch: %d = %d", n, 1)
  558. }
  559. if n := $1[0]; n.Op != ONAME {
  560. yylex.error($<pos>1, "invalid variable name %s in type switch", n);
  561. }
  562. break
  563. }
  564. $$ = yylex.colas($1, $3)
  565. }
  566. | node_expr LINC
  567. {
  568. /*
  569. $$ = nod(OASOP, $1, nodintconst(1));
  570. $$->etype = OADD;
  571. */
  572. $$ = &Node{Pos: $<pos>1, Op: OINC, NL: $1}
  573. }
  574. | node_expr LDEC
  575. {
  576. /*
  577. $$ = nod(OASOP, $1, nodintconst(1));
  578. $$->etype = OSUB;
  579. */
  580. $$ = &Node{Pos: $<pos>1, Op: ODEC, NL: $1}
  581. }
  582. node_case:
  583. LCASE list_expr_or_type_list ':'
  584. {
  585. /*
  586. Node *n, *nn;
  587. // will be converted to OCASE
  588. // right will point to next node_case
  589. // done in casebody()
  590. markdcl();
  591. $$ = nod(OXCASE, N, N);
  592. $$->list = $2;
  593. if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) {
  594. // type switch - declare variable
  595. nn = newname(n->sym_sym);
  596. declare(nn, dclcontext);
  597. $$->nname = nn;
  598. // keep track of the instances for reporting unused
  599. nn->defn = typesw->right;
  600. }
  601. */
  602. yylex.newScope(OtherScope)
  603. $$ = &Node{Pos: $<pos>1, Op: OCASE, LL: $2}
  604. }
  605. | LCASE list_expr_or_type_list '=' node_expr ':'
  606. {
  607. /*
  608. Node *n;
  609. // will be converted to OCASE
  610. // right will point to next node_case
  611. // done in casebody()
  612. markdcl();
  613. $$ = nod(OXCASE, N, N);
  614. if($2->next == nil)
  615. n = nod(OAS, $2->n, $4);
  616. else {
  617. n = nod(OAS2, N, N);
  618. n->list = $2;
  619. n->rlist = list1($4);
  620. }
  621. $$->list = list1(n);
  622. */
  623. yylex.newScope(OtherScope)
  624. $$ = &Node{Pos: $<pos>1, Op: OCASE, LL: $2, NL: $4}
  625. if len($2) == 1 {
  626. $$.NL = &Node{Pos: $<pos>2, Op: OAS, NL: $2[0], NR: $4}
  627. break
  628. }
  629. $$.NL = &Node{Pos: $<pos>2, Op: OAS, LL: $2, LR: $4.list()}
  630. }
  631. | LCASE list_expr_or_type_list LCOLAS node_expr ':'
  632. {
  633. /*
  634. // will be converted to OCASE
  635. // right will point to next node_case
  636. // done in casebody()
  637. markdcl();
  638. $$ = nod(OXCASE, N, N);
  639. $$->list = list1(colas($2, list1($4)));
  640. */
  641. yylex.newScope(OtherScope)
  642. $$ = &Node{Pos: $<pos>1, Op: OCASE}
  643. $$.NL = yylex.colas($2, $4.list())
  644. }
  645. | LDEFAULT ':'
  646. {
  647. /*
  648. Node *n, *nn;
  649. markdcl();
  650. $$ = nod(OXCASE, N, N);
  651. if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) {
  652. // type switch - declare variable
  653. nn = newname(n->sym_sym);
  654. declare(nn, dclcontext);
  655. $$->nname = nn;
  656. // keep track of the instances for reporting unused
  657. nn->defn = typesw->right;
  658. }
  659. */
  660. yylex.newScope(OtherScope)
  661. $$ = &Node{Pos: $<pos>1, Op: OCASE}
  662. }
  663. node_compound_stmt:
  664. '{'
  665. {
  666. /*
  667. markdcl();
  668. */
  669. yylex.newScope(OtherScope)
  670. }
  671. list_stmt_list '}'
  672. {
  673. /*
  674. $$ = liststmt($3);
  675. popdcl();
  676. */
  677. $$ = &Node{Pos: $<pos>1, Op: OBLOCK, LL: $3}
  678. yylex.popScope()
  679. }
  680. node_caseblock:
  681. node_case
  682. {
  683. /*
  684. // If the last token read by the lexer was consumed
  685. // as part of the node_case, clear it (parser has cleared yychar).
  686. // If the last token read by the lexer was the lookahead
  687. // leave it alone (parser has it cached in yychar).
  688. // This is so that the list_stmt_list action doesn't look at
  689. // the node_case tokens if the list_stmt_list is empty.
  690. yylast = yychar;
  691. */
  692. yylex.last = yychar
  693. }
  694. list_stmt_list
  695. {
  696. /*
  697. int last;
  698. // This is the only place in the language where a statement
  699. // list is not allowed to drop the final semicolon, because
  700. // it's the only place where a statement list is not followed
  701. // by a closing brace. Handle the error for pedantry.
  702. // Find the final token of the statement list.
  703. // yylast is lookahead; yyprev is last of list_stmt_list
  704. last = yyprev;
  705. if(last > 0 && last != ';' && yychar != '}')
  706. yyerror("missing statement after label");
  707. $$ = $1;
  708. $$->nbody = $3;
  709. popdcl();
  710. */
  711. if yylex.prev > 0 && yylex.prev != ';' && yylex.last != '}' { //e.g. test/fixedbugs/bug274.go
  712. yylex.error($<pos>3, "missing statement after label")
  713. }
  714. $$ = $1
  715. $$.LR = $3
  716. yylex.popScope()
  717. }
  718. list_caseblock_list:
  719. {
  720. /*
  721. $$ = nil;
  722. */
  723. $$ = nil
  724. }
  725. | list_caseblock_list node_caseblock
  726. {
  727. /*
  728. $$ = list($1, $2);
  729. */
  730. $$ = append($1, $2)
  731. }
  732. list_loop_body:
  733. LBODY
  734. {
  735. /*
  736. markdcl();
  737. */
  738. yylex.newScope(OtherScope)
  739. }
  740. list_stmt_list '}'
  741. {
  742. /*
  743. $$ = $3;
  744. popdcl();
  745. */
  746. $$ = $3
  747. yylex.popScope()
  748. }
  749. node_range_stmt:
  750. list_expr_list '=' LRANGE node_expr
  751. {
  752. /*
  753. $$ = nod(ORANGE, N, $4);
  754. $$->list = $1;
  755. $$->etype = 0; // := flag
  756. */
  757. $$ = &Node{Pos: $<pos>1, Op: ORANGE, LL: $1, NL: $4}
  758. }
  759. | list_expr_list LCOLAS LRANGE node_expr
  760. {
  761. /*
  762. $$ = nod(ORANGE, N, $4);
  763. $$->list = $1;
  764. $$->colas = 1;
  765. colasdefn($1, $$);
  766. */
  767. $$ = &Node{Pos: $<pos>1, Op: ORANGE, LL: $1, NL: $4, IsColas: true}
  768. yylex.colasdefn($1)
  769. }
  770. node_for_header:
  771. node_osimple_stmt ';' node_osimple_stmt ';' node_osimple_stmt
  772. {
  773. /*
  774. // init ; test ; incr
  775. if($5 != N && $5->colas != 0)
  776. yyerror("cannot declare in the for-increment");
  777. $$ = nod(OFOR, N, N);
  778. if($1 != N)
  779. $$->ninit = list1($1);
  780. $$->ntest = $3;
  781. $$->nincr = $5;
  782. */
  783. //TODO err check above
  784. $$ = &Node{Pos: $<pos>1, Op: OFOR, NL: $1, NR: $3, N3: $5}
  785. }
  786. | node_osimple_stmt
  787. {
  788. /*
  789. // normal test
  790. $$ = nod(OFOR, N, N);
  791. $$->ntest = $1;
  792. */
  793. $$ = &Node{Pos: $<pos>1, Op: OFOR, NR: $1} // <pos> invalid if $1 == nil
  794. }
  795. | node_range_stmt
  796. | LVAR node_osimple_stmt ';' node_osimple_stmt ';' node_osimple_stmt
  797. {
  798. yylex.error($<pos>1, "syntax error: var declaration not allowed in for initializer")
  799. $$ = &Node{Pos: $<pos>1, Op: OFOR, NL: $2, NR: $4, N3: $6}
  800. }
  801. node_for_body:
  802. node_for_header list_loop_body
  803. {
  804. /*
  805. $$ = $1;
  806. $$->nbody = concat($$->nbody, $2);
  807. */
  808. $$ = $1
  809. $$.LR = $2
  810. }
  811. node_for_stmt:
  812. LFOR
  813. {
  814. /*
  815. markdcl();
  816. */
  817. yylex.newScope(OtherScope)
  818. }
  819. node_for_body
  820. {
  821. /*
  822. $$ = $3;
  823. popdcl();
  824. */
  825. $$ = $3
  826. if $$.NR == nil {
  827. $$.Pos = $<pos>1 // correct Pos, see node_for_header
  828. }
  829. yylex.popScope()
  830. }
  831. node_if_header:
  832. node_osimple_stmt
  833. {
  834. /*
  835. // test
  836. $$ = nod(OIF, N, N);
  837. $$->ntest = $1;
  838. */
  839. $$ = &Node{Pos: $<pos>1, Op: OIF, NR: $1} // <pos> invalid if $1 == nil
  840. }
  841. | node_osimple_stmt ';' node_osimple_stmt
  842. {
  843. /*
  844. // init ; test
  845. $$ = nod(OIF, N, N);
  846. if($1 != N)
  847. $$->ninit = list1($1);
  848. $$->ntest = $3;
  849. */
  850. $$ = &Node{Pos: $<pos>1, Op: OIF, NL: $1, NR: $3}
  851. }
  852. /* IF cond body (ELSE IF cond body)* (ELSE block)? */
  853. node_if_stmt:
  854. LIF
  855. {
  856. /*
  857. markdcl();
  858. */
  859. yylex.newScope(OtherScope)
  860. }
  861. node_if_header
  862. {
  863. /*
  864. if($3->ntest == N)
  865. yyerror("missing condition in if statement");
  866. */
  867. if $3.NR == nil {
  868. yylex.error($<pos>1, "missing condition in if statement")
  869. }
  870. }
  871. list_loop_body
  872. {
  873. /*
  874. $3->nbody = $5;
  875. */
  876. $3.LL = $5
  877. }
  878. node_else
  879. {
  880. /*
  881. popdcl();
  882. $$ = $3;
  883. if($7 != N)
  884. $$->nelse = list1($7);
  885. */
  886. yylex.popScope()
  887. $$ = $3
  888. if $$.NR == nil { // see node_if_header
  889. $$.Pos = $<pos>1
  890. }
  891. $$.N3 = $7
  892. }
  893. node_else:
  894. {
  895. /*
  896. $$ = N;
  897. */
  898. $$ = nil
  899. }
  900. | LELSE node_if_stmt
  901. {
  902. /*
  903. $$ = $2;
  904. */
  905. $$ = $2
  906. }
  907. | LELSE node_compound_stmt
  908. {
  909. /*
  910. $$ = $2;
  911. */
  912. $$ = $2
  913. }
  914. node_switch_stmt:
  915. LSWITCH
  916. {
  917. /*
  918. markdcl();
  919. */
  920. yylex.newScope(OtherScope)
  921. }
  922. node_if_header
  923. {
  924. /*
  925. Node *n;
  926. n = $3->ntest;
  927. if(n != N && n->op != OTYPESW)
  928. n = N;
  929. typesw = nod(OXXX, typesw, n);
  930. */
  931. //TODO above?
  932. }
  933. LBODY list_caseblock_list '}'
  934. {
  935. /*
  936. $$ = $3;
  937. $$->op = OSWITCH;
  938. $$->list = $6;
  939. typesw = typesw->left;
  940. popdcl();
  941. */
  942. $$ = $3
  943. $$.Op = OSWITCH
  944. $$.LL = $6
  945. if $$.NR == nil { // see node_if_header
  946. $$.Pos = $<pos>1
  947. }
  948. yylex.popScope()
  949. }
  950. node_select_stmt:
  951. LSELECT
  952. {
  953. /*
  954. typesw = nod(OXXX, typesw, N);
  955. */
  956. //TODO OXXX?
  957. }
  958. LBODY list_caseblock_list '}'
  959. {
  960. /*
  961. $$ = nod(OSELECT, N, N);
  962. $$->lineno = typesw->lineno;
  963. $$->list = $4;
  964. typesw = typesw->left;
  965. */
  966. //TODO
  967. /*
  968. for _, item := range $4 {
  969. switch {
  970. case item.Op != OCASE:
  971. internalError()
  972. case len(item.LL) > 1:
  973. yylex.error(item.LL[0].Pos, "select case must be receive, send or assign recv")
  974. }
  975. }
  976. */
  977. $$ = &Node{Pos: $<pos>1, Op: OSELECT, LL: $4}
  978. }
  979. /*
  980. * expressions
  981. */
  982. node_expr:
  983. node_uexpr
  984. | node_expr LOROR node_expr
  985. {
  986. /*
  987. $$ = nod(OOROR, $1, $3);
  988. */
  989. $$ = &Node{Pos: $<pos>1, Op: OOROR, NL: $1, NR: $3}
  990. }
  991. | node_expr LANDAND node_expr
  992. {
  993. /*
  994. $$ = nod(OANDAND, $1, $3);
  995. */
  996. $$ = &Node{Pos: $<pos>1, Op: OANDAND, NL: $1, NR: $3}
  997. }
  998. | node_expr LEQ node_expr
  999. {
  1000. /*
  1001. $$ = nod(OEQ, $1, $3);
  1002. */
  1003. $$ = &Node{Pos: $<pos>1, Op: OEQ, NL: $1, NR: $3}
  1004. }
  1005. | node_expr LNE node_expr
  1006. {
  1007. /*
  1008. $$ = nod(ONE, $1, $3);
  1009. */
  1010. $$ = &Node{Pos: $<pos>1, Op: ONE, NL: $1, NR: $3}
  1011. }
  1012. | node_expr LLT node_expr
  1013. {
  1014. /*
  1015. $$ = nod(OLT, $1, $3);
  1016. */
  1017. $$ = &Node{Pos: $<pos>1, Op: OLT, NL: $1, NR: $3}
  1018. }
  1019. | node_expr LLE node_expr
  1020. {
  1021. /*
  1022. $$ = nod(OLE, $1, $3);
  1023. */
  1024. $$ = &Node{Pos: $<pos>1, Op: OLE, NL: $1, NR: $3}
  1025. }
  1026. | node_expr LGE node_expr
  1027. {
  1028. /*
  1029. $$ = nod(OGE, $1, $3);
  1030. */
  1031. $$ = &Node{Pos: $<pos>1, Op: OGE, NL: $1, NR: $3}
  1032. }
  1033. | node_expr LGT node_expr
  1034. {
  1035. /*
  1036. $$ = nod(OGT, $1, $3);
  1037. */
  1038. $$ = &Node{Pos: $<pos>1, Op: OGT, NL: $1, NR: $3}
  1039. }
  1040. | node_expr '+' node_expr
  1041. {
  1042. /*
  1043. $$ = nod(OADD, $1, $3);
  1044. */
  1045. $$ = &Node{Pos: $<pos>1, Op: OADD, NL: $1, NR: $3}
  1046. }
  1047. | node_expr '-' node_expr
  1048. {
  1049. /*
  1050. $$ = nod(OSUB, $1, $3);
  1051. */
  1052. $$ = &Node{Pos: $<pos>1, Op: OSUB, NL: $1, NR: $3}
  1053. }
  1054. | node_expr '|' node_expr
  1055. {
  1056. /*
  1057. $$ = nod(OOR, $1, $3);
  1058. */
  1059. $$ = &Node{Pos: $<pos>1, Op: OOR, NL: $1, NR: $3}
  1060. }
  1061. | node_expr '^' node_expr
  1062. {
  1063. /*
  1064. $$ = nod(OXOR, $1, $3);
  1065. */
  1066. $$ = &Node{Pos: $<pos>1, Op: OXOR, NL: $1, NR: $3}
  1067. }
  1068. | node_expr '*' node_expr
  1069. {
  1070. /*
  1071. $$ = nod(OMUL, $1, $3);
  1072. */
  1073. $$ = &Node{Pos: $<pos>1, Op: OMUL, NL: $1, NR: $3}
  1074. }
  1075. | node_expr '/' node_expr
  1076. {
  1077. /*
  1078. $$ = nod(ODIV, $1, $3);
  1079. */
  1080. $$ = &Node{Pos: $<pos>1, Op: ODIV, NL: $1, NR: $3}
  1081. }
  1082. | node_expr '%' node_expr
  1083. {
  1084. /*
  1085. $$ = nod(OMOD, $1, $3);
  1086. */
  1087. $$ = &Node{Pos: $<pos>1, Op: OMOD, NL: $1, NR: $3}
  1088. }
  1089. | node_expr '&' node_expr
  1090. {
  1091. /*
  1092. $$ = nod(OAND, $1, $3);
  1093. */
  1094. $$ = &Node{Pos: $<pos>1, Op: OAND, NL: $1, NR: $3}
  1095. }
  1096. | node_expr LANDNOT node_expr
  1097. {
  1098. /*
  1099. $$ = nod(OANDNOT, $1, $3);
  1100. */
  1101. $$ = &Node{Pos: $<pos>1, Op: OANDNOT, NL: $1, NR: $3}
  1102. }
  1103. | node_expr LLSH node_expr
  1104. {
  1105. /*
  1106. $$ = nod(OLSH, $1, $3);
  1107. */
  1108. $$ = &Node{Pos: $<pos>1, Op: OLSH, NL: $1, NR: $3}
  1109. }
  1110. | node_expr LRSH node_expr
  1111. {
  1112. /*
  1113. $$ = nod(ORSH, $1, $3);
  1114. */
  1115. $$ = &Node{Pos: $<pos>1, Op: ORSH, NL: $1, NR: $3}
  1116. }
  1117. /* not an expression anymore, but left in so we can give a good error */
  1118. | node_expr LCOMM node_expr
  1119. {
  1120. /*
  1121. $$ = nod(OSEND, $1, $3);
  1122. */
  1123. $$ = &Node{Pos: $<pos>1, Op: OSEND, NL: $1, NR: $3}
  1124. }
  1125. node_uexpr:
  1126. node_pexpr
  1127. | '*' node_uexpr
  1128. {
  1129. /*
  1130. $$ = nod(OIND, $2, N);
  1131. */
  1132. $$ = &Node{Pos: $<pos>1, Op: OIND, NL: $2}
  1133. }
  1134. | '&' node_uexpr
  1135. {
  1136. /*
  1137. if($2->op == OCOMPLIT) {
  1138. // Special case for &T{...}: turn into (*T){...}.
  1139. $$ = $2;
  1140. $$->right = nod(OIND, $$->right, N);
  1141. $$->right->implicit = 1;
  1142. } else {
  1143. $$ = nod(OADDR, $2, N);
  1144. }
  1145. */
  1146. switch {
  1147. case $2.Op == OCOMPLIT:
  1148. // Special case for &T{...}: turn into (*T){...}.
  1149. $$ = $2
  1150. $$.NL = &Node{Pos: $$.NL.Pos, Op: OIND, NL: $$.NL}
  1151. default:
  1152. $$ = &Node{Pos: $<pos>1, Op: OADDR, NL: $2}
  1153. }
  1154. }
  1155. | '+' node_uexpr
  1156. {
  1157. /*
  1158. $$ = nod(OPLUS, $2, N);
  1159. */
  1160. $$ = &Node{Pos: $<pos>1, Op: OPLUS, NL: $2}
  1161. }
  1162. | '-' node_uexpr
  1163. {
  1164. /**
  1165. $$ = nod(OMINUS, $2, N);
  1166. */
  1167. $$ = &Node{Pos: $<pos>1, Op: OMINUS, NL: $2}
  1168. }
  1169. | '!' node_uexpr
  1170. {
  1171. /*
  1172. $$ = nod(ONOT, $2, N);
  1173. */
  1174. $$ = &Node{Pos: $<pos>1, Op: ONOT, NL: $2}
  1175. }
  1176. | '~' node_uexpr
  1177. {
  1178. /*
  1179. yyerror("the bitwise complement operator is ^");
  1180. $$ = nod(OCOM, $2, N);
  1181. */
  1182. yylex.error($<pos>1, "the bitwise complement operator is ^")
  1183. $$ = &Node{Pos: $<pos>1, Op: OCOM, NL: $2}
  1184. }
  1185. | '^' node_uexpr
  1186. {
  1187. /*
  1188. $$ = nod(OCOM, $2, N);
  1189. */
  1190. $$ = &Node{Pos: $<pos>1, Op: OCOM, NL: $2}
  1191. }
  1192. | LCOMM node_uexpr
  1193. {
  1194. /*
  1195. $$ = nod(ORECV, $2, N);
  1196. */
  1197. $$ = &Node{Pos: $<pos>1, Op: ORECV, NL: $2}
  1198. }
  1199. /*
  1200. * call-like statements that
  1201. * can be preceded by 'defer' and 'go'
  1202. */
  1203. node_pseudocall:
  1204. node_pexpr '(' ')'
  1205. {
  1206. /*
  1207. $$ = nod(OCALL, $1, N);
  1208. */
  1209. $$ = &Node{Pos: $<pos>1, Op: OCALL, NL: $1}
  1210. }
  1211. | node_pexpr '(' list_expr_or_type_list ocomma ')'
  1212. {
  1213. /*
  1214. $$ = nod(OCALL, $1, N);
  1215. $$->list = $3;
  1216. */
  1217. $$ = &Node{Pos: $<pos>1, Op: OCALL, NL: $1, LL: $3}
  1218. }
  1219. | node_pexpr '(' list_expr_or_type_list LDDD ocomma ')'
  1220. {
  1221. /*
  1222. $$ = nod(OCALL, $1, N);
  1223. $$->list = $3;
  1224. $$->isddd = 1;
  1225. */
  1226. $$ = &Node{Pos: $<pos>1, Op: OCALL, NL: $1, LL: $3, IsDDD: true}
  1227. }
  1228. node_pexpr_no_paren:
  1229. val_LLITERAL
  1230. {
  1231. /*
  1232. $$ = nodlit($1);
  1233. */
  1234. $$ = &Node{Pos: $1.Pos, Op: OLITERAL, Literal: $1}
  1235. }
  1236. | node_name
  1237. | node_pexpr '.' sym_sym
  1238. {
  1239. /*
  1240. if($1->op == OPACK) {
  1241. Sym *s;
  1242. s = restrictlookup($3->node_name, $1->pkg);
  1243. $1->used = 1;
  1244. $$ = oldname(s);
  1245. break;
  1246. }
  1247. $$ = nod(OXDOT, $1, newname($3));
  1248. */
  1249. //TODO above
  1250. $$ = &Node{Pos: $<pos>1, Op: OXDOT, NL: $1}
  1251. $$.NR = &Node{Pos: $<pos>3, Op: ONAME, Name: $3}
  1252. }
  1253. | node_pexpr '.' '(' node_expr_or_type ')'
  1254. {
  1255. /*
  1256. $$ = nod(ODOTTYPE, $1, $4);
  1257. */
  1258. $$ = &Node{Pos: $<pos>1, Op: ODOTTYPE, NL: $1, NR: $4}
  1259. }
  1260. | node_pexpr '.' '(' LTYPE ')'
  1261. {
  1262. /*
  1263. $$ = nod(OTYPESW, N, $1);
  1264. */
  1265. $$ = &Node{Pos: $<pos>1, Op: OTYPESW, NL: $1}
  1266. }
  1267. | node_pexpr '[' node_expr ']'
  1268. {
  1269. /*
  1270. $$ = nod(OINDEX, $1, $3);
  1271. */
  1272. $$ = &Node{Pos: $<pos>1, Op: OINDEX, NL: $1, NR: $3}
  1273. }
  1274. | node_pexpr '[' node_oexpr ':' node_oexpr ']'
  1275. {
  1276. /*
  1277. $$ = nod(OSLICE, $1, nod(OKEY, $3, $5));
  1278. */
  1279. $$ = &Node{Pos: $<pos>1, Op: OSLICE, NL: $1}
  1280. $$.NR = &Node{Pos: $<pos>3, Op: OKEY, NL: $3, NR: $5}
  1281. }
  1282. | node_pseudocall
  1283. | node_convtype '(' node_expr ')'
  1284. {
  1285. /*
  1286. // conversion
  1287. $$ = nod(OCALL, $1, N);
  1288. $$->list = list1($3);
  1289. */
  1290. $$ = &Node{Pos: $<pos>1, Op: OCALL, NL: $1, LL: $3.list()}
  1291. }
  1292. | node_comptype i_lbrace node_start_complit list_braced_keyval_list '}'
  1293. {
  1294. /*
  1295. $$ = $3;
  1296. $$->right = $1;
  1297. $$->list = $4;
  1298. fixlbrace($2);
  1299. */
  1300. $$ = $3
  1301. $$.NL, $$.LL = $1, $4
  1302. yylex.fixlbrace($2)
  1303. }
  1304. | node_pexpr_no_paren '{' node_start_complit list_braced_keyval_list '}'
  1305. {
  1306. /*
  1307. $$ = $3;
  1308. $$->right = $1;
  1309. $$->list = $4;
  1310. */
  1311. $$ = $3
  1312. $$.NL, $$.LL = $1, $4
  1313. }
  1314. | '(' node_expr_or_type ')' '{' node_start_complit list_braced_keyval_list '}'
  1315. {
  1316. /*
  1317. yyerror("cannot parenthesize type in composite literal");
  1318. $$ = $5;
  1319. $$->right = $2;
  1320. $$->list = $6;
  1321. */
  1322. $$ = $5
  1323. $$.NL, $$.LL = $2, $6
  1324. yylex.error($<pos>1, "cannot parenthesize type in composite literal")
  1325. }
  1326. | node_fnliteral
  1327. node_start_complit: //TODO -?
  1328. {
  1329. /*
  1330. // composite expression.
  1331. // make node early so we get the right line number.
  1332. $$ = nod(OCOMPLIT, N, N);
  1333. */
  1334. $$ = &Node{Pos: yylex.lbrpos, Op: OCOMPLIT} //TODO check pos!
  1335. }
  1336. node_keyval:
  1337. node_expr ':' node_complitexpr
  1338. {
  1339. /*
  1340. $$ = nod(OKEY, $1, $3);
  1341. */
  1342. $$ = &Node{Pos: $<pos>1, Op: OKEY, NL: $1, NR: $3}
  1343. }
  1344. node_complitexpr:
  1345. node_expr
  1346. | '{' node_start_complit list_braced_keyval_list '}'
  1347. {
  1348. /*
  1349. $$ = $2;
  1350. $$->list = $3;
  1351. */
  1352. $$ = $2
  1353. $$.LL = $3
  1354. }
  1355. node_pexpr:
  1356. node_pexpr_no_paren
  1357. | '(' node_expr_or_type ')'
  1358. {
  1359. /*
  1360. $$ = $2;
  1361. // Need to know on lhs of := whether there are ( ).
  1362. // Don't bother with the OPAREN in other cases:
  1363. // it's just a waste of memory and time.
  1364. switch($$->op) {
  1365. case ONAME:
  1366. case ONONAME:
  1367. case OPACK:
  1368. case OTYPE:
  1369. case OLITERAL:
  1370. $$ = nod(OPAREN, $$, N);
  1371. }
  1372. */
  1373. switch $2.Op {
  1374. case ONAME, /*TODO ONONAME,*/ /*TODO OPACK,*/ /*TODO OTYPE,*/ OLITERAL:
  1375. $$ = &Node{Pos: $<pos>1, Op: OPAREN, NL: $2}
  1376. default:
  1377. $$ = $2
  1378. }
  1379. }
  1380. node_expr_or_type:
  1381. node_expr
  1382. | node_non_expr_type %prec PreferToRightParen
  1383. node_name_or_type:
  1384. node_ntype
  1385. i_lbrace:
  1386. LBODY
  1387. {
  1388. /*
  1389. $$ = LBODY;
  1390. */
  1391. $$ = LBODY
  1392. }
  1393. | '{'
  1394. {
  1395. /*
  1396. $$ = '{';
  1397. */
  1398. $$ = '{'
  1399. }
  1400. /*
  1401. * names and types
  1402. * newname is used before declared
  1403. * oldname is used after declared
  1404. */
  1405. node_new_name:
  1406. sym_sym
  1407. {
  1408. /*
  1409. $$ = newname($1);
  1410. */
  1411. $$ = &Node{Pos: $<pos>1, Op: ONAME, Name: $1}
  1412. }
  1413. node_dcl_name:
  1414. sym_sym
  1415. {
  1416. /*
  1417. $$ = dclname($1);
  1418. */
  1419. $$ = yylex.declare($1, SymTag(0)) // not known here
  1420. }
  1421. node_onew_name:
  1422. {
  1423. /*
  1424. $$ = N;
  1425. */
  1426. $$ = nil
  1427. }
  1428. | node_new_name
  1429. sym_sym:
  1430. sym_LNAME
  1431. /*
  1432. {
  1433. $$ = $1;
  1434. // during imports, unqualified non-exported identifiers are from builtinpkg
  1435. if(importpkg != nil && !exportname($1->node_name))
  1436. $$ = pkglookup($1->node_name, builtinpkg);
  1437. }
  1438. */
  1439. /*
  1440. | sym_hidden_importsym
  1441. */
  1442. /*
  1443. | '?'
  1444. {
  1445. $$ = S;
  1446. }
  1447. */
  1448. /*
  1449. sym_hidden_importsym:
  1450. '@' val_LLITERAL '.' sym_LNAME
  1451. {
  1452. if($2.u.sval->len == 0)
  1453. $$ = pkglookup($4->node_name, importpkg);
  1454. else
  1455. $$ = pkglookup($4->node_name, mkpkg($2.u.sval));
  1456. }
  1457. */
  1458. node_name:
  1459. sym_sym %prec NotParen
  1460. {
  1461. /*
  1462. $$ = oldname($1);
  1463. if($$->pack != N)
  1464. $$->pack->used = 1;
  1465. */
  1466. $$ = &Node{Pos: $<pos>1, Op: ONAME, Name: $1}
  1467. }
  1468. node_labelname:
  1469. node_new_name
  1470. /*
  1471. * to avoid parsing conflicts, type is split into
  1472. * channel types
  1473. * function types
  1474. * parenthesized types
  1475. * any other type
  1476. * the type system makes additional restrictions,
  1477. * but those are not implemented in the grammar.
  1478. */
  1479. node_dotdotdot:
  1480. LDDD
  1481. {
  1482. /*
  1483. yyerror("final argument in variadic function missing type");
  1484. $$ = nod(ODDD, typenod(typ(TINTER)), N);
  1485. */
  1486. yylex.error($<pos>1, "final argument in variadic function missing type")
  1487. $$ = &Node{Pos: $<pos>1, Op: ODDD}
  1488. $$.NL = &Node{Pos: $<pos>1, Op: OTYPE}
  1489. $$.NL.Type = &Type{Tag: TINTER}
  1490. }
  1491. | LDDD node_ntype
  1492. {
  1493. /*
  1494. $$ = nod(ODDD, $2, N);
  1495. */
  1496. $$ = &Node{Pos: $<pos>1, Op: ODDD, NL: $2}
  1497. }
  1498. node_ntype:
  1499. node_recvchantype
  1500. | node_fntype
  1501. | node_othertype
  1502. | node_ptrtype
  1503. | node_dotname
  1504. | '(' node_ntype ')'
  1505. {
  1506. /*
  1507. $$ = nod(OTPAREN, $2, N);
  1508. */
  1509. $$ = &Node{Pos: $<pos>1, Op: OTPAREN, NL: $2}
  1510. }
  1511. node_non_expr_type:
  1512. node_recvchantype
  1513. | node_fntype
  1514. | node_othertype
  1515. | '*' node_non_expr_type
  1516. {
  1517. /*
  1518. $$ = nod(OIND, $2, N);
  1519. */
  1520. $$ = &Node{Pos: $<pos>1, Op: OIND, NL: $2}
  1521. }
  1522. node_non_recvchantype:
  1523. node_fntype
  1524. | node_othertype
  1525. | node_ptrtype
  1526. | node_dotname
  1527. | '(' node_ntype ')'
  1528. {
  1529. /*
  1530. $$ = nod(OTPAREN, $2, N);
  1531. */
  1532. $$ = &Node{Pos: $<pos>1, Op: OTPAREN, NL: $2}
  1533. }
  1534. node_convtype:
  1535. node_fntype
  1536. | node_othertype
  1537. node_comptype:
  1538. node_othertype
  1539. node_fnret_type:
  1540. node_recvchantype
  1541. | node_fntype
  1542. | node_othertype
  1543. | node_ptrtype
  1544. | node_dotname
  1545. node_dotname:
  1546. node_name
  1547. | node_name '.' sym_sym
  1548. {
  1549. /*
  1550. if($1->op == OPACK) {
  1551. Sym *s;
  1552. s = restrictlookup($3->node_name, $1->pkg);
  1553. $1->used = 1;
  1554. $$ = oldname(s);
  1555. break;
  1556. }
  1557. $$ = nod(OXDOT, $1, newname($3));
  1558. */
  1559. $$ = &Node{Pos: $<pos>1, Op: OXDOT, NL: $1}
  1560. $$.NR = &Node{Pos: $<pos>3, Op: ONAME, Name: $3}
  1561. }
  1562. node_othertype:
  1563. '[' node_oexpr ']' node_ntype
  1564. {
  1565. /*
  1566. $$ = nod(OTARRAY, $2, $4);
  1567. */
  1568. $$ = &Node{Pos: $<pos>1, Op: OTARRAY, NL: $2, NR: $4}
  1569. }
  1570. | '[' LDDD ']' node_ntype
  1571. {
  1572. /*
  1573. // array literal of nelem
  1574. $$ = nod(OTARRAY, nod(ODDD, N, N), $4);
  1575. */
  1576. $$ = &Node{Pos: $<pos>1, Op: OTARRAY, NR: $4}
  1577. $$.NL = &Node{Pos: $<pos>2, Op: ODDD}
  1578. }
  1579. | LCHAN node_non_recvchantype
  1580. {
  1581. /*
  1582. $$ = nod(OTCHAN, $2, N);
  1583. $$->etype = Cboth;
  1584. */
  1585. $$ = &Node{Pos: $<pos>1, Op: OTCHAN, NL: $2, CType: Cboth}
  1586. }
  1587. | LCHAN LCOMM node_ntype
  1588. {
  1589. /*
  1590. $$ = nod(OTCHAN, $3, N);
  1591. $$->etype = Csend;
  1592. */
  1593. $$ = &Node{Pos: $<pos>1, Op: OTCHAN, NL: $3, CType: Csend}
  1594. }
  1595. | LMAP '[' node_ntype ']' node_ntype
  1596. {
  1597. /*
  1598. $$ = nod(OTMAP, $3, $5);
  1599. */
  1600. $$ = &Node{Pos: $<pos>1, Op: OTMAP, NL: $3, NR: $5}
  1601. }
  1602. | node_structtype
  1603. | node_interfacetype
  1604. node_ptrtype:
  1605. '*' node_ntype
  1606. {
  1607. /*
  1608. $$ = nod(OIND, $2, N);
  1609. */
  1610. $$ = &Node{Pos: $<pos>1, Op: OIND, NL: $2}
  1611. }
  1612. node_recvchantype:
  1613. LCOMM LCHAN node_ntype
  1614. {
  1615. /*
  1616. $$ = nod(OTCHAN, $3, N);
  1617. $$->etype = Crecv;
  1618. */
  1619. $$ = &Node{Pos: $<pos>1, Op: OTCHAN, NL: $3, CType: Crecv}
  1620. }
  1621. node_structtype:
  1622. LSTRUCT i_lbrace list_structdcl_list osemi '}'
  1623. {
  1624. /*
  1625. $$ = nod(OTSTRUCT, N, N);
  1626. $$->list = $3;
  1627. fixlbrace($2);
  1628. */
  1629. $$ = &Node{Pos: $<pos>1, Op: OTSTRUCT, LL: $3}
  1630. yylex.fixlbrace($2)
  1631. }
  1632. | LSTRUCT i_lbrace '}'
  1633. {
  1634. /*
  1635. $$ = nod(OTSTRUCT, N, N);
  1636. fixlbrace($2);
  1637. */
  1638. $$ = &Node{Pos: $<pos>1, Op: OTSTRUCT}
  1639. yylex.fixlbrace($2)
  1640. }
  1641. node_interfacetype:
  1642. LINTERFACE i_lbrace list_interfacedcl_list osemi '}'
  1643. {
  1644. /*
  1645. $$ = nod(OTINTER, N, N);
  1646. $$->list = $3;
  1647. fixlbrace($2);
  1648. */
  1649. $$ = &Node{Pos: $<pos>1, Op: OTINTER, LL: $3}
  1650. yylex.fixlbrace($2)
  1651. }
  1652. | LINTERFACE i_lbrace '}'
  1653. {
  1654. /*
  1655. $$ = nod(OTINTER, N, N);
  1656. fixlbrace($2);
  1657. */
  1658. $$ = &Node{Pos: $<pos>1, Op: OTINTER}
  1659. yylex.fixlbrace($2)
  1660. }
  1661. /*
  1662. * function stuff
  1663. * all in one place to show how crappy it all is
  1664. */
  1665. node_xfndcl:
  1666. LFUNC node_fndcl list_fnbody
  1667. {
  1668. /*
  1669. $$ = $2;
  1670. if($$ == N)
  1671. break;
  1672. $$->nbody = $3;
  1673. $$->endlineno = lineno;
  1674. funcbody($$);
  1675. */
  1676. $$ = $2
  1677. if $$ == nil {
  1678. break
  1679. }
  1680. $$.LL = $3
  1681. $$.Pos = $<pos>1
  1682. yylex.funcbody($$)
  1683. }
  1684. node_fndcl:
  1685. sym_sym '(' list_oarg_type_list_ocomma ')' list_fnres
  1686. {
  1687. /*
  1688. Node *t;
  1689. $$ = N;
  1690. $3 = checkarglist($3, 1);
  1691. if(strcmp($1->node_name, "init") == 0) {
  1692. $1 = renameinit();
  1693. if($3 != nil || $5 != nil)
  1694. yyerror("func init must have no arguments and no return values");
  1695. }
  1696. if(strcmp(localpkg->node_name, "main") == 0 && strcmp($1->node_name, "main") == 0) {
  1697. if($3 != nil || $5 != nil)
  1698. yyerror("func main must have no arguments and no return values");
  1699. }
  1700. t = nod(OTFUNC, N, N);
  1701. t->list = $3;
  1702. t->rlist = $5;
  1703. $$ = nod(ODCLFUNC, N, N);
  1704. $$->nname = newname($1);
  1705. $$->nname->defn = $$;
  1706. $$->nname->node_ntype = t; // TODO: check if nname already has an node_ntype
  1707. declare($$->nname, PFUNC);
  1708. funchdr($$);
  1709. */
  1710. $3, $<closure>3 = yylex.checkarglist($3, true)
  1711. //TODO above
  1712. $$ = &Node{Pos: $<pos>1, Op: ODCLFUNC}
  1713. $$.NL = yylex.declare($1, SymFunc)
  1714. $$.NR = &Node{Pos: $<pos>2, Op: OTFUNC, LR: $3, L3: $5}
  1715. yylex.funchdr($$)
  1716. $<closure>3()
  1717. $<closure>5()
  1718. }
  1719. | '(' list_oarg_type_list_ocomma ')' sym_sym '(' list_oarg_type_list_ocomma ')' list_fnres
  1720. {
  1721. /*
  1722. Node *rcvr, *t;
  1723. $$ = N;
  1724. $2 = checkarglist($2, 0);
  1725. $6 = checkarglist($6, 1);
  1726. if($2 == nil) {
  1727. yyerror("method has no receiver");
  1728. break;
  1729. }
  1730. if($2->next != nil) {
  1731. yyerror("method has multiple receivers");
  1732. break;
  1733. }
  1734. rcvr = $2->n;
  1735. if(rcvr->op != ODCLFIELD) {
  1736. yyerror("bad receiver in method");
  1737. break;
  1738. }
  1739. if(rcvr->right->op == OTPAREN || (rcvr->right->op == OIND && rcvr->right->left->op == OTPAREN))
  1740. yyerror("cannot parenthesize receiver type");
  1741. t = nod(OTFUNC, rcvr, N);
  1742. t->list = $6;
  1743. t->rlist = $8;
  1744. $$ = nod(ODCLFUNC, N, N);
  1745. $$->shortname = newname($4);
  1746. $$->nname = methodname1($$->shortname, rcvr->right);
  1747. $$->nname->defn = $$;
  1748. $$->nname->node_ntype = t;
  1749. declare($$->nname, PFUNC);
  1750. funchdr($$);
  1751. */
  1752. $$ = nil
  1753. $2, $<closure>2 = yylex.checkarglist($2, false)
  1754. $6, $<closure>6 = yylex.checkarglist($6, true)
  1755. //TODO above
  1756. if $2 == nil {
  1757. yylex.error($<pos>1, "method has no receiver")
  1758. break
  1759. } else if len($2) > 1 {
  1760. yylex.error($2[1].Pos, "method has multiple receivers")
  1761. break
  1762. }
  1763. var rcvr, t *Node
  1764. n := $2[0]
  1765. switch n.Op {
  1766. case OKEY:
  1767. rcvr, t = n.NL, n.NR
  1768. case ONAME, OIND:
  1769. t = n
  1770. default:
  1771. internalError(NodeOps[n.Op])
  1772. }
  1773. if rcvr == nil && t == nil {
  1774. yylex.error(n.Pos, "bad receiver in method")
  1775. break
  1776. }
  1777. if t != nil {
  1778. if t.Op == OTPAREN || t.Op == OIND && t.NL.Op == OTPAREN {
  1779. yylex.error(t.Pos, "cannot parenthesize receiver type")
  1780. }
  1781. }
  1782. $$ = &Node{Pos: $<pos>1, Op: ODCLFUNC}
  1783. $$.NL = yylex.declare(yylex.methodname($4, t), SymMeth)
  1784. $$.NR = &Node{Pos: $<pos>5, Op: OTFUNC, LL: $2, LR: $6, L3: $8}
  1785. yylex.funchdr($$)
  1786. $<closure>2()
  1787. $<closure>6()
  1788. $<closure>8()
  1789. }
  1790. /*
  1791. node_hidden_fndcl:
  1792. sym_hidden_pkg_importsym '(' list_ohidden_funarg_list ')' list_ohidden_funres
  1793. {
  1794. Sym *s;
  1795. Type *t;
  1796. $$ = N;
  1797. s = $1;
  1798. t = functype(N, $3, $5);
  1799. importsym(s, ONAME);
  1800. if(s->def != N && s->def->op == ONAME) {
  1801. if(eqtype(t, s->def->type))
  1802. break;
  1803. yyerror("inconsistent definition for func %S during import\n\t%T\n\t%T", s, s->def->type, t);
  1804. }
  1805. $$ = newname(s);
  1806. $$->type = t;
  1807. declare($$, PFUNC);
  1808. funchdr($$);
  1809. }
  1810. | '(' list_hidden_funarg_list ')' sym_sym '(' list_ohidden_funarg_list ')' list_ohidden_funres
  1811. {
  1812. $$ = methodname1(newname($4), $2->n->right);
  1813. $$->type = functype($2->n, $6, $8);
  1814. checkwidth($$->type);
  1815. addmethod($4, $$->type, 0);
  1816. funchdr($$);
  1817. // inl.c's inlnode in on a dotmeth node expects to find the inlineable body as
  1818. // (dotmeth's type)->nname->inl, and dotmeth's type has been pulled
  1819. // out by typecheck's lookdot as this $$->ttype. So by providing
  1820. // this back link here we avoid special casing there.
  1821. $$->type->nname = $$;
  1822. }
  1823. */
  1824. node_fntype:
  1825. LFUNC '(' list_oarg_type_list_ocomma ')' list_fnres
  1826. {
  1827. /*
  1828. $3 = checkarglist($3, 1);
  1829. $$ = nod(OTFUNC, N, N);
  1830. $$->list = $3;
  1831. $$->rlist = $5;
  1832. */
  1833. $3, $<closure>$ = yylex.checkarglist($3, true)
  1834. $$ = &Node{Pos: $<pos>1, Op: OTFUNC, LR: $3, L3: $5}
  1835. }
  1836. list_fnbody:
  1837. {
  1838. /*
  1839. $$ = nil;
  1840. */
  1841. $$ = nil
  1842. }
  1843. | '{' list_stmt_list '}'
  1844. {
  1845. /*
  1846. $$ = $2;
  1847. if($$ == nil)
  1848. $$ = list1(nod(OEMPTY, N, N));
  1849. */
  1850. $$ = $2
  1851. }
  1852. list_fnres:
  1853. %prec NotParen
  1854. {
  1855. /*
  1856. $$ = nil;
  1857. */
  1858. $$ = nil
  1859. $<closure>$ = nop
  1860. }
  1861. | node_fnret_type
  1862. {
  1863. /*
  1864. $$ = list1(nod(ODCLFIELD, N, $1));
  1865. */
  1866. $$ = (&Node{Pos: $<pos>1, Op: ODCLFIELD, NR: $1}).list()
  1867. $<closure>$ = nop
  1868. }
  1869. | '(' list_oarg_type_list_ocomma ')'
  1870. {
  1871. /*
  1872. $2 = checkarglist($2, 0);
  1873. $$ = $2;
  1874. */
  1875. $$, $<closure>$ = yylex.checkarglist($2, false)
  1876. }
  1877. /*
  1878. fnlitdcl:
  1879. node_fntype
  1880. {
  1881. closurehdr($1);
  1882. }
  1883. */
  1884. node_fnlitdcl:
  1885. node_fntype
  1886. {
  1887. /*
  1888. closurehdr($1);
  1889. */
  1890. $$ = &Node{Pos: $1.Pos, Op: OCLOSURE, NL: $1}
  1891. yylex.closurehdr($$)
  1892. }
  1893. /*
  1894. node_fnliteral:
  1895. fnlitdcl i_lbrace list_stmt_list '}'
  1896. {
  1897. $$ = closurebody($3);
  1898. fixlbrace($2);
  1899. }
  1900. | fnlitdcl error
  1901. {
  1902. $$ = closurebody(nil);
  1903. }
  1904. */
  1905. node_fnliteral:
  1906. node_fnlitdcl i_lbrace list_stmt_list '}'
  1907. {
  1908. /*
  1909. $$ = closurebody($3);
  1910. fixlbrace($2);
  1911. */
  1912. $$ = yylex.closurebody($3)
  1913. yylex.fixlbrace($2)
  1914. }
  1915. | node_fnlitdcl error
  1916. {
  1917. /*
  1918. $$ = closurebody(nil);
  1919. */
  1920. $$ = yylex.closurebody(nil)
  1921. //TODO yylex.error()?
  1922. }
  1923. /*
  1924. * lists of things
  1925. * note that they are left recursive
  1926. * to conserve yacc stack. they need to
  1927. * be reversed to interpret correctly
  1928. */
  1929. list_xdcl_list:
  1930. {
  1931. /*
  1932. $$ = nil;
  1933. */
  1934. $$ = nil
  1935. }
  1936. | list_xdcl_list list_xdcl ';'
  1937. {
  1938. /*
  1939. $$ = concat($1, $2);
  1940. if(nsyntaxerrors == 0)
  1941. testdclstack();
  1942. */
  1943. $$ = append($1, $2...)
  1944. }
  1945. list_vardcl_list:
  1946. list_vardcl
  1947. | list_vardcl_list ';' list_vardcl
  1948. {
  1949. /*
  1950. $$ = concat($1, $3);
  1951. */
  1952. $$ = append($1, $3...)
  1953. }
  1954. list_constdcl_list:
  1955. list_constdcl1
  1956. | list_constdcl_list ';' list_constdcl1
  1957. {
  1958. /*
  1959. $$ = concat($1, $3);
  1960. */
  1961. $$ = append($1, $3...)
  1962. }
  1963. list_typedcl_list:
  1964. node_typedcl
  1965. {
  1966. /*
  1967. $$ = list1($1);
  1968. */
  1969. $$ = $1.list()
  1970. }
  1971. | list_typedcl_list ';' node_typedcl
  1972. {
  1973. /*
  1974. $$ = list($1, $3);
  1975. */
  1976. $$ = append($1, $3)
  1977. }
  1978. list_structdcl_list:
  1979. list_structdcl
  1980. | list_structdcl_list ';' list_structdcl
  1981. {
  1982. /*
  1983. $$ = concat($1, $3);
  1984. */
  1985. $$ = append($1, $3...)
  1986. }
  1987. list_interfacedcl_list:
  1988. node_interfacedcl
  1989. {
  1990. /*
  1991. $$ = list1($1);
  1992. */
  1993. $$ = $1.list()
  1994. }
  1995. | list_interfacedcl_list ';' node_interfacedcl
  1996. {
  1997. /*
  1998. $$ = list($1, $3);
  1999. */
  2000. $$ = append($1, $3)
  2001. }
  2002. list_structdcl:
  2003. list_new_name_list node_ntype val_oliteral
  2004. {
  2005. /*
  2006. NodeList *l;
  2007. for(l=$1; l; l=l->next) {
  2008. l->n = nod(ODCLFIELD, l->n, $2);
  2009. l->n->val = $3;
  2010. }
  2011. */
  2012. $$ = (&Node{Pos: $<pos>1, Op: ODCLFIELD, LL: $1, NL: $2, Literal: $3}).list()
  2013. }
  2014. | node_embed val_oliteral
  2015. {
  2016. /*
  2017. $1->val = $2;
  2018. $$ = list1($1);
  2019. */
  2020. $1.Literal = $2
  2021. $$ = $1.list()
  2022. }
  2023. | '(' node_embed ')' val_oliteral
  2024. {
  2025. /*
  2026. $2->val = $4;
  2027. $$ = list1($2);
  2028. yyerror("cannot parenthesize embedded type");
  2029. */
  2030. $2.Literal = $4
  2031. $$ = $2.list()
  2032. yylex.error($<pos>1, "cannot parenthesize embedded type")
  2033. }
  2034. | '*' node_embed val_oliteral
  2035. {
  2036. /*
  2037. $2->right = nod(OIND, $2->right, N);
  2038. $2->val = $3;
  2039. $$ = list1($2);
  2040. */
  2041. $2.Literal = $3
  2042. $$ = (&Node{Pos: $<pos>1, Op: OIND, NL: $2}).list()
  2043. }
  2044. | '(' '*' node_embed ')' val_oliteral
  2045. {
  2046. /*
  2047. $3->right = nod(OIND, $3->right, N);
  2048. $3->val = $5;
  2049. $$ = list1($3);
  2050. yyerror("cannot parenthesize embedded type");
  2051. */
  2052. $3.Literal = $5
  2053. $$ = (&Node{Pos: $<pos>1, Op: OIND, NL: $3}).list()
  2054. yylex.error($<pos>1, "cannot parenthesize embedded type")
  2055. }
  2056. | '*' '(' node_embed ')' val_oliteral
  2057. {
  2058. /*
  2059. $3->right = nod(OIND, $3->right, N);
  2060. $3->val = $5;
  2061. $$ = list1($3);
  2062. yyerror("cannot parenthesize embedded type");
  2063. */
  2064. $3.Literal = $5
  2065. $$ = (&Node{Pos: $<pos>1, Op: OIND, NL: $3}).list()
  2066. yylex.error($<pos>2, "cannot parenthesize embedded type")
  2067. }
  2068. sym_packname:
  2069. sym_LNAME
  2070. {
  2071. /*
  2072. Node *n;
  2073. $$ = $1;
  2074. n = oldname($1);
  2075. if(n->pack != N)
  2076. n->pack->used = 1;
  2077. */
  2078. $$ = $1
  2079. }
  2080. | sym_LNAME '.' sym_sym
  2081. {
  2082. /*
  2083. Pkg *pkg;
  2084. if($1->def == N || $1->def->op != OPACK) {
  2085. yyerror("%S is not a package", $1);
  2086. pkg = localpkg;
  2087. } else {
  2088. $1->def->used = 1;
  2089. pkg = $1->def->pkg;
  2090. }
  2091. $$ = restrictlookup($3->node_name, pkg);
  2092. */
  2093. $$ = $3
  2094. $$.Pos = $1.Pos
  2095. $$.Packname = $1.Name
  2096. }
  2097. node_embed:
  2098. sym_packname
  2099. {
  2100. /*
  2101. $$ = embedded($1);
  2102. */
  2103. $$ = &Node{Pos: $<pos>1, Op: ODCLFIELD}
  2104. $$.NR = &Node{Pos: $$.Pos, Op: ONAME, Name: $1}
  2105. }
  2106. node_interfacedcl:
  2107. node_new_name node_indcl
  2108. {
  2109. /*
  2110. $$ = nod(ODCLFIELD, $1, $2);
  2111. ifacedcl($$);
  2112. */
  2113. $$ = &Node{Pos: $<pos>1, Op: ODCLFIELD, NL: $1, NR: $2}
  2114. }
  2115. | sym_packname
  2116. {
  2117. /*
  2118. $$ = nod(ODCLFIELD, N, oldname($1));
  2119. */
  2120. $$ = &Node{Pos: $<pos>1, Op: ODCLFIELD}
  2121. $$.NR = &Node{Pos: $<pos>1, Op: ONAME, Name: $1}
  2122. }
  2123. | '(' sym_packname ')'
  2124. {
  2125. /*
  2126. $$ = nod(ODCLFIELD, N, oldname($2));
  2127. yyerror("cannot parenthesize embedded type");
  2128. */
  2129. $$ = &Node{Pos: $<pos>1, Op: ODCLFIELD}
  2130. $$.NR = &Node{Pos: $<pos>2, Op: ONAME, Name: $2}
  2131. yylex.error($<pos>1, "cannot parenthesize embedded type")
  2132. }
  2133. node_indcl:
  2134. '(' list_oarg_type_list_ocomma ')' list_fnres
  2135. {
  2136. /*
  2137. // without func keyword
  2138. $2 = checkarglist($2, 1);
  2139. $$ = nod(OTFUNC, fakethis(), N);
  2140. $$->list = $2;
  2141. $$->rlist = $4;
  2142. */
  2143. $2, $<closure>$ = yylex.checkarglist($2, true)
  2144. $$ = &Node{Pos: $<pos>1, Op: OTFUNC, LR: $2, L3: $4}
  2145. }
  2146. /*
  2147. * function arguments.
  2148. */
  2149. node_arg_type:
  2150. node_name_or_type
  2151. | sym_sym node_name_or_type
  2152. {
  2153. /*
  2154. $$ = nod(ONONAME, N, N);
  2155. $$->sym_sym = $1;
  2156. $$ = nod(OKEY, $$, $2);
  2157. */
  2158. $$ = &Node{Pos: $<pos>1, Op: OKEY, NR: $2}
  2159. $$.NL = &Node{Pos: $<pos>1, Op: ONAME, Name: $1}
  2160. }
  2161. | sym_sym node_dotdotdot
  2162. {
  2163. /*
  2164. $$ = nod(ONONAME, N, N);
  2165. $$->sym_sym = $1;
  2166. $$ = nod(OKEY, $$, $2);
  2167. */
  2168. $$ = &Node{Pos: $<pos>1, Op: OKEY, NR: $2}
  2169. $$.NL = &Node{Pos: $<pos>1, Op: ONAME, Name: $1}
  2170. }
  2171. | node_dotdotdot
  2172. list_arg_type_list:
  2173. node_arg_type
  2174. {
  2175. /*
  2176. $$ = list1($1);
  2177. */
  2178. $$ = $1.list()
  2179. }
  2180. | list_arg_type_list ',' node_arg_type
  2181. {
  2182. /*
  2183. $$ = list($1, $3);
  2184. */
  2185. $$ = append($1, $3)
  2186. }
  2187. list_oarg_type_list_ocomma:
  2188. {
  2189. /*
  2190. $$ = nil;
  2191. */
  2192. $$ = nil
  2193. }
  2194. | list_arg_type_list ocomma
  2195. {
  2196. /*
  2197. $$ = $1;
  2198. */
  2199. $$ = $1
  2200. }
  2201. /*
  2202. * statement
  2203. */
  2204. node_stmt:
  2205. {
  2206. /*
  2207. $$ = N;
  2208. */
  2209. $$ = nil
  2210. }
  2211. | node_compound_stmt
  2212. | list_common_dcl
  2213. {
  2214. /*
  2215. $$ = liststmt($1);
  2216. */
  2217. $$ = &Node{Pos: $<pos>1, Op: OBLOCK, LL: $1}
  2218. }
  2219. | node_non_dcl_stmt
  2220. | error
  2221. {
  2222. /*
  2223. $$ = N;
  2224. */
  2225. $$ = nil
  2226. //TODO yylex.error()?
  2227. }
  2228. node_non_dcl_stmt:
  2229. node_simple_stmt
  2230. | node_for_stmt
  2231. | node_switch_stmt
  2232. | node_select_stmt
  2233. | node_if_stmt
  2234. | node_labelname ':'
  2235. {
  2236. /*
  2237. $1 = nod(OLABEL, $1, N);
  2238. $1->sym_sym = dclstack; // context, for goto restrictions
  2239. */
  2240. $1 = &Node{Pos: $<pos>1, Op: OLABEL, NL: $1}
  2241. }
  2242. node_stmt
  2243. {
  2244. /*
  2245. NodeList *l;
  2246. $1->defn = $4;
  2247. l = list1($1);
  2248. if($4)
  2249. l = list(l, $4);
  2250. $$ = liststmt(l);
  2251. */
  2252. //TODO above?
  2253. $$ = $1
  2254. $$.NR = $4
  2255. }
  2256. | LFALL
  2257. {
  2258. /*
  2259. // will be converted to OFALL
  2260. $$ = nod(OXFALL, N, N);
  2261. */
  2262. $$ = &Node{Pos: $<pos>1, Op: OFALL}
  2263. }
  2264. | LBREAK node_onew_name
  2265. {
  2266. /*
  2267. $$ = nod(OBREAK, $2, N);
  2268. */
  2269. $$ = &Node{Pos: $<pos>1, Op: OBREAK, NL: $2}
  2270. }
  2271. | LCONTINUE node_onew_name
  2272. {
  2273. /*
  2274. $$ = nod(OCONTINUE, $2, N);
  2275. */
  2276. $$ = &Node{Pos: $<pos>1, Op: OCONTINUE, NL: $2}
  2277. }
  2278. | LGO node_pseudocall
  2279. {
  2280. /*
  2281. $$ = nod(OPROC, $2, N);
  2282. */
  2283. $$ = &Node{Pos: $<pos>1, Op: OPROC, NL: $2}
  2284. }
  2285. | LDEFER node_pseudocall
  2286. {
  2287. /*
  2288. $$ = nod(ODEFER, $2, N);
  2289. */
  2290. $$ = &Node{Pos: $<pos>1, Op: ODEFER, NL: $2}
  2291. }
  2292. | LGOTO node_new_name
  2293. {
  2294. /*
  2295. $$ = nod(OGOTO, $2, N);
  2296. $$->sym_sym = dclstack; // context, for goto restrictions
  2297. */
  2298. $$ = &Node{Pos: $<pos>1, Op: OGOTO, NL: $2}
  2299. }
  2300. | LRETURN list_oexpr_list
  2301. {
  2302. /*
  2303. $$ = nod(ORETURN, N, N);
  2304. $$->list = $2;
  2305. if($$->list == nil && curfn != N) {
  2306. NodeList *l;
  2307. for(l=curfn->dcl; l; l=l->next) {
  2308. if(l->n->class == PPARAM)
  2309. continue;
  2310. if(l->n->class != PPARAMOUT)
  2311. break;
  2312. if(l->n->sym_sym->def != l->n)
  2313. yyerror("%s is shadowed during return", l->n->sym_sym->node_name);
  2314. }
  2315. }
  2316. */
  2317. //TODO above
  2318. $$ = &Node{Pos: $<pos>1, Op: ORETURN, LL: $2}
  2319. }
  2320. list_stmt_list:
  2321. node_stmt
  2322. {
  2323. /*
  2324. $$ = nil;
  2325. if($1 != N)
  2326. $$ = list1($1);
  2327. */
  2328. $$ = nil
  2329. if $1 != nil {
  2330. $$ = $1.list()
  2331. }
  2332. }
  2333. | list_stmt_list ';' node_stmt
  2334. {
  2335. /*
  2336. $$ = $1;
  2337. if($3 != N)
  2338. $$ = list($$, $3);
  2339. */
  2340. $$ = $1
  2341. if $3 != nil {
  2342. $$ = append($$, $3)
  2343. }
  2344. }
  2345. list_new_name_list:
  2346. node_new_name
  2347. {
  2348. /*
  2349. $$ = list1($1);
  2350. */
  2351. $$ = $1.list()
  2352. }
  2353. | list_new_name_list ',' node_new_name
  2354. {
  2355. /*
  2356. $$ = list($1, $3);
  2357. */
  2358. $$ = append($1, $3)
  2359. }
  2360. list_dcl_name_list:
  2361. node_dcl_name
  2362. {
  2363. /*
  2364. $$ = list1($1);
  2365. */
  2366. $$ = $1.list()
  2367. }
  2368. | list_dcl_name_list ',' node_dcl_name
  2369. {
  2370. /*
  2371. $$ = list($1, $3);
  2372. */
  2373. $$ = append($1, $3)
  2374. }
  2375. list_expr_list:
  2376. node_expr
  2377. {
  2378. /*
  2379. $$ = list1($1);
  2380. */
  2381. $$ = $1.list()
  2382. }
  2383. | list_expr_list ',' node_expr
  2384. {
  2385. /*
  2386. $$ = list($1, $3);
  2387. */
  2388. $$ = append($1, $3)
  2389. }
  2390. list_expr_or_type_list:
  2391. node_expr_or_type
  2392. {
  2393. /*
  2394. $$ = list1($1);
  2395. */
  2396. $$ = $1.list()
  2397. }
  2398. | list_expr_or_type_list ',' node_expr_or_type
  2399. {
  2400. /*
  2401. $$ = list($1, $3);
  2402. */
  2403. $$ = append($1, $3)
  2404. }
  2405. /*
  2406. * list of combo of node_keyval and val
  2407. */
  2408. list_keyval_list:
  2409. node_keyval
  2410. {
  2411. /*
  2412. $$ = list1($1);
  2413. */
  2414. $$ = $1.list()
  2415. }
  2416. | node_complitexpr
  2417. {
  2418. /*
  2419. $$ = list1($1);
  2420. */
  2421. $$ = $1.list()
  2422. }
  2423. | list_keyval_list ',' node_keyval
  2424. {
  2425. /*
  2426. $$ = list($1, $3);
  2427. */
  2428. $$ = append($1, $3)
  2429. }
  2430. | list_keyval_list ',' node_complitexpr
  2431. {
  2432. /*
  2433. $$ = list($1, $3);
  2434. */
  2435. $$ = append($1, $3)
  2436. }
  2437. list_braced_keyval_list:
  2438. {
  2439. /*
  2440. $$ = nil;
  2441. */
  2442. $$ = nil
  2443. }
  2444. | list_keyval_list ocomma
  2445. {
  2446. /*
  2447. $$ = $1;
  2448. */
  2449. $$ = $1
  2450. }
  2451. /*
  2452. * optional things
  2453. */
  2454. osemi:
  2455. | ';'
  2456. ocomma:
  2457. | ','
  2458. node_oexpr:
  2459. {
  2460. /*
  2461. $$ = N;
  2462. */
  2463. $$ = nil
  2464. }
  2465. | node_expr
  2466. list_oexpr_list:
  2467. {
  2468. /*
  2469. $$ = nil;
  2470. */
  2471. $$ = nil
  2472. }
  2473. | list_expr_list
  2474. node_osimple_stmt:
  2475. {
  2476. /*
  2477. $$ = N;
  2478. */
  2479. $$ = nil
  2480. }
  2481. | node_simple_stmt
  2482. /*
  2483. list_ohidden_funarg_list:
  2484. {
  2485. $$ = nil;
  2486. }
  2487. | list_hidden_funarg_list
  2488. */
  2489. /*
  2490. list_ohidden_structdcl_list:
  2491. {
  2492. $$ = nil;
  2493. }
  2494. | list_hidden_structdcl_list
  2495. */
  2496. /*
  2497. list_ohidden_interfacedcl_list:
  2498. {
  2499. $$ = nil;
  2500. }
  2501. | list_hidden_interfacedcl_list
  2502. */
  2503. val_oliteral:
  2504. {
  2505. /*
  2506. $$.ctype = CTxxx;
  2507. */
  2508. $$ = nil
  2509. }
  2510. | val_LLITERAL
  2511. /*
  2512. * import syntax from package header
  2513. */
  2514. /*
  2515. hidden_import:
  2516. LIMPORT sym_LNAME val_LLITERAL ';'
  2517. {
  2518. importimport($2, $3.u.sval);
  2519. }
  2520. | LVAR sym_hidden_pkg_importsym type_hidden_type ';'
  2521. {
  2522. importvar($2, $3);
  2523. }
  2524. | LCONST sym_hidden_pkg_importsym '=' node_hidden_constant ';'
  2525. {
  2526. importconst($2, types[TIDEAL], $4);
  2527. }
  2528. | LCONST sym_hidden_pkg_importsym type_hidden_type '=' node_hidden_constant ';'
  2529. {
  2530. importconst($2, $3, $5);
  2531. }
  2532. | LTYPE type_hidden_pkgtype type_hidden_type ';'
  2533. {
  2534. importtype($2, $3);
  2535. }
  2536. | LFUNC node_hidden_fndcl list_fnbody ';'
  2537. {
  2538. if($2 == N)
  2539. break;
  2540. $2->inl = $3;
  2541. funcbody($2);
  2542. importlist = list(importlist, $2);
  2543. if(debug['E']) {
  2544. print("import [%Z] func %lN \n", importpkg->path, $2);
  2545. if(debug['l'] > 2 && $2->inl)
  2546. print("inl body:%+H\n", $2->inl);
  2547. }
  2548. }
  2549. */
  2550. /*
  2551. sym_hidden_pkg_importsym:
  2552. sym_hidden_importsym
  2553. {
  2554. $$ = $1;
  2555. structpkg = $$->pkg;
  2556. }
  2557. type_hidden_pkgtype:
  2558. sym_hidden_pkg_importsym
  2559. {
  2560. $$ = pkgtype($1);
  2561. importsym($1, OTYPE);
  2562. }
  2563. */
  2564. /*
  2565. * importing types
  2566. */
  2567. /*
  2568. type_hidden_type:
  2569. type_hidden_type_misc
  2570. | type_hidden_type_recv_chan
  2571. | type_hidden_type_func
  2572. */
  2573. /*
  2574. type_hidden_type_non_recv_chan:
  2575. type_hidden_type_misc
  2576. | type_hidden_type_func
  2577. */
  2578. /*
  2579. type_hidden_type_misc:
  2580. sym_hidden_importsym
  2581. {
  2582. $$ = pkgtype($1);
  2583. }
  2584. | sym_LNAME
  2585. {
  2586. // predefined node_name like uint8
  2587. $1 = pkglookup($1->node_name, builtinpkg);
  2588. if($1->def == N || $1->def->op != OTYPE) {
  2589. yyerror("%s is not a type", $1->node_name);
  2590. $$ = T;
  2591. } else
  2592. $$ = $1->def->type;
  2593. }
  2594. | '[' ']' type_hidden_type
  2595. {
  2596. $$ = aindex(N, $3);
  2597. }
  2598. | '[' val_LLITERAL ']' type_hidden_type
  2599. {
  2600. $$ = aindex(nodlit($2), $4);
  2601. }
  2602. | LMAP '[' type_hidden_type ']' type_hidden_type
  2603. {
  2604. $$ = maptype($3, $5);
  2605. }
  2606. | LSTRUCT '{' list_ohidden_structdcl_list '}'
  2607. {
  2608. $$ = tostruct($3);
  2609. }
  2610. | LINTERFACE '{' list_ohidden_interfacedcl_list '}'
  2611. {
  2612. $$ = tointerface($3);
  2613. }
  2614. | '*' type_hidden_type
  2615. {
  2616. $$ = ptrto($2);
  2617. }
  2618. | LCHAN type_hidden_type_non_recv_chan
  2619. {
  2620. $$ = typ(TCHAN);
  2621. $$->type = $2;
  2622. $$->chan = Cboth;
  2623. }
  2624. | LCHAN '(' type_hidden_type_recv_chan ')'
  2625. {
  2626. $$ = typ(TCHAN);
  2627. $$->type = $3;
  2628. $$->chan = Cboth;
  2629. }
  2630. | LCHAN LCOMM type_hidden_type
  2631. {
  2632. $$ = typ(TCHAN);
  2633. $$->type = $3;
  2634. $$->chan = Csend;
  2635. }
  2636. */
  2637. /*
  2638. type_hidden_type_recv_chan:
  2639. LCOMM LCHAN type_hidden_type
  2640. {
  2641. $$ = typ(TCHAN);
  2642. $$->type = $3;
  2643. $$->chan = Crecv;
  2644. }
  2645. */
  2646. /*
  2647. type_hidden_type_func:
  2648. LFUNC '(' list_ohidden_funarg_list ')' list_ohidden_funres
  2649. {
  2650. $$ = functype(nil, $3, $5);
  2651. }
  2652. */
  2653. /*
  2654. node_hidden_fun…

Large files files are truncated, but you can click here to view the full file