[llvm] c6f20d7 - [ADT] Add STLForwardCompat.h and llvm::disjunction

Scott Linder via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 30 10:30:01 PDT 2021


Author: Scott Linder
Date: 2021-04-30T17:28:47Z
New Revision: c6f20d70a8c93ab3d50c9777c477fe040460a664

URL: https://github.com/llvm/llvm-project/commit/c6f20d70a8c93ab3d50c9777c477fe040460a664
DIFF: https://github.com/llvm/llvm-project/commit/c6f20d70a8c93ab3d50c9777c477fe040460a664.diff

LOG: [ADT] Add STLForwardCompat.h and llvm::disjunction

Move some types in STLExtras.h which are named and behave identically to
STL types from future standards into a dedicated header. This keeps them
organized (they are not "extras" in the same sense as most types in
STLExtras.h are) and fixes circular dependencies in future patches.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D100668

Added: 
    llvm/include/llvm/ADT/STLForwardCompat.h
    llvm/unittests/ADT/STLForwardCompatTest.cpp

Modified: 
    llvm/include/llvm/ADT/STLExtras.h
    llvm/unittests/ADT/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 208156ceaaf61..04cdfebc77d99 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -17,6 +17,7 @@
 #define LLVM_ADT_STLEXTRAS_H
 
 #include "llvm/ADT/Optional.h"
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/iterator.h"
 #include "llvm/ADT/iterator_range.h"
 #include "llvm/Config/abi-breaking.h"
