Skip to content

Commit d297f66

Browse files
committed
Migrate existing type checks to new framework
This removes the validate_type() methods and moves the checks into a new method check() of the corresponding type.
1 parent 29b76b4 commit d297f66

File tree

3 files changed

+68
-53
lines changed

3 files changed

+68
-53
lines changed

src/util/expr_cast.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ inline void validate_expr(const exprt &) {}
5454
/// program has entered an invalid state. We validate objects at cast time as
5555
/// that is when these checks have been used historically, but it would be
5656
/// reasonable to validate objects in this way at any time.
57-
inline void validate_type(const typet &)
58-
{
59-
}
57+
//inline void validate_type(const typet &)
58+
//{
59+
//}
6060

6161
namespace detail // NOLINT
6262
{
@@ -130,7 +130,7 @@ auto type_try_dynamic_cast(TType &base) ->
130130
if(!can_cast_type<typename std::remove_const<T>::type>(base))
131131
return nullptr;
132132
const auto ret = static_cast<returnt>(&base);
133-
validate_type(*ret);
133+
ret->check();
134134
return ret;
135135
}
136136

src/util/std_types.h

+42-47
Original file line numberDiff line numberDiff line change
@@ -1002,15 +1002,13 @@ inline bool can_cast_type<code_typet>(const typet &type)
10021002
inline const code_typet &to_code_type(const typet &type)
10031003
{
10041004
PRECONDITION(can_cast_type<code_typet>(type));
1005-
validate_type(type);
10061005
return static_cast<const code_typet &>(type);
10071006
}
10081007

10091008
/// \copydoc to_code_type(const typet &)
10101009
inline code_typet &to_code_type(typet &type)
10111010
{
10121011
PRECONDITION(can_cast_type<code_typet>(type));
1013-
validate_type(type);
10141012
return static_cast<code_typet &>(type);
10151013
}
10161014

@@ -1205,6 +1203,11 @@ class bv_typet:public bitvector_typet
12051203
{
12061204
set_width(width);
12071205
}
1206+
1207+
void check(const validation_modet vm = validation_modet::INVARIANT) const
1208+
{
1209+
DATA_CHECK(!get(ID_width).empty(), "bitvector type must have width");
1210+
}
12081211
};
12091212

12101213
/// Check whether a reference to a typet is a \ref bv_typet.
@@ -1216,11 +1219,6 @@ inline bool can_cast_type<bv_typet>(const typet &type)
12161219
return type.id() == ID_bv;
12171220
}
12181221

1219-
inline void validate_type(const bv_typet &type)
1220-
{
1221-
DATA_INVARIANT(!type.get(ID_width).empty(), "bitvector type must have width");
1222-
}
1223-
12241222
/// \brief Cast a typet to a \ref bv_typet
12251223
///
12261224
/// This is an unchecked conversion. \a type must be known to be \ref
@@ -1233,7 +1231,7 @@ inline const bv_typet &to_bv_type(const typet &type)
12331231
{
12341232
PRECONDITION(can_cast_type<bv_typet>(type));
12351233
const bv_typet &ret = static_cast<const bv_typet &>(type);
1236-
validate_type(ret);
1234+
ret.check();
12371235
return ret;
12381236
}
12391237

@@ -1242,7 +1240,7 @@ inline bv_typet &to_bv_type(typet &type)
12421240
{
12431241
PRECONDITION(can_cast_type<bv_typet>(type));
12441242
bv_typet &ret = static_cast<bv_typet &>(type);
1245-
validate_type(ret);
1243+
ret.check();
12461244
return ret;
12471245
}
12481246

@@ -1316,6 +1314,11 @@ class signedbv_typet:public bitvector_typet
13161314
constant_exprt smallest_expr() const;
13171315
constant_exprt zero_expr() const;
13181316
constant_exprt largest_expr() const;
1317+
1318+
void check(const validation_modet vm = validation_modet::INVARIANT) const
1319+
{
1320+
DATA_CHECK(!get(ID_width).empty(), "signed bitvector type must have width");
1321+
}
13191322
};
13201323

13211324
/// Check whether a reference to a typet is a \ref signedbv_typet.
@@ -1327,12 +1330,6 @@ inline bool can_cast_type<signedbv_typet>(const typet &type)
13271330
return type.id() == ID_signedbv;
13281331
}
13291332

1330-
inline void validate_type(const signedbv_typet &type)
1331-
{
1332-
DATA_INVARIANT(
1333-
!type.get(ID_width).empty(), "signed bitvector type must have width");
1334-
}
1335-
13361333
/// \brief Cast a typet to a \ref signedbv_typet
13371334
///
13381335
/// This is an unchecked conversion. \a type must be known to be \ref
@@ -1345,7 +1342,7 @@ inline const signedbv_typet &to_signedbv_type(const typet &type)
13451342
{
13461343
PRECONDITION(can_cast_type<signedbv_typet>(type));
13471344
const signedbv_typet &ret = static_cast<const signedbv_typet &>(type);
1348-
validate_type(ret);
1345+
ret.check();
13491346
return ret;
13501347
}
13511348

