llvm_wrap/
ty.rs

1//! A wrapper around a `LLVMTypeRef`
2
3use super::*;
4use super::types::*;
5
6use std::mem;
7
8/// A wrapper around a `LLVMTypeRef` for a specific context
9#[derive(Copy, Clone)]
10pub struct Type {
11    pub(crate) ty: LLVMTypeRef
12}
13
14impl Type {
15    /// Creates a function that returns this type
16    pub fn function(&self, params: Vec<Type>, va_args: bool) -> Type {
17        Type {
18            ty: unsafe {
19                LLVMFunctionType(self.ty, ty_vec(&params).as_mut_ptr(), params.len() as u32, va_args as i32)
20            }
21        }
22    }
23
24    /// Creates a pointer to this type
25    pub fn pointer(&self) -> Type {
26        Type {
27            ty: unsafe {
28                LLVMPointerType(self.ty, 0)
29            }
30        }
31    }
32
33    /// The internal reference counter
34    pub fn rc(&self) -> Type {
35        ty_struct(vec![*self, ty_i32()], false)
36    }
37
38    /// Set the body of a struct
39    pub fn struct_set_body(&self, elements: Vec<Type>, packed: bool) {
40        unsafe {
41            LLVMStructSetBody(self.ty, ty_vec(&elements).as_mut_ptr(), elements.len() as u32, packed as i32)
42        }
43    }
44
45    /// An integer constant of this type
46    pub fn const_int(&self, val: u64) -> Value {
47        Value {
48            value: unsafe {
49                LLVMConstInt(self.ty, val, 0)
50            }
51        }
52    }
53
54    /// An integer constant of this type
55    pub fn const_signed_int(&self, val: i64) -> Value {
56        Value {
57            value: unsafe {
58                LLVMConstInt(self.ty, mem::transmute(val), 0)
59            }
60        }
61    }
62
63    /// A real constant of this type
64    pub fn const_real(&self, val: f64) -> Value {
65        Value {
66            value: unsafe {
67                LLVMConstReal(self.ty, val)
68            }
69        }
70    }
71
72    /// A constant named struct with the given elements
73    pub fn const_struct(&self, elements: Vec<Value>) -> Value {
74        Value {
75            value: unsafe {
76                LLVMConstNamedStruct(self.ty, val_vec(&elements).as_mut_ptr(), elements.len() as u32)
77            }
78        }
79    }
80
81    /// A constant array with the given elements
82    pub fn const_array(&self, elements: Vec<Value>) -> Value {
83        Value {
84            value: unsafe {
85                LLVMConstArray(self.ty, val_vec(&elements).as_mut_ptr(), elements.len() as u32)
86            }
87        }
88    }
89
90    /// The `undef` value for this type
91    pub fn undef(&self) -> Value {
92        Value {
93            value: unsafe {
94                LLVMGetUndef(self.ty)
95            }
96        }
97    }
98
99    /// The `null` value for this type
100    pub fn null(&self) -> Value {
101        Value {
102            value: unsafe {
103                LLVMConstNull(self.ty)
104            }
105        }
106    }
107
108    /// The all ones value for this type
109    pub fn ones(&self) -> Value {
110        Value {
111            value: unsafe {
112                LLVMConstAllOnes(self.ty)
113            }
114        }
115    }
116
117    /// The `null` value for this pointer type
118    pub fn null_ptr(&self) -> Value {
119        Value {
120            value: unsafe {
121                LLVMConstPointerNull(self.ty)
122            }
123        }
124    }
125
126    /// Dump the contents of the type to stderr
127    pub fn dump(&self) {
128        unsafe {
129            LLVMDumpType(self.ty);
130        }
131    }
132
133    /// Returns the internal type reference
134    pub fn inner(&self) -> LLVMTypeRef {
135        self.ty
136    }
137}
138
139impl Deref for Type {
140    type Target = LLVMTypeRef;
141
142    fn deref(&self) -> &LLVMTypeRef {
143        &self.ty
144    }
145}
146
147impl Debug for Type {
148    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
149        write!(f, "Type")
150    }
151}