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"
|
2010-08-13 04:07:10 +08:00
|
|
|
#include "clang/Sema/Initialization.h"
|
2010-08-24 15:21:54 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-05-06 05:52:17 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2011-10-07 02:38:18 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2011-11-14 12:52:29 +08:00
|
|
|
#include "clang/AST/ASTMutationListener.h"
|
2012-05-22 01:02:43 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Basic/SourceManager.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;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// Check the internal consistency of a property declaration.
|
|
|
|
static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
|
|
|
|
if (property->isInvalidDecl()) return;
|
|
|
|
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind propertyKind
|
|
|
|
= property->getPropertyAttributes();
|
|
|
|
Qualifiers::ObjCLifetime propertyLifetime
|
|
|
|
= property->getType().getObjCLifetime();
|
|
|
|
|
|
|
|
// Nothing to do if we don't have a lifetime.
|
|
|
|
if (propertyLifetime == Qualifiers::OCL_None) return;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
bool *isOverridingProperty,
|
2010-09-24 05:18:05 +08:00
|
|
|
tok::ObjCKeywordKind MethodImplKind,
|
|
|
|
DeclContext *lexicalDC) {
|
2010-03-12 08:46:40 +08:00
|
|
|
unsigned Attributes = ODS.getPropertyAttributes();
|
2011-06-16 07:02:42 +08:00
|
|
|
TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
|
|
|
|
QualType T = TSI->getType();
|
2012-03-11 15:00:24 +08:00
|
|
|
if ((getLangOpts().getGC() != LangOptions::NonGC &&
|
2011-06-16 07:02:42 +08:00
|
|
|
T.isObjCGCWeak()) ||
|
2012-03-11 15:00:24 +08:00
|
|
|
(getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
T.getObjCLifetime() == Qualifiers::OCL_Weak))
|
|
|
|
Attributes |= ObjCDeclSpec::DQ_PR_weak;
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
|
|
|
|
// default is readwrite!
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_readonly));
|
|
|
|
// property is defaulted to 'assign' if it is readwrite and is
|
|
|
|
// not retain or copy
|
|
|
|
bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
|
|
|
|
(isReadWrite &&
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
|
2011-06-16 07:02:42 +08:00
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_weak)));
|
2011-03-29 07:47:18 +08:00
|
|
|
|
2010-03-12 10:31:10 +08:00
|
|
|
// Proceed with constructing the ObjCPropertDecls.
|
2011-08-22 23:54:49 +08:00
|
|
|
ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
|
2010-03-12 08:46:40 +08:00
|
|
|
if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
|
2010-07-14 06:04:56 +08:00
|
|
|
if (CDecl->IsClassExtension()) {
|
2012-03-01 06:18:55 +08:00
|
|
|
Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
|
2010-07-14 06:04:56 +08:00
|
|
|
FD, GetterSel, SetterSel,
|
|
|
|
isAssign, isReadWrite,
|
|
|
|
Attributes,
|
2011-11-07 02:58:12 +08:00
|
|
|
ODS.getPropertyAttributes(),
|
2010-07-14 06:04:56 +08:00
|
|
|
isOverridingProperty, TSI,
|
|
|
|
MethodImplKind);
|
2011-06-16 07:02:42 +08:00
|
|
|
if (Res) {
|
2012-06-21 06:57:42 +08:00
|
|
|
CheckObjCPropertyAttributes(Res, AtLoc, Attributes, false);
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
2011-06-16 07:02:42 +08:00
|
|
|
checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
|
|
|
|
}
|
2012-07-13 09:06:46 +08:00
|
|
|
ActOnDocumentableDecl(Res);
|
2010-07-14 06:04:56 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2012-03-01 06:18:55 +08:00
|
|
|
ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
|
2011-06-16 07:02:42 +08:00
|
|
|
GetterSel, SetterSel,
|
|
|
|
isAssign, isReadWrite,
|
2011-11-07 02:58:12 +08:00
|
|
|
Attributes,
|
|
|
|
ODS.getPropertyAttributes(),
|
|
|
|
TSI, MethodImplKind);
|
2010-09-24 05:18:05 +08:00
|
|
|
if (lexicalDC)
|
|
|
|
Res->setLexicalDeclContext(lexicalDC);
|
|
|
|
|
2010-03-31 06:40:11 +08:00
|
|
|
// Validate the attributes on the @property.
|
2012-06-21 06:57:42 +08:00
|
|
|
CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
|
|
|
|
(isa<ObjCInterfaceDecl>(ClassDecl) ||
|
|
|
|
isa<ObjCProtocolDecl>(ClassDecl)));
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
2011-06-16 07:02:42 +08:00
|
|
|
checkARCPropertyDecl(*this, Res);
|
|
|
|
|
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
|
|
|
|
makePropertyAttributesAsWritten(unsigned Attributes) {
|
|
|
|
unsigned attributesAsWritten = 0;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_getter)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_setter)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_assign)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_weak)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
|
|
|
|
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (Tok.is(tok::raw_identifier) &&
|
|
|
|
StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
|
|
|
|
Loc = Tok.getLocation();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (Tok.isNot(tok::r_paren));
|
|
|
|
return false;
|
|
|
|
|
2012-05-20 02:17:17 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
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,
|
2010-03-12 10:31:10 +08:00
|
|
|
Selector GetterSel, Selector SetterSel,
|
|
|
|
const bool isAssign,
|
|
|
|
const bool isReadWrite,
|
|
|
|
const unsigned Attributes,
|
2011-11-07 02:58:12 +08:00
|
|
|
const unsigned AttributesAsWritten,
|
2010-03-12 10:31:10 +08:00
|
|
|
bool *isOverridingProperty,
|
2010-06-05 04:50:08 +08:00
|
|
|
TypeSourceInfo *T,
|
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();
|
|
|
|
|
|
|
|
if (CCPrimary)
|
|
|
|
// Check for duplicate declaration of this property in current and
|
|
|
|
// other class extensions.
|
|
|
|
for (const ObjCCategoryDecl *ClsExtDecl =
|
|
|
|
CCPrimary->getFirstClassExtension();
|
|
|
|
ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
|
|
|
|
if (ObjCPropertyDecl *prevDecl =
|
|
|
|
ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
|
|
|
|
Diag(AtLoc, diag::err_duplicate_property);
|
|
|
|
Diag(prevDecl->getLocation(), diag::note_property_declare);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 10:31:10 +08:00
|
|
|
// Create a new ObjCPropertyDecl with the DeclContext being
|
|
|
|
// the class extension.
|
2010-12-11 07:36:33 +08:00
|
|
|
// FIXME. We should really be using CreatePropertyDecl for this.
|
2010-03-12 10:31:10 +08:00
|
|
|
ObjCPropertyDecl *PDecl =
|
|
|
|
ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
|
2012-03-01 06:18:55 +08:00
|
|
|
PropertyId, AtLoc, LParenLoc, T);
|
2011-10-19 03:49:16 +08:00
|
|
|
PDecl->setPropertyAttributesAsWritten(
|
2011-11-07 02:58:12 +08:00
|
|
|
makePropertyAttributesAsWritten(AttributesAsWritten));
|
2010-03-23 07:25:52 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
|
2010-12-11 07:36:33 +08:00
|
|
|
// Set setter/getter selector name. Needed later.
|
|
|
|
PDecl->setGetterName(GetterSel);
|
|
|
|
PDecl->setSetterName(SetterSel);
|
2011-07-15 23:30:21 +08:00
|
|
|
ProcessDeclAttributes(S, PDecl, FD.D);
|
2010-03-12 10:31:10 +08:00
|
|
|
DC->addDecl(PDecl);
|
|
|
|
|
|
|
|
// We need to look in the @interface to see if the @property was
|
|
|
|
// already declared.
|
|
|
|
if (!CCPrimary) {
|
|
|
|
Diag(CDecl->getLocation(), diag::err_continuation_class);
|
|
|
|
*isOverridingProperty = true;
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2010-03-12 10:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the property in continuation class's primary class only.
|
|
|
|
ObjCPropertyDecl *PIDecl =
|
|
|
|
CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
|
|
|
|
|
|
|
|
if (!PIDecl) {
|
|
|
|
// No matching property found in the primary class. Just fall thru
|
|
|
|
// and add property to continuation class's primary class.
|
2012-02-29 01:50:28 +08:00
|
|
|
ObjCPropertyDecl *PrimaryPDecl =
|
2012-03-01 06:18:55 +08:00
|
|
|
CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
|
2010-03-12 10:31:10 +08:00
|
|
|
FD, GetterSel, SetterSel, isAssign, isReadWrite,
|
2011-11-07 02:58:12 +08:00
|
|
|
Attributes,AttributesAsWritten, T, MethodImplKind, DC);
|
2010-03-12 10:31:10 +08:00
|
|
|
|
|
|
|
// A case of continuation class adding a new property in the class. This
|
|
|
|
// is not what it was meant for. However, gcc supports it and so should we.
|
|
|
|
// Make sure setter/getters are declared here.
|
2012-02-29 01:50:28 +08:00
|
|
|
ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
|
2010-09-22 04:52:59 +08:00
|
|
|
/* lexicalDC = */ CDecl);
|
2012-02-29 01:50:28 +08:00
|
|
|
PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
|
|
|
|
PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
|
2011-11-14 12:52:29 +08:00
|
|
|
if (ASTMutationListener *L = Context.getASTMutationListener())
|
2012-02-29 01:50:28 +08:00
|
|
|
L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
|
|
|
|
return PrimaryPDecl;
|
2010-03-12 10:31:10 +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.
|
2012-02-03 02:54:58 +08:00
|
|
|
if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
|
|
|
|
!isa<ObjCObjectPointerType>(PDecl->getType()) ||
|
|
|
|
(!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
|
|
|
|
ConvertedType, IncompatibleObjC))
|
|
|
|
|| IncompatibleObjC) {
|
|
|
|
Diag(AtLoc,
|
|
|
|
diag::err_type_mismatch_continuation_class) << PDecl->getType();
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
2011-09-24 08:56:59 +08:00
|
|
|
}
|
|
|
|
|
2010-03-12 10:31:10 +08:00
|
|
|
// The property 'PIDecl's readonly attribute will be over-ridden
|
|
|
|
// with continuation class's readwrite property attribute!
|
2010-06-23 07:20:40 +08:00
|
|
|
unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
|
2010-03-12 10:31:10 +08:00
|
|
|
if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
|
|
|
|
unsigned retainCopyNonatomic =
|
|
|
|
(ObjCPropertyDecl::OBJC_PR_retain |
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCPropertyDecl::OBJC_PR_strong |
|
2010-03-12 10:31:10 +08:00
|
|
|
ObjCPropertyDecl::OBJC_PR_copy |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_nonatomic);
|
|
|
|
if ((Attributes & retainCopyNonatomic) !=
|
|
|
|
(PIkind & retainCopyNonatomic)) {
|
|
|
|
Diag(AtLoc, diag::warn_property_attr_mismatch);
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
2010-03-18 09:22:36 +08:00
|
|
|
DeclContext *DC = cast<DeclContext>(CCPrimary);
|
|
|
|
if (!ObjCPropertyDecl::findPropertyDecl(DC,
|
|
|
|
PIDecl->getDeclName().getAsIdentifierInfo())) {
|
2010-03-12 10:31:10 +08:00
|
|
|
// Protocol is not in the primary class. Must build one for it.
|
|
|
|
ObjCDeclSpec ProtocolPropertyODS;
|
|
|
|
// FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
|
|
|
|
// and ObjCPropertyDecl::PropertyAttributeKind have identical
|
|
|
|
// values. Should consolidate both into one enum type.
|
|
|
|
ProtocolPropertyODS.
|
|
|
|
setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
|
|
|
|
PIkind);
|
2011-08-22 23:54:49 +08:00
|
|
|
// Must re-establish the context from class extension to primary
|
|
|
|
// class context.
|
2011-08-23 04:15:24 +08:00
|
|
|
ContextRAII SavedContext(*this, CCPrimary);
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *ProtocolPtrTy =
|
2012-03-01 06:18:55 +08:00
|
|
|
ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
|
2010-03-12 10:31:10 +08:00
|
|
|
PIDecl->getGetterName(),
|
|
|
|
PIDecl->getSetterName(),
|
2011-08-22 23:54:49 +08:00
|
|
|
isOverridingProperty,
|
2010-09-24 05:18:05 +08:00
|
|
|
MethodImplKind,
|
|
|
|
/* lexicalDC = */ CDecl);
|
2010-08-21 17:40:31 +08:00
|
|
|
PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2010-03-12 10:31:10 +08:00
|
|
|
PIDecl->makeitReadWriteAttribute();
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain)
|
|
|
|
PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
|
2011-06-16 07:02:42 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong)
|
|
|
|
PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
|
2010-03-12 10:31:10 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
|
|
|
|
PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
|
|
|
|
PIDecl->setSetterName(SetterSel);
|
|
|
|
} else {
|
2010-10-22 02:49:42 +08:00
|
|
|
// 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) &&
|
|
|
|
(PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
|
|
|
|
? diag::err_use_continuation_class_redeclaration_readwrite
|
|
|
|
: diag::err_use_continuation_class;
|
|
|
|
Diag(AtLoc, diag)
|
2010-03-12 10:31:10 +08:00
|
|
|
<< CCPrimary->getDeclName();
|
|
|
|
Diag(PIDecl->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
*isOverridingProperty = true;
|
|
|
|
// Make sure setter decl is synthesized, and added to primary class's list.
|
2010-09-22 02:28:43 +08:00
|
|
|
ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
|
2012-02-29 01:50:28 +08:00
|
|
|
PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
|
|
|
|
PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
|
2011-11-14 12:52:29 +08:00
|
|
|
if (ASTMutationListener *L = Context.getASTMutationListener())
|
|
|
|
L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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,
|
|
|
|
Selector SetterSel,
|
|
|
|
const bool isAssign,
|
|
|
|
const bool isReadWrite,
|
|
|
|
const unsigned Attributes,
|
2011-11-07 02:58:12 +08:00
|
|
|
const unsigned AttributesAsWritten,
|
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-06-05 04:50:08 +08:00
|
|
|
QualType T = TInfo->getType();
|
2010-03-12 08:46:40 +08:00
|
|
|
|
|
|
|
// 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 &&
|
2010-03-12 08:46:40 +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
|
|
|
}
|
2010-05-15 19:32:37 +08:00
|
|
|
if (T->isObjCObjectType())
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
|
|
|
|
|
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(),
|
2012-03-01 06:18:55 +08:00
|
|
|
PropertyId, AtLoc, LParenLoc, TInfo);
|
2010-03-12 10:31:10 +08:00
|
|
|
|
2010-03-16 04:11:46 +08:00
|
|
|
if (ObjCPropertyDecl *prevDecl =
|
|
|
|
ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
|
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.
|
|
|
|
PDecl->setGetterName(GetterSel);
|
|
|
|
PDecl->setSetterName(SetterSel);
|
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
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
|
|
|
|
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_getter)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
|
|
|
|
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_setter)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
|
|
|
|
|
|
|
|
if (isReadWrite)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
|
|
|
|
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
|
|
|
|
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_weak)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
|
|
|
|
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
|
|
|
|
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.
|
2010-03-12 08:46:40 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
|
|
|
|
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'.
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
2011-09-14 02:31:23 +08:00
|
|
|
// These aren't valid lifetimes for object ivars; don't diagnose twice.
|
|
|
|
if (ivarLifetime == Qualifiers::OCL_None ||
|
|
|
|
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:
|
|
|
|
S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
|
|
|
|
<< property->getDeclName()
|
|
|
|
<< ivar->getDeclName()
|
|
|
|
<< ivarLifetime;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
S.Diag(propertyImplLoc, diag::error_weak_property)
|
|
|
|
<< property->getDeclName()
|
|
|
|
<< ivar->getDeclName();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
|
|
|
|
<< 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-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);
|
|
|
|
return;
|
|
|
|
}
|
2010-03-12 08:46:40 +08:00
|
|
|
|
2012-06-21 06:57:42 +08:00
|
|
|
/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
|
|
|
|
/// attribute declared in primary class and attributes overridden in any of its
|
|
|
|
/// class extensions.
|
|
|
|
static void
|
|
|
|
DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
|
|
|
|
ObjCPropertyDecl *property) {
|
|
|
|
unsigned Attributes = property->getPropertyAttributesAsWritten();
|
|
|
|
bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
|
|
|
|
for (const ObjCCategoryDecl *CDecl = ClassDecl->getFirstClassExtension();
|
|
|
|
CDecl; CDecl = CDecl->getNextClassExtension()) {
|
|
|
|
ObjCPropertyDecl *ClassExtProperty = 0;
|
|
|
|
for (ObjCContainerDecl::prop_iterator P = CDecl->prop_begin(),
|
|
|
|
E = CDecl->prop_end(); P != E; ++P) {
|
|
|
|
if ((*P)->getIdentifier() == property->getIdentifier()) {
|
|
|
|
ClassExtProperty = *P;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ClassExtProperty) {
|
2012-06-21 07:18:57 +08:00
|
|
|
warn = false;
|
2012-06-21 06:57:42 +08:00
|
|
|
unsigned classExtPropertyAttr =
|
|
|
|
ClassExtProperty->getPropertyAttributesAsWritten();
|
|
|
|
// We are issuing the warning that we postponed because class extensions
|
|
|
|
// can override readonly->readwrite and 'setter' attributes originally
|
|
|
|
// placed on class's property declaration now make sense in the overridden
|
|
|
|
// property.
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
|
|
|
|
if (!classExtPropertyAttr ||
|
|
|
|
(classExtPropertyAttr & ObjCDeclSpec::DQ_PR_readwrite))
|
|
|
|
continue;
|
|
|
|
warn = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (warn) {
|
|
|
|
unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
|
|
|
|
ObjCDeclSpec::DQ_PR_unsafe_unretained |
|
|
|
|
ObjCDeclSpec::DQ_PR_copy |
|
|
|
|
ObjCDeclSpec::DQ_PR_retain |
|
|
|
|
ObjCDeclSpec::DQ_PR_strong);
|
|
|
|
if (Attributes & setterAttrs) {
|
|
|
|
const char * which =
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_assign) ?
|
|
|
|
"assign" :
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
|
|
|
|
"unsafe_unretained" :
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_copy) ?
|
|
|
|
"copy" :
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_retain) ?
|
|
|
|
"retain" : "strong";
|
|
|
|
|
|
|
|
S.Diag(property->getLocation(),
|
|
|
|
diag::warn_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "readonly" << which;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
SourceLocation PropertyIvarLoc) {
|
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) {
|
|
|
|
Diag(AtLoc, diag::error_missing_property_context);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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();
|
2010-03-12 08:46:40 +08:00
|
|
|
ObjCPropertyDecl *property = 0;
|
|
|
|
ObjCInterfaceDecl* IDecl = 0;
|
|
|
|
// Find the class or category class where this property must have
|
|
|
|
// a declaration.
|
|
|
|
ObjCImplementationDecl *IC = 0;
|
|
|
|
ObjCCategoryImplDecl* CatImplClass = 0;
|
|
|
|
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
|
|
|
|
property = IDecl->FindPropertyDeclaration(PropertyId);
|
|
|
|
if (!property) {
|
|
|
|
Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
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()) {
|
|
|
|
Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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()) {
|
|
|
|
Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
|
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
2012-05-22 01:02:43 +08:00
|
|
|
SourceLocation readonlyLoc;
|
2012-05-22 01:10:28 +08:00
|
|
|
if (LocPropertyAttribute(Context, "readonly",
|
2012-05-22 01:02:43 +08:00
|
|
|
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-20 02:17:17 +08:00
|
|
|
}
|
2012-06-21 06:57:42 +08:00
|
|
|
|
|
|
|
DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
|
2012-05-20 02:17:17 +08:00
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
} else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
|
|
|
|
if (Synthesize) {
|
|
|
|
Diag(AtLoc, diag::error_synthesize_category_decl);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
IDecl = CatImplClass->getClassInterface();
|
|
|
|
if (!IDecl) {
|
|
|
|
Diag(AtLoc, diag::error_missing_property_interface);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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)
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2010-03-12 08:46:40 +08:00
|
|
|
// Look for this property declaration in @implementation's category
|
|
|
|
property = Category->FindPropertyDeclaration(PropertyId);
|
|
|
|
if (!property) {
|
|
|
|
Diag(PropertyLoc, diag::error_bad_category_property_decl)
|
|
|
|
<< Category->getDeclName();
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Diag(AtLoc, diag::error_bad_property_context);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
|
|
|
ObjCIvarDecl *Ivar = 0;
|
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
|
|
|
|
|
|
|
// Add GC __weak to the ivar type if the property is weak.
|
|
|
|
if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
|
2012-03-11 15:00:24 +08:00
|
|
|
getLangOpts().getGC() != LangOptions::NonGC) {
|
|
|
|
assert(!getLangOpts().ObjCAutoRefCount);
|
2011-09-14 02:31:23 +08:00
|
|
|
if (PropertyIvarType.isObjCGCStrong()) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
|
2011-09-14 02:31:23 +08:00
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
} else {
|
|
|
|
PropertyIvarType =
|
|
|
|
Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
|
2011-09-08 00:24:21 +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.
|
|
|
|
ObjCInterfaceDecl *ClassDeclared=0;
|
|
|
|
ObjCIvarDecl *originalIvar =
|
|
|
|
IDecl->lookupInstanceVariable(property->getIdentifier(),
|
|
|
|
ClassDeclared);
|
|
|
|
if (originalIvar) {
|
|
|
|
Diag(PropertyDiagLoc,
|
|
|
|
diag::warn_autosynthesis_property_ivar_match)
|
2012-06-30 03:05:11 +08:00
|
|
|
<< PropertyId << (Ivar == 0) << 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.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
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.
|
2011-07-27 05:48:26 +08:00
|
|
|
if (!property->hasWrittenStorageAttribute() &&
|
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
|
|
|
if (lifetime == Qualifiers::OCL_Weak) {
|
|
|
|
bool err = false;
|
|
|
|
if (const ObjCObjectPointerType *ObjT =
|
|
|
|
PropertyIvarType->getAs<ObjCObjectPointerType>())
|
|
|
|
if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
|
2011-12-10 03:55:11 +08:00
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
err = true;
|
|
|
|
}
|
2012-08-21 10:47:43 +08:00
|
|
|
if (!err && !getLangOpts().ObjCARCWeak) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
|
2011-12-10 03:55:11 +08:00
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
|
2012-03-11 15:00:24 +08:00
|
|
|
!getLangOpts().ObjCAutoRefCount &&
|
|
|
|
getLangOpts().getGC() == LangOptions::NonGC) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(property->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
|
2011-03-08 16:55:46 +08:00
|
|
|
Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
|
2012-02-29 01:50:39 +08:00
|
|
|
PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
|
2011-03-29 07:47:18 +08:00
|
|
|
PropertyIvarType, /*Dinfo=*/0,
|
2010-12-16 07:29:04 +08:00
|
|
|
ObjCIvarDecl::Private,
|
2010-07-17 08:59:30 +08:00
|
|
|
(Expr *)0, true);
|
2012-05-02 06:26:06 +08:00
|
|
|
if (CompleteTypeErr)
|
|
|
|
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
|
|
|
property->setPropertyIvarDecl(Ivar);
|
|
|
|
|
2012-06-20 14:18:46 +08:00
|
|
|
if (getLangOpts().ObjCRuntime.isFragile())
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
|
|
|
|
<< 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)) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_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.
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
compat = false;
|
2011-03-29 07:47:18 +08:00
|
|
|
if (isa<ObjCObjectPointerType>(PropertyIvarType)
|
2011-06-16 07:02:42 +08:00
|
|
|
&& isa<ObjCObjectPointerType>(IvarType))
|
2010-05-19 07:04:17 +08:00
|
|
|
compat =
|
|
|
|
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) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_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()) {
|
|
|
|
Diag(PropertyDiagLoc, diag::error_property_ivar_type)
|
|
|
|
<< 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)) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_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) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_strong_property)
|
2010-03-12 08:46:40 +08:00
|
|
|
<< property->getDeclName() << Ivar->getDeclName();
|
|
|
|
// Fall thru - see previous comment
|
|
|
|
}
|
|
|
|
}
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
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
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_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.
|
|
|
|
ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
|
|
|
|
DeclRefExpr *SelfExpr =
|
2012-03-10 17:33:50 +08:00
|
|
|
new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
|
2010-11-18 14:31:45 +08:00
|
|
|
VK_RValue, SourceLocation());
|
2010-05-06 05:52:17 +08:00
|
|
|
Expr *IvarRefExpr =
|
|
|
|
new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
|
|
|
|
SelfExpr, true, true);
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Res =
|
2010-05-06 05:52:17 +08:00
|
|
|
PerformCopyInitialization(InitializedEntity::InitializeResult(
|
2010-05-15 08:13:29 +08:00
|
|
|
SourceLocation(),
|
|
|
|
getterMethod->getResultType(),
|
|
|
|
/*NRVO=*/false),
|
2010-05-06 05:52:17 +08:00
|
|
|
SourceLocation(),
|
|
|
|
Owned(IvarRefExpr));
|
|
|
|
if (!Res.isInvalid()) {
|
|
|
|
Expr *ResExpr = Res.takeAs<Expr>();
|
|
|
|
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);
|
|
|
|
}
|
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.
|
|
|
|
ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
|
|
|
|
DeclRefExpr *SelfExpr =
|
2012-03-10 17:33:50 +08:00
|
|
|
new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
|
2010-11-18 14:31:45 +08:00
|
|
|
VK_RValue, SourceLocation());
|
2010-05-06 05:52:17 +08:00
|
|
|
Expr *lhs =
|
|
|
|
new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
|
|
|
|
SelfExpr, true, true);
|
|
|
|
ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
|
|
|
|
ParmVarDecl *Param = (*P);
|
2011-10-26 01:37:35 +08:00
|
|
|
QualType T = Param->getType().getNonReferenceType();
|
2012-03-10 17:33:50 +08:00
|
|
|
Expr *rhs = new (Context) DeclRefExpr(Param, false, T,
|
2010-11-18 14:31:45 +08:00
|
|
|
VK_LValue, SourceLocation());
|
2010-10-15 05:30:10 +08:00
|
|
|
ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
|
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) {
|
|
|
|
Expr *callExpr = Res.takeAs<Expr>();
|
|
|
|
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()) {
|
|
|
|
Diag(PropertyLoc,
|
|
|
|
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
|
|
|
}
|
2010-05-06 05:52:17 +08:00
|
|
|
PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (IC) {
|
|
|
|
if (Synthesize)
|
|
|
|
if (ObjCPropertyImplDecl *PPIDecl =
|
|
|
|
IC->FindPropertyImplIvarDecl(PropertyIvar)) {
|
|
|
|
Diag(PropertyLoc, diag::error_duplicate_ivar_use)
|
|
|
|
<< PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
|
|
|
|
<< PropertyIvar;
|
|
|
|
Diag(PPIDecl->getLocation(), diag::note_previous_use);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCPropertyImplDecl *PPIDecl
|
|
|
|
= IC->FindPropertyImplDecl(PropertyId)) {
|
|
|
|
Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
|
|
|
|
Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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.
|
2011-01-21 07:34:25 +08:00
|
|
|
ObjCInterfaceDecl *ClassDeclared=0;
|
2010-07-17 08:59:30 +08:00
|
|
|
ObjCIvarDecl *Ivar = 0;
|
|
|
|
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)) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_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 =
|
|
|
|
CatImplClass->FindPropertyImplDecl(PropertyId)) {
|
2012-05-02 06:26:06 +08:00
|
|
|
Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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,
|
|
|
|
const IdentifierInfo *inheritedName) {
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind CAttr =
|
|
|
|
Property->getPropertyAttributes();
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind SAttr =
|
|
|
|
SuperProperty->getPropertyAttributes();
|
|
|
|
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))
|
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< Property->getDeclName() << "copy" << inheritedName;
|
2011-10-09 01:45:33 +08:00
|
|
|
else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
|
2011-06-16 07:02:42 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
|
|
|
if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
|
|
|
|
!= (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
|
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< Property->getDeclName() << "atomic" << inheritedName;
|
|
|
|
if (Property->getSetterName() != SuperProperty->getSetterName())
|
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< Property->getDeclName() << "setter" << inheritedName;
|
|
|
|
if (Property->getGetterName() != SuperProperty->getGetterName())
|
|
|
|
Diag(Property->getLocation(), diag::warn_property_attribute)
|
|
|
|
<< Property->getDeclName() << "getter" << inheritedName;
|
|
|
|
|
|
|
|
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;
|
|
|
|
QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
|
|
|
|
QualType PropertyIvarType = property->getType().getNonReferenceType();
|
|
|
|
bool compat = Context.hasSameType(PropertyIvarType, GetterType);
|
|
|
|
if (!compat) {
|
|
|
|
if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
|
|
|
|
isa<ObjCObjectPointerType>(GetterType))
|
|
|
|
compat =
|
|
|
|
Context.canAssignObjCInterfaces(
|
2012-05-30 03:56:01 +08:00
|
|
|
GetterType->getAs<ObjCObjectPointerType>(),
|
|
|
|
PropertyIvarType->getAs<ObjCObjectPointerType>());
|
|
|
|
else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
|
2012-05-16 06:37:04 +08:00
|
|
|
!= Compatible) {
|
|
|
|
Diag(Loc, diag::error_property_accessor_type)
|
|
|
|
<< property->getDeclName() << PropertyIvarType
|
|
|
|
<< GetterMethod->getSelector() << GetterType;
|
|
|
|
Diag(GetterMethod->getLocation(), diag::note_declared_at);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
compat = true;
|
|
|
|
QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ComparePropertiesInBaseAndSuper - This routine compares property
|
|
|
|
/// declarations in base and its super class, if any, and issues
|
2011-04-15 13:22:18 +08:00
|
|
|
/// diagnostics in a variety of inconsistent situations.
|
2010-03-12 08:38:38 +08:00
|
|
|
///
|
|
|
|
void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
|
|
|
|
ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
|
|
|
|
if (!SDecl)
|
|
|
|
return;
|
|
|
|
// FIXME: O(N^2)
|
|
|
|
for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
|
|
|
|
E = SDecl->prop_end(); S != E; ++S) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *SuperPDecl = *S;
|
2010-03-12 08:38:38 +08:00
|
|
|
// Does property in super class has declaration in current class?
|
|
|
|
for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
|
|
|
|
E = IDecl->prop_end(); I != E; ++I) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *PDecl = *I;
|
2010-03-12 08:38:38 +08:00
|
|
|
if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
|
|
|
|
DiagnosePropertyMismatch(PDecl, SuperPDecl,
|
|
|
|
SDecl->getIdentifier());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
|
|
|
|
/// of properties declared in a protocol and compares their attribute against
|
|
|
|
/// the same property declared in the class or category.
|
|
|
|
void
|
|
|
|
Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
|
|
|
|
ObjCProtocolDecl *PDecl) {
|
|
|
|
ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
|
|
|
|
if (!IDecl) {
|
|
|
|
// Category
|
|
|
|
ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
|
|
|
|
assert (CatDecl && "MatchOneProtocolPropertiesInClass");
|
|
|
|
if (!CatDecl->IsClassExtension())
|
|
|
|
for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
|
|
|
|
E = PDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Pr = *P;
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCCategoryDecl::prop_iterator CP, CE;
|
|
|
|
// Is this property already in category's list of properties?
|
2010-03-12 08:49:00 +08:00
|
|
|
for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
|
2012-04-30 10:36:29 +08:00
|
|
|
if (CP->getIdentifier() == Pr->getIdentifier())
|
2010-03-12 08:38:38 +08:00
|
|
|
break;
|
|
|
|
if (CP != CE)
|
|
|
|
// Property protocol already exist in class. Diagnose any mismatch.
|
2012-06-07 04:45:41 +08:00
|
|
|
DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
|
|
|
|
E = PDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Pr = *P;
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCInterfaceDecl::prop_iterator CP, CE;
|
|
|
|
// Is this property already in class's list of properties?
|
|
|
|
for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
|
2012-04-30 10:36:29 +08:00
|
|
|
if (CP->getIdentifier() == Pr->getIdentifier())
|
2010-03-12 08:38:38 +08:00
|
|
|
break;
|
|
|
|
if (CP != CE)
|
|
|
|
// Property protocol already exist in class. Diagnose any mismatch.
|
2012-06-07 04:45:41 +08:00
|
|
|
DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CompareProperties - This routine compares properties
|
|
|
|
/// declared in 'ClassOrProtocol' objects (which can be a class or an
|
|
|
|
/// inherited protocol with the list of properties for class/category 'CDecl'
|
|
|
|
///
|
2010-08-21 17:40:31 +08:00
|
|
|
void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
|
|
|
|
Decl *ClassDecl = ClassOrProtocol;
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
|
|
|
|
|
|
|
|
if (!IDecl) {
|
|
|
|
// Category
|
|
|
|
ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
|
|
|
|
assert (CatDecl && "CompareProperties");
|
|
|
|
if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
|
|
|
|
for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
|
|
|
|
E = MDecl->protocol_end(); P != E; ++P)
|
|
|
|
// Match properties of category with those of protocol (*P)
|
|
|
|
MatchOneProtocolPropertiesInClass(CatDecl, *P);
|
|
|
|
|
|
|
|
// Go thru the list of protocols for this category and recursively match
|
|
|
|
// their properties with those in the category.
|
|
|
|
for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
|
|
|
|
E = CatDecl->protocol_end(); P != E; ++P)
|
2010-08-21 17:40:31 +08:00
|
|
|
CompareProperties(CatDecl, *P);
|
2010-03-12 08:38:38 +08:00
|
|
|
} else {
|
|
|
|
ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
|
|
|
|
E = MD->protocol_end(); P != E; ++P)
|
|
|
|
MatchOneProtocolPropertiesInClass(CatDecl, *P);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
P = MDecl->all_referenced_protocol_begin(),
|
|
|
|
E = MDecl->all_referenced_protocol_end(); P != E; ++P)
|
2010-03-12 08:38:38 +08:00
|
|
|
// Match properties of class IDecl with those of protocol (*P).
|
|
|
|
MatchOneProtocolPropertiesInClass(IDecl, *P);
|
|
|
|
|
|
|
|
// Go thru the list of protocols for this class and recursively match
|
|
|
|
// their properties with those declared in the class.
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
P = IDecl->all_referenced_protocol_begin(),
|
|
|
|
E = IDecl->all_referenced_protocol_end(); P != E; ++P)
|
2010-08-21 17:40:31 +08:00
|
|
|
CompareProperties(IDecl, *P);
|
2010-03-12 08:38:38 +08:00
|
|
|
} else {
|
|
|
|
ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
|
|
|
|
E = MD->protocol_end(); P != E; ++P)
|
|
|
|
MatchOneProtocolPropertiesInClass(IDecl, *P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isPropertyReadonly - Return true if property is readonly, by searching
|
|
|
|
/// for the property in the class and in its categories and implementations
|
|
|
|
///
|
|
|
|
bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
|
|
|
|
ObjCInterfaceDecl *IDecl) {
|
|
|
|
// by far the most common case.
|
|
|
|
if (!PDecl->isReadOnly())
|
|
|
|
return false;
|
|
|
|
// Even if property is ready only, if interface has a user defined setter,
|
|
|
|
// it is not considered read only.
|
|
|
|
if (IDecl->getInstanceMethod(PDecl->getSetterName()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Main class has the property as 'readonly'. Must search
|
|
|
|
// through the category list to see if the property's
|
|
|
|
// attribute has been over-ridden to 'readwrite'.
|
|
|
|
for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
|
|
|
|
Category; Category = Category->getNextClassCategory()) {
|
|
|
|
// Even if property is ready only, if a category has a user defined setter,
|
|
|
|
// it is not considered read only.
|
|
|
|
if (Category->getInstanceMethod(PDecl->getSetterName()))
|
|
|
|
return false;
|
|
|
|
ObjCPropertyDecl *P =
|
|
|
|
Category->FindPropertyDeclaration(PDecl->getIdentifier());
|
|
|
|
if (P && !P->isReadOnly())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also, check for definition of a setter method in the implementation if
|
|
|
|
// all else failed.
|
|
|
|
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
|
|
|
|
if (ObjCImplementationDecl *IMD =
|
|
|
|
dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
|
|
|
|
if (IMD->getInstanceMethod(PDecl->getSetterName()))
|
|
|
|
return false;
|
|
|
|
} else if (ObjCCategoryImplDecl *CIMD =
|
|
|
|
dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
|
|
|
|
if (CIMD->getInstanceMethod(PDecl->getSetterName()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Lastly, look through the implementation (if one is in scope).
|
|
|
|
if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
|
|
|
|
if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
|
|
|
|
return false;
|
|
|
|
// If all fails, look at the super class.
|
|
|
|
if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
|
|
|
|
return isPropertyReadonly(PDecl, SIDecl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CollectImmediateProperties - This routine collects all properties in
|
|
|
|
/// the class and its conforming protocols; but not those it its super class.
|
|
|
|
void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
|
2010-06-30 02:12:32 +08:00
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
|
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
|
2010-03-12 08:38:38 +08:00
|
|
|
if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
|
|
|
|
for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
|
|
|
|
E = IDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Prop = *P;
|
2010-03-12 08:38:38 +08:00
|
|
|
PropMap[Prop->getIdentifier()] = Prop;
|
|
|
|
}
|
|
|
|
// scan through class's protocols.
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
PI = IDecl->all_referenced_protocol_begin(),
|
|
|
|
E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
|
2010-06-30 02:12:32 +08:00
|
|
|
CollectImmediateProperties((*PI), PropMap, SuperPropMap);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
|
|
|
|
if (!CATDecl->IsClassExtension())
|
|
|
|
for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
|
|
|
|
E = CATDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Prop = *P;
|
2010-03-12 08:38:38 +08:00
|
|
|
PropMap[Prop->getIdentifier()] = Prop;
|
|
|
|
}
|
|
|
|
// scan through class's protocols.
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
|
2010-03-12 08:38:38 +08:00
|
|
|
E = CATDecl->protocol_end(); PI != E; ++PI)
|
2010-06-30 02:12:32 +08:00
|
|
|
CollectImmediateProperties((*PI), PropMap, SuperPropMap);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
|
|
|
|
for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
|
|
|
|
E = PDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Prop = *P;
|
2010-06-30 02:12:32 +08:00
|
|
|
ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
|
|
|
|
// 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()) {
|
2010-06-30 02:12:32 +08:00
|
|
|
ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
|
|
|
|
if (!PropEntry)
|
|
|
|
PropEntry = Prop;
|
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
// scan through protocol's protocols.
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
|
|
|
|
E = PDecl->protocol_end(); PI != E; ++PI)
|
2010-06-30 02:12:32 +08:00
|
|
|
CollectImmediateProperties((*PI), PropMap, SuperPropMap);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-15 02:35:57 +08:00
|
|
|
/// CollectClassPropertyImplementations - This routine collects list of
|
|
|
|
/// properties to be implemented in the class. This includes, class's
|
|
|
|
/// and its conforming protocols' properties.
|
|
|
|
static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
|
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
|
|
|
|
if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
|
|
|
|
for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
|
|
|
|
E = IDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Prop = *P;
|
2010-05-15 02:35:57 +08:00
|
|
|
PropMap[Prop->getIdentifier()] = Prop;
|
|
|
|
}
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
PI = IDecl->all_referenced_protocol_begin(),
|
|
|
|
E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
|
2010-05-15 02:35:57 +08:00
|
|
|
CollectClassPropertyImplementations((*PI), PropMap);
|
|
|
|
}
|
|
|
|
else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
|
|
|
|
for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
|
|
|
|
E = PDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Prop = *P;
|
2012-02-24 02:21:25 +08:00
|
|
|
if (!PropMap.count(Prop->getIdentifier()))
|
|
|
|
PropMap[Prop->getIdentifier()] = Prop;
|
2010-05-15 02:35:57 +08:00
|
|
|
}
|
|
|
|
// scan through protocol's protocols.
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
|
|
|
|
E = PDecl->protocol_end(); PI != E; ++PI)
|
|
|
|
CollectClassPropertyImplementations((*PI), PropMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
|
|
|
|
if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
|
|
|
|
while (SDecl) {
|
|
|
|
CollectClassPropertyImplementations(SDecl, PropMap);
|
|
|
|
SDecl = SDecl->getSuperClass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
/// LookupPropertyDecl - Looks up a property in the current class and all
|
|
|
|
/// its protocols.
|
|
|
|
ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
|
|
|
|
IdentifierInfo *II) {
|
|
|
|
if (const ObjCInterfaceDecl *IDecl =
|
|
|
|
dyn_cast<ObjCInterfaceDecl>(CDecl)) {
|
|
|
|
for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
|
|
|
|
E = IDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Prop = *P;
|
2010-03-12 08:38:38 +08:00
|
|
|
if (Prop->getIdentifier() == II)
|
|
|
|
return Prop;
|
|
|
|
}
|
|
|
|
// scan through class's protocols.
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
PI = IDecl->all_referenced_protocol_begin(),
|
|
|
|
E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
|
|
|
|
if (Prop)
|
|
|
|
return Prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (const ObjCProtocolDecl *PDecl =
|
|
|
|
dyn_cast<ObjCProtocolDecl>(CDecl)) {
|
|
|
|
for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
|
|
|
|
E = PDecl->prop_end(); P != E; ++P) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Prop = *P;
|
2010-03-12 08:38:38 +08:00
|
|
|
if (Prop->getIdentifier() == II)
|
|
|
|
return Prop;
|
|
|
|
}
|
|
|
|
// scan through protocol's protocols.
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
|
|
|
|
E = PDecl->protocol_end(); PI != E; ++PI) {
|
|
|
|
ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
|
|
|
|
if (Prop)
|
|
|
|
return Prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-28 07:39:40 +08:00
|
|
|
static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
|
|
|
|
ASTContext &Ctx) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> ivarName;
|
2011-09-28 07:39:40 +08:00
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream os(ivarName);
|
|
|
|
os << '_' << Prop->getIdentifier()->getName();
|
|
|
|
}
|
|
|
|
return &Ctx.Idents.get(ivarName.str());
|
|
|
|
}
|
|
|
|
|
2012-06-15 15:13:21 +08:00
|
|
|
/// \brief Default synthesizes all properties which must be synthesized
|
|
|
|
/// in class's \@implementation.
|
2011-09-28 07:39:40 +08:00
|
|
|
void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
|
|
|
|
ObjCInterfaceDecl *IDecl) {
|
2010-05-15 02:35:57 +08:00
|
|
|
|
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
|
|
|
|
CollectClassPropertyImplementations(IDecl, PropMap);
|
|
|
|
if (PropMap.empty())
|
|
|
|
return;
|
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
|
|
|
|
CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
|
|
|
|
|
|
|
|
for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
|
|
|
|
P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
|
|
|
|
ObjCPropertyDecl *Prop = P->second;
|
|
|
|
// If property to be implemented in the super class, ignore.
|
|
|
|
if (SuperPropMap[Prop->getIdentifier()])
|
|
|
|
continue;
|
|
|
|
// Is there a matching propery synthesize/dynamic?
|
|
|
|
if (Prop->isInvalidDecl() ||
|
|
|
|
Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
|
|
|
|
IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
|
|
|
|
continue;
|
2010-07-15 02:11:52 +08:00
|
|
|
// Property may have been synthesized by user.
|
|
|
|
if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
|
|
|
|
continue;
|
2010-08-25 08:31:58 +08:00
|
|
|
if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
|
|
|
|
if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
|
|
|
|
continue;
|
|
|
|
if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-15 09:03:18 +08:00
|
|
|
if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
|
|
|
|
// We won't auto-synthesize properties declared in protocols.
|
|
|
|
Diag(IMPDecl->getLocation(),
|
|
|
|
diag::warn_auto_synthesizing_protocol_property);
|
|
|
|
Diag(Prop->getLocation(), diag::note_property_declare);
|
|
|
|
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(),
|
|
|
|
/* ivar = */ getDefaultSynthIvarName(Prop, Context),
|
2012-06-08 10:16:11 +08:00
|
|
|
Prop->getLocation()));
|
2012-05-04 00:43:30 +08:00
|
|
|
if (PIDecl) {
|
|
|
|
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
|
|
|
|
2011-09-01 06:24:06 +08:00
|
|
|
void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
|
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())
|
2012-01-04 03:46:00 +08:00
|
|
|
DefaultSynthesizeProperties(S, IC, IDecl);
|
2011-09-01 06:24:06 +08:00
|
|
|
}
|
|
|
|
|
2010-05-06 05:52:17 +08:00
|
|
|
void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCContainerDecl *CDecl,
|
2012-05-27 21:28:52 +08:00
|
|
|
const SelectorSet &InsMap) {
|
2010-06-30 02:12:32 +08:00
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
|
|
|
|
if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
|
|
|
|
CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
|
2010-06-30 02:12:32 +08:00
|
|
|
CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
|
2010-03-12 08:38:38 +08:00
|
|
|
if (PropMap.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
|
|
|
|
for (ObjCImplDecl::propimpl_iterator
|
|
|
|
I = IMPDecl->propimpl_begin(),
|
|
|
|
EI = IMPDecl->propimpl_end(); I != EI; ++I)
|
2012-04-30 10:36:29 +08:00
|
|
|
PropImplMap.insert(I->getPropertyDecl());
|
2010-03-12 08:38:38 +08:00
|
|
|
|
|
|
|
for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
|
|
|
|
P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
|
|
|
|
ObjCPropertyDecl *Prop = P->second;
|
|
|
|
// Is there a matching propery synthesize/dynamic?
|
|
|
|
if (Prop->isInvalidDecl() ||
|
|
|
|
Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
|
2011-06-25 04:31:37 +08:00
|
|
|
PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
|
2010-03-12 08:38:38 +08:00
|
|
|
continue;
|
|
|
|
if (!InsMap.count(Prop->getGetterName())) {
|
2011-08-28 05:55:47 +08:00
|
|
|
Diag(IMPDecl->getLocation(),
|
2010-03-12 08:38:38 +08:00
|
|
|
isa<ObjCCategoryDecl>(CDecl) ?
|
|
|
|
diag::warn_setter_getter_impl_required_in_category :
|
|
|
|
diag::warn_setter_getter_impl_required)
|
|
|
|
<< Prop->getDeclName() << Prop->getGetterName();
|
2011-08-28 05:55:47 +08:00
|
|
|
Diag(Prop->getLocation(),
|
|
|
|
diag::note_property_declare);
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
|
2012-01-05 07:16:13 +08:00
|
|
|
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
|
2012-01-06 06:47:47 +08:00
|
|
|
if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
|
2012-01-05 07:16:13 +08:00
|
|
|
Diag(RID->getLocation(), diag::note_suppressed_class_declare);
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
|
2011-08-28 05:55:47 +08:00
|
|
|
Diag(IMPDecl->getLocation(),
|
2010-03-12 08:38:38 +08:00
|
|
|
isa<ObjCCategoryDecl>(CDecl) ?
|
|
|
|
diag::warn_setter_getter_impl_required_in_category :
|
|
|
|
diag::warn_setter_getter_impl_required)
|
|
|
|
<< Prop->getDeclName() << Prop->getSetterName();
|
2011-08-28 05:55:47 +08:00
|
|
|
Diag(Prop->getLocation(),
|
|
|
|
diag::note_property_declare);
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
|
2012-01-05 07:16:13 +08:00
|
|
|
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
|
2012-01-06 06:47:47 +08:00
|
|
|
if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
|
2012-01-05 07:16:13 +08:00
|
|
|
Diag(RID->getLocation(), diag::note_suppressed_class_declare);
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
|
|
|
|
ObjCContainerDecl* IDecl) {
|
|
|
|
// 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;
|
|
|
|
for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
|
|
|
|
E = IDecl->prop_end();
|
|
|
|
I != E; ++I) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Property = *I;
|
2011-02-01 05:34:11 +08:00
|
|
|
ObjCMethodDecl *GetterMethod = 0;
|
|
|
|
ObjCMethodDecl *SetterMethod = 0;
|
|
|
|
bool LookedUpGetterSetter = false;
|
|
|
|
|
2010-03-12 08:38:38 +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)) {
|
2011-02-01 05:34:11 +08:00
|
|
|
GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
|
|
|
|
SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
|
|
|
|
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.
|
|
|
|
if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
|
|
|
|
!(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
|
|
|
|
continue;
|
|
|
|
if (const ObjCPropertyImplDecl *PIDecl
|
|
|
|
= IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
|
|
|
|
if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
2011-02-01 05:34:11 +08:00
|
|
|
if (!LookedUpGetterSetter) {
|
|
|
|
GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
|
|
|
|
SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
|
|
|
|
LookedUpGetterSetter = true;
|
|
|
|
}
|
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)
|
2011-10-07 07:47:58 +08:00
|
|
|
<< Property->getIdentifier() << (GetterMethod != 0)
|
|
|
|
<< (SetterMethod != 0);
|
2012-03-01 06:18:55 +08:00
|
|
|
// fixit stuff.
|
|
|
|
if (!AttributesAsWritten) {
|
|
|
|
if (Property->getLParenLoc().isValid()) {
|
|
|
|
// @property () ... case.
|
|
|
|
SourceRange PropSourceRange(Property->getAtLoc(),
|
|
|
|
Property->getLParenLoc());
|
|
|
|
Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
|
|
|
|
FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//@property id etc.
|
|
|
|
SourceLocation endLoc =
|
|
|
|
Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
|
|
|
|
endLoc = endLoc.getLocWithOffset(-1);
|
|
|
|
SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
|
|
|
|
Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
|
|
|
|
FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
|
|
|
|
// @property () ... case.
|
|
|
|
SourceLocation endLoc = Property->getLParenLoc();
|
|
|
|
SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
|
|
|
|
Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
|
|
|
|
FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (ObjCImplementationDecl::propimpl_iterator
|
|
|
|
i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyImplDecl *PID = *i;
|
2011-06-16 07:02:42 +08:00
|
|
|
if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
2011-06-25 08:17:46 +08:00
|
|
|
if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
|
|
|
|
!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)
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(PID->getLocation(), diag::err_ownin_getter_rule);
|
|
|
|
else
|
2011-10-13 02:03:37 +08:00
|
|
|
Diag(PID->getLocation(), diag::warn_owning_getter_rule);
|
2011-06-16 07:02:42 +08:00
|
|
|
Diag(PD->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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?
|
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
|
|
|
for (Decl::attr_iterator A = Property->attr_begin(),
|
|
|
|
AEnd = Property->attr_end();
|
|
|
|
A != AEnd; ++A) {
|
|
|
|
if (isa<DeprecatedAttr>(*A) ||
|
|
|
|
isa<UnavailableAttr>(*A) ||
|
|
|
|
isa<AvailabilityAttr>(*A))
|
|
|
|
PropertyMethod->addAttr((*A)->clone(S.Context));
|
|
|
|
}
|
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
|
|
|
|
/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
|
|
|
|
/// methods is the "right" thing to do.
|
|
|
|
void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
|
2010-09-22 02:28:43 +08:00
|
|
|
ObjCContainerDecl *CD,
|
|
|
|
ObjCPropertyDecl *redeclaredProperty,
|
|
|
|
ObjCContainerDecl *lexicalDC) {
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
ObjCMethodDecl *GetterMethod, *SetterMethod;
|
|
|
|
|
|
|
|
GetterMethod = CD->getInstanceMethod(property->getGetterName());
|
|
|
|
SetterMethod = CD->getInstanceMethod(property->getSetterName());
|
|
|
|
DiagnosePropertyAccessorMismatch(property, GetterMethod,
|
|
|
|
property->getLocation());
|
|
|
|
|
|
|
|
if (SetterMethod) {
|
|
|
|
ObjCPropertyDecl::PropertyAttributeKind CAttr =
|
|
|
|
property->getPropertyAttributes();
|
|
|
|
if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
|
|
|
|
Context.getCanonicalType(SetterMethod->getResultType()) !=
|
|
|
|
Context.VoidTy)
|
|
|
|
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) {
|
|
|
|
// No instance method of same name as property getter name was found.
|
|
|
|
// Declare a getter method and add it to the list of methods
|
|
|
|
// for this class.
|
2010-09-22 04:52:59 +08:00
|
|
|
SourceLocation Loc = redeclaredProperty ?
|
|
|
|
redeclaredProperty->getLocation() :
|
|
|
|
property->getLocation();
|
|
|
|
|
|
|
|
GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
|
|
|
|
property->getGetterName(),
|
2011-08-18 03:25:08 +08:00
|
|
|
property->getType(), 0, CD, /*isInstance=*/true,
|
|
|
|
/*isVariadic=*/false, /*isSynthesized=*/true,
|
|
|
|
/*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);
|
|
|
|
|
2010-05-19 05:09:07 +08:00
|
|
|
// FIXME: Eventually this shouldn't be needed, as the lexical context
|
|
|
|
// and the real context should be the same.
|
2010-09-22 04:52:59 +08:00
|
|
|
if (lexicalDC)
|
2010-05-19 05:09:07 +08:00
|
|
|
GetterMethod->setLexicalDeclContext(lexicalDC);
|
2011-06-25 08:17:46 +08:00
|
|
|
if (property->hasAttr<NSReturnsNotRetainedAttr>())
|
|
|
|
GetterMethod->addAttr(
|
|
|
|
::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
|
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
|
|
|
|
GetterMethod->setSynthesized(true);
|
|
|
|
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) {
|
|
|
|
// No instance method of same name as property setter name was found.
|
|
|
|
// Declare a setter method and add it to the list of methods
|
|
|
|
// for this class.
|
2010-09-22 02:28:43 +08:00
|
|
|
SourceLocation Loc = redeclaredProperty ?
|
|
|
|
redeclaredProperty->getLocation() :
|
|
|
|
property->getLocation();
|
|
|
|
|
|
|
|
SetterMethod =
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethodDecl::Create(Context, Loc, Loc,
|
2010-09-22 02:28:43 +08:00
|
|
|
property->getSetterName(), Context.VoidTy, 0,
|
2011-08-18 03:25:08 +08:00
|
|
|
CD, /*isInstance=*/true, /*isVariadic=*/false,
|
|
|
|
/*isSynthesized=*/true,
|
|
|
|
/*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);
|
|
|
|
|
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(),
|
2011-06-16 07:02:42 +08:00
|
|
|
property->getType().getUnqualifiedType(),
|
2010-03-12 08:38:38 +08:00
|
|
|
/*TInfo=*/0,
|
2010-08-26 11:08:43 +08:00
|
|
|
SC_None,
|
|
|
|
SC_None,
|
2010-03-12 08:38:38 +08:00
|
|
|
0);
|
2011-10-03 14:37:04 +08:00
|
|
|
SetterMethod->setMethodParams(Context, Argument,
|
|
|
|
ArrayRef<SourceLocation>());
|
2010-11-10 15:01:40 +08:00
|
|
|
|
|
|
|
AddPropertyAttrs(*this, SetterMethod, property);
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
CD->addDecl(SetterMethod);
|
2010-05-19 05:09:07 +08:00
|
|
|
// FIXME: Eventually this shouldn't be needed, as the lexical context
|
|
|
|
// and the real context should be the same.
|
2010-09-22 02:28:43 +08:00
|
|
|
if (lexicalDC)
|
2010-05-19 05:09:07 +08:00
|
|
|
SetterMethod->setLexicalDeclContext(lexicalDC);
|
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
|
|
|
|
SetterMethod->setSynthesized(true);
|
|
|
|
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];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
if (GetterMethod)
|
|
|
|
AddInstanceMethodToGlobalPool(GetterMethod);
|
|
|
|
if (SetterMethod)
|
|
|
|
AddInstanceMethodToGlobalPool(SetterMethod);
|
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-06-21 06:57:42 +08:00
|
|
|
unsigned &Attributes,
|
|
|
|
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;
|
|
|
|
|
|
|
|
ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
|
2010-03-31 06:40:11 +08:00
|
|
|
QualType PropertyTy = PropertyDecl->getType();
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2012-01-12 02:26:06 +08:00
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
PropertyTy->isObjCRetainableType()) {
|
|
|
|
// 'readonly' property with no obvious lifetime.
|
|
|
|
// its life time will be determined by its backing ivar.
|
|
|
|
unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
|
|
|
|
ObjCDeclSpec::DQ_PR_copy |
|
|
|
|
ObjCDeclSpec::DQ_PR_retain |
|
|
|
|
ObjCDeclSpec::DQ_PR_strong |
|
|
|
|
ObjCDeclSpec::DQ_PR_weak |
|
|
|
|
ObjCDeclSpec::DQ_PR_assign);
|
|
|
|
if ((Attributes & rel) == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-21 06:57:42 +08:00
|
|
|
if (propertyInPrimaryClass) {
|
|
|
|
// we postpone most property diagnosis until class's implementation
|
|
|
|
// because, its readonly attribute may be overridden in its class
|
|
|
|
// extensions making other attributes, which make no sense, to make sense.
|
|
|
|
if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_readwrite))
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "readonly" << "readwrite";
|
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
// readonly and readwrite/assign/retain/copy conflict.
|
2012-06-21 06:57:42 +08:00
|
|
|
else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
(Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
|
2010-03-12 08:46:40 +08:00
|
|
|
ObjCDeclSpec::DQ_PR_assign |
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCDeclSpec::DQ_PR_unsafe_unretained |
|
2010-03-12 08:46:40 +08:00
|
|
|
ObjCDeclSpec::DQ_PR_copy |
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCDeclSpec::DQ_PR_retain |
|
|
|
|
ObjCDeclSpec::DQ_PR_strong))) {
|
2010-03-12 08:46:40 +08:00
|
|
|
const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
|
|
|
|
"readwrite" :
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_assign) ?
|
|
|
|
"assign" :
|
2011-06-16 07:02:42 +08:00
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
|
|
|
|
"unsafe_unretained" :
|
2010-03-12 08:46:40 +08:00
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_copy) ?
|
|
|
|
"copy" : "retain";
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
|
|
|
|
diag::err_objc_property_attr_mutually_exclusive :
|
|
|
|
diag::warn_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "readonly" << which;
|
|
|
|
}
|
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.
|
2011-06-16 07:02:42 +08:00
|
|
|
if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
|
|
|
|
ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
|
|
|
|
!PropertyTy->isObjCRetainableType() &&
|
2010-03-31 06:40:11 +08:00
|
|
|
!PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
|
2010-03-12 08:46:40 +08:00
|
|
|
Diag(Loc, diag::err_objc_property_requires_object)
|
2011-06-16 07:02:42 +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 |
|
|
|
|
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 }.
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "copy";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
|
|
|
|
}
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "retain";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "strong";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
|
|
|
|
}
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "assign" << "weak";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
|
|
|
}
|
|
|
|
} else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "copy";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
|
|
|
|
}
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "retain";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
|
|
|
}
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "strong";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
|
|
|
|
}
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "unsafe_unretained" << "weak";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
|
|
|
}
|
2010-03-12 08:46:40 +08:00
|
|
|
} else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "copy" << "retain";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "copy" << "strong";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
|
|
|
|
}
|
|
|
|
if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "copy" << "weak";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "retain" << "weak";
|
2011-09-15 02:03:46 +08:00
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_weak)) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "strong" << "weak";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
|
2010-03-12 08:46:40 +08:00
|
|
|
}
|
2010-03-12 08:38:38 +08:00
|
|
|
|
2011-10-11 05:53:24 +08:00
|
|
|
if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
|
|
|
|
Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
|
|
|
|
<< "atomic" << "nonatomic";
|
|
|
|
Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
|
|
|
|
}
|
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
// Warn if user supplied no assignment attribute, property is
|
|
|
|
// readwrite, and this is an object type.
|
|
|
|
if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCDeclSpec::DQ_PR_unsafe_unretained |
|
|
|
|
ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
|
|
|
|
ObjCDeclSpec::DQ_PR_weak)) &&
|
2010-03-12 08:46:40 +08:00
|
|
|
PropertyTy->isObjCObjectPointerType()) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
2011-08-20 03:28:44 +08:00
|
|
|
// With arc, @property definitions should default to (strong) when
|
2011-11-09 04:58:53 +08:00
|
|
|
// not specified; including when property is 'readonly'.
|
2011-08-20 03:28:44 +08:00
|
|
|
PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
|
2011-11-09 04:58:53 +08:00
|
|
|
else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
|
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
|
|
|
;
|
|
|
|
else {
|
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
|
|
|
}
|
2011-08-20 03:28:44 +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
|
|
|
|
2010-03-12 08:46:40 +08:00
|
|
|
if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
|
2011-01-06 07:00:04 +08:00
|
|
|
&&!(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-06-28 04:52:46 +08:00
|
|
|
else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
|
2011-09-15 02:03:46 +08:00
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
!(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
|
|
|
|
PropertyTy->isBlockPointerType())
|
|
|
|
Diag(Loc, diag::warn_objc_property_retain_of_block);
|
2011-11-02 07:02:16 +08:00
|
|
|
|
|
|
|
if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
(Attributes & ObjCDeclSpec::DQ_PR_setter))
|
|
|
|
Diag(Loc, diag::warn_objc_readonly_property_has_setter);
|
|
|
|
|
2010-03-12 08:38:38 +08:00
|
|
|
}
|