diff options
author | Akira Hatanaka <ahatanaka@apple.com> | 2018-02-28 07:15:55 +0000 |
---|---|---|
committer | Akira Hatanaka <ahatanaka@apple.com> | 2018-02-28 07:15:55 +0000 |
commit | 7275da0f2ee24336fe83cb7cfe2ba22f9cefc117 (patch) | |
tree | 9faf6e67f81d54afcaa45b6300b4c150070a2eb3 /clang/lib/AST | |
parent | ac799b05d42fee69a91bf35beb7f87c548827a42 (diff) | |
download | bcm5719-llvm-7275da0f2ee24336fe83cb7cfe2ba22f9cefc117.tar.gz bcm5719-llvm-7275da0f2ee24336fe83cb7cfe2ba22f9cefc117.zip |
[ObjC] Allow declaring __strong pointer fields in structs in Objective-C
ARC mode.
Declaring __strong pointer fields in structs was not allowed in
Objective-C ARC until now because that would make the struct non-trivial
to default-initialize, copy/move, and destroy, which is not something C
was designed to do. This patch lifts that restriction.
Special functions for non-trivial C structs are synthesized that are
needed to default-initialize, copy/move, and destroy the structs and
manage the ownership of the objects the __strong pointer fields point
to. Non-trivial structs passed to functions are destructed in the callee
function.
rdar://problem/33599681
Differential Revision: https://reviews.llvm.org/D41228
llvm-svn: 326307
Diffstat (limited to 'clang/lib/AST')
-rw-r--r-- | clang/lib/AST/ASTContext.cpp | 17 | ||||
-rw-r--r-- | clang/lib/AST/Decl.cpp | 4 | ||||
-rw-r--r-- | clang/lib/AST/Type.cpp | 52 |
3 files changed, 60 insertions, 13 deletions
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index fff714cb6aa..05f5f73504e 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -2642,7 +2642,8 @@ void ASTContext::adjustExceptionSpec( bool ASTContext::isParamDestroyedInCallee(QualType T) const { return getTargetInfo().getCXXABI().areArgsDestroyedLeftToRightInCallee() || - T.hasTrivialABIOverride(); + T.hasTrivialABIOverride() || + T.isDestructedType() == QualType::DK_nontrivial_c_struct; } /// getComplexType - Return the uniqued reference to the type for a complex @@ -5771,6 +5772,11 @@ bool ASTContext::BlockRequiresCopying(QualType Ty, return true; } + // The block needs copy/destroy helpers if Ty is non-trivial to destructively + // move or destroy. + if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType()) + return true; + if (!Ty->isObjCRetainableType()) return false; Qualifiers qs = Ty.getQualifiers(); @@ -5784,13 +5790,12 @@ bool ASTContext::BlockRequiresCopying(QualType Ty, case Qualifiers::OCL_ExplicitNone: case Qualifiers::OCL_Autoreleasing: return false; - - // Tell the runtime that this is ARC __weak, called by the - // byref routines. + + // These cases should have been taken care of when checking the type's + // non-triviality. case Qualifiers::OCL_Weak: - // ARC __strong __block variables need to be retained. case Qualifiers::OCL_Strong: - return true; + llvm_unreachable("impossible"); } llvm_unreachable("fell out of lifetime switch!"); } diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index c4da87c013d..eb2c770b5f4 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3929,7 +3929,9 @@ RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc), HasFlexibleArrayMember(false), AnonymousStructOrUnion(false), HasObjectMember(false), HasVolatileMember(false), - LoadedFieldsFromExternalStorage(false) { + LoadedFieldsFromExternalStorage(false), + NonTrivialToPrimitiveDefaultInitialize(false), + NonTrivialToPrimitiveCopy(false), NonTrivialToPrimitiveDestroy(false) { assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); } diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp index 2690a3faf72..f6cbe9d7088 100644 --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -2208,6 +2208,38 @@ bool QualType::isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const { getObjCLifetime() != Qualifiers::OCL_Weak; } +QualType::PrimitiveDefaultInitializeKind +QualType::isNonTrivialToPrimitiveDefaultInitialize() const { + if (const auto *RT = + getTypePtr()->getBaseElementTypeUnsafe()->getAs<RecordType>()) + if (RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) + return PDIK_Struct; + + Qualifiers::ObjCLifetime Lifetime = getQualifiers().getObjCLifetime(); + if (Lifetime == Qualifiers::OCL_Strong) + return PDIK_ARCStrong; + + return PDIK_Trivial; +} + +QualType::PrimitiveCopyKind QualType::isNonTrivialToPrimitiveCopy() const { + if (const auto *RT = + getTypePtr()->getBaseElementTypeUnsafe()->getAs<RecordType>()) + if (RT->getDecl()->isNonTrivialToPrimitiveCopy()) + return PCK_Struct; + + Qualifiers Qs = getQualifiers(); + if (Qs.getObjCLifetime() == Qualifiers::OCL_Strong) + return PCK_ARCStrong; + + return Qs.hasVolatile() ? PCK_VolatileTrivial : PCK_Trivial; +} + +QualType::PrimitiveCopyKind +QualType::isNonTrivialToPrimitiveDestructiveMove() const { + return isNonTrivialToPrimitiveCopy(); +} + bool Type::isLiteralType(const ASTContext &Ctx) const { if (isDependentType()) return false; @@ -3896,12 +3928,20 @@ QualType::DestructionKind QualType::isDestructedTypeImpl(QualType type) { return DK_objc_weak_lifetime; } - /// Currently, the only destruction kind we recognize is C++ objects - /// with non-trivial destructors. - const CXXRecordDecl *record = - type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); - if (record && record->hasDefinition() && !record->hasTrivialDestructor()) - return DK_cxx_destructor; + if (const auto *RT = + type->getBaseElementTypeUnsafe()->getAs<RecordType>()) { + const RecordDecl *RD = RT->getDecl(); + if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { + /// Check if this is a C++ object with a non-trivial destructor. + if (CXXRD->hasDefinition() && !CXXRD->hasTrivialDestructor()) + return DK_cxx_destructor; + } else { + /// Check if this is a C struct that is non-trivial to destroy or an array + /// that contains such a struct. + if (RD->isNonTrivialToPrimitiveDestroy()) + return DK_nontrivial_c_struct; + } + } return DK_none; } |