Skip to content

Commit 79ed89a

Browse files
committed
Refactor to deduplicate shared bit vector validation code
1 parent 2abe40a commit 79ed89a

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

src/solvers/smt2_incremental/smt_bit_vector_theory.cpp

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,34 @@ smt_sortt smt_bit_vector_theoryt::concatt::return_sort(
1919
return smt_bit_vector_sortt{get_width(lhs) + get_width(rhs)};
2020
}
2121

22+
static void validate_bit_vector_sort(
23+
const std::string &descriptor,
24+
const smt_termt &operand)
25+
{
26+
const auto operand_sort = operand.get_sort().cast<smt_bit_vector_sortt>();
27+
INVARIANT(
28+
operand_sort,
29+
descriptor + " operand is expected to have a bit-vector sort.");
30+
}
31+
32+
static void validate_bit_vector_sort(
33+
const smt_termt &operand)
34+
{
35+
validate_bit_vector_sort("The", operand);
36+
}
37+
38+
static void
39+
validate_bit_vector_sorts(const smt_termt &lhs, const smt_termt &rhs)
40+
{
41+
validate_bit_vector_sort("Left", lhs);
42+
validate_bit_vector_sort("Right", rhs);
43+
}
44+
2245
void smt_bit_vector_theoryt::concatt::validate(
2346
const smt_termt &lhs,
2447
const smt_termt &rhs)
2548
{
26-
const auto lhs_sort = lhs.get_sort().cast<smt_bit_vector_sortt>();
27-
INVARIANT(lhs_sort, "Left operand must have bitvector sort.");
28-
const auto rhs_sort = rhs.get_sort().cast<smt_bit_vector_sortt>();
29-
INVARIANT(rhs_sort, "Right operand must have bitvector sort.");
49+
validate_bit_vector_sorts(lhs, rhs);
3050
}
3151

3252
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::concatt>
@@ -63,18 +83,14 @@ smt_bit_vector_theoryt::extract(std::size_t i, std::size_t j)
6383
return smt_function_application_termt::factoryt<extractt>(i, j);
6484
}
6585

66-
static void validate_bit_vector_operator_arguments(
67-
const smt_termt &left,
68-
const smt_termt &right)
86+
static void
87+
validate_matched_bit_vector_sorts(const smt_termt &left, const smt_termt &right)
6988
{
70-
const auto left_sort = left.get_sort().cast<smt_bit_vector_sortt>();
71-
INVARIANT(left_sort, "Left operand must have bitvector sort.");
72-
const auto right_sort = right.get_sort().cast<smt_bit_vector_sortt>();
73-
INVARIANT(right_sort, "Right operand must have bitvector sort.");
89+
validate_bit_vector_sorts(left, right);
7490
// The below invariant is based on the smtlib standard.
7591
// See http://smtlib.cs.uiowa.edu/logics-all.shtml#QF_BV
7692
INVARIANT(
77-
left_sort->bit_width() == right_sort->bit_width(),
93+
left.get_sort() == right.get_sort(),
7894
"Left and right operands must have the same bit width.");
7995
}
8096

@@ -92,8 +108,7 @@ smt_sortt smt_bit_vector_theoryt::nott::return_sort(const smt_termt &operand)
92108

93109
void smt_bit_vector_theoryt::nott::validate(const smt_termt &operand)
94110
{
95-
const auto operand_sort = operand.get_sort().cast<smt_bit_vector_sortt>();
96-
INVARIANT(operand_sort, "The operand is expected to have a bit-vector sort.");
111+
validate_bit_vector_sort(operand);
97112
}
98113

99114
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nott>
@@ -115,7 +130,7 @@ void smt_bit_vector_theoryt::andt::validate(
115130
const smt_termt &lhs,
116131
const smt_termt &rhs)
117132
{
118-
validate_bit_vector_operator_arguments(lhs, rhs);
133+
validate_matched_bit_vector_sorts(lhs, rhs);
119134
}
120135

121136
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::andt>
@@ -137,7 +152,7 @@ void smt_bit_vector_theoryt::ort::validate(
137152
const smt_termt &lhs,
138153
const smt_termt &rhs)
139154
{
140-
validate_bit_vector_operator_arguments(lhs, rhs);
155+
validate_matched_bit_vector_sorts(lhs, rhs);
141156
}
142157

143158
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::ort>
@@ -159,7 +174,7 @@ void smt_bit_vector_theoryt::nandt::validate(
159174
const smt_termt &lhs,
160175
const smt_termt &rhs)
161176
{
162-
validate_bit_vector_operator_arguments(lhs, rhs);
177+
validate_matched_bit_vector_sorts(lhs, rhs);
163178
}
164179

