shithub: mc

Download patch

ref: 89c2d615ca55d511bd394607534091b78cfe82b4
parent: 0584162d2da919d3ef7037f007021abbf793cfd3
author: Ori Bernstein <[email protected]>
date: Wed Aug 21 06:17:43 EDT 2013

Remove 'Tygeneric' in favor of 'isgeneric' attr

    Tygeneric behaved exactly the same as tyname, *except* that it
    needed to be specialized.

--- a/6/simp.c
+++ b/6/simp.c
@@ -302,7 +302,7 @@
                     sz = max(sz, tysize(t->udecls[i]->etype) + Wordsz);
             return align(sz, Ptrsz);
             break;
-        case Tybad: case Tyvar: case Typaram: case Tyunres: case Tygeneric: case Ntypes:
+        case Tybad: case Tyvar: case Typaram: case Tyunres: case Ntypes:
             die("Type %s does not have size; why did it get down to here?", tystr(t));
             break;
     }
@@ -482,7 +482,7 @@
     }
     switch (t->type) {
         case Tyvoid: case Tybad: case Tyvalist: case Tyvar:
-        case Tygeneric: case Typaram: case Tyunres: case Tyname: case Ntypes:
+        case Typaram: case Tyunres: case Tyname: case Ntypes:
         case Tyint64: case Tyuint64: case Tylong:  case Tyulong:
         case Tyfloat32: case Tyfloat64:
         case Tyslice: case Tyarray: case Tytuple: case Tystruct:
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -182,8 +182,8 @@
     size_t i;
 
     switch (t->type) {
-        case Tygeneric: return 1;
         case Typaram:   return 1;
+        case Tyname:    return t->isgeneric;
         case Tystruct:
             for (i = 0; i < t->nmemb; i++)
                 if (isgeneric(st, decltype(t->sdecls[i])))
@@ -216,7 +216,7 @@
         return t;
 
     tybind(st, t);
-    ht = mkht(tyhash, streq);
+    ht = mkht(tyhash, tyeq);
     t = tyspecialize(t, ht);
     htfree(ht);
     tyunbind(st, t);
@@ -289,11 +289,9 @@
             break;
         t = tytab[t->tid];
     }
-    if (t->type == Tygeneric)
-        st->ingeneric++;
+    st->ingeneric += t->isgeneric;
     tyresolve(st, t);
-    if (t->type == Tygeneric)
-        st->ingeneric--;
+    st->ingeneric -= t->isgeneric;
     return t;
 }
 
