github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/cmd/gc/typecheck.c (about)

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