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