|
16 | 16 |
|
17 | 17 | use crate::DeadCodeEliminator;
|
18 | 18 |
|
19 |
| -use leo_ast::{ |
20 |
| - AccessExpression, |
21 |
| - AssociatedFunction, |
22 |
| - Expression, |
23 |
| - ExpressionReconstructor, |
24 |
| - Identifier, |
25 |
| - StructExpression, |
26 |
| - StructVariableInitializer, |
27 |
| -}; |
28 |
| -use leo_span::sym; |
| 19 | +use leo_ast::{Expression, ExpressionReconstructor, Identifier}; |
29 | 20 |
|
30 |
| -impl ExpressionReconstructor for DeadCodeEliminator<'_> { |
| 21 | +impl ExpressionReconstructor for DeadCodeEliminator { |
31 | 22 | type AdditionalOutput = ();
|
32 | 23 |
|
33 |
| - /// Reconstructs the associated function access expression. |
34 |
| - fn reconstruct_associated_function(&mut self, input: AssociatedFunction) -> (Expression, Self::AdditionalOutput) { |
35 |
| - // If the associated function manipulates a mapping, or a cheat code, mark the statement as necessary. |
36 |
| - match (&input.variant.name, input.name.name) { |
37 |
| - (&sym::Mapping, sym::remove) |
38 |
| - | (&sym::Mapping, sym::set) |
39 |
| - | (&sym::Future, sym::Await) |
40 |
| - | (&sym::CheatCode, _) => { |
41 |
| - self.is_necessary = true; |
42 |
| - } |
43 |
| - _ => {} |
44 |
| - }; |
45 |
| - // Reconstruct the access expression. |
46 |
| - let result = ( |
47 |
| - Expression::Access(AccessExpression::AssociatedFunction(AssociatedFunction { |
48 |
| - variant: input.variant, |
49 |
| - name: input.name, |
50 |
| - arguments: input.arguments.into_iter().map(|arg| self.reconstruct_expression(arg).0).collect(), |
51 |
| - span: input.span, |
52 |
| - id: input.id, |
53 |
| - })), |
54 |
| - Default::default(), |
55 |
| - ); |
56 |
| - // Unset `self.is_necessary`. |
57 |
| - self.is_necessary = false; |
58 |
| - result |
59 |
| - } |
60 |
| - |
61 |
| - /// Reconstruct the components of the struct init expression. |
62 |
| - /// This is necessary since the reconstructor does not explicitly visit each component of the expression. |
63 |
| - fn reconstruct_struct_init(&mut self, input: StructExpression) -> (Expression, Self::AdditionalOutput) { |
64 |
| - ( |
65 |
| - Expression::Struct(StructExpression { |
66 |
| - name: input.name, |
67 |
| - // Reconstruct each of the struct members. |
68 |
| - members: input |
69 |
| - .members |
70 |
| - .into_iter() |
71 |
| - .map(|member| StructVariableInitializer { |
72 |
| - identifier: member.identifier, |
73 |
| - expression: match member.expression { |
74 |
| - Some(expression) => Some(self.reconstruct_expression(expression).0), |
75 |
| - None => unreachable!("Static single assignment ensures that the expression always exists."), |
76 |
| - }, |
77 |
| - span: member.span, |
78 |
| - id: member.id, |
79 |
| - }) |
80 |
| - .collect(), |
81 |
| - span: input.span, |
82 |
| - id: input.id, |
83 |
| - }), |
84 |
| - Default::default(), |
85 |
| - ) |
86 |
| - } |
87 |
| - |
88 |
| - /// Marks identifiers as used. |
89 |
| - /// This is necessary to determine which statements can be eliminated from the program. |
| 24 | + // Use and reconstruct an identifier that may not necessarily be used. |
90 | 25 | fn reconstruct_identifier(&mut self, input: Identifier) -> (Expression, Self::AdditionalOutput) {
|
91 |
| - // Add the identifier to `self.used_variables`. |
92 |
| - if self.is_necessary { |
93 |
| - self.used_variables.insert(input.name); |
94 |
| - } |
95 |
| - // Return the identifier as is. |
| 26 | + self.used_variables.insert(input.name); |
96 | 27 | (Expression::Identifier(input), Default::default())
|
97 | 28 | }
|
98 | 29 | }
|
0 commit comments