PageRenderTime 159ms CodeModel.GetById 14ms app.highlight 134ms RepoModel.GetById 1ms app.codeStats 1ms

/src/cmd/gc/go.y

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