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