I’m struggling to update some code that uses the C API and tries to get the function type from a function value.
In fact we use the C API from rust, but that should not make much of a difference. The current (llvm 14) definition is (roughly)
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
struct Value<'ctx> {
value: LLVMValueRef,
_marker: PhantomData<&'ctx ()>,
}
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
pub struct FunctionValue<'ctx> {
fn_value: Value<'ctx>,
}
pub fn get_type(self: FunctionValue<'ctx>) -> FunctionType<'ctx> {
// use the FunctionValue as an LLVMValueRef, and get its type. Apparently this is a pointer type
let ptr_type = unsafe { PointerType::new(unsafe { LLVMTypeOf(self.fn_value.value) };
// in fact it's a function pointer type, so we find the actual function type as the element type of the pointer
ptr_type.get_element_type().into_function_type()
}
Clearly, get_type
breaks with opaque pointers: ptr_type.get_element_type()
returns something invalid (just NULL maybe?) and the surrounding code will segfault.
But, LLVM should internally know the types of functions: we specify them on declaration. Is there some way to get that type back from an LLVMValueRef
that points to the function using the C API?