Index: lib/AST/Type.cpp =================================================================== --- lib/AST/Type.cpp +++ lib/AST/Type.cpp @@ -2035,7 +2035,18 @@ case Qualifiers::OCL_None: break; - } + } + } else if (Context.getLangOpts().ObjCWeak) { + switch (getObjCLifetime()) { + case Qualifiers::OCL_Weak: + return false; + + case Qualifiers::OCL_None: + case Qualifiers::OCL_ExplicitNone: + case Qualifiers::OCL_Strong: + case Qualifiers::OCL_Autoreleasing: + break; + } } QualType CanonicalType = getTypePtr()->CanonicalType; @@ -2099,7 +2110,18 @@ if ((*this)->isObjCLifetimeType()) return false; break; - } + } + } else if (Context.getLangOpts().ObjCWeak) { + switch (getObjCLifetime()) { + case Qualifiers::OCL_Weak: + return false; + + case Qualifiers::OCL_None: + case Qualifiers::OCL_ExplicitNone: + case Qualifiers::OCL_Strong: + case Qualifiers::OCL_Autoreleasing: + break; + } } QualType CanonicalType = getTypePtr()->CanonicalType; @@ -2151,7 +2173,18 @@ if ((*this)->isObjCLifetimeType()) return false; break; - } + } + } else if (Context.getLangOpts().ObjCWeak) { + switch (getObjCLifetime()) { + case Qualifiers::OCL_Weak: + return false; + + case Qualifiers::OCL_None: + case Qualifiers::OCL_ExplicitNone: + case Qualifiers::OCL_Strong: + case Qualifiers::OCL_Autoreleasing: + break; + } } // C++11 [basic.types]p9 @@ -2310,7 +2343,18 @@ case Qualifiers::OCL_None: break; - } + } + } else if (Context.getLangOpts().ObjCWeak) { + switch (getObjCLifetime()) { + case Qualifiers::OCL_Weak: + return false; + + case Qualifiers::OCL_ExplicitNone: + case Qualifiers::OCL_Strong: + case Qualifiers::OCL_Autoreleasing: + case Qualifiers::OCL_None: + break; + } } // C++11 [basic.types]p9: Index: lib/Sema/SemaExprCXX.cpp =================================================================== --- lib/Sema/SemaExprCXX.cpp +++ lib/Sema/SemaExprCXX.cpp @@ -4648,10 +4648,12 @@ return S.canThrow(Result.get()) == CT_Cannot; if (Kind == clang::TT_IsTriviallyConstructible) { - // Under Objective-C ARC, if the destination has non-trivial Objective-C - // lifetime, this is a non-trivial construction. - if (S.getLangOpts().ObjCAutoRefCount && - hasNontrivialObjCLifetime(T.getNonReferenceType())) + // Under Objective-C ARC and Weak, if the destination has non-trivial + // Objective-C lifetime, this is a non-trivial construction. + if ((S.getLangOpts().ObjCAutoRefCount && + hasNontrivialObjCLifetime(T.getNonReferenceType())) || + (S.getLangOpts().ObjCWeak && + T.getNonReferenceType().getObjCLifetime() == Qualifiers::OCL_Weak)) return false; // The initialization succeeded; now make sure there are no non-trivial @@ -4870,10 +4872,13 @@ return Self.canThrow(Result.get()) == CT_Cannot; if (BTT == BTT_IsTriviallyAssignable) { - // Under Objective-C ARC, if the destination has non-trivial Objective-C - // lifetime, this is a non-trivial assignment. - if (Self.getLangOpts().ObjCAutoRefCount && - hasNontrivialObjCLifetime(LhsT.getNonReferenceType())) + // Under Objective-C ARC and Weak, if the destination has non-trivial + // Objective-C lifetime, this is a non-trivial assignment. + if ((Self.getLangOpts().ObjCAutoRefCount && + hasNontrivialObjCLifetime(LhsT.getNonReferenceType())) || + (Self.getLangOpts().ObjCWeak && + LhsT.getNonReferenceType().getObjCLifetime() == + Qualifiers::OCL_Weak)) return false; return !Result.get()->hasNonTrivialCall(Self.Context); Index: test/SemaObjCXX/objc-weak-type-traits.mm =================================================================== --- /dev/null +++ test/SemaObjCXX/objc-weak-type-traits.mm @@ -0,0 +1,210 @@ +// RUN: %clang_cc1 -fsyntax-only -fobjc-weak -fobjc-runtime-has-weak -verify -std=c++11 %s +// expected-no-diagnostics + +// Check the results of the various type-trait query functions on +// lifetime-qualified types in ObjC Weak. + +#define TRAIT_IS_TRUE(Trait, Type) static_assert(Trait(Type), "") +#define TRAIT_IS_FALSE(Trait, Type) static_assert(!Trait(Type), "") +#define TRAIT_IS_TRUE_2(Trait, Type1, Type2) static_assert(Trait(Type1, Type2), "") +#define TRAIT_IS_FALSE_2(Trait, Type1, Type2) static_assert(!Trait(Type1, Type2), "") + +struct HasStrong { id obj; }; +struct HasWeak { __weak id obj; }; +struct HasUnsafeUnretained { __unsafe_unretained id obj; }; + +// __has_nothrow_assign +TRAIT_IS_TRUE(__has_nothrow_assign, __strong id); +TRAIT_IS_TRUE(__has_nothrow_assign, __weak id); +TRAIT_IS_TRUE(__has_nothrow_assign, __autoreleasing id); +TRAIT_IS_TRUE(__has_nothrow_assign, __unsafe_unretained id); +TRAIT_IS_TRUE(__has_nothrow_assign, HasStrong); +TRAIT_IS_TRUE(__has_nothrow_assign, HasWeak); +TRAIT_IS_TRUE(__has_nothrow_assign, HasUnsafeUnretained); + +// __has_nothrow_copy +TRAIT_IS_TRUE(__has_nothrow_copy, __strong id); +TRAIT_IS_TRUE(__has_nothrow_copy, __weak id); +TRAIT_IS_TRUE(__has_nothrow_copy, __autoreleasing id); +TRAIT_IS_TRUE(__has_nothrow_copy, __unsafe_unretained id); +TRAIT_IS_TRUE(__has_nothrow_copy, HasStrong); +TRAIT_IS_TRUE(__has_nothrow_copy, HasWeak); +TRAIT_IS_TRUE(__has_nothrow_copy, HasUnsafeUnretained); + +// __has_nothrow_constructor +TRAIT_IS_TRUE(__has_nothrow_constructor, __strong id); +TRAIT_IS_TRUE(__has_nothrow_constructor, __weak id); +TRAIT_IS_TRUE(__has_nothrow_constructor, __autoreleasing id); +TRAIT_IS_TRUE(__has_nothrow_constructor, __unsafe_unretained id); +TRAIT_IS_TRUE(__has_nothrow_constructor, HasStrong); +TRAIT_IS_TRUE(__has_nothrow_constructor, HasWeak); +TRAIT_IS_TRUE(__has_nothrow_constructor, HasUnsafeUnretained); + +// __has_trivial_assign +TRAIT_IS_TRUE(__has_trivial_assign, __strong id); +TRAIT_IS_FALSE(__has_trivial_assign, __weak id); +TRAIT_IS_TRUE(__has_trivial_assign, __autoreleasing id); +TRAIT_IS_TRUE(__has_trivial_assign, __unsafe_unretained id); +TRAIT_IS_TRUE(__has_trivial_assign, HasStrong); +TRAIT_IS_FALSE(__has_trivial_assign, HasWeak); +TRAIT_IS_TRUE(__has_trivial_assign, HasUnsafeUnretained); + +// __has_trivial_copy +TRAIT_IS_TRUE(__has_trivial_copy, __strong id); +TRAIT_IS_FALSE(__has_trivial_copy, __weak id); +TRAIT_IS_TRUE(__has_trivial_copy, __autoreleasing id); +TRAIT_IS_TRUE(__has_trivial_copy, __unsafe_unretained id); +TRAIT_IS_TRUE(__has_trivial_copy, HasStrong); +TRAIT_IS_FALSE(__has_trivial_copy, HasWeak); +TRAIT_IS_TRUE(__has_trivial_copy, HasUnsafeUnretained); + +// __has_trivial_constructor +TRAIT_IS_TRUE(__has_trivial_constructor, __strong id); +TRAIT_IS_FALSE(__has_trivial_constructor, __weak id); +TRAIT_IS_TRUE(__has_trivial_constructor, __autoreleasing id); +TRAIT_IS_TRUE(__has_trivial_constructor, __unsafe_unretained id); +TRAIT_IS_TRUE(__has_trivial_constructor, HasStrong); +TRAIT_IS_FALSE(__has_trivial_constructor, HasWeak); +TRAIT_IS_TRUE(__has_trivial_constructor, HasUnsafeUnretained); + +// __has_trivial_destructor +TRAIT_IS_TRUE(__has_trivial_destructor, __strong id); +TRAIT_IS_FALSE(__has_trivial_destructor, __weak id); +TRAIT_IS_TRUE(__has_trivial_destructor, __autoreleasing id); +TRAIT_IS_TRUE(__has_trivial_destructor, __unsafe_unretained id); +TRAIT_IS_TRUE(__has_trivial_destructor, HasStrong); +TRAIT_IS_FALSE(__has_trivial_destructor, HasWeak); +TRAIT_IS_TRUE(__has_trivial_destructor, HasUnsafeUnretained); + +// __is_literal +TRAIT_IS_TRUE(__is_literal, __strong id); +TRAIT_IS_TRUE(__is_literal, __weak id); +TRAIT_IS_TRUE(__is_literal, __autoreleasing id); +TRAIT_IS_TRUE(__is_literal, __unsafe_unretained id); + +// __is_literal_type +TRAIT_IS_TRUE(__is_literal_type, __strong id); +TRAIT_IS_TRUE(__is_literal_type, __weak id); +TRAIT_IS_TRUE(__is_literal_type, __autoreleasing id); +TRAIT_IS_TRUE(__is_literal_type, __unsafe_unretained id); + +// __is_pod +TRAIT_IS_TRUE(__is_pod, __strong id); +TRAIT_IS_FALSE(__is_pod, __weak id); +TRAIT_IS_TRUE(__is_pod, __autoreleasing id); +TRAIT_IS_TRUE(__is_pod, __unsafe_unretained id); +TRAIT_IS_TRUE(__is_pod, HasStrong); +TRAIT_IS_FALSE(__is_pod, HasWeak); +TRAIT_IS_TRUE(__is_pod, HasUnsafeUnretained); + +// __is_trivial +TRAIT_IS_TRUE(__is_trivial, __strong id); +TRAIT_IS_FALSE(__is_trivial, __weak id); +TRAIT_IS_TRUE(__is_trivial, __autoreleasing id); +TRAIT_IS_TRUE(__is_trivial, __unsafe_unretained id); +TRAIT_IS_TRUE(__is_trivial, HasStrong); +TRAIT_IS_FALSE(__is_trivial, HasWeak); +TRAIT_IS_TRUE(__is_trivial, HasUnsafeUnretained); + +// __is_scalar +TRAIT_IS_TRUE(__is_scalar, __strong id); +TRAIT_IS_FALSE(__is_scalar, __weak id); +TRAIT_IS_TRUE(__is_scalar, __autoreleasing id); +TRAIT_IS_TRUE(__is_scalar, __unsafe_unretained id); + +// __is_standard_layout +TRAIT_IS_TRUE(__is_standard_layout, __strong id); +TRAIT_IS_TRUE(__is_standard_layout, __weak id); +TRAIT_IS_TRUE(__is_standard_layout, __autoreleasing id); +TRAIT_IS_TRUE(__is_standard_layout, __unsafe_unretained id); + +// __is_trivally_assignable +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __strong id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __weak id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __autoreleasing id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __unsafe_unretained id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __strong id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __weak id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __autoreleasing id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __strong id&, __unsafe_unretained id&&); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __strong id); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __weak id); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __autoreleasing id); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __unsafe_unretained id); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __strong id&&); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __weak id&&); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __autoreleasing id&&); +TRAIT_IS_FALSE_2(__is_trivially_assignable, __weak id&, __unsafe_unretained id&&); + +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __strong id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __weak id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __autoreleasing id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __unsafe_unretained id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __strong id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __weak id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __autoreleasing id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __autoreleasing id&, __unsafe_unretained id&&); + +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __strong id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __weak id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __autoreleasing id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __unsafe_unretained id); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __strong id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __weak id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __autoreleasing id&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, __unsafe_unretained id&, __unsafe_unretained id&&); + +TRAIT_IS_TRUE_2(__is_trivially_assignable, HasStrong&, HasStrong); +TRAIT_IS_TRUE_2(__is_trivially_assignable, HasStrong&, HasStrong&&); +TRAIT_IS_FALSE_2(__is_trivially_assignable, HasWeak&, HasWeak); +TRAIT_IS_FALSE_2(__is_trivially_assignable, HasWeak&, HasWeak&&); +TRAIT_IS_TRUE_2(__is_trivially_assignable, HasUnsafeUnretained&, HasUnsafeUnretained); +TRAIT_IS_TRUE_2(__is_trivially_assignable, HasUnsafeUnretained&, HasUnsafeUnretained&&); + +// __is_trivally_constructible +TRAIT_IS_TRUE(__is_trivially_constructible, __strong id); +TRAIT_IS_FALSE(__is_trivially_constructible, __weak id); +TRAIT_IS_TRUE(__is_trivially_constructible, __autoreleasing id); +TRAIT_IS_TRUE(__is_trivially_constructible, __unsafe_unretained id); + +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __strong id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __weak id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __autoreleasing id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __unsafe_unretained id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __strong id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __weak id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __autoreleasing id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __strong id, __unsafe_unretained id&&); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __strong id); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __weak id); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __autoreleasing id); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __unsafe_unretained id); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __strong id&&); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __weak id&&); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __autoreleasing id&&); +TRAIT_IS_FALSE_2(__is_trivially_constructible, __weak id, __unsafe_unretained id&&); + +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __strong id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __weak id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __autoreleasing id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __unsafe_unretained id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __strong id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __weak id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __autoreleasing id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __autoreleasing id, __unsafe_unretained id&&); + +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __strong id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __weak id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __autoreleasing id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __unsafe_unretained id); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __strong id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __weak id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __autoreleasing id&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, __unsafe_unretained id, __unsafe_unretained id&&); + +TRAIT_IS_TRUE_2(__is_trivially_constructible, HasStrong, HasStrong); +TRAIT_IS_TRUE_2(__is_trivially_constructible, HasStrong, HasStrong&&); +TRAIT_IS_FALSE_2(__is_trivially_constructible, HasWeak, HasWeak); +TRAIT_IS_FALSE_2(__is_trivially_constructible, HasWeak, HasWeak&&); +TRAIT_IS_TRUE_2(__is_trivially_constructible, HasUnsafeUnretained, HasUnsafeUnretained); +TRAIT_IS_TRUE_2(__is_trivially_constructible, HasUnsafeUnretained, HasUnsafeUnretained&&);