dcl.c 20.6 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
	for(; n->op == OLIST; n = n->right)
27 28
		dodclvar(n->left, t);

29
	dowidth(t);
30 31 32 33 34 35 36

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

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

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

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

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

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

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

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

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

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

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

97 98 99
	switch(n->etype) {
	case TFORW:
		break;
100

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

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

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

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

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

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
/*
 * 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
164 165 166
	t->type = dostruct(this, TFUNC);
	t->type->down = dostruct(out, TFUNC);
	t->type->down->down = dostruct(in, TFUNC);
167 168 169 170 171

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

172
	checkwidth(t);
173 174 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
	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
203
Sym*
Russ Cox's avatar
Russ Cox committed
204
methodsym(Sym *nsym, Type *t0)
205
{
206
	Sym *s;
Russ Cox's avatar
Russ Cox committed
207
	char buf[NSYMB];
Russ Cox's avatar
Russ Cox committed
208
	Type *t;
209

Russ Cox's avatar
Russ Cox committed
210
	t = t0;
Ken Thompson's avatar
Ken Thompson committed
211 212
	if(t == T)
		goto bad;
213
	s = t->sym;
Russ Cox's avatar
Russ Cox committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	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
229

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

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

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

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

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

260 261 262
	pa = nil;
	sf = nil;

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

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

283
	f = dclmethod(pa);
284 285
	if(f == T)
		goto bad;
286

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

292 293
	pa = f;

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

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

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

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

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

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

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

Ken Thompson's avatar
init  
Ken Thompson committed
329 330 331 332
/*
 * 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
333 334
 * package and also uncallable, the name,
 * normally "pkg.init", is altered to "pkg.init·filename".
Ken Thompson's avatar
init  
Ken Thompson committed
335 336 337 338 339 340 341 342 343 344 345
 */
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
346 347

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

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
 * 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)) {
370 371 372 373
			if(!eqargs(n->type, on->type)) {
				yyerror("function arg names changed: %S", s);
				print("\t%T\n\t%T\n", on->type, n->type);
			}
374
		} else {
375 376
			yyerror("function redeclared: %S", s);
			print("\t%T\n\t%T\n", on->type, n->type);
377 378 379 380
			on = N;
		}
	}

381
	// check for forward declaration
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	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)
			addvar(n->nname, n->type, PEXTERN);
		else
			n->nname->class = PEXTERN;
	} 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
397
		if(dflag())
398 399 400 401 402 403 404 405 406 407 408
			print("forew  var-dcl %S %T\n", n->sym, n->type);
	}

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

	if(dclcontext != PEXTERN)
		fatal("funchdr: dclcontext");

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

}

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

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

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

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

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

/*
 * 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
468
	popdcl();
469 470 471 472 473 474 475
	dclcontext = PEXTERN;
}

/*
 * turn a parsed struct into a type
 */
