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
17pub unsafe trait AsTypeRef {
19 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
31pub unsafe trait AnyType<'ctx>: AsTypeRef + Debug {
33 fn as_any_type_enum(&self) -> AnyTypeEnum<'ctx> {
35 unsafe { AnyTypeEnum::new(self.as_type_ref()) }
36 }
37
38 fn print_to_string(&self) -> LLVMString {
40 unsafe { Type::new(self.as_type_ref()).print_to_string() }
41 }
42}
43
44pub unsafe trait BasicType<'ctx>: AnyType<'ctx> {
46 fn as_basic_type_enum(&self) -> BasicTypeEnum<'ctx> {
48 unsafe { BasicTypeEnum::new(self.as_type_ref()) }
49 }
50
51 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 fn is_sized(&self) -> bool {
84 unsafe { Type::new(self.as_type_ref()).is_sized() }
85 }
86
87 fn size_of(&self) -> Option<IntValue<'ctx>> {
101 unsafe { Type::new(self.as_type_ref()).size_of() }
102 }
103
104 fn array_type(&self, size: u32) -> ArrayType<'ctx> {
118 unsafe { Type::new(self.as_type_ref()).array_type(size) }
119 }
120
121 #[cfg_attr(
136 any(
137 all(feature = "llvm15-0", not(feature = "typed-pointers")),
138 all(feature = "llvm16-0", not(feature = "typed-pointers")),
139 feature = "llvm17-0",
140 feature = "llvm18-0"
141 ),
142 deprecated(
143 note = "Starting from version 15.0, LLVM doesn't differentiate between pointer types. Use Context::ptr_type instead."
144 )
145 )]
146 fn ptr_type(&self, address_space: AddressSpace) -> PointerType<'ctx> {
147 unsafe { Type::new(self.as_type_ref()).ptr_type(address_space) }
148 }
149}
150
151pub unsafe trait IntMathType<'ctx>: BasicType<'ctx> {
153 type ValueType: IntMathValue<'ctx>;
155 type MathConvType: FloatMathType<'ctx>;
157 type PtrConvType: PointerMathType<'ctx>;
159}
160
161pub unsafe trait FloatMathType<'ctx>: BasicType<'ctx> {
163 type ValueType: FloatMathValue<'ctx>;
165 type MathConvType: IntMathType<'ctx>;
167}
168
169pub unsafe trait PointerMathType<'ctx>: BasicType<'ctx> {
171 type ValueType: PointerMathValue<'ctx>;
173 type PtrConvType: IntMathType<'ctx>;
175}
176
177trait_type_set! {AnyType: AnyTypeEnum, BasicTypeEnum, IntType, FunctionType, FloatType, PointerType, StructType, ArrayType, VoidType, VectorType, ScalableVectorType}
178trait_type_set! {BasicType: BasicTypeEnum, IntType, FloatType, PointerType, StructType, ArrayType, VectorType, ScalableVectorType}
179
180unsafe impl<'ctx> IntMathType<'ctx> for IntType<'ctx> {
181 type ValueType = IntValue<'ctx>;
182 type MathConvType = FloatType<'ctx>;
183 type PtrConvType = PointerType<'ctx>;
184}
185
186unsafe impl<'ctx> IntMathType<'ctx> for VectorType<'ctx> {
187 type ValueType = VectorValue<'ctx>;
188 type MathConvType = VectorType<'ctx>;
189 type PtrConvType = VectorType<'ctx>;
190}
191
192unsafe impl<'ctx> IntMathType<'ctx> for ScalableVectorType<'ctx> {
193 type ValueType = ScalableVectorValue<'ctx>;
194 type MathConvType = ScalableVectorType<'ctx>;
195 type PtrConvType = ScalableVectorType<'ctx>;
196}
197
198unsafe impl<'ctx> FloatMathType<'ctx> for FloatType<'ctx> {
199 type ValueType = FloatValue<'ctx>;
200 type MathConvType = IntType<'ctx>;
201}
202
203unsafe impl<'ctx> FloatMathType<'ctx> for VectorType<'ctx> {
204 type ValueType = VectorValue<'ctx>;
205 type MathConvType = VectorType<'ctx>;
206}
207
208unsafe impl<'ctx> FloatMathType<'ctx> for ScalableVectorType<'ctx> {
209 type ValueType = ScalableVectorValue<'ctx>;
210 type MathConvType = ScalableVectorType<'ctx>;
211}
212
213unsafe impl<'ctx> PointerMathType<'ctx> for PointerType<'ctx> {
214 type ValueType = PointerValue<'ctx>;
215 type PtrConvType = IntType<'ctx>;
216}
217
218unsafe impl<'ctx> PointerMathType<'ctx> for VectorType<'ctx> {
219 type ValueType = VectorValue<'ctx>;
220 type PtrConvType = VectorType<'ctx>;
221}
222
223unsafe impl<'ctx> PointerMathType<'ctx> for ScalableVectorType<'ctx> {
224 type ValueType = ScalableVectorValue<'ctx>;
225 type PtrConvType = ScalableVectorType<'ctx>;
226}