@@ -61,16 +61,17 @@ bvt boolbvt::convert_index(const index_exprt &expr)
61
61
}
62
62
63
63
// Must have a finite size
64
- mp_integer array_size;
65
- if (to_integer (array_type.size (), array_size))
66
- throw " failed to convert array size" ;
67
-
68
- // see if the index address is constant
69
- // many of these are compacted by simplify_expr
70
- // but variable location writes will block this
71
- mp_integer index_value;
72
- if (!to_integer (index , index_value))
73
- return convert_index (array, index_value);
64
+ mp_integer array_size = numeric_cast_v<mp_integer>(array_type.size ());
65
+ {
66
+ // see if the index address is constant
67
+ // many of these are compacted by simplify_expr
68
+ // but variable location writes will block this
69
+ auto maybe_index_value = numeric_cast<mp_integer>(index );
70
+ if (maybe_index_value.has_value ())
71
+ {
72
+ return convert_index (array, maybe_index_value.value ());
73
+ }
74
+ }
74
75
75
76
// Special case : arrays of one thing (useful for constants)
76
77
// TODO : merge with ACTUAL_ARRAY_HACK so that ranges of the same
@@ -115,12 +116,10 @@ bvt boolbvt::convert_index(const index_exprt &expr)
115
116
116
117
binary_relation_exprt lower_bound (
117
118
from_integer (0 , index .type ()), ID_le, index );
119
+ CHECK_RETURN (lower_bound.lhs ().is_not_nil ());
118
120
binary_relation_exprt upper_bound (
119
121
index , ID_lt, from_integer (array_size, index .type ()));
120
-
121
- if (lower_bound.lhs ().is_nil () ||
122
- upper_bound.rhs ().is_nil ())
123
- throw " number conversion failed (2)" ;
122
+ CHECK_RETURN (upper_bound.rhs ().is_not_nil ());
124
123
125
124
and_exprt range_condition (lower_bound, upper_bound);
126
125
implies_exprt implication (range_condition, value_equality);
@@ -172,11 +171,12 @@ bvt boolbvt::convert_index(const index_exprt &expr)
172
171
for (mp_integer i=0 ; i<array_size; i=i+1 )
173
172
{
174
173
index_equality.rhs ()=from_integer (i, index_equality.lhs ().type ());
174
+ CHECK_RETURN (index_equality.rhs ().is_not_nil ());
175
175
176
- if (index_equality. rhs (). is_nil ())
177
- throw " number conversion failed (1) " ;
178
-
179
- assert (it != array. operands (). end () );
176
+ INVARIANT (
177
+ it != array. operands (). end (),
178
+ " this loop iterates over the array, so `it` shouldn't be increased "
179
+ " past the array's end" );
180
180
181
181
value_equality.rhs ()=*it++;
182
182
@@ -198,10 +198,8 @@ bvt boolbvt::convert_index(const index_exprt &expr)
198
198
199
199
// get literals for the whole array
200
200
201
- const bvt &array_bv=convert_bv (array);
202
-
203
- if (array_size*width!=array_bv.size ())
204
- throw " unexpected array size" ;
201
+ const bvt &array_bv =
202
+ convert_bv (array, numeric_cast_v<std::size_t >(array_size * width));
205
203
206
204
// TODO: maybe a shifter-like construction would be better
207
205
// Would be a lot more compact but propagate worse
@@ -229,9 +227,7 @@ bvt boolbvt::convert_index(const index_exprt &expr)
229
227
for (mp_integer i=0 ; i<array_size; i=i+1 )
230
228
{
231
229
index_equality.rhs ()=from_integer (i, index_equality.lhs ().type ());
232
-
233
- if (index_equality.rhs ().is_nil ())
234
- throw " number conversion failed (1)" ;
230
+ CHECK_RETURN (index_equality.rhs ().is_not_nil ());
235
231
236
232
mp_integer offset=i*width;
237
233
@@ -256,7 +252,9 @@ bvt boolbvt::convert_index(const index_exprt &expr)
256
252
257
253
typet constant_type=index .type (); // type of index operand
258
254
259
- assert (array_size>0 );
255
+ DATA_INVARIANT (
256
+ array_size > 0 ,
257
+ " non-positive array sizes are forbidden in goto programs" );
260
258
261
259
for (mp_integer i=0 ; i<array_size; i=i+1 )
262
260
{
0 commit comments