]> granicus.if.org Git - clang/commitdiff
Baby steps towards fixing PR5589. If a class needs a vtable pointer, add one.
authorAnders Carlsson <andersca@mac.com>
Wed, 16 Dec 2009 17:27:20 +0000 (17:27 +0000)
committerAnders Carlsson <andersca@mac.com>
Wed, 16 Dec 2009 17:27:20 +0000 (17:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@91545 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGRecordLayoutBuilder.cpp
lib/CodeGen/CGRecordLayoutBuilder.h
test/CodeGenCXX/class-layout.cpp
test/CodeGenCXX/virt.cpp

index 31784eda5a6be84f807c46f08e56c9ff9a6446d2..9f90ec5ff6e0a7ab74502eb306ad817589f3474b 100644 (file)
@@ -216,12 +216,28 @@ void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
     AppendPadding(Layout.getSize() / 8, Align);
 }
 
+void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
+                                        const ASTRecordLayout &Layout) {
+  // Check if we need to add a vtable pointer.
+  if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
+    const llvm::Type *Int8PtrTy = 
+      llvm::Type::getInt8PtrTy(Types.getLLVMContext());
+    
+    assert(NextFieldOffsetInBytes == 0 &&
+           "Vtable pointer must come first!");
+    AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
+  }
+}
+
 bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
   assert(!D->isUnion() && "Can't call LayoutFields on a union!");
   assert(Alignment && "Did not set alignment!");
 
   const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
 
+  if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
+    LayoutBases(RD, Layout);
+  
   unsigned FieldNo = 0;
 
   for (RecordDecl::field_iterator Field = D->field_begin(),
index 4ebf4e88decb1047f151f8ea797a4be5736c3ed3..cf84053e190788b260b1d5b3e04951284b450b8d 100644 (file)
@@ -23,6 +23,8 @@ namespace llvm {
 }
 
 namespace clang {
+  class ASTRecordLayout;
+  class CXXRecordDecl;
   class FieldDecl;
   class RecordDecl;
 
@@ -90,6 +92,9 @@ class CGRecordLayoutBuilder {
   /// Returns false if the operation failed because the struct is not packed.
   bool LayoutFields(const RecordDecl *D);
 
+  /// LayoutBases - layout the bases and vtable pointer of a record decl.
+  void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout);
+  
   /// LayoutField - layout a single field. Returns false if the operation failed
   /// because the current struct is not packed.
   bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
index e1ff37f1800d6ce97d0a7ea9c1dac6772cd1a61b..31091c5f45431dec97c3d8a52c03ca363e266682 100644 (file)
@@ -7,3 +7,7 @@ struct A { } a;
 // No need to add tail padding here.
 // CHECK: %struct.B = type { i8*, i32 }
 struct B { void *a; int b; } b;
+
+// C should have a vtable pointer.
+// CHECK: %struct.C = type { i8**, i32 }
+struct C { virtual void f(); int a; } *c;
index 0acd264f18784cb57afc2c26df85cee05cfd8b45..259fd03e7affe21b4cd115eec281c11da96a6897 100644 (file)
@@ -147,12 +147,12 @@ void test12_foo() {
 }
 
 // CHECK-LPLL64:define void @_Z10test12_foov() nounwind {
-// CHECK-LPLL64:  call void %2(%class.test14* %tmp)
-// CHECK-LPLL64:  call void %5(%class.test14* %tmp1)
-// CHECK-LPLL64:  call void %8(%class.test14* %tmp3)
-// CHECK-LPLL64:  call void %11(%class.test14* %tmp5)
-// CHECK-LPLL64:  call void %14(%class.test14* %tmp7)
-// CHECK-LPLL64:  call void %17(%class.test14* %tmp9)
+// CHECK-LPLL64:  call void %
+// CHECK-LPLL64:  call void %
+// CHECK-LPLL64:  call void %
+// CHECK-LPLL64:  call void %
+// CHECK-LPLL64:  call void %
+// CHECK-LPLL64:  call void %
 // CHECK-LPLL64:  call void @_ZN8test12_A3fooEv(%class.test14* %tmp11)