Commit aa347c4a authored by Russ Cox's avatar Russ Cox

move various bits of code around

and delete some dead code.
no actual changes here.

R=ken
OCL=32764
CL=32764
parent d169dcee
...@@ -15,28 +15,30 @@ YFILES=\ ...@@ -15,28 +15,30 @@ YFILES=\
go.y\ go.y\
OFILES=\ OFILES=\
reflect.$O\ align.$O\
y.tab.$O\ bits.$O\
lex.$O\ builtin.$O\
subr.$O\ compat.$O\
const.$O\
dcl.$O\ dcl.$O\
sinit.$O\
export.$O\ export.$O\
walk.$O\ gen.$O\
swt.$O\ init.$O\
const.$O\ lex.$O\
mparith1.$O\ mparith1.$O\
mparith2.$O\ mparith2.$O\
mparith3.$O\ mparith3.$O\
builtin.$O\
compat.$O\
bits.$O\
align.$O\
gen.$O\
obj.$O\ obj.$O\
print.$O\ print.$O\
typecheck.$O\ reflect.$O\
select.$O\ select.$O\
sinit.$O\
subr.$O\
swt.$O\
typecheck.$O\
unsafe.$O\
walk.$O\
y.tab.$O\
$(LIB): $(OFILES) $(LIB): $(OFILES)
ar rsc $(LIB) $(OFILES) ar rsc $(LIB) $(OFILES)
......
...@@ -18,1107 +18,557 @@ dflag(void) ...@@ -18,1107 +18,557 @@ dflag(void)
} }
/* /*
* declare (possible list) n of type t. * declaration stack & operations
* append ODCL nodes to *init
*/ */
static Sym* dclstack;
void void
dodclvar(Node *n, Type *t, NodeList **init) dcopy(Sym *a, Sym *b)
{ {
if(n == N) a->name = b->name;
return; a->def = b->def;
a->package = b->package;
if(t != T && (t->etype == TIDEAL || t->etype == TNIL)) a->undef = b->undef;
fatal("dodclvar %T", t); a->vargen = b->vargen;
dowidth(t); a->block = b->block;
a->lastlineno = b->lastlineno;
a->offset = b->offset;
}
// in case of type checking error, Sym*
// use "undefined" type for variable type, push(void)
// to avoid fatal in addvar. {
if(t == T) Sym *d;
t = typ(TFORW);
addvar(n, t, dclcontext); d = mal(sizeof(*d));
autoexport(n->sym); d->link = dclstack;
if(funcdepth > 0) dclstack = d;
*init = list(*init, nod(ODCL, n, N)); return d;
} }
// TODO(rsc): cut Sym*
void pushdcl(Sym *s)
dodclconst(Node *n, Node *e)
{ {
if(n == N) Sym *d;
return;
addconst(n, e, dclcontext); d = push();
autoexport(n->sym); dcopy(d, s);
return d;
} }
/* void
* introduce a type named n popdcl(void)
* but it is an unknown type for now
*/
Type*
dodcltype(Type *n)
{ {
Sym *s; Sym *d, *s;
// if n has been forward declared, // if(dflag())
// use the Type* created then // print("revert\n");
s = n->sym;
if((funcdepth == 0 || s->block == block) && s->def != N && s->def->op == OTYPE) { for(d=dclstack; d!=S; d=d->link) {
switch(s->def->type->etype) { if(d->name == nil)
case TFORWSTRUCT: break;
case TFORWINTER: s = pkglookup(d->name, d->package);
n = s->def->type; dcopy(s, d);
if(s->block != block) { if(dflag())
// completing forward struct from other file print("\t%L pop %S\n", lineno, s);
Dcl *d, *r;
d = dcl();
d->dsym = s;
d->dtype = n;
d->op = OTYPE;
r = externdcl;
d->back = r->back;
r->back->forw = d;
r->back = d;
}
goto found;
}
} }
if(d == S)
fatal("popdcl: no mark");
dclstack = d->link;
block = d->block;
}
// otherwise declare a new type void
addtyp(n, dclcontext); poptodcl(void)
{
Sym *d, *s;
found: for(d=dclstack; d!=S; d=d->link) {
n->local = 1; if(d->name == nil)
autoexport(n->sym); break;
return n; s = pkglookup(d->name, d->package);
dcopy(s, d);
if(dflag())
print("\t%L pop %S\n", lineno, s);
}
if(d == S)
fatal("poptodcl: no mark");
dclstack = d;
} }
/*
* now we know what n is: it's t
*/
void void
updatetype(Type *n, Type *t) markdcl(void)
{ {
Sym *s; Sym *d;
int local, vargen;
int maplineno, lno, etype;
if(t == T) d = push();
return; d->name = nil; // used as a mark in fifo
s = n->sym; d->block = block;
if(s == S || s->def == N || s->def->op != OTYPE || s->def->type != n)
fatal("updatetype %T = %T", n, t);
etype = n->etype; blockgen++;
switch(n->etype) { block = blockgen;
case TFORW:
break;
case TFORWSTRUCT: // if(dflag())
if(t->etype != TSTRUCT) { // print("markdcl\n");
yyerror("%T forward declared as struct", n); }
return;
}
n->local = 1;
break;
case TFORWINTER: void
if(t->etype != TINTER) { dumpdcl(char *st)
yyerror("%T forward declared as interface", n); {
return; Sym *s, *d;
} int i;
break;
default: i = 0;
fatal("updatetype %T / %T", n, t); for(d=dclstack; d!=S; d=d->link) {
i++;
print(" %.2d %p", i, d);
if(d->name == nil) {
print("\n");
continue;
}
print(" '%s'", d->name);
s = pkglookup(d->name, d->package);
print(" %lS\n", s);
} }
}
// decl was void
// type n t; testdclstack(void)
// copy t, but then zero out state associated with t {
// that is no longer associated with n. Sym *d;
maplineno = n->maplineno;
local = n->local;
vargen = n->vargen;
*n = *t;
n->sym = s;
n->local = local;
n->siggen = 0;
n->printed = 0;
n->method = nil;
n->vargen = vargen;
n->nod = N;
// catch declaration of incomplete type for(d=dclstack; d!=S; d=d->link) {
switch(n->etype) { if(d->name == nil) {
case TFORWSTRUCT: yyerror("mark left on the stack");
case TFORWINTER: continue;
break;
default:
checkwidth(n);
} }
// double-check use of type as map key
if(maplineno) {
lno = lineno;
lineno = maplineno;
maptype(n, types[TBOOL]);
lineno = lno;
} }
} }
/* /*
* return nelem of list * declare individual names - var, typ, const
*/ */
int static void
structcount(Type *t) redeclare(char *str, Sym *s)
{ {
int v; if(s->block == block) {
Iter s; yyerror("%s %S redeclared in this block", str, s);
print(" previous declaration at %L\n", s->lastlineno);
v = 0; }
for(t = structfirst(&s, &t); t != T; t = structnext(&s)) s->block = block;
v++; s->lastlineno = lineno;
return v;
} }
/* void
* turn a parsed function declaration addvar(Node *n, Type *t, int ctxt)
* into a type
*/
Type*
functype(Node *this, NodeList *in, NodeList *out)
{ {
Type *t; Dcl *r, *d;
NodeList *rcvr; Sym *s;
int gen;
t = typ(TFUNC);
rcvr = nil; if(n==N || n->sym == S || (n->op != ONAME && n->op != ONONAME) || t == T)
if(this) fatal("addvar: n=%N t=%T nil", n, t);
rcvr = list1(this);
t->type = dostruct(rcvr, TFUNC);
t->type->down = dostruct(out, TFUNC);
t->type->down->down = dostruct(in, TFUNC);
if(this) s = n->sym;
t->thistuple = 1;
t->outtuple = count(out);
t->intuple = count(in);
checkwidth(t); if(ctxt == PEXTERN || ctxt == PFUNC) {
return t; r = externdcl;
} gen = 0;
} else {
r = autodcl;
vargen++;
gen = vargen;
pushdcl(s);
}
int redeclare("variable", s);
methcmp(Type *t1, Type *t2) n->op = ONAME;
{ s->vargen = gen;
if(t1->etype != TFUNC) s->def = n;
return 0; s->offset = 0;
if(t2->etype != TFUNC)
return 0;
t1 = t1->type->down; // skip this arg n->funcdepth = funcdepth;
t2 = t2->type->down; // skip this arg n->type = t;
for(;;) { n->vargen = gen;
if(t1 == t2) n->class = ctxt;
break;
if(t1 == T || t2 == T)
return 0;
if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
return 0;
if(!eqtype(t1->type, t2->type)) d = dcl();
return 0; d->dsym = s;
d->dnode = n;
d->op = ONAME;
t1 = t1->down; r->back->forw = d;
t2 = t2->down; r->back = d;
if(dflag()) {
if(ctxt == PEXTERN)
print("extern var-dcl %S G%ld %T\n", s, s->vargen, t);
else if(ctxt == PFUNC)
print("extern func-dcl %S G%ld %T\n", s, s->vargen, t);
else
print("auto var-dcl %S G%ld %T\n", s, s->vargen, t);
} }
return 1;
} }
Sym* void
methodsym(Sym *nsym, Type *t0) addtyp(Type *n, int ctxt)
{ {
Dcl *r, *d;
Sym *s; Sym *s;
char buf[NSYMB]; static int typgen;
Type *t;
t = t0; if(n==T || n->sym == S)
if(t == T) fatal("addtyp: n=%T t=%T nil", n);
goto bad;
s = t->sym; s = n->sym;
if(s == S) {
if(!isptr[t->etype]) if(ctxt == PEXTERN)
goto bad; r = externdcl;
t = t->type; else {
if(t == T) r = autodcl;
goto bad; pushdcl(s);
s = t->sym; n->vargen = ++typgen;
if(s == S)
goto bad;
} }
// if t0 == *t and t0 has a sym, redeclare("type", s);
// we want to see *t, not t0, in the method name. s->def = typenod(n);
if(t != t0 && t0->sym)
t0 = ptrto(t);
snprint(buf, sizeof(buf), "%#hT·%s", t0, nsym->name); d = dcl();
//print("methodname %s\n", buf); d->dsym = s;
return pkglookup(buf, s->package); d->dtype = n;
d->op = OTYPE;
bad: d->back = r->back;
yyerror("illegal <this> type: %T", t); r->back->forw = d;
return S; r->back = d;
}
Node* d = dcl();
methodname(Node *n, Type *t) d->dtype = n;
{ d->op = OTYPE;
Sym *s;
s = methodsym(n->sym, t); r = typelist;
if(s == S) d->back = r->back;
return n; r->back->forw = d;
return newname(s); r->back = d;
if(dflag()) {
if(ctxt == PEXTERN)
print("extern typ-dcl %S G%ld %T\n", s, s->vargen, n);
else
print("auto typ-dcl %S G%ld %T\n", s, s->vargen, n);
}
} }
/* // TODO(rsc): cut
* add a method, declared as a function,
* n is fieldname, pa is base type, t is function type
*/
void void
addmethod(Node *n, Type *t, int local) addconst(Node *n, Node *e, int ctxt)
{ {
Type *f, *d, *pa; Sym *s;
Sym *sf; Dcl *r, *d;
pa = nil;
sf = nil;
// get field sym
if(n == N)
goto bad;
if(n->op != ONAME)
goto bad;
sf = n->sym;
if(sf == S)
goto bad;
// get parent type sym
pa = *getthis(t); // ptr to this structure
if(pa == T)
goto bad;
pa = pa->type; // ptr to this field
if(pa == T)
goto bad;
pa = pa->type; // ptr to this type
if(pa == T)
goto bad;
f = methtype(pa);
if(f == T)
goto bad;
pa = f;
if(pkgimportname != S && !exportname(sf->name))
sf = pkglookup(sf->name, pkgimportname->name);
n = nod(ODCLFIELD, newname(sf), N); if(n->op != ONAME && n->op != ONONAME)
n->type = t; fatal("addconst: not a name");
d = T; // last found if(e->op != OLITERAL) {
for(f=pa->method; f!=T; f=f->down) { yyerror("expression must be a constant");
d = f;
if(f->etype != TFIELD)
fatal("addmethod: not TFIELD: %N", f);
if(strcmp(sf->name, f->sym->name) != 0)
continue;
if(!eqtype(t, f->type)) {
yyerror("method redeclared: %T.%S", pa, sf);
print("\t%T\n\t%T\n", f->type, t);
}
return; return;
} }
if(local && !pa->local) { s = n->sym;
// defining method on non-local type.
// method must have been forward declared if(ctxt == PEXTERN)
// elsewhere, i.e. where the type was. r = externdcl;
yyerror("cannot define new methods on non-local type %T", pa); else {
return; r = autodcl;
pushdcl(s);
} }
if(d == T) redeclare("constant", s);
stotype(list1(n), 0, &pa->method); s->def = e;
else e->sym = s;
stotype(list1(n), 0, &d->down);
if(dflag()) d = dcl();
print("method %S of type %T\n", sf, pa); d->dsym = s;
return; d->dnode = e;
d->op = OLITERAL;
d->back = r->back;
r->back->forw = d;
r->back = d;
bad: if(dflag())
yyerror("invalid receiver type %T", pa); print("const-dcl %S %N\n", n->sym, n->sym->def);
} }
/* /*
* a function named init is a special case. * declare (possible list) n of type t.
* it is called by the initialization before * append ODCL nodes to *init
* main is run. to make it unique within a
* package and also uncallable, the name,
* normally "pkg.init", is altered to "pkg.init·filename".
*/ */
Node* void
renameinit(Node *n) dodclvar(Node *n, Type *t, NodeList **init)
{ {
Sym *s; if(n == N)
return;
s = n->sym; if(t != T && (t->etype == TIDEAL || t->etype == TNIL))
if(s == S) fatal("dodclvar %T", t);
return n; dowidth(t);
if(strcmp(s->name, "init") != 0)
return n;
snprint(namebuf, sizeof(namebuf), "init·%s", filename); // in case of type checking error,
s = lookup(namebuf); // use "undefined" type for variable type,
return newname(s); // to avoid fatal in addvar.
if(t == T)
t = typ(TFORW);
addvar(n, t, dclcontext);
autoexport(n->sym);
if(funcdepth > 0)
*init = list(*init, nod(ODCL, n, N));
} }
/* // TODO(rsc): cut
* declare the function proper.
* and declare the arguments
* called in extern-declaration context
* returns in auto-declaration context.
*/
void void
funchdr(Node *n) dodclconst(Node *n, Node *e)
{ {
Node *on; if(n == N)
Sym *s; return;
addconst(n, e, dclcontext);
autoexport(n->sym);
}
s = n->nname->sym; /*
on = s->def; * introduce a type named n
if(on != N && (on->op != ONAME || on->builtin)) * but it is an unknown type for now
on = N; */
Type*
dodcltype(Type *n)
{
Sym *s;
// check for same types // if n has been forward declared,
if(on != N) { // use the Type* created then
if(eqtype(n->type, on->type)) { s = n->sym;
if(!eqargs(n->type, on->type)) { if((funcdepth == 0 || s->block == block) && s->def != N && s->def->op == OTYPE) {
yyerror("function arg names changed: %S", s); switch(s->def->type->etype) {
print("\t%T\n\t%T\n", on->type, n->type); case TFORWSTRUCT:
} case TFORWINTER:
} else { n = s->def->type;
yyerror("function redeclared: %S", s); if(s->block != block) {
print("\t%T\n\t%T\n", on->type, n->type); // completing forward struct from other file
on = N; Dcl *d, *r;
d = dcl();
d->dsym = s;
d->dtype = n;
d->op = OTYPE;
r = externdcl;
d->back = r->back;
r->back->forw = d;
r->back = d;
} }
goto found;
} }
// check for forward declaration
if(on == N) {
// initial declaration or redeclaration
// declare fun name, argument types and argument names
n->nname->type = n->type;
if(n->type->thistuple == 0)
addvar(n->nname, n->type, PFUNC);
else
n->nname->class = PFUNC;
} else {
// identical redeclaration
// steal previous names
n->nname = on;
n->type = on->type;
n->class = on->class;
n->sym = s;
if(dflag())
print("forew var-dcl %S %T\n", n->sym, n->type);
} }
// change the declaration context from extern to auto // otherwise declare a new type
autodcl = dcl(); addtyp(n, dclcontext);
autodcl->back = autodcl;
if(funcdepth == 0 && dclcontext != PEXTERN)
fatal("funchdr: dclcontext");
dclcontext = PAUTO; found:
markdcl(); n->local = 1;
funcargs(n->type); autoexport(n->sym);
return n;
} }
/*
* now we know what n is: it's t
*/
void void
funcargs(Type *ft) updatetype(Type *n, Type *t)
{ {
Type *t; Sym *s;
Iter save; int local, vargen;
int all; int maplineno, lno, etype;
funcdepth++; if(t == T)
return;
s = n->sym;
if(s == S || s->def == N || s->def->op != OTYPE || s->def->type != n)
fatal("updatetype %T = %T", n, t);
// declare the this/in arguments etype = n->etype;
t = funcfirst(&save, ft); switch(n->etype) {
while(t != T) { case TFORW:
if(t->nname != N) { break;
t->nname->xoffset = t->width;
addvar(t->nname, t->type, PPARAM); case TFORWSTRUCT:
if(t->etype != TSTRUCT) {
yyerror("%T forward declared as struct", n);
return;
} }
t = funcnext(&save); n->local = 1;
break;
case TFORWINTER:
if(t->etype != TINTER) {
yyerror("%T forward declared as interface", n);
return;
} }
break;
// declare the outgoing arguments default:
all = 0; fatal("updatetype %T / %T", n, t);
t = structfirst(&save, getoutarg(ft));
while(t != T) {
if(t->nname != N)
t->nname->xoffset = t->width;
if(t->nname != N) {
addvar(t->nname, t->type, PPARAMOUT);
all |= 1;
} else
all |= 2;
t = structnext(&save);
} }
// this test is remarkedly similar to checkarglist // decl was
if(all == 3) // type n t;
yyerror("cannot mix anonymous and named output arguments"); // copy t, but then zero out state associated with t
// that is no longer associated with n.
maplineno = n->maplineno;
local = n->local;
vargen = n->vargen;
*n = *t;
n->sym = s;
n->local = local;
n->siggen = 0;
n->printed = 0;
n->method = nil;
n->vargen = vargen;
n->nod = N;
ft->outnamed = 0; // catch declaration of incomplete type
if(all == 1) switch(n->etype) {
ft->outnamed = 1; case TFORWSTRUCT:
case TFORWINTER:
break;
default:
checkwidth(n);
}
// double-check use of type as map key
if(maplineno) {
lno = lineno;
lineno = maplineno;
maptype(n, types[TBOOL]);
lineno = lno;
}
} }
/* /*
* compile the function. * declare variables from grammar
* called in auto-declaration context. * new_name_list (type | [type] = expr_list)
* returns in extern-declaration context.
*/ */
void NodeList*
funcbody(Node *n) variter(NodeList *vl, Node *t, NodeList *el)
{ {
int doexpr, gen;
Node *v, *e;
NodeList *init;
Sym *s;
Dcl *r, *d;
compile(n); init = nil;
doexpr = el != nil;
for(; vl; vl=vl->next) {
if(doexpr) {
if(el == nil) {
yyerror("missing expr in var dcl");
break;
}
e = el->n;
el = el->next;
} else
e = N;
// change the declaration context from auto to extern v = vl->n;
if(dclcontext != PAUTO) s = v->sym;
fatal("funcbody: dclcontext"); if(dclcontext == PEXTERN || dclcontext == PFUNC) {
popdcl(); r = externdcl;
funcdepth--; gen = 0;
if(funcdepth == 0) } else {
dclcontext = PEXTERN; r = autodcl;
} gen = ++vargen;
pushdcl(s);
}
Node* redeclare("variable", s);
funclit0(Node *t) s->def = v;
{ // TODO: vargen
Node *n; s->offset = 0;
s->block = block;
n = nod(OXXX, N, N); v->op = ONAME;
n->outer = funclit; v->class = dclcontext;
n->dcl = autodcl; v->ntype = t;
funclit = n; v->funcdepth = funcdepth;
v->vargen = gen;
if(e != N || funcdepth > 0) {
if(funcdepth > 0)
init = list(init, nod(ODCL, v, N));
e = nod(OAS, v, e);
init = list(init, e);
if(e->right != N)
v->defn = e;
}
// new declaration context d = dcl();
autodcl = dcl(); d->dsym = s;
autodcl->back = autodcl; d->dnode = v;
d->op = ONAME;
r->back->forw = d;
r->back = d;
typecheck(&t, Etype); autoexport(s);
funcargs(t->type);
return t;
}
Node*
funclit1(Node *ntype, NodeList *body)
{
Node *func;
Type *type;
Node *a, *d, *f, *n, *clos;
Type *ft, *t;
Iter save;
int narg, shift;
NodeList *args, *l, *in, *out;
type = ntype->type;
popdcl();
func = funclit;
funclit = func->outer;
// build up type of func f that we're going to compile.
// as we referred to variables from the outer function,
// we accumulated a list of PHEAP names in func->cvars.
narg = 0;
// add PHEAP versions as function arguments.
in = nil;
for(l=func->cvars; l; l=l->next) {
a = l->n;
d = nod(ODCLFIELD, a, N);
d->type = ptrto(a->type);
in = list(in, d);
// while we're here, set up a->heapaddr for back end
n = nod(ONAME, N, N);
snprint(namebuf, sizeof namebuf, "&%s", a->sym->name);
n->sym = lookup(namebuf);
n->type = ptrto(a->type);
n->class = PPARAM;
n->xoffset = narg*types[tptr]->width;
n->addable = 1;
n->ullman = 1;
narg++;
a->heapaddr = n;
a->xoffset = 0;
// unlink from actual ONAME in symbol table
a->closure->closure = a->outer;
}
// add a dummy arg for the closure's caller pc
d = nod(ODCLFIELD, N, N);
d->type = types[TUINTPTR];
in = list(in, d);
// slide param offset to make room for ptrs above.
// narg+1 to skip over caller pc.
shift = (narg+1)*types[tptr]->width;
// now the original arguments.
for(t=structfirst(&save, getinarg(type)); t; t=structnext(&save)) {
d = nod(ODCLFIELD, t->nname, N);
d->type = t->type;
in = list(in, d);
a = t->nname;
if(a != N) {
if(a->stackparam != N)
a = a->stackparam;
a->xoffset += shift;
}
}
// out arguments
out = nil;
for(t=structfirst(&save, getoutarg(type)); t; t=structnext(&save)) {
d = nod(ODCLFIELD, t->nname, N);
d->type = t->type;
out = list(out, d);
a = t->nname;
if(a != N) {
if(a->stackparam != N)
a = a->stackparam;
a->xoffset += shift;
}
}
ft = functype(N, in, out);
ft->outnamed = type->outnamed;
// declare function.
vargen++;
snprint(namebuf, sizeof(namebuf), "_f%.3ld·%s", vargen, filename);
f = newname(lookup(namebuf));
addvar(f, ft, PFUNC);
f->funcdepth = 0;
// compile function
n = nod(ODCLFUNC, N, N);
n->nname = f;
n->type = ft;
if(body == nil)
body = list1(nod(OEMPTY, N, N));
n->nbody = body;
compile(n);
funcdepth--;
autodcl = func->dcl;
// build up type for this instance of the closure func.
in = nil;
d = nod(ODCLFIELD, N, N); // siz
d->type = types[TINT];
in = list(in, d);
d = nod(ODCLFIELD, N, N); // f
d->type = ft;
in = list(in, d);
for(l=func->cvars; l; l=l->next) {
a = l->n;
d = nod(ODCLFIELD, N, N); // arg
d->type = ptrto(a->type);
in = list(in, d);
}
d = nod(ODCLFIELD, N, N);
d->type = type;
out = list1(d);
clos = syslook("closure", 1);
clos->type = functype(N, in, out);
// literal expression is sys.closure(siz, f, arg0, arg1, ...)
// which builds a function that calls f after filling in arg0,
// arg1, ... for the PHEAP arguments above.
args = nil;
if(narg*widthptr > 100)
yyerror("closure needs too many variables; runtime will reject it");
a = nodintconst(narg*widthptr);
args = list(args, a); // siz
args = list(args, f); // f
for(l=func->cvars; l; l=l->next) {
a = l->n;
d = oldname(a->sym);
args = list(args, nod(OADDR, d, N));
} }
typechecklist(args, Erv); if(el != nil)
yyerror("extra expr in var dcl");
n = nod(OCALL, clos, N); return init;
n->list = args;
return n;
} }
/* /*
* turn a parsed struct into a type * declare constants from grammar
* new_name_list [[type] = expr_list]
*/ */
Type** NodeList*
stotype(NodeList *l, int et, Type **t) constiter(NodeList *vl, Node *t, NodeList *cl)
{ {
Type *f, *t1; Node *v, *c;
Strlit *note; NodeList *vv;
int lno; Sym *s;
NodeList *init;
Node *n;
init = nil;
lno = lineno;
for(; l; l=l->next) {
n = l->n;
lineno = n->lineno;
note = nil;
if(n->op != ODCLFIELD)
fatal("stotype: oops %N\n", n);
if(n->right != N) {
typecheck(&n->right, Etype);
n->type = n->right->type;
n->right = N;
if(n->embedded && n->type != T) {
t1 = n->type;
if(t1->sym == S && isptr[t1->etype])
t1 = t1->type;
if(t1 != T && isptr[t1->etype])
yyerror("embedded type cannot be a pointer");
}
}
if(n->type == T) { vv = vl;
// assume error already printed if(cl == nil) {
continue; if(t != N)
yyerror("constdcl cannot have type without expr");
cl = lastconst;
t = lasttype;
} else {
lastconst = cl;
lasttype = t;
} }
cl = listtreecopy(cl);
switch(n->val.ctype) { for(; vl; vl=vl->next) {
case CTSTR: if(cl == nil) {
if(et != TSTRUCT) yyerror("missing expr in const dcl");
yyerror("interface method cannot have annotation");
note = n->val.u.sval;
break;
default:
if(et != TSTRUCT)
yyerror("interface method cannot have annotation");
else
yyerror("field annotation must be string");
case CTxxx:
note = nil;
break; break;
} }
c = cl->n;
cl = cl->next;
if(et == TINTER && n->left == N) { v = vl->n;
// embedded interface - inline the methods s = v->sym;
if(n->type->etype != TINTER) { if(dclcontext != PEXTERN)
yyerror("interface contains embedded non-interface %T", t); pushdcl(s);
continue; redeclare("constant", s);
} s->def = v;
for(t1=n->type->type; t1!=T; t1=t1->down) {
// TODO(rsc): Is this really an error? v->op = OLITERAL;
if(strcmp(t1->sym->package, package) != 0) v->ntype = t;
yyerror("embedded interface contains unexported method %S", t1->sym); v->defn = c;
f = typ(TFIELD); autoexport(s);
f->type = t1->type; }
f->width = BADWIDTH; if(cl != nil)
f->nname = newname(t1->sym); yyerror("extra expr in const dcl");
f->sym = t1->sym; iota += 1;
*t = f; return vv;
t = &f->down;
}
continue;
}
f = typ(TFIELD);
f->type = n->type;
f->note = note;
f->width = BADWIDTH;
if(n->left != N && n->left->op == ONAME) {
f->nname = n->left;
f->embedded = n->embedded;
f->sym = f->nname->sym;
if(pkgimportname != S && !exportname(f->sym->name))
f->sym = pkglookup(f->sym->name, structpkg);
}
*t = f;
t = &f->down;
}
*t = T;
lineno = lno;
return t;
}
Type*
dostruct(NodeList *l, int et)
{
Type *t;
int funarg;
/*
* convert a parsed id/type list into
* a type for struct/interface/arglist
*/
funarg = 0;
if(et == TFUNC) {
funarg = 1;
et = TSTRUCT;
}
t = typ(et);
t->funarg = funarg;
stotype(l, et, &t->type);
if(!funarg)
checkwidth(t);
return t;
}
Type*
sortinter(Type *t)
{
return t;
}
void
dcopy(Sym *a, Sym *b)
{
a->name = b->name;
a->def = b->def;
a->package = b->package;
a->undef = b->undef;
a->vargen = b->vargen;
a->block = b->block;
a->lastlineno = b->lastlineno;
a->offset = b->offset;
}
Sym*
push(void)
{
Sym *d;
d = mal(sizeof(*d));
d->link = dclstack;
dclstack = d;
return d;
}
Sym*
pushdcl(Sym *s)
{
Sym *d;
d = push();
dcopy(d, s);
return d;
}
void
popdcl(void)
{
Sym *d, *s;
// if(dflag())
// print("revert\n");
for(d=dclstack; d!=S; d=d->link) {
if(d->name == nil)
break;
s = pkglookup(d->name, d->package);
dcopy(s, d);
if(dflag())
print("\t%L pop %S\n", lineno, s);
}
if(d == S)
fatal("popdcl: no mark");
dclstack = d->link;
block = d->block;
}
void
poptodcl(void)
{
Sym *d, *s;
for(d=dclstack; d!=S; d=d->link) {
if(d->name == nil)
break;
s = pkglookup(d->name, d->package);
dcopy(s, d);
if(dflag())
print("\t%L pop %S\n", lineno, s);
}
if(d == S)
fatal("poptodcl: no mark");
dclstack = d;
}
void
markdcl(void)
{
Sym *d;
d = push();
d->name = nil; // used as a mark in fifo
d->block = block;
blockgen++;
block = blockgen;
// if(dflag())
// print("markdcl\n");
}
void
dumpdcl(char *st)
{
Sym *s, *d;
int i;
print("\ndumpdcl: %s %p\n", st, b0stack);
i = 0;
for(d=dclstack; d!=S; d=d->link) {
i++;
print(" %.2d %p", i, d);
if(d == b0stack)
print(" (b0)");
if(d->name == nil) {
print("\n");
continue;
}
print(" '%s'", d->name);
s = pkglookup(d->name, d->package);
print(" %lS\n", s);
}
}
void
testdclstack(void)
{
Sym *d;
for(d=dclstack; d!=S; d=d->link) {
if(d->name == nil) {
yyerror("mark left on the stack");
continue;
}
}
}
static void
redeclare(char *str, Sym *s)
{
if(s->block == block) {
yyerror("%s %S redeclared in this block", str, s);
print(" previous declaration at %L\n", s->lastlineno);
}
s->block = block;
s->lastlineno = lineno;
}
void
addvar(Node *n, Type *t, int ctxt)
{
Dcl *r, *d;
Sym *s;
int gen;
if(n==N || n->sym == S || (n->op != ONAME && n->op != ONONAME) || t == T)
fatal("addvar: n=%N t=%T nil", n, t);
s = n->sym;
if(ctxt == PEXTERN || ctxt == PFUNC) {
r = externdcl;
gen = 0;
} else {
r = autodcl;
vargen++;
gen = vargen;
pushdcl(s);
}
redeclare("variable", s);
n->op = ONAME;
s->vargen = gen;
s->def = n;
s->offset = 0;
n->funcdepth = funcdepth;
n->type = t;
n->vargen = gen;
n->class = ctxt;
d = dcl();
d->dsym = s;
d->dnode = n;
d->op = ONAME;
r->back->forw = d;
r->back = d;
if(dflag()) {
if(ctxt == PEXTERN)
print("extern var-dcl %S G%ld %T\n", s, s->vargen, t);
else if(ctxt == PFUNC)
print("extern func-dcl %S G%ld %T\n", s, s->vargen, t);
else
print("auto var-dcl %S G%ld %T\n", s, s->vargen, t);
}
}
void
addtyp(Type *n, int ctxt)
{
Dcl *r, *d;
Sym *s;
static int typgen;
if(n==T || n->sym == S)
fatal("addtyp: n=%T t=%T nil", n);
s = n->sym;
if(ctxt == PEXTERN)
r = externdcl;
else {
r = autodcl;
pushdcl(s);
n->vargen = ++typgen;
}
redeclare("type", s);
s->def = typenod(n);
d = dcl();
d->dsym = s;
d->dtype = n;
d->op = OTYPE;
d->back = r->back;
r->back->forw = d;
r->back = d;
d = dcl();
d->dtype = n;
d->op = OTYPE;
r = typelist;
d->back = r->back;
r->back->forw = d;
r->back = d;
if(dflag()) {
if(ctxt == PEXTERN)
print("extern typ-dcl %S G%ld %T\n", s, s->vargen, n);
else
print("auto typ-dcl %S G%ld %T\n", s, s->vargen, n);
}
}
// TODO(rsc): cut
void
addconst(Node *n, Node *e, int ctxt)
{
Sym *s;
Dcl *r, *d;
if(n->op != ONAME && n->op != ONONAME)
fatal("addconst: not a name");
if(e->op != OLITERAL) {
yyerror("expression must be a constant");
return;
}
s = n->sym;
if(ctxt == PEXTERN)
r = externdcl;
else {
r = autodcl;
pushdcl(s);
}
redeclare("constant", s);
s->def = e;
e->sym = s;
d = dcl();
d->dsym = s;
d->dnode = e;
d->op = OLITERAL;
d->back = r->back;
r->back->forw = d;
r->back = d;
if(dflag())
print("const-dcl %S %N\n", n->sym, n->sym->def);
}
Node*
fakethis(void)
{
Node *n;
n = nod(ODCLFIELD, N, N);
n->type = ptrto(typ(TSTRUCT));
return n;
}
/*
* Is this field a method on an interface?
* Those methods have an anonymous
* *struct{} as the receiver.
* (See fakethis above.)
*/
int
isifacemethod(Type *f)
{
Type *rcvr;
Type *t;
rcvr = getthisx(f->type)->type;
if(rcvr->sym != S)
return 0;
t = rcvr->type;
if(!isptr[t->etype])
return 0;
t = t->type;
if(t->sym != S || t->etype != TSTRUCT || t->type != T)
return 0;
return 1;
} }
/* /*
...@@ -1260,27 +710,172 @@ oldtype(Sym *s) ...@@ -1260,27 +710,172 @@ oldtype(Sym *s)
} }
/* /*
* n is a node with a name. * type check top level declarations
* make it a declaration of the given type.
*/ */
Node* void
nametodcl(Node *n, Type *t) dclchecks(void)
{ {
n = nod(ODCLFIELD, n, N); Dcl *d;
n->type = t;
return n; for(d=externdcl; d!=D; d=d->forw) {
if(d->op != ONAME)
continue;
typecheck(&d->dnode, Erv);
}
} }
/* /*
* make an anonymous declaration for t * structs, functions, and methods.
* they don't belong here, but where do they belong?
*/
/*
* turn a parsed struct into a type
*/
Type**
stotype(NodeList *l, int et, Type **t)
{
Type *f, *t1;
Strlit *note;
int lno;
NodeList *init;
Node *n;
init = nil;
lno = lineno;
for(; l; l=l->next) {
n = l->n;
lineno = n->lineno;
note = nil;
if(n->op != ODCLFIELD)
fatal("stotype: oops %N\n", n);
if(n->right != N) {
typecheck(&n->right, Etype);
n->type = n->right->type;
n->right = N;
if(n->embedded && n->type != T) {
t1 = n->type;
if(t1->sym == S && isptr[t1->etype])
t1 = t1->type;
if(t1 != T && isptr[t1->etype])
yyerror("embedded type cannot be a pointer");
}
}
if(n->type == T) {
// assume error already printed
continue;
}
switch(n->val.ctype) {
case CTSTR:
if(et != TSTRUCT)
yyerror("interface method cannot have annotation");
note = n->val.u.sval;
break;
default:
if(et != TSTRUCT)
yyerror("interface method cannot have annotation");
else
yyerror("field annotation must be string");
case CTxxx:
note = nil;
break;
}
if(et == TINTER && n->left == N) {
// embedded interface - inline the methods
if(n->type->etype != TINTER) {
yyerror("interface contains embedded non-interface %T", t);
continue;
}
for(t1=n->type->type; t1!=T; t1=t1->down) {
// TODO(rsc): Is this really an error?
if(strcmp(t1->sym->package, package) != 0)
yyerror("embedded interface contains unexported method %S", t1->sym);
f = typ(TFIELD);
f->type = t1->type;
f->width = BADWIDTH;
f->nname = newname(t1->sym);
f->sym = t1->sym;
*t = f;
t = &f->down;
}
continue;
}
f = typ(TFIELD);
f->type = n->type;
f->note = note;
f->width = BADWIDTH;
if(n->left != N && n->left->op == ONAME) {
f->nname = n->left;
f->embedded = n->embedded;
f->sym = f->nname->sym;
if(pkgimportname != S && !exportname(f->sym->name))
f->sym = pkglookup(f->sym->name, structpkg);
}
*t = f;
t = &f->down;
}
*t = T;
lineno = lno;
return t;
}
Type*
dostruct(NodeList *l, int et)
{
Type *t;
int funarg;
/*
* convert a parsed id/type list into
* a type for struct/interface/arglist
*/ */
funarg = 0;
if(et == TFUNC) {
funarg = 1;
et = TSTRUCT;
}
t = typ(et);
t->funarg = funarg;
stotype(l, et, &t->type);
if(!funarg)
checkwidth(t);
return t;
}
Node* Node*
anondcl(Type *t) embedded(Sym *s)
{ {
Node *n; Node *n;
char *name;
n = nod(ODCLFIELD, N, N); // Names sometimes have disambiguation junk
n->type = t; // appended after a center dot. Discard it when
// making the name for the embedded struct field.
enum { CenterDot = 0xB7 };
name = s->name;
if(utfrune(s->name, CenterDot)) {
name = strdup(s->name);
*utfrune(name, CenterDot) = 0;
}
n = newname(lookup(name));
n = nod(ODCLFIELD, n, N);
n->embedded = 1;
if(s == S)
return n;
n->right = oldname(s);
return n; return n;
} }
...@@ -1370,482 +965,505 @@ checkarglist(NodeList *all) ...@@ -1370,482 +965,505 @@ checkarglist(NodeList *all)
return all; return all;
} }
Node*
fakethis(void)
{
Node *n;
n = nod(ODCLFIELD, N, N);
n->type = ptrto(typ(TSTRUCT));
return n;
}
/* /*
* hand-craft the following initialization code * Is this field a method on an interface?
* var initdone·<file> uint8 (1) * Those methods have an anonymous
* func Init·<file>() (2) * *struct{} as the receiver.
* if initdone·<file> { (3) * (See fakethis above.)
* if initdone·<file> == 2 (4)
* return
* throw(); (5)
* }
* initdone.<file>++; (6)
* // over all matching imported symbols
* <pkg>.init·<file>() (7)
* { <init stmts> } (8)
* init·<file>() // if any (9)
* initdone.<file>++; (10)
* return (11)
* }
*/ */
int int
anyinit(NodeList *n) isifacemethod(Type *f)
{ {
uint32 h; Type *rcvr;
Sym *s; Type *t;
// are there any init statements rcvr = getthisx(f->type)->type;
if(n != nil) if(rcvr->sym != S)
return 0;
t = rcvr->type;
if(!isptr[t->etype])
return 0;
t = t->type;
if(t->sym != S || t->etype != TSTRUCT || t->type != T)
return 0;
return 1; return 1;
}
// is this main /*
if(strcmp(package, "main") == 0) * turn a parsed function declaration
return 1; * into a type
*/
Type*
functype(Node *this, NodeList *in, NodeList *out)
{
Type *t;
NodeList *rcvr;
t = typ(TFUNC);
rcvr = nil;
if(this)
rcvr = list1(this);
t->type = dostruct(rcvr, TFUNC);
t->type->down = dostruct(out, TFUNC);
t->type->down->down = dostruct(in, TFUNC);
if(this)
t->thistuple = 1;
t->outtuple = count(out);
t->intuple = count(in);
checkwidth(t);
return t;
}
int
methcmp(Type *t1, Type *t2)
{
if(t1->etype != TFUNC)
return 0;
if(t2->etype != TFUNC)
return 0;
t1 = t1->type->down; // skip this arg
t2 = t2->type->down; // skip this arg
for(;;) {
if(t1 == t2)
break;
if(t1 == T || t2 == T)
return 0;
if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
return 0;
// is there an explicit init function if(!eqtype(t1->type, t2->type))
snprint(namebuf, sizeof(namebuf), "init·%s", filename); return 0;
s = lookup(namebuf);
if(s->def != N)
return 1;
// are there any imported init functions t1 = t1->down;
for(h=0; h<NHASH; h++) t2 = t2->down;
for(s = hash[h]; s != S; s = s->link) {
if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0)
continue;
if(s->def == N)
continue;
return 1;
} }
return 1;
// then none
return 0;
} }
void Sym*
fninit(NodeList *n) methodsym(Sym *nsym, Type *t0)
{ {
Node *gatevar; Sym *s;
Node *a, *b, *fn; char buf[NSYMB];
NodeList *r; Type *t;
uint32 h;
Sym *s, *initsym; t = t0;
if(t == T)
if(strcmp(package, "PACKAGE") == 0) { goto bad;
// sys.go or unsafe.go during compiler build s = t->sym;
return; if(s == S) {
if(!isptr[t->etype])
goto bad;
t = t->type;
if(t == T)
goto bad;
s = t->sym;
if(s == S)
goto bad;
} }
if(!anyinit(n)) // if t0 == *t and t0 has a sym,
return; // we want to see *t, not t0, in the method name.
if(t != t0 && t0->sym)
t0 = ptrto(t);
r = nil; snprint(buf, sizeof(buf), "%#hT·%s", t0, nsym->name);
//print("methodname %s\n", buf);
return pkglookup(buf, s->package);
// (1) bad:
snprint(namebuf, sizeof(namebuf), "initdone·%s", filename); yyerror("illegal <this> type: %T", t);
gatevar = newname(lookup(namebuf)); return S;
addvar(gatevar, types[TUINT8], PEXTERN); }
// (2) Node*
methodname(Node *n, Type *t)
{
Sym *s;
maxarg = 0; s = methodsym(n->sym, t);
stksize = initstksize; if(s == S)
return n;
return newname(s);
}
snprint(namebuf, sizeof(namebuf), "Init·%s", filename); /*
* add a method, declared as a function,
* n is fieldname, pa is base type, t is function type
*/
void
addmethod(Node *n, Type *t, int local)
{
Type *f, *d, *pa;
Sym *sf;
// this is a botch since we need a known name to pa = nil;
// call the top level init function out of rt0 sf = nil;
if(strcmp(package, "main") == 0)
snprint(namebuf, sizeof(namebuf), "init");
fn = nod(ODCLFUNC, N, N); // get field sym
initsym = lookup(namebuf); if(n == N)
fn->nname = newname(initsym); goto bad;
fn->type = functype(N, nil, nil); if(n->op != ONAME)
funchdr(fn); goto bad;
sf = n->sym;
if(sf == S)
goto bad;
// (3) // get parent type sym
a = nod(OIF, N, N); pa = *getthis(t); // ptr to this structure
a->ntest = nod(ONE, gatevar, nodintconst(0)); if(pa == T)
r = list(r, a); goto bad;
pa = pa->type; // ptr to this field
if(pa == T)
goto bad;
pa = pa->type; // ptr to this type
if(pa == T)
goto bad;
// (4) f = methtype(pa);
b = nod(OIF, N, N); if(f == T)
b->ntest = nod(OEQ, gatevar, nodintconst(2)); goto bad;
b->nbody = list1(nod(ORETURN, N, N));
a->nbody = list1(b);
// (5) pa = f;
b = syslook("throwinit", 0); if(pkgimportname != S && !exportname(sf->name))
b = nod(OCALL, b, N); sf = pkglookup(sf->name, pkgimportname->name);
a->nbody = list(a->nbody, b);
// (6) n = nod(ODCLFIELD, newname(sf), N);
a = nod(OASOP, gatevar, nodintconst(1)); n->type = t;
a->etype = OADD;
r = list(r, a);
// (7) d = T; // last found
for(h=0; h<NHASH; h++) for(f=pa->method; f!=T; f=f->down) {
for(s = hash[h]; s != S; s = s->link) { d = f;
if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0) if(f->etype != TFIELD)
continue; fatal("addmethod: not TFIELD: %N", f);
if(s->def == N) if(strcmp(sf->name, f->sym->name) != 0)
continue;
if(s == initsym)
continue; continue;
if(!eqtype(t, f->type)) {
// could check that it is fn of no args/returns yyerror("method redeclared: %T.%S", pa, sf);
a = nod(OCALL, s->def, N); print("\t%T\n\t%T\n", f->type, t);
r = list(r, a);
} }
return;
// (8)
r = concat(r, initfix(n));
// (9)
// could check that it is fn of no args/returns
snprint(namebuf, sizeof(namebuf), "init·%s", filename);
s = lookup(namebuf);
if(s->def != N) {
a = nod(OCALL, s->def, N);
r = list(r, a);
} }
// (10) if(local && !pa->local) {
a = nod(OASOP, gatevar, nodintconst(1)); // defining method on non-local type.
a->etype = OADD; // method must have been forward declared
r = list(r, a); // elsewhere, i.e. where the type was.
yyerror("cannot define new methods on non-local type %T", pa);
// (11) return;
a = nod(ORETURN, N, N); }
r = list(r, a);
exportsym(fn->nname->sym);
fn->nbody = r; if(d == T)
//dump("b", fn); stotype(list1(n), 0, &pa->method);
//dump("r", fn->nbody); else
stotype(list1(n), 0, &d->down);
return;
popdcl(); bad:
initflag = 1; // flag for loader static initialization yyerror("invalid receiver type %T", pa);
compile(fn);
initflag = 0;
} }
/* /*
* when a type's width should be known, we call checkwidth * declare the function proper.
* to compute it. during a declaration like * and declare the arguments
* * called in extern-declaration context
* type T *struct { next T } * returns in auto-declaration context.
*
* it is necessary to defer the calculation of the struct width
* until after T has been initialized to be a pointer to that struct.
* similarly, during import processing structs may be used
* before their definition. in those situations, calling
* defercheckwidth() stops width calculations until
* resumecheckwidth() is called, at which point all the
* checkwidths that were deferred are executed.
* sometimes it is okay to
*/ */
typedef struct TypeList TypeList;
struct TypeList {
Type *t;
TypeList *next;
};
static TypeList *tlfree;
static TypeList *tlq;
static int defercalc;
void void
checkwidth(Type *t) funchdr(Node *n)
{ {
TypeList *l; Node *on;
Sym *s;
// function arg structs should not be checked s = n->nname->sym;
// outside of the enclosing function. on = s->def;
if(t->funarg) if(on != N && (on->op != ONAME || on->builtin))
fatal("checkwidth %T", t); on = N;
if(!defercalc) { // check for same types
dowidth(t); if(on != N) {
return; if(eqtype(n->type, on->type)) {
if(!eqargs(n->type, on->type)) {
yyerror("function arg names changed: %S", s);
print("\t%T\n\t%T\n", on->type, n->type);
}
} else {
yyerror("function redeclared: %S", s);
print("\t%T\n\t%T\n", on->type, n->type);
on = N;
}
} }
l = tlfree; // check for forward declaration
if(l != nil) if(on == N) {
tlfree = l->next; // initial declaration or redeclaration
// declare fun name, argument types and argument names
n->nname->type = n->type;
if(n->type->thistuple == 0)
addvar(n->nname, n->type, PFUNC);
else else
l = mal(sizeof *l); n->nname->class = PFUNC;
} else {
// identical redeclaration
// steal previous names
n->nname = on;
n->type = on->type;
n->class = on->class;
n->sym = s;
}
// change the declaration context from extern to auto
autodcl = dcl();
autodcl->back = autodcl;
if(funcdepth == 0 && dclcontext != PEXTERN)
fatal("funchdr: dclcontext");
l->t = t; dclcontext = PAUTO;
l->next = tlq; markdcl();
tlq = l; funcargs(n->type);
} }
void void
defercheckwidth(void) funcargs(Type *ft)
{ {
// we get out of sync on syntax errors, so don't be pedantic. Type *t;
// if(defercalc) Iter save;
// fatal("defercheckwidth"); int all;
defercalc = 1;
funcdepth++;
// declare the this/in arguments
t = funcfirst(&save, ft);
while(t != T) {
if(t->nname != N) {
t->nname->xoffset = t->width;
addvar(t->nname, t->type, PPARAM);
}
t = funcnext(&save);
}
// declare the outgoing arguments
all = 0;
t = structfirst(&save, getoutarg(ft));
while(t != T) {
if(t->nname != N)
t->nname->xoffset = t->width;
if(t->nname != N) {
addvar(t->nname, t->type, PPARAMOUT);
all |= 1;
} else
all |= 2;
t = structnext(&save);
}
// this test is remarkedly similar to checkarglist
if(all == 3)
yyerror("cannot mix anonymous and named output arguments");
ft->outnamed = 0;
if(all == 1)
ft->outnamed = 1;
} }
/*
* compile the function.
* called in auto-declaration context.
* returns in extern-declaration context.
*/
void void
resumecheckwidth(void) funcbody(Node *n)
{ {
TypeList *l;
if(!defercalc) compile(n);
fatal("restartcheckwidth");
defercalc = 0;
for(l = tlq; l != nil; l = tlq) { // change the declaration context from auto to extern
dowidth(l->t); if(dclcontext != PAUTO)
tlq = l->next; fatal("funcbody: dclcontext");
l->next = tlfree; popdcl();
tlfree = l; funcdepth--;
} if(funcdepth == 0)
dclcontext = PEXTERN;
} }
Node* Node*
embedded(Sym *s) funclit0(Node *t)
{ {
Node *n; Node *n;
char *name;
// Names sometimes have disambiguation junk n = nod(OXXX, N, N);
// appended after a center dot. Discard it when n->outer = funclit;
// making the name for the embedded struct field. n->dcl = autodcl;
enum { CenterDot = 0xB7 }; funclit = n;
name = s->name;
if(utfrune(s->name, CenterDot)) { // new declaration context
name = strdup(s->name); autodcl = dcl();
*utfrune(name, CenterDot) = 0; autodcl->back = autodcl;
}
n = newname(lookup(name)); typecheck(&t, Etype);
n = nod(ODCLFIELD, n, N); funcargs(t->type);
n->embedded = 1; return t;
if(s == S)
return n;
n->right = oldname(s);
return n;
} }
/* Node*
* declare variables from grammar funclit1(Node *ntype, NodeList *body)
* new_name_list (type | [type] = expr_list)
*/
NodeList*
variter(NodeList *vl, Node *t, NodeList *el)
{ {
int doexpr, gen; Node *func;
Node *v, *e; Type *type;
NodeList *init; Node *a, *d, *f, *n, *clos;
Sym *s; Type *ft, *t;
Dcl *r, *d; Iter save;
int narg, shift;
NodeList *args, *l, *in, *out;
init = nil; type = ntype->type;
doexpr = el != nil; popdcl();
for(; vl; vl=vl->next) { func = funclit;
if(doexpr) { funclit = func->outer;
if(el == nil) {
yyerror("missing expr in var dcl");
break;
}
e = el->n;
el = el->next;
} else
e = N;
v = vl->n; // build up type of func f that we're going to compile.
s = v->sym; // as we referred to variables from the outer function,
if(dclcontext == PEXTERN || dclcontext == PFUNC) { // we accumulated a list of PHEAP names in func->cvars.
r = externdcl; narg = 0;
gen = 0; // add PHEAP versions as function arguments.
} else { in = nil;
r = autodcl; for(l=func->cvars; l; l=l->next) {
gen = ++vargen; a = l->n;
pushdcl(s); d = nod(ODCLFIELD, a, N);
} d->type = ptrto(a->type);
in = list(in, d);
redeclare("variable", s); // while we're here, set up a->heapaddr for back end
s->def = v; n = nod(ONAME, N, N);
// TODO: vargen snprint(namebuf, sizeof namebuf, "&%s", a->sym->name);
s->offset = 0; n->sym = lookup(namebuf);
s->block = block; n->type = ptrto(a->type);
n->class = PPARAM;
n->xoffset = narg*types[tptr]->width;
n->addable = 1;
n->ullman = 1;
narg++;
a->heapaddr = n;
v->op = ONAME; a->xoffset = 0;
v->class = dclcontext;
v->ntype = t; // unlink from actual ONAME in symbol table
v->funcdepth = funcdepth; a->closure->closure = a->outer;
v->vargen = gen;
if(e != N || funcdepth > 0) {
if(funcdepth > 0)
init = list(init, nod(ODCL, v, N));
e = nod(OAS, v, e);
init = list(init, e);
if(e->right != N)
v->defn = e;
} }
d = dcl(); // add a dummy arg for the closure's caller pc
d->dsym = s; d = nod(ODCLFIELD, N, N);
d->dnode = v; d->type = types[TUINTPTR];
d->op = ONAME; in = list(in, d);
r->back->forw = d;
r->back = d;
autoexport(s); // slide param offset to make room for ptrs above.
} // narg+1 to skip over caller pc.
if(el != nil) shift = (narg+1)*types[tptr]->width;
yyerror("extra expr in var dcl");
return init;
}
/* // now the original arguments.
* declare constants from grammar for(t=structfirst(&save, getinarg(type)); t; t=structnext(&save)) {
* new_name_list [[type] = expr_list] d = nod(ODCLFIELD, t->nname, N);
*/ d->type = t->type;
NodeList* in = list(in, d);
constiter(NodeList *vl, Node *t, NodeList *cl)
{
Node *v, *c;
NodeList *vv;
Sym *s;
vv = vl; a = t->nname;
if(cl == nil) { if(a != N) {
if(t != N) if(a->stackparam != N)
yyerror("constdcl cannot have type without expr"); a = a->stackparam;
cl = lastconst; a->xoffset += shift;
t = lasttype;
} else {
lastconst = cl;
lasttype = t;
} }
cl = listtreecopy(cl);
for(; vl; vl=vl->next) {
if(cl == nil) {
yyerror("missing expr in const dcl");
break;
} }
c = cl->n;
cl = cl->next;
v = vl->n; // out arguments
s = v->sym; out = nil;
if(dclcontext != PEXTERN) for(t=structfirst(&save, getoutarg(type)); t; t=structnext(&save)) {
pushdcl(s); d = nod(ODCLFIELD, t->nname, N);
redeclare("constant", s); d->type = t->type;
s->def = v; out = list(out, d);
v->op = OLITERAL; a = t->nname;
v->ntype = t; if(a != N) {
v->defn = c; if(a->stackparam != N)
autoexport(s); a = a->stackparam;
a->xoffset += shift;
}
} }
if(cl != nil)
yyerror("extra expr in const dcl");
iota += 1;
return vv;
}
/* ft = functype(N, in, out);
* look for ft->outnamed = type->outnamed;
* unsafe.Sizeof
* unsafe.Offsetof
* rewrite with a constant
*/
Node*
unsafenmagic(Node *fn, NodeList *args)
{
Node *r, *n;
Sym *s;
Type *t, *tr;
long v;
Val val;
if(fn == N || fn->op != ONAME || (s = fn->sym) == S)
goto no;
if(strcmp(s->package, "unsafe") != 0)
goto no;
if(args == nil) {
yyerror("missing argument for %S", s);
goto no;
}
r = args->n;
n = nod(OLITERAL, N, N);
if(strcmp(s->name, "Sizeof") == 0) {
typecheck(&r, Erv);
tr = r->type;
if(r->op == OLITERAL && r->val.ctype == CTSTR)
tr = types[TSTRING];
if(tr == T)
goto no;
v = tr->width;
goto yes;
}
if(strcmp(s->name, "Offsetof") == 0) {
if(r->op != ODOT && r->op != ODOTPTR)
goto no;
typecheck(&r, Erv);
v = r->xoffset;
goto yes;
}
if(strcmp(s->name, "Alignof") == 0) {
typecheck(&r, Erv);
tr = r->type;
if(r->op == OLITERAL && r->val.ctype == CTSTR)
tr = types[TSTRING];
if(tr == T)
goto no;
// make struct { byte; T; }
t = typ(TSTRUCT);
t->type = typ(TFIELD);
t->type->type = types[TUINT8];
t->type->down = typ(TFIELD);
t->type->down->type = tr;
// compute struct widths
dowidth(t);
// the offset of T is its required alignment // declare function.
v = t->type->down->width; vargen++;
goto yes; snprint(namebuf, sizeof(namebuf), "_f%.3ld·%s", vargen, filename);
} f = newname(lookup(namebuf));
addvar(f, ft, PFUNC);
f->funcdepth = 0;
no: // compile function
return N; n = nod(ODCLFUNC, N, N);
n->nname = f;
n->type = ft;
if(body == nil)
body = list1(nod(OEMPTY, N, N));
n->nbody = body;
compile(n);
funcdepth--;
autodcl = func->dcl;
yes: // build up type for this instance of the closure func.
if(args->next != nil) in = nil;
yyerror("extra arguments for %S", s); d = nod(ODCLFIELD, N, N); // siz
// any side effects disappear; ignore init d->type = types[TINT];
val.ctype = CTINT; in = list(in, d);
val.u.xval = mal(sizeof(*n->val.u.xval)); d = nod(ODCLFIELD, N, N); // f
mpmovecfix(val.u.xval, v); d->type = ft;
n = nod(OLITERAL, N, N); in = list(in, d);
n->val = val; for(l=func->cvars; l; l=l->next) {
n->type = types[TINT]; a = l->n;
return n; d = nod(ODCLFIELD, N, N); // arg
} d->type = ptrto(a->type);
in = list(in, d);
}
void d = nod(ODCLFIELD, N, N);
dclchecks(void) d->type = type;
{ out = list1(d);
Dcl *d;
for(d=externdcl; d!=D; d=d->forw) { clos = syslook("closure", 1);
if(d->op != ONAME) clos->type = functype(N, in, out);
continue;
typecheck(&d->dnode, Erv); // literal expression is sys.closure(siz, f, arg0, arg1, ...)
// which builds a function that calls f after filling in arg0,
// arg1, ... for the PHEAP arguments above.
args = nil;
if(narg*widthptr > 100)
yyerror("closure needs too many variables; runtime will reject it");
a = nodintconst(narg*widthptr);
args = list(args, a); // siz
args = list(args, f); // f
for(l=func->cvars; l; l=l->next) {
a = l->n;
d = oldname(a->sym);
args = list(args, nod(OADDR, d, N));
} }
typechecklist(args, Erv);
n = nod(OCALL, clos, N);
n->list = args;
return n;
} }
...@@ -601,8 +601,6 @@ EXTERN char namebuf[NSYMB]; ...@@ -601,8 +601,6 @@ EXTERN char namebuf[NSYMB];
EXTERN char lexbuf[NSYMB]; EXTERN char lexbuf[NSYMB];
EXTERN char debug[256]; EXTERN char debug[256];
EXTERN Sym* hash[NHASH]; EXTERN Sym* hash[NHASH];
EXTERN Sym* dclstack;
EXTERN Sym* b0stack;
EXTERN Sym* pkgmyname; // my name for package EXTERN Sym* pkgmyname; // my name for package
EXTERN Sym* pkgimportname; // package name from imported package EXTERN Sym* pkgimportname; // package name from imported package
EXTERN int tptr; // either TPTR32 or TPTR64 EXTERN int tptr; // either TPTR32 or TPTR64
...@@ -814,7 +812,6 @@ void argtype(Node*, Type*); ...@@ -814,7 +812,6 @@ void argtype(Node*, Type*);
int eqargs(Type*, Type*); int eqargs(Type*, Type*);
uint32 typehash(Type*, int, int); uint32 typehash(Type*, int, int);
void frame(int); void frame(int);
Node* dobad(void);
Node* nodintconst(int64); Node* nodintconst(int64);
void nodconst(Node*, Type*, int64); void nodconst(Node*, Type*, int64);
Node* nodnil(void); Node* nodnil(void);
...@@ -921,7 +918,6 @@ Type* newtype(Sym*); ...@@ -921,7 +918,6 @@ Type* newtype(Sym*);
Type* oldtype(Sym*); Type* oldtype(Sym*);
void fninit(NodeList*); void fninit(NodeList*);
Node* nametodcl(Node*, Type*); Node* nametodcl(Node*, Type*);
Node* anondcl(Type*);
NodeList* checkarglist(NodeList*); NodeList* checkarglist(NodeList*);
void checkwidth(Type*); void checkwidth(Type*);
void defercheckwidth(void); void defercheckwidth(void);
......
...@@ -1137,7 +1137,6 @@ fndcl: ...@@ -1137,7 +1137,6 @@ fndcl:
{ {
Node *n; Node *n;
b0stack = dclstack; // mark base for fn literals
$$ = nod(ODCLFUNC, N, N); $$ = nod(ODCLFUNC, N, N);
$$->nname = $1; $$->nname = $1;
if($3 == nil && $5 == nil) if($3 == nil && $5 == nil)
...@@ -1159,7 +1158,6 @@ fndcl: ...@@ -1159,7 +1158,6 @@ fndcl:
rcvr = N; rcvr = N;
} }
b0stack = dclstack; // mark base for fn literals
$$ = nod(ODCLFUNC, N, N); $$ = nod(ODCLFUNC, N, N);
$$->nname = $4; $$->nname = $4;
$$->nname = methodname($4, rcvr->type); $$->nname = methodname($4, rcvr->type);
......
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go.h"
/*
* a function named init is a special case.
* it is called by the initialization before
* main is run. to make it unique within a
* package and also uncallable, the name,
* normally "pkg.init", is altered to "pkg.init·filename".
*/
Node*
renameinit(Node *n)
{
Sym *s;
s = n->sym;
if(s == S)
return n;
if(strcmp(s->name, "init") != 0)
return n;
snprint(namebuf, sizeof(namebuf), "init·%s", filename);
s = lookup(namebuf);
return newname(s);
}
/*
* hand-craft the following initialization code
* var initdone·<file> uint8 (1)
* func Init·<file>() (2)
* if initdone·<file> { (3)
* if initdone·<file> == 2 (4)
* return
* throw(); (5)
* }
* initdone.<file>++; (6)
* // over all matching imported symbols
* <pkg>.init·<file>() (7)
* { <init stmts> } (8)
* init·<file>() // if any (9)
* initdone.<file>++; (10)
* return (11)
* }
*/
int
anyinit(NodeList *n)
{
uint32 h;
Sym *s;
// are there any init statements
if(n != nil)
return 1;
// is this main
if(strcmp(package, "main") == 0)
return 1;
// is there an explicit init function
snprint(namebuf, sizeof(namebuf), "init·%s", filename);
s = lookup(namebuf);
if(s->def != N)
return 1;
// are there any imported init functions
for(h=0; h<NHASH; h++)
for(s = hash[h]; s != S; s = s->link) {
if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0)
continue;
if(s->def == N)
continue;
return 1;
}
// then none
return 0;
}
void
fninit(NodeList *n)
{
Node *gatevar;
Node *a, *b, *fn;
NodeList *r;
uint32 h;
Sym *s, *initsym;
if(strcmp(package, "PACKAGE") == 0) {
// sys.go or unsafe.go during compiler build
return;
}
if(!anyinit(n))
return;
r = nil;
// (1)
snprint(namebuf, sizeof(namebuf), "initdone·%s", filename);
gatevar = newname(lookup(namebuf));
addvar(gatevar, types[TUINT8], PEXTERN);
// (2)
maxarg = 0;
stksize = initstksize;
snprint(namebuf, sizeof(namebuf), "Init·%s", filename);
// this is a botch since we need a known name to
// call the top level init function out of rt0
if(strcmp(package, "main") == 0)
snprint(namebuf, sizeof(namebuf), "init");
fn = nod(ODCLFUNC, N, N);
initsym = lookup(namebuf);
fn->nname = newname(initsym);
fn->type = functype(N, nil, nil);
funchdr(fn);
// (3)
a = nod(OIF, N, N);
a->ntest = nod(ONE, gatevar, nodintconst(0));
r = list(r, a);
// (4)
b = nod(OIF, N, N);
b->ntest = nod(OEQ, gatevar, nodintconst(2));
b->nbody = list1(nod(ORETURN, N, N));
a->nbody = list1(b);
// (5)
b = syslook("throwinit", 0);
b = nod(OCALL, b, N);
a->nbody = list(a->nbody, b);
// (6)
a = nod(OASOP, gatevar, nodintconst(1));
a->etype = OADD;
r = list(r, a);
// (7)
for(h=0; h<NHASH; h++)
for(s = hash[h]; s != S; s = s->link) {
if(s->name[0] != 'I' || strncmp(s->name, "Init·", 6) != 0)
continue;
if(s->def == N)
continue;
if(s == initsym)
continue;
// could check that it is fn of no args/returns
a = nod(OCALL, s->def, N);
r = list(r, a);
}
// (8)
r = concat(r, initfix(n));
// (9)
// could check that it is fn of no args/returns
snprint(namebuf, sizeof(namebuf), "init·%s", filename);
s = lookup(namebuf);
if(s->def != N) {
a = nod(OCALL, s->def, N);
r = list(r, a);
}
// (10)
a = nod(OASOP, gatevar, nodintconst(1));
a->etype = OADD;
r = list(r, a);
// (11)
a = nod(ORETURN, N, N);
r = list(r, a);
exportsym(fn->nname->sym);
fn->nbody = r;
//dump("b", fn);
//dump("r", fn->nbody);
popdcl();
initflag = 1; // flag for loader static initialization
compile(fn);
initflag = 0;
}
...@@ -393,10 +393,11 @@ typ(int et) ...@@ -393,10 +393,11 @@ typ(int et)
return t; return t;
} }
Node*
dobad(void) Type*
sortinter(Type *t)
{ {
return nod(OBAD, N, N); return t;
} }
Node* Node*
...@@ -2636,23 +2637,25 @@ NodeList* ...@@ -2636,23 +2637,25 @@ NodeList*
structargs(Type **tl, int mustname) structargs(Type **tl, int mustname)
{ {
Iter savet; Iter savet;
Node *a; Node *a, *n;
NodeList *args; NodeList *args;
Type *t; Type *t;
char nam[100]; char buf[100];
int n; int gen;
args = nil; args = nil;
n = 0; gen = 0;
for(t = structfirst(&savet, tl); t != T; t = structnext(&savet)) { for(t = structfirst(&savet, tl); t != T; t = structnext(&savet)) {
n = N;
if(t->sym) if(t->sym)
a = nametodcl(newname(t->sym), t->type); n = newname(t->sym);
else if(mustname) { else if(mustname) {
// have to give it a name so we can refer to it in trampoline // have to give it a name so we can refer to it in trampoline
snprint(nam, sizeof nam, ".anon%d", n++); snprint(buf, sizeof buf, ".anon%d", gen++);
a = nametodcl(newname(lookup(nam)), t->type); n = newname(lookup(buf));
} else }
a = anondcl(t->type); a = nod(ODCLFIELD, n, N);
a->type = t->type;
args = list(args, a); args = list(args, a);
} }
return args; return args;
...@@ -2694,7 +2697,8 @@ genwrapper(Type *rcvr, Type *method, Sym *newnam) ...@@ -2694,7 +2697,8 @@ genwrapper(Type *rcvr, Type *method, Sym *newnam)
dclcontext = PEXTERN; dclcontext = PEXTERN;
markdcl(); markdcl();
this = nametodcl(newname(lookup(".this")), rcvr); this = nod(ODCLFIELD, newname(lookup(".this")), N);
this->type = rcvr;
in = structargs(getinarg(method->type), 1); in = structargs(getinarg(method->type), 1);
out = structargs(getoutarg(method->type), 0); out = structargs(getoutarg(method->type), 0);
...@@ -2982,6 +2986,9 @@ liststmt(NodeList *l) ...@@ -2982,6 +2986,9 @@ liststmt(NodeList *l)
return n; return n;
} }
/*
* return nelem of list
*/
int int
count(NodeList *l) count(NodeList *l)
{ {
...@@ -2992,3 +2999,96 @@ count(NodeList *l) ...@@ -2992,3 +2999,96 @@ count(NodeList *l)
n++; n++;
return n; return n;
} }
/*
* return nelem of list
*/
int
structcount(Type *t)
{
int v;
Iter s;
v = 0;
for(t = structfirst(&s, &t); t != T; t = structnext(&s))
v++;
return v;
}
/*
* when a type's width should be known, we call checkwidth
* to compute it. during a declaration like
*
* type T *struct { next T }
*
* it is necessary to defer the calculation of the struct width
* until after T has been initialized to be a pointer to that struct.
* similarly, during import processing structs may be used
* before their definition. in those situations, calling
* defercheckwidth() stops width calculations until
* resumecheckwidth() is called, at which point all the
* checkwidths that were deferred are executed.
* sometimes it is okay to
*/
typedef struct TypeList TypeList;
struct TypeList {
Type *t;
TypeList *next;
};
static TypeList *tlfree;
static TypeList *tlq;
static int defercalc;
void
checkwidth(Type *t)
{
TypeList *l;
// function arg structs should not be checked
// outside of the enclosing function.
if(t->funarg)
fatal("checkwidth %T", t);
if(!defercalc) {
dowidth(t);
return;
}
l = tlfree;
if(l != nil)
tlfree = l->next;
else
l = mal(sizeof *l);
l->t = t;
l->next = tlq;
tlq = l;
}
void
defercheckwidth(void)
{
// we get out of sync on syntax errors, so don't be pedantic.
// if(defercalc)
// fatal("defercheckwidth");
defercalc = 1;
}
void
resumecheckwidth(void)
{
TypeList *l;
if(!defercalc)
fatal("restartcheckwidth");
defercalc = 0;
for(l = tlq; l != nil; l = tlq) {
dowidth(l->t);
tlq = l->next;
l->next = tlfree;
tlfree = l;
}
}
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go.h"
/*
* look for
* unsafe.Sizeof
* unsafe.Offsetof
* rewrite with a constant
*/
Node*
unsafenmagic(Node *fn, NodeList *args)
{
Node *r, *n;
Sym *s;
Type *t, *tr;
long v;
Val val;
if(fn == N || fn->op != ONAME || (s = fn->sym) == S)
goto no;
if(strcmp(s->package, "unsafe") != 0)
goto no;
if(args == nil) {
yyerror("missing argument for %S", s);
goto no;
}
r = args->n;
n = nod(OLITERAL, N, N);
if(strcmp(s->name, "Sizeof") == 0) {
typecheck(&r, Erv);
tr = r->type;
if(r->op == OLITERAL && r->val.ctype == CTSTR)
tr = types[TSTRING];
if(tr == T)
goto no;
v = tr->width;
goto yes;
}
if(strcmp(s->name, "Offsetof") == 0) {
if(r->op != ODOT && r->op != ODOTPTR)
goto no;
typecheck(&r, Erv);
v = r->xoffset;
goto yes;
}
if(strcmp(s->name, "Alignof") == 0) {
typecheck(&r, Erv);
tr = r->type;
if(r->op == OLITERAL && r->val.ctype == CTSTR)
tr = types[TSTRING];
if(tr == T)
goto no;
// make struct { byte; T; }
t = typ(TSTRUCT);
t->type = typ(TFIELD);
t->type->type = types[TUINT8];
t->type->down = typ(TFIELD);
t->type->down->type = tr;
// compute struct widths
dowidth(t);
// the offset of T is its required alignment
v = t->type->down->width;
goto yes;
}
no:
return N;
yes:
if(args->next != nil)
yyerror("extra arguments for %S", s);
// any side effects disappear; ignore init
val.ctype = CTINT;
val.u.xval = mal(sizeof(*n->val.u.xval));
mpmovecfix(val.u.xval, v);
n = nod(OLITERAL, N, N);
n->val = val;
n->type = types[TINT];
return n;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment