calyx_opt/traversal/
diagnostics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use calyx_utils::{CalyxResult, Error};

use super::{Action, VisResult};

/// A pass that implements reporting Diagnostics
pub trait DiagnosticPass {
    /// Return an iterator of the diagnostics gathered by this pass.
    fn diagnostics(&self) -> &DiagnosticContext;
}

/// A type for accumulating multiple errors
#[derive(Default, Debug)]
pub struct DiagnosticContext {
    errors: Vec<Error>,
    warnings: Vec<Error>,
}

impl DiagnosticContext {
    /// Report an `error`
    pub fn err(&mut self, error: Error) {
        self.errors.push(error);
    }

    /// Report a `warning`
    pub fn warning(&mut self, warning: Error) {
        self.warnings.push(warning)
    }

    /// Accumulates `error` into the context, and returns `Ok(Action::Continue)`.
    /// This is useful for when we need to raise an Error because we couldn't
    /// construct some value that we needed to continue the computation.
    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()
    }
}

/// Accumuate the error in a [`Result`] type into the [`DiagnosticContext`].
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)
            }
        }
    }
}