@@ -1354,7 +1351,7 @@ inline signedbv_typet &to_signedbv_type(typet &type)
13541351
{
13551352
PRECONDITION(can_cast_type<signedbv_typet>(type));
13561353
signedbv_typet &ret = static_cast<signedbv_typet &>(type);
1357-
validate_type(ret);
1354+
ret.check();
13581355
return ret;
13591356
}
13601357

@@ -1380,6 +1377,11 @@ class fixedbv_typet:public bitvector_typet
13801377
{
13811378
set(ID_integer_bits, b);
13821379
}
1380+
1381+
void check(const validation_modet vm = validation_modet::INVARIANT) const
1382+
{
1383+
DATA_CHECK(!get(ID_width).empty(), "fixed bitvector type must have width");
1384+
}
13831385
};
13841386

13851387
/// Check whether a reference to a typet is a \ref fixedbv_typet.
@@ -1391,12 +1393,6 @@ inline bool can_cast_type<fixedbv_typet>(const typet &type)
13911393
return type.id() == ID_fixedbv;
13921394
}
13931395

1394-
inline void validate_type(const fixedbv_typet &type)
1395-
{
1396-
DATA_INVARIANT(
1397-
!type.get(ID_width).empty(), "fixed bitvector type must have width");
1398-
}
1399-
14001396
/// \brief Cast a typet to a \ref fixedbv_typet
14011397
///
14021398
/// This is an unchecked conversion. \a type must be known to be \ref
@@ -1409,7 +1405,7 @@ inline const fixedbv_typet &to_fixedbv_type(const typet &type)
14091405
{
14101406
PRECONDITION(can_cast_type<fixedbv_typet>(type));
14111407
const fixedbv_typet &ret = static_cast<const fixedbv_typet &>(type);
1412-
validate_type(ret);
1408+
ret.check();
14131409
return ret;
14141410
}
14151411

@@ -1418,7 +1414,7 @@ inline fixedbv_typet &to_fixedbv_type(typet &type)
14181414
{
14191415
PRECONDITION(can_cast_type<fixedbv_typet>(type));
14201416
fixedbv_typet &ret = static_cast<fixedbv_typet &>(type);
1421-
validate_type(ret);
1417+
ret.check();
14221418
return ret;
14231419
}
14241420

@@ -1442,6 +1438,11 @@ class floatbv_typet:public bitvector_typet
14421438
{
14431439
set(ID_f, b);
14441440
}
1441+
1442+
void check(const validation_modet vm = validation_modet::INVARIANT) const
1443+
{
1444+
DATA_CHECK(!get(ID_width).empty(), "float bitvector type must have width");
1445+
}
14451446
};
14461447

14471448
/// Check whether a reference to a typet is a \ref floatbv_typet.
@@ -1453,12 +1454,6 @@ inline bool can_cast_type<floatbv_typet>(const typet &type)
14531454
return type.id() == ID_floatbv;
14541455
}
14551456

1456-
inline void validate_type(const floatbv_typet &type)
1457-
{
1458-
DATA_INVARIANT(
1459-
!type.get(ID_width).empty(), "float bitvector type must have width");
1460-
}
1461-
14621457
/// \brief Cast a typet to a \ref floatbv_typet
14631458
///
14641459
/// This is an unchecked conversion. \a type must be known to be \ref
@@ -1471,7 +1466,7 @@ inline const floatbv_typet &to_floatbv_type(const typet &type)
14711466
{
14721467
PRECONDITION(can_cast_type<floatbv_typet>(type));
14731468
const floatbv_typet &ret = static_cast<const floatbv_typet &>(type);
1474-
validate_type(ret);
1469+
ret.check();
14751470
return ret;
14761471
}
14771472

@@ -1480,7 +1475,7 @@ inline floatbv_typet &to_floatbv_type(typet &type)
14801475
{
14811476
PRECONDITION(can_cast_type<floatbv_typet>(type));
14821477
floatbv_typet &ret = static_cast<floatbv_typet &>(type);
1483-
validate_type(ret);
1478+
ret.check();
14841479
return ret;
14851480
}
14861481

@@ -1539,6 +1534,11 @@ class pointer_typet:public bitvector_typet
15391534
{
15401535
return signedbv_typet(get_width());
15411536
}
1537+
1538+
void check(const validation_modet vm = validation_modet::INVARIANT) const
1539+
{
1540+
DATA_CHECK(!get(ID_width).empty(), "pointer must have width");
1541+
}
15421542
};
15431543

