bitwise resolve.c
  1#include <string.h>
  2
  3#include "util.h"
  4#include "kav.h"
  5
  6#define BUILTIN(sp, kind, str, var) struct binding *var;
  7#include "builtins.h"
  8#undef BUILTIN
  9
 10enum space {
 11	STYPE,
 12	SVALUE,
 13	SSORT,
 14};
 15
 16struct scope {
 17	struct scope *up;
 18	struct module *mod;
 19	struct binding *names[3];
 20};
 21
 22static void walksparams(struct scope *s, struct param *p);
 23
 24static struct binding *
 25lookin(struct scope *s, enum space sp, struct token name) {
 26	struct binding *b;
 27	for(b = s->names[sp]; b; b = b->next)
 28		if(tokeq(name, b->name))
 29			return b;
 30	return nil;
 31}
 32
 33static struct binding *
 34findused(struct scope *s, enum space sp, struct token name) {
 35	struct decl *d;
 36	struct binding *b;
 37	struct binding *found = nil;
 38
 39	for(d = s->mod->decls; d; d = d->next) {
 40		if(d->kind != DUSE || d->alias.length != 0)
 41			continue;
 42		b = lookin(d->mod->scope, sp, name);
 43		if(!b || !b->decl || !b->decl->pub)
 44			continue;
 45		if(b->kind == BCTOR && b->decl->abstract)
 46			continue;
 47		if(found && found != b)
 48			errorat(&name.loc, "'", tokdup(name), "' is provided by multiple modules");
 49		found = b;
 50	}
 51	return found;
 52}
 53
 54static struct binding *
 55lookup(struct scope *s, enum space sp, struct token name) {
 56	struct binding *b;
 57	for(; s; s = s->up) {
 58		b = lookin(s, sp, name);
 59		if(!b && s->mod)
 60			b = findused(s, sp, name);
 61		if(b)
 62			return b;
 63	}
 64	return nil;
 65}
 66
 67static struct binding *
 68find(struct scope *s, enum space sp, struct token name) {
 69	struct binding *b;
 70	if(!(b = lookup(s, sp, name)))
 71		errorat(&name.loc, "undefined name '", tokdup(name), "'");
 72	return b;
 73}
 74
 75static struct binding *
 76declare(struct scope *s, enum space sp, enum bindkind kind, struct token name) {
 77	struct binding *b;
 78
 79	b = mkbinding(kind, name);
 80	b->next = s->names[sp];
 81	s->names[sp] = b;
 82	return b;
 83}
 84
 85static struct binding *
 86define(struct scope *s, enum space sp, enum bindkind kind, struct token name) {
 87	if(lookin(s, sp, name))
 88		errorat(&name.loc, "duplicate definition '", tokdup(name), "'");
 89	if(s->mod && findused(s, sp, name))
 90		errorat(&name.loc, "'", tokdup(name), "' is already imported");
 91	return declare(s, sp, kind, name);
 92}
 93
 94
 95static struct scope *
 96newscope(struct scope *up) {
 97	struct scope *s;
 98
 99	s = zalloc(sizeof(*s));
100	s->up = up;
101	return s;
102}
103
104static struct binding *
105findns(struct scope *s, enum space sp, struct token ns, struct token name) {
106	struct decl *d;
107	struct binding *b;
108
109	while(!s->mod)
110		s = s->up;
111	for(d = s->mod->decls; d; d = d->next) {
112		if(d->kind != DUSE)
113			continue;
114		if(!tokeq(d->alias, ns))
115			continue;
116		if(!(b = lookin(d->mod->scope, sp, name)))
117			errorat(&name.loc, "'", tokdup(name), "' is not defined in module '", tokdup(ns), "'");
118		if(!b->decl || !b->decl->pub)
119			errorat(&name.loc, "'", tokdup(name), "' is not public in module '", tokdup(ns), "'");
120		if(b->kind == BCTOR && b->decl->abstract)
121			errorat(&name.loc, "'", tokdup(name), "' is abstract outside module '", tokdup(ns), "'");
122		return b;
123	}
124	errorat(&ns.loc, "no module named '", tokdup(ns), "'");
125}
126
127static void
128walkpat(struct scope *s, struct pat *p) {
129	struct binding *b;
130	struct pat *a;
131
132	if(p->ns.length != 0) {
133		p->binding = findns(s, SVALUE, p->ns, p->name);
134		if(p->binding->kind != BCTOR)
135			errorat(&p->name.loc, "not a constructor");
136		p->kind = PCON;
137		for(a = p->args; a; a = a->next)
138			walkpat(s, a);
139		return;
140	}
141	switch(p->kind) {
142	case PWILD:
143		break;
144	case PCON:
145		p->binding = find(s, SVALUE, p->name);
146		if(p->binding->kind != BCTOR)
147			errorat(&p->name.loc, "not a constructor");
148		for(a = p->args; a; a = a->next)
149			walkpat(s, a);
150		break;
151	case PTUPLE:
152		for(a = p->args; a; a = a->next)
153			walkpat(s, a);
154		break;
155	case PNAME:
156		/* there is some ambiguity in this case. */
157		b = lookup(s, SVALUE, p->name);
158		/* it's a constructor. */
159		if(b && b->kind == BCTOR) {
160			p->binding = b;
161			p->kind = PCON;
162			/* it's a variable */
163		} else {
164			p->binding = define(s, SVALUE, BPAT, p->name);
165		}
166		break;
167	case PNUM:
168		break;
169	}
170}
171
172static void
173walk(struct scope *s, enum space sp, struct expr *e) {
174	struct expr *a;
175	struct val *v;
176	struct arm *r;
177	struct scope inner = {0};
178	struct scope *cur;
179
180	if(!e)
181		return;
182	switch(e->kind) {
183	case ENUMBER:
184	case ESTRING:
185	case EHOLE:
186		break;
187	case EIDENT:
188		if(e->ns.length != 0)
189			e->binding = findns(s, sp, e->ns, e->tok);
190		else
191			e->binding = find(s, sp, e->tok);
192		break;
193	case ECALL:
194	case ESAPP:
195		if(e->tok.kind == TIDENT) {
196			if(e->ns.length != 0)
197				e->binding = findns(s, sp, e->ns, e->tok);
198			else
199				e->binding = find(s, sp, e->tok);
200		}
201		for(a = e->args; a; a = a->next)
202			walk(s, sp, a);
203		break;
204	case ETUPLE:
205		for(a = e->args; a; a = a->next)
206			walk(s, sp, a);
207		break;
208	case EBLOCK:
209		cur = s;
210		for(v = e->vals; v; v = v->next) {
211			walk(cur, STYPE, v->ann);
212			/* walk the init before binding so shadowing works */
213			walk(cur, sp, v->init);
214			/* each val gets its own scope so it can shadow earlier
215				 * vals but one pattern can't bind the name twice like
216				 * val (x, x) = return2ofem(x) isnt allowed */
217			cur = newscope(cur);
218			walkpat(cur, v->pat);
219		}
220		walk(cur, sp, e->body);
221		break;
222	case EGUARD:
223		for(r = e->arm; r; r = r->next) {
224			walk(s, sp, r->cond);
225			walk(s, sp, r->body);
226		}
227		break;
228	case ECASE:
229		walk(s, sp, e->scrut);
230		inner.up = s;
231		for(r = e->arm; r; r = r->next) {
232			inner.names[SVALUE] = nil;
233			walkpat(&inner, r->pat);
234			walk(&inner, sp, r->body);
235		}
236		break;
237	case EEXISTS:
238		inner.up = s;
239		walksparams(&inner, e->sparams);
240		walk(&inner, sp, e->body);
241		break;
242	case EBORROW:
243		walk(s, sp, e->body);
244		break;
245	case ECAST:
246		walk(s, sp, e->args);
247		walk(s, STYPE, e->body);
248		break;
249	case EFNTYPE:
250		for(a = e->args; a; a = a->next)
251			walk(s, sp, a);
252		walk(s, sp, e->body);
253		break;
254	}
255}
256
257static void
258walksparams(struct scope *s, struct param *p) {
259	for(; p; p = p->next) {
260		walk(s, SSORT, p->ann);
261		p->binding = define(s, STYPE, BPARAM, p->name);
262		p->binding->param = p;
263		walk(s, STYPE, p->cond);
264	}
265}
266
267static void
268walkfn(struct scope *up, struct decl *d) {
269	struct scope s = {0};
270	struct param *p;
271
272	s.up = up;
273	walksparams(&s, d->sparams);
274	for(p = d->params; p; p = p->next) {
275		walk(&s, STYPE, p->ann);
276		p->binding = define(&s, SVALUE, BPARAM, p->name);
277	}
278	walk(&s, STYPE, d->ret);
279	walk(&s, SVALUE, d->body);
280}
281
282static void
283walktypedecl(struct scope *up, struct decl *d) {
284	struct scope s = {0};
285	struct scope inner = {0};
286	struct param *p;
287	struct ctor *c;
288	struct expr *a;
289	enum space sp = STYPE;
290
291	if(d->kind == DSORTDEF)
292		sp = SSORT;
293	s.up = up;
294	walksparams(&s, d->sparams);
295	walk(&s, sp, d->ret);
296	for(p = d->params; p; p = p->next)
297		walk(&s, STYPE, p->ann);
298	inner.up = &s;
299	for(c = d->ctors; c; c = c->next) {
300		inner.names[STYPE] = nil;
301		/* constuctor local static sorts */
302		walksparams(&inner, c->sparams);
303		for(a = c->args; a; a = a->next)
304			walk(&inner, STYPE, a);
305		/* the constructor's result type */
306		walk(&inner, STYPE, c->ret);
307	}
308}
309
310static struct binding *
311builtin(struct scope *s, enum space sp, enum bindkind kind, const char *str) {
312	struct token t = {0};
313	t.kind = TIDENT;
314	t.str = str;
315	t.length = strlen(str);
316	return declare(s, sp, kind, t);
317}
318
319static void
320builtins(struct scope *s) {
321#define BUILTIN(sp, kind, str, var) var = builtin(s, sp, kind, str);
322#include "builtins.h"
323#undef BUILTIN
324}
325
326static void
327collect(struct scope *s, struct decl *d) {
328	struct ctor *c;
329	struct decl *e;
330
331	for(; d; d = d->next) {
332		switch(d->kind) {
333		case DFN:
334			d->binding = define(s, SVALUE, BFN, d->name);
335			break;
336		case DSTRUCT:
337		case DTYPEDEF:
338			d->binding = define(s, STYPE, BTYPE, d->name);
339			break;
340		case DSORTDEF:
341			d->binding = define(s, SSORT, BSORT, d->name);
342			break;
343		case DDATA:
344			d->binding = define(s, STYPE, BTYPE, d->name);
345			for(c = d->ctors; c; c = c->next) {
346				c->binding = define(s, SVALUE, BCTOR, c->name);
347				c->binding->decl = d;
348				c->binding->ctor = c;
349			}
350			break;
351		case DUSE:
352			if(d->alias.length == 0)
353				break;
354			for(e = s->mod->decls; e != d; e = e->next)
355				if(e->kind == DUSE && tokeq(e->alias, d->alias))
356					errorat(&d->alias.loc, "module alias '", tokdup(d->alias), "' is already in use");
357		}
358		/* every declared name knows its declaration */
359		if(d->binding)
360			d->binding->decl = d;
361	}
362}
363
364void
365resolve(struct module *m) {
366	struct scope univ = {0};
367	struct decl *d;
368	builtins(&univ);
369	for(; m; m = m->next) {
370		m->scope = newscope(&univ);
371		m->scope->mod = m;
372		collect(m->scope, m->decls);
373		for(d = m->decls; d; d = d->next)
374			if(d->kind == DFN)
375				walkfn(m->scope, d);
376			else
377				walktypedecl(m->scope, d);
378	}
379}