Make bool type optional for bool literals.

This commit is contained in:
chriseth 2020-01-29 18:14:20 +01:00
parent a66782537a
commit d3b53ee394
4 changed files with 17 additions and 5 deletions

View File

@ -50,7 +50,7 @@ string AsmPrinter::operator()(Literal const& _literal) const
return _literal.value.str() + appendTypeName(_literal.type);
case LiteralKind::Boolean:
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal.");
return ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type);
return ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type, true);
case LiteralKind::String:
break;
}
@ -237,9 +237,17 @@ string AsmPrinter::formatTypedName(TypedName _variable) const
return _variable.name.str() + appendTypeName(_variable.type);
}
string AsmPrinter::appendTypeName(YulString _type) const
string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
{
if (_type.empty() || (m_dialect && _type == m_dialect->defaultType))
if (m_dialect && !_type.empty())
{
if (!_isBoolLiteral && _type == m_dialect->defaultType)
_type = {};
else if (_isBoolLiteral && _type == m_dialect->boolType && !m_dialect->defaultType.empty())
// Special case: If we have a bool type but empty default type, do not remove the type.
_type = {};
}
if (_type.empty())
return {};
else
return ":" + _type.str();

View File

@ -58,7 +58,7 @@ public:
private:
std::string formatTypedName(TypedName _variable) const;
std::string appendTypeName(YulString _type) const;
std::string appendTypeName(YulString _type, bool _isBoolLiteral = false) const;
Dialect const* m_dialect = nullptr;
};

View File

@ -48,6 +48,7 @@ struct BuiltinFunction
struct Dialect: boost::noncopyable
{
/// Default type, can be omitted.
YulString defaultType;
/// Type used for the literals "true" and "false".
YulString boolType;

View File

@ -573,6 +573,7 @@ BOOST_AUTO_TEST_CASE(default_types_set)
shared_ptr<Block> result = parse(
"{"
"let x:bool := true:bool "
"let z:bool := true "
"let y := add(1, 2) "
"switch y case 0 {} default {} "
"}",
@ -586,6 +587,7 @@ BOOST_AUTO_TEST_CASE(default_types_set)
BOOST_CHECK_EQUAL(AsmPrinter{}(*result),
"{\n"
" let x:bool := true:bool\n"
" let z:bool := true:bool\n"
" let y:u256 := add(1:u256, 2:u256)\n"
" switch y\n"
" case 0:u256 { }\n"
@ -597,7 +599,8 @@ BOOST_AUTO_TEST_CASE(default_types_set)
// should be omitted.
BOOST_CHECK_EQUAL(AsmPrinter{EVMDialectTyped::instance(EVMVersion{})}(*result),
"{\n"
" let x:bool := true:bool\n"
" let x:bool := true\n"
" let z:bool := true\n"
" let y := add(1, 2)\n"
" switch y\n"
" case 0 { }\n"