diff options
| author | Anders Carlsson <andersca@mac.com> | 2009-12-16 17:27:20 +0000 |
|---|---|---|
| committer | Anders Carlsson <andersca@mac.com> | 2009-12-16 17:27:20 +0000 |
| commit | d681a29ac0cb889f34ebd377eaeb48b34a1819d6 (patch) | |
| tree | 86ce748c8f437607e203e062dc102a6531419f5f | |
| parent | 8716b478e462bdc5d742992420c925e10cc5f67b (diff) | |
| download | bcm5719-llvm-d681a29ac0cb889f34ebd377eaeb48b34a1819d6.tar.gz bcm5719-llvm-d681a29ac0cb889f34ebd377eaeb48b34a1819d6.zip | |
Baby steps towards fixing PR5589. If a class needs a vtable pointer, add one.
llvm-svn: 91545
| -rw-r--r-- | clang/lib/CodeGen/CGRecordLayoutBuilder.cpp | 16 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CGRecordLayoutBuilder.h | 5 | ||||
| -rw-r--r-- | clang/test/CodeGenCXX/class-layout.cpp | 4 | ||||
| -rw-r--r-- | clang/test/CodeGenCXX/virt.cpp | 12 |
4 files changed, 31 insertions, 6 deletions
diff --git a/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp b/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp index 31784eda5a6..9f90ec5ff6e 100644 --- a/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp +++ b/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp @@ -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(), diff --git a/clang/lib/CodeGen/CGRecordLayoutBuilder.h b/clang/lib/CodeGen/CGRecordLayoutBuilder.h index 4ebf4e88dec..cf84053e190 100644 --- a/clang/lib/CodeGen/CGRecordLayoutBuilder.h +++ b/clang/lib/CodeGen/CGRecordLayoutBuilder.h @@ -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); diff --git a/clang/test/CodeGenCXX/class-layout.cpp b/clang/test/CodeGenCXX/class-layout.cpp index e1ff37f1800..31091c5f454 100644 --- a/clang/test/CodeGenCXX/class-layout.cpp +++ b/clang/test/CodeGenCXX/class-layout.cpp @@ -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; diff --git a/clang/test/CodeGenCXX/virt.cpp b/clang/test/CodeGenCXX/virt.cpp index 0acd264f187..259fd03e7af 100644 --- a/clang/test/CodeGenCXX/virt.cpp +++ b/clang/test/CodeGenCXX/virt.cpp @@ -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) |

