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