2015-09-16 14:56:30 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-09-16 14:56:30 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-09-16 14:56:30 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-09-16 14:56:30 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-09-16 14:56:30 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @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>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
2017-10-05 19:17:54 +00:00
|
|
|
#include <libsolidity/interface/ErrorReporter.h>
|
2015-09-16 14:56:30 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
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()));
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-10-04 09:59:21 +00:00
|
|
|
{
|
2017-11-22 11:54:23 +00:00
|
|
|
auto commonType = left->binaryOperatorResult(_operation.getOperator(), right);
|
|
|
|
if (!commonType)
|
|
|
|
m_errorReporter.fatalTypeError(
|
|
|
|
_operation.location(),
|
|
|
|
"Operator " +
|
|
|
|
string(Token::toString(_operation.getOperator())) +
|
|
|
|
" not compatible with types " +
|
|
|
|
left->toString() +
|
|
|
|
" and " +
|
|
|
|
right->toString()
|
|
|
|
);
|
|
|
|
setType(
|
|
|
|
_operation,
|
|
|
|
Token::isCompareOp(_operation.getOperator()) ?
|
|
|
|
make_shared<BoolType>() :
|
2017-12-12 09:45:40 +00:00
|
|
|
commonType
|
2017-10-04 09:59:21 +00:00
|
|
|
);
|
|
|
|
}
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConstantEvaluator::endVisit(Literal const& _literal)
|
|
|
|
{
|
2017-11-22 11:54:23 +00:00
|
|
|
setType(_literal, Type::forLiteral(_literal));
|
2015-09-16 14:56:30 +00:00
|
|
|
}
|
2017-10-28 11:03:11 +00:00
|
|
|
|
|
|
|
void ConstantEvaluator::endVisit(Identifier const& _identifier)
|
|
|
|
{
|
2017-11-17 16:55:07 +00:00
|
|
|
VariableDeclaration const* variableDeclaration = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration);
|
2017-10-28 11:03:11 +00:00
|
|
|
if (!variableDeclaration)
|
|
|
|
return;
|
|
|
|
if (!variableDeclaration->isConstant())
|
2017-11-22 12:41:33 +00:00
|
|
|
return;
|
2017-10-28 11:03:11 +00:00
|
|
|
|
2017-11-22 12:41:33 +00:00
|
|
|
ASTPointer<Expression> const& value = variableDeclaration->value();
|
2017-11-17 16:55:07 +00:00
|
|
|
if (!value)
|
2017-11-22 12:41:33 +00:00
|
|
|
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)
|
2017-11-22 01:05:26 +00:00
|
|
|
m_errorReporter.fatalTypeError(_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-10-28 11:03:11 +00:00
|
|
|
}
|
2017-11-17 16:55:07 +00:00
|
|
|
|
2017-11-22 11:54:23 +00:00
|
|
|
setType(_identifier, type(*value));
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:27:06 +00:00
|
|
|
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);
|
2017-10-28 11:03:11 +00:00
|
|
|
}
|