1818#include " location_update_visitor.h"
1919#include " map_visit.h"
2020
21- bool eval_index (
21+ struct eval_index_resultt
22+ {
23+ bool is_good;
24+ mp_integer value;
25+ };
26+
27+ static eval_index_resultt eval_index (
2228 const exprt &index,
2329 const abstract_environmentt &env,
24- const namespacet &ns,
25- mp_integer &out_index);
30+ const namespacet &ns);
2631
2732template <typename index_fn>
2833abstract_object_pointert apply_to_index_range (
@@ -205,11 +210,11 @@ abstract_object_pointert full_array_abstract_objectt::read_element(
205210 const namespacet &ns) const
206211{
207212 PRECONDITION (!is_bottom ());
208- mp_integer index_value ;
209- if (eval_index (expr, env, ns, index_value) )
213+ auto index = eval_index (expr, env, ns) ;
214+ if (index. is_good )
210215 {
211216 // Here we are assuming it is always in bounds
212- auto const value = map.find (index_value );
217+ auto const value = map.find (index. value );
213218 if (value.has_value ())
214219 return value.value ();
215220 return get_top_entry (env, ns);
@@ -266,25 +271,24 @@ abstract_object_pointert full_array_abstract_objectt::write_sub_element(
266271 const auto &result =
267272 std::dynamic_pointer_cast<full_array_abstract_objectt>(mutable_clone ());
268273
269- mp_integer index_value;
270- bool good_index = eval_index (expr, environment, ns, index_value);
274+ auto index = eval_index (expr, environment, ns);
271275
272- if (good_index )
276+ if (index. is_good )
273277 {
274278 // We were able to evaluate the index to a value, which we
275279 // assume is in bounds...
276- auto const old_value = map.find (index_value );
280+ auto const old_value = map.find (index. value );
277281
278282 if (old_value.has_value ())
279283 {
280284 result->map .replace (
281- index_value ,
285+ index. value ,
282286 environment.write (old_value.value (), value, stack, ns, merging_write));
283287 }
284288 else
285289 {
286290 result->map .insert (
287- index_value ,
291+ index. value ,
288292 environment.write (
289293 get_top_entry (environment, ns), value, stack, ns, merging_write));
290294 }
@@ -321,10 +325,9 @@ abstract_object_pointert full_array_abstract_objectt::write_leaf_element(
321325 const auto &result =
322326 std::dynamic_pointer_cast<full_array_abstract_objectt>(mutable_clone ());
323327
324- mp_integer index_value;
325- bool good_index = eval_index (expr, environment, ns, index_value);
328+ auto index = eval_index (expr, environment, ns);
326329
327- if (good_index )
330+ if (index. is_good )
328331 {
329332 // We were able to evaluate the index expression to a constant
330333 if (merging_write)
@@ -337,11 +340,11 @@ abstract_object_pointert full_array_abstract_objectt::write_leaf_element(
337340
338341 INVARIANT (!result->map .empty (), " If not top, map cannot be empty" );
339342
340- auto old_value = result->map .find (index_value );
343+ auto old_value = result->map .find (index. value );
341344 if (old_value.has_value ()) // if not found it's top, so nothing to merge
342345 {
343346 result->map .replace (
344- index_value ,
347+ index. value ,
345348 abstract_objectt::merge (old_value.value (), value, widen_modet::no)
346349 .object );
347350 }
@@ -351,12 +354,12 @@ abstract_object_pointert full_array_abstract_objectt::write_leaf_element(
351354 }
352355 else
353356 {
354- auto old_value = result->map .find (index_value );
357+ auto old_value = result->map .find (index. value );
355358
356359 if (old_value.has_value ())
357- result->map .replace (index_value , value);
360+ result->map .replace (index. value , value);
358361 else
359- result->map .insert (index_value , value);
362+ result->map .insert (index. value , value);
360363
361364 result->set_not_top ();
362365 DATA_INVARIANT (result->verify (), " Structural invariants maintained" );
@@ -438,20 +441,20 @@ void full_array_abstract_objectt::statistics(
438441 statistics.objects_memory_usage += memory_sizet::from_bytes (sizeof (*this ));
439442}
440443
441- bool eval_index (
444+ static eval_index_resultt eval_index (
442445 const exprt &expr,
443446 const abstract_environmentt &env,
444- const namespacet &ns,
445- mp_integer &out_index)
447+ const namespacet &ns)
446448{
447- const index_exprt &index = to_index_expr (expr);
448- abstract_object_pointert index_abstract_object = env.eval (index .index (), ns);
449- exprt value = index_abstract_object->to_constant ();
449+ const auto &index_expr = to_index_expr (expr);
450+ auto index_abstract_object = env.eval (index_expr .index (), ns);
451+ auto value = index_abstract_object->to_constant ();
450452
451453 if (!value.is_constant ())
452- return false ;
454+ return {false };
455+
456+ mp_integer out_index;
457+ bool result = to_integer (to_constant_expr (value), out_index);
453458
454- constant_exprt constant_index = to_constant_expr (value);
455- bool result = to_integer (constant_index, out_index);
456- return !result;
459+ return {!result, out_index};
457460}
0 commit comments