CBMC
ai_domain.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Abstract Interpretation
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
39 
40 #ifndef CPROVER_ANALYSES_AI_DOMAIN_H
41 #define CPROVER_ANALYSES_AI_DOMAIN_H
42 
43 #include <util/json.h>
44 #include <util/xml.h>
45 
46 #include "ai_history.h"
47 
48 // forward reference the abstract interpreter interface
49 class ai_baset;
50 
54 {
55 protected:
59  {
60  }
61 
64  {
65  }
66 
67 public:
68  virtual ~ai_domain_baset()
69  {
70  }
71 
74 
95  virtual void transform(
96  const irep_idt &function_from,
97  trace_ptrt from,
98  const irep_idt &function_to,
99  trace_ptrt to,
100  ai_baset &ai,
101  const namespacet &ns) = 0;
102 
103  virtual void
104  output(std::ostream &, const ai_baset &, const namespacet &) const
105  {
106  }
107 
108  virtual jsont output_json(const ai_baset &ai, const namespacet &ns) const;
109 
110  virtual xmlt output_xml(const ai_baset &ai, const namespacet &ns) const;
111 
113  virtual void make_bottom() = 0;
114 
117  virtual void make_top() = 0;
118 
121  virtual void make_entry()
122  {
123  make_top();
124  }
125 
126  virtual bool is_bottom() const = 0;
127 
128  virtual bool is_top() const = 0;
129 
143 
147 
149  virtual bool ai_simplify(exprt &condition, const namespacet &) const
150  {
151  (void)condition; // unused parameter
152  return true;
153  }
154 
156  virtual bool ai_simplify_lhs(exprt &condition, const namespacet &ns) const;
157 
160  virtual exprt to_predicate(void) const
161  {
162  if(is_bottom())
163  return false_exprt();
164  else
165  return true_exprt();
166  }
167 };
168 
169 // No virtual interface is complete without a factory!
171 {
172 public:
176 
178  {
179  }
180 
181  virtual std::unique_ptr<statet> make(locationt l) const = 0;
182  virtual std::unique_ptr<statet> copy(const statet &s) const = 0;
183 
184  // Not domain construction but requires knowing the precise type of statet
185  virtual bool
186  merge(statet &dest, const statet &src, trace_ptrt from, trace_ptrt to)
187  const = 0;
188 };
189 // Converting make to take a trace_ptr instead of a location would
190 // require removing the backwards-compatible
191 // location_sensitive_storaget::get_state(locationt l)
192 // function which is used by some of the older domains
193 
194 // It would be great to have a single (templated) default implementation.
195 // However, a default constructor is not part of the ai_domain_baset interface
196 // and there are some domains which don't have one. So we need to separate the
197 // methods.
198 template <typename domainT>
200 {
201 public:
205 
206  std::unique_ptr<statet> copy(const statet &s) const override
207  {
208  return std::make_unique<domainT>(static_cast<const domainT &>(s));
209  }
210 
211  bool merge(statet &dest, const statet &src, trace_ptrt from, trace_ptrt to)
212  const override
213  {
214  // For backwards compatability, use the location version
215  return static_cast<domainT &>(dest).merge(
216  static_cast<const domainT &>(src), from, to);
217  }
218 };
219 
220 template <typename domainT>
222  : public ai_domain_factoryt<domainT>
223 {
224 public:
228 
229  std::unique_ptr<statet> make(locationt l) const override
230  {
231  auto d = std::make_unique<domainT>();
232  CHECK_RETURN(d->is_bottom());
233  return std::unique_ptr<statet>(d.release());
234  }
235 };
236 
237 template <typename domainT>
239  : public ai_domain_factoryt<domainT>
240 {
241 public:
245 
246  std::unique_ptr<statet> make(locationt l) const override
247  {
248  auto d = std::make_unique<domainT>(l);
249  CHECK_RETURN(d->is_bottom());
250  return std::unique_ptr<statet>(d.release());
251  }
252 };
253 
254 #endif
Abstract Interpretation history.
This is the basic interface of the abstract interpreter with default implementations of the core func...
Definition: ai.h:117
The interface offered by a domain, allows code to manipulate domains without knowing their exact type...
Definition: ai_domain.h:54
virtual bool is_bottom() const =0
virtual void transform(const irep_idt &function_from, trace_ptrt from, const irep_idt &function_to, trace_ptrt to, ai_baset &ai, const namespacet &ns)=0
how function calls are treated: a) there is an edge from each call site to the function head b) there...
virtual void make_bottom()=0
no states
ai_domain_baset()
The constructor is expected to produce 'false' or 'bottom' A default constructor is not part of the d...
Definition: ai_domain.h:58
virtual void make_entry()
Make this domain a reasonable entry-point state For most domains top is sufficient.
Definition: ai_domain.h:121
virtual xmlt output_xml(const ai_baset &ai, const namespacet &ns) const
Definition: ai_domain.cpp:26
virtual exprt to_predicate(void) const
Gives a Boolean condition that is true for all values represented by the domain.
Definition: ai_domain.h:160
ai_history_baset::trace_ptrt trace_ptrt
Definition: ai_domain.h:73
virtual jsont output_json(const ai_baset &ai, const namespacet &ns) const
Definition: ai_domain.cpp:17
virtual void make_top()=0
all states – the analysis doesn't use this directly (see make_entry) and domains may refuse to implem...
goto_programt::const_targett locationt
Definition: ai_domain.h:72
virtual bool ai_simplify_lhs(exprt &condition, const namespacet &ns) const
Simplifies the expression but keeps it as an l-value.
Definition: ai_domain.cpp:43
virtual void output(std::ostream &, const ai_baset &, const namespacet &) const
Definition: ai_domain.h:104
virtual bool is_top() const =0
virtual bool ai_simplify(exprt &condition, const namespacet &) const
also add
Definition: ai_domain.h:149
virtual ~ai_domain_baset()
Definition: ai_domain.h:68
ai_domain_baset(const ai_domain_baset &old)
A copy constructor is part of the domain interface.
Definition: ai_domain.h:63
virtual std::unique_ptr< statet > make(locationt l) const =0
ai_domain_baset::trace_ptrt trace_ptrt
Definition: ai_domain.h:175
virtual ~ai_domain_factory_baset()
Definition: ai_domain.h:177
ai_domain_baset::locationt locationt
Definition: ai_domain.h:174
virtual std::unique_ptr< statet > copy(const statet &s) const =0
ai_domain_baset statet
Definition: ai_domain.h:173
virtual bool merge(statet &dest, const statet &src, trace_ptrt from, trace_ptrt to) const =0
ai_domain_factory_baset::trace_ptrt trace_ptrt
Definition: ai_domain.h:227
std::unique_ptr< statet > make(locationt l) const override
Definition: ai_domain.h:229
ai_domain_factory_baset::locationt locationt
Definition: ai_domain.h:226
ai_domain_factory_baset::statet statet
Definition: ai_domain.h:225
ai_domain_factory_baset::locationt locationt
Definition: ai_domain.h:243
std::unique_ptr< statet > make(locationt l) const override
Definition: ai_domain.h:246
ai_domain_factory_baset::trace_ptrt trace_ptrt
Definition: ai_domain.h:244
ai_domain_factory_baset::statet statet
Definition: ai_domain.h:242
ai_domain_factory_baset::statet statet
Definition: ai_domain.h:202
ai_domain_factory_baset::locationt locationt
Definition: ai_domain.h:203
std::unique_ptr< statet > copy(const statet &s) const override
Definition: ai_domain.h:206
ai_domain_factory_baset::trace_ptrt trace_ptrt
Definition: ai_domain.h:204
bool merge(statet &dest, const statet &src, trace_ptrt from, trace_ptrt to) const override
Definition: ai_domain.h:211
std::shared_ptr< const ai_history_baset > trace_ptrt
History objects are intended to be immutable so they can be shared to reduce memory overhead.
Definition: ai_history.h:43
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
The Boolean constant false.
Definition: std_expr.h:3082
instructionst::const_iterator const_targett
Definition: goto_program.h:615
Definition: json.h:27
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
The Boolean constant true.
Definition: std_expr.h:3073
Definition: xml.h:21
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495