calyx_backend/traits.rs
1//! Interface for a Calyx backend.
2use calyx_ir as ir;
3use calyx_utils::{CalyxResult, OutputFile};
4
5/// A backend for Calyx.
6pub trait Backend {
7 /// The name of this backend.
8 fn name(&self) -> &'static str;
9 /// Validate this program for emitting using this backend. Returns an
10 /// Err(..) if the program has unexpected constructs.
11 fn validate(prog: &ir::Context) -> CalyxResult<()>;
12 /// Transforms the program into a formatted string representing a valid
13 /// and write it to `write`.
14 fn emit(prog: &ir::Context, write: &mut OutputFile) -> CalyxResult<()>;
15 /// Link the extern collected while parsing the program.
16 fn link_externs(
17 prog: &ir::Context,
18 write: &mut OutputFile,
19 ) -> CalyxResult<()>;
20 /// Convience function to validate and emit the program.
21 fn run(&self, prog: ir::Context, mut file: OutputFile) -> CalyxResult<()> {
22 Self::validate(&prog)?;
23 Self::link_externs(&prog, &mut file)?;
24 Self::emit(&prog, &mut file)
25 }
26}