2019-10-15 10:38:12 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-10-15 10:38:12 +00:00
|
|
|
/**
|
|
|
|
* @date 2019
|
|
|
|
* Converts inline assembly AST to JSON format
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/AsmJsonConverter.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-05-20 22:54:42 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2021-05-03 10:03:41 +00:00
|
|
|
#include <libsolutil/UTF8.h>
|
2019-10-15 10:38:12 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::yul
|
2019-10-15 10:38:12 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Block const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulBlock");
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["statements"] = vectorOfVariantsToJson(_node.statements);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(TypedName const& _node) const
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(!_node.name.empty(), "Invalid variable name.");
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulTypedName");
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["name"] = _node.name.str();
|
|
|
|
ret["type"] = _node.type.str();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Literal const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulLiteral");
|
2019-10-15 10:38:12 +00:00
|
|
|
switch (_node.kind)
|
|
|
|
{
|
|
|
|
case LiteralKind::Number:
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(
|
2019-12-11 16:31:36 +00:00
|
|
|
util::isValidDecimal(_node.value.str()) || util::isValidHex(_node.value.str()),
|
2019-10-15 10:38:12 +00:00
|
|
|
"Invalid number literal"
|
|
|
|
);
|
|
|
|
ret["kind"] = "number";
|
|
|
|
break;
|
|
|
|
case LiteralKind::Boolean:
|
|
|
|
ret["kind"] = "bool";
|
|
|
|
break;
|
|
|
|
case LiteralKind::String:
|
|
|
|
ret["kind"] = "string";
|
2021-05-03 10:03:41 +00:00
|
|
|
ret["hexValue"] = util::toHex(util::asBytes(_node.value.str()));
|
2019-10-15 10:38:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret["type"] = _node.type.str();
|
2021-05-03 10:03:41 +00:00
|
|
|
if (util::validateUTF8(_node.value.str()))
|
|
|
|
ret["value"] = _node.value.str();
|
2019-10-15 10:38:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Identifier const& _node) const
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(!_node.name.empty(), "Invalid identifier");
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulIdentifier");
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["name"] = _node.name.str();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Assignment const& _node) const
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(_node.variableNames.size() >= 1, "Invalid assignment syntax");
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulAssignment");
|
2019-10-15 10:38:12 +00:00
|
|
|
for (auto const& var: _node.variableNames)
|
|
|
|
ret["variableNames"].append((*this)(var));
|
2019-11-20 12:09:29 +00:00
|
|
|
ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json::nullValue;
|
2019-10-15 10:38:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(FunctionCall const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulFunctionCall");
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["functionName"] = (*this)(_node.functionName);
|
|
|
|
ret["arguments"] = vectorOfVariantsToJson(_node.arguments);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(ExpressionStatement const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulExpressionStatement");
|
2019-11-20 12:09:29 +00:00
|
|
|
ret["expression"] = std::visit(*this, _node.expression);
|
2019-10-15 10:38:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(VariableDeclaration const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulVariableDeclaration");
|
2019-10-15 10:38:12 +00:00
|
|
|
for (auto const& var: _node.variables)
|
|
|
|
ret["variables"].append((*this)(var));
|
|
|
|
|
2019-11-20 12:09:29 +00:00
|
|
|
ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json::nullValue;
|
2019-10-15 10:38:12 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(FunctionDefinition const& _node) const
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(!_node.name.empty(), "Invalid function name.");
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulFunctionDefinition");
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["name"] = _node.name.str();
|
|
|
|
for (auto const& var: _node.parameters)
|
|
|
|
ret["parameters"].append((*this)(var));
|
|
|
|
for (auto const& var: _node.returnVariables)
|
|
|
|
ret["returnVariables"].append((*this)(var));
|
|
|
|
ret["body"] = (*this)(_node.body);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(If const& _node) const
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(_node.condition, "Invalid if condition.");
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulIf");
|
2019-11-20 12:09:29 +00:00
|
|
|
ret["condition"] = std::visit(*this, *_node.condition);
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["body"] = (*this)(_node.body);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Switch const& _node) const
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(_node.expression, "Invalid expression pointer.");
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulSwitch");
|
2019-11-20 12:09:29 +00:00
|
|
|
ret["expression"] = std::visit(*this, *_node.expression);
|
2019-10-15 10:38:12 +00:00
|
|
|
for (auto const& var: _node.cases)
|
|
|
|
ret["cases"].append((*this)(var));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Case const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulCase");
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["value"] = _node.value ? (*this)(*_node.value) : "default";
|
|
|
|
ret["body"] = (*this)(_node.body);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(ForLoop const& _node) const
|
|
|
|
{
|
2020-05-20 22:54:42 +00:00
|
|
|
yulAssert(_node.condition, "Invalid for loop condition.");
|
2023-05-02 13:34:12 +00:00
|
|
|
Json::Value ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulForLoop");
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["pre"] = (*this)(_node.pre);
|
2019-11-20 12:09:29 +00:00
|
|
|
ret["condition"] = std::visit(*this, *_node.condition);
|
2019-10-15 10:38:12 +00:00
|
|
|
ret["post"] = (*this)(_node.post);
|
|
|
|
ret["body"] = (*this)(_node.body);
|
|
|
|
return ret;
|
2020-05-20 22:54:42 +00:00
|
|
|
}
|
2019-10-15 10:38:12 +00:00
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Break const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
return createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulBreak");
|
2019-10-15 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Continue const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
return createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulContinue");
|
2019-10-15 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value AsmJsonConverter::operator()(Leave const& _node) const
|
|
|
|
{
|
2023-05-02 13:34:12 +00:00
|
|
|
return createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulLeave");
|
2019-10-15 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 08:22:05 +00:00
|
|
|
Json::Value AsmJsonConverter::createAstNode(langutil::SourceLocation const& _originLocation, langutil::SourceLocation const& _nativeLocation, std::string _nodeType) const
|
2019-10-15 10:38:12 +00:00
|
|
|
{
|
|
|
|
Json::Value ret{Json::objectValue};
|
|
|
|
ret["nodeType"] = std::move(_nodeType);
|
2023-08-28 08:22:05 +00:00
|
|
|
auto srcLocation = [&](int start, int end) -> std::string
|
2023-05-02 13:34:12 +00:00
|
|
|
{
|
|
|
|
int length = (start >= 0 && end >= 0 && end >= start) ? end - start : -1;
|
2023-08-28 08:22:05 +00:00
|
|
|
return std::to_string(start) + ":" + std::to_string(length) + ":" + (m_sourceIndex.has_value() ? std::to_string(m_sourceIndex.value()) : "-1");
|
2023-05-02 13:34:12 +00:00
|
|
|
};
|
|
|
|
ret["src"] = srcLocation(_originLocation.start, _originLocation.end);
|
|
|
|
ret["nativeSrc"] = srcLocation(_nativeLocation.start, _nativeLocation.end);
|
2019-10-15 10:38:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2023-08-28 08:22:05 +00:00
|
|
|
Json::Value AsmJsonConverter::vectorOfVariantsToJson(std::vector<T> const& _vec) const
|
2019-10-15 10:38:12 +00:00
|
|
|
{
|
|
|
|
Json::Value ret{Json::arrayValue};
|
|
|
|
for (auto const& var: _vec)
|
2019-11-20 12:09:29 +00:00
|
|
|
ret.append(std::visit(*this, var));
|
2019-10-15 10:38:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|