leo_passes/const_propagation/
const_propagator.rsuse crate::{SymbolTable, TypeTable};
use leo_ast::{Expression, IntegerType, Literal, NodeBuilder, NodeID, TupleExpression};
use leo_errors::{StaticAnalyzerError, emitter::Handler};
use leo_interpreter::Value;
use leo_span::{Span, Symbol};
pub struct ConstPropagator<'a> {
pub(crate) symbol_table: &'a mut SymbolTable,
pub(crate) type_table: &'a TypeTable,
pub(crate) node_builder: &'a NodeBuilder,
pub(crate) handler: &'a Handler,
pub(crate) program: Symbol,
pub(crate) changed: bool,
pub(crate) const_not_evaluated: Option<Span>,
pub(crate) array_index_not_evaluated: Option<Span>,
}
impl<'a> ConstPropagator<'a> {
pub(crate) fn new(
handler: &'a Handler,
symbol_table: &'a mut SymbolTable,
type_table: &'a TypeTable,
node_builder: &'a NodeBuilder,
) -> Self {
Self {
handler,
symbol_table,
type_table,
node_builder,
program: Symbol::intern(""),
changed: false,
const_not_evaluated: None,
array_index_not_evaluated: None,
}
}
pub(crate) fn in_scope<T>(&mut self, id: NodeID, func: impl FnOnce(&mut Self) -> T) -> T {
self.symbol_table.enter_scope(Some(id));
let result = func(self);
self.symbol_table.enter_parent();
result
}
pub(crate) fn emit_err(&self, err: StaticAnalyzerError) {
self.handler.emit_err(err);
}
}
pub(crate) fn value_to_expression(value: &Value, span: Span, node_builder: &NodeBuilder) -> Expression {
use Value::*;
let id = node_builder.next_id();
match value {
Unit => Expression::Unit(leo_ast::UnitExpression { span, id }),
Bool(x) => Expression::Literal(Literal::Boolean(*x, span, id)),
U8(x) => Expression::Literal(Literal::Integer(IntegerType::U8, format!("{x}"), span, id)),
U16(x) => Expression::Literal(Literal::Integer(IntegerType::U16, format!("{x}"), span, id)),
U32(x) => Expression::Literal(Literal::Integer(IntegerType::U32, format!("{x}"), span, id)),
U64(x) => Expression::Literal(Literal::Integer(IntegerType::U64, format!("{x}"), span, id)),
U128(x) => Expression::Literal(Literal::Integer(IntegerType::U128, format!("{x}"), span, id)),
I8(x) => Expression::Literal(Literal::Integer(IntegerType::I8, format!("{x}"), span, id)),
I16(x) => Expression::Literal(Literal::Integer(IntegerType::I16, format!("{x}"), span, id)),
I32(x) => Expression::Literal(Literal::Integer(IntegerType::I32, format!("{x}"), span, id)),
I64(x) => Expression::Literal(Literal::Integer(IntegerType::I64, format!("{x}"), span, id)),
I128(x) => Expression::Literal(Literal::Integer(IntegerType::I128, format!("{x}"), span, id)),
Group(x) => {
let mut s = format!("{x}");
s.truncate(s.len() - 5);
Expression::Literal(Literal::Group(s, span, id))
}
Field(x) => {
let mut s = format!("{x}");
s.truncate(s.len() - 5);
Expression::Literal(Literal::Field(s, span, id))
}
Scalar(x) => {
let mut s = format!("{x}");
s.truncate(s.len() - 6);
Expression::Literal(Literal::Scalar(s, span, id))
}
Tuple(x) => {
let elements = x.iter().map(|val| value_to_expression(val, span, node_builder)).collect();
Expression::Tuple(TupleExpression { elements, span, id })
}
_ => panic!("Can only evaluate literals and tuples."),
}
}