Struct inkwell::types::StructType

source ·
pub struct StructType<'ctx> { /* private fields */ }
Expand description

A StructType is the type of a heterogeneous container of types.

Implementations§

source§

impl<'ctx> StructType<'ctx>

source

pub unsafe fn new(struct_type: LLVMTypeRef) -> Self

Create StructType from LLVMTypeRef

Safety

Undefined behavior, if referenced type isn’t struct type

source

pub fn get_field_type_at_index(self, index: u32) -> Option<BasicTypeEnum<'ctx>>

Gets the type of a field belonging to this StructType.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into()], false);

assert_eq!(struct_type.get_field_type_at_index(0).unwrap().into_float_type(), f32_type);
source

pub unsafe fn get_field_type_at_index_unchecked( self, index: u32 ) -> BasicTypeEnum<'ctx>

Gets the type of a field belonging to this StructType.

Safety

The index must be less than StructType::count_fields and the struct must not be opaque.

source

pub fn const_named_struct( self, values: &[BasicValueEnum<'ctx>] ) -> StructValue<'ctx>

Creates a StructValue based on this StructType’s definition.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let f32_zero = f32_type.const_float(0.);
let struct_type = context.struct_type(&[f32_type.into()], false);
let struct_val = struct_type.const_named_struct(&[f32_zero.into()]);
source

pub fn const_zero(self) -> StructValue<'ctx>

Creates a constant zero value of this StructType.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);
let struct_zero = struct_type.const_zero();
source

pub fn size_of(self) -> Option<IntValue<'ctx>>

Gets the size of this StructType. Value may vary depending on the target architecture.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let f32_struct_type = context.struct_type(&[f32_type.into()], false);
let f32_struct_type_size = f32_struct_type.size_of();
source

pub fn get_alignment(self) -> IntValue<'ctx>

Gets the alignment of this StructType. Value may vary depending on the target architecture.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);
let struct_type_alignment = struct_type.get_alignment();
source

pub fn get_context(self) -> ContextRef<'ctx>

Gets a reference to the Context this StructType was created in.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);

assert_eq!(struct_type.get_context(), context);
source

pub fn get_name(&self) -> Option<&CStr>

Gets this StructType’s name.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.opaque_struct_type("opaque_struct");

assert_eq!(struct_type.get_name().unwrap().to_str().unwrap(), "opaque_struct");
source

pub fn ptr_type(self, address_space: AddressSpace) -> PointerType<'ctx>

Creates a PointerType with this StructType for its element type.

Example
use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);
let struct_ptr_type = struct_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!(struct_ptr_type.get_element_type().into_struct_type(), struct_type);
source

pub fn fn_type( self, param_types: &[BasicMetadataTypeEnum<'ctx>], is_var_args: bool ) -> FunctionType<'ctx>

Creates a FunctionType with this StructType for its return type.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);
let fn_type = struct_type.fn_type(&[], false);
source

pub fn array_type(self, size: u32) -> ArrayType<'ctx>

Creates an ArrayType with this StructType for its element type.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);
let struct_array_type = struct_type.array_type(3);

assert_eq!(struct_array_type.len(), 3);
assert_eq!(struct_array_type.get_element_type().into_struct_type(), struct_type);
source

pub fn is_packed(self) -> bool

Determines whether or not a StructType is packed.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);

assert!(struct_type.is_packed());
source

pub fn is_opaque(self) -> bool

Determines whether or not a StructType is opaque.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.opaque_struct_type("opaque_struct");

assert!(struct_type.is_opaque());
source

pub fn count_fields(self) -> u32

Counts the number of field types.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let i8_type = context.i8_type();
let struct_type = context.struct_type(&[f32_type.into(), i8_type.into()], false);

assert_eq!(struct_type.count_fields(), 2);
source

pub fn get_field_types(self) -> Vec<BasicTypeEnum<'ctx>>

Gets this StructType’s field types.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let i8_type = context.i8_type();
let struct_type = context.struct_type(&[f32_type.into(), i8_type.into()], false);

assert_eq!(struct_type.get_field_types(), &[f32_type.into(), i8_type.into()]);
source

pub fn get_field_types_iter(self) -> FieldTypesIter<'ctx>

Get a struct field iterator.

source

pub fn print_to_string(self) -> LLVMString

Print the definition of a StructType to LLVMString.

source

pub fn get_undef(self) -> StructValue<'ctx>

Creates an undefined instance of a StructType.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let i8_type = context.i8_type();
let struct_type = context.struct_type(&[f32_type.into(), i8_type.into()], false);
let struct_type_undef = struct_type.get_undef();

assert!(struct_type_undef.is_undef());
source

pub fn get_poison(self) -> StructValue<'ctx>

Creates a poison instance of a StructType.

Example
use inkwell::context::Context;
use inkwell::values::AnyValue;

