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