@@ -26,7 +26,9 @@ deref!{$ty, Type}
26
26
27
27
/// Defines how a value should be laid out in memory.
28
28
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 }
30
32
impl Type {
31
33
#[ inline( always) ]
32
34
/// Get the type given as an LLVM type descriptor in the context given.
@@ -48,11 +50,15 @@ impl Type {
48
50
unsafe { core:: LLVMTypeIsSized ( self . into ( ) ) != 0 }
49
51
}
50
52
/// Returns true if this type is a function.
53
+ ///
54
+ /// This is equivalent to `FunctionType::is`.
51
55
pub fn is_function ( & self ) -> bool {
52
56
let kind = unsafe { core:: LLVMGetTypeKind ( self . into ( ) ) } ;
53
57
kind as c_uint == LLVMTypeKind :: LLVMFunctionTypeKind as c_uint
54
58
}
55
59
/// Returns true if this type is a struct.
60
+ ///
61
+ /// This is equivalent to `StructType::is`.
56
62
pub fn is_struct ( & self ) -> bool {
57
63
let kind = unsafe { core:: LLVMGetTypeKind ( self . into ( ) ) } ;
58
64
kind as c_uint == LLVMTypeKind :: LLVMStructTypeKind as c_uint
@@ -63,6 +69,8 @@ impl Type {
63
69
kind as c_uint == LLVMTypeKind :: LLVMVoidTypeKind as c_uint
64
70
}
65
71
/// Returns true if this type is a pointer.
72
+ ///
73
+ /// This is equivalent to `PointerType::is`.
66
74
pub fn is_pointer ( & self ) -> bool {
67
75
let kind = unsafe { core:: LLVMGetTypeKind ( self . into ( ) ) } ;
68
76
kind as c_uint == LLVMTypeKind :: LLVMPointerTypeKind as c_uint
@@ -84,12 +92,12 @@ impl Type {
84
92
unsafe { target:: LLVMABISizeOfType ( target. into ( ) , self . into ( ) ) as usize }
85
93
}
86
94
}
87
- get_context ! ( Type , LLVMGetTypeContext ) ;
88
- to_str ! ( Type , LLVMPrintTypeToString ) ;
89
95
90
96
/// A structure type, such as a tuple or struct.
91
97
pub struct StructType ;
92
- native_ref ! ( & StructType = LLVMTypeRef ) ;
98
+ native_ref ! { & StructType = LLVMTypeRef }
99
+ get_context ! { StructType , LLVMGetTypeContext }
100
+ to_str ! { StructType , LLVMPrintTypeToString }
93
101
sub ! { StructType , LLVMStructTypeKind }
94
102
impl StructType {
95
103
/// Make a new struct with the given fields and packed representation.
@@ -114,13 +122,13 @@ impl StructType {
114
122
}
115
123
}
116
124
}
117
- get_context ! ( StructType , LLVMGetTypeContext ) ;
118
- to_str ! ( StructType , LLVMPrintTypeToString ) ;
119
125
120
126
/// A function signature type.
121
127
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 }
124
132
unsafe impl Sub < Type > for FunctionType {
125
133
fn is ( mut ty : & Type ) -> bool {
126
134
unsafe {
@@ -155,13 +163,13 @@ impl FunctionType {
155
163
unsafe { core:: LLVMGetReturnType ( self . into ( ) ) . into ( ) }
156
164
}
157
165
}
158
- get_context ! ( FunctionType , LLVMGetTypeContext ) ;
159
- to_str ! ( FunctionType , LLVMPrintTypeToString ) ;
160
166
161
167
/// A pointer type.
162
168
pub struct PointerType ;
169
+ native_ref ! { & PointerType = LLVMTypeRef }
170
+ get_context ! { PointerType , LLVMGetTypeContext }
171
+ to_str ! { PointerType , LLVMPrintTypeToString }
163
172
sub ! { PointerType , LLVMPointerTypeKind }
164
- native_ref ! ( & PointerType = LLVMTypeRef ) ;
165
173
impl PointerType {
166
174
/// Make a new pointer type with the given element type.
167
175
pub fn new ( elem : & Type ) -> & Type {
@@ -172,5 +180,20 @@ impl PointerType {
172
180
unsafe { mem:: transmute ( core:: LLVMGetElementType ( self . into ( ) ) ) }
173
181
}
174
182
}
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