leo_passes/common/symbol_table/
symbols.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 std::fmt::Display;
18
19use serde::{Deserialize, Serialize};
20
21use leo_ast::{Function, Location, Mode, Type};
22use leo_span::Span;
23
24/// An enumeration of the different types of variable type.
25#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
26pub enum VariableType {
27    Const,
28    ConstParameter,
29    Input(Mode),
30    Mut,
31}
32
33impl Display for VariableType {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        use VariableType::*;
36
37        match self {
38            Const => write!(f, "const var"),
39            ConstParameter => write!(f, "const parameter"),
40            Input(m) => write!(f, "{m} input"),
41            Mut => write!(f, "mut var"),
42        }
43    }
44}
45
46/// An entry for a variable in the symbol table.
47#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
48pub struct VariableSymbol {
49    /// The `Type` of the variable.
50    pub type_: Type,
51    /// The `Span` associated with the variable.
52    pub span: Span,
53    /// The type of declaration for the variable.
54    pub declaration: VariableType,
55}
56
57impl Display for VariableSymbol {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(f, "{}: {}", self.declaration, self.type_)?;
60        Ok(())
61    }
62}
63
64#[derive(Clone, Debug)]
65pub struct FunctionSymbol {
66    pub function: Function,
67    pub finalizer: Option<Finalizer>,
68}
69
70#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
71pub struct Finalizer {
72    /// The name of the async function this async transition calls.
73    pub location: Location,
74
75    /// The locations of the futures passed to the async function called by this async transition.
76    pub future_inputs: Vec<Location>,
77
78    /// The types passed to the async function called by this async transition.
79    pub inferred_inputs: Vec<Type>,
80}