leo_passes/pass.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
17use crate::{Assigner, CallGraph, StructGraph, SymbolTable, TypeTable};
18
19use leo_ast::{Ast, NodeBuilder};
20use leo_errors::{Handler, LeoWarning, Result};
21
22use std::collections::HashSet;
23
24/// Contains data share by many compiler passes.
25#[derive(Default)]
26pub struct CompilerState {
27 /// The Abstract Syntax Tree.
28 pub ast: Ast,
29 /// The error Handler.
30 pub handler: Handler,
31 /// Maps node IDs to types.
32 pub type_table: TypeTable,
33 /// Creates incrementing node IDs.
34 pub node_builder: NodeBuilder,
35 /// Creates unique symbols and definitions.
36 pub assigner: Assigner,
37 /// Contains data about the variables and other entities in the program.
38 pub symbol_table: SymbolTable,
39 /// A graph of which structs refer to each other.
40 pub struct_graph: StructGraph,
41 /// A graph of which functions call each other.
42 pub call_graph: CallGraph,
43 /// A set of the warnings collected. This is used to make sure we don't emit the same exact warning twice.
44 pub warnings: HashSet<LeoWarning>,
45 /// Is this a test program?
46 pub is_test: bool,
47}
48
49/// A compiler pass.
50///
51/// Every pass has access to `CompilerState`, and may also specify
52/// an `Input` and `Output`.
53pub trait Pass {
54 type Input;
55 type Output;
56
57 const NAME: &str;
58
59 /// Runs the compiler pass.
60 fn do_pass(input: Self::Input, state: &mut CompilerState) -> Result<Self::Output>;
61}