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 | 8dbda51 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 717 | /// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared |
| 718 | /// in inherited protocols with mismatched types. Since any of them can |
| 719 | /// be candidate for synthesis. |
Benjamin Kramer | b1a4d37 | 2013-05-23 15:53:44 +0000 | [diff] [blame] | 720 | static void |
| 721 | DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc, |
| 722 | ObjCInterfaceDecl *ClassDecl, |
Fariborz Jahanian | 8dbda51 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 723 | ObjCPropertyDecl *Property) { |
| 724 | ObjCInterfaceDecl::ProtocolPropertyMap PropMap; |
| 725 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 726 | PI = ClassDecl->all_referenced_protocol_begin(), |
| 727 | E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) { |
| 728 | if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition()) |
| 729 | PDecl->collectInheritedProtocolProperties(Property, PropMap); |
| 730 | } |
| 731 | if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) |
| 732 | while (SDecl) { |
| 733 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 734 | PI = SDecl->all_referenced_protocol_begin(), |
| 735 | E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) { |
| 736 | if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition()) |
| 737 | PDecl->collectInheritedProtocolProperties(Property, PropMap); |
| 738 | } |
| 739 | SDecl = SDecl->getSuperClass(); |
| 740 | } |
| 741 | |
| 742 | if (PropMap.empty()) |
| 743 | return; |
| 744 | |
| 745 | QualType RHSType = S.Context.getCanonicalType(Property->getType()); |
| 746 | bool FirsTime = true; |
| 747 | for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator |
| 748 | I = PropMap.begin(), E = PropMap.end(); I != E; I++) { |
| 749 | ObjCPropertyDecl *Prop = I->second; |
| 750 | QualType LHSType = S.Context.getCanonicalType(Prop->getType()); |
| 751 | if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
| 752 | bool IncompatibleObjC = false; |
| 753 | QualType ConvertedType; |
| 754 | if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC) |
| 755 | || IncompatibleObjC) { |
| 756 | if (FirsTime) { |
| 757 | S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch) |
| 758 | << Property->getType(); |
| 759 | FirsTime = false; |
| 760 | } |
| 761 | S.Diag(Prop->getLocation(), diag::note_protocol_property_declare) |
| 762 | << Prop->getType(); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | if (!FirsTime && AtLoc.isValid()) |
| 767 | S.Diag(AtLoc, diag::note_property_synthesize); |
| 768 | } |
| 769 | |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 770 | /// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property |
| 771 | /// attribute declared in primary class and attributes overridden in any of its |
| 772 | /// class extensions. |
| 773 | static void |
| 774 | DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl, |
| 775 | ObjCPropertyDecl *property) { |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 776 | unsigned Attributes = property->getPropertyAttributesAsWritten(); |
| 777 | bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly); |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 778 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 779 | Ext = ClassDecl->known_extensions_begin(), |
| 780 | ExtEnd = ClassDecl->known_extensions_end(); |
| 781 | Ext != ExtEnd; ++Ext) { |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 782 | ObjCPropertyDecl *ClassExtProperty = 0; |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 783 | DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); |
| 784 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 785 | ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]); |
| 786 | if (ClassExtProperty) |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 787 | break; |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 788 | } |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 789 | |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 790 | if (ClassExtProperty) { |
Fariborz Jahanian | c78ff27 | 2012-06-20 23:18:57 +0000 | [diff] [blame] | 791 | warn = false; |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 792 | unsigned classExtPropertyAttr = |
| 793 | ClassExtProperty->getPropertyAttributesAsWritten(); |
| 794 | // We are issuing the warning that we postponed because class extensions |
| 795 | // can override readonly->readwrite and 'setter' attributes originally |
| 796 | // placed on class's property declaration now make sense in the overridden |
| 797 | // property. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 798 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) { |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 799 | if (!classExtPropertyAttr || |
Fariborz Jahanian | a6e28f2 | 2012-08-24 20:10:53 +0000 | [diff] [blame] | 800 | (classExtPropertyAttr & |
| 801 | (ObjCDeclSpec::DQ_PR_readwrite| |
| 802 | ObjCDeclSpec::DQ_PR_assign | |
| 803 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 804 | ObjCDeclSpec::DQ_PR_copy | |
| 805 | ObjCDeclSpec::DQ_PR_retain | |
| 806 | ObjCDeclSpec::DQ_PR_strong))) |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 807 | continue; |
| 808 | warn = true; |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | if (warn) { |
| 814 | unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign | |
| 815 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 816 | ObjCDeclSpec::DQ_PR_copy | |
| 817 | ObjCDeclSpec::DQ_PR_retain | |
| 818 | ObjCDeclSpec::DQ_PR_strong); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 819 | if (Attributes & setterAttrs) { |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 820 | const char * which = |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 821 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 822 | "assign" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 823 | (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 824 | "unsafe_unretained" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 825 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 826 | "copy" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 827 | (Attributes & ObjCDeclSpec::DQ_PR_retain) ? |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 828 | "retain" : "strong"; |
| 829 | |
| 830 | S.Diag(property->getLocation(), |
| 831 | diag::warn_objc_property_attr_mutually_exclusive) |
| 832 | << "readonly" << which; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | |
| 837 | } |
| 838 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 839 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 840 | /// builds the AST node for a property implementation declaration; declared |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 841 | /// as \@synthesize or \@dynamic. |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 842 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 843 | Decl *Sema::ActOnPropertyImplDecl(Scope *S, |
| 844 | SourceLocation AtLoc, |
| 845 | SourceLocation PropertyLoc, |
| 846 | bool Synthesize, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 847 | IdentifierInfo *PropertyId, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 848 | IdentifierInfo *PropertyIvar, |
| 849 | SourceLocation PropertyIvarLoc) { |
Ted Kremenek | e968657 | 2010-04-05 23:45:09 +0000 | [diff] [blame] | 850 | ObjCContainerDecl *ClassImpDecl = |
Fariborz Jahanian | 84e0ccf | 2011-09-19 16:32:32 +0000 | [diff] [blame] | 851 | dyn_cast<ObjCContainerDecl>(CurContext); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 852 | // Make sure we have a context for the property implementation declaration. |
| 853 | if (!ClassImpDecl) { |
| 854 | Diag(AtLoc, diag::error_missing_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 855 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 856 | } |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 857 | if (PropertyIvarLoc.isInvalid()) |
| 858 | PropertyIvarLoc = PropertyLoc; |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 859 | SourceLocation PropertyDiagLoc = PropertyLoc; |
| 860 | if (PropertyDiagLoc.isInvalid()) |
| 861 | PropertyDiagLoc = ClassImpDecl->getLocStart(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 862 | ObjCPropertyDecl *property = 0; |
| 863 | ObjCInterfaceDecl* IDecl = 0; |
| 864 | // Find the class or category class where this property must have |
| 865 | // a declaration. |
| 866 | ObjCImplementationDecl *IC = 0; |
| 867 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 868 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
| 869 | IDecl = IC->getClassInterface(); |
| 870 | // We always synthesize an interface for an implementation |
| 871 | // without an interface decl. So, IDecl is always non-zero. |
| 872 | assert(IDecl && |
| 873 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 874 | |
| 875 | // Look for this property declaration in the @implementation's @interface |
| 876 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 877 | if (!property) { |
| 878 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 879 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 880 | } |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 881 | unsigned PIkind = property->getPropertyAttributesAsWritten(); |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 882 | if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | |
| 883 | ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 884 | if (AtLoc.isValid()) |
| 885 | Diag(AtLoc, diag::warn_implicit_atomic_property); |
| 886 | else |
| 887 | Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); |
| 888 | Diag(property->getLocation(), diag::note_property_declare); |
| 889 | } |
| 890 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 891 | if (const ObjCCategoryDecl *CD = |
| 892 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
| 893 | if (!CD->IsClassExtension()) { |
| 894 | Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); |
| 895 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 896 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 899 | if (Synthesize&& |
| 900 | (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) && |
| 901 | property->hasAttr<IBOutletAttr>() && |
| 902 | !AtLoc.isValid()) { |
Fariborz Jahanian | 1256434 | 2013-02-08 23:32:30 +0000 | [diff] [blame] | 903 | bool ReadWriteProperty = false; |
| 904 | // Search into the class extensions and see if 'readonly property is |
| 905 | // redeclared 'readwrite', then no warning is to be issued. |
| 906 | for (ObjCInterfaceDecl::known_extensions_iterator |
| 907 | Ext = IDecl->known_extensions_begin(), |
| 908 | ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) { |
| 909 | DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); |
| 910 | if (!R.empty()) |
| 911 | if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) { |
| 912 | PIkind = ExtProp->getPropertyAttributesAsWritten(); |
| 913 | if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 914 | ReadWriteProperty = true; |
| 915 | break; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | if (!ReadWriteProperty) { |
Ted Kremenek | a4475a6 | 2013-02-09 07:13:16 +0000 | [diff] [blame] | 921 | Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property) |
| 922 | << property->getName(); |
Fariborz Jahanian | 1256434 | 2013-02-08 23:32:30 +0000 | [diff] [blame] | 923 | SourceLocation readonlyLoc; |
| 924 | if (LocPropertyAttribute(Context, "readonly", |
| 925 | property->getLParenLoc(), readonlyLoc)) { |
| 926 | SourceLocation endLoc = |
| 927 | readonlyLoc.getLocWithOffset(strlen("readonly")-1); |
| 928 | SourceRange ReadonlySourceRange(readonlyLoc, endLoc); |
| 929 | Diag(property->getLocation(), |
| 930 | diag::note_auto_readonly_iboutlet_fixup_suggest) << |
| 931 | FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite"); |
| 932 | } |
Fariborz Jahanian | edcc27f | 2012-05-21 17:02:43 +0000 | [diff] [blame] | 933 | } |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 934 | } |
Fariborz Jahanian | 8dbda51 | 2013-05-20 21:20:24 +0000 | [diff] [blame] | 935 | if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext())) |
| 936 | DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property); |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 937 | |
| 938 | DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property); |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 940 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
| 941 | if (Synthesize) { |
| 942 | Diag(AtLoc, diag::error_synthesize_category_decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 943 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 944 | } |
| 945 | IDecl = CatImplClass->getClassInterface(); |
| 946 | if (!IDecl) { |
| 947 | Diag(AtLoc, diag::error_missing_property_interface); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 948 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 949 | } |
| 950 | ObjCCategoryDecl *Category = |
| 951 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 952 | |
| 953 | // If category for this implementation not found, it is an error which |
| 954 | // has already been reported eralier. |
| 955 | if (!Category) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 956 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 957 | // Look for this property declaration in @implementation's category |
| 958 | property = Category->FindPropertyDeclaration(PropertyId); |
| 959 | if (!property) { |
| 960 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
| 961 | << Category->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 962 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 963 | } |
| 964 | } else { |
| 965 | Diag(AtLoc, diag::error_bad_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 966 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 967 | } |
| 968 | ObjCIvarDecl *Ivar = 0; |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 969 | bool CompleteTypeErr = false; |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 970 | bool compat = true; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 971 | // Check that we have a valid, previously declared ivar for @synthesize |
| 972 | if (Synthesize) { |
| 973 | // @synthesize |
| 974 | if (!PropertyIvar) |
| 975 | PropertyIvar = PropertyId; |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 976 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
| 977 | ObjCInterfaceDecl *ClassDeclared; |
| 978 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
| 979 | QualType PropType = property->getType(); |
| 980 | QualType PropertyIvarType = PropType.getNonReferenceType(); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 981 | |
| 982 | if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 983 | diag::err_incomplete_synthesized_property, |
| 984 | property->getDeclName())) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 985 | Diag(property->getLocation(), diag::note_property_declare); |
| 986 | CompleteTypeErr = true; |
| 987 | } |
| 988 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 989 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 990 | (property->getPropertyAttributesAsWritten() & |
Fariborz Jahanian | 3efd348 | 2012-01-11 19:48:08 +0000 | [diff] [blame] | 991 | ObjCPropertyDecl::OBJC_PR_readonly) && |
| 992 | PropertyIvarType->isObjCRetainableType()) { |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 993 | setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar); |
| 994 | } |
| 995 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 996 | ObjCPropertyDecl::PropertyAttributeKind kind |
| 997 | = property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 998 | |
| 999 | // Add GC __weak to the ivar type if the property is weak. |
| 1000 | if ((kind & ObjCPropertyDecl::OBJC_PR_weak) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1001 | getLangOpts().getGC() != LangOptions::NonGC) { |
| 1002 | assert(!getLangOpts().ObjCAutoRefCount); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1003 | if (PropertyIvarType.isObjCGCStrong()) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1004 | Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1005 | Diag(property->getLocation(), diag::note_property_declare); |
| 1006 | } else { |
| 1007 | PropertyIvarType = |
| 1008 | Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 1009 | } |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 1010 | } |
Fariborz Jahanian | e95f8ef | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 1011 | if (AtLoc.isInvalid()) { |
| 1012 | // Check when default synthesizing a property that there is |
| 1013 | // an ivar matching property name and issue warning; since this |
| 1014 | // is the most common case of not using an ivar used for backing |
| 1015 | // property in non-default synthesis case. |
| 1016 | ObjCInterfaceDecl *ClassDeclared=0; |
| 1017 | ObjCIvarDecl *originalIvar = |
| 1018 | IDecl->lookupInstanceVariable(property->getIdentifier(), |
| 1019 | ClassDeclared); |
| 1020 | if (originalIvar) { |
| 1021 | Diag(PropertyDiagLoc, |
| 1022 | diag::warn_autosynthesis_property_ivar_match) |
Fariborz Jahanian | 2578532 | 2012-06-29 19:05:11 +0000 | [diff] [blame] | 1023 | << PropertyId << (Ivar == 0) << PropertyIvar |
Fariborz Jahanian | 20e7d99 | 2012-06-29 18:43:30 +0000 | [diff] [blame] | 1024 | << originalIvar->getIdentifier(); |
Fariborz Jahanian | e95f8ef | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 1025 | Diag(property->getLocation(), diag::note_property_declare); |
| 1026 | Diag(originalIvar->getLocation(), diag::note_ivar_decl); |
Fariborz Jahanian | dd3284b | 2012-06-19 22:51:22 +0000 | [diff] [blame] | 1027 | } |
Fariborz Jahanian | e95f8ef | 2012-06-20 17:18:31 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (!Ivar) { |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1031 | // In ARC, give the ivar a lifetime qualifier based on the |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1032 | // property attributes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1033 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1034 | !PropertyIvarType.getObjCLifetime() && |
| 1035 | PropertyIvarType->isObjCRetainableType()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1036 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1037 | // It's an error if we have to do this and the user didn't |
| 1038 | // explicitly write an ownership attribute on the property. |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 1039 | if (!property->hasWrittenStorageAttribute() && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1040 | !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1041 | Diag(PropertyDiagLoc, |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 1042 | diag::err_arc_objc_property_default_assign_on_object); |
| 1043 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1044 | } else { |
| 1045 | Qualifiers::ObjCLifetime lifetime = |
| 1046 | getImpliedARCOwnership(kind, PropertyIvarType); |
| 1047 | assert(lifetime && "no lifetime for property?"); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 1048 | if (lifetime == Qualifiers::OCL_Weak) { |
| 1049 | bool err = false; |
| 1050 | if (const ObjCObjectPointerType *ObjT = |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 1051 | PropertyIvarType->getAs<ObjCObjectPointerType>()) { |
| 1052 | const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl(); |
| 1053 | if (ObjI && ObjI->isArcWeakrefUnavailable()) { |
Fariborz Jahanian | 80abce3 | 2013-04-24 19:13:05 +0000 | [diff] [blame] | 1054 | Diag(property->getLocation(), |
| 1055 | diag::err_arc_weak_unavailable_property) << PropertyIvarType; |
| 1056 | Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class) |
| 1057 | << ClassImpDecl->getName(); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 1058 | err = true; |
| 1059 | } |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 1060 | } |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 1061 | if (!err && !getLangOpts().ObjCARCWeak) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1062 | Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 1063 | Diag(property->getLocation(), diag::note_property_declare); |
| 1064 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1065 | } |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 1066 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1067 | Qualifiers qs; |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1068 | qs.addObjCLifetime(lifetime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1069 | PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); |
| 1070 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (kind & ObjCPropertyDecl::OBJC_PR_weak && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1074 | !getLangOpts().ObjCAutoRefCount && |
| 1075 | getLangOpts().getGC() == LangOptions::NonGC) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1076 | Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1077 | Diag(property->getLocation(), diag::note_property_declare); |
| 1078 | } |
| 1079 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1080 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 1081 | PropertyIvarLoc,PropertyIvarLoc, PropertyIvar, |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 1082 | PropertyIvarType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1083 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1084 | (Expr *)0, true); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1085 | if (CompleteTypeErr) |
| 1086 | Ivar->setInvalidDecl(); |
Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 1087 | ClassImpDecl->addDecl(Ivar); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1088 | IDecl->makeDeclVisibleInContext(Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1089 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1090 | if (getLangOpts().ObjCRuntime.isFragile()) |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1091 | Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl) |
| 1092 | << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1093 | // Note! I deliberately want it to fall thru so, we have a |
| 1094 | // a property implementation and to avoid future warnings. |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1095 | } else if (getLangOpts().ObjCRuntime.isNonFragile() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1096 | !declaresSameEntity(ClassDeclared, IDecl)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1097 | Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1098 | << property->getDeclName() << Ivar->getDeclName() |
| 1099 | << ClassDeclared->getDeclName(); |
| 1100 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 1101 | << Ivar << Ivar->getName(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1102 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 1103 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 1104 | property->setPropertyIvarDecl(Ivar); |
| 1105 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1106 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 1107 | |
| 1108 | // Check that type of property and its ivar are type compatible. |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1109 | if (!Context.hasSameType(PropertyIvarType, IvarType)) { |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 1110 | if (isa<ObjCObjectPointerType>(PropertyIvarType) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1111 | && isa<ObjCObjectPointerType>(IvarType)) |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 1112 | compat = |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1113 | Context.canAssignObjCInterfaces( |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 1114 | PropertyIvarType->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1115 | IvarType->getAs<ObjCObjectPointerType>()); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1116 | else { |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 1117 | compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType, |
| 1118 | IvarType) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1119 | == Compatible); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1120 | } |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 1121 | if (!compat) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1122 | Diag(PropertyDiagLoc, diag::error_property_ivar_type) |
Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 1123 | << property->getDeclName() << PropType |
| 1124 | << Ivar->getDeclName() << IvarType; |
| 1125 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1126 | // Note! I deliberately want it to fall thru so, we have a |
| 1127 | // a property implementation and to avoid future warnings. |
| 1128 | } |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1129 | else { |
| 1130 | // FIXME! Rules for properties are somewhat different that those |
| 1131 | // for assignments. Use a new routine to consolidate all cases; |
| 1132 | // specifically for property redeclarations as well as for ivars. |
| 1133 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
| 1134 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 1135 | if (lhsType != rhsType && |
| 1136 | lhsType->isArithmeticType()) { |
| 1137 | Diag(PropertyDiagLoc, diag::error_property_ivar_type) |
| 1138 | << property->getDeclName() << PropType |
| 1139 | << Ivar->getDeclName() << IvarType; |
| 1140 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
| 1141 | // Fall thru - see previous comment |
| 1142 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1143 | } |
| 1144 | // __weak is explicit. So it works on Canonical type. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1145 | if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1146 | getLangOpts().getGC() != LangOptions::NonGC)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1147 | Diag(PropertyDiagLoc, diag::error_weak_property) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1148 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 1149 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1150 | // Fall thru - see previous comment |
| 1151 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1152 | // Fall thru - see previous comment |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1153 | if ((property->getType()->isObjCObjectPointerType() || |
| 1154 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1155 | getLangOpts().getGC() != LangOptions::NonGC) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1156 | Diag(PropertyDiagLoc, diag::error_strong_property) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1157 | << property->getDeclName() << Ivar->getDeclName(); |
| 1158 | // Fall thru - see previous comment |
| 1159 | } |
| 1160 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1161 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1162 | checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1163 | } else if (PropertyIvar) |
| 1164 | // @dynamic |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1165 | Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1166 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1167 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
| 1168 | ObjCPropertyImplDecl *PIDecl = |
| 1169 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 1170 | property, |
| 1171 | (Synthesize ? |
| 1172 | ObjCPropertyImplDecl::Synthesize |
| 1173 | : ObjCPropertyImplDecl::Dynamic), |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1174 | Ivar, PropertyIvarLoc); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1175 | |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 1176 | if (CompleteTypeErr || !compat) |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1177 | PIDecl->setInvalidDecl(); |
| 1178 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1179 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
| 1180 | getterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1181 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 1182 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1183 | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
| 1184 | // returned by the getter as it must conform to C++'s copy-return rules. |
| 1185 | // FIXME. Eventually we want to do this for Objective-C as well. |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1186 | SynthesizedFunctionScope Scope(*this, getterMethod); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1187 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
| 1188 | DeclRefExpr *SelfExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1189 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1190 | VK_RValue, PropertyDiagLoc); |
| 1191 | MarkDeclRefReferenced(SelfExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1192 | Expr *IvarRefExpr = |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1193 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc, |
Fariborz Jahanian | 0c70181 | 2013-04-02 18:57:54 +0000 | [diff] [blame] | 1194 | Ivar->getLocation(), |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1195 | SelfExpr, true, true); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1196 | ExprResult Res = |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1197 | PerformCopyInitialization(InitializedEntity::InitializeResult( |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1198 | PropertyDiagLoc, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 1199 | getterMethod->getResultType(), |
| 1200 | /*NRVO=*/false), |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1201 | PropertyDiagLoc, |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1202 | Owned(IvarRefExpr)); |
| 1203 | if (!Res.isInvalid()) { |
| 1204 | Expr *ResExpr = Res.takeAs<Expr>(); |
| 1205 | if (ResExpr) |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1206 | ResExpr = MaybeCreateExprWithCleanups(ResExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1207 | PIDecl->setGetterCXXConstructor(ResExpr); |
| 1208 | } |
| 1209 | } |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1210 | if (property->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1211 | !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 1212 | Diag(getterMethod->getLocation(), |
| 1213 | diag::warn_property_getter_owning_mismatch); |
| 1214 | Diag(property->getLocation(), diag::note_property_declare); |
| 1215 | } |
Fariborz Jahanian | b8ed071 | 2013-05-16 19:08:44 +0000 | [diff] [blame] | 1216 | if (getLangOpts().ObjCAutoRefCount && Synthesize) |
| 1217 | switch (getterMethod->getMethodFamily()) { |
| 1218 | case OMF_retain: |
| 1219 | case OMF_retainCount: |
| 1220 | case OMF_release: |
| 1221 | case OMF_autorelease: |
| 1222 | Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def) |
| 1223 | << 1 << getterMethod->getSelector(); |
| 1224 | break; |
| 1225 | default: |
| 1226 | break; |
| 1227 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1228 | } |
| 1229 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
| 1230 | setterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1231 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
| 1232 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1233 | // FIXME. Eventually we want to do this for Objective-C as well. |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1234 | SynthesizedFunctionScope Scope(*this, setterMethod); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1235 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
| 1236 | DeclRefExpr *SelfExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1237 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1238 | VK_RValue, PropertyDiagLoc); |
| 1239 | MarkDeclRefReferenced(SelfExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1240 | Expr *lhs = |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1241 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc, |
Fariborz Jahanian | 0c70181 | 2013-04-02 18:57:54 +0000 | [diff] [blame] | 1242 | Ivar->getLocation(), |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1243 | SelfExpr, true, true); |
| 1244 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
| 1245 | ParmVarDecl *Param = (*P); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1246 | QualType T = Param->getType().getNonReferenceType(); |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1247 | DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T, |
| 1248 | VK_LValue, PropertyDiagLoc); |
| 1249 | MarkDeclRefReferenced(rhs); |
| 1250 | ExprResult Res = BuildBinOp(S, PropertyDiagLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1251 | BO_Assign, lhs, rhs); |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1252 | if (property->getPropertyAttributes() & |
| 1253 | ObjCPropertyDecl::OBJC_PR_atomic) { |
| 1254 | Expr *callExpr = Res.takeAs<Expr>(); |
| 1255 | if (const CXXOperatorCallExpr *CXXCE = |
Fariborz Jahanian | 13bf633 | 2011-10-07 21:08:14 +0000 | [diff] [blame] | 1256 | dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) |
| 1257 | if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1258 | if (!FuncDecl->isTrivial()) |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1259 | if (property->getType()->isReferenceType()) { |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 1260 | Diag(PropertyDiagLoc, |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1261 | diag::err_atomic_property_nontrivial_assign_op) |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1262 | << property->getType(); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 1263 | Diag(FuncDecl->getLocStart(), |
| 1264 | diag::note_callee_decl) << FuncDecl; |
| 1265 | } |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 1266 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1267 | PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); |
| 1268 | } |
| 1269 | } |
| 1270 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1271 | if (IC) { |
| 1272 | if (Synthesize) |
| 1273 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1274 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 1275 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 1276 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1277 | << PropertyIvar; |
| 1278 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1279 | } |
| 1280 | |
| 1281 | if (ObjCPropertyImplDecl *PPIDecl |
| 1282 | = IC->FindPropertyImplDecl(PropertyId)) { |
| 1283 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 1284 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1285 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1286 | } |
| 1287 | IC->addPropertyImplementation(PIDecl); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1288 | if (getLangOpts().ObjCDefaultSynthProperties && |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1289 | getLangOpts().ObjCRuntime.isNonFragile() && |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1290 | !IDecl->isObjCRequiresPropertyDefs()) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1291 | // Diagnose if an ivar was lazily synthesdized due to a previous |
| 1292 | // use and if 1) property is @dynamic or 2) property is synthesized |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1293 | // but it requires an ivar of different name. |
Fariborz Jahanian | 411c25c | 2011-01-20 23:34:25 +0000 | [diff] [blame] | 1294 | ObjCInterfaceDecl *ClassDeclared=0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1295 | ObjCIvarDecl *Ivar = 0; |
| 1296 | if (!Synthesize) |
| 1297 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1298 | else { |
| 1299 | if (PropertyIvar && PropertyIvar != PropertyId) |
| 1300 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1301 | } |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1302 | // Issue diagnostics only if Ivar belongs to current class. |
| 1303 | if (Ivar && Ivar->getSynthesize() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1304 | declaresSameEntity(IC->getClassInterface(), ClassDeclared)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1305 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) |
| 1306 | << PropertyId; |
| 1307 | Ivar->setInvalidDecl(); |
| 1308 | } |
| 1309 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1310 | } else { |
| 1311 | if (Synthesize) |
| 1312 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1313 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1314 | Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1315 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1316 | << PropertyIvar; |
| 1317 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1318 | } |
| 1319 | |
| 1320 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1321 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1322 | Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1323 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1324 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1325 | } |
| 1326 | CatImplClass->addPropertyImplementation(PIDecl); |
| 1327 | } |
| 1328 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1329 | return PIDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | //===----------------------------------------------------------------------===// |
| 1333 | // Helper methods. |
| 1334 | //===----------------------------------------------------------------------===// |
| 1335 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1336 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 1337 | /// attributes and types and warns on a variety of inconsistencies. |
| 1338 | /// |
| 1339 | void |
| 1340 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 1341 | ObjCPropertyDecl *SuperProperty, |
| 1342 | const IdentifierInfo *inheritedName) { |
| 1343 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1344 | Property->getPropertyAttributes(); |
| 1345 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 1346 | SuperProperty->getPropertyAttributes(); |
| 1347 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1348 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1349 | Diag(Property->getLocation(), diag::warn_readonly_property) |
| 1350 | << Property->getDeclName() << inheritedName; |
| 1351 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 1352 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
| 1353 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1354 | << Property->getDeclName() << "copy" << inheritedName; |
Fariborz Jahanian | 1b46d8d | 2011-10-08 17:45:33 +0000 | [diff] [blame] | 1355 | else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){ |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1356 | unsigned CAttrRetain = |
| 1357 | (CAttr & |
| 1358 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1359 | unsigned SAttrRetain = |
| 1360 | (SAttr & |
| 1361 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1362 | bool CStrong = (CAttrRetain != 0); |
| 1363 | bool SStrong = (SAttrRetain != 0); |
| 1364 | if (CStrong != SStrong) |
| 1365 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1366 | << Property->getDeclName() << "retain (or strong)" << inheritedName; |
| 1367 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1368 | |
| 1369 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
Fariborz Jahanian | b7b2565 | 2013-02-10 00:16:04 +0000 | [diff] [blame] | 1370 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1371 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1372 | << Property->getDeclName() << "atomic" << inheritedName; |
Fariborz Jahanian | b7b2565 | 2013-02-10 00:16:04 +0000 | [diff] [blame] | 1373 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1374 | } |
| 1375 | if (Property->getSetterName() != SuperProperty->getSetterName()) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1376 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1377 | << Property->getDeclName() << "setter" << inheritedName; |
Fariborz Jahanian | b7b2565 | 2013-02-10 00:16:04 +0000 | [diff] [blame] | 1378 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1379 | } |
| 1380 | if (Property->getGetterName() != SuperProperty->getGetterName()) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1381 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1382 | << Property->getDeclName() << "getter" << inheritedName; |
Fariborz Jahanian | b7b2565 | 2013-02-10 00:16:04 +0000 | [diff] [blame] | 1383 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1384 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1385 | |
| 1386 | QualType LHSType = |
| 1387 | Context.getCanonicalType(SuperProperty->getType()); |
| 1388 | QualType RHSType = |
| 1389 | Context.getCanonicalType(Property->getType()); |
| 1390 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 1391 | if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
Fariborz Jahanian | 8beb6a2 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1392 | // Do cases not handled in above. |
| 1393 | // FIXME. For future support of covariant property types, revisit this. |
| 1394 | bool IncompatibleObjC = false; |
| 1395 | QualType ConvertedType; |
| 1396 | if (!isObjCPointerConversion(RHSType, LHSType, |
| 1397 | ConvertedType, IncompatibleObjC) || |
Fariborz Jahanian | 13546a8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1398 | IncompatibleObjC) { |
Fariborz Jahanian | 8beb6a2 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1399 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 1400 | << Property->getType() << SuperProperty->getType() << inheritedName; |
Fariborz Jahanian | 13546a8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1401 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1402 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 1407 | ObjCMethodDecl *GetterMethod, |
| 1408 | SourceLocation Loc) { |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1409 | if (!GetterMethod) |
| 1410 | return false; |
| 1411 | QualType GetterType = GetterMethod->getResultType().getNonReferenceType(); |
| 1412 | QualType PropertyIvarType = property->getType().getNonReferenceType(); |
| 1413 | bool compat = Context.hasSameType(PropertyIvarType, GetterType); |
| 1414 | if (!compat) { |
| 1415 | if (isa<ObjCObjectPointerType>(PropertyIvarType) && |
| 1416 | isa<ObjCObjectPointerType>(GetterType)) |
| 1417 | compat = |
| 1418 | Context.canAssignObjCInterfaces( |
Fariborz Jahanian | 490a52b | 2012-05-29 19:56:01 +0000 | [diff] [blame] | 1419 | GetterType->getAs<ObjCObjectPointerType>(), |
| 1420 | PropertyIvarType->getAs<ObjCObjectPointerType>()); |
| 1421 | else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType) |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1422 | != Compatible) { |
| 1423 | Diag(Loc, diag::error_property_accessor_type) |
| 1424 | << property->getDeclName() << PropertyIvarType |
| 1425 | << GetterMethod->getSelector() << GetterType; |
| 1426 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1427 | return true; |
| 1428 | } else { |
| 1429 | compat = true; |
| 1430 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
| 1431 | QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType(); |
| 1432 | if (lhsType != rhsType && lhsType->isArithmeticType()) |
| 1433 | compat = false; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1436 | |
| 1437 | if (!compat) { |
| 1438 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
| 1439 | << property->getDeclName() |
| 1440 | << GetterMethod->getSelector(); |
| 1441 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1442 | return true; |
| 1443 | } |
| 1444 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1445 | return false; |
| 1446 | } |
| 1447 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1448 | /// MatchOneProtocolPropertiesInClass - This routine goes thru the list |
| 1449 | /// of properties declared in a protocol and compares their attribute against |
| 1450 | /// the same property declared in the class or category. |
| 1451 | void |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1452 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) { |
| 1453 | if (!CDecl) |
| 1454 | return; |
| 1455 | |
| 1456 | // Category case. |
| 1457 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1458 | // FIXME: We should perform this check when the property in the category |
| 1459 | // is declared. |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1460 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); |
| 1461 | if (!CatDecl->IsClassExtension()) |
| 1462 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1463 | E = PDecl->prop_end(); P != E; ++P) { |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1464 | ObjCPropertyDecl *ProtoProp = *P; |
| 1465 | DeclContext::lookup_result R |
| 1466 | = CatDecl->lookup(ProtoProp->getDeclName()); |
| 1467 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 1468 | if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) { |
| 1469 | if (CatProp != ProtoProp) { |
| 1470 | // Property protocol already exist in class. Diagnose any mismatch. |
| 1471 | DiagnosePropertyMismatch(CatProp, ProtoProp, |
| 1472 | PDecl->getIdentifier()); |
| 1473 | } |
| 1474 | } |
| 1475 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1476 | } |
| 1477 | return; |
| 1478 | } |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Class |
| 1481 | // FIXME: We should perform this check when the property in the class |
| 1482 | // is declared. |
| 1483 | ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1484 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1485 | E = PDecl->prop_end(); P != E; ++P) { |
| 1486 | ObjCPropertyDecl *ProtoProp = *P; |
| 1487 | DeclContext::lookup_result R |
Douglas Gregor | aabd094 | 2013-01-21 19:05:22 +0000 | [diff] [blame] | 1488 | = IDecl->lookup(ProtoProp->getDeclName()); |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1489 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 1490 | if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) { |
| 1491 | if (ClassProp != ProtoProp) { |
| 1492 | // Property protocol already exist in class. Diagnose any mismatch. |
| 1493 | DiagnosePropertyMismatch(ClassProp, ProtoProp, |
| 1494 | PDecl->getIdentifier()); |
| 1495 | } |
| 1496 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1497 | } |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1498 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1501 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 1502 | /// for the property in the class and in its categories and implementations |
| 1503 | /// |
| 1504 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
| 1505 | ObjCInterfaceDecl *IDecl) { |
| 1506 | // by far the most common case. |
| 1507 | if (!PDecl->isReadOnly()) |
| 1508 | return false; |
| 1509 | // Even if property is ready only, if interface has a user defined setter, |
| 1510 | // it is not considered read only. |
| 1511 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1512 | return false; |
| 1513 | |
| 1514 | // Main class has the property as 'readonly'. Must search |
| 1515 | // through the category list to see if the property's |
| 1516 | // attribute has been over-ridden to 'readwrite'. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1517 | for (ObjCInterfaceDecl::visible_categories_iterator |
| 1518 | Cat = IDecl->visible_categories_begin(), |
| 1519 | CatEnd = IDecl->visible_categories_end(); |
| 1520 | Cat != CatEnd; ++Cat) { |
| 1521 | if (Cat->getInstanceMethod(PDecl->getSetterName())) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1522 | return false; |
| 1523 | ObjCPropertyDecl *P = |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1524 | Cat->FindPropertyDeclaration(PDecl->getIdentifier()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1525 | if (P && !P->isReadOnly()) |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
| 1529 | // Also, check for definition of a setter method in the implementation if |
| 1530 | // all else failed. |
| 1531 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
| 1532 | if (ObjCImplementationDecl *IMD = |
| 1533 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
| 1534 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
| 1535 | return false; |
| 1536 | } else if (ObjCCategoryImplDecl *CIMD = |
| 1537 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1538 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
| 1539 | return false; |
| 1540 | } |
| 1541 | } |
| 1542 | // Lastly, look through the implementation (if one is in scope). |
| 1543 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) |
| 1544 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1545 | return false; |
| 1546 | // If all fails, look at the super class. |
| 1547 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 1548 | return isPropertyReadonly(PDecl, SIDecl); |
| 1549 | return true; |
| 1550 | } |
| 1551 | |
| 1552 | /// CollectImmediateProperties - This routine collects all properties in |
Douglas Gregor | 0dfe23c | 2013-01-21 18:35:55 +0000 | [diff] [blame] | 1553 | /// 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] | 1554 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, |
Anna Zaks | e63aedd | 2012-10-31 01:18:22 +0000 | [diff] [blame] | 1555 | ObjCContainerDecl::PropertyMap &PropMap, |
| 1556 | ObjCContainerDecl::PropertyMap &SuperPropMap) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1557 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1558 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1559 | E = IDecl->prop_end(); P != E; ++P) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1560 | ObjCPropertyDecl *Prop = *P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1561 | PropMap[Prop->getIdentifier()] = Prop; |
| 1562 | } |
| 1563 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1564 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1565 | PI = IDecl->all_referenced_protocol_begin(), |
| 1566 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1567 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1568 | } |
| 1569 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1570 | if (!CATDecl->IsClassExtension()) |
| 1571 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), |
| 1572 | E = CATDecl->prop_end(); P != E; ++P) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1573 | ObjCPropertyDecl *Prop = *P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1574 | PropMap[Prop->getIdentifier()] = Prop; |
| 1575 | } |
| 1576 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1577 | for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1578 | E = CATDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1579 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1580 | } |
| 1581 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1582 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1583 | E = PDecl->prop_end(); P != E; ++P) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1584 | ObjCPropertyDecl *Prop = *P; |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1585 | ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; |
| 1586 | // Exclude property for protocols which conform to class's super-class, |
| 1587 | // as super-class has to implement the property. |
Fariborz Jahanian | a929ec7 | 2011-09-27 00:23:52 +0000 | [diff] [blame] | 1588 | if (!PropertyFromSuper || |
| 1589 | PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1590 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; |
| 1591 | if (!PropEntry) |
| 1592 | PropEntry = Prop; |
| 1593 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1594 | } |
| 1595 | // scan through protocol's protocols. |
| 1596 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1597 | E = PDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1598 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1602 | /// CollectSuperClassPropertyImplementations - This routine collects list of |
| 1603 | /// properties to be implemented in super class(s) and also coming from their |
| 1604 | /// conforming protocols. |
| 1605 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
Anna Zaks | e63aedd | 2012-10-31 01:18:22 +0000 | [diff] [blame] | 1606 | ObjCInterfaceDecl::PropertyMap &PropMap) { |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1607 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
Fariborz Jahanian | cfaed8d | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1608 | ObjCInterfaceDecl::PropertyDeclOrder PO; |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1609 | while (SDecl) { |
Fariborz Jahanian | cfaed8d | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1610 | SDecl->collectPropertiesToImplement(PropMap, PO); |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1611 | SDecl = SDecl->getSuperClass(); |
| 1612 | } |
| 1613 | } |
| 1614 | } |
| 1615 | |
Fariborz Jahanian | 2620229 | 2013-02-14 19:07:19 +0000 | [diff] [blame] | 1616 | /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is |
| 1617 | /// an ivar synthesized for 'Method' and 'Method' is a property accessor |
| 1618 | /// declared in class 'IFace'. |
| 1619 | bool |
| 1620 | Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace, |
| 1621 | ObjCMethodDecl *Method, ObjCIvarDecl *IV) { |
| 1622 | if (!IV->getSynthesize()) |
| 1623 | return false; |
| 1624 | ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(), |
| 1625 | Method->isInstanceMethod()); |
| 1626 | if (!IMD || !IMD->isPropertyAccessor()) |
| 1627 | return false; |
| 1628 | |
| 1629 | // look up a property declaration whose one of its accessors is implemented |
| 1630 | // by this method. |
| 1631 | for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(), |
| 1632 | E = IFace->prop_end(); P != E; ++P) { |
| 1633 | ObjCPropertyDecl *property = *P; |
| 1634 | if ((property->getGetterName() == IMD->getSelector() || |
| 1635 | property->getSetterName() == IMD->getSelector()) && |
| 1636 | (property->getPropertyIvarDecl() == IV)) |
| 1637 | return true; |
| 1638 | } |
| 1639 | return false; |
| 1640 | } |
| 1641 | |
| 1642 | |
James Dennett | 699c904 | 2012-06-15 07:13:21 +0000 | [diff] [blame] | 1643 | /// \brief Default synthesizes all properties which must be synthesized |
| 1644 | /// in class's \@implementation. |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1645 | void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl, |
| 1646 | ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1647 | |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1648 | ObjCInterfaceDecl::PropertyMap PropMap; |
Fariborz Jahanian | cfaed8d | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1649 | ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder; |
| 1650 | IDecl->collectPropertiesToImplement(PropMap, PropertyOrder); |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1651 | if (PropMap.empty()) |
| 1652 | return; |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1653 | ObjCInterfaceDecl::PropertyMap SuperPropMap; |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1654 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1655 | |
Fariborz Jahanian | cfaed8d | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1656 | for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) { |
| 1657 | ObjCPropertyDecl *Prop = PropertyOrder[i]; |
Fariborz Jahanian | 6071af9 | 2013-06-07 20:26:51 +0000 | [diff] [blame] | 1658 | // Is there a matching property synthesize/dynamic? |
| 1659 | if (Prop->isInvalidDecl() || |
| 1660 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 1661 | continue; |
| 1662 | // Property may have been synthesized by user. |
| 1663 | if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) |
| 1664 | continue; |
| 1665 | if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { |
| 1666 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1667 | continue; |
| 1668 | if (IMPDecl->getInstanceMethod(Prop->getSetterName())) |
| 1669 | continue; |
| 1670 | } |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1671 | // If property to be implemented in the super class, ignore. |
Fariborz Jahanian | 6114a3c | 2013-03-12 19:46:17 +0000 | [diff] [blame] | 1672 | if (SuperPropMap[Prop->getIdentifier()]) { |
| 1673 | ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()]; |
| 1674 | if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) && |
| 1675 | (PropInSuperClass->getPropertyAttributes() & |
Fariborz Jahanian | 1d0d2fe | 2013-03-12 22:22:38 +0000 | [diff] [blame] | 1676 | ObjCPropertyDecl::OBJC_PR_readonly) && |
Fariborz Jahanian | 5bdaef5 | 2013-03-21 20:50:53 +0000 | [diff] [blame] | 1677 | !IMPDecl->getInstanceMethod(Prop->getSetterName()) && |
| 1678 | !IDecl->HasUserDeclaredSetterMethod(Prop)) { |
Fariborz Jahanian | 6114a3c | 2013-03-12 19:46:17 +0000 | [diff] [blame] | 1679 | Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property) |
| 1680 | << Prop->getIdentifier()->getName(); |
| 1681 | Diag(PropInSuperClass->getLocation(), diag::note_property_declare); |
| 1682 | } |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1683 | continue; |
Fariborz Jahanian | 6114a3c | 2013-03-12 19:46:17 +0000 | [diff] [blame] | 1684 | } |
Fariborz Jahanian | a6ba40c | 2013-06-07 18:32:55 +0000 | [diff] [blame] | 1685 | if (ObjCPropertyImplDecl *PID = |
| 1686 | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) { |
| 1687 | if (PID->getPropertyDecl() != Prop) { |
| 1688 | Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property) |
| 1689 | << Prop->getIdentifier()->getName(); |
| 1690 | if (!PID->getLocation().isInvalid()) |
| 1691 | Diag(PID->getLocation(), diag::note_property_synthesize); |
| 1692 | } |
| 1693 | continue; |
| 1694 | } |
Fariborz Jahanian | f8aba8c | 2011-12-15 01:03:18 +0000 | [diff] [blame] | 1695 | if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) { |
| 1696 | // We won't auto-synthesize properties declared in protocols. |
| 1697 | Diag(IMPDecl->getLocation(), |
| 1698 | diag::warn_auto_synthesizing_protocol_property); |
| 1699 | Diag(Prop->getLocation(), diag::note_property_declare); |
| 1700 | continue; |
| 1701 | } |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1702 | |
| 1703 | // We use invalid SourceLocations for the synthesized ivars since they |
| 1704 | // aren't really synthesized at a particular location; they just exist. |
| 1705 | // Saying that they are located at the @implementation isn't really going |
| 1706 | // to help users. |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1707 | ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( |
| 1708 | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), |
| 1709 | true, |
| 1710 | /* property = */ Prop->getIdentifier(), |
Anna Zaks | ad0ce53 | 2012-09-27 19:45:11 +0000 | [diff] [blame] | 1711 | /* ivar = */ Prop->getDefaultSynthIvarName(Context), |
Argyrios Kyrtzidis | 390fff8 | 2012-06-08 02:16:11 +0000 | [diff] [blame] | 1712 | Prop->getLocation())); |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1713 | if (PIDecl) { |
| 1714 | Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis); |
Fariborz Jahanian | 5ea6661 | 2012-05-08 18:03:39 +0000 | [diff] [blame] | 1715 | Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1716 | } |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1717 | } |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1718 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1719 | |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1720 | void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1721 | if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile()) |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1722 | return; |
| 1723 | ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); |
| 1724 | if (!IC) |
| 1725 | return; |
| 1726 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1727 | if (!IDecl->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | eb4f2c5 | 2012-01-03 19:46:00 +0000 | [diff] [blame] | 1728 | DefaultSynthesizeProperties(S, IC, IDecl); |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1731 | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, |
Fariborz Jahanian | c775b1a | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 1732 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | 277076a | 2012-12-19 18:58:55 +0000 | [diff] [blame] | 1733 | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
| 1734 | ObjCInterfaceDecl *IDecl; |
| 1735 | // Gather properties which need not be implemented in this class |
| 1736 | // or category. |
| 1737 | if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))) |
| 1738 | if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1739 | // For categories, no need to implement properties declared in |
| 1740 | // its primary class (and its super classes) if property is |
| 1741 | // declared in one of those containers. |
Fariborz Jahanian | cfaed8d | 2013-02-14 22:33:34 +0000 | [diff] [blame] | 1742 | if ((IDecl = C->getClassInterface())) { |
| 1743 | ObjCInterfaceDecl::PropertyDeclOrder PO; |
| 1744 | IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO); |
| 1745 | } |
Fariborz Jahanian | 277076a | 2012-12-19 18:58:55 +0000 | [diff] [blame] | 1746 | } |
| 1747 | if (IDecl) |
| 1748 | CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap); |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1749 | |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1750 | ObjCContainerDecl::PropertyMap PropMap; |
Fariborz Jahanian | 277076a | 2012-12-19 18:58:55 +0000 | [diff] [blame] | 1751 | CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1752 | if (PropMap.empty()) |
| 1753 | return; |
| 1754 | |
| 1755 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 1756 | for (ObjCImplDecl::propimpl_iterator |
| 1757 | I = IMPDecl->propimpl_begin(), |
| 1758 | EI = IMPDecl->propimpl_end(); I != EI; ++I) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1759 | PropImplMap.insert(I->getPropertyDecl()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1760 | |
Fariborz Jahanian | c775b1a | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 1761 | SelectorSet InsMap; |
| 1762 | // Collect property accessors implemented in current implementation. |
| 1763 | for (ObjCImplementationDecl::instmeth_iterator |
| 1764 | I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) |
| 1765 | InsMap.insert((*I)->getSelector()); |
| 1766 | |
| 1767 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
| 1768 | ObjCInterfaceDecl *PrimaryClass = 0; |
| 1769 | if (C && !C->IsClassExtension()) |
| 1770 | if ((PrimaryClass = C->getClassInterface())) |
| 1771 | // Report unimplemented properties in the category as well. |
| 1772 | if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) { |
| 1773 | // When reporting on missing setter/getters, do not report when |
| 1774 | // setter/getter is implemented in category's primary class |
| 1775 | // implementation. |
| 1776 | for (ObjCImplementationDecl::instmeth_iterator |
| 1777 | I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I) |
| 1778 | InsMap.insert((*I)->getSelector()); |
| 1779 | } |
| 1780 | |
Anna Zaks | b36ea37 | 2012-10-18 19:17:53 +0000 | [diff] [blame] | 1781 | for (ObjCContainerDecl::PropertyMap::iterator |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1782 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1783 | ObjCPropertyDecl *Prop = P->second; |
| 1784 | // Is there a matching propery synthesize/dynamic? |
| 1785 | if (Prop->isInvalidDecl() || |
| 1786 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
Douglas Gregor | 7cdc457 | 2013-01-08 18:16:18 +0000 | [diff] [blame] | 1787 | PropImplMap.count(Prop) || |
| 1788 | Prop->getAvailability() == AR_Unavailable) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1789 | continue; |
Fariborz Jahanian | c775b1a | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 1790 | // When reporting on missing property getter implementation in |
| 1791 | // categories, do not report when they are declared in primary class, |
| 1792 | // class's protocol, or one of it super classes. This is because, |
| 1793 | // the class is going to implement them. |
| 1794 | if (!InsMap.count(Prop->getGetterName()) && |
| 1795 | (PrimaryClass == 0 || |
Fariborz Jahanian | f3f0f35 | 2013-04-25 21:59:34 +0000 | [diff] [blame] | 1796 | !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1797 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1798 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1799 | diag::warn_setter_getter_impl_required_in_category : |
| 1800 | diag::warn_setter_getter_impl_required) |
| 1801 | << Prop->getDeclName() << Prop->getGetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1802 | Diag(Prop->getLocation(), |
| 1803 | diag::note_property_declare); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1804 | if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1805 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1806 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1807 | Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
| 1808 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1809 | } |
Fariborz Jahanian | c775b1a | 2013-04-24 17:06:38 +0000 | [diff] [blame] | 1810 | // When reporting on missing property setter implementation in |
| 1811 | // categories, do not report when they are declared in primary class, |
| 1812 | // class's protocol, or one of it super classes. This is because, |
| 1813 | // the class is going to implement them. |
| 1814 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) && |
| 1815 | (PrimaryClass == 0 || |
Fariborz Jahanian | f3f0f35 | 2013-04-25 21:59:34 +0000 | [diff] [blame] | 1816 | !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1817 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1818 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1819 | diag::warn_setter_getter_impl_required_in_category : |
| 1820 | diag::warn_setter_getter_impl_required) |
| 1821 | << Prop->getDeclName() << Prop->getSetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1822 | Diag(Prop->getLocation(), |
| 1823 | diag::note_property_declare); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 1824 | if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1825 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1826 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1827 | Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | void |
| 1833 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
| 1834 | ObjCContainerDecl* IDecl) { |
| 1835 | // Rules apply in non-GC mode only |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1836 | if (getLangOpts().getGC() != LangOptions::NonGC) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1837 | return; |
| 1838 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), |
| 1839 | E = IDecl->prop_end(); |
| 1840 | I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1841 | ObjCPropertyDecl *Property = *I; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1842 | ObjCMethodDecl *GetterMethod = 0; |
| 1843 | ObjCMethodDecl *SetterMethod = 0; |
| 1844 | bool LookedUpGetterSetter = false; |
| 1845 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1846 | unsigned Attributes = Property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1847 | unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1848 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1849 | if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && |
| 1850 | !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1851 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1852 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1853 | LookedUpGetterSetter = true; |
| 1854 | if (GetterMethod) { |
| 1855 | Diag(GetterMethod->getLocation(), |
| 1856 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1857 | << Property->getIdentifier() << 0; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1858 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1859 | } |
| 1860 | if (SetterMethod) { |
| 1861 | Diag(SetterMethod->getLocation(), |
| 1862 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1863 | << Property->getIdentifier() << 1; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1864 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1865 | } |
| 1866 | } |
| 1867 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1868 | // We only care about readwrite atomic property. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1869 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 1870 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1871 | continue; |
| 1872 | if (const ObjCPropertyImplDecl *PIDecl |
| 1873 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { |
| 1874 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1875 | continue; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1876 | if (!LookedUpGetterSetter) { |
| 1877 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1878 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1879 | LookedUpGetterSetter = true; |
| 1880 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1881 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 1882 | SourceLocation MethodLoc = |
| 1883 | (GetterMethod ? GetterMethod->getLocation() |
| 1884 | : SetterMethod->getLocation()); |
| 1885 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
Fariborz Jahanian | 7d65f69 | 2011-10-06 23:47:58 +0000 | [diff] [blame] | 1886 | << Property->getIdentifier() << (GetterMethod != 0) |
| 1887 | << (SetterMethod != 0); |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 1888 | // fixit stuff. |
| 1889 | if (!AttributesAsWritten) { |
| 1890 | if (Property->getLParenLoc().isValid()) { |
| 1891 | // @property () ... case. |
| 1892 | SourceRange PropSourceRange(Property->getAtLoc(), |
| 1893 | Property->getLParenLoc()); |
| 1894 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1895 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic"); |
| 1896 | } |
| 1897 | else { |
| 1898 | //@property id etc. |
| 1899 | SourceLocation endLoc = |
| 1900 | Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| 1901 | endLoc = endLoc.getLocWithOffset(-1); |
| 1902 | SourceRange PropSourceRange(Property->getAtLoc(), endLoc); |
| 1903 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1904 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) "); |
| 1905 | } |
| 1906 | } |
| 1907 | else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) { |
| 1908 | // @property () ... case. |
| 1909 | SourceLocation endLoc = Property->getLParenLoc(); |
| 1910 | SourceRange PropSourceRange(Property->getAtLoc(), endLoc); |
| 1911 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1912 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, "); |
| 1913 | } |
| 1914 | else |
| 1915 | Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1916 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1922 | void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1923 | if (getLangOpts().getGC() == LangOptions::GCOnly) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1924 | return; |
| 1925 | |
| 1926 | for (ObjCImplementationDecl::propimpl_iterator |
| 1927 | i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1928 | ObjCPropertyImplDecl *PID = *i; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1929 | if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 1930 | continue; |
| 1931 | |
| 1932 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1933 | if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1934 | !D->getInstanceMethod(PD->getGetterName())) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1935 | ObjCMethodDecl *method = PD->getGetterMethodDecl(); |
| 1936 | if (!method) |
| 1937 | continue; |
| 1938 | ObjCMethodFamily family = method->getMethodFamily(); |
| 1939 | if (family == OMF_alloc || family == OMF_copy || |
| 1940 | family == OMF_mutableCopy || family == OMF_new) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1941 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1942 | Diag(PID->getLocation(), diag::err_ownin_getter_rule); |
| 1943 | else |
Ted Kremenek | 920c9c1 | 2011-10-12 18:03:37 +0000 | [diff] [blame] | 1944 | Diag(PID->getLocation(), diag::warn_owning_getter_rule); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1945 | Diag(PD->getLocation(), diag::note_property_declare); |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1951 | /// AddPropertyAttrs - Propagates attributes from a property to the |
| 1952 | /// implicitly-declared getter or setter for that property. |
| 1953 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
| 1954 | ObjCPropertyDecl *Property) { |
| 1955 | // Should we just clone all attributes over? |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1956 | for (Decl::attr_iterator A = Property->attr_begin(), |
| 1957 | AEnd = Property->attr_end(); |
| 1958 | A != AEnd; ++A) { |
| 1959 | if (isa<DeprecatedAttr>(*A) || |
| 1960 | isa<UnavailableAttr>(*A) || |
| 1961 | isa<AvailabilityAttr>(*A)) |
| 1962 | PropertyMethod->addAttr((*A)->clone(S.Context)); |
| 1963 | } |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1966 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 1967 | /// have the property type and issue diagnostics if they don't. |
| 1968 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1969 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1970 | /// methods is the "right" thing to do. |
| 1971 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1972 | ObjCContainerDecl *CD, |
| 1973 | ObjCPropertyDecl *redeclaredProperty, |
| 1974 | ObjCContainerDecl *lexicalDC) { |
| 1975 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1976 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 1977 | |
| 1978 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
| 1979 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
| 1980 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
| 1981 | property->getLocation()); |
| 1982 | |
| 1983 | if (SetterMethod) { |
| 1984 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1985 | property->getPropertyAttributes(); |
| 1986 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && |
| 1987 | Context.getCanonicalType(SetterMethod->getResultType()) != |
| 1988 | Context.VoidTy) |
| 1989 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
| 1990 | if (SetterMethod->param_size() != 1 || |
Fariborz Jahanian | 2aac0c9 | 2011-09-26 22:59:09 +0000 | [diff] [blame] | 1991 | !Context.hasSameUnqualifiedType( |
Fariborz Jahanian | bb13c32 | 2011-10-15 17:36:49 +0000 | [diff] [blame] | 1992 | (*SetterMethod->param_begin())->getType().getNonReferenceType(), |
| 1993 | property->getType().getNonReferenceType())) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1994 | Diag(property->getLocation(), |
| 1995 | diag::warn_accessor_property_type_mismatch) |
| 1996 | << property->getDeclName() |
| 1997 | << SetterMethod->getSelector(); |
| 1998 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | // Synthesize getter/setter methods if none exist. |
| 2003 | // Find the default getter and if one not found, add one. |
| 2004 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 2005 | // synthesize these methods unless the user explicitly provided prototypes |
| 2006 | // (which is odd, but allowed). Sema should be typechecking that the |
| 2007 | // declarations jive in that situation (which it is not currently). |
| 2008 | if (!GetterMethod) { |
| 2009 | // No instance method of same name as property getter name was found. |
| 2010 | // Declare a getter method and add it to the list of methods |
| 2011 | // for this class. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 2012 | SourceLocation Loc = redeclaredProperty ? |
| 2013 | redeclaredProperty->getLocation() : |
| 2014 | property->getLocation(); |
| 2015 | |
| 2016 | GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, |
| 2017 | property->getGetterName(), |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2018 | property->getType(), 0, CD, /*isInstance=*/true, |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2019 | /*isVariadic=*/false, /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2020 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2021 | (property->getPropertyImplementation() == |
| 2022 | ObjCPropertyDecl::Optional) ? |
| 2023 | ObjCMethodDecl::Optional : |
| 2024 | ObjCMethodDecl::Required); |
| 2025 | CD->addDecl(GetterMethod); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 2026 | |
| 2027 | AddPropertyAttrs(*this, GetterMethod, property); |
| 2028 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 2029 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 2030 | // and the real context should be the same. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 2031 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 2032 | GetterMethod->setLexicalDeclContext(lexicalDC); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 2033 | if (property->hasAttr<NSReturnsNotRetainedAttr>()) |
| 2034 | GetterMethod->addAttr( |
| 2035 | ::new (Context) NSReturnsNotRetainedAttr(Loc, Context)); |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 2036 | |
| 2037 | if (getLangOpts().ObjCAutoRefCount) |
| 2038 | CheckARCMethodDecl(GetterMethod); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2039 | } else |
| 2040 | // A user declared getter will be synthesize when @synthesize of |
| 2041 | // the property with the same name is seen in the @implementation |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2042 | GetterMethod->setPropertyAccessor(true); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2043 | property->setGetterMethodDecl(GetterMethod); |
| 2044 | |
| 2045 | // Skip setter if property is read-only. |
| 2046 | if (!property->isReadOnly()) { |
| 2047 | // Find the default setter and if one not found, add one. |
| 2048 | if (!SetterMethod) { |
| 2049 | // No instance method of same name as property setter name was found. |
| 2050 | // Declare a setter method and add it to the list of methods |
| 2051 | // for this class. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 2052 | SourceLocation Loc = redeclaredProperty ? |
| 2053 | redeclaredProperty->getLocation() : |
| 2054 | property->getLocation(); |
| 2055 | |
| 2056 | SetterMethod = |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 2057 | ObjCMethodDecl::Create(Context, Loc, Loc, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 2058 | property->getSetterName(), Context.VoidTy, 0, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2059 | CD, /*isInstance=*/true, /*isVariadic=*/false, |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2060 | /*isPropertyAccessor=*/true, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 2061 | /*isImplicitlyDeclared=*/true, |
| 2062 | /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2063 | (property->getPropertyImplementation() == |
| 2064 | ObjCPropertyDecl::Optional) ? |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 2065 | ObjCMethodDecl::Optional : |
| 2066 | ObjCMethodDecl::Required); |
| 2067 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2068 | // Invent the arguments for the setter. We don't bother making a |
| 2069 | // nice name for the argument. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2070 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
| 2071 | Loc, Loc, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2072 | property->getIdentifier(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2073 | property->getType().getUnqualifiedType(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2074 | /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2075 | SC_None, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2076 | 0); |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 2077 | SetterMethod->setMethodParams(Context, Argument, None); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 2078 | |
| 2079 | AddPropertyAttrs(*this, SetterMethod, property); |
| 2080 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2081 | CD->addDecl(SetterMethod); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 2082 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 2083 | // and the real context should be the same. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 2084 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 2085 | SetterMethod->setLexicalDeclContext(lexicalDC); |
John McCall | b846381 | 2013-04-04 01:38:37 +0000 | [diff] [blame] | 2086 | |
| 2087 | // It's possible for the user to have set a very odd custom |
| 2088 | // setter selector that causes it to have a method family. |
| 2089 | if (getLangOpts().ObjCAutoRefCount) |
| 2090 | CheckARCMethodDecl(SetterMethod); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2091 | } else |
| 2092 | // A user declared setter will be synthesize when @synthesize of |
| 2093 | // the property with the same name is seen in the @implementation |
Jordan Rose | 1e4691b | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 2094 | SetterMethod->setPropertyAccessor(true); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2095 | property->setSetterMethodDecl(SetterMethod); |
| 2096 | } |
| 2097 | // Add any synthesized methods to the global pool. This allows us to |
| 2098 | // handle the following, which is supported by GCC (and part of the design). |
| 2099 | // |
| 2100 | // @interface Foo |
| 2101 | // @property double bar; |
| 2102 | // @end |
| 2103 | // |
| 2104 | // void thisIsUnfortunate() { |
| 2105 | // id foo; |
| 2106 | // double bar = [foo bar]; |
| 2107 | // } |
| 2108 | // |
| 2109 | if (GetterMethod) |
| 2110 | AddInstanceMethodToGlobalPool(GetterMethod); |
| 2111 | if (SetterMethod) |
| 2112 | AddInstanceMethodToGlobalPool(SetterMethod); |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 2113 | |
| 2114 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); |
| 2115 | if (!CurrentClass) { |
| 2116 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) |
| 2117 | CurrentClass = Cat->getClassInterface(); |
| 2118 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) |
| 2119 | CurrentClass = Impl->getClassInterface(); |
| 2120 | } |
| 2121 | if (GetterMethod) |
| 2122 | CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); |
| 2123 | if (SetterMethod) |
| 2124 | CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2127 | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2128 | SourceLocation Loc, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2129 | unsigned &Attributes, |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 2130 | bool propertyInPrimaryClass) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2131 | // FIXME: Improve the reported location. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2132 | if (!PDecl || PDecl->isInvalidDecl()) |
Ted Kremenek | 5fcd52a | 2010-04-05 22:39:42 +0000 | [diff] [blame] | 2133 | return; |
| 2134 | |
| 2135 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 2136 | QualType PropertyTy = PropertyDecl->getType(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2137 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2138 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2139 | (Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 2140 | PropertyTy->isObjCRetainableType()) { |
| 2141 | // 'readonly' property with no obvious lifetime. |
| 2142 | // its life time will be determined by its backing ivar. |
| 2143 | unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 2144 | ObjCDeclSpec::DQ_PR_copy | |
| 2145 | ObjCDeclSpec::DQ_PR_retain | |
| 2146 | ObjCDeclSpec::DQ_PR_strong | |
| 2147 | ObjCDeclSpec::DQ_PR_weak | |
| 2148 | ObjCDeclSpec::DQ_PR_assign); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2149 | if ((Attributes & rel) == 0) |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 2150 | return; |
| 2151 | } |
| 2152 | |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 2153 | if (propertyInPrimaryClass) { |
| 2154 | // we postpone most property diagnosis until class's implementation |
| 2155 | // because, its readonly attribute may be overridden in its class |
| 2156 | // extensions making other attributes, which make no sense, to make sense. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2157 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2158 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) |
Fariborz Jahanian | cea06d2 | 2012-06-20 22:57:42 +0000 | [diff] [blame] | 2159 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2160 | << "readonly" << "readwrite"; |
| 2161 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2162 | // readonly and readwrite/assign/retain/copy conflict. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2163 | else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2164 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2165 | ObjCDeclSpec::DQ_PR_assign | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2166 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2167 | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2168 | ObjCDeclSpec::DQ_PR_retain | |
| 2169 | ObjCDeclSpec::DQ_PR_strong))) { |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2170 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2171 | "readwrite" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2172 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2173 | "assign" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2174 | (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2175 | "unsafe_unretained" : |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2176 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2177 | "copy" : "retain"; |
| 2178 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2179 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2180 | diag::err_objc_property_attr_mutually_exclusive : |
| 2181 | diag::warn_objc_property_attr_mutually_exclusive) |
| 2182 | << "readonly" << which; |
| 2183 | } |
| 2184 | |
| 2185 | // Check for copy or retain on non-object types. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2186 | if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2187 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && |
| 2188 | !PropertyTy->isObjCRetainableType() && |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 2189 | !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2190 | Diag(Loc, diag::err_objc_property_requires_object) |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2191 | << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : |
| 2192 | Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); |
| 2193 | Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2194 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); |
John McCall | 977ea78 | 2012-02-21 21:48:05 +0000 | [diff] [blame] | 2195 | PropertyDecl->setInvalidDecl(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | // Check for more than one of { assign, copy, retain }. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2199 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 2200 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2201 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2202 | << "assign" << "copy"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2203 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2204 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2205 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2206 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2207 | << "assign" << "retain"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2208 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2209 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2210 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2211 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2212 | << "assign" << "strong"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2213 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2214 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2215 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2216 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2217 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2218 | << "assign" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2219 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2220 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2221 | } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { |
| 2222 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2223 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2224 | << "unsafe_unretained" << "copy"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2225 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2226 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2227 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2228 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2229 | << "unsafe_unretained" << "retain"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2230 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2231 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2232 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2233 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2234 | << "unsafe_unretained" << "strong"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2235 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2236 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2237 | if (getLangOpts().ObjCAutoRefCount && |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2238 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2239 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2240 | << "unsafe_unretained" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2241 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2242 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2243 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 2244 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2245 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2246 | << "copy" << "retain"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2247 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2248 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2249 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2250 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2251 | << "copy" << "strong"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2252 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2253 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2254 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2255 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2256 | << "copy" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2257 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2258 | } |
| 2259 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2260 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2261 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2262 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2263 | << "retain" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2264 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2265 | } |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2266 | else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 2267 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2268 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2269 | << "strong" << "weak"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2270 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2273 | if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && |
| 2274 | (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { |
Fariborz Jahanian | 9d1bbea | 2011-10-10 21:53:24 +0000 | [diff] [blame] | 2275 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2276 | << "atomic" << "nonatomic"; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2277 | Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; |
Fariborz Jahanian | 9d1bbea | 2011-10-10 21:53:24 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2280 | // Warn if user supplied no assignment attribute, property is |
| 2281 | // readwrite, and this is an object type. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2282 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2283 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 2284 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong | |
| 2285 | ObjCDeclSpec::DQ_PR_weak)) && |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2286 | PropertyTy->isObjCObjectPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2287 | if (getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2288 | // With arc, @property definitions should default to (strong) when |
Fariborz Jahanian | f21a92d | 2011-11-08 20:58:53 +0000 | [diff] [blame] | 2289 | // not specified; including when property is 'readonly'. |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2290 | PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2291 | else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) { |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2292 | bool isAnyClassTy = |
| 2293 | (PropertyTy->isObjCClassType() || |
| 2294 | PropertyTy->isObjCQualifiedClassType()); |
| 2295 | // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to |
| 2296 | // issue any warning. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2297 | if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2298 | ; |
Fariborz Jahanian | f224fb5 | 2012-09-17 23:57:35 +0000 | [diff] [blame] | 2299 | else if (propertyInPrimaryClass) { |
| 2300 | // Don't issue warning on property with no life time in class |
| 2301 | // extension as it is inherited from property in primary class. |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2302 | // Skip this warning in gc-only mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2303 | if (getLangOpts().getGC() != LangOptions::GCOnly) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2304 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2305 | |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2306 | // If non-gc code warn that this is likely inappropriate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2307 | if (getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2308 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2309 | } |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2310 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2311 | |
| 2312 | // FIXME: Implement warning dependent on NSCopying being |
| 2313 | // implemented. See also: |
| 2314 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 2315 | // (please trim this list while you are at it). |
| 2316 | } |
| 2317 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2318 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 2319 | &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2320 | && getLangOpts().getGC() == LangOptions::GCOnly |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2321 | && PropertyTy->isBlockPointerType()) |
| 2322 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2323 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2324 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2325 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
Fariborz Jahanian | 528a499 | 2011-09-14 18:03:46 +0000 | [diff] [blame] | 2326 | PropertyTy->isBlockPointerType()) |
| 2327 | Diag(Loc, diag::warn_objc_property_retain_of_block); |
Fariborz Jahanian | 48a98c7 | 2011-11-01 23:02:16 +0000 | [diff] [blame] | 2328 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2329 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2330 | (Attributes & ObjCDeclSpec::DQ_PR_setter)) |
Fariborz Jahanian | 48a98c7 | 2011-11-01 23:02:16 +0000 | [diff] [blame] | 2331 | Diag(Loc, diag::warn_objc_readonly_property_has_setter); |
| 2332 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2333 | } |