From 191dfe909d6cc18e9134ac23ac4daaedeceb862f Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sat, 12 Sep 2009 04:57:16 +0000 Subject: Handle derived-to-base conversion in CGExprScalar::EmitCast, if the cast kind is CK_DerivedToBase. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@81610 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprScalar.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'lib/CodeGen/CGExprScalar.cpp') diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index ef323c14ba..7adbc9fd3b 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -466,17 +466,9 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, // some native types (like Obj-C id) may map to a pointer type. if (isa(DstTy)) { // The source value may be an integer, or a pointer. - if (isa(Src->getType())) { - // Some heavy lifting for derived to base conversion. - // FIXME: This should be handled by EmitCast. - if (const CXXRecordDecl *ClassDecl = - SrcType->getCXXRecordDeclForPointerType()) - if (const CXXRecordDecl *BaseClassDecl = - DstType->getCXXRecordDeclForPointerType()) - Src = CGF.GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, - /*NullCheckValue=*/false); + if (isa(Src->getType())) return Builder.CreateBitCast(Src, DstTy, "conv"); - } + assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); // First, convert to the correct width so that we control the kind of // extension. @@ -665,6 +657,22 @@ Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy, } case CastExpr::CK_NullToMemberPointer: return CGF.CGM.EmitNullConstant(DestTy); + + case CastExpr::CK_DerivedToBase: { + const RecordType *DerivedClassTy = + E->getType()->getAs()->getPointeeType()->getAs(); + CXXRecordDecl *DerivedClassDecl = + cast(DerivedClassTy->getDecl()); + + const RecordType *BaseClassTy = + DestTy->getAs()->getPointeeType()->getAs(); + CXXRecordDecl *BaseClassDecl = cast(BaseClassTy->getDecl()); + + Value *Src = Visit(const_cast(E)); + return CGF.GetAddressCXXOfBaseClass(Src, DerivedClassDecl, BaseClassDecl, + /*NullCheckValue=*/true); + } + } // Handle cases where the source is an non-complex type. -- cgit v1.2.3