aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/ast
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2016-05-10 20:30:24 +0800
committerchriseth <c@ethdev.com>2016-05-11 01:40:37 +0800
commit656405240e08e47fce40a2f62af93abc758bd2d2 (patch)
treead1139d8209d5f98b41dee7eb82ebbd2d7d78495 /libsolidity/ast
parentcf226f0607386d1e6d9c75ebc7ce24e733ca4315 (diff)
downloaddexon-solidity-656405240e08e47fce40a2f62af93abc758bd2d2.tar
dexon-solidity-656405240e08e47fce40a2f62af93abc758bd2d2.tar.gz
dexon-solidity-656405240e08e47fce40a2f62af93abc758bd2d2.tar.bz2
dexon-solidity-656405240e08e47fce40a2f62af93abc758bd2d2.tar.lz
dexon-solidity-656405240e08e47fce40a2f62af93abc758bd2d2.tar.xz
dexon-solidity-656405240e08e47fce40a2f62af93abc758bd2d2.tar.zst
dexon-solidity-656405240e08e47fce40a2f62af93abc758bd2d2.zip
Simplify interface of RationalNumber.
Diffstat (limited to 'libsolidity/ast')
-rw-r--r--libsolidity/ast/Types.cpp24
-rw-r--r--libsolidity/ast/Types.h9
2 files changed, 17 insertions, 16 deletions
diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp
index 31248fb6..7995e7e3 100644
--- a/libsolidity/ast/Types.cpp
+++ b/libsolidity/ast/Types.cpp
@@ -534,15 +534,15 @@ bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
auto targetType = dynamic_cast<IntegerType const*>(&_convertTo);
if (m_value == 0)
return true;
- if (m_value.denominator() != 1)
+ if (isFractional())
return false;
int forSignBit = (targetType->isSigned() ? 1 : 0);
if (m_value > 0)
{
- if (integerPart() <= (u256(-1) >> (256 - targetType->numBits() + forSignBit)))
+ if (m_value.numerator() <= (u256(-1) >> (256 - targetType->numBits() + forSignBit)))
return true;
}
- else if (targetType->isSigned() && -integerPart() <= (u256(1) << (targetType->numBits() - forSignBit)))
+ else if (targetType->isSigned() && -m_value.numerator() <= (u256(1) << (targetType->numBits() - forSignBit)))
return true;
return false;
}
@@ -560,7 +560,7 @@ bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
else if (_convertTo.category() == Category::FixedBytes)
{
FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo);
- if (m_value.denominator() == 1)
+ if (!isFractional())
return fixedBytes.numBytes() * 8 >= integerType()->numBits();
else
return false;
@@ -580,7 +580,7 @@ TypePointer RationalNumberType::unaryOperatorResult(Token::Value _operator) cons
switch (_operator)
{
case Token::BitNot:
- if(m_value.denominator() != 1)
+ if (isFractional())
return TypePointer();
value = ~m_value.numerator();
break;
@@ -625,7 +625,7 @@ TypePointer RationalNumberType::binaryOperatorResult(Token::Value _operator, Typ
else
{
rational value;
- bool fractional = (m_value.denominator() != 1 || other.m_value.denominator() != 1);
+ bool fractional = isFractional() || other.isFractional();
switch (_operator)
{
//bit operations will only be enabled for integers and fixed types that resemble integers
@@ -673,7 +673,7 @@ TypePointer RationalNumberType::binaryOperatorResult(Token::Value _operator, Typ
case Token::Exp:
{
using boost::multiprecision::pow;
- if (other.m_value.denominator() != 1)
+ if (other.isFractional())
return TypePointer();
else if (abs(other.m_value) > numeric_limits<uint32_t>::max())
return TypePointer(); // This will need too much memory to represent.
@@ -704,7 +704,7 @@ bool RationalNumberType::operator==(Type const& _other) const
string RationalNumberType::toString(bool) const
{
- if (m_value.denominator() == 1)
+ if (!isFractional())
return "int_const " + m_value.numerator().str();
return "rational_const " + m_value.numerator().str() + '/' + m_value.denominator().str();
}
@@ -717,7 +717,7 @@ u256 RationalNumberType::literalValue(Literal const*) const
u256 value;
bigint shiftedValue;
- if (m_value.denominator() == 1)
+ if (!isFractional())
shiftedValue = m_value.numerator();
else
{
@@ -741,7 +741,7 @@ u256 RationalNumberType::literalValue(Literal const*) const
TypePointer RationalNumberType::mobileType() const
{
- if (m_value.denominator() == 1)
+ if (!isFractional())
return integerType();
else
return fixedPointType();
@@ -749,8 +749,8 @@ TypePointer RationalNumberType::mobileType() const
shared_ptr<IntegerType const> RationalNumberType::integerType() const
{
- solAssert(m_value.denominator() == 1, "integerType() called for fractional number.");
- bigint value = integerPart();
+ solAssert(!isFractional(), "integerType() called for fractional number.");
+ bigint value = m_value.numerator();
bool negative = (value < 0);
if (negative) // convert to positive number of same bit requirements
value = ((0 - value) - 1) << 1;
diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h
index 441ab2ae..26c0f902 100644
--- a/libsolidity/ast/Types.h
+++ b/libsolidity/ast/Types.h
@@ -204,8 +204,9 @@ public:
virtual bool isValueType() const { return false; }
virtual unsigned sizeOnStack() const { return 1; }
/// @returns the mobile (in contrast to static) type corresponding to the given type.
- /// This returns the corresponding integer type for ConstantTypes and the pointer type
- /// for storage reference types. Might return a null pointer if there is no fitting type.
+ /// This returns the corresponding IntegerType or FixedPointType for RationalNumberType
+ /// and the pointer type for storage reference types.
+ /// Might return a null pointer if there is no fitting type.
virtual TypePointer mobileType() const { return shared_from_this(); }
/// @returns true if this is a non-value type and the data of this type is stored at the
/// given location.
@@ -388,8 +389,8 @@ public:
/// If the integer part does not fit, returns an empty pointer.
std::shared_ptr<FixedPointType const> fixedPointType() const;
- bigint denominator() const { return m_value.denominator(); }
- bigint integerPart() const { return m_value.numerator() / m_value.denominator(); }
+ /// @returns true iff the value is not an integer.
+ bool isFractional() const { return m_value.denominator() != 1; }
private:
rational m_value;