[llvm] [Support] Refactor IsResizableBase with "constexpr if" (NFC) (PR #157326)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 7 00:00:24 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/157326
We have two implementations of IsResizableBase that are selected with
a boolean template parameter. This patch consolidates them into one
with "constexpr if". The "constexpr if" condition uses
llvm::is_detected to check the availability of resize().
>From 0b4c1b852ba954336e538bf401296414a91ef255 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 4 Sep 2025 22:55:01 -0700
Subject: [PATCH] [Support] Refactor IsResizableBase with "constexpr if" (NFC)
We have two implementations of IsResizableBase that are selected with
a boolean template parameter. This patch consolidates them into one
with "constexpr if". The "constexpr if" condition uses
llvm::is_detected to check the availability of resize().
---
llvm/include/llvm/Support/YAMLTraits.h | 34 +++++++++-----------------
1 file changed, 12 insertions(+), 22 deletions(-)
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index 00f645545a2be..27af2d60c837f 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -1850,39 +1850,29 @@ template <> struct IsFlowSequenceBase<true> {
static const bool flow = true;
};
-template <typename T, typename U = void>
-struct IsResizable : std::false_type {};
-
template <typename T>
-struct IsResizable<T, std::void_t<decltype(std::declval<T>().resize(0))>>
- : public std::true_type {};
-
-template <typename T, bool B> struct IsResizableBase {
- using type = typename T::value_type;
-
- static type &element(IO &io, T &seq, size_t index) {
- if (index >= seq.size())
- seq.resize(index + 1);
- return seq[index];
- }
-};
+using check_resize_t = decltype(std::declval<T>().resize(0));
-template <typename T> struct IsResizableBase<T, false> {
+template <typename T> struct IsResizableBase {
using type = typename T::value_type;
static type &element(IO &io, T &seq, size_t index) {
- if (index >= seq.size()) {
- io.setError(Twine("value sequence extends beyond static size (") +
- Twine(seq.size()) + ")");
- return seq[0];
+ if constexpr (is_detected<check_resize_t, T>::value) {
+ if (index >= seq.size())
+ seq.resize(index + 1);
+ } else {
+ if (index >= seq.size()) {
+ io.setError(Twine("value sequence extends beyond static size (") +
+ Twine(seq.size()) + ")");
+ return seq[0];
+ }
}
return seq[index];
}
};
template <typename T, bool Flow>
-struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>,
- IsResizableBase<T, IsResizable<T>::value> {
+struct SequenceTraitsImpl : IsFlowSequenceBase<Flow>, IsResizableBase<T> {
static size_t size(IO &io, T &seq) { return seq.size(); }
};
More information about the llvm-commits
mailing list