#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())
{
for (auto [begin, end] = inheritedFunctions.equal_range(proxy); begin != end; begin++)
expectedContracts.insert(&begin->contract());
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->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;
}