1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::DeadCodeEliminator;

use leo_ast::{
    AccessExpression,
    AssertStatement,
    AssertVariant,
    AssignStatement,
    Block,
    ConditionalStatement,
    ConsoleStatement,
    DefinitionStatement,
    Expression,
    ExpressionReconstructor,
    ExpressionStatement,
    IterationStatement,
    ReturnStatement,
    Statement,
    StatementReconstructor,
};

impl StatementReconstructor for DeadCodeEliminator<'_> {
    fn reconstruct_assert(&mut self, input: AssertStatement) -> (Statement, Self::AdditionalOutput) {
        // Set the `is_necessary` flag.
        self.is_necessary = true;

        // Visit the statement.
        let statement = Statement::Assert(AssertStatement {
            variant: match input.variant {
                AssertVariant::Assert(expr) => AssertVariant::Assert(self.reconstruct_expression(expr).0),
                AssertVariant::AssertEq(left, right) => {
                    AssertVariant::AssertEq(self.reconstruct_expression(left).0, self.reconstruct_expression(right).0)
                }
                AssertVariant::AssertNeq(left, right) => {
                    AssertVariant::AssertNeq(self.reconstruct_expression(left).0, self.reconstruct_expression(right).0)
                }
            },
            span: input.span,
            id: input.id,
        });

        // Unset the `is_necessary` flag.
        self.is_necessary = false;

        (statement, Default::default())
    }

    /// Reconstruct an assignment statement by eliminating any dead code.
    fn reconstruct_assign(&mut self, input: AssignStatement) -> (Statement, Self::AdditionalOutput) {
        // Check the lhs of the assignment to see any of variables are used.
        let lhs_is_used = match &input.place {
            Expression::Identifier(identifier) => self.used_variables.contains(&identifier.name),
            Expression::Tuple(tuple_expression) => tuple_expression
                .elements
                .iter()
                .map(|element| match element {
                    Expression::Identifier(identifier) => identifier.name,
                    _ => unreachable!(
                        "The previous compiler passes guarantee the tuple elements on the lhs are identifiers."
                    ),
                })
                .any(|symbol| self.used_variables.contains(&symbol)),
            _ => unreachable!(
                "The previous compiler passes guarantee that `place` is either an identifier or tuple of identifiers."
            ),
        };

        match lhs_is_used {
            // If the lhs is used, then we return the original statement.
            true => {
                // Set the `is_necessary` flag.
                self.is_necessary = true;

                // Visit the statement.
                let statement = Statement::Assign(Box::new(AssignStatement {
                    place: input.place,
                    value: self.reconstruct_expression(input.value).0,
                    span: input.span,
                    id: input.id,
                }));

                // Unset the `is_necessary` flag.
                self.is_necessary = false;

                (statement, Default::default())
            }
            // Otherwise, we can eliminate it.
            false => (Statement::dummy(Default::default(), self.node_builder.next_id()), Default::default()),
        }
    }

    /// Reconstructs the statements inside a basic block, eliminating any dead code.
    fn reconstruct_block(&mut self, block: Block) -> (Block, Self::AdditionalOutput) {
        // Reconstruct each of the statements in reverse.
        let mut statements: Vec<Statement> =
            block.statements.into_iter().rev().map(|statement| self.reconstruct_statement(statement).0).collect();

        // Reverse the direction of `statements`.
        statements.reverse();

        (Block { statements, span: block.span, id: block.id }, Default::default())
    }

    /// Flattening removes conditional statements from the program.
    fn reconstruct_conditional(&mut self, input: ConditionalStatement) -> (Statement, Self::AdditionalOutput) {
        if !self.is_async {
            unreachable!("`ConditionalStatement`s should not be in the AST at this phase of compilation.")
        } else {
            (
                Statement::Conditional(ConditionalStatement {
                    then: self.reconstruct_block(input.then).0,
                    otherwise: input.otherwise.map(|n| Box::new(self.reconstruct_statement(*n).0)),
                    condition: {
                        // Set the `is_necessary` flag.
                        self.is_necessary = true;
                        let condition = self.reconstruct_expression(input.condition).0;
                        // Unset the `is_necessary` flag.
                        self.is_necessary = false;

                        condition
                    },
                    span: input.span,
                    id: input.id,
                }),
                Default::default(),
            )
        }
    }

    /// Parsing guarantees that console statements are not present in the program.
    fn reconstruct_console(&mut self, _: ConsoleStatement) -> (Statement, Self::AdditionalOutput) {
        unreachable!("`ConsoleStatement`s should not be in the AST at this phase of compilation.")
    }

    /// Static single assignment replaces definition statements with assignment statements.
    fn reconstruct_definition(&mut self, _: DefinitionStatement) -> (Statement, Self::AdditionalOutput) {
        unreachable!("`DefinitionStatement`s should not exist in the AST at this phase of compilation.")
    }

    /// Reconstructs expression statements by eliminating any dead code.
    fn reconstruct_expression_statement(&mut self, input: ExpressionStatement) -> (Statement, Self::AdditionalOutput) {
        match input.expression {
            // If the expression is a function call, then we reconstruct it.
            // Note that we preserve function calls because they may have side effects.
            Expression::Call(expression) => {
                // Set the `is_necessary` flag.
                self.is_necessary = true;

                // Visit the expression.
                let statement = Statement::Expression(ExpressionStatement {
                    expression: self.reconstruct_call(expression).0,
                    span: input.span,
                    id: input.id,
                });

                // Unset the `is_necessary` flag.
                self.is_necessary = false;

                (statement, Default::default())
            }
            Expression::Access(AccessExpression::AssociatedFunction(associated_function)) => {
                // Visit the expression.
                (
                    Statement::Expression(ExpressionStatement {
                        expression: self
                            .reconstruct_access(AccessExpression::AssociatedFunction(associated_function))
                            .0,
                        span: input.span,
                        id: input.id,
                    }),
                    Default::default(),
                )
            }
            // Any other expression is dead code, since they do not have side effects.
            // Note: array access expressions will have side effects and need to be handled here.
            _ => (Statement::dummy(Default::default(), self.node_builder.next_id()), Default::default()),
        }
    }

    /// Loop unrolling unrolls and removes iteration statements from the program.
    fn reconstruct_iteration(&mut self, _: IterationStatement) -> (Statement, Self::AdditionalOutput) {
        unreachable!("`IterationStatement`s should not be in the AST at this phase of compilation.");
    }

    fn reconstruct_return(&mut self, input: ReturnStatement) -> (Statement, Self::AdditionalOutput) {
        // Set the `is_necessary` flag.
        self.is_necessary = true;

        // Visit the statement.
        let statement = Statement::Return(ReturnStatement {
            expression: self.reconstruct_expression(input.expression).0,
            span: input.span,
            id: input.id,
        });

        // Unset the `is_necessary` flag.
        self.is_necessary = false;

        (statement, Default::default())
    }
}