Compare commits

...

2 commits

Author SHA1 Message Date
Edgar 9ca2e336eb
improv cast 2024-03-12 13:03:35 +01:00
Edgar a70924b81b
fix 2024-03-12 12:08:02 +01:00
5 changed files with 36 additions and 13 deletions

View file

@ -176,7 +176,7 @@ pub enum Expression {
Binary(Box<Self>, BinaryOp, Box<Self>),
Deref(Box<Self>, Span),
AsRef(Box<Self>, bool, Span),
Cast(PathExpr, Type, Span),
Cast(Box<Self>, Type, Span),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

View file

@ -992,15 +992,15 @@ fn compile_rvalue<'ctx>(
for proj in &place.projection {
match proj {
ir::PlaceElem::Deref => {
ptr = ctx
.builder
.build_load(compile_basic_type(ctx, &local_ty), ptr, "deref")?
.into_pointer_value();
local_ty = match local_ty.kind {
ir::TypeKind::Ptr(_, inner) => *inner,
ir::TypeKind::Ref(_, inner) => *inner,
_ => unreachable!(),
}
};
ptr = ctx
.builder
.build_load(compile_basic_type(ctx, &local_ty), ptr, "deref")?
.into_pointer_value();
}
ir::PlaceElem::Field { .. } => todo!(),
ir::PlaceElem::Index { .. } => todo!(),
@ -1033,7 +1033,7 @@ fn compile_rvalue<'ctx>(
);
compile_unary_op(ctx, fn_id, locals, *op, value)?
}
ir::RValue::Cast(place, target_ty, span) => {
ir::RValue::Cast(op, target_ty, span) => {
ctx.set_debug_loc(
ctx.builder
.get_current_debug_location()
@ -1044,7 +1044,7 @@ fn compile_rvalue<'ctx>(
let target_ty = target_ty.clone();
let target_llvm_ty = compile_basic_type(ctx, &target_ty);
let (value, ty) = compile_load_place(ctx, fn_id, locals, place, false)?;
let (value, ty) = compile_load_operand(ctx, fn_id, locals, op)?;
let current_ty = compile_basic_type(ctx, &ty);
if target_llvm_ty.is_pointer_type() {

View file

@ -419,7 +419,7 @@ pub enum RValue {
BinOp(BinOp, Operand, Operand, Span),
LogicOp(LogicalOp, Operand, Operand, Span),
UnOp(UnOp, Operand, Span),
Cast(Place, TypeInfo, Span),
Cast(Operand, TypeInfo, Span),
}
#[derive(Debug, Clone)]

View file

@ -735,14 +735,37 @@ fn lower_expr(
(RValue::Use(Operand::Move(place), info.span), ty, info.span)
}
ast::Expression::Cast(path, cast_ty, span) => {
let (place, _ty, _path_span) = lower_path(builder, path)?;
ast::Expression::Cast(exp, cast_ty, span) => {
let (value, ty, _exp_span) = lower_expr(builder, exp, None)?;
let new_ty = lower_type(&builder.ctx, cast_ty, builder.local_module)?;
let kind = new_ty.kind.clone();
// todo: some checks?
(RValue::Cast(place, new_ty, *span), kind, *span)
// check if its a use directly, to avoid a temporary.
let rvalue = match value {
RValue::Use(op, _) => RValue::Cast(op, new_ty.clone(), *span),
value => {
let inner_local = builder.add_local(Local::temp(ty.clone()));
let inner_place = Place {
local: inner_local,
projection: Default::default(),
};
builder.statements.push(Statement {
span: None,
kind: StatementKind::StorageLive(inner_local),
});
builder.statements.push(Statement {
span: None,
kind: StatementKind::Assign(inner_place.clone(), value),
});
RValue::Cast(Operand::Move(inner_place), new_ty.clone(), *span)
}
};
(rvalue, kind, *span)
}
})
}

View file

@ -343,7 +343,7 @@ pub(crate) Expression: ast::Expression = {
Box::new(rhs)
),
#[precedence(level="5")] #[assoc(side="left")]
<lo:@L> <a:PathExpr> "as" <b: Type> <hi:@R> => ast::Expression::Cast(a, b, ast::Span::new(lo, hi)),
<lo:@L> <a:Expression> "as" <b: Type> <hi:@R> => ast::Expression::Cast(Box::new(a), b, ast::Span::new(lo, hi)),
"(" <StructInitExpr> ")" => ast::Expression::StructInit(<>),
}