Skip to main content

inkwell/types/
traits.rs

1use llvm_sys::prelude::LLVMTypeRef;
2
3use std::fmt::Debug;
4
5use crate::support::LLVMString;
6use crate::types::enums::{AnyTypeEnum, BasicMetadataTypeEnum, BasicTypeEnum};
7use crate::types::{
8    ArrayType, FloatType, FunctionType, IntType, PointerType, ScalableVectorType, StructType, Type, VectorType,
9    VoidType,
10};
11use crate::values::{
12    FloatMathValue, FloatValue, IntMathValue, IntValue, PointerMathValue, PointerValue, ScalableVectorValue,
13    VectorValue,
14};
15use crate::AddressSpace;
16
17/// Accessor to the inner LLVM type reference
18pub unsafe trait AsTypeRef {
19    /// Returns the internal LLVM reference behind the type
20    fn as_type_ref(&self) -> LLVMTypeRef;
21}
22
23macro_rules! trait_type_set {
24    ($trait_name:ident: $($args:ident),*) => (
25        $(
26            unsafe impl<'ctx> $trait_name<'ctx> for $args<'ctx> {}
27        )*
28    );
29}
30
31/// Represents any LLVM type.
32pub unsafe trait AnyType<'ctx>: AsTypeRef + Debug {
33    /// Returns an `AnyTypeEnum` that represents the current type.
34    fn as_any_type_enum(&self) -> AnyTypeEnum<'ctx> {
35        unsafe { AnyTypeEnum::new(self.as_type_ref()) }
36    }
37
38    /// Prints the definition of a Type to a `LLVMString`.
39    fn print_to_string(&self) -> LLVMString {
40        unsafe { Type::new(self.as_type_ref()).print_to_string() }
41    }
42}
43
44/// Represents a basic LLVM type, that may be used in functions and struct definitions.
45pub unsafe trait BasicType<'ctx>: AnyType<'ctx> {
46    /// Returns a `BasicTypeEnum` that represents the current type.
47    fn as_basic_type_enum(&self) -> BasicTypeEnum<'ctx> {
48        unsafe { BasicTypeEnum::new(self.as_type_ref()) }
49    }
50
51    /// Create a `FunctionType` with this `BasicType` as its return type.
52    ///
53    /// # Example:
54    ///
55    /// ```no_run
56    /// use inkwell::context::Context;
57    /// use inkwell::types::BasicType;
58    ///
59    /// let context = Context::create();
60    /// let int = context.i32_type();
61    /// let int_basic_type = int.as_basic_type_enum();
62    /// assert_eq!(int_basic_type.fn_type(&[], false), int.fn_type(&[], false));
63    /// ```
64    fn fn_type(&self, param_types: &[BasicMetadataTypeEnum<'ctx>], is_var_args: bool) -> FunctionType<'ctx> {
65        unsafe { Type::new(self.as_type_ref()).fn_type(param_types, is_var_args) }
66    }
67
68    /// Determines whether or not this `BasicType` is sized or not.
69    /// For example, opaque structs are unsized.
70    ///
71    /// # Example
72    ///
73    /// ```no_run
74    /// use inkwell::context::Context;
75    /// use inkwell::types::BasicType;
76    ///
77    /// let context = Context::create();
78    /// let f32_type = context.f32_type();
79    /// let f32_vec_type = f32_type.vec_type(40);
80    ///
81    /// assert!(f32_vec_type.is_sized());
82    /// ```
83    fn is_sized(&self) -> bool {
84        unsafe { Type::new(self.as_type_ref()).is_sized() }
85    }
86
87    /// Gets the size of this `BasicType`. Value may vary depending on the target architecture.
88    ///
89    /// # Example
90    ///
91    /// ```no_run
92    /// use inkwell::context::Context;
93    /// use inkwell::types::BasicType;
94    ///
95    /// let context = Context::create();
96    /// let f32_type = context.f32_type();
97    /// let f32_basic_type = f32_type.as_basic_type_enum();
98    /// let f32_type_size = f32_basic_type.size_of();
99    /// ```
100    fn size_of(&self) -> Option<IntValue<'ctx>> {
101        unsafe { Type::new(self.as_type_ref()).size_of() }
102    }
103
104    /// Gets the alignment of this `BasicType`. Value may vary depending on the target architecture.
105    ///
106    /// # Example
107    ///
108    /// ```no_run
109    /// use inkwell::context::Context;
110    /// use inkwell::types::BasicType;
111    ///
112    /// let context = Context::create();
113    /// let i8_type = context.i8_type();
114    /// let i8_type_alignment = i8_type.get_alignment();
115    ///
116    /// assert_eq!(i8_type_alignment.get_zero_extended_constant(), Some(1));
117    /// ```
118    fn get_alignment(&self) -> IntValue<'ctx> {
119        unsafe { Type::new(self.as_type_ref()).get_alignment() }
120    }
121
122    /// Create an `ArrayType` with this `BasicType` as its elements.
123    ///
124    /// Example:
125    /// ```no_run
126    /// use inkwell::context::Context;
127    /// use inkwell::types::BasicType;
128    ///
129    /// let context = Context::create();
130    /// let int = context.i32_type();
131    /// let int_basic_type = int.as_basic_type_enum();
132    /// assert_eq!(int_basic_type.array_type(32), int.array_type(32));
133    /// ```
134    // FIXME: This likely doesn't belong on the trait, since not all basic types can be turned into arrays?
135    fn array_type(&self, size: u32) -> ArrayType<'ctx> {
136        unsafe { Type::new(self.as_type_ref()).array_type(size) }
137    }
138
139    /// Create a `PointerType` that points to this `BasicType`.
140    ///
141    /// Example:
142    /// ```no_run
143    /// use inkwell::context::Context;
144    /// use inkwell::types::BasicType;
145    /// use inkwell::AddressSpace;
146    ///
147    /// let context = Context::create();
148    /// let int = context.i32_type();
149    /// let int_basic_type = int.as_basic_type_enum();
150    /// let addr_space = AddressSpace::default();
151    /// assert_eq!(int_basic_type.ptr_type(addr_space), int.ptr_type(addr_space));
152    /// ```
153    #[cfg_attr(
154        any(
155            all(feature = "llvm15-0", not(feature = "typed-pointers")),
156            all(feature = "llvm16-0", not(feature = "typed-pointers")),
157            feature = "llvm17-0",
158            feature = "llvm18-1",
159            feature = "llvm19-1",
160            feature = "llvm20-1",
161            feature = "llvm21-1",
162        ),
163        deprecated(
164            note = "Starting from version 15.0, LLVM doesn't differentiate between pointer types. Use Context::ptr_type instead."
165        )
166    )]
167    fn ptr_type(&self, address_space: AddressSpace) -> PointerType<'ctx> {
168        unsafe { Type::new(self.as_type_ref()).ptr_type(address_space) }
169    }
170}
171
172/// Represents an LLVM type that can have integer math operations applied to it.
173pub unsafe trait IntMathType<'ctx>: BasicType<'ctx> {
174    /// The value instance of an int or int vector type.
175    type ValueType: IntMathValue<'ctx>;
176    /// The type for int to float or int vector to float vector conversions.
177    type MathConvType: FloatMathType<'ctx>;
178    /// The type for int to pointer or int vector to pointer vector conversions.
179    type PtrConvType: PointerMathType<'ctx>;
180}
181
182/// Represents an LLVM type that can have floating point math operations applied to it.
183pub unsafe trait FloatMathType<'ctx>: BasicType<'ctx> {
184    /// The value instance of a float or float vector type.
185    type ValueType: FloatMathValue<'ctx>;
186    /// The type for float to int or float vector to int vector conversions.
187    type MathConvType: IntMathType<'ctx>;
188}
189
190/// Represents an LLVM type that can have pointer operations applied to it.
191pub unsafe trait PointerMathType<'ctx>: BasicType<'ctx> {
192    /// The value instance of a pointer type.
193    type ValueType: PointerMathValue<'ctx>;
194    /// The type for pointer to int or pointer vector to int conversions.
195    type PtrConvType: IntMathType<'ctx>;
196}
197
198trait_type_set! {AnyType: AnyTypeEnum, BasicTypeEnum, IntType, FunctionType, FloatType, PointerType, StructType, ArrayType, VoidType, VectorType, ScalableVectorType}
199trait_type_set! {BasicType: BasicTypeEnum, IntType, FloatType, PointerType, StructType, ArrayType, VectorType, ScalableVectorType}
200
201unsafe impl<'ctx> IntMathType<'ctx> for IntType<'ctx> {
202    type ValueType = IntValue<'ctx>;
203    type MathConvType = FloatType<'ctx>;
204    type PtrConvType = PointerType<'ctx>;
205}
206
207unsafe impl<'ctx> IntMathType<'ctx> for VectorType<'ctx> {
208    type ValueType = VectorValue<'ctx>;
209    type MathConvType = VectorType<'ctx>;
210    type PtrConvType = VectorType<'ctx>;
211}
212
213unsafe impl<'ctx> IntMathType<'ctx> for ScalableVectorType<'ctx> {
214    type ValueType = ScalableVectorValue<'ctx>;
215    type MathConvType = ScalableVectorType<'ctx>;
216    type PtrConvType = ScalableVectorType<'ctx>;
217}
218
219unsafe impl<'ctx> FloatMathType<'ctx> for FloatType<'ctx> {
220    type ValueType = FloatValue<'ctx>;
221    type MathConvType = IntType<'ctx>;
222}
223
224unsafe impl<'ctx> FloatMathType<'ctx> for VectorType<'ctx> {
225    type ValueType = VectorValue<'ctx>;
226    type MathConvType = VectorType<'ctx>;
227}
228
229unsafe impl<'ctx> FloatMathType<'ctx> for ScalableVectorType<'ctx> {
230    type ValueType = ScalableVectorValue<'ctx>;
231    type MathConvType = ScalableVectorType<'ctx>;
232}
233
234unsafe impl<'ctx> PointerMathType<'ctx> for PointerType<'ctx> {
235    type ValueType = PointerValue<'ctx>;
236    type PtrConvType = IntType<'ctx>;
237}
238
239unsafe impl<'ctx> PointerMathType<'ctx> for VectorType<'ctx> {
240    type ValueType = VectorValue<'ctx>;
241    type PtrConvType = VectorType<'ctx>;
242}
243
244unsafe impl<'ctx> PointerMathType<'ctx> for ScalableVectorType<'ctx> {
245    type ValueType = ScalableVectorValue<'ctx>;
246    type PtrConvType = ScalableVectorType<'ctx>;
247}