use leo_ast::{Expression::Literal, *};
use leo_errors::LoopUnrollerError;
use super::UnrollingVisitor;
impl AstReconstructor for UnrollingVisitor<'_> {
type AdditionalOutput = ();
fn reconstruct_repeat(&mut self, input: RepeatExpression) -> (Expression, Self::AdditionalOutput) {
let new_id = self.state.node_builder.next_id();
let new_count = self.reconstruct_expression(input.count).0;
let el_ty = self.state.type_table.get(&input.expr.id()).expect("guaranteed by type checking");
self.state.type_table.insert(new_id, Type::Array(ArrayType::new(el_ty, new_count.clone())));
(
RepeatExpression { expr: self.reconstruct_expression(input.expr).0, count: new_count, id: new_id, ..input }
.into(),
Default::default(),
)
}
fn reconstruct_block(&mut self, mut input: Block) -> (Block, Self::AdditionalOutput) {
self.in_scope(input.id(), |slf| {
input.statements = input.statements.into_iter().map(|stmt| slf.reconstruct_statement(stmt).0).collect();
(input, Default::default())
})
}
fn reconstruct_const(&mut self, input: ConstDeclaration) -> (Statement, Self::AdditionalOutput) {
(input.into(), Default::default())
}
fn reconstruct_definition(&mut self, input: DefinitionStatement) -> (Statement, Self::AdditionalOutput) {
(
DefinitionStatement {
type_: input.type_.map(|ty| self.reconstruct_type(ty).0),
value: self.reconstruct_expression(input.value).0,
..input
}
.into(),
Default::default(),
)
}
fn reconstruct_iteration(&mut self, input: IterationStatement) -> (Statement, Self::AdditionalOutput) {
let Literal(start_lit_ref) = &input.start else {
self.loop_not_unrolled = Some(input.start.span());
return (Statement::Iteration(Box::new(input)), Default::default());
};
let Literal(stop_lit_ref) = &input.stop else {
self.loop_not_unrolled = Some(input.stop.span());
return (Statement::Iteration(Box::new(input)), Default::default());
};
let resolve_unsuffixed = |lit: &leo_ast::Literal, expr_id| {
let mut resolved = lit.clone();
if let LiteralVariant::Unsuffixed(s) = &resolved.variant {
if let Some(Type::Integer(integer_type)) = self.state.type_table.get(&expr_id) {
resolved.variant = LiteralVariant::Integer(integer_type, s.clone());
}
}
resolved
};
let resolved_start_lit = resolve_unsuffixed(start_lit_ref, input.start.id());
let resolved_stop_lit = resolve_unsuffixed(stop_lit_ref, input.stop.id());
let start_value = Value::try_from(&resolved_start_lit).unwrap();
let stop_value = Value::try_from(&resolved_stop_lit).unwrap();
let bounds_invalid = match (&start_value, &stop_value) {
(Value::I8(a, _), Value::I8(b, _)) => a >= b,
(Value::I16(a, _), Value::I16(b, _)) => a >= b,
(Value::I32(a, _), Value::I32(b, _)) => a >= b,
(Value::I64(a, _), Value::I64(b, _)) => a >= b,
(Value::I128(a, _), Value::I128(b, _)) => a >= b,
(Value::U8(a, _), Value::U8(b, _)) => a >= b,
(Value::U16(a, _), Value::U16(b, _)) => a >= b,
(Value::U32(a, _), Value::U32(b, _)) => a >= b,
(Value::U64(a, _), Value::U64(b, _)) => a >= b,
(Value::U128(a, _), Value::U128(b, _)) => a >= b,
_ => panic!("Type checking guarantees that loop bounds are integers of the same type."),
};
if bounds_invalid {
self.emit_err(LoopUnrollerError::loop_range_decreasing(input.stop.span()));
}
self.loop_unrolled = true;
(self.unroll_iteration_statement::<i128>(input, start_value, stop_value), Default::default())
}
}