|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | #include <libyul/optimiser/FunctionCallFinder.h> |
| 19 | + |
| 20 | +#include <libyul/optimiser/ASTWalker.h> |
19 | 21 | #include <libyul/AST.h> |
20 | 22 |
|
21 | 23 | using namespace solidity; |
22 | 24 | using namespace solidity::yul; |
23 | 25 |
|
24 | | -std::vector<FunctionCall*> FunctionCallFinder::run(Block& _block, YulString _functionName) |
| 26 | +namespace |
| 27 | +{ |
| 28 | +template<typename Base, typename ResultType> |
| 29 | +class MaybeConstFunctionCallFinder: Base |
25 | 30 | { |
26 | | - FunctionCallFinder functionCallFinder(_functionName); |
27 | | - functionCallFinder(_block); |
28 | | - return functionCallFinder.m_calls; |
| 31 | +public: |
| 32 | + using MaybeConstBlock = std::conditional_t<std::is_const_v<ResultType>, Block const, Block>; |
| 33 | + static std::vector<ResultType*> run(MaybeConstBlock& _block, YulString const _functionName) |
| 34 | + { |
| 35 | + MaybeConstFunctionCallFinder functionCallFinder(_functionName); |
| 36 | + functionCallFinder(_block); |
| 37 | + return functionCallFinder.m_calls; |
| 38 | + } |
| 39 | +private: |
| 40 | + explicit MaybeConstFunctionCallFinder(YulString const _functionName): m_functionName(_functionName), m_calls() {} |
| 41 | + using Base::operator(); |
| 42 | + void operator()(ResultType& _functionCall) override |
| 43 | + { |
| 44 | + Base::operator()(_functionCall); |
| 45 | + if (_functionCall.functionName.name == m_functionName) |
| 46 | + m_calls.emplace_back(&_functionCall); |
| 47 | + } |
| 48 | + YulString m_functionName; |
| 49 | + std::vector<ResultType*> m_calls; |
| 50 | +}; |
29 | 51 | } |
30 | 52 |
|
31 | | -FunctionCallFinder::FunctionCallFinder(YulString _functionName): m_functionName(_functionName) {} |
| 53 | +std::vector<FunctionCall*> solidity::yul::findFunctionCalls(Block& _block, YulString _functionName) |
| 54 | +{ |
| 55 | + return MaybeConstFunctionCallFinder<ASTModifier, FunctionCall>::run(_block, _functionName); |
| 56 | +} |
32 | 57 |
|
33 | | -void FunctionCallFinder::operator()(FunctionCall& _functionCall) |
| 58 | +std::vector<FunctionCall const*> solidity::yul::findFunctionCalls(Block const& _block, YulString _functionName) |
34 | 59 | { |
35 | | - ASTModifier::operator()(_functionCall); |
36 | | - if (_functionCall.functionName.name == m_functionName) |
37 | | - m_calls.emplace_back(&_functionCall); |
| 60 | + return MaybeConstFunctionCallFinder<ASTWalker, FunctionCall const>::run(_block, _functionName); |
38 | 61 | } |
0 commit comments