leo_compiler/
compiler.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
17//! The compiler for Leo programs.
18//!
19//! The [`Compiler`] type compiles Leo programs into R1CS circuits.
20
21use crate::{AstSnapshots, CompilerOptions};
22
23pub use leo_ast::Ast;
24use leo_ast::Stub;
25use leo_errors::{CompilerError, Handler, Result};
26use leo_passes::*;
27use leo_span::{Symbol, source_map::FileName, with_session_globals};
28
29use snarkvm::prelude::Network;
30
31use indexmap::{IndexMap, IndexSet};
32use std::{
33    fs,
34    path::{Path, PathBuf},
35};
36
37/// The primary entry point of the Leo compiler.
38pub struct Compiler<N: Network> {
39    /// The path to where the compiler outputs all generated files.
40    output_directory: PathBuf,
41    /// The program name,
42    pub program_name: Option<String>,
43    /// Options configuring compilation.
44    compiler_options: CompilerOptions,
45    /// State.
46    state: CompilerState,
47    /// The stubs for imported programs.
48    import_stubs: IndexMap<Symbol, Stub>,
49    /// How many statements were in the AST before DCE?
50    pub statements_before_dce: u32,
51    /// How many statements were in the AST after DCE?
52    pub statements_after_dce: u32,
53    // Allows the compiler to be generic over the network.
54    phantom: std::marker::PhantomData<N>,
55}
56
57impl<N: Network> Compiler<N> {
58    pub fn parse(&mut self, source: &str, filename: FileName) -> Result<()> {
59        // Register the source in the source map.
60        let source_file = with_session_globals(|s| s.source_map.new_source(source, filename));
61
62        // Use the parser to construct the abstract syntax tree (ast).
63        self.state.ast = leo_parser::parse_ast::<N>(
64            self.state.handler.clone(),
65            &self.state.node_builder,
66            &source_file.src,
67            source_file.absolute_start,
68        )?;
69
70        // Check that the name of its program scope matches the expected name.
71        // Note that parsing enforces that there is exactly one program scope in a file.
72        let program_scope = self.state.ast.ast.program_scopes.values().next().unwrap();
73        if self.program_name.is_none() {
74            self.program_name = Some(program_scope.program_id.name.to_string());
75        } else if self.program_name != Some(program_scope.program_id.name.to_string()) {
76            return Err(CompilerError::program_name_should_match_file_name(
77                program_scope.program_id.name,
78                self.program_name.as_ref().unwrap(),
79                program_scope.program_id.name.span,
80            )
81            .into());
82        }
83
84        if self.compiler_options.initial_ast {
85            self.write_ast_to_json("initial.json")?;
86            self.write_ast("initial.ast")?;
87        }
88
89        Ok(())
90    }
91
92    pub fn parse_from_file(&mut self, source_file_path: impl AsRef<Path>) -> Result<()> {
93        // Load the program file.
94        let source = fs::read_to_string(&source_file_path)
95            .map_err(|e| CompilerError::file_read_error(source_file_path.as_ref().display().to_string(), e))?;
96        self.parse(&source, FileName::Real(source_file_path.as_ref().into()))
97    }
98
99    /// Returns a new Leo compiler.
100    pub fn new(
101        expected_program_name: Option<String>,
102        is_test: bool,
103        handler: Handler,
104        output_directory: PathBuf,
105        compiler_options: Option<CompilerOptions>,
106        import_stubs: IndexMap<Symbol, Stub>,
107    ) -> Self {
108        Self {
109            state: CompilerState { handler, is_test, ..Default::default() },
110            output_directory,
111            program_name: expected_program_name,
112            compiler_options: compiler_options.unwrap_or_default(),
113            import_stubs,
114            statements_before_dce: 0,
115            statements_after_dce: 0,
116            phantom: Default::default(),
117        }
118    }
119
120    fn do_pass<P: Pass>(&mut self, input: P::Input) -> Result<P::Output> {
121        let output = P::do_pass(input, &mut self.state)?;
122
123        let write = match &self.compiler_options.ast_snapshots {
124            AstSnapshots::All => true,
125            AstSnapshots::Some(passes) => passes.contains(P::NAME),
126        };
127
128        if write {
129            self.write_ast_to_json(&format!("{}.json", P::NAME))?;
130            self.write_ast(&format!("{}.ast", P::NAME))?;
131        }
132
133        Ok(output)
134    }
135
136    /// Runs the compiler stages.
137    pub fn intermediate_passes(&mut self) -> Result<()> {
138        let type_checking_config = TypeCheckingInput {
139            max_array_elements: N::MAX_ARRAY_ELEMENTS,
140            max_mappings: N::MAX_MAPPINGS,
141            max_functions: N::MAX_FUNCTIONS,
142        };
143
144        self.do_pass::<SymbolTableCreation>(())?;
145
146        self.do_pass::<TypeChecking>(type_checking_config.clone())?;
147
148        self.do_pass::<StaticAnalyzing>(())?;
149
150        self.do_pass::<ConstPropagationAndUnrolling>(type_checking_config)?;
151
152        self.do_pass::<ProcessingScript>(())?;
153
154        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: true })?;
155
156        self.do_pass::<Destructuring>(())?;
157
158        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
159
160        self.do_pass::<WriteTransforming>(())?;
161
162        self.do_pass::<SsaForming>(SsaFormingInput { rename_defs: false })?;
163
164        self.do_pass::<Flattening>(())?;
165
166        self.do_pass::<FunctionInlining>(())?;
167
168        let output = self.do_pass::<DeadCodeEliminating>(())?;
169        self.statements_before_dce = output.statements_before;
170        self.statements_after_dce = output.statements_after;
171
172        Ok(())
173    }
174
175    /// Returns a compiled Leo program.
176    pub fn compile(&mut self, source: &str, filename: FileName) -> Result<String> {
177        // Parse the program.
178        self.parse(source, filename)?;
179        // Merge the stubs into the AST.
180        self.add_import_stubs()?;
181        // Run the intermediate compiler stages.
182        self.intermediate_passes()?;
183        // Run code generation.
184        let bytecode = CodeGenerating::do_pass((), &mut self.state)?;
185        Ok(bytecode)
186    }
187
188    pub fn compile_from_file(&mut self, source_file_path: impl AsRef<Path>) -> Result<String> {
189        let source = fs::read_to_string(&source_file_path)
190            .map_err(|e| CompilerError::file_read_error(source_file_path.as_ref().display().to_string(), e))?;
191        self.compile(&source, FileName::Real(source_file_path.as_ref().into()))
192    }
193
194    /// Writes the AST to a JSON file.
195    fn write_ast_to_json(&self, file_suffix: &str) -> Result<()> {
196        // Remove `Span`s if they are not enabled.
197        if self.compiler_options.ast_spans_enabled {
198            self.state.ast.to_json_file(
199                self.output_directory.clone(),
200                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
201            )?;
202        } else {
203            self.state.ast.to_json_file_without_keys(
204                self.output_directory.clone(),
205                &format!("{}.{file_suffix}", self.program_name.as_ref().unwrap()),
206                &["_span", "span"],
207            )?;
208        }
209        Ok(())
210    }
211
212    /// Writes the AST to a file (Leo syntax, not JSON).
213    fn write_ast(&self, file_suffix: &str) -> Result<()> {
214        let filename = format!("{}.{file_suffix}", self.program_name.as_ref().unwrap());
215        let full_filename = self.output_directory.join(&filename);
216        let contents = self.state.ast.ast.to_string();
217        fs::write(&full_filename, contents).map_err(|e| CompilerError::failed_ast_file(full_filename.display(), e))?;
218        Ok(())
219    }
220
221    /// Merge the imported stubs which are dependencies of the current program into the AST
222    /// in topological order.
223    pub fn add_import_stubs(&mut self) -> Result<()> {
224        let mut explored = IndexSet::<Symbol>::new();
225        let mut to_explore: Vec<Symbol> = self.state.ast.ast.imports.keys().cloned().collect();
226
227        while let Some(import) = to_explore.pop() {
228            explored.insert(import);
229            if let Some(stub) = self.import_stubs.get(&import) {
230                for new_import_id in stub.imports.iter() {
231                    if !explored.contains(&new_import_id.name.name) {
232                        to_explore.push(new_import_id.name.name);
233                    }
234                }
235            } else {
236                return Err(CompilerError::imported_program_not_found(
237                    self.program_name.as_ref().unwrap(),
238                    import,
239                    self.state.ast.ast.imports[&import].1,
240                )
241                .into());
242            }
243        }
244
245        // Iterate in the order of `import_stubs` to make sure they
246        // stay topologically sorted.
247        self.state.ast.ast.stubs = self
248            .import_stubs
249            .iter()
250            .filter(|(symbol, _stub)| explored.contains(*symbol))
251            .map(|(symbol, stub)| (*symbol, stub.clone()))
252            .collect();
253        Ok(())
254    }
255}