From 82e28742702b50dd67dfebecbef0e7852ca838af Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 29 Jul 2009 00:44:13 +0000 Subject: [PATCH] Code refactoring to define getCXXRecordDeclForPointerType and use it in several places. llvm-svn: 77411 --- clang/include/clang/AST/Type.h | 3 ++- clang/lib/AST/Type.cpp | 7 +++++++ clang/lib/CodeGen/CGExpr.cpp | 19 +++++++++---------- clang/lib/CodeGen/CGExprScalar.cpp | 17 +++++------------ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h index 70a59312255a..be018026e36f 100644 --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -464,7 +464,8 @@ public: const ObjCInterfaceType *getAsObjCInterfaceType() const; const ObjCInterfaceType *getAsObjCQualifiedInterfaceType() const; const TemplateTypeParmType *getAsTemplateTypeParmType() const; - + const CXXRecordDecl *getCXXRecordDeclForPointerType() const; + // Member-template getAs'. This scheme will eventually // replace the specific getAsXXXX methods above. template const T *getAs() const; diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index c27df1fe2cfa..04961ed0fcb8 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -523,6 +523,13 @@ const TemplateTypeParmType *Type::getAsTemplateTypeParmType() const { return dyn_cast(CanonicalType); } +const CXXRecordDecl *Type::getCXXRecordDeclForPointerType() const { + if (const PointerType *PT = getAsPointerType()) + if (const RecordType *RT = PT->getPointeeType()->getAsRecordType()) + return dyn_cast(RT->getDecl()); + return 0; +} + const TemplateSpecializationType * Type::getAsTemplateSpecializationType() const { // There is no sugar for class template specialization types, so diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 3a37f2d62838..39ef799cd3e1 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -993,12 +993,10 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { if (PTy->getPointeeType()->isUnionType()) isUnion = true; CVRQualifiers = PTy->getPointeeType().getCVRQualifiers(); - QualType ClassTy = BaseExpr->getType(); - ClassTy = ClassTy->getPointeeType(); - if (CXXRecordDecl *ClassDecl = - dyn_cast(ClassTy->getAsRecordType()->getDecl())) { + if (const CXXRecordDecl *ClassDecl = + BaseExpr->getType()->getCXXRecordDeclForPointerType()) { FieldDecl *Field = dyn_cast(E->getMemberDecl()); - if (CXXRecordDecl *BaseClassDecl = + if (const CXXRecordDecl *BaseClassDecl = dyn_cast(Field->getDeclContext())) BaseValue = AddressCXXOfBaseClass(BaseValue, ClassDecl, BaseClassDecl); } @@ -1017,14 +1015,15 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { isNonGC = true; // FIXME: this isn't right for bitfields. BaseValue = BaseLV.getAddress(); - if (BaseExpr->getType()->isUnionType()) + QualType BaseTy = BaseExpr->getType(); + if (BaseTy->isUnionType()) isUnion = true; - CVRQualifiers = BaseExpr->getType().getCVRQualifiers(); - if (CXXRecordDecl *ClassDecl = + CVRQualifiers = BaseTy.getCVRQualifiers(); + if (const CXXRecordDecl *ClassDecl = dyn_cast( - BaseExpr->getType()->getAsRecordType()->getDecl())) { + BaseTy->getAsRecordType()->getDecl())) { FieldDecl *Field = dyn_cast(E->getMemberDecl()); - if (CXXRecordDecl *BaseClassDecl = + if (const CXXRecordDecl *BaseClassDecl = dyn_cast(Field->getDeclContext())) BaseValue = AddressCXXOfBaseClass(BaseValue, ClassDecl, BaseClassDecl); diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 5ded216f03ab..d78082463217 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -442,18 +442,11 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, // The source value may be an integer, or a pointer. if (isa(Src->getType())) { // Some heavy lifting for derived to base conversion. - if (const PointerType *PT = SrcType->getAsPointerType()) { - QualType SrcClassTy = PT->getPointeeType(); - if (const RecordType *RT = SrcClassTy->getAsRecordType()) - if (CXXRecordDecl *ClassDecl = - dyn_cast(RT->getDecl())) { - QualType DstClassType = DstType->getPointeeType(); - if (const RecordType *DRT = DstClassType->getAsRecordType()) - if (CXXRecordDecl *BaseClassDecl = - dyn_cast(DRT->getDecl())) - Src = CGF.AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl); - } - } + if (const CXXRecordDecl *ClassDecl = + SrcType->getCXXRecordDeclForPointerType()) + if (const CXXRecordDecl *BaseClassDecl = + DstType->getCXXRecordDeclForPointerType()) + Src = CGF.AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl); return Builder.CreateBitCast(Src, DstTy, "conv"); } assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");