From 57003c534ad02f4a3dc596130257333965e3396a Mon Sep 17 00:00:00 2001 From: Jason Cobb Date: Thu, 19 Apr 2018 19:00:05 -0400 Subject: Types changes for fixed points --- libsolidity/ast/Types.cpp | 27 ++++++++++++++------------- libsolidity/ast/Types.h | 3 +++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 425e5045..0a25268b 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -627,8 +627,7 @@ bool FixedPointType::isImplicitlyConvertibleTo(Type const& _convertTo) const bool FixedPointType::isExplicitlyConvertibleTo(Type const& _convertTo) const { return _convertTo.category() == category() || - _convertTo.category() == Category::Integer || - _convertTo.category() == Category::FixedBytes; + (_convertTo.category() == Category::Integer && !dynamic_cast(_convertTo).isAddress()); } TypePointer FixedPointType::unaryOperatorResult(Token::Value _operator) const @@ -688,7 +687,12 @@ TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePoi _other->category() != Category::Integer ) return TypePointer(); - auto commonType = Type::commonType(shared_from_this(), _other); //might be fixed point or integer + + if (auto integerType = dynamic_pointer_cast(_other)) + if (integerType->isAddress()) + return TypePointer(); + + auto commonType = Type::commonType(shared_from_this(), _other); if (!commonType) return TypePointer(); @@ -696,19 +700,16 @@ TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePoi // All fixed types can be compared if (Token::isCompareOp(_operator)) return commonType; - if (Token::isBitOp(_operator) || Token::isBooleanOp(_operator)) + if (Token::isBitOp(_operator) || Token::isBooleanOp(_operator) || _operator == Token::Exp) return TypePointer(); - if (auto fixType = dynamic_pointer_cast(commonType)) - { - if (Token::Exp == _operator) - return TypePointer(); - } - else if (auto intType = dynamic_pointer_cast(commonType)) - if (intType->isAddress()) - return TypePointer(); return commonType; } +std::shared_ptr FixedPointType::asIntegerType() const +{ + return std::make_shared(numBits(), isSigned() ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned); +} + tuple RationalNumberType::parseRational(string const& _value) { rational value; @@ -1148,7 +1149,7 @@ u256 RationalNumberType::literalValue(Literal const*) const auto fixed = fixedPointType(); solAssert(fixed, ""); int fractionalDigits = fixed->fractionalDigits(); - shiftedValue = (m_value.numerator() / m_value.denominator()) * pow(bigint(10), fractionalDigits); + shiftedValue = m_value.numerator() * pow(bigint(10), fractionalDigits) / m_value.denominator(); } // we ignore the literal and hope that the type was correctly determined diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 345f84a1..b41b2235 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -396,6 +396,9 @@ public: /// smallest value in general. bigint minIntegerValue() const; + /// @returns the smallest integer type that can hold this type with fractional parts shifted to integers. + std::shared_ptr asIntegerType() const; + private: int m_totalBits; int m_fractionalDigits; -- cgit From 5423974e87e2128b45711269bb1f4f45b2f3a402 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 20 Apr 2018 17:54:34 +0100 Subject: Remove category check in FixedPointType:binaryOperatorResult as commonType handles the same --- libsolidity/ast/Types.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 0a25268b..60b4d726 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -681,17 +681,6 @@ bigint FixedPointType::minIntegerValue() const TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const { - if ( - _other->category() != Category::RationalNumber && - _other->category() != category() && - _other->category() != Category::Integer - ) - return TypePointer(); - - if (auto integerType = dynamic_pointer_cast(_other)) - if (integerType->isAddress()) - return TypePointer(); - auto commonType = Type::commonType(shared_from_this(), _other); if (!commonType) -- cgit