solidity/libsolidity/analysis/OverrideChecker.h

194 lines
6.3 KiB
C
Raw Normal View History

/*
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 override properties.
*/
#pragma once
#include <libsolidity/ast/ASTForward.h>
2019-12-10 08:48:01 +00:00
#include <libsolidity/ast/ASTEnums.h>
#include <liblangutil/SourceLocation.h>
#include <map>
#include <functional>
#include <set>
2019-12-10 00:02:07 +00:00
#include <variant>
2019-12-10 08:48:01 +00:00
#include <optional>
2019-12-11 16:31:36 +00:00
namespace solidity::langutil
{
class ErrorReporter;
2019-12-10 08:48:01 +00:00
struct SourceLocation;
}
2019-12-11 16:31:36 +00:00
namespace solidity::frontend
{
2019-12-10 08:48:01 +00:00
class FunctionType;
class ModifierType;
2019-12-10 00:02:07 +00:00
/**
* Class that represents a function, public state variable or modifier
* and helps with overload checking.
2019-12-10 08:48:01 +00:00
* Regular comparison is performed based on AST node, while CompareBySignature
* results in two elements being equal when they can override each
2019-12-10 00:02:07 +00:00
* other.
*/
class OverrideProxy
{
public:
2019-12-10 08:48:01 +00:00
OverrideProxy() {}
explicit OverrideProxy(FunctionDefinition const* _fun): m_item{_fun} {}
explicit OverrideProxy(ModifierDefinition const* _mod): m_item{_mod} {}
explicit OverrideProxy(VariableDeclaration const* _var): m_item{_var} {}
2019-12-10 00:02:07 +00:00
bool operator<(OverrideProxy const& _other) const;
2019-12-10 08:48:01 +00:00
struct CompareBySignature
{
bool operator()(OverrideProxy const& _a, OverrideProxy const& _b) const;
};
bool isVariable() const;
bool isFunction() const;
bool isModifier() const;
size_t id() const;
std::shared_ptr<OverrideSpecifier> overrides() const;
std::set<OverrideProxy> baseFunctions() const;
/// This stores the item in the list of base items.
void storeBaseFunction(OverrideProxy const& _base) const;
std::string const& name() const;
ContractDefinition const& contract() const;
std::string const& contractName() const;
Visibility visibility() const;
StateMutability stateMutability() const;
bool virtualSemantics() const;
/// @returns receive / fallback / function (only the latter for modifiers and variables);
langutil::Token functionKind() const;
FunctionType const* functionType() const;
ModifierType const* modifierType() const;
langutil::SourceLocation const& location() const;
std::string astNodeName() const;
std::string astNodeNameCapitalized() const;
std::string distinguishingProperty() const;
/// @returns true if this AST elements supports the feature of being unimplemented
/// and is actually not implemented.
bool unimplemented() const;
/**
* Struct to help comparing override items about whether they override each other.
* Does not produce a total order.
*/
struct OverrideComparator
{
std::string name;
std::optional<langutil::Token> functionKind;
std::optional<std::vector<std::string>> parameterTypes;
bool operator<(OverrideComparator const& _other) const;
};
/// @returns a structure used to compare override items with regards to whether
/// they override each other.
OverrideComparator const& overrideComparator() const;
private:
2019-12-10 00:02:07 +00:00
std::variant<
FunctionDefinition const*,
ModifierDefinition const*,
VariableDeclaration const*
2019-12-10 08:48:01 +00:00
> m_item;
std::shared_ptr<OverrideComparator> mutable m_comparator;
2019-12-10 00:02:07 +00:00
};
/**
* Component that verifies override properties.
*/
class OverrideChecker
{
public:
/// @param _errorReporter provides the error logging functionality.
explicit OverrideChecker(langutil::ErrorReporter& _errorReporter):
m_errorReporter(_errorReporter)
{}
void check(ContractDefinition const& _contract);
private:
2019-12-11 12:39:59 +00:00
struct CompareByID
{
bool operator()(ContractDefinition const* _a, ContractDefinition const* _b) const;
};
void checkIllegalOverrides(ContractDefinition const& _contract);
/// Performs various checks related to @a _overriding overriding @a _super like
/// different return type, invalid visibility change, etc.
/// Works on functions, modifiers and public state variables.
/// Also stores @a _super as a base function of @a _function in its AST annotations.
2019-12-10 08:48:01 +00:00
void checkOverride(OverrideProxy const& _overriding, OverrideProxy const& _super);
void overrideListError(
2019-12-10 08:48:01 +00:00
OverrideProxy const& _item,
2019-12-11 12:39:59 +00:00
std::set<ContractDefinition const*, CompareByID> _secondary,
std::string const& _message1,
std::string const& _message2
);
void overrideError(
Declaration const& _overriding,
Declaration const& _super,
2019-12-10 08:48:01 +00:00
std::string const& _message,
std::string const& _secondaryMsg = "Overridden function is here:"
);
void overrideError(
OverrideProxy const& _overriding,
OverrideProxy const& _super,
std::string const& _message,
std::string const& _secondaryMsg = "Overridden function is here:"
);
/// Checks for functions in different base contracts which conflict with each
/// other and thus need to be overridden explicitly.
void checkAmbiguousOverrides(ContractDefinition const& _contract) const;
2019-12-10 08:48:01 +00:00
void checkAmbiguousOverridesInternal(std::set<OverrideProxy> _baseCallables, langutil::SourceLocation const& _location) const;
/// Resolves an override list of UserDefinedTypeNames to a list of contracts.
2019-12-11 12:39:59 +00:00
std::set<ContractDefinition const*, CompareByID> resolveOverrideList(OverrideSpecifier const& _overrides) const;
2019-12-10 08:48:01 +00:00
using OverrideProxyBySignatureMultiSet = std::multiset<OverrideProxy, OverrideProxy::CompareBySignature>;
void checkOverrideList(OverrideProxy _item, OverrideProxyBySignatureMultiSet const& _inherited);
2019-12-10 16:17:41 +00:00
/// Returns all functions of bases (including public state variables) that have not yet been overwritten.
/// May contain the same function multiple times when used with shared bases.
2019-12-10 08:48:01 +00:00
OverrideProxyBySignatureMultiSet const& inheritedFunctions(ContractDefinition const& _contract) const;
OverrideProxyBySignatureMultiSet const& inheritedModifiers(ContractDefinition const& _contract) const;
2019-12-10 00:02:07 +00:00
langutil::ErrorReporter& m_errorReporter;
/// Cache for inheritedFunctions().
2019-12-10 08:48:01 +00:00
std::map<ContractDefinition const*, OverrideProxyBySignatureMultiSet> mutable m_inheritedFunctions;
std::map<ContractDefinition const*, OverrideProxyBySignatureMultiSet> mutable m_inheritedModifiers;
};
}