Merge pull request #4342 from rnaby/l/a/Types

RationalNumberType::isImplicitlyConvertibleTo Refactor
This commit is contained in:
chriseth 2018-06-26 17:54:09 +02:00 committed by GitHub
commit 503eb8caa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -856,7 +856,9 @@ tuple<bool, rational> RationalNumberType::isValidLiteral(Literal const& _literal
bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
{
if (_convertTo.category() == Category::Integer)
switch (_convertTo.category())
{
case Category::Integer:
{
if (isFractional())
return false;
@ -870,31 +872,33 @@ bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
{
if (m_value.numerator() <= (u256(-1) >> (256 - targetType.numBits() + forSignBit)))
return true;
return false;
}
if (targetType.isSigned())
{
if (-m_value.numerator() <= (u256(1) << (targetType.numBits() - forSignBit)))
return true;
}
else if (targetType.isSigned() && -m_value.numerator() <= (u256(1) << (targetType.numBits() - forSignBit)))
return true;
return false;
}
else if (_convertTo.category() == Category::FixedPoint)
case Category::FixedPoint:
{
if (auto fixed = fixedPointType())
return fixed->isImplicitlyConvertibleTo(_convertTo);
else
return false;
return false;
}
else if (_convertTo.category() == Category::FixedBytes)
case Category::FixedBytes:
{
FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo);
if (!isFractional())
{
if (integerType())
return fixedBytes.numBytes() * 8 >= integerType()->numBits();
return false;
}
else
if (isFractional())
return false;
if (integerType())
return fixedBytes.numBytes() * 8 >= integerType()->numBits();
return false;
}
default:
return false;
}
return false;
}
bool RationalNumberType::isExplicitlyConvertibleTo(Type const& _convertTo) const