2010-03-12 08:38:38 +08:00
|
|
|
//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for Objective C @property and
|
|
|
|
// @synthesize declarations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-26 06:03:47 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ASTMutationListener.h"
|
2010-08-24 15:21:54 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2011-10-07 02:38:18 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2012-05-22 01:02:43 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2015-01-17 07:04:31 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/Initialization.h"
|
2010-08-25 15:03:20 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-03-12 08:38:38 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Grammar actions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
/// getImpliedARCOwnership - Given a set of property attributes and a
|
|
|
|
/// type, infer an expected lifetime. The type's ownership qualification
|
|
|
|
/// is not considered.
|
|
|
|
///
|
|
|
|
/// Returns OCL_None if the attributes as stated do not imply an ownership.
|
|
|
|
/// Never returns OCL_Autoreleasing.
|
|
|
|
static Qualifiers::ObjCLifetime getImpliedARCOwnership(
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind attrs,
|
|
|
|
QualType type) {
|
|
|
|
// retain, strong, copy, weak, and unsafe_unretained are only legal
|
|
|
|
// on properties of retainable pointer type.
|
|
|
|
if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_strong |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_copy)) {
|
2012-08-21 07:36:59 +08:00
|
|
|
return Qualifiers::OCL_Strong;
|
2011-09-14 02:31:23 +08:00
|
|
|
} else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
|
|
|
|
return Qualifiers::OCL_Weak;
|
|
|
|
} else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
|
|
|
|
return Qualifiers::OCL_ExplicitNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign can appear on other types, so we have to check the
|
|
|
|
// property type.
|
|
|
|
if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
|
|
|
|
type->isObjCRetainableType()) {
|
|
|
|
return Qualifiers::OCL_ExplicitNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qualifiers::OCL_None;
|
|
|
|
}
|
|
|
|
|
2015-10-27 12:54:50 +08:00
|
|
|
/// Check the internal consistency of a property declaration with
|
|
|
|
/// an explicit ownership qualifier.
|
|
|
|
static void checkPropertyDeclWithOwnership(Sema &S,
|
|
|
|
ObjCPropertyDecl *property) {
|
2011-06-16 07:02:42 +08:00
|
|
|
if (property->isInvalidDecl()) return;
|
|
|
|
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind propertyKind
|
|
|
|
= property->getPropertyAttributes();
|
|
|
|
Qualifiers::ObjCLifetime propertyLifetime
|
|
|
|
= property->getType().getObjCLifetime();
|
|
|
|
|
2015-10-27 12:54:50 +08:00
|
|
|
assert(propertyLifetime != Qualifiers::OCL_None);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
Qualifiers::ObjCLifetime expectedLifetime
|
|
|
|
= getImpliedARCOwnership(propertyKind, property->getType());
|
|
|
|
if (!expectedLifetime) {
|
2011-06-16 07:02:42 +08:00
|
|
|
// We have a lifetime qualifier but no dominating property
|
2011-09-14 02:31:23 +08:00
|
|
|
// attribute. That's okay, but restore reasonable invariants by
|
|
|
|
// setting the property attribute according to the lifetime
|
|
|
|
// qualifier.
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind attr;
|
|
|
|
if (propertyLifetime == Qualifiers::OCL_Strong) {
|
|
|
|
attr = ObjCPropertyDecl::OBJC_PR_strong;
|
|
|
|
} else if (propertyLifetime == Qualifiers::OCL_Weak) {
|
|
|
|
attr = ObjCPropertyDecl::OBJC_PR_weak;
|
|
|
|
} else {
|
|
|
|
assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
|
|
|
|
attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
|
|
|
|
}
|
|
|
|
property->setPropertyAttributes(attr);
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (propertyLifetime == expectedLifetime) return;
|
|
|
|
|
|
|
|
property->setInvalidDecl();
|
|
|
|
S.Diag(property->getLocation(),
|
2011-06-24 08:08:59 +08:00
|
|
|
diag::err_arc_inconsistent_property_ownership)
|
2011-06-16 07:02:42 +08:00
|
|
|
<< property->getDeclName()
|
2011-09-14 02:31:23 +08:00
|
|
|
<< expectedLifetime
|
2011-06-16 07:02:42 +08:00
|
|
|
<< propertyLifetime;
|
|
|
|
}
|
|
|
|
|
2013-01-22 03:42:21 +08:00
|
|
|
/// \brief Check this Objective-C property against a property declared in the
|
|
|
|
/// given protocol.
|
|
|
|
static void
|
|
|
|
CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
|
|
|
|
ObjCProtocolDecl *Proto,
|
2014-08-18 07:49:53 +08:00
|
|
|
llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
|
2013-01-22 03:42:21 +08:00
|
|
|
// Have we seen this protocol before?
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!Known.insert(Proto).second)
|
2013-01-22 03:42:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Look for a property with the same name.
|
|
|
|
DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
|
|
|
|
for (unsigned I = 0, N = R.size(); I != N; ++I) {
|
|
|
|
if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
|
2013-10-05 02:06:08 +08:00
|
|
|
S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
|
2013-01-22 03:42:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check this property against any protocols we inherit.
|
2014-03-14 06:58:06 +08:00
|
|
|
for (auto *P : Proto->protocols())
|
|
|
|
CheckPropertyAgainstProtocol(S, Prop, P, Known);
|
2013-01-22 03:42:21 +08:00
|
|
|
}
|
|
|
|
|
2015-10-27 12:54:50 +08:00
|
|
|
static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
|
|
|
|
// In GC mode, just look for the __weak qualifier.
|
|
|
|
if (S.getLangOpts().getGC() != LangOptions::NonGC) {
|
|
|
|
if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
|
|
|
|
|
|
|
|
// In ARC/MRC, look for an explicit ownership qualifier.
|
|
|
|
// For some reason, this only applies to __weak.
|
|
|
|
} else if (auto ownership = T.getObjCLifetime()) {
|
|
|
|
switch (ownership) {
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
return ObjCDeclSpec::DQ_PR_weak;
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
return ObjCDeclSpec::DQ_PR_strong;
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
return ObjCDeclSpec::DQ_PR_unsafe_unretained;
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad qualifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-11 07:02:09 +08:00
|
|
|
static const unsigned OwnershipMask =
|
|
|
|
(ObjCPropertyDecl::OBJC_PR_assign |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_retain |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_copy |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_weak |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_strong |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
|
|
|
|
|
2015-10-27 12:54:50 +08:00
|
|
|
static unsigned getOwnershipRule(unsigned attr) {
|
2015-12-11 07:02:09 +08:00
|
|
|
unsigned result = attr & OwnershipMask;
|
|
|
|
|
|
|
|
// From an ownership perspective, assign and unsafe_unretained are
|
|
|
|
// identical; make sure one also implies the other.
|
|
|
|
if (result & (ObjCPropertyDecl::OBJC_PR_assign |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
|
|
|
|
result |= ObjCPropertyDecl::OBJC_PR_assign |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2015-10-27 12:54:50 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
|
2012-03-01 06:18:55 +08:00
|
|
|
SourceLocation LParenLoc,
|
2010-08-21 17:40:31 +08:00
|
|
|
FieldDeclarator &FD,
|
|
|
|
ObjCDeclSpec &ODS,
|
|
|
|
Selector GetterSel,
|
|
|
|
Selector SetterSel,
|
2010-09-24 05:18:05 +08:00
|
|
|
tok::ObjCKeywordKind MethodImplKind,
|
|
|
|
DeclContext *lexicalDC) {
|
2012-12-21 03:22:21 +08:00
|
|
|
unsigned Attributes = ODS.getPropertyAttributes();
|
2015-06-20 02:25:57 +08:00
|
|
|
FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
|
2011-06-16 07:02:42 +08:00
|
|
|
TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
|
|
|
|
QualType T = TSI->getType();
|
2015-10-27 12:54:50 +08:00
|
|
|
if (!getOwnershipRule(Attributes)) {
|
|
|
|
Attributes |= deducePropertyOwnershipFromType(*this, T);
|
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
|
2010-03-12 08:46:40 +08:00
|
|
|
// default is readwrite!
|
2012-12-21 03:22:21 +08:00
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_readonly));
|
2015-10-27 12:54:50 +08:00
|
|
|
|
2013-01-22 03:05:22 +08:00
|
|
|
// Proceed with constructing the ObjCPropertyDecls.
|
2011-08-22 23:54:49 +08:00
|
|
|
ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCPropertyDecl *Res = nullptr;
|
2013-01-22 03:05:22 +08:00
|
|
|
if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
|
2010-07-14 06:04:56 +08:00
|
|
|
if (CDecl->IsClassExtension()) {
|
2013-01-22 03:05:22 +08:00
|
|
|
Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
|
2017-03-17 02:25:40 +08:00
|
|
|
FD,
|
|
|
|
GetterSel, ODS.getGetterNameLoc(),
|
|
|
|
SetterSel, ODS.getSetterNameLoc(),
|
|
|
|
isReadWrite, Attributes,
|
2011-11-07 02:58:12 +08:00
|
|
|
ODS.getPropertyAttributes(),
|
2015-12-11 07:02:09 +08:00
|
|
|
T, TSI, MethodImplKind);
|
2013-01-22 03:05:22 +08:00
|
|
|
if (!Res)
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-07-14 06:04:56 +08:00
|
|
|
}
|
2013-01-22 03:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Res) {
|
|
|
|
Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
|
2017-03-17 02:25:40 +08:00
|
|
|
GetterSel, ODS.getGetterNameLoc(), SetterSel,
|
|
|
|
ODS.getSetterNameLoc(), isReadWrite, Attributes,
|
|
|
|
ODS.getPropertyAttributes(), T, TSI,
|
|
|
|
MethodImplKind);
|
2013-01-22 03:05:22 +08:00
|
|
|
if (lexicalDC)
|
|
|
|
Res->setLexicalDeclContext(lexicalDC);
|
|
|
|
}
|
2010-09-24 05:18:05 +08:00
|
|
|
|
2010-03-31 06:40:11 +08:00
|
|
|
// Validate the attributes on the @property.
|
2015-10-10 04:36:17 +08:00
|
|
|
CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
|
2012-06-21 06:57:42 +08:00
|
|
|
(isa<ObjCInterfaceDecl>(ClassDecl) ||
|
|
|
|
isa<ObjCProtocolDecl>(ClassDecl)));
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2015-10-27 12:54:50 +08:00
|
|
|
// Check consistency if the type has explicit ownership qualification.
|
|
|
|
if (Res->getType().getObjCLifetime())
|
|
|
|
checkPropertyDeclWithOwnership(*this, Res);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2013-01-22 03:42:21 +08:00
|
|
|
llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
|
2013-01-22 03:05:22 +08:00
|
|
|
if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
|
2013-01-22 03:42:21 +08:00
|
|
|
// For a class, compare the property against a property in our superclass.
|
|
|
|
bool FoundInSuper = false;
|
2014-02-15 08:04:36 +08:00
|
|
|
ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
|
|
|
|
while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
|
2013-01-22 03:05:22 +08:00
|
|
|
DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
|
2013-01-22 03:42:21 +08:00
|
|
|
for (unsigned I = 0, N = R.size(); I != N; ++I) {
|
|
|
|
if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
|
2013-10-05 02:06:08 +08:00
|
|
|
DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
|
2013-01-22 03:42:21 +08:00
|
|
|
FoundInSuper = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-15 08:04:36 +08:00
|
|
|
if (FoundInSuper)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
CurrentInterfaceDecl = Super;
|
2013-01-22 03:42:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FoundInSuper) {
|
|
|
|
// Also compare the property against a property in our protocols.
|
2014-03-14 04:29:09 +08:00
|
|
|
for (auto *P : CurrentInterfaceDecl->protocols()) {
|
|
|
|
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
|
2013-01-22 03:42:21 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Slower path: look in all protocols we referenced.
|
2014-03-14 04:55:22 +08:00
|
|
|
for (auto *P : IFace->all_referenced_protocols()) {
|
|
|
|
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
|
2013-01-22 03:42:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
// We don't check if class extension. Because properties in class extension
|
|
|
|
// are meant to override some of the attributes and checking has already done
|
|
|
|
// when property in class extension is constructed.
|
|
|
|
if (!Cat->IsClassExtension())
|
|
|
|
for (auto *P : Cat->protocols())
|
|
|
|
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
|
2013-01-22 03:42:21 +08:00
|
|
|
} else {
|
|
|
|
ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
|
2014-03-14 06:58:06 +08:00
|
|
|
for (auto *P : Proto->protocols())
|
|
|
|
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
|
2013-01-22 03:05:22 +08:00
|
|
|
}
|
|
|
|
|
2012-07-13 09:06:46 +08:00
|
|
|
ActOnDocumentableDecl(Res);
|
2010-03-31 06:40:11 +08:00
|
|
|
return Res;
|
2010-03-12 10:31:10 +08:00
|
|
|
}
|
|
|
|
|
2011-10-19 03:49:16 +08:00
|
|
|
static ObjCPropertyDecl::PropertyAttributeKind
|
2012-12-21 03:22:21 +08:00
|
|
|
makePropertyAttributesAsWritten(unsigned Attributes) {
|
2011-10-19 03:49:16 +08:00
|
|
|
unsigned attributesAsWritten = 0;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_getter)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_setter)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_assign)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_weak)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
|
2011-10-19 03:49:16 +08:00
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
|
2016-01-27 02:52:43 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_class)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
|
2011-10-19 03:49:16 +08:00
|
|
|
|
|
|
|
return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
|
|
|
|
}
|
|
|
|
|
2012-05-22 01:10:28 +08:00
|
|
|
static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
|
2012-05-22 01:02:43 +08:00
|
|
|
SourceLocation LParenLoc, SourceLocation &Loc) {
|
|
|
|
if (LParenLoc.isMacroID())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceManager &SM = Context.getSourceManager();
|
|
|
|
std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
|
|
|
|
// Try to load the file buffer.
|
|
|
|
bool invalidTemp = false;
|
|
|
|
StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
|
|
|
|
if (invalidTemp)
|
|
|
|
return false;
|
|
|
|
const char *tokenBegin = file.data() + locInfo.second;
|
|
|
|
|
|
|
|
// Lex from the start of the given location.
|
|
|
|
Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
|
|
|
|
Context.getLangOpts(),
|
|
|
|
file.begin(), tokenBegin, file.end());
|
|
|
|
Token Tok;
|
|
|
|
do {
|
|
|
|
lexer.LexFromRawLexer(Tok);
|
2014-05-17 12:53:25 +08:00
|
|
|
if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
|
2012-05-22 01:02:43 +08:00
|
|
|
Loc = Tok.getLocation();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (Tok.isNot(tok::r_paren));
|
|
|
|
return false;
|
2012-05-20 02:17:17 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 06:57:32 +08:00
|
|
|
/// Check for a mismatch in the atomicity of the given properties.
|
|
|
|
static void checkAtomicPropertyMismatch(Sema &S,
|
|
|
|
ObjCPropertyDecl *OldProperty,
|
2015-12-11 07:02:09 +08:00
|
|
|
ObjCPropertyDecl *NewProperty,
|
|
|
|
bool PropagateAtomicity) {
|
2015-12-10 06:57:32 +08:00
|
|
|
// If the atomicity of both matches, we're done.
|
|
|
|
bool OldIsAtomic =
|
2015-12-11 07:02:09 +08:00
|
|
|
(OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
|
|
|
|
== 0;
|
2015-12-10 06:57:32 +08:00
|
|
|
bool NewIsAtomic =
|
2015-12-11 07:02:09 +08:00
|
|
|
(NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
|
|
|
|
== 0;
|
2015-12-10 06:57:32 +08:00
|
|
|
if (OldIsAtomic == NewIsAtomic) return;
|
|
|
|
|
|
|
|
// Determine whether the given property is readonly and implicitly
|
|
|
|
// atomic.
|
|
|
|
auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
|
|
|
|
// Is it readonly?
|
|
|
|
auto Attrs = Property->getPropertyAttributes();
|
2015-12-11 07:02:09 +08:00
|
|
|
if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
|
2015-12-10 06:57:32 +08:00
|
|
|
|
|
|
|
// Is it nonatomic?
|
2015-12-11 07:02:09 +08:00
|
|
|
if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
|
2015-12-10 06:57:32 +08:00
|
|
|
|
|
|
|
// Was 'atomic' specified directly?
|
2015-12-11 07:02:09 +08:00
|
|
|
if (Property->getPropertyAttributesAsWritten() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_atomic)
|
2015-12-10 06:57:32 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-12-11 07:02:09 +08:00
|
|
|
// If we're allowed to propagate atomicity, and the new property did
|
|
|
|
// not specify atomicity at all, propagate.
|
|
|
|
const unsigned AtomicityMask =
|
|
|
|
(ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
|
|
|
|
if (PropagateAtomicity &&
|
|
|
|
((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
|
|
|
|
unsigned Attrs = NewProperty->getPropertyAttributes();
|
|
|
|
Attrs = Attrs & ~AtomicityMask;
|
|
|
|
if (OldIsAtomic)
|
|
|
|
Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
|
|
|
|
else
|
|
|
|
Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
|
|
|
|
|
|
|
|
NewProperty->overwritePropertyAttributes(Attrs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-10 06:57:32 +08:00
|
|
|
// One of the properties is atomic; if it's a readonly property, and
|
|
|
|
// 'atomic' wasn't explicitly specified, we're okay.
|
|
|
|
if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
|
|
|
|
(NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Diagnose the conflict.
|
|
|
|
const IdentifierInfo *OldContextName;
|
|
|
|
auto *OldDC = OldProperty->getDeclContext();
|
|
|
|
if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
|
|
|
|
OldContextName = Category->getClassInterface()->getIdentifier();
|
|
|
|
else
|
|
|
|
OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
|
|
|
|
|
|
|
|
S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< NewProperty->getDeclName() << "atomic"
|
|
|
|
<< OldContextName;
|
|
|
|
S.Diag(OldProperty->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
|
2013-01-22 03:05:22 +08:00
|
|
|
ObjCPropertyDecl *
|
2011-08-22 23:54:49 +08:00
|
|
|
Sema::HandlePropertyInClassExtension(Scope *S,
|
2012-03-01 06:18:55 +08:00
|
|
|
SourceLocation AtLoc,
|
|
|
|
SourceLocation LParenLoc,
|
|
|
|
FieldDeclarator &FD,
|
2017-03-17 02:25:40 +08:00
|
|
|
Selector GetterSel,
|
|
|
|
SourceLocation GetterNameLoc,
|
|
|
|
Selector SetterSel,
|
|
|
|
SourceLocation SetterNameLoc,
|
2010-03-12 10:31:10 +08:00
|
|
|
const bool isReadWrite,
|
2015-12-11 07:02:09 +08:00
|
|
|
unsigned &Attributes,
|
2011-11-07 02:58:12 +08:00
|
|
|
const unsigned AttributesAsWritten,
|
2015-06-20 02:14:38 +08:00
|
|
|
QualType T,
|
|
|
|
TypeSourceInfo *TSI,
|
2010-03-12 10:31:10 +08:00
|
|
|
tok::ObjCKeywordKind MethodImplKind) {
|
2011-08-23 02:34:22 +08:00
|
|
|
ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
|
2010-03-12 10:31:10 +08:00
|
|
|
// Diagnose if this property is already in continuation class.
|
2011-08-22 23:54:49 +08:00
|
|
|
DeclContext *DC = CurContext;
|
2010-03-12 10:31:10 +08:00
|
|
|
IdentifierInfo *PropertyId = FD.D.getIdentifier();
|
2010-11-11 02:01:36 +08:00
|
|
|
ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
|
|
|
|
|
2010-03-12 10:31:10 +08:00
|
|
|
// We need to look in the @interface to see if the @property was
|
|
|
|
// already declared.
|
|
|
|
if (!CCPrimary) {
|
|
|
|
Diag(CDecl->getLocation(), diag::err_continuation_class);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 10:31:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 02:49:28 +08:00
|
|
|
bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_class);
|
|
|
|
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
// Find the property in the extended class's primary class or
|
|
|
|
// extensions.
|
2016-01-29 02:49:28 +08:00
|
|
|
ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
|
|
|
|
PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
|
2010-03-12 10:31:10 +08:00
|
|
|
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
// If we found a property in an extension, complain.
|
|
|
|
if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
|
|
|
|
Diag(AtLoc, diag::err_duplicate_property);
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-11 07:02:09 +08:00
|
|
|
// Check for consistency with the previous declaration, if there is one.
|
|
|
|
if (PIDecl) {
|
|
|
|
// A readonly property declared in the primary class can be refined
|
|
|
|
// by adding a readwrite property within an extension.
|
|
|
|
// Anything else is an error.
|
|
|
|
if (!(PIDecl->isReadOnly() && isReadWrite)) {
|
|
|
|
// Tailor the diagnostics for the common case where a readwrite
|
|
|
|
// property is declared both in the @interface and the continuation.
|
|
|
|
// This is a common error where the user often intended the original
|
|
|
|
// declaration to be readonly.
|
|
|
|
unsigned diag =
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
|
|
|
|
(PIDecl->getPropertyAttributesAsWritten() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_readwrite)
|
|
|
|
? diag::err_use_continuation_class_redeclaration_readwrite
|
|
|
|
: diag::err_use_continuation_class;
|
|
|
|
Diag(AtLoc, diag)
|
|
|
|
<< CCPrimary->getDeclName();
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for consistency of getters.
|
|
|
|
if (PIDecl->getGetterName() != GetterSel) {
|
|
|
|
// If the getter was written explicitly, complain.
|
|
|
|
if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
|
|
|
|
Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
|
|
|
|
<< PIDecl->getGetterName() << GetterSel;
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always adopt the getter from the original declaration.
|
|
|
|
GetterSel = PIDecl->getGetterName();
|
|
|
|
Attributes |= ObjCDeclSpec::DQ_PR_getter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check consistency of ownership.
|
|
|
|
unsigned ExistingOwnership
|
|
|
|
= getOwnershipRule(PIDecl->getPropertyAttributes());
|
|
|
|
unsigned NewOwnership = getOwnershipRule(Attributes);
|
|
|
|
if (ExistingOwnership && NewOwnership != ExistingOwnership) {
|
|
|
|
// If the ownership was written explicitly, complain.
|
|
|
|
if (getOwnershipRule(AttributesAsWritten)) {
|
|
|
|
Diag(AtLoc, diag::warn_property_attr_mismatch);
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take the ownership from the original property.
|
|
|
|
Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the redeclaration is 'weak' but the original property is not,
|
|
|
|
if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
|
|
|
|
!(PIDecl->getPropertyAttributesAsWritten()
|
|
|
|
& ObjCPropertyDecl::OBJC_PR_weak) &&
|
|
|
|
PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
|
|
|
|
PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
|
|
|
|
Diag(AtLoc, diag::warn_property_implicitly_mismatched);
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
// Create a new ObjCPropertyDecl with the DeclContext being
|
|
|
|
// the class extension.
|
|
|
|
ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
|
2017-03-17 02:25:40 +08:00
|
|
|
FD, GetterSel, GetterNameLoc,
|
|
|
|
SetterSel, SetterNameLoc,
|
2015-12-11 07:02:09 +08:00
|
|
|
isReadWrite,
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
Attributes, AttributesAsWritten,
|
|
|
|
T, TSI, MethodImplKind, DC);
|
|
|
|
|
|
|
|
// If there was no declaration of a property with the same name in
|
|
|
|
// the primary class, we're done.
|
2010-03-12 10:31:10 +08:00
|
|
|
if (!PIDecl) {
|
2015-11-04 01:02:34 +08:00
|
|
|
ProcessPropertyDecl(PDecl);
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
return PDecl;
|
2010-03-12 10:31:10 +08:00
|
|
|
}
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
|
2012-02-03 02:54:58 +08:00
|
|
|
if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
|
|
|
|
bool IncompatibleObjC = false;
|
|
|
|
QualType ConvertedType;
|
2012-02-03 03:34:05 +08:00
|
|
|
// Relax the strict type matching for property type in continuation class.
|
|
|
|
// Allow property object type of continuation class to be different as long
|
2012-02-03 06:37:48 +08:00
|
|
|
// as it narrows the object type in its primary class property. Note that
|
|
|
|
// this conversion is safe only because the wider type is for a 'readonly'
|
|
|
|
// property in primary class and 'narrowed' type for a 'readwrite' property
|
|
|
|
// in continuation class.
|
2015-04-09 05:34:04 +08:00
|
|
|
QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
|
|
|
|
QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
|
|
|
|
if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
|
|
|
|
!isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
|
|
|
|
(!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
|
2012-02-03 02:54:58 +08:00
|
|
|
ConvertedType, IncompatibleObjC))
|
|
|
|
|| IncompatibleObjC) {
|
|
|
|
Diag(AtLoc,
|
|
|
|
diag::err_type_mismatch_continuation_class) << PDecl->getType();
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2012-02-03 02:54:58 +08:00
|
|
|
}
|
2011-09-24 08:56:59 +08:00
|
|
|
}
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
|
|
|
|
// Check that atomicity of property in class extension matches the previous
|
|
|
|
// declaration.
|
2015-12-11 07:02:09 +08:00
|
|
|
checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
|
2015-11-04 01:02:34 +08:00
|
|
|
// Make sure getter/setter are appropriately synthesized.
|
|
|
|
ProcessPropertyDecl(PDecl);
|
2012-09-18 04:57:19 +08:00
|
|
|
return PDecl;
|
2010-03-12 10:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
|
|
|
|
ObjCContainerDecl *CDecl,
|
|
|
|
SourceLocation AtLoc,
|
2012-03-01 06:18:55 +08:00
|
|
|
SourceLocation LParenLoc,
|
2010-03-12 10:31:10 +08:00
|
|
|
FieldDeclarator &FD,
|
|
|
|
Selector GetterSel,
|
2017-03-17 02:25:40 +08:00
|
|
|
SourceLocation GetterNameLoc,
|
2010-03-12 10:31:10 +08:00
|
|
|
Selector SetterSel,
|
2017-03-17 02:25:40 +08:00
|
|
|
SourceLocation SetterNameLoc,
|
2010-03-12 10:31:10 +08:00
|
|
|
const bool isReadWrite,
|
2012-12-21 03:22:21 +08:00
|
|
|
const unsigned Attributes,
|
2011-11-07 02:58:12 +08:00
|
|
|
const unsigned AttributesAsWritten,
|
2015-06-20 02:14:38 +08:00
|
|
|
QualType T,
|
2010-06-05 04:50:08 +08:00
|
|
|
TypeSourceInfo *TInfo,
|
2010-05-19 05:09:07 +08:00
|
|
|
tok::ObjCKeywordKind MethodImplKind,
|
|
|
|
DeclContext *lexicalDC){
|
2010-03-12 10:31:10 +08:00
|
|
|
IdentifierInfo *PropertyId = FD.D.getIdentifier();
|
2010-03-12 08:46:40 +08:00
|
|
|
|
2015-12-11 07:02:09 +08:00
|
|
|
// Property defaults to 'assign' if it is readwrite, unless this is ARC
|
|
|
|
// and the type is retainable.
|
|
|
|
bool isAssign;
|
|
|
|
if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
|
|
|
|
ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
|
|
|
|
isAssign = true;
|
|
|
|
} else if (getOwnershipRule(Attributes) || !isReadWrite) {
|
|
|
|
isAssign = false;
|
|
|
|
} else {
|
|
|
|
isAssign = (!getLangOpts().ObjCAutoRefCount ||
|
|
|
|
!T->isObjCRetainableType());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue a warning if property is 'assign' as default and its
|
|
|
|
// object, which is gc'able conforms to NSCopying protocol
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().getGC() != LangOptions::NonGC &&
|
2015-12-11 07:02:09 +08:00
|
|
|
isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
|
2010-05-15 19:32:37 +08:00
|
|
|
if (const ObjCObjectPointerType *ObjPtrTy =
|
|
|
|
T->getAs<ObjCObjectPointerType>()) {
|
|
|
|
ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
|
|
|
|
if (IDecl)
|
|
|
|
if (ObjCProtocolDecl* PNSCopying =
|
|
|
|
LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
|
|
|
|
if (IDecl->ClassImplementsProtocol(PNSCopying, true))
|
|
|
|
Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2015-12-11 07:02:09 +08:00
|
|
|
}
|
2013-07-09 09:38:07 +08:00
|
|
|
|
|
|
|
if (T->isObjCObjectType()) {
|
|
|
|
SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
|
2014-05-03 11:45:55 +08:00
|
|
|
StarLoc = getLocForEndOfToken(StarLoc);
|
2013-07-09 09:38:07 +08:00
|
|
|
Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
|
|
|
|
<< FixItHint::CreateInsertion(StarLoc, "*");
|
|
|
|
T = Context.getObjCObjectPointerType(T);
|
|
|
|
SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
|
|
|
|
TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
|
|
|
|
}
|
2010-03-12 08:46:40 +08:00
|
|
|
|
2010-03-12 10:31:10 +08:00
|
|
|
DeclContext *DC = cast<DeclContext>(CDecl);
|
2010-03-12 08:46:40 +08:00
|
|
|
ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
|
|
|
|
FD.D.getIdentifierLoc(),
|
2015-06-20 02:14:38 +08:00
|
|
|
PropertyId, AtLoc,
|
|
|
|
LParenLoc, T, TInfo);
|
2010-03-12 10:31:10 +08:00
|
|
|
|
2016-01-29 02:49:28 +08:00
|
|
|
bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_class);
|
|
|
|
// Class property and instance property can have the same name.
|
|
|
|
if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
|
|
|
|
DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(PDecl->getLocation(), diag::err_duplicate_property);
|
2010-03-16 02:47:25 +08:00
|
|
|
Diag(prevDecl->getLocation(), diag::note_property_declare);
|
2010-03-12 08:46:40 +08:00
|
|
|
PDecl->setInvalidDecl();
|
|
|
|
}
|
2010-05-19 05:09:07 +08:00
|
|
|
else {
|
2010-03-12 08:46:40 +08:00
|
|
|
DC->addDecl(PDecl);
|
2010-05-19 05:09:07 +08:00
|
|
|
if (lexicalDC)
|
|
|
|
PDecl->setLexicalDeclContext(lexicalDC);
|
|
|
|
}
|
2010-03-12 08:46:40 +08:00
|
|
|
|
|
|
|
if (T->isArrayType() || T->isFunctionType()) {
|
|
|
|
Diag(AtLoc, diag::err_property_type) << T;
|
|
|
|
PDecl->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessDeclAttributes(S, PDecl, FD.D);
|
|
|
|
|
|
|
|
// Regardless of setter/getter attribute, we save the default getter/setter
|
|
|
|
// selector names in anticipation of declaration of setter/getter methods.
|
2017-03-17 02:25:40 +08:00
|
|
|
PDecl->setGetterName(GetterSel, GetterNameLoc);
|
|
|
|
PDecl->setSetterName(SetterSel, SetterNameLoc);
|
2011-07-12 12:30:16 +08:00
|
|
|
PDecl->setPropertyAttributesAsWritten(
|
2011-11-07 02:58:12 +08:00
|
|
|
makePropertyAttributesAsWritten(AttributesAsWritten));
|
2011-07-12 12:30:16 +08:00
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
|
2010-03-12 08:46:40 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_getter)
|
2010-03-12 08:46:40 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_setter)
|
2010-03-12 08:46:40 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
|
|
|
|
|
|
|
|
if (isReadWrite)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain)
|
2010-03-12 08:46:40 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong)
|
2011-06-16 07:02:42 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_weak)
|
2011-06-16 07:02:42 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
|
2010-03-12 08:46:40 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
|
2011-06-16 07:02:42 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (isAssign)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
|
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
// In the semantic attributes, one of nonatomic or atomic is always set.
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
|
2010-03-12 08:46:40 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
|
2011-09-14 02:31:23 +08:00
|
|
|
else
|
2011-06-11 08:45:12 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
|
2010-03-12 08:46:40 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// 'unsafe_unretained' is alias for 'assign'.
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
|
2011-06-16 07:02:42 +08:00
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
|
|
|
|
if (isAssign)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (MethodImplKind == tok::objc_required)
|
|
|
|
PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
|
|
|
|
else if (MethodImplKind == tok::objc_optional)
|
|
|
|
PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
|
|
|
|
|
2015-06-20 02:14:38 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
|
|
|
|
|
2015-06-20 02:14:46 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
|
|
|
|
|
2016-01-27 02:52:43 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_class)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
|
|
|
|
|
2010-03-12 10:31:10 +08:00
|
|
|
return PDecl;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
|
|
|
|
ObjCPropertyDecl *property,
|
|
|
|
ObjCIvarDecl *ivar) {
|
|
|
|
if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
|
|
|
|
|
|
|
|
QualType ivarType = ivar->getType();
|
|
|
|
Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
|
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
// The lifetime implied by the property's attributes.
|
|
|
|
Qualifiers::ObjCLifetime propertyLifetime =
|
|
|
|
getImpliedARCOwnership(property->getPropertyAttributes(),
|
|
|
|
property->getType());
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
// We're fine if they match.
|
|
|
|
if (propertyLifetime == ivarLifetime) return;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
// None isn't a valid lifetime for an object ivar in ARC, and
|
|
|
|
// __autoreleasing is never valid; don't diagnose twice.
|
|
|
|
if ((ivarLifetime == Qualifiers::OCL_None &&
|
|
|
|
S.getLangOpts().ObjCAutoRefCount) ||
|
2011-09-14 02:31:23 +08:00
|
|
|
ivarLifetime == Qualifiers::OCL_Autoreleasing)
|
|
|
|
return;
|
|
|
|
|
2012-08-21 07:36:59 +08:00
|
|
|
// If the ivar is private, and it's implicitly __unsafe_unretained
|
|
|
|
// becaues of its type, then pretend it was actually implicitly
|
|
|
|
// __strong. This is only sound because we're processing the
|
|
|
|
// property implementation before parsing any method bodies.
|
|
|
|
if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
|
|
|
|
propertyLifetime == Qualifiers::OCL_Strong &&
|
|
|
|
ivar->getAccessControl() == ObjCIvarDecl::Private) {
|
|
|
|
SplitQualType split = ivarType.split();
|
|
|
|
if (split.Quals.hasObjCLifetime()) {
|
|
|
|
assert(ivarType->isObjCARCImplicitlyUnretainedType());
|
|
|
|
split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
|
|
|
|
ivarType = S.Context.getQualifiedType(split);
|
|
|
|
ivar->setType(ivarType);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
switch (propertyLifetime) {
|
|
|
|
case Qualifiers::OCL_Strong:
|
2012-12-13 06:48:25 +08:00
|
|
|
S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
|
2011-09-14 02:31:23 +08:00
|
|
|
<< property->getDeclName()
|
|
|
|
<< ivar->getDeclName()
|
|
|
|
<< ivarLifetime;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
2016-12-03 06:38:31 +08:00
|
|
|
S.Diag(ivar->getLocation(), diag::err_weak_property)
|
2011-09-14 02:31:23 +08:00
|
|
|
<< property->getDeclName()
|
|
|
|
<< ivar->getDeclName();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
2012-12-13 06:48:25 +08:00
|
|
|
S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
|
2011-09-14 02:31:23 +08:00
|
|
|
<< property->getDeclName()
|
|
|
|
<< ivar->getDeclName()
|
|
|
|
<< ((property->getPropertyAttributesAsWritten()
|
|
|
|
& ObjCPropertyDecl::OBJC_PR_assign) != 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
llvm_unreachable("properties cannot be autoreleasing");
|
|
|
|
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
// Any other property should be ignored.
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(property->getLocation(), diag::note_property_declare);
|
2012-12-13 06:48:25 +08:00
|
|
|
if (propertyImplLoc.isValid())
|
|
|
|
S.Diag(propertyImplLoc, diag::note_property_synthesize);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:26:06 +08:00
|
|
|
/// setImpliedPropertyAttributeForReadOnlyProperty -
|
|
|
|
/// This routine evaludates life-time attributes for a 'readonly'
|
|
|
|
/// property with no known lifetime of its own, using backing
|
|
|
|
/// 'ivar's attribute, if any. If no backing 'ivar', property's
|
|
|
|
/// life-time is assumed 'strong'.
|
|
|
|
static void setImpliedPropertyAttributeForReadOnlyProperty(
|
|
|
|
ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
|
|
|
|
Qualifiers::ObjCLifetime propertyLifetime =
|
|
|
|
getImpliedARCOwnership(property->getPropertyAttributes(),
|
|
|
|
property->getType());
|
|
|
|
if (propertyLifetime != Qualifiers::OCL_None)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ivar) {
|
|
|
|
// if no backing ivar, make property 'strong'.
|
|
|
|
property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// property assumes owenership of backing ivar.
|
|
|
|
QualType ivarType = ivar->getType();
|
|
|
|
Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
|
|
|
|
if (ivarLifetime == Qualifiers::OCL_Strong)
|
|
|
|
property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
|
|
|
|
else if (ivarLifetime == Qualifiers::OCL_Weak)
|
|
|
|
property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
|
|
|
|
}
|
2010-03-12 08:46:40 +08:00
|
|
|
|
2017-07-13 19:06:22 +08:00
|
|
|
static bool
|
|
|
|
isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind Kind) {
|
|
|
|
return (Attr1 & Kind) != (Attr2 & Kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
|
|
|
|
unsigned Kinds) {
|
|
|
|
return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
|
|
|
|
/// property declaration that should be synthesised in all of the inherited
|
|
|
|
/// protocols. It also diagnoses properties declared in inherited protocols with
|
|
|
|
/// mismatched types or attributes, since any of them can be candidate for
|
|
|
|
/// synthesis.
|
|
|
|
static ObjCPropertyDecl *
|
|
|
|
SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
|
2013-05-23 23:53:44 +08:00
|
|
|
ObjCInterfaceDecl *ClassDecl,
|
2013-05-21 05:20:24 +08:00
|
|
|
ObjCPropertyDecl *Property) {
|
2017-07-13 19:06:22 +08:00
|
|
|
assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
|
|
|
|
"Expected a property from a protocol");
|
|
|
|
ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
|
|
|
|
ObjCInterfaceDecl::PropertyDeclOrder Properties;
|
2014-03-14 04:55:22 +08:00
|
|
|
for (const auto *PI : ClassDecl->all_referenced_protocols()) {
|
|
|
|
if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
|
2017-07-13 19:06:22 +08:00
|
|
|
PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
|
|
|
|
Properties);
|
2013-05-21 05:20:24 +08:00
|
|
|
}
|
2017-07-13 19:06:22 +08:00
|
|
|
if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
|
2013-05-21 05:20:24 +08:00
|
|
|
while (SDecl) {
|
2014-03-14 04:55:22 +08:00
|
|
|
for (const auto *PI : SDecl->all_referenced_protocols()) {
|
|
|
|
if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
|
2017-07-13 19:06:22 +08:00
|
|
|
PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
|
|
|
|
Properties);
|
2013-05-21 05:20:24 +08:00
|
|
|
}
|
|
|
|
SDecl = SDecl->getSuperClass();
|
|
|
|
}
|
2017-07-13 19:06:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Properties.empty())
|
|
|
|
return Property;
|
|
|
|
|
|
|
|
ObjCPropertyDecl *OriginalProperty = Property;
|
|
|
|
size_t SelectedIndex = 0;
|
|
|
|
for (const auto &Prop : llvm::enumerate(Properties)) {
|
|
|
|
// Select the 'readwrite' property if such property exists.
|
|
|
|
if (Property->isReadOnly() && !Prop.value()->isReadOnly()) {
|
|
|
|
Property = Prop.value();
|
|
|
|
SelectedIndex = Prop.index();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Property != OriginalProperty) {
|
|
|
|
// Check that the old property is compatible with the new one.
|
|
|
|
Properties[SelectedIndex] = OriginalProperty;
|
|
|
|
}
|
|
|
|
|
2013-05-21 05:20:24 +08:00
|
|
|
QualType RHSType = S.Context.getCanonicalType(Property->getType());
|
2017-08-22 18:38:07 +08:00
|
|
|
unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten();
|
2017-07-13 19:06:22 +08:00
|
|
|
enum MismatchKind {
|
|
|
|
IncompatibleType = 0,
|
|
|
|
HasNoExpectedAttribute,
|
|
|
|
HasUnexpectedAttribute,
|
|
|
|
DifferentGetter,
|
|
|
|
DifferentSetter
|
|
|
|
};
|
|
|
|
// Represents a property from another protocol that conflicts with the
|
|
|
|
// selected declaration.
|
|
|
|
struct MismatchingProperty {
|
|
|
|
const ObjCPropertyDecl *Prop;
|
|
|
|
MismatchKind Kind;
|
|
|
|
StringRef AttributeName;
|
|
|
|
};
|
|
|
|
SmallVector<MismatchingProperty, 4> Mismatches;
|
|
|
|
for (ObjCPropertyDecl *Prop : Properties) {
|
|
|
|
// Verify the property attributes.
|
2017-08-22 18:38:07 +08:00
|
|
|
unsigned Attr = Prop->getPropertyAttributesAsWritten();
|
2017-07-13 19:06:22 +08:00
|
|
|
if (Attr != OriginalAttributes) {
|
|
|
|
auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
|
|
|
|
MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
|
|
|
|
: HasUnexpectedAttribute;
|
|
|
|
Mismatches.push_back({Prop, Kind, AttributeName});
|
|
|
|
};
|
|
|
|
if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
|
|
|
|
ObjCPropertyDecl::OBJC_PR_copy)) {
|
|
|
|
Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (areIncompatiblePropertyAttributes(
|
|
|
|
OriginalAttributes, Attr, ObjCPropertyDecl::OBJC_PR_retain |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_strong)) {
|
|
|
|
Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_strong),
|
|
|
|
"retain (or strong)");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
|
|
|
|
ObjCPropertyDecl::OBJC_PR_atomic)) {
|
|
|
|
Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Property->getGetterName() != Prop->getGetterName()) {
|
|
|
|
Mismatches.push_back({Prop, DifferentGetter, ""});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!Property->isReadOnly() && !Prop->isReadOnly() &&
|
|
|
|
Property->getSetterName() != Prop->getSetterName()) {
|
|
|
|
Mismatches.push_back({Prop, DifferentSetter, ""});
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-21 05:20:24 +08:00
|
|
|
QualType LHSType = S.Context.getCanonicalType(Prop->getType());
|
|
|
|
if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
|
|
|
|
bool IncompatibleObjC = false;
|
|
|
|
QualType ConvertedType;
|
|
|
|
if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
|
|
|
|
|| IncompatibleObjC) {
|
2017-07-13 19:06:22 +08:00
|
|
|
Mismatches.push_back({Prop, IncompatibleType, ""});
|
|
|
|
continue;
|
2013-05-21 05:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 19:06:22 +08:00
|
|
|
|
|
|
|
if (Mismatches.empty())
|
|
|
|
return Property;
|
|
|
|
|
|
|
|
// Diagnose incompability.
|
|
|
|
{
|
|
|
|
bool HasIncompatibleAttributes = false;
|
|
|
|
for (const auto &Note : Mismatches)
|
|
|
|
HasIncompatibleAttributes =
|
|
|
|
Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes;
|
|
|
|
// Promote the warning to an error if there are incompatible attributes or
|
|
|
|
// incompatible types together with readwrite/readonly incompatibility.
|
|
|
|
auto Diag = S.Diag(Property->getLocation(),
|
|
|
|
Property != OriginalProperty || HasIncompatibleAttributes
|
|
|
|
? diag::err_protocol_property_mismatch
|
|
|
|
: diag::warn_protocol_property_mismatch);
|
|
|
|
Diag << Mismatches[0].Kind;
|
|
|
|
switch (Mismatches[0].Kind) {
|
|
|
|
case IncompatibleType:
|
|
|
|
Diag << Property->getType();
|
|
|
|
break;
|
|
|
|
case HasNoExpectedAttribute:
|
|
|
|
case HasUnexpectedAttribute:
|
|
|
|
Diag << Mismatches[0].AttributeName;
|
|
|
|
break;
|
|
|
|
case DifferentGetter:
|
|
|
|
Diag << Property->getGetterName();
|
|
|
|
break;
|
|
|
|
case DifferentSetter:
|
|
|
|
Diag << Property->getSetterName();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &Note : Mismatches) {
|
|
|
|
auto Diag =
|
|
|
|
S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
|
|
|
|
<< Note.Kind;
|
|
|
|
switch (Note.Kind) {
|
|
|
|
case IncompatibleType:
|
|
|
|
Diag << Note.Prop->getType();
|
|
|
|
break;
|
|
|
|
case HasNoExpectedAttribute:
|
|
|
|
case HasUnexpectedAttribute:
|
|
|
|
Diag << Note.AttributeName;
|
|
|
|
break;
|
|
|
|
case DifferentGetter:
|
|
|
|
Diag << Note.Prop->getGetterName();
|
|
|
|
break;
|
|
|
|
case DifferentSetter:
|
|
|
|
Diag << Note.Prop->getSetterName();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (AtLoc.isValid())
|
2013-05-21 05:20:24 +08:00
|
|
|
S.Diag(AtLoc, diag::note_property_synthesize);
|
2017-07-13 19:06:22 +08:00
|
|
|
|
|
|
|
return Property;
|
2013-05-21 05:20:24 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 07:02:09 +08:00
|
|
|
/// Determine whether any storage attributes were written on the property.
|
2016-01-29 02:49:28 +08:00
|
|
|
static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
|
|
|
|
ObjCPropertyQueryKind QueryKind) {
|
2015-12-11 07:02:09 +08:00
|
|
|
if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
|
|
|
|
|
|
|
|
// If this is a readwrite property in a class extension that refines
|
|
|
|
// a readonly property in the original class definition, check it as
|
|
|
|
// well.
|
|
|
|
|
|
|
|
// If it's a readonly property, we're not interested.
|
|
|
|
if (Prop->isReadOnly()) return false;
|
|
|
|
|
|
|
|
// Is it declared in an extension?
|
|
|
|
auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
|
|
|
|
if (!Category || !Category->IsClassExtension()) return false;
|
|
|
|
|
|
|
|
// Find the corresponding property in the primary class definition.
|
|
|
|
auto OrigClass = Category->getClassInterface();
|
|
|
|
for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
|
|
|
|
if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
|
|
|
|
return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
|
|
|
|
}
|
|
|
|
|
2015-12-18 08:52:31 +08:00
|
|
|
// Look through all of the protocols.
|
|
|
|
for (const auto *Proto : OrigClass->all_referenced_protocols()) {
|
2016-01-29 02:49:28 +08:00
|
|
|
if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
|
|
|
|
Prop->getIdentifier(), QueryKind))
|
2015-12-18 08:52:31 +08:00
|
|
|
return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
|
|
|
|
}
|
|
|
|
|
2015-12-11 07:02:09 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
/// ActOnPropertyImplDecl - This routine performs semantic checks and
|
|
|
|
/// builds the AST node for a property implementation declaration; declared
|
2012-06-15 15:13:21 +08:00
|
|
|
/// as \@synthesize or \@dynamic.
|
2010-03-12 08:46:40 +08:00
|
|
|
///
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnPropertyImplDecl(Scope *S,
|
|
|
|
SourceLocation AtLoc,
|
|
|
|
SourceLocation PropertyLoc,
|
|
|
|
bool Synthesize,
|
|
|
|
IdentifierInfo *PropertyId,
|
2010-11-17 09:03:52 +08:00
|
|
|
IdentifierInfo *PropertyIvar,
|
2016-01-29 02:49:28 +08:00
|
|
|
SourceLocation PropertyIvarLoc,
|
|
|
|
ObjCPropertyQueryKind QueryKind) {
|
2010-04-06 07:45:09 +08:00
|
|
|
ObjCContainerDecl *ClassImpDecl =
|
2011-09-20 00:32:32 +08:00
|
|
|
dyn_cast<ObjCContainerDecl>(CurContext);
|
2010-03-12 08:46:40 +08:00
|
|
|
// Make sure we have a context for the property implementation declaration.
|
|
|
|
if (!ClassImpDecl) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(AtLoc, diag::err_missing_property_context);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2012-02-29 01:50:39 +08:00
|
|
|
if (PropertyIvarLoc.isInvalid())
|
|
|
|
PropertyIvarLoc = PropertyLoc;
|
2012-05-02 06:26:06 +08:00
|
|
|
SourceLocation PropertyDiagLoc = PropertyLoc;
|
|
|
|
if (PropertyDiagLoc.isInvalid())
|
|
|
|
PropertyDiagLoc = ClassImpDecl->getLocStart();
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCPropertyDecl *property = nullptr;
|
|
|
|
ObjCInterfaceDecl *IDecl = nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
// Find the class or category class where this property must have
|
|
|
|
// a declaration.
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCImplementationDecl *IC = nullptr;
|
|
|
|
ObjCCategoryImplDecl *CatImplClass = nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
|
|
|
|
IDecl = IC->getClassInterface();
|
|
|
|
// We always synthesize an interface for an implementation
|
|
|
|
// without an interface decl. So, IDecl is always non-zero.
|
|
|
|
assert(IDecl &&
|
|
|
|
"ActOnPropertyImplDecl - @implementation without @interface");
|
|
|
|
|
|
|
|
// Look for this property declaration in the @implementation's @interface
|
2016-01-29 02:49:28 +08:00
|
|
|
property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
|
2010-03-12 08:46:40 +08:00
|
|
|
if (!property) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2016-01-30 03:16:39 +08:00
|
|
|
if (property->isClassProperty() && Synthesize) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
|
2016-01-30 03:16:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2010-12-18 06:28:16 +08:00
|
|
|
unsigned PIkind = property->getPropertyAttributesAsWritten();
|
2011-06-11 08:45:12 +08:00
|
|
|
if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
|
2010-12-18 06:28:16 +08:00
|
|
|
if (AtLoc.isValid())
|
|
|
|
Diag(AtLoc, diag::warn_implicit_atomic_property);
|
|
|
|
else
|
|
|
|
Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (const ObjCCategoryDecl *CD =
|
|
|
|
dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
|
|
|
|
if (!CD->IsClassExtension()) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-20 02:17:17 +08:00
|
|
|
if (Synthesize&&
|
|
|
|
(PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
|
|
|
|
property->hasAttr<IBOutletAttr>() &&
|
|
|
|
!AtLoc.isValid()) {
|
2013-02-09 07:32:30 +08:00
|
|
|
bool ReadWriteProperty = false;
|
|
|
|
// Search into the class extensions and see if 'readonly property is
|
|
|
|
// redeclared 'readwrite', then no warning is to be issued.
|
2014-03-14 05:57:01 +08:00
|
|
|
for (auto *Ext : IDecl->known_extensions()) {
|
2013-02-09 07:32:30 +08:00
|
|
|
DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
|
|
|
|
if (!R.empty())
|
|
|
|
if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
|
|
|
|
PIkind = ExtProp->getPropertyAttributesAsWritten();
|
|
|
|
if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
|
|
|
|
ReadWriteProperty = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadWriteProperty) {
|
2013-02-09 15:13:16 +08:00
|
|
|
Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
|
2014-01-03 22:23:03 +08:00
|
|
|
<< property;
|
2013-02-09 07:32:30 +08:00
|
|
|
SourceLocation readonlyLoc;
|
|
|
|
if (LocPropertyAttribute(Context, "readonly",
|
|
|
|
property->getLParenLoc(), readonlyLoc)) {
|
|
|
|
SourceLocation endLoc =
|
|
|
|
readonlyLoc.getLocWithOffset(strlen("readonly")-1);
|
|
|
|
SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
|
|
|
|
Diag(property->getLocation(),
|
|
|
|
diag::note_auto_readonly_iboutlet_fixup_suggest) <<
|
|
|
|
FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
|
|
|
|
}
|
2012-05-22 01:02:43 +08:00
|
|
|
}
|
2012-05-20 02:17:17 +08:00
|
|
|
}
|
2013-05-21 05:20:24 +08:00
|
|
|
if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
|
2017-07-13 19:06:22 +08:00
|
|
|
property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl,
|
|
|
|
property);
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
} else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
|
|
|
|
if (Synthesize) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(AtLoc, diag::err_synthesize_category_decl);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
IDecl = CatImplClass->getClassInterface();
|
|
|
|
if (!IDecl) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(AtLoc, diag::err_missing_property_interface);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
ObjCCategoryDecl *Category =
|
|
|
|
IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
|
|
|
|
|
|
|
|
// If category for this implementation not found, it is an error which
|
|
|
|
// has already been reported eralier.
|
|
|
|
if (!Category)
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
// Look for this property declaration in @implementation's category
|
2016-01-29 02:49:28 +08:00
|
|
|
property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
|
2010-03-12 08:46:40 +08:00
|
|
|
if (!property) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyLoc, diag::err_bad_category_property_decl)
|
2010-03-12 08:46:40 +08:00
|
|
|
<< Category->getDeclName();
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(AtLoc, diag::err_bad_property_context);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCIvarDecl *Ivar = nullptr;
|
2012-05-02 06:26:06 +08:00
|
|
|
bool CompleteTypeErr = false;
|
2012-05-16 02:12:51 +08:00
|
|
|
bool compat = true;
|
2010-03-12 08:46:40 +08:00
|
|
|
// Check that we have a valid, previously declared ivar for @synthesize
|
|
|
|
if (Synthesize) {
|
|
|
|
// @synthesize
|
|
|
|
if (!PropertyIvar)
|
|
|
|
PropertyIvar = PropertyId;
|
2012-01-12 02:26:06 +08:00
|
|
|
// Check that this is a previously declared 'ivar' in 'IDecl' interface
|
|
|
|
ObjCInterfaceDecl *ClassDeclared;
|
|
|
|
Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
|
2011-09-14 02:31:23 +08:00
|
|
|
QualType PropType = property->getType();
|
|
|
|
QualType PropertyIvarType = PropType.getNonReferenceType();
|
2012-05-02 06:26:06 +08:00
|
|
|
|
|
|
|
if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
|
2012-05-05 00:32:21 +08:00
|
|
|
diag::err_incomplete_synthesized_property,
|
|
|
|
property->getDeclName())) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
CompleteTypeErr = true;
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2012-01-12 02:26:06 +08:00
|
|
|
(property->getPropertyAttributesAsWritten() &
|
2012-01-12 03:48:08 +08:00
|
|
|
ObjCPropertyDecl::OBJC_PR_readonly) &&
|
|
|
|
PropertyIvarType->isObjCRetainableType()) {
|
2012-01-12 02:26:06 +08:00
|
|
|
setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind kind
|
|
|
|
= property->getPropertyAttributes();
|
2011-09-14 02:31:23 +08:00
|
|
|
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
bool isARCWeak = false;
|
|
|
|
if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
|
|
|
|
// Add GC __weak to the ivar type if the property is weak.
|
|
|
|
if (getLangOpts().getGC() != LangOptions::NonGC) {
|
|
|
|
assert(!getLangOpts().ObjCAutoRefCount);
|
|
|
|
if (PropertyIvarType.isObjCGCStrong()) {
|
|
|
|
Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
} else {
|
|
|
|
PropertyIvarType =
|
|
|
|
Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, check whether ARC __weak is enabled and works with
|
|
|
|
// the property type.
|
2011-09-14 02:31:23 +08:00
|
|
|
} else {
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
if (!getLangOpts().ObjCWeak) {
|
2015-10-27 12:54:50 +08:00
|
|
|
// Only complain here when synthesizing an ivar.
|
|
|
|
if (!Ivar) {
|
|
|
|
Diag(PropertyDiagLoc,
|
|
|
|
getLangOpts().ObjCWeakRuntime
|
|
|
|
? diag::err_synthesizing_arc_weak_property_disabled
|
|
|
|
: diag::err_synthesizing_arc_weak_property_no_runtime);
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
}
|
2015-10-27 12:54:50 +08:00
|
|
|
CompleteTypeErr = true; // suppress later diagnostics about the ivar
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
} else {
|
|
|
|
isARCWeak = true;
|
|
|
|
if (const ObjCObjectPointerType *ObjT =
|
|
|
|
PropertyIvarType->getAs<ObjCObjectPointerType>()) {
|
|
|
|
const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
|
|
|
|
if (ObjI && ObjI->isArcWeakrefUnavailable()) {
|
|
|
|
Diag(property->getLocation(),
|
|
|
|
diag::err_arc_weak_unavailable_property)
|
|
|
|
<< PropertyIvarType;
|
|
|
|
Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
|
|
|
|
<< ClassImpDecl->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-08 00:24:21 +08:00
|
|
|
}
|
|
|
|
}
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
|
2012-06-21 01:18:31 +08:00
|
|
|
if (AtLoc.isInvalid()) {
|
|
|
|
// Check when default synthesizing a property that there is
|
|
|
|
// an ivar matching property name and issue warning; since this
|
|
|
|
// is the most common case of not using an ivar used for backing
|
|
|
|
// property in non-default synthesis case.
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCInterfaceDecl *ClassDeclared=nullptr;
|
2012-06-21 01:18:31 +08:00
|
|
|
ObjCIvarDecl *originalIvar =
|
|
|
|
IDecl->lookupInstanceVariable(property->getIdentifier(),
|
|
|
|
ClassDeclared);
|
|
|
|
if (originalIvar) {
|
|
|
|
Diag(PropertyDiagLoc,
|
|
|
|
diag::warn_autosynthesis_property_ivar_match)
|
2014-05-26 14:22:03 +08:00
|
|
|
<< PropertyId << (Ivar == nullptr) << PropertyIvar
|
2012-06-30 02:43:30 +08:00
|
|
|
<< originalIvar->getIdentifier();
|
2012-06-21 01:18:31 +08:00
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
Diag(originalIvar->getLocation(), diag::note_ivar_decl);
|
2012-06-20 06:51:22 +08:00
|
|
|
}
|
2012-06-21 01:18:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Ivar) {
|
2011-09-14 02:31:23 +08:00
|
|
|
// In ARC, give the ivar a lifetime qualifier based on the
|
2011-06-16 07:02:42 +08:00
|
|
|
// property attributes.
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
|
2011-09-14 02:31:23 +08:00
|
|
|
!PropertyIvarType.getObjCLifetime() &&
|
|
|
|
PropertyIvarType->isObjCRetainableType()) {
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
// It's an error if we have to do this and the user didn't
|
|
|
|
// explicitly write an ownership attribute on the property.
|
2016-01-29 02:49:28 +08:00
|
|
|
if (!hasWrittenStorageAttribute(property, QueryKind) &&
|
2011-09-14 02:31:23 +08:00
|
|
|
!(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc,
|
2011-07-27 05:48:26 +08:00
|
|
|
diag::err_arc_objc_property_default_assign_on_object);
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
2011-09-14 02:31:23 +08:00
|
|
|
} else {
|
|
|
|
Qualifiers::ObjCLifetime lifetime =
|
|
|
|
getImpliedARCOwnership(kind, PropertyIvarType);
|
|
|
|
assert(lifetime && "no lifetime for property?");
|
2011-12-10 03:55:11 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
Qualifiers qs;
|
2011-09-14 02:31:23 +08:00
|
|
|
qs.addObjCLifetime(lifetime);
|
2011-06-16 07:02:42 +08:00
|
|
|
PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 16:55:46 +08:00
|
|
|
Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
|
2012-02-29 01:50:39 +08:00
|
|
|
PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
|
2014-05-26 14:22:03 +08:00
|
|
|
PropertyIvarType, /*Dinfo=*/nullptr,
|
2010-12-16 07:29:04 +08:00
|
|
|
ObjCIvarDecl::Private,
|
2014-05-26 14:22:03 +08:00
|
|
|
(Expr *)nullptr, true);
|
2013-07-06 01:18:11 +08:00
|
|
|
if (RequireNonAbstractType(PropertyIvarLoc,
|
|
|
|
PropertyIvarType,
|
|
|
|
diag::err_abstract_type_in_decl,
|
|
|
|
AbstractSynthesizedIvarType)) {
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
2016-12-15 10:28:18 +08:00
|
|
|
// An abstract type is as bad as an incomplete type.
|
|
|
|
CompleteTypeErr = true;
|
|
|
|
}
|
2017-10-24 06:01:41 +08:00
|
|
|
if (!CompleteTypeErr) {
|
|
|
|
const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>();
|
|
|
|
if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()) {
|
|
|
|
Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar)
|
|
|
|
<< PropertyIvarType;
|
|
|
|
CompleteTypeErr = true; // suppress later diagnostics about the ivar
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 10:28:18 +08:00
|
|
|
if (CompleteTypeErr)
|
2012-05-02 06:26:06 +08:00
|
|
|
Ivar->setInvalidDecl();
|
2010-04-03 03:44:54 +08:00
|
|
|
ClassImpDecl->addDecl(Ivar);
|
2012-03-13 11:12:56 +08:00
|
|
|
IDecl->makeDeclVisibleInContext(Ivar);
|
2010-03-12 08:46:40 +08:00
|
|
|
|
2012-06-20 14:18:46 +08:00
|
|
|
if (getLangOpts().ObjCRuntime.isFragile())
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
|
2012-05-02 06:26:06 +08:00
|
|
|
<< PropertyId;
|
2010-03-12 08:46:40 +08:00
|
|
|
// Note! I deliberately want it to fall thru so, we have a
|
|
|
|
// a property implementation and to avoid future warnings.
|
2012-06-20 14:18:46 +08:00
|
|
|
} else if (getLangOpts().ObjCRuntime.isNonFragile() &&
|
2011-12-15 08:29:59 +08:00
|
|
|
!declaresSameEntity(ClassDeclared, IDecl)) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
|
2010-03-12 08:46:40 +08:00
|
|
|
<< property->getDeclName() << Ivar->getDeclName()
|
|
|
|
<< ClassDeclared->getDeclName();
|
|
|
|
Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
|
2010-08-18 06:39:59 +08:00
|
|
|
<< Ivar << Ivar->getName();
|
2010-03-12 08:46:40 +08:00
|
|
|
// Note! I deliberately want it to fall thru so more errors are caught.
|
|
|
|
}
|
2012-09-27 02:55:16 +08:00
|
|
|
property->setPropertyIvarDecl(Ivar);
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
QualType IvarType = Context.getCanonicalType(Ivar->getType());
|
|
|
|
|
|
|
|
// Check that type of property and its ivar are type compatible.
|
2012-05-16 02:12:51 +08:00
|
|
|
if (!Context.hasSameType(PropertyIvarType, IvarType)) {
|
2011-03-29 07:47:18 +08:00
|
|
|
if (isa<ObjCObjectPointerType>(PropertyIvarType)
|
2011-06-16 07:02:42 +08:00
|
|
|
&& isa<ObjCObjectPointerType>(IvarType))
|
2012-09-15 02:27:01 +08:00
|
|
|
compat =
|
2010-05-19 07:04:17 +08:00
|
|
|
Context.canAssignObjCInterfaces(
|
2011-03-29 07:47:18 +08:00
|
|
|
PropertyIvarType->getAs<ObjCObjectPointerType>(),
|
2010-05-19 07:04:17 +08:00
|
|
|
IvarType->getAs<ObjCObjectPointerType>());
|
2011-01-28 10:26:04 +08:00
|
|
|
else {
|
2012-02-29 01:50:39 +08:00
|
|
|
compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
|
|
|
|
IvarType)
|
2010-11-15 17:13:47 +08:00
|
|
|
== Compatible);
|
2011-01-28 10:26:04 +08:00
|
|
|
}
|
2010-05-19 07:04:17 +08:00
|
|
|
if (!compat) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_property_ivar_type)
|
2010-03-24 03:02:22 +08:00
|
|
|
<< property->getDeclName() << PropType
|
|
|
|
<< Ivar->getDeclName() << IvarType;
|
|
|
|
Diag(Ivar->getLocation(), diag::note_ivar_decl);
|
2010-03-12 08:46:40 +08:00
|
|
|
// Note! I deliberately want it to fall thru so, we have a
|
|
|
|
// a property implementation and to avoid future warnings.
|
|
|
|
}
|
2012-05-16 02:12:51 +08:00
|
|
|
else {
|
|
|
|
// FIXME! Rules for properties are somewhat different that those
|
|
|
|
// for assignments. Use a new routine to consolidate all cases;
|
|
|
|
// specifically for property redeclarations as well as for ivars.
|
|
|
|
QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
|
|
|
|
QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
|
|
|
|
if (lhsType != rhsType &&
|
|
|
|
lhsType->isArithmeticType()) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_property_ivar_type)
|
2012-05-16 02:12:51 +08:00
|
|
|
<< property->getDeclName() << PropType
|
|
|
|
<< Ivar->getDeclName() << IvarType;
|
|
|
|
Diag(Ivar->getLocation(), diag::note_ivar_decl);
|
|
|
|
// Fall thru - see previous comment
|
|
|
|
}
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
// __weak is explicit. So it works on Canonical type.
|
2011-06-16 07:02:42 +08:00
|
|
|
if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
|
2012-03-11 15:00:24 +08:00
|
|
|
getLangOpts().getGC() != LangOptions::NonGC)) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_weak_property)
|
2010-03-12 08:46:40 +08:00
|
|
|
<< property->getDeclName() << Ivar->getDeclName();
|
2011-09-08 00:24:21 +08:00
|
|
|
Diag(Ivar->getLocation(), diag::note_ivar_decl);
|
2010-03-12 08:46:40 +08:00
|
|
|
// Fall thru - see previous comment
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
// Fall thru - see previous comment
|
2010-03-12 08:46:40 +08:00
|
|
|
if ((property->getType()->isObjCObjectPointerType() ||
|
|
|
|
PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
|
2012-03-11 15:00:24 +08:00
|
|
|
getLangOpts().getGC() != LangOptions::NonGC) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_strong_property)
|
2010-03-12 08:46:40 +08:00
|
|
|
<< property->getDeclName() << Ivar->getDeclName();
|
|
|
|
// Fall thru - see previous comment
|
|
|
|
}
|
|
|
|
}
|
Define weak and __weak to mean ARC-style weak references, even in MRC.
Previously, __weak was silently accepted and ignored in MRC mode.
That makes this a potentially source-breaking change that we have to
roll out cautiously. Accordingly, for the time being, actual support
for __weak references in MRC is experimental, and the compiler will
reject attempts to actually form such references. The intent is to
eventually enable the feature by default in all non-GC modes.
(It is, of course, incompatible with ObjC GC's interpretation of
__weak.)
If you like, you can enable this feature with
-Xclang -fobjc-weak
but like any -Xclang option, this option may be removed at any point,
e.g. if/when it is eventually enabled by default.
This patch also enables the use of the ARC __unsafe_unretained qualifier
in MRC. Unlike __weak, this is being enabled immediately. Since
variables are essentially __unsafe_unretained by default in MRC,
the only practical uses are (1) communication and (2) changing the
default behavior of by-value block capture.
As an implementation matter, this means that the ObjC ownership
qualifiers may appear in any ObjC language mode, and so this patch
removes a number of checks for getLangOpts().ObjCAutoRefCount
that were guarding the processing of these qualifiers. I don't
expect this to be a significant drain on performance; it may even
be faster to just check for these qualifiers directly on a type
(since it's probably in a register anyway) than to do N dependent
loads to grab the LangOptions.
rdar://9674298
llvm-svn: 251041
2015-10-23 02:38:17 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
|
|
|
|
Ivar->getType().getObjCLifetime())
|
2011-06-16 07:02:42 +08:00
|
|
|
checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
|
2010-03-12 08:46:40 +08:00
|
|
|
} else if (PropertyIvar)
|
|
|
|
// @dynamic
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
assert (property && "ActOnPropertyImplDecl - property declaration missing");
|
|
|
|
ObjCPropertyImplDecl *PIDecl =
|
|
|
|
ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
|
|
|
|
property,
|
|
|
|
(Synthesize ?
|
|
|
|
ObjCPropertyImplDecl::Synthesize
|
|
|
|
: ObjCPropertyImplDecl::Dynamic),
|
2010-11-17 09:03:52 +08:00
|
|
|
Ivar, PropertyIvarLoc);
|
2012-05-02 06:26:06 +08:00
|
|
|
|
2012-05-16 02:12:51 +08:00
|
|
|
if (CompleteTypeErr || !compat)
|
2012-05-02 06:26:06 +08:00
|
|
|
PIDecl->setInvalidDecl();
|
|
|
|
|
2010-05-06 05:52:17 +08:00
|
|
|
if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
|
|
|
|
getterMethod->createImplicitParams(Context, IDecl);
|
2012-05-02 06:26:06 +08:00
|
|
|
if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
|
2010-10-16 06:42:59 +08:00
|
|
|
Ivar->getType()->isRecordType()) {
|
2010-05-06 05:52:17 +08:00
|
|
|
// For Objective-C++, need to synthesize the AST for the IVAR object to be
|
|
|
|
// returned by the getter as it must conform to C++'s copy-return rules.
|
|
|
|
// FIXME. Eventually we want to do this for Objective-C as well.
|
2012-10-19 04:14:08 +08:00
|
|
|
SynthesizedFunctionScope Scope(*this, getterMethod);
|
2010-05-06 05:52:17 +08:00
|
|
|
ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
|
|
|
|
DeclRefExpr *SelfExpr =
|
2012-03-10 17:33:50 +08:00
|
|
|
new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
|
2014-01-15 01:29:00 +08:00
|
|
|
VK_LValue, PropertyDiagLoc);
|
2012-10-19 04:14:08 +08:00
|
|
|
MarkDeclRefReferenced(SelfExpr);
|
2014-01-15 01:29:00 +08:00
|
|
|
Expr *LoadSelfExpr =
|
|
|
|
ImplicitCastExpr::Create(Context, SelfDecl->getType(),
|
2014-05-26 14:22:03 +08:00
|
|
|
CK_LValueToRValue, SelfExpr, nullptr,
|
|
|
|
VK_RValue);
|
2010-05-06 05:52:17 +08:00
|
|
|
Expr *IvarRefExpr =
|
Substitute type arguments into uses of Objective-C interface members.
When messaging a method that was defined in an Objective-C class (or
category or extension thereof) that has type parameters, substitute
the type arguments for those type parameters. Similarly, substitute
into property accesses, instance variables, and other references.
This includes general infrastructure for substituting the type
arguments associated with an ObjCObject(Pointer)Type into a type
referenced within a particular context, handling all of the
substitutions required to deal with (e.g.) inheritance involving
parameterized classes. In cases where no type arguments are available
(e.g., because we're messaging via some unspecialized type, id, etc.),
we substitute in the type bounds for the type parameters instead.
Example:
@interface NSSet<T : id<NSCopying>> : NSObject <NSCopying>
- (T)firstObject;
@end
void f(NSSet<NSString *> *stringSet, NSSet *anySet) {
[stringSet firstObject]; // produces NSString*
[anySet firstObject]; // produces id<NSCopying> (the bound)
}
When substituting for the type parameters given an unspecialized
context (i.e., no specific type arguments were given), substituting
the type bounds unconditionally produces type signatures that are too
strong compared to the pre-generics signatures. Instead, use the
following rule:
- In covariant positions, such as method return types, replace type
parameters with “id” or “Class” (the latter only when the type
parameter bound is “Class” or qualified class, e.g,
“Class<NSCopying>”)
- In other positions (e.g., parameter types), replace type
parameters with their type bounds.
- When a specialized Objective-C object or object pointer type
contains a type parameter in its type arguments (e.g.,
NSArray<T>*, but not NSArray<NSString *> *), replace the entire
object/object pointer type with its unspecialized version (e.g.,
NSArray *).
llvm-svn: 241543
2015-07-07 11:57:53 +08:00
|
|
|
new (Context) ObjCIvarRefExpr(Ivar,
|
|
|
|
Ivar->getUsageType(SelfDecl->getType()),
|
|
|
|
PropertyDiagLoc,
|
2013-04-03 02:57:54 +08:00
|
|
|
Ivar->getLocation(),
|
2014-01-15 01:29:00 +08:00
|
|
|
LoadSelfExpr, true, true);
|
2014-01-26 00:55:45 +08:00
|
|
|
ExprResult Res = PerformCopyInitialization(
|
|
|
|
InitializedEntity::InitializeResult(PropertyDiagLoc,
|
|
|
|
getterMethod->getReturnType(),
|
|
|
|
/*NRVO=*/false),
|
2014-05-29 22:05:12 +08:00
|
|
|
PropertyDiagLoc, IvarRefExpr);
|
2010-05-06 05:52:17 +08:00
|
|
|
if (!Res.isInvalid()) {
|
2014-05-29 18:55:11 +08:00
|
|
|
Expr *ResExpr = Res.getAs<Expr>();
|
2010-05-06 05:52:17 +08:00
|
|
|
if (ResExpr)
|
2010-12-06 16:20:24 +08:00
|
|
|
ResExpr = MaybeCreateExprWithCleanups(ResExpr);
|
2010-05-06 05:52:17 +08:00
|
|
|
PIDecl->setGetterCXXConstructor(ResExpr);
|
|
|
|
}
|
|
|
|
}
|
2011-06-25 08:17:46 +08:00
|
|
|
if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
|
|
|
|
!getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
|
|
|
|
Diag(getterMethod->getLocation(),
|
|
|
|
diag::warn_property_getter_owning_mismatch);
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
2013-05-17 03:08:44 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount && Synthesize)
|
|
|
|
switch (getterMethod->getMethodFamily()) {
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_autorelease:
|
|
|
|
Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
|
|
|
|
<< 1 << getterMethod->getSelector();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-05-06 05:52:17 +08:00
|
|
|
}
|
|
|
|
if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
|
|
|
|
setterMethod->createImplicitParams(Context, IDecl);
|
2012-05-02 06:26:06 +08:00
|
|
|
if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
|
|
|
|
Ivar->getType()->isRecordType()) {
|
2010-05-06 05:52:17 +08:00
|
|
|
// FIXME. Eventually we want to do this for Objective-C as well.
|
2012-10-19 04:14:08 +08:00
|
|
|
SynthesizedFunctionScope Scope(*this, setterMethod);
|
2010-05-06 05:52:17 +08:00
|
|
|
ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
|
|
|
|
DeclRefExpr *SelfExpr =
|
2012-03-10 17:33:50 +08:00
|
|
|
new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
|
2014-01-15 01:29:00 +08:00
|
|
|
VK_LValue, PropertyDiagLoc);
|
2012-10-19 04:14:08 +08:00
|
|
|
MarkDeclRefReferenced(SelfExpr);
|
2014-01-15 01:29:00 +08:00
|
|
|
Expr *LoadSelfExpr =
|
|
|
|
ImplicitCastExpr::Create(Context, SelfDecl->getType(),
|
2014-05-26 14:22:03 +08:00
|
|
|
CK_LValueToRValue, SelfExpr, nullptr,
|
|
|
|
VK_RValue);
|
2010-05-06 05:52:17 +08:00
|
|
|
Expr *lhs =
|
Substitute type arguments into uses of Objective-C interface members.
When messaging a method that was defined in an Objective-C class (or
category or extension thereof) that has type parameters, substitute
the type arguments for those type parameters. Similarly, substitute
into property accesses, instance variables, and other references.
This includes general infrastructure for substituting the type
arguments associated with an ObjCObject(Pointer)Type into a type
referenced within a particular context, handling all of the
substitutions required to deal with (e.g.) inheritance involving
parameterized classes. In cases where no type arguments are available
(e.g., because we're messaging via some unspecialized type, id, etc.),
we substitute in the type bounds for the type parameters instead.
Example:
@interface NSSet<T : id<NSCopying>> : NSObject <NSCopying>
- (T)firstObject;
@end
void f(NSSet<NSString *> *stringSet, NSSet *anySet) {
[stringSet firstObject]; // produces NSString*
[anySet firstObject]; // produces id<NSCopying> (the bound)
}
When substituting for the type parameters given an unspecialized
context (i.e., no specific type arguments were given), substituting
the type bounds unconditionally produces type signatures that are too
strong compared to the pre-generics signatures. Instead, use the
following rule:
- In covariant positions, such as method return types, replace type
parameters with “id” or “Class” (the latter only when the type
parameter bound is “Class” or qualified class, e.g,
“Class<NSCopying>”)
- In other positions (e.g., parameter types), replace type
parameters with their type bounds.
- When a specialized Objective-C object or object pointer type
contains a type parameter in its type arguments (e.g.,
NSArray<T>*, but not NSArray<NSString *> *), replace the entire
object/object pointer type with its unspecialized version (e.g.,
NSArray *).
llvm-svn: 241543
2015-07-07 11:57:53 +08:00
|
|
|
new (Context) ObjCIvarRefExpr(Ivar,
|
|
|
|
Ivar->getUsageType(SelfDecl->getType()),
|
|
|
|
PropertyDiagLoc,
|
2013-04-03 02:57:54 +08:00
|
|
|
Ivar->getLocation(),
|
2014-01-15 01:29:00 +08:00
|
|
|
LoadSelfExpr, true, true);
|
2010-05-06 05:52:17 +08:00
|
|
|
ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
|
|
|
|
ParmVarDecl *Param = (*P);
|
2011-10-26 01:37:35 +08:00
|
|
|
QualType T = Param->getType().getNonReferenceType();
|
2012-10-19 04:14:08 +08:00
|
|
|
DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
|
|
|
|
VK_LValue, PropertyDiagLoc);
|
|
|
|
MarkDeclRefReferenced(rhs);
|
|
|
|
ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
|
2010-08-25 19:45:40 +08:00
|
|
|
BO_Assign, lhs, rhs);
|
2011-10-07 02:38:18 +08:00
|
|
|
if (property->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_atomic) {
|
2014-05-29 18:55:11 +08:00
|
|
|
Expr *callExpr = Res.getAs<Expr>();
|
2011-10-07 02:38:18 +08:00
|
|
|
if (const CXXOperatorCallExpr *CXXCE =
|
2011-10-08 05:08:14 +08:00
|
|
|
dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
|
|
|
|
if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
|
2011-10-07 02:38:18 +08:00
|
|
|
if (!FuncDecl->isTrivial())
|
2012-01-10 08:37:01 +08:00
|
|
|
if (property->getType()->isReferenceType()) {
|
2012-10-19 04:14:08 +08:00
|
|
|
Diag(PropertyDiagLoc,
|
2012-01-10 08:37:01 +08:00
|
|
|
diag::err_atomic_property_nontrivial_assign_op)
|
2011-10-07 02:38:18 +08:00
|
|
|
<< property->getType();
|
2012-01-10 08:37:01 +08:00
|
|
|
Diag(FuncDecl->getLocStart(),
|
|
|
|
diag::note_callee_decl) << FuncDecl;
|
|
|
|
}
|
2011-10-07 02:38:18 +08:00
|
|
|
}
|
2014-05-29 18:55:11 +08:00
|
|
|
PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
|
2010-05-06 05:52:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (IC) {
|
|
|
|
if (Synthesize)
|
|
|
|
if (ObjCPropertyImplDecl *PPIDecl =
|
|
|
|
IC->FindPropertyImplIvarDecl(PropertyIvar)) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyLoc, diag::err_duplicate_ivar_use)
|
2010-03-12 08:46:40 +08:00
|
|
|
<< PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
|
|
|
|
<< PropertyIvar;
|
|
|
|
Diag(PPIDecl->getLocation(), diag::note_previous_use);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCPropertyImplDecl *PPIDecl
|
2016-01-29 02:49:28 +08:00
|
|
|
= IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
IC->addPropertyImplementation(PIDecl);
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCDefaultSynthProperties &&
|
2012-06-20 14:18:46 +08:00
|
|
|
getLangOpts().ObjCRuntime.isNonFragile() &&
|
2012-01-06 06:47:47 +08:00
|
|
|
!IDecl->isObjCRequiresPropertyDefs()) {
|
2010-07-17 08:59:30 +08:00
|
|
|
// Diagnose if an ivar was lazily synthesdized due to a previous
|
|
|
|
// use and if 1) property is @dynamic or 2) property is synthesized
|
2010-08-25 02:48:05 +08:00
|
|
|
// but it requires an ivar of different name.
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCInterfaceDecl *ClassDeclared=nullptr;
|
|
|
|
ObjCIvarDecl *Ivar = nullptr;
|
2010-07-17 08:59:30 +08:00
|
|
|
if (!Synthesize)
|
|
|
|
Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
|
|
|
|
else {
|
|
|
|
if (PropertyIvar && PropertyIvar != PropertyId)
|
|
|
|
Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
|
|
|
|
}
|
2010-08-25 02:48:05 +08:00
|
|
|
// Issue diagnostics only if Ivar belongs to current class.
|
|
|
|
if (Ivar && Ivar->getSynthesize() &&
|
2011-12-15 08:29:59 +08:00
|
|
|
declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
|
2010-07-17 08:59:30 +08:00
|
|
|
Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
|
|
|
|
<< PropertyId;
|
|
|
|
Ivar->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
2010-03-12 08:46:40 +08:00
|
|
|
} else {
|
|
|
|
if (Synthesize)
|
|
|
|
if (ObjCPropertyImplDecl *PPIDecl =
|
|
|
|
CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
|
2010-03-12 08:46:40 +08:00
|
|
|
<< PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
|
|
|
|
<< PropertyIvar;
|
|
|
|
Diag(PPIDecl->getLocation(), diag::note_previous_use);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCPropertyImplDecl *PPIDecl =
|
2016-01-29 02:49:28 +08:00
|
|
|
CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
CatImplClass->addPropertyImplementation(PIDecl);
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
return PIDecl;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
/// DiagnosePropertyMismatch - Compares two properties for their
|
|
|
|
/// attributes and types and warns on a variety of inconsistencies.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
|
|
|
|
ObjCPropertyDecl *SuperProperty,
|
2013-10-05 02:06:08 +08:00
|
|
|
const IdentifierInfo *inheritedName,
|
|
|
|
bool OverridingProtocolProperty) {
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCPropertyDecl::PropertyAttributeKind CAttr =
|
2013-10-05 02:06:08 +08:00
|
|
|
Property->getPropertyAttributes();
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCPropertyDecl::PropertyAttributeKind SAttr =
|
2013-10-05 02:06:08 +08:00
|
|
|
SuperProperty->getPropertyAttributes();
|
|
|
|
|
|
|
|
// We allow readonly properties without an explicit ownership
|
|
|
|
// (assign/unsafe_unretained/weak/retain/strong/copy) in super class
|
|
|
|
// to be overridden by a property with any explicit ownership in the subclass.
|
|
|
|
if (!OverridingProtocolProperty &&
|
|
|
|
!getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
|
|
|
|
;
|
|
|
|
else {
|
|
|
|
if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
|
|
|
|
&& (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
|
|
|
|
Diag(Property->getLocation(), diag::warn_readonly_property)
|
|
|
|
<< Property->getDeclName() << inheritedName;
|
|
|
|
if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
|
|
|
|
!= (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
2013-10-05 02:06:08 +08:00
|
|
|
<< Property->getDeclName() << "copy" << inheritedName;
|
|
|
|
else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
|
|
|
|
unsigned CAttrRetain =
|
|
|
|
(CAttr &
|
|
|
|
(ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
|
|
|
|
unsigned SAttrRetain =
|
|
|
|
(SAttr &
|
|
|
|
(ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
|
|
|
|
bool CStrong = (CAttrRetain != 0);
|
|
|
|
bool SStrong = (SAttrRetain != 0);
|
|
|
|
if (CStrong != SStrong)
|
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< Property->getDeclName() << "retain (or strong)" << inheritedName;
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2015-12-10 06:57:32 +08:00
|
|
|
// Check for nonatomic; note that nonatomic is effectively
|
|
|
|
// meaningless for readonly properties, so don't diagnose if the
|
|
|
|
// atomic property is 'readonly'.
|
2015-12-11 07:02:09 +08:00
|
|
|
checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
|
2017-10-07 03:24:26 +08:00
|
|
|
// Readonly properties from protocols can be implemented as "readwrite"
|
|
|
|
// with a custom setter name.
|
|
|
|
if (Property->getSetterName() != SuperProperty->getSetterName() &&
|
|
|
|
!(SuperProperty->isReadOnly() &&
|
|
|
|
isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) {
|
2010-03-12 08:38:38 +08:00
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< Property->getDeclName() << "setter" << inheritedName;
|
2013-02-10 08:16:04 +08:00
|
|
|
Diag(SuperProperty->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
if (Property->getGetterName() != SuperProperty->getGetterName()) {
|
2010-03-12 08:38:38 +08:00
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< Property->getDeclName() << "getter" << inheritedName;
|
2013-02-10 08:16:04 +08:00
|
|
|
Diag(SuperProperty->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
|
|
|
QualType LHSType =
|
|
|
|
Context.getCanonicalType(SuperProperty->getType());
|
|
|
|
QualType RHSType =
|
|
|
|
Context.getCanonicalType(Property->getType());
|
|
|
|
|
2011-07-13 06:05:16 +08:00
|
|
|
if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
|
2011-07-14 01:55:01 +08:00
|
|
|
// Do cases not handled in above.
|
|
|
|
// FIXME. For future support of covariant property types, revisit this.
|
|
|
|
bool IncompatibleObjC = false;
|
|
|
|
QualType ConvertedType;
|
|
|
|
if (!isObjCPointerConversion(RHSType, LHSType,
|
|
|
|
ConvertedType, IncompatibleObjC) ||
|
2011-10-12 08:00:57 +08:00
|
|
|
IncompatibleObjC) {
|
2011-07-14 01:55:01 +08:00
|
|
|
Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
|
|
|
|
<< Property->getType() << SuperProperty->getType() << inheritedName;
|
2011-10-12 08:00:57 +08:00
|
|
|
Diag(SuperProperty->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
|
|
|
|
ObjCMethodDecl *GetterMethod,
|
|
|
|
SourceLocation Loc) {
|
2012-05-16 06:37:04 +08:00
|
|
|
if (!GetterMethod)
|
|
|
|
return false;
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
|
2016-05-26 08:37:30 +08:00
|
|
|
QualType PropertyRValueType =
|
|
|
|
property->getType().getNonReferenceType().getAtomicUnqualifiedType();
|
|
|
|
bool compat = Context.hasSameType(PropertyRValueType, GetterType);
|
2012-05-16 06:37:04 +08:00
|
|
|
if (!compat) {
|
2015-12-09 06:45:17 +08:00
|
|
|
const ObjCObjectPointerType *propertyObjCPtr = nullptr;
|
|
|
|
const ObjCObjectPointerType *getterObjCPtr = nullptr;
|
2016-05-26 08:37:30 +08:00
|
|
|
if ((propertyObjCPtr =
|
|
|
|
PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
|
2015-12-09 06:45:17 +08:00
|
|
|
(getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
|
|
|
|
compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
|
2016-05-26 08:37:30 +08:00
|
|
|
else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
|
2012-05-16 06:37:04 +08:00
|
|
|
!= Compatible) {
|
2016-12-03 06:38:31 +08:00
|
|
|
Diag(Loc, diag::err_property_accessor_type)
|
2016-05-26 08:37:30 +08:00
|
|
|
<< property->getDeclName() << PropertyRValueType
|
2012-05-16 06:37:04 +08:00
|
|
|
<< GetterMethod->getSelector() << GetterType;
|
|
|
|
Diag(GetterMethod->getLocation(), diag::note_declared_at);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
compat = true;
|
2016-05-26 08:37:30 +08:00
|
|
|
QualType lhsType = Context.getCanonicalType(PropertyRValueType);
|
2012-05-16 06:37:04 +08:00
|
|
|
QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
|
|
|
|
if (lhsType != rhsType && lhsType->isArithmeticType())
|
|
|
|
compat = false;
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-16 06:37:04 +08:00
|
|
|
|
|
|
|
if (!compat) {
|
|
|
|
Diag(Loc, diag::warn_accessor_property_type_mismatch)
|
|
|
|
<< property->getDeclName()
|
|
|
|
<< GetterMethod->getSelector();
|
|
|
|
Diag(GetterMethod->getLocation(), diag::note_declared_at);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CollectImmediateProperties - This routine collects all properties in
|
2013-01-22 02:35:55 +08:00
|
|
|
/// the class and its conforming protocols; but not those in its super class.
|
2016-04-13 07:01:55 +08:00
|
|
|
static void
|
|
|
|
CollectImmediateProperties(ObjCContainerDecl *CDecl,
|
|
|
|
ObjCContainerDecl::PropertyMap &PropMap,
|
|
|
|
ObjCContainerDecl::PropertyMap &SuperPropMap,
|
|
|
|
bool CollectClassPropsOnly = false,
|
|
|
|
bool IncludeProtocols = true) {
|
2010-03-12 08:38:38 +08:00
|
|
|
if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
|
2016-04-13 07:01:55 +08:00
|
|
|
for (auto *Prop : IDecl->properties()) {
|
|
|
|
if (CollectClassPropsOnly && !Prop->isClassProperty())
|
|
|
|
continue;
|
2016-01-29 07:36:05 +08:00
|
|
|
PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
|
|
|
|
Prop;
|
2016-04-13 07:01:55 +08:00
|
|
|
}
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
|
|
|
|
// Collect the properties from visible extensions.
|
|
|
|
for (auto *Ext : IDecl->visible_extensions())
|
2016-04-13 07:01:55 +08:00
|
|
|
CollectImmediateProperties(Ext, PropMap, SuperPropMap,
|
|
|
|
CollectClassPropsOnly, IncludeProtocols);
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
|
2014-02-22 08:02:03 +08:00
|
|
|
if (IncludeProtocols) {
|
|
|
|
// Scan through class's protocols.
|
2014-03-14 04:55:22 +08:00
|
|
|
for (auto *PI : IDecl->all_referenced_protocols())
|
2016-04-13 07:01:55 +08:00
|
|
|
CollectImmediateProperties(PI, PropMap, SuperPropMap,
|
|
|
|
CollectClassPropsOnly);
|
2014-02-22 08:02:03 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
|
2016-04-13 07:01:55 +08:00
|
|
|
for (auto *Prop : CATDecl->properties()) {
|
|
|
|
if (CollectClassPropsOnly && !Prop->isClassProperty())
|
|
|
|
continue;
|
2016-01-29 07:36:05 +08:00
|
|
|
PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
|
|
|
|
Prop;
|
2016-04-13 07:01:55 +08:00
|
|
|
}
|
2014-02-22 08:02:03 +08:00
|
|
|
if (IncludeProtocols) {
|
|
|
|
// Scan through class's protocols.
|
2014-03-14 20:55:57 +08:00
|
|
|
for (auto *PI : CATDecl->protocols())
|
2016-04-13 07:01:55 +08:00
|
|
|
CollectImmediateProperties(PI, PropMap, SuperPropMap,
|
|
|
|
CollectClassPropsOnly);
|
2014-02-22 08:02:03 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
|
2016-01-29 07:36:05 +08:00
|
|
|
for (auto *Prop : PDecl->properties()) {
|
2016-04-13 07:01:55 +08:00
|
|
|
if (CollectClassPropsOnly && !Prop->isClassProperty())
|
|
|
|
continue;
|
2016-01-29 07:36:05 +08:00
|
|
|
ObjCPropertyDecl *PropertyFromSuper =
|
|
|
|
SuperPropMap[std::make_pair(Prop->getIdentifier(),
|
|
|
|
Prop->isClassProperty())];
|
2010-06-30 02:12:32 +08:00
|
|
|
// Exclude property for protocols which conform to class's super-class,
|
|
|
|
// as super-class has to implement the property.
|
2011-09-27 08:23:52 +08:00
|
|
|
if (!PropertyFromSuper ||
|
|
|
|
PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
|
2016-01-29 07:36:05 +08:00
|
|
|
ObjCPropertyDecl *&PropEntry =
|
|
|
|
PropMap[std::make_pair(Prop->getIdentifier(),
|
|
|
|
Prop->isClassProperty())];
|
2010-06-30 02:12:32 +08:00
|
|
|
if (!PropEntry)
|
|
|
|
PropEntry = Prop;
|
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
2016-04-13 07:01:55 +08:00
|
|
|
// Scan through protocol's protocols.
|
2014-03-14 06:58:06 +08:00
|
|
|
for (auto *PI : PDecl->protocols())
|
2016-04-13 07:01:55 +08:00
|
|
|
CollectImmediateProperties(PI, PropMap, SuperPropMap,
|
|
|
|
CollectClassPropsOnly);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-15 02:35:57 +08:00
|
|
|
/// CollectSuperClassPropertyImplementations - This routine collects list of
|
|
|
|
/// properties to be implemented in super class(s) and also coming from their
|
|
|
|
/// conforming protocols.
|
|
|
|
static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
|
2012-10-31 09:18:22 +08:00
|
|
|
ObjCInterfaceDecl::PropertyMap &PropMap) {
|
2010-05-15 02:35:57 +08:00
|
|
|
if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
|
2013-02-15 06:33:34 +08:00
|
|
|
ObjCInterfaceDecl::PropertyDeclOrder PO;
|
2010-05-15 02:35:57 +08:00
|
|
|
while (SDecl) {
|
2013-02-15 06:33:34 +08:00
|
|
|
SDecl->collectPropertiesToImplement(PropMap, PO);
|
2010-05-15 02:35:57 +08:00
|
|
|
SDecl = SDecl->getSuperClass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 03:07:19 +08:00
|
|
|
/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
|
|
|
|
/// an ivar synthesized for 'Method' and 'Method' is a property accessor
|
|
|
|
/// declared in class 'IFace'.
|
|
|
|
bool
|
|
|
|
Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
|
|
|
|
ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
|
|
|
|
if (!IV->getSynthesize())
|
|
|
|
return false;
|
|
|
|
ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
|
|
|
|
Method->isInstanceMethod());
|
|
|
|
if (!IMD || !IMD->isPropertyAccessor())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// look up a property declaration whose one of its accessors is implemented
|
|
|
|
// by this method.
|
2016-01-27 02:05:23 +08:00
|
|
|
for (const auto *Property : IFace->instance_properties()) {
|
2014-03-14 02:47:37 +08:00
|
|
|
if ((Property->getGetterName() == IMD->getSelector() ||
|
|
|
|
Property->getSetterName() == IMD->getSelector()) &&
|
|
|
|
(Property->getPropertyIvarDecl() == IV))
|
2013-02-15 03:07:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
// Also look up property declaration in class extension whose one of its
|
|
|
|
// accessors is implemented by this method.
|
|
|
|
for (const auto *Ext : IFace->known_extensions())
|
2016-01-27 02:05:23 +08:00
|
|
|
for (const auto *Property : Ext->instance_properties())
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
if ((Property->getGetterName() == IMD->getSelector() ||
|
|
|
|
Property->getSetterName() == IMD->getSelector()) &&
|
|
|
|
(Property->getPropertyIvarDecl() == IV))
|
|
|
|
return true;
|
2013-02-15 03:07:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-06 07:44:00 +08:00
|
|
|
static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
|
|
|
|
ObjCPropertyDecl *Prop) {
|
|
|
|
bool SuperClassImplementsGetter = false;
|
|
|
|
bool SuperClassImplementsSetter = false;
|
|
|
|
if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
|
|
|
|
SuperClassImplementsSetter = true;
|
2014-03-12 01:17:16 +08:00
|
|
|
|
2014-03-06 07:44:00 +08:00
|
|
|
while (IDecl->getSuperClass()) {
|
|
|
|
ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
|
|
|
|
if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
|
|
|
|
SuperClassImplementsGetter = true;
|
2014-03-12 01:17:16 +08:00
|
|
|
|
2014-03-06 07:44:00 +08:00
|
|
|
if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
|
|
|
|
SuperClassImplementsSetter = true;
|
|
|
|
if (SuperClassImplementsGetter && SuperClassImplementsSetter)
|
|
|
|
return true;
|
|
|
|
IDecl = IDecl->getSuperClass();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-15 03:07:19 +08:00
|
|
|
|
2012-06-15 15:13:21 +08:00
|
|
|
/// \brief Default synthesizes all properties which must be synthesized
|
|
|
|
/// in class's \@implementation.
|
2017-07-03 18:12:24 +08:00
|
|
|
void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
|
|
|
|
ObjCInterfaceDecl *IDecl,
|
|
|
|
SourceLocation AtEnd) {
|
2012-10-19 03:17:53 +08:00
|
|
|
ObjCInterfaceDecl::PropertyMap PropMap;
|
2013-02-15 06:33:34 +08:00
|
|
|
ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
|
|
|
|
IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
|
2010-05-15 02:35:57 +08:00
|
|
|
if (PropMap.empty())
|
|
|
|
return;
|
2012-10-19 03:17:53 +08:00
|
|
|
ObjCInterfaceDecl::PropertyMap SuperPropMap;
|
2010-05-15 02:35:57 +08:00
|
|
|
CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
|
|
|
|
|
2013-02-15 06:33:34 +08:00
|
|
|
for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
|
|
|
|
ObjCPropertyDecl *Prop = PropertyOrder[i];
|
2013-06-08 04:26:51 +08:00
|
|
|
// Is there a matching property synthesize/dynamic?
|
|
|
|
if (Prop->isInvalidDecl() ||
|
2016-01-29 07:36:05 +08:00
|
|
|
Prop->isClassProperty() ||
|
2013-06-08 04:26:51 +08:00
|
|
|
Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
|
|
|
|
continue;
|
|
|
|
// Property may have been synthesized by user.
|
2016-01-29 02:49:28 +08:00
|
|
|
if (IMPDecl->FindPropertyImplDecl(
|
|
|
|
Prop->getIdentifier(), Prop->getQueryKind()))
|
2013-06-08 04:26:51 +08:00
|
|
|
continue;
|
|
|
|
if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
|
|
|
|
if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
|
|
|
|
continue;
|
|
|
|
if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-08 02:32:55 +08:00
|
|
|
if (ObjCPropertyImplDecl *PID =
|
|
|
|
IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
|
2014-07-27 04:52:26 +08:00
|
|
|
Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
|
|
|
|
<< Prop->getIdentifier();
|
2015-10-03 18:46:20 +08:00
|
|
|
if (PID->getLocation().isValid())
|
2014-07-27 04:52:26 +08:00
|
|
|
Diag(PID->getLocation(), diag::note_property_synthesize);
|
2013-06-08 02:32:55 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-29 07:36:05 +08:00
|
|
|
ObjCPropertyDecl *PropInSuperClass =
|
|
|
|
SuperPropMap[std::make_pair(Prop->getIdentifier(),
|
|
|
|
Prop->isClassProperty())];
|
2013-12-13 07:40:14 +08:00
|
|
|
if (ObjCProtocolDecl *Proto =
|
|
|
|
dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
|
2011-12-15 09:03:18 +08:00
|
|
|
// We won't auto-synthesize properties declared in protocols.
|
2014-03-06 07:44:00 +08:00
|
|
|
// Suppress the warning if class's superclass implements property's
|
|
|
|
// getter and implements property's setter (if readwrite property).
|
2014-08-30 04:29:31 +08:00
|
|
|
// Or, if property is going to be implemented in its super class.
|
|
|
|
if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
|
2014-03-06 07:44:00 +08:00
|
|
|
Diag(IMPDecl->getLocation(),
|
|
|
|
diag::warn_auto_synthesizing_protocol_property)
|
|
|
|
<< Prop << Proto;
|
|
|
|
Diag(Prop->getLocation(), diag::note_property_declare);
|
2017-07-03 18:12:24 +08:00
|
|
|
std::string FixIt =
|
|
|
|
(Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
|
|
|
|
Diag(AtEnd, diag::note_add_synthesize_directive)
|
|
|
|
<< FixItHint::CreateInsertion(AtEnd, FixIt);
|
2014-03-06 07:44:00 +08:00
|
|
|
}
|
2011-12-15 09:03:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-30 02:31:16 +08:00
|
|
|
// If property to be implemented in the super class, ignore.
|
2014-08-30 04:29:31 +08:00
|
|
|
if (PropInSuperClass) {
|
2014-08-30 02:31:16 +08:00
|
|
|
if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
|
|
|
|
(PropInSuperClass->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_readonly) &&
|
|
|
|
!IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
|
|
|
|
!IDecl->HasUserDeclaredSetterMethod(Prop)) {
|
|
|
|
Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
|
|
|
|
<< Prop->getIdentifier();
|
|
|
|
Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
|
|
|
|
<< Prop->getIdentifier();
|
2014-10-11 06:08:23 +08:00
|
|
|
Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
|
2014-08-30 02:31:16 +08:00
|
|
|
Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2010-09-24 09:23:01 +08:00
|
|
|
// We use invalid SourceLocations for the synthesized ivars since they
|
|
|
|
// aren't really synthesized at a particular location; they just exist.
|
|
|
|
// Saying that they are located at the @implementation isn't really going
|
|
|
|
// to help users.
|
2012-05-04 00:43:30 +08:00
|
|
|
ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
|
|
|
|
ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
|
|
|
|
true,
|
|
|
|
/* property = */ Prop->getIdentifier(),
|
2012-09-28 03:45:11 +08:00
|
|
|
/* ivar = */ Prop->getDefaultSynthIvarName(Context),
|
2016-01-29 02:49:28 +08:00
|
|
|
Prop->getLocation(), Prop->getQueryKind()));
|
2017-08-15 20:40:01 +08:00
|
|
|
if (PIDecl && !Prop->isUnavailable()) {
|
2012-05-04 00:43:30 +08:00
|
|
|
Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
|
2012-05-09 02:03:39 +08:00
|
|
|
Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
|
2012-05-04 00:43:30 +08:00
|
|
|
}
|
2010-09-24 09:23:01 +08:00
|
|
|
}
|
2010-05-15 02:35:57 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2017-07-03 18:12:24 +08:00
|
|
|
void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
|
|
|
|
SourceLocation AtEnd) {
|
2012-06-20 14:18:46 +08:00
|
|
|
if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
|
2011-09-01 06:24:06 +08:00
|
|
|
return;
|
|
|
|
ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
|
|
|
|
if (!IC)
|
|
|
|
return;
|
|
|
|
if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
|
2012-01-06 06:47:47 +08:00
|
|
|
if (!IDecl->isObjCRequiresPropertyDefs())
|
2017-07-03 18:12:24 +08:00
|
|
|
DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
|
2011-09-01 06:24:06 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 02:12:34 +08:00
|
|
|
static void DiagnoseUnimplementedAccessor(
|
|
|
|
Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
|
|
|
|
ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
|
|
|
|
ObjCPropertyDecl *Prop,
|
|
|
|
llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
|
|
|
|
// Check to see if we have a corresponding selector in SMap and with the
|
|
|
|
// right method type.
|
|
|
|
auto I = std::find_if(SMap.begin(), SMap.end(),
|
|
|
|
[&](const ObjCMethodDecl *x) {
|
|
|
|
return x->getSelector() == Method &&
|
|
|
|
x->isClassMethod() == Prop->isClassProperty();
|
|
|
|
});
|
2014-02-22 03:41:30 +08:00
|
|
|
// When reporting on missing property setter/getter implementation in
|
|
|
|
// categories, do not report when they are declared in primary class,
|
|
|
|
// class's protocol, or one of it super classes. This is because,
|
|
|
|
// the class is going to implement them.
|
2016-05-19 02:12:34 +08:00
|
|
|
if (I == SMap.end() &&
|
2014-05-26 14:22:03 +08:00
|
|
|
(PrimaryClass == nullptr ||
|
2016-01-28 04:10:32 +08:00
|
|
|
!PrimaryClass->lookupPropertyAccessor(Method, C,
|
|
|
|
Prop->isClassProperty()))) {
|
2016-04-13 07:01:55 +08:00
|
|
|
unsigned diag =
|
|
|
|
isa<ObjCCategoryDecl>(CDecl)
|
|
|
|
? (Prop->isClassProperty()
|
|
|
|
? diag::warn_impl_required_in_category_for_class_property
|
|
|
|
: diag::warn_setter_getter_impl_required_in_category)
|
|
|
|
: (Prop->isClassProperty()
|
|
|
|
? diag::warn_impl_required_for_class_property
|
|
|
|
: diag::warn_setter_getter_impl_required);
|
|
|
|
S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
|
|
|
|
S.Diag(Prop->getLocation(), diag::note_property_declare);
|
|
|
|
if (S.LangOpts.ObjCDefaultSynthProperties &&
|
|
|
|
S.LangOpts.ObjCRuntime.isNonFragile())
|
|
|
|
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
|
|
|
|
if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
|
|
|
|
S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
|
|
|
|
}
|
2014-02-22 03:41:30 +08:00
|
|
|
}
|
|
|
|
|
2010-05-06 05:52:17 +08:00
|
|
|
void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
|
2014-02-22 03:41:34 +08:00
|
|
|
ObjCContainerDecl *CDecl,
|
|
|
|
bool SynthesizeProperties) {
|
2012-10-19 03:17:53 +08:00
|
|
|
ObjCContainerDecl::PropertyMap PropMap;
|
2014-02-22 03:41:39 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
|
|
|
|
|
2016-04-13 07:01:55 +08:00
|
|
|
// Since we don't synthesize class properties, we should emit diagnose even
|
|
|
|
// if SynthesizeProperties is true.
|
|
|
|
ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
|
|
|
|
// Gather properties which need not be implemented in this class
|
|
|
|
// or category.
|
|
|
|
if (!IDecl)
|
|
|
|
if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
|
|
|
|
// For categories, no need to implement properties declared in
|
|
|
|
// its primary class (and its super classes) if property is
|
|
|
|
// declared in one of those containers.
|
|
|
|
if ((IDecl = C->getClassInterface())) {
|
|
|
|
ObjCInterfaceDecl::PropertyDeclOrder PO;
|
|
|
|
IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
|
2014-02-22 03:41:34 +08:00
|
|
|
}
|
2016-04-13 07:01:55 +08:00
|
|
|
}
|
|
|
|
if (IDecl)
|
|
|
|
CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
|
2014-02-22 03:41:34 +08:00
|
|
|
|
2016-04-13 07:01:55 +08:00
|
|
|
// When SynthesizeProperties is true, we only check class properties.
|
|
|
|
CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
|
|
|
|
SynthesizeProperties/*CollectClassPropsOnly*/);
|
2014-02-22 03:41:34 +08:00
|
|
|
|
2014-02-22 03:41:39 +08:00
|
|
|
// Scan the @interface to see if any of the protocols it adopts
|
|
|
|
// require an explicit implementation, via attribute
|
|
|
|
// 'objc_protocol_requires_explicit_implementation'.
|
2014-02-22 08:02:03 +08:00
|
|
|
if (IDecl) {
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
|
2014-02-22 08:02:03 +08:00
|
|
|
|
2014-03-14 04:55:22 +08:00
|
|
|
for (auto *PDecl : IDecl->all_referenced_protocols()) {
|
2014-02-22 03:41:39 +08:00
|
|
|
if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
|
|
|
|
continue;
|
2014-02-22 08:02:03 +08:00
|
|
|
// Lazily construct a set of all the properties in the @interface
|
|
|
|
// of the class, without looking at the superclass. We cannot
|
|
|
|
// use the call to CollectImmediateProperties() above as that
|
2014-05-21 01:10:39 +08:00
|
|
|
// utilizes information from the super class's properties as well
|
2014-02-22 08:02:03 +08:00
|
|
|
// as scans the adopted protocols. This work only triggers for protocols
|
|
|
|
// with the attribute, which is very rare, and only occurs when
|
|
|
|
// analyzing the @implementation.
|
|
|
|
if (!LazyMap) {
|
|
|
|
ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
|
|
|
|
LazyMap.reset(new ObjCContainerDecl::PropertyMap());
|
|
|
|
CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
|
2016-04-13 07:01:55 +08:00
|
|
|
/* CollectClassPropsOnly */ false,
|
2014-02-22 08:02:03 +08:00
|
|
|
/* IncludeProtocols */ false);
|
|
|
|
}
|
2014-02-22 03:41:39 +08:00
|
|
|
// Add the properties of 'PDecl' to the list of properties that
|
|
|
|
// need to be implemented.
|
2016-01-29 07:36:05 +08:00
|
|
|
for (auto *PropDecl : PDecl->properties()) {
|
|
|
|
if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
|
|
|
|
PropDecl->isClassProperty())])
|
2014-02-22 08:02:03 +08:00
|
|
|
continue;
|
2016-01-29 07:36:05 +08:00
|
|
|
PropMap[std::make_pair(PropDecl->getIdentifier(),
|
|
|
|
PropDecl->isClassProperty())] = PropDecl;
|
2014-02-22 03:41:39 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 08:02:03 +08:00
|
|
|
}
|
2014-02-22 03:41:39 +08:00
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
if (PropMap.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
|
2014-03-14 23:02:45 +08:00
|
|
|
for (const auto *I : IMPDecl->property_impls())
|
2012-04-30 10:36:29 +08:00
|
|
|
PropImplMap.insert(I->getPropertyDecl());
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2016-05-19 02:12:34 +08:00
|
|
|
llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
|
2013-04-25 01:06:38 +08:00
|
|
|
// Collect property accessors implemented in current implementation.
|
2016-01-29 07:36:05 +08:00
|
|
|
for (const auto *I : IMPDecl->methods())
|
2016-05-19 02:12:34 +08:00
|
|
|
InsMap.insert(I);
|
2013-04-25 01:06:38 +08:00
|
|
|
|
|
|
|
ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCInterfaceDecl *PrimaryClass = nullptr;
|
2013-04-25 01:06:38 +08:00
|
|
|
if (C && !C->IsClassExtension())
|
|
|
|
if ((PrimaryClass = C->getClassInterface()))
|
|
|
|
// Report unimplemented properties in the category as well.
|
|
|
|
if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
|
|
|
|
// When reporting on missing setter/getters, do not report when
|
|
|
|
// setter/getter is implemented in category's primary class
|
|
|
|
// implementation.
|
2016-01-29 07:36:05 +08:00
|
|
|
for (const auto *I : IMP->methods())
|
2016-05-19 02:12:34 +08:00
|
|
|
InsMap.insert(I);
|
2013-04-25 01:06:38 +08:00
|
|
|
}
|
|
|
|
|
2012-10-19 03:17:53 +08:00
|
|
|
for (ObjCContainerDecl::PropertyMap::iterator
|
2010-03-12 08:38:38 +08:00
|
|
|
P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
|
|
|
|
ObjCPropertyDecl *Prop = P->second;
|
2016-04-13 07:01:55 +08:00
|
|
|
// Is there a matching property synthesize/dynamic?
|
2010-03-12 08:38:38 +08:00
|
|
|
if (Prop->isInvalidDecl() ||
|
|
|
|
Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
|
2013-01-09 02:16:18 +08:00
|
|
|
PropImplMap.count(Prop) ||
|
|
|
|
Prop->getAvailability() == AR_Unavailable)
|
2010-03-12 08:38:38 +08:00
|
|
|
continue;
|
2014-02-22 03:41:30 +08:00
|
|
|
|
|
|
|
// Diagnose unimplemented getters and setters.
|
|
|
|
DiagnoseUnimplementedAccessor(*this,
|
|
|
|
PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
|
|
|
|
if (!Prop->isReadOnly())
|
|
|
|
DiagnoseUnimplementedAccessor(*this,
|
|
|
|
PrimaryClass, Prop->getSetterName(),
|
|
|
|
IMPDecl, CDecl, C, Prop, InsMap);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 06:02:08 +08:00
|
|
|
void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
|
2015-06-20 02:14:46 +08:00
|
|
|
for (const auto *propertyImpl : impDecl->property_impls()) {
|
|
|
|
const auto *property = propertyImpl->getPropertyDecl();
|
|
|
|
|
|
|
|
// Warn about null_resettable properties with synthesized setters,
|
|
|
|
// because the setter won't properly handle nil.
|
|
|
|
if (propertyImpl->getPropertyImplementation()
|
|
|
|
== ObjCPropertyImplDecl::Synthesize &&
|
|
|
|
(property->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_null_resettable) &&
|
|
|
|
property->getGetterMethodDecl() &&
|
|
|
|
property->getSetterMethodDecl()) {
|
|
|
|
auto *getterMethod = property->getGetterMethodDecl();
|
|
|
|
auto *setterMethod = property->getSetterMethodDecl();
|
|
|
|
if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
|
|
|
|
!impDecl->getInstanceMethod(getterMethod->getSelector())) {
|
|
|
|
SourceLocation loc = propertyImpl->getLocation();
|
|
|
|
if (loc.isInvalid())
|
|
|
|
loc = impDecl->getLocStart();
|
|
|
|
|
|
|
|
Diag(loc, diag::warn_null_resettable_setter)
|
|
|
|
<< setterMethod->getSelector() << property->getDeclName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
void
|
|
|
|
Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
ObjCInterfaceDecl* IDecl) {
|
2010-03-12 08:38:38 +08:00
|
|
|
// Rules apply in non-GC mode only
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().getGC() != LangOptions::NonGC)
|
2010-03-12 08:38:38 +08:00
|
|
|
return;
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
ObjCContainerDecl::PropertyMap PM;
|
2016-01-29 07:36:05 +08:00
|
|
|
for (auto *Prop : IDecl->properties())
|
|
|
|
PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
for (const auto *Ext : IDecl->known_extensions())
|
2016-01-29 07:36:05 +08:00
|
|
|
for (auto *Prop : Ext->properties())
|
|
|
|
PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
|
2016-01-28 04:00:32 +08:00
|
|
|
for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
|
|
|
|
I != E; ++I) {
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
const ObjCPropertyDecl *Property = I->second;
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCMethodDecl *GetterMethod = nullptr;
|
|
|
|
ObjCMethodDecl *SetterMethod = nullptr;
|
2011-02-01 05:34:11 +08:00
|
|
|
bool LookedUpGetterSetter = false;
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
unsigned Attributes = Property->getPropertyAttributes();
|
2011-09-14 02:31:23 +08:00
|
|
|
unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
|
2011-02-01 05:34:11 +08:00
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
|
|
|
|
!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
|
2016-01-28 04:10:32 +08:00
|
|
|
GetterMethod = Property->isClassProperty() ?
|
|
|
|
IMPDecl->getClassMethod(Property->getGetterName()) :
|
|
|
|
IMPDecl->getInstanceMethod(Property->getGetterName());
|
|
|
|
SetterMethod = Property->isClassProperty() ?
|
|
|
|
IMPDecl->getClassMethod(Property->getSetterName()) :
|
|
|
|
IMPDecl->getInstanceMethod(Property->getSetterName());
|
2011-02-01 05:34:11 +08:00
|
|
|
LookedUpGetterSetter = true;
|
|
|
|
if (GetterMethod) {
|
|
|
|
Diag(GetterMethod->getLocation(),
|
|
|
|
diag::warn_default_atomic_custom_getter_setter)
|
2011-02-01 07:20:03 +08:00
|
|
|
<< Property->getIdentifier() << 0;
|
2011-02-01 05:34:11 +08:00
|
|
|
Diag(Property->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
if (SetterMethod) {
|
|
|
|
Diag(SetterMethod->getLocation(),
|
|
|
|
diag::warn_default_atomic_custom_getter_setter)
|
2011-02-01 07:20:03 +08:00
|
|
|
<< Property->getIdentifier() << 1;
|
2011-02-01 05:34:11 +08:00
|
|
|
Diag(Property->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
// We only care about readwrite atomic property.
|
2012-12-21 03:22:21 +08:00
|
|
|
if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
|
|
|
|
!(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
|
2010-03-12 08:38:38 +08:00
|
|
|
continue;
|
2016-01-29 02:49:28 +08:00
|
|
|
if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
|
|
|
|
Property->getIdentifier(), Property->getQueryKind())) {
|
2010-03-12 08:38:38 +08:00
|
|
|
if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
2011-02-01 05:34:11 +08:00
|
|
|
if (!LookedUpGetterSetter) {
|
2016-01-28 04:10:32 +08:00
|
|
|
GetterMethod = Property->isClassProperty() ?
|
|
|
|
IMPDecl->getClassMethod(Property->getGetterName()) :
|
|
|
|
IMPDecl->getInstanceMethod(Property->getGetterName());
|
|
|
|
SetterMethod = Property->isClassProperty() ?
|
|
|
|
IMPDecl->getClassMethod(Property->getSetterName()) :
|
|
|
|
IMPDecl->getInstanceMethod(Property->getSetterName());
|
2011-02-01 05:34:11 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
|
|
|
|
SourceLocation MethodLoc =
|
|
|
|
(GetterMethod ? GetterMethod->getLocation()
|
|
|
|
: SetterMethod->getLocation());
|
|
|
|
Diag(MethodLoc, diag::warn_atomic_property_rule)
|
2014-05-26 14:22:03 +08:00
|
|
|
<< Property->getIdentifier() << (GetterMethod != nullptr)
|
|
|
|
<< (SetterMethod != nullptr);
|
2012-03-01 06:18:55 +08:00
|
|
|
// fixit stuff.
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
if (Property->getLParenLoc().isValid() &&
|
|
|
|
!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
|
2012-03-01 06:18:55 +08:00
|
|
|
// @property () ... case.
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
SourceLocation AfterLParen =
|
|
|
|
getLocForEndOfToken(Property->getLParenLoc());
|
|
|
|
StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
|
|
|
|
: "nonatomic";
|
|
|
|
Diag(Property->getLocation(),
|
|
|
|
diag::note_atomic_property_fixup_suggest)
|
|
|
|
<< FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
|
|
|
|
} else if (Property->getLParenLoc().isInvalid()) {
|
|
|
|
//@property id etc.
|
|
|
|
SourceLocation startLoc =
|
|
|
|
Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
|
|
|
|
Diag(Property->getLocation(),
|
|
|
|
diag::note_atomic_property_fixup_suggest)
|
|
|
|
<< FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
|
2012-03-01 06:18:55 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
|
2010-03-12 08:38:38 +08:00
|
|
|
Diag(Property->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().getGC() == LangOptions::GCOnly)
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
|
2014-03-14 23:02:45 +08:00
|
|
|
for (const auto *PID : D->property_impls()) {
|
2011-06-16 07:02:42 +08:00
|
|
|
const ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
2011-06-25 08:17:46 +08:00
|
|
|
if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
|
2016-01-28 04:10:32 +08:00
|
|
|
!PD->isClassProperty() &&
|
2011-06-25 08:17:46 +08:00
|
|
|
!D->getInstanceMethod(PD->getGetterName())) {
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCMethodDecl *method = PD->getGetterMethodDecl();
|
|
|
|
if (!method)
|
|
|
|
continue;
|
|
|
|
ObjCMethodFamily family = method->getMethodFamily();
|
|
|
|
if (family == OMF_alloc || family == OMF_copy ||
|
|
|
|
family == OMF_mutableCopy || family == OMF_new) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
2014-01-10 08:53:48 +08:00
|
|
|
Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
|
2011-06-16 07:02:42 +08:00
|
|
|
else
|
2014-01-10 08:53:48 +08:00
|
|
|
Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
|
2015-01-17 07:04:31 +08:00
|
|
|
|
|
|
|
// Look for a getter explicitly declared alongside the property.
|
|
|
|
// If we find one, use its location for the note.
|
|
|
|
SourceLocation noteLoc = PD->getLocation();
|
|
|
|
SourceLocation fixItLoc;
|
|
|
|
for (auto *getterRedecl : method->redecls()) {
|
|
|
|
if (getterRedecl->isImplicit())
|
|
|
|
continue;
|
|
|
|
if (getterRedecl->getDeclContext() != PD->getDeclContext())
|
|
|
|
continue;
|
|
|
|
noteLoc = getterRedecl->getLocation();
|
|
|
|
fixItLoc = getterRedecl->getLocEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
Preprocessor &PP = getPreprocessor();
|
|
|
|
TokenValue tokens[] = {
|
|
|
|
tok::kw___attribute, tok::l_paren, tok::l_paren,
|
|
|
|
PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
|
|
|
|
PP.getIdentifierInfo("none"), tok::r_paren,
|
|
|
|
tok::r_paren, tok::r_paren
|
|
|
|
};
|
|
|
|
StringRef spelling = "__attribute__((objc_method_family(none)))";
|
|
|
|
StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
|
|
|
|
if (!macroName.empty())
|
|
|
|
spelling = macroName;
|
|
|
|
|
|
|
|
auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
|
|
|
|
<< method->getDeclName() << spelling;
|
|
|
|
if (fixItLoc.isValid()) {
|
|
|
|
SmallString<64> fixItText(" ");
|
|
|
|
fixItText += spelling;
|
|
|
|
noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 05:11:54 +08:00
|
|
|
void Sema::DiagnoseMissingDesignatedInitOverrides(
|
2015-03-12 00:59:48 +08:00
|
|
|
const ObjCImplementationDecl *ImplD,
|
|
|
|
const ObjCInterfaceDecl *IFD) {
|
|
|
|
assert(IFD->hasDesignatedInitializers());
|
2013-12-04 05:11:54 +08:00
|
|
|
const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
|
|
|
|
if (!SuperD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SelectorSet InitSelSet;
|
2014-03-14 03:50:17 +08:00
|
|
|
for (const auto *I : ImplD->instance_methods())
|
|
|
|
if (I->getMethodFamily() == OMF_init)
|
|
|
|
InitSelSet.insert(I->getSelector());
|
2013-12-04 05:11:54 +08:00
|
|
|
|
|
|
|
SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
|
|
|
|
SuperD->getDesignatedInitializers(DesignatedInits);
|
|
|
|
for (SmallVector<const ObjCMethodDecl *, 8>::iterator
|
|
|
|
I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
|
|
|
|
const ObjCMethodDecl *MD = *I;
|
|
|
|
if (!InitSelSet.count(MD->getSelector())) {
|
2015-07-31 03:06:04 +08:00
|
|
|
bool Ignore = false;
|
|
|
|
if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
|
|
|
|
Ignore = IMD->isUnavailable();
|
|
|
|
}
|
|
|
|
if (!Ignore) {
|
|
|
|
Diag(ImplD->getLocation(),
|
|
|
|
diag::warn_objc_implementation_missing_designated_init_override)
|
|
|
|
<< MD->getSelector();
|
|
|
|
Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
|
|
|
|
}
|
2013-12-04 05:11:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-10 15:01:40 +08:00
|
|
|
/// AddPropertyAttrs - Propagates attributes from a property to the
|
|
|
|
/// implicitly-declared getter or setter for that property.
|
|
|
|
static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
|
|
|
|
ObjCPropertyDecl *Property) {
|
|
|
|
// Should we just clone all attributes over?
|
2014-03-09 06:19:01 +08:00
|
|
|
for (const auto *A : Property->attrs()) {
|
|
|
|
if (isa<DeprecatedAttr>(A) ||
|
|
|
|
isa<UnavailableAttr>(A) ||
|
|
|
|
isa<AvailabilityAttr>(A))
|
|
|
|
PropertyMethod->addAttr(A->clone(S.Context));
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
}
|
2010-11-10 15:01:40 +08:00
|
|
|
}
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
|
|
|
|
/// have the property type and issue diagnostics if they don't.
|
|
|
|
/// Also synthesize a getter/setter method if none exist (and update the
|
2015-11-04 01:02:34 +08:00
|
|
|
/// appropriate lookup tables.
|
|
|
|
void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCMethodDecl *GetterMethod, *SetterMethod;
|
2015-11-04 01:02:34 +08:00
|
|
|
ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
|
2014-05-28 02:26:09 +08:00
|
|
|
if (CD->isInvalidDecl())
|
|
|
|
return;
|
|
|
|
|
2016-01-28 04:10:32 +08:00
|
|
|
bool IsClassProperty = property->isClassProperty();
|
|
|
|
GetterMethod = IsClassProperty ?
|
|
|
|
CD->getClassMethod(property->getGetterName()) :
|
|
|
|
CD->getInstanceMethod(property->getGetterName());
|
|
|
|
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
// if setter or getter is not found in class extension, it might be
|
|
|
|
// in the primary class.
|
|
|
|
if (!GetterMethod)
|
|
|
|
if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
|
|
|
|
if (CatDecl->IsClassExtension())
|
2016-01-28 04:10:32 +08:00
|
|
|
GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
|
|
|
|
getClassMethod(property->getGetterName()) :
|
|
|
|
CatDecl->getClassInterface()->
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
getInstanceMethod(property->getGetterName());
|
|
|
|
|
2016-01-28 04:10:32 +08:00
|
|
|
SetterMethod = IsClassProperty ?
|
|
|
|
CD->getClassMethod(property->getSetterName()) :
|
|
|
|
CD->getInstanceMethod(property->getSetterName());
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
if (!SetterMethod)
|
|
|
|
if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
|
|
|
|
if (CatDecl->IsClassExtension())
|
2016-01-28 04:10:32 +08:00
|
|
|
SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
|
|
|
|
getClassMethod(property->getSetterName()) :
|
|
|
|
CatDecl->getClassInterface()->
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
getInstanceMethod(property->getSetterName());
|
2010-03-12 08:38:38 +08:00
|
|
|
DiagnosePropertyAccessorMismatch(property, GetterMethod,
|
|
|
|
property->getLocation());
|
|
|
|
|
2017-03-30 21:33:51 +08:00
|
|
|
if (!property->isReadOnly() && SetterMethod) {
|
|
|
|
if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
|
|
|
|
Context.VoidTy)
|
2010-03-12 08:38:38 +08:00
|
|
|
Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
|
|
|
|
if (SetterMethod->param_size() != 1 ||
|
2011-09-27 06:59:09 +08:00
|
|
|
!Context.hasSameUnqualifiedType(
|
2011-10-16 01:36:49 +08:00
|
|
|
(*SetterMethod->param_begin())->getType().getNonReferenceType(),
|
|
|
|
property->getType().getNonReferenceType())) {
|
2010-03-12 08:38:38 +08:00
|
|
|
Diag(property->getLocation(),
|
|
|
|
diag::warn_accessor_property_type_mismatch)
|
|
|
|
<< property->getDeclName()
|
|
|
|
<< SetterMethod->getSelector();
|
|
|
|
Diag(SetterMethod->getLocation(), diag::note_declared_at);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synthesize getter/setter methods if none exist.
|
|
|
|
// Find the default getter and if one not found, add one.
|
|
|
|
// FIXME: The synthesized property we set here is misleading. We almost always
|
|
|
|
// synthesize these methods unless the user explicitly provided prototypes
|
|
|
|
// (which is odd, but allowed). Sema should be typechecking that the
|
|
|
|
// declarations jive in that situation (which it is not currently).
|
|
|
|
if (!GetterMethod) {
|
2016-01-28 04:10:32 +08:00
|
|
|
// No instance/class method of same name as property getter name was found.
|
2010-03-12 08:38:38 +08:00
|
|
|
// Declare a getter method and add it to the list of methods
|
|
|
|
// for this class.
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
SourceLocation Loc = property->getLocation();
|
2010-09-22 04:52:59 +08:00
|
|
|
|
2016-05-26 08:37:30 +08:00
|
|
|
// The getter returns the declared property type with all qualifiers
|
|
|
|
// removed.
|
|
|
|
QualType resultTy = property->getType().getAtomicUnqualifiedType();
|
|
|
|
|
2015-06-20 02:14:46 +08:00
|
|
|
// If the property is null_resettable, the getter returns nonnull.
|
|
|
|
if (property->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_null_resettable) {
|
|
|
|
QualType modifiedTy = resultTy;
|
2015-06-25 06:02:08 +08:00
|
|
|
if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
|
2015-06-20 02:14:46 +08:00
|
|
|
if (*nullability == NullabilityKind::Unspecified)
|
|
|
|
resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
|
|
|
|
modifiedTy, modifiedTy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-22 04:52:59 +08:00
|
|
|
GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
|
|
|
|
property->getGetterName(),
|
2015-06-20 02:14:46 +08:00
|
|
|
resultTy, nullptr, CD,
|
2016-01-28 04:10:32 +08:00
|
|
|
!IsClassProperty, /*isVariadic=*/false,
|
2014-05-26 14:22:03 +08:00
|
|
|
/*isPropertyAccessor=*/true,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isImplicitlyDeclared=*/true, /*isDefined=*/false,
|
2010-03-12 08:38:38 +08:00
|
|
|
(property->getPropertyImplementation() ==
|
|
|
|
ObjCPropertyDecl::Optional) ?
|
|
|
|
ObjCMethodDecl::Optional :
|
|
|
|
ObjCMethodDecl::Required);
|
|
|
|
CD->addDecl(GetterMethod);
|
2010-11-10 15:01:40 +08:00
|
|
|
|
|
|
|
AddPropertyAttrs(*this, GetterMethod, property);
|
|
|
|
|
2011-06-25 08:17:46 +08:00
|
|
|
if (property->hasAttr<NSReturnsNotRetainedAttr>())
|
2014-01-16 21:03:14 +08:00
|
|
|
GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
|
|
|
|
Loc));
|
2013-09-20 00:37:20 +08:00
|
|
|
|
|
|
|
if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
|
|
|
|
GetterMethod->addAttr(
|
2014-01-16 21:03:14 +08:00
|
|
|
ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
|
2013-12-19 07:09:57 +08:00
|
|
|
|
|
|
|
if (const SectionAttr *SA = property->getAttr<SectionAttr>())
|
2014-04-09 06:30:47 +08:00
|
|
|
GetterMethod->addAttr(
|
|
|
|
SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
|
|
|
|
SA->getName(), Loc));
|
2013-04-04 09:38:37 +08:00
|
|
|
|
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
|
|
|
CheckARCMethodDecl(GetterMethod);
|
2010-03-12 08:38:38 +08:00
|
|
|
} else
|
|
|
|
// A user declared getter will be synthesize when @synthesize of
|
|
|
|
// the property with the same name is seen in the @implementation
|
2012-10-11 00:42:25 +08:00
|
|
|
GetterMethod->setPropertyAccessor(true);
|
2010-03-12 08:38:38 +08:00
|
|
|
property->setGetterMethodDecl(GetterMethod);
|
|
|
|
|
|
|
|
// Skip setter if property is read-only.
|
|
|
|
if (!property->isReadOnly()) {
|
|
|
|
// Find the default setter and if one not found, add one.
|
|
|
|
if (!SetterMethod) {
|
2016-01-28 04:10:32 +08:00
|
|
|
// No instance/class method of same name as property setter name was
|
|
|
|
// found.
|
2010-03-12 08:38:38 +08:00
|
|
|
// Declare a setter method and add it to the list of methods
|
|
|
|
// for this class.
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
SourceLocation Loc = property->getLocation();
|
2010-09-22 02:28:43 +08:00
|
|
|
|
|
|
|
SetterMethod =
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethodDecl::Create(Context, Loc, Loc,
|
2014-05-26 14:22:03 +08:00
|
|
|
property->getSetterName(), Context.VoidTy,
|
2016-01-28 04:10:32 +08:00
|
|
|
nullptr, CD, !IsClassProperty,
|
2014-05-26 14:22:03 +08:00
|
|
|
/*isVariadic=*/false,
|
2012-10-11 00:42:25 +08:00
|
|
|
/*isPropertyAccessor=*/true,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isImplicitlyDeclared=*/true,
|
|
|
|
/*isDefined=*/false,
|
2010-03-12 08:38:38 +08:00
|
|
|
(property->getPropertyImplementation() ==
|
|
|
|
ObjCPropertyDecl::Optional) ?
|
2010-09-22 02:28:43 +08:00
|
|
|
ObjCMethodDecl::Optional :
|
|
|
|
ObjCMethodDecl::Required);
|
|
|
|
|
2016-05-26 08:37:30 +08:00
|
|
|
// Remove all qualifiers from the setter's parameter type.
|
|
|
|
QualType paramTy =
|
|
|
|
property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
|
|
|
|
|
2015-06-20 02:14:46 +08:00
|
|
|
// If the property is null_resettable, the setter accepts a
|
|
|
|
// nullable value.
|
|
|
|
if (property->getPropertyAttributes() &
|
|
|
|
ObjCPropertyDecl::OBJC_PR_null_resettable) {
|
|
|
|
QualType modifiedTy = paramTy;
|
|
|
|
if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
|
|
|
|
if (*nullability == NullabilityKind::Unspecified)
|
|
|
|
paramTy = Context.getAttributedType(AttributedType::attr_nullable,
|
|
|
|
modifiedTy, modifiedTy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
// Invent the arguments for the setter. We don't bother making a
|
|
|
|
// nice name for the argument.
|
2011-03-08 16:55:46 +08:00
|
|
|
ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
|
|
|
|
Loc, Loc,
|
2010-03-12 08:38:38 +08:00
|
|
|
property->getIdentifier(),
|
2015-06-20 02:14:46 +08:00
|
|
|
paramTy,
|
2014-05-26 14:22:03 +08:00
|
|
|
/*TInfo=*/nullptr,
|
2010-08-26 11:08:43 +08:00
|
|
|
SC_None,
|
2014-05-26 14:22:03 +08:00
|
|
|
nullptr);
|
2013-05-05 08:41:58 +08:00
|
|
|
SetterMethod->setMethodParams(Context, Argument, None);
|
2010-11-10 15:01:40 +08:00
|
|
|
|
|
|
|
AddPropertyAttrs(*this, SetterMethod, property);
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
CD->addDecl(SetterMethod);
|
2013-12-19 07:09:57 +08:00
|
|
|
if (const SectionAttr *SA = property->getAttr<SectionAttr>())
|
2014-04-09 06:30:47 +08:00
|
|
|
SetterMethod->addAttr(
|
|
|
|
SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
|
|
|
|
SA->getName(), Loc));
|
2013-04-04 09:38:37 +08:00
|
|
|
// It's possible for the user to have set a very odd custom
|
|
|
|
// setter selector that causes it to have a method family.
|
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
|
|
|
CheckARCMethodDecl(SetterMethod);
|
2010-03-12 08:38:38 +08:00
|
|
|
} else
|
|
|
|
// A user declared setter will be synthesize when @synthesize of
|
|
|
|
// the property with the same name is seen in the @implementation
|
2012-10-11 00:42:25 +08:00
|
|
|
SetterMethod->setPropertyAccessor(true);
|
2010-03-12 08:38:38 +08:00
|
|
|
property->setSetterMethodDecl(SetterMethod);
|
|
|
|
}
|
|
|
|
// Add any synthesized methods to the global pool. This allows us to
|
|
|
|
// handle the following, which is supported by GCC (and part of the design).
|
|
|
|
//
|
|
|
|
// @interface Foo
|
|
|
|
// @property double bar;
|
|
|
|
// @end
|
|
|
|
//
|
|
|
|
// void thisIsUnfortunate() {
|
|
|
|
// id foo;
|
|
|
|
// double bar = [foo bar];
|
|
|
|
// }
|
|
|
|
//
|
2016-01-28 04:10:32 +08:00
|
|
|
if (!IsClassProperty) {
|
|
|
|
if (GetterMethod)
|
|
|
|
AddInstanceMethodToGlobalPool(GetterMethod);
|
|
|
|
if (SetterMethod)
|
|
|
|
AddInstanceMethodToGlobalPool(SetterMethod);
|
2016-03-24 05:39:31 +08:00
|
|
|
} else {
|
|
|
|
if (GetterMethod)
|
|
|
|
AddFactoryMethodToGlobalPool(GetterMethod);
|
|
|
|
if (SetterMethod)
|
|
|
|
AddFactoryMethodToGlobalPool(SetterMethod);
|
2016-01-28 04:10:32 +08:00
|
|
|
}
|
2012-05-10 00:12:57 +08:00
|
|
|
|
|
|
|
ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
|
|
|
|
if (!CurrentClass) {
|
|
|
|
if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
|
|
|
|
CurrentClass = Cat->getClassInterface();
|
|
|
|
else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
|
|
|
|
CurrentClass = Impl->getClassInterface();
|
|
|
|
}
|
|
|
|
if (GetterMethod)
|
|
|
|
CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
|
|
|
|
if (SetterMethod)
|
|
|
|
CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
|
2010-03-12 08:38:38 +08:00
|
|
|
SourceLocation Loc,
|
2012-12-21 03:22:21 +08:00
|
|
|
unsigned &Attributes,
|
2012-06-21 06:57:42 +08:00
|
|
|
bool propertyInPrimaryClass) {
|
2010-03-12 08:38:38 +08:00
|
|
|
// FIXME: Improve the reported location.
|
2011-06-16 07:02:42 +08:00
|
|
|
if (!PDecl || PDecl->isInvalidDecl())
|
2010-04-06 06:39:42 +08:00
|
|
|
return;
|
2013-10-08 01:20:02 +08:00
|
|
|
|
|
|
|
if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_readwrite))
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "readonly" << "readwrite";
|
|
|
|
|
2010-04-06 06:39:42 +08:00
|
|
|
ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
|
2013-10-08 01:20:02 +08:00
|
|
|
QualType PropertyTy = PropertyDecl->getType();
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
// Check for copy or retain on non-object types.
|
2012-12-21 03:22:21 +08:00
|
|
|
if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
|
|
|
|
!PropertyTy->isObjCRetainableType() &&
|
2013-12-19 10:39:40 +08:00
|
|
|
!PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_requires_object)
|
2012-12-21 03:22:21 +08:00
|
|
|
<< (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
|
|
|
|
Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
|
|
|
|
Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
|
2012-02-22 05:48:05 +08:00
|
|
|
PropertyDecl->setInvalidDecl();
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
// Check for more than one of { assign, copy, retain }.
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "copy";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "retain";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "strong";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2012-12-21 03:22:21 +08:00
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "weak";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2013-12-19 10:39:40 +08:00
|
|
|
if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
|
2013-06-26 01:34:50 +08:00
|
|
|
Diag(Loc, diag::warn_iboutletcollection_property_assign);
|
2012-12-21 03:22:21 +08:00
|
|
|
} else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "copy";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "retain";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "strong";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2012-12-21 03:22:21 +08:00
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "weak";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
} else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "copy" << "retain";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "copy" << "strong";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "copy" << "weak";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "retain" << "weak";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2012-12-21 03:22:21 +08:00
|
|
|
else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "strong" << "weak";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2015-06-20 02:25:57 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
|
2015-06-20 02:14:38 +08:00
|
|
|
// 'weak' and 'nonnull' are mutually exclusive.
|
|
|
|
if (auto nullability = PropertyTy->getNullability(Context)) {
|
|
|
|
if (*nullability == NullabilityKind::NonNull)
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "nonnull" << "weak";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
|
2011-10-11 05:53:24 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "atomic" << "nonatomic";
|
2012-12-21 03:22:21 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
|
2011-10-11 05:53:24 +08:00
|
|
|
}
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
// Warn if user supplied no assignment attribute, property is
|
|
|
|
// readwrite, and this is an object type.
|
2015-10-27 12:54:50 +08:00
|
|
|
if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
|
|
|
|
// do nothing
|
|
|
|
} else if (getLangOpts().ObjCAutoRefCount) {
|
|
|
|
// With arc, @property definitions should default to strong when
|
|
|
|
// not specified.
|
|
|
|
PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
|
|
|
|
} else if (PropertyTy->isObjCObjectPointerType()) {
|
2012-01-04 08:31:53 +08:00
|
|
|
bool isAnyClassTy =
|
|
|
|
(PropertyTy->isObjCClassType() ||
|
|
|
|
PropertyTy->isObjCQualifiedClassType());
|
|
|
|
// In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
|
|
|
|
// issue any warning.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
|
2012-01-04 08:31:53 +08:00
|
|
|
;
|
2012-09-18 07:57:35 +08:00
|
|
|
else if (propertyInPrimaryClass) {
|
|
|
|
// Don't issue warning on property with no life time in class
|
|
|
|
// extension as it is inherited from property in primary class.
|
2011-08-20 03:28:44 +08:00
|
|
|
// Skip this warning in gc-only mode.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().getGC() != LangOptions::GCOnly)
|
2011-08-20 03:28:44 +08:00
|
|
|
Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
|
2011-07-12 12:30:16 +08:00
|
|
|
|
2011-08-20 03:28:44 +08:00
|
|
|
// If non-gc code warn that this is likely inappropriate.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().getGC() == LangOptions::NonGC)
|
2011-08-20 03:28:44 +08:00
|
|
|
Diag(Loc, diag::warn_objc_property_default_assign_on_object);
|
2012-01-04 08:31:53 +08:00
|
|
|
}
|
2015-10-27 12:54:50 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
// FIXME: Implement warning dependent on NSCopying being
|
|
|
|
// implemented. See also:
|
|
|
|
// <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
|
|
|
|
// (please trim this list while you are at it).
|
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
|
|
|
|
&&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
|
2012-03-11 15:00:24 +08:00
|
|
|
&& getLangOpts().getGC() == LangOptions::GCOnly
|
2010-03-12 08:46:40 +08:00
|
|
|
&& PropertyTy->isBlockPointerType())
|
|
|
|
Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
|
2012-12-21 03:22:21 +08:00
|
|
|
else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
|
2011-09-15 02:03:46 +08:00
|
|
|
PropertyTy->isBlockPointerType())
|
|
|
|
Diag(Loc, diag::warn_objc_property_retain_of_block);
|
2011-11-02 07:02:16 +08:00
|
|
|
|
2012-12-21 03:22:21 +08:00
|
|
|
if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_setter))
|
2011-11-02 07:02:16 +08:00
|
|
|
Diag(Loc, diag::warn_objc_readonly_property_has_setter);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|