Remove building with NOCRYPTO option
[minix3.git] / external / bsd / libc++ / dist / libcxxrt / src / dynamic_cast.cc
blob6ae3a4035554ee2cd5903cced47a6aca80b25829
1 /*
2 * Copyright 2010-2011 PathScale, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
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.
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.
27 #include "typeinfo.h"
28 #include <stdio.h>
30 using namespace ABI_NAMESPACE;
32 /**
33 * Vtable header.
35 struct vtable_header
37 /** Offset of the leaf object. */
38 ptrdiff_t leaf_offset;
39 /** Type of the object. */
40 const __class_type_info *type;
43 /**
44 * Simple macro that does pointer arithmetic in bytes but returns a value of
45 * the same type as the original.
47 #define ADD_TO_PTR(x, off) reinterpret_cast<__typeof__(x)>(reinterpret_cast<char*>(x) + off)
49 bool std::type_info::__do_catch(std::type_info const *ex_type,
50 void **exception_object,
51 unsigned int outer) const
53 const type_info *type = this;
55 if (type == ex_type)
57 return true;
59 if (const __class_type_info *cti = dynamic_cast<const __class_type_info *>(type))
61 return ex_type->__do_upcast(cti, exception_object);
63 return false;
66 bool __pbase_type_info::__do_catch(std::type_info const *ex_type,
67 void **exception_object,
68 unsigned int outer) const
70 if (ex_type == this)
72 return true;
74 if (!ex_type->__is_pointer_p())
76 // Can't catch a non-pointer type in a pointer catch
77 return false;
80 if (!(outer & 1))
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;
86 // Clear the low bit on outer if we're not const qualified.
87 if (!(__flags & __const_mask))
89 outer &= ~1;
92 const __pbase_type_info *ptr_type =
93 static_cast<const __pbase_type_info*>(ex_type);
95 if (ptr_type->__flags & ~__flags)
97 // Handler pointer is less qualified
98 return false;
101 // Special case for void* handler.
102 if(*__pointee == typeid(void))
104 return true;
107 return __pointee->__do_catch(ptr_type->__pointee, exception_object, outer);
110 void *__class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
112 if (this == other)
114 return obj;
116 return 0;
119 void *__si_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
121 if (this == other)
123 return obj;
125 return __base_type->cast_to(obj, other);
127 bool __si_class_type_info::__do_upcast(const __class_type_info *target,
128 void **thrown_object) const
130 if (this == target)
132 return true;
134 return __base_type->__do_upcast(target, thrown_object);
137 void *__vmi_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const
139 if (__do_upcast(other, &obj))
141 return obj;
143 return 0;
146 bool __vmi_class_type_info::__do_upcast(const __class_type_info *target,
147 void **thrown_object) const
149 if (this == target)
151 return true;
153 for (unsigned int i=0 ; i<__base_count ; i++)
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:
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).'
165 void *obj = *thrown_object;
166 if (info->isVirtual())
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;
174 void *cast = ADD_TO_PTR(obj, offset);
176 if (info->__base_type == target ||
177 (info->__base_type->__do_upcast(target, &cast)))
179 *thrown_object = cast;
180 return true;
183 return 0;
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.
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
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)
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);