github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/cmd/gc/dcl.c (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 #include <u.h> 6 #include <libc.h> 7 #include "go.h" 8 #include "y.tab.h" 9 10 static void funcargs(Node*); 11 static void funcargs2(Type*); 12 13 static int 14 dflag(void) 15 { 16 if(!debug['d']) 17 return 0; 18 if(debug['y']) 19 return 1; 20 if(incannedimport) 21 return 0; 22 return 1; 23 } 24 25 /* 26 * declaration stack & operations 27 */ 28 29 static void 30 dcopy(Sym *a, Sym *b) 31 { 32 a->pkg = b->pkg; 33 a->name = b->name; 34 a->def = b->def; 35 a->block = b->block; 36 a->lastlineno = b->lastlineno; 37 } 38 39 static Sym* 40 push(void) 41 { 42 Sym *d; 43 44 d = mal(sizeof(*d)); 45 d->lastlineno = lineno; 46 d->link = dclstack; 47 dclstack = d; 48 return d; 49 } 50 51 static Sym* 52 pushdcl(Sym *s) 53 { 54 Sym *d; 55 56 d = push(); 57 dcopy(d, s); 58 if(dflag()) 59 print("\t%L push %S %p\n", lineno, s, s->def); 60 return d; 61 } 62 63 void 64 popdcl(void) 65 { 66 Sym *d, *s; 67 int lno; 68 69 // if(dflag()) 70 // print("revert\n"); 71 72 for(d=dclstack; d!=S; d=d->link) { 73 if(d->name == nil) 74 break; 75 s = pkglookup(d->name, d->pkg); 76 lno = s->lastlineno; 77 dcopy(s, d); 78 d->lastlineno = lno; 79 if(dflag()) 80 print("\t%L pop %S %p\n", lineno, s, s->def); 81 } 82 if(d == S) 83 fatal("popdcl: no mark"); 84 dclstack = d->link; 85 block = d->block; 86 } 87 88 void 89 poptodcl(void) 90 { 91 // pop the old marker and push a new one 92 // (cannot reuse the existing one) 93 // because we use the markers to identify blocks 94 // for the goto restriction checks. 95 popdcl(); 96 markdcl(); 97 } 98 99 void 100 markdcl(void) 101 { 102 Sym *d; 103 104 d = push(); 105 d->name = nil; // used as a mark in fifo 106 d->block = block; 107 108 blockgen++; 109 block = blockgen; 110 111 // if(dflag()) 112 // print("markdcl\n"); 113 } 114 115 void 116 dumpdcl(char *st) 117 { 118 Sym *s, *d; 119 int i; 120 121 USED(st); 122 123 i = 0; 124 for(d=dclstack; d!=S; d=d->link) { 125 i++; 126 print(" %.2d %p", i, d); 127 if(d->name == nil) { 128 print("\n"); 129 continue; 130 } 131 print(" '%s'", d->name); 132 s = pkglookup(d->name, d->pkg); 133 print(" %S\n", s); 134 } 135 } 136 137 void 138 testdclstack(void) 139 { 140 Sym *d; 141 142 for(d=dclstack; d!=S; d=d->link) { 143 if(d->name == nil) { 144 if(nerrors != 0) 145 errorexit(); 146 yyerror("mark left on the stack"); 147 continue; 148 } 149 } 150 } 151 152 void 153 redeclare(Sym *s, char *where) 154 { 155 Strlit *pkgstr; 156 int line1, line2; 157 158 if(s->lastlineno == 0) { 159 pkgstr = s->origpkg ? s->origpkg->path : s->pkg->path; 160 yyerror("%S redeclared %s\n" 161 "\tprevious declaration during import \"%Z\"", 162 s, where, pkgstr); 163 } else { 164 line1 = parserline(); 165 line2 = s->lastlineno; 166 167 // When an import and a declaration collide in separate files, 168 // present the import as the "redeclared", because the declaration 169 // is visible where the import is, but not vice versa. 170 // See issue 4510. 171 if(s->def == N) { 172 line2 = line1; 173 line1 = s->lastlineno; 174 } 175 176 yyerrorl(line1, "%S redeclared %s\n" 177 "\tprevious declaration at %L", 178 s, where, line2); 179 } 180 } 181 182 static int vargen; 183 184 /* 185 * declare individual names - var, typ, const 186 */ 187 void 188 declare(Node *n, int ctxt) 189 { 190 Sym *s; 191 int gen; 192 static int typegen; 193 194 if(ctxt == PDISCARD) 195 return; 196 197 if(isblank(n)) 198 return; 199 200 n->lineno = parserline(); 201 s = n->sym; 202 203 // kludgy: typecheckok means we're past parsing. Eg genwrapper may declare out of package names later. 204 if(importpkg == nil && !typecheckok && s->pkg != localpkg) 205 yyerror("cannot declare name %S", s); 206 207 if(ctxt == PEXTERN && strcmp(s->name, "init") == 0) 208 yyerror("cannot declare init - must be func", s); 209 210 gen = 0; 211 if(ctxt == PEXTERN) { 212 externdcl = list(externdcl, n); 213 if(dflag()) 214 print("\t%L global decl %S %p\n", lineno, s, n); 215 } else { 216 if(curfn == nil && ctxt == PAUTO) 217 fatal("automatic outside function"); 218 if(curfn != nil) 219 curfn->dcl = list(curfn->dcl, n); 220 if(n->op == OTYPE) 221 gen = ++typegen; 222 else if(n->op == ONAME && ctxt == PAUTO && strstr(s->name, "·") == nil) 223 gen = ++vargen; 224 pushdcl(s); 225 n->curfn = curfn; 226 } 227 if(ctxt == PAUTO) 228 n->xoffset = 0; 229 230 if(s->block == block) { 231 // functype will print errors about duplicate function arguments. 232 // Don't repeat the error here. 233 if(ctxt != PPARAM && ctxt != PPARAMOUT) 234 redeclare(s, "in this block"); 235 } 236 237 s->block = block; 238 s->lastlineno = parserline(); 239 s->def = n; 240 n->vargen = gen; 241 n->funcdepth = funcdepth; 242 n->class = ctxt; 243 244 autoexport(n, ctxt); 245 } 246 247 void 248 addvar(Node *n, Type *t, int ctxt) 249 { 250 if(n==N || n->sym == S || (n->op != ONAME && n->op != ONONAME) || t == T) 251 fatal("addvar: n=%N t=%T nil", n, t); 252 253 n->op = ONAME; 254 declare(n, ctxt); 255 n->type = t; 256 } 257 258 /* 259 * declare variables from grammar 260 * new_name_list (type | [type] = expr_list) 261 */ 262 NodeList* 263 variter(NodeList *vl, Node *t, NodeList *el) 264 { 265 int doexpr; 266 Node *v, *e, *as2; 267 NodeList *init; 268 269 init = nil; 270 doexpr = el != nil; 271 272 if(count(el) == 1 && count(vl) > 1) { 273 e = el->n; 274 as2 = nod(OAS2, N, N); 275 as2->list = vl; 276 as2->rlist = list1(e); 277 for(; vl; vl=vl->next) { 278 v = vl->n; 279 v->op = ONAME; 280 declare(v, dclcontext); 281 v->ntype = t; 282 v->defn = as2; 283 if(funcdepth > 0) 284 init = list(init, nod(ODCL, v, N)); 285 } 286 return list(init, as2); 287 } 288 289 for(; vl; vl=vl->next) { 290 if(doexpr) { 291 if(el == nil) { 292 yyerror("missing expression in var declaration"); 293 break; 294 } 295 e = el->n; 296 el = el->next; 297 } else 298 e = N; 299 300 v = vl->n; 301 v->op = ONAME; 302 declare(v, dclcontext); 303 v->ntype = t; 304 305 if(e != N || funcdepth > 0 || isblank(v)) { 306 if(funcdepth > 0) 307 init = list(init, nod(ODCL, v, N)); 308 e = nod(OAS, v, e); 309 init = list(init, e); 310 if(e->right != N) 311 v->defn = e; 312 } 313 } 314 if(el != nil) 315 yyerror("extra expression in var declaration"); 316 return init; 317 } 318 319 /* 320 * declare constants from grammar 321 * new_name_list [[type] = expr_list] 322 */ 323 NodeList* 324 constiter(NodeList *vl, Node *t, NodeList *cl) 325 { 326 Node *v, *c; 327 NodeList *vv; 328 329 vv = nil; 330 if(cl == nil) { 331 if(t != N) 332 yyerror("const declaration cannot have type without expression"); 333 cl = lastconst; 334 t = lasttype; 335 } else { 336 lastconst = cl; 337 lasttype = t; 338 } 339 cl = listtreecopy(cl); 340 341 for(; vl; vl=vl->next) { 342 if(cl == nil) { 343 yyerror("missing value in const declaration"); 344 break; 345 } 346 c = cl->n; 347 cl = cl->next; 348 349 v = vl->n; 350 v->op = OLITERAL; 351 declare(v, dclcontext); 352 353 v->ntype = t; 354 v->defn = c; 355 356 vv = list(vv, nod(ODCLCONST, v, N)); 357 } 358 if(cl != nil) 359 yyerror("extra expression in const declaration"); 360 iota += 1; 361 return vv; 362 } 363 364 /* 365 * this generates a new name node, 366 * typically for labels or other one-off names. 367 */ 368 Node* 369 newname(Sym *s) 370 { 371 Node *n; 372 373 if(s == S) 374 fatal("newname nil"); 375 376 n = nod(ONAME, N, N); 377 n->sym = s; 378 n->type = T; 379 n->addable = 1; 380 n->ullman = 1; 381 n->xoffset = 0; 382 return n; 383 } 384 385 /* 386 * this generates a new name node for a name 387 * being declared. 388 */ 389 Node* 390 dclname(Sym *s) 391 { 392 Node *n; 393 394 n = newname(s); 395 n->op = ONONAME; // caller will correct it 396 return n; 397 } 398 399 Node* 400 typenod(Type *t) 401 { 402 // if we copied another type with *t = *u 403 // then t->nod might be out of date, so 404 // check t->nod->type too 405 if(t->nod == N || t->nod->type != t) { 406 t->nod = nod(OTYPE, N, N); 407 t->nod->type = t; 408 t->nod->sym = t->sym; 409 } 410 return t->nod; 411 } 412 413 414 /* 415 * this will return an old name 416 * that has already been pushed on the 417 * declaration list. a diagnostic is 418 * generated if no name has been defined. 419 */ 420 Node* 421 oldname(Sym *s) 422 { 423 Node *n; 424 Node *c; 425 426 n = s->def; 427 if(n == N) { 428 // maybe a top-level name will come along 429 // to give this a definition later. 430 // walkdef will check s->def again once 431 // all the input source has been processed. 432 n = newname(s); 433 n->op = ONONAME; 434 n->iota = iota; // save current iota value in const declarations 435 } 436 if(curfn != nil && n->funcdepth > 0 && n->funcdepth != funcdepth && n->op == ONAME) { 437 // inner func is referring to var in outer func. 438 // 439 // TODO(rsc): If there is an outer variable x and we 440 // are parsing x := 5 inside the closure, until we get to 441 // the := it looks like a reference to the outer x so we'll 442 // make x a closure variable unnecessarily. 443 if(n->closure == N || n->closure->funcdepth != funcdepth) { 444 // create new closure var. 445 c = nod(ONAME, N, N); 446 c->sym = s; 447 c->class = PPARAMREF; 448 c->isddd = n->isddd; 449 c->defn = n; 450 c->addable = 0; 451 c->ullman = 2; 452 c->funcdepth = funcdepth; 453 c->outer = n->closure; 454 n->closure = c; 455 n->addrtaken = 1; 456 c->closure = n; 457 c->xoffset = 0; 458 curfn->cvars = list(curfn->cvars, c); 459 } 460 // return ref to closure var, not original 461 return n->closure; 462 } 463 return n; 464 } 465 466 /* 467 * := declarations 468 */ 469 470 static int 471 colasname(Node *n) 472 { 473 switch(n->op) { 474 case ONAME: 475 case ONONAME: 476 case OPACK: 477 case OTYPE: 478 case OLITERAL: 479 return n->sym != S; 480 } 481 return 0; 482 } 483 484 void 485 colasdefn(NodeList *left, Node *defn) 486 { 487 int nnew, nerr; 488 NodeList *l; 489 Node *n; 490 491 for(l=left; l; l=l->next) 492 if(l->n->sym != S) 493 l->n->sym->flags |= SymUniq; 494 495 nnew = 0; 496 nerr = 0; 497 for(l=left; l; l=l->next) { 498 n = l->n; 499 if(isblank(n)) 500 continue; 501 if(!colasname(n)) { 502 yyerrorl(defn->lineno, "non-name %N on left side of :=", n); 503 nerr++; 504 continue; 505 } 506 if((n->sym->flags & SymUniq) == 0) { 507 yyerrorl(defn->lineno, "%S repeated on left side of :=", n->sym); 508 n->diag++; 509 nerr++; 510 continue; 511 } 512 n->sym->flags &= ~SymUniq; 513 if(n->sym->block == block) 514 continue; 515 516 nnew++; 517 n = newname(n->sym); 518 declare(n, dclcontext); 519 n->defn = defn; 520 defn->ninit = list(defn->ninit, nod(ODCL, n, N)); 521 l->n = n; 522 } 523 if(nnew == 0 && nerr == 0) 524 yyerrorl(defn->lineno, "no new variables on left side of :="); 525 } 526 527 Node* 528 colas(NodeList *left, NodeList *right, int32 lno) 529 { 530 Node *as; 531 532 as = nod(OAS2, N, N); 533 as->list = left; 534 as->rlist = right; 535 as->colas = 1; 536 as->lineno = lno; 537 colasdefn(left, as); 538 539 // make the tree prettier; not necessary 540 if(count(left) == 1 && count(right) == 1) { 541 as->left = as->list->n; 542 as->right = as->rlist->n; 543 as->list = nil; 544 as->rlist = nil; 545 as->op = OAS; 546 } 547 548 return as; 549 } 550 551 /* 552 * declare the arguments in an 553 * interface field declaration. 554 */ 555 void 556 ifacedcl(Node *n) 557 { 558 if(n->op != ODCLFIELD || n->right == N) 559 fatal("ifacedcl"); 560 561 if(isblank(n->left)) 562 yyerror("methods must have a unique non-blank name"); 563 564 dclcontext = PPARAM; 565 markdcl(); 566 funcdepth++; 567 n->outer = curfn; 568 curfn = n; 569 funcargs(n->right); 570 571 // funcbody is normally called after the parser has 572 // seen the body of a function but since an interface 573 // field declaration does not have a body, we must 574 // call it now to pop the current declaration context. 575 dclcontext = PAUTO; 576 funcbody(n); 577 } 578 579 /* 580 * declare the function proper 581 * and declare the arguments. 582 * called in extern-declaration context 583 * returns in auto-declaration context. 584 */ 585 void 586 funchdr(Node *n) 587 { 588 // change the declaration context from extern to auto 589 if(funcdepth == 0 && dclcontext != PEXTERN) 590 fatal("funchdr: dclcontext"); 591 592 dclcontext = PAUTO; 593 markdcl(); 594 funcdepth++; 595 596 n->outer = curfn; 597 curfn = n; 598 599 if(n->nname) 600 funcargs(n->nname->ntype); 601 else if (n->ntype) 602 funcargs(n->ntype); 603 else 604 funcargs2(n->type); 605 } 606 607 static void 608 funcargs(Node *nt) 609 { 610 Node *n, *nn; 611 NodeList *l; 612 int gen; 613 614 if(nt->op != OTFUNC) 615 fatal("funcargs %O", nt->op); 616 617 // re-start the variable generation number 618 // we want to use small numbers for the return variables, 619 // so let them have the chunk starting at 1. 620 vargen = count(nt->rlist); 621 622 // declare the receiver and in arguments. 623 // no n->defn because type checking of func header 624 // will not fill in the types until later 625 if(nt->left != N) { 626 n = nt->left; 627 if(n->op != ODCLFIELD) 628 fatal("funcargs receiver %O", n->op); 629 if(n->left != N) { 630 n->left->op = ONAME; 631 n->left->ntype = n->right; 632 declare(n->left, PPARAM); 633 if(dclcontext == PAUTO) 634 n->left->vargen = ++vargen; 635 } 636 } 637 for(l=nt->list; l; l=l->next) { 638 n = l->n; 639 if(n->op != ODCLFIELD) 640 fatal("funcargs in %O", n->op); 641 if(n->left != N) { 642 n->left->op = ONAME; 643 n->left->ntype = n->right; 644 declare(n->left, PPARAM); 645 if(dclcontext == PAUTO) 646 n->left->vargen = ++vargen; 647 } 648 } 649 650 // declare the out arguments. 651 gen = count(nt->list); 652 int i = 0; 653 for(l=nt->rlist; l; l=l->next) { 654 n = l->n; 655 656 if(n->op != ODCLFIELD) 657 fatal("funcargs out %O", n->op); 658 659 if(n->left == N) { 660 // Name so that escape analysis can track it. ~r stands for 'result'. 661 snprint(namebuf, sizeof(namebuf), "~r%d", gen++); 662 n->left = newname(lookup(namebuf)); 663 // TODO: n->left->missing = 1; 664 } 665 666 n->left->op = ONAME; 667 668 if(isblank(n->left)) { 669 // Give it a name so we can assign to it during return. ~b stands for 'blank'. 670 // The name must be different from ~r above because if you have 671 // func f() (_ int) 672 // func g() int 673 // f is allowed to use a plain 'return' with no arguments, while g is not. 674 // So the two cases must be distinguished. 675 // We do not record a pointer to the original node (n->orig). 676 // Having multiple names causes too much confusion in later passes. 677 nn = nod(OXXX, N, N); 678 *nn = *n->left; 679 nn->orig = nn; 680 snprint(namebuf, sizeof(namebuf), "~b%d", gen++); 681 nn->sym = lookup(namebuf); 682 n->left = nn; 683 } 684 685 n->left->ntype = n->right; 686 declare(n->left, PPARAMOUT); 687 if(dclcontext == PAUTO) 688 n->left->vargen = ++i; 689 } 690 } 691 692 /* 693 * Same as funcargs, except run over an already constructed TFUNC. 694 * This happens during import, where the hidden_fndcl rule has 695 * used functype directly to parse the function's type. 696 */ 697 static void 698 funcargs2(Type *t) 699 { 700 Type *ft; 701 Node *n; 702 703 if(t->etype != TFUNC) 704 fatal("funcargs2 %T", t); 705 706 if(t->thistuple) 707 for(ft=getthisx(t)->type; ft; ft=ft->down) { 708 if(!ft->nname || !ft->nname->sym) 709 continue; 710 n = ft->nname; // no need for newname(ft->nname->sym) 711 n->type = ft->type; 712 declare(n, PPARAM); 713 } 714 715 if(t->intuple) 716 for(ft=getinargx(t)->type; ft; ft=ft->down) { 717 if(!ft->nname || !ft->nname->sym) 718 continue; 719 n = ft->nname; 720 n->type = ft->type; 721 declare(n, PPARAM); 722 } 723 724 if(t->outtuple) 725 for(ft=getoutargx(t)->type; ft; ft=ft->down) { 726 if(!ft->nname || !ft->nname->sym) 727 continue; 728 n = ft->nname; 729 n->type = ft->type; 730 declare(n, PPARAMOUT); 731 } 732 } 733 734 /* 735 * finish the body. 736 * called in auto-declaration context. 737 * returns in extern-declaration context. 738 */ 739 void 740 funcbody(Node *n) 741 { 742 // change the declaration context from auto to extern 743 if(dclcontext != PAUTO) 744 fatal("funcbody: dclcontext"); 745 popdcl(); 746 funcdepth--; 747 curfn = n->outer; 748 n->outer = N; 749 if(funcdepth == 0) 750 dclcontext = PEXTERN; 751 } 752 753 /* 754 * new type being defined with name s. 755 */ 756 Node* 757 typedcl0(Sym *s) 758 { 759 Node *n; 760 761 n = newname(s); 762 n->op = OTYPE; 763 declare(n, dclcontext); 764 return n; 765 } 766 767 /* 768 * node n, which was returned by typedcl0 769 * is being declared to have uncompiled type t. 770 * return the ODCLTYPE node to use. 771 */ 772 Node* 773 typedcl1(Node *n, Node *t, int local) 774 { 775 n->ntype = t; 776 n->local = local; 777 return nod(ODCLTYPE, n, N); 778 } 779 780 /* 781 * structs, functions, and methods. 782 * they don't belong here, but where do they belong? 783 */ 784 785 static void 786 checkembeddedtype(Type *t) 787 { 788 if (t == T) 789 return; 790 791 if(t->sym == S && isptr[t->etype]) { 792 t = t->type; 793 if(t->etype == TINTER) 794 yyerror("embedded type cannot be a pointer to interface"); 795 } 796 if(isptr[t->etype]) 797 yyerror("embedded type cannot be a pointer"); 798 else if(t->etype == TFORW && t->embedlineno == 0) 799 t->embedlineno = lineno; 800 } 801 802 static Type* 803 structfield(Node *n) 804 { 805 Type *f; 806 int lno; 807 808 lno = lineno; 809 lineno = n->lineno; 810 811 if(n->op != ODCLFIELD) 812 fatal("structfield: oops %N\n", n); 813 814 f = typ(TFIELD); 815 f->isddd = n->isddd; 816 817 if(n->right != N) { 818 typecheck(&n->right, Etype); 819 n->type = n->right->type; 820 if(n->left != N) 821 n->left->type = n->type; 822 if(n->embedded) 823 checkembeddedtype(n->type); 824 } 825 n->right = N; 826 827 f->type = n->type; 828 if(f->type == T) 829 f->broke = 1; 830 831 switch(n->val.ctype) { 832 case CTSTR: 833 f->note = n->val.u.sval; 834 break; 835 default: 836 yyerror("field annotation must be string"); 837 // fallthrough 838 case CTxxx: 839 f->note = nil; 840 break; 841 } 842 843 if(n->left && n->left->op == ONAME) { 844 f->nname = n->left; 845 f->embedded = n->embedded; 846 f->sym = f->nname->sym; 847 } 848 849 lineno = lno; 850 return f; 851 } 852 853 static uint32 uniqgen; 854 855 static void 856 checkdupfields(Type *t, char* what) 857 { 858 int lno; 859 860 lno = lineno; 861 862 for( ; t; t=t->down) { 863 if(t->sym && t->nname && !isblank(t->nname)) { 864 if(t->sym->uniqgen == uniqgen) { 865 lineno = t->nname->lineno; 866 yyerror("duplicate %s %s", what, t->sym->name); 867 } else 868 t->sym->uniqgen = uniqgen; 869 } 870 } 871 872 lineno = lno; 873 } 874 875 /* 876 * convert a parsed id/type list into 877 * a type for struct/interface/arglist 878 */ 879 Type* 880 tostruct(NodeList *l) 881 { 882 Type *t, *f, **tp; 883 t = typ(TSTRUCT); 884 885 for(tp = &t->type; l; l=l->next) { 886 f = structfield(l->n); 887 888 *tp = f; 889 tp = &f->down; 890 } 891 892 for(f=t->type; f && !t->broke; f=f->down) 893 if(f->broke) 894 t->broke = 1; 895 896 uniqgen++; 897 checkdupfields(t->type, "field"); 898 899 if (!t->broke) 900 checkwidth(t); 901 902 return t; 903 } 904 905 static Type* 906 tofunargs(NodeList *l) 907 { 908 Type *t, *f, **tp; 909 910 t = typ(TSTRUCT); 911 t->funarg = 1; 912 913 for(tp = &t->type; l; l=l->next) { 914 f = structfield(l->n); 915 f->funarg = 1; 916 917 // esc.c needs to find f given a PPARAM to add the tag. 918 if(l->n->left && l->n->left->class == PPARAM) 919 l->n->left->paramfld = f; 920 921 *tp = f; 922 tp = &f->down; 923 } 924 925 for(f=t->type; f && !t->broke; f=f->down) 926 if(f->broke) 927 t->broke = 1; 928 929 return t; 930 } 931 932 static Type* 933 interfacefield(Node *n) 934 { 935 Type *f; 936 int lno; 937 938 lno = lineno; 939 lineno = n->lineno; 940 941 if(n->op != ODCLFIELD) 942 fatal("interfacefield: oops %N\n", n); 943 944 if (n->val.ctype != CTxxx) 945 yyerror("interface method cannot have annotation"); 946 947 f = typ(TFIELD); 948 f->isddd = n->isddd; 949 950 if(n->right != N) { 951 if(n->left != N) { 952 // queue resolution of method type for later. 953 // right now all we need is the name list. 954 // avoids cycles for recursive interface types. 955 n->type = typ(TINTERMETH); 956 n->type->nname = n->right; 957 n->left->type = n->type; 958 queuemethod(n); 959 960 if(n->left->op == ONAME) { 961 f->nname = n->left; 962 f->embedded = n->embedded; 963 f->sym = f->nname->sym; 964 } 965 966 } else { 967 968 typecheck(&n->right, Etype); 969 n->type = n->right->type; 970 971 if(n->embedded) 972 checkembeddedtype(n->type); 973 974 if(n->type) 975 switch(n->type->etype) { 976 case TINTER: 977 break; 978 case TFORW: 979 yyerror("interface type loop involving %T", n->type); 980 f->broke = 1; 981 break; 982 default: 983 yyerror("interface contains embedded non-interface %T", n->type); 984 f->broke = 1; 985 break; 986 } 987 } 988 } 989 990 n->right = N; 991 992 f->type = n->type; 993 if(f->type == T) 994 f->broke = 1; 995 996 lineno = lno; 997 return f; 998 } 999 1000 Type* 1001 tointerface(NodeList *l) 1002 { 1003 Type *t, *f, **tp, *t1; 1004 1005 t = typ(TINTER); 1006 1007 tp = &t->type; 1008 for(; l; l=l->next) { 1009 f = interfacefield(l->n); 1010 1011 if (l->n->left == N && f->type->etype == TINTER) { 1012 // embedded interface, inline methods 1013 for(t1=f->type->type; t1; t1=t1->down) { 1014 f = typ(TFIELD); 1015 f->type = t1->type; 1016 f->broke = t1->broke; 1017 f->sym = t1->sym; 1018 if(f->sym) 1019 f->nname = newname(f->sym); 1020 *tp = f; 1021 tp = &f->down; 1022 } 1023 } else { 1024 *tp = f; 1025 tp = &f->down; 1026 } 1027 } 1028 1029 for(f=t->type; f && !t->broke; f=f->down) 1030 if(f->broke) 1031 t->broke = 1; 1032 1033 uniqgen++; 1034 checkdupfields(t->type, "method"); 1035 t = sortinter(t); 1036 checkwidth(t); 1037 1038 return t; 1039 } 1040 1041 Node* 1042 embedded(Sym *s, Pkg *pkg) 1043 { 1044 Node *n; 1045 char *name; 1046 1047 // Names sometimes have disambiguation junk 1048 // appended after a center dot. Discard it when 1049 // making the name for the embedded struct field. 1050 enum { CenterDot = 0xB7 }; 1051 name = s->name; 1052 if(utfrune(s->name, CenterDot)) { 1053 name = strdup(s->name); 1054 *utfrune(name, CenterDot) = 0; 1055 } 1056 1057 if(exportname(name)) 1058 n = newname(lookup(name)); 1059 else if(s->pkg == builtinpkg) 1060 // The name of embedded builtins belongs to pkg. 1061 n = newname(pkglookup(name, pkg)); 1062 else 1063 n = newname(pkglookup(name, s->pkg)); 1064 n = nod(ODCLFIELD, n, oldname(s)); 1065 n->embedded = 1; 1066 return n; 1067 } 1068 1069 /* 1070 * check that the list of declarations is either all anonymous or all named 1071 */ 1072 1073 static Node* 1074 findtype(NodeList *l) 1075 { 1076 for(; l; l=l->next) 1077 if(l->n->op == OKEY) 1078 return l->n->right; 1079 return N; 1080 } 1081 1082 NodeList* 1083 checkarglist(NodeList *all, int input) 1084 { 1085 int named; 1086 Node *n, *t, *nextt; 1087 NodeList *l; 1088 1089 named = 0; 1090 for(l=all; l; l=l->next) { 1091 if(l->n->op == OKEY) { 1092 named = 1; 1093 break; 1094 } 1095 } 1096 if(named) { 1097 n = N; 1098 for(l=all; l; l=l->next) { 1099 n = l->n; 1100 if(n->op != OKEY && n->sym == S) { 1101 yyerror("mixed named and unnamed function parameters"); 1102 break; 1103 } 1104 } 1105 if(l == nil && n != N && n->op != OKEY) 1106 yyerror("final function parameter must have type"); 1107 } 1108 1109 nextt = nil; 1110 for(l=all; l; l=l->next) { 1111 // can cache result from findtype to avoid 1112 // quadratic behavior here, but unlikely to matter. 1113 n = l->n; 1114 if(named) { 1115 if(n->op == OKEY) { 1116 t = n->right; 1117 n = n->left; 1118 nextt = nil; 1119 } else { 1120 if(nextt == nil) 1121 nextt = findtype(l); 1122 t = nextt; 1123 } 1124 } else { 1125 t = n; 1126 n = N; 1127 } 1128 1129 // during import l->n->op is OKEY, but l->n->left->sym == S 1130 // means it was a '?', not that it was 1131 // a lone type This doesn't matter for the exported 1132 // declarations, which are parsed by rules that don't 1133 // use checkargs, but can happen for func literals in 1134 // the inline bodies. 1135 // TODO(rsc) this can go when typefmt case TFIELD in exportmode fmt.c prints _ instead of ? 1136 if(importpkg && n->sym == S) 1137 n = N; 1138 1139 if(n != N && n->sym == S) { 1140 t = n; 1141 n = N; 1142 } 1143 if(n != N) 1144 n = newname(n->sym); 1145 n = nod(ODCLFIELD, n, t); 1146 if(n->right != N && n->right->op == ODDD) { 1147 if(!input) 1148 yyerror("cannot use ... in output argument list"); 1149 else if(l->next != nil) 1150 yyerror("can only use ... as final argument in list"); 1151 n->right->op = OTARRAY; 1152 n->right->right = n->right->left; 1153 n->right->left = N; 1154 n->isddd = 1; 1155 if(n->left != N) 1156 n->left->isddd = 1; 1157 } 1158 l->n = n; 1159 } 1160 return all; 1161 } 1162 1163 1164 Node* 1165 fakethis(void) 1166 { 1167 Node *n; 1168 1169 n = nod(ODCLFIELD, N, typenod(ptrto(typ(TSTRUCT)))); 1170 return n; 1171 } 1172 1173 /* 1174 * Is this field a method on an interface? 1175 * Those methods have an anonymous 1176 * *struct{} as the receiver. 1177 * (See fakethis above.) 1178 */ 1179 int 1180 isifacemethod(Type *f) 1181 { 1182 Type *rcvr; 1183 Type *t; 1184 1185 rcvr = getthisx(f)->type; 1186 if(rcvr->sym != S) 1187 return 0; 1188 t = rcvr->type; 1189 if(!isptr[t->etype]) 1190 return 0; 1191 t = t->type; 1192 if(t->sym != S || t->etype != TSTRUCT || t->type != T) 1193 return 0; 1194 return 1; 1195 } 1196 1197 /* 1198 * turn a parsed function declaration 1199 * into a type 1200 */ 1201 Type* 1202 functype(Node *this, NodeList *in, NodeList *out) 1203 { 1204 Type *t; 1205 NodeList *rcvr; 1206 Sym *s; 1207 1208 t = typ(TFUNC); 1209 1210 rcvr = nil; 1211 if(this) 1212 rcvr = list1(this); 1213 t->type = tofunargs(rcvr); 1214 t->type->down = tofunargs(out); 1215 t->type->down->down = tofunargs(in); 1216 1217 uniqgen++; 1218 checkdupfields(t->type->type, "argument"); 1219 checkdupfields(t->type->down->type, "argument"); 1220 checkdupfields(t->type->down->down->type, "argument"); 1221 1222 if (t->type->broke || t->type->down->broke || t->type->down->down->broke) 1223 t->broke = 1; 1224 1225 if(this) 1226 t->thistuple = 1; 1227 t->outtuple = count(out); 1228 t->intuple = count(in); 1229 t->outnamed = 0; 1230 if(t->outtuple > 0 && out->n->left != N && out->n->left->orig != N) { 1231 s = out->n->left->orig->sym; 1232 if(s != S && (s->name[0] != '~' || s->name[1] != 'r')) // ~r%d is the name invented for an unnamed result 1233 t->outnamed = 1; 1234 } 1235 1236 return t; 1237 } 1238 1239 Sym* 1240 methodsym(Sym *nsym, Type *t0, int iface) 1241 { 1242 Sym *s; 1243 char *p; 1244 Type *t; 1245 char *suffix; 1246 Pkg *spkg; 1247 static Pkg *toppkg; 1248 1249 t = t0; 1250 if(t == T) 1251 goto bad; 1252 s = t->sym; 1253 if(s == S && isptr[t->etype]) { 1254 t = t->type; 1255 if(t == T) 1256 goto bad; 1257 s = t->sym; 1258 } 1259 spkg = nil; 1260 if(s != S) 1261 spkg = s->pkg; 1262 1263 // if t0 == *t and t0 has a sym, 1264 // we want to see *t, not t0, in the method name. 1265 if(t != t0 && t0->sym) 1266 t0 = ptrto(t); 1267 1268 suffix = ""; 1269 if(iface) { 1270 dowidth(t0); 1271 if(t0->width < types[tptr]->width) 1272 suffix = "·i"; 1273 } 1274 if((spkg == nil || nsym->pkg != spkg) && !exportname(nsym->name)) { 1275 if(t0->sym == S && isptr[t0->etype]) 1276 p = smprint("(%-hT).%s.%s%s", t0, nsym->pkg->prefix, nsym->name, suffix); 1277 else 1278 p = smprint("%-hT.%s.%s%s", t0, nsym->pkg->prefix, nsym->name, suffix); 1279 } else { 1280 if(t0->sym == S && isptr[t0->etype]) 1281 p = smprint("(%-hT).%s%s", t0, nsym->name, suffix); 1282 else 1283 p = smprint("%-hT.%s%s", t0, nsym->name, suffix); 1284 } 1285 if(spkg == nil) { 1286 if(toppkg == nil) 1287 toppkg = mkpkg(strlit("go")); 1288 spkg = toppkg; 1289 } 1290 s = pkglookup(p, spkg); 1291 free(p); 1292 return s; 1293 1294 bad: 1295 yyerror("illegal receiver type: %T", t0); 1296 return S; 1297 } 1298 1299 Node* 1300 methodname(Node *n, Type *t) 1301 { 1302 Sym *s; 1303 1304 s = methodsym(n->sym, t, 0); 1305 if(s == S) 1306 return n; 1307 return newname(s); 1308 } 1309 1310 Node* 1311 methodname1(Node *n, Node *t) 1312 { 1313 char *star; 1314 char *p; 1315 1316 star = nil; 1317 if(t->op == OIND) { 1318 star = "*"; 1319 t = t->left; 1320 } 1321 if(t->sym == S || isblank(n)) 1322 return newname(n->sym); 1323 1324 if(star) 1325 p = smprint("(%s%S).%S", star, t->sym, n->sym); 1326 else 1327 p = smprint("%S.%S", t->sym, n->sym); 1328 1329 if(exportname(t->sym->name)) 1330 n = newname(lookup(p)); 1331 else 1332 n = newname(pkglookup(p, t->sym->pkg)); 1333 free(p); 1334 return n; 1335 } 1336 1337 /* 1338 * add a method, declared as a function, 1339 * n is fieldname, pa is base type, t is function type 1340 */ 1341 void 1342 addmethod(Sym *sf, Type *t, int local, int nointerface) 1343 { 1344 Type *f, *d, *pa; 1345 Node *n; 1346 1347 // get field sym 1348 if(sf == S) 1349 fatal("no method symbol"); 1350 1351 // get parent type sym 1352 pa = getthisx(t)->type; // ptr to this structure 1353 if(pa == T) { 1354 yyerror("missing receiver"); 1355 return; 1356 } 1357 1358 pa = pa->type; 1359 f = methtype(pa, 1); 1360 if(f == T) { 1361 t = pa; 1362 if(t == T) // rely on typecheck having complained before 1363 return; 1364 if(t != T) { 1365 if(isptr[t->etype]) { 1366 if(t->sym != S) { 1367 yyerror("invalid receiver type %T (%T is a pointer type)", pa, t); 1368 return; 1369 } 1370 t = t->type; 1371 } 1372 if(t->broke) // rely on typecheck having complained before 1373 return; 1374 if(t->sym == S) { 1375 yyerror("invalid receiver type %T (%T is an unnamed type)", pa, t); 1376 return; 1377 } 1378 if(isptr[t->etype]) { 1379 yyerror("invalid receiver type %T (%T is a pointer type)", pa, t); 1380 return; 1381 } 1382 if(t->etype == TINTER) { 1383 yyerror("invalid receiver type %T (%T is an interface type)", pa, t); 1384 return; 1385 } 1386 } 1387 // Should have picked off all the reasons above, 1388 // but just in case, fall back to generic error. 1389 yyerror("invalid receiver type %T (%lT / %lT)", pa, pa, t); 1390 return; 1391 } 1392 1393 pa = f; 1394 if(pa->etype == TSTRUCT) { 1395 for(f=pa->type; f; f=f->down) { 1396 if(f->sym == sf) { 1397 yyerror("type %T has both field and method named %S", pa, sf); 1398 return; 1399 } 1400 } 1401 } 1402 1403 if(local && !pa->local) { 1404 // defining method on non-local type. 1405 yyerror("cannot define new methods on non-local type %T", pa); 1406 return; 1407 } 1408 1409 n = nod(ODCLFIELD, newname(sf), N); 1410 n->type = t; 1411 1412 d = T; // last found 1413 for(f=pa->method; f!=T; f=f->down) { 1414 d = f; 1415 if(f->etype != TFIELD) 1416 fatal("addmethod: not TFIELD: %N", f); 1417 if(strcmp(sf->name, f->sym->name) != 0) 1418 continue; 1419 if(!eqtype(t, f->type)) 1420 yyerror("method redeclared: %T.%S\n\t%T\n\t%T", pa, sf, f->type, t); 1421 return; 1422 } 1423 1424 f = structfield(n); 1425 f->nointerface = nointerface; 1426 1427 // during import unexported method names should be in the type's package 1428 if(importpkg && f->sym && !exportname(f->sym->name) && f->sym->pkg != structpkg) 1429 fatal("imported method name %+S in wrong package %s\n", f->sym, structpkg->name); 1430 1431 if(d == T) 1432 pa->method = f; 1433 else 1434 d->down = f; 1435 return; 1436 } 1437 1438 void 1439 funccompile(Node *n, int isclosure) 1440 { 1441 stksize = BADWIDTH; 1442 maxarg = 0; 1443 1444 if(n->type == T) { 1445 if(nerrors == 0) 1446 fatal("funccompile missing type"); 1447 return; 1448 } 1449 1450 // assign parameter offsets 1451 checkwidth(n->type); 1452 1453 // record offset to actual frame pointer. 1454 // for closure, have to skip over leading pointers and PC slot. 1455 // TODO(rsc): this is the old jit closure handling code. 1456 // with the new closures, isclosure is always 0; delete this block. 1457 nodfp->xoffset = 0; 1458 if(isclosure) { 1459 NodeList *l; 1460 for(l=n->nname->ntype->list; l; l=l->next) { 1461 nodfp->xoffset += widthptr; 1462 if(l->n->left == N) // found slot for PC 1463 break; 1464 } 1465 } 1466 1467 if(curfn) 1468 fatal("funccompile %S inside %S", n->nname->sym, curfn->nname->sym); 1469 1470 stksize = 0; 1471 dclcontext = PAUTO; 1472 funcdepth = n->funcdepth + 1; 1473 compile(n); 1474 curfn = nil; 1475 funcdepth = 0; 1476 dclcontext = PEXTERN; 1477 } 1478 1479 Sym* 1480 funcsym(Sym *s) 1481 { 1482 char *p; 1483 Sym *s1; 1484 1485 p = smprint("%s·f", s->name); 1486 s1 = pkglookup(p, s->pkg); 1487 free(p); 1488 if(s1->def == N) { 1489 s1->def = newname(s1); 1490 s1->def->shortname = newname(s); 1491 funcsyms = list(funcsyms, s1->def); 1492 } 1493 return s1; 1494 }