subr.c 51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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"

void
errorexit(void)
{
	if(outfile)
		remove(outfile);
13
	exit(1);
14 15 16 17 18 19 20
}

void
yyerror(char *fmt, ...)
{
	va_list arg;

Ken Thompson's avatar
Ken Thompson committed
21
	print("%L: ", lineno);
22 23 24
	va_start(arg, fmt);
	vfprint(1, fmt, arg);
	va_end(arg);
Russ Cox's avatar
Russ Cox committed
25 26
	if(strcmp(fmt, "syntax error") == 0) {
		nsyntaxerrors++;
27
		print(" near %s", lexbuf);
Russ Cox's avatar
Russ Cox committed
28
	}
29 30 31 32 33
	print("\n");
	if(debug['h'])
		*(int*)0 = 0;

	nerrors++;
34
	if(nerrors >= 10 && !debug['e'])
35 36 37 38 39 40 41 42
		fatal("too many errors");
}

void
warn(char *fmt, ...)
{
	va_list arg;

Ken Thompson's avatar
Ken Thompson committed
43
	print("%L: ", lineno);
44 45 46 47 48 49 50 51 52 53 54 55 56
	va_start(arg, fmt);
	vfprint(1, fmt, arg);
	va_end(arg);
	print("\n");
	if(debug['h'])
		*(int*)0 = 0;
}

void
fatal(char *fmt, ...)
{
	va_list arg;

Ken Thompson's avatar
Ken Thompson committed
57
	print("%L: fatal error: ", lineno);
58 59 60 61 62 63
	va_start(arg, fmt);
	vfprint(1, fmt, arg);
	va_end(arg);
	print("\n");
	if(debug['h'])
		*(int*)0 = 0;
64
	exit(1);
65 66
}

67
void
68
linehist(char *file, int32 off, int relative)
69 70
{
	Hist *h;
Ken Thompson's avatar
Ken Thompson committed
71
	char *cp;
72

Ken Thompson's avatar
Ken Thompson committed
73 74 75 76 77 78 79 80 81
	if(debug['i']) {
		if(file != nil) {
			if(off < 0)
				print("pragma %s at line %L\n", file, lineno);
			else
				print("import %s at line %L\n", file, lineno);
		} else
			print("end of import at line %L\n", lineno);
	}
82

83
	if(off < 0 && file[0] != '/' && !relative) {
Ken Thompson's avatar
Ken Thompson committed
84 85 86 87 88
		cp = mal(strlen(file) + strlen(pathname) + 2);
		sprint(cp, "%s/%s", pathname, file);
		file = cp;
	}

89 90 91 92 93 94 95 96 97 98 99 100 101 102
	h = alloc(sizeof(Hist));
	h->name = file;
	h->line = lineno;
	h->offset = off;
	h->link = H;
	if(ehist == H) {
		hist = h;
		ehist = h;
		return;
	}
	ehist->link = h;
	ehist = h;
}

103
int32
Ken Thompson's avatar
Ken Thompson committed
104 105
setlineno(Node *n)
{
106
	int32 lno;
Ken Thompson's avatar
Ken Thompson committed
107 108

	lno = lineno;
109 110 111 112 113 114 115 116
	if(n != N)
	switch(n->op) {
	case ONAME:
	case OTYPE:
	case OPACK:
	case OLITERAL:
		break;
	default:
Ken Thompson's avatar
Ken Thompson committed
117
		lineno = n->lineno;
Ken Thompson's avatar
Ken Thompson committed
118 119 120 121 122 123
		if(lineno == 0) {
			if(debug['K'])
				warn("setlineno: line 0");
			lineno = lno;
		}
	}
Ken Thompson's avatar
Ken Thompson committed
124 125 126
	return lno;
}

127
uint32
128 129
stringhash(char *p)
{
130
	int32 h;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	int c;

	h = 0;
	for(;;) {
		c = *p++;
		if(c == 0)
			break;
		h = h*PRIME1 + c;
	}

	if(h < 0) {
		h = -h;
		if(h < 0)
			h = 0;
	}
	return h;
}

Sym*
lookup(char *p)
{
	Sym *s;
153
	uint32 h;
154 155 156 157 158 159 160 161 162
	int c;

	h = stringhash(p) % NHASH;
	c = p[0];

	for(s = hash[h]; s != S; s = s->link) {
		if(s->name[0] != c)
			continue;
		if(strcmp(s->name, p) == 0)
Russ Cox's avatar
6g:  
Russ Cox committed
163
			if(s->package && strcmp(s->package, package) == 0)
164 165 166 167 168 169
				return s;
	}

	s = mal(sizeof(*s));
	s->name = mal(strlen(p)+1);
	s->package = package;
Russ Cox's avatar
Russ Cox committed
170
	s->lexical = LNAME;
171 172 173 174 175 176 177 178 179 180 181 182 183

	strcpy(s->name, p);

	s->link = hash[h];
	hash[h] = s;

	return s;
}

Sym*
pkglookup(char *p, char *k)
{
	Sym *s;
184
	uint32 h;
185 186 187 188 189 190 191 192
	int c;

	h = stringhash(p) % NHASH;
	c = p[0];
	for(s = hash[h]; s != S; s = s->link) {
		if(s->name[0] != c)
			continue;
		if(strcmp(s->name, p) == 0)
Russ Cox's avatar
6g:  
Russ Cox committed
193
			if(s->package && strcmp(s->package, k) == 0)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
				return s;
	}

	s = mal(sizeof(*s));
	s->name = mal(strlen(p)+1);
	strcpy(s->name, p);

	// botch - should probably try to reuse the pkg string
	s->package = mal(strlen(k)+1);
	strcpy(s->package, k);

	s->link = hash[h];
	hash[h] = s;

	return s;
}

Russ Cox's avatar
6g:  
Russ Cox committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
// find all the symbols in package opkg
// and make them available in the current package
void
importdot(Sym *opkg)
{
	Sym *s, *s1;
	uint32 h;
	int c;

	if(strcmp(opkg->name, package) == 0)
		return;

	c = opkg->name[0];
	for(h=0; h<NHASH; h++) {
		for(s = hash[h]; s != S; s = s->link) {
			if(s->package[0] != c)
				continue;
			if(strcmp(s->package, opkg->name) != 0)
				continue;
			s1 = lookup(s->name);
Russ Cox's avatar
Russ Cox committed
231
			if(s1->def != N) {
Russ Cox's avatar
6g:  
Russ Cox committed
232 233 234
				yyerror("redeclaration of %S during import", s1);
				continue;
			}
Russ Cox's avatar
Russ Cox committed
235
			s1->def = s->def;
Russ Cox's avatar
6g:  
Russ Cox committed
236 237 238 239
		}
	}
}

240 241 242 243
void
gethunk(void)
{
	char *h;
244
	int32 nh;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

	nh = NHUNK;
	if(thunk >= 10L*NHUNK)
		nh = 10L*NHUNK;
	h = (char*)malloc(nh);
	if(h == (char*)-1) {
		yyerror("out of memory");
		errorexit();
	}
	hunk = h;
	nhunk = nh;
	thunk += nh;
}

void*
260
mal(int32 n)
261 262 263
{
	void *p;

264
	while((uintptr)hunk & MAXALIGN) {
265 266 267 268 269 270 271 272 273 274 275 276 277 278
		hunk++;
		nhunk--;
	}
	while(nhunk < n)
		gethunk();

	p = hunk;
	nhunk -= n;
	hunk += n;
	memset(p, 0, n);
	return p;
}

void*
279
remal(void *p, int32 on, int32 n)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
{
	void *q;

	q = (uchar*)p + on;
	if(q != hunk || nhunk < n) {
		while(nhunk < on+n)
			gethunk();
		memmove(hunk, p, on);
		p = hunk;
		hunk += on;
		nhunk -= on;
	}
	hunk += n;
	nhunk -= n;
	return p;
}

Dcl*
dcl(void)
{
	Dcl *d;

	d = mal(sizeof(*d));
Ken Thompson's avatar
Ken Thompson committed
303
	d->lineno = lineno;
304 305 306
	return d;
}

307
extern int yychar;
308 309 310 311 312 313 314 315 316
Node*
nod(int op, Node *nleft, Node *nright)
{
	Node *n;

	n = mal(sizeof(*n));
	n->op = op;
	n->left = nleft;
	n->right = nright;
317 318 319 320
	if(yychar <= 0)	// no lookahead
		n->lineno = lineno;
	else
		n->lineno = prevlineno;
Russ Cox's avatar
Russ Cox committed
321
	n->xoffset = BADWIDTH;
322 323 324
	return n;
}

Ken Thompson's avatar
Ken Thompson committed
325 326 327 328 329
int
algtype(Type *t)
{
	int a;

330
	if(issimple[t->etype] || isptr[t->etype] || t->etype == TCHAN || t->etype == TFUNC || t->etype == TMAP)
331
		a = AMEM;	// just bytes (int, ptr, etc)
332
	else if(t->etype == TSTRING)
Ken Thompson's avatar
Ken Thompson committed
333
		a = ASTRING;	// string
334 335
	else if(isnilinter(t))
		a = ANILINTER;	// nil interface
Russ Cox's avatar
allow  
Russ Cox committed
336
	else if(t->etype == TINTER || t->etype == TFORWINTER)
Ken Thompson's avatar
Ken Thompson committed
337
		a = AINTER;	// interface
338 339
	else
		a = ANOEQ;	// just bytes, but no hash/eq
Ken Thompson's avatar
Ken Thompson committed
340 341 342
	return a;
}

343 344 345 346 347
Type*
maptype(Type *key, Type *val)
{
	Type *t;

Russ Cox's avatar
Russ Cox committed
348 349 350 351 352 353 354 355 356 357 358 359 360
	if(key != nil && key->etype != TANY && algtype(key) == ANOEQ) {
		if(key->etype == TFORW) {
			// map[key] used during definition of key.
			// postpone check until key is fully defined.
			// if there are multiple uses of map[key]
			// before key is fully defined, the error
			// will only be printed for the first one.
			// good enough.
			if(key->maplineno == 0)
				key->maplineno = lineno;
		} else
			yyerror("invalid map key type %T", key);
	}
361 362 363 364 365 366 367 368 369 370 371 372
	t = typ(TMAP);
	t->down = key;
	t->type = val;
	return t;
}

int
iskeytype(Type *t)
{
	return algtype(t) != ANOEQ;
}

