Update DCE

This commit is contained in:
Pranav Gaddamadugu 2023-10-13 16:09:33 -04:00 committed by Pranav Gaddamadugu
parent 044933c0fb
commit c80aee091a

View File

@ -34,58 +34,30 @@ use leo_span::sym;
impl ExpressionReconstructor for DeadCodeEliminator<'_> {
type AdditionalOutput = ();
/// Reconstructs the components of an access expression.
fn reconstruct_access(&mut self, input: AccessExpression) -> (Expression, Self::AdditionalOutput) {
(
Expression::Access(match input {
AccessExpression::Array(array) => AccessExpression::Array(ArrayAccess {
array: Box::new(self.reconstruct_expression(*array.array).0),
index: Box::new(self.reconstruct_expression(*array.index).0),
span: array.span,
id: array.id,
}),
AccessExpression::AssociatedFunction(function) => {
// If the associated function manipulates a mapping, mark the statement as necessary.
match (&function.ty, function.name.name) {
(Type::Identifier(Identifier { name: sym::Mapping, .. }), sym::get)
| (Type::Identifier(Identifier { name: sym::Mapping, .. }), sym::get_or_use)
| (Type::Identifier(Identifier { name: sym::Mapping, .. }), sym::set) => {
self.is_necessary = true;
}
_ => {}
};
// Reconstruct the access expression.
let result = AccessExpression::AssociatedFunction(AssociatedFunction {
ty: function.ty,
name: function.name,
arguments: function
.arguments
.into_iter()
.map(|arg| self.reconstruct_expression(arg).0)
.collect(),
span: function.span,
id: function.id,
});
// Unset `self.is_necessary`.
self.is_necessary = false;
result
}
AccessExpression::Member(member) => AccessExpression::Member(MemberAccess {
inner: Box::new(self.reconstruct_expression(*member.inner).0),
name: member.name,
span: member.span,
id: member.id,
}),
AccessExpression::Tuple(tuple) => AccessExpression::Tuple(TupleAccess {
tuple: Box::new(self.reconstruct_expression(*tuple.tuple).0),
index: tuple.index,
span: tuple.span,
id: tuple.id,
}),
AccessExpression::AssociatedConstant(constant) => AccessExpression::AssociatedConstant(constant),
}),
/// Reconstructs the associated function access expression.
fn reconstruct_associated_function(&mut self, input: AssociatedFunction) -> (Expression, Self::AdditionalOutput) {
// If the associated function manipulates a mapping, mark the statement as necessary.
match (&input.ty, input.name.name) {
(Type::Identifier(Identifier { name: sym::Mapping, .. }), sym::remove)
| (Type::Identifier(Identifier { name: sym::Mapping, .. }), sym::set) => {
self.is_necessary = true;
}
_ => {}
};
// Reconstruct the access expression.
let result = (
Expression::Access(AccessExpression::AssociatedFunction(AssociatedFunction {
ty: input.ty,
name: input.name,
arguments: input.arguments.into_iter().map(|arg| self.reconstruct_expression(arg).0).collect(),
span: input.span,
id: input.id,
})),
Default::default(),
)
);
// Unset `self.is_necessary`.
self.is_necessary = false;
result
}
/// Reconstruct the components of the struct init expression.