Skip to content

Commit

Permalink
Merge pull request rust-lang#101 from jyn514/traits-indirect
Browse files Browse the repository at this point in the history
Add ptr_type and array_type to BasicType
  • Loading branch information
TheDan64 authored Jul 24, 2019
2 parents 0f318d2 + 67f7471 commit 513bddb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
48 changes: 47 additions & 1 deletion src/types/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use llvm_sys::prelude::LLVMTypeRef;

use std::fmt::Debug;

use crate::AddressSpace;
use crate::types::{IntType, FunctionType, FloatType, PointerType, StructType, ArrayType, VectorType, VoidType, Type};
use crate::types::enums::{AnyTypeEnum, BasicTypeEnum};
use crate::values::{IntMathValue, FloatMathValue, PointerMathValue, IntValue, FloatValue, PointerValue, VectorValue};
Expand Down Expand Up @@ -37,10 +38,55 @@ pub trait BasicType: AnyType {
BasicTypeEnum::new(self.as_type_ref())
}

/// Create a function type from this `BasicType`.
/// Create a `FunctionType` with this `BasicType` as its return type.
///
/// Example:
/// ```
/// use inkwell::context::Context;
/// use inkwell::types::BasicType;
///
/// let context = Context::create();
/// let int = context.i32_type();
/// let int_basic_type = int.as_basic_type_enum();
/// assert_eq!(int_basic_type.fn_type(&[], false), int.fn_type(&[], false));
/// ```
fn fn_type(&self, param_types: &[BasicTypeEnum], is_var_args: bool) -> FunctionType {
Type::new(self.as_type_ref()).fn_type(param_types, is_var_args)
}

/// Create an `ArrayType` with this `BasicType` as its elements.
///
/// Example:
/// ```
/// use inkwell::context::Context;
/// use inkwell::types::BasicType;
///
/// let context = Context::create();
/// let int = context.i32_type();
/// let int_basic_type = int.as_basic_type_enum();
/// assert_eq!(int_basic_type.array_type(32), int.array_type(32));
/// ```
fn array_type(&self, size: u32) -> ArrayType {
Type::new(self.as_type_ref()).array_type(size)
}

/// Create a `PointerType` that points to this `BasicType`.
///
/// Example:
/// ```
/// use inkwell::context::Context;
/// use inkwell::types::BasicType;
/// use inkwell::AddressSpace;
///
/// let context = Context::create();
/// let int = context.i32_type();
/// let int_basic_type = int.as_basic_type_enum();
/// let addr_space = AddressSpace::Generic;
/// assert_eq!(int_basic_type.ptr_type(addr_space), int.ptr_type(addr_space));
/// ```
fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
Type::new(self.as_type_ref()).ptr_type(address_space)
}
}

/// Represents an LLVM type that can have integer math operations applied to it.
Expand Down
23 changes: 22 additions & 1 deletion tests/all/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ffi::CString;

use self::inkwell::AddressSpace;
use self::inkwell::context::Context;
use self::inkwell::types::{FloatType, IntType, StructType, VoidType};
use self::inkwell::types::{BasicType, FloatType, IntType, StructType, VoidType};

#[test]
fn test_struct_type() {
Expand Down Expand Up @@ -345,3 +345,24 @@ fn test_ptr_type() {
assert_eq!(fn_ptr_type.get_element_type().into_function_type(), fn_type);
assert_eq!(*fn_ptr_type.get_context(), context);
}

#[test]
fn test_basic_type_enum() {
let context = Context::create();
let addr = AddressSpace::Generic;
let int = context.i32_type();
let types: &[&dyn BasicType] = &[
// ints and floats
&int, &context.i64_type(), &context.f32_type(), &context.f64_type(),
// derived types
&int.array_type(0), &int.ptr_type(addr),
&context.struct_type(&[int.as_basic_type_enum()], false),
&int.vec_type(0)
];
for basic_type in types {
assert_eq!(basic_type.as_basic_type_enum().ptr_type(addr),
basic_type.ptr_type(addr));
assert_eq!(basic_type.as_basic_type_enum().array_type(0),
basic_type.array_type(0));
}
}

0 comments on commit 513bddb

Please sign in to comment.