shithub: mc

Download patch

ref: 12df9f45f4f383a6bdf8939b44a3b499edee853e
parent: 6d4651504d015f853c5ce79e77c73b5b8d48793f
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Jul 9 19:55:41 EDT 2015

Refactor namespaces.

    We no longer store subnamespaces in the stab node; now they're
    a part of the file.

--- a/6/gen.c
+++ b/6/gen.c
@@ -17,8 +17,8 @@
 
 void fillglobls(Stab *st, Htab *globls)
 {
-    void **k;
-    size_t i, nk;
+    size_t i, j, nk, nns;
+    void **k, **ns;
     Stab *stab;
     Node *s;
 
@@ -29,13 +29,17 @@
     }
     free(k);
 
-    k = htkeys(st->ns, &nk);
-    for (i = 0; i < nk; i++) {
-        stab = htget(st->ns, k[i]);
-        fillglobls(stab, globls);
+    ns = htkeys(file->file.ns, &nns);
+    for (j = 0; j < nns; j++) {
+        stab = htget(file->file.ns, ns[j]);
+        k = htkeys(stab->dcl, &nk);
+        for (i = 0; i < nk; i++) {
+            s = htget(stab->dcl, k[i]);
+            htput(globls, s, asmname(s));
+        }
+        free(k);
     }
-
-    free(k);
+    free(ns);
 }
 
 static int islocal(Node *dcl)
