go.y 29.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

%{
#include "go.h"
%}
%union	{
	Node*		node;
	Type*		type;
	Sym*		sym;
	struct	Val	val;
	int		lint;
}
Ken Thompson's avatar
Ken Thompson committed
15 16
%token	<val>		LLITERAL
%token	<lint>		LASOP
Ken Thompson's avatar
Ken Thompson committed
17
%token	<sym>		LNAME LBASETYPE LATYPE LPACK LACONST
18
%token	<sym>		LPACKAGE LIMPORT LDEFER LCLOSE LCLOSED
Ken Thompson's avatar
Ken Thompson committed
19
%token	<sym>		LMAP LCHAN LINTERFACE LFUNC LSTRUCT
Ken Thompson's avatar
Ken Thompson committed
20
%token	<sym>		LCOLAS LFALL LRETURN LDDD
Ken Thompson's avatar
Ken Thompson committed
21
%token	<sym>		LLEN LCAP LPANIC LPANICN LPRINT LPRINTN
Ken Thompson's avatar
Ken Thompson committed
22
%token	<sym>		LVAR LTYPE LCONST LSELECT LMAKE LNEW
Ken Thompson's avatar
Ken Thompson committed
23 24 25 26
%token	<sym>		LFOR LIF LELSE LSWITCH LCASE LDEFAULT
%token	<sym>		LBREAK LCONTINUE LGO LGOTO LRANGE
%token	<sym>		LNIL LTRUE LFALSE LIOTA

27
%token			LOROR LANDAND LEQ LNE LLE LLT LGE LGT
28
%token			LLSH LRSH LINC LDEC LCOMM LANDNOT
Ken Thompson's avatar
Ken Thompson committed
29
%token			LIGNORE
30

Russ Cox's avatar
Russ Cox committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * the go semicolon rules are:
 *
 *  1. all statements and declarations are terminated by semicolons
 *  2. semicolons can be omitted at top level.
 *  3. semicolons can be omitted before and after the closing ) or }
 *	on a list of statements or declarations.
 *
 * thus the grammar must distinguish productions that
 * can omit the semicolon terminator and those that can't.
 * names like Astmt, Avardcl, etc. can drop the semicolon.
 * names like Bstmt, Bvardcl, etc. can't.
 */

Ken Thompson's avatar
Ken Thompson committed
45
%type	<sym>		sym sym1 sym2 sym3 keyword laconst lname latype lpackatype
46 47
%type	<node>		xdcl xdcl_list_r oxdcl_list
%type	<node>		common_dcl Acommon_dcl Bcommon_dcl
48
%type	<node>		oarg_type_list arg_type_list_r arg_chunk arg_chunk_list_r arg_type_list
Russ Cox's avatar
Russ Cox committed
49
%type	<node>		Aelse_stmt Belse_stmt
Ken Thompson's avatar
Ken Thompson committed
50 51
%type	<node>		complex_stmt compound_stmt ostmt_list
%type	<node>		stmt_list_r Astmt_list_r Bstmt_list_r
Russ Cox's avatar
Russ Cox committed
52
%type	<node>		Astmt Bstmt
53
%type	<node>		for_stmt for_body for_header
54
%type	<node>		if_stmt if_body if_header select_stmt condition
Russ Cox's avatar
Russ Cox committed
55
%type	<node>		simple_stmt osimple_stmt range_stmt semi_stmt
56
%type	<node>		expr uexpr pexpr expr_list oexpr oexpr_list expr_list_r
Ken Thompson's avatar
Ken Thompson committed
57
%type	<node>		exprsym3_list_r exprsym3
Russ Cox's avatar
Russ Cox committed
58
%type	<node>		name labelname onew_name new_name new_name_list_r new_field
59
%type	<node>		vardcl_list_r vardcl Avardcl Bvardcl
60
%type	<node>		interfacedcl_list_r interfacedcl interfacedcl1
Ken Thompson's avatar
Ken Thompson committed
61
%type	<node>		structdcl_list_r structdcl embed
Ken Thompson's avatar
Ken Thompson committed
62
%type	<node>		fnres Afnres Bfnres fnliteral xfndcl fndcl fnbody
Russ Cox's avatar
Russ Cox committed
63
%type	<node>		braced_keyexpr_list keyval_list_r keyval
64

Russ Cox's avatar
Russ Cox committed
65 66 67 68 69 70 71 72
%type	<type>		typedclname new_type
%type	<type>		type Atype Btype
%type	<type>		othertype Aothertype Bothertype
%type	<type>		chantype Achantype Bchantype
%type	<type>		fntype Afntype Bfntype
%type	<type>		nametype structtype interfacetype convtype
%type	<type>		non_name_type Anon_fn_type Bnon_fn_type
%type	<type>		Anon_chan_type Bnon_chan_type
Ken Thompson's avatar
Ken Thompson committed
73
%type	<type>		indcl fnlitdcl dotdotdot
74
%type	<val>		oliteral
75

76
%type	<node>		hidden_constant
Russ Cox's avatar
Russ Cox committed
77
%type	<node>		hidden_dcl hidden_structdcl
Russ Cox's avatar
Russ Cox committed
78 79 80 81 82
%type	<type>		hidden_type hidden_type1 hidden_type2
%type	<node>		hidden_structdcl_list ohidden_structdcl_list hidden_structdcl_list_r
%type	<node>		hidden_interfacedcl_list ohidden_interfacedcl_list hidden_interfacedcl_list_r
%type	<node>		hidden_interfacedcl
%type	<node>		hidden_funarg_list ohidden_funarg_list hidden_funarg_list_r
Russ Cox's avatar
Russ Cox committed
83
%type	<node>		hidden_funres ohidden_funres hidden_importsym hidden_pkg_importsym
84

85 86
%left			LOROR
%left			LANDAND
Rob Pike's avatar
Rob Pike committed
87
%left			LCOMM
88 89
%left			LEQ LNE LLE LGE LLT LGT
%left			'+' '-' '|' '^'
90
%left			'*' '/' '%' '&' LLSH LRSH LANDNOT
Russ Cox's avatar
Russ Cox committed
91

92 93 94 95 96 97
/*
 * resolve { vs condition in favor of condition
 */
%left			'{'
%left			Condition

Russ Cox's avatar
Russ Cox committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/*
 * resolve LPACKAGE vs not in favor of LPACKAGE
 */
%left			NotPackage
%left			LPACKAGE

/*
 * resolve '.' vs not in favor of '.'
 */
%left			NotDot
%left			'.'

/*
 * resolve '(' vs not in favor of '('
 */
%left			NotParen
%left			'('
115

116 117 118 119 120 121
%%
file:
	package import_there imports oxdcl_list
	{
		if(debug['f'])
			frame(1);
Ken Thompson's avatar
Ken Thompson committed
122
		fninit($4);
123 124 125 126
		testdclstack();
	}

package:
Russ Cox's avatar
Russ Cox committed
127
	%prec NotPackage
128 129 130
	{
		yyerror("package statement must be first");
		mkpackage("main");
Ken Thompson's avatar
Ken Thompson committed
131
		cannedimports("sys.6", sysimport);
132 133 134 135
	}
|	LPACKAGE sym
	{
		mkpackage($2->name);
Ken Thompson's avatar
Ken Thompson committed
136
		cannedimports("sys.6", sysimport);
137 138 139 140 141 142 143 144
	}

imports:
|	imports import

import:
	LIMPORT import_stmt
|	LIMPORT '(' import_stmt_list_r osemi ')'
145
|	LIMPORT '(' ')'
146 147

import_stmt:
Ken Thompson's avatar
Ken Thompson committed
148
	import_here import_package import_there
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

import_here:
	LLITERAL
	{
		// import with original name
		pkgmyname = S;
		importfile(&$1);
	}
|	sym LLITERAL
	{
		// import with given name
		pkgmyname = $1;
		pkgmyname->lexical = LPACK;
		importfile(&$2);
	}
|	'.' LLITERAL
	{
		// import with my name
		pkgmyname = lookup(package);
		importfile(&$2);
	}

Ken Thompson's avatar
Ken Thompson committed
171 172 173 174 175
import_package:
	LPACKAGE sym
	{
		pkgimportname = $2;

176 177 178
		if(strcmp($2->name, "main") == 0)
			yyerror("cannot import package main");

Ken Thompson's avatar
Ken Thompson committed
179 180 181 182 183 184
		// if we are not remapping the package name
		// then the imported package name is LPACK
		if(pkgmyname == S)
			pkgimportname->lexical = LPACK;
	}

185
import_there:
186
	hidden_import_list '$' '$'
187
	{
Ken Thompson's avatar
Ken Thompson committed
188
		checkimports();
189
		unimportfile();
190
		pkgimportname = S;
191
	}
192
|	LIMPORT '$' '$' hidden_import_list '$' '$'
Ken Thompson's avatar
Ken Thompson committed
193 194 195
	{
		checkimports();
	}
196

197 198 199 200 201 202 203 204 205
hidden_import_list:
	{
		defercheckwidth();
	}
	hidden_import_list_r
	{
		resumecheckwidth();
	}

206 207 208 209
/*
 * declarations
 */
xdcl:
210 211 212 213 214
	{ stksize = initstksize; } common_dcl
	{
		$$ = $2;
		initstksize = stksize;
	}
