xref: /llvm-project/libcxxabi/src/private_typeinfo.h (revision d5f84e6121f0d0cc8984dccc1774ce9ddb7168c4)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef __PRIVATE_TYPEINFO_H_
10 #define __PRIVATE_TYPEINFO_H_
11 
12 #include "__cxxabi_config.h"
13 
14 #include <typeinfo>
15 #include <stddef.h>
16 
17 namespace __cxxabiv1 {
18 
19 class _LIBCXXABI_TYPE_VIS __shim_type_info : public std::type_info {
20 public:
21   _LIBCXXABI_HIDDEN virtual ~__shim_type_info();
22 
23   _LIBCXXABI_HIDDEN virtual void noop1() const;
24   _LIBCXXABI_HIDDEN virtual void noop2() const;
25   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *thrown_type,
26                                            void *&adjustedPtr) const = 0;
27 };
28 
29 class _LIBCXXABI_TYPE_VIS __fundamental_type_info : public __shim_type_info {
30 public:
31   _LIBCXXABI_HIDDEN virtual ~__fundamental_type_info();
32   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
33                                            void *&) const;
34 };
35 
36 class _LIBCXXABI_TYPE_VIS __array_type_info : public __shim_type_info {
37 public:
38   _LIBCXXABI_HIDDEN virtual ~__array_type_info();
39   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
40                                            void *&) const;
41 };
42 
43 class _LIBCXXABI_TYPE_VIS __function_type_info : public __shim_type_info {
44 public:
45   _LIBCXXABI_HIDDEN virtual ~__function_type_info();
46   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
47                                            void *&) const;
48 };
49 
50 class _LIBCXXABI_TYPE_VIS __enum_type_info : public __shim_type_info {
51 public:
52   _LIBCXXABI_HIDDEN virtual ~__enum_type_info();
53   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
54                                            void *&) const;
55 };
56 
57 enum
58 {
59     unknown = 0,
60     public_path,
61     not_public_path,
62     yes,
63     no
64 };
65 
66 class _LIBCXXABI_TYPE_VIS __class_type_info;
67 
68 struct _LIBCXXABI_HIDDEN __dynamic_cast_info
69 {
70 // const data supplied to the search:
71 
72     const __class_type_info* dst_type;
73     const void* static_ptr;
74     const __class_type_info* static_type;
75     ptrdiff_t src2dst_offset;
76 
77 // Data that represents the answer:
78 
79     // pointer to a dst_type which has (static_ptr, static_type) above it
80     const void* dst_ptr_leading_to_static_ptr;
81     // pointer to a dst_type which does not have (static_ptr, static_type) above it
82     const void* dst_ptr_not_leading_to_static_ptr;
83 
84     // The following three paths are either unknown, public_path or not_public_path.
85     // access of path from dst_ptr_leading_to_static_ptr to (static_ptr, static_type)
86     int path_dst_ptr_to_static_ptr;
87     // access of path from (dynamic_ptr, dynamic_type) to (static_ptr, static_type)
88     //    when there is no dst_type along the path
89     int path_dynamic_ptr_to_static_ptr;
90     // access of path from (dynamic_ptr, dynamic_type) to dst_type
91     //    (not used if there is a (static_ptr, static_type) above a dst_type).
92     int path_dynamic_ptr_to_dst_ptr;
93 
94     // Number of dst_types below (static_ptr, static_type)
95     int number_to_static_ptr;
96     // Number of dst_types not below (static_ptr, static_type)
97     int number_to_dst_ptr;
98 
99 // Data that helps stop the search before the entire tree is searched:
100 
101     // is_dst_type_derived_from_static_type is either unknown, yes or no.
102     int is_dst_type_derived_from_static_type;
103     // Number of dst_type in tree.  If 0, then that means unknown.
104     int number_of_dst_type;
105     // communicates to a dst_type node that (static_ptr, static_type) was found
106     //    above it.
107     bool found_our_static_ptr;
108     // communicates to a dst_type node that a static_type was found
109     //    above it, but it wasn't (static_ptr, static_type)
110     bool found_any_static_type;
111     // Set whenever a search can be stopped
112     bool search_done;
113 
114     // Data that modifies the search mechanism.
115 
116     // There is no object (seen when we throw a null pointer to object).
117     bool have_object;
118     // Virtual base
119     const void* vbase_cookie;
120 };
121 
122 // Has no base class
123 class _LIBCXXABI_TYPE_VIS __class_type_info : public __shim_type_info {
124 public:
125   _LIBCXXABI_HIDDEN virtual ~__class_type_info();
126 
127   _LIBCXXABI_HIDDEN void process_static_type_above_dst(__dynamic_cast_info *,
128                                                        const void *,
129                                                        const void *, int) const;
130   _LIBCXXABI_HIDDEN void process_static_type_below_dst(__dynamic_cast_info *,
131                                                        const void *, int) const;
132   _LIBCXXABI_HIDDEN void process_found_base_class(__dynamic_cast_info *, void *,
133                                                   int) const;
134   _LIBCXXABI_HIDDEN virtual void search_above_dst(__dynamic_cast_info *,
135                                                   const void *, const void *,
136                                                   int, bool) const;
137   _LIBCXXABI_HIDDEN virtual void
138   search_below_dst(__dynamic_cast_info *, const void *, int, bool) const;
139   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
140                                            void *&) const;
141   _LIBCXXABI_HIDDEN virtual void
142   has_unambiguous_public_base(__dynamic_cast_info *, void *, int) const;
143 };
144 
145 // Has one non-virtual public base class at offset zero
146 class _LIBCXXABI_TYPE_VIS __si_class_type_info : public __class_type_info {
147 public:
148   const __class_type_info *__base_type;
149 
150   _LIBCXXABI_HIDDEN virtual ~__si_class_type_info();
151 
152   _LIBCXXABI_HIDDEN virtual void search_above_dst(__dynamic_cast_info *,
153                                                   const void *, const void *,
154                                                   int, bool) const;
155   _LIBCXXABI_HIDDEN virtual void
156   search_below_dst(__dynamic_cast_info *, const void *, int, bool) const;
157   _LIBCXXABI_HIDDEN virtual void
158   has_unambiguous_public_base(__dynamic_cast_info *, void *, int) const;
159 };
160 
161 struct _LIBCXXABI_HIDDEN __base_class_type_info
162 {
163 public:
164     const __class_type_info* __base_type;
165     long __offset_flags;
166 
167     enum __offset_flags_masks
168     {
169         __virtual_mask = 0x1,
170         __public_mask  = 0x2, // base is public
171         __offset_shift = 8
172     };
173 
174     void search_above_dst(__dynamic_cast_info*, const void*, const void*, int, bool) const;
175     void search_below_dst(__dynamic_cast_info*, const void*, int, bool) const;
176     void has_unambiguous_public_base(__dynamic_cast_info*, void*, int) const;
177 };
178 
179 // Has one or more base classes
180 class _LIBCXXABI_TYPE_VIS __vmi_class_type_info : public __class_type_info {
181 public:
182   unsigned int __flags;
183   unsigned int __base_count;
184   __base_class_type_info __base_info[1];
185 
186   enum __flags_masks {
187     __non_diamond_repeat_mask = 0x1, // has two or more distinct base class
188                                      //    objects of the same type
189     __diamond_shaped_mask = 0x2      // has base class object with two or
190                                      //    more derived objects
191   };
192 
193   _LIBCXXABI_HIDDEN virtual ~__vmi_class_type_info();
194 
195   _LIBCXXABI_HIDDEN virtual void search_above_dst(__dynamic_cast_info *,
196                                                   const void *, const void *,
197                                                   int, bool) const;
198   _LIBCXXABI_HIDDEN virtual void
199   search_below_dst(__dynamic_cast_info *, const void *, int, bool) const;
200   _LIBCXXABI_HIDDEN virtual void
201   has_unambiguous_public_base(__dynamic_cast_info *, void *, int) const;
202 };
203 
204 class _LIBCXXABI_TYPE_VIS __pbase_type_info : public __shim_type_info {
205 public:
206   unsigned int __flags;
207   const __shim_type_info *__pointee;
208 
209   enum __masks {
210     __const_mask = 0x1,
211     __volatile_mask = 0x2,
212     __restrict_mask = 0x4,
213     __incomplete_mask = 0x8,
214     __incomplete_class_mask = 0x10,
215     __transaction_safe_mask = 0x20,
216     // This implements the following proposal from cxx-abi-dev (not yet part of
217     // the ABI document):
218     //
219     //   http://sourcerytools.com/pipermail/cxx-abi-dev/2016-October/002986.html
220     //
221     // This is necessary for support of http://wg21.link/p0012, which permits
222     // throwing noexcept function and member function pointers and catching
223     // them as non-noexcept pointers.
224     __noexcept_mask = 0x40,
225 
226     // Flags that cannot be removed by a standard conversion.
227     __no_remove_flags_mask = __const_mask | __volatile_mask | __restrict_mask,
228     // Flags that cannot be added by a standard conversion.
229     __no_add_flags_mask = __transaction_safe_mask | __noexcept_mask
230   };
231 
232   _LIBCXXABI_HIDDEN virtual ~__pbase_type_info();
233   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
234                                            void *&) const;
235 };
236 
237 class _LIBCXXABI_TYPE_VIS __pointer_type_info : public __pbase_type_info {
238 public:
239   _LIBCXXABI_HIDDEN virtual ~__pointer_type_info();
240   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
241                                            void *&) const;
242   _LIBCXXABI_HIDDEN bool can_catch_nested(const __shim_type_info *) const;
243 };
244 
245 class _LIBCXXABI_TYPE_VIS __pointer_to_member_type_info
246     : public __pbase_type_info {
247 public:
248   const __class_type_info *__context;
249 
250   _LIBCXXABI_HIDDEN virtual ~__pointer_to_member_type_info();
251   _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info *,
252                                            void *&) const;
253   _LIBCXXABI_HIDDEN bool can_catch_nested(const __shim_type_info *) const;
254 };
255 
256 }  // __cxxabiv1
257 
258 #endif // __PRIVATE_TYPEINFO_H_
259