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

/*
 * Utility functions for working with the Abstract Syntax Tree.
 *
 * Copyright © 2025-2026 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
 */
#include <assert.h>
#include <string.h>
#include "compiler.h"
#include "semchk.h"


const struct Expr *outermost_expr(const struct Expr *expr)
{
    assert(expr != NULL);
    while (expr->rpnnext) {
        expr = expr->rpnnext;
    }
    return expr;
}

bool is_expr_const(const struct Expr *expr)
{
    const struct TypeRef *tr = expr->typeref;
    assert(tr != NULL);
    switch (tr->kind) {
    case TR_BOOL:
    case TR_INT:
        return is_const(&tr->u.num);
    case TR_UNKNOWN:
    case TR_CLASS:
        return false;
    case TR_VOID:
        unreachable();
    }
    unreachable();
    return false;
}

void compare_vardef(const struct Var *expv, const struct Var *implv)
{
    size_t namelen;
    assert(implv != NULL);
    if (!expv) {
        error_ident("Paramter in implementation doesn't exist in interface",
                   &implv->ident);
    }

    namelen = implv->ident.node.length;
    if (expv->ident.node.length != namelen ||
            memcmp(expv->ident.node.name, implv->ident.node.name, namelen)) {
        error_ident("Parameter does not match name in interface",
                    &expv->ident);
    }

    check_type_compat(expv->typeref, implv->typeref, TC_EXACT);
}