ref: 134217a2c718527a3e38abd5a60467ecb586fea9
parent: 63398e32ed2d5b8e75e26caff58e16646668733f
author: Ori Bernstein <[email protected]>
date: Thu Jun 21 20:07:59 EDT 2012
Properly duplicate types when freshening. The old method didn't allow frobbing all cloned bits, and would have caused conflicts with the tids, which are supposed to be unique.
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -62,12 +62,9 @@
return ret;
}
- ret = zalloc(sizeof(Type));
- *ret = *t;
- ret->sub = zalloc(t->nsub * sizeof(Type *));
+ ret = tydup(t);
for (i = 0; i < t->nsub; i++)
ret->sub[i] = tyfreshen(ht, t->sub[i]);
- printf("Freshened %s to %s\n", tystr(t), tystr(ret));
return ret;
}
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -287,6 +287,7 @@
void tyinit(Stab *st); /* sets up built in types */
Type *mkty(int line, Ty ty);
+Type *tydup(Type *t); /* shallow duplicate; all subtypes/members/... kept */
Type *mktyvar(int line);
Type *mktyparam(int line, char *name);
Type *mktynamed(int line, Node *name);
--- a/parse/type.c
+++ b/parse/type.c
@@ -42,6 +42,27 @@
return t;
}
+Type *tydup(Type *t)
+{
+ Type *r;
+
+ r = mkty(t->line, t->type);
+ r->resolved = 0; /* re-resolving doesn't hurt */
+ r->cstrs = bsdup(t->cstrs);
+ r->nsub = t->nsub;
+ r->nmemb = t->nmemb;
+ r->sub = memdup(t->sub, t->nsub * sizeof(Type*));
+ switch (t->type) {
+ case Tyname: r->name = t->name; break;
+ case Tyarray: r->asize = t->asize; break;
+ case Typaram: r->pname = strdup(t->pname); break;
+ case Tystruct: r->sdecls = memdup(t->sdecls, t->nmemb*sizeof(Node*)); break;
+ case Tyunion: r->udecls = memdup(t->udecls, t->nmemb*sizeof(Node*)); break;
+ default: break;
+ }
+ return r;
+}
+
Type *tylike(Type *t, Ty like)
{
int i;