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 SsaFormingVisitor<'_> {
33    /// Pushes a new scope, setting the current scope as the new scope's parent.
34    pub(crate) fn push(&mut self) {
35        let parent_table = core::mem::take(&mut self.rename_table);
36        self.rename_table = RenameTable::new(Some(Box::from(parent_table)));
37    }
38
39    /// If the RenameTable has a parent, then `self.rename_table` is set to the parent, otherwise it is set to a default `RenameTable`.
40    pub(crate) fn pop(&mut self) -> RenameTable {
41        let parent = self.rename_table.parent.clone().unwrap_or_default();
42        core::mem::replace(&mut self.rename_table, *parent)
43    }
44
45    pub(crate) fn rename_identifier(&mut self, mut identifier: Identifier) -> Identifier {
46        // Associate this name with its id.
47        self.rename_table.update(identifier.name, identifier.name, identifier.id);
48
49        let new_name = self.state.assigner.unique_symbol(identifier.name, "$#");
50        self.rename_table.update(identifier.name, new_name, identifier.id);
51        identifier.name = new_name;
52        identifier
53    }
54
55    pub(crate) fn simple_definition(&mut self, identifier: Identifier, rhs: Expression) -> Statement {
56        // Update the type table.
57        let type_ = match self.state.type_table.get(&rhs.id()) {
58            Some(type_) => type_,
59            None => unreachable!("Type checking guarantees that all expressions have a type."),
60        };
61        self.state.type_table.insert(identifier.id(), type_);
62        // Update the rename table.
63        self.rename_table.update(identifier.name, identifier.name, identifier.id);
64        // Construct the statement.
65        self.state.assigner.simple_definition(identifier, rhs, self.state.node_builder.next_id())
66    }
67
68    /// Constructs a simple assign statement for `expr` with a unique name.
69    /// For example, `expr` is transformed into `$var$0 = expr;`.
70    /// The lhs is guaranteed to be unique with respect to the `Assigner`.
71    pub(crate) fn unique_simple_definition(&mut self, expr: Expression) -> (Identifier, Statement) {
72        // Create a new variable for the expression.
73        let name = self.state.assigner.unique_symbol("$var", "$");
74
75        // Create a new identifier for the variable.
76        let place = Identifier { name, span: Default::default(), id: self.state.node_builder.next_id() };
77
78        // Construct the statement.
79        let statement = self.simple_definition(place, expr);
80
81        (place, statement)
82    }
83}