@@ -404,9 +402,8 @@
 {
     Htab *bt;
 
-    if (t->type != Tygeneric)
+    if (t->type != Tyname && !t->isgeneric)
         return;
-    st->ingeneric++;
     bt = mkht(strhash, streq);
     lappend(&st->tybindings, &st->ntybindings, bt);
     putbindings(st, bt, t);
@@ -445,7 +442,7 @@
 
 static void tyunbind(Inferstate *st, Type *t)
 {
-    if (t->type != Tygeneric)
+    if (t->type != Tyname && !t->isgeneric)
         return;
     htfree(st->tybindings[st->ntybindings - 1]);
     lpop(&st->tybindings, &st->ntybindings);
@@ -538,7 +535,7 @@
 
 static int hasparam(Type *t)
 {
-    return t->type == Tygeneric || t->type == Tyname;
+    return t->type == Tyname && t->nparam > 0;
 }
 
 /* Unifies two types, or errors if the types are not unifiable. */
@@ -1095,7 +1092,7 @@
     Type *t;
 
     t = tf(st, decltype(n));
-    if (t->type == Tygeneric) {
+    if (t->type == Tyname && t->isgeneric && !n->decl.isgeneric) {
         t = tyfreshen(st, t);
         unifyparams(st, n, t, decltype(n));
     }
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -113,10 +113,11 @@
     Node **cstrlist;    /* The names of the constraints on the type. Used to fill the bitset */
     size_t ncstrlist;   /* The length of the constraint list above */
 
-    Type **param;       /* Tygeneric: the type parameters captured */
-    size_t nparam;      /* Tygeneric: count of type parameters */
-    Type **inst;        /* Tygeneric: instances created */
-    size_t ninst;       /* Tygeneric: count of instances created */
+    int	 isgeneric;	/* Tyname: whether this is generic or not */
+    Type **param;       /* Tyname: the type parameters captured */
+    size_t nparam;      /* Tyname: count of type parameters */
+    Type **inst;        /* Tyname: instances created */
+    size_t ninst;       /* Tyname: count of instances created */
 
     Type **sub;         /* sub-types; shared by all composite types */
     size_t nsub;        /* For compound types */
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -23,7 +23,7 @@
 
     if (t->type == Typaram)
         return 1;
-    if (t->type == Tygeneric)
+    if (t->type == Tyname && t->isgeneric)
         return 1;
     for (i = 0; i < t->nsub; i++)
         if (hasparams(t->sub[i]))
@@ -35,8 +35,8 @@
  * Duplicates the type 't', with all bound type
  * parameters substituted with the substitions
  * described in 'tsmap'
- */
-/* Returns a fresh type with all unbound type
+ *
+ * Returns a fresh type with all unbound type
  * parameters (type schemes in most literature)
  * replaced with type variables that we can unify
  * against */
@@ -52,14 +52,18 @@
             ret = mktyvar(t->line);
             htput(tsmap, t, ret);
             break;
-        case Tygeneric:
-            for (i = 0; i < t->nparam; i++)
-                if (!hthas(tsmap, t->param[i]))
-                    htput(tsmap, t->param[i], mktyvar(t->param[i]->line));
-            ret = mktyname(t->line, t->name, tyspecialize(t->sub[0], tsmap));
-            htput(tsmap, t, ret);
-            for (i = 0; i < t->nparam; i++)
-                lappend(&ret->param, &ret->nparam, tyspecialize(t->param[i], tsmap));
+        case Tyname:
+	    if (!t->isgeneric) {
+		ret = t;
+	    } else {
+		for (i = 0; i < t->nparam; i++)
+		    if (!hthas(tsmap, t->param[i]))
+			htput(tsmap, t->param[i], mktyvar(t->param[i]->line));
+		ret = mktyname(t->line, t->name, tyspecialize(t->sub[0], tsmap));
+		htput(tsmap, t, ret);
+		for (i = 0; i < t->nparam; i++)
+		    lappend(&ret->param, &ret->nparam, tyspecialize(t->param[i], tsmap));
+	    }
             break;
         case Tystruct:
             ret = tydup(t);
--- a/parse/type.c
+++ b/parse/type.c
@@ -73,8 +73,7 @@
     r->nsub = t->nsub;
     r->nmemb = t->nmemb;
     switch (t->type) {
-        case Tygeneric:   r->name = t->name;              break;
-        case Tyname:   r->name = t->name;              break;
+        case Tyname:   	r->name = t->name;              break;
         case Tyunres:   r->name = t->name;              break;
         case Tyarray:   r->asize = t->asize;            break;
         case Typaram:   r->pname = strdup(t->pname);    break;
@@ -151,14 +150,10 @@
 {
     Type *t;
 
-    t = mktype(line, Tygeneric);
-    t->name = name;
-    t->nsub = 1;
-    t->cstrs = bsdup(base->cstrs);
-    t->sub = xalloc(sizeof(Type*));
-    t->sub[0] = base;
+    t = mktyname(line, name, base);
     t->param = param;
     t->nparam = nparam;
+    t->isgeneric = 1;
     return t;
 }
 
@@ -284,7 +279,7 @@
 Type *tybase(Type *t)
 {
     assert(t != NULL);
-    while (t->type == Tyname || t->type == Tygeneric)
+    while (t->type == Tyname)
         t = t->sub[0];
     return t;
 }
@@ -474,7 +469,6 @@
             }
             break;
         case Tyname:  
-        case Tygeneric:  
             p += snprintf(p, end - p, "%s", namestr(t->name));
             if (t->nparam) {
                 p += snprintf(p, end - p, "(");
--- a/parse/types.def
+++ b/parse/types.def
@@ -43,4 +43,3 @@
 Ty(Typaram, NULL)
 Ty(Tyunres, NULL) /* unresolved */
 Ty(Tyname, NULL)
-Ty(Tygeneric, NULL)
--- a/parse/use.c
+++ b/parse/use.c
@@ -214,13 +214,6 @@
                 wrtype(fd, ty->param[i]);
             wrtype(fd, ty->sub[0]);
             break;
-        case Tygeneric:
-            pickle(ty->name, fd);
-            wrint(fd, ty->nparam);
-            for (i = 0; i < ty->nparam; i++)
-                wrtype(fd, ty->param[i]);
-            wrtype(fd, ty->sub[0]);
-            break;
         default:
             for (i = 0; i < ty->nsub; i++)
                 wrtype(fd, ty->sub[i]);
@@ -301,14 +294,6 @@
                 rdtype(fd, &ty->param[i]);
             rdtype(fd, &ty->sub[0]);
             break;
-        case Tygeneric:
-            ty->name = unpickle(fd);
-            ty->nparam = rdint(fd);
-            ty->param = zalloc(ty->nparam * sizeof(Type *));
-            for (i = 0; i < ty->nparam; i++)
-                rdtype(fd, &ty->param[i]);
-            rdtype(fd, &ty->sub[0]);
-            break;
         default:
             for (i = 0; i < ty->nsub; i++)
                 rdtype(fd, &ty->sub[i]);
@@ -645,7 +630,7 @@
                 t = tyunpickle(f);
                 htput(tidmap, (void*)tid, t);
                 /* fix up types */
-                if (t->type == Tyname || t->type == Tygeneric)
+                if (t->type == Tyname)
                     if (!gettype(s, t->name))
                         puttype(s, t->name, t);
                 if (t->type == Tyunion)  {