Ken Thompson's avatar
Ken Thompson committed
215
|	xfndcl
216
	{
Russ Cox's avatar
Russ Cox committed
217 218
		if($1 != N && $1->nname != N && $1->type->thistuple == 0)
			autoexport($1->nname->sym);
Ken Thompson's avatar
Ken Thompson committed
219
		$$ = N;
220
	}
221 222 223 224
|	';'
	{
		$$ = N;
	}
Ken Thompson's avatar
Ken Thompson committed
225
|	error xdcl
226
	{
Ken Thompson's avatar
Ken Thompson committed
227
		$$ = $2;
228
	}
229 230

common_dcl:
231 232 233 234 235
	Acommon_dcl
|	Bcommon_dcl

Acommon_dcl:
	LVAR Avardcl
236 237 238 239 240 241 242
	{
		$$ = $2;
	}
|	LVAR '(' vardcl_list_r osemi ')'
	{
		$$ = rev($3);
	}
243 244 245 246
|	LVAR '(' ')'
	{
		$$ = N;
	}
Ken Thompson's avatar
Ken Thompson committed
247 248 249 250 251 252 253
|	LCONST '(' constdcl osemi ')'
	{
		iota = 0;
		lastconst = N;
		$$ = N;
	}
|	LCONST '(' constdcl ';' constdcl_list_r osemi ')'
254 255
	{
		iota = 0;
Ken Thompson's avatar
Ken Thompson committed
256
		lastconst = N;
Ken Thompson's avatar
Ken Thompson committed
257
		$$ = N;
258
	}
259 260 261 262
|	LCONST '(' ')'
	{
		$$ = N;
	}
263
|	LTYPE Atypedcl
264 265 266
	{
		$$ = N;
	}
267
|	LTYPE '(' typedcl_list_r osemi ')'
268 269 270
	{
		$$ = N;
	}
271 272 273 274
|	LTYPE '(' ')'
	{
		$$ = N;
	}
275 276 277 278 279 280 281 282 283 284

Bcommon_dcl:
	LVAR Bvardcl
	{
		$$ = $2;
	}
|	LCONST constdcl
	{
		$$ = N;
		iota = 0;
Ken Thompson's avatar
Ken Thompson committed
285
		lastconst = N;
286 287
	}
|	LTYPE Btypedcl
288 289 290 291 292
	{
		$$ = N;
	}

vardcl:
293 294 295 296
	Avardcl
|	Bvardcl

Avardcl:
Russ Cox's avatar
Russ Cox committed
297
	new_name_list_r Atype
298 299 300 301 302
	{
		$$ = rev($1);
		dodclvar($$, $2);

		$$ = nod(OAS, $$, N);
303
		addtotop($$);
304 305 306
	}

Bvardcl:
Russ Cox's avatar
Russ Cox committed
307
	new_name_list_r Btype
308 309 310 311 312
	{
		$$ = rev($1);
		dodclvar($$, $2);

		$$ = nod(OAS, $$, N);
313
		addtotop($$);
314
	}
Ken Thompson's avatar
Ken Thompson committed
315
|	new_name_list_r type '=' expr_list
316
	{
317 318 319
		if(addtop != N)
			fatal("new_name_list_r type '=' expr_list");

320
		$$ = variter($1, $2, $4);
Ken Thompson's avatar
Ken Thompson committed
321
		addtotop($$);
322
	}
323
|	new_name_list_r '=' expr_list
324
	{
325 326 327 328 329
		if(addtop != N)
			fatal("new_name_list_r '=' expr_list");

		$$ = variter($1, T, $3);
		addtotop($$);
330 331 332
	}

constdcl:
333
	new_name_list_r type '=' expr_list
Ken Thompson's avatar
Ken Thompson committed
334
	{
335
		constiter($1, $2, $4);
Ken Thompson's avatar
Ken Thompson committed
336
	}
337
|	new_name_list_r '=' expr_list
338
	{
339
		constiter($1, T, $3);
Ken Thompson's avatar
Ken Thompson committed
340 341
	}

Ken Thompson's avatar
Ken Thompson committed
342 343
constdcl1:
	constdcl
344
|	new_name_list_r type
Ken Thompson's avatar
Ken Thompson committed
345
	{
346
		constiter($1, $2, N);
347
	}
348
|	new_name_list_r
349
	{
350
		constiter($1, T, N);
351 352
	}

353 354 355 356 357 358 359
typedclname:
	new_type
	{
		$$ = dodcltype($1);
		defercheckwidth();
	}

Russ Cox's avatar
Russ Cox committed
360 361 362 363
typedcl:
	Atypedcl
|	Btypedcl

364
Atypedcl:
Russ Cox's avatar
Russ Cox committed
365 366 367 368 369 370 371 372
	typedclname Atype
	{
		updatetype($1, $2);
		resumecheckwidth();
	}

Btypedcl:
	typedclname Btype
373 374 375 376 377
	{
		updatetype($1, $2);
		resumecheckwidth();
	}
|	typedclname LSTRUCT
378
	{
379 380
		updatetype($1, typ(TFORWSTRUCT));
		resumecheckwidth();
381
	}
382 383 384 385 386
|	typedclname LINTERFACE
	{
		updatetype($1, typ(TFORWINTER));
		resumecheckwidth();
	}
387

Russ Cox's avatar
Russ Cox committed
388
Aelse_stmt:
Ken Thompson's avatar
Ken Thompson committed
389
	complex_stmt
390 391
|	compound_stmt

Russ Cox's avatar
Russ Cox committed
392
Belse_stmt:
Ken Thompson's avatar
Ken Thompson committed
393 394 395
	simple_stmt
|	semi_stmt
|	';'
396
	{
Ken Thompson's avatar
Ken Thompson committed
397
		$$ = N;
398 399 400 401 402 403 404 405 406 407
	}

simple_stmt:
	expr
	{
		$$ = $1;
	}
|	expr LASOP expr
	{
		$$ = nod(OASOP, $1, $3);
Ken Thompson's avatar
Ken Thompson committed
408
		$$->etype = $2;			// rathole to pass opcode
409
	}
Ken Thompson's avatar
Ken Thompson committed
410
|	exprsym3_list_r '=' expr_list
411
	{
Ken Thompson's avatar
Ken Thompson committed
412 413
		$$ = rev($1);
		$$ = nod(OAS, $$, $3);
414
	}
Ken Thompson's avatar
Ken Thompson committed
415
|	exprsym3_list_r LCOLAS expr_list
416
	{
417 418
		if(addtop != N)
			fatal("exprsym3_list_r LCOLAS expr_list");
Ken Thompson's avatar
Ken Thompson committed
419 420 421 422
		if($3->op == OTYPESW) {
			$$ = nod(OTYPESW, $1, $3->left);
			break;
		}
Ken Thompson's avatar
Ken Thompson committed
423 424 425
		$$ = rev($1);
		$$ = colas($$, $3);
		$$ = nod(OAS, $$, $3);
426
		$$->colas = 1;
Ken Thompson's avatar
Ken Thompson committed
427
		addtotop($$);
428
	}
429 430 431 432
|	LPRINT '(' oexpr_list ')'
	{
		$$ = nod(OPRINT, $3, N);
	}
Ken Thompson's avatar
Ken Thompson committed
433 434 435 436
|	LPRINTN '(' oexpr_list ')'
	{
		$$ = nod(OPRINTN, $3, N);
	}
437 438 439 440
|	LPANIC '(' oexpr_list ')'
	{
		$$ = nod(OPANIC, $3, N);
	}
Ken Thompson's avatar
Ken Thompson committed
441 442 443 444
|	LPANICN '(' oexpr_list ')'
	{
		$$ = nod(OPANICN, $3, N);
	}
Russ Cox's avatar
Russ Cox committed
445
|	expr LINC
Ken Thompson's avatar
Ken Thompson committed
446
	{
447
		$$ = nod(OASOP, $1, nodintconst(1));
Ken Thompson's avatar
Ken Thompson committed
448 449 450 451
		$$->etype = OADD;
	}
|	expr LDEC
	{
452
		$$ = nod(OASOP, $1, nodintconst(1));
Ken Thompson's avatar
Ken Thompson committed
453 454 455
		$$->etype = OSUB;
	}

456 457 458
complex_stmt:
	LFOR for_stmt
	{
Ken Thompson's avatar
Ken Thompson committed
459
		popdcl();
460 461 462 463
		$$ = $2;
	}
|	LSWITCH if_stmt
	{
Ken Thompson's avatar
Ken Thompson committed
464
		popdcl();
465 466 467 468 469
		$$ = $2;
		$$->op = OSWITCH;
	}
|	LIF if_stmt
	{
Ken Thompson's avatar
Ken Thompson committed
470
		popdcl();
471 472
		$$ = $2;
	}
Russ Cox's avatar
Russ Cox committed
473
|	LIF if_stmt LELSE Aelse_stmt
474
	{
Ken Thompson's avatar
Ken Thompson committed
475
		popdcl();
476 477 478
		$$ = $2;
		$$->nelse = $4;
	}
Ken Thompson's avatar
Ken Thompson committed
479 480 481 482 483
|	LSELECT select_stmt
	{
		popdcl();
		$$ = $2;
	}
