From d662d732ff471ea40eacc68cf62391acf27eec4e Mon Sep 17 00:00:00 2001
From: Jérôme Vouillon <jerome.vouillon@gmail.com>
Date: Thu, 11 Apr 2024 17:15:59 -0400
Subject: Fixes regarding explicit names (#6466)

- Only write explicit function names.
- When merging modules, the name of types, globals and tags in all
  modules but the first were lost.
- Set name as explicit when copying a function with a new name.
---
 src/ir/module-utils.cpp | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

(limited to 'src/ir/module-utils.cpp')

diff --git a/src/ir/module-utils.cpp b/src/ir/module-utils.cpp
index fdee4f869..8d9e88d18 100644
--- a/src/ir/module-utils.cpp
+++ b/src/ir/module-utils.cpp
@@ -48,6 +48,7 @@ Function* copyFunction(Function* func,
                        std::optional<std::vector<Index>> fileIndexMap) {
   auto ret = std::make_unique<Function>();
   ret->name = newName.is() ? newName : func->name;
+  ret->hasExplicitName = func->hasExplicitName;
   ret->type = func->type;
   ret->vars = func->vars;
   ret->localNames = func->localNames;
@@ -77,6 +78,7 @@ Function* copyFunction(Function* func,
 Global* copyGlobal(Global* global, Module& out) {
   auto* ret = new Global();
   ret->name = global->name;
+  ret->hasExplicitName = global->hasExplicitName;
   ret->type = global->type;
   ret->mutable_ = global->mutable_;
   ret->module = global->module;
@@ -93,6 +95,7 @@ Global* copyGlobal(Global* global, Module& out) {
 Tag* copyTag(Tag* tag, Module& out) {
   auto* ret = new Tag();
   ret->name = tag->name;
+  ret->hasExplicitName = tag->hasExplicitName;
   ret->sig = tag->sig;
   ret->module = tag->module;
   ret->base = tag->base;
@@ -209,8 +212,17 @@ void copyModuleItems(const Module& in, Module& out) {
   for (auto& curr : in.dataSegments) {
     copyDataSegment(curr.get(), out);
   }
+
+  for (auto& [type, names] : in.typeNames) {
+    if (!out.typeNames.count(type)) {
+      out.typeNames[type] = names;
+    }
+  }
 }
 
+// TODO: merge this with copyModuleItems, and add options for copying
+// exports and other things that are currently different between them,
+// if we still need those differences.
 void copyModule(const Module& in, Module& out) {
   // we use names throughout, not raw pointers, so simple copying is fine
   // for everything *but* expressions
@@ -222,7 +234,6 @@ void copyModule(const Module& in, Module& out) {
   out.customSections = in.customSections;
   out.debugInfoFileNames = in.debugInfoFileNames;
   out.features = in.features;
-  out.typeNames = in.typeNames;
 }
 
 void clearModule(Module& wasm) {
-- 
cgit v1.2.3