Use the proper error reporting interface in ConstantEvaluator

This commit is contained in:
Alex Beregszaszi 2017-10-05 20:17:54 +01:00
parent c28ed2a619
commit ed62b2583c
3 changed files with 13 additions and 7 deletions

View File

@ -22,17 +22,17 @@
#include <libsolidity/analysis/ConstantEvaluator.h> #include <libsolidity/analysis/ConstantEvaluator.h>
#include <libsolidity/ast/AST.h> #include <libsolidity/ast/AST.h>
#include <libsolidity/interface/ErrorReporter.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::solidity; using namespace dev::solidity;
void ConstantEvaluator::endVisit(UnaryOperation const& _operation) void ConstantEvaluator::endVisit(UnaryOperation const& _operation)
{ {
TypePointer const& subType = _operation.subExpression().annotation().type; TypePointer const& subType = _operation.subExpression().annotation().type;
if (!dynamic_cast<RationalNumberType const*>(subType.get())) if (!dynamic_cast<RationalNumberType const*>(subType.get()))
BOOST_THROW_EXCEPTION(_operation.subExpression().createTypeError("Invalid constant expression.")); m_errorReporter.fatalTypeError(_operation.subExpression().location(), "Invalid constant expression.");
TypePointer t = subType->unaryOperatorResult(_operation.getOperator()); TypePointer t = subType->unaryOperatorResult(_operation.getOperator());
_operation.annotation().type = t; _operation.annotation().type = t;
} }
@ -42,9 +42,9 @@ void ConstantEvaluator::endVisit(BinaryOperation const& _operation)
TypePointer const& leftType = _operation.leftExpression().annotation().type; TypePointer const& leftType = _operation.leftExpression().annotation().type;
TypePointer const& rightType = _operation.rightExpression().annotation().type; TypePointer const& rightType = _operation.rightExpression().annotation().type;
if (!dynamic_cast<RationalNumberType const*>(leftType.get())) if (!dynamic_cast<RationalNumberType const*>(leftType.get()))
BOOST_THROW_EXCEPTION(_operation.leftExpression().createTypeError("Invalid constant expression.")); m_errorReporter.fatalTypeError(_operation.leftExpression().location(), "Invalid constant expression.");
if (!dynamic_cast<RationalNumberType const*>(rightType.get())) if (!dynamic_cast<RationalNumberType const*>(rightType.get()))
BOOST_THROW_EXCEPTION(_operation.rightExpression().createTypeError("Invalid constant expression.")); m_errorReporter.fatalTypeError(_operation.rightExpression().location(), "Invalid constant expression.");
TypePointer commonType = leftType->binaryOperatorResult(_operation.getOperator(), rightType); TypePointer commonType = leftType->binaryOperatorResult(_operation.getOperator(), rightType);
if (Token::isCompareOp(_operation.getOperator())) if (Token::isCompareOp(_operation.getOperator()))
commonType = make_shared<BoolType>(); commonType = make_shared<BoolType>();
@ -55,5 +55,5 @@ void ConstantEvaluator::endVisit(Literal const& _literal)
{ {
_literal.annotation().type = Type::forLiteral(_literal); _literal.annotation().type = Type::forLiteral(_literal);
if (!_literal.annotation().type) if (!_literal.annotation().type)
BOOST_THROW_EXCEPTION(_literal.createTypeError("Invalid literal value.")); m_errorReporter.fatalTypeError(_literal.location(), "Invalid literal value.");
} }

View File

@ -29,6 +29,7 @@ namespace dev
namespace solidity namespace solidity
{ {
class ErrorReporter;
class TypeChecker; class TypeChecker;
/** /**
@ -37,13 +38,18 @@ class TypeChecker;
class ConstantEvaluator: private ASTConstVisitor class ConstantEvaluator: private ASTConstVisitor
{ {
public: public:
ConstantEvaluator(Expression const& _expr) { _expr.accept(*this); } ConstantEvaluator(Expression const& _expr, ErrorReporter& _errorReporter):
m_errorReporter(_errorReporter)
{
_expr.accept(*this);
}
private: private:
virtual void endVisit(BinaryOperation const& _operation); virtual void endVisit(BinaryOperation const& _operation);
virtual void endVisit(UnaryOperation const& _operation); virtual void endVisit(UnaryOperation const& _operation);
virtual void endVisit(Literal const& _literal); virtual void endVisit(Literal const& _literal);
ErrorReporter& m_errorReporter;
}; };
} }

View File

@ -147,7 +147,7 @@ void ReferencesResolver::endVisit(ArrayTypeName const& _typeName)
if (Expression const* length = _typeName.length()) if (Expression const* length = _typeName.length())
{ {
if (!length->annotation().type) if (!length->annotation().type)
ConstantEvaluator e(*length); ConstantEvaluator e(*length, m_errorReporter);
auto const* lengthType = dynamic_cast<RationalNumberType const*>(length->annotation().type.get()); auto const* lengthType = dynamic_cast<RationalNumberType const*>(length->annotation().type.get());
if (!lengthType || !lengthType->mobileType()) if (!lengthType || !lengthType->mobileType())
fatalTypeError(length->location(), "Invalid array length, expected integer literal."); fatalTypeError(length->location(), "Invalid array length, expected integer literal.");