cprover
std_types.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Pre-defined types
4 
5 Author: Daniel Kroening, kroening@kroening.com
6  Maria Svorenova, maria.svorenova@diffblue.com
7 
8 \*******************************************************************/
9 
12 
13 #include "std_types.h"
14 
15 #include "namespace.h"
16 #include "std_expr.h"
17 
18 void array_typet::check(const typet &type, const validation_modet vm)
19 {
20  PRECONDITION(type.id() == ID_array);
21  const array_typet &array_type = static_cast<const array_typet &>(type);
22  if(array_type.size().is_nil())
23  {
24  DATA_CHECK(
25  vm,
26  array_type.size() == nil_exprt{},
27  "nil array size must be exactly nil");
28  }
29 }
30 
33  const irep_idt &component_name) const
34 {
35  std::size_t number=0;
36 
37  for(const auto &c : components())
38  {
39  if(c.get_name() == component_name)
40  return number;
41 
42  number++;
43  }
44 
46 }
47 
50  const irep_idt &component_name) const
51 {
52  for(const auto &c : components())
53  {
54  if(c.get_name() == component_name)
55  return c;
56  }
57 
58  return static_cast<const componentt &>(get_nil_irep());
59 }
60 
61 const typet &
62 struct_union_typet::component_type(const irep_idt &component_name) const
63 {
64  const auto &c = get_component(component_name);
65  CHECK_RETURN(c.is_not_nil());
66  return c.type();
67 }
68 
70 {
72 }
73 
75 {
77 }
78 
80  : exprt(ID_base, std::move(base))
81 {
82 }
83 
85 {
86  bases().push_back(baset(base));
87 }
88 
90 {
91  for(const auto &b : bases())
92  {
93  if(to_struct_tag_type(b.type()).get_identifier() == id)
94  return b;
95  }
96  return {};
97 }
98 
103 bool struct_typet::is_prefix_of(const struct_typet &other) const
104 {
105  const componentst &ot_components=other.components();
106  const componentst &tt_components=components();
107 
108  if(ot_components.size()<
109  tt_components.size())
110  return false;
111 
112  componentst::const_iterator
113  ot_it=ot_components.begin();
114 
115  for(const auto &tt_c : tt_components)
116  {
117  if(ot_it->type() != tt_c.type() || ot_it->get_name() != tt_c.get_name())
118  {
119  return false; // they just don't match
120  }
121 
122  ot_it++;
123  }
124 
125  return true; // ok, *this is a prefix of ot
126 }
127 
129 bool is_reference(const typet &type)
130 {
131  return type.id()==ID_pointer &&
132  type.get_bool(ID_C_reference);
133 }
134 
136 bool is_rvalue_reference(const typet &type)
137 {
138  return type.id()==ID_pointer &&
139  type.get_bool(ID_C_rvalue_reference);
140 }
141 
143 {
144  set(ID_from, integer2string(from));
145 }
146 
148 {
149  set(ID_to, integer2string(to));
150 }
151 
153 {
154  return string2integer(get_string(ID_from));
155 }
156 
158 {
159  return string2integer(get_string(ID_to));
160 }
161 
172  const typet &type,
173  const namespacet &ns)
174 {
175  // Helper function to avoid the code duplication in the branches
176  // below.
177  const auto has_constant_components = [&ns](const typet &subtype) -> bool {
178  if(subtype.id() == ID_struct || subtype.id() == ID_union)
179  {
180  const auto &struct_union_type = to_struct_union_type(subtype);
181  for(const auto &component : struct_union_type.components())
182  {
184  return true;
185  }
186  }
187  return false;
188  };
189 
190  // There are 4 possibilities the code below is handling.
191  // The possibilities are enumerated as comments, to show
192  // what each code is supposed to be handling. For more
193  // comprehensive test case for this, take a look at
194  // regression/cbmc/no_nondet_static/main.c
195 
196  // const int a;
197  if(type.get_bool(ID_C_constant))
198  return true;
199 
200  // This is a termination condition to break the recursion
201  // for recursive types such as the following:
202  // struct list { const int datum; struct list * next; };
203  // NOTE: the difference between this condition and the previous
204  // one is that this one always returns.
205  if(type.id() == ID_pointer)
206  return type.get_bool(ID_C_constant);
207 
208  // When we have a case like the following, we don't immediately
209  // see the struct t. Instead, we only get to see symbol t1, which
210  // we have to use the namespace to resolve to its definition:
211  // struct t { const int a; };
212  // struct t t1;
213  if(type.id() == ID_struct_tag || type.id() == ID_union_tag)
214  {
215  const auto &resolved_type = ns.follow(type);
216  return has_constant_components(resolved_type);
217  }
218 
219  // In a case like this, where we see an array (b[3] here), we know that
220  // the array contains subtypes. We get the first one, and
221  // then resolve it to its definition through the usage of the namespace.
222  // struct contains_constant_pointer { int x; int * const p; };
223  // struct contains_constant_pointer b[3] = { {23, &y}, {23, &y}, {23, &y} };
224  if(type.has_subtype())
225  {
226  const auto &subtype = to_type_with_subtype(type).subtype();
228  }
229 
230  return false;
231 }
232 
233 vector_typet::vector_typet(const typet &_subtype, const constant_exprt &_size)
234  : type_with_subtypet(ID_vector, _subtype)
235 {
236  size() = _size;
237 }
238 
240 {
241  return static_cast<const constant_exprt &>(find(ID_size));
242 }
243 
245 {
246  return static_cast<constant_exprt &>(add(ID_size));
247 }
Arrays with given size.
Definition: std_types.h:763
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Definition: std_types.cpp:18
const exprt & size() const
Definition: std_types.h:771
A constant literal expression.
Definition: std_expr.h:2753
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
typet & type()
Return the type of the expression.
Definition: expr.h:82
irept & add(const irep_namet &name)
Definition: irep.cpp:116
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:431
const std::string & get_string(const irep_namet &name) const
Definition: irep.h:420
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:58
tree_implementationt baset
Definition: irep.h:385
const irept & find(const irep_namet &name) const
Definition: irep.cpp:106
const irep_idt & id() const
Definition: irep.h:407
bool is_nil() const
Definition: irep.h:387
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
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:2820
void set_to(const mp_integer &to)
Definition: std_types.cpp:147
void set_from(const mp_integer &_from)
Definition: std_types.cpp:142
mp_integer get_to() const
Definition: std_types.cpp:157
mp_integer get_from() const
Definition: std_types.cpp:152
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:449
struct_tag_typet & type()
Definition: std_types.cpp:69
Structure type, corresponds to C style structs.
Definition: std_types.h:231
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...
Definition: std_types.cpp:103
void add_base(const struct_tag_typet &base)
Add a base class/struct.
Definition: std_types.cpp:84
const basest & bases() const
Get the collection of base classes/structs.
Definition: std_types.h:262
optionalt< baset > get_base(const irep_idt &id) const
Return the base with the given name, if exists.
Definition: std_types.cpp:89
const typet & component_type(const irep_idt &component_name) const
Definition: std_types.cpp:62
const componentt & get_component(const irep_idt &component_name) const
Get the reference to a component with given name.
Definition: std_types.cpp:49
const componentst & components() const
Definition: std_types.h:147
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:32
std::vector< componentt > componentst
Definition: std_types.h:140
const irep_idt & get_identifier() const
Definition: std_types.h:410
Type with a single subtype.
Definition: type.h:146
The type of an expression, extends irept.
Definition: type.h:28
const typet & subtype() const
Definition: type.h:47
bool has_subtype() const
Definition: type.h:65
vector_typet(const typet &_subtype, const constant_exprt &_size)
Definition: std_types.cpp:233
const constant_exprt & size() const
Definition: std_types.cpp:239
const irept & get_nil_irep()
Definition: irep.cpp:20
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
nonstd::optional< T > optionalt
Definition: optional.h:35
BigInt mp_integer
Definition: smt_terms.h:12
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
#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:48
API to expression classes.
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.
Definition: std_types.cpp:171
bool is_reference(const typet &type)
Returns true if the type is a reference.
Definition: std_types.cpp:129
bool is_rvalue_reference(const typet &type)
Returns if the type is an R value reference.
Definition: std_types.cpp:136
Pre-defined types.
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 struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:474
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition: type.h:154
#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