solidity/libsolidity/analysis/ImmutableValidator.cpp

69 lines
2.3 KiB
C++
Raw Normal View History

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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2020-03-10 17:15:50 +00:00
#include <libsolidity/analysis/ImmutableValidator.h>
#include <range/v3/view/reverse.hpp>
2020-03-10 17:15:50 +00:00
using namespace solidity::frontend;
using namespace solidity::langutil;
2020-03-10 17:15:50 +00:00
void ImmutableValidator::analyze()
{
auto linearizedContracts = m_mostDerivedContract.annotation().linearizedBaseContracts | ranges::views::reverse;
2020-03-10 17:15:50 +00:00
for (ContractDefinition const* contract: linearizedContracts)
{
for (FunctionDefinition const* function: contract->definedFunctions())
function->accept(*this);
2020-03-10 17:15:50 +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)
{
return !_functionDefinition.isConstructor();
}
void ImmutableValidator::endVisit(MemberAccess const& _memberAccess)
{
analyseVariableReference(_memberAccess.annotation().referencedDeclaration, _memberAccess);
}
2020-03-10 17:15:50 +00:00
void ImmutableValidator::endVisit(Identifier const& _identifier)
{
analyseVariableReference(_identifier.annotation().referencedDeclaration, _identifier);
2020-03-10 17:15:50 +00:00
}
void ImmutableValidator::analyseVariableReference(Declaration const* _reference, Expression const& _expression)
2020-03-10 17:15:50 +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;
// If this is not an ordinary assignment, we write and read at the same time.
if (_expression.annotation().willBeWrittenTo)
2020-03-10 17:15:50 +00:00
m_errorReporter.typeError(
1581_error,
2020-03-10 17:15:50 +00:00
_expression.location(),
"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
);
}