Sol -> Yul Generation: Implicit conversions

This commit is contained in:
Mathias Baumann 2019-04-15 18:10:36 +02:00 committed by chriseth
parent 6292adbde6
commit 944ac6fb6d
5 changed files with 76 additions and 16 deletions

View File

@ -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]);
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) + ")";
}

View File

@ -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;

View File

@ -0,0 +1,14 @@
// Tests IRGeneratorForStatements::visit(Assignment const& _assignment)
contract C {
function f() public pure returns (uint16 x) {
uint8 y;
assembly {
y := 0x12345678
}
x = y;
}
}
// ====
// compileViaYul: true
// ----
// f() -> 0x78

View File

@ -0,0 +1,17 @@
// IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
contract C {
function f(uint b) public pure returns (uint x) {
x = b;
}
function g() public pure returns (uint x) {
uint8 a;
assembly {
a := 0x12345678
}
x = f(a);
}
}
// ====
// compileViaYul: true
// ----
// g() -> 0x78

View File

@ -0,0 +1,13 @@
// IRGeneratorForStatements::visit(VariableDeclarationStatement const& _varDeclStatement)
contract C {
function f() public pure returns (uint y) {
uint8 a;
assembly { a := 0x12345678 }
uint z = a;
y = z;
}
}
// ====
// compileViaYul: true
// ----
// f() -> 0x78