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