forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverrideChecker.h
196 lines (161 loc) · 6.46 KB
/
OverrideChecker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Component that verifies override properties.
*/
#pragma once
#include <libsolidity/ast/ASTForward.h>
#include <libsolidity/ast/ASTEnums.h>
#include <liblangutil/SourceLocation.h>
#include <map>
#include <functional>
#include <set>
#include <variant>
#include <optional>
namespace solidity::langutil
{
class ErrorReporter;
struct ErrorId;
struct SourceLocation;
}
namespace solidity::frontend
{
class FunctionType;
class ModifierType;
/**
* Class that represents a function, public state variable or modifier
* and helps with overload checking.
* Regular comparison is performed based on AST node, while CompareBySignature
* results in two elements being equal when they can override each
* other.
*/
class OverrideProxy
{
public:
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} {}
bool operator<(OverrideProxy const& _other) const;
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;
/// @returns the externally callable function type
FunctionType const* externalFunctionType() const;
/// @returns the (unmodified) function type
FunctionType const* originalFunctionType() const;
ModifierType const* modifierType() const;
Declaration const* declaration() 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.
* Compares functions based on their "externally callable" type.
* 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:
std::variant<
FunctionDefinition const*,
ModifierDefinition const*,
VariableDeclaration const*
> m_item;
std::shared_ptr<OverrideComparator> mutable m_comparator;
};
/**
* Component that verifies override properties.
*/
class OverrideChecker
{
public:
using OverrideProxyBySignatureMultiSet = std::multiset<OverrideProxy, OverrideProxy::CompareBySignature>;
/// @param _errorReporter provides the error logging functionality.
explicit OverrideChecker(langutil::ErrorReporter& _errorReporter):
m_errorReporter(_errorReporter)
{}
void check(ContractDefinition const& _contract);
struct CompareByID
{
bool operator()(ContractDefinition const* _a, ContractDefinition const* _b) const;
};
/// 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.
OverrideProxyBySignatureMultiSet const& inheritedFunctions(ContractDefinition const& _contract) const;
OverrideProxyBySignatureMultiSet const& inheritedModifiers(ContractDefinition const& _contract) const;
private:
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.
void checkOverride(OverrideProxy const& _overriding, OverrideProxy const& _super);
void overrideListError(
OverrideProxy const& _item,
std::set<ContractDefinition const*, CompareByID> _secondary,
langutil::ErrorId _error,
std::string const& _message1,
std::string const& _message2
);
void overrideError(
OverrideProxy const& _overriding,
OverrideProxy const& _super,
langutil::ErrorId _error,
std::string const& _message,
std::optional<std::string> const& _secondaryMsg = {}
);
/// 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;
void checkAmbiguousOverridesInternal(std::set<OverrideProxy> _baseCallables, langutil::SourceLocation const& _location) const;
/// Resolves an override list of UserDefinedTypeNames to a list of contracts.
std::set<ContractDefinition const*, CompareByID> resolveOverrideList(OverrideSpecifier const& _overrides) const;
void checkOverrideList(OverrideProxy _item, OverrideProxyBySignatureMultiSet const& _inherited);
langutil::ErrorReporter& m_errorReporter;
/// Cache for inheritedFunctions().
std::map<ContractDefinition const*, OverrideProxyBySignatureMultiSet> mutable m_inheritedFunctions;
std::map<ContractDefinition const*, OverrideProxyBySignatureMultiSet> mutable m_inheritedModifiers;
};
}