Skip to content

Commit fade69f

Browse files
author
Lukasz A.J. Wrona
committed
Move expr_cast to a separate file
1 parent 51e493e commit fade69f

File tree

2 files changed

+72
-57
lines changed

2 files changed

+72
-57
lines changed

src/solvers/refinement/expr_cast.h

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************\
2+
3+
Module: exprt conversion functions
4+
5+
Author: Diffblue ltd. All Rights Reserved
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Abstraction Refinement Loop
11+
12+
#ifndef CPROVER_SOLVERS_REFINEMENT_EXPR_CAST_H
13+
#define CPROVER_SOLVERS_REFINEMENT_EXPR_CAST_H
14+
15+
#include <util/optional.h>
16+
17+
/// Convert exprt to a specific type. Remove empty optional if
18+
/// conversion cannot be performed
19+
/// Generic case doesn't exist, specialize for different types accordingly
20+
/// TODO: this should go to util
21+
template<typename T>
22+
struct expr_cast_implt final { };
23+
24+
template<>
25+
struct expr_cast_implt<mp_integer> final
26+
{
27+
optionalt<mp_integer> operator()(const exprt &expr) const
28+
{
29+
mp_integer out;
30+
if(to_integer(expr, out))
31+
return {};
32+
return out;
33+
}
34+
};
35+
36+
template<>
37+
struct expr_cast_implt<std::size_t> final
38+
{
39+
optionalt<std::size_t> operator()(const exprt &expr) const
40+
{
41+
if(const auto tmp=expr_cast_implt<mp_integer>()(expr))
42+
if(tmp->is_long() && *tmp>=0)
43+
return tmp->to_long();
44+
return {};
45+
}
46+
};
47+
48+
template<>
49+
struct expr_cast_implt<string_exprt> final
50+
{
51+
optionalt<string_exprt> operator()(const exprt &expr) const
52+
{
53+
if(is_refined_string_type(expr.type()))
54+
return to_string_expr(expr);
55+
return {};
56+
}
57+
};
58+
59+
template<typename T>
60+
optionalt<T> expr_cast(const exprt& expr)
61+
{ return expr_cast_implt<T>()(expr); }
62+
63+
template<typename T>
64+
T expr_cast_v(const exprt &expr)
65+
{
66+
const auto maybe=expr_cast<T>(expr);
67+
INVARIANT(maybe, "Bad conversion");
68+
return *maybe;
69+
}
70+
71+
#endif // CPROVER_SOLVERS_REFINEMENT_EXPR_CAST_H

src/solvers/refinement/string_refinement.cpp

+1-57
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Author: Alberto Griggio, [email protected]
2626
#include <solvers/sat/satcheck.h>
2727
#include <solvers/refinement/string_constraint_instantiation.h>
2828
#include <java_bytecode/java_types.h>
29+
#include "expr_cast.h"
2930

3031
static exprt substitute_array_with_expr(const exprt &expr, const exprt &index);
3132

@@ -95,63 +96,6 @@ static exprt get_array(
9596
const std::function<exprt(const exprt &)> &super_get,
9697
const exprt &arr);
9798

98-
/// Convert exprt to a specific type. Throw bad_cast if conversion
99-
/// cannot be performed
100-
/// Generic case doesn't exist, specialize for different types accordingly
101-
/// TODO: this should go to util
102-
103-
// Tag dispatching struct
104-
105-
template<typename T>
106-
struct expr_cast_implt final { };
107-
108-
template<>
109-
struct expr_cast_implt<mp_integer> final
110-
{
111-
optionalt<mp_integer> operator()(const exprt &expr) const
112-
{
113-
mp_integer out;
114-
if(to_integer(expr, out))
115-
return {};
116-
return out;
117-
}
118-
};
119-
120-
template<>
121-
struct expr_cast_implt<std::size_t> final
122-
{
123-
optionalt<std::size_t> operator()(const exprt &expr) const
124-
{
125-
if(const auto tmp=expr_cast_implt<mp_integer>()(expr))
126-
if(tmp->is_long() && *tmp>=0)
127-
return tmp->to_long();
128-
return {};
129-
}
130-
};
131-
132-
template<>
133-
struct expr_cast_implt<string_exprt> final
134-
{
135-
optionalt<string_exprt> operator()(const exprt &expr) const
136-
{
137-
if(is_refined_string_type(expr.type()))
138-
return to_string_expr(expr);
139-
return {};
140-
}
141-
};
142-
143-
template<typename T>
144-
optionalt<T> expr_cast(const exprt& expr)
145-
{ return expr_cast_implt<T>()(expr); }
146-
147-
template<typename T>
148-
T expr_cast_v(const exprt &expr)
149-
{
150-
const auto maybe=expr_cast<T>(expr);
151-
INVARIANT(maybe, "Bad conversion");
152-
return *maybe;
153-
}
154-
15599
/// Convert index-value map to a vector of values. If a value for an
156100
/// index is not defined, set it to the value referenced by the next higher
157101
/// index. The length of the resulting vector is the key of the map's

0 commit comments

Comments
 (0)