Sort yul functions by creation time.

This commit is contained in:
chriseth 2021-09-07 17:37:36 +02:00
parent d512f7a4fd
commit c63b768fff
2 changed files with 12 additions and 17 deletions

View File

@ -33,13 +33,8 @@ using namespace solidity::util;
string MultiUseYulFunctionCollector::requestedFunctions()
{
string result;
for (auto const& [name, code]: m_requestedFunctions)
{
solAssert(code != "<<STUB<<", "");
// std::map guarantees ascending order when iterating through its keys.
result += code;
}
string result = move(m_code);
m_code.clear();
m_requestedFunctions.clear();
return result;
}
@ -48,11 +43,11 @@ string MultiUseYulFunctionCollector::createFunction(string const& _name, functio
{
if (!m_requestedFunctions.count(_name))
{
m_requestedFunctions[_name] = "<<STUB<<";
m_requestedFunctions.insert(_name);
string fun = _creator();
solAssert(!fun.empty(), "");
solAssert(fun.find("function " + _name + "(") != string::npos, "Function not properly named.");
m_requestedFunctions[_name] = std::move(fun);
m_code += move(fun);
}
return _name;
}
@ -65,13 +60,13 @@ string MultiUseYulFunctionCollector::createFunction(
solAssert(!_name.empty(), "");
if (!m_requestedFunctions.count(_name))
{
m_requestedFunctions[_name] = "<<STUB<<";
m_requestedFunctions.insert(_name);
vector<string> arguments;
vector<string> returnParameters;
string body = _creator(arguments, returnParameters);
solAssert(!body.empty(), "");
m_requestedFunctions[_name] = Whiskers(R"(
m_code += Whiskers(R"(
function <functionName>(<args>)<?+retParams> -> <retParams></+retParams> {
<body>
}
@ -80,7 +75,7 @@ string MultiUseYulFunctionCollector::createFunction(
("args", joinHumanReadable(arguments))
("retParams", joinHumanReadable(returnParameters))
("body", body)
.render();;
.render();
}
return _name;
}

View File

@ -25,6 +25,7 @@
#include <functional>
#include <map>
#include <string>
#include <set>
namespace solidity::frontend
{
@ -46,9 +47,8 @@ public:
std::function<std::string(std::vector<std::string>&, std::vector<std::string>&)> const& _creator
);
/// @returns concatenation of all generated functions.
/// Guarantees that the order of functions in the generated code is deterministic and
/// platform-independent.
/// @returns concatenation of all generated functions in the order in which they were
/// generated.
/// Clears the internal list, i.e. calling it again will result in an
/// empty return value.
std::string requestedFunctions();
@ -57,8 +57,8 @@ public:
bool contains(std::string const& _name) const { return m_requestedFunctions.count(_name) > 0; }
private:
/// Map from function name to code for a multi-use function.
std::map<std::string, std::string> m_requestedFunctions;
std::set<std::string> m_requestedFunctions;
std::string m_code;
};
}