diff options
author | Lefteris Karapetsas <lefteris@refu.co> | 2014-12-03 23:40:37 +0800 |
---|---|---|
committer | Lefteris Karapetsas <lefteris@refu.co> | 2014-12-03 23:40:37 +0800 |
commit | d25581de7cc89470a060625c043dba8cf9ae293f (patch) | |
tree | fd5aae1d54b8d07ebd208acb0eb4c4d6e8c8c499 /InterfaceHandler.cpp | |
parent | be81981ec4a3a9e7704b230096d9a580175d759e (diff) | |
download | dexon-solidity-d25581de7cc89470a060625c043dba8cf9ae293f.tar.gz dexon-solidity-d25581de7cc89470a060625c043dba8cf9ae293f.tar.zst dexon-solidity-d25581de7cc89470a060625c043dba8cf9ae293f.zip |
Moving all Interface and Documentation functionality to own class
- Creating the Interface Handler class which will take care of the
parsing of Natspec comments and of interfacing with and outputing to
JSON files.
- Will also handle the ABI interface creation
Diffstat (limited to 'InterfaceHandler.cpp')
-rw-r--r-- | InterfaceHandler.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/InterfaceHandler.cpp b/InterfaceHandler.cpp new file mode 100644 index 00000000..c0e07280 --- /dev/null +++ b/InterfaceHandler.cpp @@ -0,0 +1,88 @@ +#include <libsolidity/InterfaceHandler.h> +#include <libsolidity/AST.h> +#include <libsolidity/CompilerStack.h> + +namespace dev { +namespace solidity { + +InterfaceHandler::InterfaceHandler() +{ +} + +std::unique_ptr<std::string> InterfaceHandler::getDocumentation(std::shared_ptr<ContractDefinition> _contractDef, + enum documentation_type _type) +{ + switch(_type) + { + case NATSPEC_USER: + return getUserDocumentation(_contractDef); + case NATSPEC_DEV: + return getDevDocumentation(_contractDef); + case ABI_INTERFACE: + return getABIInterface(_contractDef); + } + + BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Internal error")); + return nullptr; +} + +std::unique_ptr<std::string> InterfaceHandler::getABIInterface(std::shared_ptr<ContractDefinition> _contractDef) +{ + Json::Value methods(Json::arrayValue); + + std::vector<FunctionDefinition const*> exportedFunctions = _contractDef->getInterfaceFunctions(); + for (FunctionDefinition const* f: exportedFunctions) + { + Json::Value method; + Json::Value inputs(Json::arrayValue); + Json::Value outputs(Json::arrayValue); + + auto streamVariables = [](std::vector<ASTPointer<VariableDeclaration>> const& _vars) + { + Json::Value params(Json::arrayValue); + for (ASTPointer<VariableDeclaration> const& var: _vars) + { + Json::Value input; + input["name"] = var->getName(); + input["type"] = var->getType()->toString(); + params.append(input); + } + return params; + }; + + method["name"] = f->getName(); + method["inputs"] = streamVariables(f->getParameters()); + method["outputs"] = streamVariables(f->getReturnParameters()); + methods.append(method); + } + return std::unique_ptr<std::string>(new std::string(m_writer.write(methods))); +} + +std::unique_ptr<std::string> InterfaceHandler::getUserDocumentation(std::shared_ptr<ContractDefinition> _contractDef) +{ + Json::Value doc; + Json::Value methods(Json::objectValue); + + for (FunctionDefinition const* f: _contractDef->getInterfaceFunctions()) + { + Json::Value user; + auto strPtr = f->getDocumentation(); + if (strPtr) + { + user["notice"] = Json::Value(*strPtr); + methods[f->getName()] = user; + } + } + doc["methods"] = methods; + + return std::unique_ptr<std::string>(new std::string(m_writer.write(doc))); +} + +std::unique_ptr<std::string> InterfaceHandler::getDevDocumentation(std::shared_ptr<ContractDefinition> _contractDef) +{ + //TODO + return nullptr; +} + +} //solidity NS +} // dev NS |