1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use llvm_sys::core::{LLVMCountStructElementTypes, LLVMGetOperand, LLVMTypeOf};
use llvm_sys::prelude::LLVMValueRef;
use std::marker::PhantomData;
use crate::types::*;
use crate::values::*;
use crate::*;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct StructConstant<'ctx>(LLVMValueRef, PhantomData<&'ctx ()>);
impl_send_sync!(StructConstant);
impl<'ctx> GetType<'ctx> for StructConstant<'ctx> {}
impl<'ctx> StructConstant<'ctx> {
pub fn num_elements(&self) -> usize {
unsafe { LLVMCountStructElementTypes(LLVMTypeOf(self.0)) as usize }
}
pub fn elements(&self) -> Vec<Constant<'ctx>> {
(0..self.num_elements() as u32)
.map(|i| Constant::from_llvm(unsafe { LLVMGetOperand(self.0, i) }))
.collect()
}
pub fn get_struct_type(&self) -> StructType<'ctx> {
StructType::from_llvm(self.get_type().type_ref())
}
}
impl_positional_value_ref!(StructConstant, 0);
impl_positional_from_llvm_value!(StructConstant);
impl<'ctx> AsConstant<'ctx> for StructConstant<'ctx> {
fn as_constant(&self) -> Constant<'ctx> {
Constant::Struct(self.clone())
}
}
impl_as_operand_for_constant!(StructConstant);