1// -*- C++ -*-
2//===--------------------------- __debug ----------------------------------===//
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_DEBUG_H
11#define _LIBCPP_DEBUG_H
12
13#include <__config>
14#include <iosfwd>
15
16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17#pragma GCC system_header
18#endif
19
20#if defined(_LIBCPP_HAS_NO_NULLPTR)
21# include <cstddef>
22#endif
23
24#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
25#   include <cstdlib>
26#   include <cstdio>
27#   include <cstddef>
28#endif
29
30#if _LIBCPP_DEBUG_LEVEL == 0
31#   define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
32#   define _LIBCPP_DEBUG_MODE(...) ((void)0)
33#   define _LIBCPP_ASSERT_IMPL(x, m) ((void)0)
34#elif _LIBCPP_DEBUG_LEVEL == 1
35#   define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
36#   define _LIBCPP_DEBUG_MODE(...) ((void)0)
37#   define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
38#elif _LIBCPP_DEBUG_LEVEL == 2
39#   define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(x, m)
40#   define _LIBCPP_DEBUG_MODE(...) __VA_ARGS__
41#   define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
42#else
43#   error _LIBCPP_DEBUG_LEVEL must be one of 0, 1, 2
44#endif
45
46#if !defined(_LIBCPP_ASSERT)
47#   define _LIBCPP_ASSERT(x, m) _LIBCPP_ASSERT_IMPL(x, m)
48#endif
49
50_LIBCPP_BEGIN_NAMESPACE_STD
51
52struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info {
53  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
54  __libcpp_debug_info()
55      : __file_(nullptr), __line_(-1), __pred_(nullptr), __msg_(nullptr) {}
56  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
57  __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m)
58    : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {}
59
60  _LIBCPP_FUNC_VIS std::string what() const;
61
62  const char* __file_;
63  int __line_;
64  const char* __pred_;
65  const char* __msg_;
66};
67
68/// __libcpp_debug_function_type - The type of the assertion failure handler.
69typedef void(*__libcpp_debug_function_type)(__libcpp_debug_info const&);
70
71/// __libcpp_debug_function - The handler function called when a _LIBCPP_ASSERT
72///    fails.
73extern _LIBCPP_EXPORTED_FROM_ABI __libcpp_debug_function_type __libcpp_debug_function;
74
75/// __libcpp_abort_debug_function - A debug handler that aborts when called.
76_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
77void __libcpp_abort_debug_function(__libcpp_debug_info const&);
78
79/// __libcpp_set_debug_function - Set the debug handler to the specified
80///    function.
81_LIBCPP_FUNC_VIS
82bool __libcpp_set_debug_function(__libcpp_debug_function_type __func);
83
84#if _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY)
85
86struct _LIBCPP_TYPE_VIS __c_node;
87
88struct _LIBCPP_TYPE_VIS __i_node
89{
90    void* __i_;
91    __i_node* __next_;
92    __c_node* __c_;
93
94#ifndef _LIBCPP_CXX03_LANG
95    __i_node(const __i_node&) = delete;
96    __i_node& operator=(const __i_node&) = delete;
97#else
98private:
99    __i_node(const __i_node&);
100    __i_node& operator=(const __i_node&);
101public:
102#endif
103    _LIBCPP_INLINE_VISIBILITY
104    __i_node(void* __i, __i_node* __next, __c_node* __c)
105        : __i_(__i), __next_(__next), __c_(__c) {}
106    ~__i_node();
107};
108
109struct _LIBCPP_TYPE_VIS __c_node
110{
111    void* __c_;
112    __c_node* __next_;
113    __i_node** beg_;
114    __i_node** end_;
115    __i_node** cap_;
116
117#ifndef _LIBCPP_CXX03_LANG
118    __c_node(const __c_node&) = delete;
119    __c_node& operator=(const __c_node&) = delete;
120#else
121private:
122    __c_node(const __c_node&);
123    __c_node& operator=(const __c_node&);
124public:
125#endif
126    _LIBCPP_INLINE_VISIBILITY
127    __c_node(void* __c, __c_node* __next)
128        : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
129    virtual ~__c_node();
130
131    virtual bool __dereferenceable(const void*) const = 0;
132    virtual bool __decrementable(const void*) const = 0;
133    virtual bool __addable(const void*, ptrdiff_t) const = 0;
134    virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
135
136    void __add(__i_node* __i);
137    _LIBCPP_HIDDEN void __remove(__i_node* __i);
138};
139
140template <class _Cont>
141struct _C_node
142    : public __c_node
143{
144    _C_node(void* __c, __c_node* __n)
145        : __c_node(__c, __n) {}
146
147    virtual bool __dereferenceable(const void*) const;
148    virtual bool __decrementable(const void*) const;
149    virtual bool __addable(const void*, ptrdiff_t) const;
150    virtual bool __subscriptable(const void*, ptrdiff_t) const;
151};
152
153template <class _Cont>
154inline bool
155_C_node<_Cont>::__dereferenceable(const void* __i) const
156{
157    typedef typename _Cont::const_iterator iterator;
158    const iterator* __j = static_cast<const iterator*>(__i);
159    _Cont* _Cp = static_cast<_Cont*>(__c_);
160    return _Cp->__dereferenceable(__j);
161}
162
163template <class _Cont>
164inline bool
165_C_node<_Cont>::__decrementable(const void* __i) const
166{
167    typedef typename _Cont::const_iterator iterator;
168    const iterator* __j = static_cast<const iterator*>(__i);
169    _Cont* _Cp = static_cast<_Cont*>(__c_);
170    return _Cp->__decrementable(__j);
171}
172
173template <class _Cont>
174inline bool
175_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const
176{
177    typedef typename _Cont::const_iterator iterator;
178    const iterator* __j = static_cast<const iterator*>(__i);
179    _Cont* _Cp = static_cast<_Cont*>(__c_);
180    return _Cp->__addable(__j, __n);
181}
182
183template <class _Cont>
184inline bool
185_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const
186{
187    typedef typename _Cont::const_iterator iterator;
188    const iterator* __j = static_cast<const iterator*>(__i);
189    _Cont* _Cp = static_cast<_Cont*>(__c_);
190    return _Cp->__subscriptable(__j, __n);
191}
192
193class _LIBCPP_TYPE_VIS __libcpp_db
194{
195    __c_node** __cbeg_;
196    __c_node** __cend_;
197    size_t   __csz_;
198    __i_node** __ibeg_;
199    __i_node** __iend_;
200    size_t   __isz_;
201
202    __libcpp_db();
203public:
204#ifndef _LIBCPP_CXX03_LANG
205    __libcpp_db(const __libcpp_db&) = delete;
206    __libcpp_db& operator=(const __libcpp_db&) = delete;
207#else
208private:
209    __libcpp_db(const __libcpp_db&);
210    __libcpp_db& operator=(const __libcpp_db&);
211public:
212#endif
213    ~__libcpp_db();
214
215    class __db_c_iterator;
216    class __db_c_const_iterator;
217    class __db_i_iterator;
218    class __db_i_const_iterator;
219
220    __db_c_const_iterator __c_end() const;
221    __db_i_const_iterator __i_end() const;
222
223    typedef __c_node*(_InsertConstruct)(void*, void*, __c_node*);
224
225    template <class _Cont>
226    _LIBCPP_INLINE_VISIBILITY static __c_node* __create_C_node(void *__mem, void *__c, __c_node *__next) {
227        return ::new(__mem) _C_node<_Cont>(__c, __next);
228    }
229
230    template <class _Cont>
231    _LIBCPP_INLINE_VISIBILITY
232    void __insert_c(_Cont* __c)
233    {
234        __insert_c(static_cast<void*>(__c), &__create_C_node<_Cont>);
235    }
236
237    void __insert_i(void* __i);
238    void __insert_c(void* __c, _InsertConstruct* __fn);
239    void __erase_c(void* __c);
240
241    void __insert_ic(void* __i, const void* __c);
242    void __iterator_copy(void* __i, const void* __i0);
243    void __erase_i(void* __i);
244
245    void* __find_c_from_i(void* __i) const;
246    void __invalidate_all(void* __c);
247    __c_node* __find_c_and_lock(void* __c) const;
248    __c_node* __find_c(void* __c) const;
249    void unlock() const;
250
251    void swap(void* __c1, void* __c2);
252
253
254    bool __dereferenceable(const void* __i) const;
255    bool __decrementable(const void* __i) const;
256    bool __addable(const void* __i, ptrdiff_t __n) const;
257    bool __subscriptable(const void* __i, ptrdiff_t __n) const;
258    bool __less_than_comparable(const void* __i, const void* __j) const;
259private:
260    _LIBCPP_HIDDEN
261    __i_node* __insert_iterator(void* __i);
262    _LIBCPP_HIDDEN
263    __i_node* __find_iterator(const void* __i) const;
264
265    friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
266};
267
268_LIBCPP_FUNC_VIS __libcpp_db* __get_db();
269_LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
270
271
272#endif // _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY)
273
274_LIBCPP_END_NAMESPACE_STD
275
276#endif  // _LIBCPP_DEBUG_H
277