[core] Add `CodeGenContext::build_in_bounds_gep_and_load`

For safer accesses to `gep`-able values and faster fails.
This commit is contained in:
David Mak 2024-08-12 17:46:47 +08:00 committed by sb10q
parent b3891b9a0d
commit 5539d144ed
1 changed files with 14 additions and 0 deletions

View File

@ -82,6 +82,20 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
self.builder.build_load(gep, name.unwrap_or_default()).unwrap()
}
/// Builds a sequence of `getelementptr inbounds` and `load` instructions which stores the value
/// of a struct field into an LLVM value.
///
/// Any out-of-bounds accesses to `ptr` will return in a `poison` value.
pub fn build_in_bounds_gep_and_load(
&mut self,
ptr: PointerValue<'ctx>,
index: &[IntValue<'ctx>],
name: Option<&str>,
) -> BasicValueEnum<'ctx> {
let gep = unsafe { self.builder.build_in_bounds_gep(ptr, index, "") }.unwrap();
self.builder.build_load(gep, name.unwrap_or_default()).unwrap()
}
fn get_subst_key(
&mut self,
obj: Option<Type>,