Skip to content

Commit b4f1f10

Browse files
committed
add nullary_exprt::check(expr)
This adds nullary_exprt::check(expr), which checks that the expression is indeed nullary. This follows the same pattern as binary_exprt::check and ternary_exprt::check.
1 parent 3c3012b commit b4f1f10

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

src/util/std_expr.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,27 @@ bool constant_exprt::value_is_zero_string() const
2323

2424
void constant_exprt::check(const exprt &expr, const validation_modet vm)
2525
{
26-
DATA_CHECK(
27-
vm, !expr.has_operands(), "constant expression must not have operands");
26+
nullary_exprt::check(expr, vm);
27+
28+
const auto value = expr.get(ID_value);
2829

2930
DATA_CHECK(
3031
vm,
31-
!can_cast_type<bitvector_typet>(expr.type()) ||
32-
!id2string(to_constant_expr(expr).get_value()).empty(),
32+
!can_cast_type<bitvector_typet>(expr.type()) || !value.empty(),
3333
"bitvector constant must have a non-empty value");
3434

3535
DATA_CHECK(
3636
vm,
3737
!can_cast_type<bitvector_typet>(expr.type()) ||
3838
can_cast_type<pointer_typet>(expr.type()) ||
39-
id2string(to_constant_expr(expr).get_value())
40-
.find_first_not_of("0123456789ABCDEF") == std::string::npos,
39+
id2string(value).find_first_not_of("0123456789ABCDEF") ==
40+
std::string::npos,
4141
"negative bitvector constant must use two's complement");
4242

4343
DATA_CHECK(
4444
vm,
45-
!can_cast_type<bitvector_typet>(expr.type()) ||
46-
to_constant_expr(expr).get_value() == ID_0 ||
47-
id2string(to_constant_expr(expr).get_value())[0] != '0',
45+
!can_cast_type<bitvector_typet>(expr.type()) || value == ID_0 ||
46+
id2string(value)[0] != '0',
4847
"bitvector constant must not have leading zeros");
4948
}
5049

src/util/std_expr.h

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ class nullary_exprt : public expr_protectedt
2626
{
2727
}
2828

29+
static void check(
30+
const exprt &expr,
31+
const validation_modet vm = validation_modet::INVARIANT)
32+
{
33+
DATA_CHECK(
34+
vm,
35+
expr.operands().size() == 0,
36+
"nullary expression must not have operands");
37+
}
38+
39+
static void validate(
40+
const exprt &expr,
41+
const namespacet &,
42+
const validation_modet vm = validation_modet::INVARIANT)
43+
{
44+
check(expr, vm);
45+
}
46+
2947
/// remove all operand methods
3048
operandst &operands() = delete;
3149
const operandst &operands() const = delete;
@@ -293,19 +311,16 @@ inline void validate_expr(const nondet_symbol_exprt &value)
293311
inline const nondet_symbol_exprt &to_nondet_symbol_expr(const exprt &expr)
294312
{
295313
PRECONDITION(expr.id()==ID_nondet_symbol);
296-
const nondet_symbol_exprt &ret =
297-
static_cast<const nondet_symbol_exprt &>(expr);
298-
validate_expr(ret);
299-
return ret;
314+
nondet_symbol_exprt::check(expr);
315+
return static_cast<const nondet_symbol_exprt &>(expr);
300316
}
301317

302318
/// \copydoc to_nondet_symbol_expr(const exprt &)
303319
inline nondet_symbol_exprt &to_nondet_symbol_expr(exprt &expr)
304320
{
305321
PRECONDITION(expr.id()==ID_symbol);
306-
nondet_symbol_exprt &ret = static_cast<nondet_symbol_exprt &>(expr);
307-
validate_expr(ret);
308-
return ret;
322+
nondet_symbol_exprt::check(expr);
323+
return static_cast<nondet_symbol_exprt &>(expr);
309324
}
310325

311326

@@ -1804,18 +1819,16 @@ inline void validate_expr(const empty_union_exprt &value)
18041819
inline const empty_union_exprt &to_empty_union_expr(const exprt &expr)
18051820
{
18061821
PRECONDITION(expr.id() == ID_empty_union);
1807-
const empty_union_exprt &ret = static_cast<const empty_union_exprt &>(expr);
1808-
validate_expr(ret);
1809-
return ret;
1822+
empty_union_exprt::check(expr);
1823+
return static_cast<const empty_union_exprt &>(expr);
18101824
}
18111825

18121826
/// \copydoc to_empty_union_expr(const exprt &)
18131827
inline empty_union_exprt &to_empty_union_expr(exprt &expr)
18141828
{
18151829
PRECONDITION(expr.id() == ID_empty_union);
1816-
empty_union_exprt &ret = static_cast<empty_union_exprt &>(expr);
1817-
validate_expr(ret);
1818-
return ret;
1830+
empty_union_exprt::check(expr);
1831+
return static_cast<empty_union_exprt &>(expr);
18191832
}
18201833

18211834
/// \brief Struct constructor from list of elements
@@ -2921,16 +2934,16 @@ inline bool can_cast_expr<type_exprt>(const exprt &base)
29212934
inline const type_exprt &to_type_expr(const exprt &expr)
29222935
{
29232936
PRECONDITION(can_cast_expr<type_exprt>(expr));
2924-
const type_exprt &ret = static_cast<const type_exprt &>(expr);
2925-
return ret;
2937+
type_exprt::check(expr);
2938+
return static_cast<const type_exprt &>(expr);
29262939
}
29272940

29282941
/// \copydoc to_type_expr(const exprt &)
29292942
inline type_exprt &to_type_expr(exprt &expr)
29302943
{
29312944
PRECONDITION(can_cast_expr<type_exprt>(expr));
2932-
type_exprt &ret = static_cast<type_exprt &>(expr);
2933-
return ret;
2945+
type_exprt::check(expr);
2946+
return static_cast<type_exprt &>(expr);
29342947
}
29352948

29362949
/// \brief A constant literal expression
@@ -2988,13 +3001,15 @@ inline void validate_expr(const constant_exprt &value)
29883001
inline const constant_exprt &to_constant_expr(const exprt &expr)
29893002
{
29903003
PRECONDITION(expr.is_constant());
3004+
constant_exprt::check(expr);
29913005
return static_cast<const constant_exprt &>(expr);
29923006
}
29933007

29943008
/// \copydoc to_constant_expr(const exprt &)
29953009
inline constant_exprt &to_constant_expr(exprt &expr)
29963010
{
29973011
PRECONDITION(expr.is_constant());
3012+
constant_exprt::check(expr);
29983013
return static_cast<constant_exprt &>(expr);
29993014
}
30003015

@@ -3534,10 +3549,8 @@ inline const class_method_descriptor_exprt &
35343549
to_class_method_descriptor_expr(const exprt &expr)
35353550
{
35363551
PRECONDITION(expr.id() == ID_virtual_function);
3537-
const class_method_descriptor_exprt &ret =
3538-
static_cast<const class_method_descriptor_exprt &>(expr);
3539-
validate_expr(ret);
3540-
return ret;
3552+
class_method_descriptor_exprt::check(expr);
3553+
return static_cast<const class_method_descriptor_exprt &>(expr);
35413554
}
35423555

35433556
template <>

0 commit comments

Comments
 (0)