Skip to content

Commit

Permalink
User-defined operators: Analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
wechman authored and cameel committed Feb 21, 2023
1 parent 9445483 commit 5b03c13
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 46 deletions.
40 changes: 38 additions & 2 deletions libsolidity/analysis/ControlFlowBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,53 @@ bool ControlFlowBuilder::visit(BinaryOperation const& _operation)
case Token::And:
{
visitNode(_operation);
solAssert(*_operation.annotation().userDefinedFunction == nullptr);
appendControlFlow(_operation.leftExpression());

auto nodes = splitFlow<2>();
nodes[0] = createFlow(nodes[0], _operation.rightExpression());
mergeFlow(nodes, nodes[1]);

return false;
}
default:
return ASTConstVisitor::visit(_operation);
{
if (*_operation.annotation().userDefinedFunction != nullptr)
{
visitNode(_operation);
_operation.leftExpression().accept(*this);
_operation.rightExpression().accept(*this);

m_currentNode->functionDefinition = *_operation.annotation().userDefinedFunction;

auto nextNode = newLabel();

connect(m_currentNode, nextNode);
m_currentNode = nextNode;
return false;
}
}
}
return ASTConstVisitor::visit(_operation);
}

bool ControlFlowBuilder::visit(UnaryOperation const& _operation)
{
solAssert(!!m_currentNode);

if (*_operation.annotation().userDefinedFunction != nullptr)
{
visitNode(_operation);
_operation.subExpression().accept(*this);
m_currentNode->functionDefinition = *_operation.annotation().userDefinedFunction;

auto nextNode = newLabel();

connect(m_currentNode, nextNode);
m_currentNode = nextNode;
return false;
}

return ASTConstVisitor::visit(_operation);
}

bool ControlFlowBuilder::visit(Conditional const& _conditional)
Expand Down
1 change: 1 addition & 0 deletions libsolidity/analysis/ControlFlowBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ControlFlowBuilder: private ASTConstVisitor, private yul::ASTWalker

// Visits for constructing the control flow.
bool visit(BinaryOperation const& _operation) override;
bool visit(UnaryOperation const& _operation) override;
bool visit(Conditional const& _conditional) override;
bool visit(TryStatement const& _tryStatement) override;
bool visit(IfStatement const& _ifStatement) override;
Expand Down
14 changes: 14 additions & 0 deletions libsolidity/analysis/FunctionCallGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ bool FunctionCallGraphBuilder::visit(MemberAccess const& _memberAccess)
return true;
}

bool FunctionCallGraphBuilder::visit(BinaryOperation const& _binaryOperation)
{
if (*_binaryOperation.annotation().userDefinedFunction != nullptr)
functionReferenced(**_binaryOperation.annotation().userDefinedFunction, true /* called directly */);
return true;
}

bool FunctionCallGraphBuilder::visit(UnaryOperation const& _unaryOperation)
{
if (*_unaryOperation.annotation().userDefinedFunction != nullptr)
functionReferenced(**_unaryOperation.annotation().userDefinedFunction, true /* called directly */);
return true;
}

bool FunctionCallGraphBuilder::visit(ModifierInvocation const& _modifierInvocation)
{
if (auto const* modifier = dynamic_cast<ModifierDefinition const*>(_modifierInvocation.name().annotation().referencedDeclaration))
Expand Down
2 changes: 2 additions & 0 deletions libsolidity/analysis/FunctionCallGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class FunctionCallGraphBuilder: private ASTConstVisitor
bool visit(EmitStatement const& _emitStatement) override;
bool visit(Identifier const& _identifier) override;
bool visit(MemberAccess const& _memberAccess) override;
bool visit(BinaryOperation const& _binaryOperation) override;
bool visit(UnaryOperation const& _unaryOperation) override;
bool visit(ModifierInvocation const& _modifierInvocation) override;
bool visit(NewExpression const& _newExpression) override;

Expand Down
6 changes: 6 additions & 0 deletions libsolidity/analysis/SyntaxChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ void SyntaxChecker::endVisit(ContractDefinition const&)

bool SyntaxChecker::visit(UsingForDirective const& _usingFor)
{
if (!_usingFor.usesBraces())
solAssert(
_usingFor.functionsAndOperators().size() == 1 &&
!get<1>(_usingFor.functionsAndOperators().front())
);

if (!m_currentContractKind && !_usingFor.typeName())
m_errorReporter.syntaxError(
8118_error,
Expand Down
Loading

0 comments on commit 5b03c13

Please sign in to comment.