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