1 // Copyright (C) 2011 The Android Open Source Project 2 // 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 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 3. Neither the name of the project nor the names of its contributors 13 // may be used to endorse or promote products derived from this software 14 // without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 // ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 // SUCH DAMAGE. 27 // 28 // pbase_type_info.cc: Methods for __pbase_type_info. 29 30 #include "cxxabi_defines.h" 31 #include "typeinfo" 32 33 #if __cplusplus < 201103L 34 extern "C" std::type_info _ZTIDn; 35 #endif 36 37 namespace __cxxabiv1 38 { ~__pbase_type_info()39 __pbase_type_info::~__pbase_type_info() 40 { 41 } 42 can_catch(const __shim_type_info * thr_type,void * & adjustedPtr) const43 bool __pbase_type_info::can_catch(const __shim_type_info* thr_type, 44 void*& adjustedPtr) const { 45 if (can_catch_typeinfo_wrapper(thr_type, adjustedPtr, first_time_init)) { 46 return true; 47 } 48 49 #if __cplusplus >= 201103L 50 // In C++ 11, the type of nullptr is std::nullptr_t, but nullptr can be 51 // casted to every pointer types. Thus, we can return true whenever 52 // the exception object is an instance of std::nullptr_t. 53 return (*thr_type == typeid(decltype(nullptr))); 54 #else 55 return (*thr_type == _ZTIDn); 56 #endif 57 } 58 can_catch_typeinfo_wrapper(const __shim_type_info * thr_type,void * & adjustedPtr,unsigned tracker) const59 bool __pbase_type_info::can_catch_typeinfo_wrapper(const __shim_type_info* thr_type, 60 void*& adjustedPtr, 61 unsigned tracker) const { 62 if (*this == *thr_type) { 63 return true; 64 } 65 66 if (typeid(*this) != typeid(*thr_type)) { 67 return false; 68 } 69 const __pbase_type_info *thrown_type = 70 static_cast<const __pbase_type_info *>(thr_type); 71 72 // Both side must be the same cv-qualified 73 if (~__flags & thrown_type->__flags) { 74 return false; 75 } 76 77 // Handle the annoying constness problem 78 // Ref: http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html 79 if (tracker == first_time_init && 80 (tracker & keep_constness) != keep_constness && 81 (tracker & after_gap) != after_gap) { 82 tracker |= keep_constness; 83 } else { 84 tracker &= ~first_time_init; 85 } 86 87 if ((tracker & first_time_init) != first_time_init && 88 (tracker & after_gap) == after_gap) { 89 return false; 90 } 91 92 if (!(__flags & __const_mask)) { 93 tracker |= after_gap; 94 } 95 96 return can_catch_ptr(thrown_type, adjustedPtr, tracker); 97 } 98 can_catch_ptr(const __pbase_type_info * thrown_type,void * & adjustedPtr,unsigned tracker) const99 bool __pbase_type_info::can_catch_ptr(const __pbase_type_info* thrown_type, 100 void*& adjustedPtr, 101 unsigned tracker) const { 102 bool result; 103 if (do_can_catch_ptr(thrown_type, adjustedPtr, tracker, 104 result)) { 105 return result; 106 } 107 108 const __pbase_type_info* ptr_pointee = 109 dynamic_cast<const __pbase_type_info*>(__pointee); 110 111 if (ptr_pointee) { 112 return ptr_pointee->can_catch_typeinfo_wrapper(thrown_type->__pointee, 113 adjustedPtr, 114 tracker); 115 } else { 116 return __pointee->can_catch(thrown_type->__pointee, adjustedPtr); 117 } 118 } 119 } // namespace __cxxabiv1 120