2018-05-04 13:58:10 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
#include <libsolidity/analysis/ControlFlowBuilder.h>
|
2022-06-14 13:31:32 +00:00
|
|
|
#include <libsolidity/ast/ASTUtils.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-03-05 09:47:01 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
using namespace solidity;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
2018-05-04 13:58:10 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2021-12-30 17:16:29 +00:00
|
|
|
ControlFlowBuilder::ControlFlowBuilder(CFG::NodeContainer& _nodeContainer, FunctionFlow const& _functionFlow, ContractDefinition const* _contract):
|
2018-12-07 17:20:35 +00:00
|
|
|
m_nodeContainer(_nodeContainer),
|
|
|
|
m_currentNode(_functionFlow.entry),
|
|
|
|
m_returnNode(_functionFlow.exit),
|
2020-03-05 09:47:01 +00:00
|
|
|
m_revertNode(_functionFlow.revert),
|
2021-12-30 17:16:29 +00:00
|
|
|
m_transactionReturnNode(_functionFlow.transactionReturn),
|
|
|
|
m_contract(_contract)
|
2018-05-04 13:58:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-05 09:47:01 +00:00
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
unique_ptr<FunctionFlow> ControlFlowBuilder::createFunctionFlow(
|
|
|
|
CFG::NodeContainer& _nodeContainer,
|
2021-12-30 17:16:29 +00:00
|
|
|
FunctionDefinition const& _function,
|
|
|
|
ContractDefinition const* _contract
|
2018-05-04 13:58:10 +00:00
|
|
|
)
|
|
|
|
{
|
2019-11-27 16:24:21 +00:00
|
|
|
auto functionFlow = make_unique<FunctionFlow>();
|
2018-05-04 13:58:10 +00:00
|
|
|
functionFlow->entry = _nodeContainer.newNode();
|
|
|
|
functionFlow->exit = _nodeContainer.newNode();
|
|
|
|
functionFlow->revert = _nodeContainer.newNode();
|
2020-03-05 09:47:01 +00:00
|
|
|
functionFlow->transactionReturn = _nodeContainer.newNode();
|
2021-12-30 17:16:29 +00:00
|
|
|
ControlFlowBuilder builder(_nodeContainer, *functionFlow, _contract);
|
2018-05-04 13:58:10 +00:00
|
|
|
builder.appendControlFlow(_function);
|
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
return functionFlow;
|
2018-05-04 13:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(BinaryOperation const& _operation)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
|
|
|
|
2019-11-11 16:09:59 +00:00
|
|
|
switch (_operation.getOperator())
|
2018-05-04 13:58:10 +00:00
|
|
|
{
|
|
|
|
case Token::Or:
|
|
|
|
case Token::And:
|
|
|
|
{
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_operation);
|
2022-07-06 07:17:59 +00:00
|
|
|
solAssert(*_operation.annotation().userDefinedFunction == nullptr);
|
2018-05-04 13:58:10 +00:00
|
|
|
appendControlFlow(_operation.leftExpression());
|
|
|
|
|
|
|
|
auto nodes = splitFlow<2>();
|
|
|
|
nodes[0] = createFlow(nodes[0], _operation.rightExpression());
|
|
|
|
mergeFlow(nodes, nodes[1]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default:
|
2022-07-06 07:17:59 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 13:58:10 +00:00
|
|
|
}
|
2022-07-06 07:17:59 +00:00
|
|
|
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);
|
2018-05-04 13:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(Conditional const& _conditional)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_conditional);
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
_conditional.condition().accept(*this);
|
|
|
|
|
|
|
|
auto nodes = splitFlow<2>();
|
|
|
|
|
|
|
|
nodes[0] = createFlow(nodes[0], _conditional.trueExpression());
|
|
|
|
nodes[1] = createFlow(nodes[1], _conditional.falseExpression());
|
|
|
|
|
|
|
|
mergeFlow(nodes);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:02:09 +00:00
|
|
|
bool ControlFlowBuilder::visit(TryStatement const& _tryStatement)
|
|
|
|
{
|
|
|
|
appendControlFlow(_tryStatement.externalCall());
|
|
|
|
|
|
|
|
auto nodes = splitFlow(_tryStatement.clauses().size());
|
|
|
|
for (size_t i = 0; i < _tryStatement.clauses().size(); ++i)
|
|
|
|
nodes[i] = createFlow(nodes[i], _tryStatement.clauses()[i]->block());
|
|
|
|
mergeFlow(nodes);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
bool ControlFlowBuilder::visit(IfStatement const& _ifStatement)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_ifStatement);
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
_ifStatement.condition().accept(*this);
|
|
|
|
|
|
|
|
auto nodes = splitFlow<2>();
|
|
|
|
nodes[0] = createFlow(nodes[0], _ifStatement.trueStatement());
|
|
|
|
|
|
|
|
if (_ifStatement.falseStatement())
|
|
|
|
{
|
|
|
|
nodes[1] = createFlow(nodes[1], *_ifStatement.falseStatement());
|
|
|
|
mergeFlow(nodes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mergeFlow(nodes, nodes[1]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(ForStatement const& _forStatement)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_forStatement);
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
if (_forStatement.initializationExpression())
|
|
|
|
_forStatement.initializationExpression()->accept(*this);
|
|
|
|
|
|
|
|
auto condition = createLabelHere();
|
|
|
|
|
|
|
|
if (_forStatement.condition())
|
|
|
|
appendControlFlow(*_forStatement.condition());
|
|
|
|
|
2020-03-05 09:47:01 +00:00
|
|
|
auto postPart = newLabel();
|
2018-05-04 13:58:10 +00:00
|
|
|
auto nodes = splitFlow<2>();
|
|
|
|
auto afterFor = nodes[1];
|
|
|
|
m_currentNode = nodes[0];
|
|
|
|
|
|
|
|
{
|
2020-03-05 09:47:01 +00:00
|
|
|
BreakContinueScope scope(*this, afterFor, postPart);
|
2018-05-04 13:58:10 +00:00
|
|
|
appendControlFlow(_forStatement.body());
|
|
|
|
}
|
|
|
|
|
2020-03-05 09:47:01 +00:00
|
|
|
placeAndConnectLabel(postPart);
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
if (auto expression = _forStatement.loopExpression())
|
|
|
|
appendControlFlow(*expression);
|
|
|
|
|
|
|
|
connect(m_currentNode, condition);
|
|
|
|
m_currentNode = afterFor;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(WhileStatement const& _whileStatement)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_whileStatement);
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
if (_whileStatement.isDoWhile())
|
|
|
|
{
|
|
|
|
auto afterWhile = newLabel();
|
|
|
|
auto whileBody = createLabelHere();
|
2018-05-15 12:19:40 +00:00
|
|
|
auto condition = newLabel();
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
{
|
2018-05-15 12:19:40 +00:00
|
|
|
BreakContinueScope scope(*this, afterWhile, condition);
|
2018-05-04 13:58:10 +00:00
|
|
|
appendControlFlow(_whileStatement.body());
|
|
|
|
}
|
2018-05-15 12:19:40 +00:00
|
|
|
|
|
|
|
placeAndConnectLabel(condition);
|
2018-05-04 13:58:10 +00:00
|
|
|
appendControlFlow(_whileStatement.condition());
|
|
|
|
|
|
|
|
connect(m_currentNode, whileBody);
|
|
|
|
placeAndConnectLabel(afterWhile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto whileCondition = createLabelHere();
|
|
|
|
|
|
|
|
appendControlFlow(_whileStatement.condition());
|
|
|
|
|
|
|
|
auto nodes = splitFlow<2>();
|
|
|
|
|
|
|
|
auto whileBody = nodes[0];
|
|
|
|
auto afterWhile = nodes[1];
|
|
|
|
|
|
|
|
m_currentNode = whileBody;
|
|
|
|
{
|
|
|
|
BreakContinueScope scope(*this, afterWhile, whileCondition);
|
|
|
|
appendControlFlow(_whileStatement.body());
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(m_currentNode, whileCondition);
|
|
|
|
|
|
|
|
m_currentNode = afterWhile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:33:46 +00:00
|
|
|
bool ControlFlowBuilder::visit(Break const& _break)
|
2018-05-04 13:58:10 +00:00
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
|
|
|
solAssert(!!m_breakJump, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_break);
|
2018-05-04 13:58:10 +00:00
|
|
|
connect(m_currentNode, m_breakJump);
|
|
|
|
m_currentNode = newLabel();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:33:46 +00:00
|
|
|
bool ControlFlowBuilder::visit(Continue const& _continue)
|
2018-05-04 13:58:10 +00:00
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
|
|
|
solAssert(!!m_continueJump, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_continue);
|
2018-05-04 13:58:10 +00:00
|
|
|
connect(m_currentNode, m_continueJump);
|
|
|
|
m_currentNode = newLabel();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:33:46 +00:00
|
|
|
bool ControlFlowBuilder::visit(Throw const& _throw)
|
2018-05-04 13:58:10 +00:00
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2018-12-07 17:20:35 +00:00
|
|
|
solAssert(!!m_revertNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_throw);
|
2018-12-07 17:20:35 +00:00
|
|
|
connect(m_currentNode, m_revertNode);
|
2018-05-04 13:58:10 +00:00
|
|
|
m_currentNode = newLabel();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-18 12:21:09 +00:00
|
|
|
bool ControlFlowBuilder::visit(RevertStatement const& _revert)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
|
|
|
solAssert(!!m_revertNode, "");
|
|
|
|
visitNode(_revert);
|
|
|
|
connect(m_currentNode, m_revertNode);
|
|
|
|
m_currentNode = newLabel();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
bool ControlFlowBuilder::visit(PlaceholderStatement const&)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2018-12-07 17:20:35 +00:00
|
|
|
solAssert(!!m_placeholderEntry, "");
|
|
|
|
solAssert(!!m_placeholderExit, "");
|
2018-05-04 13:58:10 +00:00
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
connect(m_currentNode, m_placeholderEntry);
|
2018-05-04 13:58:10 +00:00
|
|
|
m_currentNode = newLabel();
|
2018-12-07 17:20:35 +00:00
|
|
|
connect(m_placeholderExit, m_currentNode);
|
2018-05-04 13:58:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(FunctionCall const& _functionCall)
|
|
|
|
{
|
2021-06-03 13:43:05 +00:00
|
|
|
solAssert(!!m_revertNode, "");
|
2018-05-04 13:58:10 +00:00
|
|
|
solAssert(!!m_currentNode, "");
|
|
|
|
solAssert(!!_functionCall.expression().annotation().type, "");
|
|
|
|
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto functionType = dynamic_cast<FunctionType const*>(_functionCall.expression().annotation().type))
|
2018-05-04 13:58:10 +00:00
|
|
|
switch (functionType->kind())
|
|
|
|
{
|
|
|
|
case FunctionType::Kind::Revert:
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_functionCall);
|
2018-05-04 13:58:10 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
|
|
|
ASTNode::listAccept(_functionCall.arguments(), *this);
|
2021-06-03 13:43:05 +00:00
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
connect(m_currentNode, m_revertNode);
|
2021-06-03 13:43:05 +00:00
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
m_currentNode = newLabel();
|
|
|
|
return false;
|
|
|
|
case FunctionType::Kind::Require:
|
|
|
|
case FunctionType::Kind::Assert:
|
|
|
|
{
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_functionCall);
|
2018-05-04 13:58:10 +00:00
|
|
|
_functionCall.expression().accept(*this);
|
|
|
|
ASTNode::listAccept(_functionCall.arguments(), *this);
|
2021-06-03 13:43:05 +00:00
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
connect(m_currentNode, m_revertNode);
|
2021-06-03 13:43:05 +00:00
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
auto nextNode = newLabel();
|
2021-06-03 13:43:05 +00:00
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
connect(m_currentNode, nextNode);
|
|
|
|
m_currentNode = nextNode;
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-28 19:32:46 +00:00
|
|
|
case FunctionType::Kind::Internal:
|
|
|
|
{
|
2021-06-03 13:43:05 +00:00
|
|
|
visitNode(_functionCall);
|
|
|
|
_functionCall.expression().accept(*this);
|
|
|
|
ASTNode::listAccept(_functionCall.arguments(), *this);
|
|
|
|
|
2022-08-30 07:46:55 +00:00
|
|
|
m_currentNode->functionDefinition = ASTNode::resolveFunctionCall(_functionCall, m_contract);
|
2021-06-03 13:43:05 +00:00
|
|
|
|
|
|
|
auto nextNode = newLabel();
|
|
|
|
|
|
|
|
connect(m_currentNode, nextNode);
|
|
|
|
m_currentNode = nextNode;
|
|
|
|
|
|
|
|
return false;
|
2020-10-28 19:32:46 +00:00
|
|
|
}
|
2018-05-04 13:58:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ASTConstVisitor::visit(_functionCall);
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
bool ControlFlowBuilder::visit(ModifierInvocation const& _modifierInvocation)
|
|
|
|
{
|
2022-08-30 07:46:55 +00:00
|
|
|
solAssert(m_contract, "Free functions cannot have modifiers");
|
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
if (auto arguments = _modifierInvocation.arguments())
|
|
|
|
for (auto& argument: *arguments)
|
|
|
|
appendControlFlow(*argument);
|
|
|
|
|
|
|
|
auto modifierDefinition = dynamic_cast<ModifierDefinition const*>(
|
2020-08-11 09:18:22 +00:00
|
|
|
_modifierInvocation.name().annotation().referencedDeclaration
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
2021-12-30 17:16:29 +00:00
|
|
|
|
|
|
|
if (!modifierDefinition)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
VirtualLookup const& requiredLookup = *_modifierInvocation.name().annotation().requiredLookup;
|
|
|
|
|
|
|
|
if (requiredLookup == VirtualLookup::Virtual)
|
|
|
|
modifierDefinition = &modifierDefinition->resolveVirtual(*m_contract);
|
|
|
|
else
|
|
|
|
solAssert(requiredLookup == VirtualLookup::Static);
|
|
|
|
|
|
|
|
if (!modifierDefinition->isImplemented())
|
|
|
|
return false;
|
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
solAssert(!!m_returnNode, "");
|
|
|
|
|
|
|
|
m_placeholderEntry = newLabel();
|
|
|
|
m_placeholderExit = newLabel();
|
|
|
|
|
|
|
|
appendControlFlow(*modifierDefinition);
|
|
|
|
connect(m_currentNode, m_returnNode);
|
|
|
|
|
|
|
|
m_currentNode = m_placeholderEntry;
|
|
|
|
m_returnNode = m_placeholderExit;
|
|
|
|
|
|
|
|
m_placeholderEntry = nullptr;
|
|
|
|
m_placeholderExit = nullptr;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
|
|
|
for (auto const& parameter: _functionDefinition.parameters())
|
|
|
|
appendControlFlow(*parameter);
|
|
|
|
|
|
|
|
for (auto const& returnParameter: _functionDefinition.returnParameters())
|
|
|
|
{
|
|
|
|
appendControlFlow(*returnParameter);
|
|
|
|
m_returnNode->variableOccurrences.emplace_back(
|
|
|
|
*returnParameter,
|
2020-03-05 09:47:01 +00:00
|
|
|
VariableOccurrence::Kind::Return
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-30 17:16:29 +00:00
|
|
|
for (auto const& modifierInvocation: _functionDefinition.modifiers())
|
|
|
|
appendControlFlow(*modifierInvocation);
|
2018-12-07 17:20:35 +00:00
|
|
|
|
|
|
|
appendControlFlow(_functionDefinition.body());
|
|
|
|
|
|
|
|
connect(m_currentNode, m_returnNode);
|
|
|
|
m_currentNode = nullptr;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(Return const& _return)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
|
|
|
solAssert(!!m_returnNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_return);
|
2018-12-07 17:20:35 +00:00
|
|
|
if (_return.expression())
|
|
|
|
{
|
|
|
|
appendControlFlow(*_return.expression());
|
|
|
|
// Returns with return expression are considered to be assignments to the return parameters.
|
|
|
|
for (auto returnParameter: _return.annotation().functionReturnParameters->parameters())
|
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
|
|
|
*returnParameter,
|
|
|
|
VariableOccurrence::Kind::Assignment,
|
2020-03-05 09:47:01 +00:00
|
|
|
_return.location()
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
connect(m_currentNode, m_returnNode);
|
|
|
|
m_currentNode = newLabel();
|
2019-01-08 18:33:46 +00:00
|
|
|
return false;
|
2018-12-07 17:20:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 18:33:46 +00:00
|
|
|
bool ControlFlowBuilder::visit(FunctionTypeName const& _functionTypeName)
|
2018-12-07 17:20:35 +00:00
|
|
|
{
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_functionTypeName);
|
2018-12-07 17:20:35 +00:00
|
|
|
// Do not visit the parameters and return values of a function type name.
|
|
|
|
// We do not want to consider them as variable declarations for the control flow graph.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(InlineAssembly const& _inlineAssembly)
|
|
|
|
{
|
2020-03-05 09:47:01 +00:00
|
|
|
solAssert(!!m_currentNode && !m_inlineAssembly, "");
|
|
|
|
|
|
|
|
m_inlineAssembly = &_inlineAssembly;
|
|
|
|
(*this)(_inlineAssembly.operations());
|
|
|
|
m_inlineAssembly = nullptr;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::visit(yul::Statement const& _statement)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
2021-09-22 09:59:59 +00:00
|
|
|
solAssert(nativeLocationOf(_statement) == originLocationOf(_statement), "");
|
2021-09-20 15:51:01 +00:00
|
|
|
m_currentNode->location = langutil::SourceLocation::smallestCovering(m_currentNode->location, nativeLocationOf(_statement));
|
2020-03-05 09:47:01 +00:00
|
|
|
ASTWalker::visit(_statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::If const& _if)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
visit(*_if.condition);
|
|
|
|
|
|
|
|
auto nodes = splitFlow<2>();
|
|
|
|
m_currentNode = nodes[0];
|
|
|
|
(*this)(_if.body);
|
|
|
|
nodes[0] = m_currentNode;
|
|
|
|
mergeFlow(nodes, nodes[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::Switch const& _switch)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
visit(*_switch.expression);
|
|
|
|
|
|
|
|
auto beforeSwitch = m_currentNode;
|
|
|
|
|
|
|
|
auto nodes = splitFlow(_switch.cases.size());
|
|
|
|
for (size_t i = 0u; i < _switch.cases.size(); ++i)
|
|
|
|
{
|
|
|
|
m_currentNode = nodes[i];
|
|
|
|
(*this)(_switch.cases[i].body);
|
|
|
|
nodes[i] = m_currentNode;
|
|
|
|
}
|
|
|
|
mergeFlow(nodes);
|
|
|
|
|
2022-03-15 13:24:42 +00:00
|
|
|
if (!hasDefaultCase(_switch))
|
2020-03-05 09:47:01 +00:00
|
|
|
connect(beforeSwitch, m_currentNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::ForLoop const& _forLoop)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
|
|
|
|
(*this)(_forLoop.pre);
|
|
|
|
|
|
|
|
auto condition = createLabelHere();
|
|
|
|
|
|
|
|
if (_forLoop.condition)
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
|
|
|
|
auto loopExpression = newLabel();
|
|
|
|
auto nodes = splitFlow<2>();
|
|
|
|
auto afterFor = nodes[1];
|
|
|
|
m_currentNode = nodes[0];
|
|
|
|
|
|
|
|
{
|
|
|
|
BreakContinueScope scope(*this, afterFor, loopExpression);
|
|
|
|
(*this)(_forLoop.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
placeAndConnectLabel(loopExpression);
|
|
|
|
|
|
|
|
(*this)(_forLoop.post);
|
|
|
|
|
|
|
|
connect(m_currentNode, condition);
|
|
|
|
m_currentNode = afterFor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::Break const&)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
solAssert(m_breakJump, "");
|
|
|
|
connect(m_currentNode, m_breakJump);
|
|
|
|
m_currentNode = newLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::Continue const&)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
solAssert(m_continueJump, "");
|
|
|
|
connect(m_currentNode, m_continueJump);
|
|
|
|
m_currentNode = newLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
auto const& externalReferences = m_inlineAssembly->annotation().externalReferences;
|
|
|
|
if (externalReferences.count(&_identifier))
|
|
|
|
if (auto const* declaration = dynamic_cast<VariableDeclaration const*>(externalReferences.at(&_identifier).declaration))
|
2021-09-22 09:59:59 +00:00
|
|
|
{
|
|
|
|
solAssert(nativeLocationOf(_identifier) == originLocationOf(_identifier), "");
|
2018-12-07 17:20:35 +00:00
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
2020-03-05 09:47:01 +00:00
|
|
|
*declaration,
|
|
|
|
VariableOccurrence::Kind::Access,
|
2021-09-20 15:51:01 +00:00
|
|
|
nativeLocationOf(_identifier)
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
2021-09-22 09:59:59 +00:00
|
|
|
}
|
2020-03-05 09:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::Assignment const& _assignment)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
visit(*_assignment.value);
|
|
|
|
auto const& externalReferences = m_inlineAssembly->annotation().externalReferences;
|
|
|
|
for (auto const& variable: _assignment.variableNames)
|
|
|
|
if (externalReferences.count(&variable))
|
|
|
|
if (auto const* declaration = dynamic_cast<VariableDeclaration const*>(externalReferences.at(&variable).declaration))
|
2021-09-22 09:59:59 +00:00
|
|
|
{
|
|
|
|
solAssert(nativeLocationOf(variable) == originLocationOf(variable), "");
|
2020-03-05 09:47:01 +00:00
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
|
|
|
*declaration,
|
|
|
|
VariableOccurrence::Kind::Assignment,
|
2021-09-20 15:51:01 +00:00
|
|
|
nativeLocationOf(variable)
|
2020-03-05 09:47:01 +00:00
|
|
|
);
|
2021-09-22 09:59:59 +00:00
|
|
|
}
|
2020-03-05 09:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::FunctionCall const& _functionCall)
|
|
|
|
{
|
|
|
|
using namespace yul;
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
yul::ASTWalker::operator()(_functionCall);
|
|
|
|
|
|
|
|
if (auto const *builtinFunction = m_inlineAssembly->dialect().builtin(_functionCall.functionName.name))
|
2021-09-30 15:01:54 +00:00
|
|
|
{
|
|
|
|
if (builtinFunction->controlFlowSideEffects.canTerminate)
|
|
|
|
connect(m_currentNode, m_transactionReturnNode);
|
|
|
|
if (builtinFunction->controlFlowSideEffects.canRevert)
|
|
|
|
connect(m_currentNode, m_revertNode);
|
|
|
|
if (!builtinFunction->controlFlowSideEffects.canContinue)
|
2020-03-05 09:47:01 +00:00
|
|
|
m_currentNode = newLabel();
|
2021-09-30 15:01:54 +00:00
|
|
|
}
|
2020-03-05 09:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::FunctionDefinition const&)
|
|
|
|
{
|
|
|
|
solAssert(m_currentNode && m_inlineAssembly, "");
|
|
|
|
// External references cannot be accessed from within functions, so we can ignore their control flow.
|
|
|
|
// TODO: we might still want to track if they always revert or return, though.
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::operator()(yul::Leave const&)
|
|
|
|
{
|
|
|
|
// This has to be implemented, if we ever decide to visit functions.
|
2021-09-23 15:18:13 +00:00
|
|
|
solUnimplemented("");
|
2018-12-07 17:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(VariableDeclaration const& _variableDeclaration)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_variableDeclaration);
|
2018-12-07 17:20:35 +00:00
|
|
|
|
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
|
|
|
_variableDeclaration,
|
2020-03-05 09:47:01 +00:00
|
|
|
VariableOccurrence::Kind::Declaration
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Handle declaration with immediate assignment.
|
|
|
|
if (_variableDeclaration.value())
|
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
|
|
|
_variableDeclaration,
|
|
|
|
VariableOccurrence::Kind::Assignment,
|
2020-03-05 09:47:01 +00:00
|
|
|
_variableDeclaration.value()->location()
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
|
|
|
// Function arguments are considered to be immediately assigned as well (they are "externally assigned").
|
2019-09-05 18:02:34 +00:00
|
|
|
else if (_variableDeclaration.isCallableOrCatchParameter() && !_variableDeclaration.isReturnParameter())
|
2018-12-07 17:20:35 +00:00
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
|
|
|
_variableDeclaration,
|
2020-03-05 09:47:01 +00:00
|
|
|
VariableOccurrence::Kind::Assignment
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_variableDeclarationStatement);
|
2018-12-07 17:20:35 +00:00
|
|
|
|
|
|
|
for (auto const& var: _variableDeclarationStatement.declarations())
|
|
|
|
if (var)
|
|
|
|
var->accept(*this);
|
|
|
|
if (_variableDeclarationStatement.initialValue())
|
|
|
|
{
|
|
|
|
_variableDeclarationStatement.initialValue()->accept(*this);
|
|
|
|
for (size_t i = 0; i < _variableDeclarationStatement.declarations().size(); i++)
|
|
|
|
if (auto const& var = _variableDeclarationStatement.declarations()[i])
|
|
|
|
{
|
|
|
|
auto expression = _variableDeclarationStatement.initialValue();
|
|
|
|
if (auto tupleExpression = dynamic_cast<TupleExpression const*>(expression))
|
|
|
|
if (tupleExpression->components().size() > 1)
|
|
|
|
{
|
|
|
|
solAssert(tupleExpression->components().size() > i, "");
|
|
|
|
expression = tupleExpression->components()[i].get();
|
|
|
|
}
|
2022-06-14 12:55:06 +00:00
|
|
|
expression = resolveOuterUnaryTuples(expression);
|
2018-12-07 17:20:35 +00:00
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
|
|
|
*var,
|
|
|
|
VariableOccurrence::Kind::Assignment,
|
2020-03-05 09:47:01 +00:00
|
|
|
expression ? std::make_optional(expression->location()) : std::optional<langutil::SourceLocation>{}
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ControlFlowBuilder::visit(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
2019-01-08 18:33:46 +00:00
|
|
|
visitNode(_identifier);
|
2018-12-07 17:20:35 +00:00
|
|
|
|
|
|
|
if (auto const* variableDeclaration = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
|
|
|
|
m_currentNode->variableOccurrences.emplace_back(
|
|
|
|
*variableDeclaration,
|
2020-04-09 10:48:57 +00:00
|
|
|
static_cast<Expression const&>(_identifier).annotation().willBeWrittenTo ?
|
2018-12-07 17:20:35 +00:00
|
|
|
VariableOccurrence::Kind::Assignment :
|
|
|
|
VariableOccurrence::Kind::Access,
|
2020-03-05 09:47:01 +00:00
|
|
|
_identifier.location()
|
2018-12-07 17:20:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:33:46 +00:00
|
|
|
bool ControlFlowBuilder::visitNode(ASTNode const& _node)
|
|
|
|
{
|
|
|
|
solAssert(!!m_currentNode, "");
|
|
|
|
m_currentNode->location = langutil::SourceLocation::smallestCovering(m_currentNode->location, _node.location());
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-07 17:20:35 +00:00
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
void ControlFlowBuilder::appendControlFlow(ASTNode const& _node)
|
|
|
|
{
|
|
|
|
_node.accept(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGNode* ControlFlowBuilder::createFlow(CFGNode* _entry, ASTNode const& _node)
|
|
|
|
{
|
|
|
|
auto oldCurrentNode = m_currentNode;
|
|
|
|
m_currentNode = _entry;
|
|
|
|
appendControlFlow(_node);
|
|
|
|
auto endNode = m_currentNode;
|
|
|
|
m_currentNode = oldCurrentNode;
|
|
|
|
return endNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::connect(CFGNode* _from, CFGNode* _to)
|
|
|
|
{
|
|
|
|
solAssert(_from, "");
|
|
|
|
solAssert(_to, "");
|
|
|
|
_from->exits.push_back(_to);
|
|
|
|
_to->entries.push_back(_from);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGNode* ControlFlowBuilder::newLabel()
|
|
|
|
{
|
|
|
|
return m_nodeContainer.newNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGNode* ControlFlowBuilder::createLabelHere()
|
|
|
|
{
|
|
|
|
auto label = m_nodeContainer.newNode();
|
|
|
|
connect(m_currentNode, label);
|
|
|
|
m_currentNode = label;
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowBuilder::placeAndConnectLabel(CFGNode* _node)
|
|
|
|
{
|
|
|
|
connect(m_currentNode, _node);
|
|
|
|
m_currentNode = _node;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlFlowBuilder::BreakContinueScope::BreakContinueScope(
|
|
|
|
ControlFlowBuilder& _parser,
|
|
|
|
CFGNode* _breakJump,
|
|
|
|
CFGNode* _continueJump
|
|
|
|
): m_parser(_parser), m_origBreakJump(_parser.m_breakJump), m_origContinueJump(_parser.m_continueJump)
|
|
|
|
{
|
|
|
|
m_parser.m_breakJump = _breakJump;
|
|
|
|
m_parser.m_continueJump = _continueJump;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlFlowBuilder::BreakContinueScope::~BreakContinueScope()
|
|
|
|
{
|
|
|
|
m_parser.m_breakJump = m_origBreakJump;
|
|
|
|
m_parser.m_continueJump = m_origContinueJump;
|
|
|
|
}
|