mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Implement CompilerStack.lastContractName()
This commit is contained in:
parent
039cc25b1f
commit
2ce35b77be
@ -747,22 +747,28 @@ void CompilerStack::compileContract(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string const CompilerStack::lastContractName() const
|
||||||
|
{
|
||||||
|
if (m_contracts.empty())
|
||||||
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("No compiled contracts found."));
|
||||||
|
// try to find some user-supplied contract
|
||||||
|
string contractName;
|
||||||
|
for (auto const& it: m_sources)
|
||||||
|
for (ASTPointer<ASTNode> const& node: it.second.ast->nodes())
|
||||||
|
if (auto contract = dynamic_cast<ContractDefinition const*>(node.get()))
|
||||||
|
contractName = contract->fullyQualifiedName();
|
||||||
|
return contractName;
|
||||||
|
}
|
||||||
|
|
||||||
CompilerStack::Contract const& CompilerStack::contract(string const& _contractName) const
|
CompilerStack::Contract const& CompilerStack::contract(string const& _contractName) const
|
||||||
{
|
{
|
||||||
if (m_contracts.empty())
|
if (m_contracts.empty())
|
||||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("No compiled contracts found."));
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("No compiled contracts found."));
|
||||||
string contractName = _contractName;
|
auto it = m_contracts.find(_contractName);
|
||||||
if (_contractName.empty())
|
|
||||||
// try to find some user-supplied contract
|
|
||||||
for (auto const& it: m_sources)
|
|
||||||
for (ASTPointer<ASTNode> const& node: it.second.ast->nodes())
|
|
||||||
if (auto contract = dynamic_cast<ContractDefinition const*>(node.get()))
|
|
||||||
contractName = contract->fullyQualifiedName();
|
|
||||||
auto it = m_contracts.find(contractName);
|
|
||||||
// To provide a measure of backward-compatibility, if a contract is not located by its
|
// To provide a measure of backward-compatibility, if a contract is not located by its
|
||||||
// fully-qualified name, a lookup will be attempted purely on the contract's name to see
|
// fully-qualified name, a lookup will be attempted purely on the contract's name to see
|
||||||
// if anything will satisfy.
|
// if anything will satisfy.
|
||||||
if (it == m_contracts.end() && contractName.find(":") == string::npos)
|
if (it == m_contracts.end() && _contractName.find(":") == string::npos)
|
||||||
{
|
{
|
||||||
for (auto const& contractEntry: m_contracts)
|
for (auto const& contractEntry: m_contracts)
|
||||||
{
|
{
|
||||||
@ -773,7 +779,7 @@ CompilerStack::Contract const& CompilerStack::contract(string const& _contractNa
|
|||||||
string foundName;
|
string foundName;
|
||||||
getline(ss, source, ':');
|
getline(ss, source, ':');
|
||||||
getline(ss, foundName, ':');
|
getline(ss, foundName, ':');
|
||||||
if (foundName == contractName) return contractEntry.second;
|
if (foundName == _contractName) return contractEntry.second;
|
||||||
}
|
}
|
||||||
// If we get here, both lookup methods failed.
|
// If we get here, both lookup methods failed.
|
||||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Contract " + _contractName + " not found."));
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Contract " + _contractName + " not found."));
|
||||||
|
@ -168,6 +168,9 @@ public:
|
|||||||
/// @returns a list of the contract names in the sources.
|
/// @returns a list of the contract names in the sources.
|
||||||
std::vector<std::string> contractNames() const;
|
std::vector<std::string> contractNames() const;
|
||||||
|
|
||||||
|
/// @returns the name of the last contract.
|
||||||
|
std::string const lastContractName() const;
|
||||||
|
|
||||||
/// @returns either the contract's name or a mixture of its name and source file, sanitized for filesystem use
|
/// @returns either the contract's name or a mixture of its name and source file, sanitized for filesystem use
|
||||||
std::string const filesystemFriendlyName(std::string const& _contractName) const;
|
std::string const filesystemFriendlyName(std::string const& _contractName) const;
|
||||||
|
|
||||||
|
@ -952,9 +952,9 @@ void CommandLineInterface::handleAst(string const& _argStr)
|
|||||||
asts.push_back(&m_compiler->ast(sourceCode.first));
|
asts.push_back(&m_compiler->ast(sourceCode.first));
|
||||||
map<ASTNode const*, eth::GasMeter::GasConsumption> gasCosts;
|
map<ASTNode const*, eth::GasMeter::GasConsumption> gasCosts;
|
||||||
// FIXME: shouldn't this be done for every contract?
|
// FIXME: shouldn't this be done for every contract?
|
||||||
if (m_compiler->runtimeAssemblyItems(""))
|
if (m_compiler->runtimeAssemblyItems(m_compiler->lastContractName()))
|
||||||
gasCosts = GasEstimator::breakToStatementLevel(
|
gasCosts = GasEstimator::breakToStatementLevel(
|
||||||
GasEstimator::structuralEstimation(*m_compiler->runtimeAssemblyItems(""), asts),
|
GasEstimator::structuralEstimation(*m_compiler->runtimeAssemblyItems(m_compiler->lastContractName()), asts),
|
||||||
asts
|
asts
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ public:
|
|||||||
m_compiler.setOptimiserSettings(dev::test::Options::get().optimize);
|
m_compiler.setOptimiserSettings(dev::test::Options::get().optimize);
|
||||||
BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
|
BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
|
||||||
|
|
||||||
AssemblyItems const* items = m_compiler.runtimeAssemblyItems("");
|
AssemblyItems const* items = m_compiler.runtimeAssemblyItems(m_compiler.lastContractName());
|
||||||
ASTNode const& sourceUnit = m_compiler.ast("");
|
ASTNode const& sourceUnit = m_compiler.ast("");
|
||||||
BOOST_REQUIRE(items != nullptr);
|
BOOST_REQUIRE(items != nullptr);
|
||||||
m_gasCosts = GasEstimator::breakToStatementLevel(
|
m_gasCosts = GasEstimator::breakToStatementLevel(
|
||||||
@ -64,13 +64,13 @@ public:
|
|||||||
{
|
{
|
||||||
compileAndRun(_sourceCode);
|
compileAndRun(_sourceCode);
|
||||||
auto state = make_shared<KnownState>();
|
auto state = make_shared<KnownState>();
|
||||||
PathGasMeter meter(*m_compiler.assemblyItems(""));
|
PathGasMeter meter(*m_compiler.assemblyItems(m_compiler.lastContractName()));
|
||||||
GasMeter::GasConsumption gas = meter.estimateMax(0, state);
|
GasMeter::GasConsumption gas = meter.estimateMax(0, state);
|
||||||
u256 bytecodeSize(m_compiler.runtimeObject("").bytecode.size());
|
u256 bytecodeSize(m_compiler.runtimeObject(m_compiler.lastContractName()).bytecode.size());
|
||||||
// costs for deployment
|
// costs for deployment
|
||||||
gas += bytecodeSize * GasCosts::createDataGas;
|
gas += bytecodeSize * GasCosts::createDataGas;
|
||||||
// costs for transaction
|
// costs for transaction
|
||||||
gas += gasForTransaction(m_compiler.object("").bytecode, true);
|
gas += gasForTransaction(m_compiler.object(m_compiler.lastContractName()).bytecode, true);
|
||||||
|
|
||||||
BOOST_REQUIRE(!gas.isInfinite);
|
BOOST_REQUIRE(!gas.isInfinite);
|
||||||
BOOST_CHECK(gas.value == m_gasUsed);
|
BOOST_CHECK(gas.value == m_gasUsed);
|
||||||
@ -91,7 +91,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
gas += GasEstimator::functionalEstimation(
|
gas += GasEstimator::functionalEstimation(
|
||||||
*m_compiler.runtimeAssemblyItems(""),
|
*m_compiler.runtimeAssemblyItems(m_compiler.lastContractName()),
|
||||||
_sig
|
_sig
|
||||||
);
|
);
|
||||||
BOOST_REQUIRE(!gas.isInfinite);
|
BOOST_REQUIRE(!gas.isInfinite);
|
||||||
|
@ -46,7 +46,7 @@ public:
|
|||||||
m_compilerStack.addSource("", "pragma solidity >=0.0;\n" + _code);
|
m_compilerStack.addSource("", "pragma solidity >=0.0;\n" + _code);
|
||||||
BOOST_REQUIRE_MESSAGE(m_compilerStack.parseAndAnalyze(), "Parsing contract failed");
|
BOOST_REQUIRE_MESSAGE(m_compilerStack.parseAndAnalyze(), "Parsing contract failed");
|
||||||
|
|
||||||
Json::Value generatedInterface = m_compilerStack.contractABI("");
|
Json::Value generatedInterface = m_compilerStack.contractABI(m_compilerStack.lastContractName());
|
||||||
Json::Value expectedInterface;
|
Json::Value expectedInterface;
|
||||||
BOOST_REQUIRE(m_reader.parse(_expectedInterfaceString, expectedInterface));
|
BOOST_REQUIRE(m_reader.parse(_expectedInterfaceString, expectedInterface));
|
||||||
BOOST_CHECK_MESSAGE(
|
BOOST_CHECK_MESSAGE(
|
||||||
|
@ -69,7 +69,7 @@ public:
|
|||||||
);
|
);
|
||||||
BOOST_ERROR("Compiling contract failed");
|
BOOST_ERROR("Compiling contract failed");
|
||||||
}
|
}
|
||||||
eth::LinkerObject obj = m_compiler.object(_contractName);
|
eth::LinkerObject obj = m_compiler.object(_contractName.empty() ? m_compiler.lastContractName() : _contractName);
|
||||||
BOOST_REQUIRE(obj.linkReferences.empty());
|
BOOST_REQUIRE(obj.linkReferences.empty());
|
||||||
sendMessage(obj.bytecode + _arguments, true, _value);
|
sendMessage(obj.bytecode + _arguments, true, _value);
|
||||||
return m_output;
|
return m_output;
|
||||||
|
@ -51,9 +51,9 @@ public:
|
|||||||
|
|
||||||
Json::Value generatedDocumentation;
|
Json::Value generatedDocumentation;
|
||||||
if (_userDocumentation)
|
if (_userDocumentation)
|
||||||
generatedDocumentation = m_compilerStack.natspecUser("");
|
generatedDocumentation = m_compilerStack.natspecUser(m_compilerStack.lastContractName());
|
||||||
else
|
else
|
||||||
generatedDocumentation = m_compilerStack.natspecDev("");
|
generatedDocumentation = m_compilerStack.natspecDev(m_compilerStack.lastContractName());
|
||||||
Json::Value expectedDocumentation;
|
Json::Value expectedDocumentation;
|
||||||
m_reader.parse(_expectedDocumentationString, expectedDocumentation);
|
m_reader.parse(_expectedDocumentationString, expectedDocumentation);
|
||||||
BOOST_CHECK_MESSAGE(
|
BOOST_CHECK_MESSAGE(
|
||||||
|
Loading…
Reference in New Issue
Block a user