Skip to content

Commit 9fb4c37

Browse files
author
Joel Allred
committed
Rename get_length to get_or_create_length
1 parent d26d3ff commit 9fb4c37

18 files changed

+263
-168
lines changed

jbmc/unit/solvers/strings/string_constraint_instantiation/instantiate_not_contains.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ refined_string_exprt make_refined_string_exprt(
9595
array_poolt &array_pool)
9696
{
9797
return refined_string_exprt(
98-
array_pool.get_length(arr), get_data_pointer(arr));
98+
array_pool.get_or_create_length(arr), get_data_pointer(arr));
9999
}
100100

101101
/// For a constant `string_exprt`, creates a full index set.

src/solvers/strings/array_pool.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ operator()(const irep_idt &prefix, const typet &type)
2121
return result;
2222
}
2323

24-
exprt array_poolt::get_length(const array_string_exprt &s)
24+
exprt array_poolt::get_or_create_length(const array_string_exprt &s)
2525
{
2626
if(const auto &if_expr = expr_try_dynamic_cast<if_exprt>((exprt)s))
2727
{
28-
return if_exprt{if_expr->cond(),
29-
get_length(to_array_string_expr(if_expr->true_case())),
30-
get_length(to_array_string_expr(if_expr->false_case()))};
28+
return if_exprt{
29+
if_expr->cond(),
30+
get_or_create_length(to_array_string_expr(if_expr->true_case())),
31+
get_or_create_length(to_array_string_expr(if_expr->false_case()))};
3132
}
3233

3334
auto emplace_result =
@@ -60,7 +61,7 @@ array_poolt::fresh_string(const typet &index_type, const typet &char_type)
6061
address_of_exprt(index_exprt(str, from_integer(0, index_type))), str);
6162

6263
// add length to length_of_array map
63-
get_length(str);
64+
get_or_create_length(str);
6465

6566
return str;
6667
}
@@ -111,7 +112,7 @@ array_string_exprt array_poolt::make_char_array_for_char_pointer(
111112
arrays_of_pointers.insert({char_pointer, array_sym});
112113

113114
// add length to length_of_array map
114-
get_length(array_sym);
115+
get_or_create_length(array_sym);
115116

116117
return to_array_string_expr(insert_result.first->second);
117118
}

src/solvers/strings/array_pool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class array_poolt final
6262
/// each of the parts.
6363
/// \param s: array expression representing a string
6464
/// \return expression for the length of `s`
65-
exprt get_length(const array_string_exprt &s);
65+
exprt get_or_create_length(const array_string_exprt &s);
6666

6767
/// As opposed to get_length(), do not create a new symbol if the length
6868
/// of the array_string_exprt does not have one in the array_pool, but instead

