commit 95b8069

shrub  ·  2026-11-20 21:09:58 +0000 UTC
parent f15c861
fix typedefs
2 files changed,  +29, -0
M kav.h
+28, -0
 1@@ -15,6 +15,32 @@ istypesort(struct expr *ann) {
 2 	return ann->kind == EIDENT && (ann->binding == btype || ann->binding == butype);
 3 }
 4 
 5+static struct type *totype(struct expr *);
 6+
 7+static struct type *
 8+expandtypedef(struct expr *e) {
 9+	struct decl *d = e->binding->decl;
10+	struct type *to = nil;
11+	struct type **link = &to;
12+	struct type *t;
13+	struct param *sp;
14+	struct expr *x;
15+
16+	x = e->kind == ESAPP ? e->args : nil;
17+	for(sp = d->sparams; sp && x; sp = sp->next, x = x->next) {
18+		*link = istypesort(sp->ann) ? totype(x) : mkmeta();
19+		link = &(*link)->next;
20+	}
21+	if(sp || x)
22+		errorat(&e->tok.loc, "typedef '", tokdup(d->name), "' expects ", sp ? "more" : "fewer", " arguments");
23+	if(d->expanding)
24+		errorat(&e->tok.loc, "typedef '", tokdup(d->name), "' refers to itself");
25+	d->expanding = 1;
26+	t = totype(d->ret);
27+	d->expanding = 0;
28+	return subst(t, d->sparams, to);
29+}
30+
31 static struct type *
32 totype(struct expr *e) {
33 	struct type *t;
34@@ -24,6 +50,8 @@ totype(struct expr *e) {
35 	struct expr *x;
36 	struct param *sp;
37 
38+	if((e->kind == EIDENT || e->kind == ESAPP) && e->binding->decl && e->binding->decl->kind == DTYPEDEF)
39+		return expandtypedef(e);
40 	switch(e->kind) {
41 	case EIDENT:
42 		if(e->binding->kind == BPARAM)
M kav.h
+1, -0
1@@ -205,6 +205,7 @@ struct decl {
2 	int pub;
3 	int linear;
4 	int newtype;
5+	int expanding;	/* catching self-refrential typedef */
6 	struct param *sparams;
7 	struct param *params;
8 	struct expr *ret;