diff options
author | Christian <c@ethdev.com> | 2014-12-19 01:53:43 +0800 |
---|---|---|
committer | Christian <c@ethdev.com> | 2014-12-19 07:12:04 +0800 |
commit | 7dc7827907087c25d89589244f7fa57f5a66e48d (patch) | |
tree | fa011514eb4cc8f56e4fec008422e19d6bb0901c /Types.cpp | |
parent | 59835e9df19332397d6e16b25bfe0dac4807996c (diff) | |
download | dexon-solidity-7dc7827907087c25d89589244f7fa57f5a66e48d.tar.gz dexon-solidity-7dc7827907087c25d89589244f7fa57f5a66e48d.tar.zst dexon-solidity-7dc7827907087c25d89589244f7fa57f5a66e48d.zip |
Possibility for binary operators to yield types different from their operands'.
Diffstat (limited to 'Types.cpp')
-rw-r--r-- | Types.cpp | 52 |
1 files changed, 42 insertions, 10 deletions
@@ -100,6 +100,16 @@ shared_ptr<Type const> Type::forLiteral(Literal const& _literal) } } +TypePointer Type::commonType(TypePointer const& _a, TypePointer const& _b) +{ + if (_b->isImplicitlyConvertibleTo(*_a)) + return _a; + else if (_a->isImplicitlyConvertibleTo(*_b)) + return _b; + else + return TypePointer(); +} + const MemberList Type::EmptyMemberList = MemberList(); shared_ptr<IntegerType const> IntegerType::smallestTypeForLiteral(string const& _literal) @@ -146,16 +156,6 @@ bool IntegerType::isExplicitlyConvertibleTo(Type const& _convertTo) const return _convertTo.getCategory() == getCategory() || _convertTo.getCategory() == Category::CONTRACT; } -bool IntegerType::acceptsBinaryOperator(Token::Value _operator) const -{ - if (isAddress()) - return Token::isCompareOp(_operator); - else if (isHash()) - return Token::isCompareOp(_operator) || Token::isBitOp(_operator); - else - return true; -} - bool IntegerType::acceptsUnaryOperator(Token::Value _operator) const { if (_operator == Token::DELETE) @@ -192,6 +192,28 @@ u256 IntegerType::literalValue(Literal const& _literal) const return u256(value); } +TypePointer IntegerType::binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const +{ + if (getCategory() != _other->getCategory()) + return TypePointer(); + auto commonType = dynamic_pointer_cast<IntegerType const>(Type::commonType(_this, _other)); + + if (!commonType) + return TypePointer(); + + if (commonType->isAddress()) + { + if (!Token::isCompareOp(_operator)) + return TypePointer(); + } + else if (commonType->isHash()) + { + if (!(Token::isCompareOp(_operator) || Token::isBitOp(_operator))) + return TypePointer(); + } + return commonType; +} + const MemberList IntegerType::AddressMemberList = MemberList({{"balance", make_shared<IntegerType const>(256)}, @@ -266,6 +288,16 @@ u256 BoolType::literalValue(Literal const& _literal) const BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Bool type constructed from non-boolean literal.")); } +TypePointer BoolType::binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const +{ + if (getCategory() != _other->getCategory()) + return TypePointer(); + if (Token::isCompareOp(_operator) || _operator == Token::AND || _operator == Token::OR) + return _this; + else + return TypePointer(); +} + bool ContractType::isExplicitlyConvertibleTo(Type const& _convertTo) const { if (isImplicitlyConvertibleTo(_convertTo)) |