Type**
Russ Cox's avatar
bug134  
Russ Cox committed
476
stotype(Node *n, int et, Type **t)
477 478 479
{
	Type *f;
	Iter save;
Russ Cox's avatar
Russ Cox committed
480
	String *note;
Russ Cox's avatar
bug134  
Russ Cox committed
481
	int lno;
482

Russ Cox's avatar
bug134  
Russ Cox committed
483
	lno = lineno;
484 485 486
	n = listfirst(&save, &n);

loop:
Russ Cox's avatar
Russ Cox committed
487
	note = nil;
488 489
	if(n == N) {
		*t = T;
Russ Cox's avatar
bug134  
Russ Cox committed
490
		lineno = lno;
491 492 493
		return t;
	}

Russ Cox's avatar
bug134  
Russ Cox committed
494
	lineno = n->lineno;
495 496
	if(n->op == OLIST) {
		// recursive because it can be lists of lists
Russ Cox's avatar
bug134  
Russ Cox committed
497
		t = stotype(n, et, t);
498 499 500 501 502 503
		goto next;
	}

	if(n->op != ODCLFIELD || n->type == T)
		fatal("stotype: oops %N\n", n);

Russ Cox's avatar
bug134  
Russ Cox committed
504 505 506
	if(et == TSTRUCT && n->type->etype == TFUNC)
		yyerror("bad structure field type: %T", n->type);

Russ Cox's avatar
Russ Cox committed
507 508 509 510 511
	switch(n->val.ctype) {
	case CTSTR:
		note = n->val.u.sval;
		break;
	default:
Russ Cox's avatar
bug134  
Russ Cox committed
512
		yyerror("field annotation must be string");
Russ Cox's avatar
Russ Cox committed
513 514 515 516 517
	case CTxxx:
		note = nil;
		break;
	}

518 519
	f = typ(TFIELD);
	f->type = n->type;
Russ Cox's avatar
Russ Cox committed
520
	f->note = note;
Russ Cox's avatar
Russ Cox committed
521
	f->width = BADWIDTH;
522 523 524

	if(n->left != N && n->left->op == ONAME) {
		f->nname = n->left;
Ken Thompson's avatar
Ken Thompson committed
525
		f->embedded = n->embedded;
526
		f->sym = f->nname->sym;
527
		if(pkgimportname != S && !exportname(f->sym->name))
Russ Cox's avatar
bug133  
Russ Cox committed
528
			f->sym = pkglookup(f->sym->name, pkgcontext);
529 530 531 532 533 534 535 536 537 538 539 540 541 542
	}

	*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
543
	int funarg;
544 545 546 547 548 549

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

Russ Cox's avatar
Russ Cox committed
550 551 552 553 554
	funarg = 0;
	if(et == TFUNC) {
		funarg = 1;
		et = TSTRUCT;
	}
555
	t = typ(et);
Russ Cox's avatar
Russ Cox committed
556
	t->funarg = funarg;
Russ Cox's avatar
bug134  
Russ Cox committed
557
	stotype(n, et, &t->type);
Russ Cox's avatar
Russ Cox committed
558 559
	if(!funarg)
		checkwidth(t);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	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
581 582
	a->block = b->block;
	a->lastlineno = b->lastlineno;
583
	a->offset = b->offset;
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
}

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
608
popdcl(void)
609 610 611
{
	Sym *d, *s;

Ken Thompson's avatar
Ken Thompson committed
612
//	if(dflag())
613
//		print("revert\n");
614

615 616 617 618 619
	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
620
		if(dflag())
621
			print("\t%L pop %S\n", lineno, s);
622 623 624 625
	}
	if(d == S)
		fatal("popdcl: no mark");
	dclstack = d->link;
Ken Thompson's avatar
bug126  
Ken Thompson committed
626
	block = d->block;
627 628 629 630 631 632 633 634 635 636 637 638
}

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
639
		if(dflag())
640
			print("\t%L pop %S\n", lineno, s);
641 642 643
	}
	if(d == S)
		fatal("poptodcl: no mark");
Russ Cox's avatar
Russ Cox committed
644
	dclstack = d;
645 646 647
}

void
Ken Thompson's avatar
Ken Thompson committed
648
markdcl(void)
649 650 651 652 653
{
	Sym *d;

	d = push();
	d->name = nil;		// used as a mark in fifo
Ken Thompson's avatar
bug126  
Ken Thompson committed
654
	d->block = block;
655 656 657

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

Ken Thompson's avatar
Ken Thompson committed
659
//	if(dflag())
660 661 662 663 664 665 666 667
//		print("markdcl\n");
}

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

Ken Thompson's avatar
Ken Thompson committed
668
	markdcl();
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

	// copy the entire pop of the stack
	// all the way back to block0.
	// after this the symbol table is at
	// block0 and popdcl will restore it.
	for(d=dclstack; d!=S; d=d->link) {
		if(d == b0stack)
			break;
		if(d->name != nil) {
			s = pkglookup(d->name, d->package);
			pushdcl(s);
			dcopy(s, d);
		}
	}
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
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);
	}
}

709 710 711 712 713 714 715 716 717 718 719 720 721
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
722 723 724
static void
redeclare(char *str, Sym *s)
{
Ken Thompson's avatar
Ken Thompson committed
725 726 727
	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
728
	}
Ken Thompson's avatar
Ken Thompson committed
729 730
	s->block = block;
	s->lastlineno = lineno;
Ken Thompson's avatar
bug126  
Ken Thompson committed
731 732
}

733 734 735 736 737 738 739 740 741 742 743 744
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;

745 746 747 748 749 750 751
	if(ctxt == PEXTERN) {
		r = externdcl;
		gen = 0;
	} else {
		r = autodcl;
		vargen++;
		gen = vargen;
752
		pushdcl(s);
753
	}
754

Ken Thompson's avatar
bug126  
Ken Thompson committed
755
	redeclare("variable", s);
