Minor code adjustments in ExpressionCompiler and IRGeneratorForStatements

This commit is contained in:
wechman 2022-07-12 12:09:05 +02:00
parent beae1e6263
commit 61075e28db
2 changed files with 16 additions and 24 deletions

View File

@ -411,18 +411,17 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
{ {
CompilerContext::LocationSetter locationSetter(m_context, _unaryOperation); CompilerContext::LocationSetter locationSetter(m_context, _unaryOperation);
if (_unaryOperation.annotation().userDefinedFunction) if (FunctionDefinition const* function = _unaryOperation.annotation().userDefinedFunction)
{ {
FunctionDefinition const& function = *_unaryOperation.annotation().userDefinedFunction;
FunctionType const* functionType = dynamic_cast<FunctionType const*>( FunctionType const* functionType = dynamic_cast<FunctionType const*>(
function.libraryFunction() ? function.typeViaContractName() : function.type()); function->libraryFunction() ? function->typeViaContractName() : function->type());
solAssert(functionType); solAssert(functionType);
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction(); functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
solAssert(functionType); solAssert(functionType);
evmasm::AssemblyItem returnLabel = m_context.pushNewTag(); evmasm::AssemblyItem returnLabel = m_context.pushNewTag();
_unaryOperation.subExpression().accept(*this); _unaryOperation.subExpression().accept(*this);
m_context << m_context.functionEntryLabel(function).pushTag(); m_context << m_context.functionEntryLabel(*function).pushTag();
unsigned parameterSize = unsigned parameterSize =
CompilerUtils::sizeOnStack(functionType->parameterTypes()) + CompilerUtils::sizeOnStack(functionType->parameterTypes()) +
@ -540,12 +539,10 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
CompilerContext::LocationSetter locationSetter(m_context, _binaryOperation); CompilerContext::LocationSetter locationSetter(m_context, _binaryOperation);
Expression const& leftExpression = _binaryOperation.leftExpression(); Expression const& leftExpression = _binaryOperation.leftExpression();
Expression const& rightExpression = _binaryOperation.rightExpression(); Expression const& rightExpression = _binaryOperation.rightExpression();
if (_binaryOperation.annotation().userDefinedFunction) if (FunctionDefinition const* function =_binaryOperation.annotation().userDefinedFunction)
{ {
// TODO extract from function call
FunctionDefinition const& function = *_binaryOperation.annotation().userDefinedFunction;
FunctionType const* functionType = dynamic_cast<FunctionType const*>( FunctionType const* functionType = dynamic_cast<FunctionType const*>(
function.libraryFunction() ? function.typeViaContractName() : function.type() function->libraryFunction() ? function->typeViaContractName() : function->type()
); );
solAssert(functionType); solAssert(functionType);
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction(); functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
@ -555,7 +552,7 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
acceptAndConvert(leftExpression, *functionType->selfType()); acceptAndConvert(leftExpression, *functionType->selfType());
acceptAndConvert(rightExpression, *functionType->parameterTypes().at(0)); acceptAndConvert(rightExpression, *functionType->parameterTypes().at(0));
m_context << m_context.functionEntryLabel(function).pushTag(); m_context << m_context.functionEntryLabel(*function).pushTag();
unsigned parameterSize = unsigned parameterSize =
CompilerUtils::sizeOnStack(functionType->parameterTypes()) + CompilerUtils::sizeOnStack(functionType->parameterTypes()) +

View File

@ -673,20 +673,18 @@ bool IRGeneratorForStatements::visit(UnaryOperation const& _unaryOperation)
{ {
setLocation(_unaryOperation); setLocation(_unaryOperation);
if (_unaryOperation.annotation().userDefinedFunction) if (FunctionDefinition const* function = _unaryOperation.annotation().userDefinedFunction)
{ {
_unaryOperation.subExpression().accept(*this); _unaryOperation.subExpression().accept(*this);
setLocation(_unaryOperation); setLocation(_unaryOperation);
// TODO extract from function call
FunctionDefinition const& function = *_unaryOperation.annotation().userDefinedFunction;
solAssert( solAssert(
dynamic_cast<SourceUnit const*>(function.scope()), dynamic_cast<SourceUnit const*>(function->scope()),
"Only file-level functions and library functions can be bound to a user type operator." "Only file-level functions and library functions can be bound to a user type operator."
); );
FunctionType const* functionType = dynamic_cast<FunctionType const*>( FunctionType const* functionType = dynamic_cast<FunctionType const*>(
function.libraryFunction() ? function.typeViaContractName() : function.type() function->libraryFunction() ? function->typeViaContractName() : function->type()
); );
solAssert(functionType); solAssert(functionType);
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction(); functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
@ -694,10 +692,10 @@ bool IRGeneratorForStatements::visit(UnaryOperation const& _unaryOperation)
string parameter = expressionAsType(_unaryOperation.subExpression(), *functionType->selfType()); string parameter = expressionAsType(_unaryOperation.subExpression(), *functionType->selfType());
solAssert(!parameter.empty()); solAssert(!parameter.empty());
solAssert(function.isImplemented(), ""); solAssert(function->isImplemented(), "");
define(_unaryOperation) << define(_unaryOperation) <<
m_context.enqueueFunctionForCodeGeneration(function) << m_context.enqueueFunctionForCodeGeneration(*function) <<
("(" + parameter + ")\n"); ("(" + parameter + ")\n");
return false; return false;
@ -806,22 +804,19 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
{ {
setLocation(_binOp); setLocation(_binOp);
// TODO: make this nicer if (FunctionDefinition const* function = _binOp.annotation().userDefinedFunction)
if (_binOp.annotation().userDefinedFunction)
{ {
_binOp.leftExpression().accept(*this); _binOp.leftExpression().accept(*this);
_binOp.rightExpression().accept(*this); _binOp.rightExpression().accept(*this);
setLocation(_binOp); setLocation(_binOp);
// TODO extract from function call
FunctionDefinition const& function = *_binOp.annotation().userDefinedFunction;
solAssert( solAssert(
dynamic_cast<SourceUnit const*>(function.scope()), dynamic_cast<SourceUnit const*>(function->scope()),
"Only file-level functions and library functions can be bound to a user type operator." "Only file-level functions and library functions can be bound to a user type operator."
); );
FunctionType const* functionType = dynamic_cast<FunctionType const*>( FunctionType const* functionType = dynamic_cast<FunctionType const*>(
function.libraryFunction() ? function.typeViaContractName() : function.type() function->libraryFunction() ? function->typeViaContractName() : function->type()
); );
solAssert(functionType); solAssert(functionType);
functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction(); functionType = dynamic_cast<FunctionType const&>(*functionType).asBoundFunction();
@ -831,10 +826,10 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
string right = expressionAsType(_binOp.rightExpression(), *functionType->parameterTypes().at(0)); string right = expressionAsType(_binOp.rightExpression(), *functionType->parameterTypes().at(0));
solAssert(!left.empty() && !right.empty()); solAssert(!left.empty() && !right.empty());
solAssert(function.isImplemented(), ""); solAssert(function->isImplemented(), "");
define(_binOp) << define(_binOp) <<
m_context.enqueueFunctionForCodeGeneration(function) << m_context.enqueueFunctionForCodeGeneration(*function) <<
("(" + left + ", " + right + ")\n"); ("(" + left + ", " + right + ")\n");
return false; return false;