blob: 57cecc7bd772a1628984c5efde67f844aa43ed2a [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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
Joerg Sonnenberger09c4ae52020-04-04 00:48:02 +020028 max_align_t // C++11
Howard Hinnantc51e1022010-05-11 19:42:16 +000029 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>
Louis Dionne9bb7cf42021-09-08 12:57:58 -040037#include <stddef.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +000038#include <version>
Howard Hinnant59ef87b2010-06-02 18:53:22 +000039
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000040#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050041# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000042#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000043
44_LIBCPP_BEGIN_NAMESPACE_STD
45
Louis Dionne9bb7cf42021-09-08 12:57:58 -040046using ::nullptr_t;
Louis Dionne9c660c62021-06-02 10:41:37 -040047using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;
48using ::size_t _LIBCPP_USING_IF_EXISTS;
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Joerg Sonnenberger09c4ae52020-04-04 00:48:02 +020050#if !defined(_LIBCPP_CXX03_LANG)
Louis Dionne9c660c62021-06-02 10:41:37 -040051using ::max_align_t _LIBCPP_USING_IF_EXISTS;
Chandler Carruth4dcf1992014-02-21 08:37:30 +000052#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000053
Eric Fiselierd2971c62020-02-14 17:34:46 +010054template <class _Tp> struct __libcpp_is_integral { enum { value = 0 }; };
55template <> struct __libcpp_is_integral<bool> { enum { value = 1 }; };
56template <> struct __libcpp_is_integral<char> { enum { value = 1 }; };
57template <> struct __libcpp_is_integral<signed char> { enum { value = 1 }; };
58template <> struct __libcpp_is_integral<unsigned char> { enum { value = 1 }; };
Louis Dionne89258142021-08-23 15:32:36 -040059#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Eric Fiselierd2971c62020-02-14 17:34:46 +010060template <> struct __libcpp_is_integral<wchar_t> { enum { value = 1 }; };
Louis Dionne89258142021-08-23 15:32:36 -040061#endif
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -040062#ifndef _LIBCPP_HAS_NO_CHAR8_T
Eric Fiselierd2971c62020-02-14 17:34:46 +010063template <> 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 }; };
Louis Dionne96fc5f52021-09-09 11:25:10 -040068#endif
Eric Fiselierd2971c62020-02-14 17:34:46 +010069template <> 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
Howard Hinnantc51e1022010-05-11 19:42:16 +000082_LIBCPP_END_NAMESPACE_STD
83
Marshall Clowcade1f72017-03-24 05:45:39 +000084#if _LIBCPP_STD_VER > 14
85namespace std // purposefully not versioned
86{
87enum class byte : unsigned char {};
88
Eric Fiselierd2971c62020-02-14 17:34:46 +010089
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
Marshall Clowcade1f72017-03-24 05:45:39 +000094constexpr byte operator| (byte __lhs, byte __rhs) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +000095{
Louis Dionne44bcff92018-08-03 22:36:53 +000096 return static_cast<byte>(
97 static_cast<unsigned char>(
98 static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)
99 ));
Marshall Clow31c8f462017-11-14 01:14:53 +0000100}
101
102constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept
103{ return __lhs = __lhs | __rhs; }
104
105constexpr byte operator& (byte __lhs, byte __rhs) noexcept
106{
Louis Dionne44bcff92018-08-03 22:36:53 +0000107 return static_cast<byte>(
108 static_cast<unsigned char>(
109 static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)
110 ));
Marshall Clow31c8f462017-11-14 01:14:53 +0000111}
Marshall Clowcade1f72017-03-24 05:45:39 +0000112
113constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +0000114{ return __lhs = __lhs & __rhs; }
115
116constexpr byte operator^ (byte __lhs, byte __rhs) noexcept
117{
Louis Dionne44bcff92018-08-03 22:36:53 +0000118 return static_cast<byte>(
119 static_cast<unsigned char>(
120 static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)
121 ));
Marshall Clow31c8f462017-11-14 01:14:53 +0000122}
Marshall Clowcade1f72017-03-24 05:45:39 +0000123
Louis Dionne44bcff92018-08-03 22:36:53 +0000124constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +0000125{ return __lhs = __lhs ^ __rhs; }
Marshall Clowcade1f72017-03-24 05:45:39 +0000126
127constexpr byte operator~ (byte __b) noexcept
Marshall Clow31c8f462017-11-14 01:14:53 +0000128{
129 return static_cast<byte>(
130 static_cast<unsigned char>(
131 ~static_cast<unsigned int>(__b)
132 ));
133}
Eric Fiselierd2971c62020-02-14 17:34:46 +0100134template <class _Integer>
135 constexpr _EnableByteOverload<_Integer> &
136 operator<<=(byte& __lhs, _Integer __shift) noexcept
137 { return __lhs = __lhs << __shift; }
Marshall Clowcade1f72017-03-24 05:45:39 +0000138
Eric Fiselierd2971c62020-02-14 17:34:46 +0100139template <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> >
Arthur O'Dwyer108facb2021-04-05 14:56:03 -0400155 _LIBCPP_NODISCARD_EXT constexpr _Integer
Eric Fiselierd2971c62020-02-14 17:34:46 +0100156 to_integer(byte __b) noexcept { return static_cast<_Integer>(__b); }
Nikolas Klausercfa31562022-02-01 18:11:49 +0100157
158} // namespace std
Marshall Clowcade1f72017-03-24 05:45:39 +0000159
Marshall Clowcade1f72017-03-24 05:45:39 +0000160#endif
161
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400162#endif // _LIBCPP_CSTDDEF