aboutsummaryrefslogtreecommitdiffstats
path: root/Types.cpp
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2015-07-08 07:13:56 +0800
committerchriseth <c@ethdev.com>2015-07-09 23:10:58 +0800
commit46dde467e777c8339a5b58e877bd886bab9a15b9 (patch)
tree984cf47267a295f22cd349ab52a0c42bc4d8ca34 /Types.cpp
parentaa6182ab87989ed50df2ec779237a2b708ccfd12 (diff)
downloaddexon-solidity-46dde467e777c8339a5b58e877bd886bab9a15b9.tar.gz
dexon-solidity-46dde467e777c8339a5b58e877bd886bab9a15b9.tar.zst
dexon-solidity-46dde467e777c8339a5b58e877bd886bab9a15b9.zip
Flexible string literals, convertible to bytesX, bytes and string.
Diffstat (limited to 'Types.cpp')
-rw-r--r--Types.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/Types.cpp b/Types.cpp
index 5a949299..3ea9caa7 100644
--- a/Types.cpp
+++ b/Types.cpp
@@ -212,8 +212,7 @@ TypePointer Type::forLiteral(Literal const& _literal)
case Token::Number:
return make_shared<IntegerConstantType>(_literal);
case Token::StringLiteral:
- //@todo put larger strings into dynamic strings
- return FixedBytesType::smallestTypeForLiteral(_literal.getValue());
+ return make_shared<StringLiteralType>(_literal);
default:
return shared_ptr<Type>();
}
@@ -378,7 +377,7 @@ bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) cons
else if (_convertTo.getCategory() == Category::FixedBytes)
{
FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo);
- return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits();
+ return fixedBytes.numBytes() * 8 >= getIntegerType()->getNumBits();
}
else
return false;
@@ -530,6 +529,33 @@ shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const
);
}
+StringLiteralType::StringLiteralType(Literal const& _literal):
+ m_value(_literal.getValue())
+{
+}
+
+bool StringLiteralType::isImplicitlyConvertibleTo(Type const& _convertTo) const
+{
+ if (auto fixedBytes = dynamic_cast<FixedBytesType const*>(&_convertTo))
+ return size_t(fixedBytes->numBytes()) >= m_value.size();
+ else if (auto arrayType = dynamic_cast<ArrayType const*>(&_convertTo))
+ return arrayType->isByteArray();
+ else
+ return false;
+}
+
+bool StringLiteralType::operator==(const Type& _other) const
+{
+ if (_other.getCategory() != getCategory())
+ return false;
+ return m_value == dynamic_cast<StringLiteralType const&>(_other).m_value;
+}
+
+TypePointer StringLiteralType::mobileType() const
+{
+ return make_shared<ArrayType>(DataLocation::Memory, true);
+}
+
shared_ptr<FixedBytesType> FixedBytesType::smallestTypeForLiteral(string const& _literal)
{
if (_literal.length() <= 32)
@@ -590,15 +616,6 @@ bool FixedBytesType::operator==(Type const& _other) const
return other.m_bytes == m_bytes;
}
-u256 FixedBytesType::literalValue(const Literal* _literal) const
-{
- solAssert(_literal, "");
- u256 value = 0;
- for (char c: _literal->getValue())
- value = (value << 8) | byte(c);
- return value << ((32 - _literal->getValue().length()) * 8);
-}
-
bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
{
// conversion to integer is fine, but not to address