[ObjC] Make C++ triviality type traits available to non-trivial C

structs.

r326307 and r327870 made changes that allowed using non-trivial C
structs with fields qualified with __strong or __weak. This commit makes
the following C++ triviality type traits available to non-trivial C
structs:

__has_trivial_assign
__has_trivial_move_assign
__has_trivial_copy
__has_trivial_move_constructor
__has_trivial_constructor
__has_trivial_destructor

This reapplies r328680. This commit fixes a bug where the copy/move
__has_trivial_* traits would return false when a volatile type was being
passed. Thanks to Richard Smith for pointing out the mistake.

rdar://problem/33599681

Differential Revision: https://reviews.llvm.org/D44913

llvm-svn: 329289
This commit is contained in:
Akira Hatanaka 2018-04-05 14:39:57 +00:00
parent 510725c2d6
commit c1b596c4bb
3 changed files with 64 additions and 10 deletions

View File

@ -433,12 +433,12 @@ TYPE_TRAIT_1(__has_nothrow_assign, HasNothrowAssign, KEYCXX)
TYPE_TRAIT_1(__has_nothrow_move_assign, HasNothrowMoveAssign, KEYCXX) TYPE_TRAIT_1(__has_nothrow_move_assign, HasNothrowMoveAssign, KEYCXX)
TYPE_TRAIT_1(__has_nothrow_copy, HasNothrowCopy, KEYCXX) TYPE_TRAIT_1(__has_nothrow_copy, HasNothrowCopy, KEYCXX)
TYPE_TRAIT_1(__has_nothrow_constructor, HasNothrowConstructor, KEYCXX) TYPE_TRAIT_1(__has_nothrow_constructor, HasNothrowConstructor, KEYCXX)
TYPE_TRAIT_1(__has_trivial_assign, HasTrivialAssign, KEYCXX) TYPE_TRAIT_1(__has_trivial_assign, HasTrivialAssign, KEYALL)
TYPE_TRAIT_1(__has_trivial_move_assign, HasTrivialMoveAssign, KEYCXX) TYPE_TRAIT_1(__has_trivial_move_assign, HasTrivialMoveAssign, KEYALL)
TYPE_TRAIT_1(__has_trivial_copy, HasTrivialCopy, KEYCXX) TYPE_TRAIT_1(__has_trivial_copy, HasTrivialCopy, KEYALL)
TYPE_TRAIT_1(__has_trivial_constructor, HasTrivialDefaultConstructor, KEYCXX) TYPE_TRAIT_1(__has_trivial_constructor, HasTrivialDefaultConstructor, KEYALL)
TYPE_TRAIT_1(__has_trivial_move_constructor, HasTrivialMoveConstructor, KEYCXX) TYPE_TRAIT_1(__has_trivial_move_constructor, HasTrivialMoveConstructor, KEYALL)
TYPE_TRAIT_1(__has_trivial_destructor, HasTrivialDestructor, KEYCXX) TYPE_TRAIT_1(__has_trivial_destructor, HasTrivialDestructor, KEYALL)
TYPE_TRAIT_1(__has_virtual_destructor, HasVirtualDestructor, KEYCXX) TYPE_TRAIT_1(__has_virtual_destructor, HasVirtualDestructor, KEYCXX)
TYPE_TRAIT_1(__is_abstract, IsAbstract, KEYCXX) TYPE_TRAIT_1(__is_abstract, IsAbstract, KEYCXX)
TYPE_TRAIT_1(__is_aggregate, IsAggregate, KEYCXX) TYPE_TRAIT_1(__is_aggregate, IsAggregate, KEYCXX)

View File

