[analysis] ConstantEvaluator: Little cleanup & avoid requiring the use of shared_ptr<>.

This commit is contained in:
Christian Parpart 2020-11-02 13:18:44 +01:00
parent f1846b57a2
commit d99ef14db8
6 changed files with 48 additions and 18 deletions

View File

@ -25,8 +25,11 @@
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/TypeProvider.h>
#include <liblangutil/ErrorReporter.h>
#include <libsolutil/Common.h>
using namespace std;
using namespace solidity;
using namespace solidity::frontend;
@ -71,6 +74,11 @@ void ConstantEvaluator::endVisit(Literal const& _literal)
setType(_literal, TypeProvider::forLiteral(_literal));
}
bool ConstantEvaluator::evaluated(ASTNode const& _node) const noexcept
{
return m_evaluations.count(&_node) != 0;
}
void ConstantEvaluator::endVisit(Identifier const& _identifier)
{
VariableDeclaration const* variableDeclaration = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration);
@ -82,11 +90,12 @@ void ConstantEvaluator::endVisit(Identifier const& _identifier)
ASTPointer<Expression> const& value = variableDeclaration->value();
if (!value)
return;
else if (!m_types->count(value.get()))
else if (!evaluated(*value))
{
if (m_depth > 32)
m_errorReporter.fatalTypeError(5210_error, _identifier.location(), "Cyclic constant definition (or maximum recursion depth exhausted).");
ConstantEvaluator(m_errorReporter, m_depth + 1, m_types).evaluate(*value);
evaluate(*value);
}
setType(_identifier, type(*value));
@ -101,16 +110,29 @@ void ConstantEvaluator::endVisit(TupleExpression const& _tuple)
void ConstantEvaluator::setType(ASTNode const& _node, TypePointer const& _type)
{
if (_type && _type->category() == Type::Category::RationalNumber)
(*m_types)[&_node] = _type;
m_evaluations[&_node] = _type;
}
TypePointer ConstantEvaluator::type(ASTNode const& _node)
{
return (*m_types)[&_node];
if (auto p = m_evaluations.find(&_node); p != m_evaluations.end())
return p->second;
return nullptr;
}
TypePointer ConstantEvaluator::evaluate(langutil::ErrorReporter& _errorReporter, Expression const& _expr)
{
EvaluationMap evaluations;
ConstantEvaluator evaluator(_errorReporter, evaluations);
return evaluator.evaluate(_expr);
}
TypePointer ConstantEvaluator::evaluate(Expression const& _expr)
{
m_depth++;
ScopeGuard _([&]() { m_depth--; });
_expr.accept(*this);
return type(_expr);
}

View File

@ -43,17 +43,20 @@ class TypeChecker;
class ConstantEvaluator: private ASTConstVisitor
{
public:
ConstantEvaluator(
langutil::ErrorReporter& _errorReporter,
size_t _newDepth = 0,
std::shared_ptr<std::map<ASTNode const*, TypePointer>> _types = std::make_shared<std::map<ASTNode const*, TypePointer>>()
):
m_errorReporter(_errorReporter),
m_depth(_newDepth),
m_types(std::move(_types))
using EvaluationMap = std::map<ASTNode const*, TypePointer>;
ConstantEvaluator(langutil::ErrorReporter& _errorReporter, EvaluationMap& _evaluations):
m_errorReporter{ _errorReporter },
m_evaluations{ _evaluations },
m_depth{ 0 }
{
}
static TypePointer evaluate(
langutil::ErrorReporter& _errorReporter,
Expression const& _expr
);
TypePointer evaluate(Expression const& _expr);
private:
@ -63,13 +66,16 @@ private:
void endVisit(Identifier const& _identifier) override;
void endVisit(TupleExpression const& _tuple) override;
void setType(ASTNode const& _node, TypePointer const& _type);
void setType(ASTNode const& _node, TypePointer const& _value);
TypePointer type(ASTNode const& _node);
bool evaluated(ASTNode const& _node) const noexcept;
langutil::ErrorReporter& m_errorReporter;
EvaluationMap& m_evaluations;
/// Current recursion depth.
size_t m_depth = 0;
std::shared_ptr<std::map<ASTNode const*, TypePointer>> m_types;
};
}

View File

@ -249,7 +249,7 @@ void DeclarationTypeChecker::endVisit(ArrayTypeName const& _typeName)
{
TypePointer& lengthTypeGeneric = length->annotation().type;
if (!lengthTypeGeneric)
lengthTypeGeneric = ConstantEvaluator(m_errorReporter).evaluate(*length);
lengthTypeGeneric = ConstantEvaluator::evaluate(m_errorReporter, *length);
RationalNumberType const* lengthType = dynamic_cast<RationalNumberType const*>(lengthTypeGeneric);
u256 lengthValue = 0;
if (!lengthType || !lengthType->mobileType())

View File

@ -291,7 +291,7 @@ bool StaticAnalyzer::visit(BinaryOperation const& _operation)
(_operation.getOperator() == Token::Div || _operation.getOperator() == Token::Mod)
)
if (auto rhs = dynamic_cast<RationalNumberType const*>(
ConstantEvaluator(m_errorReporter).evaluate(_operation.rightExpression())
ConstantEvaluator::evaluate(m_errorReporter, _operation.rightExpression())
))
if (rhs->isZero())
m_errorReporter.typeError(
@ -314,7 +314,7 @@ bool StaticAnalyzer::visit(FunctionCall const& _functionCall)
solAssert(_functionCall.arguments().size() == 3, "");
if (*_functionCall.arguments()[2]->annotation().isPure)
if (auto lastArg = dynamic_cast<RationalNumberType const*>(
ConstantEvaluator(m_errorReporter).evaluate(*(_functionCall.arguments())[2])
ConstantEvaluator::evaluate(m_errorReporter, *(_functionCall.arguments())[2])
))
if (lastArg->isZero())
m_errorReporter.typeError(

View File

@ -347,6 +347,8 @@ TypePointer TypeProvider::forLiteral(Literal const& _literal)
case Token::FalseLiteral:
return boolean();
case Token::Number:
// TODO: return IntegerType, smallest fitting type in number of bits.
//return integer(8, IntegerType::Modifier::Unsigned); // XXX
return rationalNumber(_literal);
case Token::StringLiteral:
case Token::UnicodeStringLiteral:

View File

@ -7,4 +7,4 @@ contract C {
}
}
// ----
// TypeError 5210: (36-39): Cyclic constant definition (or maximum recursion depth exhausted).
// TypeError 5210: (69-71): Cyclic constant definition (or maximum recursion depth exhausted).