Skip to content

Commit 91b2d30

Browse files
committed
Add ArrayType and VectorType functionality in new types
1 parent d79b7b8 commit 91b2d30

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/compile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ffi::prelude::LLVMValueRef;
44
use context::Context;
55
use libc::c_char;
66
use value::Value;
7-
use ty::{StructType, FunctionType, PointerType, Type};
7+
use ty::*;
88
use std::mem;
99
use std::ffi::CStr;
1010

@@ -157,7 +157,7 @@ macro_rules! compile_array(
157157
unsafe { core::LLVMConstVector(values.as_ptr() as *mut LLVMValueRef, $num) }.into()
158158
}
159159
fn get_type(context: &'a Context) -> &'a Type {
160-
Type::new_vector(Type::get::<T>(context), $num)
160+
VectorType::new(Type::get::<T>(context), $num)
161161
}
162162
}
163163
)

src/ty.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ impl Type {
3535
pub fn get<'a, T>(context:&'a Context) -> &'a Type where T:Compile<'a> {
3636
T::get_type(context)
3737
}
38-
/// Make a new array with the length given.
39-
pub fn new_array<'a>(element: &'a Type, length: usize) -> &'a Type {
40-
unsafe { core::LLVMArrayType(element.into(), length as c_uint) }.into()
41-
}
42-
/// Make a new vector with the length given.
43-
pub fn new_vector<'a>(element: &'a Type, length: usize) -> &'a Type {
44-
unsafe { core::LLVMVectorType(element.into(), length as c_uint) }.into()
45-
}
4638
/// Returns true if the size of the type is known at compile-time.
4739
///
4840
/// This is equivalent to the type implementing `Sized` in Rust
@@ -196,4 +188,45 @@ impl IntegerType {
196188
pub fn get_width(&self) -> usize {
197189
unsafe { core::LLVMGetIntTypeWidth (self.into()) as usize }
198190
}
191+
}
192+
193+
/// A vector type.
194+
pub struct VectorType;
195+
native_ref!{&VectorType = LLVMTypeRef}
196+
get_context!{VectorType, LLVMGetTypeContext}
197+
to_str!{VectorType, LLVMPrintTypeToString}
198+
sub!{VectorType, LLVMVectorTypeKind}
199+
impl VectorType {
200+
/// Make a new vector type with the length given.
201+
pub fn new(element: &Type, length: usize) -> &VectorType {
202+
unsafe { core::LLVMVectorType(element.into(), length as c_uint) }.into()
203+
}
204+
/// Returns the element type of this vector type.
205+
pub fn get_element(&self) -> &Type {
206+
unsafe { mem::transmute(core::LLVMGetElementType(self.into())) }
207+
}
208+
/// Returns the number of elements in this vector type.
209+
pub fn get_size(&self) -> usize {
210+
unsafe { core::LLVMGetVectorSize(self.into()) as usize }
211+
}
212+
}
213+
/// An array type.
214+
pub struct ArrayType;
215+
native_ref!{&ArrayType = LLVMTypeRef}
216+
get_context!{ArrayType, LLVMGetTypeContext}
217+
to_str!{ArrayType, LLVMPrintTypeToString}
218+
sub!{ArrayType, LLVMArrayTypeKind}
219+
impl ArrayType {
220+
/// Make a new array type with the length given.
221+
pub fn new(element: &Type, length: usize) -> &ArrayType {
222+
unsafe { core::LLVMArrayType(element.into(), length as c_uint) }.into()
223+
}
224+
/// Returns the element type of this array type.
225+
pub fn get_element(&self) -> &Type {
226+
unsafe { mem::transmute(core::LLVMGetElementType(self.into())) }
227+
}
228+
/// Returns the number of elements in this vector type.
229+
pub fn get_length(&self) -> usize {
230+
unsafe { core::LLVMGetArrayLength(self.into()) as usize }
231+
}
199232
}

0 commit comments

Comments
 (0)