github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/cmd/gc/subr.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  #include	<u.h>
     6  #include	<libc.h>
     7  #include	"go.h"
     8  #include	"md5.h"
     9  #include	"y.tab.h"
    10  #include	"yerr.h"
    11  
    12  typedef struct Error Error;
    13  struct Error
    14  {
    15  	int lineno;
    16  	int seq;
    17  	char *msg;
    18  };
    19  static Error *err;
    20  static int nerr;
    21  static int merr;
    22  
    23  void
    24  errorexit(void)
    25  {
    26  	flusherrors();
    27  	if(outfile)
    28  		remove(outfile);
    29  	exits("error");
    30  }
    31  
    32  extern int yychar;
    33  int
    34  parserline(void)
    35  {
    36  	if(yychar != 0 && yychar != -2)	// parser has one symbol lookahead
    37  		return prevlineno;
    38  	return lineno;
    39  }
    40  
    41  static void
    42  adderr(int line, char *fmt, va_list arg)
    43  {
    44  	Fmt f;
    45  	Error *p;
    46  
    47  	fmtstrinit(&f);
    48  	fmtprint(&f, "%L: ", line);
    49  	fmtvprint(&f, fmt, arg);
    50  	fmtprint(&f, "\n");
    51  
    52  	if(nerr >= merr) {
    53  		if(merr == 0)
    54  			merr = 16;
    55  		else
    56  			merr *= 2;
    57  		p = realloc(err, merr*sizeof err[0]);
    58  		if(p == nil) {
    59  			merr = nerr;
    60  			flusherrors();
    61  			print("out of memory\n");
    62  			errorexit();
    63  		}
    64  		err = p;
    65  	}
    66  	err[nerr].seq = nerr;
    67  	err[nerr].lineno = line;
    68  	err[nerr].msg = fmtstrflush(&f);
    69  	nerr++;
    70  }
    71  
    72  static int
    73  errcmp(const void *va, const void *vb)
    74  {
    75  	Error *a, *b;
    76  
    77  	a = (Error*)va;
    78  	b = (Error*)vb;
    79  	if(a->lineno != b->lineno)
    80  		return a->lineno - b->lineno;
    81  	if(a->seq != b->seq)
    82  		return a->seq - b->seq;
    83  	return strcmp(a->msg, b->msg);
    84  }
    85  
    86  void
    87  flusherrors(void)
    88  {
    89  	int i;
    90  
    91  	Bflush(&bstdout);
    92  	if(nerr == 0)
    93  		return;
    94  	qsort(err, nerr, sizeof err[0], errcmp);
    95  	for(i=0; i<nerr; i++)
    96  		if(i==0 || strcmp(err[i].msg, err[i-1].msg) != 0)
    97  			print("%s", err[i].msg);
    98  	nerr = 0;
    99  }
   100  
   101  static void
   102  hcrash(void)
   103  {
   104  	if(debug['h']) {
   105  		flusherrors();
   106  		if(outfile)
   107  			remove(outfile);
   108  		*(volatile int*)0 = 0;
   109  	}
   110  }
   111  
   112  void
   113  yyerrorl(int line, char *fmt, ...)
   114  {
   115  	va_list arg;
   116  
   117  	va_start(arg, fmt);
   118  	adderr(line, fmt, arg);
   119  	va_end(arg);
   120  
   121  	hcrash();
   122  	nerrors++;
   123  	if(nsavederrors+nerrors >= 10 && !debug['e']) {
   124  		flusherrors();
   125  		print("%L: too many errors\n", line);
   126  		errorexit();
   127  	}
   128  }
   129  
   130  extern int yystate, yychar;
   131  
   132  void
   133  yyerror(char *fmt, ...)
   134  {
   135  	int i;
   136  	static int lastsyntax;
   137  	va_list arg;
   138  	char buf[512], *p;
   139  
   140  	if(strncmp(fmt, "syntax error", 12) == 0) {
   141  		nsyntaxerrors++;
   142  		
   143  		if(debug['x'])	
   144  			print("yyerror: yystate=%d yychar=%d\n", yystate, yychar);
   145  
   146  		// An unexpected EOF caused a syntax error. Use the previous
   147  		// line number since getc generated a fake newline character.
   148  		if(curio.eofnl)
   149  			lexlineno = prevlineno;
   150  
   151  		// only one syntax error per line
   152  		if(lastsyntax == lexlineno)
   153  			return;
   154  		lastsyntax = lexlineno;
   155  			
   156  		if(strstr(fmt, "{ or {") || strstr(fmt, " or ?") || strstr(fmt, " or @")) {
   157  			// The grammar has { and LBRACE but both show up as {.
   158  			// Rewrite syntax error referring to "{ or {" to say just "{".
   159  			strecpy(buf, buf+sizeof buf, fmt);
   160  			p = strstr(buf, "{ or {");
   161  			if(p)
   162  				memmove(p+1, p+6, strlen(p+6)+1);
   163  			
   164  			// The grammar has ? and @ but only for reading imports.
   165  			// Silence them in ordinary errors.
   166  			p = strstr(buf, " or ?");
   167  			if(p)
   168  				memmove(p, p+5, strlen(p+5)+1);
   169  			p = strstr(buf, " or @");
   170  			if(p)
   171  				memmove(p, p+5, strlen(p+5)+1);
   172  			fmt = buf;
   173  		}
   174  		
   175  		// look for parse state-specific errors in list (see go.errors).
   176  		for(i=0; i<nelem(yymsg); i++) {
   177  			if(yymsg[i].yystate == yystate && yymsg[i].yychar == yychar) {
   178  				yyerrorl(lexlineno, "syntax error: %s", yymsg[i].msg);
   179  				return;
   180  			}
   181  		}
   182  		
   183  		// plain "syntax error" gets "near foo" added
   184  		if(strcmp(fmt, "syntax error") == 0) {
   185  			yyerrorl(lexlineno, "syntax error near %s", lexbuf);
   186  			return;
   187  		}
   188  		
   189  		// if bison says "syntax error, more info"; print "syntax error: more info".
   190  		if(fmt[12] == ',') {
   191  			yyerrorl(lexlineno, "syntax error:%s", fmt+13);
   192  			return;
   193  		}
   194  
   195  		yyerrorl(lexlineno, "%s", fmt);
   196  		return;
   197  	}
   198  
   199  	va_start(arg, fmt);
   200  	adderr(parserline(), fmt, arg);
   201  	va_end(arg);
   202  
   203  	hcrash();
   204  	nerrors++;
   205  	if(nsavederrors+nerrors >= 10 && !debug['e']) {
   206  		flusherrors();
   207  		print("%L: too many errors\n", parserline());
   208  		errorexit();
   209  	}
   210  }
   211  
   212  void
   213  warn(char *fmt, ...)
   214  {
   215  	va_list arg;
   216  
   217  	va_start(arg, fmt);
   218  	adderr(parserline(), fmt, arg);
   219  	va_end(arg);
   220  
   221  	hcrash();
   222  }
   223  
   224  void
   225  warnl(int line, char *fmt, ...)
   226  {
   227  	va_list arg;
   228  
   229  	va_start(arg, fmt);
   230  	adderr(line, fmt, arg);
   231  	va_end(arg);
   232  	if(debug['m'])
   233  		flusherrors();
   234  }
   235  
   236  void
   237  fatal(char *fmt, ...)
   238  {
   239  	va_list arg;
   240  
   241  	flusherrors();
   242  
   243  	print("%L: internal compiler error: ", lineno);
   244  	va_start(arg, fmt);
   245  	vfprint(1, fmt, arg);
   246  	va_end(arg);
   247  	print("\n");
   248  	
   249  	// If this is a released compiler version, ask for a bug report.
   250  	if(strncmp(getgoversion(), "release", 7) == 0) {
   251  		print("\n");
   252  		print("Please file a bug report including a short program that triggers the error.\n");
   253  		print("https://golang.org/issue/new\n");
   254  	}
   255  	hcrash();
   256  	errorexit();
   257  }
   258  
   259  void
   260  linehist(char *file, int32 off, int relative)
   261  {
   262  	if(debug['i']) {
   263  		if(file != nil) {
   264  			if(off < 0)
   265  				print("pragma %s", file);
   266  			else
   267  			if(off > 0)
   268  				print("line %s", file);
   269  			else
   270  				print("import %s", file);
   271  		} else
   272  			print("end of import");
   273  		print(" at line %L\n", lexlineno);
   274  	}
   275  	
   276  	if(off < 0 && file[0] != '/' && !relative)
   277  		file = smprint("%s/%s", ctxt->pathname, file);
   278  	linklinehist(ctxt, lexlineno, file, off);
   279  }
   280  
   281  int32
   282  setlineno(Node *n)
   283  {
   284  	int32 lno;
   285  
   286  	lno = lineno;
   287  	if(n != N)
   288  	switch(n->op) {
   289  	case ONAME:
   290  	case OTYPE:
   291  	case OPACK:
   292  	case OLITERAL:
   293  		break;
   294  	default:
   295  		lineno = n->lineno;
   296  		if(lineno == 0) {
   297  			if(debug['K'])
   298  				warn("setlineno: line 0");
   299  			lineno = lno;
   300  		}
   301  	}
   302  	return lno;
   303  }
   304  
   305  uint32
   306  stringhash(char *p)
   307  {
   308  	uint32 h;
   309  	int c;
   310  
   311  	h = 0;
   312  	for(;;) {
   313  		c = *p++;
   314  		if(c == 0)
   315  			break;
   316  		h = h*PRIME1 + c;
   317  	}
   318  
   319  	if((int32)h < 0) {
   320  		h = -h;
   321  		if((int32)h < 0)
   322  			h = 0;
   323  	}
   324  	return h;
   325  }
   326  
   327  Sym*
   328  lookup(char *name)
   329  {
   330  	return pkglookup(name, localpkg);
   331  }
   332  
   333  Sym*
   334  pkglookup(char *name, Pkg *pkg)
   335  {
   336  	Sym *s;
   337  	uint32 h;
   338  	int c;
   339  
   340  	h = stringhash(name) % NHASH;
   341  	c = name[0];
   342  	for(s = hash[h]; s != S; s = s->link) {
   343  		if(s->name[0] != c || s->pkg != pkg)
   344  			continue;
   345  		if(strcmp(s->name, name) == 0)
   346  			return s;
   347  	}
   348  
   349  	s = mal(sizeof(*s));
   350  	s->name = mal(strlen(name)+1);
   351  	strcpy(s->name, name);
   352  
   353  	s->pkg = pkg;
   354  
   355  	s->link = hash[h];
   356  	hash[h] = s;
   357  	s->lexical = LNAME;
   358  
   359  	return s;
   360  }
   361  
   362  Sym*
   363  restrictlookup(char *name, Pkg *pkg)
   364  {
   365  	if(!exportname(name) && pkg != localpkg)
   366  		yyerror("cannot refer to unexported name %s.%s", pkg->name, name);
   367  	return pkglookup(name, pkg);
   368  }
   369  
   370  
   371  // find all the exported symbols in package opkg
   372  // and make them available in the current package
   373  void
   374  importdot(Pkg *opkg, Node *pack)
   375  {
   376  	Sym *s, *s1;
   377  	uint32 h;
   378  	int n;
   379  	char *pkgerror;
   380  
   381  	n = 0;
   382  	for(h=0; h<NHASH; h++) {
   383  		for(s = hash[h]; s != S; s = s->link) {
   384  			if(s->pkg != opkg)
   385  				continue;
   386  			if(s->def == N)
   387  				continue;
   388  			if(!exportname(s->name) || utfrune(s->name, 0xb7))	// 0xb7 = center dot
   389  				continue;
   390  			s1 = lookup(s->name);
   391  			if(s1->def != N) {
   392  				pkgerror = smprint("during import \"%Z\"", opkg->path);
   393  				redeclare(s1, pkgerror);
   394  				continue;
   395  			}
   396  			s1->def = s->def;
   397  			s1->block = s->block;
   398  			s1->def->pack = pack;
   399  			s1->origpkg = opkg;
   400  			n++;
   401  		}
   402  	}
   403  	if(n == 0) {
   404  		// can't possibly be used - there were no symbols
   405  		yyerrorl(pack->lineno, "imported and not used: \"%Z\"", opkg->path);
   406  	}
   407  }
   408  
   409  static void
   410  gethunk(void)
   411  {
   412  	char *h;
   413  	int32 nh;
   414  
   415  	nh = NHUNK;
   416  	if(thunk >= 10L*NHUNK)
   417  		nh = 10L*NHUNK;
   418  	h = (char*)malloc(nh);
   419  	if(h == nil) {
   420  		flusherrors();
   421  		yyerror("out of memory");
   422  		errorexit();
   423  	}
   424  	hunk = h;
   425  	nhunk = nh;
   426  	thunk += nh;
   427  }
   428  
   429  void*
   430  mal(int32 n)
   431  {
   432  	void *p;
   433  
   434  	if(n >= NHUNK) {
   435  		p = malloc(n);
   436  		if(p == nil) {
   437  			flusherrors();
   438  			yyerror("out of memory");
   439  			errorexit();
   440  		}
   441  		memset(p, 0, n);
   442  		return p;
   443  	}
   444  
   445  	while((uintptr)hunk & MAXALIGN) {
   446  		hunk++;
   447  		nhunk--;
   448  	}
   449  	if(nhunk < n)
   450  		gethunk();
   451  
   452  	p = hunk;
   453  	nhunk -= n;
   454  	hunk += n;
   455  	memset(p, 0, n);
   456  	return p;
   457  }
   458  
   459  void*
   460  remal(void *p, int32 on, int32 n)
   461  {
   462  	void *q;
   463  
   464  	q = (uchar*)p + on;
   465  	if(q != hunk || nhunk < n) {
   466  		if(on+n >= NHUNK) {
   467  			q = mal(on+n);
   468  			memmove(q, p, on);
   469  			return q;
   470  		}
   471  		if(nhunk < on+n)
   472  			gethunk();
   473  		memmove(hunk, p, on);
   474  		p = hunk;
   475  		hunk += on;
   476  		nhunk -= on;
   477  	}
   478  	hunk += n;
   479  	nhunk -= n;
   480  	return p;
   481  }
   482  
   483  Node*
   484  nod(int op, Node *nleft, Node *nright)
   485  {
   486  	Node *n;
   487  
   488  	n = mal(sizeof(*n));
   489  	n->op = op;
   490  	n->left = nleft;
   491  	n->right = nright;
   492  	n->lineno = parserline();
   493  	n->xoffset = BADWIDTH;
   494  	n->orig = n;
   495  	n->curfn = curfn;
   496  	return n;
   497  }
   498  
   499  void
   500  saveorignode(Node *n)
   501  {
   502  	Node *norig;
   503  
   504  	if(n->orig != N)
   505  		return;
   506  	norig = nod(n->op, N, N);
   507  	*norig = *n;
   508  	n->orig = norig;
   509  }
   510  
   511  // ispaddedfield reports whether the given field
   512  // is followed by padding. For the case where t is
   513  // the last field, total gives the size of the enclosing struct.
   514  static int
   515  ispaddedfield(Type *t, vlong total)
   516  {
   517  	if(t->etype != TFIELD)
   518  		fatal("ispaddedfield called non-field %T", t);
   519  	if(t->down == T)
   520  		return t->width + t->type->width != total;
   521  	return t->width + t->type->width != t->down->width;
   522  }
   523  
   524  int
   525  algtype1(Type *t, Type **bad)
   526  {
   527  	int a, ret;
   528  	Type *t1;
   529  	
   530  	if(bad)
   531  		*bad = T;
   532  	if(t->broke)
   533  		return AMEM;
   534  	if(t->noalg)
   535  		return ANOEQ;
   536  
   537  	switch(t->etype) {
   538  	case TANY:
   539  	case TFORW:
   540  		// will be defined later.
   541  		*bad = t;
   542  		return -1;
   543  
   544  	case TINT8:
   545  	case TUINT8:
   546  	case TINT16:
   547  	case TUINT16:
   548  	case TINT32:
   549  	case TUINT32:
   550  	case TINT64:
   551  	case TUINT64:
   552  	case TINT:
   553  	case TUINT:
   554  	case TUINTPTR:
   555  	case TBOOL:
   556  	case TPTR32:
   557  	case TPTR64:
   558  	case TCHAN:
   559  	case TUNSAFEPTR:
   560  		return AMEM;
   561  
   562  	case TFUNC:
   563  	case TMAP:
   564  		if(bad)
   565  			*bad = t;
   566  		return ANOEQ;
   567  
   568  	case TFLOAT32:
   569  		return AFLOAT32;
   570  
   571  	case TFLOAT64:
   572  		return AFLOAT64;
   573  
   574  	case TCOMPLEX64:
   575  		return ACPLX64;
   576  
   577  	case TCOMPLEX128:
   578  		return ACPLX128;
   579  
   580  	case TSTRING:
   581  		return ASTRING;
   582  	
   583  	case TINTER:
   584  		if(isnilinter(t))
   585  			return ANILINTER;
   586  		return AINTER;
   587  	
   588  	case TARRAY:
   589  		if(isslice(t)) {
   590  			if(bad)
   591  				*bad = t;
   592  			return ANOEQ;
   593  		}
   594  		a = algtype1(t->type, bad);
   595  		if(a == ANOEQ || a == AMEM) {
   596  			if(a == ANOEQ && bad)
   597  				*bad = t;
   598  			return a;
   599  		}
   600  		return -1;  // needs special compare
   601  
   602  	case TSTRUCT:
   603  		if(t->type != T && t->type->down == T && !isblanksym(t->type->sym)) {
   604  			// One-field struct is same as that one field alone.
   605  			return algtype1(t->type->type, bad);
   606  		}
   607  		ret = AMEM;
   608  		for(t1=t->type; t1!=T; t1=t1->down) {
   609  			// All fields must be comparable.
   610  			a = algtype1(t1->type, bad);
   611  			if(a == ANOEQ)
   612  				return ANOEQ;
   613  
   614  			// Blank fields, padded fields, fields with non-memory
   615  			// equality need special compare.
   616  			if(a != AMEM || isblanksym(t1->sym) || ispaddedfield(t1, t->width)) {
   617  				ret = -1;
   618  				continue;
   619  			}
   620  		}
   621  		return ret;
   622  	}
   623  
   624  	fatal("algtype1: unexpected type %T", t);
   625  	return 0;
   626  }
   627  
   628  int
   629  algtype(Type *t)
   630  {
   631  	int a;
   632  	
   633  	a = algtype1(t, nil);
   634  	if(a == AMEM || a == ANOEQ) {
   635  		if(isslice(t))
   636  			return ASLICE;
   637  		switch(t->width) {
   638  		case 0:
   639  			return a + AMEM0 - AMEM;
   640  		case 1:
   641  			return a + AMEM8 - AMEM;
   642  		case 2:
   643  			return a + AMEM16 - AMEM;
   644  		case 4:
   645  			return a + AMEM32 - AMEM;
   646  		case 8:
   647  			return a + AMEM64 - AMEM;
   648  		case 16:
   649  			return a + AMEM128 - AMEM;
   650  		}
   651  	}
   652  	return a;
   653  }
   654  
   655  Type*
   656  maptype(Type *key, Type *val)
   657  {
   658  	Type *t;
   659  	Type *bad;
   660  	int atype, mtype;
   661  
   662  	if(key != nil) {
   663  		atype = algtype1(key, &bad);
   664  		if(bad == T)
   665  			mtype = key->etype;
   666  		else
   667  			mtype = bad->etype;
   668  		switch(mtype) {
   669  		default:
   670  			if(atype == ANOEQ)
   671  				yyerror("invalid map key type %T", key);
   672  			break;
   673  		case TANY:
   674  			// will be resolved later.
   675  			break;
   676  		case TFORW:
   677  			// map[key] used during definition of key.
   678  			// postpone check until key is fully defined.
   679  			// if there are multiple uses of map[key]
   680  			// before key is fully defined, the error
   681  			// will only be printed for the first one.
   682  			// good enough.
   683  			if(key->maplineno == 0)
   684  				key->maplineno = lineno;
   685  			break;
   686  		}
   687  	}
   688  	t = typ(TMAP);
   689  	t->down = key;
   690  	t->type = val;
   691  	return t;
   692  }
   693  
   694  Type*
   695  typ(int et)
   696  {
   697  	Type *t;
   698  
   699  	t = mal(sizeof(*t));
   700  	t->etype = et;
   701  	t->width = BADWIDTH;
   702  	t->lineno = lineno;
   703  	t->orig = t;
   704  	return t;
   705  }
   706  
   707  static int
   708  methcmp(const void *va, const void *vb)
   709  {
   710  	Type *a, *b;
   711  	int i;
   712  	
   713  	a = *(Type**)va;
   714  	b = *(Type**)vb;
   715  	if(a->sym == S && b->sym == S)
   716  		return 0;
   717  	if(a->sym == S)
   718  		return -1;
   719  	if(b->sym == S)
   720  		return 1;
   721  	i = strcmp(a->sym->name, b->sym->name);
   722  	if(i != 0)
   723  		return i;
   724  	if(!exportname(a->sym->name)) {
   725  		i = strcmp(a->sym->pkg->path->s, b->sym->pkg->path->s);
   726  		if(i != 0)
   727  			return i;
   728  	}
   729  	return 0;
   730  }
   731  
   732  Type*
   733  sortinter(Type *t)
   734  {
   735  	Type *f;
   736  	int i;
   737  	Type **a;
   738  	
   739  	if(t->type == nil || t->type->down == nil)
   740  		return t;
   741  
   742  	i=0;
   743  	for(f=t->type; f; f=f->down)
   744  		i++;
   745  	a = mal(i*sizeof f);
   746  	i = 0;
   747  	for(f=t->type; f; f=f->down)
   748  		a[i++] = f;
   749  	qsort(a, i, sizeof a[0], methcmp);
   750  	while(i-- > 0) {
   751  		a[i]->down = f;
   752  		f = a[i];
   753  	}
   754  	t->type = f;
   755  	return t;
   756  }
   757  
   758  Node*
   759  nodintconst(int64 v)
   760  {
   761  	Node *c;
   762  
   763  	c = nod(OLITERAL, N, N);
   764  	c->addable = 1;
   765  	c->val.u.xval = mal(sizeof(*c->val.u.xval));
   766  	mpmovecfix(c->val.u.xval, v);
   767  	c->val.ctype = CTINT;
   768  	c->type = types[TIDEAL];
   769  	ullmancalc(c);
   770  	return c;
   771  }
   772  
   773  Node*
   774  nodfltconst(Mpflt* v)
   775  {
   776  	Node *c;
   777  
   778  	c = nod(OLITERAL, N, N);
   779  	c->addable = 1;
   780  	c->val.u.fval = mal(sizeof(*c->val.u.fval));
   781  	mpmovefltflt(c->val.u.fval, v);
   782  	c->val.ctype = CTFLT;
   783  	c->type = types[TIDEAL];
   784  	ullmancalc(c);
   785  	return c;
   786  }
   787  
   788  void
   789  nodconst(Node *n, Type *t, int64 v)
   790  {
   791  	memset(n, 0, sizeof(*n));
   792  	n->op = OLITERAL;
   793  	n->addable = 1;
   794  	ullmancalc(n);
   795  	n->val.u.xval = mal(sizeof(*n->val.u.xval));
   796  	mpmovecfix(n->val.u.xval, v);
   797  	n->val.ctype = CTINT;
   798  	n->type = t;
   799  
   800  	if(isfloat[t->etype])
   801  		fatal("nodconst: bad type %T", t);
   802  }
   803  
   804  Node*
   805  nodnil(void)
   806  {
   807  	Node *c;
   808  
   809  	c = nodintconst(0);
   810  	c->val.ctype = CTNIL;
   811  	c->type = types[TNIL];
   812  	return c;
   813  }
   814  
   815  Node*
   816  nodbool(int b)
   817  {
   818  	Node *c;
   819  
   820  	c = nodintconst(0);
   821  	c->val.ctype = CTBOOL;
   822  	c->val.u.bval = b;
   823  	c->type = idealbool;
   824  	return c;
   825  }
   826  
   827  Type*
   828  aindex(Node *b, Type *t)
   829  {
   830  	Type *r;
   831  	int64 bound;
   832  
   833  	bound = -1;	// open bound
   834  	typecheck(&b, Erv);
   835  	if(b != nil) {
   836  		switch(consttype(b)) {
   837  		default:
   838  			yyerror("array bound must be an integer expression");
   839  			break;
   840  		case CTINT:
   841  		case CTRUNE:
   842  			bound = mpgetfix(b->val.u.xval);
   843  			if(bound < 0)
   844  				yyerror("array bound must be non negative");
   845  			break;
   846  		}
   847  	}
   848  
   849  	// fixed array
   850  	r = typ(TARRAY);
   851  	r->type = t;
   852  	r->bound = bound;
   853  	return r;
   854  }
   855  
   856  Node*
   857  treecopy(Node *n)
   858  {
   859  	Node *m;
   860  
   861  	if(n == N)
   862  		return N;
   863  
   864  	switch(n->op) {
   865  	default:
   866  		m = nod(OXXX, N, N);
   867  		*m = *n;
   868  		m->orig = m;
   869  		m->left = treecopy(n->left);
   870  		m->right = treecopy(n->right);
   871  		m->list = listtreecopy(n->list);
   872  		if(m->defn)
   873  			abort();
   874  		break;
   875  
   876  	case ONONAME:
   877  		if(n->sym == lookup("iota")) {
   878  			// Not sure yet whether this is the real iota,
   879  			// but make a copy of the Node* just in case,
   880  			// so that all the copies of this const definition
   881  			// don't have the same iota value.
   882  			m = nod(OXXX, N, N);
   883  			*m = *n;
   884  			m->iota = iota;
   885  			break;
   886  		}
   887  		// fall through
   888  	case ONAME:
   889  	case OLITERAL:
   890  	case OTYPE:
   891  		m = n;
   892  		break;
   893  	}
   894  	return m;
   895  }
   896  
   897  
   898  int
   899  isnil(Node *n)
   900  {
   901  	if(n == N)
   902  		return 0;
   903  	if(n->op != OLITERAL)
   904  		return 0;
   905  	if(n->val.ctype != CTNIL)
   906  		return 0;
   907  	return 1;
   908  }
   909  
   910  int
   911  isptrto(Type *t, int et)
   912  {
   913  	if(t == T)
   914  		return 0;
   915  	if(!isptr[t->etype])
   916  		return 0;
   917  	t = t->type;
   918  	if(t == T)
   919  		return 0;
   920  	if(t->etype != et)
   921  		return 0;
   922  	return 1;
   923  }
   924  
   925  int
   926  istype(Type *t, int et)
   927  {
   928  	return t != T && t->etype == et;
   929  }
   930  
   931  int
   932  isfixedarray(Type *t)
   933  {
   934  	return t != T && t->etype == TARRAY && t->bound >= 0;
   935  }
   936  
   937  int
   938  isslice(Type *t)
   939  {
   940  	return t != T && t->etype == TARRAY && t->bound < 0;
   941  }
   942  
   943  int
   944  isblank(Node *n)
   945  {
   946  	if(n == N)
   947  		return 0;
   948  	return isblanksym(n->sym);
   949  }
   950  
   951  int
   952  isblanksym(Sym *s)
   953  {
   954  	char *p;
   955  
   956  	if(s == S)
   957  		return 0;
   958  	p = s->name;
   959  	if(p == nil)
   960  		return 0;
   961  	return p[0] == '_' && p[1] == '\0';
   962  }
   963  
   964  int
   965  isinter(Type *t)
   966  {
   967  	return t != T && t->etype == TINTER;
   968  }
   969  
   970  int
   971  isnilinter(Type *t)
   972  {
   973  	if(!isinter(t))
   974  		return 0;
   975  	if(t->type != T)
   976  		return 0;
   977  	return 1;
   978  }
   979  
   980  int
   981  isideal(Type *t)
   982  {
   983  	if(t == T)
   984  		return 0;
   985  	if(t == idealstring || t == idealbool)
   986  		return 1;
   987  	switch(t->etype) {
   988  	case TNIL:
   989  	case TIDEAL:
   990  		return 1;
   991  	}
   992  	return 0;
   993  }
   994  
   995  /*
   996   * given receiver of type t (t == r or t == *r)
   997   * return type to hang methods off (r).
   998   */
   999  Type*
  1000  methtype(Type *t, int mustname)
  1001  {
  1002  	if(t == T)
  1003  		return T;
  1004  
  1005  	// strip away pointer if it's there
  1006  	if(isptr[t->etype]) {
  1007  		if(t->sym != S)
  1008  			return T;
  1009  		t = t->type;
  1010  		if(t == T)
  1011  			return T;
  1012  	}
  1013  
  1014  	// need a type name
  1015  	if(t->sym == S && (mustname || t->etype != TSTRUCT))
  1016  		return T;
  1017  
  1018  	// check types
  1019  	if(!issimple[t->etype])
  1020  	switch(t->etype) {
  1021  	default:
  1022  		return T;
  1023  	case TSTRUCT:
  1024  	case TARRAY:
  1025  	case TMAP:
  1026  	case TCHAN:
  1027  	case TSTRING:
  1028  	case TFUNC:
  1029  		break;
  1030  	}
  1031  
  1032  	return t;
  1033  }
  1034  
  1035  int
  1036  cplxsubtype(int et)
  1037  {
  1038  	switch(et) {
  1039  	case TCOMPLEX64:
  1040  		return TFLOAT32;
  1041  	case TCOMPLEX128:
  1042  		return TFLOAT64;
  1043  	}
  1044  	fatal("cplxsubtype: %E\n", et);
  1045  	return 0;
  1046  }
  1047  
  1048  static int
  1049  eqnote(Strlit *a, Strlit *b)
  1050  {
  1051  	if(a == b)
  1052  		return 1;
  1053  	if(a == nil || b == nil)
  1054  		return 0;
  1055  	if(a->len != b->len)
  1056  		return 0;
  1057  	return memcmp(a->s, b->s, a->len) == 0;
  1058  }
  1059  
  1060  typedef struct TypePairList TypePairList;
  1061  struct TypePairList
  1062  {
  1063  	Type *t1;
  1064  	Type *t2;
  1065  	TypePairList *next;
  1066  };
  1067  
  1068  static int
  1069  onlist(TypePairList *l, Type *t1, Type *t2) 
  1070  {
  1071  	for(; l; l=l->next)
  1072  		if((l->t1 == t1 && l->t2 == t2) || (l->t1 == t2 && l->t2 == t1))
  1073  			return 1;
  1074  	return 0;
  1075  }
  1076  
  1077  static int eqtype1(Type*, Type*, TypePairList*);
  1078  
  1079  // Return 1 if t1 and t2 are identical, following the spec rules.
  1080  //
  1081  // Any cyclic type must go through a named type, and if one is
  1082  // named, it is only identical to the other if they are the same
  1083  // pointer (t1 == t2), so there's no chance of chasing cycles
  1084  // ad infinitum, so no need for a depth counter.
  1085  int
  1086  eqtype(Type *t1, Type *t2)
  1087  {
  1088  	return eqtype1(t1, t2, nil);
  1089  }
  1090  
  1091  static int
  1092  eqtype1(Type *t1, Type *t2, TypePairList *assumed_equal)
  1093  {
  1094  	TypePairList l;
  1095  
  1096  	if(t1 == t2)
  1097  		return 1;
  1098  	if(t1 == T || t2 == T || t1->etype != t2->etype)
  1099  		return 0;
  1100  	if(t1->sym || t2->sym) {
  1101  		// Special case: we keep byte and uint8 separate
  1102  		// for error messages.  Treat them as equal.
  1103  		switch(t1->etype) {
  1104  		case TUINT8:
  1105  			if((t1 == types[TUINT8] || t1 == bytetype) && (t2 == types[TUINT8] || t2 == bytetype))
  1106  				return 1;
  1107  			break;
  1108  		case TINT:
  1109  		case TINT32:
  1110  			if((t1 == types[runetype->etype] || t1 == runetype) && (t2 == types[runetype->etype] || t2 == runetype))
  1111  				return 1;
  1112  			break;
  1113  		}
  1114  		return 0;
  1115  	}
  1116  
  1117  	if(onlist(assumed_equal, t1, t2))
  1118  		return 1;
  1119  	l.next = assumed_equal;
  1120  	l.t1 = t1;
  1121  	l.t2 = t2;
  1122  
  1123  	switch(t1->etype) {
  1124  	case TINTER:
  1125  	case TSTRUCT:
  1126  		for(t1=t1->type, t2=t2->type; t1 && t2; t1=t1->down, t2=t2->down) {
  1127  			if(t1->etype != TFIELD || t2->etype != TFIELD)
  1128  				fatal("struct/interface missing field: %T %T", t1, t2);
  1129  			if(t1->sym != t2->sym || t1->embedded != t2->embedded || !eqtype1(t1->type, t2->type, &l) || !eqnote(t1->note, t2->note))
  1130  				goto no;
  1131  		}
  1132  		if(t1 == T && t2 == T)
  1133  			goto yes;
  1134  		goto no;
  1135  
  1136  	case TFUNC:
  1137  		// Loop over structs: receiver, in, out.
  1138  		for(t1=t1->type, t2=t2->type; t1 && t2; t1=t1->down, t2=t2->down) {
  1139  			Type *ta, *tb;
  1140  
  1141  			if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
  1142  				fatal("func missing struct: %T %T", t1, t2);
  1143  
  1144  			// Loop over fields in structs, ignoring argument names.
  1145  			for(ta=t1->type, tb=t2->type; ta && tb; ta=ta->down, tb=tb->down) {
  1146  				if(ta->etype != TFIELD || tb->etype != TFIELD)
  1147  					fatal("func struct missing field: %T %T", ta, tb);
  1148  				if(ta->isddd != tb->isddd || !eqtype1(ta->type, tb->type, &l))
  1149  					goto no;
  1150  			}
  1151  			if(ta != T || tb != T)
  1152  				goto no;
  1153  		}
  1154  		if(t1 == T && t2 == T)
  1155  			goto yes;
  1156  		goto no;
  1157  	
  1158  	case TARRAY:
  1159  		if(t1->bound != t2->bound)
  1160  			goto no;
  1161  		break;
  1162  	
  1163  	case TCHAN:
  1164  		if(t1->chan != t2->chan)
  1165  			goto no;
  1166  		break;
  1167  	}
  1168  
  1169  	if(eqtype1(t1->down, t2->down, &l) && eqtype1(t1->type, t2->type, &l))
  1170  		goto yes;
  1171  	goto no;
  1172  
  1173  yes:
  1174  	return 1;
  1175  
  1176  no:
  1177  	return 0;
  1178  }
  1179  
  1180  // Are t1 and t2 equal struct types when field names are ignored?
  1181  // For deciding whether the result struct from g can be copied
  1182  // directly when compiling f(g()).
  1183  int
  1184  eqtypenoname(Type *t1, Type *t2)
  1185  {
  1186  	if(t1 == T || t2 == T || t1->etype != TSTRUCT || t2->etype != TSTRUCT)
  1187  		return 0;
  1188  
  1189  	t1 = t1->type;
  1190  	t2 = t2->type;
  1191  	for(;;) {
  1192  		if(!eqtype(t1, t2))
  1193  			return 0;
  1194  		if(t1 == T)
  1195  			return 1;
  1196  		t1 = t1->down;
  1197  		t2 = t2->down;
  1198  	}
  1199  }
  1200  
  1201  // Is type src assignment compatible to type dst?
  1202  // If so, return op code to use in conversion.
  1203  // If not, return 0.
  1204  int
  1205  assignop(Type *src, Type *dst, char **why)
  1206  {
  1207  	Type *missing, *have;
  1208  	int ptr;
  1209  
  1210  	if(why != nil)
  1211  		*why = "";
  1212  
  1213  	// TODO(rsc,lvd): This behaves poorly in the presence of inlining.
  1214  	// https://code.google.com/p/go/issues/detail?id=2795
  1215  	if(safemode && importpkg == nil && src != T && src->etype == TUNSAFEPTR) {
  1216  		yyerror("cannot use unsafe.Pointer");
  1217  		errorexit();
  1218  	}
  1219  
  1220  	if(src == dst)
  1221  		return OCONVNOP;
  1222  	if(src == T || dst == T || src->etype == TFORW || dst->etype == TFORW || src->orig == T || dst->orig == T)
  1223  		return 0;
  1224  
  1225  	// 1. src type is identical to dst.
  1226  	if(eqtype(src, dst))
  1227  		return OCONVNOP;
  1228  	
  1229  	// 2. src and dst have identical underlying types
  1230  	// and either src or dst is not a named type or
  1231  	// both are empty interface types.
  1232  	// For assignable but different non-empty interface types,
  1233  	// we want to recompute the itab.
  1234  	if(eqtype(src->orig, dst->orig) && (src->sym == S || dst->sym == S || isnilinter(src)))
  1235  		return OCONVNOP;
  1236  
  1237  	// 3. dst is an interface type and src implements dst.
  1238  	if(dst->etype == TINTER && src->etype != TNIL) {
  1239  		if(implements(src, dst, &missing, &have, &ptr))
  1240  			return OCONVIFACE;
  1241  
  1242  		// we'll have complained about this method anyway, suppress spurious messages.
  1243  		if(have && have->sym == missing->sym && (have->type->broke || missing->type->broke))
  1244  			return OCONVIFACE;
  1245  
  1246  		if(why != nil) {
  1247  			if(isptrto(src, TINTER))
  1248  				*why = smprint(":\n\t%T is pointer to interface, not interface", src);
  1249  			else if(have && have->sym == missing->sym && have->nointerface)
  1250  				*why = smprint(":\n\t%T does not implement %T (%S method is marked 'nointerface')",
  1251  					src, dst, missing->sym);
  1252  			else if(have && have->sym == missing->sym)
  1253  				*why = smprint(":\n\t%T does not implement %T (wrong type for %S method)\n"
  1254  					"\t\thave %S%hhT\n\t\twant %S%hhT", src, dst, missing->sym,
  1255  					have->sym, have->type, missing->sym, missing->type);
  1256  			else if(ptr)
  1257  				*why = smprint(":\n\t%T does not implement %T (%S method has pointer receiver)",
  1258  					src, dst, missing->sym);
  1259  			else if(have)
  1260  				*why = smprint(":\n\t%T does not implement %T (missing %S method)\n"
  1261  					"\t\thave %S%hhT\n\t\twant %S%hhT", src, dst, missing->sym,
  1262  					have->sym, have->type, missing->sym, missing->type);
  1263  			else
  1264  				*why = smprint(":\n\t%T does not implement %T (missing %S method)",
  1265  					src, dst, missing->sym);
  1266  		}
  1267  		return 0;
  1268  	}
  1269  	if(isptrto(dst, TINTER)) {
  1270  		if(why != nil)
  1271  			*why = smprint(":\n\t%T is pointer to interface, not interface", dst);
  1272  		return 0;
  1273  	}
  1274  	if(src->etype == TINTER && dst->etype != TBLANK) {
  1275  		if(why != nil && implements(dst, src, &missing, &have, &ptr))
  1276  			*why = ": need type assertion";
  1277  		return 0;
  1278  	}
  1279  
  1280  	// 4. src is a bidirectional channel value, dst is a channel type,
  1281  	// src and dst have identical element types, and
  1282  	// either src or dst is not a named type.
  1283  	if(src->etype == TCHAN && src->chan == Cboth && dst->etype == TCHAN)
  1284  	if(eqtype(src->type, dst->type) && (src->sym == S || dst->sym == S))
  1285  		return OCONVNOP;
  1286  
  1287  	// 5. src is the predeclared identifier nil and dst is a nillable type.
  1288  	if(src->etype == TNIL) {
  1289  		switch(dst->etype) {
  1290  		case TARRAY:
  1291  			if(dst->bound != -100)	// not slice
  1292  				break;
  1293  		case TPTR32:
  1294  		case TPTR64:
  1295  		case TFUNC:
  1296  		case TMAP:
  1297  		case TCHAN:
  1298  		case TINTER:
  1299  			return OCONVNOP;
  1300  		}
  1301  	}
  1302  
  1303  	// 6. rule about untyped constants - already converted by defaultlit.
  1304  	
  1305  	// 7. Any typed value can be assigned to the blank identifier.
  1306  	if(dst->etype == TBLANK)
  1307  		return OCONVNOP;
  1308  
  1309  	return 0;
  1310  }
  1311  
  1312  // Can we convert a value of type src to a value of type dst?
  1313  // If so, return op code to use in conversion (maybe OCONVNOP).
  1314  // If not, return 0.
  1315  int
  1316  convertop(Type *src, Type *dst, char **why)
  1317  {
  1318  	int op;
  1319  	
  1320  	if(why != nil)
  1321  		*why = "";
  1322  
  1323  	if(src == dst)
  1324  		return OCONVNOP;
  1325  	if(src == T || dst == T)
  1326  		return 0;
  1327  	
  1328  	// 1. src can be assigned to dst.
  1329  	if((op = assignop(src, dst, why)) != 0)
  1330  		return op;
  1331  
  1332  	// The rules for interfaces are no different in conversions
  1333  	// than assignments.  If interfaces are involved, stop now
  1334  	// with the good message from assignop.
  1335  	// Otherwise clear the error.
  1336  	if(src->etype == TINTER || dst->etype == TINTER)
  1337  		return 0;
  1338  	if(why != nil)
  1339  		*why = "";
  1340  
  1341  	// 2. src and dst have identical underlying types.
  1342  	if(eqtype(src->orig, dst->orig))
  1343  		return OCONVNOP;
  1344  	
  1345  	// 3. src and dst are unnamed pointer types 
  1346  	// and their base types have identical underlying types.
  1347  	if(isptr[src->etype] && isptr[dst->etype] && src->sym == S && dst->sym == S)
  1348  	if(eqtype(src->type->orig, dst->type->orig))
  1349  		return OCONVNOP;
  1350  
  1351  	// 4. src and dst are both integer or floating point types.
  1352  	if((isint[src->etype] || isfloat[src->etype]) && (isint[dst->etype] || isfloat[dst->etype])) {
  1353  		if(simtype[src->etype] == simtype[dst->etype])
  1354  			return OCONVNOP;
  1355  		return OCONV;
  1356  	}
  1357  
  1358  	// 5. src and dst are both complex types.
  1359  	if(iscomplex[src->etype] && iscomplex[dst->etype]) {
  1360  		if(simtype[src->etype] == simtype[dst->etype])
  1361  			return OCONVNOP;
  1362  		return OCONV;
  1363  	}
  1364  
  1365  	// 6. src is an integer or has type []byte or []rune
  1366  	// and dst is a string type.
  1367  	if(isint[src->etype] && dst->etype == TSTRING)
  1368  		return ORUNESTR;
  1369  
  1370  	if(isslice(src) && dst->etype == TSTRING) {
  1371  		if(src->type->etype == bytetype->etype)
  1372  			return OARRAYBYTESTR;
  1373  		if(src->type->etype == runetype->etype)
  1374  			return OARRAYRUNESTR;
  1375  	}
  1376  	
  1377  	// 7. src is a string and dst is []byte or []rune.
  1378  	// String to slice.
  1379  	if(src->etype == TSTRING && isslice(dst)) {
  1380  		if(dst->type->etype == bytetype->etype)
  1381  			return OSTRARRAYBYTE;
  1382  		if(dst->type->etype == runetype->etype)
  1383  			return OSTRARRAYRUNE;
  1384  	}
  1385  	
  1386  	// 8. src is a pointer or uintptr and dst is unsafe.Pointer.
  1387  	if((isptr[src->etype] || src->etype == TUINTPTR) && dst->etype == TUNSAFEPTR)
  1388  		return OCONVNOP;
  1389  
  1390  	// 9. src is unsafe.Pointer and dst is a pointer or uintptr.
  1391  	if(src->etype == TUNSAFEPTR && (isptr[dst->etype] || dst->etype == TUINTPTR))
  1392  		return OCONVNOP;
  1393  
  1394  	return 0;
  1395  }
  1396  
  1397  // Convert node n for assignment to type t.
  1398  Node*
  1399  assignconv(Node *n, Type *t, char *context)
  1400  {
  1401  	int op;
  1402  	Node *r, *old;
  1403  	char *why;
  1404  	
  1405  	if(n == N || n->type == T || n->type->broke)
  1406  		return n;
  1407  
  1408  	if(t->etype == TBLANK && n->type->etype == TNIL)
  1409  		yyerror("use of untyped nil");
  1410  
  1411  	old = n;
  1412  	old->diag++;  // silence errors about n; we'll issue one below
  1413  	defaultlit(&n, t);
  1414  	old->diag--;
  1415  	if(t->etype == TBLANK)
  1416  		return n;
  1417  
  1418  	// Convert ideal bool from comparison to plain bool
  1419  	// if the next step is non-bool (like interface{}).
  1420  	if(n->type == idealbool && t->etype != TBOOL) {
  1421  		if(n->op == ONAME || n->op == OLITERAL) {
  1422  			r = nod(OCONVNOP, n, N);
  1423  			r->type = types[TBOOL];
  1424  			r->typecheck = 1;
  1425  			r->implicit = 1;
  1426  			n = r;
  1427  		}
  1428  	}
  1429  
  1430  	if(eqtype(n->type, t))
  1431  		return n;
  1432  
  1433  	op = assignop(n->type, t, &why);
  1434  	if(op == 0) {
  1435  		yyerror("cannot use %lN as type %T in %s%s", n, t, context, why);
  1436  		op = OCONV;
  1437  	}
  1438  
  1439  	r = nod(op, n, N);
  1440  	r->type = t;
  1441  	r->typecheck = 1;
  1442  	r->implicit = 1;
  1443  	r->orig = n->orig;
  1444  	return r;
  1445  }
  1446  
  1447  static int
  1448  subtype(Type **stp, Type *t, int d)
  1449  {
  1450  	Type *st;
  1451  
  1452  loop:
  1453  	st = *stp;
  1454  	if(st == T)
  1455  		return 0;
  1456  
  1457  	d++;
  1458  	if(d >= 10)
  1459  		return 0;
  1460  
  1461  	switch(st->etype) {
  1462  	default:
  1463  		return 0;
  1464  
  1465  	case TPTR32:
  1466  	case TPTR64:
  1467  	case TCHAN:
  1468  	case TARRAY:
  1469  		stp = &st->type;
  1470  		goto loop;
  1471  
  1472  	case TANY:
  1473  		if(!st->copyany)
  1474  			return 0;
  1475  		*stp = t;
  1476  		break;
  1477  
  1478  	case TMAP:
  1479  		if(subtype(&st->down, t, d))
  1480  			break;
  1481  		stp = &st->type;
  1482  		goto loop;
  1483  
  1484  	case TFUNC:
  1485  		for(;;) {
  1486  			if(subtype(&st->type, t, d))
  1487  				break;
  1488  			if(subtype(&st->type->down->down, t, d))
  1489  				break;
  1490  			if(subtype(&st->type->down, t, d))
  1491  				break;
  1492  			return 0;
  1493  		}
  1494  		break;
  1495  
  1496  	case TSTRUCT:
  1497  		for(st=st->type; st!=T; st=st->down)
  1498  			if(subtype(&st->type, t, d))
  1499  				return 1;
  1500  		return 0;
  1501  	}
  1502  	return 1;
  1503  }
  1504  
  1505  /*
  1506   * Is this a 64-bit type?
  1507   */
  1508  int
  1509  is64(Type *t)
  1510  {
  1511  	if(t == T)
  1512  		return 0;
  1513  	switch(simtype[t->etype]) {
  1514  	case TINT64:
  1515  	case TUINT64:
  1516  	case TPTR64:
  1517  		return 1;
  1518  	}
  1519  	return 0;
  1520  }
  1521  
  1522  /*
  1523   * Is a conversion between t1 and t2 a no-op?
  1524   */
  1525  int
  1526  noconv(Type *t1, Type *t2)
  1527  {
  1528  	int e1, e2;
  1529  
  1530  	e1 = simtype[t1->etype];
  1531  	e2 = simtype[t2->etype];
  1532  
  1533  	switch(e1) {
  1534  	case TINT8:
  1535  	case TUINT8:
  1536  		return e2 == TINT8 || e2 == TUINT8;
  1537  
  1538  	case TINT16:
  1539  	case TUINT16:
  1540  		return e2 == TINT16 || e2 == TUINT16;
  1541  
  1542  	case TINT32:
  1543  	case TUINT32:
  1544  	case TPTR32:
  1545  		return e2 == TINT32 || e2 == TUINT32 || e2 == TPTR32;
  1546  
  1547  	case TINT64:
  1548  	case TUINT64:
  1549  	case TPTR64:
  1550  		return e2 == TINT64 || e2 == TUINT64 || e2 == TPTR64;
  1551  
  1552  	case TFLOAT32:
  1553  		return e2 == TFLOAT32;
  1554  
  1555  	case TFLOAT64:
  1556  		return e2 == TFLOAT64;
  1557  	}
  1558  	return 0;
  1559  }
  1560  
  1561  void
  1562  argtype(Node *on, Type *t)
  1563  {
  1564  	dowidth(t);
  1565  	if(!subtype(&on->type, t, 0))
  1566  		fatal("argtype: failed %N %T\n", on, t);
  1567  }
  1568  
  1569  Type*
  1570  shallow(Type *t)
  1571  {
  1572  	Type *nt;
  1573  
  1574  	if(t == T)
  1575  		return T;
  1576  	nt = typ(0);
  1577  	*nt = *t;
  1578  	if(t->orig == t)
  1579  		nt->orig = nt;
  1580  	return nt;
  1581  }
  1582  
  1583  static Type*
  1584  deep(Type *t)
  1585  {
  1586  	Type *nt, *xt;
  1587  
  1588  	if(t == T)
  1589  		return T;
  1590  
  1591  	switch(t->etype) {
  1592  	default:
  1593  		nt = t;	// share from here down
  1594  		break;
  1595  
  1596  	case TANY:
  1597  		nt = shallow(t);
  1598  		nt->copyany = 1;
  1599  		break;
  1600  
  1601  	case TPTR32:
  1602  	case TPTR64:
  1603  	case TCHAN:
  1604  	case TARRAY:
  1605  		nt = shallow(t);
  1606  		nt->type = deep(t->type);
  1607  		break;
  1608  
  1609  	case TMAP:
  1610  		nt = shallow(t);
  1611  		nt->down = deep(t->down);
  1612  		nt->type = deep(t->type);
  1613  		break;
  1614  
  1615  	case TFUNC:
  1616  		nt = shallow(t);
  1617  		nt->type = deep(t->type);
  1618  		nt->type->down = deep(t->type->down);
  1619  		nt->type->down->down = deep(t->type->down->down);
  1620  		break;
  1621  
  1622  	case TSTRUCT:
  1623  		nt = shallow(t);
  1624  		nt->type = shallow(t->type);
  1625  		xt = nt->type;
  1626  
  1627  		for(t=t->type; t!=T; t=t->down) {
  1628  			xt->type = deep(t->type);
  1629  			xt->down = shallow(t->down);
  1630  			xt = xt->down;
  1631  		}
  1632  		break;
  1633  	}
  1634  	return nt;
  1635  }
  1636  
  1637  Node*
  1638  syslook(char *name, int copy)
  1639  {
  1640  	Sym *s;
  1641  	Node *n;
  1642  
  1643  	s = pkglookup(name, runtimepkg);
  1644  	if(s == S || s->def == N)
  1645  		fatal("syslook: can't find runtime.%s", name);
  1646  
  1647  	if(!copy)
  1648  		return s->def;
  1649  
  1650  	n = nod(0, N, N);
  1651  	*n = *s->def;
  1652  	n->type = deep(s->def->type);
  1653  
  1654  	return n;
  1655  }
  1656  
  1657  /*
  1658   * compute a hash value for type t.
  1659   * if t is a method type, ignore the receiver
  1660   * so that the hash can be used in interface checks.
  1661   * %T already contains
  1662   * all the necessary logic to generate a representation
  1663   * of the type that completely describes it.
  1664   * using smprint here avoids duplicating that code.
  1665   * using md5 here is overkill, but i got tired of
  1666   * accidental collisions making the runtime think
  1667   * two types are equal when they really aren't.
  1668   */
  1669  uint32
  1670  typehash(Type *t)
  1671  {
  1672  	char *p;
  1673  	MD5 d;
  1674  
  1675  	if(t->thistuple) {
  1676  		// hide method receiver from Tpretty
  1677  		t->thistuple = 0;
  1678  		p = smprint("%-uT", t);
  1679  		t->thistuple = 1;
  1680  	} else
  1681  		p = smprint("%-uT", t);
  1682  	//print("typehash: %s\n", p);
  1683  	md5reset(&d);
  1684  	md5write(&d, (uchar*)p, strlen(p));
  1685  	free(p);
  1686  	return md5sum(&d, nil);
  1687  }
  1688  
  1689  Type*
  1690  ptrto(Type *t)
  1691  {
  1692  	Type *t1;
  1693  
  1694  	if(tptr == 0)
  1695  		fatal("ptrto: no tptr");
  1696  	t1 = typ(tptr);
  1697  	t1->type = t;
  1698  	t1->width = widthptr;
  1699  	t1->align = widthptr;
  1700  	return t1;
  1701  }
  1702  
  1703  void
  1704  frame(int context)
  1705  {
  1706  	char *p;
  1707  	NodeList *l;
  1708  	Node *n;
  1709  	int flag;
  1710  
  1711  	p = "stack";
  1712  	l = nil;
  1713  	if(curfn)
  1714  		l = curfn->dcl;
  1715  	if(context) {
  1716  		p = "external";
  1717  		l = externdcl;
  1718  	}
  1719  
  1720  	flag = 1;
  1721  	for(; l; l=l->next) {
  1722  		n = l->n;
  1723  		switch(n->op) {
  1724  		case ONAME:
  1725  			if(flag)
  1726  				print("--- %s frame ---\n", p);
  1727  			print("%O %S G%d %T\n", n->op, n->sym, n->vargen, n->type);
  1728  			flag = 0;
  1729  			break;
  1730  
  1731  		case OTYPE:
  1732  			if(flag)
  1733  				print("--- %s frame ---\n", p);
  1734  			print("%O %T\n", n->op, n->type);
  1735  			flag = 0;
  1736  			break;
  1737  		}
  1738  	}
  1739  }
  1740  
  1741  /*
  1742   * calculate sethi/ullman number
  1743   * roughly how many registers needed to
  1744   * compile a node. used to compile the
  1745   * hardest side first to minimize registers.
  1746   */
  1747  void
  1748  ullmancalc(Node *n)
  1749  {
  1750  	int ul, ur;
  1751  
  1752  	if(n == N)
  1753  		return;
  1754  
  1755  	if(n->ninit != nil) {
  1756  		ul = UINF;
  1757  		goto out;
  1758  	}
  1759  
  1760  	switch(n->op) {
  1761  	case OREGISTER:
  1762  	case OLITERAL:
  1763  	case ONAME:
  1764  		ul = 1;
  1765  		if(n->class == PPARAMREF || (n->class & PHEAP))
  1766  			ul++;
  1767  		goto out;
  1768  	case OCALL:
  1769  	case OCALLFUNC:
  1770  	case OCALLMETH:
  1771  	case OCALLINTER:
  1772  		ul = UINF;
  1773  		goto out;
  1774  	case OANDAND:
  1775  	case OOROR:
  1776  		// hard with race detector
  1777  		if(flag_race) {
  1778  			ul = UINF;
  1779  			goto out;
  1780  		}
  1781  	}
  1782  	ul = 1;
  1783  	if(n->left != N)
  1784  		ul = n->left->ullman;
  1785  	ur = 1;
  1786  	if(n->right != N)
  1787  		ur = n->right->ullman;
  1788  	if(ul == ur)
  1789  		ul += 1;
  1790  	if(ur > ul)
  1791  		ul = ur;
  1792  
  1793  out:
  1794  	if(ul > 200)
  1795  		ul = 200; // clamp to uchar with room to grow
  1796  	n->ullman = ul;
  1797  }
  1798  
  1799  void
  1800  badtype(int o, Type *tl, Type *tr)
  1801  {
  1802  	Fmt fmt;
  1803  	char *s;
  1804  	
  1805  	fmtstrinit(&fmt);
  1806  	if(tl != T)
  1807  		fmtprint(&fmt, "\n	%T", tl);
  1808  	if(tr != T)
  1809  		fmtprint(&fmt, "\n	%T", tr);
  1810  
  1811  	// common mistake: *struct and *interface.
  1812  	if(tl && tr && isptr[tl->etype] && isptr[tr->etype]) {
  1813  		if(tl->type->etype == TSTRUCT && tr->type->etype == TINTER)
  1814  			fmtprint(&fmt, "\n	(*struct vs *interface)");
  1815  		else if(tl->type->etype == TINTER && tr->type->etype == TSTRUCT)
  1816  			fmtprint(&fmt, "\n	(*interface vs *struct)");
  1817  	}
  1818  	s = fmtstrflush(&fmt);
  1819  	yyerror("illegal types for operand: %O%s", o, s);
  1820  }
  1821  
  1822  /*
  1823   * iterator to walk a structure declaration
  1824   */
  1825  Type*
  1826  structfirst(Iter *s, Type **nn)
  1827  {
  1828  	Type *n, *t;
  1829  
  1830  	n = *nn;
  1831  	if(n == T)
  1832  		goto bad;
  1833  
  1834  	switch(n->etype) {
  1835  	default:
  1836  		goto bad;
  1837  
  1838  	case TSTRUCT:
  1839  	case TINTER:
  1840  	case TFUNC:
  1841  		break;
  1842  	}
  1843  
  1844  	t = n->type;
  1845  	if(t == T)
  1846  		goto rnil;
  1847  
  1848  	if(t->etype != TFIELD)
  1849  		fatal("structfirst: not field %T", t);
  1850  
  1851  	s->t = t;
  1852  	return t;
  1853  
  1854  bad:
  1855  	fatal("structfirst: not struct %T", n);
  1856  
  1857  rnil:
  1858  	return T;
  1859  }
  1860  
  1861  Type*
  1862  structnext(Iter *s)
  1863  {
  1864  	Type *n, *t;
  1865  
  1866  	n = s->t;
  1867  	t = n->down;
  1868  	if(t == T)
  1869  		goto rnil;
  1870  
  1871  	if(t->etype != TFIELD)
  1872  		goto bad;
  1873  
  1874  	s->t = t;
  1875  	return t;
  1876  
  1877  bad:
  1878  	fatal("structnext: not struct %T", n);
  1879  
  1880  rnil:
  1881  	return T;
  1882  }
  1883  
  1884  /*
  1885   * iterator to this and inargs in a function
  1886   */
  1887  Type*
  1888  funcfirst(Iter *s, Type *t)
  1889  {
  1890  	Type *fp;
  1891  
  1892  	if(t == T)
  1893  		goto bad;
  1894  
  1895  	if(t->etype != TFUNC)
  1896  		goto bad;
  1897  
  1898  	s->tfunc = t;
  1899  	s->done = 0;
  1900  	fp = structfirst(s, getthis(t));
  1901  	if(fp == T) {
  1902  		s->done = 1;
  1903  		fp = structfirst(s, getinarg(t));
  1904  	}
  1905  	return fp;
  1906  
  1907  bad:
  1908  	fatal("funcfirst: not func %T", t);
  1909  	return T;
  1910  }
  1911  
  1912  Type*
  1913  funcnext(Iter *s)
  1914  {
  1915  	Type *fp;
  1916  
  1917  	fp = structnext(s);
  1918  	if(fp == T && !s->done) {
  1919  		s->done = 1;
  1920  		fp = structfirst(s, getinarg(s->tfunc));
  1921  	}
  1922  	return fp;
  1923  }
  1924  
  1925  Type**
  1926  getthis(Type *t)
  1927  {
  1928  	if(t->etype != TFUNC)
  1929  		fatal("getthis: not a func %T", t);
  1930  	return &t->type;
  1931  }
  1932  
  1933  Type**
  1934  getoutarg(Type *t)
  1935  {
  1936  	if(t->etype != TFUNC)
  1937  		fatal("getoutarg: not a func %T", t);
  1938  	return &t->type->down;
  1939  }
  1940  
  1941  Type**
  1942  getinarg(Type *t)
  1943  {
  1944  	if(t->etype != TFUNC)
  1945  		fatal("getinarg: not a func %T", t);
  1946  	return &t->type->down->down;
  1947  }
  1948  
  1949  Type*
  1950  getthisx(Type *t)
  1951  {
  1952  	return *getthis(t);
  1953  }
  1954  
  1955  Type*
  1956  getoutargx(Type *t)
  1957  {
  1958  	return *getoutarg(t);
  1959  }
  1960  
  1961  Type*
  1962  getinargx(Type *t)
  1963  {
  1964  	return *getinarg(t);
  1965  }
  1966  
  1967  /*
  1968   * return !(op)
  1969   * eg == <=> !=
  1970   */
  1971  int
  1972  brcom(int a)
  1973  {
  1974  	switch(a) {
  1975  	case OEQ:	return ONE;
  1976  	case ONE:	return OEQ;
  1977  	case OLT:	return OGE;
  1978  	case OGT:	return OLE;
  1979  	case OLE:	return OGT;
  1980  	case OGE:	return OLT;
  1981  	}
  1982  	fatal("brcom: no com for %A\n", a);
  1983  	return a;
  1984  }
  1985  
  1986  /*
  1987   * return reverse(op)
  1988   * eg a op b <=> b r(op) a
  1989   */
  1990  int
  1991  brrev(int a)
  1992  {
  1993  	switch(a) {
  1994  	case OEQ:	return OEQ;
  1995  	case ONE:	return ONE;
  1996  	case OLT:	return OGT;
  1997  	case OGT:	return OLT;
  1998  	case OLE:	return OGE;
  1999  	case OGE:	return OLE;
  2000  	}
  2001  	fatal("brcom: no rev for %A\n", a);
  2002  	return a;
  2003  }
  2004  
  2005  /*
  2006   * return side effect-free n, appending side effects to init.
  2007   * result is assignable if n is.
  2008   */
  2009  Node*
  2010  safeexpr(Node *n, NodeList **init)
  2011  {
  2012  	Node *l;
  2013  	Node *r;
  2014  	Node *a;
  2015  
  2016  	if(n == N)
  2017  		return N;
  2018  
  2019  	if(n->ninit) {
  2020  		walkstmtlist(n->ninit);
  2021  		*init = concat(*init, n->ninit);
  2022  		n->ninit = nil;
  2023  	}
  2024  
  2025  	switch(n->op) {
  2026  	case ONAME:
  2027  	case OLITERAL:
  2028  		return n;
  2029  
  2030  	case ODOT:
  2031  		l = safeexpr(n->left, init);
  2032  		if(l == n->left)
  2033  			return n;
  2034  		r = nod(OXXX, N, N);
  2035  		*r = *n;
  2036  		r->left = l;
  2037  		typecheck(&r, Erv);
  2038  		walkexpr(&r, init);
  2039  		return r;
  2040  
  2041  	case ODOTPTR:
  2042  	case OIND:
  2043  		l = safeexpr(n->left, init);
  2044  		if(l == n->left)
  2045  			return n;
  2046  		a = nod(OXXX, N, N);
  2047  		*a = *n;
  2048  		a->left = l;
  2049  		walkexpr(&a, init);
  2050  		return a;
  2051  
  2052  	case OINDEX:
  2053  	case OINDEXMAP:
  2054  		l = safeexpr(n->left, init);
  2055  		r = safeexpr(n->right, init);
  2056  		if(l == n->left && r == n->right)
  2057  			return n;
  2058  		a = nod(OXXX, N, N);
  2059  		*a = *n;
  2060  		a->left = l;
  2061  		a->right = r;
  2062  		walkexpr(&a, init);
  2063  		return a;
  2064  	}
  2065  
  2066  	// make a copy; must not be used as an lvalue
  2067  	if(islvalue(n))
  2068  		fatal("missing lvalue case in safeexpr: %N", n);
  2069  	return cheapexpr(n, init);
  2070  }
  2071  
  2072  Node*
  2073  copyexpr(Node *n, Type *t, NodeList **init)
  2074  {
  2075  	Node *a, *l;
  2076  	
  2077  	l = temp(t);
  2078  	a = nod(OAS, l, n);
  2079  	typecheck(&a, Etop);
  2080  	walkexpr(&a, init);
  2081  	*init = list(*init, a);
  2082  	return l;
  2083  }
  2084  
  2085  /*
  2086   * return side-effect free and cheap n, appending side effects to init.
  2087   * result may not be assignable.
  2088   */
  2089  Node*
  2090  cheapexpr(Node *n, NodeList **init)
  2091  {
  2092  	switch(n->op) {
  2093  	case ONAME:
  2094  	case OLITERAL:
  2095  		return n;
  2096  	}
  2097  
  2098  	return copyexpr(n, n->type, init);
  2099  }
  2100  
  2101  /*
  2102   * return n in a local variable of type t if it is not already.
  2103   * the value is guaranteed not to change except by direct
  2104   * assignment to it.
  2105   */
  2106  Node*
  2107  localexpr(Node *n, Type *t, NodeList **init)
  2108  {
  2109  	if(n->op == ONAME && (!n->addrtaken || strncmp(n->sym->name, "autotmp_", 8) == 0) &&
  2110  		(n->class == PAUTO || n->class == PPARAM || n->class == PPARAMOUT) &&
  2111  		convertop(n->type, t, nil) == OCONVNOP)
  2112  		return n;
  2113  	
  2114  	return copyexpr(n, t, init);
  2115  }
  2116  
  2117  void
  2118  setmaxarg(Type *t, int32 extra)
  2119  {
  2120  	int64 w;
  2121  
  2122  	dowidth(t);
  2123  	w = t->argwid;
  2124  	if(w >= MAXWIDTH)
  2125  		fatal("bad argwid %T", t);
  2126  	w += extra;
  2127  	if(w >= MAXWIDTH)
  2128  		fatal("bad argwid %d + %T", extra, t);
  2129  	if(w > maxarg)
  2130  		maxarg = w;
  2131  }
  2132  
  2133  /*
  2134   * unicode-aware case-insensitive strcmp
  2135   */
  2136  
  2137  static int
  2138  ucistrcmp(char *p, char *q)
  2139  {
  2140  	Rune rp, rq;
  2141  
  2142  	while(*p || *q) {
  2143  		if(*p == 0)
  2144  			return +1;
  2145  		if(*q == 0)
  2146  			return -1;
  2147  		p += chartorune(&rp, p);
  2148  		q += chartorune(&rq, q);
  2149  		rp = tolowerrune(rp);
  2150  		rq = tolowerrune(rq);
  2151  		if(rp < rq)
  2152  			return -1;
  2153  		if(rp > rq)
  2154  			return +1;
  2155  	}
  2156  	return 0;
  2157  }
  2158  
  2159  /*
  2160   * code to resolve elided DOTs
  2161   * in embedded types
  2162   */
  2163  
  2164  // search depth 0 --
  2165  // return count of fields+methods
  2166  // found with a given name
  2167  static int
  2168  lookdot0(Sym *s, Type *t, Type **save, int ignorecase)
  2169  {
  2170  	Type *f, *u;
  2171  	int c;
  2172  
  2173  	u = t;
  2174  	if(isptr[u->etype])
  2175  		u = u->type;
  2176  
  2177  	c = 0;
  2178  	if(u->etype == TSTRUCT || u->etype == TINTER) {
  2179  		for(f=u->type; f!=T; f=f->down)
  2180  			if(f->sym == s || (ignorecase && f->type->etype == TFUNC && f->type->thistuple > 0 && ucistrcmp(f->sym->name, s->name) == 0)) {
  2181  				if(save)
  2182  					*save = f;
  2183  				c++;
  2184  			}
  2185  	}
  2186  	u = methtype(t, 0);
  2187  	if(u != T) {
  2188  		for(f=u->method; f!=T; f=f->down)
  2189  			if(f->embedded == 0 && (f->sym == s || (ignorecase && ucistrcmp(f->sym->name, s->name) == 0))) {
  2190  				if(save)
  2191  					*save = f;
  2192  				c++;
  2193  			}
  2194  	}
  2195  	return c;
  2196  }
  2197  
  2198  // search depth d for field/method s --
  2199  // return count of fields+methods
  2200  // found at search depth.
  2201  // answer is in dotlist array and
  2202  // count of number of ways is returned.
  2203  int
  2204  adddot1(Sym *s, Type *t, int d, Type **save, int ignorecase)
  2205  {
  2206  	Type *f, *u;
  2207  	int c, a;
  2208  
  2209  	if(t->trecur)
  2210  		return 0;
  2211  	t->trecur = 1;
  2212  
  2213  	if(d == 0) {
  2214  		c = lookdot0(s, t, save, ignorecase);
  2215  		goto out;
  2216  	}
  2217  
  2218  	c = 0;
  2219  	u = t;
  2220  	if(isptr[u->etype])
  2221  		u = u->type;
  2222  	if(u->etype != TSTRUCT && u->etype != TINTER)
  2223  		goto out;
  2224  
  2225  	d--;
  2226  	for(f=u->type; f!=T; f=f->down) {
  2227  		if(!f->embedded)
  2228  			continue;
  2229  		if(f->sym == S)
  2230  			continue;
  2231  		a = adddot1(s, f->type, d, save, ignorecase);
  2232  		if(a != 0 && c == 0)
  2233  			dotlist[d].field = f;
  2234  		c += a;
  2235  	}
  2236  
  2237  out:
  2238  	t->trecur = 0;
  2239  	return c;
  2240  }
  2241  
  2242  // in T.field
  2243  // find missing fields that
  2244  // will give shortest unique addressing.
  2245  // modify the tree with missing type names.
  2246  Node*
  2247  adddot(Node *n)
  2248  {
  2249  	Type *t;
  2250  	Sym *s;
  2251  	int c, d;
  2252  
  2253  	typecheck(&n->left, Etype|Erv);
  2254  	n->diag |= n->left->diag;
  2255  	t = n->left->type;
  2256  	if(t == T)
  2257  		goto ret;
  2258  	
  2259  	if(n->left->op == OTYPE)
  2260  		goto ret;
  2261  
  2262  	if(n->right->op != ONAME)
  2263  		goto ret;
  2264  	s = n->right->sym;
  2265  	if(s == S)
  2266  		goto ret;
  2267  
  2268  	for(d=0; d<nelem(dotlist); d++) {
  2269  		c = adddot1(s, t, d, nil, 0);
  2270  		if(c > 0)
  2271  			goto out;
  2272  	}
  2273  	goto ret;
  2274  
  2275  out:
  2276  	if(c > 1) {
  2277  		yyerror("ambiguous selector %N", n);
  2278  		n->left = N;
  2279  		return n;
  2280  	}
  2281  
  2282  	// rebuild elided dots
  2283  	for(c=d-1; c>=0; c--)
  2284  		n->left = nod(ODOT, n->left, newname(dotlist[c].field->sym));
  2285  ret:
  2286  	return n;
  2287  }
  2288  
  2289  
  2290  /*
  2291   * code to help generate trampoline
  2292   * functions for methods on embedded
  2293   * subtypes.
  2294   * these are approx the same as
  2295   * the corresponding adddot routines
  2296   * except that they expect to be called
  2297   * with unique tasks and they return
  2298   * the actual methods.
  2299   */
  2300  
  2301  typedef	struct	Symlink	Symlink;
  2302  struct	Symlink
  2303  {
  2304  	Type*		field;
  2305  	uchar		good;
  2306  	uchar		followptr;
  2307  	Symlink*	link;
  2308  };
  2309  static	Symlink*	slist;
  2310  
  2311  static void
  2312  expand0(Type *t, int followptr)
  2313  {
  2314  	Type *f, *u;
  2315  	Symlink *sl;
  2316  
  2317  	u = t;
  2318  	if(isptr[u->etype]) {
  2319  		followptr = 1;
  2320  		u = u->type;
  2321  	}
  2322  
  2323  	if(u->etype == TINTER) {
  2324  		for(f=u->type; f!=T; f=f->down) {
  2325  			if(f->sym->flags & SymUniq)
  2326  				continue;
  2327  			f->sym->flags |= SymUniq;
  2328  			sl = mal(sizeof(*sl));
  2329  			sl->field = f;
  2330  			sl->link = slist;
  2331  			sl->followptr = followptr;
  2332  			slist = sl;
  2333  		}
  2334  		return;
  2335  	}
  2336  
  2337  	u = methtype(t, 0);
  2338  	if(u != T) {
  2339  		for(f=u->method; f!=T; f=f->down) {
  2340  			if(f->sym->flags & SymUniq)
  2341  				continue;
  2342  			f->sym->flags |= SymUniq;
  2343  			sl = mal(sizeof(*sl));
  2344  			sl->field = f;
  2345  			sl->link = slist;
  2346  			sl->followptr = followptr;
  2347  			slist = sl;
  2348  		}
  2349  	}
  2350  }
  2351  
  2352  static void
  2353  expand1(Type *t, int d, int followptr)
  2354  {
  2355  	Type *f, *u;
  2356  
  2357  	if(t->trecur)
  2358  		return;
  2359  	if(d == 0)
  2360  		return;
  2361  	t->trecur = 1;
  2362  
  2363  	if(d != nelem(dotlist)-1)
  2364  		expand0(t, followptr);
  2365  
  2366  	u = t;
  2367  	if(isptr[u->etype]) {
  2368  		followptr = 1;
  2369  		u = u->type;
  2370  	}
  2371  	if(u->etype != TSTRUCT && u->etype != TINTER)
  2372  		goto out;
  2373  
  2374  	for(f=u->type; f!=T; f=f->down) {
  2375  		if(!f->embedded)
  2376  			continue;
  2377  		if(f->sym == S)
  2378  			continue;
  2379  		expand1(f->type, d-1, followptr);
  2380  	}
  2381  
  2382  out:
  2383  	t->trecur = 0;
  2384  }
  2385  
  2386  void
  2387  expandmeth(Type *t)
  2388  {
  2389  	Symlink *sl;
  2390  	Type *f;
  2391  	int c, d;
  2392  
  2393  	if(t == T || t->xmethod != nil)
  2394  		return;
  2395  
  2396  	// mark top-level method symbols
  2397  	// so that expand1 doesn't consider them.
  2398  	for(f=t->method; f != nil; f=f->down)
  2399  		f->sym->flags |= SymUniq;
  2400  
  2401  	// generate all reachable methods
  2402  	slist = nil;
  2403  	expand1(t, nelem(dotlist)-1, 0);
  2404  
  2405  	// check each method to be uniquely reachable
  2406  	for(sl=slist; sl!=nil; sl=sl->link) {
  2407  		sl->field->sym->flags &= ~SymUniq;
  2408  		for(d=0; d<nelem(dotlist); d++) {
  2409  			c = adddot1(sl->field->sym, t, d, &f, 0);
  2410  			if(c == 0)
  2411  				continue;
  2412  			if(c == 1) {
  2413  				// addot1 may have dug out arbitrary fields, we only want methods.
  2414  				if(f->type->etype == TFUNC && f->type->thistuple > 0) {
  2415  					sl->good = 1;
  2416  					sl->field = f;
  2417  				}
  2418  			}
  2419  			break;
  2420  		}
  2421  	}
  2422  
  2423  	for(f=t->method; f != nil; f=f->down)
  2424  		f->sym->flags &= ~SymUniq;
  2425  
  2426  	t->xmethod = t->method;
  2427  	for(sl=slist; sl!=nil; sl=sl->link) {
  2428  		if(sl->good) {
  2429  			// add it to the base type method list
  2430  			f = typ(TFIELD);
  2431  			*f = *sl->field;
  2432  			f->embedded = 1;	// needs a trampoline
  2433  			if(sl->followptr)
  2434  				f->embedded = 2;
  2435  			f->down = t->xmethod;
  2436  			t->xmethod = f;
  2437  		}
  2438  	}
  2439  }
  2440  
  2441  /*
  2442   * Given funarg struct list, return list of ODCLFIELD Node fn args.
  2443   */
  2444  static NodeList*
  2445  structargs(Type **tl, int mustname)
  2446  {
  2447  	Iter savet;
  2448  	Node *a, *n;
  2449  	NodeList *args;
  2450  	Type *t;
  2451  	char buf[100];
  2452  	int gen;
  2453  
  2454  	args = nil;
  2455  	gen = 0;
  2456  	for(t = structfirst(&savet, tl); t != T; t = structnext(&savet)) {
  2457  		n = N;
  2458  		if(mustname && (t->sym == nil || strcmp(t->sym->name, "_") == 0)) {
  2459  			// invent a name so that we can refer to it in the trampoline
  2460  			snprint(buf, sizeof buf, ".anon%d", gen++);
  2461  			n = newname(lookup(buf));
  2462  		} else if(t->sym)
  2463  			n = newname(t->sym);
  2464  		a = nod(ODCLFIELD, n, typenod(t->type));
  2465  		a->isddd = t->isddd;
  2466  		if(n != N)
  2467  			n->isddd = t->isddd;
  2468  		args = list(args, a);
  2469  	}
  2470  	return args;
  2471  }
  2472  
  2473  /*
  2474   * Generate a wrapper function to convert from
  2475   * a receiver of type T to a receiver of type U.
  2476   * That is,
  2477   *
  2478   *	func (t T) M() {
  2479   *		...
  2480   *	}
  2481   *
  2482   * already exists; this function generates
  2483   *
  2484   *	func (u U) M() {
  2485   *		u.M()
  2486   *	}
  2487   *
  2488   * where the types T and U are such that u.M() is valid
  2489   * and calls the T.M method.
  2490   * The resulting function is for use in method tables.
  2491   *
  2492   *	rcvr - U
  2493   *	method - M func (t T)(), a TFIELD type struct
  2494   *	newnam - the eventual mangled name of this function
  2495   */
  2496  void
  2497  genwrapper(Type *rcvr, Type *method, Sym *newnam, int iface)
  2498  {
  2499  	Node *this, *fn, *call, *n, *t, *pad, *dot, *as;
  2500  	NodeList *l, *args, *in, *out;
  2501  	Type *tpad, *methodrcvr;
  2502  	int isddd;
  2503  	Val v;
  2504  	static int linehistdone = 0;
  2505  
  2506  	if(0 && debug['r'])
  2507  		print("genwrapper rcvrtype=%T method=%T newnam=%S\n",
  2508  			rcvr, method, newnam);
  2509  
  2510  	lexlineno++;
  2511  	lineno = lexlineno;
  2512  	if (linehistdone == 0) {
  2513  		// All the wrappers can share the same linehist entry.
  2514  		linehist("<autogenerated>", 0, 0);
  2515  		linehistdone = 1;
  2516  	}
  2517  
  2518  	dclcontext = PEXTERN;
  2519  	markdcl();
  2520  
  2521  	this = nod(ODCLFIELD, newname(lookup(".this")), typenod(rcvr));
  2522  	this->left->ntype = this->right;
  2523  	in = structargs(getinarg(method->type), 1);
  2524  	out = structargs(getoutarg(method->type), 0);
  2525  
  2526  	t = nod(OTFUNC, N, N);
  2527  	l = list1(this);
  2528  	if(iface && rcvr->width < types[tptr]->width) {
  2529  		// Building method for interface table and receiver
  2530  		// is smaller than the single pointer-sized word
  2531  		// that the interface call will pass in.
  2532  		// Add a dummy padding argument after the
  2533  		// receiver to make up the difference.
  2534  		tpad = typ(TARRAY);
  2535  		tpad->type = types[TUINT8];
  2536  		tpad->bound = types[tptr]->width - rcvr->width;
  2537  		pad = nod(ODCLFIELD, newname(lookup(".pad")), typenod(tpad));
  2538  		l = list(l, pad);
  2539  	}
  2540  	t->list = concat(l, in);
  2541  	t->rlist = out;
  2542  
  2543  	fn = nod(ODCLFUNC, N, N);
  2544  	fn->nname = newname(newnam);
  2545  	fn->nname->defn = fn;
  2546  	fn->nname->ntype = t;
  2547  	declare(fn->nname, PFUNC);
  2548  	funchdr(fn);
  2549  
  2550  	// arg list
  2551  	args = nil;
  2552  	isddd = 0;
  2553  	for(l=in; l; l=l->next) {
  2554  		args = list(args, l->n->left);
  2555  		isddd = l->n->left->isddd;
  2556  	}
  2557  	
  2558  	methodrcvr = getthisx(method->type)->type->type;
  2559  
  2560  	// generate nil pointer check for better error
  2561  	if(isptr[rcvr->etype] && rcvr->type == methodrcvr) {
  2562  		// generating wrapper from *T to T.
  2563  		n = nod(OIF, N, N);
  2564  		n->ntest = nod(OEQ, this->left, nodnil());
  2565  		// these strings are already in the reflect tables,
  2566  		// so no space cost to use them here.
  2567  		l = nil;
  2568  		v.ctype = CTSTR;
  2569  		v.u.sval = strlit(rcvr->type->sym->pkg->name);  // package name
  2570  		l = list(l, nodlit(v));
  2571  		v.u.sval = strlit(rcvr->type->sym->name);  // type name
  2572  		l = list(l, nodlit(v));
  2573  		v.u.sval = strlit(method->sym->name);
  2574  		l = list(l, nodlit(v));  // method name
  2575  		call = nod(OCALL, syslook("panicwrap", 0), N);
  2576  		call->list = l;
  2577  		n->nbody = list1(call);
  2578  		fn->nbody = list(fn->nbody, n);
  2579  	}
  2580  	
  2581  	dot = adddot(nod(OXDOT, this->left, newname(method->sym)));
  2582  	
  2583  	// generate call
  2584  	if(!flag_race && isptr[rcvr->etype] && isptr[methodrcvr->etype] && method->embedded && !isifacemethod(method->type)) {
  2585  		// generate tail call: adjust pointer receiver and jump to embedded method.
  2586  		dot = dot->left;	// skip final .M
  2587  		if(!isptr[dotlist[0].field->type->etype])
  2588  			dot = nod(OADDR, dot, N);
  2589  		as = nod(OAS, this->left, nod(OCONVNOP, dot, N));
  2590  		as->right->type = rcvr;
  2591  		fn->nbody = list(fn->nbody, as);
  2592  		n = nod(ORETJMP, N, N);
  2593  		n->left = newname(methodsym(method->sym, methodrcvr, 0));
  2594  		fn->nbody = list(fn->nbody, n);
  2595  	} else {
  2596  		fn->wrapper = 1; // ignore frame for panic+recover matching
  2597  		call = nod(OCALL, dot, N);
  2598  		call->list = args;
  2599  		call->isddd = isddd;
  2600  		if(method->type->outtuple > 0) {
  2601  			n = nod(ORETURN, N, N);
  2602  			n->list = list1(call);
  2603  			call = n;
  2604  		}
  2605  		fn->nbody = list(fn->nbody, call);
  2606  	}
  2607  
  2608  	if(0 && debug['r'])
  2609  		dumplist("genwrapper body", fn->nbody);
  2610  
  2611  	funcbody(fn);
  2612  	curfn = fn;
  2613  	// wrappers where T is anonymous (struct or interface) can be duplicated.
  2614  	if(rcvr->etype == TSTRUCT ||
  2615  		rcvr->etype == TINTER ||
  2616  		isptr[rcvr->etype] && rcvr->type->etype == TSTRUCT)
  2617  		fn->dupok = 1;
  2618  	typecheck(&fn, Etop);
  2619  	typechecklist(fn->nbody, Etop);
  2620  	inlcalls(fn);
  2621  	curfn = nil;
  2622  	funccompile(fn, 0);
  2623  }
  2624  
  2625  static Node*
  2626  hashmem(Type *t)
  2627  {
  2628  	Node *tfn, *n;
  2629  	Sym *sym;
  2630  
  2631  	sym = pkglookup("memhash", runtimepkg);
  2632  
  2633  	n = newname(sym);
  2634  	n->class = PFUNC;
  2635  	tfn = nod(OTFUNC, N, N);
  2636  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t))));
  2637  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2638  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2639  	tfn->rlist = list(tfn->rlist, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2640  	typecheck(&tfn, Etype);
  2641  	n->type = tfn->type;
  2642  	return n;
  2643  }
  2644  
  2645  static Node*
  2646  hashfor(Type *t)
  2647  {
  2648  	int a;
  2649  	Sym *sym;
  2650  	Node *tfn, *n;
  2651  
  2652  	a = algtype1(t, nil);
  2653  	switch(a) {
  2654  	case AMEM:
  2655  		fatal("hashfor with AMEM type");
  2656  	case AINTER:
  2657  		sym = pkglookup("interhash", runtimepkg);
  2658  		break;
  2659  	case ANILINTER:
  2660  		sym = pkglookup("nilinterhash", runtimepkg);
  2661  		break;
  2662  	case ASTRING:
  2663  		sym = pkglookup("strhash", runtimepkg);
  2664  		break;
  2665  	case AFLOAT32:
  2666  		sym = pkglookup("f32hash", runtimepkg);
  2667  		break;
  2668  	case AFLOAT64:
  2669  		sym = pkglookup("f64hash", runtimepkg);
  2670  		break;
  2671  	case ACPLX64:
  2672  		sym = pkglookup("c64hash", runtimepkg);
  2673  		break;
  2674  	case ACPLX128:
  2675  		sym = pkglookup("c128hash", runtimepkg);
  2676  		break;
  2677  	default:
  2678  		sym = typesymprefix(".hash", t);
  2679  		break;
  2680  	}
  2681  
  2682  	n = newname(sym);
  2683  	n->class = PFUNC;
  2684  	tfn = nod(OTFUNC, N, N);
  2685  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t))));
  2686  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2687  	tfn->rlist = list(tfn->rlist, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2688  	typecheck(&tfn, Etype);
  2689  	n->type = tfn->type;
  2690  	return n;
  2691  }
  2692  
  2693  /*
  2694   * Generate a helper function to compute the hash of a value of type t.
  2695   */
  2696  void
  2697  genhash(Sym *sym, Type *t)
  2698  {
  2699  	Node *n, *fn, *np, *nh, *ni, *call, *nx, *na, *tfn, *r;
  2700  	Node *hashel;
  2701  	Type *first, *t1;
  2702  	int old_safemode;
  2703  	int64 size, mul, offend;
  2704  
  2705  	if(debug['r'])
  2706  		print("genhash %S %T\n", sym, t);
  2707  
  2708  	lineno = 1;  // less confusing than end of input
  2709  	dclcontext = PEXTERN;
  2710  	markdcl();
  2711  
  2712  	// func sym(p *T, h uintptr) uintptr
  2713  	fn = nod(ODCLFUNC, N, N);
  2714  	fn->nname = newname(sym);
  2715  	fn->nname->class = PFUNC;
  2716  	tfn = nod(OTFUNC, N, N);
  2717  	fn->nname->ntype = tfn;
  2718  
  2719  	n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t)));
  2720  	tfn->list = list(tfn->list, n);
  2721  	np = n->left;
  2722  	n = nod(ODCLFIELD, newname(lookup("h")), typenod(types[TUINTPTR]));
  2723  	tfn->list = list(tfn->list, n);
  2724  	nh = n->left;
  2725  	n = nod(ODCLFIELD, N, typenod(types[TUINTPTR])); // return value
  2726  	tfn->rlist = list(tfn->rlist, n);
  2727  
  2728  	funchdr(fn);
  2729  	typecheck(&fn->nname->ntype, Etype);
  2730  
  2731  	// genhash is only called for types that have equality but
  2732  	// cannot be handled by the standard algorithms,
  2733  	// so t must be either an array or a struct.
  2734  	switch(t->etype) {
  2735  	default:
  2736  		fatal("genhash %T", t);
  2737  	case TARRAY:
  2738  		if(isslice(t))
  2739  			fatal("genhash %T", t);
  2740  		// An array of pure memory would be handled by the
  2741  		// standard algorithm, so the element type must not be
  2742  		// pure memory.
  2743  		hashel = hashfor(t->type);
  2744  		n = nod(ORANGE, N, nod(OIND, np, N));
  2745  		ni = newname(lookup("i"));
  2746  		ni->type = types[TINT];
  2747  		n->list = list1(ni);
  2748  		n->colas = 1;
  2749  		colasdefn(n->list, n);
  2750  		ni = n->list->n;
  2751  
  2752  		// TODO: with aeshash we don't need these shift/mul parts
  2753  
  2754  		// h = h<<3 | h>>61
  2755  		n->nbody = list(n->nbody,
  2756  			nod(OAS,
  2757  			    nh,
  2758  				nod(OOR,
  2759  					nod(OLSH, nh, nodintconst(3)),
  2760  					nod(ORSH, nh, nodintconst(widthptr*8-3)))));
  2761  
  2762  		// h *= mul
  2763  		// Same multipliers as in runtime.memhash.
  2764  		if(widthptr == 4)
  2765  			mul = 3267000013LL;
  2766  		else
  2767  			mul = 23344194077549503LL;
  2768  		n->nbody = list(n->nbody,
  2769  			nod(OAS,
  2770  				nh,
  2771  				nod(OMUL, nh, nodintconst(mul))));
  2772  
  2773  		// h = hashel(&p[i], h)
  2774  		call = nod(OCALL, hashel, N);
  2775  		nx = nod(OINDEX, np, ni);
  2776  		nx->bounded = 1;
  2777  		na = nod(OADDR, nx, N);
  2778  		na->etype = 1;  // no escape to heap
  2779  		call->list = list(call->list, na);
  2780  		call->list = list(call->list, nh);
  2781  		n->nbody = list(n->nbody, nod(OAS, nh, call));
  2782  
  2783  		fn->nbody = list(fn->nbody, n);
  2784  		break;
  2785  
  2786  	case TSTRUCT:
  2787  		// Walk the struct using memhash for runs of AMEM
  2788  		// and calling specific hash functions for the others.
  2789  		first = T;
  2790  		offend = 0;
  2791  		for(t1=t->type;; t1=t1->down) {
  2792  			if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) {
  2793  				offend = t1->width + t1->type->width;
  2794  				if(first == T)
  2795  					first = t1;
  2796  				// If it's a memory field but it's padded, stop here.
  2797  				if(ispaddedfield(t1, t->width))
  2798  					t1 = t1->down;
  2799  				else
  2800  					continue;
  2801  			}
  2802  			// Run memhash for fields up to this one.
  2803  			if(first != T) {
  2804  				size = offend - first->width; // first->width is offset
  2805  				hashel = hashmem(first->type);
  2806  				// h = hashel(&p.first, size, h)
  2807  				call = nod(OCALL, hashel, N);
  2808  				nx = nod(OXDOT, np, newname(first->sym));  // TODO: fields from other packages?
  2809  				na = nod(OADDR, nx, N);
  2810  				na->etype = 1;  // no escape to heap
  2811  				call->list = list(call->list, na);
  2812  				call->list = list(call->list, nh);
  2813  				call->list = list(call->list, nodintconst(size));
  2814  				fn->nbody = list(fn->nbody, nod(OAS, nh, call));
  2815  
  2816  				first = T;
  2817  			}
  2818  			if(t1 == T)
  2819  				break;
  2820  			if(isblanksym(t1->sym))
  2821  				continue;
  2822  
  2823  			// Run hash for this field.
  2824  			if(algtype1(t1->type, nil) == AMEM) {
  2825  				hashel = hashmem(t1->type);
  2826  				// h = memhash(&p.t1, h, size)
  2827  				call = nod(OCALL, hashel, N);
  2828  				nx = nod(OXDOT, np, newname(t1->sym));  // TODO: fields from other packages?
  2829  				na = nod(OADDR, nx, N);
  2830  				na->etype = 1;  // no escape to heap
  2831  				call->list = list(call->list, na);
  2832  				call->list = list(call->list, nh);
  2833  				call->list = list(call->list, nodintconst(t1->type->width));
  2834  				fn->nbody = list(fn->nbody, nod(OAS, nh, call));
  2835  			} else {
  2836  				hashel = hashfor(t1->type);
  2837  				// h = hashel(&p.t1, h)
  2838  				call = nod(OCALL, hashel, N);
  2839  				nx = nod(OXDOT, np, newname(t1->sym));  // TODO: fields from other packages?
  2840  				na = nod(OADDR, nx, N);
  2841  				na->etype = 1;  // no escape to heap
  2842  				call->list = list(call->list, na);
  2843  				call->list = list(call->list, nh);
  2844  				fn->nbody = list(fn->nbody, nod(OAS, nh, call));
  2845  			}
  2846  		}
  2847  		break;
  2848  	}
  2849  	r = nod(ORETURN, N, N);
  2850  	r->list = list(r->list, nh);
  2851  	fn->nbody = list(fn->nbody, r);
  2852  
  2853  	if(debug['r'])
  2854  		dumplist("genhash body", fn->nbody);
  2855  
  2856  	funcbody(fn);
  2857  	curfn = fn;
  2858  	fn->dupok = 1;
  2859  	typecheck(&fn, Etop);
  2860  	typechecklist(fn->nbody, Etop);
  2861  	curfn = nil;
  2862  
  2863  	// Disable safemode while compiling this code: the code we
  2864  	// generate internally can refer to unsafe.Pointer.
  2865  	// In this case it can happen if we need to generate an ==
  2866  	// for a struct containing a reflect.Value, which itself has
  2867  	// an unexported field of type unsafe.Pointer.
  2868  	old_safemode = safemode;
  2869  	safemode = 0;
  2870  	funccompile(fn, 0);
  2871  	safemode = old_safemode;
  2872  }
  2873  
  2874  // Return node for
  2875  //	if p.field != q.field { return false }
  2876  static Node*
  2877  eqfield(Node *p, Node *q, Node *field)
  2878  {
  2879  	Node *nif, *nx, *ny, *r;
  2880  
  2881  	nx = nod(OXDOT, p, field);
  2882  	ny = nod(OXDOT, q, field);
  2883  	nif = nod(OIF, N, N);
  2884  	nif->ntest = nod(ONE, nx, ny);
  2885  	r = nod(ORETURN, N, N);
  2886  	r->list = list(r->list, nodbool(0));
  2887  	nif->nbody = list(nif->nbody, r);
  2888  	return nif;
  2889  }
  2890  
  2891  static Node*
  2892  eqmemfunc(vlong size, Type *type, int *needsize)
  2893  {
  2894  	char buf[30];
  2895  	Node *fn;
  2896  
  2897  	switch(size) {
  2898  	default:
  2899  		fn = syslook("memequal", 1);
  2900  		*needsize = 1;
  2901  		break;
  2902  	case 1:
  2903  	case 2:
  2904  	case 4:
  2905  	case 8:
  2906  	case 16:
  2907  		snprint(buf, sizeof buf, "memequal%d", (int)size*8);
  2908  		fn = syslook(buf, 1);
  2909  		*needsize = 0;
  2910  		break;
  2911  	}
  2912  	argtype(fn, type);
  2913  	argtype(fn, type);
  2914  	return fn;
  2915  }
  2916  
  2917  // Return node for
  2918  //	if !memequal(&p.field, &q.field [, size]) { return false }
  2919  static Node*
  2920  eqmem(Node *p, Node *q, Node *field, vlong size)
  2921  {
  2922  	Node *nif, *nx, *ny, *call, *r;
  2923  	int needsize;
  2924  
  2925  	nx = nod(OADDR, nod(OXDOT, p, field), N);
  2926  	nx->etype = 1;  // does not escape
  2927  	ny = nod(OADDR, nod(OXDOT, q, field), N);
  2928  	ny->etype = 1;  // does not escape
  2929  	typecheck(&nx, Erv);
  2930  	typecheck(&ny, Erv);
  2931  
  2932  	call = nod(OCALL, eqmemfunc(size, nx->type->type, &needsize), N);
  2933  	call->list = list(call->list, nx);
  2934  	call->list = list(call->list, ny);
  2935  	if(needsize)
  2936  		call->list = list(call->list, nodintconst(size));
  2937  
  2938  	nif = nod(OIF, N, N);
  2939  	nif->ninit = list(nif->ninit, call);
  2940  	nif->ntest = nod(ONOT, call, N);
  2941  	r = nod(ORETURN, N, N);
  2942  	r->list = list(r->list, nodbool(0));
  2943  	nif->nbody = list(nif->nbody, r);
  2944  	return nif;
  2945  }
  2946  
  2947  /*
  2948   * Generate a helper function to check equality of two values of type t.
  2949   */
  2950  void
  2951  geneq(Sym *sym, Type *t)
  2952  {
  2953  	Node *n, *fn, *np, *nq, *tfn, *nif, *ni, *nx, *ny, *nrange, *r;
  2954  	Type *t1, *first;
  2955  	int old_safemode;
  2956  	int64 size;
  2957  	int64 offend;
  2958  
  2959  	if(debug['r'])
  2960  		print("geneq %S %T\n", sym, t);
  2961  
  2962  	lineno = 1;  // less confusing than end of input
  2963  	dclcontext = PEXTERN;
  2964  	markdcl();
  2965  
  2966  	// func sym(p, q *T) bool
  2967  	fn = nod(ODCLFUNC, N, N);
  2968  	fn->nname = newname(sym);
  2969  	fn->nname->class = PFUNC;
  2970  	tfn = nod(OTFUNC, N, N);
  2971  	fn->nname->ntype = tfn;
  2972  
  2973  	n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t)));
  2974  	tfn->list = list(tfn->list, n);
  2975  	np = n->left;
  2976  	n = nod(ODCLFIELD, newname(lookup("q")), typenod(ptrto(t)));
  2977  	tfn->list = list(tfn->list, n);
  2978  	nq = n->left;
  2979  	n = nod(ODCLFIELD, N, typenod(types[TBOOL]));
  2980  	tfn->rlist = list(tfn->rlist, n);
  2981  
  2982  	funchdr(fn);
  2983  
  2984  	// geneq is only called for types that have equality but
  2985  	// cannot be handled by the standard algorithms,
  2986  	// so t must be either an array or a struct.
  2987  	switch(t->etype) {
  2988  	default:
  2989  		fatal("geneq %T", t);
  2990  	case TARRAY:
  2991  		if(isslice(t))
  2992  			fatal("geneq %T", t);
  2993  		// An array of pure memory would be handled by the
  2994  		// standard memequal, so the element type must not be
  2995  		// pure memory.  Even if we unrolled the range loop,
  2996  		// each iteration would be a function call, so don't bother
  2997  		// unrolling.
  2998  		nrange = nod(ORANGE, N, nod(OIND, np, N));
  2999  		ni = newname(lookup("i"));
  3000  		ni->type = types[TINT];
  3001  		nrange->list = list1(ni);
  3002  		nrange->colas = 1;
  3003  		colasdefn(nrange->list, nrange);
  3004  		ni = nrange->list->n;
  3005  		
  3006  		// if p[i] != q[i] { return false }
  3007  		nx = nod(OINDEX, np, ni);
  3008  		nx->bounded = 1;
  3009  		ny = nod(OINDEX, nq, ni);
  3010  		ny->bounded = 1;
  3011  
  3012  		nif = nod(OIF, N, N);
  3013  		nif->ntest = nod(ONE, nx, ny);
  3014  		r = nod(ORETURN, N, N);
  3015  		r->list = list(r->list, nodbool(0));
  3016  		nif->nbody = list(nif->nbody, r);
  3017  		nrange->nbody = list(nrange->nbody, nif);
  3018  		fn->nbody = list(fn->nbody, nrange);
  3019  		break;
  3020  
  3021  	case TSTRUCT:
  3022  		// Walk the struct using memequal for runs of AMEM
  3023  		// and calling specific equality tests for the others.
  3024  		// Skip blank-named fields.
  3025  		first = T;
  3026  		offend = 0;
  3027  		for(t1=t->type;; t1=t1->down) {
  3028  			if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) {
  3029  				offend = t1->width + t1->type->width;
  3030  				if(first == T)
  3031  					first = t1;
  3032  				// If it's a memory field but it's padded, stop here.
  3033  				if(ispaddedfield(t1, t->width))
  3034  					t1 = t1->down;
  3035  				else
  3036  					continue;
  3037  			}
  3038  			// Run memequal for fields up to this one.
  3039  			// TODO(rsc): All the calls to newname are wrong for
  3040  			// cross-package unexported fields.
  3041  			if(first != T) {
  3042  				if(first->down == t1) {
  3043  					fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym)));
  3044  				} else if(first->down->down == t1) {
  3045  					fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym)));
  3046  					first = first->down;
  3047  					if(!isblanksym(first->sym))
  3048  						fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym)));
  3049  				} else {
  3050  					// More than two fields: use memequal.
  3051  					size = offend - first->width; // first->width is offset
  3052  					fn->nbody = list(fn->nbody, eqmem(np, nq, newname(first->sym), size));
  3053  				}
  3054  				first = T;
  3055  			}
  3056  			if(t1 == T)
  3057  				break;
  3058  			if(isblanksym(t1->sym))
  3059  				continue;
  3060  
  3061  			// Check this field, which is not just memory.
  3062  			fn->nbody = list(fn->nbody, eqfield(np, nq, newname(t1->sym)));
  3063  		}
  3064  
  3065  		break;
  3066  	}
  3067  
  3068  	// return true
  3069  	r = nod(ORETURN, N, N);
  3070  	r->list = list(r->list, nodbool(1));
  3071  	fn->nbody = list(fn->nbody, r);
  3072  
  3073  	if(debug['r'])
  3074  		dumplist("geneq body", fn->nbody);
  3075  
  3076  	funcbody(fn);
  3077  	curfn = fn;
  3078  	fn->dupok = 1;
  3079  	typecheck(&fn, Etop);
  3080  	typechecklist(fn->nbody, Etop);
  3081  	curfn = nil;
  3082  	
  3083  	// Disable safemode while compiling this code: the code we
  3084  	// generate internally can refer to unsafe.Pointer.
  3085  	// In this case it can happen if we need to generate an ==
  3086  	// for a struct containing a reflect.Value, which itself has
  3087  	// an unexported field of type unsafe.Pointer.
  3088  	old_safemode = safemode;
  3089  	safemode = 0;
  3090  	funccompile(fn, 0);
  3091  	safemode = old_safemode;
  3092  }
  3093  
  3094  static Type*
  3095  ifacelookdot(Sym *s, Type *t, int *followptr, int ignorecase)
  3096  {
  3097  	int i, c, d;
  3098  	Type *m;
  3099  
  3100  	*followptr = 0;
  3101  
  3102  	if(t == T)
  3103  		return T;
  3104  
  3105  	for(d=0; d<nelem(dotlist); d++) {
  3106  		c = adddot1(s, t, d, &m, ignorecase);
  3107  		if(c > 1) {
  3108  			yyerror("%T.%S is ambiguous", t, s);
  3109  			return T;
  3110  		}
  3111  		if(c == 1) {
  3112  			for(i=0; i<d; i++) {
  3113  				if(isptr[dotlist[i].field->type->etype]) {
  3114  					*followptr = 1;
  3115  					break;
  3116  				}
  3117  			}
  3118  			if(m->type->etype != TFUNC || m->type->thistuple == 0) {
  3119  				yyerror("%T.%S is a field, not a method", t, s);
  3120  				return T;
  3121  			}
  3122  			return m;
  3123  		}
  3124  	}
  3125  	return T;
  3126  }
  3127  
  3128  int
  3129  implements(Type *t, Type *iface, Type **m, Type **samename, int *ptr)
  3130  {
  3131  	Type *t0, *im, *tm, *rcvr, *imtype;
  3132  	int followptr;
  3133  
  3134  	t0 = t;
  3135  	if(t == T)
  3136  		return 0;
  3137  
  3138  	// if this is too slow,
  3139  	// could sort these first
  3140  	// and then do one loop.
  3141  
  3142  	if(t->etype == TINTER) {
  3143  		for(im=iface->type; im; im=im->down) {
  3144  			for(tm=t->type; tm; tm=tm->down) {
  3145  				if(tm->sym == im->sym) {
  3146  					if(eqtype(tm->type, im->type))
  3147  						goto found;
  3148  					*m = im;
  3149  					*samename = tm;
  3150  					*ptr = 0;
  3151  					return 0;
  3152  				}
  3153  			}
  3154  			*m = im;
  3155  			*samename = nil;
  3156  			*ptr = 0;
  3157  			return 0;
  3158  		found:;
  3159  		}
  3160  		return 1;
  3161  	}
  3162  
  3163  	t = methtype(t, 0);
  3164  	if(t != T)
  3165  		expandmeth(t);
  3166  	for(im=iface->type; im; im=im->down) {
  3167  		imtype = methodfunc(im->type, 0);
  3168  		tm = ifacelookdot(im->sym, t, &followptr, 0);
  3169  		if(tm == T || tm->nointerface || !eqtype(methodfunc(tm->type, 0), imtype)) {
  3170  			if(tm == T)
  3171  				tm = ifacelookdot(im->sym, t, &followptr, 1);
  3172  			*m = im;
  3173  			*samename = tm;
  3174  			*ptr = 0;
  3175  			return 0;
  3176  		}
  3177  		// if pointer receiver in method,
  3178  		// the method does not exist for value types.
  3179  		rcvr = getthisx(tm->type)->type->type;
  3180  		if(isptr[rcvr->etype] && !isptr[t0->etype] && !followptr && !isifacemethod(tm->type)) {
  3181  			if(0 && debug['r'])
  3182  				yyerror("interface pointer mismatch");
  3183  
  3184  			*m = im;
  3185  			*samename = nil;
  3186  			*ptr = 1;
  3187  			return 0;
  3188  		}
  3189  	}
  3190  	return 1;
  3191  }
  3192  
  3193  /*
  3194   * even simpler simtype; get rid of ptr, bool.
  3195   * assuming that the front end has rejected
  3196   * all the invalid conversions (like ptr -> bool)
  3197   */
  3198  int
  3199  simsimtype(Type *t)
  3200  {
  3201  	int et;
  3202  
  3203  	if(t == 0)
  3204  		return 0;
  3205  
  3206  	et = simtype[t->etype];
  3207  	switch(et) {
  3208  	case TPTR32:
  3209  		et = TUINT32;
  3210  		break;
  3211  	case TPTR64:
  3212  		et = TUINT64;
  3213  		break;
  3214  	case TBOOL:
  3215  		et = TUINT8;
  3216  		break;
  3217  	}
  3218  	return et;
  3219  }
  3220  
  3221  NodeList*
  3222  concat(NodeList *a, NodeList *b)
  3223  {
  3224  	if(a == nil)
  3225  		return b;
  3226  	if(b == nil)
  3227  		return a;
  3228  
  3229  	a->end->next = b;
  3230  	a->end = b->end;
  3231  	b->end = nil;
  3232  	return a;
  3233  }
  3234  
  3235  NodeList*
  3236  list1(Node *n)
  3237  {
  3238  	NodeList *l;
  3239  
  3240  	if(n == nil)
  3241  		return nil;
  3242  	if(n->op == OBLOCK && n->ninit == nil) {
  3243  		// Flatten list and steal storage.
  3244  		// Poison pointer to catch errant uses.
  3245  		l = n->list;
  3246  		n->list = (NodeList*)1;
  3247  		return l;
  3248  	}
  3249  	l = mal(sizeof *l);
  3250  	l->n = n;
  3251  	l->end = l;
  3252  	return l;
  3253  }
  3254  
  3255  NodeList*
  3256  list(NodeList *l, Node *n)
  3257  {
  3258  	return concat(l, list1(n));
  3259  }
  3260  
  3261  void
  3262  listsort(NodeList** l, int(*f)(Node*, Node*))
  3263  {
  3264  	NodeList *l1, *l2, *le;
  3265  
  3266  	if(*l == nil || (*l)->next == nil)
  3267  		return;
  3268  
  3269  	l1 = *l;
  3270  	l2 = *l;
  3271  	for(;;) {
  3272  		l2 = l2->next;
  3273  		if(l2 == nil)
  3274  			break;
  3275  		l2 = l2->next;
  3276  		if(l2 == nil)
  3277  			break;
  3278  		l1 = l1->next;
  3279  	}
  3280  
  3281  	l2 = l1->next;
  3282  	l1->next = nil;
  3283  	l2->end = (*l)->end;
  3284  	(*l)->end = l1;
  3285  
  3286  	l1 = *l;
  3287  	listsort(&l1, f);
  3288  	listsort(&l2, f);
  3289  
  3290  	if((*f)(l1->n, l2->n) < 0) {
  3291  		*l = l1;
  3292  	} else {
  3293  		*l = l2;
  3294  		l2 = l1;
  3295  		l1 = *l;
  3296  	}
  3297  
  3298  	// now l1 == *l; and l1 < l2
  3299  
  3300  	while ((l1 != nil) && (l2 != nil)) {
  3301  		while ((l1->next != nil) && (*f)(l1->next->n, l2->n) < 0)
  3302  			l1 = l1->next;
  3303  		
  3304  		// l1 is last one from l1 that is < l2
  3305  		le = l1->next;		// le is the rest of l1, first one that is >= l2
  3306  		if(le != nil)
  3307  			le->end = (*l)->end;
  3308  
  3309  		(*l)->end = l1;		// cut *l at l1
  3310  		*l = concat(*l, l2);	// glue l2 to *l's tail
  3311  
  3312  		l1 = l2;		// l1 is the first element of *l that is < the new l2
  3313  		l2 = le;		// ... because l2 now is the old tail of l1
  3314  	}
  3315  
  3316  	*l = concat(*l, l2);		// any remainder 
  3317  }
  3318  
  3319  NodeList*
  3320  listtreecopy(NodeList *l)
  3321  {
  3322  	NodeList *out;
  3323  
  3324  	out = nil;
  3325  	for(; l; l=l->next)
  3326  		out = list(out, treecopy(l->n));
  3327  	return out;
  3328  }
  3329  
  3330  Node*
  3331  liststmt(NodeList *l)
  3332  {
  3333  	Node *n;
  3334  
  3335  	n = nod(OBLOCK, N, N);
  3336  	n->list = l;
  3337  	if(l)
  3338  		n->lineno = l->n->lineno;
  3339  	return n;
  3340  }
  3341  
  3342  /*
  3343   * return nelem of list
  3344   */
  3345  int
  3346  count(NodeList *l)
  3347  {
  3348  	vlong n;
  3349  
  3350  	n = 0;
  3351  	for(; l; l=l->next)
  3352  		n++;
  3353  	if((int)n != n) { // Overflow.
  3354  		yyerror("too many elements in list");
  3355  	}
  3356  	return n;
  3357  }
  3358  
  3359  /*
  3360   * return nelem of list
  3361   */
  3362  int
  3363  structcount(Type *t)
  3364  {
  3365  	int v;
  3366  	Iter s;
  3367  
  3368  	v = 0;
  3369  	for(t = structfirst(&s, &t); t != T; t = structnext(&s))
  3370  		v++;
  3371  	return v;
  3372  }
  3373  
  3374  /*
  3375   * return power of 2 of the constant
  3376   * operand. -1 if it is not a power of 2.
  3377   * 1000+ if it is a -(power of 2)
  3378   */
  3379  int
  3380  powtwo(Node *n)
  3381  {
  3382  	uvlong v, b;
  3383  	int i;
  3384  
  3385  	if(n == N || n->op != OLITERAL || n->type == T)
  3386  		goto no;
  3387  	if(!isint[n->type->etype])
  3388  		goto no;
  3389  
  3390  	v = mpgetfix(n->val.u.xval);
  3391  	b = 1ULL;
  3392  	for(i=0; i<64; i++) {
  3393  		if(b == v)
  3394  			return i;
  3395  		b = b<<1;
  3396  	}
  3397  
  3398  	if(!issigned[n->type->etype])
  3399  		goto no;
  3400  
  3401  	v = -v;
  3402  	b = 1ULL;
  3403  	for(i=0; i<64; i++) {
  3404  		if(b == v)
  3405  			return i+1000;
  3406  		b = b<<1;
  3407  	}
  3408  
  3409  no:
  3410  	return -1;
  3411  }
  3412  
  3413  /*
  3414   * return the unsigned type for
  3415   * a signed integer type.
  3416   * returns T if input is not a
  3417   * signed integer type.
  3418   */
  3419  Type*
  3420  tounsigned(Type *t)
  3421  {
  3422  
  3423  	// this is types[et+1], but not sure
  3424  	// that this relation is immutable
  3425  	switch(t->etype) {
  3426  	default:
  3427  		print("tounsigned: unknown type %T\n", t);
  3428  		t = T;
  3429  		break;
  3430  	case TINT:
  3431  		t = types[TUINT];
  3432  		break;
  3433  	case TINT8:
  3434  		t = types[TUINT8];
  3435  		break;
  3436  	case TINT16:
  3437  		t = types[TUINT16];
  3438  		break;
  3439  	case TINT32:
  3440  		t = types[TUINT32];
  3441  		break;
  3442  	case TINT64:
  3443  		t = types[TUINT64];
  3444  		break;
  3445  	}
  3446  	return t;
  3447  }
  3448  
  3449  /*
  3450   * magic number for signed division
  3451   * see hacker's delight chapter 10
  3452   */
  3453  void
  3454  smagic(Magic *m)
  3455  {
  3456  	int p;
  3457  	uint64 ad, anc, delta, q1, r1, q2, r2, t;
  3458  	uint64 mask, two31;
  3459  
  3460  	m->bad = 0;
  3461  	switch(m->w) {
  3462  	default:
  3463  		m->bad = 1;
  3464  		return;
  3465  	case 8:
  3466  		mask = 0xffLL;
  3467  		break;
  3468  	case 16:
  3469  		mask = 0xffffLL;
  3470  		break;
  3471  	case 32:
  3472  		mask = 0xffffffffLL;
  3473  		break;
  3474  	case 64:
  3475  		mask = 0xffffffffffffffffULL;
  3476  		break;
  3477  	}
  3478  	two31 = mask ^ (mask>>1);
  3479  
  3480  	p = m->w-1;
  3481  	ad = m->sd;
  3482  	if(m->sd < 0)
  3483  		ad = -(uvlong)m->sd;
  3484  
  3485  	// bad denominators
  3486  	if(ad == 0 || ad == 1 || ad == two31) {
  3487  		m->bad = 1;
  3488  		return;
  3489  	}
  3490  
  3491  	t = two31;
  3492  	ad &= mask;
  3493  
  3494  	anc = t - 1 - t%ad;
  3495  	anc &= mask;
  3496  
  3497  	q1 = two31/anc;
  3498  	r1 = two31 - q1*anc;
  3499  	q1 &= mask;
  3500  	r1 &= mask;
  3501  
  3502  	q2 = two31/ad;
  3503  	r2 = two31 - q2*ad;
  3504  	q2 &= mask;
  3505  	r2 &= mask;
  3506  
  3507  	for(;;) {
  3508  		p++;
  3509  		q1 <<= 1;
  3510  		r1 <<= 1;
  3511  		q1 &= mask;
  3512  		r1 &= mask;
  3513  		if(r1 >= anc) {
  3514  			q1++;
  3515  			r1 -= anc;
  3516  			q1 &= mask;
  3517  			r1 &= mask;
  3518  		}
  3519  
  3520  		q2 <<= 1;
  3521  		r2 <<= 1;
  3522  		q2 &= mask;
  3523  		r2 &= mask;
  3524  		if(r2 >= ad) {
  3525  			q2++;
  3526  			r2 -= ad;
  3527  			q2 &= mask;
  3528  			r2 &= mask;
  3529  		}
  3530  
  3531  		delta = ad - r2;
  3532  		delta &= mask;
  3533  		if(q1 < delta || (q1 == delta && r1 == 0)) {
  3534  			continue;
  3535  		}
  3536  		break;
  3537  	}
  3538  
  3539  	m->sm = q2+1;
  3540  	if(m->sm & two31)
  3541  		m->sm |= ~mask;
  3542  	m->s = p-m->w;
  3543  }
  3544  
  3545  /*
  3546   * magic number for unsigned division
  3547   * see hacker's delight chapter 10
  3548   */
  3549  void
  3550  umagic(Magic *m)
  3551  {
  3552  	int p;
  3553  	uint64 nc, delta, q1, r1, q2, r2;
  3554  	uint64 mask, two31;
  3555  
  3556  	m->bad = 0;
  3557  	m->ua = 0;
  3558  
  3559  	switch(m->w) {
  3560  	default:
  3561  		m->bad = 1;
  3562  		return;
  3563  	case 8:
  3564  		mask = 0xffLL;
  3565  		break;
  3566  	case 16:
  3567  		mask = 0xffffLL;
  3568  		break;
  3569  	case 32:
  3570  		mask = 0xffffffffLL;
  3571  		break;
  3572  	case 64:
  3573  		mask = 0xffffffffffffffffULL;
  3574  		break;
  3575  	}
  3576  	two31 = mask ^ (mask>>1);
  3577  
  3578  	m->ud &= mask;
  3579  	if(m->ud == 0 || m->ud == two31) {
  3580  		m->bad = 1;
  3581  		return;
  3582  	}
  3583  	nc = mask - (-m->ud&mask)%m->ud;
  3584  	p = m->w-1;
  3585  
  3586  	q1 = two31/nc;
  3587  	r1 = two31 - q1*nc;
  3588  	q1 &= mask;
  3589  	r1 &= mask;
  3590  
  3591  	q2 = (two31-1) / m->ud;
  3592  	r2 = (two31-1) - q2*m->ud;
  3593  	q2 &= mask;
  3594  	r2 &= mask;
  3595  
  3596  	for(;;) {
  3597  		p++;
  3598  		if(r1 >= nc-r1) {
  3599  			q1 <<= 1;
  3600  			q1++;
  3601  			r1 <<= 1;
  3602  			r1 -= nc;
  3603  		} else {
  3604  			q1 <<= 1;
  3605  			r1 <<= 1;
  3606  		}
  3607  		q1 &= mask;
  3608  		r1 &= mask;
  3609  		if(r2+1 >= m->ud-r2) {
  3610  			if(q2 >= two31-1) {
  3611  				m->ua = 1;
  3612  			}
  3613  			q2 <<= 1;
  3614  			q2++;
  3615  			r2 <<= 1;
  3616  			r2++;
  3617  			r2 -= m->ud;
  3618  		} else {
  3619  			if(q2 >= two31) {
  3620  				m->ua = 1;
  3621  			}
  3622  			q2 <<= 1;
  3623  			r2 <<= 1;
  3624  			r2++;
  3625  		}
  3626  		q2 &= mask;
  3627  		r2 &= mask;
  3628  
  3629  		delta = m->ud - 1 - r2;
  3630  		delta &= mask;
  3631  
  3632  		if(p < m->w+m->w)
  3633  		if(q1 < delta || (q1 == delta && r1 == 0)) {
  3634  			continue;
  3635  		}
  3636  		break;
  3637  	}
  3638  	m->um = q2+1;
  3639  	m->s = p-m->w;
  3640  }
  3641  
  3642  Sym*
  3643  ngotype(Node *n)
  3644  {
  3645  	if(n->type != T)
  3646  		return typenamesym(n->type);
  3647  	return S;
  3648  }
  3649  
  3650  /*
  3651   * Convert raw string to the prefix that will be used in the symbol
  3652   * table.  All control characters, space, '%' and '"', as well as
  3653   * non-7-bit clean bytes turn into %xx.  The period needs escaping
  3654   * only in the last segment of the path, and it makes for happier
  3655   * users if we escape that as little as possible.
  3656   *
  3657   * If you edit this, edit ../ld/lib.c:/^pathtoprefix too.
  3658   * If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too.
  3659   */
  3660  static char*
  3661  pathtoprefix(char *s)
  3662  {
  3663  	static char hex[] = "0123456789abcdef";
  3664  	char *p, *r, *w, *l;
  3665  	int n;
  3666  
  3667  	// find first character past the last slash, if any.
  3668  	l = s;
  3669  	for(r=s; *r; r++)
  3670  		if(*r == '/')
  3671  			l = r+1;
  3672  
  3673  	// check for chars that need escaping
  3674  	n = 0;
  3675  	for(r=s; *r; r++)
  3676  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f)
  3677  			n++;
  3678  
  3679  	// quick exit
  3680  	if(n == 0)
  3681  		return s;
  3682  
  3683  	// escape
  3684  	p = mal((r-s)+1+2*n);
  3685  	for(r=s, w=p; *r; r++) {
  3686  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) {
  3687  			*w++ = '%';
  3688  			*w++ = hex[(*r>>4)&0xF];
  3689  			*w++ = hex[*r&0xF];
  3690  		} else
  3691  			*w++ = *r;
  3692  	}
  3693  	*w = '\0';
  3694  	return p;
  3695  }
  3696  
  3697  Pkg*
  3698  mkpkg(Strlit *path)
  3699  {
  3700  	Pkg *p;
  3701  	int h;
  3702  
  3703  	h = stringhash(path->s) & (nelem(phash)-1);
  3704  	for(p=phash[h]; p; p=p->link)
  3705  		if(p->path->len == path->len && memcmp(path->s, p->path->s, path->len) == 0)
  3706  			return p;
  3707  
  3708  	p = mal(sizeof *p);
  3709  	p->path = path;
  3710  	p->prefix = pathtoprefix(path->s);
  3711  	p->link = phash[h];
  3712  	phash[h] = p;
  3713  	return p;
  3714  }
  3715  
  3716  Strlit*
  3717  strlit(char *s)
  3718  {
  3719  	Strlit *t;
  3720  	
  3721  	t = mal(sizeof *t + strlen(s));
  3722  	strcpy(t->s, s);
  3723  	t->len = strlen(s);
  3724  	return t;
  3725  }
  3726  
  3727  void
  3728  addinit(Node **np, NodeList *init)
  3729  {
  3730  	Node *n;
  3731  	
  3732  	if(init == nil)
  3733  		return;
  3734  
  3735  	n = *np;
  3736  	switch(n->op) {
  3737  	case ONAME:
  3738  	case OLITERAL:
  3739  		// There may be multiple refs to this node;
  3740  		// introduce OCONVNOP to hold init list.
  3741  		n = nod(OCONVNOP, n, N);
  3742  		n->type = n->left->type;
  3743  		n->typecheck = 1;
  3744  		*np = n;
  3745  		break;
  3746  	}
  3747  	n->ninit = concat(init, n->ninit);
  3748  	n->ullman = UINF;
  3749  }
  3750  
  3751  static char* reservedimports[] = {
  3752  	"go",
  3753  	"type",
  3754  };
  3755  
  3756  int
  3757  isbadimport(Strlit *path)
  3758  {
  3759  	int i;
  3760  	char *s;
  3761  	Rune r;
  3762  
  3763  	if(strlen(path->s) != path->len) {
  3764  		yyerror("import path contains NUL");
  3765  		return 1;
  3766  	}
  3767  	
  3768  	for(i=0; i<nelem(reservedimports); i++) {
  3769  		if(strcmp(path->s, reservedimports[i]) == 0) {
  3770  			yyerror("import path \"%s\" is reserved and cannot be used", path->s);
  3771  			return 1;
  3772  		}
  3773  	}
  3774  
  3775  	s = path->s;
  3776  	while(*s) {
  3777  		s += chartorune(&r, s);
  3778  		if(r == Runeerror) {
  3779  			yyerror("import path contains invalid UTF-8 sequence: \"%Z\"", path);
  3780  			return 1;
  3781  		}
  3782  		if(r < 0x20 || r == 0x7f) {
  3783  			yyerror("import path contains control character: \"%Z\"", path);
  3784  			return 1;
  3785  		}
  3786  		if(r == '\\') {
  3787  			yyerror("import path contains backslash; use slash: \"%Z\"", path);
  3788  			return 1;
  3789  		}
  3790  		if(isspacerune(r)) {
  3791  			yyerror("import path contains space character: \"%Z\"", path);
  3792  			return 1;
  3793  		}
  3794  		if(utfrune("!\"#$%&'()*,:;<=>?[]^`{|}", r)) {
  3795  			yyerror("import path contains invalid character '%C': \"%Z\"", r, path);
  3796  			return 1;
  3797  		}
  3798  	}
  3799  	return 0;
  3800  }
  3801  
  3802  void
  3803  checknil(Node *x, NodeList **init)
  3804  {
  3805  	Node *n;
  3806  	
  3807  	if(isinter(x->type)) {
  3808  		x = nod(OITAB, x, N);
  3809  		typecheck(&x, Erv);
  3810  	}
  3811  	n = nod(OCHECKNIL, x, N);
  3812  	n->typecheck = 1;
  3813  	*init = list(*init, n);
  3814  }
  3815  
  3816  /*
  3817   * Can this type be stored directly in an interface word?
  3818   * Yes, if the representation is a single pointer.
  3819   */
  3820  int
  3821  isdirectiface(Type *t)
  3822  {
  3823  	switch(t->etype) {
  3824  	case TPTR32:
  3825  	case TPTR64:
  3826  	case TCHAN:
  3827  	case TMAP:
  3828  	case TFUNC:
  3829  	case TUNSAFEPTR:
  3830  		return 1;
  3831  	case TARRAY:
  3832  		// Array of 1 direct iface type can be direct.
  3833  		return t->bound == 1 && isdirectiface(t->type);
  3834  	case TSTRUCT:
  3835  		// Struct with 1 field of direct iface type can be direct.
  3836  		return t->type != T && t->type->down == T && isdirectiface(t->type->type);
  3837  	}
  3838  	return 0;
  3839  }