Merge pull request #13577 from ethereum/valid-input-modes-refactoring

[solc] Refactor valid input modes.
This commit is contained in:
Kamil Śliwak 2022-10-21 21:32:41 +02:00 committed by GitHub
commit 8830be9817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,6 +88,16 @@ using namespace solidity;
using namespace solidity::util; using namespace solidity::util;
using namespace solidity::langutil; using namespace solidity::langutil;
namespace
{
set<frontend::InputMode> const CompilerInputModes{
frontend::InputMode::Compiler,
frontend::InputMode::CompilerWithASTImport
};
} // anonymous namespace
namespace solidity::frontend namespace solidity::frontend
{ {
@ -160,7 +170,7 @@ static bool coloredOutput(CommandLineOptions const& _options)
void CommandLineInterface::handleBinary(string const& _contract) void CommandLineInterface::handleBinary(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (m_options.compiler.outputs.binary) if (m_options.compiler.outputs.binary)
{ {
@ -186,7 +196,7 @@ void CommandLineInterface::handleBinary(string const& _contract)
void CommandLineInterface::handleOpcode(string const& _contract) void CommandLineInterface::handleOpcode(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.output.dir.empty()) if (!m_options.output.dir.empty())
createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode)); createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode));
@ -200,7 +210,7 @@ void CommandLineInterface::handleOpcode(string const& _contract)
void CommandLineInterface::handleIR(string const& _contractName) void CommandLineInterface::handleIR(string const& _contractName)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.ir) if (!m_options.compiler.outputs.ir)
return; return;
@ -216,7 +226,7 @@ void CommandLineInterface::handleIR(string const& _contractName)
void CommandLineInterface::handleIROptimized(string const& _contractName) void CommandLineInterface::handleIROptimized(string const& _contractName)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.irOptimized) if (!m_options.compiler.outputs.irOptimized)
return; return;
@ -232,7 +242,7 @@ void CommandLineInterface::handleIROptimized(string const& _contractName)
void CommandLineInterface::handleEwasm(string const& _contractName) void CommandLineInterface::handleEwasm(string const& _contractName)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.ewasm) if (!m_options.compiler.outputs.ewasm)
return; return;
@ -255,7 +265,7 @@ void CommandLineInterface::handleEwasm(string const& _contractName)
void CommandLineInterface::handleBytecode(string const& _contract) void CommandLineInterface::handleBytecode(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (m_options.compiler.outputs.opcodes) if (m_options.compiler.outputs.opcodes)
handleOpcode(_contract); handleOpcode(_contract);
@ -265,7 +275,7 @@ void CommandLineInterface::handleBytecode(string const& _contract)
void CommandLineInterface::handleSignatureHashes(string const& _contract) void CommandLineInterface::handleSignatureHashes(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.signatureHashes) if (!m_options.compiler.outputs.signatureHashes)
return; return;
@ -297,7 +307,7 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
void CommandLineInterface::handleMetadata(string const& _contract) void CommandLineInterface::handleMetadata(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.metadata) if (!m_options.compiler.outputs.metadata)
return; return;
@ -311,7 +321,7 @@ void CommandLineInterface::handleMetadata(string const& _contract)
void CommandLineInterface::handleABI(string const& _contract) void CommandLineInterface::handleABI(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.abi) if (!m_options.compiler.outputs.abi)
return; return;
@ -325,7 +335,7 @@ void CommandLineInterface::handleABI(string const& _contract)
void CommandLineInterface::handleStorageLayout(string const& _contract) void CommandLineInterface::handleStorageLayout(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.storageLayout) if (!m_options.compiler.outputs.storageLayout)
return; return;
@ -339,7 +349,7 @@ void CommandLineInterface::handleStorageLayout(string const& _contract)
void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract) void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
bool enabled = false; bool enabled = false;
std::string suffix; std::string suffix;
@ -382,7 +392,7 @@ void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contra
void CommandLineInterface::handleGasEstimation(string const& _contract) void CommandLineInterface::handleGasEstimation(string const& _contract)
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
Json::Value estimates = m_compiler->gasEstimates(_contract); Json::Value estimates = m_compiler->gasEstimates(_contract);
sout() << "Gas estimation:" << endl; sout() << "Gas estimation:" << endl;
@ -424,13 +434,15 @@ void CommandLineInterface::handleGasEstimation(string const& _contract)
void CommandLineInterface::readInputFiles() void CommandLineInterface::readInputFiles()
{ {
solAssert(!m_standardJsonInput.has_value(), ""); solAssert(!m_standardJsonInput.has_value());
if ( static set<frontend::InputMode> const noInputFiles{
m_options.input.mode == InputMode::Help || frontend::InputMode::Help,
m_options.input.mode == InputMode::License || frontend::InputMode::License,
m_options.input.mode == InputMode::Version frontend::InputMode::Version
) };
if (noInputFiles.count(m_options.input.mode) == 1)
return; return;
m_fileReader.setBasePath(m_options.input.basePath); m_fileReader.setBasePath(m_options.input.basePath);
@ -496,7 +508,7 @@ void CommandLineInterface::readInputFiles()
string fileContent = readFileAsString(infile); string fileContent = readFileAsString(infile);
if (m_options.input.mode == InputMode::StandardJson) if (m_options.input.mode == InputMode::StandardJson)
{ {
solAssert(!m_standardJsonInput.has_value(), ""); solAssert(!m_standardJsonInput.has_value());
m_standardJsonInput = std::move(fileContent); m_standardJsonInput = std::move(fileContent);
} }
else else
@ -510,7 +522,7 @@ void CommandLineInterface::readInputFiles()
{ {
if (m_options.input.mode == InputMode::StandardJson) if (m_options.input.mode == InputMode::StandardJson)
{ {
solAssert(!m_standardJsonInput.has_value(), ""); solAssert(!m_standardJsonInput.has_value());
m_standardJsonInput = readUntilEnd(m_sin); m_standardJsonInput = readUntilEnd(m_sin);
} }
else else
@ -527,7 +539,7 @@ void CommandLineInterface::readInputFiles()
map<string, Json::Value> CommandLineInterface::parseAstFromInput() map<string, Json::Value> CommandLineInterface::parseAstFromInput()
{ {
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(m_options.input.mode == InputMode::CompilerWithASTImport);
map<string, Json::Value> sourceJsons; map<string, Json::Value> sourceJsons;
map<string, string> tmpSources; map<string, string> tmpSources;
@ -559,7 +571,7 @@ void CommandLineInterface::createFile(string const& _fileName, string const& _da
{ {
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
solAssert(!m_options.output.dir.empty(), ""); solAssert(!m_options.output.dir.empty());
// NOTE: create_directories() raises an exception if the path consists solely of '.' or '..' // NOTE: create_directories() raises an exception if the path consists solely of '.' or '..'
// (or equivalent such as './././.'). Paths like 'a/b/.' and 'a/b/..' are fine though. // (or equivalent such as './././.'). Paths like 'a/b/.' and 'a/b/..' are fine though.
@ -639,7 +651,7 @@ void CommandLineInterface::processInput()
break; break;
case InputMode::StandardJson: case InputMode::StandardJson:
{ {
solAssert(m_standardJsonInput.has_value(), ""); solAssert(m_standardJsonInput.has_value());
StandardCompiler compiler(m_fileReader.reader(), m_options.formatting.json); StandardCompiler compiler(m_fileReader.reader(), m_options.formatting.json);
sout() << compiler.compile(std::move(m_standardJsonInput.value())) << endl; sout() << compiler.compile(std::move(m_standardJsonInput.value())) << endl;
@ -678,7 +690,7 @@ void CommandLineInterface::printLicense()
void CommandLineInterface::compile() void CommandLineInterface::compile()
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
m_compiler = make_unique<CompilerStack>(m_fileReader.reader()); m_compiler = make_unique<CompilerStack>(m_fileReader.reader());
@ -789,7 +801,7 @@ void CommandLineInterface::compile()
void CommandLineInterface::handleCombinedJSON() void CommandLineInterface::handleCombinedJSON()
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.combinedJsonRequests.has_value()) if (!m_options.compiler.combinedJsonRequests.has_value())
return; return;
@ -881,7 +893,7 @@ void CommandLineInterface::handleCombinedJSON()
void CommandLineInterface::handleAst() void CommandLineInterface::handleAst()
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
if (!m_options.compiler.outputs.astCompactJson) if (!m_options.compiler.outputs.astCompactJson)
return; return;
@ -922,7 +934,7 @@ void CommandLineInterface::serveLSP()
void CommandLineInterface::link() void CommandLineInterface::link()
{ {
solAssert(m_options.input.mode == InputMode::Linker, ""); solAssert(m_options.input.mode == InputMode::Linker);
// Map from how the libraries will be named inside the bytecode to their addresses. // Map from how the libraries will be named inside the bytecode to their addresses.
map<string, h160> librariesReplacements; map<string, h160> librariesReplacements;
@ -985,7 +997,7 @@ void CommandLineInterface::link()
void CommandLineInterface::writeLinkedFiles() void CommandLineInterface::writeLinkedFiles()
{ {
solAssert(m_options.input.mode == InputMode::Linker, ""); solAssert(m_options.input.mode == InputMode::Linker);
for (auto const& src: m_fileReader.sourceUnits()) for (auto const& src: m_fileReader.sourceUnits())
if (src.first == g_stdinFileName) if (src.first == g_stdinFileName)
@ -1019,14 +1031,14 @@ string CommandLineInterface::objectWithLinkRefsHex(evmasm::LinkerObject const& _
void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulStack::Machine _targetMachine) void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulStack::Machine _targetMachine)
{ {
solAssert(m_options.input.mode == InputMode::Assembler, ""); solAssert(m_options.input.mode == InputMode::Assembler);
bool successful = true; bool successful = true;
map<string, yul::YulStack> yulStacks; map<string, yul::YulStack> yulStacks;
for (auto const& src: m_fileReader.sourceUnits()) for (auto const& src: m_fileReader.sourceUnits())
{ {
// --no-optimize-yul option is not accepted in assembly mode. // --no-optimize-yul option is not accepted in assembly mode.
solAssert(!m_options.optimizer.noOptimizeYul, ""); solAssert(!m_options.optimizer.noOptimizeYul);
auto& stack = yulStacks[src.first] = yul::YulStack( auto& stack = yulStacks[src.first] = yul::YulStack(
m_options.output.evmVersion, m_options.output.evmVersion,
@ -1123,7 +1135,7 @@ void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulS
void CommandLineInterface::outputCompilationResults() void CommandLineInterface::outputCompilationResults()
{ {
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, ""); solAssert(CompilerInputModes.count(m_options.input.mode) == 1);
handleCombinedJSON(); handleCombinedJSON();