summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--parse/infer.c36
-rw-r--r--parse/node.c5
-rw-r--r--parse/parse.h3
-rw-r--r--parse/stab.c72
4 files changed, 70 insertions, 46 deletions
diff --git a/parse/infer.c b/parse/infer.c
index f32749f..10be7dd 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -1598,7 +1598,6 @@ static void specializeimpl(Inferstate *st, Node *n)
n->impl.trait = t;
dcl = NULL;
- proto = NULL;
if (n->impl.naux != t->naux)
fatal(n, "%s incompatibly specialized with %zd types instead of %zd types",
namestr(n->impl.traitname), n->impl.naux, t->naux);
@@ -1620,7 +1619,7 @@ static void specializeimpl(Inferstate *st, Node *n)
if (file->file.globls->name)
setns(dcl->decl.name, file->file.globls->name);
for (j = 0; j < t->nfuncs; j++) {
- if (nameeq(dcl->decl.name, t->funcs[j]->decl.name)) {
+ if (nsnameeq(dcl->decl.name, t->funcs[j]->decl.name)) {
proto = t->funcs[j];
break;
}
@@ -2397,8 +2396,8 @@ void tagexports(Node *file, int hidelocal)
/* tag the traits */
free(k);
k = htkeys(st->tr, &n);
- for (i = 0; i < n; i++) {
- tr = gettrait(st, k[i]);
+ for (j = 0; j < n; j++) {
+ tr = gettrait(st, k[j]);
if (tr->vis == Visexport) {
tr->param->vis = Visexport;
for (i = 0; i < tr->nmemb; i++) {
@@ -2448,22 +2447,35 @@ static void specialize(Inferstate *st, Node *f)
void applytraits(Inferstate *st, Node *f)
{
size_t i;
- Node *n;
+ Node *impl, *n;
Trait *tr;
Type *ty;
+ Stab *ns;
+ tr = NULL;
pushstab(f->file.globls);
/* for now, traits can only be declared globally */
for (i = 0; i < nimpltab; i++) {
- n = impltab[i];
- tr = gettrait(f->file.globls, n->impl.traitname);
- if (!tr)
- fatal(n, "trait %s does not exist near %s",
- namestr(n->impl.traitname), ctxstr(st, n));
- ty = tf(st, n->impl.type);
+ impl = impltab[i];
+ tr = impl->impl.trait;
+ if (!tr) {
+ n = impl->impl.traitname;
+ ns = file->file.globls;
+ if (n->name.ns)
+ ns = getns(file, n->name.ns);
+ if (ns)
+ tr = gettrait(ns, n);
+ if (!tr)
+ fatal(impl, "trait %s does not exist near %s",
+ namestr(impl->impl.traitname), ctxstr(st, impl));
+ if (tr->naux != impl->impl.naux)
+ fatal(impl, "incompatible implementation of %s: mismatched aux types",
+ namestr(impl->impl.traitname), ctxstr(st, impl));
+ }
+ ty = tf(st, impl->impl.type);
settrait(ty, tr);
if (tr->uid == Tciter) {
- htput(st->seqbase, tf(st, n->impl.type), tf(st, n->impl.aux[0]));
+ htput(st->seqbase, tf(st, impl->impl.type), tf(st, impl->impl.aux[0]));
}
}
popstab();
diff --git a/parse/node.c b/parse/node.c
index 62a6941..63d26c2 100644
--- a/parse/node.c
+++ b/parse/node.c
@@ -471,7 +471,10 @@ int nameeq(void *p1, void *p2)
void setns(Node *n, char *ns)
{
- assert(!n->name.ns || !strcmp(n->name.ns, ns));
+ assert(!ns || !n->name.ns || !strcmp(n->name.ns, ns));
+
+ if (!ns)
+ return;
n->name.ns = strdup(ns);
}
diff --git a/parse/parse.h b/parse/parse.h
index c5b430e..2ce0a03 100644
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -230,6 +230,7 @@ struct Node {
size_t ninit;
Node *localinit; /* and the local one, if any */
Stab *globls; /* global symtab */
+ Stab *builtins; /* global symtab */
Htab *ns; /* namespaces */
} file;
@@ -465,6 +466,8 @@ ulong tyhash(void *t);
int tyeq(void *a, void *b);
ulong namehash(void *t);
int nameeq(void *a, void *b);
+ulong nsnamehash(void *t);
+int nsnameeq(void *a, void *b);
/* util functions */
char *fname(Srcloc l);
diff --git a/parse/stab.c b/parse/stab.c
index ff85026..061de84 100644
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -53,9 +53,15 @@ void popstab(void)
/* name hashing: we want namespaced lookups to find the
* name even if we haven't set the namespace up, since
* we can update it after the fact. */
-static ulong nsnamehash(void *n) { return strhash(namestr(n)); }
+ulong nsnamehash(void *n)
+{
+ return strhash(namestr(n));
+}
-static int nsnameeq(void *a, void *b) { return a == b || !strcmp(namestr(a), namestr(b)); }
+int nsnameeq(void *a, void *b)
+{
+ return a == b || !strcmp(namestr(a), namestr(b));
+}
static ulong implhash(void *p)
{
@@ -68,6 +74,21 @@ static ulong implhash(void *p)
return h;
}
+Stab *findstab(Stab *st, Node *n)
+{
+ Stab *ns;
+
+ if (n->name.ns) {
+ ns = getns(file, n->name.ns);
+ if (!ns) {
+ ns = mkstab(0);
+ updatens(ns, n->name.ns);
+ }
+ st = ns;
+ }
+ return st;
+}
+
static int impleq(void *pa, void *pb)
{
Node *a, *b;
@@ -252,8 +273,7 @@ static int mergedecl(Node *old, Node *new)
fatal(e, "export %s declared with different externness on %s:%d", declname(e),
fname(g->loc), lnum(g->loc));
- if (new->decl.name->name.ns)
- setns(old->decl.name, new->decl.name->name.ns);
+ setns(old->decl.name, new->decl.name->name.ns);
if (e->decl.type->type == Tyvar)
e->decl.type = g->decl.type;
else if (g->decl.type->type == Tyvar)
@@ -277,26 +297,16 @@ static int mergedecl(Node *old, Node *new)
void forcedcl(Stab *st, Node *s)
{
- if (st->name)
- setns(s->decl.name, st->name);
+ setns(s->decl.name, st->name);
htput(st->dcl, s->decl.name, s);
assert(htget(st->dcl, s->decl.name) != NULL);
}
void putdcl(Stab *st, Node *s)
{
- Node *name, *old;
- Stab *ns;
+ Node *old;
- name = s->decl.name;
- if (name->name.ns) {
- ns = getns(file, name->name.ns);
- if (!ns) {
- ns = mkstab(0);
- updatens(ns, name->name.ns);
- }
- st = ns;
- }
+ st = findstab(st, s->decl.name);
old = htget(st->dcl, s->decl.name);
if (!old)
forcedcl(st, s);
@@ -342,8 +352,8 @@ void puttype(Stab *st, Node *n, Type *t)
Tydefn *td;
Type *ty;
- if (st->name)
- setns(n, st->name);
+ st = findstab(st, n);
+ setns(n, st->name);
if (st->name && t && t->name)
setns(t->name, st->name);
@@ -395,6 +405,7 @@ void puttrait(Stab *st, Node *n, Trait *c)
Trait *t;
Type *ty;
+ st = findstab(st, n);
t = gettrait(st, n);
if (t && !mergetrait(t, c))
fatal(n, "Trait %s already defined on %s:%d", namestr(n), fname(t->loc),
@@ -407,6 +418,7 @@ void puttrait(Stab *st, Node *n, Trait *c)
td->loc = n->loc;
td->name = n;
td->trait = c;
+ setns(td->name, st->name);
htput(st->tr, td->name, td);
}
@@ -434,25 +446,17 @@ static int mergeimpl(Node *old, Node *new)
void putimpl(Stab *st, Node *n)
{
- Node *impl, *name;
- Stab *ns;
-
- name = n->impl.traitname;
- if (name->name.ns) {
- ns = getns(file, name->name.ns);
- if (!ns) {
- ns = mkstab(0);
- updatens(ns, name->name.ns);
- }
- st = ns;
- }
+ Node *impl;
+ st = findstab(st, n->impl.traitname);
impl = getimpl(st, n);
if (impl && !mergeimpl(impl, n))
fatal(n, "Trait %s already implemented over %s at %s:%d",
namestr(n->impl.traitname), tystr(n->impl.type), fname(n->loc), lnum(n->loc));
- if (st->name)
- setns(n->impl.traitname, st->name);
+ /*
+ * The impl is not defined in this file, so setting the trait name would be a bug here.
+ */
+ setns(n->impl.traitname, st->name);
htput(st->impl, n, n);
}
@@ -493,10 +497,12 @@ void updatens(Stab *st, char *name)
die("Stab %s already has namespace; Can't set to %s", st->name, name);
st->name = strdup(name);
htput(file->file.ns, st->name, st);
+
k = htkeys(st->dcl, &nk);
for (i = 0; i < nk; i++)
setns(k[i], name);
free(k);
+
k = htkeys(st->ty, &nk);
for (i = 0; i < nk; i++)
setns(k[i], name);