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