| 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) { | 
| Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 411 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc, | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 412 | PropertyIvar, PropType, /*Dinfo=*/0, | 
| Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 413 | ObjCIvarDecl::Private, | 
| Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 414 | (Expr *)0, true); | 
| Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 415 | ClassImpDecl->addDecl(Ivar); | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 416 | IDecl->makeDeclVisibleInContext(Ivar, false); | 
|  | 417 | property->setPropertyIvarDecl(Ivar); | 
|  | 418 |  | 
|  | 419 | if (!getLangOptions().ObjCNonFragileABI) | 
|  | 420 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; | 
|  | 421 | // Note! I deliberately want it to fall thru so, we have a | 
|  | 422 | // a property implementation and to avoid future warnings. | 
|  | 423 | } else if (getLangOptions().ObjCNonFragileABI && | 
|  | 424 | ClassDeclared != IDecl) { | 
|  | 425 | Diag(PropertyLoc, diag::error_ivar_in_superclass_use) | 
|  | 426 | << property->getDeclName() << Ivar->getDeclName() | 
|  | 427 | << ClassDeclared->getDeclName(); | 
|  | 428 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) | 
| Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 429 | << Ivar << Ivar->getName(); | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 430 | // Note! I deliberately want it to fall thru so more errors are caught. | 
|  | 431 | } | 
|  | 432 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); | 
|  | 433 |  | 
|  | 434 | // Check that type of property and its ivar are type compatible. | 
|  | 435 | if (PropType != IvarType) { | 
| Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 436 | bool compat = false; | 
|  | 437 | if (isa<ObjCObjectPointerType>(PropType) | 
|  | 438 | && isa<ObjCObjectPointerType>(IvarType)) | 
|  | 439 | compat = | 
|  | 440 | Context.canAssignObjCInterfaces( | 
|  | 441 | PropType->getAs<ObjCObjectPointerType>(), | 
|  | 442 | IvarType->getAs<ObjCObjectPointerType>()); | 
| John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 443 | else | 
|  | 444 | compat = (CheckAssignmentConstraints(PropType, IvarType) | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 445 | == Compatible); | 
| Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 446 | if (!compat) { | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 447 | Diag(PropertyLoc, diag::error_property_ivar_type) | 
| Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 448 | << property->getDeclName() << PropType | 
|  | 449 | << Ivar->getDeclName() << IvarType; | 
|  | 450 | Diag(Ivar->getLocation(), diag::note_ivar_decl); | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 451 | // Note! I deliberately want it to fall thru so, we have a | 
|  | 452 | // a property implementation and to avoid future warnings. | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | // FIXME! Rules for properties are somewhat different that those | 
|  | 456 | // for assignments. Use a new routine to consolidate all cases; | 
|  | 457 | // specifically for property redeclarations as well as for ivars. | 
|  | 458 | QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); | 
|  | 459 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); | 
|  | 460 | if (lhsType != rhsType && | 
|  | 461 | lhsType->isArithmeticType()) { | 
|  | 462 | Diag(PropertyLoc, diag::error_property_ivar_type) | 
| Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 463 | << property->getDeclName() << PropType | 
|  | 464 | << Ivar->getDeclName() << IvarType; | 
|  | 465 | Diag(Ivar->getLocation(), diag::note_ivar_decl); | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 466 | // Fall thru - see previous comment | 
|  | 467 | } | 
|  | 468 | // __weak is explicit. So it works on Canonical type. | 
|  | 469 | if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && | 
|  | 470 | getLangOptions().getGCMode() != LangOptions::NonGC) { | 
|  | 471 | Diag(PropertyLoc, diag::error_weak_property) | 
|  | 472 | << property->getDeclName() << Ivar->getDeclName(); | 
|  | 473 | // Fall thru - see previous comment | 
|  | 474 | } | 
|  | 475 | if ((property->getType()->isObjCObjectPointerType() || | 
|  | 476 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && | 
|  | 477 | getLangOptions().getGCMode() != LangOptions::NonGC) { | 
|  | 478 | Diag(PropertyLoc, diag::error_strong_property) | 
|  | 479 | << property->getDeclName() << Ivar->getDeclName(); | 
|  | 480 | // Fall thru - see previous comment | 
|  | 481 | } | 
|  | 482 | } | 
|  | 483 | } else if (PropertyIvar) | 
|  | 484 | // @dynamic | 
|  | 485 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); | 
|  | 486 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); | 
|  | 487 | ObjCPropertyImplDecl *PIDecl = | 
|  | 488 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, | 
|  | 489 | property, | 
|  | 490 | (Synthesize ? | 
|  | 491 | ObjCPropertyImplDecl::Synthesize | 
|  | 492 | : ObjCPropertyImplDecl::Dynamic), | 
| Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 493 | Ivar, PropertyIvarLoc); | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 494 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { | 
|  | 495 | getterMethod->createImplicitParams(Context, IDecl); | 
| Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 496 | if (getLangOptions().CPlusPlus && Synthesize && | 
|  | 497 | Ivar->getType()->isRecordType()) { | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 498 | // For Objective-C++, need to synthesize the AST for the IVAR object to be | 
|  | 499 | // returned by the getter as it must conform to C++'s copy-return rules. | 
|  | 500 | // FIXME. Eventually we want to do this for Objective-C as well. | 
|  | 501 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); | 
|  | 502 | DeclRefExpr *SelfExpr = | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 503 | new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(), | 
|  | 504 | VK_RValue, SourceLocation()); | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 505 | Expr *IvarRefExpr = | 
|  | 506 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, | 
|  | 507 | SelfExpr, true, true); | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 508 | ExprResult Res = | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 509 | PerformCopyInitialization(InitializedEntity::InitializeResult( | 
| Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 510 | SourceLocation(), | 
|  | 511 | getterMethod->getResultType(), | 
|  | 512 | /*NRVO=*/false), | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 513 | SourceLocation(), | 
|  | 514 | Owned(IvarRefExpr)); | 
|  | 515 | if (!Res.isInvalid()) { | 
|  | 516 | Expr *ResExpr = Res.takeAs<Expr>(); | 
|  | 517 | if (ResExpr) | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 518 | ResExpr = MaybeCreateExprWithCleanups(ResExpr); | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 519 | PIDecl->setGetterCXXConstructor(ResExpr); | 
|  | 520 | } | 
|  | 521 | } | 
|  | 522 | } | 
|  | 523 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { | 
|  | 524 | setterMethod->createImplicitParams(Context, IDecl); | 
| Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 525 | if (getLangOptions().CPlusPlus && Synthesize | 
|  | 526 | && Ivar->getType()->isRecordType()) { | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 527 | // FIXME. Eventually we want to do this for Objective-C as well. | 
|  | 528 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); | 
|  | 529 | DeclRefExpr *SelfExpr = | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 530 | new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(), | 
|  | 531 | VK_RValue, SourceLocation()); | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 532 | Expr *lhs = | 
|  | 533 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, | 
|  | 534 | SelfExpr, true, true); | 
|  | 535 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); | 
|  | 536 | ParmVarDecl *Param = (*P); | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 537 | Expr *rhs = new (Context) DeclRefExpr(Param, Param->getType(), | 
|  | 538 | VK_LValue, SourceLocation()); | 
| Fariborz Jahanian | fa43239 | 2010-10-14 21:30:10 +0000 | [diff] [blame] | 539 | ExprResult Res = BuildBinOp(S, lhs->getLocEnd(), | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 540 | BO_Assign, lhs, rhs); | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 541 | PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); | 
|  | 542 | } | 
|  | 543 | } | 
|  | 544 |  | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 545 | if (IC) { | 
|  | 546 | if (Synthesize) | 
|  | 547 | if (ObjCPropertyImplDecl *PPIDecl = | 
|  | 548 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { | 
|  | 549 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) | 
|  | 550 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() | 
|  | 551 | << PropertyIvar; | 
|  | 552 | Diag(PPIDecl->getLocation(), diag::note_previous_use); | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | if (ObjCPropertyImplDecl *PPIDecl | 
|  | 556 | = IC->FindPropertyImplDecl(PropertyId)) { | 
|  | 557 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; | 
|  | 558 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); | 
| John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 559 | return 0; | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 560 | } | 
|  | 561 | IC->addPropertyImplementation(PIDecl); | 
| Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 562 | if (getLangOptions().ObjCNonFragileABI2) { | 
|  | 563 | // Diagnose if an ivar was lazily synthesdized due to a previous | 
|  | 564 | // use and if 1) property is @dynamic or 2) property is synthesized | 
| Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 565 | // but it requires an ivar of different name. | 
| Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 566 | ObjCInterfaceDecl *ClassDeclared; | 
|  | 567 | ObjCIvarDecl *Ivar = 0; | 
|  | 568 | if (!Synthesize) | 
|  | 569 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); | 
|  | 570 | else { | 
|  | 571 | if (PropertyIvar && PropertyIvar != PropertyId) | 
|  | 572 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); | 
|  | 573 | } | 
| Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 574 | // Issue diagnostics only if Ivar belongs to current class. | 
|  | 575 | if (Ivar && Ivar->getSynthesize() && | 
|  | 576 | IC->getClassInterface() == ClassDeclared) { | 
| Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 577 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) | 
|  | 578 | << PropertyId; | 
|  | 579 | Ivar->setInvalidDecl(); | 
|  | 580 | } | 
|  | 581 | } | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 582 | } else { | 
|  | 583 | if (Synthesize) | 
|  | 584 | if (ObjCPropertyImplDecl *PPIDecl = | 
|  | 585 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { | 
|  | 586 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) | 
|  | 587 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() | 
|  | 588 | << PropertyIvar; | 
|  | 589 | Diag(PPIDecl->getLocation(), diag::note_previous_use); | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | if (ObjCPropertyImplDecl *PPIDecl = | 
|  | 593 | CatImplClass->FindPropertyImplDecl(PropertyId)) { | 
|  | 594 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; | 
|  | 595 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); | 
| John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 596 | return 0; | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 597 | } | 
|  | 598 | CatImplClass->addPropertyImplementation(PIDecl); | 
|  | 599 | } | 
|  | 600 |  | 
| John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 601 | return PIDecl; | 
| Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 602 | } | 
|  | 603 |  | 
|  | 604 | //===----------------------------------------------------------------------===// | 
|  | 605 | // Helper methods. | 
|  | 606 | //===----------------------------------------------------------------------===// | 
|  | 607 |  | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 608 | /// DiagnosePropertyMismatch - Compares two properties for their | 
|  | 609 | /// attributes and types and warns on a variety of inconsistencies. | 
|  | 610 | /// | 
|  | 611 | void | 
|  | 612 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, | 
|  | 613 | ObjCPropertyDecl *SuperProperty, | 
|  | 614 | const IdentifierInfo *inheritedName) { | 
|  | 615 | ObjCPropertyDecl::PropertyAttributeKind CAttr = | 
|  | 616 | Property->getPropertyAttributes(); | 
|  | 617 | ObjCPropertyDecl::PropertyAttributeKind SAttr = | 
|  | 618 | SuperProperty->getPropertyAttributes(); | 
|  | 619 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) | 
|  | 620 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) | 
|  | 621 | Diag(Property->getLocation(), diag::warn_readonly_property) | 
|  | 622 | << Property->getDeclName() << inheritedName; | 
|  | 623 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) | 
|  | 624 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) | 
|  | 625 | Diag(Property->getLocation(), diag::warn_property_attribute) | 
|  | 626 | << Property->getDeclName() << "copy" << inheritedName; | 
|  | 627 | else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) | 
|  | 628 | != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) | 
|  | 629 | Diag(Property->getLocation(), diag::warn_property_attribute) | 
|  | 630 | << Property->getDeclName() << "retain" << inheritedName; | 
|  | 631 |  | 
|  | 632 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) | 
|  | 633 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) | 
|  | 634 | Diag(Property->getLocation(), diag::warn_property_attribute) | 
|  | 635 | << Property->getDeclName() << "atomic" << inheritedName; | 
|  | 636 | if (Property->getSetterName() != SuperProperty->getSetterName()) | 
|  | 637 | Diag(Property->getLocation(), diag::warn_property_attribute) | 
|  | 638 | << Property->getDeclName() << "setter" << inheritedName; | 
|  | 639 | if (Property->getGetterName() != SuperProperty->getGetterName()) | 
|  | 640 | Diag(Property->getLocation(), diag::warn_property_attribute) | 
|  | 641 | << Property->getDeclName() << "getter" << inheritedName; | 
|  | 642 |  | 
|  | 643 | QualType LHSType = | 
|  | 644 | Context.getCanonicalType(SuperProperty->getType()); | 
|  | 645 | QualType RHSType = | 
|  | 646 | Context.getCanonicalType(Property->getType()); | 
|  | 647 |  | 
|  | 648 | if (!Context.typesAreCompatible(LHSType, RHSType)) { | 
|  | 649 | // FIXME: Incorporate this test with typesAreCompatible. | 
|  | 650 | if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType()) | 
|  | 651 | if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false)) | 
|  | 652 | return; | 
|  | 653 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) | 
|  | 654 | << Property->getType() << SuperProperty->getType() << inheritedName; | 
|  | 655 | } | 
|  | 656 | } | 
|  | 657 |  | 
|  | 658 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, | 
|  | 659 | ObjCMethodDecl *GetterMethod, | 
|  | 660 | SourceLocation Loc) { | 
|  | 661 | if (GetterMethod && | 
|  | 662 | GetterMethod->getResultType() != property->getType()) { | 
|  | 663 | AssignConvertType result = Incompatible; | 
| John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 664 | if (property->getType()->isObjCObjectPointerType()) | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 665 | result = CheckAssignmentConstraints(GetterMethod->getResultType(), | 
| John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 666 | property->getType()); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 667 | if (result != Compatible) { | 
|  | 668 | Diag(Loc, diag::warn_accessor_property_type_mismatch) | 
|  | 669 | << property->getDeclName() | 
|  | 670 | << GetterMethod->getSelector(); | 
|  | 671 | Diag(GetterMethod->getLocation(), diag::note_declared_at); | 
|  | 672 | return true; | 
|  | 673 | } | 
|  | 674 | } | 
|  | 675 | return false; | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | /// ComparePropertiesInBaseAndSuper - This routine compares property | 
|  | 679 | /// declarations in base and its super class, if any, and issues | 
|  | 680 | /// diagnostics in a variety of inconsistant situations. | 
|  | 681 | /// | 
|  | 682 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { | 
|  | 683 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); | 
|  | 684 | if (!SDecl) | 
|  | 685 | return; | 
|  | 686 | // FIXME: O(N^2) | 
|  | 687 | for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), | 
|  | 688 | E = SDecl->prop_end(); S != E; ++S) { | 
|  | 689 | ObjCPropertyDecl *SuperPDecl = (*S); | 
|  | 690 | // Does property in super class has declaration in current class? | 
|  | 691 | for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), | 
|  | 692 | E = IDecl->prop_end(); I != E; ++I) { | 
|  | 693 | ObjCPropertyDecl *PDecl = (*I); | 
|  | 694 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) | 
|  | 695 | DiagnosePropertyMismatch(PDecl, SuperPDecl, | 
|  | 696 | SDecl->getIdentifier()); | 
|  | 697 | } | 
|  | 698 | } | 
|  | 699 | } | 
|  | 700 |  | 
|  | 701 | /// MatchOneProtocolPropertiesInClass - This routine goes thru the list | 
|  | 702 | /// of properties declared in a protocol and compares their attribute against | 
|  | 703 | /// the same property declared in the class or category. | 
|  | 704 | void | 
|  | 705 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, | 
|  | 706 | ObjCProtocolDecl *PDecl) { | 
|  | 707 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); | 
|  | 708 | if (!IDecl) { | 
|  | 709 | // Category | 
|  | 710 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); | 
|  | 711 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); | 
|  | 712 | if (!CatDecl->IsClassExtension()) | 
|  | 713 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), | 
|  | 714 | E = PDecl->prop_end(); P != E; ++P) { | 
|  | 715 | ObjCPropertyDecl *Pr = (*P); | 
|  | 716 | ObjCCategoryDecl::prop_iterator CP, CE; | 
|  | 717 | // Is this property already in  category's list of properties? | 
| Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 718 | for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP) | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 719 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) | 
|  | 720 | break; | 
|  | 721 | if (CP != CE) | 
|  | 722 | // Property protocol already exist in class. Diagnose any mismatch. | 
|  | 723 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); | 
|  | 724 | } | 
|  | 725 | return; | 
|  | 726 | } | 
|  | 727 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), | 
|  | 728 | E = PDecl->prop_end(); P != E; ++P) { | 
|  | 729 | ObjCPropertyDecl *Pr = (*P); | 
|  | 730 | ObjCInterfaceDecl::prop_iterator CP, CE; | 
|  | 731 | // Is this property already in  class's list of properties? | 
|  | 732 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) | 
|  | 733 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) | 
|  | 734 | break; | 
|  | 735 | if (CP != CE) | 
|  | 736 | // Property protocol already exist in class. Diagnose any mismatch. | 
|  | 737 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); | 
|  | 738 | } | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 | /// CompareProperties - This routine compares properties | 
|  | 742 | /// declared in 'ClassOrProtocol' objects (which can be a class or an | 
|  | 743 | /// inherited protocol with the list of properties for class/category 'CDecl' | 
|  | 744 | /// | 
| John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 745 | void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) { | 
|  | 746 | Decl *ClassDecl = ClassOrProtocol; | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 747 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); | 
|  | 748 |  | 
|  | 749 | if (!IDecl) { | 
|  | 750 | // Category | 
|  | 751 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); | 
|  | 752 | assert (CatDecl && "CompareProperties"); | 
|  | 753 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { | 
|  | 754 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), | 
|  | 755 | E = MDecl->protocol_end(); P != E; ++P) | 
|  | 756 | // Match properties of category with those of protocol (*P) | 
|  | 757 | MatchOneProtocolPropertiesInClass(CatDecl, *P); | 
|  | 758 |  | 
|  | 759 | // Go thru the list of protocols for this category and recursively match | 
|  | 760 | // their properties with those in the category. | 
|  | 761 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), | 
|  | 762 | E = CatDecl->protocol_end(); P != E; ++P) | 
| John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 763 | CompareProperties(CatDecl, *P); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 764 | } else { | 
|  | 765 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); | 
|  | 766 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), | 
|  | 767 | E = MD->protocol_end(); P != E; ++P) | 
|  | 768 | MatchOneProtocolPropertiesInClass(CatDecl, *P); | 
|  | 769 | } | 
|  | 770 | return; | 
|  | 771 | } | 
|  | 772 |  | 
|  | 773 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 774 | for (ObjCInterfaceDecl::all_protocol_iterator | 
|  | 775 | P = MDecl->all_referenced_protocol_begin(), | 
|  | 776 | E = MDecl->all_referenced_protocol_end(); P != E; ++P) | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 777 | // Match properties of class IDecl with those of protocol (*P). | 
|  | 778 | MatchOneProtocolPropertiesInClass(IDecl, *P); | 
|  | 779 |  | 
|  | 780 | // Go thru the list of protocols for this class and recursively match | 
|  | 781 | // their properties with those declared in the class. | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 782 | for (ObjCInterfaceDecl::all_protocol_iterator | 
|  | 783 | P = IDecl->all_referenced_protocol_begin(), | 
|  | 784 | E = IDecl->all_referenced_protocol_end(); P != E; ++P) | 
| John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 785 | CompareProperties(IDecl, *P); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 786 | } else { | 
|  | 787 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); | 
|  | 788 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), | 
|  | 789 | E = MD->protocol_end(); P != E; ++P) | 
|  | 790 | MatchOneProtocolPropertiesInClass(IDecl, *P); | 
|  | 791 | } | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | /// isPropertyReadonly - Return true if property is readonly, by searching | 
|  | 795 | /// for the property in the class and in its categories and implementations | 
|  | 796 | /// | 
|  | 797 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, | 
|  | 798 | ObjCInterfaceDecl *IDecl) { | 
|  | 799 | // by far the most common case. | 
|  | 800 | if (!PDecl->isReadOnly()) | 
|  | 801 | return false; | 
|  | 802 | // Even if property is ready only, if interface has a user defined setter, | 
|  | 803 | // it is not considered read only. | 
|  | 804 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) | 
|  | 805 | return false; | 
|  | 806 |  | 
|  | 807 | // Main class has the property as 'readonly'. Must search | 
|  | 808 | // through the category list to see if the property's | 
|  | 809 | // attribute has been over-ridden to 'readwrite'. | 
|  | 810 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); | 
|  | 811 | Category; Category = Category->getNextClassCategory()) { | 
|  | 812 | // Even if property is ready only, if a category has a user defined setter, | 
|  | 813 | // it is not considered read only. | 
|  | 814 | if (Category->getInstanceMethod(PDecl->getSetterName())) | 
|  | 815 | return false; | 
|  | 816 | ObjCPropertyDecl *P = | 
|  | 817 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); | 
|  | 818 | if (P && !P->isReadOnly()) | 
|  | 819 | return false; | 
|  | 820 | } | 
|  | 821 |  | 
|  | 822 | // Also, check for definition of a setter method in the implementation if | 
|  | 823 | // all else failed. | 
|  | 824 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { | 
|  | 825 | if (ObjCImplementationDecl *IMD = | 
|  | 826 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { | 
|  | 827 | if (IMD->getInstanceMethod(PDecl->getSetterName())) | 
|  | 828 | return false; | 
|  | 829 | } else if (ObjCCategoryImplDecl *CIMD = | 
|  | 830 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { | 
|  | 831 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) | 
|  | 832 | return false; | 
|  | 833 | } | 
|  | 834 | } | 
|  | 835 | // Lastly, look through the implementation (if one is in scope). | 
|  | 836 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) | 
|  | 837 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) | 
|  | 838 | return false; | 
|  | 839 | // If all fails, look at the super class. | 
|  | 840 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) | 
|  | 841 | return isPropertyReadonly(PDecl, SIDecl); | 
|  | 842 | return true; | 
|  | 843 | } | 
|  | 844 |  | 
|  | 845 | /// CollectImmediateProperties - This routine collects all properties in | 
|  | 846 | /// the class and its conforming protocols; but not those it its super class. | 
|  | 847 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, | 
| Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 848 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap, | 
|  | 849 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) { | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 850 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { | 
|  | 851 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), | 
|  | 852 | E = IDecl->prop_end(); P != E; ++P) { | 
|  | 853 | ObjCPropertyDecl *Prop = (*P); | 
|  | 854 | PropMap[Prop->getIdentifier()] = Prop; | 
|  | 855 | } | 
|  | 856 | // scan through class's protocols. | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 857 | for (ObjCInterfaceDecl::all_protocol_iterator | 
|  | 858 | PI = IDecl->all_referenced_protocol_begin(), | 
|  | 859 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) | 
| Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 860 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 861 | } | 
|  | 862 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { | 
|  | 863 | if (!CATDecl->IsClassExtension()) | 
|  | 864 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), | 
|  | 865 | E = CATDecl->prop_end(); P != E; ++P) { | 
|  | 866 | ObjCPropertyDecl *Prop = (*P); | 
|  | 867 | PropMap[Prop->getIdentifier()] = Prop; | 
|  | 868 | } | 
|  | 869 | // scan through class's protocols. | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 870 | for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 871 | E = CATDecl->protocol_end(); PI != E; ++PI) | 
| Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 872 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 873 | } | 
|  | 874 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { | 
|  | 875 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), | 
|  | 876 | E = PDecl->prop_end(); P != E; ++P) { | 
|  | 877 | ObjCPropertyDecl *Prop = (*P); | 
| Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 878 | ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; | 
|  | 879 | // Exclude property for protocols which conform to class's super-class, | 
|  | 880 | // as super-class has to implement the property. | 
|  | 881 | if (!PropertyFromSuper || PropertyFromSuper != Prop) { | 
|  | 882 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; | 
|  | 883 | if (!PropEntry) | 
|  | 884 | PropEntry = Prop; | 
|  | 885 | } | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 886 | } | 
|  | 887 | // scan through protocol's protocols. | 
|  | 888 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), | 
|  | 889 | E = PDecl->protocol_end(); PI != E; ++PI) | 
| Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 890 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 891 | } | 
|  | 892 | } | 
|  | 893 |  | 
| Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 894 | /// CollectClassPropertyImplementations - This routine collects list of | 
|  | 895 | /// properties to be implemented in the class. This includes, class's | 
|  | 896 | /// and its conforming protocols' properties. | 
|  | 897 | static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl, | 
|  | 898 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { | 
|  | 899 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { | 
|  | 900 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), | 
|  | 901 | E = IDecl->prop_end(); P != E; ++P) { | 
|  | 902 | ObjCPropertyDecl *Prop = (*P); | 
|  | 903 | PropMap[Prop->getIdentifier()] = Prop; | 
|  | 904 | } | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 905 | for (ObjCInterfaceDecl::all_protocol_iterator | 
|  | 906 | PI = IDecl->all_referenced_protocol_begin(), | 
|  | 907 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) | 
| Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 908 | CollectClassPropertyImplementations((*PI), PropMap); | 
|  | 909 | } | 
|  | 910 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { | 
|  | 911 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), | 
|  | 912 | E = PDecl->prop_end(); P != E; ++P) { | 
|  | 913 | ObjCPropertyDecl *Prop = (*P); | 
|  | 914 | PropMap[Prop->getIdentifier()] = Prop; | 
|  | 915 | } | 
|  | 916 | // scan through protocol's protocols. | 
|  | 917 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), | 
|  | 918 | E = PDecl->protocol_end(); PI != E; ++PI) | 
|  | 919 | CollectClassPropertyImplementations((*PI), PropMap); | 
|  | 920 | } | 
|  | 921 | } | 
|  | 922 |  | 
|  | 923 | /// CollectSuperClassPropertyImplementations - This routine collects list of | 
|  | 924 | /// properties to be implemented in super class(s) and also coming from their | 
|  | 925 | /// conforming protocols. | 
|  | 926 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, | 
|  | 927 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { | 
|  | 928 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { | 
|  | 929 | while (SDecl) { | 
|  | 930 | CollectClassPropertyImplementations(SDecl, PropMap); | 
|  | 931 | SDecl = SDecl->getSuperClass(); | 
|  | 932 | } | 
|  | 933 | } | 
|  | 934 | } | 
|  | 935 |  | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 936 | /// LookupPropertyDecl - Looks up a property in the current class and all | 
|  | 937 | /// its protocols. | 
|  | 938 | ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl, | 
|  | 939 | IdentifierInfo *II) { | 
|  | 940 | if (const ObjCInterfaceDecl *IDecl = | 
|  | 941 | dyn_cast<ObjCInterfaceDecl>(CDecl)) { | 
|  | 942 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), | 
|  | 943 | E = IDecl->prop_end(); P != E; ++P) { | 
|  | 944 | ObjCPropertyDecl *Prop = (*P); | 
|  | 945 | if (Prop->getIdentifier() == II) | 
|  | 946 | return Prop; | 
|  | 947 | } | 
|  | 948 | // scan through class's protocols. | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 949 | for (ObjCInterfaceDecl::all_protocol_iterator | 
|  | 950 | PI = IDecl->all_referenced_protocol_begin(), | 
|  | 951 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) { | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 952 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); | 
|  | 953 | if (Prop) | 
|  | 954 | return Prop; | 
|  | 955 | } | 
|  | 956 | } | 
|  | 957 | else if (const ObjCProtocolDecl *PDecl = | 
|  | 958 | dyn_cast<ObjCProtocolDecl>(CDecl)) { | 
|  | 959 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), | 
|  | 960 | E = PDecl->prop_end(); P != E; ++P) { | 
|  | 961 | ObjCPropertyDecl *Prop = (*P); | 
|  | 962 | if (Prop->getIdentifier() == II) | 
|  | 963 | return Prop; | 
|  | 964 | } | 
|  | 965 | // scan through protocol's protocols. | 
|  | 966 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), | 
|  | 967 | E = PDecl->protocol_end(); PI != E; ++PI) { | 
|  | 968 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); | 
|  | 969 | if (Prop) | 
|  | 970 | return Prop; | 
|  | 971 | } | 
|  | 972 | } | 
|  | 973 | return 0; | 
|  | 974 | } | 
|  | 975 |  | 
| Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 976 | /// DefaultSynthesizeProperties - This routine default synthesizes all | 
|  | 977 | /// properties which must be synthesized in class's @implementation. | 
|  | 978 | void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl, | 
|  | 979 | ObjCInterfaceDecl *IDecl) { | 
|  | 980 |  | 
|  | 981 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; | 
|  | 982 | CollectClassPropertyImplementations(IDecl, PropMap); | 
|  | 983 | if (PropMap.empty()) | 
|  | 984 | return; | 
|  | 985 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; | 
|  | 986 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); | 
|  | 987 |  | 
|  | 988 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator | 
|  | 989 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { | 
|  | 990 | ObjCPropertyDecl *Prop = P->second; | 
|  | 991 | // If property to be implemented in the super class, ignore. | 
|  | 992 | if (SuperPropMap[Prop->getIdentifier()]) | 
|  | 993 | continue; | 
|  | 994 | // Is there a matching propery synthesize/dynamic? | 
|  | 995 | if (Prop->isInvalidDecl() || | 
|  | 996 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || | 
|  | 997 | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) | 
|  | 998 | continue; | 
| Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 999 | // Property may have been synthesized by user. | 
|  | 1000 | if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) | 
|  | 1001 | continue; | 
| Fariborz Jahanian | 95f1b86 | 2010-08-25 00:31:58 +0000 | [diff] [blame] | 1002 | if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { | 
|  | 1003 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) | 
|  | 1004 | continue; | 
|  | 1005 | if (IMPDecl->getInstanceMethod(Prop->getSetterName())) | 
|  | 1006 | continue; | 
|  | 1007 | } | 
| Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 1008 |  | 
| Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1009 |  | 
|  | 1010 | // We use invalid SourceLocations for the synthesized ivars since they | 
|  | 1011 | // aren't really synthesized at a particular location; they just exist. | 
|  | 1012 | // Saying that they are located at the @implementation isn't really going | 
|  | 1013 | // to help users. | 
|  | 1014 | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), | 
|  | 1015 | true,IMPDecl, | 
| Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1016 | Prop->getIdentifier(), Prop->getIdentifier(), | 
|  | 1017 | SourceLocation()); | 
| Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1018 | } | 
| Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1019 | } | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1020 |  | 
| Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1021 | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1022 | ObjCContainerDecl *CDecl, | 
|  | 1023 | const llvm::DenseSet<Selector>& InsMap) { | 
| Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1024 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; | 
|  | 1025 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) | 
|  | 1026 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); | 
|  | 1027 |  | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1028 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; | 
| Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1029 | CollectImmediateProperties(CDecl, PropMap, SuperPropMap); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1030 | if (PropMap.empty()) | 
|  | 1031 | return; | 
|  | 1032 |  | 
|  | 1033 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; | 
|  | 1034 | for (ObjCImplDecl::propimpl_iterator | 
|  | 1035 | I = IMPDecl->propimpl_begin(), | 
|  | 1036 | EI = IMPDecl->propimpl_end(); I != EI; ++I) | 
|  | 1037 | PropImplMap.insert((*I)->getPropertyDecl()); | 
|  | 1038 |  | 
|  | 1039 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator | 
|  | 1040 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { | 
|  | 1041 | ObjCPropertyDecl *Prop = P->second; | 
|  | 1042 | // Is there a matching propery synthesize/dynamic? | 
|  | 1043 | if (Prop->isInvalidDecl() || | 
|  | 1044 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || | 
|  | 1045 | PropImplMap.count(Prop)) | 
|  | 1046 | continue; | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1047 | if (!InsMap.count(Prop->getGetterName())) { | 
|  | 1048 | Diag(Prop->getLocation(), | 
|  | 1049 | isa<ObjCCategoryDecl>(CDecl) ? | 
|  | 1050 | diag::warn_setter_getter_impl_required_in_category : | 
|  | 1051 | diag::warn_setter_getter_impl_required) | 
|  | 1052 | << Prop->getDeclName() << Prop->getGetterName(); | 
|  | 1053 | Diag(IMPDecl->getLocation(), | 
|  | 1054 | diag::note_property_impl_required); | 
|  | 1055 | } | 
|  | 1056 |  | 
|  | 1057 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { | 
|  | 1058 | Diag(Prop->getLocation(), | 
|  | 1059 | isa<ObjCCategoryDecl>(CDecl) ? | 
|  | 1060 | diag::warn_setter_getter_impl_required_in_category : | 
|  | 1061 | diag::warn_setter_getter_impl_required) | 
|  | 1062 | << Prop->getDeclName() << Prop->getSetterName(); | 
|  | 1063 | Diag(IMPDecl->getLocation(), | 
|  | 1064 | diag::note_property_impl_required); | 
|  | 1065 | } | 
|  | 1066 | } | 
|  | 1067 | } | 
|  | 1068 |  | 
|  | 1069 | void | 
|  | 1070 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, | 
|  | 1071 | ObjCContainerDecl* IDecl) { | 
|  | 1072 | // Rules apply in non-GC mode only | 
|  | 1073 | if (getLangOptions().getGCMode() != LangOptions::NonGC) | 
|  | 1074 | return; | 
|  | 1075 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), | 
|  | 1076 | E = IDecl->prop_end(); | 
|  | 1077 | I != E; ++I) { | 
|  | 1078 | ObjCPropertyDecl *Property = (*I); | 
|  | 1079 | unsigned Attributes = Property->getPropertyAttributes(); | 
|  | 1080 | // We only care about readwrite atomic property. | 
|  | 1081 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || | 
|  | 1082 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) | 
|  | 1083 | continue; | 
|  | 1084 | if (const ObjCPropertyImplDecl *PIDecl | 
|  | 1085 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { | 
|  | 1086 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) | 
|  | 1087 | continue; | 
|  | 1088 | ObjCMethodDecl *GetterMethod = | 
|  | 1089 | IMPDecl->getInstanceMethod(Property->getGetterName()); | 
|  | 1090 | ObjCMethodDecl *SetterMethod = | 
|  | 1091 | IMPDecl->getInstanceMethod(Property->getSetterName()); | 
|  | 1092 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { | 
|  | 1093 | SourceLocation MethodLoc = | 
|  | 1094 | (GetterMethod ? GetterMethod->getLocation() | 
|  | 1095 | : SetterMethod->getLocation()); | 
|  | 1096 | Diag(MethodLoc, diag::warn_atomic_property_rule) | 
|  | 1097 | << Property->getIdentifier(); | 
|  | 1098 | Diag(Property->getLocation(), diag::note_property_declare); | 
|  | 1099 | } | 
|  | 1100 | } | 
|  | 1101 | } | 
|  | 1102 | } | 
|  | 1103 |  | 
| John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1104 | /// AddPropertyAttrs - Propagates attributes from a property to the | 
|  | 1105 | /// implicitly-declared getter or setter for that property. | 
|  | 1106 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, | 
|  | 1107 | ObjCPropertyDecl *Property) { | 
|  | 1108 | // Should we just clone all attributes over? | 
|  | 1109 | if (DeprecatedAttr *A = Property->getAttr<DeprecatedAttr>()) | 
|  | 1110 | PropertyMethod->addAttr(A->clone(S.Context)); | 
|  | 1111 | if (UnavailableAttr *A = Property->getAttr<UnavailableAttr>()) | 
|  | 1112 | PropertyMethod->addAttr(A->clone(S.Context)); | 
|  | 1113 | } | 
|  | 1114 |  | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1115 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods | 
|  | 1116 | /// have the property type and issue diagnostics if they don't. | 
|  | 1117 | /// Also synthesize a getter/setter method if none exist (and update the | 
|  | 1118 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized | 
|  | 1119 | /// methods is the "right" thing to do. | 
|  | 1120 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, | 
| Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1121 | ObjCContainerDecl *CD, | 
|  | 1122 | ObjCPropertyDecl *redeclaredProperty, | 
|  | 1123 | ObjCContainerDecl *lexicalDC) { | 
|  | 1124 |  | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1125 | ObjCMethodDecl *GetterMethod, *SetterMethod; | 
|  | 1126 |  | 
|  | 1127 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); | 
|  | 1128 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); | 
|  | 1129 | DiagnosePropertyAccessorMismatch(property, GetterMethod, | 
|  | 1130 | property->getLocation()); | 
|  | 1131 |  | 
|  | 1132 | if (SetterMethod) { | 
|  | 1133 | ObjCPropertyDecl::PropertyAttributeKind CAttr = | 
|  | 1134 | property->getPropertyAttributes(); | 
|  | 1135 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && | 
|  | 1136 | Context.getCanonicalType(SetterMethod->getResultType()) != | 
|  | 1137 | Context.VoidTy) | 
|  | 1138 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); | 
|  | 1139 | if (SetterMethod->param_size() != 1 || | 
|  | 1140 | ((*SetterMethod->param_begin())->getType() != property->getType())) { | 
|  | 1141 | Diag(property->getLocation(), | 
|  | 1142 | diag::warn_accessor_property_type_mismatch) | 
|  | 1143 | << property->getDeclName() | 
|  | 1144 | << SetterMethod->getSelector(); | 
|  | 1145 | Diag(SetterMethod->getLocation(), diag::note_declared_at); | 
|  | 1146 | } | 
|  | 1147 | } | 
|  | 1148 |  | 
|  | 1149 | // Synthesize getter/setter methods if none exist. | 
|  | 1150 | // Find the default getter and if one not found, add one. | 
|  | 1151 | // FIXME: The synthesized property we set here is misleading. We almost always | 
|  | 1152 | // synthesize these methods unless the user explicitly provided prototypes | 
|  | 1153 | // (which is odd, but allowed). Sema should be typechecking that the | 
|  | 1154 | // declarations jive in that situation (which it is not currently). | 
|  | 1155 | if (!GetterMethod) { | 
|  | 1156 | // No instance method of same name as property getter name was found. | 
|  | 1157 | // Declare a getter method and add it to the list of methods | 
|  | 1158 | // for this class. | 
| Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1159 | SourceLocation Loc = redeclaredProperty ? | 
|  | 1160 | redeclaredProperty->getLocation() : | 
|  | 1161 | property->getLocation(); | 
|  | 1162 |  | 
|  | 1163 | GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, | 
|  | 1164 | property->getGetterName(), | 
| Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1165 | property->getType(), 0, CD, true, false, true, | 
|  | 1166 | false, | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1167 | (property->getPropertyImplementation() == | 
|  | 1168 | ObjCPropertyDecl::Optional) ? | 
|  | 1169 | ObjCMethodDecl::Optional : | 
|  | 1170 | ObjCMethodDecl::Required); | 
|  | 1171 | CD->addDecl(GetterMethod); | 
| John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1172 |  | 
|  | 1173 | AddPropertyAttrs(*this, GetterMethod, property); | 
|  | 1174 |  | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1175 | // FIXME: Eventually this shouldn't be needed, as the lexical context | 
|  | 1176 | // and the real context should be the same. | 
| Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1177 | if (lexicalDC) | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1178 | GetterMethod->setLexicalDeclContext(lexicalDC); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1179 | } else | 
|  | 1180 | // A user declared getter will be synthesize when @synthesize of | 
|  | 1181 | // the property with the same name is seen in the @implementation | 
|  | 1182 | GetterMethod->setSynthesized(true); | 
|  | 1183 | property->setGetterMethodDecl(GetterMethod); | 
|  | 1184 |  | 
|  | 1185 | // Skip setter if property is read-only. | 
|  | 1186 | if (!property->isReadOnly()) { | 
|  | 1187 | // Find the default setter and if one not found, add one. | 
|  | 1188 | if (!SetterMethod) { | 
|  | 1189 | // No instance method of same name as property setter name was found. | 
|  | 1190 | // Declare a setter method and add it to the list of methods | 
|  | 1191 | // for this class. | 
| Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1192 | SourceLocation Loc = redeclaredProperty ? | 
|  | 1193 | redeclaredProperty->getLocation() : | 
|  | 1194 | property->getLocation(); | 
|  | 1195 |  | 
|  | 1196 | SetterMethod = | 
|  | 1197 | ObjCMethodDecl::Create(Context, Loc, Loc, | 
|  | 1198 | property->getSetterName(), Context.VoidTy, 0, | 
|  | 1199 | CD, true, false, true, false, | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1200 | (property->getPropertyImplementation() == | 
|  | 1201 | ObjCPropertyDecl::Optional) ? | 
| Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1202 | ObjCMethodDecl::Optional : | 
|  | 1203 | ObjCMethodDecl::Required); | 
|  | 1204 |  | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1205 | // Invent the arguments for the setter. We don't bother making a | 
|  | 1206 | // nice name for the argument. | 
| Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1207 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, Loc, | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1208 | property->getIdentifier(), | 
|  | 1209 | property->getType(), | 
|  | 1210 | /*TInfo=*/0, | 
| John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1211 | SC_None, | 
|  | 1212 | SC_None, | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1213 | 0); | 
| Fariborz Jahanian | 4ecb25f | 2010-04-09 15:40:42 +0000 | [diff] [blame] | 1214 | SetterMethod->setMethodParams(Context, &Argument, 1, 1); | 
| John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1215 |  | 
|  | 1216 | AddPropertyAttrs(*this, SetterMethod, property); | 
|  | 1217 |  | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1218 | CD->addDecl(SetterMethod); | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1219 | // FIXME: Eventually this shouldn't be needed, as the lexical context | 
|  | 1220 | // and the real context should be the same. | 
| Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1221 | if (lexicalDC) | 
| Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1222 | SetterMethod->setLexicalDeclContext(lexicalDC); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1223 | } else | 
|  | 1224 | // A user declared setter will be synthesize when @synthesize of | 
|  | 1225 | // the property with the same name is seen in the @implementation | 
|  | 1226 | SetterMethod->setSynthesized(true); | 
|  | 1227 | property->setSetterMethodDecl(SetterMethod); | 
|  | 1228 | } | 
|  | 1229 | // Add any synthesized methods to the global pool. This allows us to | 
|  | 1230 | // handle the following, which is supported by GCC (and part of the design). | 
|  | 1231 | // | 
|  | 1232 | // @interface Foo | 
|  | 1233 | // @property double bar; | 
|  | 1234 | // @end | 
|  | 1235 | // | 
|  | 1236 | // void thisIsUnfortunate() { | 
|  | 1237 | //   id foo; | 
|  | 1238 | //   double bar = [foo bar]; | 
|  | 1239 | // } | 
|  | 1240 | // | 
|  | 1241 | if (GetterMethod) | 
|  | 1242 | AddInstanceMethodToGlobalPool(GetterMethod); | 
|  | 1243 | if (SetterMethod) | 
|  | 1244 | AddInstanceMethodToGlobalPool(SetterMethod); | 
|  | 1245 | } | 
|  | 1246 |  | 
| John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1247 | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1248 | SourceLocation Loc, | 
|  | 1249 | unsigned &Attributes) { | 
|  | 1250 | // FIXME: Improve the reported location. | 
| Ted Kremenek | 5fcd52a | 2010-04-05 22:39:42 +0000 | [diff] [blame] | 1251 | if (!PDecl) | 
|  | 1252 | return; | 
|  | 1253 |  | 
|  | 1254 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); | 
| Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1255 | QualType PropertyTy = PropertyDecl->getType(); | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1256 |  | 
|  | 1257 | // readonly and readwrite/assign/retain/copy conflict. | 
|  | 1258 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && | 
|  | 1259 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | | 
|  | 1260 | ObjCDeclSpec::DQ_PR_assign | | 
|  | 1261 | ObjCDeclSpec::DQ_PR_copy | | 
|  | 1262 | ObjCDeclSpec::DQ_PR_retain))) { | 
|  | 1263 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? | 
|  | 1264 | "readwrite" : | 
|  | 1265 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? | 
|  | 1266 | "assign" : | 
|  | 1267 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? | 
|  | 1268 | "copy" : "retain"; | 
|  | 1269 |  | 
|  | 1270 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? | 
|  | 1271 | diag::err_objc_property_attr_mutually_exclusive : | 
|  | 1272 | diag::warn_objc_property_attr_mutually_exclusive) | 
|  | 1273 | << "readonly" << which; | 
|  | 1274 | } | 
|  | 1275 |  | 
|  | 1276 | // Check for copy or retain on non-object types. | 
|  | 1277 | if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && | 
|  | 1278 | !PropertyTy->isObjCObjectPointerType() && | 
|  | 1279 | !PropertyTy->isBlockPointerType() && | 
| Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1280 | !Context.isObjCNSObjectType(PropertyTy) && | 
|  | 1281 | !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { | 
| Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1282 | Diag(Loc, diag::err_objc_property_requires_object) | 
|  | 1283 | << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); | 
|  | 1284 | Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); | 
|  | 1285 | } | 
|  | 1286 |  | 
|  | 1287 | // Check for more than one of { assign, copy, retain }. | 
|  | 1288 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { | 
|  | 1289 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { | 
|  | 1290 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) | 
|  | 1291 | << "assign" << "copy"; | 
|  | 1292 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; | 
|  | 1293 | } | 
|  | 1294 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { | 
|  | 1295 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) | 
|  | 1296 | << "assign" << "retain"; | 
|  | 1297 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; | 
|  | 1298 | } | 
|  | 1299 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { | 
|  | 1300 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { | 
|  | 1301 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) | 
|  | 1302 | << "copy" << "retain"; | 
|  | 1303 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; | 
|  | 1304 | } | 
|  | 1305 | } | 
|  | 1306 |  | 
|  | 1307 | // Warn if user supplied no assignment attribute, property is | 
|  | 1308 | // readwrite, and this is an object type. | 
|  | 1309 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | | 
|  | 1310 | ObjCDeclSpec::DQ_PR_retain)) && | 
|  | 1311 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && | 
|  | 1312 | PropertyTy->isObjCObjectPointerType()) { | 
|  | 1313 | // Skip this warning in gc-only mode. | 
|  | 1314 | if (getLangOptions().getGCMode() != LangOptions::GCOnly) | 
|  | 1315 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); | 
|  | 1316 |  | 
|  | 1317 | // If non-gc code warn that this is likely inappropriate. | 
|  | 1318 | if (getLangOptions().getGCMode() == LangOptions::NonGC) | 
|  | 1319 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); | 
|  | 1320 |  | 
|  | 1321 | // FIXME: Implement warning dependent on NSCopying being | 
|  | 1322 | // implemented. See also: | 
|  | 1323 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> | 
|  | 1324 | // (please trim this list while you are at it). | 
|  | 1325 | } | 
|  | 1326 |  | 
|  | 1327 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) | 
|  | 1328 | && getLangOptions().getGCMode() == LangOptions::GCOnly | 
|  | 1329 | && PropertyTy->isBlockPointerType()) | 
|  | 1330 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); | 
|  | 1331 | } |