xref: /freebsd/contrib/llvm-project/libcxx/include/cstddef (revision 61b95bcb42993b24633b280791438266d78f2747)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
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 <__assert> // all public C++ headers provide the assertion handler
37#include <__config>
38#include <__type_traits/enable_if.h>
39#include <__type_traits/integral_constant.h>
40#include <__type_traits/is_integral.h>
41#include <stddef.h>
42#include <version>
43
44#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45#  pragma GCC system_header
46#endif
47
48_LIBCPP_BEGIN_NAMESPACE_STD
49
50using ::nullptr_t;
51using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;
52using ::size_t _LIBCPP_USING_IF_EXISTS;
53
54#if !defined(_LIBCPP_CXX03_LANG)
55using ::max_align_t _LIBCPP_USING_IF_EXISTS;
56#endif
57
58_LIBCPP_END_NAMESPACE_STD
59
60#if _LIBCPP_STD_VER > 14
61namespace std  // purposefully not versioned
62{
63enum class byte : unsigned char {};
64
65constexpr byte  operator| (byte  __lhs, byte __rhs) noexcept
66{
67    return static_cast<byte>(
68      static_cast<unsigned char>(
69         static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)
70    ));
71}
72
73constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept
74{ return __lhs = __lhs | __rhs; }
75
76constexpr byte  operator& (byte  __lhs, byte __rhs) noexcept
77{
78    return static_cast<byte>(
79      static_cast<unsigned char>(
80         static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)
81    ));
82}
83
84constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept
85{ return __lhs = __lhs & __rhs; }
86
87constexpr byte  operator^ (byte  __lhs, byte __rhs) noexcept
88{
89    return static_cast<byte>(
90      static_cast<unsigned char>(
91         static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)
92    ));
93}
94
95constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept
96{ return __lhs = __lhs ^ __rhs; }
97
98constexpr byte  operator~ (byte __b) noexcept
99{
100    return static_cast<byte>(
101      static_cast<unsigned char>(
102        ~static_cast<unsigned int>(__b)
103    ));
104}
105
106template <class _Tp>
107using _EnableByteOverload = __enable_if_t<is_integral<_Tp>::value, byte>;
108
109template <class _Integer>
110  constexpr _EnableByteOverload<_Integer> &
111  operator<<=(byte& __lhs, _Integer __shift) noexcept
112  { return __lhs = __lhs << __shift; }
113
114template <class _Integer>
115  constexpr _EnableByteOverload<_Integer>
116  operator<< (byte  __lhs, _Integer __shift) noexcept
117  { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift)); }
118
119template <class _Integer>
120  constexpr _EnableByteOverload<_Integer> &
121  operator>>=(byte& __lhs, _Integer __shift) noexcept
122  { return __lhs = __lhs >> __shift; }
123
124template <class _Integer>
125  constexpr _EnableByteOverload<_Integer>
126  operator>> (byte  __lhs, _Integer __shift) noexcept
127  { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift)); }
128
129template <class _Integer, class = _EnableByteOverload<_Integer> >
130  _LIBCPP_NODISCARD_EXT constexpr _Integer
131  to_integer(byte __b) noexcept { return static_cast<_Integer>(__b); }
132
133} // namespace std
134
135#endif
136
137#endif // _LIBCPP_CSTDDEF
138