Skip to content

Commit 969f614

Browse files
danpoePetr Bauch
authored and
Petr Bauch
committed
Add and use validation methods in equal_exprt
1 parent 80a611c commit 969f614

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/util/std_expr.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
1313
/// \file util/std_expr.h
1414
/// API to expression classes
1515

16+
#include "base_type.h"
1617
#include "expr_cast.h"
1718
#include "invariant.h"
1819
#include "mathematical_types.h"
@@ -1414,6 +1415,26 @@ class equal_exprt:public binary_relation_exprt
14141415
binary_relation_exprt(_lhs, ID_equal, _rhs)
14151416
{
14161417
}
1418+
1419+
void check(const validation_modet vm = validation_modet::INVARIANT) const
1420+
{
1421+
DATA_CHECK(operands().size() == 2, "equality must have two operands");
1422+
}
1423+
1424+
void validate(
1425+
const namespacet &ns,
1426+
const validation_modet vm = validation_modet::INVARIANT) const
1427+
{
1428+
check(vm);
1429+
1430+
// check types
1431+
DATA_CHECK(
1432+
base_type_eq(lhs().type(), rhs().type(), ns),
1433+
"lhs and rhs should have same type");
1434+
DATA_CHECK(
1435+
type().id() == ID_bool,
1436+
"result of equal expression should be of type bool");
1437+
}
14171438
};
14181439

14191440
/// \brief Cast an exprt to an \ref equal_exprt
@@ -1425,16 +1446,18 @@ class equal_exprt:public binary_relation_exprt
14251446
inline const equal_exprt &to_equal_expr(const exprt &expr)
14261447
{
14271448
PRECONDITION(expr.id()==ID_equal);
1428-
DATA_INVARIANT(expr.operands().size()==2, "Equality must have two operands");
1429-
return static_cast<const equal_exprt &>(expr);
1449+
const equal_exprt &ret = static_cast<const equal_exprt &>(expr);
1450+
ret.check();
1451+
return ret;
14301452
}
14311453

14321454
/// \copydoc to_equal_expr(const exprt &)
14331455
inline equal_exprt &to_equal_expr(exprt &expr)
14341456
{
14351457
PRECONDITION(expr.id()==ID_equal);
1436-
DATA_INVARIANT(expr.operands().size()==2, "Equality must have two operands");
1437-
return static_cast<equal_exprt &>(expr);
1458+
equal_exprt &ret = static_cast<equal_exprt &>(expr);
1459+
ret.check();
1460+
return ret;
14381461
}
14391462

14401463
template<> inline bool can_cast_expr<equal_exprt>(const exprt &base)

0 commit comments

Comments
 (0)