15441544
/// Check whether a reference to a typet is a \ref pointer_typet.
@@ -1550,11 +1550,6 @@ inline bool can_cast_type<pointer_typet>(const typet &type)
15501550
return type.id() == ID_pointer;
15511551
}
15521552

1553-
inline void validate_type(const pointer_typet &type)
1554-
{
1555-
DATA_INVARIANT(!type.get(ID_width).empty(), "pointer must have width");
1556-
}
1557-
15581553
/// \brief Cast a typet to a \ref pointer_typet
15591554
///
15601555
/// This is an unchecked conversion. \a type must be known to be \ref
@@ -1567,7 +1562,7 @@ inline const pointer_typet &to_pointer_type(const typet &type)
15671562
{
15681563
PRECONDITION(can_cast_type<pointer_typet>(type));
15691564
const pointer_typet &ret = static_cast<const pointer_typet &>(type);
1570-
validate_type(ret);
1565+
ret.check();
15711566
return ret;
15721567
}
15731568

@@ -1576,7 +1571,7 @@ inline pointer_typet &to_pointer_type(typet &type)
15761571
{
15771572
PRECONDITION(can_cast_type<pointer_typet>(type));
15781573
pointer_typet &ret = static_cast<pointer_typet &>(type);
1579-
validate_type(ret);
1574+
ret.check();
15801575
return ret;
15811576
}
15821577

@@ -1641,6 +1636,11 @@ class c_bool_typet:public bitvector_typet
16411636
bitvector_typet(ID_c_bool, width)
16421637
{
16431638
}
1639+
1640+
void check(const validation_modet vm = validation_modet::INVARIANT) const
1641+
{
1642+
DATA_CHECK(!get(ID_width).empty(), "C bool type must have width");
1643+
}
16441644
};
16451645

16461646
/// Check whether a reference to a typet is a \ref c_bool_typet.
@@ -1652,11 +1652,6 @@ inline bool can_cast_type<c_bool_typet>(const typet &type)
16521652
return type.id() == ID_c_bool;
16531653
}
16541654

1655-
inline void validate_type(const c_bool_typet &type)
1656-
{
1657-
DATA_INVARIANT(!type.get(ID_width).empty(), "C bool type must have width");
1658-
}
1659-
16601655
/// \brief Cast a typet to a \ref c_bool_typet
16611656
///
16621657
/// This is an unchecked conversion. \a type must be known to be \ref
@@ -1669,7 +1664,7 @@ inline const c_bool_typet &to_c_bool_type(const typet &type)
16691664
{
16701665
PRECONDITION(can_cast_type<c_bool_typet>(type));
16711666
const c_bool_typet &ret = static_cast<const c_bool_typet &>(type);
1672-
validate_type(ret);
1667+
ret.check();
16731668
return ret;
16741669
}
16751670

@@ -1678,7 +1673,7 @@ inline c_bool_typet &to_c_bool_type(typet &type)
16781673
{
16791674
PRECONDITION(can_cast_type<c_bool_typet>(type));
16801675
c_bool_typet &ret = static_cast<c_bool_typet &>(type);
1681-
validate_type(ret);
1676+
ret.check();
16821677
return ret;
16831678
}
16841679

src/util/validate_types.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,34 @@ Author: Daniel Poetzl
2323
template <template <typename> class C, typename... Args>
2424
void call_on_type(const typet &type, Args &&... args)
2525
{
26-
if(type.id() == ID_signedbv)
26+
if(type.id() == ID_bv)
2727
{
28-
CALL_ON_TYPE(signedbv_typet);
28+
CALL_ON_TYPE(bv_typet);
2929
}
3030
else if(type.id() == ID_unsignedbv)
3131
{
3232
CALL_ON_TYPE(unsignedbv_typet);
3333
}
34+
else if(type.id() == ID_signedbv)
35+
{
36+
CALL_ON_TYPE(signedbv_typet);
37+
}
38+
else if(type.id() == ID_fixedbv)
39+
{
40+
CALL_ON_TYPE(fixedbv_typet);
41+
}
42+
else if(type.id() == ID_floatbv)
43+
{
44+
CALL_ON_TYPE(floatbv_typet);
45+
}
46+
else if(type.id() == ID_pointer)
47+
{
48+
CALL_ON_TYPE(pointer_typet);
49+
}
50+
else if(type.id() == ID_c_bool)
51+
{
52+
CALL_ON_TYPE(c_bool_typet);
53+
}
3454
else
3555
{
3656
#ifdef REPORT_UNIMPLEMENTED_TYPE_CHECKS

0 commit comments

Comments
 (0)