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