CBMC
Loading...
Searching...
No Matches
std_types.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Pre-defined types
4
5Author: Daniel Kroening, kroening@kroening.com
6 Maria Svorenova, maria.svorenova@diffblue.com
7
8\*******************************************************************/
9
12
13#include "std_types.h"
14
15#include "arith_tools.h"
16#include "c_types.h"
17#include "namespace.h"
18#include "std_expr.h"
19
20void array_typet::check(const typet &type, const validation_modet vm)
21{
22 PRECONDITION(type.id() == ID_array);
24 const array_typet &array_type = static_cast<const array_typet &>(type);
25 if(array_type.size().is_nil())
26 {
28 vm,
29 array_type.size() == nil_exprt{},
30 "nil array size must be exactly nil");
31 }
32}
33
35{
36 // For backwards compatibility, allow the case that the array type is
37 // not annotated with an index type.
38 const auto &annotated_type =
39 static_cast<const typet &>(find(ID_C_index_type));
40 if(annotated_type.is_nil())
41 return c_index_type();
42 else
43 return annotated_type;
44}
45
48 const irep_idt &component_name) const
49{
50 std::size_t number=0;
51
52 for(const auto &c : components())
53 {
54 if(c.get_name() == component_name)
55 return number;
56
57 number++;
58 }
59
61}
62
65 const irep_idt &component_name) const
66{
67 for(const auto &c : components())
68 {
69 if(c.get_name() == component_name)
70 return c;
71 }
72
73 return static_cast<const componentt &>(get_nil_irep());
74}
75
76const typet &
77struct_union_typet::component_type(const irep_idt &component_name) const
78{
79 const auto &c = get_component(component_name);
80 CHECK_RETURN(c.is_not_nil());
81 return c.type();
82}
83
88
93
98
100{
101 bases().push_back(baset(base));
102}
103
104std::optional<struct_typet::baset>
106{
107 for(const auto &b : bases())
108 {
109 if(to_struct_tag_type(b.type()).get_identifier() == id)
110 return b;
111 }
112 return {};
113}
114
120{
121 const componentst &ot_components=other.components();
123
124 if(ot_components.size()<
125 tt_components.size())
126 return false;
127
128 componentst::const_iterator
129 ot_it=ot_components.begin();
130
131 for(const auto &tt_c : tt_components)
132 {
133 if(ot_it->type() != tt_c.type() || ot_it->get_name() != tt_c.get_name())
134 {
135 return false; // they just don't match
136 }
137
138 ot_it++;
139 }
140
141 return true; // ok, *this is a prefix of ot
142}
143
145bool is_reference(const typet &type)
146{
147 return type.id()==ID_pointer &&
149}
150
152bool is_rvalue_reference(const typet &type)
153{
154 return type.id()==ID_pointer &&
156}
157
158std::size_t bitvector_typet::width() const
159{
160 return get_size_t(ID_width);
161}
162
164{
165 set_width(numeric_cast_v<std::size_t>(width));
166}
167
168bool range_typet::includes(const mp_integer &singleton) const
169{
170 return get_from() <= singleton && singleton <= get_to();
171}
172
174{
175 PRECONDITION(includes(1));
176 return constant_exprt{ID_1, *this};
177}
178
180{
181 PRECONDITION(includes(0));
182 return constant_exprt{ID_0, *this};
183}
184
189
194
199
204
215 const typet &type,
216 const namespacet &ns)
217{
218 // Helper function to avoid the code duplication in the branches
219 // below.
220 const auto has_constant_components = [&ns](const typet &subtype) -> bool {
221 if(subtype.id() == ID_struct || subtype.id() == ID_union)
222 {
223 const auto &struct_union_type = to_struct_union_type(subtype);
224 for(const auto &component : struct_union_type.components())
225 {
227 return true;
228 }
229 }
230 return false;
231 };
232
233 // There are 4 possibilities the code below is handling.
234 // The possibilities are enumerated as comments, to show
235 // what each code is supposed to be handling. For more
236 // comprehensive test case for this, take a look at
237 // regression/cbmc/no_nondet_static/main.c
238
239 // const int a;
240 if(type.get_bool(ID_C_constant))
241 return true;
242
243 // This is a termination condition to break the recursion
244 // for recursive types such as the following:
245 // struct list { const int datum; struct list * next; };
246 // NOTE: the difference between this condition and the previous
247 // one is that this one always returns.
248 if(type.id() == ID_pointer)
249 return type.get_bool(ID_C_constant);
250
251 // When we have a case like the following, we don't immediately
252 // see the struct t. Instead, we only get to see symbol t1, which
253 // we have to use the namespace to resolve to its definition:
254 // struct t { const int a; };
255 // struct t t1;
256 if(type.id() == ID_struct_tag)
257 {
258 const auto &resolved_type = ns.follow_tag(to_struct_tag_type(type));
260 }
261 else if(type.id() == ID_union_tag)
262 {
263 const auto &resolved_type = ns.follow_tag(to_union_tag_type(type));
265 }
266
267 // In a case like this, where we see an array (b[3] here), we know that
268 // the array contains subtypes. We get the first one, and
269 // then resolve it to its definition through the usage of the namespace.
270 // struct contains_constant_pointer { int x; int * const p; };
271 // struct contains_constant_pointer b[3] = { {23, &y}, {23, &y}, {23, &y} };
272 if(type.has_subtype())
273 {
274 const auto &subtype = to_type_with_subtype(type).subtype();
275 return is_constant_or_has_constant_components(subtype, ns);
276 }
277
278 return false;
279}
280
284 constant_exprt _size)
286{
287 index_type_nonconst() = std::move(_index_type);
288 size() = std::move(_size);
289}
290
292{
293 // For backwards compatibility, allow the case that the array type is
294 // not annotated with an index type.
295 const auto &annotated_type =
296 static_cast<const typet &>(find(ID_C_index_type));
297 if(annotated_type.is_nil())
298 return c_index_type();
299 else
300 return annotated_type;
301}
302
304{
305 return static_cast<const constant_exprt &>(find(ID_size));
306}
307
309{
310 return static_cast<constant_exprt &>(add(ID_size));
311}
bitvector_typet c_index_type()
Definition c_types.cpp:16
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition c_types.h:224
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Arrays with given size.
Definition std_types.h:807
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Definition std_types.cpp:20
typet index_type() const
The type of the index expressions into any instance of this type.
Definition std_types.cpp:34
std::size_t width() const
A constant literal expression.
Definition std_expr.h:3117
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Base class for all expressions.
Definition expr.h:56
typet & type()
Return the type of the expression.
Definition expr.h:84
bool get_bool(const irep_idt &name) const
Definition irep.cpp:57
std::size_t get_size_t(const irep_idt &name) const
Definition irep.cpp:67
const irept & find(const irep_idt &name) const
Definition irep.cpp:93
void set(const irep_idt &name, const irep_idt &value)
Definition irep.h:412
const irep_idt & id() const
Definition irep.h:388
irept & add(const irep_idt &name)
Definition irep.cpp:103
const std::string & get_string(const irep_idt &name) const
Definition irep.h:401
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition namespace.cpp:49
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The NIL expression.
Definition std_expr.h:3208
void set_to(const mp_integer &to)
constant_exprt zero_expr() const
void set_from(const mp_integer &_from)
mp_integer get_to() const
bool includes(const mp_integer &) const
constant_exprt one_expr() const
mp_integer get_from() const
A struct tag type, i.e., struct_typet with an identifier.
Definition std_types.h:493
Base class or struct that a class or struct inherits from.
Definition std_types.h:252
baset(struct_tag_typet base)
Definition std_types.cpp:94
struct_tag_typet & type()
Definition std_types.cpp:84
Structure type, corresponds to C style structs.
Definition std_types.h:231
const basest & bases() const
Get the collection of base classes/structs.
Definition std_types.h:262
std::optional< baset > get_base(const irep_idt &id) const
Return the base with the given name, if exists.
bool is_prefix_of(const struct_typet &other) const
Returns true if the struct is a prefix of other, i.e., if this struct has n components then the compo...
void add_base(const struct_tag_typet &base)
Add a base class/struct.
Definition std_types.cpp:99
const typet & component_type(const irep_idt &component_name) const
Definition std_types.cpp:77
const componentst & components() const
Definition std_types.h:147
const componentt & get_component(const irep_idt &component_name) const
Get the reference to a component with given name.
Definition std_types.cpp:64
std::size_t component_number(const irep_idt &component_name) const
Return the sequence number of the component with given name.
Definition std_types.cpp:47
std::vector< componentt > componentst
Definition std_types.h:140
Type with a single subtype.
Definition type.h:180
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Definition type.h:199
The type of an expression, extends irept.
Definition type.h:29
bool has_subtype() const
Definition type.h:64
vector_typet(typet index_type, typet element_type, constant_exprt size)
const constant_exprt & size() const
typet & index_type_nonconst()
The type of any index expression into an instance of this type.
Definition std_types.h:1074
typet index_type() const
The type of any index expression into an instance of this type.
const irept & get_nil_irep()
Definition irep.cpp:19
const mp_integer string2integer(const std::string &n, unsigned base)
Definition mp_arith.cpp:54
const std::string integer2string(const mp_integer &n, unsigned base)
Definition mp_arith.cpp:103
STL namespace.
bool is_reference(const typet &type)
Returns true if the type is a reference.
bool is_rvalue_reference(const typet &type)
Returns if the type is an R value reference.
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define PRECONDITION(CONDITION)
Definition invariant.h:463
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition std_expr.cpp:97
API to expression classes.
Pre-defined types.
bool is_constant_or_has_constant_components(const typet &type, const namespacet &ns)
Identify whether a given type is constant itself or contains constant components.
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition std_types.h:518
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition std_types.h:214
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition type.h:208
#define DATA_CHECK(vm, condition, message)
This macro takes a condition which denotes a well-formedness criterion on goto programs,...
Definition validate.h:22
validation_modet