core/codegen: gen_assign to take in value_ty

This commit is contained in:
lyken 2024-07-30 16:32:38 +08:00
parent 3d8240259c
commit 6369db94ab
3 changed files with 12 additions and 7 deletions

View File

@ -1128,7 +1128,7 @@ pub fn gen_comprehension<'ctx, G: CodeGenerator>(
)
.into_pointer_value();
let val = ctx.build_gep_and_load(arr_ptr, &[tmp], Some("val"));
generator.gen_assign(ctx, target, val.into())?;
generator.gen_assign(ctx, target, val.into(), ctx.primitives.int32)?;
}
// Emits the content of `cont_bb`

View File

@ -123,11 +123,12 @@ pub trait CodeGenerator {
ctx: &mut CodeGenContext<'ctx, '_>,
target: &Expr<Option<Type>>,
value: ValueEnum<'ctx>,
value_ty: Type,
) -> Result<(), String>
where
Self: Sized,
{
gen_assign(self, ctx, target, value)
gen_assign(self, ctx, target, value, value_ty)
}
/// Generate code for a while expression.

View File

@ -209,6 +209,7 @@ pub fn gen_assign<'ctx, G: CodeGenerator>(
ctx: &mut CodeGenContext<'ctx, '_>,
target: &Expr<Option<Type>>,
value: ValueEnum<'ctx>,
value_ty: Type,
) -> Result<(), String> {
let llvm_usize = generator.get_size_type(ctx.ctx);
@ -225,7 +226,7 @@ pub fn gen_assign<'ctx, G: CodeGenerator>(
.builder
.build_extract_value(v, u32::try_from(i).unwrap(), "struct_elem")
.unwrap();
generator.gen_assign(ctx, elt, v.into())?;
generator.gen_assign(ctx, elt, v.into(), value_ty)?;
}
}
ExprKind::Subscript { value: ls, slice, .. }
@ -434,7 +435,7 @@ pub fn gen_for<G: CodeGenerator>(
.map(BasicValueEnum::into_int_value)
.unwrap();
let val = ctx.build_gep_and_load(arr_ptr, &[index], Some("val"));
generator.gen_assign(ctx, target, val.into())?;
generator.gen_assign(ctx, target, val.into(), ctx.primitives.int32)?;
generator.gen_block(ctx, body.iter())?;
}
@ -1585,14 +1586,16 @@ pub fn gen_stmt<G: CodeGenerator>(
}
StmtKind::AnnAssign { target, value, .. } => {
if let Some(value) = value {
let value_ty = value.custom.unwrap();
let Some(value) = generator.gen_expr(ctx, value)? else { return Ok(()) };
generator.gen_assign(ctx, target, value)?;
generator.gen_assign(ctx, target, value, value_ty)?;
}
}
StmtKind::Assign { targets, value, .. } => {
let value_ty = value.custom.unwrap();
let Some(value) = generator.gen_expr(ctx, value)? else { return Ok(()) };
for target in targets {
generator.gen_assign(ctx, target, value.clone())?;
generator.gen_assign(ctx, target, value.clone(), value_ty)?;
}
}
StmtKind::Continue { .. } => {
@ -1606,6 +1609,7 @@ pub fn gen_stmt<G: CodeGenerator>(
StmtKind::For { .. } => generator.gen_for(ctx, stmt)?,
StmtKind::With { .. } => generator.gen_with(ctx, stmt)?,
StmtKind::AugAssign { target, op, value, .. } => {
let value_ty = value.custom.unwrap();
let value = gen_binop_expr(
generator,
ctx,
@ -1614,7 +1618,7 @@ pub fn gen_stmt<G: CodeGenerator>(
value,
stmt.location,
)?;
generator.gen_assign(ctx, target, value.unwrap())?;
generator.gen_assign(ctx, target, value.unwrap(), value_ty)?;
}
StmtKind::Try { .. } => gen_try(generator, ctx, stmt)?,
StmtKind::Raise { exc, .. } => {