756 757 758
	s->vargen = gen;
	s->oname = n;
	s->offset = 0;
Russ Cox's avatar
Russ Cox committed
759
	s->lexical = LNAME;
760 761 762 763 764 765 766 767 768 769 770 771 772

	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
773
	if(dflag()) {
774 775 776 777 778 779 780 781
		if(ctxt == PEXTERN)
			print("extern var-dcl %S G%ld %T\n", s, s->vargen, t);
		else
			print("auto   var-dcl %S G%ld %T\n", s, s->vargen, t);
	}
}

void
782
addtyp(Type *n, int ctxt)
783 784 785
{
	Dcl *r, *d;
	Sym *s;
Russ Cox's avatar
6g:  
Russ Cox committed
786
	static int typgen;
787

788 789
	if(n==T || n->sym == S)
		fatal("addtyp: n=%T t=%T nil", n);
790 791 792

	s = n->sym;

793
	if(ctxt == PEXTERN)
794
		r = externdcl;
795 796 797
	else {
		r = autodcl;
		pushdcl(s);
Russ Cox's avatar
Russ Cox committed
798
		n->vargen = ++typgen;
799 800
	}

Ken Thompson's avatar
bug126  
Ken Thompson committed
801
	redeclare("type", s);
802
	s->otype = n;
803 804 805 806
	s->lexical = LATYPE;

	d = dcl();
	d->dsym = s;
807
	d->dtype = n;
808 809
	d->op = OTYPE;

Russ Cox's avatar
6g:  
Russ Cox committed
810 811 812 813 814 815 816 817 818 819
	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;
820 821 822
	r->back->forw = d;
	r->back = d;

Ken Thompson's avatar
Ken Thompson committed
823
	if(dflag()) {
824
		if(ctxt == PEXTERN)
825
			print("extern typ-dcl %S G%ld %T\n", s, s->vargen, n);
826
		else
827
			print("auto   typ-dcl %S G%ld %T\n", s, s->vargen, n);
828 829 830
	}
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
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
854
	redeclare("constant", s);
855 856 857 858 859 860 861
	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
862
	d->back = r->back;
863 864 865 866 867 868 869
	r->back->forw = d;
	r->back = d;

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

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
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;
898
	n->ullman = 1;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	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;

	n = s->oname;
	if(n == N) {
915 916 917 918
		n = nod(ONONAME, N, N);
		n->sym = s;
		n->type = T;
		n->addable = 1;
919
		n->ullman = 1;
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
	}
	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;
}

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
/*
 * 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
1033
// hand-craft the following initialization code
Russ Cox's avatar
Russ Cox committed
1034 1035 1036 1037
//	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
1038
//		// over all matching imported symbols
Russ Cox's avatar
Russ Cox committed
1039
//			<pkg>.init·<file>()		(5)
Ken Thompson's avatar
init  
Ken Thompson committed
1040
//		{ <init stmts> }			(6)
Russ Cox's avatar
Russ Cox committed
1041
//		init·<file>()	// if any		(7)
Ken Thompson's avatar
Ken Thompson committed
1042 1043 1044 1045 1046 1047
//		return					(8)
//	}

void
fninit(Node *n)
{
Russ Cox's avatar
Russ Cox committed
1048
	Node *done;
Ken Thompson's avatar
Ken Thompson committed
1049
	Node *a, *fn, *r;
1050
	uint32 h;
Ken Thompson's avatar
Ken Thompson committed
1051 1052 1053 1054 1055
	Sym *s;

	r = N;

	// (1)
Russ Cox's avatar
Russ Cox committed
1056
	snprint(namebuf, sizeof(namebuf), "initdone·%s", filename);
Ken Thompson's avatar
Ken Thompson committed
1057 1058 1059 1060 1061 1062
	done = newname(lookup(namebuf));
	addvar(done, types[TBOOL], PEXTERN);

	// (2)

	maxarg = 0;
1063
	stksize = initstksize;
Ken Thompson's avatar
Ken Thompson committed
1064

Russ Cox's avatar
Russ Cox committed
1065
	snprint(namebuf, sizeof(namebuf), "Init·%s", filename);
Ken Thompson's avatar
Ken Thompson committed
1066 1067 1068

	// 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
1069
	if(strcmp(package, "main") == 0)
Russ Cox's avatar
Russ Cox committed
1070
		snprint(namebuf, sizeof(namebuf), "init");
Ken Thompson's avatar
Ken Thompson committed
1071

Ken Thompson's avatar
Ken Thompson committed
1072 1073 1074 1075
	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
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

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

	// (4)
	a = nod(OAS, done, booltrue);
	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
1090
		if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0)
Ken Thompson's avatar
Ken Thompson committed
1091 1092 1093 1094
			continue;
		if(s->oname == N)
			continue;

Ken Thompson's avatar
Ken Thompson committed
1095
		// could check that it is fn of no args/returns
Ken Thompson's avatar
Ken Thompson committed
1096 1097 1098 1099 1100 1101 1102 1103
		a = nod(OCALL, s->oname, N);
		r = list(r, a);
	}

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

	// (7)
Ken Thompson's avatar
Ken Thompson committed
1104
	// could check that it is fn of no args/returns
Russ Cox's avatar
Russ Cox committed
1105
	snprint(namebuf, sizeof(namebuf), "init·%s", filename);
Ken Thompson's avatar
init  
Ken Thompson committed
1106 1107 1108
	s = lookup(namebuf);
	if(s->oname != N) {
		a = nod(OCALL, s->oname, N);
Ken Thompson's avatar
Ken Thompson committed
1109 1110 1111 1112 1113 1114 1115
		r = list(r, a);
	}

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

1116
	exportsym(fn->nname->sym);
Ken Thompson's avatar
Ken Thompson committed
1117

Ken Thompson's avatar
Ken Thompson committed
1118 1119 1120
	fn->nbody = rev(r);
//dump("b", fn);
//dump("r", fn->nbody);
Ken Thompson's avatar
Ken Thompson committed
1121 1122

	popdcl();
Ken Thompson's avatar
Ken Thompson committed
1123
	compile(fn);
Ken Thompson's avatar
Ken Thompson committed
1124
}
1125 1126 1127 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 1156


/*
 * 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
1157 1158 1159 1160 1161
	// function arg structs should not be checked
	// outside of the enclosing function.
	if(t->funarg)
		fatal("checkwidth %T", t);

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	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)
{
1181 1182 1183
	// we get out of sync on syntax errors, so don't be pedantic.
	// if(defercalc)
	//	fatal("defercheckwidth");
1184 1185 1186 1187 1188 1189
	defercalc = 1;
}

void
resumecheckwidth(void)
{
Russ Cox's avatar
Russ Cox committed
1190
	TypeList *l;
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

	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
1203 1204

Node*
Ken Thompson's avatar
Ken Thompson committed
1205
embedded(Sym *s)
Ken Thompson's avatar
Ken Thompson committed
1206 1207
{
	Node *n;
Russ Cox's avatar
Russ Cox committed
1208
	char *name;
1209

Russ Cox's avatar
Russ Cox committed
1210 1211 1212 1213 1214 1215 1216 1217 1218
	// 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
1219

Russ Cox's avatar
Russ Cox committed
1220
	n = newname(lookup(name));
Ken Thompson's avatar
Ken Thompson committed
1221
	n = nod(ODCLFIELD, n, N);
Ken Thompson's avatar
Ken Thompson committed
1222
	n->embedded = 1;
Ken Thompson's avatar
Ken Thompson committed
1223 1224 1225 1226
	if(s == S)
		return n;
	n->type = oldtype(s);
	if(isptr[n->type->etype])
Ken Thompson's avatar
Ken Thompson committed
1227
		yyerror("embedded type cannot be a pointer");
Ken Thompson's avatar
Ken Thompson committed
1228 1229
	return n;
}
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

/*
 * 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);
1251

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	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);
		defaultlit(e);
		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
 * new_name_list [type] [= expr_list]
 */
void
constiter(Node *vv, Type *t, Node *cc)
{
	Iter viter, citer;
Ken Thompson's avatar
Ken Thompson committed
1281
	Node *v, *c;
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

	if(cc == N)
		cc = lastconst;
	lastconst = cc;
	vv = rev(vv);
	cc = rev(treecopy(cc));

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

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

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
	if(v == N || c == N) {
		yyerror("shape error in var dcl");
		iota += 1;
		return;
	}

	gettype(c, N);
	if(t != T)
		convlit(c, t);
	dodclconst(v, c);

	v = listnext(&viter);
	c = listnext(&citer);
	goto loop;
}