diff options
author | Anders Carlsson <andersca@mac.com> | 2009-10-03 15:13:22 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-10-03 15:13:22 +0000 |
commit | 2c51f092e4d75550f51b4dd6f831aa5e5e6ae928 (patch) | |
tree | 63adc1974c99e92fe07689d24a08c877bbc49573 | |
parent | f57b4e44d03266f2ec1b427645555ed8952bc830 (diff) |
Handle base-to-derived casts of member function pointers in CGExprConstant.cpp
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83265 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/CodeGen/CGExprConstant.cpp | 37 | ||||
-rw-r--r-- | test/CodeGenCXX/member-function-pointers.cpp | 6 |
2 files changed, 43 insertions, 0 deletions
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index 144fa28e19..a742355f74 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -485,6 +485,43 @@ public: } case CastExpr::CK_NullToMemberPointer: return CGM.EmitNullConstant(E->getType()); + + case CastExpr::CK_BaseToDerivedMemberPointer: { + Expr *SubExpr = E->getSubExpr(); + + const MemberPointerType *SrcTy = + SubExpr->getType()->getAs<MemberPointerType>(); + const MemberPointerType *DestTy = + E->getType()->getAs<MemberPointerType>(); + + const CXXRecordDecl *BaseClass = + cast<CXXRecordDecl>(cast<RecordType>(SrcTy->getClass())->getDecl()); + const CXXRecordDecl *DerivedClass = + cast<CXXRecordDecl>(cast<RecordType>(DestTy->getClass())->getDecl()); + + if (SrcTy->getPointeeType()->isFunctionProtoType()) { + llvm::Constant *C = + CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF); + if (!C) + return 0; + + llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C); + + // Check if we need to update the adjustment. + if (llvm::Constant *Offset = CGM.GetCXXBaseClassOffset(DerivedClass, + BaseClass)) { + llvm::Constant *Values[2]; + + Values[0] = CS->getOperand(0); + Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset); + return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2, + /*Packed=*/false); + } + + return CS; + } + } + default: { // FIXME: This should be handled by the CK_NoOp cast kind. // Explicit and implicit no-op casts diff --git a/test/CodeGenCXX/member-function-pointers.cpp b/test/CodeGenCXX/member-function-pointers.cpp index 38c13d2919..138ba173a3 100644 --- a/test/CodeGenCXX/member-function-pointers.cpp +++ b/test/CodeGenCXX/member-function-pointers.cpp @@ -15,6 +15,12 @@ void (A::*pa2)() = &A::f; // CHECK: @pa3 = global %0 { i64 1, i64 0 }, align 8 void (A::*pa3)() = &A::vf; +// CHECK: @pc2 = global %0 { i64 ptrtoint (void ()* @_ZN1A1fEv to i64), i64 16 }, align 8 +void (C::*pc2)() = &C::f; + +// CHECK: @pc3 = global %0 { i64 1, i64 0 }, align 8 +void (A::*pc3)() = &A::vf; + void f() { // CHECK: store i64 0, i64* getelementptr inbounds (%0* @pa, i32 0, i32 0) // CHECK: store i64 0, i64* getelementptr inbounds (%0* @pa, i32 0, i32 1) |