484 485 486 487 488 489
|	LCASE expr_list ':'
	{
		// will be converted to OCASE
		// right will point to next case
		// done in casebody()
		poptodcl();
490 491 492 493 494 495 496
		if(typeswvar != N && typeswvar->right != N)
		if($2->op == OLITERAL && $2->val.ctype == CTNIL) {
			// this version in type switch case nil
			$$ = nod(OTYPESW, N, N);
			$$ = nod(OXCASE, $$, N);
			break;
		}
497 498
		$$ = nod(OXCASE, $2, N);
	}
Rob Pike's avatar
Rob Pike committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|	LCASE name '=' expr ':'
	{
		// will be converted to OCASE
		// right will point to next case
		// done in casebody()
		poptodcl();
		$$ = nod(OAS, $2, $4);
		$$ = nod(OXCASE, $$, N);
	}
|	LCASE name LCOLAS expr ':'
	{
		// will be converted to OCASE
		// right will point to next case
		// done in casebody()
		poptodcl();
Ken Thompson's avatar
Ken Thompson committed
514
		$$ = nod(OAS, selectas($2,$4), $4);
Rob Pike's avatar
Rob Pike committed
515
		$$ = nod(OXCASE, $$, N);
516
		addtotop($$);
Rob Pike's avatar
Rob Pike committed
517
	}
Ken Thompson's avatar
Ken Thompson committed
518 519 520 521 522 523 524 525 526 527 528 529
|	LCASE type ':'
	{
		poptodcl();
		if(typeswvar == N || typeswvar->right == N) {
			yyerror("type case not in a type switch");
			$$ = N;
		} else
			$$ = old2new(typeswvar->right, $2);
		$$ = nod(OTYPESW, $$, N);
		$$ = nod(OXCASE, $$, N);
		addtotop($$);
	}
530 531 532 533 534
|	LDEFAULT ':'
	{
		poptodcl();
		$$ = nod(OXCASE, N, N);
	}
Ken Thompson's avatar
Ken Thompson committed
535 536 537

semi_stmt:
	LFALL
538 539 540 541
	{
		// will be converted to OFALL
		$$ = nod(OXFALL, N, N);
	}
542
|	LBREAK onew_name
543 544 545
	{
		$$ = nod(OBREAK, $2, N);
	}
546
|	LCONTINUE onew_name
547 548 549
	{
		$$ = nod(OCONTINUE, $2, N);
	}
Ken Thompson's avatar
Ken Thompson committed
550
|	LGO pexpr '(' oexpr_list ')'
551
	{
Ken Thompson's avatar
Ken Thompson committed
552 553
		$$ = nod(OCALL, $2, $4);
		$$ = nod(OPROC, $$, N);
554
	}
Ken Thompson's avatar
defer  
Ken Thompson committed
555 556 557 558 559
|	LDEFER pexpr '(' oexpr_list ')'
	{
		$$ = nod(OCALL, $2, $4);
		$$ = nod(ODEFER, $$, N);
	}
Ken Thompson's avatar
Ken Thompson committed
560
|	LGOTO new_name
561 562 563
	{
		$$ = nod(OGOTO, $2, N);
	}
Ken Thompson's avatar
Ken Thompson committed
564
|	LRETURN oexpr_list
565
	{
Ken Thompson's avatar
Ken Thompson committed
566 567
		$$ = nod(ORETURN, $2, N);
	}
Russ Cox's avatar
Russ Cox committed
568
|	LIF if_stmt LELSE Belse_stmt
Ken Thompson's avatar
Ken Thompson committed
569
	{
Ken Thompson's avatar
Ken Thompson committed
570
		popdcl();
Ken Thompson's avatar
Ken Thompson committed
571 572
		$$ = $2;
		$$->nelse = $4;
573 574 575 576 577
	}

compound_stmt:
	'{'
	{
Ken Thompson's avatar
Ken Thompson committed
578
		markdcl();
579 580 581 582 583
	} ostmt_list '}'
	{
		$$ = $3;
		if($$ == N)
			$$ = nod(OEMPTY, N, N);
Ken Thompson's avatar
Ken Thompson committed
584
		popdcl();
585 586
	}

Russ Cox's avatar
Russ Cox committed
587 588
range_stmt:
	exprsym3_list_r '=' LRANGE expr
Ken Thompson's avatar
Ken Thompson committed
589 590 591 592
	{
		$$ = nod(ORANGE, $1, $4);
		$$->etype = 0;	// := flag
	}
593
|	exprsym3_list_r LCOLAS LRANGE expr
Ken Thompson's avatar
Ken Thompson committed
594 595 596 597 598 599
	{
		$$ = nod(ORANGE, $1, $4);
		$$->etype = 1;
	}

for_header:
Russ Cox's avatar
Russ Cox committed
600 601
	osimple_stmt ';' osimple_stmt ';' osimple_stmt
	{
602
		// init ; test ; incr
603 604
		if($5 != N && $5->colas != 0)
			yyerror("cannot declare in the for-increment");
605 606 607 608 609
		$$ = nod(OFOR, N, N);
		$$->ninit = $1;
		$$->ntest = $3;
		$$->nincr = $5;
	}
610
|	condition
611
	{
Ken Thompson's avatar
Ken Thompson committed
612
		// normal test
613 614 615 616 617
		$$ = nod(OFOR, N, N);
		$$->ninit = N;
		$$->ntest = $1;
		$$->nincr = N;
	}
Russ Cox's avatar
Russ Cox committed
618 619 620 621 622
|	range_stmt
	{
		$$ = dorange($1);
		addtotop($$);
	}
623 624 625 626 627

for_body:
	for_header compound_stmt
	{
		$$ = $1;
Ken Thompson's avatar
Ken Thompson committed
628
		$$->nbody = list($$->nbody, $2);
629 630 631 632
	}

for_stmt:
	{
Ken Thompson's avatar
Ken Thompson committed
633
		markdcl();
634 635 636 637 638
	} for_body
	{
		$$ = $2;
	}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
/*
 * using cond instead of osimple_stmt creates
 * a shift/reduce conflict on an input like
 *
 *	if x == []int { true } { true }
 *
 * at the first {, giving us an opportunity
 * to resolve it by reduce, which implements
 * the rule about { } inside if conditions
 * needing parens.
 */
condition:
	osimple_stmt	%prec Condition


654
if_header:
655
	condition
656 657 658 659 660 661
	{
		// test
		$$ = nod(OIF, N, N);
		$$->ninit = N;
		$$->ntest = $1;
	}
662
|	osimple_stmt ';' condition
663 664 665 666 667 668 669 670
	{
		// init ; test
		$$ = nod(OIF, N, N);
		$$->ninit = $1;
		$$->ntest = $3;
	}

if_body:
Ken Thompson's avatar
Ken Thompson committed
671 672 673 674 675 676 677 678 679 680
	if_header
	{
		Node *n;
		n = $1->ntest;
		if(n != N && n->op == OTYPESW)
			n = n->left;
		else
			n = N;
		typeswvar = nod(OLIST, typeswvar, n);
	} compound_stmt
681 682
	{
		$$ = $1;
Ken Thompson's avatar
Ken Thompson committed
683 684
		$$->nbody = $3;
		typeswvar = typeswvar->left;
685 686 687 688
	}

if_stmt:
	{
Ken Thompson's avatar
Ken Thompson committed
689
		markdcl();
690 691 692 693 694
	} if_body
	{
		$$ = $2;
	}

Ken Thompson's avatar
Ken Thompson committed
695 696 697 698 699 700 701 702 703
select_stmt:
	{
		markdcl();
	}
	compound_stmt
	{
		$$ = nod(OSELECT, $2, N);
	}

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/*
 * expressions
 */
expr:
	uexpr
|	expr LOROR expr
	{
		$$ = nod(OOROR, $1, $3);
	}
|	expr LANDAND expr
	{
		$$ = nod(OANDAND, $1, $3);
	}
|	expr LEQ expr
	{
		$$ = nod(OEQ, $1, $3);
	}
|	expr LNE expr
	{
		$$ = nod(ONE, $1, $3);
	}
|	expr LLT expr
	{
		$$ = nod(OLT, $1, $3);
	}
|	expr LLE expr
	{
		$$ = nod(OLE, $1, $3);
	}
|	expr LGE expr
	{
		$$ = nod(OGE, $1, $3);
	}
|	expr LGT expr
	{
		$$ = nod(OGT, $1, $3);
	}
|	expr '+' expr
	{
		$$ = nod(OADD, $1, $3);
	}
|	expr '-' expr
	{
		$$ = nod(OSUB, $1, $3);
	}
|	expr '|' expr
	{
		$$ = nod(OOR, $1, $3);
	}
|	expr '^' expr
	{
		$$ = nod(OXOR, $1, $3);
	}
|	expr '*' expr
	{
		$$ = nod(OMUL, $1, $3);
	}
|	expr '/' expr
	{
		$$ = nod(ODIV, $1, $3);
	}
|	expr '%' expr
	{
		$$ = nod(OMOD, $1, $3);
	}
|	expr '&' expr
	{
		$$ = nod(OAND, $1, $3);
	}
773 774 775 776
|	expr LANDNOT expr
	{
		$$ = nod(OANDNOT, $1, $3);
	}
777 778 779 780 781 782 783 784
|	expr LLSH expr
	{
		$$ = nod(OLSH, $1, $3);
	}
|	expr LRSH expr
	{
		$$ = nod(ORSH, $1, $3);
	}
