llvm_wrap/
types.rs

1//! Provides functions that return LLVM types in the global context
2
3use super::*;
4use super::c_api::*;
5
6/// Create a named struct type
7pub fn create_named_struct<S>(name: S) -> Type where S: AsRef<str> {
8    Type {
9        ty: unsafe {
10            LLVMStructCreateNamed(context(), into_c(name).as_ptr())
11        }
12    }
13}
14
15/// A array type with a certain number of elements
16pub fn ty_array(ty: Type, count: u32) -> Type {
17    Type {
18        ty: unsafe {
19            LLVMArrayType(ty.ty, count)
20        }
21    }
22}
23
24/// A struct type with the given elements
25pub fn ty_struct(elements: Vec<Type>, packed: bool) -> Type {
26    Type {
27        ty: unsafe {
28            LLVMStructTypeInContext(context(), ty_vec(&elements).as_mut_ptr(), elements.len() as u32, packed as i32)
29        }
30    }
31}
32
33/// The `void` type
34pub fn ty_void() -> Type {
35    Type {
36        ty: unsafe {
37            LLVMVoidTypeInContext(context())
38        }
39    }
40}
41
42/// The `i1` type
43pub fn ty_i1() -> Type {
44    Type {
45        ty: unsafe {
46            LLVMInt1TypeInContext(context())
47        }
48    }
49}
50
51/// The `i8` type
52pub fn ty_i8() -> Type {
53    Type {
54        ty: unsafe {
55            LLVMInt8TypeInContext(context())
56        }
57    }
58}
59
60/// The `i16` type
61pub fn ty_i16() -> Type {
62    Type {
63        ty: unsafe {
64            LLVMInt16TypeInContext(context())
65        }
66    }
67}
68
69/// The `i32` type
70pub fn ty_i32() -> Type {
71    Type {
72        ty: unsafe {
73            LLVMInt32TypeInContext(context())
74        }
75    }
76}
77
78/// The `i64` type
79pub fn ty_i64() -> Type {
80    Type {
81        ty: unsafe {
82            LLVMInt64TypeInContext(context())
83        }
84    }
85}
86
87/// The `i128` type
88pub fn ty_i128() -> Type {
89    Type {
90        ty: unsafe {
91            LLVMInt128TypeInContext(context())
92        }
93    }
94}
95
96/// The `isize` type for a given data layout
97pub fn ty_isize(data: &target::TargetData) -> Type {
98    Type {
99        ty: unsafe {
100            llvm_sys::target::LLVMIntPtrTypeInContext(context(), data.data)
101        }
102    }
103}
104
105/// An integer type with any number of bits
106pub fn ty_i(bits: u32) -> Type {
107    Type {
108        ty: unsafe {
109            LLVMIntTypeInContext(context(), bits)
110        }
111    }
112}
113
114/// The `half` type
115pub fn ty_half() -> Type {
116    Type {
117        ty: unsafe {
118            LLVMHalfTypeInContext(context())
119        }
120    }
121}
122
123/// The `float` type
124pub fn ty_float() -> Type {
125    Type {
126        ty: unsafe {
127            LLVMFloatTypeInContext(context())
128        }
129    }
130}
131
132/// The `double` type
133pub fn ty_double() -> Type {
134    Type {
135        ty: unsafe {
136            LLVMDoubleTypeInContext(context())
137        }
138    }
139}
140
141/// The `fp128` type
142pub fn ty_fp128() -> Type {
143    Type {
144        ty: unsafe {
145            LLVMFP128TypeInContext(context())
146        }
147    }
148}