Skip to content

Commit d79b7b8

Browse files
committed
Tidy up types module and add IntegerType
1 parent 1df277b commit d79b7b8

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

src/ty.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ deref!{$ty, Type}
2626

2727
/// Defines how a value should be laid out in memory.
2828
pub struct Type(PhantomData<[u8]>);
29-
native_ref!(&Type = LLVMTypeRef);
29+
native_ref!{&Type = LLVMTypeRef}
30+
get_context!{Type, LLVMGetTypeContext}
31+
to_str!{Type, LLVMPrintTypeToString}
3032
impl Type {
3133
#[inline(always)]
3234
/// Get the type given as an LLVM type descriptor in the context given.
@@ -48,11 +50,15 @@ impl Type {
4850
unsafe { core::LLVMTypeIsSized(self.into()) != 0 }
4951
}
5052
/// Returns true if this type is a function.
53+
///
54+
/// This is equivalent to `FunctionType::is`.
5155
pub fn is_function(&self) -> bool {
5256
let kind = unsafe { core::LLVMGetTypeKind(self.into()) };
5357
kind as c_uint == LLVMTypeKind::LLVMFunctionTypeKind as c_uint
5458
}
5559
/// Returns true if this type is a struct.
60+
///
61+
/// This is equivalent to `StructType::is`.
5662
pub fn is_struct(&self) -> bool {
5763
let kind = unsafe { core::LLVMGetTypeKind(self.into()) };
5864
kind as c_uint == LLVMTypeKind::LLVMStructTypeKind as c_uint
@@ -63,6 +69,8 @@ impl Type {
6369
kind as c_uint == LLVMTypeKind::LLVMVoidTypeKind as c_uint
6470
}
6571
/// Returns true if this type is a pointer.
72+
///
73+
/// This is equivalent to `PointerType::is`.
6674
pub fn is_pointer(&self) -> bool {
6775
let kind = unsafe { core::LLVMGetTypeKind(self.into()) };
6876
kind as c_uint == LLVMTypeKind::LLVMPointerTypeKind as c_uint
@@ -84,12 +92,12 @@ impl Type {
8492
unsafe { target::LLVMABISizeOfType(target.into(), self.into()) as usize }
8593
}
8694
}
87-
get_context!(Type, LLVMGetTypeContext);
88-
to_str!(Type, LLVMPrintTypeToString);
8995

9096
/// A structure type, such as a tuple or struct.
9197
pub struct StructType;
92-
native_ref!(&StructType = LLVMTypeRef);
98+
native_ref!{&StructType = LLVMTypeRef}
99+
get_context!{StructType, LLVMGetTypeContext}
100+
to_str!{StructType, LLVMPrintTypeToString}
93101
sub!{StructType, LLVMStructTypeKind}
94102
impl StructType {
95103
/// Make a new struct with the given fields and packed representation.
@@ -114,13 +122,13 @@ impl StructType {
114122
}
115123
}
116124
}
117-
get_context!(StructType, LLVMGetTypeContext);
118-
to_str!(StructType, LLVMPrintTypeToString);
119125

120126
/// A function signature type.
121127
pub struct FunctionType;
122-
native_ref!(&FunctionType = LLVMTypeRef);
123-
deref!(FunctionType, Type);
128+
native_ref!{&FunctionType = LLVMTypeRef}
129+
get_context!{FunctionType, LLVMGetTypeContext}
130+
to_str!{FunctionType, LLVMPrintTypeToString}
131+
deref!{FunctionType, Type}
124132
unsafe impl Sub<Type> for FunctionType {
125133
fn is(mut ty: &Type) -> bool {
126134
unsafe {
@@ -155,13 +163,13 @@ impl FunctionType {
155163
unsafe { core::LLVMGetReturnType(self.into()).into() }
156164
}
157165
}
158-
get_context!(FunctionType, LLVMGetTypeContext);
159-
to_str!(FunctionType, LLVMPrintTypeToString);
160166

161167
/// A pointer type.
162168
pub struct PointerType;
169+
native_ref!{&PointerType = LLVMTypeRef}
170+
get_context!{PointerType, LLVMGetTypeContext}
171+
to_str!{PointerType, LLVMPrintTypeToString}
163172
sub!{PointerType, LLVMPointerTypeKind}
164-
native_ref!(&PointerType = LLVMTypeRef);
165173
impl PointerType {
166174
/// Make a new pointer type with the given element type.
167175
pub fn new(elem: &Type) -> &Type {
@@ -172,5 +180,20 @@ impl PointerType {
172180
unsafe { mem::transmute(core::LLVMGetElementType(self.into())) }
173181
}
174182
}
175-
get_context!(PointerType, LLVMGetTypeContext);
176-
to_str!(PointerType, LLVMPrintTypeToString);
183+
184+
/// An integer type.
185+
pub struct IntegerType;
186+
native_ref!{&IntegerType = LLVMTypeRef}
187+
get_context!{IntegerType, LLVMGetTypeContext}
188+
to_str!{IntegerType, LLVMPrintTypeToString}
189+
sub!{IntegerType, LLVMPointerTypeKind}
190+
impl IntegerType {
191+
/// Make a new integer type that will be the size of the given number of bits.
192+
pub fn new(context: &Context, numbits: usize) -> &IntegerType {
193+
unsafe { core::LLVMIntTypeInContext(context.into(), numbits as c_uint) }.into()
194+
}
195+
/// Returns how long an integer of this type is, in bits.
196+
pub fn get_width(&self) -> usize {
197+
unsafe { core::LLVMGetIntTypeWidth (self.into()) as usize }
198+
}
199+
}

0 commit comments

Comments
 (0)