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