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