leo_passes/type_checking/
mod.rs1mod 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#[derive(Clone)]
36pub struct TypeCheckingInput {
37 pub max_array_elements: usize,
38 pub max_mappings: usize,
39 pub max_functions: usize,
40}
41
42pub 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 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 visitor.state.struct_graph.retain_nodes(&visitor.used_structs);
83 visitor.state.ast = ast;
84
85 Ok(())
86 }
87}