Rob Pike's avatar
Rob Pike committed
785
|	expr LCOMM expr
Ken Thompson's avatar
Ken Thompson committed
786 787 788
	{
		$$ = nod(OSEND, $1, $3);
	}
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820

uexpr:
	pexpr
|	'*' uexpr
	{
		$$ = nod(OIND, $2, N);
	}
|	'&' uexpr
	{
		$$ = nod(OADDR, $2, N);
	}
|	'+' uexpr
	{
		$$ = nod(OPLUS, $2, N);
	}
|	'-' uexpr
	{
		$$ = nod(OMINUS, $2, N);
	}
|	'!' uexpr
	{
		$$ = nod(ONOT, $2, N);
	}
|	'~' uexpr
	{
		yyerror("the OCOM operator is ^");
		$$ = nod(OCOM, $2, N);
	}
|	'^' uexpr
	{
		$$ = nod(OCOM, $2, N);
	}
Rob Pike's avatar
Rob Pike committed
821
|	LCOMM uexpr
822 823 824 825 826 827 828
	{
		$$ = nod(ORECV, $2, N);
	}

pexpr:
	LLITERAL
	{
829
		$$ = nodlit($1);
830 831 832
	}
|	LNIL
	{
833 834 835
		Val v;
		v.ctype = CTNIL;
		$$ = nodlit(v);
836 837 838
	}
|	LTRUE
	{
839
		$$ = nodbool(1);
840 841 842
	}
|	LFALSE
	{
843 844 845 846 847
		$$ = nodbool(0);
	}
|	laconst
	{
		$$ = nod(OLITERAL, N, N);
Ken Thompson's avatar
Ken Thompson committed
848
		$$->sym = $1;
849 850
		$$->val = $1->oconst->val;
		$$->type = $1->oconst->type;
851 852 853
	}
|	LIOTA
	{
854
		$$ = nodintconst(iota);
Ken Thompson's avatar
Ken Thompson committed
855
		$$->iota = 1;	// flag to reevaluate on copy
856 857 858 859 860 861
	}
|	name
|	'(' expr ')'
	{
		$$ = $2;
	}
Ken Thompson's avatar
Ken Thompson committed
862
|	pexpr '.' sym2
863 864
	{
		$$ = nod(ODOT, $1, newname($3));
Ken Thompson's avatar
Ken Thompson committed
865
		$$ = adddot($$);
866 867 868
	}
|	pexpr '.' '(' type ')'
	{
869
		$$ = nod(ODOTTYPE, $1, N);
870 871
		$$->type = $4;
	}
Ken Thompson's avatar
Ken Thompson committed
872 873 874 875
|	pexpr '.' '(' LTYPE ')'
	{
		$$ = nod(OTYPESW, $1, N);
	}
876 877 878 879 880 881 882 883 884 885
|	pexpr '[' expr ']'
	{
		$$ = nod(OINDEX, $1, $3);
	}
|	pexpr '[' keyval ']'
	{
		$$ = nod(OSLICE, $1, $3);
	}
|	pexpr '(' oexpr_list ')'
	{
886 887 888
		$$ = unsafenmagic($1, $3);
		if($$ == N)
			$$ = nod(OCALL, $1, $3);
889
	}
Ken Thompson's avatar
Ken Thompson committed
890
|	LLEN '(' expr ')'
891 892 893
	{
		$$ = nod(OLEN, $3, N);
	}
894 895 896 897 898 899 900 901
|	LCLOSE '(' expr ')'
	{
		$$ = nod(OCLOSE, $3, N);
	}
|	LCLOSED '(' expr ')'
	{
		$$ = nod(OCLOSED, $3, N);
	}
Ken Thompson's avatar
Ken Thompson committed
902 903 904 905
|	LCAP '(' expr ')'
	{
		$$ = nod(OCAP, $3, N);
	}
906 907 908
|	LNEW '(' type ')'
	{
		$$ = nod(ONEW, N, N);
Ken Thompson's avatar
Ken Thompson committed
909
		$$->type = $3;
910
	}
Ken Thompson's avatar
Ken Thompson committed
911 912 913
|	LNEW '(' type ',' expr_list ')'
	{
		$$ = nod(ONEW, $5, N);
Ken Thompson's avatar
Ken Thompson committed
914
		$$->type = $3;
Ken Thompson's avatar
Ken Thompson committed
915
	}
Ken Thompson's avatar
Ken Thompson committed
916 917 918 919 920 921 922 923 924 925
|	LMAKE '(' type ')'
	{
		$$ = nod(OMAKE, N, N);
		$$->type = $3;
	}
|	LMAKE '(' type ',' expr_list ')'
	{
		$$ = nod(OMAKE, $5, N);
		$$->type = $3;
	}
926
|	convtype '(' expr ')'
Ken Thompson's avatar
Ken Thompson committed
927
	{
928
		// conversion
Russ Cox's avatar
Russ Cox committed
929 930 931
		$$ = rev($3);
		if($$ == N)
			$$ = nod(OEMPTY, N, N);
932
		$$ = nod(OCONV, $$, N);
Russ Cox's avatar
Russ Cox committed
933
		$$->type = $1;
Ken Thompson's avatar
Ken Thompson committed
934
	}
935
|	convtype '{' braced_keyexpr_list '}'
Ken Thompson's avatar
Ken Thompson committed
936
	{
937
		// composite expression
Ken Thompson's avatar
Ken Thompson committed
938 939 940
		$$ = rev($3);
		if($$ == N)
			$$ = nod(OEMPTY, N, N);
941
		$$ = nod(OCOMPOS, $$, N);
Ken Thompson's avatar
Ken Thompson committed
942 943 944
		$$->type = $1;
	}
|	fnliteral
945 946 947 948 949 950

/*
 * lexical symbols that can be
 * from other packages
 */
lpack:
Ken Thompson's avatar
Ken Thompson committed
951
	LPACK
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	{
		context = $1->name;
	}

laconst:
	LACONST
|	lpack '.' LACONST
	{
		$$ = $3;
		context = nil;
	}

lname:
	LNAME
|	lpack '.' LNAME
	{
		$$ = $3;
		context = nil;
	}

latype:
	LATYPE
974 975 976 977
|	lpackatype

lpackatype:
	lpack '.' LATYPE
978 979 980 981 982 983 984 985 986 987 988
	{
		$$ = $3;
		context = nil;
	}

/*
 * names and types
 *	newname is used before declared
 *	oldname is used after declared
 */
new_name:
Ken Thompson's avatar
Ken Thompson committed
989
	sym1
990 991 992 993
	{
		$$ = newname($1);
	}

994 995 996 997 998 999
new_field:
	sym2
	{
		$$ = newname($1);
	}

1000
new_type:
Ken Thompson's avatar
Ken Thompson committed
1001
	sym1
1002 1003 1004 1005
	{
		$$ = newtype($1);
	}

1006 1007 1008 1009 1010 1011
onew_name:
	{
		$$ = N;
	}
|	new_name

1012 1013 1014 1015 1016 1017
sym:
	LATYPE
|	LNAME
|	LACONST
|	LPACK

Ken Thompson's avatar
Ken Thompson committed
1018 1019
sym1:
	sym
Ken Thompson's avatar
Ken Thompson committed
1020
|	keyword
Ken Thompson's avatar
Ken Thompson committed
1021

1022 1023 1024 1025 1026
/*
 * keywords that can be field names
 * pretty much any name can be allowed
 * limited only by good taste
 */
Ken Thompson's avatar
Ken Thompson committed
1027
sym2:
Russ Cox's avatar
Russ Cox committed
1028
	sym1
Ken Thompson's avatar
Ken Thompson committed
1029 1030

/*
Ken Thompson's avatar
Ken Thompson committed
1031 1032
 * keywords that can be variables
 * but are not already legal expressions
Ken Thompson's avatar
Ken Thompson committed
1033
 */
Ken Thompson's avatar
Ken Thompson committed
1034 1035
sym3:
	LLEN
Ken Thompson's avatar
Ken Thompson committed
1036
|	LCAP
1037 1038
|	LCLOSE
|	LCLOSED
Ken Thompson's avatar
Ken Thompson committed
1039
|	LPANIC
Ken Thompson's avatar
Ken Thompson committed
1040
|	LPANICN
Ken Thompson's avatar
Ken Thompson committed
1041
|	LPRINT
Ken Thompson's avatar
Ken Thompson committed
1042
|	LPRINTN
Ken Thompson's avatar
Ken Thompson committed
1043
|	LNEW
Ken Thompson's avatar
Ken Thompson committed
1044
|	LMAKE
Ken Thompson's avatar
Ken Thompson committed
1045
|	LBASETYPE
Ken Thompson's avatar
Ken Thompson committed
1046

Ken Thompson's avatar
Ken Thompson committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
/*
 * keywords that we can
 * use as variable/type names
 */
keyword:
	sym3
|	LNIL
|	LTRUE
|	LFALSE
|	LIOTA

1058 1059 1060 1061 1062
name:
	lname
	{
		$$ = oldname($1);
	}
Russ Cox's avatar
Russ Cox committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	/*
	 * this rule introduces 1 reduce/reduce conflict
	 * with the rule lpack: LPACK above.
	 * the reduce/reduce conflict is only with
	 * lookahead '.', in which case the correct
	 * resolution is the lpack rule.  (and it wins
	 * because it is above.)
	 */
