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