shithub: mc

Download patch

ref: 77a37e3330f03b9f1a9c45810994d94c921b45ae
parent: d667281e1d2da0c39a21505bcc383af7a30b01b8
author: Ori Bernstein <[email protected]>
date: Mon May 27 09:28:39 EDT 2013

Fix cyclic type exports.

    We really need a new usefile format. But for now, this works.

--- a/parse/type.c
+++ b/parse/type.c
@@ -415,7 +415,7 @@
 
         case Typtr:     
             p += tybfmt(p, end - p, t->sub[0]);
-            p += snprintf(p, end - p, "*");
+            p += snprintf(p, end - p, "#");
             break;
         case Tyslice:
             p += tybfmt(p, end - p, t->sub[0]);
--- a/parse/use.c
+++ b/parse/use.c
@@ -12,7 +12,7 @@
 #include "parse.h"
 
 static void wrtype(FILE *fd, Type *val);
-static Type *rdtype(FILE *fd);
+static void rdtype(FILE *fd, Type **dest);
 static void wrstab(FILE *fd, Stab *val);
 static Stab *rdstab(FILE *fd);
 static void wrsym(FILE *fd, Node *val);
@@ -20,6 +20,14 @@
 static void pickle(Node *n, FILE *fd);
 static Node *unpickle(FILE *fd);
 
+/* type fixup list */
+static Htab *tidmap;    /* map from tid -> type */
+static Type ***typefixdest;  /* list of types we need to replace */
+static size_t ntypefixdest; /* size of replacement list */
+static intptr_t *typefixid;  /* list of types we need to replace */
+static size_t ntypefixid; /* size of replacement list */
+#define Builtinmask (1 << 30)
+
 /* Outputs a symbol table to file in a way that can be
  * read back usefully. Only writes declarations, types
  * and sub-namespaces. Captured variables are ommitted. */
@@ -75,7 +83,7 @@
     n = rdint(fd);
     for (i = 0; i < n; i++) {
         nm = unpickle(fd);
-        ty = rdtype(fd);
+        rdtype(fd, &ty);
         puttype(st, nm, ty);
     }
 
@@ -110,7 +118,7 @@
     id = rdint(fd);
     name = unpickle(fd);
     if (rdbool(fd))
-      et = rdtype(fd);
+      rdtype(fd, &et);
     uc = mkucon(line, name, ut, et);
     uc->id = id;
     return uc;
@@ -140,13 +148,12 @@
 {
     int line;
     Node *name;
-    Type *type;
     Node *n;
 
     line = rdint(fd);
     name = unpickle(fd);
-    type = rdtype(fd);
-    n = mkdecl(line, name, type);
+    n = mkdecl(line, name, NULL);
+    rdtype(fd, &n->decl.type);
     
     n->decl.isconst = rdbool(fd);
     n->decl.isgeneric = rdbool(fd);
@@ -160,8 +167,8 @@
 
 /* Writes types to a file. Errors on
  * internal only types like Tyvar that
- * will not be meaningful in another file */
-static void wrtype(FILE *fd, Type *ty)
+ * will not be meaningful in another file*/
+static void typickle(FILE *fd, Type *ty)
 {
     size_t i;
 
@@ -211,15 +218,33 @@
     }
 }
 
-static void typickle(Type *t, FILE *fd)
+static void wrtype(FILE *fd, Type *ty)
 {
-    wrtype(fd, t);
+    if (ty->tid >= Builtinmask)
+        die("Type id %d for %s too big", ty->tid, tystr(ty));
+    if (ty->vis == Visbuiltin)
+        wrint(fd, ty->type | Builtinmask);
+    else
+        wrint(fd, ty->tid);
 }
 
+static void rdtype(FILE *fd, Type **dest)
+{
+    intptr_t tid;
+
+    tid = rdint(fd);
+    if (tid & Builtinmask) {
+        *dest = mktype(-1, tid & ~Builtinmask);
+    } else {
+        lappend(&typefixdest, &ntypefixdest, dest);
+        lappend(&typefixid, &ntypefixid, (void*)tid);
+    }
+}
+
 /* Writes types to a file. Errors on
  * internal only types like Tyvar that
  * will not be meaningful in another file */
