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 pub max_outputs: usize,
44}
45
46impl TypeCheckingInput {
47 pub fn new(network: NetworkName) -> Self {
49 let (max_array_elements, max_mappings, max_functions, max_inputs, max_outputs) = match network {
50 NetworkName::MainnetV0 => (
51 MainnetV0::MAX_ARRAY_ELEMENTS,
52 MainnetV0::MAX_MAPPINGS,
53 MainnetV0::MAX_FUNCTIONS,
54 MainnetV0::MAX_INPUTS,
55 MainnetV0::MAX_OUTPUTS,
56 ),
57 NetworkName::TestnetV0 => (
58 TestnetV0::MAX_ARRAY_ELEMENTS,
59 TestnetV0::MAX_MAPPINGS,
60 TestnetV0::MAX_FUNCTIONS,
61 TestnetV0::MAX_INPUTS,
62 TestnetV0::MAX_OUTPUTS,
63 ),
64 NetworkName::CanaryV0 => (
65 CanaryV0::MAX_ARRAY_ELEMENTS,
66 CanaryV0::MAX_MAPPINGS,
67 CanaryV0::MAX_FUNCTIONS,
68 CanaryV0::MAX_INPUTS,
69 CanaryV0::MAX_OUTPUTS,
70 ),
71 };
72 Self { max_array_elements, max_mappings, max_functions, max_inputs, max_outputs }
73 }
74}
75
76pub struct TypeChecking;
80
81impl Pass for TypeChecking {
82 type Input = TypeCheckingInput;
83 type Output = ();
84
85 const NAME: &'static str = "TypeChecking";
86
87 fn do_pass(input: Self::Input, state: &mut CompilerState) -> Result<Self::Output> {
88 let struct_names = state
89 .symbol_table
90 .iter_records()
91 .map(|(loc, _)| loc.path.clone())
92 .chain(state.symbol_table.iter_structs().map(|(name, _)| name.clone()))
93 .collect();
94 let function_names = state.symbol_table.iter_functions().map(|(loc, _)| loc.clone()).collect();
95
96 let ast = std::mem::take(&mut state.ast);
97
98 state.struct_graph = StructGraph::new(struct_names);
100 state.call_graph = CallGraph::new(function_names);
102
103 let mut visitor = TypeCheckingVisitor {
104 state,
105 scope_state: ScopeState::new(),
106 async_function_input_types: IndexMap::new(),
107 async_function_callers: IndexMap::new(),
108 used_structs: IndexSet::new(),
109 conditional_scopes: Vec::new(),
110 limits: input,
111 async_block_id: None,
112 };
113 visitor.visit_program(ast.as_repr());
114 visitor.state.handler.last_err()?;
115
116 visitor.state.struct_graph.retain_nodes(&visitor.used_structs);
119 visitor.state.ast = ast;
120
121 Ok(())
122 }
123}