leo_passes/common/rename_table/
mod.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 leo_ast::NodeID;
18use leo_span::Symbol;
19
20use indexmap::IndexMap;
21
22/// `RenameTable` tracks the names assigned by static single assignment in a single scope.
23#[derive(Clone, Debug, Default, Eq, PartialEq)]
24pub struct RenameTable {
25    /// The `RenameTable` of the parent scope.
26    pub(crate) parent: Option<Box<RenameTable>>,
27    /// The mapping from names in the original AST to new names in the renamed AST.
28    names: IndexMap<Symbol, Symbol>,
29    /// The mapping from symbols to node IDs.
30    /// These are used to ensure that newly introduced symbols reference the appropriate information
31    /// that has been previously indexed by node ID. e,g. `TypeTable`.
32    ids: IndexMap<Symbol, NodeID>,
33}
34
35impl RenameTable {
36    /// Create a new `RenameTable` with the given parent.
37    pub(crate) fn new(parent: Option<Box<RenameTable>>) -> Self {
38        Self { parent, names: IndexMap::new(), ids: IndexMap::new() }
39    }
40
41    /// Returns the symbols that were renamed in the current scope.
42    pub(crate) fn local_names(&self) -> impl Iterator<Item = &Symbol> {
43        self.names.keys()
44    }
45
46    /// Updates `self.mapping` with the desired entry.
47    /// Creates a new entry if `symbol` is not already in `self.mapping`.
48    pub(crate) fn update(&mut self, symbol: Symbol, new_symbol: Symbol, id: NodeID) {
49        self.names.insert(symbol, new_symbol);
50        self.ids.insert(new_symbol, id);
51    }
52
53    /// Looks up the new name for `symbol`, recursively checking the parent if it is not found.
54    pub(crate) fn lookup(&self, symbol: Symbol) -> Option<&Symbol> {
55        if let Some(var) = self.names.get(&symbol) {
56            Some(var)
57        } else if let Some(parent) = &self.parent {
58            parent.lookup(symbol)
59        } else {
60            None
61        }
62    }
63
64    /// Looks up the node ID for `symbol`, recursively checking the parent if it is not found.
65    pub(crate) fn lookup_id(&self, symbol: &Symbol) -> Option<&NodeID> {
66        if let Some(id) = self.ids.get(symbol) {
67            Some(id)
68        } else if let Some(parent) = &self.parent {
69            parent.lookup_id(symbol)
70        } else {
71            None
72        }
73    }
74}