github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/cmd/gc/typecheck.c (about)

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