Merge pull request #2213 from ethereum/jsoncompiler

Rewrite jsonCompiler using StandardCompiler
This commit is contained in:
chriseth 2017-06-23 17:41:14 +02:00 committed by GitHub
commit 633b6bd61a
2 changed files with 94 additions and 124 deletions

View File

@ -5,6 +5,7 @@ Features:
* Assembly: Display auxiliary data in the assembly output. * Assembly: Display auxiliary data in the assembly output.
* Assembly: Add ``CREATE2`` (EIP86), ``STATICCALL`` (EIP214), ``RETURNDATASIZE`` and ``RETURNDATACOPY`` (EIP211) instructions. * Assembly: Add ``CREATE2`` (EIP86), ``STATICCALL`` (EIP214), ``RETURNDATASIZE`` and ``RETURNDATACOPY`` (EIP211) instructions.
* AST: export all attributes to JSON format. * AST: export all attributes to JSON format.
* C API (``jsonCompiler``): Use the Standard JSON I/O internally.
* Inline Assembly: Present proper error message when not supplying enough arguments to a functional * Inline Assembly: Present proper error message when not supplying enough arguments to a functional
instruction. instruction.
* Inline Assembly: introduce ``keccak256`` as an opcode. ``sha3`` is still a valid alias. * Inline Assembly: introduce ``keccak256`` as an opcode. ``sha3`` is still a valid alias.

View File

