From: Daniel Dunbar Date: Wed, 22 Apr 2009 17:43:55 +0000 (+0000) Subject: Reapply r69771, with updates & fixes: X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a80a0f6398df06c018af779a7ca82a29172c45d1;p=clang Reapply r69771, with updates & fixes: Rework the shadow struct that is layed out for Objective-C classes. - Superclasses are now always laid out in their shadow structure at the first field. - Prior to this, the entire class heirarchy was flattened into a single structure which meant that alignment, padding, and bitfields were incorrect (the ASTRecordLayout was correct however, which meant our debug info didn't coincide with ivar offsets, for example). - This is still very suboptimal (for example, ivar are looked up recursively, but I believe the ivar layout itself is now at least close to correct. - error: objc[29823]: layout bitmap sliding backwards git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69811 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index d91a8cefd1..7d3c119d57 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -621,11 +621,9 @@ void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, Alignment = std::max(Alignment, FieldAlign); } -void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, - llvm::SmallVectorImpl &Fields) { - const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); - if (SuperClass) - CollectObjCIvars(SuperClass, Fields); +static void CollectLocalObjCIvars(ASTContext *Ctx, + const ObjCInterfaceDecl *OI, + llvm::SmallVectorImpl &Fields) { for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), E = OI->ivar_end(); I != E; ++I) { ObjCIvarDecl *IVDecl = *I; @@ -633,31 +631,50 @@ void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, Fields.push_back(cast(IVDecl)); } // look into properties. - for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this), - E = OI->prop_end(*this); I != E; ++I) { + for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx), + E = OI->prop_end(*Ctx); I != E; ++I) { if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) Fields.push_back(cast(IV)); } } +void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, + llvm::SmallVectorImpl &Fields) { + if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) + CollectObjCIvars(SuperClass, Fields); + CollectLocalObjCIvars(this, OI, Fields); +} + /// addRecordToClass - produces record info. for the class for its /// ivars and all those inherited. /// const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) { assert(!D->isForwardDecl() && "Invalid decl!"); - // FIXME: The only client relying on this working in the presence of - // forward declarations is IRgen, which should not need it. Fix - // and simplify this code. RecordDecl *&RD = ASTRecordForInterface[D]; if (RD) return RD; llvm::SmallVector RecFields; - CollectObjCIvars(D, RecFields); + CollectLocalObjCIvars(this, D, RecFields); RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(), D->getIdentifier()); + const RecordDecl *SRD; + if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) { + SRD = addRecordToClass(SuperClass); + } else { + SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0); + const_cast(SRD)->completeDefinition(*this); + } + + RD->addDecl(*this, + FieldDecl::Create(*this, RD, + SourceLocation(), + 0, + getTagDeclType(const_cast(SRD)), + 0, false)); + /// FIXME! Can do collection of ivars and adding to the record while /// doing it. for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp index a993963680..e93ea2f19b 100644 --- a/lib/CodeGen/CGObjCMac.cpp +++ b/lib/CodeGen/CGObjCMac.cpp @@ -47,26 +47,38 @@ CGObjCRuntime::GetConcreteClassStruct(CodeGen::CodeGenModule &CGM, /// static const FieldDecl *LookupFieldDeclForIvar(ASTContext &Context, const ObjCInterfaceDecl *OID, - const ObjCIvarDecl *OIVD) { + const ObjCIvarDecl *OIVD, + const ObjCInterfaceDecl *&Found) { assert(!OID->isForwardDecl() && "Invalid interface decl!"); const RecordDecl *RecordForDecl = Context.addRecordToClass(OID); assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class"); DeclContext::lookup_const_result Lookup = RecordForDecl->lookup(Context, OIVD->getDeclName()); - assert((Lookup.first != Lookup.second) && "field decl not found"); - return cast(*Lookup.first); + + if (Lookup.first != Lookup.second) { + Found = OID; + return cast(*Lookup.first); + } + + // If lookup failed, try the superclass. + // + // FIXME: This is slow, we shouldn't need to do this. + const ObjCInterfaceDecl *Super = OID->getSuperClass(); + assert(OID && "field decl not found!"); + return LookupFieldDeclForIvar(Context, Super, OIVD, Found); } uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, const ObjCInterfaceDecl *OID, const ObjCIvarDecl *Ivar) { assert(!OID->isForwardDecl() && "Invalid interface decl!"); - QualType T = CGM.getContext().getObjCInterfaceType(OID); - const llvm::StructType *STy = GetConcreteClassStruct(CGM, OID); + const ObjCInterfaceDecl *Container; + const FieldDecl *Field = + LookupFieldDeclForIvar(CGM.getContext(), OID, Ivar, Container); + QualType T = CGM.getContext().getObjCInterfaceType(Container); + const llvm::StructType *STy = GetConcreteClassStruct(CGM, Container); const llvm::StructLayout *Layout = CGM.getTargetData().getStructLayout(STy); - const FieldDecl *Field = - LookupFieldDeclForIvar(CGM.getContext(), OID, Ivar); if (!Field->isBitField()) return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); @@ -97,8 +109,9 @@ LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, // purposes, it would be cleaner to use a GEP on the proper type // since the structure layout is fixed; however for that we need to // be able to walk the class chain for an Ivar. + const ObjCInterfaceDecl *Container; const FieldDecl *Field = - LookupFieldDeclForIvar(CGF.CGM.getContext(), OID, Ivar); + LookupFieldDeclForIvar(CGF.CGM.getContext(), OID, Ivar, Container); // (char *) BaseValue llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); diff --git a/test/CodeGenObjC/ivar-layout-64.m b/test/CodeGenObjC/ivar-layout-64.m new file mode 100644 index 0000000000..57f89b4787 --- /dev/null +++ b/test/CodeGenObjC/ivar-layout-64.m @@ -0,0 +1,56 @@ +// RUN: clang-cc -triple x86_64-apple-darwin9 -emit-llvm -o %t %s && +// RUNX: llvm-gcc -m64 -emit-llvm -S -o %t %s && + +// RUN: grep '@"OBJC_IVAR_$_I3._iv2" = global i64 8, section "__DATA, __objc_const", align 8' %t && +// RUN: grep '@"OBJC_IVAR_$_I3._iv3" = global i64 12, section "__DATA, __objc_const", align 8' %t && +// RUN: grep '@"OBJC_IVAR_$_I4._iv4" = global i64 16, section "__DATA, __objc_const", align 8' %t && +// RUN: grep '@"OBJC_IVAR_$_I5._iv5" = global i64 24, section "__DATA, __objc_const", align 8' %t && +// RUN: grep '@"OBJC_IVAR_$_I5._iv6_synth" = global i64 28, section "__DATA, __objc_const", align 8' %t && +// RUN: grep '@"OBJC_IVAR_$_I5._iv7_synth" = global i64 32, section "__DATA, __objc_const", align 8' %t && + +// RUN: true + +struct s0 { + double x; +}; + +@interface I2 { + struct s0 _iv1; +} +@end + +@interface I3 : I2 { + unsigned int _iv2 :1; + unsigned : 0; + unsigned int _iv3 : 3; +} +@end + +@interface I4 : I3 { + char _iv4; +} +@end + +@interface I5 : I4 { + char _iv5; +} + +@property int prop0; +@end + +@implementation I3 +@end + +@implementation I4 +@end + +@interface I5 () +@property int prop1; +@property char prop2; +@end + +@implementation I5 +@synthesize prop0 = _iv6_synth; +@synthesize prop1 = _iv7_synth; +@synthesize prop2 = _iv5; +@end