Skip to content

Commit

Permalink
Add set_field_at_index for llvm-plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and TheDan64 committed Jan 24, 2024
1 parent 4f18389 commit 0b13d10
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/values/struct_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use llvm_sys::core::{LLVMGetNumOperands, LLVMGetOperand};
use llvm_sys::core::{LLVMGetNumOperands, LLVMGetOperand, LLVMSetOperand};

use llvm_sys::prelude::LLVMValueRef;

Expand All @@ -7,7 +7,7 @@ use std::fmt::{self, Display};

use crate::types::StructType;
use crate::values::traits::AsValueRef;
use crate::values::{InstructionValue, Value};
use crate::values::{BasicValue, InstructionValue, Value};

use super::{AnyValue, BasicValueEnum};

Expand Down Expand Up @@ -57,6 +57,22 @@ impl<'ctx> StructValue<'ctx> {
unsafe { Some(BasicValueEnum::new(LLVMGetOperand(self.as_value_ref(), index))) }
}

/// Sets the value of a field belonging to this `StructValue`.
pub fn set_field_at_index<BV: BasicValue<'ctx>>(self, index: u32, val: BV) -> bool {
if self
.get_type()
.get_field_type_at_index(index)
.map(|t| t == val.as_basic_value_enum().get_type())
!= Some(true)
{
return false;
}

unsafe { LLVMSetOperand(self.as_value_ref(), index, val.as_value_ref()) }

true
}

/// Counts the number of fields.
///
/// ```no_run
Expand Down

0 comments on commit 0b13d10

Please sign in to comment.