github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/gc/go.y (about)

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