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