2017-02-09 21:56:49 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Alex Beregszaszi
|
|
|
|
* @date 2016
|
|
|
|
* Standard JSON compiler interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/interface/StandardCompiler.h>
|
2017-02-10 13:18:21 +00:00
|
|
|
#include <libsolidity/interface/SourceReferenceFormatter.h>
|
2017-03-29 13:59:00 +00:00
|
|
|
#include <libsolidity/ast/ASTJsonConverter.h>
|
2017-02-10 13:18:21 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
|
|
|
#include <libdevcore/JSON.h>
|
2017-04-22 13:43:10 +00:00
|
|
|
#include <libdevcore/SHA3.h>
|
2017-02-09 21:56:49 +00:00
|
|
|
|
2018-02-28 15:57:35 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2017-02-09 21:56:49 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
2017-04-19 13:11:31 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-03-29 14:40:15 +00:00
|
|
|
Json::Value formatError(
|
|
|
|
bool _warning,
|
|
|
|
string const& _type,
|
|
|
|
string const& _component,
|
|
|
|
string const& _message,
|
|
|
|
string const& _formattedMessage = "",
|
|
|
|
Json::Value const& _sourceLocation = Json::Value()
|
|
|
|
)
|
2017-03-21 11:18:00 +00:00
|
|
|
{
|
|
|
|
Json::Value error = Json::objectValue;
|
|
|
|
error["type"] = _type;
|
2017-03-29 14:40:15 +00:00
|
|
|
error["component"] = _component;
|
|
|
|
error["severity"] = _warning ? "warning" : "error";
|
|
|
|
error["message"] = _message;
|
|
|
|
error["formattedMessage"] = (_formattedMessage.length() > 0) ? _formattedMessage : _message;
|
|
|
|
if (_sourceLocation.isObject())
|
|
|
|
error["sourceLocation"] = _sourceLocation;
|
|
|
|
return error;
|
|
|
|
}
|
2017-03-21 11:18:00 +00:00
|
|
|
|
2017-03-29 14:40:15 +00:00
|
|
|
Json::Value formatFatalError(string const& _type, string const& _message)
|
|
|
|
{
|
2017-03-21 11:18:00 +00:00
|
|
|
Json::Value output = Json::objectValue;
|
|
|
|
output["errors"] = Json::arrayValue;
|
2017-03-29 14:40:15 +00:00
|
|
|
output["errors"].append(formatError(false, _type, "general", _message));
|
2017-03-21 11:18:00 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-03-29 20:50:53 +00:00
|
|
|
Json::Value formatErrorWithException(
|
|
|
|
Exception const& _exception,
|
|
|
|
bool const& _warning,
|
|
|
|
string const& _type,
|
|
|
|
string const& _component,
|
|
|
|
string const& _message,
|
|
|
|
function<Scanner const&(string const&)> const& _scannerFromSourceName
|
|
|
|
)
|
|
|
|
{
|
|
|
|
string message;
|
2017-07-05 15:02:02 +00:00
|
|
|
string formattedMessage = SourceReferenceFormatter::formatExceptionInformation(_exception, _type, _scannerFromSourceName);
|
2017-03-29 20:50:53 +00:00
|
|
|
|
|
|
|
// NOTE: the below is partially a copy from SourceReferenceFormatter
|
|
|
|
SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
|
|
|
|
|
|
|
|
if (string const* description = boost::get_error_info<errinfo_comment>(_exception))
|
|
|
|
message = ((_message.length() > 0) ? (_message + ":") : "") + *description;
|
|
|
|
else
|
|
|
|
message = _message;
|
|
|
|
|
2017-12-18 11:40:06 +00:00
|
|
|
Json::Value sourceLocation;
|
2017-03-29 20:50:53 +00:00
|
|
|
if (location && location->sourceName)
|
|
|
|
{
|
|
|
|
sourceLocation["file"] = *location->sourceName;
|
|
|
|
sourceLocation["start"] = location->start;
|
|
|
|
sourceLocation["end"] = location->end;
|
|
|
|
}
|
|
|
|
|
2017-12-18 11:40:06 +00:00
|
|
|
return formatError(_warning, _type, _component, message, formattedMessage, sourceLocation);
|
2017-03-29 20:50:53 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 18:05:39 +00:00
|
|
|
set<string> requestedContractNames(Json::Value const& _outputSelection)
|
|
|
|
{
|
|
|
|
set<string> names;
|
|
|
|
for (auto const& sourceName: _outputSelection.getMemberNames())
|
|
|
|
{
|
|
|
|
for (auto const& contractName: _outputSelection[sourceName].getMemberNames())
|
|
|
|
{
|
|
|
|
/// Consider the "all sources" shortcuts as requesting everything.
|
|
|
|
if (contractName == "*" || contractName == "")
|
|
|
|
return set<string>();
|
|
|
|
names.insert((sourceName == "*" ? "" : sourceName) + ":" + contractName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2017-04-22 13:43:10 +00:00
|
|
|
/// Returns true iff @a _hash (hex with 0x prefix) is the Keccak256 hash of the binary data in @a _content.
|
|
|
|
bool hashMatchesContent(string const& _hash, string const& _content)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return dev::h256(_hash) == dev::keccak256(_content);
|
|
|
|
}
|
2018-05-17 10:16:28 +00:00
|
|
|
catch (dev::BadHexCharacter const&)
|
2017-04-22 13:43:10 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 13:29:30 +00:00
|
|
|
StringMap createSourceList(Json::Value const& _input)
|
|
|
|
{
|
|
|
|
StringMap sources;
|
|
|
|
Json::Value const& jsonSources = _input["sources"];
|
|
|
|
if (jsonSources.isObject())
|
|
|
|
for (auto const& sourceName: jsonSources.getMemberNames())
|
|
|
|
sources[sourceName] = jsonSources[sourceName]["content"].asString();
|
|
|
|
return sources;
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:55:44 +00:00
|
|
|
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _artifact)
|
2017-03-30 10:16:28 +00:00
|
|
|
{
|
2017-11-22 12:55:44 +00:00
|
|
|
for (auto const& artifact: _outputSelection)
|
2017-03-30 10:16:28 +00:00
|
|
|
/// @TODO support sub-matching, e.g "evm" matches "evm.assembly"
|
2017-11-22 12:55:44 +00:00
|
|
|
if (artifact == "*" || artifact == _artifact)
|
2017-03-30 10:16:28 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
2017-11-22 12:55:44 +00:00
|
|
|
/// @a _outputSelection is a JSON object containining a two-level hashmap, where the first level is the filename,
|
|
|
|
/// the second level is the contract name and the value is an array of artifact names to be requested for that contract.
|
2017-03-30 10:16:28 +00:00
|
|
|
/// @a _file is the current file
|
|
|
|
/// @a _contract is the current contract
|
2017-11-22 12:55:44 +00:00
|
|
|
/// @a _artifact is the current artifact name
|
2017-03-30 10:16:28 +00:00
|
|
|
///
|
2017-11-22 12:55:44 +00:00
|
|
|
/// @returns true if the @a _outputSelection has a match for the requested target in the specific file / contract.
|
2017-03-30 10:16:28 +00:00
|
|
|
///
|
2017-11-22 12:55:44 +00:00
|
|
|
/// In @a _outputSelection the use of '*' as a wildcard is permitted.
|
2017-03-30 10:16:28 +00:00
|
|
|
///
|
|
|
|
/// @TODO optimise this. Perhaps flatten the structure upfront.
|
|
|
|
///
|
2017-11-22 12:55:44 +00:00
|
|
|
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _file, string const& _contract, string const& _artifact)
|
2017-03-30 10:16:28 +00:00
|
|
|
{
|
2017-11-22 12:55:44 +00:00
|
|
|
if (!_outputSelection.isObject())
|
2017-03-30 10:16:28 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto const& file: { _file, string("*") })
|
2017-11-22 12:55:44 +00:00
|
|
|
if (_outputSelection.isMember(file) && _outputSelection[file].isObject())
|
2017-03-30 10:16:28 +00:00
|
|
|
{
|
2017-11-22 13:35:01 +00:00
|
|
|
/// For SourceUnit-level targets (such as AST) only allow empty name, otherwise
|
|
|
|
/// for Contract-level targets try both contract name and wildcard
|
|
|
|
vector<string> contracts{ _contract };
|
|
|
|
if (!_contract.empty())
|
|
|
|
contracts.push_back("*");
|
|
|
|
for (auto const& contract: contracts)
|
2017-03-30 10:16:28 +00:00
|
|
|
if (
|
2017-11-22 13:35:01 +00:00
|
|
|
_outputSelection[file].isMember(contract) &&
|
|
|
|
_outputSelection[file][contract].isArray() &&
|
|
|
|
isArtifactRequested(_outputSelection[file][contract], _artifact)
|
2017-03-30 10:16:28 +00:00
|
|
|
)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:55:44 +00:00
|
|
|
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _file, string const& _contract, vector<string> const& _artifacts)
|
2017-10-19 13:09:40 +00:00
|
|
|
{
|
2017-11-22 12:55:44 +00:00
|
|
|
for (auto const& artifact: _artifacts)
|
|
|
|
if (isArtifactRequested(_outputSelection, _file, _contract, artifact))
|
2017-10-19 13:09:40 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-30 01:09:42 +00:00
|
|
|
Json::Value formatLinkReferences(std::map<size_t, std::string> const& linkReferences)
|
|
|
|
{
|
|
|
|
Json::Value ret(Json::objectValue);
|
|
|
|
|
|
|
|
for (auto const& ref: linkReferences)
|
|
|
|
{
|
|
|
|
string const& fullname = ref.second;
|
2018-01-05 13:24:07 +00:00
|
|
|
size_t colon = fullname.rfind(':');
|
2017-03-30 01:09:42 +00:00
|
|
|
solAssert(colon != string::npos, "");
|
|
|
|
string file = fullname.substr(0, colon);
|
|
|
|
string name = fullname.substr(colon + 1);
|
|
|
|
|
|
|
|
Json::Value fileObject = ret.get(file, Json::objectValue);
|
|
|
|
Json::Value libraryArray = fileObject.get(name, Json::arrayValue);
|
|
|
|
|
|
|
|
Json::Value entry = Json::objectValue;
|
|
|
|
entry["start"] = Json::UInt(ref.first);
|
|
|
|
entry["length"] = 20;
|
|
|
|
|
|
|
|
libraryArray.append(entry);
|
|
|
|
fileObject[name] = libraryArray;
|
|
|
|
ret[file] = fileObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 14:52:54 +00:00
|
|
|
Json::Value collectEVMObject(eth::LinkerObject const& _object, string const* _sourceMap)
|
|
|
|
{
|
|
|
|
Json::Value output = Json::objectValue;
|
|
|
|
output["object"] = _object.toHex();
|
|
|
|
output["opcodes"] = solidity::disassemble(_object.bytecode);
|
|
|
|
output["sourceMap"] = _sourceMap ? *_sourceMap : "";
|
|
|
|
output["linkReferences"] = formatLinkReferences(_object.linkReferences);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-04-19 13:11:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:08:12 +00:00
|
|
|
Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
|
2017-02-10 13:18:21 +00:00
|
|
|
{
|
|
|
|
m_compilerStack.reset(false);
|
|
|
|
|
2017-04-19 14:53:11 +00:00
|
|
|
if (!_input.isObject())
|
|
|
|
return formatFatalError("JSONError", "Input is not a JSON object.");
|
|
|
|
|
|
|
|
if (_input["language"] != "Solidity")
|
|
|
|
return formatFatalError("JSONError", "Only \"Solidity\" is supported as a language.");
|
|
|
|
|
2017-02-10 13:18:21 +00:00
|
|
|
Json::Value const& sources = _input["sources"];
|
2018-02-13 20:40:16 +00:00
|
|
|
|
|
|
|
if (!sources.isObject() && !sources.isNull())
|
|
|
|
return formatFatalError("JSONError", "\"sources\" is not a JSON object.");
|
|
|
|
|
|
|
|
if (sources.empty())
|
2017-03-29 14:19:10 +00:00
|
|
|
return formatFatalError("JSONError", "No input sources specified.");
|
2017-02-10 13:18:21 +00:00
|
|
|
|
2017-04-19 15:45:36 +00:00
|
|
|
Json::Value errors = Json::arrayValue;
|
|
|
|
|
2017-02-10 13:18:21 +00:00
|
|
|
for (auto const& sourceName: sources.getMemberNames())
|
2017-04-22 13:43:10 +00:00
|
|
|
{
|
|
|
|
string hash;
|
2017-04-24 21:38:03 +00:00
|
|
|
|
|
|
|
if (!sources[sourceName].isObject())
|
|
|
|
return formatFatalError("JSONError", "Source input is not a JSON object.");
|
|
|
|
|
2017-04-22 13:43:10 +00:00
|
|
|
if (sources[sourceName]["keccak256"].isString())
|
|
|
|
hash = sources[sourceName]["keccak256"].asString();
|
|
|
|
|
2017-03-30 02:17:17 +00:00
|
|
|
if (sources[sourceName]["content"].isString())
|
2017-04-22 13:43:10 +00:00
|
|
|
{
|
|
|
|
string content = sources[sourceName]["content"].asString();
|
|
|
|
if (!hash.empty() && !hashMatchesContent(hash, content))
|
|
|
|
errors.append(formatError(
|
|
|
|
false,
|
|
|
|
"IOError",
|
|
|
|
"general",
|
|
|
|
"Mismatch between content and supplied hash for \"" + sourceName + "\""
|
|
|
|
));
|
|
|
|
else
|
|
|
|
m_compilerStack.addSource(sourceName, content);
|
|
|
|
}
|
2017-03-30 02:17:17 +00:00
|
|
|
else if (sources[sourceName]["urls"].isArray())
|
2017-04-19 15:45:36 +00:00
|
|
|
{
|
|
|
|
if (!m_readFile)
|
|
|
|
return formatFatalError("JSONError", "No import callback supplied, but URL is requested.");
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
vector<string> failures;
|
|
|
|
|
|
|
|
for (auto const& url: sources[sourceName]["urls"])
|
|
|
|
{
|
2017-07-13 19:06:04 +00:00
|
|
|
ReadCallback::Result result = m_readFile(url.asString());
|
2017-04-19 15:45:36 +00:00
|
|
|
if (result.success)
|
|
|
|
{
|
2017-07-13 19:06:04 +00:00
|
|
|
if (!hash.empty() && !hashMatchesContent(hash, result.responseOrErrorMessage))
|
2017-04-22 13:43:10 +00:00
|
|
|
errors.append(formatError(
|
|
|
|
false,
|
|
|
|
"IOError",
|
|
|
|
"general",
|
|
|
|
"Mismatch between content and supplied hash for \"" + sourceName + "\" at \"" + url.asString() + "\""
|
|
|
|
));
|
|
|
|
else
|
|
|
|
{
|
2017-07-13 19:06:04 +00:00
|
|
|
m_compilerStack.addSource(sourceName, result.responseOrErrorMessage);
|
2017-04-22 13:43:10 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-19 15:45:36 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-13 19:06:04 +00:00
|
|
|
failures.push_back("Cannot import url (\"" + url.asString() + "\"): " + result.responseOrErrorMessage);
|
2017-04-19 15:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& failure: failures)
|
|
|
|
{
|
|
|
|
/// If the import succeeded, let mark all the others as warnings, otherwise all of them are errors.
|
|
|
|
errors.append(formatError(
|
|
|
|
found ? true : false,
|
|
|
|
"IOError",
|
|
|
|
"general",
|
|
|
|
failure
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 02:17:17 +00:00
|
|
|
else
|
|
|
|
return formatFatalError("JSONError", "Invalid input source specified.");
|
2017-04-22 13:43:10 +00:00
|
|
|
}
|
2017-02-10 13:18:21 +00:00
|
|
|
|
2017-03-17 12:29:57 +00:00
|
|
|
Json::Value const& settings = _input.get("settings", Json::Value());
|
|
|
|
|
2018-02-21 22:43:40 +00:00
|
|
|
if (settings.isMember("evmVersion"))
|
|
|
|
{
|
|
|
|
boost::optional<EVMVersion> version = EVMVersion::fromString(settings.get("evmVersion", {}).asString());
|
|
|
|
if (!version)
|
|
|
|
return formatFatalError("JSONError", "Invalid EVM version requested.");
|
|
|
|
m_compilerStack.setEVMVersion(*version);
|
|
|
|
}
|
|
|
|
|
2018-08-09 18:37:49 +00:00
|
|
|
vector<CompilerStack::Remapping> remappings;
|
2017-03-17 12:30:50 +00:00
|
|
|
for (auto const& remapping: settings.get("remappings", Json::Value()))
|
2018-08-09 18:37:49 +00:00
|
|
|
{
|
|
|
|
if (auto r = CompilerStack::parseRemapping(remapping.asString()))
|
|
|
|
remappings.emplace_back(std::move(*r));
|
|
|
|
else
|
|
|
|
return formatFatalError("JSONError", "Invalid remapping: \"" + remapping.asString() + "\"");
|
|
|
|
}
|
2017-03-17 12:30:50 +00:00
|
|
|
m_compilerStack.setRemappings(remappings);
|
|
|
|
|
2017-03-17 12:29:57 +00:00
|
|
|
Json::Value optimizerSettings = settings.get("optimizer", Json::Value());
|
2017-07-18 13:44:53 +00:00
|
|
|
bool const optimize = optimizerSettings.get("enabled", Json::Value(false)).asBool();
|
|
|
|
unsigned const optimizeRuns = optimizerSettings.get("runs", Json::Value(200u)).asUInt();
|
2017-07-17 10:54:02 +00:00
|
|
|
m_compilerStack.setOptimiserSettings(optimize, optimizeRuns);
|
2017-03-17 12:29:57 +00:00
|
|
|
|
2017-02-10 13:18:21 +00:00
|
|
|
map<string, h160> libraries;
|
2018-02-28 09:36:07 +00:00
|
|
|
Json::Value jsonLibraries = settings.get("libraries", Json::Value(Json::objectValue));
|
|
|
|
if (!jsonLibraries.isObject())
|
|
|
|
return formatFatalError("JSONError", "\"libraries\" is not a JSON object.");
|
2017-03-17 12:30:29 +00:00
|
|
|
for (auto const& sourceName: jsonLibraries.getMemberNames())
|
|
|
|
{
|
|
|
|
auto const& jsonSourceName = jsonLibraries[sourceName];
|
2018-02-28 09:36:07 +00:00
|
|
|
if (!jsonSourceName.isObject())
|
|
|
|
return formatFatalError("JSONError", "library entry is not a JSON object.");
|
2017-03-17 12:30:29 +00:00
|
|
|
for (auto const& library: jsonSourceName.getMemberNames())
|
2018-02-28 09:44:48 +00:00
|
|
|
{
|
2018-02-28 15:57:35 +00:00
|
|
|
string address = jsonSourceName[library].asString();
|
|
|
|
|
|
|
|
if (!boost::starts_with(address, "0x"))
|
|
|
|
return formatFatalError(
|
|
|
|
"JSONError",
|
|
|
|
"Library address is not prefixed with \"0x\"."
|
|
|
|
);
|
|
|
|
|
|
|
|
if (address.length() != 42)
|
|
|
|
return formatFatalError(
|
|
|
|
"JSONError",
|
|
|
|
"Library address is of invalid length."
|
|
|
|
);
|
|
|
|
|
2018-02-28 09:44:48 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// @TODO use libraries only for the given source
|
2018-02-28 15:57:35 +00:00
|
|
|
libraries[library] = h160(address);
|
2018-02-28 09:44:48 +00:00
|
|
|
}
|
2018-05-17 10:16:28 +00:00
|
|
|
catch (dev::BadHexCharacter const&)
|
2018-02-28 09:44:48 +00:00
|
|
|
{
|
|
|
|
return formatFatalError(
|
|
|
|
"JSONError",
|
2018-02-28 15:57:35 +00:00
|
|
|
"Invalid library address (\"" + address + "\") supplied."
|
2018-02-28 09:44:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 12:30:29 +00:00
|
|
|
}
|
2017-07-17 10:49:45 +00:00
|
|
|
m_compilerStack.setLibraries(libraries);
|
2017-02-10 13:18:21 +00:00
|
|
|
|
2017-03-30 01:41:38 +00:00
|
|
|
Json::Value metadataSettings = settings.get("metadata", Json::Value());
|
|
|
|
m_compilerStack.useMetadataLiteralSources(metadataSettings.get("useLiteralContent", Json::Value(false)).asBool());
|
|
|
|
|
2017-09-29 18:05:39 +00:00
|
|
|
Json::Value outputSelection = settings.get("outputSelection", Json::Value());
|
|
|
|
m_compilerStack.setRequestedContractNames(requestedContractNames(outputSelection));
|
|
|
|
|
2017-02-10 13:18:21 +00:00
|
|
|
auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compilerStack.scanner(_sourceName); };
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-07-17 10:54:02 +00:00
|
|
|
m_compilerStack.compile();
|
2017-03-29 20:50:53 +00:00
|
|
|
|
|
|
|
for (auto const& error: m_compilerStack.errors())
|
|
|
|
{
|
2017-07-05 15:02:02 +00:00
|
|
|
Error const& err = dynamic_cast<Error const&>(*error);
|
2017-03-29 20:50:53 +00:00
|
|
|
|
|
|
|
errors.append(formatErrorWithException(
|
|
|
|
*error,
|
2017-07-05 15:02:02 +00:00
|
|
|
err.type() == Error::Type::Warning,
|
|
|
|
err.typeName(),
|
2017-03-29 20:50:53 +00:00
|
|
|
"general",
|
|
|
|
"",
|
|
|
|
scannerFromSourceName
|
|
|
|
));
|
|
|
|
}
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
2017-07-26 21:52:11 +00:00
|
|
|
/// This is only thrown in a very few locations.
|
2017-02-10 13:18:21 +00:00
|
|
|
catch (Error const& _error)
|
|
|
|
{
|
2017-07-26 21:53:54 +00:00
|
|
|
errors.append(formatErrorWithException(
|
|
|
|
_error,
|
|
|
|
false,
|
|
|
|
_error.typeName(),
|
|
|
|
"general",
|
|
|
|
"Uncaught error: ",
|
|
|
|
scannerFromSourceName
|
|
|
|
));
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
2017-07-26 21:52:11 +00:00
|
|
|
/// This should not be leaked from compile().
|
|
|
|
catch (FatalError const& _exception)
|
|
|
|
{
|
|
|
|
errors.append(formatError(
|
|
|
|
false,
|
|
|
|
"FatalError",
|
|
|
|
"general",
|
|
|
|
"Uncaught fatal error: " + boost::diagnostic_information(_exception)
|
|
|
|
));
|
|
|
|
}
|
2017-02-10 13:18:21 +00:00
|
|
|
catch (CompilerError const& _exception)
|
|
|
|
{
|
2017-03-29 20:50:53 +00:00
|
|
|
errors.append(formatErrorWithException(
|
|
|
|
_exception,
|
|
|
|
false,
|
|
|
|
"CompilerError",
|
|
|
|
"general",
|
|
|
|
"Compiler error (" + _exception.lineInfo() + ")",
|
|
|
|
scannerFromSourceName
|
|
|
|
));
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
|
|
|
catch (InternalCompilerError const& _exception)
|
|
|
|
{
|
2017-03-29 20:50:53 +00:00
|
|
|
errors.append(formatErrorWithException(
|
|
|
|
_exception,
|
|
|
|
false,
|
|
|
|
"InternalCompilerError",
|
|
|
|
"general",
|
2017-07-26 21:52:11 +00:00
|
|
|
"Internal compiler error (" + _exception.lineInfo() + ")",
|
|
|
|
scannerFromSourceName
|
2017-03-29 20:50:53 +00:00
|
|
|
));
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
|
|
|
catch (UnimplementedFeatureError const& _exception)
|
|
|
|
{
|
2017-03-29 20:50:53 +00:00
|
|
|
errors.append(formatErrorWithException(
|
|
|
|
_exception,
|
|
|
|
false,
|
|
|
|
"UnimplementedFeatureError",
|
|
|
|
"general",
|
|
|
|
"Unimplemented feature (" + _exception.lineInfo() + ")",
|
2017-07-26 21:52:11 +00:00
|
|
|
scannerFromSourceName
|
|
|
|
));
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
|
|
|
catch (Exception const& _exception)
|
|
|
|
{
|
2017-03-29 20:50:53 +00:00
|
|
|
errors.append(formatError(
|
|
|
|
false,
|
|
|
|
"Exception",
|
|
|
|
"general",
|
|
|
|
"Exception during compilation: " + boost::diagnostic_information(_exception)
|
|
|
|
));
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2017-03-29 20:50:53 +00:00
|
|
|
errors.append(formatError(
|
|
|
|
false,
|
|
|
|
"Exception",
|
|
|
|
"general",
|
|
|
|
"Unknown exception during compilation."
|
|
|
|
));
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 13:44:53 +00:00
|
|
|
bool const analysisSuccess = m_compilerStack.state() >= CompilerStack::State::AnalysisSuccessful;
|
|
|
|
bool const compilationSuccess = m_compilerStack.state() == CompilerStack::State::CompilationSuccessful;
|
2017-07-03 11:02:06 +00:00
|
|
|
|
2017-03-29 20:50:53 +00:00
|
|
|
/// Inconsistent state - stop here to receive error reports from users
|
2017-07-03 11:02:06 +00:00
|
|
|
if (!compilationSuccess && (errors.size() == 0))
|
2017-03-29 20:50:53 +00:00
|
|
|
return formatFatalError("InternalCompilerError", "No error reported, but compilation failed.");
|
|
|
|
|
2017-07-18 13:43:10 +00:00
|
|
|
Json::Value output = Json::objectValue;
|
|
|
|
|
|
|
|
if (errors.size() > 0)
|
|
|
|
output["errors"] = errors;
|
|
|
|
|
2017-03-29 13:54:34 +00:00
|
|
|
output["sources"] = Json::objectValue;
|
|
|
|
unsigned sourceIndex = 0;
|
2017-07-18 13:43:10 +00:00
|
|
|
for (string const& sourceName: analysisSuccess ? m_compilerStack.sourceNames() : vector<string>())
|
2017-03-29 13:54:34 +00:00
|
|
|
{
|
|
|
|
Json::Value sourceResult = Json::objectValue;
|
|
|
|
sourceResult["id"] = sourceIndex++;
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, sourceName, "", "ast"))
|
2017-04-12 09:37:04 +00:00
|
|
|
sourceResult["ast"] = ASTJsonConverter(false, m_compilerStack.sourceIndices()).toJson(m_compilerStack.ast(sourceName));
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, sourceName, "", "legacyAST"))
|
2017-04-12 09:37:04 +00:00
|
|
|
sourceResult["legacyAST"] = ASTJsonConverter(true, m_compilerStack.sourceIndices()).toJson(m_compilerStack.ast(sourceName));
|
2017-07-18 13:43:10 +00:00
|
|
|
output["sources"][sourceName] = sourceResult;
|
2017-03-29 13:54:34 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 13:18:21 +00:00
|
|
|
Json::Value contractsOutput = Json::objectValue;
|
2017-07-03 11:02:06 +00:00
|
|
|
for (string const& contractName: compilationSuccess ? m_compilerStack.contractNames() : vector<string>())
|
2017-02-10 13:18:21 +00:00
|
|
|
{
|
2018-01-03 11:34:48 +00:00
|
|
|
size_t colon = contractName.rfind(':');
|
2017-03-30 01:32:43 +00:00
|
|
|
solAssert(colon != string::npos, "");
|
|
|
|
string file = contractName.substr(0, colon);
|
|
|
|
string name = contractName.substr(colon + 1);
|
|
|
|
|
2017-02-10 13:18:21 +00:00
|
|
|
// ABI, documentation and metadata
|
|
|
|
Json::Value contractData(Json::objectValue);
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "abi"))
|
2017-04-12 09:37:04 +00:00
|
|
|
contractData["abi"] = m_compilerStack.contractABI(contractName);
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "metadata"))
|
2017-04-12 09:37:04 +00:00
|
|
|
contractData["metadata"] = m_compilerStack.metadata(contractName);
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "userdoc"))
|
2017-04-12 09:37:04 +00:00
|
|
|
contractData["userdoc"] = m_compilerStack.natspecUser(contractName);
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "devdoc"))
|
2017-04-12 09:37:04 +00:00
|
|
|
contractData["devdoc"] = m_compilerStack.natspecDev(contractName);
|
2017-02-10 13:18:21 +00:00
|
|
|
|
|
|
|
// EVM
|
|
|
|
Json::Value evmData(Json::objectValue);
|
|
|
|
// @TODO: add ir
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "evm.assembly"))
|
2017-04-12 09:37:04 +00:00
|
|
|
evmData["assembly"] = m_compilerStack.assemblyString(contractName, createSourceList(_input));
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "evm.legacyAssembly"))
|
2017-04-12 09:37:04 +00:00
|
|
|
evmData["legacyAssembly"] = m_compilerStack.assemblyJSON(contractName, createSourceList(_input));
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "evm.methodIdentifiers"))
|
2017-04-12 09:37:04 +00:00
|
|
|
evmData["methodIdentifiers"] = m_compilerStack.methodIdentifiers(contractName);
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(outputSelection, file, name, "evm.gasEstimates"))
|
2017-04-12 09:37:04 +00:00
|
|
|
evmData["gasEstimates"] = m_compilerStack.gasEstimates(contractName);
|
|
|
|
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(
|
2017-10-19 13:09:40 +00:00
|
|
|
outputSelection,
|
|
|
|
file,
|
|
|
|
name,
|
|
|
|
{ "evm.bytecode", "evm.bytecode.object", "evm.bytecode.opcodes", "evm.bytecode.sourceMap", "evm.bytecode.linkReferences" }
|
|
|
|
))
|
2017-04-12 09:37:04 +00:00
|
|
|
evmData["bytecode"] = collectEVMObject(
|
|
|
|
m_compilerStack.object(contractName),
|
|
|
|
m_compilerStack.sourceMapping(contractName)
|
|
|
|
);
|
|
|
|
|
2017-11-22 12:55:44 +00:00
|
|
|
if (isArtifactRequested(
|
2017-10-19 13:09:40 +00:00
|
|
|
outputSelection,
|
|
|
|
file,
|
|
|
|
name,
|
|
|
|
{ "evm.deployedBytecode", "evm.deployedBytecode.object", "evm.deployedBytecode.opcodes", "evm.deployedBytecode.sourceMap", "evm.deployedBytecode.linkReferences" }
|
|
|
|
))
|
2017-04-12 09:37:04 +00:00
|
|
|
evmData["deployedBytecode"] = collectEVMObject(
|
|
|
|
m_compilerStack.runtimeObject(contractName),
|
|
|
|
m_compilerStack.runtimeSourceMapping(contractName)
|
|
|
|
);
|
2017-02-10 13:18:21 +00:00
|
|
|
|
|
|
|
contractData["evm"] = evmData;
|
|
|
|
|
2017-03-30 01:32:43 +00:00
|
|
|
if (!contractsOutput.isMember(file))
|
|
|
|
contractsOutput[file] = Json::objectValue;
|
|
|
|
|
|
|
|
contractsOutput[file][name] = contractData;
|
2017-02-10 13:18:21 +00:00
|
|
|
}
|
2017-03-30 01:32:43 +00:00
|
|
|
output["contracts"] = contractsOutput;
|
2017-02-10 13:18:21 +00:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:22:33 +00:00
|
|
|
Json::Value StandardCompiler::compile(Json::Value const& _input) noexcept
|
2017-03-17 12:08:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return compileInternal(_input);
|
|
|
|
}
|
2017-04-24 21:49:50 +00:00
|
|
|
catch (Json::LogicError const& _exception)
|
|
|
|
{
|
|
|
|
return formatFatalError("InternalCompilerError", string("JSON logic exception: ") + _exception.what());
|
|
|
|
}
|
|
|
|
catch (Json::RuntimeError const& _exception)
|
|
|
|
{
|
|
|
|
return formatFatalError("InternalCompilerError", string("JSON runtime exception: ") + _exception.what());
|
|
|
|
}
|
2017-04-23 18:43:06 +00:00
|
|
|
catch (Exception const& _exception)
|
|
|
|
{
|
|
|
|
return formatFatalError("InternalCompilerError", "Internal exception in StandardCompiler::compileInternal: " + boost::diagnostic_information(_exception));
|
|
|
|
}
|
2017-03-17 12:08:12 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
2017-04-23 18:43:06 +00:00
|
|
|
return formatFatalError("InternalCompilerError", "Internal exception in StandardCompiler::compileInternal");
|
2017-03-17 12:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:22:33 +00:00
|
|
|
string StandardCompiler::compile(string const& _input) noexcept
|
2017-02-09 21:56:49 +00:00
|
|
|
{
|
2017-02-10 13:18:21 +00:00
|
|
|
Json::Value input;
|
2018-02-07 01:05:20 +00:00
|
|
|
string errors;
|
2017-03-29 12:34:16 +00:00
|
|
|
try
|
|
|
|
{
|
2018-02-07 01:05:20 +00:00
|
|
|
if (!jsonParseStrict(_input, input, &errors))
|
|
|
|
return jsonCompactPrint(formatFatalError("JSONError", errors));
|
2017-03-29 12:34:16 +00:00
|
|
|
}
|
2018-05-17 12:57:21 +00:00
|
|
|
catch (...)
|
2017-02-10 13:18:21 +00:00
|
|
|
{
|
|
|
|
return "{\"errors\":\"[{\"type\":\"JSONError\",\"component\":\"general\",\"severity\":\"error\",\"message\":\"Error parsing input JSON.\"}]}";
|
|
|
|
}
|
|
|
|
|
|
|
|
// cout << "Input: " << input.toStyledString() << endl;
|
|
|
|
Json::Value output = compile(input);
|
|
|
|
// cout << "Output: " << output.toStyledString() << endl;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return jsonCompactPrint(output);
|
|
|
|
}
|
2018-05-17 12:57:21 +00:00
|
|
|
catch (...)
|
2017-02-10 13:18:21 +00:00
|
|
|
{
|
|
|
|
return "{\"errors\":\"[{\"type\":\"JSONError\",\"component\":\"general\",\"severity\":\"error\",\"message\":\"Error writing output JSON.\"}]}";
|
|
|
|
}
|
2017-02-09 21:56:49 +00:00
|
|
|
}
|