src/solvers/strings/string_builtin_function.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,13 @@ string_constraintst string_concat_char_builtin_functiont::constraints(
207207
constraints.universal.push_back([&] {
208208
const symbol_exprt idx =
209209
generator.fresh_symbol("QA_index_concat_char", result.length_type());
210-
const exprt upper_bound = zero_if_negative(array_pool.get_length(input));
210+
const exprt upper_bound =
211+
zero_if_negative(array_pool.get_or_create_length(input));
211212
return string_constraintt(
212213
idx, upper_bound, equal_exprt(input[idx], result[idx]));
213214
}());
214215
constraints.existential.push_back(
215-
equal_exprt(result[array_pool.get_length(input)], character));
216+
equal_exprt(result[array_pool.get_or_create_length(input)], character));
216217
constraints.existential.push_back(
217218
equal_exprt(return_code, from_integer(0, return_code.type())));
218219
return constraints;
@@ -256,15 +257,17 @@ string_constraintst string_set_char_builtin_functiont::constraints(
256257
constraints.existential.push_back(implies_exprt(
257258
and_exprt(
258259
binary_relation_exprt(from_integer(0, position.type()), ID_le, position),
259-
binary_relation_exprt(position, ID_lt, array_pool.get_length(result))),
260+
binary_relation_exprt(
261+
position, ID_lt, array_pool.get_or_create_length(result))),
260262
equal_exprt(result[position], character)));
261263
constraints.universal.push_back([&] {
262264
const symbol_exprt q =
263265
generator.fresh_symbol("QA_char_set", position.type());
264266
const equal_exprt a3_body(result[q], input[q]);
265267
return string_constraintt(
266268
q,
267-
minimum(zero_if_negative(array_pool.get_length(result)), position),
269+
minimum(
270+
zero_if_negative(array_pool.get_or_create_length(result)), position),
268271
a3_body);
269272
}());
270273
constraints.universal.push_back([&] {
@@ -275,7 +278,7 @@ string_constraintst string_set_char_builtin_functiont::constraints(
275278
return string_constraintt(
276279
q2,
277280
lower_bound,
278-
zero_if_negative(array_pool.get_length(result)),
281+
zero_if_negative(array_pool.get_or_create_length(result)),
279282
a4_body);
280283
}());
281284
return constraints;
@@ -284,15 +287,18 @@ string_constraintst string_set_char_builtin_functiont::constraints(
284287
exprt string_set_char_builtin_functiont::length_constraint() const
285288
{
286289
const exprt out_of_bounds = or_exprt(
287-
binary_relation_exprt(position, ID_ge, array_pool.get_length(input)),
290+
binary_relation_exprt(
291+
position, ID_ge, array_pool.get_or_create_length(input)),
288292
binary_relation_exprt(
289293
position, ID_lt, from_integer(0, input.length_type())));
290294
const exprt return_value = if_exprt(
291295
out_of_bounds,
292296
from_integer(1, return_code.type()),
293297
from_integer(0, return_code.type()));
294298
return and_exprt(
295-
equal_exprt(array_pool.get_length(result), array_pool.get_length(input)),
299+
equal_exprt(
300+
array_pool.get_or_create_length(result),
301+
array_pool.get_or_create_length(input)),
296302
equal_exprt(return_code, return_value));
297303
}
298304

@@ -395,7 +401,7 @@ string_constraintst string_to_lower_case_builtin_functiont::constraints(
395401
}();
396402
return string_constraintt(
397403
idx,
398-
zero_if_negative(array_pool.get_length(result)),
404+
zero_if_negative(array_pool.get_or_create_length(result)),
399405
conditional_convert);
400406
}());
401407
return constraints;
@@ -440,7 +446,7 @@ string_constraintst string_to_upper_case_builtin_functiont::constraints(
440446
minus_exprt(input[idx], from_integer(0x20, char_type));
441447
return string_constraintt(
442448
idx,
443-
zero_if_negative(array_pool.get_length(result)),
449+
zero_if_negative(array_pool.get_or_create_length(result)),
444450
equal_exprt(
445451
result[idx],
446452
if_exprt(is_lower_case(input[idx]), converted, input[idx])));
@@ -606,7 +612,8 @@ exprt string_of_int_builtin_functiont::length_constraint() const
606612

607613
const exprt size_expr_with_sign = if_exprt(
608614
negative_arg, plus_exprt(size_expr, from_integer(1, type)), size_expr);
609-
return equal_exprt(array_pool.get_length(result), size_expr_with_sign);
615+
return equal_exprt(
616+
array_pool.get_or_create_length(result), size_expr_with_sign);
610617
}
611618

612619
string_builtin_function_with_no_evalt::string_builtin_function_with_no_evalt(

src/solvers/strings/string_builtin_function.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ class string_to_lower_case_builtin_functiont
216216
exprt length_constraint() const override
217217
{
218218
return and_exprt(
219-
equal_exprt(array_pool.get_length(result), array_pool.get_length(input)),
219+
equal_exprt(
220+
array_pool.get_or_create_length(result),
221+
array_pool.get_or_create_length(input)),
220222
equal_exprt(return_code, from_integer(0, return_code.type())));
221223
};
222224
};
@@ -267,7 +269,9 @@ class string_to_upper_case_builtin_functiont
267269
exprt length_constraint() const override
268270
{
269271
return and_exprt(
270-
equal_exprt(array_pool.get_length(result), array_pool.get_length(input)),
272+
equal_exprt(
273+
array_pool.get_or_create_length(result),
274+
array_pool.get_or_create_length(input)),
271275
equal_exprt(return_code, from_integer(0, return_code.type())));
272276
};
273277
};

src/solvers/strings/string_constraint_generator_code_points.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ std::pair<exprt, string_constraintst> add_axioms_for_code_point(
4343
exprt hex0400 = from_integer(0x0400, type);
4444

4545
binary_relation_exprt small(code_point, ID_lt, hex010000);
46-
implies_exprt a1(small, equal_to(array_pool.get_length(res), 1));
46+
implies_exprt a1(small, equal_to(array_pool.get_or_create_length(res), 1));
4747
constraints.existential.push_back(a1);
4848

49-
implies_exprt a2(not_exprt(small), equal_to(array_pool.get_length(res), 2));
49+
implies_exprt a2(
50+
not_exprt(small), equal_to(array_pool.get_or_create_length(res), 2));
5051
constraints.existential.push_back(a2);
5152

5253
typecast_exprt code_point_as_char(code_point, char_type);
@@ -167,9 +168,9 @@ std::pair<exprt, string_constraintst> add_axioms_for_code_point_before(
167168
string_constraintst constraints;
168169

169170
const exprt &char1 = str[minus_exprt(
170-
args[1], from_integer(2, array_pool.get_length(str).type()))];
171+
args[1], from_integer(2, array_pool.get_or_create_length(str).type()))];
171172
const exprt &char2 = str[minus_exprt(
172-
args[1], from_integer(1, array_pool.get_length(str).type()))];
173+
args[1], from_integer(1, array_pool.get_or_create_length(str).type()))];
173174
const typecast_exprt char1_as_int(char1, return_type);
174175
const typecast_exprt char2_as_int(char2, return_type);
175176

