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