/* * Utility functions for working with the Abstract Syntax Tree. * * Copyright © 2025-2026 Samuel Lidén Borell * * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later */ #include #include #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); }