Skip to content

Commit 53c53f3

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

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

src/solvers/smt2_incremental/smt_bit_vector_theory.cpp

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,33 @@ 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(const smt_termt &operand)
33+
{
34+
validate_bit_vector_sort("The", operand);
35+
}
36+
37+
static void
38+
validate_bit_vector_sorts(const smt_termt &lhs, const smt_termt &rhs)
39+
{
40+
validate_bit_vector_sort("Left", lhs);
41+
validate_bit_vector_sort("Right", rhs);
42+
}
43+
2244
void smt_bit_vector_theoryt::concatt::validate(
2345
const smt_termt &lhs,
2446
const smt_termt &rhs)
2547
{
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.");
48+
validate_bit_vector_sorts(lhs, rhs);
3049
}
3150

3251
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::concatt>
@@ -63,18 +82,14 @@ smt_bit_vector_theoryt::extract(std::size_t i, std::size_t j)
6382
return smt_function_application_termt::factoryt<extractt>(i, j);
6483
}
6584

66-
static void validate_bit_vector_operator_arguments(
67-
const smt_termt &left,
68-
const smt_termt &right)
85+
static void
86+
validate_matched_bit_vector_sorts(const smt_termt &left, const smt_termt &right)
6987
{
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.");
88+
validate_bit_vector_sorts(left, right);
7489
// The below invariant is based on the smtlib standard.
7590
// See http://smtlib.cs.uiowa.edu/logics-all.shtml#QF_BV
7691
INVARIANT(
77-
left_sort->bit_width() == right_sort->bit_width(),
92+
left.get_sort() == right.get_sort(),
7893
"Left and right operands must have the same bit width.");
7994
}
8095

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

93108
void smt_bit_vector_theoryt::nott::validate(const smt_termt &operand)
94109
{
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.");
110+
validate_bit_vector_sort(operand);
97111
}
98112

99113
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nott>
@@ -115,7 +129,7 @@ void smt_bit_vector_theoryt::andt::validate(
115129
const smt_termt &lhs,
116130
const smt_termt &rhs)
117131
{
118-
validate_bit_vector_operator_arguments(lhs, rhs);
132+
validate_matched_bit_vector_sorts(lhs, rhs);
119133
}
120134

121135
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::andt>
@@ -137,7 +151,7 @@ void smt_bit_vector_theoryt::ort::validate(
137151
const smt_termt &lhs,
138152
const smt_termt &rhs)
139153
{
140-
validate_bit_vector_operator_arguments(lhs, rhs);
154+
validate_matched_bit_vector_sorts(lhs, rhs);
141155
}
142156

143157
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::ort>
@@ -159,7 +173,7 @@ void smt_bit_vector_theoryt::nandt::validate(
159173
const smt_termt &lhs,
160174
const smt_termt &rhs)
161175
{
162-
validate_bit_vector_operator_arguments(lhs, rhs);
176+
validate_matched_bit_vector_sorts(lhs, rhs);
163177
}
164178

165179
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nandt>
@@ -181,7 +195,7 @@ void smt_bit_vector_theoryt::nort::validate(
181195
const smt_termt &lhs,
182196
const smt_termt &rhs)
183197
{
184-
validate_bit_vector_operator_arguments(lhs, rhs);
198+
validate_matched_bit_vector_sorts(lhs, rhs);
185199
}
186200

187201
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::nort>
@@ -203,7 +217,7 @@ void smt_bit_vector_theoryt::xort::validate(
203217
const smt_termt &lhs,
204218
const smt_termt &rhs)
205219
{
206-
validate_bit_vector_operator_arguments(lhs, rhs);
220+
validate_matched_bit_vector_sorts(lhs, rhs);
207221
}
208222

209223
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::xort>
@@ -225,7 +239,7 @@ void smt_bit_vector_theoryt::xnort::validate(
225239
const smt_termt &lhs,
226240
const smt_termt &rhs)
227241
{
228-
validate_bit_vector_operator_arguments(lhs, rhs);
242+
validate_matched_bit_vector_sorts(lhs, rhs);
229243
}
230244