let context = Context::create();
let f32_type = context.f32_type();
let i8_type = context.i8_type();
let struct_type = context.struct_type(&[f32_type.into(), i8_type.into()], false);
let struct_type_poison = struct_type.get_poison();

assert!(struct_type_poison.is_poison());
source

pub fn set_body(self, field_types: &[BasicTypeEnum<'ctx>], packed: bool) -> bool

Defines the body of a StructType.

If the struct is an opaque type, it will no longer be after this call.

Resetting the packed state of a non-opaque struct type may not work.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let opaque_struct_type = context.opaque_struct_type("opaque_struct");

opaque_struct_type.set_body(&[f32_type.into()], false);

assert!(!opaque_struct_type.is_opaque());
source

pub fn const_array(self, values: &[StructValue<'ctx>]) -> ArrayValue<'ctx>

Creates a constant ArrayValue.

Example
use inkwell::context::Context;

let context = Context::create();
let f32_type = context.f32_type();
let struct_type = context.struct_type(&[f32_type.into(), f32_type.into()], false);
let struct_val = struct_type.const_named_struct(&[]);
let struct_array = struct_type.const_array(&[struct_val, struct_val]);

assert!(struct_array.is_const());

Trait Implementations§

source§

impl<'ctx> AnyType<'ctx> for StructType<'ctx>

source§

fn as_any_type_enum(&self) -> AnyTypeEnum<'ctx>

Returns an AnyTypeEnum that represents the current type.
source§

fn print_to_string(&self) -> LLVMString

Prints the definition of a Type to a LLVMString.
source§

impl AsTypeRef for StructType<'_>

source§

fn as_type_ref(&self) -> LLVMTypeRef

Returns the internal LLVM reference behind the type
source§

impl<'ctx> BasicType<'ctx> for StructType<'ctx>

source§

fn as_basic_type_enum(&self) -> BasicTypeEnum<'ctx>

Returns a BasicTypeEnum that represents the current type.
source§

fn fn_type( &self, param_types: &[BasicMetadataTypeEnum<'ctx>], is_var_args: bool ) -> FunctionType<'ctx>

Create a FunctionType with this BasicType as its return type. Read more
source§

fn is_sized(&self) -> bool

Determines whether or not this BasicType is sized or not. For example, opaque structs are unsized. Read more
source§

fn size_of(&self) -> Option<IntValue<'ctx>>

Gets the size of this BasicType. Value may vary depending on the target architecture. Read more
source§

fn array_type(&self, size: u32) -> ArrayType<'ctx>

Create an ArrayType with this BasicType as its elements. Read more
source§

fn ptr_type(&self, address_space: AddressSpace) -> PointerType<'ctx>

Create a PointerType that points to this BasicType. Read more
source§

impl<'ctx> Clone for StructType<'ctx>

source§

fn clone(&self) -> StructType<'ctx>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'ctx> Debug for StructType<'ctx>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for StructType<'_>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'ctx> From<StructType<'ctx>> for AnyTypeEnum<'ctx>

source§

fn from(value: StructType<'_>) -> AnyTypeEnum<'_>

Converts to this type from the input type.
source§

impl<'ctx> From<StructType<'ctx>> for BasicMetadataTypeEnum<'ctx>

source§

fn from(value: StructType<'_>) -> BasicMetadataTypeEnum<'_>

Converts to this type from the input type.
source§

impl<'ctx> From<StructType<'ctx>> for BasicTypeEnum<'ctx>

source§

fn from(value: StructType<'_>) -> BasicTypeEnum<'_>

Converts to this type from the input type.
source§

impl<'ctx> PartialEq for StructType<'ctx>

source§

fn eq(&self, other: &StructType<'ctx>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'ctx> TryFrom<AnyTypeEnum<'ctx>> for StructType<'ctx>

§

type Error = ()

The type returned in the event of a conversion error.
source§

fn try_from(value: AnyTypeEnum<'ctx>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'ctx> TryFrom<BasicMetadataTypeEnum<'ctx>> for StructType<'ctx>

§

type Error = ()

The type returned in the event of a conversion error.
source§

fn try_from(value: BasicMetadataTypeEnum<'ctx>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'ctx> TryFrom<BasicTypeEnum<'ctx>> for StructType<'ctx>

§

type Error = ()

The type returned in the event of a conversion error.
source§

fn try_from(value: BasicTypeEnum<'ctx>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'ctx> Copy for StructType<'ctx>

source§

impl<'ctx> Eq for StructType<'ctx>

source§

impl<'ctx> StructuralEq for StructType<'ctx>

source§

impl<'ctx> StructuralPartialEq for StructType<'ctx>

Auto Trait Implementations§

§

impl<'ctx> RefUnwindSafe for StructType<'ctx>

§

impl<'ctx> !Send for StructType<'ctx>

§

impl<'ctx> !Sync for StructType<'ctx>

§

impl<'ctx> Unpin for StructType<'ctx>

§

impl<'ctx> UnwindSafe for StructType<'ctx>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.