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