Skip to content

Commit 3ae261e

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

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

src/solvers/smt2_incremental/smt_bit_vector_theory.cpp

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,28 @@ 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
33+
validate_bit_vector_sorts(const smt_termt &lhs, const smt_termt &rhs)
34+
{
35+
validate_bit_vector_sort("Left ", lhs);
36+
validate_bit_vector_sort("Right ", rhs);
37+
}
38+
2239
void smt_bit_vector_theoryt::concatt::validate(
2340
const smt_termt &lhs,
2441
const smt_termt &rhs)
2542
{
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.");
43+
validate_bit_vector_sorts(lhs, rhs);
3044
}
3145

3246
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::concatt>
@@ -63,18 +77,14 @@ smt_bit_vector_theoryt::extract(std::size_t i, std::size_t j)
6377
return smt_function_application_termt::factoryt<extractt>(i, j);
6478
}
6579

66-
static void validate_bit_vector_operator_arguments(
67-
const smt_termt &left,
68-
const smt_termt &right)
80+
static void
81+
validate_matched_bit_vector_sorts(const smt_termt &left, const smt_termt &right)
6982
{
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.");
83+
validate_bit_vector_sorts(left, right);
7484
// The below invariant is based on the smtlib standard.
7585
// See http://smtlib.cs.uiowa.edu/logics-all.shtml#QF_BV
7686
INVARIANT(
77-
left_sort->bit_width() == right_sort->bit_width(),
87+
left.get_sort() == right.get_sort(),
7888
"Left and right operands must have the same bit width.");
7989
}
8090

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

93103
void smt_bit_vector_theoryt::nott::validate(const smt_termt &operand)
94104
{
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.");
105+
validate_bit_vector_sort("The", operand);
97106
}
98107

99108
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nott>
@@ -115,7 +124,7 @@ void smt_bit_vector_theoryt::andt::validate(
115124
const smt_termt &lhs,
116125
const smt_termt &rhs)
117126
{
118-
validate_bit_vector_operator_arguments(lhs, rhs);
127+
validate_matched_bit_vector_sorts(lhs, rhs);
119128
}
120129

121130
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::andt>
@@ -137,7 +146,7 @@ void smt_bit_vector_theoryt::ort::validate(
137146
const smt_termt &lhs,
138147
const smt_termt &rhs)
139148
{
140-
validate_bit_vector_operator_arguments(lhs, rhs);
149+
validate_matched_bit_vector_sorts(lhs, rhs);
141150
}
142151

143152
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::ort>
@@ -159,7 +168,7 @@ void smt_bit_vector_theoryt::nandt::validate(
159168
const smt_termt &lhs,
160169
const smt_termt &rhs)
161170
{
162-
validate_bit_vector_operator_arguments(lhs, rhs);
171+
validate_matched_bit_vector_sorts(lhs, rhs);
163172
}
164173

165174
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nandt>
@@ -181,7 +190,7 @@ void smt_bit_vector_theoryt::nort::validate(
181190
const smt_termt &lhs,
182191
const smt_termt &rhs)
183192
{
184-
validate_bit_vector_operator_arguments(lhs, rhs);
193+
validate_matched_bit_vector_sorts(lhs, rhs);
185194
}
186195

187196
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nort>
@@ -203,7 +212,7 @@ void smt_bit_vector_theoryt::xort::validate(
203212
const smt_termt &lhs,
204213
const smt_termt &rhs)
205214
{
206-
validate_bit_vector_operator_arguments(lhs, rhs);
215+
validate_matched_bit_vector_sorts(lhs, rhs);
207216
}
208217

209218
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::xort>
@@ -225,7 +234,7 @@ void smt_bit_vector_theoryt::xnort::validate(
225234
const smt_termt &lhs,
226235
const smt_termt &rhs)
227236
{
228-
validate_bit_vector_operator_arguments(lhs, rhs);
237+
validate_matched_bit_vector_sorts(lhs, rhs);
229238
}
230239

