diff --git a/nac3core/src/codegen/model/ptr.rs b/nac3core/src/codegen/model/ptr.rs index e059ab17..e2e8ff8b 100644 --- a/nac3core/src/codegen/model/ptr.rs +++ b/nac3core/src/codegen/model/ptr.rs @@ -9,14 +9,27 @@ use crate::codegen::{llvm_intrinsics::call_memcpy_generic, CodeGenContext, CodeG use super::*; +/// A model for [`PointerType`]. +// TODO: LLVM 15: `Item` is a Rust type-hint for the LLVM type of value the `.store()/.load()` family +// of functions return. If a truly opaque pointer is needed, tell the programmer to use `OpaquePtr`. #[derive(Debug, Clone, Copy, Default)] pub struct Ptr(pub Item); +/// An opaque pointer. Like [`Ptr`] but without any Rust type-hints about its element type. +/// +/// `.load()/.store()` is not available for [`Instance`]s of opaque pointers. +pub type OpaquePtr = Ptr<()>; + +// TODO: LLVM 15: `Item: Model<'ctx>` don't even need to be a model anymore. It will only be +// a type-hint for the `.load()/.store()` functions for the `pointee_ty`. +// +// See https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html#method.build_load. impl<'ctx, Item: Model<'ctx>> Model<'ctx> for Ptr { type Value = PointerValue<'ctx>; type Type = PointerType<'ctx>; fn get_type(&self, generator: &G, ctx: &'ctx Context) -> Self::Type { + // TODO: LLVM 15: ctx.ptr_type(AddressSpace::default()) self.0.get_type(generator, ctx).ptr_type(AddressSpace::default()) } @@ -48,13 +61,13 @@ impl<'ctx, Item: Model<'ctx>> Model<'ctx> for Ptr { } } -impl<'ctx, Element: Model<'ctx>> Ptr { +impl<'ctx, Item: Model<'ctx>> Ptr { /// Return a ***constant*** nullptr. pub fn nullptr( &self, generator: &mut G, ctx: &'ctx Context, - ) -> Instance<'ctx, Ptr> { + ) -> Instance<'ctx, Ptr> { let ptr = self.get_type(generator, ctx).const_null(); self.believe_value(ptr) } @@ -65,7 +78,12 @@ impl<'ctx, Element: Model<'ctx>> Ptr { generator: &mut G, ctx: &CodeGenContext<'ctx, '_>, ptr: PointerValue<'ctx>, - ) -> Instance<'ctx, Ptr> { + ) -> Instance<'ctx, Ptr> { + // TODO: LLVM 15: Write in an impl where `Item` does not have to be `Model<'ctx>`. + // TODO: LLVM 15: This function will only have to be: + // ``` + // return self.believe_value(ptr); + // ``` let t = self.get_type(generator, ctx.ctx); let ptr = ctx.builder.build_pointer_cast(ptr, t, "").unwrap(); self.believe_value(ptr) @@ -153,6 +171,7 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr> { ctx: &CodeGenContext<'ctx, '_>, new_item: NewItem, ) -> Instance<'ctx, Ptr> { + // TODO: LLVM 15: Write in an impl where `Item` does not have to be `Model<'ctx>`. Ptr(new_item).pointer_cast(generator, ctx, self.value) }