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