github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/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 nnew = 0; 492 nerr = 0; 493 for(l=left; l; l=l->next) { 494 n = l->n; 495 if(isblank(n)) 496 continue; 497 if(!colasname(n)) { 498 yyerrorl(defn->lineno, "non-name %N on left side of :=", n); 499 nerr++; 500 continue; 501 } 502 if(n->sym->block == block) 503 continue; 504 505 nnew++; 506 n = newname(n->sym); 507 declare(n, dclcontext); 508 n->defn = defn; 509 defn->ninit = list(defn->ninit, nod(ODCL, n, N)); 510 l->n = n; 511 } 512 if(nnew == 0 && nerr == 0) 513 yyerrorl(defn->lineno, "no new variables on left side of :="); 514 } 515 516 Node* 517 colas(NodeList *left, NodeList *right, int32 lno) 518 { 519 Node *as; 520 521 as = nod(OAS2, N, N); 522 as->list = left; 523 as->rlist = right; 524 as->colas = 1; 525 as->lineno = lno; 526 colasdefn(left, as); 527 528 // make the tree prettier; not necessary 529 if(count(left) == 1 && count(right) == 1) { 530 as->left = as->list->n; 531 as->right = as->rlist->n; 532 as->list = nil; 533 as->rlist = nil; 534 as->op = OAS; 535 } 536 537 return as; 538 } 539 540 /* 541 * declare the arguments in an 542 * interface field declaration. 543 */ 544 void 545 ifacedcl(Node *n) 546 { 547 if(n->op != ODCLFIELD || n->right == N) 548 fatal("ifacedcl"); 549 550 dclcontext = PPARAM; 551 markdcl(); 552 funcdepth++; 553 n->outer = curfn; 554 curfn = n; 555 funcargs(n->right); 556 557 // funcbody is normally called after the parser has 558 // seen the body of a function but since an interface 559 // field declaration does not have a body, we must 560 // call it now to pop the current declaration context. 561 dclcontext = PAUTO; 562 funcbody(n); 563 } 564 565 /* 566 * declare the function proper 567 * and declare the arguments. 568 * called in extern-declaration context 569 * returns in auto-declaration context. 570 */ 571 void 572 funchdr(Node *n) 573 { 574 // change the declaration context from extern to auto 575 if(funcdepth == 0 && dclcontext != PEXTERN) 576 fatal("funchdr: dclcontext"); 577 578 dclcontext = PAUTO; 579 markdcl(); 580 funcdepth++; 581 582 n->outer = curfn; 583 curfn = n; 584 585 if(n->nname) 586 funcargs(n->nname->ntype); 587 else if (n->ntype) 588 funcargs(n->ntype); 589 else 590 funcargs2(n->type); 591 } 592 593 static void 594 funcargs(Node *nt) 595 { 596 Node *n, *nn; 597 NodeList *l; 598 int gen; 599 600 if(nt->op != OTFUNC) 601 fatal("funcargs %O", nt->op); 602 603 // re-start the variable generation number 604 // we want to use small numbers for the return variables, 605 // so let them have the chunk starting at 1. 606 vargen = count(nt->rlist); 607 608 // declare the receiver and in arguments. 609 // no n->defn because type checking of func header 610 // will not fill in the types until later 611 if(nt->left != N) { 612 n = nt->left; 613 if(n->op != ODCLFIELD) 614 fatal("funcargs receiver %O", n->op); 615 if(n->left != N) { 616 n->left->op = ONAME; 617 n->left->ntype = n->right; 618 declare(n->left, PPARAM); 619 if(dclcontext == PAUTO) 620 n->left->vargen = ++vargen; 621 } 622 } 623 for(l=nt->list; l; l=l->next) { 624 n = l->n; 625 if(n->op != ODCLFIELD) 626 fatal("funcargs in %O", n->op); 627 if(n->left != N) { 628 n->left->op = ONAME; 629 n->left->ntype = n->right; 630 declare(n->left, PPARAM); 631 if(dclcontext == PAUTO) 632 n->left->vargen = ++vargen; 633 } 634 } 635 636 // declare the out arguments. 637 gen = count(nt->list); 638 int i = 0; 639 for(l=nt->rlist; l; l=l->next) { 640 n = l->n; 641 642 if(n->op != ODCLFIELD) 643 fatal("funcargs out %O", n->op); 644 645 if(n->left == N) { 646 // Name so that escape analysis can track it. ~r stands for 'result'. 647 snprint(namebuf, sizeof(namebuf), "~r%d", gen++); 648 n->left = newname(lookup(namebuf)); 649 // TODO: n->left->missing = 1; 650 } 651 652 n->left->op = ONAME; 653 654 if(isblank(n->left)) { 655 // Give it a name so we can assign to it during return. ~b stands for 'blank'. 656 // The name must be different from ~r above because if you have 657 // func f() (_ int) 658 // func g() int 659 // f is allowed to use a plain 'return' with no arguments, while g is not. 660 // So the two cases must be distinguished. 661 // We do not record a pointer to the original node (n->orig). 662 // Having multiple names causes too much confusion in later passes. 663 nn = nod(OXXX, N, N); 664 *nn = *n->left; 665 nn->orig = nn; 666 snprint(namebuf, sizeof(namebuf), "~b%d", gen++); 667 nn->sym = lookup(namebuf); 668 n->left = nn; 669 } 670 671 n->left->ntype = n->right; 672 declare(n->left, PPARAMOUT); 673 if(dclcontext == PAUTO) 674 n->left->vargen = ++i; 675 } 676 } 677 678 /* 679 * Same as funcargs, except run over an already constructed TFUNC. 680 * This happens during import, where the hidden_fndcl rule has 681 * used functype directly to parse the function's type. 682 */ 683 static void 684 funcargs2(Type *t) 685 { 686 Type *ft; 687 Node *n; 688 689 if(t->etype != TFUNC) 690 fatal("funcargs2 %T", t); 691 692 if(t->thistuple) 693 for(ft=getthisx(t)->type; ft; ft=ft->down) { 694 if(!ft->nname || !ft->nname->sym) 695 continue; 696 n = ft->nname; // no need for newname(ft->nname->sym) 697 n->type = ft->type; 698 declare(n, PPARAM); 699 } 700 701 if(t->intuple) 702 for(ft=getinargx(t)->type; ft; ft=ft->down) { 703 if(!ft->nname || !ft->nname->sym) 704 continue; 705 n = ft->nname; 706 n->type = ft->type; 707 declare(n, PPARAM); 708 } 709 710 if(t->outtuple) 711 for(ft=getoutargx(t)->type; ft; ft=ft->down) { 712 if(!ft->nname || !ft->nname->sym) 713 continue; 714 n = ft->nname; 715 n->type = ft->type; 716 declare(n, PPARAMOUT); 717 } 718 } 719 720 /* 721 * finish the body. 722 * called in auto-declaration context. 723 * returns in extern-declaration context. 724 */ 725 void 726 funcbody(Node *n) 727 { 728 // change the declaration context from auto to extern 729 if(dclcontext != PAUTO) 730 fatal("funcbody: dclcontext"); 731 popdcl(); 732 funcdepth--; 733 curfn = n->outer; 734 n->outer = N; 735 if(funcdepth == 0) 736 dclcontext = PEXTERN; 737 } 738 739 /* 740 * new type being defined with name s. 741 */ 742 Node* 743 typedcl0(Sym *s) 744 { 745 Node *n; 746 747 n = newname(s); 748 n->op = OTYPE; 749 declare(n, dclcontext); 750 return n; 751 } 752 753 /* 754 * node n, which was returned by typedcl0 755 * is being declared to have uncompiled type t. 756 * return the ODCLTYPE node to use. 757 */ 758 Node* 759 typedcl1(Node *n, Node *t, int local) 760 { 761 n->ntype = t; 762 n->local = local; 763 return nod(ODCLTYPE, n, N); 764 } 765 766 /* 767 * structs, functions, and methods. 768 * they don't belong here, but where do they belong? 769 */ 770 771 static void 772 checkembeddedtype(Type *t) 773 { 774 if (t == T) 775 return; 776 777 if(t->sym == S && isptr[t->etype]) { 778 t = t->type; 779 if(t->etype == TINTER) 780 yyerror("embedded type cannot be a pointer to interface"); 781 } 782 if(isptr[t->etype]) 783 yyerror("embedded type cannot be a pointer"); 784 else if(t->etype == TFORW && t->embedlineno == 0) 785 t->embedlineno = lineno; 786 } 787 788 static Type* 789 structfield(Node *n) 790 { 791 Type *f; 792 int lno; 793 794 lno = lineno; 795 lineno = n->lineno; 796 797 if(n->op != ODCLFIELD) 798 fatal("structfield: oops %N\n", n); 799 800 f = typ(TFIELD); 801 f->isddd = n->isddd; 802 803 if(n->right != N) { 804 typecheck(&n->right, Etype); 805 n->type = n->right->type; 806 if(n->left != N) 807 n->left->type = n->type; 808 if(n->embedded) 809 checkembeddedtype(n->type); 810 } 811 n->right = N; 812 813 f->type = n->type; 814 if(f->type == T) 815 f->broke = 1; 816 817 switch(n->val.ctype) { 818 case CTSTR: 819 f->note = n->val.u.sval; 820 break; 821 default: 822 yyerror("field annotation must be string"); 823 // fallthrough 824 case CTxxx: 825 f->note = nil; 826 break; 827 } 828 829 if(n->left && n->left->op == ONAME) { 830 f->nname = n->left; 831 f->embedded = n->embedded; 832 f->sym = f->nname->sym; 833 } 834 835 lineno = lno; 836 return f; 837 } 838 839 static uint32 uniqgen; 840 841 static void 842 checkdupfields(Type *t, char* what) 843 { 844 int lno; 845 846 lno = lineno; 847 848 for( ; t; t=t->down) { 849 if(t->sym && t->nname && !isblank(t->nname)) { 850 if(t->sym->uniqgen == uniqgen) { 851 lineno = t->nname->lineno; 852 yyerror("duplicate %s %s", what, t->sym->name); 853 } else 854 t->sym->uniqgen = uniqgen; 855 } 856 } 857 858 lineno = lno; 859 } 860 861 /* 862 * convert a parsed id/type list into 863 * a type for struct/interface/arglist 864 */ 865 Type* 866 tostruct(NodeList *l) 867 { 868 Type *t, *f, **tp; 869 t = typ(TSTRUCT); 870 871 for(tp = &t->type; l; l=l->next) { 872 f = structfield(l->n); 873 874 *tp = f; 875 tp = &f->down; 876 } 877 878 for(f=t->type; f && !t->broke; f=f->down) 879 if(f->broke) 880 t->broke = 1; 881 882 uniqgen++; 883 checkdupfields(t->type, "field"); 884 885 if (!t->broke) 886 checkwidth(t); 887 888 return t; 889 } 890 891 static Type* 892 tofunargs(NodeList *l) 893 { 894 Type *t, *f, **tp; 895 896 t = typ(TSTRUCT); 897 t->funarg = 1; 898 899 for(tp = &t->type; l; l=l->next) { 900 f = structfield(l->n); 901 f->funarg = 1; 902 903 // esc.c needs to find f given a PPARAM to add the tag. 904 if(l->n->left && l->n->left->class == PPARAM) 905 l->n->left->paramfld = f; 906 907 *tp = f; 908 tp = &f->down; 909 } 910 911 for(f=t->type; f && !t->broke; f=f->down) 912 if(f->broke) 913 t->broke = 1; 914 915 return t; 916 } 917 918 static Type* 919 interfacefield(Node *n) 920 { 921 Type *f; 922 int lno; 923 924 lno = lineno; 925 lineno = n->lineno; 926 927 if(n->op != ODCLFIELD) 928 fatal("interfacefield: oops %N\n", n); 929 930 if (n->val.ctype != CTxxx) 931 yyerror("interface method cannot have annotation"); 932 933 f = typ(TFIELD); 934 f->isddd = n->isddd; 935 936 if(n->right != N) { 937 if(n->left != N) { 938 // queue resolution of method type for later. 939 // right now all we need is the name list. 940 // avoids cycles for recursive interface types. 941 n->type = typ(TINTERMETH); 942 n->type->nname = n->right; 943 n->left->type = n->type; 944 queuemethod(n); 945 946 if(n->left->op == ONAME) { 947 f->nname = n->left; 948 f->embedded = n->embedded; 949 f->sym = f->nname->sym; 950 } 951 952 } else { 953 954 typecheck(&n->right, Etype); 955 n->type = n->right->type; 956 957 if(n->embedded) 958 checkembeddedtype(n->type); 959 960 if(n->type) 961 switch(n->type->etype) { 962 case TINTER: 963 break; 964 case TFORW: 965 yyerror("interface type loop involving %T", n->type); 966 f->broke = 1; 967 break; 968 default: 969 yyerror("interface contains embedded non-interface %T", n->type); 970 f->broke = 1; 971 break; 972 } 973 } 974 } 975 976 n->right = N; 977 978 f->type = n->type; 979 if(f->type == T) 980 f->broke = 1; 981 982 lineno = lno; 983 return f; 984 } 985 986 Type* 987 tointerface(NodeList *l) 988 { 989 Type *t, *f, **tp, *t1; 990 991 t = typ(TINTER); 992 993 tp = &t->type; 994 for(; l; l=l->next) { 995 f = interfacefield(l->n); 996 997 if (l->n->left == N && f->type->etype == TINTER) { 998 // embedded interface, inline methods 999 for(t1=f->type->type; t1; t1=t1->down) { 1000 f = typ(TFIELD); 1001 f->type = t1->type; 1002 f->broke = t1->broke; 1003 f->sym = t1->sym; 1004 if(f->sym) 1005 f->nname = newname(f->sym); 1006 *tp = f; 1007 tp = &f->down; 1008 } 1009 } else { 1010 *tp = f; 1011 tp = &f->down; 1012 } 1013 } 1014 1015 for(f=t->type; f && !t->broke; f=f->down) 1016 if(f->broke) 1017 t->broke = 1; 1018 1019 uniqgen++; 1020 checkdupfields(t->type, "method"); 1021 t = sortinter(t); 1022 checkwidth(t); 1023 1024 return t; 1025 } 1026 1027 Node* 1028 embedded(Sym *s, Pkg *pkg) 1029 { 1030 Node *n; 1031 char *name; 1032 1033 // Names sometimes have disambiguation junk 1034 // appended after a center dot. Discard it when 1035 // making the name for the embedded struct field. 1036 enum { CenterDot = 0xB7 }; 1037 name = s->name; 1038 if(utfrune(s->name, CenterDot)) { 1039 name = strdup(s->name); 1040 *utfrune(name, CenterDot) = 0; 1041 } 1042 1043 if(exportname(name)) 1044 n = newname(lookup(name)); 1045 else if(s->pkg == builtinpkg) 1046 // The name of embedded builtins belongs to pkg. 1047 n = newname(pkglookup(name, pkg)); 1048 else 1049 n = newname(pkglookup(name, s->pkg)); 1050 n = nod(ODCLFIELD, n, oldname(s)); 1051 n->embedded = 1; 1052 return n; 1053 } 1054 1055 /* 1056 * check that the list of declarations is either all anonymous or all named 1057 */ 1058 1059 static Node* 1060 findtype(NodeList *l) 1061 { 1062 for(; l; l=l->next) 1063 if(l->n->op == OKEY) 1064 return l->n->right; 1065 return N; 1066 } 1067 1068 NodeList* 1069 checkarglist(NodeList *all, int input) 1070 { 1071 int named; 1072 Node *n, *t, *nextt; 1073 NodeList *l; 1074 1075 named = 0; 1076 for(l=all; l; l=l->next) { 1077 if(l->n->op == OKEY) { 1078 named = 1; 1079 break; 1080 } 1081 } 1082 if(named) { 1083 n = N; 1084 for(l=all; l; l=l->next) { 1085 n = l->n; 1086 if(n->op != OKEY && n->sym == S) { 1087 yyerror("mixed named and unnamed function parameters"); 1088 break; 1089 } 1090 } 1091 if(l == nil && n != N && n->op != OKEY) 1092 yyerror("final function parameter must have type"); 1093 } 1094 1095 nextt = nil; 1096 for(l=all; l; l=l->next) { 1097 // can cache result from findtype to avoid 1098 // quadratic behavior here, but unlikely to matter. 1099 n = l->n; 1100 if(named) { 1101 if(n->op == OKEY) { 1102 t = n->right; 1103 n = n->left; 1104 nextt = nil; 1105 } else { 1106 if(nextt == nil) 1107 nextt = findtype(l); 1108 t = nextt; 1109 } 1110 } else { 1111 t = n; 1112 n = N; 1113 } 1114 1115 // during import l->n->op is OKEY, but l->n->left->sym == S 1116 // means it was a '?', not that it was 1117 // a lone type This doesn't matter for the exported 1118 // declarations, which are parsed by rules that don't 1119 // use checkargs, but can happen for func literals in 1120 // the inline bodies. 1121 // TODO(rsc) this can go when typefmt case TFIELD in exportmode fmt.c prints _ instead of ? 1122 if(importpkg && n->sym == S) 1123 n = N; 1124 1125 if(n != N && n->sym == S) { 1126 t = n; 1127 n = N; 1128 } 1129 if(n != N) 1130 n = newname(n->sym); 1131 n = nod(ODCLFIELD, n, t); 1132 if(n->right != N && n->right->op == ODDD) { 1133 if(!input) 1134 yyerror("cannot use ... in output argument list"); 1135 else if(l->next != nil) 1136 yyerror("can only use ... as final argument in list"); 1137 n->right->op = OTARRAY; 1138 n->right->right = n->right->left; 1139 n->right->left = N; 1140 n->isddd = 1; 1141 if(n->left != N) 1142 n->left->isddd = 1; 1143 } 1144 l->n = n; 1145 } 1146 return all; 1147 } 1148 1149 1150 Node* 1151 fakethis(void) 1152 { 1153 Node *n; 1154 1155 n = nod(ODCLFIELD, N, typenod(ptrto(typ(TSTRUCT)))); 1156 return n; 1157 } 1158 1159 /* 1160 * Is this field a method on an interface? 1161 * Those methods have an anonymous 1162 * *struct{} as the receiver. 1163 * (See fakethis above.) 1164 */ 1165 int 1166 isifacemethod(Type *f) 1167 { 1168 Type *rcvr; 1169 Type *t; 1170 1171 rcvr = getthisx(f)->type; 1172 if(rcvr->sym != S) 1173 return 0; 1174 t = rcvr->type; 1175 if(!isptr[t->etype]) 1176 return 0; 1177 t = t->type; 1178 if(t->sym != S || t->etype != TSTRUCT || t->type != T) 1179 return 0; 1180 return 1; 1181 } 1182 1183 /* 1184 * turn a parsed function declaration 1185 * into a type 1186 */ 1187 Type* 1188 functype(Node *this, NodeList *in, NodeList *out) 1189 { 1190 Type *t; 1191 NodeList *rcvr; 1192 Sym *s; 1193 1194 t = typ(TFUNC); 1195 1196 rcvr = nil; 1197 if(this) 1198 rcvr = list1(this); 1199 t->type = tofunargs(rcvr); 1200 t->type->down = tofunargs(out); 1201 t->type->down->down = tofunargs(in); 1202 1203 uniqgen++; 1204 checkdupfields(t->type->type, "argument"); 1205 checkdupfields(t->type->down->type, "argument"); 1206 checkdupfields(t->type->down->down->type, "argument"); 1207 1208 if (t->type->broke || t->type->down->broke || t->type->down->down->broke) 1209 t->broke = 1; 1210 1211 if(this) 1212 t->thistuple = 1; 1213 t->outtuple = count(out); 1214 t->intuple = count(in); 1215 t->outnamed = 0; 1216 if(t->outtuple > 0 && out->n->left != N && out->n->left->orig != N) { 1217 s = out->n->left->orig->sym; 1218 if(s != S && (s->name[0] != '~' || s->name[1] != 'r')) // ~r%d is the name invented for an unnamed result 1219 t->outnamed = 1; 1220 } 1221 1222 return t; 1223 } 1224 1225 Sym* 1226 methodsym(Sym *nsym, Type *t0, int iface) 1227 { 1228 Sym *s; 1229 char *p; 1230 Type *t; 1231 char *suffix; 1232 Pkg *spkg; 1233 static Pkg *toppkg; 1234 1235 t = t0; 1236 if(t == T) 1237 goto bad; 1238 s = t->sym; 1239 if(s == S && isptr[t->etype]) { 1240 t = t->type; 1241 if(t == T) 1242 goto bad; 1243 s = t->sym; 1244 } 1245 spkg = nil; 1246 if(s != S) 1247 spkg = s->pkg; 1248 1249 // if t0 == *t and t0 has a sym, 1250 // we want to see *t, not t0, in the method name. 1251 if(t != t0 && t0->sym) 1252 t0 = ptrto(t); 1253 1254 suffix = ""; 1255 if(iface) { 1256 dowidth(t0); 1257 if(t0->width < types[tptr]->width) 1258 suffix = "·i"; 1259 } 1260 if((spkg == nil || nsym->pkg != spkg) && !exportname(nsym->name)) { 1261 if(t0->sym == S && isptr[t0->etype]) 1262 p = smprint("(%-hT).%s.%s%s", t0, nsym->pkg->prefix, nsym->name, suffix); 1263 else 1264 p = smprint("%-hT.%s.%s%s", t0, nsym->pkg->prefix, nsym->name, suffix); 1265 } else { 1266 if(t0->sym == S && isptr[t0->etype]) 1267 p = smprint("(%-hT).%s%s", t0, nsym->name, suffix); 1268 else 1269 p = smprint("%-hT.%s%s", t0, nsym->name, suffix); 1270 } 1271 if(spkg == nil) { 1272 if(toppkg == nil) 1273 toppkg = mkpkg(strlit("go")); 1274 spkg = toppkg; 1275 } 1276 s = pkglookup(p, spkg); 1277 free(p); 1278 return s; 1279 1280 bad: 1281 yyerror("illegal receiver type: %T", t0); 1282 return S; 1283 } 1284 1285 Node* 1286 methodname(Node *n, Type *t) 1287 { 1288 Sym *s; 1289 1290 s = methodsym(n->sym, t, 0); 1291 if(s == S) 1292 return n; 1293 return newname(s); 1294 } 1295 1296 Node* 1297 methodname1(Node *n, Node *t) 1298 { 1299 char *star; 1300 char *p; 1301 1302 star = nil; 1303 if(t->op == OIND) { 1304 star = "*"; 1305 t = t->left; 1306 } 1307 if(t->sym == S || isblank(n)) 1308 return newname(n->sym); 1309 1310 if(star) 1311 p = smprint("(%s%S).%S", star, t->sym, n->sym); 1312 else 1313 p = smprint("%S.%S", t->sym, n->sym); 1314 1315 if(exportname(t->sym->name)) 1316 n = newname(lookup(p)); 1317 else 1318 n = newname(pkglookup(p, t->sym->pkg)); 1319 free(p); 1320 return n; 1321 } 1322 1323 /* 1324 * add a method, declared as a function, 1325 * n is fieldname, pa is base type, t is function type 1326 */ 1327 void 1328 addmethod(Sym *sf, Type *t, int local, int nointerface) 1329 { 1330 Type *f, *d, *pa; 1331 Node *n; 1332 1333 // get field sym 1334 if(sf == S) 1335 fatal("no method symbol"); 1336 1337 // get parent type sym 1338 pa = getthisx(t)->type; // ptr to this structure 1339 if(pa == T) { 1340 yyerror("missing receiver"); 1341 return; 1342 } 1343 1344 pa = pa->type; 1345 f = methtype(pa, 1); 1346 if(f == T) { 1347 t = pa; 1348 if(t == T) // rely on typecheck having complained before 1349 return; 1350 if(t != T) { 1351 if(isptr[t->etype]) { 1352 if(t->sym != S) { 1353 yyerror("invalid receiver type %T (%T is a pointer type)", pa, t); 1354 return; 1355 } 1356 t = t->type; 1357 } 1358 if(t->broke) // rely on typecheck having complained before 1359 return; 1360 if(t->sym == S) { 1361 yyerror("invalid receiver type %T (%T is an unnamed type)", pa, t); 1362 return; 1363 } 1364 if(isptr[t->etype]) { 1365 yyerror("invalid receiver type %T (%T is a pointer type)", pa, t); 1366 return; 1367 } 1368 if(t->etype == TINTER) { 1369 yyerror("invalid receiver type %T (%T is an interface type)", pa, t); 1370 return; 1371 } 1372 } 1373 // Should have picked off all the reasons above, 1374 // but just in case, fall back to generic error. 1375 yyerror("invalid receiver type %T (%lT / %lT)", pa, pa, t); 1376 return; 1377 } 1378 1379 pa = f; 1380 if(pa->etype == TSTRUCT) { 1381 for(f=pa->type; f; f=f->down) { 1382 if(f->sym == sf) { 1383 yyerror("type %T has both field and method named %S", pa, sf); 1384 return; 1385 } 1386 } 1387 } 1388 1389 if(local && !pa->local) { 1390 // defining method on non-local type. 1391 yyerror("cannot define new methods on non-local type %T", pa); 1392 return; 1393 } 1394 1395 n = nod(ODCLFIELD, newname(sf), N); 1396 n->type = t; 1397 1398 d = T; // last found 1399 for(f=pa->method; f!=T; f=f->down) { 1400 d = f; 1401 if(f->etype != TFIELD) 1402 fatal("addmethod: not TFIELD: %N", f); 1403 if(strcmp(sf->name, f->sym->name) != 0) 1404 continue; 1405 if(!eqtype(t, f->type)) 1406 yyerror("method redeclared: %T.%S\n\t%T\n\t%T", pa, sf, f->type, t); 1407 return; 1408 } 1409 1410 f = structfield(n); 1411 f->nointerface = nointerface; 1412 1413 // during import unexported method names should be in the type's package 1414 if(importpkg && f->sym && !exportname(f->sym->name) && f->sym->pkg != structpkg) 1415 fatal("imported method name %+S in wrong package %s\n", f->sym, structpkg->name); 1416 1417 if(d == T) 1418 pa->method = f; 1419 else 1420 d->down = f; 1421 return; 1422 } 1423 1424 void 1425 funccompile(Node *n, int isclosure) 1426 { 1427 stksize = BADWIDTH; 1428 maxarg = 0; 1429 1430 if(n->type == T) { 1431 if(nerrors == 0) 1432 fatal("funccompile missing type"); 1433 return; 1434 } 1435 1436 // assign parameter offsets 1437 checkwidth(n->type); 1438 1439 // record offset to actual frame pointer. 1440 // for closure, have to skip over leading pointers and PC slot. 1441 // TODO(rsc): this is the old jit closure handling code. 1442 // with the new closures, isclosure is always 0; delete this block. 1443 nodfp->xoffset = 0; 1444 if(isclosure) { 1445 NodeList *l; 1446 for(l=n->nname->ntype->list; l; l=l->next) { 1447 nodfp->xoffset += widthptr; 1448 if(l->n->left == N) // found slot for PC 1449 break; 1450 } 1451 } 1452 1453 if(curfn) 1454 fatal("funccompile %S inside %S", n->nname->sym, curfn->nname->sym); 1455 1456 stksize = 0; 1457 dclcontext = PAUTO; 1458 funcdepth = n->funcdepth + 1; 1459 compile(n); 1460 curfn = nil; 1461 funcdepth = 0; 1462 dclcontext = PEXTERN; 1463 } 1464 1465 Sym* 1466 funcsym(Sym *s) 1467 { 1468 char *p; 1469 Sym *s1; 1470 1471 p = smprint("%s·f", s->name); 1472 s1 = pkglookup(p, s->pkg); 1473 free(p); 1474 if(s1->def == N) { 1475 s1->def = newname(s1); 1476 s1->def->shortname = newname(s); 1477 funcsyms = list(funcsyms, s1->def); 1478 } 1479 return s1; 1480 }