dcl.c 20.3 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 40 41
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

123 124 125 126 127 128 129 130
	// catch declaration of incomplete type
	switch(n->etype) {
	case TFORWSTRUCT:
	case TFORWINTER:
		break;
	default:
		checkwidth(n);
	}
131 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
/*
 * 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
163 164 165
	t->type = dostruct(this, TFUNC);
	t->type->down = dostruct(out, TFUNC);
	t->type->down->down = dostruct(in, TFUNC);
166 167 168 169 170

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

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

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

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

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

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

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

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

259 260 261
	pa = nil;
	sf = nil;

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

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

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

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

291 292
	pa = f;

Ken Thompson's avatar
Ken Thompson committed
293 294 295
	n = nod(ODCLFIELD, newname(sf), N);
	n->type = t;

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

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

	if(d == T)
Ken Thompson's avatar
Ken Thompson committed
313
		stotype(n, &pa->method);
314 315
	else
		stotype(n, &d->down);
Ken Thompson's avatar
Ken Thompson committed
316 317

	if(dflag())
318
		print("method         %S of type %T\n", sf, pa);
319 320 321
	return;

bad:
322
	yyerror("invalid receiver type %T", pa);
323 324
}

Ken Thompson's avatar
init  
Ken Thompson committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
/*
 * a function named init is a special case.
 * it is called by the initialization before
 * main is run. to make it unique within a
 * package, the name, normally "pkg.init", is
 * altered to "pkg.<file>_init".
 */
Node*
renameinit(Node *n)
{
	Sym *s;

	s = n->sym;
	if(s == S)
		return n;
	if(strcmp(s->name, "init") != 0)
		return n;
	snprint(namebuf, sizeof(namebuf), "init_%s", filename);
	s = lookup(namebuf);
	return newname(s);
}

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

376
	// check for forward declaration
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	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
392
		if(dflag())
393 394 395 396 397 398 399 400 401 402 403
			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
404
	markdcl();
405 406 407 408 409
	funcargs(n->type);

}

void
Ken Thompson's avatar
Ken Thompson committed
410
funcargs(Type *ft)
411
{
Ken Thompson's avatar
Ken Thompson committed
412
	Type *t;
413
	Iter save;
Ken Thompson's avatar
Ken Thompson committed
414
	int all;
415

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

	// declare the outgoing arguments
Ken Thompson's avatar
Ken Thompson committed
427
	all = 0;
Ken Thompson's avatar
Ken Thompson committed
428 429
	t = structfirst(&save, getoutarg(ft));
	while(t != T) {
Ken Thompson's avatar
div bug  
Ken Thompson committed
430 431
		if(t->nname != N)
			t->nname->xoffset = t->width;
Ken Thompson's avatar
Ken Thompson committed
432 433
		if(t->nname != N && t->nname->sym->name[0] != '_') {
			addvar(t->nname, t->type, PPARAM);
Ken Thompson's avatar
Ken Thompson committed
434 435 436
			all |= 1;
		} else
			all |= 2;
Ken Thompson's avatar
Ken Thompson committed
437
		t = structnext(&save);
Ken Thompson's avatar
Ken Thompson committed
438
	}
439 440

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

Ken Thompson's avatar
Ken Thompson committed
444
	ft->outnamed = 0;
Ken Thompson's avatar
Ken Thompson committed
445
	if(all == 1)
Ken Thompson's avatar
Ken Thompson committed
446
		ft->outnamed = 1;
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
}

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

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

	n = listfirst(&save, &n);

loop:
Russ Cox's avatar
Russ Cox committed
480
	note = nil;
481 482 483 484 485 486 487 488 489 490 491 492 493 494
	if(n == N) {
		*t = T;
		return t;
	}

	if(n->op == OLIST) {
		// recursive because it can be lists of lists
		t = stotype(n, t);
		goto next;
	}

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

