-
Notifications
You must be signed in to change notification settings - Fork 274
added cond_exprt #3236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added cond_exprt #3236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,8 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include <util/invariant.h> | ||
|
||
bvt boolbvt::convert_cond(const exprt &expr) | ||
bvt boolbvt::convert_cond(const cond_exprt &expr) | ||
{ | ||
PRECONDITION(expr.id() == ID_cond); | ||
|
||
const exprt::operandst &operands=expr.operands(); | ||
|
||
std::size_t width=boolbv_width(expr.type()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4750,4 +4750,49 @@ inline void validate_expr(const popcount_exprt &value) | |
validate_operands(value, 1, "popcount must have one operand"); | ||
} | ||
|
||
/// this is a parametric version of an if-expression: it returns | ||
/// the value of the first case (using the ordering of the operands) | ||
/// whose condition evaluates to true. | ||
class cond_exprt : public multi_ary_exprt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems easily confused with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that something like that would be better; however, ID_cond is the existing name, and renaming should be a separate job. The added class simply documents the current state. |
||
{ | ||
public: | ||
explicit cond_exprt(const typet &_type) : multi_ary_exprt(ID_cond, _type) | ||
{ | ||
} | ||
|
||
/// adds a case to a cond expression | ||
/// \param condition: the condition for the case | ||
/// \param value: the value for the case | ||
void add_case(const exprt &condition, const exprt &value) | ||
{ | ||
PRECONDITION(condition.type().id() == ID_bool); | ||
operands().reserve(operands().size() + 2); | ||
operands().push_back(condition); | ||
operands().push_back(value); | ||
} | ||
}; | ||
|
||
/// \brief Cast an exprt to a \ref cond_exprt | ||
/// | ||
/// \a expr must be known to be \ref cond_exprt. | ||
/// | ||
/// \param expr: Source expression | ||
/// \return Object of type \ref cond_exprt | ||
inline const cond_exprt &to_cond_expr(const exprt &expr) | ||
{ | ||
PRECONDITION(expr.id() == ID_cond); | ||
DATA_INVARIANT( | ||
expr.operands().size() % 2 != 0, "cond must have even number of operands"); | ||
return static_cast<const cond_exprt &>(expr); | ||
} | ||
|
||
/// \copydoc to_popcount_expr(const exprt &) | ||
inline cond_exprt &to_cond_expr(exprt &expr) | ||
{ | ||
PRECONDITION(expr.id() == ID_cond); | ||
DATA_INVARIANT( | ||
expr.operands().size() % 2 != 0, "cond must have even number of operands"); | ||
return static_cast<cond_exprt &>(expr); | ||
} | ||
|
||
#endif // CPROVER_UTIL_STD_EXPR_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No else case? It's undefined if none of the conditions pass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is bugging me as well.
The origin of this expression is SMV, where it is common to end with a 'true' case.
If you don't, you get non-determinism.