From c96a433ce573ab652213a65ce66600494a582ee1 Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 23 Feb 2026 19:03:18 +0200 Subject: [PATCH] Fix DynFunctionCall and Function instructions --- src/builder.rs | 19 +++++++++++++------ src/compile.rs | 7 +++++-- src/lib.rs | 2 ++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 90e39c9..c8f5512 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -708,9 +708,12 @@ impl Builder { } Instr::Function(_) => Ok(()), Instr::DynFunctionCall(instr, _) => { - let fn_ty = instr.get_type(&self)?; - let Type::Function(..) = fn_ty else { - return Err(ErrorKind::NotPointer(instr, fn_ty)); + let ptr = instr.get_type(&self)?; + let Type::Ptr(fn_ty) = ptr else { + return Err(ErrorKind::NotPointer(instr, ptr)); + }; + let Type::Function(..) = *fn_ty else { + return Err(ErrorKind::NotFunction(instr, *fn_ty)); }; Ok(()) } @@ -880,11 +883,15 @@ impl InstructionValue { IsNull(_) => Ok(Type::Bool), Function(fun) => { let data = builder.function_data(fun); - Ok(Type::Function(data.params, Box::new(data.ret))) + Ok(Type::Ptr(Box::new(Type::Function( + data.params, + Box::new(data.ret), + )))) } DynFunctionCall(instr, _) => { - let fn_ty = instr.get_type(builder)?; - let Type::Function(_, ret_ty) = fn_ty else { + let ptr_ty = instr.get_type(builder)?; + let Type::Ptr(fn_ty) = ptr_ty else { panic!() }; + let Type::Function(_, ret_ty) = *fn_ty else { panic!() }; Ok(*ret_ty) diff --git a/src/compile.rs b/src/compile.rs index d1ba1f8..2ca5711 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -1118,8 +1118,11 @@ impl InstructionHolder { Function(function_value) => module.functions.get(function_value).unwrap().value_ref, DynFunctionCall(function_value, instruction_values) => { let fun = module.values.get(&*function_value).unwrap(); + + let Type::Ptr(fn_ty) = &fun.ty else { panic!() }; + let ret_ty = - LLVMGetReturnType(fun.ty.as_llvm(module.context_ref, &module.types)); + LLVMGetReturnType(fn_ty.as_llvm(module.context_ref, &module.types)); let mut param_list: Vec = instruction_values .iter() @@ -1132,7 +1135,7 @@ impl InstructionHolder { } let value = LLVMBuildCall2( module.builder_ref, - fun.ty.as_llvm(module.context_ref, &module.types), + fn_ty.as_llvm(module.context_ref, &module.types), fun.value_ref, param_list.as_mut_ptr(), param_list.len() as u32, diff --git a/src/lib.rs b/src/lib.rs index e41d0fe..f9d27ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,8 @@ pub enum ErrorKind { NotPointer(InstructionValue, Type), #[error("Value {0:?} is not a struct, is {1:?}")] NotStruct(InstructionValue, Type), + #[error("Value {0:?} is not a function, is {1:?}")] + NotFunction(InstructionValue, Type), #[error("Struct {0:?} has no such field as {1:?}")] NoSuchField(Type, u32), #[error("Function {0:?} has no such parameter as {1:?}")]