Ken Thompson's avatar
Ken Thompson committed
373 374 375 376 377 378 379 380 381 382
Node*
list(Node *a, Node *b)
{
	if(a == N)
		return b;
	if(b == N)
		return a;
	return nod(OLIST, a, b);
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
Type*
typ(int et)
{
	Type *t;

	t = mal(sizeof(*t));
	t->etype = et;
	return t;
}

Node*
dobad(void)
{
	return nod(OBAD, N, N);
}

Node*
400
nodintconst(int64 v)
401 402 403 404 405
{
	Node *c;

	c = nod(OLITERAL, N, N);
	c->addable = 1;
Ken Thompson's avatar
Ken Thompson committed
406 407
	c->val.u.xval = mal(sizeof(*c->val.u.xval));
	mpmovecfix(c->val.u.xval, v);
408
	c->val.ctype = CTINT;
409
	c->type = types[TIDEAL];
410 411 412 413
	ullmancalc(c);
	return c;
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
void
nodconst(Node *n, Type *t, int64 v)
{
	memset(n, 0, sizeof(*n));
	n->op = OLITERAL;
	n->addable = 1;
	ullmancalc(n);
	n->val.u.xval = mal(sizeof(*n->val.u.xval));
	mpmovecfix(n->val.u.xval, v);
	n->val.ctype = CTINT;
	n->type = t;

	if(isfloat[t->etype])
		fatal("nodconst: bad type %T", t);
}

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
Node*
nodnil(void)
{
	Node *c;

	c = nodintconst(0);
	c->val.ctype = CTNIL;
	c->type = types[TNIL];
	return c;
}

Node*
nodbool(int b)
{
	Node *c;

	c = nodintconst(0);
	c->val.ctype = CTBOOL;
	c->val.u.bval = b;
	c->type = types[TBOOL];
	return c;
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
Node*
rev(Node *na)
{
	Node *i, *n;

	/*
	 * since yacc wants to build lists
	 * stacked down on the left -
	 * this routine converts them to
	 * stack down on the right -
	 * in memory without recursion
	 */

	if(na == N || na->op != OLIST)
		return na;
	i = na;
	for(n = na->left; n != N; n = n->left) {
		if(n->op != OLIST)
			break;
		i->left = n->right;
		n->right = i;
		i = n;
	}
	i->left = n;
	return i;
}

Node*
unrev(Node *na)
{
	Node *i, *n;

	/*
	 * this restores a reverse list
	 */
	if(na == N || na->op != OLIST)
		return na;
	i = na;
	for(n = na->right; n != N; n = n->right) {
		if(n->op != OLIST)
			break;
		i->right = n->left;
		n->left = i;
		i = n;
	}
	i->right = n;
	return i;
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/*
 * na and nb are reversed lists.
 * append them into one big reversed list.
 */
Node*
appendr(Node *na, Node *nb)
{
	Node **l, *n;

	for(l=&nb; (n=*l)->op == OLIST; l=&n->left)
		;
	*l = nod(OLIST, na, *l);
	return nb;
}

517 518 519 520
Type*
aindex(Node *b, Type *t)
{
	Type *r;
Ken Thompson's avatar
Ken Thompson committed
521
	int bound;
522

523
	bound = -1;	// open bound
524
	walktype(b, Erv);
525 526 527 528 529 530 531 532 533 534 535
	if(b != nil) {
		switch(consttype(b)) {
		default:
			yyerror("array bound must be an integer expression");
			break;
		case CTINT:
			bound = mpgetfix(b->val.u.xval);
			if(bound < 0)
				yyerror("array bound must be non negative");
			break;
		}
536
	}
Ken Thompson's avatar
Ken Thompson committed
537 538 539 540 541

	// fixed array
	r = typ(TARRAY);
	r->type = t;
	r->bound = bound;
Russ Cox's avatar
Russ Cox committed
542
	checkwidth(r);
Ken Thompson's avatar
Ken Thompson committed
543
	return r;
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
}

void
indent(int dep)
{
	int i;

	for(i=0; i<dep; i++)
		print(".   ");
}

void
dodump(Node *n, int dep)
{

loop:
	if(n == N)
		return;

	switch(n->op) {
	case OLIST:
		if(n->left != N && n->left->op == OLIST)
			dodump(n->left, dep+1);
		else
			dodump(n->left, dep);
		n = n->right;
		goto loop;
	}

	indent(dep);
	if(dep > 10) {
		print("...\n");
		return;
	}

Ken Thompson's avatar
Ken Thompson committed
579 580 581 582 583 584
	if(n->ninit != N) {
		print("%O-init\n", n->op);
		dodump(n->ninit, dep+1);
		indent(dep);
	}

585 586 587 588 589 590
	switch(n->op) {
	default:
		print("%N\n", n);
		break;

	case OTYPE:
Russ Cox's avatar
Russ Cox committed
591
		print("%O %T\n", n->op, n->type);
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
		break;

	case OIF:
		print("%O%J\n", n->op, n);
		dodump(n->ntest, dep+1);
		if(n->nbody != N) {
			indent(dep);
			print("%O-then\n", n->op);
			dodump(n->nbody, dep+1);
		}
		if(n->nelse != N) {
			indent(dep);
			print("%O-else\n", n->op);
			dodump(n->nelse, dep+1);
		}
		return;

Ken Thompson's avatar
Ken Thompson committed
609 610 611 612 613
	case OSELECT:
		print("%O%J\n", n->op, n);
		dodump(n->nbody, dep+1);
		return;

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
	case OSWITCH:
	case OFOR:
		print("%O%J\n", n->op, n);
		dodump(n->ntest, dep+1);

		if(n->nbody != N) {
			indent(dep);
			print("%O-body\n", n->op);
			dodump(n->nbody, dep+1);
		}

		if(n->nincr != N) {
			indent(dep);
			print("%O-incr\n", n->op);
			dodump(n->nincr, dep+1);
		}
		return;

	case OCASE:
Ken Thompson's avatar
Ken Thompson committed
633 634 635 636 637
		// the right side points to label of the body
		if(n->right != N && n->right->op == OGOTO && n->right->left->op == ONAME)
			print("%O%J GOTO %N\n", n->op, n, n->right->left);
		else
			print("%O%J\n", n->op, n);
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
		dodump(n->left, dep+1);
		return;
	}

	dodump(n->left, dep+1);
	n = n->right;
	dep++;
	goto loop;
}

void
dump(char *s, Node *n)
{
	print("%s\n", s);
	dodump(n, 1);
}

/*
s%,%,\n%g
s%\n+%\n%g
s%^[ 	]*O%%g
s%,.*%%g
s%.+%	[O&]		= "&",%g
s%^	........*\]%&~%g
s%~	%%g
663
|sort
664 665 666 667 668 669 670 671
*/

static char*
opnames[] =
{
	[OADDR]		= "ADDR",
	[OADD]		= "ADD",
	[OANDAND]	= "ANDAND",
672
	[OANDNOT]	= "ANDNOT",
673 674 675 676 677 678 679
	[OAND]		= "AND",
	[OARRAY]	= "ARRAY",
	[OASOP]		= "ASOP",
	[OAS]		= "AS",
	[OBAD]		= "BAD",
	[OBREAK]	= "BREAK",
	[OCALLINTER]	= "CALLINTER",
680 681 682
	[OCALLMETH]	= "CALLMETH",
	[OCALL]		= "CALL",
	[OCAP]		= "CAP",
683
	[OCASE]		= "CASE",
684 685
	[OCLOSED]	= "CLOSED",
	[OCLOSE]	= "CLOSE",
686
	[OCMP]		= "CMP",
687
	[OCOMPMAP]	= "COMPMAP",
688
	[OCOMPOS]	= "COMPOS",
689
	[OCOMPSLICE]	= "COMPSLICE",
690 691
	[OCOM]		= "COM",
	[OCONTINUE]	= "CONTINUE",
692
	[OCONV]		= "CONV",
Russ Cox's avatar
Russ Cox committed
693
	[OCONVNOP]		= "CONVNOP",
694 695 696
	[ODCLARG]	= "DCLARG",
	[ODCLFIELD]	= "DCLFIELD",
	[ODCLFUNC]	= "DCLFUNC",
697 698
	[ODCL]		= "DCL",
	[ODEC]		= "DEC",
Ken Thompson's avatar
defer  
Ken Thompson committed
699
	[ODEFER]	= "DEFER",
700 701
	[ODIV]		= "DIV",
	[ODOTINTER]	= "DOTINTER",
Ken Thompson's avatar
defer  
Ken Thompson committed
702 703
	[ODOTMETH]	= "DOTMETH",
	[ODOTPTR]	= "DOTPTR",
704
	[ODOTTYPE]	= "DOTTYPE",
Ken Thompson's avatar
defer  
Ken Thompson committed
705
	[ODOT]		= "DOT",
706 707 708
	[OEMPTY]	= "EMPTY",
	[OEND]		= "END",
	[OEQ]		= "EQ",
709 710
	[OEXTEND]	= "EXTEND",
	[OFALL]		= "FALL",
711 712 713 714 715 716
	[OFOR]		= "FOR",
	[OFUNC]		= "FUNC",
	[OGE]		= "GE",
	[OGOTO]		= "GOTO",
	[OGT]		= "GT",
	[OIF]		= "IF",
717
	[OINC]		= "INC",
718
	[OINDEX]	= "INDEX",
719
	[OINDREG]	= "INDREG",
720
	[OIND]		= "IND",
Ken Thompson's avatar
Ken Thompson committed
721
	[OKEY]		= "KEY",
722 723
	[OLABEL]	= "LABEL",
	[OLEN]		= "LEN",
724
	[OLE]		= "LE",
725 726 727 728
	[OLIST]		= "LIST",
	[OLITERAL]	= "LITERAL",
	[OLSH]		= "LSH",
	[OLT]		= "LT",
729
	[OMAKE]		= "MAKE",
730 731 732 733
	[OMINUS]	= "MINUS",
	[OMOD]		= "MOD",
	[OMUL]		= "MUL",
	[ONAME]		= "NAME",
734
	[ONEW]		= "NEW",
735
	[ONE]		= "NE",
736
	[ONONAME]	= "NONAME",
737 738 739
	[ONOT]		= "NOT",
	[OOROR]		= "OROR",
	[OOR]		= "OR",
740 741
	[OPANICN]	= "PANICN",
	[OPANIC]	= "PANIC",
Russ Cox's avatar
Russ Cox committed
742
	[OPACK]		= "PACK",
743
	[OPARAM]	= "PARAM",
744
	[OPLUS]		= "PLUS",
745 746 747 748
	[OPRINTN]	= "PRINTN",
	[OPRINT]	= "PRINT",
	[OPROC]		= "PROC",
	[OPTR]		= "PTR",
Ken Thompson's avatar
Ken Thompson committed
749
	[ORANGE]	= "RANGE",
750
	[ORECV]		= "RECV",
751
	[OREGISTER]	= "REGISTER",
752 753
	[ORETURN]	= "RETURN",
	[ORSH]		= "RSH",
754 755
	[OSELECT]	= "SELECT",
	[OSEND]		= "SEND",
756 757 758
	[OSLICE]	= "SLICE",
	[OSUB]		= "SUB",
	[OSWITCH]	= "SWITCH",
759
	[OTYPEOF]	= "TYPEOF",
Ken Thompson's avatar
Ken Thompson committed
760
	[OTYPESW]	= "TYPESW",
761 762
	[OTYPE]		= "TYPE",
	[OXCASE]	= "XCASE",
763
	[OXFALL]	= "XFALL",
764
	[OXOR]		= "XOR",
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	[OXXX]		= "XXX",
};

int
Oconv(Fmt *fp)
{
	char buf[500];
	int o;

	o = va_arg(fp->args, int);
	if(o < 0 || o >= nelem(opnames) || opnames[o] == nil) {
		snprint(buf, sizeof(buf), "O-%d", o);
		return fmtstrcpy(fp, buf);
	}
	return fmtstrcpy(fp, opnames[o]);
}

782 783 784 785 786 787 788
int
Lconv(Fmt *fp)
{
	char str[STRINGSZ], s[STRINGSZ];
	struct
	{
		Hist*	incl;	/* start of this include file */
789
		int32	idel;	/* delta line number to apply to include */
790
		Hist*	line;	/* start of this #line directive */
791
		int32	ldel;	/* delta line number to apply to #line */
792
	} a[HISTSZ];
793
	int32 lno, d;
794 795 796
	int i, n;
	Hist *h;

797
	lno = va_arg(fp->args, int32);
798 799 800

	n = 0;
	for(h=hist; h!=H; h=h->link) {
Ken Thompson's avatar
Ken Thompson committed
801 802
		if(h->offset < 0)
			continue;
803 804 805 806 807 808 809 810 811 812 813 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 840 841 842 843 844
		if(lno < h->line)
			break;
		if(h->name) {
			if(n < HISTSZ) {	/* beginning of file */
				a[n].incl = h;
				a[n].idel = h->line;
				a[n].line = 0;
			}
			n++;
			continue;
		}
		n--;
		if(n > 0 && n < HISTSZ) {
			d = h->line - a[n].incl->line;
			a[n-1].ldel += d;
			a[n-1].idel += d;
		}
	}

	if(n > HISTSZ)
		n = HISTSZ;

	str[0] = 0;
	for(i=n-1; i>=0; i--) {
		if(i != n-1) {
			if(fp->flags & ~(FmtWidth|FmtPrec))
				break;
			strcat(str, " ");
		}
		if(a[i].line)
			snprint(s, STRINGSZ, "%s:%ld[%s:%ld]",
				a[i].line->name, lno-a[i].ldel+1,
				a[i].incl->name, lno-a[i].idel+1);
		else
			snprint(s, STRINGSZ, "%s:%ld",
				a[i].incl->name, lno-a[i].idel+1);
		if(strlen(s)+strlen(str) >= STRINGSZ-10)
			break;
		strcat(str, s);
		lno = a[i].incl->line - 1;	/* now print out start of this file */
	}
	if(n == 0)
Ken Thompson's avatar
Ken Thompson committed
845
		strcat(str, "<epoch>");
846 847 848 849

	return fmtstrcpy(fp, str);
}

850 851 852 853 854 855 856 857 858 859 860 861 862
/*
s%,%,\n%g
s%\n+%\n%g
s%^[ 	]*T%%g
s%,.*%%g
s%.+%	[T&]		= "&",%g
s%^	........*\]%&~%g
s%~	%%g
*/

static char*
etnames[] =
{
Ken Thompson's avatar
Ken Thompson committed
863 864
	[TINT]		= "INT",
	[TUINT]		= "UINT",
865 866 867 868 869 870 871 872
	[TINT8]		= "INT8",
	[TUINT8]	= "UINT8",
	[TINT16]	= "INT16",
	[TUINT16]	= "UINT16",
	[TINT32]	= "INT32",
	[TUINT32]	= "UINT32",
	[TINT64]	= "INT64",
	[TUINT64]	= "UINT64",
Ken Thompson's avatar
Ken Thompson committed
873 874
	[TUINTPTR]	= "UINTPTR",
	[TFLOAT]	= "FLOAT",
875 876 877 878 879 880
	[TFLOAT32]	= "FLOAT32",
	[TFLOAT64]	= "FLOAT64",
	[TFLOAT80]	= "FLOAT80",
	[TBOOL]		= "BOOL",
	[TPTR32]	= "PTR32",
	[TPTR64]	= "PTR64",
Ken Thompson's avatar
Ken Thompson committed
881
	[TDDD]		= "DDD",
882 883 884 885 886 887 888 889 890 891
	[TFUNC]		= "FUNC",
	[TARRAY]	= "ARRAY",
	[TSTRUCT]	= "STRUCT",
	[TCHAN]		= "CHAN",
	[TMAP]		= "MAP",
	[TINTER]	= "INTER",
	[TFORW]		= "FORW",
	[TFIELD]	= "FIELD",
	[TSTRING]	= "STRING",
	[TCHAN]		= "CHAN",
892
	[TANY]		= "ANY",
893 894
	[TFORWINTER]	= "FORWINTER",
	[TFORWSTRUCT]	= "FORWSTRUCT",
895 896 897 898 899 900 901 902 903 904 905 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 935 936 937 938 939
};

int
Econv(Fmt *fp)
{
	char buf[500];
	int et;

	et = va_arg(fp->args, int);
	if(et < 0 || et >= nelem(etnames) || etnames[et] == nil) {
		snprint(buf, sizeof(buf), "E-%d", et);
		return fmtstrcpy(fp, buf);
	}
	return fmtstrcpy(fp, etnames[et]);
}

int
Jconv(Fmt *fp)
{
	char buf[500], buf1[100];
	Node *n;

	n = va_arg(fp->args, Node*);
	strcpy(buf, "");

	if(n->ullman != 0) {
		snprint(buf1, sizeof(buf1), " u(%d)", n->ullman);
		strncat(buf, buf1, sizeof(buf));
	}

	if(n->addable != 0) {
		snprint(buf1, sizeof(buf1), " a(%d)", n->addable);
		strncat(buf, buf1, sizeof(buf));
	}

	if(n->vargen != 0) {
		snprint(buf1, sizeof(buf1), " g(%ld)", n->vargen);
		strncat(buf, buf1, sizeof(buf));
	}

	if(n->lineno != 0) {
		snprint(buf1, sizeof(buf1), " l(%ld)", n->lineno);
		strncat(buf, buf1, sizeof(buf));
	}

Russ Cox's avatar
Russ Cox committed
940 941 942 943 944
	if(n->xoffset != 0) {
		snprint(buf1, sizeof(buf1), " x(%lld)", n->xoffset);
		strncat(buf, buf1, sizeof(buf));
	}

945 946 947 948 949 950 951 952 953 954
	if(n->class != 0) {
		snprint(buf1, sizeof(buf1), " class(%d)", n->class);
		strncat(buf, buf1, sizeof(buf));
	}

	if(n->colas != 0) {
		snprint(buf1, sizeof(buf1), " colas(%d)", n->colas);
		strncat(buf, buf1, sizeof(buf));
	}

Russ Cox's avatar
Russ Cox committed
955 956 957 958 959 960
	if(n->funcdepth != 0) {
		snprint(buf1, sizeof(buf1), " f(%d)", n->funcdepth);
		strncat(buf, buf1, sizeof(buf));
	}


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
	return fmtstrcpy(fp, buf);
}

int
Gconv(Fmt *fp)
{
	char buf[100];
	Type *t;

	t = va_arg(fp->args, Type*);

	if(t->etype == TFUNC) {
		if(t->vargen != 0) {
			snprint(buf, sizeof(buf), "-%d%d%d g(%ld)",
				t->thistuple, t->outtuple, t->intuple, t->vargen);
			goto out;
		}
		snprint(buf, sizeof(buf), "-%d%d%d",
			t->thistuple, t->outtuple, t->intuple);
		goto out;
	}
	if(t->vargen != 0) {
		snprint(buf, sizeof(buf), " g(%ld)", t->vargen);
		goto out;
	}
	strcpy(buf, "");

out:
	return fmtstrcpy(fp, buf);
}

int
Sconv(Fmt *fp)
{
	Sym *s;
Russ Cox's avatar
6g:  
Russ Cox committed
996
	char *pkg, *nam;
997 998 999

	s = va_arg(fp->args, Sym*);
	if(s == S) {
1000 1001
		fmtstrcpy(fp, "<S>");
		return 0;
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	}

	pkg = "<nil>";
	nam = pkg;

	if(s->package != nil)
		pkg = s->package;
	if(s->name != nil)
		nam = s->name;

Russ Cox's avatar
Russ Cox committed
1012
	if(!(fp->flags & FmtShort))
Russ Cox's avatar
6g:  
Russ Cox committed
1013 1014
	if(strcmp(pkg, package) != 0 || (fp->flags & FmtLong)) {
		fmtprint(fp, "%s.%s", pkg, nam);
1015
		return 0;
1016
	}
1017 1018
	fmtstrcpy(fp, nam);
	return 0;
1019 1020
}

Ken Thompson's avatar
Ken Thompson committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
static char*
basicnames[] =
{
	[TINT]		= "int",
	[TUINT]		= "uint",
	[TINT8]		= "int8",
	[TUINT8]	= "uint8",
	[TINT16]	= "int16",
	[TUINT16]	= "uint16",
	[TINT32]	= "int32",
	[TUINT32]	= "uint32",
	[TINT64]	= "int64",
	[TUINT64]	= "uint64",
	[TUINTPTR]	= "uintptr",
	[TFLOAT]	= "float",
	[TFLOAT32]	= "float32",
	[TFLOAT64]	= "float64",
	[TFLOAT80]	= "float80",
	[TBOOL]		= "bool",
	[TANY]		= "any",
Ken Thompson's avatar
Ken Thompson committed
1041
	[TDDD]		= "...",
Russ Cox's avatar
Russ Cox committed
1042
	[TSTRING]		= "string",
1043 1044
	[TNIL]		= "nil",
	[TIDEAL]		= "ideal",
1045 1046 1047 1048 1049 1050
};

int
Tpretty(Fmt *fp, Type *t)
{
	Type *t1;
Russ Cox's avatar
6g:  
Russ Cox committed
1051
	Sym *s;
1052

Russ Cox's avatar
Russ Cox committed
1053 1054
	if(t->etype != TFIELD
	&& t->sym != S
Russ Cox's avatar
6g:  
Russ Cox committed
1055 1056
	&& !(fp->flags&FmtLong)) {
		s = t->sym;
Russ Cox's avatar
Russ Cox committed
1057
		if(t == types[t->etype])
Russ Cox's avatar
6g:  
Russ Cox committed
1058 1059
			return fmtprint(fp, "%s", s->name);
		if(exporting) {
Russ Cox's avatar
Russ Cox committed
1060 1061 1062 1063
			if(fp->flags & FmtShort)
				fmtprint(fp, "%hS", s);
			else
				fmtprint(fp, "%lS", s);
Russ Cox's avatar
Russ Cox committed
1064 1065 1066 1067 1068
			if(strcmp(s->package, package) != 0)
				return 0;
			if(s->imported)
				return 0;
			if(s->def == N || s->def->op != OTYPE || s->def->type != t || !s->export) {
Russ Cox's avatar
Russ Cox committed
1069 1070 1071 1072 1073
				fmtprint(fp, "·%s", filename);
				if(t->vargen)
					fmtprint(fp, "·%d", t->vargen);
			}
			return 0;
Russ Cox's avatar
6g:  
Russ Cox committed
1074 1075 1076
		}
		return fmtprint(fp, "%S", s);
	}
Russ Cox's avatar
Russ Cox committed
1077

1078 1079
	if(t->etype < nelem(basicnames) && basicnames[t->etype] != nil)
		return fmtprint(fp, "%s", basicnames[t->etype]);
Russ Cox's avatar
Russ Cox committed
1080

1081 1082 1083
	switch(t->etype) {
	case TPTR32:
	case TPTR64:
Russ Cox's avatar
Russ Cox committed
1084
		if(fp->flags&FmtShort)	// pass flag thru for methodsym
Russ Cox's avatar
Russ Cox committed
1085 1086
			return fmtprint(fp, "*%hT", t->type);
		return fmtprint(fp, "*%T", t->type);
1087 1088

	case TCHAN:
Russ Cox's avatar
Russ Cox committed
1089 1090 1091 1092
		switch(t->chan) {
		case Crecv:
			return fmtprint(fp, "<-chan %T", t->type);
		case Csend:
Russ Cox's avatar
Russ Cox committed
1093 1094
			if(t->type != T && t->type->etype == TCHAN)
				return fmtprint(fp, "chan<- (%T)", t->type);
Russ Cox's avatar
Russ Cox committed
1095 1096 1097 1098
			return fmtprint(fp, "chan<- %T", t->type);
		}
		return fmtprint(fp, "chan %T", t->type);

1099
	case TMAP:
Russ Cox's avatar
Russ Cox committed
1100
		return fmtprint(fp, "map[%T] %T", t->down, t->type);
Russ Cox's avatar
Russ Cox committed
1101

1102
	case TFUNC:
Russ Cox's avatar
Russ Cox committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		// t->type is method struct
		// t->type->down is result struct
		// t->type->down->down is arg struct
		if(t->thistuple && !(fp->flags&FmtSharp) && !(fp->flags&FmtShort)) {
			fmtprint(fp, "method(");
			for(t1=getthisx(t)->type; t1; t1=t1->down) {
				fmtprint(fp, "%T", t1);
				if(t1->down)
					fmtprint(fp, ", ");
			}
			fmtprint(fp, ")");
		}

Russ Cox's avatar
Russ Cox committed
1116 1117
		if(!(fp->flags&FmtByte))
			fmtprint(fp, "func");
1118
		fmtprint(fp, "(");
Russ Cox's avatar
Russ Cox committed
1119
		for(t1=getinargx(t)->type; t1; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1120 1121 1122 1123
			if(noargnames && t1->etype == TFIELD)
				fmtprint(fp, "%T", t1->type);
			else
				fmtprint(fp, "%T", t1);
1124 1125 1126 1127
			if(t1->down)
				fmtprint(fp, ", ");
		}
		fmtprint(fp, ")");
Russ Cox's avatar
Russ Cox committed
1128 1129 1130 1131 1132
		switch(t->outtuple) {
		case 0:
			break;
		case 1:
			t1 = getoutargx(t)->type;
Russ Cox's avatar
Russ Cox committed
1133
			if(t1->etype != TFIELD && t1->etype != TFUNC) {
1134
				fmtprint(fp, " %T", t1);
Russ Cox's avatar
Russ Cox committed
1135
				break;
1136
			}
Russ Cox's avatar
Russ Cox committed
1137 1138 1139 1140
		default:
			t1 = getoutargx(t)->type;
			fmtprint(fp, " (");
			for(; t1; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1141 1142 1143 1144
				if(noargnames && t1->etype == TFIELD)
					fmtprint(fp, "%T", t1->type);
				else
					fmtprint(fp, "%T", t1);
Russ Cox's avatar
Russ Cox committed
1145 1146 1147 1148 1149
				if(t1->down)
					fmtprint(fp, ", ");
			}
			fmtprint(fp, ")");
			break;
1150 1151
		}
		return 0;
Russ Cox's avatar
Russ Cox committed
1152

1153 1154 1155 1156 1157 1158 1159 1160
	case TARRAY:
		if(t->bound >= 0)
			return fmtprint(fp, "[%d]%T", (int)t->bound, t->type);
		return fmtprint(fp, "[]%T", t->type);

	case TINTER:
		fmtprint(fp, "interface {");
		for(t1=t->type; t1!=T; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1161
			fmtprint(fp, " %hS %hhT", t1->sym, t1->type);
Russ Cox's avatar
Russ Cox committed
1162 1163
			if(t1->down)
				fmtprint(fp, ";");
1164
		}
Russ Cox's avatar
Russ Cox committed
1165 1166
		return fmtprint(fp, " }");

1167 1168 1169
	case TSTRUCT:
		fmtprint(fp, "struct {");
		for(t1=t->type; t1!=T; t1=t1->down) {
Russ Cox's avatar
Russ Cox committed
1170 1171 1172
			fmtprint(fp, " %T", t1);
			if(t1->down)
				fmtprint(fp, ";");
1173 1174
		}
		return fmtprint(fp, " }");
Russ Cox's avatar
Russ Cox committed
1175

1176
	case TFIELD:
1177
		if(t->sym == S || t->embedded) {
Russ Cox's avatar
Russ Cox committed
1178 1179
			if(exporting)
				fmtprint(fp, "? ");
1180 1181 1182 1183 1184 1185
			fmtprint(fp, "%T", t->type);
		} else
			fmtprint(fp, "%hS %T", t->sym, t->type);
		if(t->note)
			fmtprint(fp, " \"%Z\"", t->note);
		return 0;
Russ Cox's avatar
Russ Cox committed
1186 1187 1188 1189 1190 1191 1192

	case TFORW:
		if(exporting)
			yyerror("undefined type %S", t->sym);
		if(t->sym)
			return fmtprint(fp, "undefined %S", t->sym);
		return fmtprint(fp, "undefined");
1193 1194 1195 1196 1197 1198
	}

	// Don't know how to handle - fall back to detailed prints.
	return -1;
}

1199 1200 1201 1202 1203
int
Tconv(Fmt *fp)
{
	char buf[500], buf1[500];
	Type *t, *t1;
Russ Cox's avatar
Russ Cox committed
1204 1205 1206 1207 1208
	int r, et, sharp, minus;

	sharp = (fp->flags & FmtSharp);
	minus = (fp->flags & FmtLeft);
	fp->flags &= ~(FmtSharp|FmtLeft);
Russ Cox's avatar
Russ Cox committed
1209

1210 1211 1212 1213 1214
	t = va_arg(fp->args, Type*);
	if(t == T)
		return fmtstrcpy(fp, "<T>");

	t->trecur++;
1215 1216 1217 1218 1219
	if(t->trecur > 5) {
		strncat(buf, "...", sizeof(buf));
		goto out;
	}

Russ Cox's avatar
6g:  
Russ Cox committed
1220
	if(!debug['t']) {
Russ Cox's avatar
Russ Cox committed
1221
		if(sharp)
Russ Cox's avatar
6g:  
Russ Cox committed
1222
			exporting++;
Russ Cox's avatar
Russ Cox committed
1223 1224 1225 1226 1227 1228 1229 1230
		if(minus)
			noargnames++;
		r = Tpretty(fp, t);
		if(sharp)
			exporting--;
		if(minus)
			noargnames--;
		if(r >= 0) {
Russ Cox's avatar
6g:  
Russ Cox committed
1231 1232 1233
			t->trecur--;
			return 0;
		}
1234 1235
	}

1236
	et = t->etype;
Russ Cox's avatar
Russ Cox committed
1237
	snprint(buf, sizeof buf, "%E ", et);
Russ Cox's avatar
Russ Cox committed
1238 1239 1240 1241
	if(t->sym != S) {
		snprint(buf1, sizeof(buf1), "<%S>", t->sym);
		strncat(buf, buf1, sizeof(buf));
	}
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

	switch(et) {
	default:
		if(t->type != T) {
			snprint(buf1, sizeof(buf1), " %T", t->type);
			strncat(buf, buf1, sizeof(buf));
		}
		break;

	case TFIELD:
		snprint(buf1, sizeof(buf1), "%T", t->type);
		strncat(buf, buf1, sizeof(buf));
		break;

	case TFUNC:
		if(fp->flags & FmtLong)
1258 1259 1260
			snprint(buf1, sizeof(buf1), "%d%d%d(%lT,%lT)%lT",
				t->thistuple, t->intuple, t->outtuple,
				t->type, t->type->down->down, t->type->down);
1261
		else
1262 1263 1264
			snprint(buf1, sizeof(buf1), "%d%d%d(%T,%T)%T",
				t->thistuple, t->intuple, t->outtuple,
				t->type, t->type->down->down, t->type->down);
1265 1266 1267 1268
		strncat(buf, buf1, sizeof(buf));
		break;

	case TINTER:
Russ Cox's avatar
Russ Cox committed
1269
		strncat(buf, "{", sizeof(buf));
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
		if(fp->flags & FmtLong) {
			for(t1=t->type; t1!=T; t1=t1->down) {
				snprint(buf1, sizeof(buf1), "%lT;", t1);
				strncat(buf, buf1, sizeof(buf));
			}
		}
		strncat(buf, "}", sizeof(buf));
		break;

	case TSTRUCT:
		strncat(buf, "{", sizeof(buf));
		if(fp->flags & FmtLong) {
			for(t1=t->type; t1!=T; t1=t1->down) {
				snprint(buf1, sizeof(buf1), "%lT;", t1);
				strncat(buf, buf1, sizeof(buf));
			}
		}
		strncat(buf, "}", sizeof(buf));
		break;

	case TMAP:
Russ Cox's avatar
Russ Cox committed
1291
		snprint(buf, sizeof(buf), "[%T]%T", t->down, t->type);
1292 1293 1294
		break;

	case TARRAY:
1295 1296 1297 1298
		if(t->bound >= 0)
			snprint(buf1, sizeof(buf1), "[%ld]%T", t->bound, t->type);
		else
			snprint(buf1, sizeof(buf1), "[]%T", t->type);
1299 1300 1301 1302 1303
		strncat(buf, buf1, sizeof(buf));
		break;

	case TPTR32:
	case TPTR64:
Russ Cox's avatar
Russ Cox committed
1304
		snprint(buf1, sizeof(buf1), "%T", t->type);
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
		strncat(buf, buf1, sizeof(buf));
		break;
	}

out:
	t->trecur--;
	return fmtstrcpy(fp, buf);
}

int
Nconv(Fmt *fp)
{
	char buf[500], buf1[500];
	Node *n;

	n = va_arg(fp->args, Node*);
	if(n == N) {
		snprint(buf, sizeof(buf), "<N>");
		goto out;
	}

	switch(n->op) {
	default:
		snprint(buf, sizeof(buf), "%O%J", n->op, n);
		break;

	case ONAME:
1332
	case ONONAME:
1333 1334 1335 1336 1337 1338 1339 1340 1341
		if(n->sym == S) {
			snprint(buf, sizeof(buf), "%O%J", n->op, n);
			break;
		}
		snprint(buf, sizeof(buf), "%O-%S G%ld%J", n->op,
			n->sym, n->sym->vargen, n);
		goto ptyp;

	case OREGISTER:
Ken Thompson's avatar
Ken Thompson committed
1342
		snprint(buf, sizeof(buf), "%O-%R%J", n->op, n->val.u.reg, n);
1343 1344 1345 1346 1347
		break;

	case OLITERAL:
		switch(n->val.ctype) {
		default:
Ken Thompson's avatar
Ken Thompson committed
1348
			snprint(buf1, sizeof(buf1), "LITERAL-ctype=%d", n->val.ctype);
1349 1350
			break;
		case CTINT:
Ken Thompson's avatar
Ken Thompson committed
1351
			snprint(buf1, sizeof(buf1), "I%B", n->val.u.xval);
1352 1353
			break;
		case CTFLT:
Ken Thompson's avatar
Ken Thompson committed
1354
			snprint(buf1, sizeof(buf1), "F%g", mpgetflt(n->val.u.fval));
1355 1356
			break;
		case CTSTR:
Ken Thompson's avatar
Ken Thompson committed
1357
			snprint(buf1, sizeof(buf1), "S\"%Z\"", n->val.u.sval);
1358 1359
			break;
		case CTBOOL:
Ken Thompson's avatar
Ken Thompson committed
1360
			snprint(buf1, sizeof(buf1), "B%d", n->val.u.bval);
1361 1362 1363 1364 1365 1366 1367
			break;
		case CTNIL:
			snprint(buf1, sizeof(buf1), "N");
			break;
		}
		snprint(buf, sizeof(buf), "%O-%s%J", n->op, buf1, n);
		break;
1368

1369 1370 1371 1372 1373
	case OASOP:
		snprint(buf, sizeof(buf), "%O-%O%J", n->op, n->etype, n);
		break;

	case OTYPE:
Russ Cox's avatar
Russ Cox committed
1374
		snprint(buf, sizeof(buf), "%O %T", n->op, n->type);
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
		break;
	}
	if(n->sym != S) {
		snprint(buf1, sizeof(buf1), " %S G%ld", n->sym, n->sym->vargen);
		strncat(buf, buf1, sizeof(buf));
	}

ptyp:
	if(n->type != T) {
		snprint(buf1, sizeof(buf1), " %T", n->type);
		strncat(buf, buf1, sizeof(buf));
	}

out:
	return fmtstrcpy(fp, buf);
}

Ken Thompson's avatar
Ken Thompson committed
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
Node*
treecopy(Node *n)
{
	Node *m;

	if(n == N)
		return N;

	switch(n->op) {
	default:
		m = nod(OXXX, N, N);
		*m = *n;
		m->left = treecopy(n->left);
		m->right = treecopy(n->right);
		break;

	case OLITERAL:
		if(n->iota) {
1410
			m = nodintconst(iota);
Ken Thompson's avatar
Ken Thompson committed
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
			break;
		}
		m = nod(OXXX, N, N);
		*m = *n;
		break;

	case ONAME:
		m = nod(OXXX, N, N);
		*m = *n;
		break;
	}
	return m;
}

1425 1426 1427
int
Zconv(Fmt *fp)
{
1428
	Rune r;
1429
	Strlit *sp;
1430
	char *s, *se;
1431

1432
	sp = va_arg(fp->args, Strlit*);
1433 1434 1435
	if(sp == nil)
		return fmtstrcpy(fp, "<nil>");

1436 1437
	s = sp->s;
	se = s + sp->len;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
	while(s < se) {
		s += chartorune(&r, s);
		switch(r) {
		default:
			fmtrune(fp, r);
			break;
		case '\0':
			fmtstrcpy(fp, "\\x00");
			break;
		case '\t':
			fmtstrcpy(fp, "\\t");
			break;
		case '\n':
			fmtstrcpy(fp, "\\n");
			break;
		case '\"':
		case '\\':
			fmtrune(fp, '\\');
			fmtrune(fp, r);
			break;
		}
1459
	}
1460
	return 0;
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
}

int
isnil(Node *n)
{
	if(n == N)
		return 0;
	if(n->op != OLITERAL)
		return 0;
	if(n->val.ctype != CTNIL)
		return 0;
	return 1;
}

int
isptrto(Type *t, int et)
{
	if(t == T)
		return 0;
	if(!isptr[t->etype])
		return 0;
	t = t->type;
	if(t == T)
		return 0;
	if(t->etype != et)
		return 0;
	return 1;
}

Russ Cox's avatar
Russ Cox committed
1490 1491 1492 1493 1494 1495
int
istype(Type *t, int et)
{
	return t != T && t->etype == et;
}

1496
int
1497
isfixedarray(Type *t)
1498
{
1499
	return t != T && t->etype == TARRAY && t->bound >= 0;
1500 1501 1502
}

int
1503
isslice(Type *t)
1504
{
1505
	return t != T && t->etype == TARRAY && t->bound < 0;
Ken Thompson's avatar
Ken Thompson committed
1506 1507
}

Ken Thompson's avatar
Ken Thompson committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
int
isselect(Node *n)
{
	Sym *s;

	if(n == N)
		return 0;
	n = n->left;
	s = pkglookup("selectsend", "sys");
	if(s == n->sym)
		return 1;
	s = pkglookup("selectrecv", "sys");
Ken Thompson's avatar
Ken Thompson committed
1520 1521 1522
	if(s == n->sym)
		return 1;
	s = pkglookup("selectdefault", "sys");
Ken Thompson's avatar
Ken Thompson committed
1523 1524 1525 1526 1527
	if(s == n->sym)
		return 1;
	return 0;
}

1528 1529 1530
int
isinter(Type *t)
{
Ken Thompson's avatar
Ken Thompson committed
1531 1532 1533 1534 1535 1536
	if(t != T) {
		if(t->etype == TINTER)
			return 1;
		if(t->etype == TDDD)
			return 1;
	}
1537 1538 1539
	return 0;
}

Ken Thompson's avatar
Ken Thompson committed
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
int
isnilinter(Type *t)
{
	if(!isinter(t))
		return 0;
	if(t->type != T)
		return 0;
	return 1;
}

Ken Thompson's avatar
Ken Thompson committed
1550 1551 1552 1553 1554 1555 1556 1557
int
isddd(Type *t)
{
	if(t != T && t->etype == TDDD)
		return 1;
	return 0;
}

1558 1559 1560 1561
/*
 * given receiver of type t (t == r or t == *r)
 * return type to hang methods off (r).
 */
1562
Type*
1563
methtype(Type *t)
1564
{
1565
	int ptr;
1566

1567 1568 1569
	if(t == T)
		return T;

1570 1571 1572 1573 1574 1575 1576 1577 1578
	// strip away pointer if it's there
	ptr = 0;
	if(isptr[t->etype]) {
		if(t->sym != S)
			return T;
		ptr = 1;
		t = t->type;
		if(t == T)
			return T;
1579 1580
	}

1581 1582
	// need a type name
	if(t->sym == S)
1583 1584
		return T;

1585 1586 1587
	// check types
	if(!issimple[t->etype])
	switch(t->etype) {
1588
	default:
1589 1590 1591
		return T;
	case TSTRUCT:
	case TARRAY:
1592 1593 1594
	case TMAP:
	case TCHAN:
	case TSTRING:
Russ Cox's avatar
Russ Cox committed
1595
	case TFUNC:
1596 1597 1598 1599
		break;
	}

	return t;
1600 1601
}

1602 1603 1604 1605 1606 1607 1608 1609
int
iscomposite(Type *t)
{
	if(t == T)
		return 0;
	switch(t->etype) {
	case TARRAY:
	case TSTRUCT:
Russ Cox's avatar
Russ Cox committed
1610
	case TMAP:
1611 1612 1613 1614 1615
		return 1;
	}
	return 0;
}

Russ Cox's avatar
Russ Cox committed
1616
Node*
Russ Cox's avatar
Russ Cox committed
1617
signame(Type *t)
Ken Thompson's avatar
Ken Thompson committed
1618
{
Russ Cox's avatar
Russ Cox committed
1619
	Sym *ss;
Ken Thompson's avatar
Ken Thompson committed
1620
	char *e;
Ken Thompson's avatar
Ken Thompson committed
1621
	Dcl *x;
Russ Cox's avatar
Russ Cox committed
1622
	char buf[NSYMB];
Ken Thompson's avatar
Ken Thompson committed
1623

Russ Cox's avatar
Russ Cox committed
1624
//print("signame %T\n", t);
Ken Thompson's avatar
Ken Thompson committed
1625
	if(t == T)
Ken Thompson's avatar
Ken Thompson committed
1626 1627
		goto bad;

Ken Thompson's avatar
Ken Thompson committed
1628
	e = "sigt";
1629
	if(t->etype == TINTER || t->etype == TDDD)
Ken Thompson's avatar
Ken Thompson committed
1630 1631
		e = "sigi";

Russ Cox's avatar
Russ Cox committed
1632 1633
	// name is exported name, like *[]byte or *Struct or Interface
	// (special symbols don't bother the linker).
Russ Cox's avatar
Russ Cox committed
1634
	snprint(buf, sizeof(buf), "%#T", t);
Russ Cox's avatar
Russ Cox committed
1635 1636 1637 1638 1639

	// special case: empty interface is named sigi.empty
	// so that it can be referred to by the runtime.
	if(strcmp(buf, "interface { }") == 0)
		strcpy(buf, "empty");
1640

1641 1642 1643
	// special case: sigi.... is just too hard to read in assembly.
	if(strcmp(buf, "...") == 0)
		strcpy(buf, "dotdotdot");
Russ Cox's avatar
Russ Cox committed
1644

Russ Cox's avatar
Russ Cox committed
1645
	ss = pkglookup(buf, e);
Russ Cox's avatar
Russ Cox committed
1646 1647 1648 1649
	if(ss->def == N) {
		ss->def = newname(ss);
		ss->def->type = types[TUINT8];
		ss->def->class = PEXTERN;
Ken Thompson's avatar
Ken Thompson committed
1650
	}
1651

Russ Cox's avatar
Russ Cox committed
1652
//print("siggen %T %d\n", t, t->siggen);
Russ Cox's avatar
Russ Cox committed
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	if(!t->siggen) {
		// special case: don't generate the empty interface
		if(strcmp(buf, "empty") == 0)
			goto out;

		// record internal type for signature generation
		x = mal(sizeof(*x));
		x->op = OTYPE;
		x->dtype = t;
		x->forw = signatlist;
		t->siggen = 1;
		signatlist = x;
	}

out:
Russ Cox's avatar
Russ Cox committed
1668
	return ss->def;
Ken Thompson's avatar
Ken Thompson committed
1669 1670

bad:
Russ Cox's avatar
Russ Cox committed
1671
	return N;
Ken Thompson's avatar
Ken Thompson committed
1672 1673
}

1674
int
Russ Cox's avatar
6g:  
Russ Cox committed
1675
eqtype1(Type *t1, Type *t2, int d, int names)
1676 1677 1678 1679 1680 1681 1682 1683 1684
{
	if(d >= 10)
		return 1;
	if(t1 == t2)
		return 1;
	if(t1 == T || t2 == T)
		return 0;
	if(t1->etype != t2->etype)
		return 0;
Russ Cox's avatar
6g:  
Russ Cox committed
1685 1686
	if(names && t1->etype != TFIELD && t1->sym && t2->sym && t1 != t2)
		return 0;
1687 1688 1689 1690 1691 1692
	switch(t1->etype) {
	case TINTER:
	case TSTRUCT:
		t1 = t1->type;
		t2 = t2->type;
		for(;;) {
Russ Cox's avatar
6g:  
Russ Cox committed
1693
			if(!eqtype1(t1, t2, d+1, names))
1694 1695 1696 1697 1698 1699
				return 0;
			if(t1 == T)
				return 1;
			if(t1->nname != N && t1->nname->sym != S) {
				if(t2->nname == N || t2->nname->sym == S)
					return 0;
1700 1701
				if(strcmp(t1->nname->sym->name, t2->nname->sym->name) != 0)
					return 0;
1702 1703 1704 1705 1706 1707 1708
			}
			t1 = t1->down;
			t2 = t2->down;
		}
		return 1;

	case TFUNC:
1709
		// Loop over structs: receiver, in, out.
1710 1711 1712
		t1 = t1->type;
		t2 = t2->type;
		for(;;) {
1713
			Type *ta, *tb;
1714 1715 1716 1717 1718 1719 1720
			if(t1 == t2)
				break;
			if(t1 == T || t2 == T)
				return 0;
			if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
				return 0;

1721 1722 1723 1724 1725 1726 1727 1728
			// Loop over fields in structs, checking type only.
			ta = t1->type;
			tb = t2->type;
			while(ta != tb) {
				if(ta == T || tb == T)
					return 0;
				if(ta->etype != TFIELD || tb->etype != TFIELD)
					return 0;
Russ Cox's avatar
6g:  
Russ Cox committed
1729
				if(!eqtype1(ta->type, tb->type, d+1, names))
1730 1731 1732 1733
					return 0;
				ta = ta->down;
				tb = tb->down;
			}
1734 1735 1736 1737 1738

			t1 = t1->down;
			t2 = t2->down;
		}
		return 1;
Ken Thompson's avatar
Ken Thompson committed
1739 1740 1741 1742 1743

	case TARRAY:
		if(t1->bound == t2->bound)
			break;
		return 0;
Russ Cox's avatar
Russ Cox committed
1744 1745 1746 1747 1748

	case TCHAN:
		if(t1->chan == t2->chan)
			break;
		return 0;
1749
	}
Russ Cox's avatar
6g:  
Russ Cox committed
1750
	return eqtype1(t1->type, t2->type, d+1, names);
1751 1752 1753 1754 1755
}

int
eqtype(Type *t1, Type *t2)
{
Russ Cox's avatar
6g:  
Russ Cox committed
1756 1757 1758
	return eqtype1(t1, t2, 0, 1);
}

1759 1760 1761 1762
/*
 * can we convert from type src to dst with
 * a trivial conversion (no bits changing)?
 */
Russ Cox's avatar
6g:  
Russ Cox committed
1763
int
1764
cvttype(Type *dst, Type *src)
Russ Cox's avatar
6g:  
Russ Cox committed
1765
{
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
	Sym *ds, *ss;
	int ret;

	if(eqtype1(dst, src, 0, 0))
		return 1;

	// Can convert if assignment compatible when
	// top-level names are ignored.
	ds = dst->sym;
	dst->sym = nil;
	ss = src->sym;
	src->sym = nil;
	ret = ascompat(dst, src);
	dst->sym = ds;
	src->sym = ss;
	return ret == 1;
1782 1783
}

Russ Cox's avatar
Russ Cox committed
1784 1785 1786 1787
int
eqtypenoname(Type *t1, Type *t2)
{
	if(t1 == T || t2 == T || t1->etype != TSTRUCT || t2->etype != TSTRUCT)
1788
		return eqtype(t1, t2);
Russ Cox's avatar
Russ Cox committed
1789 1790 1791 1792

	t1 = t1->type;
	t2 = t2->type;
	for(;;) {
1793
		if(!eqtype(t1, t2))
Russ Cox's avatar
Russ Cox committed
1794 1795 1796 1797 1798 1799 1800 1801
			return 0;
		if(t1 == T)
			return 1;
		t1 = t1->down;
		t2 = t2->down;
	}
}

1802
static int
Ken Thompson's avatar
Ken Thompson committed
1803
subtype(Type **stp, Type *t, int d)
1804 1805 1806 1807 1808 1809 1810
{
	Type *st;

loop:
	st = *stp;
	if(st == T)
		return 0;
Ken Thompson's avatar
Ken Thompson committed
1811 1812 1813 1814 1815

	d++;
	if(d >= 10)
		return 0;

1816 1817 1818 1819 1820 1821
	switch(st->etype) {
	default:
		return 0;

	case TPTR32:
	case TPTR64:
Ken Thompson's avatar
Ken Thompson committed
1822
	case TCHAN:
Ken Thompson's avatar
Ken Thompson committed
1823
	case TARRAY:
1824 1825 1826 1827
		stp = &st->type;
		goto loop;

	case TANY:
1828 1829
		if(!st->copyany)
			return 0;
1830 1831 1832 1833
		*stp = t;
		break;

	case TMAP:
Ken Thompson's avatar
Ken Thompson committed
1834
		if(subtype(&st->down, t, d))
1835 1836 1837 1838 1839 1840
			break;
		stp = &st->type;
		goto loop;

	case TFUNC:
		for(;;) {
Ken Thompson's avatar
Ken Thompson committed
1841
			if(subtype(&st->type, t, d))
1842
				break;
Ken Thompson's avatar
Ken Thompson committed
1843
			if(subtype(&st->type->down->down, t, d))
1844
				break;
Ken Thompson's avatar
Ken Thompson committed
1845
			if(subtype(&st->type->down, t, d))
1846 1847 1848 1849 1850 1851 1852
				break;
			return 0;
		}
		break;

	case TSTRUCT:
		for(st=st->type; st!=T; st=st->down)
Ken Thompson's avatar
Ken Thompson committed
1853
			if(subtype(&st->type, t, d))
1854 1855 1856 1857 1858 1859
				return 1;
		return 0;
	}
	return 1;
}

1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
/*
 * Is this a 64-bit type?
 */
int
is64(Type *t)
{
	if(t == T)
		return 0;
	switch(simtype[t->etype]) {
	case TINT64:
	case TUINT64:
	case TPTR64:
		return 1;
	}
	return 0;
}

/*
 * Is a conversion between t1 and t2 a no-op?
 */
int
noconv(Type *t1, Type *t2)
{
	int e1, e2;

	e1 = simtype[t1->etype];
	e2 = simtype[t2->etype];

	switch(e1) {
	case TINT8:
	case TUINT8:
		return e2 == TINT8 || e2 == TUINT8;

	case TINT16:
	case TUINT16:
		return e2 == TINT16 || e2 == TUINT16;

	case TINT32:
	case TUINT32:
	case TPTR32:
		return e2 == TINT32 || e2 == TUINT32 || e2 == TPTR32;

	case TINT64:
	case TUINT64:
	case TPTR64:
		return e2 == TINT64 || e2 == TUINT64 || e2 == TPTR64;

	case TFLOAT32:
		return e2 == TFLOAT32;

	case TFLOAT64:
		return e2 == TFLOAT64;
	}
	return 0;
}

1916 1917 1918
void
argtype(Node *on, Type *t)
{
Ken Thompson's avatar
Ken Thompson committed
1919
	if(!subtype(&on->type, t, 0))
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
		fatal("argtype: failed %N %T\n", on, t);
}

Type*
shallow(Type *t)
{
	Type *nt;

	if(t == T)
		return T;
	nt = typ(0);
	*nt = *t;
	return nt;
}

Type*
deep(Type *t)
{
	Type *nt, *xt;

	if(t == T)
		return T;

	switch(t->etype) {
	default:
		nt = t;	// share from here down
		break;

1948 1949 1950 1951 1952
	case TANY:
		nt = shallow(t);
		nt->copyany = 1;
		break;

1953 1954
	case TPTR32:
	case TPTR64:
Ken Thompson's avatar
chan  
Ken Thompson committed
1955
	case TCHAN:
Ken Thompson's avatar
Ken Thompson committed
1956
	case TARRAY:
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
		nt = shallow(t);
		nt->type = deep(t->type);
		break;

	case TMAP:
		nt = shallow(t);
		nt->down = deep(t->down);
		nt->type = deep(t->type);
		break;

	case TFUNC:
		nt = shallow(t);
		nt->type = deep(t->type);
		nt->type->down = deep(t->type->down);
		nt->type->down->down = deep(t->type->down->down);
		break;

	case TSTRUCT:
		nt = shallow(t);
		nt->type = shallow(t->type);
		xt = nt->type;

		for(t=t->type; t!=T; t=t->down) {
			xt->type = deep(t->type);
			xt->down = shallow(t->down);
			xt = xt->down;
		}
		break;
	}
	return nt;
}

Node*
syslook(char *name, int copy)
{
	Sym *s;
	Node *n;

	s = pkglookup(name, "sys");
Russ Cox's avatar
Russ Cox committed
1996
	if(s == S || s->def == N)
1997 1998 1999
		fatal("looksys: cant find sys.%s", name);

	if(!copy)
Russ Cox's avatar
Russ Cox committed
2000
		return s->def;
2001 2002

	n = nod(0, N, N);
Russ Cox's avatar
Russ Cox committed
2003 2004
	*n = *s->def;
	n->type = deep(s->def->type);
2005 2006 2007 2008

	return n;
}

2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
/*
 * are the arg names of two
 * functions the same. we know
 * that eqtype has been called
 * and has returned true.
 */
int
eqargs(Type *t1, Type *t2)
{
	if(t1 == t2)
		return 1;
	if(t1 == T || t2 == T)
		return 0;

	if(t1->etype != t2->etype)
		return 0;

	if(t1->etype != TFUNC)
		fatal("eqargs: oops %E", t1->etype);

	t1 = t1->type;
	t2 = t2->type;
	for(;;) {
		if(t1 == t2)
			break;
2034
		if(!eqtype(t1, t2))
2035 2036 2037 2038 2039 2040 2041
			return 0;
		t1 = t1->down;
		t2 = t2->down;
	}
	return 1;
}

2042
uint32
2043
typehash(Type *at, int addsym, int d)
2044
{
2045
	uint32 h;
2046 2047 2048 2049 2050 2051 2052 2053 2054
	Type *t;

	if(at == T)
		return PRIME2;
	if(d >= 5)
		return PRIME3;

	h = at->etype*PRIME4;

2055 2056 2057
	if(addsym && at->sym != S)
		h += stringhash(at->sym->name);

2058 2059
	switch(at->etype) {
	default:
2060
		h += PRIME5 * typehash(at->type, addsym, d+1);
2061 2062 2063 2064 2065
		break;

	case TINTER:
		// botch -- should be sorted?
		for(t=at->type; t!=T; t=t->down)
2066
			h += PRIME6 * typehash(t, addsym, d+1);
2067 2068 2069 2070
		break;

	case TSTRUCT:
		for(t=at->type; t!=T; t=t->down)
2071
			h += PRIME7 * typehash(t, addsym, d+1);
2072 2073 2074 2075
		break;

	case TFUNC:
		t = at->type;
Russ Cox's avatar
Russ Cox committed
2076
		// skip this (receiver) argument
2077 2078 2079
		if(t != T)
			t = t->down;
		for(; t!=T; t=t->down)
2080
			h += PRIME7 * typehash(t, addsym, d+1);
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
		break;
	}

	return h;
}

Type*
ptrto(Type *t)
{
	Type *t1;

	if(tptr == 0)
		fatal("ptrto: nil");
	t1 = typ(tptr);
	t1->type = t;
Ken Thompson's avatar
Ken Thompson committed
2096
	t1->width = types[tptr]->width;
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
	return t1;
}

void
frame(int context)
{
	char *p;
	Dcl *d;
	int flag;

	p = "stack";
	d = autodcl;
	if(context) {
		p = "external";
		d = externdcl;
	}

	flag = 1;
	for(; d!=D; d=d->forw) {
		switch(d->op) {
		case ONAME:
			if(flag)
				print("--- %s frame ---\n", p);
			print("%O %S G%ld T\n", d->op, d->dsym, d->dnode->vargen, d->dnode->type);
			flag = 0;
			break;

		case OTYPE:
			if(flag)
				print("--- %s frame ---\n", p);
Russ Cox's avatar
Russ Cox committed
2127
			print("%O %T\n", d->op, d->dnode);
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
			flag = 0;
			break;
		}
	}
}

/*
 * calculate sethi/ullman number
 * roughly how many registers needed to
 * compile a node. used to compile the
 * hardest side first to minimize registers.
 */
void
ullmancalc(Node *n)
{
	int ul, ur;

	if(n == N)
		return;

	switch(n->op) {
2149
	case OREGISTER:
2150 2151
	case OLITERAL:
	case ONAME:
2152
		ul = 1;
Russ Cox's avatar
Russ Cox committed
2153 2154
		if(n->class == PPARAMREF || (n->class & PHEAP))
			ul++;
2155 2156
		goto out;
	case OCALL:
Ken Thompson's avatar
Ken Thompson committed
2157 2158
	case OCALLMETH:
	case OCALLINTER:
2159 2160 2161
		ul = UINF;
		goto out;
	}
2162
	ul = 1;
2163 2164
	if(n->left != N)
		ul = n->left->ullman;
2165
	ur = 1;
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
	if(n->right != N)
		ur = n->right->ullman;
	if(ul == ur)
		ul += 1;
	if(ur > ul)
		ul = ur;

out:
	n->ullman = ul;
}

void
badtype(int o, Type *tl, Type *tr)
{
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192

loop:
	switch(o) {
	case OCALL:
		if(tl == T || tr == T)
			break;
		if(isptr[tl->etype] && isptr[tr->etype]) {
			tl = tl->type;
			tr = tr->type;
			goto loop;
		}
		if(tl->etype != TFUNC || tr->etype != TFUNC)
			break;
2193
//		if(eqtype(t1, t2))
2194 2195 2196
	}

	yyerror("illegal types for operand: %O", o);
2197
	if(tl != T)
Russ Cox's avatar
Russ Cox committed
2198
		print("	%T\n", tl);
2199
	if(tr != T)
Russ Cox's avatar
Russ Cox committed
2200
		print("	%T\n", tr);
Russ Cox's avatar
Russ Cox committed
2201 2202 2203 2204 2205 2206 2207 2208

	// common mistake: *struct and *interface.
	if(tl && tr && isptr[tl->etype] && isptr[tr->etype]) {
		if(tl->type->etype == TSTRUCT && tr->type->etype == TINTER)
			print("	(*struct vs *interface)\n");
		else if(tl->type->etype == TINTER && tr->type->etype == TSTRUCT)
			print("	(*interface vs *struct)\n");
	}
2209 2210 2211
}

/*
2212 2213 2214
 * this routine gets called to propagate the type
 * of the last decl up to the arguments before it.
 * (a,b,c int) comes out (a int, b int, c int).
2215 2216
 */
Node*
2217
cleanidlist(Node *na)
2218
{
2219
	Node *last, *n;
2220

2221 2222
	if(na->op != OLIST)
		return na;
2223

2224 2225 2226 2227 2228 2229
	for(last=na; last->op == OLIST; last=last->right)
		;
	if(last->op != ODCLFIELD)
		fatal("cleanidlist: %O", last->op);
	if(last->type == T)
		fatal("cleanidlist: no type");
2230

Russ Cox's avatar
Russ Cox committed
2231
	for(n=na; n->op == OLIST; n=n->right) {
2232
		n->left->type = last->type;
Russ Cox's avatar
Russ Cox committed
2233 2234
		n->left->val = last->val;
	}
2235
	return na;
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
}

/*
 * iterator to walk a structure declaration
 */
Type*
structfirst(Iter *s, Type **nn)
{
	Type *n, *t;

	n = *nn;
	if(n == T)
		goto bad;

	switch(n->etype) {
	default:
		goto bad;

	case TSTRUCT:
	case TINTER:
	case TFUNC:
		break;
	}

	t = n->type;
	if(t == T)
		goto rnil;

	if(t->etype != TFIELD)
		fatal("structfirst: not field %T", t);

	s->t = t;
	return t;

bad:
	fatal("structfirst: not struct %T", n);

rnil:
	return T;
}

Type*
structnext(Iter *s)
{
	Type *n, *t;

	n = s->t;
	t = n->down;
	if(t == T)
		goto rnil;

	if(t->etype != TFIELD)
		goto bad;

	s->t = t;
	return t;

bad:
	fatal("structnext: not struct %T", n);

rnil:
	return T;
}

/*
 * iterator to this and inargs in a function
 */
Type*
funcfirst(Iter *s, Type *t)
{
	Type *fp;

	if(t == T)
		goto bad;

	if(t->etype != TFUNC)
		goto bad;

	s->tfunc = t;
	s->done = 0;
	fp = structfirst(s, getthis(t));
	if(fp == T) {
		s->done = 1;
		fp = structfirst(s, getinarg(t));
	}
	return fp;

bad:
	fatal("funcfirst: not func %T", t);
	return T;
}

Type*
funcnext(Iter *s)
{
	Type *fp;

	fp = structnext(s);
	if(fp == T && !s->done) {
		s->done = 1;
		fp = structfirst(s, getinarg(s->tfunc));
	}
	return fp;
}

/*
 * iterator to walk a list
 */
Node*
listfirst(Iter *s, Node **nn)
{
	Node *n;

	n = *nn;
	if(n == N) {
		s->done = 1;
		s->an = &s->n;
		s->n = N;
		return N;
	}

	if(n->op == OLIST) {
		s->done = 0;
		s->n = n;
		s->an = &n->left;
		return n->left;
	}

	s->done = 1;
	s->an = nn;
	return n;
}

Node*
listnext(Iter *s)
{
	Node *n, *r;

	if(s->done) {
		s->an = &s->n;
		s->n = N;
		return N;
	}

	n = s->n;
	r = n->right;
2382 2383 2384 2385 2386
	if(r == N) {
		s->an = &s->n;
		s->n = N;
		return N;
	}
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
	if(r->op == OLIST) {
		s->n = r;
		s->an = &r->left;
		return r->left;
	}

	s->done = 1;
	s->an = &n->right;
	return n->right;
}

Type**
getthis(Type *t)
{
	if(t->etype != TFUNC)
2402
		fatal("getthis: not a func %T", t);
2403 2404 2405 2406 2407 2408 2409
	return &t->type;
}

Type**
getoutarg(Type *t)
{
	if(t->etype != TFUNC)
2410
		fatal("getoutarg: not a func %T", t);
2411 2412 2413 2414 2415 2416 2417
	return &t->type->down;
}

Type**
getinarg(Type *t)
{
	if(t->etype != TFUNC)
2418
		fatal("getinarg: not a func %T", t);
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
	return &t->type->down->down;
}

Type*
getthisx(Type *t)
{
	return *getthis(t);
}

Type*
getoutargx(Type *t)
{
	return *getoutarg(t);
}

Type*
getinargx(Type *t)
{
	return *getinarg(t);
}
2439

2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
/*
 * return !(op)
 * eg == <=> !=
 */
int
brcom(int a)
{
	switch(a) {
	case OEQ:	return ONE;
	case ONE:	return OEQ;
	case OLT:	return OGE;
	case OGT:	return OLE;
	case OLE:	return OGT;
	case OGE:	return OLT;
	}
	fatal("brcom: no com for %A\n", a);
	return a;
}

/*
 * return reverse(op)
 * eg a op b <=> b r(op) a
 */
int
brrev(int a)
{
	switch(a) {
	case OEQ:	return OEQ;
	case ONE:	return ONE;
	case OLT:	return OGT;
	case OGT:	return OLT;
	case OLE:	return OGE;
	case OGE:	return OLE;
	}
	fatal("brcom: no rev for %A\n", a);
	return a;
}

/*
 * make a new off the books
 */
void
tempname(Node *n, Type *t)
{
	Sym *s;
	uint32 w;

	if(t == T) {
		yyerror("tempname called with nil type");
		t = types[TINT32];
	}

2492 2493 2494
	// give each tmp a different name so that there
	// a chance to registerizer them
	snprint(namebuf, sizeof(namebuf), "autotmp_%.4d", statuniqgen);
Ken Thompson's avatar
Ken Thompson committed
2495
	statuniqgen++;
2496
	s = lookup(namebuf);
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514

	memset(n, 0, sizeof(*n));
	n->op = ONAME;
	n->sym = s;
	n->type = t;
	n->etype = t->etype;
	n->class = PAUTO;
	n->addable = 1;
	n->ullman = 1;
	n->noescape = 1;

	dowidth(t);
	w = t->width;
	stksize += w;
	stksize = rnd(stksize, w);
	n->xoffset = -stksize;
}

2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
Node*
staticname(Type *t)
{
	Node *n;

	snprint(namebuf, sizeof(namebuf), "statictmp_%.4d·%s", statuniqgen, filename);
	statuniqgen++;
	n = newname(lookup(namebuf));
	addvar(n, t, PEXTERN);
	return n;
}

2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
/*
 * return side effect-free n, moving side effects to top.
 */
Node*
saferef(Node *n)
{
	Node *l;
	Node *r;

	switch(n->op) {
	case ONAME:
		return n;
	case ODOT:
		l = saferef(n->left);
		if(l == n->left)
			return n;
		r = nod(OXXX, N, N);
		*r = *n;
		r->left = l;
		walktype(r, Elv);
		return r;

	case OINDEX:
	case ODOTPTR:
	case OIND:
		l = nod(OXXX, N, N);
		tempname(l, ptrto(n->type));
Russ Cox's avatar
Russ Cox committed
2554
		addtop = list(addtop, nod(OAS, l, nod(OADDR, n, N)));
2555 2556 2557 2558 2559 2560 2561 2562
		r = nod(OIND, l, N);
		walktype(r, Elv);
		return r;
	}
	fatal("saferef %N", n);
	return N;
}

2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
void
setmaxarg(Type *t)
{
	int32 w;

	w = t->argwid;
	if(w > maxarg)
		maxarg = w;
}

/*
 * gather series of offsets
 * >=0 is direct addressed field
 * <0 is pointer to next field (+1)
 */
int
dotoffset(Node *n, int *oary, Node **nn)
{
	int i;

	switch(n->op) {
	case ODOT:
		if(n->xoffset == BADWIDTH) {
			dump("bad width in dotoffset", n);
			fatal("bad width in dotoffset");
		}
		i = dotoffset(n->left, oary, nn);
		if(i > 0) {
			if(oary[i-1] >= 0)
				oary[i-1] += n->xoffset;
			else
				oary[i-1] -= n->xoffset;
			break;
		}
		if(i < 10)
			oary[i++] = n->xoffset;
		break;

	case ODOTPTR:
		if(n->xoffset == BADWIDTH) {
			dump("bad width in dotoffset", n);
			fatal("bad width in dotoffset");
		}
		i = dotoffset(n->left, oary, nn);
		if(i < 10)
			oary[i++] = -(n->xoffset+1);
		break;

	default:
		*nn = n;
		return 0;
	}
	if(i >= 10)
		*nn = N;
	return i;
}

2620 2621 2622 2623 2624 2625 2626 2627 2628
/*
 * code to resolve elided DOTs
 * in embedded types
 */

// search depth 0 --
// return count of fields+methods
// found with a given name
int
Russ Cox's avatar
Russ Cox committed
2629
lookdot0(Sym *s, Type *t, Type **save)
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
{
	Type *f, *u;
	int c;

	u = t;
	if(isptr[u->etype])
		u = u->type;

	c = 0;
	if(u->etype == TSTRUCT || u->etype == TINTER) {
		for(f=u->type; f!=T; f=f->down)
Russ Cox's avatar
Russ Cox committed
2641 2642 2643
			if(f->sym == s) {
				if(save)
					*save = f;
2644
				c++;
Russ Cox's avatar
Russ Cox committed
2645
			}
2646 2647 2648 2649
	}
	u = methtype(t);
	if(u != T) {
		for(f=u->method; f!=T; f=f->down)
Russ Cox's avatar
Russ Cox committed
2650 2651 2652
			if(f->sym == s && f->embedded == 0) {
				if(save)
					*save = f;
2653
				c++;
Russ Cox's avatar
Russ Cox committed
2654
			}
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
	}
	return c;
}

// search depth d --
// return count of fields+methods
// found at search depth.
// answer is in dotlist array and
// count of number of ways is returned.
int
Russ Cox's avatar
Russ Cox committed
2665
adddot1(Sym *s, Type *t, int d, Type **save)
2666 2667 2668 2669 2670 2671 2672 2673 2674
{
	Type *f, *u;
	int c, a;

	if(t->trecur)
		return 0;
	t->trecur = 1;

	if(d == 0) {
Russ Cox's avatar
Russ Cox committed
2675
		c = lookdot0(s, t, save);
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
		goto out;
	}

	c = 0;
	u = t;
	if(isptr[u->etype])
		u = u->type;
	if(u->etype != TSTRUCT && u->etype != TINTER)
		goto out;

	d--;
	for(f=u->type; f!=T; f=f->down) {
		if(!f->embedded)
			continue;
		if(f->sym == S)
			continue;
Russ Cox's avatar
Russ Cox committed
2692
		a = adddot1(s, f->type, d, save);
Ken Thompson's avatar
Ken Thompson committed
2693 2694
		if(a != 0 && c == 0)
			dotlist[d].field = f;
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
		c += a;
	}

out:
	t->trecur = 0;
	return c;
}

// in T.field
// find missing fields that
// will give shortest unique addressing.
// modify the tree with missing type names.
Node*
adddot(Node *n)
{
	Type *t;
	Sym *s;
	int c, d;

	walktype(n->left, Erv);
	t = n->left->type;
	if(t == T)
2717
		goto ret;
2718 2719

	if(n->right->op != ONAME)
2720
		goto ret;
2721 2722
	s = n->right->sym;
	if(s == S)
2723
		goto ret;
2724 2725

	for(d=0; d<nelem(dotlist); d++) {
Russ Cox's avatar
Russ Cox committed
2726
		c = adddot1(s, t, d, nil);
2727 2728 2729
		if(c > 0)
			goto out;
	}
2730
	goto ret;
2731 2732 2733

out:
	if(c > 1)
Russ Cox's avatar
Russ Cox committed
2734
		yyerror("ambiguous DOT reference %T.%S", t, s);
2735 2736 2737 2738

	// rebuild elided dots
	for(c=d-1; c>=0; c--) {
		n = nod(ODOT, n, n->right);
Ken Thompson's avatar
Ken Thompson committed
2739
		n->left->right = newname(dotlist[c].field->sym);
2740
	}
2741 2742 2743
ret:
	n->ninit = list(addtop, n->ninit);
	addtop = N;
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
	return n;
}


/*
 * code to help generate trampoline
 * functions for methods on embedded
 * subtypes.
 * these are approx the same as
 * the corresponding adddot routines
 * except that they expect to be called
 * with unique tasks and they return
 * the actual methods.
 */

typedef	struct	Symlink	Symlink;
struct	Symlink
{
	Type*		field;
	uchar		good;
2764
	uchar		followptr;
2765 2766 2767 2768
	Symlink*	link;
};
static	Symlink*	slist;

2769 2770
static void
expand0(Type *t, int followptr)
2771 2772 2773 2774 2775
{
	Type *f, *u;
	Symlink *sl;

	u = t;
2776 2777
	if(isptr[u->etype]) {
		followptr = 1;
2778
		u = u->type;
2779
	}
2780

2781 2782
	if(u->etype == TINTER) {
		for(f=u->type; f!=T; f=f->down) {
Russ Cox's avatar
6g:  
Russ Cox committed
2783
			if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0)
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
				continue;
			if(f->sym->uniq)
				continue;
			f->sym->uniq = 1;
			sl = mal(sizeof(*sl));
			sl->field = f;
			sl->link = slist;
			sl->followptr = followptr;
			slist = sl;
		}
		return;
	}

2797 2798 2799
	u = methtype(t);
	if(u != T) {
		for(f=u->method; f!=T; f=f->down) {
Russ Cox's avatar
6g:  
Russ Cox committed
2800
			if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0)
2801
				continue;
2802 2803 2804 2805 2806 2807
			if(f->sym->uniq)
				continue;
			f->sym->uniq = 1;
			sl = mal(sizeof(*sl));
			sl->field = f;
			sl->link = slist;
2808
			sl->followptr = followptr;
2809 2810 2811 2812 2813
			slist = sl;
		}
	}
}

2814 2815
static void
expand1(Type *t, int d, int followptr)
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
{
	Type *f, *u;

	if(t->trecur)
		return;
	if(d == 0)
		return;
	t->trecur = 1;

	if(d != nelem(dotlist)-1)
2826
		expand0(t, followptr);
2827 2828

	u = t;
2829 2830
	if(isptr[u->etype]) {
		followptr = 1;
2831
		u = u->type;
2832
	}
2833 2834 2835 2836 2837 2838 2839 2840
	if(u->etype != TSTRUCT && u->etype != TINTER)
		goto out;

	for(f=u->type; f!=T; f=f->down) {
		if(!f->embedded)
			continue;
		if(f->sym == S)
			continue;
2841
		expand1(f->type, d-1, followptr);
2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861
	}

out:
	t->trecur = 0;
}

void
expandmeth(Sym *s, Type *t)
{
	Symlink *sl;
	Type *f;
	int c, d;

	if(s == S)
		return;
	if(t == T)
		return;

	// generate all reachable methods
	slist = nil;
2862
	expand1(t, nelem(dotlist)-1, 0);
2863 2864 2865

	// check each method to be uniquely reachable
	for(sl=slist; sl!=nil; sl=sl->link) {
Ken Thompson's avatar
bug  
Ken Thompson committed
2866
		sl->field->sym->uniq = 0;
2867
		for(d=0; d<nelem(dotlist); d++) {
Russ Cox's avatar
Russ Cox committed
2868
			c = adddot1(sl->field->sym, t, d, &f);
2869 2870
			if(c == 0)
				continue;
Russ Cox's avatar
Russ Cox committed
2871
			if(c == 1) {
2872
				sl->good = 1;
Russ Cox's avatar
Russ Cox committed
2873 2874
				sl->field = f;
			}
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
			break;
		}
	}

	for(sl=slist; sl!=nil; sl=sl->link) {
		if(sl->good) {
			// add it to the base type method list
			f = typ(TFIELD);
			*f = *sl->field;
			f->embedded = 1;	// needs a trampoline
2885 2886
			if(sl->followptr)
				f->embedded = 2;
2887 2888 2889 2890 2891 2892
			f->down = t->method;
			t->method = f;

		}
	}
}
Russ Cox's avatar
Russ Cox committed
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923

/*
 * Given funarg struct list, return list of ODCLFIELD Node fn args.
 */
Node*
structargs(Type **tl, int mustname)
{
	Iter savet;
	Node *args, *a;
	Type *t;
	char nam[100];
	int n;

	args = N;
	n = 0;
	for(t = structfirst(&savet, tl); t != T; t = structnext(&savet)) {
		if(t->sym)
			a = nametodcl(newname(t->sym), t->type);
		else if(mustname) {
			// have to give it a name so we can refer to it in trampoline
			snprint(nam, sizeof nam, ".anon%d", n++);
			a = nametodcl(newname(lookup(nam)), t->type);
		} else
			a = anondcl(t->type);
		args = list(args, a);
	}
	args = rev(args);
	return args;
}

/*
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
 * Generate a wrapper function to convert from
 * a receiver of type T to a receiver of type U.
 * That is,
 *
 *	func (t T) M() {
 *		...
 *	}
 *
 * already exists; this function generates
 *
 *	func (u U) M() {
 *		u.M()
 *	}
 *
 * where the types T and U are such that u.M() is valid
 * and calls the T.M method.
 * The resulting function is for use in method tables.
Russ Cox's avatar
Russ Cox committed
2941
 *
Russ Cox's avatar
Russ Cox committed
2942
 *	rcvr - U
2943 2944
 *	method - M func (t T)(), a TFIELD type struct
 *	newnam - the eventual mangled name of this function
Russ Cox's avatar
Russ Cox committed
2945 2946
 */
void
Russ Cox's avatar
Russ Cox committed
2947
genwrapper(Type *rcvr, Type *method, Sym *newnam)
Russ Cox's avatar
Russ Cox committed
2948
{
2949 2950
	Node *this, *in, *out, *fn, *args, *call;
	Node *l;
Russ Cox's avatar
Russ Cox committed
2951 2952
	Iter savel;

Russ Cox's avatar
Russ Cox committed
2953
	if(debug['r'])
2954
		print("genwrapper rcvrtype=%T method=%T newnam=%S\n",
Russ Cox's avatar
Russ Cox committed
2955
			rcvr, method, newnam);
Russ Cox's avatar
Russ Cox committed
2956 2957 2958 2959

	dclcontext = PEXTERN;
	markdcl();

Russ Cox's avatar
Russ Cox committed
2960
	this = nametodcl(newname(lookup(".this")), rcvr);
2961 2962
	in = structargs(getinarg(method->type), 1);
	out = structargs(getoutarg(method->type), 0);
Russ Cox's avatar
Russ Cox committed
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974

	fn = nod(ODCLFUNC, N, N);
	fn->nname = newname(newnam);
	fn->type = functype(this, in, out);
	funchdr(fn);

	// arg list
	args = N;
	for(l = listfirst(&savel, &in); l; l = listnext(&savel))
		args = list(args, l->left);
	args = rev(args);

2975 2976
	// generate call
	call = nod(OCALL, adddot(nod(ODOT, this->left, newname(method->sym))), args);
Russ Cox's avatar
Russ Cox committed
2977
	fn->nbody = call;
2978
	if(method->type->outtuple > 0)
Russ Cox's avatar
Russ Cox committed
2979 2980 2981
		fn->nbody = nod(ORETURN, call, N);

	if(debug['r'])
Russ Cox's avatar
Russ Cox committed
2982
		dump("genwrapper body", fn->nbody);
Russ Cox's avatar
Russ Cox committed
2983 2984 2985 2986

	funcbody(fn);
}

2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
/*
 * delayed interface type check.
 * remember that there is an interface conversion
 * on the given line.  once the file is completely read
 * and all methods are known, we can check that
 * the conversions are valid.
 */

typedef struct Icheck Icheck;
struct Icheck
{
	Icheck *next;
	Type *dst;
	Type *src;
	int lineno;
3002
	int explicit;
3003 3004 3005 3006 3007
};
Icheck *icheck;
Icheck *ichecktail;

void
3008
ifacecheck(Type *dst, Type *src, int lineno, int explicit)
3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
{
	Icheck *p;

	p = mal(sizeof *p);
	if(ichecktail)
		ichecktail->next = p;
	else
		icheck = p;
	p->dst = dst;
	p->src = src;
	p->lineno = lineno;
3020
	p->explicit = explicit;
3021 3022 3023 3024
	ichecktail = p;
}

Type*
3025
ifacelookdot(Sym *s, Type *t, int *followptr)
3026
{
3027
	int i, c, d;
3028 3029
	Type *m;

3030 3031
	*followptr = 0;

3032 3033 3034
	if(t == T)
		return T;

3035 3036 3037 3038 3039 3040
	for(d=0; d<nelem(dotlist); d++) {
		c = adddot1(s, t, d, &m);
		if(c > 1) {
			yyerror("%T.%S is ambiguous", t, s);
			return T;
		}
3041 3042 3043 3044 3045 3046 3047
		if(c == 1) {
			for(i=0; i<d; i++) {
				if(isptr[dotlist[i].field->type->etype]) {
					*followptr = 1;
					break;
				}
			}
3048
			return m;
3049
		}
3050 3051 3052 3053
	}
	return T;
}

3054 3055
// check whether non-interface type t
// satisifes inteface type iface.
3056
int
3057
ifaceokT2I(Type *t0, Type *iface, Type **m)
3058
{
3059 3060
	Type *t, *im, *tm, *rcvr;
	int imhash, followptr;
3061

3062 3063
	t = methtype(t0);

3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075
	// if this is too slow,
	// could sort these first
	// and then do one loop.

	// could also do full type compare
	// instead of using hash, but have to
	// avoid checking receivers, and
	// typehash already does that for us.
	// also, it's what the runtime will do,
	// so we can both be wrong together.

	for(im=iface->type; im; im=im->down) {
3076
		imhash = typehash(im, 0, 0);
3077
		tm = ifacelookdot(im->sym, t, &followptr);
3078
		if(tm == T || typehash(tm, 0, 0) != imhash) {
3079 3080 3081
			*m = im;
			return 0;
		}
3082 3083 3084
		// if pointer receiver in method,
		// the method does not exist for value types.
		rcvr = getthisx(tm->type)->type->type;
3085
		if(isptr[rcvr->etype] && !isptr[t0->etype] && !followptr && !isifacemethod(tm)) {
3086 3087 3088 3089 3090
			if(debug['r'])
				yyerror("interface pointer mismatch");
			*m = im;
			return 0;
		}
3091 3092 3093 3094
	}
	return 1;
}

3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
// check whether interface type i1 satisifes interface type i2.
int
ifaceokI2I(Type *i1, Type *i2, Type **m)
{
	Type *m1, *m2;

	// if this is too slow,
	// could sort these first
	// and then do one loop.

	for(m2=i2->type; m2; m2=m2->down) {
		for(m1=i1->type; m1; m1=m1->down)
3107
			if(m1->sym == m2->sym && typehash(m1, 0, 0) == typehash(m2, 0, 0))
3108 3109 3110 3111 3112 3113 3114 3115
				goto found;
		*m = m2;
		return 0;
	found:;
	}
	return 1;
}

3116 3117 3118 3119
void
runifacechecks(void)
{
	Icheck *p;
3120 3121
	int lno, wrong, needexplicit;
	Type *m, *t, *iface;
3122 3123 3124 3125

	lno = lineno;
	for(p=icheck; p; p=p->next) {
		lineno = p->lineno;
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137
		wrong = 0;
		needexplicit = 0;
		m = nil;
		if(isinter(p->dst) && isinter(p->src)) {
			iface = p->dst;
			t = p->src;
			needexplicit = !ifaceokI2I(t, iface, &m);
		}
		else if(isinter(p->dst)) {
			t = p->src;
			iface = p->dst;
			wrong = !ifaceokT2I(t, iface, &m);
3138
		} else {
3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
			t = p->dst;
			iface = p->src;
			wrong = !ifaceokT2I(t, iface, &m);
			needexplicit = 1;
		}
		if(wrong)
			yyerror("%T is not %T\n\tmissing %S%hhT",
				t, iface, m->sym, m->type);
		else if(!p->explicit && needexplicit) {
			if(m)
3149
				yyerror("need type assertion to use %T as %T\n\tmissing %S%hhT",
3150 3151
					p->src, p->dst, m->sym, m->type);
			else
3152
				yyerror("need type assertion to use %T as %T",
3153
					p->src, p->dst);
3154 3155 3156 3157
		}
	}
	lineno = lno;
}
Russ Cox's avatar
Russ Cox committed
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168

/*
 * even simpler simtype; get rid of ptr, bool.
 * assuming that the front end has rejected
 * all the invalid conversions (like ptr -> bool)
 */
int
simsimtype(Type *t)
{
	int et;

Russ Cox's avatar
Russ Cox committed
3169 3170 3171
	if(t == 0)
		return 0;

Russ Cox's avatar
Russ Cox committed
3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
	et = simtype[t->etype];
	switch(et) {
	case TPTR32:
		et = TUINT32;
		break;
	case TPTR64:
		et = TUINT64;
		break;
	case TBOOL:
		et = TUINT8;
		break;
	}
	return et;
}