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" |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Argyrios Kyrtzidis | c80553e | 2011-11-14 04:52:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTMutationListener.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Grammar actions. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 30 | /// getImpliedARCOwnership - Given a set of property attributes and a |
| 31 | /// type, infer an expected lifetime. The type's ownership qualification |
| 32 | /// is not considered. |
| 33 | /// |
| 34 | /// Returns OCL_None if the attributes as stated do not imply an ownership. |
| 35 | /// Never returns OCL_Autoreleasing. |
| 36 | static Qualifiers::ObjCLifetime getImpliedARCOwnership( |
| 37 | ObjCPropertyDecl::PropertyAttributeKind attrs, |
| 38 | QualType type) { |
| 39 | // retain, strong, copy, weak, and unsafe_unretained are only legal |
| 40 | // on properties of retainable pointer type. |
| 41 | if (attrs & (ObjCPropertyDecl::OBJC_PR_retain | |
| 42 | ObjCPropertyDecl::OBJC_PR_strong | |
| 43 | ObjCPropertyDecl::OBJC_PR_copy)) { |
Fariborz Jahanian | 5fa065b | 2011-10-13 23:45:45 +0000 | [diff] [blame] | 44 | return type->getObjCARCImplicitLifetime(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 45 | } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) { |
| 46 | return Qualifiers::OCL_Weak; |
| 47 | } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) { |
| 48 | return Qualifiers::OCL_ExplicitNone; |
| 49 | } |
| 50 | |
| 51 | // assign can appear on other types, so we have to check the |
| 52 | // property type. |
| 53 | if (attrs & ObjCPropertyDecl::OBJC_PR_assign && |
| 54 | type->isObjCRetainableType()) { |
| 55 | return Qualifiers::OCL_ExplicitNone; |
| 56 | } |
| 57 | |
| 58 | return Qualifiers::OCL_None; |
| 59 | } |
| 60 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 61 | /// Check the internal consistency of a property declaration. |
| 62 | static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) { |
| 63 | if (property->isInvalidDecl()) return; |
| 64 | |
| 65 | ObjCPropertyDecl::PropertyAttributeKind propertyKind |
| 66 | = property->getPropertyAttributes(); |
| 67 | Qualifiers::ObjCLifetime propertyLifetime |
| 68 | = property->getType().getObjCLifetime(); |
| 69 | |
| 70 | // Nothing to do if we don't have a lifetime. |
| 71 | if (propertyLifetime == Qualifiers::OCL_None) return; |
| 72 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 73 | Qualifiers::ObjCLifetime expectedLifetime |
| 74 | = getImpliedARCOwnership(propertyKind, property->getType()); |
| 75 | if (!expectedLifetime) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 76 | // We have a lifetime qualifier but no dominating property |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 77 | // attribute. That's okay, but restore reasonable invariants by |
| 78 | // setting the property attribute according to the lifetime |
| 79 | // qualifier. |
| 80 | ObjCPropertyDecl::PropertyAttributeKind attr; |
| 81 | if (propertyLifetime == Qualifiers::OCL_Strong) { |
| 82 | attr = ObjCPropertyDecl::OBJC_PR_strong; |
| 83 | } else if (propertyLifetime == Qualifiers::OCL_Weak) { |
| 84 | attr = ObjCPropertyDecl::OBJC_PR_weak; |
| 85 | } else { |
| 86 | assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); |
| 87 | attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
| 88 | } |
| 89 | property->setPropertyAttributes(attr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (propertyLifetime == expectedLifetime) return; |
| 94 | |
| 95 | property->setInvalidDecl(); |
| 96 | S.Diag(property->getLocation(), |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 97 | diag::err_arc_inconsistent_property_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 98 | << property->getDeclName() |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 99 | << expectedLifetime |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 100 | << propertyLifetime; |
| 101 | } |
| 102 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 103 | Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 104 | SourceLocation LParenLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 105 | FieldDeclarator &FD, |
| 106 | ObjCDeclSpec &ODS, |
| 107 | Selector GetterSel, |
| 108 | Selector SetterSel, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 109 | bool *isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 110 | tok::ObjCKeywordKind MethodImplKind, |
| 111 | DeclContext *lexicalDC) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 112 | unsigned Attributes = ODS.getPropertyAttributes(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 113 | TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); |
| 114 | QualType T = TSI->getType(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 115 | if ((getLangOpts().getGC() != LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 116 | T.isObjCGCWeak()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 117 | (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 118 | T.getObjCLifetime() == Qualifiers::OCL_Weak)) |
| 119 | Attributes |= ObjCDeclSpec::DQ_PR_weak; |
| 120 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 121 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
| 122 | // default is readwrite! |
| 123 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
| 124 | // property is defaulted to 'assign' if it is readwrite and is |
| 125 | // not retain or copy |
| 126 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
| 127 | (isReadWrite && |
| 128 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 129 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 130 | !(Attributes & ObjCDeclSpec::DQ_PR_copy) && |
| 131 | !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) && |
| 132 | !(Attributes & ObjCDeclSpec::DQ_PR_weak))); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 134 | // Proceed with constructing the ObjCPropertDecls. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 135 | ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 137 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 138 | if (CDecl->IsClassExtension()) { |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 139 | Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc, |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 140 | FD, GetterSel, SetterSel, |
| 141 | isAssign, isReadWrite, |
| 142 | Attributes, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 143 | ODS.getPropertyAttributes(), |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 144 | isOverridingProperty, TSI, |
| 145 | MethodImplKind); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 146 | if (Res) { |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 147 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 148 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 149 | checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res)); |
| 150 | } |
Fariborz Jahanian | ae415dc | 2010-07-13 22:04:56 +0000 | [diff] [blame] | 151 | return Res; |
| 152 | } |
| 153 | |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 154 | ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 155 | GetterSel, SetterSel, |
| 156 | isAssign, isReadWrite, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 157 | Attributes, |
| 158 | ODS.getPropertyAttributes(), |
| 159 | TSI, MethodImplKind); |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 160 | if (lexicalDC) |
| 161 | Res->setLexicalDeclContext(lexicalDC); |
| 162 | |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 163 | // Validate the attributes on the @property. |
| 164 | CheckObjCPropertyAttributes(Res, AtLoc, Attributes); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 165 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 166 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 167 | checkARCPropertyDecl(*this, Res); |
| 168 | |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 169 | return Res; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 170 | } |
Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 171 | |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 172 | static ObjCPropertyDecl::PropertyAttributeKind |
| 173 | makePropertyAttributesAsWritten(unsigned Attributes) { |
| 174 | unsigned attributesAsWritten = 0; |
| 175 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 176 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly; |
| 177 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
| 178 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite; |
| 179 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
| 180 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter; |
| 181 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
| 182 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter; |
| 183 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) |
| 184 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign; |
| 185 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 186 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain; |
| 187 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
| 188 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong; |
| 189 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
| 190 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak; |
| 191 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 192 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy; |
| 193 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
| 194 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained; |
| 195 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
| 196 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic; |
| 197 | if (Attributes & ObjCDeclSpec::DQ_PR_atomic) |
| 198 | attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic; |
| 199 | |
| 200 | return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten; |
| 201 | } |
| 202 | |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame^] | 203 | static std::string getPropertyAttributeString(const ObjCPropertyDecl *property, |
| 204 | unsigned Attributes) { |
| 205 | std::string attr; |
| 206 | if (!Attributes) |
| 207 | return attr; |
| 208 | attr = "("; |
| 209 | bool first = true; |
| 210 | if (Attributes & ObjCPropertyDecl::OBJC_PR_readonly) |
| 211 | {attr += !first ? ", readonly" : "readonly"; first = false; } |
| 212 | if (Attributes & ObjCPropertyDecl::OBJC_PR_readwrite) |
| 213 | {attr += !first ? ", readwrite" : "readwrite"; first = false; } |
| 214 | if (Attributes & ObjCPropertyDecl::OBJC_PR_getter) |
| 215 | { |
| 216 | if (!first) |
| 217 | attr += ", "; |
| 218 | attr += "getter="; |
| 219 | attr += property->getGetterName().getAsString(); |
| 220 | first = false; |
| 221 | } |
| 222 | if (Attributes & ObjCPropertyDecl::OBJC_PR_setter) |
| 223 | { |
| 224 | if (!first) |
| 225 | attr += ", "; |
| 226 | attr += "setter="; |
| 227 | attr += property->getSetterName().getAsString(); |
| 228 | first = false; |
| 229 | } |
| 230 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) |
| 231 | {attr += !first ? ", assign" : "assign"; first = false; } |
| 232 | if (Attributes & ObjCPropertyDecl::OBJC_PR_retain) |
| 233 | {attr += !first ? ", retain" : "retain"; first = false; } |
| 234 | if (Attributes & ObjCPropertyDecl::OBJC_PR_strong) |
| 235 | {attr += !first ? ", strong" : "strong"; first = false; } |
| 236 | if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) |
| 237 | {attr += !first ? ", weak" : "weak"; first = false; } |
| 238 | if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) |
| 239 | {attr += !first ? ", copy" : "copy"; first = false; } |
| 240 | if (Attributes & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) |
| 241 | {attr += !first ? ", unsafe_unretained" : "unsafe_unretained"; first = false; } |
| 242 | if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 243 | {attr += !first ? ", nonatomic" : "nonatomic"; first = false; } |
| 244 | if (Attributes & ObjCPropertyDecl::OBJC_PR_atomic) |
| 245 | {attr += !first ? ", atomic" : "atomic"; first = false; } |
| 246 | attr += ")"; |
| 247 | return attr; |
| 248 | } |
| 249 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 250 | Decl * |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 251 | Sema::HandlePropertyInClassExtension(Scope *S, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 252 | SourceLocation AtLoc, |
| 253 | SourceLocation LParenLoc, |
| 254 | FieldDeclarator &FD, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 255 | Selector GetterSel, Selector SetterSel, |
| 256 | const bool isAssign, |
| 257 | const bool isReadWrite, |
| 258 | const unsigned Attributes, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 259 | const unsigned AttributesAsWritten, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 260 | bool *isOverridingProperty, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 261 | TypeSourceInfo *T, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 262 | tok::ObjCKeywordKind MethodImplKind) { |
Fariborz Jahanian | 58a7649 | 2011-08-22 18:34:22 +0000 | [diff] [blame] | 263 | ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 264 | // Diagnose if this property is already in continuation class. |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 265 | DeclContext *DC = CurContext; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 266 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Fariborz Jahanian | 2a28914 | 2010-11-10 18:01:36 +0000 | [diff] [blame] | 267 | ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); |
| 268 | |
| 269 | if (CCPrimary) |
| 270 | // Check for duplicate declaration of this property in current and |
| 271 | // other class extensions. |
| 272 | for (const ObjCCategoryDecl *ClsExtDecl = |
| 273 | CCPrimary->getFirstClassExtension(); |
| 274 | ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) { |
| 275 | if (ObjCPropertyDecl *prevDecl = |
| 276 | ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) { |
| 277 | Diag(AtLoc, diag::err_duplicate_property); |
| 278 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
| 279 | return 0; |
| 280 | } |
| 281 | } |
| 282 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 283 | // Create a new ObjCPropertyDecl with the DeclContext being |
| 284 | // the class extension. |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 285 | // FIXME. We should really be using CreatePropertyDecl for this. |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 286 | ObjCPropertyDecl *PDecl = |
| 287 | ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(), |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 288 | PropertyId, AtLoc, LParenLoc, T); |
Argyrios Kyrtzidis | b98ffde | 2011-10-18 19:49:16 +0000 | [diff] [blame] | 289 | PDecl->setPropertyAttributesAsWritten( |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 290 | makePropertyAttributesAsWritten(AttributesAsWritten)); |
Fariborz Jahanian | 22f757b | 2010-03-22 23:25:52 +0000 | [diff] [blame] | 291 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 292 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 293 | if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) |
| 294 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Fariborz Jahanian | 88f5e9b | 2010-12-10 23:36:33 +0000 | [diff] [blame] | 295 | // Set setter/getter selector name. Needed later. |
| 296 | PDecl->setGetterName(GetterSel); |
| 297 | PDecl->setSetterName(SetterSel); |
Douglas Gregor | 91ae6b4 | 2011-07-15 15:30:21 +0000 | [diff] [blame] | 298 | ProcessDeclAttributes(S, PDecl, FD.D); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 299 | DC->addDecl(PDecl); |
| 300 | |
| 301 | // We need to look in the @interface to see if the @property was |
| 302 | // already declared. |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 303 | if (!CCPrimary) { |
| 304 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 305 | *isOverridingProperty = true; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 306 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Find the property in continuation class's primary class only. |
| 310 | ObjCPropertyDecl *PIDecl = |
| 311 | CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId); |
| 312 | |
| 313 | if (!PIDecl) { |
| 314 | // No matching property found in the primary class. Just fall thru |
| 315 | // and add property to continuation class's primary class. |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 316 | ObjCPropertyDecl *PrimaryPDecl = |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 317 | CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 318 | FD, GetterSel, SetterSel, isAssign, isReadWrite, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 319 | Attributes,AttributesAsWritten, T, MethodImplKind, DC); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 320 | |
| 321 | // A case of continuation class adding a new property in the class. This |
| 322 | // is not what it was meant for. However, gcc supports it and so should we. |
| 323 | // Make sure setter/getters are declared here. |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 324 | ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0, |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 325 | /* lexicalDC = */ CDecl); |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 326 | PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl()); |
| 327 | PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl()); |
Argyrios Kyrtzidis | c80553e | 2011-11-14 04:52:29 +0000 | [diff] [blame] | 328 | if (ASTMutationListener *L = Context.getASTMutationListener()) |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 329 | L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl); |
| 330 | return PrimaryPDecl; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 331 | } |
Fariborz Jahanian | e235183 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 332 | if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) { |
| 333 | bool IncompatibleObjC = false; |
| 334 | QualType ConvertedType; |
Fariborz Jahanian | ff2a0ec | 2012-02-02 19:34:05 +0000 | [diff] [blame] | 335 | // Relax the strict type matching for property type in continuation class. |
| 336 | // Allow property object type of continuation class to be different as long |
Fariborz Jahanian | ad7eff2 | 2012-02-02 22:37:48 +0000 | [diff] [blame] | 337 | // as it narrows the object type in its primary class property. Note that |
| 338 | // this conversion is safe only because the wider type is for a 'readonly' |
| 339 | // property in primary class and 'narrowed' type for a 'readwrite' property |
| 340 | // in continuation class. |
Fariborz Jahanian | e235183 | 2012-02-02 18:54:58 +0000 | [diff] [blame] | 341 | if (!isa<ObjCObjectPointerType>(PIDecl->getType()) || |
| 342 | !isa<ObjCObjectPointerType>(PDecl->getType()) || |
| 343 | (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(), |
| 344 | ConvertedType, IncompatibleObjC)) |
| 345 | || IncompatibleObjC) { |
| 346 | Diag(AtLoc, |
| 347 | diag::err_type_mismatch_continuation_class) << PDecl->getType(); |
| 348 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 349 | } |
Fariborz Jahanian | a4b984d | 2011-09-24 00:56:59 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 352 | // The property 'PIDecl's readonly attribute will be over-ridden |
| 353 | // with continuation class's readwrite property attribute! |
Fariborz Jahanian | 80aa1cd | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 354 | unsigned PIkind = PIDecl->getPropertyAttributesAsWritten(); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 355 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
| 356 | unsigned retainCopyNonatomic = |
| 357 | (ObjCPropertyDecl::OBJC_PR_retain | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 358 | ObjCPropertyDecl::OBJC_PR_strong | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 359 | ObjCPropertyDecl::OBJC_PR_copy | |
| 360 | ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 361 | if ((Attributes & retainCopyNonatomic) != |
| 362 | (PIkind & retainCopyNonatomic)) { |
| 363 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
| 364 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 365 | } |
Ted Kremenek | 9944c76 | 2010-03-18 01:22:36 +0000 | [diff] [blame] | 366 | DeclContext *DC = cast<DeclContext>(CCPrimary); |
| 367 | if (!ObjCPropertyDecl::findPropertyDecl(DC, |
| 368 | PIDecl->getDeclName().getAsIdentifierInfo())) { |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 369 | // Protocol is not in the primary class. Must build one for it. |
| 370 | ObjCDeclSpec ProtocolPropertyODS; |
| 371 | // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind |
| 372 | // and ObjCPropertyDecl::PropertyAttributeKind have identical |
| 373 | // values. Should consolidate both into one enum type. |
| 374 | ProtocolPropertyODS. |
| 375 | setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind) |
| 376 | PIkind); |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 377 | // Must re-establish the context from class extension to primary |
| 378 | // class context. |
Fariborz Jahanian | 7939418 | 2011-08-22 20:15:24 +0000 | [diff] [blame] | 379 | ContextRAII SavedContext(*this, CCPrimary); |
| 380 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 381 | Decl *ProtocolPtrTy = |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 382 | ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 383 | PIDecl->getGetterName(), |
| 384 | PIDecl->getSetterName(), |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 385 | isOverridingProperty, |
Ted Kremenek | 4a2e9ea | 2010-09-23 21:18:05 +0000 | [diff] [blame] | 386 | MethodImplKind, |
| 387 | /* lexicalDC = */ CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 388 | PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 389 | } |
| 390 | PIDecl->makeitReadWriteAttribute(); |
| 391 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 392 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 393 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
| 394 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 395 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 396 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 397 | PIDecl->setSetterName(SetterSel); |
| 398 | } else { |
Ted Kremenek | 788f489 | 2010-10-21 18:49:42 +0000 | [diff] [blame] | 399 | // Tailor the diagnostics for the common case where a readwrite |
| 400 | // property is declared both in the @interface and the continuation. |
| 401 | // This is a common error where the user often intended the original |
| 402 | // declaration to be readonly. |
| 403 | unsigned diag = |
| 404 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite) && |
| 405 | (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) |
| 406 | ? diag::err_use_continuation_class_redeclaration_readwrite |
| 407 | : diag::err_use_continuation_class; |
| 408 | Diag(AtLoc, diag) |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 409 | << CCPrimary->getDeclName(); |
| 410 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 411 | } |
| 412 | *isOverridingProperty = true; |
| 413 | // 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] | 414 | ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl); |
Argyrios Kyrtzidis | d7c15a6 | 2012-02-28 17:50:28 +0000 | [diff] [blame] | 415 | PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl()); |
| 416 | PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl()); |
Argyrios Kyrtzidis | c80553e | 2011-11-14 04:52:29 +0000 | [diff] [blame] | 417 | if (ASTMutationListener *L = Context.getASTMutationListener()) |
| 418 | L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 419 | return 0; |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, |
| 423 | ObjCContainerDecl *CDecl, |
| 424 | SourceLocation AtLoc, |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 425 | SourceLocation LParenLoc, |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 426 | FieldDeclarator &FD, |
| 427 | Selector GetterSel, |
| 428 | Selector SetterSel, |
| 429 | const bool isAssign, |
| 430 | const bool isReadWrite, |
| 431 | const unsigned Attributes, |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 432 | const unsigned AttributesAsWritten, |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 433 | TypeSourceInfo *TInfo, |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 434 | tok::ObjCKeywordKind MethodImplKind, |
| 435 | DeclContext *lexicalDC){ |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 436 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
John McCall | 83a230c | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 437 | QualType T = TInfo->getType(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 438 | |
| 439 | // Issue a warning if property is 'assign' as default and its object, which is |
| 440 | // gc'able conforms to NSCopying protocol |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 441 | if (getLangOpts().getGC() != LangOptions::NonGC && |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 442 | isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 443 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 444 | T->getAs<ObjCObjectPointerType>()) { |
| 445 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 446 | if (IDecl) |
| 447 | if (ObjCProtocolDecl* PNSCopying = |
| 448 | LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) |
| 449 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
| 450 | Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 451 | } |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 452 | if (T->isObjCObjectType()) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 453 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); |
| 454 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 455 | DeclContext *DC = cast<DeclContext>(CDecl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 456 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
| 457 | FD.D.getIdentifierLoc(), |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 458 | PropertyId, AtLoc, LParenLoc, TInfo); |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 9f550ff | 2010-03-15 20:11:46 +0000 | [diff] [blame] | 460 | if (ObjCPropertyDecl *prevDecl = |
| 461 | ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 462 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
Ted Kremenek | 894ae6a | 2010-03-15 18:47:25 +0000 | [diff] [blame] | 463 | Diag(prevDecl->getLocation(), diag::note_property_declare); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 464 | PDecl->setInvalidDecl(); |
| 465 | } |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 466 | else { |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 467 | DC->addDecl(PDecl); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 468 | if (lexicalDC) |
| 469 | PDecl->setLexicalDeclContext(lexicalDC); |
| 470 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 471 | |
| 472 | if (T->isArrayType() || T->isFunctionType()) { |
| 473 | Diag(AtLoc, diag::err_property_type) << T; |
| 474 | PDecl->setInvalidDecl(); |
| 475 | } |
| 476 | |
| 477 | ProcessDeclAttributes(S, PDecl, FD.D); |
| 478 | |
| 479 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 480 | // selector names in anticipation of declaration of setter/getter methods. |
| 481 | PDecl->setGetterName(GetterSel); |
| 482 | PDecl->setSetterName(SetterSel); |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 483 | PDecl->setPropertyAttributesAsWritten( |
Argyrios Kyrtzidis | dbbdec9 | 2011-11-06 18:58:12 +0000 | [diff] [blame] | 484 | makePropertyAttributesAsWritten(AttributesAsWritten)); |
Argyrios Kyrtzidis | 0a68dc7 | 2011-07-12 04:30:16 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 486 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
| 487 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 488 | |
| 489 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
| 490 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
| 491 | |
| 492 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
| 493 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
| 494 | |
| 495 | if (isReadWrite) |
| 496 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
| 497 | |
| 498 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 499 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 500 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 501 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) |
| 502 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 503 | |
| 504 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) |
| 505 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); |
| 506 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 507 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 508 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 509 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 510 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
| 511 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 512 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 513 | if (isAssign) |
| 514 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 515 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 516 | // In the semantic attributes, one of nonatomic or atomic is always set. |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 517 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
| 518 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 519 | else |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 520 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 521 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 522 | // 'unsafe_unretained' is alias for 'assign'. |
| 523 | if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) |
| 524 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 525 | if (isAssign) |
| 526 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); |
| 527 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 528 | if (MethodImplKind == tok::objc_required) |
| 529 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 530 | else if (MethodImplKind == tok::objc_optional) |
| 531 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | e3d67bc | 2010-03-12 02:31:10 +0000 | [diff] [blame] | 533 | return PDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 534 | } |
| 535 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 536 | static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, |
| 537 | ObjCPropertyDecl *property, |
| 538 | ObjCIvarDecl *ivar) { |
| 539 | if (property->isInvalidDecl() || ivar->isInvalidDecl()) return; |
| 540 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 541 | QualType ivarType = ivar->getType(); |
| 542 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 543 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 544 | // The lifetime implied by the property's attributes. |
| 545 | Qualifiers::ObjCLifetime propertyLifetime = |
| 546 | getImpliedARCOwnership(property->getPropertyAttributes(), |
| 547 | property->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 548 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 549 | // We're fine if they match. |
| 550 | if (propertyLifetime == ivarLifetime) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 551 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 552 | // These aren't valid lifetimes for object ivars; don't diagnose twice. |
| 553 | if (ivarLifetime == Qualifiers::OCL_None || |
| 554 | ivarLifetime == Qualifiers::OCL_Autoreleasing) |
| 555 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 556 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 557 | switch (propertyLifetime) { |
| 558 | case Qualifiers::OCL_Strong: |
| 559 | S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership) |
| 560 | << property->getDeclName() |
| 561 | << ivar->getDeclName() |
| 562 | << ivarLifetime; |
| 563 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 564 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 565 | case Qualifiers::OCL_Weak: |
| 566 | S.Diag(propertyImplLoc, diag::error_weak_property) |
| 567 | << property->getDeclName() |
| 568 | << ivar->getDeclName(); |
| 569 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 570 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 571 | case Qualifiers::OCL_ExplicitNone: |
| 572 | S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership) |
| 573 | << property->getDeclName() |
| 574 | << ivar->getDeclName() |
| 575 | << ((property->getPropertyAttributesAsWritten() |
| 576 | & ObjCPropertyDecl::OBJC_PR_assign) != 0); |
| 577 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 578 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 579 | case Qualifiers::OCL_Autoreleasing: |
| 580 | llvm_unreachable("properties cannot be autoreleasing"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 581 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 582 | case Qualifiers::OCL_None: |
| 583 | // Any other property should be ignored. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
| 587 | S.Diag(property->getLocation(), diag::note_property_declare); |
| 588 | } |
| 589 | |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 590 | /// setImpliedPropertyAttributeForReadOnlyProperty - |
| 591 | /// This routine evaludates life-time attributes for a 'readonly' |
| 592 | /// property with no known lifetime of its own, using backing |
| 593 | /// 'ivar's attribute, if any. If no backing 'ivar', property's |
| 594 | /// life-time is assumed 'strong'. |
| 595 | static void setImpliedPropertyAttributeForReadOnlyProperty( |
| 596 | ObjCPropertyDecl *property, ObjCIvarDecl *ivar) { |
| 597 | Qualifiers::ObjCLifetime propertyLifetime = |
| 598 | getImpliedARCOwnership(property->getPropertyAttributes(), |
| 599 | property->getType()); |
| 600 | if (propertyLifetime != Qualifiers::OCL_None) |
| 601 | return; |
| 602 | |
| 603 | if (!ivar) { |
| 604 | // if no backing ivar, make property 'strong'. |
| 605 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 606 | return; |
| 607 | } |
| 608 | // property assumes owenership of backing ivar. |
| 609 | QualType ivarType = ivar->getType(); |
| 610 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
| 611 | if (ivarLifetime == Qualifiers::OCL_Strong) |
| 612 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
| 613 | else if (ivarLifetime == Qualifiers::OCL_Weak) |
| 614 | property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); |
| 615 | return; |
| 616 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 617 | |
| 618 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 619 | /// builds the AST node for a property implementation declaration; declared |
| 620 | /// as @synthesize or @dynamic. |
| 621 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 622 | Decl *Sema::ActOnPropertyImplDecl(Scope *S, |
| 623 | SourceLocation AtLoc, |
| 624 | SourceLocation PropertyLoc, |
| 625 | bool Synthesize, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 626 | IdentifierInfo *PropertyId, |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 627 | IdentifierInfo *PropertyIvar, |
| 628 | SourceLocation PropertyIvarLoc) { |
Ted Kremenek | e968657 | 2010-04-05 23:45:09 +0000 | [diff] [blame] | 629 | ObjCContainerDecl *ClassImpDecl = |
Fariborz Jahanian | 84e0ccf | 2011-09-19 16:32:32 +0000 | [diff] [blame] | 630 | dyn_cast<ObjCContainerDecl>(CurContext); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 631 | // Make sure we have a context for the property implementation declaration. |
| 632 | if (!ClassImpDecl) { |
| 633 | Diag(AtLoc, diag::error_missing_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 634 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 635 | } |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 636 | if (PropertyIvarLoc.isInvalid()) |
| 637 | PropertyIvarLoc = PropertyLoc; |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 638 | SourceLocation PropertyDiagLoc = PropertyLoc; |
| 639 | if (PropertyDiagLoc.isInvalid()) |
| 640 | PropertyDiagLoc = ClassImpDecl->getLocStart(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 641 | ObjCPropertyDecl *property = 0; |
| 642 | ObjCInterfaceDecl* IDecl = 0; |
| 643 | // Find the class or category class where this property must have |
| 644 | // a declaration. |
| 645 | ObjCImplementationDecl *IC = 0; |
| 646 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 647 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
| 648 | IDecl = IC->getClassInterface(); |
| 649 | // We always synthesize an interface for an implementation |
| 650 | // without an interface decl. So, IDecl is always non-zero. |
| 651 | assert(IDecl && |
| 652 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 653 | |
| 654 | // Look for this property declaration in the @implementation's @interface |
| 655 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 656 | if (!property) { |
| 657 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 658 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 659 | } |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 660 | unsigned PIkind = property->getPropertyAttributesAsWritten(); |
Fariborz Jahanian | 45937ae | 2011-06-11 00:45:12 +0000 | [diff] [blame] | 661 | if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | |
| 662 | ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { |
Fariborz Jahanian | dd4430e | 2010-12-17 22:28:16 +0000 | [diff] [blame] | 663 | if (AtLoc.isValid()) |
| 664 | Diag(AtLoc, diag::warn_implicit_atomic_property); |
| 665 | else |
| 666 | Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); |
| 667 | Diag(property->getLocation(), diag::note_property_declare); |
| 668 | } |
| 669 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 670 | if (const ObjCCategoryDecl *CD = |
| 671 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
| 672 | if (!CD->IsClassExtension()) { |
| 673 | Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); |
| 674 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 675 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
Fariborz Jahanian | 2b309fb | 2012-05-19 18:17:17 +0000 | [diff] [blame^] | 678 | |
| 679 | if (Synthesize&& |
| 680 | (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) && |
| 681 | property->hasAttr<IBOutletAttr>() && |
| 682 | !AtLoc.isValid()) { |
| 683 | unsigned rwPIKind = (PIkind | ObjCPropertyDecl::OBJC_PR_readwrite); |
| 684 | rwPIKind &= (~ObjCPropertyDecl::OBJC_PR_readonly); |
| 685 | Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property); |
| 686 | Diag(property->getLocation(), diag::note_property_declare); |
| 687 | // FIXME. End location must be that of closing ')' which is currently |
| 688 | // unavailable. Need to add it. |
| 689 | SourceLocation endLoc = |
| 690 | property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| 691 | SourceRange PropSourceRange(property->getLParenLoc(), endLoc); |
| 692 | Diag(property->getLocation(), |
| 693 | diag::note_auto_readonly_iboutlet_fixup_suggest) << |
| 694 | FixItHint::CreateReplacement(PropSourceRange, getPropertyAttributeString(property, |
| 695 | rwPIKind)); |
| 696 | } |
| 697 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 698 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
| 699 | if (Synthesize) { |
| 700 | Diag(AtLoc, diag::error_synthesize_category_decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 701 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 702 | } |
| 703 | IDecl = CatImplClass->getClassInterface(); |
| 704 | if (!IDecl) { |
| 705 | Diag(AtLoc, diag::error_missing_property_interface); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 706 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 707 | } |
| 708 | ObjCCategoryDecl *Category = |
| 709 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 710 | |
| 711 | // If category for this implementation not found, it is an error which |
| 712 | // has already been reported eralier. |
| 713 | if (!Category) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 714 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 715 | // Look for this property declaration in @implementation's category |
| 716 | property = Category->FindPropertyDeclaration(PropertyId); |
| 717 | if (!property) { |
| 718 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
| 719 | << Category->getDeclName(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 720 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 721 | } |
| 722 | } else { |
| 723 | Diag(AtLoc, diag::error_bad_property_context); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 724 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 725 | } |
| 726 | ObjCIvarDecl *Ivar = 0; |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 727 | bool CompleteTypeErr = false; |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 728 | bool compat = true; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 729 | // Check that we have a valid, previously declared ivar for @synthesize |
| 730 | if (Synthesize) { |
| 731 | // @synthesize |
| 732 | if (!PropertyIvar) |
| 733 | PropertyIvar = PropertyId; |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 734 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
| 735 | ObjCInterfaceDecl *ClassDeclared; |
| 736 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
| 737 | QualType PropType = property->getType(); |
| 738 | QualType PropertyIvarType = PropType.getNonReferenceType(); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 739 | |
| 740 | if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 741 | diag::err_incomplete_synthesized_property, |
| 742 | property->getDeclName())) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 743 | Diag(property->getLocation(), diag::note_property_declare); |
| 744 | CompleteTypeErr = true; |
| 745 | } |
| 746 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 747 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 748 | (property->getPropertyAttributesAsWritten() & |
Fariborz Jahanian | 3efd348 | 2012-01-11 19:48:08 +0000 | [diff] [blame] | 749 | ObjCPropertyDecl::OBJC_PR_readonly) && |
| 750 | PropertyIvarType->isObjCRetainableType()) { |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 751 | setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar); |
| 752 | } |
| 753 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 754 | ObjCPropertyDecl::PropertyAttributeKind kind |
| 755 | = property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 756 | |
| 757 | // Add GC __weak to the ivar type if the property is weak. |
| 758 | if ((kind & ObjCPropertyDecl::OBJC_PR_weak) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 759 | getLangOpts().getGC() != LangOptions::NonGC) { |
| 760 | assert(!getLangOpts().ObjCAutoRefCount); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 761 | if (PropertyIvarType.isObjCGCStrong()) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 762 | Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 763 | Diag(property->getLocation(), diag::note_property_declare); |
| 764 | } else { |
| 765 | PropertyIvarType = |
| 766 | Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 767 | } |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 768 | } |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 769 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 770 | if (!Ivar) { |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 771 | // In ARC, give the ivar a lifetime qualifier based on the |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 772 | // property attributes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 773 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 774 | !PropertyIvarType.getObjCLifetime() && |
| 775 | PropertyIvarType->isObjCRetainableType()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 776 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 777 | // It's an error if we have to do this and the user didn't |
| 778 | // explicitly write an ownership attribute on the property. |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 779 | if (!property->hasWrittenStorageAttribute() && |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 780 | !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 781 | Diag(PropertyDiagLoc, |
Argyrios Kyrtzidis | 473506b | 2011-07-26 21:48:26 +0000 | [diff] [blame] | 782 | diag::err_arc_objc_property_default_assign_on_object); |
| 783 | Diag(property->getLocation(), diag::note_property_declare); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 784 | } else { |
| 785 | Qualifiers::ObjCLifetime lifetime = |
| 786 | getImpliedARCOwnership(kind, PropertyIvarType); |
| 787 | assert(lifetime && "no lifetime for property?"); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 788 | if (lifetime == Qualifiers::OCL_Weak) { |
| 789 | bool err = false; |
| 790 | if (const ObjCObjectPointerType *ObjT = |
| 791 | PropertyIvarType->getAs<ObjCObjectPointerType>()) |
| 792 | if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 793 | Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 794 | Diag(property->getLocation(), diag::note_property_declare); |
| 795 | err = true; |
| 796 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 797 | if (!err && !getLangOpts().ObjCRuntimeHasWeak) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 798 | Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime); |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 799 | Diag(property->getLocation(), diag::note_property_declare); |
| 800 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 801 | } |
Fariborz Jahanian | 6dce88d | 2011-12-09 19:55:11 +0000 | [diff] [blame] | 802 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 803 | Qualifiers qs; |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 804 | qs.addObjCLifetime(lifetime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 805 | PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); |
| 806 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | if (kind & ObjCPropertyDecl::OBJC_PR_weak && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 810 | !getLangOpts().ObjCAutoRefCount && |
| 811 | getLangOpts().getGC() == LangOptions::NonGC) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 812 | Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 813 | Diag(property->getLocation(), diag::note_property_declare); |
| 814 | } |
| 815 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 816 | Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 817 | PropertyIvarLoc,PropertyIvarLoc, PropertyIvar, |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 818 | PropertyIvarType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 819 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 820 | (Expr *)0, true); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 821 | if (CompleteTypeErr) |
| 822 | Ivar->setInvalidDecl(); |
Daniel Dunbar | 29fa69a | 2010-04-02 19:44:54 +0000 | [diff] [blame] | 823 | ClassImpDecl->addDecl(Ivar); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 824 | IDecl->makeDeclVisibleInContext(Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 825 | property->setPropertyIvarDecl(Ivar); |
| 826 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 827 | if (!getLangOpts().ObjCNonFragileABI) |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 828 | Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl) |
| 829 | << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 830 | // Note! I deliberately want it to fall thru so, we have a |
| 831 | // a property implementation and to avoid future warnings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 832 | } else if (getLangOpts().ObjCNonFragileABI && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 833 | !declaresSameEntity(ClassDeclared, IDecl)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 834 | Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 835 | << property->getDeclName() << Ivar->getDeclName() |
| 836 | << ClassDeclared->getDeclName(); |
| 837 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 838 | << Ivar << Ivar->getName(); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 839 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 840 | } |
| 841 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 842 | |
| 843 | // Check that type of property and its ivar are type compatible. |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 844 | if (!Context.hasSameType(PropertyIvarType, IvarType)) { |
| 845 | compat = false; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 846 | if (isa<ObjCObjectPointerType>(PropertyIvarType) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 847 | && isa<ObjCObjectPointerType>(IvarType)) |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 848 | compat = |
| 849 | Context.canAssignObjCInterfaces( |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 850 | PropertyIvarType->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 851 | IvarType->getAs<ObjCObjectPointerType>()); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 852 | else { |
Argyrios Kyrtzidis | f911242 | 2012-02-28 17:50:39 +0000 | [diff] [blame] | 853 | compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType, |
| 854 | IvarType) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 855 | == Compatible); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 856 | } |
Fariborz Jahanian | 62ac5d0 | 2010-05-18 23:04:17 +0000 | [diff] [blame] | 857 | if (!compat) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 858 | Diag(PropertyDiagLoc, diag::error_property_ivar_type) |
Ted Kremenek | f921a48 | 2010-03-23 19:02:22 +0000 | [diff] [blame] | 859 | << property->getDeclName() << PropType |
| 860 | << Ivar->getDeclName() << IvarType; |
| 861 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 862 | // Note! I deliberately want it to fall thru so, we have a |
| 863 | // a property implementation and to avoid future warnings. |
| 864 | } |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 865 | else { |
| 866 | // FIXME! Rules for properties are somewhat different that those |
| 867 | // for assignments. Use a new routine to consolidate all cases; |
| 868 | // specifically for property redeclarations as well as for ivars. |
| 869 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
| 870 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 871 | if (lhsType != rhsType && |
| 872 | lhsType->isArithmeticType()) { |
| 873 | Diag(PropertyDiagLoc, diag::error_property_ivar_type) |
| 874 | << property->getDeclName() << PropType |
| 875 | << Ivar->getDeclName() << IvarType; |
| 876 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
| 877 | // Fall thru - see previous comment |
| 878 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 879 | } |
| 880 | // __weak is explicit. So it works on Canonical type. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 881 | if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 882 | getLangOpts().getGC() != LangOptions::NonGC)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 883 | Diag(PropertyDiagLoc, diag::error_weak_property) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 884 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | edc0882 | 2011-09-07 16:24:21 +0000 | [diff] [blame] | 885 | Diag(Ivar->getLocation(), diag::note_ivar_decl); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 886 | // Fall thru - see previous comment |
| 887 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 888 | // Fall thru - see previous comment |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 889 | if ((property->getType()->isObjCObjectPointerType() || |
| 890 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 891 | getLangOpts().getGC() != LangOptions::NonGC) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 892 | Diag(PropertyDiagLoc, diag::error_strong_property) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 893 | << property->getDeclName() << Ivar->getDeclName(); |
| 894 | // Fall thru - see previous comment |
| 895 | } |
| 896 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 897 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 898 | checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 899 | } else if (PropertyIvar) |
| 900 | // @dynamic |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 901 | Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 902 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 903 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
| 904 | ObjCPropertyImplDecl *PIDecl = |
| 905 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 906 | property, |
| 907 | (Synthesize ? |
| 908 | ObjCPropertyImplDecl::Synthesize |
| 909 | : ObjCPropertyImplDecl::Dynamic), |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 910 | Ivar, PropertyIvarLoc); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 911 | |
Fariborz Jahanian | 7441471 | 2012-05-15 18:12:51 +0000 | [diff] [blame] | 912 | if (CompleteTypeErr || !compat) |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 913 | PIDecl->setInvalidDecl(); |
| 914 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 915 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
| 916 | getterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 917 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
Fariborz Jahanian | 0313f44 | 2010-10-15 22:42:59 +0000 | [diff] [blame] | 918 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 919 | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
| 920 | // returned by the getter as it must conform to C++'s copy-return rules. |
| 921 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 922 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
| 923 | DeclRefExpr *SelfExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 924 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 925 | VK_RValue, SourceLocation()); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 926 | Expr *IvarRefExpr = |
| 927 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, |
| 928 | SelfExpr, true, true); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 929 | ExprResult Res = |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 930 | PerformCopyInitialization(InitializedEntity::InitializeResult( |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 931 | SourceLocation(), |
| 932 | getterMethod->getResultType(), |
| 933 | /*NRVO=*/false), |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 934 | SourceLocation(), |
| 935 | Owned(IvarRefExpr)); |
| 936 | if (!Res.isInvalid()) { |
| 937 | Expr *ResExpr = Res.takeAs<Expr>(); |
| 938 | if (ResExpr) |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 939 | ResExpr = MaybeCreateExprWithCleanups(ResExpr); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 940 | PIDecl->setGetterCXXConstructor(ResExpr); |
| 941 | } |
| 942 | } |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 943 | if (property->hasAttr<NSReturnsNotRetainedAttr>() && |
| 944 | !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 945 | Diag(getterMethod->getLocation(), |
| 946 | diag::warn_property_getter_owning_mismatch); |
| 947 | Diag(property->getLocation(), diag::note_property_declare); |
| 948 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 949 | } |
| 950 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
| 951 | setterMethod->createImplicitParams(Context, IDecl); |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 952 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
| 953 | Ivar->getType()->isRecordType()) { |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 954 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 955 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
| 956 | DeclRefExpr *SelfExpr = |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 957 | new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 958 | VK_RValue, SourceLocation()); |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 959 | Expr *lhs = |
| 960 | new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, |
| 961 | SelfExpr, true, true); |
| 962 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
| 963 | ParmVarDecl *Param = (*P); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 964 | QualType T = Param->getType().getNonReferenceType(); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 965 | Expr *rhs = new (Context) DeclRefExpr(Param, false, T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 966 | VK_LValue, SourceLocation()); |
Fariborz Jahanian | fa43239 | 2010-10-14 21:30:10 +0000 | [diff] [blame] | 967 | ExprResult Res = BuildBinOp(S, lhs->getLocEnd(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 968 | BO_Assign, lhs, rhs); |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 969 | if (property->getPropertyAttributes() & |
| 970 | ObjCPropertyDecl::OBJC_PR_atomic) { |
| 971 | Expr *callExpr = Res.takeAs<Expr>(); |
| 972 | if (const CXXOperatorCallExpr *CXXCE = |
Fariborz Jahanian | 13bf633 | 2011-10-07 21:08:14 +0000 | [diff] [blame] | 973 | dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) |
| 974 | if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 975 | if (!FuncDecl->isTrivial()) |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 976 | if (property->getType()->isReferenceType()) { |
| 977 | Diag(PropertyLoc, |
| 978 | diag::err_atomic_property_nontrivial_assign_op) |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 979 | << property->getType(); |
Fariborz Jahanian | 20abee6 | 2012-01-10 00:37:01 +0000 | [diff] [blame] | 980 | Diag(FuncDecl->getLocStart(), |
| 981 | diag::note_callee_decl) << FuncDecl; |
| 982 | } |
Fariborz Jahanian | 57e264e | 2011-10-06 18:38:18 +0000 | [diff] [blame] | 983 | } |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 984 | PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); |
| 985 | } |
| 986 | } |
| 987 | |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 988 | if (IC) { |
| 989 | if (Synthesize) |
| 990 | if (ObjCPropertyImplDecl *PPIDecl = |
| 991 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 992 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 993 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 994 | << PropertyIvar; |
| 995 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 996 | } |
| 997 | |
| 998 | if (ObjCPropertyImplDecl *PPIDecl |
| 999 | = IC->FindPropertyImplDecl(PropertyId)) { |
| 1000 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 1001 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1002 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1003 | } |
| 1004 | IC->addPropertyImplementation(PIDecl); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1005 | if (getLangOpts().ObjCDefaultSynthProperties && |
| 1006 | getLangOpts().ObjCNonFragileABI2 && |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1007 | !IDecl->isObjCRequiresPropertyDefs()) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1008 | // Diagnose if an ivar was lazily synthesdized due to a previous |
| 1009 | // use and if 1) property is @dynamic or 2) property is synthesized |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1010 | // but it requires an ivar of different name. |
Fariborz Jahanian | 411c25c | 2011-01-20 23:34:25 +0000 | [diff] [blame] | 1011 | ObjCInterfaceDecl *ClassDeclared=0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1012 | ObjCIvarDecl *Ivar = 0; |
| 1013 | if (!Synthesize) |
| 1014 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1015 | else { |
| 1016 | if (PropertyIvar && PropertyIvar != PropertyId) |
| 1017 | Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); |
| 1018 | } |
Fariborz Jahanian | cdaa6a8 | 2010-08-24 18:48:05 +0000 | [diff] [blame] | 1019 | // Issue diagnostics only if Ivar belongs to current class. |
| 1020 | if (Ivar && Ivar->getSynthesize() && |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1021 | declaresSameEntity(IC->getClassInterface(), ClassDeclared)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1022 | Diag(Ivar->getLocation(), diag::err_undeclared_var_use) |
| 1023 | << PropertyId; |
| 1024 | Ivar->setInvalidDecl(); |
| 1025 | } |
| 1026 | } |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1027 | } else { |
| 1028 | if (Synthesize) |
| 1029 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1030 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1031 | Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use) |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1032 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1033 | << PropertyIvar; |
| 1034 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1035 | } |
| 1036 | |
| 1037 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1038 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
Eli Friedman | e4c043d | 2012-05-01 22:26:06 +0000 | [diff] [blame] | 1039 | Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1040 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1041 | return 0; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1042 | } |
| 1043 | CatImplClass->addPropertyImplementation(PIDecl); |
| 1044 | } |
| 1045 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1046 | return PIDecl; |
Ted Kremenek | 28685ab | 2010-03-12 00:46:40 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | //===----------------------------------------------------------------------===// |
| 1050 | // Helper methods. |
| 1051 | //===----------------------------------------------------------------------===// |
| 1052 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1053 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 1054 | /// attributes and types and warns on a variety of inconsistencies. |
| 1055 | /// |
| 1056 | void |
| 1057 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 1058 | ObjCPropertyDecl *SuperProperty, |
| 1059 | const IdentifierInfo *inheritedName) { |
| 1060 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1061 | Property->getPropertyAttributes(); |
| 1062 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 1063 | SuperProperty->getPropertyAttributes(); |
| 1064 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1065 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1066 | Diag(Property->getLocation(), diag::warn_readonly_property) |
| 1067 | << Property->getDeclName() << inheritedName; |
| 1068 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 1069 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
| 1070 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1071 | << Property->getDeclName() << "copy" << inheritedName; |
Fariborz Jahanian | 1b46d8d | 2011-10-08 17:45:33 +0000 | [diff] [blame] | 1072 | else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){ |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1073 | unsigned CAttrRetain = |
| 1074 | (CAttr & |
| 1075 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1076 | unsigned SAttrRetain = |
| 1077 | (SAttr & |
| 1078 | (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); |
| 1079 | bool CStrong = (CAttrRetain != 0); |
| 1080 | bool SStrong = (SAttrRetain != 0); |
| 1081 | if (CStrong != SStrong) |
| 1082 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1083 | << Property->getDeclName() << "retain (or strong)" << inheritedName; |
| 1084 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1085 | |
| 1086 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 1087 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
| 1088 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1089 | << Property->getDeclName() << "atomic" << inheritedName; |
| 1090 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
| 1091 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1092 | << Property->getDeclName() << "setter" << inheritedName; |
| 1093 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
| 1094 | Diag(Property->getLocation(), diag::warn_property_attribute) |
| 1095 | << Property->getDeclName() << "getter" << inheritedName; |
| 1096 | |
| 1097 | QualType LHSType = |
| 1098 | Context.getCanonicalType(SuperProperty->getType()); |
| 1099 | QualType RHSType = |
| 1100 | Context.getCanonicalType(Property->getType()); |
| 1101 | |
Fariborz Jahanian | c286f38 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 1102 | if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
Fariborz Jahanian | 8beb6a2 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1103 | // Do cases not handled in above. |
| 1104 | // FIXME. For future support of covariant property types, revisit this. |
| 1105 | bool IncompatibleObjC = false; |
| 1106 | QualType ConvertedType; |
| 1107 | if (!isObjCPointerConversion(RHSType, LHSType, |
| 1108 | ConvertedType, IncompatibleObjC) || |
Fariborz Jahanian | 13546a8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1109 | IncompatibleObjC) { |
Fariborz Jahanian | 8beb6a2 | 2011-07-13 17:55:01 +0000 | [diff] [blame] | 1110 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 1111 | << Property->getType() << SuperProperty->getType() << inheritedName; |
Fariborz Jahanian | 13546a8 | 2011-10-12 00:00:57 +0000 | [diff] [blame] | 1112 | Diag(SuperProperty->getLocation(), diag::note_property_declare); |
| 1113 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 1118 | ObjCMethodDecl *GetterMethod, |
| 1119 | SourceLocation Loc) { |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1120 | if (!GetterMethod) |
| 1121 | return false; |
| 1122 | QualType GetterType = GetterMethod->getResultType().getNonReferenceType(); |
| 1123 | QualType PropertyIvarType = property->getType().getNonReferenceType(); |
| 1124 | bool compat = Context.hasSameType(PropertyIvarType, GetterType); |
| 1125 | if (!compat) { |
| 1126 | if (isa<ObjCObjectPointerType>(PropertyIvarType) && |
| 1127 | isa<ObjCObjectPointerType>(GetterType)) |
| 1128 | compat = |
| 1129 | Context.canAssignObjCInterfaces( |
| 1130 | PropertyIvarType->getAs<ObjCObjectPointerType>(), |
| 1131 | GetterType->getAs<ObjCObjectPointerType>()); |
| 1132 | else if (CheckAssignmentConstraints(Loc, PropertyIvarType, GetterType) |
| 1133 | != Compatible) { |
| 1134 | Diag(Loc, diag::error_property_accessor_type) |
| 1135 | << property->getDeclName() << PropertyIvarType |
| 1136 | << GetterMethod->getSelector() << GetterType; |
| 1137 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1138 | return true; |
| 1139 | } else { |
| 1140 | compat = true; |
| 1141 | QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); |
| 1142 | QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType(); |
| 1143 | if (lhsType != rhsType && lhsType->isArithmeticType()) |
| 1144 | compat = false; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
Fariborz Jahanian | 9abf88c | 2012-05-15 22:37:04 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (!compat) { |
| 1149 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
| 1150 | << property->getDeclName() |
| 1151 | << GetterMethod->getSelector(); |
| 1152 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1153 | return true; |
| 1154 | } |
| 1155 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1156 | return false; |
| 1157 | } |
| 1158 | |
| 1159 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 1160 | /// declarations in base and its super class, if any, and issues |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1161 | /// diagnostics in a variety of inconsistent situations. |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1162 | /// |
| 1163 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
| 1164 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 1165 | if (!SDecl) |
| 1166 | return; |
| 1167 | // FIXME: O(N^2) |
| 1168 | for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), |
| 1169 | E = SDecl->prop_end(); S != E; ++S) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1170 | ObjCPropertyDecl *SuperPDecl = &*S; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1171 | // Does property in super class has declaration in current class? |
| 1172 | for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), |
| 1173 | E = IDecl->prop_end(); I != E; ++I) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1174 | ObjCPropertyDecl *PDecl = &*I; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1175 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
| 1176 | DiagnosePropertyMismatch(PDecl, SuperPDecl, |
| 1177 | SDecl->getIdentifier()); |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | /// MatchOneProtocolPropertiesInClass - This routine goes thru the list |
| 1183 | /// of properties declared in a protocol and compares their attribute against |
| 1184 | /// the same property declared in the class or category. |
| 1185 | void |
| 1186 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, |
| 1187 | ObjCProtocolDecl *PDecl) { |
| 1188 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 1189 | if (!IDecl) { |
| 1190 | // Category |
| 1191 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 1192 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); |
| 1193 | if (!CatDecl->IsClassExtension()) |
| 1194 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1195 | E = PDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1196 | ObjCPropertyDecl *Pr = &*P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1197 | ObjCCategoryDecl::prop_iterator CP, CE; |
| 1198 | // Is this property already in category's list of properties? |
Ted Kremenek | 2d2f936 | 2010-03-12 00:49:00 +0000 | [diff] [blame] | 1199 | for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1200 | if (CP->getIdentifier() == Pr->getIdentifier()) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1201 | break; |
| 1202 | if (CP != CE) |
| 1203 | // Property protocol already exist in class. Diagnose any mismatch. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1204 | DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1205 | } |
| 1206 | return; |
| 1207 | } |
| 1208 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1209 | E = PDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1210 | ObjCPropertyDecl *Pr = &*P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1211 | ObjCInterfaceDecl::prop_iterator CP, CE; |
| 1212 | // Is this property already in class's list of properties? |
| 1213 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1214 | if (CP->getIdentifier() == Pr->getIdentifier()) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1215 | break; |
| 1216 | if (CP != CE) |
| 1217 | // Property protocol already exist in class. Diagnose any mismatch. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1218 | DiagnosePropertyMismatch(&*CP, Pr, PDecl->getIdentifier()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /// CompareProperties - This routine compares properties |
| 1223 | /// declared in 'ClassOrProtocol' objects (which can be a class or an |
| 1224 | /// inherited protocol with the list of properties for class/category 'CDecl' |
| 1225 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1226 | void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) { |
| 1227 | Decl *ClassDecl = ClassOrProtocol; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1228 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 1229 | |
| 1230 | if (!IDecl) { |
| 1231 | // Category |
| 1232 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 1233 | assert (CatDecl && "CompareProperties"); |
| 1234 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 1235 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 1236 | E = MDecl->protocol_end(); P != E; ++P) |
| 1237 | // Match properties of category with those of protocol (*P) |
| 1238 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 1239 | |
| 1240 | // Go thru the list of protocols for this category and recursively match |
| 1241 | // their properties with those in the category. |
| 1242 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), |
| 1243 | E = CatDecl->protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1244 | CompareProperties(CatDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1245 | } else { |
| 1246 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 1247 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 1248 | E = MD->protocol_end(); P != E; ++P) |
| 1249 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
| 1250 | } |
| 1251 | return; |
| 1252 | } |
| 1253 | |
| 1254 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1255 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1256 | P = MDecl->all_referenced_protocol_begin(), |
| 1257 | E = MDecl->all_referenced_protocol_end(); P != E; ++P) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1258 | // Match properties of class IDecl with those of protocol (*P). |
| 1259 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 1260 | |
| 1261 | // Go thru the list of protocols for this class and recursively match |
| 1262 | // their properties with those declared in the class. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1263 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1264 | P = IDecl->all_referenced_protocol_begin(), |
| 1265 | E = IDecl->all_referenced_protocol_end(); P != E; ++P) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1266 | CompareProperties(IDecl, *P); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1267 | } else { |
| 1268 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 1269 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 1270 | E = MD->protocol_end(); P != E; ++P) |
| 1271 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 1276 | /// for the property in the class and in its categories and implementations |
| 1277 | /// |
| 1278 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
| 1279 | ObjCInterfaceDecl *IDecl) { |
| 1280 | // by far the most common case. |
| 1281 | if (!PDecl->isReadOnly()) |
| 1282 | return false; |
| 1283 | // Even if property is ready only, if interface has a user defined setter, |
| 1284 | // it is not considered read only. |
| 1285 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1286 | return false; |
| 1287 | |
| 1288 | // Main class has the property as 'readonly'. Must search |
| 1289 | // through the category list to see if the property's |
| 1290 | // attribute has been over-ridden to 'readwrite'. |
| 1291 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); |
| 1292 | Category; Category = Category->getNextClassCategory()) { |
| 1293 | // Even if property is ready only, if a category has a user defined setter, |
| 1294 | // it is not considered read only. |
| 1295 | if (Category->getInstanceMethod(PDecl->getSetterName())) |
| 1296 | return false; |
| 1297 | ObjCPropertyDecl *P = |
| 1298 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
| 1299 | if (P && !P->isReadOnly()) |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
| 1303 | // Also, check for definition of a setter method in the implementation if |
| 1304 | // all else failed. |
| 1305 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
| 1306 | if (ObjCImplementationDecl *IMD = |
| 1307 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
| 1308 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
| 1309 | return false; |
| 1310 | } else if (ObjCCategoryImplDecl *CIMD = |
| 1311 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 1312 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
| 1313 | return false; |
| 1314 | } |
| 1315 | } |
| 1316 | // Lastly, look through the implementation (if one is in scope). |
| 1317 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) |
| 1318 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
| 1319 | return false; |
| 1320 | // If all fails, look at the super class. |
| 1321 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 1322 | return isPropertyReadonly(PDecl, SIDecl); |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | /// CollectImmediateProperties - This routine collects all properties in |
| 1327 | /// the class and its conforming protocols; but not those it its super class. |
| 1328 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1329 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap, |
| 1330 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1331 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1332 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1333 | E = IDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1334 | ObjCPropertyDecl *Prop = &*P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1335 | PropMap[Prop->getIdentifier()] = Prop; |
| 1336 | } |
| 1337 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1338 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1339 | PI = IDecl->all_referenced_protocol_begin(), |
| 1340 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1341 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1342 | } |
| 1343 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 1344 | if (!CATDecl->IsClassExtension()) |
| 1345 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), |
| 1346 | E = CATDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1347 | ObjCPropertyDecl *Prop = &*P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1348 | PropMap[Prop->getIdentifier()] = Prop; |
| 1349 | } |
| 1350 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1351 | for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1352 | E = CATDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1353 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1354 | } |
| 1355 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1356 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1357 | E = PDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1358 | ObjCPropertyDecl *Prop = &*P; |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1359 | ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; |
| 1360 | // Exclude property for protocols which conform to class's super-class, |
| 1361 | // as super-class has to implement the property. |
Fariborz Jahanian | a929ec7 | 2011-09-27 00:23:52 +0000 | [diff] [blame] | 1362 | if (!PropertyFromSuper || |
| 1363 | PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1364 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; |
| 1365 | if (!PropEntry) |
| 1366 | PropEntry = Prop; |
| 1367 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1368 | } |
| 1369 | // scan through protocol's protocols. |
| 1370 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1371 | E = PDecl->protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1372 | CollectImmediateProperties((*PI), PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1376 | /// CollectClassPropertyImplementations - This routine collects list of |
| 1377 | /// properties to be implemented in the class. This includes, class's |
| 1378 | /// and its conforming protocols' properties. |
| 1379 | static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl, |
| 1380 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 1381 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1382 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1383 | E = IDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1384 | ObjCPropertyDecl *Prop = &*P; |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1385 | PropMap[Prop->getIdentifier()] = Prop; |
| 1386 | } |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1387 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1388 | PI = IDecl->all_referenced_protocol_begin(), |
| 1389 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1390 | CollectClassPropertyImplementations((*PI), PropMap); |
| 1391 | } |
| 1392 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1393 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1394 | E = PDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1395 | ObjCPropertyDecl *Prop = &*P; |
Fariborz Jahanian | ac37150 | 2012-02-23 18:21:25 +0000 | [diff] [blame] | 1396 | if (!PropMap.count(Prop->getIdentifier())) |
| 1397 | PropMap[Prop->getIdentifier()] = Prop; |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1398 | } |
| 1399 | // scan through protocol's protocols. |
| 1400 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1401 | E = PDecl->protocol_end(); PI != E; ++PI) |
| 1402 | CollectClassPropertyImplementations((*PI), PropMap); |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | /// CollectSuperClassPropertyImplementations - This routine collects list of |
| 1407 | /// properties to be implemented in super class(s) and also coming from their |
| 1408 | /// conforming protocols. |
| 1409 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
| 1410 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 1411 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
| 1412 | while (SDecl) { |
| 1413 | CollectClassPropertyImplementations(SDecl, PropMap); |
| 1414 | SDecl = SDecl->getSuperClass(); |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1419 | /// LookupPropertyDecl - Looks up a property in the current class and all |
| 1420 | /// its protocols. |
| 1421 | ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl, |
| 1422 | IdentifierInfo *II) { |
| 1423 | if (const ObjCInterfaceDecl *IDecl = |
| 1424 | dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1425 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1426 | E = IDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1427 | ObjCPropertyDecl *Prop = &*P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1428 | if (Prop->getIdentifier() == II) |
| 1429 | return Prop; |
| 1430 | } |
| 1431 | // scan through class's protocols. |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1432 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 1433 | PI = IDecl->all_referenced_protocol_begin(), |
| 1434 | E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1435 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1436 | if (Prop) |
| 1437 | return Prop; |
| 1438 | } |
| 1439 | } |
| 1440 | else if (const ObjCProtocolDecl *PDecl = |
| 1441 | dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1442 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1443 | E = PDecl->prop_end(); P != E; ++P) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1444 | ObjCPropertyDecl *Prop = &*P; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1445 | if (Prop->getIdentifier() == II) |
| 1446 | return Prop; |
| 1447 | } |
| 1448 | // scan through protocol's protocols. |
| 1449 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1450 | E = PDecl->protocol_end(); PI != E; ++PI) { |
| 1451 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1452 | if (Prop) |
| 1453 | return Prop; |
| 1454 | } |
| 1455 | } |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1459 | static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop, |
| 1460 | ASTContext &Ctx) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1461 | SmallString<128> ivarName; |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1462 | { |
| 1463 | llvm::raw_svector_ostream os(ivarName); |
| 1464 | os << '_' << Prop->getIdentifier()->getName(); |
| 1465 | } |
| 1466 | return &Ctx.Idents.get(ivarName.str()); |
| 1467 | } |
| 1468 | |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1469 | /// DefaultSynthesizeProperties - This routine default synthesizes all |
| 1470 | /// properties which must be synthesized in class's @implementation. |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame] | 1471 | void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl, |
| 1472 | ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1473 | |
| 1474 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
| 1475 | CollectClassPropertyImplementations(IDecl, PropMap); |
| 1476 | if (PropMap.empty()) |
| 1477 | return; |
| 1478 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; |
| 1479 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1480 | |
| 1481 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 1482 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1483 | ObjCPropertyDecl *Prop = P->second; |
| 1484 | // If property to be implemented in the super class, ignore. |
| 1485 | if (SuperPropMap[Prop->getIdentifier()]) |
| 1486 | continue; |
| 1487 | // Is there a matching propery synthesize/dynamic? |
| 1488 | if (Prop->isInvalidDecl() || |
| 1489 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 1490 | IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) |
| 1491 | continue; |
Fariborz Jahanian | d3635b9 | 2010-07-14 18:11:52 +0000 | [diff] [blame] | 1492 | // Property may have been synthesized by user. |
| 1493 | if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) |
| 1494 | continue; |
Fariborz Jahanian | 95f1b86 | 2010-08-25 00:31:58 +0000 | [diff] [blame] | 1495 | if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { |
| 1496 | if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) |
| 1497 | continue; |
| 1498 | if (IMPDecl->getInstanceMethod(Prop->getSetterName())) |
| 1499 | continue; |
| 1500 | } |
Fariborz Jahanian | f8aba8c | 2011-12-15 01:03:18 +0000 | [diff] [blame] | 1501 | if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) { |
| 1502 | // We won't auto-synthesize properties declared in protocols. |
| 1503 | Diag(IMPDecl->getLocation(), |
| 1504 | diag::warn_auto_synthesizing_protocol_property); |
| 1505 | Diag(Prop->getLocation(), diag::note_property_declare); |
| 1506 | continue; |
| 1507 | } |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1508 | |
| 1509 | // We use invalid SourceLocations for the synthesized ivars since they |
| 1510 | // aren't really synthesized at a particular location; they just exist. |
| 1511 | // Saying that they are located at the @implementation isn't really going |
| 1512 | // to help users. |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1513 | ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( |
| 1514 | ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), |
| 1515 | true, |
| 1516 | /* property = */ Prop->getIdentifier(), |
| 1517 | /* ivar = */ getDefaultSynthIvarName(Prop, Context), |
| 1518 | SourceLocation())); |
| 1519 | if (PIDecl) { |
| 1520 | Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis); |
Fariborz Jahanian | 5ea6661 | 2012-05-08 18:03:39 +0000 | [diff] [blame] | 1521 | Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); |
Fariborz Jahanian | 975eef6 | 2012-05-03 16:43:30 +0000 | [diff] [blame] | 1522 | } |
Ted Kremenek | 2a6af6b | 2010-09-24 01:23:01 +0000 | [diff] [blame] | 1523 | } |
Fariborz Jahanian | 509d477 | 2010-05-14 18:35:57 +0000 | [diff] [blame] | 1524 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1525 | |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1526 | void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) { |
| 1527 | if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2) |
| 1528 | return; |
| 1529 | ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); |
| 1530 | if (!IC) |
| 1531 | return; |
| 1532 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1533 | if (!IDecl->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | eb4f2c5 | 2012-01-03 19:46:00 +0000 | [diff] [blame] | 1534 | DefaultSynthesizeProperties(S, IC, IDecl); |
Fariborz Jahanian | 8697d30 | 2011-08-31 22:24:06 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Fariborz Jahanian | 17cb326 | 2010-05-05 21:52:17 +0000 | [diff] [blame] | 1537 | void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1538 | ObjCContainerDecl *CDecl, |
| 1539 | const llvm::DenseSet<Selector>& InsMap) { |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1540 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; |
| 1541 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
| 1542 | CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); |
| 1543 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1544 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
Fariborz Jahanian | cfa6a27 | 2010-06-29 18:12:32 +0000 | [diff] [blame] | 1545 | CollectImmediateProperties(CDecl, PropMap, SuperPropMap); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1546 | if (PropMap.empty()) |
| 1547 | return; |
| 1548 | |
| 1549 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 1550 | for (ObjCImplDecl::propimpl_iterator |
| 1551 | I = IMPDecl->propimpl_begin(), |
| 1552 | EI = IMPDecl->propimpl_end(); I != EI; ++I) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1553 | PropImplMap.insert(I->getPropertyDecl()); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1554 | |
| 1555 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 1556 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1557 | ObjCPropertyDecl *Prop = P->second; |
| 1558 | // Is there a matching propery synthesize/dynamic? |
| 1559 | if (Prop->isInvalidDecl() || |
| 1560 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
Fariborz Jahanian | 327126e | 2011-06-24 20:31:37 +0000 | [diff] [blame] | 1561 | PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>()) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1562 | continue; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1563 | if (!InsMap.count(Prop->getGetterName())) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1564 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1565 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1566 | diag::warn_setter_getter_impl_required_in_category : |
| 1567 | diag::warn_setter_getter_impl_required) |
| 1568 | << Prop->getDeclName() << Prop->getGetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1569 | Diag(Prop->getLocation(), |
| 1570 | diag::note_property_declare); |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1571 | if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) |
| 1572 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1573 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1574 | Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
| 1575 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1579 | Diag(IMPDecl->getLocation(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1580 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1581 | diag::warn_setter_getter_impl_required_in_category : |
| 1582 | diag::warn_setter_getter_impl_required) |
| 1583 | << Prop->getDeclName() << Prop->getSetterName(); |
Fariborz Jahanian | b860739 | 2011-08-27 21:55:47 +0000 | [diff] [blame] | 1584 | Diag(Prop->getLocation(), |
| 1585 | diag::note_property_declare); |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1586 | if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) |
| 1587 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) |
Ted Kremenek | 71207fc | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 1588 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
Fariborz Jahanian | da611a7 | 2012-01-04 23:16:13 +0000 | [diff] [blame] | 1589 | Diag(RID->getLocation(), diag::note_suppressed_class_declare); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | void |
| 1595 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
| 1596 | ObjCContainerDecl* IDecl) { |
| 1597 | // Rules apply in non-GC mode only |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1598 | if (getLangOpts().getGC() != LangOptions::NonGC) |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1599 | return; |
| 1600 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), |
| 1601 | E = IDecl->prop_end(); |
| 1602 | I != E; ++I) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1603 | ObjCPropertyDecl *Property = &*I; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1604 | ObjCMethodDecl *GetterMethod = 0; |
| 1605 | ObjCMethodDecl *SetterMethod = 0; |
| 1606 | bool LookedUpGetterSetter = false; |
| 1607 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1608 | unsigned Attributes = Property->getPropertyAttributes(); |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1609 | unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1610 | |
John McCall | 265941b | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 1611 | if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && |
| 1612 | !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1613 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1614 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1615 | LookedUpGetterSetter = true; |
| 1616 | if (GetterMethod) { |
| 1617 | Diag(GetterMethod->getLocation(), |
| 1618 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1619 | << Property->getIdentifier() << 0; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1620 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1621 | } |
| 1622 | if (SetterMethod) { |
| 1623 | Diag(SetterMethod->getLocation(), |
| 1624 | diag::warn_default_atomic_custom_getter_setter) |
Argyrios Kyrtzidis | 293a45e | 2011-01-31 23:20:03 +0000 | [diff] [blame] | 1625 | << Property->getIdentifier() << 1; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1626 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1627 | } |
| 1628 | } |
| 1629 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1630 | // We only care about readwrite atomic property. |
| 1631 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 1632 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1633 | continue; |
| 1634 | if (const ObjCPropertyImplDecl *PIDecl |
| 1635 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { |
| 1636 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1637 | continue; |
Argyrios Kyrtzidis | 94659e4 | 2011-01-31 21:34:11 +0000 | [diff] [blame] | 1638 | if (!LookedUpGetterSetter) { |
| 1639 | GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1640 | SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1641 | LookedUpGetterSetter = true; |
| 1642 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1643 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 1644 | SourceLocation MethodLoc = |
| 1645 | (GetterMethod ? GetterMethod->getLocation() |
| 1646 | : SetterMethod->getLocation()); |
| 1647 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
Fariborz Jahanian | 7d65f69 | 2011-10-06 23:47:58 +0000 | [diff] [blame] | 1648 | << Property->getIdentifier() << (GetterMethod != 0) |
| 1649 | << (SetterMethod != 0); |
Fariborz Jahanian | 77bfb8b | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 1650 | // fixit stuff. |
| 1651 | if (!AttributesAsWritten) { |
| 1652 | if (Property->getLParenLoc().isValid()) { |
| 1653 | // @property () ... case. |
| 1654 | SourceRange PropSourceRange(Property->getAtLoc(), |
| 1655 | Property->getLParenLoc()); |
| 1656 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1657 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic"); |
| 1658 | } |
| 1659 | else { |
| 1660 | //@property id etc. |
| 1661 | SourceLocation endLoc = |
| 1662 | Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| 1663 | endLoc = endLoc.getLocWithOffset(-1); |
| 1664 | SourceRange PropSourceRange(Property->getAtLoc(), endLoc); |
| 1665 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1666 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) "); |
| 1667 | } |
| 1668 | } |
| 1669 | else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) { |
| 1670 | // @property () ... case. |
| 1671 | SourceLocation endLoc = Property->getLParenLoc(); |
| 1672 | SourceRange PropSourceRange(Property->getAtLoc(), endLoc); |
| 1673 | Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << |
| 1674 | FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, "); |
| 1675 | } |
| 1676 | else |
| 1677 | Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1678 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1684 | void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1685 | if (getLangOpts().getGC() == LangOptions::GCOnly) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1686 | return; |
| 1687 | |
| 1688 | for (ObjCImplementationDecl::propimpl_iterator |
| 1689 | i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1690 | ObjCPropertyImplDecl *PID = &*i; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1691 | if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 1692 | continue; |
| 1693 | |
| 1694 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1695 | if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1696 | !D->getInstanceMethod(PD->getGetterName())) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1697 | ObjCMethodDecl *method = PD->getGetterMethodDecl(); |
| 1698 | if (!method) |
| 1699 | continue; |
| 1700 | ObjCMethodFamily family = method->getMethodFamily(); |
| 1701 | if (family == OMF_alloc || family == OMF_copy || |
| 1702 | family == OMF_mutableCopy || family == OMF_new) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1703 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1704 | Diag(PID->getLocation(), diag::err_ownin_getter_rule); |
| 1705 | else |
Ted Kremenek | 920c9c1 | 2011-10-12 18:03:37 +0000 | [diff] [blame] | 1706 | Diag(PID->getLocation(), diag::warn_owning_getter_rule); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1707 | Diag(PD->getLocation(), diag::note_property_declare); |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1713 | /// AddPropertyAttrs - Propagates attributes from a property to the |
| 1714 | /// implicitly-declared getter or setter for that property. |
| 1715 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
| 1716 | ObjCPropertyDecl *Property) { |
| 1717 | // Should we just clone all attributes over? |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1718 | for (Decl::attr_iterator A = Property->attr_begin(), |
| 1719 | AEnd = Property->attr_end(); |
| 1720 | A != AEnd; ++A) { |
| 1721 | if (isa<DeprecatedAttr>(*A) || |
| 1722 | isa<UnavailableAttr>(*A) || |
| 1723 | isa<AvailabilityAttr>(*A)) |
| 1724 | PropertyMethod->addAttr((*A)->clone(S.Context)); |
| 1725 | } |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1728 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 1729 | /// have the property type and issue diagnostics if they don't. |
| 1730 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1731 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1732 | /// methods is the "right" thing to do. |
| 1733 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1734 | ObjCContainerDecl *CD, |
| 1735 | ObjCPropertyDecl *redeclaredProperty, |
| 1736 | ObjCContainerDecl *lexicalDC) { |
| 1737 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1738 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 1739 | |
| 1740 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
| 1741 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
| 1742 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
| 1743 | property->getLocation()); |
| 1744 | |
| 1745 | if (SetterMethod) { |
| 1746 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1747 | property->getPropertyAttributes(); |
| 1748 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && |
| 1749 | Context.getCanonicalType(SetterMethod->getResultType()) != |
| 1750 | Context.VoidTy) |
| 1751 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
| 1752 | if (SetterMethod->param_size() != 1 || |
Fariborz Jahanian | 2aac0c9 | 2011-09-26 22:59:09 +0000 | [diff] [blame] | 1753 | !Context.hasSameUnqualifiedType( |
Fariborz Jahanian | bb13c32 | 2011-10-15 17:36:49 +0000 | [diff] [blame] | 1754 | (*SetterMethod->param_begin())->getType().getNonReferenceType(), |
| 1755 | property->getType().getNonReferenceType())) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1756 | Diag(property->getLocation(), |
| 1757 | diag::warn_accessor_property_type_mismatch) |
| 1758 | << property->getDeclName() |
| 1759 | << SetterMethod->getSelector(); |
| 1760 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // Synthesize getter/setter methods if none exist. |
| 1765 | // Find the default getter and if one not found, add one. |
| 1766 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 1767 | // synthesize these methods unless the user explicitly provided prototypes |
| 1768 | // (which is odd, but allowed). Sema should be typechecking that the |
| 1769 | // declarations jive in that situation (which it is not currently). |
| 1770 | if (!GetterMethod) { |
| 1771 | // No instance method of same name as property getter name was found. |
| 1772 | // Declare a getter method and add it to the list of methods |
| 1773 | // for this class. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1774 | SourceLocation Loc = redeclaredProperty ? |
| 1775 | redeclaredProperty->getLocation() : |
| 1776 | property->getLocation(); |
| 1777 | |
| 1778 | GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, |
| 1779 | property->getGetterName(), |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1780 | property->getType(), 0, CD, /*isInstance=*/true, |
| 1781 | /*isVariadic=*/false, /*isSynthesized=*/true, |
| 1782 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1783 | (property->getPropertyImplementation() == |
| 1784 | ObjCPropertyDecl::Optional) ? |
| 1785 | ObjCMethodDecl::Optional : |
| 1786 | ObjCMethodDecl::Required); |
| 1787 | CD->addDecl(GetterMethod); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1788 | |
| 1789 | AddPropertyAttrs(*this, GetterMethod, property); |
| 1790 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1791 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1792 | // and the real context should be the same. |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1793 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1794 | GetterMethod->setLexicalDeclContext(lexicalDC); |
Fariborz Jahanian | 831fb96 | 2011-06-25 00:17:46 +0000 | [diff] [blame] | 1795 | if (property->hasAttr<NSReturnsNotRetainedAttr>()) |
| 1796 | GetterMethod->addAttr( |
| 1797 | ::new (Context) NSReturnsNotRetainedAttr(Loc, Context)); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1798 | } else |
| 1799 | // A user declared getter will be synthesize when @synthesize of |
| 1800 | // the property with the same name is seen in the @implementation |
| 1801 | GetterMethod->setSynthesized(true); |
| 1802 | property->setGetterMethodDecl(GetterMethod); |
| 1803 | |
| 1804 | // Skip setter if property is read-only. |
| 1805 | if (!property->isReadOnly()) { |
| 1806 | // Find the default setter and if one not found, add one. |
| 1807 | if (!SetterMethod) { |
| 1808 | // No instance method of same name as property setter name was found. |
| 1809 | // Declare a setter method and add it to the list of methods |
| 1810 | // for this class. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1811 | SourceLocation Loc = redeclaredProperty ? |
| 1812 | redeclaredProperty->getLocation() : |
| 1813 | property->getLocation(); |
| 1814 | |
| 1815 | SetterMethod = |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 1816 | ObjCMethodDecl::Create(Context, Loc, Loc, |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1817 | property->getSetterName(), Context.VoidTy, 0, |
Argyrios Kyrtzidis | 75cf3e8 | 2011-08-17 19:25:08 +0000 | [diff] [blame] | 1818 | CD, /*isInstance=*/true, /*isVariadic=*/false, |
| 1819 | /*isSynthesized=*/true, |
| 1820 | /*isImplicitlyDeclared=*/true, |
| 1821 | /*isDefined=*/false, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1822 | (property->getPropertyImplementation() == |
| 1823 | ObjCPropertyDecl::Optional) ? |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1824 | ObjCMethodDecl::Optional : |
| 1825 | ObjCMethodDecl::Required); |
| 1826 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1827 | // Invent the arguments for the setter. We don't bother making a |
| 1828 | // nice name for the argument. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1829 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
| 1830 | Loc, Loc, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1831 | property->getIdentifier(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1832 | property->getType().getUnqualifiedType(), |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1833 | /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1834 | SC_None, |
| 1835 | SC_None, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1836 | 0); |
Argyrios Kyrtzidis | 491306a | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 1837 | SetterMethod->setMethodParams(Context, Argument, |
| 1838 | ArrayRef<SourceLocation>()); |
John McCall | 5de74d1 | 2010-11-10 07:01:40 +0000 | [diff] [blame] | 1839 | |
| 1840 | AddPropertyAttrs(*this, SetterMethod, property); |
| 1841 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1842 | CD->addDecl(SetterMethod); |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1843 | // FIXME: Eventually this shouldn't be needed, as the lexical context |
| 1844 | // and the real context should be the same. |
Ted Kremenek | 8254aa6 | 2010-09-21 18:28:43 +0000 | [diff] [blame] | 1845 | if (lexicalDC) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1846 | SetterMethod->setLexicalDeclContext(lexicalDC); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1847 | } else |
| 1848 | // A user declared setter will be synthesize when @synthesize of |
| 1849 | // the property with the same name is seen in the @implementation |
| 1850 | SetterMethod->setSynthesized(true); |
| 1851 | property->setSetterMethodDecl(SetterMethod); |
| 1852 | } |
| 1853 | // Add any synthesized methods to the global pool. This allows us to |
| 1854 | // handle the following, which is supported by GCC (and part of the design). |
| 1855 | // |
| 1856 | // @interface Foo |
| 1857 | // @property double bar; |
| 1858 | // @end |
| 1859 | // |
| 1860 | // void thisIsUnfortunate() { |
| 1861 | // id foo; |
| 1862 | // double bar = [foo bar]; |
| 1863 | // } |
| 1864 | // |
| 1865 | if (GetterMethod) |
| 1866 | AddInstanceMethodToGlobalPool(GetterMethod); |
| 1867 | if (SetterMethod) |
| 1868 | AddInstanceMethodToGlobalPool(SetterMethod); |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 1869 | |
| 1870 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); |
| 1871 | if (!CurrentClass) { |
| 1872 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) |
| 1873 | CurrentClass = Cat->getClassInterface(); |
| 1874 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) |
| 1875 | CurrentClass = Impl->getClassInterface(); |
| 1876 | } |
| 1877 | if (GetterMethod) |
| 1878 | CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); |
| 1879 | if (SetterMethod) |
| 1880 | CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1883 | void Sema::CheckObjCPropertyAttributes(Decl *PDecl, |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1884 | SourceLocation Loc, |
| 1885 | unsigned &Attributes) { |
| 1886 | // FIXME: Improve the reported location. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1887 | if (!PDecl || PDecl->isInvalidDecl()) |
Ted Kremenek | 5fcd52a | 2010-04-05 22:39:42 +0000 | [diff] [blame] | 1888 | return; |
| 1889 | |
| 1890 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1891 | QualType PropertyTy = PropertyDecl->getType(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1892 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1893 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 015f608 | 2012-01-11 18:26:06 +0000 | [diff] [blame] | 1894 | (Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1895 | PropertyTy->isObjCRetainableType()) { |
| 1896 | // 'readonly' property with no obvious lifetime. |
| 1897 | // its life time will be determined by its backing ivar. |
| 1898 | unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 1899 | ObjCDeclSpec::DQ_PR_copy | |
| 1900 | ObjCDeclSpec::DQ_PR_retain | |
| 1901 | ObjCDeclSpec::DQ_PR_strong | |
| 1902 | ObjCDeclSpec::DQ_PR_weak | |
| 1903 | ObjCDeclSpec::DQ_PR_assign); |
| 1904 | if ((Attributes & rel) == 0) |
| 1905 | return; |
| 1906 | } |
| 1907 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1908 | // readonly and readwrite/assign/retain/copy conflict. |
| 1909 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1910 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 1911 | ObjCDeclSpec::DQ_PR_assign | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1912 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1913 | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1914 | ObjCDeclSpec::DQ_PR_retain | |
| 1915 | ObjCDeclSpec::DQ_PR_strong))) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1916 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
| 1917 | "readwrite" : |
| 1918 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
| 1919 | "assign" : |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1920 | (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? |
| 1921 | "unsafe_unretained" : |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1922 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
| 1923 | "copy" : "retain"; |
| 1924 | |
| 1925 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
| 1926 | diag::err_objc_property_attr_mutually_exclusive : |
| 1927 | diag::warn_objc_property_attr_mutually_exclusive) |
| 1928 | << "readonly" << which; |
| 1929 | } |
| 1930 | |
| 1931 | // Check for copy or retain on non-object types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1932 | if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
| 1933 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && |
| 1934 | !PropertyTy->isObjCRetainableType() && |
Fariborz Jahanian | 842f07b | 2010-03-30 22:40:11 +0000 | [diff] [blame] | 1935 | !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1936 | Diag(Loc, diag::err_objc_property_requires_object) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1937 | << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : |
| 1938 | Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); |
| 1939 | Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | |
| 1940 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); |
John McCall | 977ea78 | 2012-02-21 21:48:05 +0000 | [diff] [blame] | 1941 | PropertyDecl->setInvalidDecl(); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | // Check for more than one of { assign, copy, retain }. |
| 1945 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 1946 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1947 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1948 | << "assign" << "copy"; |
| 1949 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
| 1950 | } |
| 1951 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1952 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1953 | << "assign" << "retain"; |
| 1954 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1955 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1956 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
| 1957 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1958 | << "assign" << "strong"; |
| 1959 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
| 1960 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1961 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1962 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 1963 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1964 | << "assign" << "weak"; |
| 1965 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
| 1966 | } |
| 1967 | } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { |
| 1968 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1969 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1970 | << "unsafe_unretained" << "copy"; |
| 1971 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
| 1972 | } |
| 1973 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1974 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1975 | << "unsafe_unretained" << "retain"; |
| 1976 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1977 | } |
| 1978 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
| 1979 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1980 | << "unsafe_unretained" << "strong"; |
| 1981 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
| 1982 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1983 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1984 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 1985 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1986 | << "unsafe_unretained" << "weak"; |
| 1987 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
| 1988 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 1989 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1990 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
| 1991 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1992 | << "copy" << "retain"; |
| 1993 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
| 1994 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1995 | if (Attributes & ObjCDeclSpec::DQ_PR_strong) { |
| 1996 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1997 | << "copy" << "strong"; |
| 1998 | Attributes &= ~ObjCDeclSpec::DQ_PR_strong; |
| 1999 | } |
| 2000 | if (Attributes & ObjCDeclSpec::DQ_PR_weak) { |
| 2001 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2002 | << "copy" << "weak"; |
| 2003 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
| 2004 | } |
| 2005 | } |
| 2006 | else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2007 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 2008 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2009 | << "retain" << "weak"; |
Fariborz Jahanian | 528a499 | 2011-09-14 18:03:46 +0000 | [diff] [blame] | 2010 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2011 | } |
| 2012 | else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 2013 | (Attributes & ObjCDeclSpec::DQ_PR_weak)) { |
| 2014 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2015 | << "strong" << "weak"; |
| 2016 | Attributes &= ~ObjCDeclSpec::DQ_PR_weak; |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Fariborz Jahanian | 9d1bbea | 2011-10-10 21:53:24 +0000 | [diff] [blame] | 2019 | if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && |
| 2020 | (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { |
| 2021 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2022 | << "atomic" << "nonatomic"; |
| 2023 | Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; |
| 2024 | } |
| 2025 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2026 | // Warn if user supplied no assignment attribute, property is |
| 2027 | // readwrite, and this is an object type. |
| 2028 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2029 | ObjCDeclSpec::DQ_PR_unsafe_unretained | |
| 2030 | ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong | |
| 2031 | ObjCDeclSpec::DQ_PR_weak)) && |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2032 | PropertyTy->isObjCObjectPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2033 | if (getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2034 | // With arc, @property definitions should default to (strong) when |
Fariborz Jahanian | f21a92d | 2011-11-08 20:58:53 +0000 | [diff] [blame] | 2035 | // not specified; including when property is 'readonly'. |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2036 | PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); |
Fariborz Jahanian | f21a92d | 2011-11-08 20:58:53 +0000 | [diff] [blame] | 2037 | else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) { |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2038 | bool isAnyClassTy = |
| 2039 | (PropertyTy->isObjCClassType() || |
| 2040 | PropertyTy->isObjCQualifiedClassType()); |
| 2041 | // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to |
| 2042 | // issue any warning. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2043 | if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2044 | ; |
| 2045 | else { |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2046 | // Skip this warning in gc-only mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2047 | if (getLangOpts().getGC() != LangOptions::GCOnly) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2048 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2049 | |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2050 | // If non-gc code warn that this is likely inappropriate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2051 | if (getLangOpts().getGC() == LangOptions::NonGC) |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2052 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
Fariborz Jahanian | 9f37cd1 | 2012-01-04 00:31:53 +0000 | [diff] [blame] | 2053 | } |
Fariborz Jahanian | bc03aea | 2011-08-19 19:28:44 +0000 | [diff] [blame] | 2054 | } |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2055 | |
| 2056 | // FIXME: Implement warning dependent on NSCopying being |
| 2057 | // implemented. See also: |
| 2058 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 2059 | // (please trim this list while you are at it). |
| 2060 | } |
| 2061 | |
| 2062 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
Fariborz Jahanian | 2b77cb8 | 2011-01-05 23:00:04 +0000 | [diff] [blame] | 2063 | &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2064 | && getLangOpts().getGC() == LangOptions::GCOnly |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2065 | && PropertyTy->isBlockPointerType()) |
| 2066 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2067 | else if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 528a499 | 2011-09-14 18:03:46 +0000 | [diff] [blame] | 2068 | (Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 2069 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2070 | !(Attributes & ObjCDeclSpec::DQ_PR_strong) && |
| 2071 | PropertyTy->isBlockPointerType()) |
| 2072 | Diag(Loc, diag::warn_objc_property_retain_of_block); |
Fariborz Jahanian | 48a98c7 | 2011-11-01 23:02:16 +0000 | [diff] [blame] | 2073 | |
| 2074 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 2075 | (Attributes & ObjCDeclSpec::DQ_PR_setter)) |
| 2076 | Diag(Loc, diag::warn_objc_readonly_property_has_setter); |
| 2077 | |
Ted Kremenek | 9d64c15 | 2010-03-12 00:38:38 +0000 | [diff] [blame] | 2078 | } |