|	LPACK	%prec NotDot
	{
		$$ = oldname($1);
	}

labelname:
	name
|	keyword
	{
		$$ = oldname($1);
	}
1082

Ken Thompson's avatar
Ken Thompson committed
1083
convtype:
Ken Thompson's avatar
Ken Thompson committed
1084 1085 1086 1087
	latype
	{
		$$ = oldtype($1);
	}
Ken Thompson's avatar
Ken Thompson committed
1088
|	'[' oexpr ']' type
Ken Thompson's avatar
Ken Thompson committed
1089
	{
Ken Thompson's avatar
Ken Thompson committed
1090 1091
		// array literal
		$$ = aindex($2, $4);
Ken Thompson's avatar
Ken Thompson committed
1092
	}
Ken Thompson's avatar
Ken Thompson committed
1093 1094 1095 1096 1097 1098
|	'[' LDDD ']' type
	{
		// array literal of nelem
		$$ = aindex(N, $4);
		$$->bound = -100;
	}
Ken Thompson's avatar
Ken Thompson committed
1099
|	LMAP '[' type ']' type
Ken Thompson's avatar
Ken Thompson committed
1100
	{
Ken Thompson's avatar
Ken Thompson committed
1101
		// map literal
1102
		$$ = maptype($3, $5);
Ken Thompson's avatar
Ken Thompson committed
1103
	}
Ken Thompson's avatar
Ken Thompson committed
1104
|	structtype
1105 1106 1107 1108
|	'(' type ')'
	{
		$$ = $2;
	}
Ken Thompson's avatar
Ken Thompson committed
1109

Russ Cox's avatar
Russ Cox committed
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
/*
 * to avoid parsing conflicts, type is split into
 *	named types
 *	channel types
 *	function types
 *	any other type
 *
 * (and also into A/B as described above).
 *
 * the type system makes additional restrictions,
 * but those are not implemented in the grammar.
 */
1122
type:
Russ Cox's avatar
Russ Cox committed
1123 1124
	Atype
|	Btype
1125

Russ Cox's avatar
Russ Cox committed
1126 1127
Atype:
	Achantype
1128
|	Afntype
Russ Cox's avatar
Russ Cox committed
1129
|	Aothertype
1130

Russ Cox's avatar
Russ Cox committed
1131 1132 1133 1134 1135
Btype:
	nametype
|	Bchantype
|	Bfntype
|	Bothertype
1136 1137 1138 1139
|	'(' type ')'
	{
		$$ = $2;
	}
Russ Cox's avatar
Russ Cox committed
1140 1141 1142 1143 1144

non_name_type:
	chantype
|	fntype
|	othertype
Ken Thompson's avatar
Ken Thompson committed
1145 1146 1147 1148 1149 1150 1151
|	dotdotdot

dotdotdot:
	LDDD
	{
		$$ = typ(TDDD);
	}
Russ Cox's avatar
Russ Cox committed
1152 1153 1154 1155 1156 1157 1158 1159 1160

Anon_chan_type:
	Afntype
|	Aothertype

Bnon_chan_type:
	nametype
|	Bfntype
|	Bothertype
1161 1162 1163 1164
|	'(' Btype ')'
	{
		$$ = $2;
	}
Russ Cox's avatar
Russ Cox committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

Anon_fn_type:
	Achantype
|	Aothertype

Bnon_fn_type:
	nametype
|	Bchantype
|	Bothertype

nametype:
1176 1177
	LATYPE
	{
Ken Thompson's avatar
Ken Thompson committed
1178
		if($1->otype != T && $1->otype->etype == TANY)
Ken Thompson's avatar
Ken Thompson committed
1179
		if(strcmp(package, "PACKAGE") != 0)
Ken Thompson's avatar
Ken Thompson committed
1180
			yyerror("the any type is restricted");
1181 1182 1183
		$$ = oldtype($1);
	}

Russ Cox's avatar
Russ Cox committed
1184 1185 1186 1187 1188 1189
othertype:
	Aothertype
|	Bothertype

Aothertype:
	'[' oexpr ']' Atype
1190 1191 1192
	{
		$$ = aindex($2, $4);
	}
Russ Cox's avatar
Russ Cox committed
1193
|	LCOMM LCHAN Atype
1194 1195 1196
	{
		$$ = typ(TCHAN);
		$$->type = $3;
Rob Pike's avatar
Rob Pike committed
1197 1198
		$$->chan = Crecv;
	}
Russ Cox's avatar
Russ Cox committed
1199
|	LCHAN LCOMM Anon_chan_type
Rob Pike's avatar
Rob Pike committed
1200 1201 1202 1203
	{
		$$ = typ(TCHAN);
		$$->type = $3;
		$$->chan = Csend;
1204
	}
Russ Cox's avatar
Russ Cox committed
1205
|	LMAP '[' type ']' Atype
1206
	{
1207
		$$ = maptype($3, $5);
1208
	}
Russ Cox's avatar
Russ Cox committed
1209
|	'*' Atype
1210 1211 1212
	{
		$$ = ptrto($2);
	}
Russ Cox's avatar
Russ Cox committed
1213 1214
|	structtype
|	interfacetype
1215

Russ Cox's avatar
Russ Cox committed
1216 1217
Bothertype:
	lpackatype
Rob Pike's avatar
Rob Pike committed
1218
	{
Russ Cox's avatar
Russ Cox committed
1219
		$$ = oldtype($1);
Rob Pike's avatar
Rob Pike committed
1220
	}
Russ Cox's avatar
Russ Cox committed
1221
|	'[' oexpr ']' Btype
1222 1223 1224
	{
		$$ = aindex($2, $4);
	}
Russ Cox's avatar
Russ Cox committed
1225
|	LCOMM LCHAN Btype
1226 1227 1228
	{
		$$ = typ(TCHAN);
		$$->type = $3;
Rob Pike's avatar
Rob Pike committed
1229 1230
		$$->chan = Crecv;
	}
Russ Cox's avatar
Russ Cox committed
1231
|	LCHAN LCOMM Bnon_chan_type
Rob Pike's avatar
Rob Pike committed
1232 1233 1234 1235
	{
		$$ = typ(TCHAN);
		$$->type = $3;
		$$->chan = Csend;
1236
	}
Russ Cox's avatar
Russ Cox committed
1237
|	LMAP '[' type ']' Btype
1238
	{
1239
		$$ = maptype($3, $5);
1240
	}
Russ Cox's avatar
Russ Cox committed
1241
|	'*' Btype
1242 1243 1244
	{
		$$ = ptrto($2);
	}
1245

Russ Cox's avatar
Russ Cox committed
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
chantype:
	Achantype
|	Bchantype

Achantype:
	LCHAN Atype
	{
		$$ = typ(TCHAN);
		$$->type = $2;
		$$->chan = Cboth;
1256 1257
	}

Rob Pike's avatar
Rob Pike committed
1258
Bchantype:
Russ Cox's avatar
Russ Cox committed
1259
	LCHAN Btype
Rob Pike's avatar
Rob Pike committed
1260 1261 1262 1263 1264 1265
	{
		$$ = typ(TCHAN);
		$$->type = $2;
		$$->chan = Cboth;
	}

Ken Thompson's avatar
Ken Thompson committed
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
structtype:
	LSTRUCT '{' structdcl_list_r osemi '}'
	{
		$$ = dostruct(rev($3), TSTRUCT);
	}
|	LSTRUCT '{' '}'
	{
		$$ = dostruct(N, TSTRUCT);
	}

interfacetype:
	LINTERFACE '{' interfacedcl_list_r osemi '}'
	{
		$$ = dostruct(rev($3), TINTER);
		$$ = sortinter($$);
	}
|	LINTERFACE '{' '}'
	{
		$$ = dostruct(N, TINTER);
	}

1287 1288 1289
keyval:
	expr ':' expr
	{
Ken Thompson's avatar
Ken Thompson committed
1290
		$$ = nod(OKEY, $1, $3);
1291 1292 1293 1294 1295 1296 1297
	}

/*
 * function stuff
 * all in one place to show how crappy it all is
 */
xfndcl:
Ken Thompson's avatar
Ken Thompson committed
1298
	LFUNC
1299
	{
Ken Thompson's avatar
Ken Thompson committed
1300 1301 1302 1303 1304 1305
		maxarg = 0;
		stksize = 0;
	} fndcl fnbody
	{
		$$ = $3;
		$$->nbody = $4;
1306 1307 1308 1309 1310 1311 1312 1313 1314
		funcbody($$);
	}

fndcl:
	new_name '(' oarg_type_list ')' fnres
	{
		b0stack = dclstack;	// mark base for fn literals
		$$ = nod(ODCLFUNC, N, N);
		$$->nname = $1;
Ken Thompson's avatar
init  
Ken Thompson committed
1315 1316
		if($3 == N && $5 == N)
			$$->nname = renameinit($1);
1317 1318 1319 1320 1321 1322 1323
		$$->type = functype(N, $3, $5);
		funchdr($$);
	}
