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