"README.md" did not exist on "e2711cb202081261d4586be525b6281544a7dac7"
dcl.c 26.9 KB
Newer Older
1 2 3 4 5 6 7
// 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"
#include	"y.tab.h"

Ken Thompson's avatar
Ken Thompson committed
8 9 10 11 12 13 14 15 16 17 18 19
int
dflag(void)
{
	if(!debug['d'])
		return 0;
	if(debug['y'])
		return 1;
	if(inimportsys)
		return 0;
	return 1;
}

20 21 22 23 24 25
void
dodclvar(Node *n, Type *t)
{
	if(n == N)
		return;

26 27
	if(t != T && (t->etype == TIDEAL || t->etype == TNIL))
		fatal("dodclvar %T", t);
28
	for(; n->op == OLIST; n = n->right)
29 30
		dodclvar(n->left, t);

31
	dowidth(t);
32 33 34 35 36 37 38

	// in case of type checking error,
	// use "undefined" type for variable type,
	// to avoid fatal in addvar.
	if(t == T)
		t = typ(TFORW);

39
	addvar(n, t, dclcontext);
Russ Cox's avatar
Russ Cox committed
40
	autoexport(n->sym);
41
	addtop = list(addtop, nod(ODCL, n, N));
42 43 44
}

void
45
dodclconst(Node *n, Node *e)
46
{
47
	if(n == N)
48
		return;
49 50 51 52 53

	for(; n->op == OLIST; n=n->right)
		dodclconst(n, e);

	addconst(n, e, dclcontext);
Russ Cox's avatar
Russ Cox committed
54
	autoexport(n->sym);
55 56
}

57 58 59 60 61 62
/*
 * introduce a type named n
 * but it is an unknown type for now
 */
Type*
dodcltype(Type *n)
63 64 65
{
	Sym *s;

66 67 68
	// if n has been forward declared,
	// use the Type* created then
	s = n->sym;
Russ Cox's avatar
Russ Cox committed
69
	if(s->block == block && s->otype != T) {
70 71 72
		switch(s->otype->etype) {
		case TFORWSTRUCT:
		case TFORWINTER:
Russ Cox's avatar
Russ Cox committed
73 74
			n = s->otype;
			goto found;
75
		}
76 77
	}

78 79
	// otherwise declare a new type
	addtyp(n, dclcontext);
Russ Cox's avatar
Russ Cox committed
80 81

found:
82
	n->local = 1;
Russ Cox's avatar
Russ Cox committed
83
	autoexport(n->sym);
84 85 86 87 88 89 90 91 92 93
	return n;
}

/*
 * now we know what n is: it's t
 */
void
updatetype(Type *n, Type *t)
{
	Sym *s;
94 95

	s = n->sym;
96 97
	if(s == S || s->otype != n)
		fatal("updatetype %T = %T", n, t);
98

99 100 101
	switch(n->etype) {
	case TFORW:
		break;
102

103 104 105 106 107 108
	case TFORWSTRUCT:
		if(t->etype != TSTRUCT) {
			yyerror("%T forward declared as struct", n);
			return;
		}
		break;
Russ Cox's avatar
Russ Cox committed
109

110 111 112 113 114 115
	case TFORWINTER:
		if(t->etype != TINTER) {
			yyerror("%T forward declared as interface", n);
			return;
		}
		break;
116

117 118 119
	default:
		fatal("updatetype %T / %T", n, t);
	}
120

121 122
	if(n->local)
		t->local = 1;
123 124
	*n = *t;
	n->sym = s;
125

126 127 128 129 130 131 132 133
	// catch declaration of incomplete type
	switch(n->etype) {
	case TFORWSTRUCT:
	case TFORWINTER:
		break;
	default:
		checkwidth(n);
	}
134 135
}

136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/*
 * return nelem of list
 */
int
listcount(Node *n)
{
	int v;

	v = 0;
	while(n != N) {
		v++;
		if(n->op != OLIST)
			break;
		n = n->right;
	}
	return v;
}

/*
 * turn a parsed function declaration
 * into a type
 */
Type*
functype(Node *this, Node *in, Node *out)
{
	Type *t;

	t = typ(TFUNC);

Russ Cox's avatar
Russ Cox committed
166 167 168
	t->type = dostruct(this, TFUNC);
	t->type->down = dostruct(out, TFUNC);
	t->type->down->down = dostruct(in, TFUNC);
169 170 171 172 173

	t->thistuple = listcount(this);
	t->outtuple = listcount(out);
	t->intuple = listcount(in);

174
	checkwidth(t);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	return t;
}

int
methcmp(Type *t1, Type *t2)
{
	if(t1->etype != TFUNC)
		return 0;
	if(t2->etype != TFUNC)
		return 0;

	t1 = t1->type->down;	// skip this arg
	t2 = t2->type->down;	// skip this arg
	for(;;) {
		if(t1 == t2)
			break;
		if(t1 == T || t2 == T)
			return 0;
		if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
			return 0;

		if(!eqtype(t1->type, t2->type, 0))
			return 0;

		t1 = t1->down;
		t2 = t2->down;
	}
	return 1;
}

Russ Cox's avatar
Russ Cox committed
205
Sym*
Russ Cox's avatar
Russ Cox committed
206
methodsym(Sym *nsym, Type *t0)
207
{
208
	Sym *s;
Russ Cox's avatar
Russ Cox committed
209
	char buf[NSYMB];
Russ Cox's avatar
Russ Cox committed
210
	Type *t;
211

Russ Cox's avatar
Russ Cox committed
212
	t = t0;
Ken Thompson's avatar
Ken Thompson committed
213 214
	if(t == T)
		goto bad;
215
	s = t->sym;
Russ Cox's avatar
Russ Cox committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	if(s == S) {
		if(!isptr[t->etype])
			goto bad;
		t = t->type;
		if(t == T)
			goto bad;
		s = t->sym;
		if(s == S)
			goto bad;
	}

	// if t0 == *t and t0 has a sym,
	// we want to see *t, not t0, in the method name.
	if(t != t0 && t0->sym)
		t0 = ptrto(t);
Ken Thompson's avatar
Ken Thompson committed
231

Russ Cox's avatar
Russ Cox committed
232
	snprint(buf, sizeof(buf), "%#hT·%s", t0, nsym->name);
Russ Cox's avatar
Russ Cox committed
233 234
//print("methodname %s\n", buf);
	return pkglookup(buf, s->opackage);
235 236

bad:
Ken Thompson's avatar
Ken Thompson committed
237
	yyerror("illegal <this> type: %T", t);
Russ Cox's avatar
Russ Cox committed
238 239 240 241 242 243 244
	return S;
}