-static Type *rdtype(FILE *fd)
+static Type *tyunpickle(FILE *fd)
 {
     Type *ty;
     Ty t;
@@ -252,19 +277,19 @@
                 ty->udecls[i] = rducon(fd, ty);
             break;
         case Tyarray:
-            ty->sub[0] = rdtype(fd);
+            rdtype(fd, &ty->sub[0]);
             ty->asize = unpickle(fd);
             break;
         case Tyslice:
-            ty->sub[0] = rdtype(fd);
+            rdtype(fd, &ty->sub[0]);
             break;
         case Tyname:
             ty->name = unpickle(fd);
-            ty->sub[0] = rdtype(fd);
+            rdtype(fd, &ty->sub[0]);
             break;
         default:
             for (i = 0; i < ty->nsub; i++)
-                ty->sub[i] = rdtype(fd);
+                rdtype(fd, &ty->sub[i]);
             break;
     }
     return ty;
@@ -421,7 +446,7 @@
 
         case Nexpr:
             n->expr.op = rdbyte(fd);
-            n->expr.type = rdtype(fd);
+            rdtype(fd, &n->expr.type);
             n->expr.isconst = rdbool(fd);
             n->expr.nargs = rdint(fd);
             n->expr.args = xalloc(sizeof(Node *)*n->expr.nargs);
@@ -439,7 +464,7 @@
             break;
         case Nlit:
             n->lit.littype = rdbyte(fd);
-            n->lit.type = rdtype(fd);
+            rdtype(fd, &n->lit.type);
             n->lit.nelt = rdint(fd);
             switch (n->lit.littype) {
                 case Lchr:      n->lit.chrval = rdint(fd);       break;
@@ -490,7 +515,7 @@
             n->decl.did = maxdid++; /* unique within file */
             /* sym */
             n->decl.name = unpickle(fd);
-            n->decl.type = rdtype(fd);
+            rdtype(fd, &n->decl.type);
 
             /* symflags */
             n->decl.isconst = rdint(fd);
@@ -502,7 +527,7 @@
             lappend(&decls, &ndecls, n);
             break;
         case Nfunc:
-            n->func.type = rdtype(fd);
+            rdtype(fd, &n->func.type);
             n->func.scope = rdstab(fd);
             n->func.nargs = rdint(fd);
             n->func.args = xalloc(sizeof(Node *)*n->func.nargs);
@@ -543,6 +568,19 @@
     return s;
 }
 
+static void fixmappings(Stab *st)
+{
+    size_t i;
+
+    for (i = 0; i < ntypefixdest; i++) {
+        *typefixdest[i] = htget(tidmap, (void*)typefixid[i]);
+        if (!*typefixdest[i])
+            die("Couldn't find type %d\n", (int)typefixid[i]);
+    }
+    lfree(&typefixdest, &ntypefixdest);
+    lfree(&typefixid, &ntypefixid);
+}
+
 /* Usefile format:
  *     U<pkgname>
  *     T<pickled-type>
@@ -554,9 +592,9 @@
     char *pkg;
     Stab *s;
     Node *dcl;
-    Type *t, *u;
+    Type *t;
+    intptr_t tid;
     int c;
-    size_t i;
 
     if (fgetc(f) != 'U')
         return 0;
@@ -577,6 +615,7 @@
             s = st;
         }
     }
+    tidmap = mkht(ptrhash, ptreq);
     while ((c = fgetc(f)) != EOF) {
         switch(c) {
             case 'G':
@@ -585,20 +624,27 @@
                 putdcl(s, dcl);
                 break;
             case 'T':
-                t = rdtype(f);
-                assert(t->type == Tyname || t->type == Tygeneric);
-                puttype(s, t->name, t);
+                tid = rdint(f);
+                t = tyunpickle(f);
+                htput(tidmap, (void*)tid, t);
+                /* fix up types */
+                if (t->type == Tyname || t->type == Tygeneric)
+                    if (!gettype(st, t->name))
+                        puttype(st, t->name, t);
+                /*
                 u = tybase(t);
                 if (u->type == Tyunion)  {
                     for (i = 0; i < u->nmemb; i++)
                         putucon(s, u->udecls[i]);
                 }
-
+                */
                 break;
             case EOF:
                 break;
         }
     }
