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" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // Grammar actions. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 27 | Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
| 28 | FieldDeclarator &FD, |
| 29 | ObjCDeclSpec &ODS, |
| 30 | Selector GetterSel, |
| 31 | Selector SetterSel, |
| 32 | Decl *ClassCategory, |
| 33 | bool *isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 34 | tok::ObjCKeywordKind MethodImplKind, |
| 35 | DeclContext *lexicalDC) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 36 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 37 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
| 38 | // default is readwrite! |
| 39 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
| 40 | // property is defaulted to 'assign' if it is readwrite and is |
| 41 | // not retain or copy |
| 42 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
| 43 | (isReadWrite && |
| 44 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 45 | !(Attributes & ObjCDeclSpec::DQ_PR_copy))); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 46 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 47 | TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); |
| 48 | QualType T = TSI->getType(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 49 | if (T->isReferenceType()) { |
| 50 | Diag(AtLoc, diag::error_reference_property); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 51 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 52 | } |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 53 | // Proceed with constructing the ObjCPropertDecls. |
| 54 | ObjCContainerDecl *ClassDecl = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 55 | cast<ObjCContainerDecl>(ClassCategory); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 57 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 58 | if (CDecl->IsClassExtension()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 59 | Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc, |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 60 | FD, GetterSel, SetterSel, |
| 61 | isAssign, isReadWrite, |
| 62 | Attributes, |
| 63 | isOverridingProperty, TSI, |
| 64 | MethodImplKind); |
| 65 | if (Res) |
| 66 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes); |
| 67 | return Res; |
| 68 | } |
| 69 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 70 | Decl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD, |
| 71 | GetterSel, SetterSel, |
| 72 | isAssign, isReadWrite, |
| 73 | Attributes, TSI, MethodImplKind); |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 74 | if (lexicalDC) |
| 75 | Res->setLexicalDeclContext(lexicalDC); |
| 76 | |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 77 | // Validate the attributes on the @property. |
| 78 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes); |
| 79 | return Res; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 80 | } |
Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 81 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 82 | Decl * |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 83 | Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl, |
| 84 | SourceLocation AtLoc, FieldDeclarator &FD, |
| 85 | Selector GetterSel, Selector SetterSel, |
| 86 | const bool isAssign, |
| 87 | const bool isReadWrite, |
| 88 | const unsigned Attributes, |
| 89 | bool *isOverridingProperty, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 90 | TypeSourceInfo *T, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 91 | tok::ObjCKeywordKind MethodImplKind) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 93 | // Diagnose if this property is already in continuation class. |
| 94 | DeclContext *DC = cast<DeclContext>(CDecl); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 95 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Fariborz Jahanian | 2a28914 | 2010-11-10 18:01:36 +0000 | [diff] [blame] | 96 | ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); |
| 97 | |
| 98 | if (CCPrimary) |
| 99 | // Check for duplicate declaration of this property in current and |
| 100 | // other class extensions. |
| 101 | for (const ObjCCategoryDecl *ClsExtDecl = |
| 102 | CCPrimary->getFirstClassExtension(); |
| 103 | ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) { |
| 104 | if (ObjCPropertyDecl *prevDecl = |
| 105 | ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) { |
| 106 | Diag(AtLoc, diag::err_duplicate_property); |
| 107 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
| 108 | return 0; |
| 109 | } |
| 110 | } |
| 111 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 112 | // Create a new ObjCPropertyDecl with the DeclContext being |
| 113 | // the class extension. |
| 114 | ObjCPropertyDecl *PDecl = |
| 115 | ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(), |
| 116 | PropertyId, AtLoc, T); |
Fariborz Jahanian | 22f757b | 2010-03-22 23:25:52 +0000 | [diff] [blame] | 117 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 118 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 119 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
| 120 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
| 121 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 122 | DC->addDecl(PDecl); |
| 123 | |
| 124 | // We need to look in the @interface to see if the @property was |
| 125 | // already declared. |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 126 | if (!CCPrimary) { |
| 127 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 128 | *isOverridingProperty = true; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 129 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Find the property in continuation class's primary class only. |
| 133 | ObjCPropertyDecl *PIDecl = |
| 134 | CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId); |
| 135 | |
| 136 | if (!PIDecl) { |
| 137 | // No matching property found in the primary class. Just fall thru |
| 138 | // and add property to continuation class's primary class. |
| 139 | ObjCPropertyDecl *PDecl = |
| 140 | CreatePropertyDecl(S, CCPrimary, AtLoc, |
| 141 | FD, GetterSel, SetterSel, isAssign, isReadWrite, |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 142 | Attributes, T, MethodImplKind, DC); |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 143 | // Mark written attribute as having no attribute because |
| 144 | // this is not a user-written property declaration in primary |
| 145 | // class. |
| 146 | PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 147 | |
| 148 | // A case of continuation class adding a new property in the class. This |
| 149 | // is not what it was meant for. However, gcc supports it and so should we. |
| 150 | // Make sure setter/getters are declared here. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 151 | ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0, |
| 152 | /* lexicalDC = */ CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 153 | return PDecl; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // The property 'PIDecl's readonly attribute will be over-ridden |
| 157 | // with continuation class's readwrite property attribute! |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 158 | unsigned PIkind = PIDecl->getPropertyAttributesAsWritten(); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 159 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
| 160 | unsigned retainCopyNonatomic = |
| 161 | (ObjCPropertyDecl::OBJC_PR_retain | |
| 162 | ObjCPropertyDecl::OBJC_PR_copy | |
| 163 | ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 164 | if ((Attributes & retainCopyNonatomic) != |
| 165 | (PIkind & retainCopyNonatomic)) { |
| 166 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
| 167 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 168 | } |
Ted Kremenek | 9944c76 | 2010-03-18 01:22:36 +0000 | [diff] [blame] | 169 | DeclContext *DC = cast<DeclContext>(CCPrimary); |
| 170 | if (!ObjCPropertyDecl::findPropertyDecl(DC, |
| 171 | PIDecl->getDeclName().getAsIdentifierInfo())) { |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 172 | // Protocol is not in the primary class. Must build one for it. |
| 173 | ObjCDeclSpec ProtocolPropertyODS; |
| 174 | // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind |
| 175 | // and ObjCPropertyDecl::PropertyAttributeKind have identical |
| 176 | // values. Should consolidate both into one enum type. |
| 177 | ProtocolPropertyODS. |
| 178 | setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind) |
| 179 | PIkind); |
| 180 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 181 | Decl *ProtocolPtrTy = |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 182 | ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS, |
| 183 | PIDecl->getGetterName(), |
| 184 | PIDecl->getSetterName(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 185 | CCPrimary, isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 186 | MethodImplKind, |
| 187 | /* lexicalDC = */ CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 188 | PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 189 | } |
| 190 | PIDecl->makeitReadWriteAttribute(); |
| 191 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 192 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 193 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 194 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 195 | PIDecl->setSetterName(SetterSel); |
| 196 | } else { |
Ted Kremenek | 788f489 | 2010-10-21 18:49:42 +0000 | [diff] [blame] | 197 | // Tailor the diagnostics for the common case where a readwrite |
| 198 | // property is declared both in the @interface and the continuation. |
| 199 | // This is a common error where the user often intended the original |
| 200 | // declaration to be readonly. |
| 201 | unsigned diag = |
| 202 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite) && |
| 203 | (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) |
| 204 | ? diag::err_use_continuation_class_redeclaration_readwrite |
| 205 | : diag::err_use_continuation_class; |
| 206 | Diag(AtLoc, diag) |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 207 | << CCPrimary->getDeclName(); |
| 208 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 209 | } |
| 210 | *isOverridingProperty = true; |
| 211 | // 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] | 212 | ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 213 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, |
| 217 | ObjCContainerDecl *CDecl, |
| 218 | SourceLocation AtLoc, |
| 219 | FieldDeclarator &FD, |
| 220 | Selector GetterSel, |
| 221 | Selector SetterSel, |
| 222 | const bool isAssign, |
| 223 | const bool isReadWrite, |
| 224 | const unsigned Attributes, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 225 | TypeSourceInfo *TInfo, |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 226 | tok::ObjCKeywordKind MethodImplKind, |
| 227 | DeclContext *lexicalDC){ |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 228 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 229 | QualType T = TInfo->getType(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 230 | |
| 231 | // Issue a warning if property is 'assign' as default and its object, which is |
| 232 | // gc'able conforms to NSCopying protocol |
| 233 | if (getLangOptions().getGCMode() != LangOptions::NonGC && |
| 234 | isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 235 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 236 | T->getAs<ObjCObjectPointerType>()) { |
| 237 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 238 | if (IDecl) |
| 239 | if (ObjCProtocolDecl* PNSCopying = |
| 240 | LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) |
| 241 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
| 242 | Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 243 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 244 | if (T->isObjCObjectType()) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 245 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); |
| 246 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 247 | DeclContext *DC = cast<DeclContext>(CDecl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 248 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
| 249 | FD.D.getIdentifierLoc(), |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 250 | PropertyId, AtLoc, TInfo); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 252 | if (ObjCPropertyDecl *prevDecl = |
| 253 | ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 254 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
Ted Kremenek | 894ae6a | 2010-03-15 18:47:25 +0000 | [diff] [blame] | 255 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 256 | PDecl->setInvalidDecl(); |
| 257 | } |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 258 | else { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 259 | DC->addDecl(PDecl); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 260 | if (lexicalDC) |
| 261 | PDecl->setLexicalDeclContext(lexicalDC); |
| 262 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 263 | |
| 264 | if (T->isArrayType() || T->isFunctionType()) { |
| 265 | Diag(AtLoc, diag::err_property_type) << T; |
| 266 | PDecl->setInvalidDecl(); |
| 267 | } |
| 268 | |
| 269 | ProcessDeclAttributes(S, PDecl, FD.D); |
| 270 | |
| 271 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 272 | // selector names in anticipation of declaration of setter/getter methods. |
| 273 | PDecl->setGetterName(GetterSel); |
| 274 | PDecl->setSetterName(SetterSel); |
| 275 | |
| 276 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 277 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 278 | |
| 279 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
| 280 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
| 281 | |
| 282 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
| 283 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
| 284 | |
| 285 | if (isReadWrite) |
| 286 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
| 287 | |
| 288 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 289 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 290 | |
| 291 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 292 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 293 | |
| 294 | if (isAssign) |
| 295 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 296 | |
| 297 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
| 298 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 299 | |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 300 | PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes()); |
| 301 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 302 | if (MethodImplKind == tok::objc_required) |
| 303 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 304 | else if (MethodImplKind == tok::objc_optional) |
| 305 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 307 | return PDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | |
| 311 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 312 | /// builds the AST node for a property implementation declaration; declared |
| 313 | /// as @synthesize or @dynamic. |
| 314 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 315 | Decl *Sema::ActOnPropertyImplDecl(Scope *S, |
| 316 | SourceLocation AtLoc, |
| 317 | SourceLocation PropertyLoc, |
| 318 | bool Synthesize, |
| 319 | Decl *ClassCatImpDecl, |
| 320 | IdentifierInfo *PropertyId, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 321 | IdentifierInfo *PropertyIvar, |
| 322 | SourceLocation PropertyIvarLoc) { |
Ted Kremenek | e968657 | 2010-04-05 23:45:09 +0000 | [diff] [blame] | 323 | ObjCContainerDecl *ClassImpDecl = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 324 | cast_or_null<ObjCContainerDecl>(ClassCatImpDecl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 325 | // Make sure we have a context for the property implementation declaration. |
| 326 | if (!ClassImpDecl) { |
| 327 | Diag(AtLoc, diag::error_missing_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 328 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 329 | } |
| 330 | ObjCPropertyDecl *property = 0; |
| 331 | ObjCInterfaceDecl* IDecl = 0; |
| 332 | // Find the class or category class where this property must have |
| 333 | // a declaration. |
| 334 | ObjCImplementationDecl *IC = 0; |
| 335 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 336 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
| 337 | IDecl = IC->getClassInterface(); |
| 338 | // We always synthesize an interface for an implementation |
| 339 | // without an interface decl. So, IDecl is always non-zero. |
| 340 | assert(IDecl && |
| 341 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 342 | |
| 343 | // Look for this property declaration in the @implementation's @interface |
| 344 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 345 | if (!property) { |
| 346 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 347 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 348 | } |
| 349 | if (const ObjCCategoryDecl *CD = |
| 350 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
| 351 | if (!CD->IsClassExtension()) { |
| 352 | Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); |
| 353 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 354 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
| 358 | if (Synthesize) { |
| 359 | Diag(AtLoc, diag::error_synthesize_category_decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 360 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 361 | } |
| 362 | IDecl = CatImplClass->getClassInterface(); |
| 363 | if (!IDecl) { |
| 364 | Diag(AtLoc, diag::error_missing_property_interface); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 365 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 366 | } |
| 367 | ObjCCategoryDecl *Category = |
| 368 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 369 | |
| 370 | // If category for this implementation not found, it is an error which |
| 371 | // has already been reported eralier. |
| 372 | if (!Category) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 373 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 374 | // Look for this property declaration in @implementation's category |
| 375 | property = Category->FindPropertyDeclaration(PropertyId); |
| 376 | if (!property) { |
| 377 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
| 378 | << Category->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 379 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 380 | } |
| 381 | } else { |
| 382 | Diag(AtLoc, diag::error_bad_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 383 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 384 | } |
| 385 | ObjCIvarDecl *Ivar = 0; |
| 386 | // Check that we have a valid, previously declared ivar for @synthesize |
| 387 | if (Synthesize) { |
| 388 | // @synthesize |
| 389 | if (!PropertyIvar) |
| 390 | PropertyIvar = PropertyId; |
| 391 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 392 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
| 393 | ObjCInterfaceDecl *ClassDeclared; |
| 394 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
| 395 | if (!Ivar) { |
Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 396 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc, |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 397 | PropertyIvar, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 2846b2b | 2010-04-06 23:36:17 +0000 | [diff] [blame] | 398 | ObjCIvarDecl::Protected, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 399 | (Expr *)0, true); |
Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 400 | ClassImpDecl->addDecl(Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 401 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 402 | property->setPropertyIvarDecl(Ivar); |
| 403 | |
| 404 | if (!getLangOptions().ObjCNonFragileABI) |
| 405 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; |
| 406 | // Note! I deliberately want it to fall thru so, we have a |
| 407 | // a property implementation and to avoid future warnings. |
| 408 | } else if (getLangOptions().ObjCNonFragileABI && |
| 409 | ClassDeclared != IDecl) { |
| 410 | Diag(PropertyLoc, diag::error_ivar_in_superclass_use) |
| 411 | << property->getDeclName() << Ivar->getDeclName() |
| 412 | << ClassDeclared->getDeclName(); |
| 413 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 414 | << Ivar << Ivar->getName(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 415 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 416 | } |
| 417 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 418 | |
| 419 | // Check that type of property and its ivar are type compatible. |
| 420 | if (PropType != IvarType) { |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 421 | bool compat = false; |
| 422 | if (isa<ObjCObjectPointerType>(PropType) |
| 423 | && isa<ObjCObjectPointerType>(IvarType)) |
| 424 | compat = |
| 425 | Context.canAssignObjCInterfaces( |
| 426 | PropType->getAs<ObjCObjectPointerType>(), |
| 427 | IvarType->getAs<ObjCObjectPointerType>()); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 428 | else |
| 429 | compat = (CheckAssignmentConstraints(PropType, IvarType) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 430 | == Compatible); |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 431 | if (!compat) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 432 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 433 | << property->getDeclName() << PropType |
| 434 | << Ivar->getDeclName() << IvarType; |
| 435 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 436 | // Note! I deliberately want it to fall thru so, we have a |
| 437 | // a property implementation and to avoid future warnings. |
| 438 | } |
| 439 | |
| 440 | // FIXME! Rules for properties are somewhat different that those |
| 441 | // for assignments. Use a new routine to consolidate all cases; |
| 442 | // specifically for property redeclarations as well as for ivars. |
| 443 | QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); |
| 444 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 445 | if (lhsType != rhsType && |
| 446 | lhsType->isArithmeticType()) { |
| 447 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 448 | << property->getDeclName() << PropType |
| 449 | << Ivar->getDeclName() << IvarType; |
| 450 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 451 | // Fall thru - see previous comment |
| 452 | } |
| 453 | // __weak is explicit. So it works on Canonical type. |
| 454 | if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
| 455 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 456 | Diag(PropertyLoc, diag::error_weak_property) |
| 457 | << property->getDeclName() << Ivar->getDeclName(); |
| 458 | // Fall thru - see previous comment |
| 459 | } |
| 460 | if ((property->getType()->isObjCObjectPointerType() || |
| 461 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
| 462 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 463 | Diag(PropertyLoc, diag::error_strong_property) |
| 464 | << property->getDeclName() << Ivar->getDeclName(); |
| 465 | // Fall thru - see previous comment |
| 466 | } |
| 467 | } |
| 468 | } else if (PropertyIvar) |
| 469 | // @dynamic |
| 470 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
| 471 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
| 472 | ObjCPropertyImplDecl *PIDecl = |
| 473 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 474 | property, |
| 475 | (Synthesize ? |
| 476 | ObjCPropertyImplDecl::Synthesize |
| 477 | : ObjCPropertyImplDecl::Dynamic), |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 478 | Ivar, PropertyIvarLoc); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 479 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
| 480 | getterMethod->createImplicitParams(Context, IDecl); |
Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 481 | if (getLangOptions().CPlusPlus && Synthesize && |
| 482 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 483 | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
| 484 | // returned by the getter as it must conform to C++'s copy-return rules. |
| 485 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 486 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
| 487 | DeclRefExpr *SelfExpr = |
| 488 | new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(), |
| 489 | SourceLocation()); |
| 490 | Expr *IvarRefExpr = |
| 491 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, |
| 492 | SelfExpr, true, true); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 493 | ExprResult Res = |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 494 | PerformCopyInitialization(InitializedEntity::InitializeResult( |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 495 | SourceLocation(), |
| 496 | getterMethod->getResultType(), |
| 497 | /*NRVO=*/false), |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 498 | SourceLocation(), |
| 499 | Owned(IvarRefExpr)); |
| 500 | if (!Res.isInvalid()) { |
| 501 | Expr *ResExpr = Res.takeAs<Expr>(); |
| 502 | if (ResExpr) |
| 503 | ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr); |
| 504 | PIDecl->setGetterCXXConstructor(ResExpr); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
| 509 | setterMethod->createImplicitParams(Context, IDecl); |
Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 510 | if (getLangOptions().CPlusPlus && Synthesize |
| 511 | && Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 512 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 513 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
| 514 | DeclRefExpr *SelfExpr = |
| 515 | new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(), |
| 516 | SourceLocation()); |
| 517 | Expr *lhs = |
| 518 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, |
| 519 | SelfExpr, true, true); |
| 520 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
| 521 | ParmVarDecl *Param = (*P); |
| 522 | Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(), |
| 523 | SourceLocation()); |
Fariborz Jahanian | fa43239 | 2010-10-14 21:30:10 +0000 | [diff] [blame] | 524 | ExprResult Res = BuildBinOp(S, lhs->getLocEnd(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 525 | BO_Assign, lhs, rhs); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 526 | PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); |
| 527 | } |
| 528 | } |
| 529 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 530 | if (IC) { |
| 531 | if (Synthesize) |
| 532 | if (ObjCPropertyImplDecl *PPIDecl = |
| 533 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 534 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 535 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 536 | << PropertyIvar; |
| 537 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 538 | } |
| 539 | |
| 540 | if (ObjCPropertyImplDecl *PPIDecl |
| 541 | = IC->FindPropertyImplDecl(PropertyId)) { |
| 542 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 543 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 544 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 545 | } |
| 546 | IC->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 547 | if (getLangOptions().ObjCNonFragileABI2) { |
| 548 | // Diagnose if an ivar was lazily synthesdized due to a previous |
| 549 | // use and if 1) property is @dynamic or 2) property is synthesized |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 550 | // but it requires an ivar of different name. |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 551 | ObjCInterfaceDecl *ClassDeclared; |
| 552 | ObjCIvarDecl *Ivar = 0; |
| 553 | if (!Synthesize) |
| 554 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 555 | else { |
| 556 | if (PropertyIvar && PropertyIvar != PropertyId) |
| 557 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 558 | } |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 559 | // Issue diagnostics only if Ivar belongs to current class. |
| 560 | if (Ivar && Ivar->getSynthesize() && |
| 561 | IC->getClassInterface() == ClassDeclared) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 562 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) |
| 563 | << PropertyId; |
| 564 | Ivar->setInvalidDecl(); |
| 565 | } |
| 566 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 567 | } else { |
| 568 | if (Synthesize) |
| 569 | if (ObjCPropertyImplDecl *PPIDecl = |
| 570 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 571 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 572 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 573 | << PropertyIvar; |
| 574 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 575 | } |
| 576 | |
| 577 | if (ObjCPropertyImplDecl *PPIDecl = |
| 578 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
| 579 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 580 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 581 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 582 | } |
| 583 | CatImplClass->addPropertyImplementation(PIDecl); |
| 584 | } |
| 585 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 586 | return PIDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | //===----------------------------------------------------------------------===// |
| 590 | // Helper methods. |
| 591 | //===----------------------------------------------------------------------===// |
| 592 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 593 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 594 | /// attributes and types and warns on a variety of inconsistencies. |
| 595 | /// |
| 596 | void |
| 597 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 598 | ObjCPropertyDecl *SuperProperty, |
| 599 | const IdentifierInfo *inheritedName) { |
| 600 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 601 | Property->getPropertyAttributes(); |
| 602 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 603 | SuperProperty->getPropertyAttributes(); |
| 604 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 605 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 606 | Diag(Property->getLocation(), diag::warn_readonly_property) |
| 607 | << Property->getDeclName() << inheritedName; |
| 608 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 609 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
| 610 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 611 | << Property->getDeclName() << "copy" << inheritedName; |
| 612 | else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) |
| 613 | != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) |
| 614 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 615 | << Property->getDeclName() << "retain" << inheritedName; |
| 616 | |
| 617 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 618 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
| 619 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 620 | << Property->getDeclName() << "atomic" << inheritedName; |
| 621 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
| 622 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 623 | << Property->getDeclName() << "setter" << inheritedName; |
| 624 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
| 625 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 626 | << Property->getDeclName() << "getter" << inheritedName; |
| 627 | |
| 628 | QualType LHSType = |
| 629 | Context.getCanonicalType(SuperProperty->getType()); |
| 630 | QualType RHSType = |
| 631 | Context.getCanonicalType(Property->getType()); |
| 632 | |
| 633 | if (!Context.typesAreCompatible(LHSType, RHSType)) { |
| 634 | // FIXME: Incorporate this test with typesAreCompatible. |
| 635 | if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType()) |
| 636 | if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false)) |
| 637 | return; |
| 638 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 639 | << Property->getType() << SuperProperty->getType() << inheritedName; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 644 | ObjCMethodDecl *GetterMethod, |
| 645 | SourceLocation Loc) { |
| 646 | if (GetterMethod && |
| 647 | GetterMethod->getResultType() != property->getType()) { |
| 648 | AssignConvertType result = Incompatible; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 649 | if (property->getType()->isObjCObjectPointerType()) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 650 | result = CheckAssignmentConstraints(GetterMethod->getResultType(), |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 651 | property->getType()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 652 | if (result != Compatible) { |
| 653 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
| 654 | << property->getDeclName() |
| 655 | << GetterMethod->getSelector(); |
| 656 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 657 | return true; |
| 658 | } |
| 659 | } |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 664 | /// declarations in base and its super class, if any, and issues |
| 665 | /// diagnostics in a variety of inconsistant situations. |
| 666 | /// |
| 667 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
| 668 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 669 | if (!SDecl) |
| 670 | return; |
| 671 | // FIXME: O(N^2) |
| 672 | for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), |
| 673 | E = SDecl->prop_end(); S != E; ++S) { |
| 674 | ObjCPropertyDecl *SuperPDecl = (*S); |
| 675 | // Does property in super class has declaration in current class? |
| 676 | for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), |
| 677 | E = IDecl->prop_end(); I != E; ++I) { |
| 678 | ObjCPropertyDecl *PDecl = (*I); |
| 679 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
| 680 | DiagnosePropertyMismatch(PDecl, SuperPDecl, |
| 681 | SDecl->getIdentifier()); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /// MatchOneProtocolPropertiesInClass - This routine goes thru the list |
| 687 | /// of properties declared in a protocol and compares their attribute against |
| 688 | /// the same property declared in the class or category. |
| 689 | void |
| 690 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, |
| 691 | ObjCProtocolDecl *PDecl) { |
| 692 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 693 | if (!IDecl) { |
| 694 | // Category |
| 695 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 696 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); |
| 697 | if (!CatDecl->IsClassExtension()) |
| 698 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 699 | E = PDecl->prop_end(); P != E; ++P) { |
| 700 | ObjCPropertyDecl *Pr = (*P); |
| 701 | ObjCCategoryDecl::prop_iterator CP, CE; |
| 702 | // Is this property already in category's list of properties? |
Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 703 | for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 704 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 705 | break; |
| 706 | if (CP != CE) |
| 707 | // Property protocol already exist in class. Diagnose any mismatch. |
| 708 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
| 709 | } |
| 710 | return; |
| 711 | } |
| 712 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 713 | E = PDecl->prop_end(); P != E; ++P) { |
| 714 | ObjCPropertyDecl *Pr = (*P); |
| 715 | ObjCInterfaceDecl::prop_iterator CP, CE; |
| 716 | // Is this property already in class's list of properties? |
| 717 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) |
| 718 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 719 | break; |
| 720 | if (CP != CE) |
| 721 | // Property protocol already exist in class. Diagnose any mismatch. |
| 722 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /// CompareProperties - This routine compares properties |
| 727 | /// declared in 'ClassOrProtocol' objects (which can be a class or an |
| 728 | /// inherited protocol with the list of properties for class/category 'CDecl' |
| 729 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 730 | void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) { |
| 731 | Decl *ClassDecl = ClassOrProtocol; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 732 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 733 | |
| 734 | if (!IDecl) { |
| 735 | // Category |
| 736 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 737 | assert (CatDecl && "CompareProperties"); |
| 738 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 739 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 740 | E = MDecl->protocol_end(); P != E; ++P) |
| 741 | // Match properties of category with those of protocol (*P) |
| 742 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 743 | |
| 744 | // Go thru the list of protocols for this category and recursively match |
| 745 | // their properties with those in the category. |
| 746 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), |
| 747 | E = CatDecl->protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 748 | CompareProperties(CatDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 749 | } else { |
| 750 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 751 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 752 | E = MD->protocol_end(); P != E; ++P) |
| 753 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 754 | } |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 759 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 760 | P = MDecl->all_referenced_protocol_begin(), |
| 761 | E = MDecl->all_referenced_protocol_end(); P != E; ++P) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 762 | // Match properties of class IDecl with those of protocol (*P). |
| 763 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 764 | |
| 765 | // Go thru the list of protocols for this class and recursively match |
| 766 | // their properties with those declared in the class. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 767 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 768 | P = IDecl->all_referenced_protocol_begin(), |
| 769 | E = IDecl->all_referenced_protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 770 | CompareProperties(IDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 771 | } else { |
| 772 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 773 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 774 | E = MD->protocol_end(); P != E; ++P) |
| 775 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 780 | /// for the property in the class and in its categories and implementations |
| 781 | /// |
| 782 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
| 783 | ObjCInterfaceDecl *IDecl) { |
| 784 | // by far the most common case. |
| 785 | if (!PDecl->isReadOnly()) |
| 786 | return false; |
| 787 | // Even if property is ready only, if interface has a user defined setter, |
| 788 | // it is not considered read only. |
| 789 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
| 790 | return false; |
| 791 | |
| 792 | // Main class has the property as 'readonly'. Must search |
| 793 | // through the category list to see if the property's |
| 794 | // attribute has been over-ridden to 'readwrite'. |
| 795 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); |
| 796 | Category; Category = Category->getNextClassCategory()) { |
| 797 | // Even if property is ready only, if a category has a user defined setter, |
| 798 | // it is not considered read only. |
| 799 | if (Category->getInstanceMethod(PDecl->getSetterName())) |
| 800 | return false; |
| 801 | ObjCPropertyDecl *P = |
| 802 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
| 803 | if (P && !P->isReadOnly()) |
| 804 | return false; |
| 805 | } |
| 806 | |
| 807 | // Also, check for definition of a setter method in the implementation if |
| 808 | // all else failed. |
| 809 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
| 810 | if (ObjCImplementationDecl *IMD = |
| 811 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
| 812 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
| 813 | return false; |
| 814 | } else if (ObjCCategoryImplDecl *CIMD = |
| 815 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 816 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
| 817 | return false; |
| 818 | } |
| 819 | } |
| 820 | // Lastly, look through the implementation (if one is in scope). |
| 821 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) |
| 822 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
| 823 | return false; |
| 824 | // If all fails, look at the super class. |
| 825 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 826 | return isPropertyReadonly(PDecl, SIDecl); |
| 827 | return true; |
| 828 | } |
| 829 | |
| 830 | /// CollectImmediateProperties - This routine collects all properties in |
| 831 | /// the class and its conforming protocols; but not those it its super class. |
| 832 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 833 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap, |
| 834 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 835 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 836 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 837 | E = IDecl->prop_end(); P != E; ++P) { |
| 838 | ObjCPropertyDecl *Prop = (*P); |
| 839 | PropMap[Prop->getIdentifier()] = Prop; |
| 840 | } |
| 841 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 842 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 843 | PI = IDecl->all_referenced_protocol_begin(), |
| 844 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 845 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 846 | } |
| 847 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 848 | if (!CATDecl->IsClassExtension()) |
| 849 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), |
| 850 | E = CATDecl->prop_end(); P != E; ++P) { |
| 851 | ObjCPropertyDecl *Prop = (*P); |
| 852 | PropMap[Prop->getIdentifier()] = Prop; |
| 853 | } |
| 854 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 855 | for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 856 | E = CATDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 857 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 858 | } |
| 859 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 860 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 861 | E = PDecl->prop_end(); P != E; ++P) { |
| 862 | ObjCPropertyDecl *Prop = (*P); |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 863 | ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; |
| 864 | // Exclude property for protocols which conform to class's super-class, |
| 865 | // as super-class has to implement the property. |
| 866 | if (!PropertyFromSuper || PropertyFromSuper != Prop) { |
| 867 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; |
| 868 | if (!PropEntry) |
| 869 | PropEntry = Prop; |
| 870 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 871 | } |
| 872 | // scan through protocol's protocols. |
| 873 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 874 | E = PDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 875 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 879 | /// CollectClassPropertyImplementations - This routine collects list of |
| 880 | /// properties to be implemented in the class. This includes, class's |
| 881 | /// and its conforming protocols' properties. |
| 882 | static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl, |
| 883 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 884 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 885 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 886 | E = IDecl->prop_end(); P != E; ++P) { |
| 887 | ObjCPropertyDecl *Prop = (*P); |
| 888 | PropMap[Prop->getIdentifier()] = Prop; |
| 889 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 890 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 891 | PI = IDecl->all_referenced_protocol_begin(), |
| 892 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 893 | CollectClassPropertyImplementations((*PI), PropMap); |
| 894 | } |
| 895 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 896 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 897 | E = PDecl->prop_end(); P != E; ++P) { |
| 898 | ObjCPropertyDecl *Prop = (*P); |
| 899 | PropMap[Prop->getIdentifier()] = Prop; |
| 900 | } |
| 901 | // scan through protocol's protocols. |
| 902 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 903 | E = PDecl->protocol_end(); PI != E; ++PI) |
| 904 | CollectClassPropertyImplementations((*PI), PropMap); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /// CollectSuperClassPropertyImplementations - This routine collects list of |
| 909 | /// properties to be implemented in super class(s) and also coming from their |
| 910 | /// conforming protocols. |
| 911 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
| 912 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 913 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
| 914 | while (SDecl) { |
| 915 | CollectClassPropertyImplementations(SDecl, PropMap); |
| 916 | SDecl = SDecl->getSuperClass(); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 921 | /// LookupPropertyDecl - Looks up a property in the current class and all |
| 922 | /// its protocols. |
| 923 | ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl, |
| 924 | IdentifierInfo *II) { |
| 925 | if (const ObjCInterfaceDecl *IDecl = |
| 926 | dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 927 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 928 | E = IDecl->prop_end(); P != E; ++P) { |
| 929 | ObjCPropertyDecl *Prop = (*P); |
| 930 | if (Prop->getIdentifier() == II) |
| 931 | return Prop; |
| 932 | } |
| 933 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 934 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 935 | PI = IDecl->all_referenced_protocol_begin(), |
| 936 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 937 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 938 | if (Prop) |
| 939 | return Prop; |
| 940 | } |
| 941 | } |
| 942 | else if (const ObjCProtocolDecl *PDecl = |
| 943 | dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 944 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 945 | E = PDecl->prop_end(); P != E; ++P) { |
| 946 | ObjCPropertyDecl *Prop = (*P); |
| 947 | if (Prop->getIdentifier() == II) |
| 948 | return Prop; |
| 949 | } |
| 950 | // scan through protocol's protocols. |
| 951 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 952 | E = PDecl->protocol_end(); PI != E; ++PI) { |
| 953 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 954 | if (Prop) |
| 955 | return Prop; |
| 956 | } |
| 957 | } |
| 958 | return 0; |
| 959 | } |
| 960 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 961 | /// DefaultSynthesizeProperties - This routine default synthesizes all |
| 962 | /// properties which must be synthesized in class's @implementation. |
| 963 | void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl, |
| 964 | ObjCInterfaceDecl *IDecl) { |
| 965 | |
| 966 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
| 967 | CollectClassPropertyImplementations(IDecl, PropMap); |
| 968 | if (PropMap.empty()) |
| 969 | return; |
| 970 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; |
| 971 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 972 | |
| 973 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 974 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 975 | ObjCPropertyDecl *Prop = P->second; |
| 976 | // If property to be implemented in the super class, ignore. |
| 977 | if (SuperPropMap[Prop->getIdentifier()]) |
| 978 | continue; |
| 979 | // Is there a matching propery synthesize/dynamic? |
| 980 | if (Prop->isInvalidDecl() || |
| 981 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 982 | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) |
| 983 | continue; |
Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 984 | // Property may have been synthesized by user. |
| 985 | if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) |
| 986 | continue; |
Fariborz Jahanian | 95f1b86 | 2010-08-25 00:31:58 +0000 | [diff] [blame] | 987 | if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { |
| 988 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) |
| 989 | continue; |
| 990 | if (IMPDecl->getInstanceMethod(Prop->getSetterName())) |
| 991 | continue; |
| 992 | } |
Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 993 | |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 994 | |
| 995 | // We use invalid SourceLocations for the synthesized ivars since they |
| 996 | // aren't really synthesized at a particular location; they just exist. |
| 997 | // Saying that they are located at the @implementation isn't really going |
| 998 | // to help users. |
| 999 | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), |
| 1000 | true,IMPDecl, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1001 | Prop->getIdentifier(), Prop->getIdentifier(), |
| 1002 | SourceLocation()); |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1003 | } |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1004 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1005 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1006 | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1007 | ObjCContainerDecl *CDecl, |
| 1008 | const llvm::DenseSet<Selector>& InsMap) { |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1009 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; |
| 1010 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
| 1011 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1012 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1013 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1014 | CollectImmediateProperties(CDecl, PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1015 | if (PropMap.empty()) |
| 1016 | return; |
| 1017 | |
| 1018 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 1019 | for (ObjCImplDecl::propimpl_iterator |
| 1020 | I = IMPDecl->propimpl_begin(), |
| 1021 | EI = IMPDecl->propimpl_end(); I != EI; ++I) |
| 1022 | PropImplMap.insert((*I)->getPropertyDecl()); |
| 1023 | |
| 1024 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 1025 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1026 | ObjCPropertyDecl *Prop = P->second; |
| 1027 | // Is there a matching propery synthesize/dynamic? |
| 1028 | if (Prop->isInvalidDecl() || |
| 1029 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 1030 | PropImplMap.count(Prop)) |
| 1031 | continue; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1032 | if (!InsMap.count(Prop->getGetterName())) { |
| 1033 | Diag(Prop->getLocation(), |
| 1034 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1035 | diag::warn_setter_getter_impl_required_in_category : |
| 1036 | diag::warn_setter_getter_impl_required) |
| 1037 | << Prop->getDeclName() << Prop->getGetterName(); |
| 1038 | Diag(IMPDecl->getLocation(), |
| 1039 | diag::note_property_impl_required); |
| 1040 | } |
| 1041 | |
| 1042 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { |
| 1043 | Diag(Prop->getLocation(), |
| 1044 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1045 | diag::warn_setter_getter_impl_required_in_category : |
| 1046 | diag::warn_setter_getter_impl_required) |
| 1047 | << Prop->getDeclName() << Prop->getSetterName(); |
| 1048 | Diag(IMPDecl->getLocation(), |
| 1049 | diag::note_property_impl_required); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | void |
| 1055 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
| 1056 | ObjCContainerDecl* IDecl) { |
| 1057 | // Rules apply in non-GC mode only |
| 1058 | if (getLangOptions().getGCMode() != LangOptions::NonGC) |
| 1059 | return; |
| 1060 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), |
| 1061 | E = IDecl->prop_end(); |
| 1062 | I != E; ++I) { |
| 1063 | ObjCPropertyDecl *Property = (*I); |
| 1064 | unsigned Attributes = Property->getPropertyAttributes(); |
| 1065 | // We only care about readwrite atomic property. |
| 1066 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 1067 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1068 | continue; |
| 1069 | if (const ObjCPropertyImplDecl *PIDecl |
| 1070 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { |
| 1071 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1072 | continue; |
| 1073 | ObjCMethodDecl *GetterMethod = |
| 1074 | IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1075 | ObjCMethodDecl *SetterMethod = |
| 1076 | IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1077 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 1078 | SourceLocation MethodLoc = |
| 1079 | (GetterMethod ? GetterMethod->getLocation() |
| 1080 | : SetterMethod->getLocation()); |
| 1081 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
| 1082 | << Property->getIdentifier(); |
| 1083 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1089 | /// AddPropertyAttrs - Propagates attributes from a property to the |
| 1090 | /// implicitly-declared getter or setter for that property. |
| 1091 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
| 1092 | ObjCPropertyDecl *Property) { |
| 1093 | // Should we just clone all attributes over? |
| 1094 | if (DeprecatedAttr *A = Property->getAttr<DeprecatedAttr>()) |
| 1095 | PropertyMethod->addAttr(A->clone(S.Context)); |
| 1096 | if (UnavailableAttr *A = Property->getAttr<UnavailableAttr>()) |
| 1097 | PropertyMethod->addAttr(A->clone(S.Context)); |
| 1098 | } |
| 1099 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1100 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 1101 | /// have the property type and issue diagnostics if they don't. |
| 1102 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1103 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1104 | /// methods is the "right" thing to do. |
| 1105 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1106 | ObjCContainerDecl *CD, |
| 1107 | ObjCPropertyDecl *redeclaredProperty, |
| 1108 | ObjCContainerDecl *lexicalDC) { |
| 1109 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1110 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 1111 | |
| 1112 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
| 1113 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
| 1114 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
| 1115 | property->getLocation()); |
| 1116 | |
| 1117 | if (SetterMethod) { |
| 1118 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1119 | property->getPropertyAttributes(); |
| 1120 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && |
| 1121 | Context.getCanonicalType(SetterMethod->getResultType()) != |
| 1122 | Context.VoidTy) |
| 1123 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
| 1124 | if (SetterMethod->param_size() != 1 || |
| 1125 | ((*SetterMethod->param_begin())->getType() != property->getType())) { |
| 1126 | Diag(property->getLocation(), |
| 1127 | diag::warn_accessor_property_type_mismatch) |
| 1128 | << property->getDeclName() |
| 1129 | << SetterMethod->getSelector(); |
| 1130 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | // Synthesize getter/setter methods if none exist. |
| 1135 | // Find the default getter and if one not found, add one. |
| 1136 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 1137 | // synthesize these methods unless the user explicitly provided prototypes |
| 1138 | // (which is odd, but allowed). Sema should be typechecking that the |
| 1139 | // declarations jive in that situation (which it is not currently). |
| 1140 | if (!GetterMethod) { |
| 1141 | // No instance method of same name as property getter name was found. |
| 1142 | // Declare a getter method and add it to the list of methods |
| 1143 | // for this class. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1144 | SourceLocation Loc = redeclaredProperty ? |
| 1145 | redeclaredProperty->getLocation() : |
| 1146 | property->getLocation(); |
| 1147 | |
| 1148 | GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, |
| 1149 | property->getGetterName(), |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1150 | property->getType(), 0, CD, true, false, true, |
| 1151 | false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1152 | (property->getPropertyImplementation() == |
| 1153 | ObjCPropertyDecl::Optional) ? |
| 1154 | ObjCMethodDecl::Optional : |
| 1155 | ObjCMethodDecl::Required); |
| 1156 | CD->addDecl(GetterMethod); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1157 | |
| 1158 | AddPropertyAttrs(*this, GetterMethod, property); |
| 1159 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1160 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1161 | // and the real context should be the same. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1162 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1163 | GetterMethod->setLexicalDeclContext(lexicalDC); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1164 | } else |
| 1165 | // A user declared getter will be synthesize when @synthesize of |
| 1166 | // the property with the same name is seen in the @implementation |
| 1167 | GetterMethod->setSynthesized(true); |
| 1168 | property->setGetterMethodDecl(GetterMethod); |
| 1169 | |
| 1170 | // Skip setter if property is read-only. |
| 1171 | if (!property->isReadOnly()) { |
| 1172 | // Find the default setter and if one not found, add one. |
| 1173 | if (!SetterMethod) { |
| 1174 | // No instance method of same name as property setter name was found. |
| 1175 | // Declare a setter method and add it to the list of methods |
| 1176 | // for this class. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1177 | SourceLocation Loc = redeclaredProperty ? |
| 1178 | redeclaredProperty->getLocation() : |
| 1179 | property->getLocation(); |
| 1180 | |
| 1181 | SetterMethod = |
| 1182 | ObjCMethodDecl::Create(Context, Loc, Loc, |
| 1183 | property->getSetterName(), Context.VoidTy, 0, |
| 1184 | CD, true, false, true, false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1185 | (property->getPropertyImplementation() == |
| 1186 | ObjCPropertyDecl::Optional) ? |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1187 | ObjCMethodDecl::Optional : |
| 1188 | ObjCMethodDecl::Required); |
| 1189 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1190 | // Invent the arguments for the setter. We don't bother making a |
| 1191 | // nice name for the argument. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1192 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1193 | property->getIdentifier(), |
| 1194 | property->getType(), |
| 1195 | /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1196 | SC_None, |
| 1197 | SC_None, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1198 | 0); |
Fariborz Jahanian | 4ecb25f | 2010-04-09 15:40:42 +0000 | [diff] [blame] | 1199 | SetterMethod->setMethodParams(Context, &Argument, 1, 1); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1200 | |
| 1201 | AddPropertyAttrs(*this, SetterMethod, property); |
| 1202 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1203 | CD->addDecl(SetterMethod); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1204 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1205 | // and the real context should be the same. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1206 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1207 | SetterMethod->setLexicalDeclContext(lexicalDC); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1208 | } else |
| 1209 | // A user declared setter will be synthesize when @synthesize of |
| 1210 | // the property with the same name is seen in the @implementation |
| 1211 | SetterMethod->setSynthesized(true); |
| 1212 | property->setSetterMethodDecl(SetterMethod); |
| 1213 | } |
| 1214 | // Add any synthesized methods to the global pool. This allows us to |
| 1215 | // handle the following, which is supported by GCC (and part of the design). |
| 1216 | // |
| 1217 | // @interface Foo |
| 1218 | // @property double bar; |
| 1219 | // @end |
| 1220 | // |
| 1221 | // void thisIsUnfortunate() { |
| 1222 | // id foo; |
| 1223 | // double bar = [foo bar]; |
| 1224 | // } |
| 1225 | // |
| 1226 | if (GetterMethod) |
| 1227 | AddInstanceMethodToGlobalPool(GetterMethod); |
| 1228 | if (SetterMethod) |
| 1229 | AddInstanceMethodToGlobalPool(SetterMethod); |
| 1230 | } |
| 1231 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1232 | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1233 | SourceLocation Loc, |
| 1234 | unsigned &Attributes) { |
| 1235 | // FIXME: Improve the reported location. |
Ted Kremenek | 5fcd52a | 2010-04-05 22:39:42 +0000 | [diff] [blame] | 1236 | if (!PDecl) |
| 1237 | return; |
| 1238 | |
| 1239 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1240 | QualType PropertyTy = PropertyDecl->getType(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1241 | |
| 1242 | // readonly and readwrite/assign/retain/copy conflict. |
| 1243 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1244 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 1245 | ObjCDeclSpec::DQ_PR_assign | |
| 1246 | ObjCDeclSpec::DQ_PR_copy | |
| 1247 | ObjCDeclSpec::DQ_PR_retain))) { |
| 1248 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
| 1249 | "readwrite" : |
| 1250 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
| 1251 | "assign" : |
| 1252 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
| 1253 | "copy" : "retain"; |
| 1254 | |
| 1255 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
| 1256 | diag::err_objc_property_attr_mutually_exclusive : |
| 1257 | diag::warn_objc_property_attr_mutually_exclusive) |
| 1258 | << "readonly" << which; |
| 1259 | } |
| 1260 | |
| 1261 | // Check for copy or retain on non-object types. |
| 1262 | if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && |
| 1263 | !PropertyTy->isObjCObjectPointerType() && |
| 1264 | !PropertyTy->isBlockPointerType() && |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1265 | !Context.isObjCNSObjectType(PropertyTy) && |
| 1266 | !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1267 | Diag(Loc, diag::err_objc_property_requires_object) |
| 1268 | << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); |
| 1269 | Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); |
| 1270 | } |
| 1271 | |
| 1272 | // Check for more than one of { assign, copy, retain }. |
| 1273 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 1274 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1275 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1276 | << "assign" << "copy"; |
| 1277 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
| 1278 | } |
| 1279 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1280 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1281 | << "assign" << "retain"; |
| 1282 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1283 | } |
| 1284 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1285 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1286 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1287 | << "copy" << "retain"; |
| 1288 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | // Warn if user supplied no assignment attribute, property is |
| 1293 | // readwrite, and this is an object type. |
| 1294 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
| 1295 | ObjCDeclSpec::DQ_PR_retain)) && |
| 1296 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1297 | PropertyTy->isObjCObjectPointerType()) { |
| 1298 | // Skip this warning in gc-only mode. |
| 1299 | if (getLangOptions().getGCMode() != LangOptions::GCOnly) |
| 1300 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
| 1301 | |
| 1302 | // If non-gc code warn that this is likely inappropriate. |
| 1303 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 1304 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
| 1305 | |
| 1306 | // FIXME: Implement warning dependent on NSCopying being |
| 1307 | // implemented. See also: |
| 1308 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 1309 | // (please trim this list while you are at it). |
| 1310 | } |
| 1311 | |
| 1312 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 1313 | && getLangOptions().getGCMode() == LangOptions::GCOnly |
| 1314 | && PropertyTy->isBlockPointerType()) |
| 1315 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
| 1316 | } |