2020-03-10 17:15:50 +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
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
#include <libsolidity/analysis/ImmutableValidator.h>
|
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/reverse.hpp>
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
using namespace solidity::frontend;
|
2020-04-18 01:00:22 +00:00
|
|
|
using namespace solidity::langutil;
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
void ImmutableValidator::analyze()
|
|
|
|
{
|
2022-01-12 17:58:17 +00:00
|
|
|
auto linearizedContracts = m_mostDerivedContract.annotation().linearizedBaseContracts | ranges::views::reverse;
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
for (ContractDefinition const* contract: linearizedContracts)
|
|
|
|
{
|
2023-05-30 14:07:00 +00:00
|
|
|
for (FunctionDefinition const* function: contract->definedFunctions())
|
|
|
|
function->accept(*this);
|
2020-03-10 17:15:50 +00:00
|
|
|
|
2023-05-30 14:07:00 +00:00
|
|
|
for (ModifierDefinition const* modifier: contract->functionModifiers())
|
|
|
|
modifier->accept(*this);
|
2020-03-10 17:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImmutableValidator::visit(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
2023-05-30 14:07:00 +00:00
|
|
|
return !_functionDefinition.isConstructor();
|
2023-05-09 10:12:12 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 14:07:00 +00:00
|
|
|
void ImmutableValidator::endVisit(MemberAccess const& _memberAccess)
|
2020-08-11 09:18:22 +00:00
|
|
|
{
|
2023-05-30 14:07:00 +00:00
|
|
|
analyseVariableReference(_memberAccess.annotation().referencedDeclaration, _memberAccess);
|
2020-08-11 09:18:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:15:50 +00:00
|
|
|
void ImmutableValidator::endVisit(Identifier const& _identifier)
|
|
|
|
{
|
2023-05-30 14:07:00 +00:00
|
|
|
analyseVariableReference(_identifier.annotation().referencedDeclaration, _identifier);
|
2020-03-10 17:15:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 14:07:00 +00:00
|
|
|
void ImmutableValidator::analyseVariableReference(Declaration const* _reference, Expression const& _expression)
|
2020-03-10 17:15:50 +00:00
|
|
|
{
|
2023-05-30 14:07:00 +00:00
|
|
|
auto const* variable = dynamic_cast<VariableDeclaration const*>(_reference);
|
|
|
|
if (!variable || !variable->isStateVariable() || !variable->immutable())
|
2020-03-10 17:15:50 +00:00
|
|
|
return;
|
|
|
|
|
2020-08-04 11:18:41 +00:00
|
|
|
// If this is not an ordinary assignment, we write and read at the same time.
|
2023-05-30 14:07:00 +00:00
|
|
|
if (_expression.annotation().willBeWrittenTo)
|
2020-03-10 17:15:50 +00:00
|
|
|
m_errorReporter.typeError(
|
2023-05-30 14:07:00 +00:00
|
|
|
1581_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_expression.location(),
|
2023-05-30 14:07:00 +00:00
|
|
|
"Cannot write to immutable here: Immutable variables can only be initialized inline or assigned directly in the constructor."
|
2020-03-10 17:15:50 +00:00
|
|
|
);
|
|
|
|
}
|