Add structs to library interface.

This commit is contained in:
chriseth 2015-10-06 13:01:55 +02:00
parent bf5b387954
commit 44e42bf52e
2 changed files with 31 additions and 7 deletions

View File

@ -1,5 +1,8 @@
#include <libsolidity/InterfaceHandler.h>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/irange.hpp>
#include <boost/algorithm/string/join.hpp>
#include <libsolidity/AST.h>
#include <libsolidity/CompilerStack.h>
using namespace std;
@ -108,16 +111,36 @@ string InterfaceHandler::abiInterface(ContractDefinition const& _contractDef)
string InterfaceHandler::ABISolidityInterface(ContractDefinition const& _contractDef)
{
string ret = "contract " + _contractDef.name() + "{";
using namespace boost::adaptors;
using namespace boost::algorithm;
string ret = (_contractDef.isLibrary() ? "library " : "contract ") + _contractDef.name() + "{";
auto populateParameters = [](vector<string> const& _paramNames, vector<string> const& _paramTypes)
{
string r = "";
solAssert(_paramNames.size() == _paramTypes.size(), "Names and types vector size does not match");
for (unsigned i = 0; i < _paramNames.size(); ++i)
r += (r.size() ? "," : "(") + _paramTypes[i] + " " + _paramNames[i];
return r.size() ? r + ")" : "()";
return "(" + join(boost::irange<size_t>(0, _paramNames.size()) | transformed([&](size_t _i) {
return _paramTypes[_i] + " " + _paramNames[_i];
}), ",") + ")";
};
// If this is a library, include all its enum and struct types. Should be more intelligent
// in the future and check what is actually used (it might even use types from other libraries
// or contracts or in the global scope).
if (_contractDef.isLibrary())
{
for (auto const& stru: _contractDef.definedStructs())
{
ret += "struct " + stru->name() + "{";
for (ASTPointer<VariableDeclaration> const& _member: stru->members())
ret += _member->type(nullptr)->canonicalName(false) + " " + _member->name() + ";";
ret += "}";
}
for (auto const& enu: _contractDef.definedEnums())
{
ret += "enum " + enu->name() + "{" +
join(enu->members() | transformed([](ASTPointer<EnumValue> const& _value) {
return _value->name();
}), ",") + "}";
}
}
if (_contractDef.constructor())
{
auto externalFunction = FunctionType(*_contractDef.constructor()).interfaceFunctionType();

View File

@ -152,7 +152,8 @@ BOOST_AUTO_TEST_CASE(libraries)
}
)";
ContractDefinition const& contract = checkInterface(sourceCode);
set<string> expectedFunctions({"function f(uint256[] x,Lib.Str y,Lib.E z);"});
BOOST_CHECK(contract.isLibrary());
set<string> expectedFunctions({"function f(uint256[] x,Lib.Str storage y,Lib.E z);"});
BOOST_REQUIRE_EQUAL(1, contract.definedFunctions().size());
BOOST_CHECK(expectedFunctions == set<string>({sourcePart(*contract.definedFunctions().at(0))}));
}