CBMC
abstract_aggregate_object.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3  Module: analyses variable-sensitivity
4 
5  Author: Jez Higgins, jez@jezuk.co.uk
6 
7 \*******************************************************************/
8 
12 #ifndef CBMC_ABSTRACT_AGGREGATE_OBJECT_H
13 #define CBMC_ABSTRACT_AGGREGATE_OBJECT_H
14 
15 #include <util/namespace.h>
16 
19 
21 
22 #include <stack>
23 
25 {
26 };
27 
28 template <class aggregate_typet, class aggregate_traitst>
31 {
32 public:
33  abstract_aggregate_objectt(const typet &type, bool tp, bool bttm)
34  : abstract_objectt(type, tp, bttm)
35  {
37  type.id() != ID_struct_tag || ID_struct == aggregate_traitst::TYPE_ID());
39  type.id() != ID_union_tag || ID_union == aggregate_traitst::TYPE_ID());
41  type.id() == ID_struct_tag || type.id() == ID_union_tag ||
42  type.id() == aggregate_traitst::TYPE_ID());
43  }
44 
46  const exprt &expr,
47  const abstract_environmentt &environment,
48  const namespacet &ns)
49  : abstract_objectt(expr, environment, ns)
50  {
52  expr.type().id() != ID_struct_tag ||
53  ID_struct == aggregate_traitst::TYPE_ID());
55  expr.type().id() != ID_union_tag ||
56  ID_union == aggregate_traitst::TYPE_ID());
58  expr.type().id() == ID_struct_tag || expr.type().id() == ID_union_tag ||
59  expr.type().id() == aggregate_traitst::TYPE_ID());
60  }
61 
63  const exprt &expr,
64  const std::vector<abstract_object_pointert> &operands,
65  const abstract_environmentt &environment,
66  const namespacet &ns) const override
67  {
68  if(expr.id() == aggregate_traitst::ACCESS_EXPR_ID())
69  return read_component(environment, expr, ns);
70 
72  expr, operands, environment, ns);
73  }
74 
76  abstract_environmentt &environment,
77  const namespacet &ns,
78  const std::stack<exprt> &stack,
79  const exprt &specifier,
80  const abstract_object_pointert &value,
81  bool merging_write) const override
82  {
83  return write_component(
84  environment, ns, stack, specifier, value, merging_write);
85  }
86 
89  abstract_object_visitedt &visited,
90  const abstract_environmentt &env,
91  const namespacet &ns) const override
92  {
94  aggregate_traitst::get_statistics(statistics, visited, env, ns);
95  this->statistics(statistics, visited, env, ns);
96  }
97 
98 protected:
100  const abstract_environmentt &environment,
101  const exprt &expr,
102  const namespacet &ns) const
103  {
104  // If we are bottom then so are the components
105  // otherwise the components could be anything
106  return environment.abstract_object_factory(
107  aggregate_traitst::read_type(expr.type(), type()),
108  ns,
109  !is_bottom(),
110  is_bottom());
111  }
112 
114  abstract_environmentt &environment,
115  const namespacet &ns,
116  const std::stack<exprt> &stack,
117  const exprt &expr,
118  const abstract_object_pointert &value,
119  bool merging_write) const
120  {
121  if(is_top() || is_bottom())
122  {
123  return shared_from_this();
124  }
125  else
126  {
127  return std::make_shared<aggregate_typet>(type(), true, false);
128  }
129  }
130 
131  virtual void statistics(
133  abstract_object_visitedt &visited,
134  const abstract_environmentt &env,
135  const namespacet &ns) const = 0;
136 
137  template <class keyt, typename hash>
138  static bool merge_shared_maps(
142  const widen_modet &widen_mode)
143  {
144  bool modified = false;
145 
147  delta_viewt delta_view;
148  map1.get_delta_view(map2, delta_view, true);
149 
150  for(auto &item : delta_view)
151  {
152  auto v_new =
153  abstract_objectt::merge(item.m, item.get_other_map_value(), widen_mode);
154  if(v_new.modified)
155  {
156  modified = true;
157  out_map.replace(item.k, v_new.object);
158  }
159  }
160 
161  return modified;
162  }
163 };
164 
166 {
167  static const irep_idt TYPE_ID()
168  {
169  return ID_array;
170  }
171  static const irep_idt ACCESS_EXPR_ID()
172  {
173  return ID_index;
174  }
175  static typet read_type(const typet &, const typet &object_type)
176  {
177  array_typet array_type(to_array_type(object_type));
178  return array_type.element_type();
179  }
180 
181  static void get_statistics(
182  abstract_object_statisticst &statistics,
183  abstract_object_visitedt &visited,
184  const abstract_environmentt &env,
185  const namespacet &ns)
186  {
187  ++statistics.number_of_arrays;
188  }
189 };
190 
192 {
193  static const irep_idt &TYPE_ID()
194  {
195  return ID_struct;
196  }
197  static const irep_idt &ACCESS_EXPR_ID()
198  {
199  return ID_member;
200  }
201  static const typet &read_type(const typet &expr_type, const typet &)
202  {
203  return expr_type;
204  }
205 
206  static void get_statistics(
207  abstract_object_statisticst &statistics,
208  abstract_object_visitedt &visited,
209  const abstract_environmentt &env,
210  const namespacet &ns)
211  {
212  ++statistics.number_of_structs;
213  }
214 };
215 
217 {
218  static const irep_idt TYPE_ID()
219  {
220  return ID_union;
221  }
222  static const irep_idt ACCESS_EXPR_ID()
223  {
224  return ID_member;
225  }
226  static typet read_type(const typet &, const typet &object_type)
227  {
228  return object_type;
229  }
230 
231  static void get_statistics(
232  abstract_object_statisticst &statistics,
233  abstract_object_visitedt &visited,
234  const abstract_environmentt &env,
235  const namespacet &ns)
236  {
237  }
238 };
239 
240 #endif //CBMC_ABSTRACT_AGGREGATE_OBJECT_H
An abstract version of a program environment.
abstract_objectt is the top of the inheritance heirarchy of objects used to represent individual vari...
std::set< abstract_object_pointert > abstract_object_visitedt
sharing_ptrt< class abstract_objectt > abstract_object_pointert
Statistics gathering for the variable senstivity domain.
abstract_aggregate_objectt(const typet &type, bool tp, bool bttm)
virtual abstract_object_pointert read_component(const abstract_environmentt &environment, const exprt &expr, const namespacet &ns) const
static bool merge_shared_maps(const sharing_mapt< keyt, abstract_object_pointert, false, hash > &map1, const sharing_mapt< keyt, abstract_object_pointert, false, hash > &map2, sharing_mapt< keyt, abstract_object_pointert, false, hash > &out_map, const widen_modet &widen_mode)
void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const override
abstract_aggregate_objectt(const exprt &expr, const abstract_environmentt &environment, const namespacet &ns)
virtual void statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const =0
abstract_object_pointert write(abstract_environmentt &environment, const namespacet &ns, const std::stack< exprt > &stack, const exprt &specifier, const abstract_object_pointert &value, bool merging_write) const override
A helper function to evaluate writing to a component of an abstract object.
abstract_object_pointert expression_transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns) const override
Interface for transforms.
virtual abstract_object_pointert write_component(abstract_environmentt &environment, const namespacet &ns, const std::stack< exprt > &stack, const exprt &expr, const abstract_object_pointert &value, bool merging_write) const
virtual abstract_object_pointert abstract_object_factory(const typet &type, const namespacet &ns, bool top, bool bottom) const
Look at the configuration for the sensitivity and create an appropriate abstract_object.
virtual bool is_top() const
Find out if the abstract object is top.
virtual bool is_bottom() const
Find out if the abstract object is bottom.
virtual abstract_object_pointert expression_transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns) const
Interface for transforms.
static combine_result merge(const abstract_object_pointert &op1, const abstract_object_pointert &op2, const locationt &merge_location, const widen_modet &widen_mode)
virtual void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const
virtual const typet & type() const
Get the real type of the variable this abstract object is representing.
Arrays with given size.
Definition: std_types.h:807
const typet & element_type() const
The type of the elements of the array.
Definition: std_types.h:827
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
const irep_idt & id() const
Definition: irep.h:388
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
A map implemented as a tree where subtrees can be shared between different maps.
Definition: sharing_map.h:190
void replace(const key_type &k, valueU &&m)
Replace element, element must exist in map.
Definition: sharing_map.h:1425
void get_delta_view(const sharing_mapt &other, delta_viewt &delta_view, const bool only_common=true) const
Get a delta view of the elements in the map.
Definition: sharing_map.h:939
The type of an expression, extends irept.
Definition: type.h:29
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:888
static typet read_type(const typet &, const typet &object_type)
static void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns)
static const irep_idt TYPE_ID()
static const irep_idt ACCESS_EXPR_ID()
static const typet & read_type(const typet &expr_type, const typet &)
static void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns)
static const irep_idt & TYPE_ID()
static const irep_idt & ACCESS_EXPR_ID()
static typet read_type(const typet &, const typet &object_type)
static const irep_idt TYPE_ID()
static void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns)
static const irep_idt ACCESS_EXPR_ID()