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>
|
|
|
|
* @date 2014
|
2014-12-09 16:39:34 +00:00
|
|
|
* Solidity command line interface.
|
2014-12-09 12:43:08 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
#include <solc/CommandLineParser.h>
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
2019-09-18 14:44:36 +00:00
|
|
|
#include <libsolidity/interface/DebugSettings.h>
|
2021-04-06 14:12:06 +00:00
|
|
|
#include <libsolidity/interface/FileReader.h>
|
2022-03-28 04:27:11 +00:00
|
|
|
#include <libyul/YulStack.h>
|
2017-01-26 12:47:57 +00:00
|
|
|
|
2021-06-10 18:08:53 +00:00
|
|
|
#include <iostream>
|
2017-01-26 12:47:57 +00:00
|
|
|
#include <memory>
|
2021-06-07 10:33:04 +00:00
|
|
|
#include <string>
|
2017-01-26 12:47:57 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
class CommandLineInterface
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-06-10 18:08:53 +00:00
|
|
|
explicit CommandLineInterface(
|
2021-06-16 19:12:27 +00:00
|
|
|
std::istream& _sin,
|
2021-06-10 18:08:53 +00:00
|
|
|
std::ostream& _sout,
|
|
|
|
std::ostream& _serr,
|
|
|
|
CommandLineOptions const& _options = CommandLineOptions{}
|
|
|
|
):
|
2021-06-16 19:12:27 +00:00
|
|
|
m_sin(_sin),
|
2021-06-10 18:08:53 +00:00
|
|
|
m_sout(_sout),
|
|
|
|
m_serr(_serr),
|
2021-06-07 10:33:04 +00:00
|
|
|
m_options(_options)
|
|
|
|
{}
|
|
|
|
|
2021-10-12 12:16:29 +00:00
|
|
|
/// Parses command-line arguments, executes the requested operation and handles validation and
|
|
|
|
/// execution errors.
|
|
|
|
/// @returns false if it catches a @p CommandLineValidationError or if the application is
|
|
|
|
/// expected to exit with a non-zero exit code despite there being no error.
|
|
|
|
bool run(int _argc, char const* const* _argv);
|
|
|
|
|
|
|
|
/// Parses command line arguments and stores the result in @p m_options.
|
|
|
|
/// @throws CommandLineValidationError if command-line arguments are invalid.
|
|
|
|
/// @returns false if the application is expected to exit with a non-zero exit code despite
|
|
|
|
/// there being no error.
|
2021-06-07 10:33:04 +00:00
|
|
|
bool parseArguments(int _argc, char const* const* _argv);
|
2021-10-12 12:16:29 +00:00
|
|
|
|
|
|
|
/// Reads the content of all input files and initializes the file reader.
|
|
|
|
/// @throws CommandLineValidationError if it fails to read the input files (invalid paths,
|
|
|
|
/// non-existent files, not enough or too many input files, etc.).
|
|
|
|
void readInputFiles();
|
|
|
|
|
|
|
|
/// Executes the requested operation (compilation, assembling, standard JSON, etc.) and prints
|
|
|
|
/// results to the terminal.
|
|
|
|
/// @throws CommandLineExecutionError if execution fails due to errors in the input files.
|
|
|
|
/// @throws CommandLineOutputError if creating output files or writing to them fails.
|
|
|
|
void processInput();
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
CommandLineOptions const& options() const { return m_options; }
|
|
|
|
FileReader const& fileReader() const { return m_fileReader; }
|
2021-06-13 13:53:16 +00:00
|
|
|
std::optional<std::string> const& standardJsonInput() const { return m_standardJsonInput; }
|
2021-06-07 10:33:04 +00:00
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
private:
|
2021-10-11 11:03:51 +00:00
|
|
|
void printVersion();
|
|
|
|
void printLicense();
|
2021-10-12 12:16:29 +00:00
|
|
|
void compile();
|
2021-12-13 13:53:40 +00:00
|
|
|
void serveLSP();
|
2021-10-12 12:16:29 +00:00
|
|
|
void link();
|
2015-09-11 17:35:01 +00:00
|
|
|
void writeLinkedFiles();
|
2018-10-04 12:55:02 +00:00
|
|
|
/// @returns the ``// <identifier> -> name`` hint for library placeholders.
|
|
|
|
static std::string libraryPlaceholderHint(std::string const& _libraryName);
|
|
|
|
/// @returns the full object with library placeholder hints in hex.
|
2019-12-11 16:31:36 +00:00
|
|
|
static std::string objectWithLinkRefsHex(evmasm::LinkerObject const& _obj);
|
2015-09-11 17:35:01 +00:00
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
void assemble(yul::YulStack::Language _language, yul::YulStack::Machine _targetMachine);
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2015-09-11 17:35:01 +00:00
|
|
|
void outputCompilationResults();
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
void handleCombinedJSON();
|
2020-11-20 14:35:53 +00:00
|
|
|
void handleAst();
|
2014-12-09 19:29:29 +00:00
|
|
|
void handleBinary(std::string const& _contract);
|
|
|
|
void handleOpcode(std::string const& _contract);
|
2019-03-04 22:26:46 +00:00
|
|
|
void handleIR(std::string const& _contract);
|
2020-02-26 14:50:34 +00:00
|
|
|
void handleIROptimized(std::string const& _contract);
|
2019-12-09 16:36:12 +00:00
|
|
|
void handleEwasm(std::string const& _contract);
|
2014-12-09 19:29:29 +00:00
|
|
|
void handleBytecode(std::string const& _contract);
|
2015-05-04 14:21:44 +00:00
|
|
|
void handleSignatureHashes(std::string const& _contract);
|
2017-05-19 15:10:32 +00:00
|
|
|
void handleMetadata(std::string const& _contract);
|
2017-05-06 17:02:56 +00:00
|
|
|
void handleABI(std::string const& _contract);
|
2017-07-27 10:28:04 +00:00
|
|
|
void handleNatspec(bool _natspecDev, std::string const& _contract);
|
2015-05-22 12:19:58 +00:00
|
|
|
void handleGasEstimation(std::string const& _contract);
|
2020-03-24 12:22:25 +00:00
|
|
|
void handleStorageLayout(std::string const& _contract);
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2019-09-11 19:16:35 +00:00
|
|
|
/// Tries to read @ m_sourceCodes as a JSONs holding ASTs
|
|
|
|
/// such that they can be imported into the compiler (importASTs())
|
2021-09-30 14:17:36 +00:00
|
|
|
/// (produced by --combined-json ast <file.sol>
|
2019-09-11 19:16:35 +00:00
|
|
|
/// or standard-json output
|
|
|
|
std::map<std::string, Json::Value> parseAstFromInput();
|
|
|
|
|
2015-07-20 16:14:25 +00:00
|
|
|
/// Create a file in the given directory
|
|
|
|
/// @arg _fileName the name of the file
|
|
|
|
/// @arg _data to be written
|
2015-07-21 13:29:15 +00:00
|
|
|
void createFile(std::string const& _fileName, std::string const& _data);
|
2015-07-20 16:14:25 +00:00
|
|
|
|
2017-05-22 20:39:38 +00:00
|
|
|
/// Create a json file in the given directory
|
|
|
|
/// @arg _fileName the name of the file (the extension will be replaced with .json)
|
2017-07-03 23:46:30 +00:00
|
|
|
/// @arg _json json string to be written
|
|
|
|
void createJson(std::string const& _fileName, std::string const& _json);
|
2017-05-22 20:39:38 +00:00
|
|
|
|
2021-06-10 18:08:53 +00:00
|
|
|
/// Returns the stream that should receive normal output. Sets m_hasOutput to true if the
|
|
|
|
/// stream has ever been used unless @arg _markAsUsed is set to false.
|
|
|
|
std::ostream& sout(bool _markAsUsed = true);
|
|
|
|
|
|
|
|
/// Returns the stream that should receive error output. Sets m_hasOutput to true if the
|
|
|
|
/// stream has ever been used unless @arg _markAsUsed is set to false.
|
|
|
|
std::ostream& serr(bool _markAsUsed = true);
|
|
|
|
|
2021-06-16 19:12:27 +00:00
|
|
|
std::istream& m_sin;
|
2021-06-10 18:08:53 +00:00
|
|
|
std::ostream& m_sout;
|
|
|
|
std::ostream& m_serr;
|
|
|
|
bool m_hasOutput = false;
|
2021-04-06 14:12:06 +00:00
|
|
|
FileReader m_fileReader;
|
2021-06-13 13:53:16 +00:00
|
|
|
std::optional<std::string> m_standardJsonInput;
|
2019-12-11 16:31:36 +00:00
|
|
|
std::unique_ptr<frontend::CompilerStack> m_compiler;
|
2021-06-07 10:33:04 +00:00
|
|
|
CommandLineOptions m_options;
|
2014-12-09 12:43:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|