Skip to content

Commit b27eee9

Browse files
committed
Moved is_sized method into BasicType trait
1 parent 52c7a3d commit b27eee9

File tree

9 files changed

+30
-139
lines changed

9 files changed

+30
-139
lines changed

src/builder.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::basic_block::BasicBlock;
1212
use crate::values::{AggregateValue, AggregateValueEnum, AsValueRef, BasicValue, BasicValueEnum, PhiValue, FunctionValue, IntValue, PointerValue, VectorValue, InstructionValue, GlobalValue, IntMathValue, FloatMathValue, PointerMathValue, InstructionOpcode, CallSiteValue};
1313
#[llvm_versions(3.9..=latest)]
1414
use crate::values::StructValue;
15-
use crate::types::{AsTypeRef, BasicType, BasicTypeEnum, IntMathType, FloatMathType, PointerType, PointerMathType};
15+
use crate::types::{AsTypeRef, BasicType, IntMathType, FloatMathType, PointerType, PointerMathType};
1616

1717
use std::ffi::CString;
1818
use std::marker::PhantomData;
@@ -350,15 +350,7 @@ impl<'ctx> Builder<'ctx> {
350350
// TODOC: Heap allocation
351351
pub fn build_malloc<T: BasicType<'ctx>>(&self, ty: T, name: &str) -> Result<PointerValue<'ctx>, &'static str> {
352352
// LLVMBulidMalloc segfaults if ty is unsized
353-
let is_sized = match ty.as_basic_type_enum() {
354-
BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
355-
BasicTypeEnum::FloatType(ty) => ty.is_sized(),
356-
BasicTypeEnum::IntType(ty) => ty.is_sized(),
357-
BasicTypeEnum::PointerType(ty) => ty.is_sized(),
358-
BasicTypeEnum::StructType(ty) => ty.is_sized(),
359-
BasicTypeEnum::VectorType(ty) => ty.is_sized(),
360-
};
361-
if !is_sized {
353+
if !ty.is_sized() {
362354
return Err("Cannot build malloc call for an unsized type");
363355
}
364356

@@ -379,15 +371,7 @@ impl<'ctx> Builder<'ctx> {
379371
name: &str
380372
) -> Result<PointerValue<'ctx>, &'static str> {
381373
// LLVMBulidArrayMalloc segfaults if ty is unsized
382-
let is_sized = match ty.as_basic_type_enum() {
383-
BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
384-
BasicTypeEnum::FloatType(ty) => ty.is_sized(),
385-
BasicTypeEnum::IntType(ty) => ty.is_sized(),
386-
BasicTypeEnum::PointerType(ty) => ty.is_sized(),
387-
BasicTypeEnum::StructType(ty) => ty.is_sized(),
388-
BasicTypeEnum::VectorType(ty) => ty.is_sized(),
389-
};
390-
if !is_sized {
374+
if !ty.is_sized() {
391375
return Err("Cannot build array malloc call for an unsized type");
392376
}
393377

src/types/array_type.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::AddressSpace;
55
use crate::context::ContextRef;
66
use crate::support::LLVMString;
77
use crate::types::traits::AsTypeRef;
8-
use crate::types::{Type, BasicTypeEnum, PointerType, FunctionType};
8+
use crate::types::{Type, BasicTypeEnum, BasicType, PointerType, FunctionType};
99
use crate::values::{AsValueRef, ArrayValue, IntValue};
1010

1111
/// An `ArrayType` is the type of contiguous constants or variables.
@@ -23,24 +23,6 @@ impl<'ctx> ArrayType<'ctx> {
2323
}
2424
}
2525

26-
// REVIEW: Can be unsized if inner type is opaque struct
27-
/// Gets whether or not this `ArrayType` is sized or not.
28-
///
29-
/// # Example
30-
///
31-
/// ```no_run
32-
/// use inkwell::context::Context;
33-
///
34-
/// let context = Context::create();
35-
/// let i8_type = context.i8_type();
36-
/// let i8_array_type = i8_type.array_type(3);
37-
///
38-
/// assert!(i8_array_type.is_sized());
39-
/// ```
40-
pub fn is_sized(&self) -> bool {
41-
self.array_type.is_sized()
42-
}
43-
4426
// TODO: impl only for ArrayType<!StructType<Opaque>>
4527
/// Gets the size of this `ArrayType`. Value may vary depending on the target architecture.
4628
///

src/types/float_type.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,6 @@ impl<'ctx> FloatType<'ctx> {
150150
FloatValue::new(self.float_type.const_zero())
151151
}
152152

153-
// REVIEW: Always true -> const fn?
154-
/// Gets whether or not this `FloatType` is sized or not. This is likely
155-
/// always true and may be removed in the future.
156-
///
157-
/// # Example
158-
///
159-
/// ```no_run
160-
/// use inkwell::context::Context;
161-
///
162-
/// let context = Context::create();
163-
/// let f32_type = context.f32_type();
164-
///
165-
/// assert!(f32_type.is_sized());
166-
/// ```
167-
pub fn is_sized(&self) -> bool {
168-
self.float_type.is_sized()
169-
}
170-
171153
/// Gets the size of this `FloatType`. Value may vary depending on the target architecture.
172154
///
173155
/// # Example

src/types/int_type.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -249,24 +249,6 @@ impl<'ctx> IntType<'ctx> {
249249
self.int_type.get_context()
250250
}
251251

252-
// REVIEW: Always true -> const fn on trait?
253-
/// Gets whether or not this `IntType` is sized or not. This is likely
254-
/// always true and may be removed in the future.
255-
///
256-
/// # Example
257-
///
258-
/// ```no_run
259-
/// use inkwell::context::Context;
260-
///
261-
/// let context = Context::create();
262-
/// let i8_type = context.i8_type();
263-
///
264-
/// assert!(i8_type.is_sized());
265-
/// ```
266-
pub fn is_sized(&self) -> bool {
267-
self.int_type.is_sized()
268-
}
269-
270252
/// Gets the size of this `IntType`. Value may vary depending on the target architecture.
271253
///
272254
/// # Example

src/types/ptr_type.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::AddressSpace;
55
use crate::context::ContextRef;
66
use crate::support::LLVMString;
77
use crate::types::traits::AsTypeRef;
8-
use crate::types::{AnyTypeEnum, Type, BasicTypeEnum, ArrayType, FunctionType, VectorType};
8+
use crate::types::{AnyTypeEnum, BasicTypeEnum, ArrayType, FunctionType, Type, VectorType};
99
use crate::values::{AsValueRef, ArrayValue, PointerValue, IntValue};
1010

1111
use std::convert::TryFrom;
@@ -25,26 +25,6 @@ impl<'ctx> PointerType<'ctx> {
2525
}
2626
}
2727

28-
// REVIEW: Always true -> const fn on trait?
29-
/// Gets whether or not this `PointerType` is sized or not. This is likely
30-
/// always true and may be removed in the future.
31-
///
32-
/// # Example
33-
///
34-
/// ```no_run
35-
/// use inkwell::context::Context;
36-
/// use inkwell::AddressSpace;
37-
///
38-
/// let context = Context::create();
39-
/// let i8_type = context.i8_type();
40-
/// let i8_ptr_type = i8_type.ptr_type(AddressSpace::Generic);
41-
///
42-
/// assert!(i8_ptr_type.is_sized());
43-
/// ```
44-
pub fn is_sized(&self) -> bool {
45-
self.ptr_type.is_sized()
46-
}
47-
4828
/// Gets the size of this `PointerType`. Value may vary depending on the target architecture.
4929
///
5030
/// # Example

src/types/struct_type.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::AddressSpace;
1010
use crate::context::ContextRef;
1111
use crate::support::LLVMString;
1212
use crate::types::traits::AsTypeRef;
13-
use crate::types::{Type, BasicTypeEnum, ArrayType, PointerType, FunctionType};
13+
use crate::types::{ArrayType, BasicType, BasicTypeEnum, PointerType, FunctionType, Type};
1414
use crate::values::{ArrayValue, BasicValueEnum, StructValue, IntValue, AsValueRef};
1515

1616
/// A `StructType` is the type of a heterogeneous container of types.
@@ -103,25 +103,6 @@ impl<'ctx> StructType<'ctx> {
103103
StructValue::new(self.struct_type.const_zero())
104104
}
105105

106-
// REVIEW: Can be false if opaque. To make a const fn, we'd have to have
107-
// have separate impls for Struct<Opaque> and StructType<T*>
108-
/// Gets whether or not this `StructType` is sized or not.
109-
///
110-
/// # Example
111-
///
112-
/// ```no_run
113-
/// use inkwell::context::Context;
114-
///
115-
/// let context = Context::create();
116-
/// let f32_type = context.f32_type();
117-
/// let f32_struct_type = context.struct_type(&[f32_type.into()], false);
118-
///
119-
/// assert!(f32_struct_type.is_sized());
120-
/// ```
121-
pub fn is_sized(&self) -> bool {
122-
self.struct_type.is_sized()
123-
}
124-
125106
// TODO: impl it only for StructType<T*>?
126107
/// Gets the size of this `StructType`. Value may vary depending on the target architecture.
127108
///

src/types/traits.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ pub trait BasicType<'ctx>: AnyType<'ctx> {
4141

4242
/// Create a `FunctionType` with this `BasicType` as its return type.
4343
///
44-
/// Example:
44+
/// # Example:
45+
///
4546
/// ```no_run
4647
/// use inkwell::context::Context;
4748
/// use inkwell::types::BasicType;
@@ -55,6 +56,25 @@ pub trait BasicType<'ctx>: AnyType<'ctx> {
5556
Type::new(self.as_type_ref()).fn_type(param_types, is_var_args)
5657
}
5758

59+
/// Determines whether or not this `BasicType` is sized or not.
60+
/// For example, opaque structs are unsized.
61+
///
62+
/// # Example
63+
///
64+
/// ```no_run
65+
/// use inkwell::context::Context;
66+
/// use inkwell::types::BasicType;
67+
///
68+
/// let context = Context::create();
69+
/// let f32_type = context.f32_type();
70+
/// let f32_vec_type = f32_type.vec_type(40);
71+
///
72+
/// assert!(f32_vec_type.is_sized());
73+
/// ```
74+
fn is_sized(&self) -> bool {
75+
Type::new(self.as_type_ref()).is_sized()
76+
}
77+
5878
/// Create an `ArrayType` with this `BasicType` as its elements.
5979
///
6080
/// Example:
@@ -67,7 +87,6 @@ pub trait BasicType<'ctx>: AnyType<'ctx> {
6787
/// let int_basic_type = int.as_basic_type_enum();
6888
/// assert_eq!(int_basic_type.array_type(32), int.array_type(32));
6989
/// ```
70-
// FIXME: We shouldn't be able to create arrays of void types
7190
fn array_type(&self, size: u32) -> ArrayType<'ctx> {
7291
Type::new(self.as_type_ref()).array_type(size)
7392
}
@@ -86,7 +105,6 @@ pub trait BasicType<'ctx>: AnyType<'ctx> {
86105
/// let addr_space = AddressSpace::Generic;
87106
/// assert_eq!(int_basic_type.ptr_type(addr_space), int.ptr_type(addr_space));
88107
/// ```
89-
// FIXME: We shouldn't be able to create pointer of void types
90108
fn ptr_type(&self, address_space: AddressSpace) -> PointerType<'ctx> {
91109
Type::new(self.as_type_ref()).ptr_type(address_space)
92110
}

src/types/vec_type.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
44
use crate::AddressSpace;
55
use crate::context::ContextRef;
66
use crate::support::LLVMString;
7-
use crate::types::{ArrayType, BasicTypeEnum, Type, traits::AsTypeRef, FunctionType, PointerType};
7+
use crate::types::{ArrayType, BasicTypeEnum, BasicType, Type, traits::AsTypeRef, FunctionType, PointerType};
88
use crate::values::{AsValueRef, ArrayValue, BasicValue, VectorValue, IntValue};
99

1010
/// A `VectorType` is the type of a multiple value SIMD constant or variable.
@@ -22,26 +22,8 @@ impl<'ctx> VectorType<'ctx> {
2222
}
2323
}
2424

25-
// REVIEW: Can be unsized if inner type is opaque struct
26-
/// Gets whether or not this `VectorType` is sized or not.
27-
///
28-
/// # Example
29-
///
30-
/// ```no_run
31-
/// use inkwell::context::Context;
32-
///
33-
/// let context = Context::create();
34-
/// let f32_type = context.f32_type();
35-
/// let f32_vec_type = f32_type.vec_type(40);
36-
///
37-
/// assert!(f32_vec_type.is_sized());
38-
/// ```
39-
pub fn is_sized(&self) -> bool {
40-
self.vec_type.is_sized()
41-
}
42-
4325
// TODO: impl only for VectorType<!StructType<Opaque>>
44-
// REVIEW: What about Opaque struct hiding in deeper levels?
26+
// REVIEW: What about Opaque struct hiding in deeper levels
4527
// like VectorType<ArrayType<StructType<Opaque>>>?
4628
/// Gets the size of this `VectorType`. Value may vary depending on the target architecture.
4729
///

src/values/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub trait BasicValue<'ctx>: AnyValue<'ctx> {
9090
}
9191

9292
// REVIEW: Possible encompassing methods to implement:
93-
// is_sized, get/set metadata
93+
// get/set metadata
9494
}
9595

9696
/// Represents a value which is permitted in integer math operations

0 commit comments

Comments
 (0)