Adjust code to review findings

This commit is contained in:
wechman
2022-09-28 13:06:25 +02:00
parent 1084a34f28
commit a8bf1f255d
5 changed files with 52 additions and 30 deletions
+18 -26
View File
@@ -413,30 +413,27 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
if (FunctionDefinition const* function = _unaryOperation.annotation().userDefinedFunction)
{
solAssert(function->isFree(), "Only free functions can be bound to a user type operator.");
FunctionType const* functionType = dynamic_cast<FunctionType const*>(
function->libraryFunction() ? function->typeViaContractName() : function->type());
solAssert(functionType);
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
solAssert(functionType);
evmasm::AssemblyItem returnLabel = m_context.pushNewTag();
_unaryOperation.subExpression().accept(*this);
acceptAndConvert(_unaryOperation.subExpression(), *functionType->selfType());
m_context << m_context.functionEntryLabel(*function).pushTag();
unsigned parameterSize =
CompilerUtils::sizeOnStack(functionType->parameterTypes()) +
functionType->selfType()->sizeOnStack();
if (m_context.runtimeContext())
// We have a runtime context, so we need the creation part.
utils().rightShiftNumberOnStack(32);
else
// Extract the runtime part.
m_context << ((u256(1) << 32) - 1) << Instruction::AND;
m_context.appendJump(evmasm::AssemblyItem::JumpType::IntoFunction);
m_context << returnLabel;
solAssert(
functionType->parameterTypes().size() == 0,
"Functions with parameters other than self parameter cannot be bound to a user type unary operator."
);
unsigned parameterSize = functionType->selfType()->sizeOnStack();
unsigned returnParametersSize = CompilerUtils::sizeOnStack(functionType->returnParameterTypes());
// callee adds return parameters, but removes arguments and return label
@@ -445,8 +442,6 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
return false;
}
Type const& type = *_unaryOperation.annotation().type;
if (type.category() == Type::Category::RationalNumber)
{
@@ -541,6 +536,7 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
Expression const& rightExpression = _binaryOperation.rightExpression();
if (FunctionDefinition const* function =_binaryOperation.annotation().userDefinedFunction)
{
solAssert(function->isFree(), "Only free function can be bound to a user type operator.");
FunctionType const* functionType = dynamic_cast<FunctionType const*>(
function->libraryFunction() ? function->typeViaContractName() : function->type()
);
@@ -548,26 +544,22 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
solAssert(functionType);
solAssert(
functionType->parameterTypes().size() == 1,
"Only functions with one parameter other than self parameter can be bound to a user type binary operator."
);
evmasm::AssemblyItem returnLabel = m_context.pushNewTag();
acceptAndConvert(leftExpression, *functionType->selfType());
acceptAndConvert(rightExpression, *functionType->parameterTypes().at(0));
m_context << m_context.functionEntryLabel(*function).pushTag();
m_context.appendJump(evmasm::AssemblyItem::JumpType::IntoFunction);
m_context << returnLabel;
unsigned parameterSize =
CompilerUtils::sizeOnStack(functionType->parameterTypes()) +
functionType->selfType()->sizeOnStack();
if (m_context.runtimeContext())
// We have a runtime context, so we need the creation part.
utils().rightShiftNumberOnStack(32);
else
// Extract the runtime part.
m_context << ((u256(1) << 32) - 1) << Instruction::AND;
m_context.appendJump(evmasm::AssemblyItem::JumpType::IntoFunction);
m_context << returnLabel;
unsigned returnParametersSize = CompilerUtils::sizeOnStack(functionType->returnParameterTypes());
// callee adds return parameters, but removes arguments and return label
m_context.adjustStackOffset(static_cast<int>(returnParametersSize - parameterSize) - 1);
@@ -689,11 +689,23 @@ bool IRGeneratorForStatements::visit(UnaryOperation const& _unaryOperation)
solAssert(functionType);
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
solAssert(functionType);
solAssert(
functionType->parameterTypes().size() == 0,
"Functions with parameters other than self parameter cannot be bound to a user type unary operator."
);
string parameter = expressionAsType(_unaryOperation.subExpression(), *functionType->selfType());
solAssert(!parameter.empty());
solAssert(function->isImplemented(), "");
solAssert(
function->returnParameters().size() == 1,
"A function bound to the user type operator is supposed to return exactly one value."
);
solAssert(*_unaryOperation.annotation().type == *function->returnParameters().at(0)->type(),
"A return type of the bound function is supposed to be same as a operator type."
);
define(_unaryOperation) <<
m_context.enqueueFunctionForCodeGeneration(*function) <<
("(" + parameter + ")\n");
@@ -821,6 +833,10 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
solAssert(functionType);
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
solAssert(functionType);
solAssert(
functionType->parameterTypes().size() == 1,
"Only functions with one parameter other than self parameter can be bound to a user type binary operator."
);
string left = expressionAsType(_binOp.leftExpression(), *functionType->selfType());
string right = expressionAsType(_binOp.rightExpression(), *functionType->parameterTypes().at(0));
@@ -828,6 +844,14 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
solAssert(function->isImplemented(), "");
solAssert(
function->returnParameters().size() == 1,
"A function bound to the user type operator is supposed to return exactly one value."
);
solAssert(*_binOp.annotation().type == *function->returnParameters().at(0)->type(),
"A return type of the bound function is supposed to be same as a operator type."
);
define(_binOp) <<
m_context.enqueueFunctionForCodeGeneration(*function) <<
("(" + left + ", " + right + ")\n");