Skip to content

Commit 513bddb

Browse files
authored
Merge pull request rust-lang#101 from jyn514/traits-indirect
Add ptr_type and array_type to BasicType
2 parents 0f318d2 + 67f7471 commit 513bddb

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/types/traits.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use llvm_sys::prelude::LLVMTypeRef;
22

33
use std::fmt::Debug;
44

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

40-
/// Create a function type from this `BasicType`.
41+
/// Create a `FunctionType` with this `BasicType` as its return type.
42+
///
43+
/// Example:
44+
/// ```
45+
/// use inkwell::context::Context;
46+
/// use inkwell::types::BasicType;
47+
///
48+
/// let context = Context::create();
49+
/// let int = context.i32_type();
50+
/// let int_basic_type = int.as_basic_type_enum();
51+
/// assert_eq!(int_basic_type.fn_type(&[], false), int.fn_type(&[], false));
52+
/// ```
4153
fn fn_type(&self, param_types: &[BasicTypeEnum], is_var_args: bool) -> FunctionType {
4254
Type::new(self.as_type_ref()).fn_type(param_types, is_var_args)
4355
}
56+
57+
/// Create an `ArrayType` with this `BasicType` as its elements.
58+
///
59+
/// Example:
60+
/// ```
61+
/// use inkwell::context::Context;
62+
/// use inkwell::types::BasicType;
63+
///
64+
/// let context = Context::create();
65+
/// let int = context.i32_type();
66+
/// let int_basic_type = int.as_basic_type_enum();
67+
/// assert_eq!(int_basic_type.array_type(32), int.array_type(32));
68+
/// ```
69+
fn array_type(&self, size: u32) -> ArrayType {
70+
Type::new(self.as_type_ref()).array_type(size)
71+
}
72+
73+
/// Create a `PointerType` that points to this `BasicType`.
74+
///
75+
/// Example:
76+
/// ```
77+
/// use inkwell::context::Context;
78+
/// use inkwell::types::BasicType;
79+
/// use inkwell::AddressSpace;
80+
///
81+
/// let context = Context::create();
82+
/// let int = context.i32_type();
83+
/// let int_basic_type = int.as_basic_type_enum();
84+
/// let addr_space = AddressSpace::Generic;
85+
/// assert_eq!(int_basic_type.ptr_type(addr_space), int.ptr_type(addr_space));
86+
/// ```
87+
fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
88+
Type::new(self.as_type_ref()).ptr_type(address_space)
89+
}
4490
}
4591

4692
/// Represents an LLVM type that can have integer math operations applied to it.

tests/all/test_types.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ffi::CString;
44

55
use self::inkwell::AddressSpace;
66
use self::inkwell::context::Context;
7-
use self::inkwell::types::{FloatType, IntType, StructType, VoidType};
7+
use self::inkwell::types::{BasicType, FloatType, IntType, StructType, VoidType};
88

99
#[test]
1010
fn test_struct_type() {
@@ -345,3 +345,24 @@ fn test_ptr_type() {
345345
assert_eq!(fn_ptr_type.get_element_type().into_function_type(), fn_type);
346346
assert_eq!(*fn_ptr_type.get_context(), context);
347347
}
348+
349+
#[test]
350+
fn test_basic_type_enum() {
351+
let context = Context::create();
352+
let addr = AddressSpace::Generic;
353+
let int = context.i32_type();
354+
let types: &[&dyn BasicType] = &[
355+
// ints and floats
356+
&int, &context.i64_type(), &context.f32_type(), &context.f64_type(),
357+
// derived types
358+
&int.array_type(0), &int.ptr_type(addr),
359+
&context.struct_type(&[int.as_basic_type_enum()], false),
360+
&int.vec_type(0)
361+
];
362+
for basic_type in types {
363+
assert_eq!(basic_type.as_basic_type_enum().ptr_type(addr),
364+
basic_type.ptr_type(addr));
365+
assert_eq!(basic_type.as_basic_type_enum().array_type(0),
366+
basic_type.array_type(0));
367+
}
368+
}

0 commit comments

Comments
 (0)