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