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