blob: 87eee4bf5b424bf808921a7e628c860faa9bf61e [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- cstddef ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
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
Howard Hinnant3b6579a2010-08-22 00:02:43 +000020
Howard Hinnantc51e1022010-05-11 19:42:16 +000021namespace std
22{
23
24Types:
25
26 ptrdiff_t
27 size_t
28 max_align_t
29 nullptr_t
Marshall Clowcade1f72017-03-24 05:45:39 +000030 byte // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000031
32} // std
33
34*/
35
36#include <__config>
Marshall Clow0a1e7502018-09-12 19:41:40 +000037#include <version>
Howard Hinnant59ef87b2010-06-02 18:53:22 +000038
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000039#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000040#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000041#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000042
Richard Smith523b1722015-10-09 00:26:50 +000043// Don't include our own <stddef.h>; we don't want to declare ::nullptr_t.
44#include_next <stddef.h>
45#include <__nullptr>
46
Howard Hinnantc51e1022010-05-11 19:42:16 +000047_LIBCPP_BEGIN_NAMESPACE_STD
48
49using ::ptrdiff_t;
50using ::size_t;
51
David L. Jones8465cf12017-02-10 01:27:42 +000052#if defined(__CLANG_MAX_ALIGN_T_DEFINED) || defined(_GCC_MAX_ALIGN_T) || \
Kamil Rytarowski5678f1e2018-08-20 22:29:20 +000053 defined(__DEFINED_max_align_t) || defined(__NetBSD__)
Chandler Carruth4dcf1992014-02-21 08:37:30 +000054// Re-use the compiler's <stddef.h> max_align_t where possible.
55using ::max_align_t;
56#else
Howard Hinnantc51e1022010-05-11 19:42:16 +000057typedef long double max_align_t;
Chandler Carruth4dcf1992014-02-21 08:37:30 +000058#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000059
Eric Fiselierd2971c62020-02-14 17:34:46 +010060template <class _Tp> struct __libcpp_is_integral { enum { value = 0 }; };
61template <> struct __libcpp_is_integral<bool> { enum { value = 1 }; };
62template <> struct __libcpp_is_integral<char> { enum { value = 1 }; };
63template <> struct __libcpp_is_integral<signed char> { enum { value = 1 }; };
64template <> struct __libcpp_is_integral<unsigned char> { enum { value = 1 }; };
65template <> struct __libcpp_is_integral<wchar_t> { enum { value = 1 }; };
66#ifndef _LIBCPP_NO_HAS_CHAR8_T
67template <> struct __libcpp_is_integral<char8_t> { enum { value = 1 }; };
68#endif
69#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
70template <> struct __libcpp_is_integral<char16_t> { enum { value = 1 }; };
71template <> struct __libcpp_is_integral<char32_t> { enum { value = 1 }; };
72#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
73template <> struct __libcpp_is_integral<short> { enum { value = 1 }; };
74template <> struct __libcpp_is_integral<unsigned short> { enum { value = 1 }; };
75template <> struct __libcpp_is_integral<int> { enum { value = 1 }; };
76template <> struct __libcpp_is_integral<unsigned int> { enum { value = 1 }; };
77template <> struct __libcpp_is_integral<long> { enum { value = 1 }; };
78template <> struct __libcpp_is_integral<unsigned long> { enum { value = 1 }; };
79template <> struct __libcpp_is_integral<long long> { enum { value = 1 }; };
80template <> struct __libcpp_is_integral<unsigned long long> { enum { value = 1 }; };
81#ifndef _LIBCPP_HAS_NO_INT128
82template <> struct __libcpp_is_integral<__int128_t> { enum { value = 1 }; };
83template <> struct __libcpp_is_integral<__uint128_t> { enum { value = 1 }; };
84#endif
85
Howard Hinnantc51e1022010-05-11 19:42:16 +000086_LIBCPP_END_NAMESPACE_STD
87
Marshall Clowcade1f72017-03-24 05:45:39 +000088#if _LIBCPP_STD_VER > 14
89namespace std // purposefully not versioned
90{
91enum class byte : unsigned char {};
92
Eric Fiselierd2971c62020-02-14 17:34:46 +010093
94template <bool> struct __enable_if_integral_imp {};
95template <> struct __enable_if_integral_imp<true> { using type = byte; };
96template <class _Tp> using _EnableByteOverload = typename __enable_if_integral_imp<__libcpp_is_integral<_Tp>::value>::type;
97
Marshall Clowcade1f72017-03-24 05:45:39 +000098constexpr byte operator| (byte __lhs, byte __rhs) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +000099{
Louis Dionne44bcff92018-08-03 22:36:53 +0000100 return static_cast<byte>(
101 static_cast<unsigned char>(
102 static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)
103 ));
Marshall Clow31c8f462017-11-14 01:14:53 +0000104}
105
106constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept
107{ return __lhs = __lhs | __rhs; }
108
109constexpr byte operator& (byte __lhs, byte __rhs) noexcept
110{
Louis Dionne44bcff92018-08-03 22:36:53 +0000111 return static_cast<byte>(
112 static_cast<unsigned char>(
113 static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)
114 ));
Marshall Clow31c8f462017-11-14 01:14:53 +0000115}
Marshall Clowcade1f72017-03-24 05:45:39 +0000116
117constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +0000118{ return __lhs = __lhs & __rhs; }
119
120constexpr byte operator^ (byte __lhs, byte __rhs) noexcept
121{
Louis Dionne44bcff92018-08-03 22:36:53 +0000122 return static_cast<byte>(
123 static_cast<unsigned char>(
124 static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)
125 ));
Marshall Clow31c8f462017-11-14 01:14:53 +0000126}
Marshall Clowcade1f72017-03-24 05:45:39 +0000127
Louis Dionne44bcff92018-08-03 22:36:53 +0000128constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +0000129{ return __lhs = __lhs ^ __rhs; }
Marshall Clowcade1f72017-03-24 05:45:39 +0000130
131constexpr byte operator~ (byte __b) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +0000132{
133 return static_cast<byte>(
134 static_cast<unsigned char>(
135 ~static_cast<unsigned int>(__b)
136 ));
137}
Eric Fiselierd2971c62020-02-14 17:34:46 +0100138template <class _Integer>
139 constexpr _EnableByteOverload<_Integer> &
140 operator<<=(byte& __lhs, _Integer __shift) noexcept
141 { return __lhs = __lhs << __shift; }
Marshall Clowcade1f72017-03-24 05:45:39 +0000142
Eric Fiselierd2971c62020-02-14 17:34:46 +0100143template <class _Integer>
144 constexpr _EnableByteOverload<_Integer>
145 operator<< (byte __lhs, _Integer __shift) noexcept
146 { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift)); }
147
148template <class _Integer>
149 constexpr _EnableByteOverload<_Integer> &
150 operator>>=(byte& __lhs, _Integer __shift) noexcept
151 { return __lhs = __lhs >> __shift; }
152
153template <class _Integer>
154 constexpr _EnableByteOverload<_Integer>
155 operator>> (byte __lhs, _Integer __shift) noexcept
156 { return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift)); }
157
158template <class _Integer, class = _EnableByteOverload<_Integer> >
159 constexpr _Integer
160 to_integer(byte __b) noexcept { return static_cast<_Integer>(__b); }
Marshall Clowcade1f72017-03-24 05:45:39 +0000161}
162
Marshall Clowcade1f72017-03-24 05:45:39 +0000163#endif
164
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165#endif // _LIBCPP_CSTDDEF