shithub: mc

Download patch

ref: 490e90d2122d80bce4e5f710e046b90fc9cc31ca
parent: 8e1bb5d4fc204ce149ceb545aef7b783d73641bc
author: Ori Bernstein <[email protected]>
date: Wed Jan 17 17:18:26 EST 2018

Hack in seqaux.

	It's not perfect, and the right solution would be to keep a
	trait->aux mapping, but for now it works good enough.

--- a/mbld/libs.myr
+++ b/mbld/libs.myr
@@ -22,7 +22,7 @@
 		incs : byte[:][:] -> void)
 ;;
 
-const Abiversion = 15
+const Abiversion = 16
 
 const builtlib = {b, mt, dep, dyndep
 	var ldep, l, u
--- a/parse/export.c
+++ b/parse/export.c
@@ -74,6 +74,7 @@
 	if (!t || t->vis != Visintern)
 		return;
 	t->vis = Vishidden;
+	tagtype(st, t->seqaux, ingeneric, hidelocal);
 	for (i = 0; i < t->nsub; i++)
 		tagtype(st, t->sub[i], ingeneric, hidelocal);
 	for (i = 0; i < t->nspec; i++) {
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -69,7 +69,6 @@
 static Stab **specializationscope;
 static size_t nspecializationscope;
 static Traitmap *traitmap;
-static Htab *seqbase;
 
 static void
 ctxstrcall(char *buf, size_t sz, Node *n)
@@ -472,16 +471,16 @@
 	pushenv(orig->env);
 	if (!subst) {
 		subst = mksubst();
-		ty = tyspecialize(orig, subst, delayed, seqbase);
+		ty = tyspecialize(orig, subst, delayed);
 		substfree(subst);
 	} else {
-		ty = tyspecialize(orig, subst, delayed, seqbase);
+		ty = tyspecialize(orig, subst, delayed);
 	}
 	ty->spec = orig->spec;
 	ty->nspec = orig->nspec;
 	base = basetype(ty);
 	if (base)
-		htput(seqbase, ty, base);
+		ty->seqaux = base;
 	popenv(orig->env);
 	return ty;
 }
@@ -543,7 +542,7 @@
 				t->trneed = mkbs();
 			bsput(t->trneed, tr->uid);
 			if (nameeq(t->spec[i]->trait[j], traittab[Tciter]->name))
-				htput(seqbase, t, t->spec[i]->aux);
+				t->seqaux = t->spec[i]->aux;
 		}
 	}
 
