PageRenderTime 120ms CodeModel.GetById 18ms app.highlight 91ms RepoModel.GetById 1ms app.codeStats 1ms

/src/cmd/gc/go.y

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