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