2014-12-09 12:43:08 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-12-09 12:43:08 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-12-09 12:43:08 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-09 12:43:08 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-12-09 12:43:08 +00:00
|
|
|
/**
|
|
|
|
* @author Lefteris <lefteris@ethdev.com>
|
2015-01-09 07:05:52 +00:00
|
|
|
* @author Gav Wood <g@ethdev.com>
|
2014-12-09 12:43:08 +00:00
|
|
|
* @date 2014
|
2014-12-09 16:39:34 +00:00
|
|
|
* Solidity command line interface.
|
2014-12-09 12:43:08 +00:00
|
|
|
*/
|
2019-02-13 11:07:20 +00:00
|
|
|
#include <solc/CommandLineInterface.h>
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2021-10-11 11:48:03 +00:00
|
|
|
#include <solc/Exceptions.h>
|
|
|
|
|
2021-10-11 11:03:51 +00:00
|
|
|
#include "license.h"
|
2015-09-16 13:55:25 +00:00
|
|
|
#include "solidity/BuildInfo.h"
|
2017-01-26 12:47:57 +00:00
|
|
|
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/interface/Version.h>
|
|
|
|
#include <libsolidity/ast/ASTJsonConverter.h>
|
2019-09-11 19:16:35 +00:00
|
|
|
#include <libsolidity/ast/ASTJsonImporter.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/analysis/NameAndTypeResolver.h>
|
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
2017-02-09 14:55:57 +00:00
|
|
|
#include <libsolidity/interface/StandardCompiler.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/interface/GasEstimator.h>
|
2019-09-18 14:44:36 +00:00
|
|
|
#include <libsolidity/interface/DebugSettings.h>
|
2021-03-29 12:46:19 +00:00
|
|
|
#include <libsolidity/interface/ImportRemapper.h>
|
2020-03-24 12:22:25 +00:00
|
|
|
#include <libsolidity/interface/StorageLayout.h>
|
2021-12-13 13:53:40 +00:00
|
|
|
#include <libsolidity/lsp/LanguageServer.h>
|
|
|
|
#include <libsolidity/lsp/Transport.h>
|
2019-02-13 11:07:20 +00:00
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
#include <libyul/YulStack.h>
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2017-01-26 12:47:57 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2022-03-02 12:47:04 +00:00
|
|
|
#include <libevmasm/Disassemble.h>
|
2017-01-26 12:47:57 +00:00
|
|
|
#include <libevmasm/GasMeter.h>
|
|
|
|
|
2019-02-13 11:07:20 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
#include <liblangutil/Scanner.h>
|
2020-12-01 13:22:15 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2019-02-13 11:07:20 +00:00
|
|
|
|
2020-05-20 10:55:12 +00:00
|
|
|
#include <libsmtutil/Exceptions.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
#include <libsolutil/CommonIO.h>
|
|
|
|
#include <libsolutil/JSON.h>
|
2017-01-26 12:47:57 +00:00
|
|
|
|
2020-11-19 18:57:35 +00:00
|
|
|
#include <algorithm>
|
2021-12-13 13:53:40 +00:00
|
|
|
#include <fstream>
|
2018-11-26 23:21:53 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2021-04-06 14:12:06 +00:00
|
|
|
#include <range/v3/view/map.hpp>
|
|
|
|
|
2017-01-26 12:47:57 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2020-06-29 13:11:33 +00:00
|
|
|
#include <boost/range/adaptor/filtered.hpp>
|
2017-01-26 12:47:57 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#ifdef _WIN32 // windows
|
|
|
|
#include <io.h>
|
|
|
|
#define isatty _isatty
|
|
|
|
#define fileno _fileno
|
|
|
|
#else // unix
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2019-02-11 14:27:08 +00:00
|
|
|
|
2017-01-26 12:47:57 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2019-02-11 14:27:08 +00:00
|
|
|
#if !defined(STDERR_FILENO)
|
|
|
|
#define STDERR_FILENO 2
|
|
|
|
#endif
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
|
|
|
|
namespace solidity::frontend
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
|
|
|
|
2021-06-10 18:08:53 +00:00
|
|
|
ostream& CommandLineInterface::sout(bool _markAsUsed)
|
2020-12-08 20:06:10 +00:00
|
|
|
{
|
2021-06-10 18:08:53 +00:00
|
|
|
if (_markAsUsed)
|
|
|
|
m_hasOutput = true;
|
|
|
|
return m_sout;
|
2018-09-13 02:36:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 18:08:53 +00:00
|
|
|
ostream& CommandLineInterface::serr(bool _markAsUsed)
|
2018-09-13 02:36:14 +00:00
|
|
|
{
|
2021-06-10 18:08:53 +00:00
|
|
|
if (_markAsUsed)
|
|
|
|
m_hasOutput = true;
|
|
|
|
return m_serr;
|
2020-12-08 20:06:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 19:12:27 +00:00
|
|
|
#define cin
|
2018-09-13 02:36:14 +00:00
|
|
|
#define cout
|
|
|
|
#define cerr
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
static string const g_stdinFileName = "<stdin>";
|
2016-10-22 15:02:28 +00:00
|
|
|
static string const g_strAbi = "abi";
|
|
|
|
static string const g_strAsm = "asm";
|
|
|
|
static string const g_strAst = "ast";
|
|
|
|
static string const g_strBinary = "bin";
|
|
|
|
static string const g_strBinaryRuntime = "bin-runtime";
|
2016-10-22 15:17:49 +00:00
|
|
|
static string const g_strContracts = "contracts";
|
2021-06-07 10:33:04 +00:00
|
|
|
static string const g_strFunDebug = "function-debug";
|
|
|
|
static string const g_strFunDebugRuntime = "function-debug-runtime";
|
2020-05-28 11:17:16 +00:00
|
|
|
static string const g_strGeneratedSources = "generated-sources";
|
|
|
|
static string const g_strGeneratedSourcesRuntime = "generated-sources-runtime";
|
2016-10-22 15:17:49 +00:00
|
|
|
static string const g_strNatspecDev = "devdoc";
|
|
|
|
static string const g_strNatspecUser = "userdoc";
|
|
|
|
static string const g_strOpcodes = "opcodes";
|
|
|
|
static string const g_strSignatureHashes = "hashes";
|
2016-10-22 15:02:28 +00:00
|
|
|
static string const g_strSourceList = "sourceList";
|
2021-06-07 10:33:04 +00:00
|
|
|
static string const g_strSources = "sources";
|
2016-10-22 15:17:49 +00:00
|
|
|
static string const g_strSrcMap = "srcmap";
|
|
|
|
static string const g_strSrcMapRuntime = "srcmap-runtime";
|
2021-06-07 10:33:04 +00:00
|
|
|
static string const g_strStorageLayout = "storage-layout";
|
2017-05-22 13:47:09 +00:00
|
|
|
static string const g_strVersion = "version";
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
static bool needsHumanTargetedStdout(CommandLineOptions const& _options)
|
2017-06-08 09:56:14 +00:00
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
if (_options.compiler.estimateGas)
|
2015-10-21 14:43:31 +00:00
|
|
|
return true;
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!_options.output.dir.empty())
|
2015-10-21 14:43:31 +00:00
|
|
|
return false;
|
2021-06-07 10:33:04 +00:00
|
|
|
return
|
|
|
|
_options.compiler.outputs.abi ||
|
|
|
|
_options.compiler.outputs.asm_ ||
|
|
|
|
_options.compiler.outputs.asmJson ||
|
|
|
|
_options.compiler.outputs.binary ||
|
|
|
|
_options.compiler.outputs.binaryRuntime ||
|
|
|
|
_options.compiler.outputs.metadata ||
|
|
|
|
_options.compiler.outputs.natspecUser ||
|
|
|
|
_options.compiler.outputs.natspecDev ||
|
|
|
|
_options.compiler.outputs.opcodes ||
|
|
|
|
_options.compiler.outputs.signatureHashes ||
|
|
|
|
_options.compiler.outputs.storageLayout;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
static bool coloredOutput(CommandLineOptions const& _options)
|
2020-07-08 20:08:50 +00:00
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
return
|
|
|
|
(!_options.formatting.coloredOutput.has_value() && isatty(STDERR_FILENO)) ||
|
|
|
|
(_options.formatting.coloredOutput.has_value() && _options.formatting.coloredOutput.value());
|
2020-12-08 20:06:10 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
void CommandLineInterface::handleBinary(string const& _contract)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.outputs.binary)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2018-10-04 12:55:02 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + ".bin", objectWithLinkRefsHex(m_compiler->object(_contract)));
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
|
|
|
{
|
2019-11-21 19:08:50 +00:00
|
|
|
sout() << "Binary:" << endl;
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << objectWithLinkRefsHex(m_compiler->object(_contract)) << endl;
|
2015-08-10 10:40:02 +00:00
|
|
|
}
|
2014-12-09 19:29:29 +00:00
|
|
|
}
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.outputs.binaryRuntime)
|
2015-08-24 15:24:48 +00:00
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2018-10-04 12:55:02 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + ".bin-runtime", objectWithLinkRefsHex(m_compiler->runtimeObject(_contract)));
|
2015-08-24 15:24:48 +00:00
|
|
|
else
|
|
|
|
{
|
2019-11-21 19:08:50 +00:00
|
|
|
sout() << "Binary of the runtime part:" << endl;
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << objectWithLinkRefsHex(m_compiler->runtimeObject(_contract)) << endl;
|
2015-08-24 15:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 19:29:29 +00:00
|
|
|
}
|
2014-12-09 17:17:54 +00:00
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
void CommandLineInterface::handleOpcode(string const& _contract)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2019-12-11 16:31:36 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + ".opcode", evmasm::disassemble(m_compiler->object(_contract).bytecode));
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
2014-12-09 19:29:29 +00:00
|
|
|
{
|
2019-11-21 19:08:50 +00:00
|
|
|
sout() << "Opcodes:" << endl;
|
2019-12-11 16:31:36 +00:00
|
|
|
sout() << std::uppercase << evmasm::disassemble(m_compiler->object(_contract).bytecode);
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << endl;
|
2014-12-09 19:29:29 +00:00
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
void CommandLineInterface::handleIR(string const& _contractName)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.ir)
|
2020-02-26 14:50:34 +00:00
|
|
|
return;
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2020-02-26 14:50:34 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contractName) + ".yul", m_compiler->yulIR(_contractName));
|
|
|
|
else
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2020-02-26 14:50:34 +00:00
|
|
|
sout() << "IR:" << endl;
|
|
|
|
sout() << m_compiler->yulIR(_contractName) << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandLineInterface::handleIROptimized(string const& _contractName)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.irOptimized)
|
2020-02-26 14:50:34 +00:00
|
|
|
return;
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2020-02-26 14:50:34 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contractName) + "_opt.yul", m_compiler->yulIROptimized(_contractName));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sout() << "Optimized IR:" << endl;
|
|
|
|
sout() << m_compiler->yulIROptimized(_contractName) << endl;
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:36:12 +00:00
|
|
|
void CommandLineInterface::handleEwasm(string const& _contractName)
|
2019-06-18 16:11:04 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.ewasm)
|
2020-02-26 14:50:34 +00:00
|
|
|
return;
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2019-06-18 16:11:04 +00:00
|
|
|
{
|
2020-02-26 14:50:34 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contractName) + ".wast", m_compiler->ewasm(_contractName));
|
|
|
|
createFile(
|
|
|
|
m_compiler->filesystemFriendlyName(_contractName) + ".wasm",
|
|
|
|
asString(m_compiler->ewasmObject(_contractName).bytecode)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sout() << "Ewasm text:" << endl;
|
|
|
|
sout() << m_compiler->ewasm(_contractName) << endl;
|
|
|
|
sout() << "Ewasm binary (hex): " << m_compiler->ewasmObject(_contractName).toHex() << endl;
|
2019-06-18 16:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
void CommandLineInterface::handleBytecode(string const& _contract)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.outputs.opcodes)
|
2014-12-09 19:29:29 +00:00
|
|
|
handleOpcode(_contract);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.outputs.binary || m_options.compiler.outputs.binaryRuntime)
|
2014-12-09 19:29:29 +00:00
|
|
|
handleBinary(_contract);
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:21:44 +00:00
|
|
|
void CommandLineInterface::handleSignatureHashes(string const& _contract)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.signatureHashes)
|
2015-05-05 15:08:30 +00:00
|
|
|
return;
|
|
|
|
|
2022-02-01 14:42:08 +00:00
|
|
|
Json::Value interfaceSymbols = m_compiler->interfaceSymbols(_contract);
|
2022-01-17 21:28:58 +00:00
|
|
|
string out = "Function signatures:\n";
|
2022-02-01 14:42:08 +00:00
|
|
|
for (auto const& name: interfaceSymbols["methods"].getMemberNames())
|
|
|
|
out += interfaceSymbols["methods"][name].asString() + ": " + name + "\n";
|
2015-05-04 14:21:44 +00:00
|
|
|
|
2022-02-01 14:42:08 +00:00
|
|
|
if (interfaceSymbols.isMember("errors"))
|
2022-01-17 21:28:58 +00:00
|
|
|
{
|
|
|
|
out += "\nError signatures:\n";
|
2022-02-01 14:42:08 +00:00
|
|
|
for (auto const& name: interfaceSymbols["errors"].getMemberNames())
|
|
|
|
out += interfaceSymbols["errors"][name].asString() + ": " + name + "\n";
|
2022-01-17 21:28:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 14:42:08 +00:00
|
|
|
if (interfaceSymbols.isMember("events"))
|
2022-01-17 21:28:58 +00:00
|
|
|
{
|
|
|
|
out += "\nEvent signatures:\n";
|
2022-02-01 14:42:08 +00:00
|
|
|
for (auto const& name: interfaceSymbols["events"].getMemberNames())
|
|
|
|
out += interfaceSymbols["events"][name].asString() + ": " + name + "\n";
|
2022-01-17 21:28:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2017-02-05 19:19:29 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + ".signatures", out);
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
2022-01-17 21:28:58 +00:00
|
|
|
sout() << out;
|
2015-05-04 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 15:10:32 +00:00
|
|
|
void CommandLineInterface::handleMetadata(string const& _contract)
|
2016-11-14 10:46:43 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.metadata)
|
2016-11-14 10:46:43 +00:00
|
|
|
return;
|
|
|
|
|
2017-05-19 15:10:32 +00:00
|
|
|
string data = m_compiler->metadata(_contract);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2017-02-05 19:19:29 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + "_meta.json", data);
|
2016-11-14 10:46:43 +00:00
|
|
|
else
|
2019-11-21 19:08:50 +00:00
|
|
|
sout() << "Metadata:" << endl << data << endl;
|
2016-11-14 10:46:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 17:02:56 +00:00
|
|
|
void CommandLineInterface::handleABI(string const& _contract)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.abi)
|
2017-05-06 17:02:56 +00:00
|
|
|
return;
|
|
|
|
|
2021-10-22 02:05:49 +00:00
|
|
|
string data = jsonPrint(removeNullMembers(m_compiler->contractABI(_contract)), m_options.formatting.json);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2017-05-06 17:02:56 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + ".abi", data);
|
|
|
|
else
|
2019-11-21 19:08:50 +00:00
|
|
|
sout() << "Contract JSON ABI" << endl << data << endl;
|
2017-05-06 17:02:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 12:22:25 +00:00
|
|
|
void CommandLineInterface::handleStorageLayout(string const& _contract)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.storageLayout)
|
2020-03-24 12:22:25 +00:00
|
|
|
return;
|
|
|
|
|
2021-10-22 02:05:49 +00:00
|
|
|
string data = jsonPrint(removeNullMembers(m_compiler->storageLayout(_contract)), m_options.formatting.json);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2020-03-24 12:22:25 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + "_storage.json", data);
|
|
|
|
else
|
|
|
|
sout() << "Contract Storage Layout:" << endl << data << endl;
|
|
|
|
}
|
|
|
|
|
2017-07-27 10:28:04 +00:00
|
|
|
void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
bool enabled = false;
|
2014-12-09 12:43:08 +00:00
|
|
|
std::string suffix;
|
|
|
|
std::string title;
|
2017-07-27 10:28:04 +00:00
|
|
|
|
|
|
|
if (_natspecDev)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
enabled = m_options.compiler.outputs.natspecDev;
|
2014-12-09 12:43:08 +00:00
|
|
|
suffix = ".docdev";
|
|
|
|
title = "Developer Documentation";
|
2017-07-27 10:28:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
enabled = m_options.compiler.outputs.natspecUser;
|
2017-07-27 10:28:04 +00:00
|
|
|
suffix = ".docuser";
|
|
|
|
title = "User Documentation";
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (enabled)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-10-22 02:05:49 +00:00
|
|
|
std::string output = jsonPrint(
|
2020-04-27 08:28:54 +00:00
|
|
|
removeNullMembers(
|
|
|
|
_natspecDev ?
|
|
|
|
m_compiler->natspecDev(_contract) :
|
|
|
|
m_compiler->natspecUser(_contract)
|
2021-10-22 02:05:49 +00:00
|
|
|
),
|
|
|
|
m_options.formatting.json
|
2017-07-27 10:28:04 +00:00
|
|
|
);
|
2016-11-15 01:04:00 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2017-02-05 19:19:29 +00:00
|
|
|
createFile(m_compiler->filesystemFriendlyName(_contract) + suffix, output);
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
|
|
|
{
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << title << endl;
|
|
|
|
sout() << output << endl;
|
2015-08-10 10:40:02 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 12:19:58 +00:00
|
|
|
void CommandLineInterface::handleGasEstimation(string const& _contract)
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2017-04-10 13:52:13 +00:00
|
|
|
Json::Value estimates = m_compiler->gasEstimates(_contract);
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << "Gas estimation:" << endl;
|
2017-04-10 13:52:13 +00:00
|
|
|
|
|
|
|
if (estimates["creation"].isObject())
|
2015-05-22 12:19:58 +00:00
|
|
|
{
|
2017-04-10 13:52:13 +00:00
|
|
|
Json::Value creation = estimates["creation"];
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << "construction:" << endl;
|
|
|
|
sout() << " " << creation["executionCost"].asString();
|
|
|
|
sout() << " + " << creation["codeDepositCost"].asString();
|
|
|
|
sout() << " = " << creation["totalCost"].asString() << endl;
|
2015-05-22 12:19:58 +00:00
|
|
|
}
|
2017-04-10 13:52:13 +00:00
|
|
|
|
|
|
|
if (estimates["external"].isObject())
|
2015-05-26 09:27:59 +00:00
|
|
|
{
|
2017-04-10 13:52:13 +00:00
|
|
|
Json::Value externalFunctions = estimates["external"];
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << "external:" << endl;
|
2017-04-10 13:52:13 +00:00
|
|
|
for (auto const& name: externalFunctions.getMemberNames())
|
2015-05-22 12:19:58 +00:00
|
|
|
{
|
2017-04-10 13:52:13 +00:00
|
|
|
if (name.empty())
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << " fallback:\t";
|
2017-04-10 13:52:13 +00:00
|
|
|
else
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << " " << name << ":\t";
|
|
|
|
sout() << externalFunctions[name].asString() << endl;
|
2015-06-18 15:38:26 +00:00
|
|
|
}
|
2017-04-10 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (estimates["internal"].isObject())
|
|
|
|
{
|
|
|
|
Json::Value internalFunctions = estimates["internal"];
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << "internal:" << endl;
|
2017-04-10 13:52:13 +00:00
|
|
|
for (auto const& name: internalFunctions.getMemberNames())
|
2015-05-26 09:27:59 +00:00
|
|
|
{
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << " " << name << ":\t";
|
|
|
|
sout() << internalFunctions[name].asString() << endl;
|
2015-05-22 12:19:58 +00:00
|
|
|
}
|
2015-05-26 09:27:59 +00:00
|
|
|
}
|
2015-05-22 12:19:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 12:16:29 +00:00
|
|
|
void CommandLineInterface::readInputFiles()
|
2016-01-25 18:42:17 +00:00
|
|
|
{
|
2021-06-13 13:53:16 +00:00
|
|
|
solAssert(!m_standardJsonInput.has_value(), "");
|
|
|
|
|
2021-10-11 11:03:51 +00:00
|
|
|
if (
|
|
|
|
m_options.input.mode == InputMode::Help ||
|
|
|
|
m_options.input.mode == InputMode::License ||
|
|
|
|
m_options.input.mode == InputMode::Version
|
|
|
|
)
|
2021-10-12 12:16:29 +00:00
|
|
|
return;
|
2021-10-11 11:03:51 +00:00
|
|
|
|
2021-06-13 13:53:16 +00:00
|
|
|
m_fileReader.setBasePath(m_options.input.basePath);
|
|
|
|
|
2021-07-27 23:00:52 +00:00
|
|
|
if (m_fileReader.basePath() != "")
|
2021-06-13 13:53:16 +00:00
|
|
|
{
|
2021-07-27 23:00:52 +00:00
|
|
|
if (!boost::filesystem::exists(m_fileReader.basePath()))
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineValidationError, "Base path does not exist: \"" + m_fileReader.basePath().string() + '"');
|
2021-07-27 23:00:52 +00:00
|
|
|
|
|
|
|
if (!boost::filesystem::is_directory(m_fileReader.basePath()))
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineValidationError, "Base path is not a directory: \"" + m_fileReader.basePath().string() + '"');
|
2021-06-13 13:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 17:17:44 +00:00
|
|
|
for (boost::filesystem::path const& includePath: m_options.input.includePaths)
|
|
|
|
m_fileReader.addIncludePath(includePath);
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
for (boost::filesystem::path const& allowedDirectory: m_options.input.allowedDirectories)
|
|
|
|
m_fileReader.allowDirectory(allowedDirectory);
|
|
|
|
|
2021-08-26 16:34:34 +00:00
|
|
|
map<std::string, set<boost::filesystem::path>> collisions =
|
|
|
|
m_fileReader.detectSourceUnitNameCollisions(m_options.input.paths);
|
|
|
|
if (!collisions.empty())
|
|
|
|
{
|
|
|
|
auto pathToQuotedString = [](boost::filesystem::path const& _path){ return "\"" + _path.string() + "\""; };
|
|
|
|
|
2021-08-15 01:14:38 +00:00
|
|
|
string message =
|
|
|
|
"Source unit name collision detected. "
|
|
|
|
"The specified values of base path and/or include paths would result in multiple "
|
|
|
|
"input files being assigned the same source unit name:\n";
|
|
|
|
|
2021-08-26 16:34:34 +00:00
|
|
|
for (auto const& [sourceUnitName, normalizedInputPaths]: collisions)
|
|
|
|
{
|
2021-08-15 01:14:38 +00:00
|
|
|
message += sourceUnitName + " matches: ";
|
2022-03-07 04:25:35 +00:00
|
|
|
message += util::joinHumanReadable(normalizedInputPaths | ranges::views::transform(pathToQuotedString)) + "\n";
|
2021-08-26 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineValidationError, message);
|
2021-08-26 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
for (boost::filesystem::path const& infile: m_options.input.paths)
|
|
|
|
{
|
|
|
|
if (!boost::filesystem::exists(infile))
|
2016-01-25 18:42:17 +00:00
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.input.ignoreMissingFiles)
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineValidationError, '"' + infile.string() + "\" is not found.");
|
2016-01-25 18:42:17 +00:00
|
|
|
else
|
2021-06-07 10:33:04 +00:00
|
|
|
serr() << infile << " is not found. Skipping." << endl;
|
2016-01-25 18:42:17 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-25 18:42:17 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!boost::filesystem::is_regular_file(infile))
|
|
|
|
{
|
|
|
|
if (!m_options.input.ignoreMissingFiles)
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineValidationError, '"' + infile.string() + "\" is not a valid file.");
|
2021-06-07 10:33:04 +00:00
|
|
|
else
|
|
|
|
serr() << infile << " is not a valid file. Skipping." << endl;
|
|
|
|
|
|
|
|
continue;
|
2016-01-25 18:42:17 +00:00
|
|
|
}
|
2021-04-06 14:12:06 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
// NOTE: we ignore the FileNotFound exception as we manually check above
|
2021-07-13 07:12:04 +00:00
|
|
|
string fileContent = readFileAsString(infile);
|
2021-06-13 13:53:16 +00:00
|
|
|
if (m_options.input.mode == InputMode::StandardJson)
|
|
|
|
{
|
|
|
|
solAssert(!m_standardJsonInput.has_value(), "");
|
|
|
|
m_standardJsonInput = move(fileContent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-13 13:52:17 +00:00
|
|
|
m_fileReader.addOrUpdateFile(infile, move(fileContent));
|
2021-06-13 13:53:16 +00:00
|
|
|
m_fileReader.allowDirectory(boost::filesystem::canonical(infile).remove_filename());
|
|
|
|
}
|
2021-06-07 10:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_options.input.addStdin)
|
2021-06-13 13:53:16 +00:00
|
|
|
{
|
|
|
|
if (m_options.input.mode == InputMode::StandardJson)
|
|
|
|
{
|
|
|
|
solAssert(!m_standardJsonInput.has_value(), "");
|
|
|
|
m_standardJsonInput = readUntilEnd(m_sin);
|
|
|
|
}
|
|
|
|
else
|
2021-06-15 12:52:53 +00:00
|
|
|
m_fileReader.setStdin(readUntilEnd(m_sin));
|
2021-06-13 13:53:16 +00:00
|
|
|
}
|
2021-04-06 14:12:06 +00:00
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
if (
|
|
|
|
m_options.input.mode != InputMode::LanguageServer &&
|
|
|
|
m_fileReader.sourceUnits().empty() &&
|
|
|
|
!m_standardJsonInput.has_value()
|
|
|
|
)
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineValidationError, "All specified input files either do not exist or are not regular files.");
|
2016-01-25 18:42:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
map<string, Json::Value> CommandLineInterface::parseAstFromInput()
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
map<string, Json::Value> sourceJsons;
|
|
|
|
map<string, string> tmpSources;
|
|
|
|
|
2021-12-13 13:52:17 +00:00
|
|
|
for (SourceCode const& sourceCode: m_fileReader.sourceUnits() | ranges::views::values)
|
2019-09-11 19:16:35 +00:00
|
|
|
{
|
|
|
|
Json::Value ast;
|
2021-04-06 14:12:06 +00:00
|
|
|
astAssert(jsonParseStrict(sourceCode, ast), "Input file could not be parsed to JSON");
|
2019-09-11 19:16:35 +00:00
|
|
|
astAssert(ast.isMember("sources"), "Invalid Format for import-JSON: Must have 'sources'-object");
|
|
|
|
|
|
|
|
for (auto& src: ast["sources"].getMemberNames())
|
|
|
|
{
|
|
|
|
std::string astKey = ast["sources"][src].isMember("ast") ? "ast" : "AST";
|
|
|
|
|
|
|
|
astAssert(ast["sources"][src].isMember(astKey), "astkey is not member");
|
|
|
|
astAssert(ast["sources"][src][astKey]["nodeType"].asString() == "SourceUnit", "Top-level node should be a 'SourceUnit'");
|
|
|
|
astAssert(sourceJsons.count(src) == 0, "All sources must have unique names");
|
|
|
|
sourceJsons.emplace(src, move(ast["sources"][src][astKey]));
|
|
|
|
tmpSources[src] = util::jsonCompactPrint(ast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:52:17 +00:00
|
|
|
m_fileReader.setSourceUnits(tmpSources);
|
2021-04-06 14:12:06 +00:00
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
return sourceJsons;
|
|
|
|
}
|
|
|
|
|
2015-08-10 10:44:59 +00:00
|
|
|
void CommandLineInterface::createFile(string const& _fileName, string const& _data)
|
|
|
|
{
|
|
|
|
namespace fs = boost::filesystem;
|
2020-11-14 01:58:13 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
solAssert(!m_options.output.dir.empty(), "");
|
2020-11-14 01:58:13 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
// The simplest workaround is to use an absolute path.
|
2021-06-07 10:33:04 +00:00
|
|
|
fs::create_directories(fs::absolute(m_options.output.dir));
|
2020-11-14 01:58:13 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
string pathName = (m_options.output.dir / _fileName).string();
|
|
|
|
if (fs::exists(pathName) && !m_options.output.overwriteFiles)
|
2021-10-13 10:55:49 +00:00
|
|
|
solThrow(CommandLineOutputError, "Refusing to overwrite existing file \"" + pathName + "\" (use --overwrite to force).");
|
|
|
|
|
2015-08-19 23:09:39 +00:00
|
|
|
ofstream outFile(pathName);
|
2015-07-21 13:29:15 +00:00
|
|
|
outFile << _data;
|
2015-08-10 10:44:59 +00:00
|
|
|
if (!outFile)
|
2021-10-13 10:55:49 +00:00
|
|
|
solThrow(CommandLineOutputError, "Could not write to file \"" + pathName + "\".");
|
2015-08-10 10:44:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 23:46:30 +00:00
|
|
|
void CommandLineInterface::createJson(string const& _fileName, string const& _json)
|
2017-05-22 20:39:38 +00:00
|
|
|
{
|
2017-07-03 23:46:30 +00:00
|
|
|
createFile(boost::filesystem::basename(_fileName) + string(".json"), _json);
|
2017-05-22 20:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 12:16:29 +00:00
|
|
|
bool CommandLineInterface::run(int _argc, char const* const* _argv)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!parseArguments(_argc, _argv))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
readInputFiles();
|
|
|
|
processInput();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (CommandLineError const& _exception)
|
|
|
|
{
|
|
|
|
m_hasOutput = true;
|
|
|
|
|
|
|
|
// There might be no message in the exception itself if the error output is bulky and has
|
|
|
|
// already been printed to stderr (this happens e.g. for compiler errors).
|
|
|
|
if (_exception.what() != ""s)
|
|
|
|
serr() << _exception.what() << endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
bool CommandLineInterface::parseArguments(int _argc, char const* const* _argv)
|
2014-12-09 16:39:34 +00:00
|
|
|
{
|
2021-10-11 12:20:56 +00:00
|
|
|
CommandLineParser parser;
|
2021-10-11 11:03:51 +00:00
|
|
|
|
|
|
|
if (isatty(fileno(stdin)) && _argc == 1)
|
|
|
|
{
|
|
|
|
// If the terminal is taking input from the user, provide more user-friendly output.
|
|
|
|
CommandLineParser::printHelp(sout());
|
|
|
|
|
|
|
|
// In this case we want to exit with an error but not display any error message.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:16:29 +00:00
|
|
|
parser.parse(_argc, _argv);
|
2021-06-07 10:33:04 +00:00
|
|
|
m_options = parser.options();
|
2015-08-19 23:09:39 +00:00
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
return true;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 12:16:29 +00:00
|
|
|
void CommandLineInterface::processInput()
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-06-13 13:53:16 +00:00
|
|
|
switch (m_options.input.mode)
|
2017-04-19 15:59:03 +00:00
|
|
|
{
|
2021-10-11 11:03:51 +00:00
|
|
|
case InputMode::Help:
|
|
|
|
CommandLineParser::printHelp(sout());
|
2021-10-11 11:30:08 +00:00
|
|
|
break;
|
2021-10-11 11:03:51 +00:00
|
|
|
case InputMode::License:
|
|
|
|
printLicense();
|
2021-10-11 11:17:01 +00:00
|
|
|
break;
|
2021-10-11 11:03:51 +00:00
|
|
|
case InputMode::Version:
|
|
|
|
printVersion();
|
2021-10-11 11:17:01 +00:00
|
|
|
break;
|
2021-06-13 13:53:16 +00:00
|
|
|
case InputMode::StandardJson:
|
2017-02-09 14:55:57 +00:00
|
|
|
{
|
2021-06-13 13:53:16 +00:00
|
|
|
solAssert(m_standardJsonInput.has_value(), "");
|
|
|
|
|
2021-07-07 11:18:24 +00:00
|
|
|
StandardCompiler compiler(m_fileReader.reader(), m_options.formatting.json);
|
2021-06-13 13:53:16 +00:00
|
|
|
sout() << compiler.compile(move(m_standardJsonInput.value())) << endl;
|
|
|
|
m_standardJsonInput.reset();
|
2021-10-11 11:17:01 +00:00
|
|
|
break;
|
2017-02-09 14:55:57 +00:00
|
|
|
}
|
2021-12-13 13:53:40 +00:00
|
|
|
case InputMode::LanguageServer:
|
|
|
|
serveLSP();
|
|
|
|
break;
|
2021-06-13 13:53:16 +00:00
|
|
|
case InputMode::Assembler:
|
2021-10-12 12:16:29 +00:00
|
|
|
assemble(m_options.assembly.inputLanguage, m_options.assembly.targetMachine);
|
2021-10-11 11:17:01 +00:00
|
|
|
break;
|
2021-06-13 13:53:16 +00:00
|
|
|
case InputMode::Linker:
|
2021-10-12 12:16:29 +00:00
|
|
|
link();
|
2021-10-11 11:17:01 +00:00
|
|
|
writeLinkedFiles();
|
|
|
|
break;
|
2021-06-13 13:53:16 +00:00
|
|
|
case InputMode::Compiler:
|
|
|
|
case InputMode::CompilerWithASTImport:
|
2021-10-12 12:16:29 +00:00
|
|
|
compile();
|
2021-10-11 11:17:01 +00:00
|
|
|
outputCompilationResults();
|
2021-06-13 13:53:16 +00:00
|
|
|
}
|
2021-06-07 10:33:04 +00:00
|
|
|
}
|
2021-01-19 11:56:22 +00:00
|
|
|
|
2021-10-11 11:03:51 +00:00
|
|
|
void CommandLineInterface::printVersion()
|
|
|
|
{
|
|
|
|
sout() << "solc, the solidity compiler commandline interface" << endl;
|
|
|
|
sout() << "Version: " << solidity::frontend::VersionString << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandLineInterface::printLicense()
|
|
|
|
{
|
|
|
|
sout() << otherLicenses << endl;
|
|
|
|
// This is a static variable generated by cmake from LICENSE.txt
|
|
|
|
sout() << licenseText << endl;
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:16:29 +00:00
|
|
|
void CommandLineInterface::compile()
|
2021-06-07 10:33:04 +00:00
|
|
|
{
|
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
2020-11-02 20:20:20 +00:00
|
|
|
|
2021-04-06 14:12:06 +00:00
|
|
|
m_compiler = make_unique<CompilerStack>(m_fileReader.reader());
|
2017-10-26 20:56:00 +00:00
|
|
|
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReferenceFormatter formatter(serr(false), *m_compiler, coloredOutput(m_options), m_options.formatting.withErrorIds);
|
2017-10-26 20:56:00 +00:00
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
try
|
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.metadata.literalSources)
|
2017-01-25 12:45:18 +00:00
|
|
|
m_compiler->useMetadataLiteralSources(true);
|
2021-06-07 10:33:04 +00:00
|
|
|
m_compiler->setMetadataHash(m_options.metadata.hash);
|
|
|
|
if (m_options.modelChecker.initialize)
|
|
|
|
m_compiler->setModelCheckerSettings(m_options.modelChecker.settings);
|
|
|
|
m_compiler->setRemappings(m_options.input.remappings);
|
|
|
|
m_compiler->setLibraries(m_options.linker.libraries);
|
2022-03-09 15:02:31 +00:00
|
|
|
m_compiler->setViaIR(m_options.output.viaIR);
|
2021-06-07 10:33:04 +00:00
|
|
|
m_compiler->setEVMVersion(m_options.output.evmVersion);
|
|
|
|
m_compiler->setRevertStringBehaviour(m_options.output.revertStrings);
|
2021-09-13 12:17:05 +00:00
|
|
|
if (m_options.output.debugInfoSelection.has_value())
|
|
|
|
m_compiler->selectDebugInfo(m_options.output.debugInfoSelection.value());
|
2014-12-09 16:39:34 +00:00
|
|
|
// TODO: Perhaps we should not compile unless requested
|
2019-02-21 17:35:41 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
m_compiler->enableIRGeneration(m_options.compiler.outputs.ir || m_options.compiler.outputs.irOptimized);
|
|
|
|
m_compiler->enableEwasmGeneration(m_options.compiler.outputs.ewasm);
|
2021-09-03 14:50:29 +00:00
|
|
|
m_compiler->enableEvmBytecodeGeneration(
|
|
|
|
m_options.compiler.estimateGas ||
|
|
|
|
m_options.compiler.outputs.asm_ ||
|
|
|
|
m_options.compiler.outputs.asmJson ||
|
|
|
|
m_options.compiler.outputs.opcodes ||
|
|
|
|
m_options.compiler.outputs.binary ||
|
|
|
|
m_options.compiler.outputs.binaryRuntime ||
|
|
|
|
(m_options.compiler.combinedJsonRequests && (
|
|
|
|
m_options.compiler.combinedJsonRequests->binary ||
|
|
|
|
m_options.compiler.combinedJsonRequests->binaryRuntime ||
|
|
|
|
m_options.compiler.combinedJsonRequests->opcodes ||
|
|
|
|
m_options.compiler.combinedJsonRequests->asm_ ||
|
|
|
|
m_options.compiler.combinedJsonRequests->generatedSources ||
|
|
|
|
m_options.compiler.combinedJsonRequests->generatedSourcesRuntime ||
|
|
|
|
m_options.compiler.combinedJsonRequests->srcMap ||
|
|
|
|
m_options.compiler.combinedJsonRequests->srcMapRuntime ||
|
|
|
|
m_options.compiler.combinedJsonRequests->funDebug ||
|
|
|
|
m_options.compiler.combinedJsonRequests->funDebugRuntime
|
|
|
|
))
|
|
|
|
);
|
2019-03-04 22:26:46 +00:00
|
|
|
|
2021-08-03 15:11:17 +00:00
|
|
|
m_compiler->setOptimiserSettings(m_options.optimiserSettings());
|
2017-07-17 10:54:02 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.input.mode == InputMode::CompilerWithASTImport)
|
2019-09-11 19:16:35 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_compiler->importASTs(parseAstFromInput());
|
|
|
|
|
|
|
|
if (!m_compiler->analyze())
|
|
|
|
{
|
2021-06-29 12:38:59 +00:00
|
|
|
formatter.printErrorInformation(m_compiler->errors());
|
2019-09-11 19:16:35 +00:00
|
|
|
astAssert(false, "Analysis of the AST failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception const& _exc)
|
|
|
|
{
|
2021-10-12 12:16:29 +00:00
|
|
|
// FIXME: AST import is missing proper validations. This hack catches failing
|
|
|
|
// assertions and presents them as if they were compiler errors.
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineExecutionError, "Failed to import AST: "s + _exc.what());
|
2019-09-11 19:16:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-13 13:52:17 +00:00
|
|
|
m_compiler->setSources(m_fileReader.sourceUnits());
|
2021-06-07 10:33:04 +00:00
|
|
|
m_compiler->setParserErrorRecovery(m_options.input.errorRecovery);
|
2019-09-11 19:16:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
bool successful = m_compiler->compile(m_options.output.stopAfter);
|
2015-10-21 14:43:31 +00:00
|
|
|
|
2015-10-01 15:59:01 +00:00
|
|
|
for (auto const& error: m_compiler->errors())
|
2018-09-13 02:36:14 +00:00
|
|
|
{
|
2021-06-10 18:08:53 +00:00
|
|
|
m_hasOutput = true;
|
2020-10-30 21:11:29 +00:00
|
|
|
formatter.printErrorInformation(*error);
|
2018-09-13 02:36:14 +00:00
|
|
|
}
|
2015-10-21 14:43:31 +00:00
|
|
|
|
2021-08-15 01:14:38 +00:00
|
|
|
if (!successful && !m_options.input.errorRecovery)
|
|
|
|
solThrow(CommandLineExecutionError, "");
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (CompilerError const& _exception)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-06-10 18:08:53 +00:00
|
|
|
m_hasOutput = true;
|
2020-10-30 21:11:29 +00:00
|
|
|
formatter.printExceptionInformation(_exception, "Compiler error");
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineExecutionError, "");
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
2015-10-02 12:41:40 +00:00
|
|
|
catch (Error const& _error)
|
2015-06-15 13:21:23 +00:00
|
|
|
{
|
2015-10-02 12:41:40 +00:00
|
|
|
if (_error.type() == Error::Type::DocstringParsingError)
|
2021-08-15 01:14:38 +00:00
|
|
|
{
|
|
|
|
serr() << *boost::get_error_info<errinfo_comment>(_error);
|
|
|
|
solThrow(CommandLineExecutionError, "Documentation parsing failed.");
|
|
|
|
}
|
2015-10-02 12:41:40 +00:00
|
|
|
else
|
2018-09-13 02:36:14 +00:00
|
|
|
{
|
2021-06-10 18:08:53 +00:00
|
|
|
m_hasOutput = true;
|
2020-10-30 21:11:29 +00:00
|
|
|
formatter.printExceptionInformation(_error, _error.typeName());
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineExecutionError, "");
|
2018-09-13 02:36:14 +00:00
|
|
|
}
|
2015-06-15 13:21:23 +00:00
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
void CommandLineInterface::handleCombinedJSON()
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.combinedJsonRequests.has_value())
|
2015-04-24 09:48:23 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
Json::Value output(Json::objectValue);
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
output[g_strVersion] = frontend::VersionString;
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<string> contracts = m_compiler->contractNames();
|
2015-04-23 12:40:42 +00:00
|
|
|
|
|
|
|
if (!contracts.empty())
|
2016-10-22 15:02:28 +00:00
|
|
|
output[g_strContracts] = Json::Value(Json::objectValue);
|
2015-04-23 12:40:42 +00:00
|
|
|
for (string const& contractName: contracts)
|
|
|
|
{
|
2017-07-27 14:40:01 +00:00
|
|
|
Json::Value& contractData = output[g_strContracts][contractName] = Json::objectValue;
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->abi)
|
2020-11-26 10:43:35 +00:00
|
|
|
contractData[g_strAbi] = m_compiler->contractABI(contractName);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->metadata)
|
2017-05-19 15:10:32 +00:00
|
|
|
contractData["metadata"] = m_compiler->metadata(contractName);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->binary && m_compiler->compilationSuccessful())
|
2016-10-22 15:02:28 +00:00
|
|
|
contractData[g_strBinary] = m_compiler->object(contractName).toHex();
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->binaryRuntime && m_compiler->compilationSuccessful())
|
2016-10-22 15:02:28 +00:00
|
|
|
contractData[g_strBinaryRuntime] = m_compiler->runtimeObject(contractName).toHex();
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->opcodes && m_compiler->compilationSuccessful())
|
2019-12-11 16:31:36 +00:00
|
|
|
contractData[g_strOpcodes] = evmasm::disassemble(m_compiler->object(contractName).bytecode);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->asm_ && m_compiler->compilationSuccessful())
|
2020-02-18 09:22:34 +00:00
|
|
|
contractData[g_strAsm] = m_compiler->assemblyJSON(contractName);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->storageLayout && m_compiler->compilationSuccessful())
|
2020-11-26 10:43:35 +00:00
|
|
|
contractData[g_strStorageLayout] = m_compiler->storageLayout(contractName);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->generatedSources && m_compiler->compilationSuccessful())
|
2020-05-28 11:17:16 +00:00
|
|
|
contractData[g_strGeneratedSources] = m_compiler->generatedSources(contractName, false);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->generatedSourcesRuntime && m_compiler->compilationSuccessful())
|
2020-05-28 11:17:16 +00:00
|
|
|
contractData[g_strGeneratedSourcesRuntime] = m_compiler->generatedSources(contractName, true);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->srcMap && m_compiler->compilationSuccessful())
|
2016-07-01 08:14:50 +00:00
|
|
|
{
|
|
|
|
auto map = m_compiler->sourceMapping(contractName);
|
2016-10-22 15:02:28 +00:00
|
|
|
contractData[g_strSrcMap] = map ? *map : "";
|
2016-07-01 08:14:50 +00:00
|
|
|
}
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->srcMapRuntime && m_compiler->compilationSuccessful())
|
2016-07-01 08:14:50 +00:00
|
|
|
{
|
|
|
|
auto map = m_compiler->runtimeSourceMapping(contractName);
|
2016-10-22 15:02:28 +00:00
|
|
|
contractData[g_strSrcMapRuntime] = map ? *map : "";
|
2016-07-01 08:14:50 +00:00
|
|
|
}
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->funDebug && m_compiler->compilationSuccessful())
|
2020-05-07 12:46:47 +00:00
|
|
|
contractData[g_strFunDebug] = StandardCompiler::formatFunctionDebugData(
|
|
|
|
m_compiler->object(contractName).functionDebugData
|
|
|
|
);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->funDebugRuntime && m_compiler->compilationSuccessful())
|
2020-05-07 12:46:47 +00:00
|
|
|
contractData[g_strFunDebugRuntime] = StandardCompiler::formatFunctionDebugData(
|
|
|
|
m_compiler->runtimeObject(contractName).functionDebugData
|
|
|
|
);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->signatureHashes)
|
2022-02-01 14:42:08 +00:00
|
|
|
contractData[g_strSignatureHashes] = m_compiler->interfaceSymbols(contractName)["methods"];
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->natspecDev)
|
2020-11-26 10:43:35 +00:00
|
|
|
contractData[g_strNatspecDev] = m_compiler->natspecDev(contractName);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->natspecUser)
|
2020-11-26 10:43:35 +00:00
|
|
|
contractData[g_strNatspecUser] = m_compiler->natspecUser(contractName);
|
2015-04-23 12:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
bool needsSourceList =
|
|
|
|
m_options.compiler.combinedJsonRequests->ast ||
|
|
|
|
m_options.compiler.combinedJsonRequests->srcMap ||
|
|
|
|
m_options.compiler.combinedJsonRequests->srcMapRuntime;
|
2016-07-01 08:14:50 +00:00
|
|
|
if (needsSourceList)
|
|
|
|
{
|
|
|
|
// Indices into this array are used to abbreviate source names in source locations.
|
2016-10-22 15:02:28 +00:00
|
|
|
output[g_strSourceList] = Json::Value(Json::arrayValue);
|
2016-07-01 08:14:50 +00:00
|
|
|
|
|
|
|
for (auto const& source: m_compiler->sourceNames())
|
2016-10-22 15:02:28 +00:00
|
|
|
output[g_strSourceList].append(source);
|
2016-07-01 08:14:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.combinedJsonRequests->ast)
|
2015-04-23 12:40:42 +00:00
|
|
|
{
|
2016-10-22 15:02:28 +00:00
|
|
|
output[g_strSources] = Json::Value(Json::objectValue);
|
2021-12-13 13:52:17 +00:00
|
|
|
for (auto const& sourceCode: m_fileReader.sourceUnits())
|
2015-04-23 12:40:42 +00:00
|
|
|
{
|
2020-11-20 14:35:53 +00:00
|
|
|
ASTJsonConverter converter(m_compiler->state(), m_compiler->sourceIndices());
|
2016-10-22 15:02:28 +00:00
|
|
|
output[g_strSources][sourceCode.first] = Json::Value(Json::objectValue);
|
2017-03-20 18:06:17 +00:00
|
|
|
output[g_strSources][sourceCode.first]["AST"] = converter.toJson(m_compiler->ast(sourceCode.first));
|
2015-04-23 12:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 20:44:19 +00:00
|
|
|
|
2021-07-07 11:18:24 +00:00
|
|
|
string json = jsonPrint(removeNullMembers(std::move(output)), m_options.formatting.json);
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2017-07-03 23:46:30 +00:00
|
|
|
createJson("combined", json);
|
2017-05-22 20:44:19 +00:00
|
|
|
else
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << json << endl;
|
2015-04-23 12:40:42 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 14:35:53 +00:00
|
|
|
void CommandLineInterface::handleAst()
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.compiler.outputs.astCompactJson)
|
2020-11-20 14:35:53 +00:00
|
|
|
return;
|
2015-01-05 14:46:40 +00:00
|
|
|
|
2020-11-20 14:35:53 +00:00
|
|
|
vector<ASTNode const*> asts;
|
2021-12-13 13:52:17 +00:00
|
|
|
for (auto const& sourceCode: m_fileReader.sourceUnits())
|
2020-11-20 14:35:53 +00:00
|
|
|
asts.push_back(&m_compiler->ast(sourceCode.first));
|
2015-01-05 14:46:40 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2021-12-13 13:52:17 +00:00
|
|
|
for (auto const& sourceCode: m_fileReader.sourceUnits())
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2020-11-20 14:35:53 +00:00
|
|
|
stringstream data;
|
|
|
|
string postfix = "";
|
2021-10-22 02:05:49 +00:00
|
|
|
ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(data, m_compiler->ast(sourceCode.first), m_options.formatting.json);
|
2020-11-20 14:35:53 +00:00
|
|
|
postfix += "_json";
|
|
|
|
boost::filesystem::path path(sourceCode.first);
|
|
|
|
createFile(path.filename().string() + postfix + ".ast", data.str());
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
2020-11-20 14:35:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sout() << "JSON AST (compact format):" << endl << endl;
|
2021-12-13 13:52:17 +00:00
|
|
|
for (auto const& sourceCode: m_fileReader.sourceUnits())
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2020-11-20 14:35:53 +00:00
|
|
|
sout() << endl << "======= " << sourceCode.first << " =======" << endl;
|
2021-10-22 02:05:49 +00:00
|
|
|
ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(sout(), m_compiler->ast(sourceCode.first), m_options.formatting.json);
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-05 14:46:40 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 13:53:40 +00:00
|
|
|
void CommandLineInterface::serveLSP()
|
|
|
|
{
|
|
|
|
lsp::IOStreamTransport transport;
|
|
|
|
if (!lsp::LanguageServer{transport}.run())
|
|
|
|
solThrow(CommandLineExecutionError, "LSP terminated abnormally.");
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:16:29 +00:00
|
|
|
void CommandLineInterface::link()
|
2015-09-11 17:35:01 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Linker, "");
|
|
|
|
|
2016-09-06 09:12:55 +00:00
|
|
|
// Map from how the libraries will be named inside the bytecode to their addresses.
|
2016-09-01 23:16:03 +00:00
|
|
|
map<string, h160> librariesReplacements;
|
2016-09-06 09:57:21 +00:00
|
|
|
int const placeholderSize = 40; // 20 bytes or 40 hex characters
|
2021-06-07 10:33:04 +00:00
|
|
|
for (auto const& library: m_options.linker.libraries)
|
2016-09-01 23:16:03 +00:00
|
|
|
{
|
|
|
|
string const& name = library.first;
|
2016-09-06 09:12:55 +00:00
|
|
|
// Library placeholders are 40 hex digits (20 bytes) that start and end with '__'.
|
2018-10-04 12:55:02 +00:00
|
|
|
// This leaves 36 characters for the library identifier. The identifier used to
|
|
|
|
// be just the cropped or '_'-padded library name, but this changed to
|
|
|
|
// the cropped hex representation of the hash of the library name.
|
|
|
|
// We support both ways of linking here.
|
2019-12-11 16:31:36 +00:00
|
|
|
librariesReplacements["__" + evmasm::LinkerObject::libraryPlaceholder(name) + "__"] = library.second;
|
2018-10-04 12:55:02 +00:00
|
|
|
|
2016-09-01 23:16:03 +00:00
|
|
|
string replacement = "__";
|
2016-09-06 09:12:55 +00:00
|
|
|
for (size_t i = 0; i < placeholderSize - 4; ++i)
|
2016-09-01 23:16:03 +00:00
|
|
|
replacement.push_back(i < name.size() ? name[i] : '_');
|
|
|
|
replacement += "__";
|
|
|
|
librariesReplacements[replacement] = library.second;
|
|
|
|
}
|
2021-04-06 14:12:06 +00:00
|
|
|
|
2021-12-13 13:52:17 +00:00
|
|
|
FileReader::StringMap sourceCodes = m_fileReader.sourceUnits();
|
2021-04-06 14:12:06 +00:00
|
|
|
for (auto& src: sourceCodes)
|
2015-09-11 17:35:01 +00:00
|
|
|
{
|
|
|
|
auto end = src.second.end();
|
|
|
|
for (auto it = src.second.begin(); it != end;)
|
|
|
|
{
|
|
|
|
while (it != end && *it != '_') ++it;
|
2016-09-01 23:16:03 +00:00
|
|
|
if (it == end) break;
|
2020-11-13 23:24:08 +00:00
|
|
|
if (
|
|
|
|
end - it < placeholderSize ||
|
|
|
|
*(it + 1) != '_' ||
|
|
|
|
*(it + placeholderSize - 2) != '_' ||
|
|
|
|
*(it + placeholderSize - 1) != '_'
|
|
|
|
)
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(
|
|
|
|
CommandLineExecutionError,
|
|
|
|
"Error in binary object file " + src.first + " at position " + to_string(it - src.second.begin()) + "\n" +
|
|
|
|
'"' + string(it, it + min(placeholderSize, static_cast<int>(end - it))) + "\" is not a valid link reference."
|
|
|
|
);
|
2015-09-11 17:35:01 +00:00
|
|
|
|
2020-11-13 23:20:52 +00:00
|
|
|
string foundPlaceholder(it, it + placeholderSize);
|
|
|
|
if (librariesReplacements.count(foundPlaceholder))
|
2015-09-11 17:35:01 +00:00
|
|
|
{
|
2022-03-07 04:25:35 +00:00
|
|
|
string hexStr(util::toHex(librariesReplacements.at(foundPlaceholder).asBytes()));
|
2016-09-01 23:16:03 +00:00
|
|
|
copy(hexStr.begin(), hexStr.end(), it);
|
2015-09-11 17:35:01 +00:00
|
|
|
}
|
|
|
|
else
|
2020-11-13 23:20:52 +00:00
|
|
|
serr() << "Reference \"" << foundPlaceholder << "\" in file \"" << src.first << "\" still unresolved." << endl;
|
2016-09-06 09:12:55 +00:00
|
|
|
it += placeholderSize;
|
2015-09-11 17:35:01 +00:00
|
|
|
}
|
2018-10-04 12:55:02 +00:00
|
|
|
// Remove hints for resolved libraries.
|
2021-06-07 10:33:04 +00:00
|
|
|
for (auto const& library: m_options.linker.libraries)
|
2018-10-04 12:55:02 +00:00
|
|
|
boost::algorithm::erase_all(src.second, "\n" + libraryPlaceholderHint(library.first));
|
|
|
|
while (!src.second.empty() && *prev(src.second.end()) == '\n')
|
2019-02-13 15:56:46 +00:00
|
|
|
src.second.resize(src.second.size() - 1);
|
2015-09-11 17:35:01 +00:00
|
|
|
}
|
2021-12-13 13:52:17 +00:00
|
|
|
m_fileReader.setSourceUnits(move(sourceCodes));
|
2015-09-11 17:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandLineInterface::writeLinkedFiles()
|
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Linker, "");
|
|
|
|
|
2021-12-13 13:52:17 +00:00
|
|
|
for (auto const& src: m_fileReader.sourceUnits())
|
2015-09-11 17:35:01 +00:00
|
|
|
if (src.first == g_stdinFileName)
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << src.second << endl;
|
2015-09-11 17:35:01 +00:00
|
|
|
else
|
2018-10-17 22:48:07 +00:00
|
|
|
{
|
|
|
|
ofstream outFile(src.first);
|
|
|
|
outFile << src.second;
|
|
|
|
if (!outFile)
|
2021-08-15 01:14:38 +00:00
|
|
|
solThrow(CommandLineOutputError, "Could not write to file " + src.first + ". Aborting.");
|
2018-10-17 22:48:07 +00:00
|
|
|
}
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << "Linking completed." << endl;
|
2015-09-11 17:35:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 12:55:02 +00:00
|
|
|
string CommandLineInterface::libraryPlaceholderHint(string const& _libraryName)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
return "// " + evmasm::LinkerObject::libraryPlaceholder(_libraryName) + " -> " + _libraryName;
|
2018-10-04 12:55:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
string CommandLineInterface::objectWithLinkRefsHex(evmasm::LinkerObject const& _obj)
|
2018-10-04 12:55:02 +00:00
|
|
|
{
|
|
|
|
string out = _obj.toHex();
|
|
|
|
if (!_obj.linkReferences.empty())
|
|
|
|
{
|
|
|
|
out += "\n";
|
|
|
|
for (auto const& linkRef: _obj.linkReferences)
|
|
|
|
out += "\n" + libraryPlaceholderHint(linkRef.second);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
void CommandLineInterface::assemble(yul::YulStack::Language _language, yul::YulStack::Machine _targetMachine)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Assembler, "");
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
bool successful = true;
|
2022-03-28 04:27:11 +00:00
|
|
|
map<string, yul::YulStack> yulStacks;
|
2021-12-13 13:52:17 +00:00
|
|
|
for (auto const& src: m_fileReader.sourceUnits())
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2021-08-03 15:11:17 +00:00
|
|
|
// --no-optimize-yul option is not accepted in assembly mode.
|
|
|
|
solAssert(!m_options.optimizer.noOptimizeYul, "");
|
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
auto& stack = yulStacks[src.first] = yul::YulStack(
|
2021-08-03 15:11:17 +00:00
|
|
|
m_options.output.evmVersion,
|
|
|
|
_language,
|
2021-09-17 18:15:19 +00:00
|
|
|
m_options.optimiserSettings(),
|
2021-09-13 12:17:05 +00:00
|
|
|
m_options.output.debugInfoSelection.has_value() ?
|
|
|
|
m_options.output.debugInfoSelection.value() :
|
|
|
|
DebugInfoSelection::Default()
|
2021-08-03 15:11:17 +00:00
|
|
|
);
|
2020-05-08 16:20:14 +00:00
|
|
|
|
2021-08-09 09:36:29 +00:00
|
|
|
if (!stack.parseAndAnalyze(src.first, src.second))
|
|
|
|
successful = false;
|
|
|
|
else
|
|
|
|
stack.optimize();
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2017-10-26 20:56:00 +00:00
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
for (auto const& sourceAndStack: yulStacks)
|
2017-01-26 12:45:23 +00:00
|
|
|
{
|
2017-05-23 16:57:06 +00:00
|
|
|
auto const& stack = sourceAndStack.second;
|
2021-06-29 12:38:59 +00:00
|
|
|
SourceReferenceFormatter formatter(serr(false), stack, coloredOutput(m_options), m_options.formatting.withErrorIds);
|
2017-10-26 20:56:00 +00:00
|
|
|
|
2017-05-23 16:57:06 +00:00
|
|
|
for (auto const& error: stack.errors())
|
2018-09-13 02:36:14 +00:00
|
|
|
{
|
2021-06-10 18:08:53 +00:00
|
|
|
m_hasOutput = true;
|
2020-10-30 21:11:29 +00:00
|
|
|
formatter.printErrorInformation(*error);
|
2018-09-13 02:36:14 +00:00
|
|
|
}
|
2021-06-30 12:48:45 +00:00
|
|
|
if (Error::containsErrors(stack.errors()))
|
2017-01-26 12:45:23 +00:00
|
|
|
successful = false;
|
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2017-05-23 10:13:34 +00:00
|
|
|
if (!successful)
|
2021-08-15 01:14:38 +00:00
|
|
|
{
|
|
|
|
solAssert(m_hasOutput);
|
|
|
|
solThrow(CommandLineExecutionError, "");
|
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2021-12-13 13:52:17 +00:00
|
|
|
for (auto const& src: m_fileReader.sourceUnits())
|
2016-03-01 21:56:39 +00:00
|
|
|
{
|
2017-05-23 16:57:06 +00:00
|
|
|
string machine =
|
2022-03-28 04:27:11 +00:00
|
|
|
_targetMachine == yul::YulStack::Machine::EVM ? "EVM" :
|
2019-12-09 16:36:12 +00:00
|
|
|
"Ewasm";
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << endl << "======= " << src.first << " (" << machine << ") =======" << endl;
|
2019-11-27 18:36:58 +00:00
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
yul::YulStack& stack = yulStacks[src.first];
|
2017-05-29 23:12:38 +00:00
|
|
|
|
2021-09-30 18:38:28 +00:00
|
|
|
if (m_options.compiler.outputs.irOptimized)
|
|
|
|
{
|
|
|
|
// NOTE: This actually outputs unoptimized code when the optimizer is disabled but
|
|
|
|
// 'ir' output in StandardCompiler works the same way.
|
|
|
|
sout() << endl << "Pretty printed source:" << endl;
|
|
|
|
sout() << stack.print() << endl;
|
|
|
|
}
|
2017-05-29 23:12:38 +00:00
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
if (_language != yul::YulStack::Language::Ewasm && _targetMachine == yul::YulStack::Machine::Ewasm)
|
2019-11-27 18:36:58 +00:00
|
|
|
{
|
2022-03-28 04:27:11 +00:00
|
|
|
stack.translate(yul::YulStack::Language::Ewasm);
|
2021-08-09 09:36:29 +00:00
|
|
|
stack.optimize();
|
2019-11-27 18:36:58 +00:00
|
|
|
|
2021-11-03 16:01:48 +00:00
|
|
|
if (m_options.compiler.outputs.ewasmIR)
|
2021-09-30 18:38:28 +00:00
|
|
|
{
|
|
|
|
sout() << endl << "==========================" << endl;
|
|
|
|
sout() << endl << "Translated source:" << endl;
|
|
|
|
sout() << stack.print() << endl;
|
|
|
|
}
|
2019-11-27 18:36:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 11:07:20 +00:00
|
|
|
yul::MachineAssemblyObject object;
|
2021-08-09 09:36:29 +00:00
|
|
|
object = stack.assemble(_targetMachine);
|
|
|
|
object.bytecode->link(m_options.linker.libraries);
|
2017-05-29 23:12:38 +00:00
|
|
|
|
2021-09-30 18:38:28 +00:00
|
|
|
if (m_options.compiler.outputs.binary)
|
|
|
|
{
|
|
|
|
sout() << endl << "Binary representation:" << endl;
|
|
|
|
if (object.bytecode)
|
|
|
|
sout() << object.bytecode->toHex() << endl;
|
|
|
|
else
|
|
|
|
serr() << "No binary representation found." << endl;
|
|
|
|
}
|
2017-05-29 23:12:38 +00:00
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
solAssert(_targetMachine == yul::YulStack::Machine::Ewasm || _targetMachine == yul::YulStack::Machine::EVM, "");
|
2021-09-30 18:38:28 +00:00
|
|
|
if (
|
2022-03-28 04:27:11 +00:00
|
|
|
(_targetMachine == yul::YulStack::Machine::EVM && m_options.compiler.outputs.asm_) ||
|
|
|
|
(_targetMachine == yul::YulStack::Machine::Ewasm && m_options.compiler.outputs.ewasm)
|
2021-09-30 18:38:28 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
sout() << endl << "Text representation:" << endl;
|
|
|
|
if (!object.assembly.empty())
|
|
|
|
sout() << object.assembly << endl;
|
|
|
|
else
|
|
|
|
serr() << "No text representation found." << endl;
|
|
|
|
}
|
2016-03-01 21:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 17:35:01 +00:00
|
|
|
void CommandLineInterface::outputCompilationResults()
|
2015-01-05 14:46:40 +00:00
|
|
|
{
|
2021-09-14 12:09:18 +00:00
|
|
|
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
handleCombinedJSON();
|
|
|
|
|
2015-01-05 14:46:40 +00:00
|
|
|
// do we need AST output?
|
2020-11-20 14:35:53 +00:00
|
|
|
handleAst();
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2020-07-08 20:08:50 +00:00
|
|
|
if (
|
|
|
|
!m_compiler->compilationSuccessful() &&
|
2021-06-07 10:33:04 +00:00
|
|
|
m_options.output.stopAfter == CompilerStack::State::CompilationSuccessful
|
2020-07-08 20:08:50 +00:00
|
|
|
)
|
2019-05-28 15:24:54 +00:00
|
|
|
{
|
|
|
|
serr() << endl << "Compilation halted after AST generation due to errors." << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:44:29 +00:00
|
|
|
vector<string> contracts = m_compiler->contractNames();
|
2014-12-09 12:43:08 +00:00
|
|
|
for (string const& contract: contracts)
|
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
if (needsHumanTargetedStdout(m_options))
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << endl << "======= " << contract << " =======" << endl;
|
2014-12-09 12:43:08 +00:00
|
|
|
|
|
|
|
// do we need EVM assembly?
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.outputs.asm_ || m_options.compiler.outputs.asmJson)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2017-09-11 14:18:56 +00:00
|
|
|
string ret;
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.outputs.asmJson)
|
2021-10-22 02:05:49 +00:00
|
|
|
ret = util::jsonPrint(removeNullMembers(m_compiler->assemblyJSON(contract)), m_options.formatting.json);
|
2017-09-11 14:18:56 +00:00
|
|
|
else
|
2021-12-13 13:52:17 +00:00
|
|
|
ret = m_compiler->assemblyString(contract, m_fileReader.sourceUnits());
|
2017-09-11 14:18:56 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
|
|
|
createFile(m_compiler->filesystemFriendlyName(contract) + (m_options.compiler.outputs.asmJson ? "_evm.json" : ".evm"), ret);
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
2018-09-13 02:36:14 +00:00
|
|
|
sout() << "EVM assembly:" << endl << ret << endl;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
if (m_options.compiler.estimateGas)
|
2015-05-22 12:19:58 +00:00
|
|
|
handleGasEstimation(contract);
|
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
handleBytecode(contract);
|
2019-03-04 22:26:46 +00:00
|
|
|
handleIR(contract);
|
2020-02-26 14:50:34 +00:00
|
|
|
handleIROptimized(contract);
|
2019-12-09 16:36:12 +00:00
|
|
|
handleEwasm(contract);
|
2015-05-04 14:21:44 +00:00
|
|
|
handleSignatureHashes(contract);
|
2017-05-19 15:10:32 +00:00
|
|
|
handleMetadata(contract);
|
2017-05-06 17:02:56 +00:00
|
|
|
handleABI(contract);
|
2020-03-24 12:22:25 +00:00
|
|
|
handleStorageLayout(contract);
|
2017-07-27 10:28:04 +00:00
|
|
|
handleNatspec(true, contract);
|
|
|
|
handleNatspec(false, contract);
|
2014-12-09 12:43:08 +00:00
|
|
|
} // end of contracts iteration
|
2018-09-13 02:36:14 +00:00
|
|
|
|
2021-06-10 18:08:53 +00:00
|
|
|
if (!m_hasOutput)
|
2018-09-13 02:36:14 +00:00
|
|
|
{
|
2021-06-07 10:33:04 +00:00
|
|
|
if (!m_options.output.dir.empty())
|
|
|
|
sout() << "Compiler run successful. Artifact(s) can be found in directory " << m_options.output.dir << "." << endl;
|
2018-09-13 02:36:14 +00:00
|
|
|
else
|
|
|
|
serr() << "Compiler run successful, no output requested." << endl;
|
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|