Skip to content

Commit

Permalink
Added VoidType docs & fixed const_null_ptr
Browse files Browse the repository at this point in the history
which apparently doesn't actually create a null ptr but a null value...
  • Loading branch information
TheDan64 committed Sep 22, 2018
1 parent f9cb692 commit ee692cb
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 175 deletions.
3 changes: 3 additions & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A `Module` represets a single code compilation unit.

use llvm_sys::analysis::{LLVMVerifyModule, LLVMVerifierFailureAction};
#[allow(deprecated)]
use llvm_sys::bit_reader::{LLVMParseBitcode, LLVMParseBitcodeInContext};
use llvm_sys::bit_writer::{LLVMWriteBitcodeToFile, LLVMWriteBitcodeToMemoryBuffer};
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};
Expand Down Expand Up @@ -1065,6 +1066,7 @@ impl Module {
// LLVM has a newer version of this function w/o the error result since 3.8 but this deprecated function
// hasen't yet been removed even in the unreleased LLVM 7. Seems fine to use instead of switching to their
// error diagnostics handler
#[allow(deprecated)]
let success = unsafe {
LLVMParseBitcode(buffer.memory_buffer, &mut module, &mut err_string)
};
Expand Down Expand Up @@ -1101,6 +1103,7 @@ impl Module {
// LLVM has a newer version of this function w/o the error result since 3.8 but this deprecated function
// hasen't yet been removed even in the unreleased LLVM 7. Seems fine to use instead of switching to their
// error diagnostics handler
#[allow(deprecated)]
let success = unsafe {
LLVMParseBitcodeInContext(*context.context, buffer.memory_buffer, &mut module, &mut err_string)
};
Expand Down
42 changes: 18 additions & 24 deletions src/types/array_type.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use llvm_sys::core::{LLVMConstArray, LLVMConstNull, LLVMGetArrayLength};
use llvm_sys::core::{LLVMConstArray, LLVMGetArrayLength};
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};

use AddressSpace;
use context::ContextRef;
use support::LLVMString;
use types::traits::AsTypeRef;
use types::{Type, BasicTypeEnum, PointerType, FunctionType};
use values::{AsValueRef, ArrayValue, PointerValue, IntValue};
use values::{AsValueRef, ArrayValue, IntValue};

/// An `ArrayType` is the type of contiguous constants or variables.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -158,7 +158,7 @@ impl ArrayType {
/// let context = Context::create();
/// let f32_type = context.f32_type();
/// let f32_array_type = f32_type.array_type(3);
/// let f32_array_val = f32_array_type.const_null();
/// let f32_array_val = f32_array_type.const_zero();
/// let f32_array_array = f32_array_type.const_array(&[f32_array_val, f32_array_val]);
///
/// assert!(f32_array_array.is_const());
Expand All @@ -174,7 +174,7 @@ impl ArrayType {
ArrayValue::new(value)
}

/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `ArrayType`.
/// Creates a null `ArrayValue` of this `ArrayType`.
/// It will be automatically assigned this `ArrayType`'s `Context`.
///
/// # Example
Expand All @@ -185,24 +185,24 @@ impl ArrayType {
///
/// // Global Context
/// let f32_type = FloatType::f32_type();
/// let f32_ptr_type = f32_type.ptr_type(AddressSpace::Generic);
/// let f32_ptr_ptr_value = f32_ptr_type.const_null_ptr();
/// let f32_array_type = f32_type.array_type(7);
/// let f32_array_null = f32_array_type.const_null();
///
/// assert!(f32_ptr_ptr_value.is_null());
/// assert!(f32_array_null.is_null());
///
/// // Custom Context
/// let context = Context::create();
/// let f32_type = context.f32_type();
/// let f32_ptr_type = f32_type.ptr_type(AddressSpace::Generic);
/// let f32_ptr_ptr_value = f32_ptr_type.const_null_ptr();
/// let f32_array_type = f32_type.array_type(7);
/// let f32_array_null = f32_array_type.const_null();
///
/// assert!(f32_ptr_ptr_value.is_null());
/// assert!(f32_array_null.is_null());
/// ```
pub fn const_null_ptr(&self) -> PointerValue {
self.array_type.const_null_ptr()
pub fn const_null(&self) -> ArrayValue {
ArrayValue::new(self.array_type.const_null())
}

/// Creates a constant null (zero) value of this `ArrayType`.
/// Creates a constant zero value of this `ArrayType`.
///
/// # Example
///
Expand All @@ -212,16 +212,10 @@ impl ArrayType {
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_zero = i8_array_type.const_null();
///
/// assert!(i8_array_zero.is_null());
/// let i8_array_zero = i8_array_type.const_zero();
/// ```
pub fn const_null(&self) -> ArrayValue {
let null = unsafe {
LLVMConstNull(self.as_type_ref())
};

ArrayValue::new(null)
pub fn const_zero(&self) -> ArrayValue {
ArrayValue::new(self.array_type.const_zero())
}

/// Gets the length of this `ArrayType`.
Expand Down Expand Up @@ -249,7 +243,7 @@ impl ArrayType {
}

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

}
Expand Down
16 changes: 10 additions & 6 deletions src/types/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl AnyTypeEnum {
LLVMTypeKind::LLVMTokenTypeKind => panic!("FIXME: Unsupported type: Token"),
}
}

pub(crate) fn to_basic_type_enum(&self) -> BasicTypeEnum {
BasicTypeEnum::new(self.as_type_ref())
}
}

impl BasicTypeEnum {
Expand All @@ -85,13 +89,13 @@ impl BasicTypeEnum {
LLVMTypeKind::LLVMPointerTypeKind => BasicTypeEnum::PointerType(PointerType::new(type_)),
LLVMTypeKind::LLVMArrayTypeKind => BasicTypeEnum::ArrayType(ArrayType::new(type_)),
LLVMTypeKind::LLVMVectorTypeKind => BasicTypeEnum::VectorType(VectorType::new(type_)),
LLVMTypeKind::LLVMMetadataTypeKind => unreachable!("Unsupported type: Metadata"),
LLVMTypeKind::LLVMX86_MMXTypeKind => unreachable!("Unsupported type: MMX"),
LLVMTypeKind::LLVMLabelTypeKind => unreachable!("Unsupported type: Label"),
LLVMTypeKind::LLVMVoidTypeKind => unreachable!("Unsupported type: VoidType"),
LLVMTypeKind::LLVMFunctionTypeKind => unreachable!("Unsupported type: FunctionType"),
LLVMTypeKind::LLVMMetadataTypeKind => unreachable!("Unsupported basic type: Metadata"),
LLVMTypeKind::LLVMX86_MMXTypeKind => unreachable!("Unsupported basic type: MMX"),
LLVMTypeKind::LLVMLabelTypeKind => unreachable!("Unsupported basic type: Label"),
LLVMTypeKind::LLVMVoidTypeKind => unreachable!("Unsupported basic type: VoidType"),
LLVMTypeKind::LLVMFunctionTypeKind => unreachable!("Unsupported basic type: FunctionType"),
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7")))]
LLVMTypeKind::LLVMTokenTypeKind => panic!("FIXME: Unsupported type: Token"),
LLVMTypeKind::LLVMTokenTypeKind => unreachable!("Unsupported basic type: Token"),
}
}
}
27 changes: 11 additions & 16 deletions src/types/float_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use llvm_sys::core::{LLVMConstReal, LLVMConstNull, LLVMHalfType, LLVMFloatType, LLVMDoubleType, LLVMFP128Type, LLVMPPCFP128Type, LLVMConstRealOfStringAndSize, LLVMX86FP80Type, LLVMConstArray};
use llvm_sys::core::{LLVMConstReal, LLVMHalfType, LLVMFloatType, LLVMDoubleType, LLVMFP128Type, LLVMPPCFP128Type, LLVMConstRealOfStringAndSize, LLVMX86FP80Type, LLVMConstArray};
use llvm_sys::execution_engine::LLVMCreateGenericValueOfFloat;
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};

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

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

