PageRenderTime 122ms CodeModel.GetById 2ms app.highlight 110ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cmd/gc/go.y

https://bitbucket.org/adkulkar/goose
Happy | 2044 lines | 1853 code | 191 blank | 0 comment | 0 complexity | fc29adae45e65cd56275769672c8333a 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
1300fntype:
1301	LFUNC '(' oarg_type_list_ocomma ')' fnres
1302	{
1303		$3 = checkarglist($3, 1);
1304		$$ = nod(OTFUNC, N, N);
1305		$$->list = $3;
1306		$$->rlist = $5;
1307	}
1308
1309fnbody:
1310	{
1311		$$ = nil;
1312	}
1313|	'{' stmt_list '}'
1314	{
1315		$$ = $2;
1316		if($$ == nil)
1317			$$ = list1(nod(OEMPTY, N, N));
1318	}
1319
1320fnres:
1321	%prec NotParen
1322	{
1323		$$ = nil;
1324	}
1325|	fnret_type
1326	{
1327		$$ = list1(nod(ODCLFIELD, N, $1));
1328	}
1329|	'(' oarg_type_list_ocomma ')'
1330	{
1331		$2 = checkarglist($2, 0);
1332		$$ = $2;
1333	}
1334
1335fnlitdcl:
1336	fntype
1337	{
1338		closurehdr($1);
1339	}
1340
1341fnliteral:
1342	fnlitdcl lbrace stmt_list '}'
1343	{
1344		$$ = closurebody($3);
1345		fixlbrace($2);
1346	}
1347|	fnlitdcl error
1348	{
1349		$$ = closurebody(nil);
1350	}
1351
1352/*
1353 * lists of things
1354 * note that they are left recursive
1355 * to conserve yacc stack. they need to
1356 * be reversed to interpret correctly
1357 */
1358xdcl_list:
1359	{
1360		$$ = nil;
1361	}
1362|	xdcl_list xdcl ';'
1363	{
1364		$$ = concat($1, $2);
1365		if(nsyntaxerrors == 0)
1366			testdclstack();
1367	}
1368
1369vardcl_list:
1370	vardcl
1371|	vardcl_list ';' vardcl
1372	{
1373		$$ = concat($1, $3);
1374	}
1375
1376constdcl_list:
1377	constdcl1
1378|	constdcl_list ';' constdcl1
1379	{
1380		$$ = concat($1, $3);
1381	}
1382
1383typedcl_list:
1384	typedcl
1385	{
1386		$$ = list1($1);
1387	}
1388|	typedcl_list ';' typedcl
1389	{
1390		$$ = list($1, $3);
1391	}
1392
1393structdcl_list:
1394	structdcl
1395|	structdcl_list ';' structdcl
1396	{
1397		$$ = concat($1, $3);
1398	}
1399
1400interfacedcl_list:
1401	interfacedcl
1402	{
1403		$$ = list1($1);
1404	}
1405|	interfacedcl_list ';' interfacedcl
1406	{
1407		$$ = list($1, $3);
1408	}
1409
1410structdcl:
1411	new_name_list ntype oliteral
1412	{
1413		NodeList *l;
1414
1415		for(l=$1; l; l=l->next) {
1416			l->n = nod(ODCLFIELD, l->n, $2);
1417			l->n->val = $3;
1418		}
1419	}
1420|	embed oliteral
1421	{
1422		$1->val = $2;
1423		$$ = list1($1);
1424	}
1425|	'(' embed ')' oliteral
1426	{
1427		$2->val = $4;
1428		$$ = list1($2);
1429		yyerror("cannot parenthesize embedded type");
1430	}
1431|	'*' embed oliteral
1432	{
1433		$2->right = nod(OIND, $2->right, N);
1434		$2->val = $3;
1435		$$ = list1($2);
1436	}
1437|	'(' '*' embed ')' oliteral
1438	{
1439		$3->right = nod(OIND, $3->right, N);
1440		$3->val = $5;
1441		$$ = list1($3);
1442		yyerror("cannot parenthesize embedded type");
1443	}
1444|	'*' '(' embed ')' oliteral
1445	{
1446		$3->right = nod(OIND, $3->right, N);
1447		$3->val = $5;
1448		$$ = list1($3);
1449		yyerror("cannot parenthesize embedded type");
1450	}
1451
1452packname:
1453	LNAME
1454	{
1455		Node *n;
1456
1457		$$ = $1;
1458		n = oldname($1);
1459		if(n->pack != N)
1460			n->pack->used = 1;
1461	}
1462|	LNAME '.' sym
1463	{
1464		Pkg *pkg;
1465
1466		if($1->def == N || $1->def->op != OPACK) {
1467			yyerror("%S is not a package", $1);
1468			pkg = localpkg;
1469		} else {
1470			$1->def->used = 1;
1471			pkg = $1->def->pkg;
1472		}
1473		$$ = restrictlookup($3->name, pkg);
1474	}
1475
1476embed:
1477	packname
1478	{
1479		$$ = embedded($1);
1480	}
1481
1482interfacedcl:
1483	new_name indcl
1484	{
1485		$$ = nod(ODCLFIELD, $1, $2);
1486		ifacedcl($$);
1487	}
1488|	packname
1489	{
1490		$$ = nod(ODCLFIELD, N, oldname($1));
1491	}
1492|	'(' packname ')'
1493	{
1494		$$ = nod(ODCLFIELD, N, oldname($2));
1495		yyerror("cannot parenthesize embedded type");
1496	}
1497
1498indcl:
1499	'(' oarg_type_list_ocomma ')' fnres
1500	{
1501		// without func keyword
1502		$2 = checkarglist($2, 1);
1503		$$ = nod(OTFUNC, fakethis(), N);
1504		$$->list = $2;
1505		$$->rlist = $4;
1506	}
1507
1508/*
1509 * function arguments.
1510 */
1511arg_type:
1512	name_or_type
1513|	sym name_or_type
1514	{
1515		$$ = nod(ONONAME, N, N);
1516		$$->sym = $1;
1517		$$ = nod(OKEY, $$, $2);
1518	}
1519|	sym dotdotdot
1520	{
1521		$$ = nod(ONONAME, N, N);
1522		$$->sym = $1;
1523		$$ = nod(OKEY, $$, $2);
1524	}
1525|	dotdotdot
1526
1527arg_type_list:
1528	arg_type
1529	{
1530		$$ = list1($1);
1531	}
1532|	arg_type_list ',' arg_type
1533	{
1534		$$ = list($1, $3);
1535	}
1536
1537oarg_type_list_ocomma:
1538	{
1539		$$ = nil;
1540	}
1541|	arg_type_list ocomma
1542	{
1543		$$ = $1;
1544	}
1545
1546/*
1547 * statement
1548 */
1549stmt:
1550	{
1551		$$ = N;
1552	}
1553|	compound_stmt
1554|	common_dcl
1555	{
1556		$$ = liststmt($1);
1557	}
1558|	non_dcl_stmt
1559|	error
1560	{
1561		$$ = N;
1562	}
1563
1564non_dcl_stmt:
1565	simple_stmt
1566|	for_stmt
1567|	switch_stmt
1568|	select_stmt
1569|	if_stmt
1570|	labelname ':'
1571	{
1572		$1 = nod(OLABEL, $1, N);
1573		$1->sym = dclstack;  // context, for goto restrictions
1574	}
1575	stmt
1576	{
1577		NodeList *l;
1578
1579		$1->defn = $4;
1580		l = list1($1);
1581		if($4)
1582			l = list(l, $4);
1583		$$ = liststmt(l);
1584	}
1585|	LFALL
1586	{
1587		// will be converted to OFALL
1588		$$ = nod(OXFALL, N, N);
1589	}
1590|	LBREAK onew_name
1591	{
1592		$$ = nod(OBREAK, $2, N);
1593	}
1594|	LCONTINUE onew_name
1595	{
1596		$$ = nod(OCONTINUE, $2, N);
1597	}
1598|	LGO pseudocall
1599	{
1600		$$ = nod(OPROC, $2, N);
1601	}
1602|	LDEFER pseudocall
1603	{
1604		$$ = nod(ODEFER, $2, N);
1605	}
1606|	LGOTO new_name
1607	{
1608		$$ = nod(OGOTO, $2, N);
1609		$$->sym = dclstack;  // context, for goto restrictions
1610	}
1611|	LRETURN oexpr_list
1612	{
1613		$$ = nod(ORETURN, N, N);
1614		$$->list = $2;
1615		if($$->list == nil) {
1616			NodeList *l;
1617			
1618			for(l=curfn->dcl; l; l=l->next) {
1619				if(l->n->class == PPARAM)
1620					continue;
1621				if(l->n->class != PPARAMOUT)
1622					break;
1623				if(l->n->sym->def != l->n)
1624					yyerror("%s is shadowed during return", l->n->sym->name);
1625			}
1626		}
1627	}
1628
1629stmt_list:
1630	stmt
1631	{
1632		$$ = nil;
1633		if($1 != N)
1634			$$ = list1($1);
1635	}
1636|	stmt_list ';' stmt
1637	{
1638		$$ = $1;
1639		if($3 != N)
1640			$$ = list($$, $3);
1641	}
1642
1643new_name_list:
1644	new_name
1645	{
1646		$$ = list1($1);
1647	}
1648|	new_name_list ',' new_name
1649	{
1650		$$ = list($1, $3);
1651	}
1652
1653dcl_name_list:
1654	dcl_name
1655	{
1656		$$ = list1($1);
1657	}
1658|	dcl_name_list ',' dcl_name
1659	{
1660		$$ = list($1, $3);
1661	}
1662
1663expr_list:
1664	expr
1665	{
1666		$$ = list1($1);
1667	}
1668|	expr_list ',' expr
1669	{
1670		$$ = list($1, $3);
1671	}
1672
1673expr_or_type_list:
1674	expr_or_type
1675	{
1676		$$ = list1($1);
1677	}
1678|	expr_or_type_list ',' expr_or_type
1679	{
1680		$$ = list($1, $3);
1681	}
1682
1683/*
1684 * list of combo of keyval and val
1685 */
1686keyval_list:
1687	keyval
1688	{
1689		$$ = list1($1);
1690	}
1691|	complitexpr
1692	{
1693		$$ = list1($1);
1694	}
1695|	keyval_list ',' keyval
1696	{
1697		$$ = list($1, $3);
1698	}
1699|	keyval_list ',' complitexpr
1700	{
1701		$$ = list($1, $3);
1702	}
1703
1704braced_keyval_list:
1705	{
1706		$$ = nil;
1707	}
1708|	keyval_list ocomma
1709	{
1710		$$ = $1;
1711	}
1712
1713/*
1714 * optional things
1715 */
1716osemi:
1717|	';'
1718
1719ocomma:
1720|	','
1721
1722oexpr:
1723	{
1724		$$ = N;
1725	}
1726|	expr
1727
1728oexpr_list:
1729	{
1730		$$ = nil;
1731	}
1732|	expr_list
1733
1734osimple_stmt:
1735	{
1736		$$ = N;
1737	}
1738|	simple_stmt
1739
1740ohidden_funarg_list:
1741	{
1742		$$ = nil;
1743	}
1744|	hidden_funarg_list
1745
1746ohidden_structdcl_list:
1747	{
1748		$$ = nil;
1749	}
1750|	hidden_structdcl_list
1751
1752ohidden_interfacedcl_list:
1753	{
1754		$$ = nil;
1755	}
1756|	hidden_interfacedcl_list
1757
1758oliteral:
1759	{
1760		$$.ctype = CTxxx;
1761	}
1762|	LLITERAL
1763
1764/*
1765 * import syntax from package header
1766 */
1767hidden_import:
1768	LIMPORT LNAME LLITERAL ';'
1769	{
1770		importimport($2, $3.u.sval);
1771	}
1772|	LVAR hidden_pkg_importsym hidden_type ';'
1773	{
1774		importvar($2, $3);
1775	}
1776|	LCONST hidden_pkg_importsym '=' hidden_constant ';'
1777	{
1778		importconst($2, types[TIDEAL], $4);
1779	}
1780|	LCONST hidden_pkg_importsym hidden_type '=' hidden_constant ';'
1781	{
1782		importconst($2, $3, $5);
1783	}
1784|	LTYPE hidden_pkgtype hidden_type ';'
1785	{
1786		importtype($2, $3);
1787	}
1788|	LFUNC hidden_fndcl fnbody ';'
1789	{
1790		if($2 == N)
1791			break;
1792
1793		funcbody($2);
1794		importlist = list(importlist, $2);
1795
1796		if(debug['E']) {
1797			print("import [%Z] func %lN \n", $2->sym->pkg->path, $2);
1798		}
1799	}
1800
1801hidden_pkg_importsym:
1802	hidden_importsym
1803	{
1804		$$ = $1;
1805		structpkg = $$->pkg;
1806	}
1807
1808hidden_pkgtype:
1809	hidden_pkg_importsym
1810	{
1811		$$ = pkgtype($1);
1812		importsym($1, OTYPE);
1813	}
1814
1815/*
1816 *  importing types
1817 */
1818
1819hidden_type:
1820	hidden_type_misc
1821|	hidden_type_recv_chan
1822|	hidden_type_func
1823
1824hidden_type_non_recv_chan:
1825	hidden_type_misc
1826|	hidden_type_func
1827
1828hidden_type_misc:
1829	hidden_importsym
1830	{
1831		$$ = pkgtype($1);
1832	}
1833|	LNAME
1834	{
1835		// predefined name like uint8
1836		$1 = pkglookup($1->name, builtinpkg);
1837		if($1->def == N || $1->def->op != OTYPE) {
1838			yyerror("%s is not a type", $1->name);
1839			$$ = T;
1840		} else
1841			$$ = $1->def->type;
1842	}
1843|	'[' ']' hidden_type
1844	{
1845		$$ = aindex(N, $3);
1846	}
1847|	'[' LLITERAL ']' hidden_type
1848	{
1849		$$ = aindex(nodlit($2), $4);
1850	}
1851|	LMAP '[' hidden_type ']' hidden_type
1852	{
1853		$$ = maptype($3, $5);
1854	}
1855|	LSTRUCT '{' ohidden_structdcl_list '}'
1856	{
1857		$$ = tostruct($3);
1858	}
1859|	LINTERFACE '{' ohidden_interfacedcl_list '}'
1860	{
1861		$$ = tointerface($3);
1862	}
1863|	'*' hidden_type
1864	{
1865		$$ = ptrto($2);
1866	}
1867|	LCHAN hidden_type_non_recv_chan
1868	{
1869		$$ = typ(TCHAN);
1870		$$->type = $2;
1871		$$->chan = Cboth;
1872	}
1873|	LCHAN '(' hidden_type_recv_chan ')'
1874	{
1875		$$ = typ(TCHAN);
1876		$$->type = $3;
1877		$$->chan = Cboth;
1878	}
1879|	LCHAN LCOMM hidden_type
1880	{
1881		$$ = typ(TCHAN);
1882		$$->type = $3;
1883		$$->chan = Csend;
1884	}
1885
1886hidden_type_recv_chan:
1887	LCOMM LCHAN hidden_type
1888	{
1889		$$ = typ(TCHAN);
1890		$$->type = $3;
1891		$$->chan = Crecv;
1892	}
1893
1894hidden_type_func:
1895	LFUNC '(' ohidden_funarg_list ')' ohidden_funres
1896	{
1897		$$ = functype(nil, $3, $5);
1898	}
1899
1900hidden_funarg:
1901	sym hidden_type oliteral
1902	{
1903		$$ = nod(ODCLFIELD, N, typenod($2));
1904		if($1)
1905			$$->left = newname($1);
1906		$$->val = $3;
1907	}
1908|	sym LDDD hidden_type oliteral
1909	{
1910		Type *t;
1911	
1912		t = typ(TARRAY);
1913		t->bound = -1;
1914		t->type = $3;
1915
1916		$$ = nod(ODCLFIELD, N, typenod(t));
1917		if($1)
1918			$$->left = newname($1);
1919		$$->isddd = 1;
1920		$$->val = $4;
1921	}
1922
1923hidden_structdcl:
1924	sym hidden_type oliteral
1925	{
1926		Sym *s;
1927
1928		if($1 != S) {
1929			$$ = nod(ODCLFIELD, newname($1), typenod($2));
1930			$$->val = $3;
1931		} else {
1932			s = $2->sym;
1933			if(s == S && isptr[$2->etype])
1934				s = $2->type->sym;
1935			$$ = embedded(s);
1936			$$->right = typenod($2);
1937			$$->val = $3;
1938		}
1939	}
1940
1941hidden_interfacedcl:
1942	sym '(' ohidden_funarg_list ')' ohidden_funres
1943	{
1944		$$ = nod(ODCLFIELD, newname($1), typenod(functype(fakethis(), $3, $5)));
1945	}
1946
1947ohidden_funres:
1948	{
1949		$$ = nil;
1950	}
1951|	hidden_funres
1952
1953hidden_funres:
1954	'(' ohidden_funarg_list ')'
1955	{
1956		$$ = $2;
1957	}
1958|	hidden_type
1959	{
1960		$$ = list1(nod(ODCLFIELD, N, typenod($1)));
1961	}
1962
1963/*
1964 *  importing constants
1965 */
1966
1967hidden_literal:
1968	LLITERAL
1969	{
1970		$$ = nodlit($1);
1971	}
1972|	'-' LLITERAL
1973	{
1974		$$ = nodlit($2);
1975		switch($$->val.ctype){
1976		case CTINT:
1977			mpnegfix($$->val.u.xval);
1978			break;
1979		case CTFLT:
1980			mpnegflt($$->val.u.fval);
1981			break;
1982		default:
1983			yyerror("bad negated constant");
1984		}
1985	}
1986|	sym
1987	{
1988		$$ = oldname(pkglookup($1->name, builtinpkg));
1989		if($$->op != OLITERAL)
1990			yyerror("bad constant %S", $$->sym);
1991	}
1992
1993hidden_constant:
1994	hidden_literal
1995|	'(' hidden_literal '+' hidden_literal ')'
1996	{
1997		$$ = nodcplxlit($2->val, $4->val);
1998	}
1999
2000hidden_import_list:
2001|	hidden_import_list hidden_import
2002
2003hidden_funarg_list:
2004	hidden_funarg
2005	{
2006		$$ = list1($1);
2007	}
2008|	hidden_funarg_list ',' hidden_funarg
2009	{
2010		$$ = list($1, $3);
2011	}
2012
2013hidden_structdcl_list:
2014	hidden_structdcl
2015	{
2016		$$ = list1($1);
2017	}
2018|	hidden_structdcl_list ';' hidden_structdcl
2019	{
2020		$$ = list($1, $3);
2021	}
2022
2023hidden_interfacedcl_list:
2024	hidden_interfacedcl
2025	{
2026		$$ = list1($1);
2027	}
2028|	hidden_interfacedcl_list ';' hidden_interfacedcl
2029	{
2030		$$ = list($1, $3);
2031	}
2032
2033%%
2034
2035static void
2036fixlbrace(int lbr)
2037{
2038	// If the opening brace was an LBODY,
2039	// set up for another one now that we're done.
2040	// See comment in lex.c about loophack.
2041	if(lbr == LBODY)
2042		loophack = 1;
2043}
2044