use crate::ast::{self, ExprKind};
use crate::codegen::backend::{CodegenError, CodegenResult};
use crate::codegen::builder::values;
use crate::codegen::ir::*;
use crate::lexer::Span;
use super::MirLowerer;
fn peel(expr: &ast::Expr) -> &ast::Expr {
match &expr.kind {
ExprKind::Paren(inner) => peel(inner),
_ => expr,
}
}
pub(super) fn is_named(expr: &ast::Expr) -> bool {
matches!(peel(expr).kind, ExprKind::Ident(_) | ExprKind::Path(_))
}
fn is_static_path(expr: &ast::Expr) -> bool {
match &expr.kind {
ExprKind::Ident(_) => true,
ExprKind::Paren(e) | ExprKind::Deref(e) => is_static_path(e),
ExprKind::Field { expr: e, .. } | ExprKind::TupleField { expr: e, .. } => is_static_path(e),
_ => false,
}
}
fn location(span: Span) -> Option<(u32, u32)> {
(span.end.0 > span.start.0).then_some((span.start.0, span.end.0))
}
fn element_mismatch(what: &str, have: &MirType, elem: &MirType, span: Span) -> CodegenError {
CodegenError::Rejected {
message: format!("this {what} holds `{have}` but the parameter is a slice of `{elem}`"),
location: location(span),
help: Some(format!("give the {what} the element type `{elem}`")),
}
}
impl<'ctx> MirLowerer<'ctx> {
pub(super) fn is_projected_array(&mut self, expr: &ast::Expr) -> CodegenResult<bool> {
if is_named(expr) {
return Ok(self.is_global_array_name(expr));
}
if matches!(self.infer_expr_type(expr), MirType::Array(..)) {
return Ok(true);
}
if !is_static_path(expr) {
return Ok(false);
}
Ok(matches!(
self.lower_place(expr)?,
Some((_, MirType::Array(..)))
))
}
pub(super) fn store_into_projected_array(
&mut self,
op: ast::AssignOp,
target: &ast::Expr,
arr: &ast::Expr,
val: &MirValue,
) -> CodegenResult<Option<MirValue>> {
if !self.is_projected_array(arr)? {
return Ok(None);
}
let Some((place, place_ty)) = self.lower_place(target)? else {
return Err(Self::unaddressable_array(arr.span));
};
let store = self.compound_store_value(op, target, val.clone(), &place_ty)?;
self.store_through_place(place, place_ty, MirRValue::Use(store))?;
Ok(Some(values::unit()))
}
pub(super) fn unaddressable_array(span: Span) -> CodegenError {
CodegenError::Rejected {
message: "cannot store into an element of this array: it is a temporary copy, \
so the store would be lost"
.to_string(),
location: location(span),
help: Some(
"bind the array to a `let mut` local, store into the local, then write it back"
.to_string(),
),
}
}
pub(super) fn unaddressable_field(span: Span) -> CodegenError {
CodegenError::Rejected {
message: "cannot assign to a field of this value: it is a temporary copy, \
so the assignment would be lost"
.to_string(),
location: location(span),
help: Some(
"copy the value to a `let mut` local, assign to the local, then store it back"
.to_string(),
),
}
}
pub(super) fn slice_from_parts(
&mut self,
ptr: MirValue,
len: MirValue,
elem: &MirType,
target: &MirType,
) -> CodegenResult<MirValue> {
let builder = self
.current_fn
.as_mut()
.ok_or_else(|| CodegenError::Internal("No current function".to_string()))?;
let fat = builder.create_local(target.clone());
builder.aggregate(
fat,
AggregateKind::Struct(MirType::slice_type_name(elem)),
vec![ptr, len],
);
let result = builder.create_local(MirType::Ptr(Box::new(target.clone())));
builder.assign(
result,
MirRValue::AddressOf {
is_mut: false,
place: MirPlace::local(fat),
},
);
Ok(values::local(result))
}
pub(super) fn projected_array_ref_as_slice(
&mut self,
inner: &ast::Expr,
elem: &MirType,
target: &MirType,
) -> CodegenResult<Option<MirValue>> {
if !self.is_projected_array(inner)? {
return Ok(None);
}
let (place, have, n) = match self.lower_place(inner)? {
Some((place, MirType::Array(have, n))) => (place, have, n),
_ => self.array_temporary(inner)?,
};
if *have != *elem {
return Err(element_mismatch("array", &have, elem, inner.span));
}
let builder = self
.current_fn
.as_mut()
.ok_or_else(|| CodegenError::Internal("No current function".to_string()))?;
let ptr = builder.create_local(MirType::Ptr(Box::new(MirType::Array(have, n))));
builder.assign(
ptr,
MirRValue::AddressOf {
is_mut: true,
place,
},
);
let len = MirValue::Const(MirConst::Int(n as i128, MirType::usize()));
self.slice_from_parts(values::local(ptr), len, elem, target)
.map(Some)
}
fn array_temporary(
&mut self,
expr: &ast::Expr,
) -> CodegenResult<(MirPlace, Box<MirType>, u64)> {
let value = self.lower_expr(expr)?;
match (&value, self.type_of_value(&value)) {
(MirValue::Local(id), MirType::Array(have, n)) => Ok((MirPlace::local(*id), have, n)),
_ => Err(CodegenError::Rejected {
message: "cannot borrow this expression as a slice: it is not a fixed array"
.to_string(),
location: location(expr.span),
help: Some("bind the array to a local and borrow the local".to_string()),
}),
}
}
fn referent_collection(&self, expr: &ast::Expr) -> Option<MirType> {
match self.infer_expr_type(expr) {
MirType::Ptr(inner) if matches!(*inner, MirType::Vec(_) | MirType::Array(..)) => {
Some(*inner)
}
_ => None,
}
}
pub(super) fn is_collection_ref(&self, expr: &ast::Expr) -> bool {
self.referent_collection(expr).is_some()
}
pub(super) fn forwarded_ref_as_slice(
&mut self,
arg: &ast::Expr,
elem: &MirType,
target: &MirType,
) -> CodegenResult<Option<MirValue>> {
let Some(referent) = self.referent_collection(arg) else {
return Ok(None);
};
let ptr = self.lower_expr(arg)?;
match referent {
MirType::Vec(have) => {
let builder = self
.current_fn
.as_mut()
.ok_or_else(|| CodegenError::Internal("No current function".to_string()))?;
let handle = builder.create_local(MirType::Vec(have.clone()));
builder.assign(
handle,
MirRValue::Deref {
ptr,
pointee_ty: MirType::Vec(have.clone()),
},
);
let target = Box::new(target.clone());
self.vec_ref_as_slice(values::local(handle), &have, elem, target, arg.span)
.map(Some)
}
MirType::Array(have, n) => {
if *have != *elem {
return Err(element_mismatch("array", &have, elem, arg.span));
}
let len = MirValue::Const(MirConst::Int(n as i128, MirType::usize()));
self.slice_from_parts(ptr, len, elem, target).map(Some)
}
_ => Ok(None),
}
}
}