Russ Cox's avatar
Russ Cox committed
495 496 497 498 499 500 501 502 503 504 505
	switch(n->val.ctype) {
	case CTSTR:
		note = n->val.u.sval;
		break;
	default:
		yyerror("structure field annotation must be string");
	case CTxxx:
		note = nil;
		break;
	}

506 507
	f = typ(TFIELD);
	f->type = n->type;
Russ Cox's avatar
Russ Cox committed
508
	f->note = note;
Russ Cox's avatar
Russ Cox committed
509
	f->width = BADWIDTH;
510 511 512

	if(n->left != N && n->left->op == ONAME) {
		f->nname = n->left;
Ken Thompson's avatar
Ken Thompson committed
513
		f->embedded = n->embedded;
514
		f->sym = f->nname->sym;
515 516 517 518 519 520 521 522 523 524 525 526 527 528
	}

	*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
529
	int funarg;
530 531 532 533 534 535

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

Russ Cox's avatar
Russ Cox committed
536 537 538 539 540
	funarg = 0;
	if(et == TFUNC) {
		funarg = 1;
		et = TSTRUCT;
	}
541
	t = typ(et);
Russ Cox's avatar
Russ Cox committed
542
	t->funarg = funarg;
543
	stotype(n, &t->type);
Russ Cox's avatar
Russ Cox committed
544 545
	if(!funarg)
		checkwidth(t);
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	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
567 568
	a->block = b->block;
	a->lastlineno = b->lastlineno;
569
	a->offset = b->offset;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
}

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
594
popdcl(void)
595 596 597
{
	Sym *d, *s;

Ken Thompson's avatar
Ken Thompson committed
598
//	if(dflag())
599
//		print("revert\n");
600

601 602 603 604 605
	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
606
		if(dflag())
607
			print("\t%L pop %S\n", lineno, s);
608 609 610 611
	}
	if(d == S)
		fatal("popdcl: no mark");
	dclstack = d->link;
Ken Thompson's avatar
bug126  
Ken Thompson committed
612
	block = d->block;
613 614 615 616 617 618 619 620 621 622 623 624
}

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
625
		if(dflag())
626
			print("\t%L pop %S\n", lineno, s);
627 628 629
	}
	if(d == S)
		fatal("poptodcl: no mark");
Russ Cox's avatar
Russ Cox committed
630
	dclstack = d;
631 632 633
}

void
Ken Thompson's avatar
Ken Thompson committed
634
markdcl(void)
635 636 637 638 639
{
	Sym *d;

	d = push();
	d->name = nil;		// used as a mark in fifo
Ken Thompson's avatar
bug126  
Ken Thompson committed
640
	d->block = block;
641 642 643

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

Ken Thompson's avatar
Ken Thompson committed
645
//	if(dflag())
646 647 648 649 650 651 652 653
//		print("markdcl\n");
}

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

Ken Thompson's avatar
Ken Thompson committed
654
	markdcl();
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

	// 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);
		}
	}
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
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);
	}
}

695 696 697 698 699 700 701 702 703 704 705 706 707
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
708 709 710
static void
redeclare(char *str, Sym *s)
{
Ken Thompson's avatar
Ken Thompson committed
711 712 713
	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
714
	}
Ken Thompson's avatar
Ken Thompson committed
715 716
	s->block = block;
	s->lastlineno = lineno;
Ken Thompson's avatar
bug126  
Ken Thompson committed
717 718
}

719 720 721 722 723 724 725 726 727 728 729 730
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;

731 732 733 734 735 736 737
	if(ctxt == PEXTERN) {
		r = externdcl;
		gen = 0;
	} else {
		r = autodcl;
		vargen++;
		gen = vargen;
738
		pushdcl(s);
739
	}
740

Ken Thompson's avatar
bug126  
Ken Thompson committed
741
	redeclare("variable", s);
742 743 744
	s->vargen = gen;
	s->oname = n;
	s->offset = 0;
Russ Cox's avatar
Russ Cox committed
745
	s->lexical = LNAME;
746 747 748 749 750 751 752 753 754 755 756 757 758

	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
