summaryrefslogtreecommitdiff
path: root/decls_cgen.c
blob: 190c0044c26542b3c188d039d39481a4ec6c80a1 (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
static bool cgen_decls_stmt(CGenerator *g, Statement *s);
static bool cgen_decls_block(CGenerator *g, Block *b);

static bool cgen_decls_expr(CGenerator *g, Expression *e) {
	switch (e->kind) {
	case EXPR_CALL:
		if (e->call.fn->kind == EXPR_IDENT) {
			IdentDecl *idecl = ident_decl(e->call.fn->ident);
			if (idecl->kind == IDECL_DECL &&
				idecl->decl->expr.kind == EXPR_FN) {
				/* directly calling a function; might need to generate a copy of this function */
				/* TODO ASDF */
			}
		}
		break;
	case EXPR_FN:
		e->fn.c.name = NULL;
	    e->fn.c.id = g->ident_counter++;
		fn_enter(&e->fn, 0);
		if (!cgen_fn_header(g, &e->fn, e->where))
			return false;
		cgen_write(g, ";");
		cgen_nl(g);
		fn_exit(&e->fn);
		break;
	default:
		break;
	}
	cgen_recurse_subexprs(g, e, cgen_decls_expr, cgen_decls_block);
	
	return true;
}

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

static bool cgen_decls_decl(CGenerator *g, Declaration *d) {
	if (cgen_fn_is_direct(g, d)) {
		if (!fn_has_any_const_params(&d->expr.fn)) {
			d->expr.fn.c.name = d->idents[0];
			fn_enter(&d->expr.fn, 0);
			if (!cgen_fn_header(g, &d->expr.fn, d->where))
				return false;
			cgen_write(g, ";");
			cgen_nl(g);
		}
		if (!cgen_decls_block(g, &d->expr.fn.body))
			return false;
		fn_exit(&d->expr.fn);
	} else if ((d->flags & DECL_HAS_EXPR) && !(d->flags & DECL_IS_CONST)) {
		if (!cgen_decls_expr(g, &d->expr))
			return false;
	}
	return true;
}

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

static bool cgen_decls_file(CGenerator *g, ParsedFile *f) {
	cgen_write(g, "/* declarations */\n");
	arr_foreach(f->stmts, Statement, s) {
		if (!cgen_decls_stmt(g, s))
			return false;
	}
	return true;
}