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