// Call this function when searching below a dst_type node. This function searches // for a path to (static_ptr, static_type) and for paths to one or more dst_type nodes. // If it finds a static_type node, there is no need to further search base classes // above. // If it finds a dst_type node it should search base classes using search_above_dst // to find out if this dst_type points to (static_ptr, static_type) or not. // Either way, the dst_type is recorded as one of two "flavors": one that does // or does not point to (static_ptr, static_type). // If this is neither a static_type nor a dst_type node, continue searching // base classes above. // All the hoopla surrounding the search code is doing nothing but looking for // excuses to stop the search prematurely (break out of the for-loop). That is, // the algorithm below is simply an optimization of this: // void // __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info, // const void* current_ptr, // int path_below) const // { // typedef const __base_class_type_info* Iter; // if (this == info->static_type) // process_static_type_below_dst(info, current_ptr, path_below); // else if (this == info->dst_type) // { // // Record the most public access path that got us here // if (info->path_dynamic_ptr_to_dst_ptr != public_path) // info->path_dynamic_ptr_to_dst_ptr = path_below; // bool does_dst_type_point_to_our_static_type = false; // for (Iter p = __base_info, e= __base_info + __base_count; p < e; ++p) // { // p->search_above_dst(info, current_ptr, current_ptr, public_path); // if (info->found_our_static_ptr) // does_dst_type_point_to_our_static_type = true; // // break out early here if you can detect it doesn't matter if you do // } // if (!does_dst_type_point_to_our_static_type) // { // // We found a dst_type that doesn't point to (static_ptr, static_type) // // So record the address of this dst_ptr and increment the // // count of the number of such dst_types found in the tree. // info->dst_ptr_not_leading_to_static_ptr = current_ptr; // info->number_to_dst_ptr += 1; // } // } // else // { // // This is not a static_type and not a dst_type. // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p) // { // p->search_below_dst(info, current_ptr, public_path); // // break out early here if you can detect it doesn't matter if you do // } // } // } void __vmi_class_type_info::search_below_dst(__dynamic_cast_info* info, const void* current_ptr, int path_below, bool use_strcmp) const { typedef const __base_class_type_info* Iter; if (is_equal(this, info->static_type, use_strcmp)) process_static_type_below_dst(info, current_ptr, path_below); else if (is_equal(this, info->dst_type, use_strcmp)) { // We've been here before if we've recorded current_ptr in one of these // two places: if (current_ptr == info->dst_ptr_leading_to_static_ptr || current_ptr == info->dst_ptr_not_leading_to_static_ptr) { // We've seen this node before, and therefore have already searched // its base classes above. // Update path to here that is "most public". if (path_below == public_path) info->path_dynamic_ptr_to_dst_ptr = public_path; } else // We have haven't been here before { // Record the access path that got us here // If there is more than one dst_type this path doesn't matter. info->path_dynamic_ptr_to_dst_ptr = path_below; // Only search above here if dst_type derives from static_type, or // if it is unknown if dst_type derives from static_type. if (info->is_dst_type_derived_from_static_type != no) { // Set up flags to record results from all base classes bool is_dst_type_derived_from_static_type = false; bool does_dst_type_point_to_our_static_type = false; // We've found a dst_type with a potentially public path to here. // We have to assume the path is public because it may become // public later (if we get back to here with a public path). // We can stop looking above if: // 1. We've found a public path to (static_ptr, static_type). // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type. // This is detected at the (static_ptr, static_type). // 3. We can prove that there is no public path to (static_ptr, static_type) // above here. const Iter e = __base_info + __base_count; for (Iter p = __base_info; p < e; ++p) { // Zero out found flags info->found_our_static_ptr = false; info->found_any_static_type = false; p->search_above_dst(info, current_ptr, current_ptr, public_path, use_strcmp); if (info->search_done) break; if (info->found_any_static_type) { is_dst_type_derived_from_static_type = true; if (info->found_our_static_ptr) { does_dst_type_point_to_our_static_type = true; // If we found what we're looking for, stop looking above. if (info->path_dst_ptr_to_static_ptr == public_path) break; // We found a private path to (static_ptr, static_type) // If there is no diamond then there is only one path // to (static_ptr, static_type) and we just found it. if (!(__flags & __diamond_shaped_mask)) break; } else { // If we found a static_type that isn't the one we're looking // for, and if there are no repeated types above here, // then stop looking. if (!(__flags & __non_diamond_repeat_mask)) break; } } } if (!does_dst_type_point_to_our_static_type) { // We found a dst_type that doesn't point to (static_ptr, static_type) // So record the address of this dst_ptr and increment the // count of the number of such dst_types found in the tree. info->dst_ptr_not_leading_to_static_ptr = current_ptr; info->number_to_dst_ptr += 1; // If there exists another dst with a private path to // (static_ptr, static_type), then the cast from // (dynamic_ptr, dynamic_type) to dst_type is now ambiguous, // so stop search. if (info->number_to_static_ptr == 1 && info->path_dst_ptr_to_static_ptr == not_public_path) info->search_done = true; } // If we found no static_type,s then dst_type doesn't derive // from static_type, else it does. Record this result so that // next time we hit a dst_type we will know not to search above // it if it doesn't derive from static_type. if (is_dst_type_derived_from_static_type) info->is_dst_type_derived_from_static_type = yes; else info->is_dst_type_derived_from_static_type = no; } } } else { // This is not a static_type and not a dst_type. const Iter e = __base_info + __base_count; Iter p = __base_info; p->search_below_dst(info, current_ptr, path_below, use_strcmp); if (++p < e) { if ((__flags & __diamond_shaped_mask) || info->number_to_static_ptr == 1) { // If there are multiple paths to a base above from here, or if // a dst_type pointing to (static_ptr, static_type) has been found, // then there is no way to break out of this loop early unless // something below detects the search is done. do { if (info->search_done) break; p->search_below_dst(info, current_ptr, path_below, use_strcmp); } while (++p < e); } else if (__flags & __non_diamond_repeat_mask) { // There are not multiple paths to any base class from here and a // dst_type pointing to (static_ptr, static_type) has not yet been // found. do { if (info->search_done) break; // If we just found a dst_type with a public path to (static_ptr, static_type), // then the only reason to continue the search is to make sure // no other dst_type points to (static_ptr, static_type). // If !diamond, then we don't need to search here. if (info->number_to_static_ptr == 1 && info->path_dst_ptr_to_static_ptr == public_path) break; p->search_below_dst(info, current_ptr, path_below, use_strcmp); } while (++p < e); } else { // There are no repeated types above this node. // There are no nodes with multiple parents above this node. // no dst_type has been found to (static_ptr, static_type) do { if (info->search_done) break; // If we just found a dst_type with a public path to (static_ptr, static_type), // then the only reason to continue the search is to make sure sure // no other dst_type points to (static_ptr, static_type). // If !diamond, then we don't need to search here. // if we just found a dst_type with a private path to (static_ptr, static_type), // then we're only looking for a public path to (static_ptr, static_type) // and to check for other dst_types. // If !diamond & !repeat, then there is not a pointer to (static_ptr, static_type) // and not a dst_type under here. if (info->number_to_static_ptr == 1) break; p->search_below_dst(info, current_ptr, path_below, use_strcmp); } while (++p < e); } } } }
// Call this function when searching above a dst_type node. This function searches // for a public path to (static_ptr, static_type). // This function is guaranteed not to find a node of type dst_type. // Theoretically this is a very simple function which just stops if it finds a // static_type node: All the hoopla surrounding the search code is doing // nothing but looking for excuses to stop the search prematurely (break out of // the for-loop). That is, the algorithm below is simply an optimization of this: // void // __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info, // const void* dst_ptr, // const void* current_ptr, // int path_below) const // { // if (this == info->static_type) // process_static_type_above_dst(info, dst_ptr, current_ptr, path_below); // else // { // typedef const __base_class_type_info* Iter; // // This is not a static_type and not a dst_type // for (Iter p = __base_info, e = __base_info + __base_count; p < e; ++p) // { // p->search_above_dst(info, dst_ptr, current_ptr, public_path); // // break out early here if you can detect it doesn't matter if you do // } // } // } void __vmi_class_type_info::search_above_dst(__dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, int path_below, bool use_strcmp) const { if (is_equal(this, info->static_type, use_strcmp)) process_static_type_above_dst(info, dst_ptr, current_ptr, path_below); else { typedef const __base_class_type_info* Iter; // This is not a static_type and not a dst_type // Save flags so they can be restored when returning to nodes below. bool found_our_static_ptr = info->found_our_static_ptr; bool found_any_static_type = info->found_any_static_type; // We've found a dst_type below with a path to here. If the path // to here is not public, there may be another path to here that // is public. So we have to assume that the path to here is public. // We can stop looking above if: // 1. We've found a public path to (static_ptr, static_type). // 2. We've found an ambiguous cast from (static_ptr, static_type) to a dst_type. // This is detected at the (static_ptr, static_type). // 3. We can prove that there is no public path to (static_ptr, static_type) // above here. const Iter e = __base_info + __base_count; Iter p = __base_info; // Zero out found flags info->found_our_static_ptr = false; info->found_any_static_type = false; p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp); if (++p < e) { do { if (info->search_done) break; if (info->found_our_static_ptr) { // If we found what we're looking for, stop looking above. if (info->path_dst_ptr_to_static_ptr == public_path) break; // We found a private path to (static_ptr, static_type) // If there is no diamond then there is only one path // to (static_ptr, static_type) from here and we just found it. if (!(__flags & __diamond_shaped_mask)) break; } else if (info->found_any_static_type) { // If we found a static_type that isn't the one we're looking // for, and if there are no repeated types above here, // then stop looking. if (!(__flags & __non_diamond_repeat_mask)) break; } // Zero out found flags info->found_our_static_ptr = false; info->found_any_static_type = false; p->search_above_dst(info, dst_ptr, current_ptr, path_below, use_strcmp); } while (++p < e); } // Restore flags info->found_our_static_ptr = found_our_static_ptr; info->found_any_static_type = found_any_static_type; } }