1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[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
This commit is contained in:
Scott Linder 2021-04-30 17:15:49 +00:00
parent ac096d1d93
commit babaad1cad
4 changed files with 100 additions and 27 deletions

View File

@ -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

View File

@ -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

View File

@ -55,6 +55,7 @@ add_llvm_unittest(ADTTests
RangeAdapterTest.cpp
SCCIteratorTest.cpp
STLExtrasTest.cpp
STLForwardCompatTest.cpp
ScopeExitTest.cpp
SequenceTest.cpp
SetVectorTest.cpp

View File

@ -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