CBMC
string_dependencies.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: String solver
4 
5 Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_SOLVERS_STRINGS_STRING_DEPENDENCIES_H
13 #define CPROVER_SOLVERS_STRINGS_STRING_DEPENDENCIES_H
14 
15 #include <memory>
16 
18 
23 {
24 public:
27  {
28  public:
29  // index in the `builtin_function_nodes` vector
30  std::size_t index;
31  // pointer to the builtin function
32  std::unique_ptr<string_builtin_functiont> data;
33 
35  std::unique_ptr<string_builtin_functiont> d,
36  std::size_t i)
37  : index(i), data(std::move(d))
38  {
39  }
40 
42  : index(other.index), data(std::move(other.data))
43  {
44  }
45 
47  {
48  index = other.index;
49  data = std::move(other.data);
50  return *this;
51  }
52  };
53 
56  {
57  public:
58  // expression the node corresponds to
60  // index in the string_nodes vector
61  std::size_t index;
62  // builtin functions on which it depends, refered by there index in
63  // builtin_function node vector.
64  // \todo should these we shared pointers?
65  std::vector<std::size_t> dependencies;
66  // builtin function of which it is the result
67  std::optional<std::size_t> result_from;
68 
69  explicit string_nodet(array_string_exprt e, const std::size_t index)
70  : expr(std::move(e)), index(index)
71  {
72  }
73  };
74 
75  string_nodet &get_node(const array_string_exprt &e);
76 
77  std::unique_ptr<const string_nodet>
78  node_at(const array_string_exprt &e) const;
79 
80  builtin_function_nodet &
81  make_node(std::unique_ptr<string_builtin_functiont> builtin_function);
83  get_builtin_function(const builtin_function_nodet &node) const;
84 
88  void add_dependency(
89  const array_string_exprt &e,
90  const builtin_function_nodet &builtin_function);
91 
94  const string_nodet &node,
95  const std::function<void(const builtin_function_nodet &)> &f) const;
97  const builtin_function_nodet &node,
98  const std::function<void(const string_nodet &)> &f) const;
99 
104  std::optional<exprt> eval(
105  const array_string_exprt &s,
106  const std::function<exprt(const exprt &)> &get_value) const;
107 
109  void clean_cache();
110 
111  void output_dot(std::ostream &stream) const;
112 
116  [[nodiscard]] string_constraintst add_constraints(
117  string_constraint_generatort &generatort,
118  message_handlert &message_handler);
119 
121  void clear();
122 
123 private:
125  std::vector<builtin_function_nodet> builtin_function_nodes;
126 
128  std::vector<string_nodet> string_nodes;
129 
132  std::unordered_map<array_string_exprt, std::size_t, irep_hash>
134 
135  class nodet
136  {
137  public:
138  enum
139  {
141  STRING
142  } kind;
143  std::size_t index;
144 
145  explicit nodet(const builtin_function_nodet &builtin)
146  : kind(BUILTIN), index(builtin.index)
147  {
148  }
149 
150  explicit nodet(const string_nodet &string_node)
151  : kind(STRING), index(string_node.index)
152  {
153  }
154 
155  bool operator==(const nodet &n) const
156  {
157  return n.kind == kind && n.index == index;
158  }
159  };
160 
162  // NOLINTNEXTLINE(readability/identifiers)
163  struct node_hash
164  {
165  size_t operator()(const string_dependenciest::nodet &node) const
166  {
167  return 2 * node.index +
168  (node.kind == string_dependenciest::nodet::STRING ? 0 : 1);
169  }
170  };
171 
172  mutable std::vector<std::optional<exprt>> eval_string_cache;
173 
175  void for_each_node(const std::function<void(const nodet &)> &f) const;
176 
178  void for_each_successor(
179  const nodet &i,
180  const std::function<void(const nodet &)> &f) const;
181 };
182 
200 std::optional<exprt> add_node(
201  string_dependenciest &dependencies,
202  const exprt &expr,
203  array_poolt &array_pool,
204  symbol_generatort &fresh_symbol);
205 
206 #endif // CPROVER_SOLVERS_STRINGS_STRING_DEPENDENCIES_H
Correspondance between arrays and pointers string representations.
Definition: array_pool.h:42
Base class for all expressions.
Definition: expr.h:56
Base class for string functions that are built in the solver.
A builtin function node contains a builtin function call.
std::unique_ptr< string_builtin_functiont > data
builtin_function_nodet(builtin_function_nodet &&other)
builtin_function_nodet & operator=(builtin_function_nodet &&other)
builtin_function_nodet(std::unique_ptr< string_builtin_functiont > d, std::size_t i)
nodet(const string_nodet &string_node)
nodet(const builtin_function_nodet &builtin)
bool operator==(const nodet &n) const
enum string_dependenciest::nodet::@6 kind
A string node points to builtin_function on which it depends.
string_nodet(array_string_exprt e, const std::size_t index)
std::optional< std::size_t > result_from
std::vector< std::size_t > dependencies
Keep track of dependencies between strings.
std::unique_ptr< const string_nodet > node_at(const array_string_exprt &e) const
std::vector< std::optional< exprt > > eval_string_cache
void for_each_node(const std::function< void(const nodet &)> &f) const
Applies f on all nodes.
builtin_function_nodet & make_node(std::unique_ptr< string_builtin_functiont > builtin_function)
void output_dot(std::ostream &stream) const
void clean_cache()
Clean the cache used by eval
std::vector< builtin_function_nodet > builtin_function_nodes
Set of nodes representing builtin_functions.
std::optional< 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...
void clear()
Clear the content of the dependency graph.
const string_builtin_functiont & get_builtin_function(const builtin_function_nodet &node) const
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...
std::vector< string_nodet > string_nodes
Set of nodes representing strings.
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.
string_constraintst add_constraints(string_constraint_generatort &generatort, message_handlert &message_handler)
For all builtin call on which a test (or an unsupported buitin) result depends, add the corresponding...
void for_each_successor(const nodet &i, const std::function< void(const nodet &)> &f) const
Applies f on all successors of the node n.
void add_dependency(const array_string_exprt &e, const builtin_function_nodet &builtin_function)
Add edge from node for e&#160;to node for builtin_function if e is a simple array expression.
string_nodet & get_node(const array_string_exprt &e)
Generation of fresh symbols of a given type.
Definition: array_pool.h:22
std::optional< exprt > add_node(string_dependenciest &dependencies, const exprt &expr, array_poolt &array_pool, symbol_generatort &fresh_symbol)
When a sub-expression of expr is a builtin_function, add a "string_builtin_function" node to the grap...
Collection of constraints of different types: existential formulas, universal formulas,...
size_t operator()(const string_dependenciest::nodet &node) const