calyx_opt/traversal/
diagnostics.rsuse calyx_utils::{CalyxResult, Error};
use super::{Action, VisResult};
pub trait DiagnosticPass {
fn diagnostics(&self) -> &DiagnosticContext;
}
#[derive(Default, Debug)]
pub struct DiagnosticContext {
errors: Vec<Error>,
warnings: Vec<Error>,
}
impl DiagnosticContext {
pub fn err(&mut self, error: Error) {
self.errors.push(error);
}
pub fn warning(&mut self, warning: Error) {
self.warnings.push(warning)
}
pub fn early_return_err(&mut self, error: Error) -> VisResult {
self.err(error);
Ok(Action::Continue)
}
pub fn warning_iter(&self) -> impl Iterator<Item = &Error> {
self.warnings.iter()
}
pub fn errors_iter(&self) -> impl Iterator<Item = &Error> {
self.errors.iter()
}
}
pub trait DiagnosticResult {
fn accumulate_err(self, diag: &mut DiagnosticContext) -> Self;
}
impl<T> DiagnosticResult for CalyxResult<T>
where
T: Default,
{
fn accumulate_err(self, diag: &mut DiagnosticContext) -> Self {
match self {
Ok(act) => Ok(act),
Err(err) => {
diag.err(err);
Ok(T::default())
}
}
}
}
impl DiagnosticResult for VisResult {
fn accumulate_err(self, diag: &mut DiagnosticContext) -> Self {
match self {
Ok(act) => Ok(act),
Err(err) => {
diag.err(err);
Ok(Action::Continue)
}
}
}
}