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);
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);
break;
case CastExpr::CK_NullToMemberPointer: {
- QualType T = E->getType();
const llvm::Type *PtrDiffTy =
CGF.ConvertType(CGF.getContext().getPointerDiffType());
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;
+ }
}
}
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,