main type.c
  1#include "util.h"
  2#include "kav.h"
  3
  4struct type *
  5mktype(enum typekind kind, struct binding *b) {
  6	struct type *t;
  7
  8	t = zalloc(sizeof(*t));
  9	t->kind = kind;
 10	t->binding = b;
 11	return t;
 12}
 13
 14static int
 15occurs(struct type *m, struct type *t) {
 16	struct type *a;
 17
 18	t = deref(t);
 19	if(t == m)
 20		return 1;
 21	if(t->ret && occurs(m, t->ret))
 22		return 1;
 23	for(a = t->args; a; a = a->next)
 24		if(occurs(m, a))
 25			return 1;
 26	return 0;
 27}
 28
 29static int
 30borrowed(struct type *t) {
 31	while(t) {
 32		if(t->borrow)
 33			return 1;
 34		t = t->kind == YMETA ? t->link : nil;
 35	}
 36	return 0;
 37}
 38
 39int
 40unify(struct type *a, struct type *b) {
 41	struct type *x;
 42	struct type *y;
 43
 44	a = deref(a);
 45	b = deref(b);
 46	if(a == b)
 47		return 1;
 48	if(a->kind == YMETA) {
 49		if(occurs(a, b))
 50			return 0;
 51		a->link = b;
 52		return 1;
 53	}
 54	if(b->kind == YMETA) {
 55		if(occurs(b, a))
 56			return 0;
 57		b->link = a;
 58		return 1;
 59	}
 60	if(a->kind != b->kind)
 61		return 0;
 62	if((a->kind == YNAME || a->kind == YVAR) && a->binding != b->binding)
 63		return 0;
 64	if(a->kind == YFN && !unify(a->ret, b->ret))
 65		return 0;
 66	for(x = a->args, y = b->args; x && y; x = x->next, y = y->next) {
 67		if(!unify(x, y))
 68			return 0;
 69		/* a function that borrows a param isn't the same as one that takes it */
 70		if(a->kind == YFN && borrowed(x) != borrowed(y))
 71			return 0;
 72	}
 73	return (!x && !y);
 74}
 75
 76
 77static void
 78typecat(stralloc *sa, struct type *t) {
 79	struct type *a;
 80
 81	t = deref(t);
 82	if(t->borrow)
 83		sacatl(sa, "!");
 84	switch(t->kind) {
 85	case YNAME:
 86	case YVAR:
 87		sacatb(sa, t->binding->name.str, t->binding->name.length);
 88		if(!t->args)
 89			return;
 90		sacatl(sa, "[");
 91		break;
 92	case YTUPLE:
 93		sacatl(sa, "(");
 94		break;
 95	case YFN:
 96		sacatl(sa, "fn(");
 97		break;
 98	case YMETA:
 99		sacatl(sa, "?");
100		return;
101	}
102	for(a = t->args; a; a = a->next) {
103		typecat(sa, a);
104		if(a->next)
105			sacatl(sa, ", ");
106	}
107	if(t->kind == YFN) {
108		sacatl(sa, ") -> ");
109		typecat(sa, t->ret);
110		return;
111	}
112	sacatb(sa, t->kind == YTUPLE ? ")" : "]", 1);
113}
114
115char *
116typestr(struct type *t) {
117	stralloc sa = {0};
118
119	typecat(&sa, t);
120	sa0(&sa);
121	return sa.s;
122}
123
124struct type *
125mkfntype(struct type *params, struct type *ret) {
126	struct type *t;
127
128	t = mktype(YFN, nil);
129	t->args = params;
130	t->ret = ret;
131	return t;
132}
133
134/* copy of t that can be linked into a new list without touching t->next */
135struct type *
136tydup(struct type *t) {
137	struct type *u;
138
139	if(t->kind == YMETA) {
140		u = mkmeta();
141		u->link = t;
142		return u;
143	}
144	u = mktype(t->kind, t->binding);
145	u->args = t->args;
146	u->ret = t->ret;
147	u->borrow = t->borrow;
148	return u;
149}
150
151struct type *
152mkmeta(void) {
153	return mktype(YMETA, nil);
154}
155
156struct type *
157deref(struct type *t) {
158	while(t->kind == YMETA && t->link)
159		t = t->link;
160	return t;
161}
162
163/* one of the builtin integer types */
164int
165isint(struct type *t) {
166	t = deref(t);
167	if(t->kind != YNAME)
168		return 0;
169	return t->binding == bint || t->binding == bi8 || t->binding == bi16 || t->binding == bi32 ||
170	       t->binding == bu8 || t->binding == bu16 || t->binding == bu32 || t->binding == bu64;
171}
172
173u64
174intmax(struct type *t) {
175	t = deref(t);
176	if(t->binding == bi8)
177		return INT8_MAX;
178	if(t->binding == bu8)
179		return UINT8_MAX;
180	if(t->binding == bi16)
181		return INT16_MAX;
182	if(t->binding == bu16)
183		return UINT16_MAX;
184	if(t->binding == bi32)
185		return INT32_MAX;
186	if(t->binding == bu32)
187		return UINT32_MAX;
188	if(t->binding == bu64)
189		return UINT64_MAX;
190	return INT64_MAX;
191}
192
193/* u64 is the only type that needs unsigned compares and division */
194int
195isu64(struct type *t) {
196	return deref(t)->binding == bu64;
197}
198
199i64
200wrap(struct type *t, i64 n) {
201	t = deref(t);
202	if(t->binding == bi8)
203		return (i8)n;
204	if(t->binding == bi16)
205		return (i16)n;
206	if(t->binding == bi32)
207		return (i32)n;
208	if(t->binding == bu8)
209		return (u8)n;
210	if(t->binding == bu16)
211		return (u16)n;
212	if(t->binding == bu32)
213		return (u32)n;
214	/* int and u64 are already 64 bits */
215	return n;
216}
217
218/* a copy of t with each param in from replaced by the matching type in to */
219struct type *
220subst(struct type *t, struct param *from, struct type *to) {
221	struct type *a;
222	struct type *b;
223	struct type **link;
224	struct param *p;
225
226	t = deref(t);
227	switch(t->kind) {
228	case YVAR:
229		for(p = from, b = to; p && b; p = p->next, b = b->next)
230			if(p->binding == t->binding) {
231				a = tydup(b);
232				if(t->borrow)
233					a->borrow = 1;
234				return a;
235			}
236		return mktype(YVAR, t->binding);
237	case YMETA:
238		return tydup(t);
239	case YNAME:
240	case YTUPLE:
241	case YFN:
242		a = mktype(t->kind, t->binding);
243		a->borrow = t->borrow;
244		link = &a->args;
245		for(b = t->args; b; b = b->next) {
246			*link = subst(b, from, to);
247			link = &(*link)->next;
248		}
249		if(t->ret)
250			a->ret = subst(t->ret, from, to);
251		return a;
252	}
253	return t;
254}