dynamic_cast.cc (232950) | dynamic_cast.cc (233235) |
---|---|
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. --- 32 unchanged lines hidden (view full) --- 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) (__typeof__(x))(((char*)x) + off) 48 | 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. --- 32 unchanged lines hidden (view full) --- 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) (__typeof__(x))(((char*)x) + off) 48 |
49bool __class_type_info::can_cast_to(const struct __class_type_info *other) const | 49bool std::type_info::__do_catch(std::type_info const *ex_type, 50 void **exception_object, 51 unsigned int outer) const |
50{ | 52{ |
51 return this == other; | 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; |
52} 53 | 64} 65 |
66bool __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 |
|
54void *__class_type_info::cast_to(void *obj, const struct __class_type_info *other) const 55{ 56 if (this == other) 57 { 58 return obj; 59 } 60 return 0; 61} 62 | 110void *__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 |
63 64bool __si_class_type_info::can_cast_to(const struct __class_type_info *other) const 65{ 66 return this == other || __base_type->can_cast_to(other); 67} 68 | |
69void *__si_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const 70{ 71 if (this == other) 72 { 73 return obj; 74 } 75 return __base_type->cast_to(obj, other); 76} | 119void *__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} |
77 78 79bool __vmi_class_type_info::can_cast_to(const struct __class_type_info *other) const | 127bool __si_class_type_info::__do_upcast(const __class_type_info *target, 128 void **thrown_object) const |
80{ | 129{ |
81 if (this == other) | 130 if (this == target) |
82 { 83 return true; 84 } | 131 { 132 return true; 133 } |
85 for (unsigned int i=0 ; i<__base_count ; i++) 86 { 87 const __base_class_type_info *info = &__base_info[i]; 88 if(info->isPublic() && info->__base_type->can_cast_to(other)) 89 { 90 return true; 91 } 92 } 93 return false; | 134 return __base_type->__do_upcast(target, thrown_object); |
94} 95 96void *__vmi_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const 97{ | 135} 136 137void *__vmi_class_type_info::cast_to(void *obj, const struct __class_type_info *other) const 138{ |
98 if (this == other) | 139 if (__do_upcast(other, &obj)) |
99 { 100 return obj; 101 } | 140 { 141 return obj; 142 } |
143 return 0; 144} 145 146bool __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 } |
|
102 for (unsigned int i=0 ; i<__base_count ; i++) 103 { 104 const __base_class_type_info *info = &__base_info[i]; 105 ptrdiff_t offset = info->offset(); 106 // If this is a virtual superclass, the offset is stored in the 107 // object's vtable at the offset requested; 2.9.5.6.c: 108 // 109 // 'For a non-virtual base, this is the offset in the object of the 110 // base subobject. For a virtual base, this is the offset in the 111 // virtual table of the virtual base offset for the virtual base 112 // referenced (negative).' 113 | 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; |
|
114 if (info->isVirtual()) 115 { 116 // Object's vtable 117 ptrdiff_t *off = *(ptrdiff_t**)obj; 118 // Offset location in vtable 119 off = ADD_TO_PTR(off, offset); 120 offset = *off; 121 } 122 void *cast = ADD_TO_PTR(obj, offset); 123 | 166 if (info->isVirtual()) 167 { 168 // Object's vtable 169 ptrdiff_t *off = *(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 |
124 if (info->__base_type == other) | 176 if (info->__base_type == target || 177 (info->__base_type->__do_upcast(target, &cast))) |
125 { | 178 { |
126 return cast; | 179 *thrown_object = cast; 180 return true; |
127 } | 181 } |
128 if ((cast = info->__base_type->cast_to(cast, other))) 129 { 130 return cast; 131 } | |
132 } 133 return 0; 134} 135 | 182 } 183 return 0; 184} 185 |
186 |
|
136/** 137 * ABI function used to implement the dynamic_cast<> operator. Some cases of 138 * this operator are implemented entirely in the compiler (e.g. to void*). 139 * This function implements the dynamic casts of the form dynamic_cast<T>(v). 140 * This will be translated to a call to this function with the value v as the 141 * first argument. The type id of the static type of v is the second argument 142 * and the type id of the destination type (T) is the third argument. 143 * --- 16 unchanged lines hidden --- | 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 * --- 16 unchanged lines hidden --- |