2021-06-07 10:31:05 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
/**
|
2021-06-07 10:33:04 +00:00
|
|
|
* Validates and parses command-line options into an internal representation.
|
2021-06-07 10:31:05 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
|
|
|
#include <libsolidity/interface/DebugSettings.h>
|
|
|
|
#include <libsolidity/interface/FileReader.h>
|
|
|
|
#include <libsolidity/interface/ImportRemapper.h>
|
2021-09-13 12:17:05 +00:00
|
|
|
|
2021-06-07 10:31:05 +00:00
|
|
|
#include <libyul/AssemblyStack.h>
|
2021-09-13 12:17:05 +00:00
|
|
|
|
|
|
|
#include <liblangutil/DebugInfoSelection.h>
|
2021-06-07 10:31:05 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2021-09-13 12:17:05 +00:00
|
|
|
|
2021-07-07 11:18:24 +00:00
|
|
|
#include <libsolutil/JSON.h>
|
2021-06-07 10:31:05 +00:00
|
|
|
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
#include <map>
|
2021-06-07 10:31:05 +00:00
|
|
|
#include <memory>
|
2021-06-07 10:33:04 +00:00
|
|
|
#include <optional>
|
2021-06-10 15:45:09 +00:00
|
|
|
#include <ostream>
|
2021-06-07 10:33:04 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-06-07 10:31:05 +00:00
|
|
|
|
|
|
|
namespace solidity::frontend
|
|
|
|
{
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
enum class InputMode
|
|
|
|
{
|
2021-10-11 11:03:51 +00:00
|
|
|
Help,
|
|
|
|
License,
|
|
|
|
Version,
|
2021-06-07 10:33:04 +00:00
|
|
|
Compiler,
|
|
|
|
CompilerWithASTImport,
|
|
|
|
StandardJson,
|
|
|
|
Linker,
|
|
|
|
Assembler,
|
2021-12-13 13:53:40 +00:00
|
|
|
LanguageServer
|
2021-06-07 10:33:04 +00:00
|
|
|
};
|
2021-06-07 10:31:05 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
struct CompilerOutputs
|
2021-06-07 10:31:05 +00:00
|
|
|
{
|
2021-06-10 14:33:08 +00:00
|
|
|
bool operator!=(CompilerOutputs const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
bool operator==(CompilerOutputs const& _other) const noexcept;
|
2021-06-10 13:46:46 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& _out, CompilerOutputs const& _requests);
|
2021-06-10 14:33:08 +00:00
|
|
|
|
2021-09-30 13:51:24 +00:00
|
|
|
static std::string const& componentName(bool CompilerOutputs::* _component);
|
|
|
|
static auto const& componentMap()
|
|
|
|
{
|
|
|
|
static std::map<std::string, bool CompilerOutputs::*> const components = {
|
|
|
|
{"ast-compact-json", &CompilerOutputs::astCompactJson},
|
|
|
|
{"asm", &CompilerOutputs::asm_},
|
|
|
|
{"asm-json", &CompilerOutputs::asmJson},
|
|
|
|
{"opcodes", &CompilerOutputs::opcodes},
|
|
|
|
{"bin", &CompilerOutputs::binary},
|
|
|
|
{"bin-runtime", &CompilerOutputs::binaryRuntime},
|
|
|
|
{"abi", &CompilerOutputs::abi},
|
|
|
|
{"ir", &CompilerOutputs::ir},
|
|
|
|
{"ir-optimized", &CompilerOutputs::irOptimized},
|
|
|
|
{"ewasm", &CompilerOutputs::ewasm},
|
2021-11-03 16:01:48 +00:00
|
|
|
{"ewasm-ir", &CompilerOutputs::ewasmIR},
|
2021-09-30 13:51:24 +00:00
|
|
|
{"hashes", &CompilerOutputs::signatureHashes},
|
|
|
|
{"userdoc", &CompilerOutputs::natspecUser},
|
|
|
|
{"devdoc", &CompilerOutputs::natspecDev},
|
|
|
|
{"metadata", &CompilerOutputs::metadata},
|
|
|
|
{"storage-layout", &CompilerOutputs::storageLayout},
|
|
|
|
};
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
bool astCompactJson = false;
|
|
|
|
bool asm_ = false;
|
|
|
|
bool asmJson = false;
|
|
|
|
bool opcodes = false;
|
|
|
|
bool binary = false;
|
|
|
|
bool binaryRuntime = false;
|
|
|
|
bool abi = false;
|
|
|
|
bool ir = false;
|
|
|
|
bool irOptimized = false;
|
|
|
|
bool ewasm = false;
|
2021-11-03 16:01:48 +00:00
|
|
|
bool ewasmIR = false;
|
2021-06-07 10:33:04 +00:00
|
|
|
bool signatureHashes = false;
|
|
|
|
bool natspecUser = false;
|
|
|
|
bool natspecDev = false;
|
|
|
|
bool metadata = false;
|
|
|
|
bool storageLayout = false;
|
|
|
|
};
|
2021-06-07 10:31:05 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
struct CombinedJsonRequests
|
|
|
|
{
|
2021-06-10 14:33:08 +00:00
|
|
|
bool operator!=(CombinedJsonRequests const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
bool operator==(CombinedJsonRequests const& _other) const noexcept;
|
2021-06-10 13:46:46 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& _out, CombinedJsonRequests const& _requests);
|
2021-06-10 14:33:08 +00:00
|
|
|
|
2021-09-30 14:33:00 +00:00
|
|
|
static std::string const& componentName(bool CombinedJsonRequests::* _component);
|
|
|
|
static auto const& componentMap()
|
|
|
|
{
|
|
|
|
static std::map<std::string, bool CombinedJsonRequests::*> const components = {
|
|
|
|
{"abi", &CombinedJsonRequests::abi},
|
|
|
|
{"metadata", &CombinedJsonRequests::metadata},
|
|
|
|
{"bin", &CombinedJsonRequests::binary},
|
|
|
|
{"bin-runtime", &CombinedJsonRequests::binaryRuntime},
|
|
|
|
{"opcodes", &CombinedJsonRequests::opcodes},
|
|
|
|
{"asm", &CombinedJsonRequests::asm_},
|
|
|
|
{"storage-layout", &CombinedJsonRequests::storageLayout},
|
|
|
|
{"generated-sources", &CombinedJsonRequests::generatedSources},
|
|
|
|
{"generated-sources-runtime", &CombinedJsonRequests::generatedSourcesRuntime},
|
|
|
|
{"srcmap", &CombinedJsonRequests::srcMap},
|
|
|
|
{"srcmap-runtime", &CombinedJsonRequests::srcMapRuntime},
|
|
|
|
{"function-debug", &CombinedJsonRequests::funDebug},
|
|
|
|
{"function-debug-runtime", &CombinedJsonRequests::funDebugRuntime},
|
|
|
|
{"hashes", &CombinedJsonRequests::signatureHashes},
|
|
|
|
{"devdoc", &CombinedJsonRequests::natspecDev},
|
|
|
|
{"userdoc", &CombinedJsonRequests::natspecUser},
|
|
|
|
{"ast", &CombinedJsonRequests::ast},
|
|
|
|
};
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
bool abi = false;
|
|
|
|
bool metadata = false;
|
|
|
|
bool binary = false;
|
|
|
|
bool binaryRuntime = false;
|
|
|
|
bool opcodes = false;
|
|
|
|
bool asm_ = false;
|
|
|
|
bool storageLayout = false;
|
|
|
|
bool generatedSources = false;
|
|
|
|
bool generatedSourcesRuntime = false;
|
|
|
|
bool srcMap = false;
|
|
|
|
bool srcMapRuntime = false;
|
|
|
|
bool funDebug = false;
|
|
|
|
bool funDebugRuntime = false;
|
|
|
|
bool signatureHashes = false;
|
|
|
|
bool natspecDev = false;
|
|
|
|
bool natspecUser = false;
|
|
|
|
bool ast = false;
|
|
|
|
};
|
2021-06-07 10:31:05 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
struct CommandLineOptions
|
|
|
|
{
|
2021-06-10 14:33:08 +00:00
|
|
|
bool operator==(CommandLineOptions const& _other) const noexcept;
|
|
|
|
bool operator!=(CommandLineOptions const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
|
2021-08-03 15:11:17 +00:00
|
|
|
OptimiserSettings optimiserSettings() const;
|
2021-07-07 11:18:24 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
InputMode mode = InputMode::Compiler;
|
|
|
|
std::set<boost::filesystem::path> paths;
|
|
|
|
std::vector<ImportRemapper::Remapping> remappings;
|
|
|
|
bool addStdin = false;
|
|
|
|
boost::filesystem::path basePath = "";
|
2021-08-20 17:17:44 +00:00
|
|
|
std::vector<boost::filesystem::path> includePaths;
|
2021-06-07 10:33:04 +00:00
|
|
|
FileReader::FileSystemPathSet allowedDirectories;
|
|
|
|
bool ignoreMissingFiles = false;
|
|
|
|
bool errorRecovery = false;
|
|
|
|
} input;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
boost::filesystem::path dir;
|
|
|
|
bool overwriteFiles = false;
|
|
|
|
langutil::EVMVersion evmVersion;
|
|
|
|
bool experimentalViaIR = false;
|
|
|
|
RevertStrings revertStrings = RevertStrings::Default;
|
2021-09-13 12:17:05 +00:00
|
|
|
std::optional<langutil::DebugInfoSelection> debugInfoSelection;
|
2021-06-07 10:33:04 +00:00
|
|
|
CompilerStack::State stopAfter = CompilerStack::State::CompilationSuccessful;
|
|
|
|
} output;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
yul::AssemblyStack::Machine targetMachine = yul::AssemblyStack::Machine::EVM;
|
|
|
|
yul::AssemblyStack::Language inputLanguage = yul::AssemblyStack::Language::StrictAssembly;
|
|
|
|
} assembly;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
std::map<std::string, util::h160> libraries; // library name -> address
|
|
|
|
} linker;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2021-07-07 11:18:24 +00:00
|
|
|
util::JsonFormat json;
|
2021-06-07 10:33:04 +00:00
|
|
|
std::optional<bool> coloredOutput;
|
|
|
|
bool withErrorIds = false;
|
|
|
|
} formatting;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
CompilerOutputs outputs;
|
|
|
|
bool estimateGas = false;
|
|
|
|
std::optional<CombinedJsonRequests> combinedJsonRequests;
|
|
|
|
} compiler;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
CompilerStack::MetadataHash hash = CompilerStack::MetadataHash::IPFS;
|
|
|
|
bool literalSources = false;
|
|
|
|
} metadata;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
bool enabled = false;
|
2021-07-28 15:53:19 +00:00
|
|
|
std::optional<unsigned> expectedExecutionsPerDeployment;
|
2021-06-07 10:33:04 +00:00
|
|
|
bool noOptimizeYul = false;
|
|
|
|
std::optional<std::string> yulSteps;
|
|
|
|
} optimizer;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
bool initialize = false;
|
|
|
|
ModelCheckerSettings settings;
|
|
|
|
} modelChecker;
|
|
|
|
};
|
2021-06-07 10:31:05 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
/// Parses the command-line arguments and produces a filled-out CommandLineOptions structure.
|
2021-10-11 11:48:03 +00:00
|
|
|
/// Validates provided values and reports errors by throwing @p CommandLineValidationErrors.
|
2021-06-07 10:33:04 +00:00
|
|
|
class CommandLineParser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Parses the command-line arguments and fills out the internal CommandLineOptions structure.
|
2021-10-11 11:48:03 +00:00
|
|
|
/// @throws CommandLineValidationError if the arguments cannot be properly parsed or are invalid.
|
|
|
|
/// When an exception is thrown, the @p CommandLineOptions may be only partially filled out.
|
|
|
|
void parse(int _argc, char const* const* _argv);
|
2021-06-07 10:33:04 +00:00
|
|
|
|
|
|
|
CommandLineOptions const& options() const { return m_options; }
|
|
|
|
|
2021-10-11 11:03:51 +00:00
|
|
|
static void printHelp(std::ostream& _out) { _out << optionsDescription(); }
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
private:
|
2021-09-16 12:15:17 +00:00
|
|
|
/// @returns a specification of all named command-line options accepted by the compiler.
|
|
|
|
/// The object can be used to parse command-line arguments or to generate the help screen.
|
|
|
|
static boost::program_options::options_description optionsDescription();
|
|
|
|
|
|
|
|
/// @returns a specification of all positional command-line arguments accepted by the compiler.
|
|
|
|
/// The object can be used to parse command-line arguments or to generate the help screen.
|
|
|
|
static boost::program_options::positional_options_description positionalOptionsDescription();
|
|
|
|
|
|
|
|
/// Uses boost::program_options to parse the command-line arguments and leaves the result in @a m_args.
|
|
|
|
/// Also handles the arguments that result in information being printed followed by immediate exit.
|
|
|
|
/// @returns false if parsing fails due to syntactical errors or the arguments not matching the description.
|
2021-10-11 11:48:03 +00:00
|
|
|
void parseArgs(int _argc, char const* const* _argv);
|
2021-09-16 12:15:17 +00:00
|
|
|
|
|
|
|
/// Validates parsed arguments stored in @a m_args and fills out the internal CommandLineOptions
|
|
|
|
/// structure.
|
2021-10-11 11:48:03 +00:00
|
|
|
/// @throws CommandLineValidationError in case of validation errors.
|
|
|
|
void processArgs();
|
2021-09-16 12:15:17 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
/// Parses the value supplied to --combined-json.
|
2021-10-11 11:48:03 +00:00
|
|
|
/// @throws CommandLineValidationError in case of validation errors.
|
|
|
|
void parseCombinedJsonOption();
|
2021-06-07 10:33:04 +00:00
|
|
|
|
2021-10-11 11:48:03 +00:00
|
|
|
/// Parses the names of the input files, remappings. Does not check if the files actually exist.
|
|
|
|
/// @throws CommandLineValidationError in case of validation errors.
|
|
|
|
void parseInputPathsAndRemappings();
|
2021-06-07 10:33:04 +00:00
|
|
|
|
|
|
|
/// Tries to read from the file @a _input or interprets @a _input literally if that fails.
|
2021-10-11 11:48:03 +00:00
|
|
|
/// It then tries to parse the contents and appends to @a m_options.libraries.
|
|
|
|
/// @throws CommandLineValidationError in case of validation errors.
|
|
|
|
void parseLibraryOption(std::string const& _input);
|
2021-06-07 10:31:05 +00:00
|
|
|
|
2021-10-11 11:48:03 +00:00
|
|
|
void parseOutputSelection();
|
2021-10-04 14:35:59 +00:00
|
|
|
|
2021-10-11 11:48:03 +00:00
|
|
|
void checkMutuallyExclusive(std::vector<std::string> const& _optionNames);
|
2021-06-07 10:31:05 +00:00
|
|
|
size_t countEnabledOptions(std::vector<std::string> const& _optionNames) const;
|
|
|
|
static std::string joinOptionNames(std::vector<std::string> const& _optionNames, std::string _separator = ", ");
|
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
CommandLineOptions m_options;
|
2021-06-07 10:31:05 +00:00
|
|
|
|
2021-06-07 10:33:04 +00:00
|
|
|
/// Map of command-line arguments produced by boost::program_options.
|
|
|
|
/// Serves as input for filling out m_options.
|
2021-06-07 10:31:05 +00:00
|
|
|
boost::program_options::variables_map m_args;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|