@@ -54,14 +54,24 @@ optionalt<mp_integer> expr_cast<mp_integer>(const exprt& expr)
54
54
template <>
55
55
optionalt<std::size_t > expr_cast<std::size_t >(const exprt& expr)
56
56
{
57
- if (const auto tmp=expr_cast<mp_integer>(expr))
57
+ if (const auto tmp=expr_cast<mp_integer>(expr))
58
58
{
59
59
if (tmp->is_long () && *tmp >= 0 )
60
60
return tmp->to_long ();
61
61
}
62
62
return { };
63
63
}
64
64
65
+ template <>
66
+ optionalt<string_exprt> expr_cast<string_exprt>(const exprt& expr)
67
+ {
68
+ if (is_refined_string_type (expr.type ()))
69
+ {
70
+ return to_string_expr (expr);
71
+ }
72
+ return { };
73
+ }
74
+
65
75
template <typename T>
66
76
T expr_cast_v (const exprt& expr) {
67
77
const auto maybe=expr_cast<T>(expr);
@@ -343,29 +353,28 @@ bool string_refinementt::add_axioms_for_string_assigns(
343
353
// / last value that has been initialized.
344
354
void string_refinementt::concretize_string (const exprt &expr)
345
355
{
346
- if (is_refined_string_type (expr. type () ))
356
+ if (const auto str=expr_cast<string_exprt> (expr))
347
357
{
348
- const string_exprt str=to_string_expr (expr);
349
- const exprt length=get (str.length ());
350
- exprt content=str.content ();
358
+ const exprt length=get (str->length ());
359
+ exprt content=str->content ();
351
360
replace_expr (symbol_resolve, content);
352
361
found_length[content]=length;
353
362
const auto string_length=expr_cast_v<std::size_t >(length);
354
363
INVARIANT (
355
364
string_length<=generator.max_string_length ,
356
365
string_refinement_invariantt (" string length must be less than the max "
357
366
" length" ));
358
- if (index_set[str. content ()].empty ())
367
+ if (index_set[str-> content ()].empty ())
359
368
return ;
360
369
361
370
std::map<std::size_t , exprt> map;
362
371
363
- for (const auto &i : index_set[str. content ()])
372
+ for (const auto &i : index_set[str-> content ()])
364
373
{
365
374
const exprt simple_i=simplify_expr (get (i), ns);
366
375
if (const auto index=expr_cast<std::size_t >(simple_i))
367
376
{
368
- const exprt str_i=simplify_expr (str[simple_i], ns);
377
+ const exprt str_i=simplify_expr ((* str) [simple_i], ns);
369
378
const exprt value=simplify_expr (get (str_i), ns);
370
379
map.emplace (*index, value);
371
380
}
@@ -377,7 +386,7 @@ void string_refinementt::concretize_string(const exprt &expr)
377
386
}
378
387
array_exprt arr (to_array_type (content.type ()));
379
388
arr.operands ()=fill_in_map_as_vector (map);
380
- debug () << " Concretized " << from_expr (ns, " " , str. content ())
389
+ debug () << " Concretized " << from_expr (ns, " " , str-> content ())
381
390
<< " = " << from_expr (ns, " " , arr) << eom;
382
391
found_content[content]=arr;
383
392
}
@@ -399,24 +408,22 @@ void string_refinementt::concretize_results()
399
408
// / `found_length`
400
409
void string_refinementt::concretize_lengths ()
401
410
{
402
- for (const auto &it : symbol_resolve)
411
+ for (const auto &pair : symbol_resolve)
403
412
{
404
- if (is_refined_string_type (it .second . type () ))
413
+ if (const auto str=expr_cast<string_exprt>(pair .second ))
405
414
{
406
- string_exprt str=to_string_expr (it.second );
407
- exprt length=get (str.length ());
408
- exprt content=str.content ();
415
+ exprt length=get (str->length ());
416
+ exprt content=str->content ();
409
417
replace_expr (symbol_resolve, content);
410
418
found_length[content]=length;
411
419
}
412
420
}
413
421
for (const auto &it : generator.get_created_strings ())
414
422
{
415
- if (is_refined_string_type (it. type () ))
423
+ if (const auto str=expr_cast<string_exprt> (it))
416
424
{
417
- string_exprt str=to_string_expr (it);
418
- exprt length=get (str.length ());
419
- exprt content=str.content ();
425
+ exprt length=get (str->length ());
426
+ exprt content=str->content ();
420
427
replace_expr (symbol_resolve, content);
421
428
found_length[content]=length;
422
429
}
@@ -794,20 +801,19 @@ void string_refinementt::debug_model()
794
801
const std::string indent (" " );
795
802
for (auto it : symbol_resolve)
796
803
{
797
- if (is_refined_string_type (it.second . type () ))
804
+ if (const auto refined=expr_cast<string_exprt> (it.second ))
798
805
{
799
806
debug () << " - " << from_expr (ns, " " , to_symbol_expr (it.first )) << " :\n " ;
800
- string_exprt refined=to_string_expr (it.second );
801
807
debug () << indent << indent << " in_map: "
802
- << from_expr (ns, " " , refined) << eom;
808
+ << from_expr (ns, " " , * refined) << eom;
803
809
debug () << indent << indent << " resolved: "
804
- << from_expr (ns, " " , refined) << eom;
805
- const exprt &econtent=refined. content ();
806
- const exprt &elength=refined. length ();
810
+ << from_expr (ns, " " , * refined) << eom;
811
+ const exprt &econtent=refined-> content ();
812
+ const exprt &elength=refined-> length ();
807
813
808
814
exprt len=supert::get (elength);
809
815
len=simplify_expr (len, ns);
810
- exprt arr=get_array (econtent, len);
816
+ const exprt arr=get_array (econtent, len);
811
817
if (arr.id ()==ID_array)
812
818
debug () << indent << indent << " as_string: \" "
813
819
<< string_of_array (to_array_expr (arr)) << " \"\n " ;
@@ -1680,11 +1686,11 @@ std::vector<exprt> string_refinementt::instantiate_not_contains(
1680
1686
// / \return an expression containing no array-list
1681
1687
exprt substitute_array_lists (exprt expr, size_t string_max_length)
1682
1688
{
1683
- for (size_t i= 0 ; i< expr.operands (). size (); ++i )
1689
+ for (auto & operand : expr.operands ())
1684
1690
{
1685
1691
// TODO: only copy when necessary
1686
- exprt op=(expr. operands ()[i] );
1687
- expr. operands ()[i] =substitute_array_lists (op, string_max_length);
1692
+ const exprt op (operand );
1693
+ operand =substitute_array_lists (op, string_max_length);
1688
1694
}
1689
1695
1690
1696
if (expr.id ()==" array-list" )
@@ -1729,14 +1735,16 @@ exprt string_refinementt::get(const exprt &expr) const
1729
1735
if (it!=found_length.end ())
1730
1736
return get_array (ecopy, it->second );
1731
1737
}
1732
- else if (is_refined_string_type (ecopy. type ()) && ecopy.id ()==ID_struct)
1738
+ else if (ecopy.id ()==ID_struct)
1733
1739
{
1734
- const string_exprt &string=to_string_expr (ecopy);
1735
- const exprt &content=string.content ();
1736
- const exprt &length=string.length ();
1740
+ if (const auto string=expr_cast<string_exprt>(ecopy))
1741
+ {
1742
+ const exprt &content=string->content ();
1743
+ const exprt &length=string->length ();
1737
1744
1738
- const exprt arr=get_array (content, length);
1739
- ecopy=string_exprt (length, arr, string.type ());
1745
+ const exprt arr=get_array (content, length);
1746
+ ecopy=string_exprt (length, arr, string->type ());
1747
+ }
1740
1748
}
1741
1749
1742
1750
ecopy=supert::get (ecopy);
0 commit comments