|	'(' oarg_type_list ')' new_name '(' oarg_type_list ')' fnres
	{
		b0stack = dclstack;	// mark base for fn literals
		$$ = nod(ODCLFUNC, N, N);
Ken Thompson's avatar
Ken Thompson committed
1324
		if(listcount($2) == 1) {
1325
			$$->nname = $4;
1326
			$$->nname = methodname($4, $2->type);
Ken Thompson's avatar
Ken Thompson committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
			$$->type = functype($2, $6, $8);
			funchdr($$);
			addmethod($4, $$->type, 1);
		} else {
			/* declare it as a function */
			yyerror("unknown method receiver");
			$$->nname = $4;
			$$->type = functype(N, $6, $8);
			funchdr($$);
		}
Ken Thompson's avatar
Ken Thompson committed
1337

1338 1339
	}

Ken Thompson's avatar
Ken Thompson committed
1340 1341 1342
fntype:
	Afntype
|	Bfntype
1343

Ken Thompson's avatar
Ken Thompson committed
1344
Afntype:
Russ Cox's avatar
Russ Cox committed
1345
	LFUNC '(' oarg_type_list ')' Afnres
1346
	{
Russ Cox's avatar
Russ Cox committed
1347
		$$ = functype(N, $3, $5);
1348 1349
	}

Ken Thompson's avatar
Ken Thompson committed
1350
Bfntype:
Russ Cox's avatar
Russ Cox committed
1351
	LFUNC '(' oarg_type_list ')' Bfnres
1352
	{
Russ Cox's avatar
Russ Cox committed
1353
		$$ = functype(N, $3, $5);
1354 1355 1356 1357 1358
	}

fnlitdcl:
	fntype
	{
Russ Cox's avatar
Russ Cox committed
1359
		markdcl();
1360
		$$ = $1;
Russ Cox's avatar
Russ Cox committed
1361
		funclit0($$);
1362 1363 1364
	}

fnliteral:
Russ Cox's avatar
Russ Cox committed
1365
	fnlitdcl '{' ostmt_list '}'
1366
	{
Russ Cox's avatar
Russ Cox committed
1367
		$$ = funclit1($1, $3);
1368 1369 1370
	}

fnbody:
Ken Thompson's avatar
Ken Thompson committed
1371
	'{' ostmt_list '}'
1372
	{
Ken Thompson's avatar
Ken Thompson committed
1373 1374
		$$ = $2;
		if($$ == N)
1375 1376
			$$ = nod(ORETURN, N, N);
	}
Russ Cox's avatar
Russ Cox committed
1377
|	{
1378 1379
		$$ = N;
	}
Ken Thompson's avatar
Ken Thompson committed
1380

1381
fnres:
1382 1383 1384 1385
	Afnres
|	Bfnres

Afnres:
Russ Cox's avatar
Russ Cox committed
1386
	Anon_fn_type
1387 1388 1389 1390 1391 1392
	{
		$$ = nod(ODCLFIELD, N, N);
		$$->type = $1;
		$$ = cleanidlist($$);
	}

1393
Bfnres:
Russ Cox's avatar
Russ Cox committed
1394
	%prec NotParen
1395 1396 1397
	{
		$$ = N;
	}
Russ Cox's avatar
Russ Cox committed
1398
|	Bnon_fn_type
1399 1400 1401 1402 1403
	{
		$$ = nod(ODCLFIELD, N, N);
		$$->type = $1;
		$$ = cleanidlist($$);
	}
Russ Cox's avatar
Russ Cox committed
1404 1405 1406 1407
|	'(' oarg_type_list ')'
	{
		$$ = $2;
	}
1408

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
/*
 * lists of things
 * note that they are left recursive
 * to conserve yacc stack. they need to
 * be reversed to interpret correctly
 */
xdcl_list_r:
	xdcl
|	xdcl_list_r xdcl
	{
Ken Thompson's avatar
Ken Thompson committed
1419
		$$ = list($1, $2);
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
	}

vardcl_list_r:
	vardcl
|	vardcl_list_r ';' vardcl
	{
		$$ = nod(OLIST, $1, $3);
	}

constdcl_list_r:
Ken Thompson's avatar
Ken Thompson committed
1430 1431
	constdcl1
|	constdcl_list_r ';' constdcl1
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459

typedcl_list_r:
	typedcl
|	typedcl_list_r ';' typedcl

structdcl_list_r:
	structdcl
	{
		$$ = cleanidlist($1);
	}
|	structdcl_list_r ';' structdcl
	{
		$$ = cleanidlist($3);
		$$ = nod(OLIST, $1, $$);
	}

interfacedcl_list_r:
	interfacedcl
	{
		$$ = cleanidlist($1);
	}
|	interfacedcl_list_r ';' interfacedcl
	{
		$$ = cleanidlist($3);
		$$ = nod(OLIST, $1, $$);
	}

structdcl:
1460
	new_field ',' structdcl
1461 1462 1463 1464
	{
		$$ = nod(ODCLFIELD, $1, N);
		$$ = nod(OLIST, $$, $3);
	}
1465
|	new_field type oliteral
1466 1467 1468
	{
		$$ = nod(ODCLFIELD, $1, N);
		$$->type = $2;
1469
		$$->val = $3;
1470
	}
Russ Cox's avatar
Russ Cox committed
1471 1472 1473 1474 1475 1476
|	embed oliteral
	{
		$$ = $1;
		$$->val = $2;
	}
|	'*' embed oliteral
Ken Thompson's avatar
Ken Thompson committed
1477
	{
Ken Thompson's avatar
Ken Thompson committed
1478 1479
		$$ = $2;
		$$->type = ptrto($$->type);
Russ Cox's avatar
Russ Cox committed
1480
		$$->val = $3;
Ken Thompson's avatar
Ken Thompson committed
1481 1482
	}

Ken Thompson's avatar
Ken Thompson committed
1483
embed:
Ken Thompson's avatar
Ken Thompson committed
1484 1485
	LATYPE
	{
Ken Thompson's avatar
Ken Thompson committed
1486
		$$ = embedded($1);
1487 1488 1489
	}
|	lpack '.' LATYPE
	{
Ken Thompson's avatar
Ken Thompson committed
1490
		$$ = embedded($3);
1491
		context = nil;
Ken Thompson's avatar
Ken Thompson committed
1492
	}
1493

1494 1495
interfacedcl1:
	new_name ',' interfacedcl1
1496 1497 1498 1499
	{
		$$ = nod(ODCLFIELD, $1, N);
		$$ = nod(OLIST, $$, $3);
	}
Ken Thompson's avatar
Ken Thompson committed
1500
|	new_name indcl
1501 1502 1503 1504 1505
	{
		$$ = nod(ODCLFIELD, $1, N);
		$$->type = $2;
	}

1506 1507 1508 1509 1510 1511 1512 1513
interfacedcl:
	interfacedcl1
|	latype
	{
		$$ = nod(ODCLFIELD, N, N);
		$$->type = oldtype($1);
	}

Ken Thompson's avatar
Ken Thompson committed
1514
indcl:
1515 1516 1517 1518 1519 1520
	'(' oarg_type_list ')' fnres
	{
		// without func keyword
		$$ = functype(fakethis(), $2, $4);
	}

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
/*
 * function arguments.
 *
 * the hard part is that when we're reading a list of names,
 * we don't know if they are going to be the names of
 * parameters (like "a,b,c int") or the types of anonymous
 * parameters (like "int, string, bool").
 *
 * an arg_chunk is a comma-separated list of arguments
 * that ends in an obvious type, either "a, b, c x" or "a, b, c, *x".
 * in the first case, a, b, c are parameters of type x.
 * in the second case, a, b, c, and *x are types of anonymous parameters.
 */
arg_chunk:
	new_name_list_r type
1536
	{
1537
		$$ = nametodcl($1, $2);
1538
	}
Ken Thompson's avatar
Ken Thompson committed
1539 1540 1541 1542
|	new_name_list_r dotdotdot
	{
		$$ = nametodcl($1, $2);
	}
1543
|	non_name_type
1544
	{
1545
		$$ = anondcl($1);
1546
	}
1547
|	new_name_list_r ',' non_name_type
1548
	{
1549 1550 1551 1552 1553 1554 1555 1556 1557
		$1 = nametoanondcl($1);
		$$ = appendr($1, anondcl($3));
	}

arg_chunk_list_r:
	arg_chunk
|	arg_chunk_list_r ',' arg_chunk
	{
		$$ = appendr($1, $3);
1558 1559
	}

1560 1561 1562 1563 1564
/*
 * an arg type list is a sequence of arg chunks,
 * possibly ending in a list of names (plain "a,b,c"),
 * which must be the types of anonymous parameters.
 */
1565
arg_type_list_r:
1566 1567
	arg_chunk_list_r
|	arg_chunk_list_r ',' new_name_list_r
1568
	{
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
		$3 = nametoanondcl($3);
		$$ = appendr($1, $3);
	}
|	new_name_list_r
	{
		$$ = nametoanondcl($1);
	}

arg_type_list:
	arg_type_list_r
	{
		$$ = rev($1);
		checkarglist($$);
1582 1583
	}

Ken Thompson's avatar
Ken Thompson committed
1584
/*
Russ Cox's avatar
Russ Cox committed
1585
 * statement that doesn't need semicolon terminator
Ken Thompson's avatar
Ken Thompson committed
1586
 */
Ken Thompson's avatar
Ken Thompson committed
1587 1588
Astmt:
	complex_stmt
