Skip to content

Commit

Permalink
Added ArrayType & PointerType docs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Sep 22, 2018
1 parent 3ca44a2 commit f9cb692
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 9 deletions.
186 changes: 186 additions & 0 deletions src/types/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use types::traits::AsTypeRef;
use types::{Type, BasicTypeEnum, PointerType, FunctionType};
use values::{AsValueRef, ArrayValue, PointerValue, IntValue};

/// An `ArrayType` is the type of contiguous constants or variables.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct ArrayType {
array_type: Type,
Expand All @@ -23,11 +24,36 @@ impl ArrayType {
}

// REVIEW: Can be unsized if inner type is opaque struct
/// Gets whether or not this `ArrayType` is sized or not.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
///
/// assert!(i8_array_type.is_sized());
/// ```
pub fn is_sized(&self) -> bool {
self.array_type.is_sized()
}

// TODO: impl only for ArrayType<!StructType<Opaque>>
/// Gets the size of this `ArrayType`. Value may vary depending on the target architecture.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_type_size = i8_array_type.size_of();
/// ```
pub fn size_of(&self) -> Option<IntValue> {
if self.is_sized() {
return Some(self.array_type.size_of())
Expand All @@ -36,26 +62,107 @@ impl ArrayType {
None
}

/// Gets the alignment of this `ArrayType`. Value may vary depending on the target architecture.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_type_alignment = i8_array_type.get_alignment();
/// ```
pub fn get_alignment(&self) -> IntValue {
self.array_type.get_alignment()
}

/// Creates a `PointerType` with this `ArrayType` for its element type.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
/// use inkwell::AddressSpace;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_ptr_type = i8_array_type.ptr_type(AddressSpace::Generic);
///
/// assert_eq!(i8_array_ptr_type.get_element_type().into_array_type(), i8_array_type);
/// ```
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
self.array_type.ptr_type(address_space)
}

/// Gets a reference to the `Context` this `ArrayType` was created in.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
///
/// assert_eq!(*i8_array_type.get_context(), context);
/// ```
pub fn get_context(&self) -> ContextRef {
self.array_type.get_context()
}

/// Creates a `FunctionType` with this `ArrayType` for its return type.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let fn_type = i8_array_type.fn_type(&[], false);
/// ```
pub fn fn_type(&self, param_types: &[BasicTypeEnum], is_var_args: bool) -> FunctionType {
self.array_type.fn_type(param_types, is_var_args)
}

/// Creates an `ArrayType` with this `ArrayType` for its element type.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_array_type = i8_array_type.array_type(3);
///
/// assert_eq!(i8_array_array_type.len(), 3);
/// assert_eq!(i8_array_array_type.get_element_type().into_array_type(), i8_array_type);
/// ```
pub fn array_type(&self, size: u32) -> ArrayType {
self.array_type.array_type(size)
}

/// Creates a constant `ArrayValue`.
///
/// # Example
/// ```no_run
/// use inkwell::context::Context;
///
/// 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_array = f32_array_type.const_array(&[f32_array_val, f32_array_val]);
///
/// assert!(f32_array_array.is_const());
/// ```
pub fn const_array(&self, values: &[ArrayValue]) -> ArrayValue {
let mut values: Vec<LLVMValueRef> = values.iter()
.map(|val| val.as_value_ref())
Expand All @@ -67,10 +174,48 @@ impl ArrayType {
ArrayValue::new(value)
}

/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `ArrayType`.
/// It will be automatically assigned this `ArrayType`'s `Context`.
///
/// # Example
/// ```
/// use inkwell::context::Context;
/// use inkwell::types::FloatType;
/// use inkwell::AddressSpace;
///
/// // 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();
///
/// assert!(f32_ptr_ptr_value.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();
///
/// assert!(f32_ptr_ptr_value.is_null());
/// ```
pub fn const_null_ptr(&self) -> PointerValue {
self.array_type.const_null_ptr()
}

/// Creates a constant null (zero) value of this `ArrayType`.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// 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());
/// ```
pub fn const_null(&self) -> ArrayValue {
let null = unsafe {
LLVMConstNull(self.as_type_ref())
Expand All @@ -79,27 +224,68 @@ impl ArrayType {
ArrayValue::new(null)
}

/// Gets the length of this `ArrayType`.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
///
/// assert_eq!(i8_array_type.len(), 3);
/// ```
pub fn len(&self) -> u32 {
unsafe {
LLVMGetArrayLength(self.as_type_ref())
}
}

/// Prints the definition of a `ArrayType` to a `LLVMString`.
pub fn print_to_string(&self) -> LLVMString {
self.array_type.print_to_string()
}

// See Type::print_to_stderr note on 5.0+ status
/// Prints the definition of an `IntType` 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()
}

/// Creates an undefined instance of a `ArrayType`.
///
/// # Example
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_undef = i8_array_type.get_undef();
///
/// assert!(i8_array_undef.is_undef());
/// ```
pub fn get_undef(&self) -> ArrayValue {
ArrayValue::new(self.array_type.get_undef())
}

// SubType: ArrayType<BT> -> BT?
/// Gets the element type of this `ArrayType`.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let i8_type = context.i8_type();
/// let i8_array_type = i8_type.array_type(3);
///
/// 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()
}
Expand Down
3 changes: 1 addition & 2 deletions src/types/float_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl FloatType {
self.float_type.size_of()
}

/// Gets the aligment of this `FloatType`. Value may vary depending on the target architecture.
/// Gets the alignment of this `FloatType`. Value may vary depending on the target architecture.
///
/// # Example
///
Expand Down Expand Up @@ -402,7 +402,6 @@ impl FloatType {
/// # Example
/// ```no_run
/// use inkwell::context::Context;
/// use inkwell::AddressSpace;
///
/// let context = Context::create();
/// let f32_type = context.f32_type();
Expand Down
2 changes: 1 addition & 1 deletion src/types/int_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl IntType {
self.int_type.size_of()
}

/// Gets the aligment of this `IntType`. Value may vary depending on the target architecture.
/// Gets the alignment of this `IntType`. Value may vary depending on the target architecture.
///
/// # Example
///
Expand Down
2 changes: 2 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#[deny(missing_docs)]
mod array_type;
mod enums;
#[deny(missing_docs)]
mod float_type;
mod fn_type;
#[deny(missing_docs)]
mod int_type;
#[deny(missing_docs)]
mod ptr_type;
mod struct_type;
mod traits;
Expand Down
Loading

0 comments on commit f9cb692

Please sign in to comment.