leo_passes/static_single_assignment/
visitor.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{CompilerState, RenameTable};
18
19use leo_ast::{Expression, Identifier, Node, Statement};
20use leo_span::Symbol;
21
22pub struct SsaFormingVisitor<'a> {
23    pub state: &'a mut CompilerState,
24    /// The `RenameTable` for the current basic block in the AST
25    pub rename_table: RenameTable,
26    /// The main program name.
27    pub program: Symbol,
28    /// Whether to rename places in definitions.
29    pub rename_defs: bool,
30}
31
32impl<'a> SsaFormingVisitor<'a> {
33    pub fn new(state: &'a mut CompilerState, input: crate::SsaFormingInput, program: Symbol) -> Self {
34        Self { state, rename_table: RenameTable::new(None), program, rename_defs: input.rename_defs }
35    }
36
37    /// Pushes a new scope, setting the current scope as the new scope's parent.
38    pub(crate) fn push(&mut self) {
39        let parent_table = core::mem::take(&mut self.rename_table);
40        self.rename_table = RenameTable::new(Some(Box::from(parent_table)));
41    }
42
43    /// If the RenameTable has a parent, then `self.rename_table` is set to the parent, otherwise it is set to a default `RenameTable`.
44    pub(crate) fn pop(&mut self) -> RenameTable {
45        let parent = self.rename_table.parent.clone().unwrap_or_default();
46        core::mem::replace(&mut self.rename_table, *parent)
47    }
48
49    pub(crate) fn rename_identifier(&mut self, mut identifier: Identifier) -> Identifier {
50        // Associate this name with its id.
51        self.rename_table.update(identifier.name, identifier.name, identifier.id);
52
53        let new_name = self.state.assigner.unique_symbol(identifier.name, "$#");
54        self.rename_table.update(identifier.name, new_name, identifier.id);
55        identifier.name = new_name;
56        identifier
57    }
58
59    pub(crate) fn simple_definition(&mut self, identifier: Identifier, rhs: Expression) -> Statement {
60        // Update the type table.
61        let type_ = match self.state.type_table.get(&rhs.id()) {
62            Some(type_) => type_,
63            None => unreachable!("Type checking guarantees that all expressions have a type."),
64        };
65        self.state.type_table.insert(identifier.id(), type_);
66        // Update the rename table.
67        self.rename_table.update(identifier.name, identifier.name, identifier.id);
68        // Construct the statement.
69        self.state.assigner.simple_definition(identifier, rhs, self.state.node_builder.next_id())
70    }
71
72    /// Constructs a simple assign statement for `expr` with a unique name.
73    /// For example, `expr` is transformed into `$var$0 = expr;`.
74    /// The lhs is guaranteed to be unique with respect to the `Assigner`.
75    pub(crate) fn unique_simple_definition(&mut self, expr: Expression) -> (Identifier, Statement) {
76        // Create a new variable for the expression.
77        let name = self.state.assigner.unique_symbol("$var", "$");
78
79        // Create a new identifier for the variable.
80        let place = Identifier { name, span: Default::default(), id: self.state.node_builder.next_id() };
81
82        // Construct the statement.
83        let statement = self.simple_definition(place, expr);
84
85        (place, statement)
86    }
87}