231245
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::xnort>
@@ -249,7 +263,7 @@ void smt_bit_vector_theoryt::unsigned_less_thant::validate(
249263
const smt_termt &lhs,
250264
const smt_termt &rhs)
251265
{
252-
validate_bit_vector_operator_arguments(lhs, rhs);
266+
validate_matched_bit_vector_sorts(lhs, rhs);
253267
}
254268

255269
const smt_function_application_termt::factoryt<
@@ -272,7 +286,7 @@ void smt_bit_vector_theoryt::unsigned_less_than_or_equalt::validate(
272286
const smt_termt &lhs,
273287
const smt_termt &rhs)
274288
{
275-
validate_bit_vector_operator_arguments(lhs, rhs);
289+
validate_matched_bit_vector_sorts(lhs, rhs);
276290
}
277291

278292
const smt_function_application_termt::factoryt<
@@ -295,7 +309,7 @@ void smt_bit_vector_theoryt::unsigned_greater_thant::validate(
295309
const smt_termt &lhs,
296310
const smt_termt &rhs)
297311
{
298-
validate_bit_vector_operator_arguments(lhs, rhs);
312+
validate_matched_bit_vector_sorts(lhs, rhs);
299313
}
300314

301315
const smt_function_application_termt::factoryt<
@@ -319,7 +333,7 @@ void smt_bit_vector_theoryt::unsigned_greater_than_or_equalt::validate(
319333
const smt_termt &lhs,
320334
const smt_termt &rhs)
321335
{
322-
validate_bit_vector_operator_arguments(lhs, rhs);
336+
validate_matched_bit_vector_sorts(lhs, rhs);
323337
}
324338

325339
const smt_function_application_termt::factoryt<
@@ -342,7 +356,7 @@ void smt_bit_vector_theoryt::signed_less_thant::validate(
342356
const smt_termt &lhs,
343357
const smt_termt &rhs)
344358
{
345-
validate_bit_vector_operator_arguments(lhs, rhs);
359+
validate_matched_bit_vector_sorts(lhs, rhs);
346360
}
347361

348362
const smt_function_application_termt::factoryt<
@@ -365,7 +379,7 @@ void smt_bit_vector_theoryt::signed_less_than_or_equalt::validate(
365379
const smt_termt &lhs,
366380
const smt_termt &rhs)
367381
{
368-
validate_bit_vector_operator_arguments(lhs, rhs);
382+
validate_matched_bit_vector_sorts(lhs, rhs);
369383
}
370384

371385
const smt_function_application_termt::factoryt<
@@ -388,7 +402,7 @@ void smt_bit_vector_theoryt::signed_greater_thant::validate(
388402
const smt_termt &lhs,
389403
const smt_termt &rhs)
390404
{
391-
validate_bit_vector_operator_arguments(lhs, rhs);
405+
validate_matched_bit_vector_sorts(lhs, rhs);
392406
}
393407

394408
const smt_function_application_termt::factoryt<
@@ -411,7 +425,7 @@ void smt_bit_vector_theoryt::signed_greater_than_or_equalt::validate(
411425
const smt_termt &lhs,
412426
const smt_termt &rhs)
413427
{
414-
validate_bit_vector_operator_arguments(lhs, rhs);
428+
validate_matched_bit_vector_sorts(lhs, rhs);
415429
}
416430

417431
const smt_function_application_termt::factoryt<
@@ -434,7 +448,7 @@ void smt_bit_vector_theoryt::addt::validate(
434448
const smt_termt &lhs,
435449
const smt_termt &rhs)
436450
{
437-
validate_bit_vector_operator_arguments(lhs, rhs);
451+
validate_matched_bit_vector_sorts(lhs, rhs);
438452
}
439453

