aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/funccall.c
blob: a97100110f159840fe150f3f6f0fa2feed8af53a (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

/*
 * Semantic checking of function calls.
 *
 * Copyright © 2025 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
 */
#include <assert.h>
#include "compiler.h"

static void check_args(struct ExprCall *call, struct Func *func)
{
    struct CallArg *arg;
    struct Var *param;

    arg = call->args;
    param = func->params;
    for (;;) {
        if (!arg && !param) {
            break;
        } else if (!arg) {
            error("Too few arguments to function");
        } else if (!param) {
            error("Too many arguments to function");
        }
        check_type_compat(
                param->typeref,
                arg->expr->typeref,
                TC_ASSIGN);
        arg = arg->next;
        param = param->next;
    }
}

struct TypeRef funccall_check(struct Expr *e)
{
    struct TypeRef tr;
    size_t namelen;
    const char *name;
    struct Type *type;
    struct Func *func;
    HashCode h;

    assert(e->kind == E_CALL);
    namelen = e->u.call->ident.namelen;
    assert(namelen != 0);
    name = e->u.call->ident.u.name;
    assert(name != NULL);

    /* TODO typeidents ( .xx or :xx ?) */
    /* TODO methods  ( obj.xx ?) */

    func = NULL;
    h = hash_str(name, namelen);
    for (type = current_type; type != NULL; type = type->outer) {
        func = (struct Func *)tree_search(type->funcs,
                h, namelen, name);
    }
    if (!func) {
        func = (struct Func *)tree_search(module->funcs,
                h, namelen, name);
    }
    if (!func) {
        error(e->u.call->args != NULL ?
              "No such function" :
              "No such function or variable");
    }

    check_args(e->u.call, func);

    if (func->num_returns == 0) {
        tr.kind = TR_VOID;
    } else if (func->num_returns == 1) {
        tr = *func->returns->typeref;
    } else {
        /* TODO multiple return values */
        assert(0);
    }

    return tr;
}