export.c 11 KB
Newer Older
1 2 3 4 5 6 7 8
// 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
9
exportsym(Sym *s)
10 11 12
{
	Dcl *d, *r;

13
	if(s == S)
14 15 16 17 18 19 20 21
		return;
	if(s->export != 0)
		return;
	s->export = 1;

	d = mal(sizeof(*d));
	d->dsym = s;
	d->dnode = N;
22
	d->lineno = lineno;
23 24 25 26 27 28 29 30 31 32 33 34 35

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

void
reexport(Type *t)
{
	Sym *s;

	if(t == T)
Ken Thompson's avatar
Ken Thompson committed
36
		fatal("reexport: type nil");
37 38 39 40

	s = t->sym;
	if(s == S/* || s->name[0] == '_'*/) {
		exportgen++;
Ken Thompson's avatar
Ken Thompson committed
41
		snprint(namebuf, sizeof(namebuf), "_e%s_%.3ld", filename, exportgen);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
		s = lookup(namebuf);
		s->lexical = LATYPE;
		s->otype = t;
		t->sym = s;
	}
	dumpexporttype(s);
}

void
dumpexportconst(Sym *s)
{
	Node *n;
	Type *t;

	if(s->exported != 0)
		return;
	s->exported = 1;

	n = s->oconst;
	if(n == N || n->op != OLITERAL)
Ken Thompson's avatar
Ken Thompson committed
62
		fatal("dumpexportconst: oconst nil: %S", s);
63 64 65 66 67 68 69 70 71 72 73 74 75 76

	t = n->type;	// may or may not be specified
	if(t != T)
		reexport(t);

	Bprint(bout, "\tconst ");
	if(s->export != 0)
		Bprint(bout, "!");
	Bprint(bout, "%lS ", s);
	if(t != T)
		Bprint(bout, "%lS ", t->sym);

	switch(n->val.ctype) {
	default:
Ken Thompson's avatar
Ken Thompson committed
77
		fatal("dumpexportconst: unknown ctype: %S", s);
78 79 80
	case CTINT:
	case CTSINT:
	case CTUINT:
Ken Thompson's avatar
Ken Thompson committed
81
		Bprint(bout, "%B\n", n->val.u.xval);
Ken Thompson's avatar
Ken Thompson committed
82
		break;
83
	case CTBOOL:
Ken Thompson's avatar
Ken Thompson committed
84
		Bprint(bout, "0x%llux\n", n->val.u.bval);
85 86
		break;
	case CTFLT:
Ken Thompson's avatar
Ken Thompson committed
87
		Bprint(bout, "%.17e\n", mpgetflt(n->val.u.fval));
88 89
		break;
	case CTSTR:
Ken Thompson's avatar
Ken Thompson committed
90
		Bprint(bout, "\"%Z\"\n", n->val.u.sval);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		break;
	}
}

void
dumpexportvar(Sym *s)
{
	Node *n;
	Type *t;

	if(s->exported != 0)
		return;
	s->exported = 1;

	n = s->oname;
Ken Thompson's avatar
Ken Thompson committed
106
	if(n == N || n->type == T) {
Ken Thompson's avatar
Ken Thompson committed
107
		yyerror("variable exported but not defined: %S", s);
Ken Thompson's avatar
Ken Thompson committed
108 109
		return;
	}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

	t = n->type;
	reexport(t);

	Bprint(bout, "\tvar ");
	if(s->export != 0)
		Bprint(bout, "!");
	Bprint(bout, "%lS %lS\n", s, t->sym);
}

void
dumpexporttype(Sym *s)
{
	Type *t, *f;
	Sym *ts;
	int et;

	if(s->exported != 0)
		return;
	s->exported = 1;

	t = s->otype;
Ken Thompson's avatar
Ken Thompson committed
132
	if(t == T) {
Ken Thompson's avatar
Ken Thompson committed
133
		yyerror("type exported but not defined: %S", s);
Ken Thompson's avatar
Ken Thompson committed
134 135 136
		return;
	}

137
	if(t->sym != s)
Ken Thompson's avatar
Ken Thompson committed
138
		fatal("dumpexporttype: cross reference: %S", s);
139 140 141 142 143

	et = t->etype;
	switch(et) {
	default:
		if(et < 0 || et >= nelem(types) || types[et] == T)
Ken Thompson's avatar
Ken Thompson committed
144
			fatal("dumpexporttype: basic type: %S %E", s, et);
145
		/* type 5 */
Russ Cox's avatar
Russ Cox committed
146 147 148 149
		Bprint(bout, "\ttype ");
		if(s->export != 0)
			Bprint(bout, "!");
		Bprint(bout, "%lS %d\n", s, et);
150 151 152 153 154 155 156 157 158
		break;

	case TARRAY:
		reexport(t->type);

		/* type 2 */
		Bprint(bout, "\ttype ");
		if(s->export != 0)
			Bprint(bout, "!");
159 160 161
		if(t->bound >= 0)
			Bprint(bout, "%lS [%lud] %lS\n", s, t->bound, t->type->sym);
		else
Ken Thompson's avatar
arrays  
Ken Thompson committed
162
			Bprint(bout, "%lS [] %lS\n", s, t->type->sym);
163 164 165 166
		break;

	case TPTR32:
	case TPTR64:
Ken Thompson's avatar
Ken Thompson committed
167 168
		if(t->type != T && t->type->sym == S)
			reexport(t->type);
169 170 171 172 173 174 175 176 177 178 179

		/* type 6 */
		Bprint(bout, "\ttype ");
		if(s->export != 0)
			Bprint(bout, "!");
		Bprint(bout, "%lS *%lS\n", s, t->type->sym);
		break;

	case TFUNC:
		for(f=t->type; f!=T; f=f->down) {
			if(f->etype != TSTRUCT)
Ken Thompson's avatar
Ken Thompson committed
180
				fatal("dumpexporttype: funct not field: %T", f);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
			reexport(f);
		}

		/* type 3 */
		Bprint(bout, "\ttype ");
		if(s->export != 0)
			Bprint(bout, "!");
		Bprint(bout, "%lS (", s);
		for(f=t->type; f!=T; f=f->down) {
			if(f != t->type)
				Bprint(bout, " ");
			Bprint(bout, "%lS", f->sym);
		}
		Bprint(bout, ")\n");
		break;

	case TSTRUCT:
	case TINTER:
		for(f=t->type; f!=T; f=f->down) {
			if(f->etype != TFIELD)
Ken Thompson's avatar
Ken Thompson committed
201
				fatal("dumpexporttype: funct not field: %lT", f);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
			reexport(f->type);
		}

		/* type 4 */
		Bprint(bout, "\ttype ");
		if(s->export)
			Bprint(bout, "!");
		Bprint(bout, "%lS %c", s, (et==TSTRUCT)? '{': '<');
		for(f=t->type; f!=T; f=f->down) {
			ts = f->type->sym;
			if(f != t->type)
				Bprint(bout, " ");
			Bprint(bout, "%s %lS", f->sym->name, ts);
		}
		Bprint(bout, "%c\n", (et==TSTRUCT)? '}': '>');
		break;
218 219 220 221 222 223 224 225 226 227 228

	case TMAP:
		reexport(t->type);
		reexport(t->down);

		/* type 6 */
		Bprint(bout, "\ttype ");
		if(s->export != 0)
			Bprint(bout, "!");
		Bprint(bout, "%lS [%lS] %lS\n", s, t->down->sym, t->type->sym);
		break;
Ken Thompson's avatar
Ken Thompson committed
229 230 231 232 233 234 235 236 237 238

	case TCHAN:
		reexport(t->type);

		/* type 8 */
		Bprint(bout, "\ttype ");
		if(s->export != 0)
			Bprint(bout, "!");
		Bprint(bout, "%lS %d %lS\n", s, t->chan, t->type->sym);
		break;
239 240 241 242 243 244 245 246
	}
}

void
dumpe(Sym *s)
{
	switch(s->lexical) {
	default:
Ken Thompson's avatar
Ken Thompson committed
247
		yyerror("unknown export symbol: %S", s, s->lexical);
248 249
		break;
	case LPACK:
Ken Thompson's avatar
Ken Thompson committed
250
		yyerror("package export symbol: %S", s);
251 252 253 254 255 256 257 258 259 260 261 262 263 264
		break;
	case LATYPE:
	case LBASETYPE:
		dumpexporttype(s);
		break;
	case LNAME:
		dumpexportvar(s);
		break;
	case LACONST:
		dumpexportconst(s);
		break;
	}
}

Ken Thompson's avatar
Ken Thompson committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
void
dumpm(Sym *s)
{
	Type *t, *f;

	switch(s->lexical) {
	default:
		return;

	case LATYPE:
	case LBASETYPE:
		break;
	}

	t = s->otype;
	if(t == T) {
		yyerror("type exported but not defined: %S", s);
		return;
	}

	for(f=t->method; f!=T; f=f->down) {
		if(f->etype != TFIELD)
			fatal("dumpexporttype: method not field: %lT", f);
		reexport(f->type);
		Bprint(bout, "\tfunc %S %lS\n", f->sym, f->type->sym);
	}
}

293 294 295 296
void
dumpexport(void)
{
	Dcl *d;
297
	int32 lno;
298

Ken Thompson's avatar
Ken Thompson committed
299
	lno = lineno;
300 301 302 303

	Bprint(bout, "   import\n");
	Bprint(bout, "   ((\n");

Ken Thompson's avatar
Ken Thompson committed
304 305
	Bprint(bout, "    package %s\n", package);

Ken Thompson's avatar
Ken Thompson committed
306
	// first pass dump vars/types depth first
307
	for(d=exportlist->forw; d!=D; d=d->forw) {
Ken Thompson's avatar
Ken Thompson committed
308
		lineno = d->lineno;
309 310 311
		dumpe(d->dsym);
	}

Ken Thompson's avatar
Ken Thompson committed
312 313 314 315 316 317
	// second pass dump methods
	for(d=exportlist->forw; d!=D; d=d->forw) {
		lineno = d->lineno;
		dumpm(d->dsym);
	}

318 319
	Bprint(bout, "   ))\n");

Ken Thompson's avatar
Ken Thompson committed
320
	lineno = lno;
321 322 323 324 325
}

/*
 * ******* import *******
 */
Ken Thompson's avatar
Ken Thompson committed
326 327 328 329 330 331 332 333
void
renamepkg(Node *n)
{
	if(n->psym == pkgimportname)
		if(pkgmyname != S)
			n->psym = pkgmyname;
}

Ken Thompson's avatar
Ken Thompson committed
334 335 336 337 338 339 340
Sym*
getimportsym(Node *ss)
{
	char *pkg;
	Sym *s;

	if(ss->op != OIMPORT)
Ken Thompson's avatar
Ken Thompson committed
341
		fatal("getimportsym: oops1 %N", ss);
Ken Thompson's avatar
Ken Thompson committed
342 343 344 345 346 347 348 349 350

	pkg = ss->psym->name;
	s = pkglookup(ss->sym->name, pkg);

	/* botch - need some diagnostic checking for the following assignment */
	s->opackage = ss->osym->name;
	return s;
}

351 352 353 354 355
Type*
importlooktype(Node *n)
{
	Sym *s;

Ken Thompson's avatar
Ken Thompson committed
356
	s = getimportsym(n);
357
	if(s->otype == T)
Ken Thompson's avatar
Ken Thompson committed
358
		fatal("importlooktype: oops2 %S", s);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	return s->otype;
}

Type**
importstotype(Node *fl, Type **t, Type *uber)
{
	Type *f;
	Iter save;
	Node *n;

	n = listfirst(&save, &fl);

loop:
	if(n == N) {
		*t = T;
		return t;
	}
	f = typ(TFIELD);
	f->type = importlooktype(n);

	if(n->fsym != S) {
		f->nname = newname(n->fsym);
	} else {
		vargen++;
		snprint(namebuf, sizeof(namebuf), "_m%.3ld", vargen);
		f->nname = newname(lookup(namebuf));
	}
	f->sym = f->nname->sym;

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

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

int
importcount(Type *t)
{
	int i;
	Type *f;

	if(t == T || t->etype != TSTRUCT)
		fatal("importcount: not a struct: %N", t);

	i = 0;
	for(f=t->type; f!=T; f=f->down)
		i = i+1;
	return i;
}

void
importfuncnam(Type *t)
{
	Node *n;
	Type *t1;

	if(t->etype != TFUNC)
Ken Thompson's avatar
Ken Thompson committed
417
		fatal("importfuncnam: not func %T", t);
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

	if(t->thistuple > 0) {
		t1 = t->type;
		if(t1->sym == S)
			fatal("importfuncnam: no this");
		n = newname(t1->sym);
		vargen++;
		n->vargen = vargen;
		t1->nname = n;
	}
	if(t->outtuple > 0) {
		t1 = t->type->down;
		if(t1->sym == S)
			fatal("importfuncnam: no output");
		n = newname(t1->sym);
		vargen++;
		n->vargen = vargen;
		t1->nname = n;
	}
	if(t->intuple > 0) {
		t1 = t->type->down->down;
		if(t1->sym == S)
			fatal("importfuncnam: no input");
		n = newname(t1->sym);
		vargen++;
		n->vargen = vargen;
		t1->nname = n;
	}
}

void
importaddtyp(Node *ss, Type *t)
{
	Sym *s;

	s = getimportsym(ss);
Russ Cox's avatar
Russ Cox committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467
	if(ss->etype){	// exported
		if(s->otype == T || !eqtype(t, s->otype, 0)) {
			if(s->otype != T)
				print("redeclaring %S %lT => %lT\n", s, s->otype, t);
			addtyp(newtype(s), t, PEXTERN);
			/*
			 * mark as export to avoid conflicting export bits
			 * in multi-file package.
			 */
			s->export = 1;
		}
	}else{
		s->otype = t;
		t->sym = s;
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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	}
}

/*
 * LCONST importsym LITERAL
 * untyped constant
 */
void
doimportc1(Node *ss, Val *v)
{
	Node *n;
	Sym *s;

	n = nod(OLITERAL, N, N);
	n->val = *v;

	s = getimportsym(ss);
	if(s->oconst == N) {
		// botch sould ask if already declared the same
		dodclconst(newname(s), n);
	}
}

/*
 * LCONST importsym importsym LITERAL
 * typed constant
 */
void
doimportc2(Node *ss, Node *st, Val *v)
{
	Node *n;
	Type *t;
	Sym *s;

	n = nod(OLITERAL, N, N);
	n->val = *v;

	t = importlooktype(st);
	n->type = t;

	s = getimportsym(ss);
	if(s->oconst == N) {
		// botch sould ask if already declared the same
		dodclconst(newname(s), n);
	}
}

/*
 * LVAR importsym importsym
 * variable
 */
void
doimportv1(Node *ss, Node *st)
{
	Type *t;
	Sym *s;

	t = importlooktype(st);
	s = getimportsym(ss);
	if(s->oname == N || !eqtype(t, s->oname->type, 0)) {
		addvar(newname(s), t, dclcontext);
	}
}

/*
 * LTYPE importsym [ importsym ] importsym
 * array type
 */
void
537
doimport1(Node *ss, Node *si, Node *st)
538
{
539 540 541 542 543 544 545 546 547 548
	Type *t;
	Sym *s;

	t = typ(TMAP);
	s = pkglookup(si->sym->name, si->psym->name);
	t->down = s->otype;
	s = pkglookup(st->sym->name, st->psym->name);
	t->type = s->otype;

	importaddtyp(ss, t);
549 550 551 552 553 554 555 556 557 558 559 560
}

/*
 * LTYPE importsym [ LLITERAL ] importsym
 * array type
 */
void
doimport2(Node *ss, Val *b, Node *st)
{
	Type *t;
	Sym *s;

561 562 563
	t = typ(TARRAY);
	t->bound = -1;
	if(b != nil)
Ken Thompson's avatar
arrays  
Ken Thompson committed
564
		t->bound = mpgetfix(b->u.xval);
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
	s = pkglookup(st->sym->name, st->psym->name);
	t->type = s->otype;

	importaddtyp(ss, t);
}

/*
 * LTYPE importsym '(' importsym_list ')'
 * function/method type
 */
void
doimport3(Node *ss, Node *n)
{
	Type *t;

	t = typ(TFUNC);

	t->type = importlooktype(n->left);
	t->type->down = importlooktype(n->right->left);
	t->type->down->down = importlooktype(n->right->right);

	t->thistuple = importcount(t->type);
	t->outtuple = importcount(t->type->down);
	t->intuple = importcount(t->type->down->down);
Ken Thompson's avatar
Ken Thompson committed
589
	dowidth(t);
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	importfuncnam(t);

	importaddtyp(ss, t);
}

/*
 * LTYPE importsym '{' importsym_list '}'
 * structure type
 */
void
doimport4(Node *ss, Node *n)
{
	Type *t;

	t = typ(TSTRUCT);
	importstotype(n, &t->type, t);
606
	dowidth(t);
607 608 609 610 611 612 613 614 615 616 617 618 619 620

	importaddtyp(ss, t);
}

/*
 * LTYPE importsym LLITERAL
 * basic type
 */
void
doimport5(Node *ss, Val *v)
{
	int et;
	Type *t;

Ken Thompson's avatar
Ken Thompson committed
621
	et = mpgetfix(v->u.xval);
622
	if(et <= 0 || et >= nelem(types) || types[et] == T)
Ken Thompson's avatar
Ken Thompson committed
623
		fatal("doimport5: bad type index: %E", et);
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

	t = typ(et);
	t->sym = S;

	importaddtyp(ss, t);
}

/*
 * LTYPE importsym * importsym
 * pointer type
 */
void
doimport6(Node *ss, Node *st)
{
	Type *t;
	Sym *s;

	s = pkglookup(st->sym->name, st->psym->name);
	t = s->otype;
	if(t == T)
		t = forwdcl(s);
	else
		t = ptrto(t);

	importaddtyp(ss, t);
}

/*
 * LTYPE importsym '<' importsym '>'
 * interface type
 */
void
doimport7(Node *ss, Node *n)
{
	Type *t;

	t = typ(TINTER);
	importstotype(n, &t->type, t);
662
	dowidth(t);
663 664 665

	importaddtyp(ss, t);
}
Ken Thompson's avatar
Ken Thompson committed
666 667 668 669 670 671 672 673 674 675 676 677 678

/*
 * LTYPE importsym chdir importsym
 * interface type
 */
void
doimport8(Node *ss, Val *v, Node *st)
{
	Type *t;
	Sym *s;
	int dir;

	s = pkglookup(st->sym->name, st->psym->name);
Ken Thompson's avatar
Ken Thompson committed
679
	dir = mpgetfix(v->u.xval);
Ken Thompson's avatar
Ken Thompson committed
680 681 682 683 684 685 686 687

	t = typ(TCHAN);
	s = pkglookup(st->sym->name, st->psym->name);
	t->type = s->otype;
	t->chan = dir;

	importaddtyp(ss, t);
}
Ken Thompson's avatar
Ken Thompson committed
688 689 690 691 692 693 694 695 696 697 698 699 700

/*
 * LFUNC importsym sym
 * method type
 */
void
doimport9(Sym *sf, Node *ss)
{
	Sym *sfun;

	sfun = getimportsym(ss);
	addmethod(newname(sf), sfun->otype, 0);
}