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