2018-11-29 16:35:14 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Component that verifies overloads, abstract contracts, function clashes and others
|
|
|
|
* checks at contract or function level.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/analysis/ContractLevelChecker.h>
|
|
|
|
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2019-03-04 13:33:46 +00:00
|
|
|
#include <libsolidity/analysis/TypeChecker.h>
|
2018-11-29 16:35:14 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2018-11-29 16:51:57 +00:00
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2019-09-16 12:33:43 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2018-11-29 16:51:57 +00:00
|
|
|
|
2018-11-29 16:35:14 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace langutil;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
2019-09-16 12:33:43 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// Helper struct to do a search by name
|
|
|
|
struct MatchByName
|
|
|
|
{
|
|
|
|
string const& m_name;
|
|
|
|
bool operator()(CallableDeclaration const* _callable)
|
|
|
|
{
|
|
|
|
return _callable->name() == m_name;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vector<ASTPointer<UserDefinedTypeName>> sortByContract(vector<ASTPointer<UserDefinedTypeName>> const& _list)
|
|
|
|
{
|
|
|
|
auto sorted = _list;
|
|
|
|
|
2019-11-04 12:37:23 +00:00
|
|
|
stable_sort(sorted.begin(), sorted.end(),
|
2019-09-16 12:33:43 +00:00
|
|
|
[] (ASTPointer<UserDefinedTypeName> _a, ASTPointer<UserDefinedTypeName> _b) {
|
|
|
|
if (!_a || !_b)
|
|
|
|
return _a < _b;
|
|
|
|
|
|
|
|
Declaration const* aDecl = _a->annotation().referencedDeclaration;
|
|
|
|
Declaration const* bDecl = _b->annotation().referencedDeclaration;
|
|
|
|
|
|
|
|
if (!aDecl || !bDecl)
|
|
|
|
return aDecl < bDecl;
|
|
|
|
|
|
|
|
return aDecl->id() < bDecl->id();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return sorted;
|
|
|
|
}
|
2019-09-16 10:27:54 +00:00
|
|
|
|
2019-11-28 15:53:13 +00:00
|
|
|
template <class T, class B>
|
|
|
|
bool hasEqualNameAndParameters(T const& _a, B const& _b)
|
2019-09-16 10:27:54 +00:00
|
|
|
{
|
2019-09-16 12:33:43 +00:00
|
|
|
return
|
|
|
|
_a.name() == _b.name() &&
|
2019-09-16 10:27:54 +00:00
|
|
|
FunctionType(_a).asCallableFunction(false)->hasEqualParameterTypes(
|
|
|
|
*FunctionType(_b).asCallableFunction(false)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:33:43 +00:00
|
|
|
vector<ContractDefinition const*> resolveDirectBaseContracts(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
vector<ContractDefinition const*> resolvedContracts;
|
|
|
|
|
|
|
|
for (ASTPointer<InheritanceSpecifier> const& specifier: _contract.baseContracts())
|
|
|
|
{
|
|
|
|
Declaration const* baseDecl =
|
|
|
|
specifier->name().annotation().referencedDeclaration;
|
|
|
|
auto contract = dynamic_cast<ContractDefinition const*>(baseDecl);
|
|
|
|
solAssert(contract, "contract is null");
|
|
|
|
resolvedContracts.emplace_back(contract);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolvedContracts;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractLevelChecker::LessFunction::operator()(ModifierDefinition const* _a, ModifierDefinition const* _b) const
|
|
|
|
{
|
|
|
|
return _a->name() < _b->name();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractLevelChecker::LessFunction::operator()(FunctionDefinition const* _a, FunctionDefinition const* _b) const
|
|
|
|
{
|
|
|
|
if (_a->name() != _b->name())
|
|
|
|
return _a->name() < _b->name();
|
|
|
|
|
2019-09-09 16:22:02 +00:00
|
|
|
if (_a->kind() != _b->kind())
|
|
|
|
return _a->kind() < _b->kind();
|
|
|
|
|
2019-09-16 12:33:43 +00:00
|
|
|
return boost::lexicographical_compare(
|
|
|
|
FunctionType(*_a).asCallableFunction(false)->parameterTypes(),
|
|
|
|
FunctionType(*_b).asCallableFunction(false)->parameterTypes(),
|
|
|
|
[](auto const& _paramTypeA, auto const& _paramTypeB)
|
|
|
|
{
|
|
|
|
return _paramTypeA->richIdentifier() < _paramTypeB->richIdentifier();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContractLevelChecker::LessFunction::operator()(ContractDefinition const* _a, ContractDefinition const* _b) const
|
|
|
|
{
|
|
|
|
if (!_a || !_b)
|
|
|
|
return _a < _b;
|
|
|
|
|
|
|
|
return _a->id() < _b->id();
|
2019-09-16 10:27:54 +00:00
|
|
|
}
|
2018-11-29 16:35:14 +00:00
|
|
|
|
2018-11-29 16:39:57 +00:00
|
|
|
bool ContractLevelChecker::check(ContractDefinition const& _contract)
|
2018-11-29 16:35:14 +00:00
|
|
|
{
|
2018-11-29 17:23:58 +00:00
|
|
|
checkDuplicateFunctions(_contract);
|
|
|
|
checkDuplicateEvents(_contract);
|
|
|
|
checkIllegalOverrides(_contract);
|
2019-09-16 12:33:43 +00:00
|
|
|
checkAmbiguousOverrides(_contract);
|
2018-11-29 17:23:58 +00:00
|
|
|
checkBaseConstructorArguments(_contract);
|
2019-11-04 13:12:58 +00:00
|
|
|
checkAbstractFunctions(_contract);
|
2018-11-29 17:32:06 +00:00
|
|
|
checkExternalTypeClashes(_contract);
|
2018-11-29 17:33:54 +00:00
|
|
|
checkHashCollisions(_contract);
|
2018-11-29 17:36:26 +00:00
|
|
|
checkLibraryRequirements(_contract);
|
2019-03-04 13:33:46 +00:00
|
|
|
checkBaseABICompatibility(_contract);
|
2019-09-09 16:22:02 +00:00
|
|
|
checkPayableFallbackWithoutReceive(_contract);
|
2018-11-29 16:39:57 +00:00
|
|
|
|
2018-11-29 16:35:14 +00:00
|
|
|
return Error::containsOnlyWarnings(m_errorReporter.errors());
|
|
|
|
}
|
2018-11-29 16:39:57 +00:00
|
|
|
|
2018-11-29 17:23:58 +00:00
|
|
|
void ContractLevelChecker::checkDuplicateFunctions(ContractDefinition const& _contract)
|
2018-11-29 16:39:57 +00:00
|
|
|
{
|
|
|
|
/// Checks that two functions with the same name defined in this contract have different
|
|
|
|
/// argument types and that there is at most one constructor.
|
|
|
|
map<string, vector<FunctionDefinition const*>> functions;
|
|
|
|
FunctionDefinition const* constructor = nullptr;
|
|
|
|
FunctionDefinition const* fallback = nullptr;
|
2019-09-09 16:22:02 +00:00
|
|
|
FunctionDefinition const* receive = nullptr;
|
2018-11-29 16:39:57 +00:00
|
|
|
for (FunctionDefinition const* function: _contract.definedFunctions())
|
|
|
|
if (function->isConstructor())
|
|
|
|
{
|
|
|
|
if (constructor)
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
function->location(),
|
|
|
|
SecondarySourceLocation().append("Another declaration is here:", constructor->location()),
|
|
|
|
"More than one constructor defined."
|
|
|
|
);
|
|
|
|
constructor = function;
|
|
|
|
}
|
|
|
|
else if (function->isFallback())
|
|
|
|
{
|
|
|
|
if (fallback)
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
function->location(),
|
|
|
|
SecondarySourceLocation().append("Another declaration is here:", fallback->location()),
|
|
|
|
"Only one fallback function is allowed."
|
|
|
|
);
|
|
|
|
fallback = function;
|
|
|
|
}
|
2019-09-09 16:22:02 +00:00
|
|
|
else if (function->isReceive())
|
|
|
|
{
|
|
|
|
if (receive)
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
function->location(),
|
|
|
|
SecondarySourceLocation().append("Another declaration is here:", receive->location()),
|
|
|
|
"Only one receive function is allowed."
|
|
|
|
);
|
|
|
|
receive = function;
|
|
|
|
}
|
2018-11-29 16:39:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(!function->name().empty(), "");
|
|
|
|
functions[function->name()].push_back(function);
|
|
|
|
}
|
|
|
|
|
|
|
|
findDuplicateDefinitions(functions, "Function with same name and arguments defined twice.");
|
|
|
|
}
|
|
|
|
|
2018-11-29 17:23:58 +00:00
|
|
|
void ContractLevelChecker::checkDuplicateEvents(ContractDefinition const& _contract)
|
2018-11-29 16:39:57 +00:00
|
|
|
{
|
|
|
|
/// Checks that two events with the same name defined in this contract have different
|
|
|
|
/// argument types
|
|
|
|
map<string, vector<EventDefinition const*>> events;
|
|
|
|
for (EventDefinition const* event: _contract.events())
|
|
|
|
events[event->name()].push_back(event);
|
|
|
|
|
|
|
|
findDuplicateDefinitions(events, "Event with same name and arguments defined twice.");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void ContractLevelChecker::findDuplicateDefinitions(map<string, vector<T>> const& _definitions, string _message)
|
|
|
|
{
|
|
|
|
for (auto const& it: _definitions)
|
|
|
|
{
|
|
|
|
vector<T> const& overloads = it.second;
|
|
|
|
set<size_t> reported;
|
|
|
|
for (size_t i = 0; i < overloads.size() && !reported.count(i); ++i)
|
|
|
|
{
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
|
|
|
|
for (size_t j = i + 1; j < overloads.size(); ++j)
|
2019-09-16 10:27:54 +00:00
|
|
|
if (hasEqualNameAndParameters(*overloads[i], *overloads[j]))
|
2018-11-29 16:39:57 +00:00
|
|
|
{
|
|
|
|
ssl.append("Other declaration is here:", overloads[j]->location());
|
|
|
|
reported.insert(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl.infos.size() > 0)
|
|
|
|
{
|
|
|
|
ssl.limitSize(_message);
|
|
|
|
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
overloads[i]->location(),
|
|
|
|
ssl,
|
|
|
|
_message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 16:44:38 +00:00
|
|
|
|
2018-11-29 17:23:58 +00:00
|
|
|
void ContractLevelChecker::checkIllegalOverrides(ContractDefinition const& _contract)
|
2018-11-29 16:44:38 +00:00
|
|
|
{
|
2019-12-06 09:07:56 +00:00
|
|
|
FunctionMultiSet const& inheritedFuncs = inheritedFunctions(_contract);
|
|
|
|
ModifierMultiSet const& inheritedMods = inheritedModifiers(_contract);
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-11-28 15:53:13 +00:00
|
|
|
for (auto const* stateVar: _contract.stateVariables())
|
|
|
|
{
|
|
|
|
if (!stateVar->isPublic())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
for (
|
2019-12-06 09:07:56 +00:00
|
|
|
auto it = find_if(inheritedFuncs.begin(), inheritedFuncs.end(), MatchByName{stateVar->name()});
|
|
|
|
it != inheritedFuncs.end();
|
|
|
|
it = find_if(++it, inheritedFuncs.end(), MatchByName{stateVar->name()})
|
2019-11-28 15:53:13 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!hasEqualNameAndParameters(*stateVar, **it))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((*it)->visibility() != Declaration::Visibility::External)
|
|
|
|
overrideError(*stateVar, **it, "Public state variables can only override functions with external visibility.");
|
|
|
|
else
|
2019-12-06 09:07:56 +00:00
|
|
|
checkOverride(*stateVar, **it);
|
2019-11-28 15:53:13 +00:00
|
|
|
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found && stateVar->overrides())
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
stateVar->overrides()->location(),
|
|
|
|
"Public state variable has override specified but does not override anything."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
for (ModifierDefinition const* modifier: _contract.functionModifiers())
|
|
|
|
{
|
|
|
|
if (contains_if(inheritedFuncs, MatchByName{modifier->name()}))
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
modifier->location(),
|
|
|
|
"Override changes function to modifier."
|
|
|
|
);
|
|
|
|
|
|
|
|
auto [begin, end] = inheritedMods.equal_range(modifier);
|
|
|
|
|
|
|
|
if (begin == end && modifier->overrides())
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
modifier->overrides()->location(),
|
|
|
|
"Modifier has override specified but does not override anything."
|
|
|
|
);
|
|
|
|
|
|
|
|
for (; begin != end; begin++)
|
|
|
|
if (ModifierType(**begin) != ModifierType(*modifier))
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
modifier->location(),
|
|
|
|
"Override changes modifier signature."
|
|
|
|
);
|
|
|
|
|
|
|
|
checkOverrideList(inheritedMods, *modifier);
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:33:43 +00:00
|
|
|
for (FunctionDefinition const* function: _contract.definedFunctions())
|
2018-11-29 16:44:38 +00:00
|
|
|
{
|
2019-11-05 17:25:34 +00:00
|
|
|
if (function->isConstructor())
|
|
|
|
continue;
|
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
if (contains_if(inheritedMods, MatchByName{function->name()}))
|
2019-09-16 12:33:43 +00:00
|
|
|
m_errorReporter.typeError(function->location(), "Override changes modifier to function.");
|
2018-11-29 16:44:38 +00:00
|
|
|
|
2019-09-16 12:33:43 +00:00
|
|
|
// No inheriting functions found
|
2019-12-06 09:07:56 +00:00
|
|
|
if (!inheritedFuncs.count(function) && function->overrides())
|
2019-09-16 12:33:43 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
function->overrides()->location(),
|
|
|
|
"Function has override specified but does not override anything."
|
|
|
|
);
|
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
checkOverrideList(inheritedFuncs, *function);
|
2018-11-29 16:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
template<class T, class U>
|
|
|
|
void ContractLevelChecker::checkOverride(T const& _overriding, U const& _super)
|
2018-11-29 16:44:38 +00:00
|
|
|
{
|
2019-12-06 09:07:56 +00:00
|
|
|
static_assert(
|
|
|
|
std::is_same<VariableDeclaration, T>::value ||
|
|
|
|
std::is_same<FunctionDefinition, T>::value ||
|
|
|
|
std::is_same<ModifierDefinition, T>::value,
|
|
|
|
"Invalid call to checkOverride."
|
|
|
|
);
|
2019-11-28 15:53:13 +00:00
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
static_assert(
|
|
|
|
std::is_same<FunctionDefinition, U>::value ||
|
|
|
|
std::is_same<ModifierDefinition, U>::value,
|
|
|
|
"Invalid call to checkOverride."
|
|
|
|
);
|
|
|
|
static_assert(
|
|
|
|
!std::is_same<ModifierDefinition, U>::value ||
|
|
|
|
std::is_same<ModifierDefinition, T>::value,
|
|
|
|
"Invalid call to checkOverride."
|
|
|
|
);
|
|
|
|
|
|
|
|
string overridingName;
|
2019-11-28 15:53:13 +00:00
|
|
|
if constexpr(std::is_same<FunctionDefinition, T>::value)
|
|
|
|
overridingName = "function";
|
2019-12-06 09:07:56 +00:00
|
|
|
else if constexpr(std::is_same<ModifierDefinition, T>::value)
|
|
|
|
overridingName = "modifier";
|
2019-11-28 15:53:13 +00:00
|
|
|
else
|
|
|
|
overridingName = "public state variable";
|
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
string superName;
|
|
|
|
if constexpr(std::is_same<FunctionDefinition, U>::value)
|
|
|
|
superName = "function";
|
|
|
|
else
|
|
|
|
superName = "modifier";
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-11-28 15:53:13 +00:00
|
|
|
if (!_overriding.overrides())
|
|
|
|
overrideError(_overriding, _super, "Overriding " + overridingName + " is missing 'override' specifier.");
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-11-05 17:25:34 +00:00
|
|
|
if (!_super.virtualSemantics())
|
2019-12-06 09:07:56 +00:00
|
|
|
overrideError(
|
|
|
|
_super,
|
|
|
|
_overriding,
|
|
|
|
"Trying to override non-virtual " + superName + ". Did you forget to add \"virtual\"?",
|
|
|
|
"Overriding " + overridingName + " is here:"
|
|
|
|
);
|
2018-11-29 16:44:38 +00:00
|
|
|
|
2019-11-28 15:53:13 +00:00
|
|
|
if (_overriding.visibility() != _super.visibility())
|
|
|
|
{
|
2018-11-29 16:44:38 +00:00
|
|
|
// Visibility change from external to public is fine.
|
|
|
|
// Any other change is disallowed.
|
|
|
|
if (!(
|
|
|
|
_super.visibility() == FunctionDefinition::Visibility::External &&
|
2019-11-28 15:53:13 +00:00
|
|
|
_overriding.visibility() == FunctionDefinition::Visibility::Public
|
2018-11-29 16:44:38 +00:00
|
|
|
))
|
2019-11-28 15:53:13 +00:00
|
|
|
overrideError(_overriding, _super, "Overriding " + overridingName + " visibility differs.");
|
|
|
|
}
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
// This is only relevant for overriding functions by functions or state variables,
|
|
|
|
// it is skipped for modifiers.
|
|
|
|
if constexpr(std::is_same<FunctionDefinition, U>::value)
|
2019-11-28 15:53:13 +00:00
|
|
|
{
|
2019-12-06 09:07:56 +00:00
|
|
|
FunctionTypePointer functionType = FunctionType(_overriding).asCallableFunction(false);
|
|
|
|
FunctionTypePointer superType = FunctionType(_super).asCallableFunction(false);
|
2019-11-28 15:53:13 +00:00
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
solAssert(functionType->hasEqualParameterTypes(*superType), "Override doesn't have equal parameters!");
|
|
|
|
|
|
|
|
if (!functionType->hasEqualReturnTypes(*superType))
|
|
|
|
overrideError(_overriding, _super, "Overriding " + overridingName + " return types differ.");
|
|
|
|
|
|
|
|
// This is only relevant for a function overriding a function.
|
|
|
|
if constexpr(std::is_same<T, FunctionDefinition>::value)
|
|
|
|
{
|
|
|
|
_overriding.annotation().baseFunctions.emplace(&_super);
|
|
|
|
|
|
|
|
if (_overriding.stateMutability() != _super.stateMutability())
|
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
"Overriding function changes state mutability from \"" +
|
|
|
|
stateMutabilityToString(_super.stateMutability()) +
|
|
|
|
"\" to \"" +
|
|
|
|
stateMutabilityToString(_overriding.stateMutability()) +
|
|
|
|
"\"."
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!_overriding.isImplemented() && _super.isImplemented())
|
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
"Overriding an implemented function with an unimplemented function is not allowed."
|
|
|
|
);
|
|
|
|
}
|
2019-11-28 15:53:13 +00:00
|
|
|
}
|
2018-11-29 16:44:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
void ContractLevelChecker::overrideListError(
|
|
|
|
CallableDeclaration const& _callable,
|
|
|
|
set<ContractDefinition const*, LessFunction> _secondary,
|
|
|
|
string const& _message1,
|
|
|
|
string const& _message2
|
|
|
|
)
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
|
|
|
// Using a set rather than a vector so the order is always the same
|
|
|
|
set<string> names;
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
for (Declaration const* c: _secondary)
|
|
|
|
{
|
|
|
|
ssl.append("This contract: ", c->location());
|
|
|
|
names.insert(c->name());
|
|
|
|
}
|
|
|
|
string contractSingularPlural = "contract ";
|
|
|
|
if (_secondary.size() > 1)
|
|
|
|
contractSingularPlural = "contracts ";
|
|
|
|
|
|
|
|
m_errorReporter.typeError(
|
2019-12-06 09:07:56 +00:00
|
|
|
_callable.overrides() ? _callable.overrides()->location() : _callable.location(),
|
2019-09-16 12:33:43 +00:00
|
|
|
ssl,
|
|
|
|
_message1 +
|
|
|
|
contractSingularPlural +
|
|
|
|
_message2 +
|
|
|
|
joinHumanReadable(names, ", ", " and ") +
|
|
|
|
"."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-28 15:53:13 +00:00
|
|
|
void ContractLevelChecker::overrideError(Declaration const& _overriding, Declaration const& _super, string _message, string _secondaryMsg)
|
2018-11-29 16:44:38 +00:00
|
|
|
{
|
|
|
|
m_errorReporter.typeError(
|
2019-11-28 15:53:13 +00:00
|
|
|
_overriding.location(),
|
|
|
|
SecondarySourceLocation().append(_secondaryMsg, _super.location()),
|
|
|
|
_message
|
2018-11-29 16:44:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-29 17:23:58 +00:00
|
|
|
void ContractLevelChecker::checkAbstractFunctions(ContractDefinition const& _contract)
|
2018-11-29 16:51:57 +00:00
|
|
|
{
|
|
|
|
// Mapping from name to function definition (exactly one per argument type equality class) and
|
|
|
|
// flag to indicate whether it is fully implemented.
|
|
|
|
using FunTypeAndFlag = std::pair<FunctionTypePointer, bool>;
|
|
|
|
map<string, vector<FunTypeAndFlag>> functions;
|
|
|
|
|
2018-11-30 23:08:42 +00:00
|
|
|
auto registerFunction = [&](Declaration const& _declaration, FunctionTypePointer const& _type, bool _implemented)
|
|
|
|
{
|
|
|
|
auto& overloads = functions[_declaration.name()];
|
|
|
|
auto it = find_if(overloads.begin(), overloads.end(), [&](FunTypeAndFlag const& _funAndFlag)
|
2018-11-29 16:51:57 +00:00
|
|
|
{
|
2018-11-30 23:08:42 +00:00
|
|
|
return _type->hasEqualParameterTypes(*_funAndFlag.first);
|
|
|
|
});
|
|
|
|
if (it == overloads.end())
|
2018-12-10 18:02:39 +00:00
|
|
|
overloads.emplace_back(_type, _implemented);
|
2018-11-30 23:08:42 +00:00
|
|
|
else if (_implemented)
|
|
|
|
it->second = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Search from base to derived, collect all functions and update
|
|
|
|
// the 'implemented' flag.
|
|
|
|
for (ContractDefinition const* contract: boost::adaptors::reverse(_contract.annotation().linearizedBaseContracts))
|
|
|
|
{
|
|
|
|
for (VariableDeclaration const* v: contract->stateVariables())
|
|
|
|
if (v->isPartOfExternalInterface())
|
2019-04-17 11:40:50 +00:00
|
|
|
registerFunction(*v, TypeProvider::function(*v), true);
|
2018-11-30 23:08:42 +00:00
|
|
|
|
|
|
|
for (FunctionDefinition const* function: contract->definedFunctions())
|
|
|
|
if (!function->isConstructor())
|
|
|
|
registerFunction(
|
|
|
|
*function,
|
2019-04-17 11:40:50 +00:00
|
|
|
TypeProvider::function(*function)->asCallableFunction(false),
|
2018-11-30 23:08:42 +00:00
|
|
|
function->isImplemented()
|
|
|
|
);
|
|
|
|
}
|
2018-11-29 16:51:57 +00:00
|
|
|
|
|
|
|
// Set to not fully implemented if at least one flag is false.
|
2019-11-04 13:12:58 +00:00
|
|
|
// Note that `_contract.annotation().unimplementedFunctions` has already been
|
|
|
|
// pre-filled by `checkBaseConstructorArguments`.
|
2018-11-29 16:51:57 +00:00
|
|
|
for (auto const& it: functions)
|
|
|
|
for (auto const& funAndFlag: it.second)
|
|
|
|
if (!funAndFlag.second)
|
|
|
|
{
|
|
|
|
FunctionDefinition const* function = dynamic_cast<FunctionDefinition const*>(&funAndFlag.first->declaration());
|
|
|
|
solAssert(function, "");
|
|
|
|
_contract.annotation().unimplementedFunctions.push_back(function);
|
|
|
|
break;
|
|
|
|
}
|
2019-09-04 22:01:13 +00:00
|
|
|
|
2019-11-04 13:12:58 +00:00
|
|
|
if (_contract.abstract())
|
|
|
|
{
|
|
|
|
if (_contract.contractKind() == ContractDefinition::ContractKind::Interface)
|
|
|
|
m_errorReporter.typeError(_contract.location(), "Interfaces do not need the \"abstract\" keyword, they are abstract implicitly.");
|
|
|
|
else if (_contract.contractKind() == ContractDefinition::ContractKind::Library)
|
|
|
|
m_errorReporter.typeError(_contract.location(), "Libraries cannot be abstract.");
|
|
|
|
else
|
|
|
|
solAssert(_contract.contractKind() == ContractDefinition::ContractKind::Contract, "");
|
|
|
|
}
|
2019-09-04 22:01:13 +00:00
|
|
|
|
2019-11-04 13:12:58 +00:00
|
|
|
// For libraries, we emit errors on function-level, so this is fine as long as we do
|
|
|
|
// not have inheritance for libraries.
|
2019-09-04 22:01:13 +00:00
|
|
|
if (
|
|
|
|
_contract.contractKind() == ContractDefinition::ContractKind::Contract &&
|
2019-11-04 13:12:58 +00:00
|
|
|
!_contract.abstract() &&
|
|
|
|
!_contract.annotation().unimplementedFunctions.empty()
|
2019-09-04 22:01:13 +00:00
|
|
|
)
|
2019-11-08 17:06:40 +00:00
|
|
|
{
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
for (auto function: _contract.annotation().unimplementedFunctions)
|
|
|
|
ssl.append("Missing implementation:", function->location());
|
|
|
|
m_errorReporter.typeError(_contract.location(), ssl,
|
|
|
|
"Contract \"" + _contract.annotation().canonicalName
|
|
|
|
+ "\" should be marked as abstract.");
|
|
|
|
}
|
2018-11-29 16:51:57 +00:00
|
|
|
}
|
2018-11-29 16:58:15 +00:00
|
|
|
|
|
|
|
|
2018-11-29 17:23:58 +00:00
|
|
|
void ContractLevelChecker::checkBaseConstructorArguments(ContractDefinition const& _contract)
|
2018-11-29 16:58:15 +00:00
|
|
|
{
|
|
|
|
vector<ContractDefinition const*> const& bases = _contract.annotation().linearizedBaseContracts;
|
|
|
|
|
|
|
|
// Determine the arguments that are used for the base constructors.
|
|
|
|
for (ContractDefinition const* contract: bases)
|
|
|
|
{
|
|
|
|
if (FunctionDefinition const* constructor = contract->constructor())
|
|
|
|
for (auto const& modifier: constructor->modifiers())
|
|
|
|
if (auto baseContract = dynamic_cast<ContractDefinition const*>(
|
|
|
|
modifier->name()->annotation().referencedDeclaration
|
|
|
|
))
|
|
|
|
{
|
|
|
|
if (modifier->arguments())
|
|
|
|
{
|
|
|
|
if (baseContract->constructor())
|
|
|
|
annotateBaseConstructorArguments(_contract, baseContract->constructor(), modifier.get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
modifier->location(),
|
|
|
|
"Modifier-style base constructor call without arguments."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ASTPointer<InheritanceSpecifier> const& base: contract->baseContracts())
|
|
|
|
{
|
|
|
|
ContractDefinition const* baseContract = dynamic_cast<ContractDefinition const*>(
|
|
|
|
base->name().annotation().referencedDeclaration
|
|
|
|
);
|
|
|
|
solAssert(baseContract, "");
|
|
|
|
|
|
|
|
if (baseContract->constructor() && base->arguments() && !base->arguments()->empty())
|
|
|
|
annotateBaseConstructorArguments(_contract, baseContract->constructor(), base.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that we get arguments for all base constructors that need it.
|
|
|
|
// If not mark the contract as abstract (not fully implemented)
|
|
|
|
for (ContractDefinition const* contract: bases)
|
|
|
|
if (FunctionDefinition const* constructor = contract->constructor())
|
|
|
|
if (contract != &_contract && !constructor->parameters().empty())
|
|
|
|
if (!_contract.annotation().baseConstructorArguments.count(constructor))
|
|
|
|
_contract.annotation().unimplementedFunctions.push_back(constructor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContractLevelChecker::annotateBaseConstructorArguments(
|
|
|
|
ContractDefinition const& _currentContract,
|
|
|
|
FunctionDefinition const* _baseConstructor,
|
|
|
|
ASTNode const* _argumentNode
|
|
|
|
)
|
|
|
|
{
|
|
|
|
solAssert(_baseConstructor, "");
|
|
|
|
solAssert(_argumentNode, "");
|
|
|
|
|
|
|
|
auto insertionResult = _currentContract.annotation().baseConstructorArguments.insert(
|
|
|
|
std::make_pair(_baseConstructor, _argumentNode)
|
|
|
|
);
|
|
|
|
if (!insertionResult.second)
|
|
|
|
{
|
|
|
|
ASTNode const* previousNode = insertionResult.first->second;
|
|
|
|
|
|
|
|
SourceLocation const* mainLocation = nullptr;
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
|
|
|
|
if (
|
|
|
|
_currentContract.location().contains(previousNode->location()) ||
|
|
|
|
_currentContract.location().contains(_argumentNode->location())
|
|
|
|
)
|
|
|
|
{
|
|
|
|
mainLocation = &previousNode->location();
|
|
|
|
ssl.append("Second constructor call is here:", _argumentNode->location());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mainLocation = &_currentContract.location();
|
2019-07-08 12:28:22 +00:00
|
|
|
ssl.append("First constructor call is here:", _argumentNode->location());
|
|
|
|
ssl.append("Second constructor call is here:", previousNode->location());
|
2018-11-29 16:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_errorReporter.declarationError(
|
|
|
|
*mainLocation,
|
|
|
|
ssl,
|
|
|
|
"Base constructor arguments given twice."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-29 17:26:53 +00:00
|
|
|
|
2018-11-29 17:32:06 +00:00
|
|
|
void ContractLevelChecker::checkExternalTypeClashes(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
map<string, vector<pair<Declaration const*, FunctionTypePointer>>> externalDeclarations;
|
|
|
|
for (ContractDefinition const* contract: _contract.annotation().linearizedBaseContracts)
|
|
|
|
{
|
|
|
|
for (FunctionDefinition const* f: contract->definedFunctions())
|
|
|
|
if (f->isPartOfExternalInterface())
|
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
auto functionType = TypeProvider::function(*f);
|
2018-11-29 17:32:06 +00:00
|
|
|
// under non error circumstances this should be true
|
|
|
|
if (functionType->interfaceFunctionType())
|
2018-12-10 18:02:39 +00:00
|
|
|
externalDeclarations[functionType->externalSignature()].emplace_back(
|
|
|
|
f, functionType->asCallableFunction(false)
|
2018-11-29 17:32:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
for (VariableDeclaration const* v: contract->stateVariables())
|
|
|
|
if (v->isPartOfExternalInterface())
|
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
auto functionType = TypeProvider::function(*v);
|
2018-11-29 17:32:06 +00:00
|
|
|
// under non error circumstances this should be true
|
|
|
|
if (functionType->interfaceFunctionType())
|
2018-12-10 18:02:39 +00:00
|
|
|
externalDeclarations[functionType->externalSignature()].emplace_back(
|
|
|
|
v, functionType->asCallableFunction(false)
|
2018-11-29 17:32:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto const& it: externalDeclarations)
|
|
|
|
for (size_t i = 0; i < it.second.size(); ++i)
|
|
|
|
for (size_t j = i + 1; j < it.second.size(); ++j)
|
|
|
|
if (!it.second[i].second->hasEqualParameterTypes(*it.second[j].second))
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
it.second[j].first->location(),
|
|
|
|
"Function overload clash during conversion to external types for arguments."
|
|
|
|
);
|
|
|
|
}
|
2018-11-29 17:33:54 +00:00
|
|
|
|
|
|
|
void ContractLevelChecker::checkHashCollisions(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
set<FixedHash<4>> hashes;
|
|
|
|
for (auto const& it: _contract.interfaceFunctionList())
|
|
|
|
{
|
|
|
|
FixedHash<4> const& hash = it.first;
|
|
|
|
if (hashes.count(hash))
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
_contract.location(),
|
|
|
|
string("Function signature hash collision for ") + it.second->externalSignature()
|
|
|
|
);
|
|
|
|
hashes.insert(hash);
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 17:36:26 +00:00
|
|
|
|
|
|
|
void ContractLevelChecker::checkLibraryRequirements(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
if (!_contract.isLibrary())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!_contract.baseContracts().empty())
|
|
|
|
m_errorReporter.typeError(_contract.location(), "Library is not allowed to inherit.");
|
|
|
|
|
|
|
|
for (auto const& var: _contract.stateVariables())
|
|
|
|
if (!var->isConstant())
|
|
|
|
m_errorReporter.typeError(var->location(), "Library cannot have non-constant state variables");
|
|
|
|
}
|
2019-03-04 13:33:46 +00:00
|
|
|
|
|
|
|
void ContractLevelChecker::checkBaseABICompatibility(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
if (_contract.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::ABIEncoderV2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_contract.isLibrary())
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
_contract.baseContracts().empty() || m_errorReporter.hasErrors(),
|
|
|
|
"Library is not allowed to inherit"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SecondarySourceLocation errors;
|
|
|
|
|
|
|
|
// interfaceFunctionList contains all inherited functions as well
|
|
|
|
for (auto const& func: _contract.interfaceFunctionList())
|
|
|
|
{
|
|
|
|
solAssert(func.second->hasDeclaration(), "Function has no declaration?!");
|
|
|
|
|
|
|
|
if (!func.second->declaration().sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::ABIEncoderV2))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto const& currentLoc = func.second->declaration().location();
|
|
|
|
|
|
|
|
for (TypePointer const& paramType: func.second->parameterTypes() + func.second->parameterTypes())
|
|
|
|
if (!TypeChecker::typeSupportedByOldABIEncoder(*paramType, false))
|
|
|
|
{
|
|
|
|
errors.append("Type only supported by the new experimental ABI encoder", currentLoc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!errors.infos.empty())
|
|
|
|
m_errorReporter.fatalTypeError(
|
|
|
|
_contract.location(),
|
|
|
|
errors,
|
|
|
|
std::string("Contract \"") +
|
|
|
|
_contract.name() +
|
|
|
|
"\" does not use the new experimental ABI encoder but wants to inherit from a contract " +
|
|
|
|
"which uses types that require it. " +
|
|
|
|
"Use \"pragma experimental ABIEncoderV2;\" for the inheriting contract as well to enable the feature."
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2019-09-16 12:33:43 +00:00
|
|
|
|
|
|
|
void ContractLevelChecker::checkAmbiguousOverrides(ContractDefinition const& _contract) const
|
|
|
|
{
|
2019-12-09 16:42:12 +00:00
|
|
|
std::function<bool(CallableDeclaration const*, CallableDeclaration const*)> compareById =
|
|
|
|
[](auto const* _a, auto const* _b) { return _a->id() < _b->id(); };
|
2019-12-06 09:07:56 +00:00
|
|
|
|
2019-12-09 16:42:12 +00:00
|
|
|
{
|
|
|
|
// Fetch inherited functions and sort them by signature.
|
|
|
|
// We get at least one function per signature and direct base contract, which is
|
|
|
|
// enough because we re-construct the inheritance graph later.
|
|
|
|
FunctionMultiSet nonOverriddenFunctions = inheritedFunctions(_contract);
|
|
|
|
// Remove all functions that match the signature of a function in the current contract.
|
|
|
|
nonOverriddenFunctions -= _contract.definedFunctions();
|
|
|
|
|
|
|
|
// Walk through the set of functions signature by signature.
|
|
|
|
for (auto it = nonOverriddenFunctions.cbegin(); it != nonOverriddenFunctions.cend();)
|
|
|
|
{
|
|
|
|
std::set<CallableDeclaration const*, decltype(compareById)> baseFunctions(compareById);
|
|
|
|
for (auto nextSignature = nonOverriddenFunctions.upper_bound(*it); it != nextSignature; ++it)
|
|
|
|
baseFunctions.insert(*it);
|
2019-12-06 09:07:56 +00:00
|
|
|
|
2019-12-09 16:42:12 +00:00
|
|
|
checkAmbiguousOverridesInternal(std::move(baseFunctions), _contract.location());
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 12:58:30 +00:00
|
|
|
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
2019-12-09 16:42:12 +00:00
|
|
|
ModifierMultiSet modifiers = inheritedModifiers(_contract);
|
|
|
|
modifiers -= _contract.functionModifiers();
|
|
|
|
for (auto it = modifiers.cbegin(); it != modifiers.cend();)
|
|
|
|
{
|
|
|
|
std::set<CallableDeclaration const*, decltype(compareById)> baseModifiers(compareById);
|
|
|
|
for (auto next = modifiers.upper_bound(*it); it != next; ++it)
|
|
|
|
baseModifiers.insert(*it);
|
|
|
|
|
|
|
|
checkAmbiguousOverridesInternal(std::move(baseModifiers), _contract.location());
|
|
|
|
}
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
void ContractLevelChecker::checkAmbiguousOverridesInternal(set<
|
|
|
|
CallableDeclaration const*,
|
|
|
|
std::function<bool(CallableDeclaration const*, CallableDeclaration const*)>
|
|
|
|
> _baseCallables, SourceLocation const& _location) const
|
|
|
|
{
|
|
|
|
if (_baseCallables.size() <= 1)
|
|
|
|
return;
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
// Construct the override graph for this signature.
|
|
|
|
// Reserve node 0 for the current contract and node
|
|
|
|
// 1 for an artificial top node to which all override paths
|
|
|
|
// connect at the end.
|
|
|
|
struct OverrideGraph
|
|
|
|
{
|
|
|
|
OverrideGraph(decltype(_baseCallables) const& __baseCallables)
|
2019-12-03 12:58:30 +00:00
|
|
|
{
|
2019-12-09 15:54:52 +00:00
|
|
|
for (auto const* baseFunction: __baseCallables)
|
|
|
|
addEdge(0, visit(baseFunction));
|
|
|
|
}
|
|
|
|
std::map<CallableDeclaration const*, int> nodes;
|
|
|
|
std::map<int, CallableDeclaration const*> nodeInv;
|
|
|
|
std::map<int, std::set<int>> edges;
|
|
|
|
int numNodes = 2;
|
|
|
|
void addEdge(int _a, int _b)
|
|
|
|
{
|
|
|
|
edges[_a].insert(_b);
|
|
|
|
edges[_b].insert(_a);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
/// Completes the graph starting from @a _function and
|
|
|
|
/// @returns the node ID.
|
|
|
|
int visit(CallableDeclaration const* _function)
|
|
|
|
{
|
|
|
|
auto it = nodes.find(_function);
|
|
|
|
if (it != nodes.end())
|
|
|
|
return it->second;
|
|
|
|
int currentNode = numNodes++;
|
|
|
|
nodes[_function] = currentNode;
|
|
|
|
nodeInv[currentNode] = _function;
|
|
|
|
if (_function->overrides())
|
|
|
|
for (auto const* baseFunction: _function->annotation().baseFunctions)
|
|
|
|
addEdge(currentNode, visit(baseFunction));
|
|
|
|
else
|
|
|
|
addEdge(currentNode, 1);
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
return currentNode;
|
|
|
|
}
|
|
|
|
} overrideGraph(_baseCallables);
|
2019-12-03 12:58:30 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
// Detect cut vertices following https://en.wikipedia.org/wiki/Biconnected_component#Pseudocode
|
|
|
|
// Can ignore the root node, since it is never a cut vertex in our case.
|
|
|
|
struct CutVertexFinder
|
|
|
|
{
|
|
|
|
CutVertexFinder(OverrideGraph const& _graph): m_graph(_graph)
|
|
|
|
{
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
std::set<CallableDeclaration const*> const& cutVertices() const { return m_cutVertices; }
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
private:
|
|
|
|
OverrideGraph const& m_graph;
|
2019-12-03 12:58:30 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
std::vector<bool> m_visited = std::vector<bool>(m_graph.numNodes, false);
|
|
|
|
std::vector<int> m_depths = std::vector<int>(m_graph.numNodes, -1);
|
|
|
|
std::vector<int> m_low = std::vector<int>(m_graph.numNodes, -1);
|
|
|
|
std::vector<int> m_parent = std::vector<int>(m_graph.numNodes, -1);
|
|
|
|
std::set<CallableDeclaration const*> m_cutVertices{};
|
|
|
|
|
|
|
|
void run(int _u = 0, int _depth = 0)
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
2019-12-09 15:54:52 +00:00
|
|
|
m_visited.at(_u) = true;
|
|
|
|
m_depths.at(_u) = m_low.at(_u) = _depth;
|
|
|
|
for (int v: m_graph.edges.at(_u))
|
|
|
|
if (!m_visited.at(v))
|
|
|
|
{
|
|
|
|
m_parent[v] = _u;
|
|
|
|
run(v, _depth + 1);
|
|
|
|
if (m_low[v] >= m_depths[_u] && m_parent[_u] != -1)
|
|
|
|
m_cutVertices.insert(m_graph.nodeInv.at(_u));
|
|
|
|
m_low[_u] = min(m_low[_u], m_low[v]);
|
|
|
|
}
|
|
|
|
else if (v != m_parent[_u])
|
|
|
|
m_low[_u] = min(m_low[_u], m_depths[v]);
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
2019-12-09 15:54:52 +00:00
|
|
|
} cutVertexFinder{overrideGraph};
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
// Remove all base functions overridden by cut vertices (they don't need to be overridden).
|
|
|
|
for (auto const* function: cutVertexFinder.cutVertices())
|
|
|
|
{
|
|
|
|
std::set<CallableDeclaration const*> toTraverse = function->annotation().baseFunctions;
|
|
|
|
while (!toTraverse.empty())
|
|
|
|
{
|
|
|
|
auto const *base = *toTraverse.begin();
|
|
|
|
toTraverse.erase(toTraverse.begin());
|
|
|
|
_baseCallables.erase(base);
|
|
|
|
for (CallableDeclaration const* f: base->annotation().baseFunctions)
|
|
|
|
toTraverse.insert(f);
|
|
|
|
}
|
|
|
|
// Remove unimplemented base functions at the cut vertices itself as well.
|
|
|
|
if (auto opt = dynamic_cast<ImplementationOptional const*>(function))
|
|
|
|
if (!opt->isImplemented())
|
|
|
|
_baseCallables.erase(function);
|
|
|
|
}
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
// If more than one function is left, they have to be overridden.
|
|
|
|
if (_baseCallables.size() <= 1)
|
|
|
|
return;
|
2019-12-03 12:58:30 +00:00
|
|
|
|
2019-12-09 15:54:52 +00:00
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
for (auto const* baseFunction: _baseCallables)
|
|
|
|
{
|
|
|
|
string contractName = dynamic_cast<ContractDefinition const&>(*baseFunction->scope()).name();
|
|
|
|
ssl.append("Definition in \"" + contractName + "\": ", baseFunction->location());
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
2019-12-09 15:54:52 +00:00
|
|
|
|
|
|
|
string callableName;
|
2019-12-09 16:42:12 +00:00
|
|
|
string distinguishigProperty;
|
2019-12-09 15:54:52 +00:00
|
|
|
if (dynamic_cast<FunctionDefinition const*>(*_baseCallables.begin()))
|
2019-12-09 16:42:12 +00:00
|
|
|
{
|
2019-12-09 15:54:52 +00:00
|
|
|
callableName = "function";
|
2019-12-09 16:42:12 +00:00
|
|
|
distinguishigProperty = "name and parameter types";
|
|
|
|
}
|
2019-12-09 15:54:52 +00:00
|
|
|
else if (dynamic_cast<ModifierDefinition const*>(*_baseCallables.begin()))
|
2019-12-09 16:42:12 +00:00
|
|
|
{
|
2019-12-09 15:54:52 +00:00
|
|
|
callableName = "modifier";
|
2019-12-09 16:42:12 +00:00
|
|
|
distinguishigProperty = "name";
|
|
|
|
}
|
2019-12-09 15:54:52 +00:00
|
|
|
else
|
|
|
|
solAssert(false, "Invalid type for ambiguous override.");
|
|
|
|
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
_location,
|
|
|
|
ssl,
|
|
|
|
"Derived contract must override " + callableName + " \"" +
|
|
|
|
(*_baseCallables.begin())->name() +
|
2019-12-09 16:42:12 +00:00
|
|
|
"\". Two or more base classes define " + callableName + " with same " + distinguishigProperty + "."
|
2019-12-09 15:54:52 +00:00
|
|
|
);
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set<ContractDefinition const*, ContractLevelChecker::LessFunction> ContractLevelChecker::resolveOverrideList(OverrideSpecifier const& _overrides) const
|
|
|
|
{
|
|
|
|
set<ContractDefinition const*, LessFunction> resolved;
|
|
|
|
|
|
|
|
for (ASTPointer<UserDefinedTypeName> const& override: _overrides.overrides())
|
|
|
|
{
|
|
|
|
Declaration const* decl = override->annotation().referencedDeclaration;
|
|
|
|
solAssert(decl, "Expected declaration to be resolved.");
|
|
|
|
|
|
|
|
// If it's not a contract it will be caught
|
|
|
|
// in the reference resolver
|
|
|
|
if (ContractDefinition const* contract = dynamic_cast<decltype(contract)>(decl))
|
|
|
|
resolved.insert(contract);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolved;
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
template <class T>
|
|
|
|
void ContractLevelChecker::checkOverrideList(
|
|
|
|
std::multiset<T const*, LessFunction> const& _inheritedCallables,
|
|
|
|
T const& _callable
|
|
|
|
)
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
|
|
|
set<ContractDefinition const*, LessFunction> specifiedContracts =
|
2019-12-06 09:07:56 +00:00
|
|
|
_callable.overrides() ?
|
|
|
|
resolveOverrideList(*_callable.overrides()) :
|
2019-09-16 12:33:43 +00:00
|
|
|
decltype(specifiedContracts){};
|
|
|
|
|
|
|
|
// Check for duplicates in override list
|
2019-12-06 09:07:56 +00:00
|
|
|
if (_callable.overrides() && specifiedContracts.size() != _callable.overrides()->overrides().size())
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
|
|
|
// Sort by contract id to find duplicate for error reporting
|
|
|
|
vector<ASTPointer<UserDefinedTypeName>> list =
|
2019-12-06 09:07:56 +00:00
|
|
|
sortByContract(_callable.overrides()->overrides());
|
2019-09-16 12:33:43 +00:00
|
|
|
|
|
|
|
// Find duplicates and output error
|
|
|
|
for (size_t i = 1; i < list.size(); i++)
|
|
|
|
{
|
|
|
|
Declaration const* aDecl = list[i]->annotation().referencedDeclaration;
|
|
|
|
Declaration const* bDecl = list[i-1]->annotation().referencedDeclaration;
|
|
|
|
if (!aDecl || !bDecl)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (aDecl->id() == bDecl->id())
|
|
|
|
{
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
ssl.append("First occurrence here: ", list[i-1]->location());
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
list[i]->location(),
|
|
|
|
ssl,
|
|
|
|
"Duplicate contract \"" +
|
|
|
|
joinHumanReadable(list[i]->namePath(), ".") +
|
|
|
|
"\" found in override list of \"" +
|
2019-12-06 09:07:56 +00:00
|
|
|
_callable.name() +
|
2019-09-16 12:33:43 +00:00
|
|
|
"\"."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(specifiedContracts) expectedContracts;
|
|
|
|
|
|
|
|
// Build list of expected contracts
|
2019-12-06 09:07:56 +00:00
|
|
|
for (auto [begin, end] = _inheritedCallables.equal_range(&_callable); begin != end; begin++)
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
|
|
|
// Validate the override
|
2019-12-06 09:07:56 +00:00
|
|
|
checkOverride(_callable, **begin);
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-06 09:07:56 +00:00
|
|
|
expectedContracts.insert(&dynamic_cast<ContractDefinition const&>(*(*begin)->scope()));
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decltype(specifiedContracts) missingContracts;
|
|
|
|
decltype(specifiedContracts) surplusContracts;
|
|
|
|
|
|
|
|
// If we expect only one contract, no contract needs to be specified
|
|
|
|
if (expectedContracts.size() > 1)
|
|
|
|
missingContracts = expectedContracts - specifiedContracts;
|
|
|
|
|
|
|
|
surplusContracts = specifiedContracts - expectedContracts;
|
|
|
|
|
|
|
|
if (!missingContracts.empty())
|
|
|
|
overrideListError(
|
2019-12-06 09:07:56 +00:00
|
|
|
_callable,
|
2019-09-16 12:33:43 +00:00
|
|
|
missingContracts,
|
|
|
|
"Function needs to specify overridden ",
|
|
|
|
""
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!surplusContracts.empty())
|
|
|
|
overrideListError(
|
2019-12-06 09:07:56 +00:00
|
|
|
_callable,
|
2019-09-16 12:33:43 +00:00
|
|
|
surplusContracts,
|
|
|
|
"Invalid ",
|
|
|
|
"specified in override list: "
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
ContractLevelChecker::FunctionMultiSet const& ContractLevelChecker::inheritedFunctions(ContractDefinition const& _contract) const
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
2019-12-03 12:58:30 +00:00
|
|
|
if (!m_inheritedFunctions.count(&_contract))
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
|
|
|
FunctionMultiSet set;
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
for (auto const* base: resolveDirectBaseContracts(_contract))
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
2019-12-03 12:58:30 +00:00
|
|
|
std::set<FunctionDefinition const*, LessFunction> functionsInBase;
|
|
|
|
for (FunctionDefinition const* fun: base->definedFunctions())
|
|
|
|
if (!fun->isConstructor())
|
|
|
|
functionsInBase.emplace(fun);
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
for (auto const& func: inheritedFunctions(*base))
|
|
|
|
functionsInBase.insert(func);
|
2019-09-16 12:33:43 +00:00
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
set += functionsInBase;
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
m_inheritedFunctions[&_contract] = set;
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
return m_inheritedFunctions[&_contract];
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
ContractLevelChecker::ModifierMultiSet const& ContractLevelChecker::inheritedModifiers(ContractDefinition const& _contract) const
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
2019-12-03 12:58:30 +00:00
|
|
|
auto const& result = m_contractBaseModifiers.find(&_contract);
|
2019-09-16 12:33:43 +00:00
|
|
|
|
|
|
|
if (result != m_contractBaseModifiers.cend())
|
|
|
|
return result->second;
|
|
|
|
|
|
|
|
ModifierMultiSet set;
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
for (auto const* base: resolveDirectBaseContracts(_contract))
|
2019-09-16 12:33:43 +00:00
|
|
|
{
|
|
|
|
std::set<ModifierDefinition const*, LessFunction> tmpSet =
|
|
|
|
convertContainer<decltype(tmpSet)>(base->functionModifiers());
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
for (auto const& mod: inheritedModifiers(*base))
|
2019-09-16 12:33:43 +00:00
|
|
|
tmpSet.insert(mod);
|
|
|
|
|
|
|
|
set += tmpSet;
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:58:30 +00:00
|
|
|
return m_contractBaseModifiers[&_contract] = set;
|
2019-09-16 12:33:43 +00:00
|
|
|
}
|
2019-09-09 16:22:02 +00:00
|
|
|
|
|
|
|
void ContractLevelChecker::checkPayableFallbackWithoutReceive(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
if (auto const* fallback = _contract.fallbackFunction())
|
|
|
|
if (fallback->isPayable() && !_contract.interfaceFunctionList().empty() && !_contract.receiveFunction())
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_contract.location(),
|
|
|
|
"This contract has a payable fallback function, but no receive ether function. Consider adding a receive ether function.",
|
|
|
|
SecondarySourceLocation{}.append("The payable fallback function is defined here.", fallback->location())
|
|
|
|
);
|
|
|
|
}
|