Merge solc files from cpp-ethereum.

This commit is contained in:
chriseth 2015-08-19 13:12:18 +02:00
commit 4e7f473c1b
2 changed files with 137 additions and 160 deletions

View File

@ -54,32 +54,32 @@ namespace dev
namespace solidity namespace solidity
{ {
static string const g_argAbiStr = "json-abi"; static string const g_argAbiStr = "abi";
static string const g_argSolAbiStr = "sol-abi"; static string const g_argSolInterfaceStr = "interface";
static string const g_argSignatureHashes = "hashes"; static string const g_argSignatureHashes = "hashes";
static string const g_argGas = "gas"; static string const g_argGas = "gas";
static string const g_argAsmStr = "asm"; static string const g_argAsmStr = "asm";
static string const g_argAsmJsonStr = "asm-json"; static string const g_argAsmJsonStr = "asm-json";
static string const g_argAstStr = "ast"; static string const g_argAstStr = "ast";
static string const g_argAstJson = "ast-json"; static string const g_argAstJson = "ast-json";
static string const g_argBinaryStr = "binary"; static string const g_argBinaryStr = "bin";
static string const g_argCloneBinaryStr = "clone-binary"; static string const g_argCloneBinaryStr = "clone-bin";
static string const g_argOpcodesStr = "opcodes"; static string const g_argOpcodesStr = "opcodes";
static string const g_argNatspecDevStr = "natspec-dev"; static string const g_argNatspecDevStr = "devdoc";
static string const g_argNatspecUserStr = "natspec-user"; static string const g_argNatspecUserStr = "userdoc";
static string const g_argAddStandard = "add-std"; static string const g_argAddStandard = "add-std";
/// Possible arguments to for --combined-json /// Possible arguments to for --combined-json
static set<string> const g_combinedJsonArgs{ static set<string> const g_combinedJsonArgs{
"binary", "bin",
"clone-binary", "clone-bin",
"opcodes", "opcodes",
"json-abi", "abi",
"sol-abi", "interface",
"asm", "asm",
"ast", "ast",
"natspec-user", "userdoc",
"natspec-dev" "devdoc"
}; };
static void version() static void version()
@ -95,7 +95,7 @@ static void version()
static inline bool humanTargetedStdout(po::variables_map const& _args, string const& _name) static inline bool humanTargetedStdout(po::variables_map const& _args, string const& _name)
{ {
return _args.count(_name) && _args[_name].as<OutputType>() != OutputType::FILE; return _args.count(_name) && !(_args.count("output-dir"));
} }
static bool needsHumanTargetedStdout(po::variables_map const& _args) static bool needsHumanTargetedStdout(po::variables_map const& _args)
@ -104,7 +104,7 @@ static bool needsHumanTargetedStdout(po::variables_map const& _args)
return return
_args.count(g_argGas) || _args.count(g_argGas) ||
humanTargetedStdout(_args, g_argAbiStr) || humanTargetedStdout(_args, g_argAbiStr) ||
humanTargetedStdout(_args, g_argSolAbiStr) || humanTargetedStdout(_args, g_argSolInterfaceStr) ||
humanTargetedStdout(_args, g_argSignatureHashes) || humanTargetedStdout(_args, g_argSignatureHashes) ||
humanTargetedStdout(_args, g_argNatspecUserStr) || humanTargetedStdout(_args, g_argNatspecUserStr) ||
humanTargetedStdout(_args, g_argAstJson) || humanTargetedStdout(_args, g_argAstJson) ||
@ -116,79 +116,60 @@ static bool needsHumanTargetedStdout(po::variables_map const& _args)
humanTargetedStdout(_args, g_argCloneBinaryStr); humanTargetedStdout(_args, g_argCloneBinaryStr);
} }
static inline bool outputToFile(OutputType type)
{
return type == OutputType::FILE || type == OutputType::BOTH;
}
static inline bool outputToStdout(OutputType type)
{
return type == OutputType::STDOUT || type == OutputType::BOTH;
}
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;
}
void CommandLineInterface::handleBinary(string const& _contract) void CommandLineInterface::handleBinary(string const& _contract)
{ {
if (m_args.count(g_argBinaryStr)) if (m_args.count(g_argBinaryStr))
{ {
if (outputToStdout(m_args[g_argBinaryStr].as<OutputType>())) if (m_args.count("output-dir"))
{
stringstream data;
data << toHex(m_compiler->getBytecode(_contract));
createFile(_contract + ".bin", data.str());
}
else
{ {
cout << "Binary: " << endl; cout << "Binary: " << endl;
cout << toHex(m_compiler->getBytecode(_contract)) << endl; cout << toHex(m_compiler->getBytecode(_contract)) << endl;
} }
if (outputToFile(m_args[g_argBinaryStr].as<OutputType>()))
{
ofstream outFile(_contract + ".binary");
outFile << toHex(m_compiler->getBytecode(_contract));
outFile.close();
}
} }
if (m_args.count(g_argCloneBinaryStr)) if (m_args.count(g_argCloneBinaryStr))
{ {
if (outputToStdout(m_args[g_argCloneBinaryStr].as<OutputType>())) if (m_args.count("output-dir"))
{
stringstream data;
data << toHex(m_compiler->getCloneBytecode(_contract));
createFile(_contract + ".clone_bin", data.str());
}
else
{ {
cout << "Clone Binary: " << endl; cout << "Clone Binary: " << endl;
cout << toHex(m_compiler->getCloneBytecode(_contract)) << endl; cout << toHex(m_compiler->getCloneBytecode(_contract)) << endl;
} }
if (outputToFile(m_args[g_argCloneBinaryStr].as<OutputType>()))
{
ofstream outFile(_contract + ".clone_binary");
outFile << toHex(m_compiler->getCloneBytecode(_contract));
outFile.close();
}
} }
else
{
cout << "Binary: " << endl;
cout << toHex(m_compiler->getBytecode(_contract)) << endl;
}
} }
void CommandLineInterface::handleOpcode(string const& _contract) void CommandLineInterface::handleOpcode(string const& _contract)
{ {
auto choice = m_args[g_argOpcodesStr].as<OutputType>(); if (m_args.count("output-dir"))
if (outputToStdout(choice)) {
stringstream data;
data << eth::disassemble(m_compiler->getBytecode(_contract));
createFile(_contract + ".opcode", data.str());
}
else
{ {
cout << "Opcodes: " << endl; cout << "Opcodes: " << endl;
cout << eth::disassemble(m_compiler->getBytecode(_contract)); cout << eth::disassemble(m_compiler->getBytecode(_contract));
cout << endl; cout << endl;
} }
if (outputToFile(choice))
{
ofstream outFile(_contract + ".opcode");
outFile << eth::disassemble(m_compiler->getBytecode(_contract));
outFile.close();
}
} }
void CommandLineInterface::handleBytecode(string const& _contract) void CommandLineInterface::handleBytecode(string const& _contract)
@ -208,16 +189,14 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
for (auto const& it: m_compiler->getContractDefinition(_contract).getInterfaceFunctions()) for (auto const& it: m_compiler->getContractDefinition(_contract).getInterfaceFunctions())
out += toHex(it.first.ref()) + ": " + it.second->externalSignature() + "\n"; out += toHex(it.first.ref()) + ": " + it.second->externalSignature() + "\n";
auto choice = m_args[g_argSignatureHashes].as<OutputType>(); if (m_args.count("output-dir"))
if (outputToStdout(choice))
cout << "Function signatures: " << endl << out;
if (outputToFile(choice))
{ {
ofstream outFile(_contract + ".signatures"); stringstream data;
outFile << out; data << out;
outFile.close(); createFile(_contract + ".signatures", data.str());
} }
else
cout << "Function signatures: " << endl << out;
} }
void CommandLineInterface::handleMeta(DocumentationType _type, string const& _contract) void CommandLineInterface::handleMeta(DocumentationType _type, string const& _contract)
@ -233,8 +212,8 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co
title = "Contract JSON ABI"; title = "Contract JSON ABI";
break; break;
case DocumentationType::ABISolidityInterface: case DocumentationType::ABISolidityInterface:
argName = g_argSolAbiStr; argName = g_argSolInterfaceStr;
suffix = ".sol"; suffix = "_interface.sol";
title = "Contract Solidity ABI"; title = "Contract Solidity ABI";
break; break;
case DocumentationType::NatspecUser: case DocumentationType::NatspecUser:
@ -254,19 +233,18 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co
if (m_args.count(argName)) if (m_args.count(argName))
{ {
auto choice = m_args[argName].as<OutputType>(); if (m_args.count("output-dir"))
if (outputToStdout(choice)) {
stringstream data;
data << m_compiler->getMetadata(_contract, _type);
createFile(_contract + suffix, data.str());
}
else
{ {
cout << title << endl; cout << title << endl;
cout << m_compiler->getMetadata(_contract, _type) << endl; cout << m_compiler->getMetadata(_contract, _type) << endl;
} }
if (outputToFile(choice))
{
ofstream outFile(_contract + suffix);
outFile << m_compiler->getMetadata(_contract, _type);
outFile.close();
}
} }
} }
@ -319,7 +297,19 @@ void CommandLineInterface::handleGasEstimation(string const& _contract)
} }
} }
bool CommandLineInterface::parseArguments(int argc, char** argv) void CommandLineInterface::createFile(string const& _fileName, string const& _data)
{
namespace fs = boost::filesystem;
// create directory if not existent
fs::path p(m_args["output-dir"].as<string>());
fs::create_directories(p);
ofstream outFile((p / _fileName).string());
outFile << _data;
if (!outFile)
BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + _fileName));
}
bool CommandLineInterface::parseArguments(int _argc, char** _argv)
{ {
// Declare the supported options. // Declare the supported options.
po::options_description desc("Allowed options"); po::options_description desc("Allowed options");
@ -330,52 +320,43 @@ bool CommandLineInterface::parseArguments(int argc, char** argv)
("optimize-runs", po::value<unsigned>()->default_value(200), "Estimated number of contract runs for optimizer.") ("optimize-runs", po::value<unsigned>()->default_value(200), "Estimated number of contract runs for optimizer.")
("add-std", po::value<bool>()->default_value(false), "Add standard contracts") ("add-std", po::value<bool>()->default_value(false), "Add standard contracts")
("input-file", po::value<vector<string>>(), "input file") ("input-file", po::value<vector<string>>(), "input file")
("output-dir,o", po::value<string>(), "Output directory path")
( (
"combined-json", "combined-json",
po::value<string>()->value_name(boost::join(g_combinedJsonArgs, ",")), po::value<string>()->value_name(boost::join(g_combinedJsonArgs, ",")),
"Output a single json document containing the specified information, can be combined." "Output a single json document containing the specified information, can be combined."
) )
(g_argAstStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argAstStr.c_str(), "Outputs the AST of the contract.")
"Request to output the AST of the contract.") (g_argAstJson.c_str(), "Outputs the AST of the contract in JSON format.")
(g_argAstJson.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argAsmStr.c_str(), "Outputs the EVM assembly of the contract.")
"Request to output the AST of the contract in JSON format.") (g_argAsmJsonStr.c_str(), "Outputs the EVM assembly of the contract in JSON format.")
(g_argAsmStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argOpcodesStr.c_str(), "Outputs the Opcodes of the contract.")
"Request to output the EVM assembly of the contract.") (g_argBinaryStr.c_str(), "Outputs the contract in binary (hexadecimal).")
(g_argAsmJsonStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argCloneBinaryStr.c_str(), "Output the clone contract in binary (hexadecimal).")
"Request to output the EVM assembly of the contract in JSON format.") (g_argAbiStr.c_str(), "Outputs the contract's JSON ABI interface.")
(g_argOpcodesStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argSolInterfaceStr.c_str(), "Outputs the contract's Solidity interface.")
"Request to output the Opcodes of the contract.") (g_argSignatureHashes.c_str(), "Outputs the contract's functions' signature hashes.")
(g_argBinaryStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argGas.c_str(), "Outputs an estimate for each function's maximal gas usage.")
"Request to output the contract in binary (hexadecimal).") (g_argNatspecUserStr.c_str(), "Outputs the contract's Natspec user documentation.")
(g_argCloneBinaryStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argNatspecDevStr.c_str(), "Outputs the contract's Natspec developer documentation.");
"Request to output the clone contract in binary (hexadecimal).")
(g_argAbiStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's JSON ABI interface.")
(g_argSolAbiStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's Solidity ABI interface.")
(g_argSignatureHashes.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's functions' signature hashes.")
(g_argGas.c_str(),
"Request to output an estimate for each function's maximal gas usage.")
(g_argNatspecUserStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's Natspec user documentation.")
(g_argNatspecDevStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's Natspec developer documentation.");
// All positional options should be interpreted as input files // All positional options should be interpreted as input files
po::positional_options_description p; po::positional_options_description filesPositions;
p.add("input-file", -1); filesPositions.add("output-dir", 1);
filesPositions.add("input-file", -1);
// parse the compiler arguments // parse the compiler arguments
try try
{ {
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); po::store(po::command_line_parser(_argc, _argv).options(desc).positional(filesPositions).allow_unregistered().run(), m_args);
} }
catch (po::error const& _exception) catch (po::error const& _exception)
{ {
cerr << _exception.what() << endl; cerr << _exception.what() << endl;
return false; return false;
} }
if (m_args.count("combined-json")) if (m_args.count("combined-json"))
{ {
vector<string> requests; vector<string> requests;
@ -504,14 +485,14 @@ void CommandLineInterface::handleCombinedJSON()
for (string const& contractName: contracts) for (string const& contractName: contracts)
{ {
Json::Value contractData(Json::objectValue); Json::Value contractData(Json::objectValue);
if (requests.count("sol-abi")) if (requests.count("interface"))
contractData["sol-abi"] = m_compiler->getSolidityInterface(contractName); contractData["interface"] = m_compiler->getSolidityInterface(contractName);
if (requests.count("json-abi")) if (requests.count("abi"))
contractData["json-abi"] = m_compiler->getInterface(contractName); contractData["abi"] = m_compiler->getInterface(contractName);
if (requests.count("binary")) if (requests.count("bin"))
contractData["binary"] = toHex(m_compiler->getBytecode(contractName)); contractData["bin"] = toHex(m_compiler->getBytecode(contractName));
if (requests.count("clone-binary")) if (requests.count("clone-bin"))
contractData["clone-binary"] = toHex(m_compiler->getCloneBytecode(contractName)); contractData["clone-bin"] = toHex(m_compiler->getCloneBytecode(contractName));
if (requests.count("opcodes")) if (requests.count("opcodes"))
contractData["opcodes"] = eth::disassemble(m_compiler->getBytecode(contractName)); contractData["opcodes"] = eth::disassemble(m_compiler->getBytecode(contractName));
if (requests.count("asm")) if (requests.count("asm"))
@ -519,10 +500,10 @@ void CommandLineInterface::handleCombinedJSON()
ostringstream unused; ostringstream unused;
contractData["asm"] = m_compiler->streamAssembly(unused, contractName, m_sourceCodes, true); contractData["asm"] = m_compiler->streamAssembly(unused, contractName, m_sourceCodes, true);
} }
if (requests.count("natspec-dev")) if (requests.count("devdoc"))
contractData["natspec-dev"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecDev); contractData["devdoc"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecDev);
if (requests.count("natspec-user")) if (requests.count("userdoc"))
contractData["natspec-user"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecUser); contractData["userdoc"] = m_compiler->getMetadata(contractName, DocumentationType::NatspecUser);
output["contracts"][contractName] = contractData; output["contracts"][contractName] = contractData;
} }
@ -563,8 +544,27 @@ void CommandLineInterface::handleAst(string const& _argStr)
asts asts
); );
auto choice = m_args[_argStr].as<OutputType>(); if (m_args.count("output-dir"))
if (outputToStdout(choice)) {
for (auto const& sourceCode: m_sourceCodes)
{
stringstream data;
string postfix = "";
if (_argStr == g_argAstStr)
{
ASTPrinter printer(m_compiler->getAST(sourceCode.first), sourceCode.second);
printer.print(data);
}
else
{
ASTJsonConverter converter(m_compiler->getAST(sourceCode.first));
converter.print(data);
postfix += "_json";
}
createFile(sourceCode.first + postfix + ".ast", data.str());
}
}
else
{ {
cout << title << endl << endl; cout << title << endl << endl;
for (auto const& sourceCode: m_sourceCodes) for (auto const& sourceCode: m_sourceCodes)
@ -586,26 +586,6 @@ void CommandLineInterface::handleAst(string const& _argStr)
} }
} }
} }
if (outputToFile(choice))
{
for (auto const& sourceCode: m_sourceCodes)
{
boost::filesystem::path p(sourceCode.first);
ofstream outFile(p.stem().string() + ".ast");
if (_argStr == g_argAstStr)
{
ASTPrinter printer(m_compiler->getAST(sourceCode.first), sourceCode.second);
printer.print(outFile);
}
else
{
ASTJsonConverter converter(m_compiler->getAST(sourceCode.first));
converter.print(outFile);
}
outFile.close();
}
}
} }
} }
@ -626,19 +606,17 @@ void CommandLineInterface::actOnInput()
// do we need EVM assembly? // do we need EVM assembly?
if (m_args.count(g_argAsmStr) || m_args.count(g_argAsmJsonStr)) if (m_args.count(g_argAsmStr) || m_args.count(g_argAsmJsonStr))
{ {
auto choice = m_args.count(g_argAsmStr) ? m_args[g_argAsmStr].as<OutputType>() : m_args[g_argAsmJsonStr].as<OutputType>(); if (m_args.count("output-dir"))
if (outputToStdout(choice)) {
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());
}
else
{ {
cout << "EVM assembly:" << endl; cout << "EVM assembly:" << endl;
m_compiler->streamAssembly(cout, contract, m_sourceCodes, m_args.count(g_argAsmJsonStr)); m_compiler->streamAssembly(cout, contract, m_sourceCodes, m_args.count(g_argAsmJsonStr));
} }
if (outputToFile(choice))
{
ofstream outFile(contract + (m_args.count(g_argAsmJsonStr) ? "_evm.json" : ".evm"));
m_compiler->streamAssembly(outFile, contract, m_sourceCodes, m_args.count(g_argAsmJsonStr));
outFile.close();
}
} }
if (m_args.count(g_argGas)) if (m_args.count(g_argGas))

