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