Node*
methodname(Node *n, Type *t)
{
	Sym *s;
245

Russ Cox's avatar
Russ Cox committed
246 247 248 249
	s = methodsym(n->sym, t);
	if(s == S)
		return n;
	return newname(s);
250 251 252 253
}

/*
 * add a method, declared as a function,
Ken Thompson's avatar
Ken Thompson committed
254
 * n is fieldname, pa is base type, t is function type
255 256
 */
void
Ken Thompson's avatar
Ken Thompson committed
257
addmethod(Node *n, Type *t, int local)
258
{
Ken Thompson's avatar
Ken Thompson committed
259
	Type *f, *d, *pa;
260
	Sym *sf;
261

262 263 264
	pa = nil;
	sf = nil;

Ken Thompson's avatar
Ken Thompson committed
265 266 267
	// get field sym
	if(n == N)
		goto bad;
268 269
	if(n->op != ONAME)
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
270 271
	sf = n->sym;
	if(sf == S)
272
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
273 274 275

	// get parent type sym
	pa = *getthis(t);	// ptr to this structure
276 277
	if(pa == T)
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
278 279
	pa = pa->type;		// ptr to this field
	if(pa == T)
280
		goto bad;
Ken Thompson's avatar
Ken Thompson committed
281 282
	pa = pa->type;		// ptr to this type
	if(pa == T)
283 284
		goto bad;

285
	f = dclmethod(pa);
286 287
	if(f == T)
		goto bad;
288

289
	if(local && !f->local) {
290
		yyerror("cannot define methods on non-local type %T", t);
291 292 293
		return;
	}

294 295
	pa = f;

296 297 298
	if(pkgimportname != S && !exportname(sf->name))
		sf = pkglookup(sf->name, pkgimportname->name);

Ken Thompson's avatar
Ken Thompson committed
299 300 301
	n = nod(ODCLFIELD, newname(sf), N);
	n->type = t;

302
	d = T;	// last found
Ken Thompson's avatar
Ken Thompson committed
303
	for(f=pa->method; f!=T; f=f->down) {
304 305 306
		if(f->etype != TFIELD)
			fatal("addmethod: not TFIELD: %N", f);

Ken Thompson's avatar
Ken Thompson committed
307
		if(strcmp(sf->name, f->sym->name) != 0) {
308 309 310
			d = f;
			continue;
		}
311
		if(!eqtype(t, f->type, 0)) {
312
			yyerror("method redeclared: %T.%S", pa, sf);
313 314
			print("\t%T\n\t%T\n", f->type, t);
		}
Ken Thompson's avatar
Ken Thompson committed
315
		return;
316 317 318
	}

	if(d == T)
Russ Cox's avatar
bug134  
Russ Cox committed
319
		stotype(n, 0, &pa->method);
320
	else
Russ Cox's avatar
bug134  
Russ Cox committed
321
		stotype(n, 0, &d->down);
Ken Thompson's avatar
Ken Thompson committed
322 323

	if(dflag())
324
		print("method         %S of type %T\n", sf, pa);
325 326 327
	return;

bad:
328
	yyerror("invalid receiver type %T", pa);
329 330
}

Ken Thompson's avatar
init  
Ken Thompson committed
331 332 333 334
/*
 * a function named init is a special case.
 * it is called by the initialization before
 * main is run. to make it unique within a
Russ Cox's avatar
Russ Cox committed
335 336
 * package and also uncallable, the name,
 * normally "pkg.init", is altered to "pkg.init·filename".
Ken Thompson's avatar
init  
Ken Thompson committed
337 338 339 340 341 342 343 344 345 346 347
 */
