cprover
string_refinement_util.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3  Module: String solver
4 
5  Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_SOLVERS_REFINEMENT_STRING_REFINEMENT_UTIL_H
10 #define CPROVER_SOLVERS_REFINEMENT_STRING_REFINEMENT_UTIL_H
11 
13 #include "string_constraint.h"
15 
23 bool is_char_type(const typet &type);
24 
30 bool is_char_array_type(const typet &type, const namespacet &ns);
31 
35 bool is_char_pointer_type(const typet &type);
36 
44 bool has_char_pointer_subtype(const typet &type, const namespacet &ns);
45 
49 bool has_char_array_subexpr(const exprt &expr, const namespacet &ns);
50 
52 {
53  std::map<exprt, std::set<exprt>> cumulative;
54  std::map<exprt, std::set<exprt>> current;
55 };
56 
58 {
59  std::vector<string_constraintt> universal;
60  std::vector<string_not_contains_constraintt> not_contains;
61 };
62 
66 {
67 public:
72  explicit sparse_arrayt(const with_exprt &expr);
73 
76  static exprt to_if_expression(const with_exprt &expr, const exprt &index);
77 
78 protected:
80  std::map<std::size_t, exprt> entries;
82  {
83  }
84 };
85 
91 {
92 public:
97  explicit interval_sparse_arrayt(const with_exprt &expr) : sparse_arrayt(expr)
98  {
99  }
100 
104  interval_sparse_arrayt(const array_exprt &expr, const exprt &extra_value);
105 
110  const array_list_exprt &expr,
111  const exprt &extra_value);
112 
113  exprt to_if_expression(const exprt &index) const;
114 
118  of_expr(const exprt &expr, const exprt &extra_value);
119 
121  array_exprt concretize(std::size_t size, const typet &index_type) const;
122 
125  exprt at(std::size_t index) const;
126 
130  {
131  }
132 };
133 
139 {
140 public:
142  void add(const std::size_t i, const exprt &expr);
143 
147  std::vector<exprt> find_expressions(const std::size_t i);
148 
151  std::vector<std::size_t> find_equations(const exprt &expr);
152 
153 private:
155  std::map<exprt, std::vector<std::size_t>> equations_containing;
157  std::unordered_map<std::size_t, std::vector<exprt>> strings_in_equation;
158 };
159 
160 
165 {
166 public:
169  {
170  public:
171  // index in the `builtin_function_nodes` vector
172  std::size_t index;
173  // pointer to the builtin function
174  std::unique_ptr<string_builtin_functiont> data;
175 
177  std::unique_ptr<string_builtin_functiont> d,
178  std::size_t i)
179  : index(i), data(std::move(d))
180  {
181  }
182 
184  : index(other.index), data(std::move(other.data))
185  {
186  }
187 
189  {
190  index = other.index;
191  data = std::move(other.data);
192  return *this;
193  }
194  };
195 
198  {
199  public:
200  // expression the node corresponds to
202  // index in the string_nodes vector
203  std::size_t index;
204  // builtin functions on which it depends, refered by there index in
205  // builtin_function node vector.
206  // \todo should these we shared pointers?
207  std::vector<std::size_t> dependencies;
208  // builtin function of which it is the result
210 
211  explicit string_nodet(array_string_exprt e, const std::size_t index)
212  : expr(std::move(e)), index(index)
213  {
214  }
215  };
216 
217  string_nodet &get_node(const array_string_exprt &e);
218 
219  std::unique_ptr<const string_nodet>
220  node_at(const array_string_exprt &e) const;
221 
223  builtin_function_nodet &
224  make_node(std::unique_ptr<string_builtin_functiont> &builtin_function);
226  get_builtin_function(const builtin_function_nodet &node) const;
227 
231  void add_dependency(
232  const array_string_exprt &e,
233  const builtin_function_nodet &builtin_function);
234 
236  void for_each_dependency(
237  const string_nodet &node,
238  const std::function<void(const builtin_function_nodet &)> &f) const;
239  void for_each_dependency(
240  const builtin_function_nodet &node,
241  const std::function<void(const string_nodet &)> &f) const;
242 
248  const array_string_exprt &s,
249  const std::function<exprt(const exprt &)> &get_value) const;
250 
252  void clean_cache();
253 
254  void output_dot(std::ostream &stream) const;
255 
260 
262  void clear();
263 
264 private:
266  std::vector<builtin_function_nodet> builtin_function_nodes;
267 
269  std::vector<string_nodet> string_nodes;
270 
273  std::unordered_map<array_string_exprt, std::size_t, irep_hash>
275 
276  class nodet
277  {
278  public:
279  enum
280  {
283  } kind;
284  std::size_t index;
285 
286  explicit nodet(const builtin_function_nodet &builtin)
287  : kind(BUILTIN), index(builtin.index)
288  {
289  }
290 
291  explicit nodet(const string_nodet &string_node)
292  : kind(STRING), index(string_node.index)
293  {
294  }
295 
296  bool operator==(const nodet &n) const
297  {
298  return n.kind == kind && n.index == index;
299  }
300  };
301 
303  // NOLINTNEXTLINE(readability/identifiers)
304  struct node_hash
305  {
306  size_t
307  operator()(const string_dependenciest::nodet &node) const optional_noexcept
308  {
309  return 2 * node.index +
310  (node.kind == string_dependenciest::nodet::STRING ? 0 : 1);
311  }
312  };
313 
314  mutable std::vector<optionalt<exprt>> eval_string_cache;
315 
317  void for_each_node(const std::function<void(const nodet &)> &f) const;
318 
320  void for_each_successor(
321  const nodet &i,
322  const std::function<void(const nodet &)> &f) const;
323 };
324 
338 bool add_node(
339  string_dependenciest &dependencies,
340  const equal_exprt &equation,
341  array_poolt &array_pool);
342 
343 #endif // CPROVER_SOLVERS_REFINEMENT_STRING_REFINEMENT_UTIL_H
The type of an expression.
Definition: type.h:22
array_exprt concretize(std::size_t size, const typet &index_type) const
Convert to an array representation, ignores elements at index >= size.
string_nodet(array_string_exprt e, const std::size_t index)
const string_builtin_functiont & get_builtin_function(const builtin_function_nodet &node) const
Generates string constraints to link results from string functions with their arguments.
void add_constraints(string_constraint_generatort &generatort)
For all builtin call on which a test (or an unsupported buitin) result depends, add the corresponding...
builtin_function_nodet & make_node(std::unique_ptr< string_builtin_functiont > &builtin_function)
builtin_function is reset to an empty pointer after the node is created
void clean_cache()
Clean the cache used by eval
std::unique_ptr< string_builtin_functiont > data
string_nodet & get_node(const array_string_exprt &e)
enum string_dependenciest::nodet::@4 kind
std::map< exprt, std::set< exprt > > current
interval_sparse_arrayt(const with_exprt &expr)
An expression of the form array_of(x) with {i:=a} with {j:=b} is converted to an array arr where for ...
std::unique_ptr< const string_nodet > node_at(const array_string_exprt &e) const
exprt at(std::size_t index) const
Get the value at the specified index.
STL namespace.
std::vector< builtin_function_nodet > builtin_function_nodes
Set of nodes representing builtin_functions.
Correspondance between arrays and pointers string representations.
Base class for string functions that are built in the solver.
std::unordered_map< array_string_exprt, std::size_t, irep_hash > node_index_pool
Nodes describing dependencies of a string: values of the map correspond to indexes in the vector stri...
nodet(const string_nodet &string_node)
equality
Definition: std_expr.h:1354
void for_each_successor(const nodet &i, const std::function< void(const nodet &)> &f) const
Applies f on all successors of the node n.
optionalt< exprt > eval(const array_string_exprt &s, const std::function< exprt(const exprt &)> &get_value) const
Attempt to evaluate the given string from the dependencies and valuation of strings on which it depen...
Defines string constraints.
bool add_node(string_dependenciest &dependencies, const equal_exprt &equation, array_poolt &array_pool)
When right hand side of equation is a builtin_function add a "string_builtin_function" node to the gr...
std::map< exprt, std::vector< std::size_t > > equations_containing
Record index of the equations that contain a given expression.
std::map< exprt, std::set< exprt > > cumulative
Represents arrays by the indexes up to which the value remains the same.
nonstd::optional< T > optionalt
Definition: optional.h:35
std::vector< string_nodet > string_nodes
Set of nodes representing strings.
void clear()
Clear the content of the dependency graph.
void add(const std::size_t i, const exprt &expr)
Record the fact that equation i contains expression expr
Represents arrays of the form array_of(x) with {i:=a} with {j:=b} ... by a default value x and a list...
TO_BE_DOCUMENTED.
Definition: namespace.h:74
std::map< std::size_t, exprt > entries
bool operator==(const nodet &n) const
bitvector_typet index_type()
Definition: c_types.cpp:16
builtin_function_nodet(std::unique_ptr< string_builtin_functiont > d, std::size_t i)
std::vector< optionalt< exprt > > eval_string_cache
std::unordered_map< std::size_t, std::vector< exprt > > strings_in_equation
Record expressions that are contained in the equation with the given index.
Keep track of dependencies between strings.
interval_sparse_arrayt(exprt default_value)
Array containing the same value at each index.
A builtin function node contains a builtin function call.
static exprt to_if_expression(const with_exprt &expr, const exprt &index)
Creates an if_expr corresponding to the result of accessing the array at the given index...
A string node points to builtin_function on which it depends.
std::vector< string_constraintt > universal
bool has_char_array_subexpr(const exprt &expr, const namespacet &ns)
std::vector< exprt > find_expressions(const std::size_t i)
Maps equation to expressions contained in them and conversely expressions to equations that contain t...
size_t operator()(const string_dependenciest::nodet &node) const optional_noexcept
Base class for all expressions.
Definition: expr.h:42
builtin_function_nodet & operator=(builtin_function_nodet &&other)
bool is_char_pointer_type(const typet &type)
For now, any unsigned bitvector type is considered a character.
nodet(const builtin_function_nodet &builtin)
Operator to update elements in structs and arrays.
Definition: std_expr.h:3459
exprt to_if_expression(const exprt &index) const
std::vector< std::size_t > dependencies
bool has_char_pointer_subtype(const typet &type, const namespacet &ns)
sparse_arrayt(const with_exprt &expr)
Initialize a sparse array from an expression of the form array_of(x) with {i:=a} with {j:=b} ...
bool is_char_type(const typet &type)
For now, any unsigned bitvector type of width smaller or equal to 16 is considered a character...
void add_dependency(const array_string_exprt &e, const builtin_function_nodet &builtin_function)
Add edge from node for e to node for builtin_function if e is a simple array expression.
static optionalt< interval_sparse_arrayt > of_expr(const exprt &expr, const exprt &extra_value)
If the expression is an array_exprt or a with_exprt uses the appropriate constructor, otherwise returns empty optional.
bool is_char_array_type(const typet &type, const namespacet &ns)
Distinguish char array from other types.
builtin_function_nodet(builtin_function_nodet &&other)
std::vector< std::size_t > find_equations(const exprt &expr)
sparse_arrayt(exprt default_value)
void for_each_node(const std::function< void(const nodet &)> &f) const
Applies f on all nodes.
array constructor from list of elements
Definition: std_expr.h:1617
Definition: kdev_t.h:24
std::vector< string_not_contains_constraintt > not_contains
void for_each_dependency(const string_nodet &node, const std::function< void(const builtin_function_nodet &)> &f) const
Applies f to each node on which node depends.
Array constructor from a list of index-element pairs Operands are index/value pairs, alternating.
Definition: std_expr.h:1662
void output_dot(std::ostream &stream) const