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