From ee0e30934ac2d2e52e2b3286e37a5737141c311c Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 13 Apr 2026 21:32:34 +0300 Subject: [PATCH] Allow typechecker to perform implicit casts --- src/typechecker.cpp | 13 +++++++++---- src/types.cpp | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/typechecker.cpp b/src/typechecker.cpp index c941e69..1c697d2 100644 --- a/src/typechecker.cpp +++ b/src/typechecker.cpp @@ -10,10 +10,15 @@ namespace { Castable, }; - Result check_type(std::shared_ptr checked, std::shared_ptr target) { + Result check_type(typecheck::State& state, std::shared_ptr checked, std::shared_ptr target) { + auto potential_cast = types::find_cast(state.casts, checked, target); + if (types::types_equal(checked, target)) { return TypecheckRes::Ok; } + else if (potential_cast.has_value() && potential_cast->allow_implicit) { + return TypecheckRes::Castable; + } return std::string{ "Types " + checked->formatted() + " and " + target->formatted() + " incompatible" }; } @@ -156,7 +161,7 @@ namespace AST { auto expected_param_ty = fn_ty->m_param_tys[i]; auto param_ty = this->m_args[i]->typecheck(state, scope, expected_param_ty); - auto check_res = check_type(param_ty, expected_param_ty); + auto check_res = check_type(state, param_ty, expected_param_ty); this->m_args[i] = handle_res(std::move(this->m_args[i]), check_res, state); } else { @@ -190,7 +195,7 @@ namespace AST { void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) { auto res_ty = this->m_expr->typecheck(state, scope, scope.return_ty); if (scope.return_ty) { - auto check_res = check_type(res_ty, *scope.return_ty); + auto check_res = check_type(state, res_ty, *scope.return_ty); this->m_expr = handle_res(std::move(this->m_expr), check_res, state); } } @@ -211,7 +216,7 @@ namespace AST { new types::FundamentalType{ types::FundamentalTypeKind::Bool } }; auto expr_ty = this->m_condition->typecheck(state, scope, bool_ty); - auto check_res = check_type(expr_ty, bool_ty); + auto check_res = check_type(state, expr_ty, bool_ty); this->m_condition = handle_res(std::move(this->m_condition), check_res, state); this->m_then->typecheck(state, scope); diff --git a/src/types.cpp b/src/types.cpp index e715457..dde30a8 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -76,7 +76,7 @@ namespace types { } bool Type::is_signed() { - false; + return false; } std::pair> FundamentalType::load(codegen::Builder&, llvm::Value* ptr) {