Node*
renameinit(Node *n)
{
	Sym *s;

	s = n->sym;
	if(s == S)
		return n;
	if(strcmp(s->name, "init") != 0)
		return n;
Russ Cox's avatar
Russ Cox committed
348 349

	snprint(namebuf, sizeof(namebuf), "init·%s", filename);
Ken Thompson's avatar
init  
Ken Thompson committed
350 351 352 353
	s = lookup(namebuf);
	return newname(s);
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/*
 * declare the function proper.
 * and declare the arguments
 * called in extern-declaration context
 * returns in auto-declaration context.
 */
void
funchdr(Node *n)
{
	Node *on;
	Sym *s;

	s = n->nname->sym;
	on = s->oname;

	// check for same types
	if(on != N) {
		if(eqtype(n->type, on->type, 0)) {
372 373 374 375
			if(!eqargs(n->type, on->type)) {
				yyerror("function arg names changed: %S", s);
				print("\t%T\n\t%T\n", on->type, n->type);
			}
376
		} else {
377 378
			yyerror("function redeclared: %S", s);
			print("\t%T\n\t%T\n", on->type, n->type);
379 380 381 382
			on = N;
		}
	}

383
	// check for forward declaration
384 385 386 387 388
	if(on == N) {
		// initial declaration or redeclaration
		// declare fun name, argument types and argument names
		n->nname->type = n->type;
		if(n->type->thistuple == 0)
Russ Cox's avatar
Russ Cox committed
389
			addvar(n->nname, n->type, PFUNC);
390
		else
Russ Cox's avatar
Russ Cox committed
391
			n->nname->class = PFUNC;
392 393 394 395 396 397 398
	} else {
		// identical redeclaration
		// steal previous names
		n->nname = on;
		n->type = on->type;
		n->class = on->class;
		n->sym = s;
Ken Thompson's avatar
Ken Thompson committed
399
		if(dflag())
400 401 402 403 404 405 406
			print("forew  var-dcl %S %T\n", n->sym, n->type);
	}

	// change the declaration context from extern to auto
	autodcl = dcl();
	autodcl->back = autodcl;

Russ Cox's avatar
Russ Cox committed
407
	if(funcdepth == 0 && dclcontext != PEXTERN)
408 409 410
		fatal("funchdr: dclcontext");

	dclcontext = PAUTO;
Ken Thompson's avatar
Ken Thompson committed
411
	markdcl();
412 413 414 415
	funcargs(n->type);
}

void
Ken Thompson's avatar
Ken Thompson committed
416
funcargs(Type *ft)
417
{
Ken Thompson's avatar
Ken Thompson committed
418
	Type *t;
419
	Iter save;
Ken Thompson's avatar
Ken Thompson committed
420
	int all;
421

Russ Cox's avatar
Russ Cox committed
422 423
	funcdepth++;

Ken Thompson's avatar
Ken Thompson committed
424
	// declare the this/in arguments
Ken Thompson's avatar
Ken Thompson committed
425 426
	t = funcfirst(&save, ft);
	while(t != T) {
Ken Thompson's avatar
div bug  
Ken Thompson committed
427 428
		if(t->nname != N) {
			t->nname->xoffset = t->width;
Ken Thompson's avatar
Ken Thompson committed
429
			addvar(t->nname, t->type, PPARAM);
Ken Thompson's avatar
div bug  
Ken Thompson committed
430
		}
Ken Thompson's avatar
Ken Thompson committed
431
		t = funcnext(&save);
432 433 434
	}

	// declare the outgoing arguments
Ken Thompson's avatar
Ken Thompson committed
435
	all = 0;
Ken Thompson's avatar
Ken Thompson committed
436 437
	t = structfirst(&save, getoutarg(ft));
	while(t != T) {
Ken Thompson's avatar
div bug  
Ken Thompson committed
438 439
		if(t->nname != N)
			t->nname->xoffset = t->width;
440
		if(t->nname != N) {
441
			addvar(t->nname, t->type, PPARAMOUT);
Ken Thompson's avatar
Ken Thompson committed
442 443 444
			all |= 1;
		} else
			all |= 2;
Ken Thompson's avatar
Ken Thompson committed
445
		t = structnext(&save);
Ken Thompson's avatar
Ken Thompson committed
446
	}
447 448

	// this test is remarkedly similar to checkarglist
Ken Thompson's avatar
Ken Thompson committed
449
	if(all == 3)
450
		yyerror("cannot mix anonymous and named output arguments");
Ken Thompson's avatar
Ken Thompson committed
451

Ken Thompson's avatar
Ken Thompson committed
452
	ft->outnamed = 0;
Ken Thompson's avatar
Ken Thompson committed
453
	if(all == 1)
Ken Thompson's avatar
Ken Thompson committed
454
		ft->outnamed = 1;
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}

/*
 * compile the function.
 * called in auto-declaration context.
 * returns in extern-declaration context.
 */
void
funcbody(Node *n)
{

	compile(n);

	// change the declaration context from auto to extern
	if(dclcontext != PAUTO)
		fatal("funcbody: dclcontext");
Ken Thompson's avatar
Ken Thompson committed
471
	popdcl();
Russ Cox's avatar
Russ Cox committed
472 473 474 475 476 477 478 479 480 481 482 483
	funcdepth--;
	if(funcdepth == 0)
		dclcontext = PEXTERN;
}

void
funclit0(Type *t)
{
	Node *n;

	n = nod(OXXX, N, N);
	n->outer = funclit;
Russ Cox's avatar
Russ Cox committed
484
	n->dcl = autodcl;
Russ Cox's avatar
Russ Cox committed
485 486
	funclit = n;

Russ Cox's avatar
Russ Cox committed
487 488 489 490
	// new declaration context
	autodcl = dcl();
	autodcl->back = autodcl;

Russ Cox's avatar
Russ Cox committed
491
	funcargs(t);
492 493
}

Russ Cox's avatar
Russ Cox committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
Node*
funclit1(Type *type, Node *body)
{
	Node *func;
	Node *a, *d, *f, *n, *args, *clos, *in, *out;
	Type *ft, *t;
	Iter save;
	int narg, shift;

	popdcl();
	func = funclit;
	funclit = func->outer;

	// build up type of func f that we're going to compile.
	// as we referred to variables from the outer function,
	// we accumulated a list of PHEAP names in func.
	//
	narg = 0;
	if(func->cvars == N)
		ft = type;
	else {
		// add PHEAP versions as function arguments.
		in = N;
		for(a=listfirst(&save, &func->cvars); a; a=listnext(&save)) {
			d = nod(ODCLFIELD, a, N);
			d->type = ptrto(a->type);
			in = list(in, d);

			// while we're here, set up a->heapaddr for back end
			n = nod(ONAME, N, N);
			snprint(namebuf, sizeof namebuf, "&%s", a->sym->name);
			n->sym = lookup(namebuf);
			n->type = ptrto(a->type);
			n->class = PPARAM;
			n->xoffset = narg*types[tptr]->width;
			n->addable = 1;
			n->ullman = 1;
			narg++;
			a->heapaddr = n;

			a->xoffset = 0;

			// unlink from actual ONAME in symbol table
			a->closure->closure = a->outer;
		}

		// add a dummy arg for the closure's caller pc
		d = nod(ODCLFIELD, a, N);
		d->type = types[TUINTPTR];
		in = list(in, d);

		// slide param offset to make room for ptrs above.
		// narg+1 to skip over caller pc.
		shift = (narg+1)*types[tptr]->width;

		// now the original arguments.
		for(t=structfirst(&save, getinarg(type)); t; t=structnext(&save)) {
			d = nod(ODCLFIELD, t->nname, N);
			d->type = t->type;
			in = list(in, d);

			a = t->nname;
			if(a != N) {
				if(a->stackparam != N)
					a = a->stackparam;
				a->xoffset += shift;
			}
		}
		in = rev(in);

		// out arguments
		out = N;
		for(t=structfirst(&save, getoutarg(type)); t; t=structnext(&save)) {
			d = nod(ODCLFIELD, t->nname, N);
			d->type = t->type;
			out = list(out, d);

			a = t->nname;
			if(a != N) {
				if(a->stackparam != N)
					a = a->stackparam;
				a->xoffset += shift;
			}
		}
		out = rev(out);

		ft = functype(N, in, out);
581
		ft->outnamed = type->outnamed;
Russ Cox's avatar
Russ Cox committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
	}

	// declare function.
	vargen++;
	snprint(namebuf, sizeof(namebuf), "_f%.3ld", vargen);
	f = newname(lookup(namebuf));
	addvar(f, ft, PFUNC);
	f->funcdepth = 0;

	// compile function
	n = nod(ODCLFUNC, N, N);
	n->nname = f;
	n->type = ft;
	if(body == N)
		body = nod(ORETURN, N, N);
	n->nbody = body;
	compile(n);
	funcdepth--;
Russ Cox's avatar
Russ Cox committed
600
	autodcl = func->dcl;
Russ Cox's avatar
Russ Cox committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

	// if there's no closure, we can use f directly
	if(func->cvars == N)
		return f;

	// build up type for this instance of the closure func.
	in = N;
	d = nod(ODCLFIELD, N, N);	// siz
	d->type = types[TINT];
	in = list(in, d);
	d = nod(ODCLFIELD, N, N);	// f
	d->type = ft;
	in = list(in, d);
	for(a=listfirst(&save, &func->cvars); a; a=listnext(&save)) {
		d = nod(ODCLFIELD, N, N);	// arg
		d->type = ptrto(a->type);
		in = list(in, d);
	}
	in = rev(in);

	d = nod(ODCLFIELD, N, N);
	d->type = type;
	out = d;

	clos = syslook("closure", 1);
	clos->type = functype(N, in, out);

	// literal expression is sys.closure(siz, f, arg0, arg1, ...)
	// which builds a function that calls f after filling in arg0,
	// arg1, ... for the PHEAP arguments above.
	args = N;
	if(narg*8 > 100)
		yyerror("closure needs too many variables; runtime will reject it");
	a = nodintconst(narg*8);
	args = list(args, a);	// siz
	args = list(args, f);	// f
	for(a=listfirst(&save, &func->cvars); a; a=listnext(&save)) {
		d = oldname(a->sym);
		addrescapes(d);
		args = list(args, nod(OADDR, d, N));
	}
	args = rev(args);

	return nod(OCALL, clos, args);
}



649 650 651 652
/*
 * turn a parsed struct into a type
 */
Type**
Russ Cox's avatar
bug134  
Russ Cox committed
653
stotype(Node *n, int et, Type **t)
654
{
655
	Type *f, *t1;
656
	Iter save;
Russ Cox's avatar
Russ Cox committed
657
	String *note;
Russ Cox's avatar
bug134  
Russ Cox committed
658
	int lno;
659

Russ Cox's avatar
bug134  
Russ Cox committed
660
	lno = lineno;
661 662 663
	n = listfirst(&save, &n);

loop:
Russ Cox's avatar
Russ Cox committed
664
	note = nil;
665 666
	if(n == N) {
		*t = T;
Russ Cox's avatar
bug134  
Russ Cox committed
667
		lineno = lno;
668 669 670
		return t;
	}

Russ Cox's avatar
bug134  
Russ Cox committed
671
	lineno = n->lineno;
672 673
	if(n->op == OLIST) {
		// recursive because it can be lists of lists
Russ Cox's avatar
bug134  
Russ Cox committed
674
		t = stotype(n, et, t);
675 676 677
		goto next;
	}

678
	if(n->op != ODCLFIELD)
679 680
		fatal("stotype: oops %N\n", n);

681 682 683 684 685
	if(n->type == T) {
		// assume error already printed
		goto next;
	}

Russ Cox's avatar
Russ Cox committed
686 687
	switch(n->val.ctype) {
	case CTSTR:
688 689
		if(et != TSTRUCT)
			yyerror("interface method cannot have annotation");
Russ Cox's avatar
Russ Cox committed
690 691 692
		note = n->val.u.sval;
		break;
	default:
693 694 695 696
		if(et != TSTRUCT)
			yyerror("interface method cannot have annotation");
		else
			yyerror("field annotation must be string");
Russ Cox's avatar
Russ Cox committed
697 698 699 700 701
	case CTxxx:
		note = nil;
		break;
	}

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
	if(et == TINTER && n->left == N) {
		// embedded interface - inline the methods
		if(n->type->etype != TINTER) {
			yyerror("interface contains embedded non-interface %T", t);
			goto next;
		}
		for(t1=n->type->type; t1!=T; t1=t1->down) {
			if(strcmp(t1->sym->package, package) != 0)
				yyerror("embedded interface contains unexported method %S", t1->sym);
			f = typ(TFIELD);
			f->type = t1->type;
			f->width = BADWIDTH;
			f->nname = newname(t1->sym);
			f->sym = t1->sym;
			*t = f;
			t = &f->down;
		}
		goto next;
	}

722 723
	f = typ(TFIELD);
	f->type = n->type;
Russ Cox's avatar
Russ Cox committed
724
	f->note = note;
Russ Cox's avatar
Russ Cox committed
725
	f->width = BADWIDTH;
726 727 728

	if(n->left != N && n->left->op == ONAME) {
		f->nname = n->left;
Ken Thompson's avatar
Ken Thompson committed
729
		f->embedded = n->embedded;
730
		f->sym = f->nname->sym;
731
		if(pkgimportname != S && !exportname(f->sym->name))
Russ Cox's avatar
bug133  
Russ Cox committed
732
			f->sym = pkglookup(f->sym->name, pkgcontext);
733 734 735 736 737 738 739 740 741 742 743 744 745 746
	}

	*t = f;
	t = &f->down;

next:
	n = listnext(&save);
	goto loop;
}

Type*
dostruct(Node *n, int et)
{
	Type *t;
Russ Cox's avatar
Russ Cox committed
747
	int funarg;
748 749 750 751 752 753

	/*
	 * convert a parsed id/type list into
	 * a type for struct/interface/arglist
	 */

Russ Cox's avatar
Russ Cox committed
754 755 756 757 758
	funarg = 0;
	if(et == TFUNC) {
		funarg = 1;
		et = TSTRUCT;
	}
759
	t = typ(et);
Russ Cox's avatar
Russ Cox committed
760
	t->funarg = funarg;
Russ Cox's avatar
bug134  
Russ Cox committed
761
	stotype(n, et, &t->type);
Russ Cox's avatar
Russ Cox committed
762 763
	if(!funarg)
		checkwidth(t);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
	return t;
}

Type*
sortinter(Type *t)
{
	return t;
}

void
dcopy(Sym *a, Sym *b)
{
	a->name = b->name;
	a->oname = b->oname;
	a->otype = b->otype;
	a->oconst = b->oconst;
	a->package = b->package;
	a->opackage = b->opackage;
	a->lexical = b->lexical;
	a->undef = b->undef;
	a->vargen = b->vargen;
Ken Thompson's avatar
bug126  
Ken Thompson committed
785 786
	a->block = b->block;
	a->lastlineno = b->lastlineno;
787
	a->offset = b->offset;
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
}

Sym*
push(void)
{
	Sym *d;

	d = mal(sizeof(*d));
	d->link = dclstack;
	dclstack = d;
	return d;
}

Sym*
pushdcl(Sym *s)
{
	Sym *d;

	d = push();
	dcopy(d, s);
	return d;
}

void
Ken Thompson's avatar
Ken Thompson committed
812
popdcl(void)
813 814 815
{
	Sym *d, *s;

Ken Thompson's avatar
Ken Thompson committed
816
//	if(dflag())
817
//		print("revert\n");
818

819 820 821 822 823
	for(d=dclstack; d!=S; d=d->link) {
		if(d->name == nil)
			break;
		s = pkglookup(d->name, d->package);
		dcopy(s, d);
Ken Thompson's avatar
Ken Thompson committed
824
		if(dflag())
825
			print("\t%L pop %S\n", lineno, s);
826 827 828 829
	}
	if(d == S)
		fatal("popdcl: no mark");
	dclstack = d->link;
Ken Thompson's avatar
bug126  
Ken Thompson committed
830
	block = d->block;
831 832 833 834 835 836 837 838 839 840 841 842
}

void
poptodcl(void)
{
	Sym *d, *s;

	for(d=dclstack; d!=S; d=d->link) {
		if(d->name == nil)
			break;
		s = pkglookup(d->name, d->package);
		dcopy(s, d);
Ken Thompson's avatar
Ken Thompson committed
843
		if(dflag())
844
			print("\t%L pop %S\n", lineno, s);
845 846 847
	}
	if(d == S)
		fatal("poptodcl: no mark");
Russ Cox's avatar
Russ Cox committed
848
	dclstack = d;
849 850 851
}

void
Ken Thompson's avatar
Ken Thompson committed
852
markdcl(void)
853 854 855 856 857
{
	Sym *d;

	d = push();
	d->name = nil;		// used as a mark in fifo
Ken Thompson's avatar
bug126  
Ken Thompson committed
858
	d->block = block;
859 860 861

	blockgen++;
	block = blockgen;
Ken Thompson's avatar
Ken Thompson committed
862

Ken Thompson's avatar
Ken Thompson committed
863
//	if(dflag())
864 865 866
//		print("markdcl\n");
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
void
dumpdcl(char *st)
{
	Sym *s, *d;
	int i;

	print("\ndumpdcl: %s %p\n", st, b0stack);

	i = 0;
	for(d=dclstack; d!=S; d=d->link) {
		i++;
		print("    %.2d %p", i, d);
		if(d == b0stack)
			print(" (b0)");
		if(d->name == nil) {
			print("\n");
			continue;
		}
		print(" '%s'", d->name);
		s = pkglookup(d->name, d->package);
		print(" %lS\n", s);
	}
}

891 892 893 894 895 896 897 898 899 900 901 902 903
void
testdclstack(void)
{
	Sym *d;

	for(d=dclstack; d!=S; d=d->link) {
		if(d->name == nil) {
			yyerror("mark left on the stack");
			continue;
		}
	}
}

Ken Thompson's avatar
bug126  
Ken Thompson committed
904 905 906
static void
redeclare(char *str, Sym *s)
{
Ken Thompson's avatar
Ken Thompson committed
907 908 909
	if(s->block == block) {
		yyerror("%s %S redeclared in this block", str, s);
		print("	previous declaration at %L\n", s->lastlineno);
Ken Thompson's avatar
bug126  
Ken Thompson committed
910
	}
Ken Thompson's avatar
Ken Thompson committed
911 912
	s->block = block;
	s->lastlineno = lineno;
Ken Thompson's avatar
bug126  
Ken Thompson committed
913 914
}

915 916 917 918 919 920 921 922 923 924 925 926
void
addvar(Node *n, Type *t, int ctxt)
{
	Dcl *r, *d;
	Sym *s;
	int gen;

	if(n==N || n->sym == S || n->op != ONAME || t == T)
		fatal("addvar: n=%N t=%T nil", n, t);

	s = n->sym;

Russ Cox's avatar
Russ Cox committed
927
	if(ctxt == PEXTERN || ctxt == PFUNC) {
928 929 930 931 932 933
		r = externdcl;
		gen = 0;
	} else {
		r = autodcl;
		vargen++;
		gen = vargen;
934
		pushdcl(s);
935
	}
936

Ken Thompson's avatar
bug126  
Ken Thompson committed
937
	redeclare("variable", s);
938 939 940
	s->vargen = gen;
	s->oname = n;
	s->offset = 0;
Russ Cox's avatar
Russ Cox committed
941
	s->lexical = LNAME;
942

Russ Cox's avatar
Russ Cox committed
943
	n->funcdepth = funcdepth;
944 945 946 947 948 949 950 951 952 953 954 955
	n->type = t;
	n->vargen = gen;
	n->class = ctxt;

	d = dcl();
	d->dsym = s;
	d->dnode = n;
	d->op = ONAME;

	r->back->forw = d;
	r->back = d;

Ken Thompson's avatar
Ken Thompson committed
956
	if(dflag()) {
957 958
		if(ctxt == PEXTERN)
			print("extern var-dcl %S G%ld %T\n", s, s->vargen, t);
Russ Cox's avatar
Russ Cox committed
959 960
		else if(ctxt == PFUNC)
			print("extern func-dcl %S G%ld %T\n", s, s->vargen, t);
961 962 963 964 965 966
		else
			print("auto   var-dcl %S G%ld %T\n", s, s->vargen, t);
	}
}

void
967
addtyp(Type *n, int ctxt)
968 969 970
{
	Dcl *r, *d;
	Sym *s;
Russ Cox's avatar
6g:  
Russ Cox committed
971
	static int typgen;
972

973 974
	if(n==T || n->sym == S)
		fatal("addtyp: n=%T t=%T nil", n);
975 976 977

	s = n->sym;

978
	if(ctxt == PEXTERN)
979
		r = externdcl;
980 981 982
	else {
		r = autodcl;
		pushdcl(s);
Russ Cox's avatar
Russ Cox committed
983
		n->vargen = ++typgen;
984 985
	}

Ken Thompson's avatar
bug126  
Ken Thompson committed
986
	redeclare("type", s);
987
	s->otype = n;
988 989 990 991
	s->lexical = LATYPE;

	d = dcl();
	d->dsym = s;
992
	d->dtype = n;
993 994
	d->op = OTYPE;

Russ Cox's avatar
6g:  
Russ Cox committed
995 996 997 998 999 1000 1001 1002 1003 1004
	d->back = r->back;
	r->back->forw = d;
	r->back = d;

	d = dcl();
	d->dtype = n;
	d->op = OTYPE;

	r = typelist;
	d->back = r->back;
1005 1006 1007
	r->back->forw = d;
	r->back = d;

Ken Thompson's avatar
Ken Thompson committed
1008
	if(dflag()) {
1009
		if(ctxt == PEXTERN)
1010
			print("extern typ-dcl %S G%ld %T\n", s, s->vargen, n);
1011
		else
1012
			print("auto   typ-dcl %S G%ld %T\n", s, s->vargen, n);
1013 1014 1015
	}
}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
void
addconst(Node *n, Node *e, int ctxt)
{
	Sym *s;
	Dcl *r, *d;

	if(n->op != ONAME)
		fatal("addconst: not a name");

	if(e->op != OLITERAL) {
		yyerror("expression must be a constant");
		return;
	}

	s = n->sym;

	if(ctxt == PEXTERN)
		r = externdcl;
	else {
		r = autodcl;
		pushdcl(s);
	}

Ken Thompson's avatar
bug126  
Ken Thompson committed
1039
	redeclare("constant", s);
1040 1041 1042 1043 1044 1045 1046
	s->oconst = e;
	s->lexical = LACONST;

	d = dcl();
	d->dsym = s;
	d->dnode = e;
	d->op = OCONST;
Russ Cox's avatar
6g:  
Russ Cox committed
1047
	d->back = r->back;
1048 1049 1050 1051 1052 1053 1054
	r->back->forw = d;
	r->back = d;

	if(dflag())
		print("const-dcl %S %N\n", n->sym, n->sym->oconst);
}

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
Node*
fakethis(void)
{
	Node *n;
	Type *t;

	n = nod(ODCLFIELD, N, N);
	t = dostruct(N, TSTRUCT);
	t = ptrto(t);
	n->type = t;
	return n;
}

/*
 * this generates a new name that is
 * pushed down on the declaration list.
 * no diagnostics are produced as this
 * name will soon be declared.
 */
Node*
newname(Sym *s)
{
	Node *n;

	n = nod(ONAME, N, N);
	n->sym = s;
	n->type = T;
	n->addable = 1;
1083
	n->ullman = 1;
Russ Cox's avatar
Russ Cox committed
1084
	n->xoffset = 0;
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
	return n;
}

/*
 * this will return an old name
 * that has already been pushed on the
 * declaration list. a diagnostic is
 * generated if no name has been defined.
 */
Node*
oldname(Sym *s)
{
	Node *n;
Russ Cox's avatar
Russ Cox committed
1098
	Node *c;
1099 1100 1101

	n = s->oname;
	if(n == N) {
1102 1103 1104 1105
		n = nod(ONONAME, N, N);
		n->sym = s;
		n->type = T;
		n->addable = 1;
1106
		n->ullman = 1;
1107
	}
Russ Cox's avatar
Russ Cox committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	if(n->funcdepth > 0 && n->funcdepth != funcdepth) {
		// inner func is referring to var
		// in outer func.
		if(n->closure == N || n->closure->funcdepth != funcdepth) {
			// create new closure var.
			c = nod(ONAME, N, N);
			c->sym = s;
			c->class = PPARAMREF;
			c->type = n->type;
			c->addable = 0;
			c->ullman = 2;
			c->funcdepth = funcdepth;
			c->outer = n->closure;
			n->closure = c;
			c->closure = n;
			funclit->cvars = list(c, funclit->cvars);
		}
		// return ref to closure var, not original
		return n->closure;
	}
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	return n;
}

/*
 * same for types
 */
Type*
newtype(Sym *s)
{
	Type *t;

	t = typ(TFORW);
	t->sym = s;
	t->type = T;
	return t;
}

Type*
oldtype(Sym *s)
{
	Type *t;

	t = s->otype;
	if(t == T)
		fatal("%S not a type", s); // cant happen
	return t;
}

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
/*
 * n is a node with a name (or a reversed list of them).
 * make it an anonymous declaration of that name's type.
 */
Node*
nametoanondcl(Node *na)
{
	Node **l, *n;
	Type *t;

	for(l=&na; (n=*l)->op == OLIST; l=&n->left)
		n->right = nametoanondcl(n->right);

	if(n->sym->lexical != LATYPE && n->sym->lexical != LBASETYPE) {
		yyerror("%s is not a type", n->sym->name);
		t = typ(TINT32);
	} else
		t = oldtype(n->sym);
	n = nod(ODCLFIELD, N, N);
	n->type = t;
	*l = n;
	return na;
}

/*
 * n is a node with a name (or a reversed list of them).
 * make it a declaration of the given type.
 */
Node*
nametodcl(Node *na, Type *t)
{
	Node **l, *n;

	for(l=&na; (n=*l)->op == OLIST; l=&n->left)
		n->right = nametodcl(n->right, t);

	n = nod(ODCLFIELD, n, N);
	n->type = t;
	*l = n;
	return na;
}

/*
 * make an anonymous declaration for t
 */
Node*
anondcl(Type *t)
{
	Node *n;

	n = nod(ODCLFIELD, N, N);
	n->type = t;
	return n;
}

/*
 * check that the list of declarations is either all anonymous or all named
 */
void
checkarglist(Node *n)
{
	if(n->op != OLIST)
		return;
	if(n->left->op != ODCLFIELD)
		fatal("checkarglist");
	if(n->left->left != N) {
		for(n=n->right; n->op == OLIST; n=n->right)
			if(n->left->left == N)
				goto mixed;
		if(n->left == N)
			goto mixed;
	} else {
		for(n=n->right; n->op == OLIST; n=n->right)
			if(n->left->left != N)
				goto mixed;
		if(n->left != N)
			goto mixed;
	}
	return;

mixed:
	yyerror("cannot mix anonymous and named function arguments");
}

Ken Thompson's avatar
Ken Thompson committed
1240
// hand-craft the following initialization code
Russ Cox's avatar
Russ Cox committed
1241 1242 1243 1244
//	var initdone·<file> bool 			(1)
//	func	Init·<file>()				(2)
//		if initdone·<file> { return }		(3)
//		initdone.<file> = true;			(4)
Ken Thompson's avatar
init  
Ken Thompson committed
1245
//		// over all matching imported symbols
Russ Cox's avatar
Russ Cox committed
1246
//			<pkg>.init·<file>()		(5)
Ken Thompson's avatar
init  
Ken Thompson committed
1247
//		{ <init stmts> }			(6)
Russ Cox's avatar
Russ Cox committed
1248
//		init·<file>()	// if any		(7)
Ken Thompson's avatar
Ken Thompson committed
1249 1250 1251 1252 1253 1254
//		return					(8)
//	}

void
fninit(Node *n)
{
Russ Cox's avatar
Russ Cox committed
1255
	Node *done;
Ken Thompson's avatar
Ken Thompson committed
1256
	Node *a, *fn, *r;
1257
	uint32 h;
Ken Thompson's avatar
Ken Thompson committed
1258 1259
	Sym *s;

1260 1261 1262 1263 1264
	if(strcmp(package, "PACKAGE") == 0) {
		// sys.go or unsafe.go during compiler build
		return;
	}

Ken Thompson's avatar
Ken Thompson committed
1265 1266 1267
	r = N;

	// (1)
Russ Cox's avatar
Russ Cox committed
1268
	snprint(namebuf, sizeof(namebuf), "initdone·%s", filename);
Ken Thompson's avatar
Ken Thompson committed
1269 1270 1271 1272 1273 1274
	done = newname(lookup(namebuf));
	addvar(done, types[TBOOL], PEXTERN);

	// (2)

	maxarg = 0;
1275
	stksize = initstksize;
Ken Thompson's avatar
Ken Thompson committed
1276

Russ Cox's avatar
Russ Cox committed
1277
	snprint(namebuf, sizeof(namebuf), "Init·%s", filename);
Ken Thompson's avatar
Ken Thompson committed
1278 1279 1280

	// this is a botch since we need a known name to
	// call the top level init function out of rt0
Ken Thompson's avatar
Ken Thompson committed
1281
	if(strcmp(package, "main") == 0)
Russ Cox's avatar
Russ Cox committed
1282
		snprint(namebuf, sizeof(namebuf), "init");
Ken Thompson's avatar
Ken Thompson committed
1283

Ken Thompson's avatar
Ken Thompson committed
1284 1285 1286 1287
	fn = nod(ODCLFUNC, N, N);
	fn->nname = newname(lookup(namebuf));
	fn->type = functype(N, N, N);
	funchdr(fn);
Ken Thompson's avatar
Ken Thompson committed
1288 1289 1290 1291 1292 1293 1294 1295

	// (3)
	a = nod(OIF, N, N);
	a->ntest = done;
	a->nbody = nod(ORETURN, N, N);
	r = list(r, a);

	// (4)
1296
	a = nod(OAS, done, nodbool(1));
Ken Thompson's avatar
Ken Thompson committed
1297 1298 1299 1300 1301
	r = list(r, a);

	// (5)
	for(h=0; h<NHASH; h++)
	for(s = hash[h]; s != S; s = s->link) {
Russ Cox's avatar
Russ Cox committed
1302
		if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0)
Ken Thompson's avatar
Ken Thompson committed
1303 1304 1305 1306
			continue;
		if(s->oname == N)
			continue;

Ken Thompson's avatar
Ken Thompson committed
1307
		// could check that it is fn of no args/returns
Ken Thompson's avatar
Ken Thompson committed
1308 1309 1310 1311 1312 1313 1314 1315
		a = nod(OCALL, s->oname, N);
		r = list(r, a);
	}

	// (6)
	r = list(r, n);

	// (7)
Ken Thompson's avatar
Ken Thompson committed
1316
	// could check that it is fn of no args/returns
Russ Cox's avatar
Russ Cox committed
1317
	snprint(namebuf, sizeof(namebuf), "init·%s", filename);
Ken Thompson's avatar
init  
Ken Thompson committed
1318 1319 1320
	s = lookup(namebuf);
	if(s->oname != N) {
		a = nod(OCALL, s->oname, N);
Ken Thompson's avatar
Ken Thompson committed
1321 1322 1323 1324 1325 1326 1327
		r = list(r, a);
	}

	// (8)
	a = nod(ORETURN, N, N);
	r = list(r, a);

1328
	exportsym(fn->nname->sym);
Ken Thompson's avatar
Ken Thompson committed
1329

Ken Thompson's avatar
Ken Thompson committed
1330 1331 1332
	fn->nbody = rev(r);
//dump("b", fn);
//dump("r", fn->nbody);
Ken Thompson's avatar
Ken Thompson committed
1333 1334

	popdcl();
Ken Thompson's avatar
Ken Thompson committed
1335
	compile(fn);
Ken Thompson's avatar
Ken Thompson committed
1336
}
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368


/*
 * when a type's width should be known, we call checkwidth
 * to compute it.  during a declaration like
 *
 *	type T *struct { next T }
 *
 * it is necessary to defer the calculation of the struct width
 * until after T has been initialized to be a pointer to that struct.
 * similarly, during import processing structs may be used
 * before their definition.  in those situations, calling
 * defercheckwidth() stops width calculations until
 * resumecheckwidth() is called, at which point all the
 * checkwidths that were deferred are executed.
 * sometimes it is okay to
 */
typedef struct TypeList TypeList;
struct TypeList {
	Type *t;
	TypeList *next;
};

static TypeList *tlfree;
static TypeList *tlq;
static int defercalc;

void
checkwidth(Type *t)
{
	TypeList *l;

Russ Cox's avatar
Russ Cox committed
1369 1370 1371 1372 1373
	// function arg structs should not be checked
	// outside of the enclosing function.
	if(t->funarg)
		fatal("checkwidth %T", t);

1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
	if(!defercalc) {
		dowidth(t);
		return;
	}

	l = tlfree;
	if(l != nil)
		tlfree = l->next;
	else
		l = mal(sizeof *l);

	l->t = t;
	l->next = tlq;
	tlq = l;
}

void
defercheckwidth(void)
{
1393 1394 1395
	// we get out of sync on syntax errors, so don't be pedantic.
	// if(defercalc)
	//	fatal("defercheckwidth");
1396 1397 1398 1399 1400 1401
	defercalc = 1;
}

void
resumecheckwidth(void)
{
Russ Cox's avatar
Russ Cox committed
1402
	TypeList *l;
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

	if(!defercalc)
		fatal("restartcheckwidth");
	defercalc = 0;

	for(l = tlq; l != nil; l = tlq) {
		dowidth(l->t);
		tlq = l->next;
		l->next = tlfree;
		tlfree = l;
	}
}
Ken Thompson's avatar
Ken Thompson committed
1415 1416

Node*
Ken Thompson's avatar
Ken Thompson committed
1417
embedded(Sym *s)
Ken Thompson's avatar
Ken Thompson committed
1418 1419
{
	Node *n;
Russ Cox's avatar
Russ Cox committed
1420
	char *name;
1421

Russ Cox's avatar
Russ Cox committed
1422 1423 1424 1425 1426 1427 1428 1429 1430
	// Names sometimes have disambiguation junk
	// appended after a center dot.  Discard it when
	// making the name for the embedded struct field.
	enum { CenterDot = 0xB7 };
	name = s->name;
	if(utfrune(s->name, CenterDot)) {
		name = strdup(s->name);
		*utfrune(name, CenterDot) = 0;
	}
Ken Thompson's avatar
Ken Thompson committed
1431

Russ Cox's avatar
Russ Cox committed
1432
	n = newname(lookup(name));
Ken Thompson's avatar
Ken Thompson committed
1433
	n = nod(ODCLFIELD, n, N);
Ken Thompson's avatar
Ken Thompson committed
1434
	n->embedded = 1;
Ken Thompson's avatar
Ken Thompson committed
1435 1436 1437 1438
	if(s == S)
		return n;
	n->type = oldtype(s);
	if(isptr[n->type->etype])
Ken Thompson's avatar
Ken Thompson committed
1439
		yyerror("embedded type cannot be a pointer");
Ken Thompson's avatar
Ken Thompson committed
1440 1441
	return n;
}
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

/*
 * declare variables from grammar
 * new_name_list [type] = expr_list
 */
Node*
variter(Node *vv, Type *t, Node *ee)
{
	Iter viter, eiter;
	Node *v, *e, *r, *a;

	vv = rev(vv);
	ee = rev(ee);

	v = listfirst(&viter, &vv);
	e = listfirst(&eiter, &ee);
	r = N;

loop:
	if(v == N && e == N)
		return rev(r);
1463

1464 1465 1466 1467 1468 1469 1470 1471
	if(v == N || e == N) {
		yyerror("shape error in var dcl");
		return rev(r);
	}

	a = nod(OAS, v, N);
	if(t == T) {
		gettype(e, a);
1472
		defaultlit(e, T);
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
		dodclvar(v, e->type);
	} else
		dodclvar(v, t);
	a->right = e;

	r = list(r, a);

	v = listnext(&viter);
	e = listnext(&eiter);
	goto loop;
}

/*
 * declare constants from grammar
1487
 * new_name_list [[type] = expr_list]
1488 1489 1490 1491 1492
 */
void
constiter(Node *vv, Type *t, Node *cc)
{
	Iter viter, citer;
Ken Thompson's avatar
Ken Thompson committed
1493
	Node *v, *c;
1494

1495 1496 1497
	if(cc == N) {
		if(t != T)
			yyerror("constdcl cannot have type without expr");
1498
		cc = lastconst;
1499 1500
		t = lasttype;
	}
1501
	lastconst = cc;
1502
	lasttype = t;
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
	vv = rev(vv);
	cc = rev(treecopy(cc));

	v = listfirst(&viter, &vv);
	c = listfirst(&citer, &cc);

loop:
	if(v == N && c == N) {
		iota += 1;
		return;
	}
1514

1515
	if(v == N || c == N) {
1516
		yyerror("shape error in const dcl");
1517 1518 1519 1520 1521 1522 1523
		iota += 1;
		return;
	}

	gettype(c, N);
	if(t != T)
		convlit(c, t);
1524 1525
	if(t == T)
		lasttype = c->type;
1526 1527 1528 1529 1530 1531
	dodclconst(v, c);

	v = listnext(&viter);
	c = listnext(&citer);
	goto loop;
}
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543

/*
 * look for
 *	unsafe.Sizeof
 *	unsafe.Offsetof
 * rewrite with a constant
 */
Node*
unsafenmagic(Node *l, Node *r)
{
	Node *n;
	Sym *s;
Ken Thompson's avatar
Ken Thompson committed
1544
	Type *t, *tr;
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	long v;
	Val val;

	if(l == N || r == N)
		goto no;
	if(l->op != ONAME)
		goto no;
	s = l->sym;
	if(s == S)
		goto no;
	if(strcmp(s->opackage, "unsafe") != 0)
		goto no;

	if(strcmp(s->name, "Sizeof") == 0) {
		walktype(r, Erv);
Ken Thompson's avatar
Ken Thompson committed
1560 1561 1562 1563
		tr = r->type;
		if(r->op == OLITERAL && r->val.ctype == CTSTR)
			tr = types[TSTRING];
		if(tr == T)
1564
			goto no;
Ken Thompson's avatar
Ken Thompson committed
1565
		v = tr->width;
1566 1567 1568 1569 1570 1571
		goto yes;
	}
	if(strcmp(s->name, "Offsetof") == 0) {
		if(r->op != ODOT && r->op != ODOTPTR)
			goto no;
		walktype(r, Erv);
Ian Lance Taylor's avatar
Ian Lance Taylor committed
1572 1573 1574 1575 1576
		v = r->xoffset;
		goto yes;
	}
	if(strcmp(s->name, "Alignof") == 0) {
		walktype(r, Erv);
Ken Thompson's avatar
Ken Thompson committed
1577 1578 1579 1580
		tr = r->type;
		if(r->op == OLITERAL && r->val.ctype == CTSTR)
			tr = types[TSTRING];
		if(tr == T)
Ian Lance Taylor's avatar
Ian Lance Taylor committed
1581
			goto no;
Ken Thompson's avatar
Ken Thompson committed
1582

Ian Lance Taylor's avatar
Ian Lance Taylor committed
1583 1584 1585 1586 1587
		// make struct { byte; T; }
		t = typ(TSTRUCT);
		t->type = typ(TFIELD);
		t->type->type = types[TUINT8];
		t->type->down = typ(TFIELD);
Ken Thompson's avatar
Ken Thompson committed
1588
		t->type->down->type = tr;
Ian Lance Taylor's avatar
Ian Lance Taylor committed
1589 1590
		// compute struct widths
		dowidth(t);
Ken Thompson's avatar
Ken Thompson committed
1591

Ian Lance Taylor's avatar
Ian Lance Taylor committed
1592 1593
		// the offset of T is its required alignment
		v = t->type->down->width;
1594 1595 1596 1597 1598 1599 1600
		goto yes;
	}

no:
	return N;

yes:
Russ Cox's avatar
Russ Cox committed
1601
	addtop = N;	// any side effects disappear
1602 1603 1604 1605 1606
	val.ctype = CTINT;
	val.u.xval = mal(sizeof(*n->val.u.xval));
	mpmovecfix(val.u.xval, v);
	n = nod(OLITERAL, N, N);
	n->val = val;
1607
	n->type = types[TINT];
1608 1609
	return n;
}