From 382ac85aa5e8bbcfa48e274d7aec0923b79666d4 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Wed, 3 Jun 2015 16:14:23 +0200 Subject: - conversion of positive literals to signed int - tests --- Types.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'Types.cpp') diff --git a/Types.cpp b/Types.cpp index 0e9ea987..c4238f77 100644 --- a/Types.cpp +++ b/Types.cpp @@ -361,17 +361,28 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal) bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const { - shared_ptr integerType = getIntegerType(); - if (!integerType) - return false; - - if (_convertTo.getCategory() == Category::FixedBytes) + if (IntegerType const* integerType = dynamic_cast(&_convertTo)) { - FixedBytesType const& convertTo = dynamic_cast(_convertTo); - return convertTo.getNumBytes() * 8 >= integerType->getNumBits(); + if (m_value == 0) + return true; + int forSignBit = (integerType->isSigned() ? 1 : 0); + if (m_value > 0) + { + if (m_value <= (u256(-1) >> (256 - integerType->getNumBits() + forSignBit))) + return true; + } + else if (-m_value <= (u256(1) << (integerType->getNumBits() - forSignBit))) + return true; + return false; } - - return integerType->isImplicitlyConvertibleTo(_convertTo); + else + if (_convertTo.getCategory() == Category::FixedBytes) + { + FixedBytesType const& fixedBytes = dynamic_cast(_convertTo); + return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits(); + } + else + return false; } bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const @@ -514,9 +525,10 @@ shared_ptr IntegerConstantType::getIntegerType() const if (value > u256(-1)) return shared_ptr(); else - return make_shared(max(bytesRequired(value), 1u) * 8, - negative ? IntegerType::Modifier::Signed - : IntegerType::Modifier::Unsigned); + return make_shared( + max(bytesRequired(value), 1u) * 8, + negative ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned + ); } shared_ptr FixedBytesType::smallestTypeForLiteral(string const& _literal) -- cgit From 89aab7e234e3c96bbae2e4066cfa1249f5d1793e Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 4 Jun 2015 12:42:55 +0200 Subject: - style fixes - added test for uint8 = -1 which doesn't fail; todo: fix that --- Types.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'Types.cpp') diff --git a/Types.cpp b/Types.cpp index c4238f77..c10e29ea 100644 --- a/Types.cpp +++ b/Types.cpp @@ -361,22 +361,21 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal) bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const { - if (IntegerType const* integerType = dynamic_cast(&_convertTo)) + if (auto targetType = dynamic_cast(&_convertTo)) { if (m_value == 0) return true; - int forSignBit = (integerType->isSigned() ? 1 : 0); + int forSignBit = (targetType->isSigned() ? 1 : 0); if (m_value > 0) { - if (m_value <= (u256(-1) >> (256 - integerType->getNumBits() + forSignBit))) + if (m_value <= (u256(-1) >> (256 - targetType->getNumBits() + forSignBit))) return true; } - else if (-m_value <= (u256(1) << (integerType->getNumBits() - forSignBit))) + else if (-m_value <= (u256(1) << (targetType->getNumBits() - forSignBit))) return true; return false; } - else - if (_convertTo.getCategory() == Category::FixedBytes) + else if (_convertTo.getCategory() == Category::FixedBytes) { FixedBytesType const& fixedBytes = dynamic_cast(_convertTo); return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits(); -- cgit From 4e15a391076bfe3421cee0ef6a4bc847ddc0f46f Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 4 Jun 2015 14:09:19 +0200 Subject: fixed assigning negative number to unsigned --- Types.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Types.cpp') diff --git a/Types.cpp b/Types.cpp index c10e29ea..bf79be31 100644 --- a/Types.cpp +++ b/Types.cpp @@ -371,7 +371,7 @@ bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) cons if (m_value <= (u256(-1) >> (256 - targetType->getNumBits() + forSignBit))) return true; } - else if (-m_value <= (u256(1) << (targetType->getNumBits() - forSignBit))) + else if (targetType->isSigned() && -m_value <= (u256(1) << (targetType->getNumBits() - forSignBit))) return true; return false; } -- cgit From a8505e598f6a72f3c823068f4ea3320810906835 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 4 Jun 2015 18:06:06 +0200 Subject: Update Types.cpp --- Types.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'Types.cpp') diff --git a/Types.cpp b/Types.cpp index bf79be31..33eafb15 100644 --- a/Types.cpp +++ b/Types.cpp @@ -376,12 +376,12 @@ bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) cons return false; } else if (_convertTo.getCategory() == Category::FixedBytes) - { - FixedBytesType const& fixedBytes = dynamic_cast(_convertTo); - return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits(); - } - else - return false; + { + FixedBytesType const& fixedBytes = dynamic_cast(_convertTo); + return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits(); + } + else + return false; } bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const -- cgit