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