194e3ee44SDavid Chisnall /*
294e3ee44SDavid Chisnall * Copyright 2010-2011 PathScale, Inc. All rights reserved.
394e3ee44SDavid Chisnall *
494e3ee44SDavid Chisnall * Redistribution and use in source and binary forms, with or without
594e3ee44SDavid Chisnall * modification, are permitted provided that the following conditions are met:
694e3ee44SDavid Chisnall *
794e3ee44SDavid Chisnall * 1. Redistributions of source code must retain the above copyright notice,
894e3ee44SDavid Chisnall * this list of conditions and the following disclaimer.
994e3ee44SDavid Chisnall *
1094e3ee44SDavid Chisnall * 2. Redistributions in binary form must reproduce the above copyright notice,
1194e3ee44SDavid Chisnall * this list of conditions and the following disclaimer in the documentation
1294e3ee44SDavid Chisnall * and/or other materials provided with the distribution.
1394e3ee44SDavid Chisnall *
1494e3ee44SDavid Chisnall * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
1594e3ee44SDavid Chisnall * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1694e3ee44SDavid Chisnall * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1794e3ee44SDavid Chisnall * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
1894e3ee44SDavid Chisnall * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1994e3ee44SDavid Chisnall * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2094e3ee44SDavid Chisnall * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2194e3ee44SDavid Chisnall * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2294e3ee44SDavid Chisnall * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2394e3ee44SDavid Chisnall * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2494e3ee44SDavid Chisnall * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2594e3ee44SDavid Chisnall */
2694e3ee44SDavid Chisnall
277a984708SDavid Chisnall #include "typeinfo.h"
287a984708SDavid Chisnall #include <stdio.h>
297a984708SDavid Chisnall
307a984708SDavid Chisnall using namespace ABI_NAMESPACE;
317a984708SDavid Chisnall
327a984708SDavid Chisnall /**
337a984708SDavid Chisnall * Vtable header.
347a984708SDavid Chisnall */
357a984708SDavid Chisnall struct vtable_header
367a984708SDavid Chisnall {
377a984708SDavid Chisnall /** Offset of the leaf object. */
387a984708SDavid Chisnall ptrdiff_t leaf_offset;
397a984708SDavid Chisnall /** Type of the object. */
407a984708SDavid Chisnall const __class_type_info *type;
417a984708SDavid Chisnall };
427a984708SDavid Chisnall
437a984708SDavid Chisnall /**
447a984708SDavid Chisnall * Simple macro that does pointer arithmetic in bytes but returns a value of
457a984708SDavid Chisnall * the same type as the original.
467a984708SDavid Chisnall */
47*f2dc4184SDimitry Andric #define ADD_TO_PTR(x, off) reinterpret_cast<__typeof__(x)>(reinterpret_cast<char*>(x) + off)
487a984708SDavid Chisnall
__do_catch(std::type_info const * ex_type,void ** exception_object,unsigned int outer) const49c725650dSDavid Chisnall bool std::type_info::__do_catch(std::type_info const *ex_type,
50c725650dSDavid Chisnall void **exception_object,
51c725650dSDavid Chisnall unsigned int outer) const
527a984708SDavid Chisnall {
53c725650dSDavid Chisnall const type_info *type = this;
54c725650dSDavid Chisnall
55c725650dSDavid Chisnall if (type == ex_type)
56c725650dSDavid Chisnall {
57c725650dSDavid Chisnall return true;
58c725650dSDavid Chisnall }
59c725650dSDavid Chisnall if (const __class_type_info *cti = dynamic_cast<const __class_type_info *>(type))
60c725650dSDavid Chisnall {
61c725650dSDavid Chisnall return ex_type->__do_upcast(cti, exception_object);
62c725650dSDavid Chisnall }
63c725650dSDavid Chisnall return false;
64c725650dSDavid Chisnall }
65c725650dSDavid Chisnall
__do_catch(std::type_info const * ex_type,void ** exception_object,unsigned int outer) const66c725650dSDavid Chisnall bool __pbase_type_info::__do_catch(std::type_info const *ex_type,
67c725650dSDavid Chisnall void **exception_object,
68c725650dSDavid Chisnall unsigned int outer) const
69c725650dSDavid Chisnall {
70c725650dSDavid Chisnall if (ex_type == this)
71c725650dSDavid Chisnall {
72c725650dSDavid Chisnall return true;
73c725650dSDavid Chisnall }
74c725650dSDavid Chisnall if (!ex_type->__is_pointer_p())
75c725650dSDavid Chisnall {
76c725650dSDavid Chisnall // Can't catch a non-pointer type in a pointer catch
77c725650dSDavid Chisnall return false;
78c725650dSDavid Chisnall }
79c725650dSDavid Chisnall
80c725650dSDavid Chisnall if (!(outer & 1))
81c725650dSDavid Chisnall {
82c725650dSDavid Chisnall // If the low bit is cleared on this means that we've gone
83c725650dSDavid Chisnall // through a pointer that is not const qualified.
84c725650dSDavid Chisnall return false;
85c725650dSDavid Chisnall }
86c725650dSDavid Chisnall // Clear the low bit on outer if we're not const qualified.
87c725650dSDavid Chisnall if (!(__flags & __const_mask))
88c725650dSDavid Chisnall {
89c725650dSDavid Chisnall outer &= ~1;
90c725650dSDavid Chisnall }
91c725650dSDavid Chisnall
92c725650dSDavid Chisnall const __pbase_type_info *ptr_type =
93c725650dSDavid Chisnall static_cast<const __pbase_type_info*>(ex_type);
94c725650dSDavid Chisnall
95c725650dSDavid Chisnall if (ptr_type->__flags & ~__flags)
96c725650dSDavid Chisnall {
97c725650dSDavid Chisnall // Handler pointer is less qualified
98c725650dSDavid Chisnall return false;
99c725650dSDavid Chisnall }
100c725650dSDavid Chisnall
101c725650dSDavid Chisnall // Special case for void* handler.
102c725650dSDavid Chisnall if(*__pointee == typeid(void))
103c725650dSDavid Chisnall {
104c725650dSDavid Chisnall return true;
105c725650dSDavid Chisnall }
106c725650dSDavid Chisnall
107c725650dSDavid Chisnall return __pointee->__do_catch(ptr_type->__pointee, exception_object, outer);
1087a984708SDavid Chisnall }
1097a984708SDavid Chisnall
cast_to(void * obj,const struct __class_type_info * other) const1107a984708SDavid Chisnall void *__class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
1117a984708SDavid Chisnall {
1127a984708SDavid Chisnall if (this == other)
1137a984708SDavid Chisnall {
1147a984708SDavid Chisnall return obj;
1157a984708SDavid Chisnall }
1167a984708SDavid Chisnall return 0;
1177a984708SDavid Chisnall }
1187a984708SDavid Chisnall
cast_to(void * obj,const struct __class_type_info * other) const1197a984708SDavid Chisnall void *__si_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
1207a984708SDavid Chisnall {
1217a984708SDavid Chisnall if (this == other)
1227a984708SDavid Chisnall {
1237a984708SDavid Chisnall return obj;
1247a984708SDavid Chisnall }
1257a984708SDavid Chisnall return __base_type->cast_to(obj, other);
1267a984708SDavid Chisnall }
__do_upcast(const __class_type_info * target,void ** thrown_object) const127c725650dSDavid Chisnall bool __si_class_type_info::__do_upcast(const __class_type_info *target,
128c725650dSDavid Chisnall void **thrown_object) const
1297a984708SDavid Chisnall {
130c725650dSDavid Chisnall if (this == target)
1317a984708SDavid Chisnall {
1327a984708SDavid Chisnall return true;
1337a984708SDavid Chisnall }
134c725650dSDavid Chisnall return __base_type->__do_upcast(target, thrown_object);
1357a984708SDavid Chisnall }
1367a984708SDavid Chisnall
cast_to(void * obj,const struct __class_type_info * other) const1377a984708SDavid Chisnall void *__vmi_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
1387a984708SDavid Chisnall {
139c725650dSDavid Chisnall if (__do_upcast(other, &obj))
1407a984708SDavid Chisnall {
1417a984708SDavid Chisnall return obj;
1427a984708SDavid Chisnall }
143c725650dSDavid Chisnall return 0;
144c725650dSDavid Chisnall }
145c725650dSDavid Chisnall
__do_upcast(const __class_type_info * target,void ** thrown_object) const146c725650dSDavid Chisnall bool __vmi_class_type_info::__do_upcast(const __class_type_info *target,
147c725650dSDavid Chisnall void **thrown_object) const
148c725650dSDavid Chisnall {
149c725650dSDavid Chisnall if (this == target)
150c725650dSDavid Chisnall {
151c725650dSDavid Chisnall return true;
152c725650dSDavid Chisnall }
1537a984708SDavid Chisnall for (unsigned int i=0 ; i<__base_count ; i++)
1547a984708SDavid Chisnall {
1557a984708SDavid Chisnall const __base_class_type_info *info = &__base_info[i];
1567a984708SDavid Chisnall ptrdiff_t offset = info->offset();
1577a984708SDavid Chisnall // If this is a virtual superclass, the offset is stored in the
1587a984708SDavid Chisnall // object's vtable at the offset requested; 2.9.5.6.c:
1597a984708SDavid Chisnall //
1607a984708SDavid Chisnall // 'For a non-virtual base, this is the offset in the object of the
1617a984708SDavid Chisnall // base subobject. For a virtual base, this is the offset in the
1627a984708SDavid Chisnall // virtual table of the virtual base offset for the virtual base
1637a984708SDavid Chisnall // referenced (negative).'
1647a984708SDavid Chisnall
165c725650dSDavid Chisnall void *obj = *thrown_object;
1667a984708SDavid Chisnall if (info->isVirtual())
1677a984708SDavid Chisnall {
1687a984708SDavid Chisnall // Object's vtable
169*f2dc4184SDimitry Andric ptrdiff_t *off = *static_cast<ptrdiff_t**>(obj);
1707a984708SDavid Chisnall // Offset location in vtable
1717a984708SDavid Chisnall off = ADD_TO_PTR(off, offset);
1727a984708SDavid Chisnall offset = *off;
1737a984708SDavid Chisnall }
1747a984708SDavid Chisnall void *cast = ADD_TO_PTR(obj, offset);
1757a984708SDavid Chisnall
176c725650dSDavid Chisnall if (info->__base_type == target ||
177c725650dSDavid Chisnall (info->__base_type->__do_upcast(target, &cast)))
1787a984708SDavid Chisnall {
179c725650dSDavid Chisnall *thrown_object = cast;
180c725650dSDavid Chisnall return true;
1817a984708SDavid Chisnall }
1827a984708SDavid Chisnall }
1837a984708SDavid Chisnall return 0;
1847a984708SDavid Chisnall }
1857a984708SDavid Chisnall
186c725650dSDavid Chisnall
1877a984708SDavid Chisnall /**
1887a984708SDavid Chisnall * ABI function used to implement the dynamic_cast<> operator. Some cases of
1897a984708SDavid Chisnall * this operator are implemented entirely in the compiler (e.g. to void*).
1907a984708SDavid Chisnall * This function implements the dynamic casts of the form dynamic_cast<T>(v).
1917a984708SDavid Chisnall * This will be translated to a call to this function with the value v as the
1927a984708SDavid Chisnall * first argument. The type id of the static type of v is the second argument
1937a984708SDavid Chisnall * and the type id of the destination type (T) is the third argument.
1947a984708SDavid Chisnall *
1957a984708SDavid Chisnall * The third argument is a hint about the compiler's guess at the correct
1967a984708SDavid Chisnall * pointer offset. If this value is negative, then -1 indicates no hint, -2
1977a984708SDavid Chisnall * that src is not a public base of dst, and -3 that src is a multiple public
1987a984708SDavid Chisnall * base type but never a virtual base type
1997a984708SDavid Chisnall */
__dynamic_cast(const void * sub,const __class_type_info * src,const __class_type_info * dst,ptrdiff_t src2dst_offset)2007a984708SDavid Chisnall extern "C" void* __dynamic_cast(const void *sub,
2017a984708SDavid Chisnall const __class_type_info *src,
2027a984708SDavid Chisnall const __class_type_info *dst,
2037a984708SDavid Chisnall ptrdiff_t src2dst_offset)
2047a984708SDavid Chisnall {
205*f2dc4184SDimitry Andric const char *vtable_location = *static_cast<const char * const *>(sub);
2067a984708SDavid Chisnall const vtable_header *header =
207*f2dc4184SDimitry Andric reinterpret_cast<const vtable_header*>(vtable_location - sizeof(vtable_header));
208*f2dc4184SDimitry Andric void *leaf = ADD_TO_PTR(const_cast<void *>(sub), header->leaf_offset);
2097a984708SDavid Chisnall return header->type->cast_to(leaf, dst);
2107a984708SDavid Chisnall }
211