2014-12-09 12:43:08 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum 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.
|
|
|
|
|
|
|
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
*/
|
2014-12-09 16:39:34 +00:00
|
|
|
#include "CommandLineInterface.h"
|
2014-12-09 12:43:08 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2015-04-23 12:40:42 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2014-12-09 12:43:08 +00:00
|
|
|
|
|
|
|
#include "BuildInfo.h"
|
|
|
|
#include <libdevcore/Common.h>
|
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
#include <libdevcore/CommonIO.h>
|
|
|
|
#include <libevmcore/Instruction.h>
|
2015-05-22 12:19:58 +00:00
|
|
|
#include <libevmcore/Params.h>
|
2015-07-08 17:32:43 +00:00
|
|
|
#include <libsolidity/Version.h>
|
2014-12-09 12:43:08 +00:00
|
|
|
#include <libsolidity/Scanner.h>
|
|
|
|
#include <libsolidity/Parser.h>
|
|
|
|
#include <libsolidity/ASTPrinter.h>
|
2015-01-05 14:46:40 +00:00
|
|
|
#include <libsolidity/ASTJsonConverter.h>
|
2014-12-09 12:43:08 +00:00
|
|
|
#include <libsolidity/NameAndTypeResolver.h>
|
|
|
|
#include <libsolidity/Exceptions.h>
|
|
|
|
#include <libsolidity/CompilerStack.h>
|
|
|
|
#include <libsolidity/SourceReferenceFormatter.h>
|
2015-05-22 08:48:54 +00:00
|
|
|
#include <libsolidity/GasEstimator.h>
|
2014-12-09 12:43:08 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
2015-08-10 10:49:28 +00:00
|
|
|
static string const g_argAbiStr = "abi";
|
2015-08-10 10:51:23 +00:00
|
|
|
static string const g_argSolInterfaceStr = "interface";
|
2015-05-04 14:21:44 +00:00
|
|
|
static string const g_argSignatureHashes = "hashes";
|
2015-05-22 12:19:58 +00:00
|
|
|
static string const g_argGas = "gas";
|
2015-05-04 14:50:30 +00:00
|
|
|
static string const g_argAsmStr = "asm";
|
|
|
|
static string const g_argAsmJsonStr = "asm-json";
|
|
|
|
static string const g_argAstStr = "ast";
|
|
|
|
static string const g_argAstJson = "ast-json";
|
2015-08-10 10:49:28 +00:00
|
|
|
static string const g_argBinaryStr = "bin";
|
|
|
|
static string const g_argCloneBinaryStr = "clone-bin";
|
2015-05-04 14:50:30 +00:00
|
|
|
static string const g_argOpcodesStr = "opcodes";
|
2015-08-10 10:49:28 +00:00
|
|
|
static string const g_argNatspecDevStr = "devdoc";
|
|
|
|
static string const g_argNatspecUserStr = "userdoc";
|
2015-05-04 14:50:30 +00:00
|
|
|
static string const g_argAddStandard = "add-std";
|
2014-12-16 22:55:38 +00:00
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
/// Possible arguments to for --combined-json
|
|
|
|
static set<string> const g_combinedJsonArgs{
|
2015-08-10 10:49:28 +00:00
|
|
|
"bin",
|
|
|
|
"clone-bin",
|
2015-04-23 12:40:42 +00:00
|
|
|
"opcodes",
|
2015-08-10 10:49:28 +00:00
|
|
|
"abi",
|
|
|
|
"interface",
|
2015-04-23 12:40:42 +00:00
|
|
|
"asm",
|
|
|
|
"ast",
|
2015-08-10 10:49:28 +00:00
|
|
|
"userdoc",
|
|
|
|
"devdoc"
|
2015-04-23 12:40:42 +00:00
|
|
|
};
|
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
static void version()
|
|
|
|
{
|
2015-07-08 17:32:43 +00:00
|
|
|
cout <<
|
|
|
|
"solc, the solidity compiler commandline interface" <<
|
|
|
|
endl <<
|
|
|
|
"Version: " <<
|
|
|
|
dev::solidity::VersionString <<
|
|
|
|
endl;
|
2014-12-09 12:43:08 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
static inline bool humanTargetedStdout(po::variables_map const& _args, string const& _name)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
return _args.count(_name) && !(_args.count("output-dir"));
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
static bool needsHumanTargetedStdout(po::variables_map const& _args)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-01-05 14:46:40 +00:00
|
|
|
|
2015-01-09 07:05:52 +00:00
|
|
|
return
|
2015-05-22 12:19:58 +00:00
|
|
|
_args.count(g_argGas) ||
|
2015-04-23 12:40:42 +00:00
|
|
|
humanTargetedStdout(_args, g_argAbiStr) ||
|
2015-08-10 10:51:23 +00:00
|
|
|
humanTargetedStdout(_args, g_argSolInterfaceStr) ||
|
2015-05-04 14:21:44 +00:00
|
|
|
humanTargetedStdout(_args, g_argSignatureHashes) ||
|
2015-04-23 12:40:42 +00:00
|
|
|
humanTargetedStdout(_args, g_argNatspecUserStr) ||
|
|
|
|
humanTargetedStdout(_args, g_argAstJson) ||
|
|
|
|
humanTargetedStdout(_args, g_argNatspecDevStr) ||
|
|
|
|
humanTargetedStdout(_args, g_argAsmStr) ||
|
|
|
|
humanTargetedStdout(_args, g_argAsmJsonStr) ||
|
|
|
|
humanTargetedStdout(_args, g_argOpcodesStr) ||
|
2015-07-31 17:23:31 +00:00
|
|
|
humanTargetedStdout(_args, g_argBinaryStr) ||
|
|
|
|
humanTargetedStdout(_args, g_argCloneBinaryStr);
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
void CommandLineInterface::handleBinary(string const& _contract)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-07-31 17:23:31 +00:00
|
|
|
if (m_args.count(g_argBinaryStr))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
if (m_args.count("output-dir"))
|
2015-08-19 23:09:39 +00:00
|
|
|
createFile(_contract + ".bin", toHex(m_compiler->getBytecode(_contract)));
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Binary: " << endl;
|
|
|
|
cout << toHex(m_compiler->getBytecode(_contract)) << endl;
|
|
|
|
}
|
2014-12-09 19:29:29 +00:00
|
|
|
}
|
2015-07-31 17:23:31 +00:00
|
|
|
if (m_args.count(g_argCloneBinaryStr))
|
2014-12-09 19:29:29 +00:00
|
|
|
{
|
2015-08-10 11:01:28 +00:00
|
|
|
if (m_args.count("output-dir"))
|
2015-08-19 23:09:39 +00:00
|
|
|
createFile(_contract + ".clone_bin", toHex(m_compiler->getCloneBytecode(_contract)));
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Clone Binary: " << endl;
|
|
|
|
cout << toHex(m_compiler->getCloneBytecode(_contract)) << endl;
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
if (m_args.count("output-dir"))
|
2015-08-19 23:09:39 +00:00
|
|
|
createFile(_contract + ".opcode", eth::disassemble(m_compiler->getBytecode(_contract)));
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
2014-12-09 19:29:29 +00:00
|
|
|
{
|
|
|
|
cout << "Opcodes: " << endl;
|
2015-01-29 00:29:43 +00:00
|
|
|
cout << eth::disassemble(m_compiler->getBytecode(_contract));
|
2014-12-09 19:29:29 +00:00
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
void CommandLineInterface::handleBytecode(string const& _contract)
|
|
|
|
{
|
2014-12-16 22:55:38 +00:00
|
|
|
if (m_args.count(g_argOpcodesStr))
|
2014-12-09 19:29:29 +00:00
|
|
|
handleOpcode(_contract);
|
2015-07-31 17:23:31 +00:00
|
|
|
if (m_args.count(g_argBinaryStr) || m_args.count(g_argCloneBinaryStr))
|
2014-12-09 19:29:29 +00:00
|
|
|
handleBinary(_contract);
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:21:44 +00:00
|
|
|
void CommandLineInterface::handleSignatureHashes(string const& _contract)
|
|
|
|
{
|
2015-05-05 15:08:30 +00:00
|
|
|
if (!m_args.count(g_argSignatureHashes))
|
|
|
|
return;
|
|
|
|
|
2015-05-04 14:21:44 +00:00
|
|
|
string out;
|
|
|
|
for (auto const& it: m_compiler->getContractDefinition(_contract).getInterfaceFunctions())
|
|
|
|
out += toHex(it.first.ref()) + ": " + it.second->externalSignature() + "\n";
|
|
|
|
|
2015-08-10 10:40:02 +00:00
|
|
|
if (m_args.count("output-dir"))
|
2015-08-19 23:09:39 +00:00
|
|
|
createFile(_contract + ".signatures", out);
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
|
|
|
cout << "Function signatures: " << endl << out;
|
2015-05-04 14:21:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:05:52 +00:00
|
|
|
void CommandLineInterface::handleMeta(DocumentationType _type, string const& _contract)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
|
|
|
std::string argName;
|
|
|
|
std::string suffix;
|
|
|
|
std::string title;
|
|
|
|
switch(_type)
|
|
|
|
{
|
2015-02-09 13:12:36 +00:00
|
|
|
case DocumentationType::ABIInterface:
|
2014-12-16 22:55:38 +00:00
|
|
|
argName = g_argAbiStr;
|
2014-12-09 12:43:08 +00:00
|
|
|
suffix = ".abi";
|
2015-01-09 07:09:30 +00:00
|
|
|
title = "Contract JSON ABI";
|
2014-12-09 12:43:08 +00:00
|
|
|
break;
|
2015-02-09 13:12:36 +00:00
|
|
|
case DocumentationType::ABISolidityInterface:
|
2015-08-10 10:51:23 +00:00
|
|
|
argName = g_argSolInterfaceStr;
|
2015-08-10 10:49:28 +00:00
|
|
|
suffix = "_interface.sol";
|
2015-01-09 07:05:52 +00:00
|
|
|
title = "Contract Solidity ABI";
|
|
|
|
break;
|
2015-02-09 13:12:36 +00:00
|
|
|
case DocumentationType::NatspecUser:
|
2015-01-09 07:05:52 +00:00
|
|
|
argName = g_argNatspecUserStr;
|
2014-12-09 12:43:08 +00:00
|
|
|
suffix = ".docuser";
|
|
|
|
title = "User Documentation";
|
|
|
|
break;
|
2015-02-09 13:12:36 +00:00
|
|
|
case DocumentationType::NatspecDev:
|
2014-12-16 22:55:38 +00:00
|
|
|
argName = g_argNatspecDevStr;
|
2014-12-09 12:43:08 +00:00
|
|
|
suffix = ".docdev";
|
|
|
|
title = "Developer Documentation";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// should never happen
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_args.count(argName))
|
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
if (m_args.count("output-dir"))
|
2015-08-19 23:09:39 +00:00
|
|
|
createFile(_contract + suffix, m_compiler->getMetadata(_contract, _type));
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << title << endl;
|
|
|
|
cout << m_compiler->getMetadata(_contract, _type) << endl;
|
|
|
|
}
|
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 12:19:58 +00:00
|
|
|
void CommandLineInterface::handleGasEstimation(string const& _contract)
|
|
|
|
{
|
|
|
|
using Gas = GasEstimator::GasConsumption;
|
|
|
|
if (!m_compiler->getAssemblyItems(_contract) && !m_compiler->getRuntimeAssemblyItems(_contract))
|
|
|
|
return;
|
|
|
|
cout << "Gas estimation:" << endl;
|
|
|
|
if (eth::AssemblyItems const* items = m_compiler->getAssemblyItems(_contract))
|
|
|
|
{
|
|
|
|
Gas gas = GasEstimator::functionalEstimation(*items);
|
|
|
|
u256 bytecodeSize(m_compiler->getRuntimeBytecode(_contract).size());
|
2015-05-26 09:27:59 +00:00
|
|
|
cout << "construction:" << endl;
|
|
|
|
cout << " " << gas << " + " << (bytecodeSize * eth::c_createDataGas) << " = ";
|
2015-05-22 12:19:58 +00:00
|
|
|
gas += bytecodeSize * eth::c_createDataGas;
|
|
|
|
cout << gas << endl;
|
|
|
|
}
|
|
|
|
if (eth::AssemblyItems const* items = m_compiler->getRuntimeAssemblyItems(_contract))
|
2015-05-26 09:27:59 +00:00
|
|
|
{
|
|
|
|
ContractDefinition const& contract = m_compiler->getContractDefinition(_contract);
|
|
|
|
cout << "external:" << endl;
|
|
|
|
for (auto it: contract.getInterfaceFunctions())
|
2015-05-22 12:19:58 +00:00
|
|
|
{
|
|
|
|
string sig = it.second->externalSignature();
|
|
|
|
GasEstimator::GasConsumption gas = GasEstimator::functionalEstimation(*items, sig);
|
2015-05-26 09:27:59 +00:00
|
|
|
cout << " " << sig << ":\t" << gas << endl;
|
|
|
|
}
|
2015-06-18 15:38:26 +00:00
|
|
|
if (contract.getFallbackFunction())
|
|
|
|
{
|
|
|
|
GasEstimator::GasConsumption gas = GasEstimator::functionalEstimation(*items, "INVALID");
|
|
|
|
cout << " fallback:\t" << gas << endl;
|
|
|
|
}
|
2015-05-26 09:27:59 +00:00
|
|
|
cout << "internal:" << endl;
|
|
|
|
for (auto const& it: contract.getDefinedFunctions())
|
|
|
|
{
|
|
|
|
if (it->isPartOfExternalInterface() || it->isConstructor())
|
|
|
|
continue;
|
|
|
|
size_t entry = m_compiler->getFunctionEntryPoint(_contract, *it);
|
|
|
|
GasEstimator::GasConsumption gas = GasEstimator::GasConsumption::infinite();
|
|
|
|
if (entry > 0)
|
|
|
|
gas = GasEstimator::functionalEstimation(*items, entry, *it);
|
|
|
|
FunctionType type(*it);
|
|
|
|
cout << " " << it->getName() << "(";
|
|
|
|
auto end = type.getParameterTypes().end();
|
|
|
|
for (auto it = type.getParameterTypes().begin(); it != end; ++it)
|
|
|
|
cout << (*it)->toString() << (it + 1 == end ? "" : ",");
|
|
|
|
cout << "):\t" << gas << 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
|
|
|
}
|
|
|
|
|
2015-08-10 10:44:59 +00:00
|
|
|
void CommandLineInterface::createFile(string const& _fileName, string const& _data)
|
|
|
|
{
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
// create directory if not existent
|
2015-08-19 23:09:39 +00:00
|
|
|
fs::path p(m_args.at("output-dir").as<string>());
|
2015-08-10 10:44:59 +00:00
|
|
|
fs::create_directories(p);
|
2015-08-19 23:09:39 +00:00
|
|
|
string pathName = (p / _fileName).string();
|
|
|
|
ofstream outFile(pathName);
|
2015-07-21 13:29:15 +00:00
|
|
|
outFile << _data;
|
2015-08-10 10:44:59 +00:00
|
|
|
if (!outFile)
|
2015-08-19 23:09:39 +00:00
|
|
|
BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + pathName));
|
2015-08-10 10:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CommandLineInterface::parseArguments(int _argc, char** _argv)
|
2014-12-09 16:39:34 +00:00
|
|
|
{
|
|
|
|
// Declare the supported options.
|
2015-08-19 23:09:39 +00:00
|
|
|
po::options_description desc(
|
|
|
|
R"(solc, the Solidity commandline compiler.
|
|
|
|
Usage: solc [options] [input_file...]
|
|
|
|
Compiles the given Solidity input files (or the standard input if none given) and
|
|
|
|
outputs the components specified in the options at standard output or in files in
|
|
|
|
the output directory, if specified.
|
|
|
|
Example: solc --bin -o /tmp/solcoutput contract.sol
|
|
|
|
|
|
|
|
Allowed options)",
|
|
|
|
po::options_description::m_default_line_length,
|
|
|
|
po::options_description::m_default_line_length - 23);
|
2014-12-09 16:39:34 +00:00
|
|
|
desc.add_options()
|
2015-08-19 23:09:39 +00:00
|
|
|
("help", "Show help message and exit.")
|
|
|
|
("version", "Show version and exit.")
|
|
|
|
("optimize", "Enable bytecode optimizer.")
|
|
|
|
(
|
|
|
|
"optimize-runs",
|
|
|
|
po::value<unsigned>()->value_name("n")->default_value(200),
|
|
|
|
"Estimated number of contract runs for optimizer tuning."
|
|
|
|
)
|
|
|
|
(g_argAddStandard.c_str(), "Add standard contracts.")
|
|
|
|
(
|
|
|
|
"output-dir,o",
|
|
|
|
po::value<string>()->value_name("path"),
|
|
|
|
"If given, creates one file per component and contract/file at the specified directory."
|
|
|
|
)
|
2015-04-23 12:40:42 +00:00
|
|
|
(
|
|
|
|
"combined-json",
|
|
|
|
po::value<string>()->value_name(boost::join(g_combinedJsonArgs, ",")),
|
2015-08-19 23:09:39 +00:00
|
|
|
"Output a single json document containing the specified information."
|
2015-04-23 12:40:42 +00:00
|
|
|
)
|
2015-08-19 23:09:39 +00:00
|
|
|
(g_argGas.c_str(), "Print an estimate of the maximal gas usage for each function.");
|
|
|
|
po::options_description outputComponents("Output Components");
|
|
|
|
outputComponents.add_options()
|
|
|
|
(g_argAstStr.c_str(), "AST of all source files.")
|
|
|
|
(g_argAstJson.c_str(), "AST of all source files in JSON format.")
|
|
|
|
(g_argAsmStr.c_str(), "EVM assembly of the contracts.")
|
|
|
|
(g_argAsmJsonStr.c_str(), "EVM assembly of the contracts in JSON format.")
|
|
|
|
(g_argOpcodesStr.c_str(), "Opcodes of the contracts.")
|
|
|
|
(g_argBinaryStr.c_str(), "Binary of the contracts in hex.")
|
|
|
|
(g_argCloneBinaryStr.c_str(), "Binary of the clone contracts in hex.")
|
|
|
|
(g_argAbiStr.c_str(), "ABI specification of the contracts.")
|
|
|
|
(g_argSolInterfaceStr.c_str(), "Solidity interface of the contracts.")
|
|
|
|
(g_argSignatureHashes.c_str(), "Function signature hashes of the contracts.")
|
|
|
|
(g_argNatspecUserStr.c_str(), "Natspec user documentation of all contracts.")
|
|
|
|
(g_argNatspecDevStr.c_str(), "Natspec developer documentation of all contracts.");
|
|
|
|
desc.add(outputComponents);
|
|
|
|
|
|
|
|
po::options_description allOptions = desc;
|
|
|
|
allOptions.add_options()("input-file", po::value<vector<string>>(), "input file");
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
// All positional options should be interpreted as input files
|
2015-08-10 10:44:59 +00:00
|
|
|
po::positional_options_description filesPositions;
|
|
|
|
filesPositions.add("input-file", -1);
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
// parse the compiler arguments
|
|
|
|
try
|
|
|
|
{
|
2015-08-19 23:09:39 +00:00
|
|
|
po::command_line_parser cmdLineParser(_argc, _argv);
|
|
|
|
cmdLineParser.options(allOptions).positional(filesPositions).allow_unregistered();
|
|
|
|
po::store(cmdLineParser.run(), m_args);
|
2014-12-09 16:39:34 +00:00
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (po::error const& _exception)
|
2014-12-09 16:39:34 +00:00
|
|
|
{
|
2015-04-23 16:42:01 +00:00
|
|
|
cerr << _exception.what() << endl;
|
2014-12-09 16:39:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-10 10:40:02 +00:00
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
if (m_args.count("help"))
|
|
|
|
{
|
|
|
|
cout << desc;
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
if (m_args.count("version"))
|
|
|
|
{
|
|
|
|
version();
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2015-08-19 23:09:39 +00:00
|
|
|
if (m_args.count("combined-json"))
|
|
|
|
{
|
|
|
|
vector<string> requests;
|
|
|
|
for (string const& item: boost::split(requests, m_args["combined-json"].as<string>(), boost::is_any_of(",")))
|
|
|
|
if (!g_combinedJsonArgs.count(item))
|
|
|
|
{
|
|
|
|
cerr << "Invalid option to --combined-json: " << item << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
po::notify(m_args);
|
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
return true;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
bool CommandLineInterface::processInput()
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
|
|
|
if (!m_args.count("input-file"))
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
while (!cin.eof())
|
|
|
|
{
|
|
|
|
getline(cin, s);
|
2015-03-09 12:15:59 +00:00
|
|
|
m_sourceCodes["<stdin>"].append(s + '\n');
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (string const& infile: m_args["input-file"].as<vector<string>>())
|
2014-12-10 23:22:35 +00:00
|
|
|
{
|
|
|
|
auto path = boost::filesystem::path(infile);
|
|
|
|
if (!boost::filesystem::exists(path))
|
|
|
|
{
|
2015-04-23 16:42:01 +00:00
|
|
|
cerr << "Skipping non existant input file \"" << infile << "\"" << endl;
|
2014-12-10 23:22:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!boost::filesystem::is_regular_file(path))
|
|
|
|
{
|
2015-04-23 16:42:01 +00:00
|
|
|
cerr << "\"" << infile << "\" is not a valid file. Skipping" << endl;
|
2014-12-10 23:22:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:58:03 +00:00
|
|
|
m_sourceCodes[infile] = dev::contentsString(infile);
|
2014-12-10 23:22:35 +00:00
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2015-08-19 23:09:39 +00:00
|
|
|
m_compiler.reset(new CompilerStack(m_args.count(g_argAddStandard) > 0));
|
2014-12-09 12:43:08 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for (auto const& sourceCode: m_sourceCodes)
|
2015-01-29 00:29:43 +00:00
|
|
|
m_compiler->addSource(sourceCode.first, sourceCode.second);
|
2014-12-09 16:39:34 +00:00
|
|
|
// TODO: Perhaps we should not compile unless requested
|
2015-08-19 23:09:39 +00:00
|
|
|
bool optimize = m_args.count("optimize") > 0;
|
2015-06-01 10:32:59 +00:00
|
|
|
unsigned runs = m_args["optimize-runs"].as<unsigned>();
|
|
|
|
m_compiler->compile(optimize, runs);
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (ParserError const& _exception)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Parser error", *m_compiler);
|
2014-12-09 12:43:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (DeclarationError const& _exception)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Declaration error", *m_compiler);
|
2014-12-09 12:43:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (TypeError const& _exception)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Type error", *m_compiler);
|
2014-12-09 12:43:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (CompilerError const& _exception)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Compiler error", *m_compiler);
|
2014-12-09 12:43:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (InternalCompilerError const& _exception)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
cerr << "Internal compiler error during compilation:" << endl
|
2014-12-17 16:08:57 +00:00
|
|
|
<< boost::diagnostic_information(_exception);
|
2014-12-09 12:43:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-15 13:21:23 +00:00
|
|
|
catch (DocstringParsingError const& _exception)
|
|
|
|
{
|
|
|
|
cerr << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_exception) << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-17 16:08:57 +00:00
|
|
|
catch (Exception const& _exception)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2014-12-17 16:08:57 +00:00
|
|
|
cerr << "Exception during compilation: " << boost::diagnostic_information(_exception) << endl;
|
2014-12-09 12:43:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
cerr << "Unknown exception during compilation." << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
return true;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
void CommandLineInterface::handleCombinedJSON()
|
|
|
|
{
|
2015-04-24 09:48:23 +00:00
|
|
|
if (!m_args.count("combined-json"))
|
|
|
|
return;
|
|
|
|
|
2015-04-23 12:40:42 +00:00
|
|
|
Json::Value output(Json::objectValue);
|
|
|
|
|
|
|
|
set<string> requests;
|
|
|
|
boost::split(requests, m_args["combined-json"].as<string>(), boost::is_any_of(","));
|
|
|
|
vector<string> contracts = m_compiler->getContractNames();
|
|
|
|
|
|
|
|
if (!contracts.empty())
|
|
|
|
output["contracts"] = Json::Value(Json::objectValue);
|
|
|
|
for (string const& contractName: contracts)
|
|
|
|
{
|
|
|
|
Json::Value contractData(Json::objectValue);
|
2015-08-10 10:49:28 +00:00
|
|
|
if (requests.count("interface"))
|
|
|
|
contractData["interface"] = m_compiler->getSolidityInterface(contractName);
|
|
|
|
if (requests.count("abi"))
|
|
|
|
contractData["abi"] = m_compiler->getInterface(contractName);
|
|
|
|
if (requests.count("bin"))
|
|
|
|
contractData["bin"] = toHex(m_compiler->getBytecode(contractName));
|
|
|
|
if (requests.count("clone-bin"))
|
|
|
|
contractData["clone-bin"] = toHex(m_compiler->getCloneBytecode(contractName));
|
2015-04-23 12:40:42 +00:00
|
|
|
if (requests.count("opcodes"))
|
|
|
|
contractData["opcodes"] = eth::disassemble(m_compiler->getBytecode(contractName));
|
|
|
|
if (requests.count("asm"))
|
|
|
|
{
|
|
|
|
ostringstream unused;
|
|
|
|
contractData["asm"] = m_compiler->streamAssembly(unused, contractName, m_sourceCodes, true);
|
|
|
|
}
|
2015-08-10 10:49:28 +00:00
|
|
|
if (requests.count("devdoc"))
|
|
|
|
contractData["devdoc"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecDev);
|
|
|
|
if (requests.count("userdoc"))
|
|
|
|
contractData["userdoc"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecUser);
|
2015-04-23 12:40:42 +00:00
|
|
|
output["contracts"][contractName] = contractData;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requests.count("ast"))
|
|
|
|
{
|
|
|
|
output["sources"] = Json::Value(Json::objectValue);
|
|
|
|
for (auto const& sourceCode: m_sourceCodes)
|
|
|
|
{
|
|
|
|
ASTJsonConverter converter(m_compiler->getAST(sourceCode.first));
|
|
|
|
output["sources"][sourceCode.first] = Json::Value(Json::objectValue);
|
|
|
|
output["sources"][sourceCode.first]["AST"] = converter.json();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cout << Json::FastWriter().write(output) << endl;
|
|
|
|
}
|
|
|
|
|
2015-01-16 10:44:55 +00:00
|
|
|
void CommandLineInterface::handleAst(string const& _argStr)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-01-16 10:44:55 +00:00
|
|
|
string title;
|
2015-01-05 14:46:40 +00:00
|
|
|
|
|
|
|
if (_argStr == g_argAstStr)
|
|
|
|
title = "Syntax trees:";
|
|
|
|
else if (_argStr == g_argAstJson)
|
|
|
|
title = "JSON AST:";
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal argStr for AST"));
|
|
|
|
|
2014-12-09 12:43:08 +00:00
|
|
|
// do we need AST output?
|
2015-01-05 14:46:40 +00:00
|
|
|
if (m_args.count(_argStr))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-05-06 08:43:59 +00:00
|
|
|
vector<ASTNode const*> asts;
|
|
|
|
for (auto const& sourceCode: m_sourceCodes)
|
|
|
|
asts.push_back(&m_compiler->getAST(sourceCode.first));
|
|
|
|
map<ASTNode const*, eth::GasMeter::GasConsumption> gasCosts;
|
|
|
|
if (m_compiler->getRuntimeAssemblyItems())
|
2015-05-22 08:48:54 +00:00
|
|
|
gasCosts = GasEstimator::breakToStatementLevel(
|
|
|
|
GasEstimator::structuralEstimation(*m_compiler->getRuntimeAssemblyItems(), asts),
|
2015-05-06 08:43:59 +00:00
|
|
|
asts
|
|
|
|
);
|
|
|
|
|
2015-08-10 10:40:02 +00:00
|
|
|
if (m_args.count("output-dir"))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
|
|
|
for (auto const& sourceCode: m_sourceCodes)
|
|
|
|
{
|
2015-08-10 10:44:59 +00:00
|
|
|
stringstream data;
|
2015-08-10 10:49:28 +00:00
|
|
|
string postfix = "";
|
2015-01-05 14:46:40 +00:00
|
|
|
if (_argStr == g_argAstStr)
|
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
ASTPrinter printer(m_compiler->getAST(sourceCode.first), sourceCode.second);
|
2015-08-10 10:44:59 +00:00
|
|
|
printer.print(data);
|
2015-01-05 14:46:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
ASTJsonConverter converter(m_compiler->getAST(sourceCode.first));
|
2015-08-10 10:44:59 +00:00
|
|
|
converter.print(data);
|
2015-08-10 10:49:28 +00:00
|
|
|
postfix += "_json";
|
2015-01-05 14:46:40 +00:00
|
|
|
}
|
2015-08-19 23:09:39 +00:00
|
|
|
boost::filesystem::path path(sourceCode.first);
|
|
|
|
createFile(path.filename().string() + postfix + ".ast", data.str());
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
cout << title << endl << endl;
|
2014-12-09 12:43:08 +00:00
|
|
|
for (auto const& sourceCode: m_sourceCodes)
|
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
cout << endl << "======= " << sourceCode.first << " =======" << endl;
|
2015-01-05 14:46:40 +00:00
|
|
|
if (_argStr == g_argAstStr)
|
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
ASTPrinter printer(
|
|
|
|
m_compiler->getAST(sourceCode.first),
|
|
|
|
sourceCode.second,
|
|
|
|
gasCosts
|
|
|
|
);
|
|
|
|
printer.print(cout);
|
2015-01-05 14:46:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
ASTJsonConverter converter(m_compiler->getAST(sourceCode.first));
|
2015-08-10 10:40:02 +00:00
|
|
|
converter.print(cout);
|
2015-01-05 14:46:40 +00:00
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 14:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandLineInterface::actOnInput()
|
|
|
|
{
|
2015-04-23 12:40:42 +00:00
|
|
|
handleCombinedJSON();
|
|
|
|
|
2015-01-05 14:46:40 +00:00
|
|
|
// do we need AST output?
|
|
|
|
handleAst(g_argAstStr);
|
|
|
|
handleAst(g_argAstJson);
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2015-01-29 00:29:43 +00:00
|
|
|
vector<string> contracts = m_compiler->getContractNames();
|
2014-12-09 12:43:08 +00:00
|
|
|
for (string const& contract: contracts)
|
|
|
|
{
|
2015-04-23 12:40:42 +00:00
|
|
|
if (needsHumanTargetedStdout(m_args))
|
2014-12-09 12:43:08 +00:00
|
|
|
cout << endl << "======= " << contract << " =======" << endl;
|
|
|
|
|
|
|
|
// do we need EVM assembly?
|
2015-04-17 14:13:34 +00:00
|
|
|
if (m_args.count(g_argAsmStr) || m_args.count(g_argAsmJsonStr))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-08-10 10:40:02 +00:00
|
|
|
if (m_args.count("output-dir"))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-08-10 10:44:59 +00:00
|
|
|
stringstream data;
|
|
|
|
m_compiler->streamAssembly(data, contract, m_sourceCodes, m_args.count(g_argAsmJsonStr));
|
|
|
|
createFile(contract + (m_args.count(g_argAsmJsonStr) ? "_evm.json" : ".evm"), data.str());
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
2015-08-10 10:40:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "EVM assembly:" << endl;
|
|
|
|
m_compiler->streamAssembly(cout, contract, m_sourceCodes, m_args.count(g_argAsmJsonStr));
|
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 12:19:58 +00:00
|
|
|
if (m_args.count(g_argGas))
|
|
|
|
handleGasEstimation(contract);
|
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
handleBytecode(contract);
|
2015-05-04 14:21:44 +00:00
|
|
|
handleSignatureHashes(contract);
|
2015-02-09 13:12:36 +00:00
|
|
|
handleMeta(DocumentationType::ABIInterface, contract);
|
|
|
|
handleMeta(DocumentationType::ABISolidityInterface, contract);
|
|
|
|
handleMeta(DocumentationType::NatspecDev, contract);
|
|
|
|
handleMeta(DocumentationType::NatspecUser, contract);
|
2014-12-09 12:43:08 +00:00
|
|
|
} // end of contracts iteration
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|