summaryrefslogtreecommitdiff
path: root/type_checker.c
blob: 8a07f2ad41c34e3d4b604806a954bfb0fba0a931 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "type_checker.h"
#include "scope.h"
#include <stdio.h>
#include <stdlib.h>

#define TYPE_PANIC(format, ...) {\
    fprintf(\
        stderr,\
        "ccc: type error: " format "\n" __VA_OPT__(,)\
        __VA_ARGS__);\
    exit(1);\
}

static struct ast* ast_ref;
static struct scope* scope;

static void type_check_expr(struct expr_node* node);
static void type_check_lval(struct lval_node* node);
static void type_check_stmt(struct stmt_node* node);
static void type_check_group(struct group_node* node);

static void assert_cast_compatible(
    const struct type_def* lval_type,
    const struct type_def* rval_type
) {
    /* TODO: impl */
    /* TODO: we should also insert cast nodes eventually */
    if (false) {
        TYPE_PANIC(
            "cannot assign value of type '%s' to '%s'",
            lval_type->key.name,
            rval_type->key.name);
    }
}

static void enter_scope(struct scope* child_scope) {
    if (child_scope == NULL ||
            child_scope->next_out != scope)
        TYPE_PANIC("enter_scope: scopes are misaligned");

    scope = child_scope;
}

static void exit_scope(struct scope* child_scope) {
    if (child_scope != scope || child_scope->next_out == NULL)
        TYPE_PANIC("exit_scope: scopes are misaligned");

    scope = child_scope->next_out;
}

static const struct type_def* resolve_type_ref(struct type_ref_node* node) {
    /* TODO: support pointers and such */
    if (node->type.raw_type == NULL) TYPE_PANIC("use of unresolved type");
    if (node->type.ptr_level > 0) return ast_ref->pointer_type;
    return node->type.raw_type;
}

static const struct type_def* resolve_int_lit(struct int_lit_node* node) {
    return ast_ref->integral_type;
}

static const struct type_def* resolve_float_lit(struct float_lit_node* node) {
    return ast_ref->decimal_type;
}

static const struct type_def* resolve_char_lit(struct char_lit_node* node) {
    return ast_ref->char_type;
}

static const struct type_def* resolve_str_lit(struct str_lit_node* node) {
    return ast_ref->pointer_type;
}

static const struct type_def* resolve_var_ref(struct var_ref_node* node) {
    if (node->def_ref->resolved_type == NULL)
        TYPE_PANIC("variable '%s' has unresolved type.", node->def_ref->name);
    return node->def_ref->resolved_type;
}

static void type_check_var_decl(struct var_decl_node* node) {
    node->def_ref->resolved_type = resolve_type_ref(&node->type);
}

static const struct type_def* resolve_assign(struct assign_node* node) {
    type_check_lval(&node->lval);
    type_check_expr(node->rval);
    const struct type_def* lval_type = node->lval.resolved_type;
    const struct type_def* rval_type = node->rval->resolved_type;

    assert_cast_compatible(lval_type, rval_type);
    return lval_type;
}

static const struct type_def* resolve_call(struct call_node* node) {
    struct args_decl_node* arg_decl = node->called_fn_ref->args;
    struct args_eval_node* arg_eval = node->args;
    while (arg_decl != NULL && arg_eval != NULL) {
        type_check_var_decl(arg_decl->decl);
        const struct type_def* decl_type =
            arg_decl->decl->def_ref->resolved_type;
        type_check_expr(arg_eval->expr);
        const struct type_def* eval_type = arg_eval->expr->resolved_type;
        assert_cast_compatible(decl_type, eval_type);

        arg_decl = arg_decl->next;
        arg_eval = arg_eval->next;
    }
    if (arg_decl != NULL || arg_eval != NULL)
        TYPE_PANIC(
            "mismatched argument count in call to '%s'",
            node->called_fn_ref->name);

    return resolve_type_ref(&node->called_fn_ref->return_type);
}

static const struct type_def* resolve_unary(struct unary_node* node) {
    type_check_expr(node->expr);
    /* TODO: delegate by individual operation to prohibit shit like -"yes" */
    return node->expr->resolved_type;
}

