summaryrefslogtreecommitdiff
path: root/typedefs_cgen.c
blob: 531fb0d1862b8385053669cc3ec9f030372f60e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
  Copyright (C) 2019, 2020 Leo Tenenbaum.
  This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever.
  You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>.
*/
static bool typedefs_stmt(CGenerator *g, Statement *s);
static bool typedefs_decl(CGenerator *g, Declaration *d);
static bool typedefs_expr(CGenerator *g, Expression *e);

#define cgen_recurse_into_type(f, g, type, extra)	\
	switch (type->kind) {							\
	case TYPE_STRUCT:								\
		arr_foreach(type->struc->fields, Field, fl)	\
			if (!f(g, fl->type, extra))				\
				return false;						\
		break;										\
	case TYPE_FN:									\
		arr_foreach(type->fn.types, Type, sub) {	\
			if (!f(g, sub, extra))					\
				return false;						\
		}											\
		break;										\
	case TYPE_TUPLE:								\
		arr_foreach(type->tuple, Type, sub)			\
			if (!f(g, sub, extra))					\
				return false;						\
		break;										\
	case TYPE_ARR:									\
		if (!f(g, type->arr.of, extra))				\
			return false;							\
		break;										\
	case TYPE_SLICE:								\
		if (!f(g, type->slice, extra))				\
			return false;							\
		break;										\
	case TYPE_PTR:									\
		if (!f(g, type->ptr, extra))				\
			return false;							\
		break;										\
	case TYPE_VOID:									\
	case TYPE_BUILTIN:								\
	case TYPE_PKG:									\
	case TYPE_TYPE:									\
	case TYPE_UNKNOWN:								\
		break;										\
	case TYPE_EXPR: assert(0);						\
	}


/* i is the name for this type, NULL if not available */
/* ALWAYS RETURNS TRUE. it just returns a bool for cgen_recurse_into_type to work */
static bool typedefs_type(CGenerator *g, Type *type, Identifier i) {
	if (!(type->flags & TYPE_IS_RESOLVED)) /* non-instance constant fn parameter type */
		return true;
	if (type->kind == TYPE_STRUCT) {
		StructDef *sdef = type->struc;
		/* we'll actually define the struct later; here we can just declare it */
	
		if (sdef->c.id || sdef->c.name) {
			/* we've already done this */
		} else {
			cgen_write(g, "struct ");
			if (i) {
				cgen_ident(g, i);
				sdef->c.name = i;
			} else {
				IdentID id = ++g->ident_counter;
				cgen_ident_id(g, id);
				sdef->c.id = id;
			}
			cgen_write(g, ";");
			cgen_nl(g);
		}
	}
	cgen_recurse_into_type(typedefs_type, g, type, NULL);
	return true;
}

static bool typedefs_block(CGenerator *g, Block *b) {
	Block *prev = g->block;
	if (!cgen_block_enter(g, b))
		return false;
	arr_foreach(b->stmts, Statement, s)
		if (!typedefs_stmt(g, s))
			return false;
	if (b->ret_expr && !typedefs_expr(g, b->ret_expr))
		return false;
	cgen_block_exit(g, prev);
	return true;
}

static bool typedefs_expr(CGenerator *g, Expression *e) {
	cgen_recurse_subexprs(g, e, typedefs_expr, typedefs_block, typedefs_decl);
	if (e->kind == EXPR_CAST) {
		typedefs_type(g, &e->cast.type, NULL);
	}
	if (e->kind == EXPR_FN) {
		/* needs to go before decls_cgen.c... */
		e->fn->c.id = ++g->ident_counter;
	}
	if (e->kind == EXPR_TYPE && e->typeval.kind == TYPE_STRUCT) {
		StructDef *sdef = e->typeval.struc;
		if (sdef->c.id || sdef->c.name) {
			/* we've already done this */
		} else {
			cgen_write(g, "struct ");
			IdentID id = ++g->ident_counter;
			cgen_ident_id(g, id);
			sdef->c.id = id;
			cgen_write(g, ";");
		}
	}
	return true;

}

static bool typedefs_decl(CGenerator *g, Declaration *d) {
	typedefs_type(g, &d->type, NULL);
	if (cgen_fn_is_direct(g, d)) {
		d->expr.fn->c.name = d->idents[0];
	}
	for (int idx = 0; idx < (int)arr_len(d->idents); ++idx) {
		Identifier i = d->idents[idx];
		Type *type = decl_type_at_index(d, idx);
		Value *val = decl_val_at_index(d, idx);
		if (type->kind == TYPE_TYPE) {
			/* generate typedef */
			typedefs_type(g, val->type, i);
			
			if (val->type->kind != TYPE_STRUCT) {
				IdentID id = 0;
				if (g->block != NULL || g->fn != NULL)
					id = ++g->ident_counter;
				if (val->type->kind != TYPE_TYPE) {
					cgen_write(g, "typedef ");
					if (!cgen_type_pre(g, val->type, d->where)) return false;
					cgen_write(g, " ");
					if (id) {
						cgen_ident_id(g, id);
					} else {
						cgen_ident(g, i);
					}
					if (val->type->kind != TYPE_STRUCT) {
						if (!cgen_type_post(g, val->type, d->where)) return false;
					}
					cgen_write(g, ";");
				}
			}
			cgen_nl(g);
		}
	}
	if (d->flags & DECL_HAS_EXPR) {
		if (!typedefs_expr(g, &d->expr))
			return false;
	}
	return true;
}

static bool typedefs_stmt(CGenerator *g, Statement *s) {
	switch (s->kind) {
	case STMT_DECL:
		if (!typedefs_decl(g, &s->decl))
			return false;
		break;
	case STMT_EXPR:
		if (!typedefs_expr(g, &s->expr))
			return false;
		break;
	case STMT_RET:
		if (s->ret.flags & RET_HAS_EXPR)
			if (!typedefs_expr(g, &s->ret.expr))
				return false;
		break;
	}
	return true;
}

static bool typedefs_file(CGenerator *g, ParsedFile *f) {
	arr_foreach(f->stmts, Statement, s) {
		if (!typedefs_stmt(g, s))
			return false;
	}
	return true;
}