/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `FloatType`.
/// Creates a constant null value of this `FloatType`.
/// It will be automatically assigned this `FloatType`'s `Context`.
///
/// # Example
Expand All @@ -148,18 +148,18 @@ impl FloatType {
///
/// // Global Context
/// let f32_type = FloatType::f32_type();
/// let f32_value = f32_type.const_null_ptr();
/// let f32_value = f32_type.const_null();
///
/// // Custom Context
/// let context = Context::create();
/// let f32_type = context.f32_type();
/// let f32_value = f32_type.const_null_ptr();
/// let f32_value = f32_type.const_null();
/// ```
pub fn const_null_ptr(&self) -> PointerValue {
self.float_type.const_null_ptr()
pub fn const_null(&self) -> FloatValue {
FloatValue::new(self.float_type.const_null())
}

/// Creates a constant null (zero) value of this `FloatType`.
/// Creates a constant zero value of this `FloatType`.
///
/// # Example
///
Expand All @@ -168,17 +168,12 @@ impl FloatType {
///
/// let context = Context::create();
/// let f32_type = context.f32_type();
/// let f32_zero = f32_type.const_null();
/// let f32_zero = f32_type.const_zero();
///
/// assert!(f32_zero.is_null());
/// assert_eq!(f32_zero.print_to_string().to_string(), "f32 0");
/// ```
pub fn const_null(&self) -> FloatValue {
let null = unsafe {
LLVMConstNull(self.as_type_ref())
};

FloatValue::new(null)
pub fn const_zero(&self) -> FloatValue {
FloatValue::new(self.float_type.const_zero())
}

// REVIEW: Always true -> const fn?
Expand Down
10 changes: 8 additions & 2 deletions src/types/fn_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use llvm_sys::prelude::LLVMTypeRef;
use std::fmt;
use std::mem::forget;

use AddressSpace;
use context::ContextRef;
use support::LLVMString;
use types::traits::AsTypeRef;
use types::{Type, BasicTypeEnum};
// use values::FunctionValue;
use types::{PointerType, Type, BasicTypeEnum};

// REVIEW: Add a get_return_type() -> Option<BasicTypeEnum>?

#[derive(PartialEq, Eq, Clone, Copy)]
pub struct FunctionType {
Expand All @@ -24,6 +26,10 @@ impl FunctionType {
}
}

pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
self.fn_type.ptr_type(address_space)
}

pub fn is_var_arg(&self) -> bool {
unsafe {
LLVMIsFunctionVarArg(self.as_type_ref()) != 0
Expand Down
27 changes: 11 additions & 16 deletions src/types/int_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use llvm_sys::core::{LLVMInt1Type, LLVMInt8Type, LLVMInt16Type, LLVMInt32Type, LLVMInt64Type, LLVMConstInt, LLVMConstNull, LLVMConstAllOnes, LLVMIntType, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray};
use llvm_sys::core::{LLVMInt1Type, LLVMInt8Type, LLVMInt16Type, LLVMInt32Type, LLVMInt64Type, LLVMConstInt, LLVMConstAllOnes, LLVMIntType, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray};
use llvm_sys::execution_engine::LLVMCreateGenericValueOfInt;
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};

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

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

/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `IntType`. It will be automatically assigned this `IntType`'s `Context`.
/// Creates a constant null value of this `IntType`.
///
/// # Example
/// ```
Expand All @@ -289,18 +289,18 @@ impl IntType {
///
/// // Global Context
/// let i32_type = IntType::i32_type();
/// let i32_value = i32_type.const_null_ptr();
/// let i32_value = i32_type.const_null();
///
/// // Custom Context
/// let context = Context::create();
/// let i32_type = context.i32_type();
/// let i32_value = i32_type.const_null_ptr();
/// let i32_value = i32_type.const_null();
/// ```
pub fn const_null_ptr(&self) -> PointerValue {
self.int_type.const_null_ptr()
pub fn const_null(&self) -> IntValue {
IntValue::new(self.int_type.const_null())
}

/// Creates a constant null (zero) value of this `IntType`.
/// Creates a constant zero value of this `IntType`.
///
/// # Example
///
Expand All @@ -309,17 +309,12 @@ impl IntType {
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_zero = i8_type.const_null();
/// let i8_zero = i8_type.const_zero();
///
/// assert!(i8_zero.is_null());
/// assert_eq!(i8_zero.print_to_string().to_string(), "i8 0");
/// ```
pub fn const_null(&self) -> IntValue {
let null = unsafe {
LLVMConstNull(self.as_type_ref())
};

IntValue::new(null)
pub fn const_zero(&self) -> IntValue {
IntValue::new(self.int_type.const_zero())
}

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

0 comments on commit ee692cb

Please sign in to comment.