@@ -60,15 +61,6 @@ using ValueOfRange = typename std::remove_reference<decltype(
 //     Extra additions to <type_traits>
 //===----------------------------------------------------------------------===//
 
-template <typename T>
-struct negation : std::integral_constant<bool, !bool(T::value)> {};
-
-template <typename...> struct conjunction : std::true_type {};
-template <typename B1> struct conjunction<B1> : B1 {};
-template <typename B1, typename... Bn>
-struct conjunction<B1, Bn...>
-    : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
-
 template <typename T> struct make_const_ptr {
   using type =
       typename std::add_pointer<typename std::add_const<T>::type>::type;
@@ -1300,27 +1292,13 @@ template <> struct rank<0> {};
 
 /// traits class for checking whether type T is one of any of the given
 /// types in the variadic list.
-template <typename T, typename... Ts> struct is_one_of {
-  static const bool value = false;
-};
-
-template <typename T, typename U, typename... Ts>
-struct is_one_of<T, U, Ts...> {
-  static const bool value =
-      std::is_same<T, U>::value || is_one_of<T, Ts...>::value;
-};
+template <typename T, typename... Ts>
+using is_one_of = disjunction<std::is_same<T, Ts>...>;
 
 /// traits class for checking whether type T is a base class for all
 ///  the given types in the variadic list.
-template <typename T, typename... Ts> struct are_base_of {
-  static const bool value = true;
-};
-
-template <typename T, typename U, typename... Ts>
-struct are_base_of<T, U, Ts...> {
-  static const bool value =
-      std::is_base_of<T, U>::value && are_base_of<T, Ts...>::value;
-};
+template <typename T, typename... Ts>
+using are_base_of = conjunction<std::is_base_of<T, Ts>...>;
 
 //===----------------------------------------------------------------------===//
 //     Extra additions for arrays

diff  --git a/llvm/include/llvm/ADT/STLForwardCompat.h b/llvm/include/llvm/ADT/STLForwardCompat.h
new file mode 100644
index 0000000000000..40176e72ab1bc
--- /dev/null
+++ b/llvm/include/llvm/ADT/STLForwardCompat.h
@@ -0,0 +1,49 @@
+//===- STLForwardCompat.h - Library features from future STLs ------C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains library features backported from future STL versions.
+//
+// These should be replaced with their STL counterparts as the C++ version LLVM
+// is compiled with is updated.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_STLFORWARDCOMPAT_H
+#define LLVM_ADT_STLFORWARDCOMPAT_H
+
+#include <type_traits>
+
+namespace llvm {
+
+//===----------------------------------------------------------------------===//
+//     Features from C++17
+//===----------------------------------------------------------------------===//
+
+template <typename T>
+struct negation // NOLINT(readability-identifier-naming)
+    : std::integral_constant<bool, !bool(T::value)> {};
+
+template <typename...>
+struct conjunction // NOLINT(readability-identifier-naming)
+    : std::true_type {};
+template <typename B1> struct conjunction<B1> : B1 {};
+template <typename B1, typename... Bn>
+struct conjunction<B1, Bn...>
+    : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
+
+template <typename...>
+struct disjunction // NOLINT(readability-identifier-naming)
+    : std::false_type {};
+template <typename B1> struct disjunction<B1> : B1 {};
+template <typename B1, typename... Bn>
+struct disjunction<B1, Bn...>
+    : std::conditional<bool(B1::value), B1, disjunction<Bn...>>::type {};
+
+} // namespace llvm
+
+#endif // LLVM_ADT_STLFORWARDCOMPAT_H

diff  --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt
index 51f1b114f5cb0..e64b1a0c7b3e6 100644
--- a/llvm/unittests/ADT/CMakeLists.txt
+++ b/llvm/unittests/ADT/CMakeLists.txt
@@ -55,6 +55,7 @@ add_llvm_unittest(ADTTests
   RangeAdapterTest.cpp
   SCCIteratorTest.cpp
   STLExtrasTest.cpp
+  STLForwardCompatTest.cpp
   ScopeExitTest.cpp
   SequenceTest.cpp
   SetVectorTest.cpp

diff  --git a/llvm/unittests/ADT/STLForwardCompatTest.cpp b/llvm/unittests/ADT/STLForwardCompatTest.cpp
new file mode 100644
index 0000000000000..ce7ecba02c7be
--- /dev/null
+++ b/llvm/unittests/ADT/STLForwardCompatTest.cpp
@@ -0,0 +1,45 @@
+//===- STLForwardCompatTest.cpp - Unit tests for STLForwardCompat ---------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/STLForwardCompat.h"
+#include "gtest/gtest.h"
+
+namespace {
+
+TEST(STLForwardCompatTest, NegationTest) {
+  EXPECT_TRUE((llvm::negation<std::false_type>::value));
+  EXPECT_FALSE((llvm::negation<std::true_type>::value));
+}
+
+struct incomplete_type;
+
+TEST(STLForwardCompatTest, ConjunctionTest) {
+  EXPECT_TRUE((llvm::conjunction<>::value));
+  EXPECT_FALSE((llvm::conjunction<std::false_type>::value));
+  EXPECT_TRUE((llvm::conjunction<std::true_type>::value));
+  EXPECT_FALSE((llvm::conjunction<std::false_type, incomplete_type>::value));
+  EXPECT_FALSE((llvm::conjunction<std::false_type, std::true_type>::value));
+  EXPECT_FALSE((llvm::conjunction<std::true_type, std::false_type>::value));
+  EXPECT_TRUE((llvm::conjunction<std::true_type, std::true_type>::value));
+  EXPECT_TRUE((llvm::conjunction<std::true_type, std::true_type,
+                                 std::true_type>::value));
+}
+
+TEST(STLForwardCompatTest, DisjunctionTest) {
+  EXPECT_FALSE((llvm::disjunction<>::value));
+  EXPECT_FALSE((llvm::disjunction<std::false_type>::value));
+  EXPECT_TRUE((llvm::disjunction<std::true_type>::value));
+  EXPECT_TRUE((llvm::disjunction<std::true_type, incomplete_type>::value));
+  EXPECT_TRUE((llvm::disjunction<std::false_type, std::true_type>::value));
+  EXPECT_TRUE((llvm::disjunction<std::true_type, std::false_type>::value));
+  EXPECT_TRUE((llvm::disjunction<std::true_type, std::true_type>::value));
+  EXPECT_TRUE((llvm::disjunction<std::true_type, std::true_type,
+                                 std::true_type>::value));
+}
+
+} // namespace


        


More information about the llvm-commits mailing list