2019-12-09 22:43:58 +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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-12-09 22:43:58 +00:00
|
|
|
/**
|
|
|
|
* Component that verifies overloads, abstract contracts, function clashes and others
|
|
|
|
* checks at contract or function level.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/analysis/OverrideChecker.h>
|
|
|
|
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
|
|
|
#include <libsolidity/analysis/TypeChecker.h>
|
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Visitor.h>
|
2019-12-09 16:51:53 +00:00
|
|
|
|
2019-12-09 22:43:58 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
|
|
|
|
using solidity::util::GenericVisitor;
|
|
|
|
using solidity::util::contains_if;
|
|
|
|
using solidity::util::joinHumanReadable;
|
2019-12-09 22:43:58 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// Helper struct to do a search by name
|
|
|
|
struct MatchByName
|
|
|
|
{
|
|
|
|
string const& m_name;
|
2019-12-10 08:48:01 +00:00
|
|
|
bool operator()(OverrideProxy const& _item)
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
return _item.name() == m_name;
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-11 12:39:59 +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(set<OverrideProxy> const& _baseCallables)
|
|
|
|
{
|
|
|
|
for (auto const& baseFunction: _baseCallables)
|
|
|
|
addEdge(0, visit(baseFunction));
|
|
|
|
}
|
|
|
|
std::map<OverrideProxy, int> nodes;
|
|
|
|
std::map<int, OverrideProxy> nodeInv;
|
|
|
|
std::map<int, std::set<int>> edges;
|
2020-06-02 13:42:46 +00:00
|
|
|
size_t numNodes = 2;
|
2019-12-11 12:39:59 +00:00
|
|
|
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(OverrideProxy const& _function)
|
|
|
|
{
|
|
|
|
auto it = nodes.find(_function);
|
|
|
|
if (it != nodes.end())
|
|
|
|
return it->second;
|
2020-06-02 13:42:46 +00:00
|
|
|
int currentNode = static_cast<int>(numNodes++);
|
2019-12-11 12:39:59 +00:00
|
|
|
nodes[_function] = currentNode;
|
|
|
|
nodeInv[currentNode] = _function;
|
|
|
|
if (_function.overrides())
|
|
|
|
for (auto const& baseFunction: _function.baseFunctions())
|
|
|
|
addEdge(currentNode, visit(baseFunction));
|
|
|
|
else
|
|
|
|
addEdge(currentNode, 1);
|
|
|
|
|
|
|
|
return currentNode;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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<OverrideProxy> const& cutVertices() const { return m_cutVertices; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
OverrideGraph const& m_graph;
|
|
|
|
|
|
|
|
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<OverrideProxy> m_cutVertices{};
|
|
|
|
|
2020-06-02 13:42:46 +00:00
|
|
|
void run(size_t _u = 0, size_t _depth = 0)
|
2019-12-11 12:39:59 +00:00
|
|
|
{
|
|
|
|
m_visited.at(_u) = true;
|
2020-06-02 13:42:46 +00:00
|
|
|
m_depths.at(_u) = m_low.at(_u) = static_cast<int>(_depth);
|
|
|
|
for (int const v: m_graph.edges.at(static_cast<int>(_u)))
|
|
|
|
{
|
|
|
|
auto const vInd = static_cast<size_t>(v);
|
|
|
|
if (!m_visited.at(vInd))
|
2019-12-11 12:39:59 +00:00
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
m_parent[vInd] = static_cast<int>(_u);
|
|
|
|
run(vInd, _depth + 1);
|
|
|
|
if (m_low[vInd] >= m_depths[_u] && m_parent[_u] != -1)
|
|
|
|
m_cutVertices.insert(m_graph.nodeInv.at(static_cast<int>(_u)));
|
|
|
|
m_low[_u] = min(m_low[_u], m_low[vInd]);
|
2019-12-11 12:39:59 +00:00
|
|
|
}
|
|
|
|
else if (v != m_parent[_u])
|
2020-06-02 13:42:46 +00:00
|
|
|
m_low[_u] = min(m_low[_u], m_depths[vInd]);
|
|
|
|
}
|
2019-12-11 12:39:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-09 22:43:58 +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);
|
2019-12-09 16:01:31 +00:00
|
|
|
if (contract)
|
|
|
|
resolvedContracts.emplace_back(contract);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resolvedContracts;
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:18:22 +00:00
|
|
|
vector<ASTPointer<IdentifierPath>> sortByContract(vector<ASTPointer<IdentifierPath>> const& _list)
|
2019-12-10 00:02:07 +00:00
|
|
|
{
|
|
|
|
auto sorted = _list;
|
|
|
|
|
|
|
|
stable_sort(sorted.begin(), sorted.end(),
|
2020-08-11 09:18:22 +00:00
|
|
|
[] (ASTPointer<IdentifierPath> _a, ASTPointer<IdentifierPath> _b) {
|
2019-12-10 00:02:07 +00:00
|
|
|
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-12-10 08:48:01 +00:00
|
|
|
OverrideProxy makeOverrideProxy(CallableDeclaration const& _callable)
|
|
|
|
{
|
|
|
|
if (auto const* fun = dynamic_cast<FunctionDefinition const*>(&_callable))
|
|
|
|
return OverrideProxy{fun};
|
|
|
|
else if (auto const* mod = dynamic_cast<ModifierDefinition const*>(&_callable))
|
|
|
|
return OverrideProxy{mod};
|
|
|
|
else
|
|
|
|
solAssert(false, "Invalid call to makeOverrideProxy.");
|
|
|
|
return {};
|
|
|
|
}
|
2019-12-10 00:02:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::operator<(OverrideProxy const& _other) const
|
2019-12-10 08:48:01 +00:00
|
|
|
{
|
|
|
|
return id() < _other.id();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::isVariable() const
|
|
|
|
{
|
|
|
|
return holds_alternative<VariableDeclaration const*>(m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::isFunction() const
|
|
|
|
{
|
|
|
|
return holds_alternative<FunctionDefinition const*>(m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::isModifier() const
|
|
|
|
{
|
|
|
|
return holds_alternative<ModifierDefinition const*>(m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::CompareBySignature::operator()(OverrideProxy const& _a, OverrideProxy const& _b) const
|
|
|
|
{
|
|
|
|
return _a.overrideComparator() < _b.overrideComparator();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OverrideProxy::id() const
|
2019-12-10 00:02:07 +00:00
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
2020-06-02 13:42:46 +00:00
|
|
|
[&](auto const* _item) -> size_t { return static_cast<size_t>(_item->id()); }
|
2019-12-10 08:48:01 +00:00
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<OverrideSpecifier> OverrideProxy::overrides() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](auto const* _item) { return _item->overrides(); }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
set<OverrideProxy> OverrideProxy::baseFunctions() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](auto const* _item) -> set<OverrideProxy> {
|
|
|
|
set<OverrideProxy> ret;
|
|
|
|
for (auto const* f: _item->annotation().baseFunctions)
|
|
|
|
ret.insert(makeOverrideProxy(*f));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverrideProxy::storeBaseFunction(OverrideProxy const& _base) const
|
|
|
|
{
|
|
|
|
std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _item) {
|
|
|
|
_item->annotation().baseFunctions.emplace(std::get<FunctionDefinition const*>(_base.m_item));
|
2019-12-10 00:02:07 +00:00
|
|
|
},
|
2019-12-10 08:48:01 +00:00
|
|
|
[&](ModifierDefinition const* _item) {
|
|
|
|
_item->annotation().baseFunctions.emplace(std::get<ModifierDefinition const*>(_base.m_item));
|
2019-12-10 00:02:07 +00:00
|
|
|
},
|
2019-12-10 08:48:01 +00:00
|
|
|
[&](VariableDeclaration const* _item) {
|
|
|
|
_item->annotation().baseFunctions.emplace(std::get<FunctionDefinition const*>(_base.m_item));
|
|
|
|
}
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
string const& OverrideProxy::name() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](auto const* _item) -> string const& { return _item->name(); }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
ContractDefinition const& OverrideProxy::contract() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](auto const* _item) -> ContractDefinition const& {
|
|
|
|
return dynamic_cast<ContractDefinition const&>(*_item->scope());
|
2019-12-10 00:02:07 +00:00
|
|
|
}
|
2019-12-10 08:48:01 +00:00
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
string const& OverrideProxy::contractName() const
|
|
|
|
{
|
|
|
|
return contract().name();
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility OverrideProxy::visibility() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _item) { return _item->visibility(); },
|
|
|
|
[&](ModifierDefinition const* _item) { return _item->visibility(); },
|
|
|
|
[&](VariableDeclaration const*) { return Visibility::External; }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
StateMutability OverrideProxy::stateMutability() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _item) { return _item->stateMutability(); },
|
|
|
|
[&](ModifierDefinition const*) { solAssert(false, "Requested state mutability from modifier."); return StateMutability{}; },
|
2020-07-14 18:18:34 +00:00
|
|
|
[&](VariableDeclaration const* _var) { return _var->isConstant() ? StateMutability::Pure : StateMutability::View; }
|
2019-12-10 08:48:01 +00:00
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::virtualSemantics() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _item) { return _item->virtualSemantics(); },
|
|
|
|
[&](ModifierDefinition const* _item) { return _item->virtualSemantics(); },
|
|
|
|
[&](VariableDeclaration const*) { return false; }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
Token OverrideProxy::functionKind() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _item) { return _item->kind(); },
|
|
|
|
[&](ModifierDefinition const*) { return Token::Function; },
|
|
|
|
[&](VariableDeclaration const*) { return Token::Function; }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionType const* OverrideProxy::functionType() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
2020-05-26 08:48:17 +00:00
|
|
|
[&](FunctionDefinition const* _item) { return FunctionType(*_item).asExternallyCallableFunction(false); },
|
|
|
|
[&](VariableDeclaration const* _item) { return FunctionType(*_item).asExternallyCallableFunction(false); },
|
2019-12-10 08:48:01 +00:00
|
|
|
[&](ModifierDefinition const*) -> FunctionType const* { solAssert(false, "Requested function type of modifier."); return nullptr; }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
ModifierType const* OverrideProxy::modifierType() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const*) -> ModifierType const* { solAssert(false, "Requested modifier type of function."); return nullptr; },
|
|
|
|
[&](VariableDeclaration const*) -> ModifierType const* { solAssert(false, "Requested modifier type of variable."); return nullptr; },
|
|
|
|
[&](ModifierDefinition const* _modifier) -> ModifierType const* { return TypeProvider::modifier(*_modifier); }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
2020-04-15 10:42:15 +00:00
|
|
|
|
|
|
|
Declaration const* OverrideProxy::declaration() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _function) -> Declaration const* { return _function; },
|
|
|
|
[&](VariableDeclaration const* _variable) -> Declaration const* { return _variable; },
|
|
|
|
[&](ModifierDefinition const* _modifier) -> Declaration const* { return _modifier; }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
SourceLocation const& OverrideProxy::location() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](auto const* _item) -> SourceLocation const& { return _item->location(); }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
string OverrideProxy::astNodeName() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const*) { return "function"; },
|
|
|
|
[&](ModifierDefinition const*) { return "modifier"; },
|
|
|
|
[&](VariableDeclaration const*) { return "public state variable"; },
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
string OverrideProxy::astNodeNameCapitalized() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const*) { return "Function"; },
|
|
|
|
[&](ModifierDefinition const*) { return "Modifier"; },
|
|
|
|
[&](VariableDeclaration const*) { return "Public state variable"; },
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
string OverrideProxy::distinguishingProperty() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const*) { return "name and parameter types"; },
|
|
|
|
[&](ModifierDefinition const*) { return "name"; },
|
|
|
|
[&](VariableDeclaration const*) { return "name and parameter types"; },
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::unimplemented() const
|
|
|
|
{
|
|
|
|
return std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _item) { return !_item->isImplemented(); },
|
2020-04-15 10:42:15 +00:00
|
|
|
[&](ModifierDefinition const* _item) { return !_item->isImplemented(); },
|
2019-12-10 08:48:01 +00:00
|
|
|
[&](VariableDeclaration const*) { return false; }
|
|
|
|
}, m_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideProxy::OverrideComparator::operator<(OverrideComparator const& _other) const
|
|
|
|
{
|
|
|
|
if (name != _other.name)
|
|
|
|
return name < _other.name;
|
|
|
|
|
|
|
|
if (!functionKind || !_other.functionKind)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (functionKind != _other.functionKind)
|
|
|
|
return *functionKind < *_other.functionKind;
|
|
|
|
|
2020-11-11 14:24:11 +00:00
|
|
|
// Parameters do not matter for non-regular functions.
|
|
|
|
if (functionKind != Token::Function)
|
|
|
|
return false;
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
if (!parameterTypes || !_other.parameterTypes)
|
|
|
|
return false;
|
2019-12-10 00:02:07 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
return boost::lexicographical_compare(*parameterTypes, *_other.parameterTypes);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxy::OverrideComparator const& OverrideProxy::overrideComparator() const
|
|
|
|
{
|
|
|
|
if (!m_comparator)
|
|
|
|
{
|
|
|
|
m_comparator = make_shared<OverrideComparator>(std::visit(GenericVisitor{
|
|
|
|
[&](FunctionDefinition const* _function)
|
|
|
|
{
|
|
|
|
vector<string> paramTypes;
|
|
|
|
for (Type const* t: functionType()->parameterTypes())
|
|
|
|
paramTypes.emplace_back(t->richIdentifier());
|
|
|
|
return OverrideComparator{
|
|
|
|
_function->name(),
|
|
|
|
_function->kind(),
|
|
|
|
std::move(paramTypes)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](VariableDeclaration const* _var)
|
|
|
|
{
|
|
|
|
vector<string> paramTypes;
|
|
|
|
for (Type const* t: functionType()->parameterTypes())
|
|
|
|
paramTypes.emplace_back(t->richIdentifier());
|
|
|
|
return OverrideComparator{
|
|
|
|
_var->name(),
|
|
|
|
Token::Function,
|
|
|
|
std::move(paramTypes)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[&](ModifierDefinition const* _mod)
|
|
|
|
{
|
|
|
|
return OverrideComparator{
|
|
|
|
_mod->name(),
|
|
|
|
{},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, m_item));
|
|
|
|
}
|
|
|
|
|
|
|
|
return *m_comparator;
|
|
|
|
}
|
2019-12-10 00:02:07 +00:00
|
|
|
|
2019-12-11 12:39:59 +00:00
|
|
|
bool OverrideChecker::CompareByID::operator()(ContractDefinition const* _a, ContractDefinition const* _b) const
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
if (!_a || !_b)
|
|
|
|
return _a < _b;
|
|
|
|
|
|
|
|
return _a->id() < _b->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverrideChecker::check(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
checkIllegalOverrides(_contract);
|
|
|
|
checkAmbiguousOverrides(_contract);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverrideChecker::checkIllegalOverrides(ContractDefinition const& _contract)
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxyBySignatureMultiSet const& inheritedFuncs = inheritedFunctions(_contract);
|
|
|
|
OverrideProxyBySignatureMultiSet const& inheritedMods = inheritedModifiers(_contract);
|
2019-12-09 22:43:58 +00:00
|
|
|
|
|
|
|
for (ModifierDefinition const* modifier: _contract.functionModifiers())
|
|
|
|
{
|
|
|
|
if (contains_if(inheritedFuncs, MatchByName{modifier->name()}))
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
5631_error,
|
2019-12-09 22:43:58 +00:00
|
|
|
modifier->location(),
|
2019-12-10 16:17:41 +00:00
|
|
|
"Override changes function or public state variable to modifier."
|
2019-12-09 22:43:58 +00:00
|
|
|
);
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
checkOverrideList(OverrideProxy{modifier}, inheritedMods);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (FunctionDefinition const* function: _contract.definedFunctions())
|
|
|
|
{
|
|
|
|
if (function->isConstructor())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (contains_if(inheritedMods, MatchByName{function->name()}))
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.typeError(1469_error, function->location(), "Override changes modifier to function.");
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
checkOverrideList(OverrideProxy{function}, inheritedFuncs);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
2019-12-10 16:17:41 +00:00
|
|
|
for (auto const* stateVar: _contract.stateVariables())
|
|
|
|
{
|
|
|
|
if (!stateVar->isPublic())
|
2020-06-03 11:53:11 +00:00
|
|
|
{
|
|
|
|
if (stateVar->overrides())
|
|
|
|
m_errorReporter.typeError(8022_error, stateVar->location(), "Override can only be used with public state variables.");
|
|
|
|
|
2019-12-10 16:17:41 +00:00
|
|
|
continue;
|
2020-06-03 11:53:11 +00:00
|
|
|
}
|
2019-12-10 16:17:41 +00:00
|
|
|
|
|
|
|
if (contains_if(inheritedMods, MatchByName{stateVar->name()}))
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.typeError(1456_error, stateVar->location(), "Override changes modifier to public state variable.");
|
2019-12-10 16:17:41 +00:00
|
|
|
|
|
|
|
checkOverrideList(OverrideProxy{stateVar}, inheritedFuncs);
|
|
|
|
}
|
|
|
|
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
void OverrideChecker::checkOverride(OverrideProxy const& _overriding, OverrideProxy const& _super)
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
solAssert(_super.isModifier() == _overriding.isModifier(), "");
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 16:17:41 +00:00
|
|
|
if (_super.isFunction() || _super.isModifier())
|
|
|
|
_overriding.storeBaseFunction(_super);
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
if (_overriding.isModifier() && *_overriding.modifierType() != *_super.modifierType())
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
1078_error,
|
2019-12-10 08:48:01 +00:00
|
|
|
_overriding.location(),
|
|
|
|
"Override changes modifier signature."
|
|
|
|
);
|
2019-12-09 22:43:58 +00:00
|
|
|
|
|
|
|
if (!_overriding.overrides())
|
2020-07-07 09:57:16 +00:00
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
9456_error,
|
|
|
|
"Overriding " + _overriding.astNodeName() + " is missing \"override\" specifier.",
|
|
|
|
"Overridden " + _overriding.astNodeName() + " is here:"
|
|
|
|
);
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 16:17:41 +00:00
|
|
|
if (_super.isVariable())
|
|
|
|
overrideError(
|
|
|
|
_super,
|
|
|
|
_overriding,
|
2020-05-12 00:26:02 +00:00
|
|
|
1452_error,
|
2019-12-10 16:17:41 +00:00
|
|
|
"Cannot override public state variable.",
|
|
|
|
"Overriding " + _overriding.astNodeName() + " is here:"
|
|
|
|
);
|
|
|
|
else if (!_super.virtualSemantics())
|
2019-12-09 22:43:58 +00:00
|
|
|
overrideError(
|
|
|
|
_super,
|
|
|
|
_overriding,
|
2020-05-12 00:26:02 +00:00
|
|
|
4334_error,
|
2019-12-10 08:48:01 +00:00
|
|
|
"Trying to override non-virtual " + _super.astNodeName() + ". Did you forget to add \"virtual\"?",
|
|
|
|
"Overriding " + _overriding.astNodeName() + " is here:"
|
2019-12-09 22:43:58 +00:00
|
|
|
);
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
if (_overriding.isVariable())
|
|
|
|
{
|
|
|
|
if (_super.visibility() != Visibility::External)
|
2020-07-07 09:57:16 +00:00
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
5225_error,
|
|
|
|
"Public state variables can only override functions with external visibility.",
|
|
|
|
"Overridden function is here:"
|
|
|
|
);
|
2019-12-10 08:48:01 +00:00
|
|
|
solAssert(_overriding.visibility() == Visibility::External, "");
|
|
|
|
}
|
|
|
|
else if (_overriding.visibility() != _super.visibility())
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
// Visibility change from external to public is fine.
|
|
|
|
// Any other change is disallowed.
|
|
|
|
if (!(
|
2019-12-10 14:54:09 +00:00
|
|
|
_super.visibility() == Visibility::External &&
|
|
|
|
_overriding.visibility() == Visibility::Public
|
2019-12-09 22:43:58 +00:00
|
|
|
))
|
2020-07-07 09:57:16 +00:00
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
9098_error,
|
|
|
|
"Overriding " + _overriding.astNodeName() + " visibility differs.",
|
|
|
|
"Overridden " + _overriding.astNodeName() + " is here:"
|
|
|
|
);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
if (_super.isFunction())
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
FunctionType const* functionType = _overriding.functionType();
|
|
|
|
FunctionType const* superType = _super.functionType();
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2020-11-11 14:24:11 +00:00
|
|
|
if (_overriding.functionKind() != Token::Fallback)
|
|
|
|
{
|
|
|
|
solAssert(functionType->hasEqualParameterTypes(*superType), "Override doesn't have equal parameters!");
|
|
|
|
|
|
|
|
if (!functionType->hasEqualReturnTypes(*superType))
|
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
4822_error,
|
|
|
|
"Overriding " + _overriding.astNodeName() + " return types differ.",
|
|
|
|
"Overridden " + _overriding.astNodeName() + " is here:"
|
|
|
|
);
|
|
|
|
}
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2020-07-14 18:11:01 +00:00
|
|
|
// Stricter mutability is always okay except when super is Payable
|
|
|
|
if (
|
|
|
|
(_overriding.isFunction() || _overriding.isVariable()) &&
|
|
|
|
(
|
2020-07-06 14:36:06 +00:00
|
|
|
_overriding.stateMutability() > _super.stateMutability() ||
|
|
|
|
_super.stateMutability() == StateMutability::Payable
|
2020-07-14 18:11:01 +00:00
|
|
|
) &&
|
|
|
|
_overriding.stateMutability() != _super.stateMutability()
|
|
|
|
)
|
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
6959_error,
|
|
|
|
"Overriding " +
|
|
|
|
_overriding.astNodeName() +
|
|
|
|
" changes state mutability from \"" +
|
|
|
|
stateMutabilityToString(_super.stateMutability()) +
|
|
|
|
"\" to \"" +
|
|
|
|
stateMutabilityToString(_overriding.stateMutability()) +
|
|
|
|
"\"."
|
|
|
|
);
|
|
|
|
|
|
|
|
if (_overriding.unimplemented() && !_super.unimplemented())
|
|
|
|
overrideError(
|
|
|
|
_overriding,
|
|
|
|
_super,
|
|
|
|
4593_error,
|
|
|
|
"Overriding an implemented function with an unimplemented function is not allowed."
|
|
|
|
);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OverrideChecker::overrideListError(
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxy const& _item,
|
2019-12-11 12:39:59 +00:00
|
|
|
set<ContractDefinition const*, CompareByID> _secondary,
|
2020-05-12 00:26:02 +00:00
|
|
|
ErrorId _error,
|
2019-12-09 22:43:58 +00:00
|
|
|
string const& _message1,
|
|
|
|
string const& _message2
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// 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());
|
2019-12-09 16:51:53 +00:00
|
|
|
names.insert("\"" + c->name() + "\"");
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
string contractSingularPlural = "contract ";
|
|
|
|
if (_secondary.size() > 1)
|
|
|
|
contractSingularPlural = "contracts ";
|
|
|
|
|
|
|
|
m_errorReporter.typeError(
|
2020-05-12 00:26:02 +00:00
|
|
|
_error,
|
2019-12-10 08:48:01 +00:00
|
|
|
_item.overrides() ? _item.overrides()->location() : _item.location(),
|
2019-12-09 22:43:58 +00:00
|
|
|
ssl,
|
|
|
|
_message1 +
|
|
|
|
contractSingularPlural +
|
|
|
|
_message2 +
|
|
|
|
joinHumanReadable(names, ", ", " and ") +
|
|
|
|
"."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:26:02 +00:00
|
|
|
void OverrideChecker::overrideError(Declaration const& _overriding, Declaration const& _super, ErrorId _error, string const& _message, string const& _secondaryMsg)
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
m_errorReporter.typeError(
|
2020-05-12 00:26:02 +00:00
|
|
|
_error,
|
2019-12-09 22:43:58 +00:00
|
|
|
_overriding.location(),
|
|
|
|
SecondarySourceLocation().append(_secondaryMsg, _super.location()),
|
|
|
|
_message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
|
2020-05-12 00:26:02 +00:00
|
|
|
void OverrideChecker::overrideError(OverrideProxy const& _overriding, OverrideProxy const& _super, ErrorId _error, string const& _message, string const& _secondaryMsg)
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
m_errorReporter.typeError(
|
2020-05-12 00:26:02 +00:00
|
|
|
_error,
|
2019-12-10 08:48:01 +00:00
|
|
|
_overriding.location(),
|
|
|
|
SecondarySourceLocation().append(_secondaryMsg, _super.location()),
|
|
|
|
_message
|
|
|
|
);
|
|
|
|
}
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
void OverrideChecker::checkAmbiguousOverrides(ContractDefinition const& _contract) const
|
|
|
|
{
|
2019-12-09 22:43:58 +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.
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxyBySignatureMultiSet nonOverriddenFunctions = inheritedFunctions(_contract);
|
|
|
|
|
2019-12-09 22:43:58 +00:00
|
|
|
// Remove all functions that match the signature of a function in the current contract.
|
2019-12-10 08:48:01 +00:00
|
|
|
for (FunctionDefinition const* f: _contract.definedFunctions())
|
|
|
|
nonOverriddenFunctions.erase(OverrideProxy{f});
|
2019-12-10 16:17:41 +00:00
|
|
|
for (VariableDeclaration const* v: _contract.stateVariables())
|
|
|
|
if (v->isPublic())
|
|
|
|
nonOverriddenFunctions.erase(OverrideProxy{v});
|
2019-12-09 22:43:58 +00:00
|
|
|
|
|
|
|
// Walk through the set of functions signature by signature.
|
|
|
|
for (auto it = nonOverriddenFunctions.cbegin(); it != nonOverriddenFunctions.cend();)
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
std::set<OverrideProxy> baseFunctions;
|
2019-12-09 22:43:58 +00:00
|
|
|
for (auto nextSignature = nonOverriddenFunctions.upper_bound(*it); it != nextSignature; ++it)
|
|
|
|
baseFunctions.insert(*it);
|
|
|
|
|
|
|
|
checkAmbiguousOverridesInternal(std::move(baseFunctions), _contract.location());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxyBySignatureMultiSet modifiers = inheritedModifiers(_contract);
|
|
|
|
for (ModifierDefinition const* mod: _contract.functionModifiers())
|
|
|
|
modifiers.erase(OverrideProxy{mod});
|
|
|
|
|
2019-12-09 22:43:58 +00:00
|
|
|
for (auto it = modifiers.cbegin(); it != modifiers.cend();)
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
std::set<OverrideProxy> baseModifiers;
|
2019-12-09 22:43:58 +00:00
|
|
|
for (auto next = modifiers.upper_bound(*it); it != next; ++it)
|
|
|
|
baseModifiers.insert(*it);
|
|
|
|
|
|
|
|
checkAmbiguousOverridesInternal(std::move(baseModifiers), _contract.location());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
void OverrideChecker::checkAmbiguousOverridesInternal(set<OverrideProxy> _baseCallables, SourceLocation const& _location) const
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
if (_baseCallables.size() <= 1)
|
|
|
|
return;
|
|
|
|
|
2019-12-11 12:39:59 +00:00
|
|
|
OverrideGraph overrideGraph(_baseCallables);
|
|
|
|
CutVertexFinder cutVertexFinder{overrideGraph};
|
2019-12-09 22:43:58 +00:00
|
|
|
|
|
|
|
// Remove all base functions overridden by cut vertices (they don't need to be overridden).
|
2019-12-10 08:48:01 +00:00
|
|
|
for (OverrideProxy const& function: cutVertexFinder.cutVertices())
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
std::set<OverrideProxy> toTraverse = function.baseFunctions();
|
2019-12-09 22:43:58 +00:00
|
|
|
while (!toTraverse.empty())
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxy base = *toTraverse.begin();
|
2019-12-09 22:43:58 +00:00
|
|
|
toTraverse.erase(toTraverse.begin());
|
|
|
|
_baseCallables.erase(base);
|
2019-12-10 08:48:01 +00:00
|
|
|
for (OverrideProxy const& f: base.baseFunctions())
|
2019-12-09 22:43:58 +00:00
|
|
|
toTraverse.insert(f);
|
|
|
|
}
|
|
|
|
// Remove unimplemented base functions at the cut vertices itself as well.
|
2019-12-10 08:48:01 +00:00
|
|
|
if (function.unimplemented())
|
|
|
|
_baseCallables.erase(function);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If more than one function is left, they have to be overridden.
|
|
|
|
if (_baseCallables.size() <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SecondarySourceLocation ssl;
|
2019-12-10 08:48:01 +00:00
|
|
|
for (OverrideProxy const& baseFunction: _baseCallables)
|
|
|
|
ssl.append("Definition in \"" + baseFunction.contractName() + "\": ", baseFunction.location());
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 16:17:41 +00:00
|
|
|
string callableName = _baseCallables.begin()->astNodeName();
|
|
|
|
if (_baseCallables.begin()->isVariable())
|
|
|
|
callableName = "function";
|
2019-12-10 08:48:01 +00:00
|
|
|
string distinguishigProperty = _baseCallables.begin()->distinguishingProperty();
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
bool foundVariable = false;
|
|
|
|
for (auto const& base: _baseCallables)
|
|
|
|
if (base.isVariable())
|
|
|
|
foundVariable = true;
|
|
|
|
|
|
|
|
string message =
|
2019-12-09 22:43:58 +00:00
|
|
|
"Derived contract must override " + callableName + " \"" +
|
2019-12-10 08:48:01 +00:00
|
|
|
_baseCallables.begin()->name() +
|
|
|
|
"\". Two or more base classes define " + callableName + " with same " + distinguishigProperty + ".";
|
|
|
|
|
|
|
|
if (foundVariable)
|
|
|
|
message +=
|
|
|
|
" Since one of the bases defines a public state variable which cannot be overridden, "
|
|
|
|
"you have to change the inheritance layout or the names of the functions.";
|
|
|
|
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.typeError(6480_error, _location, ssl, message);
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 12:39:59 +00:00
|
|
|
set<ContractDefinition const*, OverrideChecker::CompareByID> OverrideChecker::resolveOverrideList(OverrideSpecifier const& _overrides) const
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-11 12:39:59 +00:00
|
|
|
set<ContractDefinition const*, CompareByID> resolved;
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2020-08-11 09:18:22 +00:00
|
|
|
for (ASTPointer<IdentifierPath> const& override: _overrides.overrides())
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
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-10 08:48:01 +00:00
|
|
|
void OverrideChecker::checkOverrideList(OverrideProxy _item, OverrideProxyBySignatureMultiSet const& _inherited)
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-11 12:39:59 +00:00
|
|
|
set<ContractDefinition const*, CompareByID> specifiedContracts =
|
2019-12-10 08:48:01 +00:00
|
|
|
_item.overrides() ?
|
|
|
|
resolveOverrideList(*_item.overrides()) :
|
2019-12-09 22:43:58 +00:00
|
|
|
decltype(specifiedContracts){};
|
|
|
|
|
|
|
|
// Check for duplicates in override list
|
2019-12-10 08:48:01 +00:00
|
|
|
if (_item.overrides() && specifiedContracts.size() != _item.overrides()->overrides().size())
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
// Sort by contract id to find duplicate for error reporting
|
2020-08-11 09:18:22 +00:00
|
|
|
vector<ASTPointer<IdentifierPath>> list =
|
2019-12-10 08:48:01 +00:00
|
|
|
sortByContract(_item.overrides()->overrides());
|
2019-12-09 22:43:58 +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(
|
2020-05-05 22:38:28 +00:00
|
|
|
4520_error,
|
2019-12-09 22:43:58 +00:00
|
|
|
list[i]->location(),
|
|
|
|
ssl,
|
2020-05-12 00:26:02 +00:00
|
|
|
"Duplicate contract \"" +
|
2020-08-11 09:18:22 +00:00
|
|
|
joinHumanReadable(list[i]->path(), ".") +
|
2020-05-12 00:26:02 +00:00
|
|
|
"\" found in override list of \"" +
|
|
|
|
_item.name() +
|
|
|
|
"\"."
|
2019-12-09 22:43:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:39:59 +00:00
|
|
|
set<ContractDefinition const*, CompareByID> expectedContracts;
|
2019-12-09 22:43:58 +00:00
|
|
|
|
|
|
|
// Build list of expected contracts
|
2019-12-10 08:48:01 +00:00
|
|
|
for (auto [begin, end] = _inherited.equal_range(_item); begin != end; begin++)
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
// Validate the override
|
2019-12-10 08:48:01 +00:00
|
|
|
checkOverride(_item, *begin);
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
expectedContracts.insert(&begin->contract());
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
if (_item.overrides() && expectedContracts.empty())
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
7792_error,
|
2019-12-10 08:48:01 +00:00
|
|
|
_item.overrides()->location(),
|
|
|
|
_item.astNodeNameCapitalized() + " has override specified but does not override anything."
|
|
|
|
);
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-11 12:39:59 +00:00
|
|
|
set<ContractDefinition const*, CompareByID> missingContracts;
|
2019-12-09 22:43:58 +00:00
|
|
|
// If we expect only one contract, no contract needs to be specified
|
|
|
|
if (expectedContracts.size() > 1)
|
|
|
|
missingContracts = expectedContracts - specifiedContracts;
|
|
|
|
|
|
|
|
if (!missingContracts.empty())
|
|
|
|
overrideListError(
|
2019-12-10 08:48:01 +00:00
|
|
|
_item,
|
2019-12-09 22:43:58 +00:00
|
|
|
missingContracts,
|
2020-05-12 00:26:02 +00:00
|
|
|
4327_error,
|
2019-12-10 08:48:01 +00:00
|
|
|
_item.astNodeNameCapitalized() + " needs to specify overridden ",
|
2019-12-09 22:43:58 +00:00
|
|
|
""
|
|
|
|
);
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
auto surplusContracts = specifiedContracts - expectedContracts;
|
2019-12-09 22:43:58 +00:00
|
|
|
if (!surplusContracts.empty())
|
|
|
|
overrideListError(
|
2019-12-10 08:48:01 +00:00
|
|
|
_item,
|
2019-12-09 22:43:58 +00:00
|
|
|
surplusContracts,
|
2020-05-12 00:26:02 +00:00
|
|
|
2353_error,
|
2019-12-09 22:43:58 +00:00
|
|
|
"Invalid ",
|
|
|
|
"specified in override list: "
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideChecker::OverrideProxyBySignatureMultiSet const& OverrideChecker::inheritedFunctions(ContractDefinition const& _contract) const
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
|
|
|
if (!m_inheritedFunctions.count(&_contract))
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxyBySignatureMultiSet result;
|
2019-12-09 22:43:58 +00:00
|
|
|
|
|
|
|
for (auto const* base: resolveDirectBaseContracts(_contract))
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
set<OverrideProxy, OverrideProxy::CompareBySignature> functionsInBase;
|
2019-12-09 22:43:58 +00:00
|
|
|
for (FunctionDefinition const* fun: base->definedFunctions())
|
|
|
|
if (!fun->isConstructor())
|
2019-12-10 08:48:01 +00:00
|
|
|
functionsInBase.emplace(OverrideProxy{fun});
|
2019-12-10 16:17:41 +00:00
|
|
|
for (VariableDeclaration const* var: base->stateVariables())
|
|
|
|
if (var->isPublic())
|
|
|
|
functionsInBase.emplace(OverrideProxy{var});
|
2019-12-09 22:43:58 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
for (OverrideProxy const& func: inheritedFunctions(*base))
|
2019-12-09 22:43:58 +00:00
|
|
|
functionsInBase.insert(func);
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
result += functionsInBase;
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
m_inheritedFunctions[&_contract] = result;
|
2019-12-09 22:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_inheritedFunctions[&_contract];
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideChecker::OverrideProxyBySignatureMultiSet const& OverrideChecker::inheritedModifiers(ContractDefinition const& _contract) const
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
if (!m_inheritedModifiers.count(&_contract))
|
2019-12-09 22:43:58 +00:00
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
OverrideProxyBySignatureMultiSet result;
|
2019-12-10 00:02:07 +00:00
|
|
|
|
|
|
|
for (auto const* base: resolveDirectBaseContracts(_contract))
|
|
|
|
{
|
2019-12-10 08:48:01 +00:00
|
|
|
set<OverrideProxy, OverrideProxy::CompareBySignature> modifiersInBase;
|
|
|
|
for (ModifierDefinition const* mod: base->functionModifiers())
|
|
|
|
modifiersInBase.emplace(OverrideProxy{mod});
|
2019-12-10 00:02:07 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
for (OverrideProxy const& mod: inheritedModifiers(*base))
|
|
|
|
modifiersInBase.insert(mod);
|
2019-12-10 00:02:07 +00:00
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
result += modifiersInBase;
|
2019-12-10 00:02:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
m_inheritedModifiers[&_contract] = result;
|
2019-12-10 00:02:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:01 +00:00
|
|
|
return m_inheritedModifiers[&_contract];
|
2019-12-10 00:02:07 +00:00
|
|
|
}
|