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
// 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 itertools::Itertools;
use leo_ast::{Expression::Literal, Type::Integer, *};
use leo_errors::loop_unroller::LoopUnrollerError;
use leo_span::{Span, Symbol};

use crate::{VariableSymbol, VariableType, unroller::Unroller};

impl StatementReconstructor for Unroller<'_> {
    fn reconstruct_block(&mut self, input: Block) -> (Block, Self::AdditionalOutput) {
        let scope_index = self.current_scope_index();

        // Enter the block scope.
        let previous_scope_index = self.enter_scope(scope_index);

        // Filter out the statements that have additional output = true
        let filtered_statements: Vec<_> = input
            .statements
            .into_iter()
            .filter_map(|s| {
                let (reconstructed_statement, additional_output) = self.reconstruct_statement(s);
                if additional_output {
                    None // Exclude this statement from the block since it is a constant variable definition
                } else {
                    Some(reconstructed_statement)
                }
            })
            .collect();

        let block = Block { statements: filtered_statements, span: input.span, id: input.id };

        // Exit the block scope.
        self.exit_scope(previous_scope_index);

        (block, Default::default())
    }

    fn reconstruct_const(&mut self, input: ConstDeclaration) -> (Statement, Self::AdditionalOutput) {
        // Reconstruct the RHS expression to allow for constant propagation
        let reconstructed_value_expression = self.reconstruct_expression(input.value.clone()).0;

        // Add to constant propagation table. Since TC completed we know that the RHS is a literal or tuple of literals.
        if let Err(err) = self.constant_propagation_table.borrow_mut().insert_constant(input.place.name, input.value) {
            self.handler.emit_err(err);
        }

        // Remove from symbol table
        self.symbol_table.borrow_mut().remove_variable_from_current_scope(Location::new(None, input.place.name));

        (
            Statement::Const(ConstDeclaration {
                place: input.place,
                type_: input.type_,
                value: reconstructed_value_expression,
                span: input.span,
                id: input.id,
            }),
            true,
        )
    }

    fn reconstruct_definition(&mut self, input: DefinitionStatement) -> (Statement, Self::AdditionalOutput) {
        // Helper function to add  variables to symbol table
        let insert_variable = |symbol: Symbol, type_: Type, span: Span| {
            if let Err(err) =
                self.symbol_table.borrow_mut().insert_variable(Location::new(None, symbol), VariableSymbol {
                    type_,
                    span,
                    declaration: VariableType::Mut,
                })
            {
                self.handler.emit_err(err);
            }
        };

        // If we are unrolling a loop, then we need to repopulate the symbol table.
        if self.is_unrolling {
            match &input.place {
                Expression::Identifier(identifier) => {
                    insert_variable(identifier.name, input.type_.clone(), input.span);
                }
                Expression::Tuple(tuple_expression) => {
                    let tuple_type = match input.type_ {
                        Type::Tuple(ref tuple_type) => tuple_type,
                        _ => unreachable!(
                            "Type checking guarantees that if the lhs is a tuple, its associated type is also a tuple."
                        ),
                    };
                    tuple_expression.elements.iter().zip_eq(tuple_type.elements().iter()).for_each(|(expression, _type_)| {
                        let identifier = match expression {
                            Expression::Identifier(identifier) => identifier,
                            _ => unreachable!("Type checking guarantees that if the lhs is a tuple, all of its elements are identifiers.")
                        };
                        insert_variable(identifier.name, input.type_.clone(), input.span);
                    });
                }
                _ => unreachable!(
                    "Type checking guarantees that the lhs of a `DefinitionStatement` is either an identifier or tuple."
                ),
            }
        }

        // Reconstruct the expression and return
        (
            Statement::Definition(DefinitionStatement {
                declaration_type: input.declaration_type,
                place: input.place,
                type_: input.type_,
                value: self.reconstruct_expression(input.value).0,
                span: input.span,
                id: input.id,
            }),
            Default::default(),
        )
    }

    fn reconstruct_iteration(&mut self, input: IterationStatement) -> (Statement, Self::AdditionalOutput) {
        // Reconstruct the bound expressions
        let (new_start, _) = self.reconstruct_expression(input.start);
        let (new_stop, _) = self.reconstruct_expression(input.stop);

        // Convert into values
        match (new_start.clone(), new_stop.clone()) {
            (Literal(start_lit), Literal(stop_lit)) => {
                input.start_value.replace(Some(Value::try_from(&start_lit).unwrap()));
                input.stop_value.replace(Some(Value::try_from(&stop_lit).unwrap()));
            }
            _ => unreachable!("Type checking guarantees that the loop bounds are literals."),
        };

        // Ensure loop bounds are increasing. This cannot be done in the type checker because constant propagation occurs in this pass.
        if match (input.type_.clone(), input.start_value.borrow().as_ref(), input.stop_value.borrow().as_ref()) {
            (Integer(IntegerType::I8), Some(Value::I8(lower_bound, _)), Some(Value::I8(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::I16), Some(Value::I16(lower_bound, _)), Some(Value::I16(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::I32), Some(Value::I32(lower_bound, _)), Some(Value::I32(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::I64), Some(Value::I64(lower_bound, _)), Some(Value::I64(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::I128), Some(Value::I128(lower_bound, _)), Some(Value::I128(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::U8), Some(Value::U8(lower_bound, _)), Some(Value::U8(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::U16), Some(Value::U16(lower_bound, _)), Some(Value::U16(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::U32), Some(Value::U32(lower_bound, _)), Some(Value::U32(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::U64), Some(Value::U64(lower_bound, _)), Some(Value::U64(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            (Integer(IntegerType::U128), Some(Value::U128(lower_bound, _)), Some(Value::U128(upper_bound, _))) => {
                lower_bound >= upper_bound
            }
            _ => unreachable!("Type checking guarantees that the loop bounds have same type as loop variable."),
        } {
            self.emit_err(LoopUnrollerError::loop_range_decreasing(new_stop.span()));
        }

        (
            self.unroll_iteration_statement::<i128>(IterationStatement {
                variable: input.variable,
                type_: input.type_,
                start: new_start,
                stop: new_stop,
                start_value: input.start_value.clone(),
                stop_value: input.stop_value.clone(),
                inclusive: false,
                block: input.block,
                span: input.span,
                id: input.id,
            }),
            Default::default(),
        )
    }
}