@ -21,24 +21,9 @@
*/ */
#include <string> #include <string>
#include <functional>
#include <iostream>
#include <json/json.h>
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/JSON.h> #include <libdevcore/JSON.h>
#include <libevmasm/Instruction.h>
#include <libevmasm/GasMeter.h>
#include <libsolidity/parsing/Scanner.h>
#include <libsolidity/parsing/Parser.h>
#include <libsolidity/ast/ASTPrinter.h>
#include <libsolidity/analysis/NameAndTypeResolver.h>
#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/StandardCompiler.h> #include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/SourceReferenceFormatter.h>
#include <libsolidity/ast/ASTJsonConverter.h>
#include <libsolidity/interface/Version.h> #include <libsolidity/interface/Version.h>
#include "license.h" #include "license.h"
@ -109,9 +94,8 @@ Json::Value gasToJson(Json::Value const& _value)
return Json::Value(Json::LargestUInt(value)); return Json::Value(Json::LargestUInt(value));
} }
Json::Value estimateGas(CompilerStack const& _compiler, string const& _contract) Json::Value translateGasEstimates(Json::Value const& estimates)
{ {
Json::Value estimates = _compiler.gasEstimates(_contract);
Json::Value output(Json::objectValue); Json::Value output(Json::objectValue);
if (estimates["creation"].isObject()) if (estimates["creation"].isObject())
@ -131,120 +115,105 @@ Json::Value estimateGas(CompilerStack const& _compiler, string const& _contract)
string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback _readCallback) string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback _readCallback)
{ {
Json::Value output(Json::objectValue); /// create new JSON input format
Json::Value errors(Json::arrayValue); Json::Value input = Json::objectValue;
CompilerStack compiler(wrapReadCallback(_readCallback)); input["language"] = "Solidity";
auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return compiler.scanner(_sourceName); }; input["sources"] = Json::objectValue;
bool success = false; for (auto const& source: _sources)
try
{ {
compiler.addSources(_sources); input["sources"][source.first] = Json::objectValue;
bool succ = compiler.compile(_optimize); input["sources"][source.first]["content"] = source.second;
for (auto const& error: compiler.errors())
{
auto err = dynamic_pointer_cast<Error const>(error);
errors.append(SourceReferenceFormatter::formatExceptionInformation(
*error,
(err->type() == Error::Type::Warning) ? "Warning" : "Error",
scannerFromSourceName
));
} }
success = succ; // keep success false on exception input["settings"] = Json::objectValue;
} input["settings"]["optimizer"] = Json::objectValue;
catch (Error const& error) input["settings"]["optimizer"]["enabled"] = _optimize;
input["settings"]["optimizer"]["runs"] = 200;
StandardCompiler compiler(wrapReadCallback(_readCallback));
Json::Value ret = compiler.compile(input);
/// transform JSON to match the old format
// {
// "errors": [ "Error 1", "Error 2" ],
// "sourceList": [ "sourcename1", "sourcename2" ],
// "sources": {
// "sourcename1": {
// "AST": {}
// }
// },
// "contracts": {
// "Contract1": {
// "interface": "[...abi...]",
// "bytecode": "ff0011...",
// "runtimeBytecode": "ff0011",
// "opcodes": "PUSH 1 POP STOP",
// "metadata": "{...metadata...}",
// "functionHashes": {
// "test(uint256)": "11ff2233"
// },
// "gasEstimates": {
// "creation": [ 224, 42000 ],
// "external": {
// "11ff2233": null,
// "3322ff11": 1234
// },
// "internal": {
// }
// },
// "srcmap" = "0:1:2",
// "srcmapRuntime" = "0:1:2",
// "assembly" = {}
// }
// },
// "formal": {
// "errors": [ "Error 1" ],
// "why3": "why3 source"
// }
// }
Json::Value output = Json::objectValue;
if (ret.isMember("errors"))
{ {
errors.append(SourceReferenceFormatter::formatExceptionInformation(error, error.typeName(), scannerFromSourceName)); output["errors"] = Json::arrayValue;
} for (auto const& error: ret["errors"])
catch (CompilerError const& exception) output["errors"].append(
{ !error["formattedMessage"].empty() ? error["formattedMessage"] : error["message"]
errors.append(SourceReferenceFormatter::formatExceptionInformation(exception, "Compiler error (" + exception.lineInfo() + ")", scannerFromSourceName)); );
}
catch (InternalCompilerError const& exception)
{
errors.append(SourceReferenceFormatter::formatExceptionInformation(exception, "Internal compiler error (" + exception.lineInfo() + ")", scannerFromSourceName));
}
catch (UnimplementedFeatureError const& exception)
{
errors.append(SourceReferenceFormatter::formatExceptionInformation(exception, "Unimplemented feature (" + exception.lineInfo() + ")", scannerFromSourceName));
}
catch (Exception const& exception)
{
errors.append("Exception during compilation: " + boost::diagnostic_information(exception));
}
catch (...)
{
errors.append("Unknown exception during compilation.");
} }
if (errors.size() > 0) output["sourceList"] = Json::arrayValue;
output["errors"] = errors; for (auto const& source: _sources)
output["sourceList"].append(source.first);
if (success) if (ret.isMember("sources"))
{ {
try output["sources"] = Json::objectValue;
for (auto const& sourceName: ret["sources"].getMemberNames())
{ {
output["contracts"] = Json::Value(Json::objectValue); output["sources"][sourceName] = Json::objectValue;
for (string const& contractName: compiler.contractNames()) output["sources"][sourceName]["AST"] = ret["sources"][sourceName]["legacyAST"];
{
Json::Value contractData(Json::objectValue);
contractData["interface"] = dev::jsonCompactPrint(compiler.contractABI(contractName));
contractData["bytecode"] = compiler.object(contractName).toHex();
contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex();
contractData["opcodes"] = solidity::disassemble(compiler.object(contractName).bytecode);
contractData["metadata"] = compiler.onChainMetadata(contractName);
contractData["functionHashes"] = compiler.methodIdentifiers(contractName);
contractData["gasEstimates"] = estimateGas(compiler, contractName);
auto sourceMap = compiler.sourceMapping(contractName);
contractData["srcmap"] = sourceMap ? *sourceMap : "";
auto runtimeSourceMap = compiler.runtimeSourceMapping(contractName);
contractData["srcmapRuntime"] = runtimeSourceMap ? *runtimeSourceMap : "";
ostringstream unused;
contractData["assembly"] = compiler.streamAssembly(unused, contractName, _sources, true);
output["contracts"][contractName] = contractData;
} }
} }
catch (...)
{
output["errors"].append("Unknown exception while generating contract data output.");
}
try if (ret.isMember("contracts"))
{ {
// Do not taint the internal error list output["contracts"] = Json::objectValue;
ErrorList formalErrors; for (auto const& sourceName: ret["contracts"].getMemberNames())
ErrorReporter errorReporter(formalErrors); for (auto const& contractName: ret["contracts"][sourceName].getMemberNames())
if (compiler.prepareFormalAnalysis(&errorReporter))
output["formal"]["why3"] = compiler.formalTranslation();
if (!errorReporter.errors().empty())
{ {
Json::Value errors(Json::arrayValue); Json::Value contractInput = ret["contracts"][sourceName][contractName];
for (auto const& error: errorReporter.errors()) Json::Value contractOutput = Json::objectValue;
errors.append(SourceReferenceFormatter::formatExceptionInformation( contractOutput["interface"] = dev::jsonCompactPrint(contractInput["abi"]);
*error, contractOutput["metadata"] = contractInput["metadata"];
(error->type() == Error::Type::Warning) ? "Warning" : "Error", contractOutput["functionHashes"] = contractInput["evm"]["methodIdentifiers"];
scannerFromSourceName contractOutput["gasEstimates"] = translateGasEstimates(contractInput["evm"]["gasEstimates"]);
)); contractOutput["assembly"] = contractInput["evm"]["legacyAssembly"];
output["formal"]["errors"] = errors; contractOutput["bytecode"] = contractInput["evm"]["bytecode"]["object"];
} contractOutput["opcodes"] = contractInput["evm"]["bytecode"]["opcodes"];
} contractOutput["srcmap"] = contractInput["evm"]["bytecode"]["sourceMap"];
catch (...) contractOutput["runtimeBytecode"] = contractInput["evm"]["deployedBytecode"]["object"];
{ contractOutput["srcmapRuntime"] = contractInput["evm"]["deployedBytecode"]["sourceMap"];
output["errors"].append("Unknown exception while generating formal method output."); output["contracts"][sourceName + ":" + contractName] = contractOutput;
}
try
{
// Indices into this array are used to abbreviate source names in source locations.
output["sourceList"] = Json::Value(Json::arrayValue);
for (auto const& source: compiler.sourceNames())
output["sourceList"].append(source);
output["sources"] = Json::Value(Json::objectValue);
for (auto const& source: compiler.sourceNames())
output["sources"][source]["AST"] = ASTJsonConverter(true, compiler.sourceIndices()).toJson(compiler.ast(source));
}
catch (...)
{
output["errors"].append("Unknown exception while generating source name output.");
} }
} }