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::{CompilerState, Pass};
28
29use leo_ast::{CallGraph, NetworkName, ProgramVisitor, StructGraph};
30use leo_errors::Result;
31
32use snarkvm::prelude::{CanaryV0, MainnetV0, Network, TestnetV0};
33
34use indexmap::{IndexMap, IndexSet};
35
36#[derive(Clone)]
38pub struct TypeCheckingInput {
39 pub max_array_elements: usize,
40 pub max_mappings: usize,
41 pub max_functions: usize,
42 pub max_inputs: usize,
43}
44
45impl TypeCheckingInput {
46 pub fn new(network: NetworkName) -> Self {
48 let (max_array_elements, max_mappings, max_functions, max_inputs) = match network {
49 NetworkName::MainnetV0 => (
50 MainnetV0::MAX_ARRAY_ELEMENTS,
51 MainnetV0::MAX_MAPPINGS,
52 MainnetV0::MAX_FUNCTIONS,
53 MainnetV0::MAX_INPUTS,
54 ),
55 NetworkName::TestnetV0 => (
56 TestnetV0::MAX_ARRAY_ELEMENTS,
57 TestnetV0::MAX_MAPPINGS,
58 TestnetV0::MAX_FUNCTIONS,
59 TestnetV0::MAX_INPUTS,
60 ),
61 NetworkName::CanaryV0 => {
62 (CanaryV0::MAX_ARRAY_ELEMENTS, CanaryV0::MAX_MAPPINGS, CanaryV0::MAX_FUNCTIONS, CanaryV0::MAX_INPUTS)
63 }
64 };
65 Self { max_array_elements, max_mappings, max_functions, max_inputs }
66 }
67}
68
69pub struct TypeChecking;
73
74impl Pass for TypeChecking {
75 type Input = TypeCheckingInput;
76 type Output = ();
77
78 const NAME: &'static str = "TypeChecking";
79
80 fn do_pass(input: Self::Input, state: &mut CompilerState) -> Result<Self::Output> {
81 let struct_names = state
82 .symbol_table
83 .iter_records()
84 .map(|(loc, _)| loc.path.clone())
85 .chain(state.symbol_table.iter_structs().map(|(name, _)| name.clone()))
86 .collect();
87 let function_names = state.symbol_table.iter_functions().map(|(loc, _)| loc.clone()).collect();
88
89 let ast = std::mem::take(&mut state.ast);
90
91 state.struct_graph = StructGraph::new(struct_names);
93 state.call_graph = CallGraph::new(function_names);
95
96 let mut visitor = TypeCheckingVisitor {
97 state,
98 scope_state: ScopeState::new(),
99 async_function_input_types: IndexMap::new(),
100 async_function_callers: IndexMap::new(),
101 used_structs: IndexSet::new(),
102 conditional_scopes: Vec::new(),
103 limits: input,
104 async_block_id: None,
105 };
106 visitor.visit_program(ast.as_repr());
107 visitor.state.handler.last_err().map_err(|e| *e)?;
108
109 visitor.state.struct_graph.retain_nodes(&visitor.used_structs);
112 visitor.state.ast = ast;
113
114 Ok(())
115 }
116}