|
2 | 2 |
|
3 | 3 | #include "smt2_incremental_decision_procedure.h" |
4 | 4 |
|
| 5 | +#include <util/arith_tools.h> |
5 | 6 | #include <util/expr.h> |
6 | 7 | #include <util/namespace.h> |
7 | 8 | #include <util/nodiscard.h> |
|
12 | 13 |
|
13 | 14 | #include <solvers/smt2_incremental/construct_value_expr_from_smt.h> |
14 | 15 | #include <solvers/smt2_incremental/convert_expr_to_smt.h> |
| 16 | +#include <solvers/smt2_incremental/smt_array_theory.h> |
15 | 17 | #include <solvers/smt2_incremental/smt_commands.h> |
16 | 18 | #include <solvers/smt2_incremental/smt_core_theory.h> |
17 | 19 | #include <solvers/smt2_incremental/smt_responses.h> |
@@ -64,14 +66,40 @@ static std::vector<exprt> gather_dependent_expressions(const exprt &expr) |
64 | 66 | { |
65 | 67 | std::vector<exprt> dependent_expressions; |
66 | 68 | expr.visit_pre([&](const exprt &expr_node) { |
67 | | - if(can_cast_expr<symbol_exprt>(expr_node)) |
| 69 | + if( |
| 70 | + can_cast_expr<symbol_exprt>(expr_node) || |
| 71 | + can_cast_expr<array_exprt>(expr_node)) |
68 | 72 | { |
69 | 73 | dependent_expressions.push_back(expr_node); |
70 | 74 | } |
71 | 75 | }); |
72 | 76 | return dependent_expressions; |
73 | 77 | } |
74 | 78 |
|
| 79 | +void smt2_incremental_decision_proceduret::define_array_function( |
| 80 | + const array_exprt &array) |
| 81 | +{ |
| 82 | + const smt_sortt array_sort = convert_type_to_smt_sort(array.type()); |
| 83 | + INVARIANT( |
| 84 | + array_sort.cast<smt_array_sortt>(), |
| 85 | + "Converting array typed expression to SMT should result in a term of array " |
| 86 | + "sort."); |
| 87 | + const smt_identifier_termt array_identifier = smt_identifier_termt{ |
| 88 | + "array_" + std::to_string(array_sequence()), array_sort}; |
| 89 | + solver_process->send(smt_declare_function_commandt{array_identifier, {}}); |
| 90 | + const std::vector<exprt> &elements = array.operands(); |
| 91 | + const typet &index_type = array.type().index_type(); |
| 92 | + for(std::size_t i = 0; i < elements.size(); ++i) |
| 93 | + { |
| 94 | + const smt_termt index = convert_expr_to_smt(from_integer(i, index_type)); |
| 95 | + const smt_assert_commandt element_definition{smt_core_theoryt::equal( |
| 96 | + smt_array_theoryt::select(array_identifier, index), |
| 97 | + convert_expr_to_smt(elements.at(i)))}; |
| 98 | + solver_process->send(element_definition); |
| 99 | + } |
| 100 | + expression_identifiers.emplace(array, array_identifier); |
| 101 | +} |
| 102 | + |
75 | 103 | /// \brief Defines any functions which \p expr depends on, which have not yet |
76 | 104 | /// been defined, along with their dependencies in turn. |
77 | 105 | void smt2_incremental_decision_proceduret::define_dependent_functions( |
@@ -123,10 +151,29 @@ void smt2_incremental_decision_proceduret::define_dependent_functions( |
123 | 151 | solver_process->send(function); |
124 | 152 | } |
125 | 153 | } |
| 154 | + if(const auto array_expr = expr_try_dynamic_cast<array_exprt>(current)) |
| 155 | + define_array_function(*array_expr); |
126 | 156 | to_be_defined.pop(); |
127 | 157 | } |
128 | 158 | } |
129 | 159 |
|
| 160 | +/// Replaces the sub expressions of \p expr which have been defined as separate |
| 161 | +/// functions in the smt solver, using the \p expression_identifiers map. |
| 162 | +static exprt substitute_identifiers( |
| 163 | + exprt expr, |
| 164 | + const std::unordered_map<exprt, smt_identifier_termt, irep_hash> |
| 165 | + &expression_identifiers) |
| 166 | +{ |
| 167 | + expr.visit_pre([&](exprt &node) -> void { |
| 168 | + auto find_result = expression_identifiers.find(node); |
| 169 | + if(find_result == expression_identifiers.cend()) |
| 170 | + return; |
| 171 | + const auto type = find_result->first.type(); |
| 172 | + node = symbol_exprt{find_result->second.identifier(), type}; |
| 173 | + }); |
| 174 | + return expr; |
| 175 | +} |
| 176 | + |
130 | 177 | smt2_incremental_decision_proceduret::smt2_incremental_decision_proceduret( |
131 | 178 | const namespacet &_ns, |
132 | 179 | std::unique_ptr<smt_base_solver_processt> _solver_process, |
@@ -164,15 +211,20 @@ void smt2_incremental_decision_proceduret::ensure_handle_for_expr_defined( |
164 | 211 | smt_termt |
165 | 212 | smt2_incremental_decision_proceduret::convert_expr_to_smt(const exprt &expr) |
166 | 213 | { |
167 | | - track_expression_objects(expr, ns, object_map); |
| 214 | + const exprt substituted = |
| 215 | + substitute_identifiers(expr, expression_identifiers); |
| 216 | + track_expression_objects(substituted, ns, object_map); |
168 | 217 | associate_pointer_sizes( |
169 | | - expr, |
| 218 | + substituted, |
170 | 219 | ns, |
171 | 220 | pointer_sizes_map, |
172 | 221 | object_map, |
173 | 222 | object_size_function.make_application); |
174 | 223 | return ::convert_expr_to_smt( |
175 | | - expr, object_map, pointer_sizes_map, object_size_function.make_application); |
| 224 | + substituted, |
| 225 | + object_map, |
| 226 | + pointer_sizes_map, |
| 227 | + object_size_function.make_application); |
176 | 228 | } |
177 | 229 |
|
178 | 230 | exprt smt2_incremental_decision_proceduret::handle(const exprt &expr) |
|
0 commit comments