Handle CK_BaseToDerivedMemberPointer for member function pointers. Fixes PR5091.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@83041 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCXXClass.cpp b/lib/CodeGen/CGCXXClass.cpp
index 6d53e83..5d7b8a0 100644
--- a/lib/CodeGen/CGCXXClass.cpp
+++ b/lib/CodeGen/CGCXXClass.cpp
@@ -69,21 +69,38 @@
     return Offset;
 }
 
+llvm::Constant *
+CodeGenFunction::GetCXXBaseClassOffset(const CXXRecordDecl *ClassDecl,
+                                       const CXXRecordDecl *BaseClassDecl) {
+  if (ClassDecl == BaseClassDecl)
+    return 0;
+
+  QualType BTy =
+    getContext().getCanonicalType(
+      getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl)));
+
+  uint64_t Offset = ComputeBaseClassOffset(getContext(), 
+                                           ClassDecl, BaseClassDecl);
+  if (!Offset)
+    return 0;
+
+  const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
+
+  return llvm::ConstantInt::get(PtrDiffTy, Offset);
+}
+
 llvm::Value *
 CodeGenFunction::GetAddressCXXOfBaseClass(llvm::Value *BaseValue,
                                           const CXXRecordDecl *ClassDecl,
                                           const CXXRecordDecl *BaseClassDecl,
                                           bool NullCheckValue) {
-  if (ClassDecl == BaseClassDecl)
-    return BaseValue;
-
+  llvm::Constant *Offset = GetCXXBaseClassOffset(ClassDecl, BaseClassDecl);
+  
   QualType BTy =
     getContext().getCanonicalType(
       getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl)));
   const llvm::Type *BasePtrTy = llvm::PointerType::getUnqual(ConvertType(BTy));
 
-  uint64_t Offset = ComputeBaseClassOffset(getContext(), 
-                                           ClassDecl, BaseClassDecl);
   if (!Offset) {
     // Just cast back.
     return Builder.CreateBitCast(BaseValue, BasePtrTy);
@@ -105,16 +122,12 @@
     EmitBlock(CastNotNull);
   }
   
-  const llvm::Type *LongTy = 
-    CGM.getTypes().ConvertType(CGM.getContext().LongTy);
   const llvm::Type *Int8PtrTy = 
     llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
   
-  llvm::Value *OffsetVal = llvm::ConstantInt::get(LongTy, Offset);
-  
   // Apply the offset.
   BaseValue = Builder.CreateBitCast(BaseValue, Int8PtrTy);
-  BaseValue = Builder.CreateGEP(BaseValue, OffsetVal, "add.ptr");
+  BaseValue = Builder.CreateGEP(BaseValue, Offset, "add.ptr");
   
   // Cast back.
   BaseValue = Builder.CreateBitCast(BaseValue, BasePtrTy);