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