bitwise check.c
  1#include "util.h"
  2#include "kav.h"
  3
  4struct blank {
  5	struct blank *next;
  6	struct type *meta;
  7	struct location loc;
  8	int unrestricted;
  9};
 10
 11static struct blank *blanks;
 12
 13static int
 14istypesort(struct expr *ann) {
 15	return ann->kind == EIDENT && (ann->binding == btype || ann->binding == butype);
 16}
 17
 18static struct type *totype(struct expr *);
 19
 20static struct type *
 21expandtypedef(struct expr *e) {
 22	struct decl *d = e->binding->decl;
 23	struct type *to = nil;
 24	struct type **link = &to;
 25	struct type *t;
 26	struct param *sp;
 27	struct expr *x;
 28
 29	x = e->kind == ESAPP ? e->args : nil;
 30	for(sp = d->sparams; sp && x; sp = sp->next, x = x->next) {
 31		*link = istypesort(sp->ann) ? totype(x) : mkmeta();
 32		link = &(*link)->next;
 33	}
 34	if(sp || x)
 35		errorat(&e->tok.loc, "typedef '", tokdup(d->name), "' expects ", sp ? "more" : "fewer", " arguments");
 36	if(d->expanding)
 37		errorat(&e->tok.loc, "typedef '", tokdup(d->name), "' refers to itself");
 38	d->expanding = 1;
 39	t = totype(d->ret);
 40	d->expanding = 0;
 41	return subst(t, d->sparams, to);
 42}
 43
 44static struct type *
 45totype(struct expr *e) {
 46	struct type *t;
 47	struct type *a;
 48	struct type *head = nil;
 49	struct type **end = &head;
 50	struct expr *x;
 51	struct param *sp;
 52
 53	if((e->kind == EIDENT || e->kind == ESAPP) && e->binding->decl && e->binding->decl->kind == DTYPEDEF)
 54		return expandtypedef(e);
 55	switch(e->kind) {
 56	case EIDENT:
 57		if(e->binding->kind == BPARAM)
 58			return mktype(YVAR, e->binding);
 59		else
 60			return mktype(YNAME, e->binding);
 61	case ETUPLE:
 62		t = mktype(YTUPLE, nil);
 63		for(x = e->args; x; x = x->next) {
 64			a = totype(x);
 65			*end = a;
 66			end = &a->next;
 67		}
 68		t->args = head;
 69		return t;
 70	case ESAPP:
 71		t = mktype(YNAME, e->binding);
 72		/* walk the type's sort params alongside, builtin types have no decl */
 73		sp = e->binding->decl ? e->binding->decl->sparams : nil;
 74		for(x = e->args; x; x = x->next, sp = sp ? sp->next : nil) {
 75			/* solver later */
 76			if(sp && !istypesort(sp->ann))
 77				continue;
 78			a = totype(x);
 79			*end = a;
 80			end = &a->next;
 81		}
 82		t->args = head;
 83		return t;
 84	case EBORROW:
 85		t = totype(e->body);
 86		t->borrow = 1;
 87		return t;
 88	case EEXISTS:
 89		return totype(e->body);
 90	case EFNTYPE:
 91		for(x = e->args; x; x = x->next) {
 92			a = totype(x);
 93			*end = a;
 94			end = &a->next;
 95		}
 96		return mkfntype(head, totype(e->body));
 97	default:
 98		errorat(&e->tok.loc, "not a type");
 99	}
100}
101
102static struct type *
103fnsig(struct decl *d) {
104	struct type *params = nil;
105	struct type **link = &params;
106	struct param *p;
107
108	for(p = d->params; p; p = p->next) {
109		*link = totype(p->ann);
110		link = &(*link)->next;
111	}
112	return mkfntype(params, totype(d->ret));
113}
114
115/* constructors, what it builds if it takes nothing, else fn(args) -> what it builds */
116static struct type *
117ctorsig(struct decl *d, struct ctor *c) {
118	struct type *params = nil;
119	struct type **link = &params;
120	struct type *ret;
121	struct type **rlink;
122	struct param *sp;
123	struct expr *x;
124
125	if(c->ret) {
126		ret = totype(c->ret);
127	} else {
128		ret = mktype(YNAME, d->binding);
129		rlink = &ret->args;
130		for(sp = d->sparams; sp; sp = sp->next) {
131			if(!istypesort(sp->ann))
132				errorat(&c->name.loc, "constructor needs a result type");
133			*rlink = mktype(YVAR, sp->binding);
134			rlink = &(*rlink)->next;
135		}
136	}
137	if(!c->args)
138		return ret;
139	for(x = c->args; x; x = x->next) {
140		*link = totype(x);
141		link = &(*link)->next;
142	}
143	return mkfntype(params, ret);
144}
145
146static void
147sigs(struct decl *d) {
148	struct ctor *c;
149
150	for(; d; d = d->next) {
151		switch(d->kind) {
152		case DFN:
153			d->binding->type = fnsig(d);
154			break;
155		case DDATA:
156			for(c = d->ctors; c; c = c->next)
157				c->binding->type = ctorsig(d, c);
158			break;
159		default:
160			break;
161		}
162	}
163}
164
165
166static struct type *
167instantiate(struct binding *b, struct location *loc) {
168	struct type *metas = nil;
169	struct type **link = &metas;
170	struct param *p;
171	struct blank *k;
172
173	if(!b->decl || !b->decl->sparams)
174		return b->type;
175	for(p = b->decl->sparams; p; p = p->next) {
176		*link = mkmeta();
177		if(istypesort(p->ann)) {
178			k = alloc(sizeof(*k));
179			k->meta = *link;
180			k->loc = *loc;
181			k->next = blanks;
182			k->unrestricted = p->ann->binding == butype;
183			blanks = k;
184		}
185		link = &(*link)->next;
186	}
187	return subst(b->type, b->decl->sparams, metas);
188}
189
190
191static void
192checklit(struct token tok, struct type *t) {
193	u64 n = 0;
194	u64 d;
195	usize i;
196
197	if(tok.kind == TCHAR)
198		n = tonum(tok);
199	else
200		for(i = 0; i < tok.length; i++) {
201			d = tok.str[i] - '0';
202			if(n > (UINT64_MAX - d) / 10)
203				errorat(&tok.loc, "number too big");
204			n = n * 10 + d;
205		}
206	if(n > intmax(t))
207		errorat(&tok.loc, tokdup(tok), " doesn't fit in ", typestr(t));
208}
209
210static void
211bindpat(struct pat *p, struct type *t) {
212	struct pat *a;
213	struct type *y;
214	struct type *u;
215
216	t = deref(t);
217	/* record the type we match */
218	p->type = t;
219	switch(p->kind) {
220	case PWILD:
221		break;
222	case PNAME:
223		p->binding->type = t;
224		break;
225	case PNUM:
226		if(isint(t)) {
227			checklit(p->name, t);
228			break;
229		}
230		u = mktype(YNAME, bint);
231		if(!unify(u, t))
232			errorat(&p->name.loc, "type mismatch: expected ", typestr(t), ", got ", typestr(u));
233		checklit(p->name, u);
234		break;
235	case PTUPLE:
236		if(t->kind != YTUPLE)
237			errorat(&p->name.loc, "tuple pattern, but value has type ", typestr(t));
238		for(a = p->args, y = t->args; a && y; a = a->next, y = y->next)
239			bindpat(a, y);
240		if(a)
241			errorat(&a->name.loc, "tuple pattern has too many elements");
242		if(y)
243			errorat(&p->name.loc, "tuple pattern has too few elements");
244		break;
245	case PCON:
246		/* a constructor taking args has a fn type so match its result, pair its params with the sub patterns */
247		u = instantiate(p->binding, &p->name.loc);
248		y = nil;
249		if(u->kind == YFN) {
250			y = u->args;
251			u = u->ret;
252		}
253		if(!unify(u, t))
254			errorat(&p->name.loc, "type mismatch: expected ", typestr(t), ", got ", typestr(u));
255		for(a = p->args; a && y; a = a->next, y = y->next)
256			bindpat(a, y);
257		if(a)
258			errorat(&a->name.loc, "pattern has too many arguments");
259		if(y)
260			errorat(&p->name.loc, "pattern has too few arguments");
261		break;
262	}
263}
264
265static struct type *trup(struct expr *);
266static void trdn(struct expr *, struct type *);
267static struct type *apply(struct expr *, struct type *);
268
269/* type a block's vals and give the names in their patterns types */
270static void
271vals(struct expr *e) {
272	struct val *v;
273	struct type *t;
274
275	for(v = e->vals; v; v = v->next) {
276		/* use the annotation */
277		if(v->ann) {
278			t = totype(v->ann);
279			trdn(v->init, t);
280		} else {
281			t = trup(v->init);
282		}
283		bindpat(v->pat, t);
284	}
285}
286
287static void
288casepats(struct expr *e) {
289	struct type *t;
290	struct arm *arm;
291
292	t = trup(e->scrut);
293	for(arm = e->arm; arm; arm = arm->next)
294		bindpat(arm->pat, t);
295}
296
297/* every guard cond must be bool */
298static void
299guardconds(struct expr *e) {
300	struct arm *arm;
301
302	for(arm = e->arm; arm; arm = arm->next)
303		if(arm->cond)
304			trdn(arm->cond, mktype(YNAME, bbool));
305}
306
307static struct type *
308armsup(struct expr *e) {
309	struct arm *arm;
310	struct type *bt;
311
312	bt = trup(e->arm->body);
313	for(arm = e->arm->next; arm; arm = arm->next)
314		trdn(arm->body, bt);
315	return bt;
316}
317
318static void
319trdn(struct expr *e, struct type *t) {
320	struct type *u;
321	struct arm *arm;
322
323	switch(e->kind) {
324	case EBLOCK:
325		vals(e);
326		trdn(e->body, t);
327		break;
328	case ECASE:
329		casepats(e);
330		for(arm = e->arm; arm; arm = arm->next)
331			trdn(arm->body, t);
332		break;
333	case EGUARD:
334		guardconds(e);
335		for(arm = e->arm; arm; arm = arm->next)
336			trdn(arm->body, t);
337		break;
338	case ENUMBER:
339		if(isint(t)) {
340			checklit(e->tok, t);
341			break;
342		}
343		/* from here the case is the same */
344	default:
345		/* operators get told the type we want */
346		if(e->kind == ECALL && e->tok.kind != TIDENT)
347			u = e->type = apply(e, t);
348		else
349			u = trup(e);
350		if(!unify(u, t))
351			errorat(&e->tok.loc, "type mismatch: expected ", typestr(t), ", got ", typestr(u));
352	}
353	e->type = t;
354}
355
356
357static int
358iscmp(enum tokenkind k) {
359	switch(k) {
360	case TEQ:
361	case TNEQ:
362	case TLEQ:
363	case TLESS:
364	case TMORE:
365	case TMEQ:
366		return 1;
367	default:
368		return 0;
369	}
370}
371
372static struct type *
373apply(struct expr *e, struct type *want) {
374	struct type *ft;
375	struct type *y;
376	struct expr *a;
377	struct expr *first = nil;
378	struct type *t;
379
380	if(e->tok.kind != TIDENT) {
381		if(e->tok.kind == TAND || e->tok.kind == TOR || e->tok.kind == TBANG) {
382			for(a = e->args; a; a = a->next)
383				trdn(a, mktype(YNAME, bbool));
384			return mktype(YNAME, bbool);
385		}
386		if(want && isint(want) && !iscmp(e->tok.kind)) {
387			/* we know the type */
388			t = want;
389		} else {
390			/* take the type from the first operand thats not a literal */
391			for(a = e->args; a; a = a->next) {
392				if(a->kind != ENUMBER) {
393					first = a;
394					break;
395				}
396			}
397			if(first)
398				t = trup(first);
399			else
400				t = mktype(YNAME, bint);
401			if(!isint(t))
402				if(!unify(t, mktype(YNAME, bint)))
403					errorat(&e->tok.loc, "operator needs integers, got ", typestr(t));
404		}
405		for(a = e->args; a; a = a->next) {
406			if(a == first)
407				continue;
408			trdn(a, t);
409		}
410		if(iscmp(e->tok.kind))
411			return mktype(YNAME, bbool);
412		return t;
413	}
414
415	ft = instantiate(e->binding, &e->tok.loc);
416	if(!ft || ft->kind != YFN)
417		errorat(&e->tok.loc, "not a function or constructor");
418
419	for(a = e->args, y = ft->args; a && y; a = a->next, y = y->next)
420		trdn(a, y);
421	if(a)
422		errorat(&a->tok.loc, "arity mismatch: too many arguments");
423	if(y)
424		errorat(&e->tok.loc, "arity mismatch: too few arguments");
425	return ft->ret;
426}
427
428int
429isnamed(struct type *t, struct binding *b) {
430	t = deref(t);
431	return t->kind == YNAME && t->binding == b;
432}
433
434static struct type *
435trup(struct expr *e) {
436	struct type *t;
437	struct type *to;
438	struct type **link;
439	struct expr *a;
440
441	switch(e->kind) {
442	case ENUMBER:
443		e->type = mktype(YNAME, bint);
444		checklit(e->tok, e->type);
445		return e->type;
446	case ESTRING:
447		return e->type = mktype(YNAME, bcstr);
448	case EIDENT:
449		if(!e->binding->type)
450			errorat(&e->tok.loc, "sorry, cant type this yet");
451		if(e->binding->kind == BFN || e->binding->kind == BCTOR)
452			return e->type = instantiate(e->binding, &e->tok.loc);
453		return e->type = e->binding->type;
454	case EBLOCK:
455		vals(e);
456		return e->type = trup(e->body);
457	case ECALL:
458		return e->type = apply(e, nil);
459	case ETUPLE:
460		t = mktype(YTUPLE, nil);
461		link = &t->args;
462		for(a = e->args; a; a = a->next) {
463			*link = tydup(trup(a));
464			link = &(*link)->next;
465		}
466		return e->type = t;
467	case ECAST:
468		t = trup(e->args);
469		to = totype(e->body);
470		/* a ptr can only come from a cstr, never from an int */
471		if(isnamed(to, bptr)) {
472			if(!isnamed(t, bcstr) && !isnamed(t, bptr))
473				errorat(&e->tok.loc, "can only cast a cstr to ptr, got ", typestr(t));
474			return e->type = to;
475		}
476		if(!isint(t) && !isnamed(t, bcstr) && !isnamed(t, bptr))
477			if(!unify(t, mktype(YNAME, bint)))
478				errorat(&e->tok.loc, "can only cast integers, cstrs and ptrs, got ", typestr(t));
479		if(!isint(to))
480			errorat(&e->tok.loc, "can only cast to an integer type or ptr, got ", typestr(to));
481		return e->type = to;
482	default:
483		errorat(&e->tok.loc, "sorry, can't type this yet");
484	case EGUARD:
485		guardconds(e);
486		return e->type = armsup(e);
487	case ECASE:
488		casepats(e);
489		return e->type = armsup(e);
490	}
491}
492
493static void
494checkfn(struct decl *d) {
495	struct param *p;
496	struct type *y;
497
498	for(p = d->params, y = d->binding->type->args; p; p = p->next, y = y->next)
499		p->binding->type = y;
500	if(d->body) 
501		trdn(d->body, d->binding->type->ret);
502}
503
504
505static void
506checkmain(struct decl *d) {
507	struct type *t = d->binding->type;
508
509	if(!t->args && isnamed(t->ret, bint))
510		return;
511	if(t->args && !t->args->next && isnamed(t->args, bworld) && isnamed(t->ret, bworld))
512		return;
513	errorat(&d->name.loc, "main must be fn main() -> int or fn main(w: World) -> World");
514}
515
516void
517check(struct module *m) {
518	struct module *x;
519	struct decl *d;
520	struct blank *k;
521
522	btrue->type = mktype(YNAME, bbool);
523	bfalse->type = mktype(YNAME, bbool);
524	bcone->type = mktype(YNAME, bone);
525	for(x = m; x; x = x->next)
526		sigs(x->decls);
527	for(x = m; x; x = x->next) {
528		for(d = x->decls; d; d = d->next) {
529			if(d->kind != DFN)
530				continue;
531			blanks = nil;
532			if(tokis(d->name, "main"))
533				checkmain(d);
534			checkfn(d);
535			for(k = blanks; k; k = k->next) {
536				if(deref(k->meta)->kind == YMETA)
537					errorat(&k->loc, "cannot infer type, add an annotation");
538				if(k->unrestricted && islinear(k->meta))
539					errorat(&k->loc, "expected unrestricted type, got linear '", typestr(k->meta), "'");
540			}
541		}
542	}
543}