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