View File

@ -33,20 +33,13 @@ namespace solidity
//forward declaration //forward declaration
enum class DocumentationType: uint8_t; enum class DocumentationType: uint8_t;
enum class OutputType: uint8_t
{
STDOUT,
FILE,
BOTH
};
class CommandLineInterface class CommandLineInterface
{ {
public: public:
CommandLineInterface() {} CommandLineInterface() {}
/// Parse command line arguments and return false if we should not continue /// Parse command line arguments and return false if we should not continue
bool parseArguments(int argc, char** argv); bool parseArguments(int _argc, char** _argv);
/// Parse the files and create source code objects /// Parse the files and create source code objects
bool processInput(); bool processInput();
/// Perform actions on the input depending on provided compiler arguments /// Perform actions on the input depending on provided compiler arguments
@ -63,6 +56,12 @@ private:
std::string const& _contract); std::string const& _contract);
void handleGasEstimation(std::string const& _contract); void handleGasEstimation(std::string const& _contract);
/// Create a file in the given directory
/// @arg _fileName the name of the file
/// @arg _data to be written
void createFile(std::string const& _fileName, std::string const& _data);
/// Compiler arguments variable map /// Compiler arguments variable map
boost::program_options::variables_map m_args; boost::program_options::variables_map m_args;
/// map of input files to source code strings /// map of input files to source code strings