Skip to content

Commit 1df277b

Browse files
committed
Seperate pointer and function functionality on Type into new types
1 parent f8458d4 commit 1df277b

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

src/compile.rs

Lines changed: 4 additions & 4 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, Type};
7+
use ty::{StructType, FunctionType, PointerType, Type};
88
use std::mem;
99
use std::ffi::CStr;
1010

@@ -80,7 +80,7 @@ impl<'a> Compile<'a> for *const c_char {
8080
}
8181
}
8282
fn get_type(ctx: &'a Context) -> &'a Type {
83-
Type::new_pointer(Type::get::<c_char>(ctx))
83+
PointerType::new(Type::get::<c_char>(ctx))
8484
}
8585
}
8686
impl<'a> Compile<'a> for *const str {
@@ -181,7 +181,7 @@ macro_rules! compile_func(
181181
}.into()
182182
}
183183
fn get_type(context: &'a Context) -> &'a Type {
184-
Type::new_function(R::get_type(context), &[$($name::get_type(context)),*])
184+
FunctionType::new(R::get_type(context), &[$($name::get_type(context)),*])
185185
}
186186
}
187187
impl<'a, R, $($name),*> Compile<'a> for extern fn($($name),*) -> R where R:Compile<'a>, $($name:Compile<'a>),* {
@@ -193,7 +193,7 @@ macro_rules! compile_func(
193193
}.into()
194194
}
195195
fn get_type(context: &'a Context) -> &'a Type {
196-
Type::new_function(R::get_type(context), &[$($name::get_type(context)),*])
196+
FunctionType::new(R::get_type(context), &[$($name::get_type(context)),*])
197197
}
198198
}
199199
)

src/ty.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ impl Type {
3333
pub fn get<'a, T>(context:&'a Context) -> &'a Type where T:Compile<'a> {
3434
T::get_type(context)
3535
}
36-
/// Make a new function signature with the return type and arguments given.
37-
pub fn new_function<'a>(ret: &'a Type, args: &[&'a Type]) -> &'a FunctionType {
38-
unsafe { core::LLVMFunctionType(ret.into(), args.as_ptr() as *mut LLVMTypeRef, args.len() as c_uint, 0) }.into()
39-
}
4036
/// Make a new array with the length given.
4137
pub fn new_array<'a>(element: &'a Type, length: usize) -> &'a Type {
4238
unsafe { core::LLVMArrayType(element.into(), length as c_uint) }.into()
@@ -45,10 +41,6 @@ impl Type {
4541
pub fn new_vector<'a>(element: &'a Type, length: usize) -> &'a Type {
4642
unsafe { core::LLVMVectorType(element.into(), length as c_uint) }.into()
4743
}
48-
/// Make a new pointer with the given element type.
49-
pub fn new_pointer<'a>(elem: &'a Type) -> &'a Type {
50-
unsafe { core::LLVMPointerType(elem.into(), 0 as c_uint) }.into()
51-
}
5244
/// Returns true if the size of the type is known at compile-time.
5345
///
5446
/// This is equivalent to the type implementing `Sized` in Rust
@@ -91,10 +83,6 @@ impl Type {
9183
pub fn get_size(&self, target: &TargetData) -> usize {
9284
unsafe { target::LLVMABISizeOfType(target.into(), self.into()) as usize }
9385
}
94-
/// Returns the element of this pointer type.
95-
pub fn get_element(&self) -> Option<&Type> {
96-
unsafe { mem::transmute(core::LLVMGetElementType(self.into())) }
97-
}
9886
}
9987
get_context!(Type, LLVMGetTypeContext);
10088
to_str!(Type, LLVMPrintTypeToString);
@@ -136,15 +124,19 @@ deref!(FunctionType, Type);
136124
unsafe impl Sub<Type> for FunctionType {
137125
fn is(mut ty: &Type) -> bool {
138126
unsafe {
139-
while let Some(elem) = ty.get_element() {
140-
ty = elem;
127+
while let Some(ptr) = PointerType::from_super(ty) {
128+
ty = ptr.get_element();
141129
}
142130
let kind = core::LLVMGetTypeKind(ty.into());
143131
kind as c_uint == LLVMTypeKind::LLVMFunctionTypeKind as c_uint
144132
}
145133
}
146134
}
147135
impl FunctionType {
136+
/// Make a new function signature with the return type and arguments given.
137+
pub fn new<'a>(ret: &'a Type, args: &[&'a Type]) -> &'a FunctionType {
138+
unsafe { core::LLVMFunctionType(ret.into(), args.as_ptr() as *mut LLVMTypeRef, args.len() as c_uint, 0) }.into()
139+
}
148140
/// Returns the number of parameters this signature takes.
149141
pub fn num_params(&self) -> usize {
150142
unsafe { core::LLVMCountParamTypes(self.into()) as usize }
@@ -165,3 +157,20 @@ impl FunctionType {
165157
}
166158
get_context!(FunctionType, LLVMGetTypeContext);
167159
to_str!(FunctionType, LLVMPrintTypeToString);
160+
161+
/// A pointer type.
162+
pub struct PointerType;
163+
sub!{PointerType, LLVMPointerTypeKind}
164+
native_ref!(&PointerType = LLVMTypeRef);
165+
impl PointerType {
166+
/// Make a new pointer type with the given element type.
167+
pub fn new(elem: &Type) -> &Type {
168+
unsafe { core::LLVMPointerType(elem.into(), 0 as c_uint) }.into()
169+
}
170+
/// Returns the element of this pointer type.
171+
pub fn get_element(&self) -> &Type {
172+
unsafe { mem::transmute(core::LLVMGetElementType(self.into())) }
173+
}
174+
}
175+
get_context!(PointerType, LLVMGetTypeContext);
176+
to_str!(PointerType, LLVMPrintTypeToString);

src/value.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,7 @@ impl Index<usize> for Function {
213213
}
214214
unsafe impl Sub<Value> for Function {
215215
fn is(value: &Value) -> bool {
216-
let mut ty = value.get_type();
217-
while let Some(elem) = ty.get_element() {
218-
ty = elem;
219-
}
220-
ty.is_function()
216+
FunctionType::is(value.get_type())
221217
}
222218
}
223219
impl Function {

0 commit comments

Comments
 (0)