Skip to content

Commit a46dbab

Browse files
committed
[NFC] Separate out GlobalTypeRewriter::mapTypeNames
Previously a module's type names were updated in `GlobalTypeRewriter::rebuildTypes`, which builds new versions of the existing types, rather than `GlobalTypeRewriter::mapTypes`, which otherwise handles replacing old types with new types everywhere in a module, but should not necessarily replace names. So that users of `mapTypes` who are building their own versions of existing types can also easily update type names, split type name mapping logic out into a new method `GlobalTypeRewriter::mapTypeNames`.
1 parent d7e09b4 commit a46dbab

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/ir/type-updating.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,31 +145,7 @@ GlobalTypeRewriter::TypeMap GlobalTypeRewriter::rebuildTypes(
145145
for (auto [type, index] : typeIndices) {
146146
oldToNewTypes[type] = newTypes[index];
147147
}
148-
149-
// Update type names to avoid duplicates.
150-
std::unordered_set<Name> typeNames;
151-
for (auto& [type, info] : wasm.typeNames) {
152-
typeNames.insert(info.name);
153-
}
154-
for (auto& [old, new_] : oldToNewTypes) {
155-
if (old == new_) {
156-
// The type is being mapped to itself; no need to rename anything.
157-
continue;
158-
}
159-
160-
if (auto it = wasm.typeNames.find(old); it != wasm.typeNames.end()) {
161-
wasm.typeNames[new_] = wasm.typeNames[old];
162-
// Use the existing name in the new type, as usually it completely
163-
// replaces the old. Rename the old name in a unique way to avoid
164-
// confusion in the case that it remains used.
165-
auto deduped =
166-
Names::getValidName(wasm.typeNames[old].name,
167-
[&](Name test) { return !typeNames.count(test); });
168-
wasm.typeNames[old].name = deduped;
169-
typeNames.insert(deduped);
170-
}
171-
}
172-
148+
mapTypeNames(oldToNewTypes);
173149
return oldToNewTypes;
174150
}
175151

@@ -293,6 +269,32 @@ void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) {
293269
}
294270
}
295271

272+
void GlobalTypeRewriter::mapTypeNames(const TypeMap& oldToNewTypes) {
273+
// Update type names to avoid duplicates.
274+
std::unordered_set<Name> typeNames;
275+
for (auto& [type, info] : wasm.typeNames) {
276+
typeNames.insert(info.name);
277+
}
278+
for (auto& [old, new_] : oldToNewTypes) {
279+
if (old == new_) {
280+
// The type is being mapped to itself; no need to rename anything.
281+
continue;
282+
}
283+
284+
if (auto it = wasm.typeNames.find(old); it != wasm.typeNames.end()) {
285+
wasm.typeNames[new_] = wasm.typeNames[old];
286+
// Use the existing name in the new type, as usually it completely
287+
// replaces the old. Rename the old name in a unique way to avoid
288+
// confusion in the case that it remains used.
289+
auto deduped =
290+
Names::getValidName(wasm.typeNames[old].name,
291+
[&](Name test) { return !typeNames.count(test); });
292+
wasm.typeNames[old].name = deduped;
293+
typeNames.insert(deduped);
294+
}
295+
}
296+
}
297+
296298
Type GlobalTypeRewriter::getTempType(Type type) {
297299
if (type.isBasic()) {
298300
return type;

src/ir/type-updating.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ class GlobalTypeRewriter {
369369
// not appear, it is mapped to itself.
370370
void mapTypes(const TypeMap& oldToNewTypes);
371371

372+
// Users of `mapTypes` may want to update the type names according to their
373+
// mapping. This is not done automatically in `mapTypes` because other users
374+
// may want the names to reflect that types have been replaced.
375+
void mapTypeNames(const TypeMap& oldToNewTypes);
376+
372377
// Subclasses can implement these methods to modify the new set of types that
373378
// we map to. By default, we simply copy over the types, and these functions
374379
// are the hooks to apply changes through. The methods receive as input the

0 commit comments

Comments
 (0)