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
26template <class _Tp>
27inline _LIBCPP_CONSTEXPR_AFTER_CXX14
28_LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
29_Tp*
30addressof(_Tp& __x) _NOEXCEPT
31{
32    return __builtin_addressof(__x);
33}
34
35#else
36
37template <class _Tp>
38inline _LIBCPP_NO_CFI _LIBCPP_INLINE_VISIBILITY
39_Tp*
40addressof(_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.
53template <class _Tp>
54inline _LIBCPP_INLINE_VISIBILITY
55__strong _Tp*
56addressof(__strong _Tp& __x) _NOEXCEPT
57{
58  return &__x;
59}
60
61#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
62template <class _Tp>
63inline _LIBCPP_INLINE_VISIBILITY
64__weak _Tp*
65addressof(__weak _Tp& __x) _NOEXCEPT
66{
67  return &__x;
68}
69#endif
70
71template <class _Tp>
72inline _LIBCPP_INLINE_VISIBILITY
73__autoreleasing _Tp*
74addressof(__autoreleasing _Tp& __x) _NOEXCEPT
75{
76  return &__x;
77}
78
79template <class _Tp>
80inline _LIBCPP_INLINE_VISIBILITY
81__unsafe_unretained _Tp*
82addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
83{
84  return &__x;
85}
86#endif
87
88#if !defined(_LIBCPP_CXX03_LANG)
89template <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