Build erased types

This commit is contained in:
Ayaz Hafiz 2023-07-06 15:32:42 -05:00
parent b8c1436a75
commit e1c88c6101
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
3 changed files with 55 additions and 8 deletions

View file

@ -1,4 +1,8 @@
use inkwell::{types::StructType, AddressSpace};
use inkwell::{
types::StructType,
values::{PointerValue, StructValue},
AddressSpace,
};
use super::build::Env;
@ -17,3 +21,46 @@ pub fn basic_type<'a, 'ctx>(env: &Env<'a, 'ctx, '_>) -> StructType<'ctx> {
env.context
.struct_type(&[ptr_ty.into(), ptr_ty.into(), ptr_ty.into()], false)
}
fn bitcast_to_opaque_ptr<'ctx>(
env: &Env<'_, 'ctx, '_>,
value: PointerValue<'ctx>,
) -> PointerValue<'ctx> {
env.builder
.build_bitcast(
value,
env.context.i8_type().ptr_type(AddressSpace::default()),
"to_opaque_ptr",
)
.into_pointer_value()
}
pub fn build<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>,
value: Option<PointerValue<'ctx>>,
callee: PointerValue<'ctx>,
) -> StructValue<'ctx> {
let struct_type = basic_type(env);
let struct_value = struct_type.const_zero().into();
let struct_value = match value {
Some(value) => {
let value = bitcast_to_opaque_ptr(env, value);
env.builder
.build_insert_value(struct_value, value, 0, "insert_value")
.unwrap()
}
None => struct_value,
};
let callee = bitcast_to_opaque_ptr(env, callee);
let struct_value = env
.builder
.build_insert_value(struct_value, callee, 1, "insert_callee")
.unwrap();
// TODO: insert refcounter
struct_value.into_struct_value()
}