Skip to content

Commit 64facd4

Browse files
authored
Merge pull request #15253 from ethereum/const_function_call_finder
Yul: Add const version of FunctionCallFinder
2 parents e9d4239 + 5468686 commit 64facd4

File tree

6 files changed

+49
-28
lines changed

6 files changed

+49
-28
lines changed

libyul/backends/evm/EVMObjectCompiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ void EVMObjectCompiler::run(Object& _object, bool _optimize)
9090
);
9191
if (!stackErrors.empty())
9292
{
93-
std::vector<FunctionCall*> memoryGuardCalls = FunctionCallFinder::run(
94-
*_object.code,
93+
std::vector<FunctionCall const*> memoryGuardCalls = findFunctionCalls(
94+
std::as_const(*_object.code),
9595
"memoryguard"_yulstring
9696
);
9797
auto stackError = stackErrors.front();

libyul/optimiser/FullInliner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ FullInliner::FullInliner(Block& _ast, NameDispenser& _dispenser, Dialect const&
8383
}
8484

8585
// Check for memory guard.
86-
std::vector<FunctionCall*> memoryGuardCalls = FunctionCallFinder::run(
86+
std::vector<FunctionCall*> memoryGuardCalls = findFunctionCalls(
8787
_ast,
8888
"memoryguard"_yulstring
8989
);

libyul/optimiser/FunctionCallFinder.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,46 @@
1616
*/
1717

1818
#include <libyul/optimiser/FunctionCallFinder.h>
19+
20+
#include <libyul/optimiser/ASTWalker.h>
1921
#include <libyul/AST.h>
2022

2123
using namespace solidity;
2224
using namespace solidity::yul;
2325

24-
std::vector<FunctionCall*> FunctionCallFinder::run(Block& _block, YulString _functionName)
26+
namespace
27+
{
28+
template<typename Base, typename ResultType>
29+
class MaybeConstFunctionCallFinder: Base
2530
{
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+
};
2951
}
3052

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+
}
3257

33-
void FunctionCallFinder::operator()(FunctionCall& _functionCall)
58+
std::vector<FunctionCall const*> solidity::yul::findFunctionCalls(Block const& _block, YulString _functionName)
3459
{
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);
3861
}

libyul/optimiser/FunctionCallFinder.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,26 @@
2020

2121
#pragma once
2222

23-
#include <libyul/optimiser/ASTWalker.h>
23+
#include <libyul/ASTForward.h>
24+
#include <libyul/YulString.h>
2425

2526
#include <vector>
2627

2728
namespace solidity::yul
2829
{
2930

3031
/**
31-
* AST walker that finds all calls to a function of a given name.
32+
* Finds all calls to a function of a given name using an ASTModifier.
3233
*
3334
* Prerequisite: Disambiguator
3435
*/
35-
class FunctionCallFinder: ASTModifier
36-
{
37-
public:
38-
static std::vector<FunctionCall*> run(Block& _block, YulString _functionName);
39-
private:
40-
FunctionCallFinder(YulString _functionName);
41-
using ASTModifier::operator();
42-
void operator()(FunctionCall& _functionCall) override;
43-
YulString m_functionName;
44-
std::vector<FunctionCall*> m_calls;
45-
};
36+
std::vector<FunctionCall*> findFunctionCalls(Block& _block, YulString _functionName);
37+
38+
/**
39+
* Finds all calls to a function of a given name using an ASTWalker.
40+
*
41+
* Prerequisite: Disambiguator
42+
*/
43+
std::vector<FunctionCall const*> findFunctionCalls(Block const& _block, YulString _functionName);
4644

4745
}

libyul/optimiser/StackLimitEvader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void StackLimitEvader::run(
174174
"StackLimitEvader can only be run on objects using the EVMDialect with object access."
175175
);
176176

177-
std::vector<FunctionCall*> memoryGuardCalls = FunctionCallFinder::run(
177+
std::vector<FunctionCall*> memoryGuardCalls = findFunctionCalls(
178178
*_object.code,
179179
"memoryguard"_yulstring
180180
);
@@ -206,7 +206,7 @@ void StackLimitEvader::run(
206206
StackToMemoryMover::run(_context, reservedMemory, memoryOffsetAllocator.slotAllocations, requiredSlots, *_object.code);
207207

208208
reservedMemory += 32 * requiredSlots;
209-
for (FunctionCall* memoryGuardCall: FunctionCallFinder::run(*_object.code, "memoryguard"_yulstring))
209+
for (FunctionCall* memoryGuardCall: findFunctionCalls(*_object.code, "memoryguard"_yulstring))
210210
{
211211
Literal* literal = std::get_if<Literal>(&memoryGuardCall->arguments.front());
212212
yulAssert(literal && literal->kind == LiteralKind::Number, "");

test/libsolidity/MemoryGuardTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ TestCase::TestResult MemoryGuardTest::run(std::ostream& _stream, std::string con
7373
}
7474

7575
auto handleObject = [&](std::string const& _kind, Object const& _object) {
76-
m_obtainedResult += contractName + "(" + _kind + ") " + (FunctionCallFinder::run(
76+
m_obtainedResult += contractName + "(" + _kind + ") " + (findFunctionCalls(
7777
*_object.code,
7878
"memoryguard"_yulstring
7979
).empty() ? "false" : "true") + "\n";

0 commit comments

Comments
 (0)