231240
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::xnort>
@@ -249,7 +258,7 @@ void smt_bit_vector_theoryt::unsigned_less_thant::validate(
249258
const smt_termt &lhs,
250259
const smt_termt &rhs)
251260
{
252-
validate_bit_vector_operator_arguments(lhs, rhs);
261+
validate_matched_bit_vector_sorts(lhs, rhs);
253262
}
254263

255264
const smt_function_application_termt::factoryt<
@@ -272,7 +281,7 @@ void smt_bit_vector_theoryt::unsigned_less_than_or_equalt::validate(
272281
const smt_termt &lhs,
273282
const smt_termt &rhs)
274283
{
275-
validate_bit_vector_operator_arguments(lhs, rhs);
284+
validate_matched_bit_vector_sorts(lhs, rhs);
276285
}
277286

278287
const smt_function_application_termt::factoryt<
@@ -295,7 +304,7 @@ void smt_bit_vector_theoryt::unsigned_greater_thant::validate(
295304
const smt_termt &lhs,
296305
const smt_termt &rhs)
297306
{
298-
validate_bit_vector_operator_arguments(lhs, rhs);
307+
validate_matched_bit_vector_sorts(lhs, rhs);
299308
}
300309

301310
const smt_function_application_termt::factoryt<
@@ -319,7 +328,7 @@ void smt_bit_vector_theoryt::unsigned_greater_than_or_equalt::validate(
319328
const smt_termt &lhs,
320329
const smt_termt &rhs)
321330
{
322-
validate_bit_vector_operator_arguments(lhs, rhs);
331+
validate_matched_bit_vector_sorts(lhs, rhs);
323332
}
324333

325334
const smt_function_application_termt::factoryt<
@@ -342,7 +351,7 @@ void smt_bit_vector_theoryt::signed_less_thant::validate(
342351
const smt_termt &lhs,
343352
const smt_termt &rhs)
344353
{
345-
validate_bit_vector_operator_arguments(lhs, rhs);
354+
validate_matched_bit_vector_sorts(lhs, rhs);
346355
}
347356

348357
const smt_function_application_termt::factoryt<
@@ -365,7 +374,7 @@ void smt_bit_vector_theoryt::signed_less_than_or_equalt::validate(
365374
const smt_termt &lhs,
366375
const smt_termt &rhs)
367376
{
368-
validate_bit_vector_operator_arguments(lhs, rhs);
377+
validate_matched_bit_vector_sorts(lhs, rhs);
369378
}
370379

371380
const smt_function_application_termt::factoryt<
@@ -388,7 +397,7 @@ void smt_bit_vector_theoryt::signed_greater_thant::validate(
388397
const smt_termt &lhs,
389398
const smt_termt &rhs)
390399
{
391-
validate_bit_vector_operator_arguments(lhs, rhs);
400+
validate_matched_bit_vector_sorts(lhs, rhs);
392401
}
393402

394403
const smt_function_application_termt::factoryt<
@@ -411,7 +420,7 @@ void smt_bit_vector_theoryt::signed_greater_than_or_equalt::validate(
411420
const smt_termt &lhs,
412421
const smt_termt &rhs)
413422
{
414-
validate_bit_vector_operator_arguments(lhs, rhs);
423+
validate_matched_bit_vector_sorts(lhs, rhs);
415424
}
416425

417426
const smt_function_application_termt::factoryt<
@@ -434,7 +443,7 @@ void smt_bit_vector_theoryt::addt::validate(
434443
const smt_termt &lhs,
435444
const smt_termt &rhs)
436445
{
437-
validate_bit_vector_operator_arguments(lhs, rhs);
446+
validate_matched_bit_vector_sorts(lhs, rhs);
438447
}
439448