@@ -1027,7 +1026,7 @@
 {
 	if (t->type == Tyvar) {
 		/* has associated iterator type */
-		if (hthas(seqbase, t))
+		if (t->seqaux)
 			return 1;
 		else
 			return 0;
@@ -1118,10 +1117,10 @@
 {
 	Type *t;
 
-	t = htget(seqbase, a);
+	t = a->seqaux;
 	while (!t && a->type == Tyname) {
 		a = a->sub[0];
-		t = htget(seqbase, a);
+		t = a->seqaux;
 	}
 	if (!t && (a->type == Tyslice || a->type == Tyarray || a->type == Typtr))
 		t = a->sub[0];
@@ -1732,7 +1731,7 @@
 		infersub(n, ret, sawret, &isconst);
 		b = mktyvar(n->loc);
 		t = mktyvar(n->loc);
-		htput(seqbase, t, b);
+		t->seqaux = b;
 		unify(n, type(args[0]), t);
 		constrain(n, type(args[0]), traittab[Tcidx]);
 		constrain(n, type(args[1]), traittab[Tcint]);
@@ -1742,7 +1741,7 @@
 		infersub(n, ret, sawret, &isconst);
 		b = mktyvar(n->loc);
 		t = mktyvar(n->loc);
-		htput(seqbase, t, b);
+		t->seqaux = b;
 		unify(n, type(args[0]), t);
 		constrain(n, type(args[1]), traittab[Tcint]);
 		constrain(n, type(args[1]), traittab[Tcnum]);
@@ -1956,7 +1955,7 @@
 		substput(subst, tr->param, n->impl.type);
 		for (j = 0; j < tr->naux; j++)
 			substput(subst, tr->aux[j], n->impl.aux[j]);
-		ty = tyspecialize(type(proto), subst, delayed, NULL);
+		ty = tyspecialize(type(proto), subst, delayed);
 		substfree(subst);
 		popenv(proto->decl.env);
 
@@ -2125,7 +2124,7 @@
 		if (b)
 			unify(n, e, b);
 		else
-			htput(seqbase, t, e);
+			t->seqaux = e;
 		delayedcheck(n, curstab());
 		break;
 	case Nmatchstmt:
@@ -2190,7 +2189,7 @@
 	env = t->env;
 	if (env)
 		pushenv(env);
-	base = htget(seqbase, orig);
+	base = orig->seqaux;
 	if (orig->type == Tyvar && hthas(delayed, orig)) {
 		d = htget(delayed, orig);
 		if (t->type == Tyvar) {
@@ -2235,7 +2234,7 @@
 	if (t->type == Tyvar && !noerr)
 		fatal(ctx, "underconstrained type %s near %s", tyfmt(buf, 1024, t), ctxstr(ctx));
 	if (base)
-		htput(seqbase, t, tyfix(ctx, base, noerr));
+		t->seqaux = tyfix(ctx, base, noerr);
 	if (env)
 		popenv(env);
 	return t;
@@ -2380,7 +2379,7 @@
 	Node *impl;
 
 	ty = tysearch(ty);
-	b = htget(seqbase, ty);
+	b = ty->seqaux;
 	if (!b)
 		return;
 	bestrank = -1;
@@ -2898,7 +2897,6 @@
 	Type *ty;
 
 	pushstab(file->file.globls);
-	seqbase = mkht(tyhash, tyeq);
 	traitmap = zalloc(sizeof(Traitmap));
 	builtintraits();
 	for (i = 0; i < nimpltab; i++) {
@@ -2908,9 +2906,8 @@
 		pushenv(impl->impl.env);
 		ty = tf(impl->impl.type);
 		addtraittab(traitmap, tr, ty);
-		if (tr->uid == Tciter) {
-			htput(seqbase, tf(impl->impl.type), tf(impl->impl.aux[0]));
-		}
+		if (tr->uid == Tciter)
+			ty->seqaux = tf(impl->impl.aux[0]);
 		popenv(impl->impl.env);
 	}
 	popstab();
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -1,4 +1,4 @@
-#define Abiversion 15
+#define Abiversion 16
 
 typedef struct Srcloc Srcloc;
 typedef struct Tysubst Tysubst;
@@ -135,8 +135,7 @@
 
 	Traitspec **spec;
 	size_t nspec;
-	//Node **traits;		/* trait list */
-	//size_t ntraits;		/* trait list size */
+	Type *seqaux;
 
 	Type **gparam;		/* Tygeneric: type parameters that match the type args */
 	size_t ngparam;		/* Tygeneric: count of type parameters */
@@ -544,7 +543,7 @@
 void substput(Tysubst *subst, Type *from, Type *to);
 Type *substget(Tysubst *subst, Type *from);
 Node *specializedcl(Node *n, Type *param, Type *to, Node **name);
-Type *tyspecialize(Type *t, Tysubst *tymap, Htab *delayed, Htab *tybase);
+Type *tyspecialize(Type *t, Tysubst *tymap, Htab *delayed);
 Node *genericname(Node *n, Type *param, Type *t);
 void geninit(void);
 
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -82,9 +82,9 @@
  * replaced with type variables that we can unify
  * against */
 Type *
-tyspecialize(Type *orig, Tysubst *tsmap, Htab *delayed, Htab *trbase)
+tyspecialize(Type *orig, Tysubst *tsmap, Htab *delayed)
 {
-	Type *t, *ret, *tmp, *var, *base;
+	Type *t, *ret, *tmp, *var;
 	Traitspec *ts;
 	size_t i, narg;
 	Type **arg;
@@ -104,16 +104,11 @@
 			ts = zalloc(sizeof(Traitspec));
 			ts->trait = t->spec[i]->trait;
 			ts->ntrait = t->spec[i]->ntrait;
-			ts->param = tyspecialize(t->spec[i]->param, tsmap, delayed, trbase);
+			ts->param = tyspecialize(t->spec[i]->param, tsmap, delayed);
 			if (t->spec[i]->aux)
-				ts->aux = tyspecialize(t->spec[i]->aux, tsmap, delayed, trbase);
+				ts->aux = tyspecialize(t->spec[i]->aux, tsmap, delayed);
 			lappend(&ret->spec, &ret->nspec, ts);
 		}
-		tmp = htget(trbase, t);
-		if (tmp) {
-			tmp = tyspecialize(tmp, tsmap, delayed, trbase);
-			htput(trbase, ret, tmp);
-		}
 		break;
 	case Tygeneric:
 		var = mktyvar(t->loc);
@@ -124,8 +119,8 @@
 				substput(tsmap, t->gparam[i], orig->arg[i]);
 		}
 		for (i = 0; i < t->ngparam; i++)
-			lappend(&arg, &narg, tyspecialize(t->gparam[i], tsmap, delayed, trbase));
-		ret = mktyname(t->loc, t->name, tyspecialize(t->sub[0], tsmap, delayed, trbase));
+			lappend(&arg, &narg, tyspecialize(t->gparam[i], tsmap, delayed));
+		ret = mktyname(t->loc, t->name, tyspecialize(t->sub[0], tsmap, delayed));
 		if (orig->type == Tyunres)
 			substpop(tsmap);
 		ret->arg = arg;
@@ -138,14 +133,10 @@
 		var = mktyvar(t->loc);
 		substput(tsmap, t, var);
 		for (i = 0; i < t->narg; i++)
-			lappend(&arg, &narg, tyspecialize(t->arg[i], tsmap, delayed, trbase));
-		ret = mktyname(t->loc, t->name, tyspecialize(t->sub[0], tsmap, delayed, trbase));
+			lappend(&arg, &narg, tyspecialize(t->arg[i], tsmap, delayed));
+		ret = mktyname(t->loc, t->name, tyspecialize(t->sub[0], tsmap, delayed));
 		ret->arg = arg;
 		ret->narg = narg;
-		if (trbase && hthas(trbase, orig) && !hthas(trbase, ret)) {
-			base = htget(trbase, orig);
-			htput(trbase, ret, tyspecialize(base, tsmap, delayed, trbase));
-		}
 		tytab[var->tid] = ret;
 		break;
 	case Tystruct:
@@ -162,7 +153,7 @@
 		for (i = 0; i < t->nmemb; i++) {
 			tmp = NULL;
 			if (ret->udecls[i]->etype)
-				tmp = tyspecialize(t->udecls[i]->etype, tsmap, delayed, trbase);
+				tmp = tyspecialize(t->udecls[i]->etype, tsmap, delayed);
 			ret->udecls[i] = mkucon(t->loc, t->udecls[i]->name, ret, tmp);
 			ret->udecls[i]->utype = ret;
 			ret->udecls[i]->id = i;
@@ -173,7 +164,7 @@
 		ret = t;
 		if (delayed && hthas(delayed, t)) {
 			tmp = htget(delayed, t);
-			htput(delayed, ret, tyspecialize(tmp, tsmap, delayed, trbase));
+			htput(delayed, ret, tyspecialize(tmp, tsmap, delayed));
 		}
 		break;
 	default:
@@ -181,12 +172,14 @@
 			ret = tydup(t);
 			substput(tsmap, t, ret);
 			for (i = 0; i < t->nsub; i++)
-				ret->sub[i] = tyspecialize(t->sub[i], tsmap, delayed, trbase);
+				ret->sub[i] = tyspecialize(t->sub[i], tsmap, delayed);
 		} else {
 			ret = t;
 		}
 		break;
 	}
+	if (t->seqaux)
+		ret->seqaux = tyspecialize(t->seqaux, tsmap, delayed);
 	return ret;
 }
 
@@ -197,7 +190,7 @@
 tysubst(Type *t, Tysubst *tsmap)
 {
 	if (hasparams(t))
-		return tyspecialize(t, tsmap, NULL, NULL);
+		return tyspecialize(t, tsmap, NULL);
 	else
 		return t;
 }
--- a/parse/use.c
+++ b/parse/use.c
@@ -236,6 +236,7 @@
 		if (ty->spec[i]->aux)
 			wrtype(fd, ty->spec[i]->aux);
 	}
+	wrtype(fd, ty->seqaux);
 	wrint(fd, ty->nsub);
 	switch (ty->type) {
 	case Tyunres:
@@ -312,6 +313,10 @@
 static void
 wrtype(FILE *fd, Type *ty)
 {
+	if (!ty) {
+		wrint(fd, 0);
+		return;
+	}
 	if (ty->tid >= Builtinmask)
 		die("Type id %d for %s too big", ty->tid, tystr(ty));
 	if (ty->vis == Visbuiltin)
@@ -326,7 +331,9 @@
 	uintptr_t tid;
 
 	tid = rdint(fd);
-	if (tid & Builtinmask) {
+	if (tid == 0) {
+		return;
+	} else if (tid & Builtinmask) {
 		*dest = mktype(Zloc, tid & ~Builtinmask);
 	} else {
 		typefix = xrealloc(typefix, (ntypefix + 1) * sizeof(typefix[0]));
@@ -382,6 +389,7 @@
 		if (rdbool(fd))
 			rdtype(fd, &ty->spec[i]->aux);
 	}
+	rdtype(fd, &ty->seqaux);
 	ty->nsub = rdint(fd);
 	if (ty->nsub > 0)
 		ty->sub = zalloc(ty->nsub * sizeof(Type *));