Skip to content

Commit ee692cb

Browse files
committed
Added VoidType docs & fixed const_null_ptr
which apparently doesn't actually create a null ptr but a null value...
1 parent f9cb692 commit ee692cb

File tree

15 files changed

+266
-175
lines changed

15 files changed

+266
-175
lines changed

src/module.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A `Module` represets a single code compilation unit.
22
33
use llvm_sys::analysis::{LLVMVerifyModule, LLVMVerifierFailureAction};
4+
#[allow(deprecated)]
45
use llvm_sys::bit_reader::{LLVMParseBitcode, LLVMParseBitcodeInContext};
56
use llvm_sys::bit_writer::{LLVMWriteBitcodeToFile, LLVMWriteBitcodeToMemoryBuffer};
67
use llvm_sys::core::{LLVMAddFunction, LLVMAddGlobal, LLVMDumpModule, LLVMGetNamedFunction, LLVMGetTypeByName, LLVMSetDataLayout, LLVMSetTarget, LLVMCloneModule, LLVMDisposeModule, LLVMGetTarget, LLVMModuleCreateWithName, LLVMGetModuleContext, LLVMGetFirstFunction, LLVMGetLastFunction, LLVMAddGlobalInAddressSpace, LLVMPrintModuleToString, LLVMGetNamedMetadataNumOperands, LLVMAddNamedMetadataOperand, LLVMGetNamedMetadataOperands, LLVMGetFirstGlobal, LLVMGetLastGlobal, LLVMGetNamedGlobal, LLVMPrintModuleToFile, LLVMSetModuleInlineAsm};
@@ -1065,6 +1066,7 @@ impl Module {
10651066
// LLVM has a newer version of this function w/o the error result since 3.8 but this deprecated function
10661067
// hasen't yet been removed even in the unreleased LLVM 7. Seems fine to use instead of switching to their
10671068
// error diagnostics handler
1069+
#[allow(deprecated)]
10681070
let success = unsafe {
10691071
LLVMParseBitcode(buffer.memory_buffer, &mut module, &mut err_string)
10701072
};
@@ -1101,6 +1103,7 @@ impl Module {
11011103
// LLVM has a newer version of this function w/o the error result since 3.8 but this deprecated function
11021104
// hasen't yet been removed even in the unreleased LLVM 7. Seems fine to use instead of switching to their
11031105
// error diagnostics handler
1106+
#[allow(deprecated)]
11041107
let success = unsafe {
11051108
LLVMParseBitcodeInContext(*context.context, buffer.memory_buffer, &mut module, &mut err_string)
11061109
};

src/types/array_type.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use llvm_sys::core::{LLVMConstArray, LLVMConstNull, LLVMGetArrayLength};
1+
use llvm_sys::core::{LLVMConstArray, LLVMGetArrayLength};
22
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
33

44
use AddressSpace;
55
use context::ContextRef;
66
use support::LLVMString;
77
use types::traits::AsTypeRef;
88
use types::{Type, BasicTypeEnum, PointerType, FunctionType};
9-
use values::{AsValueRef, ArrayValue, PointerValue, IntValue};
9+
use values::{AsValueRef, ArrayValue, IntValue};
1010

1111
/// An `ArrayType` is the type of contiguous constants or variables.
1212
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -158,7 +158,7 @@ impl ArrayType {
158158
/// let context = Context::create();
159159
/// let f32_type = context.f32_type();
160160
/// let f32_array_type = f32_type.array_type(3);
161-
/// let f32_array_val = f32_array_type.const_null();
161+
/// let f32_array_val = f32_array_type.const_zero();
162162
/// let f32_array_array = f32_array_type.const_array(&[f32_array_val, f32_array_val]);
163163
///
164164
/// assert!(f32_array_array.is_const());
@@ -174,7 +174,7 @@ impl ArrayType {
174174
ArrayValue::new(value)
175175
}
176176

177-
/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `ArrayType`.
177+
/// Creates a null `ArrayValue` of this `ArrayType`.
178178
/// It will be automatically assigned this `ArrayType`'s `Context`.
179179
///
180180
/// # Example
@@ -185,24 +185,24 @@ impl ArrayType {
185185
///
186186
/// // Global Context
187187
/// let f32_type = FloatType::f32_type();
188-
/// let f32_ptr_type = f32_type.ptr_type(AddressSpace::Generic);
189-
/// let f32_ptr_ptr_value = f32_ptr_type.const_null_ptr();
188+
/// let f32_array_type = f32_type.array_type(7);
189+
/// let f32_array_null = f32_array_type.const_null();
190190
///
191-
/// assert!(f32_ptr_ptr_value.is_null());
191+
/// assert!(f32_array_null.is_null());
192192
///
193193
/// // Custom Context
194194
/// let context = Context::create();
195195
/// let f32_type = context.f32_type();
196-
/// let f32_ptr_type = f32_type.ptr_type(AddressSpace::Generic);
197-
/// let f32_ptr_ptr_value = f32_ptr_type.const_null_ptr();
196+
/// let f32_array_type = f32_type.array_type(7);
197+
/// let f32_array_null = f32_array_type.const_null();
198198
///
199-
/// assert!(f32_ptr_ptr_value.is_null());
199+
/// assert!(f32_array_null.is_null());
200200
/// ```
201-
pub fn const_null_ptr(&self) -> PointerValue {
202-
self.array_type.const_null_ptr()
201+
pub fn const_null(&self) -> ArrayValue {
202+
ArrayValue::new(self.array_type.const_null())
203203
}
204204

205-
/// Creates a constant null (zero) value of this `ArrayType`.
205+
/// Creates a constant zero value of this `ArrayType`.
206206
///
207207
/// # Example
208208
///
@@ -212,16 +212,10 @@ impl ArrayType {
212212
/// let context = Context::create();
213213
/// let i8_type = context.i8_type();
214214
/// let i8_array_type = i8_type.array_type(3);
215-
/// let i8_array_zero = i8_array_type.const_null();
216-
///
217-
/// assert!(i8_array_zero.is_null());
215+
/// let i8_array_zero = i8_array_type.const_zero();
218216
/// ```
219-
pub fn const_null(&self) -> ArrayValue {
220-
let null = unsafe {
221-
LLVMConstNull(self.as_type_ref())
222-
};
223-
224-
ArrayValue::new(null)
217+
pub fn const_zero(&self) -> ArrayValue {
218+
ArrayValue::new(self.array_type.const_zero())
225219
}
226220

227221
/// Gets the length of this `ArrayType`.
@@ -249,7 +243,7 @@ impl ArrayType {
249243
}
250244

251245
// See Type::print_to_stderr note on 5.0+ status
252-
/// Prints the definition of an `IntType` to stderr. Not available in newer LLVM versions.
246+
/// Prints the definition of an `ArrayType` to stderr. Not available in newer LLVM versions.
253247
#[cfg(not(any(feature = "llvm3-6", feature = "llvm5-0")))]
254248
pub fn print_to_stderr(&self) {
255249
self.array_type.print_to_stderr()
@@ -287,7 +281,7 @@ impl ArrayType {
287281
/// assert_eq!(i8_array_type.get_element_type().into_int_type(), i8_type);
288282
/// ```
289283
pub fn get_element_type(&self) -> BasicTypeEnum {
290-
self.array_type.get_element_type()
284+
self.array_type.get_element_type().to_basic_type_enum()
291285
}
292286

293287
}

src/types/enums.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ impl AnyTypeEnum {
6565
LLVMTypeKind::LLVMTokenTypeKind => panic!("FIXME: Unsupported type: Token"),
6666
}
6767
}
68+
69+
pub(crate) fn to_basic_type_enum(&self) -> BasicTypeEnum {
70+
BasicTypeEnum::new(self.as_type_ref())
71+
}
6872
}
6973

7074
impl BasicTypeEnum {
@@ -85,13 +89,13 @@ impl BasicTypeEnum {
8589
LLVMTypeKind::LLVMPointerTypeKind => BasicTypeEnum::PointerType(PointerType::new(type_)),
8690
LLVMTypeKind::LLVMArrayTypeKind => BasicTypeEnum::ArrayType(ArrayType::new(type_)),
8791
LLVMTypeKind::LLVMVectorTypeKind => BasicTypeEnum::VectorType(VectorType::new(type_)),
88-
LLVMTypeKind::LLVMMetadataTypeKind => unreachable!("Unsupported type: Metadata"),
89-
LLVMTypeKind::LLVMX86_MMXTypeKind => unreachable!("Unsupported type: MMX"),
90-
LLVMTypeKind::LLVMLabelTypeKind => unreachable!("Unsupported type: Label"),
91-
LLVMTypeKind::LLVMVoidTypeKind => unreachable!("Unsupported type: VoidType"),
92-
LLVMTypeKind::LLVMFunctionTypeKind => unreachable!("Unsupported type: FunctionType"),
92+
LLVMTypeKind::LLVMMetadataTypeKind => unreachable!("Unsupported basic type: Metadata"),
93+
LLVMTypeKind::LLVMX86_MMXTypeKind => unreachable!("Unsupported basic type: MMX"),
94+
LLVMTypeKind::LLVMLabelTypeKind => unreachable!("Unsupported basic type: Label"),
95+
LLVMTypeKind::LLVMVoidTypeKind => unreachable!("Unsupported basic type: VoidType"),
96+
LLVMTypeKind::LLVMFunctionTypeKind => unreachable!("Unsupported basic type: FunctionType"),
9397
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7")))]
94-
LLVMTypeKind::LLVMTokenTypeKind => panic!("FIXME: Unsupported type: Token"),
98+
LLVMTypeKind::LLVMTokenTypeKind => unreachable!("Unsupported basic type: Token"),
9599
}
96100
}
97101
}

src/types/float_type.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use llvm_sys::core::{LLVMConstReal, LLVMConstNull, LLVMHalfType, LLVMFloatType, LLVMDoubleType, LLVMFP128Type, LLVMPPCFP128Type, LLVMConstRealOfStringAndSize, LLVMX86FP80Type, LLVMConstArray};
1+
use llvm_sys::core::{LLVMConstReal, LLVMHalfType, LLVMFloatType, LLVMDoubleType, LLVMFP128Type, LLVMPPCFP128Type, LLVMConstRealOfStringAndSize, LLVMX86FP80Type, LLVMConstArray};
22
use llvm_sys::execution_engine::LLVMCreateGenericValueOfFloat;
33
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
44

@@ -7,7 +7,7 @@ use context::ContextRef;
77
use support::LLVMString;
88
use types::traits::AsTypeRef;
99
use types::{Type, PointerType, FunctionType, BasicTypeEnum, ArrayType, VectorType};
10-
use values::{AsValueRef, ArrayValue, FloatValue, GenericValue, PointerValue, IntValue};
10+
use values::{AsValueRef, ArrayValue, FloatValue, GenericValue, IntValue};
1111

1212
/// A `FloatType` is the type of a floating point constant or variable.
1313
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -138,7 +138,7 @@ impl FloatType {
138138
FloatValue::new(value)
139139
}
140140

141-
/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `FloatType`.
141+
/// Creates a constant null value of this `FloatType`.
142142
/// It will be automatically assigned this `FloatType`'s `Context`.
143143
///
144144
/// # Example
@@ -148,18 +148,18 @@ impl FloatType {
148148
///
149149
/// // Global Context
150150
/// let f32_type = FloatType::f32_type();
151-
/// let f32_value = f32_type.const_null_ptr();
151+
/// let f32_value = f32_type.const_null();
152152
///
153153
/// // Custom Context
154154
/// let context = Context::create();
155155
/// let f32_type = context.f32_type();
156-
/// let f32_value = f32_type.const_null_ptr();
156+
/// let f32_value = f32_type.const_null();
157157
/// ```
158-
pub fn const_null_ptr(&self) -> PointerValue {
159-
self.float_type.const_null_ptr()
158+
pub fn const_null(&self) -> FloatValue {
159+
FloatValue::new(self.float_type.const_null())
160160
}
161161

162-
/// Creates a constant null (zero) value of this `FloatType`.
162+
/// Creates a constant zero value of this `FloatType`.
163163
///
164164
/// # Example
165165
///
@@ -168,17 +168,12 @@ impl FloatType {
168168
///
169169
/// let context = Context::create();
170170
/// let f32_type = context.f32_type();
171-
/// let f32_zero = f32_type.const_null();
171+
/// let f32_zero = f32_type.const_zero();
172172
///
173-
/// assert!(f32_zero.is_null());
174173
/// assert_eq!(f32_zero.print_to_string().to_string(), "f32 0");
175174
/// ```
176-
pub fn const_null(&self) -> FloatValue {
177-
let null = unsafe {
178-
LLVMConstNull(self.as_type_ref())
179-
};
180-
181-
FloatValue::new(null)
175+
pub fn const_zero(&self) -> FloatValue {
176+
FloatValue::new(self.float_type.const_zero())
182177
}
183178

184179
// REVIEW: Always true -> const fn?

src/types/fn_type.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ use llvm_sys::prelude::LLVMTypeRef;
44
use std::fmt;
55
use std::mem::forget;
66

7+
use AddressSpace;
78
use context::ContextRef;
89
use support::LLVMString;
910
use types::traits::AsTypeRef;
10-
use types::{Type, BasicTypeEnum};
11-
// use values::FunctionValue;
11+
use types::{PointerType, Type, BasicTypeEnum};
12+
13+
// REVIEW: Add a get_return_type() -> Option<BasicTypeEnum>?
1214

1315
#[derive(PartialEq, Eq, Clone, Copy)]
1416
pub struct FunctionType {
@@ -24,6 +26,10 @@ impl FunctionType {
2426
}
2527
}
2628

29+
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
30+
self.fn_type.ptr_type(address_space)
31+
}
32+
2733
pub fn is_var_arg(&self) -> bool {
2834
unsafe {
2935
LLVMIsFunctionVarArg(self.as_type_ref()) != 0

src/types/int_type.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use llvm_sys::core::{LLVMInt1Type, LLVMInt8Type, LLVMInt16Type, LLVMInt32Type, LLVMInt64Type, LLVMConstInt, LLVMConstNull, LLVMConstAllOnes, LLVMIntType, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray};
1+
use llvm_sys::core::{LLVMInt1Type, LLVMInt8Type, LLVMInt16Type, LLVMInt32Type, LLVMInt64Type, LLVMConstInt, LLVMConstAllOnes, LLVMIntType, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray};
22
use llvm_sys::execution_engine::LLVMCreateGenericValueOfInt;
33
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
44

@@ -7,7 +7,7 @@ use context::ContextRef;
77
use support::LLVMString;
88
use types::traits::AsTypeRef;
99
use types::{Type, ArrayType, BasicTypeEnum, VectorType, PointerType, FunctionType};
10-
use values::{AsValueRef, ArrayValue, GenericValue, IntValue, PointerValue};
10+
use values::{AsValueRef, ArrayValue, GenericValue, IntValue};
1111

1212
/// An `IntType` is the type of an integer constant or variable.
1313
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -280,7 +280,7 @@ impl IntType {
280280
IntValue::new(value)
281281
}
282282

283-
/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `IntType`. It will be automatically assigned this `IntType`'s `Context`.
283+
/// Creates a constant null value of this `IntType`.
284284
///
285285
/// # Example
286286
/// ```
@@ -289,18 +289,18 @@ impl IntType {
289289
///
290290
/// // Global Context
291291
/// let i32_type = IntType::i32_type();
292-
/// let i32_value = i32_type.const_null_ptr();
292+
/// let i32_value = i32_type.const_null();
293293
///
294294
/// // Custom Context
295295
/// let context = Context::create();
296296
/// let i32_type = context.i32_type();
297-
/// let i32_value = i32_type.const_null_ptr();
297+
/// let i32_value = i32_type.const_null();
298298
/// ```
299-
pub fn const_null_ptr(&self) -> PointerValue {
300-
self.int_type.const_null_ptr()
299+
pub fn const_null(&self) -> IntValue {
300+
IntValue::new(self.int_type.const_null())
301301
}
302302

303-
/// Creates a constant null (zero) value of this `IntType`.
303+
/// Creates a constant zero value of this `IntType`.
304304
///
305305
/// # Example
306306
///
@@ -309,17 +309,12 @@ impl IntType {
309309
///
310310
/// let context = Context::create();
311311
/// let i8_type = context.i8_type();
312-
/// let i8_zero = i8_type.const_null();
312+
/// let i8_zero = i8_type.const_zero();
313313
///
314-
/// assert!(i8_zero.is_null());
315314
/// assert_eq!(i8_zero.print_to_string().to_string(), "i8 0");
316315
/// ```
317-
pub fn const_null(&self) -> IntValue {
318-
let null = unsafe {
319-
LLVMConstNull(self.as_type_ref())
320-
};
321-
322-
IntValue::new(null)
316+
pub fn const_zero(&self) -> IntValue {
317+
IntValue::new(self.int_type.const_zero())
323318
}
324319

325320
/// Creates a `FunctionType` with this `IntType` for its return type.

0 commit comments

Comments
 (0)