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___STRING_CONSTEXPR_C_FUNCTIONS_H
10#define _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H
11
12#include <__config>
13#include <__cstddef/size_t.h>
14#include <__memory/addressof.h>
15#include <__memory/construct_at.h>
16#include <__type_traits/datasizeof.h>
17#include <__type_traits/enable_if.h>
18#include <__type_traits/is_always_bitcastable.h>
19#include <__type_traits/is_assignable.h>
20#include <__type_traits/is_constant_evaluated.h>
21#include <__type_traits/is_constructible.h>
22#include <__type_traits/is_equality_comparable.h>
23#include <__type_traits/is_integral.h>
24#include <__type_traits/is_same.h>
25#include <__type_traits/is_trivially_lexicographically_comparable.h>
26#include <__type_traits/remove_cv.h>
27#include <__utility/element_count.h>
28#include <__utility/is_pointer_in_range.h>
29
30#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31# pragma GCC system_header
32#endif
33
34_LIBCPP_BEGIN_NAMESPACE_STD
35
36template <class _Tp>
37inline const bool __is_char_type = false;
38
39template <>
40inline const bool __is_char_type<char> = true;
41
42#if _LIBCPP_HAS_CHAR8_T
43template <>
44inline const bool __is_char_type<char8_t> = true;
45#endif
46
47template <class _Tp>
48inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 size_t __constexpr_strlen(const _Tp* __str) _NOEXCEPT {
49 static_assert(__is_char_type<_Tp>, "__constexpr_strlen only works with char and char8_t");
50 // GCC currently doesn't support __builtin_strlen for heap-allocated memory during constant evaluation.
51 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70816
52 if (__libcpp_is_constant_evaluated()) {
53#if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_COMPILER_CLANG_BASED)
54 if constexpr (is_same_v<_Tp, char>)
55 return __builtin_strlen(__str);
56#endif
57 size_t __i = 0;
58 for (; __str[__i] != '\0'; ++__i)
59 ;
60 return __i;
61 }
62 return __builtin_strlen(reinterpret_cast<const char*>(__str));
63}
64
65// Because of __is_trivially_lexicographically_comparable_v we know that comparing the object representations is
66// equivalent to a std::memcmp. Since we have multiple objects contiguously in memory, we can call memcmp once instead
67// of invoking it on every object individually.
68template <class _Tp, class _Up>
69_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int
70__constexpr_memcmp(const _Tp* __lhs, const _Up* __rhs, __element_count __n) {
71 static_assert(__is_trivially_lexicographically_comparable_v<_Tp, _Up>,
72 "_Tp and _Up have to be trivially lexicographically comparable");
73
74 auto __count = static_cast<size_t>(__n);
75
76 if (__libcpp_is_constant_evaluated()) {
77#ifdef _LIBCPP_COMPILER_CLANG_BASED
78 if (sizeof(_Tp) == 1 && !is_same<_Tp, bool>::value)
79 return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp));
80#endif
81
82 while (__count != 0) {
83 if (*__lhs < *__rhs)
84 return -1;
85 if (*__rhs < *__lhs)
86 return 1;
87
88 --__count;
89 ++__lhs;
90 ++__rhs;
91 }
92 return 0;
93 } else {
94 return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp));
95 }
96}
97
98// Because of __is_trivially_equality_comparable_v we know that comparing the object representations is equivalent
99// to a std::memcmp(...) == 0. Since we have multiple objects contiguously in memory, we can call memcmp once instead
100// of invoking it on every object individually.
101template <class _Tp, class _Up>
102_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
103__constexpr_memcmp_equal(const _Tp* __lhs, const _Up* __rhs, __element_count __n) {
104 static_assert(__is_trivially_equality_comparable_v<_Tp, _Up>, "_Tp and _Up have to be trivially equality comparable");
105
106 auto __count = static_cast<size_t>(__n);
107
108 if (__libcpp_is_constant_evaluated()) {
109#ifdef _LIBCPP_COMPILER_CLANG_BASED
110 if (sizeof(_Tp) == 1 && is_integral<_Tp>::value && !is_same<_Tp, bool>::value)
111 return __builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp)) == 0;
112#endif
113 while (__count != 0) {
114 if (*__lhs != *__rhs)
115 return false;
116
117 --__count;
118 ++__lhs;
119 ++__rhs;
120 }
121 return true;
122 } else {
123 return ::__builtin_memcmp(__lhs, __rhs, __count * sizeof(_Tp)) == 0;
124 }
125}
126
127template <class _Tp, class _Up>
128_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __constexpr_memchr(_Tp* __str, _Up __value, size_t __count) {
129 static_assert(sizeof(_Tp) == 1 && __is_trivially_equality_comparable_v<_Tp, _Up>,
130 "Calling memchr on non-trivially equality comparable types is unsafe.");
131
132 if (__libcpp_is_constant_evaluated()) {
133// use __builtin_char_memchr to optimize constexpr evaluation if we can
134#if _LIBCPP_STD_VER >= 17 && __has_builtin(__builtin_char_memchr)
135 if constexpr (is_same_v<remove_cv_t<_Tp>, char> && is_same_v<remove_cv_t<_Up>, char>)
136 return __builtin_char_memchr(__str, __value, __count);
137#endif
138
139 for (; __count; --__count) {
140 if (*__str == __value)
141 return __str;
142 ++__str;
143 }
144 return nullptr;
145 } else {
146 char __value_buffer = 0;
147 __builtin_memcpy(&__value_buffer, std::addressof(__value), sizeof(char));
148 return static_cast<_Tp*>(__builtin_memchr(__str, __value_buffer, __count));
149 }
150}
151
152// This function performs an assignment to an existing, already alive TriviallyCopyable object
153// from another TriviallyCopyable object.
154//
155// It basically works around the fact that TriviallyCopyable objects are not required to be
156// syntactically copy/move constructible or copy/move assignable. Technically, only one of the
157// four operations is required to be syntactically valid -- but at least one definitely has to
158// be valid.
159//
160// This is necessary in order to implement __constexpr_memmove below in a way that mirrors as
161// closely as possible what the compiler's __builtin_memmove is able to do.
162template <class _Tp, class _Up, __enable_if_t<is_assignable<_Tp&, _Up const&>::value, int> = 0>
163_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up const& __src) {
164 __dest = __src;
165 return __dest;
166}
167
168// clang-format off
169template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&
170 is_assignable<_Tp&, _Up&&>::value, int> = 0>
171// clang-format on
172_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up& __src) {
173 __dest =
174 static_cast<_Up&&>(__src); // this is safe, we're not actually moving anything since the assignment is trivial
175 return __dest;
176}
177
178// clang-format off
179template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&
180 !is_assignable<_Tp&, _Up&&>::value &&
181 is_constructible<_Tp, _Up const&>::value, int> = 0>
182// clang-format on
183_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up const& __src) {
184 // _Tp is trivially destructible, so we don't need to call its destructor to end the lifetime of the object
185 // that was there previously
186 std::__construct_at(std::addressof(__dest), __src);
187 return __dest;
188}
189
190// clang-format off
191template <class _Tp, class _Up, __enable_if_t<!is_assignable<_Tp&, _Up const&>::value &&
192 !is_assignable<_Tp&, _Up&&>::value &&
193 !is_constructible<_Tp, _Up const&>::value &&
194 is_constructible<_Tp, _Up&&>::value, int> = 0>
195// clang-format on
196_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __assign_trivially_copyable(_Tp& __dest, _Up& __src) {
197 // _Tp is trivially destructible, so we don't need to call its destructor to end the lifetime of the object
198 // that was there previously
199 std::__construct_at(
200 std::addressof(__dest),
201 static_cast<_Up&&>(__src)); // this is safe, we're not actually moving anything since the constructor is trivial
202 return __dest;
203}
204
205template <class _Tp, class _Up>
206_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
207__constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {
208 static_assert(__is_always_bitcastable<_Up, _Tp>::value);
209 size_t __count = static_cast<size_t>(__n);
210 if (__libcpp_is_constant_evaluated()) {
211#ifdef _LIBCPP_COMPILER_CLANG_BASED
212 if _LIBCPP_CONSTEXPR (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value) {
213 ::__builtin_memmove(__dest, __src, __count * sizeof(_Tp));
214 return __dest;
215 } else
216#endif
217 {
218 if (std::__is_pointer_in_range(__src, __src + __count, __dest)) {
219 for (; __count > 0; --__count)
220 std::__assign_trivially_copyable(__dest[__count - 1], __src[__count - 1]);
221 } else {
222 for (size_t __i = 0; __i != __count; ++__i)
223 std::__assign_trivially_copyable(__dest[__i], __src[__i]);
224 }
225 }
226 } else if _LIBCPP_CONSTEXPR (sizeof(_Tp) == __datasizeof_v<_Tp>) {
227 ::__builtin_memmove(__dest, __src, __count * sizeof(_Tp));
228 } else if (__count > 0) {
229 ::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __datasizeof_v<_Tp>);
230 }
231 return __dest;
232}
233
234_LIBCPP_END_NAMESPACE_STD
235
236#endif // _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H
237