diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ir/names.h | 64 |
1 files changed, 45 insertions, 19 deletions
diff --git a/src/ir/names.h b/src/ir/names.h index 0cc47d46e..e2a168940 100644 --- a/src/ir/names.h +++ b/src/ir/names.h @@ -44,12 +44,18 @@ inline void ensureNames(Function* func) { // Given a root of a name, finds a valid name with perhaps a number appended // to it, by calling a function to check if a name is valid. -inline Name getValidName(Name root, std::function<bool(Name)> check) { +// +// An optional index can be given as a hint, and if so, the search for a valid +// name will begin there. This can be used to avoid trying the same 0,1,2,.. +// etc. names each time (which could lead to quadratic behavior in certain +// cases). +inline Name +getValidName(Name root, std::function<bool(Name)> check, Index hint = 0) { if (check(root)) { return root; } auto prefixed = std::string(root.str) + '_'; - Index num = 0; + Index num = hint; while (1) { auto name = prefixed + std::to_string(num); if (check(name)) { @@ -60,46 +66,66 @@ inline Name getValidName(Name root, std::function<bool(Name)> check) { } inline Name getValidExportName(Module& module, Name root) { - return getValidName(root, - [&](Name test) { return !module.getExportOrNull(test); }); + return getValidName( + root, + [&](Name test) { return !module.getExportOrNull(test); }, + module.exports.size()); } inline Name getValidGlobalName(Module& module, Name root) { - return getValidName(root, - [&](Name test) { return !module.getGlobalOrNull(test); }); + return getValidName( + root, + [&](Name test) { return !module.getGlobalOrNull(test); }, + module.globals.size()); } inline Name getValidFunctionName(Module& module, Name root) { return getValidName( - root, [&](Name test) { return !module.getFunctionOrNull(test); }); + root, + [&](Name test) { return !module.getFunctionOrNull(test); }, + module.functions.size()); } inline Name getValidTableName(Module& module, Name root) { - return getValidName(root, - [&](Name test) { return !module.getTableOrNull(test); }); + return getValidName( + root, + [&](Name test) { return !module.getTableOrNull(test); }, + module.tables.size()); } inline Name getValidTagName(Module& module, Name root) { - return getValidName(root, - [&](Name test) { return !module.getTagOrNull(test); }); + return getValidName( + root, + [&](Name test) { return !module.getTagOrNull(test); }, + module.tags.size()); } inline Name getValidElementSegmentName(Module& module, Name root) { return getValidName( - root, [&](Name test) { return !module.getElementSegmentOrNull(test); }); + root, + [&](Name test) { return !module.getElementSegmentOrNull(test); }, + module.elementSegments.size()); } inline Name getValidDataSegmentName(Module& module, Name root) { return getValidName( - root, [&](Name test) { return !module.getDataSegmentOrNull(test); }); + root, + [&](Name test) { return !module.getDataSegmentOrNull(test); }, + module.dataSegments.size()); } inline Name getValidMemoryName(Module& module, Name root) { - return getValidName(root, - [&](Name test) { return !module.getMemoryOrNull(test); }); + return getValidName( + root, + [&](Name test) { return !module.getMemoryOrNull(test); }, + module.memories.size()); } inline Name getValidLocalName(Function& func, Name root) { - return getValidName(root, - [&](Name test) { return !func.hasLocalIndex(test); }); + return getValidName( + root, + [&](Name test) { return !func.hasLocalIndex(test); }, + func.getNumLocals()); } template<typename T> inline Name getValidNameGivenExisting(Name root, const T& existingNames) { - return getValidName(root, - [&](Name test) { return !existingNames.count(test); }); + return getValidName( + root, + [&](Name test) { return !existingNames.count(test); }, + existingNames.size()); } class MinifiedNameGenerator { |