leo_passes/type_checking/scope_state.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 indexmap::IndexMap;
18use leo_ast::{Location, Variant};
19use leo_span::Symbol;
20
21pub struct ScopeState {
22 /// The name of the function that we are currently traversing.
23 pub(crate) function: Option<Symbol>,
24 /// The variant of the function that we are currently traversing.
25 pub(crate) variant: Option<Variant>,
26 /// Whether or not the function that we are currently traversing has a return statement.
27 pub(crate) has_return: bool,
28 /// Current program name.
29 pub(crate) program_name: Option<Symbol>,
30 /// Whether or not we are currently traversing a stub.
31 pub(crate) is_stub: bool,
32 /// The futures that must be propagated to an async function.
33 pub(crate) futures: IndexMap<Symbol, Location>,
34 /// Whether the finalize caller has called the finalize function.
35 pub(crate) has_called_finalize: bool,
36 /// Whether currently traversing a conditional statement.
37 pub(crate) is_conditional: bool,
38 /// Whether the current function is a call.
39 pub(crate) is_call: bool,
40 /// Location of most recent external call that produced a future.
41 pub(crate) call_location: Option<Location>,
42}
43
44impl ScopeState {
45 /// Initializes a new `ScopeState`.
46 pub fn new() -> Self {
47 Self {
48 function: None,
49 variant: None,
50 has_return: false,
51 program_name: None,
52 is_stub: true,
53 futures: IndexMap::new(),
54 has_called_finalize: false,
55 is_conditional: false,
56 is_call: false,
57 call_location: None,
58 }
59 }
60
61 /// Initialize state variables for new function.
62 pub fn initialize_function_state(&mut self, variant: Variant) {
63 self.variant = Some(variant);
64 self.has_called_finalize = false;
65 self.futures = IndexMap::new();
66 }
67
68 /// Get the current location.
69 pub fn location(&self) -> Location {
70 Location::new(
71 self.program_name.expect("Only call ScopeState::location when visiting a function or function stub."),
72 self.function.expect("Only call ScopeState::location when visiting a function or function stub."),
73 )
74 }
75}