[cfe-commits] r83041 - in /cfe/trunk/lib/CodeGen: CGCXXClass.cpp CGExprAgg.cpp CodeGenFunction.h

Anders Carlsson andersca at mac.com
Mon Sep 28 20:13:21 PDT 2009


Author: andersca
Date: Mon Sep 28 22:13:20 2009
New Revision: 83041

URL: http://llvm.org/viewvc/llvm-project?rev=83041&view=rev
Log:
Handle CK_BaseToDerivedMemberPointer for member function pointers. Fixes PR5091.

Modified:
    cfe/trunk/lib/CodeGen/CGCXXClass.cpp
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGCXXClass.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXXClass.cpp?rev=83041&r1=83040&r2=83041&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXXClass.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXXClass.cpp Mon Sep 28 22:13:20 2009
@@ -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);

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=83041&r1=83040&r2=83041&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Mon Sep 28 22:13:20 2009
@@ -200,7 +200,6 @@
     break;
 
   case CastExpr::CK_NullToMemberPointer: {
-    QualType T = E->getType();
     const llvm::Type *PtrDiffTy = 
       CGF.ConvertType(CGF.getContext().getPointerDiffType());
 
@@ -213,6 +212,40 @@
 
     break;
   }
+      
+  case CastExpr::CK_BaseToDerivedMemberPointer: {
+    QualType SrcType = E->getSubExpr()->getType();
+    
+    llvm::Value *Src = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(SrcType), 
+                                            "tmp");
+    CGF.EmitAggExpr(E->getSubExpr(), Src, SrcType.isVolatileQualified());
+    
+    llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr");
+    SrcPtr = Builder.CreateLoad(SrcPtr);
+    
+    llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj");
+    SrcAdj = Builder.CreateLoad(SrcAdj);
+    
+    llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
+    Builder.CreateStore(SrcPtr, DstPtr, VolatileDest);
+    
+    llvm::Value *DstAdj = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
+    
+    // Now See if we need to update the adjustment.
+    const CXXRecordDecl *SrcDecl = 
+      cast<CXXRecordDecl>(SrcType->getAs<MemberPointerType>()->
+                          getClass()->getAs<RecordType>()->getDecl());
+    const CXXRecordDecl *DstDecl = 
+      cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
+                          getClass()->getAs<RecordType>()->getDecl());
+    
+    llvm::Constant *Adj = CGF.GetCXXBaseClassOffset(DstDecl, SrcDecl);
+    if (Adj)
+      SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
+    
+    Builder.CreateStore(SrcAdj, DstAdj, VolatileDest);
+    break;
+  }
   }
 }
 

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=83041&r1=83040&r2=83041&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Mon Sep 28 22:13:20 2009
@@ -589,6 +589,11 @@
                                         const CXXRecordDecl *BaseClassDecl,
                                         bool NullCheckValue);
 
+  /// GetCXXBaseClassOffset - Returns the offset from a derived class to its
+  /// base class. Returns null if the offset is 0.
+  llvm::Constant *GetCXXBaseClassOffset(const CXXRecordDecl *ClassDecl,
+                                        const CXXRecordDecl *BaseClassDecl);
+  
   void EmitClassAggrMemberwiseCopy(llvm::Value *DestValue,
                                    llvm::Value *SrcValue,
                                    const ArrayType *Array,





More information about the cfe-commits mailing list