Skip to content

[libc++][modules] Introduce a forward-declaration for std::byte #107402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Sep 5, 2024

We need a forward-declaration so that we can know about std::byte from some type traits without having to include std::byte's definition, which (circularly) depends back on type traits.

@ldionne ldionne requested a review from a team as a code owner September 5, 2024 13:45
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2024

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

We need a forward-declaration so that we can know about std::byte from some type traits without having to include std::byte's definition, which (circularly) depends back on type traits.


Full diff: https://github.com/llvm/llvm-project/pull/107402.diff

6 Files Affected:

  • (modified) libcxx/include/CMakeLists.txt (+1)
  • (modified) libcxx/include/__cstddef/byte.h (+1)
  • (added) libcxx/include/__fwd/byte.h (+26)
  • (modified) libcxx/include/__type_traits/is_trivially_lexicographically_comparable.h (+1-1)
  • (modified) libcxx/include/module.modulemap (+3)
  • (modified) libcxx/utils/generate_iwyu_mapping.py (+2)
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 0f43916dae4384..a571832ab724d4 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -418,6 +418,7 @@ set(files
   __functional/weak_result_type.h
   __fwd/array.h
   __fwd/bit_reference.h
+  __fwd/byte.h
   __fwd/complex.h
   __fwd/deque.h
   __fwd/format.h
diff --git a/libcxx/include/__cstddef/byte.h b/libcxx/include/__cstddef/byte.h
index b8cfe5e8d1c7ef..09e1d75e0b41f6 100644
--- a/libcxx/include/__cstddef/byte.h
+++ b/libcxx/include/__cstddef/byte.h
@@ -10,6 +10,7 @@
 #define _LIBCPP___CSTDDEF_BYTE_H
 
 #include <__config>
+#include <__fwd/byte.h>
 #include <__type_traits/enable_if.h>
 #include <__type_traits/is_integral.h>
 
diff --git a/libcxx/include/__fwd/byte.h b/libcxx/include/__fwd/byte.h
new file mode 100644
index 00000000000000..e15311961bebd4
--- /dev/null
+++ b/libcxx/include/__fwd/byte.h
@@ -0,0 +1,26 @@
+//===---------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FWD_BYTE_H
+#define _LIBCPP___FWD_BYTE_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER >= 17
+namespace std { // purposefully not versioned
+
+enum class byte : unsigned char;
+
+}
+#endif // _LIBCPP_STD_VER >= 17
+
+#endif // _LIBCPP___FWD_BYTE_H
diff --git a/libcxx/include/__type_traits/is_trivially_lexicographically_comparable.h b/libcxx/include/__type_traits/is_trivially_lexicographically_comparable.h
index 337f878fea5c1d..15dda5824a3623 100644
--- a/libcxx/include/__type_traits/is_trivially_lexicographically_comparable.h
+++ b/libcxx/include/__type_traits/is_trivially_lexicographically_comparable.h
@@ -10,13 +10,13 @@
 #define _LIBCPP___TYPE_TRAITS_IS_TRIVIALLY_LEXICOGRAPHICALLY_COMPARABLE_H
 
 #include <__config>
+#include <__fwd/byte.h>
 #include <__type_traits/integral_constant.h>
 #include <__type_traits/is_same.h>
 #include <__type_traits/is_unsigned.h>
 #include <__type_traits/remove_cv.h>
 #include <__type_traits/void_t.h>
 #include <__utility/declval.h>
-#include <cstddef>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 3abc11723a5a92..65df579b8d6dd7 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -636,6 +636,9 @@ module std_private_bit_reference     [system] {
 module std_private_fwd_bit_reference [system] {
   header "__fwd/bit_reference.h"
 }
+module std_private_fwd_byte [system] {
+  header "__fwd/byte.h"
+}
 module std_private_config            [system] {
   textual header "__config"
   textual header "__configuration/abi.h"
diff --git a/libcxx/utils/generate_iwyu_mapping.py b/libcxx/utils/generate_iwyu_mapping.py
index b0ebe1b9e93d26..599201808bb79b 100644
--- a/libcxx/utils/generate_iwyu_mapping.py
+++ b/libcxx/utils/generate_iwyu_mapping.py
@@ -40,6 +40,8 @@ def IWYU_mapping(header: str) -> typing.Optional[typing.List[str]]:
         return ["atomic", "mutex", "semaphore", "thread"]
     elif header == "__tree":
         return ["map", "set"]
+    elif header == "__fwd/byte.h":
+        return ["cstddef"]
     elif header == "__fwd/pair.h":
         return ["utility"]
     elif header == "__fwd/subrange.h":

We need a forward-declaration so that we can know about std::byte
from some type traits without having to include std::byte's
definition, which (circularly) depends back on type traits.
@ldionne ldionne force-pushed the review/modularization-byte-forward-declare branch from 4d477ca to 1cf699b Compare September 5, 2024 14:00
@ldionne ldionne merged commit f8350f1 into llvm:main Sep 6, 2024
64 checks passed
@ldionne ldionne deleted the review/modularization-byte-forward-declare branch September 6, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants