Skip to content

Commit d5db868

Browse files
author
Daniel Kroening
committed
smt2_format can now format symbols and constants
1 parent 449a3f9 commit d5db868

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/solvers/smt2/smt2_format.cpp

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,64 @@ Author: Daniel Kroening, kroening@kroening.com
88

99
#include "smt2_format.h"
1010

11+
#include <util/arith_tools.h>
12+
#include <util/std_expr.h>
1113
#include <util/std_types.h>
1214

13-
std::ostream &operator<<(std::ostream &out, const smt2_format &f)
15+
std::ostream &smt2_format_rec(std::ostream &out, const typet &type)
1416
{
15-
if(f.type.id() == ID_unsignedbv)
16-
out << "(_ BitVec " << to_unsignedbv_type(f.type).get_width() << ')';
17-
else if(f.type.id() == ID_bool)
17+
if(type.id() == ID_unsignedbv)
18+
out << "(_ BitVec " << to_unsignedbv_type(type).get_width() << ')';
19+
else if(type.id() == ID_bool)
1820
out << "Bool";
19-
else if(f.type.id() == ID_integer)
21+
else if(type.id() == ID_integer)
2022
out << "Int";
21-
else if(f.type.id() == ID_real)
23+
else if(type.id() == ID_real)
2224
out << "Real";
2325
else
24-
out << "? " << f.type.id();
26+
out << "? " << type.id();
27+
28+
return out;
29+
}
30+
31+
std::ostream &smt2_format_rec(std::ostream &out, const exprt &expr)
32+
{
33+
if(expr.id() == ID_constant)
34+
{
35+
const auto &value = to_constant_expr(expr).get_value();
36+
37+
const typet &expr_type = expr.type();
38+
39+
if(expr_type.id() == ID_unsignedbv)
40+
{
41+
const std::size_t width = to_unsignedbv_type(expr_type).get_width();
42+
43+
const auto value = numeric_cast_v<mp_integer>(expr);
44+
45+
out << "(_ bv" << value << " " << width << ")";
46+
}
47+
else if(expr_type.id() == ID_bool)
48+
{
49+
if(expr.is_true())
50+
out << "true";
51+
else if(expr.is_false())
52+
out << "false";
53+
else
54+
DATA_INVARIANT(false, "unknown Boolean constant");
55+
}
56+
else if(expr_type.id() == ID_integer)
57+
{
58+
out << value;
59+
}
60+
else
61+
DATA_INVARIANT(false, "unhandled constant: " + expr_type.id_string());
62+
}
63+
else if(expr.id() == ID_symbol)
64+
{
65+
out << to_symbol_expr(expr).get_identifier();
66+
}
67+
else
68+
out << "? " << expr.id();
2569

2670
return out;
2771
}

src/solvers/smt2/smt2_format.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,32 @@ Author: Daniel Kroening, kroening@kroening.com
99
#ifndef CPROVER_SOLVERS_SMT2_SMT2_FORMAT_H
1010
#define CPROVER_SOLVERS_SMT2_SMT2_FORMAT_H
1111

12-
#include <util/type.h>
12+
#include <util/expr.h>
1313

14-
struct smt2_format
14+
template <typename T>
15+
struct smt2_format_containert
1516
{
16-
explicit smt2_format(const typet &_type) : type(_type)
17+
explicit smt2_format_containert(const T &_o) : o(_o)
1718
{
1819
}
1920

20-
const typet &type;
21+
const T &o;
2122
};
2223

23-
std::ostream &operator<<(std::ostream &, const smt2_format &);
24+
template <typename T>
25+
static inline smt2_format_containert<T> smt2_format(const T &_o)
26+
{
27+
return smt2_format_containert<T>(_o);
28+
}
29+
30+
template <typename T>
31+
static inline std::ostream &
32+
operator<<(std::ostream &out, const smt2_format_containert<T> &c)
33+
{
34+
return smt2_format_rec(out, c.o);
35+
}
36+
37+
std::ostream &smt2_format_rec(std::ostream &, const exprt &);
38+
std::ostream &smt2_format_rec(std::ostream &, const typet &);
2439

2540
#endif // CPROVER_SOLVERS_SMT2_SMT2_FORMAT_H

0 commit comments

Comments
 (0)