diff options
-rw-r--r-- | src/ir/type-updating.cpp | 52 | ||||
-rw-r--r-- | src/ir/type-updating.h | 5 |
2 files changed, 32 insertions, 25 deletions
diff --git a/src/ir/type-updating.cpp b/src/ir/type-updating.cpp index 92ec97980..123b13119 100644 --- a/src/ir/type-updating.cpp +++ b/src/ir/type-updating.cpp @@ -145,31 +145,7 @@ GlobalTypeRewriter::TypeMap GlobalTypeRewriter::rebuildTypes( for (auto [type, index] : typeIndices) { oldToNewTypes[type] = newTypes[index]; } - - // Update type names to avoid duplicates. - std::unordered_set<Name> typeNames; - for (auto& [type, info] : wasm.typeNames) { - typeNames.insert(info.name); - } - for (auto& [old, new_] : oldToNewTypes) { - if (old == new_) { - // The type is being mapped to itself; no need to rename anything. - continue; - } - - if (auto it = wasm.typeNames.find(old); it != wasm.typeNames.end()) { - wasm.typeNames[new_] = wasm.typeNames[old]; - // Use the existing name in the new type, as usually it completely - // replaces the old. Rename the old name in a unique way to avoid - // confusion in the case that it remains used. - auto deduped = - Names::getValidName(wasm.typeNames[old].name, - [&](Name test) { return !typeNames.count(test); }); - wasm.typeNames[old].name = deduped; - typeNames.insert(deduped); - } - } - + mapTypeNames(oldToNewTypes); return oldToNewTypes; } @@ -293,6 +269,32 @@ void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) { } } +void GlobalTypeRewriter::mapTypeNames(const TypeMap& oldToNewTypes) { + // Update type names to avoid duplicates. + std::unordered_set<Name> typeNames; + for (auto& [type, info] : wasm.typeNames) { + typeNames.insert(info.name); + } + for (auto& [old, new_] : oldToNewTypes) { + if (old == new_) { + // The type is being mapped to itself; no need to rename anything. + continue; + } + + if (auto it = wasm.typeNames.find(old); it != wasm.typeNames.end()) { + wasm.typeNames[new_] = wasm.typeNames[old]; + // Use the existing name in the new type, as usually it completely + // replaces the old. Rename the old name in a unique way to avoid + // confusion in the case that it remains used. + auto deduped = + Names::getValidName(wasm.typeNames[old].name, + [&](Name test) { return !typeNames.count(test); }); + wasm.typeNames[old].name = deduped; + typeNames.insert(deduped); + } + } +} + Type GlobalTypeRewriter::getTempType(Type type) { if (type.isBasic()) { return type; diff --git a/src/ir/type-updating.h b/src/ir/type-updating.h index 60b92e585..a8e071fb6 100644 --- a/src/ir/type-updating.h +++ b/src/ir/type-updating.h @@ -369,6 +369,11 @@ public: // not appear, it is mapped to itself. void mapTypes(const TypeMap& oldToNewTypes); + // Users of `mapTypes` may want to update the type names according to their + // mapping. This is not done automatically in `mapTypes` because other users + // may want the names to reflect that types have been replaced. + void mapTypeNames(const TypeMap& oldToNewTypes); + // Subclasses can implement these methods to modify the new set of types that // we map to. By default, we simply copy over the types, and these functions // are the hooks to apply changes through. The methods receive as input the |