[cfe-commits] r78265 - in /cfe/trunk: lib/AST/RecordLayoutBuilder.cpp test/CodeGenCXX/virt.cpp

Mike Stump mrs at apple.com
Wed Aug 5 17:38:53 PDT 2009


Author: mrs
Date: Wed Aug  5 19:38:46 2009
New Revision: 78265

URL: http://llvm.org/viewvc/llvm-project?rev=78265&view=rev
Log:
Fixup object layout when we have a primary base (it goes first).  Start preping for
virtual base layout.

Modified:
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/test/CodeGenCXX/virt.cpp

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=78265&r1=78264&r2=78265&view=diff

==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Wed Aug  5 19:38:46 2009
@@ -25,10 +25,14 @@
   : Ctx(Ctx), Size(0), Alignment(8), StructPacking(0), NextOffset(0),
   IsUnion(false), NonVirtualSize(0), NonVirtualAlignment(8) {}
 
+/// LayoutVtable - Lay out the vtable and set PrimaryBase.
 void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD) {
   // FIXME: audit indirect virtual bases
-  if (!RD->isPolymorphic() && !RD->getNumVBases())
+  if (!RD->isPolymorphic() && !RD->getNumVBases()) {
+    // There is no primary base in this case.
+    setPrimaryBase(0);
     return;
+  }
 
   SelectPrimaryBase(RD);
   if (PrimaryBase == 0) {
@@ -46,7 +50,9 @@
     if (!i->isVirtual()) {
       const CXXRecordDecl *Base = 
         cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
-      LayoutNonVirtualBase(Base);
+      // Skip the PrimaryBase here, as it is laid down first.
+      if (Base != PrimaryBase)
+        LayoutNonVirtualBase(Base);
     }
   }
 }
@@ -186,12 +192,10 @@
   // If this is a C++ class, lay out the nonvirtual bases.
   if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
     LayoutVtable(RD);
+    // PrimaryBase goes first.
+    if (PrimaryBase)
+      LayoutNonVirtualBase(PrimaryBase);
     LayoutNonVirtualBases(RD);
-
-    // FIXME: audit indirect virtual bases
-    assert (RD->getNumVBases() == 0
-            && "FIXME: We don't support virtual bases yet!");
-    // FIXME: We need to layout the virtual bases in the complete object layout.
   }
 
   LayoutFields(D);

Modified: cfe/trunk/test/CodeGenCXX/virt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/virt.cpp?rev=78265&r1=78264&r2=78265&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/virt.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/virt.cpp Wed Aug  5 19:38:46 2009
@@ -7,6 +7,7 @@
 struct B {
   virtual void bar1();
   virtual void bar2();
+  int b;
 };
 void B::bar1() { }
 void B::bar2() { }
@@ -18,22 +19,42 @@
 void C::bee1() { }
 void C::bee2() { }
 
-static_assert (sizeof (B) == (sizeof(void *)), "vtable pointer layout");
+struct D {
+  virtual void boo();
+};
+void D::boo() { }
+
+struct E {
+  int e;
+};
 
-class A : public B, public C {
+static_assert (sizeof (C) == (sizeof(void *)), "vtable pointer layout");
+
+class A : public E, public B, public C, /* virtual */ public D {
 public:
   virtual void foo1();
   virtual void foo2();
   A() { }
-} *a;
+  int a;
+} *ap;
 void A::foo1() { }
 void A::foo2() { }
 
 int main() {
   A a;
   B b;
+  ap->e = 1;
+  ap->b = 2;
 }
 
+// CHECK-LP32: main:
+// CHECK-LP32: movl $1, 8(%eax)
+// CHECK-LP32: movl $2, 4(%eax)
+
+// CHECK-LP64: main:
+// CHECK-LP64: movl $1, 12(%rax)
+// CHECK-LP64: movl $2, 8(%rax)
+
 // CHECK-LP64: __ZTV1B:
 // CHECK-LP64: .space 8
 // CHECK-LP64: .space 8
@@ -53,7 +74,7 @@
 // CHECK-LP64: .quad __ZN1B4bar2Ev
 // CHECK-LP64: .quad __ZN1A4foo1Ev
 // CHECK-LP64: .quad __ZN1A4foo2Ev
-// CHECK-LP64: .quad 18446744073709551608
+// CHECK-LP64: .quad 18446744073709551600
 // CHECK-LP64: .space 8
 // CHECK-LP64: .quad __ZN1C4bee1Ev
 // CHECK-LP64: .quad __ZN1C4bee2Ev
@@ -65,7 +86,7 @@
 // CHECK-LP32: .long __ZN1B4bar2Ev
 // CHECK-LP32: .long __ZN1A4foo1Ev
 // CHECK-LP32: .long __ZN1A4foo2Ev
-// CHECK-LP32: .long 4294967292
+// CHECK-LP32: .long 4294967284
 // CHECK-LP32: .space 4
 // CHECK-LP32: .long __ZN1C4bee1Ev
 // CHECK-LP32: .long __ZN1C4bee2Ev





More information about the cfe-commits mailing list