leo_passes/type_checking/
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
17mod ast;
18
19mod program;
20
21mod scope_state;
22
23mod visitor;
24use visitor::*;
25
26use self::scope_state::ScopeState;
27use crate::{CallGraph, CompilerState, Pass, StructGraph};
28
29use leo_ast::ProgramVisitor;
30use leo_errors::Result;
31
32use indexmap::{IndexMap, IndexSet};
33
34/// Specify network limits for type checking.
35#[derive(Clone)]
36pub struct TypeCheckingInput {
37    pub max_array_elements: usize,
38    pub max_mappings: usize,
39    pub max_functions: usize,
40}
41
42/// A pass to check types.
43///
44/// Also constructs the struct graph, call graph, and local symbol table data.
45pub struct TypeChecking;
46
47impl Pass for TypeChecking {
48    type Input = TypeCheckingInput;
49    type Output = ();
50
51    const NAME: &'static str = "TypeChecking";
52
53    fn do_pass(input: Self::Input, state: &mut CompilerState) -> Result<Self::Output> {
54        let struct_names = state
55            .symbol_table
56            .iter_records()
57            .map(|(loc, _)| loc.name)
58            .chain(state.symbol_table.iter_structs().map(|(name, _)| name))
59            .collect();
60        let function_names = state.symbol_table.iter_functions().map(|(loc, _)| loc.name).collect();
61
62        let ast = std::mem::take(&mut state.ast);
63
64        // Note that the `struct_graph` and `call_graph` are initialized with their full node sets.
65        state.struct_graph = StructGraph::new(struct_names);
66        state.call_graph = CallGraph::new(function_names);
67
68        let mut visitor = TypeCheckingVisitor {
69            state,
70            scope_state: ScopeState::new(),
71            async_function_input_types: IndexMap::new(),
72            async_function_callers: IndexMap::new(),
73            used_structs: IndexSet::new(),
74            conditional_scopes: Vec::new(),
75            limits: input,
76        };
77        visitor.visit_program(ast.as_repr());
78        visitor.state.handler.last_err().map_err(|e| *e)?;
79
80        // Remove unused structs from the struct graph.
81        // This prevents unused struct definitions from being included in the generated bytecode.
82        visitor.state.struct_graph.retain_nodes(&visitor.used_structs);
83        visitor.state.ast = ast;
84
85        Ok(())
86    }
87}