|
| 1 | +/// Module: String solver |
| 2 | +/// Author: Diffblue Ltd. |
| 3 | + |
| 4 | +#include "string_builtin_function.h" |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include "string_constraint_generator.h" |
| 8 | + |
| 9 | +/// Get the valuation of the string, given a valuation |
| 10 | +static optionalt<std::vector<mp_integer>> eval_string( |
| 11 | + const array_string_exprt &a, |
| 12 | + const std::function<exprt(const exprt &)> &get_value); |
| 13 | + |
| 14 | +/// Make a string from a constant array |
| 15 | +static array_string_exprt make_string( |
| 16 | + const std::vector<mp_integer> &array, |
| 17 | + const array_typet &array_type); |
| 18 | + |
| 19 | +string_transformation_builtin_functiont:: |
| 20 | + string_transformation_builtin_functiont( |
| 21 | + const std::vector<exprt> &fun_args, |
| 22 | + array_poolt &array_pool) |
| 23 | +{ |
| 24 | + PRECONDITION(fun_args.size() > 2); |
| 25 | + const auto arg1 = expr_checked_cast<struct_exprt>(fun_args[2]); |
| 26 | + input = array_pool.find(arg1.op1(), arg1.op0()); |
| 27 | + result = array_pool.find(fun_args[1], fun_args[0]); |
| 28 | + args.insert(args.end(), fun_args.begin() + 3, fun_args.end()); |
| 29 | +} |
| 30 | + |
| 31 | +optionalt<exprt> string_transformation_builtin_functiont::eval( |
| 32 | + const std::function<exprt(const exprt &)> &get_value) const |
| 33 | +{ |
| 34 | + const auto &input_value = eval_string(input, get_value); |
| 35 | + if(!input_value.has_value()) |
| 36 | + return {}; |
| 37 | + |
| 38 | + std::vector<mp_integer> arg_values; |
| 39 | + const auto &insert = std::back_inserter(arg_values); |
| 40 | + const mp_integer unknown('?'); |
| 41 | + std::transform( |
| 42 | + args.begin(), args.end(), insert, [&](const exprt &e) { // NOLINT |
| 43 | + if(const auto val = numeric_cast<mp_integer>(get_value(e))) |
| 44 | + return *val; |
| 45 | + INVARIANT( |
| 46 | + get_value(e).id() == ID_unknown, |
| 47 | + "array valuation should only contain constants and unknown"); |
| 48 | + return unknown; |
| 49 | + }); |
| 50 | + |
| 51 | + const auto result_value = eval(*input_value, arg_values); |
| 52 | + const auto length = from_integer(result_value.size(), result.length().type()); |
| 53 | + const array_typet type(result.type().subtype(), length); |
| 54 | + return make_string(result_value, type); |
| 55 | +} |
| 56 | + |
| 57 | +string_insertion_builtin_functiont::string_insertion_builtin_functiont( |
| 58 | + const std::vector<exprt> &fun_args, |
| 59 | + array_poolt &array_pool) |
| 60 | +{ |
| 61 | + PRECONDITION(fun_args.size() > 4); |
| 62 | + const auto arg1 = expr_checked_cast<struct_exprt>(fun_args[2]); |
| 63 | + input1 = array_pool.find(arg1.op1(), arg1.op0()); |
| 64 | + const auto arg2 = expr_checked_cast<struct_exprt>(fun_args[4]); |
| 65 | + input2 = array_pool.find(arg2.op1(), arg2.op0()); |
| 66 | + result = array_pool.find(fun_args[1], fun_args[0]); |
| 67 | + args.push_back(fun_args[3]); |
| 68 | + args.insert(args.end(), fun_args.begin() + 5, fun_args.end()); |
| 69 | +} |
| 70 | + |
| 71 | +string_concatenation_builtin_functiont::string_concatenation_builtin_functiont( |
| 72 | + const std::vector<exprt> &fun_args, |
| 73 | + array_poolt &array_pool) |
| 74 | +{ |
| 75 | + PRECONDITION(fun_args.size() >= 4 && fun_args.size() <= 6); |
| 76 | + const auto arg1 = expr_checked_cast<struct_exprt>(fun_args[2]); |
| 77 | + input1 = array_pool.find(arg1.op1(), arg1.op0()); |
| 78 | + const auto arg2 = expr_checked_cast<struct_exprt>(fun_args[3]); |
| 79 | + input2 = array_pool.find(arg2.op1(), arg2.op0()); |
| 80 | + result = array_pool.find(fun_args[1], fun_args[0]); |
| 81 | + args.insert(args.end(), fun_args.begin() + 4, fun_args.end()); |
| 82 | +} |
| 83 | + |
| 84 | +optionalt<std::vector<mp_integer>> eval_string( |
| 85 | + const array_string_exprt &a, |
| 86 | + const std::function<exprt(const exprt &)> &get_value) |
| 87 | +{ |
| 88 | + if(a.id() == ID_if) |
| 89 | + { |
| 90 | + const if_exprt &ite = to_if_expr(a); |
| 91 | + const exprt cond = get_value(ite.cond()); |
| 92 | + if(!cond.is_constant()) |
| 93 | + return {}; |
| 94 | + return cond.is_true() |
| 95 | + ? eval_string(to_array_string_expr(ite.true_case()), get_value) |
| 96 | + : eval_string(to_array_string_expr(ite.false_case()), get_value); |
| 97 | + } |
| 98 | + |
| 99 | + const exprt content = get_value(a.content()); |
| 100 | + const auto &array = expr_try_dynamic_cast<array_exprt>(content); |
| 101 | + if(!array) |
| 102 | + return {}; |
| 103 | + |
| 104 | + const auto &ops = array->operands(); |
| 105 | + std::vector<mp_integer> result; |
| 106 | + const mp_integer unknown('?'); |
| 107 | + const auto &insert = std::back_inserter(result); |
| 108 | + std::transform( |
| 109 | + ops.begin(), ops.end(), insert, [unknown](const exprt &e) { // NOLINT |
| 110 | + if(const auto i = numeric_cast<mp_integer>(e)) |
| 111 | + return *i; |
| 112 | + return unknown; |
| 113 | + }); |
| 114 | + return result; |
| 115 | +} |
| 116 | + |
| 117 | +array_string_exprt |
| 118 | +make_string(const std::vector<mp_integer> &array, const array_typet &array_type) |
| 119 | +{ |
| 120 | + const typet &char_type = array_type.subtype(); |
| 121 | + array_exprt array_expr(array_type); |
| 122 | + const auto &insert = std::back_inserter(array_expr.operands()); |
| 123 | + std::transform( |
| 124 | + array.begin(), array.end(), insert, [&](const mp_integer &i) { // NOLINT |
| 125 | + return from_integer(i, char_type); |
| 126 | + }); |
| 127 | + return to_array_string_expr(array_expr); |
| 128 | +} |
| 129 | + |
| 130 | +std::vector<mp_integer> string_concatenation_builtin_functiont::eval( |
| 131 | + const std::vector<mp_integer> &input1_value, |
| 132 | + const std::vector<mp_integer> &input2_value, |
| 133 | + const std::vector<mp_integer> &args_value) const |
| 134 | +{ |
| 135 | + const auto start_index = |
| 136 | + args_value.size() > 0 && args_value[0] > 0 ? args_value[0] : mp_integer(0); |
| 137 | + const mp_integer input2_size(input2_value.size()); |
| 138 | + const auto end_index = |
| 139 | + args_value.size() > 1 |
| 140 | + ? std::max(std::min(args_value[1], input2_size), start_index) |
| 141 | + : input2_size; |
| 142 | + |
| 143 | + std::vector<mp_integer> result(input1_value); |
| 144 | + result.insert( |
| 145 | + result.end(), |
| 146 | + input2_value.begin() + numeric_cast_v<std::size_t>(start_index), |
| 147 | + input2_value.begin() + numeric_cast_v<std::size_t>(end_index)); |
| 148 | + return result; |
| 149 | +} |
| 150 | + |
| 151 | +std::vector<mp_integer> string_concat_char_builtin_functiont::eval( |
| 152 | + const std::vector<mp_integer> &input_value, |
| 153 | + const std::vector<mp_integer> &args_value) const |
| 154 | +{ |
| 155 | + PRECONDITION(args_value.size() == 1); |
| 156 | + std::vector<mp_integer> result(input_value); |
| 157 | + result.push_back(args_value[0]); |
| 158 | + return result; |
| 159 | +} |
| 160 | + |
| 161 | +std::vector<mp_integer> string_insertion_builtin_functiont::eval( |
| 162 | + const std::vector<mp_integer> &input1_value, |
| 163 | + const std::vector<mp_integer> &input2_value, |
| 164 | + const std::vector<mp_integer> &args_value) const |
| 165 | +{ |
| 166 | + PRECONDITION(args_value.size() >= 1 || args_value.size() <= 3); |
| 167 | + const auto offset = std::min( |
| 168 | + std::max(args_value[0], mp_integer(0)), mp_integer(input1_value.size())); |
| 169 | + const auto start = args_value.size() > 1 |
| 170 | + ? std::max(args_value[1], mp_integer(0)) |
| 171 | + : mp_integer(0); |
| 172 | + |
| 173 | + const mp_integer input2_size(input2_value.size()); |
| 174 | + const auto end = |
| 175 | + args_value.size() > 2 |
| 176 | + ? std::max(std::min(args_value[2], input2_size), mp_integer(0)) |
| 177 | + : input2_size; |
| 178 | + |
| 179 | + std::vector<mp_integer> result(input1_value); |
| 180 | + result.insert( |
| 181 | + result.begin() + numeric_cast_v<std::size_t>(offset), |
| 182 | + input2_value.begin() + numeric_cast_v<std::size_t>(start), |
| 183 | + input2_value.begin() + numeric_cast_v<std::size_t>(end)); |
| 184 | + return result; |
| 185 | +} |
| 186 | + |
| 187 | +optionalt<exprt> string_insertion_builtin_functiont::eval( |
| 188 | + const std::function<exprt(const exprt &)> &get_value) const |
| 189 | +{ |
| 190 | + const auto &input1_value = eval_string(input1, get_value); |
| 191 | + const auto &input2_value = eval_string(input2, get_value); |
| 192 | + if(!input2_value.has_value() || !input1_value.has_value()) |
| 193 | + return {}; |
| 194 | + |
| 195 | + std::vector<mp_integer> arg_values; |
| 196 | + const auto &insert = std::back_inserter(arg_values); |
| 197 | + const mp_integer unknown('?'); |
| 198 | + std::transform( |
| 199 | + args.begin(), args.end(), insert, [&](const exprt &e) { // NOLINT |
| 200 | + if(const auto val = numeric_cast<mp_integer>(get_value(e))) |
| 201 | + return *val; |
| 202 | + return unknown; |
| 203 | + }); |
| 204 | + |
| 205 | + const auto result_value = eval(*input1_value, *input2_value, arg_values); |
| 206 | + const auto length = from_integer(result_value.size(), result.length().type()); |
| 207 | + const array_typet type(result.type().subtype(), length); |
| 208 | + return make_string(result_value, type); |
| 209 | +} |
0 commit comments