165180
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nandt>
@@ -181,7 +196,7 @@ void smt_bit_vector_theoryt::nort::validate(
181196
const smt_termt &lhs,
182197
const smt_termt &rhs)
183198
{
184-
validate_bit_vector_operator_arguments(lhs, rhs);
199+
validate_matched_bit_vector_sorts(lhs, rhs);
185200
}
186201

187202
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nort>
@@ -203,7 +218,7 @@ void smt_bit_vector_theoryt::xort::validate(
203218
const smt_termt &lhs,
204219
const smt_termt &rhs)
205220
{
206-
validate_bit_vector_operator_arguments(lhs, rhs);
221+
validate_matched_bit_vector_sorts(lhs, rhs);
207222
}
208223

209224
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::xort>
@@ -225,7 +240,7 @@ void smt_bit_vector_theoryt::xnort::validate(
225240
const smt_termt &lhs,
226241
const smt_termt &rhs)
227242
{
228-
validate_bit_vector_operator_arguments(lhs, rhs);
243+
validate_matched_bit_vector_sorts(lhs, rhs);
229244
}
230245

231246
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::xnort>
@@ -249,7 +264,7 @@ void smt_bit_vector_theoryt::unsigned_less_thant::validate(
249264
const smt_termt &lhs,
250265
const smt_termt &rhs)
251266
{
252-
validate_bit_vector_operator_arguments(lhs, rhs);
267+
validate_matched_bit_vector_sorts(lhs, rhs);
253268
}
254269

255270
const smt_function_application_termt::factoryt<
@@ -272,7 +287,7 @@ void smt_bit_vector_theoryt::unsigned_less_than_or_equalt::validate(
272287
const smt_termt &lhs,
273288
const smt_termt &rhs)
274289
{
275-
validate_bit_vector_operator_arguments(lhs, rhs);
290+
validate_matched_bit_vector_sorts(lhs, rhs);
276291
}
277292

278293
const smt_function_application_termt::factoryt<
@@ -295,7 +310,7 @@ void smt_bit_vector_theoryt::unsigned_greater_thant::validate(
295310
const smt_termt &lhs,
296311
const smt_termt &rhs)
297312
{
298-
validate_bit_vector_operator_arguments(lhs, rhs);
313+
validate_matched_bit_vector_sorts(lhs, rhs);
299314
}
300315

301316
const smt_function_application_termt::factoryt<
@@ -319,7 +334,7 @@ void smt_bit_vector_theoryt::unsigned_greater_than_or_equalt::validate(
319334
const smt_termt &lhs,
320335
const smt_termt &rhs)
321336
{
322-
validate_bit_vector_operator_arguments(lhs, rhs);
337+
validate_matched_bit_vector_sorts(lhs, rhs);
323338
}
324339

325340
const smt_function_application_termt::factoryt<
@@ -342,7 +357,7 @@ void smt_bit_vector_theoryt::signed_less_thant::validate(
342357
const smt_termt &lhs,
343358
const smt_termt &rhs)
344359
{
345-
validate_bit_vector_operator_arguments(lhs, rhs);
360+
validate_matched_bit_vector_sorts(lhs, rhs);
346361
}
347362

348363
const smt_function_application_termt::factoryt<
@@ -365,7 +380,7 @@ void smt_bit_vector_theoryt::signed_less_than_or_equalt::validate(
365380
const smt_termt &lhs,
366381
const smt_termt &rhs)
367382
{
368-
validate_bit_vector_operator_arguments(lhs, rhs);
383+
validate_matched_bit_vector_sorts(lhs, rhs);
369384
}
370385

371386
const smt_function_application_termt::factoryt<
@@ -388,7 +403,7 @@ void smt_bit_vector_theoryt::signed_greater_thant::validate(
388403
const smt_termt &lhs,
389404
const smt_termt &rhs)
390405
{
391-
validate_bit_vector_operator_arguments(lhs, rhs);
406+
validate_matched_bit_vector_sorts(lhs, rhs);
392407
}
393408

394409
const smt_function_application_termt::factoryt<
@@ -411,7 +426,7 @@ void smt_bit_vector_theoryt::signed_greater_than_or_equalt::validate(
411426
const smt_termt &lhs,
412427
const smt_termt &rhs)
413428
{
414-
validate_bit_vector_operator_arguments(lhs, rhs);
429+
validate_matched_bit_vector_sorts(lhs, rhs);
415430
}
416431