@ -4521,6 +4521,8 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
// does not correctly compute triviality in the presence of multiple special // does not correctly compute triviality in the presence of multiple special
// members of the same kind. Revisit this once the g++ bug is fixed. // members of the same kind. Revisit this once the g++ bug is fixed.
case UTT_HasTrivialDefaultConstructor: case UTT_HasTrivialDefaultConstructor:
if (T.isNonTrivialToPrimitiveDefaultInitialize())
return false;
// http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
// If __is_pod (type) is true then the trait is true, else if type is // If __is_pod (type) is true then the trait is true, else if type is
// a cv class or union type (or array thereof) with a trivial default // a cv class or union type (or array thereof) with a trivial default
@ -4531,7 +4533,11 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
return RD->hasTrivialDefaultConstructor() && return RD->hasTrivialDefaultConstructor() &&
!RD->hasNonTrivialDefaultConstructor(); !RD->hasNonTrivialDefaultConstructor();
return false; return false;
case UTT_HasTrivialMoveConstructor: case UTT_HasTrivialMoveConstructor: {
QualType::PrimitiveCopyKind PCK =
T.isNonTrivialToPrimitiveDestructiveMove();
if (PCK != QualType::PCK_Trivial && PCK != QualType::PCK_VolatileTrivial)
return false;
// This trait is implemented by MSVC 2012 and needed to parse the // This trait is implemented by MSVC 2012 and needed to parse the
// standard library headers. Specifically this is used as the logic // standard library headers. Specifically this is used as the logic
// behind std::is_trivially_move_constructible (20.9.4.3). // behind std::is_trivially_move_constructible (20.9.4.3).
@ -4540,7 +4546,11 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl())
return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor(); return RD->hasTrivialMoveConstructor() && !RD->hasNonTrivialMoveConstructor();
return false; return false;
case UTT_HasTrivialCopy: }
case UTT_HasTrivialCopy: {
QualType::PrimitiveCopyKind PCK = T.isNonTrivialToPrimitiveCopy();
if (PCK != QualType::PCK_Trivial && PCK != QualType::PCK_VolatileTrivial)
return false;
// http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
// If __is_pod (type) is true or type is a reference type then // If __is_pod (type) is true or type is a reference type then
// the trait is true, else if type is a cv class or union type // the trait is true, else if type is a cv class or union type
@ -4552,7 +4562,12 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
return RD->hasTrivialCopyConstructor() && return RD->hasTrivialCopyConstructor() &&
!RD->hasNonTrivialCopyConstructor(); !RD->hasNonTrivialCopyConstructor();
return false; return false;
case UTT_HasTrivialMoveAssign: }
case UTT_HasTrivialMoveAssign: {
QualType::PrimitiveCopyKind PCK =
T.isNonTrivialToPrimitiveDestructiveMove();
if (PCK != QualType::PCK_Trivial && PCK != QualType::PCK_VolatileTrivial)
return false;
// This trait is implemented by MSVC 2012 and needed to parse the // This trait is implemented by MSVC 2012 and needed to parse the
// standard library headers. Specifically it is used as the logic // standard library headers. Specifically it is used as the logic
// behind std::is_trivially_move_assignable (20.9.4.3) // behind std::is_trivially_move_assignable (20.9.4.3)
@ -4561,7 +4576,11 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) if (CXXRecordDecl *RD = C.getBaseElementType(T)->getAsCXXRecordDecl())
return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment(); return RD->hasTrivialMoveAssignment() && !RD->hasNonTrivialMoveAssignment();
return false; return false;
case UTT_HasTrivialAssign: }
case UTT_HasTrivialAssign: {
QualType::PrimitiveCopyKind PCK = T.isNonTrivialToPrimitiveCopy();
if (PCK != QualType::PCK_Trivial && PCK != QualType::PCK_VolatileTrivial)
return false;
// http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
// If type is const qualified or is a reference type then the // If type is const qualified or is a reference type then the
// trait is false. Otherwise if __is_pod (type) is true then the // trait is false. Otherwise if __is_pod (type) is true then the
@ -4582,6 +4601,7 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
return RD->hasTrivialCopyAssignment() && return RD->hasTrivialCopyAssignment() &&
!RD->hasNonTrivialCopyAssignment(); !RD->hasNonTrivialCopyAssignment();
return false; return false;
}
case UTT_IsDestructible: case UTT_IsDestructible:
case UTT_IsTriviallyDestructible: case UTT_IsTriviallyDestructible:
case UTT_IsNothrowDestructible: case UTT_IsNothrowDestructible:
@ -4632,6 +4652,8 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
return true; return true;
case UTT_HasTrivialDestructor: case UTT_HasTrivialDestructor:
if (T.isDestructedType() == QualType::DK_nontrivial_c_struct)
return false;
// http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html
// If __is_pod (type) is true or type is a reference type // If __is_pod (type) is true or type is a reference type
// then the trait is true, else if type is a cv class or union // then the trait is true, else if type is a cv class or union

View File

@ -0,0 +1,32 @@
// RUN: %clang_cc1 -fsyntax-only -fobjc-arc -verify %s
// expected-no-diagnostics
struct Trivial {
int x;
};
struct NonTrivial {
id x;
};
int trivial_assign[__has_trivial_assign(struct Trivial) ? 1 : -1];
int trivial_move_assign[__has_trivial_move_assign(struct Trivial) ? 1 : -1];
int trivial_copy_constructor[__has_trivial_copy(struct Trivial) ? 1 : -1];
int trivial_move_constructor[__has_trivial_move_constructor(struct Trivial) ? 1 : -1];
int trivial_constructor[__has_trivial_constructor(struct Trivial) ? 1 : -1];
int trivial_destructor[__has_trivial_destructor(struct Trivial) ? 1 : -1];
int non_trivial_assign[__has_trivial_assign(struct NonTrivial) ? -1 : 1];
int non_trivial_move_assign[__has_trivial_move_assign(struct NonTrivial) ? -1 : 1];
int non_trivial_copy_constructor[__has_trivial_copy(struct NonTrivial) ? -1 : 1];
int non_trivial_move_constructor[__has_trivial_move_constructor(struct NonTrivial) ? -1 : 1];
int non_trivial_constructor[__has_trivial_constructor(struct NonTrivial) ? -1 : 1];
int non_trivial_destructor[__has_trivial_destructor(struct NonTrivial) ? -1 : 1];
int volatile_trivial_assign[__has_trivial_assign(volatile int) ? 1 : -1];
int volatile_trivial_move_assign[__has_trivial_move_assign(volatile int) ? 1 : -1];
int volatile_trivial_copy_constructor[__has_trivial_copy(volatile int) ? 1 : -1];
int volatile_trivial_move_constructor[__has_trivial_move_constructor(volatile int) ? 1 : -1];
int volatile_trivial_copy_constructor2[__has_trivial_copy(volatile struct Trivial) ? 1 : -1];
int volatile_trivial_move_constructor2[__has_trivial_move_constructor(volatile struct Trivial) ? 1 : -1];