/*
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 .
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Yul code and data object container.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::util;
using namespace solidity::yul;
std::string Data::toString(DebugInfoSelection const&, CharStreamProvider const*) const
{
return "data \"" + name + "\" hex\"" + util::toHex(data) + "\"";
}
std::string Object::toString(
DebugInfoSelection const& _debugInfoSelection,
CharStreamProvider const* _soliditySourceProvider
) const
{
yulAssert(hasCode(), "No code");
yulAssert(dialect(), "No dialect");
yulAssert(debugData, "No debug data");
std::string inner = "code " + AsmPrinter::format(
*code(),
debugData->sourceNames,
_debugInfoSelection,
_soliditySourceProvider
);
for (auto const& obj: subObjects)
inner += "\n" + obj->toString(_debugInfoSelection, _soliditySourceProvider);
return
debugData->formatUseSrcComment() +
"object \"" + name + "\" {\n" +
indent(inner) + "\n" +
"}";
}
Json Data::toJson() const
{
Json ret;
ret["nodeType"] = "YulData";
ret["value"] = util::toHex(data);
return ret;
}
std::string ObjectDebugData::formatUseSrcComment() const
{
if (!sourceNames)
return "";
auto formatIdNamePair = [](auto&& _pair) {
return std::to_string(_pair.first) + ":" + util::escapeAndQuoteString(*_pair.second);
};
std::string serializedSourceNames = joinHumanReadable(
ranges::views::transform(*sourceNames, formatIdNamePair)
);
return "/// @use-src " + serializedSourceNames + "\n";
}
Json Object::toJson() const
{
yulAssert(hasCode(), "No code");
yulAssert(dialect(), "No dialect");
Json codeJson;
codeJson["nodeType"] = "YulCode";
codeJson["block"] = AsmJsonConverter(*dialect(), 0 /* sourceIndex */)(code()->root());
Json subObjectsJson = Json::array();
for (std::shared_ptr const& subObject: subObjects)
subObjectsJson.emplace_back(subObject->toJson());
Json ret;
ret["nodeType"] = "YulObject";
ret["name"] = name;
ret["code"] = codeJson;
ret["subObjects"] = subObjectsJson;
return ret;
}
std::set Object::Structure::topLevelSubObjectNames() const
{
std::set topLevelObjectNames;
for (auto const& path: objectPaths)
if (!util::contains(path, '.') && path != objectName)
topLevelObjectNames.insert(path);
return topLevelObjectNames;
}
Object::Structure Object::summarizeStructure() const
{
Structure structure;
structure.objectPaths =
name.empty() || util::contains(name, '.') ?
std::set{} :
std::set{name};
structure.objectName = name;
for (std::shared_ptr const& subObjectNode: subObjects)
{
yulAssert(!structure.contains(subObjectNode->name));
if (util::contains(subObjectNode->name, '.'))
continue;
if (auto const* subObject = dynamic_cast