Skip to content

Commit fc47fe3

Browse files
committed
flattening/arrays.cpp: replace throw and assert with invariants
1 parent cf0875a commit fc47fe3

File tree

1 file changed

+78
-41
lines changed

1 file changed

+78
-41
lines changed

src/solvers/flattening/arrays.cpp

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Author: Daniel Kroening, [email protected]
77
\*******************************************************************/
88

99

10-
#include <cassert>
1110
#include <iostream>
1211

1312
#include <langapi/language_util.h>
@@ -50,10 +49,12 @@ literalt arrayst::record_array_equality(
5049
if(!base_type_eq(op0.type(), op1.type(), ns))
5150
{
5251
prop.error() << equality.pretty() << messaget::eom;
53-
throw "record_array_equality got equality without matching types";
52+
DATA_INVARIANT(false, "record_array_equality got equality without matching types");
5453
}
5554

56-
assert(ns.follow(op0.type()).id()==ID_array);
55+
DATA_INVARIANT(
56+
ns.follow(op0.type()).id()==ID_array,
57+
"record_array_equality parameter should be array-typed");
5758

5859
array_equalities.push_back(array_equalityt());
5960

@@ -109,14 +110,15 @@ void arrayst::collect_arrays(const exprt &a)
109110

110111
if(a.id()==ID_with)
111112
{
112-
if(a.operands().size()!=3)
113-
throw "with expected to have three operands";
113+
DATA_INVARIANT(
114+
a.operands().size()==3,
115+
"with expected to have three operands");
114116

115117
// check types
116118
if(!base_type_eq(array_type, a.op0().type(), ns))
117119
{
118120
prop.error() << a.pretty() << messaget::eom;
119-
throw "collect_arrays got 'with' without matching types";
121+
DATA_INVARIANT(false, "collect_arrays got 'with' without matching types");
120122
}
121123

122124
arrays.make_union(a, a.op0());
@@ -131,14 +133,15 @@ void arrayst::collect_arrays(const exprt &a)
131133
}
132134
else if(a.id()==ID_update) // TODO: is this obsolete?
133135
{
134-
if(a.operands().size()!=3)
135-
throw "update expected to have three operands";
136+
DATA_INVARIANT(
137+
a.operands().size()==3,
138+
"update expected to have three operands");
136139

137140
// check types
138141
if(!base_type_eq(array_type, a.op0().type(), ns))
139142
{
140143
prop.error() << a.pretty() << messaget::eom;
141-
throw "collect_arrays got 'update' without matching types";
144+
DATA_INVARIANT(false, "collect_arrays got 'update' without matching types");
142145
}
143146

144147
arrays.make_union(a, a.op0());
@@ -155,21 +158,22 @@ void arrayst::collect_arrays(const exprt &a)
155158
}
156159
else if(a.id()==ID_if)
157160
{
158-
if(a.operands().size()!=3)
159-
throw "if expected to have three operands";
161+
DATA_INVARIANT(
162+
a.operands().size()==3,
163+
"if expected to have three operands");
160164

161165
// check types
162166
if(!base_type_eq(array_type, a.op1().type(), ns))
163167
{
164168
prop.error() << a.pretty() << messaget::eom;
165-
throw "collect_arrays got if without matching types";
169+
DATA_INVARIANT(false, "collect_arrays got if without matching types");
166170
}
167171

168172
// check types
169173
if(!base_type_eq(array_type, a.op2().type(), ns))
170174
{
171175
prop.error() << a.pretty() << messaget::eom;
172-
throw "collect_arrays got if without matching types";
176+
DATA_INVARIANT(false, "collect_arrays got if without matching types");
173177
}
174178

175179
arrays.make_union(a, a.op1());
@@ -185,9 +189,10 @@ void arrayst::collect_arrays(const exprt &a)
185189
}
186190
else if(a.id()==ID_member)
187191
{
188-
if(to_member_expr(a).struct_op().id()!=ID_symbol)
189-
throw
190-
"unexpected array expression: member with `"+a.op0().id_string()+"'";
192+
DATA_INVARIANT(
193+
to_member_expr(a).struct_op().id()==ID_symbol,
194+
("unexpected array expression: member with `"+
195+
a.op0().id_string()+"'").c_str());
191196
}
192197
else if(a.id()==ID_constant ||
193198
a.id()==ID_array ||
@@ -200,20 +205,24 @@ void arrayst::collect_arrays(const exprt &a)
200205
else if(a.id()==ID_byte_update_little_endian ||
201206
a.id()==ID_byte_update_big_endian)
202207
{
203-
assert(0);
208+
DATA_INVARIANT(
209+
false,
210+
"byte_update should be removed before collect_arrays");
204211
}
205212
else if(a.id()==ID_typecast)
206213
{
207214
// cast between array types?
208-
assert(a.operands().size()==1);
215+
DATA_INVARIANT(
216+
a.operands().size()==1,
217+
"typecast must have one operand");
209218

210-
if(a.op0().type().id()==ID_array)
211-
{
212-
arrays.make_union(a, a.op0());
213-
collect_arrays(a.op0());
214-
}
215-
else
216-
throw "unexpected array type cast from "+a.op0().type().id_string();
219+
DATA_INVARIANT(
220+
a.op0().type().id()==ID_array,
221+
("unexpected array type cast from "+
222+
a.op0().type().id_string()).c_str());
223+
224+
arrays.make_union(a, a.op0());
225+
collect_arrays(a.op0());
217226
}
218227
else if(a.id()==ID_index)
219228
{
@@ -222,7 +231,12 @@ void arrayst::collect_arrays(const exprt &a)
222231
collect_arrays(a.op0());
223232
}
224233
else
225-
throw "unexpected array expression (collect_arrays): `"+a.id_string()+"'";
234+
{
235+
DATA_INVARIANT(
236+
false,
237+
("unexpected array expression (collect_arrays): `"+
238+
a.id_string()+"'").c_str());
239+
}
226240
}
227241

228242
/// adds array constraints (refine=true...lazily for the refinement loop)
@@ -364,7 +378,7 @@ void arrayst::update_index_map(std::size_t i)
364378
return;
365379

366380
std::size_t root_number=arrays.find_number(i);
367-
assert(root_number!=i);
381+
INVARIANT(root_number!=i, "is_root_number incorrect?");
368382

369383
index_sett &root_index_set=index_map[root_number];
370384
index_sett &index_set=index_map[i];
@@ -435,7 +449,9 @@ void arrayst::add_array_constraints(
435449
index_expr2.array()=array_equality.f2;
436450
index_expr2.index()=*it;
437451

438-
assert(index_expr1.type()==index_expr2.type());
452+
DATA_INVARIANT(
453+
index_expr1.type()==index_expr2.type(),
454+
"array elements should all have same type");
439455

440456
array_equalityt equal;
441457
equal.f1 = index_expr1;
@@ -477,12 +493,14 @@ void arrayst::add_array_constraints(
477493
else if(expr.id()==ID_byte_update_little_endian ||
478494
expr.id()==ID_byte_update_big_endian)
479495
{
480-
assert(0);
496+
INVARIANT(false, "byte_update should be removed before arrayst");
481497
}
482498
else if(expr.id()==ID_typecast)
483499
{
484500
// we got a=(type[])b
485-
assert(expr.operands().size()==1);
501+
DATA_INVARIANT(
502+
expr.operands().size()==1,
503+
"typecast should have one operand");
486504

487505
// add a[i]=b[i]
488506
for(index_sett::const_iterator
@@ -500,7 +518,9 @@ void arrayst::add_array_constraints(
500518
index_expr2.array()=expr.op0();
501519
index_expr2.index()=*it;
502520

503-
assert(index_expr1.type()==index_expr2.type());
521+
DATA_INVARIANT(
522+
index_expr1.type()==index_expr2.type(),
523+
"array elements should all have same type");
504524

505525
// add constraint
506526
lazy_constraintt lazy(lazy_typet::ARRAY_TYPECAST,
@@ -512,9 +532,12 @@ void arrayst::add_array_constraints(
512532
{
513533
}
514534
else
515-
throw
516-
"unexpected array expression (add_array_constraints): `"+
517-
expr.id_string()+"'";
535+
{
536+
DATA_INVARIANT(
537+
false,
538+
("unexpected array expression (add_array_constraints): `"+
539+
expr.id_string()+"'").c_str());
540+
}
518541
}
519542

520543
void arrayst::add_array_constraints_with(
@@ -536,7 +559,9 @@ void arrayst::add_array_constraints_with(
536559
if(index_expr.type()!=value.type())
537560
{
538561
prop.error() << expr.pretty() << messaget::eom;
539-
throw "index_expr and value types should match";
562+
DATA_INVARIANT(
563+
false,
564+
"with-expression operand should match array element type");
540565
}
541566

542567
lazy_constraintt lazy(
@@ -575,7 +600,9 @@ void arrayst::add_array_constraints_with(
575600
index_expr2.array()=expr.op0();
576601
index_expr2.index()=other_index;
577602

578-
assert(index_expr1.type()==index_expr2.type());
603+
DATA_INVARIANT(
604+
index_expr1.type()==index_expr2.type(),
605+
"array elements should all have same type");
579606

580607
equal_exprt equality_expr(index_expr1, index_expr2);
581608

@@ -620,7 +647,9 @@ void arrayst::add_array_constraints_update(
620647
if(index_expr.type()!=value.type())
621648
{
622649
prop.error() << expr.pretty() << messaget::eom;
623-
throw "index_expr and value types should match";
650+
DATA_INVARIANT(
651+
false,
652+
"update operand should match array element type");
624653
}
625654

626655
set_to_true(equal_exprt(index_expr, value));
@@ -657,7 +686,9 @@ void arrayst::add_array_constraints_update(
657686
index_expr2.array()=expr.op0();
658687
index_expr2.index()=other_index;
659688

660-
assert(index_expr1.type()==index_expr2.type());
689+
DATA_INVARIANT(
690+
index_expr1.type()==index_expr2.type(),
691+
"array elements should all have same type");
661692

662693
equal_exprt equality_expr(index_expr1, index_expr2);
663694

@@ -693,7 +724,9 @@ void arrayst::add_array_constraints_array_of(
693724
index_expr.array()=expr;
694725
index_expr.index()=*it;
695726

696-
assert(base_type_eq(index_expr.type(), expr.op0().type(), ns));
727+
DATA_INVARIANT(
728+
base_type_eq(index_expr.type(), expr.op0().type(), ns),
729+
"array_of operand type should match array element type");
697730

698731
// add constraint
699732
lazy_constraintt lazy(
@@ -730,7 +763,9 @@ void arrayst::add_array_constraints_if(
730763
index_expr2.array()=expr.true_case();
731764
index_expr2.index()=*it;
732765

733-
assert(index_expr1.type()==index_expr2.type());
766+
DATA_INVARIANT(
767+
index_expr1.type()==index_expr2.type(),
768+
"array elements should all have same type");
734769

735770
// add implication
736771
lazy_constraintt lazy(lazy_typet::ARRAY_IF,
@@ -759,7 +794,9 @@ void arrayst::add_array_constraints_if(
759794
index_expr2.array()=expr.false_case();
760795
index_expr2.index()=*it;
761796

762-
assert(index_expr1.type()==index_expr2.type());
797+
DATA_INVARIANT(
798+
index_expr1.type()==index_expr2.type(),
799+
"array elements should all have same type");
763800

764801
// add implication
765802
lazy_constraintt lazy(

0 commit comments

Comments
 (0)