improv cast

This commit is contained in:
Edgar 2024-03-12 13:03:35 +01:00
parent a70924b81b
commit 9ca2e336eb
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
5 changed files with 31 additions and 8 deletions

View file

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

View file

@ -1033,7 +1033,7 @@ fn compile_rvalue<'ctx>(
); );
compile_unary_op(ctx, fn_id, locals, *op, value)? 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.set_debug_loc(
ctx.builder ctx.builder
.get_current_debug_location() .get_current_debug_location()
@ -1044,7 +1044,7 @@ fn compile_rvalue<'ctx>(
let target_ty = target_ty.clone(); let target_ty = target_ty.clone();
let target_llvm_ty = compile_basic_type(ctx, &target_ty); 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); let current_ty = compile_basic_type(ctx, &ty);
if target_llvm_ty.is_pointer_type() { if target_llvm_ty.is_pointer_type() {

View file

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

View file

@ -735,14 +735,37 @@ fn lower_expr(
(RValue::Use(Operand::Move(place), info.span), ty, info.span) (RValue::Use(Operand::Move(place), info.span), ty, info.span)
} }
ast::Expression::Cast(path, cast_ty, span) => { ast::Expression::Cast(exp, cast_ty, span) => {
let (place, _ty, _path_span) = lower_path(builder, path)?; let (value, ty, _exp_span) = lower_expr(builder, exp, None)?;
let new_ty = lower_type(&builder.ctx, cast_ty, builder.local_module)?; let new_ty = lower_type(&builder.ctx, cast_ty, builder.local_module)?;
let kind = new_ty.kind.clone(); let kind = new_ty.kind.clone();
// todo: some checks? // 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) Box::new(rhs)
), ),
#[precedence(level="5")] #[assoc(side="left")] #[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(<>), "(" <StructInitExpr> ")" => ast::Expression::StructInit(<>),
} }