759
	if(dflag()) {
760 761 762 763 764 765 766 767
		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
768
addtyp(Type *n, int ctxt)
769 770 771
{
	Dcl *r, *d;
	Sym *s;
Russ Cox's avatar
6g:  
Russ Cox committed
772
	static int typgen;
773

774 775
	if(n==T || n->sym == S)
		fatal("addtyp: n=%T t=%T nil", n);
776 777 778

	s = n->sym;

779
	if(ctxt == PEXTERN)
780
		r = externdcl;
781 782 783
	else {
		r = autodcl;
		pushdcl(s);
Russ Cox's avatar
Russ Cox committed
784
		n->vargen = ++typgen;
785 786
	}

Ken Thompson's avatar
bug126  
Ken Thompson committed
787
	redeclare("type", s);
788
	s->otype = n;
789 790 791 792
	s->lexical = LATYPE;

	d = dcl();
	d->dsym = s;
793
	d->dtype = n;
794 795
	d->op = OTYPE;

Russ Cox's avatar
6g:  
Russ Cox committed
796 797 798 799 800 801 802 803 804 805
	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;
806 807 808
	r->back->forw = d;
	r->back = d;

Ken Thompson's avatar
Ken Thompson committed
809
	if(dflag()) {
810
		if(ctxt == PEXTERN)
811
			print("extern typ-dcl %S G%ld %T\n", s, s->vargen, n);
812
		else
813
			print("auto   typ-dcl %S G%ld %T\n", s, s->vargen, n);
814 815 816
	}
}

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
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
840
	redeclare("constant", s);
841 842 843 844 845 846 847
	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
848
	d->back = r->back;
849 850 851 852 853 854 855
	r->back->forw = d;
	r->back = d;

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

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
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;
884
	n->ullman = 1;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	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) {
901 902 903 904
		n = nod(ONONAME, N, N);
		n->sym = s;
		n->type = T;
		n->addable = 1;
905
		n->ullman = 1;
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
	}
	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;
}