Ken Thompson's avatar
Ken Thompson committed
1589
|	compound_stmt
1590
|	Acommon_dcl
Ken Thompson's avatar
asdf  
Ken Thompson committed
1591 1592 1593 1594
|	';'
	{
		$$ = N;
	}
Ken Thompson's avatar
Ken Thompson committed
1595
|	error Astmt
1596 1597 1598
	{
		$$ = N;
	}
Russ Cox's avatar
Russ Cox committed
1599
|	labelname ':'
Russ Cox's avatar
Russ Cox committed
1600 1601 1602 1603
	{
		$$ = nod(OLABEL, $1, N);
	}
|	Bstmt ';'
Ken Thompson's avatar
Ken Thompson committed
1604

Ken Thompson's avatar
Ken Thompson committed
1605
/*
Russ Cox's avatar
Russ Cox committed
1606
 * statement that does
Ken Thompson's avatar
Ken Thompson committed
1607
 */
Ken Thompson's avatar
Ken Thompson committed
1608 1609
Bstmt:
	semi_stmt
1610
|	Bcommon_dcl
Russ Cox's avatar
Russ Cox committed
1611
|	simple_stmt
Ken Thompson's avatar
Ken Thompson committed
1612 1613

/*
Russ Cox's avatar
Russ Cox committed
1614
 * statement list that doesn't need semicolon terminator
Ken Thompson's avatar
Ken Thompson committed
1615
 */
Ken Thompson's avatar
Ken Thompson committed
1616 1617 1618
Astmt_list_r:
	Astmt
|	Astmt_list_r Astmt
1619
	{
Ken Thompson's avatar
Ken Thompson committed
1620
		$$ = list($1, $2);
Ken Thompson's avatar
Ken Thompson committed
1621 1622
	}

Ken Thompson's avatar
Ken Thompson committed
1623
/*
Russ Cox's avatar
Russ Cox committed
1624
 * statement list that needs semicolon terminator
Ken Thompson's avatar
Ken Thompson committed
1625
 */
Ken Thompson's avatar
Ken Thompson committed
1626 1627
Bstmt_list_r:
	Bstmt
Ken Thompson's avatar
Ken Thompson committed
1628
|	Astmt_list_r Bstmt
1629
	{
Ken Thompson's avatar
Ken Thompson committed
1630
		$$ = list($1, $2);
1631
	}
Ken Thompson's avatar
Ken Thompson committed
1632 1633 1634 1635

stmt_list_r:
	Astmt_list_r
|	Bstmt_list_r
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

expr_list_r:
	expr
|	expr_list_r ',' expr
	{
		$$ = nod(OLIST, $1, $3);
	}

new_name_list_r:
	new_name
|	new_name_list_r ',' new_name
	{
		$$ = nod(OLIST, $1, $3);
	}

Ken Thompson's avatar
Ken Thompson committed
1651 1652 1653 1654 1655 1656
exprsym3:
	expr
|	sym3
	{
		$$ = newname($1);
	}
Ken Thompson's avatar
Ken Thompson committed
1657 1658 1659 1660
|	LATYPE
	{
		$$ = newname($1);
	}
Ken Thompson's avatar
Ken Thompson committed
1661 1662 1663 1664 1665 1666 1667 1668

exprsym3_list_r:
	exprsym3
|	exprsym3_list_r ',' exprsym3
	{
		$$ = nod(OLIST, $1, $3);
	}

1669 1670
import_stmt_list_r:
	import_stmt
Russ Cox's avatar
Russ Cox committed
1671
|	import_stmt_list_r ';' import_stmt
1672 1673 1674 1675

hidden_import_list_r:
|	hidden_import_list_r hidden_import

Russ Cox's avatar
Russ Cox committed
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
hidden_funarg_list_r:
	hidden_dcl
|	hidden_funarg_list_r ',' hidden_dcl
	{
		$$ = nod(OLIST, $1, $3);
	}

hidden_funarg_list:
	hidden_funarg_list_r
	{
		$$ = rev($1);
	}

hidden_structdcl_list_r:
Russ Cox's avatar
Russ Cox committed
1690 1691
	hidden_structdcl
|	hidden_structdcl_list_r ';' hidden_structdcl
Russ Cox's avatar
Russ Cox committed
1692 1693 1694 1695 1696 1697
	{
		$$ = nod(OLIST, $1, $3);
	}

hidden_structdcl_list:
	hidden_structdcl_list_r
1698
	{
Russ Cox's avatar
Russ Cox committed
1699
		$$ = rev($1);
1700 1701
	}

Russ Cox's avatar
Russ Cox committed
1702 1703 1704
hidden_interfacedcl_list_r:
	hidden_interfacedcl
|	hidden_interfacedcl_list_r ';' hidden_interfacedcl
1705
	{
Russ Cox's avatar
Russ Cox committed
1706 1707 1708 1709 1710 1711 1712
		$$ = nod(OLIST, $1, $3);
	}

hidden_interfacedcl_list:
	hidden_interfacedcl_list_r
	{
		$$ = rev($1);
1713 1714 1715 1716 1717 1718 1719 1720 1721
	}

keyval_list_r:
	keyval
|	keyval_list_r ',' keyval
	{
		$$ = nod(OLIST, $1, $3);
	}

1722 1723 1724 1725 1726 1727 1728
/*
 * have to spell this out using _r lists to avoid yacc conflict
 */
braced_keyexpr_list:
	{
		$$ = N;
	}
1729
|	keyval_list_r ocomma
1730 1731 1732
	{
		$$ = rev($1);
	}
1733
|	expr_list_r ocomma
1734 1735 1736 1737
	{
		$$ = rev($1);
	}

Ken Thompson's avatar
Ken Thompson committed
1738

1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
/*
 * the one compromise of a
 * non-reversed list
 */
expr_list:
	expr_list_r
	{
		$$ = rev($1);
	}

1749

1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
/*
 * optional things
 */
osemi:
|	';'

ocomma:
|	','

oexpr:
	{
		$$ = N;
	}
|	expr

oexpr_list:
	{
		$$ = N;
	}
|	expr_list

osimple_stmt:
	{
		$$ = N;
	}
|	simple_stmt

ostmt_list:
	{
		$$ = N;
	}
|	stmt_list_r
	{
		$$ = rev($1);
	}

oxdcl_list:
	{
		$$ = N;
	}
|	xdcl_list_r
	{
		$$ = rev($1);
	}

Russ Cox's avatar
Russ Cox committed
1795
oarg_type_list:
1796 1797 1798
	{
		$$ = N;
	}
Russ Cox's avatar
Russ Cox committed
1799 1800 1801
|	arg_type_list

ohidden_funarg_list:
1802
	{
Russ Cox's avatar
Russ Cox committed
1803
		$$ = N;
1804
	}
Russ Cox's avatar
Russ Cox committed
1805
|	hidden_funarg_list
1806

Russ Cox's avatar
Russ Cox committed
1807
ohidden_structdcl_list:
1808 1809 1810
	{
		$$ = N;
	}
Russ Cox's avatar
Russ Cox committed
1811 1812 1813
|	hidden_structdcl_list

ohidden_interfacedcl_list:
1814
	{
Russ Cox's avatar
Russ Cox committed
1815
		$$ = N;
1816
	}
Russ Cox's avatar
Russ Cox committed
1817
|	hidden_interfacedcl_list
1818

1819 1820 1821 1822 1823 1824
oliteral:
	{
		$$.ctype = CTxxx;
	}
|	LLITERAL

1825 1826 1827 1828 1829
/*
 * import syntax from header of
 * an output package
 */
hidden_import:
Russ Cox's avatar
Russ Cox committed
1830
	LPACKAGE sym1
1831
	/* variables */
Russ Cox's avatar
Russ Cox committed
1832
|	LVAR hidden_pkg_importsym hidden_type
Russ Cox's avatar
Russ Cox committed
1833
	{
Russ Cox's avatar
Russ Cox committed
1834
		importvar($2, $3, PEXTERN);
Russ Cox's avatar
Russ Cox committed
1835
	}
Russ Cox's avatar
Russ Cox committed
1836
|	LCONST hidden_pkg_importsym '=' hidden_constant
Russ Cox's avatar
Russ Cox committed
1837
	{
1838
		importconst($2, types[TIDEAL], $4);
Russ Cox's avatar
Russ Cox committed
1839
	}
Russ Cox's avatar
Russ Cox committed
1840
|	LCONST hidden_pkg_importsym hidden_type '=' hidden_constant
Russ Cox's avatar
Russ Cox committed
1841
	{
1842
		importconst($2, $3, $5);
Russ Cox's avatar
Russ Cox committed
1843
	}
Russ Cox's avatar
Russ Cox committed
1844
|	LTYPE hidden_pkg_importsym hidden_type
Russ Cox's avatar
Russ Cox committed
1845
	{
Russ Cox's avatar
Russ Cox committed
1846
		importtype($2, $3);
Russ Cox's avatar
Russ Cox committed
1847
	}
Russ Cox's avatar
Russ Cox committed
1848
|	LFUNC hidden_pkg_importsym '(' ohidden_funarg_list ')' ohidden_funres
1849
	{
Russ Cox's avatar
Russ Cox committed
1850
		importvar($2, functype(N, $4, $6), PFUNC);
1851
	}
