Skip to content

Commit 9ce0da5

Browse files
committed
check type invariant of type_with_subtypet
This commit does two things: 1) The type invariant of type_with_subtypet (that there is one subtype) is now checked when casting to it. 2) The check in .subtype() that there is a subtype is removed.
1 parent cedd5ea commit 9ce0da5

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/util/pointer_expr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class pointer_typet : public bitvector_typet
5454
const typet &type,
5555
const validation_modet vm = validation_modet::INVARIANT)
5656
{
57+
type_with_subtypet::check(type);
5758
DATA_CHECK(vm, !type.get(ID_width).empty(), "pointer must have width");
5859
}
5960
};
@@ -116,6 +117,8 @@ class reference_typet : public pointer_typet
116117
const validation_modet vm = validation_modet::INVARIANT)
117118
{
118119
PRECONDITION(type.id() == ID_pointer);
120+
DATA_CHECK(
121+
vm, type.get_sub().size() == 1, "reference must have one type parameter");
119122
const reference_typet &reference_type =
120123
static_cast<const reference_typet &>(type);
121124
DATA_CHECK(

src/util/std_types.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Author: Daniel Kroening, [email protected]
1919
void array_typet::check(const typet &type, const validation_modet vm)
2020
{
2121
PRECONDITION(type.id() == ID_array);
22+
type_with_subtypet::check(type);
2223
const array_typet &array_type = static_cast<const array_typet &>(type);
2324
if(array_type.size().is_nil())
2425
{

src/util/std_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,13 +832,15 @@ inline bool can_cast_type<array_typet>(const typet &type)
832832
inline const array_typet &to_array_type(const typet &type)
833833
{
834834
PRECONDITION(can_cast_type<array_typet>(type));
835+
array_typet::check(type);
835836
return static_cast<const array_typet &>(type);
836837
}
837838

838839
/// \copydoc to_array_type(const typet &)
839840
inline array_typet &to_array_type(typet &type)
840841
{
841842
PRECONDITION(can_cast_type<array_typet>(type));
843+
array_typet::check(type);
842844
return static_cast<array_typet &>(type);
843845
}
844846

src/util/type.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
1616
class namespacet;
1717

1818
#include "source_location.h"
19+
#include "validate.h"
1920
#include "validate_types.h"
2021
#include "validation_mode.h"
2122

@@ -63,7 +64,9 @@ class typet:public irept
6364
{ return !get_sub().empty(); }
6465

6566
bool has_subtype() const
66-
{ return !get_sub().empty(); }
67+
{
68+
return get_sub().size() == 1;
69+
}
6770

6871
void remove_subtype()
6972
{ get_sub().clear(); }
@@ -149,17 +152,37 @@ class type_with_subtypet:public typet
149152
: typet(std::move(_id), std::move(_subtype))
150153
{
151154
}
155+
156+
const typet &subtype() const
157+
{
158+
// the existence of get_sub().front() is established by check()
159+
return static_cast<const typet &>(get_sub().front());
160+
}
161+
162+
typet &subtype()
163+
{
164+
// the existence of get_sub().front() is established by check()
165+
return static_cast<typet &>(get_sub().front());
166+
}
167+
168+
static void check(
169+
const typet &type,
170+
const validation_modet vm = validation_modet::INVARIANT)
171+
{
172+
DATA_CHECK(
173+
vm, type.get_sub().size() == 1, "type must have one type parameter");
174+
}
152175
};
153176

154177
inline const type_with_subtypet &to_type_with_subtype(const typet &type)
155178
{
156-
PRECONDITION(type.has_subtype());
179+
type_with_subtypet::check(type);
157180
return static_cast<const type_with_subtypet &>(type);
158181
}
159182

160183
inline type_with_subtypet &to_type_with_subtype(typet &type)
161184
{
162-
PRECONDITION(type.has_subtype());
185+
type_with_subtypet::check(type);
163186
return static_cast<type_with_subtypet &>(type);
164187
}
165188

0 commit comments

Comments
 (0)