shithub: mc

Download patch

ref: 8e1b57e6cb50118d28a86115520b7331cc0fdf23
parent: 16fb3943b8f4d15b91b133f01678d04793d0d138
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Feb 27 18:29:40 EST 2016

Add ability to deduplicate types.

--- a/parse/parse.h
+++ b/parse/parse.h
@@ -142,6 +142,7 @@
 	char ispkglocal; /* Tyname: whether this is package local or not */
 	char isimport;   /* Tyname: whether tyis type was imported. */
 	char isreflect;  /* Tyname: whether this type has reflection info */
+	char isemitted;  /* Tyname: whether this type has been emitted */
 };
 
 struct Ucon {
@@ -420,6 +421,7 @@
 
 Type *mktype(Srcloc l, Ty ty);
 Type *tydup(Type *t); /* shallow duplicate; all subtypes/members/... kept */
+Type *tydedup(Type *t);
 Type *mktyvar(Srcloc l);
 Type *mktyparam(Srcloc l, char *name);
 Type *mktygeneric(Srcloc l, Node *name, Type **params, size_t nparams, Type *base);
--- a/parse/type.c
+++ b/parse/type.c
@@ -28,6 +28,7 @@
 Node **impltab;
 size_t nimpltab;
 
+static Htab *tydeduptab;
 /* Built in type constraints */
 static Trait *traits[Ntypes + 1][4];
 static int tybfmt(char *buf, size_t len, Type *t);
@@ -40,6 +41,20 @@
 
 int isstacktype(Type *t) { return stackness[tybase(t)->type]; }
 
+Type *tydedup(Type *ty)
+{
+	Type *had;
+
+	had = htget(tydeduptab, ty);
+	if (!had) {
+		htput(tydeduptab, ty, ty);
+		return ty;
+	}
+	/* if one is emitted, both are */
+	ty->isemitted = ty->isemitted || had->isemitted;
+	return had;
+}
+
 Type *mktype(Srcloc loc, Ty ty)
 {
 	Type *t;
@@ -644,11 +659,6 @@
 
 	t = (Type *)ty;
 	switch (t->type) {
-		/* Important: we want tyhash to be consistent cross-file, since it
-		 * is used in naming trait impls and such.
-		 *
-		 * We should find a better name.
-		 */
 	case Tyvar:	hash = inthash(t->tid);	break;
 	case Typaram:	hash = strhash(t->pname);	break;
 	case Tyunion:	hash = inthash(t->type);	break;
@@ -893,6 +903,7 @@
 	Type *ty;
 	Trait *tr;
 
+	tydeduptab = mkht(tyhash, tyeq);
 	/* this must be done after all the types are created, otherwise we will
 	 * clobber the memoized bunch of types with the type params. */
 #define Tc(c, n) \
@@ -948,6 +959,7 @@
 		ty = mktype(Zloc, t);	\
 		if (n) {	\
 			puttype(st, mkname(Zloc, n), ty);	\
+			htput(tydeduptab, ty, ty); \
 		}	\
 	}
 #include "types.def"