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