Sol -> Yul Generation: Implicit conversions

This commit is contained in:
Mathias Baumann
2019-04-24 22:52:17 +02:00
committed by chriseth
parent 6292adbde6
commit 944ac6fb6d
5 changed files with 76 additions and 16 deletions
@@ -90,15 +90,12 @@ bool IRGeneratorForStatements::visit(VariableDeclarationStatement const& _varDec
expression->accept(*this);
solUnimplementedAssert(
*expression->annotation().type == *_varDeclStatement.declarations().front()->type(),
"Type conversion not yet implemented"
);
VariableDeclaration const& varDecl = *_varDeclStatement.declarations().front();
m_code <<
"let " <<
m_context.variableName(*_varDeclStatement.declarations().front()) <<
m_context.variableName(varDecl) <<
" := " <<
m_context.variable(*expression) <<
expressionAsType(*expression, *varDecl.type()) <<
"\n";
}
else
@@ -115,15 +112,17 @@ bool IRGeneratorForStatements::visit(Assignment const& _assignment)
_assignment.rightHandSide().accept(*this);
// solUnimplementedAssert(
// *_assignment.rightHandSide().annotation().type == *_assignment.leftHandSide().annotation().type,
// "Type conversion not yet implemented"
// );
// TODO proper lvalue handling
auto const& identifier = dynamic_cast<Identifier const&>(_assignment.leftHandSide());
string varName = m_context.variableName(dynamic_cast<VariableDeclaration const&>(*identifier.annotation().referencedDeclaration));
m_code << varName << " := " << m_context.variable(_assignment.rightHandSide()) << "\n";
auto const& lvalue = dynamic_cast<Identifier const&>(_assignment.leftHandSide());
string varName = m_context.variableName(dynamic_cast<VariableDeclaration const&>(*lvalue.annotation().referencedDeclaration));
m_code <<
varName <<
" := " <<
expressionAsType(_assignment.rightHandSide(), *lvalue.annotation().type) <<
"\n";
m_code << "let " << m_context.variable(_assignment) << " := " << varName << "\n";
return false;
}
@@ -185,9 +184,11 @@ bool IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
for (unsigned i = 0; i < arguments.size(); ++i)
{
arguments[i]->accept(*this);
// TODO convert
//utils().convertType(*arguments[i]->annotation().type, *function.parameterTypes()[i]);
args.emplace_back(m_context.variable(*arguments[i]));
if (functionType->takesArbitraryParameters())
args.emplace_back(m_context.variable(*arguments[i]));
else
args.emplace_back(expressionAsType(*arguments[i], *parameterTypes[i]));
}
if (auto identifier = dynamic_cast<Identifier const*>(&_functionCall.expression()))
@@ -273,3 +274,14 @@ bool IRGeneratorForStatements::visit(Literal const& _literal)
}
return false;
}
string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to)
{
Type const& from = *_expression.annotation().type;
string varName = m_context.variable(_expression);
if (from == _to)
return varName;
else
return m_utils.conversionFunction(from, _to) + "(" + std::move(varName) + ")";
}
@@ -53,6 +53,10 @@ public:
bool visit(Literal const& _literal) override;
private:
/// @returns a Yul expression representing the current value of @a _expression,
/// converted to type @a _to if it does not yet have that type.
std::string expressionAsType(Expression const& _expression, Type const& _to);
std::ostringstream m_code;
IRGenerationContext& m_context;
YulUtilFunctions& m_utils;