solidity/libsolidity/analysis/ConstantEvaluator.cpp

117 lines
3.3 KiB
C++
Raw Normal View History

/*
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
/**
* @author Christian <c@ethdev.com>
* @date 2015
* Evaluator for types of constant expressions.
*/
2015-10-20 22:21:52 +00:00
#include <libsolidity/analysis/ConstantEvaluator.h>
2018-12-17 11:30:08 +00:00
2015-10-20 22:21:52 +00:00
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/TypeProvider.h>
#include <liblangutil/ErrorReporter.h>
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::frontend;
using namespace solidity::langutil;
void ConstantEvaluator::endVisit(UnaryOperation const& _operation)
{
2017-11-22 11:54:23 +00:00
auto sub = type(_operation.subExpression());
if (sub)
setType(_operation, sub->unaryOperatorResult(_operation.getOperator()));
}
void ConstantEvaluator::endVisit(BinaryOperation const& _operation)
{
2017-11-22 11:54:23 +00:00
auto left = type(_operation.leftExpression());
auto right = type(_operation.rightExpression());
if (left && right)
{
TypePointer commonType = left->binaryOperatorResult(_operation.getOperator(), right);
2017-11-22 11:54:23 +00:00
if (!commonType)
m_errorReporter.fatalTypeError(
6020_error,
2017-11-22 11:54:23 +00:00
_operation.location(),
"Operator " +
string(TokenTraits::toString(_operation.getOperator())) +
2017-11-22 11:54:23 +00:00
" not compatible with types " +
left->toString() +
" and " +
right->toString()
);
setType(
_operation,
TokenTraits::isCompareOp(_operation.getOperator()) ?
TypeProvider::boolean() :
2017-12-12 09:45:40 +00:00
commonType
);
}
}
void ConstantEvaluator::endVisit(Literal const& _literal)
{
setType(_literal, TypeProvider::forLiteral(_literal));
}
void ConstantEvaluator::endVisit(Identifier const& _identifier)
{
2017-11-17 16:55:07 +00:00
VariableDeclaration const* variableDeclaration = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration);
if (!variableDeclaration)
return;
if (!variableDeclaration->isConstant())
return;
ASTPointer<Expression> const& value = variableDeclaration->value();
2017-11-17 16:55:07 +00:00
if (!value)
return;
2017-11-22 11:54:23 +00:00
else if (!m_types->count(value.get()))
2017-11-17 16:55:07 +00:00
{
if (m_depth > 32)
m_errorReporter.fatalTypeError(5210_error, _identifier.location(), "Cyclic constant definition (or maximum recursion depth exhausted).");
2017-11-22 11:54:23 +00:00
ConstantEvaluator(m_errorReporter, m_depth + 1, m_types).evaluate(*value);
}
2017-11-17 16:55:07 +00:00
2017-11-22 11:54:23 +00:00
setType(_identifier, type(*value));
}
void ConstantEvaluator::endVisit(TupleExpression const& _tuple)
{
if (!_tuple.isInlineArray() && _tuple.components().size() == 1)
setType(_tuple, type(*_tuple.components().front()));
}
2017-11-22 11:54:23 +00:00
void ConstantEvaluator::setType(ASTNode const& _node, TypePointer const& _type)
{
if (_type && _type->category() == Type::Category::RationalNumber)
(*m_types)[&_node] = _type;
}
TypePointer ConstantEvaluator::type(ASTNode const& _node)
{
return (*m_types)[&_node];
}
TypePointer ConstantEvaluator::evaluate(Expression const& _expr)
{
_expr.accept(*this);
return type(_expr);
}