Skip to content

Commit 3588b9b

Browse files
Define two helper functions for for_each_successor
These two functions deal with one type of node. Type safety ensures we don't mix the two kinds of node, so these two functions can be made public.
1 parent 52ad17c commit 3588b9b

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

src/solvers/refinement/string_refinement_util.cpp

+41-28
Original file line numberDiff line numberDiff line change
@@ -336,44 +336,57 @@ bool add_node(
336336
return true;
337337
}
338338

339-
void string_dependenciest::for_each_successor(
340-
const nodet &node,
341-
const std::function<void(const nodet &)> &f) const
339+
void string_dependenciest::for_each_dependency(
340+
const builtin_function_nodet &node,
341+
std::function<void(const string_nodet &)> f) const
342342
{
343-
if(node.kind == nodet::BUILTIN)
343+
for(const auto &s : node.data->string_arguments())
344344
{
345-
const auto &builtin = builtin_function_nodes[node.index];
346-
for(const auto &s : builtin.data->string_arguments())
345+
std::vector<std::reference_wrapper<const exprt>> stack({s});
346+
while(!stack.empty())
347347
{
348-
std::vector<std::reference_wrapper<const exprt>> stack({s});
349-
while(!stack.empty())
348+
const auto current = stack.back();
349+
stack.pop_back();
350+
if(const auto if_expr = expr_try_dynamic_cast<if_exprt>(current.get()))
350351
{
351-
const auto current = stack.back();
352-
stack.pop_back();
353-
if(const auto if_expr = expr_try_dynamic_cast<if_exprt>(current.get()))
354-
{
355-
stack.emplace_back(if_expr->true_case());
356-
stack.emplace_back(if_expr->false_case());
357-
}
358-
else if(const auto node = node_at(to_array_string_expr(current)))
359-
f(nodet(*node));
360-
else
361-
UNREACHABLE;
352+
stack.emplace_back(if_expr->true_case());
353+
stack.emplace_back(if_expr->false_case());
362354
}
355+
else if(const auto string_node = node_at(to_array_string_expr(current)))
356+
f(*string_node);
357+
else
358+
UNREACHABLE;
363359
}
364360
}
365-
else if(node.kind == nodet::STRING)
361+
}
362+
363+
void string_dependenciest::for_each_dependency(
364+
const string_nodet &node,
365+
std::function<void(const builtin_function_nodet &)> f) const
366+
{
367+
std::for_each(
368+
node.dependencies.begin(),
369+
node.dependencies.end(),
370+
[&](const std::size_t &index) { f(builtin_function_nodes[index]); });
371+
}
372+
373+
void string_dependenciest::for_each_successor(
374+
const nodet &node,
375+
const std::function<void(const nodet &)> &f) const
376+
{
377+
if(node.kind == nodet::BUILTIN)
366378
{
367-
const auto &s_node = string_nodes[node.index];
368-
std::for_each(
369-
s_node.dependencies.begin(),
370-
s_node.dependencies.end(),
371-
[&](const std::size_t &index) { // NOLINT
372-
f(nodet(builtin_function_nodes[index]));
373-
});
379+
for_each_dependency(
380+
builtin_function_nodes[node.index],
381+
[&](const string_nodet &n) { return f(nodet(n)); });
374382
}
375383
else
376-
UNREACHABLE;
384+
{
385+
INVARIANT(node.kind == nodet::STRING, "nodes are either BUILTIN or STRING");
386+
for_each_dependency(
387+
string_nodes[node.index],
388+
[&](const builtin_function_nodet &n) { return f(nodet(n)); });
389+
}
377390
}
378391

379392
void string_dependenciest::for_each_node(

src/solvers/refinement/string_refinement_util.h

+8
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ class string_dependenciest
217217
const array_string_exprt &e,
218218
const builtin_function_nodet &builtin_function);
219219

220+
/// Applies `f` to each node on which `node` depends
221+
void for_each_dependency(
222+
const string_nodet &node,
223+
std::function<void(const builtin_function_nodet &)> f) const;
224+
void for_each_dependency(
225+
const builtin_function_nodet &node,
226+
std::function<void(const string_nodet &)> f) const;
227+
220228
/// Attempt to evaluate the given string from the dependencies and valuation
221229
/// of strings on which it depends
222230
/// Warning: eval uses a cache which must be cleaned everytime the valuations

0 commit comments

Comments
 (0)