github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/gc/dcl.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	"y.tab.h"
     9  
    10  static	void	funcargs(Node*);
    11  static	void	funcargs2(Type*);
    12  
    13  static int
    14  dflag(void)
    15  {
    16  	if(!debug['d'])
    17  		return 0;
    18  	if(debug['y'])
    19  		return 1;
    20  	if(incannedimport)
    21  		return 0;
    22  	return 1;
    23  }
    24  
    25  /*
    26   * declaration stack & operations
    27   */
    28  
    29  static void
    30  dcopy(Sym *a, Sym *b)
    31  {
    32  	a->pkg = b->pkg;
    33  	a->name = b->name;
    34  	a->def = b->def;
    35  	a->block = b->block;
    36  	a->lastlineno = b->lastlineno;
    37  }
    38  
    39  static Sym*
    40  push(void)
    41  {
    42  	Sym *d;
    43  
    44  	d = mal(sizeof(*d));
    45  	d->lastlineno = lineno;
    46  	d->link = dclstack;
    47  	dclstack = d;
    48  	return d;
    49  }
    50  
    51  static Sym*
    52  pushdcl(Sym *s)
    53  {
    54  	Sym *d;
    55  
    56  	d = push();
    57  	dcopy(d, s);
    58  	if(dflag())
    59  		print("\t%L push %S %p\n", lineno, s, s->def);
    60  	return d;
    61  }
    62  
    63  void
    64  popdcl(void)
    65  {
    66  	Sym *d, *s;
    67  	int lno;
    68  
    69  //	if(dflag())
    70  //		print("revert\n");
    71  
    72  	for(d=dclstack; d!=S; d=d->link) {
    73  		if(d->name == nil)
    74  			break;
    75  		s = pkglookup(d->name, d->pkg);
    76  		lno = s->lastlineno;
    77  		dcopy(s, d);
    78  		d->lastlineno = lno;
    79  		if(dflag())
    80  			print("\t%L pop %S %p\n", lineno, s, s->def);
    81  	}
    82  	if(d == S)
    83  		fatal("popdcl: no mark");
    84  	dclstack = d->link;
    85  	block = d->block;
    86  }
    87  
    88  void
    89  poptodcl(void)
    90  {
    91  	// pop the old marker and push a new one
    92  	// (cannot reuse the existing one)
    93  	// because we use the markers to identify blocks
    94  	// for the goto restriction checks.
    95  	popdcl();
    96  	markdcl();
    97  }
    98  
    99  void
   100  markdcl(void)
   101  {
   102  	Sym *d;
   103  
   104  	d = push();
   105  	d->name = nil;		// used as a mark in fifo
   106  	d->block = block;
   107  
   108  	blockgen++;
   109  	block = blockgen;
   110  
   111  //	if(dflag())
   112  //		print("markdcl\n");
   113  }
   114  
   115  void
   116  dumpdcl(char *st)
   117  {
   118  	Sym *s, *d;
   119  	int i;
   120  
   121  	USED(st);
   122  
   123  	i = 0;
   124  	for(d=dclstack; d!=S; d=d->link) {
   125  		i++;
   126  		print("    %.2d %p", i, d);
   127  		if(d->name == nil) {
   128  			print("\n");
   129  			continue;
   130  		}
   131  		print(" '%s'", d->name);
   132  		s = pkglookup(d->name, d->pkg);
   133  		print(" %lS\n", s);
   134  	}
   135  }
   136  
   137  void
   138  testdclstack(void)
   139  {
   140  	Sym *d;
   141  
   142  	for(d=dclstack; d!=S; d=d->link) {
   143  		if(d->name == nil) {
   144  			yyerror("mark left on the stack");
   145  			continue;
   146  		}
   147  	}
   148  }
   149  
   150  void
   151  redeclare(Sym *s, char *where)
   152  {
   153  	Strlit *pkgstr;
   154  	int line1, line2;
   155  
   156  	if(s->lastlineno == 0) {
   157  		pkgstr = s->origpkg ? s->origpkg->path : s->pkg->path;
   158  		yyerror("%S redeclared %s\n"
   159  			"\tprevious declaration during import \"%Z\"",
   160  			s, where, pkgstr);
   161  	} else {
   162  		line1 = parserline();
   163  		line2 = s->lastlineno;
   164  		
   165  		// When an import and a declaration collide in separate files,
   166  		// present the import as the "redeclared", because the declaration
   167  		// is visible where the import is, but not vice versa.
   168  		// See issue 4510.
   169  		if(s->def == N) {
   170  			line2 = line1;
   171  			line1 = s->lastlineno;
   172  		}
   173  
   174  		yyerrorl(line1, "%S redeclared %s\n"
   175  			"\tprevious declaration at %L",
   176  			s, where, line2);
   177  	}
   178  }
   179  
   180  static int vargen;
   181  
   182  /*
   183   * declare individual names - var, typ, const
   184   */
   185  void
   186  declare(Node *n, int ctxt)
   187  {
   188  	Sym *s;
   189  	int gen;
   190  	static int typegen;
   191  	
   192  	if(ctxt == PDISCARD)
   193  		return;
   194  
   195  	if(isblank(n))
   196  		return;
   197  
   198  	n->lineno = parserline();
   199  	s = n->sym;
   200  
   201  	// kludgy: typecheckok means we're past parsing.  Eg genwrapper may declare out of package names later.
   202  	if(importpkg == nil && !typecheckok && s->pkg != localpkg)
   203  		yyerror("cannot declare name %S", s);
   204  
   205  	if(ctxt == PEXTERN && strcmp(s->name, "init") == 0)
   206  		yyerror("cannot declare init - must be func", s);
   207  
   208  	gen = 0;
   209  	if(ctxt == PEXTERN) {
   210  		externdcl = list(externdcl, n);
   211  		if(dflag())
   212  			print("\t%L global decl %S %p\n", lineno, s, n);
   213  	} else {
   214  		if(curfn == nil && ctxt == PAUTO)
   215  			fatal("automatic outside function");
   216  		if(curfn != nil)
   217  			curfn->dcl = list(curfn->dcl, n);
   218  		if(n->op == OTYPE)
   219  			gen = ++typegen;
   220  		else if(n->op == ONAME && ctxt == PAUTO && strstr(s->name, "·") == nil)
   221  			gen = ++vargen;
   222  		pushdcl(s);
   223  		n->curfn = curfn;
   224  	}
   225  	if(ctxt == PAUTO)
   226  		n->xoffset = 0;
   227  
   228  	if(s->block == block) {
   229  		// functype will print errors about duplicate function arguments.
   230  		// Don't repeat the error here.
   231  		if(ctxt != PPARAM && ctxt != PPARAMOUT)
   232  			redeclare(s, "in this block");
   233  	}
   234  
   235  	s->block = block;
   236  	s->lastlineno = parserline();
   237  	s->def = n;
   238  	n->vargen = gen;
   239  	n->funcdepth = funcdepth;
   240  	n->class = ctxt;
   241  
   242  	autoexport(n, ctxt);
   243  }
   244  
   245  void
   246  addvar(Node *n, Type *t, int ctxt)
   247  {
   248  	if(n==N || n->sym == S || (n->op != ONAME && n->op != ONONAME) || t == T)
   249  		fatal("addvar: n=%N t=%T nil", n, t);
   250  
   251  	n->op = ONAME;
   252  	declare(n, ctxt);
   253  	n->type = t;
   254  }
   255  
   256  /*
   257   * declare variables from grammar
   258   * new_name_list (type | [type] = expr_list)
   259   */
   260  NodeList*
   261  variter(NodeList *vl, Node *t, NodeList *el)
   262  {
   263  	int doexpr;
   264  	Node *v, *e, *as2;
   265  	NodeList *init;
   266  
   267  	init = nil;
   268  	doexpr = el != nil;
   269  
   270  	if(count(el) == 1 && count(vl) > 1) {
   271  		e = el->n;
   272  		as2 = nod(OAS2, N, N);
   273  		as2->list = vl;
   274  		as2->rlist = list1(e);
   275  		for(; vl; vl=vl->next) {
   276  			v = vl->n;
   277  			v->op = ONAME;
   278  			declare(v, dclcontext);
   279  			v->ntype = t;
   280  			v->defn = as2;
   281  			if(funcdepth > 0)
   282  				init = list(init, nod(ODCL, v, N));
   283  		}
   284  		return list(init, as2);
   285  	}
   286  	
   287  	for(; vl; vl=vl->next) {
   288  		if(doexpr) {
   289  			if(el == nil) {
   290  				yyerror("missing expr in var dcl");
   291  				break;
   292  			}
   293  			e = el->n;
   294  			el = el->next;
   295  		} else
   296  			e = N;
   297  
   298  		v = vl->n;
   299  		v->op = ONAME;
   300  		declare(v, dclcontext);
   301  		v->ntype = t;
   302  
   303  		if(e != N || funcdepth > 0 || isblank(v)) {
   304  			if(funcdepth > 0)
   305  				init = list(init, nod(ODCL, v, N));
   306  			e = nod(OAS, v, e);
   307  			init = list(init, e);
   308  			if(e->right != N)
   309  				v->defn = e;
   310  		}
   311  	}
   312  	if(el != nil)
   313  		yyerror("extra expr in var dcl");
   314  	return init;
   315  }
   316  
   317  /*
   318   * declare constants from grammar
   319   * new_name_list [[type] = expr_list]
   320   */
   321  NodeList*
   322  constiter(NodeList *vl, Node *t, NodeList *cl)
   323  {
   324  	Node *v, *c;
   325  	NodeList *vv;
   326  
   327  	vv = nil;
   328  	if(cl == nil) {
   329  		if(t != N)
   330  			yyerror("constdcl cannot have type without expr");
   331  		cl = lastconst;
   332  		t = lasttype;
   333  	} else {
   334  		lastconst = cl;
   335  		lasttype = t;
   336  	}
   337  	cl = listtreecopy(cl);
   338  
   339  	for(; vl; vl=vl->next) {
   340  		if(cl == nil) {
   341  			yyerror("missing expr in const dcl");
   342  			break;
   343  		}
   344  		c = cl->n;
   345  		cl = cl->next;
   346  
   347  		v = vl->n;
   348  		v->op = OLITERAL;
   349  		declare(v, dclcontext);
   350  
   351  		v->ntype = t;
   352  		v->defn = c;
   353  
   354  		vv = list(vv, nod(ODCLCONST, v, N));
   355  	}
   356  	if(cl != nil)
   357  		yyerror("extra expr in const dcl");
   358  	iota += 1;
   359  	return vv;
   360  }
   361  
   362  /*
   363   * this generates a new name node,
   364   * typically for labels or other one-off names.
   365   */
   366  Node*
   367  newname(Sym *s)
   368  {
   369  	Node *n;
   370  
   371  	if(s == S)
   372  		fatal("newname nil");
   373  
   374  	n = nod(ONAME, N, N);
   375  	n->sym = s;
   376  	n->type = T;
   377  	n->addable = 1;
   378  	n->ullman = 1;
   379  	n->xoffset = 0;
   380  	return n;
   381  }
   382  
   383  /*
   384   * this generates a new name node for a name
   385   * being declared.
   386   */
   387  Node*
   388  dclname(Sym *s)
   389  {
   390  	Node *n;
   391  
   392  	n = newname(s);
   393  	n->op = ONONAME;	// caller will correct it
   394  	return n;
   395  }
   396  
   397  Node*
   398  typenod(Type *t)
   399  {
   400  	// if we copied another type with *t = *u
   401  	// then t->nod might be out of date, so
   402  	// check t->nod->type too
   403  	if(t->nod == N || t->nod->type != t) {
   404  		t->nod = nod(OTYPE, N, N);
   405  		t->nod->type = t;
   406  		t->nod->sym = t->sym;
   407  	}
   408  	return t->nod;
   409  }
   410  
   411  
   412  /*
   413   * this will return an old name
   414   * that has already been pushed on the
   415   * declaration list. a diagnostic is
   416   * generated if no name has been defined.
   417   */
   418  Node*
   419  oldname(Sym *s)
   420  {
   421  	Node *n;
   422  	Node *c;
   423  
   424  	n = s->def;
   425  	if(n == N) {
   426  		// maybe a top-level name will come along
   427  		// to give this a definition later.
   428  		// walkdef will check s->def again once
   429  		// all the input source has been processed.
   430  		n = newname(s);
   431  		n->op = ONONAME;
   432  		n->iota = iota;	// save current iota value in const declarations
   433  	}
   434  	if(curfn != nil && n->funcdepth > 0 && n->funcdepth != funcdepth && n->op == ONAME) {
   435  		// inner func is referring to var in outer func.
   436  		//
   437  		// TODO(rsc): If there is an outer variable x and we
   438  		// are parsing x := 5 inside the closure, until we get to
   439  		// the := it looks like a reference to the outer x so we'll
   440  		// make x a closure variable unnecessarily.
   441  		if(n->closure == N || n->closure->funcdepth != funcdepth) {
   442  			// create new closure var.
   443  			c = nod(ONAME, N, N);
   444  			c->sym = s;
   445  			c->class = PPARAMREF;
   446  			c->isddd = n->isddd;
   447  			c->defn = n;
   448  			c->addable = 0;
   449  			c->ullman = 2;
   450  			c->funcdepth = funcdepth;
   451  			c->outer = n->closure;
   452  			n->closure = c;
   453  			n->addrtaken = 1;
   454  			c->closure = n;
   455  			c->xoffset = 0;
   456  			curfn->cvars = list(curfn->cvars, c);
   457  		}
   458  		// return ref to closure var, not original
   459  		return n->closure;
   460  	}
   461  	return n;
   462  }
   463  
   464  /*
   465   * := declarations
   466   */
   467  
   468  static int
   469  colasname(Node *n)
   470  {
   471  	switch(n->op) {
   472  	case ONAME:
   473  	case ONONAME:
   474  	case OPACK:
   475  	case OTYPE:
   476  	case OLITERAL:
   477  		return n->sym != S;
   478  	}
   479  	return 0;
   480  }
   481  
   482  void
   483  colasdefn(NodeList *left, Node *defn)
   484  {
   485  	int nnew, nerr;
   486  	NodeList *l;
   487  	Node *n;
   488  
   489  	nnew = 0;
   490  	nerr = 0;
   491  	for(l=left; l; l=l->next) {
   492  		n = l->n;
   493  		if(isblank(n))
   494  			continue;
   495  		if(!colasname(n)) {
   496  			yyerrorl(defn->lineno, "non-name %N on left side of :=", n);
   497  			nerr++;
   498  			continue;
   499  		}
   500  		if(n->sym->block == block)
   501  			continue;
   502  
   503  		nnew++;
   504  		n = newname(n->sym);
   505  		declare(n, dclcontext);
   506  		n->defn = defn;
   507  		defn->ninit = list(defn->ninit, nod(ODCL, n, N));
   508  		l->n = n;
   509  	}
   510  	if(nnew == 0 && nerr == 0)
   511  		yyerrorl(defn->lineno, "no new variables on left side of :=");
   512  }
   513  
   514  Node*
   515  colas(NodeList *left, NodeList *right, int32 lno)
   516  {
   517  	Node *as;
   518  
   519  	as = nod(OAS2, N, N);
   520  	as->list = left;
   521  	as->rlist = right;
   522  	as->colas = 1;
   523  	as->lineno = lno;
   524  	colasdefn(left, as);
   525  
   526  	// make the tree prettier; not necessary
   527  	if(count(left) == 1 && count(right) == 1) {
   528  		as->left = as->list->n;
   529  		as->right = as->rlist->n;
   530  		as->list = nil;
   531  		as->rlist = nil;
   532  		as->op = OAS;
   533  	}
   534  
   535  	return as;
   536  }
   537  
   538  /*
   539   * declare the arguments in an
   540   * interface field declaration.
   541   */
   542  void
   543  ifacedcl(Node *n)
   544  {
   545  	if(n->op != ODCLFIELD || n->right == N)
   546  		fatal("ifacedcl");
   547  
   548  	dclcontext = PPARAM;
   549  	markdcl();
   550  	funcdepth++;
   551  	n->outer = curfn;
   552  	curfn = n;
   553  	funcargs(n->right);
   554  
   555  	// funcbody is normally called after the parser has
   556  	// seen the body of a function but since an interface
   557  	// field declaration does not have a body, we must
   558  	// call it now to pop the current declaration context.
   559  	dclcontext = PAUTO;
   560  	funcbody(n);
   561  }
   562  
   563  /*
   564   * declare the function proper
   565   * and declare the arguments.
   566   * called in extern-declaration context
   567   * returns in auto-declaration context.
   568   */
   569  void
   570  funchdr(Node *n)
   571  {
   572  	// change the declaration context from extern to auto
   573  	if(funcdepth == 0 && dclcontext != PEXTERN)
   574  		fatal("funchdr: dclcontext");
   575  
   576  	dclcontext = PAUTO;
   577  	markdcl();
   578  	funcdepth++;
   579  
   580  	n->outer = curfn;
   581  	curfn = n;
   582  
   583  	if(n->nname)
   584  		funcargs(n->nname->ntype);
   585  	else if (n->ntype)
   586  		funcargs(n->ntype);
   587  	else
   588  		funcargs2(n->type);
   589  }
   590  
   591  static void
   592  funcargs(Node *nt)
   593  {
   594  	Node *n, *nn;
   595  	NodeList *l;
   596  	int gen;
   597  
   598  	if(nt->op != OTFUNC)
   599  		fatal("funcargs %O", nt->op);
   600  
   601  	// re-start the variable generation number
   602  	// we want to use small numbers for the return variables,
   603  	// so let them have the chunk starting at 1.
   604  	vargen = count(nt->rlist);
   605  
   606  	// declare the receiver and in arguments.
   607  	// no n->defn because type checking of func header
   608  	// will not fill in the types until later
   609  	if(nt->left != N) {
   610  		n = nt->left;
   611  		if(n->op != ODCLFIELD)
   612  			fatal("funcargs receiver %O", n->op);
   613  		if(n->left != N) {
   614  			n->left->op = ONAME;
   615  			n->left->ntype = n->right;
   616  			declare(n->left, PPARAM);
   617  			if(dclcontext == PAUTO)
   618  				n->left->vargen = ++vargen;
   619  		}
   620  	}
   621  	for(l=nt->list; l; l=l->next) {
   622  		n = l->n;
   623  		if(n->op != ODCLFIELD)
   624  			fatal("funcargs in %O", n->op);
   625  		if(n->left != N) {
   626  			n->left->op = ONAME;
   627  			n->left->ntype = n->right;
   628  			declare(n->left, PPARAM);
   629  			if(dclcontext == PAUTO)
   630  				n->left->vargen = ++vargen;
   631  		}
   632  	}
   633  
   634  	// declare the out arguments.
   635  	gen = count(nt->list);
   636  	int i = 0;
   637  	for(l=nt->rlist; l; l=l->next) {
   638  		n = l->n;
   639  
   640  		if(n->op != ODCLFIELD)
   641  			fatal("funcargs out %O", n->op);
   642  
   643  		if(n->left == N) {
   644  			// give it a name so escape analysis has nodes to work with
   645  			snprint(namebuf, sizeof(namebuf), "~anon%d", gen++);
   646  			n->left = newname(lookup(namebuf));
   647  			// TODO: n->left->missing = 1;
   648  		} 
   649  
   650  		n->left->op = ONAME;
   651  
   652  		if(isblank(n->left)) {
   653  			// Give it a name so we can assign to it during return.
   654  			// preserve the original in ->orig
   655  			nn = nod(OXXX, N, N);
   656  			*nn = *n->left;
   657  			n->left = nn;
   658  			
   659  			snprint(namebuf, sizeof(namebuf), "~anon%d", gen++);
   660  			n->left->sym = lookup(namebuf);
   661  		}
   662  
   663  		n->left->ntype = n->right;
   664  		declare(n->left, PPARAMOUT);
   665  		if(dclcontext == PAUTO)
   666  			n->left->vargen = ++i;
   667  	}
   668  }
   669  
   670  /*
   671   * Same as funcargs, except run over an already constructed TFUNC.
   672   * This happens during import, where the hidden_fndcl rule has
   673   * used functype directly to parse the function's type.
   674   */
   675  static void
   676  funcargs2(Type *t)
   677  {
   678  	Type *ft;
   679  	Node *n;
   680  
   681  	if(t->etype != TFUNC)
   682  		fatal("funcargs2 %T", t);
   683  	
   684  	if(t->thistuple)
   685  		for(ft=getthisx(t)->type; ft; ft=ft->down) {
   686  			if(!ft->nname || !ft->nname->sym)
   687  				continue;
   688  			n = ft->nname;  // no need for newname(ft->nname->sym)
   689  			n->type = ft->type;
   690  			declare(n, PPARAM);
   691  		}
   692  
   693  	if(t->intuple)
   694  		for(ft=getinargx(t)->type; ft; ft=ft->down) {
   695  			if(!ft->nname || !ft->nname->sym)
   696  				continue;
   697  			n = ft->nname;
   698  			n->type = ft->type;
   699  			declare(n, PPARAM);
   700  		}
   701  
   702  	if(t->outtuple)
   703  		for(ft=getoutargx(t)->type; ft; ft=ft->down) {
   704  			if(!ft->nname || !ft->nname->sym)
   705  				continue;
   706  			n = ft->nname;
   707  			n->type = ft->type;
   708  			declare(n, PPARAMOUT);
   709  		}
   710  }
   711  
   712  /*
   713   * finish the body.
   714   * called in auto-declaration context.
   715   * returns in extern-declaration context.
   716   */
   717  void
   718  funcbody(Node *n)
   719  {
   720  	// change the declaration context from auto to extern
   721  	if(dclcontext != PAUTO)
   722  		fatal("funcbody: dclcontext");
   723  	popdcl();
   724  	funcdepth--;
   725  	curfn = n->outer;
   726  	n->outer = N;
   727  	if(funcdepth == 0)
   728  		dclcontext = PEXTERN;
   729  }
   730  
   731  /*
   732   * new type being defined with name s.
   733   */
   734  Node*
   735  typedcl0(Sym *s)
   736  {
   737  	Node *n;
   738  
   739  	n = newname(s);
   740  	n->op = OTYPE;
   741  	declare(n, dclcontext);
   742  	return n;
   743  }
   744  
   745  /*
   746   * node n, which was returned by typedcl0
   747   * is being declared to have uncompiled type t.
   748   * return the ODCLTYPE node to use.
   749   */
   750  Node*
   751  typedcl1(Node *n, Node *t, int local)
   752  {
   753  	n->ntype = t;
   754  	n->local = local;
   755  	return nod(ODCLTYPE, n, N);
   756  }
   757  
   758  /*
   759   * structs, functions, and methods.
   760   * they don't belong here, but where do they belong?
   761   */
   762  
   763  static void
   764  checkembeddedtype(Type *t)
   765  {
   766  	if (t == T)
   767  		return;
   768  
   769  	if(t->sym == S && isptr[t->etype]) {
   770  		t = t->type;
   771  		if(t->etype == TINTER)
   772  			yyerror("embedded type cannot be a pointer to interface");
   773  	}
   774  	if(isptr[t->etype])
   775  		yyerror("embedded type cannot be a pointer");
   776  	else if(t->etype == TFORW && t->embedlineno == 0)
   777  		t->embedlineno = lineno;
   778  }
   779  
   780  static Type*
   781  structfield(Node *n)
   782  {
   783  	Type *f;
   784  	int lno;
   785  
   786  	lno = lineno;
   787  	lineno = n->lineno;
   788  
   789  	if(n->op != ODCLFIELD)
   790  		fatal("structfield: oops %N\n", n);
   791  
   792  	f = typ(TFIELD);
   793  	f->isddd = n->isddd;
   794  
   795  	if(n->right != N) {
   796  		typecheck(&n->right, Etype);
   797  		n->type = n->right->type;
   798  		if(n->left != N)
   799  			n->left->type = n->type;
   800  		if(n->embedded)
   801  			checkembeddedtype(n->type);
   802  	}
   803  	n->right = N;
   804  		
   805  	f->type = n->type;
   806  	if(f->type == T)
   807  		f->broke = 1;
   808  
   809  	switch(n->val.ctype) {
   810  	case CTSTR:
   811  		f->note = n->val.u.sval;
   812  		break;
   813  	default:
   814  		yyerror("field annotation must be string");
   815  		// fallthrough
   816  	case CTxxx:
   817  		f->note = nil;
   818  		break;
   819  	}
   820  
   821  	if(n->left && n->left->op == ONAME) {
   822  		f->nname = n->left;
   823  		f->embedded = n->embedded;
   824  		f->sym = f->nname->sym;
   825  	}
   826  
   827  	lineno = lno;
   828  	return f;
   829  }
   830  
   831  static uint32 uniqgen;
   832  
   833  static void
   834  checkdupfields(Type *t, char* what)
   835  {
   836  	int lno;
   837  
   838  	lno = lineno;
   839  
   840  	for( ; t; t=t->down) {
   841  		if(t->sym && t->nname && !isblank(t->nname)) {
   842  			if(t->sym->uniqgen == uniqgen) {
   843  				lineno = t->nname->lineno;
   844  				yyerror("duplicate %s %s", what, t->sym->name);
   845  			} else
   846  				t->sym->uniqgen = uniqgen;
   847  		}
   848  	}
   849  
   850  	lineno = lno;
   851  }
   852  
   853  /*
   854   * convert a parsed id/type list into
   855   * a type for struct/interface/arglist
   856   */
   857  Type*
   858  tostruct(NodeList *l)
   859  {
   860  	Type *t, *f, **tp;
   861  	t = typ(TSTRUCT);
   862  
   863  	for(tp = &t->type; l; l=l->next) {
   864  		f = structfield(l->n);
   865  
   866  		*tp = f;
   867  		tp = &f->down;
   868  	}
   869  
   870  	for(f=t->type; f && !t->broke; f=f->down)
   871  		if(f->broke)
   872  			t->broke = 1;
   873  
   874  	uniqgen++;
   875  	checkdupfields(t->type, "field");
   876  
   877  	if (!t->broke)
   878  		checkwidth(t);
   879  
   880  	return t;
   881  }
   882  
   883  static Type*
   884  tofunargs(NodeList *l)
   885  {
   886  	Type *t, *f, **tp;
   887  
   888  	t = typ(TSTRUCT);
   889  	t->funarg = 1;
   890  
   891  	for(tp = &t->type; l; l=l->next) {
   892  		f = structfield(l->n);
   893  		f->funarg = 1;
   894  
   895  		// esc.c needs to find f given a PPARAM to add the tag.
   896  		if(l->n->left && l->n->left->class == PPARAM)
   897  			l->n->left->paramfld = f;
   898  
   899  		*tp = f;
   900  		tp = &f->down;
   901  	}
   902  
   903  	for(f=t->type; f && !t->broke; f=f->down)
   904  		if(f->broke)
   905  			t->broke = 1;
   906  
   907  	return t;
   908  }
   909  
   910  static Type*
   911  interfacefield(Node *n)
   912  {
   913  	Type *f;
   914  	int lno;
   915  
   916  	lno = lineno;
   917  	lineno = n->lineno;
   918  
   919  	if(n->op != ODCLFIELD)
   920  		fatal("interfacefield: oops %N\n", n);
   921  
   922  	if (n->val.ctype != CTxxx)
   923  		yyerror("interface method cannot have annotation");
   924  
   925  	f = typ(TFIELD);
   926  	f->isddd = n->isddd;
   927  	
   928  	if(n->right != N) {
   929  		if(n->left != N) {
   930  			// queue resolution of method type for later.
   931  			// right now all we need is the name list.
   932  			// avoids cycles for recursive interface types.
   933  			n->type = typ(TINTERMETH);
   934  			n->type->nname = n->right;
   935  			n->left->type = n->type;
   936  			queuemethod(n);
   937  
   938  			if(n->left->op == ONAME) {
   939  				f->nname = n->left;
   940  				f->embedded = n->embedded;
   941  				f->sym = f->nname->sym;
   942  				if(importpkg && !exportname(f->sym->name))
   943  					f->sym = pkglookup(f->sym->name, structpkg);
   944  			}
   945  
   946  		} else {
   947  
   948  			typecheck(&n->right, Etype);
   949  			n->type = n->right->type;
   950  
   951  			if(n->embedded)
   952  				checkembeddedtype(n->type);
   953  
   954  			if(n->type)
   955  				switch(n->type->etype) {
   956  				case TINTER:
   957  					break;
   958  				case TFORW:
   959  					yyerror("interface type loop involving %T", n->type);
   960  					f->broke = 1;
   961  					break;
   962  				default:
   963  					yyerror("interface contains embedded non-interface %T", n->type);
   964  					f->broke = 1;
   965  					break;
   966  				}
   967  		}
   968  	}
   969  
   970  	n->right = N;
   971  	
   972  	f->type = n->type;
   973  	if(f->type == T)
   974  		f->broke = 1;
   975  	
   976  	lineno = lno;
   977  	return f;
   978  }
   979  
   980  Type*
   981  tointerface(NodeList *l)
   982  {
   983  	Type *t, *f, **tp, *t1;
   984  
   985  	t = typ(TINTER);
   986  
   987  	tp = &t->type;
   988  	for(; l; l=l->next) {
   989  		f = interfacefield(l->n);
   990  
   991  		if (l->n->left == N && f->type->etype == TINTER) {
   992  			// embedded interface, inline methods
   993  			for(t1=f->type->type; t1; t1=t1->down) {
   994  				f = typ(TFIELD);
   995  				f->type = t1->type;
   996  				f->broke = t1->broke;
   997  				f->sym = t1->sym;
   998  				if(f->sym)
   999  					f->nname = newname(f->sym);
  1000  				*tp = f;
  1001  				tp = &f->down;
  1002  			}
  1003  		} else {
  1004  			*tp = f;
  1005  			tp = &f->down;
  1006  		}
  1007  	}
  1008  
  1009  	for(f=t->type; f && !t->broke; f=f->down)
  1010  		if(f->broke)
  1011  			t->broke = 1;
  1012  
  1013  	uniqgen++;
  1014  	checkdupfields(t->type, "method");
  1015  	t = sortinter(t);
  1016  	checkwidth(t);
  1017  
  1018  	return t;
  1019  }
  1020  
  1021  Node*
  1022  embedded(Sym *s)
  1023  {
  1024  	Node *n;
  1025  	char *name;
  1026  
  1027  	// Names sometimes have disambiguation junk
  1028  	// appended after a center dot.  Discard it when
  1029  	// making the name for the embedded struct field.
  1030  	enum { CenterDot = 0xB7 };
  1031  	name = s->name;
  1032  	if(utfrune(s->name, CenterDot)) {
  1033  		name = strdup(s->name);
  1034  		*utfrune(name, CenterDot) = 0;
  1035  	}
  1036  
  1037  	if(exportname(name))
  1038  		n = newname(lookup(name));
  1039  	else if(s->pkg == builtinpkg && importpkg != nil)
  1040  		// The name of embedded builtins during imports belongs to importpkg.
  1041  		n = newname(pkglookup(name, importpkg));
  1042  	else
  1043  		n = newname(pkglookup(name, s->pkg));
  1044  	n = nod(ODCLFIELD, n, oldname(s));
  1045  	n->embedded = 1;
  1046  	return n;
  1047  }
  1048  
  1049  /*
  1050   * check that the list of declarations is either all anonymous or all named
  1051   */
  1052  
  1053  static Node*
  1054  findtype(NodeList *l)
  1055  {
  1056  	for(; l; l=l->next)
  1057  		if(l->n->op == OKEY)
  1058  			return l->n->right;
  1059  	return N;
  1060  }
  1061  
  1062  NodeList*
  1063  checkarglist(NodeList *all, int input)
  1064  {
  1065  	int named;
  1066  	Node *n, *t, *nextt;
  1067  	NodeList *l;
  1068  
  1069  	named = 0;
  1070  	for(l=all; l; l=l->next) {
  1071  		if(l->n->op == OKEY) {
  1072  			named = 1;
  1073  			break;
  1074  		}
  1075  	}
  1076  	if(named) {
  1077  		n = N;
  1078  		for(l=all; l; l=l->next) {
  1079  			n = l->n;
  1080  			if(n->op != OKEY && n->sym == S) {
  1081  				yyerror("mixed named and unnamed function parameters");
  1082  				break;
  1083  			}
  1084  		}
  1085  		if(l == nil && n != N && n->op != OKEY)
  1086  			yyerror("final function parameter must have type");
  1087  	}
  1088  
  1089  	nextt = nil;
  1090  	for(l=all; l; l=l->next) {
  1091  		// can cache result from findtype to avoid
  1092  		// quadratic behavior here, but unlikely to matter.
  1093  		n = l->n;
  1094  		if(named) {
  1095  			if(n->op == OKEY) {
  1096  				t = n->right;
  1097  				n = n->left;
  1098  				nextt = nil;
  1099  			} else {
  1100  				if(nextt == nil)
  1101  					nextt = findtype(l);
  1102  				t = nextt;
  1103  			}
  1104  		} else {
  1105  			t = n;
  1106  			n = N;
  1107  		}
  1108  
  1109  		// during import l->n->op is OKEY, but l->n->left->sym == S
  1110  		// means it was a '?', not that it was
  1111  		// a lone type This doesn't matter for the exported
  1112  		// declarations, which are parsed by rules that don't
  1113  		// use checkargs, but can happen for func literals in
  1114  		// the inline bodies.
  1115  		// TODO(rsc) this can go when typefmt case TFIELD in exportmode fmt.c prints _ instead of ?
  1116  		if(importpkg && n->sym == S)
  1117  			n = N;
  1118  
  1119  		if(n != N && n->sym == S) {
  1120  			t = n;
  1121  			n = N;
  1122  		}
  1123  		if(n != N)
  1124  			n = newname(n->sym);
  1125  		n = nod(ODCLFIELD, n, t);
  1126  		if(n->right != N && n->right->op == ODDD) {
  1127  			if(!input)
  1128  				yyerror("cannot use ... in output argument list");
  1129  			else if(l->next != nil)
  1130  				yyerror("can only use ... as final argument in list");
  1131  			n->right->op = OTARRAY;
  1132  			n->right->right = n->right->left;
  1133  			n->right->left = N;
  1134  			n->isddd = 1;
  1135  			if(n->left != N)
  1136  				n->left->isddd = 1;
  1137  		}
  1138  		l->n = n;
  1139  	}
  1140  	return all;
  1141  }
  1142  
  1143  
  1144  Node*
  1145  fakethis(void)
  1146  {
  1147  	Node *n;
  1148  
  1149  	n = nod(ODCLFIELD, N, typenod(ptrto(typ(TSTRUCT))));
  1150  	return n;
  1151  }
  1152  
  1153  /*
  1154   * Is this field a method on an interface?
  1155   * Those methods have an anonymous
  1156   * *struct{} as the receiver.
  1157   * (See fakethis above.)
  1158   */
  1159  int
  1160  isifacemethod(Type *f)
  1161  {
  1162  	Type *rcvr;
  1163  	Type *t;
  1164  
  1165  	rcvr = getthisx(f)->type;
  1166  	if(rcvr->sym != S)
  1167  		return 0;
  1168  	t = rcvr->type;
  1169  	if(!isptr[t->etype])
  1170  		return 0;
  1171  	t = t->type;
  1172  	if(t->sym != S || t->etype != TSTRUCT || t->type != T)
  1173  		return 0;
  1174  	return 1;
  1175  }
  1176  
  1177  /*
  1178   * turn a parsed function declaration
  1179   * into a type
  1180   */
  1181  Type*
  1182  functype(Node *this, NodeList *in, NodeList *out)
  1183  {
  1184  	Type *t;
  1185  	NodeList *rcvr;
  1186  	Sym *s;
  1187  
  1188  	t = typ(TFUNC);
  1189  
  1190  	rcvr = nil;
  1191  	if(this)
  1192  		rcvr = list1(this);
  1193  	t->type = tofunargs(rcvr);
  1194  	t->type->down = tofunargs(out);
  1195  	t->type->down->down = tofunargs(in);
  1196  
  1197  	uniqgen++;
  1198  	checkdupfields(t->type->type, "argument");
  1199  	checkdupfields(t->type->down->type, "argument");
  1200  	checkdupfields(t->type->down->down->type, "argument");
  1201  
  1202  	if (t->type->broke || t->type->down->broke || t->type->down->down->broke)
  1203  		t->broke = 1;
  1204  
  1205  	if(this)
  1206  		t->thistuple = 1;
  1207  	t->outtuple = count(out);
  1208  	t->intuple = count(in);
  1209  	t->outnamed = 0;
  1210  	if(t->outtuple > 0 && out->n->left != N && out->n->left->orig != N) {
  1211  		s = out->n->left->orig->sym;
  1212  		if(s != S && s->name[0] != '~')
  1213  			t->outnamed = 1;
  1214  	}
  1215  
  1216  	return t;
  1217  }
  1218  
  1219  Sym*
  1220  methodsym(Sym *nsym, Type *t0, int iface)
  1221  {
  1222  	Sym *s;
  1223  	char *p;
  1224  	Type *t;
  1225  	char *suffix;
  1226  	Pkg *spkg;
  1227  	static Pkg *toppkg;
  1228  
  1229  	t = t0;
  1230  	if(t == T)
  1231  		goto bad;
  1232  	s = t->sym;
  1233  	if(s == S && isptr[t->etype]) {
  1234  		t = t->type;
  1235  		if(t == T)
  1236  			goto bad;
  1237  		s = t->sym;
  1238  	}
  1239  	spkg = nil;
  1240  	if(s != S)
  1241  		spkg = s->pkg;
  1242  
  1243  	// if t0 == *t and t0 has a sym,
  1244  	// we want to see *t, not t0, in the method name.
  1245  	if(t != t0 && t0->sym)
  1246  		t0 = ptrto(t);
  1247  
  1248  	suffix = "";
  1249  	if(iface) {
  1250  		dowidth(t0);
  1251  		if(t0->width < types[tptr]->width)
  1252  			suffix = "·i";
  1253  	}
  1254  	if((spkg == nil || nsym->pkg != spkg) && !exportname(nsym->name)) {
  1255  		if(t0->sym == S && isptr[t0->etype])
  1256  			p = smprint("(%-hT).%s.%s%s", t0, nsym->pkg->prefix, nsym->name, suffix);
  1257  		else
  1258  			p = smprint("%-hT.%s.%s%s", t0, nsym->pkg->prefix, nsym->name, suffix);
  1259  	} else {
  1260  		if(t0->sym == S && isptr[t0->etype])
  1261  			p = smprint("(%-hT).%s%s", t0, nsym->name, suffix);
  1262  		else
  1263  			p = smprint("%-hT.%s%s", t0, nsym->name, suffix);
  1264  	}
  1265  	if(spkg == nil) {
  1266  		if(toppkg == nil)
  1267  			toppkg = mkpkg(strlit("go"));
  1268  		spkg = toppkg;
  1269  	}
  1270  	s = pkglookup(p, spkg);
  1271  	free(p);
  1272  	return s;
  1273  
  1274  bad:
  1275  	yyerror("illegal receiver type: %T", t0);
  1276  	return S;
  1277  }
  1278  
  1279  Node*
  1280  methodname(Node *n, Type *t)
  1281  {
  1282  	Sym *s;
  1283  
  1284  	s = methodsym(n->sym, t, 0);
  1285  	if(s == S)
  1286  		return n;
  1287  	return newname(s);
  1288  }
  1289  
  1290  Node*
  1291  methodname1(Node *n, Node *t)
  1292  {
  1293  	char *star;
  1294  	char *p;
  1295  
  1296  	star = nil;
  1297  	if(t->op == OIND) {
  1298  		star = "*";
  1299  		t = t->left;
  1300  	}
  1301  	if(t->sym == S || isblank(n))
  1302  		return newname(n->sym);
  1303  
  1304  	if(star)
  1305  		p = smprint("(%s%S).%S", star, t->sym, n->sym);
  1306  	else
  1307  		p = smprint("%S.%S", t->sym, n->sym);
  1308  
  1309  	if(exportname(t->sym->name))
  1310  		n = newname(lookup(p));
  1311  	else
  1312  		n = newname(pkglookup(p, t->sym->pkg));
  1313  	free(p);
  1314  	return n;
  1315  }
  1316  
  1317  /*
  1318   * add a method, declared as a function,
  1319   * n is fieldname, pa is base type, t is function type
  1320   */
  1321  void
  1322  addmethod(Sym *sf, Type *t, int local, int nointerface)
  1323  {
  1324  	Type *f, *d, *pa;
  1325  	Node *n;
  1326  
  1327  	// get field sym
  1328  	if(sf == S)
  1329  		fatal("no method symbol");
  1330  
  1331  	// get parent type sym
  1332  	pa = getthisx(t)->type;	// ptr to this structure
  1333  	if(pa == T) {
  1334  		yyerror("missing receiver");
  1335  		return;
  1336  	}
  1337  
  1338  	pa = pa->type;
  1339  	f = methtype(pa, 1);
  1340  	if(f == T) {
  1341  		t = pa;
  1342  		if(t == T) // rely on typecheck having complained before
  1343  			return;
  1344  		if(t != T) {
  1345  			if(isptr[t->etype]) {
  1346  				if(t->sym != S) {
  1347  					yyerror("invalid receiver type %T (%T is a pointer type)", pa, t);
  1348  					return;
  1349  				}
  1350  				t = t->type;
  1351  			}
  1352  			if(t->broke) // rely on typecheck having complained before
  1353  				return;
  1354  			if(t->sym == S) {
  1355  				yyerror("invalid receiver type %T (%T is an unnamed type)", pa, t);
  1356  				return;
  1357  			}
  1358  			if(isptr[t->etype]) {
  1359  				yyerror("invalid receiver type %T (%T is a pointer type)", pa, t);
  1360  				return;
  1361  			}
  1362  			if(t->etype == TINTER) {
  1363  				yyerror("invalid receiver type %T (%T is an interface type)", pa, t);
  1364  				return;
  1365  			}
  1366  		}
  1367  		// Should have picked off all the reasons above,
  1368  		// but just in case, fall back to generic error.
  1369  		yyerror("invalid receiver type %T (%lT / %lT)", pa, pa, t);
  1370  		return;
  1371  	}
  1372  
  1373  	pa = f;
  1374  	if(pa->etype == TSTRUCT) {
  1375  		for(f=pa->type; f; f=f->down) {
  1376  			if(f->sym == sf) {
  1377  				yyerror("type %T has both field and method named %S", pa, sf);
  1378  				return;
  1379  			}
  1380  		}
  1381  	}
  1382  
  1383  	if(local && !pa->local) {
  1384  		// defining method on non-local type.
  1385  		yyerror("cannot define new methods on non-local type %T", pa);
  1386  		return;
  1387  	}
  1388  
  1389  	n = nod(ODCLFIELD, newname(sf), N);
  1390  	n->type = t;
  1391  
  1392  	d = T;	// last found
  1393  	for(f=pa->method; f!=T; f=f->down) {
  1394  		d = f;
  1395  		if(f->etype != TFIELD)
  1396  			fatal("addmethod: not TFIELD: %N", f);
  1397  		if(strcmp(sf->name, f->sym->name) != 0)
  1398  			continue;
  1399  		if(!eqtype(t, f->type))
  1400  			yyerror("method redeclared: %T.%S\n\t%T\n\t%T", pa, sf, f->type, t);
  1401  		return;
  1402  	}
  1403  
  1404  	f = structfield(n);
  1405  	f->nointerface = nointerface;
  1406  
  1407  	// during import unexported method names should be in the type's package
  1408  	if(importpkg && f->sym && !exportname(f->sym->name) && f->sym->pkg != structpkg)
  1409  		fatal("imported method name %+S in wrong package %s\n", f->sym, structpkg->name);
  1410  
  1411  	if(d == T)
  1412  		pa->method = f;
  1413  	else
  1414  		d->down = f;
  1415  	return;
  1416  }
  1417  
  1418  void
  1419  funccompile(Node *n, int isclosure)
  1420  {
  1421  	stksize = BADWIDTH;
  1422  	maxarg = 0;
  1423  
  1424  	if(n->type == T) {
  1425  		if(nerrors == 0)
  1426  			fatal("funccompile missing type");
  1427  		return;
  1428  	}
  1429  
  1430  	// assign parameter offsets
  1431  	checkwidth(n->type);
  1432  	
  1433  	// record offset to actual frame pointer.
  1434  	// for closure, have to skip over leading pointers and PC slot.
  1435  	nodfp->xoffset = 0;
  1436  	if(isclosure) {
  1437  		NodeList *l;
  1438  		for(l=n->nname->ntype->list; l; l=l->next) {
  1439  			nodfp->xoffset += widthptr;
  1440  			if(l->n->left == N)	// found slot for PC
  1441  				break;
  1442  		}
  1443  	}
  1444  
  1445  	if(curfn)
  1446  		fatal("funccompile %S inside %S", n->nname->sym, curfn->nname->sym);
  1447  
  1448  	stksize = 0;
  1449  	dclcontext = PAUTO;
  1450  	funcdepth = n->funcdepth + 1;
  1451  	compile(n);
  1452  	curfn = nil;
  1453  	funcdepth = 0;
  1454  	dclcontext = PEXTERN;
  1455  }
  1456  
  1457  Sym*
  1458  funcsym(Sym *s)
  1459  {
  1460  	char *p;
  1461  	Sym *s1;
  1462  	
  1463  	p = smprint("%s·f", s->name);
  1464  	s1 = pkglookup(p, s->pkg);
  1465  	free(p);
  1466  	if(s1->def == N) {
  1467  		s1->def = newname(s1);
  1468  		s1->def->shortname = newname(s);
  1469  		funcsyms = list(funcsyms, s1->def);
  1470  	}
  1471  	return s1;
  1472  }