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

Added get_field_at_index and count_fields to struct values #451

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/values/struct_value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use llvm_sys::core::{LLVMGetNumOperands, LLVMGetOperand};

use llvm_sys::prelude::LLVMValueRef;

use std::ffi::CStr;
Expand All @@ -7,7 +9,7 @@ use crate::types::StructType;
use crate::values::traits::AsValueRef;
use crate::values::{InstructionValue, Value};

use super::AnyValue;
use super::{AnyValue, BasicValueEnum};

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct StructValue<'ctx> {
Expand All @@ -23,6 +25,53 @@ impl<'ctx> StructValue<'ctx> {
}
}

/// Gets the value of a field belonging to this `StructValue`.
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i32_type = context.i32_type();
/// let i8_type = context.i8_type();
/// let i8_val = i8_type.const_all_ones();
/// let i32_val = i32_type.const_all_ones();
/// let struct_type = context.struct_type(&[i8_type.into(), i32_type.into()], false);
/// let struct_val = struct_type.const_named_struct(&[i8_val.into(), i32_val.into()]);
///
/// assert!(struct_val.get_field_at_index(0).is_some());
/// assert!(struct_val.get_field_at_index(1).is_some());
/// assert!(struct_val.get_field_at_index(3).is_none());
/// assert!(struct_val.get_field_at_index(0).unwrap().is_int_value());
/// ```
pub fn get_field_at_index(self, index: u32) -> Option<BasicValueEnum<'ctx>> {
// OoB indexing seems to be unchecked and therefore is UB
if index >= self.count_fields() {
return None;
}

unsafe { Some(BasicValueEnum::new(LLVMGetOperand(self.as_value_ref(), index))) }
}

/// Counts the number of fields.
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i32_type = context.i32_type();
/// let i8_type = context.i8_type();
/// let i8_val = i8_type.const_all_ones();
/// let i32_val = i32_type.const_all_ones();
/// let struct_type = context.struct_type(&[i8_type.into(), i32_type.into()], false);
/// let struct_val = struct_type.const_named_struct(&[i8_val.into(), i32_val.into()]);
///
/// assert_eq!(struct_val.count_fields(), 2);
/// assert_eq!(struct_val.count_fields(), struct_type.count_fields());
/// ```
pub fn count_fields(self) -> u32 {
unsafe { LLVMGetNumOperands(self.as_value_ref()) as u32 }
}

/// Gets the name of a `StructValue`. If the value is a constant, this will
/// return an empty string.
pub fn get_name(&self) -> &CStr {
Expand Down
39 changes: 35 additions & 4 deletions tests/all/test_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use inkwell::attributes::AttributeLoc;
use inkwell::comdat::ComdatSelectionKind;
use inkwell::context::Context;
use inkwell::module::Linkage::*;
use inkwell::types::{StringRadix, VectorType};
use inkwell::values::{AnyValue, InstructionOpcode::*, FIRST_CUSTOM_METADATA_KIND_ID};
use inkwell::types::{BasicType, StringRadix, VectorType};
use inkwell::values::{AnyValue, BasicValue, InstructionOpcode::*, FIRST_CUSTOM_METADATA_KIND_ID};
use inkwell::{AddressSpace, DLLStorageClass, GlobalVisibility, ThreadLocalMode};

use std::convert::TryFrom;
Expand Down Expand Up @@ -1184,10 +1184,41 @@ fn test_consts() {
assert_eq!(f128_val.get_constant(), Some((7.8, false)));
assert_eq!(ppc_f128_val.get_constant(), Some((9.0, false)));

//const struct member access
let void_type = context.void_type();
let struct_type = context.struct_type(&[i8_type.into(), f32_type.into()], false);
let module = context.create_module("test");
let test_func = module.add_function("strut_test", void_type.fn_type(&[struct_type.into()], false), None);
let non_const_struct_val = test_func.get_nth_param(0).unwrap().into_struct_value();
assert_eq!(non_const_struct_val.count_fields(), 0);

let struct_val = struct_type.const_named_struct(&[i8_val.into(), f32_val.into()]);
assert_eq!(struct_val.count_fields(), 2);
assert_eq!(struct_val.count_fields(), struct_type.count_fields());
assert!(struct_val.get_field_at_index(0).is_some());
assert!(struct_val.get_field_at_index(1).is_some());
assert!(struct_val.get_field_at_index(3).is_none());
assert!(struct_val.get_field_at_index(0).unwrap().is_int_value());
assert!(struct_val.get_field_at_index(1).unwrap().is_float_value());
assert_eq!(
struct_val
.get_field_at_index(0)
.unwrap()
.into_int_value()
.get_sign_extended_constant(),
Some(-1)
);
assert_eq!(
struct_val
.get_field_at_index(1)
.unwrap()
.into_float_value()
.get_constant(),
Some((3.4000000953674316, false))
);

// Non const test
let builder = context.create_builder();
let module = context.create_module("fns");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[i32_type.into(), f32_type.into()], false);
let function = module.add_function("fn", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");
Expand Down