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