mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #3958 from meowingtwurtle/fixedPointTypes
Types class changes for fixed points
This commit is contained in:
commit
9e61b25dc4
@ -627,8 +627,7 @@ bool FixedPointType::isImplicitlyConvertibleTo(Type const& _convertTo) const
|
|||||||
bool FixedPointType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
bool FixedPointType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
{
|
{
|
||||||
return _convertTo.category() == category() ||
|
return _convertTo.category() == category() ||
|
||||||
_convertTo.category() == Category::Integer ||
|
(_convertTo.category() == Category::Integer && !dynamic_cast<IntegerType const&>(_convertTo).isAddress());
|
||||||
_convertTo.category() == Category::FixedBytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TypePointer FixedPointType::unaryOperatorResult(Token::Value _operator) const
|
TypePointer FixedPointType::unaryOperatorResult(Token::Value _operator) const
|
||||||
@ -682,13 +681,7 @@ bigint FixedPointType::minIntegerValue() const
|
|||||||
|
|
||||||
TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
|
TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
|
||||||
{
|
{
|
||||||
if (
|
auto commonType = Type::commonType(shared_from_this(), _other);
|
||||||
_other->category() != Category::RationalNumber &&
|
|
||||||
_other->category() != category() &&
|
|
||||||
_other->category() != Category::Integer
|
|
||||||
)
|
|
||||||
return TypePointer();
|
|
||||||
auto commonType = Type::commonType(shared_from_this(), _other); //might be fixed point or integer
|
|
||||||
|
|
||||||
if (!commonType)
|
if (!commonType)
|
||||||
return TypePointer();
|
return TypePointer();
|
||||||
@ -696,19 +689,16 @@ TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePoi
|
|||||||
// All fixed types can be compared
|
// All fixed types can be compared
|
||||||
if (Token::isCompareOp(_operator))
|
if (Token::isCompareOp(_operator))
|
||||||
return commonType;
|
return commonType;
|
||||||
if (Token::isBitOp(_operator) || Token::isBooleanOp(_operator))
|
if (Token::isBitOp(_operator) || Token::isBooleanOp(_operator) || _operator == Token::Exp)
|
||||||
return TypePointer();
|
return TypePointer();
|
||||||
if (auto fixType = dynamic_pointer_cast<FixedPointType const>(commonType))
|
|
||||||
{
|
|
||||||
if (Token::Exp == _operator)
|
|
||||||
return TypePointer();
|
|
||||||
}
|
|
||||||
else if (auto intType = dynamic_pointer_cast<IntegerType const>(commonType))
|
|
||||||
if (intType->isAddress())
|
|
||||||
return TypePointer();
|
|
||||||
return commonType;
|
return commonType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<IntegerType> FixedPointType::asIntegerType() const
|
||||||
|
{
|
||||||
|
return std::make_shared<IntegerType>(numBits(), isSigned() ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned);
|
||||||
|
}
|
||||||
|
|
||||||
tuple<bool, rational> RationalNumberType::parseRational(string const& _value)
|
tuple<bool, rational> RationalNumberType::parseRational(string const& _value)
|
||||||
{
|
{
|
||||||
rational value;
|
rational value;
|
||||||
@ -1148,7 +1138,7 @@ u256 RationalNumberType::literalValue(Literal const*) const
|
|||||||
auto fixed = fixedPointType();
|
auto fixed = fixedPointType();
|
||||||
solAssert(fixed, "");
|
solAssert(fixed, "");
|
||||||
int fractionalDigits = fixed->fractionalDigits();
|
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
|
// we ignore the literal and hope that the type was correctly determined
|
||||||
|
@ -396,6 +396,9 @@ public:
|
|||||||
/// smallest value in general.
|
/// smallest value in general.
|
||||||
bigint minIntegerValue() const;
|
bigint minIntegerValue() const;
|
||||||
|
|
||||||
|
/// @returns the smallest integer type that can hold this type with fractional parts shifted to integers.
|
||||||
|
std::shared_ptr<IntegerType> asIntegerType() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_totalBits;
|
int m_totalBits;
|
||||||
int m_fractionalDigits;
|
int m_fractionalDigits;
|
||||||
|
Loading…
Reference in New Issue
Block a user