From a0614bad836fcd1d0d3a42743ceb8f1637fc987f Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 12 Aug 2024 20:17:41 +0800 Subject: [PATCH] [core] codegen/expr: Make gen_string return `StructValue` So that it is clear that the value itself is returned rather than a pointer to the struct or its data. --- nac3core/src/codegen/expr.rs | 14 ++++++++------ nac3core/src/codegen/numpy.rs | 4 ++-- nac3core/src/codegen/stmt.rs | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index b63da080..0817fce2 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -32,7 +32,7 @@ use crate::{ use inkwell::{ attributes::{Attribute, AttributeLoc}, types::{AnyType, BasicType, BasicTypeEnum}, - values::{BasicValueEnum, CallSiteValue, FunctionValue, IntValue, PointerValue}, + values::{BasicValueEnum, CallSiteValue, FunctionValue, IntValue, PointerValue, StructValue}, AddressSpace, IntPredicate, OptimizationLevel, }; use itertools::{chain, izip, Either, Itertools}; @@ -322,7 +322,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { self.raise_exn( generator, "0:NotImplementedError", - msg, + msg.into(), [None, None, None], self.current_loc, ); @@ -582,12 +582,14 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { } /// Helper function for generating a LLVM variable storing a [String]. - pub fn gen_string(&mut self, generator: &mut G, s: S) -> BasicValueEnum<'ctx> + pub fn gen_string(&mut self, generator: &mut G, s: S) -> StructValue<'ctx> where G: CodeGenerator + ?Sized, S: Into, { - self.gen_const(generator, &Constant::Str(s.into()), self.primitives.str).unwrap() + self.gen_const(generator, &Constant::Str(s.into()), self.primitives.str) + .map(BasicValueEnum::into_struct_value) + .unwrap() } pub fn raise_exn( @@ -646,7 +648,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { loc: Location, ) { let err_msg = self.gen_string(generator, err_msg); - self.make_assert_impl(generator, cond, err_name, err_msg, params, loc); + self.make_assert_impl(generator, cond, err_name, err_msg.into(), params, loc); } pub fn make_assert_impl( @@ -3067,7 +3069,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>( ctx.raise_exn( generator, "0:UnwrapNoneError", - err_msg, + err_msg.into(), [None, None, None], ctx.current_loc, ); diff --git a/nac3core/src/codegen/numpy.rs b/nac3core/src/codegen/numpy.rs index 6ebffe80..ff8c55eb 100644 --- a/nac3core/src/codegen/numpy.rs +++ b/nac3core/src/codegen/numpy.rs @@ -257,7 +257,7 @@ fn ndarray_zero_value<'ctx, G: CodeGenerator + ?Sized>( } else if ctx.unifier.unioned(elem_ty, ctx.primitives.bool) { ctx.ctx.bool_type().const_zero().into() } else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) { - ctx.gen_string(generator, "") + ctx.gen_string(generator, "").into() } else { unreachable!() } @@ -285,7 +285,7 @@ fn ndarray_one_value<'ctx, G: CodeGenerator + ?Sized>( } else if ctx.unifier.unioned(elem_ty, ctx.primitives.bool) { ctx.ctx.bool_type().const_int(1, false).into() } else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) { - ctx.gen_string(generator, "1") + ctx.gen_string(generator, "1").into() } else { unreachable!() } diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index ea5869d6..4b2cf4e2 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -1780,7 +1780,7 @@ pub fn gen_stmt( return Ok(()); } } - None => ctx.gen_string(generator, ""), + None => ctx.gen_string(generator, "").into(), }; ctx.make_assert_impl( generator,