[cfe-dev] How to determine base class direct or not?

John McCall rjmccall at apple.com
Fri Nov 11 09:31:57 PST 2011


On Nov 11, 2011, at 12:09 AM, r4start wrote:
> This information is need to implement RTTI Class Hierarchy Descriptor in MS ABI.
> This struct has following fields:
> struct RTTIClassHierarchyDescriptor
> {
>    DWORD signature;      //always zero?
>    DWORD attributes;     //bit 0 set = multiple inheritance, bit 1 set = virtual inheritance
>    DWORD numBaseClasses; //number of classes in pBaseClassArray
>    struct RTTIBaseClassArray* pBaseClassArray;
> };
> Yesterday I discovered that third bit(bit 2) in attributes field set when we have class like A1.

Aha.  I would guess that this bit indicates whether the object has any public ambiguous base-class subobjects;  if it does, the dynamic_cast algorithm can't just stop at the first such subobject it finds, it has to keep searching to see if the cast fails due to ambiguity.  You should definitely be doing this as a walk over the class hierarchy.  Pseudocode:

  queue : list<class>
  visited : set<class>
  visitedVirtual : set<class>

  queue.insert(mostDerived)
  while derived = queue.pop():
    foreach base in derived.bases:
      next unless base.public
      next if base.virtual and visitedVirtual.insertAndContains(base.class)
      return true if visited.insertAndContains(base.class)
      queue.insert(base.class)
  return false

John.



More information about the cfe-dev mailing list