leo_passes/common/type_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, Type};
18
19use indexmap::IndexMap;
20use std::cell::RefCell;
21
22/// A mapping between node IDs and their types.
23#[derive(Debug, Default, Clone)]
24pub struct TypeTable {
25    /// The inner table.
26    /// `RefCell` is used here to avoid `&mut` all over the compiler.
27    inner: RefCell<IndexMap<NodeID, Type>>,
28}
29
30impl TypeTable {
31    /// Gets an entry from the table.
32    pub fn get(&self, index: &NodeID) -> Option<Type> {
33        self.inner.borrow().get(index).cloned()
34    }
35
36    /// Inserts an entry into the table.
37    pub fn insert(&self, index: NodeID, value: Type) {
38        self.inner.borrow_mut().insert(index, value);
39    }
40}