Merge pull request #4038 from danieljoonlee/develop

Change m_bytes to unsigned in FixedBytesType
This commit is contained in:
Alex Beregszaszi 2018-05-03 23:40:06 +01:00 committed by GitHub
commit a244f1a383
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 24 deletions

View File

@ -1200,8 +1200,9 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement)
string extension; string extension;
if (auto type = dynamic_cast<IntegerType const*>(var.annotation().type.get())) if (auto type = dynamic_cast<IntegerType const*>(var.annotation().type.get()))
{ {
int numBits = type->numBits(); unsigned numBits = type->numBits();
bool isSigned = type->isSigned(); bool isSigned = type->isSigned();
solAssert(numBits > 0, "");
string minValue; string minValue;
string maxValue; string maxValue;
if (isSigned) if (isSigned)

View File

@ -425,14 +425,14 @@ bool isValidShiftAndAmountType(Token::Value _operator, Type const& _shiftAmountT
} }
IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier): IntegerType::IntegerType(unsigned _bits, IntegerType::Modifier _modifier):
m_bits(_bits), m_modifier(_modifier) m_bits(_bits), m_modifier(_modifier)
{ {
if (isAddress()) if (isAddress())
solAssert(m_bits == 160, ""); solAssert(m_bits == 160, "");
solAssert( solAssert(
m_bits > 0 && m_bits <= 256 && m_bits % 8 == 0, m_bits > 0 && m_bits <= 256 && m_bits % 8 == 0,
"Invalid bit number for integer type: " + dev::toString(_bits) "Invalid bit number for integer type: " + dev::toString(m_bits)
); );
} }
@ -584,7 +584,7 @@ MemberList::MemberMap IntegerType::nativeMembers(ContractDefinition const*) cons
{ {
if (isAddress()) if (isAddress())
return { return {
{"balance", make_shared<IntegerType >(256)}, {"balance", make_shared<IntegerType>(256)},
{"call", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Kind::BareCall, true, StateMutability::Payable)}, {"call", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Kind::BareCall, true, StateMutability::Payable)},
{"callcode", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Kind::BareCallCode, true, StateMutability::Payable)}, {"callcode", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Kind::BareCallCode, true, StateMutability::Payable)},
{"delegatecall", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Kind::BareDelegateCall, true)}, {"delegatecall", make_shared<FunctionType>(strings(), strings{"bool"}, FunctionType::Kind::BareDelegateCall, true)},
@ -595,12 +595,11 @@ MemberList::MemberMap IntegerType::nativeMembers(ContractDefinition const*) cons
return MemberList::MemberMap(); return MemberList::MemberMap();
} }
FixedPointType::FixedPointType(int _totalBits, int _fractionalDigits, FixedPointType::Modifier _modifier): FixedPointType::FixedPointType(unsigned _totalBits, unsigned _fractionalDigits, FixedPointType::Modifier _modifier):
m_totalBits(_totalBits), m_fractionalDigits(_fractionalDigits), m_modifier(_modifier) m_totalBits(_totalBits), m_fractionalDigits(_fractionalDigits), m_modifier(_modifier)
{ {
solAssert( solAssert(
8 <= m_totalBits && m_totalBits <= 256 && m_totalBits % 8 == 0 && 8 <= m_totalBits && m_totalBits <= 256 && m_totalBits % 8 == 0 && m_fractionalDigits <= 80,
0 <= m_fractionalDigits && m_fractionalDigits <= 80,
"Invalid bit number(s) for fixed type: " + "Invalid bit number(s) for fixed type: " +
dev::toString(_totalBits) + "x" + dev::toString(_fractionalDigits) dev::toString(_totalBits) + "x" + dev::toString(_fractionalDigits)
); );
@ -696,7 +695,7 @@ TypePointer FixedPointType::binaryOperatorResult(Token::Value _operator, TypePoi
std::shared_ptr<IntegerType> FixedPointType::asIntegerType() const std::shared_ptr<IntegerType> FixedPointType::asIntegerType() const
{ {
return std::make_shared<IntegerType>(numBits(), isSigned() ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned); return 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)
@ -850,7 +849,7 @@ bool RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) const
if (isFractional()) if (isFractional())
return false; return false;
IntegerType const& targetType = dynamic_cast<IntegerType const&>(_convertTo); IntegerType const& targetType = dynamic_cast<IntegerType const&>(_convertTo);
int forSignBit = (targetType.isSigned() ? 1 : 0); unsigned forSignBit = (targetType.isSigned() ? 1 : 0);
if (m_value > rational(0)) if (m_value > rational(0))
{ {
if (m_value.numerator() <= (u256(-1) >> (256 - targetType.numBits() + forSignBit))) if (m_value.numerator() <= (u256(-1) >> (256 - targetType.numBits() + forSignBit)))
@ -1264,7 +1263,7 @@ bool StringLiteralType::isValidUTF8() const
return dev::validateUTF8(m_value); return dev::validateUTF8(m_value);
} }
FixedBytesType::FixedBytesType(int _bytes): m_bytes(_bytes) FixedBytesType::FixedBytesType(unsigned _bytes): m_bytes(_bytes)
{ {
solAssert( solAssert(
m_bytes > 0 && m_bytes <= 32, m_bytes > 0 && m_bytes <= 32,

View File

@ -319,7 +319,7 @@ public:
}; };
virtual Category category() const override { return Category::Integer; } virtual Category category() const override { return Category::Integer; }
explicit IntegerType(int _bits, Modifier _modifier = Modifier::Unsigned); explicit IntegerType(unsigned _bits, Modifier _modifier = Modifier::Unsigned);
virtual std::string richIdentifier() const override; virtual std::string richIdentifier() const override;
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override; virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
@ -342,7 +342,7 @@ public:
virtual TypePointer encodingType() const override { return shared_from_this(); } virtual TypePointer encodingType() const override { return shared_from_this(); }
virtual TypePointer interfaceType(bool) const override { return shared_from_this(); } virtual TypePointer interfaceType(bool) const override { return shared_from_this(); }
int numBits() const { return m_bits; } unsigned numBits() const { return m_bits; }
bool isAddress() const { return m_modifier == Modifier::Address; } bool isAddress() const { return m_modifier == Modifier::Address; }
bool isSigned() const { return m_modifier == Modifier::Signed; } bool isSigned() const { return m_modifier == Modifier::Signed; }
@ -350,7 +350,7 @@ public:
bigint maxValue() const; bigint maxValue() const;
private: private:
int m_bits; unsigned m_bits;
Modifier m_modifier; Modifier m_modifier;
}; };
@ -366,7 +366,7 @@ public:
}; };
virtual Category category() const override { return Category::FixedPoint; } virtual Category category() const override { return Category::FixedPoint; }
explicit FixedPointType(int _totalBits, int _fractionalDigits, Modifier _modifier = Modifier::Unsigned); explicit FixedPointType(unsigned _totalBits, unsigned _fractionalDigits, Modifier _modifier = Modifier::Unsigned);
virtual std::string richIdentifier() const override; virtual std::string richIdentifier() const override;
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override; virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
@ -386,9 +386,9 @@ public:
virtual TypePointer interfaceType(bool) const override { return shared_from_this(); } virtual TypePointer interfaceType(bool) const override { return shared_from_this(); }
/// Number of bits used for this type in total. /// Number of bits used for this type in total.
int numBits() const { return m_totalBits; } unsigned numBits() const { return m_totalBits; }
/// Number of decimal digits after the radix point. /// Number of decimal digits after the radix point.
int fractionalDigits() const { return m_fractionalDigits; } unsigned fractionalDigits() const { return m_fractionalDigits; }
bool isSigned() const { return m_modifier == Modifier::Signed; } bool isSigned() const { return m_modifier == Modifier::Signed; }
/// @returns the largest integer value this type con hold. Note that this is not the /// @returns the largest integer value this type con hold. Note that this is not the
/// largest value in general. /// largest value in general.
@ -401,8 +401,8 @@ public:
std::shared_ptr<IntegerType> asIntegerType() const; std::shared_ptr<IntegerType> asIntegerType() const;
private: private:
int m_totalBits; unsigned m_totalBits;
int m_fractionalDigits; unsigned m_fractionalDigits;
Modifier m_modifier; Modifier m_modifier;
}; };
@ -506,7 +506,7 @@ class FixedBytesType: public Type
public: public:
virtual Category category() const override { return Category::FixedBytes; } virtual Category category() const override { return Category::FixedBytes; }
explicit FixedBytesType(int _bytes); explicit FixedBytesType(unsigned _bytes);
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override; virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override; virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
@ -524,10 +524,10 @@ public:
virtual TypePointer encodingType() const override { return shared_from_this(); } virtual TypePointer encodingType() const override { return shared_from_this(); }
virtual TypePointer interfaceType(bool) const override { return shared_from_this(); } virtual TypePointer interfaceType(bool) const override { return shared_from_this(); }
int numBytes() const { return m_bytes; } unsigned numBytes() const { return m_bytes; }
private: private:
int m_bytes; unsigned m_bytes;
}; };
/** /**

View File

@ -688,7 +688,7 @@ void CompilerUtils::convertType(
m_context << Instruction::POP << u256(0); m_context << Instruction::POP << u256(0);
else if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded) else if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded)
{ {
int bytes = min(typeOnStack.numBytes(), targetType.numBytes()); unsigned bytes = min(typeOnStack.numBytes(), targetType.numBytes());
m_context << ((u256(1) << (256 - bytes * 8)) - 1); m_context << ((u256(1) << (256 - bytes * 8)) - 1);
m_context << Instruction::NOT << Instruction::AND; m_context << Instruction::NOT << Instruction::AND;
} }
@ -796,7 +796,7 @@ void CompilerUtils::convertType(
bytesConstRef data(value); bytesConstRef data(value);
if (targetTypeCategory == Type::Category::FixedBytes) if (targetTypeCategory == Type::Category::FixedBytes)
{ {
int const numBytes = dynamic_cast<FixedBytesType const&>(_targetType).numBytes(); unsigned const numBytes = dynamic_cast<FixedBytesType const&>(_targetType).numBytes();
solAssert(data.size() <= 32, ""); solAssert(data.size() <= 32, "");
m_context << (h256::Arith(h256(data, h256::AlignLeft)) & (~(u256(-1) >> (8 * numBytes)))); m_context << (h256::Arith(h256(data, h256::AlignLeft)) & (~(u256(-1) >> (8 * numBytes))));
} }