pub struct Compiler {
pub unit_name: Option<String>,
pub statements_before_dce: u32,
pub statements_after_dce: u32,
/* private fields */
}Expand description
The primary entry point of the Leo compiler.
Fields§
§unit_name: Option<String>The name of the compilation unit (program or library).
statements_before_dce: u32How many statements were in the AST before DCE?
statements_after_dce: u32How many statements were in the AST after DCE?
Implementations§
Source§impl Compiler
impl Compiler
Sourcepub fn network(&self) -> NetworkName
pub fn network(&self) -> NetworkName
Return the network selected for this compiler instance.
Sourcepub fn parse_program(
&mut self,
source: &str,
filename: FileName,
modules: &[(&str, FileName)],
) -> Result<()>
pub fn parse_program( &mut self, source: &str, filename: FileName, modules: &[(&str, FileName)], ) -> Result<()>
Parses the given source into a program AST and stores it in the compiler state.
The source file and any provided module sources are first registered in the session source map so spans can be resolved correctly. The parser then constructs the program AST from the main source and its modules.
After parsing, this verifies that the program scope name matches the expected
program name (from program.json or the test filename). The resulting AST is
stored in self.state.ast, and optionally written to disk if configured.
Sourcepub fn parse_and_return_program(
&mut self,
source: &str,
filename: FileName,
modules: &[(&str, FileName)],
) -> Result<Program>
pub fn parse_and_return_program( &mut self, source: &str, filename: FileName, modules: &[(&str, FileName)], ) -> Result<Program>
Simple wrapper around parse_program that also returns a program AST.
Sourcepub fn parse_and_return_library(
&mut self,
library_name: &str,
source: &str,
filename: FileName,
modules: &[(&str, FileName)],
) -> Result<Library>
pub fn parse_and_return_library( &mut self, library_name: &str, source: &str, filename: FileName, modules: &[(&str, FileName)], ) -> Result<Library>
Simple wrapper around parse_library that also returns a library AST.
Sourcepub fn parse_library(
&mut self,
library_name: Symbol,
source: &str,
filename: FileName,
modules: &[(&str, FileName)],
) -> Result<()>
pub fn parse_library( &mut self, library_name: Symbol, source: &str, filename: FileName, modules: &[(&str, FileName)], ) -> Result<()>
Parses a library source (and its submodules) into a library AST.
All source strings are registered in the session source map so span information
can be resolved correctly. The resulting AST is stored in self.state.ast.
Sourcepub fn analyze_frontend_from_directory_with_file_source(
&mut self,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
file_source: &impl FileSource,
) -> Result<FrontendAnalysis<'_>>
pub fn analyze_frontend_from_directory_with_file_source( &mut self, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, file_source: &impl FileSource, ) -> Result<FrontendAnalysis<'_>>
Parses a package entry file, merges import stubs when applicable, and runs frontend passes.
Unlike the full compile pipeline, this stops after semantic frontend analysis and returns borrowed access to the AST, symbol table, and type table. The LSP uses this to build semantic indices without running code generation or writing artifacts to disk.
Sourcepub fn analyze_frontend_from_directory_with_file_source_and_check<C>(
&mut self,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
file_source: &impl FileSource,
should_continue: C,
) -> Result<FrontendAnalysis<'_>>
pub fn analyze_frontend_from_directory_with_file_source_and_check<C>( &mut self, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, file_source: &impl FileSource, should_continue: C, ) -> Result<FrontendAnalysis<'_>>
Equivalent to Self::analyze_frontend_from_directory_with_file_source, but checks
should_continue at parse and pass boundaries so editor tooling can abandon
stale work before completing the entire frontend pipeline.
Sourcepub fn new(
expected_unit_name: Option<String>,
is_test: bool,
handler: Handler,
node_builder: Rc<NodeBuilder>,
output_directory: PathBuf,
compiler_options: Option<CompilerOptions>,
import_stubs: IndexMap<Symbol, Stub>,
network: NetworkName,
) -> Self
pub fn new( expected_unit_name: Option<String>, is_test: bool, handler: Handler, node_builder: Rc<NodeBuilder>, output_directory: PathBuf, compiler_options: Option<CompilerOptions>, import_stubs: IndexMap<Symbol, Stub>, network: NetworkName, ) -> Self
Returns a new Leo compiler.
Sourcepub fn do_pass<P: Pass>(&mut self, input: P::Input) -> Result<P::Output>
pub fn do_pass<P: Pass>(&mut self, input: P::Input) -> Result<P::Output>
Run a compiler pass without an external cancellation check.
Sourcepub fn frontend_passes(&mut self) -> Result<()>
pub fn frontend_passes(&mut self) -> Result<()>
Runs all frontend passes: NameValidation through StaticAnalyzing.
Sourcepub fn frontend_passes_with_check<C>(
&mut self,
should_continue: C,
) -> Result<()>
pub fn frontend_passes_with_check<C>( &mut self, should_continue: C, ) -> Result<()>
Runs all frontend passes while checking whether the caller still wants the result.
Sourcepub fn intermediate_passes(
&mut self,
) -> Result<(Program, IndexMap<String, Program>, Vec<CompiledInterface>)>
pub fn intermediate_passes( &mut self, ) -> Result<(Program, IndexMap<String, Program>, Vec<CompiledInterface>)>
Runs the compiler stages.
Returns the generated ABIs (primary and imports), which are captured immediately after monomorphisation to ensure all types are resolved, but not yet lowered.
Sourcepub fn generate_library_interface_abis(&self) -> Vec<CompiledInterface>
pub fn generate_library_interface_abis(&self) -> Vec<CompiledInterface>
Generates interface ABIs for a validated library.
Must be called after build_library() since it reads the resolved AST.
Sourcepub fn compile(
&mut self,
source: &str,
filename: FileName,
modules: &Vec<(&str, FileName)>,
) -> Result<Compiled>
pub fn compile( &mut self, source: &str, filename: FileName, modules: &Vec<(&str, FileName)>, ) -> Result<Compiled>
Compiles a program from a given source string and a list of module sources.
§Arguments
source- The main source code as a string slice.filename- The name of the main source file.modules- A vector of tuples where each tuple contains:- A module source as a string slice.
- Its associated
FileName.
§Returns
Ok(CompiledPrograms)containing the generated bytecode and ABI if compilation succeeds.Err(CompilerError)if any stage of the pipeline fails.
Sourcepub fn compile_from_directory(
&mut self,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
) -> Result<Compiled>
pub fn compile_from_directory( &mut self, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, ) -> Result<Compiled>
Compiles a program from a source file and its associated module files in the same directory tree.
Sourcepub fn compile_from_directory_with_file_source(
&mut self,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
file_source: &impl FileSource,
) -> Result<Compiled>
pub fn compile_from_directory_with_file_source( &mut self, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, file_source: &impl FileSource, ) -> Result<Compiled>
Compiles a program from a source file using the given file source.
Sourcepub fn parse_program_from_directory(
&mut self,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
) -> Result<Program>
pub fn parse_program_from_directory( &mut self, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, ) -> Result<Program>
Parses a program from a source file and its associated module files in the same directory tree.
Sourcepub fn parse_program_from_directory_with_file_source(
&mut self,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
file_source: &impl FileSource,
) -> Result<Program>
pub fn parse_program_from_directory_with_file_source( &mut self, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, file_source: &impl FileSource, ) -> Result<Program>
Parses a program from a source file using the given file source.
Sourcepub fn parse_library_from_directory(
&mut self,
library_name: Symbol,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
) -> Result<Library>
pub fn parse_library_from_directory( &mut self, library_name: Symbol, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, ) -> Result<Library>
Parses a program from a source file and its associated module files in the same directory tree.
Sourcepub fn parse_library_from_directory_with_file_source(
&mut self,
library_name: Symbol,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
file_source: &impl FileSource,
) -> Result<Library>
pub fn parse_library_from_directory_with_file_source( &mut self, library_name: Symbol, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, file_source: &impl FileSource, ) -> Result<Library>
Parses a library from a source file.
Sourcepub fn add_import_stubs(&mut self) -> Result<()>
pub fn add_import_stubs(&mut self) -> Result<()>
Resolves and registers all import stubs for the current program.
This method performs a graph traversal over the program’s import relationships to:
- Establish parent–child relationships between stubs based on imports.
- Collect all reachable stubs in traversal order.
- Store the explored stubs back into
self.state.ast.ast.stubs.
The traversal starts from the imports of the main program and recursively follows their transitive dependencies. Any missing stub during traversal results in an error.
§Returns
Ok(())if all imports are successfully resolved and stubs are collected.Err(CompilerError)if any imported program cannot be found.
Sourcepub fn build_library(
&mut self,
library_name: Symbol,
source: &str,
filename: FileName,
modules: &[(&str, FileName)],
) -> Result<Library>
pub fn build_library( &mut self, library_name: Symbol, source: &str, filename: FileName, modules: &[(&str, FileName)], ) -> Result<Library>
Builds a library: parses the source, resolves import stubs, and runs all frontend passes.
Unlike Self::compile, this does not run monomorphisation, lowerings, or code generation.
No bytecode is produced. Returns the validated library AST, which callers can convert into
a Stub for downstream units in the same build graph.
Sourcepub fn build_library_from_directory(
&mut self,
library_name: Symbol,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
) -> Result<Library>
pub fn build_library_from_directory( &mut self, library_name: Symbol, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, ) -> Result<Library>
Builds a library from a source file and its associated module files in the same directory tree.
Sourcepub fn build_library_from_directory_with_file_source(
&mut self,
library_name: Symbol,
entry_file_path: impl AsRef<Path>,
source_directory: impl AsRef<Path>,
file_source: &impl FileSource,
) -> Result<Library>
pub fn build_library_from_directory_with_file_source( &mut self, library_name: Symbol, entry_file_path: impl AsRef<Path>, source_directory: impl AsRef<Path>, file_source: &impl FileSource, ) -> Result<Library>
Builds a library from a source file using the given file source.
Auto Trait Implementations§
impl !Freeze for Compiler
impl !RefUnwindSafe for Compiler
impl !Send for Compiler
impl !Sync for Compiler
impl Unpin for Compiler
impl UnsafeUnpin for Compiler
impl !UnwindSafe for Compiler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);