aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/optimiser/NameDispenser.h
diff options
context:
space:
mode:
Diffstat (limited to 'libyul/optimiser/NameDispenser.h')
-rw-r--r--libyul/optimiser/NameDispenser.h26
1 files changed, 24 insertions, 2 deletions
diff --git a/libyul/optimiser/NameDispenser.h b/libyul/optimiser/NameDispenser.h
index 64ec318f..5fbf5f8e 100644
--- a/libyul/optimiser/NameDispenser.h
+++ b/libyul/optimiser/NameDispenser.h
@@ -19,6 +19,8 @@
*/
#pragma once
+#include <libyul/ASTDataForward.h>
+
#include <set>
#include <string>
@@ -27,9 +29,29 @@ namespace dev
namespace yul
{
-struct NameDispenser
+/**
+ * Optimizer component that can be used to generate new names that
+ * do not conflict with existing names.
+ *
+ * Tries to keep names short and appends decimals to disambiguate.
+ */
+class NameDispenser
{
- std::string newName(std::string const& _prefix);
+public:
+ /// Initialize the name dispenser with all the names used in the given AST.
+ explicit NameDispenser(Block const& _ast);
+ /// Initialize the name dispenser with the given used names.
+ explicit NameDispenser(std::set<std::string> _usedNames);
+
+ /// @returns a currently unused name that should be similar to _nameHint
+ /// and prefixed by _context if present.
+ /// If the resulting name would be too long, trims the context at the end
+ /// and the name hint at the start.
+ std::string newName(std::string const& _nameHint, std::string const& _context = {});
+
+private:
+ std::string newNameInternal(std::string const& _nameHint);
+
std::set<std::string> m_usedNames;
};