1use super::*;
4use super::types::*;
5
6use std::mem;
7
8#[derive(Copy, Clone)]
10pub struct Type {
11 pub(crate) ty: LLVMTypeRef
12}
13
14impl Type {
15 pub fn function(&self, params: Vec<Type>, va_args: bool) -> Type {
17 Type {
18 ty: unsafe {
19 LLVMFunctionType(self.ty, ty_vec(¶ms).as_mut_ptr(), params.len() as u32, va_args as i32)
20 }
21 }
22 }
23
24 pub fn pointer(&self) -> Type {
26 Type {
27 ty: unsafe {
28 LLVMPointerType(self.ty, 0)
29 }
30 }
31 }
32
33 pub fn rc(&self) -> Type {
35 ty_struct(vec![*self, ty_i32()], false)
36 }
37
38 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 pub fn const_int(&self, val: u64) -> Value {
47 Value {
48 value: unsafe {
49 LLVMConstInt(self.ty, val, 0)
50 }
51 }
52 }
53
54 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 pub fn const_real(&self, val: f64) -> Value {
65 Value {
66 value: unsafe {
67 LLVMConstReal(self.ty, val)
68 }
69 }
70 }
71
72 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 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 pub fn undef(&self) -> Value {
92 Value {
93 value: unsafe {
94 LLVMGetUndef(self.ty)
95 }
96 }
97 }
98
99 pub fn null(&self) -> Value {
101 Value {
102 value: unsafe {
103 LLVMConstNull(self.ty)
104 }
105 }
106 }
107
108 pub fn ones(&self) -> Value {
110 Value {
111 value: unsafe {
112 LLVMConstAllOnes(self.ty)
113 }
114 }
115 }
116
117 pub fn null_ptr(&self) -> Value {
119 Value {
120 value: unsafe {
121 LLVMConstPointerNull(self.ty)
122 }
123 }
124 }
125
126 pub fn dump(&self) {
128 unsafe {
129 LLVMDumpType(self.ty);
130 }
131 }
132
133 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}