417432
const smt_function_application_termt::factoryt<
@@ -434,7 +449,7 @@ void smt_bit_vector_theoryt::addt::validate(
434449
const smt_termt &lhs,
435450
const smt_termt &rhs)
436451
{
437-
validate_bit_vector_operator_arguments(lhs, rhs);
452+
validate_matched_bit_vector_sorts(lhs, rhs);
438453
}
439454

440455
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::addt>
@@ -456,7 +471,7 @@ void smt_bit_vector_theoryt::subtractt::validate(
456471
const smt_termt &lhs,
457472
const smt_termt &rhs)
458473
{
459-
validate_bit_vector_operator_arguments(lhs, rhs);
474+
validate_matched_bit_vector_sorts(lhs, rhs);
460475
}
461476

462477
const smt_function_application_termt::factoryt<
@@ -479,7 +494,7 @@ void smt_bit_vector_theoryt::multiplyt::validate(
479494
const smt_termt &lhs,
480495
const smt_termt &rhs)
481496
{
482-
validate_bit_vector_operator_arguments(lhs, rhs);
497+
validate_matched_bit_vector_sorts(lhs, rhs);
483498
}
484499

485500
const smt_function_application_termt::factoryt<
@@ -502,7 +517,7 @@ void smt_bit_vector_theoryt::unsigned_dividet::validate(
502517
const smt_termt &lhs,
503518
const smt_termt &rhs)
504519
{
505-
validate_bit_vector_operator_arguments(lhs, rhs);
520+
validate_matched_bit_vector_sorts(lhs, rhs);
506521
}
507522

508523
const smt_function_application_termt::factoryt<
@@ -525,7 +540,7 @@ void smt_bit_vector_theoryt::signed_dividet::validate(
525540
const smt_termt &lhs,
526541
const smt_termt &rhs)
527542
{
528-
validate_bit_vector_operator_arguments(lhs, rhs);
543+
validate_matched_bit_vector_sorts(lhs, rhs);
529544
}
530545

531546
const smt_function_application_termt::factoryt<
@@ -548,7 +563,7 @@ void smt_bit_vector_theoryt::unsigned_remaindert::validate(
548563
const smt_termt &lhs,
549564
const smt_termt &rhs)
550565
{
551-
validate_bit_vector_operator_arguments(lhs, rhs);
566+
validate_matched_bit_vector_sorts(lhs, rhs);
552567
}
553568

554569
const smt_function_application_termt::factoryt<
@@ -571,7 +586,7 @@ void smt_bit_vector_theoryt::signed_remaindert::validate(
571586
const smt_termt &lhs,
572587
const smt_termt &rhs)
573588
{
574-
validate_bit_vector_operator_arguments(lhs, rhs);
589+
validate_matched_bit_vector_sorts(lhs, rhs);
575590
}
576591

577592
const smt_function_application_termt::factoryt<
@@ -590,8 +605,7 @@ smt_sortt smt_bit_vector_theoryt::negatet::return_sort(const smt_termt &operand)
590605

591606
void smt_bit_vector_theoryt::negatet::validate(const smt_termt &operand)
592607
{
593-
const auto operand_sort = operand.get_sort().cast<smt_bit_vector_sortt>();
594-
INVARIANT(operand_sort, "The operand is expected to have a bit-vector sort.");
608+
validate_bit_vector_sort(operand);
595609
}
596610

597611
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::negatet>
@@ -613,7 +627,7 @@ void smt_bit_vector_theoryt::shift_leftt::validate(
613627
const smt_termt &lhs,
614628
const smt_termt &rhs)
615629
{
616-
validate_bit_vector_operator_arguments(lhs, rhs);
630+
validate_matched_bit_vector_sorts(lhs, rhs);
617631
}
618632

619633
const smt_function_application_termt::factoryt<
@@ -636,7 +650,7 @@ void smt_bit_vector_theoryt::logical_shift_rightt::validate(
636650
const smt_termt &lhs,
637651
const smt_termt &rhs)
638652
{
639-
validate_bit_vector_operator_arguments(lhs, rhs);
653+
validate_matched_bit_vector_sorts(lhs, rhs);
640654
}
641655

642656
const smt_function_application_termt::factoryt<
@@ -659,7 +673,7 @@ void smt_bit_vector_theoryt::arithmetic_shift_rightt::validate(
659673
const smt_termt &lhs,
660674
const smt_termt &rhs)
661675
{
662-
validate_bit_vector_operator_arguments(lhs, rhs);
676+
validate_matched_bit_vector_sorts(lhs, rhs);
663677
}
664678

665679
const smt_function_application_termt::factoryt<

0 commit comments

Comments
 (0)