Some more compiler state tests.

This commit is contained in:
chriseth 2018-12-18 18:27:38 +01:00
parent 8875092073
commit 4e89945d82

View File

@ -389,18 +389,27 @@ string const CompilerStack::lastContractName() const
eth::AssemblyItems const* CompilerStack::assemblyItems(string const& _contractName) const eth::AssemblyItems const* CompilerStack::assemblyItems(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
Contract const& currentContract = contract(_contractName); Contract const& currentContract = contract(_contractName);
return currentContract.compiler ? &contract(_contractName).compiler->assemblyItems() : nullptr; return currentContract.compiler ? &contract(_contractName).compiler->assemblyItems() : nullptr;
} }
eth::AssemblyItems const* CompilerStack::runtimeAssemblyItems(string const& _contractName) const eth::AssemblyItems const* CompilerStack::runtimeAssemblyItems(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
Contract const& currentContract = contract(_contractName); Contract const& currentContract = contract(_contractName);
return currentContract.compiler ? &contract(_contractName).compiler->runtimeAssemblyItems() : nullptr; return currentContract.compiler ? &contract(_contractName).compiler->runtimeAssemblyItems() : nullptr;
} }
string const* CompilerStack::sourceMapping(string const& _contractName) const string const* CompilerStack::sourceMapping(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
Contract const& c = contract(_contractName); Contract const& c = contract(_contractName);
if (!c.sourceMapping) if (!c.sourceMapping)
{ {
@ -412,6 +421,9 @@ string const* CompilerStack::sourceMapping(string const& _contractName) const
string const* CompilerStack::runtimeSourceMapping(string const& _contractName) const string const* CompilerStack::runtimeSourceMapping(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
Contract const& c = contract(_contractName); Contract const& c = contract(_contractName);
if (!c.runtimeSourceMapping) if (!c.runtimeSourceMapping)
{ {
@ -447,17 +459,26 @@ std::string const CompilerStack::filesystemFriendlyName(string const& _contractN
eth::LinkerObject const& CompilerStack::object(string const& _contractName) const eth::LinkerObject const& CompilerStack::object(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
return contract(_contractName).object; return contract(_contractName).object;
} }
eth::LinkerObject const& CompilerStack::runtimeObject(string const& _contractName) const eth::LinkerObject const& CompilerStack::runtimeObject(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
return contract(_contractName).runtimeObject; return contract(_contractName).runtimeObject;
} }
/// FIXME: cache this string /// FIXME: cache this string
string CompilerStack::assemblyString(string const& _contractName, StringMap _sourceCodes) const string CompilerStack::assemblyString(string const& _contractName, StringMap _sourceCodes) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
Contract const& currentContract = contract(_contractName); Contract const& currentContract = contract(_contractName);
if (currentContract.compiler) if (currentContract.compiler)
return currentContract.compiler->assemblyString(_sourceCodes); return currentContract.compiler->assemblyString(_sourceCodes);
@ -468,6 +489,9 @@ string CompilerStack::assemblyString(string const& _contractName, StringMap _sou
/// FIXME: cache the JSON /// FIXME: cache the JSON
Json::Value CompilerStack::assemblyJSON(string const& _contractName, StringMap _sourceCodes) const Json::Value CompilerStack::assemblyJSON(string const& _contractName, StringMap _sourceCodes) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
Contract const& currentContract = contract(_contractName); Contract const& currentContract = contract(_contractName);
if (currentContract.compiler) if (currentContract.compiler)
return currentContract.compiler->assemblyJSON(_sourceCodes); return currentContract.compiler->assemblyJSON(_sourceCodes);
@ -494,13 +518,16 @@ map<string, unsigned> CompilerStack::sourceIndices() const
Json::Value const& CompilerStack::contractABI(string const& _contractName) const Json::Value const& CompilerStack::contractABI(string const& _contractName) const
{ {
if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
return contractABI(contract(_contractName)); return contractABI(contract(_contractName));
} }
Json::Value const& CompilerStack::contractABI(Contract const& _contract) const Json::Value const& CompilerStack::contractABI(Contract const& _contract) const
{ {
if (m_stackState < AnalysisSuccessful) if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
solAssert(_contract.contract, ""); solAssert(_contract.contract, "");
@ -513,13 +540,16 @@ Json::Value const& CompilerStack::contractABI(Contract const& _contract) const
Json::Value const& CompilerStack::natspecUser(string const& _contractName) const Json::Value const& CompilerStack::natspecUser(string const& _contractName) const
{ {
if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
return natspecUser(contract(_contractName)); return natspecUser(contract(_contractName));
} }
Json::Value const& CompilerStack::natspecUser(Contract const& _contract) const Json::Value const& CompilerStack::natspecUser(Contract const& _contract) const
{ {
if (m_stackState < AnalysisSuccessful) if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
solAssert(_contract.contract, ""); solAssert(_contract.contract, "");
@ -532,13 +562,16 @@ Json::Value const& CompilerStack::natspecUser(Contract const& _contract) const
Json::Value const& CompilerStack::natspecDev(string const& _contractName) const Json::Value const& CompilerStack::natspecDev(string const& _contractName) const
{ {
if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
return natspecDev(contract(_contractName)); return natspecDev(contract(_contractName));
} }
Json::Value const& CompilerStack::natspecDev(Contract const& _contract) const Json::Value const& CompilerStack::natspecDev(Contract const& _contract) const
{ {
if (m_stackState < AnalysisSuccessful) if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
solAssert(_contract.contract, ""); solAssert(_contract.contract, "");
@ -551,6 +584,9 @@ Json::Value const& CompilerStack::natspecDev(Contract const& _contract) const
Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const
{ {
if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
Json::Value methodIdentifiers(Json::objectValue); Json::Value methodIdentifiers(Json::objectValue);
for (auto const& it: contractDefinition(_contractName).interfaceFunctions()) for (auto const& it: contractDefinition(_contractName).interfaceFunctions())
methodIdentifiers[it.second->externalSignature()] = it.first.hex(); methodIdentifiers[it.second->externalSignature()] = it.first.hex();
@ -583,8 +619,8 @@ SourceUnit const& CompilerStack::ast(string const& _sourceName) const
ContractDefinition const& CompilerStack::contractDefinition(string const& _contractName) const ContractDefinition const& CompilerStack::contractDefinition(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful) if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Analysis was not successful."));
return *contract(_contractName).contract; return *contract(_contractName).contract;
} }
@ -594,6 +630,9 @@ size_t CompilerStack::functionEntryPoint(
FunctionDefinition const& _function FunctionDefinition const& _function
) const ) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
shared_ptr<Compiler> const& compiler = contract(_contractName).compiler; shared_ptr<Compiler> const& compiler = contract(_contractName).compiler;
if (!compiler) if (!compiler)
return 0; return 0;
@ -936,6 +975,9 @@ bytes CompilerStack::createCBORMetadata(string const& _metadata, bool _experimen
string CompilerStack::computeSourceMapping(eth::AssemblyItems const& _items) const string CompilerStack::computeSourceMapping(eth::AssemblyItems const& _items) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
string ret; string ret;
map<string, unsigned> sourceIndicesMap = sourceIndices(); map<string, unsigned> sourceIndicesMap = sourceIndices();
int prevStart = -1; int prevStart = -1;
@ -1022,6 +1064,9 @@ Json::Value gasToJson(GasEstimator::GasConsumption const& _gas)
Json::Value CompilerStack::gasEstimates(string const& _contractName) const Json::Value CompilerStack::gasEstimates(string const& _contractName) const
{ {
if (m_stackState != CompilationSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
if (!assemblyItems(_contractName) && !runtimeAssemblyItems(_contractName)) if (!assemblyItems(_contractName) && !runtimeAssemblyItems(_contractName))
return Json::Value(); return Json::Value();