Russ Cox's avatar
Russ Cox committed
1852
|	LFUNC '(' hidden_funarg_list ')' sym1 '(' ohidden_funarg_list ')' ohidden_funres
Russ Cox's avatar
Russ Cox committed
1853
	{
Russ Cox's avatar
Russ Cox committed
1854
		if($3->op != ODCLFIELD) {
Russ Cox's avatar
Russ Cox committed
1855 1856 1857
			yyerror("bad receiver in method");
			YYERROR;
		}
Russ Cox's avatar
Russ Cox committed
1858
		importmethod($5, functype($3, $7, $9));
Russ Cox's avatar
Russ Cox committed
1859 1860 1861 1862 1863
	}

hidden_type:
	hidden_type1
|	hidden_type2
1864

Russ Cox's avatar
Russ Cox committed
1865 1866
hidden_type1:
	hidden_importsym
1867
	{
Russ Cox's avatar
Russ Cox committed
1868
		$$ = pkgtype($1->sym->name, $1->psym->name);
1869
	}
Russ Cox's avatar
Russ Cox committed
1870
|	LATYPE
1871
	{
Russ Cox's avatar
Russ Cox committed
1872
		$$ = oldtype($1);
1873
	}
Russ Cox's avatar
Russ Cox committed
1874 1875 1876 1877 1878 1879
|	'[' ']' hidden_type
	{
		$$ = aindex(N, $3);
	}
|	'[' LLITERAL ']' hidden_type
	{
1880
		$$ = aindex(nodlit($2), $4);
Russ Cox's avatar
Russ Cox committed
1881 1882 1883
	}
|	LMAP '[' hidden_type ']' hidden_type
	{
1884
		$$ = maptype($3, $5);
Russ Cox's avatar
Russ Cox committed
1885 1886
	}
|	LSTRUCT '{' ohidden_structdcl_list '}'
1887
	{
Russ Cox's avatar
Russ Cox committed
1888
		$$ = dostruct($3, TSTRUCT);
1889
	}
Russ Cox's avatar
Russ Cox committed
1890
|	LINTERFACE '{' ohidden_interfacedcl_list '}'
1891
	{
Russ Cox's avatar
Russ Cox committed
1892 1893
		$$ = dostruct($3, TINTER);
		$$ = sortinter($$);
1894
	}
Russ Cox's avatar
Russ Cox committed
1895
|	'*' hidden_type
Ken Thompson's avatar
Ken Thompson committed
1896
	{
1897
		checkwidth($2);
Russ Cox's avatar
Russ Cox committed
1898
		$$ = ptrto($2);
Ken Thompson's avatar
Ken Thompson committed
1899
	}
Russ Cox's avatar
Russ Cox committed
1900
|	LCOMM LCHAN hidden_type
1901
	{
Russ Cox's avatar
Russ Cox committed
1902 1903 1904
		$$ = typ(TCHAN);
		$$->type = $3;
		$$->chan = Crecv;
1905
	}
Russ Cox's avatar
Russ Cox committed
1906
|	LCHAN LCOMM hidden_type1
1907
	{
Russ Cox's avatar
Russ Cox committed
1908 1909 1910
		$$ = typ(TCHAN);
		$$->type = $3;
		$$->chan = Csend;
1911
	}
Ken Thompson's avatar
Ken Thompson committed
1912 1913 1914 1915
|	LDDD
	{
		$$ = typ(TDDD);
	}
Russ Cox's avatar
Russ Cox committed
1916 1917 1918

hidden_type2:
	LCHAN hidden_type
1919
	{
Russ Cox's avatar
Russ Cox committed
1920 1921 1922
		$$ = typ(TCHAN);
		$$->type = $2;
		$$->chan = Cboth;
1923
	}
Russ Cox's avatar
Russ Cox committed
1924
|	LFUNC '(' ohidden_funarg_list ')' ohidden_funres
1925
	{
Russ Cox's avatar
Russ Cox committed
1926
		$$ = functype(N, $3, $5);
1927
	}
Russ Cox's avatar
Russ Cox committed
1928 1929 1930

hidden_dcl:
	sym1 hidden_type
1931
	{
Russ Cox's avatar
Russ Cox committed
1932 1933
		$$ = nod(ODCLFIELD, newname($1), N);
		$$->type = $2;
1934
	}
Russ Cox's avatar
Russ Cox committed
1935
|	'?' hidden_type
Ken Thompson's avatar
Ken Thompson committed
1936
	{
Russ Cox's avatar
Russ Cox committed
1937 1938
		$$ = nod(ODCLFIELD, N, N);
		$$->type = $2;
Ken Thompson's avatar
Ken Thompson committed
1939
	}
Russ Cox's avatar
Russ Cox committed
1940

Russ Cox's avatar
Russ Cox committed
1941
hidden_structdcl:
Russ Cox's avatar
Russ Cox committed
1942
	sym1 hidden_type oliteral
Russ Cox's avatar
Russ Cox committed
1943 1944 1945
	{
		$$ = nod(ODCLFIELD, newname($1), N);
		$$->type = $2;
Russ Cox's avatar
Russ Cox committed
1946
		$$->val = $3;
Russ Cox's avatar
Russ Cox committed
1947
	}
Russ Cox's avatar
Russ Cox committed
1948
|	'?' hidden_type oliteral
Russ Cox's avatar
Russ Cox committed
1949
	{
Ken Thompson's avatar
Ken Thompson committed
1950 1951 1952 1953 1954
		if(isptr[$2->etype]) {
			$$ = embedded($2->type->sym);
			$$->type = ptrto($$->type);
		} else
			$$ = embedded($2->sym);
Russ Cox's avatar
Russ Cox committed
1955
		$$->val = $3;
Russ Cox's avatar
Russ Cox committed
1956 1957
	}

Russ Cox's avatar
Russ Cox committed
1958 1959
hidden_interfacedcl:
	sym1 '(' ohidden_funarg_list ')' ohidden_funres
Ken Thompson's avatar
Ken Thompson committed
1960
	{
Russ Cox's avatar
Russ Cox committed
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
		$$ = nod(ODCLFIELD, newname($1), N);
		$$->type = functype(fakethis(), $3, $5);
	}

ohidden_funres:
	{
		$$ = N;
	}
|	hidden_funres

hidden_funres:
	'(' ohidden_funarg_list ')'
	{
		$$ = $2;
	}
|	hidden_type1
	{
		$$ = nod(ODCLFIELD, N, N);
		$$->type = $1;
Ken Thompson's avatar
Ken Thompson committed
1980
	}
1981

1982 1983
hidden_constant:
	LLITERAL
1984 1985 1986
	{
		$$ = nodlit($1);
	}
1987 1988
|	'-' LLITERAL
	{
1989 1990
		$$ = nodlit($2);
		switch($$->val.ctype){
1991
		case CTINT:
1992
			mpnegfix($$->val.u.xval);
1993 1994
			break;
		case CTFLT:
1995
			mpnegflt($$->val.u.fval);
1996 1997 1998 1999 2000
			break;
		default:
			yyerror("bad negated constant");
		}
	}
Russ Cox's avatar
Russ Cox committed
2001 2002
|	LTRUE
	{
2003
		$$ = nodbool(1);
Russ Cox's avatar
Russ Cox committed
2004 2005 2006
	}
|	LFALSE
	{
2007
		$$ = nodbool(0);
Russ Cox's avatar
Russ Cox committed
2008
	}
2009

Russ Cox's avatar
Russ Cox committed
2010
hidden_importsym:
2011
	sym1 '.' sym2
2012 2013 2014 2015 2016 2017
	{
		$$ = nod(OIMPORT, N, N);
		$$->osym = $1;
		$$->psym = $1;
		$$->sym = $3;
	}
2018

Russ Cox's avatar
Russ Cox committed
2019 2020 2021 2022 2023 2024 2025 2026
hidden_pkg_importsym:
	hidden_importsym
	{
		$$ = $1;
		pkgcontext = $$->psym->name;
	}


2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
/*
 * helpful error messages.
 * THIS SECTION MUST BE AT THE END OF THE FILE.
 *
 * these rules trigger reduce/reduce conflicts in the grammar.
 * they are safe because reduce/reduce conflicts are resolved
 * in favor of rules appearing earlier in the grammar, and these
 * are at the end of the file.
 *
 * to check whether the rest of the grammar is free of
 * reduce/reduce conflicts, comment this section out by
 * removing the slash on the next line.
2039
 */
2040 2041 2042 2043 2044 2045 2046 2047
lpack:
	LATYPE
	{
		yyerror("%s is type, not package", $1->name);
		YYERROR;
	}

laconst:
Russ Cox's avatar
Russ Cox committed
2048
	LATYPE
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
	{
		yyerror("%s is type, not var", $1->name);
		YYERROR;
	}

latype:
	LACONST
	{
		yyerror("%s is const, not type", $1->name);
		YYERROR;
	}
|	LPACK
	{
		yyerror("%s is package, not type", $1->name);
		YYERROR;
	}
|	LNAME
	{
Russ Cox's avatar
Russ Cox committed
2067 2068 2069
		yyerror("no type %s", $1->name);
		YYERROR;
	}
2070

Russ Cox's avatar
Russ Cox committed
2071 2072 2073 2074 2075 2076 2077
nametype:
	LNAME
	{
		yyerror("no type %s", $1->name);
		YYERROR;
	}

2078 2079
/**/