Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_field_at_index for llvm-plugin #464

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -52,6 +52,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()) }
TheDan64 marked this conversation as resolved.
Show resolved Hide resolved

true
}

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