diff options
author | chriseth <c@ethdev.com> | 2015-06-05 17:15:09 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-06-05 17:15:09 +0800 |
commit | 4987eec3d1e87868e091850d31af58e054ab5ee5 (patch) | |
tree | 719dce1d8670f07b8fc0adc752f4304664646031 /Types.cpp | |
parent | 3d0807a9fa2cf26fc750b3eed35036602946ead4 (diff) | |
parent | a8505e598f6a72f3c823068f4ea3320810906835 (diff) | |
download | dexon-solidity-4987eec3d1e87868e091850d31af58e054ab5ee5.tar.gz dexon-solidity-4987eec3d1e87868e091850d31af58e054ab5ee5.tar.zst dexon-solidity-4987eec3d1e87868e091850d31af58e054ab5ee5.zip |
Merge pull request #2074 from LianaHus/sol_PosIntegerLiteralsConversation
Solidity Positive integer literals conversion to signed if in value range.
Diffstat (limited to 'Types.cpp')
-rw-r--r-- | Types.cpp | 33 |
1 files changed, 22 insertions, 11 deletions
@@ -361,17 +361,27 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal) bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const { - shared_ptr<IntegerType const> integerType = getIntegerType(); - if (!integerType) + if (auto targetType = dynamic_cast<IntegerType const*>(&_convertTo)) + { + if (m_value == 0) + return true; + int forSignBit = (targetType->isSigned() ? 1 : 0); + if (m_value > 0) + { + if (m_value <= (u256(-1) >> (256 - targetType->getNumBits() + forSignBit))) + return true; + } + else if (targetType->isSigned() && -m_value <= (u256(1) << (targetType->getNumBits() - forSignBit))) + return true; return false; - - if (_convertTo.getCategory() == Category::FixedBytes) + } + else if (_convertTo.getCategory() == Category::FixedBytes) { - FixedBytesType const& convertTo = dynamic_cast<FixedBytesType const&>(_convertTo); - return convertTo.getNumBytes() * 8 >= integerType->getNumBits(); + FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo); + return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits(); } - - return integerType->isImplicitlyConvertibleTo(_convertTo); + else + return false; } bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const @@ -514,9 +524,10 @@ shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const if (value > u256(-1)) return shared_ptr<IntegerType const>(); else - return make_shared<IntegerType>(max(bytesRequired(value), 1u) * 8, - negative ? IntegerType::Modifier::Signed - : IntegerType::Modifier::Unsigned); + return make_shared<IntegerType>( + max(bytesRequired(value), 1u) * 8, + negative ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned + ); } shared_ptr<FixedBytesType> FixedBytesType::smallestTypeForLiteral(string const& _literal) |