2015-10-21 14:43:31 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum 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.
|
|
|
|
|
|
|
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Component that translates Solidity code into the why3 programming language.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/formal/Why3Translator.h>
|
2015-11-22 19:39:24 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2015-10-21 14:43:31 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
bool Why3Translator::process(SourceUnit const& _source)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-11-22 19:39:24 +00:00
|
|
|
if (m_lines.size() != 1 || !m_lines.back().contents.empty())
|
2015-10-21 14:43:31 +00:00
|
|
|
fatalError(_source, "Multiple source units not yet supported");
|
|
|
|
appendPreface();
|
|
|
|
_source.accept(*this);
|
|
|
|
}
|
2016-03-18 08:22:15 +00:00
|
|
|
catch (FatalError& /*_e*/)
|
2015-10-21 14:43:31 +00:00
|
|
|
{
|
|
|
|
solAssert(m_errorOccured, "");
|
|
|
|
}
|
|
|
|
return !m_errorOccured;
|
|
|
|
}
|
|
|
|
|
2015-11-22 19:39:24 +00:00
|
|
|
string Why3Translator::translation() const
|
|
|
|
{
|
|
|
|
string result;
|
|
|
|
for (auto const& line: m_lines)
|
|
|
|
result += string(line.indentation, '\t') + line.contents + "\n";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-21 14:43:31 +00:00
|
|
|
void Why3Translator::error(ASTNode const& _node, string const& _description)
|
|
|
|
{
|
2015-10-27 16:45:58 +00:00
|
|
|
auto err = make_shared<Error>(Error::Type::Why3TranslatorError);
|
2015-10-21 14:43:31 +00:00
|
|
|
*err <<
|
|
|
|
errinfo_sourceLocation(_node.location()) <<
|
|
|
|
errinfo_comment(_description);
|
|
|
|
m_errors.push_back(err);
|
|
|
|
m_errorOccured = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Why3Translator::fatalError(ASTNode const& _node, string const& _description)
|
|
|
|
{
|
|
|
|
error(_node, _description);
|
|
|
|
BOOST_THROW_EXCEPTION(FatalError());
|
|
|
|
}
|
|
|
|
|
|
|
|
string Why3Translator::toFormalType(Type const& _type) const
|
|
|
|
{
|
2016-07-13 09:16:00 +00:00
|
|
|
if (_type.category() == Type::Category::Bool)
|
|
|
|
return "bool";
|
|
|
|
else if (auto type = dynamic_cast<IntegerType const*>(&_type))
|
2015-10-21 14:43:31 +00:00
|
|
|
{
|
|
|
|
if (!type->isAddress() && !type->isSigned() && type->numBits() == 256)
|
|
|
|
return "uint256";
|
|
|
|
}
|
|
|
|
else if (auto type = dynamic_cast<ArrayType const*>(&_type))
|
2016-09-07 15:41:12 +00:00
|
|
|
{
|
2015-10-21 14:43:31 +00:00
|
|
|
if (!type->isByteArray() && type->isDynamicallySized() && type->dataStoredIn(DataLocation::Memory))
|
|
|
|
{
|
2016-09-07 18:16:46 +00:00
|
|
|
// Not catching NoFormalType exception. Let the caller deal with it.
|
2015-10-21 14:43:31 +00:00
|
|
|
string base = toFormalType(*type->baseType());
|
2016-09-07 18:16:46 +00:00
|
|
|
return "array " + base;
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
2016-09-07 15:41:12 +00:00
|
|
|
}
|
|
|
|
else if (auto mappingType = dynamic_cast<MappingType const*>(&_type))
|
|
|
|
{
|
|
|
|
solAssert(mappingType->keyType(), "A mappingType misses a keyType.");
|
|
|
|
if (dynamic_cast<IntegerType const*>(&*mappingType->keyType()))
|
|
|
|
{
|
|
|
|
//@TODO Use the information from the key type and specify the length of the array as an invariant.
|
|
|
|
// Also the constructor need to specify the length of the array.
|
2016-09-07 18:16:46 +00:00
|
|
|
solAssert(mappingType->valueType(), "A mappingType misses a valueType.");
|
|
|
|
// Not catching NoFormalType exception. Let the caller deal with it.
|
|
|
|
string valueTypeFormal = toFormalType(*mappingType->valueType());
|
|
|
|
return "array " + valueTypeFormal;
|
2016-09-07 15:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
|
2016-09-07 18:16:46 +00:00
|
|
|
BOOST_THROW_EXCEPTION(NoFormalType()
|
|
|
|
<< errinfo_noFormalTypeFrom(_type.toString(true)));
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Why3Translator::addLine(string const& _line)
|
|
|
|
{
|
|
|
|
newLine();
|
|
|
|
add(_line);
|
|
|
|
newLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Why3Translator::add(string const& _str)
|
|
|
|
{
|
2015-11-22 19:39:24 +00:00
|
|
|
m_lines.back().contents += _str;
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Why3Translator::newLine()
|
|
|
|
{
|
2015-11-22 19:39:24 +00:00
|
|
|
if (!m_lines.back().contents.empty())
|
|
|
|
m_lines.push_back({"", m_lines.back().indentation});
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Why3Translator::unindent()
|
|
|
|
{
|
2015-11-11 14:20:53 +00:00
|
|
|
newLine();
|
2015-11-22 19:39:24 +00:00
|
|
|
solAssert(m_lines.back().indentation > 0, "");
|
|
|
|
m_lines.back().indentation--;
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
if (m_seenContract)
|
|
|
|
error(_contract, "More than one contract not supported.");
|
|
|
|
m_seenContract = true;
|
2016-07-13 09:16:00 +00:00
|
|
|
m_currentContract.contract = &_contract;
|
2015-10-21 14:43:31 +00:00
|
|
|
if (_contract.isLibrary())
|
|
|
|
error(_contract, "Libraries not supported.");
|
|
|
|
|
2015-11-16 19:35:54 +00:00
|
|
|
addLine("module Contract_" + _contract.name());
|
|
|
|
indent();
|
|
|
|
addLine("use import int.Int");
|
|
|
|
addLine("use import ref.Ref");
|
|
|
|
addLine("use import map.Map");
|
|
|
|
addLine("use import array.Array");
|
|
|
|
addLine("use import int.ComputerDivision");
|
|
|
|
addLine("use import mach.int.Unsigned");
|
|
|
|
addLine("use import UInt256");
|
2016-07-13 09:16:00 +00:00
|
|
|
addLine("exception Revert");
|
|
|
|
addLine("exception Return");
|
2015-11-16 19:35:54 +00:00
|
|
|
|
2016-07-13 09:16:00 +00:00
|
|
|
if (_contract.stateVariables().empty())
|
|
|
|
addLine("type state = ()");
|
|
|
|
else
|
2015-11-16 19:35:54 +00:00
|
|
|
{
|
2016-07-13 09:16:00 +00:00
|
|
|
addLine("type state = {");
|
|
|
|
indent();
|
|
|
|
m_currentContract.stateVariables = _contract.stateVariables();
|
|
|
|
for (VariableDeclaration const* variable: m_currentContract.stateVariables)
|
|
|
|
{
|
2016-09-07 18:16:46 +00:00
|
|
|
string varType;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
varType = toFormalType(*variable->annotation().type);
|
|
|
|
}
|
|
|
|
catch (NoFormalType &err)
|
|
|
|
{
|
|
|
|
string const* typeNamePtr = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
|
|
|
|
string typeName = typeNamePtr ? " \"" + *typeNamePtr + "\"" : "";
|
|
|
|
fatalError(*variable, "Type" + typeName + " not supported for state variable.");
|
|
|
|
}
|
2016-07-13 09:16:00 +00:00
|
|
|
addLine("mutable _" + variable->name() + ": " + varType);
|
|
|
|
}
|
|
|
|
unindent();
|
|
|
|
addLine("}");
|
2015-11-16 19:35:54 +00:00
|
|
|
}
|
2016-07-13 09:16:00 +00:00
|
|
|
|
|
|
|
addLine("type account = {");
|
|
|
|
indent();
|
|
|
|
addLine("mutable balance: uint256;");
|
|
|
|
addLine("storage: state");
|
2015-11-16 19:35:54 +00:00
|
|
|
unindent();
|
|
|
|
addLine("}");
|
2015-10-21 14:43:31 +00:00
|
|
|
|
2016-07-13 09:16:00 +00:00
|
|
|
addLine("val external_call (this: account): bool");
|
|
|
|
indent();
|
|
|
|
addLine("ensures { result = false -> this = (old this) }");
|
|
|
|
addLine("writes { this }");
|
|
|
|
addSourceFromDocStrings(m_currentContract.contract->annotation());
|
|
|
|
unindent();
|
|
|
|
|
2015-11-16 19:35:54 +00:00
|
|
|
if (!_contract.baseContracts().empty())
|
|
|
|
error(*_contract.baseContracts().front(), "Inheritance not supported.");
|
|
|
|
if (!_contract.definedStructs().empty())
|
|
|
|
error(*_contract.definedStructs().front(), "User-defined types not supported.");
|
|
|
|
if (!_contract.definedEnums().empty())
|
|
|
|
error(*_contract.definedEnums().front(), "User-defined types not supported.");
|
|
|
|
if (!_contract.events().empty())
|
|
|
|
error(*_contract.events().front(), "Events not supported.");
|
|
|
|
if (!_contract.functionModifiers().empty())
|
|
|
|
error(*_contract.functionModifiers().front(), "Modifiers not supported.");
|
|
|
|
|
|
|
|
ASTNode::listAccept(_contract.definedFunctions(), *this);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 09:16:00 +00:00
|
|
|
void Why3Translator::endVisit(ContractDefinition const&)
|
2015-11-16 19:35:54 +00:00
|
|
|
{
|
2016-07-13 09:16:00 +00:00
|
|
|
m_currentContract.reset();
|
2015-11-16 19:35:54 +00:00
|
|
|
unindent();
|
|
|
|
addLine("end");
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
if (!_function.isImplemented())
|
|
|
|
{
|
|
|
|
error(_function, "Unimplemented functions not supported.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_function.name().empty())
|
|
|
|
{
|
|
|
|
error(_function, "Fallback functions not supported.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!_function.modifiers().empty())
|
|
|
|
{
|
|
|
|
error(_function, "Modifiers not supported.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:26:04 +00:00
|
|
|
m_localVariables.clear();
|
|
|
|
for (auto const& var: _function.parameters())
|
|
|
|
m_localVariables[var->name()] = var.get();
|
|
|
|
for (auto const& var: _function.returnParameters())
|
|
|
|
m_localVariables[var->name()] = var.get();
|
|
|
|
for (auto const& var: _function.localVariables())
|
|
|
|
m_localVariables[var->name()] = var;
|
|
|
|
|
2015-10-21 14:43:31 +00:00
|
|
|
add("let rec _" + _function.name());
|
2016-07-13 09:16:00 +00:00
|
|
|
add(" (this: account)");
|
2015-10-21 14:43:31 +00:00
|
|
|
for (auto const& param: _function.parameters())
|
|
|
|
{
|
2016-09-07 18:16:46 +00:00
|
|
|
string paramType;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
paramType = toFormalType(*param->annotation().type);
|
|
|
|
}
|
|
|
|
catch (NoFormalType &err)
|
|
|
|
{
|
|
|
|
string const* typeName = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
|
|
|
|
error(*param, "Parameter type \"" + (typeName ? *typeName : "") + "\" not supported.");
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
if (param->name().empty())
|
|
|
|
error(*param, "Anonymous function parameters not supported.");
|
|
|
|
add(" (arg_" + param->name() + ": " + paramType + ")");
|
|
|
|
}
|
|
|
|
add(":");
|
|
|
|
|
|
|
|
indent();
|
|
|
|
indent();
|
|
|
|
string retString = "(";
|
|
|
|
for (auto const& retParam: _function.returnParameters())
|
|
|
|
{
|
2016-09-07 18:16:46 +00:00
|
|
|
string paramType;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
paramType = toFormalType(*retParam->annotation().type);
|
|
|
|
}
|
|
|
|
catch (NoFormalType &err)
|
|
|
|
{
|
|
|
|
string const* typeName = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
|
|
|
|
error(*retParam, "Parameter type " + (typeName ? *typeName : "") + " not supported.");
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
if (retString.size() != 1)
|
|
|
|
retString += ", ";
|
|
|
|
retString += paramType;
|
|
|
|
}
|
2015-11-11 14:20:53 +00:00
|
|
|
add(retString + ")");
|
2015-10-21 14:43:31 +00:00
|
|
|
unindent();
|
|
|
|
|
|
|
|
addSourceFromDocStrings(_function.annotation());
|
2016-07-13 09:16:00 +00:00
|
|
|
if (!m_currentContract.contract)
|
|
|
|
error(_function, "Only functions inside contracts allowed.");
|
|
|
|
addSourceFromDocStrings(m_currentContract.contract->annotation());
|
|
|
|
|
|
|
|
if (_function.isDeclaredConst())
|
|
|
|
addLine("ensures { (old this) = this }");
|
|
|
|
else
|
|
|
|
addLine("writes { this }");
|
2015-10-21 14:43:31 +00:00
|
|
|
|
|
|
|
addLine("=");
|
|
|
|
|
2016-07-13 09:16:00 +00:00
|
|
|
// store the prestate in the case we need to revert
|
|
|
|
addLine("let prestate = {balance = this.balance; storage = " + copyOfStorage() + "} in ");
|
|
|
|
|
2015-10-21 14:43:31 +00:00
|
|
|
// initialise local variables
|
|
|
|
for (auto const& variable: _function.parameters())
|
|
|
|
addLine("let _" + variable->name() + " = ref arg_" + variable->name() + " in");
|
|
|
|
for (auto const& variable: _function.returnParameters())
|
|
|
|
{
|
|
|
|
if (variable->name().empty())
|
|
|
|
error(*variable, "Unnamed return variables not yet supported.");
|
2016-09-07 18:16:46 +00:00
|
|
|
string varType;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
varType = toFormalType(*variable->annotation().type);
|
|
|
|
}
|
|
|
|
catch (NoFormalType &err)
|
|
|
|
{
|
|
|
|
string const* typeNamePtr = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
|
|
|
|
error(*variable, "Type " + (typeNamePtr ? *typeNamePtr : "") + "in return parameter not yet supported.");
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
addLine("let _" + variable->name() + ": ref " + varType + " = ref (of_int 0) in");
|
|
|
|
}
|
|
|
|
for (VariableDeclaration const* variable: _function.localVariables())
|
|
|
|
{
|
|
|
|
if (variable->name().empty())
|
|
|
|
error(*variable, "Unnamed variables not yet supported.");
|
2016-09-07 18:16:46 +00:00
|
|
|
string varType;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
varType = toFormalType(*variable->annotation().type);
|
|
|
|
}
|
|
|
|
catch (NoFormalType &err)
|
|
|
|
{
|
|
|
|
string const* typeNamePtr = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
|
|
|
|
error(*variable, "Type " + (typeNamePtr ? *typeNamePtr : "") + "in variable declaration not yet supported.");
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
addLine("let _" + variable->name() + ": ref " + varType + " = ref (of_int 0) in");
|
|
|
|
}
|
|
|
|
addLine("try");
|
|
|
|
|
|
|
|
_function.body().accept(*this);
|
2015-11-11 14:20:53 +00:00
|
|
|
add(";");
|
2016-07-13 09:16:00 +00:00
|
|
|
addLine("raise Return");
|
2015-10-21 14:43:31 +00:00
|
|
|
|
|
|
|
string retVals;
|
|
|
|
for (auto const& variable: _function.returnParameters())
|
|
|
|
{
|
|
|
|
if (!retVals.empty())
|
|
|
|
retVals += ", ";
|
|
|
|
retVals += "!_" + variable->name();
|
|
|
|
}
|
2016-07-13 09:16:00 +00:00
|
|
|
addLine("with Return -> (" + retVals + ") |");
|
|
|
|
string reversion = " Revert -> this.balance <- prestate.balance; ";
|
|
|
|
for (auto const* variable: m_currentContract.stateVariables)
|
|
|
|
reversion += "this.storage._" + variable->name() + " <- prestate.storage._" + variable->name() + "; ";
|
|
|
|
//@TODO in case of reversion the return values are wrong - we need to change the
|
|
|
|
// return type to include a bool to signify if an exception was thrown.
|
|
|
|
reversion += "(" + retVals + ")";
|
|
|
|
addLine(reversion);
|
2015-10-21 14:43:31 +00:00
|
|
|
unindent();
|
|
|
|
addLine("end");
|
|
|
|
addLine("");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:26:04 +00:00
|
|
|
void Why3Translator::endVisit(FunctionDefinition const&)
|
|
|
|
{
|
|
|
|
m_localVariables.clear();
|
|
|
|
}
|
|
|
|
|
2015-10-21 14:43:31 +00:00
|
|
|
bool Why3Translator::visit(Block const& _node)
|
|
|
|
{
|
|
|
|
addSourceFromDocStrings(_node.annotation());
|
2015-11-11 14:20:53 +00:00
|
|
|
add("begin");
|
2015-10-21 14:43:31 +00:00
|
|
|
indent();
|
2015-11-11 14:20:53 +00:00
|
|
|
for (size_t i = 0; i < _node.statements().size(); ++i)
|
|
|
|
{
|
|
|
|
_node.statements()[i]->accept(*this);
|
2015-11-22 19:39:24 +00:00
|
|
|
if (i != _node.statements().size() - 1)
|
|
|
|
{
|
|
|
|
auto it = m_lines.end() - 1;
|
2015-11-24 15:20:12 +00:00
|
|
|
while (it != m_lines.begin() && it->contents.empty())
|
|
|
|
--it;
|
2015-11-22 19:39:24 +00:00
|
|
|
if (!boost::algorithm::ends_with(it->contents, "begin"))
|
|
|
|
it->contents += ";";
|
|
|
|
}
|
2015-11-11 14:20:53 +00:00
|
|
|
newLine();
|
|
|
|
}
|
|
|
|
unindent();
|
|
|
|
add("end");
|
|
|
|
return false;
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(IfStatement const& _node)
|
|
|
|
{
|
|
|
|
addSourceFromDocStrings(_node.annotation());
|
|
|
|
|
|
|
|
add("if ");
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
add(" then");
|
2015-11-11 14:20:53 +00:00
|
|
|
visitIndentedUnlessBlock(_node.trueStatement());
|
2015-10-21 14:43:31 +00:00
|
|
|
if (_node.falseStatement())
|
|
|
|
{
|
2015-11-11 14:20:53 +00:00
|
|
|
newLine();
|
|
|
|
add("else");
|
|
|
|
visitIndentedUnlessBlock(*_node.falseStatement());
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(WhileStatement const& _node)
|
|
|
|
{
|
|
|
|
addSourceFromDocStrings(_node.annotation());
|
|
|
|
|
|
|
|
add("while ");
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
newLine();
|
2015-11-11 14:20:53 +00:00
|
|
|
add("do");
|
|
|
|
visitIndentedUnlessBlock(_node.body());
|
|
|
|
add("done");
|
2015-10-21 14:43:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(Return const& _node)
|
|
|
|
{
|
|
|
|
addSourceFromDocStrings(_node.annotation());
|
|
|
|
|
|
|
|
if (_node.expression())
|
|
|
|
{
|
|
|
|
solAssert(!!_node.annotation().functionReturnParameters, "");
|
|
|
|
auto const& params = _node.annotation().functionReturnParameters->parameters();
|
|
|
|
if (params.size() != 1)
|
|
|
|
{
|
|
|
|
error(_node, "Directly returning tuples not supported. Rather assign to return variable.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
add("begin _" + params.front()->name() + " := ");
|
|
|
|
_node.expression()->accept(*this);
|
2016-07-13 09:16:00 +00:00
|
|
|
add("; raise Return end");
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
else
|
2016-07-13 09:16:00 +00:00
|
|
|
add("raise Return");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(Throw const& _node)
|
|
|
|
{
|
|
|
|
addSourceFromDocStrings(_node.annotation());
|
|
|
|
add("raise Revert");
|
2015-10-21 14:43:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(VariableDeclarationStatement const& _node)
|
|
|
|
{
|
|
|
|
addSourceFromDocStrings(_node.annotation());
|
|
|
|
|
|
|
|
if (_node.declarations().size() != 1)
|
|
|
|
{
|
|
|
|
error(_node, "Multiple variables not supported.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_node.initialValue())
|
|
|
|
{
|
|
|
|
add("_" + _node.declarations().front()->name() + " := ");
|
|
|
|
_node.initialValue()->accept(*this);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(ExpressionStatement const& _node)
|
|
|
|
{
|
|
|
|
addSourceFromDocStrings(_node.annotation());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(Assignment const& _node)
|
|
|
|
{
|
|
|
|
if (_node.assignmentOperator() != Token::Assign)
|
|
|
|
error(_node, "Compound assignment not supported.");
|
|
|
|
|
|
|
|
_node.leftHandSide().accept(*this);
|
2016-07-13 09:16:00 +00:00
|
|
|
|
|
|
|
add(m_currentLValueIsRef ? " := " : " <- ");
|
2015-10-21 14:43:31 +00:00
|
|
|
_node.rightHandSide().accept(*this);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(TupleExpression const& _node)
|
|
|
|
{
|
|
|
|
if (_node.components().size() != 1)
|
|
|
|
error(_node, "Only tuples with exactly one component supported.");
|
|
|
|
add("(");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(UnaryOperation const& _unaryOperation)
|
|
|
|
{
|
2016-09-07 18:16:46 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
toFormalType(*_unaryOperation.annotation().type);
|
|
|
|
}
|
|
|
|
catch (NoFormalType &err)
|
|
|
|
{
|
|
|
|
string const* typeNamePtr = boost::get_error_info<errinfo_noFormalTypeFrom>(err);
|
|
|
|
error(_unaryOperation, "Type \"" + (typeNamePtr ? *typeNamePtr : "") + "\" supported in unary operation.");
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
|
|
|
|
switch (_unaryOperation.getOperator())
|
|
|
|
{
|
|
|
|
case Token::Not: // !
|
|
|
|
add("(not ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error(_unaryOperation, "Operator not supported.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_unaryOperation.subExpression().accept(*this);
|
|
|
|
add(")");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(BinaryOperation const& _binaryOperation)
|
|
|
|
{
|
|
|
|
Expression const& leftExpression = _binaryOperation.leftExpression();
|
|
|
|
Expression const& rightExpression = _binaryOperation.rightExpression();
|
|
|
|
solAssert(!!_binaryOperation.annotation().commonType, "");
|
|
|
|
Type const& commonType = *_binaryOperation.annotation().commonType;
|
|
|
|
Token::Value const c_op = _binaryOperation.getOperator();
|
|
|
|
|
2016-03-29 20:08:51 +00:00
|
|
|
if (commonType.category() == Type::Category::RationalNumber)
|
2015-10-21 14:43:31 +00:00
|
|
|
{
|
2016-03-29 20:08:51 +00:00
|
|
|
auto const& constantNumber = dynamic_cast<RationalNumberType const&>(commonType);
|
2016-05-10 12:30:24 +00:00
|
|
|
if (constantNumber.isFractional())
|
2016-02-18 22:39:11 +00:00
|
|
|
error(_binaryOperation, "Fractional numbers not supported.");
|
2016-08-20 01:44:26 +00:00
|
|
|
else
|
|
|
|
add("(of_int " + toString(commonType.literalValue(nullptr)) + ")");
|
2015-10-21 14:43:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
static const map<Token::Value, char const*> optrans({
|
|
|
|
{Token::And, " && "},
|
|
|
|
{Token::Or, " || "},
|
|
|
|
{Token::BitOr, " lor "},
|
|
|
|
{Token::BitXor, " lxor "},
|
|
|
|
{Token::BitAnd, " land "},
|
|
|
|
{Token::Add, " + "},
|
|
|
|
{Token::Sub, " - "},
|
|
|
|
{Token::Mul, " * "},
|
|
|
|
{Token::Div, " / "},
|
|
|
|
{Token::Mod, " mod "},
|
|
|
|
{Token::Equal, " = "},
|
|
|
|
{Token::NotEqual, " <> "},
|
|
|
|
{Token::LessThan, " < "},
|
|
|
|
{Token::GreaterThan, " > "},
|
|
|
|
{Token::LessThanOrEqual, " <= "},
|
|
|
|
{Token::GreaterThanOrEqual, " >= "}
|
|
|
|
});
|
|
|
|
if (!optrans.count(c_op))
|
2016-08-20 01:44:26 +00:00
|
|
|
{
|
2015-10-21 14:43:31 +00:00
|
|
|
error(_binaryOperation, "Operator not supported.");
|
2016-08-20 01:44:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
|
|
|
|
add("(");
|
|
|
|
leftExpression.accept(*this);
|
|
|
|
add(optrans.at(c_op));
|
|
|
|
rightExpression.accept(*this);
|
|
|
|
add(")");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(FunctionCall const& _node)
|
|
|
|
{
|
|
|
|
if (_node.annotation().isTypeConversion || _node.annotation().isStructConstructorCall)
|
|
|
|
{
|
|
|
|
error(_node, "Only ordinary function calls supported.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
FunctionType const& function = dynamic_cast<FunctionType const&>(*_node.expression().annotation().type);
|
2015-11-22 23:57:58 +00:00
|
|
|
switch (function.location())
|
2015-10-21 14:43:31 +00:00
|
|
|
{
|
2015-11-22 23:57:58 +00:00
|
|
|
case FunctionType::Location::AddMod:
|
|
|
|
case FunctionType::Location::MulMod:
|
2015-10-21 14:43:31 +00:00
|
|
|
{
|
2015-11-22 23:57:58 +00:00
|
|
|
//@todo require that third parameter is not zero
|
|
|
|
add("(of_int (mod (Int.(");
|
|
|
|
add(function.location() == FunctionType::Location::AddMod ? "+" : "*");
|
|
|
|
add(") (to_int ");
|
|
|
|
_node.arguments().at(0)->accept(*this);
|
|
|
|
add(") (to_int ");
|
|
|
|
_node.arguments().at(1)->accept(*this);
|
|
|
|
add(")) (to_int ");
|
|
|
|
_node.arguments().at(2)->accept(*this);
|
|
|
|
add(")))");
|
|
|
|
return false;
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
2015-11-22 23:57:58 +00:00
|
|
|
case FunctionType::Location::Internal:
|
|
|
|
{
|
|
|
|
if (!_node.names().empty())
|
|
|
|
{
|
|
|
|
error(_node, "Function calls with named arguments not supported.");
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
|
2015-11-22 23:57:58 +00:00
|
|
|
//@TODO check type conversions
|
2015-10-21 14:43:31 +00:00
|
|
|
|
2015-11-22 23:57:58 +00:00
|
|
|
add("(");
|
|
|
|
_node.expression().accept(*this);
|
|
|
|
add(" state");
|
|
|
|
for (auto const& arg: _node.arguments())
|
|
|
|
{
|
|
|
|
add(" ");
|
|
|
|
arg->accept(*this);
|
|
|
|
}
|
|
|
|
add(")");
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-13 09:16:00 +00:00
|
|
|
case FunctionType::Location::Bare:
|
|
|
|
{
|
|
|
|
if (!_node.arguments().empty())
|
|
|
|
{
|
|
|
|
error(_node, "Function calls with named arguments not supported.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
add("(");
|
|
|
|
indent();
|
|
|
|
add("let amount = 0 in ");
|
|
|
|
_node.expression().accept(*this);
|
|
|
|
addLine("if amount <= this.balance then");
|
|
|
|
indent();
|
|
|
|
addLine("let balance_precall = this.balance in");
|
|
|
|
addLine("begin");
|
|
|
|
indent();
|
|
|
|
addLine("this.balance <- this.balance - amount;");
|
|
|
|
addLine("if not (external_call this) then begin this.balance = balance_precall; false end else true");
|
|
|
|
unindent();
|
|
|
|
addLine("end");
|
|
|
|
unindent();
|
|
|
|
addLine("else false");
|
|
|
|
|
|
|
|
unindent();
|
|
|
|
add(")");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case FunctionType::Location::SetValue:
|
|
|
|
{
|
|
|
|
add("let amount = ");
|
|
|
|
solAssert(_node.arguments().size() == 1, "");
|
|
|
|
_node.arguments()[0]->accept(*this);
|
|
|
|
add(" in ");
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-22 23:57:58 +00:00
|
|
|
default:
|
|
|
|
error(_node, "Only internal function calls supported.");
|
|
|
|
return true;
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(MemberAccess const& _node)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
_node.expression().annotation().type->category() == Type::Category::Array &&
|
|
|
|
_node.memberName() == "length" &&
|
|
|
|
!_node.annotation().lValueRequested
|
|
|
|
)
|
|
|
|
{
|
|
|
|
add("(of_int ");
|
|
|
|
_node.expression().accept(*this);
|
|
|
|
add(".length");
|
|
|
|
add(")");
|
|
|
|
}
|
2016-07-13 09:16:00 +00:00
|
|
|
else if (
|
|
|
|
_node.memberName() == "call" &&
|
|
|
|
*_node.expression().annotation().type == IntegerType(160, IntegerType::Modifier::Address)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Do nothing, do not even visit the address because this will be an external call
|
|
|
|
//@TODO ensure that the expression itself does not have side-effects
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
else
|
2016-07-13 09:16:00 +00:00
|
|
|
error(_node, "Member access: Only call and array length supported.");
|
2015-10-21 14:43:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(IndexAccess const& _node)
|
|
|
|
{
|
|
|
|
auto baseType = dynamic_cast<ArrayType const*>(_node.baseExpression().annotation().type.get());
|
|
|
|
if (!baseType)
|
|
|
|
{
|
|
|
|
error(_node, "Index access only supported for arrays.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (_node.annotation().lValueRequested)
|
|
|
|
{
|
|
|
|
error(_node, "Assignment to array elements not supported.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
add("(");
|
|
|
|
_node.baseExpression().accept(*this);
|
|
|
|
add("[to_int ");
|
|
|
|
_node.indexExpression()->accept(*this);
|
|
|
|
add("]");
|
|
|
|
add(")");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
Declaration const* declaration = _identifier.annotation().referencedDeclaration;
|
|
|
|
if (FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(declaration))
|
|
|
|
add("_" + functionDef->name());
|
|
|
|
else if (auto variable = dynamic_cast<VariableDeclaration const*>(declaration))
|
|
|
|
{
|
2015-11-16 19:35:54 +00:00
|
|
|
bool isStateVar = isStateVariable(variable);
|
|
|
|
bool lvalue = _identifier.annotation().lValueRequested;
|
|
|
|
if (isStateVar)
|
2016-07-13 09:16:00 +00:00
|
|
|
add("this.storage.");
|
|
|
|
else if (!lvalue)
|
|
|
|
add("!(");
|
2015-11-16 19:35:54 +00:00
|
|
|
add("_" + variable->name());
|
2016-07-13 09:16:00 +00:00
|
|
|
if (!isStateVar && !lvalue)
|
2015-11-16 19:35:54 +00:00
|
|
|
add(")");
|
2016-07-13 09:16:00 +00:00
|
|
|
m_currentLValueIsRef = !isStateVar;
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
error(_identifier, "Not supported.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::visit(Literal const& _literal)
|
|
|
|
{
|
|
|
|
TypePointer type = _literal.annotation().type;
|
|
|
|
switch (type->category())
|
|
|
|
{
|
|
|
|
case Type::Category::Bool:
|
|
|
|
if (type->literalValue(&_literal) == 0)
|
|
|
|
add("false");
|
|
|
|
else
|
|
|
|
add("true");
|
|
|
|
break;
|
2016-03-29 20:08:51 +00:00
|
|
|
case Type::Category::RationalNumber:
|
2016-02-18 22:39:11 +00:00
|
|
|
{
|
2016-03-29 20:08:51 +00:00
|
|
|
auto const& constantNumber = dynamic_cast<RationalNumberType const&>(*type);
|
2016-05-10 12:30:24 +00:00
|
|
|
if (constantNumber.isFractional())
|
2016-02-18 22:39:11 +00:00
|
|
|
error(_literal, "Fractional numbers not supported.");
|
2016-08-20 01:44:26 +00:00
|
|
|
else
|
|
|
|
add("(of_int " + toString(type->literalValue(&_literal)) + ")");
|
2015-10-21 14:43:31 +00:00
|
|
|
break;
|
2016-09-07 11:54:57 +00:00
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
default:
|
|
|
|
error(_literal, "Not supported.");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-16 19:35:54 +00:00
|
|
|
bool Why3Translator::isStateVariable(VariableDeclaration const* _var) const
|
|
|
|
{
|
2016-07-13 09:16:00 +00:00
|
|
|
return contains(m_currentContract.stateVariables, _var);
|
2015-11-16 19:35:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-22 23:26:04 +00:00
|
|
|
bool Why3Translator::isStateVariable(string const& _name) const
|
|
|
|
{
|
2016-07-13 09:16:00 +00:00
|
|
|
for (auto const& var: m_currentContract.stateVariables)
|
2015-11-22 23:26:04 +00:00
|
|
|
if (var->name() == _name)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::isLocalVariable(VariableDeclaration const* _var) const
|
|
|
|
{
|
|
|
|
for (auto const& var: m_localVariables)
|
|
|
|
if (var.second == _var)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Why3Translator::isLocalVariable(string const& _name) const
|
|
|
|
{
|
|
|
|
return m_localVariables.count(_name);
|
|
|
|
}
|
|
|
|
|
2016-07-13 09:16:00 +00:00
|
|
|
string Why3Translator::copyOfStorage() const
|
|
|
|
{
|
|
|
|
if (m_currentContract.stateVariables.empty())
|
|
|
|
return "()";
|
|
|
|
string ret = "{";
|
|
|
|
bool first = true;
|
|
|
|
for (auto const* variable: m_currentContract.stateVariables)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
ret += "; ";
|
|
|
|
ret += "_" + variable->name() + " = this.storage._" + variable->name();
|
|
|
|
}
|
|
|
|
return ret + "}";
|
|
|
|
}
|
|
|
|
|
2015-11-11 14:20:53 +00:00
|
|
|
void Why3Translator::visitIndentedUnlessBlock(Statement const& _statement)
|
|
|
|
{
|
|
|
|
bool isBlock = !!dynamic_cast<Block const*>(&_statement);
|
|
|
|
if (isBlock)
|
|
|
|
newLine();
|
|
|
|
else
|
|
|
|
indent();
|
|
|
|
_statement.accept(*this);
|
|
|
|
if (isBlock)
|
|
|
|
newLine();
|
|
|
|
else
|
|
|
|
unindent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Why3Translator::addSourceFromDocStrings(DocumentedAnnotation const& _annotation)
|
2015-10-21 14:43:31 +00:00
|
|
|
{
|
|
|
|
auto why3Range = _annotation.docTags.equal_range("why3");
|
|
|
|
for (auto i = why3Range.first; i != why3Range.second; ++i)
|
2015-11-22 23:26:04 +00:00
|
|
|
addLine(transformVariableReferences(i->second.content));
|
2015-10-21 14:43:31 +00:00
|
|
|
}
|
2015-11-22 23:26:04 +00:00
|
|
|
|
|
|
|
string Why3Translator::transformVariableReferences(string const& _annotation)
|
|
|
|
{
|
|
|
|
string ret;
|
|
|
|
auto pos = _annotation.begin();
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
auto hash = find(pos, _annotation.end(), '#');
|
|
|
|
ret.append(pos, hash);
|
|
|
|
if (hash == _annotation.end())
|
|
|
|
break;
|
|
|
|
|
|
|
|
auto hashEnd = find_if(hash + 1, _annotation.end(), [](char _c)
|
|
|
|
{
|
2015-11-23 15:30:51 +00:00
|
|
|
return
|
|
|
|
(_c != '_' && _c != '$') &&
|
|
|
|
!('a' <= _c && _c <= 'z') &&
|
|
|
|
!('A' <= _c && _c <= 'Z') &&
|
|
|
|
!('0' <= _c && _c <= '9');
|
2015-11-22 23:26:04 +00:00
|
|
|
});
|
|
|
|
string varName(hash + 1, hashEnd);
|
|
|
|
if (isLocalVariable(varName))
|
2016-07-13 09:16:00 +00:00
|
|
|
ret += "(!_" + varName + ")";
|
2015-11-22 23:26:04 +00:00
|
|
|
else if (isStateVariable(varName))
|
2016-07-13 09:16:00 +00:00
|
|
|
ret += "(this.storage._" + varName + ")";
|
|
|
|
//@todo return variables
|
2015-11-22 23:26:04 +00:00
|
|
|
else
|
|
|
|
ret.append(hash, hashEnd);
|
|
|
|
|
|
|
|
pos = hashEnd;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-13 09:16:00 +00:00
|
|
|
void Why3Translator::appendPreface()
|
|
|
|
{
|
|
|
|
m_lines.push_back(Line{R"(
|
|
|
|
module UInt256
|
|
|
|
use import mach.int.Unsigned
|
|
|
|
type uint256
|
|
|
|
constant max_uint256: int = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
|
|
|
|
clone export mach.int.Unsigned with
|
|
|
|
type t = uint256,
|
|
|
|
constant max = max_uint256
|
|
|
|
end
|
2016-09-07 16:30:53 +00:00
|
|
|
|
|
|
|
module Address
|
|
|
|
use import mach.int.Unsigned
|
|
|
|
type address
|
|
|
|
constant max_address: int = 0xffffffffffffffffffffffffffffffffffffffff (* 160 bit = 40 f's *)
|
|
|
|
clone export mach.int.Unsigned with
|
|
|
|
type t = address,
|
|
|
|
constant max = max_address
|
|
|
|
end
|
2016-07-13 09:16:00 +00:00
|
|
|
)", 0});
|
|
|
|
}
|