[cfe-dev] Cannot access the offset of a virtual base class in a given case

Devang Patel dpatel at apple.com
Wed Aug 31 10:00:01 PDT 2011


See if, CGDebugInfo::CollectCXXBases() helps you.
-
Devang
On Aug 22, 2011, at 7:01 AM, Xiaolong Tang wrote:

> 
> Hi Everyone, 
> 
> I have a question about the layout of a C++ record. 
> 
> Given the following class hierarchy:
> 
> // A virtual class
> class Foo {
> public:
>  virtual void f() {};
> };
> 
> // A virtual class
> class Bar {
> public:
>  virtual void g() {};
> };
> 
> // Two virtual bases 
> class Zoo : virtual public Foo, virtual public Bar {
> public:
>  int x;
>  int y;
>  virtual void g() { x = x + y; };
>  virtual void f() { x = x - y; };
> };
> 
> 
> Following is the code I use to access the offset of the virtual bases of a
> class: 
> 
>    // Suppose that "Record" denotes a RecordDecl
> 
>    // The layout code generation for the backend, e.g., LLVM IR
>    const CGRecordLayout &Layout = CGM.getTypes().getCGRecordLayout(Record);
>    // In C++ mode
>    const CXXRecordDecl *CXXRecord = cast<CXXRecordDecl>(Record);
> 
>    // Virtual bases 
>    for (CXXRecordDecl::base_class_const_iterator I = CXXRecord->vbases_begin(),
>         E = CXXRecord->vbases_end(); I != E; ++I) {
>      // The type of a virtual base 	 
>      const RecordType *BaseTy = I->getType()->getAs<RecordType>();
>      // The decl of the virtual base
>      const CXXRecordDecl *BaseDecl = dyn_cast<CXXRecordDecl>(BaseTy->getDecl());
>      // Accessing the index of the base 
>      unsigned fieldNo  = Layout.getVirtualBaseIndex(BaseDecl);
>    }
> 
> Fed with the given class hierarchy, the last statement of the above
> code, i.e., "getVirtualBaseIndex(BaseDecl)", causes an assertion
> failure. 
> 
>  Assertion failed: (CompleteObjectVirtualBases.count(base) && "Invalid virtual base!"), ...
> 
> The failure means that the virtual base "Foo" is not in the
> layout mapping from the virtual bases of the class "Zoo" to their
> offsets. So, can someone point out any hints about this missing? 
> 
> P.S. When the class "Foo" contains one data member, the above
> issue goes away. 
> 
> 
> Xiaolong
> 
> 
> 
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list