Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1 | //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective C @property and |
| 11 | // @synthesize declarations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Argyrios Kyrtzidis | c80553e | 2011-11-14 04:52:29 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
| 19 | #include "clang/AST/ExprObjC.h" |
Fariborz Jahanian | edcc27f | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Lexer.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
| 23 | #include "clang/Sema/Initialization.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Grammar actions. |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 33 | /// getImpliedARCOwnership - Given a set of property attributes and a |
| 34 | /// type, infer an expected lifetime. The type's ownership qualification |
| 35 | /// is not considered. |
| 36 | /// |
| 37 | /// Returns OCL_None if the attributes as stated do not imply an ownership. |
| 38 | /// Never returns OCL_Autoreleasing. |
| 39 | static Qualifiers::ObjCLifetime getImpliedARCOwnership( |
| 40 | ObjCPropertyDecl::PropertyAttributeKind attrs, |
| 41 | QualType type) { |
| 42 | // retain, strong, copy, weak, and unsafe_unretained are only legal |
| 43 | // on properties of retainable pointer type. |
| 44 | if (attrs & (ObjCPropertyDecl::OBJC_PR_retain | |
| 45 | ObjCPropertyDecl::OBJC_PR_strong | |
| 46 | ObjCPropertyDecl::OBJC_PR_copy)) { |
John McCall | d64c2eb | 2012-08-20 23:36:59 +0000 | [diff] [blame] | 47 | return Qualifiers::OCL_Strong; |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 48 | } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) { |
| 49 | return Qualifiers::OCL_Weak; |
| 50 | } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) { |
| 51 | return Qualifiers::OCL_ExplicitNone; |
| 52 | } |
| 53 | |
| 54 | // assign can appear on other types, so we have to check the |
| 55 | // property type. |
| 56 | if (attrs & ObjCPropertyDecl::OBJC_PR_assign && |
| 57 | type->isObjCRetainableType()) { |
| 58 | return Qualifiers::OCL_ExplicitNone; |
| 59 | } |
| 60 | |
| 61 | return Qualifiers::OCL_None; |
| 62 | } |
| 63 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 64 | /// Check the internal consistency of a property declaration. |
| 65 | static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) { |
| 66 | if (property->isInvalidDecl()) return; |
| 67 | |
| 68 | ObjCPropertyDecl::PropertyAttributeKind propertyKind |
| 69 | = property->getPropertyAttributes(); |
| 70 | Qualifiers::ObjCLifetime propertyLifetime |
| 71 | = property->getType().getObjCLifetime(); |
| 72 | |
| 73 | // Nothing to do if we don't have a lifetime. |
| 74 | if (propertyLifetime == Qualifiers::OCL_None) return; |
| 75 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 76 | Qualifiers::ObjCLifetime expectedLifetime |
| 77 | = getImpliedARCOwnership(propertyKind, property->getType()); |
| 78 | if (!expectedLifetime) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 79 | // We have a lifetime qualifier but no dominating property |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 80 | // attribute. That's okay, but restore reasonable invariants by |
| 81 | // setting the property attribute according to the lifetime |
| 82 | // qualifier. |
| 83 | ObjCPropertyDecl::PropertyAttributeKind attr; |
| 84 | if (propertyLifetime == Qualifiers::OCL_Strong) { |
| 85 | attr = ObjCPropertyDecl::OBJC_PR_strong; |
| 86 | } else if (propertyLifetime == Qualifiers::OCL_Weak) { |
| 87 | attr = ObjCPropertyDecl::OBJC_PR_weak; |
| 88 | } else { |
| 89 | assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); |
| 90 | attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
| 91 | } |
| 92 | property->setPropertyAttributes(attr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (propertyLifetime == expectedLifetime) return; |
| 97 | |
| 98 | property->setInvalidDecl(); |
| 99 | S.Diag(property->getLocation(), |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 100 | diag::err_arc_inconsistent_property_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 101 | << property->getDeclName() |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 102 | << expectedLifetime |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 103 | << propertyLifetime; |
| 104 | } |
| 105 | |
Fariborz Jahanian | dad633b | 2012-08-21 21:45:58 +0000 | [diff] [blame] | 106 | static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) { |
| 107 | if ((S.getLangOpts().getGC() != LangOptions::NonGC && |
| 108 | T.isObjCGCWeak()) || |
| 109 | (S.getLangOpts().ObjCAutoRefCount && |
| 110 | T.getObjCLifetime() == Qualifiers::OCL_Weak)) |
| 111 | return ObjCDeclSpec::DQ_PR_weak; |
| 112 | return 0; |
| 113 | } |
| 114 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 115 | Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 116 | SourceLocation LParenLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 117 | FieldDeclarator &FD, |
| 118 | ObjCDeclSpec &ODS, |
| 119 | Selector GetterSel, |
| 120 | Selector SetterSel, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 121 | bool *isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 122 | tok::ObjCKeywordKind MethodImplKind, |
| 123 | DeclContext *lexicalDC) { |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 124 | unsigned Attributes = ODS.getPropertyAttributes(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 125 | TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); |
| 126 | QualType T = TSI->getType(); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 127 | Attributes |= deduceWeakPropertyFromType(*this, T); |
Fariborz Jahanian | dad633b | 2012-08-21 21:45:58 +0000 | [diff] [blame] | 128 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 129 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 130 | // default is readwrite! |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 131 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 132 | // property is defaulted to 'assign' if it is readwrite and is |
| 133 | // not retain or copy |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 134 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 135 | (isReadWrite && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 136 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 137 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 138 | !(Attributes & ObjCDeclSpec::DQ_PR_copy) && |
| 139 | !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) && |
| 140 | !(Attributes & ObjCDeclSpec::DQ_PR_weak))); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 141 | |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 142 | // Proceed with constructing the ObjCPropertyDecls. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 143 | ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 144 | ObjCPropertyDecl *Res = 0; |
| 145 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 146 | if (CDecl->IsClassExtension()) { |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 147 | Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc, |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 148 | FD, GetterSel, SetterSel, |
| 149 | isAssign, isReadWrite, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 150 | Attributes, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 151 | ODS.getPropertyAttributes(), |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 152 | isOverridingProperty, TSI, |
| 153 | MethodImplKind); |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 154 | if (!Res) |
| 155 | return 0; |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 156 | } |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 157 | } |
| 158 | |
| 159 | if (!Res) { |
| 160 | Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD, |
| 161 | GetterSel, SetterSel, isAssign, isReadWrite, |
| 162 | Attributes, ODS.getPropertyAttributes(), |
| 163 | TSI, MethodImplKind); |
| 164 | if (lexicalDC) |
| 165 | Res->setLexicalDeclContext(lexicalDC); |
| 166 | } |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 167 | |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 168 | // Validate the attributes on the @property. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 169 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes, |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 170 | (isa<ObjCInterfaceDecl>(ClassDecl) || |
| 171 | isa<ObjCProtocolDecl>(ClassDecl))); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 172 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 173 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 174 | checkARCPropertyDecl(*this, Res); |
| 175 | |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 176 | // Compare this property against the property in our superclass. |
| 177 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
| 178 | if (ObjCInterfaceDecl *Super = IFace->getSuperClass()) { |
| 179 | DeclContext::lookup_result R = Super->lookup(Res->getDeclName()); |
| 180 | for (unsigned I = 0, N = R.size(); I != N; ++I) |
| 181 | if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) |
| 182 | DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier()); |
| 183 | } |
| 184 | } |
| 185 | |
Dmitri Gribenko | abd56c8 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 186 | ActOnDocumentableDecl(Res); |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 187 | return Res; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 188 | } |
Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 189 | |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 190 | static ObjCPropertyDecl::PropertyAttributeKind |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 191 | makePropertyAttributesAsWritten(unsigned Attributes) { |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 192 | unsigned attributesAsWritten = 0; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 193 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 194 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 195 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 196 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 197 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 198 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 199 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 200 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 201 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 202 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 203 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 204 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 205 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 206 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 207 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 208 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 209 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 210 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 211 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 212 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 213 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 214 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 215 | if (Attributes & ObjCDeclSpec::DQ_PR_atomic) |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 216 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic; |
| 217 | |
| 218 | return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten; |
| 219 | } |
| 220 | |
Fariborz Jahanian | 5bf0e35 | 2012-05-21 17:10:28 +0000 | [diff] [blame] | 221 | static bool LocPropertyAttribute( ASTContext &Context, const char *attrName, |
Fariborz Jahanian | edcc27f | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 222 | SourceLocation LParenLoc, SourceLocation &Loc) { |
| 223 | if (LParenLoc.isMacroID()) |
| 224 | return false; |
| 225 | |
| 226 | SourceManager &SM = Context.getSourceManager(); |
| 227 | std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc); |
| 228 | // Try to load the file buffer. |
| 229 | bool invalidTemp = false; |
| 230 | StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
| 231 | if (invalidTemp) |
| 232 | return false; |
| 233 | const char *tokenBegin = file.data() + locInfo.second; |
| 234 | |
| 235 | // Lex from the start of the given location. |
| 236 | Lexer lexer(SM.getLocForStartOfFile(locInfo.first), |
| 237 | Context.getLangOpts(), |
| 238 | file.begin(), tokenBegin, file.end()); |
| 239 | Token Tok; |
| 240 | do { |
| 241 | lexer.LexFromRawLexer(Tok); |
| 242 | if (Tok.is(tok::raw_identifier) && |
| 243 | StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) { |
| 244 | Loc = Tok.getLocation(); |
| 245 | return true; |
| 246 | } |
| 247 | } while (Tok.isNot(tok::r_paren)); |
| 248 | return false; |
| 249 | |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Fariborz Jahanian | d9f95b3 | 2012-08-21 21:52:02 +0000 | [diff] [blame] | 252 | static unsigned getOwnershipRule(unsigned attr) { |
Fariborz Jahanian | dad633b | 2012-08-21 21:45:58 +0000 | [diff] [blame] | 253 | return attr & (ObjCPropertyDecl::OBJC_PR_assign | |
| 254 | ObjCPropertyDecl::OBJC_PR_retain | |
| 255 | ObjCPropertyDecl::OBJC_PR_copy | |
| 256 | ObjCPropertyDecl::OBJC_PR_weak | |
| 257 | ObjCPropertyDecl::OBJC_PR_strong | |
| 258 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 259 | } |
| 260 | |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 261 | ObjCPropertyDecl * |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 262 | Sema::HandlePropertyInClassExtension(Scope *S, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 263 | SourceLocation AtLoc, |
| 264 | SourceLocation LParenLoc, |
| 265 | FieldDeclarator &FD, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 266 | Selector GetterSel, Selector SetterSel, |
| 267 | const bool isAssign, |
| 268 | const bool isReadWrite, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 269 | const unsigned Attributes, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 270 | const unsigned AttributesAsWritten, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 271 | bool *isOverridingProperty, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 272 | TypeSourceInfo *T, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 273 | tok::ObjCKeywordKind MethodImplKind) { |
Fariborz Jahanian | 58a7649 | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 274 | ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 275 | // Diagnose if this property is already in continuation class. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 276 | DeclContext *DC = CurContext; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 277 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Fariborz Jahanian | 2a28914 | 2010-11-10 18:01:36 +0000 | [diff] [blame] | 278 | ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); |
| 279 | |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 280 | if (CCPrimary) { |
Fariborz Jahanian | 2a28914 | 2010-11-10 18:01:36 +0000 | [diff] [blame] | 281 | // Check for duplicate declaration of this property in current and |
| 282 | // other class extensions. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 283 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 284 | Ext = CCPrimary->known_extensions_begin(), |
| 285 | ExtEnd = CCPrimary->known_extensions_end(); |
| 286 | Ext != ExtEnd; ++Ext) { |
| 287 | if (ObjCPropertyDecl *prevDecl |
| 288 | = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) { |
Fariborz Jahanian | 2a28914 | 2010-11-10 18:01:36 +0000 | [diff] [blame] | 289 | Diag(AtLoc, diag::err_duplicate_property); |
| 290 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
| 291 | return 0; |
| 292 | } |
| 293 | } |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 296 | // Create a new ObjCPropertyDecl with the DeclContext being |
| 297 | // the class extension. |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 298 | // FIXME. We should really be using CreatePropertyDecl for this. |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 299 | ObjCPropertyDecl *PDecl = |
| 300 | ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(), |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 301 | PropertyId, AtLoc, LParenLoc, T); |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 302 | PDecl->setPropertyAttributesAsWritten( |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 303 | makePropertyAttributesAsWritten(AttributesAsWritten)); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 304 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Fariborz Jahanian | 22f757b | 2010-03-22 23:25:52 +0000 | [diff] [blame] | 305 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 306 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
Fariborz Jahanian | 22f757b | 2010-03-22 23:25:52 +0000 | [diff] [blame] | 307 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 308 | // Set setter/getter selector name. Needed later. |
| 309 | PDecl->setGetterName(GetterSel); |
| 310 | PDecl->setSetterName(SetterSel); |
Douglas Gregor | 91ae6b4 | 2011-07-15 15:30:21 +0000 | [diff] [blame] | 311 | ProcessDeclAttributes(S, PDecl, FD.D); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 312 | DC->addDecl(PDecl); |
| 313 | |
| 314 | // We need to look in the @interface to see if the @property was |
| 315 | // already declared. |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 316 | if (!CCPrimary) { |
| 317 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 318 | *isOverridingProperty = true; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 319 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // Find the property in continuation class's primary class only. |
| 323 | ObjCPropertyDecl *PIDecl = |
| 324 | CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId); |
| 325 | |
| 326 | if (!PIDecl) { |
| 327 | // No matching property found in the primary class. Just fall thru |
| 328 | // and add property to continuation class's primary class. |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 329 | ObjCPropertyDecl *PrimaryPDecl = |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 330 | CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 331 | FD, GetterSel, SetterSel, isAssign, isReadWrite, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 332 | Attributes,AttributesAsWritten, T, MethodImplKind, DC); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 333 | |
| 334 | // A case of continuation class adding a new property in the class. This |
| 335 | // is not what it was meant for. However, gcc supports it and so should we. |
| 336 | // Make sure setter/getters are declared here. |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 337 | ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0, |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 338 | /* lexicalDC = */ CDecl); |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 339 | PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl()); |
| 340 | PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl()); |
Argyrios Kyrtzidis | c80553e | 2011-11-14 04:52:29 +0000 | [diff] [blame] | 341 | if (ASTMutationListener *L = Context.getASTMutationListener()) |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 342 | L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl); |
| 343 | return PrimaryPDecl; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 344 | } |
Fariborz Jahanian | e235183 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 345 | if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) { |
| 346 | bool IncompatibleObjC = false; |
| 347 | QualType ConvertedType; |
Fariborz Jahanian | ff2a0ec | 2012-02-02 19:34:05 +0000 | [diff] [blame] | 348 | // Relax the strict type matching for property type in continuation class. |
| 349 | // Allow property object type of continuation class to be different as long |
Fariborz Jahanian | ad7eff2 | 2012-02-02 22:37:48 +0000 | [diff] [blame] | 350 | // as it narrows the object type in its primary class property. Note that |
| 351 | // this conversion is safe only because the wider type is for a 'readonly' |
| 352 | // property in primary class and 'narrowed' type for a 'readwrite' property |
| 353 | // in continuation class. |
Fariborz Jahanian | e235183 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 354 | if (!isa<ObjCObjectPointerType>(PIDecl->getType()) || |
| 355 | !isa<ObjCObjectPointerType>(PDecl->getType()) || |
| 356 | (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(), |
| 357 | ConvertedType, IncompatibleObjC)) |
| 358 | || IncompatibleObjC) { |
| 359 | Diag(AtLoc, |
| 360 | diag::err_type_mismatch_continuation_class) << PDecl->getType(); |
| 361 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | 6defd9f | 2012-09-17 20:57:19 +0000 | [diff] [blame] | 362 | return 0; |
Fariborz Jahanian | e235183 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 363 | } |
Fariborz Jahanian | a4b984d | 2011-09-24 00:56:59 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 366 | // The property 'PIDecl's readonly attribute will be over-ridden |
| 367 | // with continuation class's readwrite property attribute! |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 368 | unsigned PIkind = PIDecl->getPropertyAttributesAsWritten(); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 369 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
Fariborz Jahanian | dad633b | 2012-08-21 21:45:58 +0000 | [diff] [blame] | 370 | PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType()); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 371 | unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes); |
Fariborz Jahanian | d9f95b3 | 2012-08-21 21:52:02 +0000 | [diff] [blame] | 372 | unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind); |
Fariborz Jahanian | dad633b | 2012-08-21 21:45:58 +0000 | [diff] [blame] | 373 | if (PrimaryClassMemoryModel && ClassExtensionMemoryModel && |
| 374 | (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) { |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 375 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
| 376 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 377 | } |
Ted Kremenek | 9944c76 | 2010-03-18 01:22:36 +0000 | [diff] [blame] | 378 | DeclContext *DC = cast<DeclContext>(CCPrimary); |
| 379 | if (!ObjCPropertyDecl::findPropertyDecl(DC, |
| 380 | PIDecl->getDeclName().getAsIdentifierInfo())) { |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 381 | // Protocol is not in the primary class. Must build one for it. |
| 382 | ObjCDeclSpec ProtocolPropertyODS; |
| 383 | // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind |
| 384 | // and ObjCPropertyDecl::PropertyAttributeKind have identical |
| 385 | // values. Should consolidate both into one enum type. |
| 386 | ProtocolPropertyODS. |
| 387 | setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind) |
| 388 | PIkind); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 389 | // Must re-establish the context from class extension to primary |
| 390 | // class context. |
Fariborz Jahanian | 7939418 | 2011-08-22 20:15:24 +0000 | [diff] [blame] | 391 | ContextRAII SavedContext(*this, CCPrimary); |
| 392 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 393 | Decl *ProtocolPtrTy = |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 394 | ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 395 | PIDecl->getGetterName(), |
| 396 | PIDecl->getSetterName(), |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 397 | isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 398 | MethodImplKind, |
| 399 | /* lexicalDC = */ CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 400 | PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 401 | } |
| 402 | PIDecl->makeitReadWriteAttribute(); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 403 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 404 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 405 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 406 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 407 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 408 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 409 | PIDecl->setSetterName(SetterSel); |
| 410 | } else { |
Ted Kremenek | 788f489 | 2010-10-21 18:49:42 +0000 | [diff] [blame] | 411 | // Tailor the diagnostics for the common case where a readwrite |
| 412 | // property is declared both in the @interface and the continuation. |
| 413 | // This is a common error where the user often intended the original |
| 414 | // declaration to be readonly. |
| 415 | unsigned diag = |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 416 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite) && |
Ted Kremenek | 788f489 | 2010-10-21 18:49:42 +0000 | [diff] [blame] | 417 | (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) |
| 418 | ? diag::err_use_continuation_class_redeclaration_readwrite |
| 419 | : diag::err_use_continuation_class; |
| 420 | Diag(AtLoc, diag) |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 421 | << CCPrimary->getDeclName(); |
| 422 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | 6defd9f | 2012-09-17 20:57:19 +0000 | [diff] [blame] | 423 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 424 | } |
| 425 | *isOverridingProperty = true; |
| 426 | // Make sure setter decl is synthesized, and added to primary class's list. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 427 | ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl); |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 428 | PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl()); |
| 429 | PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl()); |
Argyrios Kyrtzidis | c80553e | 2011-11-14 04:52:29 +0000 | [diff] [blame] | 430 | if (ASTMutationListener *L = Context.getASTMutationListener()) |
| 431 | L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl); |
Fariborz Jahanian | 6defd9f | 2012-09-17 20:57:19 +0000 | [diff] [blame] | 432 | return PDecl; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, |
| 436 | ObjCContainerDecl *CDecl, |
| 437 | SourceLocation AtLoc, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 438 | SourceLocation LParenLoc, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 439 | FieldDeclarator &FD, |
| 440 | Selector GetterSel, |
| 441 | Selector SetterSel, |
| 442 | const bool isAssign, |
| 443 | const bool isReadWrite, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 444 | const unsigned Attributes, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 445 | const unsigned AttributesAsWritten, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 446 | TypeSourceInfo *TInfo, |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 447 | tok::ObjCKeywordKind MethodImplKind, |
| 448 | DeclContext *lexicalDC){ |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 449 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 450 | QualType T = TInfo->getType(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 451 | |
| 452 | // Issue a warning if property is 'assign' as default and its object, which is |
| 453 | // gc'able conforms to NSCopying protocol |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 454 | if (getLangOpts().getGC() != LangOptions::NonGC && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 455 | isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 456 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 457 | T->getAs<ObjCObjectPointerType>()) { |
| 458 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 459 | if (IDecl) |
| 460 | if (ObjCProtocolDecl* PNSCopying = |
| 461 | LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) |
| 462 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
| 463 | Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 464 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 465 | if (T->isObjCObjectType()) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 466 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); |
| 467 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 468 | DeclContext *DC = cast<DeclContext>(CDecl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 469 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
| 470 | FD.D.getIdentifierLoc(), |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 471 | PropertyId, AtLoc, LParenLoc, TInfo); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 473 | if (ObjCPropertyDecl *prevDecl = |
| 474 | ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 475 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
Ted Kremenek | 894ae6a | 2010-03-15 18:47:25 +0000 | [diff] [blame] | 476 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 477 | PDecl->setInvalidDecl(); |
| 478 | } |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 479 | else { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 480 | DC->addDecl(PDecl); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 481 | if (lexicalDC) |
| 482 | PDecl->setLexicalDeclContext(lexicalDC); |
| 483 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 484 | |
| 485 | if (T->isArrayType() || T->isFunctionType()) { |
| 486 | Diag(AtLoc, diag::err_property_type) << T; |
| 487 | PDecl->setInvalidDecl(); |
| 488 | } |
| 489 | |
| 490 | ProcessDeclAttributes(S, PDecl, FD.D); |
| 491 | |
| 492 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 493 | // selector names in anticipation of declaration of setter/getter methods. |
| 494 | PDecl->setGetterName(GetterSel); |
| 495 | PDecl->setSetterName(SetterSel); |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 496 | PDecl->setPropertyAttributesAsWritten( |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 497 | makePropertyAttributesAsWritten(AttributesAsWritten)); |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 498 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 499 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 500 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 501 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 502 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 503 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
| 504 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 505 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 506 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
| 507 | |
| 508 | if (isReadWrite) |
| 509 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
| 510 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 511 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 512 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 513 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 514 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 515 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 516 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 517 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 518 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); |
| 519 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 520 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 521 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 522 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 523 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 524 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 525 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 526 | if (isAssign) |
| 527 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 528 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 529 | // In the semantic attributes, one of nonatomic or atomic is always set. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 530 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 531 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 532 | else |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 533 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 534 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 535 | // 'unsafe_unretained' is alias for 'assign'. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 536 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 537 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 538 | if (isAssign) |
| 539 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 540 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 541 | if (MethodImplKind == tok::objc_required) |
| 542 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 543 | else if (MethodImplKind == tok::objc_optional) |
| 544 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 546 | return PDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 547 | } |
| 548 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 549 | static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, |
| 550 | ObjCPropertyDecl *property, |
| 551 | ObjCIvarDecl *ivar) { |
| 552 | if (property->isInvalidDecl() || ivar->isInvalidDecl()) return; |
| 553 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 554 | QualType ivarType = ivar->getType(); |
| 555 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 556 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 557 | // The lifetime implied by the property's attributes. |
| 558 | Qualifiers::ObjCLifetime propertyLifetime = |
| 559 | getImpliedARCOwnership(property->getPropertyAttributes(), |
| 560 | property->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 561 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 562 | // We're fine if they match. |
| 563 | if (propertyLifetime == ivarLifetime) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 564 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 565 | // These aren't valid lifetimes for object ivars; don't diagnose twice. |
| 566 | if (ivarLifetime == Qualifiers::OCL_None || |
| 567 | ivarLifetime == Qualifiers::OCL_Autoreleasing) |
| 568 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 569 | |
John McCall | d64c2eb | 2012-08-20 23:36:59 +0000 | [diff] [blame] | 570 | // If the ivar is private, and it's implicitly __unsafe_unretained |
| 571 | // becaues of its type, then pretend it was actually implicitly |
| 572 | // __strong. This is only sound because we're processing the |
| 573 | // property implementation before parsing any method bodies. |
| 574 | if (ivarLifetime == Qualifiers::OCL_ExplicitNone && |
| 575 | propertyLifetime == Qualifiers::OCL_Strong && |
| 576 | ivar->getAccessControl() == ObjCIvarDecl::Private) { |
| 577 | SplitQualType split = ivarType.split(); |
| 578 | if (split.Quals.hasObjCLifetime()) { |
| 579 | assert(ivarType->isObjCARCImplicitlyUnretainedType()); |
| 580 | split.Quals.setObjCLifetime(Qualifiers::OCL_Strong); |
| 581 | ivarType = S.Context.getQualifiedType(split); |
| 582 | ivar->setType(ivarType); |
| 583 | return; |
| 584 | } |
| 585 | } |
| 586 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 587 | switch (propertyLifetime) { |
| 588 | case Qualifiers::OCL_Strong: |
Argyrios Kyrtzidis | 135aa60 | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 589 | S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership) |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 590 | << property->getDeclName() |
| 591 | << ivar->getDeclName() |
| 592 | << ivarLifetime; |
| 593 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 594 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 595 | case Qualifiers::OCL_Weak: |
Argyrios Kyrtzidis | 135aa60 | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 596 | S.Diag(ivar->getLocation(), diag::error_weak_property) |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 597 | << property->getDeclName() |
| 598 | << ivar->getDeclName(); |
| 599 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 600 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 601 | case Qualifiers::OCL_ExplicitNone: |
Argyrios Kyrtzidis | 135aa60 | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 602 | S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership) |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 603 | << property->getDeclName() |
| 604 | << ivar->getDeclName() |
| 605 | << ((property->getPropertyAttributesAsWritten() |
| 606 | & ObjCPropertyDecl::OBJC_PR_assign) != 0); |
| 607 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 608 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 609 | case Qualifiers::OCL_Autoreleasing: |
| 610 | llvm_unreachable("properties cannot be autoreleasing"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 611 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 612 | case Qualifiers::OCL_None: |
| 613 | // Any other property should be ignored. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
| 617 | S.Diag(property->getLocation(), diag::note_property_declare); |
Argyrios Kyrtzidis | 135aa60 | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 618 | if (propertyImplLoc.isValid()) |
| 619 | S.Diag(propertyImplLoc, diag::note_property_synthesize); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 622 | /// setImpliedPropertyAttributeForReadOnlyProperty - |
| 623 | /// This routine evaludates life-time attributes for a 'readonly' |
| 624 | /// property with no known lifetime of its own, using backing |
| 625 | /// 'ivar's attribute, if any. If no backing 'ivar', property's |
| 626 | /// life-time is assumed 'strong'. |
| 627 | static void setImpliedPropertyAttributeForReadOnlyProperty( |
| 628 | ObjCPropertyDecl *property, ObjCIvarDecl *ivar) { |
| 629 | Qualifiers::ObjCLifetime propertyLifetime = |
| 630 | getImpliedARCOwnership(property->getPropertyAttributes(), |
| 631 | property->getType()); |
| 632 | if (propertyLifetime != Qualifiers::OCL_None) |
| 633 | return; |
| 634 | |
| 635 | if (!ivar) { |
| 636 | // if no backing ivar, make property 'strong'. |
| 637 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 638 | return; |
| 639 | } |
| 640 | // property assumes owenership of backing ivar. |
| 641 | QualType ivarType = ivar->getType(); |
| 642 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
| 643 | if (ivarLifetime == Qualifiers::OCL_Strong) |
| 644 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 645 | else if (ivarLifetime == Qualifiers::OCL_Weak) |
| 646 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); |
| 647 | return; |
| 648 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 649 | |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 650 | /// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property |
| 651 | /// attribute declared in primary class and attributes overridden in any of its |
| 652 | /// class extensions. |
| 653 | static void |
| 654 | DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl, |
| 655 | ObjCPropertyDecl *property) { |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 656 | unsigned Attributes = property->getPropertyAttributesAsWritten(); |
| 657 | bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 658 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 659 | Ext = ClassDecl->known_extensions_begin(), |
| 660 | ExtEnd = ClassDecl->known_extensions_end(); |
| 661 | Ext != ExtEnd; ++Ext) { |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 662 | ObjCPropertyDecl *ClassExtProperty = 0; |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 663 | DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); |
| 664 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 665 | ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]); |
| 666 | if (ClassExtProperty) |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 667 | break; |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 668 | } |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 669 | |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 670 | if (ClassExtProperty) { |
Fariborz Jahanian | c78ff27 | 2012-06-20 23:18:57 +0000 | [diff] [blame] | 671 | warn = false; |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 672 | unsigned classExtPropertyAttr = |
| 673 | ClassExtProperty->getPropertyAttributesAsWritten(); |
| 674 | // We are issuing the warning that we postponed because class extensions |
| 675 | // can override readonly->readwrite and 'setter' attributes originally |
| 676 | // placed on class's property declaration now make sense in the overridden |
| 677 | // property. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 678 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) { |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 679 | if (!classExtPropertyAttr || |
Fariborz Jahanian | a6e28f2 | 2012-08-24 20:10:53 +0000 | [diff] [blame] | 680 | (classExtPropertyAttr & |
| 681 | (ObjCDeclSpec::DQ_PR_readwrite| |
| 682 | ObjCDeclSpec::DQ_PR_assign | |
| 683 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 684 | ObjCDeclSpec::DQ_PR_copy | |
| 685 | ObjCDeclSpec::DQ_PR_retain | |
| 686 | ObjCDeclSpec::DQ_PR_strong))) |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 687 | continue; |
| 688 | warn = true; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | if (warn) { |
| 694 | unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign | |
| 695 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 696 | ObjCDeclSpec::DQ_PR_copy | |
| 697 | ObjCDeclSpec::DQ_PR_retain | |
| 698 | ObjCDeclSpec::DQ_PR_strong); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 699 | if (Attributes & setterAttrs) { |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 700 | const char * which = |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 701 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 702 | "assign" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 703 | (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 704 | "unsafe_unretained" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 705 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 706 | "copy" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 707 | (Attributes & ObjCDeclSpec::DQ_PR_retain) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 708 | "retain" : "strong"; |
| 709 | |
| 710 | S.Diag(property->getLocation(), |
| 711 | diag::warn_objc_property_attr_mutually_exclusive) |
| 712 | << "readonly" << which; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | |
| 717 | } |
| 718 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 719 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 720 | /// builds the AST node for a property implementation declaration; declared |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 721 | /// as \@synthesize or \@dynamic. |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 722 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 723 | Decl *Sema::ActOnPropertyImplDecl(Scope *S, |
| 724 | SourceLocation AtLoc, |
| 725 | SourceLocation PropertyLoc, |
| 726 | bool Synthesize, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 727 | IdentifierInfo *PropertyId, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 728 | IdentifierInfo *PropertyIvar, |
| 729 | SourceLocation PropertyIvarLoc) { |
Ted Kremenek | e968657 | 2010-04-05 23:45:09 +0000 | [diff] [blame] | 730 | ObjCContainerDecl *ClassImpDecl = |
Fariborz Jahanian | 84e0ccf | 2011-09-19 16:32:32 +0000 | [diff] [blame] | 731 | dyn_cast<ObjCContainerDecl>(CurContext); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 732 | // Make sure we have a context for the property implementation declaration. |
| 733 | if (!ClassImpDecl) { |
| 734 | Diag(AtLoc, diag::error_missing_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 735 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 736 | } |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 737 | if (PropertyIvarLoc.isInvalid()) |
| 738 | PropertyIvarLoc = PropertyLoc; |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 739 | SourceLocation PropertyDiagLoc = PropertyLoc; |
| 740 | if (PropertyDiagLoc.isInvalid()) |
| 741 | PropertyDiagLoc = ClassImpDecl->getLocStart(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 742 | ObjCPropertyDecl *property = 0; |
| 743 | ObjCInterfaceDecl* IDecl = 0; |
| 744 | // Find the class or category class where this property must have |
| 745 | // a declaration. |
| 746 | ObjCImplementationDecl *IC = 0; |
| 747 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 748 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
| 749 | IDecl = IC->getClassInterface(); |
| 750 | // We always synthesize an interface for an implementation |
| 751 | // without an interface decl. So, IDecl is always non-zero. |
| 752 | assert(IDecl && |
| 753 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 754 | |
| 755 | // Look for this property declaration in the @implementation's @interface |
| 756 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 757 | if (!property) { |
| 758 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 759 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 760 | } |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 761 | unsigned PIkind = property->getPropertyAttributesAsWritten(); |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 762 | if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | |
| 763 | ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 764 | if (AtLoc.isValid()) |
| 765 | Diag(AtLoc, diag::warn_implicit_atomic_property); |
| 766 | else |
| 767 | Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); |
| 768 | Diag(property->getLocation(), diag::note_property_declare); |
| 769 | } |
| 770 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 771 | if (const ObjCCategoryDecl *CD = |
| 772 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
| 773 | if (!CD->IsClassExtension()) { |
| 774 | Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); |
| 775 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 776 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 779 | |
| 780 | if (Synthesize&& |
| 781 | (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) && |
| 782 | property->hasAttr<IBOutletAttr>() && |
| 783 | !AtLoc.isValid()) { |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 784 | Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property); |
| 785 | Diag(property->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | edcc27f | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 786 | SourceLocation readonlyLoc; |
Fariborz Jahanian | 5bf0e35 | 2012-05-21 17:10:28 +0000 | [diff] [blame] | 787 | if (LocPropertyAttribute(Context, "readonly", |
Fariborz Jahanian | edcc27f | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 788 | property->getLParenLoc(), readonlyLoc)) { |
| 789 | SourceLocation endLoc = |
| 790 | readonlyLoc.getLocWithOffset(strlen("readonly")-1); |
| 791 | SourceRange ReadonlySourceRange(readonlyLoc, endLoc); |
| 792 | Diag(property->getLocation(), |
| 793 | diag::note_auto_readonly_iboutlet_fixup_suggest) << |
| 794 | FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite"); |
| 795 | } |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 796 | } |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 797 | |
| 798 | DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property); |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 799 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 800 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
| 801 | if (Synthesize) { |
| 802 | Diag(AtLoc, diag::error_synthesize_category_decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 803 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 804 | } |
| 805 | IDecl = CatImplClass->getClassInterface(); |
| 806 | if (!IDecl) { |
| 807 | Diag(AtLoc, diag::error_missing_property_interface); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 808 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 809 | } |
| 810 | ObjCCategoryDecl *Category = |
| 811 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 812 | |
| 813 | // If category for this implementation not found, it is an error which |
| 814 | // has already been reported eralier. |
| 815 | if (!Category) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 816 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 817 | // Look for this property declaration in @implementation's category |
| 818 | property = Category->FindPropertyDeclaration(PropertyId); |
| 819 | if (!property) { |
| 820 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
| 821 | << Category->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 822 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 823 | } |
| 824 | } else { |
| 825 | Diag(AtLoc, diag::error_bad_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 826 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 827 | } |
| 828 | ObjCIvarDecl *Ivar = 0; |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 829 | bool CompleteTypeErr = false; |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 830 | bool compat = true; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 831 | // Check that we have a valid, previously declared ivar for @synthesize |
| 832 | if (Synthesize) { |
| 833 | // @synthesize |
| 834 | if (!PropertyIvar) |
| 835 | PropertyIvar = PropertyId; |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 836 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
| 837 | ObjCInterfaceDecl *ClassDeclared; |
| 838 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
| 839 | QualType PropType = property->getType(); |
| 840 | QualType PropertyIvarType = PropType.getNonReferenceType(); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 841 | |
| 842 | if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 843 | diag::err_incomplete_synthesized_property, |
| 844 | property->getDeclName())) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 845 | Diag(property->getLocation(), diag::note_property_declare); |
| 846 | CompleteTypeErr = true; |
| 847 | } |
| 848 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 849 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 850 | (property->getPropertyAttributesAsWritten() & |
Fariborz Jahanian | 3efd348 | 2012-01-11 19:48:08 +0000 | [diff] [blame] | 851 | ObjCPropertyDecl::OBJC_PR_readonly) && |
| 852 | PropertyIvarType->isObjCRetainableType()) { |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 853 | setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar); |
| 854 | } |
| 855 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 856 | ObjCPropertyDecl::PropertyAttributeKind kind |
| 857 | = property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 858 | |
| 859 | // Add GC __weak to the ivar type if the property is weak. |
| 860 | if ((kind & ObjCPropertyDecl::OBJC_PR_weak) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 861 | getLangOpts().getGC() != LangOptions::NonGC) { |
| 862 | assert(!getLangOpts().ObjCAutoRefCount); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 863 | if (PropertyIvarType.isObjCGCStrong()) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 864 | Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 865 | Diag(property->getLocation(), diag::note_property_declare); |
| 866 | } else { |
| 867 | PropertyIvarType = |
| 868 | Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 869 | } |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 870 | } |
Fariborz Jahanian | e95f8ef | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 871 | if (AtLoc.isInvalid()) { |
| 872 | // Check when default synthesizing a property that there is |
| 873 | // an ivar matching property name and issue warning; since this |
| 874 | // is the most common case of not using an ivar used for backing |
| 875 | // property in non-default synthesis case. |
| 876 | ObjCInterfaceDecl *ClassDeclared=0; |
| 877 | ObjCIvarDecl *originalIvar = |
| 878 | IDecl->lookupInstanceVariable(property->getIdentifier(), |
| 879 | ClassDeclared); |
| 880 | if (originalIvar) { |
| 881 | Diag(PropertyDiagLoc, |
| 882 | diag::warn_autosynthesis_property_ivar_match) |
Fariborz Jahanian | 2578532 | 2012-06-29 19:05:11 +0000 | [diff] [blame] | 883 | << PropertyId << (Ivar == 0) << PropertyIvar |
Fariborz Jahanian | 20e7d99 | 2012-06-29 18:43:30 +0000 | [diff] [blame] | 884 | << originalIvar->getIdentifier(); |
Fariborz Jahanian | e95f8ef | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 885 | Diag(property->getLocation(), diag::note_property_declare); |
| 886 | Diag(originalIvar->getLocation(), diag::note_ivar_decl); |
Fariborz Jahanian | dd3284b | 2012-06-19 22:51:22 +0000 | [diff] [blame] | 887 | } |
Fariborz Jahanian | e95f8ef | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if (!Ivar) { |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 891 | // In ARC, give the ivar a lifetime qualifier based on the |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 892 | // property attributes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 893 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 894 | !PropertyIvarType.getObjCLifetime() && |
| 895 | PropertyIvarType->isObjCRetainableType()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 896 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 897 | // It's an error if we have to do this and the user didn't |
| 898 | // explicitly write an ownership attribute on the property. |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 899 | if (!property->hasWrittenStorageAttribute() && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 900 | !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 901 | Diag(PropertyDiagLoc, |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 902 | diag::err_arc_objc_property_default_assign_on_object); |
| 903 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 904 | } else { |
| 905 | Qualifiers::ObjCLifetime lifetime = |
| 906 | getImpliedARCOwnership(kind, PropertyIvarType); |
| 907 | assert(lifetime && "no lifetime for property?"); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 908 | if (lifetime == Qualifiers::OCL_Weak) { |
| 909 | bool err = false; |
| 910 | if (const ObjCObjectPointerType *ObjT = |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 911 | PropertyIvarType->getAs<ObjCObjectPointerType>()) { |
| 912 | const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl(); |
| 913 | if (ObjI && ObjI->isArcWeakrefUnavailable()) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 914 | Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 915 | Diag(property->getLocation(), diag::note_property_declare); |
| 916 | err = true; |
| 917 | } |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 918 | } |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 919 | if (!err && !getLangOpts().ObjCARCWeak) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 920 | Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 921 | Diag(property->getLocation(), diag::note_property_declare); |
| 922 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 923 | } |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 924 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 925 | Qualifiers qs; |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 926 | qs.addObjCLifetime(lifetime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 927 | PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); |
| 928 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (kind & ObjCPropertyDecl::OBJC_PR_weak && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 932 | !getLangOpts().ObjCAutoRefCount && |
| 933 | getLangOpts().getGC() == LangOptions::NonGC) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 934 | Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 935 | Diag(property->getLocation(), diag::note_property_declare); |
| 936 | } |
| 937 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 938 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 939 | PropertyIvarLoc,PropertyIvarLoc, PropertyIvar, |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 940 | PropertyIvarType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 941 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 942 | (Expr *)0, true); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 943 | if (CompleteTypeErr) |
| 944 | Ivar->setInvalidDecl(); |
Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 945 | ClassImpDecl->addDecl(Ivar); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 946 | IDecl->makeDeclVisibleInContext(Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 947 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 948 | if (getLangOpts().ObjCRuntime.isFragile()) |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 949 | Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl) |
| 950 | << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 951 | // Note! I deliberately want it to fall thru so, we have a |
| 952 | // a property implementation and to avoid future warnings. |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 953 | } else if (getLangOpts().ObjCRuntime.isNonFragile() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 954 | !declaresSameEntity(ClassDeclared, IDecl)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 955 | Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 956 | << property->getDeclName() << Ivar->getDeclName() |
| 957 | << ClassDeclared->getDeclName(); |
| 958 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 959 | << Ivar << Ivar->getName(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 960 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 961 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 962 | property->setPropertyIvarDecl(Ivar); |
| 963 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 964 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 965 | |
| 966 | // Check that type of property and its ivar are type compatible. |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 967 | if (!Context.hasSameType(PropertyIvarType, IvarType)) { |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 968 | if (isa<ObjCObjectPointerType>(PropertyIvarType) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 969 | && isa<ObjCObjectPointerType>(IvarType)) |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 970 | compat = |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 971 | Context.canAssignObjCInterfaces( |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 972 | PropertyIvarType->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 973 | IvarType->getAs<ObjCObjectPointerType>()); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 974 | else { |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 975 | compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType, |
| 976 | IvarType) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 977 | == Compatible); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 978 | } |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 979 | if (!compat) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 980 | Diag(PropertyDiagLoc, diag::error_property_ivar_type) |
Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 981 | << property->getDeclName() << PropType |
| 982 | << Ivar->getDeclName() << IvarType; |
| 983 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 984 | // Note! I deliberately want it to fall thru so, we have a |
| 985 | // a property implementation and to avoid future warnings. |
| 986 | } |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 987 | else { |
| 988 | // FIXME! Rules for properties are somewhat different that those |
| 989 | // for assignments. Use a new routine to consolidate all cases; |
| 990 | // specifically for property redeclarations as well as for ivars. |
| 991 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
| 992 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 993 | if (lhsType != rhsType && |
| 994 | lhsType->isArithmeticType()) { |
| 995 | Diag(PropertyDiagLoc, diag::error_property_ivar_type) |
| 996 | << property->getDeclName() << PropType |
| 997 | << Ivar->getDeclName() << IvarType; |
| 998 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
| 999 | // Fall thru - see previous comment |
| 1000 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1001 | } |
| 1002 | // __weak is explicit. So it works on Canonical type. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1003 | if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1004 | getLangOpts().getGC() != LangOptions::NonGC)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1005 | Diag(PropertyDiagLoc, diag::error_weak_property) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1006 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 1007 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1008 | // Fall thru - see previous comment |
| 1009 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1010 | // Fall thru - see previous comment |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1011 | if ((property->getType()->isObjCObjectPointerType() || |
| 1012 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1013 | getLangOpts().getGC() != LangOptions::NonGC) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1014 | Diag(PropertyDiagLoc, diag::error_strong_property) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1015 | << property->getDeclName() << Ivar->getDeclName(); |
| 1016 | // Fall thru - see previous comment |
| 1017 | } |
| 1018 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1019 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1020 | checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1021 | } else if (PropertyIvar) |
| 1022 | // @dynamic |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1023 | Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1024 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1025 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
| 1026 | ObjCPropertyImplDecl *PIDecl = |
| 1027 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 1028 | property, |
| 1029 | (Synthesize ? |
| 1030 | ObjCPropertyImplDecl::Synthesize |
| 1031 | : ObjCPropertyImplDecl::Dynamic), |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1032 | Ivar, PropertyIvarLoc); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1033 | |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1034 | if (CompleteTypeErr || !compat) |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1035 | PIDecl->setInvalidDecl(); |
| 1036 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1037 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
| 1038 | getterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1039 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 1040 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1041 | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
| 1042 | // returned by the getter as it must conform to C++'s copy-return rules. |
| 1043 | // FIXME. Eventually we want to do this for Objective-C as well. |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1044 | SynthesizedFunctionScope Scope(*this, getterMethod); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1045 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
| 1046 | DeclRefExpr *SelfExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1047 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1048 | VK_RValue, PropertyDiagLoc); |
| 1049 | MarkDeclRefReferenced(SelfExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1050 | Expr *IvarRefExpr = |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1051 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc, |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1052 | SelfExpr, true, true); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1053 | ExprResult Res = |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1054 | PerformCopyInitialization(InitializedEntity::InitializeResult( |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1055 | PropertyDiagLoc, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 1056 | getterMethod->getResultType(), |
| 1057 | /*NRVO=*/false), |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1058 | PropertyDiagLoc, |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1059 | Owned(IvarRefExpr)); |
| 1060 | if (!Res.isInvalid()) { |
| 1061 | Expr *ResExpr = Res.takeAs<Expr>(); |
| 1062 | if (ResExpr) |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1063 | ResExpr = MaybeCreateExprWithCleanups(ResExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1064 | PIDecl->setGetterCXXConstructor(ResExpr); |
| 1065 | } |
| 1066 | } |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1067 | if (property->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1068 | !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 1069 | Diag(getterMethod->getLocation(), |
| 1070 | diag::warn_property_getter_owning_mismatch); |
| 1071 | Diag(property->getLocation(), diag::note_property_declare); |
| 1072 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1073 | } |
| 1074 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
| 1075 | setterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1076 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
| 1077 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1078 | // FIXME. Eventually we want to do this for Objective-C as well. |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1079 | SynthesizedFunctionScope Scope(*this, setterMethod); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1080 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
| 1081 | DeclRefExpr *SelfExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1082 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1083 | VK_RValue, PropertyDiagLoc); |
| 1084 | MarkDeclRefReferenced(SelfExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1085 | Expr *lhs = |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1086 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc, |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1087 | SelfExpr, true, true); |
| 1088 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
| 1089 | ParmVarDecl *Param = (*P); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1090 | QualType T = Param->getType().getNonReferenceType(); |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1091 | DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T, |
| 1092 | VK_LValue, PropertyDiagLoc); |
| 1093 | MarkDeclRefReferenced(rhs); |
| 1094 | ExprResult Res = BuildBinOp(S, PropertyDiagLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1095 | BO_Assign, lhs, rhs); |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1096 | if (property->getPropertyAttributes() & |
| 1097 | ObjCPropertyDecl::OBJC_PR_atomic) { |
| 1098 | Expr *callExpr = Res.takeAs<Expr>(); |
| 1099 | if (const CXXOperatorCallExpr *CXXCE = |
Fariborz Jahanian | 13bf633 | 2011-10-07 21:08:14 +0000 | [diff] [blame] | 1100 | dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) |
| 1101 | if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1102 | if (!FuncDecl->isTrivial()) |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1103 | if (property->getType()->isReferenceType()) { |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1104 | Diag(PropertyDiagLoc, |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1105 | diag::err_atomic_property_nontrivial_assign_op) |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1106 | << property->getType(); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1107 | Diag(FuncDecl->getLocStart(), |
| 1108 | diag::note_callee_decl) << FuncDecl; |
| 1109 | } |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1110 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1111 | PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1115 | if (IC) { |
| 1116 | if (Synthesize) |
| 1117 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1118 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 1119 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 1120 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1121 | << PropertyIvar; |
| 1122 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1123 | } |
| 1124 | |
| 1125 | if (ObjCPropertyImplDecl *PPIDecl |
| 1126 | = IC->FindPropertyImplDecl(PropertyId)) { |
| 1127 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 1128 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1129 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1130 | } |
| 1131 | IC->addPropertyImplementation(PIDecl); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1132 | if (getLangOpts().ObjCDefaultSynthProperties && |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1133 | getLangOpts().ObjCRuntime.isNonFragile() && |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1134 | !IDecl->isObjCRequiresPropertyDefs()) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1135 | // Diagnose if an ivar was lazily synthesdized due to a previous |
| 1136 | // use and if 1) property is @dynamic or 2) property is synthesized |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1137 | // but it requires an ivar of different name. |
Fariborz Jahanian | 411c25c | 2011-01-20 23:34:25 +0000 | [diff] [blame] | 1138 | ObjCInterfaceDecl *ClassDeclared=0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1139 | ObjCIvarDecl *Ivar = 0; |
| 1140 | if (!Synthesize) |
| 1141 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1142 | else { |
| 1143 | if (PropertyIvar && PropertyIvar != PropertyId) |
| 1144 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1145 | } |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1146 | // Issue diagnostics only if Ivar belongs to current class. |
| 1147 | if (Ivar && Ivar->getSynthesize() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1148 | declaresSameEntity(IC->getClassInterface(), ClassDeclared)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1149 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) |
| 1150 | << PropertyId; |
| 1151 | Ivar->setInvalidDecl(); |
| 1152 | } |
| 1153 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1154 | } else { |
| 1155 | if (Synthesize) |
| 1156 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1157 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1158 | Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1159 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1160 | << PropertyIvar; |
| 1161 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1162 | } |
| 1163 | |
| 1164 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1165 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1166 | Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1167 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1168 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1169 | } |
| 1170 | CatImplClass->addPropertyImplementation(PIDecl); |
| 1171 | } |
| 1172 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1173 | return PIDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | //===----------------------------------------------------------------------===// |
| 1177 | // Helper methods. |
| 1178 | //===----------------------------------------------------------------------===// |
| 1179 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1180 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 1181 | /// attributes and types and warns on a variety of inconsistencies. |
| 1182 | /// |
| 1183 | void |
| 1184 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 1185 | ObjCPropertyDecl *SuperProperty, |
| 1186 | const IdentifierInfo *inheritedName) { |
| 1187 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1188 | Property->getPropertyAttributes(); |
| 1189 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 1190 | SuperProperty->getPropertyAttributes(); |
| 1191 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1192 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1193 | Diag(Property->getLocation(), diag::warn_readonly_property) |
| 1194 | << Property->getDeclName() << inheritedName; |
| 1195 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 1196 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
| 1197 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1198 | << Property->getDeclName() << "copy" << inheritedName; |
Fariborz Jahanian | 1b46d8d | 2011-10-08 17:45:33 +0000 | [diff] [blame] | 1199 | else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){ |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1200 | unsigned CAttrRetain = |
| 1201 | (CAttr & |
| 1202 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1203 | unsigned SAttrRetain = |
| 1204 | (SAttr & |
| 1205 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1206 | bool CStrong = (CAttrRetain != 0); |
| 1207 | bool SStrong = (SAttrRetain != 0); |
| 1208 | if (CStrong != SStrong) |
| 1209 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1210 | << Property->getDeclName() << "retain (or strong)" << inheritedName; |
| 1211 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1212 | |
| 1213 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 1214 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
| 1215 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1216 | << Property->getDeclName() << "atomic" << inheritedName; |
| 1217 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
| 1218 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1219 | << Property->getDeclName() << "setter" << inheritedName; |
| 1220 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
| 1221 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1222 | << Property->getDeclName() << "getter" << inheritedName; |
| 1223 | |
| 1224 | QualType LHSType = |
| 1225 | Context.getCanonicalType(SuperProperty->getType()); |
| 1226 | QualType RHSType = |
| 1227 | Context.getCanonicalType(Property->getType()); |
| 1228 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 1229 | if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
Fariborz Jahanian | 8beb6a2 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1230 | // Do cases not handled in above. |
| 1231 | // FIXME. For future support of covariant property types, revisit this. |
| 1232 | bool IncompatibleObjC = false; |
| 1233 | QualType ConvertedType; |
| 1234 | if (!isObjCPointerConversion(RHSType, LHSType, |
| 1235 | ConvertedType, IncompatibleObjC) || |
Fariborz Jahanian | 13546a8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1236 | IncompatibleObjC) { |
Fariborz Jahanian | 8beb6a2 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1237 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 1238 | << Property->getType() << SuperProperty->getType() << inheritedName; |
Fariborz Jahanian | 13546a8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1239 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1240 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 1245 | ObjCMethodDecl *GetterMethod, |
| 1246 | SourceLocation Loc) { |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1247 | if (!GetterMethod) |
| 1248 | return false; |
| 1249 | QualType GetterType = GetterMethod->getResultType().getNonReferenceType(); |
| 1250 | QualType PropertyIvarType = property->getType().getNonReferenceType(); |
| 1251 | bool compat = Context.hasSameType(PropertyIvarType, GetterType); |
| 1252 | if (!compat) { |
| 1253 | if (isa<ObjCObjectPointerType>(PropertyIvarType) && |
| 1254 | isa<ObjCObjectPointerType>(GetterType)) |
| 1255 | compat = |
| 1256 | Context.canAssignObjCInterfaces( |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 1257 | GetterType->getAs<ObjCObjectPointerType>(), |
| 1258 | PropertyIvarType->getAs<ObjCObjectPointerType>()); |
| 1259 | else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType) |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1260 | != Compatible) { |
| 1261 | Diag(Loc, diag::error_property_accessor_type) |
| 1262 | << property->getDeclName() << PropertyIvarType |
| 1263 | << GetterMethod->getSelector() << GetterType; |
| 1264 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1265 | return true; |
| 1266 | } else { |
| 1267 | compat = true; |
| 1268 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
| 1269 | QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType(); |
| 1270 | if (lhsType != rhsType && lhsType->isArithmeticType()) |
| 1271 | compat = false; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1272 | } |
| 1273 | } |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1274 | |
| 1275 | if (!compat) { |
| 1276 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
| 1277 | << property->getDeclName() |
| 1278 | << GetterMethod->getSelector(); |
| 1279 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1280 | return true; |
| 1281 | } |
| 1282 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1283 | return false; |
| 1284 | } |
| 1285 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1286 | /// MatchOneProtocolPropertiesInClass - This routine goes thru the list |
| 1287 | /// of properties declared in a protocol and compares their attribute against |
| 1288 | /// the same property declared in the class or category. |
| 1289 | void |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1290 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) { |
| 1291 | if (!CDecl) |
| 1292 | return; |
| 1293 | |
| 1294 | // Category case. |
| 1295 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1296 | // FIXME: We should perform this check when the property in the category |
| 1297 | // is declared. |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1298 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); |
| 1299 | if (!CatDecl->IsClassExtension()) |
| 1300 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1301 | E = PDecl->prop_end(); P != E; ++P) { |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1302 | ObjCPropertyDecl *ProtoProp = *P; |
| 1303 | DeclContext::lookup_result R |
| 1304 | = CatDecl->lookup(ProtoProp->getDeclName()); |
| 1305 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 1306 | if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) { |
| 1307 | if (CatProp != ProtoProp) { |
| 1308 | // Property protocol already exist in class. Diagnose any mismatch. |
| 1309 | DiagnosePropertyMismatch(CatProp, ProtoProp, |
| 1310 | PDecl->getIdentifier()); |
| 1311 | } |
| 1312 | } |
| 1313 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1314 | } |
| 1315 | return; |
| 1316 | } |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1317 | |
| 1318 | // Class |
| 1319 | // FIXME: We should perform this check when the property in the class |
| 1320 | // is declared. |
| 1321 | ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1322 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1323 | E = PDecl->prop_end(); P != E; ++P) { |
| 1324 | ObjCPropertyDecl *ProtoProp = *P; |
| 1325 | DeclContext::lookup_result R |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame^] | 1326 | = IDecl->lookup(ProtoProp->getDeclName()); |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1327 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 1328 | if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) { |
| 1329 | if (ClassProp != ProtoProp) { |
| 1330 | // Property protocol already exist in class. Diagnose any mismatch. |
| 1331 | DiagnosePropertyMismatch(ClassProp, ProtoProp, |
| 1332 | PDecl->getIdentifier()); |
| 1333 | } |
| 1334 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1335 | } |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1336 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | /// CompareProperties - This routine compares properties |
| 1340 | /// declared in 'ClassOrProtocol' objects (which can be a class or an |
| 1341 | /// inherited protocol with the list of properties for class/category 'CDecl' |
| 1342 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1343 | void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) { |
| 1344 | Decl *ClassDecl = ClassOrProtocol; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1345 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 1346 | |
| 1347 | if (!IDecl) { |
| 1348 | // Category |
| 1349 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 1350 | assert (CatDecl && "CompareProperties"); |
| 1351 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 1352 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 1353 | E = MDecl->protocol_end(); P != E; ++P) |
| 1354 | // Match properties of category with those of protocol (*P) |
| 1355 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 1356 | |
| 1357 | // Go thru the list of protocols for this category and recursively match |
| 1358 | // their properties with those in the category. |
| 1359 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), |
| 1360 | E = CatDecl->protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1361 | CompareProperties(CatDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1362 | } else { |
| 1363 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 1364 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 1365 | E = MD->protocol_end(); P != E; ++P) |
| 1366 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 1367 | } |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1372 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1373 | P = MDecl->all_referenced_protocol_begin(), |
| 1374 | E = MDecl->all_referenced_protocol_end(); P != E; ++P) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1375 | // Match properties of class IDecl with those of protocol (*P). |
| 1376 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 1377 | |
| 1378 | // Go thru the list of protocols for this class and recursively match |
| 1379 | // their properties with those declared in the class. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1380 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1381 | P = IDecl->all_referenced_protocol_begin(), |
| 1382 | E = IDecl->all_referenced_protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1383 | CompareProperties(IDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1384 | } else { |
| 1385 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 1386 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 1387 | E = MD->protocol_end(); P != E; ++P) |
| 1388 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 1393 | /// for the property in the class and in its categories and implementations |
| 1394 | /// |
| 1395 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
| 1396 | ObjCInterfaceDecl *IDecl) { |
| 1397 | // by far the most common case. |
| 1398 | if (!PDecl->isReadOnly()) |
| 1399 | return false; |
| 1400 | // Even if property is ready only, if interface has a user defined setter, |
| 1401 | // it is not considered read only. |
| 1402 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1403 | return false; |
| 1404 | |
| 1405 | // Main class has the property as 'readonly'. Must search |
| 1406 | // through the category list to see if the property's |
| 1407 | // attribute has been over-ridden to 'readwrite'. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1408 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1409 | Cat = IDecl->visible_categories_begin(), |
| 1410 | CatEnd = IDecl->visible_categories_end(); |
| 1411 | Cat != CatEnd; ++Cat) { |
| 1412 | if (Cat->getInstanceMethod(PDecl->getSetterName())) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1413 | return false; |
| 1414 | ObjCPropertyDecl *P = |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1415 | Cat->FindPropertyDeclaration(PDecl->getIdentifier()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1416 | if (P && !P->isReadOnly()) |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | // Also, check for definition of a setter method in the implementation if |
| 1421 | // all else failed. |
| 1422 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
| 1423 | if (ObjCImplementationDecl *IMD = |
| 1424 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
| 1425 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
| 1426 | return false; |
| 1427 | } else if (ObjCCategoryImplDecl *CIMD = |
| 1428 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1429 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
| 1430 | return false; |
| 1431 | } |
| 1432 | } |
| 1433 | // Lastly, look through the implementation (if one is in scope). |
| 1434 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) |
| 1435 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1436 | return false; |
| 1437 | // If all fails, look at the super class. |
| 1438 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 1439 | return isPropertyReadonly(PDecl, SIDecl); |
| 1440 | return true; |
| 1441 | } |
| 1442 | |
| 1443 | /// CollectImmediateProperties - This routine collects all properties in |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1444 | /// the class and its conforming protocols; but not those in its super class. |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1445 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, |
Anna Zaks | e63aedd | 2012-10-31 01:18:22 +0000 | [diff] [blame] | 1446 | ObjCContainerDecl::PropertyMap &PropMap, |
| 1447 | ObjCContainerDecl::PropertyMap &SuperPropMap) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1448 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1449 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1450 | E = IDecl->prop_end(); P != E; ++P) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1451 | ObjCPropertyDecl *Prop = *P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1452 | PropMap[Prop->getIdentifier()] = Prop; |
| 1453 | } |
| 1454 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1455 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1456 | PI = IDecl->all_referenced_protocol_begin(), |
| 1457 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1458 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1459 | } |
| 1460 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1461 | if (!CATDecl->IsClassExtension()) |
| 1462 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), |
| 1463 | E = CATDecl->prop_end(); P != E; ++P) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1464 | ObjCPropertyDecl *Prop = *P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1465 | PropMap[Prop->getIdentifier()] = Prop; |
| 1466 | } |
| 1467 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1468 | for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1469 | E = CATDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1470 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1471 | } |
| 1472 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1473 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1474 | E = PDecl->prop_end(); P != E; ++P) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1475 | ObjCPropertyDecl *Prop = *P; |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1476 | ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; |
| 1477 | // Exclude property for protocols which conform to class's super-class, |
| 1478 | // as super-class has to implement the property. |
Fariborz Jahanian | a929ec7 | 2011-09-27 00:23:52 +0000 | [diff] [blame] | 1479 | if (!PropertyFromSuper || |
| 1480 | PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1481 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; |
| 1482 | if (!PropEntry) |
| 1483 | PropEntry = Prop; |
| 1484 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1485 | } |
| 1486 | // scan through protocol's protocols. |
| 1487 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1488 | E = PDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1489 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1493 | /// CollectSuperClassPropertyImplementations - This routine collects list of |
| 1494 | /// properties to be implemented in super class(s) and also coming from their |
| 1495 | /// conforming protocols. |
| 1496 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
Anna Zaks | e63aedd | 2012-10-31 01:18:22 +0000 | [diff] [blame] | 1497 | ObjCInterfaceDecl::PropertyMap &PropMap) { |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1498 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
| 1499 | while (SDecl) { |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1500 | SDecl->collectPropertiesToImplement(PropMap); |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1501 | SDecl = SDecl->getSuperClass(); |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1506 | /// \brief Default synthesizes all properties which must be synthesized |
| 1507 | /// in class's \@implementation. |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1508 | void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl, |
| 1509 | ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1510 | |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1511 | ObjCInterfaceDecl::PropertyMap PropMap; |
| 1512 | IDecl->collectPropertiesToImplement(PropMap); |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1513 | if (PropMap.empty()) |
| 1514 | return; |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1515 | ObjCInterfaceDecl::PropertyMap SuperPropMap; |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1516 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1517 | |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1518 | for (ObjCInterfaceDecl::PropertyMap::iterator |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1519 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1520 | ObjCPropertyDecl *Prop = P->second; |
| 1521 | // If property to be implemented in the super class, ignore. |
| 1522 | if (SuperPropMap[Prop->getIdentifier()]) |
| 1523 | continue; |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1524 | // Is there a matching property synthesize/dynamic? |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1525 | if (Prop->isInvalidDecl() || |
| 1526 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 1527 | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) |
| 1528 | continue; |
Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 1529 | // Property may have been synthesized by user. |
| 1530 | if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) |
| 1531 | continue; |
Fariborz Jahanian | 95f1b86 | 2010-08-25 00:31:58 +0000 | [diff] [blame] | 1532 | if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { |
| 1533 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1534 | continue; |
| 1535 | if (IMPDecl->getInstanceMethod(Prop->getSetterName())) |
| 1536 | continue; |
| 1537 | } |
Fariborz Jahanian | f8aba8c | 2011-12-15 01:03:18 +0000 | [diff] [blame] | 1538 | if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) { |
| 1539 | // We won't auto-synthesize properties declared in protocols. |
| 1540 | Diag(IMPDecl->getLocation(), |
| 1541 | diag::warn_auto_synthesizing_protocol_property); |
| 1542 | Diag(Prop->getLocation(), diag::note_property_declare); |
| 1543 | continue; |
| 1544 | } |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1545 | |
| 1546 | // We use invalid SourceLocations for the synthesized ivars since they |
| 1547 | // aren't really synthesized at a particular location; they just exist. |
| 1548 | // Saying that they are located at the @implementation isn't really going |
| 1549 | // to help users. |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1550 | ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( |
| 1551 | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), |
| 1552 | true, |
| 1553 | /* property = */ Prop->getIdentifier(), |
Anna Zaks | ad0ce53 | 2012-09-27 19:45:11 +0000 | [diff] [blame] | 1554 | /* ivar = */ Prop->getDefaultSynthIvarName(Context), |
Argyrios Kyrtzidis | 390fff8 | 2012-06-08 02:16:11 +0000 | [diff] [blame] | 1555 | Prop->getLocation())); |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1556 | if (PIDecl) { |
| 1557 | Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis); |
Fariborz Jahanian | 5ea6661 | 2012-05-08 18:03:39 +0000 | [diff] [blame] | 1558 | Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1559 | } |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1560 | } |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1561 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1562 | |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1563 | void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1564 | if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile()) |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1565 | return; |
| 1566 | ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); |
| 1567 | if (!IC) |
| 1568 | return; |
| 1569 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1570 | if (!IDecl->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | eb4f2c5 | 2012-01-03 19:46:00 +0000 | [diff] [blame] | 1571 | DefaultSynthesizeProperties(S, IC, IDecl); |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1574 | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1575 | ObjCContainerDecl *CDecl, |
Benjamin Kramer | 811bfcd | 2012-05-27 13:28:52 +0000 | [diff] [blame] | 1576 | const SelectorSet &InsMap) { |
Fariborz Jahanian | 277076a | 2012-12-19 18:58:55 +0000 | [diff] [blame] | 1577 | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
| 1578 | ObjCInterfaceDecl *IDecl; |
| 1579 | // Gather properties which need not be implemented in this class |
| 1580 | // or category. |
| 1581 | if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))) |
| 1582 | if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1583 | // For categories, no need to implement properties declared in |
| 1584 | // its primary class (and its super classes) if property is |
| 1585 | // declared in one of those containers. |
| 1586 | if ((IDecl = C->getClassInterface())) |
| 1587 | IDecl->collectPropertiesToImplement(NoNeedToImplPropMap); |
| 1588 | } |
| 1589 | if (IDecl) |
| 1590 | CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap); |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1591 | |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1592 | ObjCContainerDecl::PropertyMap PropMap; |
Fariborz Jahanian | 277076a | 2012-12-19 18:58:55 +0000 | [diff] [blame] | 1593 | CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1594 | if (PropMap.empty()) |
| 1595 | return; |
| 1596 | |
| 1597 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 1598 | for (ObjCImplDecl::propimpl_iterator |
| 1599 | I = IMPDecl->propimpl_begin(), |
| 1600 | EI = IMPDecl->propimpl_end(); I != EI; ++I) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1601 | PropImplMap.insert(I->getPropertyDecl()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1602 | |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1603 | for (ObjCContainerDecl::PropertyMap::iterator |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1604 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1605 | ObjCPropertyDecl *Prop = P->second; |
| 1606 | // Is there a matching propery synthesize/dynamic? |
| 1607 | if (Prop->isInvalidDecl() || |
| 1608 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
Douglas Gregor | 7cdc457 | 2013-01-08 18:16:18 +0000 | [diff] [blame] | 1609 | PropImplMap.count(Prop) || |
| 1610 | Prop->getAvailability() == AR_Unavailable) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1611 | continue; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1612 | if (!InsMap.count(Prop->getGetterName())) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1613 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1614 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1615 | diag::warn_setter_getter_impl_required_in_category : |
| 1616 | diag::warn_setter_getter_impl_required) |
| 1617 | << Prop->getDeclName() << Prop->getGetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1618 | Diag(Prop->getLocation(), |
| 1619 | diag::note_property_declare); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1620 | if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1621 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1622 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1623 | Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
| 1624 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1628 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1629 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1630 | diag::warn_setter_getter_impl_required_in_category : |
| 1631 | diag::warn_setter_getter_impl_required) |
| 1632 | << Prop->getDeclName() << Prop->getSetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1633 | Diag(Prop->getLocation(), |
| 1634 | diag::note_property_declare); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1635 | if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1636 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1637 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1638 | Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | void |
| 1644 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
| 1645 | ObjCContainerDecl* IDecl) { |
| 1646 | // Rules apply in non-GC mode only |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1647 | if (getLangOpts().getGC() != LangOptions::NonGC) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), |
| 1650 | E = IDecl->prop_end(); |
| 1651 | I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1652 | ObjCPropertyDecl *Property = *I; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1653 | ObjCMethodDecl *GetterMethod = 0; |
| 1654 | ObjCMethodDecl *SetterMethod = 0; |
| 1655 | bool LookedUpGetterSetter = false; |
| 1656 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1657 | unsigned Attributes = Property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1658 | unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1659 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1660 | if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && |
| 1661 | !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1662 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1663 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1664 | LookedUpGetterSetter = true; |
| 1665 | if (GetterMethod) { |
| 1666 | Diag(GetterMethod->getLocation(), |
| 1667 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1668 | << Property->getIdentifier() << 0; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1669 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1670 | } |
| 1671 | if (SetterMethod) { |
| 1672 | Diag(SetterMethod->getLocation(), |
| 1673 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1674 | << Property->getIdentifier() << 1; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1675 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1676 | } |
| 1677 | } |
| 1678 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1679 | // We only care about readwrite atomic property. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1680 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 1681 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1682 | continue; |
| 1683 | if (const ObjCPropertyImplDecl *PIDecl |
| 1684 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { |
| 1685 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1686 | continue; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1687 | if (!LookedUpGetterSetter) { |
| 1688 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1689 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1690 | LookedUpGetterSetter = true; |
| 1691 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1692 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 1693 | SourceLocation MethodLoc = |
| 1694 | (GetterMethod ? GetterMethod->getLocation() |
| 1695 | : SetterMethod->getLocation()); |
| 1696 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
Fariborz Jahanian | 7d65f69 | 2011-10-06 23:47:58 +0000 | [diff] [blame] | 1697 | << Property->getIdentifier() << (GetterMethod != 0) |
| 1698 | << (SetterMethod != 0); |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 1699 | // fixit stuff. |
| 1700 | if (!AttributesAsWritten) { |
| 1701 | if (Property->getLParenLoc().isValid()) { |
| 1702 | // @property () ... case. |
| 1703 | SourceRange PropSourceRange(Property->getAtLoc(), |
| 1704 | Property->getLParenLoc()); |
| 1705 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1706 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic"); |
| 1707 | } |
| 1708 | else { |
| 1709 | //@property id etc. |
| 1710 | SourceLocation endLoc = |
| 1711 | Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| 1712 | endLoc = endLoc.getLocWithOffset(-1); |
| 1713 | SourceRange PropSourceRange(Property->getAtLoc(), endLoc); |
| 1714 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1715 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) "); |
| 1716 | } |
| 1717 | } |
| 1718 | else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) { |
| 1719 | // @property () ... case. |
| 1720 | SourceLocation endLoc = Property->getLParenLoc(); |
| 1721 | SourceRange PropSourceRange(Property->getAtLoc(), endLoc); |
| 1722 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1723 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, "); |
| 1724 | } |
| 1725 | else |
| 1726 | Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1727 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | } |
| 1732 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1733 | void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1734 | if (getLangOpts().getGC() == LangOptions::GCOnly) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1735 | return; |
| 1736 | |
| 1737 | for (ObjCImplementationDecl::propimpl_iterator |
| 1738 | i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1739 | ObjCPropertyImplDecl *PID = *i; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1740 | if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 1741 | continue; |
| 1742 | |
| 1743 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1744 | if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1745 | !D->getInstanceMethod(PD->getGetterName())) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1746 | ObjCMethodDecl *method = PD->getGetterMethodDecl(); |
| 1747 | if (!method) |
| 1748 | continue; |
| 1749 | ObjCMethodFamily family = method->getMethodFamily(); |
| 1750 | if (family == OMF_alloc || family == OMF_copy || |
| 1751 | family == OMF_mutableCopy || family == OMF_new) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1752 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1753 | Diag(PID->getLocation(), diag::err_ownin_getter_rule); |
| 1754 | else |
Ted Kremenek | 920c9c1 | 2011-10-12 18:03:37 +0000 | [diff] [blame] | 1755 | Diag(PID->getLocation(), diag::warn_owning_getter_rule); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1756 | Diag(PD->getLocation(), diag::note_property_declare); |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1762 | /// AddPropertyAttrs - Propagates attributes from a property to the |
| 1763 | /// implicitly-declared getter or setter for that property. |
| 1764 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
| 1765 | ObjCPropertyDecl *Property) { |
| 1766 | // Should we just clone all attributes over? |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1767 | for (Decl::attr_iterator A = Property->attr_begin(), |
| 1768 | AEnd = Property->attr_end(); |
| 1769 | A != AEnd; ++A) { |
| 1770 | if (isa<DeprecatedAttr>(*A) || |
| 1771 | isa<UnavailableAttr>(*A) || |
| 1772 | isa<AvailabilityAttr>(*A)) |
| 1773 | PropertyMethod->addAttr((*A)->clone(S.Context)); |
| 1774 | } |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1777 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 1778 | /// have the property type and issue diagnostics if they don't. |
| 1779 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1780 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1781 | /// methods is the "right" thing to do. |
| 1782 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1783 | ObjCContainerDecl *CD, |
| 1784 | ObjCPropertyDecl *redeclaredProperty, |
| 1785 | ObjCContainerDecl *lexicalDC) { |
| 1786 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1787 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 1788 | |
| 1789 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
| 1790 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
| 1791 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
| 1792 | property->getLocation()); |
| 1793 | |
| 1794 | if (SetterMethod) { |
| 1795 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1796 | property->getPropertyAttributes(); |
| 1797 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && |
| 1798 | Context.getCanonicalType(SetterMethod->getResultType()) != |
| 1799 | Context.VoidTy) |
| 1800 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
| 1801 | if (SetterMethod->param_size() != 1 || |
Fariborz Jahanian | 2aac0c9 | 2011-09-26 22:59:09 +0000 | [diff] [blame] | 1802 | !Context.hasSameUnqualifiedType( |
Fariborz Jahanian | bb13c32 | 2011-10-15 17:36:49 +0000 | [diff] [blame] | 1803 | (*SetterMethod->param_begin())->getType().getNonReferenceType(), |
| 1804 | property->getType().getNonReferenceType())) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1805 | Diag(property->getLocation(), |
| 1806 | diag::warn_accessor_property_type_mismatch) |
| 1807 | << property->getDeclName() |
| 1808 | << SetterMethod->getSelector(); |
| 1809 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | // Synthesize getter/setter methods if none exist. |
| 1814 | // Find the default getter and if one not found, add one. |
| 1815 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 1816 | // synthesize these methods unless the user explicitly provided prototypes |
| 1817 | // (which is odd, but allowed). Sema should be typechecking that the |
| 1818 | // declarations jive in that situation (which it is not currently). |
| 1819 | if (!GetterMethod) { |
| 1820 | // No instance method of same name as property getter name was found. |
| 1821 | // Declare a getter method and add it to the list of methods |
| 1822 | // for this class. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1823 | SourceLocation Loc = redeclaredProperty ? |
| 1824 | redeclaredProperty->getLocation() : |
| 1825 | property->getLocation(); |
| 1826 | |
| 1827 | GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, |
| 1828 | property->getGetterName(), |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1829 | property->getType(), 0, CD, /*isInstance=*/true, |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1830 | /*isVariadic=*/false, /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1831 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1832 | (property->getPropertyImplementation() == |
| 1833 | ObjCPropertyDecl::Optional) ? |
| 1834 | ObjCMethodDecl::Optional : |
| 1835 | ObjCMethodDecl::Required); |
| 1836 | CD->addDecl(GetterMethod); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1837 | |
| 1838 | AddPropertyAttrs(*this, GetterMethod, property); |
| 1839 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1840 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1841 | // and the real context should be the same. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1842 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1843 | GetterMethod->setLexicalDeclContext(lexicalDC); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1844 | if (property->hasAttr<NSReturnsNotRetainedAttr>()) |
| 1845 | GetterMethod->addAttr( |
| 1846 | ::new (Context) NSReturnsNotRetainedAttr(Loc, Context)); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1847 | } else |
| 1848 | // A user declared getter will be synthesize when @synthesize of |
| 1849 | // the property with the same name is seen in the @implementation |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1850 | GetterMethod->setPropertyAccessor(true); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1851 | property->setGetterMethodDecl(GetterMethod); |
| 1852 | |
| 1853 | // Skip setter if property is read-only. |
| 1854 | if (!property->isReadOnly()) { |
| 1855 | // Find the default setter and if one not found, add one. |
| 1856 | if (!SetterMethod) { |
| 1857 | // No instance method of same name as property setter name was found. |
| 1858 | // Declare a setter method and add it to the list of methods |
| 1859 | // for this class. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1860 | SourceLocation Loc = redeclaredProperty ? |
| 1861 | redeclaredProperty->getLocation() : |
| 1862 | property->getLocation(); |
| 1863 | |
| 1864 | SetterMethod = |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 1865 | ObjCMethodDecl::Create(Context, Loc, Loc, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1866 | property->getSetterName(), Context.VoidTy, 0, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1867 | CD, /*isInstance=*/true, /*isVariadic=*/false, |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1868 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1869 | /*isImplicitlyDeclared=*/true, |
| 1870 | /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1871 | (property->getPropertyImplementation() == |
| 1872 | ObjCPropertyDecl::Optional) ? |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1873 | ObjCMethodDecl::Optional : |
| 1874 | ObjCMethodDecl::Required); |
| 1875 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1876 | // Invent the arguments for the setter. We don't bother making a |
| 1877 | // nice name for the argument. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1878 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
| 1879 | Loc, Loc, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1880 | property->getIdentifier(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1881 | property->getType().getUnqualifiedType(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1882 | /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1883 | SC_None, |
| 1884 | SC_None, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1885 | 0); |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 1886 | SetterMethod->setMethodParams(Context, Argument, |
| 1887 | ArrayRef<SourceLocation>()); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1888 | |
| 1889 | AddPropertyAttrs(*this, SetterMethod, property); |
| 1890 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1891 | CD->addDecl(SetterMethod); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1892 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1893 | // and the real context should be the same. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1894 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1895 | SetterMethod->setLexicalDeclContext(lexicalDC); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1896 | } else |
| 1897 | // A user declared setter will be synthesize when @synthesize of |
| 1898 | // the property with the same name is seen in the @implementation |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 1899 | SetterMethod->setPropertyAccessor(true); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1900 | property->setSetterMethodDecl(SetterMethod); |
| 1901 | } |
| 1902 | // Add any synthesized methods to the global pool. This allows us to |
| 1903 | // handle the following, which is supported by GCC (and part of the design). |
| 1904 | // |
| 1905 | // @interface Foo |
| 1906 | // @property double bar; |
| 1907 | // @end |
| 1908 | // |
| 1909 | // void thisIsUnfortunate() { |
| 1910 | // id foo; |
| 1911 | // double bar = [foo bar]; |
| 1912 | // } |
| 1913 | // |
| 1914 | if (GetterMethod) |
| 1915 | AddInstanceMethodToGlobalPool(GetterMethod); |
| 1916 | if (SetterMethod) |
| 1917 | AddInstanceMethodToGlobalPool(SetterMethod); |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 1918 | |
| 1919 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); |
| 1920 | if (!CurrentClass) { |
| 1921 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) |
| 1922 | CurrentClass = Cat->getClassInterface(); |
| 1923 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) |
| 1924 | CurrentClass = Impl->getClassInterface(); |
| 1925 | } |
| 1926 | if (GetterMethod) |
| 1927 | CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); |
| 1928 | if (SetterMethod) |
| 1929 | CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1932 | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1933 | SourceLocation Loc, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1934 | unsigned &Attributes, |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 1935 | bool propertyInPrimaryClass) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1936 | // FIXME: Improve the reported location. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1937 | if (!PDecl || PDecl->isInvalidDecl()) |
Ted Kremenek | 5fcd52a | 2010-04-05 22:39:42 +0000 | [diff] [blame] | 1938 | return; |
| 1939 | |
| 1940 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1941 | QualType PropertyTy = PropertyDecl->getType(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1942 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1943 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1944 | (Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 1945 | PropertyTy->isObjCRetainableType()) { |
| 1946 | // 'readonly' property with no obvious lifetime. |
| 1947 | // its life time will be determined by its backing ivar. |
| 1948 | unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 1949 | ObjCDeclSpec::DQ_PR_copy | |
| 1950 | ObjCDeclSpec::DQ_PR_retain | |
| 1951 | ObjCDeclSpec::DQ_PR_strong | |
| 1952 | ObjCDeclSpec::DQ_PR_weak | |
| 1953 | ObjCDeclSpec::DQ_PR_assign); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1954 | if ((Attributes & rel) == 0) |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 1955 | return; |
| 1956 | } |
| 1957 | |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 1958 | if (propertyInPrimaryClass) { |
| 1959 | // we postpone most property diagnosis until class's implementation |
| 1960 | // because, its readonly attribute may be overridden in its class |
| 1961 | // extensions making other attributes, which make no sense, to make sense. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1962 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1963 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 1964 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1965 | << "readonly" << "readwrite"; |
| 1966 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1967 | // readonly and readwrite/assign/retain/copy conflict. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1968 | else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1969 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1970 | ObjCDeclSpec::DQ_PR_assign | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1971 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1972 | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1973 | ObjCDeclSpec::DQ_PR_retain | |
| 1974 | ObjCDeclSpec::DQ_PR_strong))) { |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1975 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1976 | "readwrite" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1977 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1978 | "assign" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1979 | (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1980 | "unsafe_unretained" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1981 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1982 | "copy" : "retain"; |
| 1983 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1984 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1985 | diag::err_objc_property_attr_mutually_exclusive : |
| 1986 | diag::warn_objc_property_attr_mutually_exclusive) |
| 1987 | << "readonly" << which; |
| 1988 | } |
| 1989 | |
| 1990 | // Check for copy or retain on non-object types. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1991 | if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1992 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && |
| 1993 | !PropertyTy->isObjCRetainableType() && |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1994 | !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1995 | Diag(Loc, diag::err_objc_property_requires_object) |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1996 | << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : |
| 1997 | Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); |
| 1998 | Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1999 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); |
John McCall | 977ea78 | 2012-02-21 21:48:05 +0000 | [diff] [blame] | 2000 | PropertyDecl->setInvalidDecl(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | // Check for more than one of { assign, copy, retain }. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2004 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 2005 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2006 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2007 | << "assign" << "copy"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2008 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2009 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2010 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2011 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2012 | << "assign" << "retain"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2013 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2014 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2015 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2016 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2017 | << "assign" << "strong"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2018 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2019 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2020 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2021 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2022 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2023 | << "assign" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2024 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2025 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2026 | } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { |
| 2027 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2028 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2029 | << "unsafe_unretained" << "copy"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2030 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2031 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2032 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2033 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2034 | << "unsafe_unretained" << "retain"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2035 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2036 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2037 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2038 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2039 | << "unsafe_unretained" << "strong"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2040 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2041 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2042 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2043 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2044 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2045 | << "unsafe_unretained" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2046 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2047 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2048 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 2049 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2050 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2051 | << "copy" << "retain"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2052 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2053 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2054 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2055 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2056 | << "copy" << "strong"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2057 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2058 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2059 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2060 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2061 | << "copy" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2062 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2063 | } |
| 2064 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2065 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2066 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2067 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2068 | << "retain" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2069 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2070 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2071 | else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 2072 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2073 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2074 | << "strong" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2075 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2078 | if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && |
| 2079 | (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { |
Fariborz Jahanian | 9d1bbea | 2011-10-10 21:53:24 +0000 | [diff] [blame] | 2080 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2081 | << "atomic" << "nonatomic"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2082 | Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; |
Fariborz Jahanian | 9d1bbea | 2011-10-10 21:53:24 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2085 | // Warn if user supplied no assignment attribute, property is |
| 2086 | // readwrite, and this is an object type. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2087 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2088 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 2089 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong | |
| 2090 | ObjCDeclSpec::DQ_PR_weak)) && |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2091 | PropertyTy->isObjCObjectPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2092 | if (getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2093 | // With arc, @property definitions should default to (strong) when |
Fariborz Jahanian | f21a92d | 2011-11-08 20:58:53 +0000 | [diff] [blame] | 2094 | // not specified; including when property is 'readonly'. |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2095 | PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2096 | else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) { |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2097 | bool isAnyClassTy = |
| 2098 | (PropertyTy->isObjCClassType() || |
| 2099 | PropertyTy->isObjCQualifiedClassType()); |
| 2100 | // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to |
| 2101 | // issue any warning. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2102 | if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2103 | ; |
Fariborz Jahanian | f224fb5 | 2012-09-17 23:57:35 +0000 | [diff] [blame] | 2104 | else if (propertyInPrimaryClass) { |
| 2105 | // Don't issue warning on property with no life time in class |
| 2106 | // extension as it is inherited from property in primary class. |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2107 | // Skip this warning in gc-only mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2108 | if (getLangOpts().getGC() != LangOptions::GCOnly) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2109 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2110 | |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2111 | // If non-gc code warn that this is likely inappropriate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2112 | if (getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2113 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2114 | } |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2115 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2116 | |
| 2117 | // FIXME: Implement warning dependent on NSCopying being |
| 2118 | // implemented. See also: |
| 2119 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 2120 | // (please trim this list while you are at it). |
| 2121 | } |
| 2122 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2123 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 2124 | &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2125 | && getLangOpts().getGC() == LangOptions::GCOnly |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2126 | && PropertyTy->isBlockPointerType()) |
| 2127 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2128 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2129 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2130 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
Fariborz Jahanian | 528a499 | 2011-09-14 18:03:46 +0000 | [diff] [blame] | 2131 | PropertyTy->isBlockPointerType()) |
| 2132 | Diag(Loc, diag::warn_objc_property_retain_of_block); |
Fariborz Jahanian | 48a98c7 | 2011-11-01 23:02:16 +0000 | [diff] [blame] | 2133 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2134 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2135 | (Attributes & ObjCDeclSpec::DQ_PR_setter)) |
Fariborz Jahanian | 48a98c7 | 2011-11-01 23:02:16 +0000 | [diff] [blame] | 2136 | Diag(Loc, diag::warn_objc_readonly_property_has_setter); |
| 2137 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2138 | } |