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