440449
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::addt>
@@ -456,7 +465,7 @@ void smt_bit_vector_theoryt::subtractt::validate(
456465
const smt_termt &lhs,
457466
const smt_termt &rhs)
458467
{
459-
validate_bit_vector_operator_arguments(lhs, rhs);
468+
validate_matched_bit_vector_sorts(lhs, rhs);
460469
}
461470

462471
const smt_function_application_termt::factoryt<
@@ -479,7 +488,7 @@ void smt_bit_vector_theoryt::multiplyt::validate(
479488
const smt_termt &lhs,
480489
const smt_termt &rhs)
481490
{
482-
validate_bit_vector_operator_arguments(lhs, rhs);
491+
validate_matched_bit_vector_sorts(lhs, rhs);
483492
}
484493

485494
const smt_function_application_termt::factoryt<
@@ -502,7 +511,7 @@ void smt_bit_vector_theoryt::unsigned_dividet::validate(
502511
const smt_termt &lhs,
503512
const smt_termt &rhs)
504513
{
505-
validate_bit_vector_operator_arguments(lhs, rhs);
514+
validate_matched_bit_vector_sorts(lhs, rhs);
506515
}
507516

508517
const smt_function_application_termt::factoryt<
@@ -525,7 +534,7 @@ void smt_bit_vector_theoryt::signed_dividet::validate(
525534
const smt_termt &lhs,
526535
const smt_termt &rhs)
527536
{
528-
validate_bit_vector_operator_arguments(lhs, rhs);
537+
validate_matched_bit_vector_sorts(lhs, rhs);
529538
}
530539

531540
const smt_function_application_termt::factoryt<
@@ -548,7 +557,7 @@ void smt_bit_vector_theoryt::unsigned_remaindert::validate(
548557
const smt_termt &lhs,
549558
const smt_termt &rhs)
550559
{
551-
validate_bit_vector_operator_arguments(lhs, rhs);
560+
validate_matched_bit_vector_sorts(lhs, rhs);
552561
}
553562

554563
const smt_function_application_termt::factoryt<
@@ -571,7 +580,7 @@ void smt_bit_vector_theoryt::signed_remaindert::validate(
571580
const smt_termt &lhs,
572581
const smt_termt &rhs)
573582
{
574-
validate_bit_vector_operator_arguments(lhs, rhs);
583+
validate_matched_bit_vector_sorts(lhs, rhs);
575584
}
576585

577586
const smt_function_application_termt::factoryt<
@@ -590,8 +599,7 @@ smt_sortt smt_bit_vector_theoryt::negatet::return_sort(const smt_termt &operand)
590599

591600
void smt_bit_vector_theoryt::negatet::validate(const smt_termt &operand)
592601
{
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.");
602+
validate_bit_vector_sort("The", operand);
595603
}
596604

597605
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::negatet>
@@ -613,7 +621,7 @@ void smt_bit_vector_theoryt::shift_leftt::validate(
613621
const smt_termt &lhs,
614622
const smt_termt &rhs)
615623
{
616-
validate_bit_vector_operator_arguments(lhs, rhs);
624+
validate_matched_bit_vector_sorts(lhs, rhs);
617625
}
618626

619627
const smt_function_application_termt::factoryt<
@@ -636,7 +644,7 @@ void smt_bit_vector_theoryt::logical_shift_rightt::validate(
636644
const smt_termt &lhs,
637645
const smt_termt &rhs)
638646
{
639-
validate_bit_vector_operator_arguments(lhs, rhs);
647+
validate_matched_bit_vector_sorts(lhs, rhs);
640648
}
641649

642650
const smt_function_application_termt::factoryt<
@@ -659,7 +667,7 @@ void smt_bit_vector_theoryt::arithmetic_shift_rightt::validate(
659667
const smt_termt &lhs,
660668
const smt_termt &rhs)
661669
{
662-
validate_bit_vector_operator_arguments(lhs, rhs);
670+
validate_matched_bit_vector_sorts(lhs, rhs);
663671
}
664672

665673
const smt_function_application_termt::factoryt<

0 commit comments

Comments
 (0)