diff --git a/nac3core/src/codegen/structs/list.rs b/nac3core/src/codegen/structs/list.rs new file mode 100644 index 00000000..b21a2f8a --- /dev/null +++ b/nac3core/src/codegen/structs/list.rs @@ -0,0 +1,42 @@ +use crate::codegen::{model::*, CodeGenContext}; + +/// Fields of [`List`] +pub struct ListFields<'ctx, T: Model<'ctx>> { + /// Length of the list + pub size: Field>, + /// Base pointer of the list + pub data: Field>, +} + +/// nac3core's `List` definition +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct List<'ctx, T: Model<'ctx>> { + pub sizet: SizeTModel<'ctx>, + pub element: T, +} + +impl<'ctx, T: Model<'ctx> + 'ctx> StructKind<'ctx> for List<'ctx, T> { + type Fields = ListFields<'ctx, T>; + + fn struct_name(&self) -> &'static str { + "List" + } + + fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields { + Self::Fields { + size: builder.add_field("size", self.sizet), + data: builder.add_field("data", PointerModel(self.element)), + } + } +} + +impl<'ctx, T: Model<'ctx> + 'ctx> Pointer<'ctx, StructModel>> { + pub fn as_slice( + &self, + ctx: &CodeGenContext<'ctx, '_>, + ) -> ArraySlice<'ctx, SizeTModel<'ctx>, T> { + let num_elements = self.gep(ctx, |f| f.size).load(ctx, "num_elements"); + let pointer = self.gep(ctx, |f| f.data).load(ctx, "base"); + ArraySlice { num_elements, pointer } + } +} diff --git a/nac3core/src/codegen/structs/mod.rs b/nac3core/src/codegen/structs/mod.rs index 51c9cab0..1220e8d7 100644 --- a/nac3core/src/codegen/structs/mod.rs +++ b/nac3core/src/codegen/structs/mod.rs @@ -1,3 +1,4 @@ pub mod cslice; pub mod exception; +pub mod list; pub mod ndarray;