diff options
author | chriseth <chris@ethereum.org> | 2018-10-16 23:01:13 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2018-10-17 23:21:55 +0800 |
commit | 732d09cef1548e2111c47b6f5b04a54db10a9d41 (patch) | |
tree | 757af93eb855da8de07c99d5429be27a95f78ab1 /libyul/optimiser/NameDispenser.h | |
parent | f2f72ff7eea9a461ae8c71a13428e499c8b91025 (diff) | |
download | dexon-solidity-732d09cef1548e2111c47b6f5b04a54db10a9d41.tar dexon-solidity-732d09cef1548e2111c47b6f5b04a54db10a9d41.tar.gz dexon-solidity-732d09cef1548e2111c47b6f5b04a54db10a9d41.tar.bz2 dexon-solidity-732d09cef1548e2111c47b6f5b04a54db10a9d41.tar.lz dexon-solidity-732d09cef1548e2111c47b6f5b04a54db10a9d41.tar.xz dexon-solidity-732d09cef1548e2111c47b6f5b04a54db10a9d41.tar.zst dexon-solidity-732d09cef1548e2111c47b6f5b04a54db10a9d41.zip |
Limit size of generated names and add convenience constructors.
Diffstat (limited to 'libyul/optimiser/NameDispenser.h')
-rw-r--r-- | libyul/optimiser/NameDispenser.h | 26 |
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; }; |