Skip to content

Commit 1a44efa

Browse files
author
Daniel Kroening
authored
Merge pull request #3236 from diffblue/cond_exprt
added cond_exprt
2 parents e401667 + c4810ef commit 1a44efa

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
207207
else if(expr.id()==ID_case)
208208
return convert_case(expr);
209209
else if(expr.id()==ID_cond)
210-
return convert_cond(expr);
210+
return convert_cond(to_cond_expr(expr));
211211
else if(expr.id()==ID_if)
212212
return convert_if(to_if_expr(expr));
213213
else if(expr.id()==ID_constant)
@@ -481,7 +481,7 @@ literalt boolbvt::convert_rest(const exprt &expr)
481481
}
482482
else if(expr.id()==ID_cond)
483483
{
484-
bvt bv=convert_cond(expr);
484+
bvt bv = convert_cond(to_cond_expr(expr));
485485
CHECK_RETURN(bv.size() == 1);
486486
return bv[0];
487487
}

src/solvers/flattening/boolbv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class boolbvt:public arrayst
161161
virtual bvt convert_with(const exprt &expr);
162162
virtual bvt convert_update(const exprt &expr);
163163
virtual bvt convert_case(const exprt &expr);
164-
virtual bvt convert_cond(const exprt &expr);
164+
virtual bvt convert_cond(const cond_exprt &);
165165
virtual bvt convert_shift(const binary_exprt &expr);
166166
virtual bvt convert_bitwise(const exprt &expr);
167167
virtual bvt convert_unary_minus(const unary_minus_exprt &expr);

src/solvers/flattening/boolbv_cond.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <util/invariant.h>
1212

13-
bvt boolbvt::convert_cond(const exprt &expr)
13+
bvt boolbvt::convert_cond(const cond_exprt &expr)
1414
{
15-
PRECONDITION(expr.id() == ID_cond);
16-
1715
const exprt::operandst &operands=expr.operands();
1816

1917
std::size_t width=boolbv_width(expr.type());

src/util/std_expr.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,4 +4749,49 @@ inline void validate_expr(const popcount_exprt &value)
47494749
validate_operands(value, 1, "popcount must have one operand");
47504750
}
47514751

4752+
/// this is a parametric version of an if-expression: it returns
4753+
/// the value of the first case (using the ordering of the operands)
4754+
/// whose condition evaluates to true.
4755+
class cond_exprt : public multi_ary_exprt
4756+
{
4757+
public:
4758+
explicit cond_exprt(const typet &_type) : multi_ary_exprt(ID_cond, _type)
4759+
{
4760+
}
4761+
4762+
/// adds a case to a cond expression
4763+
/// \param condition: the condition for the case
4764+
/// \param value: the value for the case
4765+
void add_case(const exprt &condition, const exprt &value)
4766+
{
4767+
PRECONDITION(condition.type().id() == ID_bool);
4768+
operands().reserve(operands().size() + 2);
4769+
operands().push_back(condition);
4770+
operands().push_back(value);
4771+
}
4772+
};
4773+
4774+
/// \brief Cast an exprt to a \ref cond_exprt
4775+
///
4776+
/// \a expr must be known to be \ref cond_exprt.
4777+
///
4778+
/// \param expr: Source expression
4779+
/// \return Object of type \ref cond_exprt
4780+
inline const cond_exprt &to_cond_expr(const exprt &expr)
4781+
{
4782+
PRECONDITION(expr.id() == ID_cond);
4783+
DATA_INVARIANT(
4784+
expr.operands().size() % 2 != 0, "cond must have even number of operands");
4785+
return static_cast<const cond_exprt &>(expr);
4786+
}
4787+
4788+
/// \copydoc to_popcount_expr(const exprt &)
4789+
inline cond_exprt &to_cond_expr(exprt &expr)
4790+
{
4791+
PRECONDITION(expr.id() == ID_cond);
4792+
DATA_INVARIANT(
4793+
expr.operands().size() % 2 != 0, "cond must have even number of operands");
4794+
return static_cast<cond_exprt &>(expr);
4795+
}
4796+
47524797
#endif // CPROVER_UTIL_STD_EXPR_H

0 commit comments

Comments
 (0)