CBMC
context_abstract_object.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3  Module: analyses variable-sensitivity context_abstract_object
4 
5  Author: Diffblue Ltd
6 
7 \*******************************************************************/
8 
10 
12 
13 #include <algorithm>
14 
16 {
17  return child_abstract_object;
18 }
19 
21 {
22  child_abstract_object = child;
23 }
24 
26 {
27  if(!child_abstract_object->is_top())
28  set_child(child_abstract_object->make_top());
29 }
30 
32 {
33  if(child_abstract_object->is_top())
34  set_child(child_abstract_object->clear_top());
35 }
36 
54  abstract_environmentt &environment,
55  const namespacet &ns,
56  const std::stack<exprt> &stack,
57  const exprt &specifier,
58  const abstract_object_pointert &value,
59  bool merging_write) const
60 {
61  abstract_object_pointert updated_child = child_abstract_object->write(
62  environment, ns, stack, specifier, value, merging_write);
63 
64  // Only perform an update if the write to the child has in fact changed it...
65  if(updated_child == child_abstract_object)
66  return shared_from_this();
67 
68  return envelop(updated_child);
69 }
70 
84  const exprt &expr,
85  const std::vector<abstract_object_pointert> &operands,
86  const abstract_environmentt &environment,
87  const namespacet &ns) const
88 {
89  PRECONDITION(expr.operands().size() == operands.size());
90  std::vector<abstract_object_pointert> child_operands;
91 
93  operands.begin(),
94  operands.end(),
95  std::back_inserter(child_operands),
96  [](const abstract_object_pointert &op) {
97  PRECONDITION(op != nullptr);
98  auto p = std::dynamic_pointer_cast<const context_abstract_objectt>(op);
99  INVARIANT(p, "Operand shall be of type context_abstract_objectt");
100  return p->child_abstract_object;
101  });
102 
103  auto result = child_abstract_object->expression_transform(
104  expr, child_operands, environment, ns);
105  return envelop(result);
106 }
107 
109  const locationt &location) const
110 {
111  auto result = update_location_context_internal({location});
112 
113  auto updated_child = child_abstract_object->write_location_context(location);
114  result->set_child(updated_child);
115 
116  return result;
117 }
118 
121 {
122  if(
123  std::dynamic_pointer_cast<const context_abstract_objectt>(object) !=
124  nullptr)
125  return object;
126 
127  const auto &envelope =
128  std::dynamic_pointer_cast<context_abstract_objectt>(mutable_clone());
129  envelope->set_child(object);
130  return envelope;
131 }
132 
142  std::ostream &out,
143  const ai_baset &ai,
144  const namespacet &ns) const
145 {
146  child_abstract_object->output(out, ai, ns);
147 }
148 
158  const abstract_object_pointert &before) const
159 {
160  // Default implementation, with no other information to go on
161  // falls back to relying on copy-on-write and pointer inequality
162  // to indicate if an abstract_objectt has been modified
163  auto before_context =
164  std::dynamic_pointer_cast<const context_abstract_objectt>(before);
165 
166  return this->child_abstract_object != before_context->child_abstract_object;
167 }
168 
170 {
171  return child_abstract_object->unwrap_context();
172 }
173 
175 {
176  return child_abstract_object->to_predicate(name);
177 }
178 
180  abstract_object_statisticst &statistics,
181  abstract_object_visitedt &visited,
182  const abstract_environmentt &env,
183  const namespacet &ns) const
184 {
185  abstract_objectt::get_statistics(statistics, visited, env, ns);
186  if(visited.find(child_abstract_object) == visited.end())
187  {
188  child_abstract_object->get_statistics(statistics, visited, env, ns);
189  }
190  statistics.objects_memory_usage += memory_sizet::from_bytes(sizeof(*this));
191 }
std::set< abstract_object_pointert > abstract_object_visitedt
sharing_ptrt< class abstract_objectt > abstract_object_pointert
Statistics gathering for the variable senstivity domain.
static abstract_object_pointert transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns)
virtual internal_abstract_object_pointert mutable_clone() const
goto_programt::const_targett locationt
virtual void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const
This is the basic interface of the abstract interpreter with default implementations of the core func...
Definition: ai.h:117
virtual context_abstract_object_ptrt update_location_context_internal(const locationst &locations) const =0
abstract_object_pointert write_location_context(const locationt &location) const override
Update the location context for an abstract object.
abstract_object_pointert envelop(abstract_object_pointert &child) const
void output(std::ostream &out, const class ai_baset &ai, const namespacet &ns) const override
Output a representation of the value of this abstract object.
bool has_been_modified(const abstract_object_pointert &before) const override
Determine whether 'this' abstract_object has been modified in comparison to a previous 'before' state...
abstract_object_pointert child_abstract_object
void set_child(const abstract_object_pointert &child)
void get_statistics(abstract_object_statisticst &statistics, abstract_object_visitedt &visited, const abstract_environmentt &env, const namespacet &ns) const override
abstract_object_pointert expression_transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns) const override
Try to resolve an expression with the maximum level of precision available.
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 get_child() const
abstract_object_pointert unwrap_context() const override
exprt to_predicate_internal(const exprt &name) const override
to_predicate implementation - derived classes will override
Base class for all expressions.
Definition: expr.h:56
operandst & operands()
Definition: expr.h:94
static memory_sizet from_bytes(std::size_t bytes)
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
General implementation of a an abstract_objectt which can track side information in the form of a 'co...
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
memory_sizet objects_memory_usage
An underestimation of the memory usage of the abstract objects.