Skip to content

Commit ec89991

Browse files
Merge pull request #1572 from romainbrenguier/refactor/expr_cast_to_util
Move expr_cast from refinement to util/convert_expr.h
2 parents 5cd0f2f + 9a811d8 commit ec89991

8 files changed

+169
-167
lines changed

src/solvers/refinement/expr_cast.h

-71
This file was deleted.

src/solvers/refinement/string_constraint_generator.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ exprt get_numeric_value_from_character(
373373

374374
size_t max_printed_string_length(const typet &type, unsigned long ul_radix);
375375

376-
std::string utf16_constant_array_to_java(
377-
const array_exprt &arr, unsigned length);
376+
std::string
377+
utf16_constant_array_to_java(const array_exprt &arr, std::size_t length);
378378

379379
#endif

src/solvers/refinement/string_constraint_generator_format.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ exprt string_constraint_generatort::add_axioms_for_format(
417417
/// \param length: an unsigned value representing the length of the array
418418
/// \return String of length `length` represented by the array assuming each
419419
/// field in `arr` represents a character.
420-
std::string utf16_constant_array_to_java(
421-
const array_exprt &arr, unsigned int length)
420+
std::string
421+
utf16_constant_array_to_java(const array_exprt &arr, std::size_t length)
422422
{
423423
std::wstring out(length, '?');
424424
unsigned int c;

src/solvers/refinement/string_constraint_generator_transformation.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Author: Romain Brenguier, [email protected]
1313

1414
#include <solvers/refinement/string_refinement_invariant.h>
1515
#include <solvers/refinement/string_constraint_generator.h>
16-
#include "expr_cast.h"
16+
#include <util/arith_tools.h>
1717

1818
/// Reduce or extend a string to have the given length
1919
///
@@ -442,8 +442,8 @@ static optionalt<std::pair<exprt, exprt>> to_char_pair(
442442
return std::make_pair(expr1, expr2);
443443
const auto expr1_str = get_string_expr(expr1);
444444
const auto expr2_str = get_string_expr(expr2);
445-
const auto expr1_length=expr_cast<size_t>(expr1_str.length());
446-
const auto expr2_length=expr_cast<size_t>(expr2_str.length());
445+
const auto expr1_length = numeric_cast<std::size_t>(expr1_str.length());
446+
const auto expr2_length = numeric_cast<std::size_t>(expr2_str.length());
447447
if(expr1_length && expr2_length && *expr1_length==1 && *expr2_length==1)
448448
return std::make_pair(exprt(expr1_str[0]), exprt(expr2_str[0]));
449449
return { };

src/solvers/refinement/string_refinement.cpp

+23-22
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ Author: Alberto Griggio, [email protected]
2222
#include <iomanip>
2323
#include <stack>
2424
#include <util/expr_iterator.h>
25-
#include <util/optional.h>
25+
#include <util/arith_tools.h>
2626
#include <util/simplify_expr.h>
2727
#include <solvers/sat/satcheck.h>
2828
#include <solvers/refinement/string_constraint_instantiation.h>
2929
#include <java_bytecode/java_types.h>
30-
#include "expr_cast.h"
3130

3231
static exprt substitute_array_with_expr(const exprt &expr, const exprt &index);
3332

@@ -795,12 +794,13 @@ static optionalt<exprt> get_array(
795794
return {};
796795
}
797796

798-
unsigned n;
799-
if(to_unsigned_integer(to_constant_expr(size_val), n))
797+
auto n_opt = numeric_cast<std::size_t>(size_val);
798+
if(!n_opt)
800799
{
801800
stream << "(sr::get_array) size is not valid" << eom;
802801
return {};
803802
}
803+
std::size_t n = *n_opt;
804804

805805
const array_typet ret_type(char_type, from_integer(n, index_type));
806806
array_exprt ret(ret_type);
@@ -825,9 +825,11 @@ static optionalt<exprt> get_array(
825825
for(size_t i = 0; i < arr_val.operands().size(); i += 2)
826826
{
827827
exprt index = arr_val.operands()[i];
828-
unsigned idx;
829-
if(!to_unsigned_integer(to_constant_expr(index), idx) && idx<n)
830-
initial_map[idx] = arr_val.operands()[i + 1];
828+
if(auto idx = numeric_cast<std::size_t>(index))
829+
{
830+
if(*idx < n)
831+
initial_map[*idx] = arr_val.operands()[i + 1];
832+
}
831833
}
832834

833835
// Pad the concretized values to the left to assign the uninitialized
@@ -852,13 +854,11 @@ static optionalt<exprt> get_array(
852854
/// \return a string
853855
static std::string string_of_array(const array_exprt &arr)
854856
{
855-
unsigned n;
856857
if(arr.type().id()!=ID_array)
857858
return std::string("");
858859

859860
exprt size_expr=to_array_type(arr.type()).size();
860-
PRECONDITION(size_expr.id()==ID_constant);
861-
to_unsigned_integer(to_constant_expr(size_expr), n);
861+
auto n = numeric_cast_v<std::size_t>(size_expr);
862862
return utf16_constant_array_to_java(arr, n);
863863
}
864864

@@ -1010,7 +1010,7 @@ exprt fill_in_array_with_expr(
10101010
std::map<std::size_t, exprt> initial_map;
10111011

10121012
// Set the last index to be sure the array will have the right length
1013-
const auto &array_size_opt = expr_cast<std::size_t>(array_type.size());
1013+
const auto &array_size_opt = numeric_cast<std::size_t>(array_type.size());
10141014
if(array_size_opt && *array_size_opt > 0)
10151015
initial_map.emplace(
10161016
*array_size_opt - 1,
@@ -1022,7 +1022,8 @@ exprt fill_in_array_with_expr(
10221022
// statements
10231023
const with_exprt &with_expr = to_with_expr(it);
10241024
const exprt &then_expr=with_expr.new_value();
1025-
const auto index=expr_cast_v<std::size_t>(with_expr.where());
1025+
const auto index =
1026+
numeric_cast_v<std::size_t>(to_constant_expr(with_expr.where()));
10261027
if(
10271028
index < string_max_length && (!array_size_opt || index < *array_size_opt))
10281029
initial_map.emplace(index, then_expr);
@@ -1047,7 +1048,7 @@ exprt fill_in_array_expr(const array_exprt &expr, std::size_t string_max_length)
10471048

10481049
// Map of the parts of the array that are initialized
10491050
std::map<std::size_t, exprt> initial_map;
1050-
const auto &array_size_opt = expr_cast<std::size_t>(array_type.size());
1051+
const auto &array_size_opt = numeric_cast<std::size_t>(array_type.size());
10511052

10521053
if(array_size_opt && *array_size_opt > 0)
10531054
initial_map.emplace(
@@ -1180,14 +1181,14 @@ static exprt negation_of_not_contains_constraint(
11801181
const exprt &ubu=axiom.univ_upper_bound();
11811182
if(lbu.id()==ID_constant && ubu.id()==ID_constant)
11821183
{
1183-
const auto lb_int=expr_cast<mp_integer>(lbu);
1184-
const auto ub_int=expr_cast<mp_integer>(ubu);
1184+
const auto lb_int = numeric_cast<mp_integer>(lbu);
1185+
const auto ub_int = numeric_cast<mp_integer>(ubu);
11851186
if(!lb_int || !ub_int || *ub_int<=*lb_int)
11861187
return false_exprt();
11871188
}
11881189

1189-
const auto lbe=expr_cast_v<mp_integer>(axiom.exists_lower_bound());
1190-
const auto ube=expr_cast_v<mp_integer>(axiom.exists_upper_bound());
1190+
const auto lbe = numeric_cast_v<mp_integer>(axiom.exists_lower_bound());
1191+
const auto ube = numeric_cast_v<mp_integer>(axiom.exists_upper_bound());
11911192

11921193
// If the premise is false, the implication is trivially true, so the
11931194
// negation is false.
@@ -1230,8 +1231,8 @@ static exprt negation_of_constraint(const string_constraintt &axiom)
12301231
const exprt &ub=axiom.upper_bound();
12311232
if(lb.id()==ID_constant && ub.id()==ID_constant)
12321233
{
1233-
const auto lb_int=expr_cast<mp_integer>(lb);
1234-
const auto ub_int=expr_cast<mp_integer>(ub);
1234+
const auto lb_int = numeric_cast<mp_integer>(lb);
1235+
const auto ub_int = numeric_cast<mp_integer>(ub);
12351236
if(!lb_int || !ub_int || ub_int<=lb_int)
12361237
return false_exprt();
12371238
}
@@ -1786,7 +1787,7 @@ static void add_to_index_set(
17861787
exprt i)
17871788
{
17881789
simplify(i, ns);
1789-
const bool is_size_t=expr_cast<std::size_t>(i).has_value();
1790+
const bool is_size_t = numeric_cast<std::size_t>(i).has_value();
17901791
if(i.id()!=ID_constant || is_size_t)
17911792
{
17921793
std::vector<exprt> sub_arrays;
@@ -2047,7 +2048,7 @@ exprt substitute_array_lists(exprt expr, size_t string_max_length)
20472048
{
20482049
const exprt &index=expr.operands()[i];
20492050
const exprt &value=expr.operands()[i+1];
2050-
const auto index_value=expr_cast<std::size_t>(index);
2051+
const auto index_value = numeric_cast<std::size_t>(index);
20512052
if(!index.is_constant() ||
20522053
(index_value && *index_value<string_max_length))
20532054
ret_expr=with_exprt(ret_expr, index, value);
@@ -2097,7 +2098,7 @@ exprt string_refinementt::get(const exprt &expr) const
20972098
if(set.find(arr) != set.end())
20982099
{
20992100
exprt length = super_get(arr.length());
2100-
if(const auto n = expr_cast<std::size_t>(length))
2101+
if(const auto n = numeric_cast<std::size_t>(length))
21012102
{
21022103
exprt arr_model =
21032104
array_exprt(array_typet(arr.type().subtype(), length));

0 commit comments

Comments
 (0)