github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/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  		return hashmem(t);
  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->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2688  	tfn->rlist = list(tfn->rlist, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2689  	typecheck(&tfn, Etype);
  2690  	n->type = tfn->type;
  2691  	return n;
  2692  }
  2693  
  2694  /*
  2695   * Generate a helper function to compute the hash of a value of type t.
  2696   */
  2697  void
  2698  genhash(Sym *sym, Type *t)
  2699  {
  2700  	Node *n, *fn, *np, *nh, *ni, *call, *nx, *na, *tfn, *r;
  2701  	Node *hashel;
  2702  	Type *first, *t1;
  2703  	int old_safemode;
  2704  	int64 size, mul, offend;
  2705  
  2706  	if(debug['r'])
  2707  		print("genhash %S %T\n", sym, t);
  2708  
  2709  	lineno = 1;  // less confusing than end of input
  2710  	dclcontext = PEXTERN;
  2711  	markdcl();
  2712  
  2713  	// func sym(p *T, s uintptr, h uintptr) uintptr
  2714  	fn = nod(ODCLFUNC, N, N);
  2715  	fn->nname = newname(sym);
  2716  	fn->nname->class = PFUNC;
  2717  	tfn = nod(OTFUNC, N, N);
  2718  	fn->nname->ntype = tfn;
  2719  
  2720  	n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t)));
  2721  	tfn->list = list(tfn->list, n);
  2722  	np = n->left;
  2723  	n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR]));
  2724  	tfn->list = list(tfn->list, n);
  2725  	n = nod(ODCLFIELD, newname(lookup("h")), typenod(types[TUINTPTR]));
  2726  	tfn->list = list(tfn->list, n);
  2727  	nh = n->left;
  2728  	n = nod(ODCLFIELD, N, typenod(types[TUINTPTR])); // return value
  2729  	tfn->rlist = list(tfn->rlist, n);
  2730  
  2731  	funchdr(fn);
  2732  	typecheck(&fn->nname->ntype, Etype);
  2733  
  2734  	// genhash is only called for types that have equality but
  2735  	// cannot be handled by the standard algorithms,
  2736  	// so t must be either an array or a struct.
  2737  	switch(t->etype) {
  2738  	default:
  2739  		fatal("genhash %T", t);
  2740  	case TARRAY:
  2741  		if(isslice(t))
  2742  			fatal("genhash %T", t);
  2743  		// An array of pure memory would be handled by the
  2744  		// standard algorithm, so the element type must not be
  2745  		// pure memory.
  2746  		hashel = hashfor(t->type);
  2747  		n = nod(ORANGE, N, nod(OIND, np, N));
  2748  		ni = newname(lookup("i"));
  2749  		ni->type = types[TINT];
  2750  		n->list = list1(ni);
  2751  		n->colas = 1;
  2752  		colasdefn(n->list, n);
  2753  		ni = n->list->n;
  2754  
  2755  		// TODO: with aeshash we don't need these shift/mul parts
  2756  
  2757  		// h = h<<3 | h>>61
  2758  		n->nbody = list(n->nbody,
  2759  			nod(OAS,
  2760  			    nh,
  2761  				nod(OOR,
  2762  					nod(OLSH, nh, nodintconst(3)),
  2763  					nod(ORSH, nh, nodintconst(widthptr*8-3)))));
  2764  
  2765  		// h *= mul
  2766  		// Same multipliers as in runtime.memhash.
  2767  		if(widthptr == 4)
  2768  			mul = 3267000013LL;
  2769  		else
  2770  			mul = 23344194077549503LL;
  2771  		n->nbody = list(n->nbody,
  2772  			nod(OAS,
  2773  				nh,
  2774  				nod(OMUL, nh, nodintconst(mul))));
  2775  
  2776  		// h = hashel(&p[i], sizeof(p[i]), h)
  2777  		call = nod(OCALL, hashel, N);
  2778  		nx = nod(OINDEX, np, ni);
  2779  		nx->bounded = 1;
  2780  		na = nod(OADDR, nx, N);
  2781  		na->etype = 1;  // no escape to heap
  2782  		call->list = list(call->list, na);
  2783  		call->list = list(call->list, nodintconst(t->type->width));
  2784  		call->list = list(call->list, nh);
  2785  		n->nbody = list(n->nbody, nod(OAS, nh, call));
  2786  
  2787  		fn->nbody = list(fn->nbody, n);
  2788  		break;
  2789  
  2790  	case TSTRUCT:
  2791  		// Walk the struct using memhash for runs of AMEM
  2792  		// and calling specific hash functions for the others.
  2793  		first = T;
  2794  		offend = 0;
  2795  		for(t1=t->type;; t1=t1->down) {
  2796  			if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) {
  2797  				offend = t1->width + t1->type->width;
  2798  				if(first == T)
  2799  					first = t1;
  2800  				// If it's a memory field but it's padded, stop here.
  2801  				if(ispaddedfield(t1, t->width))
  2802  					t1 = t1->down;
  2803  				else
  2804  					continue;
  2805  			}
  2806  			// Run memhash for fields up to this one.
  2807  			if(first != T) {
  2808  				size = offend - first->width; // first->width is offset
  2809  				hashel = hashmem(first->type);
  2810  				// h = hashel(&p.first, size, h)
  2811  				call = nod(OCALL, hashel, N);
  2812  				nx = nod(OXDOT, np, newname(first->sym));  // TODO: fields from other packages?
  2813  				na = nod(OADDR, nx, N);
  2814  				na->etype = 1;  // no escape to heap
  2815  				call->list = list(call->list, na);
  2816  				call->list = list(call->list, nodintconst(size));
  2817  				call->list = list(call->list, nh);
  2818  				fn->nbody = list(fn->nbody, nod(OAS, nh, call));
  2819  
  2820  				first = T;
  2821  			}
  2822  			if(t1 == T)
  2823  				break;
  2824  			if(isblanksym(t1->sym))
  2825  				continue;
  2826  
  2827  			// Run hash for this field.
  2828  			hashel = hashfor(t1->type);
  2829  			// h = hashel(&p.t1, size, h)
  2830  			call = nod(OCALL, hashel, N);
  2831  			nx = nod(OXDOT, np, newname(t1->sym));  // TODO: fields from other packages?
  2832  			na = nod(OADDR, nx, N);
  2833  			na->etype = 1;  // no escape to heap
  2834  			call->list = list(call->list, na);
  2835  			call->list = list(call->list, nodintconst(t1->type->width));
  2836  			call->list = list(call->list, nh);
  2837  			fn->nbody = list(fn->nbody, nod(OAS, nh, call));
  2838  		}
  2839  		break;
  2840  	}
  2841  	r = nod(ORETURN, N, N);
  2842  	r->list = list(r->list, nh);
  2843  	fn->nbody = list(fn->nbody, r);
  2844  
  2845  	if(debug['r'])
  2846  		dumplist("genhash body", fn->nbody);
  2847  
  2848  	funcbody(fn);
  2849  	curfn = fn;
  2850  	fn->dupok = 1;
  2851  	typecheck(&fn, Etop);
  2852  	typechecklist(fn->nbody, Etop);
  2853  	curfn = nil;
  2854  
  2855  	// Disable safemode while compiling this code: the code we
  2856  	// generate internally can refer to unsafe.Pointer.
  2857  	// In this case it can happen if we need to generate an ==
  2858  	// for a struct containing a reflect.Value, which itself has
  2859  	// an unexported field of type unsafe.Pointer.
  2860  	old_safemode = safemode;
  2861  	safemode = 0;
  2862  	funccompile(fn, 0);
  2863  	safemode = old_safemode;
  2864  }
  2865  
  2866  // Return node for
  2867  //	if p.field != q.field { return false }
  2868  static Node*
  2869  eqfield(Node *p, Node *q, Node *field)
  2870  {
  2871  	Node *nif, *nx, *ny, *r;
  2872  
  2873  	nx = nod(OXDOT, p, field);
  2874  	ny = nod(OXDOT, q, field);
  2875  	nif = nod(OIF, N, N);
  2876  	nif->ntest = nod(ONE, nx, ny);
  2877  	r = nod(ORETURN, N, N);
  2878  	r->list = list(r->list, nodbool(0));
  2879  	nif->nbody = list(nif->nbody, r);
  2880  	return nif;
  2881  }
  2882  
  2883  static Node*
  2884  eqmemfunc(vlong size, Type *type)
  2885  {
  2886  	char buf[30];
  2887  	Node *fn;
  2888  
  2889  	switch(size) {
  2890  	default:
  2891  		fn = syslook("memequal", 1);
  2892  		break;
  2893  	case 1:
  2894  	case 2:
  2895  	case 4:
  2896  	case 8:
  2897  	case 16:
  2898  		snprint(buf, sizeof buf, "memequal%d", (int)size*8);
  2899  		fn = syslook(buf, 1);
  2900  		break;
  2901  	}
  2902  	argtype(fn, type);
  2903  	argtype(fn, type);
  2904  	return fn;
  2905  }
  2906  
  2907  // Return node for
  2908  //	if !memequal(&p.field, &q.field, size) { return false }
  2909  static Node*
  2910  eqmem(Node *p, Node *q, Node *field, vlong size)
  2911  {
  2912  	Node *nif, *nx, *ny, *call, *r;
  2913  
  2914  	nx = nod(OADDR, nod(OXDOT, p, field), N);
  2915  	nx->etype = 1;  // does not escape
  2916  	ny = nod(OADDR, nod(OXDOT, q, field), N);
  2917  	ny->etype = 1;  // does not escape
  2918  	typecheck(&nx, Erv);
  2919  	typecheck(&ny, Erv);
  2920  
  2921  	call = nod(OCALL, eqmemfunc(size, nx->type->type), N);
  2922  	call->list = list(call->list, nx);
  2923  	call->list = list(call->list, ny);
  2924  	call->list = list(call->list, nodintconst(size));
  2925  
  2926  	nif = nod(OIF, N, N);
  2927  	nif->ninit = list(nif->ninit, call);
  2928  	nif->ntest = nod(ONOT, call, N);
  2929  	r = nod(ORETURN, N, N);
  2930  	r->list = list(r->list, nodbool(0));
  2931  	nif->nbody = list(nif->nbody, r);
  2932  	return nif;
  2933  }
  2934  
  2935  /*
  2936   * Generate a helper function to check equality of two values of type t.
  2937   */
  2938  void
  2939  geneq(Sym *sym, Type *t)
  2940  {
  2941  	Node *n, *fn, *np, *nq, *tfn, *nif, *ni, *nx, *ny, *nrange, *r;
  2942  	Type *t1, *first;
  2943  	int old_safemode;
  2944  	int64 size;
  2945  	int64 offend;
  2946  
  2947  	if(debug['r'])
  2948  		print("geneq %S %T\n", sym, t);
  2949  
  2950  	lineno = 1;  // less confusing than end of input
  2951  	dclcontext = PEXTERN;
  2952  	markdcl();
  2953  
  2954  	// func sym(p, q *T, s uintptr) bool
  2955  	fn = nod(ODCLFUNC, N, N);
  2956  	fn->nname = newname(sym);
  2957  	fn->nname->class = PFUNC;
  2958  	tfn = nod(OTFUNC, N, N);
  2959  	fn->nname->ntype = tfn;
  2960  
  2961  	n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t)));
  2962  	tfn->list = list(tfn->list, n);
  2963  	np = n->left;
  2964  	n = nod(ODCLFIELD, newname(lookup("q")), typenod(ptrto(t)));
  2965  	tfn->list = list(tfn->list, n);
  2966  	nq = n->left;
  2967  	n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR]));
  2968  	tfn->list = list(tfn->list, n);
  2969  	n = nod(ODCLFIELD, N, typenod(types[TBOOL]));
  2970  	tfn->rlist = list(tfn->rlist, n);
  2971  
  2972  	funchdr(fn);
  2973  
  2974  	// geneq is only called for types that have equality but
  2975  	// cannot be handled by the standard algorithms,
  2976  	// so t must be either an array or a struct.
  2977  	switch(t->etype) {
  2978  	default:
  2979  		fatal("geneq %T", t);
  2980  	case TARRAY:
  2981  		if(isslice(t))
  2982  			fatal("geneq %T", t);
  2983  		// An array of pure memory would be handled by the
  2984  		// standard memequal, so the element type must not be
  2985  		// pure memory.  Even if we unrolled the range loop,
  2986  		// each iteration would be a function call, so don't bother
  2987  		// unrolling.
  2988  		nrange = nod(ORANGE, N, nod(OIND, np, N));
  2989  		ni = newname(lookup("i"));
  2990  		ni->type = types[TINT];
  2991  		nrange->list = list1(ni);
  2992  		nrange->colas = 1;
  2993  		colasdefn(nrange->list, nrange);
  2994  		ni = nrange->list->n;
  2995  		
  2996  		// if p[i] != q[i] { return false }
  2997  		nx = nod(OINDEX, np, ni);
  2998  		nx->bounded = 1;
  2999  		ny = nod(OINDEX, nq, ni);
  3000  		ny->bounded = 1;
  3001  
  3002  		nif = nod(OIF, N, N);
  3003  		nif->ntest = nod(ONE, nx, ny);
  3004  		r = nod(ORETURN, N, N);
  3005  		r->list = list(r->list, nodbool(0));
  3006  		nif->nbody = list(nif->nbody, r);
  3007  		nrange->nbody = list(nrange->nbody, nif);
  3008  		fn->nbody = list(fn->nbody, nrange);
  3009  		break;
  3010  
  3011  	case TSTRUCT:
  3012  		// Walk the struct using memequal for runs of AMEM
  3013  		// and calling specific equality tests for the others.
  3014  		// Skip blank-named fields.
  3015  		first = T;
  3016  		offend = 0;
  3017  		for(t1=t->type;; t1=t1->down) {
  3018  			if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) {
  3019  				offend = t1->width + t1->type->width;
  3020  				if(first == T)
  3021  					first = t1;
  3022  				// If it's a memory field but it's padded, stop here.
  3023  				if(ispaddedfield(t1, t->width))
  3024  					t1 = t1->down;
  3025  				else
  3026  					continue;
  3027  			}
  3028  			// Run memequal for fields up to this one.
  3029  			// TODO(rsc): All the calls to newname are wrong for
  3030  			// cross-package unexported fields.
  3031  			if(first != T) {
  3032  				if(first->down == t1) {
  3033  					fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym)));
  3034  				} else if(first->down->down == t1) {
  3035  					fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym)));
  3036  					first = first->down;
  3037  					if(!isblanksym(first->sym))
  3038  						fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym)));
  3039  				} else {
  3040  					// More than two fields: use memequal.
  3041  					size = offend - first->width; // first->width is offset
  3042  					fn->nbody = list(fn->nbody, eqmem(np, nq, newname(first->sym), size));
  3043  				}
  3044  				first = T;
  3045  			}
  3046  			if(t1 == T)
  3047  				break;
  3048  			if(isblanksym(t1->sym))
  3049  				continue;
  3050  
  3051  			// Check this field, which is not just memory.
  3052  			fn->nbody = list(fn->nbody, eqfield(np, nq, newname(t1->sym)));
  3053  		}
  3054  
  3055  		break;
  3056  	}
  3057  
  3058  	// return true
  3059  	r = nod(ORETURN, N, N);
  3060  	r->list = list(r->list, nodbool(1));
  3061  	fn->nbody = list(fn->nbody, r);
  3062  
  3063  	if(debug['r'])
  3064  		dumplist("geneq body", fn->nbody);
  3065  
  3066  	funcbody(fn);
  3067  	curfn = fn;
  3068  	fn->dupok = 1;
  3069  	typecheck(&fn, Etop);
  3070  	typechecklist(fn->nbody, Etop);
  3071  	curfn = nil;
  3072  	
  3073  	// Disable safemode while compiling this code: the code we
  3074  	// generate internally can refer to unsafe.Pointer.
  3075  	// In this case it can happen if we need to generate an ==
  3076  	// for a struct containing a reflect.Value, which itself has
  3077  	// an unexported field of type unsafe.Pointer.
  3078  	old_safemode = safemode;
  3079  	safemode = 0;
  3080  	funccompile(fn, 0);
  3081  	safemode = old_safemode;
  3082  }
  3083  
  3084  static Type*
  3085  ifacelookdot(Sym *s, Type *t, int *followptr, int ignorecase)
  3086  {
  3087  	int i, c, d;
  3088  	Type *m;
  3089  
  3090  	*followptr = 0;
  3091  
  3092  	if(t == T)
  3093  		return T;
  3094  
  3095  	for(d=0; d<nelem(dotlist); d++) {
  3096  		c = adddot1(s, t, d, &m, ignorecase);
  3097  		if(c > 1) {
  3098  			yyerror("%T.%S is ambiguous", t, s);
  3099  			return T;
  3100  		}
  3101  		if(c == 1) {
  3102  			for(i=0; i<d; i++) {
  3103  				if(isptr[dotlist[i].field->type->etype]) {
  3104  					*followptr = 1;
  3105  					break;
  3106  				}
  3107  			}
  3108  			if(m->type->etype != TFUNC || m->type->thistuple == 0) {
  3109  				yyerror("%T.%S is a field, not a method", t, s);
  3110  				return T;
  3111  			}
  3112  			return m;
  3113  		}
  3114  	}
  3115  	return T;
  3116  }
  3117  
  3118  int
  3119  implements(Type *t, Type *iface, Type **m, Type **samename, int *ptr)
  3120  {
  3121  	Type *t0, *im, *tm, *rcvr, *imtype;
  3122  	int followptr;
  3123  
  3124  	t0 = t;
  3125  	if(t == T)
  3126  		return 0;
  3127  
  3128  	// if this is too slow,
  3129  	// could sort these first
  3130  	// and then do one loop.
  3131  
  3132  	if(t->etype == TINTER) {
  3133  		for(im=iface->type; im; im=im->down) {
  3134  			for(tm=t->type; tm; tm=tm->down) {
  3135  				if(tm->sym == im->sym) {
  3136  					if(eqtype(tm->type, im->type))
  3137  						goto found;
  3138  					*m = im;
  3139  					*samename = tm;
  3140  					*ptr = 0;
  3141  					return 0;
  3142  				}
  3143  			}
  3144  			*m = im;
  3145  			*samename = nil;
  3146  			*ptr = 0;
  3147  			return 0;
  3148  		found:;
  3149  		}
  3150  		return 1;
  3151  	}
  3152  
  3153  	t = methtype(t, 0);
  3154  	if(t != T)
  3155  		expandmeth(t);
  3156  	for(im=iface->type; im; im=im->down) {
  3157  		imtype = methodfunc(im->type, 0);
  3158  		tm = ifacelookdot(im->sym, t, &followptr, 0);
  3159  		if(tm == T || tm->nointerface || !eqtype(methodfunc(tm->type, 0), imtype)) {
  3160  			if(tm == T)
  3161  				tm = ifacelookdot(im->sym, t, &followptr, 1);
  3162  			*m = im;
  3163  			*samename = tm;
  3164  			*ptr = 0;
  3165  			return 0;
  3166  		}
  3167  		// if pointer receiver in method,
  3168  		// the method does not exist for value types.
  3169  		rcvr = getthisx(tm->type)->type->type;
  3170  		if(isptr[rcvr->etype] && !isptr[t0->etype] && !followptr && !isifacemethod(tm->type)) {
  3171  			if(0 && debug['r'])
  3172  				yyerror("interface pointer mismatch");
  3173  
  3174  			*m = im;
  3175  			*samename = nil;
  3176  			*ptr = 1;
  3177  			return 0;
  3178  		}
  3179  	}
  3180  	return 1;
  3181  }
  3182  
  3183  /*
  3184   * even simpler simtype; get rid of ptr, bool.
  3185   * assuming that the front end has rejected
  3186   * all the invalid conversions (like ptr -> bool)
  3187   */
  3188  int
  3189  simsimtype(Type *t)
  3190  {
  3191  	int et;
  3192  
  3193  	if(t == 0)
  3194  		return 0;
  3195  
  3196  	et = simtype[t->etype];
  3197  	switch(et) {
  3198  	case TPTR32:
  3199  		et = TUINT32;
  3200  		break;
  3201  	case TPTR64:
  3202  		et = TUINT64;
  3203  		break;
  3204  	case TBOOL:
  3205  		et = TUINT8;
  3206  		break;
  3207  	}
  3208  	return et;
  3209  }
  3210  
  3211  NodeList*
  3212  concat(NodeList *a, NodeList *b)
  3213  {
  3214  	if(a == nil)
  3215  		return b;
  3216  	if(b == nil)
  3217  		return a;
  3218  
  3219  	a->end->next = b;
  3220  	a->end = b->end;
  3221  	b->end = nil;
  3222  	return a;
  3223  }
  3224  
  3225  NodeList*
  3226  list1(Node *n)
  3227  {
  3228  	NodeList *l;
  3229  
  3230  	if(n == nil)
  3231  		return nil;
  3232  	if(n->op == OBLOCK && n->ninit == nil) {
  3233  		// Flatten list and steal storage.
  3234  		// Poison pointer to catch errant uses.
  3235  		l = n->list;
  3236  		n->list = (NodeList*)1;
  3237  		return l;
  3238  	}
  3239  	l = mal(sizeof *l);
  3240  	l->n = n;
  3241  	l->end = l;
  3242  	return l;
  3243  }
  3244  
  3245  NodeList*
  3246  list(NodeList *l, Node *n)
  3247  {
  3248  	return concat(l, list1(n));
  3249  }
  3250  
  3251  void
  3252  listsort(NodeList** l, int(*f)(Node*, Node*))
  3253  {
  3254  	NodeList *l1, *l2, *le;
  3255  
  3256  	if(*l == nil || (*l)->next == nil)
  3257  		return;
  3258  
  3259  	l1 = *l;
  3260  	l2 = *l;
  3261  	for(;;) {
  3262  		l2 = l2->next;
  3263  		if(l2 == nil)
  3264  			break;
  3265  		l2 = l2->next;
  3266  		if(l2 == nil)
  3267  			break;
  3268  		l1 = l1->next;
  3269  	}
  3270  
  3271  	l2 = l1->next;
  3272  	l1->next = nil;
  3273  	l2->end = (*l)->end;
  3274  	(*l)->end = l1;
  3275  
  3276  	l1 = *l;
  3277  	listsort(&l1, f);
  3278  	listsort(&l2, f);
  3279  
  3280  	if((*f)(l1->n, l2->n) < 0) {
  3281  		*l = l1;
  3282  	} else {
  3283  		*l = l2;
  3284  		l2 = l1;
  3285  		l1 = *l;
  3286  	}
  3287  
  3288  	// now l1 == *l; and l1 < l2
  3289  
  3290  	while ((l1 != nil) && (l2 != nil)) {
  3291  		while ((l1->next != nil) && (*f)(l1->next->n, l2->n) < 0)
  3292  			l1 = l1->next;
  3293  		
  3294  		// l1 is last one from l1 that is < l2
  3295  		le = l1->next;		// le is the rest of l1, first one that is >= l2
  3296  		if(le != nil)
  3297  			le->end = (*l)->end;
  3298  
  3299  		(*l)->end = l1;		// cut *l at l1
  3300  		*l = concat(*l, l2);	// glue l2 to *l's tail
  3301  
  3302  		l1 = l2;		// l1 is the first element of *l that is < the new l2
  3303  		l2 = le;		// ... because l2 now is the old tail of l1
  3304  	}
  3305  
  3306  	*l = concat(*l, l2);		// any remainder 
  3307  }
  3308  
  3309  NodeList*
  3310  listtreecopy(NodeList *l)
  3311  {
  3312  	NodeList *out;
  3313  
  3314  	out = nil;
  3315  	for(; l; l=l->next)
  3316  		out = list(out, treecopy(l->n));
  3317  	return out;
  3318  }
  3319  
  3320  Node*
  3321  liststmt(NodeList *l)
  3322  {
  3323  	Node *n;
  3324  
  3325  	n = nod(OBLOCK, N, N);
  3326  	n->list = l;
  3327  	if(l)
  3328  		n->lineno = l->n->lineno;
  3329  	return n;
  3330  }
  3331  
  3332  /*
  3333   * return nelem of list
  3334   */
  3335  int
  3336  count(NodeList *l)
  3337  {
  3338  	vlong n;
  3339  
  3340  	n = 0;
  3341  	for(; l; l=l->next)
  3342  		n++;
  3343  	if((int)n != n) { // Overflow.
  3344  		yyerror("too many elements in list");
  3345  	}
  3346  	return n;
  3347  }
  3348  
  3349  /*
  3350   * return nelem of list
  3351   */
  3352  int
  3353  structcount(Type *t)
  3354  {
  3355  	int v;
  3356  	Iter s;
  3357  
  3358  	v = 0;
  3359  	for(t = structfirst(&s, &t); t != T; t = structnext(&s))
  3360  		v++;
  3361  	return v;
  3362  }
  3363  
  3364  /*
  3365   * return power of 2 of the constant
  3366   * operand. -1 if it is not a power of 2.
  3367   * 1000+ if it is a -(power of 2)
  3368   */
  3369  int
  3370  powtwo(Node *n)
  3371  {
  3372  	uvlong v, b;
  3373  	int i;
  3374  
  3375  	if(n == N || n->op != OLITERAL || n->type == T)
  3376  		goto no;
  3377  	if(!isint[n->type->etype])
  3378  		goto no;
  3379  
  3380  	v = mpgetfix(n->val.u.xval);
  3381  	b = 1ULL;
  3382  	for(i=0; i<64; i++) {
  3383  		if(b == v)
  3384  			return i;
  3385  		b = b<<1;
  3386  	}
  3387  
  3388  	if(!issigned[n->type->etype])
  3389  		goto no;
  3390  
  3391  	v = -v;
  3392  	b = 1ULL;
  3393  	for(i=0; i<64; i++) {
  3394  		if(b == v)
  3395  			return i+1000;
  3396  		b = b<<1;
  3397  	}
  3398  
  3399  no:
  3400  	return -1;
  3401  }
  3402  
  3403  /*
  3404   * return the unsigned type for
  3405   * a signed integer type.
  3406   * returns T if input is not a
  3407   * signed integer type.
  3408   */
  3409  Type*
  3410  tounsigned(Type *t)
  3411  {
  3412  
  3413  	// this is types[et+1], but not sure
  3414  	// that this relation is immutable
  3415  	switch(t->etype) {
  3416  	default:
  3417  		print("tounsigned: unknown type %T\n", t);
  3418  		t = T;
  3419  		break;
  3420  	case TINT:
  3421  		t = types[TUINT];
  3422  		break;
  3423  	case TINT8:
  3424  		t = types[TUINT8];
  3425  		break;
  3426  	case TINT16:
  3427  		t = types[TUINT16];
  3428  		break;
  3429  	case TINT32:
  3430  		t = types[TUINT32];
  3431  		break;
  3432  	case TINT64:
  3433  		t = types[TUINT64];
  3434  		break;
  3435  	}
  3436  	return t;
  3437  }
  3438  
  3439  /*
  3440   * magic number for signed division
  3441   * see hacker's delight chapter 10
  3442   */
  3443  void
  3444  smagic(Magic *m)
  3445  {
  3446  	int p;
  3447  	uint64 ad, anc, delta, q1, r1, q2, r2, t;
  3448  	uint64 mask, two31;
  3449  
  3450  	m->bad = 0;
  3451  	switch(m->w) {
  3452  	default:
  3453  		m->bad = 1;
  3454  		return;
  3455  	case 8:
  3456  		mask = 0xffLL;
  3457  		break;
  3458  	case 16:
  3459  		mask = 0xffffLL;
  3460  		break;
  3461  	case 32:
  3462  		mask = 0xffffffffLL;
  3463  		break;
  3464  	case 64:
  3465  		mask = 0xffffffffffffffffULL;
  3466  		break;
  3467  	}
  3468  	two31 = mask ^ (mask>>1);
  3469  
  3470  	p = m->w-1;
  3471  	ad = m->sd;
  3472  	if(m->sd < 0)
  3473  		ad = -(uvlong)m->sd;
  3474  
  3475  	// bad denominators
  3476  	if(ad == 0 || ad == 1 || ad == two31) {
  3477  		m->bad = 1;
  3478  		return;
  3479  	}
  3480  
  3481  	t = two31;
  3482  	ad &= mask;
  3483  
  3484  	anc = t - 1 - t%ad;
  3485  	anc &= mask;
  3486  
  3487  	q1 = two31/anc;
  3488  	r1 = two31 - q1*anc;
  3489  	q1 &= mask;
  3490  	r1 &= mask;
  3491  
  3492  	q2 = two31/ad;
  3493  	r2 = two31 - q2*ad;
  3494  	q2 &= mask;
  3495  	r2 &= mask;
  3496  
  3497  	for(;;) {
  3498  		p++;
  3499  		q1 <<= 1;
  3500  		r1 <<= 1;
  3501  		q1 &= mask;
  3502  		r1 &= mask;
  3503  		if(r1 >= anc) {
  3504  			q1++;
  3505  			r1 -= anc;
  3506  			q1 &= mask;
  3507  			r1 &= mask;
  3508  		}
  3509  
  3510  		q2 <<= 1;
  3511  		r2 <<= 1;
  3512  		q2 &= mask;
  3513  		r2 &= mask;
  3514  		if(r2 >= ad) {
  3515  			q2++;
  3516  			r2 -= ad;
  3517  			q2 &= mask;
  3518  			r2 &= mask;
  3519  		}
  3520  
  3521  		delta = ad - r2;
  3522  		delta &= mask;
  3523  		if(q1 < delta || (q1 == delta && r1 == 0)) {
  3524  			continue;
  3525  		}
  3526  		break;
  3527  	}
  3528  
  3529  	m->sm = q2+1;
  3530  	if(m->sm & two31)
  3531  		m->sm |= ~mask;
  3532  	m->s = p-m->w;
  3533  }
  3534  
  3535  /*
  3536   * magic number for unsigned division
  3537   * see hacker's delight chapter 10
  3538   */
  3539  void
  3540  umagic(Magic *m)
  3541  {
  3542  	int p;
  3543  	uint64 nc, delta, q1, r1, q2, r2;
  3544  	uint64 mask, two31;
  3545  
  3546  	m->bad = 0;
  3547  	m->ua = 0;
  3548  
  3549  	switch(m->w) {
  3550  	default:
  3551  		m->bad = 1;
  3552  		return;
  3553  	case 8:
  3554  		mask = 0xffLL;
  3555  		break;
  3556  	case 16:
  3557  		mask = 0xffffLL;
  3558  		break;
  3559  	case 32:
  3560  		mask = 0xffffffffLL;
  3561  		break;
  3562  	case 64:
  3563  		mask = 0xffffffffffffffffULL;
  3564  		break;
  3565  	}
  3566  	two31 = mask ^ (mask>>1);
  3567  
  3568  	m->ud &= mask;
  3569  	if(m->ud == 0 || m->ud == two31) {
  3570  		m->bad = 1;
  3571  		return;
  3572  	}
  3573  	nc = mask - (-m->ud&mask)%m->ud;
  3574  	p = m->w-1;
  3575  
  3576  	q1 = two31/nc;
  3577  	r1 = two31 - q1*nc;
  3578  	q1 &= mask;
  3579  	r1 &= mask;
  3580  
  3581  	q2 = (two31-1) / m->ud;
  3582  	r2 = (two31-1) - q2*m->ud;
  3583  	q2 &= mask;
  3584  	r2 &= mask;
  3585  
  3586  	for(;;) {
  3587  		p++;
  3588  		if(r1 >= nc-r1) {
  3589  			q1 <<= 1;
  3590  			q1++;
  3591  			r1 <<= 1;
  3592  			r1 -= nc;
  3593  		} else {
  3594  			q1 <<= 1;
  3595  			r1 <<= 1;
  3596  		}
  3597  		q1 &= mask;
  3598  		r1 &= mask;
  3599  		if(r2+1 >= m->ud-r2) {
  3600  			if(q2 >= two31-1) {
  3601  				m->ua = 1;
  3602  			}
  3603  			q2 <<= 1;
  3604  			q2++;
  3605  			r2 <<= 1;
  3606  			r2++;
  3607  			r2 -= m->ud;
  3608  		} else {
  3609  			if(q2 >= two31) {
  3610  				m->ua = 1;
  3611  			}
  3612  			q2 <<= 1;
  3613  			r2 <<= 1;
  3614  			r2++;
  3615  		}
  3616  		q2 &= mask;
  3617  		r2 &= mask;
  3618  
  3619  		delta = m->ud - 1 - r2;
  3620  		delta &= mask;
  3621  
  3622  		if(p < m->w+m->w)
  3623  		if(q1 < delta || (q1 == delta && r1 == 0)) {
  3624  			continue;
  3625  		}
  3626  		break;
  3627  	}
  3628  	m->um = q2+1;
  3629  	m->s = p-m->w;
  3630  }
  3631  
  3632  Sym*
  3633  ngotype(Node *n)
  3634  {
  3635  	if(n->type != T)
  3636  		return typenamesym(n->type);
  3637  	return S;
  3638  }
  3639  
  3640  /*
  3641   * Convert raw string to the prefix that will be used in the symbol
  3642   * table.  All control characters, space, '%' and '"', as well as
  3643   * non-7-bit clean bytes turn into %xx.  The period needs escaping
  3644   * only in the last segment of the path, and it makes for happier
  3645   * users if we escape that as little as possible.
  3646   *
  3647   * If you edit this, edit ../ld/lib.c:/^pathtoprefix too.
  3648   * If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too.
  3649   */
  3650  static char*
  3651  pathtoprefix(char *s)
  3652  {
  3653  	static char hex[] = "0123456789abcdef";
  3654  	char *p, *r, *w, *l;
  3655  	int n;
  3656  
  3657  	// find first character past the last slash, if any.
  3658  	l = s;
  3659  	for(r=s; *r; r++)
  3660  		if(*r == '/')
  3661  			l = r+1;
  3662  
  3663  	// check for chars that need escaping
  3664  	n = 0;
  3665  	for(r=s; *r; r++)
  3666  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f)
  3667  			n++;
  3668  
  3669  	// quick exit
  3670  	if(n == 0)
  3671  		return s;
  3672  
  3673  	// escape
  3674  	p = mal((r-s)+1+2*n);
  3675  	for(r=s, w=p; *r; r++) {
  3676  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) {
  3677  			*w++ = '%';
  3678  			*w++ = hex[(*r>>4)&0xF];
  3679  			*w++ = hex[*r&0xF];
  3680  		} else
  3681  			*w++ = *r;
  3682  	}
  3683  	*w = '\0';
  3684  	return p;
  3685  }
  3686  
  3687  Pkg*
  3688  mkpkg(Strlit *path)
  3689  {
  3690  	Pkg *p;
  3691  	int h;
  3692  
  3693  	h = stringhash(path->s) & (nelem(phash)-1);
  3694  	for(p=phash[h]; p; p=p->link)
  3695  		if(p->path->len == path->len && memcmp(path->s, p->path->s, path->len) == 0)
  3696  			return p;
  3697  
  3698  	p = mal(sizeof *p);
  3699  	p->path = path;
  3700  	p->prefix = pathtoprefix(path->s);
  3701  	p->link = phash[h];
  3702  	phash[h] = p;
  3703  	return p;
  3704  }
  3705  
  3706  Strlit*
  3707  strlit(char *s)
  3708  {
  3709  	Strlit *t;
  3710  	
  3711  	t = mal(sizeof *t + strlen(s));
  3712  	strcpy(t->s, s);
  3713  	t->len = strlen(s);
  3714  	return t;
  3715  }
  3716  
  3717  void
  3718  addinit(Node **np, NodeList *init)
  3719  {
  3720  	Node *n;
  3721  	
  3722  	if(init == nil)
  3723  		return;
  3724  
  3725  	n = *np;
  3726  	switch(n->op) {
  3727  	case ONAME:
  3728  	case OLITERAL:
  3729  		// There may be multiple refs to this node;
  3730  		// introduce OCONVNOP to hold init list.
  3731  		n = nod(OCONVNOP, n, N);
  3732  		n->type = n->left->type;
  3733  		n->typecheck = 1;
  3734  		*np = n;
  3735  		break;
  3736  	}
  3737  	n->ninit = concat(init, n->ninit);
  3738  	n->ullman = UINF;
  3739  }
  3740  
  3741  static char* reservedimports[] = {
  3742  	"go",
  3743  	"type",
  3744  };
  3745  
  3746  int
  3747  isbadimport(Strlit *path)
  3748  {
  3749  	int i;
  3750  	char *s;
  3751  	Rune r;
  3752  
  3753  	if(strlen(path->s) != path->len) {
  3754  		yyerror("import path contains NUL");
  3755  		return 1;
  3756  	}
  3757  	
  3758  	for(i=0; i<nelem(reservedimports); i++) {
  3759  		if(strcmp(path->s, reservedimports[i]) == 0) {
  3760  			yyerror("import path \"%s\" is reserved and cannot be used", path->s);
  3761  			return 1;
  3762  		}
  3763  	}
  3764  
  3765  	s = path->s;
  3766  	while(*s) {
  3767  		s += chartorune(&r, s);
  3768  		if(r == Runeerror) {
  3769  			yyerror("import path contains invalid UTF-8 sequence: \"%Z\"", path);
  3770  			return 1;
  3771  		}
  3772  		if(r < 0x20 || r == 0x7f) {
  3773  			yyerror("import path contains control character: \"%Z\"", path);
  3774  			return 1;
  3775  		}
  3776  		if(r == '\\') {
  3777  			yyerror("import path contains backslash; use slash: \"%Z\"", path);
  3778  			return 1;
  3779  		}
  3780  		if(isspacerune(r)) {
  3781  			yyerror("import path contains space character: \"%Z\"", path);
  3782  			return 1;
  3783  		}
  3784  		if(utfrune("!\"#$%&'()*,:;<=>?[]^`{|}", r)) {
  3785  			yyerror("import path contains invalid character '%C': \"%Z\"", r, path);
  3786  			return 1;
  3787  		}
  3788  	}
  3789  	return 0;
  3790  }
  3791  
  3792  void
  3793  checknil(Node *x, NodeList **init)
  3794  {
  3795  	Node *n;
  3796  	
  3797  	if(isinter(x->type)) {
  3798  		x = nod(OITAB, x, N);
  3799  		typecheck(&x, Erv);
  3800  	}
  3801  	n = nod(OCHECKNIL, x, N);
  3802  	n->typecheck = 1;
  3803  	*init = list(*init, n);
  3804  }
  3805  
  3806  /*
  3807   * Can this type be stored directly in an interface word?
  3808   * Yes, if the representation is a single pointer.
  3809   */
  3810  int
  3811  isdirectiface(Type *t)
  3812  {
  3813  	switch(t->etype) {
  3814  	case TPTR32:
  3815  	case TPTR64:
  3816  	case TCHAN:
  3817  	case TMAP:
  3818  	case TFUNC:
  3819  	case TUNSAFEPTR:
  3820  		return 1;
  3821  	case TARRAY:
  3822  		// Array of 1 direct iface type can be direct.
  3823  		return t->bound == 1 && isdirectiface(t->type);
  3824  	case TSTRUCT:
  3825  		// Struct with 1 field of direct iface type can be direct.
  3826  		return t->type != T && t->type->down == T && isdirectiface(t->type->type);
  3827  	}
  3828  	return 0;
  3829  }