2017-07-13 19:08:24 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-07-13 19:08:24 +00:00
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/Z3Interface.h>
|
2017-07-13 19:08:24 +00:00
|
|
|
|
2020-10-17 18:38:54 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
2017-07-13 19:08:24 +00:00
|
|
|
|
2020-12-03 01:06:30 +00:00
|
|
|
#ifdef HAVE_Z3_DLOPEN
|
|
|
|
#include <libsmtutil/Z3Loader.h>
|
|
|
|
#endif
|
2020-10-17 18:38:54 +00:00
|
|
|
|
2017-07-13 19:08:24 +00:00
|
|
|
using namespace std;
|
2020-05-19 12:14:46 +00:00
|
|
|
using namespace solidity::smtutil;
|
2020-10-17 18:38:54 +00:00
|
|
|
using namespace solidity::util;
|
2017-07-13 19:08:24 +00:00
|
|
|
|
2020-12-03 01:06:30 +00:00
|
|
|
bool Z3Interface::available()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_Z3_DLOPEN
|
|
|
|
return Z3Loader::get().available();
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:20:20 +00:00
|
|
|
Z3Interface::Z3Interface(std::optional<unsigned> _queryTimeout):
|
|
|
|
SolverInterface(_queryTimeout),
|
2017-07-13 19:08:24 +00:00
|
|
|
m_solver(m_context)
|
|
|
|
{
|
2019-12-03 00:11:21 +00:00
|
|
|
// These need to be set globally.
|
2018-03-04 13:41:27 +00:00
|
|
|
z3::set_param("rewriter.pull_cheap_ite", true);
|
2020-11-02 20:20:20 +00:00
|
|
|
|
|
|
|
if (m_queryTimeout)
|
|
|
|
m_context.set("timeout", int(*m_queryTimeout));
|
|
|
|
else
|
|
|
|
z3::set_param("rlimit", resourceLimit);
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Z3Interface::reset()
|
|
|
|
{
|
|
|
|
m_constants.clear();
|
|
|
|
m_functions.clear();
|
|
|
|
m_solver.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Z3Interface::push()
|
|
|
|
{
|
|
|
|
m_solver.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Z3Interface::pop()
|
|
|
|
{
|
|
|
|
m_solver.pop();
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:35:31 +00:00
|
|
|
void Z3Interface::declareVariable(string const& _name, SortPointer const& _sort)
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort, "");
|
2019-09-24 15:35:31 +00:00
|
|
|
if (_sort->kind == Kind::Function)
|
|
|
|
declareFunction(_name, *_sort);
|
2019-09-28 19:04:49 +00:00
|
|
|
else if (m_constants.count(_name))
|
2019-09-24 15:35:31 +00:00
|
|
|
m_constants.at(_name) = m_context.constant(_name.c_str(), z3Sort(*_sort));
|
2019-09-28 19:04:49 +00:00
|
|
|
else
|
2019-09-24 15:35:31 +00:00
|
|
|
m_constants.emplace(_name, m_context.constant(_name.c_str(), z3Sort(*_sort)));
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:57:02 +00:00
|
|
|
void Z3Interface::declareFunction(string const& _name, Sort const& _sort)
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort.kind == Kind::Function, "");
|
2019-09-28 19:04:49 +00:00
|
|
|
FunctionSort fSort = dynamic_cast<FunctionSort const&>(_sort);
|
|
|
|
if (m_functions.count(_name))
|
|
|
|
m_functions.at(_name) = m_context.function(_name.c_str(), z3Sort(fSort.domain), z3Sort(*fSort.codomain));
|
|
|
|
else
|
|
|
|
m_functions.emplace(_name, m_context.function(_name.c_str(), z3Sort(fSort.domain), z3Sort(*fSort.codomain)));
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Z3Interface::addAssertion(Expression const& _expr)
|
|
|
|
{
|
|
|
|
m_solver.add(toZ3Expr(_expr));
|
|
|
|
}
|
|
|
|
|
|
|
|
pair<CheckResult, vector<string>> Z3Interface::check(vector<Expression> const& _expressionsToEvaluate)
|
|
|
|
{
|
|
|
|
CheckResult result;
|
2017-10-11 13:15:17 +00:00
|
|
|
vector<string> values;
|
|
|
|
try
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2017-10-11 13:15:17 +00:00
|
|
|
switch (m_solver.check())
|
|
|
|
{
|
|
|
|
case z3::check_result::sat:
|
|
|
|
result = CheckResult::SATISFIABLE;
|
|
|
|
break;
|
|
|
|
case z3::check_result::unsat:
|
|
|
|
result = CheckResult::UNSATISFIABLE;
|
|
|
|
break;
|
|
|
|
case z3::check_result::unknown:
|
|
|
|
result = CheckResult::UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-07-27 12:13:22 +00:00
|
|
|
if (result == CheckResult::SATISFIABLE && !_expressionsToEvaluate.empty())
|
2017-10-11 13:15:17 +00:00
|
|
|
{
|
|
|
|
z3::model m = m_solver.get_model();
|
|
|
|
for (Expression const& e: _expressionsToEvaluate)
|
2019-12-11 16:31:36 +00:00
|
|
|
values.push_back(util::toString(m.eval(toZ3Expr(e))));
|
2017-10-11 13:15:17 +00:00
|
|
|
}
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
2020-11-02 20:20:20 +00:00
|
|
|
catch (z3::exception const& _err)
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2020-11-02 20:20:20 +00:00
|
|
|
set<string> msgs{
|
|
|
|
/// Resource limit (rlimit) exhausted.
|
|
|
|
"max. resource limit exceeded",
|
|
|
|
/// User given timeout exhausted.
|
|
|
|
"canceled"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (msgs.count(_err.msg()))
|
|
|
|
result = CheckResult::UNKNOWN;
|
|
|
|
else
|
|
|
|
result = CheckResult::ERROR;
|
2017-10-11 13:15:17 +00:00
|
|
|
values.clear();
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
2017-10-11 13:15:17 +00:00
|
|
|
|
2017-07-13 19:08:24 +00:00
|
|
|
return make_pair(result, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
z3::expr Z3Interface::toZ3Expr(Expression const& _expr)
|
|
|
|
{
|
|
|
|
if (_expr.arguments.empty() && m_constants.count(_expr.name))
|
|
|
|
return m_constants.at(_expr.name);
|
|
|
|
z3::expr_vector arguments(m_context);
|
|
|
|
for (auto const& arg: _expr.arguments)
|
|
|
|
arguments.push_back(toZ3Expr(arg));
|
|
|
|
|
2019-06-05 09:51:43 +00:00
|
|
|
try
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2019-06-05 09:51:43 +00:00
|
|
|
string const& n = _expr.name;
|
|
|
|
if (m_functions.count(n))
|
|
|
|
return m_functions.at(n)(arguments);
|
|
|
|
else if (m_constants.count(n))
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(arguments.empty(), "");
|
2019-06-05 09:51:43 +00:00
|
|
|
return m_constants.at(n);
|
|
|
|
}
|
|
|
|
else if (arguments.empty())
|
|
|
|
{
|
|
|
|
if (n == "true")
|
|
|
|
return m_context.bool_val(true);
|
|
|
|
else if (n == "false")
|
|
|
|
return m_context.bool_val(false);
|
2019-08-22 13:44:30 +00:00
|
|
|
else if (_expr.sort->kind == Kind::Sort)
|
|
|
|
{
|
|
|
|
auto sortSort = dynamic_pointer_cast<SortSort>(_expr.sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(sortSort, "");
|
2019-08-22 13:44:30 +00:00
|
|
|
return m_context.constant(n.c_str(), z3Sort(*sortSort->inner));
|
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
else
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return m_context.int_val(n.c_str());
|
|
|
|
}
|
|
|
|
catch (z3::exception const& _e)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.msg());
|
2019-06-05 09:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_expr.hasCorrectArity(), "");
|
2019-06-05 09:51:43 +00:00
|
|
|
if (n == "ite")
|
|
|
|
return z3::ite(arguments[0], arguments[1], arguments[2]);
|
|
|
|
else if (n == "not")
|
|
|
|
return !arguments[0];
|
|
|
|
else if (n == "and")
|
|
|
|
return arguments[0] && arguments[1];
|
|
|
|
else if (n == "or")
|
|
|
|
return arguments[0] || arguments[1];
|
2021-05-18 17:15:08 +00:00
|
|
|
else if (n == "=>")
|
2019-06-05 09:51:43 +00:00
|
|
|
return z3::implies(arguments[0], arguments[1]);
|
|
|
|
else if (n == "=")
|
|
|
|
return arguments[0] == arguments[1];
|
|
|
|
else if (n == "<")
|
|
|
|
return arguments[0] < arguments[1];
|
|
|
|
else if (n == "<=")
|
|
|
|
return arguments[0] <= arguments[1];
|
|
|
|
else if (n == ">")
|
|
|
|
return arguments[0] > arguments[1];
|
|
|
|
else if (n == ">=")
|
|
|
|
return arguments[0] >= arguments[1];
|
|
|
|
else if (n == "+")
|
|
|
|
return arguments[0] + arguments[1];
|
|
|
|
else if (n == "-")
|
|
|
|
return arguments[0] - arguments[1];
|
|
|
|
else if (n == "*")
|
|
|
|
return arguments[0] * arguments[1];
|
2021-05-18 17:47:00 +00:00
|
|
|
else if (n == "div")
|
2019-06-05 09:51:43 +00:00
|
|
|
return arguments[0] / arguments[1];
|
|
|
|
else if (n == "mod")
|
|
|
|
return z3::mod(arguments[0], arguments[1]);
|
2020-08-23 19:21:45 +00:00
|
|
|
else if (n == "bvnot")
|
|
|
|
return ~arguments[0];
|
2020-05-13 11:08:48 +00:00
|
|
|
else if (n == "bvand")
|
|
|
|
return arguments[0] & arguments[1];
|
2020-08-23 19:21:45 +00:00
|
|
|
else if (n == "bvor")
|
|
|
|
return arguments[0] | arguments[1];
|
|
|
|
else if (n == "bvxor")
|
|
|
|
return arguments[0] ^ arguments[1];
|
2020-09-04 14:55:45 +00:00
|
|
|
else if (n == "bvshl")
|
|
|
|
return z3::shl(arguments[0], arguments[1]);
|
|
|
|
else if (n == "bvlshr")
|
|
|
|
return z3::lshr(arguments[0], arguments[1]);
|
|
|
|
else if (n == "bvashr")
|
|
|
|
return z3::ashr(arguments[0], arguments[1]);
|
2020-05-13 11:08:48 +00:00
|
|
|
else if (n == "int2bv")
|
|
|
|
{
|
2020-06-15 10:51:58 +00:00
|
|
|
size_t size = std::stoul(_expr.arguments[1].name);
|
2020-07-13 20:40:33 +00:00
|
|
|
return z3::int2bv(static_cast<unsigned>(size), arguments[0]);
|
2020-05-13 11:08:48 +00:00
|
|
|
}
|
|
|
|
else if (n == "bv2int")
|
|
|
|
{
|
|
|
|
auto intSort = dynamic_pointer_cast<IntSort>(_expr.sort);
|
|
|
|
smtAssert(intSort, "");
|
|
|
|
return z3::bv2int(arguments[0], intSort->isSigned);
|
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
else if (n == "select")
|
|
|
|
return z3::select(arguments[0], arguments[1]);
|
|
|
|
else if (n == "store")
|
|
|
|
return z3::store(arguments[0], arguments[1], arguments[2]);
|
2019-08-22 13:44:30 +00:00
|
|
|
else if (n == "const_array")
|
|
|
|
{
|
|
|
|
shared_ptr<SortSort> sortSort = std::dynamic_pointer_cast<SortSort>(_expr.arguments[0].sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(sortSort, "");
|
2019-08-22 13:44:30 +00:00
|
|
|
auto arraySort = dynamic_pointer_cast<ArraySort>(sortSort->inner);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(arraySort && arraySort->domain, "");
|
2019-08-22 13:44:30 +00:00
|
|
|
return z3::const_array(z3Sort(*arraySort->domain), arguments[1]);
|
|
|
|
}
|
2020-04-03 13:10:16 +00:00
|
|
|
else if (n == "tuple_get")
|
|
|
|
{
|
2020-06-02 13:37:45 +00:00
|
|
|
size_t index = stoul(_expr.arguments[1].name);
|
2020-07-13 20:40:33 +00:00
|
|
|
return z3::func_decl(m_context, Z3_get_tuple_sort_field_decl(m_context, z3Sort(*_expr.arguments[0].sort), static_cast<unsigned>(index)))(arguments[0]);
|
2020-04-03 13:10:16 +00:00
|
|
|
}
|
2020-04-14 09:09:38 +00:00
|
|
|
else if (n == "tuple_constructor")
|
|
|
|
{
|
|
|
|
auto constructor = z3::func_decl(m_context, Z3_get_tuple_sort_mk_decl(m_context, z3Sort(*_expr.sort)));
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(constructor.arity() == arguments.size(), "");
|
2020-04-14 09:09:38 +00:00
|
|
|
z3::expr_vector args(m_context);
|
|
|
|
for (auto const& arg: arguments)
|
|
|
|
args.push_back(arg);
|
|
|
|
return constructor(args);
|
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "");
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
catch (z3::exception const& _e)
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.msg());
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "");
|
2017-07-13 19:08:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 18:38:54 +00:00
|
|
|
Expression Z3Interface::fromZ3Expr(z3::expr const& _expr)
|
|
|
|
{
|
|
|
|
auto sort = fromZ3Sort(_expr.get_sort());
|
|
|
|
if (_expr.is_const() || _expr.is_var())
|
|
|
|
return Expression(_expr.to_string(), {}, sort);
|
|
|
|
|
2021-10-06 09:45:15 +00:00
|
|
|
if (_expr.is_quantifier())
|
|
|
|
{
|
|
|
|
string quantifierName;
|
|
|
|
if (_expr.is_exists())
|
|
|
|
quantifierName = "exists";
|
|
|
|
else if (_expr.is_forall())
|
|
|
|
quantifierName = "forall";
|
|
|
|
else if (_expr.is_lambda())
|
|
|
|
quantifierName = "lambda";
|
|
|
|
else
|
|
|
|
smtAssert(false, "");
|
|
|
|
return Expression(quantifierName, {fromZ3Expr(_expr.body())}, sort);
|
|
|
|
}
|
2020-10-17 18:38:54 +00:00
|
|
|
smtAssert(_expr.is_app(), "");
|
|
|
|
vector<Expression> arguments;
|
|
|
|
for (unsigned i = 0; i < _expr.num_args(); ++i)
|
|
|
|
arguments.push_back(fromZ3Expr(_expr.arg(i)));
|
|
|
|
|
|
|
|
auto kind = _expr.decl().decl_kind();
|
2022-05-10 09:23:51 +00:00
|
|
|
|
2020-10-17 18:38:54 +00:00
|
|
|
if (_expr.is_ite())
|
|
|
|
return Expression::ite(arguments[0], arguments[1], arguments[2]);
|
|
|
|
else if (_expr.is_not())
|
|
|
|
return !arguments[0];
|
|
|
|
else if (_expr.is_and())
|
2021-10-06 09:45:15 +00:00
|
|
|
return Expression::mkAnd(arguments);
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (_expr.is_or())
|
2021-10-06 09:45:15 +00:00
|
|
|
return Expression::mkOr(arguments);
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (_expr.is_implies())
|
|
|
|
return Expression::implies(arguments[0], arguments[1]);
|
|
|
|
else if (_expr.is_eq())
|
2021-10-06 09:45:15 +00:00
|
|
|
{
|
|
|
|
smtAssert(arguments.size() == 2, "");
|
2020-10-17 18:38:54 +00:00
|
|
|
return arguments[0] == arguments[1];
|
2021-10-06 09:45:15 +00:00
|
|
|
}
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_ULT || kind == Z3_OP_SLT)
|
|
|
|
return arguments[0] < arguments[1];
|
2021-10-06 09:45:15 +00:00
|
|
|
else if (kind == Z3_OP_LE || kind == Z3_OP_ULEQ || kind == Z3_OP_SLEQ)
|
2020-10-17 18:38:54 +00:00
|
|
|
return arguments[0] <= arguments[1];
|
|
|
|
else if (kind == Z3_OP_GT || kind == Z3_OP_SGT)
|
|
|
|
return arguments[0] > arguments[1];
|
2021-10-06 09:45:15 +00:00
|
|
|
else if (kind == Z3_OP_GE || kind == Z3_OP_UGEQ || kind == Z3_OP_SGEQ)
|
2020-10-17 18:38:54 +00:00
|
|
|
return arguments[0] >= arguments[1];
|
|
|
|
else if (kind == Z3_OP_ADD)
|
2021-10-06 09:45:15 +00:00
|
|
|
return Expression::mkPlus(arguments);
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_SUB)
|
2021-10-06 09:45:15 +00:00
|
|
|
{
|
|
|
|
smtAssert(arguments.size() == 2, "");
|
2020-10-17 18:38:54 +00:00
|
|
|
return arguments[0] - arguments[1];
|
2021-10-06 09:45:15 +00:00
|
|
|
}
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_MUL)
|
2021-10-06 09:45:15 +00:00
|
|
|
return Expression::mkMul(arguments);
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_DIV)
|
2021-10-06 09:45:15 +00:00
|
|
|
{
|
|
|
|
smtAssert(arguments.size() == 2, "");
|
2020-10-17 18:38:54 +00:00
|
|
|
return arguments[0] / arguments[1];
|
2021-10-06 09:45:15 +00:00
|
|
|
}
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_MOD)
|
|
|
|
return arguments[0] % arguments[1];
|
|
|
|
else if (kind == Z3_OP_XOR)
|
|
|
|
return arguments[0] ^ arguments[1];
|
2021-10-06 09:45:15 +00:00
|
|
|
else if (kind == Z3_OP_BNOT)
|
|
|
|
return !arguments[0];
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_BSHL)
|
|
|
|
return arguments[0] << arguments[1];
|
|
|
|
else if (kind == Z3_OP_BLSHR)
|
|
|
|
return arguments[0] >> arguments[1];
|
|
|
|
else if (kind == Z3_OP_BASHR)
|
|
|
|
return Expression::ashr(arguments[0], arguments[1]);
|
|
|
|
else if (kind == Z3_OP_INT2BV)
|
2021-10-06 09:45:15 +00:00
|
|
|
return Expression::int2bv(arguments[0], _expr.get_sort().bv_size());
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_BV2INT)
|
2021-10-06 09:45:15 +00:00
|
|
|
return Expression::bv2int(arguments[0]);
|
|
|
|
else if (kind == Z3_OP_EXTRACT)
|
|
|
|
return Expression("extract", arguments, sort);
|
2020-10-17 18:38:54 +00:00
|
|
|
else if (kind == Z3_OP_SELECT)
|
|
|
|
return Expression::select(arguments[0], arguments[1]);
|
|
|
|
else if (kind == Z3_OP_STORE)
|
|
|
|
return Expression::store(arguments[0], arguments[1], arguments[2]);
|
|
|
|
else if (kind == Z3_OP_CONST_ARRAY)
|
|
|
|
{
|
|
|
|
auto sortSort = make_shared<SortSort>(fromZ3Sort(_expr.get_sort()));
|
|
|
|
return Expression::const_array(Expression(sortSort), arguments[0]);
|
|
|
|
}
|
|
|
|
else if (kind == Z3_OP_DT_CONSTRUCTOR)
|
|
|
|
{
|
|
|
|
auto sortSort = make_shared<SortSort>(fromZ3Sort(_expr.get_sort()));
|
|
|
|
return Expression::tuple_constructor(Expression(sortSort), arguments);
|
|
|
|
}
|
|
|
|
else if (kind == Z3_OP_DT_ACCESSOR)
|
2021-10-06 09:45:15 +00:00
|
|
|
return Expression("dt_accessor_" + _expr.decl().name().str(), arguments, sort);
|
|
|
|
else if (kind == Z3_OP_DT_IS)
|
|
|
|
return Expression("dt_is", {arguments.at(0)}, sort);
|
2022-05-10 09:23:51 +00:00
|
|
|
else if (
|
|
|
|
kind == Z3_OP_UNINTERPRETED ||
|
|
|
|
kind == Z3_OP_RECURSIVE
|
|
|
|
)
|
2020-10-17 18:38:54 +00:00
|
|
|
return Expression(_expr.decl().name().str(), arguments, fromZ3Sort(_expr.get_sort()));
|
|
|
|
|
|
|
|
smtAssert(false, "");
|
|
|
|
}
|
|
|
|
|
2018-11-21 14:13:50 +00:00
|
|
|
z3::sort Z3Interface::z3Sort(Sort const& _sort)
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
switch (_sort.kind)
|
2017-07-13 19:08:24 +00:00
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
case Kind::Bool:
|
2017-07-13 19:08:24 +00:00
|
|
|
return m_context.bool_sort();
|
2018-11-21 14:13:50 +00:00
|
|
|
case Kind::Int:
|
2017-07-13 19:08:24 +00:00
|
|
|
return m_context.int_sort();
|
2020-05-11 17:56:29 +00:00
|
|
|
case Kind::BitVector:
|
|
|
|
return m_context.bv_sort(dynamic_cast<BitVectorSort const&>(_sort).size);
|
2018-11-22 10:24:12 +00:00
|
|
|
case Kind::Array:
|
|
|
|
{
|
|
|
|
auto const& arraySort = dynamic_cast<ArraySort const&>(_sort);
|
|
|
|
return m_context.array_sort(z3Sort(*arraySort.domain), z3Sort(*arraySort.range));
|
|
|
|
}
|
2020-04-03 13:10:16 +00:00
|
|
|
case Kind::Tuple:
|
|
|
|
{
|
|
|
|
auto const& tupleSort = dynamic_cast<TupleSort const&>(_sort);
|
|
|
|
vector<char const*> cMembers;
|
|
|
|
for (auto const& member: tupleSort.members)
|
|
|
|
cMembers.emplace_back(member.c_str());
|
|
|
|
/// Using this instead of the function below because with that one
|
|
|
|
/// we can't use `&sorts[0]` here.
|
|
|
|
vector<z3::sort> sorts;
|
|
|
|
for (auto const& sort: tupleSort.components)
|
|
|
|
sorts.push_back(z3Sort(*sort));
|
|
|
|
z3::func_decl_vector projs(m_context);
|
|
|
|
z3::func_decl tupleConstructor = m_context.tuple_sort(
|
|
|
|
tupleSort.name.c_str(),
|
2020-07-13 20:40:33 +00:00
|
|
|
static_cast<unsigned>(tupleSort.members.size()),
|
2020-04-22 18:41:24 +00:00
|
|
|
cMembers.data(),
|
|
|
|
sorts.data(),
|
2020-04-03 13:10:16 +00:00
|
|
|
projs
|
|
|
|
);
|
|
|
|
return tupleConstructor.range();
|
|
|
|
}
|
|
|
|
|
2017-07-13 19:08:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "");
|
2017-07-13 19:08:24 +00:00
|
|
|
// Cannot be reached.
|
|
|
|
return m_context.int_sort();
|
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
|
2018-11-21 14:13:50 +00:00
|
|
|
z3::sort_vector Z3Interface::z3Sort(vector<SortPointer> const& _sorts)
|
2018-10-25 14:00:09 +00:00
|
|
|
{
|
|
|
|
z3::sort_vector z3Sorts(m_context);
|
|
|
|
for (auto const& _sort: _sorts)
|
2018-11-21 14:13:50 +00:00
|
|
|
z3Sorts.push_back(z3Sort(*_sort));
|
2018-10-25 14:00:09 +00:00
|
|
|
return z3Sorts;
|
|
|
|
}
|
2020-10-17 18:38:54 +00:00
|
|
|
|
|
|
|
SortPointer Z3Interface::fromZ3Sort(z3::sort const& _sort)
|
|
|
|
{
|
|
|
|
if (_sort.is_bool())
|
|
|
|
return SortProvider::boolSort;
|
|
|
|
if (_sort.is_int())
|
|
|
|
return SortProvider::sintSort;
|
|
|
|
if (_sort.is_bv())
|
|
|
|
return make_shared<BitVectorSort>(_sort.bv_size());
|
|
|
|
if (_sort.is_array())
|
|
|
|
return make_shared<ArraySort>(fromZ3Sort(_sort.array_domain()), fromZ3Sort(_sort.array_range()));
|
|
|
|
if (_sort.is_datatype())
|
|
|
|
{
|
|
|
|
auto name = _sort.name().str();
|
|
|
|
auto constructor = z3::func_decl(m_context, Z3_get_tuple_sort_mk_decl(m_context, _sort));
|
|
|
|
vector<string> memberNames;
|
|
|
|
vector<SortPointer> memberSorts;
|
|
|
|
for (unsigned i = 0; i < constructor.arity(); ++i)
|
|
|
|
{
|
|
|
|
auto accessor = z3::func_decl(m_context, Z3_get_tuple_sort_field_decl(m_context, _sort, i));
|
|
|
|
memberNames.push_back(accessor.name().str());
|
|
|
|
memberSorts.push_back(fromZ3Sort(accessor.range()));
|
|
|
|
}
|
|
|
|
return make_shared<TupleSort>(name, memberNames, memberSorts);
|
|
|
|
}
|
|
|
|
smtAssert(false, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<SortPointer> Z3Interface::fromZ3Sort(z3::sort_vector const& _sorts)
|
|
|
|
{
|
|
|
|
return applyMap(_sorts, [this](auto const& sort) { return fromZ3Sort(sort); });
|
|
|
|
}
|