ArrayLen::{get_length -> length}

This commit is contained in:
lyken 2024-08-27 16:33:31 +08:00
parent 5c4ba09e2f
commit dbcfc9538a
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8
1 changed files with 8 additions and 8 deletions

View File

@ -12,7 +12,7 @@ use super::*;
/// Trait for Rust structs identifying length values for [`Array`]. /// Trait for Rust structs identifying length values for [`Array`].
pub trait ArrayLen: fmt::Debug + Clone + Copy { pub trait ArrayLen: fmt::Debug + Clone + Copy {
fn get_length(&self) -> u32; fn length(&self) -> u32;
} }
/// A statically known length. /// A statically known length.
@ -24,13 +24,13 @@ pub struct Len<const N: u32>;
pub struct AnyLen(pub u32); pub struct AnyLen(pub u32);
impl<const N: u32> ArrayLen for Len<N> { impl<const N: u32> ArrayLen for Len<N> {
fn get_length(&self) -> u32 { fn length(&self) -> u32 {
N N
} }
} }
impl ArrayLen for AnyLen { impl ArrayLen for AnyLen {
fn get_length(&self) -> u32 { fn length(&self) -> u32 {
self.0 self.0
} }
} }
@ -51,7 +51,7 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Model<'ctx> for Array<Len, Item> {
type Type = ArrayType<'ctx>; type Type = ArrayType<'ctx>;
fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type { fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type {
self.item.get_type(generator, ctx).array_type(self.len.get_length()) self.item.get_type(generator, ctx).array_type(self.len.length())
} }
fn check_type<T: BasicType<'ctx>, G: CodeGenerator + ?Sized>( fn check_type<T: BasicType<'ctx>, G: CodeGenerator + ?Sized>(
@ -65,11 +65,11 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Model<'ctx> for Array<Len, Item> {
return Err(ModelError(format!("Expecting ArrayType, but got {ty:?}"))); return Err(ModelError(format!("Expecting ArrayType, but got {ty:?}")));
}; };
if ty.len() != self.len.get_length() { if ty.len() != self.len.length() {
return Err(ModelError(format!( return Err(ModelError(format!(
"Expecting ArrayType with size {}, but got an ArrayType with size {}", "Expecting ArrayType with size {}, but got an ArrayType with size {}",
ty.len(), ty.len(),
self.len.get_length() self.len.length()
))); )));
} }
@ -97,9 +97,9 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Instance<'ctx, Ptr<Array<Len, Item>
/// Like `gep` but `i` is a constant. /// Like `gep` but `i` is a constant.
pub fn gep_const(&self, ctx: &CodeGenContext<'ctx, '_>, i: u64) -> Instance<'ctx, Ptr<Item>> { pub fn gep_const(&self, ctx: &CodeGenContext<'ctx, '_>, i: u64) -> Instance<'ctx, Ptr<Item>> {
assert!( assert!(
i < u64::from(self.model.0.len.get_length()), i < u64::from(self.model.0.len.length()),
"Index {i} is out of bounds. Array length = {}", "Index {i} is out of bounds. Array length = {}",
self.model.0.len.get_length() self.model.0.len.length()
); );
let i = ctx.ctx.i32_type().const_int(i, false); let i = ctx.ctx.i32_type().const_int(i, false);