1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___TYPE_TRAITS_MAKE_SIGNED_H
10#define _LIBCPP___TYPE_TRAITS_MAKE_SIGNED_H
11
12#include <__config>
13#include <__type_traits/apply_cv.h>
14#include <__type_traits/is_enum.h>
15#include <__type_traits/is_integral.h>
16#include <__type_traits/nat.h>
17#include <__type_traits/remove_cv.h>
18#include <__type_traits/type_list.h>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21#  pragma GCC system_header
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26#if __has_builtin(__make_signed)
27
28template <class _Tp>
29using __make_signed_t = __make_signed(_Tp);
30
31#else
32// clang-format off
33typedef __type_list<signed char,
34        __type_list<signed short,
35        __type_list<signed int,
36        __type_list<signed long,
37        __type_list<signed long long,
38#  ifndef _LIBCPP_HAS_NO_INT128
39        __type_list<__int128_t,
40#  endif
41        __nat
42#  ifndef _LIBCPP_HAS_NO_INT128
43        >
44#  endif
45        > > > > > __signed_types;
46// clang-format on
47
48template <class _Tp, bool = is_integral<_Tp>::value || is_enum<_Tp>::value>
49struct __make_signed{};
50
51template <class _Tp>
52struct __make_signed<_Tp, true> {
53  typedef typename __find_first<__signed_types, sizeof(_Tp)>::type type;
54};
55
56// clang-format off
57template <> struct __make_signed<bool,               true> {};
58template <> struct __make_signed<  signed short,     true> {typedef short     type;};
59template <> struct __make_signed<unsigned short,     true> {typedef short     type;};
60template <> struct __make_signed<  signed int,       true> {typedef int       type;};
61template <> struct __make_signed<unsigned int,       true> {typedef int       type;};
62template <> struct __make_signed<  signed long,      true> {typedef long      type;};
63template <> struct __make_signed<unsigned long,      true> {typedef long      type;};
64template <> struct __make_signed<  signed long long, true> {typedef long long type;};
65template <> struct __make_signed<unsigned long long, true> {typedef long long type;};
66#  ifndef _LIBCPP_HAS_NO_INT128
67template <> struct __make_signed<__int128_t,         true> {typedef __int128_t type;};
68template <> struct __make_signed<__uint128_t,        true> {typedef __int128_t type;};
69#  endif
70// clang-format on
71
72template <class _Tp>
73using __make_signed_t = __apply_cv_t<_Tp, typename __make_signed<__remove_cv_t<_Tp> >::type>;
74
75#endif // __has_builtin(__make_signed)
76
77template <class _Tp>
78struct make_signed {
79  using type _LIBCPP_NODEBUG = __make_signed_t<_Tp>;
80};
81
82#if _LIBCPP_STD_VER >= 14
83template <class _Tp>
84using make_signed_t = __make_signed_t<_Tp>;
85#endif
86
87_LIBCPP_END_NAMESPACE_STD
88
89#endif // _LIBCPP___TYPE_TRAITS_MAKE_SIGNED_H
90