static const struct type_def* resolve_binary(struct binary_node* node) {
    /* TODO: math rules and such lol */
    type_check_expr(node->lhs);
    type_check_expr(node->rhs);
    assert_cast_compatible(node->lhs->resolved_type, node->rhs->resolved_type);
    return node->lhs->resolved_type;
}

static void type_check_lval(struct lval_node* node) {
    switch (node->type) {
        case LVAL_VAR_DECL:
            type_check_var_decl(&node->inner.var_decl);
            node->resolved_type = node->inner.var_decl.def_ref->resolved_type;
            break;
        case LVAL_VAR_REF:
            node->resolved_type = resolve_var_ref(&node->inner.var_ref);
            break;
    }
}

static void type_check_expr(struct expr_node* node) {
    switch (node->type) {
        case EXPR_INT_LIT:
            node->resolved_type = resolve_int_lit(&node->inner.int_lit);
            break;
        case EXPR_FLOAT_LIT:
            node->resolved_type = resolve_float_lit(&node->inner.float_lit);
            break;
        case EXPR_CHAR_LIT:
            node->resolved_type = resolve_char_lit(&node->inner.char_lit);
            break;
        case EXPR_STR_LIT:
            node->resolved_type = resolve_str_lit(&node->inner.str_lit);
            break;
        case EXPR_VAR_REF:
            node->resolved_type = resolve_var_ref(&node->inner.var_ref);
            break;
        case EXPR_ASSIGN:
            node->resolved_type = resolve_assign(&node->inner.assign);
            break;
        case EXPR_CALL:
            node->resolved_type = resolve_call(&node->inner.call);
            break;
        case EXPR_UNARY:
            node->resolved_type = resolve_unary(&node->inner.unary);
            break;
        case EXPR_BINARY:
            node->resolved_type = resolve_binary(&node->inner.binary);
            break;
    }
}

static void type_check_return(struct return_node* node) {
    if (node->ret_val != NULL) type_check_expr(node->ret_val);
}

static void type_check_if(struct if_node* node) {
    enter_scope(node->scope);

    type_check_expr(node->cond);
    type_check_stmt(node->true_branch);
    if (node->false_branch != NULL) type_check_stmt(node->false_branch);

    exit_scope(node->scope);
}

static void type_check_loop(struct loop_node* node) {
    enter_scope(node->scope);

    if (node->init != NULL) type_check_expr(node->init);
    if (node->cond != NULL) type_check_expr(node->cond);
    if (node->incr != NULL) type_check_expr(node->incr);
    type_check_stmt(node->body);

    exit_scope(node->scope);
}

static void type_check_stmt(struct stmt_node* node) {
    switch (node->type) {
        case STMT_EMPTY:
            break;
        case STMT_EXPR:
            type_check_expr(&node->inner.expr);
            break;
        case STMT_VAR_DECL:
            type_check_var_decl(&node->inner.var_decl);
            break;
        case STMT_RETURN:
            type_check_return(&node->inner.return_);
            break;
        case STMT_GROUP:
            type_check_group(&node->inner.group);
            break;
        case STMT_IF:
            type_check_if(&node->inner.if_);
            break;
        case STMT_LOOP:
            type_check_loop(&node->inner.loop);
            break;
    }
}

static void type_check_group(struct group_node* node) {
    enter_scope(node->scope);

    struct stmt_node* stmt = node->head;
    while (stmt != NULL) {
        type_check_stmt(stmt);
        stmt = stmt->next;
    }

    exit_scope(node->scope);
}

static void type_check_fn_decl(struct fn_decl_node* node) {
    enter_scope(node->scope);

    node->resolved_return_type = resolve_type_ref(&node->return_type);

    for (struct args_decl_node* arg_decl = node->args;
            arg_decl != NULL;
            arg_decl = arg_decl->next)
        type_check_var_decl(arg_decl->decl);

    type_check_group(&node->body);

    exit_scope(node->scope);
}

static void type_check_root(struct root_node* node) {
    switch (node->type) {
        case ROOT_FN_DECL:
            type_check_fn_decl(&node->inner.fn_decl);
            break;
    }
}

void type_check(struct ast* ast) {
    ast_ref = ast;
    scope = ast->root_scope;

    struct root_node* node = ast->root_node;
    while (node != NULL) {
        type_check_root(node);
        node = node->next;
    }

    ast_ref = NULL;
    scope = NULL;
}