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