xref: /freebsd/contrib/libcxxrt/dynamic_cast.cc (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 /*
2  * Copyright 2010-2011 PathScale, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "typeinfo.h"
28 #include <stdio.h>
29 
30 using namespace ABI_NAMESPACE;
31 
32 /**
33  * Vtable header.
34  */
35 struct vtable_header
36 {
37 	/** Offset of the leaf object. */
38 	ptrdiff_t leaf_offset;
39 	/** Type of the object. */
40 	const __class_type_info *type;
41 };
42 
43 /**
44  * Simple macro that does pointer arithmetic in bytes but returns a value of
45  * the same type as the original.
46  */
47 #define ADD_TO_PTR(x, off) reinterpret_cast<__typeof__(x)>(reinterpret_cast<char*>(x) + off)
48 
49 bool std::type_info::__do_catch(std::type_info const *ex_type,
50                                 void **exception_object,
51                                 unsigned int outer) const
52 {
53 	const type_info *type = this;
54 
55 	if (type == ex_type)
56 	{
57 		return true;
58 	}
59 	if (const __class_type_info *cti = dynamic_cast<const __class_type_info *>(type))
60 	{
61 		return ex_type->__do_upcast(cti, exception_object);
62 	}
63 	return false;
64 }
65 
66 bool __pbase_type_info::__do_catch(std::type_info const *ex_type,
67                                    void **exception_object,
68                                    unsigned int outer) const
69 {
70 	if (ex_type == this)
71 	{
72 		return true;
73 	}
74 	if (!ex_type->__is_pointer_p())
75 	{
76 		// Can't catch a non-pointer type in a pointer catch
77 		return false;
78 	}
79 
80 	if (!(outer & 1))
81 	{
82 		// If the low bit is cleared on this means that we've gone
83 		// through a pointer that is not const qualified.
84 		return false;
85 	}
86 	// Clear the low bit on outer if we're not const qualified.
87 	if (!(__flags & __const_mask))
88 	{
89 		outer &= ~1;
90 	}
91 
92 	const __pbase_type_info *ptr_type =
93 	        static_cast<const __pbase_type_info*>(ex_type);
94 
95 	if (ptr_type->__flags & ~__flags)
96 	{
97 		// Handler pointer is less qualified
98 		return false;
99 	}
100 
101 	// Special case for void* handler.
102 	if(*__pointee == typeid(void))
103 	{
104 		return true;
105 	}
106 
107 	return __pointee->__do_catch(ptr_type->__pointee, exception_object, outer);
108 }
109 
110 void *__class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
111 {
112 	if (this == other)
113 	{
114 		return obj;
115 	}
116 	return 0;
117 }
118 
119 void *__si_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
120 {
121 	if (this == other)
122 	{
123 		return obj;
124 	}
125 	return __base_type->cast_to(obj, other);
126 }
127 bool __si_class_type_info::__do_upcast(const __class_type_info *target,
128                                      void **thrown_object) const
129 {
130 	if (this == target)
131 	{
132 		return true;
133 	}
134 	return __base_type->__do_upcast(target, thrown_object);
135 }
136 
137 void *__vmi_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
138 {
139 	if (__do_upcast(other, &obj))
140 	{
141 		return obj;
142 	}
143 	return 0;
144 }
145 
146 bool __vmi_class_type_info::__do_upcast(const __class_type_info *target,
147 		                      void **thrown_object) const
148 {
149 	if (this == target)
150 	{
151 		return true;
152 	}
153 	for (unsigned int i=0 ; i<__base_count ; i++)
154 	{
155 		const __base_class_type_info *info = &__base_info[i];
156 		ptrdiff_t offset = info->offset();
157 		// If this is a virtual superclass, the offset is stored in the
158 		// object's vtable at the offset requested; 2.9.5.6.c:
159 		//
160 		// 'For a non-virtual base, this is the offset in the object of the
161 		// base subobject. For a virtual base, this is the offset in the
162 		// virtual table of the virtual base offset for the virtual base
163 		// referenced (negative).'
164 
165 		void *obj = *thrown_object;
166 		if (info->isVirtual())
167 		{
168 			// Object's vtable
169 			ptrdiff_t *off = *static_cast<ptrdiff_t**>(obj);
170 			// Offset location in vtable
171 			off = ADD_TO_PTR(off, offset);
172 			offset = *off;
173 		}
174 		void *cast = ADD_TO_PTR(obj, offset);
175 
176 		if (info->__base_type == target ||
177 		    (info->__base_type->__do_upcast(target, &cast)))
178 		{
179 			*thrown_object = cast;
180 			return true;
181 		}
182 	}
183 	return 0;
184 }
185 
186 
187 /**
188  * ABI function used to implement the dynamic_cast<> operator.  Some cases of
189  * this operator are implemented entirely in the compiler (e.g. to void*).
190  * This function implements the dynamic casts of the form dynamic_cast<T>(v).
191  * This will be translated to a call to this function with the value v as the
192  * first argument.  The type id of the static type of v is the second argument
193  * and the type id of the destination type (T) is the third argument.
194  *
195  * The third argument is a hint about the compiler's guess at the correct
196  * pointer offset.  If this value is negative, then -1 indicates no hint, -2
197  * that src is not a public base of dst, and -3 that src is a multiple public
198  * base type but never a virtual base type
199  */
200 extern "C" void* __dynamic_cast(const void *sub,
201                                 const __class_type_info *src,
202                                 const __class_type_info *dst,
203                                 ptrdiff_t src2dst_offset)
204 {
205 	const char *vtable_location = *static_cast<const char * const *>(sub);
206 	const vtable_header *header =
207 		reinterpret_cast<const vtable_header*>(vtable_location - sizeof(vtable_header));
208 	void *leaf = ADD_TO_PTR(const_cast<void *>(sub), header->leaf_offset);
209 	return header->type->cast_to(leaf, dst);
210 }
211