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-05-04 14:50:30 +00:00
|
|
|
static string const g_argAbiStr = "json-abi";
|
|
|
|
static string const g_argSolAbiStr = "sol-abi";
|
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";
|
|
|
|
static string const g_argBinaryStr = "binary";
|
|
|
|
static string const g_argOpcodesStr = "opcodes";
|
|
|
|
static string const g_argNatspecDevStr = "natspec-dev";
|
2014-12-16 23:17:38 +00:00
|
|
|
static string const g_argNatspecUserStr = "natspec-user";
|
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{
|
|
|
|
"binary",
|
|
|
|
"opcodes",
|
|
|
|
"json-abi",
|
|
|
|
"sol-abi",
|
|
|
|
"asm",
|
|
|
|
"ast",
|
|
|
|
"natspec-user",
|
|
|
|
"natspec-dev"
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return _args.count(_name) && _args[_name].as<OutputType>() != OutputType::FILE;
|
|
|
|
}
|
|
|
|
|
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) ||
|
|
|
|
humanTargetedStdout(_args, g_argSolAbiStr) ||
|
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) ||
|
|
|
|
humanTargetedStdout(_args, g_argBinaryStr);
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool outputToFile(OutputType type)
|
|
|
|
{
|
2014-12-09 16:39:34 +00:00
|
|
|
return type == OutputType::FILE || type == OutputType::BOTH;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool outputToStdout(OutputType type)
|
|
|
|
{
|
2014-12-09 16:39:34 +00:00
|
|
|
return type == OutputType::STDOUT || type == OutputType::BOTH;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::istream& operator>>(std::istream& _in, OutputType& io_output)
|
|
|
|
{
|
|
|
|
std::string token;
|
|
|
|
_in >> token;
|
|
|
|
if (token == "stdout")
|
|
|
|
io_output = OutputType::STDOUT;
|
|
|
|
else if (token == "file")
|
|
|
|
io_output = OutputType::FILE;
|
|
|
|
else if (token == "both")
|
|
|
|
io_output = OutputType::BOTH;
|
|
|
|
else
|
|
|
|
throw boost::program_options::invalid_option_value(token);
|
|
|
|
return _in;
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
void CommandLineInterface::handleBinary(string const& _contract)
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2014-12-16 22:55:38 +00:00
|
|
|
auto choice = m_args[g_argBinaryStr].as<OutputType>();
|
2014-12-09 19:29:29 +00:00
|
|
|
if (outputToStdout(choice))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2014-12-09 19:29:29 +00:00
|
|
|
cout << "Binary: " << endl;
|
2015-01-29 00:29:43 +00:00
|
|
|
cout << toHex(m_compiler->getBytecode(_contract)) << endl;
|
2014-12-09 19:29:29 +00:00
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
if (outputToFile(choice))
|
|
|
|
{
|
|
|
|
ofstream outFile(_contract + ".binary");
|
2015-01-29 00:29:43 +00:00
|
|
|
outFile << toHex(m_compiler->getBytecode(_contract));
|
2014-12-09 19:29:29 +00:00
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
}
|
2014-12-09 17:17:54 +00:00
|
|
|
|
2014-12-09 19:29:29 +00:00
|
|
|
void CommandLineInterface::handleOpcode(string const& _contract)
|
|
|
|
{
|
2014-12-16 22:55:38 +00:00
|
|
|
auto choice = m_args[g_argOpcodesStr].as<OutputType>();
|
2014-12-09 19:29:29 +00:00
|
|
|
if (outputToStdout(choice))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputToFile(choice))
|
|
|
|
{
|
|
|
|
ofstream outFile(_contract + ".opcode");
|
2015-01-29 00:29:43 +00:00
|
|
|
outFile << eth::disassemble(m_compiler->getBytecode(_contract));
|
2014-12-09 19:29:29 +00:00
|
|
|
outFile.close();
|
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);
|
2014-12-16 22:55:38 +00:00
|
|
|
if (m_args.count(g_argBinaryStr))
|
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";
|
|
|
|
|
|
|
|
auto choice = m_args[g_argSignatureHashes].as<OutputType>();
|
|
|
|
if (outputToStdout(choice))
|
|
|
|
cout << "Function signatures: " << endl << out;
|
|
|
|
|
|
|
|
if (outputToFile(choice))
|
|
|
|
{
|
|
|
|
ofstream outFile(_contract + ".signatures");
|
|
|
|
outFile << out;
|
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-09 07:05:52 +00:00
|
|
|
argName = g_argSolAbiStr;
|
|
|
|
suffix = ".sol";
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
auto choice = m_args[argName].as<OutputType>();
|
|
|
|
if (outputToStdout(choice))
|
|
|
|
{
|
|
|
|
cout << title << endl;
|
2015-01-29 00:29:43 +00:00
|
|
|
cout << m_compiler->getMetadata(_contract, _type) << endl;
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outputToFile(choice))
|
|
|
|
{
|
|
|
|
ofstream outFile(_contract + suffix);
|
2015-01-29 00:29:43 +00:00
|
|
|
outFile << m_compiler->getMetadata(_contract, _type);
|
2014-12-09 12:43:08 +00:00
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
bool CommandLineInterface::parseArguments(int argc, char** argv)
|
|
|
|
{
|
|
|
|
// Declare the supported options.
|
|
|
|
po::options_description desc("Allowed options");
|
|
|
|
desc.add_options()
|
|
|
|
("help", "Show help message and exit")
|
|
|
|
("version", "Show version and exit")
|
2015-06-01 10:32:59 +00:00
|
|
|
("optimize", po::value<bool>()->default_value(false), "Optimize bytecode")
|
|
|
|
("optimize-runs", po::value<unsigned>()->default_value(200), "Estimated number of contract runs for optimizer.")
|
2015-01-29 00:29:43 +00:00
|
|
|
("add-std", po::value<bool>()->default_value(false), "Add standard contracts")
|
2014-12-09 16:39:34 +00:00
|
|
|
("input-file", po::value<vector<string>>(), "input file")
|
2015-04-23 12:40:42 +00:00
|
|
|
(
|
|
|
|
"combined-json",
|
|
|
|
po::value<string>()->value_name(boost::join(g_combinedJsonArgs, ",")),
|
|
|
|
"Output a single json document containing the specified information, can be combined."
|
|
|
|
)
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argAstStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the AST of the contract.")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argAstJson.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the AST of the contract in JSON format.")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argAsmStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the EVM assembly of the contract.")
|
|
|
|
(g_argAsmJsonStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
|
|
|
"Request to output the EVM assembly of the contract in JSON format.")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argOpcodesStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the Opcodes of the contract.")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argBinaryStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the contract in binary (hexadecimal).")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argAbiStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the contract's JSON ABI interface.")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argSolAbiStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the contract's Solidity ABI interface.")
|
2015-05-04 14:21:44 +00:00
|
|
|
(g_argSignatureHashes.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
|
|
|
"Request to output the contract's functions' signature hashes.")
|
2015-05-22 12:19:58 +00:00
|
|
|
(g_argGas.c_str(),
|
|
|
|
"Request to output an estimate for each function's maximal gas usage.")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argNatspecUserStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the contract's Natspec user documentation.")
|
2015-01-26 16:26:13 +00:00
|
|
|
(g_argNatspecDevStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
|
2015-04-17 15:41:41 +00:00
|
|
|
"Request to output the contract's Natspec developer documentation.");
|
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
|
|
|
|
po::positional_options_description p;
|
|
|
|
p.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
|
|
|
|
{
|
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args);
|
|
|
|
}
|
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-04-23 12:40:42 +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))
|
|
|
|
{
|
2015-04-23 16:42:01 +00:00
|
|
|
cerr << "Invalid option to --combined-json: " << item << endl;
|
2015-04-23 12:40:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-12-09 16:39:34 +00:00
|
|
|
po::notify(m_args);
|
2014-12-09 12:43:08 +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
|
|
|
|
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-01-29 00:29:43 +00:00
|
|
|
m_compiler.reset(new CompilerStack(m_args["add-std"].as<bool>()));
|
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-06-01 10:32:59 +00:00
|
|
|
bool optimize = m_args["optimize"].as<bool>();
|
|
|
|
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;
|
|
|
|
}
|
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);
|
|
|
|
if (requests.count("sol-abi"))
|
|
|
|
contractData["sol-abi"] = m_compiler->getSolidityInterface(contractName);
|
|
|
|
if (requests.count("json-abi"))
|
|
|
|
contractData["json-abi"] = m_compiler->getInterface(contractName);
|
|
|
|
if (requests.count("binary"))
|
|
|
|
contractData["binary"] = toHex(m_compiler->getBytecode(contractName));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (requests.count("natspec-dev"))
|
|
|
|
contractData["natspec-dev"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecDev);
|
|
|
|
if (requests.count("natspec-user"))
|
|
|
|
contractData["natspec-user"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecUser);
|
|
|
|
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-01-05 14:46:40 +00:00
|
|
|
auto choice = m_args[_argStr].as<OutputType>();
|
2014-12-09 12:43:08 +00:00
|
|
|
if (outputToStdout(choice))
|
|
|
|
{
|
2015-01-05 14:46:40 +00:00
|
|
|
cout << title << endl << endl;
|
2014-12-09 12:43:08 +00:00
|
|
|
for (auto const& sourceCode: m_sourceCodes)
|
|
|
|
{
|
|
|
|
cout << endl << "======= " << sourceCode.first << " =======" << endl;
|
2015-01-05 14:46:40 +00:00
|
|
|
if (_argStr == g_argAstStr)
|
|
|
|
{
|
2015-05-06 08:43:59 +00:00
|
|
|
ASTPrinter printer(
|
|
|
|
m_compiler->getAST(sourceCode.first),
|
|
|
|
sourceCode.second,
|
|
|
|
gasCosts
|
|
|
|
);
|
2015-01-05 14:46:40 +00:00
|
|
|
printer.print(cout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
ASTJsonConverter converter(m_compiler->getAST(sourceCode.first));
|
2015-01-05 14:46:40 +00:00
|
|
|
converter.print(cout);
|
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputToFile(choice))
|
|
|
|
{
|
|
|
|
for (auto const& sourceCode: m_sourceCodes)
|
|
|
|
{
|
|
|
|
boost::filesystem::path p(sourceCode.first);
|
|
|
|
ofstream outFile(p.stem().string() + ".ast");
|
2015-01-05 14:46:40 +00:00
|
|
|
if (_argStr == g_argAstStr)
|
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
ASTPrinter printer(m_compiler->getAST(sourceCode.first), sourceCode.second);
|
2015-01-05 14:46:40 +00:00
|
|
|
printer.print(outFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-29 00:29:43 +00:00
|
|
|
ASTJsonConverter converter(m_compiler->getAST(sourceCode.first));
|
2015-01-05 14:46:40 +00:00
|
|
|
converter.print(outFile);
|
|
|
|
}
|
2014-12-09 12:43:08 +00:00
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-04-17 14:13:34 +00:00
|
|
|
auto choice = m_args.count(g_argAsmStr) ? m_args[g_argAsmStr].as<OutputType>() : m_args[g_argAsmJsonStr].as<OutputType>();
|
2014-12-09 16:39:34 +00:00
|
|
|
if (outputToStdout(choice))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
|
|
|
cout << "EVM assembly:" << endl;
|
2015-04-17 15:41:41 +00:00
|
|
|
m_compiler->streamAssembly(cout, contract, m_sourceCodes, m_args.count(g_argAsmJsonStr));
|
2014-12-09 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 16:39:34 +00:00
|
|
|
if (outputToFile(choice))
|
2014-12-09 12:43:08 +00:00
|
|
|
{
|
2015-04-15 11:28:49 +00:00
|
|
|
ofstream outFile(contract + (m_args.count(g_argAsmJsonStr) ? "_evm.json" : ".evm"));
|
2015-04-17 15:41:41 +00:00
|
|
|
m_compiler->streamAssembly(outFile, contract, m_sourceCodes, m_args.count(g_argAsmJsonStr));
|
2014-12-09 12:43:08 +00:00
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|