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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use bitwidth::BitWidth;
use bitpos::BitPos;
use radix::Radix;
use apint::{ApInt, ShiftAmount};
use apint::{PrimitiveTy};
use std::result;
use std::error;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ErrorKind {
InvalidRadix(u8),
InvalidStringRepr{
input: String,
radix: Radix,
pos_char: Option<(usize, char)>
},
InvalidBitAccess{
pos: BitPos,
width: BitWidth
},
InvalidShiftAmount{
shift_amount: ShiftAmount,
width: BitWidth
},
ValueUnrepresentable{
value: ApInt,
destination_ty: PrimitiveTy
},
UnmatchingBitwidth(BitWidth, BitWidth),
InvalidBitWidth(usize),
TruncationBitWidthTooLarge{
target: BitWidth,
current: BitWidth
},
ExtensionBitWidthTooSmall{
target: BitWidth,
current: BitWidth
},
ExpectedNonEmptyDigits,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Error{
kind : ErrorKind,
message : String,
annotation: Option<String>
}
impl Error {
#[inline]
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
#[inline]
pub fn message(&self) -> &str {
self.message.as_str()
}
#[inline]
pub fn annotation(&self) -> Option<&str> {
match self.annotation {
Some(ref ann) => Some(ann.as_str()),
None => None
}
}
}
impl Error {
#[inline]
pub(crate) fn with_annotation<A>(mut self, annotation: A) -> Error
where A: Into<String>
{
self.annotation = Some(annotation.into());
self
}
}
impl Error {
pub(crate) fn invalid_radix(val: u8) -> Error {
Error{
kind: ErrorKind::InvalidRadix(val),
message: format!("Encountered an invalid parsing radix of {:?}.", val),
annotation: None
}
}
pub(crate) fn invalid_string_repr<S>(input: S, radix: Radix) -> Error
where S: Into<String>
{
let input = input.into();
Error{
kind: ErrorKind::InvalidStringRepr{input, radix, pos_char: None},
message: format!("Encountered an invalid string representation for the given radix (= {:?}).", radix),
annotation: None
}
}
pub(crate) fn invalid_char_in_string_repr<S>(input: S, radix: Radix, pos: usize, ch: char) -> Error
where S: Into<String>
{
let input = input.into();
Error{
kind: ErrorKind::InvalidStringRepr{input, radix, pos_char: None},
message: format!("Encountered an invalid character (= '{:?}') at position {:?} within the given string \
representation for the given radix (= {:?}).", ch, pos, radix),
annotation: None
}
}
pub(crate) fn invalid_bitwidth(val: usize) -> Error {
Error{
kind: ErrorKind::InvalidBitWidth(val),
message: format!("Encountered invalid bitwidth of {:?}.", val),
annotation: None
}
}
pub(crate) fn invalid_zero_bitwidth() -> Error {
Error::invalid_bitwidth(0)
}
pub(crate) fn extension_bitwidth_too_small<W1, W2>(target: W1, current: W2) -> Error
where W1: Into<BitWidth>,
W2: Into<BitWidth>
{
let target = target.into();
let current = current.into();
Error{
kind: ErrorKind::ExtensionBitWidthTooSmall{target, current},
message: format!("Tried to extend an `ApInt` with a width of {:?} to a smaller target width of {:?}", current, target),
annotation: None
}
}
pub(crate) fn truncation_bitwidth_too_large<W1, W2>(target: W1, current: W2) -> Error
where W1: Into<BitWidth>,
W2: Into<BitWidth>
{
let target = target.into();
let current = current.into();
Error{
kind: ErrorKind::TruncationBitWidthTooLarge{target, current},
message: format!("Tried to truncate an `ApInt` with a width of {:?} to a larger target width of {:?}", current, target),
annotation: None
}
}
pub(crate) fn unmatching_bitwidths<W1, W2>(lhs: W1, rhs: W2) -> Error
where W1: Into<BitWidth>,
W2: Into<BitWidth>
{
let lhs = lhs.into();
let rhs = rhs.into();
Error{
kind: ErrorKind::UnmatchingBitwidth(lhs, rhs),
message: format!("Encountered invalid operation on entities with non-matching bit-widths of {:?} and {:?}.", lhs, rhs),
annotation: None
}
}
pub(crate) fn invalid_shift_amount<S, W>(shift_amount: S, width: W) -> Error
where S: Into<ShiftAmount>,
W: Into<BitWidth>
{
let shift_amount = shift_amount.into();
let width = width.into();
Error{
kind: ErrorKind::InvalidShiftAmount{shift_amount, width},
message: format!("Encountered invalid shift amount of {:?} on bit-width with {:?} bits.", shift_amount, width),
annotation: None
}
}
pub(crate) fn invalid_bit_access<P, W>(pos: P, width: W) -> Error
where P: Into<BitPos>,
W: Into<BitWidth>
{
let pos = pos.into();
let width = width.into();
Error{
kind: ErrorKind::InvalidBitAccess{pos, width},
message: format!("Encountered invalid bit access at position {:?} with a total bit-width of {:?}.", pos, width),
annotation: None
}
}
pub(crate) fn expected_non_empty_digits() -> Error {
Error{
kind: ErrorKind::ExpectedNonEmptyDigits,
message: "Encountered an empty iterator upon construction of an `ApInt` from a digit iterator.".to_owned(),
annotation: None
}
}
pub(crate) fn encountered_unrepresentable_value(
value: ApInt,
destination_ty: PrimitiveTy
)
-> Error
{
let message = format!(
"Encountered a value ({:?}) that is unrepresentable \
by the destination type {:?}.", value, destination_ty);
Error{
kind: ErrorKind::ValueUnrepresentable{value, destination_ty},
message,
annotation: None
}
}
}
impl<T> Into<Result<T>> for Error {
fn into(self) -> Result<T> {
Err(self)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<Self as fmt::Debug>::fmt(self, f)
}
}
impl error::Error for Error {
fn description(&self) -> &str {
self.message.as_str()
}
}
pub type Result<T> = result::Result<T, Error>;