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