diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index e6a766b0..bdadb621 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -599,11 +599,11 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { *self.exception_val.insert(exn) }); - exn.gep(self, |f| f.id).store(self, exn_id); - exn.gep(self, |f| f.msg).store(self, msg); + exn.set(self, |f| f.id, exn_id); + exn.set(self, |f| f.msg, msg); for (i, param) in params.iter().enumerate() { if let Some(param) = param { - exn.gep(self, |f| f.params[i]).store(self, *param); + exn.set(self, |f| f.params[i], *param); } } diff --git a/nac3core/src/codegen/model/structure.rs b/nac3core/src/codegen/model/structure.rs index 88b297af..63e1040f 100644 --- a/nac3core/src/codegen/model/structure.rs +++ b/nac3core/src/codegen/model/structure.rs @@ -166,4 +166,32 @@ impl<'ctx, S: StructKind<'ctx>> Ptr<'ctx, StructModel> { let ptr_model = PtrModel(field.model); ptr_model.believe_value(ptr) } + + /// Convenience function equivalent to `.gep(...).load(...)`. + pub fn get( + &self, + tyctx: TypeContext<'ctx>, + ctx: &CodeGenContext<'ctx, '_>, + get_field: GetField, + name: &str, + ) -> Instance<'ctx, M> + where + M: Model<'ctx>, + GetField: FnOnce(S::Fields) -> GepField, + { + self.gep(ctx, get_field).load(tyctx, ctx, name) + } + + /// Convenience function equivalent to `.gep(...).store(...)`. + pub fn set( + &self, + ctx: &CodeGenContext<'ctx, '_>, + get_field: GetField, + value: Instance<'ctx, M>, + ) where + M: Model<'ctx>, + GetField: FnOnce(S::Fields) -> GepField, + { + self.gep(ctx, get_field).store(ctx, value) + } } diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 85049730..321178a0 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -1274,17 +1274,17 @@ pub fn gen_raise<'ctx, G: CodeGenerator + ?Sized>( let filename = loc.file.0; let filename = ctx.gen_string(generator, &String::from(filename)).value; let filename = cslice_model.check_value(type_context, ctx.ctx, filename).unwrap(); - pexn.gep(ctx, |f| f.filename).store(ctx, filename); + pexn.set(ctx, |f| f.filename, filename); let row = i32_model.constant(type_context, ctx.ctx, loc.row as u64); - pexn.gep(ctx, |f| f.line).store(ctx, row); + pexn.set(ctx, |f| f.line, row); let column = i32_model.constant(type_context, ctx.ctx, loc.column as u64); - pexn.gep(ctx, |f| f.column).store(ctx, column); + pexn.set(ctx, |f| f.column, column); let current_fn = ctx.builder.get_insert_block().unwrap().get_parent().unwrap(); let fn_name = ctx.gen_string(generator, current_fn.get_name().to_str().unwrap()); - pexn.gep(ctx, |f| f.function).store(ctx, fn_name); + pexn.set(ctx, |f| f.function, fn_name); let raise = get_builtins(generator, ctx, "__nac3_raise"); ctx.build_call_or_invoke(raise, &[pexn.value.into()], "raise"); diff --git a/nac3core/src/codegen/structure/ndarray.rs b/nac3core/src/codegen/structure/ndarray.rs index a0c44561..c870bb45 100644 --- a/nac3core/src/codegen/structure/ndarray.rs +++ b/nac3core/src/codegen/structure/ndarray.rs @@ -95,11 +95,11 @@ impl<'ctx> NDArrayObject<'ctx> { let shape = sizet_model.array_alloca(tyctx, ctx, ndims_val.value, "shape"); let strides = sizet_model.array_alloca(tyctx, ctx, ndims_val.value, "strides"); - pndarray.gep(ctx, |f| f.data).store(ctx, data); - pndarray.gep(ctx, |f| f.itemsize).store(ctx, itemsize); - pndarray.gep(ctx, |f| f.ndims).store(ctx, ndims_val); - pndarray.gep(ctx, |f| f.shape).store(ctx, shape); - pndarray.gep(ctx, |f| f.strides).store(ctx, strides); + pndarray.set(ctx, |f| f.data, data); + pndarray.set(ctx, |f| f.itemsize, itemsize); + pndarray.set(ctx, |f| f.ndims, ndims_val); + pndarray.set(ctx, |f| f.shape, shape); + pndarray.set(ctx, |f| f.strides, strides); NDArrayObject { dtype, ndims, value: pndarray } } @@ -136,7 +136,7 @@ impl<'ctx> NDArrayObject<'ctx> { let byte_model = IntModel(Byte); let data = byte_model.array_alloca(tyctx, ctx, self.get_ndims(tyctx, ctx).value, "data"); - self.value.gep(ctx, |f| f.data).store(ctx, data); + self.value.set(ctx, |f| f.data, data); self.update_strides_by_shape(tyctx, ctx); }