+    fixmappings(s);
+    htfree(tidmap);
     return 1;
 }
 
@@ -632,7 +678,7 @@
         die("Could not load usefile %s", use->use.name);
 }
 
-void taghidden(Type *t)
+static void taghidden(Type *t)
 {
     size_t i;
 
@@ -653,12 +699,82 @@
     }
 }
 
-void tagexports(Stab *st)
+static void nodetag(Node *n)
 {
+    size_t i;
+
+    if (!n)
+        return;
+    switch (n->type) {
+        case Nblock:
+            for (i = 0; i < n->block.nstmts; i++)
+                nodetag(n->block.stmts[i]);
+            break;
+        case Nifstmt:
+            nodetag(n->ifstmt.cond);
+            nodetag(n->ifstmt.iftrue);
+            nodetag(n->ifstmt.iffalse);
+            break;
+        case Nloopstmt:
+            nodetag(n->loopstmt.init);
+            nodetag(n->loopstmt.cond);
+            nodetag(n->loopstmt.step);
+            nodetag(n->loopstmt.body);
+            break;
+        case Nmatchstmt:
+            nodetag(n->matchstmt.val);
+            for (i = 0; i < n->matchstmt.nmatches; i++)
+                nodetag(n->matchstmt.matches[i]);
+            break;
+        case Nmatch:
+            nodetag(n->match.pat);
+            nodetag(n->match.block);
+            break;
+        case Nexpr:
+            taghidden(n->expr.type);
+            for (i = 0; i < n->expr.nargs; i++)
+                nodetag(n->expr.args[i]);
+            break;
+        case Nlit:
+            taghidden(n->lit.type);
+            switch (n->lit.littype) {
+                case Lfunc: nodetag(n->lit.fnval); break;
+                case Lseq:
+                    for (i = 0; i < n->lit.nelt; i++)
+                        nodetag(n->lit.seqval[i]);
+                    break;
+                default:
+                    break;
+            }
+            break;
+        case Ndecl:
+            taghidden(n->decl.type);
+            /* generics export their body. */
+            if (n->decl.isgeneric)
+                nodetag(n->decl.init);
+            break;
+        case Nfunc:
+            taghidden(n->func.type);
+            for (i = 0; i < n->func.nargs; i++)
+                nodetag(n->func.args[i]);
+            nodetag(n->func.body);
+
+        case Nuse: case Nname:
+            break;
+        case Nfile: case Nnone:
+            die("Invalid node for type export\n");
+            break;
+    }
+}
+
+static void tagexports(Stab *st)
+{
     void **k;
+    Node *s;
     Type *t;
     size_t i, j, n;
 
+    /* get the explicitly exported symbols */
     k = htkeys(st->ty, &n);
     for (i = 0; i < n; i++) {
         t = gettype(st, k[i]);
@@ -667,6 +783,12 @@
             taghidden(t->sub[j]);
     }
     free(k);
+
+    k = htkeys(st->dcl, &n);
+    for (i = 0; i < n; i++) {
+        s = getdcl(st, k[i]);
+        nodetag(s);
+    }
 }
 
 
@@ -693,11 +815,11 @@
 
     tagexports(st);
     for (i = 0; i < ntypes; i++) {
-        if (types[i]->vis != Visexport)
-            continue;
-        assert(types[i]->type == Tyname || types[i]->type == Tygeneric);
-        wrbyte(f, 'T');
-        typickle(types[i], f);
+        if (types[i]->vis == Visexport || types[i]->vis == Vishidden) {
+            wrbyte(f, 'T');
+            wrint(f, types[i]->tid);
+            typickle(f, types[i]);
+        }
     }
     k = htkeys(st->dcl, &n);
     for (i = 0; i < n; i++) {