__availability 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #ifndef _LIBCPP___AVAILABILITY
  10. #define _LIBCPP___AVAILABILITY
  11. #include <__config>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. // Libc++ is shipped by various vendors. In particular, it is used as a system
  16. // library on macOS, iOS and other Apple platforms. In order for users to be
  17. // able to compile a binary that is intended to be deployed to an older version
  18. // of a platform, Clang provides availability attributes [1]. These attributes
  19. // can be placed on declarations and are used to describe the life cycle of a
  20. // symbol in the library.
  21. //
  22. // The main goal is to ensure a compile-time error if a symbol that hasn't been
  23. // introduced in a previously released library is used in a program that targets
  24. // that previously released library. Normally, this would be a load-time error
  25. // when one tries to launch the program against the older library.
  26. //
  27. // For example, the filesystem library was introduced in the dylib in macOS 10.15.
  28. // If a user compiles on a macOS 10.15 host but targets macOS 10.13 with their
  29. // program, the compiler would normally not complain (because the required
  30. // declarations are in the headers), but the dynamic loader would fail to find
  31. // the symbols when actually trying to launch the program on macOS 10.13. To
  32. // turn this into a compile-time issue instead, declarations are annotated with
  33. // when they were introduced, and the compiler can produce a diagnostic if the
  34. // program references something that isn't available on the deployment target.
  35. //
  36. // This mechanism is general in nature, and any vendor can add their markup to
  37. // the library (see below). Whenever a new feature is added that requires support
  38. // in the shared library, a macro should be added below to mark this feature
  39. // as unavailable. When vendors decide to ship the feature as part of their
  40. // shared library, they can update the markup appropriately.
  41. //
  42. // Furthermore, many features in the standard library have corresponding
  43. // feature-test macros. When a feature is made unavailable on some deployment
  44. // target, a macro should be defined to signal that it is unavailable. That
  45. // macro can then be picked up when feature-test macros are generated (see
  46. // generate_feature_test_macro_components.py) to make sure that feature-test
  47. // macros don't announce a feature as being implemented if it has been marked
  48. // as unavailable.
  49. //
  50. // Note that this mechanism is disabled by default in the "upstream" libc++.
  51. // Availability annotations are only meaningful when shipping libc++ inside
  52. // a platform (i.e. as a system library), and so vendors that want them should
  53. // turn those annotations on at CMake configuration time.
  54. //
  55. // [1]: https://clang.llvm.org/docs/AttributeReference.html#availability
  56. // For backwards compatibility, allow users to define _LIBCPP_DISABLE_AVAILABILITY
  57. // for a while.
  58. #if defined(_LIBCPP_DISABLE_AVAILABILITY)
  59. # if !defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  60. # define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS
  61. # endif
  62. #endif
  63. // Availability markup is disabled when building the library, or when the compiler
  64. // doesn't support the proper attributes.
  65. #if defined(_LIBCPP_BUILDING_LIBRARY) || \
  66. defined(_LIBCXXABI_BUILDING_LIBRARY) || \
  67. !__has_feature(attribute_availability_with_strict) || \
  68. !__has_feature(attribute_availability_in_templates) || \
  69. !__has_extension(pragma_clang_attribute_external_declaration)
  70. # if !defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  71. # define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS
  72. # endif
  73. #endif
  74. #if defined(_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
  75. // This controls the availability of std::shared_mutex and std::shared_timed_mutex,
  76. // which were added to the dylib later.
  77. # define _LIBCPP_AVAILABILITY_SHARED_MUTEX
  78. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex
  79. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex
  80. // These macros control the availability of std::bad_optional_access and
  81. // other exception types. These were put in the shared library to prevent
  82. // code bloat from every user program defining the vtable for these exception
  83. // types.
  84. # define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  85. # define _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS
  86. # define _LIBCPP_AVAILABILITY_BAD_ANY_CAST
  87. // This controls the availability of std::uncaught_exceptions().
  88. # define _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS
  89. // This controls the availability of the sized version of ::operator delete,
  90. // which was added to the dylib later.
  91. # define _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE
  92. // This controls the availability of the std::future_error exception.
  93. # define _LIBCPP_AVAILABILITY_FUTURE_ERROR
  94. // This controls the availability of std::type_info's vtable.
  95. // I can't imagine how using std::type_info can work at all if
  96. // this isn't supported.
  97. # define _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
  98. // This controls the availability of std::locale::category members
  99. // (e.g. std::locale::collate), which are defined in the dylib.
  100. # define _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
  101. // This controls the availability of atomic operations on std::shared_ptr
  102. // (e.g. `std::atomic_store(std::shared_ptr)`), which require a shared
  103. // lock table located in the dylib.
  104. # define _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  105. // These macros control the availability of all parts of <filesystem> that
  106. // depend on something in the dylib.
  107. # define _LIBCPP_AVAILABILITY_FILESYSTEM
  108. # define _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
  109. # define _LIBCPP_AVAILABILITY_FILESYSTEM_POP
  110. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem
  111. // This controls the availability of std::to_chars.
  112. # define _LIBCPP_AVAILABILITY_TO_CHARS
  113. // This controls the availability of floating-point std::to_chars functions.
  114. // These overloads were added later than the integer overloads.
  115. # define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT
  116. // This controls the availability of the C++20 synchronization library,
  117. // which requires shared library support for various operations
  118. // (see libcxx/src/atomic.cpp).
  119. # define _LIBCPP_AVAILABILITY_SYNC
  120. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait
  121. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier
  122. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch
  123. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
  124. // This controls the availability of the C++20 format library.
  125. // The library is in development and not ABI stable yet. P2216 is
  126. // retroactively accepted in C++20. This paper contains ABI breaking
  127. // changes.
  128. # define _LIBCPP_AVAILABILITY_FORMAT
  129. // # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format
  130. #elif defined(__APPLE__)
  131. # define _LIBCPP_AVAILABILITY_SHARED_MUTEX \
  132. __attribute__((availability(macosx,strict,introduced=10.12))) \
  133. __attribute__((availability(ios,strict,introduced=10.0))) \
  134. __attribute__((availability(tvos,strict,introduced=10.0))) \
  135. __attribute__((availability(watchos,strict,introduced=3.0)))
  136. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200) || \
  137. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 100000) || \
  138. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 100000) || \
  139. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 30000)
  140. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_mutex
  141. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_shared_timed_mutex
  142. # endif
  143. # define _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS \
  144. __attribute__((availability(macosx,strict,introduced=10.13))) \
  145. __attribute__((availability(ios,strict,introduced=11.0))) \
  146. __attribute__((availability(tvos,strict,introduced=11.0))) \
  147. __attribute__((availability(watchos,strict,introduced=4.0)))
  148. # define _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS \
  149. _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  150. # define _LIBCPP_AVAILABILITY_BAD_ANY_CAST \
  151. _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  152. # define _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS \
  153. __attribute__((availability(macosx,strict,introduced=10.12))) \
  154. __attribute__((availability(ios,strict,introduced=10.0))) \
  155. __attribute__((availability(tvos,strict,introduced=10.0))) \
  156. __attribute__((availability(watchos,strict,introduced=3.0)))
  157. # define _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE \
  158. __attribute__((availability(macosx,strict,introduced=10.12))) \
  159. __attribute__((availability(ios,strict,introduced=10.0))) \
  160. __attribute__((availability(tvos,strict,introduced=10.0))) \
  161. __attribute__((availability(watchos,strict,introduced=3.0)))
  162. # define _LIBCPP_AVAILABILITY_FUTURE_ERROR \
  163. __attribute__((availability(ios,strict,introduced=6.0)))
  164. # define _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE \
  165. __attribute__((availability(macosx,strict,introduced=10.9))) \
  166. __attribute__((availability(ios,strict,introduced=7.0)))
  167. # define _LIBCPP_AVAILABILITY_LOCALE_CATEGORY \
  168. __attribute__((availability(macosx,strict,introduced=10.9))) \
  169. __attribute__((availability(ios,strict,introduced=7.0)))
  170. # define _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR \
  171. __attribute__((availability(macosx,strict,introduced=10.9))) \
  172. __attribute__((availability(ios,strict,introduced=7.0)))
  173. # define _LIBCPP_AVAILABILITY_FILESYSTEM \
  174. __attribute__((availability(macosx,strict,introduced=10.15))) \
  175. __attribute__((availability(ios,strict,introduced=13.0))) \
  176. __attribute__((availability(tvos,strict,introduced=13.0))) \
  177. __attribute__((availability(watchos,strict,introduced=6.0)))
  178. # define _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH \
  179. _Pragma("clang attribute push(__attribute__((availability(macosx,strict,introduced=10.15))), apply_to=any(function,record))") \
  180. _Pragma("clang attribute push(__attribute__((availability(ios,strict,introduced=13.0))), apply_to=any(function,record))") \
  181. _Pragma("clang attribute push(__attribute__((availability(tvos,strict,introduced=13.0))), apply_to=any(function,record))") \
  182. _Pragma("clang attribute push(__attribute__((availability(watchos,strict,introduced=6.0))), apply_to=any(function,record))")
  183. # define _LIBCPP_AVAILABILITY_FILESYSTEM_POP \
  184. _Pragma("clang attribute pop") \
  185. _Pragma("clang attribute pop") \
  186. _Pragma("clang attribute pop") \
  187. _Pragma("clang attribute pop")
  188. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500) || \
  189. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  190. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 130000) || \
  191. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 60000)
  192. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_filesystem
  193. # endif
  194. # define _LIBCPP_AVAILABILITY_TO_CHARS \
  195. _LIBCPP_AVAILABILITY_FILESYSTEM
  196. # define _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT \
  197. __attribute__((unavailable))
  198. # define _LIBCPP_AVAILABILITY_SYNC \
  199. __attribute__((availability(macosx,strict,introduced=11.0))) \
  200. __attribute__((availability(ios,strict,introduced=14.0))) \
  201. __attribute__((availability(tvos,strict,introduced=14.0))) \
  202. __attribute__((availability(watchos,strict,introduced=7.0)))
  203. # if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 110000) || \
  204. (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  205. (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 140000) || \
  206. (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 70000)
  207. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_atomic_wait
  208. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_barrier
  209. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_latch
  210. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_semaphore
  211. # endif
  212. # define _LIBCPP_AVAILABILITY_FORMAT \
  213. __attribute__((unavailable))
  214. # define _LIBCPP_AVAILABILITY_DISABLE_FTM___cpp_lib_format
  215. #else
  216. // ...New vendors can add availability markup here...
  217. # error "It looks like you're trying to enable vendor availability markup, but you haven't defined the corresponding macros yet!"
  218. #endif
  219. // Define availability attributes that depend on _LIBCPP_NO_EXCEPTIONS.
  220. // Those are defined in terms of the availability attributes above, and
  221. // should not be vendor-specific.
  222. #if defined(_LIBCPP_NO_EXCEPTIONS)
  223. # define _LIBCPP_AVAILABILITY_FUTURE
  224. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
  225. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
  226. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS
  227. #else
  228. # define _LIBCPP_AVAILABILITY_FUTURE _LIBCPP_AVAILABILITY_FUTURE_ERROR
  229. # define _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _LIBCPP_AVAILABILITY_BAD_ANY_CAST
  230. # define _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
  231. # define _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS _LIBCPP_AVAILABILITY_BAD_VARIANT_ACCESS
  232. #endif
  233. #endif // _LIBCPP___AVAILABILITY