935 936 937 938 939 940 941 942 943 944 945 946 947 948 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
/*
 * 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
1019
// hand-craft the following initialization code
Ken Thompson's avatar
init  
Ken Thompson committed
1020 1021 1022 1023 1024 1025 1026
//	var	init_<file>_done bool;			(1)
//	func	init_<file>_function()			(2)
//		if init_<file>_done { return }		(3)
//		init_<file>_done = true;		(4)
//		// over all matching imported symbols
//			<pkg>.init_<file>_function()	(5)
//		{ <init stmts> }			(6)
Ken Thompson's avatar
Ken Thompson committed
1027 1028 1029
//		init()	// if any			(7)
//		return					(8)
//	}
Ken Thompson's avatar
init  
Ken Thompson committed
1030
//	export	init_<file>_function			(9)
Ken Thompson's avatar
Ken Thompson committed
1031 1032 1033 1034

void
fninit(Node *n)
{
Russ Cox's avatar
Russ Cox committed
1035
	Node *done;
Ken Thompson's avatar
Ken Thompson committed
1036
	Node *a, *fn, *r;
1037
	uint32 h;
Ken Thompson's avatar
Ken Thompson committed
1038 1039 1040 1041 1042
	Sym *s;

	r = N;

	// (1)
Ken Thompson's avatar
init  
Ken Thompson committed
1043
	snprint(namebuf, sizeof(namebuf), "init_%s_done", filename);
Ken Thompson's avatar
Ken Thompson committed
1044 1045 1046 1047 1048 1049
	done = newname(lookup(namebuf));
	addvar(done, types[TBOOL], PEXTERN);

	// (2)

	maxarg = 0;
1050
	stksize = initstksize;
Ken Thompson's avatar
Ken Thompson committed
1051

Ken Thompson's avatar
Ken Thompson committed
1052 1053 1054 1055
	snprint(namebuf, sizeof(namebuf), "init_%s_function", filename);

	// 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
1056
	if(strcmp(package, "main") == 0)
Ken Thompson's avatar
Ken Thompson committed
1057 1058
		snprint(namebuf, sizeof(namebuf), "init_function");

Ken Thompson's avatar
Ken Thompson committed
1059 1060 1061 1062
	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
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

	// (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) {
		if(s->name[0] != 'i')
			continue;
Ken Thompson's avatar
init  
Ken Thompson committed
1079
		if(strstr(s->name, "init_") == nil)
Ken Thompson's avatar
Ken Thompson committed
1080
			continue;
Ken Thompson's avatar
init  
Ken Thompson committed
1081
		if(strstr(s->name, "_function") == nil)
Ken Thompson's avatar
Ken Thompson committed
1082 1083 1084 1085
			continue;
		if(s->oname == N)
			continue;

Ken Thompson's avatar
Ken Thompson committed
1086
		// could check that it is fn of no args/returns
Ken Thompson's avatar
Ken Thompson committed
1087 1088 1089 1090 1091 1092 1093 1094
		a = nod(OCALL, s->oname, N);
		r = list(r, a);
	}

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

	// (7)
Ken Thompson's avatar
Ken Thompson committed
1095
	// could check that it is fn of no args/returns
Ken Thompson's avatar
init  
Ken Thompson committed
1096 1097 1098 1099
	snprint(namebuf, sizeof(namebuf), "init_%s", filename);
	s = lookup(namebuf);
	if(s->oname != N) {
		a = nod(OCALL, s->oname, N);
Ken Thompson's avatar
Ken Thompson committed
1100 1101 1102 1103 1104 1105 1106 1107
		r = list(r, a);
	}

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

	// (9)
1108
	exportsym(fn->nname->sym);
Ken Thompson's avatar
Ken Thompson committed
1109

Ken Thompson's avatar
Ken Thompson committed
1110 1111 1112
	fn->nbody = rev(r);
//dump("b", fn);
//dump("r", fn->nbody);
Ken Thompson's avatar
Ken Thompson committed
1113 1114

	popdcl();
Ken Thompson's avatar
Ken Thompson committed
1115
	compile(fn);
Ken Thompson's avatar
Ken Thompson committed
1116
}
1117 1118 1119 1120 1121 1122 1123 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


/*
 * 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
1149 1150 1151 1152 1153
	// function arg structs should not be checked
	// outside of the enclosing function.
	if(t->funarg)
		fatal("checkwidth %T", t);

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	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)
{
1173 1174 1175
	// we get out of sync on syntax errors, so don't be pedantic.
	// if(defercalc)
	//	fatal("defercheckwidth");
1176 1177 1178 1179 1180 1181
	defercalc = 1;
}

void
resumecheckwidth(void)
{
Russ Cox's avatar
Russ Cox committed
1182
	TypeList *l;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

	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
1195 1196

Node*
Ken Thompson's avatar
Ken Thompson committed
1197
embedded(Sym *s)
Ken Thompson's avatar
Ken Thompson committed
1198 1199
{
	Node *n;
Russ Cox's avatar
Russ Cox committed
1200
	char *name;
1201

Russ Cox's avatar
Russ Cox committed
1202 1203 1204 1205 1206 1207 1208 1209 1210
	// 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
1211

Russ Cox's avatar
Russ Cox committed
1212
	n = newname(lookup(name));
Ken Thompson's avatar
Ken Thompson committed
1213
	n = nod(ODCLFIELD, n, N);
Ken Thompson's avatar
Ken Thompson committed
1214
	n->embedded = 1;
Ken Thompson's avatar
Ken Thompson committed
1215 1216 1217 1218
	if(s == S)
		return n;
	n->type = oldtype(s);
	if(isptr[n->type->etype])
Ken Thompson's avatar
Ken Thompson committed
1219
		yyerror("embedded type cannot be a pointer");
Ken Thompson's avatar
Ken Thompson committed
1220 1221
	return n;
}
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242

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

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	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
1273
	Node *v, *c;
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288

	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;
	}
1289

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	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;
}