src/solvers/strings/string_constraint_generator_comparison.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ std::pair<exprt, string_constraintst> add_axioms_for_equals(
4747

4848
// Axiom 1.
4949
constraints.existential.push_back(implies_exprt(
50-
eq, equal_exprt(array_pool.get_length(s1), array_pool.get_length(s2))));
50+
eq,
51+
equal_exprt(
52+
array_pool.get_or_create_length(s1),
53+
array_pool.get_or_create_length(s2))));
5154

5255
// Axiom 2.
5356
constraints.universal.push_back([&] {
5457
const symbol_exprt qvar = fresh_symbol("QA_equal", index_type);
5558
return string_constraintt(
5659
qvar,
57-
zero_if_negative(array_pool.get_length(s1)),
60+
zero_if_negative(array_pool.get_or_create_length(s1)),
5861
implies_exprt(eq, equal_exprt(s1[qvar], s2[qvar])));
5962
}());
6063

@@ -63,12 +66,15 @@ std::pair<exprt, string_constraintst> add_axioms_for_equals(
6366
const symbol_exprt witness = fresh_symbol("witness_unequal", index_type);
6467
const exprt zero = from_integer(0, index_type);
6568
const and_exprt bound_witness(
66-
binary_relation_exprt(witness, ID_lt, array_pool.get_length(s1)),
69+
binary_relation_exprt(
70+
witness, ID_lt, array_pool.get_or_create_length(s1)),
6771
binary_relation_exprt(witness, ID_ge, zero));
6872
const and_exprt witnessing(
6973
bound_witness, notequal_exprt(s1[witness], s2[witness]));
7074
const and_exprt diff_length(
71-
notequal_exprt(array_pool.get_length(s1), array_pool.get_length(s2)),
75+
notequal_exprt(
76+
array_pool.get_or_create_length(s1),
77+
array_pool.get_or_create_length(s2)),
7278
equal_exprt(witness, from_integer(-1, index_type)));
7379
return implies_exprt(not_exprt(eq), or_exprt(diff_length, witnessing));
7480
}());
@@ -148,31 +154,36 @@ std::pair<exprt, string_constraintst> add_axioms_for_equals_ignore_case(
148154
const typet index_type = s1.length_type();
149155

150156
const implies_exprt a1(
151-
eq, equal_exprt(array_pool.get_length(s1), array_pool.get_length(s2)));
157+
eq,
158+
equal_exprt(
159+
array_pool.get_or_create_length(s1),
160+
array_pool.get_or_create_length(s2)));
152161
constraints.existential.push_back(a1);
153162

154163
const symbol_exprt qvar = fresh_symbol("QA_equal_ignore_case", index_type);
155164
const exprt constr2 =
156165
character_equals_ignore_case(s1[qvar], s2[qvar], char_a, char_A, char_Z);
157166
const string_constraintt a2(
158167
qvar,
159-
zero_if_negative(array_pool.get_length(s1)),
168+
zero_if_negative(array_pool.get_or_create_length(s1)),
160169
implies_exprt(eq, constr2));
161170
constraints.universal.push_back(a2);
162171

163172
const symbol_exprt witness =
164173
fresh_symbol("witness_unequal_ignore_case", index_type);
165174
const exprt zero = from_integer(0, witness.type());
166175
const and_exprt bound_witness(
167-
binary_relation_exprt(witness, ID_lt, array_pool.get_length(s1)),
176+
binary_relation_exprt(witness, ID_lt, array_pool.get_or_create_length(s1)),
168177
binary_relation_exprt(witness, ID_ge, zero));
169178
const exprt witness_eq = character_equals_ignore_case(
170179
s1[witness], s2[witness], char_a, char_A, char_Z);
171180
const not_exprt witness_diff(witness_eq);
172181
const implies_exprt a3(
173182
not_exprt(eq),
174183
or_exprt(
175-
notequal_exprt(array_pool.get_length(s1), array_pool.get_length(s2)),
184+
notequal_exprt(
185+
array_pool.get_or_create_length(s1),
186+
array_pool.get_or_create_length(s2)),
176187
and_exprt(bound_witness, witness_diff)));
177188
constraints.existential.push_back(a3);
178189

