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