inkwell/types/array_type.rs
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
#[allow(deprecated)]
use llvm_sys::core::LLVMGetArrayLength;
use llvm_sys::prelude::LLVMTypeRef;
use crate::context::ContextRef;
use crate::support::LLVMString;
use crate::types::enums::BasicMetadataTypeEnum;
use crate::types::traits::AsTypeRef;
use crate::types::{BasicTypeEnum, FunctionType, PointerType, Type};
use crate::values::{ArrayValue, IntValue};
use crate::AddressSpace;
use std::fmt::{self, Display};
/// An `ArrayType` is the type of contiguous constants or variables.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct ArrayType<'ctx> {
array_type: Type<'ctx>,
}
impl<'ctx> ArrayType<'ctx> {
/// Create `ArrayType` from [`LLVMTypeRef`]
///
/// # Safety
/// Undefined behavior, if referenced type isn't array type
pub unsafe fn new(array_type: LLVMTypeRef) -> Self {
assert!(!array_type.is_null());
ArrayType {
array_type: Type::new(array_type),
}
}
// TODO: impl only for ArrayType<!StructType<Opaque>>
/// Gets the size of this `ArrayType`. Value may vary depending on the target architecture.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_type_size = i8_array_type.size_of();
/// ```
pub fn size_of(self) -> Option<IntValue<'ctx>> {
self.array_type.size_of()
}
/// Gets the alignment of this `ArrayType`. Value may vary depending on the target architecture.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_type_alignment = i8_array_type.get_alignment();
/// ```
pub fn get_alignment(self) -> IntValue<'ctx> {
self.array_type.get_alignment()
}
/// Creates a `PointerType` with this `ArrayType` for its element type.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
/// use inkwell::AddressSpace;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_ptr_type = i8_array_type.ptr_type(AddressSpace::default());
///
/// #[cfg(any(
/// feature = "llvm4-0",
/// feature = "llvm5-0",
/// feature = "llvm6-0",
/// feature = "llvm7-0",
/// feature = "llvm8-0",
/// feature = "llvm9-0",
/// feature = "llvm10-0",
/// feature = "llvm11-0",
/// feature = "llvm12-0",
/// feature = "llvm13-0",
/// feature = "llvm14-0"
/// ))]
/// assert_eq!(i8_array_ptr_type.get_element_type().into_array_type(), i8_array_type);
/// ```
#[cfg_attr(
any(
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0",
feature = "llvm18-0"
),
deprecated(
note = "Starting from version 15.0, LLVM doesn't differentiate between pointer types. Use Context::ptr_type instead."
)
)]
pub fn ptr_type(self, address_space: AddressSpace) -> PointerType<'ctx> {
self.array_type.ptr_type(address_space)
}
/// Gets a reference to the `Context` this `ArrayType` was created in.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
///
/// assert_eq!(i8_array_type.get_context(), context);
/// ```
pub fn get_context(self) -> ContextRef<'ctx> {
self.array_type.get_context()
}
/// Creates a `FunctionType` with this `ArrayType` for its return type.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let fn_type = i8_array_type.fn_type(&[], false);
/// ```
pub fn fn_type(self, param_types: &[BasicMetadataTypeEnum<'ctx>], is_var_args: bool) -> FunctionType<'ctx> {
self.array_type.fn_type(param_types, is_var_args)
}
/// Creates an `ArrayType` with this `ArrayType` for its element type.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_array_type = i8_array_type.array_type(3);
///
/// assert_eq!(i8_array_array_type.len(), 3);
/// assert_eq!(i8_array_array_type.get_element_type().into_array_type(), i8_array_type);
/// ```
pub fn array_type(self, size: u32) -> ArrayType<'ctx> {
self.array_type.array_type(size)
}
/// Creates a constant `ArrayValue` of `ArrayValue`s.
///
/// # Example
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let f32_type = context.f32_type();
/// let f32_array_type = f32_type.array_type(3);
/// let f32_array_val = f32_array_type.const_zero();
/// let f32_array_array = f32_array_type.const_array(&[f32_array_val, f32_array_val]);
///
/// assert!(f32_array_array.is_const());
/// ```
pub fn const_array(self, values: &[ArrayValue<'ctx>]) -> ArrayValue<'ctx> {
unsafe { ArrayValue::new_const_array(&self, values) }
}
/// Creates a constant zero value of this `ArrayType`.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_zero = i8_array_type.const_zero();
/// ```
pub fn const_zero(self) -> ArrayValue<'ctx> {
unsafe { ArrayValue::new(self.array_type.const_zero()) }
}
/// Gets the length of this `ArrayType`.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
///
/// assert_eq!(i8_array_type.len(), 3);
/// ```
pub fn len(self) -> u32 {
#[allow(deprecated)]
unsafe {
LLVMGetArrayLength(self.as_type_ref())
}
}
/// Returns `true` if this `ArrayType` contains no elements.
pub fn is_empty(self) -> bool {
self.len() == 0
}
/// Print the definition of an `ArrayType` to `LLVMString`
pub fn print_to_string(self) -> LLVMString {
self.array_type.print_to_string()
}
/// Creates an undefined instance of a `ArrayType`.
///
/// # Example
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_undef = i8_array_type.get_undef();
///
/// assert!(i8_array_undef.is_undef());
/// ```
pub fn get_undef(self) -> ArrayValue<'ctx> {
unsafe { ArrayValue::new(self.array_type.get_undef()) }
}
/// Creates a poison instance of a `ArrayType`.
///
/// # Example
/// ```no_run
/// use inkwell::context::Context;
/// use inkwell::values::AnyValue;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_poison = i8_array_type.get_poison();
///
/// assert!(i8_array_poison.is_poison());
/// ```
#[llvm_versions(12..)]
pub fn get_poison(self) -> ArrayValue<'ctx> {
unsafe { ArrayValue::new(self.array_type.get_poison()) }
}
// SubType: ArrayType<BT> -> BT?
/// Gets the element type of this `ArrayType`.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
///
/// assert_eq!(i8_array_type.get_element_type().into_int_type(), i8_type);
/// ```
pub fn get_element_type(self) -> BasicTypeEnum<'ctx> {
self.array_type.get_element_type().as_basic_type_enum()
}
}
unsafe impl AsTypeRef for ArrayType<'_> {
fn as_type_ref(&self) -> LLVMTypeRef {
self.array_type.ty
}
}
impl Display for ArrayType<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.print_to_string())
}
}