PageRenderTime 102ms CodeModel.GetById 2ms app.highlight 89ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cmd/gc/go.y

https://bitbucket.org/cyberroadie/go-sctp
Happy | 2089 lines | 1896 code | 193 blank | 0 comment | 0 complexity | 7fd0f82143240a549b03a98c1c70130c 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
  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 else 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 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	else
 665	{
 666		popdcl();
 667		$$ = $3;
 668		if($7 != N)
 669			$$->nelse = list1($7);
 670	}
 671
 672else:
 673	{
 674		$$ = N;
 675	}
 676|	LELSE if_stmt
 677	{
 678		$$ = $2;
 679	}
 680|	LELSE compound_stmt
 681	{
 682		$$ = $2;
 683	}
 684
 685switch_stmt:
 686	LSWITCH
 687	{
 688		markdcl();
 689	}
 690	if_header
 691	{
 692		Node *n;
 693		n = $3->ntest;
 694		if(n != N && n->op != OTYPESW)
 695			n = N;
 696		typesw = nod(OXXX, typesw, n);
 697	}
 698	LBODY caseblock_list '}'
 699	{
 700		$$ = $3;
 701		$$->op = OSWITCH;
 702		$$->list = $6;
 703		typesw = typesw->left;
 704		popdcl();
 705	}
 706
 707select_stmt:
 708	LSELECT
 709	{
 710		typesw = nod(OXXX, typesw, N);
 711	}
 712	LBODY caseblock_list '}'
 713	{
 714		$$ = nod(OSELECT, N, N);
 715		$$->lineno = typesw->lineno;
 716		$$->list = $4;
 717		typesw = typesw->left;
 718	}
 719
 720/*
 721 * expressions
 722 */
 723expr:
 724	uexpr
 725|	expr LOROR expr
 726	{
 727		$$ = nod(OOROR, $1, $3);
 728	}
 729|	expr LANDAND expr
 730	{
 731		$$ = nod(OANDAND, $1, $3);
 732	}
 733|	expr LEQ expr
 734	{
 735		$$ = nod(OEQ, $1, $3);
 736	}
 737|	expr LNE expr
 738	{
 739		$$ = nod(ONE, $1, $3);
 740	}
 741|	expr LLT expr
 742	{
 743		$$ = nod(OLT, $1, $3);
 744	}
 745|	expr LLE expr
 746	{
 747		$$ = nod(OLE, $1, $3);
 748	}
 749|	expr LGE expr
 750	{
 751		$$ = nod(OGE, $1, $3);
 752	}
 753|	expr LGT expr
 754	{
 755		$$ = nod(OGT, $1, $3);
 756	}
 757|	expr '+' expr
 758	{
 759		$$ = nod(OADD, $1, $3);
 760	}
 761|	expr '-' expr
 762	{
 763		$$ = nod(OSUB, $1, $3);
 764	}
 765|	expr '|' expr
 766	{
 767		$$ = nod(OOR, $1, $3);
 768	}
 769|	expr '^' expr
 770	{
 771		$$ = nod(OXOR, $1, $3);
 772	}
 773|	expr '*' expr
 774	{
 775		$$ = nod(OMUL, $1, $3);
 776	}
 777|	expr '/' expr
 778	{
 779		$$ = nod(ODIV, $1, $3);
 780	}
 781|	expr '%' expr
 782	{
 783		$$ = nod(OMOD, $1, $3);
 784	}
 785|	expr '&' expr
 786	{
 787		$$ = nod(OAND, $1, $3);
 788	}
 789|	expr LANDNOT expr
 790	{
 791		$$ = nod(OANDNOT, $1, $3);
 792	}
 793|	expr LLSH expr
 794	{
 795		$$ = nod(OLSH, $1, $3);
 796	}
 797|	expr LRSH expr
 798	{
 799		$$ = nod(ORSH, $1, $3);
 800	}
 801	/* not an expression anymore, but left in so we can give a good error */
 802|	expr LCOMM expr
 803	{
 804		$$ = nod(OSEND, $1, $3);
 805	}
 806
 807uexpr:
 808	pexpr
 809|	'*' uexpr
 810	{
 811		$$ = nod(OIND, $2, N);
 812	}
 813|	'&' uexpr
 814	{
 815		if($2->op == OCOMPLIT) {
 816			// Special case for &T{...}: turn into (*T){...}.
 817			$$ = $2;
 818			$$->right = nod(OIND, $$->right, N);
 819			$$->right->implicit = 1;
 820		} else {
 821			$$ = nod(OADDR, $2, N);
 822		}
 823	}
 824|	'+' uexpr
 825	{
 826		$$ = nod(OPLUS, $2, N);
 827	}
 828|	'-' uexpr
 829	{
 830		$$ = nod(OMINUS, $2, N);
 831	}
 832|	'!' uexpr
 833	{
 834		$$ = nod(ONOT, $2, N);
 835	}
 836|	'~' uexpr
 837	{
 838		yyerror("the bitwise complement operator is ^");
 839		$$ = nod(OCOM, $2, N);
 840	}
 841|	'^' uexpr
 842	{
 843		$$ = nod(OCOM, $2, N);
 844	}
 845|	LCOMM uexpr
 846	{
 847		$$ = nod(ORECV, $2, N);
 848	}
 849
 850/*
 851 * call-like statements that
 852 * can be preceded by 'defer' and 'go'
 853 */
 854pseudocall:
 855	pexpr '(' ')'
 856	{
 857		$$ = nod(OCALL, $1, N);
 858	}
 859|	pexpr '(' expr_or_type_list ocomma ')'
 860	{
 861		$$ = nod(OCALL, $1, N);
 862		$$->list = $3;
 863	}
 864|	pexpr '(' expr_or_type_list LDDD ocomma ')'
 865	{
 866		$$ = nod(OCALL, $1, N);
 867		$$->list = $3;
 868		$$->isddd = 1;
 869	}
 870
 871pexpr_no_paren:
 872	LLITERAL
 873	{
 874		$$ = nodlit($1);
 875	}
 876|	name
 877|	pexpr '.' sym
 878	{
 879		if($1->op == OPACK) {
 880			Sym *s;
 881			s = restrictlookup($3->name, $1->pkg);
 882			$1->used = 1;
 883			$$ = oldname(s);
 884			break;
 885		}
 886		$$ = nod(OXDOT, $1, newname($3));
 887	}
 888|	pexpr '.' '(' expr_or_type ')'
 889	{
 890		$$ = nod(ODOTTYPE, $1, $4);
 891	}
 892|	pexpr '.' '(' LTYPE ')'
 893	{
 894		$$ = nod(OTYPESW, N, $1);
 895	}
 896|	pexpr '[' expr ']'
 897	{
 898		$$ = nod(OINDEX, $1, $3);
 899	}
 900|	pexpr '[' oexpr ':' oexpr ']'
 901	{
 902		$$ = nod(OSLICE, $1, nod(OKEY, $3, $5));
 903	}
 904|	pseudocall
 905|	convtype '(' expr ')'
 906	{
 907		// conversion
 908		$$ = nod(OCALL, $1, N);
 909		$$->list = list1($3);
 910	}
 911|	comptype lbrace start_complit braced_keyval_list '}'
 912	{
 913		$$ = $3;
 914		$$->right = $1;
 915		$$->list = $4;
 916		fixlbrace($2);
 917	}
 918|	pexpr_no_paren '{' start_complit braced_keyval_list '}'
 919	{
 920		$$ = $3;
 921		$$->right = $1;
 922		$$->list = $4;
 923	}
 924|	'(' expr_or_type ')' '{' start_complit braced_keyval_list '}'
 925	{
 926		yyerror("cannot parenthesize type in composite literal");
 927		$$ = $5;
 928		$$->right = $2;
 929		$$->list = $6;
 930	}
 931|	fnliteral
 932
 933start_complit:
 934	{
 935		// composite expression.
 936		// make node early so we get the right line number.
 937		$$ = nod(OCOMPLIT, N, N);
 938	}
 939
 940keyval:
 941	expr ':' complitexpr
 942	{
 943		$$ = nod(OKEY, $1, $3);
 944	}
 945
 946complitexpr:
 947	expr
 948|	'{' start_complit braced_keyval_list '}'
 949	{
 950		$$ = $2;
 951		$$->list = $3;
 952	}
 953
 954pexpr:
 955	pexpr_no_paren
 956|	'(' expr_or_type ')'
 957	{
 958		$$ = $2;
 959		
 960		// Need to know on lhs of := whether there are ( ).
 961		// Don't bother with the OPAREN in other cases:
 962		// it's just a waste of memory and time.
 963		switch($$->op) {
 964		case ONAME:
 965		case ONONAME:
 966		case OPACK:
 967		case OTYPE:
 968		case OLITERAL:
 969			$$ = nod(OPAREN, $$, N);
 970		}
 971	}
 972
 973expr_or_type:
 974	expr
 975|	non_expr_type	%prec PreferToRightParen
 976
 977name_or_type:
 978	ntype
 979
 980lbrace:
 981	LBODY
 982	{
 983		$$ = LBODY;
 984	}
 985|	'{'
 986	{
 987		$$ = '{';
 988	}
 989
 990/*
 991 * names and types
 992 *	newname is used before declared
 993 *	oldname is used after declared
 994 */
 995new_name:
 996	sym
 997	{
 998		if($1 == S)
 999			$$ = N;
1000		else
1001			$$ = newname($1);
1002	}
1003
1004dcl_name:
1005	sym
1006	{
1007		$$ = dclname($1);
1008	}
1009
1010onew_name:
1011	{
1012		$$ = N;
1013	}
1014|	new_name
1015
1016sym:
1017	LNAME
1018	{
1019		$$ = $1;
1020		// during imports, unqualified non-exported identifiers are from builtinpkg
1021		if(importpkg != nil && !exportname($1->name))
1022			$$ = pkglookup($1->name, builtinpkg);
1023	}
1024|	hidden_importsym
1025|	'?'
1026	{
1027		$$ = S;
1028	}
1029
1030hidden_importsym:
1031	'@' LLITERAL '.' LNAME
1032	{
1033		if($2.u.sval->len == 0)
1034			$$ = pkglookup($4->name, importpkg);
1035		else
1036			$$ = pkglookup($4->name, mkpkg($2.u.sval));
1037	}
1038
1039name:
1040	sym	%prec NotParen
1041	{
1042		$$ = oldname($1);
1043		if($$->pack != N)
1044			$$->pack->used = 1;
1045	}
1046
1047labelname:
1048	new_name
1049
1050/*
1051 * to avoid parsing conflicts, type is split into
1052 *	channel types
1053 *	function types
1054 *	parenthesized types
1055 *	any other type
1056 * the type system makes additional restrictions,
1057 * but those are not implemented in the grammar.
1058 */
1059dotdotdot:
1060	LDDD
1061	{
1062		yyerror("final argument in variadic function missing type");
1063		$$ = nod(ODDD, typenod(typ(TINTER)), N);
1064	}
1065|	LDDD ntype
1066	{
1067		$$ = nod(ODDD, $2, N);
1068	}
1069
1070ntype:
1071	recvchantype
1072|	fntype
1073|	othertype
1074|	ptrtype
1075|	dotname
1076|	'(' ntype ')'
1077	{
1078		$$ = nod(OTPAREN, $2, N);
1079	}
1080
1081non_expr_type:
1082	recvchantype
1083|	fntype
1084|	othertype
1085|	'*' non_expr_type
1086	{
1087		$$ = nod(OIND, $2, N);
1088	}
1089
1090non_recvchantype:
1091	fntype
1092|	othertype
1093|	ptrtype
1094|	dotname
1095|	'(' ntype ')'
1096	{
1097		$$ = nod(OTPAREN, $2, N);
1098	}
1099
1100convtype:
1101	fntype
1102|	othertype
1103
1104comptype:
1105	othertype
1106
1107fnret_type:
1108	recvchantype
1109|	fntype
1110|	othertype
1111|	ptrtype
1112|	dotname
1113
1114dotname:
1115	name
1116|	name '.' sym
1117	{
1118		if($1->op == OPACK) {
1119			Sym *s;
1120			s = restrictlookup($3->name, $1->pkg);
1121			$1->used = 1;
1122			$$ = oldname(s);
1123			break;
1124		}
1125		$$ = nod(OXDOT, $1, newname($3));
1126	}
1127
1128othertype:
1129	'[' oexpr ']' ntype
1130	{
1131		$$ = nod(OTARRAY, $2, $4);
1132	}
1133|	'[' LDDD ']' ntype
1134	{
1135		// array literal of nelem
1136		$$ = nod(OTARRAY, nod(ODDD, N, N), $4);
1137	}
1138|	LCHAN non_recvchantype
1139	{
1140		$$ = nod(OTCHAN, $2, N);
1141		$$->etype = Cboth;
1142	}
1143|	LCHAN LCOMM ntype
1144	{
1145		$$ = nod(OTCHAN, $3, N);
1146		$$->etype = Csend;
1147	}
1148|	LMAP '[' ntype ']' ntype
1149	{
1150		$$ = nod(OTMAP, $3, $5);
1151	}
1152|	structtype
1153|	interfacetype
1154
1155ptrtype:
1156	'*' ntype
1157	{
1158		$$ = nod(OIND, $2, N);
1159	}
1160
1161recvchantype:
1162	LCOMM LCHAN ntype
1163	{
1164		$$ = nod(OTCHAN, $3, N);
1165		$$->etype = Crecv;
1166	}
1167
1168structtype:
1169	LSTRUCT lbrace structdcl_list osemi '}'
1170	{
1171		$$ = nod(OTSTRUCT, N, N);
1172		$$->list = $3;
1173		fixlbrace($2);
1174	}
1175|	LSTRUCT lbrace '}'
1176	{
1177		$$ = nod(OTSTRUCT, N, N);
1178		fixlbrace($2);
1179	}
1180
1181interfacetype:
1182	LINTERFACE lbrace interfacedcl_list osemi '}'
1183	{
1184		$$ = nod(OTINTER, N, N);
1185		$$->list = $3;
1186		fixlbrace($2);
1187	}
1188|	LINTERFACE lbrace '}'
1189	{
1190		$$ = nod(OTINTER, N, N);
1191		fixlbrace($2);
1192	}
1193
1194/*
1195 * function stuff
1196 * all in one place to show how crappy it all is
1197 */
1198xfndcl:
1199	LFUNC fndcl fnbody
1200	{
1201		$$ = $2;
1202		if($$ == N)
1203			break;
1204		$$->nbody = $3;
1205		$$->endlineno = lineno;
1206		funcbody($$);
1207	}
1208
1209fndcl:
1210	sym '(' oarg_type_list_ocomma ')' fnres
1211	{
1212		Node *t;
1213
1214		$$ = N;
1215		$3 = checkarglist($3, 1);
1216
1217		if(strcmp($1->name, "init") == 0) {
1218			$1 = renameinit();
1219			if($3 != nil || $5 != nil)
1220				yyerror("func init must have no arguments and no return values");
1221		}
1222		if(strcmp(localpkg->name, "main") == 0 && strcmp($1->name, "main") == 0) {
1223			if($3 != nil || $5 != nil)
1224				yyerror("func main must have no arguments and no return values");
1225		}
1226
1227		t = nod(OTFUNC, N, N);
1228		t->list = $3;
1229		t->rlist = $5;
1230
1231		$$ = nod(ODCLFUNC, N, N);
1232		$$->nname = newname($1);
1233		$$->nname->defn = $$;
1234		$$->nname->ntype = t;		// TODO: check if nname already has an ntype
1235		declare($$->nname, PFUNC);
1236
1237		funchdr($$);
1238	}
1239|	'(' oarg_type_list_ocomma ')' sym '(' oarg_type_list_ocomma ')' fnres
1240	{
1241		Node *rcvr, *t;
1242
1243		$$ = N;
1244		$2 = checkarglist($2, 0);
1245		$6 = checkarglist($6, 1);
1246
1247		if($2 == nil) {
1248			yyerror("method has no receiver");
1249			break;
1250		}
1251		if($2->next != nil) {
1252			yyerror("method has multiple receivers");
1253			break;
1254		}
1255		rcvr = $2->n;
1256		if(rcvr->op != ODCLFIELD) {
1257			yyerror("bad receiver in method");
1258			break;
1259		}
1260		if(rcvr->right->op == OTPAREN || (rcvr->right->op == OIND && rcvr->right->left->op == OTPAREN))
1261			yyerror("cannot parenthesize receiver type");
1262
1263		t = nod(OTFUNC, rcvr, N);
1264		t->list = $6;
1265		t->rlist = $8;
1266
1267		$$ = nod(ODCLFUNC, N, N);
1268		$$->shortname = newname($4);
1269		$$->nname = methodname1($$->shortname, rcvr->right);
1270		$$->nname->defn = $$;
1271		$$->nname->ntype = t;
1272		declare($$->nname, PFUNC);
1273
1274		funchdr($$);
1275	}
1276
1277hidden_fndcl:
1278	hidden_pkg_importsym '(' ohidden_funarg_list ')' ohidden_funres
1279	{
1280		Sym *s;
1281		Type *t;
1282
1283		$$ = N;
1284
1285		s = $1;
1286		t = functype(N, $3, $5);
1287
1288		importsym(s, ONAME);
1289		if(s->def != N && s->def->op == ONAME) {
1290			if(eqtype(t, s->def->type))
1291				break;
1292			yyerror("inconsistent definition for func %S during import\n\t%T\n\t%T", s, s->def->type, t);
1293		}
1294
1295		$$ = newname(s);
1296		$$->type = t;
1297		declare($$, PFUNC);
1298
1299		funchdr($$);
1300	}
1301|	'(' hidden_funarg_list ')' sym '(' ohidden_funarg_list ')' ohidden_funres
1302	{
1303		$$ = methodname1(newname($4), $2->n->right); 
1304		$$->type = functype($2->n, $6, $8);
1305
1306		checkwidth($$->type);
1307		addmethod($4, $$->type, 0);
1308		funchdr($$);
1309		
1310		// inl.c's inlnode in on a dotmeth node expects to find the inlineable body as
1311		// (dotmeth's type)->nname->inl, and dotmeth's type has been pulled
1312		// out by typecheck's lookdot as this $$->ttype.  So by providing
1313		// this back link here we avoid special casing there.
1314		$$->type->nname = $$;
1315	}
1316
1317fntype:
1318	LFUNC '(' oarg_type_list_ocomma ')' fnres
1319	{
1320		$3 = checkarglist($3, 1);
1321		$$ = nod(OTFUNC, N, N);
1322		$$->list = $3;
1323		$$->rlist = $5;
1324	}
1325
1326fnbody:
1327	{
1328		$$ = nil;
1329	}
1330|	'{' stmt_list '}'
1331	{
1332		$$ = $2;
1333		if($$ == nil)
1334			$$ = list1(nod(OEMPTY, N, N));
1335	}
1336
1337fnres:
1338	%prec NotParen
1339	{
1340		$$ = nil;
1341	}
1342|	fnret_type
1343	{
1344		$$ = list1(nod(ODCLFIELD, N, $1));
1345	}
1346|	'(' oarg_type_list_ocomma ')'
1347	{
1348		$2 = checkarglist($2, 0);
1349		$$ = $2;
1350	}
1351
1352fnlitdcl:
1353	fntype
1354	{
1355		closurehdr($1);
1356	}
1357
1358fnliteral:
1359	fnlitdcl lbrace stmt_list '}'
1360	{
1361		$$ = closurebody($3);
1362		fixlbrace($2);
1363	}
1364|	fnlitdcl error
1365	{
1366		$$ = closurebody(nil);
1367	}
1368
1369/*
1370 * lists of things
1371 * note that they are left recursive
1372 * to conserve yacc stack. they need to
1373 * be reversed to interpret correctly
1374 */
1375xdcl_list:
1376	{
1377		$$ = nil;
1378	}
1379|	xdcl_list xdcl ';'
1380	{
1381		$$ = concat($1, $2);
1382		if(nsyntaxerrors == 0)
1383			testdclstack();
1384	}
1385
1386vardcl_list:
1387	vardcl
1388|	vardcl_list ';' vardcl
1389	{
1390		$$ = concat($1, $3);
1391	}
1392
1393constdcl_list:
1394	constdcl1
1395|	constdcl_list ';' constdcl1
1396	{
1397		$$ = concat($1, $3);
1398	}
1399
1400typedcl_list:
1401	typedcl
1402	{
1403		$$ = list1($1);
1404	}
1405|	typedcl_list ';' typedcl
1406	{
1407		$$ = list($1, $3);
1408	}
1409
1410structdcl_list:
1411	structdcl
1412|	structdcl_list ';' structdcl
1413	{
1414		$$ = concat($1, $3);
1415	}
1416
1417interfacedcl_list:
1418	interfacedcl
1419	{
1420		$$ = list1($1);
1421	}
1422|	interfacedcl_list ';' interfacedcl
1423	{
1424		$$ = list($1, $3);
1425	}
1426
1427structdcl:
1428	new_name_list ntype oliteral
1429	{
1430		NodeList *l;
1431
1432		Node *n;
1433		l = $1;
1434		if(l != nil && l->next == nil && l->n == nil) {
1435			// ? symbol, during import
1436			n = $2;
1437			if(n->op == OIND)
1438				n = n->left;
1439			n = embedded(n->sym);
1440			n->right = $2;
1441			n->val = $3;
1442			$$ = list1(n);
1443			break;
1444		}
1445
1446		for(l=$1; l; l=l->next) {
1447			l->n = nod(ODCLFIELD, l->n, $2);
1448			l->n->val = $3;
1449		}
1450	}
1451|	embed oliteral
1452	{
1453		$1->val = $2;
1454		$$ = list1($1);
1455	}
1456|	'(' embed ')' oliteral
1457	{
1458		$2->val = $4;
1459		$$ = list1($2);
1460		yyerror("cannot parenthesize embedded type");
1461	}
1462|	'*' embed oliteral
1463	{
1464		$2->right = nod(OIND, $2->right, N);
1465		$2->val = $3;
1466		$$ = list1($2);
1467	}
1468|	'(' '*' embed ')' oliteral
1469	{
1470		$3->right = nod(OIND, $3->right, N);
1471		$3->val = $5;
1472		$$ = list1($3);
1473		yyerror("cannot parenthesize embedded type");
1474	}
1475|	'*' '(' embed ')' oliteral
1476	{
1477		$3->right = nod(OIND, $3->right, N);
1478		$3->val = $5;
1479		$$ = list1($3);
1480		yyerror("cannot parenthesize embedded type");
1481	}
1482
1483packname:
1484	LNAME
1485	{
1486		Node *n;
1487
1488		$$ = $1;
1489		n = oldname($1);
1490		if(n->pack != N)
1491			n->pack->used = 1;
1492	}
1493|	LNAME '.' sym
1494	{
1495		Pkg *pkg;
1496
1497		if($1->def == N || $1->def->op != OPACK) {
1498			yyerror("%S is not a package", $1);
1499			pkg = localpkg;
1500		} else {
1501			$1->def->used = 1;
1502			pkg = $1->def->pkg;
1503		}
1504		$$ = restrictlookup($3->name, pkg);
1505	}
1506
1507embed:
1508	packname
1509	{
1510		$$ = embedded($1);
1511	}
1512
1513interfacedcl:
1514	new_name indcl
1515	{
1516		$$ = nod(ODCLFIELD, $1, $2);
1517		ifacedcl($$);
1518	}
1519|	packname
1520	{
1521		$$ = nod(ODCLFIELD, N, oldname($1));
1522	}
1523|	'(' packname ')'
1524	{
1525		$$ = nod(ODCLFIELD, N, oldname($2));
1526		yyerror("cannot parenthesize embedded type");
1527	}
1528
1529indcl:
1530	'(' oarg_type_list_ocomma ')' fnres
1531	{
1532		// without func keyword
1533		$2 = checkarglist($2, 1);
1534		$$ = nod(OTFUNC, fakethis(), N);
1535		$$->list = $2;
1536		$$->rlist = $4;
1537	}
1538
1539/*
1540 * function arguments.
1541 */
1542arg_type:
1543	name_or_type
1544|	sym name_or_type
1545	{
1546		$$ = nod(ONONAME, N, N);
1547		$$->sym = $1;
1548		$$ = nod(OKEY, $$, $2);
1549	}
1550|	sym dotdotdot
1551	{
1552		$$ = nod(ONONAME, N, N);
1553		$$->sym = $1;
1554		$$ = nod(OKEY, $$, $2);
1555	}
1556|	dotdotdot
1557
1558arg_type_list:
1559	arg_type
1560	{
1561		$$ = list1($1);
1562	}
1563|	arg_type_list ',' arg_type
1564	{
1565		$$ = list($1, $3);
1566	}
1567
1568oarg_type_list_ocomma:
1569	{
1570		$$ = nil;
1571	}
1572|	arg_type_list ocomma
1573	{
1574		$$ = $1;
1575	}
1576
1577/*
1578 * statement
1579 */
1580stmt:
1581	{
1582		$$ = N;
1583	}
1584|	compound_stmt
1585|	common_dcl
1586	{
1587		$$ = liststmt($1);
1588	}
1589|	non_dcl_stmt
1590|	error
1591	{
1592		$$ = N;
1593	}
1594
1595non_dcl_stmt:
1596	simple_stmt
1597|	for_stmt
1598|	switch_stmt
1599|	select_stmt
1600|	if_stmt
1601|	labelname ':'
1602	{
1603		$1 = nod(OLABEL, $1, N);
1604		$1->sym = dclstack;  // context, for goto restrictions
1605	}
1606	stmt
1607	{
1608		NodeList *l;
1609
1610		$1->defn = $4;
1611		l = list1($1);
1612		if($4)
1613			l = list(l, $4);
1614		$$ = liststmt(l);
1615	}
1616|	LFALL
1617	{
1618		// will be converted to OFALL
1619		$$ = nod(OXFALL, N, N);
1620	}
1621|	LBREAK onew_name
1622	{
1623		$$ = nod(OBREAK, $2, N);
1624	}
1625|	LCONTINUE onew_name
1626	{
1627		$$ = nod(OCONTINUE, $2, N);
1628	}
1629|	LGO pseudocall
1630	{
1631		$$ = nod(OPROC, $2, N);
1632	}
1633|	LDEFER pseudocall
1634	{
1635		$$ = nod(ODEFER, $2, N);
1636	}
1637|	LGOTO new_name
1638	{
1639		$$ = nod(OGOTO, $2, N);
1640		$$->sym = dclstack;  // context, for goto restrictions
1641	}
1642|	LRETURN oexpr_list
1643	{
1644		$$ = nod(ORETURN, N, N);
1645		$$->list = $2;
1646		if($$->list == nil && curfn != N) {
1647			NodeList *l;
1648
1649			for(l=curfn->dcl; l; l=l->next) {
1650				if(l->n->class == PPARAM)
1651					continue;
1652				if(l->n->class != PPARAMOUT)
1653					break;
1654				if(l->n->sym->def != l->n)
1655					yyerror("%s is shadowed during return", l->n->sym->name);
1656			}
1657		}
1658	}
1659
1660stmt_list:
1661	stmt
1662	{
1663		$$ = nil;
1664		if($1 != N)
1665			$$ = list1($1);
1666	}
1667|	stmt_list ';' stmt
1668	{
1669		$$ = $1;
1670		if($3 != N)
1671			$$ = list($$, $3);
1672	}
1673
1674new_name_list:
1675	new_name
1676	{
1677		$$ = list1($1);
1678	}
1679|	new_name_list ',' new_name
1680	{
1681		$$ = list($1, $3);
1682	}
1683
1684dcl_name_list:
1685	dcl_name
1686	{
1687		$$ = list1($1);
1688	}
1689|	dcl_name_list ',' dcl_name
1690	{
1691		$$ = list($1, $3);
1692	}
1693
1694expr_list:
1695	expr
1696	{
1697		$$ = list1($1);
1698	}
1699|	expr_list ',' expr
1700	{
1701		$$ = list($1, $3);
1702	}
1703
1704expr_or_type_list:
1705	expr_or_type
1706	{
1707		$$ = list1($1);
1708	}
1709|	expr_or_type_list ',' expr_or_type
1710	{
1711		$$ = list($1, $3);
1712	}
1713
1714/*
1715 * list of combo of keyval and val
1716 */
1717keyval_list:
1718	keyval
1719	{
1720		$$ = list1($1);
1721	}
1722|	complitexpr
1723	{
1724		$$ = list1($1);
1725	}
1726|	keyval_list ',' keyval
1727	{
1728		$$ = list($1, $3);
1729	}
1730|	keyval_list ',' complitexpr
1731	{
1732		$$ = list($1, $3);
1733	}
1734
1735braced_keyval_list:
1736	{
1737		$$ = nil;
1738	}
1739|	keyval_list ocomma
1740	{
1741		$$ = $1;
1742	}
1743
1744/*
1745 * optional things
1746 */
1747osemi:
1748|	';'
1749
1750ocomma:
1751|	','
1752
1753oexpr:
1754	{
1755		$$ = N;
1756	}
1757|	expr
1758
1759oexpr_list:
1760	{
1761		$$ = nil;
1762	}
1763|	expr_list
1764
1765osimple_stmt:
1766	{
1767		$$ = N;
1768	}
1769|	simple_stmt
1770
1771ohidden_funarg_list:
1772	{
1773		$$ = nil;
1774	}
1775|	hidden_funarg_list
1776
1777ohidden_structdcl_list:
1778	{
1779		$$ = nil;
1780	}
1781|	hidden_structdcl_list
1782
1783ohidden_interfacedcl_list:
1784	{
1785		$$ = nil;
1786	}
1787|	hidden_interfacedcl_list
1788
1789oliteral:
1790	{
1791		$$.ctype = CTxxx;
1792	}
1793|	LLITERAL
1794
1795/*
1796 * import syntax from package header
1797 */
1798hidden_import:
1799	LIMPORT LNAME LLITERAL ';'
1800	{
1801		importimport($2, $3.u.sval);
1802	}
1803|	LVAR hidden_pkg_importsym hidden_type ';'
1804	{
1805		importvar($2, $3);
1806	}
1807|	LCONST hidden_pkg_importsym '=' hidden_constant ';'
1808	{
1809		importconst($2, types[TIDEAL], $4);
1810	}
1811|	LCONST hidden_pkg_importsym hidden_type '=' hidden_constant ';'
1812	{
1813		importconst($2, $3, $5);
1814	}
1815|	LTYPE hidden_pkgtype hidden_type ';'
1816	{
1817		importtype($2, $3);
1818	}
1819|	LFUNC hidden_fndcl fnbody ';'
1820	{
1821		if($2 == N)
1822			break;
1823
1824		$2->inl = $3;
1825
1826		funcbody($2);
1827		importlist = list(importlist, $2);
1828
1829		if(debug['E']) {
1830			print("import [%Z] func %lN \n", importpkg->path, $2);
1831			if(debug['l'] > 2 && $2->inl)
1832				print("inl body:%+H\n", $2->inl);
1833		}
1834	}
1835
1836hidden_pkg_importsym:
1837	hidden_importsym
1838	{
1839		$$ = $1;
1840		structpkg = $$->pkg;
1841	}
1842
1843hidden_pkgtype:
1844	hidden_pkg_importsym
1845	{
1846		$$ = pkgtype($1);
1847		importsym($1, OTYPE);
1848	}
1849
1850/*
1851 *  importing types
1852 */
1853
1854hidden_type:
1855	hidden_type_misc
1856|	hidden_type_recv_chan
1857|	hidden_type_func
1858
1859hidden_type_non_recv_chan:
1860	hidden_type_misc
1861|	hidden_type_func
1862
1863hidden_type_misc:
1864	hidden_importsym
1865	{
1866		$$ = pkgtype($1);
1867	}
1868|	LNAME
1869	{
1870		// predefined name like uint8
1871		$1 = pkglookup($1->name, builtinpkg);
1872		if($1->def == N || $1->def->op != OTYPE) {
1873			yyerror("%s is not a type", $1->name);
1874			$$ = T;
1875		} else
1876			$$ = $1->def->type;
1877	}
1878|	'[' ']' hidden_type
1879	{
1880		$$ = aindex(N, $3);
1881	}
1882|	'[' LLITERAL ']' hidden_type
1883	{
1884		$$ = aindex(nodlit($2), $4);
1885	}
1886|	LMAP '[' hidden_type ']' hidden_type
1887	{
1888		$$ = maptype($3, $5);
1889	}
1890|	LSTRUCT '{' ohidden_structdcl_list '}'
1891	{
1892		$$ = tostruct($3);
1893	}
1894|	LINTERFACE '{' ohidden_interfacedcl_list '}'
1895	{
1896		$$ = tointerface($3);
1897	}
1898|	'*' hidden_type
1899	{
1900		$$ = ptrto($2);
1901	}
1902|	LCHAN hidden_type_non_recv_chan
1903	{
1904		$$ = typ(TCHAN);
1905		$$->type = $2;
1906		$$->chan = Cboth;
1907	}
1908|	LCHAN '(' hidden_type_recv_chan ')'
1909	{
1910		$$ = typ(TCHAN);
1911		$$->type = $3;
1912		$$->chan = Cboth;
1913	}
1914|	LCHAN LCOMM hidden_type
1915	{
1916		$$ = typ(TCHAN);
1917		$$->type = $3;
1918		$$->chan = Csend;
1919	}
1920
1921hidden_type_recv_chan:
1922	LCOMM LCHAN hidden_type
1923	{
1924		$$ = typ(TCHAN);
1925		$$->type = $3;
1926		$$->chan = Crecv;
1927	}
1928
1929hidden_type_func:
1930	LFUNC '(' ohidden_funarg_list ')' ohidden_funres
1931	{
1932		$$ = functype(nil, $3, $5);
1933	}
1934
1935hidden_funarg:
1936	sym hidden_type oliteral
1937	{
1938		$$ = nod(ODCLFIELD, N, typenod($2));
1939		if($1)
1940			$$->left = newname($1);
1941		$$->val = $3;
1942	}
1943|	sym LDDD hidden_type oliteral
1944	{
1945		Type *t;
1946	
1947		t = typ(TARRAY);
1948		t->bound = -1;
1949		t->type = $3;
1950
1951		$$ = nod(ODCLFIELD, N, typenod(t));
1952		if($1)
1953			$$->left = newname($1);
1954		$$->isddd = 1;
1955		$$->val = $4;
1956	}
1957
1958hidden_structdcl:
1959	sym hidden_type oliteral
1960	{
1961		Sym *s;
1962
1963		if($1 != S) {
1964			$$ = nod(ODCLFIELD, newname($1), typenod($2));
1965			$$->val = $3;
1966		} else {
1967			s = $2->sym;
1968			if(s == S && isptr[$2->etype])
1969				s = $2->type->sym;
1970			$$ = embedded(s);
1971			$$->right = typenod($2);
1972			$$->val = $3;
1973		}
1974	}
1975
1976hidden_interfacedcl:
1977	sym '(' ohidden_funarg_list ')' ohidden_funres
1978	{
1979		$$ = nod(ODCLFIELD, newname($1), typenod(functype(fakethis(), $3, $5)));
1980	}
1981|	hidden_type
1982	{
1983		$$ = nod(ODCLFIELD, N, typenod($1));
1984	}
1985
1986ohidden_funres:
1987	{
1988		$$ = nil;
1989	}
1990|	hidden_funres
1991
1992hidden_funres:
1993	'(' ohidden_funarg_list ')'
1994	{
1995		$$ = $2;
1996	}
1997|	hidden_type
1998	{
1999		$$ = list1(nod(ODCLFIELD, N, typenod($1)));
2000	}
2001
2002/*
2003 *  importing constants
2004 */
2005
2006hidden_literal:
2007	LLITERAL
2008	{
2009		$$ = nodlit($1);
2010	}
2011|	'-' LLITERAL
2012	{
2013		$$ = nodlit($2);
2014		switch($$->val.ctype){
2015		case CTINT:
2016		case CTRUNE:
2017			mpnegfix($$->val.u.xval);
2018			break;
2019		case CTFLT:
2020			mpnegflt($$->val.u.fval);
2021			break;
2022		default:
2023			yyerror("bad negated constant");
2024		}
2025	}
2026|	sym
2027	{
2028		$$ = oldname(pkglookup($1->name, builtinpkg));
2029		if($$->op != OLITERAL)
2030			yyerror("bad constant %S", $$->sym);
2031	}
2032
2033hidden_constant:
2034	hidden_literal
2035|	'(' hidden_literal '+' hidden_literal ')'
2036	{
2037		if($2->val.ctype == CTRUNE && $4->val.ctype == CTINT) {
2038			$$ = $2;
2039			mpaddfixfix($2->val.u.xval, $4->val.u.xval, 0);
2040			break;
2041		}
2042		$$ = nodcplxlit($2->val, $4->val);
2043	}
2044
2045hidden_import_list:
2046|	hidden_import_list hidden_import
2047
2048hidden_funarg_list:
2049	hidden_funarg
2050	{
2051		$$ = list1($1);
2052	}
2053|	hidden_funarg_list ',' hidden_funarg
2054	{
2055		$$ = list($1, $3);
2056	}
2057
2058hidden_structdcl_list:
2059	hidden_structdcl
2060	{
2061		$$ = list1($1);
2062	}
2063|	hidden_structdcl_list ';' hidden_structdcl
2064	{
2065		$$ = list($1, $3);
2066	}
2067
2068hidden_interfacedcl_list:
2069	hidden_interfacedcl
2070	{
2071		$$ = list1($1);
2072	}
2073|	hidden_interfacedcl_list ';' hidden_interfacedcl
2074	{
2075		$$ = list($1, $3);
2076	}
2077
2078%%
2079
2080static void
2081fixlbrace(int lbr)
2082{
2083	// If the opening brace was an LBODY,
2084	// set up for another one now that we're done.
2085	// See comment in lex.c about loophack.
2086	if(lbr == LBODY)
2087		loophack = 1;
2088}
2089