@@ -207,13 +218,17 @@ string_constraint_generatort::add_axioms_for_hash_code(
207218
const symbol_exprt i = fresh_symbol("index_hash", index_type);
208219
const equal_exprt c1(it.second, hash);
209220
const notequal_exprt c2(
210-
array_pool.get_length(it.first), array_pool.get_length(str));
221+
array_pool.get_or_create_length(it.first),
222+
array_pool.get_or_create_length(str));
211223
const and_exprt c3(
212-
equal_exprt(array_pool.get_length(it.first), array_pool.get_length(str)),
224+
equal_exprt(
225+
array_pool.get_or_create_length(it.first),
226+
array_pool.get_or_create_length(str)),
213227
and_exprt(
214228
notequal_exprt(str[i], it.first[i]),
215229
and_exprt(
216-
greater_than(array_pool.get_length(str), i), is_positive(i))));
230+
greater_than(array_pool.get_or_create_length(str), i),
231+
is_positive(i))));
217232
hash_constraints.existential.push_back(or_exprt(c1, or_exprt(c2, c3)));
218233
}
219234
return {hash, std::move(hash_constraints)};
@@ -257,13 +272,15 @@ std::pair<exprt, string_constraintst> add_axioms_for_compare_to(
257272
const equal_exprt res_null(res, from_integer(0, return_type));
258273
const implies_exprt a1(
259274
res_null,
260-
equal_exprt(array_pool.get_length(s1), array_pool.get_length(s2)));
275+
equal_exprt(
276+
array_pool.get_or_create_length(s1),
277+
array_pool.get_or_create_length(s2)));
261278
constraints.existential.push_back(a1);
262279

263280
const symbol_exprt i = fresh_symbol("QA_compare_to", index_type);
264281
const string_constraintt a2(
265282
i,
266-
zero_if_negative(array_pool.get_length(s1)),
283+
zero_if_negative(array_pool.get_or_create_length(s1)),
267284
implies_exprt(res_null, equal_exprt(s1[i], s2[i])));
268285
constraints.universal.push_back(a2);
269286

@@ -275,24 +292,31 @@ std::pair<exprt, string_constraintst> add_axioms_for_compare_to(
275292
const equal_exprt ret_length_diff(
276293
res,
277294
minus_exprt(
278-
typecast_exprt(array_pool.get_length(s1), return_type),
279-
typecast_exprt(array_pool.get_length(s2), return_type)));
295+
typecast_exprt(array_pool.get_or_create_length(s1), return_type),
296+
typecast_exprt(array_pool.get_or_create_length(s2), return_type)));
280297
const or_exprt guard1(
281298
and_exprt(
282299
less_than_or_equal_to(
283-
array_pool.get_length(s1), array_pool.get_length(s2)),
284-
greater_than(array_pool.get_length(s1), x)),
300+
array_pool.get_or_create_length(s1),
301+
array_pool.get_or_create_length(s2)),
302+
greater_than(array_pool.get_or_create_length(s1), x)),
285303
and_exprt(
286-
greater_or_equal_to(array_pool.get_length(s1), array_pool.get_length(s2)),
287-
greater_than(array_pool.get_length(s2), x)));
304+
greater_or_equal_to(
305+
array_pool.get_or_create_length(s1),
306+
array_pool.get_or_create_length(s2)),
307+
greater_than(array_pool.get_or_create_length(s2), x)));
288308
const and_exprt cond1(ret_char_diff, guard1);
289309
const or_exprt guard2(
290310
and_exprt(
291-
greater_than(array_pool.get_length(s2), array_pool.get_length(s1)),
292-
equal_to(array_pool.get_length(s1), x)),
311+
greater_than(
312+
array_pool.get_or_create_length(s2),
313+
array_pool.get_or_create_length(s1)),
314+
equal_to(array_pool.get_or_create_length(s1), x)),
293315
and_exprt(
294-
greater_than(array_pool.get_length(s1), array_pool.get_length(s2)),
295-
equal_to(array_pool.get_length(s2), x)));
316+
greater_than(
317+
array_pool.get_or_create_length(s1),
318+
array_pool.get_or_create_length(s2)),
319+
equal_to(array_pool.get_or_create_length(s2), x)));
296320
const and_exprt cond2(ret_length_diff, guard2);
297321

298322
const implies_exprt a3(

0 commit comments

Comments
 (0)