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