xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/addressof.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 
10 #ifndef _LIBCPP___MEMORY_ADDRESSOF_H
11 #define _LIBCPP___MEMORY_ADDRESSOF_H
12 
13 #include <__config>
14 
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 #  pragma GCC system_header
17 #endif
18 
19 _LIBCPP_BEGIN_NAMESPACE_STD
20 
21 template <class _Tp>
22 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
23 _LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
24 _Tp*
25 addressof(_Tp& __x) _NOEXCEPT
26 {
27     return __builtin_addressof(__x);
28 }
29 
30 #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
31 // Objective-C++ Automatic Reference Counting uses qualified pointers
32 // that require special addressof() signatures. When
33 // _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
34 // itself is providing these definitions. Otherwise, we provide them.
35 template <class _Tp>
36 inline _LIBCPP_INLINE_VISIBILITY
37 __strong _Tp*
38 addressof(__strong _Tp& __x) _NOEXCEPT
39 {
40   return &__x;
41 }
42 
43 #ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
44 template <class _Tp>
45 inline _LIBCPP_INLINE_VISIBILITY
46 __weak _Tp*
47 addressof(__weak _Tp& __x) _NOEXCEPT
48 {
49   return &__x;
50 }
51 #endif
52 
53 template <class _Tp>
54 inline _LIBCPP_INLINE_VISIBILITY
55 __autoreleasing _Tp*
56 addressof(__autoreleasing _Tp& __x) _NOEXCEPT
57 {
58   return &__x;
59 }
60 
61 template <class _Tp>
62 inline _LIBCPP_INLINE_VISIBILITY
63 __unsafe_unretained _Tp*
64 addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
65 {
66   return &__x;
67 }
68 #endif
69 
70 #if !defined(_LIBCPP_CXX03_LANG)
71 template <class _Tp> _Tp* addressof(const _Tp&&) noexcept = delete;
72 #endif
73 
74 _LIBCPP_END_NAMESPACE_STD
75 
76 #endif // _LIBCPP___MEMORY_ADDRESSOF_H
77