Skip to main content

litex/error/
error.rs

1use crate::prelude::*;
2use std::fmt;
3
4#[derive(Debug)]
5pub enum RuntimeError {
6    ArithmeticError(RuntimeErrorStruct),
7    NewFactError(RuntimeErrorStruct),
8    StoreFactError(RuntimeErrorStruct),
9    ParseError(RuntimeErrorStruct),
10    ExecStmtError(RuntimeErrorStruct),
11    WellDefinedError(RuntimeErrorStruct),
12    VerifyError(RuntimeErrorStruct),
13    UnknownError(RuntimeErrorStruct),
14    InferError(RuntimeErrorStruct),
15    NameAlreadyUsedError(RuntimeErrorStruct),
16    DefineParamsError(RuntimeErrorStruct),
17    InstantiateError(RuntimeErrorStruct),
18}
19
20#[derive(Debug)]
21pub struct RuntimeErrorStruct {
22    pub statement: Option<Stmt>,
23    pub msg: String,
24    pub line_file: LineFile,
25    pub previous_error: Option<Box<RuntimeError>>,
26    pub inside_results: Vec<StmtResult>,
27}
28
29#[derive(Debug)]
30pub struct ArithmeticRuntimeError(pub RuntimeErrorStruct);
31
32impl From<ArithmeticRuntimeError> for RuntimeError {
33    fn from(w: ArithmeticRuntimeError) -> Self {
34        RuntimeError::ArithmeticError(w.0)
35    }
36}
37
38#[derive(Debug)]
39pub struct NewFactRuntimeError(pub RuntimeErrorStruct);
40
41impl From<NewFactRuntimeError> for RuntimeError {
42    fn from(w: NewFactRuntimeError) -> Self {
43        RuntimeError::NewFactError(w.0)
44    }
45}
46
47#[derive(Debug)]
48pub struct StoreFactRuntimeError(pub RuntimeErrorStruct);
49
50impl From<StoreFactRuntimeError> for RuntimeError {
51    fn from(w: StoreFactRuntimeError) -> Self {
52        RuntimeError::StoreFactError(w.0)
53    }
54}
55
56#[derive(Debug)]
57pub struct ParseRuntimeError(pub RuntimeErrorStruct);
58
59impl From<ParseRuntimeError> for RuntimeError {
60    fn from(w: ParseRuntimeError) -> Self {
61        RuntimeError::ParseError(w.0)
62    }
63}
64
65#[derive(Debug)]
66pub struct WellDefinedRuntimeError(pub RuntimeErrorStruct);
67
68impl From<WellDefinedRuntimeError> for RuntimeError {
69    fn from(w: WellDefinedRuntimeError) -> Self {
70        RuntimeError::WellDefinedError(w.0)
71    }
72}
73
74#[derive(Debug)]
75pub struct VerifyRuntimeError(pub RuntimeErrorStruct);
76
77impl From<VerifyRuntimeError> for RuntimeError {
78    fn from(w: VerifyRuntimeError) -> Self {
79        RuntimeError::VerifyError(w.0)
80    }
81}
82
83#[derive(Debug)]
84pub struct UnknownRuntimeError(pub RuntimeErrorStruct);
85
86impl From<UnknownRuntimeError> for RuntimeError {
87    fn from(w: UnknownRuntimeError) -> Self {
88        RuntimeError::UnknownError(w.0)
89    }
90}
91
92#[derive(Debug)]
93pub struct InferRuntimeError(pub RuntimeErrorStruct);
94
95impl From<InferRuntimeError> for RuntimeError {
96    fn from(w: InferRuntimeError) -> Self {
97        RuntimeError::InferError(w.0)
98    }
99}
100
101#[derive(Debug)]
102pub struct NameAlreadyUsedRuntimeError(pub RuntimeErrorStruct);
103
104impl From<NameAlreadyUsedRuntimeError> for RuntimeError {
105    fn from(w: NameAlreadyUsedRuntimeError) -> Self {
106        RuntimeError::NameAlreadyUsedError(w.0)
107    }
108}
109
110#[derive(Debug)]
111pub struct DefineParamsRuntimeError(pub RuntimeErrorStruct);
112
113impl From<DefineParamsRuntimeError> for RuntimeError {
114    fn from(w: DefineParamsRuntimeError) -> Self {
115        RuntimeError::DefineParamsError(w.0)
116    }
117}
118
119#[derive(Debug)]
120pub struct InstantiateRuntimeError(pub RuntimeErrorStruct);
121
122impl From<InstantiateRuntimeError> for RuntimeError {
123    fn from(w: InstantiateRuntimeError) -> Self {
124        RuntimeError::InstantiateError(w.0)
125    }
126}
127
128impl RuntimeErrorStruct {
129    pub fn new(
130        statement: Option<Stmt>,
131        msg: String,
132        line_file: LineFile,
133        previous_error: Option<RuntimeError>,
134        inside_results: Vec<StmtResult>,
135    ) -> Self {
136        RuntimeErrorStruct {
137            statement,
138            msg,
139            line_file,
140            previous_error: previous_error.map(Box::new),
141            inside_results,
142        }
143    }
144}
145
146pub fn short_exec_error(
147    stmt: Stmt,
148    message: impl Into<String>,
149    cause: Option<RuntimeError>,
150    inside_results: Vec<StmtResult>,
151) -> RuntimeError {
152    let message = message.into();
153    let line_file = stmt.line_file();
154    RuntimeError::ExecStmtError(RuntimeErrorStruct::new(
155        Some(stmt.clone()),
156        message,
157        line_file.clone(),
158        cause,
159        inside_results,
160    ))
161}
162
163pub fn exec_stmt_error_with_stmt_and_cause(stmt: Stmt, cause: RuntimeError) -> RuntimeError {
164    RuntimeError::ExecStmtError(RuntimeErrorStruct::new(
165        Some(stmt.clone()),
166        String::new(),
167        stmt.line_file(),
168        Some(cause),
169        vec![],
170    ))
171}
172
173impl std::error::Error for RuntimeError {}
174
175impl RuntimeError {
176    pub fn wrap_new_fact_as_store_conflict(e: RuntimeError) -> RuntimeError {
177        match e {
178            RuntimeError::NewFactError(s) => NewFactRuntimeError(RuntimeErrorStruct::new(
179                s.statement.clone(),
180                s.msg.clone(),
181                s.line_file.clone(),
182                Some(NewFactRuntimeError(s).into()),
183                vec![],
184            ))
185            .into(),
186            _ => e,
187        }
188    }
189
190    pub fn line_file(&self) -> LineFile {
191        match self {
192            RuntimeError::ArithmeticError(e) => e.line_file.clone(),
193            RuntimeError::NewFactError(e) => e.line_file.clone(),
194            RuntimeError::StoreFactError(e) => e.line_file.clone(),
195            RuntimeError::ParseError(e) => e.line_file.clone(),
196            RuntimeError::ExecStmtError(e) => e.line_file.clone(),
197            RuntimeError::WellDefinedError(e) => e.line_file.clone(),
198            RuntimeError::VerifyError(e) => e.line_file.clone(),
199            RuntimeError::UnknownError(e) => e.line_file.clone(),
200            RuntimeError::InferError(e) => e.line_file.clone(),
201            RuntimeError::NameAlreadyUsedError(e) => e.line_file.clone(),
202            RuntimeError::DefineParamsError(e) => e.line_file.clone(),
203            RuntimeError::InstantiateError(e) => e.line_file.clone(),
204        }
205    }
206
207    pub fn display_label(&self) -> &'static str {
208        match self {
209            RuntimeError::ArithmeticError(_) => "ArithmeticError",
210            RuntimeError::NewFactError(_) => "NewFactError",
211            RuntimeError::StoreFactError(_) => "StoreFactError",
212            RuntimeError::ParseError(_) => "ParseError",
213            RuntimeError::ExecStmtError(_) => "ExecStmtError",
214            RuntimeError::WellDefinedError(_) => "WellDefinedError",
215            RuntimeError::VerifyError(_) => "VerifyError",
216            RuntimeError::UnknownError(_) => "UnknownError",
217            RuntimeError::InferError(_) => "InferError",
218            RuntimeError::NameAlreadyUsedError(_) => "NameAlreadyUsedError",
219            RuntimeError::DefineParamsError(_) => "DefineParamsError",
220            RuntimeError::InstantiateError(_) => "InstantiateError",
221        }
222    }
223}
224
225// Display outputs a short placeholder; JSON: `display_runtime_error_json` in `crate::pipeline`.
226impl fmt::Display for RuntimeError {
227    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228        write!(f, "{}", "error")
229    }
230}
231
232impl fmt::Display for RuntimeErrorStruct {
233    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234        write!(f, "{}", self.msg)
235    }
236}
237
238impl std::error::Error for RuntimeErrorStruct {}
239
240impl RuntimeErrorStruct {
241    pub fn new_with_just_msg(msg: String) -> Self {
242        Self::new(None, msg, default_line_file(), None, vec![])
243    }
244
245    pub fn new_with_msg_and_line_file(msg: String, line_file: LineFile) -> Self {
246        Self::new(None, msg, line_file, None, vec![])
247    }
248
249    pub fn new_with_msg_and_cause(msg: String, cause: RuntimeError) -> Self {
250        Self::new(None, msg, default_line_file(), Some(cause), vec![])
251    }
252}