github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/cmd/gc/typecheck.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 /* 6 * type check the whole tree of an expression. 7 * calculates expression types. 8 * evaluates compile time constants. 9 * marks variables that escape the local frame. 10 * rewrites n->op to be more specific in some cases. 11 */ 12 13 #include <u.h> 14 #include <libc.h> 15 #include "go.h" 16 17 static void implicitstar(Node**); 18 static int onearg(Node*, char*, ...); 19 static int twoarg(Node*); 20 static int lookdot(Node*, Type*, int); 21 static int looktypedot(Node*, Type*, int); 22 static void typecheckaste(int, Node*, int, Type*, NodeList*, char*); 23 static Type* lookdot1(Node*, Sym *s, Type *t, Type *f, int); 24 static int nokeys(NodeList*); 25 static void typecheckcomplit(Node**); 26 static void typecheckas2(Node*); 27 static void typecheckas(Node*); 28 static void typecheckfunc(Node*); 29 static void checklvalue(Node*, char*); 30 static void checkassign(Node*); 31 static void checkassignlist(NodeList*); 32 static void stringtoarraylit(Node**); 33 static Node* resolve(Node*); 34 static void checkdefergo(Node*); 35 static int checkmake(Type*, char*, Node*); 36 static int checksliceindex(Node*, Node*, Type*); 37 static int checksliceconst(Node*, Node*); 38 39 static NodeList* typecheckdefstack; 40 41 /* 42 * resolve ONONAME to definition, if any. 43 */ 44 static Node* 45 resolve(Node *n) 46 { 47 Node *r; 48 49 if(n != N && n->op == ONONAME && n->sym != S && (r = n->sym->def) != N) { 50 if(r->op != OIOTA) 51 n = r; 52 else if(n->iota >= 0) 53 n = nodintconst(n->iota); 54 } 55 return n; 56 } 57 58 void 59 typechecklist(NodeList *l, int top) 60 { 61 for(; l; l=l->next) 62 typecheck(&l->n, top); 63 } 64 65 static char* _typekind[] = { 66 [TINT] = "int", 67 [TUINT] = "uint", 68 [TINT8] = "int8", 69 [TUINT8] = "uint8", 70 [TINT16] = "int16", 71 [TUINT16] = "uint16", 72 [TINT32] = "int32", 73 [TUINT32] = "uint32", 74 [TINT64] = "int64", 75 [TUINT64] = "uint64", 76 [TUINTPTR] = "uintptr", 77 [TCOMPLEX64] = "complex64", 78 [TCOMPLEX128] = "complex128", 79 [TFLOAT32] = "float32", 80 [TFLOAT64] = "float64", 81 [TBOOL] = "bool", 82 [TSTRING] = "string", 83 [TPTR32] = "pointer", 84 [TPTR64] = "pointer", 85 [TUNSAFEPTR] = "unsafe.Pointer", 86 [TSTRUCT] = "struct", 87 [TINTER] = "interface", 88 [TCHAN] = "chan", 89 [TMAP] = "map", 90 [TARRAY] = "array", 91 [TFUNC] = "func", 92 [TNIL] = "nil", 93 [TIDEAL] = "untyped number", 94 }; 95 96 static char* 97 typekind(Type *t) 98 { 99 int et; 100 static char buf[50]; 101 char *s; 102 103 if(isslice(t)) 104 return "slice"; 105 et = t->etype; 106 if(0 <= et && et < nelem(_typekind) && (s=_typekind[et]) != nil) 107 return s; 108 snprint(buf, sizeof buf, "etype=%d", et); 109 return buf; 110 } 111 112 /* 113 * sprint_depchain prints a dependency chain 114 * of nodes into fmt. 115 * It is used by typecheck in the case of OLITERAL nodes 116 * to print constant definition loops. 117 */ 118 static void 119 sprint_depchain(Fmt *fmt, NodeList *stack, Node *cur, Node *first) 120 { 121 NodeList *l; 122 123 for(l = stack; l; l=l->next) { 124 if(l->n->op == cur->op) { 125 if(l->n != first) 126 sprint_depchain(fmt, l->next, l->n, first); 127 fmtprint(fmt, "\n\t%L: %N uses %N", l->n->lineno, l->n, cur); 128 return; 129 } 130 } 131 } 132 133 /* 134 * type check node *np. 135 * replaces *np with a new pointer in some cases. 136 * returns the final value of *np as a convenience. 137 */ 138 static void typecheck1(Node **, int); 139 Node* 140 typecheck(Node **np, int top) 141 { 142 Node *n; 143 int lno; 144 Fmt fmt; 145 NodeList *l; 146 static NodeList *tcstack, *tcfree; 147 148 // cannot type check until all the source has been parsed 149 if(!typecheckok) 150 fatal("early typecheck"); 151 152 n = *np; 153 if(n == N) 154 return N; 155 156 lno = setlineno(n); 157 158 // Skip over parens. 159 while(n->op == OPAREN) 160 n = n->left; 161 162 // Resolve definition of name and value of iota lazily. 163 n = resolve(n); 164 165 *np = n; 166 167 // Skip typecheck if already done. 168 // But re-typecheck ONAME/OTYPE/OLITERAL/OPACK node in case context has changed. 169 if(n->typecheck == 1) { 170 switch(n->op) { 171 case ONAME: 172 case OTYPE: 173 case OLITERAL: 174 case OPACK: 175 break; 176 default: 177 lineno = lno; 178 return n; 179 } 180 } 181 182 if(n->typecheck == 2) { 183 // Typechecking loop. Trying printing a meaningful message, 184 // otherwise a stack trace of typechecking. 185 switch(n->op) { 186 case ONAME: 187 // We can already diagnose variables used as types. 188 if((top & (Erv|Etype)) == Etype) 189 yyerror("%N is not a type", n); 190 break; 191 case OLITERAL: 192 if((top & (Erv|Etype)) == Etype) { 193 yyerror("%N is not a type", n); 194 break; 195 } 196 fmtstrinit(&fmt); 197 sprint_depchain(&fmt, tcstack, n, n); 198 yyerrorl(n->lineno, "constant definition loop%s", fmtstrflush(&fmt)); 199 break; 200 } 201 if(nsavederrors+nerrors == 0) { 202 fmtstrinit(&fmt); 203 for(l=tcstack; l; l=l->next) 204 fmtprint(&fmt, "\n\t%L %N", l->n->lineno, l->n); 205 yyerror("typechecking loop involving %N%s", n, fmtstrflush(&fmt)); 206 } 207 lineno = lno; 208 return n; 209 } 210 n->typecheck = 2; 211 212 if(tcfree != nil) { 213 l = tcfree; 214 tcfree = l->next; 215 } else 216 l = mal(sizeof *l); 217 l->next = tcstack; 218 l->n = n; 219 tcstack = l; 220 221 typecheck1(&n, top); 222 *np = n; 223 n->typecheck = 1; 224 225 if(tcstack != l) 226 fatal("typecheck stack out of sync"); 227 tcstack = l->next; 228 l->next = tcfree; 229 tcfree = l; 230 231 lineno = lno; 232 return n; 233 } 234 235 /* 236 * does n contain a call or receive operation? 237 */ 238 static int callrecvlist(NodeList*); 239 240 static int 241 callrecv(Node *n) 242 { 243 if(n == nil) 244 return 0; 245 246 switch(n->op) { 247 case OCALL: 248 case OCALLMETH: 249 case OCALLINTER: 250 case OCALLFUNC: 251 case ORECV: 252 case OCAP: 253 case OLEN: 254 case OCOPY: 255 case ONEW: 256 case OAPPEND: 257 case ODELETE: 258 return 1; 259 } 260 261 return callrecv(n->left) || 262 callrecv(n->right) || 263 callrecv(n->ntest) || 264 callrecv(n->nincr) || 265 callrecvlist(n->ninit) || 266 callrecvlist(n->nbody) || 267 callrecvlist(n->nelse) || 268 callrecvlist(n->list) || 269 callrecvlist(n->rlist); 270 } 271 272 static int 273 callrecvlist(NodeList *l) 274 { 275 for(; l; l=l->next) 276 if(callrecv(l->n)) 277 return 1; 278 return 0; 279 } 280 281 // indexlit implements typechecking of untyped values as 282 // array/slice indexes. It is equivalent to defaultlit 283 // except for constants of numerical kind, which are acceptable 284 // whenever they can be represented by a value of type int. 285 static void 286 indexlit(Node **np) 287 { 288 Node *n; 289 290 n = *np; 291 if(n == N || !isideal(n->type)) 292 return; 293 switch(consttype(n)) { 294 case CTINT: 295 case CTRUNE: 296 case CTFLT: 297 case CTCPLX: 298 defaultlit(np, types[TINT]); 299 break; 300 } 301 defaultlit(np, T); 302 } 303 304 static void 305 typecheck1(Node **np, int top) 306 { 307 int et, aop, op, ptr; 308 Node *n, *l, *r, *lo, *mid, *hi; 309 NodeList *args; 310 int ok, ntop; 311 Type *t, *tp, *missing, *have, *badtype; 312 Val v; 313 char *why, *desc, descbuf[64]; 314 vlong x; 315 316 n = *np; 317 318 if(n->sym) { 319 if(n->op == ONAME && n->etype != 0 && !(top & Ecall)) { 320 yyerror("use of builtin %S not in function call", n->sym); 321 goto error; 322 } 323 324 typecheckdef(n); 325 if(n->op == ONONAME) 326 goto error; 327 } 328 *np = n; 329 330 reswitch: 331 ok = 0; 332 switch(n->op) { 333 default: 334 // until typecheck is complete, do nothing. 335 dump("typecheck", n); 336 fatal("typecheck %O", n->op); 337 338 /* 339 * names 340 */ 341 case OLITERAL: 342 ok |= Erv; 343 if(n->type == T && n->val.ctype == CTSTR) 344 n->type = idealstring; 345 goto ret; 346 347 case ONONAME: 348 ok |= Erv; 349 goto ret; 350 351 case ONAME: 352 if(n->etype != 0) { 353 ok |= Ecall; 354 goto ret; 355 } 356 if(!(top & Easgn)) { 357 // not a write to the variable 358 if(isblank(n)) { 359 yyerror("cannot use _ as value"); 360 goto error; 361 } 362 n->used = 1; 363 } 364 if(!(top &Ecall) && isunsafebuiltin(n)) { 365 yyerror("%N is not an expression, must be called", n); 366 goto error; 367 } 368 ok |= Erv; 369 goto ret; 370 371 case OPACK: 372 yyerror("use of package %S without selector", n->sym); 373 goto error; 374 375 case ODDD: 376 break; 377 378 /* 379 * types (OIND is with exprs) 380 */ 381 case OTYPE: 382 ok |= Etype; 383 if(n->type == T) 384 goto error; 385 break; 386 387 case OTARRAY: 388 ok |= Etype; 389 t = typ(TARRAY); 390 l = n->left; 391 r = n->right; 392 if(l == nil) { 393 t->bound = -1; // slice 394 } else if(l->op == ODDD) { 395 t->bound = -100; // to be filled in 396 if(!(top&Ecomplit) && !n->diag) { 397 t->broke = 1; 398 n->diag = 1; 399 yyerror("use of [...] array outside of array literal"); 400 } 401 } else { 402 l = typecheck(&n->left, Erv); 403 switch(consttype(l)) { 404 case CTINT: 405 case CTRUNE: 406 v = l->val; 407 break; 408 case CTFLT: 409 v = toint(l->val); 410 break; 411 default: 412 if(l->type != T && isint[l->type->etype] && l->op != OLITERAL) 413 yyerror("non-constant array bound %N", l); 414 else 415 yyerror("invalid array bound %N", l); 416 goto error; 417 } 418 t->bound = mpgetfix(v.u.xval); 419 if(doesoverflow(v, types[TINT])) { 420 yyerror("array bound is too large"); 421 goto error; 422 } else if(t->bound < 0) { 423 yyerror("array bound must be non-negative"); 424 goto error; 425 } 426 } 427 typecheck(&r, Etype); 428 if(r->type == T) 429 goto error; 430 t->type = r->type; 431 n->op = OTYPE; 432 n->type = t; 433 n->left = N; 434 n->right = N; 435 if(t->bound != -100) 436 checkwidth(t); 437 break; 438 439 case OTMAP: 440 ok |= Etype; 441 l = typecheck(&n->left, Etype); 442 r = typecheck(&n->right, Etype); 443 if(l->type == T || r->type == T) 444 goto error; 445 n->op = OTYPE; 446 n->type = maptype(l->type, r->type); 447 n->left = N; 448 n->right = N; 449 break; 450 451 case OTCHAN: 452 ok |= Etype; 453 l = typecheck(&n->left, Etype); 454 if(l->type == T) 455 goto error; 456 t = typ(TCHAN); 457 t->type = l->type; 458 t->chan = n->etype; 459 n->op = OTYPE; 460 n->type = t; 461 n->left = N; 462 n->etype = 0; 463 break; 464 465 case OTSTRUCT: 466 ok |= Etype; 467 n->op = OTYPE; 468 n->type = tostruct(n->list); 469 if(n->type == T || n->type->broke) 470 goto error; 471 n->list = nil; 472 break; 473 474 case OTINTER: 475 ok |= Etype; 476 n->op = OTYPE; 477 n->type = tointerface(n->list); 478 if(n->type == T) 479 goto error; 480 break; 481 482 case OTFUNC: 483 ok |= Etype; 484 n->op = OTYPE; 485 n->type = functype(n->left, n->list, n->rlist); 486 if(n->type == T) 487 goto error; 488 break; 489 490 /* 491 * type or expr 492 */ 493 case OIND: 494 ntop = Erv | Etype; 495 if(!(top & Eaddr)) // The *x in &*x is not an indirect. 496 ntop |= Eindir; 497 ntop |= top & Ecomplit; 498 l = typecheck(&n->left, ntop); 499 if((t = l->type) == T) 500 goto error; 501 if(l->op == OTYPE) { 502 ok |= Etype; 503 n->op = OTYPE; 504 n->type = ptrto(l->type); 505 n->left = N; 506 goto ret; 507 } 508 if(!isptr[t->etype]) { 509 if(top & (Erv | Etop)) { 510 yyerror("invalid indirect of %lN", n->left); 511 goto error; 512 } 513 goto ret; 514 } 515 ok |= Erv; 516 n->type = t->type; 517 goto ret; 518 519 /* 520 * arithmetic exprs 521 */ 522 case OASOP: 523 ok |= Etop; 524 l = typecheck(&n->left, Erv); 525 checkassign(n->left); 526 r = typecheck(&n->right, Erv); 527 if(l->type == T || r->type == T) 528 goto error; 529 op = n->etype; 530 goto arith; 531 532 case OADD: 533 case OAND: 534 case OANDAND: 535 case OANDNOT: 536 case ODIV: 537 case OEQ: 538 case OGE: 539 case OGT: 540 case OLE: 541 case OLT: 542 case OLSH: 543 case ORSH: 544 case OMOD: 545 case OMUL: 546 case ONE: 547 case OOR: 548 case OOROR: 549 case OSUB: 550 case OXOR: 551 ok |= Erv; 552 l = typecheck(&n->left, Erv | (top & Eiota)); 553 r = typecheck(&n->right, Erv | (top & Eiota)); 554 if(l->type == T || r->type == T) 555 goto error; 556 op = n->op; 557 arith: 558 if(op == OLSH || op == ORSH) 559 goto shift; 560 // ideal mixed with non-ideal 561 defaultlit2(&l, &r, 0); 562 n->left = l; 563 n->right = r; 564 if(l->type == T || r->type == T) 565 goto error; 566 t = l->type; 567 if(t->etype == TIDEAL) 568 t = r->type; 569 et = t->etype; 570 if(et == TIDEAL) 571 et = TINT; 572 if(iscmp[n->op] && t->etype != TIDEAL && !eqtype(l->type, r->type)) { 573 // comparison is okay as long as one side is 574 // assignable to the other. convert so they have 575 // the same type. 576 // 577 // the only conversion that isn't a no-op is concrete == interface. 578 // in that case, check comparability of the concrete type. 579 if(r->type->etype != TBLANK && (aop = assignop(l->type, r->type, nil)) != 0) { 580 if(isinter(r->type) && !isinter(l->type) && algtype1(l->type, nil) == ANOEQ) { 581 yyerror("invalid operation: %N (operator %O not defined on %s)", n, op, typekind(l->type)); 582 goto error; 583 } 584 l = nod(aop, l, N); 585 l->type = r->type; 586 l->typecheck = 1; 587 n->left = l; 588 t = l->type; 589 } else if(l->type->etype != TBLANK && (aop = assignop(r->type, l->type, nil)) != 0) { 590 if(isinter(l->type) && !isinter(r->type) && algtype1(r->type, nil) == ANOEQ) { 591 yyerror("invalid operation: %N (operator %O not defined on %s)", n, op, typekind(r->type)); 592 goto error; 593 } 594 r = nod(aop, r, N); 595 r->type = l->type; 596 r->typecheck = 1; 597 n->right = r; 598 t = r->type; 599 } 600 et = t->etype; 601 } 602 if(t->etype != TIDEAL && !eqtype(l->type, r->type)) { 603 defaultlit2(&l, &r, 1); 604 if(n->op == OASOP && n->implicit) { 605 yyerror("invalid operation: %N (non-numeric type %T)", n, l->type); 606 goto error; 607 } 608 yyerror("invalid operation: %N (mismatched types %T and %T)", n, l->type, r->type); 609 goto error; 610 } 611 if(!okfor[op][et]) { 612 yyerror("invalid operation: %N (operator %O not defined on %s)", n, op, typekind(t)); 613 goto error; 614 } 615 // okfor allows any array == array, map == map, func == func. 616 // restrict to slice/map/func == nil and nil == slice/map/func. 617 if(isfixedarray(l->type) && algtype1(l->type, nil) == ANOEQ) { 618 yyerror("invalid operation: %N (%T cannot be compared)", n, l->type); 619 goto error; 620 } 621 if(isslice(l->type) && !isnil(l) && !isnil(r)) { 622 yyerror("invalid operation: %N (slice can only be compared to nil)", n); 623 goto error; 624 } 625 if(l->type->etype == TMAP && !isnil(l) && !isnil(r)) { 626 yyerror("invalid operation: %N (map can only be compared to nil)", n); 627 goto error; 628 } 629 if(l->type->etype == TFUNC && !isnil(l) && !isnil(r)) { 630 yyerror("invalid operation: %N (func can only be compared to nil)", n); 631 goto error; 632 } 633 if(l->type->etype == TSTRUCT && algtype1(l->type, &badtype) == ANOEQ) { 634 yyerror("invalid operation: %N (struct containing %T cannot be compared)", n, badtype); 635 goto error; 636 } 637 638 t = l->type; 639 if(iscmp[n->op]) { 640 evconst(n); 641 t = idealbool; 642 if(n->op != OLITERAL) { 643 defaultlit2(&l, &r, 1); 644 n->left = l; 645 n->right = r; 646 } 647 // non-comparison operators on ideal bools should make them lose their ideal-ness 648 } else if(t == idealbool) 649 t = types[TBOOL]; 650 651 if(et == TSTRING) { 652 if(iscmp[n->op]) { 653 n->etype = n->op; 654 n->op = OCMPSTR; 655 } else if(n->op == OADD) { 656 // create OADDSTR node with list of strings in x + y + z + (w + v) + ... 657 n->op = OADDSTR; 658 if(l->op == OADDSTR) 659 n->list = l->list; 660 else 661 n->list = list1(l); 662 if(r->op == OADDSTR) 663 n->list = concat(n->list, r->list); 664 else 665 n->list = list(n->list, r); 666 n->left = N; 667 n->right = N; 668 } 669 } 670 if(et == TINTER) { 671 if(l->op == OLITERAL && l->val.ctype == CTNIL) { 672 // swap for back end 673 n->left = r; 674 n->right = l; 675 } else if(r->op == OLITERAL && r->val.ctype == CTNIL) { 676 // leave alone for back end 677 } else { 678 n->etype = n->op; 679 n->op = OCMPIFACE; 680 } 681 } 682 683 if((op == ODIV || op == OMOD) && isconst(r, CTINT)) 684 if(mpcmpfixc(r->val.u.xval, 0) == 0) { 685 yyerror("division by zero"); 686 goto error; 687 } 688 689 n->type = t; 690 goto ret; 691 692 shift: 693 defaultlit(&r, types[TUINT]); 694 n->right = r; 695 t = r->type; 696 if(!isint[t->etype] || issigned[t->etype]) { 697 yyerror("invalid operation: %N (shift count type %T, must be unsigned integer)", n, r->type); 698 goto error; 699 } 700 t = l->type; 701 if(t != T && t->etype != TIDEAL && !isint[t->etype]) { 702 yyerror("invalid operation: %N (shift of type %T)", n, t); 703 goto error; 704 } 705 // no defaultlit for left 706 // the outer context gives the type 707 n->type = l->type; 708 goto ret; 709 710 case OCOM: 711 case OMINUS: 712 case ONOT: 713 case OPLUS: 714 ok |= Erv; 715 l = typecheck(&n->left, Erv | (top & Eiota)); 716 if((t = l->type) == T) 717 goto error; 718 if(!okfor[n->op][t->etype]) { 719 yyerror("invalid operation: %O %T", n->op, t); 720 goto error; 721 } 722 n->type = t; 723 goto ret; 724 725 /* 726 * exprs 727 */ 728 case OADDR: 729 ok |= Erv; 730 typecheck(&n->left, Erv | Eaddr); 731 if(n->left->type == T) 732 goto error; 733 checklvalue(n->left, "take the address of"); 734 r = outervalue(n->left); 735 for(l = n->left; l != r; l = l->left) 736 l->addrtaken = 1; 737 if(l->orig != l && l->op == ONAME) 738 fatal("found non-orig name node %N", l); 739 l->addrtaken = 1; 740 defaultlit(&n->left, T); 741 l = n->left; 742 if((t = l->type) == T) 743 goto error; 744 n->type = ptrto(t); 745 goto ret; 746 747 case OCOMPLIT: 748 ok |= Erv; 749 typecheckcomplit(&n); 750 if(n->type == T) 751 goto error; 752 goto ret; 753 754 case OXDOT: 755 n = adddot(n); 756 n->op = ODOT; 757 if(n->left == N) 758 goto error; 759 // fall through 760 case ODOT: 761 typecheck(&n->left, Erv|Etype); 762 defaultlit(&n->left, T); 763 if((t = n->left->type) == T) 764 goto error; 765 if(n->right->op != ONAME) { 766 yyerror("rhs of . must be a name"); // impossible 767 goto error; 768 } 769 r = n->right; 770 771 if(n->left->op == OTYPE) { 772 if(!looktypedot(n, t, 0)) { 773 if(looktypedot(n, t, 1)) 774 yyerror("%N undefined (cannot refer to unexported method %S)", n, n->right->sym); 775 else 776 yyerror("%N undefined (type %T has no method %S)", n, t, n->right->sym); 777 goto error; 778 } 779 if(n->type->etype != TFUNC || n->type->thistuple != 1) { 780 yyerror("type %T has no method %hS", n->left->type, n->right->sym); 781 n->type = T; 782 goto error; 783 } 784 n->op = ONAME; 785 n->sym = n->right->sym; 786 n->type = methodfunc(n->type, n->left->type); 787 n->xoffset = 0; 788 n->class = PFUNC; 789 ok = Erv; 790 goto ret; 791 } 792 if(isptr[t->etype] && t->type->etype != TINTER) { 793 t = t->type; 794 if(t == T) 795 goto error; 796 n->op = ODOTPTR; 797 checkwidth(t); 798 } 799 if(isblank(n->right)) { 800 yyerror("cannot refer to blank field or method"); 801 goto error; 802 } 803 if(!lookdot(n, t, 0)) { 804 if(lookdot(n, t, 1)) 805 yyerror("%N undefined (cannot refer to unexported field or method %S)", n, n->right->sym); 806 else 807 yyerror("%N undefined (type %T has no field or method %S)", n, n->left->type, n->right->sym); 808 goto error; 809 } 810 switch(n->op) { 811 case ODOTINTER: 812 case ODOTMETH: 813 if(top&Ecall) 814 ok |= Ecall; 815 else { 816 typecheckpartialcall(n, r); 817 ok |= Erv; 818 } 819 break; 820 default: 821 ok |= Erv; 822 break; 823 } 824 goto ret; 825 826 case ODOTTYPE: 827 ok |= Erv; 828 typecheck(&n->left, Erv); 829 defaultlit(&n->left, T); 830 l = n->left; 831 if((t = l->type) == T) 832 goto error; 833 if(!isinter(t)) { 834 yyerror("invalid type assertion: %N (non-interface type %T on left)", n, t); 835 goto error; 836 } 837 if(n->right != N) { 838 typecheck(&n->right, Etype); 839 n->type = n->right->type; 840 n->right = N; 841 if(n->type == T) 842 goto error; 843 } 844 if(n->type != T && n->type->etype != TINTER) 845 if(!implements(n->type, t, &missing, &have, &ptr)) { 846 if(have && have->sym == missing->sym) 847 yyerror("impossible type assertion:\n\t%T does not implement %T (wrong type for %S method)\n" 848 "\t\thave %S%hhT\n\t\twant %S%hhT", n->type, t, missing->sym, 849 have->sym, have->type, missing->sym, missing->type); 850 else if(ptr) 851 yyerror("impossible type assertion:\n\t%T does not implement %T (%S method has pointer receiver)", 852 n->type, t, missing->sym); 853 else if(have) 854 yyerror("impossible type assertion:\n\t%T does not implement %T (missing %S method)\n" 855 "\t\thave %S%hhT\n\t\twant %S%hhT", n->type, t, missing->sym, 856 have->sym, have->type, missing->sym, missing->type); 857 else 858 yyerror("impossible type assertion:\n\t%T does not implement %T (missing %S method)", 859 n->type, t, missing->sym); 860 goto error; 861 } 862 goto ret; 863 864 case OINDEX: 865 ok |= Erv; 866 typecheck(&n->left, Erv); 867 defaultlit(&n->left, T); 868 implicitstar(&n->left); 869 l = n->left; 870 typecheck(&n->right, Erv); 871 r = n->right; 872 if((t = l->type) == T || r->type == T) 873 goto error; 874 switch(t->etype) { 875 default: 876 yyerror("invalid operation: %N (type %T does not support indexing)", n, t); 877 goto error; 878 879 880 case TSTRING: 881 case TARRAY: 882 indexlit(&n->right); 883 if(t->etype == TSTRING) 884 n->type = types[TUINT8]; 885 else 886 n->type = t->type; 887 why = "string"; 888 if(t->etype == TARRAY) { 889 if(isfixedarray(t)) 890 why = "array"; 891 else 892 why = "slice"; 893 } 894 if(n->right->type != T && !isint[n->right->type->etype]) { 895 yyerror("non-integer %s index %N", why, n->right); 896 break; 897 } 898 if(isconst(n->right, CTINT)) { 899 x = mpgetfix(n->right->val.u.xval); 900 if(x < 0) 901 yyerror("invalid %s index %N (index must be non-negative)", why, n->right); 902 else if(isfixedarray(t) && t->bound > 0 && x >= t->bound) 903 yyerror("invalid array index %N (out of bounds for %d-element array)", n->right, t->bound); 904 else if(isconst(n->left, CTSTR) && x >= n->left->val.u.sval->len) 905 yyerror("invalid string index %N (out of bounds for %d-byte string)", n->right, n->left->val.u.sval->len); 906 else if(mpcmpfixfix(n->right->val.u.xval, maxintval[TINT]) > 0) 907 yyerror("invalid %s index %N (index too large)", why, n->right); 908 } 909 break; 910 911 case TMAP: 912 n->etype = 0; 913 defaultlit(&n->right, t->down); 914 if(n->right->type != T) 915 n->right = assignconv(n->right, t->down, "map index"); 916 n->type = t->type; 917 n->op = OINDEXMAP; 918 break; 919 } 920 goto ret; 921 922 case ORECV: 923 ok |= Etop | Erv; 924 typecheck(&n->left, Erv); 925 defaultlit(&n->left, T); 926 l = n->left; 927 if((t = l->type) == T) 928 goto error; 929 if(t->etype != TCHAN) { 930 yyerror("invalid operation: %N (receive from non-chan type %T)", n, t); 931 goto error; 932 } 933 if(!(t->chan & Crecv)) { 934 yyerror("invalid operation: %N (receive from send-only type %T)", n, t); 935 goto error; 936 } 937 n->type = t->type; 938 goto ret; 939 940 case OSEND: 941 ok |= Etop; 942 l = typecheck(&n->left, Erv); 943 typecheck(&n->right, Erv); 944 defaultlit(&n->left, T); 945 l = n->left; 946 if((t = l->type) == T) 947 goto error; 948 if(t->etype != TCHAN) { 949 yyerror("invalid operation: %N (send to non-chan type %T)", n, t); 950 goto error; 951 } 952 if(!(t->chan & Csend)) { 953 yyerror("invalid operation: %N (send to receive-only type %T)", n, t); 954 goto error; 955 } 956 defaultlit(&n->right, t->type); 957 r = n->right; 958 if(r->type == T) 959 goto error; 960 n->right = assignconv(r, l->type->type, "send"); 961 // TODO: more aggressive 962 n->etype = 0; 963 n->type = T; 964 goto ret; 965 966 case OSLICE: 967 ok |= Erv; 968 typecheck(&n->left, top); 969 typecheck(&n->right->left, Erv); 970 typecheck(&n->right->right, Erv); 971 defaultlit(&n->left, T); 972 indexlit(&n->right->left); 973 indexlit(&n->right->right); 974 l = n->left; 975 if(isfixedarray(l->type)) { 976 if(!islvalue(n->left)) { 977 yyerror("invalid operation %N (slice of unaddressable value)", n); 978 goto error; 979 } 980 n->left = nod(OADDR, n->left, N); 981 n->left->implicit = 1; 982 typecheck(&n->left, Erv); 983 l = n->left; 984 } 985 if((t = l->type) == T) 986 goto error; 987 tp = nil; 988 if(istype(t, TSTRING)) { 989 n->type = t; 990 n->op = OSLICESTR; 991 } else if(isptr[t->etype] && isfixedarray(t->type)) { 992 tp = t->type; 993 n->type = typ(TARRAY); 994 n->type->type = tp->type; 995 n->type->bound = -1; 996 dowidth(n->type); 997 n->op = OSLICEARR; 998 } else if(isslice(t)) { 999 n->type = t; 1000 } else { 1001 yyerror("cannot slice %N (type %T)", l, t); 1002 goto error; 1003 } 1004 if((lo = n->right->left) != N && checksliceindex(l, lo, tp) < 0) 1005 goto error; 1006 if((hi = n->right->right) != N && checksliceindex(l, hi, tp) < 0) 1007 goto error; 1008 if(checksliceconst(lo, hi) < 0) 1009 goto error; 1010 goto ret; 1011 1012 case OSLICE3: 1013 ok |= Erv; 1014 typecheck(&n->left, top); 1015 typecheck(&n->right->left, Erv); 1016 typecheck(&n->right->right->left, Erv); 1017 typecheck(&n->right->right->right, Erv); 1018 defaultlit(&n->left, T); 1019 indexlit(&n->right->left); 1020 indexlit(&n->right->right->left); 1021 indexlit(&n->right->right->right); 1022 l = n->left; 1023 if(isfixedarray(l->type)) { 1024 if(!islvalue(n->left)) { 1025 yyerror("invalid operation %N (slice of unaddressable value)", n); 1026 goto error; 1027 } 1028 n->left = nod(OADDR, n->left, N); 1029 n->left->implicit = 1; 1030 typecheck(&n->left, Erv); 1031 l = n->left; 1032 } 1033 if((t = l->type) == T) 1034 goto error; 1035 tp = nil; 1036 if(istype(t, TSTRING)) { 1037 yyerror("invalid operation %N (3-index slice of string)", n); 1038 goto error; 1039 } 1040 if(isptr[t->etype] && isfixedarray(t->type)) { 1041 tp = t->type; 1042 n->type = typ(TARRAY); 1043 n->type->type = tp->type; 1044 n->type->bound = -1; 1045 dowidth(n->type); 1046 n->op = OSLICE3ARR; 1047 } else if(isslice(t)) { 1048 n->type = t; 1049 } else { 1050 yyerror("cannot slice %N (type %T)", l, t); 1051 goto error; 1052 } 1053 if((lo = n->right->left) != N && checksliceindex(l, lo, tp) < 0) 1054 goto error; 1055 if((mid = n->right->right->left) != N && checksliceindex(l, mid, tp) < 0) 1056 goto error; 1057 if((hi = n->right->right->right) != N && checksliceindex(l, hi, tp) < 0) 1058 goto error; 1059 if(checksliceconst(lo, hi) < 0 || checksliceconst(lo, mid) < 0 || checksliceconst(mid, hi) < 0) 1060 goto error; 1061 goto ret; 1062 1063 /* 1064 * call and call like 1065 */ 1066 case OCALL: 1067 l = n->left; 1068 if(l->op == ONAME && (r = unsafenmagic(n)) != N) { 1069 if(n->isddd) 1070 yyerror("invalid use of ... with builtin %N", l); 1071 n = r; 1072 goto reswitch; 1073 } 1074 typecheck(&n->left, Erv | Etype | Ecall |(top&Eproc)); 1075 n->diag |= n->left->diag; 1076 l = n->left; 1077 if(l->op == ONAME && l->etype != 0) { 1078 if(n->isddd && l->etype != OAPPEND) 1079 yyerror("invalid use of ... with builtin %N", l); 1080 // builtin: OLEN, OCAP, etc. 1081 n->op = l->etype; 1082 n->left = n->right; 1083 n->right = N; 1084 goto reswitch; 1085 } 1086 defaultlit(&n->left, T); 1087 l = n->left; 1088 if(l->op == OTYPE) { 1089 if(n->isddd || l->type->bound == -100) { 1090 if(!l->type->broke) 1091 yyerror("invalid use of ... in type conversion", l); 1092 n->diag = 1; 1093 } 1094 // pick off before type-checking arguments 1095 ok |= Erv; 1096 // turn CALL(type, arg) into CONV(arg) w/ type 1097 n->left = N; 1098 n->op = OCONV; 1099 n->type = l->type; 1100 if(onearg(n, "conversion to %T", l->type) < 0) 1101 goto error; 1102 goto doconv; 1103 } 1104 1105 if(count(n->list) == 1 && !n->isddd) 1106 typecheck(&n->list->n, Erv | Efnstruct); 1107 else 1108 typechecklist(n->list, Erv); 1109 if((t = l->type) == T) 1110 goto error; 1111 checkwidth(t); 1112 1113 switch(l->op) { 1114 case ODOTINTER: 1115 n->op = OCALLINTER; 1116 break; 1117 1118 case ODOTMETH: 1119 n->op = OCALLMETH; 1120 // typecheckaste was used here but there wasn't enough 1121 // information further down the call chain to know if we 1122 // were testing a method receiver for unexported fields. 1123 // It isn't necessary, so just do a sanity check. 1124 tp = getthisx(t)->type->type; 1125 if(l->left == N || !eqtype(l->left->type, tp)) 1126 fatal("method receiver"); 1127 break; 1128 1129 default: 1130 n->op = OCALLFUNC; 1131 if(t->etype != TFUNC) { 1132 yyerror("cannot call non-function %N (type %T)", l, t); 1133 goto error; 1134 } 1135 break; 1136 } 1137 if(snprint(descbuf, sizeof descbuf, "argument to %N", n->left) < sizeof descbuf) 1138 desc = descbuf; 1139 else 1140 desc = "function argument"; 1141 typecheckaste(OCALL, n->left, n->isddd, getinargx(t), n->list, desc); 1142 ok |= Etop; 1143 if(t->outtuple == 0) 1144 goto ret; 1145 ok |= Erv; 1146 if(t->outtuple == 1) { 1147 t = getoutargx(l->type)->type; 1148 if(t == T) 1149 goto error; 1150 if(t->etype == TFIELD) 1151 t = t->type; 1152 n->type = t; 1153 goto ret; 1154 } 1155 // multiple return 1156 if(!(top & (Efnstruct | Etop))) { 1157 yyerror("multiple-value %N() in single-value context", l); 1158 goto ret; 1159 } 1160 n->type = getoutargx(l->type); 1161 goto ret; 1162 1163 case OCAP: 1164 case OLEN: 1165 case OREAL: 1166 case OIMAG: 1167 ok |= Erv; 1168 if(onearg(n, "%O", n->op) < 0) 1169 goto error; 1170 typecheck(&n->left, Erv); 1171 defaultlit(&n->left, T); 1172 implicitstar(&n->left); 1173 l = n->left; 1174 t = l->type; 1175 if(t == T) 1176 goto error; 1177 switch(n->op) { 1178 case OCAP: 1179 if(!okforcap[t->etype]) 1180 goto badcall1; 1181 break; 1182 case OLEN: 1183 if(!okforlen[t->etype]) 1184 goto badcall1; 1185 break; 1186 case OREAL: 1187 case OIMAG: 1188 if(!iscomplex[t->etype]) 1189 goto badcall1; 1190 if(isconst(l, CTCPLX)){ 1191 r = n; 1192 if(n->op == OREAL) 1193 n = nodfltconst(&l->val.u.cval->real); 1194 else 1195 n = nodfltconst(&l->val.u.cval->imag); 1196 n->orig = r; 1197 } 1198 n->type = types[cplxsubtype(t->etype)]; 1199 goto ret; 1200 } 1201 // might be constant 1202 switch(t->etype) { 1203 case TSTRING: 1204 if(isconst(l, CTSTR)) { 1205 r = nod(OXXX, N, N); 1206 nodconst(r, types[TINT], l->val.u.sval->len); 1207 r->orig = n; 1208 n = r; 1209 } 1210 break; 1211 case TARRAY: 1212 if(t->bound < 0) // slice 1213 break; 1214 if(callrecv(l)) // has call or receive 1215 break; 1216 r = nod(OXXX, N, N); 1217 nodconst(r, types[TINT], t->bound); 1218 r->orig = n; 1219 n = r; 1220 break; 1221 } 1222 n->type = types[TINT]; 1223 goto ret; 1224 1225 case OCOMPLEX: 1226 ok |= Erv; 1227 if(count(n->list) == 1) { 1228 typechecklist(n->list, Efnstruct); 1229 t = n->list->n->left->type; 1230 if(t->outtuple != 2) { 1231 yyerror("invalid operation: complex expects two arguments, %N returns %d results", n->list->n, t->outtuple); 1232 goto error; 1233 } 1234 t = n->list->n->type->type; 1235 l = t->nname; 1236 r = t->down->nname; 1237 } else { 1238 if(twoarg(n) < 0) 1239 goto error; 1240 l = typecheck(&n->left, Erv | (top & Eiota)); 1241 r = typecheck(&n->right, Erv | (top & Eiota)); 1242 if(l->type == T || r->type == T) 1243 goto error; 1244 defaultlit2(&l, &r, 0); 1245 if(l->type == T || r->type == T) 1246 goto error; 1247 n->left = l; 1248 n->right = r; 1249 } 1250 if(!eqtype(l->type, r->type)) { 1251 yyerror("invalid operation: %N (mismatched types %T and %T)", n, l->type, r->type); 1252 goto error; 1253 } 1254 switch(l->type->etype) { 1255 default: 1256 yyerror("invalid operation: %N (arguments have type %T, expected floating-point)", n, l->type, r->type); 1257 goto error; 1258 case TIDEAL: 1259 t = types[TIDEAL]; 1260 break; 1261 case TFLOAT32: 1262 t = types[TCOMPLEX64]; 1263 break; 1264 case TFLOAT64: 1265 t = types[TCOMPLEX128]; 1266 break; 1267 } 1268 if(l->op == OLITERAL && r->op == OLITERAL) { 1269 // make it a complex literal 1270 r = nodcplxlit(l->val, r->val); 1271 r->orig = n; 1272 n = r; 1273 } 1274 n->type = t; 1275 goto ret; 1276 1277 case OCLOSE: 1278 if(onearg(n, "%O", n->op) < 0) 1279 goto error; 1280 typecheck(&n->left, Erv); 1281 defaultlit(&n->left, T); 1282 l = n->left; 1283 if((t = l->type) == T) 1284 goto error; 1285 if(t->etype != TCHAN) { 1286 yyerror("invalid operation: %N (non-chan type %T)", n, t); 1287 goto error; 1288 } 1289 if(!(t->chan & Csend)) { 1290 yyerror("invalid operation: %N (cannot close receive-only channel)", n); 1291 goto error; 1292 } 1293 ok |= Etop; 1294 goto ret; 1295 1296 case ODELETE: 1297 args = n->list; 1298 if(args == nil) { 1299 yyerror("missing arguments to delete"); 1300 goto error; 1301 } 1302 if(args->next == nil) { 1303 yyerror("missing second (key) argument to delete"); 1304 goto error; 1305 } 1306 if(args->next->next != nil) { 1307 yyerror("too many arguments to delete"); 1308 goto error; 1309 } 1310 ok |= Etop; 1311 typechecklist(args, Erv); 1312 l = args->n; 1313 r = args->next->n; 1314 if(l->type != T && l->type->etype != TMAP) { 1315 yyerror("first argument to delete must be map; have %lT", l->type); 1316 goto error; 1317 } 1318 args->next->n = assignconv(r, l->type->down, "delete"); 1319 goto ret; 1320 1321 case OAPPEND: 1322 ok |= Erv; 1323 args = n->list; 1324 if(args == nil) { 1325 yyerror("missing arguments to append"); 1326 goto error; 1327 } 1328 1329 if(count(args) == 1 && !n->isddd) 1330 typecheck(&args->n, Erv | Efnstruct); 1331 else 1332 typechecklist(args, Erv); 1333 1334 if((t = args->n->type) == T) 1335 goto error; 1336 1337 // Unpack multiple-return result before type-checking. 1338 if(istype(t, TSTRUCT)) { 1339 t = t->type; 1340 if(istype(t, TFIELD)) 1341 t = t->type; 1342 } 1343 1344 n->type = t; 1345 if(!isslice(t)) { 1346 if(isconst(args->n, CTNIL)) { 1347 yyerror("first argument to append must be typed slice; have untyped nil", t); 1348 goto error; 1349 } 1350 yyerror("first argument to append must be slice; have %lT", t); 1351 goto error; 1352 } 1353 1354 if(n->isddd) { 1355 if(args->next == nil) { 1356 yyerror("cannot use ... on first argument to append"); 1357 goto error; 1358 } 1359 if(args->next->next != nil) { 1360 yyerror("too many arguments to append"); 1361 goto error; 1362 } 1363 if(istype(t->type, TUINT8) && istype(args->next->n->type, TSTRING)) { 1364 defaultlit(&args->next->n, types[TSTRING]); 1365 goto ret; 1366 } 1367 args->next->n = assignconv(args->next->n, t->orig, "append"); 1368 goto ret; 1369 } 1370 for(args=args->next; args != nil; args=args->next) { 1371 if(args->n->type == T) 1372 continue; 1373 args->n = assignconv(args->n, t->type, "append"); 1374 } 1375 goto ret; 1376 1377 case OCOPY: 1378 ok |= Etop|Erv; 1379 args = n->list; 1380 if(args == nil || args->next == nil) { 1381 yyerror("missing arguments to copy"); 1382 goto error; 1383 } 1384 if(args->next->next != nil) { 1385 yyerror("too many arguments to copy"); 1386 goto error; 1387 } 1388 n->left = args->n; 1389 n->right = args->next->n; 1390 n->list = nil; 1391 n->type = types[TINT]; 1392 typecheck(&n->left, Erv); 1393 typecheck(&n->right, Erv); 1394 if(n->left->type == T || n->right->type == T) 1395 goto error; 1396 defaultlit(&n->left, T); 1397 defaultlit(&n->right, T); 1398 if(n->left->type == T || n->right->type == T) 1399 goto error; 1400 1401 // copy([]byte, string) 1402 if(isslice(n->left->type) && n->right->type->etype == TSTRING) { 1403 if(eqtype(n->left->type->type, bytetype)) 1404 goto ret; 1405 yyerror("arguments to copy have different element types: %lT and string", n->left->type); 1406 goto error; 1407 } 1408 1409 if(!isslice(n->left->type) || !isslice(n->right->type)) { 1410 if(!isslice(n->left->type) && !isslice(n->right->type)) 1411 yyerror("arguments to copy must be slices; have %lT, %lT", n->left->type, n->right->type); 1412 else if(!isslice(n->left->type)) 1413 yyerror("first argument to copy should be slice; have %lT", n->left->type); 1414 else 1415 yyerror("second argument to copy should be slice or string; have %lT", n->right->type); 1416 goto error; 1417 } 1418 if(!eqtype(n->left->type->type, n->right->type->type)) { 1419 yyerror("arguments to copy have different element types: %lT and %lT", n->left->type, n->right->type); 1420 goto error; 1421 } 1422 goto ret; 1423 1424 case OCONV: 1425 doconv: 1426 ok |= Erv; 1427 saveorignode(n); 1428 typecheck(&n->left, Erv | (top & (Eindir | Eiota))); 1429 convlit1(&n->left, n->type, 1); 1430 if((t = n->left->type) == T || n->type == T) 1431 goto error; 1432 if((n->op = convertop(t, n->type, &why)) == 0) { 1433 if(!n->diag && !n->type->broke) { 1434 yyerror("cannot convert %lN to type %T%s", n->left, n->type, why); 1435 n->diag = 1; 1436 } 1437 n->op = OCONV; 1438 } 1439 switch(n->op) { 1440 case OCONVNOP: 1441 if(n->left->op == OLITERAL) { 1442 r = nod(OXXX, N, N); 1443 n->op = OCONV; 1444 n->orig = r; 1445 *r = *n; 1446 n->op = OLITERAL; 1447 n->val = n->left->val; 1448 } 1449 break; 1450 case OSTRARRAYBYTE: 1451 // do not use stringtoarraylit. 1452 // generated code and compiler memory footprint is better without it. 1453 break; 1454 case OSTRARRAYRUNE: 1455 if(n->left->op == OLITERAL) 1456 stringtoarraylit(&n); 1457 break; 1458 } 1459 goto ret; 1460 1461 case OMAKE: 1462 ok |= Erv; 1463 args = n->list; 1464 if(args == nil) { 1465 yyerror("missing argument to make"); 1466 goto error; 1467 } 1468 n->list = nil; 1469 l = args->n; 1470 args = args->next; 1471 typecheck(&l, Etype); 1472 if((t = l->type) == T) 1473 goto error; 1474 1475 switch(t->etype) { 1476 default: 1477 badmake: 1478 yyerror("cannot make type %T", t); 1479 goto error; 1480 1481 case TARRAY: 1482 if(!isslice(t)) 1483 goto badmake; 1484 if(args == nil) { 1485 yyerror("missing len argument to make(%T)", t); 1486 goto error; 1487 } 1488 l = args->n; 1489 args = args->next; 1490 typecheck(&l, Erv); 1491 r = N; 1492 if(args != nil) { 1493 r = args->n; 1494 args = args->next; 1495 typecheck(&r, Erv); 1496 } 1497 if(l->type == T || (r && r->type == T)) 1498 goto error; 1499 et = checkmake(t, "len", l) < 0; 1500 et |= r && checkmake(t, "cap", r) < 0; 1501 if(et) 1502 goto error; 1503 if(isconst(l, CTINT) && r && isconst(r, CTINT) && mpcmpfixfix(l->val.u.xval, r->val.u.xval) > 0) { 1504 yyerror("len larger than cap in make(%T)", t); 1505 goto error; 1506 } 1507 n->left = l; 1508 n->right = r; 1509 n->op = OMAKESLICE; 1510 break; 1511 1512 case TMAP: 1513 if(args != nil) { 1514 l = args->n; 1515 args = args->next; 1516 typecheck(&l, Erv); 1517 defaultlit(&l, types[TINT]); 1518 if(l->type == T) 1519 goto error; 1520 if(checkmake(t, "size", l) < 0) 1521 goto error; 1522 n->left = l; 1523 } else 1524 n->left = nodintconst(0); 1525 n->op = OMAKEMAP; 1526 break; 1527 1528 case TCHAN: 1529 l = N; 1530 if(args != nil) { 1531 l = args->n; 1532 args = args->next; 1533 typecheck(&l, Erv); 1534 defaultlit(&l, types[TINT]); 1535 if(l->type == T) 1536 goto error; 1537 if(checkmake(t, "buffer", l) < 0) 1538 goto error; 1539 n->left = l; 1540 } else 1541 n->left = nodintconst(0); 1542 n->op = OMAKECHAN; 1543 break; 1544 } 1545 if(args != nil) { 1546 yyerror("too many arguments to make(%T)", t); 1547 n->op = OMAKE; 1548 goto error; 1549 } 1550 n->type = t; 1551 goto ret; 1552 1553 case ONEW: 1554 ok |= Erv; 1555 args = n->list; 1556 if(args == nil) { 1557 yyerror("missing argument to new"); 1558 goto error; 1559 } 1560 l = args->n; 1561 typecheck(&l, Etype); 1562 if((t = l->type) == T) 1563 goto error; 1564 if(args->next != nil) { 1565 yyerror("too many arguments to new(%T)", t); 1566 goto error; 1567 } 1568 n->left = l; 1569 n->type = ptrto(t); 1570 goto ret; 1571 1572 case OPRINT: 1573 case OPRINTN: 1574 ok |= Etop; 1575 typechecklist(n->list, Erv | Eindir); // Eindir: address does not escape 1576 for(args=n->list; args; args=args->next) { 1577 // Special case for print: int constant is int64, not int. 1578 if(isconst(args->n, CTINT)) 1579 defaultlit(&args->n, types[TINT64]); 1580 else 1581 defaultlit(&args->n, T); 1582 } 1583 goto ret; 1584 1585 case OPANIC: 1586 ok |= Etop; 1587 if(onearg(n, "panic") < 0) 1588 goto error; 1589 typecheck(&n->left, Erv); 1590 defaultlit(&n->left, types[TINTER]); 1591 if(n->left->type == T) 1592 goto error; 1593 goto ret; 1594 1595 case ORECOVER: 1596 ok |= Erv|Etop; 1597 if(n->list != nil) { 1598 yyerror("too many arguments to recover"); 1599 goto error; 1600 } 1601 n->type = types[TINTER]; 1602 goto ret; 1603 1604 case OCLOSURE: 1605 ok |= Erv; 1606 typecheckclosure(n, top); 1607 if(n->type == T) 1608 goto error; 1609 goto ret; 1610 1611 case OITAB: 1612 ok |= Erv; 1613 typecheck(&n->left, Erv); 1614 if((t = n->left->type) == T) 1615 goto error; 1616 if(t->etype != TINTER) 1617 fatal("OITAB of %T", t); 1618 n->type = ptrto(types[TUINTPTR]); 1619 goto ret; 1620 1621 case OSPTR: 1622 ok |= Erv; 1623 typecheck(&n->left, Erv); 1624 if((t = n->left->type) == T) 1625 goto error; 1626 if(!isslice(t) && t->etype != TSTRING) 1627 fatal("OSPTR of %T", t); 1628 if(t->etype == TSTRING) 1629 n->type = ptrto(types[TUINT8]); 1630 else 1631 n->type = ptrto(t->type); 1632 goto ret; 1633 1634 case OCLOSUREVAR: 1635 ok |= Erv; 1636 goto ret; 1637 1638 case OCFUNC: 1639 ok |= Erv; 1640 typecheck(&n->left, Erv); 1641 n->type = types[TUINTPTR]; 1642 goto ret; 1643 1644 case OCONVNOP: 1645 ok |= Erv; 1646 typecheck(&n->left, Erv); 1647 goto ret; 1648 1649 /* 1650 * statements 1651 */ 1652 case OAS: 1653 ok |= Etop; 1654 typecheckas(n); 1655 goto ret; 1656 1657 case OAS2: 1658 ok |= Etop; 1659 typecheckas2(n); 1660 goto ret; 1661 1662 case OBREAK: 1663 case OCONTINUE: 1664 case ODCL: 1665 case OEMPTY: 1666 case OGOTO: 1667 case OLABEL: 1668 case OXFALL: 1669 case OVARKILL: 1670 ok |= Etop; 1671 goto ret; 1672 1673 case ODEFER: 1674 ok |= Etop; 1675 typecheck(&n->left, Etop|Erv); 1676 if(!n->left->diag) 1677 checkdefergo(n); 1678 goto ret; 1679 1680 case OPROC: 1681 ok |= Etop; 1682 typecheck(&n->left, Etop|Eproc|Erv); 1683 checkdefergo(n); 1684 goto ret; 1685 1686 case OFOR: 1687 ok |= Etop; 1688 typechecklist(n->ninit, Etop); 1689 typecheck(&n->ntest, Erv); 1690 if(n->ntest != N && (t = n->ntest->type) != T && t->etype != TBOOL) 1691 yyerror("non-bool %lN used as for condition", n->ntest); 1692 typecheck(&n->nincr, Etop); 1693 typechecklist(n->nbody, Etop); 1694 goto ret; 1695 1696 case OIF: 1697 ok |= Etop; 1698 typechecklist(n->ninit, Etop); 1699 typecheck(&n->ntest, Erv); 1700 if(n->ntest != N && (t = n->ntest->type) != T && t->etype != TBOOL) 1701 yyerror("non-bool %lN used as if condition", n->ntest); 1702 typechecklist(n->nbody, Etop); 1703 typechecklist(n->nelse, Etop); 1704 goto ret; 1705 1706 case ORETURN: 1707 ok |= Etop; 1708 if(count(n->list) == 1) 1709 typechecklist(n->list, Erv | Efnstruct); 1710 else 1711 typechecklist(n->list, Erv); 1712 if(curfn == N) { 1713 yyerror("return outside function"); 1714 goto error; 1715 } 1716 if(curfn->type->outnamed && n->list == nil) 1717 goto ret; 1718 typecheckaste(ORETURN, nil, 0, getoutargx(curfn->type), n->list, "return argument"); 1719 goto ret; 1720 1721 case ORETJMP: 1722 ok |= Etop; 1723 goto ret; 1724 1725 case OSELECT: 1726 ok |= Etop; 1727 typecheckselect(n); 1728 goto ret; 1729 1730 case OSWITCH: 1731 ok |= Etop; 1732 typecheckswitch(n); 1733 goto ret; 1734 1735 case ORANGE: 1736 ok |= Etop; 1737 typecheckrange(n); 1738 goto ret; 1739 1740 case OTYPESW: 1741 yyerror("use of .(type) outside type switch"); 1742 goto error; 1743 1744 case OXCASE: 1745 ok |= Etop; 1746 typechecklist(n->list, Erv); 1747 typechecklist(n->nbody, Etop); 1748 goto ret; 1749 1750 case ODCLFUNC: 1751 ok |= Etop; 1752 typecheckfunc(n); 1753 goto ret; 1754 1755 case ODCLCONST: 1756 ok |= Etop; 1757 typecheck(&n->left, Erv); 1758 goto ret; 1759 1760 case ODCLTYPE: 1761 ok |= Etop; 1762 typecheck(&n->left, Etype); 1763 if(!incannedimport) 1764 checkwidth(n->left->type); 1765 goto ret; 1766 } 1767 1768 ret: 1769 t = n->type; 1770 if(t && !t->funarg && n->op != OTYPE) { 1771 switch(t->etype) { 1772 case TFUNC: // might have TANY; wait until its called 1773 case TANY: 1774 case TFORW: 1775 case TIDEAL: 1776 case TNIL: 1777 case TBLANK: 1778 break; 1779 default: 1780 checkwidth(t); 1781 } 1782 } 1783 1784 if(safemode && !incannedimport && !importpkg && !compiling_wrappers && t && t->etype == TUNSAFEPTR) 1785 yyerror("cannot use unsafe.Pointer"); 1786 1787 evconst(n); 1788 if(n->op == OTYPE && !(top & Etype)) { 1789 yyerror("type %T is not an expression", n->type); 1790 goto error; 1791 } 1792 if((top & (Erv|Etype)) == Etype && n->op != OTYPE) { 1793 yyerror("%N is not a type", n); 1794 goto error; 1795 } 1796 // TODO(rsc): simplify 1797 if((top & (Ecall|Erv|Etype)) && !(top & Etop) && !(ok & (Erv|Etype|Ecall))) { 1798 yyerror("%N used as value", n); 1799 goto error; 1800 } 1801 if((top & Etop) && !(top & (Ecall|Erv|Etype)) && !(ok & Etop)) { 1802 if(n->diag == 0) { 1803 yyerror("%N evaluated but not used", n); 1804 n->diag = 1; 1805 } 1806 goto error; 1807 } 1808 1809 /* TODO 1810 if(n->type == T) 1811 fatal("typecheck nil type"); 1812 */ 1813 goto out; 1814 1815 badcall1: 1816 yyerror("invalid argument %lN for %O", n->left, n->op); 1817 goto error; 1818 1819 error: 1820 n->type = T; 1821 1822 out: 1823 *np = n; 1824 } 1825 1826 static int 1827 checksliceindex(Node *l, Node *r, Type *tp) 1828 { 1829 Type *t; 1830 1831 if((t = r->type) == T) 1832 return -1; 1833 if(!isint[t->etype]) { 1834 yyerror("invalid slice index %N (type %T)", r, t); 1835 return -1; 1836 } 1837 if(r->op == OLITERAL) { 1838 if(mpgetfix(r->val.u.xval) < 0) { 1839 yyerror("invalid slice index %N (index must be non-negative)", r); 1840 return -1; 1841 } else if(tp != nil && tp->bound > 0 && mpgetfix(r->val.u.xval) > tp->bound) { 1842 yyerror("invalid slice index %N (out of bounds for %d-element array)", r, tp->bound); 1843 return -1; 1844 } else if(isconst(l, CTSTR) && mpgetfix(r->val.u.xval) > l->val.u.sval->len) { 1845 yyerror("invalid slice index %N (out of bounds for %d-byte string)", r, l->val.u.sval->len); 1846 return -1; 1847 } else if(mpcmpfixfix(r->val.u.xval, maxintval[TINT]) > 0) { 1848 yyerror("invalid slice index %N (index too large)", r); 1849 return -1; 1850 } 1851 } 1852 return 0; 1853 } 1854 1855 static int 1856 checksliceconst(Node *lo, Node *hi) 1857 { 1858 if(lo != N && hi != N && lo->op == OLITERAL && hi->op == OLITERAL 1859 && mpcmpfixfix(lo->val.u.xval, hi->val.u.xval) > 0) { 1860 yyerror("invalid slice index: %N > %N", lo, hi); 1861 return -1; 1862 } 1863 return 0; 1864 } 1865 1866 static void 1867 checkdefergo(Node *n) 1868 { 1869 char *what; 1870 1871 what = "defer"; 1872 if(n->op == OPROC) 1873 what = "go"; 1874 1875 switch(n->left->op) { 1876 case OCALLINTER: 1877 case OCALLMETH: 1878 case OCALLFUNC: 1879 case OCLOSE: 1880 case OCOPY: 1881 case ODELETE: 1882 case OPANIC: 1883 case OPRINT: 1884 case OPRINTN: 1885 case ORECOVER: 1886 // ok 1887 break; 1888 case OAPPEND: 1889 case OCAP: 1890 case OCOMPLEX: 1891 case OIMAG: 1892 case OLEN: 1893 case OMAKE: 1894 case OMAKESLICE: 1895 case OMAKECHAN: 1896 case OMAKEMAP: 1897 case ONEW: 1898 case OREAL: 1899 case OLITERAL: // conversion or unsafe.Alignof, Offsetof, Sizeof 1900 if(n->left->orig != N && n->left->orig->op == OCONV) 1901 goto conv; 1902 yyerror("%s discards result of %N", what, n->left); 1903 break; 1904 default: 1905 conv: 1906 // type is broken or missing, most likely a method call on a broken type 1907 // we will warn about the broken type elsewhere. no need to emit a potentially confusing error 1908 if(n->left->type == T || n->left->type->broke) 1909 break; 1910 1911 if(!n->diag) { 1912 // The syntax made sure it was a call, so this must be 1913 // a conversion. 1914 n->diag = 1; 1915 yyerror("%s requires function call, not conversion", what); 1916 } 1917 break; 1918 } 1919 } 1920 1921 static void 1922 implicitstar(Node **nn) 1923 { 1924 Type *t; 1925 Node *n; 1926 1927 // insert implicit * if needed for fixed array 1928 n = *nn; 1929 t = n->type; 1930 if(t == T || !isptr[t->etype]) 1931 return; 1932 t = t->type; 1933 if(t == T) 1934 return; 1935 if(!isfixedarray(t)) 1936 return; 1937 n = nod(OIND, n, N); 1938 n->implicit = 1; 1939 typecheck(&n, Erv); 1940 *nn = n; 1941 } 1942 1943 static int 1944 onearg(Node *n, char *f, ...) 1945 { 1946 va_list arg; 1947 char *p; 1948 1949 if(n->left != N) 1950 return 0; 1951 if(n->list == nil) { 1952 va_start(arg, f); 1953 p = vsmprint(f, arg); 1954 va_end(arg); 1955 yyerror("missing argument to %s: %N", p, n); 1956 return -1; 1957 } 1958 if(n->list->next != nil) { 1959 va_start(arg, f); 1960 p = vsmprint(f, arg); 1961 va_end(arg); 1962 yyerror("too many arguments to %s: %N", p, n); 1963 n->left = n->list->n; 1964 n->list = nil; 1965 return -1; 1966 } 1967 n->left = n->list->n; 1968 n->list = nil; 1969 return 0; 1970 } 1971 1972 static int 1973 twoarg(Node *n) 1974 { 1975 if(n->left != N) 1976 return 0; 1977 if(n->list == nil) { 1978 yyerror("missing argument to %O - %N", n->op, n); 1979 return -1; 1980 } 1981 n->left = n->list->n; 1982 if(n->list->next == nil) { 1983 yyerror("missing argument to %O - %N", n->op, n); 1984 n->list = nil; 1985 return -1; 1986 } 1987 if(n->list->next->next != nil) { 1988 yyerror("too many arguments to %O - %N", n->op, n); 1989 n->list = nil; 1990 return -1; 1991 } 1992 n->right = n->list->next->n; 1993 n->list = nil; 1994 return 0; 1995 } 1996 1997 static Type* 1998 lookdot1(Node *errnode, Sym *s, Type *t, Type *f, int dostrcmp) 1999 { 2000 Type *r; 2001 2002 r = T; 2003 for(; f!=T; f=f->down) { 2004 if(dostrcmp && strcmp(f->sym->name, s->name) == 0) 2005 return f; 2006 if(f->sym != s) 2007 continue; 2008 if(r != T) { 2009 if(errnode) 2010 yyerror("ambiguous selector %N", errnode); 2011 else if(isptr[t->etype]) 2012 yyerror("ambiguous selector (%T).%S", t, s); 2013 else 2014 yyerror("ambiguous selector %T.%S", t, s); 2015 break; 2016 } 2017 r = f; 2018 } 2019 return r; 2020 } 2021 2022 static int 2023 looktypedot(Node *n, Type *t, int dostrcmp) 2024 { 2025 Type *f1, *f2; 2026 Sym *s; 2027 2028 s = n->right->sym; 2029 2030 if(t->etype == TINTER) { 2031 f1 = lookdot1(n, s, t, t->type, dostrcmp); 2032 if(f1 == T) 2033 return 0; 2034 2035 n->right = methodname(n->right, t); 2036 n->xoffset = f1->width; 2037 n->type = f1->type; 2038 n->op = ODOTINTER; 2039 return 1; 2040 } 2041 2042 // Find the base type: methtype will fail if t 2043 // is not of the form T or *T. 2044 f2 = methtype(t, 0); 2045 if(f2 == T) 2046 return 0; 2047 2048 expandmeth(f2); 2049 f2 = lookdot1(n, s, f2, f2->xmethod, dostrcmp); 2050 if(f2 == T) 2051 return 0; 2052 2053 // disallow T.m if m requires *T receiver 2054 if(isptr[getthisx(f2->type)->type->type->etype] 2055 && !isptr[t->etype] 2056 && f2->embedded != 2 2057 && !isifacemethod(f2->type)) { 2058 yyerror("invalid method expression %N (needs pointer receiver: (*%T).%hS)", n, t, f2->sym); 2059 return 0; 2060 } 2061 2062 n->right = methodname(n->right, t); 2063 n->xoffset = f2->width; 2064 n->type = f2->type; 2065 n->op = ODOTMETH; 2066 return 1; 2067 } 2068 2069 static Type* 2070 derefall(Type* t) 2071 { 2072 while(t && t->etype == tptr) 2073 t = t->type; 2074 return t; 2075 } 2076 2077 static int 2078 lookdot(Node *n, Type *t, int dostrcmp) 2079 { 2080 Type *f1, *f2, *tt, *rcvr; 2081 Sym *s; 2082 2083 s = n->right->sym; 2084 2085 dowidth(t); 2086 f1 = T; 2087 if(t->etype == TSTRUCT || t->etype == TINTER) 2088 f1 = lookdot1(n, s, t, t->type, dostrcmp); 2089 2090 f2 = T; 2091 if(n->left->type == t || n->left->type->sym == S) { 2092 f2 = methtype(t, 0); 2093 if(f2 != T) { 2094 // Use f2->method, not f2->xmethod: adddot has 2095 // already inserted all the necessary embedded dots. 2096 f2 = lookdot1(n, s, f2, f2->method, dostrcmp); 2097 } 2098 } 2099 2100 if(f1 != T) { 2101 if(f2 != T) 2102 yyerror("%S is both field and method", 2103 n->right->sym); 2104 if(f1->width == BADWIDTH) 2105 fatal("lookdot badwidth %T %p", f1, f1); 2106 n->xoffset = f1->width; 2107 n->type = f1->type; 2108 n->paramfld = f1; 2109 if(t->etype == TINTER) { 2110 if(isptr[n->left->type->etype]) { 2111 n->left = nod(OIND, n->left, N); // implicitstar 2112 n->left->implicit = 1; 2113 typecheck(&n->left, Erv); 2114 } 2115 n->op = ODOTINTER; 2116 } 2117 return 1; 2118 } 2119 2120 if(f2 != T) { 2121 tt = n->left->type; 2122 dowidth(tt); 2123 rcvr = getthisx(f2->type)->type->type; 2124 if(!eqtype(rcvr, tt)) { 2125 if(rcvr->etype == tptr && eqtype(rcvr->type, tt)) { 2126 checklvalue(n->left, "call pointer method on"); 2127 n->left = nod(OADDR, n->left, N); 2128 n->left->implicit = 1; 2129 typecheck(&n->left, Etype|Erv); 2130 } else if(tt->etype == tptr && rcvr->etype != tptr && eqtype(tt->type, rcvr)) { 2131 n->left = nod(OIND, n->left, N); 2132 n->left->implicit = 1; 2133 typecheck(&n->left, Etype|Erv); 2134 } else if(tt->etype == tptr && tt->type->etype == tptr && eqtype(derefall(tt), derefall(rcvr))) { 2135 yyerror("calling method %N with receiver %lN requires explicit dereference", n->right, n->left); 2136 while(tt->etype == tptr) { 2137 // Stop one level early for method with pointer receiver. 2138 if(rcvr->etype == tptr && tt->type->etype != tptr) 2139 break; 2140 n->left = nod(OIND, n->left, N); 2141 n->left->implicit = 1; 2142 typecheck(&n->left, Etype|Erv); 2143 tt = tt->type; 2144 } 2145 } else { 2146 fatal("method mismatch: %T for %T", rcvr, tt); 2147 } 2148 } 2149 n->right = methodname(n->right, n->left->type); 2150 n->xoffset = f2->width; 2151 n->type = f2->type; 2152 // print("lookdot found [%p] %T\n", f2->type, f2->type); 2153 n->op = ODOTMETH; 2154 return 1; 2155 } 2156 2157 return 0; 2158 } 2159 2160 static int 2161 nokeys(NodeList *l) 2162 { 2163 for(; l; l=l->next) 2164 if(l->n->op == OKEY) 2165 return 0; 2166 return 1; 2167 } 2168 2169 static int 2170 hasddd(Type *t) 2171 { 2172 Type *tl; 2173 2174 for(tl=t->type; tl; tl=tl->down) { 2175 if(tl->isddd) 2176 return 1; 2177 } 2178 return 0; 2179 } 2180 2181 static int 2182 downcount(Type *t) 2183 { 2184 Type *tl; 2185 int n; 2186 2187 n = 0; 2188 for(tl=t->type; tl; tl=tl->down) { 2189 n++; 2190 } 2191 return n; 2192 } 2193 2194 /* 2195 * typecheck assignment: type list = expression list 2196 */ 2197 static void 2198 typecheckaste(int op, Node *call, int isddd, Type *tstruct, NodeList *nl, char *desc) 2199 { 2200 Type *t, *tl, *tn; 2201 Node *n; 2202 int lno; 2203 char *why; 2204 int n1, n2; 2205 2206 lno = lineno; 2207 2208 if(tstruct->broke) 2209 goto out; 2210 2211 n = N; 2212 if(nl != nil && nl->next == nil && (n = nl->n)->type != T) 2213 if(n->type->etype == TSTRUCT && n->type->funarg) { 2214 if(!hasddd(tstruct)) { 2215 n1 = downcount(tstruct); 2216 n2 = downcount(n->type); 2217 if(n2 > n1) 2218 goto toomany; 2219 if(n2 < n1) 2220 goto notenough; 2221 } 2222 2223 tn = n->type->type; 2224 for(tl=tstruct->type; tl; tl=tl->down) { 2225 if(tl->isddd) { 2226 for(; tn; tn=tn->down) { 2227 if(assignop(tn->type, tl->type->type, &why) == 0) { 2228 if(call != N) 2229 yyerror("cannot use %T as type %T in argument to %N%s", tn->type, tl->type->type, call, why); 2230 else 2231 yyerror("cannot use %T as type %T in %s%s", tn->type, tl->type->type, desc, why); 2232 } 2233 } 2234 goto out; 2235 } 2236 if(tn == T) 2237 goto notenough; 2238 if(assignop(tn->type, tl->type, &why) == 0) { 2239 if(call != N) 2240 yyerror("cannot use %T as type %T in argument to %N%s", tn->type, tl->type, call, why); 2241 else 2242 yyerror("cannot use %T as type %T in %s%s", tn->type, tl->type, desc, why); 2243 } 2244 tn = tn->down; 2245 } 2246 if(tn != T) 2247 goto toomany; 2248 goto out; 2249 } 2250 2251 n1 = downcount(tstruct); 2252 n2 = count(nl); 2253 if(!hasddd(tstruct)) { 2254 if(n2 > n1) 2255 goto toomany; 2256 if(n2 < n1) 2257 goto notenough; 2258 } 2259 else { 2260 if(!isddd) { 2261 if(n2 < n1-1) 2262 goto notenough; 2263 } else { 2264 if(n2 > n1) 2265 goto toomany; 2266 if(n2 < n1) 2267 goto notenough; 2268 } 2269 } 2270 2271 for(tl=tstruct->type; tl; tl=tl->down) { 2272 t = tl->type; 2273 if(tl->isddd) { 2274 if(isddd) { 2275 if(nl == nil) 2276 goto notenough; 2277 if(nl->next != nil) 2278 goto toomany; 2279 n = nl->n; 2280 setlineno(n); 2281 if(n->type != T) 2282 nl->n = assignconv(n, t, desc); 2283 goto out; 2284 } 2285 for(; nl; nl=nl->next) { 2286 n = nl->n; 2287 setlineno(nl->n); 2288 if(n->type != T) 2289 nl->n = assignconv(n, t->type, desc); 2290 } 2291 goto out; 2292 } 2293 if(nl == nil) 2294 goto notenough; 2295 n = nl->n; 2296 setlineno(n); 2297 if(n->type != T) 2298 nl->n = assignconv(n, t, desc); 2299 nl = nl->next; 2300 } 2301 if(nl != nil) 2302 goto toomany; 2303 if(isddd) { 2304 if(call != N) 2305 yyerror("invalid use of ... in call to %N", call); 2306 else 2307 yyerror("invalid use of ... in %O", op); 2308 } 2309 2310 out: 2311 lineno = lno; 2312 return; 2313 2314 notenough: 2315 if(n == N || !n->diag) { 2316 if(call != N) 2317 yyerror("not enough arguments in call to %N", call); 2318 else 2319 yyerror("not enough arguments to %O", op); 2320 if(n != N) 2321 n->diag = 1; 2322 } 2323 goto out; 2324 2325 toomany: 2326 if(call != N) 2327 yyerror("too many arguments in call to %N", call); 2328 else 2329 yyerror("too many arguments to %O", op); 2330 goto out; 2331 } 2332 2333 /* 2334 * type check composite 2335 */ 2336 2337 static void 2338 fielddup(Node *n, Node *hash[], ulong nhash) 2339 { 2340 uint h; 2341 char *s; 2342 Node *a; 2343 2344 if(n->op != ONAME) 2345 fatal("fielddup: not ONAME"); 2346 s = n->sym->name; 2347 h = stringhash(s)%nhash; 2348 for(a=hash[h]; a!=N; a=a->ntest) { 2349 if(strcmp(a->sym->name, s) == 0) { 2350 yyerror("duplicate field name in struct literal: %s", s); 2351 return; 2352 } 2353 } 2354 n->ntest = hash[h]; 2355 hash[h] = n; 2356 } 2357 2358 static void 2359 keydup(Node *n, Node *hash[], ulong nhash) 2360 { 2361 uint h; 2362 ulong b; 2363 double d; 2364 int i; 2365 Node *a, *orign; 2366 Node cmp; 2367 char *s; 2368 2369 orign = n; 2370 if(n->op == OCONVIFACE) 2371 n = n->left; 2372 evconst(n); 2373 if(n->op != OLITERAL) 2374 return; // we dont check variables 2375 2376 switch(n->val.ctype) { 2377 default: // unknown, bool, nil 2378 b = 23; 2379 break; 2380 case CTINT: 2381 case CTRUNE: 2382 b = mpgetfix(n->val.u.xval); 2383 break; 2384 case CTFLT: 2385 d = mpgetflt(n->val.u.fval); 2386 s = (char*)&d; 2387 b = 0; 2388 for(i=sizeof(d); i>0; i--) 2389 b = b*PRIME1 + *s++; 2390 break; 2391 case CTSTR: 2392 b = 0; 2393 s = n->val.u.sval->s; 2394 for(i=n->val.u.sval->len; i>0; i--) 2395 b = b*PRIME1 + *s++; 2396 break; 2397 } 2398 2399 h = b%nhash; 2400 memset(&cmp, 0, sizeof(cmp)); 2401 for(a=hash[h]; a!=N; a=a->ntest) { 2402 cmp.op = OEQ; 2403 cmp.left = n; 2404 b = 0; 2405 if(a->op == OCONVIFACE && orign->op == OCONVIFACE) { 2406 if(eqtype(a->left->type, n->type)) { 2407 cmp.right = a->left; 2408 evconst(&cmp); 2409 b = cmp.val.u.bval; 2410 } 2411 } else if(eqtype(a->type, n->type)) { 2412 cmp.right = a; 2413 evconst(&cmp); 2414 b = cmp.val.u.bval; 2415 } 2416 if(b) { 2417 yyerror("duplicate key %N in map literal", n); 2418 return; 2419 } 2420 } 2421 orign->ntest = hash[h]; 2422 hash[h] = orign; 2423 } 2424 2425 static void 2426 indexdup(Node *n, Node *hash[], ulong nhash) 2427 { 2428 uint h; 2429 Node *a; 2430 ulong b, c; 2431 2432 if(n->op != OLITERAL) 2433 fatal("indexdup: not OLITERAL"); 2434 2435 b = mpgetfix(n->val.u.xval); 2436 h = b%nhash; 2437 for(a=hash[h]; a!=N; a=a->ntest) { 2438 c = mpgetfix(a->val.u.xval); 2439 if(b == c) { 2440 yyerror("duplicate index in array literal: %ld", b); 2441 return; 2442 } 2443 } 2444 n->ntest = hash[h]; 2445 hash[h] = n; 2446 } 2447 2448 static int 2449 prime(ulong h, ulong sr) 2450 { 2451 ulong n; 2452 2453 for(n=3; n<=sr; n+=2) 2454 if(h%n == 0) 2455 return 0; 2456 return 1; 2457 } 2458 2459 static ulong 2460 inithash(Node *n, Node ***hash, Node **autohash, ulong nautohash) 2461 { 2462 ulong h, sr; 2463 NodeList *ll; 2464 int i; 2465 2466 // count the number of entries 2467 h = 0; 2468 for(ll=n->list; ll; ll=ll->next) 2469 h++; 2470 2471 // if the auto hash table is 2472 // large enough use it. 2473 if(h <= nautohash) { 2474 *hash = autohash; 2475 memset(*hash, 0, nautohash * sizeof(**hash)); 2476 return nautohash; 2477 } 2478 2479 // make hash size odd and 12% larger than entries 2480 h += h/8; 2481 h |= 1; 2482 2483 // calculate sqrt of h 2484 sr = h/2; 2485 for(i=0; i<5; i++) 2486 sr = (sr + h/sr)/2; 2487 2488 // check for primeality 2489 while(!prime(h, sr)) 2490 h += 2; 2491 2492 // build and return a throw-away hash table 2493 *hash = mal(h * sizeof(**hash)); 2494 memset(*hash, 0, h * sizeof(**hash)); 2495 return h; 2496 } 2497 2498 static int 2499 iscomptype(Type *t) 2500 { 2501 switch(t->etype) { 2502 case TARRAY: 2503 case TSTRUCT: 2504 case TMAP: 2505 return 1; 2506 case TPTR32: 2507 case TPTR64: 2508 switch(t->type->etype) { 2509 case TARRAY: 2510 case TSTRUCT: 2511 case TMAP: 2512 return 1; 2513 } 2514 break; 2515 } 2516 return 0; 2517 } 2518 2519 static void 2520 pushtype(Node *n, Type *t) 2521 { 2522 if(n == N || n->op != OCOMPLIT || !iscomptype(t)) 2523 return; 2524 2525 if(n->right == N) { 2526 n->right = typenod(t); 2527 n->implicit = 1; // don't print 2528 n->right->implicit = 1; // * is okay 2529 } 2530 else if(debug['s']) { 2531 typecheck(&n->right, Etype); 2532 if(n->right->type != T && eqtype(n->right->type, t)) 2533 print("%lL: redundant type: %T\n", n->lineno, t); 2534 } 2535 } 2536 2537 static void 2538 typecheckcomplit(Node **np) 2539 { 2540 int bad, i, nerr; 2541 int64 len; 2542 Node *l, *n, *norig, *r, **hash; 2543 NodeList *ll; 2544 Type *t, *f; 2545 Sym *s, *s1; 2546 int32 lno; 2547 ulong nhash; 2548 Node *autohash[101]; 2549 2550 n = *np; 2551 lno = lineno; 2552 2553 if(n->right == N) { 2554 if(n->list != nil) 2555 setlineno(n->list->n); 2556 yyerror("missing type in composite literal"); 2557 goto error; 2558 } 2559 2560 // Save original node (including n->right) 2561 norig = nod(n->op, N, N); 2562 *norig = *n; 2563 2564 setlineno(n->right); 2565 l = typecheck(&n->right /* sic */, Etype|Ecomplit); 2566 if((t = l->type) == T) 2567 goto error; 2568 nerr = nerrors; 2569 n->type = t; 2570 2571 if(isptr[t->etype]) { 2572 // For better or worse, we don't allow pointers as the composite literal type, 2573 // except when using the &T syntax, which sets implicit on the OIND. 2574 if(!n->right->implicit) { 2575 yyerror("invalid pointer type %T for composite literal (use &%T instead)", t, t->type); 2576 goto error; 2577 } 2578 // Also, the underlying type must be a struct, map, slice, or array. 2579 if(!iscomptype(t)) { 2580 yyerror("invalid pointer type %T for composite literal", t); 2581 goto error; 2582 } 2583 t = t->type; 2584 } 2585 2586 switch(t->etype) { 2587 default: 2588 yyerror("invalid type for composite literal: %T", t); 2589 n->type = T; 2590 break; 2591 2592 case TARRAY: 2593 nhash = inithash(n, &hash, autohash, nelem(autohash)); 2594 2595 len = 0; 2596 i = 0; 2597 for(ll=n->list; ll; ll=ll->next) { 2598 l = ll->n; 2599 setlineno(l); 2600 if(l->op != OKEY) { 2601 l = nod(OKEY, nodintconst(i), l); 2602 l->left->type = types[TINT]; 2603 l->left->typecheck = 1; 2604 ll->n = l; 2605 } 2606 2607 typecheck(&l->left, Erv); 2608 evconst(l->left); 2609 i = nonnegconst(l->left); 2610 if(i < 0 && !l->left->diag) { 2611 yyerror("array index must be non-negative integer constant"); 2612 l->left->diag = 1; 2613 i = -(1<<30); // stay negative for a while 2614 } 2615 if(i >= 0) 2616 indexdup(l->left, hash, nhash); 2617 i++; 2618 if(i > len) { 2619 len = i; 2620 if(t->bound >= 0 && len > t->bound) { 2621 setlineno(l); 2622 yyerror("array index %lld out of bounds [0:%lld]", len-1, t->bound); 2623 t->bound = -1; // no more errors 2624 } 2625 } 2626 2627 r = l->right; 2628 pushtype(r, t->type); 2629 typecheck(&r, Erv); 2630 defaultlit(&r, t->type); 2631 l->right = assignconv(r, t->type, "array element"); 2632 } 2633 if(t->bound == -100) 2634 t->bound = len; 2635 if(t->bound < 0) 2636 n->right = nodintconst(len); 2637 n->op = OARRAYLIT; 2638 break; 2639 2640 case TMAP: 2641 nhash = inithash(n, &hash, autohash, nelem(autohash)); 2642 2643 for(ll=n->list; ll; ll=ll->next) { 2644 l = ll->n; 2645 setlineno(l); 2646 if(l->op != OKEY) { 2647 typecheck(&ll->n, Erv); 2648 yyerror("missing key in map literal"); 2649 continue; 2650 } 2651 2652 typecheck(&l->left, Erv); 2653 defaultlit(&l->left, t->down); 2654 l->left = assignconv(l->left, t->down, "map key"); 2655 if (l->left->op != OCONV) 2656 keydup(l->left, hash, nhash); 2657 2658 r = l->right; 2659 pushtype(r, t->type); 2660 typecheck(&r, Erv); 2661 defaultlit(&r, t->type); 2662 l->right = assignconv(r, t->type, "map value"); 2663 } 2664 n->op = OMAPLIT; 2665 break; 2666 2667 case TSTRUCT: 2668 bad = 0; 2669 if(n->list != nil && nokeys(n->list)) { 2670 // simple list of variables 2671 f = t->type; 2672 for(ll=n->list; ll; ll=ll->next) { 2673 setlineno(ll->n); 2674 typecheck(&ll->n, Erv); 2675 if(f == nil) { 2676 if(!bad++) 2677 yyerror("too many values in struct initializer"); 2678 continue; 2679 } 2680 s = f->sym; 2681 if(s != nil && !exportname(s->name) && s->pkg != localpkg) 2682 yyerror("implicit assignment of unexported field '%s' in %T literal", s->name, t); 2683 // No pushtype allowed here. Must name fields for that. 2684 ll->n = assignconv(ll->n, f->type, "field value"); 2685 ll->n = nod(OKEY, newname(f->sym), ll->n); 2686 ll->n->left->type = f; 2687 ll->n->left->typecheck = 1; 2688 f = f->down; 2689 } 2690 if(f != nil) 2691 yyerror("too few values in struct initializer"); 2692 } else { 2693 nhash = inithash(n, &hash, autohash, nelem(autohash)); 2694 2695 // keyed list 2696 for(ll=n->list; ll; ll=ll->next) { 2697 l = ll->n; 2698 setlineno(l); 2699 if(l->op != OKEY) { 2700 if(!bad++) 2701 yyerror("mixture of field:value and value initializers"); 2702 typecheck(&ll->n, Erv); 2703 continue; 2704 } 2705 s = l->left->sym; 2706 if(s == S) { 2707 yyerror("invalid field name %N in struct initializer", l->left); 2708 typecheck(&l->right, Erv); 2709 continue; 2710 } 2711 2712 // Sym might have resolved to name in other top-level 2713 // package, because of import dot. Redirect to correct sym 2714 // before we do the lookup. 2715 if(s->pkg != localpkg && exportname(s->name)) { 2716 s1 = lookup(s->name); 2717 if(s1->origpkg == s->pkg) 2718 s = s1; 2719 } 2720 f = lookdot1(nil, s, t, t->type, 0); 2721 if(f == nil) { 2722 yyerror("unknown %T field '%S' in struct literal", t, s); 2723 continue; 2724 } 2725 l->left = newname(s); 2726 l->left->typecheck = 1; 2727 l->left->type = f; 2728 s = f->sym; 2729 fielddup(newname(s), hash, nhash); 2730 r = l->right; 2731 // No pushtype allowed here. Tried and rejected. 2732 typecheck(&r, Erv); 2733 l->right = assignconv(r, f->type, "field value"); 2734 } 2735 } 2736 n->op = OSTRUCTLIT; 2737 break; 2738 } 2739 if(nerr != nerrors) 2740 goto error; 2741 2742 n->orig = norig; 2743 if(isptr[n->type->etype]) { 2744 n = nod(OPTRLIT, n, N); 2745 n->typecheck = 1; 2746 n->type = n->left->type; 2747 n->left->type = t; 2748 n->left->typecheck = 1; 2749 } 2750 2751 n->orig = norig; 2752 *np = n; 2753 lineno = lno; 2754 return; 2755 2756 error: 2757 n->type = T; 2758 *np = n; 2759 lineno = lno; 2760 } 2761 2762 /* 2763 * lvalue etc 2764 */ 2765 int 2766 islvalue(Node *n) 2767 { 2768 switch(n->op) { 2769 case OINDEX: 2770 if(isfixedarray(n->left->type)) 2771 return islvalue(n->left); 2772 if(n->left->type != T && n->left->type->etype == TSTRING) 2773 return 0; 2774 // fall through 2775 case OIND: 2776 case ODOTPTR: 2777 case OCLOSUREVAR: 2778 return 1; 2779 case ODOT: 2780 return islvalue(n->left); 2781 case ONAME: 2782 if(n->class == PFUNC) 2783 return 0; 2784 return 1; 2785 } 2786 return 0; 2787 } 2788 2789 static void 2790 checklvalue(Node *n, char *verb) 2791 { 2792 if(!islvalue(n)) 2793 yyerror("cannot %s %N", verb, n); 2794 } 2795 2796 static void 2797 checkassign(Node *n) 2798 { 2799 if(islvalue(n)) 2800 return; 2801 if(n->op == OINDEXMAP) { 2802 n->etype = 1; 2803 return; 2804 } 2805 2806 // have already complained about n being undefined 2807 if(n->op == ONONAME) 2808 return; 2809 2810 yyerror("cannot assign to %N", n); 2811 } 2812 2813 static void 2814 checkassignlist(NodeList *l) 2815 { 2816 for(; l; l=l->next) 2817 checkassign(l->n); 2818 } 2819 2820 // Check whether l and r are the same side effect-free expression, 2821 // so that it is safe to reuse one instead of computing both. 2822 static int 2823 samesafeexpr(Node *l, Node *r) 2824 { 2825 if(l->op != r->op || !eqtype(l->type, r->type)) 2826 return 0; 2827 2828 switch(l->op) { 2829 case ONAME: 2830 case OCLOSUREVAR: 2831 return l == r; 2832 2833 case ODOT: 2834 case ODOTPTR: 2835 return l->right != nil && r->right != nil && l->right->sym == r->right->sym && samesafeexpr(l->left, r->left); 2836 2837 case OIND: 2838 return samesafeexpr(l->left, r->left); 2839 2840 case OINDEX: 2841 return samesafeexpr(l->left, r->left) && samesafeexpr(l->right, r->right); 2842 } 2843 2844 return 0; 2845 } 2846 2847 /* 2848 * type check assignment. 2849 * if this assignment is the definition of a var on the left side, 2850 * fill in the var's type. 2851 */ 2852 2853 static void 2854 typecheckas(Node *n) 2855 { 2856 // delicate little dance. 2857 // the definition of n may refer to this assignment 2858 // as its definition, in which case it will call typecheckas. 2859 // in that case, do not call typecheck back, or it will cycle. 2860 // if the variable has a type (ntype) then typechecking 2861 // will not look at defn, so it is okay (and desirable, 2862 // so that the conversion below happens). 2863 n->left = resolve(n->left); 2864 if(n->left->defn != n || n->left->ntype) 2865 typecheck(&n->left, Erv | Easgn); 2866 2867 checkassign(n->left); 2868 typecheck(&n->right, Erv); 2869 if(n->right && n->right->type != T) { 2870 if(n->left->type != T) 2871 n->right = assignconv(n->right, n->left->type, "assignment"); 2872 } 2873 if(n->left->defn == n && n->left->ntype == N) { 2874 defaultlit(&n->right, T); 2875 n->left->type = n->right->type; 2876 } 2877 2878 // second half of dance. 2879 // now that right is done, typecheck the left 2880 // just to get it over with. see dance above. 2881 n->typecheck = 1; 2882 if(n->left->typecheck == 0) 2883 typecheck(&n->left, Erv | Easgn); 2884 2885 // Recognize slices being updated in place, for better code generation later. 2886 // Don't rewrite if using race detector, to avoid needing to teach race detector 2887 // about this optimization. 2888 if(n->left && n->left->op != OINDEXMAP && n->right && !flag_race) { 2889 switch(n->right->op) { 2890 case OSLICE: 2891 case OSLICE3: 2892 case OSLICESTR: 2893 // For x = x[0:y], x can be updated in place, without touching pointer. 2894 // TODO(rsc): Reenable once it is actually updated in place without touching the pointer. 2895 if(0 && samesafeexpr(n->left, n->right->left) && (n->right->right->left == N || iszero(n->right->right->left))) 2896 n->right->reslice = 1; 2897 break; 2898 2899 case OAPPEND: 2900 // For x = append(x, ...), x can be updated in place when there is capacity, 2901 // without touching the pointer; otherwise the emitted code to growslice 2902 // can take care of updating the pointer, and only in that case. 2903 // TODO(rsc): Reenable once the emitted code does update the pointer. 2904 if(0 && n->right->list != nil && samesafeexpr(n->left, n->right->list->n)) 2905 n->right->reslice = 1; 2906 break; 2907 } 2908 } 2909 } 2910 2911 static void 2912 checkassignto(Type *src, Node *dst) 2913 { 2914 char *why; 2915 2916 if(assignop(src, dst->type, &why) == 0) { 2917 yyerror("cannot assign %T to %lN in multiple assignment%s", src, dst, why); 2918 return; 2919 } 2920 } 2921 2922 static void 2923 typecheckas2(Node *n) 2924 { 2925 int cl, cr; 2926 NodeList *ll, *lr; 2927 Node *l, *r; 2928 Iter s; 2929 Type *t; 2930 2931 for(ll=n->list; ll; ll=ll->next) { 2932 // delicate little dance. 2933 ll->n = resolve(ll->n); 2934 if(ll->n->defn != n || ll->n->ntype) 2935 typecheck(&ll->n, Erv | Easgn); 2936 } 2937 cl = count(n->list); 2938 cr = count(n->rlist); 2939 checkassignlist(n->list); 2940 if(cl > 1 && cr == 1) 2941 typecheck(&n->rlist->n, Erv | Efnstruct); 2942 else 2943 typechecklist(n->rlist, Erv); 2944 2945 if(cl == cr) { 2946 // easy 2947 for(ll=n->list, lr=n->rlist; ll; ll=ll->next, lr=lr->next) { 2948 if(ll->n->type != T && lr->n->type != T) 2949 lr->n = assignconv(lr->n, ll->n->type, "assignment"); 2950 if(ll->n->defn == n && ll->n->ntype == N) { 2951 defaultlit(&lr->n, T); 2952 ll->n->type = lr->n->type; 2953 } 2954 } 2955 goto out; 2956 } 2957 2958 2959 l = n->list->n; 2960 r = n->rlist->n; 2961 2962 // m[i] = x, ok 2963 if(cl == 1 && cr == 2 && l->op == OINDEXMAP) { 2964 if(l->type == T) 2965 goto out; 2966 yyerror("assignment count mismatch: %d = %d (use delete)", cl, cr); 2967 goto out; 2968 } 2969 2970 // x,y,z = f() 2971 if(cr == 1) { 2972 if(r->type == T) 2973 goto out; 2974 switch(r->op) { 2975 case OCALLMETH: 2976 case OCALLINTER: 2977 case OCALLFUNC: 2978 if(r->type->etype != TSTRUCT || r->type->funarg == 0) 2979 break; 2980 cr = structcount(r->type); 2981 if(cr != cl) 2982 goto mismatch; 2983 n->op = OAS2FUNC; 2984 t = structfirst(&s, &r->type); 2985 for(ll=n->list; ll; ll=ll->next) { 2986 if(t->type != T && ll->n->type != T) 2987 checkassignto(t->type, ll->n); 2988 if(ll->n->defn == n && ll->n->ntype == N) 2989 ll->n->type = t->type; 2990 t = structnext(&s); 2991 } 2992 goto out; 2993 } 2994 } 2995 2996 // x, ok = y 2997 if(cl == 2 && cr == 1) { 2998 if(r->type == T) 2999 goto out; 3000 switch(r->op) { 3001 case OINDEXMAP: 3002 n->op = OAS2MAPR; 3003 goto common; 3004 case ORECV: 3005 n->op = OAS2RECV; 3006 goto common; 3007 case ODOTTYPE: 3008 n->op = OAS2DOTTYPE; 3009 r->op = ODOTTYPE2; 3010 common: 3011 if(l->type != T) 3012 checkassignto(r->type, l); 3013 if(l->defn == n) 3014 l->type = r->type; 3015 l = n->list->next->n; 3016 if(l->type != T && l->type->etype != TBOOL) 3017 checkassignto(types[TBOOL], l); 3018 if(l->defn == n && l->ntype == N) 3019 l->type = types[TBOOL]; 3020 goto out; 3021 } 3022 } 3023 3024 mismatch: 3025 yyerror("assignment count mismatch: %d = %d", cl, cr); 3026 3027 out: 3028 // second half of dance 3029 n->typecheck = 1; 3030 for(ll=n->list; ll; ll=ll->next) 3031 if(ll->n->typecheck == 0) 3032 typecheck(&ll->n, Erv | Easgn); 3033 } 3034 3035 /* 3036 * type check function definition 3037 */ 3038 static void 3039 typecheckfunc(Node *n) 3040 { 3041 Type *t, *rcvr; 3042 3043 typecheck(&n->nname, Erv | Easgn); 3044 if((t = n->nname->type) == T) 3045 return; 3046 n->type = t; 3047 t->nname = n->nname; 3048 rcvr = getthisx(t)->type; 3049 if(rcvr != nil && n->shortname != N && !isblank(n->shortname)) 3050 addmethod(n->shortname->sym, t, 1, n->nname->nointerface); 3051 } 3052 3053 static void 3054 stringtoarraylit(Node **np) 3055 { 3056 int32 i; 3057 NodeList *l; 3058 Strlit *s; 3059 char *p, *ep; 3060 Rune r; 3061 Node *nn, *n; 3062 3063 n = *np; 3064 if(n->left->op != OLITERAL || n->left->val.ctype != CTSTR) 3065 fatal("stringtoarraylit %N", n); 3066 3067 s = n->left->val.u.sval; 3068 l = nil; 3069 p = s->s; 3070 ep = s->s + s->len; 3071 i = 0; 3072 if(n->type->type->etype == TUINT8) { 3073 // raw []byte 3074 while(p < ep) 3075 l = list(l, nod(OKEY, nodintconst(i++), nodintconst((uchar)*p++))); 3076 } else { 3077 // utf-8 []rune 3078 while(p < ep) { 3079 p += chartorune(&r, p); 3080 l = list(l, nod(OKEY, nodintconst(i++), nodintconst(r))); 3081 } 3082 } 3083 nn = nod(OCOMPLIT, N, typenod(n->type)); 3084 nn->list = l; 3085 typecheck(&nn, Erv); 3086 *np = nn; 3087 } 3088 3089 3090 static int ntypecheckdeftype; 3091 static NodeList *methodqueue; 3092 3093 static void 3094 domethod(Node *n) 3095 { 3096 Node *nt; 3097 Type *t; 3098 3099 nt = n->type->nname; 3100 typecheck(&nt, Etype); 3101 if(nt->type == T) { 3102 // type check failed; leave empty func 3103 n->type->etype = TFUNC; 3104 n->type->nod = N; 3105 return; 3106 } 3107 3108 // If we have 3109 // type I interface { 3110 // M(_ int) 3111 // } 3112 // then even though I.M looks like it doesn't care about the 3113 // value of its argument, a specific implementation of I may 3114 // care. The _ would suppress the assignment to that argument 3115 // while generating a call, so remove it. 3116 for(t=getinargx(nt->type)->type; t; t=t->down) { 3117 if(t->sym != nil && strcmp(t->sym->name, "_") == 0) 3118 t->sym = nil; 3119 } 3120 3121 *n->type = *nt->type; 3122 n->type->nod = N; 3123 checkwidth(n->type); 3124 } 3125 3126 static NodeList *mapqueue; 3127 3128 void 3129 copytype(Node *n, Type *t) 3130 { 3131 int maplineno, embedlineno, lno; 3132 NodeList *l; 3133 3134 if(t->etype == TFORW) { 3135 // This type isn't computed yet; when it is, update n. 3136 t->copyto = list(t->copyto, n); 3137 return; 3138 } 3139 3140 maplineno = n->type->maplineno; 3141 embedlineno = n->type->embedlineno; 3142 3143 l = n->type->copyto; 3144 *n->type = *t; 3145 3146 t = n->type; 3147 t->sym = n->sym; 3148 t->local = n->local; 3149 t->vargen = n->vargen; 3150 t->siggen = 0; 3151 t->method = nil; 3152 t->xmethod = nil; 3153 t->nod = N; 3154 t->printed = 0; 3155 t->deferwidth = 0; 3156 t->copyto = nil; 3157 3158 // Update nodes waiting on this type. 3159 for(; l; l=l->next) 3160 copytype(l->n, t); 3161 3162 // Double-check use of type as embedded type. 3163 lno = lineno; 3164 if(embedlineno) { 3165 lineno = embedlineno; 3166 if(isptr[t->etype]) 3167 yyerror("embedded type cannot be a pointer"); 3168 } 3169 lineno = lno; 3170 3171 // Queue check for map until all the types are done settling. 3172 if(maplineno) { 3173 t->maplineno = maplineno; 3174 mapqueue = list(mapqueue, n); 3175 } 3176 } 3177 3178 static void 3179 typecheckdeftype(Node *n) 3180 { 3181 int lno; 3182 Type *t; 3183 NodeList *l; 3184 3185 ntypecheckdeftype++; 3186 lno = lineno; 3187 setlineno(n); 3188 n->type->sym = n->sym; 3189 n->typecheck = 1; 3190 typecheck(&n->ntype, Etype); 3191 if((t = n->ntype->type) == T) { 3192 n->diag = 1; 3193 n->type = T; 3194 goto ret; 3195 } 3196 if(n->type == T) { 3197 n->diag = 1; 3198 goto ret; 3199 } 3200 3201 // copy new type and clear fields 3202 // that don't come along. 3203 // anything zeroed here must be zeroed in 3204 // typedcl2 too. 3205 copytype(n, t); 3206 3207 ret: 3208 lineno = lno; 3209 3210 // if there are no type definitions going on, it's safe to 3211 // try to resolve the method types for the interfaces 3212 // we just read. 3213 if(ntypecheckdeftype == 1) { 3214 while((l = methodqueue) != nil) { 3215 methodqueue = nil; 3216 for(; l; l=l->next) 3217 domethod(l->n); 3218 } 3219 for(l=mapqueue; l; l=l->next) { 3220 lineno = l->n->type->maplineno; 3221 maptype(l->n->type, types[TBOOL]); 3222 } 3223 lineno = lno; 3224 } 3225 ntypecheckdeftype--; 3226 } 3227 3228 void 3229 queuemethod(Node *n) 3230 { 3231 if(ntypecheckdeftype == 0) { 3232 domethod(n); 3233 return; 3234 } 3235 methodqueue = list(methodqueue, n); 3236 } 3237 3238 Node* 3239 typecheckdef(Node *n) 3240 { 3241 int lno, nerrors0; 3242 Node *e; 3243 Type *t; 3244 NodeList *l; 3245 3246 lno = lineno; 3247 setlineno(n); 3248 3249 if(n->op == ONONAME) { 3250 if(!n->diag) { 3251 n->diag = 1; 3252 if(n->lineno != 0) 3253 lineno = n->lineno; 3254 yyerror("undefined: %S", n->sym); 3255 } 3256 return n; 3257 } 3258 3259 if(n->walkdef == 1) 3260 return n; 3261 3262 l = mal(sizeof *l); 3263 l->n = n; 3264 l->next = typecheckdefstack; 3265 typecheckdefstack = l; 3266 3267 if(n->walkdef == 2) { 3268 flusherrors(); 3269 print("typecheckdef loop:"); 3270 for(l=typecheckdefstack; l; l=l->next) 3271 print(" %S", l->n->sym); 3272 print("\n"); 3273 fatal("typecheckdef loop"); 3274 } 3275 n->walkdef = 2; 3276 3277 if(n->type != T || n->sym == S) // builtin or no name 3278 goto ret; 3279 3280 switch(n->op) { 3281 default: 3282 fatal("typecheckdef %O", n->op); 3283 3284 case OGOTO: 3285 case OLABEL: 3286 // not really syms 3287 break; 3288 3289 case OLITERAL: 3290 if(n->ntype != N) { 3291 typecheck(&n->ntype, Etype); 3292 n->type = n->ntype->type; 3293 n->ntype = N; 3294 if(n->type == T) { 3295 n->diag = 1; 3296 goto ret; 3297 } 3298 } 3299 e = n->defn; 3300 n->defn = N; 3301 if(e == N) { 3302 lineno = n->lineno; 3303 dump("typecheckdef nil defn", n); 3304 yyerror("xxx"); 3305 } 3306 typecheck(&e, Erv | Eiota); 3307 if(isconst(e, CTNIL)) { 3308 yyerror("const initializer cannot be nil"); 3309 goto ret; 3310 } 3311 if(e->type != T && e->op != OLITERAL || !isgoconst(e)) { 3312 if(!e->diag) { 3313 yyerror("const initializer %N is not a constant", e); 3314 e->diag = 1; 3315 } 3316 goto ret; 3317 } 3318 t = n->type; 3319 if(t != T) { 3320 if(!okforconst[t->etype]) { 3321 yyerror("invalid constant type %T", t); 3322 goto ret; 3323 } 3324 if(!isideal(e->type) && !eqtype(t, e->type)) { 3325 yyerror("cannot use %lN as type %T in const initializer", e, t); 3326 goto ret; 3327 } 3328 convlit(&e, t); 3329 } 3330 n->val = e->val; 3331 n->type = e->type; 3332 break; 3333 3334 case ONAME: 3335 if(n->ntype != N) { 3336 typecheck(&n->ntype, Etype); 3337 n->type = n->ntype->type; 3338 3339 if(n->type == T) { 3340 n->diag = 1; 3341 goto ret; 3342 } 3343 } 3344 if(n->type != T) 3345 break; 3346 if(n->defn == N) { 3347 if(n->etype != 0) // like OPRINTN 3348 break; 3349 if(nsavederrors+nerrors > 0) { 3350 // Can have undefined variables in x := foo 3351 // that make x have an n->ndefn == nil. 3352 // If there are other errors anyway, don't 3353 // bother adding to the noise. 3354 break; 3355 } 3356 fatal("var without type, init: %S", n->sym); 3357 } 3358 if(n->defn->op == ONAME) { 3359 typecheck(&n->defn, Erv); 3360 n->type = n->defn->type; 3361 break; 3362 } 3363 typecheck(&n->defn, Etop); // fills in n->type 3364 break; 3365 3366 case OTYPE: 3367 if(curfn) 3368 defercheckwidth(); 3369 n->walkdef = 1; 3370 n->type = typ(TFORW); 3371 n->type->sym = n->sym; 3372 nerrors0 = nerrors; 3373 typecheckdeftype(n); 3374 if(n->type->etype == TFORW && nerrors > nerrors0) { 3375 // Something went wrong during type-checking, 3376 // but it was reported. Silence future errors. 3377 n->type->broke = 1; 3378 } 3379 if(curfn) 3380 resumecheckwidth(); 3381 break; 3382 3383 case OPACK: 3384 // nothing to see here 3385 break; 3386 } 3387 3388 ret: 3389 if(n->op != OLITERAL && n->type != T && isideal(n->type)) 3390 fatal("got %T for %N", n->type, n); 3391 if(typecheckdefstack->n != n) 3392 fatal("typecheckdefstack mismatch"); 3393 l = typecheckdefstack; 3394 typecheckdefstack = l->next; 3395 3396 lineno = lno; 3397 n->walkdef = 1; 3398 return n; 3399 } 3400 3401 static int 3402 checkmake(Type *t, char *arg, Node *n) 3403 { 3404 if(n->op == OLITERAL) { 3405 switch(n->val.ctype) { 3406 case CTINT: 3407 case CTRUNE: 3408 case CTFLT: 3409 case CTCPLX: 3410 n->val = toint(n->val); 3411 if(mpcmpfixc(n->val.u.xval, 0) < 0) { 3412 yyerror("negative %s argument in make(%T)", arg, t); 3413 return -1; 3414 } 3415 if(mpcmpfixfix(n->val.u.xval, maxintval[TINT]) > 0) { 3416 yyerror("%s argument too large in make(%T)", arg, t); 3417 return -1; 3418 } 3419 3420 // Delay defaultlit until after we've checked range, to avoid 3421 // a redundant "constant NNN overflows int" error. 3422 defaultlit(&n, types[TINT]); 3423 return 0; 3424 default: 3425 break; 3426 } 3427 } 3428 3429 if(!isint[n->type->etype] && n->type->etype != TIDEAL) { 3430 yyerror("non-integer %s argument in make(%T) - %T", arg, t, n->type); 3431 return -1; 3432 } 3433 3434 // Defaultlit still necessary for non-constant: n might be 1<<k. 3435 defaultlit(&n, types[TINT]); 3436 3437 return 0; 3438 } 3439 3440 static void markbreaklist(NodeList*, Node*); 3441 3442 static void 3443 markbreak(Node *n, Node *implicit) 3444 { 3445 Label *lab; 3446 3447 if(n == N) 3448 return; 3449 3450 switch(n->op) { 3451 case OBREAK: 3452 if(n->left == N) { 3453 if(implicit) 3454 implicit->hasbreak = 1; 3455 } else { 3456 lab = n->left->sym->label; 3457 if(lab != L) 3458 lab->def->hasbreak = 1; 3459 } 3460 break; 3461 3462 case OFOR: 3463 case OSWITCH: 3464 case OTYPESW: 3465 case OSELECT: 3466 case ORANGE: 3467 implicit = n; 3468 // fall through 3469 3470 default: 3471 markbreak(n->left, implicit); 3472 markbreak(n->right, implicit); 3473 markbreak(n->ntest, implicit); 3474 markbreak(n->nincr, implicit); 3475 markbreaklist(n->ninit, implicit); 3476 markbreaklist(n->nbody, implicit); 3477 markbreaklist(n->nelse, implicit); 3478 markbreaklist(n->list, implicit); 3479 markbreaklist(n->rlist, implicit); 3480 break; 3481 } 3482 } 3483 3484 static void 3485 markbreaklist(NodeList *l, Node *implicit) 3486 { 3487 Node *n; 3488 Label *lab; 3489 3490 for(; l; l=l->next) { 3491 n = l->n; 3492 if(n->op == OLABEL && l->next && n->defn == l->next->n) { 3493 switch(n->defn->op) { 3494 case OFOR: 3495 case OSWITCH: 3496 case OTYPESW: 3497 case OSELECT: 3498 case ORANGE: 3499 lab = mal(sizeof *lab); 3500 lab->def = n->defn; 3501 n->left->sym->label = lab; 3502 markbreak(n->defn, n->defn); 3503 n->left->sym->label = L; 3504 l = l->next; 3505 continue; 3506 } 3507 } 3508 markbreak(n, implicit); 3509 } 3510 } 3511 3512 static int 3513 isterminating(NodeList *l, int top) 3514 { 3515 int def; 3516 Node *n; 3517 3518 if(l == nil) 3519 return 0; 3520 if(top) { 3521 while(l->next && l->n->op != OLABEL) 3522 l = l->next; 3523 markbreaklist(l, nil); 3524 } 3525 while(l->next) 3526 l = l->next; 3527 n = l->n; 3528 3529 if(n == N) 3530 return 0; 3531 3532 switch(n->op) { 3533 // NOTE: OLABEL is treated as a separate statement, 3534 // not a separate prefix, so skipping to the last statement 3535 // in the block handles the labeled statement case by 3536 // skipping over the label. No case OLABEL here. 3537 3538 case OBLOCK: 3539 return isterminating(n->list, 0); 3540 3541 case OGOTO: 3542 case ORETURN: 3543 case ORETJMP: 3544 case OPANIC: 3545 case OXFALL: 3546 return 1; 3547 3548 case OFOR: 3549 if(n->ntest != N) 3550 return 0; 3551 if(n->hasbreak) 3552 return 0; 3553 return 1; 3554 3555 case OIF: 3556 return isterminating(n->nbody, 0) && isterminating(n->nelse, 0); 3557 3558 case OSWITCH: 3559 case OTYPESW: 3560 case OSELECT: 3561 if(n->hasbreak) 3562 return 0; 3563 def = 0; 3564 for(l=n->list; l; l=l->next) { 3565 if(!isterminating(l->n->nbody, 0)) 3566 return 0; 3567 if(l->n->list == nil) // default 3568 def = 1; 3569 } 3570 if(n->op != OSELECT && !def) 3571 return 0; 3572 return 1; 3573 } 3574 3575 return 0; 3576 } 3577 3578 void 3579 checkreturn(Node *fn) 3580 { 3581 if(fn->type->outtuple && fn->nbody != nil) 3582 if(!isterminating(fn->nbody, 1)) 3583 yyerrorl(fn->endlineno, "missing return at end of function"); 3584 }