1// -*- C++ -*-
2//===--------------------------- cstddef ----------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_CSTDDEF
11#define _LIBCPP_CSTDDEF
12
13/*
14    cstddef synopsis
15
16Macros:
17
18    offsetof(type,member-designator)
19    NULL
20
21namespace std
22{
23
24Types:
25
26    ptrdiff_t
27    size_t
28    max_align_t // C++11
29    nullptr_t
30    byte // C++17
31
32}  // std
33
34*/
35
36#include <__config>
37#include <version>
38
39#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
40#pragma GCC system_header
41#endif
42
43// Don't include our own <stddef.h>; we don't want to declare ::nullptr_t.
44#include_next <stddef.h>
45#include <__nullptr>
46
47_LIBCPP_BEGIN_NAMESPACE_STD
48
49using ::ptrdiff_t;
50using ::size_t;
51
52#if !defined(_LIBCPP_CXX03_LANG)
53using ::max_align_t;
54#endif
55
56template <class _Tp> struct __libcpp_is_integral                     { enum { value = 0 }; };
57template <>          struct __libcpp_is_integral<bool>               { enum { value = 1 }; };
58template <>          struct __libcpp_is_integral<char>               { enum { value = 1 }; };
59template <>          struct __libcpp_is_integral<signed char>        { enum { value = 1 }; };
60template <>          struct __libcpp_is_integral<unsigned char>      { enum { value = 1 }; };
61template <>          struct __libcpp_is_integral<wchar_t>            { enum { value = 1 }; };
62#ifndef _LIBCPP_NO_HAS_CHAR8_T
63template <>          struct __libcpp_is_integral<char8_t>            { enum { value = 1 }; };
64#endif
65#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
66template <>          struct __libcpp_is_integral<char16_t>           { enum { value = 1 }; };
67template <>          struct __libcpp_is_integral<char32_t>           { enum { value = 1 }; };
68#endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
69template <>          struct __libcpp_is_integral<short>              { enum { value = 1 }; };
70template <>          struct __libcpp_is_integral<unsigned short>     { enum { value = 1 }; };
71template <>          struct __libcpp_is_integral<int>                { enum { value = 1 }; };
72template <>          struct __libcpp_is_integral<unsigned int>       { enum { value = 1 }; };
73template <>          struct __libcpp_is_integral<long>               { enum { value = 1 }; };
74template <>          struct __libcpp_is_integral<unsigned long>      { enum { value = 1 }; };
75template <>          struct __libcpp_is_integral<long long>          { enum { value = 1 }; };
76template <>          struct __libcpp_is_integral<unsigned long long> { enum { value = 1 }; };
77#ifndef _LIBCPP_HAS_NO_INT128
78template <>          struct __libcpp_is_integral<__int128_t>         { enum { value = 1 }; };
79template <>          struct __libcpp_is_integral<__uint128_t>        { enum { value = 1 }; };
80#endif
81
82_LIBCPP_END_NAMESPACE_STD
83
84#if _LIBCPP_STD_VER > 14
85namespace std  // purposefully not versioned
86{
87enum class byte : unsigned char {};
88
89
90template <bool> struct __enable_if_integral_imp {};
91template <> struct __enable_if_integral_imp<true> { using type = byte; };
92template <class _Tp> using _EnableByteOverload = typename __enable_if_integral_imp<__libcpp_is_integral<_Tp>::value>::type;
93
94constexpr byte  operator| (byte  __lhs, byte __rhs) noexcept
95{
96    return static_cast<byte>(
97      static_cast<unsigned char>(
98         static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)
99    ));
100}
101
102constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept
103{ return __lhs = __lhs | __rhs; }
104
105constexpr byte  operator& (byte  __lhs, byte __rhs) noexcept
106{
107    return static_cast<byte>(
108      static_cast<unsigned char>(
109         static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)
110    ));
111}
112
113constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept
114{ return __lhs = __lhs & __rhs; }
115
116constexpr byte  operator^ (byte  __lhs, byte __rhs) noexcept
117{
118    return static_cast<byte>(
119      static_cast<unsigned char>(
120         static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)
121    ));
122}
123
124constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept
125{ return __lhs = __lhs ^ __rhs; }
126
127constexpr byte  operator~ (byte __b) noexcept
128{
129    return static_cast<byte>(
130      static_cast<unsigned char>(
131        ~static_cast<unsigned int>(__b)
132    ));
133}
134template <class _Integer>
135  constexpr _EnableByteOverload<_Integer> &
136  operator<<=(byte& __lhs, _Integer __shift) noexcept
137  { return __lhs = __lhs << __shift; }
138
139template <class _Integer>
140  constexpr _EnableByteOverload<_Integer>
141  operator<< (byte  __lhs, _Integer __shift) noexcept
142  { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift)); }
143
144template <class _Integer>
145  constexpr _EnableByteOverload<_Integer> &
146  operator>>=(byte& __lhs, _Integer __shift) noexcept
147  { return __lhs = __lhs >> __shift; }
148
149template <class _Integer>
150  constexpr _EnableByteOverload<_Integer>
151  operator>> (byte  __lhs, _Integer __shift) noexcept
152  { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift)); }
153
154template <class _Integer, class = _EnableByteOverload<_Integer> >
155  constexpr _Integer
156  to_integer(byte __b) noexcept { return static_cast<_Integer>(__b); }
157}
158
159#endif
160
161#endif  // _LIBCPP_CSTDDEF
162