forked from argotorg/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpgrade060.cpp
More file actions
197 lines (171 loc) · 5.72 KB
/
Upgrade060.cpp
File metadata and controls
197 lines (171 loc) · 5.72 KB
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
#include <tools/solidityUpgrade/Upgrade060.h>
#include <tools/solidityUpgrade/SourceTransform.h>
#include <libsolidity/analysis/OverrideChecker.h>
#include <libyul/AST.h>
#include <regex>
using namespace std;
using namespace solidity;
using namespace solidity::frontend;
using namespace solidity::tools;
void AbstractContract::endVisit(ContractDefinition const& _contract)
{
bool isFullyImplemented = _contract.annotation().unimplementedDeclarations->empty();
if (
!isFullyImplemented &&
!_contract.abstract() &&
!_contract.isInterface()
)
m_changes.emplace_back(
UpgradeChange::Level::Safe,
_contract.location(),
SourceTransform{m_charStreamProvider}.insertBeforeKeyword(_contract.location(), "contract", "abstract")
);
}
void OverridingFunction::endVisit(ContractDefinition const& _contract)
{
auto const& inheritedFunctions = m_overrideChecker.inheritedFunctions(_contract);
for (auto const* function: _contract.definedFunctions())
{
Contracts expectedContracts;
OverrideProxy proxy{function};
if (!function->isConstructor())
{
/// Build list of contracts expected to be mentioned in the override list (if any).
for (auto [begin, end] = inheritedFunctions.equal_range(proxy); begin != end; begin++)
expectedContracts.insert(&begin->contract());
/// Add override with contract list, if needed.
if (!function->overrides() && expectedContracts.size() > 1)
m_changes.emplace_back(
UpgradeChange::Level::Safe,
function->location(),
appendOverride(*function, expectedContracts)
);
for (auto [begin, end] = inheritedFunctions.equal_range(proxy); begin != end; begin++)
{
auto& super = (*begin);
auto functionType = FunctionType(*function).asExternallyCallableFunction(false);
auto superType = super.functionType()->asExternallyCallableFunction(false);
if (functionType && functionType->hasEqualParameterTypes(*superType))
{
/// If function does not specify override and no override with
/// contract list was added before.
if (!function->overrides() && expectedContracts.size() <= 1)
m_changes.emplace_back(
UpgradeChange::Level::Safe,
function->location(),
appendOverride(*function, expectedContracts)
);
}
}
}
}
}
string OverridingFunction::appendOverride(
FunctionDefinition const& _function,
Contracts const& _expectedContracts
)
{
auto location = _function.location();
string upgradedCode;
string overrideExpression = SourceGeneration::functionOverride(_expectedContracts);
if (SourceAnalysis{m_charStreamProvider}.hasVirtualKeyword(location))
upgradedCode = SourceTransform{m_charStreamProvider}.insertAfterKeyword(
location,
"virtual",
overrideExpression
);
else if (SourceAnalysis{m_charStreamProvider}.hasMutabilityKeyword(location))
upgradedCode = SourceTransform{m_charStreamProvider}.insertAfterKeyword(
location,
stateMutabilityToString(_function.stateMutability()),
overrideExpression
);
else if (SourceAnalysis{m_charStreamProvider}.hasVisibilityKeyword(location))
upgradedCode = SourceTransform{m_charStreamProvider}.insertAfterKeyword(
location,
Declaration::visibilityToString(_function.visibility()),
overrideExpression
);
else
upgradedCode = SourceTransform{m_charStreamProvider}.insertAfterRightParenthesis(
location,
overrideExpression
);
return upgradedCode;
}
void VirtualFunction::endVisit(ContractDefinition const& _contract)
{
auto const& inheritedFunctions = m_overrideChecker.inheritedFunctions(_contract);
for (FunctionDefinition const* function: _contract.definedFunctions())
{
OverrideProxy proxy{function};
if (!function->isConstructor())
{
if (
!function->markedVirtual() &&
!function->isImplemented() &&
!function->virtualSemantics() &&
function->visibility() > Visibility::Private
)
{
m_changes.emplace_back(
UpgradeChange::Level::Safe,
function->location(),
appendVirtual(*function)
);
}
for (auto [begin, end] = inheritedFunctions.equal_range(proxy); begin != end; begin++)
{
auto& super = (*begin);
if (
!function->markedVirtual() &&
!super.virtualSemantics()
)
{
m_changes.emplace_back(
UpgradeChange::Level::Safe,
function->location(),
appendVirtual(*function)
);
}
}
}
}
}
string VirtualFunction::appendVirtual(FunctionDefinition const& _function) const
{
auto location = _function.location();
string upgradedCode;
if (SourceAnalysis{m_charStreamProvider}.hasMutabilityKeyword(location))
upgradedCode = SourceTransform{m_charStreamProvider}.insertAfterKeyword(
location,
stateMutabilityToString(_function.stateMutability()),
"virtual"
);
else if (SourceAnalysis{m_charStreamProvider}.hasVisibilityKeyword(location))
upgradedCode = SourceTransform{m_charStreamProvider}.insertAfterKeyword(
location,
Declaration::visibilityToString(_function.visibility()),
"virtual"
);
else
upgradedCode = SourceTransform{m_charStreamProvider}.insertAfterRightParenthesis(
_function.location(),
"virtual"
);
return upgradedCode;
}