xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/addressof.h (revision 093cf790569775b80662926efea6d9d3464bde94)
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_PUSH_MACROS
20 #include <__undef_macros>
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
25 
26 template <class _Tp>
27 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
28 _LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
29 _Tp*
30 addressof(_Tp& __x) _NOEXCEPT
31 {
32     return __builtin_addressof(__x);
33 }
34 
35 #else
36 
37 template <class _Tp>
38 inline _LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
39 _Tp*
40 addressof(_Tp& __x) _NOEXCEPT
41 {
42   return reinterpret_cast<_Tp *>(
43       const_cast<char *>(&reinterpret_cast<const volatile char &>(__x)));
44 }
45 
46 #endif // _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
47 
48 #if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
49 // Objective-C++ Automatic Reference Counting uses qualified pointers
50 // that require special addressof() signatures. When
51 // _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
52 // itself is providing these definitions. Otherwise, we provide them.
53 template <class _Tp>
54 inline _LIBCPP_INLINE_VISIBILITY
55 __strong _Tp*
56 addressof(__strong _Tp& __x) _NOEXCEPT
57 {
58   return &__x;
59 }
60 
61 #ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
62 template <class _Tp>
63 inline _LIBCPP_INLINE_VISIBILITY
64 __weak _Tp*
65 addressof(__weak _Tp& __x) _NOEXCEPT
66 {
67   return &__x;
68 }
69 #endif
70 
71 template <class _Tp>
72 inline _LIBCPP_INLINE_VISIBILITY
73 __autoreleasing _Tp*
74 addressof(__autoreleasing _Tp& __x) _NOEXCEPT
75 {
76   return &__x;
77 }
78 
79 template <class _Tp>
80 inline _LIBCPP_INLINE_VISIBILITY
81 __unsafe_unretained _Tp*
82 addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
83 {
84   return &__x;
85 }
86 #endif
87 
88 #if !defined(_LIBCPP_CXX03_LANG)
89 template <class _Tp> _Tp* addressof(const _Tp&&) noexcept = delete;
90 #endif
91 
92 _LIBCPP_END_NAMESPACE_STD
93 
94 _LIBCPP_POP_MACROS
95 
96 #endif // _LIBCPP___MEMORY_ADDRESSOF_H
97