From 2c4bf3ce59f3cde09973e4ac694bd5d44032869c Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 19 Mar 2024 18:24:30 +0800 Subject: [PATCH] core: Allow unsized CodeGenerator to be passed to some codegen functions Enables codegen_callback to call these codegen functions as well. --- nac3core/src/codegen/classes.rs | 64 ++++++++++++++++---------------- nac3core/src/codegen/expr.rs | 44 ++++++++++++---------- nac3core/src/codegen/irrt/mod.rs | 40 ++++++++++---------- nac3core/src/codegen/mod.rs | 8 ++-- nac3core/src/codegen/numpy.rs | 59 +++++++++++++++-------------- nac3core/src/codegen/stmt.rs | 28 +++++++------- 6 files changed, 126 insertions(+), 117 deletions(-) diff --git a/nac3core/src/codegen/classes.rs b/nac3core/src/codegen/classes.rs index b74a2cdfe..656b13d35 100644 --- a/nac3core/src/codegen/classes.rs +++ b/nac3core/src/codegen/classes.rs @@ -125,10 +125,10 @@ impl<'ctx> ListValue<'ctx> { } /// Stores the `size` of this `list` into this instance. - pub fn store_size( + pub fn store_size( &self, ctx: &CodeGenContext<'ctx, '_>, - generator: &dyn CodeGenerator, + generator: &G, size: IntValue<'ctx>, ) { debug_assert_eq!(size.get_type(), generator.get_size_type(ctx.ctx)); @@ -192,10 +192,10 @@ impl<'ctx> ListDataProxy<'ctx> { } /// Returns the pointer to the data at the `idx`-th index. - pub fn ptr_offset( + pub fn ptr_offset( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, idx: IntValue<'ctx>, name: Option<&str>, ) -> PointerValue<'ctx> { @@ -235,10 +235,10 @@ impl<'ctx> ListDataProxy<'ctx> { } /// Returns the data at the `idx`-th flattened index. - pub fn get( + pub fn get( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, idx: IntValue<'ctx>, name: Option<&str>, ) -> BasicValueEnum<'ctx> { @@ -507,10 +507,10 @@ impl<'ctx> NDArrayValue<'ctx> { } /// Stores the number of dimensions `ndims` into this instance. - pub fn store_ndims( + pub fn store_ndims( &self, ctx: &CodeGenContext<'ctx, '_>, - generator: &dyn CodeGenerator, + generator: &G, ndims: IntValue<'ctx>, ) { debug_assert_eq!(ndims.get_type(), generator.get_size_type(ctx.ctx)); @@ -642,10 +642,10 @@ impl<'ctx> NDArrayDimsProxy<'ctx> { } /// Returns the pointer to the size of the `idx`-th dimension. - pub fn ptr_offset( + pub fn ptr_offset( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, idx: IntValue<'ctx>, name: Option<&str>, ) -> PointerValue<'ctx> { @@ -685,10 +685,10 @@ impl<'ctx> NDArrayDimsProxy<'ctx> { } /// Returns the size of the `idx`-th dimension. - pub fn get( + pub fn get( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, idx: IntValue<'ctx>, name: Option<&str>, ) -> IntValue<'ctx> { @@ -730,10 +730,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { } /// Returns the pointer to the data at the `idx`-th flattened index. - pub fn ptr_to_data_flattened( + pub fn ptr_to_data_flattened( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, idx: IntValue<'ctx>, name: Option<&str>, ) -> PointerValue<'ctx> { @@ -775,10 +775,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { } /// Returns the data at the `idx`-th flattened index. - pub fn get_flattened( + pub fn get_flattened( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, idx: IntValue<'ctx>, name: Option<&str>, ) -> BasicValueEnum<'ctx> { @@ -789,10 +789,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { /// # Safety /// /// This function should be called with valid indices. - pub unsafe fn ptr_offset_unchecked( + pub unsafe fn ptr_offset_unchecked( &self, ctx: &CodeGenContext<'ctx, '_>, - generator: &dyn CodeGenerator, + generator: &G, indices: ListValue<'ctx>, name: Option<&str>, ) -> PointerValue<'ctx> { @@ -821,10 +821,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { /// # Safety /// /// This function should be called with valid indices. - pub unsafe fn ptr_offset_unchecked_const( + pub unsafe fn ptr_offset_unchecked_const( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, indices: ArrayValue<'ctx>, name: Option<&str>, ) -> PointerValue<'ctx> { @@ -845,10 +845,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { } /// Returns the pointer to the data at the index specified by `indices`. - pub fn ptr_offset_const( + pub fn ptr_offset_const( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, indices: ArrayValue<'ctx>, name: Option<&str>, ) -> PointerValue<'ctx> { @@ -910,10 +910,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { } /// Returns the pointer to the data at the index specified by `indices`. - pub fn ptr_offset( + pub fn ptr_offset( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, indices: ListValue<'ctx>, name: Option<&str>, ) -> PointerValue<'ctx> { @@ -979,10 +979,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { /// # Safety /// /// This function should be called with valid indices. - pub unsafe fn get_unchecked_const( + pub unsafe fn get_unchecked_const( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, indices: ArrayValue<'ctx>, name: Option<&str>, ) -> BasicValueEnum<'ctx> { @@ -993,10 +993,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { /// # Safety /// /// This function should be called with valid indices. - pub unsafe fn get_unchecked( + pub unsafe fn get_unchecked( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &dyn CodeGenerator, + generator: &G, indices: ListValue<'ctx>, name: Option<&str>, ) -> BasicValueEnum<'ctx> { @@ -1005,10 +1005,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { } /// Returns the data at the index specified by `indices`. - pub fn get_const( + pub fn get_const( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, indices: ArrayValue<'ctx>, name: Option<&str>, ) -> BasicValueEnum<'ctx> { @@ -1017,10 +1017,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> { } /// Returns the data at the index specified by `indices`. - pub fn get( + pub fn get( &self, ctx: &mut CodeGenContext<'ctx, '_>, - generator: &mut dyn CodeGenerator, + generator: &mut G, indices: ListValue<'ctx>, name: Option<&str>, ) -> BasicValueEnum<'ctx> { diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 5711a6a17..faa3ee127 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -103,9 +103,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { index } - pub fn gen_symbol_val( + pub fn gen_symbol_val( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, val: &SymbolValue, ty: Type, ) -> BasicValueEnum<'ctx> { @@ -174,9 +174,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { } /// See [`get_llvm_type`]. - pub fn get_llvm_type( + pub fn get_llvm_type( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, ty: Type, ) -> BasicTypeEnum<'ctx> { get_llvm_type( @@ -191,9 +191,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { } /// See [`get_llvm_abi_type`]. - pub fn get_llvm_abi_type( + pub fn get_llvm_abi_type( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, ty: Type, ) -> BasicTypeEnum<'ctx> { get_llvm_abi_type( @@ -209,9 +209,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { } /// Generates an LLVM variable for a [constant value][value] with a given [type][ty]. - pub fn gen_const( + pub fn gen_const( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, value: &Constant, ty: Type, ) -> Option> { @@ -291,9 +291,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { } /// Generates a binary operation `op` between two integral operands `lhs` and `rhs`. - pub fn gen_int_ops( + pub fn gen_int_ops( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, op: &Operator, lhs: BasicValueEnum<'ctx>, rhs: BasicValueEnum<'ctx>, @@ -492,17 +492,21 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { } /// Helper function for generating a LLVM variable storing a [String]. - pub fn gen_string>( + pub fn gen_string( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, s: S, - ) -> BasicValueEnum<'ctx> { + ) -> BasicValueEnum<'ctx> + where + G: CodeGenerator + ?Sized, + S: Into, + { self.gen_const(generator, &Constant::Str(s.into()), self.primitives.str).unwrap() } - pub fn raise_exn( + pub fn raise_exn( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, name: &str, msg: BasicValueEnum<'ctx>, params: [Option>; 3], @@ -546,9 +550,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { gen_raise(generator, self, Some(&zelf.into()), loc); } - pub fn make_assert( + pub fn make_assert( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, cond: IntValue<'ctx>, err_name: &str, err_msg: &str, @@ -559,9 +563,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { self.make_assert_impl(generator, cond, err_name, err_msg, params, loc); } - pub fn make_assert_impl( + pub fn make_assert_impl( &mut self, - generator: &mut dyn CodeGenerator, + generator: &mut G, cond: IntValue<'ctx>, err_name: &str, err_msg: BasicValueEnum<'ctx>, @@ -878,7 +882,7 @@ pub fn destructure_range<'ctx>( /// Returns an instance of [`PointerValue`] pointing to the List structure. The List structure is /// defined as `type { ty*, size_t }` in LLVM, where the first element stores the pointer to the /// data, and the second element stores the size of the List. -pub fn allocate_list<'ctx, G: CodeGenerator>( +pub fn allocate_list<'ctx, G: CodeGenerator + ?Sized>( generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, ty: BasicTypeEnum<'ctx>, diff --git a/nac3core/src/codegen/irrt/mod.rs b/nac3core/src/codegen/irrt/mod.rs index 3e03d5ba9..bf3404407 100644 --- a/nac3core/src/codegen/irrt/mod.rs +++ b/nac3core/src/codegen/irrt/mod.rs @@ -39,8 +39,8 @@ pub fn load_irrt(ctx: &Context) -> Module { // repeated squaring method adapted from GNU Scientific Library: // https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c -pub fn integer_power<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn integer_power<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, base: IntValue<'ctx>, exp: IntValue<'ctx>, @@ -81,8 +81,8 @@ pub fn integer_power<'ctx>( .unwrap() } -pub fn calculate_len_for_slice_range<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn calculate_len_for_slice_range<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, start: IntValue<'ctx>, end: IntValue<'ctx>, @@ -303,8 +303,8 @@ pub fn handle_slice_index_bound<'ctx, G: CodeGenerator>( /// This function handles 'end' **inclusively**. /// Order of tuples `assign_idx` and `value_idx` is ('start', 'end', 'step'). /// Negative index should be handled before entering this function -pub fn list_slice_assignment<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn list_slice_assignment<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, ty: BasicTypeEnum<'ctx>, dest_arr: ListValue<'ctx>, @@ -468,8 +468,8 @@ pub fn list_slice_assignment<'ctx>( } /// Generates a call to `isinf` in IR. Returns an `i1` representing the result. -pub fn call_isinf<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn call_isinf<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &CodeGenContext<'ctx, '_>, v: FloatValue<'ctx>, ) -> IntValue<'ctx> { @@ -489,8 +489,8 @@ pub fn call_isinf<'ctx>( } /// Generates a call to `isnan` in IR. Returns an `i1` representing the result. -pub fn call_isnan<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn call_isnan<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &CodeGenContext<'ctx, '_>, v: FloatValue<'ctx>, ) -> IntValue<'ctx> { @@ -574,8 +574,8 @@ pub fn call_j0<'ctx>( /// /// * `num_dims` - An [`IntValue`] containing the number of dimensions. /// * `dims` - A [`PointerValue`] to an array containing the size of each dimension. -pub fn call_ndarray_calc_size<'ctx>( - generator: &dyn CodeGenerator, +pub fn call_ndarray_calc_size<'ctx, G: CodeGenerator + ?Sized>( + generator: &G, ctx: &mut CodeGenContext<'ctx, '_>, num_dims: IntValue<'ctx>, dims: PointerValue<'ctx>, @@ -622,8 +622,8 @@ pub fn call_ndarray_calc_size<'ctx>( /// * `index` - The index to compute the multidimensional index for. /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an /// `NDArray`. -pub fn call_ndarray_calc_nd_indices<'ctx>( - generator: &dyn CodeGenerator, +pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>( + generator: &G, ctx: &mut CodeGenContext<'ctx, '_>, index: IntValue<'ctx>, ndarray: NDArrayValue<'ctx>, @@ -677,8 +677,8 @@ pub fn call_ndarray_calc_nd_indices<'ctx>( indices } -fn call_ndarray_flatten_index_impl<'ctx>( - generator: &dyn CodeGenerator, +fn call_ndarray_flatten_index_impl<'ctx, G: CodeGenerator + ?Sized>( + generator: &G, ctx: &CodeGenContext<'ctx, '_>, ndarray: NDArrayValue<'ctx>, indices: PointerValue<'ctx>, @@ -750,8 +750,8 @@ fn call_ndarray_flatten_index_impl<'ctx>( /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an /// `NDArray`. /// * `indices` - The multidimensional index to compute the flattened index for. -pub fn call_ndarray_flatten_index<'ctx>( - generator: &dyn CodeGenerator, +pub fn call_ndarray_flatten_index<'ctx, G: CodeGenerator + ?Sized>( + generator: &G, ctx: &CodeGenContext<'ctx, '_>, ndarray: NDArrayValue<'ctx>, indices: ListValue<'ctx>, @@ -773,8 +773,8 @@ pub fn call_ndarray_flatten_index<'ctx>( /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an /// `NDArray`. /// * `indices` - The multidimensional index to compute the flattened index for. -pub fn call_ndarray_flatten_index_const<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn call_ndarray_flatten_index_const<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, ndarray: NDArrayValue<'ctx>, indices: ArrayValue<'ctx>, diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index b7f67daa6..c528eabca 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -416,10 +416,10 @@ pub struct CodeGenTask { /// This function is used to obtain the in-memory representation of `ty`, e.g. a `bool` variable /// would be represented by an `i8`. #[allow(clippy::too_many_arguments)] -fn get_llvm_type<'ctx>( +fn get_llvm_type<'ctx, G: CodeGenerator + ?Sized>( ctx: &'ctx Context, module: &Module<'ctx>, - generator: &mut dyn CodeGenerator, + generator: &mut G, unifier: &mut Unifier, top_level: &TopLevelContext, type_cache: &mut HashMap>, @@ -554,10 +554,10 @@ fn get_llvm_type<'ctx>( /// be byte-aligned for the variable to be addressable in memory, whereas there is no such /// restriction for ABI representations. #[allow(clippy::too_many_arguments)] -fn get_llvm_abi_type<'ctx>( +fn get_llvm_abi_type<'ctx, G: CodeGenerator + ?Sized>( ctx: &'ctx Context, module: &Module<'ctx>, - generator: &mut dyn CodeGenerator, + generator: &mut G, unifier: &mut Unifier, top_level: &TopLevelContext, type_cache: &mut HashMap>, diff --git a/nac3core/src/codegen/numpy.rs b/nac3core/src/codegen/numpy.rs index 9c670e0f2..ec06dada0 100644 --- a/nac3core/src/codegen/numpy.rs +++ b/nac3core/src/codegen/numpy.rs @@ -30,8 +30,8 @@ use crate::{ /// * `shape` - The shape of the `NDArray`. /// * `shape_len_fn` - A function that retrieves the number of dimensions from `shape`. /// * `shape_data_fn` - A function that retrieves the size of a dimension from `shape`. -fn create_ndarray_dyn_shape<'ctx, 'a, V, LenFn, DataFn>( - generator: &mut dyn CodeGenerator, +fn create_ndarray_dyn_shape<'ctx, 'a, G, V, LenFn, DataFn>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, 'a>, elem_ty: Type, shape: &V, @@ -39,8 +39,9 @@ fn create_ndarray_dyn_shape<'ctx, 'a, V, LenFn, DataFn>( shape_data_fn: DataFn, ) -> Result, String> where - LenFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, &V) -> Result, String>, - DataFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, &V, IntValue<'ctx>) -> Result, String>, + G: CodeGenerator + ?Sized, + LenFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, &V) -> Result, String>, + DataFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, &V, IntValue<'ctx>) -> Result, String>, { let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None); @@ -133,8 +134,8 @@ fn create_ndarray_dyn_shape<'ctx, 'a, V, LenFn, DataFn>( /// /// * `elem_ty` - The element type of the `NDArray`. /// * `shape` - The shape of the `NDArray`, represented as an LLVM [`ArrayValue`]. -fn create_ndarray_const_shape<'ctx>( - generator: &mut dyn CodeGenerator, +fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, shape: ArrayValue<'ctx> @@ -204,8 +205,8 @@ fn create_ndarray_const_shape<'ctx>( Ok(ndarray) } -fn ndarray_zero_value<'ctx>( - generator: &mut dyn CodeGenerator, +fn ndarray_zero_value<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, ) -> BasicValueEnum<'ctx> { @@ -224,8 +225,8 @@ fn ndarray_zero_value<'ctx>( } } -fn ndarray_one_value<'ctx>( - generator: &mut dyn CodeGenerator, +fn ndarray_one_value<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, ) -> BasicValueEnum<'ctx> { @@ -250,8 +251,8 @@ fn ndarray_one_value<'ctx>( /// /// * `elem_ty` - The element type of the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`. -fn call_ndarray_empty_impl<'ctx>( - generator: &mut dyn CodeGenerator, +fn call_ndarray_empty_impl<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, shape: ListValue<'ctx>, @@ -272,14 +273,15 @@ fn call_ndarray_empty_impl<'ctx>( /// Generates LLVM IR for populating the entire `NDArray` using a lambda with its flattened index as /// its input. -fn ndarray_fill_flattened<'ctx, 'a, ValueFn>( - generator: &mut dyn CodeGenerator, +fn ndarray_fill_flattened<'ctx, 'a, G, ValueFn>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, 'a>, ndarray: NDArrayValue<'ctx>, value_fn: ValueFn, ) -> Result<(), String> where - ValueFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result, String>, + G: CodeGenerator + ?Sized, + ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result, String>, { let llvm_usize = generator.get_size_type(ctx.ctx); @@ -311,14 +313,15 @@ fn ndarray_fill_flattened<'ctx, 'a, ValueFn>( /// Generates LLVM IR for populating the entire `NDArray` using a lambda with the dimension-indices /// as its input. -fn ndarray_fill_indexed<'ctx, ValueFn>( - generator: &mut dyn CodeGenerator, +fn ndarray_fill_indexed<'ctx, G, ValueFn>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, ndarray: NDArrayValue<'ctx>, value_fn: ValueFn, ) -> Result<(), String> where - ValueFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, '_>, PointerValue<'ctx>) -> Result, String>, + G: CodeGenerator + ?Sized, + ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, '_>, PointerValue<'ctx>) -> Result, String>, { ndarray_fill_flattened( generator, @@ -341,8 +344,8 @@ fn ndarray_fill_indexed<'ctx, ValueFn>( /// /// * `elem_ty` - The element type of the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`. -fn call_ndarray_zeros_impl<'ctx>( - generator: &mut dyn CodeGenerator, +fn call_ndarray_zeros_impl<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, shape: ListValue<'ctx>, @@ -377,8 +380,8 @@ fn call_ndarray_zeros_impl<'ctx>( /// /// * `elem_ty` - The element type of the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`. -fn call_ndarray_ones_impl<'ctx>( - generator: &mut dyn CodeGenerator, +fn call_ndarray_ones_impl<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, shape: ListValue<'ctx>, @@ -413,8 +416,8 @@ fn call_ndarray_ones_impl<'ctx>( /// /// * `elem_ty` - The element type of the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`. -fn call_ndarray_full_impl<'ctx>( - generator: &mut dyn CodeGenerator, +fn call_ndarray_full_impl<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, shape: ListValue<'ctx>, @@ -456,8 +459,8 @@ fn call_ndarray_full_impl<'ctx>( /// LLVM-typed implementation for generating the implementation for `ndarray.eye`. /// /// * `elem_ty` - The element type of the `NDArray`. -fn call_ndarray_eye_impl<'ctx>( - generator: &mut dyn CodeGenerator, +fn call_ndarray_eye_impl<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, nrows: IntValue<'ctx>, @@ -529,8 +532,8 @@ fn call_ndarray_eye_impl<'ctx>( /// LLVM-typed implementation for generating the implementation for `ndarray.copy`. /// /// * `elem_ty` - The element type of the `NDArray`. -fn ndarray_copy_impl<'ctx>( - generator: &mut dyn CodeGenerator, +fn ndarray_copy_impl<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, elem_ty: Type, this: NDArrayValue<'ctx>, diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 0983163d9..6ab20bed6 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -478,8 +478,8 @@ pub fn gen_for( /// executing. The result value must be an `i1` indicating if the loop should continue. /// * `body` - A lambda containing IR statements within the loop body. /// * `update` - A lambda containing IR statements updating loop variables. -pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>( - generator: &mut dyn CodeGenerator, +pub fn gen_for_callback<'ctx, 'a, G, I, InitFn, CondFn, BodyFn, UpdateFn>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, 'a>, init: InitFn, cond: CondFn, @@ -487,11 +487,12 @@ pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>( update: UpdateFn, ) -> Result<(), String> where + G: CodeGenerator + ?Sized, I: Clone, - InitFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>) -> Result, - CondFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result, String>, - BodyFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>, - UpdateFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>, + InitFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>) -> Result, + CondFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result, String>, + BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>, + UpdateFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>, { let current = ctx.builder.get_insert_block().and_then(BasicBlock::get_parent).unwrap(); let init_bb = ctx.ctx.append_basic_block(current, "for.init"); @@ -551,8 +552,8 @@ pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>( /// value should be treated as inclusive (as opposed to exclusive). /// * `body` - A lambda containing IR statements within the loop body. /// * `incr_val` - The value to increment the loop variable on each iteration. -pub fn gen_for_callback_incrementing<'ctx, 'a, BodyFn>( - generator: &mut dyn CodeGenerator, +pub fn gen_for_callback_incrementing<'ctx, 'a, G, BodyFn>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, 'a>, init_val: IntValue<'ctx>, max_val: (IntValue<'ctx>, bool), @@ -560,7 +561,8 @@ pub fn gen_for_callback_incrementing<'ctx, 'a, BodyFn>( incr_val: IntValue<'ctx>, ) -> Result<(), String> where - BodyFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<(), String>, + G: CodeGenerator + ?Sized, + BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<(), String>, { let init_val_t = init_val.get_type(); @@ -779,8 +781,8 @@ pub fn final_proxy<'ctx>( /// Inserts the declaration of the builtin function with the specified `symbol` name, and returns /// the function. -pub fn get_builtins<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn get_builtins<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, symbol: &str, ) -> FunctionValue<'ctx> { @@ -873,8 +875,8 @@ pub fn exn_constructor<'ctx>( /// /// * `exception` - The exception thrown by the `raise` statement. /// * `loc` - The location where the exception is raised from. -pub fn gen_raise<'ctx>( - generator: &mut dyn CodeGenerator, +pub fn gen_raise<'ctx, G: CodeGenerator + ?Sized>( + generator: &mut G, ctx: &mut CodeGenContext<'ctx, '_>, exception: Option<&BasicValueEnum<'ctx>>, loc: Location,