440454
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::addt>
@@ -456,7 +470,7 @@ void smt_bit_vector_theoryt::subtractt::validate(
456470
const smt_termt &lhs,
457471
const smt_termt &rhs)
458472
{
459-
validate_bit_vector_operator_arguments(lhs, rhs);
473+
validate_matched_bit_vector_sorts(lhs, rhs);
460474
}
461475

462476
const smt_function_application_termt::factoryt<
@@ -479,7 +493,7 @@ void smt_bit_vector_theoryt::multiplyt::validate(
479493
const smt_termt &lhs,
480494
const smt_termt &rhs)
481495
{
482-
validate_bit_vector_operator_arguments(lhs, rhs);
496+
validate_matched_bit_vector_sorts(lhs, rhs);
483497
}
484498

485499
const smt_function_application_termt::factoryt<
@@ -502,7 +516,7 @@ void smt_bit_vector_theoryt::unsigned_dividet::validate(
502516
const smt_termt &lhs,
503517
const smt_termt &rhs)
504518
{
505-
validate_bit_vector_operator_arguments(lhs, rhs);
519+
validate_matched_bit_vector_sorts(lhs, rhs);
506520
}
507521

508522
const smt_function_application_termt::factoryt<
@@ -525,7 +539,7 @@ void smt_bit_vector_theoryt::signed_dividet::validate(
525539
const smt_termt &lhs,
526540
const smt_termt &rhs)
527541
{
528-
validate_bit_vector_operator_arguments(lhs, rhs);
542+
validate_matched_bit_vector_sorts(lhs, rhs);
529543
}
530544

531545
const smt_function_application_termt::factoryt<
@@ -548,7 +562,7 @@ void smt_bit_vector_theoryt::unsigned_remaindert::validate(
548562
const smt_termt &lhs,
549563
const smt_termt &rhs)
550564
{
551-
validate_bit_vector_operator_arguments(lhs, rhs);
565+
validate_matched_bit_vector_sorts(lhs, rhs);
552566
}
553567

554568
const smt_function_application_termt::factoryt<
@@ -571,7 +585,7 @@ void smt_bit_vector_theoryt::signed_remaindert::validate(
571585
const smt_termt &lhs,
572586
const smt_termt &rhs)
573587
{
574-
validate_bit_vector_operator_arguments(lhs, rhs);
588+
validate_matched_bit_vector_sorts(lhs, rhs);
575589
}
576590

577591
const smt_function_application_termt::factoryt<
@@ -590,8 +604,7 @@ smt_sortt smt_bit_vector_theoryt::negatet::return_sort(const smt_termt &operand)
590604

591605
void smt_bit_vector_theoryt::negatet::validate(const smt_termt &operand)
592606
{
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.");
607+
validate_bit_vector_sort(operand);
595608
}
596609

597610
const smt_function_application_termt::factoryt<smt_bit_vector_theoryt::negatet>
@@ -613,7 +626,7 @@ void smt_bit_vector_theoryt::shift_leftt::validate(
613626
const smt_termt &lhs,
614627
const smt_termt &rhs)
615628
{
616-
validate_bit_vector_operator_arguments(lhs, rhs);
629+
validate_matched_bit_vector_sorts(lhs, rhs);
617630
}
618631

619632
const smt_function_application_termt::factoryt<
@@ -636,7 +649,7 @@ void smt_bit_vector_theoryt::logical_shift_rightt::validate(
636649
const smt_termt &lhs,
637650
const smt_termt &rhs)
638651
{
639-
validate_bit_vector_operator_arguments(lhs, rhs);
652+
validate_matched_bit_vector_sorts(lhs, rhs);
640653
}
641654

642655
const smt_function_application_termt::factoryt<
@@ -659,7 +672,7 @@ void smt_bit_vector_theoryt::arithmetic_shift_rightt::validate(
659672
const smt_termt &lhs,
660673
const smt_termt &rhs)
661674
{
662-
validate_bit_vector_operator_arguments(lhs, rhs);
675+
validate_matched_bit_vector_sorts(lhs, rhs);
663676
}
664677

665678
const smt_function_application_termt::factoryt<

0 commit comments

Comments
 (0)