summaryrefslogtreecommitdiff
path: root/src/wasm-type-printing.h
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2023-01-18 13:31:26 -0600
committerGitHub <noreply@github.com>2023-01-18 11:31:26 -0800
commit82bd2c4e5717cbe19a1a6c34cfdd5116b13a68dc (patch)
treec801db56ab7288144c0fd288cb57dec0023a9569 /src/wasm-type-printing.h
parentab5b3e455770fec0e9ac78e566b48ec5a873a7ca (diff)
downloadbinaryen-82bd2c4e5717cbe19a1a6c34cfdd5116b13a68dc.tar.gz
binaryen-82bd2c4e5717cbe19a1a6c34cfdd5116b13a68dc.tar.bz2
binaryen-82bd2c4e5717cbe19a1a6c34cfdd5116b13a68dc.zip
Add a TypeNameGenerator that uses names from a Module (#5437)
If the module does not have a name for a particular type, the new utility falls back to use a different user-configurable type name generator, just like the existing IndexedTypeNameGenerator does. Also change how heap types are printed by this printing machinery (which is currently only used for debugging) so that their names are printed in addition to their contents. This makes the printer much more useful for debugging.
Diffstat (limited to 'src/wasm-type-printing.h')
-rw-r--r--src/wasm-type-printing.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/wasm-type-printing.h b/src/wasm-type-printing.h
index 685d488fd..3a8740350 100644
--- a/src/wasm-type-printing.h
+++ b/src/wasm-type-printing.h
@@ -24,6 +24,7 @@
#include "support/name.h"
#include "support/utilities.h"
#include "wasm-type.h"
+#include "wasm.h"
namespace wasm {
@@ -93,6 +94,33 @@ struct IndexedTypeNameGenerator
}
};
+// Prints heap types stored in a module, falling back to the given
+// FallbackGenerator if the module does not have a name for type type.
+template<typename FallbackGenerator = DefaultTypeNameGenerator>
+struct ModuleTypeNameGenerator
+ : TypeNameGeneratorBase<ModuleTypeNameGenerator<FallbackGenerator>> {
+ const Module& wasm;
+ DefaultTypeNameGenerator defaultGenerator;
+ FallbackGenerator& fallback;
+
+ ModuleTypeNameGenerator(const Module& wasm, FallbackGenerator& fallback)
+ : wasm(wasm), fallback(fallback) {}
+
+ // TODO: Use C++20 `requires` to clean this up.
+ template<class T = FallbackGenerator>
+ ModuleTypeNameGenerator(
+ const Module& wasm,
+ std::enable_if_t<std::is_same_v<T, DefaultTypeNameGenerator>>* = nullptr)
+ : ModuleTypeNameGenerator(wasm, defaultGenerator) {}
+
+ TypeNames getNames(HeapType type) {
+ if (auto it = wasm.typeNames.find(type); it != wasm.typeNames.end()) {
+ return it->second;
+ }
+ return fallback.getNames(type);
+ }
+};
+
} // namespace wasm
#endif // wasm_wasm_type_printing_h