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