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

View File

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