Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libsolidity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ set(sources
interface/SMTSolverCommand.h
interface/StandardCompiler.cpp
interface/StandardCompiler.h
interface/StandardJSONInput.h
interface/StandardJSONInput.cpp
interface/StandardJSONOutput.h
interface/StandardJSONOutput.cpp
interface/StorageLayout.cpp
interface/StorageLayout.h
interface/UniversalCallback.h
Expand Down
131 changes: 131 additions & 0 deletions libsolidity/interface/StandardJSONInput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsolidity/interface/StandardJSONInput.h>

#include <liblangutil/Exceptions.h>
#include <libsolidity/interface/Common.h>

#include <range/v3/algorithm/transform.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/map.hpp>
#include <range/v3/view/transform.hpp>

#include <optional>
using namespace solidity::frontend;
using namespace solidity::frontend::json;
using namespace solidity::frontend::json::input;

void input::to_json(Json& _json, YulOptimizerDetails const& _details)
{
if (_details.stackAllocation)
_json["stackAllocation"] = *_details.stackAllocation;
if (_details.optimizerSteps)
_json["optimizerSteps"] = *_details.optimizerSteps;
}

void input::to_json(Json& _json, OptimizerDetails const& _details)
{
if (_details.peephole)
_json["peephole"] = *_details.peephole;
if (_details.orderLiterals)
_json["orderLiterals"] = *_details.orderLiterals;
if (_details.inliner)
_json["inliner"] = *_details.inliner;
if (_details.jumpdestRemover)
_json["jumpdestRemover"] = *_details.jumpdestRemover;
if (_details.deduplicate)
_json["deduplicate"] = *_details.deduplicate;
if (_details.cse)
_json["cse"] = *_details.cse;
if (_details.constantOptimizer)
_json["constantOptimizer"] = *_details.constantOptimizer;
if (_details.simpleCounterForLoopUncheckedIncrement)
_json["simpleCounterForLoopUncheckedIncrement"] = *_details.simpleCounterForLoopUncheckedIncrement;
if (_details.yul)
_json["yul"] = *_details.yul;
if (_details.yulDetails && _details.yul && *_details.yul)
_json["yulDetails"] = *_details.yulDetails;
}

void input::to_json(Json& _json, Optimizer const& _optimizer)
{
if (_optimizer.enable)
_json["enabled"] = *_optimizer.enable;
if (_optimizer.runs)
_json["runs"] = *_optimizer.runs;
if (_optimizer.details)
_json["details"] = *_optimizer.details;
}

void input::to_json(Json& _json, Debug const& _debug)
{
if (_debug.revertStrings)
_json["revertStrings"] = revertStringsToString(_debug.revertStrings.value());
}

void input::to_json(Json& _json, Metadata const& _metadata)
{
if (_metadata.appendCBOR)
{
_json["appendCBOR"] = *_metadata.appendCBOR;
if (*_metadata.appendCBOR && _metadata.bytecodeHash)
_json["bytecodeHash"] = metadataHashToString(_metadata.bytecodeHash.value());
}
}

void input::to_json(Json& _json, Settings const& _settings)
{
if (_settings.optimizer)
_json["optimizer"] = *_settings.optimizer;
if (_settings.evmVersion)
_json["evmVersion"] = (*_settings.evmVersion).name();
if (_settings.eofVersion)
_json["eofVersion"] = *_settings.eofVersion;
if (_settings.viaIR)
_json["viaIR"] = *_settings.viaIR;
if (_settings.viaSSACFG)
_json["viaSSACFG"] = *_settings.viaSSACFG;
if (_settings.debug)
_json["debug"] = *_settings.debug;
if (_settings.metadata)
_json["metadata"] = *_settings.metadata;
}

void json::to_json(Json& _json, StandardJSONInput const& _input)
{
_json["language"] = "Solidity";

for (auto& [name, source] : _input.sources)
_json["sources"][name]["content"] = source;

if (_input.settings)
{
_json["settings"] = *_input.settings;
for (auto& [name, source] : _input.sources)
{
_json["settings"]["libraries"][name] = _input.libraries | ranges::views::transform([](auto const& entry) {
auto const& [libraryName, address] = entry;
auto parts = libraryName | ranges::views::split(':') | ranges::to<std::vector<std::string>>();
return std::pair{parts.back(), "0x" + address.hex()};
}) | ranges::to<std::map>();
}
_json["settings"]["outputSelection"]["*"]["*"] = Json::array({"*"});
_json["settings"]["outputSelection"]["*"][""] = Json::array({"ast"});
}
}

158 changes: 158 additions & 0 deletions libsolidity/interface/StandardJSONInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0

#pragma once

#include <libsolutil/FixedHash.h>
#include <libsolutil/JSON.h>

#include <liblangutil/EVMVersion.h>
#include <liblangutil/Exceptions.h>

#include <libsolidity/interface/Common.h>
#include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/OptimiserSettings.h>

#include <range/v3/algorithm.hpp>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/map.hpp>
#include <range/v3/view/split.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/range/conversion.hpp>

#include <optional>

using namespace solidity;
using namespace solidity::util;

namespace solidity::frontend::json
{

namespace input
{
struct YulOptimizerDetails
{
/// Improve allocation of stack slots for variables, can free up stack slots early.
/// Default: true if Yul optimizer is enabled.
std::optional<bool> stackAllocation;
/// Optimization step sequence. The general form of the value is "<main sequence>:<cleanup sequence>".
/// If it does not contain the ':' delimiter, it is interpreted as the main
/// sequence and the default is used for the cleanup sequence.
/// Default: true.
std::optional<std::string> optimizerSteps;
};

struct OptimizerDetails
{
// Peephole optimizer (opcode-based). Default: true.
std::optional<bool> peephole = std::nullopt;
// Inliner (opcode-based). Optional. Default: true when optimization is enabled.
std::optional<bool> inliner = std::nullopt;
// Unused JUMPDEST remover (opcode-based). Default: true.
std::optional<bool> jumpdestRemover = std::nullopt;
// Literal reordering (codegen-based). Default: true when optimization is enabled.
std::optional<bool> orderLiterals = std::nullopt;
// Block deduplicator (opcode-based). Default: true when optimization is enabled.
std::optional<bool> deduplicate = std::nullopt;
// Common subexpression elimination (opcode-based). Default: true when optimization is enabled.
std::optional<bool> cse = std::nullopt;
// Constant optimizer (opcode-based). Default: true when optimization is enabled.
std::optional<bool> constantOptimizer = std::nullopt;
// Unchecked loop increment (codegen-based). Default: true.
std::optional<bool> simpleCounterForLoopUncheckedIncrement = std::nullopt;
// Yul optimizer. Default: true when optimization is enabled.
std::optional<bool> yul = std::nullopt;
/// Tuning options for the Yul optimizer.
std::optional<YulOptimizerDetails> yulDetails;
};

struct Optimizer
{
/// Turn on the optimizer. Default: false.
std::optional<bool> enable;
/// Optimize for how many times you intend to run the code. Default: 200.
std::optional<size_t> runs;
/// State of all optimizer components.
std::optional<OptimizerDetails> details;
};

struct Debug
{
/// How to treat revert (and require) reason strings. Default: `RevertStrings::Default`.
std::optional<RevertStrings> revertStrings = std::nullopt;
};

struct Metadata
{
/// The CBOR metadata is appended at the end of the bytecode by default. Default: true.
/// Setting this to false omits the metadata from the runtime and deploy time code
std::optional<bool> appendCBOR = std::nullopt;
/// Use the given hash method for the metadata hash that is appended to the bytecode. Default: `MetadataHash::IPFS`.
std::optional<MetadataHash> bytecodeHash = std::nullopt;
};

struct Settings
{
/// Experimental mode toggle. Default: false.
std::optional<bool> experimental = std::nullopt;
/// The optimizer settings.
std::optional<Optimizer> optimizer = std::nullopt;
/// Version of the EVM to compile for.
std::optional<langutil::EVMVersion> evmVersion = std::nullopt;
/// EVM Object Format version to compile for (experimental).
std::optional<uint8_t> eofVersion = std::nullopt;
/// Change compilation pipeline to go through the Yul intermediate representation. Default: false.
std::optional<bool> viaIR = std::nullopt;
/// Turn on SSA CFG-based code generation via the IR (experimental, implies viaIR: true). Default: true;
std::optional<bool> viaSSACFG = std::nullopt;
/// Debugging settings.
std::optional<Debug> debug = std::nullopt;
/// Metadata settings.
std::optional<Metadata> metadata = std::nullopt;
};

///
void to_json(Json&, YulOptimizerDetails const&);
void to_json(Json&, OptimizerDetails const&);
void to_json(Json&, Optimizer const&);
void to_json(Json&, Debug const&);
void to_json(Json&, Metadata const&);
void to_json(Json&, Settings const&);
}

/**
* The input the compiler is requested to compile with. It carries source and
* compiler configuration.
*/
struct StandardJSONInput
{
/// Source code which should be be compiled.
std::map<std::string, std::string> sources = {};
/// Information on which library is deployed where.
std::map<std::string, util::h160> libraries = {};
/// Contract name without a colon prefix.
std::optional<std::string> contractName = std::nullopt;
/// The compiler settings, e.g. EVM version, optimizer settings and Yul config.
std::optional<input::Settings> settings = std::nullopt;
};

///
void to_json(Json&, StandardJSONInput const&);

}
Loading