leo_passes/common/symbol_table/symbols.rs
1// Copyright (C) 2019-2026 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. This is an `Option` because variables are inserted into the
52 /// symbol table first without types. The types are only set in `TypeChecking`.
53 pub type_: Option<Type>,
54 /// The `Span` associated with the variable.
55 pub span: Span,
56 /// The type of declaration for the variable.
57 pub declaration: VariableType,
58}
59
60impl Display for VariableSymbol {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 if let Some(type_) = &self.type_ {
63 write!(f, "{}: {}", self.declaration, type_)?;
64 } else {
65 write!(f, "{}", self.declaration)?;
66 }
67 Ok(())
68 }
69}
70
71#[derive(Clone, Debug)]
72pub struct FunctionSymbol {
73 pub function: Function,
74 pub finalizer: Option<Finalizer>,
75}
76
77#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
78pub struct Finalizer {
79 /// The name of the async function this async transition calls.
80 pub location: Location,
81
82 /// The locations of the futures passed to the async function called by this async transition.
83 pub future_inputs: Vec<Location>,
84
85 /// The types passed to the async function called by this async transition.
86 pub inferred_inputs: Vec<Type>,
87}