Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use C++20 requires clauses in TypeNameGeneratorBase
This addresses the TODOs in src/wasm-type-printing.h by utilizing C++20
concepts and requires clauses.

- Replaced the manual SFINAE/macro-based check in TypeNameGeneratorBase
  with a static_assert(requires { ... }) to ensure subclasses implement
  getNames correctly. This is cleaner and more robust.
- Updated the ModuleTypeNameGenerator constructor to use a requires
  clause instead of std::enable_if_t for its default constructor,
  improving readability.
- Added #include <concepts> as required.
  • Loading branch information
sbc100 committed Mar 30, 2026
commit f30fd81d8cf72ce93a901bc1d10026d5392145ef
24 changes: 6 additions & 18 deletions src/wasm-type-printing.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef wasm_wasm_type_printing_h
#define wasm_wasm_type_printing_h

#include <concepts>
#include <cstddef>
#include <iostream>
#include <unordered_map>
Expand All @@ -34,9 +35,6 @@ namespace wasm {
template<typename Subclass> struct TypeNameGeneratorBase {
TypeNameGeneratorBase() { assertValidUsage(); }

TypeNames getNames(HeapType type) {
WASM_UNREACHABLE("Derived class must implement getNames");
}
HeapType::Printed operator()(HeapType type) {
return type.print(
[&](HeapType ht) { return static_cast<Subclass*>(this)->getNames(ht); });
Expand All @@ -48,16 +46,9 @@ template<typename Subclass> struct TypeNameGeneratorBase {

private:
constexpr void assertValidUsage() {
// This check current causes a crash on MSVC
// TODO: Convert to C++20 requires check
#if !defined(_MSC_VER) && (!defined(__GNUC__) || __GNUC__ >= 14)
// Check that the subclass provides `getNames` with the correct type.
using Self = TypeNameGeneratorBase<Subclass>;
static_assert(
static_cast<TypeNames (Self::*)(HeapType)>(&Self::getNames) !=
static_cast<TypeNames (Self::*)(HeapType)>(&Subclass::getNames),
"Derived class must implement getNames");
#endif
static_assert(requires(Subclass& s, HeapType ht) {
{ s.getNames(ht) } -> std::same_as<TypeNames>;
}, "Derived class must implement getNames");
}
};

Expand Down Expand Up @@ -123,11 +114,8 @@ struct ModuleTypeNameGenerator
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(const Module& wasm)
requires std::is_same_v<FallbackGenerator, DefaultTypeNameGenerator>
: ModuleTypeNameGenerator(wasm, defaultGenerator) {}

TypeNames getNames(HeapType type) {
Expand Down
Loading