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