mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add structs to library interface.
This commit is contained in:
parent
bf5b387954
commit
44e42bf52e
@ -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();
|
||||
|
@ -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))}));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user