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