ref: 3cbdb83dbbc80b2c8a3458e5b1a1ae5d3080fb3b
parent: 8c7095cc5e7701e9e9f6f17d8dd60e32c2dc5bd0
parent: 1ef5877bce5a4fcbe6e68b00f1ba087384d6343c
author: Ori Bernstein <[email protected]>
date: Wed Aug 21 07:24:35 EDT 2013
Merge branch 'master' of git+ssh://git.eigenstate.org/git/ori/mc
--- 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/gram.y
+++ b/parse/gram.y
@@ -269,12 +269,13 @@
tydef : Ttype typeid Tasn type
{$$ = $2;
- if ($2.params)
- $$.type = mktygeneric($2.line, mkname($2.line, $2.name), $2.params, $2.nparams, $4);
- else
- $$.type = mktyname($2.line, mkname($2.line, $2.name), $4);}
+ $$.type = mktyname($2.line, mkname($2.line, $2.name), $2.params, $2.nparams, $4);
+ if ($2.params)
+ $$.type->isgeneric = 1;}
| Ttype typeid
- {$$ = $2;}
+ {$$ = $2;
+ if ($2.params)
+ $$.type->isgeneric = 1;}
;
typeid : Tident
--- 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;
}
@@ -398,8 +396,6 @@
htput(bt, t->pname, t);
for (i = 0; i < t->nparam; i++)
putbindings(st, bt, t->param[i]);
- for (i = 0; i < t->nsub; i++)
- putbindings(st, bt, t->sub[i]);
}
static void tybind(Inferstate *st, Type *t)
@@ -406,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);
@@ -447,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);
@@ -540,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. */
@@ -1097,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 */
@@ -352,8 +353,7 @@
Type *tydup(Type *t); /* shallow duplicate; all subtypes/members/... kept */
Type *mktyvar(int line);
Type *mktyparam(int line, char *name);
-Type *mktyname(int line, Node *name, Type *base);
-Type *mktygeneric(int line, Node *name, Type **params, size_t nparams, Type *base);
+Type *mktyname(int line, Node *name, Type **params, size_t nparams, Type *base);
Type *mktyunres(int line, Node *name, Type **params, size_t nparams);
Type *mktyarray(int line, Type *base, Node *sz);
Type *mktyslice(int line, Type *base);
--- 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, NULL, 0, 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;
@@ -147,11 +146,11 @@
return t;
}
-Type *mktygeneric(int line, Node *name, Type **param, size_t nparam, Type *base)
+Type *mktyname(int line, Node *name, Type **param, size_t nparam, Type *base)
{
Type *t;
- t = mktype(line, Tygeneric);
+ t = mktype(line, Tyname);
t->name = name;
t->nsub = 1;
t->cstrs = bsdup(base->cstrs);
@@ -162,19 +161,6 @@
return t;
}
-Type *mktyname(int line, Node *name, Type *base)
-{
- Type *t;
-
- t = mktype(line, Tyname);
- t->name = name;
- t->nsub = 1;
- t->cstrs = bsdup(base->cstrs);
- t->sub = xalloc(sizeof(Type*));
- t->sub[0] = base;
- return t;
-}
-
Type *mktyarray(int line, Type *base, Node *sz)
{
Type *t;
@@ -284,7 +270,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 +460,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) {