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" |
Jordan Rose | a34d04d | 2015-01-16 23:04:31 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 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 | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 64 | /// Check the internal consistency of a property declaration with |
| 65 | /// an explicit ownership qualifier. |
| 66 | static void checkPropertyDeclWithOwnership(Sema &S, |
| 67 | ObjCPropertyDecl *property) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 68 | if (property->isInvalidDecl()) return; |
| 69 | |
| 70 | ObjCPropertyDecl::PropertyAttributeKind propertyKind |
| 71 | = property->getPropertyAttributes(); |
| 72 | Qualifiers::ObjCLifetime propertyLifetime |
| 73 | = property->getType().getObjCLifetime(); |
| 74 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 75 | assert(propertyLifetime != Qualifiers::OCL_None); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 76 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 77 | Qualifiers::ObjCLifetime expectedLifetime |
| 78 | = getImpliedARCOwnership(propertyKind, property->getType()); |
| 79 | if (!expectedLifetime) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 80 | // We have a lifetime qualifier but no dominating property |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 81 | // attribute. That's okay, but restore reasonable invariants by |
| 82 | // setting the property attribute according to the lifetime |
| 83 | // qualifier. |
| 84 | ObjCPropertyDecl::PropertyAttributeKind attr; |
| 85 | if (propertyLifetime == Qualifiers::OCL_Strong) { |
| 86 | attr = ObjCPropertyDecl::OBJC_PR_strong; |
| 87 | } else if (propertyLifetime == Qualifiers::OCL_Weak) { |
| 88 | attr = ObjCPropertyDecl::OBJC_PR_weak; |
| 89 | } else { |
| 90 | assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); |
| 91 | attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
| 92 | } |
| 93 | property->setPropertyAttributes(attr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (propertyLifetime == expectedLifetime) return; |
| 98 | |
| 99 | property->setInvalidDecl(); |
| 100 | S.Diag(property->getLocation(), |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 101 | diag::err_arc_inconsistent_property_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 102 | << property->getDeclName() |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 103 | << expectedLifetime |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 104 | << propertyLifetime; |
| 105 | } |
| 106 | |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 107 | /// \brief Check this Objective-C property against a property declared in the |
| 108 | /// given protocol. |
| 109 | static void |
| 110 | CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop, |
| 111 | ObjCProtocolDecl *Proto, |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 112 | llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) { |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 113 | // Have we seen this protocol before? |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 114 | if (!Known.insert(Proto).second) |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 115 | return; |
| 116 | |
| 117 | // Look for a property with the same name. |
| 118 | DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName()); |
| 119 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 120 | if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) { |
Fariborz Jahanian | b809a0e | 2013-10-04 18:06:08 +0000 | [diff] [blame] | 121 | S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true); |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Check this property against any protocols we inherit. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 127 | for (auto *P : Proto->protocols()) |
| 128 | CheckPropertyAgainstProtocol(S, Prop, P, Known); |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 129 | } |
| 130 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 131 | static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) { |
| 132 | // In GC mode, just look for the __weak qualifier. |
| 133 | if (S.getLangOpts().getGC() != LangOptions::NonGC) { |
| 134 | if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak; |
| 135 | |
| 136 | // In ARC/MRC, look for an explicit ownership qualifier. |
| 137 | // For some reason, this only applies to __weak. |
| 138 | } else if (auto ownership = T.getObjCLifetime()) { |
| 139 | switch (ownership) { |
| 140 | case Qualifiers::OCL_Weak: |
| 141 | return ObjCDeclSpec::DQ_PR_weak; |
| 142 | case Qualifiers::OCL_Strong: |
| 143 | return ObjCDeclSpec::DQ_PR_strong; |
| 144 | case Qualifiers::OCL_ExplicitNone: |
| 145 | return ObjCDeclSpec::DQ_PR_unsafe_unretained; |
| 146 | case Qualifiers::OCL_Autoreleasing: |
| 147 | case Qualifiers::OCL_None: |
| 148 | return 0; |
| 149 | } |
| 150 | llvm_unreachable("bad qualifier"); |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 156 | static const unsigned OwnershipMask = |
| 157 | (ObjCPropertyDecl::OBJC_PR_assign | |
| 158 | ObjCPropertyDecl::OBJC_PR_retain | |
| 159 | ObjCPropertyDecl::OBJC_PR_copy | |
| 160 | ObjCPropertyDecl::OBJC_PR_weak | |
| 161 | ObjCPropertyDecl::OBJC_PR_strong | |
| 162 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 163 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 164 | static unsigned getOwnershipRule(unsigned attr) { |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 165 | unsigned result = attr & OwnershipMask; |
| 166 | |
| 167 | // From an ownership perspective, assign and unsafe_unretained are |
| 168 | // identical; make sure one also implies the other. |
| 169 | if (result & (ObjCPropertyDecl::OBJC_PR_assign | |
| 170 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) { |
| 171 | result |= ObjCPropertyDecl::OBJC_PR_assign | |
| 172 | ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
| 173 | } |
| 174 | |
| 175 | return result; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 176 | } |
| 177 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 178 | Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 179 | SourceLocation LParenLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 180 | FieldDeclarator &FD, |
| 181 | ObjCDeclSpec &ODS, |
| 182 | Selector GetterSel, |
| 183 | Selector SetterSel, |
Ted Kremenek | cba5849 | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 184 | tok::ObjCKeywordKind MethodImplKind, |
| 185 | DeclContext *lexicalDC) { |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 186 | unsigned Attributes = ODS.getPropertyAttributes(); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 187 | FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 188 | TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); |
| 189 | QualType T = TSI->getType(); |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 190 | if (!getOwnershipRule(Attributes)) { |
| 191 | Attributes |= deducePropertyOwnershipFromType(*this, T); |
| 192 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 193 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 194 | // default is readwrite! |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 195 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 197 | // Proceed with constructing the ObjCPropertyDecls. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 198 | ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 199 | ObjCPropertyDecl *Res = nullptr; |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 200 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 5848d33 | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 201 | if (CDecl->IsClassExtension()) { |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 202 | Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc, |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 203 | FD, |
| 204 | GetterSel, ODS.getGetterNameLoc(), |
| 205 | SetterSel, ODS.getSetterNameLoc(), |
| 206 | isReadWrite, Attributes, |
Argyrios Kyrtzidis | b458ffb | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 207 | ODS.getPropertyAttributes(), |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 208 | T, TSI, MethodImplKind); |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 209 | if (!Res) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 210 | return nullptr; |
Fariborz Jahanian | 5848d33 | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 211 | } |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (!Res) { |
| 215 | Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD, |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 216 | GetterSel, ODS.getGetterNameLoc(), SetterSel, |
| 217 | ODS.getSetterNameLoc(), isReadWrite, Attributes, |
| 218 | ODS.getPropertyAttributes(), T, TSI, |
| 219 | MethodImplKind); |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 220 | if (lexicalDC) |
| 221 | Res->setLexicalDeclContext(lexicalDC); |
| 222 | } |
Ted Kremenek | cba5849 | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 223 | |
Fariborz Jahanian | df58603 | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 224 | // Validate the attributes on the @property. |
Douglas Gregor | d4f2afa | 2015-10-09 20:36:17 +0000 | [diff] [blame] | 225 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes, |
Fariborz Jahanian | 876cc65 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 226 | (isa<ObjCInterfaceDecl>(ClassDecl) || |
| 227 | isa<ObjCProtocolDecl>(ClassDecl))); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 228 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 229 | // Check consistency if the type has explicit ownership qualification. |
| 230 | if (Res->getType().getObjCLifetime()) |
| 231 | checkPropertyDeclWithOwnership(*this, Res); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 232 | |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 233 | llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos; |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 234 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 235 | // For a class, compare the property against a property in our superclass. |
| 236 | bool FoundInSuper = false; |
Fariborz Jahanian | 92e3aa2 | 2014-02-15 00:04:36 +0000 | [diff] [blame] | 237 | ObjCInterfaceDecl *CurrentInterfaceDecl = IFace; |
| 238 | while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) { |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 239 | DeclContext::lookup_result R = Super->lookup(Res->getDeclName()); |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 240 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 241 | if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) { |
Fariborz Jahanian | b809a0e | 2013-10-04 18:06:08 +0000 | [diff] [blame] | 242 | DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false); |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 243 | FoundInSuper = true; |
| 244 | break; |
| 245 | } |
| 246 | } |
Fariborz Jahanian | 92e3aa2 | 2014-02-15 00:04:36 +0000 | [diff] [blame] | 247 | if (FoundInSuper) |
| 248 | break; |
| 249 | else |
| 250 | CurrentInterfaceDecl = Super; |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (FoundInSuper) { |
| 254 | // Also compare the property against a property in our protocols. |
Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 255 | for (auto *P : CurrentInterfaceDecl->protocols()) { |
| 256 | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 257 | } |
| 258 | } else { |
| 259 | // Slower path: look in all protocols we referenced. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 260 | for (auto *P : IFace->all_referenced_protocols()) { |
| 261 | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 265 | // We don't check if class extension. Because properties in class extension |
| 266 | // are meant to override some of the attributes and checking has already done |
| 267 | // when property in class extension is constructed. |
| 268 | if (!Cat->IsClassExtension()) |
| 269 | for (auto *P : Cat->protocols()) |
| 270 | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
Douglas Gregor | b898209 | 2013-01-21 19:42:21 +0000 | [diff] [blame] | 271 | } else { |
| 272 | ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 273 | for (auto *P : Proto->protocols()) |
| 274 | CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 277 | ActOnDocumentableDecl(Res); |
Fariborz Jahanian | df58603 | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 278 | return Res; |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 279 | } |
Ted Kremenek | 90e2fc2 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 280 | |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 281 | static ObjCPropertyDecl::PropertyAttributeKind |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 282 | makePropertyAttributesAsWritten(unsigned Attributes) { |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 283 | unsigned attributesAsWritten = 0; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 284 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 285 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 286 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 287 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 288 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 289 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 290 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 291 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 292 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 293 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 294 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 295 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 296 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 297 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 298 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 299 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 300 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 301 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 302 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 303 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 304 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 305 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 306 | if (Attributes & ObjCDeclSpec::DQ_PR_atomic) |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 307 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic; |
Manman Ren | 387ff7f | 2016-01-26 18:52:43 +0000 | [diff] [blame] | 308 | if (Attributes & ObjCDeclSpec::DQ_PR_class) |
| 309 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class; |
Argyrios Kyrtzidis | b51684d | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 310 | |
| 311 | return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten; |
| 312 | } |
| 313 | |
Fariborz Jahanian | 19e09cb | 2012-05-21 17:10:28 +0000 | [diff] [blame] | 314 | static bool LocPropertyAttribute( ASTContext &Context, const char *attrName, |
Fariborz Jahanian | b52d8d2 | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 315 | SourceLocation LParenLoc, SourceLocation &Loc) { |
| 316 | if (LParenLoc.isMacroID()) |
| 317 | return false; |
| 318 | |
| 319 | SourceManager &SM = Context.getSourceManager(); |
| 320 | std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc); |
| 321 | // Try to load the file buffer. |
| 322 | bool invalidTemp = false; |
| 323 | StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
| 324 | if (invalidTemp) |
| 325 | return false; |
| 326 | const char *tokenBegin = file.data() + locInfo.second; |
| 327 | |
| 328 | // Lex from the start of the given location. |
| 329 | Lexer lexer(SM.getLocForStartOfFile(locInfo.first), |
| 330 | Context.getLangOpts(), |
| 331 | file.begin(), tokenBegin, file.end()); |
| 332 | Token Tok; |
| 333 | do { |
| 334 | lexer.LexFromRawLexer(Tok); |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 335 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) { |
Fariborz Jahanian | b52d8d2 | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 336 | Loc = Tok.getLocation(); |
| 337 | return true; |
| 338 | } |
| 339 | } while (Tok.isNot(tok::r_paren)); |
| 340 | return false; |
Fariborz Jahanian | 199a9b5 | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 343 | /// Check for a mismatch in the atomicity of the given properties. |
| 344 | static void checkAtomicPropertyMismatch(Sema &S, |
| 345 | ObjCPropertyDecl *OldProperty, |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 346 | ObjCPropertyDecl *NewProperty, |
| 347 | bool PropagateAtomicity) { |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 348 | // If the atomicity of both matches, we're done. |
| 349 | bool OldIsAtomic = |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 350 | (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 351 | == 0; |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 352 | bool NewIsAtomic = |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 353 | (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 354 | == 0; |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 355 | if (OldIsAtomic == NewIsAtomic) return; |
| 356 | |
| 357 | // Determine whether the given property is readonly and implicitly |
| 358 | // atomic. |
| 359 | auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool { |
| 360 | // Is it readonly? |
| 361 | auto Attrs = Property->getPropertyAttributes(); |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 362 | if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false; |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 363 | |
| 364 | // Is it nonatomic? |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 365 | if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false; |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 366 | |
| 367 | // Was 'atomic' specified directly? |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 368 | if (Property->getPropertyAttributesAsWritten() & |
| 369 | ObjCPropertyDecl::OBJC_PR_atomic) |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 370 | return false; |
| 371 | |
| 372 | return true; |
| 373 | }; |
| 374 | |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 375 | // If we're allowed to propagate atomicity, and the new property did |
| 376 | // not specify atomicity at all, propagate. |
| 377 | const unsigned AtomicityMask = |
| 378 | (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 379 | if (PropagateAtomicity && |
| 380 | ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) { |
| 381 | unsigned Attrs = NewProperty->getPropertyAttributes(); |
| 382 | Attrs = Attrs & ~AtomicityMask; |
| 383 | if (OldIsAtomic) |
| 384 | Attrs |= ObjCPropertyDecl::OBJC_PR_atomic; |
| 385 | else |
| 386 | Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic; |
| 387 | |
| 388 | NewProperty->overwritePropertyAttributes(Attrs); |
| 389 | return; |
| 390 | } |
| 391 | |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 392 | // One of the properties is atomic; if it's a readonly property, and |
| 393 | // 'atomic' wasn't explicitly specified, we're okay. |
| 394 | if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) || |
| 395 | (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty))) |
| 396 | return; |
| 397 | |
| 398 | // Diagnose the conflict. |
| 399 | const IdentifierInfo *OldContextName; |
| 400 | auto *OldDC = OldProperty->getDeclContext(); |
| 401 | if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC)) |
| 402 | OldContextName = Category->getClassInterface()->getIdentifier(); |
| 403 | else |
| 404 | OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier(); |
| 405 | |
| 406 | S.Diag(NewProperty->getLocation(), diag::warn_property_attribute) |
| 407 | << NewProperty->getDeclName() << "atomic" |
| 408 | << OldContextName; |
| 409 | S.Diag(OldProperty->getLocation(), diag::note_property_declare); |
| 410 | } |
| 411 | |
Douglas Gregor | 90d3442 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 412 | ObjCPropertyDecl * |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 413 | Sema::HandlePropertyInClassExtension(Scope *S, |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 414 | SourceLocation AtLoc, |
| 415 | SourceLocation LParenLoc, |
| 416 | FieldDeclarator &FD, |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 417 | Selector GetterSel, |
| 418 | SourceLocation GetterNameLoc, |
| 419 | Selector SetterSel, |
| 420 | SourceLocation SetterNameLoc, |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 421 | const bool isReadWrite, |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 422 | unsigned &Attributes, |
Argyrios Kyrtzidis | b458ffb | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 423 | const unsigned AttributesAsWritten, |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 424 | QualType T, |
| 425 | TypeSourceInfo *TSI, |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 426 | tok::ObjCKeywordKind MethodImplKind) { |
Fariborz Jahanian | f36734d | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 427 | ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext); |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 428 | // Diagnose if this property is already in continuation class. |
Fariborz Jahanian | 8d382dc | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 429 | DeclContext *DC = CurContext; |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 430 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Fariborz Jahanian | a0a9d85 | 2010-11-10 18:01:36 +0000 | [diff] [blame] | 431 | ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); |
| 432 | |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 433 | // We need to look in the @interface to see if the @property was |
| 434 | // already declared. |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 435 | if (!CCPrimary) { |
| 436 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 437 | return nullptr; |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 440 | bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) || |
| 441 | (Attributes & ObjCDeclSpec::DQ_PR_class); |
| 442 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 443 | // Find the property in the extended class's primary class or |
| 444 | // extensions. |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 445 | ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass( |
| 446 | PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty)); |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 447 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 448 | // If we found a property in an extension, complain. |
| 449 | if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) { |
| 450 | Diag(AtLoc, diag::err_duplicate_property); |
| 451 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 452 | return nullptr; |
| 453 | } |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 455 | // Check for consistency with the previous declaration, if there is one. |
| 456 | if (PIDecl) { |
| 457 | // A readonly property declared in the primary class can be refined |
| 458 | // by adding a readwrite property within an extension. |
| 459 | // Anything else is an error. |
| 460 | if (!(PIDecl->isReadOnly() && isReadWrite)) { |
| 461 | // Tailor the diagnostics for the common case where a readwrite |
| 462 | // property is declared both in the @interface and the continuation. |
| 463 | // This is a common error where the user often intended the original |
| 464 | // declaration to be readonly. |
| 465 | unsigned diag = |
| 466 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite) && |
| 467 | (PIDecl->getPropertyAttributesAsWritten() & |
| 468 | ObjCPropertyDecl::OBJC_PR_readwrite) |
| 469 | ? diag::err_use_continuation_class_redeclaration_readwrite |
| 470 | : diag::err_use_continuation_class; |
| 471 | Diag(AtLoc, diag) |
| 472 | << CCPrimary->getDeclName(); |
| 473 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 474 | return nullptr; |
| 475 | } |
| 476 | |
| 477 | // Check for consistency of getters. |
| 478 | if (PIDecl->getGetterName() != GetterSel) { |
| 479 | // If the getter was written explicitly, complain. |
| 480 | if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) { |
| 481 | Diag(AtLoc, diag::warn_property_redecl_getter_mismatch) |
| 482 | << PIDecl->getGetterName() << GetterSel; |
| 483 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 484 | } |
| 485 | |
| 486 | // Always adopt the getter from the original declaration. |
| 487 | GetterSel = PIDecl->getGetterName(); |
| 488 | Attributes |= ObjCDeclSpec::DQ_PR_getter; |
| 489 | } |
| 490 | |
| 491 | // Check consistency of ownership. |
| 492 | unsigned ExistingOwnership |
| 493 | = getOwnershipRule(PIDecl->getPropertyAttributes()); |
| 494 | unsigned NewOwnership = getOwnershipRule(Attributes); |
| 495 | if (ExistingOwnership && NewOwnership != ExistingOwnership) { |
| 496 | // If the ownership was written explicitly, complain. |
| 497 | if (getOwnershipRule(AttributesAsWritten)) { |
| 498 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
| 499 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 500 | } |
| 501 | |
| 502 | // Take the ownership from the original property. |
| 503 | Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership; |
| 504 | } |
| 505 | |
| 506 | // If the redeclaration is 'weak' but the original property is not, |
| 507 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) && |
| 508 | !(PIDecl->getPropertyAttributesAsWritten() |
| 509 | & ObjCPropertyDecl::OBJC_PR_weak) && |
| 510 | PIDecl->getType()->getAs<ObjCObjectPointerType>() && |
| 511 | PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) { |
| 512 | Diag(AtLoc, diag::warn_property_implicitly_mismatched); |
| 513 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 514 | } |
| 515 | } |
| 516 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 517 | // Create a new ObjCPropertyDecl with the DeclContext being |
| 518 | // the class extension. |
| 519 | ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc, |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 520 | FD, GetterSel, GetterNameLoc, |
| 521 | SetterSel, SetterNameLoc, |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 522 | isReadWrite, |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 523 | Attributes, AttributesAsWritten, |
| 524 | T, TSI, MethodImplKind, DC); |
| 525 | |
| 526 | // If there was no declaration of a property with the same name in |
| 527 | // the primary class, we're done. |
| 528 | if (!PIDecl) { |
Douglas Gregor | e17765e | 2015-11-03 17:02:34 +0000 | [diff] [blame] | 529 | ProcessPropertyDecl(PDecl); |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 530 | return PDecl; |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 531 | } |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 532 | |
Fariborz Jahanian | 6a73384 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 533 | if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) { |
| 534 | bool IncompatibleObjC = false; |
| 535 | QualType ConvertedType; |
Fariborz Jahanian | 24c2ccc | 2012-02-02 19:34:05 +0000 | [diff] [blame] | 536 | // Relax the strict type matching for property type in continuation class. |
| 537 | // Allow property object type of continuation class to be different as long |
Fariborz Jahanian | 57539cf | 2012-02-02 22:37:48 +0000 | [diff] [blame] | 538 | // as it narrows the object type in its primary class property. Note that |
| 539 | // this conversion is safe only because the wider type is for a 'readonly' |
| 540 | // property in primary class and 'narrowed' type for a 'readwrite' property |
| 541 | // in continuation class. |
Fariborz Jahanian | 576ff12 | 2015-04-08 21:34:04 +0000 | [diff] [blame] | 542 | QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType()); |
| 543 | QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType()); |
| 544 | if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) || |
| 545 | !isa<ObjCObjectPointerType>(ClassExtPropertyT) || |
| 546 | (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT, |
Fariborz Jahanian | 6a73384 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 547 | ConvertedType, IncompatibleObjC)) |
| 548 | || IncompatibleObjC) { |
| 549 | Diag(AtLoc, |
| 550 | diag::err_type_mismatch_continuation_class) << PDecl->getType(); |
| 551 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 552 | return nullptr; |
Fariborz Jahanian | 6a73384 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 553 | } |
Fariborz Jahanian | 11ee283 | 2011-09-24 00:56:59 +0000 | [diff] [blame] | 554 | } |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 555 | |
| 556 | // Check that atomicity of property in class extension matches the previous |
| 557 | // declaration. |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 558 | checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true); |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | e17765e | 2015-11-03 17:02:34 +0000 | [diff] [blame] | 560 | // Make sure getter/setter are appropriately synthesized. |
| 561 | ProcessPropertyDecl(PDecl); |
Fariborz Jahanian | b14a3b2 | 2012-09-17 20:57:19 +0000 | [diff] [blame] | 562 | return PDecl; |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, |
| 566 | ObjCContainerDecl *CDecl, |
| 567 | SourceLocation AtLoc, |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 568 | SourceLocation LParenLoc, |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 569 | FieldDeclarator &FD, |
| 570 | Selector GetterSel, |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 571 | SourceLocation GetterNameLoc, |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 572 | Selector SetterSel, |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 573 | SourceLocation SetterNameLoc, |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 574 | const bool isReadWrite, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 575 | const unsigned Attributes, |
Argyrios Kyrtzidis | b458ffb | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 576 | const unsigned AttributesAsWritten, |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 577 | QualType T, |
John McCall | 339bb66 | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 578 | TypeSourceInfo *TInfo, |
Ted Kremenek | 49be9e0 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 579 | tok::ObjCKeywordKind MethodImplKind, |
| 580 | DeclContext *lexicalDC){ |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 581 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 582 | |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 583 | // Property defaults to 'assign' if it is readwrite, unless this is ARC |
| 584 | // and the type is retainable. |
| 585 | bool isAssign; |
| 586 | if (Attributes & (ObjCDeclSpec::DQ_PR_assign | |
| 587 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) { |
| 588 | isAssign = true; |
| 589 | } else if (getOwnershipRule(Attributes) || !isReadWrite) { |
| 590 | isAssign = false; |
| 591 | } else { |
| 592 | isAssign = (!getLangOpts().ObjCAutoRefCount || |
| 593 | !T->isObjCRetainableType()); |
| 594 | } |
| 595 | |
| 596 | // Issue a warning if property is 'assign' as default and its |
| 597 | // object, which is gc'able conforms to NSCopying protocol |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 598 | if (getLangOpts().getGC() != LangOptions::NonGC && |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 599 | isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 600 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 601 | T->getAs<ObjCObjectPointerType>()) { |
| 602 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 603 | if (IDecl) |
| 604 | if (ObjCProtocolDecl* PNSCopying = |
| 605 | LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) |
| 606 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
| 607 | Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 608 | } |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 609 | } |
Eli Friedman | 999af7b | 2013-07-09 01:38:07 +0000 | [diff] [blame] | 610 | |
| 611 | if (T->isObjCObjectType()) { |
| 612 | SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd(); |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 613 | StarLoc = getLocForEndOfToken(StarLoc); |
Eli Friedman | 999af7b | 2013-07-09 01:38:07 +0000 | [diff] [blame] | 614 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object) |
| 615 | << FixItHint::CreateInsertion(StarLoc, "*"); |
| 616 | T = Context.getObjCObjectPointerType(T); |
| 617 | SourceLocation TLoc = TInfo->getTypeLoc().getLocStart(); |
| 618 | TInfo = Context.getTrivialTypeSourceInfo(T, TLoc); |
| 619 | } |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 620 | |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 621 | DeclContext *DC = cast<DeclContext>(CDecl); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 622 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
| 623 | FD.D.getIdentifierLoc(), |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 624 | PropertyId, AtLoc, |
| 625 | LParenLoc, T, TInfo); |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 626 | |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 627 | bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) || |
| 628 | (Attributes & ObjCDeclSpec::DQ_PR_class); |
| 629 | // Class property and instance property can have the same name. |
| 630 | if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl( |
| 631 | DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) { |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 632 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
Ted Kremenek | 679708e | 2010-03-15 18:47:25 +0000 | [diff] [blame] | 633 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 634 | PDecl->setInvalidDecl(); |
| 635 | } |
Ted Kremenek | 49be9e0 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 636 | else { |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 637 | DC->addDecl(PDecl); |
Ted Kremenek | 49be9e0 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 638 | if (lexicalDC) |
| 639 | PDecl->setLexicalDeclContext(lexicalDC); |
| 640 | } |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 641 | |
| 642 | if (T->isArrayType() || T->isFunctionType()) { |
| 643 | Diag(AtLoc, diag::err_property_type) << T; |
| 644 | PDecl->setInvalidDecl(); |
| 645 | } |
| 646 | |
| 647 | ProcessDeclAttributes(S, PDecl, FD.D); |
| 648 | |
| 649 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 650 | // selector names in anticipation of declaration of setter/getter methods. |
Argyrios Kyrtzidis | 194b28e | 2017-03-16 18:25:40 +0000 | [diff] [blame] | 651 | PDecl->setGetterName(GetterSel, GetterNameLoc); |
| 652 | PDecl->setSetterName(SetterSel, SetterNameLoc); |
Argyrios Kyrtzidis | 52bfc2b | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 653 | PDecl->setPropertyAttributesAsWritten( |
Argyrios Kyrtzidis | b458ffb | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 654 | makePropertyAttributesAsWritten(AttributesAsWritten)); |
Argyrios Kyrtzidis | 52bfc2b | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 655 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 656 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 657 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 658 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 659 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 660 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
| 661 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 662 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 663 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
| 664 | |
| 665 | if (isReadWrite) |
| 666 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
| 667 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 668 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 669 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 670 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 671 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 672 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 673 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 674 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 675 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); |
| 676 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 677 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 678 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 679 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 680 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 681 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 682 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 683 | if (isAssign) |
| 684 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 685 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 686 | // In the semantic attributes, one of nonatomic or atomic is always set. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 687 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 688 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 689 | else |
Fariborz Jahanian | c3bcde0 | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 690 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 691 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 692 | // 'unsafe_unretained' is alias for 'assign'. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 693 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 694 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 695 | if (isAssign) |
| 696 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 697 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 698 | if (MethodImplKind == tok::objc_required) |
| 699 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 700 | else if (MethodImplKind == tok::objc_optional) |
| 701 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 703 | if (Attributes & ObjCDeclSpec::DQ_PR_nullability) |
| 704 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability); |
| 705 | |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 706 | if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable) |
| 707 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable); |
| 708 | |
Manman Ren | 387ff7f | 2016-01-26 18:52:43 +0000 | [diff] [blame] | 709 | if (Attributes & ObjCDeclSpec::DQ_PR_class) |
| 710 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class); |
| 711 | |
Ted Kremenek | 959e830 | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 712 | return PDecl; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 713 | } |
| 714 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 715 | static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, |
| 716 | ObjCPropertyDecl *property, |
| 717 | ObjCIvarDecl *ivar) { |
| 718 | if (property->isInvalidDecl() || ivar->isInvalidDecl()) return; |
| 719 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 720 | QualType ivarType = ivar->getType(); |
| 721 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
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 | // The lifetime implied by the property's attributes. |
| 724 | Qualifiers::ObjCLifetime propertyLifetime = |
| 725 | getImpliedARCOwnership(property->getPropertyAttributes(), |
| 726 | property->getType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 727 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 728 | // We're fine if they match. |
| 729 | if (propertyLifetime == ivarLifetime) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 730 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 731 | // None isn't a valid lifetime for an object ivar in ARC, and |
| 732 | // __autoreleasing is never valid; don't diagnose twice. |
| 733 | if ((ivarLifetime == Qualifiers::OCL_None && |
| 734 | S.getLangOpts().ObjCAutoRefCount) || |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 735 | ivarLifetime == Qualifiers::OCL_Autoreleasing) |
| 736 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 737 | |
John McCall | d8561f0 | 2012-08-20 23:36:59 +0000 | [diff] [blame] | 738 | // If the ivar is private, and it's implicitly __unsafe_unretained |
| 739 | // becaues of its type, then pretend it was actually implicitly |
| 740 | // __strong. This is only sound because we're processing the |
| 741 | // property implementation before parsing any method bodies. |
| 742 | if (ivarLifetime == Qualifiers::OCL_ExplicitNone && |
| 743 | propertyLifetime == Qualifiers::OCL_Strong && |
| 744 | ivar->getAccessControl() == ObjCIvarDecl::Private) { |
| 745 | SplitQualType split = ivarType.split(); |
| 746 | if (split.Quals.hasObjCLifetime()) { |
| 747 | assert(ivarType->isObjCARCImplicitlyUnretainedType()); |
| 748 | split.Quals.setObjCLifetime(Qualifiers::OCL_Strong); |
| 749 | ivarType = S.Context.getQualifiedType(split); |
| 750 | ivar->setType(ivarType); |
| 751 | return; |
| 752 | } |
| 753 | } |
| 754 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 755 | switch (propertyLifetime) { |
| 756 | case Qualifiers::OCL_Strong: |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 757 | S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership) |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 758 | << property->getDeclName() |
| 759 | << ivar->getDeclName() |
| 760 | << ivarLifetime; |
| 761 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 762 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 763 | case Qualifiers::OCL_Weak: |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 764 | S.Diag(ivar->getLocation(), diag::err_weak_property) |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 765 | << property->getDeclName() |
| 766 | << ivar->getDeclName(); |
| 767 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 768 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 769 | case Qualifiers::OCL_ExplicitNone: |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 770 | S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership) |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 771 | << property->getDeclName() |
| 772 | << ivar->getDeclName() |
| 773 | << ((property->getPropertyAttributesAsWritten() |
| 774 | & ObjCPropertyDecl::OBJC_PR_assign) != 0); |
| 775 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 776 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 777 | case Qualifiers::OCL_Autoreleasing: |
| 778 | llvm_unreachable("properties cannot be autoreleasing"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 779 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 780 | case Qualifiers::OCL_None: |
| 781 | // Any other property should be ignored. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 782 | return; |
| 783 | } |
| 784 | |
| 785 | S.Diag(property->getLocation(), diag::note_property_declare); |
Argyrios Kyrtzidis | f5b993f | 2012-12-12 22:48:25 +0000 | [diff] [blame] | 786 | if (propertyImplLoc.isValid()) |
| 787 | S.Diag(propertyImplLoc, diag::note_property_synthesize); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Fariborz Jahanian | 39ba639 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 790 | /// setImpliedPropertyAttributeForReadOnlyProperty - |
| 791 | /// This routine evaludates life-time attributes for a 'readonly' |
| 792 | /// property with no known lifetime of its own, using backing |
| 793 | /// 'ivar's attribute, if any. If no backing 'ivar', property's |
| 794 | /// life-time is assumed 'strong'. |
| 795 | static void setImpliedPropertyAttributeForReadOnlyProperty( |
| 796 | ObjCPropertyDecl *property, ObjCIvarDecl *ivar) { |
| 797 | Qualifiers::ObjCLifetime propertyLifetime = |
| 798 | getImpliedARCOwnership(property->getPropertyAttributes(), |
| 799 | property->getType()); |
| 800 | if (propertyLifetime != Qualifiers::OCL_None) |
| 801 | return; |
| 802 | |
| 803 | if (!ivar) { |
| 804 | // if no backing ivar, make property 'strong'. |
| 805 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 806 | return; |
| 807 | } |
| 808 | // property assumes owenership of backing ivar. |
| 809 | QualType ivarType = ivar->getType(); |
| 810 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
| 811 | if (ivarLifetime == Qualifiers::OCL_Strong) |
| 812 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 813 | else if (ivarLifetime == Qualifiers::OCL_Weak) |
| 814 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); |
Fariborz Jahanian | 39ba639 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 815 | } |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 816 | |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 817 | static bool |
| 818 | isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2, |
| 819 | ObjCPropertyDecl::PropertyAttributeKind Kind) { |
| 820 | return (Attr1 & Kind) != (Attr2 & Kind); |
| 821 | } |
| 822 | |
| 823 | static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2, |
| 824 | unsigned Kinds) { |
| 825 | return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0); |
| 826 | } |
| 827 | |
| 828 | /// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate |
| 829 | /// property declaration that should be synthesised in all of the inherited |
| 830 | /// protocols. It also diagnoses properties declared in inherited protocols with |
| 831 | /// mismatched types or attributes, since any of them can be candidate for |
| 832 | /// synthesis. |
| 833 | static ObjCPropertyDecl * |
| 834 | SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc, |
Benjamin Kramer | bf8d254 | 2013-05-23 15:53:44 +0000 | [diff] [blame] | 835 | ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 836 | ObjCPropertyDecl *Property) { |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 837 | assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) && |
| 838 | "Expected a property from a protocol"); |
| 839 | ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet; |
| 840 | ObjCInterfaceDecl::PropertyDeclOrder Properties; |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 841 | for (const auto *PI : ClassDecl->all_referenced_protocols()) { |
| 842 | if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 843 | PDecl->collectInheritedProtocolProperties(Property, ProtocolSet, |
| 844 | Properties); |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 845 | } |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 846 | if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) { |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 847 | while (SDecl) { |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 848 | for (const auto *PI : SDecl->all_referenced_protocols()) { |
| 849 | if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 850 | PDecl->collectInheritedProtocolProperties(Property, ProtocolSet, |
| 851 | Properties); |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 852 | } |
| 853 | SDecl = SDecl->getSuperClass(); |
| 854 | } |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | if (Properties.empty()) |
| 858 | return Property; |
| 859 | |
| 860 | ObjCPropertyDecl *OriginalProperty = Property; |
| 861 | size_t SelectedIndex = 0; |
| 862 | for (const auto &Prop : llvm::enumerate(Properties)) { |
| 863 | // Select the 'readwrite' property if such property exists. |
| 864 | if (Property->isReadOnly() && !Prop.value()->isReadOnly()) { |
| 865 | Property = Prop.value(); |
| 866 | SelectedIndex = Prop.index(); |
| 867 | } |
| 868 | } |
| 869 | if (Property != OriginalProperty) { |
| 870 | // Check that the old property is compatible with the new one. |
| 871 | Properties[SelectedIndex] = OriginalProperty; |
| 872 | } |
| 873 | |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 874 | QualType RHSType = S.Context.getCanonicalType(Property->getType()); |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 875 | unsigned OriginalAttributes = Property->getPropertyAttributes(); |
| 876 | enum MismatchKind { |
| 877 | IncompatibleType = 0, |
| 878 | HasNoExpectedAttribute, |
| 879 | HasUnexpectedAttribute, |
| 880 | DifferentGetter, |
| 881 | DifferentSetter |
| 882 | }; |
| 883 | // Represents a property from another protocol that conflicts with the |
| 884 | // selected declaration. |
| 885 | struct MismatchingProperty { |
| 886 | const ObjCPropertyDecl *Prop; |
| 887 | MismatchKind Kind; |
| 888 | StringRef AttributeName; |
| 889 | }; |
| 890 | SmallVector<MismatchingProperty, 4> Mismatches; |
| 891 | for (ObjCPropertyDecl *Prop : Properties) { |
| 892 | // Verify the property attributes. |
| 893 | unsigned Attr = Prop->getPropertyAttributes(); |
| 894 | if (Attr != OriginalAttributes) { |
| 895 | auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) { |
| 896 | MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute |
| 897 | : HasUnexpectedAttribute; |
| 898 | Mismatches.push_back({Prop, Kind, AttributeName}); |
| 899 | }; |
| 900 | if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr, |
| 901 | ObjCPropertyDecl::OBJC_PR_copy)) { |
| 902 | Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy"); |
| 903 | continue; |
| 904 | } |
| 905 | if (areIncompatiblePropertyAttributes( |
| 906 | OriginalAttributes, Attr, ObjCPropertyDecl::OBJC_PR_retain | |
| 907 | ObjCPropertyDecl::OBJC_PR_strong)) { |
| 908 | Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain | |
| 909 | ObjCPropertyDecl::OBJC_PR_strong), |
| 910 | "retain (or strong)"); |
| 911 | continue; |
| 912 | } |
| 913 | if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr, |
| 914 | ObjCPropertyDecl::OBJC_PR_atomic)) { |
| 915 | Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic"); |
| 916 | continue; |
| 917 | } |
| 918 | } |
| 919 | if (Property->getGetterName() != Prop->getGetterName()) { |
| 920 | Mismatches.push_back({Prop, DifferentGetter, ""}); |
| 921 | continue; |
| 922 | } |
| 923 | if (!Property->isReadOnly() && !Prop->isReadOnly() && |
| 924 | Property->getSetterName() != Prop->getSetterName()) { |
| 925 | Mismatches.push_back({Prop, DifferentSetter, ""}); |
| 926 | continue; |
| 927 | } |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 928 | QualType LHSType = S.Context.getCanonicalType(Prop->getType()); |
| 929 | if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
| 930 | bool IncompatibleObjC = false; |
| 931 | QualType ConvertedType; |
| 932 | if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC) |
| 933 | || IncompatibleObjC) { |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 934 | Mismatches.push_back({Prop, IncompatibleType, ""}); |
| 935 | continue; |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | } |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 939 | |
| 940 | if (Mismatches.empty()) |
| 941 | return Property; |
| 942 | |
| 943 | // Diagnose incompability. |
| 944 | { |
| 945 | bool HasIncompatibleAttributes = false; |
| 946 | for (const auto &Note : Mismatches) |
| 947 | HasIncompatibleAttributes = |
| 948 | Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes; |
| 949 | // Promote the warning to an error if there are incompatible attributes or |
| 950 | // incompatible types together with readwrite/readonly incompatibility. |
| 951 | auto Diag = S.Diag(Property->getLocation(), |
| 952 | Property != OriginalProperty || HasIncompatibleAttributes |
| 953 | ? diag::err_protocol_property_mismatch |
| 954 | : diag::warn_protocol_property_mismatch); |
| 955 | Diag << Mismatches[0].Kind; |
| 956 | switch (Mismatches[0].Kind) { |
| 957 | case IncompatibleType: |
| 958 | Diag << Property->getType(); |
| 959 | break; |
| 960 | case HasNoExpectedAttribute: |
| 961 | case HasUnexpectedAttribute: |
| 962 | Diag << Mismatches[0].AttributeName; |
| 963 | break; |
| 964 | case DifferentGetter: |
| 965 | Diag << Property->getGetterName(); |
| 966 | break; |
| 967 | case DifferentSetter: |
| 968 | Diag << Property->getSetterName(); |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | for (const auto &Note : Mismatches) { |
| 973 | auto Diag = |
| 974 | S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare) |
| 975 | << Note.Kind; |
| 976 | switch (Note.Kind) { |
| 977 | case IncompatibleType: |
| 978 | Diag << Note.Prop->getType(); |
| 979 | break; |
| 980 | case HasNoExpectedAttribute: |
| 981 | case HasUnexpectedAttribute: |
| 982 | Diag << Note.AttributeName; |
| 983 | break; |
| 984 | case DifferentGetter: |
| 985 | Diag << Note.Prop->getGetterName(); |
| 986 | break; |
| 987 | case DifferentSetter: |
| 988 | Diag << Note.Prop->getSetterName(); |
| 989 | break; |
| 990 | } |
| 991 | } |
| 992 | if (AtLoc.isValid()) |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 993 | S.Diag(AtLoc, diag::note_property_synthesize); |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 994 | |
| 995 | return Property; |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 998 | /// Determine whether any storage attributes were written on the property. |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 999 | static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop, |
| 1000 | ObjCPropertyQueryKind QueryKind) { |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 1001 | if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true; |
| 1002 | |
| 1003 | // If this is a readwrite property in a class extension that refines |
| 1004 | // a readonly property in the original class definition, check it as |
| 1005 | // well. |
| 1006 | |
| 1007 | // If it's a readonly property, we're not interested. |
| 1008 | if (Prop->isReadOnly()) return false; |
| 1009 | |
| 1010 | // Is it declared in an extension? |
| 1011 | auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext()); |
| 1012 | if (!Category || !Category->IsClassExtension()) return false; |
| 1013 | |
| 1014 | // Find the corresponding property in the primary class definition. |
| 1015 | auto OrigClass = Category->getClassInterface(); |
| 1016 | for (auto Found : OrigClass->lookup(Prop->getDeclName())) { |
| 1017 | if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found)) |
| 1018 | return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; |
| 1019 | } |
| 1020 | |
Douglas Gregor | 0253543 | 2015-12-18 00:52:31 +0000 | [diff] [blame] | 1021 | // Look through all of the protocols. |
| 1022 | for (const auto *Proto : OrigClass->all_referenced_protocols()) { |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1023 | if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration( |
| 1024 | Prop->getIdentifier(), QueryKind)) |
Douglas Gregor | 0253543 | 2015-12-18 00:52:31 +0000 | [diff] [blame] | 1025 | return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; |
| 1026 | } |
| 1027 | |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 1028 | return false; |
| 1029 | } |
| 1030 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1031 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 1032 | /// builds the AST node for a property implementation declaration; declared |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1033 | /// as \@synthesize or \@dynamic. |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1034 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1035 | Decl *Sema::ActOnPropertyImplDecl(Scope *S, |
| 1036 | SourceLocation AtLoc, |
| 1037 | SourceLocation PropertyLoc, |
| 1038 | bool Synthesize, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1039 | IdentifierInfo *PropertyId, |
Douglas Gregor | b1b71e5 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1040 | IdentifierInfo *PropertyIvar, |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1041 | SourceLocation PropertyIvarLoc, |
| 1042 | ObjCPropertyQueryKind QueryKind) { |
Ted Kremenek | 273c4f5 | 2010-04-05 23:45:09 +0000 | [diff] [blame] | 1043 | ObjCContainerDecl *ClassImpDecl = |
Fariborz Jahanian | a195a51 | 2011-09-19 16:32:32 +0000 | [diff] [blame] | 1044 | dyn_cast<ObjCContainerDecl>(CurContext); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1045 | // Make sure we have a context for the property implementation declaration. |
| 1046 | if (!ClassImpDecl) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1047 | Diag(AtLoc, diag::err_missing_property_context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1048 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1049 | } |
Argyrios Kyrtzidis | 3460880 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 1050 | if (PropertyIvarLoc.isInvalid()) |
| 1051 | PropertyIvarLoc = PropertyLoc; |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1052 | SourceLocation PropertyDiagLoc = PropertyLoc; |
| 1053 | if (PropertyDiagLoc.isInvalid()) |
| 1054 | PropertyDiagLoc = ClassImpDecl->getLocStart(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1055 | ObjCPropertyDecl *property = nullptr; |
| 1056 | ObjCInterfaceDecl *IDecl = nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1057 | // Find the class or category class where this property must have |
| 1058 | // a declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1059 | ObjCImplementationDecl *IC = nullptr; |
| 1060 | ObjCCategoryImplDecl *CatImplClass = nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1061 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
| 1062 | IDecl = IC->getClassInterface(); |
| 1063 | // We always synthesize an interface for an implementation |
| 1064 | // without an interface decl. So, IDecl is always non-zero. |
| 1065 | assert(IDecl && |
| 1066 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 1067 | |
| 1068 | // Look for this property declaration in the @implementation's @interface |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1069 | property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1070 | if (!property) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1071 | Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1072 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1073 | } |
Manman Ren | dfef406 | 2016-01-29 19:16:39 +0000 | [diff] [blame] | 1074 | if (property->isClassProperty() && Synthesize) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1075 | Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId; |
Manman Ren | dfef406 | 2016-01-29 19:16:39 +0000 | [diff] [blame] | 1076 | return nullptr; |
| 1077 | } |
Fariborz Jahanian | 382c040 | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 1078 | unsigned PIkind = property->getPropertyAttributesAsWritten(); |
Fariborz Jahanian | c3bcde0 | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 1079 | if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | |
| 1080 | ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { |
Fariborz Jahanian | 382c040 | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 1081 | if (AtLoc.isValid()) |
| 1082 | Diag(AtLoc, diag::warn_implicit_atomic_property); |
| 1083 | else |
| 1084 | Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); |
| 1085 | Diag(property->getLocation(), diag::note_property_declare); |
| 1086 | } |
| 1087 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1088 | if (const ObjCCategoryDecl *CD = |
| 1089 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
| 1090 | if (!CD->IsClassExtension()) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1091 | Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName(); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1092 | Diag(property->getLocation(), diag::note_property_declare); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1093 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
Fariborz Jahanian | 199a9b5 | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 1096 | if (Synthesize&& |
| 1097 | (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) && |
| 1098 | property->hasAttr<IBOutletAttr>() && |
| 1099 | !AtLoc.isValid()) { |
Fariborz Jahanian | f3c171e | 2013-02-08 23:32:30 +0000 | [diff] [blame] | 1100 | bool ReadWriteProperty = false; |
| 1101 | // Search into the class extensions and see if 'readonly property is |
| 1102 | // redeclared 'readwrite', then no warning is to be issued. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 1103 | for (auto *Ext : IDecl->known_extensions()) { |
Fariborz Jahanian | f3c171e | 2013-02-08 23:32:30 +0000 | [diff] [blame] | 1104 | DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); |
| 1105 | if (!R.empty()) |
| 1106 | if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) { |
| 1107 | PIkind = ExtProp->getPropertyAttributesAsWritten(); |
| 1108 | if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 1109 | ReadWriteProperty = true; |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (!ReadWriteProperty) { |
Ted Kremenek | 7ee2567 | 2013-02-09 07:13:16 +0000 | [diff] [blame] | 1116 | Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property) |
Aaron Ballman | 1fb3955 | 2014-01-03 14:23:03 +0000 | [diff] [blame] | 1117 | << property; |
Fariborz Jahanian | f3c171e | 2013-02-08 23:32:30 +0000 | [diff] [blame] | 1118 | SourceLocation readonlyLoc; |
| 1119 | if (LocPropertyAttribute(Context, "readonly", |
| 1120 | property->getLParenLoc(), readonlyLoc)) { |
| 1121 | SourceLocation endLoc = |
| 1122 | readonlyLoc.getLocWithOffset(strlen("readonly")-1); |
| 1123 | SourceRange ReadonlySourceRange(readonlyLoc, endLoc); |
| 1124 | Diag(property->getLocation(), |
| 1125 | diag::note_auto_readonly_iboutlet_fixup_suggest) << |
| 1126 | FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite"); |
| 1127 | } |
Fariborz Jahanian | b52d8d2 | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 1128 | } |
Fariborz Jahanian | 199a9b5 | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 1129 | } |
Fariborz Jahanian | 0ebf879 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 1130 | if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext())) |
Alex Lorenz | 50b2dd3 | 2017-07-13 11:06:22 +0000 | [diff] [blame] | 1131 | property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl, |
| 1132 | property); |
| 1133 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1134 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
| 1135 | if (Synthesize) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1136 | Diag(AtLoc, diag::err_synthesize_category_decl); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1137 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1138 | } |
| 1139 | IDecl = CatImplClass->getClassInterface(); |
| 1140 | if (!IDecl) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1141 | Diag(AtLoc, diag::err_missing_property_interface); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1142 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1143 | } |
| 1144 | ObjCCategoryDecl *Category = |
| 1145 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 1146 | |
| 1147 | // If category for this implementation not found, it is an error which |
| 1148 | // has already been reported eralier. |
| 1149 | if (!Category) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1150 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1151 | // Look for this property declaration in @implementation's category |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1152 | property = Category->FindPropertyDeclaration(PropertyId, QueryKind); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1153 | if (!property) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1154 | Diag(PropertyLoc, diag::err_bad_category_property_decl) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1155 | << Category->getDeclName(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1156 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } else { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1159 | Diag(AtLoc, diag::err_bad_property_context); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1160 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1161 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1162 | ObjCIvarDecl *Ivar = nullptr; |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1163 | bool CompleteTypeErr = false; |
Fariborz Jahanian | 3da7775 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1164 | bool compat = true; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1165 | // Check that we have a valid, previously declared ivar for @synthesize |
| 1166 | if (Synthesize) { |
| 1167 | // @synthesize |
| 1168 | if (!PropertyIvar) |
| 1169 | PropertyIvar = PropertyId; |
Fariborz Jahanian | 39ba639 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 1170 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
| 1171 | ObjCInterfaceDecl *ClassDeclared; |
| 1172 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
| 1173 | QualType PropType = property->getType(); |
| 1174 | QualType PropertyIvarType = PropType.getNonReferenceType(); |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1175 | |
| 1176 | if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1177 | diag::err_incomplete_synthesized_property, |
| 1178 | property->getDeclName())) { |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1179 | Diag(property->getLocation(), diag::note_property_declare); |
| 1180 | CompleteTypeErr = true; |
| 1181 | } |
| 1182 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1183 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 39ba639 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 1184 | (property->getPropertyAttributesAsWritten() & |
Fariborz Jahanian | a230dea | 2012-01-11 19:48:08 +0000 | [diff] [blame] | 1185 | ObjCPropertyDecl::OBJC_PR_readonly) && |
| 1186 | PropertyIvarType->isObjCRetainableType()) { |
Fariborz Jahanian | 39ba639 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 1187 | setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar); |
| 1188 | } |
| 1189 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1190 | ObjCPropertyDecl::PropertyAttributeKind kind |
| 1191 | = property->getPropertyAttributes(); |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1192 | |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1193 | bool isARCWeak = false; |
| 1194 | if (kind & ObjCPropertyDecl::OBJC_PR_weak) { |
| 1195 | // Add GC __weak to the ivar type if the property is weak. |
| 1196 | if (getLangOpts().getGC() != LangOptions::NonGC) { |
| 1197 | assert(!getLangOpts().ObjCAutoRefCount); |
| 1198 | if (PropertyIvarType.isObjCGCStrong()) { |
| 1199 | Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type); |
| 1200 | Diag(property->getLocation(), diag::note_property_declare); |
| 1201 | } else { |
| 1202 | PropertyIvarType = |
| 1203 | Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); |
| 1204 | } |
| 1205 | |
| 1206 | // Otherwise, check whether ARC __weak is enabled and works with |
| 1207 | // the property type. |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1208 | } else { |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1209 | if (!getLangOpts().ObjCWeak) { |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 1210 | // Only complain here when synthesizing an ivar. |
| 1211 | if (!Ivar) { |
| 1212 | Diag(PropertyDiagLoc, |
| 1213 | getLangOpts().ObjCWeakRuntime |
| 1214 | ? diag::err_synthesizing_arc_weak_property_disabled |
| 1215 | : diag::err_synthesizing_arc_weak_property_no_runtime); |
| 1216 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1217 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 1218 | CompleteTypeErr = true; // suppress later diagnostics about the ivar |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1219 | } else { |
| 1220 | isARCWeak = true; |
| 1221 | if (const ObjCObjectPointerType *ObjT = |
| 1222 | PropertyIvarType->getAs<ObjCObjectPointerType>()) { |
| 1223 | const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl(); |
| 1224 | if (ObjI && ObjI->isArcWeakrefUnavailable()) { |
| 1225 | Diag(property->getLocation(), |
| 1226 | diag::err_arc_weak_unavailable_property) |
| 1227 | << PropertyIvarType; |
| 1228 | Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class) |
| 1229 | << ClassImpDecl->getName(); |
| 1230 | } |
| 1231 | } |
| 1232 | } |
Fariborz Jahanian | eebdb67 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 1233 | } |
Fariborz Jahanian | eebdb67 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 1234 | } |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1235 | |
Fariborz Jahanian | edc2971 | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 1236 | if (AtLoc.isInvalid()) { |
| 1237 | // Check when default synthesizing a property that there is |
| 1238 | // an ivar matching property name and issue warning; since this |
| 1239 | // is the most common case of not using an ivar used for backing |
| 1240 | // property in non-default synthesis case. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1241 | ObjCInterfaceDecl *ClassDeclared=nullptr; |
Fariborz Jahanian | edc2971 | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 1242 | ObjCIvarDecl *originalIvar = |
| 1243 | IDecl->lookupInstanceVariable(property->getIdentifier(), |
| 1244 | ClassDeclared); |
| 1245 | if (originalIvar) { |
| 1246 | Diag(PropertyDiagLoc, |
| 1247 | diag::warn_autosynthesis_property_ivar_match) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1248 | << PropertyId << (Ivar == nullptr) << PropertyIvar |
Fariborz Jahanian | 1db30fc | 2012-06-29 18:43:30 +0000 | [diff] [blame] | 1249 | << originalIvar->getIdentifier(); |
Fariborz Jahanian | edc2971 | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 1250 | Diag(property->getLocation(), diag::note_property_declare); |
| 1251 | Diag(originalIvar->getLocation(), diag::note_ivar_decl); |
Fariborz Jahanian | 63d4020 | 2012-06-19 22:51:22 +0000 | [diff] [blame] | 1252 | } |
Fariborz Jahanian | edc2971 | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | if (!Ivar) { |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1256 | // In ARC, give the ivar a lifetime qualifier based on the |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1257 | // property attributes. |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1258 | if ((getLangOpts().ObjCAutoRefCount || isARCWeak) && |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1259 | !PropertyIvarType.getObjCLifetime() && |
| 1260 | PropertyIvarType->isObjCRetainableType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1261 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1262 | // It's an error if we have to do this and the user didn't |
| 1263 | // explicitly write an ownership attribute on the property. |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1264 | if (!hasWrittenStorageAttribute(property, QueryKind) && |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1265 | !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1266 | Diag(PropertyDiagLoc, |
Argyrios Kyrtzidis | e3be979 | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 1267 | diag::err_arc_objc_property_default_assign_on_object); |
| 1268 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1269 | } else { |
| 1270 | Qualifiers::ObjCLifetime lifetime = |
| 1271 | getImpliedARCOwnership(kind, PropertyIvarType); |
| 1272 | assert(lifetime && "no lifetime for property?"); |
Fariborz Jahanian | e283346 | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 1273 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1274 | Qualifiers qs; |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1275 | qs.addObjCLifetime(lifetime); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1276 | PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); |
| 1277 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1280 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Argyrios Kyrtzidis | 3460880 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 1281 | PropertyIvarLoc,PropertyIvarLoc, PropertyIvar, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1282 | PropertyIvarType, /*Dinfo=*/nullptr, |
Fariborz Jahanian | 522eb7b | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1283 | ObjCIvarDecl::Private, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1284 | (Expr *)nullptr, true); |
Fariborz Jahanian | 873bae7 | 2013-07-05 17:18:11 +0000 | [diff] [blame] | 1285 | if (RequireNonAbstractType(PropertyIvarLoc, |
| 1286 | PropertyIvarType, |
| 1287 | diag::err_abstract_type_in_decl, |
| 1288 | AbstractSynthesizedIvarType)) { |
| 1289 | Diag(property->getLocation(), diag::note_property_declare); |
Richard Smith | 81f5ade | 2016-12-15 02:28:18 +0000 | [diff] [blame] | 1290 | // An abstract type is as bad as an incomplete type. |
| 1291 | CompleteTypeErr = true; |
| 1292 | } |
| 1293 | if (CompleteTypeErr) |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1294 | Ivar->setInvalidDecl(); |
Daniel Dunbar | ab5d7ae | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 1295 | ClassImpDecl->addDecl(Ivar); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1296 | IDecl->makeDeclVisibleInContext(Ivar); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1297 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1298 | if (getLangOpts().ObjCRuntime.isFragile()) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1299 | Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl) |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1300 | << PropertyId; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1301 | // Note! I deliberately want it to fall thru so, we have a |
| 1302 | // a property implementation and to avoid future warnings. |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1303 | } else if (getLangOpts().ObjCRuntime.isNonFragile() && |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1304 | !declaresSameEntity(ClassDeclared, IDecl)) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1305 | Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1306 | << property->getDeclName() << Ivar->getDeclName() |
| 1307 | << ClassDeclared->getDeclName(); |
| 1308 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1309 | << Ivar << Ivar->getName(); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1310 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 1311 | } |
Anna Zaks | 9802f9f | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 1312 | property->setPropertyIvarDecl(Ivar); |
| 1313 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1314 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 1315 | |
| 1316 | // Check that type of property and its ivar are type compatible. |
Fariborz Jahanian | 3da7775 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1317 | if (!Context.hasSameType(PropertyIvarType, IvarType)) { |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 1318 | if (isa<ObjCObjectPointerType>(PropertyIvarType) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1319 | && isa<ObjCObjectPointerType>(IvarType)) |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 1320 | compat = |
Fariborz Jahanian | 9f963c2 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1321 | Context.canAssignObjCInterfaces( |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 1322 | PropertyIvarType->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 9f963c2 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1323 | IvarType->getAs<ObjCObjectPointerType>()); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1324 | else { |
Argyrios Kyrtzidis | 3460880 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 1325 | compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType, |
| 1326 | IvarType) |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1327 | == Compatible); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1328 | } |
Fariborz Jahanian | 9f963c2 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1329 | if (!compat) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1330 | Diag(PropertyDiagLoc, diag::err_property_ivar_type) |
Ted Kremenek | 5921b83 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 1331 | << property->getDeclName() << PropType |
| 1332 | << Ivar->getDeclName() << IvarType; |
| 1333 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1334 | // Note! I deliberately want it to fall thru so, we have a |
| 1335 | // a property implementation and to avoid future warnings. |
| 1336 | } |
Fariborz Jahanian | 3da7775 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1337 | else { |
| 1338 | // FIXME! Rules for properties are somewhat different that those |
| 1339 | // for assignments. Use a new routine to consolidate all cases; |
| 1340 | // specifically for property redeclarations as well as for ivars. |
| 1341 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
| 1342 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 1343 | if (lhsType != rhsType && |
| 1344 | lhsType->isArithmeticType()) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1345 | Diag(PropertyDiagLoc, diag::err_property_ivar_type) |
Fariborz Jahanian | 3da7775 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1346 | << property->getDeclName() << PropType |
| 1347 | << Ivar->getDeclName() << IvarType; |
| 1348 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
| 1349 | // Fall thru - see previous comment |
| 1350 | } |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1351 | } |
| 1352 | // __weak is explicit. So it works on Canonical type. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1353 | if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1354 | getLangOpts().getGC() != LangOptions::NonGC)) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1355 | Diag(PropertyDiagLoc, diag::err_weak_property) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1356 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | eebdb67 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 1357 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1358 | // Fall thru - see previous comment |
| 1359 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1360 | // Fall thru - see previous comment |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1361 | if ((property->getType()->isObjCObjectPointerType() || |
| 1362 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1363 | getLangOpts().getGC() != LangOptions::NonGC) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1364 | Diag(PropertyDiagLoc, diag::err_strong_property) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1365 | << property->getDeclName() << Ivar->getDeclName(); |
| 1366 | // Fall thru - see previous comment |
| 1367 | } |
| 1368 | } |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1369 | if (getLangOpts().ObjCAutoRefCount || isARCWeak || |
| 1370 | Ivar->getType().getObjCLifetime()) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1371 | checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1372 | } else if (PropertyIvar) |
| 1373 | // @dynamic |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1374 | Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1375 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1376 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
| 1377 | ObjCPropertyImplDecl *PIDecl = |
| 1378 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 1379 | property, |
| 1380 | (Synthesize ? |
| 1381 | ObjCPropertyImplDecl::Synthesize |
| 1382 | : ObjCPropertyImplDecl::Dynamic), |
Douglas Gregor | b1b71e5 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1383 | Ivar, PropertyIvarLoc); |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1384 | |
Fariborz Jahanian | 3da7775 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1385 | if (CompleteTypeErr || !compat) |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1386 | PIDecl->setInvalidDecl(); |
| 1387 | |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1388 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
| 1389 | getterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1390 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
Fariborz Jahanian | dddf158 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 1391 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1392 | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
| 1393 | // returned by the getter as it must conform to C++'s copy-return rules. |
| 1394 | // FIXME. Eventually we want to do this for Objective-C as well. |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1395 | SynthesizedFunctionScope Scope(*this, getterMethod); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1396 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
| 1397 | DeclRefExpr *SelfExpr = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1398 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
Jordan Rose | 31c05a1 | 2014-01-14 17:29:00 +0000 | [diff] [blame] | 1399 | VK_LValue, PropertyDiagLoc); |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1400 | MarkDeclRefReferenced(SelfExpr); |
Jordan Rose | 31c05a1 | 2014-01-14 17:29:00 +0000 | [diff] [blame] | 1401 | Expr *LoadSelfExpr = |
| 1402 | ImplicitCastExpr::Create(Context, SelfDecl->getType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1403 | CK_LValueToRValue, SelfExpr, nullptr, |
| 1404 | VK_RValue); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1405 | Expr *IvarRefExpr = |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1406 | new (Context) ObjCIvarRefExpr(Ivar, |
| 1407 | Ivar->getUsageType(SelfDecl->getType()), |
| 1408 | PropertyDiagLoc, |
Fariborz Jahanian | f12ff4df | 2013-04-02 18:57:54 +0000 | [diff] [blame] | 1409 | Ivar->getLocation(), |
Jordan Rose | 31c05a1 | 2014-01-14 17:29:00 +0000 | [diff] [blame] | 1410 | LoadSelfExpr, true, true); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1411 | ExprResult Res = PerformCopyInitialization( |
| 1412 | InitializedEntity::InitializeResult(PropertyDiagLoc, |
| 1413 | getterMethod->getReturnType(), |
| 1414 | /*NRVO=*/false), |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1415 | PropertyDiagLoc, IvarRefExpr); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1416 | if (!Res.isInvalid()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1417 | Expr *ResExpr = Res.getAs<Expr>(); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1418 | if (ResExpr) |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1419 | ResExpr = MaybeCreateExprWithCleanups(ResExpr); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1420 | PIDecl->setGetterCXXConstructor(ResExpr); |
| 1421 | } |
| 1422 | } |
Fariborz Jahanian | f4105f5 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1423 | if (property->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1424 | !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 1425 | Diag(getterMethod->getLocation(), |
| 1426 | diag::warn_property_getter_owning_mismatch); |
| 1427 | Diag(property->getLocation(), diag::note_property_declare); |
| 1428 | } |
Fariborz Jahanian | 39d1c42 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 1429 | if (getLangOpts().ObjCAutoRefCount && Synthesize) |
| 1430 | switch (getterMethod->getMethodFamily()) { |
| 1431 | case OMF_retain: |
| 1432 | case OMF_retainCount: |
| 1433 | case OMF_release: |
| 1434 | case OMF_autorelease: |
| 1435 | Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def) |
| 1436 | << 1 << getterMethod->getSelector(); |
| 1437 | break; |
| 1438 | default: |
| 1439 | break; |
| 1440 | } |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1441 | } |
| 1442 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
| 1443 | setterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | 169ec35 | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1444 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
| 1445 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1446 | // FIXME. Eventually we want to do this for Objective-C as well. |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1447 | SynthesizedFunctionScope Scope(*this, setterMethod); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1448 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
| 1449 | DeclRefExpr *SelfExpr = |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1450 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
Jordan Rose | 31c05a1 | 2014-01-14 17:29:00 +0000 | [diff] [blame] | 1451 | VK_LValue, PropertyDiagLoc); |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1452 | MarkDeclRefReferenced(SelfExpr); |
Jordan Rose | 31c05a1 | 2014-01-14 17:29:00 +0000 | [diff] [blame] | 1453 | Expr *LoadSelfExpr = |
| 1454 | ImplicitCastExpr::Create(Context, SelfDecl->getType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1455 | CK_LValueToRValue, SelfExpr, nullptr, |
| 1456 | VK_RValue); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1457 | Expr *lhs = |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1458 | new (Context) ObjCIvarRefExpr(Ivar, |
| 1459 | Ivar->getUsageType(SelfDecl->getType()), |
| 1460 | PropertyDiagLoc, |
Fariborz Jahanian | f12ff4df | 2013-04-02 18:57:54 +0000 | [diff] [blame] | 1461 | Ivar->getLocation(), |
Jordan Rose | 31c05a1 | 2014-01-14 17:29:00 +0000 | [diff] [blame] | 1462 | LoadSelfExpr, true, true); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1463 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
| 1464 | ParmVarDecl *Param = (*P); |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1465 | QualType T = Param->getType().getNonReferenceType(); |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1466 | DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T, |
| 1467 | VK_LValue, PropertyDiagLoc); |
| 1468 | MarkDeclRefReferenced(rhs); |
| 1469 | ExprResult Res = BuildBinOp(S, PropertyDiagLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1470 | BO_Assign, lhs, rhs); |
Fariborz Jahanian | 565ed7a | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1471 | if (property->getPropertyAttributes() & |
| 1472 | ObjCPropertyDecl::OBJC_PR_atomic) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1473 | Expr *callExpr = Res.getAs<Expr>(); |
Fariborz Jahanian | 565ed7a | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1474 | if (const CXXOperatorCallExpr *CXXCE = |
Fariborz Jahanian | 13d3f86 | 2011-10-07 21:08:14 +0000 | [diff] [blame] | 1475 | dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) |
| 1476 | if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) |
Fariborz Jahanian | 565ed7a | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1477 | if (!FuncDecl->isTrivial()) |
Fariborz Jahanian | a08a747 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1478 | if (property->getType()->isReferenceType()) { |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1479 | Diag(PropertyDiagLoc, |
Fariborz Jahanian | a08a747 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1480 | diag::err_atomic_property_nontrivial_assign_op) |
Fariborz Jahanian | 565ed7a | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1481 | << property->getType(); |
Fariborz Jahanian | a08a747 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1482 | Diag(FuncDecl->getLocStart(), |
| 1483 | diag::note_callee_decl) << FuncDecl; |
| 1484 | } |
Fariborz Jahanian | 565ed7a | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1485 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1486 | PIDecl->setSetterCXXAssignment(Res.getAs<Expr>()); |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1490 | if (IC) { |
| 1491 | if (Synthesize) |
| 1492 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1493 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1494 | Diag(PropertyLoc, diag::err_duplicate_ivar_use) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1495 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1496 | << PropertyIvar; |
| 1497 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1498 | } |
| 1499 | |
| 1500 | if (ObjCPropertyImplDecl *PPIDecl |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1501 | = IC->FindPropertyImplDecl(PropertyId, QueryKind)) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1502 | Diag(PropertyLoc, diag::err_property_implemented) << PropertyId; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1503 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1504 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1505 | } |
| 1506 | IC->addPropertyImplementation(PIDecl); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1507 | if (getLangOpts().ObjCDefaultSynthProperties && |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1508 | getLangOpts().ObjCRuntime.isNonFragile() && |
Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1509 | !IDecl->isObjCRequiresPropertyDefs()) { |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1510 | // Diagnose if an ivar was lazily synthesdized due to a previous |
| 1511 | // use and if 1) property is @dynamic or 2) property is synthesized |
Fariborz Jahanian | 76b3537 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1512 | // but it requires an ivar of different name. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1513 | ObjCInterfaceDecl *ClassDeclared=nullptr; |
| 1514 | ObjCIvarDecl *Ivar = nullptr; |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1515 | if (!Synthesize) |
| 1516 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1517 | else { |
| 1518 | if (PropertyIvar && PropertyIvar != PropertyId) |
| 1519 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1520 | } |
Fariborz Jahanian | 76b3537 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1521 | // Issue diagnostics only if Ivar belongs to current class. |
| 1522 | if (Ivar && Ivar->getSynthesize() && |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1523 | declaresSameEntity(IC->getClassInterface(), ClassDeclared)) { |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1524 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) |
| 1525 | << PropertyId; |
| 1526 | Ivar->setInvalidDecl(); |
| 1527 | } |
| 1528 | } |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1529 | } else { |
| 1530 | if (Synthesize) |
| 1531 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1532 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1533 | Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use) |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1534 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1535 | << PropertyIvar; |
| 1536 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1537 | } |
| 1538 | |
| 1539 | if (ObjCPropertyImplDecl *PPIDecl = |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1540 | CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1541 | Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1542 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1543 | return nullptr; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1544 | } |
| 1545 | CatImplClass->addPropertyImplementation(PIDecl); |
| 1546 | } |
| 1547 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1548 | return PIDecl; |
Ted Kremenek | ac597f3 | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | //===----------------------------------------------------------------------===// |
| 1552 | // Helper methods. |
| 1553 | //===----------------------------------------------------------------------===// |
| 1554 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1555 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 1556 | /// attributes and types and warns on a variety of inconsistencies. |
| 1557 | /// |
| 1558 | void |
| 1559 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 1560 | ObjCPropertyDecl *SuperProperty, |
Fariborz Jahanian | b809a0e | 2013-10-04 18:06:08 +0000 | [diff] [blame] | 1561 | const IdentifierInfo *inheritedName, |
| 1562 | bool OverridingProtocolProperty) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1563 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
Fariborz Jahanian | b809a0e | 2013-10-04 18:06:08 +0000 | [diff] [blame] | 1564 | Property->getPropertyAttributes(); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1565 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
Fariborz Jahanian | b809a0e | 2013-10-04 18:06:08 +0000 | [diff] [blame] | 1566 | SuperProperty->getPropertyAttributes(); |
| 1567 | |
| 1568 | // We allow readonly properties without an explicit ownership |
| 1569 | // (assign/unsafe_unretained/weak/retain/strong/copy) in super class |
| 1570 | // to be overridden by a property with any explicit ownership in the subclass. |
| 1571 | if (!OverridingProtocolProperty && |
| 1572 | !getOwnershipRule(SAttr) && getOwnershipRule(CAttr)) |
| 1573 | ; |
| 1574 | else { |
| 1575 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1576 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1577 | Diag(Property->getLocation(), diag::warn_readonly_property) |
| 1578 | << Property->getDeclName() << inheritedName; |
| 1579 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 1580 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1581 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Fariborz Jahanian | b809a0e | 2013-10-04 18:06:08 +0000 | [diff] [blame] | 1582 | << Property->getDeclName() << "copy" << inheritedName; |
| 1583 | else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){ |
| 1584 | unsigned CAttrRetain = |
| 1585 | (CAttr & |
| 1586 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1587 | unsigned SAttrRetain = |
| 1588 | (SAttr & |
| 1589 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1590 | bool CStrong = (CAttrRetain != 0); |
| 1591 | bool SStrong = (SAttrRetain != 0); |
| 1592 | if (CStrong != SStrong) |
| 1593 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1594 | << Property->getDeclName() << "retain (or strong)" << inheritedName; |
| 1595 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1596 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1597 | |
Douglas Gregor | 429183e | 2015-12-09 22:57:32 +0000 | [diff] [blame] | 1598 | // Check for nonatomic; note that nonatomic is effectively |
| 1599 | // meaningless for readonly properties, so don't diagnose if the |
| 1600 | // atomic property is 'readonly'. |
Douglas Gregor | 9dd25b7 | 2015-12-10 23:02:09 +0000 | [diff] [blame] | 1601 | checkAtomicPropertyMismatch(*this, SuperProperty, Property, false); |
Fariborz Jahanian | 234c00d | 2013-02-10 00:16:04 +0000 | [diff] [blame] | 1602 | if (Property->getSetterName() != SuperProperty->getSetterName()) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1603 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1604 | << Property->getDeclName() << "setter" << inheritedName; |
Fariborz Jahanian | 234c00d | 2013-02-10 00:16:04 +0000 | [diff] [blame] | 1605 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1606 | } |
| 1607 | if (Property->getGetterName() != SuperProperty->getGetterName()) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1608 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1609 | << Property->getDeclName() << "getter" << inheritedName; |
Fariborz Jahanian | 234c00d | 2013-02-10 00:16:04 +0000 | [diff] [blame] | 1610 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1611 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1612 | |
| 1613 | QualType LHSType = |
| 1614 | Context.getCanonicalType(SuperProperty->getType()); |
| 1615 | QualType RHSType = |
| 1616 | Context.getCanonicalType(Property->getType()); |
| 1617 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 1618 | if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
Fariborz Jahanian | 17585e7 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1619 | // Do cases not handled in above. |
| 1620 | // FIXME. For future support of covariant property types, revisit this. |
| 1621 | bool IncompatibleObjC = false; |
| 1622 | QualType ConvertedType; |
| 1623 | if (!isObjCPointerConversion(RHSType, LHSType, |
| 1624 | ConvertedType, IncompatibleObjC) || |
Fariborz Jahanian | fa643c8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1625 | IncompatibleObjC) { |
Fariborz Jahanian | 17585e7 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1626 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 1627 | << Property->getType() << SuperProperty->getType() << inheritedName; |
Fariborz Jahanian | fa643c8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1628 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1629 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 1634 | ObjCMethodDecl *GetterMethod, |
| 1635 | SourceLocation Loc) { |
Fariborz Jahanian | 0ebc0fa | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1636 | if (!GetterMethod) |
| 1637 | return false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1638 | QualType GetterType = GetterMethod->getReturnType().getNonReferenceType(); |
Akira Hatanaka | de6f25f | 2016-05-26 00:37:30 +0000 | [diff] [blame] | 1639 | QualType PropertyRValueType = |
| 1640 | property->getType().getNonReferenceType().getAtomicUnqualifiedType(); |
| 1641 | bool compat = Context.hasSameType(PropertyRValueType, GetterType); |
Fariborz Jahanian | 0ebc0fa | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1642 | if (!compat) { |
Douglas Gregor | 1cbb289 | 2015-12-08 22:45:17 +0000 | [diff] [blame] | 1643 | const ObjCObjectPointerType *propertyObjCPtr = nullptr; |
| 1644 | const ObjCObjectPointerType *getterObjCPtr = nullptr; |
Akira Hatanaka | de6f25f | 2016-05-26 00:37:30 +0000 | [diff] [blame] | 1645 | if ((propertyObjCPtr = |
| 1646 | PropertyRValueType->getAs<ObjCObjectPointerType>()) && |
Douglas Gregor | 1cbb289 | 2015-12-08 22:45:17 +0000 | [diff] [blame] | 1647 | (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>())) |
| 1648 | compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr); |
Akira Hatanaka | de6f25f | 2016-05-26 00:37:30 +0000 | [diff] [blame] | 1649 | else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType) |
Fariborz Jahanian | 0ebc0fa | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1650 | != Compatible) { |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1651 | Diag(Loc, diag::err_property_accessor_type) |
Akira Hatanaka | de6f25f | 2016-05-26 00:37:30 +0000 | [diff] [blame] | 1652 | << property->getDeclName() << PropertyRValueType |
Fariborz Jahanian | 0ebc0fa | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1653 | << GetterMethod->getSelector() << GetterType; |
| 1654 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1655 | return true; |
| 1656 | } else { |
| 1657 | compat = true; |
Akira Hatanaka | de6f25f | 2016-05-26 00:37:30 +0000 | [diff] [blame] | 1658 | QualType lhsType = Context.getCanonicalType(PropertyRValueType); |
Fariborz Jahanian | 0ebc0fa | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1659 | QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType(); |
| 1660 | if (lhsType != rhsType && lhsType->isArithmeticType()) |
| 1661 | compat = false; |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1662 | } |
| 1663 | } |
Fariborz Jahanian | 0ebc0fa | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1664 | |
| 1665 | if (!compat) { |
| 1666 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
| 1667 | << property->getDeclName() |
| 1668 | << GetterMethod->getSelector(); |
| 1669 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1670 | return true; |
| 1671 | } |
| 1672 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1673 | return false; |
| 1674 | } |
| 1675 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1676 | /// CollectImmediateProperties - This routine collects all properties in |
Douglas Gregor | a669ab9 | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1677 | /// the class and its conforming protocols; but not those in its super class. |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1678 | static void |
| 1679 | CollectImmediateProperties(ObjCContainerDecl *CDecl, |
| 1680 | ObjCContainerDecl::PropertyMap &PropMap, |
| 1681 | ObjCContainerDecl::PropertyMap &SuperPropMap, |
| 1682 | bool CollectClassPropsOnly = false, |
| 1683 | bool IncludeProtocols = true) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1684 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1685 | for (auto *Prop : IDecl->properties()) { |
| 1686 | if (CollectClassPropsOnly && !Prop->isClassProperty()) |
| 1687 | continue; |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 1688 | PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = |
| 1689 | Prop; |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1690 | } |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 1691 | |
| 1692 | // Collect the properties from visible extensions. |
| 1693 | for (auto *Ext : IDecl->visible_extensions()) |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1694 | CollectImmediateProperties(Ext, PropMap, SuperPropMap, |
| 1695 | CollectClassPropsOnly, IncludeProtocols); |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 1696 | |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1697 | if (IncludeProtocols) { |
| 1698 | // Scan through class's protocols. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 1699 | for (auto *PI : IDecl->all_referenced_protocols()) |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1700 | CollectImmediateProperties(PI, PropMap, SuperPropMap, |
| 1701 | CollectClassPropsOnly); |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1702 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1703 | } |
| 1704 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1705 | for (auto *Prop : CATDecl->properties()) { |
| 1706 | if (CollectClassPropsOnly && !Prop->isClassProperty()) |
| 1707 | continue; |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 1708 | PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = |
| 1709 | Prop; |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1710 | } |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1711 | if (IncludeProtocols) { |
| 1712 | // Scan through class's protocols. |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 1713 | for (auto *PI : CATDecl->protocols()) |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1714 | CollectImmediateProperties(PI, PropMap, SuperPropMap, |
| 1715 | CollectClassPropsOnly); |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1716 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1717 | } |
| 1718 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 1719 | for (auto *Prop : PDecl->properties()) { |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1720 | if (CollectClassPropsOnly && !Prop->isClassProperty()) |
| 1721 | continue; |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 1722 | ObjCPropertyDecl *PropertyFromSuper = |
| 1723 | SuperPropMap[std::make_pair(Prop->getIdentifier(), |
| 1724 | Prop->isClassProperty())]; |
Fariborz Jahanian | 66f9a65 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1725 | // Exclude property for protocols which conform to class's super-class, |
| 1726 | // as super-class has to implement the property. |
Fariborz Jahanian | 698bd31 | 2011-09-27 00:23:52 +0000 | [diff] [blame] | 1727 | if (!PropertyFromSuper || |
| 1728 | PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 1729 | ObjCPropertyDecl *&PropEntry = |
| 1730 | PropMap[std::make_pair(Prop->getIdentifier(), |
| 1731 | Prop->isClassProperty())]; |
Fariborz Jahanian | 66f9a65 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1732 | if (!PropEntry) |
| 1733 | PropEntry = Prop; |
| 1734 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1735 | } |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1736 | // Scan through protocol's protocols. |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1737 | for (auto *PI : PDecl->protocols()) |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1738 | CollectImmediateProperties(PI, PropMap, SuperPropMap, |
| 1739 | CollectClassPropsOnly); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1743 | /// CollectSuperClassPropertyImplementations - This routine collects list of |
| 1744 | /// properties to be implemented in super class(s) and also coming from their |
| 1745 | /// conforming protocols. |
| 1746 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
Anna Zaks | 408f7d0 | 2012-10-31 01:18:22 +0000 | [diff] [blame] | 1747 | ObjCInterfaceDecl::PropertyMap &PropMap) { |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1748 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
Fariborz Jahanian | aedaaa4 | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1749 | ObjCInterfaceDecl::PropertyDeclOrder PO; |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1750 | while (SDecl) { |
Fariborz Jahanian | aedaaa4 | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1751 | SDecl->collectPropertiesToImplement(PropMap, PO); |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1752 | SDecl = SDecl->getSuperClass(); |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | |
Fariborz Jahanian | a934a02 | 2013-02-14 19:07:19 +0000 | [diff] [blame] | 1757 | /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is |
| 1758 | /// an ivar synthesized for 'Method' and 'Method' is a property accessor |
| 1759 | /// declared in class 'IFace'. |
| 1760 | bool |
| 1761 | Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace, |
| 1762 | ObjCMethodDecl *Method, ObjCIvarDecl *IV) { |
| 1763 | if (!IV->getSynthesize()) |
| 1764 | return false; |
| 1765 | ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(), |
| 1766 | Method->isInstanceMethod()); |
| 1767 | if (!IMD || !IMD->isPropertyAccessor()) |
| 1768 | return false; |
| 1769 | |
| 1770 | // look up a property declaration whose one of its accessors is implemented |
| 1771 | // by this method. |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 1772 | for (const auto *Property : IFace->instance_properties()) { |
Aaron Ballman | dc4bea4 | 2014-03-13 18:47:37 +0000 | [diff] [blame] | 1773 | if ((Property->getGetterName() == IMD->getSelector() || |
| 1774 | Property->getSetterName() == IMD->getSelector()) && |
| 1775 | (Property->getPropertyIvarDecl() == IV)) |
Fariborz Jahanian | a934a02 | 2013-02-14 19:07:19 +0000 | [diff] [blame] | 1776 | return true; |
| 1777 | } |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 1778 | // Also look up property declaration in class extension whose one of its |
| 1779 | // accessors is implemented by this method. |
| 1780 | for (const auto *Ext : IFace->known_extensions()) |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 1781 | for (const auto *Property : Ext->instance_properties()) |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 1782 | if ((Property->getGetterName() == IMD->getSelector() || |
| 1783 | Property->getSetterName() == IMD->getSelector()) && |
| 1784 | (Property->getPropertyIvarDecl() == IV)) |
| 1785 | return true; |
Fariborz Jahanian | a934a02 | 2013-02-14 19:07:19 +0000 | [diff] [blame] | 1786 | return false; |
| 1787 | } |
| 1788 | |
Fariborz Jahanian | 6766f8d | 2014-03-05 23:44:00 +0000 | [diff] [blame] | 1789 | static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl, |
| 1790 | ObjCPropertyDecl *Prop) { |
| 1791 | bool SuperClassImplementsGetter = false; |
| 1792 | bool SuperClassImplementsSetter = false; |
| 1793 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1794 | SuperClassImplementsSetter = true; |
Bob Wilson | 3ca7904 | 2014-03-11 17:17:16 +0000 | [diff] [blame] | 1795 | |
Fariborz Jahanian | 6766f8d | 2014-03-05 23:44:00 +0000 | [diff] [blame] | 1796 | while (IDecl->getSuperClass()) { |
| 1797 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 1798 | if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName())) |
| 1799 | SuperClassImplementsGetter = true; |
Bob Wilson | 3ca7904 | 2014-03-11 17:17:16 +0000 | [diff] [blame] | 1800 | |
Fariborz Jahanian | 6766f8d | 2014-03-05 23:44:00 +0000 | [diff] [blame] | 1801 | if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName())) |
| 1802 | SuperClassImplementsSetter = true; |
| 1803 | if (SuperClassImplementsGetter && SuperClassImplementsSetter) |
| 1804 | return true; |
| 1805 | IDecl = IDecl->getSuperClass(); |
| 1806 | } |
| 1807 | return false; |
| 1808 | } |
Fariborz Jahanian | a934a02 | 2013-02-14 19:07:19 +0000 | [diff] [blame] | 1809 | |
James Dennett | 2a4d13c | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1810 | /// \brief Default synthesizes all properties which must be synthesized |
| 1811 | /// in class's \@implementation. |
Alex Lorenz | 6c9af50 | 2017-07-03 10:12:24 +0000 | [diff] [blame] | 1812 | void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl, |
| 1813 | ObjCInterfaceDecl *IDecl, |
| 1814 | SourceLocation AtEnd) { |
Anna Zaks | 673d76b | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1815 | ObjCInterfaceDecl::PropertyMap PropMap; |
Fariborz Jahanian | aedaaa4 | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1816 | ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder; |
| 1817 | IDecl->collectPropertiesToImplement(PropMap, PropertyOrder); |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1818 | if (PropMap.empty()) |
| 1819 | return; |
Anna Zaks | 673d76b | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1820 | ObjCInterfaceDecl::PropertyMap SuperPropMap; |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1821 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1822 | |
Fariborz Jahanian | aedaaa4 | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1823 | for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) { |
| 1824 | ObjCPropertyDecl *Prop = PropertyOrder[i]; |
Fariborz Jahanian | bbc126e | 2013-06-07 20:26:51 +0000 | [diff] [blame] | 1825 | // Is there a matching property synthesize/dynamic? |
| 1826 | if (Prop->isInvalidDecl() || |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 1827 | Prop->isClassProperty() || |
Fariborz Jahanian | bbc126e | 2013-06-07 20:26:51 +0000 | [diff] [blame] | 1828 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 1829 | continue; |
| 1830 | // Property may have been synthesized by user. |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1831 | if (IMPDecl->FindPropertyImplDecl( |
| 1832 | Prop->getIdentifier(), Prop->getQueryKind())) |
Fariborz Jahanian | bbc126e | 2013-06-07 20:26:51 +0000 | [diff] [blame] | 1833 | continue; |
| 1834 | if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { |
| 1835 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1836 | continue; |
| 1837 | if (IMPDecl->getInstanceMethod(Prop->getSetterName())) |
| 1838 | continue; |
| 1839 | } |
Fariborz Jahanian | 4614524 | 2013-06-07 18:32:55 +0000 | [diff] [blame] | 1840 | if (ObjCPropertyImplDecl *PID = |
| 1841 | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) { |
Fariborz Jahanian | 6c9ee7b | 2014-07-26 20:52:26 +0000 | [diff] [blame] | 1842 | Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property) |
| 1843 | << Prop->getIdentifier(); |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 1844 | if (PID->getLocation().isValid()) |
Fariborz Jahanian | 6c9ee7b | 2014-07-26 20:52:26 +0000 | [diff] [blame] | 1845 | Diag(PID->getLocation(), diag::note_property_synthesize); |
Fariborz Jahanian | 4614524 | 2013-06-07 18:32:55 +0000 | [diff] [blame] | 1846 | continue; |
| 1847 | } |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 1848 | ObjCPropertyDecl *PropInSuperClass = |
| 1849 | SuperPropMap[std::make_pair(Prop->getIdentifier(), |
| 1850 | Prop->isClassProperty())]; |
Ted Kremenek | 6d69ac8 | 2013-12-12 23:40:14 +0000 | [diff] [blame] | 1851 | if (ObjCProtocolDecl *Proto = |
| 1852 | dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) { |
Fariborz Jahanian | 9e49b6a | 2011-12-15 01:03:18 +0000 | [diff] [blame] | 1853 | // We won't auto-synthesize properties declared in protocols. |
Fariborz Jahanian | 6766f8d | 2014-03-05 23:44:00 +0000 | [diff] [blame] | 1854 | // Suppress the warning if class's superclass implements property's |
| 1855 | // getter and implements property's setter (if readwrite property). |
Fariborz Jahanian | 3b23008 | 2014-08-29 20:29:31 +0000 | [diff] [blame] | 1856 | // Or, if property is going to be implemented in its super class. |
| 1857 | if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) { |
Fariborz Jahanian | 6766f8d | 2014-03-05 23:44:00 +0000 | [diff] [blame] | 1858 | Diag(IMPDecl->getLocation(), |
| 1859 | diag::warn_auto_synthesizing_protocol_property) |
| 1860 | << Prop << Proto; |
| 1861 | Diag(Prop->getLocation(), diag::note_property_declare); |
Alex Lorenz | 6c9af50 | 2017-07-03 10:12:24 +0000 | [diff] [blame] | 1862 | std::string FixIt = |
| 1863 | (Twine("@synthesize ") + Prop->getName() + ";\n\n").str(); |
| 1864 | Diag(AtEnd, diag::note_add_synthesize_directive) |
| 1865 | << FixItHint::CreateInsertion(AtEnd, FixIt); |
Fariborz Jahanian | 6766f8d | 2014-03-05 23:44:00 +0000 | [diff] [blame] | 1866 | } |
Fariborz Jahanian | 9e49b6a | 2011-12-15 01:03:18 +0000 | [diff] [blame] | 1867 | continue; |
| 1868 | } |
Fariborz Jahanian | c9b7715 | 2014-08-29 18:31:16 +0000 | [diff] [blame] | 1869 | // If property to be implemented in the super class, ignore. |
Fariborz Jahanian | 3b23008 | 2014-08-29 20:29:31 +0000 | [diff] [blame] | 1870 | if (PropInSuperClass) { |
Fariborz Jahanian | c9b7715 | 2014-08-29 18:31:16 +0000 | [diff] [blame] | 1871 | if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) && |
| 1872 | (PropInSuperClass->getPropertyAttributes() & |
| 1873 | ObjCPropertyDecl::OBJC_PR_readonly) && |
| 1874 | !IMPDecl->getInstanceMethod(Prop->getSetterName()) && |
| 1875 | !IDecl->HasUserDeclaredSetterMethod(Prop)) { |
| 1876 | Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property) |
| 1877 | << Prop->getIdentifier(); |
| 1878 | Diag(PropInSuperClass->getLocation(), diag::note_property_declare); |
| 1879 | } |
| 1880 | else { |
| 1881 | Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass) |
| 1882 | << Prop->getIdentifier(); |
Fariborz Jahanian | c985a7f | 2014-10-10 22:08:23 +0000 | [diff] [blame] | 1883 | Diag(PropInSuperClass->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | c9b7715 | 2014-08-29 18:31:16 +0000 | [diff] [blame] | 1884 | Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); |
| 1885 | } |
| 1886 | continue; |
| 1887 | } |
Ted Kremenek | 74a9f98 | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1888 | // We use invalid SourceLocations for the synthesized ivars since they |
| 1889 | // aren't really synthesized at a particular location; they just exist. |
| 1890 | // Saying that they are located at the @implementation isn't really going |
| 1891 | // to help users. |
Fariborz Jahanian | d5f34f9 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1892 | ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( |
| 1893 | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), |
| 1894 | true, |
| 1895 | /* property = */ Prop->getIdentifier(), |
Anna Zaks | 454477c | 2012-09-27 19:45:11 +0000 | [diff] [blame] | 1896 | /* ivar = */ Prop->getDefaultSynthIvarName(Context), |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1897 | Prop->getLocation(), Prop->getQueryKind())); |
Alex Lorenz | 1e23dd6 | 2017-08-15 12:40:01 +0000 | [diff] [blame^] | 1898 | if (PIDecl && !Prop->isUnavailable()) { |
Fariborz Jahanian | d5f34f9 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1899 | Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis); |
Fariborz Jahanian | d6886e7 | 2012-05-08 18:03:39 +0000 | [diff] [blame] | 1900 | Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); |
Fariborz Jahanian | d5f34f9 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1901 | } |
Ted Kremenek | 74a9f98 | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1902 | } |
Fariborz Jahanian | bdb1b0d | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1903 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1904 | |
Alex Lorenz | 6c9af50 | 2017-07-03 10:12:24 +0000 | [diff] [blame] | 1905 | void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D, |
| 1906 | SourceLocation AtEnd) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1907 | if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile()) |
Fariborz Jahanian | 97d744b | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1908 | return; |
| 1909 | ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); |
| 1910 | if (!IC) |
| 1911 | return; |
| 1912 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1913 | if (!IDecl->isObjCRequiresPropertyDefs()) |
Alex Lorenz | 6c9af50 | 2017-07-03 10:12:24 +0000 | [diff] [blame] | 1914 | DefaultSynthesizeProperties(S, IC, IDecl, AtEnd); |
Fariborz Jahanian | 97d744b | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Manman Ren | 08ce734 | 2016-05-18 18:12:34 +0000 | [diff] [blame] | 1917 | static void DiagnoseUnimplementedAccessor( |
| 1918 | Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method, |
| 1919 | ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C, |
| 1920 | ObjCPropertyDecl *Prop, |
| 1921 | llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) { |
| 1922 | // Check to see if we have a corresponding selector in SMap and with the |
| 1923 | // right method type. |
| 1924 | auto I = std::find_if(SMap.begin(), SMap.end(), |
| 1925 | [&](const ObjCMethodDecl *x) { |
| 1926 | return x->getSelector() == Method && |
| 1927 | x->isClassMethod() == Prop->isClassProperty(); |
| 1928 | }); |
Ted Kremenek | 7e81295 | 2014-02-21 19:41:30 +0000 | [diff] [blame] | 1929 | // When reporting on missing property setter/getter implementation in |
| 1930 | // categories, do not report when they are declared in primary class, |
| 1931 | // class's protocol, or one of it super classes. This is because, |
| 1932 | // the class is going to implement them. |
Manman Ren | 08ce734 | 2016-05-18 18:12:34 +0000 | [diff] [blame] | 1933 | if (I == SMap.end() && |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1934 | (PrimaryClass == nullptr || |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 1935 | !PrimaryClass->lookupPropertyAccessor(Method, C, |
| 1936 | Prop->isClassProperty()))) { |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1937 | unsigned diag = |
| 1938 | isa<ObjCCategoryDecl>(CDecl) |
| 1939 | ? (Prop->isClassProperty() |
| 1940 | ? diag::warn_impl_required_in_category_for_class_property |
| 1941 | : diag::warn_setter_getter_impl_required_in_category) |
| 1942 | : (Prop->isClassProperty() |
| 1943 | ? diag::warn_impl_required_for_class_property |
| 1944 | : diag::warn_setter_getter_impl_required); |
| 1945 | S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method; |
| 1946 | S.Diag(Prop->getLocation(), diag::note_property_declare); |
| 1947 | if (S.LangOpts.ObjCDefaultSynthProperties && |
| 1948 | S.LangOpts.ObjCRuntime.isNonFragile()) |
| 1949 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
| 1950 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
| 1951 | S.Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
| 1952 | } |
Ted Kremenek | 7e81295 | 2014-02-21 19:41:30 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Fariborz Jahanian | 25491a2 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1955 | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 1956 | ObjCContainerDecl *CDecl, |
| 1957 | bool SynthesizeProperties) { |
Anna Zaks | 673d76b | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1958 | ObjCContainerDecl::PropertyMap PropMap; |
Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 1959 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 1960 | |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1961 | // Since we don't synthesize class properties, we should emit diagnose even |
| 1962 | // if SynthesizeProperties is true. |
| 1963 | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
| 1964 | // Gather properties which need not be implemented in this class |
| 1965 | // or category. |
| 1966 | if (!IDecl) |
| 1967 | if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1968 | // For categories, no need to implement properties declared in |
| 1969 | // its primary class (and its super classes) if property is |
| 1970 | // declared in one of those containers. |
| 1971 | if ((IDecl = C->getClassInterface())) { |
| 1972 | ObjCInterfaceDecl::PropertyDeclOrder PO; |
| 1973 | IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO); |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 1974 | } |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1975 | } |
| 1976 | if (IDecl) |
| 1977 | CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap); |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 1978 | |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 1979 | // When SynthesizeProperties is true, we only check class properties. |
| 1980 | CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap, |
| 1981 | SynthesizeProperties/*CollectClassPropsOnly*/); |
Ted Kremenek | 348e88c | 2014-02-21 19:41:34 +0000 | [diff] [blame] | 1982 | |
Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 1983 | // Scan the @interface to see if any of the protocols it adopts |
| 1984 | // require an explicit implementation, via attribute |
| 1985 | // 'objc_protocol_requires_explicit_implementation'. |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1986 | if (IDecl) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1987 | std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap; |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1988 | |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 1989 | for (auto *PDecl : IDecl->all_referenced_protocols()) { |
Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 1990 | if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 1991 | continue; |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1992 | // Lazily construct a set of all the properties in the @interface |
| 1993 | // of the class, without looking at the superclass. We cannot |
| 1994 | // use the call to CollectImmediateProperties() above as that |
Eric Christopher | c9e2a68 | 2014-05-20 17:10:39 +0000 | [diff] [blame] | 1995 | // utilizes information from the super class's properties as well |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 1996 | // as scans the adopted protocols. This work only triggers for protocols |
| 1997 | // with the attribute, which is very rare, and only occurs when |
| 1998 | // analyzing the @implementation. |
| 1999 | if (!LazyMap) { |
| 2000 | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
| 2001 | LazyMap.reset(new ObjCContainerDecl::PropertyMap()); |
| 2002 | CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap, |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 2003 | /* CollectClassPropsOnly */ false, |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 2004 | /* IncludeProtocols */ false); |
| 2005 | } |
Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 2006 | // Add the properties of 'PDecl' to the list of properties that |
| 2007 | // need to be implemented. |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 2008 | for (auto *PropDecl : PDecl->properties()) { |
| 2009 | if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(), |
| 2010 | PropDecl->isClassProperty())]) |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 2011 | continue; |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 2012 | PropMap[std::make_pair(PropDecl->getIdentifier(), |
| 2013 | PropDecl->isClassProperty())] = PropDecl; |
Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 2014 | } |
| 2015 | } |
Ted Kremenek | 204c3c5 | 2014-02-22 00:02:03 +0000 | [diff] [blame] | 2016 | } |
Ted Kremenek | 3888202 | 2014-02-21 19:41:39 +0000 | [diff] [blame] | 2017 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2018 | if (PropMap.empty()) |
| 2019 | return; |
| 2020 | |
| 2021 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 2022 | for (const auto *I : IMPDecl->property_impls()) |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2023 | PropImplMap.insert(I->getPropertyDecl()); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2024 | |
Manman Ren | 08ce734 | 2016-05-18 18:12:34 +0000 | [diff] [blame] | 2025 | llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap; |
Fariborz Jahanian | eb3f100 | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 2026 | // Collect property accessors implemented in current implementation. |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 2027 | for (const auto *I : IMPDecl->methods()) |
Manman Ren | 08ce734 | 2016-05-18 18:12:34 +0000 | [diff] [blame] | 2028 | InsMap.insert(I); |
Fariborz Jahanian | eb3f100 | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 2029 | |
| 2030 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2031 | ObjCInterfaceDecl *PrimaryClass = nullptr; |
Fariborz Jahanian | eb3f100 | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 2032 | if (C && !C->IsClassExtension()) |
| 2033 | if ((PrimaryClass = C->getClassInterface())) |
| 2034 | // Report unimplemented properties in the category as well. |
| 2035 | if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) { |
| 2036 | // When reporting on missing setter/getters, do not report when |
| 2037 | // setter/getter is implemented in category's primary class |
| 2038 | // implementation. |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 2039 | for (const auto *I : IMP->methods()) |
Manman Ren | 08ce734 | 2016-05-18 18:12:34 +0000 | [diff] [blame] | 2040 | InsMap.insert(I); |
Fariborz Jahanian | eb3f100 | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Anna Zaks | 673d76b | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 2043 | for (ObjCContainerDecl::PropertyMap::iterator |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2044 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 2045 | ObjCPropertyDecl *Prop = P->second; |
Manman Ren | 16a7d63 | 2016-04-12 23:01:55 +0000 | [diff] [blame] | 2046 | // Is there a matching property synthesize/dynamic? |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2047 | if (Prop->isInvalidDecl() || |
| 2048 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
Douglas Gregor | c4c1fb3 | 2013-01-08 18:16:18 +0000 | [diff] [blame] | 2049 | PropImplMap.count(Prop) || |
| 2050 | Prop->getAvailability() == AR_Unavailable) |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2051 | continue; |
Ted Kremenek | 7e81295 | 2014-02-21 19:41:30 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Diagnose unimplemented getters and setters. |
| 2054 | DiagnoseUnimplementedAccessor(*this, |
| 2055 | PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap); |
| 2056 | if (!Prop->isReadOnly()) |
| 2057 | DiagnoseUnimplementedAccessor(*this, |
| 2058 | PrimaryClass, Prop->getSetterName(), |
| 2059 | IMPDecl, CDecl, C, Prop, InsMap); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2060 | } |
| 2061 | } |
| 2062 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2063 | void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) { |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2064 | for (const auto *propertyImpl : impDecl->property_impls()) { |
| 2065 | const auto *property = propertyImpl->getPropertyDecl(); |
| 2066 | |
| 2067 | // Warn about null_resettable properties with synthesized setters, |
| 2068 | // because the setter won't properly handle nil. |
| 2069 | if (propertyImpl->getPropertyImplementation() |
| 2070 | == ObjCPropertyImplDecl::Synthesize && |
| 2071 | (property->getPropertyAttributes() & |
| 2072 | ObjCPropertyDecl::OBJC_PR_null_resettable) && |
| 2073 | property->getGetterMethodDecl() && |
| 2074 | property->getSetterMethodDecl()) { |
| 2075 | auto *getterMethod = property->getGetterMethodDecl(); |
| 2076 | auto *setterMethod = property->getSetterMethodDecl(); |
| 2077 | if (!impDecl->getInstanceMethod(setterMethod->getSelector()) && |
| 2078 | !impDecl->getInstanceMethod(getterMethod->getSelector())) { |
| 2079 | SourceLocation loc = propertyImpl->getLocation(); |
| 2080 | if (loc.isInvalid()) |
| 2081 | loc = impDecl->getLocStart(); |
| 2082 | |
| 2083 | Diag(loc, diag::warn_null_resettable_setter) |
| 2084 | << setterMethod->getSelector() << property->getDeclName(); |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | } |
| 2089 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2090 | void |
| 2091 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2092 | ObjCInterfaceDecl* IDecl) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2093 | // Rules apply in non-GC mode only |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2094 | if (getLangOpts().getGC() != LangOptions::NonGC) |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2095 | return; |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2096 | ObjCContainerDecl::PropertyMap PM; |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 2097 | for (auto *Prop : IDecl->properties()) |
| 2098 | PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2099 | for (const auto *Ext : IDecl->known_extensions()) |
Manman Ren | 494ee5b | 2016-01-28 23:36:05 +0000 | [diff] [blame] | 2100 | for (auto *Prop : Ext->properties()) |
| 2101 | PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2102 | |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 2103 | for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end(); |
| 2104 | I != E; ++I) { |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2105 | const ObjCPropertyDecl *Property = I->second; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2106 | ObjCMethodDecl *GetterMethod = nullptr; |
| 2107 | ObjCMethodDecl *SetterMethod = nullptr; |
Argyrios Kyrtzidis | dd88dbf | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 2108 | bool LookedUpGetterSetter = false; |
| 2109 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2110 | unsigned Attributes = Property->getPropertyAttributes(); |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 2111 | unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); |
Argyrios Kyrtzidis | dd88dbf | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 2112 | |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 2113 | if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && |
| 2114 | !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2115 | GetterMethod = Property->isClassProperty() ? |
| 2116 | IMPDecl->getClassMethod(Property->getGetterName()) : |
| 2117 | IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 2118 | SetterMethod = Property->isClassProperty() ? |
| 2119 | IMPDecl->getClassMethod(Property->getSetterName()) : |
| 2120 | IMPDecl->getInstanceMethod(Property->getSetterName()); |
Argyrios Kyrtzidis | dd88dbf | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 2121 | LookedUpGetterSetter = true; |
| 2122 | if (GetterMethod) { |
| 2123 | Diag(GetterMethod->getLocation(), |
| 2124 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | b5b5a59 | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 2125 | << Property->getIdentifier() << 0; |
Argyrios Kyrtzidis | dd88dbf | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 2126 | Diag(Property->getLocation(), diag::note_property_declare); |
| 2127 | } |
| 2128 | if (SetterMethod) { |
| 2129 | Diag(SetterMethod->getLocation(), |
| 2130 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | b5b5a59 | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 2131 | << Property->getIdentifier() << 1; |
Argyrios Kyrtzidis | dd88dbf | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 2132 | Diag(Property->getLocation(), diag::note_property_declare); |
| 2133 | } |
| 2134 | } |
| 2135 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2136 | // We only care about readwrite atomic property. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2137 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 2138 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2139 | continue; |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 2140 | if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl( |
| 2141 | Property->getIdentifier(), Property->getQueryKind())) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2142 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 2143 | continue; |
Argyrios Kyrtzidis | dd88dbf | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 2144 | if (!LookedUpGetterSetter) { |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2145 | GetterMethod = Property->isClassProperty() ? |
| 2146 | IMPDecl->getClassMethod(Property->getGetterName()) : |
| 2147 | IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 2148 | SetterMethod = Property->isClassProperty() ? |
| 2149 | IMPDecl->getClassMethod(Property->getSetterName()) : |
| 2150 | IMPDecl->getInstanceMethod(Property->getSetterName()); |
Argyrios Kyrtzidis | dd88dbf | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 2151 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2152 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 2153 | SourceLocation MethodLoc = |
| 2154 | (GetterMethod ? GetterMethod->getLocation() |
| 2155 | : SetterMethod->getLocation()); |
| 2156 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2157 | << Property->getIdentifier() << (GetterMethod != nullptr) |
| 2158 | << (SetterMethod != nullptr); |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 2159 | // fixit stuff. |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2160 | if (Property->getLParenLoc().isValid() && |
| 2161 | !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) { |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 2162 | // @property () ... case. |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2163 | SourceLocation AfterLParen = |
| 2164 | getLocForEndOfToken(Property->getLParenLoc()); |
| 2165 | StringRef NonatomicStr = AttributesAsWritten? "nonatomic, " |
| 2166 | : "nonatomic"; |
| 2167 | Diag(Property->getLocation(), |
| 2168 | diag::note_atomic_property_fixup_suggest) |
| 2169 | << FixItHint::CreateInsertion(AfterLParen, NonatomicStr); |
| 2170 | } else if (Property->getLParenLoc().isInvalid()) { |
| 2171 | //@property id etc. |
| 2172 | SourceLocation startLoc = |
| 2173 | Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| 2174 | Diag(Property->getLocation(), |
| 2175 | diag::note_atomic_property_fixup_suggest) |
| 2176 | << FixItHint::CreateInsertion(startLoc, "(nonatomic) "); |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 2177 | } |
| 2178 | else |
| 2179 | Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2180 | Diag(Property->getLocation(), diag::note_property_declare); |
| 2181 | } |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2186 | void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2187 | if (getLangOpts().getGC() == LangOptions::GCOnly) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2188 | return; |
| 2189 | |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 2190 | for (const auto *PID : D->property_impls()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2191 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | f4105f5 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 2192 | if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2193 | !PD->isClassProperty() && |
Fariborz Jahanian | f4105f5 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 2194 | !D->getInstanceMethod(PD->getGetterName())) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2195 | ObjCMethodDecl *method = PD->getGetterMethodDecl(); |
| 2196 | if (!method) |
| 2197 | continue; |
| 2198 | ObjCMethodFamily family = method->getMethodFamily(); |
| 2199 | if (family == OMF_alloc || family == OMF_copy || |
| 2200 | family == OMF_mutableCopy || family == OMF_new) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2201 | if (getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 65b1377 | 2014-01-10 00:53:48 +0000 | [diff] [blame] | 2202 | Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2203 | else |
Fariborz Jahanian | 65b1377 | 2014-01-10 00:53:48 +0000 | [diff] [blame] | 2204 | Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule); |
Jordan Rose | a34d04d | 2015-01-16 23:04:31 +0000 | [diff] [blame] | 2205 | |
| 2206 | // Look for a getter explicitly declared alongside the property. |
| 2207 | // If we find one, use its location for the note. |
| 2208 | SourceLocation noteLoc = PD->getLocation(); |
| 2209 | SourceLocation fixItLoc; |
| 2210 | for (auto *getterRedecl : method->redecls()) { |
| 2211 | if (getterRedecl->isImplicit()) |
| 2212 | continue; |
| 2213 | if (getterRedecl->getDeclContext() != PD->getDeclContext()) |
| 2214 | continue; |
| 2215 | noteLoc = getterRedecl->getLocation(); |
| 2216 | fixItLoc = getterRedecl->getLocEnd(); |
| 2217 | } |
| 2218 | |
| 2219 | Preprocessor &PP = getPreprocessor(); |
| 2220 | TokenValue tokens[] = { |
| 2221 | tok::kw___attribute, tok::l_paren, tok::l_paren, |
| 2222 | PP.getIdentifierInfo("objc_method_family"), tok::l_paren, |
| 2223 | PP.getIdentifierInfo("none"), tok::r_paren, |
| 2224 | tok::r_paren, tok::r_paren |
| 2225 | }; |
| 2226 | StringRef spelling = "__attribute__((objc_method_family(none)))"; |
| 2227 | StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens); |
| 2228 | if (!macroName.empty()) |
| 2229 | spelling = macroName; |
| 2230 | |
| 2231 | auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family) |
| 2232 | << method->getDeclName() << spelling; |
| 2233 | if (fixItLoc.isValid()) { |
| 2234 | SmallString<64> fixItText(" "); |
| 2235 | fixItText += spelling; |
| 2236 | noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText); |
| 2237 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | } |
| 2241 | } |
| 2242 | |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 2243 | void Sema::DiagnoseMissingDesignatedInitOverrides( |
Fariborz Jahanian | 20cfff3 | 2015-03-11 16:59:48 +0000 | [diff] [blame] | 2244 | const ObjCImplementationDecl *ImplD, |
| 2245 | const ObjCInterfaceDecl *IFD) { |
| 2246 | assert(IFD->hasDesignatedInitializers()); |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 2247 | const ObjCInterfaceDecl *SuperD = IFD->getSuperClass(); |
| 2248 | if (!SuperD) |
| 2249 | return; |
| 2250 | |
| 2251 | SelectorSet InitSelSet; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2252 | for (const auto *I : ImplD->instance_methods()) |
| 2253 | if (I->getMethodFamily() == OMF_init) |
| 2254 | InitSelSet.insert(I->getSelector()); |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 2255 | |
| 2256 | SmallVector<const ObjCMethodDecl *, 8> DesignatedInits; |
| 2257 | SuperD->getDesignatedInitializers(DesignatedInits); |
| 2258 | for (SmallVector<const ObjCMethodDecl *, 8>::iterator |
| 2259 | I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) { |
| 2260 | const ObjCMethodDecl *MD = *I; |
| 2261 | if (!InitSelSet.count(MD->getSelector())) { |
Argyrios Kyrtzidis | c0d4b00f | 2015-07-30 19:06:04 +0000 | [diff] [blame] | 2262 | bool Ignore = false; |
| 2263 | if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) { |
| 2264 | Ignore = IMD->isUnavailable(); |
| 2265 | } |
| 2266 | if (!Ignore) { |
| 2267 | Diag(ImplD->getLocation(), |
| 2268 | diag::warn_objc_implementation_missing_designated_init_override) |
| 2269 | << MD->getSelector(); |
| 2270 | Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here); |
| 2271 | } |
Argyrios Kyrtzidis | db5ce0f | 2013-12-03 21:11:54 +0000 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | |
John McCall | ad31b5f | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 2276 | /// AddPropertyAttrs - Propagates attributes from a property to the |
| 2277 | /// implicitly-declared getter or setter for that property. |
| 2278 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
| 2279 | ObjCPropertyDecl *Property) { |
| 2280 | // Should we just clone all attributes over? |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 2281 | for (const auto *A : Property->attrs()) { |
| 2282 | if (isa<DeprecatedAttr>(A) || |
| 2283 | isa<UnavailableAttr>(A) || |
| 2284 | isa<AvailabilityAttr>(A)) |
| 2285 | PropertyMethod->addAttr(A->clone(S.Context)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2286 | } |
John McCall | ad31b5f | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2289 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 2290 | /// have the property type and issue diagnostics if they don't. |
| 2291 | /// Also synthesize a getter/setter method if none exist (and update the |
Douglas Gregor | e17765e | 2015-11-03 17:02:34 +0000 | [diff] [blame] | 2292 | /// appropriate lookup tables. |
| 2293 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2294 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
Douglas Gregor | e17765e | 2015-11-03 17:02:34 +0000 | [diff] [blame] | 2295 | ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext()); |
Fariborz Jahanian | 0c1c311 | 2014-05-27 18:26:09 +0000 | [diff] [blame] | 2296 | if (CD->isInvalidDecl()) |
| 2297 | return; |
| 2298 | |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2299 | bool IsClassProperty = property->isClassProperty(); |
| 2300 | GetterMethod = IsClassProperty ? |
| 2301 | CD->getClassMethod(property->getGetterName()) : |
| 2302 | CD->getInstanceMethod(property->getGetterName()); |
| 2303 | |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2304 | // if setter or getter is not found in class extension, it might be |
| 2305 | // in the primary class. |
| 2306 | if (!GetterMethod) |
| 2307 | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) |
| 2308 | if (CatDecl->IsClassExtension()) |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2309 | GetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> |
| 2310 | getClassMethod(property->getGetterName()) : |
| 2311 | CatDecl->getClassInterface()-> |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2312 | getInstanceMethod(property->getGetterName()); |
| 2313 | |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2314 | SetterMethod = IsClassProperty ? |
| 2315 | CD->getClassMethod(property->getSetterName()) : |
| 2316 | CD->getInstanceMethod(property->getSetterName()); |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2317 | if (!SetterMethod) |
| 2318 | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) |
| 2319 | if (CatDecl->IsClassExtension()) |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2320 | SetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> |
| 2321 | getClassMethod(property->getSetterName()) : |
| 2322 | CatDecl->getClassInterface()-> |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2323 | getInstanceMethod(property->getSetterName()); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2324 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
| 2325 | property->getLocation()); |
| 2326 | |
Alex Lorenz | 535571a | 2017-03-30 13:33:51 +0000 | [diff] [blame] | 2327 | if (!property->isReadOnly() && SetterMethod) { |
| 2328 | if (Context.getCanonicalType(SetterMethod->getReturnType()) != |
| 2329 | Context.VoidTy) |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2330 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
| 2331 | if (SetterMethod->param_size() != 1 || |
Fariborz Jahanian | 0ee58d6 | 2011-09-26 22:59:09 +0000 | [diff] [blame] | 2332 | !Context.hasSameUnqualifiedType( |
Fariborz Jahanian | 7c386f8 | 2011-10-15 17:36:49 +0000 | [diff] [blame] | 2333 | (*SetterMethod->param_begin())->getType().getNonReferenceType(), |
| 2334 | property->getType().getNonReferenceType())) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2335 | Diag(property->getLocation(), |
| 2336 | diag::warn_accessor_property_type_mismatch) |
| 2337 | << property->getDeclName() |
| 2338 | << SetterMethod->getSelector(); |
| 2339 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | // Synthesize getter/setter methods if none exist. |
| 2344 | // Find the default getter and if one not found, add one. |
| 2345 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 2346 | // synthesize these methods unless the user explicitly provided prototypes |
| 2347 | // (which is odd, but allowed). Sema should be typechecking that the |
| 2348 | // declarations jive in that situation (which it is not currently). |
| 2349 | if (!GetterMethod) { |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2350 | // No instance/class method of same name as property getter name was found. |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2351 | // Declare a getter method and add it to the list of methods |
| 2352 | // for this class. |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2353 | SourceLocation Loc = property->getLocation(); |
Ted Kremenek | 2f07563 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 2354 | |
Akira Hatanaka | de6f25f | 2016-05-26 00:37:30 +0000 | [diff] [blame] | 2355 | // The getter returns the declared property type with all qualifiers |
| 2356 | // removed. |
| 2357 | QualType resultTy = property->getType().getAtomicUnqualifiedType(); |
| 2358 | |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2359 | // If the property is null_resettable, the getter returns nonnull. |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2360 | if (property->getPropertyAttributes() & |
| 2361 | ObjCPropertyDecl::OBJC_PR_null_resettable) { |
| 2362 | QualType modifiedTy = resultTy; |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 2363 | if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) { |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2364 | if (*nullability == NullabilityKind::Unspecified) |
| 2365 | resultTy = Context.getAttributedType(AttributedType::attr_nonnull, |
| 2366 | modifiedTy, modifiedTy); |
| 2367 | } |
| 2368 | } |
| 2369 | |
Ted Kremenek | 2f07563 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 2370 | GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, |
| 2371 | property->getGetterName(), |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2372 | resultTy, nullptr, CD, |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2373 | !IsClassProperty, /*isVariadic=*/false, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2374 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2375 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2376 | (property->getPropertyImplementation() == |
| 2377 | ObjCPropertyDecl::Optional) ? |
| 2378 | ObjCMethodDecl::Optional : |
| 2379 | ObjCMethodDecl::Required); |
| 2380 | CD->addDecl(GetterMethod); |
John McCall | ad31b5f | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 2381 | |
| 2382 | AddPropertyAttrs(*this, GetterMethod, property); |
| 2383 | |
Fariborz Jahanian | f4105f5 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 2384 | if (property->hasAttr<NSReturnsNotRetainedAttr>()) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2385 | GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context, |
| 2386 | Loc)); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 2387 | |
| 2388 | if (property->hasAttr<ObjCReturnsInnerPointerAttr>()) |
| 2389 | GetterMethod->addAttr( |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2390 | ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc)); |
Fariborz Jahanian | cb8c7da | 2013-12-18 23:09:57 +0000 | [diff] [blame] | 2391 | |
| 2392 | if (const SectionAttr *SA = property->getAttr<SectionAttr>()) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2393 | GetterMethod->addAttr( |
| 2394 | SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section, |
| 2395 | SA->getName(), Loc)); |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 2396 | |
| 2397 | if (getLangOpts().ObjCAutoRefCount) |
| 2398 | CheckARCMethodDecl(GetterMethod); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2399 | } else |
| 2400 | // A user declared getter will be synthesize when @synthesize of |
| 2401 | // the property with the same name is seen in the @implementation |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2402 | GetterMethod->setPropertyAccessor(true); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2403 | property->setGetterMethodDecl(GetterMethod); |
| 2404 | |
| 2405 | // Skip setter if property is read-only. |
| 2406 | if (!property->isReadOnly()) { |
| 2407 | // Find the default setter and if one not found, add one. |
| 2408 | if (!SetterMethod) { |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2409 | // No instance/class method of same name as property setter name was |
| 2410 | // found. |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2411 | // Declare a setter method and add it to the list of methods |
| 2412 | // for this class. |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 2413 | SourceLocation Loc = property->getLocation(); |
Ted Kremenek | e3a7d1b | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 2414 | |
| 2415 | SetterMethod = |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2416 | ObjCMethodDecl::Create(Context, Loc, Loc, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2417 | property->getSetterName(), Context.VoidTy, |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2418 | nullptr, CD, !IsClassProperty, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2419 | /*isVariadic=*/false, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2420 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 004df6e | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2421 | /*isImplicitlyDeclared=*/true, |
| 2422 | /*isDefined=*/false, |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2423 | (property->getPropertyImplementation() == |
| 2424 | ObjCPropertyDecl::Optional) ? |
Ted Kremenek | e3a7d1b | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 2425 | ObjCMethodDecl::Optional : |
| 2426 | ObjCMethodDecl::Required); |
| 2427 | |
Akira Hatanaka | de6f25f | 2016-05-26 00:37:30 +0000 | [diff] [blame] | 2428 | // Remove all qualifiers from the setter's parameter type. |
| 2429 | QualType paramTy = |
| 2430 | property->getType().getUnqualifiedType().getAtomicUnqualifiedType(); |
| 2431 | |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2432 | // If the property is null_resettable, the setter accepts a |
| 2433 | // nullable value. |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2434 | if (property->getPropertyAttributes() & |
| 2435 | ObjCPropertyDecl::OBJC_PR_null_resettable) { |
| 2436 | QualType modifiedTy = paramTy; |
| 2437 | if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){ |
| 2438 | if (*nullability == NullabilityKind::Unspecified) |
| 2439 | paramTy = Context.getAttributedType(AttributedType::attr_nullable, |
| 2440 | modifiedTy, modifiedTy); |
| 2441 | } |
| 2442 | } |
| 2443 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2444 | // Invent the arguments for the setter. We don't bother making a |
| 2445 | // nice name for the argument. |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2446 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
| 2447 | Loc, Loc, |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2448 | property->getIdentifier(), |
Douglas Gregor | 849ebc2 | 2015-06-19 18:14:46 +0000 | [diff] [blame] | 2449 | paramTy, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2450 | /*TInfo=*/nullptr, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2451 | SC_None, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2452 | nullptr); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 2453 | SetterMethod->setMethodParams(Context, Argument, None); |
John McCall | ad31b5f | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 2454 | |
| 2455 | AddPropertyAttrs(*this, SetterMethod, property); |
| 2456 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2457 | CD->addDecl(SetterMethod); |
Fariborz Jahanian | cb8c7da | 2013-12-18 23:09:57 +0000 | [diff] [blame] | 2458 | if (const SectionAttr *SA = property->getAttr<SectionAttr>()) |
Warren Hunt | c3b1896 | 2014-04-08 22:30:47 +0000 | [diff] [blame] | 2459 | SetterMethod->addAttr( |
| 2460 | SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section, |
| 2461 | SA->getName(), Loc)); |
John McCall | e48f389 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 2462 | // It's possible for the user to have set a very odd custom |
| 2463 | // setter selector that causes it to have a method family. |
| 2464 | if (getLangOpts().ObjCAutoRefCount) |
| 2465 | CheckARCMethodDecl(SetterMethod); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2466 | } else |
| 2467 | // A user declared setter will be synthesize when @synthesize of |
| 2468 | // the property with the same name is seen in the @implementation |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2469 | SetterMethod->setPropertyAccessor(true); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2470 | property->setSetterMethodDecl(SetterMethod); |
| 2471 | } |
| 2472 | // Add any synthesized methods to the global pool. This allows us to |
| 2473 | // handle the following, which is supported by GCC (and part of the design). |
| 2474 | // |
| 2475 | // @interface Foo |
| 2476 | // @property double bar; |
| 2477 | // @end |
| 2478 | // |
| 2479 | // void thisIsUnfortunate() { |
| 2480 | // id foo; |
| 2481 | // double bar = [foo bar]; |
| 2482 | // } |
| 2483 | // |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2484 | if (!IsClassProperty) { |
| 2485 | if (GetterMethod) |
| 2486 | AddInstanceMethodToGlobalPool(GetterMethod); |
| 2487 | if (SetterMethod) |
| 2488 | AddInstanceMethodToGlobalPool(SetterMethod); |
Manman Ren | 15325f8 | 2016-03-23 21:39:31 +0000 | [diff] [blame] | 2489 | } else { |
| 2490 | if (GetterMethod) |
| 2491 | AddFactoryMethodToGlobalPool(GetterMethod); |
| 2492 | if (SetterMethod) |
| 2493 | AddFactoryMethodToGlobalPool(SetterMethod); |
Manman Ren | d36f7d5 | 2016-01-27 20:10:32 +0000 | [diff] [blame] | 2494 | } |
Argyrios Kyrtzidis | 08f96a9 | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2495 | |
| 2496 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); |
| 2497 | if (!CurrentClass) { |
| 2498 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) |
| 2499 | CurrentClass = Cat->getClassInterface(); |
| 2500 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) |
| 2501 | CurrentClass = Impl->getClassInterface(); |
| 2502 | } |
| 2503 | if (GetterMethod) |
| 2504 | CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); |
| 2505 | if (SetterMethod) |
| 2506 | CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2509 | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2510 | SourceLocation Loc, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2511 | unsigned &Attributes, |
Fariborz Jahanian | 876cc65 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 2512 | bool propertyInPrimaryClass) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2513 | // FIXME: Improve the reported location. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2514 | if (!PDecl || PDecl->isInvalidDecl()) |
Ted Kremenek | b535782 | 2010-04-05 22:39:42 +0000 | [diff] [blame] | 2515 | return; |
Fariborz Jahanian | 39ba639 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 2516 | |
Fariborz Jahanian | 88ff20e | 2013-10-07 17:20:02 +0000 | [diff] [blame] | 2517 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2518 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
| 2519 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2520 | << "readonly" << "readwrite"; |
| 2521 | |
| 2522 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); |
| 2523 | QualType PropertyTy = PropertyDecl->getType(); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Check for copy or retain on non-object types. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2526 | if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2527 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && |
| 2528 | !PropertyTy->isObjCRetainableType() && |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 2529 | !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2530 | Diag(Loc, diag::err_objc_property_requires_object) |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2531 | << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : |
| 2532 | Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); |
| 2533 | Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2534 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); |
John McCall | 2499237 | 2012-02-21 21:48:05 +0000 | [diff] [blame] | 2535 | PropertyDecl->setInvalidDecl(); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | // Check for more than one of { assign, copy, retain }. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2539 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 2540 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2541 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2542 | << "assign" << "copy"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2543 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2544 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2545 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2546 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2547 | << "assign" << "retain"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2548 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2549 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2550 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2551 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2552 | << "assign" << "strong"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2553 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2554 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2555 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2556 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2557 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2558 | << "assign" << "weak"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2559 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2560 | } |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 2561 | if (PropertyDecl->hasAttr<IBOutletCollectionAttr>()) |
Fariborz Jahanian | f030d16 | 2013-06-25 17:34:50 +0000 | [diff] [blame] | 2562 | Diag(Loc, diag::warn_iboutletcollection_property_assign); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2563 | } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { |
| 2564 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2565 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2566 | << "unsafe_unretained" << "copy"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2567 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2568 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2569 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2570 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2571 | << "unsafe_unretained" << "retain"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2572 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2573 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2574 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2575 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2576 | << "unsafe_unretained" << "strong"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2577 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2578 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2579 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2580 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2581 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2582 | << "unsafe_unretained" << "weak"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2583 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2584 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2585 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 2586 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2587 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2588 | << "copy" << "retain"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2589 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2590 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2591 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2592 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2593 | << "copy" << "strong"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2594 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2595 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2596 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2597 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2598 | << "copy" << "weak"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2599 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2602 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2603 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2604 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2605 | << "retain" << "weak"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2606 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2607 | } |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2608 | else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 2609 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2610 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2611 | << "strong" << "weak"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2612 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 2615 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) { |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2616 | // 'weak' and 'nonnull' are mutually exclusive. |
| 2617 | if (auto nullability = PropertyTy->getNullability(Context)) { |
| 2618 | if (*nullability == NullabilityKind::NonNull) |
| 2619 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2620 | << "nonnull" << "weak"; |
Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 2621 | } |
| 2622 | } |
| 2623 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2624 | if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && |
| 2625 | (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { |
Fariborz Jahanian | 55b4e5c | 2011-10-10 21:53:24 +0000 | [diff] [blame] | 2626 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2627 | << "atomic" << "nonatomic"; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2628 | Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; |
Fariborz Jahanian | 55b4e5c | 2011-10-10 21:53:24 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2631 | // Warn if user supplied no assignment attribute, property is |
| 2632 | // readwrite, and this is an object type. |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 2633 | if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) { |
| 2634 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) { |
| 2635 | // do nothing |
| 2636 | } else if (getLangOpts().ObjCAutoRefCount) { |
| 2637 | // With arc, @property definitions should default to strong when |
| 2638 | // not specified. |
| 2639 | PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 2640 | } else if (PropertyTy->isObjCObjectPointerType()) { |
Fariborz Jahanian | 1fc1c6c | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2641 | bool isAnyClassTy = |
| 2642 | (PropertyTy->isObjCClassType() || |
| 2643 | PropertyTy->isObjCQualifiedClassType()); |
| 2644 | // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to |
| 2645 | // issue any warning. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2646 | if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | 1fc1c6c | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2647 | ; |
Fariborz Jahanian | cfb00a4 | 2012-09-17 23:57:35 +0000 | [diff] [blame] | 2648 | else if (propertyInPrimaryClass) { |
| 2649 | // Don't issue warning on property with no life time in class |
| 2650 | // extension as it is inherited from property in primary class. |
Fariborz Jahanian | 7e47de3 | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2651 | // Skip this warning in gc-only mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2652 | if (getLangOpts().getGC() != LangOptions::GCOnly) |
Fariborz Jahanian | 7e47de3 | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2653 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2654 | |
Fariborz Jahanian | 7e47de3 | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2655 | // If non-gc code warn that this is likely inappropriate. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2656 | if (getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | 7e47de3 | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2657 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
Fariborz Jahanian | 1fc1c6c | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2658 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 2659 | } |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2660 | |
| 2661 | // FIXME: Implement warning dependent on NSCopying being |
| 2662 | // implemented. See also: |
| 2663 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 2664 | // (please trim this list while you are at it). |
| 2665 | } |
| 2666 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2667 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 2668 | &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2669 | && getLangOpts().getGC() == LangOptions::GCOnly |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2670 | && PropertyTy->isBlockPointerType()) |
| 2671 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2672 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2673 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2674 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
Fariborz Jahanian | 1723e17 | 2011-09-14 18:03:46 +0000 | [diff] [blame] | 2675 | PropertyTy->isBlockPointerType()) |
| 2676 | Diag(Loc, diag::warn_objc_property_retain_of_block); |
Fariborz Jahanian | 3018b95 | 2011-11-01 23:02:16 +0000 | [diff] [blame] | 2677 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2678 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2679 | (Attributes & ObjCDeclSpec::DQ_PR_setter)) |
Fariborz Jahanian | 3018b95 | 2011-11-01 23:02:16 +0000 | [diff] [blame] | 2680 | Diag(Loc, diag::warn_objc_readonly_property_has_setter); |
Ted Kremenek | 7a7a080 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2681 | } |