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