From a9c6ff4ac8c271aaada1965894b34933b662c044 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 26 Jan 2017 18:20:54 +0100 Subject: [PATCH] Refactor json return type generation. --- libsolidity/ast/Types.cpp | 18 ------- libsolidity/ast/Types.h | 2 - libsolidity/interface/InterfaceHandler.cpp | 48 +++++++++++-------- libsolidity/interface/InterfaceHandler.h | 10 ++++ .../SolidityNameAndTypeResolution.cpp | 22 ++++----- 5 files changed, 49 insertions(+), 51 deletions(-) diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 971e1f184..dbabc8db5 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -2512,24 +2512,6 @@ FunctionTypePointer FunctionType::asMemberFunction(bool _inLibrary, bool _bound) ); } -vector const FunctionType::parameterTypeNames(bool _addDataLocation) const -{ - vector names; - for (TypePointer const& t: parameterTypes()) - names.push_back(t->canonicalName(_addDataLocation)); - - return names; -} - -vector const FunctionType::returnParameterTypeNames(bool _addDataLocation) const -{ - vector names; - for (TypePointer const& t: m_returnParameterTypes) - names.push_back(t->canonicalName(_addDataLocation)); - - return names; -} - TypePointer const& FunctionType::selfType() const { solAssert(bound(), "Function is not bound."); diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 770cbb300..a5147f176 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -915,10 +915,8 @@ public: TypePointers parameterTypes() const; std::vector parameterNames() const; - std::vector const parameterTypeNames(bool _addDataLocation) const; TypePointers const& returnParameterTypes() const { return m_returnParameterTypes; } std::vector const& returnParameterNames() const { return m_returnParameterNames; } - std::vector const returnParameterTypeNames(bool _addDataLocation) const; /// @returns the "self" parameter type for a bound function TypePointer const& selfType() const; diff --git a/libsolidity/interface/InterfaceHandler.cpp b/libsolidity/interface/InterfaceHandler.cpp index 9944bb22d..6c1bb0c4d 100644 --- a/libsolidity/interface/InterfaceHandler.cpp +++ b/libsolidity/interface/InterfaceHandler.cpp @@ -30,20 +30,6 @@ Json::Value InterfaceHandler::abiInterface(ContractDefinition const& _contractDe { Json::Value abi(Json::arrayValue); - auto populateParameters = [](vector const& _paramNames, vector const& _paramTypes) - { - Json::Value params(Json::arrayValue); - solAssert(_paramNames.size() == _paramTypes.size(), "Names and types vector size does not match"); - for (unsigned i = 0; i < _paramNames.size(); ++i) - { - Json::Value param; - param["name"] = _paramNames[i]; - param["type"] = _paramTypes[i]; - params.append(param); - } - return params; - }; - for (auto it: _contractDef.interfaceFunctions()) { auto externalFunctionType = it.second->interfaceFunctionType(); @@ -52,13 +38,15 @@ Json::Value InterfaceHandler::abiInterface(ContractDefinition const& _contractDe method["name"] = it.second->declaration().name(); method["constant"] = it.second->isConstant(); method["payable"] = it.second->isPayable(); - method["inputs"] = populateParameters( + method["inputs"] = formatTypeList( externalFunctionType->parameterNames(), - externalFunctionType->parameterTypeNames(_contractDef.isLibrary()) + externalFunctionType->parameterTypes(), + _contractDef.isLibrary() ); - method["outputs"] = populateParameters( + method["outputs"] = formatTypeList( externalFunctionType->returnParameterNames(), - externalFunctionType->returnParameterTypeNames(_contractDef.isLibrary()) + externalFunctionType->returnParameterTypes(), + _contractDef.isLibrary() ); abi.append(method); } @@ -69,9 +57,10 @@ Json::Value InterfaceHandler::abiInterface(ContractDefinition const& _contractDe auto externalFunction = FunctionType(*_contractDef.constructor(), false).interfaceFunctionType(); solAssert(!!externalFunction, ""); method["payable"] = externalFunction->isPayable(); - method["inputs"] = populateParameters( + method["inputs"] = formatTypeList( externalFunction->parameterNames(), - externalFunction->parameterTypeNames(_contractDef.isLibrary()) + externalFunction->parameterTypes(), + _contractDef.isLibrary() ); abi.append(method); } @@ -179,6 +168,25 @@ Json::Value InterfaceHandler::devDocumentation(ContractDefinition const& _contra return doc; } +Json::Value InterfaceHandler::formatTypeList( + vector const& _names, + vector const& _types, + bool _forLibrary +) +{ + Json::Value params(Json::arrayValue); + solAssert(_names.size() == _types.size(), "Names and types vector size does not match"); + for (unsigned i = 0; i < _names.size(); ++i) + { + solAssert(_types[i], ""); + Json::Value param; + param["name"] = _names[i]; + param["type"] = _types[i]->canonicalName(_forLibrary); + params.append(param); + } + return params; +} + string InterfaceHandler::extractDoc(multimap const& _tags, string const& _name) { string value; diff --git a/libsolidity/interface/InterfaceHandler.h b/libsolidity/interface/InterfaceHandler.h index b7e1bb003..56927d44e 100644 --- a/libsolidity/interface/InterfaceHandler.h +++ b/libsolidity/interface/InterfaceHandler.h @@ -37,6 +37,8 @@ namespace solidity // Forward declarations class ContractDefinition; +class Type; +using TypePointer = std::shared_ptr; struct DocTag; enum class DocumentationType: uint8_t; @@ -84,6 +86,14 @@ public: static Json::Value devDocumentation(ContractDefinition const& _contractDef); private: + /// @returns a json value suitable for a list of types in function input or output + /// parameters or other places. If @a _forLibrary is true, complex types are referenced + /// by name, otherwise they are anonymously expanded. + static Json::Value formatTypeList( + std::vector const& _names, + std::vector const& _types, + bool _forLibrary + ); /// @returns concatenation of all content under the given tag name. static std::string extractDoc(std::multimap const& _tags, std::string const& _name); }; diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index b6067ea5c..472067ecd 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -1102,25 +1102,25 @@ BOOST_AUTO_TEST_CASE(state_variable_accessors) BOOST_REQUIRE((contract = retrieveContract(source, 0)) != nullptr); FunctionTypePointer function = retrieveFunctionBySignature(*contract, "foo()"); BOOST_REQUIRE(function && function->hasDeclaration()); - auto returnParams = function->returnParameterTypeNames(false); - BOOST_CHECK_EQUAL(returnParams.at(0), "uint256"); + auto returnParams = function->returnParameterTypes(); + BOOST_CHECK_EQUAL(returnParams.at(0)->canonicalName(false), "uint256"); BOOST_CHECK(function->isConstant()); function = retrieveFunctionBySignature(*contract, "map(uint256)"); BOOST_REQUIRE(function && function->hasDeclaration()); - auto params = function->parameterTypeNames(false); - BOOST_CHECK_EQUAL(params.at(0), "uint256"); - returnParams = function->returnParameterTypeNames(false); - BOOST_CHECK_EQUAL(returnParams.at(0), "bytes4"); + auto params = function->parameterTypes(); + BOOST_CHECK_EQUAL(params.at(0)->canonicalName(false), "uint256"); + returnParams = function->returnParameterTypes(); + BOOST_CHECK_EQUAL(returnParams.at(0)->canonicalName(false), "bytes4"); BOOST_CHECK(function->isConstant()); function = retrieveFunctionBySignature(*contract, "multiple_map(uint256,uint256)"); BOOST_REQUIRE(function && function->hasDeclaration()); - params = function->parameterTypeNames(false); - BOOST_CHECK_EQUAL(params.at(0), "uint256"); - BOOST_CHECK_EQUAL(params.at(1), "uint256"); - returnParams = function->returnParameterTypeNames(false); - BOOST_CHECK_EQUAL(returnParams.at(0), "bytes4"); + params = function->parameterTypes(); + BOOST_CHECK_EQUAL(params.at(0)->canonicalName(false), "uint256"); + BOOST_CHECK_EQUAL(params.at(1)->canonicalName(false), "uint256"); + returnParams = function->returnParameterTypes(); + BOOST_CHECK_EQUAL(returnParams.at(0)->canonicalName(false), "bytes4"); BOOST_CHECK(function->isConstant()); }