From 1c698e0d4f9bf3d141c019d33d9040085a8a67dd Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Tue, 28 Jul 2009 18:54:57 +0000 Subject: [PATCH] More cleanup of data member access and then some. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77351 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCXX.cpp | 11 +++++++++-- lib/CodeGen/CGExpr.cpp | 24 ++++++++++++++++-------- test/CodeGenCXX/constructor-init.cpp | 3 +++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp index d82b976951..0b22eb49be 100644 --- a/lib/CodeGen/CGCXX.cpp +++ b/lib/CodeGen/CGCXX.cpp @@ -168,16 +168,23 @@ llvm::Value *CodeGenFunction::AddressCXXOfBaseClass(llvm::Value *BaseValue, getContext().getASTRecordLayout(ClassDecl); llvm::Type *I8Ptr = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); unsigned Idx = 0; + bool DerivedToBaseConversion = false; for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(), e = ClassDecl->bases_end(); i != e; ++i, ++Idx) { if (!i->isVirtual()) { const CXXRecordDecl *Base = cast(i->getType()->getAsRecordType()->getDecl()); - if (Base == BaseClassDecl) - break; + if (Base == BaseClassDecl) { + DerivedToBaseConversion = true; + break; + } } } + if (!DerivedToBaseConversion) { + assert(false && "FIXME - Only derived to imm. base convesion is supported"); + return BaseValue; + } uint64_t Offset = Layout.getFieldOffset(Idx) / 8; llvm::Value *OffsetVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset); BaseValue = Builder.CreateBitCast(BaseValue, I8Ptr); diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index b0e091bd0a..ab0f9e9c74 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -993,15 +993,14 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { if (PTy->getPointeeType()->isUnionType()) isUnion = true; CVRQualifiers = PTy->getPointeeType().getCVRQualifiers(); - if (CXXThisExpr *ThisExpr = dyn_cast(BaseExpr)) { - QualType ClassTy = ThisExpr->getType(); - ClassTy = ClassTy->getPointeeType(); - CXXRecordDecl *ClassDecl = - cast(ClassTy->getAsRecordType()->getDecl()); + QualType ClassTy = BaseExpr->getType(); + ClassTy = ClassTy->getPointeeType(); + if (CXXRecordDecl *ClassDecl = + dyn_cast(ClassTy->getAsRecordType()->getDecl())) { FieldDecl *Field = dyn_cast(E->getMemberDecl()); - CXXRecordDecl *BaseClassDecl = - cast(Field->getDeclContext()); - BaseValue = AddressCXXOfBaseClass(BaseValue, ClassDecl, BaseClassDecl); + if (CXXRecordDecl *BaseClassDecl = + dyn_cast(Field->getDeclContext())) + BaseValue = AddressCXXOfBaseClass(BaseValue, ClassDecl, BaseClassDecl); } } else if (isa(BaseExpr) || isa(BaseExpr)) { @@ -1021,6 +1020,15 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { if (BaseExpr->getType()->isUnionType()) isUnion = true; CVRQualifiers = BaseExpr->getType().getCVRQualifiers(); + if (CXXRecordDecl *ClassDecl = + dyn_cast( + BaseExpr->getType()->getAsRecordType()->getDecl())) { + FieldDecl *Field = dyn_cast(E->getMemberDecl()); + if (CXXRecordDecl *BaseClassDecl = + dyn_cast(Field->getDeclContext())) + BaseValue = + AddressCXXOfBaseClass(BaseValue, ClassDecl, BaseClassDecl); + } } FieldDecl *Field = dyn_cast(E->getMemberDecl()); diff --git a/test/CodeGenCXX/constructor-init.cpp b/test/CodeGenCXX/constructor-init.cpp index 4f7f8ee8c5..c46a561c30 100644 --- a/test/CodeGenCXX/constructor-init.cpp +++ b/test/CodeGenCXX/constructor-init.cpp @@ -38,6 +38,9 @@ struct N : M , P, Q { printf("iQ = %d\n", iQ); printf("iP = %d\n", iP); printf("iM = %d\n", iM); + printf("iQ = %d\n", (*this).iQ); + printf("iP = %d\n", ((*this)).iP); + printf("iM = %d\n", this->iM); } float ld; float ff; -- 2.40.0