--- a/mbld/deps.myr
+++ b/mbld/deps.myr
@@ -234,7 +234,8 @@
 	| `std.None:	std.fatal("library {}: could not read usefile\n", lib)
 	;;
 	match bio.getbe32(f)
-	| `std.Some 3:	/* nothing: version matches. */
+	| `std.Some 4:	/* nothing: version matches. */
+	| `std.Some 3:	std.fput(1, "library {}: warning: old usefile version\n", lib)
 	| `std.Some 2:	std.fput(1, "library {}: warning: old usefile version\n", lib)
 	| `std.Some 1:	std.fput(1, "library {}: warning: old usefile version\n", lib)
 	| `std.Some 0:	std.fput(1, "library {}: warning: old usefile version\n", lib)
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -56,7 +56,7 @@
     char *ty;
     Type *t;
 
-    findentf(fd, depth, "Stab %p (super = %p, name=\"%s\")\n", st, st->super, st->ns);
+    findentf(fd, depth, "Stab %p (super = %p, name=\"%s\")\n", st, st->super, st->name);
     if (!st)
         return;
 
@@ -82,14 +82,6 @@
         findentf(fd, depth, "S ");
         /* already indented */
         outsym(getdcl(st, k[i]), fd, 0);
-    }
-    free(k);
-
-    /* dump sub-namespaces */
-    k = htkeys(st->ns, &n);
-    for (i = 0; i < n; i++) {
-        findentf(fd, depth + 1, "N  %s\n", (char*)k[i]);
-        outstab(getns_str(st, k[i]), fd, depth + 1);
     }
     free(k);
 }
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -446,7 +446,7 @@
         if (!tytab[t->tid] && t->type == Tyunres) {
             ns = curstab();
             if (t->name->name.ns) {
-                ns = getns_str(ns, t->name->name.ns);
+                ns = getns(file, t->name->name.ns);
             }
             if (!ns)
                 fatal(t->name, "could not resolve namespace \"%s\"", t->name->name.ns);
@@ -585,7 +585,7 @@
     args = n->expr.args;
     ns = curstab();
     if (args[0]->name.ns)
-        ns = getns_str(ns, args[0]->name.ns);
+        ns = getns(file, args[0]->name.ns);
     if (!ns)
         fatal(n, "no namespace %s\n", args[0]->name.ns);
     uc = getucon(ns, args[0]);
@@ -1073,7 +1073,7 @@
     if (args[0]->type != Nexpr || exprop(args[0]) != Ovar)
         return n;
     name = args[0]->expr.args[0];
-    stab = getns(curstab(), name);
+    stab = getns(file, namestr(name));
     if (!stab)
         return n;
 
@@ -1196,7 +1196,7 @@
         case Ovar:
             ns = curstab();
             if (args[0]->name.ns)
-                ns = getns_str(ns, args[0]->name.ns);
+                ns = getns(file, args[0]->name.ns);
             s = getdcl(ns, args[0]);
             if (s && !s->decl.ishidden) {
                 if (s->decl.isgeneric)
--- a/parse/node.c
+++ b/parse/node.c
@@ -62,6 +62,7 @@
     Node *n;
 
     n = mknode(Zloc, Nfile);
+    n->file.ns = mkht(strhash, streq);
     lappend(&n->file.files, &n->file.nfiles, strdup(name));
     return n;
 }
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -4,7 +4,7 @@
 #	define FATAL
 #endif
 
-#define Abiversion 3
+#define Abiversion 4
 
 typedef uint8_t         byte;
 typedef unsigned int    uint;
@@ -135,7 +135,6 @@
      * types and values are in separate namespaces. */
     Htab *dcl;
     Htab *closure;      /* the syms we close over */
-    Htab *ns;           /* namespaces */
     Htab *ty;           /* types */
     Htab *tr;           /* traits */
     Htab *uc;           /* union constructors */
@@ -223,6 +222,7 @@
             size_t ninit;
             Node *localinit;    /* and the local one, if any */
             Stab  *globls;      /* global symtab */
+            Htab *ns;           /* namespaces */
         } file;
 
         struct {
@@ -478,12 +478,10 @@
 void putimpl(Stab *st, Node *impl);
 void updatetype(Stab *st, Node *n, Type *t);
 void putdcl(Stab *st, Node *dcl);
-void putnsdcl(Node *dcl);
 void forcedcl(Stab *st, Node *dcl);
 void putucon(Stab *st, Ucon *uc);
 
-Stab *getns(Stab *st, Node *n);
-Stab *getns_str(Stab *st, char *n);
+Stab *getns(Node *file, char *n);
 Node *getdcl(Stab *st, Node *n);
 Type *gettype_l(Stab *st, Node *n);
 Type *gettype(Stab *st, Node *n);
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -164,7 +164,7 @@
             if (n->expr.op == Ovar) {
                 ns = curstab();
                 if (n->expr.args[0]->name.ns)
-                    ns = getns_str(ns, n->expr.args[0]->name.ns);
+                    ns = getns(file, n->expr.args[0]->name.ns);
                 if (!ns)
                     fatal(n, "No namespace %s\n", n->expr.args[0]->name.ns);
                 d = getdcl(ns, n->expr.args[0]);
@@ -393,7 +393,7 @@
     n = genericname(g, to);
     *name = n;
     if (n->name.ns)
-        st = getns_str(curstab(), n->name.ns);
+        st = getns(file, n->name.ns);
     else
         st = file->file.globls;
     if (!st)
@@ -453,7 +453,7 @@
         dcl->decl.isconst = 1;
         dcl->decl.isinit = 1;
         dcl->decl.isextern = 1;
-        putnsdcl(dcl);
+        putdcl(file->file.globls, dcl);
     }
     return dcl;
 }
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -90,7 +90,6 @@
     Stab *st;
 
     st = zalloc(sizeof(Stab));
-    st->ns = mkht(strhash, streq);
     st->dcl = mkht(nsnamehash, nsnameeq);
     st->ty = mkht(nsnamehash, nsnameeq);
     st->tr = mkht(nsnamehash, nsnameeq);
@@ -186,25 +185,11 @@
     return NULL;
 }
 
-Stab *getns_str(Stab *st, char *name)
+Stab *getns(Node *file, char *name)
 {
-    Stab *s;
-
-    do {
-        if (streq(st->name, name))
-            return st;
-        if ((s = htget(st->ns, name)))
-            return s;
-        st = st->super;
-    } while (st);
-    return NULL;
+    return htget(file->file.ns, name);
 }
 
-Stab *getns(Stab *st, Node *n)
-{
-    return getns_str(st, namestr(n));
-}
-
 static int mergedecl(Node *old, Node *new)
 {
     Node *e, *g;
@@ -262,8 +247,19 @@
 
 void putdcl(Stab *st, Node *s)
 {
-    Node *old;
+    Node *name, *old;
+    Stab *ns;
 
+    name = s->decl.name;
+    if (name->name.ns) {
+        ns = getns(file, name->name.ns);
+        if (!ns) {
+            ns = mkstab();
+            updatens(ns, name->name.ns);
+            putns(file->file.globls, ns);
+        }
+        st = ns;
+    }
     old = htget(st->dcl, s->decl.name);
     if (!old)
         forcedcl(st, s);
@@ -271,21 +267,6 @@
         fatal(old, "%s already declared on %s:%d", namestr(s->decl.name), fname(s->loc), lnum(s->loc));
 }
 
-void putnsdcl(Node *dcl)
-{
-    Node *name;
-    Stab *ns;
-
-    name = dcl->decl.name;
-    ns = getns_str(file->file.globls, name->name.ns);
-    if (!ns) {
-        ns = mkstab();
-        updatens(ns, name->name.ns);
-        putns(file->file.globls, ns);
-    }
-    putdcl(ns, dcl);
-}
-
 void updatetype(Stab *st, Node *n, Type *t)
 {
     Tydefn *td;
@@ -425,10 +406,10 @@
 {
     Stab *s;
 
-    s = getns_str(st, scope->name);
+    s = getns(file, scope->name);
     if (s)
         lfatal(Zloc, "Namespace %s already defined", st->name);
-    htput(st->ns, scope->name, scope);
+    htput(file->file.ns, scope->name, scope);
 }
 
 /*
@@ -445,6 +426,7 @@
     if (st->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);
@@ -457,9 +439,5 @@
         if (td->type && (td->type->type == Tyname || td->type->type == Tygeneric))
             setns(td->type->name, name);
     }
-    free(k);
-    k = htkeys(st->ns, &nk);
-    for (i = 0; i < nk; i++)
-        setns(k[i], name);
     free(k);
 }
--- a/parse/use.c
+++ b/parse/use.c
@@ -67,12 +67,6 @@
     }
     free(keys);
 
-    /* write stabs */
-    keys = htkeys(val->ns, &n);
-    wrint(fd, n);
-    for (i = 0; i < n; i++)
-        wrstab(fd, getns(val, keys[i]));
-    free(keys);
 }
 
 /* Reads a symbol table from file. The converse
@@ -99,12 +93,6 @@
         rdtype(fd, &ty);
         puttype(st, nm, ty);
     }
-
-    /* read stabs */
-    n = rdint(fd);
-    for (i = 0; i < n; i++)
-        putns(st, rdstab(fd));
-
     return st;
 }
 
@@ -714,7 +702,7 @@
             return NULL;
     }
 
-    s = getns_str(st, pkg);
+    s = getns(file, pkg);
     if (!s) {
         s = mkstab();
         s->name = strdup(pkg);